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.

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