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.

2534 lines
94KB

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