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.

3327 lines
121KB

  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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "h264chroma.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. //#define DEBUG
  58. static const uint8_t ff_default_chroma_qscale_table[32] = {
  59. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  60. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  61. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  62. };
  63. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  64. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. 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. };
  74. static const uint8_t mpeg2_dc_scale_table1[128] = {
  75. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  78. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  79. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  80. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  81. 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. };
  85. static const uint8_t mpeg2_dc_scale_table2[128] = {
  86. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  90. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  91. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  92. 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. };
  96. static const uint8_t mpeg2_dc_scale_table3[128] = {
  97. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  98. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  99. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  100. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  101. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  102. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  103. 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. };
  107. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  108. ff_mpeg1_dc_scale_table,
  109. mpeg2_dc_scale_table1,
  110. mpeg2_dc_scale_table2,
  111. mpeg2_dc_scale_table3,
  112. };
  113. const enum AVPixelFormat ff_pixfmt_list_420[] = {
  114. AV_PIX_FMT_YUV420P,
  115. AV_PIX_FMT_NONE
  116. };
  117. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  118. int (*mv)[2][4][2],
  119. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  120. {
  121. MpegEncContext *s = opaque;
  122. s->mv_dir = mv_dir;
  123. s->mv_type = mv_type;
  124. s->mb_intra = mb_intra;
  125. s->mb_skipped = mb_skipped;
  126. s->mb_x = mb_x;
  127. s->mb_y = mb_y;
  128. memcpy(s->mv, mv, sizeof(*mv));
  129. ff_init_block_index(s);
  130. ff_update_block_index(s);
  131. s->dsp.clear_blocks(s->block[0]);
  132. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  133. 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);
  134. 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);
  135. assert(ref == 0);
  136. ff_MPV_decode_mb(s, s->block);
  137. }
  138. /* init common dct for both encoder and decoder */
  139. av_cold int ff_dct_common_init(MpegEncContext *s)
  140. {
  141. ff_dsputil_init(&s->dsp, s->avctx);
  142. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  143. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  144. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  145. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  146. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  147. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  148. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  149. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  150. if (s->flags & CODEC_FLAG_BITEXACT)
  151. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  152. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  153. #if ARCH_X86
  154. ff_MPV_common_init_x86(s);
  155. #elif ARCH_ALPHA
  156. ff_MPV_common_init_axp(s);
  157. #elif ARCH_ARM
  158. ff_MPV_common_init_arm(s);
  159. #elif HAVE_ALTIVEC
  160. ff_MPV_common_init_altivec(s);
  161. #elif ARCH_BFIN
  162. ff_MPV_common_init_bfin(s);
  163. #endif
  164. /* load & permutate scantables
  165. * note: only wmv uses different ones
  166. */
  167. if (s->alternate_scan) {
  168. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  170. } else {
  171. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  172. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  173. }
  174. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  175. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  176. return 0;
  177. }
  178. int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
  179. {
  180. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  181. // edge emu needs blocksize + filter length - 1
  182. // (= 17x17 for halfpel / 21x21 for h264)
  183. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  184. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  185. // linesize * interlaced * MBsize
  186. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
  187. fail);
  188. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
  189. fail)
  190. s->me.temp = s->me.scratchpad;
  191. s->rd_scratchpad = s->me.scratchpad;
  192. s->b_scratchpad = s->me.scratchpad;
  193. s->obmc_scratchpad = s->me.scratchpad + 16;
  194. return 0;
  195. fail:
  196. av_freep(&s->edge_emu_buffer);
  197. return AVERROR(ENOMEM);
  198. }
  199. /**
  200. * Allocate a frame buffer
  201. */
  202. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  203. {
  204. int r, ret;
  205. pic->tf.f = &pic->f;
  206. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  207. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  208. s->codec_id != AV_CODEC_ID_MSS2)
  209. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  210. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  211. else {
  212. pic->f.width = s->avctx->width;
  213. pic->f.height = s->avctx->height;
  214. pic->f.format = s->avctx->pix_fmt;
  215. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  216. }
  217. if (r < 0 || !pic->f.data[0]) {
  218. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  219. r, pic->f.data[0]);
  220. return -1;
  221. }
  222. if (s->avctx->hwaccel) {
  223. assert(!pic->hwaccel_picture_private);
  224. if (s->avctx->hwaccel->priv_data_size) {
  225. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  226. if (!pic->hwaccel_priv_buf) {
  227. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  228. return -1;
  229. }
  230. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  231. }
  232. }
  233. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  234. s->uvlinesize != pic->f.linesize[1])) {
  235. av_log(s->avctx, AV_LOG_ERROR,
  236. "get_buffer() failed (stride changed)\n");
  237. ff_mpeg_unref_picture(s, pic);
  238. return -1;
  239. }
  240. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  241. av_log(s->avctx, AV_LOG_ERROR,
  242. "get_buffer() failed (uv stride mismatch)\n");
  243. ff_mpeg_unref_picture(s, pic);
  244. return -1;
  245. }
  246. if (!s->edge_emu_buffer &&
  247. (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  248. av_log(s->avctx, AV_LOG_ERROR,
  249. "get_buffer() failed to allocate context scratch buffers.\n");
  250. ff_mpeg_unref_picture(s, pic);
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static void free_picture_tables(Picture *pic)
  256. {
  257. int i;
  258. av_buffer_unref(&pic->mb_var_buf);
  259. av_buffer_unref(&pic->mc_mb_var_buf);
  260. av_buffer_unref(&pic->mb_mean_buf);
  261. av_buffer_unref(&pic->mbskip_table_buf);
  262. av_buffer_unref(&pic->qscale_table_buf);
  263. av_buffer_unref(&pic->mb_type_buf);
  264. for (i = 0; i < 2; i++) {
  265. av_buffer_unref(&pic->motion_val_buf[i]);
  266. av_buffer_unref(&pic->ref_index_buf[i]);
  267. }
  268. }
  269. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  270. {
  271. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  272. const int mb_array_size = s->mb_stride * s->mb_height;
  273. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  274. int i;
  275. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  276. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  277. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  278. sizeof(uint32_t));
  279. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  280. return AVERROR(ENOMEM);
  281. if (s->encoding) {
  282. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  283. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  284. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  285. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  286. return AVERROR(ENOMEM);
  287. }
  288. if (s->out_format == FMT_H263 || s->encoding ||
  289. (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
  290. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  291. int ref_index_size = 4 * mb_array_size;
  292. for (i = 0; mv_size && i < 2; i++) {
  293. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  294. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  295. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  296. return AVERROR(ENOMEM);
  297. }
  298. }
  299. return 0;
  300. }
  301. static int make_tables_writable(Picture *pic)
  302. {
  303. int ret, i;
  304. #define MAKE_WRITABLE(table) \
  305. do {\
  306. if (pic->table &&\
  307. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  308. return ret;\
  309. } while (0)
  310. MAKE_WRITABLE(mb_var_buf);
  311. MAKE_WRITABLE(mc_mb_var_buf);
  312. MAKE_WRITABLE(mb_mean_buf);
  313. MAKE_WRITABLE(mbskip_table_buf);
  314. MAKE_WRITABLE(qscale_table_buf);
  315. MAKE_WRITABLE(mb_type_buf);
  316. for (i = 0; i < 2; i++) {
  317. MAKE_WRITABLE(motion_val_buf[i]);
  318. MAKE_WRITABLE(ref_index_buf[i]);
  319. }
  320. return 0;
  321. }
  322. /**
  323. * Allocate a Picture.
  324. * The pixels are allocated/set by calling get_buffer() if shared = 0
  325. */
  326. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  327. {
  328. int i, ret;
  329. if (pic->qscale_table_buf)
  330. if (pic->mbskip_table_buf->size < s->mb_stride * s->mb_height + 2 ||
  331. pic->qscale_table_buf->size < s->mb_stride * (s->mb_height + 1) + 1 + s->mb_stride)
  332. free_picture_tables(pic);
  333. if (shared) {
  334. assert(pic->f.data[0]);
  335. pic->shared = 1;
  336. } else {
  337. assert(!pic->f.data[0]);
  338. if (alloc_frame_buffer(s, pic) < 0)
  339. return -1;
  340. s->linesize = pic->f.linesize[0];
  341. s->uvlinesize = pic->f.linesize[1];
  342. }
  343. if (!pic->qscale_table_buf)
  344. ret = alloc_picture_tables(s, pic);
  345. else
  346. ret = make_tables_writable(pic);
  347. if (ret < 0)
  348. goto fail;
  349. if (s->encoding) {
  350. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  351. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  352. pic->mb_mean = pic->mb_mean_buf->data;
  353. }
  354. pic->mbskip_table = pic->mbskip_table_buf->data;
  355. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  356. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  357. if (pic->motion_val_buf[0]) {
  358. for (i = 0; i < 2; i++) {
  359. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  360. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  361. }
  362. }
  363. return 0;
  364. fail:
  365. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  366. ff_mpeg_unref_picture(s, pic);
  367. free_picture_tables(pic);
  368. return AVERROR(ENOMEM);
  369. }
  370. /**
  371. * Deallocate a picture.
  372. */
  373. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  374. {
  375. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  376. pic->tf.f = &pic->f;
  377. /* WM Image / Screen codecs allocate internal buffers with different
  378. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  379. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  380. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  381. s->codec_id != AV_CODEC_ID_MSS2)
  382. ff_thread_release_buffer(s->avctx, &pic->tf);
  383. else
  384. av_frame_unref(&pic->f);
  385. av_buffer_unref(&pic->hwaccel_priv_buf);
  386. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  387. }
  388. static int update_picture_tables(Picture *dst, Picture *src)
  389. {
  390. int i;
  391. #define UPDATE_TABLE(table)\
  392. do {\
  393. if (src->table &&\
  394. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  395. av_buffer_unref(&dst->table);\
  396. dst->table = av_buffer_ref(src->table);\
  397. if (!dst->table) {\
  398. free_picture_tables(dst);\
  399. return AVERROR(ENOMEM);\
  400. }\
  401. }\
  402. } while (0)
  403. UPDATE_TABLE(mb_var_buf);
  404. UPDATE_TABLE(mc_mb_var_buf);
  405. UPDATE_TABLE(mb_mean_buf);
  406. UPDATE_TABLE(mbskip_table_buf);
  407. UPDATE_TABLE(qscale_table_buf);
  408. UPDATE_TABLE(mb_type_buf);
  409. for (i = 0; i < 2; i++) {
  410. UPDATE_TABLE(motion_val_buf[i]);
  411. UPDATE_TABLE(ref_index_buf[i]);
  412. }
  413. dst->mb_var = src->mb_var;
  414. dst->mc_mb_var = src->mc_mb_var;
  415. dst->mb_mean = src->mb_mean;
  416. dst->mbskip_table = src->mbskip_table;
  417. dst->qscale_table = src->qscale_table;
  418. dst->mb_type = src->mb_type;
  419. for (i = 0; i < 2; i++) {
  420. dst->motion_val[i] = src->motion_val[i];
  421. dst->ref_index[i] = src->ref_index[i];
  422. }
  423. return 0;
  424. }
  425. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  426. {
  427. int ret;
  428. av_assert0(!dst->f.buf[0]);
  429. av_assert0(src->f.buf[0]);
  430. src->tf.f = &src->f;
  431. dst->tf.f = &dst->f;
  432. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  433. if (ret < 0)
  434. goto fail;
  435. ret = update_picture_tables(dst, src);
  436. if (ret < 0)
  437. goto fail;
  438. if (src->hwaccel_picture_private) {
  439. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  440. if (!dst->hwaccel_priv_buf)
  441. goto fail;
  442. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  443. }
  444. dst->field_picture = src->field_picture;
  445. dst->mb_var_sum = src->mb_var_sum;
  446. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  447. dst->b_frame_score = src->b_frame_score;
  448. dst->needs_realloc = src->needs_realloc;
  449. dst->reference = src->reference;
  450. dst->shared = src->shared;
  451. return 0;
  452. fail:
  453. ff_mpeg_unref_picture(s, dst);
  454. return ret;
  455. }
  456. static int init_duplicate_context(MpegEncContext *s)
  457. {
  458. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  459. int c_size = s->mb_stride * (s->mb_height + 1);
  460. int yc_size = y_size + 2 * c_size;
  461. int i;
  462. s->edge_emu_buffer =
  463. s->me.scratchpad =
  464. s->me.temp =
  465. s->rd_scratchpad =
  466. s->b_scratchpad =
  467. s->obmc_scratchpad = NULL;
  468. if (s->encoding) {
  469. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  470. ME_MAP_SIZE * sizeof(uint32_t), fail)
  471. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  472. ME_MAP_SIZE * sizeof(uint32_t), fail)
  473. if (s->avctx->noise_reduction) {
  474. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  475. 2 * 64 * sizeof(int), fail)
  476. }
  477. }
  478. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  479. s->block = s->blocks[0];
  480. for (i = 0; i < 12; i++) {
  481. s->pblocks[i] = &s->block[i];
  482. }
  483. if (s->out_format == FMT_H263) {
  484. /* ac values */
  485. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  486. yc_size * sizeof(int16_t) * 16, fail);
  487. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  488. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  489. s->ac_val[2] = s->ac_val[1] + c_size;
  490. }
  491. return 0;
  492. fail:
  493. return -1; // free() through ff_MPV_common_end()
  494. }
  495. static void free_duplicate_context(MpegEncContext *s)
  496. {
  497. if (s == NULL)
  498. return;
  499. av_freep(&s->edge_emu_buffer);
  500. av_freep(&s->me.scratchpad);
  501. s->me.temp =
  502. s->rd_scratchpad =
  503. s->b_scratchpad =
  504. s->obmc_scratchpad = NULL;
  505. av_freep(&s->dct_error_sum);
  506. av_freep(&s->me.map);
  507. av_freep(&s->me.score_map);
  508. av_freep(&s->blocks);
  509. av_freep(&s->ac_val_base);
  510. s->block = NULL;
  511. }
  512. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  513. {
  514. #define COPY(a) bak->a = src->a
  515. COPY(edge_emu_buffer);
  516. COPY(me.scratchpad);
  517. COPY(me.temp);
  518. COPY(rd_scratchpad);
  519. COPY(b_scratchpad);
  520. COPY(obmc_scratchpad);
  521. COPY(me.map);
  522. COPY(me.score_map);
  523. COPY(blocks);
  524. COPY(block);
  525. COPY(start_mb_y);
  526. COPY(end_mb_y);
  527. COPY(me.map_generation);
  528. COPY(pb);
  529. COPY(dct_error_sum);
  530. COPY(dct_count[0]);
  531. COPY(dct_count[1]);
  532. COPY(ac_val_base);
  533. COPY(ac_val[0]);
  534. COPY(ac_val[1]);
  535. COPY(ac_val[2]);
  536. #undef COPY
  537. }
  538. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  539. {
  540. MpegEncContext bak;
  541. int i, ret;
  542. // FIXME copy only needed parts
  543. // START_TIMER
  544. backup_duplicate_context(&bak, dst);
  545. memcpy(dst, src, sizeof(MpegEncContext));
  546. backup_duplicate_context(dst, &bak);
  547. for (i = 0; i < 12; i++) {
  548. dst->pblocks[i] = &dst->block[i];
  549. }
  550. if (!dst->edge_emu_buffer &&
  551. (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
  552. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  553. "scratch buffers.\n");
  554. return ret;
  555. }
  556. // STOP_TIMER("update_duplicate_context")
  557. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  558. return 0;
  559. }
  560. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  561. const AVCodecContext *src)
  562. {
  563. int i, ret;
  564. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  565. if (dst == src)
  566. return 0;
  567. av_assert0(s != s1);
  568. // FIXME can parameters change on I-frames?
  569. // in that case dst may need a reinit
  570. if (!s->context_initialized) {
  571. memcpy(s, s1, sizeof(MpegEncContext));
  572. s->avctx = dst;
  573. s->bitstream_buffer = NULL;
  574. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  575. if (s1->context_initialized){
  576. // s->picture_range_start += MAX_PICTURE_COUNT;
  577. // s->picture_range_end += MAX_PICTURE_COUNT;
  578. if((ret = ff_MPV_common_init(s)) < 0){
  579. memset(s, 0, sizeof(MpegEncContext));
  580. s->avctx = dst;
  581. return ret;
  582. }
  583. }
  584. }
  585. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  586. s->context_reinit = 0;
  587. s->height = s1->height;
  588. s->width = s1->width;
  589. if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
  590. return ret;
  591. }
  592. s->avctx->coded_height = s1->avctx->coded_height;
  593. s->avctx->coded_width = s1->avctx->coded_width;
  594. s->avctx->width = s1->avctx->width;
  595. s->avctx->height = s1->avctx->height;
  596. s->coded_picture_number = s1->coded_picture_number;
  597. s->picture_number = s1->picture_number;
  598. s->input_picture_number = s1->input_picture_number;
  599. av_assert0(!s->picture || s->picture != s1->picture);
  600. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  601. ff_mpeg_unref_picture(s, &s->picture[i]);
  602. if (s1->picture[i].f.data[0] &&
  603. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  604. return ret;
  605. }
  606. #define UPDATE_PICTURE(pic)\
  607. do {\
  608. ff_mpeg_unref_picture(s, &s->pic);\
  609. if (s1->pic.f.data[0])\
  610. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  611. else\
  612. ret = update_picture_tables(&s->pic, &s1->pic);\
  613. if (ret < 0)\
  614. return ret;\
  615. } while (0)
  616. UPDATE_PICTURE(current_picture);
  617. UPDATE_PICTURE(last_picture);
  618. UPDATE_PICTURE(next_picture);
  619. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  620. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  621. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  622. // Error/bug resilience
  623. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  624. s->workaround_bugs = s1->workaround_bugs;
  625. s->padding_bug_score = s1->padding_bug_score;
  626. // MPEG4 timing info
  627. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  628. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  629. // B-frame info
  630. s->max_b_frames = s1->max_b_frames;
  631. s->low_delay = s1->low_delay;
  632. s->droppable = s1->droppable;
  633. // DivX handling (doesn't work)
  634. s->divx_packed = s1->divx_packed;
  635. if (s1->bitstream_buffer) {
  636. if (s1->bitstream_buffer_size +
  637. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  638. av_fast_malloc(&s->bitstream_buffer,
  639. &s->allocated_bitstream_buffer_size,
  640. s1->allocated_bitstream_buffer_size);
  641. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  642. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  643. s1->bitstream_buffer_size);
  644. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  645. FF_INPUT_BUFFER_PADDING_SIZE);
  646. }
  647. // linesize dependend scratch buffer allocation
  648. if (!s->edge_emu_buffer)
  649. if (s1->linesize) {
  650. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  651. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  652. "scratch buffers.\n");
  653. return AVERROR(ENOMEM);
  654. }
  655. } else {
  656. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  657. "be allocated due to unknown size.\n");
  658. }
  659. // MPEG2/interlacing info
  660. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  661. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  662. if (!s1->first_field) {
  663. s->last_pict_type = s1->pict_type;
  664. if (s1->current_picture_ptr)
  665. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  666. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  667. s->last_non_b_pict_type = s1->pict_type;
  668. }
  669. }
  670. return 0;
  671. }
  672. /**
  673. * Set the given MpegEncContext to common defaults
  674. * (same for encoding and decoding).
  675. * The changed fields will not depend upon the
  676. * prior state of the MpegEncContext.
  677. */
  678. void ff_MPV_common_defaults(MpegEncContext *s)
  679. {
  680. s->y_dc_scale_table =
  681. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  682. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  683. s->progressive_frame = 1;
  684. s->progressive_sequence = 1;
  685. s->picture_structure = PICT_FRAME;
  686. s->coded_picture_number = 0;
  687. s->picture_number = 0;
  688. s->input_picture_number = 0;
  689. s->picture_in_gop_number = 0;
  690. s->f_code = 1;
  691. s->b_code = 1;
  692. s->slice_context_count = 1;
  693. }
  694. /**
  695. * Set the given MpegEncContext to defaults for decoding.
  696. * the changed fields will not depend upon
  697. * the prior state of the MpegEncContext.
  698. */
  699. void ff_MPV_decode_defaults(MpegEncContext *s)
  700. {
  701. ff_MPV_common_defaults(s);
  702. }
  703. static int init_er(MpegEncContext *s)
  704. {
  705. ERContext *er = &s->er;
  706. int mb_array_size = s->mb_height * s->mb_stride;
  707. int i;
  708. er->avctx = s->avctx;
  709. er->dsp = &s->dsp;
  710. er->mb_index2xy = s->mb_index2xy;
  711. er->mb_num = s->mb_num;
  712. er->mb_width = s->mb_width;
  713. er->mb_height = s->mb_height;
  714. er->mb_stride = s->mb_stride;
  715. er->b8_stride = s->b8_stride;
  716. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  717. er->error_status_table = av_mallocz(mb_array_size);
  718. if (!er->er_temp_buffer || !er->error_status_table)
  719. goto fail;
  720. er->mbskip_table = s->mbskip_table;
  721. er->mbintra_table = s->mbintra_table;
  722. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  723. er->dc_val[i] = s->dc_val[i];
  724. er->decode_mb = mpeg_er_decode_mb;
  725. er->opaque = s;
  726. return 0;
  727. fail:
  728. av_freep(&er->er_temp_buffer);
  729. av_freep(&er->error_status_table);
  730. return AVERROR(ENOMEM);
  731. }
  732. /**
  733. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  734. */
  735. static int init_context_frame(MpegEncContext *s)
  736. {
  737. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  738. s->mb_width = (s->width + 15) / 16;
  739. s->mb_stride = s->mb_width + 1;
  740. s->b8_stride = s->mb_width * 2 + 1;
  741. s->b4_stride = s->mb_width * 4 + 1;
  742. mb_array_size = s->mb_height * s->mb_stride;
  743. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  744. /* set default edge pos, will be overriden
  745. * in decode_header if needed */
  746. s->h_edge_pos = s->mb_width * 16;
  747. s->v_edge_pos = s->mb_height * 16;
  748. s->mb_num = s->mb_width * s->mb_height;
  749. s->block_wrap[0] =
  750. s->block_wrap[1] =
  751. s->block_wrap[2] =
  752. s->block_wrap[3] = s->b8_stride;
  753. s->block_wrap[4] =
  754. s->block_wrap[5] = s->mb_stride;
  755. y_size = s->b8_stride * (2 * s->mb_height + 1);
  756. c_size = s->mb_stride * (s->mb_height + 1);
  757. yc_size = y_size + 2 * c_size;
  758. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  759. for (y = 0; y < s->mb_height; y++)
  760. for (x = 0; x < s->mb_width; x++)
  761. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  762. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  763. if (s->encoding) {
  764. /* Allocate MV tables */
  765. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  766. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  767. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  768. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  769. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  770. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  771. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  772. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  773. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  774. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  775. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  776. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  777. /* Allocate MB type table */
  778. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  779. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * 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], mb_array_size * 2 * sizeof(uint8_t), fail)
  800. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  801. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  802. }
  803. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  804. }
  805. }
  806. if (s->out_format == FMT_H263) {
  807. /* cbp values */
  808. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  809. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  810. /* cbp, ac_pred, pred_dir */
  811. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  812. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  813. }
  814. if (s->h263_pred || s->h263_plus || !s->encoding) {
  815. /* dc values */
  816. // MN: we need these for error resilience of intra-frames
  817. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  818. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  819. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  820. s->dc_val[2] = s->dc_val[1] + c_size;
  821. for (i = 0; i < yc_size; i++)
  822. s->dc_val_base[i] = 1024;
  823. }
  824. /* which mb is a intra block */
  825. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  826. memset(s->mbintra_table, 1, mb_array_size);
  827. /* init macroblock skip table */
  828. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  829. // Note the + 1 is for a quicker mpeg4 slice_end detection
  830. return init_er(s);
  831. fail:
  832. return AVERROR(ENOMEM);
  833. }
  834. /**
  835. * init common structure for both encoder and decoder.
  836. * this assumes that some variables like width/height are already set
  837. */
  838. av_cold int ff_MPV_common_init(MpegEncContext *s)
  839. {
  840. int i;
  841. int nb_slices = (HAVE_THREADS &&
  842. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  843. s->avctx->thread_count : 1;
  844. if (s->encoding && s->avctx->slices)
  845. nb_slices = s->avctx->slices;
  846. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  847. s->mb_height = (s->height + 31) / 32 * 2;
  848. else
  849. s->mb_height = (s->height + 15) / 16;
  850. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  851. av_log(s->avctx, AV_LOG_ERROR,
  852. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  853. return -1;
  854. }
  855. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  856. int max_slices;
  857. if (s->mb_height)
  858. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  859. else
  860. max_slices = MAX_THREADS;
  861. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  862. " reducing to %d\n", nb_slices, max_slices);
  863. nb_slices = max_slices;
  864. }
  865. if ((s->width || s->height) &&
  866. av_image_check_size(s->width, s->height, 0, s->avctx))
  867. return -1;
  868. ff_dct_common_init(s);
  869. s->flags = s->avctx->flags;
  870. s->flags2 = s->avctx->flags2;
  871. /* set chroma shifts */
  872. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  873. /* convert fourcc to upper case */
  874. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  875. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  876. s->avctx->coded_frame = &s->current_picture.f;
  877. if (s->encoding) {
  878. if (s->msmpeg4_version) {
  879. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  880. 2 * 2 * (MAX_LEVEL + 1) *
  881. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  882. }
  883. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  884. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail)
  885. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail)
  886. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail)
  887. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
  888. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
  889. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
  890. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
  891. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
  892. if (s->avctx->noise_reduction) {
  893. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
  894. }
  895. }
  896. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  897. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  898. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  899. avcodec_get_frame_defaults(&s->picture[i].f);
  900. }
  901. memset(&s->next_picture, 0, sizeof(s->next_picture));
  902. memset(&s->last_picture, 0, sizeof(s->last_picture));
  903. memset(&s->current_picture, 0, sizeof(s->current_picture));
  904. avcodec_get_frame_defaults(&s->next_picture.f);
  905. avcodec_get_frame_defaults(&s->last_picture.f);
  906. avcodec_get_frame_defaults(&s->current_picture.f);
  907. if (init_context_frame(s))
  908. goto fail;
  909. s->parse_context.state = -1;
  910. s->context_initialized = 1;
  911. s->thread_context[0] = s;
  912. // if (s->width && s->height) {
  913. if (nb_slices > 1) {
  914. for (i = 1; i < nb_slices; i++) {
  915. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  916. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  917. }
  918. for (i = 0; i < nb_slices; i++) {
  919. if (init_duplicate_context(s->thread_context[i]) < 0)
  920. goto fail;
  921. s->thread_context[i]->start_mb_y =
  922. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  923. s->thread_context[i]->end_mb_y =
  924. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  925. }
  926. } else {
  927. if (init_duplicate_context(s) < 0)
  928. goto fail;
  929. s->start_mb_y = 0;
  930. s->end_mb_y = s->mb_height;
  931. }
  932. s->slice_context_count = nb_slices;
  933. // }
  934. return 0;
  935. fail:
  936. ff_MPV_common_end(s);
  937. return -1;
  938. }
  939. /**
  940. * Frees and resets MpegEncContext fields depending on the resolution.
  941. * Is used during resolution changes to avoid a full reinitialization of the
  942. * codec.
  943. */
  944. static int free_context_frame(MpegEncContext *s)
  945. {
  946. int i, j, k;
  947. av_freep(&s->mb_type);
  948. av_freep(&s->p_mv_table_base);
  949. av_freep(&s->b_forw_mv_table_base);
  950. av_freep(&s->b_back_mv_table_base);
  951. av_freep(&s->b_bidir_forw_mv_table_base);
  952. av_freep(&s->b_bidir_back_mv_table_base);
  953. av_freep(&s->b_direct_mv_table_base);
  954. s->p_mv_table = NULL;
  955. s->b_forw_mv_table = NULL;
  956. s->b_back_mv_table = NULL;
  957. s->b_bidir_forw_mv_table = NULL;
  958. s->b_bidir_back_mv_table = NULL;
  959. s->b_direct_mv_table = NULL;
  960. for (i = 0; i < 2; i++) {
  961. for (j = 0; j < 2; j++) {
  962. for (k = 0; k < 2; k++) {
  963. av_freep(&s->b_field_mv_table_base[i][j][k]);
  964. s->b_field_mv_table[i][j][k] = NULL;
  965. }
  966. av_freep(&s->b_field_select_table[i][j]);
  967. av_freep(&s->p_field_mv_table_base[i][j]);
  968. s->p_field_mv_table[i][j] = NULL;
  969. }
  970. av_freep(&s->p_field_select_table[i]);
  971. }
  972. av_freep(&s->dc_val_base);
  973. av_freep(&s->coded_block_base);
  974. av_freep(&s->mbintra_table);
  975. av_freep(&s->cbp_table);
  976. av_freep(&s->pred_dir_table);
  977. av_freep(&s->mbskip_table);
  978. av_freep(&s->er.error_status_table);
  979. av_freep(&s->er.er_temp_buffer);
  980. av_freep(&s->mb_index2xy);
  981. av_freep(&s->lambda_table);
  982. av_freep(&s->cplx_tab);
  983. av_freep(&s->bits_tab);
  984. s->linesize = s->uvlinesize = 0;
  985. return 0;
  986. }
  987. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  988. {
  989. int i, err = 0;
  990. if (s->slice_context_count > 1) {
  991. for (i = 0; i < s->slice_context_count; i++) {
  992. free_duplicate_context(s->thread_context[i]);
  993. }
  994. for (i = 1; i < s->slice_context_count; i++) {
  995. av_freep(&s->thread_context[i]);
  996. }
  997. } else
  998. free_duplicate_context(s);
  999. if ((err = free_context_frame(s)) < 0)
  1000. return err;
  1001. if (s->picture)
  1002. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1003. s->picture[i].needs_realloc = 1;
  1004. }
  1005. s->last_picture_ptr =
  1006. s->next_picture_ptr =
  1007. s->current_picture_ptr = NULL;
  1008. // init
  1009. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1010. s->mb_height = (s->height + 31) / 32 * 2;
  1011. else
  1012. s->mb_height = (s->height + 15) / 16;
  1013. if ((s->width || s->height) &&
  1014. av_image_check_size(s->width, s->height, 0, s->avctx))
  1015. return AVERROR_INVALIDDATA;
  1016. if ((err = init_context_frame(s)))
  1017. goto fail;
  1018. s->thread_context[0] = s;
  1019. if (s->width && s->height) {
  1020. int nb_slices = s->slice_context_count;
  1021. if (nb_slices > 1) {
  1022. for (i = 1; i < nb_slices; i++) {
  1023. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1024. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1025. }
  1026. for (i = 0; i < nb_slices; i++) {
  1027. if (init_duplicate_context(s->thread_context[i]) < 0)
  1028. goto fail;
  1029. s->thread_context[i]->start_mb_y =
  1030. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1031. s->thread_context[i]->end_mb_y =
  1032. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1033. }
  1034. } else {
  1035. if (init_duplicate_context(s) < 0)
  1036. goto fail;
  1037. s->start_mb_y = 0;
  1038. s->end_mb_y = s->mb_height;
  1039. }
  1040. s->slice_context_count = nb_slices;
  1041. }
  1042. return 0;
  1043. fail:
  1044. ff_MPV_common_end(s);
  1045. return err;
  1046. }
  1047. /* init common structure for both encoder and decoder */
  1048. void ff_MPV_common_end(MpegEncContext *s)
  1049. {
  1050. int i;
  1051. if (s->slice_context_count > 1) {
  1052. for (i = 0; i < s->slice_context_count; i++) {
  1053. free_duplicate_context(s->thread_context[i]);
  1054. }
  1055. for (i = 1; i < s->slice_context_count; i++) {
  1056. av_freep(&s->thread_context[i]);
  1057. }
  1058. s->slice_context_count = 1;
  1059. } else free_duplicate_context(s);
  1060. av_freep(&s->parse_context.buffer);
  1061. s->parse_context.buffer_size = 0;
  1062. av_freep(&s->bitstream_buffer);
  1063. s->allocated_bitstream_buffer_size = 0;
  1064. av_freep(&s->avctx->stats_out);
  1065. av_freep(&s->ac_stats);
  1066. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  1067. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  1068. s->q_chroma_intra_matrix= NULL;
  1069. s->q_chroma_intra_matrix16= NULL;
  1070. av_freep(&s->q_intra_matrix);
  1071. av_freep(&s->q_inter_matrix);
  1072. av_freep(&s->q_intra_matrix16);
  1073. av_freep(&s->q_inter_matrix16);
  1074. av_freep(&s->input_picture);
  1075. av_freep(&s->reordered_input_picture);
  1076. av_freep(&s->dct_offset);
  1077. if (s->picture) {
  1078. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1079. free_picture_tables(&s->picture[i]);
  1080. ff_mpeg_unref_picture(s, &s->picture[i]);
  1081. }
  1082. }
  1083. av_freep(&s->picture);
  1084. free_picture_tables(&s->last_picture);
  1085. ff_mpeg_unref_picture(s, &s->last_picture);
  1086. free_picture_tables(&s->current_picture);
  1087. ff_mpeg_unref_picture(s, &s->current_picture);
  1088. free_picture_tables(&s->next_picture);
  1089. ff_mpeg_unref_picture(s, &s->next_picture);
  1090. free_picture_tables(&s->new_picture);
  1091. ff_mpeg_unref_picture(s, &s->new_picture);
  1092. free_context_frame(s);
  1093. s->context_initialized = 0;
  1094. s->last_picture_ptr =
  1095. s->next_picture_ptr =
  1096. s->current_picture_ptr = NULL;
  1097. s->linesize = s->uvlinesize = 0;
  1098. }
  1099. void ff_init_rl(RLTable *rl,
  1100. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1101. {
  1102. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1103. uint8_t index_run[MAX_RUN + 1];
  1104. int last, run, level, start, end, i;
  1105. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1106. if (static_store && rl->max_level[0])
  1107. return;
  1108. /* compute max_level[], max_run[] and index_run[] */
  1109. for (last = 0; last < 2; last++) {
  1110. if (last == 0) {
  1111. start = 0;
  1112. end = rl->last;
  1113. } else {
  1114. start = rl->last;
  1115. end = rl->n;
  1116. }
  1117. memset(max_level, 0, MAX_RUN + 1);
  1118. memset(max_run, 0, MAX_LEVEL + 1);
  1119. memset(index_run, rl->n, MAX_RUN + 1);
  1120. for (i = start; i < end; i++) {
  1121. run = rl->table_run[i];
  1122. level = rl->table_level[i];
  1123. if (index_run[run] == rl->n)
  1124. index_run[run] = i;
  1125. if (level > max_level[run])
  1126. max_level[run] = level;
  1127. if (run > max_run[level])
  1128. max_run[level] = run;
  1129. }
  1130. if (static_store)
  1131. rl->max_level[last] = static_store[last];
  1132. else
  1133. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1134. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1135. if (static_store)
  1136. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1137. else
  1138. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1139. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1140. if (static_store)
  1141. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1142. else
  1143. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1144. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1145. }
  1146. }
  1147. void ff_init_vlc_rl(RLTable *rl)
  1148. {
  1149. int i, q;
  1150. for (q = 0; q < 32; q++) {
  1151. int qmul = q * 2;
  1152. int qadd = (q - 1) | 1;
  1153. if (q == 0) {
  1154. qmul = 1;
  1155. qadd = 0;
  1156. }
  1157. for (i = 0; i < rl->vlc.table_size; i++) {
  1158. int code = rl->vlc.table[i][0];
  1159. int len = rl->vlc.table[i][1];
  1160. int level, run;
  1161. if (len == 0) { // illegal code
  1162. run = 66;
  1163. level = MAX_LEVEL;
  1164. } else if (len < 0) { // more bits needed
  1165. run = 0;
  1166. level = code;
  1167. } else {
  1168. if (code == rl->n) { // esc
  1169. run = 66;
  1170. level = 0;
  1171. } else {
  1172. run = rl->table_run[code] + 1;
  1173. level = rl->table_level[code] * qmul + qadd;
  1174. if (code >= rl->last) run += 192;
  1175. }
  1176. }
  1177. rl->rl_vlc[q][i].len = len;
  1178. rl->rl_vlc[q][i].level = level;
  1179. rl->rl_vlc[q][i].run = run;
  1180. }
  1181. }
  1182. }
  1183. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1184. {
  1185. int i;
  1186. /* release non reference frames */
  1187. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1188. if (!s->picture[i].reference &&
  1189. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1190. ff_mpeg_unref_picture(s, &s->picture[i]);
  1191. }
  1192. }
  1193. }
  1194. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1195. {
  1196. if (pic == s->last_picture_ptr)
  1197. return 0;
  1198. if (pic->f.data[0] == NULL)
  1199. return 1;
  1200. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1201. return 1;
  1202. return 0;
  1203. }
  1204. static int find_unused_picture(MpegEncContext *s, int shared)
  1205. {
  1206. int i;
  1207. if (shared) {
  1208. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1209. if (s->picture[i].f.data[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1210. return i;
  1211. }
  1212. } else {
  1213. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1214. if (pic_is_unused(s, &s->picture[i]))
  1215. return i;
  1216. }
  1217. }
  1218. av_log(s->avctx, AV_LOG_FATAL,
  1219. "Internal error, picture buffer overflow\n");
  1220. /* We could return -1, but the codec would crash trying to draw into a
  1221. * non-existing frame anyway. This is safer than waiting for a random crash.
  1222. * Also the return of this is never useful, an encoder must only allocate
  1223. * as much as allowed in the specification. This has no relationship to how
  1224. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1225. * enough for such valid streams).
  1226. * Plus, a decoder has to check stream validity and remove frames if too
  1227. * many reference frames are around. Waiting for "OOM" is not correct at
  1228. * all. Similarly, missing reference frames have to be replaced by
  1229. * interpolated/MC frames, anything else is a bug in the codec ...
  1230. */
  1231. abort();
  1232. return -1;
  1233. }
  1234. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1235. {
  1236. int ret = find_unused_picture(s, shared);
  1237. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1238. if (s->picture[ret].needs_realloc) {
  1239. s->picture[ret].needs_realloc = 0;
  1240. free_picture_tables(&s->picture[ret]);
  1241. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1242. avcodec_get_frame_defaults(&s->picture[ret].f);
  1243. }
  1244. }
  1245. return ret;
  1246. }
  1247. static void update_noise_reduction(MpegEncContext *s)
  1248. {
  1249. int intra, i;
  1250. for (intra = 0; intra < 2; intra++) {
  1251. if (s->dct_count[intra] > (1 << 16)) {
  1252. for (i = 0; i < 64; i++) {
  1253. s->dct_error_sum[intra][i] >>= 1;
  1254. }
  1255. s->dct_count[intra] >>= 1;
  1256. }
  1257. for (i = 0; i < 64; i++) {
  1258. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1259. s->dct_count[intra] +
  1260. s->dct_error_sum[intra][i] / 2) /
  1261. (s->dct_error_sum[intra][i] + 1);
  1262. }
  1263. }
  1264. }
  1265. /**
  1266. * generic function for encode/decode called after coding/decoding
  1267. * the header and before a frame is coded/decoded.
  1268. */
  1269. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1270. {
  1271. int i, ret;
  1272. Picture *pic;
  1273. s->mb_skipped = 0;
  1274. if (!ff_thread_can_start_frame(avctx)) {
  1275. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1276. return -1;
  1277. }
  1278. /* mark & release old frames */
  1279. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1280. s->last_picture_ptr != s->next_picture_ptr &&
  1281. s->last_picture_ptr->f.data[0]) {
  1282. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1283. }
  1284. /* release forgotten pictures */
  1285. /* if (mpeg124/h263) */
  1286. if (!s->encoding) {
  1287. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1288. if (&s->picture[i] != s->last_picture_ptr &&
  1289. &s->picture[i] != s->next_picture_ptr &&
  1290. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1291. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1292. av_log(avctx, AV_LOG_ERROR,
  1293. "releasing zombie picture\n");
  1294. ff_mpeg_unref_picture(s, &s->picture[i]);
  1295. }
  1296. }
  1297. }
  1298. if (!s->encoding) {
  1299. ff_release_unused_pictures(s, 1);
  1300. if (s->current_picture_ptr &&
  1301. s->current_picture_ptr->f.data[0] == NULL) {
  1302. // we already have a unused image
  1303. // (maybe it was set before reading the header)
  1304. pic = s->current_picture_ptr;
  1305. } else {
  1306. i = ff_find_unused_picture(s, 0);
  1307. if (i < 0) {
  1308. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1309. return i;
  1310. }
  1311. pic = &s->picture[i];
  1312. }
  1313. pic->reference = 0;
  1314. if (!s->droppable) {
  1315. if (s->pict_type != AV_PICTURE_TYPE_B)
  1316. pic->reference = 3;
  1317. }
  1318. pic->f.coded_picture_number = s->coded_picture_number++;
  1319. if (ff_alloc_picture(s, pic, 0) < 0)
  1320. return -1;
  1321. s->current_picture_ptr = pic;
  1322. // FIXME use only the vars from current_pic
  1323. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1324. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1325. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1326. if (s->picture_structure != PICT_FRAME)
  1327. s->current_picture_ptr->f.top_field_first =
  1328. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1329. }
  1330. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1331. !s->progressive_sequence;
  1332. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1333. }
  1334. s->current_picture_ptr->f.pict_type = s->pict_type;
  1335. // if (s->flags && CODEC_FLAG_QSCALE)
  1336. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1337. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1338. ff_mpeg_unref_picture(s, &s->current_picture);
  1339. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1340. s->current_picture_ptr)) < 0)
  1341. return ret;
  1342. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1343. s->last_picture_ptr = s->next_picture_ptr;
  1344. if (!s->droppable)
  1345. s->next_picture_ptr = s->current_picture_ptr;
  1346. }
  1347. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1348. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1349. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1350. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1351. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1352. s->pict_type, s->droppable);
  1353. if ((s->last_picture_ptr == NULL ||
  1354. s->last_picture_ptr->f.data[0] == NULL) &&
  1355. (s->pict_type != AV_PICTURE_TYPE_I ||
  1356. s->picture_structure != PICT_FRAME)) {
  1357. int h_chroma_shift, v_chroma_shift;
  1358. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1359. &h_chroma_shift, &v_chroma_shift);
  1360. if (s->pict_type != AV_PICTURE_TYPE_I)
  1361. av_log(avctx, AV_LOG_ERROR,
  1362. "warning: first frame is no keyframe\n");
  1363. else if (s->picture_structure != PICT_FRAME)
  1364. av_log(avctx, AV_LOG_INFO,
  1365. "allocate dummy last picture for field based first keyframe\n");
  1366. /* Allocate a dummy frame */
  1367. i = ff_find_unused_picture(s, 0);
  1368. if (i < 0) {
  1369. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1370. return i;
  1371. }
  1372. s->last_picture_ptr = &s->picture[i];
  1373. s->last_picture_ptr->f.key_frame = 0;
  1374. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1375. s->last_picture_ptr = NULL;
  1376. return -1;
  1377. }
  1378. memset(s->last_picture_ptr->f.data[0], 0x80,
  1379. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1380. memset(s->last_picture_ptr->f.data[1], 0x80,
  1381. (avctx->height >> v_chroma_shift) *
  1382. s->last_picture_ptr->f.linesize[1]);
  1383. memset(s->last_picture_ptr->f.data[2], 0x80,
  1384. (avctx->height >> v_chroma_shift) *
  1385. s->last_picture_ptr->f.linesize[2]);
  1386. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1387. for(i=0; i<avctx->height; i++)
  1388. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1389. }
  1390. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1391. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1392. }
  1393. if ((s->next_picture_ptr == NULL ||
  1394. s->next_picture_ptr->f.data[0] == NULL) &&
  1395. s->pict_type == AV_PICTURE_TYPE_B) {
  1396. /* Allocate a dummy frame */
  1397. i = ff_find_unused_picture(s, 0);
  1398. if (i < 0) {
  1399. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1400. return i;
  1401. }
  1402. s->next_picture_ptr = &s->picture[i];
  1403. s->next_picture_ptr->f.key_frame = 0;
  1404. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1405. s->next_picture_ptr = NULL;
  1406. return -1;
  1407. }
  1408. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1409. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1410. }
  1411. #if 0 // BUFREF-FIXME
  1412. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1413. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1414. #endif
  1415. if (s->last_picture_ptr) {
  1416. ff_mpeg_unref_picture(s, &s->last_picture);
  1417. if (s->last_picture_ptr->f.data[0] &&
  1418. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1419. s->last_picture_ptr)) < 0)
  1420. return ret;
  1421. }
  1422. if (s->next_picture_ptr) {
  1423. ff_mpeg_unref_picture(s, &s->next_picture);
  1424. if (s->next_picture_ptr->f.data[0] &&
  1425. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1426. s->next_picture_ptr)) < 0)
  1427. return ret;
  1428. }
  1429. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1430. s->last_picture_ptr->f.data[0]));
  1431. if (s->picture_structure!= PICT_FRAME) {
  1432. int i;
  1433. for (i = 0; i < 4; i++) {
  1434. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1435. s->current_picture.f.data[i] +=
  1436. s->current_picture.f.linesize[i];
  1437. }
  1438. s->current_picture.f.linesize[i] *= 2;
  1439. s->last_picture.f.linesize[i] *= 2;
  1440. s->next_picture.f.linesize[i] *= 2;
  1441. }
  1442. }
  1443. s->err_recognition = avctx->err_recognition;
  1444. /* set dequantizer, we can't do it during init as
  1445. * it might change for mpeg4 and we can't do it in the header
  1446. * decode as init is not called for mpeg4 there yet */
  1447. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1448. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1449. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1450. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1451. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1452. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1453. } else {
  1454. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1455. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1456. }
  1457. if (s->dct_error_sum) {
  1458. assert(s->avctx->noise_reduction && s->encoding);
  1459. update_noise_reduction(s);
  1460. }
  1461. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1462. return ff_xvmc_field_start(s, avctx);
  1463. return 0;
  1464. }
  1465. /* generic function for encode/decode called after a
  1466. * frame has been coded/decoded. */
  1467. void ff_MPV_frame_end(MpegEncContext *s)
  1468. {
  1469. int i;
  1470. /* redraw edges for the frame if decoding didn't complete */
  1471. // just to make sure that all data is rendered.
  1472. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1473. ff_xvmc_field_end(s);
  1474. } else if ((s->er.error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1475. !s->avctx->hwaccel &&
  1476. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1477. s->unrestricted_mv &&
  1478. s->current_picture.reference &&
  1479. !s->intra_only &&
  1480. !(s->flags & CODEC_FLAG_EMU_EDGE) &&
  1481. !s->avctx->lowres
  1482. ) {
  1483. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1484. int hshift = desc->log2_chroma_w;
  1485. int vshift = desc->log2_chroma_h;
  1486. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1487. s->h_edge_pos, s->v_edge_pos,
  1488. EDGE_WIDTH, EDGE_WIDTH,
  1489. EDGE_TOP | EDGE_BOTTOM);
  1490. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1491. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1492. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1493. EDGE_TOP | EDGE_BOTTOM);
  1494. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1495. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1496. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1497. EDGE_TOP | EDGE_BOTTOM);
  1498. }
  1499. emms_c();
  1500. s->last_pict_type = s->pict_type;
  1501. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1502. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1503. s->last_non_b_pict_type = s->pict_type;
  1504. }
  1505. #if 0
  1506. /* copy back current_picture variables */
  1507. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1508. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1509. s->picture[i] = s->current_picture;
  1510. break;
  1511. }
  1512. }
  1513. assert(i < MAX_PICTURE_COUNT);
  1514. #endif
  1515. if (s->encoding) {
  1516. /* release non-reference frames */
  1517. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1518. if (!s->picture[i].reference)
  1519. ff_mpeg_unref_picture(s, &s->picture[i]);
  1520. }
  1521. }
  1522. // clear copies, to avoid confusion
  1523. #if 0
  1524. memset(&s->last_picture, 0, sizeof(Picture));
  1525. memset(&s->next_picture, 0, sizeof(Picture));
  1526. memset(&s->current_picture, 0, sizeof(Picture));
  1527. #endif
  1528. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1529. if (s->current_picture.reference)
  1530. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1531. }
  1532. /**
  1533. * Draw a line from (ex, ey) -> (sx, sy).
  1534. * @param w width of the image
  1535. * @param h height of the image
  1536. * @param stride stride/linesize of the image
  1537. * @param color color of the arrow
  1538. */
  1539. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1540. int w, int h, int stride, int color)
  1541. {
  1542. int x, y, fr, f;
  1543. sx = av_clip(sx, 0, w - 1);
  1544. sy = av_clip(sy, 0, h - 1);
  1545. ex = av_clip(ex, 0, w - 1);
  1546. ey = av_clip(ey, 0, h - 1);
  1547. buf[sy * stride + sx] += color;
  1548. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1549. if (sx > ex) {
  1550. FFSWAP(int, sx, ex);
  1551. FFSWAP(int, sy, ey);
  1552. }
  1553. buf += sx + sy * stride;
  1554. ex -= sx;
  1555. f = ((ey - sy) << 16) / ex;
  1556. for (x = 0; x <= ex; x++) {
  1557. y = (x * f) >> 16;
  1558. fr = (x * f) & 0xFFFF;
  1559. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1560. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1561. }
  1562. } else {
  1563. if (sy > ey) {
  1564. FFSWAP(int, sx, ex);
  1565. FFSWAP(int, sy, ey);
  1566. }
  1567. buf += sx + sy * stride;
  1568. ey -= sy;
  1569. if (ey)
  1570. f = ((ex - sx) << 16) / ey;
  1571. else
  1572. f = 0;
  1573. for(y= 0; y <= ey; y++){
  1574. x = (y*f) >> 16;
  1575. fr = (y*f) & 0xFFFF;
  1576. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1577. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1578. }
  1579. }
  1580. }
  1581. /**
  1582. * Draw an arrow from (ex, ey) -> (sx, sy).
  1583. * @param w width of the image
  1584. * @param h height of the image
  1585. * @param stride stride/linesize of the image
  1586. * @param color color of the arrow
  1587. */
  1588. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1589. int ey, int w, int h, int stride, int color)
  1590. {
  1591. int dx,dy;
  1592. sx = av_clip(sx, -100, w + 100);
  1593. sy = av_clip(sy, -100, h + 100);
  1594. ex = av_clip(ex, -100, w + 100);
  1595. ey = av_clip(ey, -100, h + 100);
  1596. dx = ex - sx;
  1597. dy = ey - sy;
  1598. if (dx * dx + dy * dy > 3 * 3) {
  1599. int rx = dx + dy;
  1600. int ry = -dx + dy;
  1601. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1602. // FIXME subpixel accuracy
  1603. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1604. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1605. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1606. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1607. }
  1608. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1609. }
  1610. /**
  1611. * Print debugging info for the given picture.
  1612. */
  1613. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
  1614. int *low_delay,
  1615. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1616. {
  1617. if (avctx->hwaccel || !p || !p->mb_type
  1618. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1619. return;
  1620. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1621. int x,y;
  1622. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1623. av_get_picture_type_char(pict->pict_type));
  1624. for (y = 0; y < mb_height; y++) {
  1625. for (x = 0; x < mb_width; x++) {
  1626. if (avctx->debug & FF_DEBUG_SKIP) {
  1627. int count = mbskip_table[x + y * mb_stride];
  1628. if (count > 9)
  1629. count = 9;
  1630. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1631. }
  1632. if (avctx->debug & FF_DEBUG_QP) {
  1633. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1634. p->qscale_table[x + y * mb_stride]);
  1635. }
  1636. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1637. int mb_type = p->mb_type[x + y * mb_stride];
  1638. // Type & MV direction
  1639. if (IS_PCM(mb_type))
  1640. av_log(avctx, AV_LOG_DEBUG, "P");
  1641. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1642. av_log(avctx, AV_LOG_DEBUG, "A");
  1643. else if (IS_INTRA4x4(mb_type))
  1644. av_log(avctx, AV_LOG_DEBUG, "i");
  1645. else if (IS_INTRA16x16(mb_type))
  1646. av_log(avctx, AV_LOG_DEBUG, "I");
  1647. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1648. av_log(avctx, AV_LOG_DEBUG, "d");
  1649. else if (IS_DIRECT(mb_type))
  1650. av_log(avctx, AV_LOG_DEBUG, "D");
  1651. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1652. av_log(avctx, AV_LOG_DEBUG, "g");
  1653. else if (IS_GMC(mb_type))
  1654. av_log(avctx, AV_LOG_DEBUG, "G");
  1655. else if (IS_SKIP(mb_type))
  1656. av_log(avctx, AV_LOG_DEBUG, "S");
  1657. else if (!USES_LIST(mb_type, 1))
  1658. av_log(avctx, AV_LOG_DEBUG, ">");
  1659. else if (!USES_LIST(mb_type, 0))
  1660. av_log(avctx, AV_LOG_DEBUG, "<");
  1661. else {
  1662. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1663. av_log(avctx, AV_LOG_DEBUG, "X");
  1664. }
  1665. // segmentation
  1666. if (IS_8X8(mb_type))
  1667. av_log(avctx, AV_LOG_DEBUG, "+");
  1668. else if (IS_16X8(mb_type))
  1669. av_log(avctx, AV_LOG_DEBUG, "-");
  1670. else if (IS_8X16(mb_type))
  1671. av_log(avctx, AV_LOG_DEBUG, "|");
  1672. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1673. av_log(avctx, AV_LOG_DEBUG, " ");
  1674. else
  1675. av_log(avctx, AV_LOG_DEBUG, "?");
  1676. if (IS_INTERLACED(mb_type))
  1677. av_log(avctx, AV_LOG_DEBUG, "=");
  1678. else
  1679. av_log(avctx, AV_LOG_DEBUG, " ");
  1680. }
  1681. }
  1682. av_log(avctx, AV_LOG_DEBUG, "\n");
  1683. }
  1684. }
  1685. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1686. (avctx->debug_mv)) {
  1687. const int shift = 1 + quarter_sample;
  1688. int mb_y;
  1689. uint8_t *ptr;
  1690. int i;
  1691. int h_chroma_shift, v_chroma_shift, block_height;
  1692. const int width = avctx->width;
  1693. const int height = avctx->height;
  1694. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1695. const int mv_stride = (mb_width << mv_sample_log2) +
  1696. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1697. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1698. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1699. av_frame_make_writable(pict);
  1700. pict->opaque = NULL;
  1701. ptr = pict->data[0];
  1702. block_height = 16 >> v_chroma_shift;
  1703. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1704. int mb_x;
  1705. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1706. const int mb_index = mb_x + mb_y * mb_stride;
  1707. if ((avctx->debug_mv) && p->motion_val[0]) {
  1708. int type;
  1709. for (type = 0; type < 3; type++) {
  1710. int direction = 0;
  1711. switch (type) {
  1712. case 0:
  1713. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1714. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1715. continue;
  1716. direction = 0;
  1717. break;
  1718. case 1:
  1719. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1720. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1721. continue;
  1722. direction = 0;
  1723. break;
  1724. case 2:
  1725. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1726. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1727. continue;
  1728. direction = 1;
  1729. break;
  1730. }
  1731. if (!USES_LIST(p->mb_type[mb_index], direction))
  1732. continue;
  1733. if (IS_8X8(p->mb_type[mb_index])) {
  1734. int i;
  1735. for (i = 0; i < 4; i++) {
  1736. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1737. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1738. int xy = (mb_x * 2 + (i & 1) +
  1739. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1740. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1741. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1742. draw_arrow(ptr, sx, sy, mx, my, width,
  1743. height, pict->linesize[0], 100);
  1744. }
  1745. } else if (IS_16X8(p->mb_type[mb_index])) {
  1746. int i;
  1747. for (i = 0; i < 2; i++) {
  1748. int sx = mb_x * 16 + 8;
  1749. int sy = mb_y * 16 + 4 + 8 * i;
  1750. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1751. int mx = (p->motion_val[direction][xy][0] >> shift);
  1752. int my = (p->motion_val[direction][xy][1] >> shift);
  1753. if (IS_INTERLACED(p->mb_type[mb_index]))
  1754. my *= 2;
  1755. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1756. height, pict->linesize[0], 100);
  1757. }
  1758. } else if (IS_8X16(p->mb_type[mb_index])) {
  1759. int i;
  1760. for (i = 0; i < 2; i++) {
  1761. int sx = mb_x * 16 + 4 + 8 * i;
  1762. int sy = mb_y * 16 + 8;
  1763. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1764. int mx = p->motion_val[direction][xy][0] >> shift;
  1765. int my = p->motion_val[direction][xy][1] >> shift;
  1766. if (IS_INTERLACED(p->mb_type[mb_index]))
  1767. my *= 2;
  1768. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1769. height, pict->linesize[0], 100);
  1770. }
  1771. } else {
  1772. int sx= mb_x * 16 + 8;
  1773. int sy= mb_y * 16 + 8;
  1774. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1775. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1776. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1777. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1778. }
  1779. }
  1780. }
  1781. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1782. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1783. 0x0101010101010101ULL;
  1784. int y;
  1785. for (y = 0; y < block_height; y++) {
  1786. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1787. (block_height * mb_y + y) *
  1788. pict->linesize[1]) = c;
  1789. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1790. (block_height * mb_y + y) *
  1791. pict->linesize[2]) = c;
  1792. }
  1793. }
  1794. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1795. p->motion_val[0]) {
  1796. int mb_type = p->mb_type[mb_index];
  1797. uint64_t u,v;
  1798. int y;
  1799. #define COLOR(theta, r) \
  1800. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1801. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1802. u = v = 128;
  1803. if (IS_PCM(mb_type)) {
  1804. COLOR(120, 48)
  1805. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1806. IS_INTRA16x16(mb_type)) {
  1807. COLOR(30, 48)
  1808. } else if (IS_INTRA4x4(mb_type)) {
  1809. COLOR(90, 48)
  1810. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1811. // COLOR(120, 48)
  1812. } else if (IS_DIRECT(mb_type)) {
  1813. COLOR(150, 48)
  1814. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1815. COLOR(170, 48)
  1816. } else if (IS_GMC(mb_type)) {
  1817. COLOR(190, 48)
  1818. } else if (IS_SKIP(mb_type)) {
  1819. // COLOR(180, 48)
  1820. } else if (!USES_LIST(mb_type, 1)) {
  1821. COLOR(240, 48)
  1822. } else if (!USES_LIST(mb_type, 0)) {
  1823. COLOR(0, 48)
  1824. } else {
  1825. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1826. COLOR(300,48)
  1827. }
  1828. u *= 0x0101010101010101ULL;
  1829. v *= 0x0101010101010101ULL;
  1830. for (y = 0; y < block_height; y++) {
  1831. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1832. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1833. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1834. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1835. }
  1836. // segmentation
  1837. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1838. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1839. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1840. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1841. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1842. }
  1843. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1844. for (y = 0; y < 16; y++)
  1845. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1846. pict->linesize[0]] ^= 0x80;
  1847. }
  1848. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1849. int dm = 1 << (mv_sample_log2 - 2);
  1850. for (i = 0; i < 4; i++) {
  1851. int sx = mb_x * 16 + 8 * (i & 1);
  1852. int sy = mb_y * 16 + 8 * (i >> 1);
  1853. int xy = (mb_x * 2 + (i & 1) +
  1854. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1855. // FIXME bidir
  1856. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1857. if (mv[0] != mv[dm] ||
  1858. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1859. for (y = 0; y < 8; y++)
  1860. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1861. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1862. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1863. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1864. }
  1865. }
  1866. if (IS_INTERLACED(mb_type) &&
  1867. avctx->codec->id == AV_CODEC_ID_H264) {
  1868. // hmm
  1869. }
  1870. }
  1871. mbskip_table[mb_index] = 0;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1877. {
  1878. ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
  1879. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1880. }
  1881. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1882. {
  1883. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1884. int offset = 2*s->mb_stride + 1;
  1885. if(!ref)
  1886. return AVERROR(ENOMEM);
  1887. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1888. ref->size -= offset;
  1889. ref->data += offset;
  1890. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1891. }
  1892. static inline int hpel_motion_lowres(MpegEncContext *s,
  1893. uint8_t *dest, uint8_t *src,
  1894. int field_based, int field_select,
  1895. int src_x, int src_y,
  1896. int width, int height, int stride,
  1897. int h_edge_pos, int v_edge_pos,
  1898. int w, int h, h264_chroma_mc_func *pix_op,
  1899. int motion_x, int motion_y)
  1900. {
  1901. const int lowres = s->avctx->lowres;
  1902. const int op_index = FFMIN(lowres, 2);
  1903. const int s_mask = (2 << lowres) - 1;
  1904. int emu = 0;
  1905. int sx, sy;
  1906. if (s->quarter_sample) {
  1907. motion_x /= 2;
  1908. motion_y /= 2;
  1909. }
  1910. sx = motion_x & s_mask;
  1911. sy = motion_y & s_mask;
  1912. src_x += motion_x >> lowres + 1;
  1913. src_y += motion_y >> lowres + 1;
  1914. src += src_y * stride + src_x;
  1915. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1916. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1917. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
  1918. (h + 1) << field_based, src_x,
  1919. src_y << field_based,
  1920. h_edge_pos,
  1921. v_edge_pos);
  1922. src = s->edge_emu_buffer;
  1923. emu = 1;
  1924. }
  1925. sx = (sx << 2) >> lowres;
  1926. sy = (sy << 2) >> lowres;
  1927. if (field_select)
  1928. src += s->linesize;
  1929. pix_op[op_index](dest, src, stride, h, sx, sy);
  1930. return emu;
  1931. }
  1932. /* apply one mpeg motion vector to the three components */
  1933. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1934. uint8_t *dest_y,
  1935. uint8_t *dest_cb,
  1936. uint8_t *dest_cr,
  1937. int field_based,
  1938. int bottom_field,
  1939. int field_select,
  1940. uint8_t **ref_picture,
  1941. h264_chroma_mc_func *pix_op,
  1942. int motion_x, int motion_y,
  1943. int h, int mb_y)
  1944. {
  1945. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1946. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
  1947. uvsx, uvsy;
  1948. const int lowres = s->avctx->lowres;
  1949. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 2);
  1950. const int block_s = 8>>lowres;
  1951. const int s_mask = (2 << lowres) - 1;
  1952. const int h_edge_pos = s->h_edge_pos >> lowres;
  1953. const int v_edge_pos = s->v_edge_pos >> lowres;
  1954. linesize = s->current_picture.f.linesize[0] << field_based;
  1955. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1956. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1957. if (s->quarter_sample) {
  1958. motion_x /= 2;
  1959. motion_y /= 2;
  1960. }
  1961. if(field_based){
  1962. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1963. }
  1964. sx = motion_x & s_mask;
  1965. sy = motion_y & s_mask;
  1966. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1967. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1968. if (s->out_format == FMT_H263) {
  1969. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1970. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1971. uvsrc_x = src_x >> 1;
  1972. uvsrc_y = src_y >> 1;
  1973. } else if (s->out_format == FMT_H261) {
  1974. // even chroma mv's are full pel in H261
  1975. mx = motion_x / 4;
  1976. my = motion_y / 4;
  1977. uvsx = (2 * mx) & s_mask;
  1978. uvsy = (2 * my) & s_mask;
  1979. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1980. uvsrc_y = mb_y * block_s + (my >> lowres);
  1981. } else {
  1982. if(s->chroma_y_shift){
  1983. mx = motion_x / 2;
  1984. my = motion_y / 2;
  1985. uvsx = mx & s_mask;
  1986. uvsy = my & s_mask;
  1987. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1988. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1989. } else {
  1990. if(s->chroma_x_shift){
  1991. //Chroma422
  1992. mx = motion_x / 2;
  1993. uvsx = mx & s_mask;
  1994. uvsy = motion_y & s_mask;
  1995. uvsrc_y = src_y;
  1996. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1997. } else {
  1998. //Chroma444
  1999. uvsx = motion_x & s_mask;
  2000. uvsy = motion_y & s_mask;
  2001. uvsrc_x = src_x;
  2002. uvsrc_y = src_y;
  2003. }
  2004. }
  2005. }
  2006. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2007. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2008. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2009. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
  2010. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2011. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2012. linesize >> field_based, 17, 17 + field_based,
  2013. src_x, src_y << field_based, h_edge_pos,
  2014. v_edge_pos);
  2015. ptr_y = s->edge_emu_buffer;
  2016. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2017. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  2018. s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
  2019. 9 + field_based,
  2020. uvsrc_x, uvsrc_y << field_based,
  2021. h_edge_pos >> 1, v_edge_pos >> 1);
  2022. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
  2023. 9 + field_based,
  2024. uvsrc_x, uvsrc_y << field_based,
  2025. h_edge_pos >> 1, v_edge_pos >> 1);
  2026. ptr_cb = uvbuf;
  2027. ptr_cr = uvbuf + 16;
  2028. }
  2029. }
  2030. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  2031. if (bottom_field) {
  2032. dest_y += s->linesize;
  2033. dest_cb += s->uvlinesize;
  2034. dest_cr += s->uvlinesize;
  2035. }
  2036. if (field_select) {
  2037. ptr_y += s->linesize;
  2038. ptr_cb += s->uvlinesize;
  2039. ptr_cr += s->uvlinesize;
  2040. }
  2041. sx = (sx << 2) >> lowres;
  2042. sy = (sy << 2) >> lowres;
  2043. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2044. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2045. uvsx = (uvsx << 2) >> lowres;
  2046. uvsy = (uvsy << 2) >> lowres;
  2047. if (h >> s->chroma_y_shift) {
  2048. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2049. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2050. }
  2051. }
  2052. // FIXME h261 lowres loop filter
  2053. }
  2054. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2055. uint8_t *dest_cb, uint8_t *dest_cr,
  2056. uint8_t **ref_picture,
  2057. h264_chroma_mc_func * pix_op,
  2058. int mx, int my)
  2059. {
  2060. const int lowres = s->avctx->lowres;
  2061. const int op_index = FFMIN(lowres, 2);
  2062. const int block_s = 8 >> lowres;
  2063. const int s_mask = (2 << lowres) - 1;
  2064. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2065. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2066. int emu = 0, src_x, src_y, offset, sx, sy;
  2067. uint8_t *ptr;
  2068. if (s->quarter_sample) {
  2069. mx /= 2;
  2070. my /= 2;
  2071. }
  2072. /* In case of 8X8, we construct a single chroma motion vector
  2073. with a special rounding */
  2074. mx = ff_h263_round_chroma(mx);
  2075. my = ff_h263_round_chroma(my);
  2076. sx = mx & s_mask;
  2077. sy = my & s_mask;
  2078. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2079. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2080. offset = src_y * s->uvlinesize + src_x;
  2081. ptr = ref_picture[1] + offset;
  2082. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  2083. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2084. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2085. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  2086. 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  2087. ptr = s->edge_emu_buffer;
  2088. emu = 1;
  2089. }
  2090. }
  2091. sx = (sx << 2) >> lowres;
  2092. sy = (sy << 2) >> lowres;
  2093. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2094. ptr = ref_picture[2] + offset;
  2095. if (emu) {
  2096. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
  2097. src_x, src_y, h_edge_pos, v_edge_pos);
  2098. ptr = s->edge_emu_buffer;
  2099. }
  2100. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2101. }
  2102. /**
  2103. * motion compensation of a single macroblock
  2104. * @param s context
  2105. * @param dest_y luma destination pointer
  2106. * @param dest_cb chroma cb/u destination pointer
  2107. * @param dest_cr chroma cr/v destination pointer
  2108. * @param dir direction (0->forward, 1->backward)
  2109. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2110. * @param pix_op halfpel motion compensation function (average or put normally)
  2111. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2112. */
  2113. static inline void MPV_motion_lowres(MpegEncContext *s,
  2114. uint8_t *dest_y, uint8_t *dest_cb,
  2115. uint8_t *dest_cr,
  2116. int dir, uint8_t **ref_picture,
  2117. h264_chroma_mc_func *pix_op)
  2118. {
  2119. int mx, my;
  2120. int mb_x, mb_y, i;
  2121. const int lowres = s->avctx->lowres;
  2122. const int block_s = 8 >>lowres;
  2123. mb_x = s->mb_x;
  2124. mb_y = s->mb_y;
  2125. switch (s->mv_type) {
  2126. case MV_TYPE_16X16:
  2127. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2128. 0, 0, 0,
  2129. ref_picture, pix_op,
  2130. s->mv[dir][0][0], s->mv[dir][0][1],
  2131. 2 * block_s, mb_y);
  2132. break;
  2133. case MV_TYPE_8X8:
  2134. mx = 0;
  2135. my = 0;
  2136. for (i = 0; i < 4; i++) {
  2137. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2138. s->linesize) * block_s,
  2139. ref_picture[0], 0, 0,
  2140. (2 * mb_x + (i & 1)) * block_s,
  2141. (2 * mb_y + (i >> 1)) * block_s,
  2142. s->width, s->height, s->linesize,
  2143. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2144. block_s, block_s, pix_op,
  2145. s->mv[dir][i][0], s->mv[dir][i][1]);
  2146. mx += s->mv[dir][i][0];
  2147. my += s->mv[dir][i][1];
  2148. }
  2149. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2150. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2151. pix_op, mx, my);
  2152. break;
  2153. case MV_TYPE_FIELD:
  2154. if (s->picture_structure == PICT_FRAME) {
  2155. /* top field */
  2156. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2157. 1, 0, s->field_select[dir][0],
  2158. ref_picture, pix_op,
  2159. s->mv[dir][0][0], s->mv[dir][0][1],
  2160. block_s, mb_y);
  2161. /* bottom field */
  2162. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2163. 1, 1, s->field_select[dir][1],
  2164. ref_picture, pix_op,
  2165. s->mv[dir][1][0], s->mv[dir][1][1],
  2166. block_s, mb_y);
  2167. } else {
  2168. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2169. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2170. ref_picture = s->current_picture_ptr->f.data;
  2171. }
  2172. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2173. 0, 0, s->field_select[dir][0],
  2174. ref_picture, pix_op,
  2175. s->mv[dir][0][0],
  2176. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2177. }
  2178. break;
  2179. case MV_TYPE_16X8:
  2180. for (i = 0; i < 2; i++) {
  2181. uint8_t **ref2picture;
  2182. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2183. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2184. ref2picture = ref_picture;
  2185. } else {
  2186. ref2picture = s->current_picture_ptr->f.data;
  2187. }
  2188. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2189. 0, 0, s->field_select[dir][i],
  2190. ref2picture, pix_op,
  2191. s->mv[dir][i][0], s->mv[dir][i][1] +
  2192. 2 * block_s * i, block_s, mb_y >> 1);
  2193. dest_y += 2 * block_s * s->linesize;
  2194. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2195. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2196. }
  2197. break;
  2198. case MV_TYPE_DMV:
  2199. if (s->picture_structure == PICT_FRAME) {
  2200. for (i = 0; i < 2; i++) {
  2201. int j;
  2202. for (j = 0; j < 2; j++) {
  2203. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2204. 1, j, j ^ i,
  2205. ref_picture, pix_op,
  2206. s->mv[dir][2 * i + j][0],
  2207. s->mv[dir][2 * i + j][1],
  2208. block_s, mb_y);
  2209. }
  2210. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2211. }
  2212. } else {
  2213. for (i = 0; i < 2; i++) {
  2214. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2215. 0, 0, s->picture_structure != i + 1,
  2216. ref_picture, pix_op,
  2217. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2218. 2 * block_s, mb_y >> 1);
  2219. // after put we make avg of the same block
  2220. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2221. // opposite parity is always in the same
  2222. // frame if this is second field
  2223. if (!s->first_field) {
  2224. ref_picture = s->current_picture_ptr->f.data;
  2225. }
  2226. }
  2227. }
  2228. break;
  2229. default:
  2230. av_assert2(0);
  2231. }
  2232. }
  2233. /**
  2234. * find the lowest MB row referenced in the MVs
  2235. */
  2236. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2237. {
  2238. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2239. int my, off, i, mvs;
  2240. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2241. goto unhandled;
  2242. switch (s->mv_type) {
  2243. case MV_TYPE_16X16:
  2244. mvs = 1;
  2245. break;
  2246. case MV_TYPE_16X8:
  2247. mvs = 2;
  2248. break;
  2249. case MV_TYPE_8X8:
  2250. mvs = 4;
  2251. break;
  2252. default:
  2253. goto unhandled;
  2254. }
  2255. for (i = 0; i < mvs; i++) {
  2256. my = s->mv[dir][i][1]<<qpel_shift;
  2257. my_max = FFMAX(my_max, my);
  2258. my_min = FFMIN(my_min, my);
  2259. }
  2260. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2261. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2262. unhandled:
  2263. return s->mb_height-1;
  2264. }
  2265. /* put block[] to dest[] */
  2266. static inline void put_dct(MpegEncContext *s,
  2267. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2268. {
  2269. s->dct_unquantize_intra(s, block, i, qscale);
  2270. s->dsp.idct_put (dest, line_size, block);
  2271. }
  2272. /* add block[] to dest[] */
  2273. static inline void add_dct(MpegEncContext *s,
  2274. int16_t *block, int i, uint8_t *dest, int line_size)
  2275. {
  2276. if (s->block_last_index[i] >= 0) {
  2277. s->dsp.idct_add (dest, line_size, block);
  2278. }
  2279. }
  2280. static inline void add_dequant_dct(MpegEncContext *s,
  2281. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2282. {
  2283. if (s->block_last_index[i] >= 0) {
  2284. s->dct_unquantize_inter(s, block, i, qscale);
  2285. s->dsp.idct_add (dest, line_size, block);
  2286. }
  2287. }
  2288. /**
  2289. * Clean dc, ac, coded_block for the current non-intra MB.
  2290. */
  2291. void ff_clean_intra_table_entries(MpegEncContext *s)
  2292. {
  2293. int wrap = s->b8_stride;
  2294. int xy = s->block_index[0];
  2295. s->dc_val[0][xy ] =
  2296. s->dc_val[0][xy + 1 ] =
  2297. s->dc_val[0][xy + wrap] =
  2298. s->dc_val[0][xy + 1 + wrap] = 1024;
  2299. /* ac pred */
  2300. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2301. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2302. if (s->msmpeg4_version>=3) {
  2303. s->coded_block[xy ] =
  2304. s->coded_block[xy + 1 ] =
  2305. s->coded_block[xy + wrap] =
  2306. s->coded_block[xy + 1 + wrap] = 0;
  2307. }
  2308. /* chroma */
  2309. wrap = s->mb_stride;
  2310. xy = s->mb_x + s->mb_y * wrap;
  2311. s->dc_val[1][xy] =
  2312. s->dc_val[2][xy] = 1024;
  2313. /* ac pred */
  2314. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2315. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2316. s->mbintra_table[xy]= 0;
  2317. }
  2318. /* generic function called after a macroblock has been parsed by the
  2319. decoder or after it has been encoded by the encoder.
  2320. Important variables used:
  2321. s->mb_intra : true if intra macroblock
  2322. s->mv_dir : motion vector direction
  2323. s->mv_type : motion vector type
  2324. s->mv : motion vector
  2325. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2326. */
  2327. static av_always_inline
  2328. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2329. int lowres_flag, int is_mpeg12)
  2330. {
  2331. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2332. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  2333. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  2334. return;
  2335. }
  2336. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2337. /* print DCT coefficients */
  2338. int i,j;
  2339. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2340. for(i=0; i<6; i++){
  2341. for(j=0; j<64; j++){
  2342. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2343. }
  2344. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2345. }
  2346. }
  2347. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2348. /* update DC predictors for P macroblocks */
  2349. if (!s->mb_intra) {
  2350. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2351. if(s->mbintra_table[mb_xy])
  2352. ff_clean_intra_table_entries(s);
  2353. } else {
  2354. s->last_dc[0] =
  2355. s->last_dc[1] =
  2356. s->last_dc[2] = 128 << s->intra_dc_precision;
  2357. }
  2358. }
  2359. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2360. s->mbintra_table[mb_xy]=1;
  2361. 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
  2362. uint8_t *dest_y, *dest_cb, *dest_cr;
  2363. int dct_linesize, dct_offset;
  2364. op_pixels_func (*op_pix)[4];
  2365. qpel_mc_func (*op_qpix)[16];
  2366. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2367. const int uvlinesize = s->current_picture.f.linesize[1];
  2368. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2369. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2370. /* avoid copy if macroblock skipped in last frame too */
  2371. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2372. if(!s->encoding){
  2373. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2374. if (s->mb_skipped) {
  2375. s->mb_skipped= 0;
  2376. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2377. *mbskip_ptr = 1;
  2378. } else if(!s->current_picture.reference) {
  2379. *mbskip_ptr = 1;
  2380. } else{
  2381. *mbskip_ptr = 0; /* not skipped */
  2382. }
  2383. }
  2384. dct_linesize = linesize << s->interlaced_dct;
  2385. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2386. if(readable){
  2387. dest_y= s->dest[0];
  2388. dest_cb= s->dest[1];
  2389. dest_cr= s->dest[2];
  2390. }else{
  2391. dest_y = s->b_scratchpad;
  2392. dest_cb= s->b_scratchpad+16*linesize;
  2393. dest_cr= s->b_scratchpad+32*linesize;
  2394. }
  2395. if (!s->mb_intra) {
  2396. /* motion handling */
  2397. /* decoding or more than one mb_type (MC was already done otherwise) */
  2398. if(!s->encoding){
  2399. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2400. if (s->mv_dir & MV_DIR_FORWARD) {
  2401. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2402. ff_MPV_lowest_referenced_row(s, 0),
  2403. 0);
  2404. }
  2405. if (s->mv_dir & MV_DIR_BACKWARD) {
  2406. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2407. ff_MPV_lowest_referenced_row(s, 1),
  2408. 0);
  2409. }
  2410. }
  2411. if(lowres_flag){
  2412. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2413. if (s->mv_dir & MV_DIR_FORWARD) {
  2414. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2415. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2416. }
  2417. if (s->mv_dir & MV_DIR_BACKWARD) {
  2418. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2419. }
  2420. }else{
  2421. op_qpix= s->me.qpel_put;
  2422. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2423. op_pix = s->hdsp.put_pixels_tab;
  2424. }else{
  2425. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2426. }
  2427. if (s->mv_dir & MV_DIR_FORWARD) {
  2428. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2429. op_pix = s->hdsp.avg_pixels_tab;
  2430. op_qpix= s->me.qpel_avg;
  2431. }
  2432. if (s->mv_dir & MV_DIR_BACKWARD) {
  2433. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2434. }
  2435. }
  2436. }
  2437. /* skip dequant / idct if we are really late ;) */
  2438. if(s->avctx->skip_idct){
  2439. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2440. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2441. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2442. goto skip_idct;
  2443. }
  2444. /* add dct residue */
  2445. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2446. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2447. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2448. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2449. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2450. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2451. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2452. if (s->chroma_y_shift){
  2453. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2454. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2455. }else{
  2456. dct_linesize >>= 1;
  2457. dct_offset >>=1;
  2458. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2459. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2460. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2461. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2462. }
  2463. }
  2464. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2465. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2466. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2467. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2468. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2469. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2470. if(s->chroma_y_shift){//Chroma420
  2471. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2472. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2473. }else{
  2474. //chroma422
  2475. dct_linesize = uvlinesize << s->interlaced_dct;
  2476. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2477. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2478. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2479. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2480. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2481. if(!s->chroma_x_shift){//Chroma444
  2482. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2483. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2484. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2485. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2486. }
  2487. }
  2488. }//fi gray
  2489. }
  2490. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2491. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2492. }
  2493. } else {
  2494. /* dct only in intra block */
  2495. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2496. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2497. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2498. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2499. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2500. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2501. if(s->chroma_y_shift){
  2502. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2503. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2504. }else{
  2505. dct_offset >>=1;
  2506. dct_linesize >>=1;
  2507. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2508. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2509. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2510. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2511. }
  2512. }
  2513. }else{
  2514. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2515. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2516. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2517. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2518. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2519. if(s->chroma_y_shift){
  2520. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2521. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2522. }else{
  2523. dct_linesize = uvlinesize << s->interlaced_dct;
  2524. dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
  2525. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2526. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2527. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2528. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2529. if(!s->chroma_x_shift){//Chroma444
  2530. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2531. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2532. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2533. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2534. }
  2535. }
  2536. }//gray
  2537. }
  2538. }
  2539. skip_idct:
  2540. if(!readable){
  2541. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2542. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2543. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2544. }
  2545. }
  2546. }
  2547. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2548. #if !CONFIG_SMALL
  2549. if(s->out_format == FMT_MPEG1) {
  2550. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2551. else MPV_decode_mb_internal(s, block, 0, 1);
  2552. } else
  2553. #endif
  2554. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2555. else MPV_decode_mb_internal(s, block, 0, 0);
  2556. }
  2557. /**
  2558. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2559. */
  2560. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  2561. Picture *last, int y, int h, int picture_structure,
  2562. int first_field, int draw_edges, int low_delay,
  2563. int v_edge_pos, int h_edge_pos)
  2564. {
  2565. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2566. int hshift = desc->log2_chroma_w;
  2567. int vshift = desc->log2_chroma_h;
  2568. const int field_pic = picture_structure != PICT_FRAME;
  2569. if(field_pic){
  2570. h <<= 1;
  2571. y <<= 1;
  2572. }
  2573. if (!avctx->hwaccel &&
  2574. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  2575. draw_edges &&
  2576. cur->reference &&
  2577. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  2578. int *linesize = cur->f.linesize;
  2579. int sides = 0, edge_h;
  2580. if (y==0) sides |= EDGE_TOP;
  2581. if (y + h >= v_edge_pos)
  2582. sides |= EDGE_BOTTOM;
  2583. edge_h= FFMIN(h, v_edge_pos - y);
  2584. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  2585. linesize[0], h_edge_pos, edge_h,
  2586. EDGE_WIDTH, EDGE_WIDTH, sides);
  2587. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  2588. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  2589. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2590. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  2591. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  2592. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2593. }
  2594. h = FFMIN(h, avctx->height - y);
  2595. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2596. if (avctx->draw_horiz_band) {
  2597. AVFrame *src;
  2598. int offset[AV_NUM_DATA_POINTERS];
  2599. int i;
  2600. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2601. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2602. src = &cur->f;
  2603. else if (last)
  2604. src = &last->f;
  2605. else
  2606. return;
  2607. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2608. picture_structure == PICT_FRAME &&
  2609. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2610. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2611. offset[i] = 0;
  2612. }else{
  2613. offset[0]= y * src->linesize[0];
  2614. offset[1]=
  2615. offset[2]= (y >> vshift) * src->linesize[1];
  2616. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2617. offset[i] = 0;
  2618. }
  2619. emms_c();
  2620. avctx->draw_horiz_band(avctx, src, offset,
  2621. y, picture_structure, h);
  2622. }
  2623. }
  2624. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2625. {
  2626. int draw_edges = s->unrestricted_mv && !s->intra_only;
  2627. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  2628. &s->last_picture, y, h, s->picture_structure,
  2629. s->first_field, draw_edges, s->low_delay,
  2630. s->v_edge_pos, s->h_edge_pos);
  2631. }
  2632. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2633. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2634. const int uvlinesize = s->current_picture.f.linesize[1];
  2635. const int mb_size= 4 - s->avctx->lowres;
  2636. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2637. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2638. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2639. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2640. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2641. 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;
  2642. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2643. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2644. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2645. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2646. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2647. {
  2648. if(s->picture_structure==PICT_FRAME){
  2649. s->dest[0] += s->mb_y * linesize << mb_size;
  2650. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2651. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2652. }else{
  2653. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2654. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2655. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2656. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2657. }
  2658. }
  2659. }
  2660. /**
  2661. * Permute an 8x8 block.
  2662. * @param block the block which will be permuted according to the given permutation vector
  2663. * @param permutation the permutation vector
  2664. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2665. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2666. * (inverse) permutated to scantable order!
  2667. */
  2668. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2669. {
  2670. int i;
  2671. int16_t temp[64];
  2672. if(last<=0) return;
  2673. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2674. for(i=0; i<=last; i++){
  2675. const int j= scantable[i];
  2676. temp[j]= block[j];
  2677. block[j]=0;
  2678. }
  2679. for(i=0; i<=last; i++){
  2680. const int j= scantable[i];
  2681. const int perm_j= permutation[j];
  2682. block[perm_j]= temp[j];
  2683. }
  2684. }
  2685. void ff_mpeg_flush(AVCodecContext *avctx){
  2686. int i;
  2687. MpegEncContext *s = avctx->priv_data;
  2688. if(s==NULL || s->picture==NULL)
  2689. return;
  2690. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2691. ff_mpeg_unref_picture(s, &s->picture[i]);
  2692. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2693. s->mb_x= s->mb_y= 0;
  2694. s->closed_gop= 0;
  2695. s->parse_context.state= -1;
  2696. s->parse_context.frame_start_found= 0;
  2697. s->parse_context.overread= 0;
  2698. s->parse_context.overread_index= 0;
  2699. s->parse_context.index= 0;
  2700. s->parse_context.last_index= 0;
  2701. s->bitstream_buffer_size=0;
  2702. s->pp_time=0;
  2703. }
  2704. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2705. int16_t *block, int n, int qscale)
  2706. {
  2707. int i, level, nCoeffs;
  2708. const uint16_t *quant_matrix;
  2709. nCoeffs= s->block_last_index[n];
  2710. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2711. /* XXX: only mpeg1 */
  2712. quant_matrix = s->intra_matrix;
  2713. for(i=1;i<=nCoeffs;i++) {
  2714. int j= s->intra_scantable.permutated[i];
  2715. level = block[j];
  2716. if (level) {
  2717. if (level < 0) {
  2718. level = -level;
  2719. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2720. level = (level - 1) | 1;
  2721. level = -level;
  2722. } else {
  2723. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2724. level = (level - 1) | 1;
  2725. }
  2726. block[j] = level;
  2727. }
  2728. }
  2729. }
  2730. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2731. int16_t *block, int n, int qscale)
  2732. {
  2733. int i, level, nCoeffs;
  2734. const uint16_t *quant_matrix;
  2735. nCoeffs= s->block_last_index[n];
  2736. quant_matrix = s->inter_matrix;
  2737. for(i=0; i<=nCoeffs; i++) {
  2738. int j= s->intra_scantable.permutated[i];
  2739. level = block[j];
  2740. if (level) {
  2741. if (level < 0) {
  2742. level = -level;
  2743. level = (((level << 1) + 1) * qscale *
  2744. ((int) (quant_matrix[j]))) >> 4;
  2745. level = (level - 1) | 1;
  2746. level = -level;
  2747. } else {
  2748. level = (((level << 1) + 1) * qscale *
  2749. ((int) (quant_matrix[j]))) >> 4;
  2750. level = (level - 1) | 1;
  2751. }
  2752. block[j] = level;
  2753. }
  2754. }
  2755. }
  2756. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2757. int16_t *block, int n, int qscale)
  2758. {
  2759. int i, level, nCoeffs;
  2760. const uint16_t *quant_matrix;
  2761. if(s->alternate_scan) nCoeffs= 63;
  2762. else nCoeffs= s->block_last_index[n];
  2763. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2764. quant_matrix = s->intra_matrix;
  2765. for(i=1;i<=nCoeffs;i++) {
  2766. int j= s->intra_scantable.permutated[i];
  2767. level = block[j];
  2768. if (level) {
  2769. if (level < 0) {
  2770. level = -level;
  2771. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2772. level = -level;
  2773. } else {
  2774. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2775. }
  2776. block[j] = level;
  2777. }
  2778. }
  2779. }
  2780. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2781. int16_t *block, int n, int qscale)
  2782. {
  2783. int i, level, nCoeffs;
  2784. const uint16_t *quant_matrix;
  2785. int sum=-1;
  2786. if(s->alternate_scan) nCoeffs= 63;
  2787. else nCoeffs= s->block_last_index[n];
  2788. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2789. sum += block[0];
  2790. quant_matrix = s->intra_matrix;
  2791. for(i=1;i<=nCoeffs;i++) {
  2792. int j= s->intra_scantable.permutated[i];
  2793. level = block[j];
  2794. if (level) {
  2795. if (level < 0) {
  2796. level = -level;
  2797. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2798. level = -level;
  2799. } else {
  2800. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2801. }
  2802. block[j] = level;
  2803. sum+=level;
  2804. }
  2805. }
  2806. block[63]^=sum&1;
  2807. }
  2808. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2809. int16_t *block, int n, int qscale)
  2810. {
  2811. int i, level, nCoeffs;
  2812. const uint16_t *quant_matrix;
  2813. int sum=-1;
  2814. if(s->alternate_scan) nCoeffs= 63;
  2815. else nCoeffs= s->block_last_index[n];
  2816. quant_matrix = s->inter_matrix;
  2817. for(i=0; i<=nCoeffs; i++) {
  2818. int j= s->intra_scantable.permutated[i];
  2819. level = block[j];
  2820. if (level) {
  2821. if (level < 0) {
  2822. level = -level;
  2823. level = (((level << 1) + 1) * qscale *
  2824. ((int) (quant_matrix[j]))) >> 4;
  2825. level = -level;
  2826. } else {
  2827. level = (((level << 1) + 1) * qscale *
  2828. ((int) (quant_matrix[j]))) >> 4;
  2829. }
  2830. block[j] = level;
  2831. sum+=level;
  2832. }
  2833. }
  2834. block[63]^=sum&1;
  2835. }
  2836. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2837. int16_t *block, int n, int qscale)
  2838. {
  2839. int i, level, qmul, qadd;
  2840. int nCoeffs;
  2841. assert(s->block_last_index[n]>=0);
  2842. qmul = qscale << 1;
  2843. if (!s->h263_aic) {
  2844. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2845. qadd = (qscale - 1) | 1;
  2846. }else{
  2847. qadd = 0;
  2848. }
  2849. if(s->ac_pred)
  2850. nCoeffs=63;
  2851. else
  2852. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2853. for(i=1; i<=nCoeffs; i++) {
  2854. level = block[i];
  2855. if (level) {
  2856. if (level < 0) {
  2857. level = level * qmul - qadd;
  2858. } else {
  2859. level = level * qmul + qadd;
  2860. }
  2861. block[i] = level;
  2862. }
  2863. }
  2864. }
  2865. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2866. int16_t *block, int n, int qscale)
  2867. {
  2868. int i, level, qmul, qadd;
  2869. int nCoeffs;
  2870. assert(s->block_last_index[n]>=0);
  2871. qadd = (qscale - 1) | 1;
  2872. qmul = qscale << 1;
  2873. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2874. for(i=0; i<=nCoeffs; i++) {
  2875. level = block[i];
  2876. if (level) {
  2877. if (level < 0) {
  2878. level = level * qmul - qadd;
  2879. } else {
  2880. level = level * qmul + qadd;
  2881. }
  2882. block[i] = level;
  2883. }
  2884. }
  2885. }
  2886. /**
  2887. * set qscale and update qscale dependent variables.
  2888. */
  2889. void ff_set_qscale(MpegEncContext * s, int qscale)
  2890. {
  2891. if (qscale < 1)
  2892. qscale = 1;
  2893. else if (qscale > 31)
  2894. qscale = 31;
  2895. s->qscale = qscale;
  2896. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2897. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2898. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2899. }
  2900. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2901. {
  2902. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2903. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2904. }
  2905. #if CONFIG_ERROR_RESILIENCE
  2906. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2907. {
  2908. ERContext *er = &s->er;
  2909. er->cur_pic = s->current_picture_ptr;
  2910. er->last_pic = s->last_picture_ptr;
  2911. er->next_pic = s->next_picture_ptr;
  2912. er->pp_time = s->pp_time;
  2913. er->pb_time = s->pb_time;
  2914. er->quarter_sample = s->quarter_sample;
  2915. er->partitioned_frame = s->partitioned_frame;
  2916. ff_er_frame_start(er);
  2917. }
  2918. #endif /* CONFIG_ERROR_RESILIENCE */