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.

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