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