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.

2893 lines
110KB

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