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.

2543 lines
95KB

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