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.

4770 lines
175KB

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