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