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.

4371 lines
157KB

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