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 "mpeg12codecs.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) {
650 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
652  s->rtp_mode = 1;
653  /* fallthrough */
655  s->out_format = FMT_MPEG1;
656  s->low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
657  avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
659  break;
660 #endif
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)
686  if (ret < 0)
687  return ret;
688  s->out_format = FMT_H261;
689  avctx->delay = 0;
690  s->low_delay = 1;
691  s->rtp_mode = 0; /* Sliced encoding not supported */
692  break;
693  case AV_CODEC_ID_H263:
694  if (!CONFIG_H263_ENCODER)
697  s->width, s->height) == 8) {
699  "The specified picture size of %dx%d is not valid for "
700  "the H.263 codec.\nValid sizes are 128x96, 176x144, "
701  "352x288, 704x576, and 1408x1152. "
702  "Try H.263+.\n", s->width, s->height);
703  return AVERROR(EINVAL);
704  }
705  s->out_format = FMT_H263;
706  avctx->delay = 0;
707  s->low_delay = 1;
708  break;
709  case AV_CODEC_ID_H263P:
710  s->out_format = FMT_H263;
711  s->h263_plus = 1;
712  /* Fx */
713  s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
714  s->modified_quant = s->h263_aic;
715  s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
716  s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
717 
718  /* /Fx */
719  /* These are just to be sure */
720  avctx->delay = 0;
721  s->low_delay = 1;
722  break;
723  case AV_CODEC_ID_FLV1:
724  s->out_format = FMT_H263;
725  s->h263_flv = 2; /* format = 1; 11-bit codes */
726  s->unrestricted_mv = 1;
727  s->rtp_mode = 0; /* don't allow GOB */
728  avctx->delay = 0;
729  s->low_delay = 1;
730  break;
731  case AV_CODEC_ID_RV10:
732  s->out_format = FMT_H263;
733  avctx->delay = 0;
734  s->low_delay = 1;
735  break;
736  case AV_CODEC_ID_RV20:
737  s->out_format = FMT_H263;
738  avctx->delay = 0;
739  s->low_delay = 1;
740  s->modified_quant = 1;
741  s->h263_aic = 1;
742  s->h263_plus = 1;
743  s->loop_filter = 1;
744  s->unrestricted_mv = 0;
745  break;
746  case AV_CODEC_ID_MPEG4:
747  s->out_format = FMT_H263;
748  s->h263_pred = 1;
749  s->unrestricted_mv = 1;
750  s->low_delay = s->max_b_frames ? 0 : 1;
751  avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
752  break;
754  s->out_format = FMT_H263;
755  s->h263_pred = 1;
756  s->unrestricted_mv = 1;
757  s->msmpeg4_version = 2;
758  avctx->delay = 0;
759  s->low_delay = 1;
760  break;
762  s->out_format = FMT_H263;
763  s->h263_pred = 1;
764  s->unrestricted_mv = 1;
765  s->msmpeg4_version = 3;
766  s->flipflop_rounding = 1;
767  avctx->delay = 0;
768  s->low_delay = 1;
769  break;
770  case AV_CODEC_ID_WMV1:
771  s->out_format = FMT_H263;
772  s->h263_pred = 1;
773  s->unrestricted_mv = 1;
774  s->msmpeg4_version = 4;
775  s->flipflop_rounding = 1;
776  avctx->delay = 0;
777  s->low_delay = 1;
778  break;
779  case AV_CODEC_ID_WMV2:
780  s->out_format = FMT_H263;
781  s->h263_pred = 1;
782  s->unrestricted_mv = 1;
783  s->msmpeg4_version = 5;
784  s->flipflop_rounding = 1;
785  avctx->delay = 0;
786  s->low_delay = 1;
787  break;
788  default:
789  return AVERROR(EINVAL);
790  }
791 
792  avctx->has_b_frames = !s->low_delay;
793 
794  s->encoding = 1;
795 
796  s->progressive_frame =
797  s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
799  s->alternate_scan);
800 
801  /* init */
803  if ((ret = ff_mpv_common_init(s)) < 0)
804  return ret;
805 
806  ff_fdctdsp_init(&s->fdsp, avctx);
807  ff_me_cmp_init(&s->mecc, avctx);
808  ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
809  ff_pixblockdsp_init(&s->pdsp, avctx);
810 
811  if (!(avctx->stats_out = av_mallocz(256)) ||
812  !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||
813  !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) ||
814  !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix, 32) ||
815  !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16, 32) ||
816  !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) ||
817  !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16, 32) ||
818  !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_PICTURE_COUNT) ||
819  !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT))
820  return AVERROR(ENOMEM);
821 
822  /* Allocate MV tables; the MV and MB tables will be copied
823  * to slice contexts by ff_update_duplicate_context(). */
824  mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
825  if (!FF_ALLOCZ_TYPED_ARRAY(s->p_mv_table_base, mv_table_size) ||
826  !FF_ALLOCZ_TYPED_ARRAY(s->b_forw_mv_table_base, mv_table_size) ||
827  !FF_ALLOCZ_TYPED_ARRAY(s->b_back_mv_table_base, mv_table_size) ||
828  !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_forw_mv_table_base, mv_table_size) ||
829  !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_back_mv_table_base, mv_table_size) ||
830  !FF_ALLOCZ_TYPED_ARRAY(s->b_direct_mv_table_base, mv_table_size))
831  return AVERROR(ENOMEM);
832  s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
833  s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
834  s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
835  s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
836  s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
837  s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
838 
839  /* Allocate MB type table */
840  mb_array_size = s->mb_stride * s->mb_height;
841  if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) ||
842  !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) ||
843  !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) ||
844  !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size) ||
845  !FF_ALLOCZ_TYPED_ARRAY(s->mc_mb_var, mb_array_size) ||
846  !FF_ALLOCZ_TYPED_ARRAY(s->mb_var, mb_array_size) ||
847  !(s->mb_mean = av_mallocz(mb_array_size)))
848  return AVERROR(ENOMEM);
849 
850 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
851  if (s->codec_id == AV_CODEC_ID_MPEG4 ||
852  (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
853  int16_t (*tmp1)[2];
854  uint8_t *tmp2;
855  if (!(tmp1 = ALLOCZ_ARRAYS(s->b_field_mv_table_base, 8, mv_table_size)) ||
856  !(tmp2 = ALLOCZ_ARRAYS(s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
857  !ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * 2, mv_table_size))
858  return AVERROR(ENOMEM);
859 
860  s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
861  tmp1 += s->mb_stride + 1;
862 
863  for (int i = 0; i < 2; i++) {
864  for (int j = 0; j < 2; j++) {
865  for (int k = 0; k < 2; k++) {
866  s->b_field_mv_table[i][j][k] = tmp1;
867  tmp1 += mv_table_size;
868  }
869  s->b_field_select_table[i][j] = tmp2;
870  tmp2 += 2 * mv_table_size;
871  }
872  }
873  }
874 
875  if (s->noise_reduction) {
876  if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
877  return AVERROR(ENOMEM);
878  }
879 
881 
882  if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
883  s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
884  s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
885  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
886  s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
887  s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
888  } else {
889  s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
890  s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
891  }
892 
893  if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
894  s->chroma_qscale_table = ff_h263_chroma_qscale_table;
895 
896  if (s->slice_context_count > 1) {
897  s->rtp_mode = 1;
898 
900  s->h263_slice_structured = 1;
901  }
902 
903  s->quant_precision = 5;
904 
905  ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, avctx->ildct_cmp);
906  ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
907 
908  if (CONFIG_H263_ENCODER && s->out_format == FMT_H263) {
910  if (CONFIG_MSMPEG4ENC && s->msmpeg4_version)
912  }
913 
914  /* init q matrix */
915  for (i = 0; i < 64; i++) {
916  int j = s->idsp.idct_permutation[i];
917  if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
918  s->mpeg_quant) {
919  s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
920  s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
921  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
922  s->intra_matrix[j] =
923  s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
924  } else if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
925  s->intra_matrix[j] =
926  s->inter_matrix[j] = ff_mpeg1_default_intra_matrix[i];
927  } else {
928  /* MPEG-1/2 */
929  s->chroma_intra_matrix[j] =
930  s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
931  s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
932  }
933  if (avctx->intra_matrix)
934  s->intra_matrix[j] = avctx->intra_matrix[i];
935  if (avctx->inter_matrix)
936  s->inter_matrix[j] = avctx->inter_matrix[i];
937  }
938 
939  /* precompute matrix */
940  /* for mjpeg, we do include qscale in the matrix */
941  if (s->out_format != FMT_MJPEG) {
942  ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
943  s->intra_matrix, s->intra_quant_bias, avctx->qmin,
944  31, 1);
945  ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
946  s->inter_matrix, s->inter_quant_bias, avctx->qmin,
947  31, 0);
948  }
949 
950  if ((ret = ff_rate_control_init(s)) < 0)
951  return ret;
952 
953  if (s->b_frame_strategy == 2) {
954  for (i = 0; i < s->max_b_frames + 2; i++) {
955  s->tmp_frames[i] = av_frame_alloc();
956  if (!s->tmp_frames[i])
957  return AVERROR(ENOMEM);
958 
959  s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
960  s->tmp_frames[i]->width = s->width >> s->brd_scale;
961  s->tmp_frames[i]->height = s->height >> s->brd_scale;
962 
963  ret = av_frame_get_buffer(s->tmp_frames[i], 0);
964  if (ret < 0)
965  return ret;
966  }
967  }
968 
969  cpb_props = ff_add_cpb_side_data(avctx);
970  if (!cpb_props)
971  return AVERROR(ENOMEM);
972  cpb_props->max_bitrate = avctx->rc_max_rate;
973  cpb_props->min_bitrate = avctx->rc_min_rate;
974  cpb_props->avg_bitrate = avctx->bit_rate;
975  cpb_props->buffer_size = avctx->rc_buffer_size;
976 
977  return 0;
978 }
979 
981 {
983  int i;
984 
986 
988 
989  for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
990  av_frame_free(&s->tmp_frames[i]);
991 
992  av_frame_free(&s->new_picture);
993 
995 
996  av_freep(&s->p_mv_table_base);
997  av_freep(&s->b_forw_mv_table_base);
998  av_freep(&s->b_back_mv_table_base);
999  av_freep(&s->b_bidir_forw_mv_table_base);
1000  av_freep(&s->b_bidir_back_mv_table_base);
1001  av_freep(&s->b_direct_mv_table_base);
1002  av_freep(&s->b_field_mv_table_base);
1003  av_freep(&s->b_field_select_table[0][0]);
1004  av_freep(&s->p_field_select_table[0]);
1005 
1006  av_freep(&s->mb_type);
1007  av_freep(&s->lambda_table);
1008 
1009  av_freep(&s->cplx_tab);
1010  av_freep(&s->bits_tab);
1011 
1012  if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
1013  if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
1014  s->q_chroma_intra_matrix= NULL;
1015  s->q_chroma_intra_matrix16= NULL;
1016  av_freep(&s->q_intra_matrix);
1017  av_freep(&s->q_inter_matrix);
1018  av_freep(&s->q_intra_matrix16);
1019  av_freep(&s->q_inter_matrix16);
1020  av_freep(&s->input_picture);
1021  av_freep(&s->reordered_input_picture);
1022  av_freep(&s->dct_offset);
1023  av_freep(&s->mb_var);
1024  av_freep(&s->mc_mb_var);
1025  av_freep(&s->mb_mean);
1026 
1027  return 0;
1028 }
1029 
1030 #define IS_ENCODER 1
1032 
1033 static void mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
1034 {
1035  if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
1036  /* print DCT coefficients */
1037  av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1038  for (int i = 0; i < 6; i++) {
1039  for (int j = 0; j < 64; j++) {
1040  av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1041  block[i][s->idsp.idct_permutation[j]]);
1042  }
1043  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1044  }
1045  }
1046 
1048 }
1049 
1050 static int get_sae(const uint8_t *src, int ref, int stride)
1051 {
1052  int x,y;
1053  int acc = 0;
1054 
1055  for (y = 0; y < 16; y++) {
1056  for (x = 0; x < 16; x++) {
1057  acc += FFABS(src[x + y * stride] - ref);
1058  }
1059  }
1060 
1061  return acc;
1062 }
1063 
1064 static int get_intra_count(MpegEncContext *s, const uint8_t *src,
1065  const uint8_t *ref, int stride)
1066 {
1067  int x, y, w, h;
1068  int acc = 0;
1069 
1070  w = s->width & ~15;
1071  h = s->height & ~15;
1072 
1073  for (y = 0; y < h; y += 16) {
1074  for (x = 0; x < w; x += 16) {
1075  int offset = x + y * stride;
1076  int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
1077  stride, 16);
1078  int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1079  int sae = get_sae(src + offset, mean, stride);
1080 
1081  acc += sae + 500 < sad;
1082  }
1083  }
1084  return acc;
1085 }
1086 
1087 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
1088 {
1089  return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
1090  s->chroma_x_shift, s->chroma_y_shift, s->out_format,
1091  s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
1092  &s->linesize, &s->uvlinesize);
1093 }
1094 
1095 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1096 {
1097  Picture *pic = NULL;
1098  int64_t pts;
1099  int i, display_picture_number = 0, ret;
1100  int encoding_delay = s->max_b_frames ? s->max_b_frames
1101  : (s->low_delay ? 0 : 1);
1102  int flush_offset = 1;
1103  int direct = 1;
1104 
1105  if (pic_arg) {
1106  pts = pic_arg->pts;
1107  display_picture_number = s->input_picture_number++;
1108 
1109  if (pts != AV_NOPTS_VALUE) {
1110  if (s->user_specified_pts != AV_NOPTS_VALUE) {
1111  int64_t last = s->user_specified_pts;
1112 
1113  if (pts <= last) {
1114  av_log(s->avctx, AV_LOG_ERROR,
1115  "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1116  pts, last);
1117  return AVERROR(EINVAL);
1118  }
1119 
1120  if (!s->low_delay && display_picture_number == 1)
1121  s->dts_delta = pts - last;
1122  }
1123  s->user_specified_pts = pts;
1124  } else {
1125  if (s->user_specified_pts != AV_NOPTS_VALUE) {
1126  s->user_specified_pts =
1127  pts = s->user_specified_pts + 1;
1128  av_log(s->avctx, AV_LOG_INFO,
1129  "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1130  pts);
1131  } else {
1132  pts = display_picture_number;
1133  }
1134  }
1135 
1136  if (!pic_arg->buf[0] ||
1137  pic_arg->linesize[0] != s->linesize ||
1138  pic_arg->linesize[1] != s->uvlinesize ||
1139  pic_arg->linesize[2] != s->uvlinesize)
1140  direct = 0;
1141  if ((s->width & 15) || (s->height & 15))
1142  direct = 0;
1143  if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1144  direct = 0;
1145  if (s->linesize & (STRIDE_ALIGN-1))
1146  direct = 0;
1147 
1148  ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
1149  pic_arg->linesize[1], s->linesize, s->uvlinesize);
1150 
1151  i = ff_find_unused_picture(s->avctx, s->picture, direct);
1152  if (i < 0)
1153  return i;
1154 
1155  pic = &s->picture[i];
1156  pic->reference = 3;
1157 
1158  if (direct) {
1159  if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1160  return ret;
1161  }
1162  ret = alloc_picture(s, pic, direct);
1163  if (ret < 0)
1164  return ret;
1165 
1166  if (!direct) {
1167  if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1168  pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1169  pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1170  // empty
1171  } else {
1172  int h_chroma_shift, v_chroma_shift;
1173  av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1174  &h_chroma_shift,
1175  &v_chroma_shift);
1176 
1177  for (i = 0; i < 3; i++) {
1178  int src_stride = pic_arg->linesize[i];
1179  int dst_stride = i ? s->uvlinesize : s->linesize;
1180  int h_shift = i ? h_chroma_shift : 0;
1181  int v_shift = i ? v_chroma_shift : 0;
1182  int w = s->width >> h_shift;
1183  int h = s->height >> v_shift;
1184  const uint8_t *src = pic_arg->data[i];
1185  uint8_t *dst = pic->f->data[i];
1186  int vpad = 16;
1187 
1188  if ( s->codec_id == AV_CODEC_ID_MPEG2VIDEO
1189  && !s->progressive_sequence
1190  && FFALIGN(s->height, 32) - s->height > 16)
1191  vpad = 32;
1192 
1193  if (!s->avctx->rc_buffer_size)
1194  dst += INPLACE_OFFSET;
1195 
1196  if (src_stride == dst_stride)
1197  memcpy(dst, src, src_stride * h);
1198  else {
1199  int h2 = h;
1200  uint8_t *dst2 = dst;
1201  while (h2--) {
1202  memcpy(dst2, src, w);
1203  dst2 += dst_stride;
1204  src += src_stride;
1205  }
1206  }
1207  if ((s->width & 15) || (s->height & (vpad-1))) {
1208  s->mpvencdsp.draw_edges(dst, dst_stride,
1209  w, h,
1210  16 >> h_shift,
1211  vpad >> v_shift,
1212  EDGE_BOTTOM);
1213  }
1214  }
1215  emms_c();
1216  }
1217  }
1218  ret = av_frame_copy_props(pic->f, pic_arg);
1219  if (ret < 0)
1220  return ret;
1221 
1222  pic->f->display_picture_number = display_picture_number;
1223  pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1224  } else {
1225  /* Flushing: When we have not received enough input frames,
1226  * ensure s->input_picture[0] contains the first picture */
1227  for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1228  if (s->input_picture[flush_offset])
1229  break;
1230 
1231  if (flush_offset <= 1)
1232  flush_offset = 1;
1233  else
1234  encoding_delay = encoding_delay - flush_offset + 1;
1235  }
1236 
1237  /* shift buffer entries */
1238  for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1239  s->input_picture[i - flush_offset] = s->input_picture[i];
1240 
1241  s->input_picture[encoding_delay] = pic;
1242 
1243  return 0;
1244 }
1245 
1246 static int skip_check(MpegEncContext *s, const Picture *p, const Picture *ref)
1247 {
1248  int x, y, plane;
1249  int score = 0;
1250  int64_t score64 = 0;
1251 
1252  for (plane = 0; plane < 3; plane++) {
1253  const int stride = p->f->linesize[plane];
1254  const int bw = plane ? 1 : 2;
1255  for (y = 0; y < s->mb_height * bw; y++) {
1256  for (x = 0; x < s->mb_width * bw; x++) {
1257  int off = p->shared ? 0 : 16;
1258  const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1259  const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1260  int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1261 
1262  switch (FFABS(s->frame_skip_exp)) {
1263  case 0: score = FFMAX(score, v); break;
1264  case 1: score += FFABS(v); break;
1265  case 2: score64 += v * (int64_t)v; break;
1266  case 3: score64 += FFABS(v * (int64_t)v * v); break;
1267  case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1268  }
1269  }
1270  }
1271  }
1272  emms_c();
1273 
1274  if (score)
1275  score64 = score;
1276  if (s->frame_skip_exp < 0)
1277  score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1278  -1.0/s->frame_skip_exp);
1279 
1280  if (score64 < s->frame_skip_threshold)
1281  return 1;
1282  if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
1283  return 1;
1284  return 0;
1285 }
1286 
1288 {
1289  int ret;
1290  int size = 0;
1291 
1293  if (ret < 0)
1294  return ret;
1295 
1296  do {
1298  if (ret >= 0) {
1299  size += pkt->size;
1301  } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1302  return ret;
1303  } while (ret >= 0);
1304 
1305  return size;
1306 }
1307 
1309 {
1310  AVPacket *pkt;
1311  const int scale = s->brd_scale;
1312  int width = s->width >> scale;
1313  int height = s->height >> scale;
1314  int i, j, out_size, p_lambda, b_lambda, lambda2;
1315  int64_t best_rd = INT64_MAX;
1316  int best_b_count = -1;
1317  int ret = 0;
1318 
1319  av_assert0(scale >= 0 && scale <= 3);
1320 
1321  pkt = av_packet_alloc();
1322  if (!pkt)
1323  return AVERROR(ENOMEM);
1324 
1325  //emms_c();
1326  //s->next_picture_ptr->quality;
1327  p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1328  //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1329  b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1330  if (!b_lambda) // FIXME we should do this somewhere else
1331  b_lambda = p_lambda;
1332  lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1334 
1335  for (i = 0; i < s->max_b_frames + 2; i++) {
1336  const Picture *pre_input_ptr = i ? s->input_picture[i - 1] :
1337  s->next_picture_ptr;
1338 
1339  if (pre_input_ptr) {
1340  const uint8_t *data[4];
1341  memcpy(data, pre_input_ptr->f->data, sizeof(data));
1342 
1343  if (!pre_input_ptr->shared && i) {
1344  data[0] += INPLACE_OFFSET;
1345  data[1] += INPLACE_OFFSET;
1346  data[2] += INPLACE_OFFSET;
1347  }
1348 
1349  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1350  s->tmp_frames[i]->linesize[0],
1351  data[0],
1352  pre_input_ptr->f->linesize[0],
1353  width, height);
1354  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1355  s->tmp_frames[i]->linesize[1],
1356  data[1],
1357  pre_input_ptr->f->linesize[1],
1358  width >> 1, height >> 1);
1359  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1360  s->tmp_frames[i]->linesize[2],
1361  data[2],
1362  pre_input_ptr->f->linesize[2],
1363  width >> 1, height >> 1);
1364  }
1365  }
1366 
1367  for (j = 0; j < s->max_b_frames + 1; j++) {
1368  AVCodecContext *c;
1369  int64_t rd = 0;
1370 
1371  if (!s->input_picture[j])
1372  break;
1373 
1375  if (!c) {
1376  ret = AVERROR(ENOMEM);
1377  goto fail;
1378  }
1379 
1380  c->width = width;
1381  c->height = height;
1383  c->flags |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
1384  c->mb_decision = s->avctx->mb_decision;
1385  c->me_cmp = s->avctx->me_cmp;
1386  c->mb_cmp = s->avctx->mb_cmp;
1387  c->me_sub_cmp = s->avctx->me_sub_cmp;
1388  c->pix_fmt = AV_PIX_FMT_YUV420P;
1389  c->time_base = s->avctx->time_base;
1390  c->max_b_frames = s->max_b_frames;
1391 
1392  ret = avcodec_open2(c, s->avctx->codec, NULL);
1393  if (ret < 0)
1394  goto fail;
1395 
1396 
1397  s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1398  s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1399 
1400  out_size = encode_frame(c, s->tmp_frames[0], pkt);
1401  if (out_size < 0) {
1402  ret = out_size;
1403  goto fail;
1404  }
1405 
1406  //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1407 
1408  for (i = 0; i < s->max_b_frames + 1; i++) {
1409  int is_p = i % (j + 1) == j || i == s->max_b_frames;
1410 
1411  s->tmp_frames[i + 1]->pict_type = is_p ?
1413  s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1414 
1415  out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
1416  if (out_size < 0) {
1417  ret = out_size;
1418  goto fail;
1419  }
1420 
1421  rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1422  }
1423 
1424  /* get the delayed frames */
1426  if (out_size < 0) {
1427  ret = out_size;
1428  goto fail;
1429  }
1430  rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1431 
1432  rd += c->error[0] + c->error[1] + c->error[2];
1433 
1434  if (rd < best_rd) {
1435  best_rd = rd;
1436  best_b_count = j;
1437  }
1438 
1439 fail:
1442  if (ret < 0) {
1443  best_b_count = ret;
1444  break;
1445  }
1446  }
1447 
1448  av_packet_free(&pkt);
1449 
1450  return best_b_count;
1451 }
1452 
1454 {
1455  int i, ret;
1456 
1457  for (i = 1; i < MAX_PICTURE_COUNT; i++)
1458  s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1459  s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1460 
1461  /* set next picture type & ordering */
1462  if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1463  if (s->frame_skip_threshold || s->frame_skip_factor) {
1464  if (s->picture_in_gop_number < s->gop_size &&
1465  s->next_picture_ptr &&
1466  skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1467  // FIXME check that the gop check above is +-1 correct
1468  av_frame_unref(s->input_picture[0]->f);
1469 
1470  ff_vbv_update(s, 0);
1471 
1472  goto no_output_pic;
1473  }
1474  }
1475 
1476  if (/*s->picture_in_gop_number >= s->gop_size ||*/
1477  !s->next_picture_ptr || s->intra_only) {
1478  s->reordered_input_picture[0] = s->input_picture[0];
1479  s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1480  s->reordered_input_picture[0]->f->coded_picture_number =
1481  s->coded_picture_number++;
1482  } else {
1483  int b_frames = 0;
1484 
1485  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
1486  for (i = 0; i < s->max_b_frames + 1; i++) {
1487  int pict_num = s->input_picture[0]->f->display_picture_number + i;
1488 
1489  if (pict_num >= s->rc_context.num_entries)
1490  break;
1491  if (!s->input_picture[i]) {
1492  s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1493  break;
1494  }
1495 
1496  s->input_picture[i]->f->pict_type =
1497  s->rc_context.entry[pict_num].new_pict_type;
1498  }
1499  }
1500 
1501  if (s->b_frame_strategy == 0) {
1502  b_frames = s->max_b_frames;
1503  while (b_frames && !s->input_picture[b_frames])
1504  b_frames--;
1505  } else if (s->b_frame_strategy == 1) {
1506  for (i = 1; i < s->max_b_frames + 1; i++) {
1507  if (s->input_picture[i] &&
1508  s->input_picture[i]->b_frame_score == 0) {
1509  s->input_picture[i]->b_frame_score =
1511  s->input_picture[i ]->f->data[0],
1512  s->input_picture[i - 1]->f->data[0],
1513  s->linesize) + 1;
1514  }
1515  }
1516  for (i = 0; i < s->max_b_frames + 1; i++) {
1517  if (!s->input_picture[i] ||
1518  s->input_picture[i]->b_frame_score - 1 >
1519  s->mb_num / s->b_sensitivity)
1520  break;
1521  }
1522 
1523  b_frames = FFMAX(0, i - 1);
1524 
1525  /* reset scores */
1526  for (i = 0; i < b_frames + 1; i++) {
1527  s->input_picture[i]->b_frame_score = 0;
1528  }
1529  } else if (s->b_frame_strategy == 2) {
1530  b_frames = estimate_best_b_count(s);
1531  if (b_frames < 0)
1532  return b_frames;
1533  }
1534 
1535  emms_c();
1536 
1537  for (i = b_frames - 1; i >= 0; i--) {
1538  int type = s->input_picture[i]->f->pict_type;
1539  if (type && type != AV_PICTURE_TYPE_B)
1540  b_frames = i;
1541  }
1542  if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1543  b_frames == s->max_b_frames) {
1544  av_log(s->avctx, AV_LOG_ERROR,
1545  "warning, too many B-frames in a row\n");
1546  }
1547 
1548  if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1549  if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1550  s->gop_size > s->picture_in_gop_number) {
1551  b_frames = s->gop_size - s->picture_in_gop_number - 1;
1552  } else {
1553  if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1554  b_frames = 0;
1555  s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1556  }
1557  }
1558 
1559  if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1560  s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1561  b_frames--;
1562 
1563  s->reordered_input_picture[0] = s->input_picture[b_frames];
1564  if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1565  s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1566  s->reordered_input_picture[0]->f->coded_picture_number =
1567  s->coded_picture_number++;
1568  for (i = 0; i < b_frames; i++) {
1569  s->reordered_input_picture[i + 1] = s->input_picture[i];
1570  s->reordered_input_picture[i + 1]->f->pict_type =
1572  s->reordered_input_picture[i + 1]->f->coded_picture_number =
1573  s->coded_picture_number++;
1574  }
1575  }
1576  }
1577 no_output_pic:
1578  av_frame_unref(s->new_picture);
1579 
1580  if (s->reordered_input_picture[0]) {
1581  s->reordered_input_picture[0]->reference =
1582  s->reordered_input_picture[0]->f->pict_type !=
1583  AV_PICTURE_TYPE_B ? 3 : 0;
1584 
1585  if ((ret = av_frame_ref(s->new_picture,
1586  s->reordered_input_picture[0]->f)))
1587  return ret;
1588 
1589  if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1590  // input is a shared pix, so we can't modify it -> allocate a new
1591  // one & ensure that the shared one is reuseable
1592 
1593  Picture *pic;
1594  int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1595  if (i < 0)
1596  return i;
1597  pic = &s->picture[i];
1598 
1599  pic->reference = s->reordered_input_picture[0]->reference;
1600  if (alloc_picture(s, pic, 0) < 0) {
1601  return -1;
1602  }
1603 
1604  ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1605  if (ret < 0)
1606  return ret;
1607 
1608  /* mark us unused / free shared pic */
1609  av_frame_unref(s->reordered_input_picture[0]->f);
1610  s->reordered_input_picture[0]->shared = 0;
1611 
1612  s->current_picture_ptr = pic;
1613  } else {
1614  // input is not a shared pix -> reuse buffer for current_pix
1615  s->current_picture_ptr = s->reordered_input_picture[0];
1616  for (i = 0; i < 4; i++) {
1617  if (s->new_picture->data[i])
1618  s->new_picture->data[i] += INPLACE_OFFSET;
1619  }
1620  }
1621  s->picture_number = s->new_picture->display_picture_number;
1622  }
1623  return 0;
1624 }
1625 
1627 {
1628  if (s->unrestricted_mv &&
1629  s->current_picture.reference &&
1630  !s->intra_only) {
1631  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1632  int hshift = desc->log2_chroma_w;
1633  int vshift = desc->log2_chroma_h;
1634  s->mpvencdsp.draw_edges(s->current_picture.f->data[0],
1635  s->current_picture.f->linesize[0],
1636  s->h_edge_pos, s->v_edge_pos,
1638  EDGE_TOP | EDGE_BOTTOM);
1639  s->mpvencdsp.draw_edges(s->current_picture.f->data[1],
1640  s->current_picture.f->linesize[1],
1641  s->h_edge_pos >> hshift,
1642  s->v_edge_pos >> vshift,
1643  EDGE_WIDTH >> hshift,
1644  EDGE_WIDTH >> vshift,
1645  EDGE_TOP | EDGE_BOTTOM);
1646  s->mpvencdsp.draw_edges(s->current_picture.f->data[2],
1647  s->current_picture.f->linesize[2],
1648  s->h_edge_pos >> hshift,
1649  s->v_edge_pos >> vshift,
1650  EDGE_WIDTH >> hshift,
1651  EDGE_WIDTH >> vshift,
1652  EDGE_TOP | EDGE_BOTTOM);
1653  }
1654 
1655  emms_c();
1656 
1657  s->last_pict_type = s->pict_type;
1658  s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1659  if (s->pict_type!= AV_PICTURE_TYPE_B)
1660  s->last_non_b_pict_type = s->pict_type;
1661 }
1662 
1664 {
1665  int intra, i;
1666 
1667  for (intra = 0; intra < 2; intra++) {
1668  if (s->dct_count[intra] > (1 << 16)) {
1669  for (i = 0; i < 64; i++) {
1670  s->dct_error_sum[intra][i] >>= 1;
1671  }
1672  s->dct_count[intra] >>= 1;
1673  }
1674 
1675  for (i = 0; i < 64; i++) {
1676  s->dct_offset[intra][i] = (s->noise_reduction *
1677  s->dct_count[intra] +
1678  s->dct_error_sum[intra][i] / 2) /
1679  (s->dct_error_sum[intra][i] + 1);
1680  }
1681  }
1682 }
1683 
1685 {
1686  int ret;
1687 
1688  /* mark & release old frames */
1689  if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1690  s->last_picture_ptr != s->next_picture_ptr &&
1691  s->last_picture_ptr->f->buf[0]) {
1692  ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1693  }
1694 
1695  s->current_picture_ptr->f->pict_type = s->pict_type;
1696  s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1697 
1698  ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1699  if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1700  s->current_picture_ptr)) < 0)
1701  return ret;
1702 
1703  if (s->pict_type != AV_PICTURE_TYPE_B) {
1704  s->last_picture_ptr = s->next_picture_ptr;
1705  s->next_picture_ptr = s->current_picture_ptr;
1706  }
1707 
1708  if (s->last_picture_ptr) {
1709  ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1710  if (s->last_picture_ptr->f->buf[0] &&
1711  (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1712  s->last_picture_ptr)) < 0)
1713  return ret;
1714  }
1715  if (s->next_picture_ptr) {
1716  ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1717  if (s->next_picture_ptr->f->buf[0] &&
1718  (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1719  s->next_picture_ptr)) < 0)
1720  return ret;
1721  }
1722 
1723  if (s->picture_structure!= PICT_FRAME) {
1724  int i;
1725  for (i = 0; i < 4; i++) {
1726  if (s->picture_structure == PICT_BOTTOM_FIELD) {
1727  s->current_picture.f->data[i] +=
1728  s->current_picture.f->linesize[i];
1729  }
1730  s->current_picture.f->linesize[i] *= 2;
1731  s->last_picture.f->linesize[i] *= 2;
1732  s->next_picture.f->linesize[i] *= 2;
1733  }
1734  }
1735 
1736  if (s->dct_error_sum) {
1737  av_assert2(s->noise_reduction && s->encoding);
1739  }
1740 
1741  return 0;
1742 }
1743 
1745  const AVFrame *pic_arg, int *got_packet)
1746 {
1748  int i, stuffing_count, ret;
1749  int context_count = s->slice_context_count;
1750 
1751  s->vbv_ignore_qmax = 0;
1752 
1753  s->picture_in_gop_number++;
1754 
1755  if (load_input_picture(s, pic_arg) < 0)
1756  return -1;
1757 
1758  if (select_input_picture(s) < 0) {
1759  return -1;
1760  }
1761 
1762  /* output? */
1763  if (s->new_picture->data[0]) {
1764  int growing_buffer = context_count == 1 && !s->data_partitioning;
1765  size_t pkt_size = 10000 + s->mb_width * s->mb_height *
1766  (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
1767  if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
1768  ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_picture, &pkt_size);
1769  if (ret < 0)
1770  return ret;
1771  }
1772  if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
1773  return ret;
1775  if (s->mb_info) {
1776  s->mb_info_ptr = av_packet_new_side_data(pkt,
1778  s->mb_width*s->mb_height*12);
1779  s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1780  }
1781 
1782  for (i = 0; i < context_count; i++) {
1783  int start_y = s->thread_context[i]->start_mb_y;
1784  int end_y = s->thread_context[i]-> end_mb_y;
1785  int h = s->mb_height;
1786  uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1787  uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1788 
1789  init_put_bits(&s->thread_context[i]->pb, start, end - start);
1790  }
1791 
1792  s->pict_type = s->new_picture->pict_type;
1793  //emms_c();
1794  ret = frame_start(s);
1795  if (ret < 0)
1796  return ret;
1797 vbv_retry:
1798  ret = encode_picture(s, s->picture_number);
1799  if (growing_buffer) {
1800  av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1801  pkt->data = s->pb.buf;
1803  }
1804  if (ret < 0)
1805  return -1;
1806 
1807  frame_end(s);
1808 
1809  if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->out_format == FMT_MJPEG)
1810  ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1811 
1812  if (avctx->rc_buffer_size) {
1813  RateControlContext *rcc = &s->rc_context;
1814  int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
1815  int hq = (avctx->mb_decision == FF_MB_DECISION_RD || avctx->trellis);
1816  int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1817 
1818  if (put_bits_count(&s->pb) > max_size &&
1819  s->lambda < s->lmax) {
1820  s->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1821  (s->qscale + 1) / s->qscale);
1822  if (s->adaptive_quant) {
1823  int i;
1824  for (i = 0; i < s->mb_height * s->mb_stride; i++)
1825  s->lambda_table[i] =
1826  FFMAX(s->lambda_table[i] + min_step,
1827  s->lambda_table[i] * (s->qscale + 1) /
1828  s->qscale);
1829  }
1830  s->mb_skipped = 0; // done in frame_start()
1831  // done in encode_picture() so we must undo it
1832  if (s->pict_type == AV_PICTURE_TYPE_P) {
1833  if (s->flipflop_rounding ||
1834  s->codec_id == AV_CODEC_ID_H263P ||
1835  s->codec_id == AV_CODEC_ID_MPEG4)
1836  s->no_rounding ^= 1;
1837  }
1838  if (s->pict_type != AV_PICTURE_TYPE_B) {
1839  s->time_base = s->last_time_base;
1840  s->last_non_b_time = s->time - s->pp_time;
1841  }
1842  for (i = 0; i < context_count; i++) {
1843  PutBitContext *pb = &s->thread_context[i]->pb;
1844  init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1845  }
1846  s->vbv_ignore_qmax = 1;
1847  av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
1848  goto vbv_retry;
1849  }
1850 
1852  }
1853 
1856 
1857  for (i = 0; i < 4; i++) {
1858  avctx->error[i] += s->encoding_error[i];
1859  }
1860  ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
1861  s->encoding_error,
1863  s->pict_type);
1864 
1866  assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
1867  s->misc_bits + s->i_tex_bits +
1868  s->p_tex_bits);
1869  flush_put_bits(&s->pb);
1870  s->frame_bits = put_bits_count(&s->pb);
1871 
1872  stuffing_count = ff_vbv_update(s, s->frame_bits);
1873  s->stuffing_bits = 8*stuffing_count;
1874  if (stuffing_count) {
1875  if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
1876  av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
1877  return -1;
1878  }
1879 
1880  switch (s->codec_id) {
1883  while (stuffing_count--) {
1884  put_bits(&s->pb, 8, 0);
1885  }
1886  break;
1887  case AV_CODEC_ID_MPEG4:
1888  put_bits(&s->pb, 16, 0);
1889  put_bits(&s->pb, 16, 0x1C3);
1890  stuffing_count -= 4;
1891  while (stuffing_count--) {
1892  put_bits(&s->pb, 8, 0xFF);
1893  }
1894  break;
1895  default:
1896  av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1897  s->stuffing_bits = 0;
1898  }
1899  flush_put_bits(&s->pb);
1900  s->frame_bits = put_bits_count(&s->pb);
1901  }
1902 
1903  /* update MPEG-1/2 vbv_delay for CBR */
1904  if (avctx->rc_max_rate &&
1906  s->out_format == FMT_MPEG1 &&
1907  90000LL * (avctx->rc_buffer_size - 1) <=
1908  avctx->rc_max_rate * 0xFFFFLL) {
1909  AVCPBProperties *props;
1910  size_t props_size;
1911 
1912  int vbv_delay, min_delay;
1913  double inbits = avctx->rc_max_rate *
1915  int minbits = s->frame_bits - 8 *
1916  (s->vbv_delay_pos - 1);
1917  double bits = s->rc_context.buffer_index + minbits - inbits;
1918  uint8_t *const vbv_delay_ptr = s->pb.buf + s->vbv_delay_pos;
1919 
1920  if (bits < 0)
1922  "Internal error, negative bits\n");
1923 
1924  av_assert1(s->repeat_first_field == 0);
1925 
1926  vbv_delay = bits * 90000 / avctx->rc_max_rate;
1927  min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
1928  avctx->rc_max_rate;
1929 
1930  vbv_delay = FFMAX(vbv_delay, min_delay);
1931 
1932  av_assert0(vbv_delay < 0xFFFF);
1933 
1934  vbv_delay_ptr[0] &= 0xF8;
1935  vbv_delay_ptr[0] |= vbv_delay >> 13;
1936  vbv_delay_ptr[1] = vbv_delay >> 5;
1937  vbv_delay_ptr[2] &= 0x07;
1938  vbv_delay_ptr[2] |= vbv_delay << 3;
1939 
1940  props = av_cpb_properties_alloc(&props_size);
1941  if (!props)
1942  return AVERROR(ENOMEM);
1943  props->vbv_delay = vbv_delay * 300;
1944 
1946  (uint8_t*)props, props_size);
1947  if (ret < 0) {
1948  av_freep(&props);
1949  return ret;
1950  }
1951  }
1952  s->total_bits += s->frame_bits;
1953 
1954  pkt->pts = s->current_picture.f->pts;
1955  if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1956  if (!s->current_picture.f->coded_picture_number)
1957  pkt->dts = pkt->pts - s->dts_delta;
1958  else
1959  pkt->dts = s->reordered_pts;
1960  s->reordered_pts = pkt->pts;
1961  } else
1962  pkt->dts = pkt->pts;
1963  if (s->current_picture.f->key_frame)
1965  if (s->mb_info)
1967  } else {
1968  s->frame_bits = 0;
1969  }
1970 
1971  /* release non-reference frames */
1972  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1973  if (!s->picture[i].reference)
1974  ff_mpeg_unref_picture(avctx, &s->picture[i]);
1975  }
1976 
1977  av_assert1((s->frame_bits & 7) == 0);
1978 
1979  pkt->size = s->frame_bits / 8;
1980  *got_packet = !!pkt->size;
1981  return 0;
1982 }
1983 
1985  int n, int threshold)
1986 {
1987  static const char tab[64] = {
1988  3, 2, 2, 1, 1, 1, 1, 1,
1989  1, 1, 1, 1, 1, 1, 1, 1,
1990  1, 1, 1, 1, 1, 1, 1, 1,
1991  0, 0, 0, 0, 0, 0, 0, 0,
1992  0, 0, 0, 0, 0, 0, 0, 0,
1993  0, 0, 0, 0, 0, 0, 0, 0,
1994  0, 0, 0, 0, 0, 0, 0, 0,
1995  0, 0, 0, 0, 0, 0, 0, 0
1996  };
1997  int score = 0;
1998  int run = 0;
1999  int i;
2000  int16_t *block = s->block[n];
2001  const int last_index = s->block_last_index[n];
2002  int skip_dc;
2003 
2004  if (threshold < 0) {
2005  skip_dc = 0;
2006  threshold = -threshold;
2007  } else
2008  skip_dc = 1;
2009 
2010  /* Are all we could set to zero already zero? */
2011  if (last_index <= skip_dc - 1)
2012  return;
2013 
2014  for (i = 0; i <= last_index; i++) {
2015  const int j = s->intra_scantable.permutated[i];
2016  const int level = FFABS(block[j]);
2017  if (level == 1) {
2018  if (skip_dc && i == 0)
2019  continue;
2020  score += tab[run];
2021  run = 0;
2022  } else if (level > 1) {
2023  return;
2024  } else {
2025  run++;
2026  }
2027  }
2028  if (score >= threshold)
2029  return;
2030  for (i = skip_dc; i <= last_index; i++) {
2031  const int j = s->intra_scantable.permutated[i];
2032  block[j] = 0;
2033  }
2034  if (block[0])
2035  s->block_last_index[n] = 0;
2036  else
2037  s->block_last_index[n] = -1;
2038 }
2039 
2040 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
2041  int last_index)
2042 {
2043  int i;
2044  const int maxlevel = s->max_qcoeff;
2045  const int minlevel = s->min_qcoeff;
2046  int overflow = 0;
2047 
2048  if (s->mb_intra) {
2049  i = 1; // skip clipping of intra dc
2050  } else
2051  i = 0;
2052 
2053  for (; i <= last_index; i++) {
2054  const int j = s->intra_scantable.permutated[i];
2055  int level = block[j];
2056 
2057  if (level > maxlevel) {
2058  level = maxlevel;
2059  overflow++;
2060  } else if (level < minlevel) {
2061  level = minlevel;
2062  overflow++;
2063  }
2064 
2065  block[j] = level;
2066  }
2067 
2068  if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2069  av_log(s->avctx, AV_LOG_INFO,
2070  "warning, clipping %d dct coefficients to %d..%d\n",
2071  overflow, minlevel, maxlevel);
2072 }
2073 
2074 static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
2075 {
2076  int x, y;
2077  // FIXME optimize
2078  for (y = 0; y < 8; y++) {
2079  for (x = 0; x < 8; x++) {
2080  int x2, y2;
2081  int sum = 0;
2082  int sqr = 0;
2083  int count = 0;
2084 
2085  for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2086  for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2087  int v = ptr[x2 + y2 * stride];
2088  sum += v;
2089  sqr += v * v;
2090  count++;
2091  }
2092  }
2093  weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2094  }
2095  }
2096 }
2097 
2099  int motion_x, int motion_y,
2100  int mb_block_height,
2101  int mb_block_width,
2102  int mb_block_count,
2103  int chroma_x_shift,
2104  int chroma_y_shift,
2105  int chroma_format)
2106 {
2107 /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
2108  * and neither of these encoders currently supports 444. */
2109 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2110  (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2111  int16_t weight[12][64];
2112  int16_t orig[12][64];
2113  const int mb_x = s->mb_x;
2114  const int mb_y = s->mb_y;
2115  int i;
2116  int skip_dct[12];
2117  int dct_offset = s->linesize * 8; // default for progressive frames
2118  int uv_dct_offset = s->uvlinesize * 8;
2119  const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2120  ptrdiff_t wrap_y, wrap_c;
2121 
2122  for (i = 0; i < mb_block_count; i++)
2123  skip_dct[i] = s->skipdct;
2124 
2125  if (s->adaptive_quant) {
2126  const int last_qp = s->qscale;
2127  const int mb_xy = mb_x + mb_y * s->mb_stride;
2128 
2129  s->lambda = s->lambda_table[mb_xy];
2130  update_qscale(s);
2131 
2132  if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2133  s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2134  s->dquant = s->qscale - last_qp;
2135 
2136  if (s->out_format == FMT_H263) {
2137  s->dquant = av_clip(s->dquant, -2, 2);
2138 
2139  if (s->codec_id == AV_CODEC_ID_MPEG4) {
2140  if (!s->mb_intra) {
2141  if (s->pict_type == AV_PICTURE_TYPE_B) {
2142  if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2143  s->dquant = 0;
2144  }
2145  if (s->mv_type == MV_TYPE_8X8)
2146  s->dquant = 0;
2147  }
2148  }
2149  }
2150  }
2151  ff_set_qscale(s, last_qp + s->dquant);
2152  } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2153  ff_set_qscale(s, s->qscale + s->dquant);
2154 
2155  wrap_y = s->linesize;
2156  wrap_c = s->uvlinesize;
2157  ptr_y = s->new_picture->data[0] +
2158  (mb_y * 16 * wrap_y) + mb_x * 16;
2159  ptr_cb = s->new_picture->data[1] +
2160  (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2161  ptr_cr = s->new_picture->data[2] +
2162  (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2163 
2164  if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
2165  uint8_t *ebuf = s->sc.edge_emu_buffer + 38 * wrap_y;
2166  int cw = (s->width + chroma_x_shift) >> chroma_x_shift;
2167  int ch = (s->height + chroma_y_shift) >> chroma_y_shift;
2168  s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2169  wrap_y, wrap_y,
2170  16, 16, mb_x * 16, mb_y * 16,
2171  s->width, s->height);
2172  ptr_y = ebuf;
2173  s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2174  wrap_c, wrap_c,
2175  mb_block_width, mb_block_height,
2176  mb_x * mb_block_width, mb_y * mb_block_height,
2177  cw, ch);
2178  ptr_cb = ebuf + 16 * wrap_y;
2179  s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2180  wrap_c, wrap_c,
2181  mb_block_width, mb_block_height,
2182  mb_x * mb_block_width, mb_y * mb_block_height,
2183  cw, ch);
2184  ptr_cr = ebuf + 16 * wrap_y + 16;
2185  }
2186 
2187  if (s->mb_intra) {
2188  if (INTERLACED_DCT(s)) {
2189  int progressive_score, interlaced_score;
2190 
2191  s->interlaced_dct = 0;
2192  progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2193  s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2194  NULL, wrap_y, 8) - 400;
2195 
2196  if (progressive_score > 0) {
2197  interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2198  NULL, wrap_y * 2, 8) +
2199  s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2200  NULL, wrap_y * 2, 8);
2201  if (progressive_score > interlaced_score) {
2202  s->interlaced_dct = 1;
2203 
2204  dct_offset = wrap_y;
2205  uv_dct_offset = wrap_c;
2206  wrap_y <<= 1;
2207  if (chroma_format == CHROMA_422 ||
2209  wrap_c <<= 1;
2210  }
2211  }
2212  }
2213 
2214  s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2215  s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2216  s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2217  s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2218 
2219  if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2220  skip_dct[4] = 1;
2221  skip_dct[5] = 1;
2222  } else {
2223  s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2224  s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2225  if (chroma_format == CHROMA_422) {
2226  s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2227  s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2228  } else if (chroma_format == CHROMA_444) {
2229  s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2230  s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2231  s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2232  s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2233  s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2234  s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2235  }
2236  }
2237  } else {
2238  op_pixels_func (*op_pix)[4];
2239  qpel_mc_func (*op_qpix)[16];
2240  uint8_t *dest_y, *dest_cb, *dest_cr;
2241 
2242  dest_y = s->dest[0];
2243  dest_cb = s->dest[1];
2244  dest_cr = s->dest[2];
2245 
2246  if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2247  op_pix = s->hdsp.put_pixels_tab;
2248  op_qpix = s->qdsp.put_qpel_pixels_tab;
2249  } else {
2250  op_pix = s->hdsp.put_no_rnd_pixels_tab;
2251  op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2252  }
2253 
2254  if (s->mv_dir & MV_DIR_FORWARD) {
2255  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2256  s->last_picture.f->data,
2257  op_pix, op_qpix);
2258  op_pix = s->hdsp.avg_pixels_tab;
2259  op_qpix = s->qdsp.avg_qpel_pixels_tab;
2260  }
2261  if (s->mv_dir & MV_DIR_BACKWARD) {
2262  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2263  s->next_picture.f->data,
2264  op_pix, op_qpix);
2265  }
2266 
2267  if (INTERLACED_DCT(s)) {
2268  int progressive_score, interlaced_score;
2269 
2270  s->interlaced_dct = 0;
2271  progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2272  s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2273  ptr_y + wrap_y * 8,
2274  wrap_y, 8) - 400;
2275 
2276  if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2277  progressive_score -= 400;
2278 
2279  if (progressive_score > 0) {
2280  interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2281  wrap_y * 2, 8) +
2282  s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2283  ptr_y + wrap_y,
2284  wrap_y * 2, 8);
2285 
2286  if (progressive_score > interlaced_score) {
2287  s->interlaced_dct = 1;
2288 
2289  dct_offset = wrap_y;
2290  uv_dct_offset = wrap_c;
2291  wrap_y <<= 1;
2292  if (chroma_format == CHROMA_422)
2293  wrap_c <<= 1;
2294  }
2295  }
2296  }
2297 
2298  s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2299  s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2300  s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2301  dest_y + dct_offset, wrap_y);
2302  s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2303  dest_y + dct_offset + 8, wrap_y);
2304 
2305  if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2306  skip_dct[4] = 1;
2307  skip_dct[5] = 1;
2308  } else {
2309  s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2310  s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2311  if (!chroma_y_shift) { /* 422 */
2312  s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2313  dest_cb + uv_dct_offset, wrap_c);
2314  s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2315  dest_cr + uv_dct_offset, wrap_c);
2316  }
2317  }
2318  /* pre quantization */
2319  if (s->mc_mb_var[s->mb_stride * mb_y + mb_x] < 2 * s->qscale * s->qscale) {
2320  // FIXME optimize
2321  if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2322  skip_dct[0] = 1;
2323  if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2324  skip_dct[1] = 1;
2325  if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2326  wrap_y, 8) < 20 * s->qscale)
2327  skip_dct[2] = 1;
2328  if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2329  wrap_y, 8) < 20 * s->qscale)
2330  skip_dct[3] = 1;
2331  if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2332  skip_dct[4] = 1;
2333  if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2334  skip_dct[5] = 1;
2335  if (!chroma_y_shift) { /* 422 */
2336  if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
2337  dest_cb + uv_dct_offset,
2338  wrap_c, 8) < 20 * s->qscale)
2339  skip_dct[6] = 1;
2340  if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
2341  dest_cr + uv_dct_offset,
2342  wrap_c, 8) < 20 * s->qscale)
2343  skip_dct[7] = 1;
2344  }
2345  }
2346  }
2347 
2348  if (s->quantizer_noise_shaping) {
2349  if (!skip_dct[0])
2350  get_visual_weight(weight[0], ptr_y , wrap_y);
2351  if (!skip_dct[1])
2352  get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2353  if (!skip_dct[2])
2354  get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2355  if (!skip_dct[3])
2356  get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2357  if (!skip_dct[4])
2358  get_visual_weight(weight[4], ptr_cb , wrap_c);
2359  if (!skip_dct[5])
2360  get_visual_weight(weight[5], ptr_cr , wrap_c);
2361  if (!chroma_y_shift) { /* 422 */
2362  if (!skip_dct[6])
2363  get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2364  wrap_c);
2365  if (!skip_dct[7])
2366  get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2367  wrap_c);
2368  }
2369  memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2370  }
2371 
2372  /* DCT & quantize */
2373  av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2374  {
2375  for (i = 0; i < mb_block_count; i++) {
2376  if (!skip_dct[i]) {
2377  int overflow;
2378  s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2379  // FIXME we could decide to change to quantizer instead of
2380  // clipping
2381  // JS: I don't think that would be a good idea it could lower
2382  // quality instead of improve it. Just INTRADC clipping
2383  // deserves changes in quantizer
2384  if (overflow)
2385  clip_coeffs(s, s->block[i], s->block_last_index[i]);
2386  } else
2387  s->block_last_index[i] = -1;
2388  }
2389  if (s->quantizer_noise_shaping) {
2390  for (i = 0; i < mb_block_count; i++) {
2391  if (!skip_dct[i]) {
2392  s->block_last_index[i] =
2393  dct_quantize_refine(s, s->block[i], weight[i],
2394  orig[i], i, s->qscale);
2395  }
2396  }
2397  }
2398 
2399  if (s->luma_elim_threshold && !s->mb_intra)
2400  for (i = 0; i < 4; i++)
2401  dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2402  if (s->chroma_elim_threshold && !s->mb_intra)
2403  for (i = 4; i < mb_block_count; i++)
2404  dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2405 
2406  if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2407  for (i = 0; i < mb_block_count; i++) {
2408  if (s->block_last_index[i] == -1)
2409  s->coded_score[i] = INT_MAX / 256;
2410  }
2411  }
2412  }
2413 
2414  if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
2415  s->block_last_index[4] =
2416  s->block_last_index[5] = 0;
2417  s->block[4][0] =
2418  s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2419  if (!chroma_y_shift) { /* 422 / 444 */
2420  for (i=6; i<12; i++) {
2421  s->block_last_index[i] = 0;
2422  s->block[i][0] = s->block[4][0];
2423  }
2424  }
2425  }
2426 
2427  // non c quantize code returns incorrect block_last_index FIXME
2428  if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2429  for (i = 0; i < mb_block_count; i++) {
2430  int j;
2431  if (s->block_last_index[i] > 0) {
2432  for (j = 63; j > 0; j--) {
2433  if (s->block[i][s->intra_scantable.permutated[j]])
2434  break;
2435  }
2436  s->block_last_index[i] = j;
2437  }
2438  }
2439  }
2440 
2441  /* huffman encode */
2442  switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2445  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2446  ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2447  break;
2448  case AV_CODEC_ID_MPEG4:
2449  if (CONFIG_MPEG4_ENCODER)
2450  ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2451  break;
2452  case AV_CODEC_ID_MSMPEG4V2:
2453  case AV_CODEC_ID_MSMPEG4V3:
2454  case AV_CODEC_ID_WMV1:
2455  if (CONFIG_MSMPEG4ENC)
2456  ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2457  break;
2458  case AV_CODEC_ID_WMV2:
2459  if (CONFIG_WMV2_ENCODER)
2460  ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2461  break;
2462  case AV_CODEC_ID_H261:
2463  if (CONFIG_H261_ENCODER)
2464  ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2465  break;
2466  case AV_CODEC_ID_H263:
2467  case AV_CODEC_ID_H263P:
2468  case AV_CODEC_ID_FLV1:
2469  case AV_CODEC_ID_RV10:
2470  case AV_CODEC_ID_RV20:
2471  if (CONFIG_H263_ENCODER)
2472  ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2473  break;
2474 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2475  case AV_CODEC_ID_MJPEG:
2476  case AV_CODEC_ID_AMV:
2477  ff_mjpeg_encode_mb(s, s->block);
2478  break;
2479 #endif
2480  case AV_CODEC_ID_SPEEDHQ:
2481  if (CONFIG_SPEEDHQ_ENCODER)
2482  ff_speedhq_encode_mb(s, s->block);
2483  break;
2484  default:
2485  av_assert1(0);
2486  }
2487 }
2488 
2489 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2490 {
2491  if (s->chroma_format == CHROMA_420)
2492  encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
2493  else if (s->chroma_format == CHROMA_422)
2494  encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
2495  else
2496  encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
2497 }
2498 
2500  const MpegEncContext *s)
2501 {
2502  int i;
2503 
2504  memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2505 
2506  /* MPEG-1 */
2507  d->mb_skip_run= s->mb_skip_run;
2508  for(i=0; i<3; i++)
2509  d->last_dc[i] = s->last_dc[i];
2510 
2511  /* statistics */
2512  d->mv_bits= s->mv_bits;
2513  d->i_tex_bits= s->i_tex_bits;
2514  d->p_tex_bits= s->p_tex_bits;
2515  d->i_count= s->i_count;
2516  d->skip_count= s->skip_count;
2517  d->misc_bits= s->misc_bits;
2518  d->last_bits= 0;
2519 
2520  d->mb_skipped= 0;
2521  d->qscale= s->qscale;
2522  d->dquant= s->dquant;
2523 
2524  d->esc3_level_length= s->esc3_level_length;
2525 }
2526 
2528  const MpegEncContext *s)
2529 {
2530  int i;
2531 
2532  memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2533  memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2534 
2535  /* MPEG-1 */
2536  d->mb_skip_run= s->mb_skip_run;
2537  for(i=0; i<3; i++)
2538  d->last_dc[i] = s->last_dc[i];
2539 
2540  /* statistics */
2541  d->mv_bits= s->mv_bits;
2542  d->i_tex_bits= s->i_tex_bits;
2543  d->p_tex_bits= s->p_tex_bits;
2544  d->i_count= s->i_count;
2545  d->skip_count= s->skip_count;
2546  d->misc_bits= s->misc_bits;
2547 
2548  d->mb_intra= s->mb_intra;
2549  d->mb_skipped= s->mb_skipped;
2550  d->mv_type= s->mv_type;
2551  d->mv_dir= s->mv_dir;
2552  d->pb= s->pb;
2553  if(s->data_partitioning){
2554  d->pb2= s->pb2;
2555  d->tex_pb= s->tex_pb;
2556  }
2557  d->block= s->block;
2558  for(i=0; i<8; i++)
2559  d->block_last_index[i]= s->block_last_index[i];
2560  d->interlaced_dct= s->interlaced_dct;
2561  d->qscale= s->qscale;
2562 
2563  d->esc3_level_length= s->esc3_level_length;
2564 }
2565 
2566 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best,
2568  int *dmin, int *next_block, int motion_x, int motion_y)
2569 {
2570  int score;
2571  uint8_t *dest_backup[3];
2572 
2573  copy_context_before_encode(s, backup);
2574 
2575  s->block= s->blocks[*next_block];
2576  s->pb= pb[*next_block];
2577  if(s->data_partitioning){
2578  s->pb2 = pb2 [*next_block];
2579  s->tex_pb= tex_pb[*next_block];
2580  }
2581 
2582  if(*next_block){
2583  memcpy(dest_backup, s->dest, sizeof(s->dest));
2584  s->dest[0] = s->sc.rd_scratchpad;
2585  s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2586  s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2587  av_assert0(s->linesize >= 32); //FIXME
2588  }
2589 
2590  encode_mb(s, motion_x, motion_y);
2591 
2592  score= put_bits_count(&s->pb);
2593  if(s->data_partitioning){
2594  score+= put_bits_count(&s->pb2);
2595  score+= put_bits_count(&s->tex_pb);
2596  }
2597 
2598  if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2599  mpv_reconstruct_mb(s, s->block);
2600 
2601  score *= s->lambda2;
2602  score += sse_mb(s) << FF_LAMBDA_SHIFT;
2603  }
2604 
2605  if(*next_block){
2606  memcpy(s->dest, dest_backup, sizeof(s->dest));
2607  }
2608 
2609  if(score<*dmin){
2610  *dmin= score;
2611  *next_block^=1;
2612 
2614  }
2615 }
2616 
2617 static int sse(MpegEncContext *s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride){
2618  const uint32_t *sq = ff_square_tab + 256;
2619  int acc=0;
2620  int x,y;
2621 
2622  if(w==16 && h==16)
2623  return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2624  else if(w==8 && h==8)
2625  return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2626 
2627  for(y=0; y<h; y++){
2628  for(x=0; x<w; x++){
2629  acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2630  }
2631  }
2632 
2633  av_assert2(acc>=0);
2634 
2635  return acc;
2636 }
2637 
2638 static int sse_mb(MpegEncContext *s){
2639  int w= 16;
2640  int h= 16;
2641  int chroma_mb_w = w >> s->chroma_x_shift;
2642  int chroma_mb_h = h >> s->chroma_y_shift;
2643 
2644  if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2645  if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2646 
2647  if(w==16 && h==16)
2648  if(s->avctx->mb_cmp == FF_CMP_NSSE){
2649  return s->mecc.nsse[0](s, s->new_picture->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16,
2650  s->dest[0], s->linesize, 16) +
2651  s->mecc.nsse[1](s, s->new_picture->data[1] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2652  s->dest[1], s->uvlinesize, chroma_mb_h) +
2653  s->mecc.nsse[1](s, s->new_picture->data[2] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2654  s->dest[2], s->uvlinesize, chroma_mb_h);
2655  }else{
2656  return s->mecc.sse[0](NULL, s->new_picture->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16,
2657  s->dest[0], s->linesize, 16) +
2658  s->mecc.sse[1](NULL, s->new_picture->data[1] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2659  s->dest[1], s->uvlinesize, chroma_mb_h) +
2660  s->mecc.sse[1](NULL, s->new_picture->data[2] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2661  s->dest[2], s->uvlinesize, chroma_mb_h);
2662  }
2663  else
2664  return sse(s, s->new_picture->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16,
2665  s->dest[0], w, h, s->linesize) +
2666  sse(s, s->new_picture->data[1] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2667  s->dest[1], w >> s->chroma_x_shift, h >> s->chroma_y_shift, s->uvlinesize) +
2668  sse(s, s->new_picture->data[2] + s->mb_x * chroma_mb_w + s->mb_y * s->uvlinesize * chroma_mb_h,
2669  s->dest[2], w >> s->chroma_x_shift, h >> s->chroma_y_shift, s->uvlinesize);
2670 }
2671 
2673  MpegEncContext *s= *(void**)arg;
2674 
2675 
2676  s->me.pre_pass=1;
2677  s->me.dia_size= s->avctx->pre_dia_size;
2678  s->first_slice_line=1;
2679  for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2680  for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2681  ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2682  }
2683  s->first_slice_line=0;
2684  }
2685 
2686  s->me.pre_pass=0;
2687 
2688  return 0;
2689 }
2690 
2692  MpegEncContext *s= *(void**)arg;
2693 
2694  s->me.dia_size= s->avctx->dia_size;
2695  s->first_slice_line=1;
2696  for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2697  s->mb_x=0; //for block init below
2699  for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2700  s->block_index[0]+=2;
2701  s->block_index[1]+=2;
2702  s->block_index[2]+=2;
2703  s->block_index[3]+=2;
2704 
2705  /* compute motion vector & mb_type and store in context */
2706  if(s->pict_type==AV_PICTURE_TYPE_B)
2707  ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2708  else
2709  ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2710  }
2711  s->first_slice_line=0;
2712  }
2713  return 0;
2714 }
2715 
2716 static int mb_var_thread(AVCodecContext *c, void *arg){
2717  MpegEncContext *s= *(void**)arg;
2718  int mb_x, mb_y;
2719 
2720  for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2721  for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2722  int xx = mb_x * 16;
2723  int yy = mb_y * 16;
2724  const uint8_t *pix = s->new_picture->data[0] + (yy * s->linesize) + xx;
2725  int varc;
2726  int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2727 
2728  varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2729  (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2730 
2731  s->mb_var [s->mb_stride * mb_y + mb_x] = varc;
2732  s->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2733  s->me.mb_var_sum_temp += varc;
2734  }
2735  }
2736  return 0;
2737 }
2738 
2740  if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2741  if(s->partitioned_frame){
2743  }
2744 
2745  ff_mpeg4_stuffing(&s->pb);
2746  } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2747  s->out_format == FMT_MJPEG) {
2749  } else if (CONFIG_SPEEDHQ_ENCODER && s->out_format == FMT_SPEEDHQ) {
2751  }
2752 
2753  flush_put_bits(&s->pb);
2754 
2755  if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2756  s->misc_bits+= get_bits_diff(s);
2757 }
2758 
2760 {
2761  uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2762  int offset = put_bits_count(&s->pb);
2763  int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2764  int gobn = s->mb_y / s->gob_index;
2765  int pred_x, pred_y;
2766  if (CONFIG_H263_ENCODER)
2767  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2768  bytestream_put_le32(&ptr, offset);
2769  bytestream_put_byte(&ptr, s->qscale);
2770  bytestream_put_byte(&ptr, gobn);
2771  bytestream_put_le16(&ptr, mba);
2772  bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2773  bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2774  /* 4MV not implemented */
2775  bytestream_put_byte(&ptr, 0); /* hmv2 */
2776  bytestream_put_byte(&ptr, 0); /* vmv2 */
2777 }
2778 
2779 static void update_mb_info(MpegEncContext *s, int startcode)
2780 {
2781  if (!s->mb_info)
2782  return;
2783  if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2784  s->mb_info_size += 12;
2785  s->prev_mb_info = s->last_mb_info;
2786  }
2787  if (startcode) {
2788  s->prev_mb_info = put_bytes_count(&s->pb, 0);
2789  /* This might have incremented mb_info_size above, and we return without
2790  * actually writing any info into that slot yet. But in that case,
2791  * this will be called again at the start of the after writing the
2792  * start code, actually writing the mb info. */
2793  return;
2794  }
2795 
2796  s->last_mb_info = put_bytes_count(&s->pb, 0);
2797  if (!s->mb_info_size)
2798  s->mb_info_size += 12;
2799  write_mb_info(s);
2800 }
2801 
2802 int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
2803 {
2804  if (put_bytes_left(&s->pb, 0) < threshold
2805  && s->slice_context_count == 1
2806  && s->pb.buf == s->avctx->internal->byte_buffer) {
2807  int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2808 
2809  uint8_t *new_buffer = NULL;
2810  int new_buffer_size = 0;
2811 
2812  if ((s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2813  av_log(s->avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2814  return AVERROR(ENOMEM);
2815  }
2816 
2817  emms_c();
2818 
2819  av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2820  s->avctx->internal->byte_buffer_size + size_increase);
2821  if (!new_buffer)
2822  return AVERROR(ENOMEM);
2823 
2824  memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
2825  av_free(s->avctx->internal->byte_buffer);
2826  s->avctx->internal->byte_buffer = new_buffer;
2827  s->avctx->internal->byte_buffer_size = new_buffer_size;
2828  rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2829  s->ptr_lastgob = s->pb.buf + lastgob_pos;
2830  }
2831  if (put_bytes_left(&s->pb, 0) < threshold)
2832  return AVERROR(EINVAL);
2833  return 0;
2834 }
2835 
2836 static int encode_thread(AVCodecContext *c, void *arg){
2837  MpegEncContext *s= *(void**)arg;
2838  int mb_x, mb_y, mb_y_order;
2839  int chr_h= 16>>s->chroma_y_shift;
2840  int i, j;
2841  MpegEncContext best_s = { 0 }, backup_s;
2842  uint8_t bit_buf[2][MAX_MB_BYTES];
2843  uint8_t bit_buf2[2][MAX_MB_BYTES];
2844  uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2845  PutBitContext pb[2], pb2[2], tex_pb[2];
2846 
2847  for(i=0; i<2; i++){
2848  init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2849  init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2850  init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2851  }
2852 
2853  s->last_bits= put_bits_count(&s->pb);
2854  s->mv_bits=0;
2855  s->misc_bits=0;
2856  s->i_tex_bits=0;
2857  s->p_tex_bits=0;
2858  s->i_count=0;
2859  s->skip_count=0;
2860 
2861  for(i=0; i<3; i++){
2862  /* init last dc values */
2863  /* note: quant matrix value (8) is implied here */
2864  s->last_dc[i] = 128 << s->intra_dc_precision;
2865 
2866  s->encoding_error[i] = 0;
2867  }
2868  if(s->codec_id==AV_CODEC_ID_AMV){
2869  s->last_dc[0] = 128*8/13;
2870  s->last_dc[1] = 128*8/14;
2871  s->last_dc[2] = 128*8/14;
2872  }
2873  s->mb_skip_run = 0;
2874  memset(s->last_mv, 0, sizeof(s->last_mv));
2875 
2876  s->last_mv_dir = 0;
2877 
2878  switch(s->codec_id){
2879  case AV_CODEC_ID_H263:
2880  case AV_CODEC_ID_H263P:
2881  case AV_CODEC_ID_FLV1:
2882  if (CONFIG_H263_ENCODER)
2883  s->gob_index = H263_GOB_HEIGHT(s->height);
2884  break;
2885  case AV_CODEC_ID_MPEG4:
2886  if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2888  break;
2889  }
2890 
2891  s->resync_mb_x=0;
2892  s->resync_mb_y=0;
2893  s->first_slice_line = 1;
2894  s->ptr_lastgob = s->pb.buf;
2895  for (mb_y_order = s->start_mb_y; mb_y_order < s->end_mb_y; mb_y_order++) {
2896  if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
2897  int first_in_slice;
2898  mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->mb_height, &first_in_slice);
2899  if (first_in_slice && mb_y_order != s->start_mb_y)
2901  s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024 << s->intra_dc_precision;
2902  } else {
2903  mb_y = mb_y_order;
2904  }
2905  s->mb_x=0;
2906  s->mb_y= mb_y;
2907 
2908  ff_set_qscale(s, s->qscale);
2910 
2911  for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2912  int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2913  int mb_type= s->mb_type[xy];
2914 // int d;
2915  int dmin= INT_MAX;
2916  int dir;
2917  int size_increase = s->avctx->internal->byte_buffer_size/4
2918  + s->mb_width*MAX_MB_BYTES;
2919 
2921  if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
2922  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2923  return -1;
2924  }
2925  if(s->data_partitioning){
2926  if (put_bytes_left(&s->pb2, 0) < MAX_MB_BYTES ||
2927  put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
2928  av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2929  return -1;
2930  }
2931  }
2932 
2933  s->mb_x = mb_x;
2934  s->mb_y = mb_y; // moved into loop, can get changed by H.261
2935  ff_update_block_index(s, 8, 0, s->chroma_x_shift);
2936 
2937  if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2939  xy= s->mb_y*s->mb_stride + s->mb_x;
2940  mb_type= s->mb_type[xy];
2941  }
2942 
2943  /* write gob / video packet header */
2944  if(s->rtp_mode){
2945  int current_packet_size, is_gob_start;
2946 
2947  current_packet_size = put_bytes_count(&s->pb, 1)
2948  - (s->ptr_lastgob - s->pb.buf);
2949 
2950  is_gob_start = s->rtp_payload_size &&
2951  current_packet_size >= s->rtp_payload_size &&
2952  mb_y + mb_x > 0;
2953 
2954  if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2955 
2956  switch(s->codec_id){
2957  case AV_CODEC_ID_H263:
2958  case AV_CODEC_ID_H263P:
2959  if(!s->h263_slice_structured)
2960  if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2961  break;
2963  if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2965  if(s->mb_skip_run) is_gob_start=0;
2966  break;
2967  case AV_CODEC_ID_MJPEG:
2968  if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2969  break;
2970  }
2971 
2972  if(is_gob_start){
2973  if(s->start_mb_y != mb_y || mb_x!=0){
2974  write_slice_end(s);
2975 
2976  if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2978  }
2979  }
2980 
2981  av_assert2((put_bits_count(&s->pb)&7) == 0);
2982  current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2983 
2984  if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2985  int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->mb_x + s->mb_y;
2986  int d = 100 / s->error_rate;
2987  if(r % d == 0){
2988  current_packet_size=0;
2989  s->pb.buf_ptr= s->ptr_lastgob;
2990  av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2991  }
2992  }
2993 
2994  switch(s->codec_id){
2995  case AV_CODEC_ID_MPEG4:
2996  if (CONFIG_MPEG4_ENCODER) {
2999  }
3000  break;
3003  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3006  }
3007  break;
3008  case AV_CODEC_ID_H263:
3009  case AV_CODEC_ID_H263P:
3010  if (CONFIG_H263_ENCODER) {
3011  update_mb_info(s, 1);
3013  }
3014  break;
3015  }
3016 
3017  if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
3018  int bits= put_bits_count(&s->pb);
3019  s->misc_bits+= bits - s->last_bits;
3020  s->last_bits= bits;
3021  }
3022 
3023  s->ptr_lastgob += current_packet_size;
3024  s->first_slice_line=1;
3025  s->resync_mb_x=mb_x;
3026  s->resync_mb_y=mb_y;
3027  }
3028  }
3029 
3030  if( (s->resync_mb_x == s->mb_x)
3031  && s->resync_mb_y+1 == s->mb_y){
3032  s->first_slice_line=0;
3033  }
3034 
3035  s->mb_skipped=0;
3036  s->dquant=0; //only for QP_RD
3037 
3038  update_mb_info(s, 0);
3039 
3040  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
3041  int next_block=0;
3042  int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3043 
3044  copy_context_before_encode(&backup_s, s);
3045  backup_s.pb= s->pb;
3046  best_s.data_partitioning= s->data_partitioning;
3047  best_s.partitioned_frame= s->partitioned_frame;
3048  if(s->data_partitioning){
3049  backup_s.pb2= s->pb2;
3050  backup_s.tex_pb= s->tex_pb;
3051  }
3052 
3054  s->mv_dir = MV_DIR_FORWARD;
3055  s->mv_type = MV_TYPE_16X16;
3056  s->mb_intra= 0;
3057  s->mv[0][0][0] = s->p_mv_table[xy][0];
3058  s->mv[0][0][1] = s->p_mv_table[xy][1];
3059  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3060  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3061  }
3063  s->mv_dir = MV_DIR_FORWARD;
3064  s->mv_type = MV_TYPE_FIELD;
3065  s->mb_intra= 0;
3066  for(i=0; i<2; i++){
3067  j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3068  s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3069  s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3070  }
3071  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3072  &dmin, &next_block, 0, 0);
3073  }
3075  s->mv_dir = MV_DIR_FORWARD;
3076  s->mv_type = MV_TYPE_16X16;
3077  s->mb_intra= 0;
3078  s->mv[0][0][0] = 0;
3079  s->mv[0][0][1] = 0;
3080  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3081  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3082  }
3084  s->mv_dir = MV_DIR_FORWARD;
3085  s->mv_type = MV_TYPE_8X8;
3086  s->mb_intra= 0;
3087  for(i=0; i<4; i++){
3088  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3089  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3090  }
3091  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3092  &dmin, &next_block, 0, 0);
3093  }
3095  s->mv_dir = MV_DIR_FORWARD;
3096  s->mv_type = MV_TYPE_16X16;
3097  s->mb_intra= 0;
3098  s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3099  s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3100  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3101  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3102  }
3104  s->mv_dir = MV_DIR_BACKWARD;
3105  s->mv_type = MV_TYPE_16X16;
3106  s->mb_intra= 0;
3107  s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3108  s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3109  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3110  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3111  }
3113  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3114  s->mv_type = MV_TYPE_16X16;
3115  s->mb_intra= 0;
3116  s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3117  s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3118  s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3119  s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3120  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3121  &dmin, &next_block, 0, 0);
3122  }
3124  s->mv_dir = MV_DIR_FORWARD;
3125  s->mv_type = MV_TYPE_FIELD;
3126  s->mb_intra= 0;
3127  for(i=0; i<2; i++){
3128  j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3129  s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3130  s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3131  }
3132  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3133  &dmin, &next_block, 0, 0);
3134  }
3136  s->mv_dir = MV_DIR_BACKWARD;
3137  s->mv_type = MV_TYPE_FIELD;
3138  s->mb_intra= 0;
3139  for(i=0; i<2; i++){
3140  j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3141  s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3142  s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3143  }
3144  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3145  &dmin, &next_block, 0, 0);
3146  }
3148  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3149  s->mv_type = MV_TYPE_FIELD;
3150  s->mb_intra= 0;
3151  for(dir=0; dir<2; dir++){
3152  for(i=0; i<2; i++){
3153  j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3154  s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3155  s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3156  }
3157  }
3158  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3159  &dmin, &next_block, 0, 0);
3160  }
3162  s->mv_dir = 0;
3163  s->mv_type = MV_TYPE_16X16;
3164  s->mb_intra= 1;
3165  s->mv[0][0][0] = 0;
3166  s->mv[0][0][1] = 0;
3167  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3168  &dmin, &next_block, 0, 0);
3169  if(s->h263_pred || s->h263_aic){
3170  if(best_s.mb_intra)
3171  s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3172  else
3173  ff_clean_intra_table_entries(s); //old mode?
3174  }
3175  }
3176 
3177  if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3178  if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
3179  const int last_qp= backup_s.qscale;
3180  int qpi, qp, dc[6];
3181  int16_t ac[6][16];
3182  const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3183  static const int dquant_tab[4]={-1,1,-2,2};
3184  int storecoefs = s->mb_intra && s->dc_val[0];
3185 
3186  av_assert2(backup_s.dquant == 0);
3187 
3188  //FIXME intra
3189  s->mv_dir= best_s.mv_dir;
3190  s->mv_type = MV_TYPE_16X16;
3191  s->mb_intra= best_s.mb_intra;
3192  s->mv[0][0][0] = best_s.mv[0][0][0];
3193  s->mv[0][0][1] = best_s.mv[0][0][1];
3194  s->mv[1][0][0] = best_s.mv[1][0][0];
3195  s->mv[1][0][1] = best_s.mv[1][0][1];
3196 
3197  qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3198  for(; qpi<4; qpi++){
3199  int dquant= dquant_tab[qpi];
3200  qp= last_qp + dquant;
3201  if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3202  continue;
3203  backup_s.dquant= dquant;
3204  if(storecoefs){
3205  for(i=0; i<6; i++){
3206  dc[i]= s->dc_val[0][ s->block_index[i] ];
3207  memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3208  }
3209  }
3210 
3211  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3212  &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3213  if(best_s.qscale != qp){
3214  if(storecoefs){
3215  for(i=0; i<6; i++){
3216  s->dc_val[0][ s->block_index[i] ]= dc[i];
3217  memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3218  }
3219  }
3220  }
3221  }
3222  }
3223  }
3224  if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3225  int mx= s->b_direct_mv_table[xy][0];
3226  int my= s->b_direct_mv_table[xy][1];
3227 
3228  backup_s.dquant = 0;
3229  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3230  s->mb_intra= 0;
3231  ff_mpeg4_set_direct_mv(s, mx, my);
3232  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3233  &dmin, &next_block, mx, my);
3234  }
3235  if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3236  backup_s.dquant = 0;
3237  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3238  s->mb_intra= 0;
3239  ff_mpeg4_set_direct_mv(s, 0, 0);
3240  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3241  &dmin, &next_block, 0, 0);
3242  }
3243  if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3244  int coded=0;
3245  for(i=0; i<6; i++)
3246  coded |= s->block_last_index[i];
3247  if(coded){
3248  int mx,my;
3249  memcpy(s->mv, best_s.mv, sizeof(s->mv));
3250  if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3251  mx=my=0; //FIXME find the one we actually used
3252  ff_mpeg4_set_direct_mv(s, mx, my);
3253  }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3254  mx= s->mv[1][0][0];
3255  my= s->mv[1][0][1];
3256  }else{
3257  mx= s->mv[0][0][0];
3258  my= s->mv[0][0][1];
3259  }
3260 
3261  s->mv_dir= best_s.mv_dir;
3262  s->mv_type = best_s.mv_type;
3263  s->mb_intra= 0;
3264 /* s->mv[0][0][0] = best_s.mv[0][0][0];
3265  s->mv[0][0][1] = best_s.mv[0][0][1];
3266  s->mv[1][0][0] = best_s.mv[1][0][0];
3267  s->mv[1][0][1] = best_s.mv[1][0][1];*/
3268  backup_s.dquant= 0;
3269  s->skipdct=1;
3270  encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3271  &dmin, &next_block, mx, my);
3272  s->skipdct=0;
3273  }
3274  }
3275 
3276  s->current_picture.qscale_table[xy] = best_s.qscale;
3277 
3278  copy_context_after_encode(s, &best_s);
3279 
3280  pb_bits_count= put_bits_count(&s->pb);
3281  flush_put_bits(&s->pb);
3282  ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3283  s->pb= backup_s.pb;
3284 
3285  if(s->data_partitioning){
3286  pb2_bits_count= put_bits_count(&s->pb2);
3287  flush_put_bits(&s->pb2);
3288  ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3289  s->pb2= backup_s.pb2;
3290 
3291  tex_pb_bits_count= put_bits_count(&s->tex_pb);
3292  flush_put_bits(&s->tex_pb);
3293  ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3294  s->tex_pb= backup_s.tex_pb;
3295  }
3296  s->last_bits= put_bits_count(&s->pb);
3297 
3298  if (CONFIG_H263_ENCODER &&
3299  s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3301 
3302  if(next_block==0){ //FIXME 16 vs linesize16
3303  s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
3304  s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
3305  s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3306  }
3307 
3308  if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3309  mpv_reconstruct_mb(s, s->block);
3310  } else {
3311  int motion_x = 0, motion_y = 0;
3312  s->mv_type=MV_TYPE_16X16;
3313  // only one MB-Type possible
3314 
3315  switch(mb_type){
3317  s->mv_dir = 0;
3318  s->mb_intra= 1;
3319  motion_x= s->mv[0][0][0] = 0;
3320  motion_y= s->mv[0][0][1] = 0;
3321  break;
3323  s->mv_dir = MV_DIR_FORWARD;
3324  s->mb_intra= 0;
3325  motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3326  motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3327  break;
3329  s->mv_dir = MV_DIR_FORWARD;
3330  s->mv_type = MV_TYPE_FIELD;
3331  s->mb_intra= 0;
3332  for(i=0; i<2; i++){
3333  j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3334  s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3335  s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3336  }
3337  break;
3339  s->mv_dir = MV_DIR_FORWARD;
3340  s->mv_type = MV_TYPE_8X8;
3341  s->mb_intra= 0;
3342  for(i=0; i<4; i++){
3343  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3344  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3345  }
3346  break;
3348  if (CONFIG_MPEG4_ENCODER) {
3350  s->mb_intra= 0;
3351  motion_x=s->b_direct_mv_table[xy][0];
3352  motion_y=s->b_direct_mv_table[xy][1];
3353  ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3354  }
3355  break;
3357  if (CONFIG_MPEG4_ENCODER) {
3359  s->mb_intra= 0;
3360  ff_mpeg4_set_direct_mv(s, 0, 0);
3361  }
3362  break;
3364  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3365  s->mb_intra= 0;
3366  s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3367  s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3368  s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3369  s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3370  break;
3372  s->mv_dir = MV_DIR_BACKWARD;
3373  s->mb_intra= 0;
3374  motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3375  motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3376  break;
3378  s->mv_dir = MV_DIR_FORWARD;
3379  s->mb_intra= 0;
3380  motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3381  motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3382  break;
3384  s->mv_dir = MV_DIR_FORWARD;
3385  s->mv_type = MV_TYPE_FIELD;
3386  s->mb_intra= 0;
3387  for(i=0; i<2; i++){
3388  j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3389  s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3390  s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3391  }
3392  break;
3394  s->mv_dir = MV_DIR_BACKWARD;
3395  s->mv_type = MV_TYPE_FIELD;
3396  s->mb_intra= 0;
3397  for(i=0; i<2; i++){
3398  j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3399  s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3400  s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3401  }
3402  break;
3404  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3405  s->mv_type = MV_TYPE_FIELD;
3406  s->mb_intra= 0;
3407  for(dir=0; dir<2; dir++){
3408  for(i=0; i<2; i++){
3409  j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3410  s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3411  s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3412  }
3413  }
3414  break;
3415  default:
3416  av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3417  }
3418 
3419  encode_mb(s, motion_x, motion_y);
3420 
3421  // RAL: Update last macroblock type
3422  s->last_mv_dir = s->mv_dir;
3423 
3424  if (CONFIG_H263_ENCODER &&
3425  s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3427 
3428  mpv_reconstruct_mb(s, s->block);
3429  }
3430 
3431  /* clean the MV table in IPS frames for direct mode in B-frames */
3432  if(s->mb_intra /* && I,P,S_TYPE */){
3433  s->p_mv_table[xy][0]=0;
3434  s->p_mv_table[xy][1]=0;
3435  }
3436 
3437  if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
3438  int w= 16;
3439  int h= 16;
3440 
3441  if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3442  if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3443 
3444  s->encoding_error[0] += sse(
3445  s, s->new_picture->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3446  s->dest[0], w, h, s->linesize);
3447  s->encoding_error[1] += sse(
3448  s, s->new_picture->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3449  s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3450  s->encoding_error[2] += sse(
3451  s, s->new_picture->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3452  s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3453  }
3454  if(s->loop_filter){
3455  if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3457  }
3458  ff_dlog(s->avctx, "MB %d %d bits\n",
3459  s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3460  }
3461  }
3462 
3463  //not beautiful here but we must write it before flushing so it has to be here
3464  if (CONFIG_MSMPEG4ENC && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3466 
3467  write_slice_end(s);
3468 
3469  return 0;
3470 }
3471 
3472 #define MERGE(field) dst->field += src->field; src->field=0
3474  MERGE(me.scene_change_score);
3475  MERGE(me.mc_mb_var_sum_temp);
3476  MERGE(me.mb_var_sum_temp);
3477 }
3478 
3480  int i;
3481 
3482  MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3483  MERGE(dct_count[1]);
3484  MERGE(mv_bits);
3485  MERGE(i_tex_bits);
3486  MERGE(p_tex_bits);
3487  MERGE(i_count);
3488  MERGE(skip_count);
3489  MERGE(misc_bits);
3490  MERGE(encoding_error[0]);
3491  MERGE(encoding_error[1]);
3492  MERGE(encoding_error[2]);
3493 
3494  if (dst->noise_reduction){
3495  for(i=0; i<64; i++){
3496  MERGE(dct_error_sum[0][i]);
3497  MERGE(dct_error_sum[1][i]);
3498  }
3499  }
3500 
3501  av_assert1(put_bits_count(&src->pb) % 8 ==0);
3502  av_assert1(put_bits_count(&dst->pb) % 8 ==0);
3503  ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3504  flush_put_bits(&dst->pb);
3505 }
3506 
3507 static int estimate_qp(MpegEncContext *s, int dry_run){
3508  if (s->next_lambda){
3509  s->current_picture_ptr->f->quality =
3510  s->current_picture.f->quality = s->next_lambda;
3511  if(!dry_run) s->next_lambda= 0;
3512  } else if (!s->fixed_qscale) {
3513  int quality = ff_rate_estimate_qscale(s, dry_run);
3514  s->current_picture_ptr->f->quality =
3515  s->current_picture.f->quality = quality;
3516  if (s->current_picture.f->quality < 0)
3517  return -1;
3518  }
3519 
3520  if(s->adaptive_quant){
3521  switch(s->codec_id){
3522  case AV_CODEC_ID_MPEG4:
3523  if (CONFIG_MPEG4_ENCODER)
3525  break;
3526  case AV_CODEC_ID_H263:
3527  case AV_CODEC_ID_H263P:
3528  case AV_CODEC_ID_FLV1:
3529  if (CONFIG_H263_ENCODER)
3531  break;
3532  default:
3534  }
3535 
3536  s->lambda= s->lambda_table[0];
3537  //FIXME broken
3538  }else
3539  s->lambda = s->current_picture.f->quality;
3540  update_qscale(s);
3541  return 0;
3542 }
3543 
3544 /* must be called before writing the header */
3546  av_assert1(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3547  s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3548 
3549  if(s->pict_type==AV_PICTURE_TYPE_B){
3550  s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3551  av_assert1(s->pb_time > 0 && s->pb_time < s->pp_time);
3552  }else{
3553  s->pp_time= s->time - s->last_non_b_time;
3554  s->last_non_b_time= s->time;
3555  av_assert1(s->picture_number==0 || s->pp_time > 0);
3556  }
3557 }
3558 
3560 {
3561  int i, ret;
3562  int bits;
3563  int context_count = s->slice_context_count;
3564 
3565  s->picture_number = picture_number;
3566 
3567  /* Reset the average MB variance */
3568  s->me.mb_var_sum_temp =
3569  s->me.mc_mb_var_sum_temp = 0;
3570 
3571  /* we need to initialize some time vars before we can encode B-frames */
3572  // RAL: Condition added for MPEG1VIDEO
3573  if (s->out_format == FMT_MPEG1 || (s->h263_pred && !s->msmpeg4_version))
3575  if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3577 
3578  s->me.scene_change_score=0;
3579 
3580 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3581 
3582  if(s->pict_type==AV_PICTURE_TYPE_I){
3583  if(s->msmpeg4_version >= 3) s->no_rounding=1;
3584  else s->no_rounding=0;
3585  }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3586  if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3587  s->no_rounding ^= 1;
3588  }
3589 
3590  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
3591  if (estimate_qp(s,1) < 0)
3592  return -1;
3594  } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3595  if(s->pict_type==AV_PICTURE_TYPE_B)
3596  s->lambda= s->last_lambda_for[s->pict_type];
3597  else
3598  s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3599  update_qscale(s);
3600  }
3601 
3602  if (s->out_format != FMT_MJPEG) {
3603  if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
3604  if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3605  s->q_chroma_intra_matrix = s->q_intra_matrix;
3606  s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3607  }
3608 
3609  s->mb_intra=0; //for the rate distortion & bit compare functions
3610  for(i=1; i<context_count; i++){
3611  ret = ff_update_duplicate_context(s->thread_context[i], s);
3612  if (ret < 0)
3613  return ret;
3614  }
3615 
3616  if(ff_init_me(s)<0)
3617  return -1;
3618 
3619  /* Estimate motion for every MB */
3620  if(s->pict_type != AV_PICTURE_TYPE_I){
3621  s->lambda = (s->lambda * s->me_penalty_compensation + 128) >> 8;
3622  s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
3623  if (s->pict_type != AV_PICTURE_TYPE_B) {
3624  if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3625  s->me_pre == 2) {
3626  s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3627  }
3628  }
3629 
3630  s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3631  }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3632  /* I-Frame */
3633  for(i=0; i<s->mb_stride*s->mb_height; i++)
3634  s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3635 
3636  if(!s->fixed_qscale){
3637  /* finding spatial complexity for I-frame rate control */
3638  s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3639  }
3640  }
3641  for(i=1; i<context_count; i++){
3642  merge_context_after_me(s, s->thread_context[i]);
3643  }
3644  s->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
3645  s->mb_var_sum = s->me. mb_var_sum_temp;
3646  emms_c();
3647 
3648  if (s->me.scene_change_score > s->scenechange_threshold &&
3649  s->pict_type == AV_PICTURE_TYPE_P) {
3650  s->pict_type= AV_PICTURE_TYPE_I;
3651  for(i=0; i<s->mb_stride*s->mb_height; i++)
3652  s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3653  if(s->msmpeg4_version >= 3)
3654  s->no_rounding=1;
3655  ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3656  s->mb_var_sum, s->mc_mb_var_sum);
3657  }
3658 
3659  if(!s->umvplus){
3660  if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3661  s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3662 
3663  if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3664  int a,b;
3665  a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3666  b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3667  s->f_code= FFMAX3(s->f_code, a, b);
3668  }
3669 
3671  ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
3672  if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3673  int j;
3674  for(i=0; i<2; i++){
3675  for(j=0; j<2; j++)
3676  ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3677  s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
3678  }
3679  }
3680  } else if (s->pict_type == AV_PICTURE_TYPE_B) {
3681  int a, b;
3682 
3683  a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3684  b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3685  s->f_code = FFMAX(a, b);
3686 
3687  a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3688  b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3689  s->b_code = FFMAX(a, b);
3690 
3691  ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3692  ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3693  ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3694  ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3695  if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3696  int dir, j;
3697  for(dir=0; dir<2; dir++){
3698  for(i=0; i<2; i++){
3699  for(j=0; j<2; j++){
3702  ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3703  s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3704  }
3705  }
3706  }
3707  }
3708  }
3709  }
3710 
3711  if (estimate_qp(s, 0) < 0)
3712  return -1;
3713 
3714  if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3715  s->pict_type == AV_PICTURE_TYPE_I &&
3716  !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
3717  s->qscale= 3; //reduce clipping problems
3718 
3719  if (s->out_format == FMT_MJPEG) {
3720  const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3721  const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3722 
3723  if (s->avctx->intra_matrix) {
3724  chroma_matrix =
3725  luma_matrix = s->avctx->intra_matrix;
3726  }
3727  if (s->avctx->chroma_intra_matrix)
3728  chroma_matrix = s->avctx->chroma_intra_matrix;
3729 
3730  /* for mjpeg, we do include qscale in the matrix */
3731  for(i=1;i<64;i++){
3732  int j = s->idsp.idct_permutation[i];
3733 
3734  s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->qscale) >> 3);
3735  s-> intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->qscale) >> 3);
3736  }
3737  s->y_dc_scale_table=
3738  s->c_dc_scale_table = ff_mpeg12_dc_scale_table[s->intra_dc_precision];
3739  s->chroma_intra_matrix[0] =
3740  s->intra_matrix[0] = ff_mpeg12_dc_scale_table[s->intra_dc_precision][8];
3741  ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3742  s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3743  ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3744  s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3745  s->qscale= 8;
3746 
3747  if (s->codec_id == AV_CODEC_ID_AMV) {
3748  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};
3749  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};
3750  for (int i = 1; i < 64; i++) {
3751  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
3752 
3753  s->intra_matrix[j] = sp5x_qscale_five_quant_table[0][i];
3754  s->chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
3755  }
3756  s->y_dc_scale_table = y;
3757  s->c_dc_scale_table = c;
3758  s->intra_matrix[0] = 13;
3759  s->chroma_intra_matrix[0] = 14;
3760  ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3761  s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3762  ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3763  s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3764  s->qscale = 8;
3765  }
3766  }
3767 
3768  //FIXME var duplication
3769  s->current_picture_ptr->f->key_frame =
3770  s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3771  s->current_picture_ptr->f->pict_type =
3772  s->current_picture.f->pict_type = s->pict_type;
3773 
3774  if (s->current_picture.f->key_frame)
3775  s->picture_in_gop_number=0;
3776 
3777  s->mb_x = s->mb_y = 0;
3778  s->last_bits= put_bits_count(&s->pb);
3779  switch(s->out_format) {
3780 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3781  case FMT_MJPEG:
3783  break;
3784 #endif
3785  case FMT_SPEEDHQ:
3786  if (CONFIG_SPEEDHQ_ENCODER)
3788  break;
3789  case FMT_H261:
3790  if (CONFIG_H261_ENCODER)
3792  break;
3793  case FMT_H263:
3794  if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3796  else if (CONFIG_MSMPEG4ENC && s->msmpeg4_version)
3798  else if (CONFIG_MPEG4_ENCODER && s->h263_pred) {
3800  if (ret < 0)
3801  return ret;
3802  } else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3804  if (ret < 0)
3805  return ret;
3806  }
3807  else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3809  else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3811  else if (CONFIG_H263_ENCODER)
3813  break;
3814  case FMT_MPEG1:
3815  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3817  break;
3818  default:
3819  av_assert0(0);
3820  }
3821  bits= put_bits_count(&s->pb);
3822  s->header_bits= bits - s->last_bits;
3823 
3824  for(i=1; i<context_count; i++){
3825  update_duplicate_context_after_me(s->thread_context[i], s);
3826  }
3827  s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3828  for(i=1; i<context_count; i++){
3829  if (s->pb.buf_end == s->thread_context[i]->pb.buf)
3830  set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
3831  merge_context_after_encode(s, s->thread_context[i]);
3832  }
3833  emms_c();
3834  return 0;
3835 }
3836 
3837 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3838  const int intra= s->mb_intra;
3839  int i;
3840 
3841  s->dct_count[intra]++;
3842 
3843  for(i=0; i<64; i++){
3844  int level= block[i];
3845 
3846  if(level){
3847  if(level>0){
3848  s->dct_error_sum[intra][i] += level;
3849  level -= s->dct_offset[intra][i];
3850  if(level<0) level=0;
3851  }else{
3852  s->dct_error_sum[intra][i] -= level;
3853  level += s->dct_offset[intra][i];
3854  if(level>0) level=0;
3855  }
3856  block[i]= level;
3857  }
3858  }
3859 }
3860 
3862  int16_t *block, int n,
3863  int qscale, int *overflow){
3864  const int *qmat;
3865  const uint16_t *matrix;
3866  const uint8_t *scantable;
3867  const uint8_t *perm_scantable;
3868  int max=0;
3869  unsigned int threshold1, threshold2;
3870  int bias=0;
3871  int run_tab[65];
3872  int level_tab[65];
3873  int score_tab[65];
3874  int survivor[65];
3875  int survivor_count;
3876  int last_run=0;
3877  int last_level=0;
3878  int last_score= 0;
3879  int last_i;
3880  int coeff[2][64];
3881  int coeff_count[64];
3882  int qmul, qadd, start_i, last_non_zero, i, dc;
3883  const int esc_length= s->ac_esc_length;
3884  uint8_t * length;
3885  uint8_t * last_length;
3886  const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3887  int mpeg2_qscale;
3888 
3889  s->fdsp.fdct(block);
3890 
3891  if(s->dct_error_sum)
3892  s->denoise_dct(s, block);
3893  qmul= qscale*16;
3894  qadd= ((qscale-1)|1)*8;
3895 
3896  if (s->q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
3897  else mpeg2_qscale = qscale << 1;
3898 
3899  if (s->mb_intra) {
3900  int q;
3901  scantable= s->intra_scantable.scantable;
3902  perm_scantable= s->intra_scantable.permutated;
3903  if (!s->h263_aic) {
3904  if (n < 4)
3905  q = s->y_dc_scale;
3906  else
3907  q = s->c_dc_scale;
3908  q = q << 3;
3909  } else{
3910  /* For AIC we skip quant/dequant of INTRADC */
3911  q = 1 << 3;
3912  qadd=0;
3913  }
3914 
3915  /* note: block[0] is assumed to be positive */
3916  block[0] = (block[0] + (q >> 1)) / q;
3917  start_i = 1;
3918  last_non_zero = 0;
3919  qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3920  matrix = n < 4 ? s->intra_matrix : s->chroma_intra_matrix;
3921  if(s->mpeg_quant || s->out_format == FMT_MPEG1 || s->out_format == FMT_MJPEG)
3922  bias= 1<<(QMAT_SHIFT-1);
3923 
3924  if (n > 3 && s->intra_chroma_ac_vlc_length) {
3925  length = s->intra_chroma_ac_vlc_length;
3926  last_length= s->intra_chroma_ac_vlc_last_length;
3927  } else {
3928  length = s->intra_ac_vlc_length;
3929  last_length= s->intra_ac_vlc_last_length;
3930  }
3931  } else {
3932  scantable= s->inter_scantable.scantable;
3933  perm_scantable= s->inter_scantable.permutated;
3934  start_i = 0;
3935  last_non_zero = -1;
3936  qmat = s->q_inter_matrix[qscale];
3937  matrix = s->inter_matrix;
3938  length = s->inter_ac_vlc_length;
3939  last_length= s->inter_ac_vlc_last_length;
3940  }
3941  last_i= start_i;
3942 
3943  threshold1= (1<<QMAT_SHIFT) - bias - 1;
3944  threshold2= (threshold1<<1);
3945 
3946  for(i=63; i>=start_i; i--) {
3947  const int j = scantable[i];
3948  int level = block[j] * qmat[j];
3949 
3950  if(((unsigned)(level+threshold1))>threshold2){
3951  last_non_zero = i;
3952  break;
3953  }
3954  }
3955 
3956  for(i=start_i; i<=last_non_zero; i++) {
3957  const int j = scantable[i];
3958  int level = block[j] * qmat[j];
3959 
3960 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3961 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3962  if(((unsigned)(level+threshold1))>threshold2){
3963  if(level>0){
3964  level= (bias + level)>>QMAT_SHIFT;
3965  coeff[0][i]= level;
3966  coeff[1][i]= level-1;
3967 // coeff[2][k]= level-2;
3968  }else{
3969  level= (bias - level)>>QMAT_SHIFT;
3970  coeff[0][i]= -level;
3971  coeff[1][i]= -level+1;
3972 // coeff[2][k]= -level+2;
3973  }
3974  coeff_count[i]= FFMIN(level, 2);
3975  av_assert2(coeff_count[i]);
3976  max |=level;
3977  }else{
3978  coeff[0][i]= (level>>31)|1;
3979  coeff_count[i]= 1;
3980  }
3981  }
3982 
3983  *overflow= s->max_qcoeff < max; //overflow might have happened
3984 
3985  if(last_non_zero < start_i){
3986  memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3987  return last_non_zero;
3988  }
3989 
3990  score_tab[start_i]= 0;
3991  survivor[0]= start_i;
3992  survivor_count= 1;
3993 
3994  for(i=start_i; i<=last_non_zero; i++){
3995  int level_index, j, zero_distortion;
3996  int dct_coeff= FFABS(block[ scantable[i] ]);
3997  int best_score=256*256*256*120;
3998 
3999  if (s->fdsp.fdct == ff_fdct_ifast)
4000  dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4001  zero_distortion= dct_coeff*dct_coeff;
4002 
4003  for(level_index=0; level_index < coeff_count[i]; level_index++){
4004  int distortion;
4005  int level= coeff[level_index][i];
4006  const int alevel= FFABS(level);
4007  int unquant_coeff;
4008 
4009  av_assert2(level);
4010 
4011  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4012  unquant_coeff= alevel*qmul + qadd;
4013  } else if(s->out_format == FMT_MJPEG) {
4014  j = s->idsp.idct_permutation[scantable[i]];
4015  unquant_coeff = alevel * matrix[j] * 8;
4016  }else{ // MPEG-1
4017  j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4018  if(s->mb_intra){
4019  unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4020  unquant_coeff = (unquant_coeff - 1) | 1;
4021  }else{
4022  unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4023  unquant_coeff = (unquant_coeff - 1) | 1;
4024  }
4025  unquant_coeff<<= 3;
4026  }
4027 
4028  distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4029  level+=64;
4030  if((level&(~127)) == 0){
4031  for(j=survivor_count-1; j>=0; j--){
4032  int run= i - survivor[j];
4033  int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4034  score += score_tab[i-run];
4035 
4036  if(score < best_score){
4037  best_score= score;
4038  run_tab[i+1]= run;
4039  level_tab[i+1]= level-64;
4040  }
4041  }
4042 
4043  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4044  for(j=survivor_count-1; j>=0; j--){
4045  int run= i - survivor[j];
4046  int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4047  score += score_tab[i-run];
4048  if(score < last_score){
4049  last_score= score;
4050  last_run= run;
4051  last_level= level-64;
4052  last_i= i+1;
4053  }
4054  }
4055  }
4056  }else{
4057  distortion += esc_length*lambda;
4058  for(j=survivor_count-1; j>=0; j--){
4059  int run= i - survivor[j];
4060  int score= distortion + score_tab[i-run];
4061 
4062  if(score < best_score){
4063  best_score= score;
4064  run_tab[i+1]= run;
4065  level_tab[i+1]= level-64;
4066  }
4067  }
4068 
4069  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4070  for(j=survivor_count-1; j>=0; j--){
4071  int run= i - survivor[j];
4072  int score= distortion + score_tab[i-run];
4073  if(score < last_score){
4074  last_score= score;
4075  last_run= run;
4076  last_level= level-64;
4077  last_i= i+1;
4078  }
4079  }
4080  }
4081  }
4082  }
4083 
4084  score_tab[i+1]= best_score;
4085 
4086  // 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
4087  if(last_non_zero <= 27){
4088  for(; survivor_count; survivor_count--){
4089  if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4090  break;
4091  }
4092  }else{
4093  for(; survivor_count; survivor_count--){
4094  if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4095  break;
4096  }
4097  }
4098 
4099  survivor[ survivor_count++ ]= i+1;
4100  }
4101 
4102  if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
4103  last_score= 256*256*256*120;
4104  for(i= survivor[0]; i<=last_non_zero + 1; i++){
4105  int score= score_tab[i];
4106  if (i)
4107  score += lambda * 2; // FIXME more exact?
4108 
4109  if(score < last_score){
4110  last_score= score;
4111  last_i= i;
4112  last_level= level_tab[i];
4113  last_run= run_tab[i];
4114  }
4115  }
4116  }
4117 
4118  s->coded_score[n] = last_score;
4119 
4120  dc= FFABS(block[0]);
4121  last_non_zero= last_i - 1;
4122  memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4123 
4124  if(last_non_zero < start_i)
4125  return last_non_zero;
4126 
4127  if(last_non_zero == 0 && start_i == 0){
4128  int best_level= 0;
4129  int best_score= dc * dc;
4130 
4131  for(i=0; i<coeff_count[0]; i++){
4132  int level= coeff[i][0];
4133  int alevel= FFABS(level);
4134  int unquant_coeff, score, distortion;
4135 
4136  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4137  unquant_coeff= (alevel*qmul + qadd)>>3;
4138  } else{ // MPEG-1
4139  unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4140  unquant_coeff = (unquant_coeff - 1) | 1;
4141  }
4142  unquant_coeff = (unquant_coeff + 4) >> 3;
4143  unquant_coeff<<= 3 + 3;
4144 
4145  distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4146  level+=64;
4147  if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4148  else score= distortion + esc_length*lambda;
4149 
4150  if(score < best_score){
4151  best_score= score;
4152  best_level= level - 64;
4153  }
4154  }
4155  block[0]= best_level;
4156  s->coded_score[n] = best_score - dc*dc;
4157  if(best_level == 0) return -1;
4158  else return last_non_zero;
4159  }
4160 
4161  i= last_i;
4162  av_assert2(last_level);
4163 
4164  block[ perm_scantable[last_non_zero] ]= last_level;
4165  i -= last_run + 1;
4166 
4167  for(; i>start_i; i -= run_tab[i] + 1){
4168  block[ perm_scantable[i-1] ]= level_tab[i];
4169  }
4170 
4171  return last_non_zero;
4172 }
4173 
4174 static int16_t basis[64][64];
4175 
4176 static void build_basis(uint8_t *perm){
4177  int i, j, x, y;
4178  emms_c();
4179  for(i=0; i<8; i++){
4180  for(j=0; j<8; j++){
4181  for(y=0; y<8; y++){
4182  for(x=0; x<8; x++){
4183  double s= 0.25*(1<<BASIS_SHIFT);
4184  int index= 8*i + j;
4185  int perm_index= perm[index];
4186  if(i==0) s*= sqrt(0.5);
4187  if(j==0) s*= sqrt(0.5);
4188  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)));
4189  }
4190  }
4191  }
4192  }
4193 }
4194 
4195 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
4196  int16_t *block, int16_t *weight, int16_t *orig,
4197  int n, int qscale){
4198  int16_t rem[64];
4199  LOCAL_ALIGNED_16(int16_t, d1, [64]);
4200  const uint8_t *scantable;
4201  const uint8_t *perm_scantable;
4202 // unsigned int threshold1, threshold2;
4203 // int bias=0;
4204  int run_tab[65];
4205  int prev_run=0;
4206  int prev_level=0;
4207  int qmul, qadd, start_i, last_non_zero, i, dc;
4208  uint8_t * length;
4209  uint8_t * last_length;
4210  int lambda;
4211  int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
4212 
4213  if(basis[0][0] == 0)
4214  build_basis(s->idsp.idct_permutation);
4215 
4216  qmul= qscale*2;
4217  qadd= (qscale-1)|1;
4218  if (s->mb_intra) {
4219  scantable= s->intra_scantable.scantable;
4220  perm_scantable= s->intra_scantable.permutated;
4221  if (!s->h263_aic) {
4222  if (n < 4)
4223  q = s->y_dc_scale;
4224  else
4225  q = s->c_dc_scale;
4226  } else{
4227  /* For AIC we skip quant/dequant of INTRADC */
4228  q = 1;
4229  qadd=0;
4230  }
4231  q <<= RECON_SHIFT-3;
4232  /* note: block[0] is assumed to be positive */
4233