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.

4362 lines
156KB

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