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.

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