You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4886 lines
180KB

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