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.

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