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.

2544 lines
95KB

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