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.

2889 lines
110KB

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