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.

4358 lines
156KB

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