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