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.

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