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.

2697 lines
98KB

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