You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2697 lines
98KB

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