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.

2545 lines
95KB

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