You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2631 lines
91KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "internal.h"
  34. #include "mathops.h"
  35. #include "mpegvideo.h"
  36. #include "mjpegenc.h"
  37. #include "msmpeg4.h"
  38. #include "xvmc_internal.h"
  39. #include "thread.h"
  40. #include <limits.h>
  41. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  42. int16_t *block, int n, int qscale);
  43. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  44. int16_t *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  46. int16_t *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  48. int16_t *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  50. int16_t *block, int n, int qscale);
  51. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  52. int16_t *block, int n, int qscale);
  53. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  54. int16_t *block, int n, int qscale);
  55. static const uint8_t ff_default_chroma_qscale_table[32] = {
  56. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  57. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  58. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  59. };
  60. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  61. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  62. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  63. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  64. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. };
  71. static const uint8_t mpeg2_dc_scale_table1[128] = {
  72. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  78. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  79. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  80. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  81. };
  82. static const uint8_t mpeg2_dc_scale_table2[128] = {
  83. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  84. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  85. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  86. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  90. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  91. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  92. };
  93. static const uint8_t mpeg2_dc_scale_table3[128] = {
  94. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  95. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  96. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  97. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  98. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  99. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  100. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  101. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  102. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  103. };
  104. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  105. ff_mpeg1_dc_scale_table,
  106. mpeg2_dc_scale_table1,
  107. mpeg2_dc_scale_table2,
  108. mpeg2_dc_scale_table3,
  109. };
  110. const enum AVPixelFormat ff_pixfmt_list_420[] = {
  111. AV_PIX_FMT_YUV420P,
  112. AV_PIX_FMT_NONE
  113. };
  114. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  115. int (*mv)[2][4][2],
  116. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  117. {
  118. MpegEncContext *s = opaque;
  119. s->mv_dir = mv_dir;
  120. s->mv_type = mv_type;
  121. s->mb_intra = mb_intra;
  122. s->mb_skipped = mb_skipped;
  123. s->mb_x = mb_x;
  124. s->mb_y = mb_y;
  125. memcpy(s->mv, mv, sizeof(*mv));
  126. ff_init_block_index(s);
  127. ff_update_block_index(s);
  128. s->dsp.clear_blocks(s->block[0]);
  129. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  130. s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  131. s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  132. assert(ref == 0);
  133. ff_MPV_decode_mb(s, s->block);
  134. }
  135. /* init common dct for both encoder and decoder */
  136. av_cold int ff_dct_common_init(MpegEncContext *s)
  137. {
  138. ff_dsputil_init(&s->dsp, s->avctx);
  139. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  140. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  141. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  142. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  143. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  144. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  145. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  146. if (s->flags & CODEC_FLAG_BITEXACT)
  147. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  148. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  149. if (ARCH_ALPHA)
  150. ff_MPV_common_init_axp(s);
  151. if (ARCH_ARM)
  152. ff_MPV_common_init_arm(s);
  153. if (ARCH_BFIN)
  154. ff_MPV_common_init_bfin(s);
  155. if (ARCH_PPC)
  156. ff_MPV_common_init_ppc(s);
  157. if (ARCH_X86)
  158. ff_MPV_common_init_x86(s);
  159. /* load & permutate scantables
  160. * note: only wmv uses different ones
  161. */
  162. if (s->alternate_scan) {
  163. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  164. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  165. } else {
  166. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  167. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  168. }
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  170. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  171. return 0;
  172. }
  173. int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
  174. {
  175. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  176. // edge emu needs blocksize + filter length - 1
  177. // (= 17x17 for halfpel / 21x21 for h264)
  178. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  179. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  180. // linesize * interlaced * MBsize
  181. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
  182. fail);
  183. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
  184. fail)
  185. s->me.temp = s->me.scratchpad;
  186. s->rd_scratchpad = s->me.scratchpad;
  187. s->b_scratchpad = s->me.scratchpad;
  188. s->obmc_scratchpad = s->me.scratchpad + 16;
  189. return 0;
  190. fail:
  191. av_freep(&s->edge_emu_buffer);
  192. return AVERROR(ENOMEM);
  193. }
  194. /**
  195. * Allocate a frame buffer
  196. */
  197. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  198. {
  199. int r, ret;
  200. pic->tf.f = &pic->f;
  201. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  202. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  203. s->codec_id != AV_CODEC_ID_MSS2)
  204. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  205. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  206. else {
  207. pic->f.width = s->avctx->width;
  208. pic->f.height = s->avctx->height;
  209. pic->f.format = s->avctx->pix_fmt;
  210. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  211. }
  212. if (r < 0 || !pic->f.data[0]) {
  213. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  214. r, pic->f.data[0]);
  215. return -1;
  216. }
  217. if (s->avctx->hwaccel) {
  218. assert(!pic->hwaccel_picture_private);
  219. if (s->avctx->hwaccel->priv_data_size) {
  220. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  221. if (!pic->hwaccel_priv_buf) {
  222. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  223. return -1;
  224. }
  225. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  226. }
  227. }
  228. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  229. s->uvlinesize != pic->f.linesize[1])) {
  230. av_log(s->avctx, AV_LOG_ERROR,
  231. "get_buffer() failed (stride changed)\n");
  232. ff_mpeg_unref_picture(s, pic);
  233. return -1;
  234. }
  235. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  236. av_log(s->avctx, AV_LOG_ERROR,
  237. "get_buffer() failed (uv stride mismatch)\n");
  238. ff_mpeg_unref_picture(s, pic);
  239. return -1;
  240. }
  241. if (!s->edge_emu_buffer &&
  242. (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  243. av_log(s->avctx, AV_LOG_ERROR,
  244. "get_buffer() failed to allocate context scratch buffers.\n");
  245. ff_mpeg_unref_picture(s, pic);
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. static void free_picture_tables(Picture *pic)
  251. {
  252. int i;
  253. av_buffer_unref(&pic->mb_var_buf);
  254. av_buffer_unref(&pic->mc_mb_var_buf);
  255. av_buffer_unref(&pic->mb_mean_buf);
  256. av_buffer_unref(&pic->mbskip_table_buf);
  257. av_buffer_unref(&pic->qscale_table_buf);
  258. av_buffer_unref(&pic->mb_type_buf);
  259. for (i = 0; i < 2; i++) {
  260. av_buffer_unref(&pic->motion_val_buf[i]);
  261. av_buffer_unref(&pic->ref_index_buf[i]);
  262. }
  263. }
  264. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  265. {
  266. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  267. const int mb_array_size = s->mb_stride * s->mb_height;
  268. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  269. int i;
  270. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  271. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  272. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  273. sizeof(uint32_t));
  274. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  275. return AVERROR(ENOMEM);
  276. if (s->encoding) {
  277. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  278. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  279. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  280. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  281. return AVERROR(ENOMEM);
  282. }
  283. if (s->out_format == FMT_H263 || s->encoding ||
  284. (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
  285. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  286. int ref_index_size = 4 * mb_array_size;
  287. for (i = 0; mv_size && i < 2; i++) {
  288. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  289. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  290. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  291. return AVERROR(ENOMEM);
  292. }
  293. }
  294. return 0;
  295. }
  296. static int make_tables_writable(Picture *pic)
  297. {
  298. int ret, i;
  299. #define MAKE_WRITABLE(table) \
  300. do {\
  301. if (pic->table &&\
  302. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  303. return ret;\
  304. } while (0)
  305. MAKE_WRITABLE(mb_var_buf);
  306. MAKE_WRITABLE(mc_mb_var_buf);
  307. MAKE_WRITABLE(mb_mean_buf);
  308. MAKE_WRITABLE(mbskip_table_buf);
  309. MAKE_WRITABLE(qscale_table_buf);
  310. MAKE_WRITABLE(mb_type_buf);
  311. for (i = 0; i < 2; i++) {
  312. MAKE_WRITABLE(motion_val_buf[i]);
  313. MAKE_WRITABLE(ref_index_buf[i]);
  314. }
  315. return 0;
  316. }
  317. /**
  318. * Allocate a Picture.
  319. * The pixels are allocated/set by calling get_buffer() if shared = 0
  320. */
  321. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  322. {
  323. int i, ret;
  324. if (shared) {
  325. assert(pic->f.data[0]);
  326. pic->shared = 1;
  327. } else {
  328. assert(!pic->f.data[0]);
  329. if (alloc_frame_buffer(s, pic) < 0)
  330. return -1;
  331. s->linesize = pic->f.linesize[0];
  332. s->uvlinesize = pic->f.linesize[1];
  333. }
  334. if (!pic->qscale_table_buf)
  335. ret = alloc_picture_tables(s, pic);
  336. else
  337. ret = make_tables_writable(pic);
  338. if (ret < 0)
  339. goto fail;
  340. if (s->encoding) {
  341. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  342. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  343. pic->mb_mean = pic->mb_mean_buf->data;
  344. }
  345. pic->mbskip_table = pic->mbskip_table_buf->data;
  346. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  347. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  348. if (pic->motion_val_buf[0]) {
  349. for (i = 0; i < 2; i++) {
  350. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  351. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  352. }
  353. }
  354. return 0;
  355. fail:
  356. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  357. ff_mpeg_unref_picture(s, pic);
  358. free_picture_tables(pic);
  359. return AVERROR(ENOMEM);
  360. }
  361. /**
  362. * Deallocate a picture.
  363. */
  364. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  365. {
  366. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  367. pic->tf.f = &pic->f;
  368. /* WM Image / Screen codecs allocate internal buffers with different
  369. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  370. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  371. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  372. s->codec_id != AV_CODEC_ID_MSS2)
  373. ff_thread_release_buffer(s->avctx, &pic->tf);
  374. else
  375. av_frame_unref(&pic->f);
  376. av_buffer_unref(&pic->hwaccel_priv_buf);
  377. if (pic->needs_realloc)
  378. free_picture_tables(pic);
  379. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  380. }
  381. static int update_picture_tables(Picture *dst, Picture *src)
  382. {
  383. int i;
  384. #define UPDATE_TABLE(table)\
  385. do {\
  386. if (src->table &&\
  387. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  388. av_buffer_unref(&dst->table);\
  389. dst->table = av_buffer_ref(src->table);\
  390. if (!dst->table) {\
  391. free_picture_tables(dst);\
  392. return AVERROR(ENOMEM);\
  393. }\
  394. }\
  395. } while (0)
  396. UPDATE_TABLE(mb_var_buf);
  397. UPDATE_TABLE(mc_mb_var_buf);
  398. UPDATE_TABLE(mb_mean_buf);
  399. UPDATE_TABLE(mbskip_table_buf);
  400. UPDATE_TABLE(qscale_table_buf);
  401. UPDATE_TABLE(mb_type_buf);
  402. for (i = 0; i < 2; i++) {
  403. UPDATE_TABLE(motion_val_buf[i]);
  404. UPDATE_TABLE(ref_index_buf[i]);
  405. }
  406. dst->mb_var = src->mb_var;
  407. dst->mc_mb_var = src->mc_mb_var;
  408. dst->mb_mean = src->mb_mean;
  409. dst->mbskip_table = src->mbskip_table;
  410. dst->qscale_table = src->qscale_table;
  411. dst->mb_type = src->mb_type;
  412. for (i = 0; i < 2; i++) {
  413. dst->motion_val[i] = src->motion_val[i];
  414. dst->ref_index[i] = src->ref_index[i];
  415. }
  416. return 0;
  417. }
  418. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  419. {
  420. int ret;
  421. av_assert0(!dst->f.buf[0]);
  422. av_assert0(src->f.buf[0]);
  423. src->tf.f = &src->f;
  424. dst->tf.f = &dst->f;
  425. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  426. if (ret < 0)
  427. goto fail;
  428. ret = update_picture_tables(dst, src);
  429. if (ret < 0)
  430. goto fail;
  431. if (src->hwaccel_picture_private) {
  432. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  433. if (!dst->hwaccel_priv_buf)
  434. goto fail;
  435. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  436. }
  437. dst->field_picture = src->field_picture;
  438. dst->mb_var_sum = src->mb_var_sum;
  439. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  440. dst->b_frame_score = src->b_frame_score;
  441. dst->needs_realloc = src->needs_realloc;
  442. dst->reference = src->reference;
  443. dst->shared = src->shared;
  444. return 0;
  445. fail:
  446. ff_mpeg_unref_picture(s, dst);
  447. return ret;
  448. }
  449. static int init_duplicate_context(MpegEncContext *s)
  450. {
  451. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  452. int c_size = s->mb_stride * (s->mb_height + 1);
  453. int yc_size = y_size + 2 * c_size;
  454. int i;
  455. s->edge_emu_buffer =
  456. s->me.scratchpad =
  457. s->me.temp =
  458. s->rd_scratchpad =
  459. s->b_scratchpad =
  460. s->obmc_scratchpad = NULL;
  461. if (s->encoding) {
  462. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  463. ME_MAP_SIZE * sizeof(uint32_t), fail)
  464. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  465. ME_MAP_SIZE * sizeof(uint32_t), fail)
  466. if (s->avctx->noise_reduction) {
  467. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  468. 2 * 64 * sizeof(int), fail)
  469. }
  470. }
  471. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  472. s->block = s->blocks[0];
  473. for (i = 0; i < 12; i++) {
  474. s->pblocks[i] = &s->block[i];
  475. }
  476. if (s->out_format == FMT_H263) {
  477. /* ac values */
  478. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  479. yc_size * sizeof(int16_t) * 16, fail);
  480. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  481. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  482. s->ac_val[2] = s->ac_val[1] + c_size;
  483. }
  484. return 0;
  485. fail:
  486. return -1; // free() through ff_MPV_common_end()
  487. }
  488. static void free_duplicate_context(MpegEncContext *s)
  489. {
  490. if (s == NULL)
  491. return;
  492. av_freep(&s->edge_emu_buffer);
  493. av_freep(&s->me.scratchpad);
  494. s->me.temp =
  495. s->rd_scratchpad =
  496. s->b_scratchpad =
  497. s->obmc_scratchpad = NULL;
  498. av_freep(&s->dct_error_sum);
  499. av_freep(&s->me.map);
  500. av_freep(&s->me.score_map);
  501. av_freep(&s->blocks);
  502. av_freep(&s->ac_val_base);
  503. s->block = NULL;
  504. }
  505. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  506. {
  507. #define COPY(a) bak->a = src->a
  508. COPY(edge_emu_buffer);
  509. COPY(me.scratchpad);
  510. COPY(me.temp);
  511. COPY(rd_scratchpad);
  512. COPY(b_scratchpad);
  513. COPY(obmc_scratchpad);
  514. COPY(me.map);
  515. COPY(me.score_map);
  516. COPY(blocks);
  517. COPY(block);
  518. COPY(start_mb_y);
  519. COPY(end_mb_y);
  520. COPY(me.map_generation);
  521. COPY(pb);
  522. COPY(dct_error_sum);
  523. COPY(dct_count[0]);
  524. COPY(dct_count[1]);
  525. COPY(ac_val_base);
  526. COPY(ac_val[0]);
  527. COPY(ac_val[1]);
  528. COPY(ac_val[2]);
  529. #undef COPY
  530. }
  531. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  532. {
  533. MpegEncContext bak;
  534. int i, ret;
  535. // FIXME copy only needed parts
  536. // START_TIMER
  537. backup_duplicate_context(&bak, dst);
  538. memcpy(dst, src, sizeof(MpegEncContext));
  539. backup_duplicate_context(dst, &bak);
  540. for (i = 0; i < 12; i++) {
  541. dst->pblocks[i] = &dst->block[i];
  542. }
  543. if (!dst->edge_emu_buffer &&
  544. (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
  545. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  546. "scratch buffers.\n");
  547. return ret;
  548. }
  549. // STOP_TIMER("update_duplicate_context")
  550. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  551. return 0;
  552. }
  553. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  554. const AVCodecContext *src)
  555. {
  556. int i, ret;
  557. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  558. if (dst == src || !s1->context_initialized)
  559. return 0;
  560. // FIXME can parameters change on I-frames?
  561. // in that case dst may need a reinit
  562. if (!s->context_initialized) {
  563. memcpy(s, s1, sizeof(MpegEncContext));
  564. s->avctx = dst;
  565. s->bitstream_buffer = NULL;
  566. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  567. ff_MPV_common_init(s);
  568. }
  569. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  570. int err;
  571. s->context_reinit = 0;
  572. s->height = s1->height;
  573. s->width = s1->width;
  574. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  575. return err;
  576. }
  577. s->avctx->coded_height = s1->avctx->coded_height;
  578. s->avctx->coded_width = s1->avctx->coded_width;
  579. s->avctx->width = s1->avctx->width;
  580. s->avctx->height = s1->avctx->height;
  581. s->coded_picture_number = s1->coded_picture_number;
  582. s->picture_number = s1->picture_number;
  583. s->input_picture_number = s1->input_picture_number;
  584. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  585. ff_mpeg_unref_picture(s, &s->picture[i]);
  586. if (s1->picture[i].f.data[0] &&
  587. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  588. return ret;
  589. }
  590. #define UPDATE_PICTURE(pic)\
  591. do {\
  592. ff_mpeg_unref_picture(s, &s->pic);\
  593. if (s1->pic.f.data[0])\
  594. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  595. else\
  596. ret = update_picture_tables(&s->pic, &s1->pic);\
  597. if (ret < 0)\
  598. return ret;\
  599. } while (0)
  600. UPDATE_PICTURE(current_picture);
  601. UPDATE_PICTURE(last_picture);
  602. UPDATE_PICTURE(next_picture);
  603. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  604. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  605. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  606. // Error/bug resilience
  607. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  608. s->workaround_bugs = s1->workaround_bugs;
  609. // MPEG4 timing info
  610. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  611. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  612. // B-frame info
  613. s->max_b_frames = s1->max_b_frames;
  614. s->low_delay = s1->low_delay;
  615. s->droppable = s1->droppable;
  616. // DivX handling (doesn't work)
  617. s->divx_packed = s1->divx_packed;
  618. if (s1->bitstream_buffer) {
  619. if (s1->bitstream_buffer_size +
  620. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  621. av_fast_malloc(&s->bitstream_buffer,
  622. &s->allocated_bitstream_buffer_size,
  623. s1->allocated_bitstream_buffer_size);
  624. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  625. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  626. s1->bitstream_buffer_size);
  627. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  628. FF_INPUT_BUFFER_PADDING_SIZE);
  629. }
  630. // linesize dependend scratch buffer allocation
  631. if (!s->edge_emu_buffer)
  632. if (s1->linesize) {
  633. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  634. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  635. "scratch buffers.\n");
  636. return AVERROR(ENOMEM);
  637. }
  638. } else {
  639. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  640. "be allocated due to unknown size.\n");
  641. return AVERROR_BUG;
  642. }
  643. // MPEG2/interlacing info
  644. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  645. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  646. if (!s1->first_field) {
  647. s->last_pict_type = s1->pict_type;
  648. if (s1->current_picture_ptr)
  649. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  650. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  651. s->last_non_b_pict_type = s1->pict_type;
  652. }
  653. }
  654. return 0;
  655. }
  656. /**
  657. * Set the given MpegEncContext to common defaults
  658. * (same for encoding and decoding).
  659. * The changed fields will not depend upon the
  660. * prior state of the MpegEncContext.
  661. */
  662. void ff_MPV_common_defaults(MpegEncContext *s)
  663. {
  664. s->y_dc_scale_table =
  665. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  666. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  667. s->progressive_frame = 1;
  668. s->progressive_sequence = 1;
  669. s->picture_structure = PICT_FRAME;
  670. s->coded_picture_number = 0;
  671. s->picture_number = 0;
  672. s->input_picture_number = 0;
  673. s->picture_in_gop_number = 0;
  674. s->f_code = 1;
  675. s->b_code = 1;
  676. s->slice_context_count = 1;
  677. }
  678. /**
  679. * Set the given MpegEncContext to defaults for decoding.
  680. * the changed fields will not depend upon
  681. * the prior state of the MpegEncContext.
  682. */
  683. void ff_MPV_decode_defaults(MpegEncContext *s)
  684. {
  685. ff_MPV_common_defaults(s);
  686. }
  687. static int init_er(MpegEncContext *s)
  688. {
  689. ERContext *er = &s->er;
  690. int mb_array_size = s->mb_height * s->mb_stride;
  691. int i;
  692. er->avctx = s->avctx;
  693. er->dsp = &s->dsp;
  694. er->mb_index2xy = s->mb_index2xy;
  695. er->mb_num = s->mb_num;
  696. er->mb_width = s->mb_width;
  697. er->mb_height = s->mb_height;
  698. er->mb_stride = s->mb_stride;
  699. er->b8_stride = s->b8_stride;
  700. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  701. er->error_status_table = av_mallocz(mb_array_size);
  702. if (!er->er_temp_buffer || !er->error_status_table)
  703. goto fail;
  704. er->mbskip_table = s->mbskip_table;
  705. er->mbintra_table = s->mbintra_table;
  706. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  707. er->dc_val[i] = s->dc_val[i];
  708. er->decode_mb = mpeg_er_decode_mb;
  709. er->opaque = s;
  710. return 0;
  711. fail:
  712. av_freep(&er->er_temp_buffer);
  713. av_freep(&er->error_status_table);
  714. return AVERROR(ENOMEM);
  715. }
  716. /**
  717. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  718. */
  719. static int init_context_frame(MpegEncContext *s)
  720. {
  721. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  722. s->mb_width = (s->width + 15) / 16;
  723. s->mb_stride = s->mb_width + 1;
  724. s->b8_stride = s->mb_width * 2 + 1;
  725. s->b4_stride = s->mb_width * 4 + 1;
  726. mb_array_size = s->mb_height * s->mb_stride;
  727. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  728. /* set default edge pos, will be overriden
  729. * in decode_header if needed */
  730. s->h_edge_pos = s->mb_width * 16;
  731. s->v_edge_pos = s->mb_height * 16;
  732. s->mb_num = s->mb_width * s->mb_height;
  733. s->block_wrap[0] =
  734. s->block_wrap[1] =
  735. s->block_wrap[2] =
  736. s->block_wrap[3] = s->b8_stride;
  737. s->block_wrap[4] =
  738. s->block_wrap[5] = s->mb_stride;
  739. y_size = s->b8_stride * (2 * s->mb_height + 1);
  740. c_size = s->mb_stride * (s->mb_height + 1);
  741. yc_size = y_size + 2 * c_size;
  742. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  743. fail); // error ressilience code looks cleaner with this
  744. for (y = 0; y < s->mb_height; y++)
  745. for (x = 0; x < s->mb_width; x++)
  746. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  747. s->mb_index2xy[s->mb_height * s->mb_width] =
  748. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  749. if (s->encoding) {
  750. /* Allocate MV tables */
  751. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  752. mv_table_size * 2 * sizeof(int16_t), fail);
  753. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  754. mv_table_size * 2 * sizeof(int16_t), fail);
  755. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  756. mv_table_size * 2 * sizeof(int16_t), fail);
  757. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  758. mv_table_size * 2 * sizeof(int16_t), fail);
  759. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  760. mv_table_size * 2 * sizeof(int16_t), fail);
  761. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  762. mv_table_size * 2 * sizeof(int16_t), fail);
  763. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  764. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  765. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  766. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  767. s->mb_stride + 1;
  768. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  769. s->mb_stride + 1;
  770. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  771. /* Allocate MB type table */
  772. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  773. sizeof(uint16_t), fail); // needed for encoding
  774. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  775. sizeof(int), fail);
  776. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  777. mb_array_size * sizeof(float), fail);
  778. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  779. mb_array_size * sizeof(float), fail);
  780. }
  781. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  782. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  783. /* interlaced direct mode decoding tables */
  784. for (i = 0; i < 2; i++) {
  785. int j, k;
  786. for (j = 0; j < 2; j++) {
  787. for (k = 0; k < 2; k++) {
  788. FF_ALLOCZ_OR_GOTO(s->avctx,
  789. s->b_field_mv_table_base[i][j][k],
  790. mv_table_size * 2 * sizeof(int16_t),
  791. fail);
  792. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  793. s->mb_stride + 1;
  794. }
  795. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  796. mb_array_size * 2 * sizeof(uint8_t), fail);
  797. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  798. mv_table_size * 2 * sizeof(int16_t), fail);
  799. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  800. + s->mb_stride + 1;
  801. }
  802. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  803. mb_array_size * 2 * sizeof(uint8_t), fail);
  804. }
  805. }
  806. if (s->out_format == FMT_H263) {
  807. /* cbp values */
  808. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  809. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  810. /* cbp, ac_pred, pred_dir */
  811. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  812. mb_array_size * sizeof(uint8_t), fail);
  813. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  814. mb_array_size * sizeof(uint8_t), fail);
  815. }
  816. if (s->h263_pred || s->h263_plus || !s->encoding) {
  817. /* dc values */
  818. // MN: we need these for error resilience of intra-frames
  819. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  820. yc_size * sizeof(int16_t), fail);
  821. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  822. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  823. s->dc_val[2] = s->dc_val[1] + c_size;
  824. for (i = 0; i < yc_size; i++)
  825. s->dc_val_base[i] = 1024;
  826. }
  827. /* which mb is a intra block */
  828. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  829. memset(s->mbintra_table, 1, mb_array_size);
  830. /* init macroblock skip table */
  831. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  832. // Note the + 1 is for a quicker mpeg4 slice_end detection
  833. return init_er(s);
  834. fail:
  835. return AVERROR(ENOMEM);
  836. }
  837. /**
  838. * init common structure for both encoder and decoder.
  839. * this assumes that some variables like width/height are already set
  840. */
  841. av_cold int ff_MPV_common_init(MpegEncContext *s)
  842. {
  843. int i;
  844. int nb_slices = (HAVE_THREADS &&
  845. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  846. s->avctx->thread_count : 1;
  847. if (s->encoding && s->avctx->slices)
  848. nb_slices = s->avctx->slices;
  849. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  850. s->mb_height = (s->height + 31) / 32 * 2;
  851. else
  852. s->mb_height = (s->height + 15) / 16;
  853. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  854. av_log(s->avctx, AV_LOG_ERROR,
  855. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  856. return -1;
  857. }
  858. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  859. int max_slices;
  860. if (s->mb_height)
  861. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  862. else
  863. max_slices = MAX_THREADS;
  864. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  865. " reducing to %d\n", nb_slices, max_slices);
  866. nb_slices = max_slices;
  867. }
  868. if ((s->width || s->height) &&
  869. av_image_check_size(s->width, s->height, 0, s->avctx))
  870. return -1;
  871. ff_dct_common_init(s);
  872. s->flags = s->avctx->flags;
  873. s->flags2 = s->avctx->flags2;
  874. /* set chroma shifts */
  875. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  876. &s->chroma_x_shift,
  877. &s->chroma_y_shift);
  878. /* convert fourcc to upper case */
  879. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  880. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  881. if (s->width && s->height) {
  882. s->avctx->coded_frame = &s->current_picture.f;
  883. if (s->encoding) {
  884. if (s->msmpeg4_version) {
  885. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  886. 2 * 2 * (MAX_LEVEL + 1) *
  887. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  888. }
  889. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  890. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  891. 64 * 32 * sizeof(int), fail);
  892. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  893. 64 * 32 * sizeof(int), fail);
  894. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  895. 64 * 32 * 2 * sizeof(uint16_t), fail);
  896. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  897. 64 * 32 * 2 * sizeof(uint16_t), fail);
  898. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  899. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  900. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  901. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  902. if (s->avctx->noise_reduction) {
  903. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  904. 2 * 64 * sizeof(uint16_t), fail);
  905. }
  906. }
  907. }
  908. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  909. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  910. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  911. avcodec_get_frame_defaults(&s->picture[i].f);
  912. }
  913. memset(&s->next_picture, 0, sizeof(s->next_picture));
  914. memset(&s->last_picture, 0, sizeof(s->last_picture));
  915. memset(&s->current_picture, 0, sizeof(s->current_picture));
  916. avcodec_get_frame_defaults(&s->next_picture.f);
  917. avcodec_get_frame_defaults(&s->last_picture.f);
  918. avcodec_get_frame_defaults(&s->current_picture.f);
  919. if (s->width && s->height) {
  920. if (init_context_frame(s))
  921. goto fail;
  922. s->parse_context.state = -1;
  923. }
  924. s->context_initialized = 1;
  925. s->thread_context[0] = s;
  926. if (s->width && s->height) {
  927. if (nb_slices > 1) {
  928. for (i = 1; i < nb_slices; i++) {
  929. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  930. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  931. }
  932. for (i = 0; i < nb_slices; i++) {
  933. if (init_duplicate_context(s->thread_context[i]) < 0)
  934. goto fail;
  935. s->thread_context[i]->start_mb_y =
  936. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  937. s->thread_context[i]->end_mb_y =
  938. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  939. }
  940. } else {
  941. if (init_duplicate_context(s) < 0)
  942. goto fail;
  943. s->start_mb_y = 0;
  944. s->end_mb_y = s->mb_height;
  945. }
  946. s->slice_context_count = nb_slices;
  947. }
  948. return 0;
  949. fail:
  950. ff_MPV_common_end(s);
  951. return -1;
  952. }
  953. /**
  954. * Frees and resets MpegEncContext fields depending on the resolution.
  955. * Is used during resolution changes to avoid a full reinitialization of the
  956. * codec.
  957. */
  958. static int free_context_frame(MpegEncContext *s)
  959. {
  960. int i, j, k;
  961. av_freep(&s->mb_type);
  962. av_freep(&s->p_mv_table_base);
  963. av_freep(&s->b_forw_mv_table_base);
  964. av_freep(&s->b_back_mv_table_base);
  965. av_freep(&s->b_bidir_forw_mv_table_base);
  966. av_freep(&s->b_bidir_back_mv_table_base);
  967. av_freep(&s->b_direct_mv_table_base);
  968. s->p_mv_table = NULL;
  969. s->b_forw_mv_table = NULL;
  970. s->b_back_mv_table = NULL;
  971. s->b_bidir_forw_mv_table = NULL;
  972. s->b_bidir_back_mv_table = NULL;
  973. s->b_direct_mv_table = NULL;
  974. for (i = 0; i < 2; i++) {
  975. for (j = 0; j < 2; j++) {
  976. for (k = 0; k < 2; k++) {
  977. av_freep(&s->b_field_mv_table_base[i][j][k]);
  978. s->b_field_mv_table[i][j][k] = NULL;
  979. }
  980. av_freep(&s->b_field_select_table[i][j]);
  981. av_freep(&s->p_field_mv_table_base[i][j]);
  982. s->p_field_mv_table[i][j] = NULL;
  983. }
  984. av_freep(&s->p_field_select_table[i]);
  985. }
  986. av_freep(&s->dc_val_base);
  987. av_freep(&s->coded_block_base);
  988. av_freep(&s->mbintra_table);
  989. av_freep(&s->cbp_table);
  990. av_freep(&s->pred_dir_table);
  991. av_freep(&s->mbskip_table);
  992. av_freep(&s->er.error_status_table);
  993. av_freep(&s->er.er_temp_buffer);
  994. av_freep(&s->mb_index2xy);
  995. av_freep(&s->lambda_table);
  996. av_freep(&s->cplx_tab);
  997. av_freep(&s->bits_tab);
  998. s->linesize = s->uvlinesize = 0;
  999. return 0;
  1000. }
  1001. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1002. {
  1003. int i, err = 0;
  1004. if (s->slice_context_count > 1) {
  1005. for (i = 0; i < s->slice_context_count; i++) {
  1006. free_duplicate_context(s->thread_context[i]);
  1007. }
  1008. for (i = 1; i < s->slice_context_count; i++) {
  1009. av_freep(&s->thread_context[i]);
  1010. }
  1011. } else
  1012. free_duplicate_context(s);
  1013. if ((err = free_context_frame(s)) < 0)
  1014. return err;
  1015. if (s->picture)
  1016. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1017. s->picture[i].needs_realloc = 1;
  1018. }
  1019. s->last_picture_ptr =
  1020. s->next_picture_ptr =
  1021. s->current_picture_ptr = NULL;
  1022. // init
  1023. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1024. s->mb_height = (s->height + 31) / 32 * 2;
  1025. else
  1026. s->mb_height = (s->height + 15) / 16;
  1027. if ((s->width || s->height) &&
  1028. av_image_check_size(s->width, s->height, 0, s->avctx))
  1029. return AVERROR_INVALIDDATA;
  1030. if ((err = init_context_frame(s)))
  1031. goto fail;
  1032. s->thread_context[0] = s;
  1033. if (s->width && s->height) {
  1034. int nb_slices = s->slice_context_count;
  1035. if (nb_slices > 1) {
  1036. for (i = 1; i < nb_slices; i++) {
  1037. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1038. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1039. }
  1040. for (i = 0; i < nb_slices; i++) {
  1041. if (init_duplicate_context(s->thread_context[i]) < 0)
  1042. goto fail;
  1043. s->thread_context[i]->start_mb_y =
  1044. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1045. s->thread_context[i]->end_mb_y =
  1046. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1047. }
  1048. } else {
  1049. if (init_duplicate_context(s) < 0)
  1050. goto fail;
  1051. s->start_mb_y = 0;
  1052. s->end_mb_y = s->mb_height;
  1053. }
  1054. s->slice_context_count = nb_slices;
  1055. }
  1056. return 0;
  1057. fail:
  1058. ff_MPV_common_end(s);
  1059. return err;
  1060. }
  1061. /* init common structure for both encoder and decoder */
  1062. void ff_MPV_common_end(MpegEncContext *s)
  1063. {
  1064. int i;
  1065. if (s->slice_context_count > 1) {
  1066. for (i = 0; i < s->slice_context_count; i++) {
  1067. free_duplicate_context(s->thread_context[i]);
  1068. }
  1069. for (i = 1; i < s->slice_context_count; i++) {
  1070. av_freep(&s->thread_context[i]);
  1071. }
  1072. s->slice_context_count = 1;
  1073. } else free_duplicate_context(s);
  1074. av_freep(&s->parse_context.buffer);
  1075. s->parse_context.buffer_size = 0;
  1076. av_freep(&s->bitstream_buffer);
  1077. s->allocated_bitstream_buffer_size = 0;
  1078. av_freep(&s->avctx->stats_out);
  1079. av_freep(&s->ac_stats);
  1080. av_freep(&s->q_intra_matrix);
  1081. av_freep(&s->q_inter_matrix);
  1082. av_freep(&s->q_intra_matrix16);
  1083. av_freep(&s->q_inter_matrix16);
  1084. av_freep(&s->input_picture);
  1085. av_freep(&s->reordered_input_picture);
  1086. av_freep(&s->dct_offset);
  1087. if (s->picture) {
  1088. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1089. free_picture_tables(&s->picture[i]);
  1090. ff_mpeg_unref_picture(s, &s->picture[i]);
  1091. }
  1092. }
  1093. av_freep(&s->picture);
  1094. free_picture_tables(&s->last_picture);
  1095. ff_mpeg_unref_picture(s, &s->last_picture);
  1096. free_picture_tables(&s->current_picture);
  1097. ff_mpeg_unref_picture(s, &s->current_picture);
  1098. free_picture_tables(&s->next_picture);
  1099. ff_mpeg_unref_picture(s, &s->next_picture);
  1100. free_picture_tables(&s->new_picture);
  1101. ff_mpeg_unref_picture(s, &s->new_picture);
  1102. free_context_frame(s);
  1103. s->context_initialized = 0;
  1104. s->last_picture_ptr =
  1105. s->next_picture_ptr =
  1106. s->current_picture_ptr = NULL;
  1107. s->linesize = s->uvlinesize = 0;
  1108. }
  1109. av_cold void ff_init_rl(RLTable *rl,
  1110. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1111. {
  1112. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1113. uint8_t index_run[MAX_RUN + 1];
  1114. int last, run, level, start, end, i;
  1115. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1116. if (static_store && rl->max_level[0])
  1117. return;
  1118. /* compute max_level[], max_run[] and index_run[] */
  1119. for (last = 0; last < 2; last++) {
  1120. if (last == 0) {
  1121. start = 0;
  1122. end = rl->last;
  1123. } else {
  1124. start = rl->last;
  1125. end = rl->n;
  1126. }
  1127. memset(max_level, 0, MAX_RUN + 1);
  1128. memset(max_run, 0, MAX_LEVEL + 1);
  1129. memset(index_run, rl->n, MAX_RUN + 1);
  1130. for (i = start; i < end; i++) {
  1131. run = rl->table_run[i];
  1132. level = rl->table_level[i];
  1133. if (index_run[run] == rl->n)
  1134. index_run[run] = i;
  1135. if (level > max_level[run])
  1136. max_level[run] = level;
  1137. if (run > max_run[level])
  1138. max_run[level] = run;
  1139. }
  1140. if (static_store)
  1141. rl->max_level[last] = static_store[last];
  1142. else
  1143. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1144. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1145. if (static_store)
  1146. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1147. else
  1148. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1149. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1150. if (static_store)
  1151. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1152. else
  1153. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1154. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1155. }
  1156. }
  1157. av_cold void ff_init_vlc_rl(RLTable *rl)
  1158. {
  1159. int i, q;
  1160. for (q = 0; q < 32; q++) {
  1161. int qmul = q * 2;
  1162. int qadd = (q - 1) | 1;
  1163. if (q == 0) {
  1164. qmul = 1;
  1165. qadd = 0;
  1166. }
  1167. for (i = 0; i < rl->vlc.table_size; i++) {
  1168. int code = rl->vlc.table[i][0];
  1169. int len = rl->vlc.table[i][1];
  1170. int level, run;
  1171. if (len == 0) { // illegal code
  1172. run = 66;
  1173. level = MAX_LEVEL;
  1174. } else if (len < 0) { // more bits needed
  1175. run = 0;
  1176. level = code;
  1177. } else {
  1178. if (code == rl->n) { // esc
  1179. run = 66;
  1180. level = 0;
  1181. } else {
  1182. run = rl->table_run[code] + 1;
  1183. level = rl->table_level[code] * qmul + qadd;
  1184. if (code >= rl->last) run += 192;
  1185. }
  1186. }
  1187. rl->rl_vlc[q][i].len = len;
  1188. rl->rl_vlc[q][i].level = level;
  1189. rl->rl_vlc[q][i].run = run;
  1190. }
  1191. }
  1192. }
  1193. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1194. {
  1195. int i;
  1196. /* release non reference frames */
  1197. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1198. if (!s->picture[i].reference &&
  1199. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1200. ff_mpeg_unref_picture(s, &s->picture[i]);
  1201. }
  1202. }
  1203. }
  1204. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1205. {
  1206. if (pic->f.data[0] == NULL)
  1207. return 1;
  1208. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1209. return 1;
  1210. return 0;
  1211. }
  1212. static int find_unused_picture(MpegEncContext *s, int shared)
  1213. {
  1214. int i;
  1215. if (shared) {
  1216. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1217. if (s->picture[i].f.data[0] == NULL)
  1218. return i;
  1219. }
  1220. } else {
  1221. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1222. if (pic_is_unused(s, &s->picture[i]))
  1223. return i;
  1224. }
  1225. }
  1226. return AVERROR_INVALIDDATA;
  1227. }
  1228. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1229. {
  1230. int ret = find_unused_picture(s, shared);
  1231. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1232. if (s->picture[ret].needs_realloc) {
  1233. s->picture[ret].needs_realloc = 0;
  1234. free_picture_tables(&s->picture[ret]);
  1235. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1236. avcodec_get_frame_defaults(&s->picture[ret].f);
  1237. }
  1238. }
  1239. return ret;
  1240. }
  1241. static void update_noise_reduction(MpegEncContext *s)
  1242. {
  1243. int intra, i;
  1244. for (intra = 0; intra < 2; intra++) {
  1245. if (s->dct_count[intra] > (1 << 16)) {
  1246. for (i = 0; i < 64; i++) {
  1247. s->dct_error_sum[intra][i] >>= 1;
  1248. }
  1249. s->dct_count[intra] >>= 1;
  1250. }
  1251. for (i = 0; i < 64; i++) {
  1252. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1253. s->dct_count[intra] +
  1254. s->dct_error_sum[intra][i] / 2) /
  1255. (s->dct_error_sum[intra][i] + 1);
  1256. }
  1257. }
  1258. }
  1259. /**
  1260. * generic function for encode/decode called after coding/decoding
  1261. * the header and before a frame is coded/decoded.
  1262. */
  1263. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1264. {
  1265. int i, ret;
  1266. Picture *pic;
  1267. s->mb_skipped = 0;
  1268. /* mark & release old frames */
  1269. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1270. s->last_picture_ptr != s->next_picture_ptr &&
  1271. s->last_picture_ptr->f.data[0]) {
  1272. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1273. }
  1274. /* release forgotten pictures */
  1275. /* if (mpeg124/h263) */
  1276. if (!s->encoding) {
  1277. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1278. if (&s->picture[i] != s->last_picture_ptr &&
  1279. &s->picture[i] != s->next_picture_ptr &&
  1280. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1281. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1282. av_log(avctx, AV_LOG_ERROR,
  1283. "releasing zombie picture\n");
  1284. ff_mpeg_unref_picture(s, &s->picture[i]);
  1285. }
  1286. }
  1287. }
  1288. ff_mpeg_unref_picture(s, &s->current_picture);
  1289. if (!s->encoding) {
  1290. ff_release_unused_pictures(s, 1);
  1291. if (s->current_picture_ptr &&
  1292. s->current_picture_ptr->f.data[0] == NULL) {
  1293. // we already have a unused image
  1294. // (maybe it was set before reading the header)
  1295. pic = s->current_picture_ptr;
  1296. } else {
  1297. i = ff_find_unused_picture(s, 0);
  1298. if (i < 0) {
  1299. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1300. return i;
  1301. }
  1302. pic = &s->picture[i];
  1303. }
  1304. pic->reference = 0;
  1305. if (!s->droppable) {
  1306. if (s->pict_type != AV_PICTURE_TYPE_B)
  1307. pic->reference = 3;
  1308. }
  1309. pic->f.coded_picture_number = s->coded_picture_number++;
  1310. if (ff_alloc_picture(s, pic, 0) < 0)
  1311. return -1;
  1312. s->current_picture_ptr = pic;
  1313. // FIXME use only the vars from current_pic
  1314. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1315. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1316. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1317. if (s->picture_structure != PICT_FRAME)
  1318. s->current_picture_ptr->f.top_field_first =
  1319. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1320. }
  1321. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1322. !s->progressive_sequence;
  1323. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1324. }
  1325. s->current_picture_ptr->f.pict_type = s->pict_type;
  1326. // if (s->flags && CODEC_FLAG_QSCALE)
  1327. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1328. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1329. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1330. s->current_picture_ptr)) < 0)
  1331. return ret;
  1332. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1333. s->last_picture_ptr = s->next_picture_ptr;
  1334. if (!s->droppable)
  1335. s->next_picture_ptr = s->current_picture_ptr;
  1336. }
  1337. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1338. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1339. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1340. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1341. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1342. s->pict_type, s->droppable);
  1343. if ((s->last_picture_ptr == NULL ||
  1344. s->last_picture_ptr->f.data[0] == NULL) &&
  1345. (s->pict_type != AV_PICTURE_TYPE_I ||
  1346. s->picture_structure != PICT_FRAME)) {
  1347. int h_chroma_shift, v_chroma_shift;
  1348. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1349. &h_chroma_shift, &v_chroma_shift);
  1350. if (s->pict_type != AV_PICTURE_TYPE_I)
  1351. av_log(avctx, AV_LOG_ERROR,
  1352. "warning: first frame is no keyframe\n");
  1353. else if (s->picture_structure != PICT_FRAME)
  1354. av_log(avctx, AV_LOG_INFO,
  1355. "allocate dummy last picture for field based first keyframe\n");
  1356. /* Allocate a dummy frame */
  1357. i = ff_find_unused_picture(s, 0);
  1358. if (i < 0) {
  1359. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1360. return i;
  1361. }
  1362. s->last_picture_ptr = &s->picture[i];
  1363. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1364. s->last_picture_ptr = NULL;
  1365. return -1;
  1366. }
  1367. memset(s->last_picture_ptr->f.data[0], 0,
  1368. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1369. memset(s->last_picture_ptr->f.data[1], 0x80,
  1370. (avctx->height >> v_chroma_shift) *
  1371. s->last_picture_ptr->f.linesize[1]);
  1372. memset(s->last_picture_ptr->f.data[2], 0x80,
  1373. (avctx->height >> v_chroma_shift) *
  1374. s->last_picture_ptr->f.linesize[2]);
  1375. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1376. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1377. }
  1378. if ((s->next_picture_ptr == NULL ||
  1379. s->next_picture_ptr->f.data[0] == NULL) &&
  1380. s->pict_type == AV_PICTURE_TYPE_B) {
  1381. /* Allocate a dummy frame */
  1382. i = ff_find_unused_picture(s, 0);
  1383. if (i < 0) {
  1384. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1385. return i;
  1386. }
  1387. s->next_picture_ptr = &s->picture[i];
  1388. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1389. s->next_picture_ptr = NULL;
  1390. return -1;
  1391. }
  1392. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1393. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1394. }
  1395. if (s->last_picture_ptr) {
  1396. ff_mpeg_unref_picture(s, &s->last_picture);
  1397. if (s->last_picture_ptr->f.data[0] &&
  1398. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1399. s->last_picture_ptr)) < 0)
  1400. return ret;
  1401. }
  1402. if (s->next_picture_ptr) {
  1403. ff_mpeg_unref_picture(s, &s->next_picture);
  1404. if (s->next_picture_ptr->f.data[0] &&
  1405. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1406. s->next_picture_ptr)) < 0)
  1407. return ret;
  1408. }
  1409. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1410. !(s->last_picture_ptr && s->last_picture_ptr->f.data[0])) {
  1411. av_log(s, AV_LOG_ERROR,
  1412. "Non-reference picture received and no reference available\n");
  1413. return AVERROR_INVALIDDATA;
  1414. }
  1415. if (s->picture_structure!= PICT_FRAME) {
  1416. int i;
  1417. for (i = 0; i < 4; i++) {
  1418. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1419. s->current_picture.f.data[i] +=
  1420. s->current_picture.f.linesize[i];
  1421. }
  1422. s->current_picture.f.linesize[i] *= 2;
  1423. s->last_picture.f.linesize[i] *= 2;
  1424. s->next_picture.f.linesize[i] *= 2;
  1425. }
  1426. }
  1427. s->err_recognition = avctx->err_recognition;
  1428. /* set dequantizer, we can't do it during init as
  1429. * it might change for mpeg4 and we can't do it in the header
  1430. * decode as init is not called for mpeg4 there yet */
  1431. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1432. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1433. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1434. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1435. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1436. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1437. } else {
  1438. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1439. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1440. }
  1441. if (s->dct_error_sum) {
  1442. assert(s->avctx->noise_reduction && s->encoding);
  1443. update_noise_reduction(s);
  1444. }
  1445. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1446. return ff_xvmc_field_start(s, avctx);
  1447. return 0;
  1448. }
  1449. /* generic function for encode/decode called after a
  1450. * frame has been coded/decoded. */
  1451. void ff_MPV_frame_end(MpegEncContext *s)
  1452. {
  1453. int i;
  1454. /* redraw edges for the frame if decoding didn't complete */
  1455. // just to make sure that all data is rendered.
  1456. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1457. ff_xvmc_field_end(s);
  1458. } else if ((s->er.error_count || s->encoding) &&
  1459. !s->avctx->hwaccel &&
  1460. s->unrestricted_mv &&
  1461. s->current_picture.reference &&
  1462. !s->intra_only &&
  1463. !(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1464. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1465. int hshift = desc->log2_chroma_w;
  1466. int vshift = desc->log2_chroma_h;
  1467. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1468. s->h_edge_pos, s->v_edge_pos,
  1469. EDGE_WIDTH, EDGE_WIDTH,
  1470. EDGE_TOP | EDGE_BOTTOM);
  1471. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1472. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1473. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1474. EDGE_TOP | EDGE_BOTTOM);
  1475. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1476. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1477. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1478. EDGE_TOP | EDGE_BOTTOM);
  1479. }
  1480. emms_c();
  1481. s->last_pict_type = s->pict_type;
  1482. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1483. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1484. s->last_non_b_pict_type = s->pict_type;
  1485. }
  1486. #if 0
  1487. /* copy back current_picture variables */
  1488. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1489. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1490. s->picture[i] = s->current_picture;
  1491. break;
  1492. }
  1493. }
  1494. assert(i < MAX_PICTURE_COUNT);
  1495. #endif
  1496. if (s->encoding) {
  1497. /* release non-reference frames */
  1498. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1499. if (!s->picture[i].reference)
  1500. ff_mpeg_unref_picture(s, &s->picture[i]);
  1501. }
  1502. }
  1503. // clear copies, to avoid confusion
  1504. #if 0
  1505. memset(&s->last_picture, 0, sizeof(Picture));
  1506. memset(&s->next_picture, 0, sizeof(Picture));
  1507. memset(&s->current_picture, 0, sizeof(Picture));
  1508. #endif
  1509. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1510. if (s->current_picture.reference)
  1511. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1512. }
  1513. /**
  1514. * Print debugging info for the given picture.
  1515. */
  1516. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1517. {
  1518. AVFrame *pict;
  1519. if (s->avctx->hwaccel || !p || !p->mb_type)
  1520. return;
  1521. pict = &p->f;
  1522. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1523. int x,y;
  1524. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1525. switch (pict->pict_type) {
  1526. case AV_PICTURE_TYPE_I:
  1527. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1528. break;
  1529. case AV_PICTURE_TYPE_P:
  1530. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1531. break;
  1532. case AV_PICTURE_TYPE_B:
  1533. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1534. break;
  1535. case AV_PICTURE_TYPE_S:
  1536. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1537. break;
  1538. case AV_PICTURE_TYPE_SI:
  1539. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1540. break;
  1541. case AV_PICTURE_TYPE_SP:
  1542. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1543. break;
  1544. }
  1545. for (y = 0; y < s->mb_height; y++) {
  1546. for (x = 0; x < s->mb_width; x++) {
  1547. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1548. int count = s->mbskip_table[x + y * s->mb_stride];
  1549. if (count > 9)
  1550. count = 9;
  1551. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1552. }
  1553. if (s->avctx->debug & FF_DEBUG_QP) {
  1554. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1555. p->qscale_table[x + y * s->mb_stride]);
  1556. }
  1557. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1558. int mb_type = p->mb_type[x + y * s->mb_stride];
  1559. // Type & MV direction
  1560. if (IS_PCM(mb_type))
  1561. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1562. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1563. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1564. else if (IS_INTRA4x4(mb_type))
  1565. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1566. else if (IS_INTRA16x16(mb_type))
  1567. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1568. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1569. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1570. else if (IS_DIRECT(mb_type))
  1571. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1572. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1573. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1574. else if (IS_GMC(mb_type))
  1575. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1576. else if (IS_SKIP(mb_type))
  1577. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1578. else if (!USES_LIST(mb_type, 1))
  1579. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1580. else if (!USES_LIST(mb_type, 0))
  1581. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1582. else {
  1583. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1584. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1585. }
  1586. // segmentation
  1587. if (IS_8X8(mb_type))
  1588. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1589. else if (IS_16X8(mb_type))
  1590. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1591. else if (IS_8X16(mb_type))
  1592. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1593. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1594. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1595. else
  1596. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1597. if (IS_INTERLACED(mb_type))
  1598. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1599. else
  1600. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1601. }
  1602. }
  1603. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1604. }
  1605. }
  1606. }
  1607. /**
  1608. * find the lowest MB row referenced in the MVs
  1609. */
  1610. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1611. {
  1612. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1613. int my, off, i, mvs;
  1614. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1615. goto unhandled;
  1616. switch (s->mv_type) {
  1617. case MV_TYPE_16X16:
  1618. mvs = 1;
  1619. break;
  1620. case MV_TYPE_16X8:
  1621. mvs = 2;
  1622. break;
  1623. case MV_TYPE_8X8:
  1624. mvs = 4;
  1625. break;
  1626. default:
  1627. goto unhandled;
  1628. }
  1629. for (i = 0; i < mvs; i++) {
  1630. my = s->mv[dir][i][1]<<qpel_shift;
  1631. my_max = FFMAX(my_max, my);
  1632. my_min = FFMIN(my_min, my);
  1633. }
  1634. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1635. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1636. unhandled:
  1637. return s->mb_height-1;
  1638. }
  1639. /* put block[] to dest[] */
  1640. static inline void put_dct(MpegEncContext *s,
  1641. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1642. {
  1643. s->dct_unquantize_intra(s, block, i, qscale);
  1644. s->dsp.idct_put (dest, line_size, block);
  1645. }
  1646. /* add block[] to dest[] */
  1647. static inline void add_dct(MpegEncContext *s,
  1648. int16_t *block, int i, uint8_t *dest, int line_size)
  1649. {
  1650. if (s->block_last_index[i] >= 0) {
  1651. s->dsp.idct_add (dest, line_size, block);
  1652. }
  1653. }
  1654. static inline void add_dequant_dct(MpegEncContext *s,
  1655. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1656. {
  1657. if (s->block_last_index[i] >= 0) {
  1658. s->dct_unquantize_inter(s, block, i, qscale);
  1659. s->dsp.idct_add (dest, line_size, block);
  1660. }
  1661. }
  1662. /**
  1663. * Clean dc, ac, coded_block for the current non-intra MB.
  1664. */
  1665. void ff_clean_intra_table_entries(MpegEncContext *s)
  1666. {
  1667. int wrap = s->b8_stride;
  1668. int xy = s->block_index[0];
  1669. s->dc_val[0][xy ] =
  1670. s->dc_val[0][xy + 1 ] =
  1671. s->dc_val[0][xy + wrap] =
  1672. s->dc_val[0][xy + 1 + wrap] = 1024;
  1673. /* ac pred */
  1674. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1675. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1676. if (s->msmpeg4_version>=3) {
  1677. s->coded_block[xy ] =
  1678. s->coded_block[xy + 1 ] =
  1679. s->coded_block[xy + wrap] =
  1680. s->coded_block[xy + 1 + wrap] = 0;
  1681. }
  1682. /* chroma */
  1683. wrap = s->mb_stride;
  1684. xy = s->mb_x + s->mb_y * wrap;
  1685. s->dc_val[1][xy] =
  1686. s->dc_val[2][xy] = 1024;
  1687. /* ac pred */
  1688. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1689. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1690. s->mbintra_table[xy]= 0;
  1691. }
  1692. /* generic function called after a macroblock has been parsed by the
  1693. decoder or after it has been encoded by the encoder.
  1694. Important variables used:
  1695. s->mb_intra : true if intra macroblock
  1696. s->mv_dir : motion vector direction
  1697. s->mv_type : motion vector type
  1698. s->mv : motion vector
  1699. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1700. */
  1701. static av_always_inline
  1702. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1703. int is_mpeg12)
  1704. {
  1705. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1706. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1707. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1708. return;
  1709. }
  1710. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1711. /* print DCT coefficients */
  1712. int i,j;
  1713. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1714. for(i=0; i<6; i++){
  1715. for(j=0; j<64; j++){
  1716. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  1717. }
  1718. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1719. }
  1720. }
  1721. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1722. /* update DC predictors for P macroblocks */
  1723. if (!s->mb_intra) {
  1724. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1725. if(s->mbintra_table[mb_xy])
  1726. ff_clean_intra_table_entries(s);
  1727. } else {
  1728. s->last_dc[0] =
  1729. s->last_dc[1] =
  1730. s->last_dc[2] = 128 << s->intra_dc_precision;
  1731. }
  1732. }
  1733. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1734. s->mbintra_table[mb_xy]=1;
  1735. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1736. uint8_t *dest_y, *dest_cb, *dest_cr;
  1737. int dct_linesize, dct_offset;
  1738. op_pixels_func (*op_pix)[4];
  1739. qpel_mc_func (*op_qpix)[16];
  1740. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1741. const int uvlinesize = s->current_picture.f.linesize[1];
  1742. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1743. const int block_size = 8;
  1744. /* avoid copy if macroblock skipped in last frame too */
  1745. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1746. if(!s->encoding){
  1747. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1748. if (s->mb_skipped) {
  1749. s->mb_skipped= 0;
  1750. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1751. *mbskip_ptr = 1;
  1752. } else if(!s->current_picture.reference) {
  1753. *mbskip_ptr = 1;
  1754. } else{
  1755. *mbskip_ptr = 0; /* not skipped */
  1756. }
  1757. }
  1758. dct_linesize = linesize << s->interlaced_dct;
  1759. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1760. if(readable){
  1761. dest_y= s->dest[0];
  1762. dest_cb= s->dest[1];
  1763. dest_cr= s->dest[2];
  1764. }else{
  1765. dest_y = s->b_scratchpad;
  1766. dest_cb= s->b_scratchpad+16*linesize;
  1767. dest_cr= s->b_scratchpad+32*linesize;
  1768. }
  1769. if (!s->mb_intra) {
  1770. /* motion handling */
  1771. /* decoding or more than one mb_type (MC was already done otherwise) */
  1772. if(!s->encoding){
  1773. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1774. if (s->mv_dir & MV_DIR_FORWARD) {
  1775. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1776. ff_MPV_lowest_referenced_row(s, 0),
  1777. 0);
  1778. }
  1779. if (s->mv_dir & MV_DIR_BACKWARD) {
  1780. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1781. ff_MPV_lowest_referenced_row(s, 1),
  1782. 0);
  1783. }
  1784. }
  1785. op_qpix= s->me.qpel_put;
  1786. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1787. op_pix = s->hdsp.put_pixels_tab;
  1788. }else{
  1789. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1790. }
  1791. if (s->mv_dir & MV_DIR_FORWARD) {
  1792. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1793. op_pix = s->hdsp.avg_pixels_tab;
  1794. op_qpix= s->me.qpel_avg;
  1795. }
  1796. if (s->mv_dir & MV_DIR_BACKWARD) {
  1797. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1798. }
  1799. }
  1800. /* skip dequant / idct if we are really late ;) */
  1801. if(s->avctx->skip_idct){
  1802. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1803. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1804. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1805. goto skip_idct;
  1806. }
  1807. /* add dct residue */
  1808. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1809. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1810. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1811. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1812. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1813. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1814. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1815. if (s->chroma_y_shift){
  1816. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1817. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1818. }else{
  1819. dct_linesize >>= 1;
  1820. dct_offset >>=1;
  1821. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1822. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1823. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1824. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1825. }
  1826. }
  1827. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1828. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1829. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1830. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1831. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1832. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1833. if(s->chroma_y_shift){//Chroma420
  1834. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1835. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1836. }else{
  1837. //chroma422
  1838. dct_linesize = uvlinesize << s->interlaced_dct;
  1839. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1840. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1841. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1842. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1843. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1844. if(!s->chroma_x_shift){//Chroma444
  1845. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1846. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1847. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1848. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1849. }
  1850. }
  1851. }//fi gray
  1852. }
  1853. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1854. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1855. }
  1856. } else {
  1857. /* dct only in intra block */
  1858. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1859. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1860. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1861. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1862. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1863. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1864. if(s->chroma_y_shift){
  1865. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1866. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1867. }else{
  1868. dct_offset >>=1;
  1869. dct_linesize >>=1;
  1870. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1871. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1872. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1873. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1874. }
  1875. }
  1876. }else{
  1877. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1878. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1879. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1880. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1881. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1882. if(s->chroma_y_shift){
  1883. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1884. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1885. }else{
  1886. dct_linesize = uvlinesize << s->interlaced_dct;
  1887. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1888. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1889. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1890. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1891. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1892. if(!s->chroma_x_shift){//Chroma444
  1893. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1894. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1895. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1896. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1897. }
  1898. }
  1899. }//gray
  1900. }
  1901. }
  1902. skip_idct:
  1903. if(!readable){
  1904. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1905. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1906. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1907. }
  1908. }
  1909. }
  1910. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  1911. #if !CONFIG_SMALL
  1912. if(s->out_format == FMT_MPEG1) {
  1913. MPV_decode_mb_internal(s, block, 1);
  1914. } else
  1915. #endif
  1916. MPV_decode_mb_internal(s, block, 0);
  1917. }
  1918. /**
  1919. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1920. */
  1921. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  1922. Picture *last, int y, int h, int picture_structure,
  1923. int first_field, int draw_edges, int low_delay,
  1924. int v_edge_pos, int h_edge_pos)
  1925. {
  1926. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1927. int hshift = desc->log2_chroma_w;
  1928. int vshift = desc->log2_chroma_h;
  1929. const int field_pic = picture_structure != PICT_FRAME;
  1930. if(field_pic){
  1931. h <<= 1;
  1932. y <<= 1;
  1933. }
  1934. if (!avctx->hwaccel &&
  1935. draw_edges &&
  1936. cur->reference &&
  1937. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1938. int *linesize = cur->f.linesize;
  1939. int sides = 0, edge_h;
  1940. if (y==0) sides |= EDGE_TOP;
  1941. if (y + h >= v_edge_pos)
  1942. sides |= EDGE_BOTTOM;
  1943. edge_h= FFMIN(h, v_edge_pos - y);
  1944. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  1945. linesize[0], h_edge_pos, edge_h,
  1946. EDGE_WIDTH, EDGE_WIDTH, sides);
  1947. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  1948. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  1949. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1950. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  1951. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  1952. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1953. }
  1954. h = FFMIN(h, avctx->height - y);
  1955. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1956. if (avctx->draw_horiz_band) {
  1957. AVFrame *src;
  1958. int offset[AV_NUM_DATA_POINTERS];
  1959. int i;
  1960. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  1961. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  1962. src = &cur->f;
  1963. else if (last)
  1964. src = &last->f;
  1965. else
  1966. return;
  1967. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  1968. picture_structure == PICT_FRAME &&
  1969. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  1970. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  1971. offset[i] = 0;
  1972. }else{
  1973. offset[0]= y * src->linesize[0];
  1974. offset[1]=
  1975. offset[2]= (y >> vshift) * src->linesize[1];
  1976. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1977. offset[i] = 0;
  1978. }
  1979. emms_c();
  1980. avctx->draw_horiz_band(avctx, src, offset,
  1981. y, picture_structure, h);
  1982. }
  1983. }
  1984. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1985. {
  1986. int draw_edges = s->unrestricted_mv && !s->intra_only;
  1987. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  1988. &s->last_picture, y, h, s->picture_structure,
  1989. s->first_field, draw_edges, s->low_delay,
  1990. s->v_edge_pos, s->h_edge_pos);
  1991. }
  1992. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1993. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1994. const int uvlinesize = s->current_picture.f.linesize[1];
  1995. const int mb_size= 4;
  1996. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1997. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1998. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1999. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2000. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2001. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2002. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2003. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2004. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2005. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2006. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2007. {
  2008. if(s->picture_structure==PICT_FRAME){
  2009. s->dest[0] += s->mb_y * linesize << mb_size;
  2010. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2011. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2012. }else{
  2013. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2014. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2015. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2016. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2017. }
  2018. }
  2019. }
  2020. /**
  2021. * Permute an 8x8 block.
  2022. * @param block the block which will be permuted according to the given permutation vector
  2023. * @param permutation the permutation vector
  2024. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2025. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2026. * (inverse) permutated to scantable order!
  2027. */
  2028. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2029. {
  2030. int i;
  2031. int16_t temp[64];
  2032. if(last<=0) return;
  2033. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2034. for(i=0; i<=last; i++){
  2035. const int j= scantable[i];
  2036. temp[j]= block[j];
  2037. block[j]=0;
  2038. }
  2039. for(i=0; i<=last; i++){
  2040. const int j= scantable[i];
  2041. const int perm_j= permutation[j];
  2042. block[perm_j]= temp[j];
  2043. }
  2044. }
  2045. void ff_mpeg_flush(AVCodecContext *avctx){
  2046. int i;
  2047. MpegEncContext *s = avctx->priv_data;
  2048. if(s==NULL || s->picture==NULL)
  2049. return;
  2050. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2051. ff_mpeg_unref_picture(s, &s->picture[i]);
  2052. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2053. ff_mpeg_unref_picture(s, &s->current_picture);
  2054. ff_mpeg_unref_picture(s, &s->last_picture);
  2055. ff_mpeg_unref_picture(s, &s->next_picture);
  2056. s->mb_x= s->mb_y= 0;
  2057. s->parse_context.state= -1;
  2058. s->parse_context.frame_start_found= 0;
  2059. s->parse_context.overread= 0;
  2060. s->parse_context.overread_index= 0;
  2061. s->parse_context.index= 0;
  2062. s->parse_context.last_index= 0;
  2063. s->bitstream_buffer_size=0;
  2064. s->pp_time=0;
  2065. }
  2066. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2067. int16_t *block, int n, int qscale)
  2068. {
  2069. int i, level, nCoeffs;
  2070. const uint16_t *quant_matrix;
  2071. nCoeffs= s->block_last_index[n];
  2072. if (n < 4)
  2073. block[0] = block[0] * s->y_dc_scale;
  2074. else
  2075. block[0] = block[0] * s->c_dc_scale;
  2076. /* XXX: only mpeg1 */
  2077. quant_matrix = s->intra_matrix;
  2078. for(i=1;i<=nCoeffs;i++) {
  2079. int j= s->intra_scantable.permutated[i];
  2080. level = block[j];
  2081. if (level) {
  2082. if (level < 0) {
  2083. level = -level;
  2084. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2085. level = (level - 1) | 1;
  2086. level = -level;
  2087. } else {
  2088. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2089. level = (level - 1) | 1;
  2090. }
  2091. block[j] = level;
  2092. }
  2093. }
  2094. }
  2095. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2096. int16_t *block, int n, int qscale)
  2097. {
  2098. int i, level, nCoeffs;
  2099. const uint16_t *quant_matrix;
  2100. nCoeffs= s->block_last_index[n];
  2101. quant_matrix = s->inter_matrix;
  2102. for(i=0; i<=nCoeffs; i++) {
  2103. int j= s->intra_scantable.permutated[i];
  2104. level = block[j];
  2105. if (level) {
  2106. if (level < 0) {
  2107. level = -level;
  2108. level = (((level << 1) + 1) * qscale *
  2109. ((int) (quant_matrix[j]))) >> 4;
  2110. level = (level - 1) | 1;
  2111. level = -level;
  2112. } else {
  2113. level = (((level << 1) + 1) * qscale *
  2114. ((int) (quant_matrix[j]))) >> 4;
  2115. level = (level - 1) | 1;
  2116. }
  2117. block[j] = level;
  2118. }
  2119. }
  2120. }
  2121. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2122. int16_t *block, int n, int qscale)
  2123. {
  2124. int i, level, nCoeffs;
  2125. const uint16_t *quant_matrix;
  2126. if(s->alternate_scan) nCoeffs= 63;
  2127. else nCoeffs= s->block_last_index[n];
  2128. if (n < 4)
  2129. block[0] = block[0] * s->y_dc_scale;
  2130. else
  2131. block[0] = block[0] * s->c_dc_scale;
  2132. quant_matrix = s->intra_matrix;
  2133. for(i=1;i<=nCoeffs;i++) {
  2134. int j= s->intra_scantable.permutated[i];
  2135. level = block[j];
  2136. if (level) {
  2137. if (level < 0) {
  2138. level = -level;
  2139. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2140. level = -level;
  2141. } else {
  2142. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2143. }
  2144. block[j] = level;
  2145. }
  2146. }
  2147. }
  2148. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2149. int16_t *block, int n, int qscale)
  2150. {
  2151. int i, level, nCoeffs;
  2152. const uint16_t *quant_matrix;
  2153. int sum=-1;
  2154. if(s->alternate_scan) nCoeffs= 63;
  2155. else nCoeffs= s->block_last_index[n];
  2156. if (n < 4)
  2157. block[0] = block[0] * s->y_dc_scale;
  2158. else
  2159. block[0] = block[0] * s->c_dc_scale;
  2160. quant_matrix = s->intra_matrix;
  2161. for(i=1;i<=nCoeffs;i++) {
  2162. int j= s->intra_scantable.permutated[i];
  2163. level = block[j];
  2164. if (level) {
  2165. if (level < 0) {
  2166. level = -level;
  2167. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2168. level = -level;
  2169. } else {
  2170. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2171. }
  2172. block[j] = level;
  2173. sum+=level;
  2174. }
  2175. }
  2176. block[63]^=sum&1;
  2177. }
  2178. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2179. int16_t *block, int n, int qscale)
  2180. {
  2181. int i, level, nCoeffs;
  2182. const uint16_t *quant_matrix;
  2183. int sum=-1;
  2184. if(s->alternate_scan) nCoeffs= 63;
  2185. else nCoeffs= s->block_last_index[n];
  2186. quant_matrix = s->inter_matrix;
  2187. for(i=0; i<=nCoeffs; i++) {
  2188. int j= s->intra_scantable.permutated[i];
  2189. level = block[j];
  2190. if (level) {
  2191. if (level < 0) {
  2192. level = -level;
  2193. level = (((level << 1) + 1) * qscale *
  2194. ((int) (quant_matrix[j]))) >> 4;
  2195. level = -level;
  2196. } else {
  2197. level = (((level << 1) + 1) * qscale *
  2198. ((int) (quant_matrix[j]))) >> 4;
  2199. }
  2200. block[j] = level;
  2201. sum+=level;
  2202. }
  2203. }
  2204. block[63]^=sum&1;
  2205. }
  2206. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2207. int16_t *block, int n, int qscale)
  2208. {
  2209. int i, level, qmul, qadd;
  2210. int nCoeffs;
  2211. assert(s->block_last_index[n]>=0);
  2212. qmul = qscale << 1;
  2213. if (!s->h263_aic) {
  2214. if (n < 4)
  2215. block[0] = block[0] * s->y_dc_scale;
  2216. else
  2217. block[0] = block[0] * s->c_dc_scale;
  2218. qadd = (qscale - 1) | 1;
  2219. }else{
  2220. qadd = 0;
  2221. }
  2222. if(s->ac_pred)
  2223. nCoeffs=63;
  2224. else
  2225. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2226. for(i=1; i<=nCoeffs; i++) {
  2227. level = block[i];
  2228. if (level) {
  2229. if (level < 0) {
  2230. level = level * qmul - qadd;
  2231. } else {
  2232. level = level * qmul + qadd;
  2233. }
  2234. block[i] = level;
  2235. }
  2236. }
  2237. }
  2238. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2239. int16_t *block, int n, int qscale)
  2240. {
  2241. int i, level, qmul, qadd;
  2242. int nCoeffs;
  2243. assert(s->block_last_index[n]>=0);
  2244. qadd = (qscale - 1) | 1;
  2245. qmul = qscale << 1;
  2246. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2247. for(i=0; i<=nCoeffs; i++) {
  2248. level = block[i];
  2249. if (level) {
  2250. if (level < 0) {
  2251. level = level * qmul - qadd;
  2252. } else {
  2253. level = level * qmul + qadd;
  2254. }
  2255. block[i] = level;
  2256. }
  2257. }
  2258. }
  2259. /**
  2260. * set qscale and update qscale dependent variables.
  2261. */
  2262. void ff_set_qscale(MpegEncContext * s, int qscale)
  2263. {
  2264. if (qscale < 1)
  2265. qscale = 1;
  2266. else if (qscale > 31)
  2267. qscale = 31;
  2268. s->qscale = qscale;
  2269. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2270. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2271. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2272. }
  2273. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2274. {
  2275. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2276. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2277. }
  2278. #if CONFIG_ERROR_RESILIENCE
  2279. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2280. {
  2281. ERContext *er = &s->er;
  2282. er->cur_pic = s->current_picture_ptr;
  2283. er->last_pic = s->last_picture_ptr;
  2284. er->next_pic = s->next_picture_ptr;
  2285. er->pp_time = s->pp_time;
  2286. er->pb_time = s->pb_time;
  2287. er->quarter_sample = s->quarter_sample;
  2288. er->partitioned_frame = s->partitioned_frame;
  2289. ff_er_frame_start(er);
  2290. }
  2291. #endif /* CONFIG_ERROR_RESILIENCE */