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.

2769 lines
100KB

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