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.

2882 lines
109KB

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