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.

4885 lines
180KB

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