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.

3060 lines
114KB

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