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.

4885 lines
178KB

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