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.

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