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.

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