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.

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