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