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.

2891 lines
104KB

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