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.

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