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.

4878 lines
179KB

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