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.

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