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.

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