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.

2903 lines
109KB

  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. 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. MPV_common_init_mmx(s);
  167. #elif ARCH_ALPHA
  168. MPV_common_init_axp(s);
  169. #elif CONFIG_MLIB
  170. MPV_common_init_mlib(s);
  171. #elif HAVE_MMI
  172. MPV_common_init_mmi(s);
  173. #elif ARCH_ARM
  174. MPV_common_init_arm(s);
  175. #elif HAVE_ALTIVEC
  176. MPV_common_init_altivec(s);
  177. #elif ARCH_BFIN
  178. MPV_common_init_bfin(s);
  179. #endif
  180. /* load & permutate scantables
  181. * note: only wmv uses different ones
  182. */
  183. if (s->alternate_scan) {
  184. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  185. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  186. } else {
  187. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  188. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  189. }
  190. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  191. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  192. return 0;
  193. }
  194. void ff_copy_picture(Picture *dst, Picture *src)
  195. {
  196. *dst = *src;
  197. dst->f.type = FF_BUFFER_TYPE_COPY;
  198. }
  199. /**
  200. * Release a frame buffer
  201. */
  202. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  203. {
  204. /* Windows Media Image codecs allocate internal buffers with different
  205. * dimensions; ignore user defined callbacks for these
  206. */
  207. if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
  208. ff_thread_release_buffer(s->avctx, (AVFrame *) pic);
  209. else
  210. avcodec_default_release_buffer(s->avctx, (AVFrame *) pic);
  211. av_freep(&pic->f.hwaccel_picture_private);
  212. }
  213. /**
  214. * Allocate a frame buffer
  215. */
  216. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  217. {
  218. int r;
  219. if (s->avctx->hwaccel) {
  220. assert(!pic->f.hwaccel_picture_private);
  221. if (s->avctx->hwaccel->priv_data_size) {
  222. pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  223. if (!pic->f.hwaccel_picture_private) {
  224. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  225. return -1;
  226. }
  227. }
  228. }
  229. if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
  230. r = ff_thread_get_buffer(s->avctx, (AVFrame *) pic);
  231. else
  232. r = avcodec_default_get_buffer(s->avctx, (AVFrame *) pic);
  233. if (r < 0 || !pic->f.type || !pic->f.data[0]) {
  234. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
  235. r, pic->f.type, pic->f.data[0]);
  236. av_freep(&pic->f.hwaccel_picture_private);
  237. return -1;
  238. }
  239. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  240. s->uvlinesize != pic->f.linesize[1])) {
  241. av_log(s->avctx, AV_LOG_ERROR,
  242. "get_buffer() failed (stride changed)\n");
  243. free_frame_buffer(s, pic);
  244. return -1;
  245. }
  246. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  247. av_log(s->avctx, AV_LOG_ERROR,
  248. "get_buffer() failed (uv stride mismatch)\n");
  249. free_frame_buffer(s, pic);
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. /**
  255. * Allocate a Picture.
  256. * The pixels are allocated/set by calling get_buffer() if shared = 0
  257. */
  258. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  259. {
  260. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  261. // the + 1 is needed so memset(,,stride*height) does not sig11
  262. const int mb_array_size = s->mb_stride * s->mb_height;
  263. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  264. const int b4_array_size = s->b4_stride * s->mb_height * 4;
  265. int i;
  266. int r = -1;
  267. if (shared) {
  268. assert(pic->f.data[0]);
  269. assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
  270. pic->f.type = FF_BUFFER_TYPE_SHARED;
  271. } else {
  272. assert(!pic->f.data[0]);
  273. if (alloc_frame_buffer(s, pic) < 0)
  274. return -1;
  275. s->linesize = pic->f.linesize[0];
  276. s->uvlinesize = pic->f.linesize[1];
  277. }
  278. if (pic->f.qscale_table == NULL) {
  279. if (s->encoding) {
  280. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var,
  281. mb_array_size * sizeof(int16_t), fail)
  282. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var,
  283. mb_array_size * sizeof(int16_t), fail)
  284. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean,
  285. mb_array_size * sizeof(int8_t ), fail)
  286. }
  287. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table,
  288. mb_array_size * sizeof(uint8_t) + 2, fail)// the + 2 is for the slice end check
  289. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base,
  290. (big_mb_num + s->mb_stride) * sizeof(uint8_t),
  291. fail)
  292. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base,
  293. (big_mb_num + s->mb_stride) * sizeof(uint32_t),
  294. fail)
  295. pic->f.mb_type = pic->mb_type_base + 2 * s->mb_stride + 1;
  296. pic->f.qscale_table = pic->qscale_table_base + 2 * s->mb_stride + 1;
  297. if (s->out_format == FMT_H264) {
  298. for (i = 0; i < 2; i++) {
  299. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
  300. 2 * (b4_array_size + 4) * sizeof(int16_t),
  301. fail)
  302. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  303. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
  304. 4 * mb_array_size * sizeof(uint8_t), fail)
  305. }
  306. pic->f.motion_subsample_log2 = 2;
  307. } else if (s->out_format == FMT_H263 || s->encoding ||
  308. (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
  309. for (i = 0; i < 2; i++) {
  310. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
  311. 2 * (b8_array_size + 4) * sizeof(int16_t),
  312. fail)
  313. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  314. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
  315. 4 * mb_array_size * sizeof(uint8_t), fail)
  316. }
  317. pic->f.motion_subsample_log2 = 3;
  318. }
  319. if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  320. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
  321. 64 * mb_array_size * sizeof(DCTELEM) * 6, fail)
  322. }
  323. pic->f.qstride = s->mb_stride;
  324. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
  325. 1 * sizeof(AVPanScan), fail)
  326. }
  327. pic->owner2 = s;
  328. return 0;
  329. fail: // for the FF_ALLOCZ_OR_GOTO macro
  330. if (r >= 0)
  331. free_frame_buffer(s, pic);
  332. return -1;
  333. }
  334. /**
  335. * Deallocate a picture.
  336. */
  337. static void free_picture(MpegEncContext *s, Picture *pic)
  338. {
  339. int i;
  340. if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
  341. free_frame_buffer(s, pic);
  342. }
  343. av_freep(&pic->mb_var);
  344. av_freep(&pic->mc_mb_var);
  345. av_freep(&pic->mb_mean);
  346. av_freep(&pic->f.mbskip_table);
  347. av_freep(&pic->qscale_table_base);
  348. av_freep(&pic->mb_type_base);
  349. av_freep(&pic->f.dct_coeff);
  350. av_freep(&pic->f.pan_scan);
  351. pic->f.mb_type = NULL;
  352. for (i = 0; i < 2; i++) {
  353. av_freep(&pic->motion_val_base[i]);
  354. av_freep(&pic->f.ref_index[i]);
  355. }
  356. if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
  357. for (i = 0; i < 4; i++) {
  358. pic->f.base[i] =
  359. pic->f.data[i] = NULL;
  360. }
  361. pic->f.type = 0;
  362. }
  363. }
  364. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
  365. {
  366. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  367. int c_size = s->mb_stride * (s->mb_height + 1);
  368. int yc_size = y_size + 2 * c_size;
  369. int i;
  370. // edge emu needs blocksize + filter length - 1
  371. // (= 17x17 for halfpel / 21x21 for h264)
  372. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer,
  373. (s->width + 64) * 2 * 21 * 2, fail); // (width + edge + align)*interlaced*MBsize*tolerance
  374. // FIXME should be linesize instead of s->width * 2
  375. // but that is not known before get_buffer()
  376. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad,
  377. (s->width + 64) * 4 * 16 * 2 * sizeof(uint8_t), fail)
  378. s->me.temp = s->me.scratchpad;
  379. s->rd_scratchpad = s->me.scratchpad;
  380. s->b_scratchpad = s->me.scratchpad;
  381. s->obmc_scratchpad = s->me.scratchpad + 16;
  382. if (s->encoding) {
  383. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  384. ME_MAP_SIZE * sizeof(uint32_t), fail)
  385. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  386. ME_MAP_SIZE * sizeof(uint32_t), fail)
  387. if (s->avctx->noise_reduction) {
  388. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  389. 2 * 64 * sizeof(int), fail)
  390. }
  391. }
  392. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(DCTELEM), fail)
  393. s->block = s->blocks[0];
  394. for (i = 0; i < 12; i++) {
  395. s->pblocks[i] = &s->block[i];
  396. }
  397. if (s->out_format == FMT_H263) {
  398. /* ac values */
  399. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  400. yc_size * sizeof(int16_t) * 16, fail);
  401. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  402. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  403. s->ac_val[2] = s->ac_val[1] + c_size;
  404. }
  405. return 0;
  406. fail:
  407. return -1; // free() through MPV_common_end()
  408. }
  409. static void free_duplicate_context(MpegEncContext *s)
  410. {
  411. if (s == NULL)
  412. return;
  413. av_freep(&s->edge_emu_buffer);
  414. av_freep(&s->me.scratchpad);
  415. s->me.temp =
  416. s->rd_scratchpad =
  417. s->b_scratchpad =
  418. s->obmc_scratchpad = NULL;
  419. av_freep(&s->dct_error_sum);
  420. av_freep(&s->me.map);
  421. av_freep(&s->me.score_map);
  422. av_freep(&s->blocks);
  423. av_freep(&s->ac_val_base);
  424. s->block = NULL;
  425. }
  426. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  427. {
  428. #define COPY(a) bak->a = src->a
  429. COPY(edge_emu_buffer);
  430. COPY(me.scratchpad);
  431. COPY(me.temp);
  432. COPY(rd_scratchpad);
  433. COPY(b_scratchpad);
  434. COPY(obmc_scratchpad);
  435. COPY(me.map);
  436. COPY(me.score_map);
  437. COPY(blocks);
  438. COPY(block);
  439. COPY(start_mb_y);
  440. COPY(end_mb_y);
  441. COPY(me.map_generation);
  442. COPY(pb);
  443. COPY(dct_error_sum);
  444. COPY(dct_count[0]);
  445. COPY(dct_count[1]);
  446. COPY(ac_val_base);
  447. COPY(ac_val[0]);
  448. COPY(ac_val[1]);
  449. COPY(ac_val[2]);
  450. #undef COPY
  451. }
  452. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  453. {
  454. MpegEncContext bak;
  455. int i;
  456. // FIXME copy only needed parts
  457. // START_TIMER
  458. backup_duplicate_context(&bak, dst);
  459. memcpy(dst, src, sizeof(MpegEncContext));
  460. backup_duplicate_context(dst, &bak);
  461. for (i = 0; i < 12; i++) {
  462. dst->pblocks[i] = &dst->block[i];
  463. }
  464. // STOP_TIMER("update_duplicate_context")
  465. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  466. }
  467. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  468. const AVCodecContext *src)
  469. {
  470. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  471. if (dst == src || !s1->context_initialized)
  472. return 0;
  473. // FIXME can parameters change on I-frames?
  474. // in that case dst may need a reinit
  475. if (!s->context_initialized) {
  476. memcpy(s, s1, sizeof(MpegEncContext));
  477. s->avctx = dst;
  478. s->picture_range_start += MAX_PICTURE_COUNT;
  479. s->picture_range_end += MAX_PICTURE_COUNT;
  480. s->bitstream_buffer = NULL;
  481. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  482. MPV_common_init(s);
  483. }
  484. s->avctx->coded_height = s1->avctx->coded_height;
  485. s->avctx->coded_width = s1->avctx->coded_width;
  486. s->avctx->width = s1->avctx->width;
  487. s->avctx->height = s1->avctx->height;
  488. s->coded_picture_number = s1->coded_picture_number;
  489. s->picture_number = s1->picture_number;
  490. s->input_picture_number = s1->input_picture_number;
  491. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  492. memcpy(&s->last_picture, &s1->last_picture,
  493. (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
  494. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  495. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  496. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  497. // Error/bug resilience
  498. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  499. s->workaround_bugs = s1->workaround_bugs;
  500. // MPEG4 timing info
  501. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  502. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  503. // B-frame info
  504. s->max_b_frames = s1->max_b_frames;
  505. s->low_delay = s1->low_delay;
  506. s->dropable = s1->dropable;
  507. // DivX handling (doesn't work)
  508. s->divx_packed = s1->divx_packed;
  509. if (s1->bitstream_buffer) {
  510. if (s1->bitstream_buffer_size +
  511. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  512. av_fast_malloc(&s->bitstream_buffer,
  513. &s->allocated_bitstream_buffer_size,
  514. s1->allocated_bitstream_buffer_size);
  515. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  516. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  517. s1->bitstream_buffer_size);
  518. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  519. FF_INPUT_BUFFER_PADDING_SIZE);
  520. }
  521. // MPEG2/interlacing info
  522. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  523. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  524. if (!s1->first_field) {
  525. s->last_pict_type = s1->pict_type;
  526. if (s1->current_picture_ptr)
  527. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  528. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  529. s->last_non_b_pict_type = s1->pict_type;
  530. }
  531. }
  532. return 0;
  533. }
  534. /**
  535. * Set the given MpegEncContext to common defaults
  536. * (same for encoding and decoding).
  537. * The changed fields will not depend upon the
  538. * prior state of the MpegEncContext.
  539. */
  540. void MPV_common_defaults(MpegEncContext *s)
  541. {
  542. s->y_dc_scale_table =
  543. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  544. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  545. s->progressive_frame = 1;
  546. s->progressive_sequence = 1;
  547. s->picture_structure = PICT_FRAME;
  548. s->coded_picture_number = 0;
  549. s->picture_number = 0;
  550. s->input_picture_number = 0;
  551. s->picture_in_gop_number = 0;
  552. s->f_code = 1;
  553. s->b_code = 1;
  554. s->picture_range_start = 0;
  555. s->picture_range_end = MAX_PICTURE_COUNT;
  556. s->slice_context_count = 1;
  557. }
  558. /**
  559. * Set the given MpegEncContext to defaults for decoding.
  560. * the changed fields will not depend upon
  561. * the prior state of the MpegEncContext.
  562. */
  563. void MPV_decode_defaults(MpegEncContext *s)
  564. {
  565. MPV_common_defaults(s);
  566. }
  567. /**
  568. * init common structure for both encoder and decoder.
  569. * this assumes that some variables like width/height are already set
  570. */
  571. av_cold int MPV_common_init(MpegEncContext *s)
  572. {
  573. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  574. int nb_slices = (HAVE_THREADS &&
  575. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  576. s->avctx->thread_count : 1;
  577. if (s->encoding && s->avctx->slices)
  578. nb_slices = s->avctx->slices;
  579. if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  580. s->mb_height = (s->height + 31) / 32 * 2;
  581. else if (s->codec_id != CODEC_ID_H264)
  582. s->mb_height = (s->height + 15) / 16;
  583. if (s->avctx->pix_fmt == PIX_FMT_NONE) {
  584. av_log(s->avctx, AV_LOG_ERROR,
  585. "decoding to PIX_FMT_NONE is not supported.\n");
  586. return -1;
  587. }
  588. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  589. int max_slices;
  590. if (s->mb_height)
  591. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  592. else
  593. max_slices = MAX_THREADS;
  594. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  595. " reducing to %d\n", nb_slices, max_slices);
  596. nb_slices = max_slices;
  597. }
  598. if ((s->width || s->height) &&
  599. av_image_check_size(s->width, s->height, 0, s->avctx))
  600. return -1;
  601. ff_dct_common_init(s);
  602. s->flags = s->avctx->flags;
  603. s->flags2 = s->avctx->flags2;
  604. if (s->width && s->height) {
  605. s->mb_width = (s->width + 15) / 16;
  606. s->mb_stride = s->mb_width + 1;
  607. s->b8_stride = s->mb_width * 2 + 1;
  608. s->b4_stride = s->mb_width * 4 + 1;
  609. mb_array_size = s->mb_height * s->mb_stride;
  610. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  611. /* set chroma shifts */
  612. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
  613. &s->chroma_y_shift);
  614. /* set default edge pos, will be overriden
  615. * in decode_header if needed */
  616. s->h_edge_pos = s->mb_width * 16;
  617. s->v_edge_pos = s->mb_height * 16;
  618. s->mb_num = s->mb_width * s->mb_height;
  619. s->block_wrap[0] =
  620. s->block_wrap[1] =
  621. s->block_wrap[2] =
  622. s->block_wrap[3] = s->b8_stride;
  623. s->block_wrap[4] =
  624. s->block_wrap[5] = s->mb_stride;
  625. y_size = s->b8_stride * (2 * s->mb_height + 1);
  626. c_size = s->mb_stride * (s->mb_height + 1);
  627. yc_size = y_size + 2 * c_size;
  628. /* convert fourcc to upper case */
  629. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  630. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  631. s->avctx->coded_frame = (AVFrame *)&s->current_picture;
  632. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  633. fail); // error ressilience code looks cleaner with this
  634. for (y = 0; y < s->mb_height; y++)
  635. for (x = 0; x < s->mb_width; x++)
  636. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  637. s->mb_index2xy[s->mb_height * s->mb_width] =
  638. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  639. if (s->encoding) {
  640. /* Allocate MV tables */
  641. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  642. mv_table_size * 2 * sizeof(int16_t), fail);
  643. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  644. mv_table_size * 2 * sizeof(int16_t), fail);
  645. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  646. mv_table_size * 2 * sizeof(int16_t), fail);
  647. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  648. mv_table_size * 2 * sizeof(int16_t), fail);
  649. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  650. mv_table_size * 2 * sizeof(int16_t), fail);
  651. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  652. mv_table_size * 2 * sizeof(int16_t), fail);
  653. s->p_mv_table = s->p_mv_table_base +
  654. s->mb_stride + 1;
  655. s->b_forw_mv_table = s->b_forw_mv_table_base +
  656. s->mb_stride + 1;
  657. s->b_back_mv_table = s->b_back_mv_table_base +
  658. s->mb_stride + 1;
  659. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  660. s->mb_stride + 1;
  661. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  662. s->mb_stride + 1;
  663. s->b_direct_mv_table = s->b_direct_mv_table_base +
  664. s->mb_stride + 1;
  665. if (s->msmpeg4_version) {
  666. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  667. 2 * 2 * (MAX_LEVEL + 1) *
  668. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  669. }
  670. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  671. /* Allocate MB type table */
  672. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  673. sizeof(uint16_t), fail); // needed for encoding
  674. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  675. sizeof(int), fail);
  676. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  677. 64 * 32 * sizeof(int), fail);
  678. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  679. 64 * 32 * sizeof(int), fail);
  680. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  681. 64 * 32 * 2 * sizeof(uint16_t), fail);
  682. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  683. 64 * 32 * 2 * sizeof(uint16_t), fail);
  684. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  685. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  686. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  687. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  688. if (s->avctx->noise_reduction) {
  689. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  690. 2 * 64 * sizeof(uint16_t), fail);
  691. }
  692. }
  693. }
  694. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  695. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  696. s->picture_count * sizeof(Picture), fail);
  697. for (i = 0; i < s->picture_count; i++) {
  698. avcodec_get_frame_defaults((AVFrame *) &s->picture[i]);
  699. }
  700. if (s->width && s->height) {
  701. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
  702. mb_array_size * sizeof(uint8_t), fail);
  703. if (s->codec_id == CODEC_ID_MPEG4 ||
  704. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  705. /* interlaced direct mode decoding tables */
  706. for (i = 0; i < 2; i++) {
  707. int j, k;
  708. for (j = 0; j < 2; j++) {
  709. for (k = 0; k < 2; k++) {
  710. FF_ALLOCZ_OR_GOTO(s->avctx,
  711. s->b_field_mv_table_base[i][j][k],
  712. mv_table_size * 2 * sizeof(int16_t),
  713. fail);
  714. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  715. s->mb_stride + 1;
  716. }
  717. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  718. mb_array_size * 2 * sizeof(uint8_t),
  719. fail);
  720. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  721. mv_table_size * 2 * sizeof(int16_t),
  722. fail);
  723. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  724. + s->mb_stride + 1;
  725. }
  726. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  727. mb_array_size * 2 * sizeof(uint8_t),
  728. fail);
  729. }
  730. }
  731. if (s->out_format == FMT_H263) {
  732. /* cbp values */
  733. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  734. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  735. /* cbp, ac_pred, pred_dir */
  736. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  737. mb_array_size * sizeof(uint8_t), fail);
  738. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  739. mb_array_size * sizeof(uint8_t), fail);
  740. }
  741. if (s->h263_pred || s->h263_plus || !s->encoding) {
  742. /* dc values */
  743. // MN: we need these for error resilience of intra-frames
  744. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  745. yc_size * sizeof(int16_t), fail);
  746. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  747. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  748. s->dc_val[2] = s->dc_val[1] + c_size;
  749. for (i = 0; i < yc_size; i++)
  750. s->dc_val_base[i] = 1024;
  751. }
  752. /* which mb is a intra block */
  753. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  754. memset(s->mbintra_table, 1, mb_array_size);
  755. /* init macroblock skip table */
  756. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  757. // Note the + 1 is for a quicker mpeg4 slice_end detection
  758. s->parse_context.state = -1;
  759. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  760. s->avctx->debug_mv) {
  761. s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
  762. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  763. s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
  764. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  765. s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
  766. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  767. }
  768. }
  769. s->context_initialized = 1;
  770. s->thread_context[0] = s;
  771. if (s->width && s->height) {
  772. if (nb_slices > 1) {
  773. for (i = 1; i < nb_slices; i++) {
  774. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  775. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  776. }
  777. for (i = 0; i < nb_slices; i++) {
  778. if (init_duplicate_context(s->thread_context[i], s) < 0)
  779. goto fail;
  780. s->thread_context[i]->start_mb_y =
  781. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  782. s->thread_context[i]->end_mb_y =
  783. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  784. }
  785. } else {
  786. if (init_duplicate_context(s, s) < 0)
  787. goto fail;
  788. s->start_mb_y = 0;
  789. s->end_mb_y = s->mb_height;
  790. }
  791. s->slice_context_count = nb_slices;
  792. }
  793. return 0;
  794. fail:
  795. MPV_common_end(s);
  796. return -1;
  797. }
  798. /* init common structure for both encoder and decoder */
  799. void MPV_common_end(MpegEncContext *s)
  800. {
  801. int i, j, k;
  802. if (s->slice_context_count > 1) {
  803. for (i = 0; i < s->slice_context_count; i++) {
  804. free_duplicate_context(s->thread_context[i]);
  805. }
  806. for (i = 1; i < s->slice_context_count; i++) {
  807. av_freep(&s->thread_context[i]);
  808. }
  809. s->slice_context_count = 1;
  810. } else free_duplicate_context(s);
  811. av_freep(&s->parse_context.buffer);
  812. s->parse_context.buffer_size = 0;
  813. av_freep(&s->mb_type);
  814. av_freep(&s->p_mv_table_base);
  815. av_freep(&s->b_forw_mv_table_base);
  816. av_freep(&s->b_back_mv_table_base);
  817. av_freep(&s->b_bidir_forw_mv_table_base);
  818. av_freep(&s->b_bidir_back_mv_table_base);
  819. av_freep(&s->b_direct_mv_table_base);
  820. s->p_mv_table = NULL;
  821. s->b_forw_mv_table = NULL;
  822. s->b_back_mv_table = NULL;
  823. s->b_bidir_forw_mv_table = NULL;
  824. s->b_bidir_back_mv_table = NULL;
  825. s->b_direct_mv_table = NULL;
  826. for (i = 0; i < 2; i++) {
  827. for (j = 0; j < 2; j++) {
  828. for (k = 0; k < 2; k++) {
  829. av_freep(&s->b_field_mv_table_base[i][j][k]);
  830. s->b_field_mv_table[i][j][k] = NULL;
  831. }
  832. av_freep(&s->b_field_select_table[i][j]);
  833. av_freep(&s->p_field_mv_table_base[i][j]);
  834. s->p_field_mv_table[i][j] = NULL;
  835. }
  836. av_freep(&s->p_field_select_table[i]);
  837. }
  838. av_freep(&s->dc_val_base);
  839. av_freep(&s->coded_block_base);
  840. av_freep(&s->mbintra_table);
  841. av_freep(&s->cbp_table);
  842. av_freep(&s->pred_dir_table);
  843. av_freep(&s->mbskip_table);
  844. av_freep(&s->bitstream_buffer);
  845. s->allocated_bitstream_buffer_size = 0;
  846. av_freep(&s->avctx->stats_out);
  847. av_freep(&s->ac_stats);
  848. av_freep(&s->error_status_table);
  849. av_freep(&s->mb_index2xy);
  850. av_freep(&s->lambda_table);
  851. av_freep(&s->q_intra_matrix);
  852. av_freep(&s->q_inter_matrix);
  853. av_freep(&s->q_intra_matrix16);
  854. av_freep(&s->q_inter_matrix16);
  855. av_freep(&s->input_picture);
  856. av_freep(&s->reordered_input_picture);
  857. av_freep(&s->dct_offset);
  858. if (s->picture && !s->avctx->internal->is_copy) {
  859. for (i = 0; i < s->picture_count; i++) {
  860. free_picture(s, &s->picture[i]);
  861. }
  862. }
  863. av_freep(&s->picture);
  864. s->context_initialized = 0;
  865. s->last_picture_ptr =
  866. s->next_picture_ptr =
  867. s->current_picture_ptr = NULL;
  868. s->linesize = s->uvlinesize = 0;
  869. for (i = 0; i < 3; i++)
  870. av_freep(&s->visualization_buffer[i]);
  871. if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
  872. avcodec_default_free_buffers(s->avctx);
  873. }
  874. void init_rl(RLTable *rl,
  875. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  876. {
  877. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  878. uint8_t index_run[MAX_RUN + 1];
  879. int last, run, level, start, end, i;
  880. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  881. if (static_store && rl->max_level[0])
  882. return;
  883. /* compute max_level[], max_run[] and index_run[] */
  884. for (last = 0; last < 2; last++) {
  885. if (last == 0) {
  886. start = 0;
  887. end = rl->last;
  888. } else {
  889. start = rl->last;
  890. end = rl->n;
  891. }
  892. memset(max_level, 0, MAX_RUN + 1);
  893. memset(max_run, 0, MAX_LEVEL + 1);
  894. memset(index_run, rl->n, MAX_RUN + 1);
  895. for (i = start; i < end; i++) {
  896. run = rl->table_run[i];
  897. level = rl->table_level[i];
  898. if (index_run[run] == rl->n)
  899. index_run[run] = i;
  900. if (level > max_level[run])
  901. max_level[run] = level;
  902. if (run > max_run[level])
  903. max_run[level] = run;
  904. }
  905. if (static_store)
  906. rl->max_level[last] = static_store[last];
  907. else
  908. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  909. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  910. if (static_store)
  911. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  912. else
  913. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  914. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  915. if (static_store)
  916. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  917. else
  918. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  919. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  920. }
  921. }
  922. void init_vlc_rl(RLTable *rl)
  923. {
  924. int i, q;
  925. for (q = 0; q < 32; q++) {
  926. int qmul = q * 2;
  927. int qadd = (q - 1) | 1;
  928. if (q == 0) {
  929. qmul = 1;
  930. qadd = 0;
  931. }
  932. for (i = 0; i < rl->vlc.table_size; i++) {
  933. int code = rl->vlc.table[i][0];
  934. int len = rl->vlc.table[i][1];
  935. int level, run;
  936. if (len == 0) { // illegal code
  937. run = 66;
  938. level = MAX_LEVEL;
  939. } else if (len < 0) { // more bits needed
  940. run = 0;
  941. level = code;
  942. } else {
  943. if (code == rl->n) { // esc
  944. run = 66;
  945. level = 0;
  946. } else {
  947. run = rl->table_run[code] + 1;
  948. level = rl->table_level[code] * qmul + qadd;
  949. if (code >= rl->last) run += 192;
  950. }
  951. }
  952. rl->rl_vlc[q][i].len = len;
  953. rl->rl_vlc[q][i].level = level;
  954. rl->rl_vlc[q][i].run = run;
  955. }
  956. }
  957. }
  958. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  959. {
  960. int i;
  961. /* release non reference frames */
  962. for (i = 0; i < s->picture_count; i++) {
  963. if (s->picture[i].f.data[0] && !s->picture[i].f.reference &&
  964. (!s->picture[i].owner2 || s->picture[i].owner2 == s) &&
  965. (remove_current || &s->picture[i] != s->current_picture_ptr)
  966. /* && s->picture[i].type!= FF_BUFFER_TYPE_SHARED */) {
  967. free_frame_buffer(s, &s->picture[i]);
  968. }
  969. }
  970. }
  971. int ff_find_unused_picture(MpegEncContext *s, int shared)
  972. {
  973. int i;
  974. if (shared) {
  975. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  976. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
  977. return i;
  978. }
  979. } else {
  980. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  981. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type != 0)
  982. return i; // FIXME
  983. }
  984. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  985. if (s->picture[i].f.data[0] == NULL)
  986. return i;
  987. }
  988. }
  989. return AVERROR_INVALIDDATA;
  990. }
  991. static void update_noise_reduction(MpegEncContext *s)
  992. {
  993. int intra, i;
  994. for (intra = 0; intra < 2; intra++) {
  995. if (s->dct_count[intra] > (1 << 16)) {
  996. for (i = 0; i < 64; i++) {
  997. s->dct_error_sum[intra][i] >>= 1;
  998. }
  999. s->dct_count[intra] >>= 1;
  1000. }
  1001. for (i = 0; i < 64; i++) {
  1002. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1003. s->dct_count[intra] +
  1004. s->dct_error_sum[intra][i] / 2) /
  1005. (s->dct_error_sum[intra][i] + 1);
  1006. }
  1007. }
  1008. }
  1009. /**
  1010. * generic function for encode/decode called after coding/decoding
  1011. * the header and before a frame is coded/decoded.
  1012. */
  1013. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1014. {
  1015. int i;
  1016. Picture *pic;
  1017. s->mb_skipped = 0;
  1018. assert(s->last_picture_ptr == NULL || s->out_format != FMT_H264 ||
  1019. s->codec_id == CODEC_ID_SVQ3);
  1020. /* mark & release old frames */
  1021. if (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3) {
  1022. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1023. s->last_picture_ptr != s->next_picture_ptr &&
  1024. s->last_picture_ptr->f.data[0]) {
  1025. if (s->last_picture_ptr->owner2 == s)
  1026. free_frame_buffer(s, s->last_picture_ptr);
  1027. }
  1028. /* release forgotten pictures */
  1029. /* if (mpeg124/h263) */
  1030. if (!s->encoding) {
  1031. for (i = 0; i < s->picture_count; i++) {
  1032. if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
  1033. &s->picture[i] != s->last_picture_ptr &&
  1034. &s->picture[i] != s->next_picture_ptr &&
  1035. s->picture[i].f.reference) {
  1036. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1037. av_log(avctx, AV_LOG_ERROR,
  1038. "releasing zombie picture\n");
  1039. free_frame_buffer(s, &s->picture[i]);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. if (!s->encoding) {
  1045. ff_release_unused_pictures(s, 1);
  1046. if (s->current_picture_ptr &&
  1047. s->current_picture_ptr->f.data[0] == NULL) {
  1048. // we already have a unused image
  1049. // (maybe it was set before reading the header)
  1050. pic = s->current_picture_ptr;
  1051. } else {
  1052. i = ff_find_unused_picture(s, 0);
  1053. pic = &s->picture[i];
  1054. }
  1055. pic->f.reference = 0;
  1056. if (!s->dropable) {
  1057. if (s->codec_id == CODEC_ID_H264)
  1058. pic->f.reference = s->picture_structure;
  1059. else if (s->pict_type != AV_PICTURE_TYPE_B)
  1060. pic->f.reference = 3;
  1061. }
  1062. pic->f.coded_picture_number = s->coded_picture_number++;
  1063. if (ff_alloc_picture(s, pic, 0) < 0)
  1064. return -1;
  1065. s->current_picture_ptr = pic;
  1066. // FIXME use only the vars from current_pic
  1067. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1068. if (s->codec_id == CODEC_ID_MPEG1VIDEO ||
  1069. s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1070. if (s->picture_structure != PICT_FRAME)
  1071. s->current_picture_ptr->f.top_field_first =
  1072. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1073. }
  1074. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1075. !s->progressive_sequence;
  1076. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1077. }
  1078. s->current_picture_ptr->f.pict_type = s->pict_type;
  1079. // if (s->flags && CODEC_FLAG_QSCALE)
  1080. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1081. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1082. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  1083. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1084. s->last_picture_ptr = s->next_picture_ptr;
  1085. if (!s->dropable)
  1086. s->next_picture_ptr = s->current_picture_ptr;
  1087. }
  1088. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1089. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1090. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1091. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1092. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1093. s->pict_type, s->dropable); */
  1094. if (s->codec_id != CODEC_ID_H264) {
  1095. if ((s->last_picture_ptr == NULL ||
  1096. s->last_picture_ptr->f.data[0] == NULL) &&
  1097. (s->pict_type != AV_PICTURE_TYPE_I ||
  1098. s->picture_structure != PICT_FRAME)) {
  1099. if (s->pict_type != AV_PICTURE_TYPE_I)
  1100. av_log(avctx, AV_LOG_ERROR,
  1101. "warning: first frame is no keyframe\n");
  1102. else if (s->picture_structure != PICT_FRAME)
  1103. av_log(avctx, AV_LOG_INFO,
  1104. "allocate dummy last picture for field based first keyframe\n");
  1105. /* Allocate a dummy frame */
  1106. i = ff_find_unused_picture(s, 0);
  1107. s->last_picture_ptr = &s->picture[i];
  1108. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  1109. return -1;
  1110. ff_thread_report_progress((AVFrame *) s->last_picture_ptr,
  1111. INT_MAX, 0);
  1112. ff_thread_report_progress((AVFrame *) s->last_picture_ptr,
  1113. INT_MAX, 1);
  1114. }
  1115. if ((s->next_picture_ptr == NULL ||
  1116. s->next_picture_ptr->f.data[0] == NULL) &&
  1117. s->pict_type == AV_PICTURE_TYPE_B) {
  1118. /* Allocate a dummy frame */
  1119. i = ff_find_unused_picture(s, 0);
  1120. s->next_picture_ptr = &s->picture[i];
  1121. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  1122. return -1;
  1123. ff_thread_report_progress((AVFrame *) s->next_picture_ptr,
  1124. INT_MAX, 0);
  1125. ff_thread_report_progress((AVFrame *) s->next_picture_ptr,
  1126. INT_MAX, 1);
  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 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 = (AVFrame *) s->current_picture_ptr;
  1239. if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
  1240. ff_thread_report_progress((AVFrame *) s->current_picture_ptr,
  1241. s->mb_height - 1, 0);
  1242. }
  1243. }
  1244. /**
  1245. * Draw a line from (ex, ey) -> (sx, sy).
  1246. * @param w width of the image
  1247. * @param h height of the image
  1248. * @param stride stride/linesize of the image
  1249. * @param color color of the arrow
  1250. */
  1251. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1252. int w, int h, int stride, int color)
  1253. {
  1254. int x, y, fr, f;
  1255. sx = av_clip(sx, 0, w - 1);
  1256. sy = av_clip(sy, 0, h - 1);
  1257. ex = av_clip(ex, 0, w - 1);
  1258. ey = av_clip(ey, 0, h - 1);
  1259. buf[sy * stride + sx] += color;
  1260. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1261. if (sx > ex) {
  1262. FFSWAP(int, sx, ex);
  1263. FFSWAP(int, sy, ey);
  1264. }
  1265. buf += sx + sy * stride;
  1266. ex -= sx;
  1267. f = ((ey - sy) << 16) / ex;
  1268. for (x = 0; x = ex; x++) {
  1269. y = (x * f) >> 16;
  1270. fr = (x * f) & 0xFFFF;
  1271. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1272. buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1273. }
  1274. } else {
  1275. if (sy > ey) {
  1276. FFSWAP(int, sx, ex);
  1277. FFSWAP(int, sy, ey);
  1278. }
  1279. buf += sx + sy * stride;
  1280. ey -= sy;
  1281. if (ey)
  1282. f = ((ex - sx) << 16) / ey;
  1283. else
  1284. f = 0;
  1285. for (y = 0; y = ey; y++) {
  1286. x = (y * f) >> 16;
  1287. fr = (y * f) & 0xFFFF;
  1288. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1289. buf[y * stride + x + 1] += (color * fr ) >> 16;
  1290. }
  1291. }
  1292. }
  1293. /**
  1294. * Draw an arrow from (ex, ey) -> (sx, sy).
  1295. * @param w width of the image
  1296. * @param h height of the image
  1297. * @param stride stride/linesize of the image
  1298. * @param color color of the arrow
  1299. */
  1300. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1301. int ey, int w, int h, int stride, int color)
  1302. {
  1303. int dx,dy;
  1304. sx = av_clip(sx, -100, w + 100);
  1305. sy = av_clip(sy, -100, h + 100);
  1306. ex = av_clip(ex, -100, w + 100);
  1307. ey = av_clip(ey, -100, h + 100);
  1308. dx = ex - sx;
  1309. dy = ey - sy;
  1310. if (dx * dx + dy * dy > 3 * 3) {
  1311. int rx = dx + dy;
  1312. int ry = -dx + dy;
  1313. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1314. // FIXME subpixel accuracy
  1315. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1316. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1317. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1318. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1319. }
  1320. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1321. }
  1322. /**
  1323. * Print debugging info for the given picture.
  1324. */
  1325. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
  1326. {
  1327. if (s->avctx->hwaccel || !pict || !pict->mb_type)
  1328. return;
  1329. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1330. int x,y;
  1331. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1332. switch (pict->pict_type) {
  1333. case AV_PICTURE_TYPE_I:
  1334. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1335. break;
  1336. case AV_PICTURE_TYPE_P:
  1337. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1338. break;
  1339. case AV_PICTURE_TYPE_B:
  1340. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1341. break;
  1342. case AV_PICTURE_TYPE_S:
  1343. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1344. break;
  1345. case AV_PICTURE_TYPE_SI:
  1346. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1347. break;
  1348. case AV_PICTURE_TYPE_SP:
  1349. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1350. break;
  1351. }
  1352. for (y = 0; y < s->mb_height; y++) {
  1353. for (x = 0; x < s->mb_width; x++) {
  1354. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1355. int count = s->mbskip_table[x + y * s->mb_stride];
  1356. if (count > 9)
  1357. count = 9;
  1358. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1359. }
  1360. if (s->avctx->debug & FF_DEBUG_QP) {
  1361. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1362. pict->qscale_table[x + y * s->mb_stride]);
  1363. }
  1364. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1365. int mb_type = pict->mb_type[x + y * s->mb_stride];
  1366. // Type & MV direction
  1367. if (IS_PCM(mb_type))
  1368. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1369. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1370. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1371. else if (IS_INTRA4x4(mb_type))
  1372. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1373. else if (IS_INTRA16x16(mb_type))
  1374. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1375. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1376. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1377. else if (IS_DIRECT(mb_type))
  1378. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1379. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1380. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1381. else if (IS_GMC(mb_type))
  1382. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1383. else if (IS_SKIP(mb_type))
  1384. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1385. else if (!USES_LIST(mb_type, 1))
  1386. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1387. else if (!USES_LIST(mb_type, 0))
  1388. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1389. else {
  1390. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1391. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1392. }
  1393. // segmentation
  1394. if (IS_8X8(mb_type))
  1395. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1396. else if (IS_16X8(mb_type))
  1397. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1398. else if (IS_8X16(mb_type))
  1399. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1400. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1401. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1402. else
  1403. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1404. if (IS_INTERLACED(mb_type))
  1405. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1406. else
  1407. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1408. }
  1409. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1410. }
  1411. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1412. }
  1413. }
  1414. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1415. (s->avctx->debug_mv)) {
  1416. const int shift = 1 + s->quarter_sample;
  1417. int mb_y;
  1418. uint8_t *ptr;
  1419. int i;
  1420. int h_chroma_shift, v_chroma_shift, block_height;
  1421. const int width = s->avctx->width;
  1422. const int height = s->avctx->height;
  1423. const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
  1424. const int mv_stride = (s->mb_width << mv_sample_log2) +
  1425. (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1426. s->low_delay = 0; // needed to see the vectors without trashing the buffers
  1427. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1428. &h_chroma_shift, &v_chroma_shift);
  1429. for (i = 0; i < 3; i++) {
  1430. memcpy(s->visualization_buffer[i], pict->data[i],
  1431. (i == 0) ? pict->linesize[i] * height:
  1432. pict->linesize[i] * height >> v_chroma_shift);
  1433. pict->data[i] = s->visualization_buffer[i];
  1434. }
  1435. pict->type = FF_BUFFER_TYPE_COPY;
  1436. ptr = pict->data[0];
  1437. block_height = 16 >> v_chroma_shift;
  1438. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1439. int mb_x;
  1440. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1441. const int mb_index = mb_x + mb_y * s->mb_stride;
  1442. if ((s->avctx->debug_mv) && pict->motion_val) {
  1443. int type;
  1444. for (type = 0; type < 3; type++) {
  1445. int direction = 0;
  1446. switch (type) {
  1447. case 0:
  1448. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1449. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1450. continue;
  1451. direction = 0;
  1452. break;
  1453. case 1:
  1454. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1455. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1456. continue;
  1457. direction = 0;
  1458. break;
  1459. case 2:
  1460. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1461. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1462. continue;
  1463. direction = 1;
  1464. break;
  1465. }
  1466. if (!USES_LIST(pict->mb_type[mb_index], direction))
  1467. continue;
  1468. if (IS_8X8(pict->mb_type[mb_index])) {
  1469. int i;
  1470. for (i = 0; i < 4; i++) {
  1471. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1472. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1473. int xy = (mb_x * 2 + (i & 1) +
  1474. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1475. int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
  1476. int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
  1477. draw_arrow(ptr, sx, sy, mx, my, width,
  1478. height, s->linesize, 100);
  1479. }
  1480. } else if (IS_16X8(pict->mb_type[mb_index])) {
  1481. int i;
  1482. for (i = 0; i < 2; i++) {
  1483. int sx = mb_x * 16 + 8;
  1484. int sy = mb_y * 16 + 4 + 8 * i;
  1485. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1486. int mx = (pict->motion_val[direction][xy][0] >> shift);
  1487. int my = (pict->motion_val[direction][xy][1] >> shift);
  1488. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1489. my *= 2;
  1490. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1491. height, s->linesize, 100);
  1492. }
  1493. } else if (IS_8X16(pict->mb_type[mb_index])) {
  1494. int i;
  1495. for (i = 0; i < 2; i++) {
  1496. int sx = mb_x * 16 + 4 + 8 * i;
  1497. int sy = mb_y * 16 + 8;
  1498. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1499. int mx = pict->motion_val[direction][xy][0] >> shift;
  1500. int my = pict->motion_val[direction][xy][1] >> shift;
  1501. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1502. my *= 2;
  1503. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1504. height, s->linesize, 100);
  1505. }
  1506. } else {
  1507. int sx = mb_x * 16 + 8;
  1508. int sy = mb_y * 16 + 8;
  1509. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1510. int mx = pict->motion_val[direction][xy][0] >> shift + sx;
  1511. int my = pict->motion_val[direction][xy][1] >> shift + sy;
  1512. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1513. }
  1514. }
  1515. }
  1516. if ((s->avctx->debug & FF_DEBUG_VIS_QP) && pict->motion_val) {
  1517. uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
  1518. 0x0101010101010101ULL;
  1519. int y;
  1520. for (y = 0; y < block_height; y++) {
  1521. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1522. (block_height * mb_y + y) *
  1523. pict->linesize[1]) = c;
  1524. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1525. (block_height * mb_y + y) *
  1526. pict->linesize[2]) = c;
  1527. }
  1528. }
  1529. if ((s->avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1530. pict->motion_val) {
  1531. int mb_type = pict->mb_type[mb_index];
  1532. uint64_t u,v;
  1533. int y;
  1534. #define COLOR(theta, r) \
  1535. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1536. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1537. u = v = 128;
  1538. if (IS_PCM(mb_type)) {
  1539. COLOR(120, 48)
  1540. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1541. IS_INTRA16x16(mb_type)) {
  1542. COLOR(30, 48)
  1543. } else if (IS_INTRA4x4(mb_type)) {
  1544. COLOR(90, 48)
  1545. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1546. // COLOR(120, 48)
  1547. } else if (IS_DIRECT(mb_type)) {
  1548. COLOR(150, 48)
  1549. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1550. COLOR(170, 48)
  1551. } else if (IS_GMC(mb_type)) {
  1552. COLOR(190, 48)
  1553. } else if (IS_SKIP(mb_type)) {
  1554. // COLOR(180, 48)
  1555. } else if (!USES_LIST(mb_type, 1)) {
  1556. COLOR(240, 48)
  1557. } else if (!USES_LIST(mb_type, 0)) {
  1558. COLOR(0, 48)
  1559. } else {
  1560. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1561. COLOR(300,48)
  1562. }
  1563. u *= 0x0101010101010101ULL;
  1564. v *= 0x0101010101010101ULL;
  1565. for (y = 0; y < block_height; y++) {
  1566. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1567. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1568. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1569. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1570. }
  1571. // segmentation
  1572. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1573. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1574. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1575. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1576. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1577. }
  1578. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1579. for (y = 0; y < 16; y++)
  1580. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1581. pict->linesize[0]] ^= 0x80;
  1582. }
  1583. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1584. int dm = 1 << (mv_sample_log2 - 2);
  1585. for (i = 0; i < 4; i++) {
  1586. int sx = mb_x * 16 + 8 * (i & 1);
  1587. int sy = mb_y * 16 + 8 * (i >> 1);
  1588. int xy = (mb_x * 2 + (i & 1) +
  1589. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1590. // FIXME bidir
  1591. int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
  1592. if (mv[0] != mv[dm] ||
  1593. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1594. for (y = 0; y < 8; y++)
  1595. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1596. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1597. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1598. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1599. }
  1600. }
  1601. if (IS_INTERLACED(mb_type) &&
  1602. s->codec_id == CODEC_ID_H264) {
  1603. // hmm
  1604. }
  1605. }
  1606. s->mbskip_table[mb_index] = 0;
  1607. }
  1608. }
  1609. }
  1610. }
  1611. static inline int hpel_motion_lowres(MpegEncContext *s,
  1612. uint8_t *dest, uint8_t *src,
  1613. int field_based, int field_select,
  1614. int src_x, int src_y,
  1615. int width, int height, int stride,
  1616. int h_edge_pos, int v_edge_pos,
  1617. int w, int h, h264_chroma_mc_func *pix_op,
  1618. int motion_x, int motion_y)
  1619. {
  1620. const int lowres = s->avctx->lowres;
  1621. const int op_index = FFMIN(lowres, 2);
  1622. const int s_mask = (2 << lowres) - 1;
  1623. int emu = 0;
  1624. int sx, sy;
  1625. if (s->quarter_sample) {
  1626. motion_x /= 2;
  1627. motion_y /= 2;
  1628. }
  1629. sx = motion_x & s_mask;
  1630. sy = motion_y & s_mask;
  1631. src_x += motion_x >> lowres + 1;
  1632. src_y += motion_y >> lowres + 1;
  1633. src += src_y * stride + src_x;
  1634. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1635. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1636. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
  1637. (h + 1) << field_based, src_x,
  1638. src_y << field_based,
  1639. h_edge_pos,
  1640. v_edge_pos);
  1641. src = s->edge_emu_buffer;
  1642. emu = 1;
  1643. }
  1644. sx = (sx << 2) >> lowres;
  1645. sy = (sy << 2) >> lowres;
  1646. if (field_select)
  1647. src += s->linesize;
  1648. pix_op[op_index](dest, src, stride, h, sx, sy);
  1649. return emu;
  1650. }
  1651. /* apply one mpeg motion vector to the three components */
  1652. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1653. uint8_t *dest_y,
  1654. uint8_t *dest_cb,
  1655. uint8_t *dest_cr,
  1656. int field_based,
  1657. int bottom_field,
  1658. int field_select,
  1659. uint8_t **ref_picture,
  1660. h264_chroma_mc_func *pix_op,
  1661. int motion_x, int motion_y,
  1662. int h, int mb_y)
  1663. {
  1664. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1665. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
  1666. uvsx, uvsy;
  1667. const int lowres = s->avctx->lowres;
  1668. const int op_index = FFMIN(lowres, 2);
  1669. const int block_s = 8>>lowres;
  1670. const int s_mask = (2 << lowres) - 1;
  1671. const int h_edge_pos = s->h_edge_pos >> lowres;
  1672. const int v_edge_pos = s->v_edge_pos >> lowres;
  1673. linesize = s->current_picture.f.linesize[0] << field_based;
  1674. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1675. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1676. if (s->quarter_sample) {
  1677. motion_x /= 2;
  1678. motion_y /= 2;
  1679. }
  1680. if (field_based) {
  1681. motion_y += (bottom_field - field_select) * (1 << lowres - 1);
  1682. }
  1683. sx = motion_x & s_mask;
  1684. sy = motion_y & s_mask;
  1685. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1686. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1687. if (s->out_format == FMT_H263) {
  1688. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1689. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1690. uvsrc_x = src_x >> 1;
  1691. uvsrc_y = src_y >> 1;
  1692. } else if (s->out_format == FMT_H261) {
  1693. // even chroma mv's are full pel in H261
  1694. mx = motion_x / 4;
  1695. my = motion_y / 4;
  1696. uvsx = (2 * mx) & s_mask;
  1697. uvsy = (2 * my) & s_mask;
  1698. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1699. uvsrc_y = mb_y * block_s + (my >> lowres);
  1700. } else {
  1701. mx = motion_x / 2;
  1702. my = motion_y / 2;
  1703. uvsx = mx & s_mask;
  1704. uvsy = my & s_mask;
  1705. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1706. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1707. }
  1708. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1709. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1710. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1711. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
  1712. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1713. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  1714. s->linesize, 17, 17 + field_based,
  1715. src_x, src_y << field_based, h_edge_pos,
  1716. v_edge_pos);
  1717. ptr_y = s->edge_emu_buffer;
  1718. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1719. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  1720. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9,
  1721. 9 + field_based,
  1722. uvsrc_x, uvsrc_y << field_based,
  1723. h_edge_pos >> 1, v_edge_pos >> 1);
  1724. s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9,
  1725. 9 + field_based,
  1726. uvsrc_x, uvsrc_y << field_based,
  1727. h_edge_pos >> 1, v_edge_pos >> 1);
  1728. ptr_cb = uvbuf;
  1729. ptr_cr = uvbuf + 16;
  1730. }
  1731. }
  1732. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1733. if (bottom_field) {
  1734. dest_y += s->linesize;
  1735. dest_cb += s->uvlinesize;
  1736. dest_cr += s->uvlinesize;
  1737. }
  1738. if (field_select) {
  1739. ptr_y += s->linesize;
  1740. ptr_cb += s->uvlinesize;
  1741. ptr_cr += s->uvlinesize;
  1742. }
  1743. sx = (sx << 2) >> lowres;
  1744. sy = (sy << 2) >> lowres;
  1745. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1746. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1747. uvsx = (uvsx << 2) >> lowres;
  1748. uvsy = (uvsy << 2) >> lowres;
  1749. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift,
  1750. uvsx, uvsy);
  1751. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift,
  1752. uvsx, uvsy);
  1753. }
  1754. // FIXME h261 lowres loop filter
  1755. }
  1756. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1757. uint8_t *dest_cb, uint8_t *dest_cr,
  1758. uint8_t **ref_picture,
  1759. h264_chroma_mc_func * pix_op,
  1760. int mx, int my)
  1761. {
  1762. const int lowres = s->avctx->lowres;
  1763. const int op_index = FFMIN(lowres, 2);
  1764. const int block_s = 8 >> lowres;
  1765. const int s_mask = (2 << lowres) - 1;
  1766. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1767. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1768. int emu = 0, src_x, src_y, offset, sx, sy;
  1769. uint8_t *ptr;
  1770. if (s->quarter_sample) {
  1771. mx /= 2;
  1772. my /= 2;
  1773. }
  1774. /* In case of 8X8, we construct a single chroma motion vector
  1775. with a special rounding */
  1776. mx = ff_h263_round_chroma(mx);
  1777. my = ff_h263_round_chroma(my);
  1778. sx = mx & s_mask;
  1779. sy = my & s_mask;
  1780. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1781. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1782. offset = src_y * s->uvlinesize + src_x;
  1783. ptr = ref_picture[1] + offset;
  1784. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  1785. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1786. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1787. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  1788. 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1789. ptr = s->edge_emu_buffer;
  1790. emu = 1;
  1791. }
  1792. }
  1793. sx = (sx << 2) >> lowres;
  1794. sy = (sy << 2) >> lowres;
  1795. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1796. ptr = ref_picture[2] + offset;
  1797. if (emu) {
  1798. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
  1799. src_x, src_y, h_edge_pos, v_edge_pos);
  1800. ptr = s->edge_emu_buffer;
  1801. }
  1802. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1803. }
  1804. /**
  1805. * motion compensation of a single macroblock
  1806. * @param s context
  1807. * @param dest_y luma destination pointer
  1808. * @param dest_cb chroma cb/u destination pointer
  1809. * @param dest_cr chroma cr/v destination pointer
  1810. * @param dir direction (0->forward, 1->backward)
  1811. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1812. * @param pix_op halfpel motion compensation function (average or put normally)
  1813. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1814. */
  1815. static inline void MPV_motion_lowres(MpegEncContext *s,
  1816. uint8_t *dest_y, uint8_t *dest_cb,
  1817. uint8_t *dest_cr,
  1818. int dir, uint8_t **ref_picture,
  1819. h264_chroma_mc_func *pix_op)
  1820. {
  1821. int mx, my;
  1822. int mb_x, mb_y, i;
  1823. const int lowres = s->avctx->lowres;
  1824. const int block_s = 8 >>lowres;
  1825. mb_x = s->mb_x;
  1826. mb_y = s->mb_y;
  1827. switch (s->mv_type) {
  1828. case MV_TYPE_16X16:
  1829. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1830. 0, 0, 0,
  1831. ref_picture, pix_op,
  1832. s->mv[dir][0][0], s->mv[dir][0][1],
  1833. 2 * block_s, mb_y);
  1834. break;
  1835. case MV_TYPE_8X8:
  1836. mx = 0;
  1837. my = 0;
  1838. for (i = 0; i < 4; i++) {
  1839. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  1840. s->linesize) * block_s,
  1841. ref_picture[0], 0, 0,
  1842. (2 * mb_x + (i & 1)) * block_s,
  1843. (2 * mb_y + (i >> 1)) * block_s,
  1844. s->width, s->height, s->linesize,
  1845. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1846. block_s, block_s, pix_op,
  1847. s->mv[dir][i][0], s->mv[dir][i][1]);
  1848. mx += s->mv[dir][i][0];
  1849. my += s->mv[dir][i][1];
  1850. }
  1851. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  1852. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  1853. pix_op, mx, my);
  1854. break;
  1855. case MV_TYPE_FIELD:
  1856. if (s->picture_structure == PICT_FRAME) {
  1857. /* top field */
  1858. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1859. 1, 0, s->field_select[dir][0],
  1860. ref_picture, pix_op,
  1861. s->mv[dir][0][0], s->mv[dir][0][1],
  1862. block_s, mb_y);
  1863. /* bottom field */
  1864. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1865. 1, 1, s->field_select[dir][1],
  1866. ref_picture, pix_op,
  1867. s->mv[dir][1][0], s->mv[dir][1][1],
  1868. block_s, mb_y);
  1869. } else {
  1870. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  1871. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  1872. ref_picture = s->current_picture_ptr->f.data;
  1873. }
  1874. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1875. 0, 0, s->field_select[dir][0],
  1876. ref_picture, pix_op,
  1877. s->mv[dir][0][0],
  1878. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  1879. }
  1880. break;
  1881. case MV_TYPE_16X8:
  1882. for (i = 0; i < 2; i++) {
  1883. uint8_t **ref2picture;
  1884. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  1885. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  1886. ref2picture = ref_picture;
  1887. } else {
  1888. ref2picture = s->current_picture_ptr->f.data;
  1889. }
  1890. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1891. 0, 0, s->field_select[dir][i],
  1892. ref2picture, pix_op,
  1893. s->mv[dir][i][0], s->mv[dir][i][1] +
  1894. 2 * block_s * i, block_s, mb_y >> 1);
  1895. dest_y += 2 * block_s * s->linesize;
  1896. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1897. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1898. }
  1899. break;
  1900. case MV_TYPE_DMV:
  1901. if (s->picture_structure == PICT_FRAME) {
  1902. for (i = 0; i < 2; i++) {
  1903. int j;
  1904. for (j = 0; j < 2; j++) {
  1905. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1906. 1, j, j ^ i,
  1907. ref_picture, pix_op,
  1908. s->mv[dir][2 * i + j][0],
  1909. s->mv[dir][2 * i + j][1],
  1910. block_s, mb_y);
  1911. }
  1912. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1913. }
  1914. } else {
  1915. for (i = 0; i < 2; i++) {
  1916. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1917. 0, 0, s->picture_structure != i + 1,
  1918. ref_picture, pix_op,
  1919. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  1920. 2 * block_s, mb_y >> 1);
  1921. // after put we make avg of the same block
  1922. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1923. // opposite parity is always in the same
  1924. // frame if this is second field
  1925. if (!s->first_field) {
  1926. ref_picture = s->current_picture_ptr->f.data;
  1927. }
  1928. }
  1929. }
  1930. break;
  1931. default:
  1932. assert(0);
  1933. }
  1934. }
  1935. /**
  1936. * find the lowest MB row referenced in the MVs
  1937. */
  1938. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1939. {
  1940. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1941. int my, off, i, mvs;
  1942. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1943. switch (s->mv_type) {
  1944. case MV_TYPE_16X16:
  1945. mvs = 1;
  1946. break;
  1947. case MV_TYPE_16X8:
  1948. mvs = 2;
  1949. break;
  1950. case MV_TYPE_8X8:
  1951. mvs = 4;
  1952. break;
  1953. default:
  1954. goto unhandled;
  1955. }
  1956. for (i = 0; i < mvs; i++) {
  1957. my = s->mv[dir][i][1]<<qpel_shift;
  1958. my_max = FFMAX(my_max, my);
  1959. my_min = FFMIN(my_min, my);
  1960. }
  1961. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1962. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1963. unhandled:
  1964. return s->mb_height-1;
  1965. }
  1966. /* put block[] to dest[] */
  1967. static inline void put_dct(MpegEncContext *s,
  1968. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1969. {
  1970. s->dct_unquantize_intra(s, block, i, qscale);
  1971. s->dsp.idct_put (dest, line_size, block);
  1972. }
  1973. /* add block[] to dest[] */
  1974. static inline void add_dct(MpegEncContext *s,
  1975. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1976. {
  1977. if (s->block_last_index[i] >= 0) {
  1978. s->dsp.idct_add (dest, line_size, block);
  1979. }
  1980. }
  1981. static inline void add_dequant_dct(MpegEncContext *s,
  1982. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1983. {
  1984. if (s->block_last_index[i] >= 0) {
  1985. s->dct_unquantize_inter(s, block, i, qscale);
  1986. s->dsp.idct_add (dest, line_size, block);
  1987. }
  1988. }
  1989. /**
  1990. * Clean dc, ac, coded_block for the current non-intra MB.
  1991. */
  1992. void ff_clean_intra_table_entries(MpegEncContext *s)
  1993. {
  1994. int wrap = s->b8_stride;
  1995. int xy = s->block_index[0];
  1996. s->dc_val[0][xy ] =
  1997. s->dc_val[0][xy + 1 ] =
  1998. s->dc_val[0][xy + wrap] =
  1999. s->dc_val[0][xy + 1 + wrap] = 1024;
  2000. /* ac pred */
  2001. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2002. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2003. if (s->msmpeg4_version>=3) {
  2004. s->coded_block[xy ] =
  2005. s->coded_block[xy + 1 ] =
  2006. s->coded_block[xy + wrap] =
  2007. s->coded_block[xy + 1 + wrap] = 0;
  2008. }
  2009. /* chroma */
  2010. wrap = s->mb_stride;
  2011. xy = s->mb_x + s->mb_y * wrap;
  2012. s->dc_val[1][xy] =
  2013. s->dc_val[2][xy] = 1024;
  2014. /* ac pred */
  2015. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2016. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2017. s->mbintra_table[xy]= 0;
  2018. }
  2019. /* generic function called after a macroblock has been parsed by the
  2020. decoder or after it has been encoded by the encoder.
  2021. Important variables used:
  2022. s->mb_intra : true if intra macroblock
  2023. s->mv_dir : motion vector direction
  2024. s->mv_type : motion vector type
  2025. s->mv : motion vector
  2026. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2027. */
  2028. static av_always_inline
  2029. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  2030. int lowres_flag, int is_mpeg12)
  2031. {
  2032. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2033. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  2034. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  2035. return;
  2036. }
  2037. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2038. /* save DCT coefficients */
  2039. int i,j;
  2040. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  2041. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2042. for(i=0; i<6; i++){
  2043. for(j=0; j<64; j++){
  2044. *dct++ = block[i][s->dsp.idct_permutation[j]];
  2045. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  2046. }
  2047. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2048. }
  2049. }
  2050. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  2051. /* update DC predictors for P macroblocks */
  2052. if (!s->mb_intra) {
  2053. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2054. if(s->mbintra_table[mb_xy])
  2055. ff_clean_intra_table_entries(s);
  2056. } else {
  2057. s->last_dc[0] =
  2058. s->last_dc[1] =
  2059. s->last_dc[2] = 128 << s->intra_dc_precision;
  2060. }
  2061. }
  2062. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2063. s->mbintra_table[mb_xy]=1;
  2064. 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
  2065. uint8_t *dest_y, *dest_cb, *dest_cr;
  2066. int dct_linesize, dct_offset;
  2067. op_pixels_func (*op_pix)[4];
  2068. qpel_mc_func (*op_qpix)[16];
  2069. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2070. const int uvlinesize = s->current_picture.f.linesize[1];
  2071. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2072. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2073. /* avoid copy if macroblock skipped in last frame too */
  2074. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2075. if(!s->encoding){
  2076. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2077. if (s->mb_skipped) {
  2078. s->mb_skipped= 0;
  2079. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  2080. *mbskip_ptr = 1;
  2081. } else if(!s->current_picture.f.reference) {
  2082. *mbskip_ptr = 1;
  2083. } else{
  2084. *mbskip_ptr = 0; /* not skipped */
  2085. }
  2086. }
  2087. dct_linesize = linesize << s->interlaced_dct;
  2088. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2089. if(readable){
  2090. dest_y= s->dest[0];
  2091. dest_cb= s->dest[1];
  2092. dest_cr= s->dest[2];
  2093. }else{
  2094. dest_y = s->b_scratchpad;
  2095. dest_cb= s->b_scratchpad+16*linesize;
  2096. dest_cr= s->b_scratchpad+32*linesize;
  2097. }
  2098. if (!s->mb_intra) {
  2099. /* motion handling */
  2100. /* decoding or more than one mb_type (MC was already done otherwise) */
  2101. if(!s->encoding){
  2102. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2103. if (s->mv_dir & MV_DIR_FORWARD) {
  2104. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  2105. }
  2106. if (s->mv_dir & MV_DIR_BACKWARD) {
  2107. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  2108. }
  2109. }
  2110. if(lowres_flag){
  2111. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  2112. if (s->mv_dir & MV_DIR_FORWARD) {
  2113. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2114. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  2115. }
  2116. if (s->mv_dir & MV_DIR_BACKWARD) {
  2117. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2118. }
  2119. }else{
  2120. op_qpix= s->me.qpel_put;
  2121. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2122. op_pix = s->dsp.put_pixels_tab;
  2123. }else{
  2124. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2125. }
  2126. if (s->mv_dir & MV_DIR_FORWARD) {
  2127. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2128. op_pix = s->dsp.avg_pixels_tab;
  2129. op_qpix= s->me.qpel_avg;
  2130. }
  2131. if (s->mv_dir & MV_DIR_BACKWARD) {
  2132. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2133. }
  2134. }
  2135. }
  2136. /* skip dequant / idct if we are really late ;) */
  2137. if(s->avctx->skip_idct){
  2138. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2139. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2140. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2141. goto skip_idct;
  2142. }
  2143. /* add dct residue */
  2144. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2145. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2146. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2147. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2148. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2149. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2150. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2151. if (s->chroma_y_shift){
  2152. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2153. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2154. }else{
  2155. dct_linesize >>= 1;
  2156. dct_offset >>=1;
  2157. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2158. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2159. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2160. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2161. }
  2162. }
  2163. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  2164. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2165. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2166. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2167. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2168. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2169. if(s->chroma_y_shift){//Chroma420
  2170. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2171. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2172. }else{
  2173. //chroma422
  2174. dct_linesize = uvlinesize << s->interlaced_dct;
  2175. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  2176. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2177. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2178. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2179. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2180. if(!s->chroma_x_shift){//Chroma444
  2181. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  2182. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  2183. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  2184. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  2185. }
  2186. }
  2187. }//fi gray
  2188. }
  2189. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2190. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2191. }
  2192. } else {
  2193. /* dct only in intra block */
  2194. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2195. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2196. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2197. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2198. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2199. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2200. if(s->chroma_y_shift){
  2201. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2202. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2203. }else{
  2204. dct_offset >>=1;
  2205. dct_linesize >>=1;
  2206. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2207. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2208. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2209. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2210. }
  2211. }
  2212. }else{
  2213. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2214. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2215. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2216. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2217. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2218. if(s->chroma_y_shift){
  2219. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2220. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2221. }else{
  2222. dct_linesize = uvlinesize << s->interlaced_dct;
  2223. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  2224. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2225. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2226. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2227. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2228. if(!s->chroma_x_shift){//Chroma444
  2229. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2230. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2231. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2232. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2233. }
  2234. }
  2235. }//gray
  2236. }
  2237. }
  2238. skip_idct:
  2239. if(!readable){
  2240. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2241. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2242. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2243. }
  2244. }
  2245. }
  2246. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  2247. #if !CONFIG_SMALL
  2248. if(s->out_format == FMT_MPEG1) {
  2249. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2250. else MPV_decode_mb_internal(s, block, 0, 1);
  2251. } else
  2252. #endif
  2253. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2254. else MPV_decode_mb_internal(s, block, 0, 0);
  2255. }
  2256. /**
  2257. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2258. */
  2259. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2260. const int field_pic= s->picture_structure != PICT_FRAME;
  2261. if(field_pic){
  2262. h <<= 1;
  2263. y <<= 1;
  2264. }
  2265. if (!s->avctx->hwaccel
  2266. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2267. && s->unrestricted_mv
  2268. && s->current_picture.f.reference
  2269. && !s->intra_only
  2270. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2271. int sides = 0, edge_h;
  2272. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  2273. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  2274. if (y==0) sides |= EDGE_TOP;
  2275. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2276. edge_h= FFMIN(h, s->v_edge_pos - y);
  2277. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  2278. s->linesize, s->h_edge_pos, edge_h,
  2279. EDGE_WIDTH, EDGE_WIDTH, sides);
  2280. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  2281. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2282. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2283. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  2284. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2285. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2286. }
  2287. h= FFMIN(h, s->avctx->height - y);
  2288. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2289. if (s->avctx->draw_horiz_band) {
  2290. AVFrame *src;
  2291. int offset[AV_NUM_DATA_POINTERS];
  2292. int i;
  2293. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2294. src= (AVFrame*)s->current_picture_ptr;
  2295. else if(s->last_picture_ptr)
  2296. src= (AVFrame*)s->last_picture_ptr;
  2297. else
  2298. return;
  2299. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2300. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2301. offset[i] = 0;
  2302. }else{
  2303. offset[0]= y * s->linesize;
  2304. offset[1]=
  2305. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2306. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2307. offset[i] = 0;
  2308. }
  2309. emms_c();
  2310. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2311. y, s->picture_structure, h);
  2312. }
  2313. }
  2314. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2315. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2316. const int uvlinesize = s->current_picture.f.linesize[1];
  2317. const int mb_size= 4 - s->avctx->lowres;
  2318. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2319. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2320. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2321. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2322. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2323. 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;
  2324. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2325. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2326. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2327. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2328. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2329. {
  2330. if(s->picture_structure==PICT_FRAME){
  2331. s->dest[0] += s->mb_y * linesize << mb_size;
  2332. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2333. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2334. }else{
  2335. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2336. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2337. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2338. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2339. }
  2340. }
  2341. }
  2342. void ff_mpeg_flush(AVCodecContext *avctx){
  2343. int i;
  2344. MpegEncContext *s = avctx->priv_data;
  2345. if(s==NULL || s->picture==NULL)
  2346. return;
  2347. for(i=0; i<s->picture_count; i++){
  2348. if (s->picture[i].f.data[0] &&
  2349. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2350. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2351. free_frame_buffer(s, &s->picture[i]);
  2352. }
  2353. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2354. s->mb_x= s->mb_y= 0;
  2355. s->parse_context.state= -1;
  2356. s->parse_context.frame_start_found= 0;
  2357. s->parse_context.overread= 0;
  2358. s->parse_context.overread_index= 0;
  2359. s->parse_context.index= 0;
  2360. s->parse_context.last_index= 0;
  2361. s->bitstream_buffer_size=0;
  2362. s->pp_time=0;
  2363. }
  2364. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2365. DCTELEM *block, int n, int qscale)
  2366. {
  2367. int i, level, nCoeffs;
  2368. const uint16_t *quant_matrix;
  2369. nCoeffs= s->block_last_index[n];
  2370. if (n < 4)
  2371. block[0] = block[0] * s->y_dc_scale;
  2372. else
  2373. block[0] = block[0] * s->c_dc_scale;
  2374. /* XXX: only mpeg1 */
  2375. quant_matrix = s->intra_matrix;
  2376. for(i=1;i<=nCoeffs;i++) {
  2377. int j= s->intra_scantable.permutated[i];
  2378. level = block[j];
  2379. if (level) {
  2380. if (level < 0) {
  2381. level = -level;
  2382. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2383. level = (level - 1) | 1;
  2384. level = -level;
  2385. } else {
  2386. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2387. level = (level - 1) | 1;
  2388. }
  2389. block[j] = level;
  2390. }
  2391. }
  2392. }
  2393. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2394. DCTELEM *block, int n, int qscale)
  2395. {
  2396. int i, level, nCoeffs;
  2397. const uint16_t *quant_matrix;
  2398. nCoeffs= s->block_last_index[n];
  2399. quant_matrix = s->inter_matrix;
  2400. for(i=0; i<=nCoeffs; i++) {
  2401. int j= s->intra_scantable.permutated[i];
  2402. level = block[j];
  2403. if (level) {
  2404. if (level < 0) {
  2405. level = -level;
  2406. level = (((level << 1) + 1) * qscale *
  2407. ((int) (quant_matrix[j]))) >> 4;
  2408. level = (level - 1) | 1;
  2409. level = -level;
  2410. } else {
  2411. level = (((level << 1) + 1) * qscale *
  2412. ((int) (quant_matrix[j]))) >> 4;
  2413. level = (level - 1) | 1;
  2414. }
  2415. block[j] = level;
  2416. }
  2417. }
  2418. }
  2419. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2420. DCTELEM *block, int n, int qscale)
  2421. {
  2422. int i, level, nCoeffs;
  2423. const uint16_t *quant_matrix;
  2424. if(s->alternate_scan) nCoeffs= 63;
  2425. else nCoeffs= s->block_last_index[n];
  2426. if (n < 4)
  2427. block[0] = block[0] * s->y_dc_scale;
  2428. else
  2429. block[0] = block[0] * s->c_dc_scale;
  2430. quant_matrix = s->intra_matrix;
  2431. for(i=1;i<=nCoeffs;i++) {
  2432. int j= s->intra_scantable.permutated[i];
  2433. level = block[j];
  2434. if (level) {
  2435. if (level < 0) {
  2436. level = -level;
  2437. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2438. level = -level;
  2439. } else {
  2440. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2441. }
  2442. block[j] = level;
  2443. }
  2444. }
  2445. }
  2446. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2447. DCTELEM *block, int n, int qscale)
  2448. {
  2449. int i, level, nCoeffs;
  2450. const uint16_t *quant_matrix;
  2451. int sum=-1;
  2452. if(s->alternate_scan) nCoeffs= 63;
  2453. else nCoeffs= s->block_last_index[n];
  2454. if (n < 4)
  2455. block[0] = block[0] * s->y_dc_scale;
  2456. else
  2457. block[0] = block[0] * s->c_dc_scale;
  2458. quant_matrix = s->intra_matrix;
  2459. for(i=1;i<=nCoeffs;i++) {
  2460. int j= s->intra_scantable.permutated[i];
  2461. level = block[j];
  2462. if (level) {
  2463. if (level < 0) {
  2464. level = -level;
  2465. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2466. level = -level;
  2467. } else {
  2468. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2469. }
  2470. block[j] = level;
  2471. sum+=level;
  2472. }
  2473. }
  2474. block[63]^=sum&1;
  2475. }
  2476. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2477. DCTELEM *block, int n, int qscale)
  2478. {
  2479. int i, level, nCoeffs;
  2480. const uint16_t *quant_matrix;
  2481. int sum=-1;
  2482. if(s->alternate_scan) nCoeffs= 63;
  2483. else nCoeffs= s->block_last_index[n];
  2484. quant_matrix = s->inter_matrix;
  2485. for(i=0; i<=nCoeffs; i++) {
  2486. int j= s->intra_scantable.permutated[i];
  2487. level = block[j];
  2488. if (level) {
  2489. if (level < 0) {
  2490. level = -level;
  2491. level = (((level << 1) + 1) * qscale *
  2492. ((int) (quant_matrix[j]))) >> 4;
  2493. level = -level;
  2494. } else {
  2495. level = (((level << 1) + 1) * qscale *
  2496. ((int) (quant_matrix[j]))) >> 4;
  2497. }
  2498. block[j] = level;
  2499. sum+=level;
  2500. }
  2501. }
  2502. block[63]^=sum&1;
  2503. }
  2504. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2505. DCTELEM *block, int n, int qscale)
  2506. {
  2507. int i, level, qmul, qadd;
  2508. int nCoeffs;
  2509. assert(s->block_last_index[n]>=0);
  2510. qmul = qscale << 1;
  2511. if (!s->h263_aic) {
  2512. if (n < 4)
  2513. block[0] = block[0] * s->y_dc_scale;
  2514. else
  2515. block[0] = block[0] * s->c_dc_scale;
  2516. qadd = (qscale - 1) | 1;
  2517. }else{
  2518. qadd = 0;
  2519. }
  2520. if(s->ac_pred)
  2521. nCoeffs=63;
  2522. else
  2523. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2524. for(i=1; i<=nCoeffs; i++) {
  2525. level = block[i];
  2526. if (level) {
  2527. if (level < 0) {
  2528. level = level * qmul - qadd;
  2529. } else {
  2530. level = level * qmul + qadd;
  2531. }
  2532. block[i] = level;
  2533. }
  2534. }
  2535. }
  2536. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2537. DCTELEM *block, int n, int qscale)
  2538. {
  2539. int i, level, qmul, qadd;
  2540. int nCoeffs;
  2541. assert(s->block_last_index[n]>=0);
  2542. qadd = (qscale - 1) | 1;
  2543. qmul = qscale << 1;
  2544. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2545. for(i=0; i<=nCoeffs; i++) {
  2546. level = block[i];
  2547. if (level) {
  2548. if (level < 0) {
  2549. level = level * qmul - qadd;
  2550. } else {
  2551. level = level * qmul + qadd;
  2552. }
  2553. block[i] = level;
  2554. }
  2555. }
  2556. }
  2557. /**
  2558. * set qscale and update qscale dependent variables.
  2559. */
  2560. void ff_set_qscale(MpegEncContext * s, int qscale)
  2561. {
  2562. if (qscale < 1)
  2563. qscale = 1;
  2564. else if (qscale > 31)
  2565. qscale = 31;
  2566. s->qscale = qscale;
  2567. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2568. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2569. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2570. }
  2571. void MPV_report_decode_progress(MpegEncContext *s)
  2572. {
  2573. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2574. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2575. }