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.

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