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.

4370 lines
157KB

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