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.

2545 lines
95KB

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