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.

4839 lines
178KB

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