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.

2493 lines
93KB

  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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 + 95) * 2 * 21 * 4, 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 + 95) * 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)
  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->bitstream_buffer = NULL;
  477. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  478. if (s1->context_initialized){
  479. s->picture_range_start += MAX_PICTURE_COUNT;
  480. s->picture_range_end += MAX_PICTURE_COUNT;
  481. ff_MPV_common_init(s);
  482. }
  483. }
  484. s->avctx->coded_height = s1->avctx->coded_height;
  485. s->avctx->coded_width = s1->avctx->coded_width;
  486. s->avctx->width = s1->avctx->width;
  487. s->avctx->height = s1->avctx->height;
  488. s->coded_picture_number = s1->coded_picture_number;
  489. s->picture_number = s1->picture_number;
  490. s->input_picture_number = s1->input_picture_number;
  491. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  492. memcpy(&s->last_picture, &s1->last_picture,
  493. (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
  494. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  495. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  496. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  497. // Error/bug resilience
  498. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  499. s->workaround_bugs = s1->workaround_bugs;
  500. s->padding_bug_score = s1->padding_bug_score;
  501. // MPEG4 timing info
  502. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  503. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  504. // B-frame info
  505. s->max_b_frames = s1->max_b_frames;
  506. s->low_delay = s1->low_delay;
  507. s->dropable = s1->dropable;
  508. // DivX handling (doesn't work)
  509. s->divx_packed = s1->divx_packed;
  510. if (s1->bitstream_buffer) {
  511. if (s1->bitstream_buffer_size +
  512. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  513. av_fast_malloc(&s->bitstream_buffer,
  514. &s->allocated_bitstream_buffer_size,
  515. s1->allocated_bitstream_buffer_size);
  516. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  517. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  518. s1->bitstream_buffer_size);
  519. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  520. FF_INPUT_BUFFER_PADDING_SIZE);
  521. }
  522. // MPEG2/interlacing info
  523. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  524. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  525. if (!s1->first_field) {
  526. s->last_pict_type = s1->pict_type;
  527. if (s1->current_picture_ptr)
  528. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  529. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  530. s->last_non_b_pict_type = s1->pict_type;
  531. }
  532. }
  533. return 0;
  534. }
  535. /**
  536. * Set the given MpegEncContext to common defaults
  537. * (same for encoding and decoding).
  538. * The changed fields will not depend upon the
  539. * prior state of the MpegEncContext.
  540. */
  541. void ff_MPV_common_defaults(MpegEncContext *s)
  542. {
  543. s->y_dc_scale_table =
  544. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  545. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  546. s->progressive_frame = 1;
  547. s->progressive_sequence = 1;
  548. s->picture_structure = PICT_FRAME;
  549. s->coded_picture_number = 0;
  550. s->picture_number = 0;
  551. s->input_picture_number = 0;
  552. s->picture_in_gop_number = 0;
  553. s->f_code = 1;
  554. s->b_code = 1;
  555. s->picture_range_start = 0;
  556. s->picture_range_end = MAX_PICTURE_COUNT;
  557. s->slice_context_count = 1;
  558. }
  559. /**
  560. * Set the given MpegEncContext to defaults for decoding.
  561. * the changed fields will not depend upon
  562. * the prior state of the MpegEncContext.
  563. */
  564. void ff_MPV_decode_defaults(MpegEncContext *s)
  565. {
  566. ff_MPV_common_defaults(s);
  567. }
  568. /**
  569. * init common structure for both encoder and decoder.
  570. * this assumes that some variables like width/height are already set
  571. */
  572. av_cold int ff_MPV_common_init(MpegEncContext *s)
  573. {
  574. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  575. int nb_slices = (HAVE_THREADS &&
  576. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  577. s->avctx->thread_count : 1;
  578. if (s->encoding && s->avctx->slices)
  579. nb_slices = s->avctx->slices;
  580. if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  581. s->mb_height = (s->height + 31) / 32 * 2;
  582. else if (s->codec_id != CODEC_ID_H264)
  583. s->mb_height = (s->height + 15) / 16;
  584. if (s->avctx->pix_fmt == PIX_FMT_NONE) {
  585. av_log(s->avctx, AV_LOG_ERROR,
  586. "decoding to PIX_FMT_NONE is not supported.\n");
  587. return -1;
  588. }
  589. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  590. int max_slices;
  591. if (s->mb_height)
  592. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  593. else
  594. max_slices = MAX_THREADS;
  595. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  596. " reducing to %d\n", nb_slices, max_slices);
  597. nb_slices = max_slices;
  598. }
  599. if ((s->width || s->height) &&
  600. av_image_check_size(s->width, s->height, 0, s->avctx))
  601. return -1;
  602. ff_dct_common_init(s);
  603. s->flags = s->avctx->flags;
  604. s->flags2 = s->avctx->flags2;
  605. s->mb_width = (s->width + 15) / 16;
  606. s->mb_stride = s->mb_width + 1;
  607. s->b8_stride = s->mb_width * 2 + 1;
  608. s->b4_stride = s->mb_width * 4 + 1;
  609. mb_array_size = s->mb_height * s->mb_stride;
  610. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  611. /* set chroma shifts */
  612. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
  613. &s->chroma_y_shift);
  614. /* set default edge pos, will be overriden in decode_header if needed */
  615. s->h_edge_pos = s->mb_width * 16;
  616. s->v_edge_pos = s->mb_height * 16;
  617. s->mb_num = s->mb_width * s->mb_height;
  618. s->block_wrap[0] =
  619. s->block_wrap[1] =
  620. s->block_wrap[2] =
  621. s->block_wrap[3] = s->b8_stride;
  622. s->block_wrap[4] =
  623. s->block_wrap[5] = s->mb_stride;
  624. y_size = s->b8_stride * (2 * s->mb_height + 1);
  625. c_size = s->mb_stride * (s->mb_height + 1);
  626. yc_size = y_size + 2 * c_size;
  627. /* convert fourcc to upper case */
  628. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  629. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  630. s->avctx->coded_frame = &s->current_picture.f;
  631. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), 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] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  636. if (s->encoding) {
  637. /* Allocate MV tables */
  638. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  639. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  640. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  641. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  642. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  643. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  644. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  645. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  646. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  647. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  648. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  649. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  650. if(s->msmpeg4_version){
  651. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  652. }
  653. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  654. /* Allocate MB type table */
  655. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  656. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  657. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  658. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix , 64*32 * sizeof(int), fail)
  659. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  660. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16 , 64*32*2 * sizeof(uint16_t), fail)
  661. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  662. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16 , 64*32*2 * sizeof(uint16_t), fail)
  663. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  664. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  665. if(s->avctx->noise_reduction){
  666. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  667. }
  668. }
  669. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  670. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  671. s->picture_count * sizeof(Picture), fail);
  672. for (i = 0; i < s->picture_count; i++) {
  673. avcodec_get_frame_defaults(&s->picture[i].f);
  674. }
  675. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  676. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  677. /* interlaced direct mode decoding tables */
  678. for (i = 0; i < 2; i++) {
  679. int j, k;
  680. for (j = 0; j < 2; j++) {
  681. for (k = 0; k < 2; k++) {
  682. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  683. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  684. }
  685. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  686. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  687. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  688. }
  689. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  690. }
  691. }
  692. if (s->out_format == FMT_H263) {
  693. /* cbp values */
  694. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  695. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  696. /* cbp, ac_pred, pred_dir */
  697. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  698. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  699. }
  700. if (s->h263_pred || s->h263_plus || !s->encoding) {
  701. /* dc values */
  702. // MN: we need these for error resilience of intra-frames
  703. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  704. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  705. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  706. s->dc_val[2] = s->dc_val[1] + c_size;
  707. for (i = 0; i < yc_size; i++)
  708. s->dc_val_base[i] = 1024;
  709. }
  710. /* which mb is a intra block */
  711. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  712. memset(s->mbintra_table, 1, mb_array_size);
  713. /* init macroblock skip table */
  714. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  715. // Note the + 1 is for a quicker mpeg4 slice_end detection
  716. s->parse_context.state = -1;
  717. s->context_initialized = 1;
  718. s->thread_context[0] = s;
  719. // if (s->width && s->height) {
  720. if (nb_slices > 1) {
  721. for (i = 1; i < nb_slices; i++) {
  722. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  723. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  724. }
  725. for (i = 0; i < nb_slices; i++) {
  726. if (init_duplicate_context(s->thread_context[i], s) < 0)
  727. goto fail;
  728. s->thread_context[i]->start_mb_y =
  729. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  730. s->thread_context[i]->end_mb_y =
  731. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  732. }
  733. } else {
  734. if (init_duplicate_context(s, s) < 0)
  735. goto fail;
  736. s->start_mb_y = 0;
  737. s->end_mb_y = s->mb_height;
  738. }
  739. s->slice_context_count = nb_slices;
  740. // }
  741. return 0;
  742. fail:
  743. ff_MPV_common_end(s);
  744. return -1;
  745. }
  746. /* init common structure for both encoder and decoder */
  747. void ff_MPV_common_end(MpegEncContext *s)
  748. {
  749. int i, j, k;
  750. if (s->slice_context_count > 1) {
  751. for (i = 0; i < s->slice_context_count; i++) {
  752. free_duplicate_context(s->thread_context[i]);
  753. }
  754. for (i = 1; i < s->slice_context_count; i++) {
  755. av_freep(&s->thread_context[i]);
  756. }
  757. s->slice_context_count = 1;
  758. } else free_duplicate_context(s);
  759. av_freep(&s->parse_context.buffer);
  760. s->parse_context.buffer_size = 0;
  761. av_freep(&s->mb_type);
  762. av_freep(&s->p_mv_table_base);
  763. av_freep(&s->b_forw_mv_table_base);
  764. av_freep(&s->b_back_mv_table_base);
  765. av_freep(&s->b_bidir_forw_mv_table_base);
  766. av_freep(&s->b_bidir_back_mv_table_base);
  767. av_freep(&s->b_direct_mv_table_base);
  768. s->p_mv_table = NULL;
  769. s->b_forw_mv_table = NULL;
  770. s->b_back_mv_table = NULL;
  771. s->b_bidir_forw_mv_table = NULL;
  772. s->b_bidir_back_mv_table = NULL;
  773. s->b_direct_mv_table = NULL;
  774. for (i = 0; i < 2; i++) {
  775. for (j = 0; j < 2; j++) {
  776. for (k = 0; k < 2; k++) {
  777. av_freep(&s->b_field_mv_table_base[i][j][k]);
  778. s->b_field_mv_table[i][j][k] = NULL;
  779. }
  780. av_freep(&s->b_field_select_table[i][j]);
  781. av_freep(&s->p_field_mv_table_base[i][j]);
  782. s->p_field_mv_table[i][j] = NULL;
  783. }
  784. av_freep(&s->p_field_select_table[i]);
  785. }
  786. av_freep(&s->dc_val_base);
  787. av_freep(&s->coded_block_base);
  788. av_freep(&s->mbintra_table);
  789. av_freep(&s->cbp_table);
  790. av_freep(&s->pred_dir_table);
  791. av_freep(&s->mbskip_table);
  792. av_freep(&s->bitstream_buffer);
  793. s->allocated_bitstream_buffer_size = 0;
  794. av_freep(&s->avctx->stats_out);
  795. av_freep(&s->ac_stats);
  796. av_freep(&s->error_status_table);
  797. av_freep(&s->mb_index2xy);
  798. av_freep(&s->lambda_table);
  799. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  800. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  801. s->q_chroma_intra_matrix= NULL;
  802. s->q_chroma_intra_matrix16= NULL;
  803. av_freep(&s->q_intra_matrix);
  804. av_freep(&s->q_inter_matrix);
  805. av_freep(&s->q_intra_matrix16);
  806. av_freep(&s->q_inter_matrix16);
  807. av_freep(&s->input_picture);
  808. av_freep(&s->reordered_input_picture);
  809. av_freep(&s->dct_offset);
  810. if (s->picture && !s->avctx->internal->is_copy) {
  811. for (i = 0; i < s->picture_count; i++) {
  812. free_picture(s, &s->picture[i]);
  813. }
  814. }
  815. av_freep(&s->picture);
  816. s->context_initialized = 0;
  817. s->last_picture_ptr =
  818. s->next_picture_ptr =
  819. s->current_picture_ptr = NULL;
  820. s->linesize = s->uvlinesize = 0;
  821. for (i = 0; i < 3; i++)
  822. av_freep(&s->visualization_buffer[i]);
  823. if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
  824. avcodec_default_free_buffers(s->avctx);
  825. }
  826. void ff_init_rl(RLTable *rl,
  827. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  828. {
  829. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  830. uint8_t index_run[MAX_RUN + 1];
  831. int last, run, level, start, end, i;
  832. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  833. if (static_store && rl->max_level[0])
  834. return;
  835. /* compute max_level[], max_run[] and index_run[] */
  836. for (last = 0; last < 2; last++) {
  837. if (last == 0) {
  838. start = 0;
  839. end = rl->last;
  840. } else {
  841. start = rl->last;
  842. end = rl->n;
  843. }
  844. memset(max_level, 0, MAX_RUN + 1);
  845. memset(max_run, 0, MAX_LEVEL + 1);
  846. memset(index_run, rl->n, MAX_RUN + 1);
  847. for (i = start; i < end; i++) {
  848. run = rl->table_run[i];
  849. level = rl->table_level[i];
  850. if (index_run[run] == rl->n)
  851. index_run[run] = i;
  852. if (level > max_level[run])
  853. max_level[run] = level;
  854. if (run > max_run[level])
  855. max_run[level] = run;
  856. }
  857. if (static_store)
  858. rl->max_level[last] = static_store[last];
  859. else
  860. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  861. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  862. if (static_store)
  863. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  864. else
  865. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  866. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  867. if (static_store)
  868. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  869. else
  870. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  871. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  872. }
  873. }
  874. void ff_init_vlc_rl(RLTable *rl)
  875. {
  876. int i, q;
  877. for (q = 0; q < 32; q++) {
  878. int qmul = q * 2;
  879. int qadd = (q - 1) | 1;
  880. if (q == 0) {
  881. qmul = 1;
  882. qadd = 0;
  883. }
  884. for (i = 0; i < rl->vlc.table_size; i++) {
  885. int code = rl->vlc.table[i][0];
  886. int len = rl->vlc.table[i][1];
  887. int level, run;
  888. if (len == 0) { // illegal code
  889. run = 66;
  890. level = MAX_LEVEL;
  891. } else if (len < 0) { // more bits needed
  892. run = 0;
  893. level = code;
  894. } else {
  895. if (code == rl->n) { // esc
  896. run = 66;
  897. level = 0;
  898. } else {
  899. run = rl->table_run[code] + 1;
  900. level = rl->table_level[code] * qmul + qadd;
  901. if (code >= rl->last) run += 192;
  902. }
  903. }
  904. rl->rl_vlc[q][i].len = len;
  905. rl->rl_vlc[q][i].level = level;
  906. rl->rl_vlc[q][i].run = run;
  907. }
  908. }
  909. }
  910. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  911. {
  912. int i;
  913. /* release non reference frames */
  914. for (i = 0; i < s->picture_count; i++) {
  915. if (s->picture[i].f.data[0] && !s->picture[i].f.reference &&
  916. (!s->picture[i].owner2 || s->picture[i].owner2 == s) &&
  917. (remove_current || &s->picture[i] != s->current_picture_ptr)
  918. /* && s->picture[i].type!= FF_BUFFER_TYPE_SHARED */) {
  919. free_frame_buffer(s, &s->picture[i]);
  920. }
  921. }
  922. }
  923. int ff_find_unused_picture(MpegEncContext *s, int shared)
  924. {
  925. int i;
  926. if (shared) {
  927. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  928. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
  929. return i;
  930. }
  931. } else {
  932. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  933. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type != 0)
  934. return i; // FIXME
  935. }
  936. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  937. if (s->picture[i].f.data[0] == NULL)
  938. return i;
  939. }
  940. }
  941. av_log(s->avctx, AV_LOG_FATAL,
  942. "Internal error, picture buffer overflow\n");
  943. /* We could return -1, but the codec would crash trying to draw into a
  944. * non-existing frame anyway. This is safer than waiting for a random crash.
  945. * Also the return of this is never useful, an encoder must only allocate
  946. * as much as allowed in the specification. This has no relationship to how
  947. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  948. * enough for such valid streams).
  949. * Plus, a decoder has to check stream validity and remove frames if too
  950. * many reference frames are around. Waiting for "OOM" is not correct at
  951. * all. Similarly, missing reference frames have to be replaced by
  952. * interpolated/MC frames, anything else is a bug in the codec ...
  953. */
  954. abort();
  955. return -1;
  956. }
  957. static void update_noise_reduction(MpegEncContext *s)
  958. {
  959. int intra, i;
  960. for (intra = 0; intra < 2; intra++) {
  961. if (s->dct_count[intra] > (1 << 16)) {
  962. for (i = 0; i < 64; i++) {
  963. s->dct_error_sum[intra][i] >>= 1;
  964. }
  965. s->dct_count[intra] >>= 1;
  966. }
  967. for (i = 0; i < 64; i++) {
  968. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  969. s->dct_count[intra] +
  970. s->dct_error_sum[intra][i] / 2) /
  971. (s->dct_error_sum[intra][i] + 1);
  972. }
  973. }
  974. }
  975. /**
  976. * generic function for encode/decode called after coding/decoding
  977. * the header and before a frame is coded/decoded.
  978. */
  979. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  980. {
  981. int i;
  982. Picture *pic;
  983. s->mb_skipped = 0;
  984. assert(s->last_picture_ptr == NULL || s->out_format != FMT_H264 ||
  985. s->codec_id == CODEC_ID_SVQ3);
  986. if (!ff_thread_can_start_frame(avctx)) {
  987. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  988. return -1;
  989. }
  990. /* mark & release old frames */
  991. if (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3) {
  992. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  993. s->last_picture_ptr != s->next_picture_ptr &&
  994. s->last_picture_ptr->f.data[0]) {
  995. if (s->last_picture_ptr->owner2 == s)
  996. free_frame_buffer(s, s->last_picture_ptr);
  997. }
  998. /* release forgotten pictures */
  999. /* if (mpeg124/h263) */
  1000. if (!s->encoding) {
  1001. for (i = 0; i < s->picture_count; i++) {
  1002. if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
  1003. &s->picture[i] != s->last_picture_ptr &&
  1004. &s->picture[i] != s->next_picture_ptr &&
  1005. s->picture[i].f.reference) {
  1006. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1007. av_log(avctx, AV_LOG_ERROR,
  1008. "releasing zombie picture\n");
  1009. free_frame_buffer(s, &s->picture[i]);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. if (!s->encoding) {
  1015. ff_release_unused_pictures(s, 1);
  1016. if (s->current_picture_ptr &&
  1017. s->current_picture_ptr->f.data[0] == NULL) {
  1018. // we already have a unused image
  1019. // (maybe it was set before reading the header)
  1020. pic = s->current_picture_ptr;
  1021. } else {
  1022. i = ff_find_unused_picture(s, 0);
  1023. if (i < 0)
  1024. return i;
  1025. pic = &s->picture[i];
  1026. }
  1027. pic->f.reference = 0;
  1028. if (!s->dropable) {
  1029. if (s->codec_id == CODEC_ID_H264)
  1030. pic->f.reference = s->picture_structure;
  1031. else if (s->pict_type != AV_PICTURE_TYPE_B)
  1032. pic->f.reference = 3;
  1033. }
  1034. pic->f.coded_picture_number = s->coded_picture_number++;
  1035. if (ff_alloc_picture(s, pic, 0) < 0)
  1036. return -1;
  1037. s->current_picture_ptr = pic;
  1038. // FIXME use only the vars from current_pic
  1039. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1040. if (s->codec_id == CODEC_ID_MPEG1VIDEO ||
  1041. s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1042. if (s->picture_structure != PICT_FRAME)
  1043. s->current_picture_ptr->f.top_field_first =
  1044. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1045. }
  1046. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1047. !s->progressive_sequence;
  1048. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1049. }
  1050. s->current_picture_ptr->f.pict_type = s->pict_type;
  1051. // if (s->flags && CODEC_FLAG_QSCALE)
  1052. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1053. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1054. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  1055. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1056. s->last_picture_ptr = s->next_picture_ptr;
  1057. if (!s->dropable)
  1058. s->next_picture_ptr = s->current_picture_ptr;
  1059. }
  1060. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1061. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1062. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1063. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1064. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1065. s->pict_type, s->dropable); */
  1066. if (s->codec_id != CODEC_ID_H264) {
  1067. if ((s->last_picture_ptr == NULL ||
  1068. s->last_picture_ptr->f.data[0] == NULL) &&
  1069. (s->pict_type != AV_PICTURE_TYPE_I ||
  1070. s->picture_structure != PICT_FRAME)) {
  1071. if (s->pict_type != AV_PICTURE_TYPE_I)
  1072. av_log(avctx, AV_LOG_ERROR,
  1073. "warning: first frame is no keyframe\n");
  1074. else if (s->picture_structure != PICT_FRAME)
  1075. av_log(avctx, AV_LOG_INFO,
  1076. "allocate dummy last picture for field based first keyframe\n");
  1077. /* Allocate a dummy frame */
  1078. i = ff_find_unused_picture(s, 0);
  1079. if (i < 0)
  1080. return i;
  1081. s->last_picture_ptr = &s->picture[i];
  1082. s->last_picture_ptr->f.key_frame = 0;
  1083. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1084. s->last_picture_ptr = NULL;
  1085. return -1;
  1086. }
  1087. if(s->codec_id == CODEC_ID_FLV1 || s->codec_id == CODEC_ID_H263){
  1088. for(i=0; i<avctx->height; i++)
  1089. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1090. }
  1091. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
  1092. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
  1093. s->last_picture_ptr->f.reference = 3;
  1094. }
  1095. if ((s->next_picture_ptr == NULL ||
  1096. s->next_picture_ptr->f.data[0] == NULL) &&
  1097. s->pict_type == AV_PICTURE_TYPE_B) {
  1098. /* Allocate a dummy frame */
  1099. i = ff_find_unused_picture(s, 0);
  1100. if (i < 0)
  1101. return i;
  1102. s->next_picture_ptr = &s->picture[i];
  1103. s->next_picture_ptr->f.key_frame = 0;
  1104. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1105. s->next_picture_ptr = NULL;
  1106. return -1;
  1107. }
  1108. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
  1109. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
  1110. s->next_picture_ptr->f.reference = 3;
  1111. }
  1112. }
  1113. if (s->last_picture_ptr)
  1114. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1115. if (s->next_picture_ptr)
  1116. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1117. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME) &&
  1118. (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3)) {
  1119. if (s->next_picture_ptr)
  1120. s->next_picture_ptr->owner2 = s;
  1121. if (s->last_picture_ptr)
  1122. s->last_picture_ptr->owner2 = s;
  1123. }
  1124. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1125. s->last_picture_ptr->f.data[0]));
  1126. if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
  1127. int i;
  1128. for (i = 0; i < 4; i++) {
  1129. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1130. s->current_picture.f.data[i] +=
  1131. s->current_picture.f.linesize[i];
  1132. }
  1133. s->current_picture.f.linesize[i] *= 2;
  1134. s->last_picture.f.linesize[i] *= 2;
  1135. s->next_picture.f.linesize[i] *= 2;
  1136. }
  1137. }
  1138. s->err_recognition = avctx->err_recognition;
  1139. /* set dequantizer, we can't do it during init as
  1140. * it might change for mpeg4 and we can't do it in the header
  1141. * decode as init is not called for mpeg4 there yet */
  1142. if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1143. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1144. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1145. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1146. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1147. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1148. } else {
  1149. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1150. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1151. }
  1152. if (s->dct_error_sum) {
  1153. assert(s->avctx->noise_reduction && s->encoding);
  1154. update_noise_reduction(s);
  1155. }
  1156. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1157. return ff_xvmc_field_start(s, avctx);
  1158. return 0;
  1159. }
  1160. /* generic function for encode/decode called after a
  1161. * frame has been coded/decoded. */
  1162. void ff_MPV_frame_end(MpegEncContext *s)
  1163. {
  1164. int i;
  1165. /* redraw edges for the frame if decoding didn't complete */
  1166. // just to make sure that all data is rendered.
  1167. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1168. ff_xvmc_field_end(s);
  1169. } else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1170. !s->avctx->hwaccel &&
  1171. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1172. s->unrestricted_mv &&
  1173. s->current_picture.f.reference &&
  1174. !s->intra_only &&
  1175. !(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1176. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1177. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1178. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1179. s->h_edge_pos, s->v_edge_pos,
  1180. EDGE_WIDTH, EDGE_WIDTH,
  1181. EDGE_TOP | EDGE_BOTTOM);
  1182. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1183. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1184. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1185. EDGE_TOP | EDGE_BOTTOM);
  1186. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1187. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1188. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1189. EDGE_TOP | EDGE_BOTTOM);
  1190. }
  1191. emms_c();
  1192. s->last_pict_type = s->pict_type;
  1193. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1194. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1195. s->last_non_b_pict_type = s->pict_type;
  1196. }
  1197. #if 0
  1198. /* copy back current_picture variables */
  1199. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1200. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1201. s->picture[i] = s->current_picture;
  1202. break;
  1203. }
  1204. }
  1205. assert(i < MAX_PICTURE_COUNT);
  1206. #endif
  1207. if (s->encoding) {
  1208. /* release non-reference frames */
  1209. for (i = 0; i < s->picture_count; i++) {
  1210. if (s->picture[i].f.data[0] && !s->picture[i].f.reference
  1211. /* && s->picture[i].type != FF_BUFFER_TYPE_SHARED */) {
  1212. free_frame_buffer(s, &s->picture[i]);
  1213. }
  1214. }
  1215. }
  1216. // clear copies, to avoid confusion
  1217. #if 0
  1218. memset(&s->last_picture, 0, sizeof(Picture));
  1219. memset(&s->next_picture, 0, sizeof(Picture));
  1220. memset(&s->current_picture, 0, sizeof(Picture));
  1221. #endif
  1222. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1223. if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
  1224. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  1225. }
  1226. }
  1227. /**
  1228. * Draw a line from (ex, ey) -> (sx, sy).
  1229. * @param w width of the image
  1230. * @param h height of the image
  1231. * @param stride stride/linesize of the image
  1232. * @param color color of the arrow
  1233. */
  1234. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1235. int w, int h, int stride, int color)
  1236. {
  1237. int x, y, fr, f;
  1238. sx = av_clip(sx, 0, w - 1);
  1239. sy = av_clip(sy, 0, h - 1);
  1240. ex = av_clip(ex, 0, w - 1);
  1241. ey = av_clip(ey, 0, h - 1);
  1242. buf[sy * stride + sx] += color;
  1243. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1244. if (sx > ex) {
  1245. FFSWAP(int, sx, ex);
  1246. FFSWAP(int, sy, ey);
  1247. }
  1248. buf += sx + sy * stride;
  1249. ex -= sx;
  1250. f = ((ey - sy) << 16) / ex;
  1251. for(x= 0; x <= ex; x++){
  1252. y = (x * f) >> 16;
  1253. fr = (x * f) & 0xFFFF;
  1254. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1255. buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1256. }
  1257. } else {
  1258. if (sy > ey) {
  1259. FFSWAP(int, sx, ex);
  1260. FFSWAP(int, sy, ey);
  1261. }
  1262. buf += sx + sy * stride;
  1263. ey -= sy;
  1264. if (ey)
  1265. f = ((ex - sx) << 16) / ey;
  1266. else
  1267. f = 0;
  1268. for(y= 0; y <= ey; y++){
  1269. x = (y*f) >> 16;
  1270. fr = (y*f) & 0xFFFF;
  1271. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1272. buf[y * stride + x + 1] += (color * fr ) >> 16;
  1273. }
  1274. }
  1275. }
  1276. /**
  1277. * Draw an arrow from (ex, ey) -> (sx, sy).
  1278. * @param w width of the image
  1279. * @param h height of the image
  1280. * @param stride stride/linesize of the image
  1281. * @param color color of the arrow
  1282. */
  1283. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1284. int ey, int w, int h, int stride, int color)
  1285. {
  1286. int dx,dy;
  1287. sx = av_clip(sx, -100, w + 100);
  1288. sy = av_clip(sy, -100, h + 100);
  1289. ex = av_clip(ex, -100, w + 100);
  1290. ey = av_clip(ey, -100, h + 100);
  1291. dx = ex - sx;
  1292. dy = ey - sy;
  1293. if (dx * dx + dy * dy > 3 * 3) {
  1294. int rx = dx + dy;
  1295. int ry = -dx + dy;
  1296. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1297. // FIXME subpixel accuracy
  1298. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1299. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1300. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1301. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1302. }
  1303. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1304. }
  1305. /**
  1306. * Print debugging info for the given picture.
  1307. */
  1308. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
  1309. {
  1310. if (s->avctx->hwaccel || !pict || !pict->mb_type)
  1311. return;
  1312. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1313. int x,y;
  1314. av_log(s->avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1315. av_get_picture_type_char(pict->pict_type));
  1316. for (y = 0; y < s->mb_height; y++) {
  1317. for (x = 0; x < s->mb_width; x++) {
  1318. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1319. int count = s->mbskip_table[x + y * s->mb_stride];
  1320. if (count > 9)
  1321. count = 9;
  1322. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1323. }
  1324. if (s->avctx->debug & FF_DEBUG_QP) {
  1325. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1326. pict->qscale_table[x + y * s->mb_stride]);
  1327. }
  1328. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1329. int mb_type = pict->mb_type[x + y * s->mb_stride];
  1330. // Type & MV direction
  1331. if (IS_PCM(mb_type))
  1332. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1333. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1334. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1335. else if (IS_INTRA4x4(mb_type))
  1336. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1337. else if (IS_INTRA16x16(mb_type))
  1338. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1339. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1340. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1341. else if (IS_DIRECT(mb_type))
  1342. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1343. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1344. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1345. else if (IS_GMC(mb_type))
  1346. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1347. else if (IS_SKIP(mb_type))
  1348. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1349. else if (!USES_LIST(mb_type, 1))
  1350. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1351. else if (!USES_LIST(mb_type, 0))
  1352. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1353. else {
  1354. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1355. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1356. }
  1357. // segmentation
  1358. if (IS_8X8(mb_type))
  1359. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1360. else if (IS_16X8(mb_type))
  1361. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1362. else if (IS_8X16(mb_type))
  1363. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1364. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1365. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1366. else
  1367. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1368. if (IS_INTERLACED(mb_type))
  1369. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1370. else
  1371. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1372. }
  1373. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1374. }
  1375. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1376. }
  1377. }
  1378. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1379. (s->avctx->debug_mv)) {
  1380. const int shift = 1 + s->quarter_sample;
  1381. int mb_y;
  1382. uint8_t *ptr;
  1383. int i;
  1384. int h_chroma_shift, v_chroma_shift, block_height;
  1385. const int width = s->avctx->width;
  1386. const int height = s->avctx->height;
  1387. const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
  1388. const int mv_stride = (s->mb_width << mv_sample_log2) +
  1389. (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1390. s->low_delay = 0; // needed to see the vectors without trashing the buffers
  1391. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1392. &h_chroma_shift, &v_chroma_shift);
  1393. for (i = 0; i < 3; i++) {
  1394. size_t size= (i == 0) ? pict->linesize[i] * height:
  1395. pict->linesize[i] * height >> v_chroma_shift;
  1396. s->visualization_buffer[i]= av_realloc(s->visualization_buffer[i], size);
  1397. memcpy(s->visualization_buffer[i], pict->data[i], size);
  1398. pict->data[i] = s->visualization_buffer[i];
  1399. }
  1400. pict->type = FF_BUFFER_TYPE_COPY;
  1401. pict->opaque= NULL;
  1402. ptr = pict->data[0];
  1403. block_height = 16 >> v_chroma_shift;
  1404. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1405. int mb_x;
  1406. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1407. const int mb_index = mb_x + mb_y * s->mb_stride;
  1408. if ((s->avctx->debug_mv) && pict->motion_val) {
  1409. int type;
  1410. for (type = 0; type < 3; type++) {
  1411. int direction = 0;
  1412. switch (type) {
  1413. case 0:
  1414. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1415. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1416. continue;
  1417. direction = 0;
  1418. break;
  1419. case 1:
  1420. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1421. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1422. continue;
  1423. direction = 0;
  1424. break;
  1425. case 2:
  1426. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1427. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1428. continue;
  1429. direction = 1;
  1430. break;
  1431. }
  1432. if (!USES_LIST(pict->mb_type[mb_index], direction))
  1433. continue;
  1434. if (IS_8X8(pict->mb_type[mb_index])) {
  1435. int i;
  1436. for (i = 0; i < 4; i++) {
  1437. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1438. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1439. int xy = (mb_x * 2 + (i & 1) +
  1440. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1441. int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
  1442. int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
  1443. draw_arrow(ptr, sx, sy, mx, my, width,
  1444. height, s->linesize, 100);
  1445. }
  1446. } else if (IS_16X8(pict->mb_type[mb_index])) {
  1447. int i;
  1448. for (i = 0; i < 2; i++) {
  1449. int sx = mb_x * 16 + 8;
  1450. int sy = mb_y * 16 + 4 + 8 * i;
  1451. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1452. int mx = (pict->motion_val[direction][xy][0] >> shift);
  1453. int my = (pict->motion_val[direction][xy][1] >> shift);
  1454. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1455. my *= 2;
  1456. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1457. height, s->linesize, 100);
  1458. }
  1459. } else if (IS_8X16(pict->mb_type[mb_index])) {
  1460. int i;
  1461. for (i = 0; i < 2; i++) {
  1462. int sx = mb_x * 16 + 4 + 8 * i;
  1463. int sy = mb_y * 16 + 8;
  1464. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1465. int mx = pict->motion_val[direction][xy][0] >> shift;
  1466. int my = pict->motion_val[direction][xy][1] >> shift;
  1467. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1468. my *= 2;
  1469. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1470. height, s->linesize, 100);
  1471. }
  1472. } else {
  1473. int sx= mb_x * 16 + 8;
  1474. int sy= mb_y * 16 + 8;
  1475. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1476. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1477. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1478. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1479. }
  1480. }
  1481. }
  1482. if ((s->avctx->debug & FF_DEBUG_VIS_QP) && pict->motion_val) {
  1483. uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
  1484. 0x0101010101010101ULL;
  1485. int y;
  1486. for (y = 0; y < block_height; y++) {
  1487. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1488. (block_height * mb_y + y) *
  1489. pict->linesize[1]) = c;
  1490. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1491. (block_height * mb_y + y) *
  1492. pict->linesize[2]) = c;
  1493. }
  1494. }
  1495. if ((s->avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1496. pict->motion_val) {
  1497. int mb_type = pict->mb_type[mb_index];
  1498. uint64_t u,v;
  1499. int y;
  1500. #define COLOR(theta, r) \
  1501. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1502. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1503. u = v = 128;
  1504. if (IS_PCM(mb_type)) {
  1505. COLOR(120, 48)
  1506. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1507. IS_INTRA16x16(mb_type)) {
  1508. COLOR(30, 48)
  1509. } else if (IS_INTRA4x4(mb_type)) {
  1510. COLOR(90, 48)
  1511. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1512. // COLOR(120, 48)
  1513. } else if (IS_DIRECT(mb_type)) {
  1514. COLOR(150, 48)
  1515. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1516. COLOR(170, 48)
  1517. } else if (IS_GMC(mb_type)) {
  1518. COLOR(190, 48)
  1519. } else if (IS_SKIP(mb_type)) {
  1520. // COLOR(180, 48)
  1521. } else if (!USES_LIST(mb_type, 1)) {
  1522. COLOR(240, 48)
  1523. } else if (!USES_LIST(mb_type, 0)) {
  1524. COLOR(0, 48)
  1525. } else {
  1526. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1527. COLOR(300,48)
  1528. }
  1529. u *= 0x0101010101010101ULL;
  1530. v *= 0x0101010101010101ULL;
  1531. for (y = 0; y < block_height; y++) {
  1532. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1533. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1534. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1535. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1536. }
  1537. // segmentation
  1538. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1539. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1540. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1541. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1542. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1543. }
  1544. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1545. for (y = 0; y < 16; y++)
  1546. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1547. pict->linesize[0]] ^= 0x80;
  1548. }
  1549. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1550. int dm = 1 << (mv_sample_log2 - 2);
  1551. for (i = 0; i < 4; i++) {
  1552. int sx = mb_x * 16 + 8 * (i & 1);
  1553. int sy = mb_y * 16 + 8 * (i >> 1);
  1554. int xy = (mb_x * 2 + (i & 1) +
  1555. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1556. // FIXME bidir
  1557. int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
  1558. if (mv[0] != mv[dm] ||
  1559. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1560. for (y = 0; y < 8; y++)
  1561. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1562. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1563. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1564. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1565. }
  1566. }
  1567. if (IS_INTERLACED(mb_type) &&
  1568. s->codec_id == CODEC_ID_H264) {
  1569. // hmm
  1570. }
  1571. }
  1572. s->mbskip_table[mb_index] = 0;
  1573. }
  1574. }
  1575. }
  1576. }
  1577. /**
  1578. * find the lowest MB row referenced in the MVs
  1579. */
  1580. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1581. {
  1582. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1583. int my, off, i, mvs;
  1584. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1585. switch (s->mv_type) {
  1586. case MV_TYPE_16X16:
  1587. mvs = 1;
  1588. break;
  1589. case MV_TYPE_16X8:
  1590. mvs = 2;
  1591. break;
  1592. case MV_TYPE_8X8:
  1593. mvs = 4;
  1594. break;
  1595. default:
  1596. goto unhandled;
  1597. }
  1598. for (i = 0; i < mvs; i++) {
  1599. my = s->mv[dir][i][1]<<qpel_shift;
  1600. my_max = FFMAX(my_max, my);
  1601. my_min = FFMIN(my_min, my);
  1602. }
  1603. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1604. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1605. unhandled:
  1606. return s->mb_height-1;
  1607. }
  1608. /* put block[] to dest[] */
  1609. static inline void put_dct(MpegEncContext *s,
  1610. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1611. {
  1612. s->dct_unquantize_intra(s, block, i, qscale);
  1613. s->dsp.idct_put (dest, line_size, block);
  1614. }
  1615. /* add block[] to dest[] */
  1616. static inline void add_dct(MpegEncContext *s,
  1617. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1618. {
  1619. if (s->block_last_index[i] >= 0) {
  1620. s->dsp.idct_add (dest, line_size, block);
  1621. }
  1622. }
  1623. static inline void add_dequant_dct(MpegEncContext *s,
  1624. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1625. {
  1626. if (s->block_last_index[i] >= 0) {
  1627. s->dct_unquantize_inter(s, block, i, qscale);
  1628. s->dsp.idct_add (dest, line_size, block);
  1629. }
  1630. }
  1631. /**
  1632. * Clean dc, ac, coded_block for the current non-intra MB.
  1633. */
  1634. void ff_clean_intra_table_entries(MpegEncContext *s)
  1635. {
  1636. int wrap = s->b8_stride;
  1637. int xy = s->block_index[0];
  1638. s->dc_val[0][xy ] =
  1639. s->dc_val[0][xy + 1 ] =
  1640. s->dc_val[0][xy + wrap] =
  1641. s->dc_val[0][xy + 1 + wrap] = 1024;
  1642. /* ac pred */
  1643. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1644. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1645. if (s->msmpeg4_version>=3) {
  1646. s->coded_block[xy ] =
  1647. s->coded_block[xy + 1 ] =
  1648. s->coded_block[xy + wrap] =
  1649. s->coded_block[xy + 1 + wrap] = 0;
  1650. }
  1651. /* chroma */
  1652. wrap = s->mb_stride;
  1653. xy = s->mb_x + s->mb_y * wrap;
  1654. s->dc_val[1][xy] =
  1655. s->dc_val[2][xy] = 1024;
  1656. /* ac pred */
  1657. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1658. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1659. s->mbintra_table[xy]= 0;
  1660. }
  1661. /* generic function called after a macroblock has been parsed by the
  1662. decoder or after it has been encoded by the encoder.
  1663. Important variables used:
  1664. s->mb_intra : true if intra macroblock
  1665. s->mv_dir : motion vector direction
  1666. s->mv_type : motion vector type
  1667. s->mv : motion vector
  1668. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1669. */
  1670. static av_always_inline
  1671. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1672. int is_mpeg12)
  1673. {
  1674. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1675. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1676. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1677. return;
  1678. }
  1679. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1680. /* save DCT coefficients */
  1681. int i,j;
  1682. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  1683. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1684. for(i=0; i<6; i++){
  1685. for(j=0; j<64; j++){
  1686. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1687. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1688. }
  1689. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1690. }
  1691. }
  1692. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  1693. /* update DC predictors for P macroblocks */
  1694. if (!s->mb_intra) {
  1695. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1696. if(s->mbintra_table[mb_xy])
  1697. ff_clean_intra_table_entries(s);
  1698. } else {
  1699. s->last_dc[0] =
  1700. s->last_dc[1] =
  1701. s->last_dc[2] = 128 << s->intra_dc_precision;
  1702. }
  1703. }
  1704. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1705. s->mbintra_table[mb_xy]=1;
  1706. 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
  1707. uint8_t *dest_y, *dest_cb, *dest_cr;
  1708. int dct_linesize, dct_offset;
  1709. op_pixels_func (*op_pix)[4];
  1710. qpel_mc_func (*op_qpix)[16];
  1711. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1712. const int uvlinesize = s->current_picture.f.linesize[1];
  1713. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1714. const int block_size = 8;
  1715. /* avoid copy if macroblock skipped in last frame too */
  1716. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1717. if(!s->encoding){
  1718. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1719. if (s->mb_skipped) {
  1720. s->mb_skipped= 0;
  1721. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1722. *mbskip_ptr = 1;
  1723. } else if(!s->current_picture.f.reference) {
  1724. *mbskip_ptr = 1;
  1725. } else{
  1726. *mbskip_ptr = 0; /* not skipped */
  1727. }
  1728. }
  1729. dct_linesize = linesize << s->interlaced_dct;
  1730. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1731. if(readable){
  1732. dest_y= s->dest[0];
  1733. dest_cb= s->dest[1];
  1734. dest_cr= s->dest[2];
  1735. }else{
  1736. dest_y = s->b_scratchpad;
  1737. dest_cb= s->b_scratchpad+16*linesize;
  1738. dest_cr= s->b_scratchpad+32*linesize;
  1739. }
  1740. if (!s->mb_intra) {
  1741. /* motion handling */
  1742. /* decoding or more than one mb_type (MC was already done otherwise) */
  1743. if(!s->encoding){
  1744. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1745. if (s->mv_dir & MV_DIR_FORWARD) {
  1746. ff_thread_await_progress(&s->last_picture_ptr->f,
  1747. ff_MPV_lowest_referenced_row(s, 0),
  1748. 0);
  1749. }
  1750. if (s->mv_dir & MV_DIR_BACKWARD) {
  1751. ff_thread_await_progress(&s->next_picture_ptr->f,
  1752. ff_MPV_lowest_referenced_row(s, 1),
  1753. 0);
  1754. }
  1755. }
  1756. op_qpix= s->me.qpel_put;
  1757. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1758. op_pix = s->dsp.put_pixels_tab;
  1759. }else{
  1760. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1761. }
  1762. if (s->mv_dir & MV_DIR_FORWARD) {
  1763. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1764. op_pix = s->dsp.avg_pixels_tab;
  1765. op_qpix= s->me.qpel_avg;
  1766. }
  1767. if (s->mv_dir & MV_DIR_BACKWARD) {
  1768. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1769. }
  1770. }
  1771. /* skip dequant / idct if we are really late ;) */
  1772. if(s->avctx->skip_idct){
  1773. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1774. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1775. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1776. goto skip_idct;
  1777. }
  1778. /* add dct residue */
  1779. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1780. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1781. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1782. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1783. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1784. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1785. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1786. if (s->chroma_y_shift){
  1787. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1788. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1789. }else{
  1790. dct_linesize >>= 1;
  1791. dct_offset >>=1;
  1792. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1793. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1794. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1795. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1796. }
  1797. }
  1798. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1799. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1800. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1801. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1802. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1803. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1804. if(s->chroma_y_shift){//Chroma420
  1805. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1806. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1807. }else{
  1808. //chroma422
  1809. dct_linesize = uvlinesize << s->interlaced_dct;
  1810. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1811. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1812. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1813. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1814. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1815. if(!s->chroma_x_shift){//Chroma444
  1816. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1817. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1818. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1819. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1820. }
  1821. }
  1822. }//fi gray
  1823. }
  1824. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1825. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1826. }
  1827. } else {
  1828. /* dct only in intra block */
  1829. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1830. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1831. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1832. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1833. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1834. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1835. if(s->chroma_y_shift){
  1836. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1837. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1838. }else{
  1839. dct_offset >>=1;
  1840. dct_linesize >>=1;
  1841. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1842. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1843. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1844. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1845. }
  1846. }
  1847. }else{
  1848. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1849. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1850. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1851. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1852. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1853. if(s->chroma_y_shift){
  1854. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1855. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1856. }else{
  1857. dct_linesize = uvlinesize << s->interlaced_dct;
  1858. dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
  1859. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1860. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1861. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1862. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1863. if(!s->chroma_x_shift){//Chroma444
  1864. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1865. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  1866. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  1867. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  1868. }
  1869. }
  1870. }//gray
  1871. }
  1872. }
  1873. skip_idct:
  1874. if(!readable){
  1875. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1876. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1877. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1878. }
  1879. }
  1880. }
  1881. void ff_MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1882. #if !CONFIG_SMALL
  1883. if(s->out_format == FMT_MPEG1) {
  1884. MPV_decode_mb_internal(s, block, 1);
  1885. } else
  1886. #endif
  1887. MPV_decode_mb_internal(s, block, 0);
  1888. }
  1889. /**
  1890. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1891. */
  1892. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1893. const int field_pic= s->picture_structure != PICT_FRAME;
  1894. if(field_pic){
  1895. h <<= 1;
  1896. y <<= 1;
  1897. }
  1898. if (!s->avctx->hwaccel
  1899. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1900. && s->unrestricted_mv
  1901. && s->current_picture.f.reference
  1902. && !s->intra_only
  1903. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1904. int sides = 0, edge_h;
  1905. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1906. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1907. if (y==0) sides |= EDGE_TOP;
  1908. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  1909. edge_h= FFMIN(h, s->v_edge_pos - y);
  1910. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  1911. s->linesize, s->h_edge_pos, edge_h,
  1912. EDGE_WIDTH, EDGE_WIDTH, sides);
  1913. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  1914. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  1915. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  1916. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  1917. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  1918. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  1919. }
  1920. h= FFMIN(h, s->avctx->height - y);
  1921. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1922. if (s->avctx->draw_horiz_band) {
  1923. AVFrame *src;
  1924. int offset[AV_NUM_DATA_POINTERS];
  1925. int i;
  1926. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1927. src = &s->current_picture_ptr->f;
  1928. else if(s->last_picture_ptr)
  1929. src = &s->last_picture_ptr->f;
  1930. else
  1931. return;
  1932. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1933. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  1934. offset[i] = 0;
  1935. }else{
  1936. offset[0]= y * s->linesize;
  1937. offset[1]=
  1938. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1939. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1940. offset[i] = 0;
  1941. }
  1942. emms_c();
  1943. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1944. y, s->picture_structure, h);
  1945. }
  1946. }
  1947. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1948. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1949. const int uvlinesize = s->current_picture.f.linesize[1];
  1950. const int mb_size= 4;
  1951. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1952. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1953. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1954. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1955. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1956. 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;
  1957. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1958. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  1959. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1960. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1961. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1962. {
  1963. if(s->picture_structure==PICT_FRAME){
  1964. s->dest[0] += s->mb_y * linesize << mb_size;
  1965. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1966. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1967. }else{
  1968. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  1969. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1970. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1971. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  1972. }
  1973. }
  1974. }
  1975. void ff_mpeg_flush(AVCodecContext *avctx){
  1976. int i;
  1977. MpegEncContext *s = avctx->priv_data;
  1978. if(s==NULL || s->picture==NULL)
  1979. return;
  1980. for(i=0; i<s->picture_count; i++){
  1981. if (s->picture[i].f.data[0] &&
  1982. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  1983. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  1984. free_frame_buffer(s, &s->picture[i]);
  1985. }
  1986. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1987. s->mb_x= s->mb_y= 0;
  1988. s->closed_gop= 0;
  1989. s->parse_context.state= -1;
  1990. s->parse_context.frame_start_found= 0;
  1991. s->parse_context.overread= 0;
  1992. s->parse_context.overread_index= 0;
  1993. s->parse_context.index= 0;
  1994. s->parse_context.last_index= 0;
  1995. s->bitstream_buffer_size=0;
  1996. s->pp_time=0;
  1997. }
  1998. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1999. DCTELEM *block, int n, int qscale)
  2000. {
  2001. int i, level, nCoeffs;
  2002. const uint16_t *quant_matrix;
  2003. nCoeffs= s->block_last_index[n];
  2004. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2005. /* XXX: only mpeg1 */
  2006. quant_matrix = s->intra_matrix;
  2007. for(i=1;i<=nCoeffs;i++) {
  2008. int j= s->intra_scantable.permutated[i];
  2009. level = block[j];
  2010. if (level) {
  2011. if (level < 0) {
  2012. level = -level;
  2013. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2014. level = (level - 1) | 1;
  2015. level = -level;
  2016. } else {
  2017. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2018. level = (level - 1) | 1;
  2019. }
  2020. block[j] = level;
  2021. }
  2022. }
  2023. }
  2024. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2025. DCTELEM *block, int n, int qscale)
  2026. {
  2027. int i, level, nCoeffs;
  2028. const uint16_t *quant_matrix;
  2029. nCoeffs= s->block_last_index[n];
  2030. quant_matrix = s->inter_matrix;
  2031. for(i=0; i<=nCoeffs; i++) {
  2032. int j= s->intra_scantable.permutated[i];
  2033. level = block[j];
  2034. if (level) {
  2035. if (level < 0) {
  2036. level = -level;
  2037. level = (((level << 1) + 1) * qscale *
  2038. ((int) (quant_matrix[j]))) >> 4;
  2039. level = (level - 1) | 1;
  2040. level = -level;
  2041. } else {
  2042. level = (((level << 1) + 1) * qscale *
  2043. ((int) (quant_matrix[j]))) >> 4;
  2044. level = (level - 1) | 1;
  2045. }
  2046. block[j] = level;
  2047. }
  2048. }
  2049. }
  2050. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2051. DCTELEM *block, int n, int qscale)
  2052. {
  2053. int i, level, nCoeffs;
  2054. const uint16_t *quant_matrix;
  2055. if(s->alternate_scan) nCoeffs= 63;
  2056. else nCoeffs= s->block_last_index[n];
  2057. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2058. quant_matrix = s->intra_matrix;
  2059. for(i=1;i<=nCoeffs;i++) {
  2060. int j= s->intra_scantable.permutated[i];
  2061. level = block[j];
  2062. if (level) {
  2063. if (level < 0) {
  2064. level = -level;
  2065. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2066. level = -level;
  2067. } else {
  2068. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2069. }
  2070. block[j] = level;
  2071. }
  2072. }
  2073. }
  2074. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2075. DCTELEM *block, int n, int qscale)
  2076. {
  2077. int i, level, nCoeffs;
  2078. const uint16_t *quant_matrix;
  2079. int sum=-1;
  2080. if(s->alternate_scan) nCoeffs= 63;
  2081. else nCoeffs= s->block_last_index[n];
  2082. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2083. sum += block[0];
  2084. quant_matrix = s->intra_matrix;
  2085. for(i=1;i<=nCoeffs;i++) {
  2086. int j= s->intra_scantable.permutated[i];
  2087. level = block[j];
  2088. if (level) {
  2089. if (level < 0) {
  2090. level = -level;
  2091. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2092. level = -level;
  2093. } else {
  2094. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2095. }
  2096. block[j] = level;
  2097. sum+=level;
  2098. }
  2099. }
  2100. block[63]^=sum&1;
  2101. }
  2102. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2103. DCTELEM *block, int n, int qscale)
  2104. {
  2105. int i, level, nCoeffs;
  2106. const uint16_t *quant_matrix;
  2107. int sum=-1;
  2108. if(s->alternate_scan) nCoeffs= 63;
  2109. else nCoeffs= s->block_last_index[n];
  2110. quant_matrix = s->inter_matrix;
  2111. for(i=0; i<=nCoeffs; i++) {
  2112. int j= s->intra_scantable.permutated[i];
  2113. level = block[j];
  2114. if (level) {
  2115. if (level < 0) {
  2116. level = -level;
  2117. level = (((level << 1) + 1) * qscale *
  2118. ((int) (quant_matrix[j]))) >> 4;
  2119. level = -level;
  2120. } else {
  2121. level = (((level << 1) + 1) * qscale *
  2122. ((int) (quant_matrix[j]))) >> 4;
  2123. }
  2124. block[j] = level;
  2125. sum+=level;
  2126. }
  2127. }
  2128. block[63]^=sum&1;
  2129. }
  2130. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2131. DCTELEM *block, int n, int qscale)
  2132. {
  2133. int i, level, qmul, qadd;
  2134. int nCoeffs;
  2135. assert(s->block_last_index[n]>=0);
  2136. qmul = qscale << 1;
  2137. if (!s->h263_aic) {
  2138. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2139. qadd = (qscale - 1) | 1;
  2140. }else{
  2141. qadd = 0;
  2142. }
  2143. if(s->ac_pred)
  2144. nCoeffs=63;
  2145. else
  2146. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2147. for(i=1; i<=nCoeffs; i++) {
  2148. level = block[i];
  2149. if (level) {
  2150. if (level < 0) {
  2151. level = level * qmul - qadd;
  2152. } else {
  2153. level = level * qmul + qadd;
  2154. }
  2155. block[i] = level;
  2156. }
  2157. }
  2158. }
  2159. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2160. DCTELEM *block, int n, int qscale)
  2161. {
  2162. int i, level, qmul, qadd;
  2163. int nCoeffs;
  2164. assert(s->block_last_index[n]>=0);
  2165. qadd = (qscale - 1) | 1;
  2166. qmul = qscale << 1;
  2167. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2168. for(i=0; i<=nCoeffs; i++) {
  2169. level = block[i];
  2170. if (level) {
  2171. if (level < 0) {
  2172. level = level * qmul - qadd;
  2173. } else {
  2174. level = level * qmul + qadd;
  2175. }
  2176. block[i] = level;
  2177. }
  2178. }
  2179. }
  2180. /**
  2181. * set qscale and update qscale dependent variables.
  2182. */
  2183. void ff_set_qscale(MpegEncContext * s, int qscale)
  2184. {
  2185. if (qscale < 1)
  2186. qscale = 1;
  2187. else if (qscale > 31)
  2188. qscale = 31;
  2189. s->qscale = qscale;
  2190. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2191. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2192. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2193. }
  2194. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2195. {
  2196. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2197. ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);
  2198. }