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.

4314 lines
156KB

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