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.

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