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.

2630 lines
90KB

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