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.

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