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.

2726 lines
103KB

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