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.

3264 lines
120KB

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