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.

4852 lines
178KB

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