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.

2891 lines
109KB

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