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.

3127 lines
116KB

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