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.

2768 lines
104KB

  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. int intra, i;
  992. for(intra=0; intra<2; intra++){
  993. if(s->dct_count[intra] > (1<<16)){
  994. for(i=0; i<64; i++){
  995. s->dct_error_sum[intra][i] >>=1;
  996. }
  997. s->dct_count[intra] >>= 1;
  998. }
  999. for(i=0; i<64; i++){
  1000. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  1001. }
  1002. }
  1003. }
  1004. /**
  1005. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  1006. */
  1007. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1008. {
  1009. int i;
  1010. Picture *pic;
  1011. s->mb_skipped = 0;
  1012. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  1013. /* mark&release old frames */
  1014. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->f.data[0]) {
  1015. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  1016. if (s->last_picture_ptr->owner2 == s)
  1017. free_frame_buffer(s, s->last_picture_ptr);
  1018. /* release forgotten pictures */
  1019. /* if(mpeg124/h263) */
  1020. if(!s->encoding){
  1021. for(i=0; i<s->picture_count; i++){
  1022. if (s->picture[i].owner2 == s && s->picture[i].f.data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].f.reference) {
  1023. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1024. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  1025. free_frame_buffer(s, &s->picture[i]);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. }
  1031. if(!s->encoding){
  1032. ff_release_unused_pictures(s, 1);
  1033. if (s->current_picture_ptr && s->current_picture_ptr->f.data[0] == NULL)
  1034. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  1035. else{
  1036. i= ff_find_unused_picture(s, 0);
  1037. if (i < 0)
  1038. return i;
  1039. pic= &s->picture[i];
  1040. }
  1041. pic->f.reference = 0;
  1042. if (!s->dropable){
  1043. if (s->codec_id == CODEC_ID_H264)
  1044. pic->f.reference = s->picture_structure;
  1045. else if (s->pict_type != AV_PICTURE_TYPE_B)
  1046. pic->f.reference = 3;
  1047. }
  1048. pic->f.coded_picture_number = s->coded_picture_number++;
  1049. if(ff_alloc_picture(s, pic, 0) < 0)
  1050. return -1;
  1051. s->current_picture_ptr= pic;
  1052. //FIXME use only the vars from current_pic
  1053. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1054. if(s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1055. if(s->picture_structure != PICT_FRAME)
  1056. s->current_picture_ptr->f.top_field_first = (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1057. }
  1058. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame && !s->progressive_sequence;
  1059. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1060. }
  1061. s->current_picture_ptr->f.pict_type = s->pict_type;
  1062. // if(s->flags && CODEC_FLAG_QSCALE)
  1063. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  1064. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1065. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  1066. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1067. s->last_picture_ptr= s->next_picture_ptr;
  1068. if(!s->dropable)
  1069. s->next_picture_ptr= s->current_picture_ptr;
  1070. }
  1071. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1072. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1073. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1074. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1075. s->pict_type, s->dropable);*/
  1076. if(s->codec_id != CODEC_ID_H264){
  1077. if ((s->last_picture_ptr == NULL || s->last_picture_ptr->f.data[0] == NULL) &&
  1078. (s->pict_type!=AV_PICTURE_TYPE_I || s->picture_structure != PICT_FRAME)){
  1079. if (s->pict_type != AV_PICTURE_TYPE_I)
  1080. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  1081. else if (s->picture_structure != PICT_FRAME)
  1082. av_log(avctx, AV_LOG_INFO, "allocate dummy last picture for field based first keyframe\n");
  1083. /* Allocate a dummy frame */
  1084. i= ff_find_unused_picture(s, 0);
  1085. if (i < 0)
  1086. return i;
  1087. s->last_picture_ptr= &s->picture[i];
  1088. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  1089. return -1;
  1090. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
  1091. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
  1092. }
  1093. if ((s->next_picture_ptr == NULL || s->next_picture_ptr->f.data[0] == NULL) && s->pict_type == AV_PICTURE_TYPE_B) {
  1094. /* Allocate a dummy frame */
  1095. i= ff_find_unused_picture(s, 0);
  1096. if (i < 0)
  1097. return i;
  1098. s->next_picture_ptr= &s->picture[i];
  1099. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  1100. return -1;
  1101. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
  1102. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
  1103. }
  1104. }
  1105. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1106. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1107. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->f.data[0]));
  1108. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  1109. int i;
  1110. for(i=0; i<4; i++){
  1111. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1112. s->current_picture.f.data[i] += s->current_picture.f.linesize[i];
  1113. }
  1114. s->current_picture.f.linesize[i] *= 2;
  1115. s->last_picture.f.linesize[i] *= 2;
  1116. s->next_picture.f.linesize[i] *= 2;
  1117. }
  1118. }
  1119. s->err_recognition = avctx->err_recognition;
  1120. /* set dequantizer, we can't do it during init as it might change for mpeg4
  1121. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  1122. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  1123. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1124. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1125. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  1126. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1127. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1128. }else{
  1129. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1130. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1131. }
  1132. if(s->dct_error_sum){
  1133. assert(s->avctx->noise_reduction && s->encoding);
  1134. update_noise_reduction(s);
  1135. }
  1136. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1137. return ff_xvmc_field_start(s, avctx);
  1138. return 0;
  1139. }
  1140. /* generic function for encode/decode called after a frame has been coded/decoded */
  1141. void MPV_frame_end(MpegEncContext *s)
  1142. {
  1143. int i;
  1144. /* redraw edges for the frame if decoding didn't complete */
  1145. //just to make sure that all data is rendered.
  1146. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1147. ff_xvmc_field_end(s);
  1148. }else if((s->error_count || s->encoding)
  1149. && !s->avctx->hwaccel
  1150. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1151. && s->unrestricted_mv
  1152. && s->current_picture.f.reference
  1153. && !s->intra_only
  1154. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1155. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1156. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1157. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1158. s->h_edge_pos , s->v_edge_pos,
  1159. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1160. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1161. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1162. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1163. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1164. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1165. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1166. }
  1167. emms_c();
  1168. s->last_pict_type = s->pict_type;
  1169. s->last_lambda_for[s->pict_type] = s->current_picture_ptr->f.quality;
  1170. if(s->pict_type!=AV_PICTURE_TYPE_B){
  1171. s->last_non_b_pict_type= s->pict_type;
  1172. }
  1173. #if 0
  1174. /* copy back current_picture variables */
  1175. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1176. if(s->picture[i].f.data[0] == s->current_picture.f.data[0]){
  1177. s->picture[i]= s->current_picture;
  1178. break;
  1179. }
  1180. }
  1181. assert(i<MAX_PICTURE_COUNT);
  1182. #endif
  1183. if(s->encoding){
  1184. /* release non-reference frames */
  1185. for(i=0; i<s->picture_count; i++){
  1186. if (s->picture[i].f.data[0] && !s->picture[i].f.reference /*&& s->picture[i].type != FF_BUFFER_TYPE_SHARED*/) {
  1187. free_frame_buffer(s, &s->picture[i]);
  1188. }
  1189. }
  1190. }
  1191. // clear copies, to avoid confusion
  1192. #if 0
  1193. memset(&s->last_picture, 0, sizeof(Picture));
  1194. memset(&s->next_picture, 0, sizeof(Picture));
  1195. memset(&s->current_picture, 0, sizeof(Picture));
  1196. #endif
  1197. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1198. if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
  1199. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
  1200. }
  1201. }
  1202. /**
  1203. * Draw a line from (ex, ey) -> (sx, sy).
  1204. * @param w width of the image
  1205. * @param h height of the image
  1206. * @param stride stride/linesize of the image
  1207. * @param color color of the arrow
  1208. */
  1209. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1210. int x, y, fr, f;
  1211. sx= av_clip(sx, 0, w-1);
  1212. sy= av_clip(sy, 0, h-1);
  1213. ex= av_clip(ex, 0, w-1);
  1214. ey= av_clip(ey, 0, h-1);
  1215. buf[sy*stride + sx]+= color;
  1216. if(FFABS(ex - sx) > FFABS(ey - sy)){
  1217. if(sx > ex){
  1218. FFSWAP(int, sx, ex);
  1219. FFSWAP(int, sy, ey);
  1220. }
  1221. buf+= sx + sy*stride;
  1222. ex-= sx;
  1223. f= ((ey-sy)<<16)/ex;
  1224. for(x= 0; x <= ex; x++){
  1225. y = (x*f)>>16;
  1226. fr= (x*f)&0xFFFF;
  1227. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1228. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1229. }
  1230. }else{
  1231. if(sy > ey){
  1232. FFSWAP(int, sx, ex);
  1233. FFSWAP(int, sy, ey);
  1234. }
  1235. buf+= sx + sy*stride;
  1236. ey-= sy;
  1237. if(ey) f= ((ex-sx)<<16)/ey;
  1238. else f= 0;
  1239. for(y= 0; y <= ey; y++){
  1240. x = (y*f)>>16;
  1241. fr= (y*f)&0xFFFF;
  1242. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1243. buf[y*stride + x+1]+= (color* fr )>>16;
  1244. }
  1245. }
  1246. }
  1247. /**
  1248. * Draw an arrow from (ex, ey) -> (sx, sy).
  1249. * @param w width of the image
  1250. * @param h height of the image
  1251. * @param stride stride/linesize of the image
  1252. * @param color color of the arrow
  1253. */
  1254. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1255. int dx,dy;
  1256. sx= av_clip(sx, -100, w+100);
  1257. sy= av_clip(sy, -100, h+100);
  1258. ex= av_clip(ex, -100, w+100);
  1259. ey= av_clip(ey, -100, h+100);
  1260. dx= ex - sx;
  1261. dy= ey - sy;
  1262. if(dx*dx + dy*dy > 3*3){
  1263. int rx= dx + dy;
  1264. int ry= -dx + dy;
  1265. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1266. //FIXME subpixel accuracy
  1267. rx= ROUNDED_DIV(rx*3<<4, length);
  1268. ry= ROUNDED_DIV(ry*3<<4, length);
  1269. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1270. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1271. }
  1272. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1273. }
  1274. /**
  1275. * Print debuging info for the given picture.
  1276. */
  1277. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1278. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1279. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1280. int x,y;
  1281. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1282. switch (pict->pict_type) {
  1283. case AV_PICTURE_TYPE_I: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1284. case AV_PICTURE_TYPE_P: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1285. case AV_PICTURE_TYPE_B: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1286. case AV_PICTURE_TYPE_S: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1287. case AV_PICTURE_TYPE_SI: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1288. case AV_PICTURE_TYPE_SP: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1289. }
  1290. for(y=0; y<s->mb_height; y++){
  1291. for(x=0; x<s->mb_width; x++){
  1292. if(s->avctx->debug&FF_DEBUG_SKIP){
  1293. int count= s->mbskip_table[x + y*s->mb_stride];
  1294. if(count>9) count=9;
  1295. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1296. }
  1297. if(s->avctx->debug&FF_DEBUG_QP){
  1298. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1299. }
  1300. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1301. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1302. //Type & MV direction
  1303. if(IS_PCM(mb_type))
  1304. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1305. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1306. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1307. else if(IS_INTRA4x4(mb_type))
  1308. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1309. else if(IS_INTRA16x16(mb_type))
  1310. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1311. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1312. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1313. else if(IS_DIRECT(mb_type))
  1314. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1315. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1316. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1317. else if(IS_GMC(mb_type))
  1318. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1319. else if(IS_SKIP(mb_type))
  1320. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1321. else if(!USES_LIST(mb_type, 1))
  1322. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1323. else if(!USES_LIST(mb_type, 0))
  1324. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1325. else{
  1326. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1327. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1328. }
  1329. //segmentation
  1330. if(IS_8X8(mb_type))
  1331. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1332. else if(IS_16X8(mb_type))
  1333. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1334. else if(IS_8X16(mb_type))
  1335. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1336. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1337. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1338. else
  1339. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1340. if(IS_INTERLACED(mb_type))
  1341. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1342. else
  1343. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1344. }
  1345. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1346. }
  1347. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1348. }
  1349. }
  1350. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1351. s->avctx->debug_mv) {
  1352. const int shift= 1 + s->quarter_sample;
  1353. int mb_y;
  1354. uint8_t *ptr;
  1355. int i;
  1356. int h_chroma_shift, v_chroma_shift, block_height;
  1357. const int width = s->avctx->width;
  1358. const int height= s->avctx->height;
  1359. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1360. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1361. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1362. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1363. for(i=0; i<3; i++){
  1364. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1365. pict->data[i]= s->visualization_buffer[i];
  1366. }
  1367. pict->type= FF_BUFFER_TYPE_COPY;
  1368. ptr= pict->data[0];
  1369. block_height = 16>>v_chroma_shift;
  1370. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1371. int mb_x;
  1372. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1373. const int mb_index= mb_x + mb_y*s->mb_stride;
  1374. if (s->avctx->debug_mv && pict->motion_val) {
  1375. int type;
  1376. for(type=0; type<3; type++){
  1377. int direction = 0;
  1378. switch (type) {
  1379. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_P))
  1380. continue;
  1381. direction = 0;
  1382. break;
  1383. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1384. continue;
  1385. direction = 0;
  1386. break;
  1387. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1388. continue;
  1389. direction = 1;
  1390. break;
  1391. }
  1392. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1393. continue;
  1394. if(IS_8X8(pict->mb_type[mb_index])){
  1395. int i;
  1396. for(i=0; i<4; i++){
  1397. int sx= mb_x*16 + 4 + 8*(i&1);
  1398. int sy= mb_y*16 + 4 + 8*(i>>1);
  1399. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1400. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1401. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1402. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1403. }
  1404. }else if(IS_16X8(pict->mb_type[mb_index])){
  1405. int i;
  1406. for(i=0; i<2; i++){
  1407. int sx=mb_x*16 + 8;
  1408. int sy=mb_y*16 + 4 + 8*i;
  1409. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1410. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1411. int my=(pict->motion_val[direction][xy][1]>>shift);
  1412. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1413. my*=2;
  1414. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1415. }
  1416. }else if(IS_8X16(pict->mb_type[mb_index])){
  1417. int i;
  1418. for(i=0; i<2; i++){
  1419. int sx=mb_x*16 + 4 + 8*i;
  1420. int sy=mb_y*16 + 8;
  1421. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1422. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1423. int my=(pict->motion_val[direction][xy][1]>>shift);
  1424. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1425. my*=2;
  1426. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1427. }
  1428. }else{
  1429. int sx= mb_x*16 + 8;
  1430. int sy= mb_y*16 + 8;
  1431. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1432. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1433. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1434. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1435. }
  1436. }
  1437. }
  1438. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1439. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1440. int y;
  1441. for(y=0; y<block_height; y++){
  1442. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1443. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1444. }
  1445. }
  1446. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1447. int mb_type= pict->mb_type[mb_index];
  1448. uint64_t u,v;
  1449. int y;
  1450. #define COLOR(theta, r)\
  1451. u= (int)(128 + r*cos(theta*3.141592/180));\
  1452. v= (int)(128 + r*sin(theta*3.141592/180));
  1453. u=v=128;
  1454. if(IS_PCM(mb_type)){
  1455. COLOR(120,48)
  1456. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1457. COLOR(30,48)
  1458. }else if(IS_INTRA4x4(mb_type)){
  1459. COLOR(90,48)
  1460. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1461. // COLOR(120,48)
  1462. }else if(IS_DIRECT(mb_type)){
  1463. COLOR(150,48)
  1464. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1465. COLOR(170,48)
  1466. }else if(IS_GMC(mb_type)){
  1467. COLOR(190,48)
  1468. }else if(IS_SKIP(mb_type)){
  1469. // COLOR(180,48)
  1470. }else if(!USES_LIST(mb_type, 1)){
  1471. COLOR(240,48)
  1472. }else if(!USES_LIST(mb_type, 0)){
  1473. COLOR(0,48)
  1474. }else{
  1475. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1476. COLOR(300,48)
  1477. }
  1478. u*= 0x0101010101010101ULL;
  1479. v*= 0x0101010101010101ULL;
  1480. for(y=0; y<block_height; y++){
  1481. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1482. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1483. }
  1484. //segmentation
  1485. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1486. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1487. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1488. }
  1489. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1490. for(y=0; y<16; y++)
  1491. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1492. }
  1493. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1494. int dm= 1 << (mv_sample_log2-2);
  1495. for(i=0; i<4; i++){
  1496. int sx= mb_x*16 + 8*(i&1);
  1497. int sy= mb_y*16 + 8*(i>>1);
  1498. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1499. //FIXME bidir
  1500. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1501. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1502. for(y=0; y<8; y++)
  1503. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1504. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1505. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1506. }
  1507. }
  1508. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1509. // hmm
  1510. }
  1511. }
  1512. s->mbskip_table[mb_index]=0;
  1513. }
  1514. }
  1515. }
  1516. }
  1517. static inline int hpel_motion_lowres(MpegEncContext *s,
  1518. uint8_t *dest, uint8_t *src,
  1519. int field_based, int field_select,
  1520. int src_x, int src_y,
  1521. int width, int height, int stride,
  1522. int h_edge_pos, int v_edge_pos,
  1523. int w, int h, h264_chroma_mc_func *pix_op,
  1524. int motion_x, int motion_y)
  1525. {
  1526. const int lowres= s->avctx->lowres;
  1527. const int op_index= FFMIN(lowres, 2);
  1528. const int s_mask= (2<<lowres)-1;
  1529. int emu=0;
  1530. int sx, sy;
  1531. if(s->quarter_sample){
  1532. motion_x/=2;
  1533. motion_y/=2;
  1534. }
  1535. sx= motion_x & s_mask;
  1536. sy= motion_y & s_mask;
  1537. src_x += motion_x >> (lowres+1);
  1538. src_y += motion_y >> (lowres+1);
  1539. src += src_y * stride + src_x;
  1540. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1541. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1542. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1543. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1544. src= s->edge_emu_buffer;
  1545. emu=1;
  1546. }
  1547. sx= (sx << 2) >> lowres;
  1548. sy= (sy << 2) >> lowres;
  1549. if(field_select)
  1550. src += s->linesize;
  1551. pix_op[op_index](dest, src, stride, h, sx, sy);
  1552. return emu;
  1553. }
  1554. /* apply one mpeg motion vector to the three components */
  1555. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1556. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1557. int field_based, int bottom_field, int field_select,
  1558. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1559. int motion_x, int motion_y, int h, int mb_y)
  1560. {
  1561. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1562. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1563. const int lowres= s->avctx->lowres;
  1564. const int op_index= FFMIN(lowres, 2);
  1565. const int block_s= 8>>lowres;
  1566. const int s_mask= (2<<lowres)-1;
  1567. const int h_edge_pos = s->h_edge_pos >> lowres;
  1568. const int v_edge_pos = s->v_edge_pos >> lowres;
  1569. linesize = s->current_picture.f.linesize[0] << field_based;
  1570. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1571. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1572. motion_x/=2;
  1573. motion_y/=2;
  1574. }
  1575. if(field_based){
  1576. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1577. }
  1578. sx= motion_x & s_mask;
  1579. sy= motion_y & s_mask;
  1580. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1581. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1582. if (s->out_format == FMT_H263) {
  1583. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1584. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1585. uvsrc_x = src_x>>1;
  1586. uvsrc_y = src_y>>1;
  1587. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1588. mx = motion_x / 4;
  1589. my = motion_y / 4;
  1590. uvsx = (2*mx) & s_mask;
  1591. uvsy = (2*my) & s_mask;
  1592. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1593. uvsrc_y = mb_y*block_s + (my >> lowres);
  1594. } else {
  1595. mx = motion_x / 2;
  1596. my = motion_y / 2;
  1597. uvsx = mx & s_mask;
  1598. uvsy = my & s_mask;
  1599. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1600. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1601. }
  1602. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1603. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1604. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1605. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1606. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1607. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1608. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1609. ptr_y = s->edge_emu_buffer;
  1610. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1611. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1612. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1613. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1614. s->dsp.emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1615. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1616. ptr_cb= uvbuf;
  1617. ptr_cr= uvbuf+16;
  1618. }
  1619. }
  1620. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1621. dest_y += s->linesize;
  1622. dest_cb+= s->uvlinesize;
  1623. dest_cr+= s->uvlinesize;
  1624. }
  1625. if(field_select){
  1626. ptr_y += s->linesize;
  1627. ptr_cb+= s->uvlinesize;
  1628. ptr_cr+= s->uvlinesize;
  1629. }
  1630. sx= (sx << 2) >> lowres;
  1631. sy= (sy << 2) >> lowres;
  1632. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1633. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1634. uvsx= (uvsx << 2) >> lowres;
  1635. uvsy= (uvsy << 2) >> lowres;
  1636. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1637. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1638. }
  1639. //FIXME h261 lowres loop filter
  1640. }
  1641. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1642. uint8_t *dest_cb, uint8_t *dest_cr,
  1643. uint8_t **ref_picture,
  1644. h264_chroma_mc_func *pix_op,
  1645. int mx, int my){
  1646. const int lowres= s->avctx->lowres;
  1647. const int op_index= FFMIN(lowres, 2);
  1648. const int block_s= 8>>lowres;
  1649. const int s_mask= (2<<lowres)-1;
  1650. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1651. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1652. int emu=0, src_x, src_y, offset, sx, sy;
  1653. uint8_t *ptr;
  1654. if(s->quarter_sample){
  1655. mx/=2;
  1656. my/=2;
  1657. }
  1658. /* In case of 8X8, we construct a single chroma motion vector
  1659. with a special rounding */
  1660. mx= ff_h263_round_chroma(mx);
  1661. my= ff_h263_round_chroma(my);
  1662. sx= mx & s_mask;
  1663. sy= my & s_mask;
  1664. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1665. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1666. offset = src_y * s->uvlinesize + src_x;
  1667. ptr = ref_picture[1] + offset;
  1668. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1669. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1670. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1671. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1672. ptr= s->edge_emu_buffer;
  1673. emu=1;
  1674. }
  1675. }
  1676. sx= (sx << 2) >> lowres;
  1677. sy= (sy << 2) >> lowres;
  1678. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1679. ptr = ref_picture[2] + offset;
  1680. if(emu){
  1681. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1682. ptr= s->edge_emu_buffer;
  1683. }
  1684. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1685. }
  1686. /**
  1687. * motion compensation of a single macroblock
  1688. * @param s context
  1689. * @param dest_y luma destination pointer
  1690. * @param dest_cb chroma cb/u destination pointer
  1691. * @param dest_cr chroma cr/v destination pointer
  1692. * @param dir direction (0->forward, 1->backward)
  1693. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1694. * @param pix_op halfpel motion compensation function (average or put normally)
  1695. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1696. */
  1697. static inline void MPV_motion_lowres(MpegEncContext *s,
  1698. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1699. int dir, uint8_t **ref_picture,
  1700. h264_chroma_mc_func *pix_op)
  1701. {
  1702. int mx, my;
  1703. int mb_x, mb_y, i;
  1704. const int lowres= s->avctx->lowres;
  1705. const int block_s= 8>>lowres;
  1706. mb_x = s->mb_x;
  1707. mb_y = s->mb_y;
  1708. switch(s->mv_type) {
  1709. case MV_TYPE_16X16:
  1710. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1711. 0, 0, 0,
  1712. ref_picture, pix_op,
  1713. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1714. break;
  1715. case MV_TYPE_8X8:
  1716. mx = 0;
  1717. my = 0;
  1718. for(i=0;i<4;i++) {
  1719. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1720. ref_picture[0], 0, 0,
  1721. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1722. s->width, s->height, s->linesize,
  1723. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1724. block_s, block_s, pix_op,
  1725. s->mv[dir][i][0], s->mv[dir][i][1]);
  1726. mx += s->mv[dir][i][0];
  1727. my += s->mv[dir][i][1];
  1728. }
  1729. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1730. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1731. break;
  1732. case MV_TYPE_FIELD:
  1733. if (s->picture_structure == PICT_FRAME) {
  1734. /* top field */
  1735. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1736. 1, 0, s->field_select[dir][0],
  1737. ref_picture, pix_op,
  1738. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1739. /* bottom field */
  1740. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1741. 1, 1, s->field_select[dir][1],
  1742. ref_picture, pix_op,
  1743. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1744. } else {
  1745. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  1746. ref_picture = s->current_picture_ptr->f.data;
  1747. }
  1748. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1749. 0, 0, s->field_select[dir][0],
  1750. ref_picture, pix_op,
  1751. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1752. }
  1753. break;
  1754. case MV_TYPE_16X8:
  1755. for(i=0; i<2; i++){
  1756. uint8_t ** ref2picture;
  1757. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  1758. ref2picture= ref_picture;
  1759. }else{
  1760. ref2picture = s->current_picture_ptr->f.data;
  1761. }
  1762. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1763. 0, 0, s->field_select[dir][i],
  1764. ref2picture, pix_op,
  1765. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1766. dest_y += 2*block_s*s->linesize;
  1767. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1768. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1769. }
  1770. break;
  1771. case MV_TYPE_DMV:
  1772. if(s->picture_structure == PICT_FRAME){
  1773. for(i=0; i<2; i++){
  1774. int j;
  1775. for(j=0; j<2; j++){
  1776. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1777. 1, j, j^i,
  1778. ref_picture, pix_op,
  1779. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1780. }
  1781. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1782. }
  1783. }else{
  1784. for(i=0; i<2; i++){
  1785. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1786. 0, 0, s->picture_structure != i+1,
  1787. ref_picture, pix_op,
  1788. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1789. // after put we make avg of the same block
  1790. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1791. //opposite parity is always in the same frame if this is second field
  1792. if(!s->first_field){
  1793. ref_picture = s->current_picture_ptr->f.data;
  1794. }
  1795. }
  1796. }
  1797. break;
  1798. default: assert(0);
  1799. }
  1800. }
  1801. /**
  1802. * find the lowest MB row referenced in the MVs
  1803. */
  1804. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1805. {
  1806. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1807. int my, off, i, mvs;
  1808. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1809. switch (s->mv_type) {
  1810. case MV_TYPE_16X16:
  1811. mvs = 1;
  1812. break;
  1813. case MV_TYPE_16X8:
  1814. mvs = 2;
  1815. break;
  1816. case MV_TYPE_8X8:
  1817. mvs = 4;
  1818. break;
  1819. default:
  1820. goto unhandled;
  1821. }
  1822. for (i = 0; i < mvs; i++) {
  1823. my = s->mv[dir][i][1]<<qpel_shift;
  1824. my_max = FFMAX(my_max, my);
  1825. my_min = FFMIN(my_min, my);
  1826. }
  1827. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1828. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1829. unhandled:
  1830. return s->mb_height-1;
  1831. }
  1832. /* put block[] to dest[] */
  1833. static inline void put_dct(MpegEncContext *s,
  1834. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1835. {
  1836. s->dct_unquantize_intra(s, block, i, qscale);
  1837. s->dsp.idct_put (dest, line_size, block);
  1838. }
  1839. /* add block[] to dest[] */
  1840. static inline void add_dct(MpegEncContext *s,
  1841. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1842. {
  1843. if (s->block_last_index[i] >= 0) {
  1844. s->dsp.idct_add (dest, line_size, block);
  1845. }
  1846. }
  1847. static inline void add_dequant_dct(MpegEncContext *s,
  1848. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1849. {
  1850. if (s->block_last_index[i] >= 0) {
  1851. s->dct_unquantize_inter(s, block, i, qscale);
  1852. s->dsp.idct_add (dest, line_size, block);
  1853. }
  1854. }
  1855. /**
  1856. * Clean dc, ac, coded_block for the current non-intra MB.
  1857. */
  1858. void ff_clean_intra_table_entries(MpegEncContext *s)
  1859. {
  1860. int wrap = s->b8_stride;
  1861. int xy = s->block_index[0];
  1862. s->dc_val[0][xy ] =
  1863. s->dc_val[0][xy + 1 ] =
  1864. s->dc_val[0][xy + wrap] =
  1865. s->dc_val[0][xy + 1 + wrap] = 1024;
  1866. /* ac pred */
  1867. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1868. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1869. if (s->msmpeg4_version>=3) {
  1870. s->coded_block[xy ] =
  1871. s->coded_block[xy + 1 ] =
  1872. s->coded_block[xy + wrap] =
  1873. s->coded_block[xy + 1 + wrap] = 0;
  1874. }
  1875. /* chroma */
  1876. wrap = s->mb_stride;
  1877. xy = s->mb_x + s->mb_y * wrap;
  1878. s->dc_val[1][xy] =
  1879. s->dc_val[2][xy] = 1024;
  1880. /* ac pred */
  1881. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1882. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1883. s->mbintra_table[xy]= 0;
  1884. }
  1885. /* generic function called after a macroblock has been parsed by the
  1886. decoder or after it has been encoded by the encoder.
  1887. Important variables used:
  1888. s->mb_intra : true if intra macroblock
  1889. s->mv_dir : motion vector direction
  1890. s->mv_type : motion vector type
  1891. s->mv : motion vector
  1892. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1893. */
  1894. static av_always_inline
  1895. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1896. int lowres_flag, int is_mpeg12)
  1897. {
  1898. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1899. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1900. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1901. return;
  1902. }
  1903. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1904. /* save DCT coefficients */
  1905. int i,j;
  1906. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  1907. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1908. for(i=0; i<6; i++){
  1909. for(j=0; j<64; j++){
  1910. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1911. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1912. }
  1913. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1914. }
  1915. }
  1916. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  1917. /* update DC predictors for P macroblocks */
  1918. if (!s->mb_intra) {
  1919. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1920. if(s->mbintra_table[mb_xy])
  1921. ff_clean_intra_table_entries(s);
  1922. } else {
  1923. s->last_dc[0] =
  1924. s->last_dc[1] =
  1925. s->last_dc[2] = 128 << s->intra_dc_precision;
  1926. }
  1927. }
  1928. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1929. s->mbintra_table[mb_xy]=1;
  1930. 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
  1931. uint8_t *dest_y, *dest_cb, *dest_cr;
  1932. int dct_linesize, dct_offset;
  1933. op_pixels_func (*op_pix)[4];
  1934. qpel_mc_func (*op_qpix)[16];
  1935. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1936. const int uvlinesize = s->current_picture.f.linesize[1];
  1937. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1938. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1939. /* avoid copy if macroblock skipped in last frame too */
  1940. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1941. if(!s->encoding){
  1942. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1943. if (s->mb_skipped) {
  1944. s->mb_skipped= 0;
  1945. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1946. *mbskip_ptr = 1;
  1947. } else if(!s->current_picture.f.reference) {
  1948. *mbskip_ptr = 1;
  1949. } else{
  1950. *mbskip_ptr = 0; /* not skipped */
  1951. }
  1952. }
  1953. dct_linesize = linesize << s->interlaced_dct;
  1954. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1955. if(readable){
  1956. dest_y= s->dest[0];
  1957. dest_cb= s->dest[1];
  1958. dest_cr= s->dest[2];
  1959. }else{
  1960. dest_y = s->b_scratchpad;
  1961. dest_cb= s->b_scratchpad+16*linesize;
  1962. dest_cr= s->b_scratchpad+32*linesize;
  1963. }
  1964. if (!s->mb_intra) {
  1965. /* motion handling */
  1966. /* decoding or more than one mb_type (MC was already done otherwise) */
  1967. if(!s->encoding){
  1968. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1969. if (s->mv_dir & MV_DIR_FORWARD) {
  1970. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  1971. }
  1972. if (s->mv_dir & MV_DIR_BACKWARD) {
  1973. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  1974. }
  1975. }
  1976. if(lowres_flag){
  1977. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1978. if (s->mv_dir & MV_DIR_FORWARD) {
  1979. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  1980. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1981. }
  1982. if (s->mv_dir & MV_DIR_BACKWARD) {
  1983. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  1984. }
  1985. }else{
  1986. op_qpix= s->me.qpel_put;
  1987. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1988. op_pix = s->dsp.put_pixels_tab;
  1989. }else{
  1990. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1991. }
  1992. if (s->mv_dir & MV_DIR_FORWARD) {
  1993. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1994. op_pix = s->dsp.avg_pixels_tab;
  1995. op_qpix= s->me.qpel_avg;
  1996. }
  1997. if (s->mv_dir & MV_DIR_BACKWARD) {
  1998. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1999. }
  2000. }
  2001. }
  2002. /* skip dequant / idct if we are really late ;) */
  2003. if(s->avctx->skip_idct){
  2004. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2005. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2006. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2007. goto skip_idct;
  2008. }
  2009. /* add dct residue */
  2010. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2011. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2012. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2013. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2014. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2015. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2016. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2017. if (s->chroma_y_shift){
  2018. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2019. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2020. }else{
  2021. dct_linesize >>= 1;
  2022. dct_offset >>=1;
  2023. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2024. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2025. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2026. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2027. }
  2028. }
  2029. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  2030. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2031. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2032. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2033. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2034. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2035. if(s->chroma_y_shift){//Chroma420
  2036. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2037. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2038. }else{
  2039. //chroma422
  2040. dct_linesize = uvlinesize << s->interlaced_dct;
  2041. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  2042. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2043. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2044. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2045. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2046. if(!s->chroma_x_shift){//Chroma444
  2047. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  2048. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  2049. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  2050. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  2051. }
  2052. }
  2053. }//fi gray
  2054. }
  2055. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2056. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2057. }
  2058. } else {
  2059. /* dct only in intra block */
  2060. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2061. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2062. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2063. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2064. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2065. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2066. if(s->chroma_y_shift){
  2067. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2068. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2069. }else{
  2070. dct_offset >>=1;
  2071. dct_linesize >>=1;
  2072. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2073. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2074. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2075. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2076. }
  2077. }
  2078. }else{
  2079. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2080. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2081. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2082. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2083. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2084. if(s->chroma_y_shift){
  2085. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2086. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2087. }else{
  2088. dct_linesize = uvlinesize << s->interlaced_dct;
  2089. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  2090. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2091. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2092. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2093. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2094. if(!s->chroma_x_shift){//Chroma444
  2095. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2096. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2097. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2098. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2099. }
  2100. }
  2101. }//gray
  2102. }
  2103. }
  2104. skip_idct:
  2105. if(!readable){
  2106. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2107. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2108. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2109. }
  2110. }
  2111. }
  2112. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  2113. #if !CONFIG_SMALL
  2114. if(s->out_format == FMT_MPEG1) {
  2115. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2116. else MPV_decode_mb_internal(s, block, 0, 1);
  2117. } else
  2118. #endif
  2119. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2120. else MPV_decode_mb_internal(s, block, 0, 0);
  2121. }
  2122. /**
  2123. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2124. */
  2125. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2126. const int field_pic= s->picture_structure != PICT_FRAME;
  2127. if(field_pic){
  2128. h <<= 1;
  2129. y <<= 1;
  2130. }
  2131. if (!s->avctx->hwaccel
  2132. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2133. && s->unrestricted_mv
  2134. && s->current_picture.f.reference
  2135. && !s->intra_only
  2136. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2137. int sides = 0, edge_h;
  2138. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  2139. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  2140. if (y==0) sides |= EDGE_TOP;
  2141. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2142. edge_h= FFMIN(h, s->v_edge_pos - y);
  2143. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  2144. s->linesize, s->h_edge_pos, edge_h,
  2145. EDGE_WIDTH, EDGE_WIDTH, sides);
  2146. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  2147. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2148. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2149. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  2150. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2151. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2152. }
  2153. h= FFMIN(h, s->avctx->height - y);
  2154. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2155. if (s->avctx->draw_horiz_band) {
  2156. AVFrame *src;
  2157. int offset[AV_NUM_DATA_POINTERS];
  2158. int i;
  2159. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2160. src= (AVFrame*)s->current_picture_ptr;
  2161. else if(s->last_picture_ptr)
  2162. src= (AVFrame*)s->last_picture_ptr;
  2163. else
  2164. return;
  2165. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2166. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2167. offset[i] = 0;
  2168. }else{
  2169. offset[0]= y * s->linesize;
  2170. offset[1]=
  2171. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2172. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2173. offset[i] = 0;
  2174. }
  2175. emms_c();
  2176. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2177. y, s->picture_structure, h);
  2178. }
  2179. }
  2180. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2181. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2182. const int uvlinesize = s->current_picture.f.linesize[1];
  2183. const int mb_size= 4 - s->avctx->lowres;
  2184. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2185. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2186. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2187. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2188. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2189. 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;
  2190. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2191. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2192. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2193. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2194. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2195. {
  2196. if(s->picture_structure==PICT_FRAME){
  2197. s->dest[0] += s->mb_y * linesize << mb_size;
  2198. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2199. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2200. }else{
  2201. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2202. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2203. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2204. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2205. }
  2206. }
  2207. }
  2208. void ff_mpeg_flush(AVCodecContext *avctx){
  2209. int i;
  2210. MpegEncContext *s = avctx->priv_data;
  2211. if(s==NULL || s->picture==NULL)
  2212. return;
  2213. for(i=0; i<s->picture_count; i++){
  2214. if (s->picture[i].f.data[0] &&
  2215. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2216. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2217. free_frame_buffer(s, &s->picture[i]);
  2218. }
  2219. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2220. s->mb_x= s->mb_y= 0;
  2221. s->parse_context.state= -1;
  2222. s->parse_context.frame_start_found= 0;
  2223. s->parse_context.overread= 0;
  2224. s->parse_context.overread_index= 0;
  2225. s->parse_context.index= 0;
  2226. s->parse_context.last_index= 0;
  2227. s->bitstream_buffer_size=0;
  2228. s->pp_time=0;
  2229. }
  2230. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2231. DCTELEM *block, int n, int qscale)
  2232. {
  2233. int i, level, nCoeffs;
  2234. const uint16_t *quant_matrix;
  2235. nCoeffs= s->block_last_index[n];
  2236. if (n < 4)
  2237. block[0] = block[0] * s->y_dc_scale;
  2238. else
  2239. block[0] = block[0] * s->c_dc_scale;
  2240. /* XXX: only mpeg1 */
  2241. quant_matrix = s->intra_matrix;
  2242. for(i=1;i<=nCoeffs;i++) {
  2243. int j= s->intra_scantable.permutated[i];
  2244. level = block[j];
  2245. if (level) {
  2246. if (level < 0) {
  2247. level = -level;
  2248. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2249. level = (level - 1) | 1;
  2250. level = -level;
  2251. } else {
  2252. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2253. level = (level - 1) | 1;
  2254. }
  2255. block[j] = level;
  2256. }
  2257. }
  2258. }
  2259. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2260. DCTELEM *block, int n, int qscale)
  2261. {
  2262. int i, level, nCoeffs;
  2263. const uint16_t *quant_matrix;
  2264. nCoeffs= s->block_last_index[n];
  2265. quant_matrix = s->inter_matrix;
  2266. for(i=0; i<=nCoeffs; i++) {
  2267. int j= s->intra_scantable.permutated[i];
  2268. level = block[j];
  2269. if (level) {
  2270. if (level < 0) {
  2271. level = -level;
  2272. level = (((level << 1) + 1) * qscale *
  2273. ((int) (quant_matrix[j]))) >> 4;
  2274. level = (level - 1) | 1;
  2275. level = -level;
  2276. } else {
  2277. level = (((level << 1) + 1) * qscale *
  2278. ((int) (quant_matrix[j]))) >> 4;
  2279. level = (level - 1) | 1;
  2280. }
  2281. block[j] = level;
  2282. }
  2283. }
  2284. }
  2285. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2286. DCTELEM *block, int n, int qscale)
  2287. {
  2288. int i, level, nCoeffs;
  2289. const uint16_t *quant_matrix;
  2290. if(s->alternate_scan) nCoeffs= 63;
  2291. else nCoeffs= s->block_last_index[n];
  2292. if (n < 4)
  2293. block[0] = block[0] * s->y_dc_scale;
  2294. else
  2295. block[0] = block[0] * s->c_dc_scale;
  2296. quant_matrix = s->intra_matrix;
  2297. for(i=1;i<=nCoeffs;i++) {
  2298. int j= s->intra_scantable.permutated[i];
  2299. level = block[j];
  2300. if (level) {
  2301. if (level < 0) {
  2302. level = -level;
  2303. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2304. level = -level;
  2305. } else {
  2306. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2307. }
  2308. block[j] = level;
  2309. }
  2310. }
  2311. }
  2312. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2313. DCTELEM *block, int n, int qscale)
  2314. {
  2315. int i, level, nCoeffs;
  2316. const uint16_t *quant_matrix;
  2317. int sum=-1;
  2318. if(s->alternate_scan) nCoeffs= 63;
  2319. else nCoeffs= s->block_last_index[n];
  2320. if (n < 4)
  2321. block[0] = block[0] * s->y_dc_scale;
  2322. else
  2323. block[0] = block[0] * s->c_dc_scale;
  2324. quant_matrix = s->intra_matrix;
  2325. for(i=1;i<=nCoeffs;i++) {
  2326. int j= s->intra_scantable.permutated[i];
  2327. level = block[j];
  2328. if (level) {
  2329. if (level < 0) {
  2330. level = -level;
  2331. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2332. level = -level;
  2333. } else {
  2334. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2335. }
  2336. block[j] = level;
  2337. sum+=level;
  2338. }
  2339. }
  2340. block[63]^=sum&1;
  2341. }
  2342. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2343. DCTELEM *block, int n, int qscale)
  2344. {
  2345. int i, level, nCoeffs;
  2346. const uint16_t *quant_matrix;
  2347. int sum=-1;
  2348. if(s->alternate_scan) nCoeffs= 63;
  2349. else nCoeffs= s->block_last_index[n];
  2350. quant_matrix = s->inter_matrix;
  2351. for(i=0; i<=nCoeffs; i++) {
  2352. int j= s->intra_scantable.permutated[i];
  2353. level = block[j];
  2354. if (level) {
  2355. if (level < 0) {
  2356. level = -level;
  2357. level = (((level << 1) + 1) * qscale *
  2358. ((int) (quant_matrix[j]))) >> 4;
  2359. level = -level;
  2360. } else {
  2361. level = (((level << 1) + 1) * qscale *
  2362. ((int) (quant_matrix[j]))) >> 4;
  2363. }
  2364. block[j] = level;
  2365. sum+=level;
  2366. }
  2367. }
  2368. block[63]^=sum&1;
  2369. }
  2370. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2371. DCTELEM *block, int n, int qscale)
  2372. {
  2373. int i, level, qmul, qadd;
  2374. int nCoeffs;
  2375. assert(s->block_last_index[n]>=0);
  2376. qmul = qscale << 1;
  2377. if (!s->h263_aic) {
  2378. if (n < 4)
  2379. block[0] = block[0] * s->y_dc_scale;
  2380. else
  2381. block[0] = block[0] * s->c_dc_scale;
  2382. qadd = (qscale - 1) | 1;
  2383. }else{
  2384. qadd = 0;
  2385. }
  2386. if(s->ac_pred)
  2387. nCoeffs=63;
  2388. else
  2389. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2390. for(i=1; i<=nCoeffs; i++) {
  2391. level = block[i];
  2392. if (level) {
  2393. if (level < 0) {
  2394. level = level * qmul - qadd;
  2395. } else {
  2396. level = level * qmul + qadd;
  2397. }
  2398. block[i] = level;
  2399. }
  2400. }
  2401. }
  2402. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2403. DCTELEM *block, int n, int qscale)
  2404. {
  2405. int i, level, qmul, qadd;
  2406. int nCoeffs;
  2407. assert(s->block_last_index[n]>=0);
  2408. qadd = (qscale - 1) | 1;
  2409. qmul = qscale << 1;
  2410. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2411. for(i=0; i<=nCoeffs; i++) {
  2412. level = block[i];
  2413. if (level) {
  2414. if (level < 0) {
  2415. level = level * qmul - qadd;
  2416. } else {
  2417. level = level * qmul + qadd;
  2418. }
  2419. block[i] = level;
  2420. }
  2421. }
  2422. }
  2423. /**
  2424. * set qscale and update qscale dependent variables.
  2425. */
  2426. void ff_set_qscale(MpegEncContext * s, int qscale)
  2427. {
  2428. if (qscale < 1)
  2429. qscale = 1;
  2430. else if (qscale > 31)
  2431. qscale = 31;
  2432. s->qscale = qscale;
  2433. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2434. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2435. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2436. }
  2437. void MPV_report_decode_progress(MpegEncContext *s)
  2438. {
  2439. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2440. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2441. }