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