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.

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