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.

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