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.

4805 lines
176KB

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