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.

2892 lines
110KB

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