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.

3139 lines
117KB

  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. int h_chroma_shift, v_chroma_shift;
  1270. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1271. &h_chroma_shift, &v_chroma_shift);
  1272. if (s->pict_type != AV_PICTURE_TYPE_I)
  1273. av_log(avctx, AV_LOG_ERROR,
  1274. "warning: first frame is no keyframe\n");
  1275. else if (s->picture_structure != PICT_FRAME)
  1276. av_log(avctx, AV_LOG_INFO,
  1277. "allocate dummy last picture for field based first keyframe\n");
  1278. /* Allocate a dummy frame */
  1279. i = ff_find_unused_picture(s, 0);
  1280. if (i < 0) {
  1281. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1282. return i;
  1283. }
  1284. s->last_picture_ptr = &s->picture[i];
  1285. s->last_picture_ptr->f.key_frame = 0;
  1286. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1287. s->last_picture_ptr = NULL;
  1288. return -1;
  1289. }
  1290. memset(s->last_picture_ptr->f.data[0], 0x80,
  1291. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1292. memset(s->last_picture_ptr->f.data[1], 0x80,
  1293. (avctx->height >> v_chroma_shift) *
  1294. s->last_picture_ptr->f.linesize[1]);
  1295. memset(s->last_picture_ptr->f.data[2], 0x80,
  1296. (avctx->height >> v_chroma_shift) *
  1297. s->last_picture_ptr->f.linesize[2]);
  1298. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1299. for(i=0; i<avctx->height; i++)
  1300. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1301. }
  1302. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
  1303. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
  1304. s->last_picture_ptr->f.reference = 3;
  1305. }
  1306. if ((s->next_picture_ptr == NULL ||
  1307. s->next_picture_ptr->f.data[0] == NULL) &&
  1308. s->pict_type == AV_PICTURE_TYPE_B) {
  1309. /* Allocate a dummy frame */
  1310. i = ff_find_unused_picture(s, 0);
  1311. if (i < 0) {
  1312. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1313. return i;
  1314. }
  1315. s->next_picture_ptr = &s->picture[i];
  1316. s->next_picture_ptr->f.key_frame = 0;
  1317. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1318. s->next_picture_ptr = NULL;
  1319. return -1;
  1320. }
  1321. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
  1322. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
  1323. s->next_picture_ptr->f.reference = 3;
  1324. }
  1325. }
  1326. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1327. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1328. if (s->last_picture_ptr)
  1329. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1330. if (s->next_picture_ptr)
  1331. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1332. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1333. if (s->next_picture_ptr)
  1334. s->next_picture_ptr->owner2 = s;
  1335. if (s->last_picture_ptr)
  1336. s->last_picture_ptr->owner2 = s;
  1337. }
  1338. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1339. s->last_picture_ptr->f.data[0]));
  1340. if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
  1341. int i;
  1342. for (i = 0; i < 4; i++) {
  1343. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1344. s->current_picture.f.data[i] +=
  1345. s->current_picture.f.linesize[i];
  1346. }
  1347. s->current_picture.f.linesize[i] *= 2;
  1348. s->last_picture.f.linesize[i] *= 2;
  1349. s->next_picture.f.linesize[i] *= 2;
  1350. }
  1351. }
  1352. s->err_recognition = avctx->err_recognition;
  1353. /* set dequantizer, we can't do it during init as
  1354. * it might change for mpeg4 and we can't do it in the header
  1355. * decode as init is not called for mpeg4 there yet */
  1356. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1357. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1358. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1359. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1360. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1361. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1362. } else {
  1363. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1364. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1365. }
  1366. if (s->dct_error_sum) {
  1367. assert(s->avctx->noise_reduction && s->encoding);
  1368. update_noise_reduction(s);
  1369. }
  1370. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1371. return ff_xvmc_field_start(s, avctx);
  1372. return 0;
  1373. }
  1374. /* generic function for encode/decode called after a
  1375. * frame has been coded/decoded. */
  1376. void ff_MPV_frame_end(MpegEncContext *s)
  1377. {
  1378. int i;
  1379. /* redraw edges for the frame if decoding didn't complete */
  1380. // just to make sure that all data is rendered.
  1381. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1382. ff_xvmc_field_end(s);
  1383. } else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1384. !s->avctx->hwaccel &&
  1385. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1386. s->unrestricted_mv &&
  1387. s->current_picture.f.reference &&
  1388. !s->intra_only &&
  1389. !(s->flags & CODEC_FLAG_EMU_EDGE) &&
  1390. !s->avctx->lowres
  1391. ) {
  1392. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1393. int hshift = desc->log2_chroma_w;
  1394. int vshift = desc->log2_chroma_h;
  1395. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1396. s->h_edge_pos, s->v_edge_pos,
  1397. EDGE_WIDTH, EDGE_WIDTH,
  1398. EDGE_TOP | EDGE_BOTTOM);
  1399. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1400. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1401. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1402. EDGE_TOP | EDGE_BOTTOM);
  1403. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1404. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1405. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1406. EDGE_TOP | EDGE_BOTTOM);
  1407. }
  1408. emms_c();
  1409. s->last_pict_type = s->pict_type;
  1410. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1411. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1412. s->last_non_b_pict_type = s->pict_type;
  1413. }
  1414. #if 0
  1415. /* copy back current_picture variables */
  1416. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1417. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1418. s->picture[i] = s->current_picture;
  1419. break;
  1420. }
  1421. }
  1422. assert(i < MAX_PICTURE_COUNT);
  1423. #endif
  1424. if (s->encoding) {
  1425. /* release non-reference frames */
  1426. for (i = 0; i < s->picture_count; i++) {
  1427. if (s->picture[i].f.data[0] && !s->picture[i].f.reference
  1428. /* && s->picture[i].type != FF_BUFFER_TYPE_SHARED */) {
  1429. free_frame_buffer(s, &s->picture[i]);
  1430. }
  1431. }
  1432. }
  1433. // clear copies, to avoid confusion
  1434. #if 0
  1435. memset(&s->last_picture, 0, sizeof(Picture));
  1436. memset(&s->next_picture, 0, sizeof(Picture));
  1437. memset(&s->current_picture, 0, sizeof(Picture));
  1438. #endif
  1439. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1440. if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {
  1441. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  1442. }
  1443. }
  1444. /**
  1445. * Draw a line from (ex, ey) -> (sx, sy).
  1446. * @param w width of the image
  1447. * @param h height of the image
  1448. * @param stride stride/linesize of the image
  1449. * @param color color of the arrow
  1450. */
  1451. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1452. int w, int h, int stride, int color)
  1453. {
  1454. int x, y, fr, f;
  1455. sx = av_clip(sx, 0, w - 1);
  1456. sy = av_clip(sy, 0, h - 1);
  1457. ex = av_clip(ex, 0, w - 1);
  1458. ey = av_clip(ey, 0, h - 1);
  1459. buf[sy * stride + sx] += color;
  1460. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1461. if (sx > ex) {
  1462. FFSWAP(int, sx, ex);
  1463. FFSWAP(int, sy, ey);
  1464. }
  1465. buf += sx + sy * stride;
  1466. ex -= sx;
  1467. f = ((ey - sy) << 16) / ex;
  1468. for (x = 0; x <= ex; x++) {
  1469. y = (x * f) >> 16;
  1470. fr = (x * f) & 0xFFFF;
  1471. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1472. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1473. }
  1474. } else {
  1475. if (sy > ey) {
  1476. FFSWAP(int, sx, ex);
  1477. FFSWAP(int, sy, ey);
  1478. }
  1479. buf += sx + sy * stride;
  1480. ey -= sy;
  1481. if (ey)
  1482. f = ((ex - sx) << 16) / ey;
  1483. else
  1484. f = 0;
  1485. for(y= 0; y <= ey; y++){
  1486. x = (y*f) >> 16;
  1487. fr = (y*f) & 0xFFFF;
  1488. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1489. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1490. }
  1491. }
  1492. }
  1493. /**
  1494. * Draw an arrow from (ex, ey) -> (sx, sy).
  1495. * @param w width of the image
  1496. * @param h height of the image
  1497. * @param stride stride/linesize of the image
  1498. * @param color color of the arrow
  1499. */
  1500. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1501. int ey, int w, int h, int stride, int color)
  1502. {
  1503. int dx,dy;
  1504. sx = av_clip(sx, -100, w + 100);
  1505. sy = av_clip(sy, -100, h + 100);
  1506. ex = av_clip(ex, -100, w + 100);
  1507. ey = av_clip(ey, -100, h + 100);
  1508. dx = ex - sx;
  1509. dy = ey - sy;
  1510. if (dx * dx + dy * dy > 3 * 3) {
  1511. int rx = dx + dy;
  1512. int ry = -dx + dy;
  1513. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1514. // FIXME subpixel accuracy
  1515. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1516. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1517. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1518. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1519. }
  1520. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1521. }
  1522. /**
  1523. * Print debugging info for the given picture.
  1524. */
  1525. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
  1526. {
  1527. if ( s->avctx->hwaccel || !pict || !pict->mb_type
  1528. || (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1529. return;
  1530. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1531. int x,y;
  1532. av_log(s->avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1533. av_get_picture_type_char(pict->pict_type));
  1534. for (y = 0; y < s->mb_height; y++) {
  1535. for (x = 0; x < s->mb_width; x++) {
  1536. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1537. int count = s->mbskip_table[x + y * s->mb_stride];
  1538. if (count > 9)
  1539. count = 9;
  1540. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1541. }
  1542. if (s->avctx->debug & FF_DEBUG_QP) {
  1543. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1544. pict->qscale_table[x + y * s->mb_stride]);
  1545. }
  1546. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1547. int mb_type = pict->mb_type[x + y * s->mb_stride];
  1548. // Type & MV direction
  1549. if (IS_PCM(mb_type))
  1550. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1551. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1552. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1553. else if (IS_INTRA4x4(mb_type))
  1554. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1555. else if (IS_INTRA16x16(mb_type))
  1556. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1557. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1558. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1559. else if (IS_DIRECT(mb_type))
  1560. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1561. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1562. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1563. else if (IS_GMC(mb_type))
  1564. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1565. else if (IS_SKIP(mb_type))
  1566. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1567. else if (!USES_LIST(mb_type, 1))
  1568. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1569. else if (!USES_LIST(mb_type, 0))
  1570. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1571. else {
  1572. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1573. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1574. }
  1575. // segmentation
  1576. if (IS_8X8(mb_type))
  1577. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1578. else if (IS_16X8(mb_type))
  1579. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1580. else if (IS_8X16(mb_type))
  1581. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1582. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1583. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1584. else
  1585. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1586. if (IS_INTERLACED(mb_type))
  1587. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1588. else
  1589. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1590. }
  1591. }
  1592. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1593. }
  1594. }
  1595. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1596. (s->avctx->debug_mv)) {
  1597. const int shift = 1 + s->quarter_sample;
  1598. int mb_y;
  1599. uint8_t *ptr;
  1600. int i;
  1601. int h_chroma_shift, v_chroma_shift, block_height;
  1602. const int width = s->avctx->width;
  1603. const int height = s->avctx->height;
  1604. const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
  1605. const int mv_stride = (s->mb_width << mv_sample_log2) +
  1606. (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
  1607. s->low_delay = 0; // needed to see the vectors without trashing the buffers
  1608. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1609. for (i = 0; i < 3; i++) {
  1610. size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
  1611. pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
  1612. s->visualization_buffer[i]= av_realloc(s->visualization_buffer[i], size);
  1613. memcpy(s->visualization_buffer[i], pict->data[i], size);
  1614. pict->data[i] = s->visualization_buffer[i];
  1615. }
  1616. pict->type = FF_BUFFER_TYPE_COPY;
  1617. pict->opaque= NULL;
  1618. ptr = pict->data[0];
  1619. block_height = 16 >> v_chroma_shift;
  1620. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1621. int mb_x;
  1622. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1623. const int mb_index = mb_x + mb_y * s->mb_stride;
  1624. if ((s->avctx->debug_mv) && pict->motion_val[0]) {
  1625. int type;
  1626. for (type = 0; type < 3; type++) {
  1627. int direction = 0;
  1628. switch (type) {
  1629. case 0:
  1630. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1631. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1632. continue;
  1633. direction = 0;
  1634. break;
  1635. case 1:
  1636. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1637. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1638. continue;
  1639. direction = 0;
  1640. break;
  1641. case 2:
  1642. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1643. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1644. continue;
  1645. direction = 1;
  1646. break;
  1647. }
  1648. if (!USES_LIST(pict->mb_type[mb_index], direction))
  1649. continue;
  1650. if (IS_8X8(pict->mb_type[mb_index])) {
  1651. int i;
  1652. for (i = 0; i < 4; i++) {
  1653. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1654. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1655. int xy = (mb_x * 2 + (i & 1) +
  1656. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1657. int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
  1658. int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
  1659. draw_arrow(ptr, sx, sy, mx, my, width,
  1660. height, s->linesize, 100);
  1661. }
  1662. } else if (IS_16X8(pict->mb_type[mb_index])) {
  1663. int i;
  1664. for (i = 0; i < 2; i++) {
  1665. int sx = mb_x * 16 + 8;
  1666. int sy = mb_y * 16 + 4 + 8 * i;
  1667. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1668. int mx = (pict->motion_val[direction][xy][0] >> shift);
  1669. int my = (pict->motion_val[direction][xy][1] >> shift);
  1670. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1671. my *= 2;
  1672. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1673. height, s->linesize, 100);
  1674. }
  1675. } else if (IS_8X16(pict->mb_type[mb_index])) {
  1676. int i;
  1677. for (i = 0; i < 2; i++) {
  1678. int sx = mb_x * 16 + 4 + 8 * i;
  1679. int sy = mb_y * 16 + 8;
  1680. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1681. int mx = pict->motion_val[direction][xy][0] >> shift;
  1682. int my = pict->motion_val[direction][xy][1] >> shift;
  1683. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1684. my *= 2;
  1685. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1686. height, s->linesize, 100);
  1687. }
  1688. } else {
  1689. int sx= mb_x * 16 + 8;
  1690. int sy= mb_y * 16 + 8;
  1691. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1692. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1693. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1694. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1695. }
  1696. }
  1697. }
  1698. if ((s->avctx->debug & FF_DEBUG_VIS_QP)) {
  1699. uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
  1700. 0x0101010101010101ULL;
  1701. int y;
  1702. for (y = 0; y < block_height; y++) {
  1703. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1704. (block_height * mb_y + y) *
  1705. pict->linesize[1]) = c;
  1706. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1707. (block_height * mb_y + y) *
  1708. pict->linesize[2]) = c;
  1709. }
  1710. }
  1711. if ((s->avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1712. pict->motion_val[0]) {
  1713. int mb_type = pict->mb_type[mb_index];
  1714. uint64_t u,v;
  1715. int y;
  1716. #define COLOR(theta, r) \
  1717. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1718. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1719. u = v = 128;
  1720. if (IS_PCM(mb_type)) {
  1721. COLOR(120, 48)
  1722. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1723. IS_INTRA16x16(mb_type)) {
  1724. COLOR(30, 48)
  1725. } else if (IS_INTRA4x4(mb_type)) {
  1726. COLOR(90, 48)
  1727. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1728. // COLOR(120, 48)
  1729. } else if (IS_DIRECT(mb_type)) {
  1730. COLOR(150, 48)
  1731. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1732. COLOR(170, 48)
  1733. } else if (IS_GMC(mb_type)) {
  1734. COLOR(190, 48)
  1735. } else if (IS_SKIP(mb_type)) {
  1736. // COLOR(180, 48)
  1737. } else if (!USES_LIST(mb_type, 1)) {
  1738. COLOR(240, 48)
  1739. } else if (!USES_LIST(mb_type, 0)) {
  1740. COLOR(0, 48)
  1741. } else {
  1742. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1743. COLOR(300,48)
  1744. }
  1745. u *= 0x0101010101010101ULL;
  1746. v *= 0x0101010101010101ULL;
  1747. for (y = 0; y < block_height; y++) {
  1748. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1749. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1750. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1751. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1752. }
  1753. // segmentation
  1754. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1755. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1756. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1757. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1758. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1759. }
  1760. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1761. for (y = 0; y < 16; y++)
  1762. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1763. pict->linesize[0]] ^= 0x80;
  1764. }
  1765. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1766. int dm = 1 << (mv_sample_log2 - 2);
  1767. for (i = 0; i < 4; i++) {
  1768. int sx = mb_x * 16 + 8 * (i & 1);
  1769. int sy = mb_y * 16 + 8 * (i >> 1);
  1770. int xy = (mb_x * 2 + (i & 1) +
  1771. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1772. // FIXME bidir
  1773. int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
  1774. if (mv[0] != mv[dm] ||
  1775. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1776. for (y = 0; y < 8; y++)
  1777. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1778. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1779. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1780. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1781. }
  1782. }
  1783. if (IS_INTERLACED(mb_type) &&
  1784. s->codec_id == AV_CODEC_ID_H264) {
  1785. // hmm
  1786. }
  1787. }
  1788. s->mbskip_table[mb_index] = 0;
  1789. }
  1790. }
  1791. }
  1792. }
  1793. static inline int hpel_motion_lowres(MpegEncContext *s,
  1794. uint8_t *dest, uint8_t *src,
  1795. int field_based, int field_select,
  1796. int src_x, int src_y,
  1797. int width, int height, int stride,
  1798. int h_edge_pos, int v_edge_pos,
  1799. int w, int h, h264_chroma_mc_func *pix_op,
  1800. int motion_x, int motion_y)
  1801. {
  1802. const int lowres = s->avctx->lowres;
  1803. const int op_index = FFMIN(lowres, 2);
  1804. const int s_mask = (2 << lowres) - 1;
  1805. int emu = 0;
  1806. int sx, sy;
  1807. if (s->quarter_sample) {
  1808. motion_x /= 2;
  1809. motion_y /= 2;
  1810. }
  1811. sx = motion_x & s_mask;
  1812. sy = motion_y & s_mask;
  1813. src_x += motion_x >> lowres + 1;
  1814. src_y += motion_y >> lowres + 1;
  1815. src += src_y * stride + src_x;
  1816. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1817. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1818. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
  1819. (h + 1) << field_based, src_x,
  1820. src_y << field_based,
  1821. h_edge_pos,
  1822. v_edge_pos);
  1823. src = s->edge_emu_buffer;
  1824. emu = 1;
  1825. }
  1826. sx = (sx << 2) >> lowres;
  1827. sy = (sy << 2) >> lowres;
  1828. if (field_select)
  1829. src += s->linesize;
  1830. pix_op[op_index](dest, src, stride, h, sx, sy);
  1831. return emu;
  1832. }
  1833. /* apply one mpeg motion vector to the three components */
  1834. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1835. uint8_t *dest_y,
  1836. uint8_t *dest_cb,
  1837. uint8_t *dest_cr,
  1838. int field_based,
  1839. int bottom_field,
  1840. int field_select,
  1841. uint8_t **ref_picture,
  1842. h264_chroma_mc_func *pix_op,
  1843. int motion_x, int motion_y,
  1844. int h, int mb_y)
  1845. {
  1846. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1847. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
  1848. uvsx, uvsy;
  1849. const int lowres = s->avctx->lowres;
  1850. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 2);
  1851. const int block_s = 8>>lowres;
  1852. const int s_mask = (2 << lowres) - 1;
  1853. const int h_edge_pos = s->h_edge_pos >> lowres;
  1854. const int v_edge_pos = s->v_edge_pos >> lowres;
  1855. linesize = s->current_picture.f.linesize[0] << field_based;
  1856. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1857. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1858. if (s->quarter_sample) {
  1859. motion_x /= 2;
  1860. motion_y /= 2;
  1861. }
  1862. if(field_based){
  1863. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1864. }
  1865. sx = motion_x & s_mask;
  1866. sy = motion_y & s_mask;
  1867. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1868. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1869. if (s->out_format == FMT_H263) {
  1870. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1871. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1872. uvsrc_x = src_x >> 1;
  1873. uvsrc_y = src_y >> 1;
  1874. } else if (s->out_format == FMT_H261) {
  1875. // even chroma mv's are full pel in H261
  1876. mx = motion_x / 4;
  1877. my = motion_y / 4;
  1878. uvsx = (2 * mx) & s_mask;
  1879. uvsy = (2 * my) & s_mask;
  1880. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1881. uvsrc_y = mb_y * block_s + (my >> lowres);
  1882. } else {
  1883. if(s->chroma_y_shift){
  1884. mx = motion_x / 2;
  1885. my = motion_y / 2;
  1886. uvsx = mx & s_mask;
  1887. uvsy = my & s_mask;
  1888. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1889. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1890. } else {
  1891. if(s->chroma_x_shift){
  1892. //Chroma422
  1893. mx = motion_x / 2;
  1894. uvsx = mx & s_mask;
  1895. uvsy = motion_y & s_mask;
  1896. uvsrc_y = src_y;
  1897. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1898. } else {
  1899. //Chroma444
  1900. uvsx = motion_x & s_mask;
  1901. uvsy = motion_y & s_mask;
  1902. uvsrc_x = src_x;
  1903. uvsrc_y = src_y;
  1904. }
  1905. }
  1906. }
  1907. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1908. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1909. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1910. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
  1911. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1912. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  1913. linesize >> field_based, 17, 17 + field_based,
  1914. src_x, src_y << field_based, h_edge_pos,
  1915. v_edge_pos);
  1916. ptr_y = s->edge_emu_buffer;
  1917. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1918. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  1919. s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
  1920. 9 + field_based,
  1921. uvsrc_x, uvsrc_y << field_based,
  1922. h_edge_pos >> 1, v_edge_pos >> 1);
  1923. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
  1924. 9 + field_based,
  1925. uvsrc_x, uvsrc_y << field_based,
  1926. h_edge_pos >> 1, v_edge_pos >> 1);
  1927. ptr_cb = uvbuf;
  1928. ptr_cr = uvbuf + 16;
  1929. }
  1930. }
  1931. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1932. if (bottom_field) {
  1933. dest_y += s->linesize;
  1934. dest_cb += s->uvlinesize;
  1935. dest_cr += s->uvlinesize;
  1936. }
  1937. if (field_select) {
  1938. ptr_y += s->linesize;
  1939. ptr_cb += s->uvlinesize;
  1940. ptr_cr += s->uvlinesize;
  1941. }
  1942. sx = (sx << 2) >> lowres;
  1943. sy = (sy << 2) >> lowres;
  1944. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1945. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1946. uvsx = (uvsx << 2) >> lowres;
  1947. uvsy = (uvsy << 2) >> lowres;
  1948. if (h >> s->chroma_y_shift) {
  1949. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1950. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1951. }
  1952. }
  1953. // FIXME h261 lowres loop filter
  1954. }
  1955. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1956. uint8_t *dest_cb, uint8_t *dest_cr,
  1957. uint8_t **ref_picture,
  1958. h264_chroma_mc_func * pix_op,
  1959. int mx, int my)
  1960. {
  1961. const int lowres = s->avctx->lowres;
  1962. const int op_index = FFMIN(lowres, 2);
  1963. const int block_s = 8 >> lowres;
  1964. const int s_mask = (2 << lowres) - 1;
  1965. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1966. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1967. int emu = 0, src_x, src_y, offset, sx, sy;
  1968. uint8_t *ptr;
  1969. if (s->quarter_sample) {
  1970. mx /= 2;
  1971. my /= 2;
  1972. }
  1973. /* In case of 8X8, we construct a single chroma motion vector
  1974. with a special rounding */
  1975. mx = ff_h263_round_chroma(mx);
  1976. my = ff_h263_round_chroma(my);
  1977. sx = mx & s_mask;
  1978. sy = my & s_mask;
  1979. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1980. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1981. offset = src_y * s->uvlinesize + src_x;
  1982. ptr = ref_picture[1] + offset;
  1983. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  1984. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1985. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1986. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  1987. 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1988. ptr = s->edge_emu_buffer;
  1989. emu = 1;
  1990. }
  1991. }
  1992. sx = (sx << 2) >> lowres;
  1993. sy = (sy << 2) >> lowres;
  1994. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1995. ptr = ref_picture[2] + offset;
  1996. if (emu) {
  1997. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
  1998. src_x, src_y, h_edge_pos, v_edge_pos);
  1999. ptr = s->edge_emu_buffer;
  2000. }
  2001. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2002. }
  2003. /**
  2004. * motion compensation of a single macroblock
  2005. * @param s context
  2006. * @param dest_y luma destination pointer
  2007. * @param dest_cb chroma cb/u destination pointer
  2008. * @param dest_cr chroma cr/v destination pointer
  2009. * @param dir direction (0->forward, 1->backward)
  2010. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2011. * @param pix_op halfpel motion compensation function (average or put normally)
  2012. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2013. */
  2014. static inline void MPV_motion_lowres(MpegEncContext *s,
  2015. uint8_t *dest_y, uint8_t *dest_cb,
  2016. uint8_t *dest_cr,
  2017. int dir, uint8_t **ref_picture,
  2018. h264_chroma_mc_func *pix_op)
  2019. {
  2020. int mx, my;
  2021. int mb_x, mb_y, i;
  2022. const int lowres = s->avctx->lowres;
  2023. const int block_s = 8 >>lowres;
  2024. mb_x = s->mb_x;
  2025. mb_y = s->mb_y;
  2026. switch (s->mv_type) {
  2027. case MV_TYPE_16X16:
  2028. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2029. 0, 0, 0,
  2030. ref_picture, pix_op,
  2031. s->mv[dir][0][0], s->mv[dir][0][1],
  2032. 2 * block_s, mb_y);
  2033. break;
  2034. case MV_TYPE_8X8:
  2035. mx = 0;
  2036. my = 0;
  2037. for (i = 0; i < 4; i++) {
  2038. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2039. s->linesize) * block_s,
  2040. ref_picture[0], 0, 0,
  2041. (2 * mb_x + (i & 1)) * block_s,
  2042. (2 * mb_y + (i >> 1)) * block_s,
  2043. s->width, s->height, s->linesize,
  2044. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2045. block_s, block_s, pix_op,
  2046. s->mv[dir][i][0], s->mv[dir][i][1]);
  2047. mx += s->mv[dir][i][0];
  2048. my += s->mv[dir][i][1];
  2049. }
  2050. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2051. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2052. pix_op, mx, my);
  2053. break;
  2054. case MV_TYPE_FIELD:
  2055. if (s->picture_structure == PICT_FRAME) {
  2056. /* top field */
  2057. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2058. 1, 0, s->field_select[dir][0],
  2059. ref_picture, pix_op,
  2060. s->mv[dir][0][0], s->mv[dir][0][1],
  2061. block_s, mb_y);
  2062. /* bottom field */
  2063. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2064. 1, 1, s->field_select[dir][1],
  2065. ref_picture, pix_op,
  2066. s->mv[dir][1][0], s->mv[dir][1][1],
  2067. block_s, mb_y);
  2068. } else {
  2069. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2070. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2071. ref_picture = s->current_picture_ptr->f.data;
  2072. }
  2073. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2074. 0, 0, s->field_select[dir][0],
  2075. ref_picture, pix_op,
  2076. s->mv[dir][0][0],
  2077. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2078. }
  2079. break;
  2080. case MV_TYPE_16X8:
  2081. for (i = 0; i < 2; i++) {
  2082. uint8_t **ref2picture;
  2083. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2084. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2085. ref2picture = ref_picture;
  2086. } else {
  2087. ref2picture = s->current_picture_ptr->f.data;
  2088. }
  2089. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2090. 0, 0, s->field_select[dir][i],
  2091. ref2picture, pix_op,
  2092. s->mv[dir][i][0], s->mv[dir][i][1] +
  2093. 2 * block_s * i, block_s, mb_y >> 1);
  2094. dest_y += 2 * block_s * s->linesize;
  2095. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2096. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2097. }
  2098. break;
  2099. case MV_TYPE_DMV:
  2100. if (s->picture_structure == PICT_FRAME) {
  2101. for (i = 0; i < 2; i++) {
  2102. int j;
  2103. for (j = 0; j < 2; j++) {
  2104. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2105. 1, j, j ^ i,
  2106. ref_picture, pix_op,
  2107. s->mv[dir][2 * i + j][0],
  2108. s->mv[dir][2 * i + j][1],
  2109. block_s, mb_y);
  2110. }
  2111. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  2112. }
  2113. } else {
  2114. for (i = 0; i < 2; i++) {
  2115. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2116. 0, 0, s->picture_structure != i + 1,
  2117. ref_picture, pix_op,
  2118. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2119. 2 * block_s, mb_y >> 1);
  2120. // after put we make avg of the same block
  2121. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  2122. // opposite parity is always in the same
  2123. // frame if this is second field
  2124. if (!s->first_field) {
  2125. ref_picture = s->current_picture_ptr->f.data;
  2126. }
  2127. }
  2128. }
  2129. break;
  2130. default:
  2131. av_assert2(0);
  2132. }
  2133. }
  2134. /**
  2135. * find the lowest MB row referenced in the MVs
  2136. */
  2137. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2138. {
  2139. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2140. int my, off, i, mvs;
  2141. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2142. goto unhandled;
  2143. switch (s->mv_type) {
  2144. case MV_TYPE_16X16:
  2145. mvs = 1;
  2146. break;
  2147. case MV_TYPE_16X8:
  2148. mvs = 2;
  2149. break;
  2150. case MV_TYPE_8X8:
  2151. mvs = 4;
  2152. break;
  2153. default:
  2154. goto unhandled;
  2155. }
  2156. for (i = 0; i < mvs; i++) {
  2157. my = s->mv[dir][i][1]<<qpel_shift;
  2158. my_max = FFMAX(my_max, my);
  2159. my_min = FFMIN(my_min, my);
  2160. }
  2161. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2162. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2163. unhandled:
  2164. return s->mb_height-1;
  2165. }
  2166. /* put block[] to dest[] */
  2167. static inline void put_dct(MpegEncContext *s,
  2168. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2169. {
  2170. s->dct_unquantize_intra(s, block, i, qscale);
  2171. s->dsp.idct_put (dest, line_size, block);
  2172. }
  2173. /* add block[] to dest[] */
  2174. static inline void add_dct(MpegEncContext *s,
  2175. int16_t *block, int i, uint8_t *dest, int line_size)
  2176. {
  2177. if (s->block_last_index[i] >= 0) {
  2178. s->dsp.idct_add (dest, line_size, block);
  2179. }
  2180. }
  2181. static inline void add_dequant_dct(MpegEncContext *s,
  2182. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2183. {
  2184. if (s->block_last_index[i] >= 0) {
  2185. s->dct_unquantize_inter(s, block, i, qscale);
  2186. s->dsp.idct_add (dest, line_size, block);
  2187. }
  2188. }
  2189. /**
  2190. * Clean dc, ac, coded_block for the current non-intra MB.
  2191. */
  2192. void ff_clean_intra_table_entries(MpegEncContext *s)
  2193. {
  2194. int wrap = s->b8_stride;
  2195. int xy = s->block_index[0];
  2196. s->dc_val[0][xy ] =
  2197. s->dc_val[0][xy + 1 ] =
  2198. s->dc_val[0][xy + wrap] =
  2199. s->dc_val[0][xy + 1 + wrap] = 1024;
  2200. /* ac pred */
  2201. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2202. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2203. if (s->msmpeg4_version>=3) {
  2204. s->coded_block[xy ] =
  2205. s->coded_block[xy + 1 ] =
  2206. s->coded_block[xy + wrap] =
  2207. s->coded_block[xy + 1 + wrap] = 0;
  2208. }
  2209. /* chroma */
  2210. wrap = s->mb_stride;
  2211. xy = s->mb_x + s->mb_y * wrap;
  2212. s->dc_val[1][xy] =
  2213. s->dc_val[2][xy] = 1024;
  2214. /* ac pred */
  2215. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2216. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2217. s->mbintra_table[xy]= 0;
  2218. }
  2219. /* generic function called after a macroblock has been parsed by the
  2220. decoder or after it has been encoded by the encoder.
  2221. Important variables used:
  2222. s->mb_intra : true if intra macroblock
  2223. s->mv_dir : motion vector direction
  2224. s->mv_type : motion vector type
  2225. s->mv : motion vector
  2226. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2227. */
  2228. static av_always_inline
  2229. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2230. int lowres_flag, int is_mpeg12)
  2231. {
  2232. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2233. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  2234. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  2235. return;
  2236. }
  2237. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2238. /* save DCT coefficients */
  2239. int i,j;
  2240. int16_t *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  2241. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2242. for(i=0; i<6; i++){
  2243. for(j=0; j<64; j++){
  2244. *dct++ = block[i][s->dsp.idct_permutation[j]];
  2245. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  2246. }
  2247. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2248. }
  2249. }
  2250. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  2251. /* update DC predictors for P macroblocks */
  2252. if (!s->mb_intra) {
  2253. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2254. if(s->mbintra_table[mb_xy])
  2255. ff_clean_intra_table_entries(s);
  2256. } else {
  2257. s->last_dc[0] =
  2258. s->last_dc[1] =
  2259. s->last_dc[2] = 128 << s->intra_dc_precision;
  2260. }
  2261. }
  2262. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2263. s->mbintra_table[mb_xy]=1;
  2264. 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
  2265. uint8_t *dest_y, *dest_cb, *dest_cr;
  2266. int dct_linesize, dct_offset;
  2267. op_pixels_func (*op_pix)[4];
  2268. qpel_mc_func (*op_qpix)[16];
  2269. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2270. const int uvlinesize = s->current_picture.f.linesize[1];
  2271. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2272. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2273. /* avoid copy if macroblock skipped in last frame too */
  2274. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2275. if(!s->encoding){
  2276. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2277. if (s->mb_skipped) {
  2278. s->mb_skipped= 0;
  2279. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2280. *mbskip_ptr = 1;
  2281. } else if(!s->current_picture.f.reference) {
  2282. *mbskip_ptr = 1;
  2283. } else{
  2284. *mbskip_ptr = 0; /* not skipped */
  2285. }
  2286. }
  2287. dct_linesize = linesize << s->interlaced_dct;
  2288. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2289. if(readable){
  2290. dest_y= s->dest[0];
  2291. dest_cb= s->dest[1];
  2292. dest_cr= s->dest[2];
  2293. }else{
  2294. dest_y = s->b_scratchpad;
  2295. dest_cb= s->b_scratchpad+16*linesize;
  2296. dest_cr= s->b_scratchpad+32*linesize;
  2297. }
  2298. if (!s->mb_intra) {
  2299. /* motion handling */
  2300. /* decoding or more than one mb_type (MC was already done otherwise) */
  2301. if(!s->encoding){
  2302. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2303. if (s->mv_dir & MV_DIR_FORWARD) {
  2304. ff_thread_await_progress(&s->last_picture_ptr->f,
  2305. ff_MPV_lowest_referenced_row(s, 0),
  2306. 0);
  2307. }
  2308. if (s->mv_dir & MV_DIR_BACKWARD) {
  2309. ff_thread_await_progress(&s->next_picture_ptr->f,
  2310. ff_MPV_lowest_referenced_row(s, 1),
  2311. 0);
  2312. }
  2313. }
  2314. if(lowres_flag){
  2315. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  2316. if (s->mv_dir & MV_DIR_FORWARD) {
  2317. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2318. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  2319. }
  2320. if (s->mv_dir & MV_DIR_BACKWARD) {
  2321. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2322. }
  2323. }else{
  2324. op_qpix= s->me.qpel_put;
  2325. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2326. op_pix = s->dsp.put_pixels_tab;
  2327. }else{
  2328. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2329. }
  2330. if (s->mv_dir & MV_DIR_FORWARD) {
  2331. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2332. op_pix = s->dsp.avg_pixels_tab;
  2333. op_qpix= s->me.qpel_avg;
  2334. }
  2335. if (s->mv_dir & MV_DIR_BACKWARD) {
  2336. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2337. }
  2338. }
  2339. }
  2340. /* skip dequant / idct if we are really late ;) */
  2341. if(s->avctx->skip_idct){
  2342. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2343. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2344. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2345. goto skip_idct;
  2346. }
  2347. /* add dct residue */
  2348. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2349. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2350. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2351. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2352. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2353. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2354. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2355. if (s->chroma_y_shift){
  2356. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2357. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2358. }else{
  2359. dct_linesize >>= 1;
  2360. dct_offset >>=1;
  2361. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2362. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2363. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2364. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2365. }
  2366. }
  2367. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2368. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2369. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2370. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2371. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2372. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2373. if(s->chroma_y_shift){//Chroma420
  2374. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2375. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2376. }else{
  2377. //chroma422
  2378. dct_linesize = uvlinesize << s->interlaced_dct;
  2379. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2380. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2381. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2382. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2383. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2384. if(!s->chroma_x_shift){//Chroma444
  2385. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2386. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2387. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2388. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2389. }
  2390. }
  2391. }//fi gray
  2392. }
  2393. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2394. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2395. }
  2396. } else {
  2397. /* dct only in intra block */
  2398. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2399. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2400. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2401. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2402. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2403. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2404. if(s->chroma_y_shift){
  2405. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2406. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2407. }else{
  2408. dct_offset >>=1;
  2409. dct_linesize >>=1;
  2410. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2411. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2412. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2413. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2414. }
  2415. }
  2416. }else{
  2417. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2418. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2419. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2420. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2421. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2422. if(s->chroma_y_shift){
  2423. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2424. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2425. }else{
  2426. dct_linesize = uvlinesize << s->interlaced_dct;
  2427. dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
  2428. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2429. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2430. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2431. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2432. if(!s->chroma_x_shift){//Chroma444
  2433. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2434. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2435. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2436. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2437. }
  2438. }
  2439. }//gray
  2440. }
  2441. }
  2442. skip_idct:
  2443. if(!readable){
  2444. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2445. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2446. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2447. }
  2448. }
  2449. }
  2450. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2451. #if !CONFIG_SMALL
  2452. if(s->out_format == FMT_MPEG1) {
  2453. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2454. else MPV_decode_mb_internal(s, block, 0, 1);
  2455. } else
  2456. #endif
  2457. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2458. else MPV_decode_mb_internal(s, block, 0, 0);
  2459. }
  2460. /**
  2461. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2462. */
  2463. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2464. const int field_pic= s->picture_structure != PICT_FRAME;
  2465. if(field_pic){
  2466. h <<= 1;
  2467. y <<= 1;
  2468. }
  2469. if (!s->avctx->hwaccel
  2470. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2471. && s->unrestricted_mv
  2472. && s->current_picture.f.reference
  2473. && !s->intra_only
  2474. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2475. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  2476. int sides = 0, edge_h;
  2477. int hshift = desc->log2_chroma_w;
  2478. int vshift = desc->log2_chroma_h;
  2479. if (y==0) sides |= EDGE_TOP;
  2480. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2481. edge_h= FFMIN(h, s->v_edge_pos - y);
  2482. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  2483. s->linesize, s->h_edge_pos, edge_h,
  2484. EDGE_WIDTH, EDGE_WIDTH, sides);
  2485. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  2486. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2487. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2488. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  2489. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2490. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2491. }
  2492. h= FFMIN(h, s->avctx->height - y);
  2493. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2494. if (s->avctx->draw_horiz_band) {
  2495. AVFrame *src;
  2496. int offset[AV_NUM_DATA_POINTERS];
  2497. int i;
  2498. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2499. src = &s->current_picture_ptr->f;
  2500. else if(s->last_picture_ptr)
  2501. src = &s->last_picture_ptr->f;
  2502. else
  2503. return;
  2504. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2505. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2506. offset[i] = 0;
  2507. }else{
  2508. offset[0]= y * s->linesize;
  2509. offset[1]=
  2510. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2511. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2512. offset[i] = 0;
  2513. }
  2514. emms_c();
  2515. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2516. y, s->picture_structure, h);
  2517. }
  2518. }
  2519. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2520. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2521. const int uvlinesize = s->current_picture.f.linesize[1];
  2522. const int mb_size= 4 - s->avctx->lowres;
  2523. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2524. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2525. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2526. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2527. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2528. 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;
  2529. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2530. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2531. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2532. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2533. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2534. {
  2535. if(s->picture_structure==PICT_FRAME){
  2536. s->dest[0] += s->mb_y * linesize << mb_size;
  2537. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2538. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2539. }else{
  2540. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2541. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2542. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2543. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2544. }
  2545. }
  2546. }
  2547. void ff_mpeg_flush(AVCodecContext *avctx){
  2548. int i;
  2549. MpegEncContext *s = avctx->priv_data;
  2550. if(s==NULL || s->picture==NULL)
  2551. return;
  2552. for(i=0; i<s->picture_count; i++){
  2553. if (s->picture[i].f.data[0] &&
  2554. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2555. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2556. free_frame_buffer(s, &s->picture[i]);
  2557. }
  2558. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2559. s->mb_x= s->mb_y= 0;
  2560. s->closed_gop= 0;
  2561. s->parse_context.state= -1;
  2562. s->parse_context.frame_start_found= 0;
  2563. s->parse_context.overread= 0;
  2564. s->parse_context.overread_index= 0;
  2565. s->parse_context.index= 0;
  2566. s->parse_context.last_index= 0;
  2567. s->bitstream_buffer_size=0;
  2568. s->pp_time=0;
  2569. }
  2570. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2571. int16_t *block, int n, int qscale)
  2572. {
  2573. int i, level, nCoeffs;
  2574. const uint16_t *quant_matrix;
  2575. nCoeffs= s->block_last_index[n];
  2576. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2577. /* XXX: only mpeg1 */
  2578. quant_matrix = s->intra_matrix;
  2579. for(i=1;i<=nCoeffs;i++) {
  2580. int j= s->intra_scantable.permutated[i];
  2581. level = block[j];
  2582. if (level) {
  2583. if (level < 0) {
  2584. level = -level;
  2585. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2586. level = (level - 1) | 1;
  2587. level = -level;
  2588. } else {
  2589. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2590. level = (level - 1) | 1;
  2591. }
  2592. block[j] = level;
  2593. }
  2594. }
  2595. }
  2596. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2597. int16_t *block, int n, int qscale)
  2598. {
  2599. int i, level, nCoeffs;
  2600. const uint16_t *quant_matrix;
  2601. nCoeffs= s->block_last_index[n];
  2602. quant_matrix = s->inter_matrix;
  2603. for(i=0; i<=nCoeffs; i++) {
  2604. int j= s->intra_scantable.permutated[i];
  2605. level = block[j];
  2606. if (level) {
  2607. if (level < 0) {
  2608. level = -level;
  2609. level = (((level << 1) + 1) * qscale *
  2610. ((int) (quant_matrix[j]))) >> 4;
  2611. level = (level - 1) | 1;
  2612. level = -level;
  2613. } else {
  2614. level = (((level << 1) + 1) * qscale *
  2615. ((int) (quant_matrix[j]))) >> 4;
  2616. level = (level - 1) | 1;
  2617. }
  2618. block[j] = level;
  2619. }
  2620. }
  2621. }
  2622. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2623. int16_t *block, int n, int qscale)
  2624. {
  2625. int i, level, nCoeffs;
  2626. const uint16_t *quant_matrix;
  2627. if(s->alternate_scan) nCoeffs= 63;
  2628. else nCoeffs= s->block_last_index[n];
  2629. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2630. quant_matrix = s->intra_matrix;
  2631. for(i=1;i<=nCoeffs;i++) {
  2632. int j= s->intra_scantable.permutated[i];
  2633. level = block[j];
  2634. if (level) {
  2635. if (level < 0) {
  2636. level = -level;
  2637. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2638. level = -level;
  2639. } else {
  2640. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2641. }
  2642. block[j] = level;
  2643. }
  2644. }
  2645. }
  2646. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2647. int16_t *block, int n, int qscale)
  2648. {
  2649. int i, level, nCoeffs;
  2650. const uint16_t *quant_matrix;
  2651. int sum=-1;
  2652. if(s->alternate_scan) nCoeffs= 63;
  2653. else nCoeffs= s->block_last_index[n];
  2654. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2655. sum += block[0];
  2656. quant_matrix = s->intra_matrix;
  2657. for(i=1;i<=nCoeffs;i++) {
  2658. int j= s->intra_scantable.permutated[i];
  2659. level = block[j];
  2660. if (level) {
  2661. if (level < 0) {
  2662. level = -level;
  2663. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2664. level = -level;
  2665. } else {
  2666. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2667. }
  2668. block[j] = level;
  2669. sum+=level;
  2670. }
  2671. }
  2672. block[63]^=sum&1;
  2673. }
  2674. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2675. int16_t *block, int n, int qscale)
  2676. {
  2677. int i, level, nCoeffs;
  2678. const uint16_t *quant_matrix;
  2679. int sum=-1;
  2680. if(s->alternate_scan) nCoeffs= 63;
  2681. else nCoeffs= s->block_last_index[n];
  2682. quant_matrix = s->inter_matrix;
  2683. for(i=0; i<=nCoeffs; i++) {
  2684. int j= s->intra_scantable.permutated[i];
  2685. level = block[j];
  2686. if (level) {
  2687. if (level < 0) {
  2688. level = -level;
  2689. level = (((level << 1) + 1) * qscale *
  2690. ((int) (quant_matrix[j]))) >> 4;
  2691. level = -level;
  2692. } else {
  2693. level = (((level << 1) + 1) * qscale *
  2694. ((int) (quant_matrix[j]))) >> 4;
  2695. }
  2696. block[j] = level;
  2697. sum+=level;
  2698. }
  2699. }
  2700. block[63]^=sum&1;
  2701. }
  2702. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2703. int16_t *block, int n, int qscale)
  2704. {
  2705. int i, level, qmul, qadd;
  2706. int nCoeffs;
  2707. assert(s->block_last_index[n]>=0);
  2708. qmul = qscale << 1;
  2709. if (!s->h263_aic) {
  2710. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2711. qadd = (qscale - 1) | 1;
  2712. }else{
  2713. qadd = 0;
  2714. }
  2715. if(s->ac_pred)
  2716. nCoeffs=63;
  2717. else
  2718. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2719. for(i=1; i<=nCoeffs; i++) {
  2720. level = block[i];
  2721. if (level) {
  2722. if (level < 0) {
  2723. level = level * qmul - qadd;
  2724. } else {
  2725. level = level * qmul + qadd;
  2726. }
  2727. block[i] = level;
  2728. }
  2729. }
  2730. }
  2731. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2732. int16_t *block, int n, int qscale)
  2733. {
  2734. int i, level, qmul, qadd;
  2735. int nCoeffs;
  2736. assert(s->block_last_index[n]>=0);
  2737. qadd = (qscale - 1) | 1;
  2738. qmul = qscale << 1;
  2739. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2740. for(i=0; i<=nCoeffs; i++) {
  2741. level = block[i];
  2742. if (level) {
  2743. if (level < 0) {
  2744. level = level * qmul - qadd;
  2745. } else {
  2746. level = level * qmul + qadd;
  2747. }
  2748. block[i] = level;
  2749. }
  2750. }
  2751. }
  2752. /**
  2753. * set qscale and update qscale dependent variables.
  2754. */
  2755. void ff_set_qscale(MpegEncContext * s, int qscale)
  2756. {
  2757. if (qscale < 1)
  2758. qscale = 1;
  2759. else if (qscale > 31)
  2760. qscale = 31;
  2761. s->qscale = qscale;
  2762. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2763. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2764. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2765. }
  2766. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2767. {
  2768. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2769. ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);
  2770. }