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.

4837 lines
177KB

  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. * non linear quantizers with large QPs and VBV with restrictive qmin fixes sponsored by NOA GmbH
  26. */
  27. /**
  28. * @file
  29. * The simplest mpeg encoder (well, it was the simplest!).
  30. */
  31. #include <stdint.h>
  32. #include "libavutil/internal.h"
  33. #include "libavutil/intmath.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/opt.h"
  37. #include "avcodec.h"
  38. #include "dct.h"
  39. #include "idctdsp.h"
  40. #include "mpeg12.h"
  41. #include "mpegvideo.h"
  42. #include "mpegvideodata.h"
  43. #include "h261.h"
  44. #include "h263.h"
  45. #include "h263data.h"
  46. #include "mjpegenc_common.h"
  47. #include "mathops.h"
  48. #include "mpegutils.h"
  49. #include "mjpegenc.h"
  50. #include "msmpeg4.h"
  51. #include "pixblockdsp.h"
  52. #include "qpeldsp.h"
  53. #include "faandct.h"
  54. #include "thread.h"
  55. #include "aandcttab.h"
  56. #include "flv.h"
  57. #include "mpeg4video.h"
  58. #include "internal.h"
  59. #include "bytestream.h"
  60. #include "wmv2.h"
  61. #include "rv10.h"
  62. #include "libxvid.h"
  63. #include <limits.h>
  64. #include "sp5x.h"
  65. #define QUANT_BIAS_SHIFT 8
  66. #define QMAT_SHIFT_MMX 16
  67. #define QMAT_SHIFT 21
  68. static int encode_picture(MpegEncContext *s, int picture_number);
  69. static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
  70. static int sse_mb(MpegEncContext *s);
  71. static void denoise_dct_c(MpegEncContext *s, int16_t *block);
  72. static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
  73. static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
  74. static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
  75. const AVOption ff_mpv_generic_options[] = {
  76. FF_MPV_COMMON_OPTS
  77. { NULL },
  78. };
  79. void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
  80. uint16_t (*qmat16)[2][64],
  81. const uint16_t *quant_matrix,
  82. int bias, int qmin, int qmax, int intra)
  83. {
  84. FDCTDSPContext *fdsp = &s->fdsp;
  85. int qscale;
  86. int shift = 0;
  87. for (qscale = qmin; qscale <= qmax; qscale++) {
  88. int i;
  89. int qscale2;
  90. if (s->q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
  91. else qscale2 = qscale << 1;
  92. if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
  93. #if CONFIG_FAANDCT
  94. fdsp->fdct == ff_faandct ||
  95. #endif /* CONFIG_FAANDCT */
  96. fdsp->fdct == ff_jpeg_fdct_islow_10) {
  97. for (i = 0; i < 64; i++) {
  98. const int j = s->idsp.idct_permutation[i];
  99. int64_t den = (int64_t) qscale2 * quant_matrix[j];
  100. /* 16 <= qscale * quant_matrix[i] <= 7905
  101. * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
  102. * 19952 <= x <= 249205026
  103. * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
  104. * 3444240 >= (1 << 36) / (x) >= 275 */
  105. qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
  106. }
  107. } else if (fdsp->fdct == ff_fdct_ifast) {
  108. for (i = 0; i < 64; i++) {
  109. const int j = s->idsp.idct_permutation[i];
  110. int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
  111. /* 16 <= qscale * quant_matrix[i] <= 7905
  112. * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
  113. * 19952 <= x <= 249205026
  114. * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
  115. * 3444240 >= (1 << 36) / (x) >= 275 */
  116. qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
  117. }
  118. } else {
  119. for (i = 0; i < 64; i++) {
  120. const int j = s->idsp.idct_permutation[i];
  121. int64_t den = (int64_t) qscale2 * quant_matrix[j];
  122. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  123. * Assume x = qscale * quant_matrix[i]
  124. * So 16 <= x <= 7905
  125. * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
  126. * so 32768 >= (1 << 19) / (x) >= 67 */
  127. qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
  128. //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
  129. // (qscale * quant_matrix[i]);
  130. qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
  131. if (qmat16[qscale][0][i] == 0 ||
  132. qmat16[qscale][0][i] == 128 * 256)
  133. qmat16[qscale][0][i] = 128 * 256 - 1;
  134. qmat16[qscale][1][i] =
  135. ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
  136. qmat16[qscale][0][i]);
  137. }
  138. }
  139. for (i = intra; i < 64; i++) {
  140. int64_t max = 8191;
  141. if (fdsp->fdct == ff_fdct_ifast) {
  142. max = (8191LL * ff_aanscales[i]) >> 14;
  143. }
  144. while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
  145. shift++;
  146. }
  147. }
  148. }
  149. if (shift) {
  150. av_log(s->avctx, AV_LOG_INFO,
  151. "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
  152. QMAT_SHIFT - shift);
  153. }
  154. }
  155. static inline void update_qscale(MpegEncContext *s)
  156. {
  157. if (s->q_scale_type == 1 && 0) {
  158. int i;
  159. int bestdiff=INT_MAX;
  160. int best = 1;
  161. for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
  162. int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
  163. if (ff_mpeg2_non_linear_qscale[i] < s->avctx->qmin ||
  164. (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !s->vbv_ignore_qmax))
  165. continue;
  166. if (diff < bestdiff) {
  167. bestdiff = diff;
  168. best = i;
  169. }
  170. }
  171. s->qscale = best;
  172. } else {
  173. s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
  174. (FF_LAMBDA_SHIFT + 7);
  175. s->qscale = av_clip(s->qscale, s->avctx->qmin, s->vbv_ignore_qmax ? 31 : s->avctx->qmax);
  176. }
  177. s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
  178. FF_LAMBDA_SHIFT;
  179. }
  180. void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
  181. {
  182. int i;
  183. if (matrix) {
  184. put_bits(pb, 1, 1);
  185. for (i = 0; i < 64; i++) {
  186. put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
  187. }
  188. } else
  189. put_bits(pb, 1, 0);
  190. }
  191. /**
  192. * init s->current_picture.qscale_table from s->lambda_table
  193. */
  194. void ff_init_qscale_tab(MpegEncContext *s)
  195. {
  196. int8_t * const qscale_table = s->current_picture.qscale_table;
  197. int i;
  198. for (i = 0; i < s->mb_num; i++) {
  199. unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
  200. int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
  201. qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
  202. s->avctx->qmax);
  203. }
  204. }
  205. static void update_duplicate_context_after_me(MpegEncContext *dst,
  206. MpegEncContext *src)
  207. {
  208. #define COPY(a) dst->a= src->a
  209. COPY(pict_type);
  210. COPY(current_picture);
  211. COPY(f_code);
  212. COPY(b_code);
  213. COPY(qscale);
  214. COPY(lambda);
  215. COPY(lambda2);
  216. COPY(picture_in_gop_number);
  217. COPY(gop_picture_number);
  218. COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
  219. COPY(progressive_frame); // FIXME don't set in encode_header
  220. COPY(partitioned_frame); // FIXME don't set in encode_header
  221. #undef COPY
  222. }
  223. /**
  224. * Set the given MpegEncContext to defaults for encoding.
  225. * the changed fields will not depend upon the prior state of the MpegEncContext.
  226. */
  227. static void mpv_encode_defaults(MpegEncContext *s)
  228. {
  229. int i;
  230. ff_mpv_common_defaults(s);
  231. for (i = -16; i < 16; i++) {
  232. default_fcode_tab[i + MAX_MV] = 1;
  233. }
  234. s->me.mv_penalty = default_mv_penalty;
  235. s->fcode_tab = default_fcode_tab;
  236. s->input_picture_number = 0;
  237. s->picture_in_gop_number = 0;
  238. }
  239. av_cold int ff_dct_encode_init(MpegEncContext *s)
  240. {
  241. if (ARCH_X86)
  242. ff_dct_encode_init_x86(s);
  243. if (CONFIG_H263_ENCODER)
  244. ff_h263dsp_init(&s->h263dsp);
  245. if (!s->dct_quantize)
  246. s->dct_quantize = ff_dct_quantize_c;
  247. if (!s->denoise_dct)
  248. s->denoise_dct = denoise_dct_c;
  249. s->fast_dct_quantize = s->dct_quantize;
  250. if (s->avctx->trellis)
  251. s->dct_quantize = dct_quantize_trellis_c;
  252. return 0;
  253. }
  254. /* init video encoder */
  255. av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
  256. {
  257. MpegEncContext *s = avctx->priv_data;
  258. AVCPBProperties *cpb_props;
  259. int i, ret, format_supported;
  260. mpv_encode_defaults(s);
  261. switch (avctx->codec_id) {
  262. case AV_CODEC_ID_MPEG2VIDEO:
  263. if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
  264. avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
  265. av_log(avctx, AV_LOG_ERROR,
  266. "only YUV420 and YUV422 are supported\n");
  267. return -1;
  268. }
  269. break;
  270. case AV_CODEC_ID_MJPEG:
  271. case AV_CODEC_ID_AMV:
  272. format_supported = 0;
  273. /* JPEG color space */
  274. if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  275. avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  276. avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
  277. (avctx->color_range == AVCOL_RANGE_JPEG &&
  278. (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  279. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  280. avctx->pix_fmt == AV_PIX_FMT_YUV444P)))
  281. format_supported = 1;
  282. /* MPEG color space */
  283. else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
  284. (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  285. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  286. avctx->pix_fmt == AV_PIX_FMT_YUV444P))
  287. format_supported = 1;
  288. if (!format_supported) {
  289. av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
  290. return -1;
  291. }
  292. break;
  293. default:
  294. if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
  295. av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
  296. return -1;
  297. }
  298. }
  299. switch (avctx->pix_fmt) {
  300. case AV_PIX_FMT_YUVJ444P:
  301. case AV_PIX_FMT_YUV444P:
  302. s->chroma_format = CHROMA_444;
  303. break;
  304. case AV_PIX_FMT_YUVJ422P:
  305. case AV_PIX_FMT_YUV422P:
  306. s->chroma_format = CHROMA_422;
  307. break;
  308. case AV_PIX_FMT_YUVJ420P:
  309. case AV_PIX_FMT_YUV420P:
  310. default:
  311. s->chroma_format = CHROMA_420;
  312. break;
  313. }
  314. avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
  315. #if FF_API_PRIVATE_OPT
  316. FF_DISABLE_DEPRECATION_WARNINGS
  317. if (avctx->rtp_payload_size)
  318. s->rtp_payload_size = avctx->rtp_payload_size;
  319. if (avctx->me_penalty_compensation)
  320. s->me_penalty_compensation = avctx->me_penalty_compensation;
  321. if (avctx->pre_me)
  322. s->me_pre = avctx->pre_me;
  323. FF_ENABLE_DEPRECATION_WARNINGS
  324. #endif
  325. s->bit_rate = avctx->bit_rate;
  326. s->width = avctx->width;
  327. s->height = avctx->height;
  328. if (avctx->gop_size > 600 &&
  329. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  330. av_log(avctx, AV_LOG_WARNING,
  331. "keyframe interval too large!, reducing it from %d to %d\n",
  332. avctx->gop_size, 600);
  333. avctx->gop_size = 600;
  334. }
  335. s->gop_size = avctx->gop_size;
  336. s->avctx = avctx;
  337. if (avctx->max_b_frames > MAX_B_FRAMES) {
  338. av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
  339. "is %d.\n", MAX_B_FRAMES);
  340. avctx->max_b_frames = MAX_B_FRAMES;
  341. }
  342. s->max_b_frames = avctx->max_b_frames;
  343. s->codec_id = avctx->codec->id;
  344. s->strict_std_compliance = avctx->strict_std_compliance;
  345. s->quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
  346. s->rtp_mode = !!s->rtp_payload_size;
  347. s->intra_dc_precision = avctx->intra_dc_precision;
  348. // workaround some differences between how applications specify dc precision
  349. if (s->intra_dc_precision < 0) {
  350. s->intra_dc_precision += 8;
  351. } else if (s->intra_dc_precision >= 8)
  352. s->intra_dc_precision -= 8;
  353. if (s->intra_dc_precision < 0) {
  354. av_log(avctx, AV_LOG_ERROR,
  355. "intra dc precision must be positive, note some applications use"
  356. " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
  357. return AVERROR(EINVAL);
  358. }
  359. if (avctx->codec_id == AV_CODEC_ID_AMV || (avctx->active_thread_type & FF_THREAD_SLICE))
  360. s->huffman = 0;
  361. if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
  362. av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
  363. return AVERROR(EINVAL);
  364. }
  365. s->user_specified_pts = AV_NOPTS_VALUE;
  366. if (s->gop_size <= 1) {
  367. s->intra_only = 1;
  368. s->gop_size = 12;
  369. } else {
  370. s->intra_only = 0;
  371. }
  372. /* Fixed QSCALE */
  373. s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
  374. s->adaptive_quant = (s->avctx->lumi_masking ||
  375. s->avctx->dark_masking ||
  376. s->avctx->temporal_cplx_masking ||
  377. s->avctx->spatial_cplx_masking ||
  378. s->avctx->p_masking ||
  379. s->border_masking ||
  380. (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
  381. !s->fixed_qscale;
  382. s->loop_filter = !!(s->avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
  383. if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
  384. switch(avctx->codec_id) {
  385. case AV_CODEC_ID_MPEG1VIDEO:
  386. case AV_CODEC_ID_MPEG2VIDEO:
  387. avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
  388. break;
  389. case AV_CODEC_ID_MPEG4:
  390. case AV_CODEC_ID_MSMPEG4V1:
  391. case AV_CODEC_ID_MSMPEG4V2:
  392. case AV_CODEC_ID_MSMPEG4V3:
  393. if (avctx->rc_max_rate >= 15000000) {
  394. avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
  395. } else if(avctx->rc_max_rate >= 2000000) {
  396. avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
  397. } else if(avctx->rc_max_rate >= 384000) {
  398. avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
  399. } else
  400. avctx->rc_buffer_size = 40;
  401. avctx->rc_buffer_size *= 16384;
  402. break;
  403. }
  404. if (avctx->rc_buffer_size) {
  405. av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
  406. }
  407. }
  408. if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
  409. av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
  410. return -1;
  411. }
  412. if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
  413. av_log(avctx, AV_LOG_INFO,
  414. "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
  415. }
  416. if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
  417. av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
  418. return -1;
  419. }
  420. if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
  421. av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
  422. return -1;
  423. }
  424. if (avctx->rc_max_rate &&
  425. avctx->rc_max_rate == avctx->bit_rate &&
  426. avctx->rc_max_rate != avctx->rc_min_rate) {
  427. av_log(avctx, AV_LOG_INFO,
  428. "impossible bitrate constraints, this will fail\n");
  429. }
  430. if (avctx->rc_buffer_size &&
  431. avctx->bit_rate * (int64_t)avctx->time_base.num >
  432. avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
  433. av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
  434. return -1;
  435. }
  436. if (!s->fixed_qscale &&
  437. avctx->bit_rate * av_q2d(avctx->time_base) >
  438. avctx->bit_rate_tolerance) {
  439. av_log(avctx, AV_LOG_WARNING,
  440. "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
  441. avctx->bit_rate_tolerance = 5 * avctx->bit_rate * av_q2d(avctx->time_base);
  442. }
  443. if (s->avctx->rc_max_rate &&
  444. s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
  445. (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  446. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
  447. 90000LL * (avctx->rc_buffer_size - 1) >
  448. s->avctx->rc_max_rate * 0xFFFFLL) {
  449. av_log(avctx, AV_LOG_INFO,
  450. "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
  451. "specified vbv buffer is too large for the given bitrate!\n");
  452. }
  453. if ((s->avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
  454. s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
  455. s->codec_id != AV_CODEC_ID_FLV1) {
  456. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  457. return -1;
  458. }
  459. if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
  460. av_log(avctx, AV_LOG_ERROR,
  461. "OBMC is only supported with simple mb decision\n");
  462. return -1;
  463. }
  464. if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
  465. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  466. return -1;
  467. }
  468. if (s->max_b_frames &&
  469. s->codec_id != AV_CODEC_ID_MPEG4 &&
  470. s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
  471. s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
  472. av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
  473. return -1;
  474. }
  475. if (s->max_b_frames < 0) {
  476. av_log(avctx, AV_LOG_ERROR,
  477. "max b frames must be 0 or positive for mpegvideo based encoders\n");
  478. return -1;
  479. }
  480. if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
  481. s->codec_id == AV_CODEC_ID_H263 ||
  482. s->codec_id == AV_CODEC_ID_H263P) &&
  483. (avctx->sample_aspect_ratio.num > 255 ||
  484. avctx->sample_aspect_ratio.den > 255)) {
  485. av_log(avctx, AV_LOG_WARNING,
  486. "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
  487. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
  488. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  489. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
  490. }
  491. if ((s->codec_id == AV_CODEC_ID_H263 ||
  492. s->codec_id == AV_CODEC_ID_H263P) &&
  493. (avctx->width > 2048 ||
  494. avctx->height > 1152 )) {
  495. av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
  496. return -1;
  497. }
  498. if ((s->codec_id == AV_CODEC_ID_H263 ||
  499. s->codec_id == AV_CODEC_ID_H263P) &&
  500. ((avctx->width &3) ||
  501. (avctx->height&3) )) {
  502. av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
  503. return -1;
  504. }
  505. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
  506. (avctx->width > 4095 ||
  507. avctx->height > 4095 )) {
  508. av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
  509. return -1;
  510. }
  511. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  512. (avctx->width > 16383 ||
  513. avctx->height > 16383 )) {
  514. av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
  515. return -1;
  516. }
  517. if (s->codec_id == AV_CODEC_ID_RV10 &&
  518. (avctx->width &15 ||
  519. avctx->height&15 )) {
  520. av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
  521. return AVERROR(EINVAL);
  522. }
  523. if (s->codec_id == AV_CODEC_ID_RV20 &&
  524. (avctx->width &3 ||
  525. avctx->height&3 )) {
  526. av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
  527. return AVERROR(EINVAL);
  528. }
  529. if ((s->codec_id == AV_CODEC_ID_WMV1 ||
  530. s->codec_id == AV_CODEC_ID_WMV2) &&
  531. avctx->width & 1) {
  532. av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
  533. return -1;
  534. }
  535. if ((s->avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
  536. s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
  537. av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
  538. return -1;
  539. }
  540. #if FF_API_PRIVATE_OPT
  541. FF_DISABLE_DEPRECATION_WARNINGS
  542. if (avctx->mpeg_quant)
  543. s->mpeg_quant = avctx->mpeg_quant;
  544. FF_ENABLE_DEPRECATION_WARNINGS
  545. #endif
  546. // FIXME mpeg2 uses that too
  547. if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
  548. && s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
  549. av_log(avctx, AV_LOG_ERROR,
  550. "mpeg2 style quantization not supported by codec\n");
  551. return -1;
  552. }
  553. if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
  554. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  555. return -1;
  556. }
  557. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
  558. s->avctx->mb_decision != FF_MB_DECISION_RD) {
  559. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  560. return -1;
  561. }
  562. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
  563. (s->codec_id == AV_CODEC_ID_AMV ||
  564. s->codec_id == AV_CODEC_ID_MJPEG)) {
  565. // Used to produce garbage with MJPEG.
  566. av_log(avctx, AV_LOG_ERROR,
  567. "QP RD is no longer compatible with MJPEG or AMV\n");
  568. return -1;
  569. }
  570. #if FF_API_PRIVATE_OPT
  571. FF_DISABLE_DEPRECATION_WARNINGS
  572. if (avctx->scenechange_threshold)
  573. s->scenechange_threshold = avctx->scenechange_threshold;
  574. FF_ENABLE_DEPRECATION_WARNINGS
  575. #endif
  576. if (s->scenechange_threshold < 1000000000 &&
  577. (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
  578. av_log(avctx, AV_LOG_ERROR,
  579. "closed gop with scene change detection are not supported yet, "
  580. "set threshold to 1000000000\n");
  581. return -1;
  582. }
  583. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
  584. if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
  585. s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  586. av_log(avctx, AV_LOG_ERROR,
  587. "low delay forcing is only available for mpeg2, "
  588. "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
  589. return -1;
  590. }
  591. if (s->max_b_frames != 0) {
  592. av_log(avctx, AV_LOG_ERROR,
  593. "B-frames cannot be used with low delay\n");
  594. return -1;
  595. }
  596. }
  597. if (s->q_scale_type == 1) {
  598. if (avctx->qmax > 28) {
  599. av_log(avctx, AV_LOG_ERROR,
  600. "non linear quant only supports qmax <= 28 currently\n");
  601. return -1;
  602. }
  603. }
  604. if (avctx->slices > 1 &&
  605. (avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
  606. av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
  607. return AVERROR(EINVAL);
  608. }
  609. if (s->avctx->thread_count > 1 &&
  610. s->codec_id != AV_CODEC_ID_MPEG4 &&
  611. s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
  612. s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
  613. s->codec_id != AV_CODEC_ID_MJPEG &&
  614. (s->codec_id != AV_CODEC_ID_H263P)) {
  615. av_log(avctx, AV_LOG_ERROR,
  616. "multi threaded encoding not supported by codec\n");
  617. return -1;
  618. }
  619. if (s->avctx->thread_count < 1) {
  620. av_log(avctx, AV_LOG_ERROR,
  621. "automatic thread number detection not supported by codec, "
  622. "patch welcome\n");
  623. return -1;
  624. }
  625. if (!avctx->time_base.den || !avctx->time_base.num) {
  626. av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
  627. return -1;
  628. }
  629. #if FF_API_PRIVATE_OPT
  630. FF_DISABLE_DEPRECATION_WARNINGS
  631. if (avctx->b_frame_strategy)
  632. s->b_frame_strategy = avctx->b_frame_strategy;
  633. if (avctx->b_sensitivity != 40)
  634. s->b_sensitivity = avctx->b_sensitivity;
  635. FF_ENABLE_DEPRECATION_WARNINGS
  636. #endif
  637. if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
  638. av_log(avctx, AV_LOG_INFO,
  639. "notice: b_frame_strategy only affects the first pass\n");
  640. s->b_frame_strategy = 0;
  641. }
  642. i = av_gcd(avctx->time_base.den, avctx->time_base.num);
  643. if (i > 1) {
  644. av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
  645. avctx->time_base.den /= i;
  646. avctx->time_base.num /= i;
  647. //return -1;
  648. }
  649. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id==AV_CODEC_ID_AMV) {
  650. // (a + x * 3 / 8) / x
  651. s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
  652. s->inter_quant_bias = 0;
  653. } else {
  654. s->intra_quant_bias = 0;
  655. // (a - x / 4) / x
  656. s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
  657. }
  658. if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
  659. av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
  660. return AVERROR(EINVAL);
  661. }
  662. av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
  663. if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
  664. s->avctx->time_base.den > (1 << 16) - 1) {
  665. av_log(avctx, AV_LOG_ERROR,
  666. "timebase %d/%d not supported by MPEG 4 standard, "
  667. "the maximum admitted value for the timebase denominator "
  668. "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
  669. (1 << 16) - 1);
  670. return -1;
  671. }
  672. s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
  673. switch (avctx->codec->id) {
  674. case AV_CODEC_ID_MPEG1VIDEO:
  675. s->out_format = FMT_MPEG1;
  676. s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
  677. avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
  678. break;
  679. case AV_CODEC_ID_MPEG2VIDEO:
  680. s->out_format = FMT_MPEG1;
  681. s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
  682. avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
  683. s->rtp_mode = 1;
  684. break;
  685. case AV_CODEC_ID_MJPEG:
  686. case AV_CODEC_ID_AMV:
  687. s->out_format = FMT_MJPEG;
  688. s->intra_only = 1; /* force intra only for jpeg */
  689. if (!CONFIG_MJPEG_ENCODER ||
  690. ff_mjpeg_encode_init(s) < 0)
  691. return -1;
  692. avctx->delay = 0;
  693. s->low_delay = 1;
  694. break;
  695. case AV_CODEC_ID_H261:
  696. if (!CONFIG_H261_ENCODER)
  697. return -1;
  698. if (ff_h261_get_picture_format(s->width, s->height) < 0) {
  699. av_log(avctx, AV_LOG_ERROR,
  700. "The specified picture size of %dx%d is not valid for the "
  701. "H.261 codec.\nValid sizes are 176x144, 352x288\n",
  702. s->width, s->height);
  703. return -1;
  704. }
  705. s->out_format = FMT_H261;
  706. avctx->delay = 0;
  707. s->low_delay = 1;
  708. s->rtp_mode = 0; /* Sliced encoding not supported */
  709. break;
  710. case AV_CODEC_ID_H263:
  711. if (!CONFIG_H263_ENCODER)
  712. return -1;
  713. if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
  714. s->width, s->height) == 8) {
  715. av_log(avctx, AV_LOG_ERROR,
  716. "The specified picture size of %dx%d is not valid for "
  717. "the H.263 codec.\nValid sizes are 128x96, 176x144, "
  718. "352x288, 704x576, and 1408x1152. "
  719. "Try H.263+.\n", s->width, s->height);
  720. return -1;
  721. }
  722. s->out_format = FMT_H263;
  723. avctx->delay = 0;
  724. s->low_delay = 1;
  725. break;
  726. case AV_CODEC_ID_H263P:
  727. s->out_format = FMT_H263;
  728. s->h263_plus = 1;
  729. /* Fx */
  730. s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
  731. s->modified_quant = s->h263_aic;
  732. s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
  733. s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
  734. /* /Fx */
  735. /* These are just to be sure */
  736. avctx->delay = 0;
  737. s->low_delay = 1;
  738. break;
  739. case AV_CODEC_ID_FLV1:
  740. s->out_format = FMT_H263;
  741. s->h263_flv = 2; /* format = 1; 11-bit codes */
  742. s->unrestricted_mv = 1;
  743. s->rtp_mode = 0; /* don't allow GOB */
  744. avctx->delay = 0;
  745. s->low_delay = 1;
  746. break;
  747. case AV_CODEC_ID_RV10:
  748. s->out_format = FMT_H263;
  749. avctx->delay = 0;
  750. s->low_delay = 1;
  751. break;
  752. case AV_CODEC_ID_RV20:
  753. s->out_format = FMT_H263;
  754. avctx->delay = 0;
  755. s->low_delay = 1;
  756. s->modified_quant = 1;
  757. s->h263_aic = 1;
  758. s->h263_plus = 1;
  759. s->loop_filter = 1;
  760. s->unrestricted_mv = 0;
  761. break;
  762. case AV_CODEC_ID_MPEG4:
  763. s->out_format = FMT_H263;
  764. s->h263_pred = 1;
  765. s->unrestricted_mv = 1;
  766. s->low_delay = s->max_b_frames ? 0 : 1;
  767. avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
  768. break;
  769. case AV_CODEC_ID_MSMPEG4V2:
  770. s->out_format = FMT_H263;
  771. s->h263_pred = 1;
  772. s->unrestricted_mv = 1;
  773. s->msmpeg4_version = 2;
  774. avctx->delay = 0;
  775. s->low_delay = 1;
  776. break;
  777. case AV_CODEC_ID_MSMPEG4V3:
  778. s->out_format = FMT_H263;
  779. s->h263_pred = 1;
  780. s->unrestricted_mv = 1;
  781. s->msmpeg4_version = 3;
  782. s->flipflop_rounding = 1;
  783. avctx->delay = 0;
  784. s->low_delay = 1;
  785. break;
  786. case AV_CODEC_ID_WMV1:
  787. s->out_format = FMT_H263;
  788. s->h263_pred = 1;
  789. s->unrestricted_mv = 1;
  790. s->msmpeg4_version = 4;
  791. s->flipflop_rounding = 1;
  792. avctx->delay = 0;
  793. s->low_delay = 1;
  794. break;
  795. case AV_CODEC_ID_WMV2:
  796. s->out_format = FMT_H263;
  797. s->h263_pred = 1;
  798. s->unrestricted_mv = 1;
  799. s->msmpeg4_version = 5;
  800. s->flipflop_rounding = 1;
  801. avctx->delay = 0;
  802. s->low_delay = 1;
  803. break;
  804. default:
  805. return -1;
  806. }
  807. #if FF_API_PRIVATE_OPT
  808. FF_DISABLE_DEPRECATION_WARNINGS
  809. if (avctx->noise_reduction)
  810. s->noise_reduction = avctx->noise_reduction;
  811. FF_ENABLE_DEPRECATION_WARNINGS
  812. #endif
  813. avctx->has_b_frames = !s->low_delay;
  814. s->encoding = 1;
  815. s->progressive_frame =
  816. s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
  817. AV_CODEC_FLAG_INTERLACED_ME) ||
  818. s->alternate_scan);
  819. /* init */
  820. ff_mpv_idct_init(s);
  821. if (ff_mpv_common_init(s) < 0)
  822. return -1;
  823. ff_fdctdsp_init(&s->fdsp, avctx);
  824. ff_me_cmp_init(&s->mecc, avctx);
  825. ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
  826. ff_pixblockdsp_init(&s->pdsp, avctx);
  827. ff_qpeldsp_init(&s->qdsp);
  828. if (s->msmpeg4_version) {
  829. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  830. 2 * 2 * (MAX_LEVEL + 1) *
  831. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  832. }
  833. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  834. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
  835. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
  836. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
  837. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
  838. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
  839. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
  840. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  841. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  842. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  843. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  844. if (s->noise_reduction) {
  845. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  846. 2 * 64 * sizeof(uint16_t), fail);
  847. }
  848. ff_dct_encode_init(s);
  849. if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
  850. s->chroma_qscale_table = ff_h263_chroma_qscale_table;
  851. if (s->slice_context_count > 1) {
  852. s->rtp_mode = 1;
  853. if (avctx->codec_id == AV_CODEC_ID_H263P)
  854. s->h263_slice_structured = 1;
  855. }
  856. s->quant_precision = 5;
  857. #if FF_API_PRIVATE_OPT
  858. FF_DISABLE_DEPRECATION_WARNINGS
  859. if (avctx->frame_skip_threshold)
  860. s->frame_skip_threshold = avctx->frame_skip_threshold;
  861. if (avctx->frame_skip_factor)
  862. s->frame_skip_factor = avctx->frame_skip_factor;
  863. if (avctx->frame_skip_exp)
  864. s->frame_skip_exp = avctx->frame_skip_exp;
  865. if (avctx->frame_skip_cmp != FF_CMP_DCTMAX)
  866. s->frame_skip_cmp = avctx->frame_skip_cmp;
  867. FF_ENABLE_DEPRECATION_WARNINGS
  868. #endif
  869. ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, s->avctx->ildct_cmp);
  870. ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
  871. if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
  872. ff_h261_encode_init(s);
  873. if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
  874. ff_h263_encode_init(s);
  875. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
  876. if ((ret = ff_msmpeg4_encode_init(s)) < 0)
  877. return ret;
  878. if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  879. && s->out_format == FMT_MPEG1)
  880. ff_mpeg1_encode_init(s);
  881. /* init q matrix */
  882. for (i = 0; i < 64; i++) {
  883. int j = s->idsp.idct_permutation[i];
  884. if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
  885. s->mpeg_quant) {
  886. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  887. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  888. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  889. s->intra_matrix[j] =
  890. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  891. } else {
  892. /* MPEG-1/2 */
  893. s->chroma_intra_matrix[j] =
  894. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  895. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  896. }
  897. if (s->avctx->intra_matrix)
  898. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  899. if (s->avctx->inter_matrix)
  900. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  901. }
  902. /* precompute matrix */
  903. /* for mjpeg, we do include qscale in the matrix */
  904. if (s->out_format != FMT_MJPEG) {
  905. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  906. s->intra_matrix, s->intra_quant_bias, avctx->qmin,
  907. 31, 1);
  908. ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
  909. s->inter_matrix, s->inter_quant_bias, avctx->qmin,
  910. 31, 0);
  911. }
  912. if (ff_rate_control_init(s) < 0)
  913. return -1;
  914. #if FF_API_PRIVATE_OPT
  915. FF_DISABLE_DEPRECATION_WARNINGS
  916. if (avctx->brd_scale)
  917. s->brd_scale = avctx->brd_scale;
  918. if (avctx->prediction_method)
  919. s->pred = avctx->prediction_method + 1;
  920. FF_ENABLE_DEPRECATION_WARNINGS
  921. #endif
  922. if (s->b_frame_strategy == 2) {
  923. for (i = 0; i < s->max_b_frames + 2; i++) {
  924. s->tmp_frames[i] = av_frame_alloc();
  925. if (!s->tmp_frames[i])
  926. return AVERROR(ENOMEM);
  927. s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
  928. s->tmp_frames[i]->width = s->width >> s->brd_scale;
  929. s->tmp_frames[i]->height = s->height >> s->brd_scale;
  930. ret = av_frame_get_buffer(s->tmp_frames[i], 32);
  931. if (ret < 0)
  932. return ret;
  933. }
  934. }
  935. cpb_props = ff_add_cpb_side_data(avctx);
  936. if (!cpb_props)
  937. return AVERROR(ENOMEM);
  938. cpb_props->max_bitrate = avctx->rc_max_rate;
  939. cpb_props->min_bitrate = avctx->rc_min_rate;
  940. cpb_props->avg_bitrate = avctx->bit_rate;
  941. cpb_props->buffer_size = avctx->rc_buffer_size;
  942. return 0;
  943. fail:
  944. ff_mpv_encode_end(avctx);
  945. return AVERROR_UNKNOWN;
  946. }
  947. av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
  948. {
  949. MpegEncContext *s = avctx->priv_data;
  950. int i;
  951. ff_rate_control_uninit(s);
  952. ff_mpv_common_end(s);
  953. if (CONFIG_MJPEG_ENCODER &&
  954. s->out_format == FMT_MJPEG)
  955. ff_mjpeg_encode_close(s);
  956. av_freep(&avctx->extradata);
  957. for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
  958. av_frame_free(&s->tmp_frames[i]);
  959. ff_free_picture_tables(&s->new_picture);
  960. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  961. av_freep(&s->avctx->stats_out);
  962. av_freep(&s->ac_stats);
  963. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  964. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  965. s->q_chroma_intra_matrix= NULL;
  966. s->q_chroma_intra_matrix16= NULL;
  967. av_freep(&s->q_intra_matrix);
  968. av_freep(&s->q_inter_matrix);
  969. av_freep(&s->q_intra_matrix16);
  970. av_freep(&s->q_inter_matrix16);
  971. av_freep(&s->input_picture);
  972. av_freep(&s->reordered_input_picture);
  973. av_freep(&s->dct_offset);
  974. return 0;
  975. }
  976. static int get_sae(uint8_t *src, int ref, int stride)
  977. {
  978. int x,y;
  979. int acc = 0;
  980. for (y = 0; y < 16; y++) {
  981. for (x = 0; x < 16; x++) {
  982. acc += FFABS(src[x + y * stride] - ref);
  983. }
  984. }
  985. return acc;
  986. }
  987. static int get_intra_count(MpegEncContext *s, uint8_t *src,
  988. uint8_t *ref, int stride)
  989. {
  990. int x, y, w, h;
  991. int acc = 0;
  992. w = s->width & ~15;
  993. h = s->height & ~15;
  994. for (y = 0; y < h; y += 16) {
  995. for (x = 0; x < w; x += 16) {
  996. int offset = x + y * stride;
  997. int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
  998. stride, 16);
  999. int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
  1000. int sae = get_sae(src + offset, mean, stride);
  1001. acc += sae + 500 < sad;
  1002. }
  1003. }
  1004. return acc;
  1005. }
  1006. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  1007. {
  1008. return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
  1009. s->chroma_x_shift, s->chroma_y_shift, s->out_format,
  1010. s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
  1011. &s->linesize, &s->uvlinesize);
  1012. }
  1013. static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
  1014. {
  1015. Picture *pic = NULL;
  1016. int64_t pts;
  1017. int i, display_picture_number = 0, ret;
  1018. int encoding_delay = s->max_b_frames ? s->max_b_frames
  1019. : (s->low_delay ? 0 : 1);
  1020. int flush_offset = 1;
  1021. int direct = 1;
  1022. if (pic_arg) {
  1023. pts = pic_arg->pts;
  1024. display_picture_number = s->input_picture_number++;
  1025. if (pts != AV_NOPTS_VALUE) {
  1026. if (s->user_specified_pts != AV_NOPTS_VALUE) {
  1027. int64_t last = s->user_specified_pts;
  1028. if (pts <= last) {
  1029. av_log(s->avctx, AV_LOG_ERROR,
  1030. "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
  1031. pts, last);
  1032. return AVERROR(EINVAL);
  1033. }
  1034. if (!s->low_delay && display_picture_number == 1)
  1035. s->dts_delta = pts - last;
  1036. }
  1037. s->user_specified_pts = pts;
  1038. } else {
  1039. if (s->user_specified_pts != AV_NOPTS_VALUE) {
  1040. s->user_specified_pts =
  1041. pts = s->user_specified_pts + 1;
  1042. av_log(s->avctx, AV_LOG_INFO,
  1043. "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
  1044. pts);
  1045. } else {
  1046. pts = display_picture_number;
  1047. }
  1048. }
  1049. if (!pic_arg->buf[0] ||
  1050. pic_arg->linesize[0] != s->linesize ||
  1051. pic_arg->linesize[1] != s->uvlinesize ||
  1052. pic_arg->linesize[2] != s->uvlinesize)
  1053. direct = 0;
  1054. if ((s->width & 15) || (s->height & 15))
  1055. direct = 0;
  1056. if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
  1057. direct = 0;
  1058. if (s->linesize & (STRIDE_ALIGN-1))
  1059. direct = 0;
  1060. ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
  1061. pic_arg->linesize[1], s->linesize, s->uvlinesize);
  1062. i = ff_find_unused_picture(s->avctx, s->picture, direct);
  1063. if (i < 0)
  1064. return i;
  1065. pic = &s->picture[i];
  1066. pic->reference = 3;
  1067. if (direct) {
  1068. if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
  1069. return ret;
  1070. }
  1071. ret = alloc_picture(s, pic, direct);
  1072. if (ret < 0)
  1073. return ret;
  1074. if (!direct) {
  1075. if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
  1076. pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
  1077. pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
  1078. // empty
  1079. } else {
  1080. int h_chroma_shift, v_chroma_shift;
  1081. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1082. &h_chroma_shift,
  1083. &v_chroma_shift);
  1084. for (i = 0; i < 3; i++) {
  1085. int src_stride = pic_arg->linesize[i];
  1086. int dst_stride = i ? s->uvlinesize : s->linesize;
  1087. int h_shift = i ? h_chroma_shift : 0;
  1088. int v_shift = i ? v_chroma_shift : 0;
  1089. int w = s->width >> h_shift;
  1090. int h = s->height >> v_shift;
  1091. uint8_t *src = pic_arg->data[i];
  1092. uint8_t *dst = pic->f->data[i];
  1093. int vpad = 16;
  1094. if ( s->codec_id == AV_CODEC_ID_MPEG2VIDEO
  1095. && !s->progressive_sequence
  1096. && FFALIGN(s->height, 32) - s->height > 16)
  1097. vpad = 32;
  1098. if (!s->avctx->rc_buffer_size)
  1099. dst += INPLACE_OFFSET;
  1100. if (src_stride == dst_stride)
  1101. memcpy(dst, src, src_stride * h);
  1102. else {
  1103. int h2 = h;
  1104. uint8_t *dst2 = dst;
  1105. while (h2--) {
  1106. memcpy(dst2, src, w);
  1107. dst2 += dst_stride;
  1108. src += src_stride;
  1109. }
  1110. }
  1111. if ((s->width & 15) || (s->height & (vpad-1))) {
  1112. s->mpvencdsp.draw_edges(dst, dst_stride,
  1113. w, h,
  1114. 16 >> h_shift,
  1115. vpad >> v_shift,
  1116. EDGE_BOTTOM);
  1117. }
  1118. }
  1119. emms_c();
  1120. }
  1121. }
  1122. ret = av_frame_copy_props(pic->f, pic_arg);
  1123. if (ret < 0)
  1124. return ret;
  1125. pic->f->display_picture_number = display_picture_number;
  1126. pic->f->pts = pts; // we set this here to avoid modifying pic_arg
  1127. } else {
  1128. /* Flushing: When we have not received enough input frames,
  1129. * ensure s->input_picture[0] contains the first picture */
  1130. for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
  1131. if (s->input_picture[flush_offset])
  1132. break;
  1133. if (flush_offset <= 1)
  1134. flush_offset = 1;
  1135. else
  1136. encoding_delay = encoding_delay - flush_offset + 1;
  1137. }
  1138. /* shift buffer entries */
  1139. for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
  1140. s->input_picture[i - flush_offset] = s->input_picture[i];
  1141. s->input_picture[encoding_delay] = (Picture*) pic;
  1142. return 0;
  1143. }
  1144. static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
  1145. {
  1146. int x, y, plane;
  1147. int score = 0;
  1148. int64_t score64 = 0;
  1149. for (plane = 0; plane < 3; plane++) {
  1150. const int stride = p->f->linesize[plane];
  1151. const int bw = plane ? 1 : 2;
  1152. for (y = 0; y < s->mb_height * bw; y++) {
  1153. for (x = 0; x < s->mb_width * bw; x++) {
  1154. int off = p->shared ? 0 : 16;
  1155. uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
  1156. uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
  1157. int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
  1158. switch (FFABS(s->frame_skip_exp)) {
  1159. case 0: score = FFMAX(score, v); break;
  1160. case 1: score += FFABS(v); break;
  1161. case 2: score64 += v * (int64_t)v; break;
  1162. case 3: score64 += FFABS(v * (int64_t)v * v); break;
  1163. case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. emms_c();
  1169. if (score)
  1170. score64 = score;
  1171. if (s->frame_skip_exp < 0)
  1172. score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
  1173. -1.0/s->frame_skip_exp);
  1174. if (score64 < s->frame_skip_threshold)
  1175. return 1;
  1176. if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
  1177. return 1;
  1178. return 0;
  1179. }
  1180. static int encode_frame(AVCodecContext *c, AVFrame *frame)
  1181. {
  1182. AVPacket pkt = { 0 };
  1183. int ret;
  1184. int size = 0;
  1185. av_init_packet(&pkt);
  1186. ret = avcodec_send_frame(c, frame);
  1187. if (ret < 0)
  1188. return ret;
  1189. do {
  1190. ret = avcodec_receive_packet(c, &pkt);
  1191. if (ret >= 0) {
  1192. size += pkt.size;
  1193. av_packet_unref(&pkt);
  1194. } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  1195. return ret;
  1196. } while (ret >= 0);
  1197. return size;
  1198. }
  1199. static int estimate_best_b_count(MpegEncContext *s)
  1200. {
  1201. const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
  1202. const int scale = s->brd_scale;
  1203. int width = s->width >> scale;
  1204. int height = s->height >> scale;
  1205. int i, j, out_size, p_lambda, b_lambda, lambda2;
  1206. int64_t best_rd = INT64_MAX;
  1207. int best_b_count = -1;
  1208. int ret = 0;
  1209. av_assert0(scale >= 0 && scale <= 3);
  1210. //emms_c();
  1211. //s->next_picture_ptr->quality;
  1212. p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
  1213. //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
  1214. b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
  1215. if (!b_lambda) // FIXME we should do this somewhere else
  1216. b_lambda = p_lambda;
  1217. lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
  1218. FF_LAMBDA_SHIFT;
  1219. for (i = 0; i < s->max_b_frames + 2; i++) {
  1220. Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
  1221. s->next_picture_ptr;
  1222. uint8_t *data[4];
  1223. if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
  1224. pre_input = *pre_input_ptr;
  1225. memcpy(data, pre_input_ptr->f->data, sizeof(data));
  1226. if (!pre_input.shared && i) {
  1227. data[0] += INPLACE_OFFSET;
  1228. data[1] += INPLACE_OFFSET;
  1229. data[2] += INPLACE_OFFSET;
  1230. }
  1231. s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
  1232. s->tmp_frames[i]->linesize[0],
  1233. data[0],
  1234. pre_input.f->linesize[0],
  1235. width, height);
  1236. s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
  1237. s->tmp_frames[i]->linesize[1],
  1238. data[1],
  1239. pre_input.f->linesize[1],
  1240. width >> 1, height >> 1);
  1241. s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
  1242. s->tmp_frames[i]->linesize[2],
  1243. data[2],
  1244. pre_input.f->linesize[2],
  1245. width >> 1, height >> 1);
  1246. }
  1247. }
  1248. for (j = 0; j < s->max_b_frames + 1; j++) {
  1249. AVCodecContext *c;
  1250. int64_t rd = 0;
  1251. if (!s->input_picture[j])
  1252. break;
  1253. c = avcodec_alloc_context3(NULL);
  1254. if (!c)
  1255. return AVERROR(ENOMEM);
  1256. c->width = width;
  1257. c->height = height;
  1258. c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
  1259. c->flags |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
  1260. c->mb_decision = s->avctx->mb_decision;
  1261. c->me_cmp = s->avctx->me_cmp;
  1262. c->mb_cmp = s->avctx->mb_cmp;
  1263. c->me_sub_cmp = s->avctx->me_sub_cmp;
  1264. c->pix_fmt = AV_PIX_FMT_YUV420P;
  1265. c->time_base = s->avctx->time_base;
  1266. c->max_b_frames = s->max_b_frames;
  1267. ret = avcodec_open2(c, codec, NULL);
  1268. if (ret < 0)
  1269. goto fail;
  1270. s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
  1271. s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
  1272. out_size = encode_frame(c, s->tmp_frames[0]);
  1273. if (out_size < 0) {
  1274. ret = out_size;
  1275. goto fail;
  1276. }
  1277. //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
  1278. for (i = 0; i < s->max_b_frames + 1; i++) {
  1279. int is_p = i % (j + 1) == j || i == s->max_b_frames;
  1280. s->tmp_frames[i + 1]->pict_type = is_p ?
  1281. AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
  1282. s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
  1283. out_size = encode_frame(c, s->tmp_frames[i + 1]);
  1284. if (out_size < 0) {
  1285. ret = out_size;
  1286. goto fail;
  1287. }
  1288. rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
  1289. }
  1290. /* get the delayed frames */
  1291. out_size = encode_frame(c, NULL);
  1292. if (out_size < 0) {
  1293. ret = out_size;
  1294. goto fail;
  1295. }
  1296. rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
  1297. rd += c->error[0] + c->error[1] + c->error[2];
  1298. if (rd < best_rd) {
  1299. best_rd = rd;
  1300. best_b_count = j;
  1301. }
  1302. fail:
  1303. avcodec_free_context(&c);
  1304. if (ret < 0)
  1305. return ret;
  1306. }
  1307. return best_b_count;
  1308. }
  1309. static int select_input_picture(MpegEncContext *s)
  1310. {
  1311. int i, ret;
  1312. for (i = 1; i < MAX_PICTURE_COUNT; i++)
  1313. s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
  1314. s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
  1315. /* set next picture type & ordering */
  1316. if (!s->reordered_input_picture[0] && s->input_picture[0]) {
  1317. if (s->frame_skip_threshold || s->frame_skip_factor) {
  1318. if (s->picture_in_gop_number < s->gop_size &&
  1319. s->next_picture_ptr &&
  1320. skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
  1321. // FIXME check that the gop check above is +-1 correct
  1322. av_frame_unref(s->input_picture[0]->f);
  1323. ff_vbv_update(s, 0);
  1324. goto no_output_pic;
  1325. }
  1326. }
  1327. if (/*s->picture_in_gop_number >= s->gop_size ||*/
  1328. !s->next_picture_ptr || s->intra_only) {
  1329. s->reordered_input_picture[0] = s->input_picture[0];
  1330. s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
  1331. s->reordered_input_picture[0]->f->coded_picture_number =
  1332. s->coded_picture_number++;
  1333. } else {
  1334. int b_frames = 0;
  1335. if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
  1336. for (i = 0; i < s->max_b_frames + 1; i++) {
  1337. int pict_num = s->input_picture[0]->f->display_picture_number + i;
  1338. if (pict_num >= s->rc_context.num_entries)
  1339. break;
  1340. if (!s->input_picture[i]) {
  1341. s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
  1342. break;
  1343. }
  1344. s->input_picture[i]->f->pict_type =
  1345. s->rc_context.entry[pict_num].new_pict_type;
  1346. }
  1347. }
  1348. if (s->b_frame_strategy == 0) {
  1349. b_frames = s->max_b_frames;
  1350. while (b_frames && !s->input_picture[b_frames])
  1351. b_frames--;
  1352. } else if (s->b_frame_strategy == 1) {
  1353. for (i = 1; i < s->max_b_frames + 1; i++) {
  1354. if (s->input_picture[i] &&
  1355. s->input_picture[i]->b_frame_score == 0) {
  1356. s->input_picture[i]->b_frame_score =
  1357. get_intra_count(s,
  1358. s->input_picture[i ]->f->data[0],
  1359. s->input_picture[i - 1]->f->data[0],
  1360. s->linesize) + 1;
  1361. }
  1362. }
  1363. for (i = 0; i < s->max_b_frames + 1; i++) {
  1364. if (!s->input_picture[i] ||
  1365. s->input_picture[i]->b_frame_score - 1 >
  1366. s->mb_num / s->b_sensitivity)
  1367. break;
  1368. }
  1369. b_frames = FFMAX(0, i - 1);
  1370. /* reset scores */
  1371. for (i = 0; i < b_frames + 1; i++) {
  1372. s->input_picture[i]->b_frame_score = 0;
  1373. }
  1374. } else if (s->b_frame_strategy == 2) {
  1375. b_frames = estimate_best_b_count(s);
  1376. if (b_frames < 0)
  1377. return b_frames;
  1378. }
  1379. emms_c();
  1380. for (i = b_frames - 1; i >= 0; i--) {
  1381. int type = s->input_picture[i]->f->pict_type;
  1382. if (type && type != AV_PICTURE_TYPE_B)
  1383. b_frames = i;
  1384. }
  1385. if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
  1386. b_frames == s->max_b_frames) {
  1387. av_log(s->avctx, AV_LOG_ERROR,
  1388. "warning, too many B-frames in a row\n");
  1389. }
  1390. if (s->picture_in_gop_number + b_frames >= s->gop_size) {
  1391. if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
  1392. s->gop_size > s->picture_in_gop_number) {
  1393. b_frames = s->gop_size - s->picture_in_gop_number - 1;
  1394. } else {
  1395. if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
  1396. b_frames = 0;
  1397. s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
  1398. }
  1399. }
  1400. if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
  1401. s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
  1402. b_frames--;
  1403. s->reordered_input_picture[0] = s->input_picture[b_frames];
  1404. if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
  1405. s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
  1406. s->reordered_input_picture[0]->f->coded_picture_number =
  1407. s->coded_picture_number++;
  1408. for (i = 0; i < b_frames; i++) {
  1409. s->reordered_input_picture[i + 1] = s->input_picture[i];
  1410. s->reordered_input_picture[i + 1]->f->pict_type =
  1411. AV_PICTURE_TYPE_B;
  1412. s->reordered_input_picture[i + 1]->f->coded_picture_number =
  1413. s->coded_picture_number++;
  1414. }
  1415. }
  1416. }
  1417. no_output_pic:
  1418. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1419. if (s->reordered_input_picture[0]) {
  1420. s->reordered_input_picture[0]->reference =
  1421. s->reordered_input_picture[0]->f->pict_type !=
  1422. AV_PICTURE_TYPE_B ? 3 : 0;
  1423. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
  1424. return ret;
  1425. if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
  1426. // input is a shared pix, so we can't modify it -> allocate a new
  1427. // one & ensure that the shared one is reuseable
  1428. Picture *pic;
  1429. int i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1430. if (i < 0)
  1431. return i;
  1432. pic = &s->picture[i];
  1433. pic->reference = s->reordered_input_picture[0]->reference;
  1434. if (alloc_picture(s, pic, 0) < 0) {
  1435. return -1;
  1436. }
  1437. ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
  1438. if (ret < 0)
  1439. return ret;
  1440. /* mark us unused / free shared pic */
  1441. av_frame_unref(s->reordered_input_picture[0]->f);
  1442. s->reordered_input_picture[0]->shared = 0;
  1443. s->current_picture_ptr = pic;
  1444. } else {
  1445. // input is not a shared pix -> reuse buffer for current_pix
  1446. s->current_picture_ptr = s->reordered_input_picture[0];
  1447. for (i = 0; i < 4; i++) {
  1448. s->new_picture.f->data[i] += INPLACE_OFFSET;
  1449. }
  1450. }
  1451. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1452. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1453. s->current_picture_ptr)) < 0)
  1454. return ret;
  1455. s->picture_number = s->new_picture.f->display_picture_number;
  1456. }
  1457. return 0;
  1458. }
  1459. static void frame_end(MpegEncContext *s)
  1460. {
  1461. if (s->unrestricted_mv &&
  1462. s->current_picture.reference &&
  1463. !s->intra_only) {
  1464. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1465. int hshift = desc->log2_chroma_w;
  1466. int vshift = desc->log2_chroma_h;
  1467. s->mpvencdsp.draw_edges(s->current_picture.f->data[0],
  1468. s->current_picture.f->linesize[0],
  1469. s->h_edge_pos, s->v_edge_pos,
  1470. EDGE_WIDTH, EDGE_WIDTH,
  1471. EDGE_TOP | EDGE_BOTTOM);
  1472. s->mpvencdsp.draw_edges(s->current_picture.f->data[1],
  1473. s->current_picture.f->linesize[1],
  1474. s->h_edge_pos >> hshift,
  1475. s->v_edge_pos >> vshift,
  1476. EDGE_WIDTH >> hshift,
  1477. EDGE_WIDTH >> vshift,
  1478. EDGE_TOP | EDGE_BOTTOM);
  1479. s->mpvencdsp.draw_edges(s->current_picture.f->data[2],
  1480. s->current_picture.f->linesize[2],
  1481. s->h_edge_pos >> hshift,
  1482. s->v_edge_pos >> vshift,
  1483. EDGE_WIDTH >> hshift,
  1484. EDGE_WIDTH >> vshift,
  1485. EDGE_TOP | EDGE_BOTTOM);
  1486. }
  1487. emms_c();
  1488. s->last_pict_type = s->pict_type;
  1489. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
  1490. if (s->pict_type!= AV_PICTURE_TYPE_B)
  1491. s->last_non_b_pict_type = s->pict_type;
  1492. #if FF_API_CODED_FRAME
  1493. FF_DISABLE_DEPRECATION_WARNINGS
  1494. av_frame_unref(s->avctx->coded_frame);
  1495. av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
  1496. FF_ENABLE_DEPRECATION_WARNINGS
  1497. #endif
  1498. #if FF_API_ERROR_FRAME
  1499. FF_DISABLE_DEPRECATION_WARNINGS
  1500. memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
  1501. sizeof(s->current_picture.encoding_error));
  1502. FF_ENABLE_DEPRECATION_WARNINGS
  1503. #endif
  1504. }
  1505. static void update_noise_reduction(MpegEncContext *s)
  1506. {
  1507. int intra, i;
  1508. for (intra = 0; intra < 2; intra++) {
  1509. if (s->dct_count[intra] > (1 << 16)) {
  1510. for (i = 0; i < 64; i++) {
  1511. s->dct_error_sum[intra][i] >>= 1;
  1512. }
  1513. s->dct_count[intra] >>= 1;
  1514. }
  1515. for (i = 0; i < 64; i++) {
  1516. s->dct_offset[intra][i] = (s->noise_reduction *
  1517. s->dct_count[intra] +
  1518. s->dct_error_sum[intra][i] / 2) /
  1519. (s->dct_error_sum[intra][i] + 1);
  1520. }
  1521. }
  1522. }
  1523. static int frame_start(MpegEncContext *s)
  1524. {
  1525. int ret;
  1526. /* mark & release old frames */
  1527. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1528. s->last_picture_ptr != s->next_picture_ptr &&
  1529. s->last_picture_ptr->f->buf[0]) {
  1530. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1531. }
  1532. s->current_picture_ptr->f->pict_type = s->pict_type;
  1533. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1534. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1535. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1536. s->current_picture_ptr)) < 0)
  1537. return ret;
  1538. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1539. s->last_picture_ptr = s->next_picture_ptr;
  1540. if (!s->droppable)
  1541. s->next_picture_ptr = s->current_picture_ptr;
  1542. }
  1543. if (s->last_picture_ptr) {
  1544. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1545. if (s->last_picture_ptr->f->buf[0] &&
  1546. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1547. s->last_picture_ptr)) < 0)
  1548. return ret;
  1549. }
  1550. if (s->next_picture_ptr) {
  1551. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1552. if (s->next_picture_ptr->f->buf[0] &&
  1553. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1554. s->next_picture_ptr)) < 0)
  1555. return ret;
  1556. }
  1557. if (s->picture_structure!= PICT_FRAME) {
  1558. int i;
  1559. for (i = 0; i < 4; i++) {
  1560. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1561. s->current_picture.f->data[i] +=
  1562. s->current_picture.f->linesize[i];
  1563. }
  1564. s->current_picture.f->linesize[i] *= 2;
  1565. s->last_picture.f->linesize[i] *= 2;
  1566. s->next_picture.f->linesize[i] *= 2;
  1567. }
  1568. }
  1569. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1570. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1571. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1572. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1573. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1574. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1575. } else {
  1576. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1577. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1578. }
  1579. if (s->dct_error_sum) {
  1580. av_assert2(s->noise_reduction && s->encoding);
  1581. update_noise_reduction(s);
  1582. }
  1583. return 0;
  1584. }
  1585. int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  1586. const AVFrame *pic_arg, int *got_packet)
  1587. {
  1588. MpegEncContext *s = avctx->priv_data;
  1589. int i, stuffing_count, ret;
  1590. int context_count = s->slice_context_count;
  1591. s->vbv_ignore_qmax = 0;
  1592. s->picture_in_gop_number++;
  1593. if (load_input_picture(s, pic_arg) < 0)
  1594. return -1;
  1595. if (select_input_picture(s) < 0) {
  1596. return -1;
  1597. }
  1598. /* output? */
  1599. if (s->new_picture.f->data[0]) {
  1600. int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
  1601. int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
  1602. :
  1603. s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
  1604. if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
  1605. return ret;
  1606. if (s->mb_info) {
  1607. s->mb_info_ptr = av_packet_new_side_data(pkt,
  1608. AV_PKT_DATA_H263_MB_INFO,
  1609. s->mb_width*s->mb_height*12);
  1610. s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
  1611. }
  1612. for (i = 0; i < context_count; i++) {
  1613. int start_y = s->thread_context[i]->start_mb_y;
  1614. int end_y = s->thread_context[i]-> end_mb_y;
  1615. int h = s->mb_height;
  1616. uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
  1617. uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
  1618. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1619. }
  1620. s->pict_type = s->new_picture.f->pict_type;
  1621. //emms_c();
  1622. ret = frame_start(s);
  1623. if (ret < 0)
  1624. return ret;
  1625. vbv_retry:
  1626. ret = encode_picture(s, s->picture_number);
  1627. if (growing_buffer) {
  1628. av_assert0(s->pb.buf == avctx->internal->byte_buffer);
  1629. pkt->data = s->pb.buf;
  1630. pkt->size = avctx->internal->byte_buffer_size;
  1631. }
  1632. if (ret < 0)
  1633. return -1;
  1634. #if FF_API_STAT_BITS
  1635. FF_DISABLE_DEPRECATION_WARNINGS
  1636. avctx->header_bits = s->header_bits;
  1637. avctx->mv_bits = s->mv_bits;
  1638. avctx->misc_bits = s->misc_bits;
  1639. avctx->i_tex_bits = s->i_tex_bits;
  1640. avctx->p_tex_bits = s->p_tex_bits;
  1641. avctx->i_count = s->i_count;
  1642. // FIXME f/b_count in avctx
  1643. avctx->p_count = s->mb_num - s->i_count - s->skip_count;
  1644. avctx->skip_count = s->skip_count;
  1645. FF_ENABLE_DEPRECATION_WARNINGS
  1646. #endif
  1647. frame_end(s);
  1648. if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
  1649. ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
  1650. if (avctx->rc_buffer_size) {
  1651. RateControlContext *rcc = &s->rc_context;
  1652. int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
  1653. int hq = (s->avctx->mb_decision == FF_MB_DECISION_RD || s->avctx->trellis);
  1654. int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
  1655. if (put_bits_count(&s->pb) > max_size &&
  1656. s->lambda < s->lmax) {
  1657. s->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
  1658. (s->qscale + 1) / s->qscale);
  1659. if (s->adaptive_quant) {
  1660. int i;
  1661. for (i = 0; i < s->mb_height * s->mb_stride; i++)
  1662. s->lambda_table[i] =
  1663. FFMAX(s->lambda_table[i] + min_step,
  1664. s->lambda_table[i] * (s->qscale + 1) /
  1665. s->qscale);
  1666. }
  1667. s->mb_skipped = 0; // done in frame_start()
  1668. // done in encode_picture() so we must undo it
  1669. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1670. if (s->flipflop_rounding ||
  1671. s->codec_id == AV_CODEC_ID_H263P ||
  1672. s->codec_id == AV_CODEC_ID_MPEG4)
  1673. s->no_rounding ^= 1;
  1674. }
  1675. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1676. s->time_base = s->last_time_base;
  1677. s->last_non_b_time = s->time - s->pp_time;
  1678. }
  1679. for (i = 0; i < context_count; i++) {
  1680. PutBitContext *pb = &s->thread_context[i]->pb;
  1681. init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
  1682. }
  1683. s->vbv_ignore_qmax = 1;
  1684. av_log(s->avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
  1685. goto vbv_retry;
  1686. }
  1687. av_assert0(s->avctx->rc_max_rate);
  1688. }
  1689. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1690. ff_write_pass1_stats(s);
  1691. for (i = 0; i < 4; i++) {
  1692. s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
  1693. avctx->error[i] += s->current_picture_ptr->encoding_error[i];
  1694. }
  1695. ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
  1696. s->current_picture_ptr->encoding_error,
  1697. (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
  1698. s->pict_type);
  1699. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1700. assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
  1701. s->misc_bits + s->i_tex_bits +
  1702. s->p_tex_bits);
  1703. flush_put_bits(&s->pb);
  1704. s->frame_bits = put_bits_count(&s->pb);
  1705. stuffing_count = ff_vbv_update(s, s->frame_bits);
  1706. s->stuffing_bits = 8*stuffing_count;
  1707. if (stuffing_count) {
  1708. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
  1709. stuffing_count + 50) {
  1710. av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
  1711. return -1;
  1712. }
  1713. switch (s->codec_id) {
  1714. case AV_CODEC_ID_MPEG1VIDEO:
  1715. case AV_CODEC_ID_MPEG2VIDEO:
  1716. while (stuffing_count--) {
  1717. put_bits(&s->pb, 8, 0);
  1718. }
  1719. break;
  1720. case AV_CODEC_ID_MPEG4:
  1721. put_bits(&s->pb, 16, 0);
  1722. put_bits(&s->pb, 16, 0x1C3);
  1723. stuffing_count -= 4;
  1724. while (stuffing_count--) {
  1725. put_bits(&s->pb, 8, 0xFF);
  1726. }
  1727. break;
  1728. default:
  1729. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1730. }
  1731. flush_put_bits(&s->pb);
  1732. s->frame_bits = put_bits_count(&s->pb);
  1733. }
  1734. /* update MPEG-1/2 vbv_delay for CBR */
  1735. if (s->avctx->rc_max_rate &&
  1736. s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
  1737. s->out_format == FMT_MPEG1 &&
  1738. 90000LL * (avctx->rc_buffer_size - 1) <=
  1739. s->avctx->rc_max_rate * 0xFFFFLL) {
  1740. AVCPBProperties *props;
  1741. size_t props_size;
  1742. int vbv_delay, min_delay;
  1743. double inbits = s->avctx->rc_max_rate *
  1744. av_q2d(s->avctx->time_base);
  1745. int minbits = s->frame_bits - 8 *
  1746. (s->vbv_delay_ptr - s->pb.buf - 1);
  1747. double bits = s->rc_context.buffer_index + minbits - inbits;
  1748. if (bits < 0)
  1749. av_log(s->avctx, AV_LOG_ERROR,
  1750. "Internal error, negative bits\n");
  1751. av_assert1(s->repeat_first_field == 0);
  1752. vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
  1753. min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
  1754. s->avctx->rc_max_rate;
  1755. vbv_delay = FFMAX(vbv_delay, min_delay);
  1756. av_assert0(vbv_delay < 0xFFFF);
  1757. s->vbv_delay_ptr[0] &= 0xF8;
  1758. s->vbv_delay_ptr[0] |= vbv_delay >> 13;
  1759. s->vbv_delay_ptr[1] = vbv_delay >> 5;
  1760. s->vbv_delay_ptr[2] &= 0x07;
  1761. s->vbv_delay_ptr[2] |= vbv_delay << 3;
  1762. props = av_cpb_properties_alloc(&props_size);
  1763. if (!props)
  1764. return AVERROR(ENOMEM);
  1765. props->vbv_delay = vbv_delay * 300;
  1766. ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
  1767. (uint8_t*)props, props_size);
  1768. if (ret < 0) {
  1769. av_freep(&props);
  1770. return ret;
  1771. }
  1772. #if FF_API_VBV_DELAY
  1773. FF_DISABLE_DEPRECATION_WARNINGS
  1774. avctx->vbv_delay = vbv_delay * 300;
  1775. FF_ENABLE_DEPRECATION_WARNINGS
  1776. #endif
  1777. }
  1778. s->total_bits += s->frame_bits;
  1779. #if FF_API_STAT_BITS
  1780. FF_DISABLE_DEPRECATION_WARNINGS
  1781. avctx->frame_bits = s->frame_bits;
  1782. FF_ENABLE_DEPRECATION_WARNINGS
  1783. #endif
  1784. pkt->pts = s->current_picture.f->pts;
  1785. if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
  1786. if (!s->current_picture.f->coded_picture_number)
  1787. pkt->dts = pkt->pts - s->dts_delta;
  1788. else
  1789. pkt->dts = s->reordered_pts;
  1790. s->reordered_pts = pkt->pts;
  1791. } else
  1792. pkt->dts = pkt->pts;
  1793. if (s->current_picture.f->key_frame)
  1794. pkt->flags |= AV_PKT_FLAG_KEY;
  1795. if (s->mb_info)
  1796. av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
  1797. } else {
  1798. s->frame_bits = 0;
  1799. }
  1800. /* release non-reference frames */
  1801. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1802. if (!s->picture[i].reference)
  1803. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1804. }
  1805. av_assert1((s->frame_bits & 7) == 0);
  1806. pkt->size = s->frame_bits / 8;
  1807. *got_packet = !!pkt->size;
  1808. return 0;
  1809. }
  1810. static inline void dct_single_coeff_elimination(MpegEncContext *s,
  1811. int n, int threshold)
  1812. {
  1813. static const char tab[64] = {
  1814. 3, 2, 2, 1, 1, 1, 1, 1,
  1815. 1, 1, 1, 1, 1, 1, 1, 1,
  1816. 1, 1, 1, 1, 1, 1, 1, 1,
  1817. 0, 0, 0, 0, 0, 0, 0, 0,
  1818. 0, 0, 0, 0, 0, 0, 0, 0,
  1819. 0, 0, 0, 0, 0, 0, 0, 0,
  1820. 0, 0, 0, 0, 0, 0, 0, 0,
  1821. 0, 0, 0, 0, 0, 0, 0, 0
  1822. };
  1823. int score = 0;
  1824. int run = 0;
  1825. int i;
  1826. int16_t *block = s->block[n];
  1827. const int last_index = s->block_last_index[n];
  1828. int skip_dc;
  1829. if (threshold < 0) {
  1830. skip_dc = 0;
  1831. threshold = -threshold;
  1832. } else
  1833. skip_dc = 1;
  1834. /* Are all we could set to zero already zero? */
  1835. if (last_index <= skip_dc - 1)
  1836. return;
  1837. for (i = 0; i <= last_index; i++) {
  1838. const int j = s->intra_scantable.permutated[i];
  1839. const int level = FFABS(block[j]);
  1840. if (level == 1) {
  1841. if (skip_dc && i == 0)
  1842. continue;
  1843. score += tab[run];
  1844. run = 0;
  1845. } else if (level > 1) {
  1846. return;
  1847. } else {
  1848. run++;
  1849. }
  1850. }
  1851. if (score >= threshold)
  1852. return;
  1853. for (i = skip_dc; i <= last_index; i++) {
  1854. const int j = s->intra_scantable.permutated[i];
  1855. block[j] = 0;
  1856. }
  1857. if (block[0])
  1858. s->block_last_index[n] = 0;
  1859. else
  1860. s->block_last_index[n] = -1;
  1861. }
  1862. static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
  1863. int last_index)
  1864. {
  1865. int i;
  1866. const int maxlevel = s->max_qcoeff;
  1867. const int minlevel = s->min_qcoeff;
  1868. int overflow = 0;
  1869. if (s->mb_intra) {
  1870. i = 1; // skip clipping of intra dc
  1871. } else
  1872. i = 0;
  1873. for (; i <= last_index; i++) {
  1874. const int j = s->intra_scantable.permutated[i];
  1875. int level = block[j];
  1876. if (level > maxlevel) {
  1877. level = maxlevel;
  1878. overflow++;
  1879. } else if (level < minlevel) {
  1880. level = minlevel;
  1881. overflow++;
  1882. }
  1883. block[j] = level;
  1884. }
  1885. if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  1886. av_log(s->avctx, AV_LOG_INFO,
  1887. "warning, clipping %d dct coefficients to %d..%d\n",
  1888. overflow, minlevel, maxlevel);
  1889. }
  1890. static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
  1891. {
  1892. int x, y;
  1893. // FIXME optimize
  1894. for (y = 0; y < 8; y++) {
  1895. for (x = 0; x < 8; x++) {
  1896. int x2, y2;
  1897. int sum = 0;
  1898. int sqr = 0;
  1899. int count = 0;
  1900. for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
  1901. for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
  1902. int v = ptr[x2 + y2 * stride];
  1903. sum += v;
  1904. sqr += v * v;
  1905. count++;
  1906. }
  1907. }
  1908. weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
  1909. }
  1910. }
  1911. }
  1912. static av_always_inline void encode_mb_internal(MpegEncContext *s,
  1913. int motion_x, int motion_y,
  1914. int mb_block_height,
  1915. int mb_block_width,
  1916. int mb_block_count)
  1917. {
  1918. int16_t weight[12][64];
  1919. int16_t orig[12][64];
  1920. const int mb_x = s->mb_x;
  1921. const int mb_y = s->mb_y;
  1922. int i;
  1923. int skip_dct[12];
  1924. int dct_offset = s->linesize * 8; // default for progressive frames
  1925. int uv_dct_offset = s->uvlinesize * 8;
  1926. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1927. ptrdiff_t wrap_y, wrap_c;
  1928. for (i = 0; i < mb_block_count; i++)
  1929. skip_dct[i] = s->skipdct;
  1930. if (s->adaptive_quant) {
  1931. const int last_qp = s->qscale;
  1932. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1933. s->lambda = s->lambda_table[mb_xy];
  1934. update_qscale(s);
  1935. if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
  1936. s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
  1937. s->dquant = s->qscale - last_qp;
  1938. if (s->out_format == FMT_H263) {
  1939. s->dquant = av_clip(s->dquant, -2, 2);
  1940. if (s->codec_id == AV_CODEC_ID_MPEG4) {
  1941. if (!s->mb_intra) {
  1942. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1943. if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
  1944. s->dquant = 0;
  1945. }
  1946. if (s->mv_type == MV_TYPE_8X8)
  1947. s->dquant = 0;
  1948. }
  1949. }
  1950. }
  1951. }
  1952. ff_set_qscale(s, last_qp + s->dquant);
  1953. } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
  1954. ff_set_qscale(s, s->qscale + s->dquant);
  1955. wrap_y = s->linesize;
  1956. wrap_c = s->uvlinesize;
  1957. ptr_y = s->new_picture.f->data[0] +
  1958. (mb_y * 16 * wrap_y) + mb_x * 16;
  1959. ptr_cb = s->new_picture.f->data[1] +
  1960. (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
  1961. ptr_cr = s->new_picture.f->data[2] +
  1962. (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
  1963. if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
  1964. uint8_t *ebuf = s->sc.edge_emu_buffer + 38 * wrap_y;
  1965. int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
  1966. int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
  1967. s->vdsp.emulated_edge_mc(ebuf, ptr_y,
  1968. wrap_y, wrap_y,
  1969. 16, 16, mb_x * 16, mb_y * 16,
  1970. s->width, s->height);
  1971. ptr_y = ebuf;
  1972. s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
  1973. wrap_c, wrap_c,
  1974. mb_block_width, mb_block_height,
  1975. mb_x * mb_block_width, mb_y * mb_block_height,
  1976. cw, ch);
  1977. ptr_cb = ebuf + 16 * wrap_y;
  1978. s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
  1979. wrap_c, wrap_c,
  1980. mb_block_width, mb_block_height,
  1981. mb_x * mb_block_width, mb_y * mb_block_height,
  1982. cw, ch);
  1983. ptr_cr = ebuf + 16 * wrap_y + 16;
  1984. }
  1985. if (s->mb_intra) {
  1986. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  1987. int progressive_score, interlaced_score;
  1988. s->interlaced_dct = 0;
  1989. progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
  1990. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
  1991. NULL, wrap_y, 8) - 400;
  1992. if (progressive_score > 0) {
  1993. interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
  1994. NULL, wrap_y * 2, 8) +
  1995. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
  1996. NULL, wrap_y * 2, 8);
  1997. if (progressive_score > interlaced_score) {
  1998. s->interlaced_dct = 1;
  1999. dct_offset = wrap_y;
  2000. uv_dct_offset = wrap_c;
  2001. wrap_y <<= 1;
  2002. if (s->chroma_format == CHROMA_422 ||
  2003. s->chroma_format == CHROMA_444)
  2004. wrap_c <<= 1;
  2005. }
  2006. }
  2007. }
  2008. s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
  2009. s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  2010. s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
  2011. s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  2012. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  2013. skip_dct[4] = 1;
  2014. skip_dct[5] = 1;
  2015. } else {
  2016. s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  2017. s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  2018. if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
  2019. s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
  2020. s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
  2021. } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
  2022. s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
  2023. s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
  2024. s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
  2025. s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
  2026. s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
  2027. s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
  2028. }
  2029. }
  2030. } else {
  2031. op_pixels_func (*op_pix)[4];
  2032. qpel_mc_func (*op_qpix)[16];
  2033. uint8_t *dest_y, *dest_cb, *dest_cr;
  2034. dest_y = s->dest[0];
  2035. dest_cb = s->dest[1];
  2036. dest_cr = s->dest[2];
  2037. if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
  2038. op_pix = s->hdsp.put_pixels_tab;
  2039. op_qpix = s->qdsp.put_qpel_pixels_tab;
  2040. } else {
  2041. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2042. op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
  2043. }
  2044. if (s->mv_dir & MV_DIR_FORWARD) {
  2045. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
  2046. s->last_picture.f->data,
  2047. op_pix, op_qpix);
  2048. op_pix = s->hdsp.avg_pixels_tab;
  2049. op_qpix = s->qdsp.avg_qpel_pixels_tab;
  2050. }
  2051. if (s->mv_dir & MV_DIR_BACKWARD) {
  2052. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
  2053. s->next_picture.f->data,
  2054. op_pix, op_qpix);
  2055. }
  2056. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  2057. int progressive_score, interlaced_score;
  2058. s->interlaced_dct = 0;
  2059. progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
  2060. s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
  2061. ptr_y + wrap_y * 8,
  2062. wrap_y, 8) - 400;
  2063. if (s->avctx->ildct_cmp == FF_CMP_VSSE)
  2064. progressive_score -= 400;
  2065. if (progressive_score > 0) {
  2066. interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
  2067. wrap_y * 2, 8) +
  2068. s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
  2069. ptr_y + wrap_y,
  2070. wrap_y * 2, 8);
  2071. if (progressive_score > interlaced_score) {
  2072. s->interlaced_dct = 1;
  2073. dct_offset = wrap_y;
  2074. uv_dct_offset = wrap_c;
  2075. wrap_y <<= 1;
  2076. if (s->chroma_format == CHROMA_422)
  2077. wrap_c <<= 1;
  2078. }
  2079. }
  2080. }
  2081. s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
  2082. s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2083. s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
  2084. dest_y + dct_offset, wrap_y);
  2085. s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
  2086. dest_y + dct_offset + 8, wrap_y);
  2087. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  2088. skip_dct[4] = 1;
  2089. skip_dct[5] = 1;
  2090. } else {
  2091. s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2092. s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2093. if (!s->chroma_y_shift) { /* 422 */
  2094. s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
  2095. dest_cb + uv_dct_offset, wrap_c);
  2096. s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
  2097. dest_cr + uv_dct_offset, wrap_c);
  2098. }
  2099. }
  2100. /* pre quantization */
  2101. if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
  2102. 2 * s->qscale * s->qscale) {
  2103. // FIXME optimize
  2104. if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
  2105. skip_dct[0] = 1;
  2106. if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
  2107. skip_dct[1] = 1;
  2108. if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
  2109. wrap_y, 8) < 20 * s->qscale)
  2110. skip_dct[2] = 1;
  2111. if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
  2112. wrap_y, 8) < 20 * s->qscale)
  2113. skip_dct[3] = 1;
  2114. if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
  2115. skip_dct[4] = 1;
  2116. if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
  2117. skip_dct[5] = 1;
  2118. if (!s->chroma_y_shift) { /* 422 */
  2119. if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
  2120. dest_cb + uv_dct_offset,
  2121. wrap_c, 8) < 20 * s->qscale)
  2122. skip_dct[6] = 1;
  2123. if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
  2124. dest_cr + uv_dct_offset,
  2125. wrap_c, 8) < 20 * s->qscale)
  2126. skip_dct[7] = 1;
  2127. }
  2128. }
  2129. }
  2130. if (s->quantizer_noise_shaping) {
  2131. if (!skip_dct[0])
  2132. get_visual_weight(weight[0], ptr_y , wrap_y);
  2133. if (!skip_dct[1])
  2134. get_visual_weight(weight[1], ptr_y + 8, wrap_y);
  2135. if (!skip_dct[2])
  2136. get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  2137. if (!skip_dct[3])
  2138. get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  2139. if (!skip_dct[4])
  2140. get_visual_weight(weight[4], ptr_cb , wrap_c);
  2141. if (!skip_dct[5])
  2142. get_visual_weight(weight[5], ptr_cr , wrap_c);
  2143. if (!s->chroma_y_shift) { /* 422 */
  2144. if (!skip_dct[6])
  2145. get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
  2146. wrap_c);
  2147. if (!skip_dct[7])
  2148. get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
  2149. wrap_c);
  2150. }
  2151. memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
  2152. }
  2153. /* DCT & quantize */
  2154. av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
  2155. {
  2156. for (i = 0; i < mb_block_count; i++) {
  2157. if (!skip_dct[i]) {
  2158. int overflow;
  2159. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2160. // FIXME we could decide to change to quantizer instead of
  2161. // clipping
  2162. // JS: I don't think that would be a good idea it could lower
  2163. // quality instead of improve it. Just INTRADC clipping
  2164. // deserves changes in quantizer
  2165. if (overflow)
  2166. clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2167. } else
  2168. s->block_last_index[i] = -1;
  2169. }
  2170. if (s->quantizer_noise_shaping) {
  2171. for (i = 0; i < mb_block_count; i++) {
  2172. if (!skip_dct[i]) {
  2173. s->block_last_index[i] =
  2174. dct_quantize_refine(s, s->block[i], weight[i],
  2175. orig[i], i, s->qscale);
  2176. }
  2177. }
  2178. }
  2179. if (s->luma_elim_threshold && !s->mb_intra)
  2180. for (i = 0; i < 4; i++)
  2181. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2182. if (s->chroma_elim_threshold && !s->mb_intra)
  2183. for (i = 4; i < mb_block_count; i++)
  2184. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2185. if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
  2186. for (i = 0; i < mb_block_count; i++) {
  2187. if (s->block_last_index[i] == -1)
  2188. s->coded_score[i] = INT_MAX / 256;
  2189. }
  2190. }
  2191. }
  2192. if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
  2193. s->block_last_index[4] =
  2194. s->block_last_index[5] = 0;
  2195. s->block[4][0] =
  2196. s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
  2197. if (!s->chroma_y_shift) { /* 422 / 444 */
  2198. for (i=6; i<12; i++) {
  2199. s->block_last_index[i] = 0;
  2200. s->block[i][0] = s->block[4][0];
  2201. }
  2202. }
  2203. }
  2204. // non c quantize code returns incorrect block_last_index FIXME
  2205. if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
  2206. for (i = 0; i < mb_block_count; i++) {
  2207. int j;
  2208. if (s->block_last_index[i] > 0) {
  2209. for (j = 63; j > 0; j--) {
  2210. if (s->block[i][s->intra_scantable.permutated[j]])
  2211. break;
  2212. }
  2213. s->block_last_index[i] = j;
  2214. }
  2215. }
  2216. }
  2217. /* huffman encode */
  2218. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2219. case AV_CODEC_ID_MPEG1VIDEO:
  2220. case AV_CODEC_ID_MPEG2VIDEO:
  2221. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  2222. ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  2223. break;
  2224. case AV_CODEC_ID_MPEG4:
  2225. if (CONFIG_MPEG4_ENCODER)
  2226. ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2227. break;
  2228. case AV_CODEC_ID_MSMPEG4V2:
  2229. case AV_CODEC_ID_MSMPEG4V3:
  2230. case AV_CODEC_ID_WMV1:
  2231. if (CONFIG_MSMPEG4_ENCODER)
  2232. ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2233. break;
  2234. case AV_CODEC_ID_WMV2:
  2235. if (CONFIG_WMV2_ENCODER)
  2236. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
  2237. break;
  2238. case AV_CODEC_ID_H261:
  2239. if (CONFIG_H261_ENCODER)
  2240. ff_h261_encode_mb(s, s->block, motion_x, motion_y);
  2241. break;
  2242. case AV_CODEC_ID_H263:
  2243. case AV_CODEC_ID_H263P:
  2244. case AV_CODEC_ID_FLV1:
  2245. case AV_CODEC_ID_RV10:
  2246. case AV_CODEC_ID_RV20:
  2247. if (CONFIG_H263_ENCODER)
  2248. ff_h263_encode_mb(s, s->block, motion_x, motion_y);
  2249. break;
  2250. case AV_CODEC_ID_MJPEG:
  2251. case AV_CODEC_ID_AMV:
  2252. if (CONFIG_MJPEG_ENCODER)
  2253. ff_mjpeg_encode_mb(s, s->block);
  2254. break;
  2255. default:
  2256. av_assert1(0);
  2257. }
  2258. }
  2259. static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2260. {
  2261. if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 8, 6);
  2262. else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
  2263. else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
  2264. }
  2265. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2266. int i;
  2267. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2268. /* MPEG-1 */
  2269. d->mb_skip_run= s->mb_skip_run;
  2270. for(i=0; i<3; i++)
  2271. d->last_dc[i] = s->last_dc[i];
  2272. /* statistics */
  2273. d->mv_bits= s->mv_bits;
  2274. d->i_tex_bits= s->i_tex_bits;
  2275. d->p_tex_bits= s->p_tex_bits;
  2276. d->i_count= s->i_count;
  2277. d->f_count= s->f_count;
  2278. d->b_count= s->b_count;
  2279. d->skip_count= s->skip_count;
  2280. d->misc_bits= s->misc_bits;
  2281. d->last_bits= 0;
  2282. d->mb_skipped= 0;
  2283. d->qscale= s->qscale;
  2284. d->dquant= s->dquant;
  2285. d->esc3_level_length= s->esc3_level_length;
  2286. }
  2287. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2288. int i;
  2289. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2290. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2291. /* MPEG-1 */
  2292. d->mb_skip_run= s->mb_skip_run;
  2293. for(i=0; i<3; i++)
  2294. d->last_dc[i] = s->last_dc[i];
  2295. /* statistics */
  2296. d->mv_bits= s->mv_bits;
  2297. d->i_tex_bits= s->i_tex_bits;
  2298. d->p_tex_bits= s->p_tex_bits;
  2299. d->i_count= s->i_count;
  2300. d->f_count= s->f_count;
  2301. d->b_count= s->b_count;
  2302. d->skip_count= s->skip_count;
  2303. d->misc_bits= s->misc_bits;
  2304. d->mb_intra= s->mb_intra;
  2305. d->mb_skipped= s->mb_skipped;
  2306. d->mv_type= s->mv_type;
  2307. d->mv_dir= s->mv_dir;
  2308. d->pb= s->pb;
  2309. if(s->data_partitioning){
  2310. d->pb2= s->pb2;
  2311. d->tex_pb= s->tex_pb;
  2312. }
  2313. d->block= s->block;
  2314. for(i=0; i<8; i++)
  2315. d->block_last_index[i]= s->block_last_index[i];
  2316. d->interlaced_dct= s->interlaced_dct;
  2317. d->qscale= s->qscale;
  2318. d->esc3_level_length= s->esc3_level_length;
  2319. }
  2320. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2321. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2322. int *dmin, int *next_block, int motion_x, int motion_y)
  2323. {
  2324. int score;
  2325. uint8_t *dest_backup[3];
  2326. copy_context_before_encode(s, backup, type);
  2327. s->block= s->blocks[*next_block];
  2328. s->pb= pb[*next_block];
  2329. if(s->data_partitioning){
  2330. s->pb2 = pb2 [*next_block];
  2331. s->tex_pb= tex_pb[*next_block];
  2332. }
  2333. if(*next_block){
  2334. memcpy(dest_backup, s->dest, sizeof(s->dest));
  2335. s->dest[0] = s->sc.rd_scratchpad;
  2336. s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
  2337. s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
  2338. av_assert0(s->linesize >= 32); //FIXME
  2339. }
  2340. encode_mb(s, motion_x, motion_y);
  2341. score= put_bits_count(&s->pb);
  2342. if(s->data_partitioning){
  2343. score+= put_bits_count(&s->pb2);
  2344. score+= put_bits_count(&s->tex_pb);
  2345. }
  2346. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  2347. ff_mpv_reconstruct_mb(s, s->block);
  2348. score *= s->lambda2;
  2349. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  2350. }
  2351. if(*next_block){
  2352. memcpy(s->dest, dest_backup, sizeof(s->dest));
  2353. }
  2354. if(score<*dmin){
  2355. *dmin= score;
  2356. *next_block^=1;
  2357. copy_context_after_encode(best, s, type);
  2358. }
  2359. }
  2360. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2361. const uint32_t *sq = ff_square_tab + 256;
  2362. int acc=0;
  2363. int x,y;
  2364. if(w==16 && h==16)
  2365. return s->mecc.sse[0](NULL, src1, src2, stride, 16);
  2366. else if(w==8 && h==8)
  2367. return s->mecc.sse[1](NULL, src1, src2, stride, 8);
  2368. for(y=0; y<h; y++){
  2369. for(x=0; x<w; x++){
  2370. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2371. }
  2372. }
  2373. av_assert2(acc>=0);
  2374. return acc;
  2375. }
  2376. static int sse_mb(MpegEncContext *s){
  2377. int w= 16;
  2378. int h= 16;
  2379. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2380. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2381. if(w==16 && h==16)
  2382. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  2383. return s->mecc.nsse[0](s, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
  2384. s->mecc.nsse[1](s, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
  2385. s->mecc.nsse[1](s, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
  2386. }else{
  2387. return s->mecc.sse[0](NULL, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
  2388. s->mecc.sse[1](NULL, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
  2389. s->mecc.sse[1](NULL, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
  2390. }
  2391. else
  2392. return sse(s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  2393. +sse(s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  2394. +sse(s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  2395. }
  2396. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  2397. MpegEncContext *s= *(void**)arg;
  2398. s->me.pre_pass=1;
  2399. s->me.dia_size= s->avctx->pre_dia_size;
  2400. s->first_slice_line=1;
  2401. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  2402. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  2403. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2404. }
  2405. s->first_slice_line=0;
  2406. }
  2407. s->me.pre_pass=0;
  2408. return 0;
  2409. }
  2410. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  2411. MpegEncContext *s= *(void**)arg;
  2412. ff_check_alignment();
  2413. s->me.dia_size= s->avctx->dia_size;
  2414. s->first_slice_line=1;
  2415. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  2416. s->mb_x=0; //for block init below
  2417. ff_init_block_index(s);
  2418. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  2419. s->block_index[0]+=2;
  2420. s->block_index[1]+=2;
  2421. s->block_index[2]+=2;
  2422. s->block_index[3]+=2;
  2423. /* compute motion vector & mb_type and store in context */
  2424. if(s->pict_type==AV_PICTURE_TYPE_B)
  2425. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  2426. else
  2427. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2428. }
  2429. s->first_slice_line=0;
  2430. }
  2431. return 0;
  2432. }
  2433. static int mb_var_thread(AVCodecContext *c, void *arg){
  2434. MpegEncContext *s= *(void**)arg;
  2435. int mb_x, mb_y;
  2436. ff_check_alignment();
  2437. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2438. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2439. int xx = mb_x * 16;
  2440. int yy = mb_y * 16;
  2441. uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
  2442. int varc;
  2443. int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
  2444. varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
  2445. (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
  2446. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  2447. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  2448. s->me.mb_var_sum_temp += varc;
  2449. }
  2450. }
  2451. return 0;
  2452. }
  2453. static void write_slice_end(MpegEncContext *s){
  2454. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
  2455. if(s->partitioned_frame){
  2456. ff_mpeg4_merge_partitions(s);
  2457. }
  2458. ff_mpeg4_stuffing(&s->pb);
  2459. }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
  2460. ff_mjpeg_encode_stuffing(s);
  2461. }
  2462. avpriv_align_put_bits(&s->pb);
  2463. flush_put_bits(&s->pb);
  2464. if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
  2465. s->misc_bits+= get_bits_diff(s);
  2466. }
  2467. static void write_mb_info(MpegEncContext *s)
  2468. {
  2469. uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
  2470. int offset = put_bits_count(&s->pb);
  2471. int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
  2472. int gobn = s->mb_y / s->gob_index;
  2473. int pred_x, pred_y;
  2474. if (CONFIG_H263_ENCODER)
  2475. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  2476. bytestream_put_le32(&ptr, offset);
  2477. bytestream_put_byte(&ptr, s->qscale);
  2478. bytestream_put_byte(&ptr, gobn);
  2479. bytestream_put_le16(&ptr, mba);
  2480. bytestream_put_byte(&ptr, pred_x); /* hmv1 */
  2481. bytestream_put_byte(&ptr, pred_y); /* vmv1 */
  2482. /* 4MV not implemented */
  2483. bytestream_put_byte(&ptr, 0); /* hmv2 */
  2484. bytestream_put_byte(&ptr, 0); /* vmv2 */
  2485. }
  2486. static void update_mb_info(MpegEncContext *s, int startcode)
  2487. {
  2488. if (!s->mb_info)
  2489. return;
  2490. if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
  2491. s->mb_info_size += 12;
  2492. s->prev_mb_info = s->last_mb_info;
  2493. }
  2494. if (startcode) {
  2495. s->prev_mb_info = put_bits_count(&s->pb)/8;
  2496. /* This might have incremented mb_info_size above, and we return without
  2497. * actually writing any info into that slot yet. But in that case,
  2498. * this will be called again at the start of the after writing the
  2499. * start code, actually writing the mb info. */
  2500. return;
  2501. }
  2502. s->last_mb_info = put_bits_count(&s->pb)/8;
  2503. if (!s->mb_info_size)
  2504. s->mb_info_size += 12;
  2505. write_mb_info(s);
  2506. }
  2507. int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
  2508. {
  2509. if ( s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold
  2510. && s->slice_context_count == 1
  2511. && s->pb.buf == s->avctx->internal->byte_buffer) {
  2512. int lastgob_pos = s->ptr_lastgob - s->pb.buf;
  2513. int vbv_pos = s->vbv_delay_ptr - s->pb.buf;
  2514. uint8_t *new_buffer = NULL;
  2515. int new_buffer_size = 0;
  2516. if ((s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
  2517. av_log(s->avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
  2518. return AVERROR(ENOMEM);
  2519. }
  2520. emms_c();
  2521. av_fast_padded_malloc(&new_buffer, &new_buffer_size,
  2522. s->avctx->internal->byte_buffer_size + size_increase);
  2523. if (!new_buffer)
  2524. return AVERROR(ENOMEM);
  2525. memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
  2526. av_free(s->avctx->internal->byte_buffer);
  2527. s->avctx->internal->byte_buffer = new_buffer;
  2528. s->avctx->internal->byte_buffer_size = new_buffer_size;
  2529. rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
  2530. s->ptr_lastgob = s->pb.buf + lastgob_pos;
  2531. s->vbv_delay_ptr = s->pb.buf + vbv_pos;
  2532. }
  2533. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold)
  2534. return AVERROR(EINVAL);
  2535. return 0;
  2536. }
  2537. static int encode_thread(AVCodecContext *c, void *arg){
  2538. MpegEncContext *s= *(void**)arg;
  2539. int mb_x, mb_y;
  2540. int chr_h= 16>>s->chroma_y_shift;
  2541. int i, j;
  2542. MpegEncContext best_s = { 0 }, backup_s;
  2543. uint8_t bit_buf[2][MAX_MB_BYTES];
  2544. uint8_t bit_buf2[2][MAX_MB_BYTES];
  2545. uint8_t bit_buf_tex[2][MAX_MB_BYTES];
  2546. PutBitContext pb[2], pb2[2], tex_pb[2];
  2547. ff_check_alignment();
  2548. for(i=0; i<2; i++){
  2549. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  2550. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
  2551. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
  2552. }
  2553. s->last_bits= put_bits_count(&s->pb);
  2554. s->mv_bits=0;
  2555. s->misc_bits=0;
  2556. s->i_tex_bits=0;
  2557. s->p_tex_bits=0;
  2558. s->i_count=0;
  2559. s->f_count=0;
  2560. s->b_count=0;
  2561. s->skip_count=0;
  2562. for(i=0; i<3; i++){
  2563. /* init last dc values */
  2564. /* note: quant matrix value (8) is implied here */
  2565. s->last_dc[i] = 128 << s->intra_dc_precision;
  2566. s->current_picture.encoding_error[i] = 0;
  2567. }
  2568. if(s->codec_id==AV_CODEC_ID_AMV){
  2569. s->last_dc[0] = 128*8/13;
  2570. s->last_dc[1] = 128*8/14;
  2571. s->last_dc[2] = 128*8/14;
  2572. }
  2573. s->mb_skip_run = 0;
  2574. memset(s->last_mv, 0, sizeof(s->last_mv));
  2575. s->last_mv_dir = 0;
  2576. switch(s->codec_id){
  2577. case AV_CODEC_ID_H263:
  2578. case AV_CODEC_ID_H263P:
  2579. case AV_CODEC_ID_FLV1:
  2580. if (CONFIG_H263_ENCODER)
  2581. s->gob_index = H263_GOB_HEIGHT(s->height);
  2582. break;
  2583. case AV_CODEC_ID_MPEG4:
  2584. if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
  2585. ff_mpeg4_init_partitions(s);
  2586. break;
  2587. }
  2588. s->resync_mb_x=0;
  2589. s->resync_mb_y=0;
  2590. s->first_slice_line = 1;
  2591. s->ptr_lastgob = s->pb.buf;
  2592. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2593. s->mb_x=0;
  2594. s->mb_y= mb_y;
  2595. ff_set_qscale(s, s->qscale);
  2596. ff_init_block_index(s);
  2597. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2598. int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
  2599. int mb_type= s->mb_type[xy];
  2600. // int d;
  2601. int dmin= INT_MAX;
  2602. int dir;
  2603. int size_increase = s->avctx->internal->byte_buffer_size/4
  2604. + s->mb_width*MAX_MB_BYTES;
  2605. ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
  2606. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
  2607. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2608. return -1;
  2609. }
  2610. if(s->data_partitioning){
  2611. if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
  2612. || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
  2613. av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
  2614. return -1;
  2615. }
  2616. }
  2617. s->mb_x = mb_x;
  2618. s->mb_y = mb_y; // moved into loop, can get changed by H.261
  2619. ff_update_block_index(s);
  2620. if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
  2621. ff_h261_reorder_mb_index(s);
  2622. xy= s->mb_y*s->mb_stride + s->mb_x;
  2623. mb_type= s->mb_type[xy];
  2624. }
  2625. /* write gob / video packet header */
  2626. if(s->rtp_mode){
  2627. int current_packet_size, is_gob_start;
  2628. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  2629. is_gob_start = s->rtp_payload_size &&
  2630. current_packet_size >= s->rtp_payload_size &&
  2631. mb_y + mb_x > 0;
  2632. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  2633. switch(s->codec_id){
  2634. case AV_CODEC_ID_H263:
  2635. case AV_CODEC_ID_H263P:
  2636. if(!s->h263_slice_structured)
  2637. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  2638. break;
  2639. case AV_CODEC_ID_MPEG2VIDEO:
  2640. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  2641. case AV_CODEC_ID_MPEG1VIDEO:
  2642. if(s->mb_skip_run) is_gob_start=0;
  2643. break;
  2644. case AV_CODEC_ID_MJPEG:
  2645. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  2646. break;
  2647. }
  2648. if(is_gob_start){
  2649. if(s->start_mb_y != mb_y || mb_x!=0){
  2650. write_slice_end(s);
  2651. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
  2652. ff_mpeg4_init_partitions(s);
  2653. }
  2654. }
  2655. av_assert2((put_bits_count(&s->pb)&7) == 0);
  2656. current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2657. if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
  2658. int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
  2659. int d = 100 / s->error_rate;
  2660. if(r % d == 0){
  2661. current_packet_size=0;
  2662. s->pb.buf_ptr= s->ptr_lastgob;
  2663. av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
  2664. }
  2665. }
  2666. #if FF_API_RTP_CALLBACK
  2667. FF_DISABLE_DEPRECATION_WARNINGS
  2668. if (s->avctx->rtp_callback){
  2669. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
  2670. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
  2671. }
  2672. FF_ENABLE_DEPRECATION_WARNINGS
  2673. #endif
  2674. update_mb_info(s, 1);
  2675. switch(s->codec_id){
  2676. case AV_CODEC_ID_MPEG4:
  2677. if (CONFIG_MPEG4_ENCODER) {
  2678. ff_mpeg4_encode_video_packet_header(s);
  2679. ff_mpeg4_clean_buffers(s);
  2680. }
  2681. break;
  2682. case AV_CODEC_ID_MPEG1VIDEO:
  2683. case AV_CODEC_ID_MPEG2VIDEO:
  2684. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
  2685. ff_mpeg1_encode_slice_header(s);
  2686. ff_mpeg1_clean_buffers(s);
  2687. }
  2688. break;
  2689. case AV_CODEC_ID_H263:
  2690. case AV_CODEC_ID_H263P:
  2691. if (CONFIG_H263_ENCODER)
  2692. ff_h263_encode_gob_header(s, mb_y);
  2693. break;
  2694. }
  2695. if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
  2696. int bits= put_bits_count(&s->pb);
  2697. s->misc_bits+= bits - s->last_bits;
  2698. s->last_bits= bits;
  2699. }
  2700. s->ptr_lastgob += current_packet_size;
  2701. s->first_slice_line=1;
  2702. s->resync_mb_x=mb_x;
  2703. s->resync_mb_y=mb_y;
  2704. }
  2705. }
  2706. if( (s->resync_mb_x == s->mb_x)
  2707. && s->resync_mb_y+1 == s->mb_y){
  2708. s->first_slice_line=0;
  2709. }
  2710. s->mb_skipped=0;
  2711. s->dquant=0; //only for QP_RD
  2712. update_mb_info(s, 0);
  2713. if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
  2714. int next_block=0;
  2715. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2716. copy_context_before_encode(&backup_s, s, -1);
  2717. backup_s.pb= s->pb;
  2718. best_s.data_partitioning= s->data_partitioning;
  2719. best_s.partitioned_frame= s->partitioned_frame;
  2720. if(s->data_partitioning){
  2721. backup_s.pb2= s->pb2;
  2722. backup_s.tex_pb= s->tex_pb;
  2723. }
  2724. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  2725. s->mv_dir = MV_DIR_FORWARD;
  2726. s->mv_type = MV_TYPE_16X16;
  2727. s->mb_intra= 0;
  2728. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2729. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2730. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  2731. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2732. }
  2733. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  2734. s->mv_dir = MV_DIR_FORWARD;
  2735. s->mv_type = MV_TYPE_FIELD;
  2736. s->mb_intra= 0;
  2737. for(i=0; i<2; i++){
  2738. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2739. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2740. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2741. }
  2742. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  2743. &dmin, &next_block, 0, 0);
  2744. }
  2745. if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
  2746. s->mv_dir = MV_DIR_FORWARD;
  2747. s->mv_type = MV_TYPE_16X16;
  2748. s->mb_intra= 0;
  2749. s->mv[0][0][0] = 0;
  2750. s->mv[0][0][1] = 0;
  2751. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
  2752. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2753. }
  2754. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  2755. s->mv_dir = MV_DIR_FORWARD;
  2756. s->mv_type = MV_TYPE_8X8;
  2757. s->mb_intra= 0;
  2758. for(i=0; i<4; i++){
  2759. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2760. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2761. }
  2762. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2763. &dmin, &next_block, 0, 0);
  2764. }
  2765. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  2766. s->mv_dir = MV_DIR_FORWARD;
  2767. s->mv_type = MV_TYPE_16X16;
  2768. s->mb_intra= 0;
  2769. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2770. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2771. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2772. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2773. }
  2774. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  2775. s->mv_dir = MV_DIR_BACKWARD;
  2776. s->mv_type = MV_TYPE_16X16;
  2777. s->mb_intra= 0;
  2778. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2779. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2780. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2781. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2782. }
  2783. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  2784. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2785. s->mv_type = MV_TYPE_16X16;
  2786. s->mb_intra= 0;
  2787. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2788. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2789. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2790. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2791. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2792. &dmin, &next_block, 0, 0);
  2793. }
  2794. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  2795. s->mv_dir = MV_DIR_FORWARD;
  2796. s->mv_type = MV_TYPE_FIELD;
  2797. s->mb_intra= 0;
  2798. for(i=0; i<2; i++){
  2799. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2800. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2801. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2802. }
  2803. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  2804. &dmin, &next_block, 0, 0);
  2805. }
  2806. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  2807. s->mv_dir = MV_DIR_BACKWARD;
  2808. s->mv_type = MV_TYPE_FIELD;
  2809. s->mb_intra= 0;
  2810. for(i=0; i<2; i++){
  2811. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2812. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2813. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2814. }
  2815. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  2816. &dmin, &next_block, 0, 0);
  2817. }
  2818. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  2819. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2820. s->mv_type = MV_TYPE_FIELD;
  2821. s->mb_intra= 0;
  2822. for(dir=0; dir<2; dir++){
  2823. for(i=0; i<2; i++){
  2824. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2825. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2826. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2827. }
  2828. }
  2829. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  2830. &dmin, &next_block, 0, 0);
  2831. }
  2832. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  2833. s->mv_dir = 0;
  2834. s->mv_type = MV_TYPE_16X16;
  2835. s->mb_intra= 1;
  2836. s->mv[0][0][0] = 0;
  2837. s->mv[0][0][1] = 0;
  2838. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  2839. &dmin, &next_block, 0, 0);
  2840. if(s->h263_pred || s->h263_aic){
  2841. if(best_s.mb_intra)
  2842. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  2843. else
  2844. ff_clean_intra_table_entries(s); //old mode?
  2845. }
  2846. }
  2847. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
  2848. if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
  2849. const int last_qp= backup_s.qscale;
  2850. int qpi, qp, dc[6];
  2851. int16_t ac[6][16];
  2852. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  2853. static const int dquant_tab[4]={-1,1,-2,2};
  2854. int storecoefs = s->mb_intra && s->dc_val[0];
  2855. av_assert2(backup_s.dquant == 0);
  2856. //FIXME intra
  2857. s->mv_dir= best_s.mv_dir;
  2858. s->mv_type = MV_TYPE_16X16;
  2859. s->mb_intra= best_s.mb_intra;
  2860. s->mv[0][0][0] = best_s.mv[0][0][0];
  2861. s->mv[0][0][1] = best_s.mv[0][0][1];
  2862. s->mv[1][0][0] = best_s.mv[1][0][0];
  2863. s->mv[1][0][1] = best_s.mv[1][0][1];
  2864. qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
  2865. for(; qpi<4; qpi++){
  2866. int dquant= dquant_tab[qpi];
  2867. qp= last_qp + dquant;
  2868. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  2869. continue;
  2870. backup_s.dquant= dquant;
  2871. if(storecoefs){
  2872. for(i=0; i<6; i++){
  2873. dc[i]= s->dc_val[0][ s->block_index[i] ];
  2874. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
  2875. }
  2876. }
  2877. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2878. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  2879. if(best_s.qscale != qp){
  2880. if(storecoefs){
  2881. for(i=0; i<6; i++){
  2882. s->dc_val[0][ s->block_index[i] ]= dc[i];
  2883. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
  2884. }
  2885. }
  2886. }
  2887. }
  2888. }
  2889. }
  2890. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
  2891. int mx= s->b_direct_mv_table[xy][0];
  2892. int my= s->b_direct_mv_table[xy][1];
  2893. backup_s.dquant = 0;
  2894. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2895. s->mb_intra= 0;
  2896. ff_mpeg4_set_direct_mv(s, mx, my);
  2897. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2898. &dmin, &next_block, mx, my);
  2899. }
  2900. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
  2901. backup_s.dquant = 0;
  2902. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2903. s->mb_intra= 0;
  2904. ff_mpeg4_set_direct_mv(s, 0, 0);
  2905. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2906. &dmin, &next_block, 0, 0);
  2907. }
  2908. if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
  2909. int coded=0;
  2910. for(i=0; i<6; i++)
  2911. coded |= s->block_last_index[i];
  2912. if(coded){
  2913. int mx,my;
  2914. memcpy(s->mv, best_s.mv, sizeof(s->mv));
  2915. if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
  2916. mx=my=0; //FIXME find the one we actually used
  2917. ff_mpeg4_set_direct_mv(s, mx, my);
  2918. }else if(best_s.mv_dir&MV_DIR_BACKWARD){
  2919. mx= s->mv[1][0][0];
  2920. my= s->mv[1][0][1];
  2921. }else{
  2922. mx= s->mv[0][0][0];
  2923. my= s->mv[0][0][1];
  2924. }
  2925. s->mv_dir= best_s.mv_dir;
  2926. s->mv_type = best_s.mv_type;
  2927. s->mb_intra= 0;
  2928. /* s->mv[0][0][0] = best_s.mv[0][0][0];
  2929. s->mv[0][0][1] = best_s.mv[0][0][1];
  2930. s->mv[1][0][0] = best_s.mv[1][0][0];
  2931. s->mv[1][0][1] = best_s.mv[1][0][1];*/
  2932. backup_s.dquant= 0;
  2933. s->skipdct=1;
  2934. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2935. &dmin, &next_block, mx, my);
  2936. s->skipdct=0;
  2937. }
  2938. }
  2939. s->current_picture.qscale_table[xy] = best_s.qscale;
  2940. copy_context_after_encode(s, &best_s, -1);
  2941. pb_bits_count= put_bits_count(&s->pb);
  2942. flush_put_bits(&s->pb);
  2943. avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2944. s->pb= backup_s.pb;
  2945. if(s->data_partitioning){
  2946. pb2_bits_count= put_bits_count(&s->pb2);
  2947. flush_put_bits(&s->pb2);
  2948. avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2949. s->pb2= backup_s.pb2;
  2950. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  2951. flush_put_bits(&s->tex_pb);
  2952. avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2953. s->tex_pb= backup_s.tex_pb;
  2954. }
  2955. s->last_bits= put_bits_count(&s->pb);
  2956. if (CONFIG_H263_ENCODER &&
  2957. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  2958. ff_h263_update_motion_val(s);
  2959. if(next_block==0){ //FIXME 16 vs linesize16
  2960. s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
  2961. s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  2962. s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  2963. }
  2964. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  2965. ff_mpv_reconstruct_mb(s, s->block);
  2966. } else {
  2967. int motion_x = 0, motion_y = 0;
  2968. s->mv_type=MV_TYPE_16X16;
  2969. // only one MB-Type possible
  2970. switch(mb_type){
  2971. case CANDIDATE_MB_TYPE_INTRA:
  2972. s->mv_dir = 0;
  2973. s->mb_intra= 1;
  2974. motion_x= s->mv[0][0][0] = 0;
  2975. motion_y= s->mv[0][0][1] = 0;
  2976. break;
  2977. case CANDIDATE_MB_TYPE_INTER:
  2978. s->mv_dir = MV_DIR_FORWARD;
  2979. s->mb_intra= 0;
  2980. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2981. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2982. break;
  2983. case CANDIDATE_MB_TYPE_INTER_I:
  2984. s->mv_dir = MV_DIR_FORWARD;
  2985. s->mv_type = MV_TYPE_FIELD;
  2986. s->mb_intra= 0;
  2987. for(i=0; i<2; i++){
  2988. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2989. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2990. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2991. }
  2992. break;
  2993. case CANDIDATE_MB_TYPE_INTER4V:
  2994. s->mv_dir = MV_DIR_FORWARD;
  2995. s->mv_type = MV_TYPE_8X8;
  2996. s->mb_intra= 0;
  2997. for(i=0; i<4; i++){
  2998. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2999. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3000. }
  3001. break;
  3002. case CANDIDATE_MB_TYPE_DIRECT:
  3003. if (CONFIG_MPEG4_ENCODER) {
  3004. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  3005. s->mb_intra= 0;
  3006. motion_x=s->b_direct_mv_table[xy][0];
  3007. motion_y=s->b_direct_mv_table[xy][1];
  3008. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3009. }
  3010. break;
  3011. case CANDIDATE_MB_TYPE_DIRECT0:
  3012. if (CONFIG_MPEG4_ENCODER) {
  3013. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  3014. s->mb_intra= 0;
  3015. ff_mpeg4_set_direct_mv(s, 0, 0);
  3016. }
  3017. break;
  3018. case CANDIDATE_MB_TYPE_BIDIR:
  3019. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3020. s->mb_intra= 0;
  3021. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3022. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3023. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3024. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3025. break;
  3026. case CANDIDATE_MB_TYPE_BACKWARD:
  3027. s->mv_dir = MV_DIR_BACKWARD;
  3028. s->mb_intra= 0;
  3029. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3030. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3031. break;
  3032. case CANDIDATE_MB_TYPE_FORWARD:
  3033. s->mv_dir = MV_DIR_FORWARD;
  3034. s->mb_intra= 0;
  3035. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3036. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3037. break;
  3038. case CANDIDATE_MB_TYPE_FORWARD_I:
  3039. s->mv_dir = MV_DIR_FORWARD;
  3040. s->mv_type = MV_TYPE_FIELD;
  3041. s->mb_intra= 0;
  3042. for(i=0; i<2; i++){
  3043. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  3044. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3045. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3046. }
  3047. break;
  3048. case CANDIDATE_MB_TYPE_BACKWARD_I:
  3049. s->mv_dir = MV_DIR_BACKWARD;
  3050. s->mv_type = MV_TYPE_FIELD;
  3051. s->mb_intra= 0;
  3052. for(i=0; i<2; i++){
  3053. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  3054. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3055. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3056. }
  3057. break;
  3058. case CANDIDATE_MB_TYPE_BIDIR_I:
  3059. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3060. s->mv_type = MV_TYPE_FIELD;
  3061. s->mb_intra= 0;
  3062. for(dir=0; dir<2; dir++){
  3063. for(i=0; i<2; i++){
  3064. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3065. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3066. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  3067. }
  3068. }
  3069. break;
  3070. default:
  3071. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  3072. }
  3073. encode_mb(s, motion_x, motion_y);
  3074. // RAL: Update last macroblock type
  3075. s->last_mv_dir = s->mv_dir;
  3076. if (CONFIG_H263_ENCODER &&
  3077. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  3078. ff_h263_update_motion_val(s);
  3079. ff_mpv_reconstruct_mb(s, s->block);
  3080. }
  3081. /* clean the MV table in IPS frames for direct mode in B-frames */
  3082. if(s->mb_intra /* && I,P,S_TYPE */){
  3083. s->p_mv_table[xy][0]=0;
  3084. s->p_mv_table[xy][1]=0;
  3085. }
  3086. if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
  3087. int w= 16;
  3088. int h= 16;
  3089. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3090. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3091. s->current_picture.encoding_error[0] += sse(
  3092. s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  3093. s->dest[0], w, h, s->linesize);
  3094. s->current_picture.encoding_error[1] += sse(
  3095. s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  3096. s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  3097. s->current_picture.encoding_error[2] += sse(
  3098. s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  3099. s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  3100. }
  3101. if(s->loop_filter){
  3102. if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
  3103. ff_h263_loop_filter(s);
  3104. }
  3105. ff_dlog(s->avctx, "MB %d %d bits\n",
  3106. s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
  3107. }
  3108. }
  3109. //not beautiful here but we must write it before flushing so it has to be here
  3110. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
  3111. ff_msmpeg4_encode_ext_header(s);
  3112. write_slice_end(s);
  3113. #if FF_API_RTP_CALLBACK
  3114. FF_DISABLE_DEPRECATION_WARNINGS
  3115. /* Send the last GOB if RTP */
  3116. if (s->avctx->rtp_callback) {
  3117. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
  3118. int pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
  3119. /* Call the RTP callback to send the last GOB */
  3120. emms_c();
  3121. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
  3122. }
  3123. FF_ENABLE_DEPRECATION_WARNINGS
  3124. #endif
  3125. return 0;
  3126. }
  3127. #define MERGE(field) dst->field += src->field; src->field=0
  3128. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  3129. MERGE(me.scene_change_score);
  3130. MERGE(me.mc_mb_var_sum_temp);
  3131. MERGE(me.mb_var_sum_temp);
  3132. }
  3133. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  3134. int i;
  3135. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  3136. MERGE(dct_count[1]);
  3137. MERGE(mv_bits);
  3138. MERGE(i_tex_bits);
  3139. MERGE(p_tex_bits);
  3140. MERGE(i_count);
  3141. MERGE(f_count);
  3142. MERGE(b_count);
  3143. MERGE(skip_count);
  3144. MERGE(misc_bits);
  3145. MERGE(er.error_count);
  3146. MERGE(padding_bug_score);
  3147. MERGE(current_picture.encoding_error[0]);
  3148. MERGE(current_picture.encoding_error[1]);
  3149. MERGE(current_picture.encoding_error[2]);
  3150. if (dst->noise_reduction){
  3151. for(i=0; i<64; i++){
  3152. MERGE(dct_error_sum[0][i]);
  3153. MERGE(dct_error_sum[1][i]);
  3154. }
  3155. }
  3156. av_assert1(put_bits_count(&src->pb) % 8 ==0);
  3157. av_assert1(put_bits_count(&dst->pb) % 8 ==0);
  3158. avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  3159. flush_put_bits(&dst->pb);
  3160. }
  3161. static int estimate_qp(MpegEncContext *s, int dry_run){
  3162. if (s->next_lambda){
  3163. s->current_picture_ptr->f->quality =
  3164. s->current_picture.f->quality = s->next_lambda;
  3165. if(!dry_run) s->next_lambda= 0;
  3166. } else if (!s->fixed_qscale) {
  3167. int quality = ff_rate_estimate_qscale(s, dry_run);
  3168. s->current_picture_ptr->f->quality =
  3169. s->current_picture.f->quality = quality;
  3170. if (s->current_picture.f->quality < 0)
  3171. return -1;
  3172. }
  3173. if(s->adaptive_quant){
  3174. switch(s->codec_id){
  3175. case AV_CODEC_ID_MPEG4:
  3176. if (CONFIG_MPEG4_ENCODER)
  3177. ff_clean_mpeg4_qscales(s);
  3178. break;
  3179. case AV_CODEC_ID_H263:
  3180. case AV_CODEC_ID_H263P:
  3181. case AV_CODEC_ID_FLV1:
  3182. if (CONFIG_H263_ENCODER)
  3183. ff_clean_h263_qscales(s);
  3184. break;
  3185. default:
  3186. ff_init_qscale_tab(s);
  3187. }
  3188. s->lambda= s->lambda_table[0];
  3189. //FIXME broken
  3190. }else
  3191. s->lambda = s->current_picture.f->quality;
  3192. update_qscale(s);
  3193. return 0;
  3194. }
  3195. /* must be called before writing the header */
  3196. static void set_frame_distances(MpegEncContext * s){
  3197. av_assert1(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
  3198. s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
  3199. if(s->pict_type==AV_PICTURE_TYPE_B){
  3200. s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
  3201. av_assert1(s->pb_time > 0 && s->pb_time < s->pp_time);
  3202. }else{
  3203. s->pp_time= s->time - s->last_non_b_time;
  3204. s->last_non_b_time= s->time;
  3205. av_assert1(s->picture_number==0 || s->pp_time > 0);
  3206. }
  3207. }
  3208. static int encode_picture(MpegEncContext *s, int picture_number)
  3209. {
  3210. int i, ret;
  3211. int bits;
  3212. int context_count = s->slice_context_count;
  3213. s->picture_number = picture_number;
  3214. /* Reset the average MB variance */
  3215. s->me.mb_var_sum_temp =
  3216. s->me.mc_mb_var_sum_temp = 0;
  3217. /* we need to initialize some time vars before we can encode B-frames */
  3218. // RAL: Condition added for MPEG1VIDEO
  3219. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
  3220. set_frame_distances(s);
  3221. if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
  3222. ff_set_mpeg4_time(s);
  3223. s->me.scene_change_score=0;
  3224. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
  3225. if(s->pict_type==AV_PICTURE_TYPE_I){
  3226. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  3227. else s->no_rounding=0;
  3228. }else if(s->pict_type!=AV_PICTURE_TYPE_B){
  3229. if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
  3230. s->no_rounding ^= 1;
  3231. }
  3232. if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
  3233. if (estimate_qp(s,1) < 0)
  3234. return -1;
  3235. ff_get_2pass_fcode(s);
  3236. } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  3237. if(s->pict_type==AV_PICTURE_TYPE_B)
  3238. s->lambda= s->last_lambda_for[s->pict_type];
  3239. else
  3240. s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
  3241. update_qscale(s);
  3242. }
  3243. if(s->codec_id != AV_CODEC_ID_AMV && s->codec_id != AV_CODEC_ID_MJPEG){
  3244. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  3245. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  3246. s->q_chroma_intra_matrix = s->q_intra_matrix;
  3247. s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
  3248. }
  3249. s->mb_intra=0; //for the rate distortion & bit compare functions
  3250. for(i=1; i<context_count; i++){
  3251. ret = ff_update_duplicate_context(s->thread_context[i], s);
  3252. if (ret < 0)
  3253. return ret;
  3254. }
  3255. if(ff_init_me(s)<0)
  3256. return -1;
  3257. /* Estimate motion for every MB */
  3258. if(s->pict_type != AV_PICTURE_TYPE_I){
  3259. s->lambda = (s->lambda * s->me_penalty_compensation + 128) >> 8;
  3260. s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
  3261. if (s->pict_type != AV_PICTURE_TYPE_B) {
  3262. if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
  3263. s->me_pre == 2) {
  3264. s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3265. }
  3266. }
  3267. s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3268. }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
  3269. /* I-Frame */
  3270. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3271. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3272. if(!s->fixed_qscale){
  3273. /* finding spatial complexity for I-frame rate control */
  3274. s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3275. }
  3276. }
  3277. for(i=1; i<context_count; i++){
  3278. merge_context_after_me(s, s->thread_context[i]);
  3279. }
  3280. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  3281. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  3282. emms_c();
  3283. if (s->me.scene_change_score > s->scenechange_threshold &&
  3284. s->pict_type == AV_PICTURE_TYPE_P) {
  3285. s->pict_type= AV_PICTURE_TYPE_I;
  3286. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3287. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3288. if(s->msmpeg4_version >= 3)
  3289. s->no_rounding=1;
  3290. ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
  3291. s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  3292. }
  3293. if(!s->umvplus){
  3294. if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
  3295. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  3296. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3297. int a,b;
  3298. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  3299. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  3300. s->f_code= FFMAX3(s->f_code, a, b);
  3301. }
  3302. ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
  3303. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
  3304. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3305. int j;
  3306. for(i=0; i<2; i++){
  3307. for(j=0; j<2; j++)
  3308. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  3309. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
  3310. }
  3311. }
  3312. }
  3313. if(s->pict_type==AV_PICTURE_TYPE_B){
  3314. int a, b;
  3315. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  3316. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3317. s->f_code = FFMAX(a, b);
  3318. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  3319. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3320. s->b_code = FFMAX(a, b);
  3321. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  3322. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  3323. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3324. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3325. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3326. int dir, j;
  3327. for(dir=0; dir<2; dir++){
  3328. for(i=0; i<2; i++){
  3329. for(j=0; j<2; j++){
  3330. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  3331. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  3332. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  3333. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  3334. }
  3335. }
  3336. }
  3337. }
  3338. }
  3339. }
  3340. if (estimate_qp(s, 0) < 0)
  3341. return -1;
  3342. if (s->qscale < 3 && s->max_qcoeff <= 128 &&
  3343. s->pict_type == AV_PICTURE_TYPE_I &&
  3344. !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
  3345. s->qscale= 3; //reduce clipping problems
  3346. if (s->out_format == FMT_MJPEG) {
  3347. const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
  3348. const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
  3349. if (s->avctx->intra_matrix) {
  3350. chroma_matrix =
  3351. luma_matrix = s->avctx->intra_matrix;
  3352. }
  3353. if (s->avctx->chroma_intra_matrix)
  3354. chroma_matrix = s->avctx->chroma_intra_matrix;
  3355. /* for mjpeg, we do include qscale in the matrix */
  3356. for(i=1;i<64;i++){
  3357. int j = s->idsp.idct_permutation[i];
  3358. s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->qscale) >> 3);
  3359. s-> intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->qscale) >> 3);
  3360. }
  3361. s->y_dc_scale_table=
  3362. s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  3363. s->chroma_intra_matrix[0] =
  3364. s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
  3365. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  3366. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3367. ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
  3368. s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3369. s->qscale= 8;
  3370. }
  3371. if(s->codec_id == AV_CODEC_ID_AMV){
  3372. static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
  3373. static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
  3374. for(i=1;i<64;i++){
  3375. int j= s->idsp.idct_permutation[ff_zigzag_direct[i]];
  3376. s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
  3377. s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
  3378. }
  3379. s->y_dc_scale_table= y;
  3380. s->c_dc_scale_table= c;
  3381. s->intra_matrix[0] = 13;
  3382. s->chroma_intra_matrix[0] = 14;
  3383. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  3384. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3385. ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
  3386. s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3387. s->qscale= 8;
  3388. }
  3389. //FIXME var duplication
  3390. s->current_picture_ptr->f->key_frame =
  3391. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
  3392. s->current_picture_ptr->f->pict_type =
  3393. s->current_picture.f->pict_type = s->pict_type;
  3394. if (s->current_picture.f->key_frame)
  3395. s->picture_in_gop_number=0;
  3396. s->mb_x = s->mb_y = 0;
  3397. s->last_bits= put_bits_count(&s->pb);
  3398. switch(s->out_format) {
  3399. case FMT_MJPEG:
  3400. if (CONFIG_MJPEG_ENCODER && s->huffman != HUFFMAN_TABLE_OPTIMAL)
  3401. ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
  3402. s->pred, s->intra_matrix, s->chroma_intra_matrix);
  3403. break;
  3404. case FMT_H261:
  3405. if (CONFIG_H261_ENCODER)
  3406. ff_h261_encode_picture_header(s, picture_number);
  3407. break;
  3408. case FMT_H263:
  3409. if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
  3410. ff_wmv2_encode_picture_header(s, picture_number);
  3411. else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
  3412. ff_msmpeg4_encode_picture_header(s, picture_number);
  3413. else if (CONFIG_MPEG4_ENCODER && s->h263_pred) {
  3414. ret = ff_mpeg4_encode_picture_header(s, picture_number);
  3415. if (ret < 0)
  3416. return ret;
  3417. } else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
  3418. ret = ff_rv10_encode_picture_header(s, picture_number);
  3419. if (ret < 0)
  3420. return ret;
  3421. }
  3422. else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
  3423. ff_rv20_encode_picture_header(s, picture_number);
  3424. else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
  3425. ff_flv_encode_picture_header(s, picture_number);
  3426. else if (CONFIG_H263_ENCODER)
  3427. ff_h263_encode_picture_header(s, picture_number);
  3428. break;
  3429. case FMT_MPEG1:
  3430. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  3431. ff_mpeg1_encode_picture_header(s, picture_number);
  3432. break;
  3433. default:
  3434. av_assert0(0);
  3435. }
  3436. bits= put_bits_count(&s->pb);
  3437. s->header_bits= bits - s->last_bits;
  3438. for(i=1; i<context_count; i++){
  3439. update_duplicate_context_after_me(s->thread_context[i], s);
  3440. }
  3441. s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3442. for(i=1; i<context_count; i++){
  3443. if (s->pb.buf_end == s->thread_context[i]->pb.buf)
  3444. set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-32));
  3445. merge_context_after_encode(s, s->thread_context[i]);
  3446. }
  3447. emms_c();
  3448. return 0;
  3449. }
  3450. static void denoise_dct_c(MpegEncContext *s, int16_t *block){
  3451. const int intra= s->mb_intra;
  3452. int i;
  3453. s->dct_count[intra]++;
  3454. for(i=0; i<64; i++){
  3455. int level= block[i];
  3456. if(level){
  3457. if(level>0){
  3458. s->dct_error_sum[intra][i] += level;
  3459. level -= s->dct_offset[intra][i];
  3460. if(level<0) level=0;
  3461. }else{
  3462. s->dct_error_sum[intra][i] -= level;
  3463. level += s->dct_offset[intra][i];
  3464. if(level>0) level=0;
  3465. }
  3466. block[i]= level;
  3467. }
  3468. }
  3469. }
  3470. static int dct_quantize_trellis_c(MpegEncContext *s,
  3471. int16_t *block, int n,
  3472. int qscale, int *overflow){
  3473. const int *qmat;
  3474. const uint16_t *matrix;
  3475. const uint8_t *scantable;
  3476. const uint8_t *perm_scantable;
  3477. int max=0;
  3478. unsigned int threshold1, threshold2;
  3479. int bias=0;
  3480. int run_tab[65];
  3481. int level_tab[65];
  3482. int score_tab[65];
  3483. int survivor[65];
  3484. int survivor_count;
  3485. int last_run=0;
  3486. int last_level=0;
  3487. int last_score= 0;
  3488. int last_i;
  3489. int coeff[2][64];
  3490. int coeff_count[64];
  3491. int qmul, qadd, start_i, last_non_zero, i, dc;
  3492. const int esc_length= s->ac_esc_length;
  3493. uint8_t * length;
  3494. uint8_t * last_length;
  3495. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3496. int mpeg2_qscale;
  3497. s->fdsp.fdct(block);
  3498. if(s->dct_error_sum)
  3499. s->denoise_dct(s, block);
  3500. qmul= qscale*16;
  3501. qadd= ((qscale-1)|1)*8;
  3502. if (s->q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
  3503. else mpeg2_qscale = qscale << 1;
  3504. if (s->mb_intra) {
  3505. int q;
  3506. scantable= s->intra_scantable.scantable;
  3507. perm_scantable= s->intra_scantable.permutated;
  3508. if (!s->h263_aic) {
  3509. if (n < 4)
  3510. q = s->y_dc_scale;
  3511. else
  3512. q = s->c_dc_scale;
  3513. q = q << 3;
  3514. } else{
  3515. /* For AIC we skip quant/dequant of INTRADC */
  3516. q = 1 << 3;
  3517. qadd=0;
  3518. }
  3519. /* note: block[0] is assumed to be positive */
  3520. block[0] = (block[0] + (q >> 1)) / q;
  3521. start_i = 1;
  3522. last_non_zero = 0;
  3523. qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
  3524. matrix = n < 4 ? s->intra_matrix : s->chroma_intra_matrix;
  3525. if(s->mpeg_quant || s->out_format == FMT_MPEG1 || s->out_format == FMT_MJPEG)
  3526. bias= 1<<(QMAT_SHIFT-1);
  3527. if (n > 3 && s->intra_chroma_ac_vlc_length) {
  3528. length = s->intra_chroma_ac_vlc_length;
  3529. last_length= s->intra_chroma_ac_vlc_last_length;
  3530. } else {
  3531. length = s->intra_ac_vlc_length;
  3532. last_length= s->intra_ac_vlc_last_length;
  3533. }
  3534. } else {
  3535. scantable= s->inter_scantable.scantable;
  3536. perm_scantable= s->inter_scantable.permutated;
  3537. start_i = 0;
  3538. last_non_zero = -1;
  3539. qmat = s->q_inter_matrix[qscale];
  3540. matrix = s->inter_matrix;
  3541. length = s->inter_ac_vlc_length;
  3542. last_length= s->inter_ac_vlc_last_length;
  3543. }
  3544. last_i= start_i;
  3545. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3546. threshold2= (threshold1<<1);
  3547. for(i=63; i>=start_i; i--) {
  3548. const int j = scantable[i];
  3549. int level = block[j] * qmat[j];
  3550. if(((unsigned)(level+threshold1))>threshold2){
  3551. last_non_zero = i;
  3552. break;
  3553. }
  3554. }
  3555. for(i=start_i; i<=last_non_zero; i++) {
  3556. const int j = scantable[i];
  3557. int level = block[j] * qmat[j];
  3558. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3559. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3560. if(((unsigned)(level+threshold1))>threshold2){
  3561. if(level>0){
  3562. level= (bias + level)>>QMAT_SHIFT;
  3563. coeff[0][i]= level;
  3564. coeff[1][i]= level-1;
  3565. // coeff[2][k]= level-2;
  3566. }else{
  3567. level= (bias - level)>>QMAT_SHIFT;
  3568. coeff[0][i]= -level;
  3569. coeff[1][i]= -level+1;
  3570. // coeff[2][k]= -level+2;
  3571. }
  3572. coeff_count[i]= FFMIN(level, 2);
  3573. av_assert2(coeff_count[i]);
  3574. max |=level;
  3575. }else{
  3576. coeff[0][i]= (level>>31)|1;
  3577. coeff_count[i]= 1;
  3578. }
  3579. }
  3580. *overflow= s->max_qcoeff < max; //overflow might have happened
  3581. if(last_non_zero < start_i){
  3582. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3583. return last_non_zero;
  3584. }
  3585. score_tab[start_i]= 0;
  3586. survivor[0]= start_i;
  3587. survivor_count= 1;
  3588. for(i=start_i; i<=last_non_zero; i++){
  3589. int level_index, j, zero_distortion;
  3590. int dct_coeff= FFABS(block[ scantable[i] ]);
  3591. int best_score=256*256*256*120;
  3592. if (s->fdsp.fdct == ff_fdct_ifast)
  3593. dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
  3594. zero_distortion= dct_coeff*dct_coeff;
  3595. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3596. int distortion;
  3597. int level= coeff[level_index][i];
  3598. const int alevel= FFABS(level);
  3599. int unquant_coeff;
  3600. av_assert2(level);
  3601. if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  3602. unquant_coeff= alevel*qmul + qadd;
  3603. } else if(s->out_format == FMT_MJPEG) {
  3604. j = s->idsp.idct_permutation[scantable[i]];
  3605. unquant_coeff = alevel * matrix[j] * 8;
  3606. }else{ // MPEG-1
  3607. j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
  3608. if(s->mb_intra){
  3609. unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
  3610. unquant_coeff = (unquant_coeff - 1) | 1;
  3611. }else{
  3612. unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
  3613. unquant_coeff = (unquant_coeff - 1) | 1;
  3614. }
  3615. unquant_coeff<<= 3;
  3616. }
  3617. distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
  3618. level+=64;
  3619. if((level&(~127)) == 0){
  3620. for(j=survivor_count-1; j>=0; j--){
  3621. int run= i - survivor[j];
  3622. int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3623. score += score_tab[i-run];
  3624. if(score < best_score){
  3625. best_score= score;
  3626. run_tab[i+1]= run;
  3627. level_tab[i+1]= level-64;
  3628. }
  3629. }
  3630. if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  3631. for(j=survivor_count-1; j>=0; j--){
  3632. int run= i - survivor[j];
  3633. int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3634. score += score_tab[i-run];
  3635. if(score < last_score){
  3636. last_score= score;
  3637. last_run= run;
  3638. last_level= level-64;
  3639. last_i= i+1;
  3640. }
  3641. }
  3642. }
  3643. }else{
  3644. distortion += esc_length*lambda;
  3645. for(j=survivor_count-1; j>=0; j--){
  3646. int run= i - survivor[j];
  3647. int score= distortion + score_tab[i-run];
  3648. if(score < best_score){
  3649. best_score= score;
  3650. run_tab[i+1]= run;
  3651. level_tab[i+1]= level-64;
  3652. }
  3653. }
  3654. if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  3655. for(j=survivor_count-1; j>=0; j--){
  3656. int run= i - survivor[j];
  3657. int score= distortion + score_tab[i-run];
  3658. if(score < last_score){
  3659. last_score= score;
  3660. last_run= run;
  3661. last_level= level-64;
  3662. last_i= i+1;
  3663. }
  3664. }
  3665. }
  3666. }
  3667. }
  3668. score_tab[i+1]= best_score;
  3669. // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
  3670. if(last_non_zero <= 27){
  3671. for(; survivor_count; survivor_count--){
  3672. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  3673. break;
  3674. }
  3675. }else{
  3676. for(; survivor_count; survivor_count--){
  3677. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  3678. break;
  3679. }
  3680. }
  3681. survivor[ survivor_count++ ]= i+1;
  3682. }
  3683. if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
  3684. last_score= 256*256*256*120;
  3685. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  3686. int score= score_tab[i];
  3687. if (i)
  3688. score += lambda * 2; // FIXME more exact?
  3689. if(score < last_score){
  3690. last_score= score;
  3691. last_i= i;
  3692. last_level= level_tab[i];
  3693. last_run= run_tab[i];
  3694. }
  3695. }
  3696. }
  3697. s->coded_score[n] = last_score;
  3698. dc= FFABS(block[0]);
  3699. last_non_zero= last_i - 1;
  3700. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3701. if(last_non_zero < start_i)
  3702. return last_non_zero;
  3703. if(last_non_zero == 0 && start_i == 0){
  3704. int best_level= 0;
  3705. int best_score= dc * dc;
  3706. for(i=0; i<coeff_count[0]; i++){
  3707. int level= coeff[i][0];
  3708. int alevel= FFABS(level);
  3709. int unquant_coeff, score, distortion;
  3710. if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  3711. unquant_coeff= (alevel*qmul + qadd)>>3;
  3712. } else{ // MPEG-1
  3713. unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
  3714. unquant_coeff = (unquant_coeff - 1) | 1;
  3715. }
  3716. unquant_coeff = (unquant_coeff + 4) >> 3;
  3717. unquant_coeff<<= 3 + 3;
  3718. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  3719. level+=64;
  3720. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  3721. else score= distortion + esc_length*lambda;
  3722. if(score < best_score){
  3723. best_score= score;
  3724. best_level= level - 64;
  3725. }
  3726. }
  3727. block[0]= best_level;
  3728. s->coded_score[n] = best_score - dc*dc;
  3729. if(best_level == 0) return -1;
  3730. else return last_non_zero;
  3731. }
  3732. i= last_i;
  3733. av_assert2(last_level);
  3734. block[ perm_scantable[last_non_zero] ]= last_level;
  3735. i -= last_run + 1;
  3736. for(; i>start_i; i -= run_tab[i] + 1){
  3737. block[ perm_scantable[i-1] ]= level_tab[i];
  3738. }
  3739. return last_non_zero;
  3740. }
  3741. static int16_t basis[64][64];
  3742. static void build_basis(uint8_t *perm){
  3743. int i, j, x, y;
  3744. emms_c();
  3745. for(i=0; i<8; i++){
  3746. for(j=0; j<8; j++){
  3747. for(y=0; y<8; y++){
  3748. for(x=0; x<8; x++){
  3749. double s= 0.25*(1<<BASIS_SHIFT);
  3750. int index= 8*i + j;
  3751. int perm_index= perm[index];
  3752. if(i==0) s*= sqrt(0.5);
  3753. if(j==0) s*= sqrt(0.5);
  3754. basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  3755. }
  3756. }
  3757. }
  3758. }
  3759. }
  3760. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  3761. int16_t *block, int16_t *weight, int16_t *orig,
  3762. int n, int qscale){
  3763. int16_t rem[64];
  3764. LOCAL_ALIGNED_16(int16_t, d1, [64]);
  3765. const uint8_t *scantable;
  3766. const uint8_t *perm_scantable;
  3767. // unsigned int threshold1, threshold2;
  3768. // int bias=0;
  3769. int run_tab[65];
  3770. int prev_run=0;
  3771. int prev_level=0;
  3772. int qmul, qadd, start_i, last_non_zero, i, dc;
  3773. uint8_t * length;
  3774. uint8_t * last_length;
  3775. int lambda;
  3776. int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
  3777. if(basis[0][0] == 0)
  3778. build_basis(s->idsp.idct_permutation);
  3779. qmul= qscale*2;
  3780. qadd= (qscale-1)|1;
  3781. if (s->mb_intra) {
  3782. scantable= s->intra_scantable.scantable;
  3783. perm_scantable= s->intra_scantable.permutated;
  3784. if (!s->h263_aic) {
  3785. if (n < 4)
  3786. q = s->y_dc_scale;
  3787. else
  3788. q = s->c_dc_scale;
  3789. } else{
  3790. /* For AIC we skip quant/dequant of INTRADC */
  3791. q = 1;
  3792. qadd=0;
  3793. }
  3794. q <<= RECON_SHIFT-3;
  3795. /* note: block[0] is assumed to be positive */
  3796. dc= block[0]*q;
  3797. // block[0] = (block[0] + (q >> 1)) / q;
  3798. start_i = 1;
  3799. // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3800. // bias= 1<<(QMAT_SHIFT-1);
  3801. if (n > 3 && s->intra_chroma_ac_vlc_length) {
  3802. length = s->intra_chroma_ac_vlc_length;
  3803. last_length= s->intra_chroma_ac_vlc_last_length;
  3804. } else {
  3805. length = s->intra_ac_vlc_length;
  3806. last_length= s->intra_ac_vlc_last_length;
  3807. }
  3808. } else {
  3809. scantable= s->inter_scantable.scantable;
  3810. perm_scantable= s->inter_scantable.permutated;
  3811. dc= 0;
  3812. start_i = 0;
  3813. length = s->inter_ac_vlc_length;
  3814. last_length= s->inter_ac_vlc_last_length;
  3815. }
  3816. last_non_zero = s->block_last_index[n];
  3817. dc += (1<<(RECON_SHIFT-1));
  3818. for(i=0; i<64; i++){
  3819. rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
  3820. }
  3821. sum=0;
  3822. for(i=0; i<64; i++){
  3823. int one= 36;
  3824. int qns=4;
  3825. int w;
  3826. w= FFABS(weight[i]) + qns*one;
  3827. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  3828. weight[i] = w;
  3829. // w=weight[i] = (63*qns + (w/2)) / w;
  3830. av_assert2(w>0);
  3831. av_assert2(w<(1<<6));
  3832. sum += w*w;
  3833. }
  3834. lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  3835. run=0;
  3836. rle_index=0;
  3837. for(i=start_i; i<=last_non_zero; i++){
  3838. int j= perm_scantable[i];
  3839. const int level= block[j];
  3840. int coeff;
  3841. if(level){
  3842. if(level<0) coeff= qmul*level - qadd;
  3843. else coeff= qmul*level + qadd;
  3844. run_tab[rle_index++]=run;
  3845. run=0;
  3846. s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
  3847. }else{
  3848. run++;
  3849. }
  3850. }
  3851. for(;;){
  3852. int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
  3853. int best_coeff=0;
  3854. int best_change=0;
  3855. int run2, best_unquant_change=0, analyze_gradient;
  3856. analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
  3857. if(analyze_gradient){
  3858. for(i=0; i<64; i++){
  3859. int w= weight[i];
  3860. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  3861. }
  3862. s->fdsp.fdct(d1);
  3863. }
  3864. if(start_i){
  3865. const int level= block[0];
  3866. int change, old_coeff;
  3867. av_assert2(s->mb_intra);
  3868. old_coeff= q*level;
  3869. for(change=-1; change<=1; change+=2){
  3870. int new_level= level + change;
  3871. int score, new_coeff;
  3872. new_coeff= q*new_level;
  3873. if(new_coeff >= 2048 || new_coeff < 0)
  3874. continue;
  3875. score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
  3876. new_coeff - old_coeff);
  3877. if(score<best_score){
  3878. best_score= score;
  3879. best_coeff= 0;
  3880. best_change= change;
  3881. best_unquant_change= new_coeff - old_coeff;
  3882. }
  3883. }
  3884. }
  3885. run=0;
  3886. rle_index=0;
  3887. run2= run_tab[rle_index++];
  3888. prev_level=0;
  3889. prev_run=0;
  3890. for(i=start_i; i<64; i++){
  3891. int j= perm_scantable[i];
  3892. const int level= block[j];
  3893. int change, old_coeff;
  3894. if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  3895. break;
  3896. if(level){
  3897. if(level<0) old_coeff= qmul*level - qadd;
  3898. else old_coeff= qmul*level + qadd;
  3899. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  3900. }else{
  3901. old_coeff=0;
  3902. run2--;
  3903. av_assert2(run2>=0 || i >= last_non_zero );
  3904. }
  3905. for(change=-1; change<=1; change+=2){
  3906. int new_level= level + change;
  3907. int score, new_coeff, unquant_change;
  3908. score=0;
  3909. if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
  3910. continue;
  3911. if(new_level){
  3912. if(new_level<0) new_coeff= qmul*new_level - qadd;
  3913. else new_coeff= qmul*new_level + qadd;
  3914. if(new_coeff >= 2048 || new_coeff <= -2048)
  3915. continue;
  3916. //FIXME check for overflow
  3917. if(level){
  3918. if(level < 63 && level > -63){
  3919. if(i < last_non_zero)
  3920. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3921. - length[UNI_AC_ENC_INDEX(run, level+64)];
  3922. else
  3923. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3924. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  3925. }
  3926. }else{
  3927. av_assert2(FFABS(new_level)==1);
  3928. if(analyze_gradient){
  3929. int g= d1[ scantable[i] ];
  3930. if(g && (g^new_level) >= 0)
  3931. continue;
  3932. }
  3933. if(i < last_non_zero){
  3934. int next_i= i + run2 + 1;
  3935. int next_level= block[ perm_scantable[next_i] ] + 64;
  3936. if(next_level&(~127))
  3937. next_level= 0;
  3938. if(next_i < last_non_zero)
  3939. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3940. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  3941. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3942. else
  3943. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3944. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3945. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3946. }else{
  3947. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  3948. if(prev_level){
  3949. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3950. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3951. }
  3952. }
  3953. }
  3954. }else{
  3955. new_coeff=0;
  3956. av_assert2(FFABS(level)==1);
  3957. if(i < last_non_zero){
  3958. int next_i= i + run2 + 1;
  3959. int next_level= block[ perm_scantable[next_i] ] + 64;
  3960. if(next_level&(~127))
  3961. next_level= 0;
  3962. if(next_i < last_non_zero)
  3963. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3964. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  3965. - length[UNI_AC_ENC_INDEX(run, 65)];
  3966. else
  3967. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3968. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3969. - length[UNI_AC_ENC_INDEX(run, 65)];
  3970. }else{
  3971. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  3972. if(prev_level){
  3973. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3974. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3975. }
  3976. }
  3977. }
  3978. score *= lambda;
  3979. unquant_change= new_coeff - old_coeff;
  3980. av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
  3981. score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
  3982. unquant_change);
  3983. if(score<best_score){
  3984. best_score= score;
  3985. best_coeff= i;
  3986. best_change= change;
  3987. best_unquant_change= unquant_change;
  3988. }
  3989. }
  3990. if(level){
  3991. prev_level= level + 64;
  3992. if(prev_level&(~127))
  3993. prev_level= 0;
  3994. prev_run= run;
  3995. run=0;
  3996. }else{
  3997. run++;
  3998. }
  3999. }
  4000. if(best_change){
  4001. int j= perm_scantable[ best_coeff ];
  4002. block[j] += best_change;
  4003. if(best_coeff > last_non_zero){
  4004. last_non_zero= best_coeff;
  4005. av_assert2(block[j]);
  4006. }else{
  4007. for(; last_non_zero>=start_i; last_non_zero--){
  4008. if(block[perm_scantable[last_non_zero]])
  4009. break;
  4010. }
  4011. }
  4012. run=0;
  4013. rle_index=0;
  4014. for(i=start_i; i<=last_non_zero; i++){
  4015. int j= perm_scantable[i];
  4016. const int level= block[j];
  4017. if(level){
  4018. run_tab[rle_index++]=run;
  4019. run=0;
  4020. }else{
  4021. run++;
  4022. }
  4023. }
  4024. s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
  4025. }else{
  4026. break;
  4027. }
  4028. }
  4029. return last_non_zero;
  4030. }
  4031. /**
  4032. * Permute an 8x8 block according to permutation.
  4033. * @param block the block which will be permuted according to
  4034. * the given permutation vector
  4035. * @param permutation the permutation vector
  4036. * @param last the last non zero coefficient in scantable order, used to
  4037. * speed the permutation up
  4038. * @param scantable the used scantable, this is only used to speed the
  4039. * permutation up, the block is not (inverse) permutated
  4040. * to scantable order!
  4041. */
  4042. void ff_block_permute(int16_t *block, uint8_t *permutation,
  4043. const uint8_t *scantable, int last)
  4044. {
  4045. int i;
  4046. int16_t temp[64];
  4047. if (last <= 0)
  4048. return;
  4049. //FIXME it is ok but not clean and might fail for some permutations
  4050. // if (permutation[1] == 1)
  4051. // return;
  4052. for (i = 0; i <= last; i++) {
  4053. const int j = scantable[i];
  4054. temp[j] = block[j];
  4055. block[j] = 0;
  4056. }
  4057. for (i = 0; i <= last; i++) {
  4058. const int j = scantable[i];
  4059. const int perm_j = permutation[j];
  4060. block[perm_j] = temp[j];
  4061. }
  4062. }
  4063. int ff_dct_quantize_c(MpegEncContext *s,
  4064. int16_t *block, int n,
  4065. int qscale, int *overflow)
  4066. {
  4067. int i, j, level, last_non_zero, q, start_i;
  4068. const int *qmat;
  4069. const uint8_t *scantable;
  4070. int bias;
  4071. int max=0;
  4072. unsigned int threshold1, threshold2;
  4073. s->fdsp.fdct(block);
  4074. if(s->dct_error_sum)
  4075. s->denoise_dct(s, block);
  4076. if (s->mb_intra) {
  4077. scantable= s->intra_scantable.scantable;
  4078. if (!s->h263_aic) {
  4079. if (n < 4)
  4080. q = s->y_dc_scale;
  4081. else
  4082. q = s->c_dc_scale;
  4083. q = q << 3;
  4084. } else
  4085. /* For AIC we skip quant/dequant of INTRADC */
  4086. q = 1 << 3;
  4087. /* note: block[0] is assumed to be positive */
  4088. block[0] = (block[0] + (q >> 1)) / q;
  4089. start_i = 1;
  4090. last_non_zero = 0;
  4091. qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
  4092. bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
  4093. } else {
  4094. scantable= s->inter_scantable.scantable;
  4095. start_i = 0;
  4096. last_non_zero = -1;
  4097. qmat = s->q_inter_matrix[qscale];
  4098. bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
  4099. }
  4100. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4101. threshold2= (threshold1<<1);
  4102. for(i=63;i>=start_i;i--) {
  4103. j = scantable[i];
  4104. level = block[j] * qmat[j];
  4105. if(((unsigned)(level+threshold1))>threshold2){
  4106. last_non_zero = i;
  4107. break;
  4108. }else{
  4109. block[j]=0;
  4110. }
  4111. }
  4112. for(i=start_i; i<=last_non_zero; i++) {
  4113. j = scantable[i];
  4114. level = block[j] * qmat[j];
  4115. // if( bias+level >= (1<<QMAT_SHIFT)
  4116. // || bias-level >= (1<<QMAT_SHIFT)){
  4117. if(((unsigned)(level+threshold1))>threshold2){
  4118. if(level>0){
  4119. level= (bias + level)>>QMAT_SHIFT;
  4120. block[j]= level;
  4121. }else{
  4122. level= (bias - level)>>QMAT_SHIFT;
  4123. block[j]= -level;
  4124. }
  4125. max |=level;
  4126. }else{
  4127. block[j]=0;
  4128. }
  4129. }
  4130. *overflow= s->max_qcoeff < max; //overflow might have happened
  4131. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4132. if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
  4133. ff_block_permute(block, s->idsp.idct_permutation,
  4134. scantable, last_non_zero);
  4135. return last_non_zero;
  4136. }
  4137. #define OFFSET(x) offsetof(MpegEncContext, x)
  4138. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  4139. static const AVOption h263_options[] = {
  4140. { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  4141. { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  4142. FF_MPV_COMMON_OPTS
  4143. { NULL },
  4144. };
  4145. static const AVClass h263_class = {
  4146. .class_name = "H.263 encoder",
  4147. .item_name = av_default_item_name,
  4148. .option = h263_options,
  4149. .version = LIBAVUTIL_VERSION_INT,
  4150. };
  4151. AVCodec ff_h263_encoder = {
  4152. .name = "h263",
  4153. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
  4154. .type = AVMEDIA_TYPE_VIDEO,
  4155. .id = AV_CODEC_ID_H263,
  4156. .priv_data_size = sizeof(MpegEncContext),
  4157. .init = ff_mpv_encode_init,
  4158. .encode2 = ff_mpv_encode_picture,
  4159. .close = ff_mpv_encode_end,
  4160. .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
  4161. .priv_class = &h263_class,
  4162. };
  4163. static const AVOption h263p_options[] = {
  4164. { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  4165. { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  4166. { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  4167. { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
  4168. FF_MPV_COMMON_OPTS
  4169. { NULL },
  4170. };
  4171. static const AVClass h263p_class = {
  4172. .class_name = "H.263p encoder",
  4173. .item_name = av_default_item_name,
  4174. .option = h263p_options,
  4175. .version = LIBAVUTIL_VERSION_INT,
  4176. };
  4177. AVCodec ff_h263p_encoder = {
  4178. .name = "h263p",
  4179. .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
  4180. .type = AVMEDIA_TYPE_VIDEO,
  4181. .id = AV_CODEC_ID_H263P,
  4182. .priv_data_size = sizeof(MpegEncContext),
  4183. .init = ff_mpv_encode_init,
  4184. .encode2 = ff_mpv_encode_picture,
  4185. .close = ff_mpv_encode_end,
  4186. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  4187. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  4188. .priv_class = &h263p_class,
  4189. };
  4190. static const AVClass msmpeg4v2_class = {
  4191. .class_name = "msmpeg4v2 encoder",
  4192. .item_name = av_default_item_name,
  4193. .option = ff_mpv_generic_options,
  4194. .version = LIBAVUTIL_VERSION_INT,
  4195. };
  4196. AVCodec ff_msmpeg4v2_encoder = {
  4197. .name = "msmpeg4v2",
  4198. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
  4199. .type = AVMEDIA_TYPE_VIDEO,
  4200. .id = AV_CODEC_ID_MSMPEG4V2,
  4201. .priv_data_size = sizeof(MpegEncContext),
  4202. .init = ff_mpv_encode_init,
  4203. .encode2 = ff_mpv_encode_picture,
  4204. .close = ff_mpv_encode_end,
  4205. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  4206. .priv_class = &msmpeg4v2_class,
  4207. };
  4208. static const AVClass msmpeg4v3_class = {
  4209. .class_name = "msmpeg4v3 encoder",
  4210. .item_name = av_default_item_name,
  4211. .option = ff_mpv_generic_options,
  4212. .version = LIBAVUTIL_VERSION_INT,
  4213. };
  4214. AVCodec ff_msmpeg4v3_encoder = {
  4215. .name = "msmpeg4",
  4216. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
  4217. .type = AVMEDIA_TYPE_VIDEO,
  4218. .id = AV_CODEC_ID_MSMPEG4V3,
  4219. .priv_data_size = sizeof(MpegEncContext),
  4220. .init = ff_mpv_encode_init,
  4221. .encode2 = ff_mpv_encode_picture,
  4222. .close = ff_mpv_encode_end,
  4223. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  4224. .priv_class = &msmpeg4v3_class,
  4225. };
  4226. static const AVClass wmv1_class = {
  4227. .class_name = "wmv1 encoder",
  4228. .item_name = av_default_item_name,
  4229. .option = ff_mpv_generic_options,
  4230. .version = LIBAVUTIL_VERSION_INT,
  4231. };
  4232. AVCodec ff_wmv1_encoder = {
  4233. .name = "wmv1",
  4234. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
  4235. .type = AVMEDIA_TYPE_VIDEO,
  4236. .id = AV_CODEC_ID_WMV1,
  4237. .priv_data_size = sizeof(MpegEncContext),
  4238. .init = ff_mpv_encode_init,
  4239. .encode2 = ff_mpv_encode_picture,
  4240. .close = ff_mpv_encode_end,
  4241. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  4242. .priv_class = &wmv1_class,
  4243. };