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.

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