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.

4918 lines
179KB

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