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.

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