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