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 "mpegvideodata.h"
44 #include "h261.h"
45 #include "h263.h"
46 #include "mjpegenc_common.h"
47 #include "mathops.h"
48 #include "mpegutils.h"
49 #include "mjpegenc.h"
50 #include "msmpeg4.h"
51 #include "pixblockdsp.h"
52 #include "qpeldsp.h"
53 #include "faandct.h"
54 #include "thread.h"
55 #include "aandcttab.h"
56 #include "flv.h"
57 #include "mpeg4video.h"
58 #include "internal.h"
59 #include "bytestream.h"
60 #include "wmv2.h"
61 #include <limits.h>
62 #include "sp5x.h"
63 
64 #define QUANT_BIAS_SHIFT 8
65 
66 #define QMAT_SHIFT_MMX 16
67 #define QMAT_SHIFT 21
68 
70 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
71 static int sse_mb(MpegEncContext *s);
72 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
73 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
74 
77 
80  { NULL },
81 };
82 
83 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
84  uint16_t (*qmat16)[2][64],
85  const uint16_t *quant_matrix,
86  int bias, int qmin, int qmax, int intra)
87 {
88  FDCTDSPContext *fdsp = &s->fdsp;
89  int qscale;
90  int shift = 0;
91 
92  for (qscale = qmin; qscale <= qmax; qscale++) {
93  int i;
94  if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
95 #if CONFIG_FAANDCT
96  fdsp->fdct == ff_faandct ||
97 #endif /* CONFIG_FAANDCT */
98  fdsp->fdct == ff_jpeg_fdct_islow_10) {
99  for (i = 0; i < 64; i++) {
100  const int j = s->idsp.idct_permutation[i];
101  int64_t den = (int64_t) qscale * quant_matrix[j];
102  /* 16 <= qscale * quant_matrix[i] <= 7905
103  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
104  * 19952 <= x <= 249205026
105  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
106  * 3444240 >= (1 << 36) / (x) >= 275 */
107 
108  qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
109  }
110  } else if (fdsp->fdct == ff_fdct_ifast) {
111  for (i = 0; i < 64; i++) {
112  const int j = s->idsp.idct_permutation[i];
113  int64_t den = ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
114  /* 16 <= qscale * quant_matrix[i] <= 7905
115  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
116  * 19952 <= x <= 249205026
117  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
118  * 3444240 >= (1 << 36) / (x) >= 275 */
119 
120  qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / den);
121  }
122  } else {
123  for (i = 0; i < 64; i++) {
124  const int j = s->idsp.idct_permutation[i];
125  int64_t den = (int64_t) qscale * quant_matrix[j];
126  /* We can safely suppose that 16 <= quant_matrix[i] <= 255
127  * Assume x = qscale * quant_matrix[i]
128  * So 16 <= x <= 7905
129  * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
130  * so 32768 >= (1 << 19) / (x) >= 67 */
131  qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
132  //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
133  // (qscale * quant_matrix[i]);
134  qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / den;
135 
136  if (qmat16[qscale][0][i] == 0 ||
137  qmat16[qscale][0][i] == 128 * 256)
138  qmat16[qscale][0][i] = 128 * 256 - 1;
139  qmat16[qscale][1][i] =
140  ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
141  qmat16[qscale][0][i]);
142  }
143  }
144 
145  for (i = intra; i < 64; i++) {
146  int64_t max = 8191;
147  if (fdsp->fdct == ff_fdct_ifast) {
148  max = (8191LL * ff_aanscales[i]) >> 14;
149  }
150  while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
151  shift++;
152  }
153  }
154  }
155  if (shift) {
157  "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
158  QMAT_SHIFT - shift);
159  }
160 }
161 
162 static inline void update_qscale(MpegEncContext *s)
163 {
164  s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
165  (FF_LAMBDA_SHIFT + 7);
166  s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
167 
168  s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
170 }
171 
172 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
173 {
174  int i;
175 
176  if (matrix) {
177  put_bits(pb, 1, 1);
178  for (i = 0; i < 64; i++) {
179  put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
180  }
181  } else
182  put_bits(pb, 1, 0);
183 }
184 
185 /**
186  * init s->current_picture.qscale_table from s->lambda_table
187  */
189 {
190  int8_t * const qscale_table = s->current_picture.qscale_table;
191  int i;
192 
193  for (i = 0; i < s->mb_num; i++) {
194  unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
195  int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
196  qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
197  s->avctx->qmax);
198  }
199 }
200 
203 {
204 #define COPY(a) dst->a= src->a
205  COPY(pict_type);
207  COPY(f_code);
208  COPY(b_code);
209  COPY(qscale);
210  COPY(lambda);
211  COPY(lambda2);
214  COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
215  COPY(progressive_frame); // FIXME don't set in encode_header
216  COPY(partitioned_frame); // FIXME don't set in encode_header
217 #undef COPY
218 }
219 
220 /**
221  * Set the given MpegEncContext to defaults for encoding.
222  * the changed fields will not depend upon the prior state of the MpegEncContext.
223  */
225 {
226  int i;
228 
229  for (i = -16; i < 16; i++) {
230  default_fcode_tab[i + MAX_MV] = 1;
231  }
234 
235  s->input_picture_number = 0;
236  s->picture_in_gop_number = 0;
237 }
238 
240  if (ARCH_X86)
242 
243  if (CONFIG_H263_ENCODER)
245  if (!s->dct_quantize)
247  if (!s->denoise_dct)
250  if (s->avctx->trellis)
252 
253  return 0;
254 }
255 
256 /* init video encoder */
258 {
259  MpegEncContext *s = avctx->priv_data;
260  int i, ret, format_supported;
261 
263 
264  switch (avctx->codec_id) {
266  if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
267  avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
268  av_log(avctx, AV_LOG_ERROR,
269  "only YUV420 and YUV422 are supported\n");
270  return -1;
271  }
272  break;
273  case AV_CODEC_ID_MJPEG:
274  case AV_CODEC_ID_AMV:
275  format_supported = 0;
276  /* JPEG color space */
277  if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
278  avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
279  avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
280  (avctx->color_range == AVCOL_RANGE_JPEG &&
281  (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
282  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
283  avctx->pix_fmt == AV_PIX_FMT_YUV444P)))
284  format_supported = 1;
285  /* MPEG color space */
286  else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
287  (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
288  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
289  avctx->pix_fmt == AV_PIX_FMT_YUV444P))
290  format_supported = 1;
291 
292  if (!format_supported) {
293  av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
294  return -1;
295  }
296  break;
297  default:
298  if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
299  av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
300  return -1;
301  }
302  }
303 
304  switch (avctx->pix_fmt) {
305  case AV_PIX_FMT_YUVJ444P:
306  case AV_PIX_FMT_YUV444P:
308  break;
309  case AV_PIX_FMT_YUVJ422P:
310  case AV_PIX_FMT_YUV422P:
312  break;
313  case AV_PIX_FMT_YUVJ420P:
314  case AV_PIX_FMT_YUV420P:
315  default:
317  break;
318  }
319 
320  s->bit_rate = avctx->bit_rate;
321  s->width = avctx->width;
322  s->height = avctx->height;
323  if (avctx->gop_size > 600 &&
325  av_log(avctx, AV_LOG_WARNING,
326  "keyframe interval too large!, reducing it from %d to %d\n",
327  avctx->gop_size, 600);
328  avctx->gop_size = 600;
329  }
330  s->gop_size = avctx->gop_size;
331  s->avctx = avctx;
332  if (avctx->max_b_frames > MAX_B_FRAMES) {
333  av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
334  "is %d.\n", MAX_B_FRAMES);
335  avctx->max_b_frames = MAX_B_FRAMES;
336  }
337  s->max_b_frames = avctx->max_b_frames;
338  s->codec_id = avctx->codec->id;
340  s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
341  s->mpeg_quant = avctx->mpeg_quant;
342  s->rtp_mode = !!avctx->rtp_payload_size;
344 
345  // workaround some differences between how applications specify dc precision
346  if (s->intra_dc_precision < 0) {
347  s->intra_dc_precision += 8;
348  } else if (s->intra_dc_precision >= 8)
349  s->intra_dc_precision -= 8;
350 
351  if (s->intra_dc_precision < 0) {
352  av_log(avctx, AV_LOG_ERROR,
353  "intra dc precision must be positive, note some applications use"
354  " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
355  return AVERROR(EINVAL);
356  }
357 
358  if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
359  av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
360  return AVERROR(EINVAL);
361  }
363 
364  if (s->gop_size <= 1) {
365  s->intra_only = 1;
366  s->gop_size = 12;
367  } else {
368  s->intra_only = 0;
369  }
370 
371  s->me_method = avctx->me_method;
372 
373  /* Fixed QSCALE */
374  s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
375 
376 #if FF_API_MPV_OPT
378  if (avctx->border_masking != 0.0)
379  s->border_masking = avctx->border_masking;
381 #endif
382 
383  s->adaptive_quant = (s->avctx->lumi_masking ||
384  s->avctx->dark_masking ||
387  s->avctx->p_masking ||
388  s->border_masking ||
389  (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
390  !s->fixed_qscale;
391 
393 
394  if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
395  switch(avctx->codec_id) {
398  avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
399  break;
400  case AV_CODEC_ID_MPEG4:
404  if (avctx->rc_max_rate >= 15000000) {
405  avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
406  } else if(avctx->rc_max_rate >= 2000000) {
407  avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
408  } else if(avctx->rc_max_rate >= 384000) {
409  avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
410  } else
411  avctx->rc_buffer_size = 40;
412  avctx->rc_buffer_size *= 16384;
413  break;
414  }
415  if (avctx->rc_buffer_size) {
416  av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
417  }
418  }
419 
420  if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
421  av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
422  return -1;
423  }
424 
425  if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
426  av_log(avctx, AV_LOG_INFO,
427  "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
428  }
429 
430  if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
431  av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
432  return -1;
433  }
434 
435  if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
436  av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
437  return -1;
438  }
439 
440  if (avctx->rc_max_rate &&
441  avctx->rc_max_rate == avctx->bit_rate &&
442  avctx->rc_max_rate != avctx->rc_min_rate) {
443  av_log(avctx, AV_LOG_INFO,
444  "impossible bitrate constraints, this will fail\n");
445  }
446 
447  if (avctx->rc_buffer_size &&
448  avctx->bit_rate * (int64_t)avctx->time_base.num >
449  avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
450  av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
451  return -1;
452  }
453 
454  if (!s->fixed_qscale &&
455  avctx->bit_rate * av_q2d(avctx->time_base) >
456  avctx->bit_rate_tolerance) {
457  av_log(avctx, AV_LOG_WARNING,
458  "bitrate tolerance %d too small for bitrate %d, overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
459  avctx->bit_rate_tolerance = 5 * avctx->bit_rate * av_q2d(avctx->time_base);
460  }
461 
462  if (s->avctx->rc_max_rate &&
463  s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
466  90000LL * (avctx->rc_buffer_size - 1) >
467  s->avctx->rc_max_rate * 0xFFFFLL) {
468  av_log(avctx, AV_LOG_INFO,
469  "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
470  "specified vbv buffer is too large for the given bitrate!\n");
471  }
472 
473  if ((s->avctx->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
475  s->codec_id != AV_CODEC_ID_FLV1) {
476  av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
477  return -1;
478  }
479 
480  if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
481  av_log(avctx, AV_LOG_ERROR,
482  "OBMC is only supported with simple mb decision\n");
483  return -1;
484  }
485 
486  if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
487  av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
488  return -1;
489  }
490 
491  if (s->max_b_frames &&
492  s->codec_id != AV_CODEC_ID_MPEG4 &&
495  av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
496  return -1;
497  }
498  if (s->max_b_frames < 0) {
499  av_log(avctx, AV_LOG_ERROR,
500  "max b frames must be 0 or positive for mpegvideo based encoders\n");
501  return -1;
502  }
503 
504  if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
505  s->codec_id == AV_CODEC_ID_H263 ||
506  s->codec_id == AV_CODEC_ID_H263P) &&
507  (avctx->sample_aspect_ratio.num > 255 ||
508  avctx->sample_aspect_ratio.den > 255)) {
509  av_log(avctx, AV_LOG_WARNING,
510  "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
513  avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
514  }
515 
516  if ((s->codec_id == AV_CODEC_ID_H263 ||
517  s->codec_id == AV_CODEC_ID_H263P) &&
518  (avctx->width > 2048 ||
519  avctx->height > 1152 )) {
520  av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
521  return -1;
522  }
523  if ((s->codec_id == AV_CODEC_ID_H263 ||
524  s->codec_id == AV_CODEC_ID_H263P) &&
525  ((avctx->width &3) ||
526  (avctx->height&3) )) {
527  av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
528  return -1;
529  }
530 
531  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
532  (avctx->width > 4095 ||
533  avctx->height > 4095 )) {
534  av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
535  return -1;
536  }
537 
538  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
539  (avctx->width > 16383 ||
540  avctx->height > 16383 )) {
541  av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
542  return -1;
543  }
544 
545  if (s->codec_id == AV_CODEC_ID_RV10 &&
546  (avctx->width &15 ||
547  avctx->height&15 )) {
548  av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
549  return AVERROR(EINVAL);
550  }
551 
552  if (s->codec_id == AV_CODEC_ID_RV20 &&
553  (avctx->width &3 ||
554  avctx->height&3 )) {
555  av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
556  return AVERROR(EINVAL);
557  }
558 
559  if ((s->codec_id == AV_CODEC_ID_WMV1 ||
560  s->codec_id == AV_CODEC_ID_WMV2) &&
561  avctx->width & 1) {
562  av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
563  return -1;
564  }
565 
568  av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
569  return -1;
570  }
571 
572  // FIXME mpeg2 uses that too
573  if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
574  && s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
575  av_log(avctx, AV_LOG_ERROR,
576  "mpeg2 style quantization not supported by codec\n");
577  return -1;
578  }
579 
580  if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
581  av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
582  return -1;
583  }
584 
585  if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
587  av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
588  return -1;
589  }
590 
591  if (s->avctx->scenechange_threshold < 1000000000 &&
593  av_log(avctx, AV_LOG_ERROR,
594  "closed gop with scene change detection are not supported yet, "
595  "set threshold to 1000000000\n");
596  return -1;
597  }
598 
599  if (s->avctx->flags & CODEC_FLAG_LOW_DELAY) {
600  if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
601  av_log(avctx, AV_LOG_ERROR,
602  "low delay forcing is only available for mpeg2\n");
603  return -1;
604  }
605  if (s->max_b_frames != 0) {
606  av_log(avctx, AV_LOG_ERROR,
607  "b frames cannot be used with low delay\n");
608  return -1;
609  }
610  }
611 
612  if (s->q_scale_type == 1) {
613  if (avctx->qmax > 12) {
614  av_log(avctx, AV_LOG_ERROR,
615  "non linear quant only supports qmax <= 12 currently\n");
616  return -1;
617  }
618  }
619 
620  if (s->avctx->thread_count > 1 &&
621  s->codec_id != AV_CODEC_ID_MPEG4 &&
624  s->codec_id != AV_CODEC_ID_MJPEG &&
625  (s->codec_id != AV_CODEC_ID_H263P)) {
626  av_log(avctx, AV_LOG_ERROR,
627  "multi threaded encoding not supported by codec\n");
628  return -1;
629  }
630 
631  if (s->avctx->thread_count < 1) {
632  av_log(avctx, AV_LOG_ERROR,
633  "automatic thread number detection not supported by codec, "
634  "patch welcome\n");
635  return -1;
636  }
637 
638  if (s->avctx->slices > 1 || s->avctx->thread_count > 1)
639  s->rtp_mode = 1;
640 
641  if (s->avctx->thread_count > 1 && s->codec_id == AV_CODEC_ID_H263P)
642  s->h263_slice_structured = 1;
643 
644  if (!avctx->time_base.den || !avctx->time_base.num) {
645  av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
646  return -1;
647  }
648 
649  if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
650  av_log(avctx, AV_LOG_INFO,
651  "notice: b_frame_strategy only affects the first pass\n");
652  avctx->b_frame_strategy = 0;
653  }
654 
655  i = av_gcd(avctx->time_base.den, avctx->time_base.num);
656  if (i > 1) {
657  av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
658  avctx->time_base.den /= i;
659  avctx->time_base.num /= i;
660  //return -1;
661  }
662 
664  // (a + x * 3 / 8) / x
665  s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
666  s->inter_quant_bias = 0;
667  } else {
668  s->intra_quant_bias = 0;
669  // (a - x / 4) / x
670  s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
671  }
672 
673  if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
674  av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
675  return AVERROR(EINVAL);
676  }
677 
682 
683  av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
684 
685  if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
686  s->avctx->time_base.den > (1 << 16) - 1) {
687  av_log(avctx, AV_LOG_ERROR,
688  "timebase %d/%d not supported by MPEG 4 standard, "
689  "the maximum admitted value for the timebase denominator "
690  "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
691  (1 << 16) - 1);
692  return -1;
693  }
694  s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
695 
696  switch (avctx->codec->id) {
698  s->out_format = FMT_MPEG1;
699  s->low_delay = !!(s->avctx->flags & CODEC_FLAG_LOW_DELAY);
700  avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
701  break;
703  s->out_format = FMT_MPEG1;
704  s->low_delay = !!(s->avctx->flags & CODEC_FLAG_LOW_DELAY);
705  avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
706  s->rtp_mode = 1;
707  break;
708  case AV_CODEC_ID_MJPEG:
709  case AV_CODEC_ID_AMV:
710  s->out_format = FMT_MJPEG;
711  s->intra_only = 1; /* force intra only for jpeg */
712  if (!CONFIG_MJPEG_ENCODER ||
713  ff_mjpeg_encode_init(s) < 0)
714  return -1;
715  avctx->delay = 0;
716  s->low_delay = 1;
717  break;
718  case AV_CODEC_ID_H261:
719  if (!CONFIG_H261_ENCODER)
720  return -1;
721  if (ff_h261_get_picture_format(s->width, s->height) < 0) {
722  av_log(avctx, AV_LOG_ERROR,
723  "The specified picture size of %dx%d is not valid for the "
724  "H.261 codec.\nValid sizes are 176x144, 352x288\n",
725  s->width, s->height);
726  return -1;
727  }
728  s->out_format = FMT_H261;
729  avctx->delay = 0;
730  s->low_delay = 1;
731  s->rtp_mode = 0; /* Sliced encoding not supported */
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)
892  if ((ret = ff_msmpeg4_encode_init(s)) < 0)
893  return ret;
894  if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
895  && s->out_format == FMT_MPEG1)
897 
898  /* init q matrix */
899  for (i = 0; i < 64; i++) {
900  int j = s->idsp.idct_permutation[i];
901  if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
902  s->mpeg_quant) {
905  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
906  s->intra_matrix[j] =
908  } else {
909  /* mpeg1/2 */
910  s->chroma_intra_matrix[j] =
913  }
914  if (s->avctx->intra_matrix)
915  s->intra_matrix[j] = s->avctx->intra_matrix[i];
916  if (s->avctx->inter_matrix)
917  s->inter_matrix[j] = s->avctx->inter_matrix[i];
918  }
919 
920  /* precompute matrix */
921  /* for mjpeg, we do include qscale in the matrix */
922  if (s->out_format != FMT_MJPEG) {
924  s->intra_matrix, s->intra_quant_bias, avctx->qmin,
925  31, 1);
927  s->inter_matrix, s->inter_quant_bias, avctx->qmin,
928  31, 0);
929  }
930 
931  if (ff_rate_control_init(s) < 0)
932  return -1;
933 
934 #if FF_API_ERROR_RATE
936  if (avctx->error_rate)
937  s->error_rate = avctx->error_rate;
939 #endif
940 
941 #if FF_API_NORMALIZE_AQP
943  if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
946 #endif
947 
948 #if FF_API_MV0
950  if (avctx->flags & CODEC_FLAG_MV0)
953 #endif
954 
955 #if FF_API_MPV_OPT
957  if (avctx->rc_qsquish != 0.0)
958  s->rc_qsquish = avctx->rc_qsquish;
959  if (avctx->rc_qmod_amp != 0.0)
960  s->rc_qmod_amp = avctx->rc_qmod_amp;
961  if (avctx->rc_qmod_freq)
962  s->rc_qmod_freq = avctx->rc_qmod_freq;
963  if (avctx->rc_buffer_aggressivity != 1.0)
965  if (avctx->rc_initial_cplx != 0.0)
966  s->rc_initial_cplx = avctx->rc_initial_cplx;
967  if (avctx->lmin)
968  s->lmin = avctx->lmin;
969  if (avctx->lmax)
970  s->lmax = avctx->lmax;
971 
972  if (avctx->rc_eq) {
973  av_freep(&s->rc_eq);
974  s->rc_eq = av_strdup(avctx->rc_eq);
975  if (!s->rc_eq)
976  return AVERROR(ENOMEM);
977  }
979 #endif
980 
981  if (avctx->b_frame_strategy == 2) {
982  for (i = 0; i < s->max_b_frames + 2; i++) {
983  s->tmp_frames[i] = av_frame_alloc();
984  if (!s->tmp_frames[i])
985  return AVERROR(ENOMEM);
986 
988  s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
989  s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
990 
991  ret = av_frame_get_buffer(s->tmp_frames[i], 32);
992  if (ret < 0)
993  return ret;
994  }
995  }
996 
997  return 0;
998 fail:
999  ff_mpv_encode_end(avctx);
1000  return AVERROR_UNKNOWN;
1001 }
1002 
1004 {
1005  MpegEncContext *s = avctx->priv_data;
1006  int i;
1007 
1009 
1010  ff_mpv_common_end(s);
1011  if (CONFIG_MJPEG_ENCODER &&
1012  s->out_format == FMT_MJPEG)
1014 
1015  av_freep(&avctx->extradata);
1016 
1017  for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
1018  av_frame_free(&s->tmp_frames[i]);
1019 
1022 
1023  av_freep(&s->avctx->stats_out);
1024  av_freep(&s->ac_stats);
1025 
1030  av_freep(&s->q_intra_matrix);
1031  av_freep(&s->q_inter_matrix);
1034  av_freep(&s->input_picture);
1036  av_freep(&s->dct_offset);
1037 
1038  return 0;
1039 }
1040 
1041 static int get_sae(uint8_t *src, int ref, int stride)
1042 {
1043  int x,y;
1044  int acc = 0;
1045 
1046  for (y = 0; y < 16; y++) {
1047  for (x = 0; x < 16; x++) {
1048  acc += FFABS(src[x + y * stride] - ref);
1049  }
1050  }
1051 
1052  return acc;
1053 }
1054 
1056  uint8_t *ref, int stride)
1057 {
1058  int x, y, w, h;
1059  int acc = 0;
1060 
1061  w = s->width & ~15;
1062  h = s->height & ~15;
1063 
1064  for (y = 0; y < h; y += 16) {
1065  for (x = 0; x < w; x += 16) {
1066  int offset = x + y * stride;
1067  int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
1068  stride, 16);
1069  int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1070  int sae = get_sae(src + offset, mean, stride);
1071 
1072  acc += sae + 500 < sad;
1073  }
1074  }
1075  return acc;
1076 }
1077 
1078 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
1079 {
1080  return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
1082  s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
1083  &s->linesize, &s->uvlinesize);
1084 }
1085 
1086 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1087 {
1088  Picture *pic = NULL;
1089  int64_t pts;
1090  int i, display_picture_number = 0, ret;
1091  const int encoding_delay = s->max_b_frames ? s->max_b_frames :
1092  (s->low_delay ? 0 : 1);
1093  int direct = 1;
1094 
1095  if (pic_arg) {
1096  pts = pic_arg->pts;
1097  display_picture_number = s->input_picture_number++;
1098 
1099  if (pts != AV_NOPTS_VALUE) {
1100  if (s->user_specified_pts != AV_NOPTS_VALUE) {
1101  int64_t last = s->user_specified_pts;
1102 
1103  if (pts <= last) {
1105  "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1106  pts, last);
1107  return AVERROR(EINVAL);
1108  }
1109 
1110  if (!s->low_delay && display_picture_number == 1)
1111  s->dts_delta = pts - last;
1112  }
1113  s->user_specified_pts = pts;
1114  } else {
1115  if (s->user_specified_pts != AV_NOPTS_VALUE) {
1116  s->user_specified_pts =
1117  pts = s->user_specified_pts + 1;
1118  av_log(s->avctx, AV_LOG_INFO,
1119  "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1120  pts);
1121  } else {
1122  pts = display_picture_number;
1123  }
1124  }
1125  }
1126 
1127  if (pic_arg) {
1128  if (!pic_arg->buf[0] ||
1129  pic_arg->linesize[0] != s->linesize ||
1130  pic_arg->linesize[1] != s->uvlinesize ||
1131  pic_arg->linesize[2] != s->uvlinesize)
1132  direct = 0;
1133  if ((s->width & 15) || (s->height & 15))
1134  direct = 0;
1135  if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1136  direct = 0;
1137  if (s->linesize & (STRIDE_ALIGN-1))
1138  direct = 0;
1139 
1140  ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
1141  pic_arg->linesize[1], s->linesize, s->uvlinesize);
1142 
1143  i = ff_find_unused_picture(s->avctx, s->picture, direct);
1144  if (i < 0)
1145  return i;
1146 
1147  pic = &s->picture[i];
1148  pic->reference = 3;
1149 
1150  if (direct) {
1151  if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1152  return ret;
1153  }
1154  ret = alloc_picture(s, pic, direct);
1155  if (ret < 0)
1156  return ret;
1157 
1158  if (!direct) {
1159  if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1160  pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1161  pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1162  // empty
1163  } else {
1164  int h_chroma_shift, v_chroma_shift;
1166  &h_chroma_shift,
1167  &v_chroma_shift);
1168 
1169  for (i = 0; i < 3; i++) {
1170  int src_stride = pic_arg->linesize[i];
1171  int dst_stride = i ? s->uvlinesize : s->linesize;
1172  int h_shift = i ? h_chroma_shift : 0;
1173  int v_shift = i ? v_chroma_shift : 0;
1174  int w = s->width >> h_shift;
1175  int h = s->height >> v_shift;
1176  uint8_t *src = pic_arg->data[i];
1177  uint8_t *dst = pic->f->data[i];
1178  int vpad = 16;
1179 
1180  if ( s->codec_id == AV_CODEC_ID_MPEG2VIDEO
1181  && !s->progressive_sequence
1182  && FFALIGN(s->height, 32) - s->height > 16)
1183  vpad = 32;
1184 
1185  if (!s->avctx->rc_buffer_size)
1186  dst += INPLACE_OFFSET;
1187 
1188  if (src_stride == dst_stride)
1189  memcpy(dst, src, src_stride * h);
1190  else {
1191  int h2 = h;
1192  uint8_t *dst2 = dst;
1193  while (h2--) {
1194  memcpy(dst2, src, w);
1195  dst2 += dst_stride;
1196  src += src_stride;
1197  }
1198  }
1199  if ((s->width & 15) || (s->height & (vpad-1))) {
1200  s->mpvencdsp.draw_edges(dst, dst_stride,
1201  w, h,
1202  16 >> h_shift,
1203  vpad >> v_shift,
1204  EDGE_BOTTOM);
1205  }
1206  }
1207  }
1208  }
1209  ret = av_frame_copy_props(pic->f, pic_arg);
1210  if (ret < 0)
1211  return ret;
1212 
1213  pic->f->display_picture_number = display_picture_number;
1214  pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
1215  }
1216 
1217  /* shift buffer entries */
1218  for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1219  s->input_picture[i - 1] = s->input_picture[i];
1220 
1221  s->input_picture[encoding_delay] = (Picture*) pic;
1222 
1223  return 0;
1224 }
1225 
1226 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1227 {
1228  int x, y, plane;
1229  int score = 0;
1230  int64_t score64 = 0;
1231 
1232  for (plane = 0; plane < 3; plane++) {
1233  const int stride = p->f->linesize[plane];
1234  const int bw = plane ? 1 : 2;
1235  for (y = 0; y < s->mb_height * bw; y++) {
1236  for (x = 0; x < s->mb_width * bw; x++) {
1237  int off = p->shared ? 0 : 16;
1238  uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1239  uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1240  int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1241 
1242  switch (FFABS(s->avctx->frame_skip_exp)) {
1243  case 0: score = FFMAX(score, v); break;
1244  case 1: score += FFABS(v); break;
1245  case 2: score64 += v * (int64_t)v; break;
1246  case 3: score64 += FFABS(v * (int64_t)v * v); break;
1247  case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1248  }
1249  }
1250  }
1251  }
1252  emms_c();
1253 
1254  if (score)
1255  score64 = score;
1256  if (s->avctx->frame_skip_exp < 0)
1257  score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1258  -1.0/s->avctx->frame_skip_exp);
1259 
1260  if (score64 < s->avctx->frame_skip_threshold)
1261  return 1;
1262  if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1263  return 1;
1264  return 0;
1265 }
1266 
1268 {
1269  AVPacket pkt = { 0 };
1270  int ret, got_output;
1271 
1272  av_init_packet(&pkt);
1273  ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1274  if (ret < 0)
1275  return ret;
1276 
1277  ret = pkt.size;
1278  av_free_packet(&pkt);
1279  return ret;
1280 }
1281 
1283 {
1286  const int scale = s->avctx->brd_scale;
1287  int i, j, out_size, p_lambda, b_lambda, lambda2;
1288  int64_t best_rd = INT64_MAX;
1289  int best_b_count = -1;
1290 
1291  if (!c)
1292  return AVERROR(ENOMEM);
1293  av_assert0(scale >= 0 && scale <= 3);
1294 
1295  //emms_c();
1296  //s->next_picture_ptr->quality;
1297  p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1298  //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1299  b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1300  if (!b_lambda) // FIXME we should do this somewhere else
1301  b_lambda = p_lambda;
1302  lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1304 
1305  c->width = s->width >> scale;
1306  c->height = s->height >> scale;
1308  c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1309  c->mb_decision = s->avctx->mb_decision;
1310  c->me_cmp = s->avctx->me_cmp;
1311  c->mb_cmp = s->avctx->mb_cmp;
1312  c->me_sub_cmp = s->avctx->me_sub_cmp;
1314  c->time_base = s->avctx->time_base;
1315  c->max_b_frames = s->max_b_frames;
1316 
1317  if (avcodec_open2(c, codec, NULL) < 0)
1318  return -1;
1319 
1320  for (i = 0; i < s->max_b_frames + 2; i++) {
1321  Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1322  s->next_picture_ptr;
1323  uint8_t *data[4];
1324 
1325  if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1326  pre_input = *pre_input_ptr;
1327  memcpy(data, pre_input_ptr->f->data, sizeof(data));
1328 
1329  if (!pre_input.shared && i) {
1330  data[0] += INPLACE_OFFSET;
1331  data[1] += INPLACE_OFFSET;
1332  data[2] += INPLACE_OFFSET;
1333  }
1334 
1335  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1336  s->tmp_frames[i]->linesize[0],
1337  data[0],
1338  pre_input.f->linesize[0],
1339  c->width, c->height);
1340  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1341  s->tmp_frames[i]->linesize[1],
1342  data[1],
1343  pre_input.f->linesize[1],
1344  c->width >> 1, c->height >> 1);
1345  s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1346  s->tmp_frames[i]->linesize[2],
1347  data[2],
1348  pre_input.f->linesize[2],
1349  c->width >> 1, c->height >> 1);
1350  }
1351  }
1352 
1353  for (j = 0; j < s->max_b_frames + 1; j++) {
1354  int64_t rd = 0;
1355 
1356  if (!s->input_picture[j])
1357  break;
1358 
1359  c->error[0] = c->error[1] = c->error[2] = 0;
1360 
1362  s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1363 
1364  out_size = encode_frame(c, s->tmp_frames[0]);
1365 
1366  //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1367 
1368  for (i = 0; i < s->max_b_frames + 1; i++) {
1369  int is_p = i % (j + 1) == j || i == s->max_b_frames;
1370 
1371  s->tmp_frames[i + 1]->pict_type = is_p ?
1373  s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1374 
1375  out_size = encode_frame(c, s->tmp_frames[i + 1]);
1376 
1377  rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1378  }
1379 
1380  /* get the delayed frames */
1381  while (out_size) {
1382  out_size = encode_frame(c, NULL);
1383  rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1384  }
1385 
1386  rd += c->error[0] + c->error[1] + c->error[2];
1387 
1388  if (rd < best_rd) {
1389  best_rd = rd;
1390  best_b_count = j;
1391  }
1392  }
1393 
1394  avcodec_close(c);
1395  av_freep(&c);
1396 
1397  return best_b_count;
1398 }
1399 
1401 {
1402  int i, ret;
1403 
1404  for (i = 1; i < MAX_PICTURE_COUNT; i++)
1406  s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1407 
1408  /* set next picture type & ordering */
1409  if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1411  if (s->picture_in_gop_number < s->gop_size &&
1412  s->next_picture_ptr &&
1413  skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1414  // FIXME check that te gop check above is +-1 correct
1415  av_frame_unref(s->input_picture[0]->f);
1416 
1417  ff_vbv_update(s, 0);
1418 
1419  goto no_output_pic;
1420  }
1421  }
1422 
1423  if (/*s->picture_in_gop_number >= s->gop_size ||*/
1424  !s->next_picture_ptr || s->intra_only) {
1425  s->reordered_input_picture[0] = s->input_picture[0];
1428  s->coded_picture_number++;
1429  } else {
1430  int b_frames;
1431 
1432  if (s->avctx->flags & CODEC_FLAG_PASS2) {
1433  for (i = 0; i < s->max_b_frames + 1; i++) {
1434  int pict_num = s->input_picture[0]->f->display_picture_number + i;
1435 
1436  if (pict_num >= s->rc_context.num_entries)
1437  break;
1438  if (!s->input_picture[i]) {
1439  s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1440  break;
1441  }
1442 
1443  s->input_picture[i]->f->pict_type =
1444  s->rc_context.entry[pict_num].new_pict_type;
1445  }
1446  }
1447 
1448  if (s->avctx->b_frame_strategy == 0) {
1449  b_frames = s->max_b_frames;
1450  while (b_frames && !s->input_picture[b_frames])
1451  b_frames--;
1452  } else if (s->avctx->b_frame_strategy == 1) {
1453  for (i = 1; i < s->max_b_frames + 1; i++) {
1454  if (s->input_picture[i] &&
1455  s->input_picture[i]->b_frame_score == 0) {
1456  s->input_picture[i]->b_frame_score =
1457  get_intra_count(s,
1458  s->input_picture[i ]->f->data[0],
1459  s->input_picture[i - 1]->f->data[0],
1460  s->linesize) + 1;
1461  }
1462  }
1463  for (i = 0; i < s->max_b_frames + 1; i++) {
1464  if (!s->input_picture[i] ||
1465  s->input_picture[i]->b_frame_score - 1 >
1466  s->mb_num / s->avctx->b_sensitivity)
1467  break;
1468  }
1469 
1470  b_frames = FFMAX(0, i - 1);
1471 
1472  /* reset scores */
1473  for (i = 0; i < b_frames + 1; i++) {
1474  s->input_picture[i]->b_frame_score = 0;
1475  }
1476  } else if (s->avctx->b_frame_strategy == 2) {
1477  b_frames = estimate_best_b_count(s);
1478  } else {
1479  av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1480  b_frames = 0;
1481  }
1482 
1483  emms_c();
1484 
1485  for (i = b_frames - 1; i >= 0; i--) {
1486  int type = s->input_picture[i]->f->pict_type;
1487  if (type && type != AV_PICTURE_TYPE_B)
1488  b_frames = i;
1489  }
1490  if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1491  b_frames == s->max_b_frames) {
1493  "warning, too many b frames in a row\n");
1494  }
1495 
1496  if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1497  if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1498  s->gop_size > s->picture_in_gop_number) {
1499  b_frames = s->gop_size - s->picture_in_gop_number - 1;
1500  } else {
1501  if (s->avctx->flags & CODEC_FLAG_CLOSED_GOP)
1502  b_frames = 0;
1503  s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1504  }
1505  }
1506 
1507  if ((s->avctx->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1508  s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1509  b_frames--;
1510 
1511  s->reordered_input_picture[0] = s->input_picture[b_frames];
1515  s->coded_picture_number++;
1516  for (i = 0; i < b_frames; i++) {
1517  s->reordered_input_picture[i + 1] = s->input_picture[i];
1518  s->reordered_input_picture[i + 1]->f->pict_type =
1521  s->coded_picture_number++;
1522  }
1523  }
1524  }
1525 no_output_pic:
1526  if (s->reordered_input_picture[0]) {
1529  AV_PICTURE_TYPE_B ? 3 : 0;
1530 
1532  if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1533  return ret;
1534 
1535  if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1536  // input is a shared pix, so we can't modifiy it -> alloc a new
1537  // one & ensure that the shared one is reuseable
1538 
1539  Picture *pic;
1540  int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1541  if (i < 0)
1542  return i;
1543  pic = &s->picture[i];
1544 
1546  if (alloc_picture(s, pic, 0) < 0) {
1547  return -1;
1548  }
1549 
1550  ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1551  if (ret < 0)
1552  return ret;
1553 
1554  /* mark us unused / free shared pic */
1556  s->reordered_input_picture[0]->shared = 0;
1557 
1558  s->current_picture_ptr = pic;
1559  } else {
1560  // input is not a shared pix -> reuse buffer for current_pix
1562  for (i = 0; i < 4; i++) {
1563  s->new_picture.f->data[i] += INPLACE_OFFSET;
1564  }
1565  }
1567  if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1568  s->current_picture_ptr)) < 0)
1569  return ret;
1570 
1572  } else {
1574  }
1575  return 0;
1576 }
1577 
1578 static void frame_end(MpegEncContext *s)
1579 {
1580  if (s->unrestricted_mv &&
1582  !s->intra_only) {
1584  int hshift = desc->log2_chroma_w;
1585  int vshift = desc->log2_chroma_h;
1587  s->current_picture.f->linesize[0],
1588  s->h_edge_pos, s->v_edge_pos,
1590  EDGE_TOP | EDGE_BOTTOM);
1592  s->current_picture.f->linesize[1],
1593  s->h_edge_pos >> hshift,
1594  s->v_edge_pos >> vshift,
1595  EDGE_WIDTH >> hshift,
1596  EDGE_WIDTH >> vshift,
1597  EDGE_TOP | EDGE_BOTTOM);
1599  s->current_picture.f->linesize[2],
1600  s->h_edge_pos >> hshift,
1601  s->v_edge_pos >> vshift,
1602  EDGE_WIDTH >> hshift,
1603  EDGE_WIDTH >> vshift,
1604  EDGE_TOP | EDGE_BOTTOM);
1605  }
1606 
1607  emms_c();
1608 
1609  s->last_pict_type = s->pict_type;
1611  if (s->pict_type!= AV_PICTURE_TYPE_B)
1613 
1615 
1616 }
1617 
1619 {
1620  int intra, i;
1621 
1622  for (intra = 0; intra < 2; intra++) {
1623  if (s->dct_count[intra] > (1 << 16)) {
1624  for (i = 0; i < 64; i++) {
1625  s->dct_error_sum[intra][i] >>= 1;
1626  }
1627  s->dct_count[intra] >>= 1;
1628  }
1629 
1630  for (i = 0; i < 64; i++) {
1631  s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1632  s->dct_count[intra] +
1633  s->dct_error_sum[intra][i] / 2) /
1634  (s->dct_error_sum[intra][i] + 1);
1635  }
1636  }
1637 }
1638 
1640 {
1641  int ret;
1642 
1643  /* mark & release old frames */
1644  if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1646  s->last_picture_ptr->f->buf[0]) {
1648  }
1649 
1652 
1654  if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1655  s->current_picture_ptr)) < 0)
1656  return ret;
1657 
1658  if (s->pict_type != AV_PICTURE_TYPE_B) {
1660  if (!s->droppable)
1662  }
1663 
1664  if (s->last_picture_ptr) {
1666  if (s->last_picture_ptr->f->buf[0] &&
1667  (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1668  s->last_picture_ptr)) < 0)
1669  return ret;
1670  }
1671  if (s->next_picture_ptr) {
1673  if (s->next_picture_ptr->f->buf[0] &&
1674  (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1675  s->next_picture_ptr)) < 0)
1676  return ret;
1677  }
1678 
1679  if (s->picture_structure!= PICT_FRAME) {
1680  int i;
1681  for (i = 0; i < 4; i++) {
1683  s->current_picture.f->data[i] +=
1684  s->current_picture.f->linesize[i];
1685  }
1686  s->current_picture.f->linesize[i] *= 2;
1687  s->last_picture.f->linesize[i] *= 2;
1688  s->next_picture.f->linesize[i] *= 2;
1689  }
1690  }
1691 
1692  if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1695  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1698  } else {
1701  }
1702 
1703  if (s->dct_error_sum) {
1706  }
1707 
1708  return 0;
1709 }
1710 
1712  const AVFrame *pic_arg, int *got_packet)
1713 {
1714  MpegEncContext *s = avctx->priv_data;
1715  int i, stuffing_count, ret;
1716  int context_count = s->slice_context_count;
1717 
1718  s->picture_in_gop_number++;
1719 
1720  if (load_input_picture(s, pic_arg) < 0)
1721  return -1;
1722 
1723  if (select_input_picture(s) < 0) {
1724  return -1;
1725  }
1726 
1727  /* output? */
1728  if (s->new_picture.f->data[0]) {
1729  int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
1730  int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - FF_INPUT_BUFFER_PADDING_SIZE
1731  :
1732  s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
1733  if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size)) < 0)
1734  return ret;
1735  if (s->mb_info) {
1738  s->mb_width*s->mb_height*12);
1739  s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1740  }
1741 
1742  for (i = 0; i < context_count; i++) {
1743  int start_y = s->thread_context[i]->start_mb_y;
1744  int end_y = s->thread_context[i]-> end_mb_y;
1745  int h = s->mb_height;
1746  uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1747  uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1748 
1749  init_put_bits(&s->thread_context[i]->pb, start, end - start);
1750  }
1751 
1752  s->pict_type = s->new_picture.f->pict_type;
1753  //emms_c();
1754  ret = frame_start(s);
1755  if (ret < 0)
1756  return ret;
1757 vbv_retry:
1758  ret = encode_picture(s, s->picture_number);
1759  if (growing_buffer) {
1760  av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1761  pkt->data = s->pb.buf;
1762  pkt->size = avctx->internal->byte_buffer_size;
1763  }
1764  if (ret < 0)
1765  return -1;
1766 
1767  avctx->header_bits = s->header_bits;
1768  avctx->mv_bits = s->mv_bits;
1769  avctx->misc_bits = s->misc_bits;
1770  avctx->i_tex_bits = s->i_tex_bits;
1771  avctx->p_tex_bits = s->p_tex_bits;
1772  avctx->i_count = s->i_count;
1773  // FIXME f/b_count in avctx
1774  avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1775  avctx->skip_count = s->skip_count;
1776 
1777  frame_end(s);
1778 
1779  if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1781 
1782  if (avctx->rc_buffer_size) {
1783  RateControlContext *rcc = &s->rc_context;
1784  int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
1785 
1786  if (put_bits_count(&s->pb) > max_size &&
1787  s->lambda < s->lmax) {
1788  s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1789  (s->qscale + 1) / s->qscale);
1790  if (s->adaptive_quant) {
1791  int i;
1792  for (i = 0; i < s->mb_height * s->mb_stride; i++)
1793  s->lambda_table[i] =
1794  FFMAX(s->lambda_table[i] + 1,
1795  s->lambda_table[i] * (s->qscale + 1) /
1796  s->qscale);
1797  }
1798  s->mb_skipped = 0; // done in frame_start()
1799  // done in encode_picture() so we must undo it
1800  if (s->pict_type == AV_PICTURE_TYPE_P) {
1801  if (s->flipflop_rounding ||
1802  s->codec_id == AV_CODEC_ID_H263P ||
1804  s->no_rounding ^= 1;
1805  }
1806  if (s->pict_type != AV_PICTURE_TYPE_B) {
1807  s->time_base = s->last_time_base;
1808  s->last_non_b_time = s->time - s->pp_time;
1809  }
1810  for (i = 0; i < context_count; i++) {
1811  PutBitContext *pb = &s->thread_context[i]->pb;
1812  init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1813  }
1814  av_log(s->avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
1815  goto vbv_retry;
1816  }
1817 
1819  }
1820 
1821  if (s->avctx->flags & CODEC_FLAG_PASS1)
1823 
1824  for (i = 0; i < 4; i++) {
1825  s->current_picture_ptr->f->error[i] =
1826  s->current_picture.f->error[i] =
1827  s->current_picture.error[i];
1828  avctx->error[i] += s->current_picture_ptr->f->error[i];
1829  }
1830 
1831  if (s->avctx->flags & CODEC_FLAG_PASS1)
1832  assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1833  avctx->i_tex_bits + avctx->p_tex_bits ==
1834  put_bits_count(&s->pb));
1835  flush_put_bits(&s->pb);
1836  s->frame_bits = put_bits_count(&s->pb);
1837 
1838  stuffing_count = ff_vbv_update(s, s->frame_bits);
1839  s->stuffing_bits = 8*stuffing_count;
1840  if (stuffing_count) {
1841  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1842  stuffing_count + 50) {
1843  av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1844  return -1;
1845  }
1846 
1847  switch (s->codec_id) {
1850  while (stuffing_count--) {
1851  put_bits(&s->pb, 8, 0);
1852  }
1853  break;
1854  case AV_CODEC_ID_MPEG4:
1855  put_bits(&s->pb, 16, 0);
1856  put_bits(&s->pb, 16, 0x1C3);
1857  stuffing_count -= 4;
1858  while (stuffing_count--) {
1859  put_bits(&s->pb, 8, 0xFF);
1860  }
1861  break;
1862  default:
1863  av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1864  }
1865  flush_put_bits(&s->pb);
1866  s->frame_bits = put_bits_count(&s->pb);
1867  }
1868 
1869  /* update mpeg1/2 vbv_delay for CBR */
1870  if (s->avctx->rc_max_rate &&
1871  s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1872  s->out_format == FMT_MPEG1 &&
1873  90000LL * (avctx->rc_buffer_size - 1) <=
1874  s->avctx->rc_max_rate * 0xFFFFLL) {
1875  int vbv_delay, min_delay;
1876  double inbits = s->avctx->rc_max_rate *
1877  av_q2d(s->avctx->time_base);
1878  int minbits = s->frame_bits - 8 *
1879  (s->vbv_delay_ptr - s->pb.buf - 1);
1880  double bits = s->rc_context.buffer_index + minbits - inbits;
1881 
1882  if (bits < 0)
1884  "Internal error, negative bits\n");
1885 
1886  assert(s->repeat_first_field == 0);
1887 
1888  vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1889  min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1890  s->avctx->rc_max_rate;
1891 
1892  vbv_delay = FFMAX(vbv_delay, min_delay);
1893 
1894  av_assert0(vbv_delay < 0xFFFF);
1895 
1896  s->vbv_delay_ptr[0] &= 0xF8;
1897  s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1898  s->vbv_delay_ptr[1] = vbv_delay >> 5;
1899  s->vbv_delay_ptr[2] &= 0x07;
1900  s->vbv_delay_ptr[2] |= vbv_delay << 3;
1901  avctx->vbv_delay = vbv_delay * 300;
1902  }
1903  s->total_bits += s->frame_bits;
1904  avctx->frame_bits = s->frame_bits;
1905 
1906  pkt->pts = s->current_picture.f->pts;
1907  if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1909  pkt->dts = pkt->pts - s->dts_delta;
1910  else
1911  pkt->dts = s->reordered_pts;
1912  s->reordered_pts = pkt->pts;
1913  } else
1914  pkt->dts = pkt->pts;
1915  if (s->current_picture.f->key_frame)
1916  pkt->flags |= AV_PKT_FLAG_KEY;
1917  if (s->mb_info)
1919  } else {
1920  s->frame_bits = 0;
1921  }
1922 
1923  /* release non-reference frames */
1924  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1925  if (!s->picture[i].reference)
1926  ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1927  }
1928 
1929  av_assert1((s->frame_bits & 7) == 0);
1930 
1931  pkt->size = s->frame_bits / 8;
1932  *got_packet = !!pkt->size;
1933  return 0;
1934 }
1935 
1937  int n, int threshold)
1938 {
1939  static const char tab[64] = {
1940  3, 2, 2, 1, 1, 1, 1, 1,
1941  1, 1, 1, 1, 1, 1, 1, 1,
1942  1, 1, 1, 1, 1, 1, 1, 1,
1943  0, 0, 0, 0, 0, 0, 0, 0,
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  };
1949  int score = 0;
1950  int run = 0;
1951  int i;
1952  int16_t *block = s->block[n];
1953  const int last_index = s->block_last_index[n];
1954  int skip_dc;
1955 
1956  if (threshold < 0) {
1957  skip_dc = 0;
1958  threshold = -threshold;
1959  } else
1960  skip_dc = 1;
1961 
1962  /* Are all we could set to zero already zero? */
1963  if (last_index <= skip_dc - 1)
1964  return;
1965 
1966  for (i = 0; i <= last_index; i++) {
1967  const int j = s->intra_scantable.permutated[i];
1968  const int level = FFABS(block[j]);
1969  if (level == 1) {
1970  if (skip_dc && i == 0)
1971  continue;
1972  score += tab[run];
1973  run = 0;
1974  } else if (level > 1) {
1975  return;
1976  } else {
1977  run++;
1978  }
1979  }
1980  if (score >= threshold)
1981  return;
1982  for (i = skip_dc; i <= last_index; i++) {
1983  const int j = s->intra_scantable.permutated[i];
1984  block[j] = 0;
1985  }
1986  if (block[0])
1987  s->block_last_index[n] = 0;
1988  else
1989  s->block_last_index[n] = -1;
1990 }
1991 
1992 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1993  int last_index)
1994 {
1995  int i;
1996  const int maxlevel = s->max_qcoeff;
1997  const int minlevel = s->min_qcoeff;
1998  int overflow = 0;
1999 
2000  if (s->mb_intra) {
2001  i = 1; // skip clipping of intra dc
2002  } else
2003  i = 0;
2004 
2005  for (; i <= last_index; i++) {
2006  const int j = s->intra_scantable.permutated[i];
2007  int level = block[j];
2008 
2009  if (level > maxlevel) {
2010  level = maxlevel;
2011  overflow++;
2012  } else if (level < minlevel) {
2013  level = minlevel;
2014  overflow++;
2015  }
2016 
2017  block[j] = level;
2018  }
2019 
2020  if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2021  av_log(s->avctx, AV_LOG_INFO,
2022  "warning, clipping %d dct coefficients to %d..%d\n",
2023  overflow, minlevel, maxlevel);
2024 }
2025 
2026 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
2027 {
2028  int x, y;
2029  // FIXME optimize
2030  for (y = 0; y < 8; y++) {
2031  for (x = 0; x < 8; x++) {
2032  int x2, y2;
2033  int sum = 0;
2034  int sqr = 0;
2035  int count = 0;
2036 
2037  for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2038  for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2039  int v = ptr[x2 + y2 * stride];
2040  sum += v;
2041  sqr += v * v;
2042  count++;
2043  }
2044  }
2045  weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2046  }
2047  }
2048 }
2049 
2051  int motion_x, int motion_y,
2052  int mb_block_height,
2053  int mb_block_width,
2054  int mb_block_count)
2055 {
2056  int16_t weight[12][64];
2057  int16_t orig[12][64];
2058  const int mb_x = s->mb_x;
2059  const int mb_y = s->mb_y;
2060  int i;
2061  int skip_dct[12];
2062  int dct_offset = s->linesize * 8; // default for progressive frames
2063  int uv_dct_offset = s->uvlinesize * 8;
2064  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2065  ptrdiff_t wrap_y, wrap_c;
2066 
2067  for (i = 0; i < mb_block_count; i++)
2068  skip_dct[i] = s->skipdct;
2069 
2070  if (s->adaptive_quant) {
2071  const int last_qp = s->qscale;
2072  const int mb_xy = mb_x + mb_y * s->mb_stride;
2073 
2074  s->lambda = s->lambda_table[mb_xy];
2075  update_qscale(s);
2076 
2077  if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2078  s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2079  s->dquant = s->qscale - last_qp;
2080 
2081  if (s->out_format == FMT_H263) {
2082  s->dquant = av_clip(s->dquant, -2, 2);
2083 
2084  if (s->codec_id == AV_CODEC_ID_MPEG4) {
2085  if (!s->mb_intra) {
2086  if (s->pict_type == AV_PICTURE_TYPE_B) {
2087  if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2088  s->dquant = 0;
2089  }
2090  if (s->mv_type == MV_TYPE_8X8)
2091  s->dquant = 0;
2092  }
2093  }
2094  }
2095  }
2096  ff_set_qscale(s, last_qp + s->dquant);
2097  } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2098  ff_set_qscale(s, s->qscale + s->dquant);
2099 
2100  wrap_y = s->linesize;
2101  wrap_c = s->uvlinesize;
2102  ptr_y = s->new_picture.f->data[0] +
2103  (mb_y * 16 * wrap_y) + mb_x * 16;
2104  ptr_cb = s->new_picture.f->data[1] +
2105  (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2106  ptr_cr = s->new_picture.f->data[2] +
2107  (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2108 
2109  if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
2110  uint8_t *ebuf = s->sc.edge_emu_buffer + 36 * wrap_y;
2111  int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
2112  int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
2113  s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2114  wrap_y, wrap_y,
2115  16, 16, mb_x * 16, mb_y * 16,
2116  s->width, s->height);
2117  ptr_y = ebuf;
2118  s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2119  wrap_c, wrap_c,
2120  mb_block_width, mb_block_height,
2121  mb_x * mb_block_width, mb_y * mb_block_height,
2122  cw, ch);
2123  ptr_cb = ebuf + 16 * wrap_y;
2124  s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2125  wrap_c, wrap_c,
2126  mb_block_width, mb_block_height,
2127  mb_x * mb_block_width, mb_y * mb_block_height,
2128  cw, ch);
2129  ptr_cr = ebuf + 16 * wrap_y + 16;
2130  }
2131 
2132  if (s->mb_intra) {
2134  int progressive_score, interlaced_score;
2135 
2136  s->interlaced_dct = 0;
2137  progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2138  s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2139  NULL, wrap_y, 8) - 400;
2140 
2141  if (progressive_score > 0) {
2142  interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2143  NULL, wrap_y * 2, 8) +
2144  s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2145  NULL, wrap_y * 2, 8);
2146  if (progressive_score > interlaced_score) {
2147  s->interlaced_dct = 1;
2148 
2149  dct_offset = wrap_y;
2150  uv_dct_offset = wrap_c;
2151  wrap_y <<= 1;
2152  if (s->chroma_format == CHROMA_422 ||
2153  s->chroma_format == CHROMA_444)
2154  wrap_c <<= 1;
2155  }
2156  }
2157  }
2158 
2159  s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2160  s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2161  s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2162  s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2163 
2164  if (s->avctx->flags & CODEC_FLAG_GRAY) {
2165  skip_dct[4] = 1;
2166  skip_dct[5] = 1;
2167  } else {
2168  s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2169  s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2170  if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
2171  s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2172  s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2173  } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
2174  s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2175  s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2176  s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2177  s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2178  s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2179  s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2180  }
2181  }
2182  } else {
2183  op_pixels_func (*op_pix)[4];
2184  qpel_mc_func (*op_qpix)[16];
2185  uint8_t *dest_y, *dest_cb, *dest_cr;
2186 
2187  dest_y = s->dest[0];
2188  dest_cb = s->dest[1];
2189  dest_cr = s->dest[2];
2190 
2191  if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2192  op_pix = s->hdsp.put_pixels_tab;
2193  op_qpix = s->qdsp.put_qpel_pixels_tab;
2194  } else {
2195  op_pix = s->hdsp.put_no_rnd_pixels_tab;
2196  op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2197  }
2198 
2199  if (s->mv_dir & MV_DIR_FORWARD) {
2200  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2201  s->last_picture.f->data,
2202  op_pix, op_qpix);
2203  op_pix = s->hdsp.avg_pixels_tab;
2204  op_qpix = s->qdsp.avg_qpel_pixels_tab;
2205  }
2206  if (s->mv_dir & MV_DIR_BACKWARD) {
2207  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2208  s->next_picture.f->data,
2209  op_pix, op_qpix);
2210  }
2211 
2213  int progressive_score, interlaced_score;
2214 
2215  s->interlaced_dct = 0;
2216  progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2217  s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2218  ptr_y + wrap_y * 8,
2219  wrap_y, 8) - 400;
2220 
2221  if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2222  progressive_score -= 400;
2223 
2224  if (progressive_score > 0) {
2225  interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2226  wrap_y * 2, 8) +
2227  s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2228  ptr_y + wrap_y,
2229  wrap_y * 2, 8);
2230 
2231  if (progressive_score > interlaced_score) {
2232  s->interlaced_dct = 1;
2233 
2234  dct_offset = wrap_y;
2235  uv_dct_offset = wrap_c;
2236  wrap_y <<= 1;
2237  if (s->chroma_format == CHROMA_422)
2238  wrap_c <<= 1;
2239  }
2240  }
2241  }
2242 
2243  s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2244  s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2245  s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2246  dest_y + dct_offset, wrap_y);
2247  s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2248  dest_y + dct_offset + 8, wrap_y);
2249 
2250  if (s->avctx->flags & CODEC_FLAG_GRAY) {
2251  skip_dct[4] = 1;
2252  skip_dct[5] = 1;
2253  } else {
2254  s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2255  s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2256  if (!s->chroma_y_shift) { /* 422 */
2257  s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2258  dest_cb + uv_dct_offset, wrap_c);
2259  s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2260  dest_cr + uv_dct_offset, wrap_c);
2261  }
2262  }
2263  /* pre quantization */
2264  if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2265  2 * s->qscale * s->qscale) {
2266  // FIXME optimize
2267  if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2268  skip_dct[0] = 1;
2269  if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2270  skip_dct[1] = 1;
2271  if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2272  wrap_y, 8) < 20 * s->qscale)
2273  skip_dct[2] = 1;
2274  if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2275  wrap_y, 8) < 20 * s->qscale)
2276  skip_dct[3] = 1;
2277  if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2278  skip_dct[4] = 1;
2279  if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2280  skip_dct[5] = 1;
2281  if (!s->chroma_y_shift) { /* 422 */
2282  if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
2283  dest_cb + uv_dct_offset,
2284  wrap_c, 8) < 20 * s->qscale)
2285  skip_dct[6] = 1;
2286  if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
2287  dest_cr + uv_dct_offset,
2288  wrap_c, 8) < 20 * s->qscale)
2289  skip_dct[7] = 1;
2290  }
2291  }
2292  }
2293 
2294  if (s->quantizer_noise_shaping) {
2295  if (!skip_dct[0])
2296  get_visual_weight(weight[0], ptr_y , wrap_y);
2297  if (!skip_dct[1])
2298  get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2299  if (!skip_dct[2])
2300  get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2301  if (!skip_dct[3])
2302  get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2303  if (!skip_dct[4])
2304  get_visual_weight(weight[4], ptr_cb , wrap_c);
2305  if (!skip_dct[5])
2306  get_visual_weight(weight[5], ptr_cr , wrap_c);
2307  if (!s->chroma_y_shift) { /* 422 */
2308  if (!skip_dct[6])
2309  get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2310  wrap_c);
2311  if (!skip_dct[7])
2312  get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2313  wrap_c);
2314  }
2315  memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2316  }
2317 
2318  /* DCT & quantize */
2319  av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2320  {
2321  for (i = 0; i < mb_block_count; i++) {
2322  if (!skip_dct[i]) {
2323  int overflow;
2324  s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2325  // FIXME we could decide to change to quantizer instead of
2326  // clipping
2327  // JS: I don't think that would be a good idea it could lower
2328  // quality instead of improve it. Just INTRADC clipping
2329  // deserves changes in quantizer
2330  if (overflow)
2331  clip_coeffs(s, s->block[i], s->block_last_index[i]);
2332  } else
2333  s->block_last_index[i] = -1;
2334  }
2335  if (s->quantizer_noise_shaping) {
2336  for (i = 0; i < mb_block_count; i++) {
2337  if (!skip_dct[i]) {
2338  s->block_last_index[i] =
2339  dct_quantize_refine(s, s->block[i], weight[i],
2340  orig[i], i, s->qscale);
2341  }
2342  }
2343  }
2344 
2345  if (s->luma_elim_threshold && !s->mb_intra)
2346  for (i = 0; i < 4; i++)
2348  if (s->chroma_elim_threshold && !s->mb_intra)
2349  for (i = 4; i < mb_block_count; i++)
2351 
2352  if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2353  for (i = 0; i < mb_block_count; i++) {
2354  if (s->block_last_index[i] == -1)
2355  s->coded_score[i] = INT_MAX / 256;
2356  }
2357  }
2358  }
2359 
2360  if ((s->avctx->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2361  s->block_last_index[4] =
2362  s->block_last_index[5] = 0;
2363  s->block[4][0] =
2364  s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2365  if (!s->chroma_y_shift) { /* 422 / 444 */
2366  for (i=6; i<12; i++) {
2367  s->block_last_index[i] = 0;
2368  s->block[i][0] = s->block[4][0];
2369  }
2370  }
2371  }
2372 
2373  // non c quantize code returns incorrect block_last_index FIXME
2374  if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2375  for (i = 0; i < mb_block_count; i++) {
2376  int j;
2377  if (s->block_last_index[i] > 0) {
2378  for (j = 63; j > 0; j--) {
2379  if (s->block[i][s->intra_scantable.permutated[j]])
2380  break;
2381  }
2382  s->block_last_index[i] = j;
2383  }
2384  }
2385  }
2386 
2387  /* huffman encode */
2388  switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2391  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2392  ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2393  break;
2394  case AV_CODEC_ID_MPEG4:
2395  if (CONFIG_MPEG4_ENCODER)
2396  ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2397  break;
2398  case AV_CODEC_ID_MSMPEG4V2:
2399  case AV_CODEC_ID_MSMPEG4V3:
2400  case AV_CODEC_ID_WMV1:
2402  ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2403  break;
2404  case AV_CODEC_ID_WMV2:
2405  if (CONFIG_WMV2_ENCODER)
2406  ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2407  break;
2408  case AV_CODEC_ID_H261:
2409  if (CONFIG_H261_ENCODER)
2410  ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2411  break;
2412  case AV_CODEC_ID_H263:
2413  case AV_CODEC_ID_H263P:
2414  case AV_CODEC_ID_FLV1:
2415  case AV_CODEC_ID_RV10:
2416  case AV_CODEC_ID_RV20:
2417  if (CONFIG_H263_ENCODER)
2418  ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2419  break;
2420  case AV_CODEC_ID_MJPEG:
2421  case AV_CODEC_ID_AMV:
2422  if (CONFIG_MJPEG_ENCODER)
2423  ff_mjpeg_encode_mb(s, s->block);
2424  break;
2425  default:
2426  av_assert1(0);
2427  }
2428 }
2429 
2430 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2431 {
2432  if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 8, 6);
2433  else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
2434  else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
2435 }
2436 
2438  int i;
2439 
2440  memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2441 
2442  /* mpeg1 */
2443  d->mb_skip_run= s->mb_skip_run;
2444  for(i=0; i<3; i++)
2445  d->last_dc[i] = s->last_dc[i];
2446 
2447  /* statistics */
2448  d->mv_bits= s->mv_bits;
2449  d->i_tex_bits= s->i_tex_bits;
2450  d->p_tex_bits= s->p_tex_bits;
2451  d->i_count= s->i_count;
2452  d->f_count= s->f_count;
2453  d->b_count= s->b_count;
2454  d->skip_count= s->skip_count;
2455  d->misc_bits= s->misc_bits;
2456  d->last_bits= 0;
2457 
2458  d->mb_skipped= 0;
2459  d->qscale= s->qscale;
2460  d->dquant= s->dquant;
2461 
2463 }
2464 
2466  int i;
2467 
2468  memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2469  memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2470 
2471  /* mpeg1 */
2472  d->mb_skip_run= s->mb_skip_run;
2473  for(i=0; i<3; i++)
2474  d->last_dc[i] = s->last_dc[i];
2475 
2476  /* statistics */
2477  d->mv_bits= s->mv_bits;
2478  d->i_tex_bits= s->i_tex_bits;
2479  d->p_tex_bits= s->p_tex_bits;
2480  d->i_count= s->i_count;
2481  d->f_count= s->f_count;
2482  d->b_count= s->b_count;
2483  d->skip_count= s->skip_count;
2484  d->misc_bits= s->misc_bits;
2485 
2486  d->mb_intra= s->mb_intra;
2487  d->mb_skipped= s->mb_skipped;
2488  d->mv_type= s->mv_type;
2489  d->mv_dir= s->mv_dir;
2490  d->pb= s->pb;
2491  if(s->data_partitioning){
2492  d->pb2= s->pb2;
2493  d->tex_pb= s->tex_pb;
2494  }
2495  d->block= s->block;
2496  for(i=0; i<8; i++)
2497  d->block_last_index[i]= s->block_last_index[i];
2499  d->qscale= s->qscale;
2500 
2502 }
2503 
2504 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2506  int *dmin, int *next_block, int motion_x, int motion_y)
2507 {
2508  int score;
2509  uint8_t *dest_backup[3];
2510 
2511  copy_context_before_encode(s, backup, type);
2512 
2513  s->block= s->blocks[*next_block];
2514  s->pb= pb[*next_block];
2515  if(s->data_partitioning){
2516  s->pb2 = pb2 [*next_block];
2517  s->tex_pb= tex_pb[*next_block];
2518  }
2519 
2520  if(*next_block){
2521  memcpy(dest_backup, s->dest, sizeof(s->dest));
2522  s->dest[0] = s->sc.rd_scratchpad;
2523  s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2524  s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2525  av_assert0(s->linesize >= 32); //FIXME
2526  }
2527 
2528  encode_mb(s, motion_x, motion_y);
2529 
2530  score= put_bits_count(&s->pb);
2531  if(s->data_partitioning){
2532  score+= put_bits_count(&s->pb2);
2533  score+= put_bits_count(&s->tex_pb);
2534  }
2535 
2536  if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2537  ff_mpv_decode_mb(s, s->block);
2538 
2539  score *= s->lambda2;
2540  score += sse_mb(s) << FF_LAMBDA_SHIFT;
2541  }
2542 
2543  if(*next_block){
2544  memcpy(s->dest, dest_backup, sizeof(s->dest));
2545  }
2546 
2547  if(score<*dmin){
2548  *dmin= score;
2549  *next_block^=1;
2550 
2551  copy_context_after_encode(best, s, type);
2552  }
2553 }
2554 
2555 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2556  uint32_t *sq = ff_square_tab + 256;
2557  int acc=0;
2558  int x,y;
2559 
2560  if(w==16 && h==16)
2561  return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2562  else if(w==8 && h==8)
2563  return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2564 
2565  for(y=0; y<h; y++){
2566  for(x=0; x<w; x++){
2567  acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2568  }
2569  }
2570 
2571  av_assert2(acc>=0);
2572 
2573  return acc;
2574 }
2575 
2576 static int sse_mb(MpegEncContext *s){
2577  int w= 16;
2578  int h= 16;
2579 
2580  if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2581  if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2582 
2583  if(w==16 && h==16)
2584  if(s->avctx->mb_cmp == FF_CMP_NSSE){
2585  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) +
2586  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) +
2587  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);
2588  }else{
2589  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) +
2590  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) +
2591  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);
2592  }
2593  else
2594  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)
2595  +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)
2596  +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);
2597 }
2598 
2600  MpegEncContext *s= *(void**)arg;
2601 
2602 
2603  s->me.pre_pass=1;
2604  s->me.dia_size= s->avctx->pre_dia_size;
2605  s->first_slice_line=1;
2606  for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2607  for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2609  }
2610  s->first_slice_line=0;
2611  }
2612 
2613  s->me.pre_pass=0;
2614 
2615  return 0;
2616 }
2617 
2619  MpegEncContext *s= *(void**)arg;
2620 
2622 
2623  s->me.dia_size= s->avctx->dia_size;
2624  s->first_slice_line=1;
2625  for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2626  s->mb_x=0; //for block init below
2628  for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2629  s->block_index[0]+=2;
2630  s->block_index[1]+=2;
2631  s->block_index[2]+=2;
2632  s->block_index[3]+=2;
2633 
2634  /* compute motion vector & mb_type and store in context */
2637  else
2639  }
2640  s->first_slice_line=0;
2641  }
2642  return 0;
2643 }
2644 
2645 static int mb_var_thread(AVCodecContext *c, void *arg){
2646  MpegEncContext *s= *(void**)arg;
2647  int mb_x, mb_y;
2648 
2650 
2651  for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2652  for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2653  int xx = mb_x * 16;
2654  int yy = mb_y * 16;
2655  uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2656  int varc;
2657  int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2658 
2659  varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2660  (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2661 
2662  s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2663  s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2664  s->me.mb_var_sum_temp += varc;
2665  }
2666  }
2667  return 0;
2668 }
2669 
2671  if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2672  if(s->partitioned_frame){
2674  }
2675 
2676  ff_mpeg4_stuffing(&s->pb);
2677  }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2679  }
2680 
2682  flush_put_bits(&s->pb);
2683 
2684  if ((s->avctx->flags & CODEC_FLAG_PASS1) && !s->partitioned_frame)
2685  s->misc_bits+= get_bits_diff(s);
2686 }
2687 
2689 {
2690  uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2691  int offset = put_bits_count(&s->pb);
2692  int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2693  int gobn = s->mb_y / s->gob_index;
2694  int pred_x, pred_y;
2695  if (CONFIG_H263_ENCODER)
2696  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2697  bytestream_put_le32(&ptr, offset);
2698  bytestream_put_byte(&ptr, s->qscale);
2699  bytestream_put_byte(&ptr, gobn);
2700  bytestream_put_le16(&ptr, mba);
2701  bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2702  bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2703  /* 4MV not implemented */
2704  bytestream_put_byte(&ptr, 0); /* hmv2 */
2705  bytestream_put_byte(&ptr, 0); /* vmv2 */
2706 }
2707 
2708 static void update_mb_info(MpegEncContext *s, int startcode)
2709 {
2710  if (!s->mb_info)
2711  return;
2712  if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2713  s->mb_info_size += 12;
2714  s->prev_mb_info = s->last_mb_info;
2715  }
2716  if (startcode) {
2717  s->prev_mb_info = put_bits_count(&s->pb)/8;
2718  /* This might have incremented mb_info_size above, and we return without
2719  * actually writing any info into that slot yet. But in that case,
2720  * this will be called again at the start of the after writing the
2721  * start code, actually writing the mb info. */
2722  return;
2723  }
2724 
2725  s->last_mb_info = put_bits_count(&s->pb)/8;
2726  if (!s->mb_info_size)
2727  s->mb_info_size += 12;
2728  write_mb_info(s);
2729 }
2730 
2731 int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
2732 {
2733  if ( s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold
2734  && s->slice_context_count == 1
2735  && s->pb.buf == s->avctx->internal->byte_buffer) {
2736  int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2737  int vbv_pos = s->vbv_delay_ptr - s->pb.buf;
2738 
2739  uint8_t *new_buffer = NULL;
2740  int new_buffer_size = 0;
2741 
2742  av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2743  s->avctx->internal->byte_buffer_size + size_increase);
2744  if (!new_buffer)
2745  return AVERROR(ENOMEM);
2746 
2747  memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
2749  s->avctx->internal->byte_buffer = new_buffer;
2750  s->avctx->internal->byte_buffer_size = new_buffer_size;
2751  rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2752  s->ptr_lastgob = s->pb.buf + lastgob_pos;
2753  s->vbv_delay_ptr = s->pb.buf + vbv_pos;
2754  }
2755  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold)
2756  return AVERROR(EINVAL);
2757  return 0;
2758 }
2759 
2760 static int encode_thread(AVCodecContext *c, void *arg){
2761  MpegEncContext *s= *(void**)arg;
2762  int mb_x, mb_y, pdif = 0;
2763  int chr_h= 16>>s->chroma_y_shift;
2764  int i, j;
2765  MpegEncContext best_s = { 0 }, backup_s;
2766  uint8_t bit_buf[2][MAX_MB_BYTES];
2767  uint8_t bit_buf2[2][MAX_MB_BYTES];
2768  uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2769  PutBitContext pb[2], pb2[2], tex_pb[2];
2770 
2772 
2773  for(i=0; i<2; i++){
2774  init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2775  init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2776  init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2777  }
2778 
2779  s->last_bits= put_bits_count(&s->pb);
2780  s->mv_bits=0;
2781  s->misc_bits=0;
2782  s->i_tex_bits=0;
2783  s->p_tex_bits=0;
2784  s->i_count=0;
2785  s->f_count=0;
2786  s->b_count=0;
2787  s->skip_count=0;
2788 
2789  for(i=0; i<3; i++){
2790  /* init last dc values */
2791  /* note: quant matrix value (8) is implied here */
2792  s->last_dc[i] = 128 << s->intra_dc_precision;
2793 
2794  s->current_picture.error[i] = 0;
2795  }
2796  if(s->codec_id==AV_CODEC_ID_AMV){
2797  s->last_dc[0] = 128*8/13;
2798  s->last_dc[1] = 128*8/14;
2799  s->last_dc[2] = 128*8/14;
2800  }
2801  s->mb_skip_run = 0;
2802  memset(s->last_mv, 0, sizeof(s->last_mv));
2803 
2804  s->last_mv_dir = 0;
2805 
2806  switch(s->codec_id){
2807  case AV_CODEC_ID_H263:
2808  case AV_CODEC_ID_H263P:
2809  case AV_CODEC_ID_FLV1:
2810  if (CONFIG_H263_ENCODER)
2811  s->gob_index = H263_GOB_HEIGHT(s->height);
2812  break;
2813  case AV_CODEC_ID_MPEG4:
2814  if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2816  break;
2817  }
2818 
2819  s->resync_mb_x=0;
2820  s->resync_mb_y=0;
2821  s->first_slice_line = 1;
2822  s->ptr_lastgob = s->pb.buf;
2823  for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2824  s->mb_x=0;
2825  s->mb_y= mb_y;
2826 
2827  ff_set_qscale(s, s->qscale);
2829 
2830  for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2831  int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2832  int mb_type= s->mb_type[xy];
2833 // int d;
2834  int dmin= INT_MAX;
2835  int dir;
2836  int size_increase = s->avctx->internal->byte_buffer_size/4
2837  + s->mb_width*MAX_MB_BYTES;
2838 
2839  ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
2840  if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2841  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2842  return -1;
2843  }
2844  if(s->data_partitioning){
2845  if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2846  || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2847  av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2848  return -1;
2849  }
2850  }
2851 
2852  s->mb_x = mb_x;
2853  s->mb_y = mb_y; // moved into loop, can get changed by H.261
2855 
2856  if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2858  xy= s->mb_y*s->mb_stride + s->mb_x;
2859  mb_type= s->mb_type[xy];
2860  }
2861 
2862  /* write gob / video packet header */
2863  if(s->rtp_mode){
2864  int current_packet_size, is_gob_start;
2865 
2866  current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2867 
2868  is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2869 
2870  if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2871 
2872  switch(s->codec_id){
2873  case AV_CODEC_ID_H263:
2874  case AV_CODEC_ID_H263P:
2875  if(!s->h263_slice_structured)
2876  if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2877  break;
2879  if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2881  if(s->mb_skip_run) is_gob_start=0;
2882  break;
2883  case AV_CODEC_ID_MJPEG:
2884  if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2885  break;
2886  }
2887 
2888  if(is_gob_start){
2889  if(s->start_mb_y != mb_y || mb_x!=0){
2890  write_slice_end(s);
2891 
2892  if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2894  }
2895  }
2896 
2897  av_assert2((put_bits_count(&s->pb)&7) == 0);
2898  current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2899 
2900  if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2901  int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2902  int d = 100 / s->error_rate;
2903  if(r % d == 0){
2904  current_packet_size=0;
2905  s->pb.buf_ptr= s->ptr_lastgob;
2906  assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2907  }
2908  }
2909 
2910  if (s->avctx->rtp_callback){
2911  int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2912  s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2913  }
2914  update_mb_info(s, 1);
2915 
2916  switch(s->codec_id){
2917  case AV_CODEC_ID_MPEG4:
2918  if (CONFIG_MPEG4_ENCODER) {
2921  }
2922  break;
2925  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2928  }
2929  break;
2930  case AV_CODEC_ID_H263:
2931  case AV_CODEC_ID_H263P:
2932  if (CONFIG_H263_ENCODER)
2933  ff_h263_encode_gob_header(s, mb_y);
2934  break;
2935  }
2936 
2937  if (s->avctx->flags & CODEC_FLAG_PASS1) {
2938  int bits= put_bits_count(&s->pb);
2939  s->misc_bits+= bits - s->last_bits;
2940  s->last_bits= bits;
2941  }
2942 
2943  s->ptr_lastgob += current_packet_size;
2944  s->first_slice_line=1;
2945  s->resync_mb_x=mb_x;
2946  s->resync_mb_y=mb_y;
2947  }
2948  }
2949 
2950  if( (s->resync_mb_x == s->mb_x)
2951  && s->resync_mb_y+1 == s->mb_y){
2952  s->first_slice_line=0;
2953  }
2954 
2955  s->mb_skipped=0;
2956  s->dquant=0; //only for QP_RD
2957 
2958  update_mb_info(s, 0);
2959 
2960  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
2961  int next_block=0;
2962  int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2963 
2964  copy_context_before_encode(&backup_s, s, -1);
2965  backup_s.pb= s->pb;
2968  if(s->data_partitioning){
2969  backup_s.pb2= s->pb2;
2970  backup_s.tex_pb= s->tex_pb;
2971  }
2972 
2973  if(mb_type&CANDIDATE_MB_TYPE_INTER){
2974  s->mv_dir = MV_DIR_FORWARD;
2975  s->mv_type = MV_TYPE_16X16;
2976  s->mb_intra= 0;
2977  s->mv[0][0][0] = s->p_mv_table[xy][0];
2978  s->mv[0][0][1] = s->p_mv_table[xy][1];
2979  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2980  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2981  }
2982  if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2983  s->mv_dir = MV_DIR_FORWARD;
2984  s->mv_type = MV_TYPE_FIELD;
2985  s->mb_intra= 0;
2986  for(i=0; i<2; i++){
2987  j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2988  s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2989  s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2990  }
2991  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2992  &dmin, &next_block, 0, 0);
2993  }
2994  if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2995  s->mv_dir = MV_DIR_FORWARD;
2996  s->mv_type = MV_TYPE_16X16;
2997  s->mb_intra= 0;
2998  s->mv[0][0][0] = 0;
2999  s->mv[0][0][1] = 0;
3000  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3001  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3002  }
3003  if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3004  s->mv_dir = MV_DIR_FORWARD;
3005  s->mv_type = MV_TYPE_8X8;
3006  s->mb_intra= 0;
3007  for(i=0; i<4; i++){
3008  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3009  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3010  }
3011  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3012  &dmin, &next_block, 0, 0);
3013  }
3014  if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3015  s->mv_dir = MV_DIR_FORWARD;
3016  s->mv_type = MV_TYPE_16X16;
3017  s->mb_intra= 0;
3018  s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3019  s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3020  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3021  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3022  }
3023  if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3024  s->mv_dir = MV_DIR_BACKWARD;
3025  s->mv_type = MV_TYPE_16X16;
3026  s->mb_intra= 0;
3027  s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3028  s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3029  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3030  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3031  }
3032  if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3034  s->mv_type = MV_TYPE_16X16;
3035  s->mb_intra= 0;
3036  s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3037  s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3038  s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3039  s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3040  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3041  &dmin, &next_block, 0, 0);
3042  }
3043  if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3044  s->mv_dir = MV_DIR_FORWARD;
3045  s->mv_type = MV_TYPE_FIELD;
3046  s->mb_intra= 0;
3047  for(i=0; i<2; i++){
3048  j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3049  s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3050  s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3051  }
3052  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3053  &dmin, &next_block, 0, 0);
3054  }
3055  if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3056  s->mv_dir = MV_DIR_BACKWARD;
3057  s->mv_type = MV_TYPE_FIELD;
3058  s->mb_intra= 0;
3059  for(i=0; i<2; i++){
3060  j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3061  s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3062  s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3063  }
3064  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3065  &dmin, &next_block, 0, 0);
3066  }
3067  if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3069  s->mv_type = MV_TYPE_FIELD;
3070  s->mb_intra= 0;
3071  for(dir=0; dir<2; dir++){
3072  for(i=0; i<2; i++){
3073  j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3074  s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3075  s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3076  }
3077  }
3078  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3079  &dmin, &next_block, 0, 0);
3080  }
3081  if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3082  s->mv_dir = 0;
3083  s->mv_type = MV_TYPE_16X16;
3084  s->mb_intra= 1;
3085  s->mv[0][0][0] = 0;
3086  s->mv[0][0][1] = 0;
3087  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3088  &dmin, &next_block, 0, 0);
3089  if(s->h263_pred || s->h263_aic){
3090  if(best_s.mb_intra)
3091  s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3092  else
3093  ff_clean_intra_table_entries(s); //old mode?
3094  }
3095  }
3096 
3097  if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3098  if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
3099  const int last_qp= backup_s.qscale;
3100  int qpi, qp, dc[6];
3101  int16_t ac[6][16];
3102  const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3103  static const int dquant_tab[4]={-1,1,-2,2};
3104  int storecoefs = s->mb_intra && s->dc_val[0];
3105 
3106  av_assert2(backup_s.dquant == 0);
3107 
3108  //FIXME intra
3109  s->mv_dir= best_s.mv_dir;
3110  s->mv_type = MV_TYPE_16X16;
3111  s->mb_intra= best_s.mb_intra;
3112  s->mv[0][0][0] = best_s.mv[0][0][0];
3113  s->mv[0][0][1] = best_s.mv[0][0][1];
3114  s->mv[1][0][0] = best_s.mv[1][0][0];
3115  s->mv[1][0][1] = best_s.mv[1][0][1];
3116 
3117  qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3118  for(; qpi<4; qpi++){
3119  int dquant= dquant_tab[qpi];
3120  qp= last_qp + dquant;
3121  if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3122  continue;
3123  backup_s.dquant= dquant;
3124  if(storecoefs){
3125  for(i=0; i<6; i++){
3126  dc[i]= s->dc_val[0][ s->block_index[i] ];
3127  memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3128  }
3129  }
3130 
3131  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3132  &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3133  if(best_s.qscale != qp){
3134  if(storecoefs){
3135  for(i=0; i<6; i++){
3136  s->dc_val[0][ s->block_index[i] ]= dc[i];
3137  memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3138  }
3139  }
3140  }
3141  }
3142  }
3143  }
3144  if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3145  int mx= s->b_direct_mv_table[xy][0];
3146  int my= s->b_direct_mv_table[xy][1];
3147 
3148  backup_s.dquant = 0;
3150  s->mb_intra= 0;
3151  ff_mpeg4_set_direct_mv(s, mx, my);
3152  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3153  &dmin, &next_block, mx, my);
3154  }
3155  if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3156  backup_s.dquant = 0;
3158  s->mb_intra= 0;
3159  ff_mpeg4_set_direct_mv(s, 0, 0);
3160  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3161  &dmin, &next_block, 0, 0);
3162  }
3163  if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3164  int coded=0;
3165  for(i=0; i<6; i++)
3166  coded |= s->block_last_index[i];
3167  if(coded){
3168  int mx,my;
3169  memcpy(s->mv, best_s.mv, sizeof(s->mv));
3170  if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3171  mx=my=0; //FIXME find the one we actually used
3172  ff_mpeg4_set_direct_mv(s, mx, my);
3173  }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3174  mx= s->mv[1][0][0];
3175  my= s->mv[1][0][1];
3176  }else{
3177  mx= s->mv[0][0][0];
3178  my= s->mv[0][0][1];
3179  }
3180 
3181  s->mv_dir= best_s.mv_dir;
3182  s->mv_type = best_s.mv_type;
3183  s->mb_intra= 0;
3184 /* s->mv[0][0][0] = best_s.mv[0][0][0];
3185  s->mv[0][0][1] = best_s.mv[0][0][1];
3186  s->mv[1][0][0] = best_s.mv[1][0][0];
3187  s->mv[1][0][1] = best_s.mv[1][0][1];*/
3188  backup_s.dquant= 0;
3189  s->skipdct=1;
3190  encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3191  &dmin, &next_block, mx, my);
3192  s->skipdct=0;
3193  }
3194  }
3195 
3196  s->current_picture.qscale_table[xy] = best_s.qscale;
3197 
3198  copy_context_after_encode(s, &best_s, -1);
3199 
3200  pb_bits_count= put_bits_count(&s->pb);
3201  flush_put_bits(&s->pb);
3202  avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3203  s->pb= backup_s.pb;
3204 
3205  if(s->data_partitioning){
3206  pb2_bits_count= put_bits_count(&s->pb2);
3207  flush_put_bits(&s->pb2);
3208  avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3209  s->pb2= backup_s.pb2;
3210 
3211  tex_pb_bits_count= put_bits_count(&s->tex_pb);
3212  flush_put_bits(&s->tex_pb);
3213  avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3214  s->tex_pb= backup_s.tex_pb;
3215  }
3216  s->last_bits= put_bits_count(&s->pb);
3217 
3218  if (CONFIG_H263_ENCODER &&
3221 
3222  if(next_block==0){ //FIXME 16 vs linesize16
3223  s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
3224  s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
3225  s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3226  }
3227 
3229  ff_mpv_decode_mb(s, s->block);
3230  } else {
3231  int motion_x = 0, motion_y = 0;
3233  // only one MB-Type possible
3234 
3235  switch(mb_type){
3237  s->mv_dir = 0;
3238  s->mb_intra= 1;
3239  motion_x= s->mv[0][0][0] = 0;
3240  motion_y= s->mv[0][0][1] = 0;
3241  break;
3243  s->mv_dir = MV_DIR_FORWARD;
3244  s->mb_intra= 0;
3245  motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3246  motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3247  break;
3249  s->mv_dir = MV_DIR_FORWARD;
3250  s->mv_type = MV_TYPE_FIELD;
3251  s->mb_intra= 0;
3252  for(i=0; i<2; i++){
3253  j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3254  s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3255  s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3256  }
3257  break;
3259  s->mv_dir = MV_DIR_FORWARD;
3260  s->mv_type = MV_TYPE_8X8;
3261  s->mb_intra= 0;
3262  for(i=0; i<4; i++){
3263  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3264  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3265  }
3266  break;
3268  if (CONFIG_MPEG4_ENCODER) {
3270  s->mb_intra= 0;
3271  motion_x=s->b_direct_mv_table[xy][0];
3272  motion_y=s->b_direct_mv_table[xy][1];
3273  ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3274  }
3275  break;
3277  if (CONFIG_MPEG4_ENCODER) {
3279  s->mb_intra= 0;
3280  ff_mpeg4_set_direct_mv(s, 0, 0);
3281  }
3282  break;
3285  s->mb_intra= 0;
3286  s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3287  s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3288  s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3289  s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3290  break;
3292  s->mv_dir = MV_DIR_BACKWARD;
3293  s->mb_intra= 0;
3294  motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3295  motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3296  break;
3298  s->mv_dir = MV_DIR_FORWARD;
3299  s->mb_intra= 0;
3300  motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3301  motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3302  break;
3304  s->mv_dir = MV_DIR_FORWARD;
3305  s->mv_type = MV_TYPE_FIELD;
3306  s->mb_intra= 0;
3307  for(i=0; i<2; i++){
3308  j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3309  s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3310  s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3311  }
3312  break;
3314  s->mv_dir = MV_DIR_BACKWARD;
3315  s->mv_type = MV_TYPE_FIELD;
3316  s->mb_intra= 0;
3317  for(i=0; i<2; i++){
3318  j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3319  s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3320  s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3321  }
3322  break;
3325  s->mv_type = MV_TYPE_FIELD;
3326  s->mb_intra= 0;
3327  for(dir=0; dir<2; dir++){
3328  for(i=0; i<2; i++){
3329  j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3330  s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3331  s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3332  }
3333  }
3334  break;
3335  default:
3336  av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3337  }
3338 
3339  encode_mb(s, motion_x, motion_y);
3340 
3341  // RAL: Update last macroblock type
3342  s->last_mv_dir = s->mv_dir;
3343 
3344  if (CONFIG_H263_ENCODER &&
3347 
3348  ff_mpv_decode_mb(s, s->block);
3349  }
3350 
3351  /* clean the MV table in IPS frames for direct mode in B frames */
3352  if(s->mb_intra /* && I,P,S_TYPE */){
3353  s->p_mv_table[xy][0]=0;
3354  s->p_mv_table[xy][1]=0;
3355  }
3356 
3357  if (s->avctx->flags & CODEC_FLAG_PSNR) {
3358  int w= 16;
3359  int h= 16;
3360 
3361  if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3362  if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3363 
3364  s->current_picture.error[0] += sse(
3365  s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3366  s->dest[0], w, h, s->linesize);
3367  s->current_picture.error[1] += sse(
3368  s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3369  s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3370  s->current_picture.error[2] += sse(
3371  s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3372  s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3373  }
3374  if(s->loop_filter){
3375  if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3377  }
3378  ff_dlog(s->avctx, "MB %d %d bits\n",
3379  s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3380  }
3381  }
3382 
3383  //not beautiful here but we must write it before flushing so it has to be here
3386 
3387  write_slice_end(s);
3388 
3389  /* Send the last GOB if RTP */
3390  if (s->avctx->rtp_callback) {
3391  int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3392  pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3393  /* Call the RTP callback to send the last GOB */
3394  emms_c();
3395  s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3396  }
3397 
3398  return 0;
3399 }
3400 
3401 #define MERGE(field) dst->field += src->field; src->field=0
3403  MERGE(me.scene_change_score);
3404  MERGE(me.mc_mb_var_sum_temp);
3405  MERGE(me.mb_var_sum_temp);
3406 }
3407 
3409  int i;
3410 
3411  MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3412  MERGE(dct_count[1]);
3413  MERGE(mv_bits);
3414  MERGE(i_tex_bits);
3415  MERGE(p_tex_bits);
3416  MERGE(i_count);
3417  MERGE(f_count);
3418  MERGE(b_count);
3419  MERGE(skip_count);
3420  MERGE(misc_bits);
3421  MERGE(er.error_count);
3426 
3427  if(dst->avctx->noise_reduction){
3428  for(i=0; i<64; i++){
3429  MERGE(dct_error_sum[0][i]);
3430  MERGE(dct_error_sum[1][i]);
3431  }
3432  }
3433 
3434  assert(put_bits_count(&src->pb) % 8 ==0);
3435  assert(put_bits_count(&dst->pb) % 8 ==0);
3436  avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3437  flush_put_bits(&dst->pb);
3438 }
3439 
3440 static int estimate_qp(MpegEncContext *s, int dry_run){
3441  if (s->next_lambda){
3444  if(!dry_run) s->next_lambda= 0;
3445  } else if (!s->fixed_qscale) {
3448  if (s->current_picture.f->quality < 0)
3449  return -1;
3450  }
3451 
3452  if(s->adaptive_quant){
3453  switch(s->codec_id){
3454  case AV_CODEC_ID_MPEG4:
3455  if (CONFIG_MPEG4_ENCODER)
3457  break;
3458  case AV_CODEC_ID_H263:
3459  case AV_CODEC_ID_H263P:
3460  case AV_CODEC_ID_FLV1:
3461  if (CONFIG_H263_ENCODER)
3463  break;
3464  default:
3465  ff_init_qscale_tab(s);
3466  }
3467 
3468  s->lambda= s->lambda_table[0];
3469  //FIXME broken
3470  }else
3471  s->lambda = s->current_picture.f->quality;
3472  update_qscale(s);
3473  return 0;
3474 }
3475 
3476 /* must be called before writing the header */
3479  s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3480 
3481  if(s->pict_type==AV_PICTURE_TYPE_B){
3482  s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3483  assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3484  }else{
3485  s->pp_time= s->time - s->last_non_b_time;
3486  s->last_non_b_time= s->time;
3487  assert(s->picture_number==0 || s->pp_time > 0);
3488  }
3489 }
3490 
3492 {
3493  int i, ret;
3494  int bits;
3495  int context_count = s->slice_context_count;
3496 
3498 
3499  /* Reset the average MB variance */
3500  s->me.mb_var_sum_temp =
3501  s->me.mc_mb_var_sum_temp = 0;
3502 
3503  /* we need to initialize some time vars before we can encode b-frames */
3504  // RAL: Condition added for MPEG1VIDEO
3507  if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3508  ff_set_mpeg4_time(s);
3509 
3510  s->me.scene_change_score=0;
3511 
3512 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3513 
3514  if(s->pict_type==AV_PICTURE_TYPE_I){
3515  if(s->msmpeg4_version >= 3) s->no_rounding=1;
3516  else s->no_rounding=0;
3517  }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3519  s->no_rounding ^= 1;
3520  }
3521 
3522  if (s->avctx->flags & CODEC_FLAG_PASS2) {
3523  if (estimate_qp(s,1) < 0)
3524  return -1;
3525  ff_get_2pass_fcode(s);
3526  } else if (!(s->avctx->flags & CODEC_FLAG_QSCALE)) {
3528  s->lambda= s->last_lambda_for[s->pict_type];
3529  else
3531  update_qscale(s);
3532  }
3533 
3539  }
3540 
3541  s->mb_intra=0; //for the rate distortion & bit compare functions
3542  for(i=1; i<context_count; i++){
3544  if (ret < 0)
3545  return ret;
3546  }
3547 
3548  if(ff_init_me(s)<0)
3549  return -1;
3550 
3551  /* Estimate motion for every MB */
3552  if(s->pict_type != AV_PICTURE_TYPE_I){
3553  s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3554  s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3555  if (s->pict_type != AV_PICTURE_TYPE_B) {
3556  if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3557  s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3558  }
3559  }
3560 
3561  s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3562  }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3563  /* I-Frame */
3564  for(i=0; i<s->mb_stride*s->mb_height; i++)
3566 
3567  if(!s->fixed_qscale){
3568  /* finding spatial complexity for I-frame rate control */
3569  s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3570  }
3571  }
3572  for(i=1; i<context_count; i++){
3574  }
3576  s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3577  emms_c();
3578 
3581  for(i=0; i<s->mb_stride*s->mb_height; i++)
3583  if(s->msmpeg4_version >= 3)
3584  s->no_rounding=1;
3585  ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3587  }
3588 
3589  if(!s->umvplus){
3592 
3593  if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3594  int a,b;
3595  a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3597  s->f_code= FFMAX3(s->f_code, a, b);
3598  }
3599 
3600  ff_fix_long_p_mvs(s);
3602  if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3603  int j;
3604  for(i=0; i<2; i++){
3605  for(j=0; j<2; j++)
3608  }
3609  }
3610  }
3611 
3612  if(s->pict_type==AV_PICTURE_TYPE_B){
3613  int a, b;
3614 
3617  s->f_code = FFMAX(a, b);
3618 
3621  s->b_code = FFMAX(a, b);
3622 
3627  if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3628  int dir, j;
3629  for(dir=0; dir<2; dir++){
3630  for(i=0; i<2; i++){
3631  for(j=0; j<2; j++){
3634  ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3635  s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3636  }
3637  }
3638  }
3639  }
3640  }
3641  }
3642 
3643  if (estimate_qp(s, 0) < 0)
3644  return -1;
3645 
3646  if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3647  s->pict_type == AV_PICTURE_TYPE_I &&
3648  !(s->avctx->flags & CODEC_FLAG_QSCALE))
3649  s->qscale= 3; //reduce clipping problems
3650 
3651  if (s->out_format == FMT_MJPEG) {
3652  const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3653  const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3654 
3655  if (s->avctx->intra_matrix) {
3656  chroma_matrix =
3657  luma_matrix = s->avctx->intra_matrix;
3658  }
3659  if (s->avctx->chroma_intra_matrix)
3660  chroma_matrix = s->avctx->chroma_intra_matrix;
3661 
3662  /* for mjpeg, we do include qscale in the matrix */
3663  for(i=1;i<64;i++){
3664  int j = s->idsp.idct_permutation[i];
3665 
3666  s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->qscale) >> 3);
3667  s-> intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->qscale) >> 3);
3668  }
3669  s->y_dc_scale_table=
3671  s->chroma_intra_matrix[0] =
3674  s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3676  s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3677  s->qscale= 8;
3678  }
3679  if(s->codec_id == AV_CODEC_ID_AMV){
3680  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};
3681  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};
3682  for(i=1;i<64;i++){
3683  int j= s->idsp.idct_permutation[ff_zigzag_direct[i]];
3684 
3685  s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
3686  s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
3687  }
3688  s->y_dc_scale_table= y;
3689  s->c_dc_scale_table= c;
3690  s->intra_matrix[0] = 13;
3691  s->chroma_intra_matrix[0] = 14;
3693  s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3695  s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3696  s->qscale= 8;
3697  }
3698 
3699  //FIXME var duplication
3701  s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3704 
3705  if (s->current_picture.f->key_frame)
3706  s->picture_in_gop_number=0;
3707 
3708  s->mb_x = s->mb_y = 0;
3709  s->last_bits= put_bits_count(&s->pb);
3710  switch(s->out_format) {
3711  case FMT_MJPEG:
3712  if (CONFIG_MJPEG_ENCODER)
3715  break;
3716  case FMT_H261:
3717  if (CONFIG_H261_ENCODER)
3718  ff_h261_encode_picture_header(s, picture_number);
3719  break;
3720  case FMT_H263:
3721  if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3722  ff_wmv2_encode_picture_header(s, picture_number);
3723  else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3724  ff_msmpeg4_encode_picture_header(s, picture_number);
3725  else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3726  ff_mpeg4_encode_picture_header(s, picture_number);
3727  else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3728  ret = ff_rv10_encode_picture_header(s, picture_number);
3729  if (ret < 0)
3730  return ret;
3731  }
3732  else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3733  ff_rv20_encode_picture_header(s, picture_number);
3734  else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3735  ff_flv_encode_picture_header(s, picture_number);
3736  else if (CONFIG_H263_ENCODER)
3737  ff_h263_encode_picture_header(s, picture_number);
3738  break;
3739  case FMT_MPEG1:
3740  if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3741  ff_mpeg1_encode_picture_header(s, picture_number);
3742  break;
3743  default:
3744  av_assert0(0);
3745  }
3746  bits= put_bits_count(&s->pb);
3747  s->header_bits= bits - s->last_bits;
3748 
3749  for(i=1; i<context_count; i++){
3751  }
3752  s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3753  for(i=1; i<context_count; i++){
3754  if (s->pb.buf_end == s->thread_context[i]->pb.buf)
3755  set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-32));
3757  }
3758  emms_c();
3759  return 0;
3760 }
3761 
3762 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3763  const int intra= s->mb_intra;
3764  int i;
3765 
3766  s->dct_count[intra]++;
3767 
3768  for(i=0; i<64; i++){
3769  int level= block[i];
3770 
3771  if(level){
3772  if(level>0){
3773  s->dct_error_sum[intra][i] += level;
3774  level -= s->dct_offset[intra][i];
3775  if(level<0) level=0;
3776  }else{
3777  s->dct_error_sum[intra][i] -= level;
3778  level += s->dct_offset[intra][i];
3779  if(level>0) level=0;
3780  }
3781  block[i]= level;
3782  }
3783  }
3784 }
3785 
3787  int16_t *block, int n,
3788  int qscale, int *overflow){
3789  const int *qmat;
3790  const uint16_t *matrix;
3791  const uint8_t *scantable= s->intra_scantable.scantable;
3792  const uint8_t *perm_scantable= s->intra_scantable.permutated;
3793  int max=0;
3794  unsigned int threshold1, threshold2;
3795  int bias=0;
3796  int run_tab[65];
3797  int level_tab[65];
3798  int score_tab[65];
3799  int survivor[65];
3800  int survivor_count;
3801  int last_run=0;
3802  int last_level=0;
3803  int last_score= 0;
3804  int last_i;
3805  int coeff[2][64];
3806  int coeff_count[64];
3807  int qmul, qadd, start_i, last_non_zero, i, dc;
3808  const int esc_length= s->ac_esc_length;
3809  uint8_t * length;
3810  uint8_t * last_length;
3811  const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3812 
3813  s->fdsp.fdct(block);
3814 
3815  if(s->dct_error_sum)
3816  s->denoise_dct(s, block);
3817  qmul= qscale*16;
3818  qadd= ((qscale-1)|1)*8;
3819 
3820  if (s->mb_intra) {
3821  int q;
3822  if (!s->h263_aic) {
3823  if (n < 4)
3824  q = s->y_dc_scale;
3825  else
3826  q = s->c_dc_scale;
3827  q = q << 3;
3828  } else{
3829  /* For AIC we skip quant/dequant of INTRADC */
3830  q = 1 << 3;
3831  qadd=0;
3832  }
3833 
3834  /* note: block[0] is assumed to be positive */
3835  block[0] = (block[0] + (q >> 1)) / q;
3836  start_i = 1;
3837  last_non_zero = 0;
3838  qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3839  matrix = n < 4 ? s->intra_matrix : s->chroma_intra_matrix;
3840  if(s->mpeg_quant || s->out_format == FMT_MPEG1 || s->out_format == FMT_MJPEG)
3841  bias= 1<<(QMAT_SHIFT-1);
3842 
3843  if (n > 3 && s->intra_chroma_ac_vlc_length) {
3844  length = s->intra_chroma_ac_vlc_length;
3845  last_length= s->intra_chroma_ac_vlc_last_length;
3846  } else {
3847  length = s->intra_ac_vlc_length;
3848  last_length= s->intra_ac_vlc_last_length;
3849  }
3850  } else {
3851  start_i = 0;
3852  last_non_zero = -1;
3853  qmat = s->q_inter_matrix[qscale];
3854  matrix = s->inter_matrix;
3855  length = s->inter_ac_vlc_length;
3856  last_length= s->inter_ac_vlc_last_length;
3857  }
3858  last_i= start_i;
3859 
3860  threshold1= (1<<QMAT_SHIFT) - bias - 1;
3861  threshold2= (threshold1<<1);
3862 
3863  for(i=63; i>=start_i; i--) {
3864  const int j = scantable[i];
3865  int level = block[j] * qmat[j];
3866 
3867  if(((unsigned)(level+threshold1))>threshold2){
3868  last_non_zero = i;
3869  break;
3870  }
3871  }
3872 
3873  for(i=start_i; i<=last_non_zero; i++) {
3874  const int j = scantable[i];
3875  int level = block[j] * qmat[j];
3876 
3877 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3878 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3879  if(((unsigned)(level+threshold1))>threshold2){
3880  if(level>0){
3881  level= (bias + level)>>QMAT_SHIFT;
3882  coeff[0][i]= level;
3883  coeff[1][i]= level-1;
3884 // coeff[2][k]= level-2;
3885  }else{
3886  level= (bias - level)>>QMAT_SHIFT;
3887  coeff[0][i]= -level;
3888  coeff[1][i]= -level+1;
3889 // coeff[2][k]= -level+2;
3890  }
3891  coeff_count[i]= FFMIN(level, 2);
3892  av_assert2(coeff_count[i]);
3893  max |=level;
3894  }else{
3895  coeff[0][i]= (level>>31)|1;
3896  coeff_count[i]= 1;
3897  }
3898  }
3899 
3900  *overflow= s->max_qcoeff < max; //overflow might have happened
3901 
3902  if(last_non_zero < start_i){
3903  memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3904  return last_non_zero;
3905  }
3906 
3907  score_tab[start_i]= 0;
3908  survivor[0]= start_i;
3909  survivor_count= 1;
3910 
3911  for(i=start_i; i<=last_non_zero; i++){
3912  int level_index, j, zero_distortion;
3913  int dct_coeff= FFABS(block[ scantable[i] ]);
3914  int best_score=256*256*256*120;
3915 
3916  if (s->fdsp.fdct == ff_fdct_ifast)
3917  dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3918  zero_distortion= dct_coeff*dct_coeff;
3919 
3920  for(level_index=0; level_index < coeff_count[i]; level_index++){
3921  int distortion;
3922  int level= coeff[level_index][i];
3923  const int alevel= FFABS(level);
3924  int unquant_coeff;
3925 
3926  av_assert2(level);
3927 
3928  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3929  unquant_coeff= alevel*qmul + qadd;
3930  } else if(s->out_format == FMT_MJPEG) {
3931  j = s->idsp.idct_permutation[scantable[i]];
3932  unquant_coeff = alevel * matrix[j] * 8;
3933  }else{ //MPEG1
3934  j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3935  if(s->mb_intra){
3936  unquant_coeff = (int)( alevel * qscale * matrix[j]) >> 3;
3937  unquant_coeff = (unquant_coeff - 1) | 1;
3938  }else{
3939  unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[j])) >> 4;
3940  unquant_coeff = (unquant_coeff - 1) | 1;
3941  }
3942  unquant_coeff<<= 3;
3943  }
3944 
3945  distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3946  level+=64;
3947  if((level&(~127)) == 0){
3948  for(j=survivor_count-1; j>=0; j--){
3949  int run= i - survivor[j];
3950  int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3951  score += score_tab[i-run];
3952 
3953  if(score < best_score){
3954  best_score= score;
3955  run_tab[i+1]= run;
3956  level_tab[i+1]= level-64;
3957  }
3958  }
3959 
3960  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3961  for(j=survivor_count-1; j>=0; j--){
3962  int run= i - survivor[j];
3963  int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3964  score += score_tab[i-run];
3965  if(score < last_score){
3966  last_score= score;
3967  last_run= run;
3968  last_level= level-64;
3969  last_i= i+1;
3970  }
3971  }
3972  }
3973  }else{
3974  distortion += esc_length*lambda;
3975  for(j=survivor_count-1; j>=0; j--){
3976  int run= i - survivor[j];
3977  int score= distortion + score_tab[i-run];
3978 
3979  if(score < best_score){
3980  best_score= score;
3981  run_tab[i+1]= run;
3982  level_tab[i+1]= level-64;
3983  }
3984  }
3985 
3986  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3987  for(j=survivor_count-1; j>=0; j--){
3988  int run= i - survivor[j];
3989  int score= distortion + score_tab[i-run];
3990  if(score < last_score){
3991  last_score= score;
3992  last_run= run;
3993  last_level= level-64;
3994  last_i= i+1;
3995  }
3996  }
3997  }
3998  }
3999  }
4000 
4001  score_tab[i+1]= best_score;
4002 
4003  //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
4004  if(last_non_zero <= 27){
4005  for(; survivor_count; survivor_count--){
4006  if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4007  break;
4008  }
4009  }else{
4010  for(; survivor_count; survivor_count--){
4011  if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4012  break;
4013  }
4014  }
4015 
4016  survivor[ survivor_count++ ]= i+1;
4017  }
4018 
4019  if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
4020  last_score= 256*256*256*120;
4021  for(i= survivor[0]; i<=last_non_zero + 1; i++){
4022  int score= score_tab[i];
4023  if(i) score += lambda*2; //FIXME exacter?
4024 
4025  if(score < last_score){
4026  last_score= score;
4027  last_i= i;
4028  last_level= level_tab[i];
4029  last_run= run_tab[i];
4030  }
4031  }
4032  }
4033 
4034  s->coded_score[n] = last_score;
4035 
4036  dc= FFABS(block[0]);
4037  last_non_zero= last_i - 1;
4038  memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4039 
4040  if(last_non_zero < start_i)
4041  return last_non_zero;
4042 
4043  if(last_non_zero == 0 && start_i == 0){
4044  int best_level= 0;
4045  int best_score= dc * dc;
4046 
4047  for(i=0; i<coeff_count[0]; i++){
4048  int level= coeff[i][0];
4049  int alevel= FFABS(level);
4050  int unquant_coeff, score, distortion;
4051 
4052  if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4053  unquant_coeff= (alevel*qmul + qadd)>>3;
4054  }else{ //MPEG1
4055  unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[0])) >> 4;
4056  unquant_coeff = (unquant_coeff - 1) | 1;
4057  }
4058  unquant_coeff = (unquant_coeff + 4) >> 3;
4059  unquant_coeff<<= 3 + 3;
4060 
4061  distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4062  level+=64;
4063  if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4064  else score= distortion + esc_length*lambda;
4065 
4066  if(score < best_score){
4067  best_score= score;
4068  best_level= level - 64;
4069  }
4070  }
4071  block[0]= best_level;
4072  s->coded_score[n] = best_score - dc*dc;
4073  if(best_level == 0) return -1;
4074  else return last_non_zero;
4075  }
4076 
4077  i= last_i;
4078  av_assert2(last_level);
4079 
4080  block[ perm_scantable[last_non_zero] ]= last_level;
4081  i -= last_run + 1;
4082 
4083  for(; i>start_i; i -= run_tab[i] + 1){
4084  block[ perm_scantable[i-1] ]= level_tab[i];
4085  }
4086 
4087  return last_non_zero;
4088 }
4089 
4090 //#define REFINE_STATS 1
4091 static int16_t basis[64][64];
4092 
4093 static void build_basis(uint8_t *perm){
4094  int i, j, x, y;
4095  emms_c();
4096  for(i=0; i<8; i++){
4097  for(j=0; j<8; j++){
4098  for(y=0; y<8; y++){
4099  for(x=0; x<8; x++){
4100  double s= 0.25*(1<<BASIS_SHIFT);
4101  int index= 8*i + j;
4102  int perm_index= perm[index];
4103  if(i==0) s*= sqrt(0.5);
4104  if(j==0) s*= sqrt(0.5);
4105  basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
4106  }
4107  }
4108  }
4109  }
4110 }
4111 
4112 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
4113  int16_t *block, int16_t *weight, int16_t *orig,
4114  int n, int qscale){
4115  int16_t rem[64];
4116  LOCAL_ALIGNED_16(int16_t, d1, [64]);
4117  const uint8_t *scantable= s->intra_scantable.scantable;
4118  const uint8_t *perm_scantable= s->intra_scantable.permutated;
4119 // unsigned int threshold1, threshold2;
4120 // int bias=0;
4121  int run_tab[65];
4122  int prev_run=0;
4123  int prev_level=0;
4124  int qmul, qadd, start_i, last_non_zero, i, dc;
4125  uint8_t * length;
4126  uint8_t * last_length;
4127  int lambda;
4128  int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
4129 #ifdef REFINE_STATS
4130 static int count=0;
4131 static int after_last=0;
4132 static int to_zero=0;
4133 static int from_zero=0;
4134 static int raise=0;
4135 static int lower=0;
4136 static int messed_sign=0;
4137 #endif
4138 
4139  if(basis[0][0] == 0)
4141 
4142  qmul= qscale*2;
4143  qadd= (qscale-1)|1;
4144  if (s->mb_intra) {
4145  if (!s->h263_aic) {
4146  if (n < 4)
4147  q = s->y_dc_scale;
4148  else
4149  q = s->c_dc_scale;
4150  } else{
4151  /* For AIC we skip quant/dequant of INTRADC */
4152  q = 1;
4153  qadd=0;
4154  }
4155  q <<= RECON_SHIFT-3;
4156  /* note: block[0] is assumed to be positive */
4157  dc= block[0]*q;
4158 // block[0] = (block[0] + (q >> 1)) / q;
4159  start_i = 1;
4160 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
4161 // bias= 1<<(QMAT_SHIFT-1);
4162  if (n > 3 && s->intra_chroma_ac_vlc_length) {
4163  length = s->intra_chroma_ac_vlc_length;
4164  last_length= s->intra_chroma_ac_vlc_last_length;
4165  } else {
4166  length = s->intra_ac_vlc_length;
4167  last_length= s->intra_ac_vlc_last_length;
4168  }
4169  } else {
4170  dc= 0;
4171  start_i = 0;
4172  length = s->inter_ac_vlc_length;
4173  last_length= s->inter_ac_vlc_last_length;
4174  }
4175  last_non_zero = s->block_last_index[n];
4176 
4177 #ifdef REFINE_STATS
4178 {START_TIMER
4179 #endif
4180  dc += (1<<(RECON_SHIFT-1));
4181  for(i=0; i<64; i++){
4182  rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
4183  }
4184 #ifdef REFINE_STATS
4185 STOP_TIMER("memset rem[]")}
4186 #endif
4187  sum=0;
4188  for(i=0; i<64; i++){
4189  int one= 36;
4190  int qns=4;
4191  int w;
4192 
4193  w= FFABS(weight[i]) + qns*one;
4194  w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4195 
4196  weight[i] = w;
4197 // w=weight[i] = (63*qns + (w/2)) / w;
4198 
4199  av_assert2(w>0);
4200  av_assert2(w<(1<<6));
4201  sum += w*w;
4202  }
4203  lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4204 #ifdef REFINE_STATS
4205 {START_TIMER
4206 #endif
4207  run=0;
4208  rle_index=0;
4209  for(i=start_i; i<=last_non_zero; i++){
4210  int j= perm_scantable[i];
4211  const int level= block[j];
4212  int coeff;
4213 
4214  if(level){
4215  if(level<0) coeff= qmul*level - qadd;
4216  else coeff= qmul*level + qadd;
4217  run_tab[rle_index++]=run;
4218  run=0;
4219 
4220  s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4221  }else{
4222  run++;
4223  }
4224  }
4225 #ifdef REFINE_STATS
4226 if(last_non_zero>0){
4227 STOP_TIMER("init rem[]")
4228 }
4229 }
4230 
4231 {START_TIMER
4232 #endif
4233  for(;;){
4234  int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4235  int best_coeff=0;
4236  int best_change=0;
4237  int run2, best_unquant_change=0, analyze_gradient;
4238 #ifdef REFINE_STATS
4239 {START_TIMER
4240 #endif
4241  analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4242 
4243  if(analyze_gradient){
4244 #ifdef REFINE_STATS
4245 {START_TIMER
4246 #endif
4247  for(i=0; i<64; i++){
4248  int w= weight[i];
4249 
4250  d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4251  }
4252 #ifdef REFINE_STATS
4253 STOP_TIMER("rem*w*w")}
4254 {START_TIMER
4255 #endif
4256  s->fdsp.fdct(d1);
4257 #ifdef REFINE_STATS
4258 STOP_TIMER("dct")}
4259 #endif
4260  }
4261 
4262  if(start_i){
4263  const int level= block[0];
4264  int change, old_coeff;
4265 
4266  av_assert2(s->mb_intra);
4267 
4268  old_coeff= q*level;
4269 
4270  for(change=-1; change<=1; change+=2){
4271  int new_level= level + change;
4272  int score, new_coeff;
4273 
4274  new_coeff= q*new_level;
4275  if(new_coeff >= 2048 || new_coeff < 0)
4276  continue;
4277 
4278  score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4279  new_coeff - old_coeff);
4280  if(score<best_score){
4281  best_score= score;
4282  best_coeff= 0;
4283  best_change= change;
4284  best_unquant_change= new_coeff - old_coeff;
4285  }
4286  }
4287  }
4288 
4289  run=0;
4290  rle_index=0;
4291  run2= run_tab[rle_index++];
4292  prev_level=0;
4293  prev_run=0;
4294 
4295  for(i=start_i; i<64; i++){
4296  int j= perm_scantable[i];
4297  const int level= block[j];
4298  int change, old_coeff;
4299 
4300  if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4301  break;
4302 
4303  if(level){
4304  if(level<0) old_coeff= qmul*level - qadd;
4305  else old_coeff= qmul*level + qadd;
4306  run2= run_tab[rle_index++]; //FIXME ! maybe after last
4307  }else{
4308  old_coeff=0;
4309  run2--;
4310  av_assert2(run2>=0 || i >= last_non_zero );
4311  }
4312 
4313  for(change=-1; change<=1; change+=2){
4314  int new_level= level + change;
4315  int score, new_coeff, unquant_change;
4316 
4317  score=0;
4318  if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4319  continue;
4320 
4321  if(new_level){
4322  if(new_level<0) new_coeff= qmul*new_level - qadd;
4323  else new_coeff= qmul*new_level + qadd;
4324  if(new_coeff >= 2048 || new_coeff <= -2048)
4325  continue;
4326  //FIXME check for overflow
4327 
4328  if(level){
4329  if(level < 63 && level > -63){
4330  if(i < last_non_zero)
4331  score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4332  - length[UNI_AC_ENC_INDEX(run, level+64)];
4333  else
4334  score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4335  - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4336  }
4337  }else{
4338  av_assert2(FFABS(new_level)==1);
4339 
4340  if(analyze_gradient){
4341  int g= d1[ scantable[i] ];
4342  if(g && (g^new_level) >= 0)
4343  continue;
4344  }
4345 
4346  if(i < last_non_zero){
4347  int next_i= i + run2 + 1;
4348  int next_level= block[ perm_scantable[next_i] ] + 64;
4349 
4350  if(next_level&(~127))
4351  next_level= 0;
4352 
4353  if(next_i < last_non_zero)
4354  score += length[UNI_AC_ENC_INDEX(run, 65)]
4355  + length[UNI_AC_ENC_INDEX(run2, next_level)]
4356  - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4357  else
4358  score += length[UNI_AC_ENC_INDEX(run, 65)]
4359  + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4360  - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4361  }else{
4362  score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4363  if(prev_level){
4364  score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4365  - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4366  }
4367  }
4368  }
4369  }else{
4370  new_coeff=0;
4371  av_assert2(FFABS(level)==1);
4372 
4373  if(i < last_non_zero){
4374  int next_i= i + run2 + 1;
4375  int next_level= block[ perm_scantable[next_i] ] + 64;
4376 
4377  if(next_level&(~127))
4378  next_level= 0;
4379 
4380  if(next_i < last_non_zero)
4381  score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4382  - length[UNI_AC_ENC_INDEX(run2, next_level)]
4383  - length[UNI_AC_ENC_INDEX(run, 65)];
4384  else
4385  score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4386  - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4387  - length[UNI_AC_ENC_INDEX(run, 65)];
4388  }else{
4389  score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4390  if(prev_level){
4391  score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4392  - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4393  }
4394  }
4395  }
4396 
4397  score *= lambda;
4398 
4399  unquant_change= new_coeff - old_coeff;
4400  av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4401 
4402  score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4403  unquant_change);
4404  if(score<best_score){
4405  best_score= score;
4406  best_coeff= i;
4407  best_change= change;
4408  best_unquant_change= unquant_change;
4409  }
4410  }
4411  if(level){
4412  prev_level= level + 64;
4413  if(prev_level&(~127))
4414  prev_level= 0;
4415  prev_run= run;
4416  run=0;
4417  }else{
4418  run++;
4419  }
4420  }
4421 #ifdef REFINE_STATS
4422 STOP_TIMER("iterative step")}
4423 #endif
4424 
4425  if(best_change){
4426  int j= perm_scantable[ best_coeff ];
4427 
4428  block[j] += best_change;
4429 
4430  if(best_coeff > last_non_zero){
4431  last_non_zero= best_coeff;
4432  av_assert2(block[j]);
4433 #ifdef REFINE_STATS
4434 after_last++;
4435 #endif
4436  }else{
4437 #ifdef REFINE_STATS
4438 if(block[j]){
4439  if(block[j] - best_change){
4440  if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4441  raise++;
4442  }else{
4443  lower++;
4444  }
4445  }else{
4446  from_zero++;
4447  }
4448 }else{
4449  to_zero++;
4450 }
4451 #endif
4452  for(; last_non_zero>=start_i; last_non_zero--){
4453  if(block[perm_scantable[last_non_zero]])
4454  break;
4455  }
4456  }
4457 #ifdef REFINE_STATS
4458 count++;
4459 if(256*256*256*64 % count == 0){
4460  av_log(s->avctx, AV_LOG_DEBUG, "after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
4461 }
4462 #endif
4463  run=0;
4464  rle_index=0;
4465  for(i=start_i; i<=last_non_zero; i++){
4466  int j= perm_scantable[i];
4467  const int level= block[j];
4468 
4469  if(level){
4470  run_tab[rle_index++]=run;
4471  run=0;
4472  }else{
4473  run++;
4474  }
4475  }
4476 
4477  s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4478  }else{
4479  break;
4480  }
4481  }
4482 #ifdef REFINE_STATS
4483 if(last_non_zero>0){
4484 STOP_TIMER("iterative search")
4485 }
4486 }
4487 #endif
4488 
4489  return last_non_zero;
4490 }
4491 
4493  int16_t *block, int n,
4494  int qscale, int *overflow)
4495 {
4496  int i, j, level, last_non_zero, q, start_i;
4497  const int *qmat;
4498  const uint8_t *scantable= s->intra_scantable.scantable;
4499  int bias;
4500  int max=0;
4501  unsigned int threshold1, threshold2;
4502 
4503  s->fdsp.fdct(block);
4504 
4505  if(s->dct_error_sum)
4506  s->denoise_dct(s, block);
4507 
4508  if (s->mb_intra) {
4509  if (!s->h263_aic) {
4510  if (n < 4)
4511  q = s->y_dc_scale;
4512  else
4513  q = s->c_dc_scale;
4514  q = q << 3;
4515  } else
4516  /* For AIC we skip quant/dequant of INTRADC */
4517  q = 1 << 3;
4518 
4519  /* note: block[0] is assumed to be positive */
4520  block[0] = (block[0] + (q >> 1)) / q;
4521  start_i = 1;
4522  last_non_zero = 0;
4523  qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4525  } else {
4526  start_i = 0;
4527  last_non_zero = -1;
4528  qmat = s->q_inter_matrix[qscale];
4530  }
4531  threshold1= (1<<QMAT_SHIFT) - bias - 1;
4532  threshold2= (threshold1<<1);
4533  for(i=63;i>=start_i;i--) {
4534  j = scantable[i];
4535  level = block[j] * qmat[j];
4536 
4537  if(((unsigned)(level+threshold1))>threshold2){
4538  last_non_zero = i;
4539  break;
4540  }else{
4541  block[j]=0;
4542  }
4543  }
4544  for(i=start_i; i<=last_non_zero; i++) {
4545  j = scantable[i];
4546  level = block[j] * qmat[j];
4547 
4548 // if( bias+level >= (1<<QMAT_SHIFT)
4549 // || bias-level >= (1<<QMAT_SHIFT)){
4550  if(((unsigned)(level+threshold1))>threshold2){
4551  if(level>0){
4552  level= (bias + level)>>QMAT_SHIFT;
4553  block[j]= level;
4554  }else{
4555  level= (bias - level)>>QMAT_SHIFT;
4556  block[j]= -level;
4557  }
4558  max |=level;
4559  }else{
4560  block[j]=0;
4561  }
4562  }
4563  *overflow= s->max_qcoeff < max; //overflow might have happened
4564 
4565  /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4566  if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4568  scantable, last_non_zero);
4569 
4570  return last_non_zero;
4571 }
4572 
4573 #define OFFSET(x) offsetof(MpegEncContext, x)
4574 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4575 static const AVOption h263_options[] = {
4576  { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4577  { "structured_slices","Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
4578  { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
4580  { NULL },
4581 };
4582 
4583 static const AVClass h263_class = {
4584  .class_name = "H.263 encoder",
4585  .item_name = av_default_item_name,
4586  .option = h263_options,
4587  .version = LIBAVUTIL_VERSION_INT,
4588 };
4589 
4591  .name = "h263",
4592  .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4593  .type = AVMEDIA_TYPE_VIDEO,
4594  .id = AV_CODEC_ID_H263,
4595  .priv_data_size = sizeof(MpegEncContext),
4597  .encode2 = ff_mpv_encode_picture,
4598  .close = ff_mpv_encode_end,
4599  .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4600  .priv_class = &h263_class,
4601 };
4602 
4603 static const AVOption h263p_options[] = {
4604  { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4605  { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4606  { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4607  { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
4609  { NULL },
4610 };
4611 static const AVClass h263p_class = {
4612  .class_name = "H.263p encoder",
4613  .item_name = av_default_item_name,
4614  .option = h263p_options,
4615  .version = LIBAVUTIL_VERSION_INT,
4616 };
4617 
4619  .name = "h263p",
4620  .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4621  .type = AVMEDIA_TYPE_VIDEO,
4622  .id = AV_CODEC_ID_H263P,
4623  .priv_data_size = sizeof(MpegEncContext),
4625  .encode2 = ff_mpv_encode_picture,
4626  .close = ff_mpv_encode_end,
4627  .capabilities = CODEC_CAP_SLICE_THREADS,
4628  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4629  .priv_class = &h263p_class,
4630 };
4631 
4632 FF_MPV_GENERIC_CLASS(msmpeg4v2)
4633 
4635  .name = "msmpeg4v2",
4636  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4637  .type = AVMEDIA_TYPE_VIDEO,
4638  .id = AV_CODEC_ID_MSMPEG4V2,
4639  .priv_data_size = sizeof(MpegEncContext),
4641  .encode2 = ff_mpv_encode_picture,
4642  .close = ff_mpv_encode_end,
4643  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4644  .priv_class = &msmpeg4v2_class,
4645 };
4646 
4647 FF_MPV_GENERIC_CLASS(msmpeg4v3)
4648 
4650  .name = "msmpeg4",
4651  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4652  .type = AVMEDIA_TYPE_VIDEO,
4653  .id = AV_CODEC_ID_MSMPEG4V3,
4654  .priv_data_size = sizeof(MpegEncContext),
4656  .encode2 = ff_mpv_encode_picture,
4657  .close = ff_mpv_encode_end,
4658  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4659  .priv_class = &msmpeg4v3_class,
4660 };
4661 
4663 
4665  .name = "wmv1",
4666  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4667  .type = AVMEDIA_TYPE_VIDEO,
4668  .id = AV_CODEC_ID_WMV1,
4669  .priv_data_size = sizeof(MpegEncContext),
4671  .encode2 = ff_mpv_encode_picture,
4672  .close = ff_mpv_encode_end,
4673  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4674  .priv_class = &wmv1_class,
4675 };
int last_time_base
Definition: mpegvideo.h:451
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
Definition: mpeg12data.c:41
int plane
Definition: avisynth_c.h:291
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Definition: me_cmp.c:936
void ff_h261_reorder_mb_index(MpegEncContext *s)
Definition: h261enc.c:108
int(* try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale)
int chroma_elim_threshold
Definition: mpegvideo.h:186
void ff_jpeg_fdct_islow_10(int16_t *data)
static const AVOption h263_options[]
int frame_bits
bits used for the current frame
Definition: mpegvideo.h:404
IDCTDSPContext idsp
Definition: mpegvideo.h:299
av_cold int ff_dct_encode_init(MpegEncContext *s)
#define NULL
Definition: coverity.c:32
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
Definition: mpegvideo.h:407
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
const struct AVCodec * codec
Definition: avcodec.h:1250
int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
av_cold void ff_rate_control_uninit(MpegEncContext *s)
Definition: ratecontrol.c:303
#define FF_MPV_FLAG_STRICT_GOP
Definition: mpegvideo.h:620
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:3148
void ff_estimate_b_frame_motion(MpegEncContext *s, int mb_x, int mb_y)
Definition: motion_est.c:1513
float v
qpel_mc_func avg_qpel_pixels_tab[2][16]
Definition: qpeldsp.h:74
int picture_number
Definition: mpegvideo.h:196
const char * s
Definition: avisynth_c.h:631
#define MAX_PICTURE_COUNT
Definition: mpegvideo.h:64
#define RECON_SHIFT
me_cmp_func frame_skip_cmp[6]
Definition: me_cmp.h:76
#define CANDIDATE_MB_TYPE_SKIPPED
Definition: mpegutils.h:103
rate control context.
Definition: ratecontrol.h:63
static int shift(int a, int b)
Definition: sonic.c:82
S(GMC)-VOP MPEG4.
Definition: avutil.h:270
void(* dct_unquantize_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:580
void ff_mpeg1_encode_init(MpegEncContext *s)
Definition: mpeg12enc.c:1007
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
int esc3_level_length
Definition: mpegvideo.h:501
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2090
static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
int time_increment_bits
< number of bits to represent the fractional part of time (encoder only)
Definition: mpegvideo.h:450
void ff_h263_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: ituh263enc.c:103
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2932
int16_t(* p_mv_table)[2]
MV table (1MV per MB) p-frame encoding.
Definition: mpegvideo.h:317
int mpeg_quant
0-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1540
void ff_fdct_ifast(int16_t *data)
Definition: jfdctfst.c:208
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t * fcode_tab
smallest fcode needed for each MV
Definition: mpegvideo.h:346
int start_mb_y
start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:222
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:336
void ff_estimate_p_frame_motion(MpegEncContext *s, int mb_x, int mb_y)
Definition: motion_est.c:889
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:257
uint8_t * mb_mean
Table for MB luminance.
Definition: mpegvideo.h:118
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:2663
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:737
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
Definition: mpegvideo.h:345
uint8_t * edge_emu_buffer
temporary buffer for if MVs point to out-of-frame data
Definition: mpegvideo.h:141
int pre_pass
= 1 for the pre pass
Definition: motion_est.h:62
op_pixels_func avg_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:68
#define FF_MPV_FLAG_SKIP_RD
Definition: mpegvideo.h:619
#define FF_MPV_GENERIC_CLASS(name)
Definition: mpegvideo.h:662
AVFrame * tmp_frames[MAX_B_FRAMES+2]
Definition: mpegvideo.h:615
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
#define FF_CMP_NSSE
Definition: avcodec.h:1659
attribute_deprecated int rc_qmod_freq
Definition: avcodec.h:2296
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
RateControlEntry * entry
Definition: ratecontrol.h:65
qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16]
Definition: qpeldsp.h:75
void(* shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height)
#define CANDIDATE_MB_TYPE_INTER_I
Definition: mpegutils.h:110
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
const char * g
Definition: vf_curves.c:108
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:736
void ff_h263_encode_init(MpegEncContext *s)
Definition: ituh263enc.c:761
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2745
int end_mb_y
end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:223
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void ff_init_qscale_tab(MpegEncContext *s)
init s->current_picture.qscale_table from s->lambda_table
#define OFFSET(x)
uint16_t * mb_var
Table for MB variances.
Definition: mpegvideo.h:109
uint16_t(* q_chroma_intra_matrix16)[2][64]
Definition: mpegvideo.h:394
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block.
Definition: mpegvideo.c:3188
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:368
static int estimate_qp(MpegEncContext *s, int dry_run)
#define MAX_MV
Definition: motion_est.h:32
int acc
Definition: yuv2rgb.c:532
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1503
int16_t(*[3] ac_val)[16]
used for mpeg4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:263
MJPEG encoder.
void(* add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale)
int v_edge_pos
horizontal / vertical position of the right/bottom edge (pixel replication)
Definition: mpegvideo.h:201
#define FF_MPV_COMMON_OPTS
Definition: mpegvideo.h:630
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1960
#define me
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Definition: mpegvideo.h:499
#define CANDIDATE_MB_TYPE_BIDIR
Definition: mpegutils.h:108
int num
numerator
Definition: rational.h:44
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
Definition: h263dsp.c:117
void(* rtp_callback)(struct AVCodecContext *avctx, void *data, int size, int mb_nb)
Definition: avcodec.h:2463
int size
Definition: avcodec.h:1163
attribute_deprecated int lmax
Definition: avcodec.h:2400
enum AVCodecID codec_id
Definition: mpegvideo.h:181
const char * b
Definition: vf_curves.c:109
void ff_get_2pass_fcode(MpegEncContext *s)
Definition: ratecontrol.c:733
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:64
int obmc
overlapped block motion compensation
Definition: mpegvideo.h:432
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:48
void ff_mpeg1_clean_buffers(MpegEncContext *s)
Definition: mpeg12.c:123
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1623
int ff_h261_get_picture_format(int width, int height)
Definition: h261enc.c:40
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
#define FF_MPV_FLAG_NAQ
Definition: mpegvideo.h:623
int16_t(*[2][2] p_field_mv_table)[2]
MV table (2MV per MB) interlaced p-frame encoding.
Definition: mpegvideo.h:323
static int select_input_picture(MpegEncContext *s)
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:374
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:139
int ildct_cmp
interlaced DCT comparison function
Definition: avcodec.h:1648
void(* qpel_mc_func)(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
Definition: qpeldsp.h:65
int coded_score[12]
Definition: mpegvideo.h:386
mpegvideo header.
#define FF_ARRAY_ELEMS(a)
int rtp_payload_size
Definition: avcodec.h:2465
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
Definition: mjpegenc.c:71
int scene_change_score
Definition: motion_est.h:77
int mpv_flags
flags set by private options
Definition: mpegvideo.h:586
uint8_t permutated[64]
Definition: idctdsp.h:31
int intra_quant_bias
intra quantizer bias
Definition: avcodec.h:1742
static const AVClass h263_class
uint8_t run
Definition: svq3.c:149
static AVPacket pkt
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:377
#define EDGE_TOP
int padding_bug_score
used to detect the VERY common padding bug in MPEG4
Definition: mpegvideo.h:474
const uint16_t ff_h263_format[8][2]
Definition: h263data.h:239
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:384
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:202
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64])
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2429
#define FF_LAMBDA_SHIFT
Definition: avutil.h:218
static void clip_coeffs(MpegEncContext *s, int16_t *block, int last_index)
QpelDSPContext qdsp
Definition: mpegvideo.h:304
An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of structures with info about macroblo...
Definition: avcodec.h:1017
AVCodec.
Definition: avcodec.h:3181
static void write_mb_info(MpegEncContext *s)
int time_base
time in seconds of last I,P,S Frame
Definition: mpegvideo.h:452
int qscale
QP.
Definition: mpegvideo.h:273
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:156
int16_t(* b_back_mv_table)[2]
MV table (1MV per MB) backward mode b-frame encoding.
Definition: mpegvideo.h:319
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:313
int chroma_x_shift
Definition: mpegvideo.h:537
#define INPLACE_OFFSET
Definition: mpegvideo.h:72
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:183
uint64_t vbv_delay
VBV delay coded in the last frame (in periods of a 27 MHz clock).
Definition: avcodec.h:2983
void(* dct_unquantize_h263_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:574
int field_select[2][2]
Definition: mpegvideo.h:344
void(* dct_unquantize_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:578
#define CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:715
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:1802
uint32_t ff_square_tab[512]
Definition: me_cmp.c:32
#define FFALIGN(x, a)
Definition: common.h:71
int quant_precision
Definition: mpegvideo.h:463
void ff_mpeg4_merge_partitions(MpegEncContext *s)
static int mb_var_thread(AVCodecContext *c, void *arg)
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:2840
void(* dct_unquantize_h263_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:576
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1369
int modified_quant
Definition: mpegvideo.h:444
static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
#define FF_MPV_FLAG_CBP_RD
Definition: mpegvideo.h:622
int skipdct
skip dct and code zero residual
Definition: mpegvideo.h:289
float rc_buffer_aggressivity
Definition: mpegvideo.h:597
int b_frame_score
Definition: mpegvideo.h:131
void ff_mpeg4_clean_buffers(MpegEncContext *s)
Definition: mpeg4video.c:45
#define CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:746
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define CANDIDATE_MB_TYPE_INTER
Definition: mpegutils.h:101
float p_masking
p block masking (0-> disabled)
Definition: avcodec.h:1584
int picture_in_gop_number
0-> first pic in gop, ...
Definition: mpegvideo.h:197
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
int alt_inter_vlc
alternative inter vlc
Definition: mpegvideo.h:443
void ff_mpeg1_encode_slice_header(MpegEncContext *s)
Definition: mpeg12enc.c:412
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
uint8_t * ptr_lastgob
Definition: mpegvideo.h:553
int64_t time
time of current frame
Definition: mpegvideo.h:453
static int encode_picture(MpegEncContext *s, int picture_number)
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1313
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4) ...
Definition: mpegvideo.h:331
ScratchpadContext sc
Definition: mpegvideo.h:271
if()
Definition: avfilter.c:975
uint8_t bits
Definition: crc.c:295
attribute_deprecated const char * rc_eq
Definition: avcodec.h:2319
attribute_deprecated float rc_buffer_aggressivity
Definition: avcodec.h:2341
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
Picture ** input_picture
next pictures on display order for encoding
Definition: mpegvideo.h:206
#define CANDIDATE_MB_TYPE_INTER4V
Definition: mpegutils.h:102
void(* get_pixels)(int16_t *block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: pixblockdsp.h:27
AVOptions.
void(* denoise_dct)(struct MpegEncContext *s, int16_t *block)
Definition: mpegvideo.h:584
PutBitContext pb2
used for data partitioned VOPs
Definition: mpegvideo.h:472
enum OutputFormat out_format
output format
Definition: mpegvideo.h:173
#define CANDIDATE_MB_TYPE_FORWARD_I
Definition: mpegutils.h:111
int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, ScratchpadContext *sc, int shared, int encoding, int chroma_x_shift, int chroma_y_shift, int out_format, int mb_stride, int mb_width, int mb_height, int b8_stride, ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
Allocate a Picture.
Definition: mpegvideo.c:585
uint16_t(* dct_offset)[64]
Definition: mpegvideo.h:400
void ff_dct_encode_init_x86(MpegEncContext *s)
Definition: mpegvideoenc.c:200
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
uint16_t * chroma_intra_matrix
custom intra quantization matrix Code outside libavcodec should access this field using av_codec_g/se...
Definition: avcodec.h:3130
void ff_mpv_motion(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int dir, uint8_t **ref_picture, op_pixels_func(*pix_op)[4], qpel_mc_func(*qpix_op)[16])
void ff_msmpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: msmpeg4enc.c:224
static void mpv_encode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for encoding.
Multithreading support functions.
int pre_dia_size
ME prepass diamond size & shape.
Definition: avcodec.h:1699
AVCodec ff_h263_encoder
static const AVOption h263p_options[]
static int get_sae(uint8_t *src, int ref, int stride)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:363
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:418
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1355
int no_rounding
apply no rounding to motion compensation (MPEG4, msmpeg4, ...) for b-frames rounding mode is always 0...
Definition: mpegvideo.h:351
#define CANDIDATE_MB_TYPE_BACKWARD_I
Definition: mpegutils.h:112
int interlaced_dct
Definition: mpegvideo.h:542
int(* q_chroma_intra_matrix)[64]
Definition: mpegvideo.h:390
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1630
void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:3129
#define QUANT_BIAS_SHIFT
Definition: mpegvideo_enc.c:64
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:249
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:2101
#define CHROMA_420
Definition: mpegvideo.h:534
int intra_dc_precision
Definition: mpegvideo.h:523
int repeat_first_field
Definition: mpegvideo.h:531
static AVFrame * frame
quarterpel DSP functions
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
int16_t(* b_bidir_forw_mv_table)[2]
MV table (1MV per MB) bidir mode b-frame encoding.
Definition: mpegvideo.h:320
static void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
uint8_t * data
Definition: avcodec.h:1162
const uint16_t ff_aanscales[64]
Definition: aandcttab.c:26
uint8_t(* mv_penalty)[MAX_MV *2+1]
bit amount needed to encode a MV
Definition: motion_est.h:83
void ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
int ff_wmv2_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: wmv2enc.c:73
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:455
me_cmp_func nsse[6]
Definition: me_cmp.h:65
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define lrintf(x)
Definition: libm_mips.h:70
#define CODEC_FLAG_MV0
Definition: avcodec.h:727
const uint8_t * scantable
Definition: idctdsp.h:30
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:346
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:198
static void rebase_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Rebase the bit writer onto a reallocated buffer.
Definition: put_bits.h:71
float lumi_masking
luminance masking (0-> disabled)
Definition: avcodec.h:1563
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2494
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:375
high precision timer, useful to profile code
static void update_noise_reduction(MpegEncContext *s)
#define FF_MPV_FLAG_QP_RD
Definition: mpegvideo.h:621
#define CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
Definition: avcodec.h:761
void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], int motion_x, int motion_y)
Definition: mpeg12enc.c:998
#define MAX_LEVEL
Definition: rl.h:35
void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: h261enc.c:53
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:279
int flipflop_rounding
Definition: mpegvideo.h:498
#define CHROMA_444
Definition: mpegvideo.h:536
int num_entries
number of RateControlEntries
Definition: ratecontrol.h:64
int gop_picture_number
index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
Definition: mpegvideo.h:512
uint8_t * mb_info_ptr
Definition: mpegvideo.h:435
#define av_log(a,...)
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:759
#define ff_sqrt
Definition: mathops.h:215
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:3240
#define ROUNDED_DIV(a, b)
Definition: common.h:55
int(* q_inter_matrix)[64]
Definition: mpegvideo.h:391
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
#define FF_CMP_VSSE
Definition: avcodec.h:1658
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:773
#define CODEC_FLAG_LOOP_FILTER
loop filter
Definition: avcodec.h:762
int(* q_intra_matrix)[64]
precomputed matrix (combine qscale and DCT renorm)
Definition: mpegvideo.h:389
#define FF_MPV_FLAG_MV0
Definition: mpegvideo.h:624
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:171
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:2843
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:256
enum AVCodecID id
Definition: avcodec.h:3195
int h263_plus
h263 plus headers
Definition: mpegvideo.h:178
H263DSPContext h263dsp
Definition: mpegvideo.h:306
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:225
int last_non_b_pict_type
used for mpeg4 gmc b-frames & ratecontrol
Definition: mpegvideo.h:284
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:219
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1533
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:254
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
attribute_deprecated float rc_initial_cplx
Definition: avcodec.h:2344
uint8_t * inter_ac_vlc_last_length
Definition: mpegvideo.h:382
#define FF_MB_DECISION_BITS
chooses the one which needs the fewest bits
Definition: avcodec.h:1779
#define CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:763
int64_t total_bits
Definition: mpegvideo.h:403
#define PTRDIFF_SPECIFIER
Definition: internal.h:249
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:264
int chroma_y_shift
Definition: mpegvideo.h:538
int strict_std_compliance
strictly follow the std (MPEG4, ...)
Definition: mpegvideo.h:187
av_default_item_name
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:468
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegvideo.h:142
#define AVERROR(e)
Definition: error.h:43
int frame_skip_threshold
frame skip threshold
Definition: avcodec.h:2408
av_cold int ff_rate_control_init(MpegEncContext *s)
Definition: ratecontrol.c:86
int me_sub_cmp
subpixel motion estimation comparison function
Definition: avcodec.h:1636
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
int qmax
maximum quantizer
Definition: avcodec.h:2277
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2118
static void update_mb_info(MpegEncContext *s, int startcode)
#define MERGE(field)
void ff_write_pass1_stats(MpegEncContext *s)
Definition: ratecontrol.c:44
int unrestricted_mv
mv can point outside of the coded picture
Definition: mpegvideo.h:292
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
ERContext er
Definition: mpegvideo.h:610
int last_lambda_for[5]
last lambda for a specific pict type
Definition: mpegvideo.h:288
static int sse_mb(MpegEncContext *s)
int reference
Definition: mpegvideo.h:134
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
void(* dct_unquantize_mpeg2_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:572
PixblockDSPContext pdsp
Definition: mpegvideo.h:303
const char * arg
Definition: jacosubdec.c:66
uint8_t * intra_chroma_ac_vlc_length
Definition: mpegvideo.h:379
void(* dct_unquantize_mpeg1_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:566
int h263_slice_structured
Definition: mpegvideo.h:442
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1335
uint8_t * buf
Definition: put_bits.h:38
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:712
uint64_t error[AV_NUM_DATA_POINTERS]
Definition: mpegvideo.h:137
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2327
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:55
GLsizei GLsizei * length
Definition: opengl_enc.c:115
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:302
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:464
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:358
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Shrink the already allocated side data buffer.
Definition: avpacket.c:506
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:469
qpel_mc_func put_qpel_pixels_tab[2][16]
Definition: qpeldsp.h:73
uint8_t *[2][2] b_field_select_table
Definition: mpegvideo.h:326
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1474
#define FFMAX(a, b)
Definition: common.h:64
Libavcodec external API header.
#define CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:757
int64_t mb_var_sum_temp
Definition: motion_est.h:76
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
static void frame_end(MpegEncContext *s)
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:422
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2304
void ff_clean_h263_qscales(MpegEncContext *s)
modify qscale so that encoding is actually possible in h263 (limit difference to -2..2)
Definition: ituh263enc.c:266
int coded_picture_number
used to set pic->coded_picture_number, should not be used for/by anything else
Definition: mpegvideo.h:195
int * lambda_table
Definition: mpegvideo.h:277
static int estimate_best_b_count(MpegEncContext *s)
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:1830
int me_penalty_compensation
Definition: avcodec.h:1873
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
void ff_wmv2_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: wmv2enc.c:146
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:630
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:378
static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_width, int mb_block_count)
const uint8_t *const ff_mpeg2_dc_scale_table[4]
Definition: mpegvideodata.c:75
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2548
void ff_h263_loop_filter(MpegEncContext *s)
Definition: h263.c:139
void(* op_pixels_func)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
Definition: hpeldsp.h:38
#define CHROMA_422
Definition: mpegvideo.h:535
int bit_rate
the average bitrate
Definition: avcodec.h:1305
float border_masking
Definition: mpegvideo.h:598
int progressive_frame
Definition: mpegvideo.h:540
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
Definition: mpegvideo.c:715
#define FFMIN(a, b)
Definition: common.h:66
int display_picture_number
picture number in display order
Definition: frame.h:278
uint16_t(* q_inter_matrix16)[2][64]
Definition: mpegvideo.h:395
uint8_t * vbv_delay_ptr
pointer to vbv_delay in the bitstream
Definition: mpegvideo.h:514
int fixed_qscale
fixed qscale if non zero
Definition: mpegvideo.h:182
float y
void ff_clean_mpeg4_qscales(MpegEncContext *s)
modify mb_type & qscale so that encoding is actually possible in mpeg4
#define MAX_MB_BYTES
Definition: mpegvideo.h:70
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
int me_method
ME algorithm.
Definition: mpegvideo.h:327
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:147
int umvplus
== H263+ && unrestricted_mv
Definition: mpegvideo.h:440
Picture new_picture
copy of the source picture structure for encoding.
Definition: mpegvideo.h:243
void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
Deallocate a picture.
Definition: mpegvideo.c:648
int intra_quant_bias
bias for the quantizer
Definition: mpegvideo.h:372
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
int(* pix_sum)(uint8_t *pix, int line_size)
int16_t(*[2] motion_val)[2]
Definition: mpegvideo.h:97
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:253
Picture.
Definition: mpegvideo.h:89
int alternate_scan
Definition: mpegvideo.h:529
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow. ...
Definition: avcodec.h:2352
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
Definition: ratecontrol.c:744
int b_frame_strategy
Definition: avcodec.h:1518
uint16_t(* q_intra_matrix16)[2][64]
identical to the above but for MMX & these are not permutated, second 64 entries are bias ...
Definition: mpegvideo.h:393
perm
Definition: f_perms.c:74
#define FFABS(a)
Definition: common.h:61
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:283
int(* ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2]
[mb_intra][isChroma][level][run][last]
Definition: mpegvideo.h:504
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:756
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:155
MotionEstContext me
Definition: mpegvideo.h:349
int n
Definition: avisynth_c.h:547
static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale)
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
const int16_t ff_mpeg4_default_non_intra_matrix[64]
Definition: mpeg4data.h:348
int mb_decision
macroblock decision mode
Definition: avcodec.h:1777
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride)
attribute_deprecated float rc_qsquish
Definition: avcodec.h:2291
uint8_t * mbintra_table
used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
Definition: mpegvideo.h:267
#define MAX_B_FRAMES
Definition: mpegvideo.h:66
int ff_msmpeg4_encode_init(MpegEncContext *s)
Definition: msmpeg4enc.c:121
int ac_esc_length
num of bits needed to encode the longest esc
Definition: mpegvideo.h:376
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
Definition: put_bits.h:253
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2753
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:360
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:364
int inter_quant_bias
inter quantizer bias
Definition: avcodec.h:1750
static uint8_t default_fcode_tab[MAX_MV *2+1]
Definition: mpegvideo_enc.c:76
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
AVCodec ff_h263p_encoder
static void build_basis(uint8_t *perm)
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:333
int frame_skip_factor
frame skip factor
Definition: avcodec.h:2415
int first_slice_line
used in mpeg4 too to handle resync markers
Definition: mpegvideo.h:497
int frame_pred_frame_dct
Definition: mpegvideo.h:524
uint16_t * mc_mb_var
Table for motion compensated MB variances.
Definition: mpegvideo.h:112
void ff_flv_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: flvenc.c:26
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:330
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2547
int coded_picture_number
picture number in bitstream order
Definition: frame.h:274
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
uint16_t inter_matrix[64]
Definition: mpegvideo.h:369
#define FF_LAMBDA_SCALE
Definition: avutil.h:219
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: frame.h:351
void ff_jpeg_fdct_islow_8(int16_t *data)
int64_t last_non_b_time
Definition: mpegvideo.h:454
void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h261enc.c:237
#define QMAT_SHIFT
Definition: mpegvideo_enc.c:67
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:224
#define CONFIG_MSMPEG4_ENCODER
Definition: msmpeg4.h:75
AVS_Value src
Definition: avisynth_c.h:482
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
Definition: mpegvideo.h:276
#define CODEC_FLAG_NORMALIZE_AQP
Definition: avcodec.h:754
void ff_faandct(int16_t *data)
Definition: faandct.c:123
double buffer_index
amount of bits in the video/audio buffer
Definition: ratecontrol.h:66
void ff_free_picture_tables(Picture *pic)
Definition: mpegvideo.c:485
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:46
#define ff_dlog(ctx,...)
Definition: internal.h:54
int h263_flv
use flv h263 header
Definition: mpegvideo.h:179
static const AVClass h263p_class
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:203
enum AVCodecID codec_id
Definition: avcodec.h:1258
#define QMAT_SHIFT_MMX
Definition: mpegvideo_enc.c:66
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
void ff_convert_matrix(MpegEncContext *s, int(*qmat)[64], uint16_t(*qmat16)[2][64], const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
Definition: mpegvideo_enc.c:83
const uint16_t ff_inv_aanscales[64]
Definition: aandcttab.c:38
void ff_set_cmp(MECmpContext *c, me_cmp_func *cmp, int type)
Definition: me_cmp.c:370
#define START_TIMER
Definition: timer.h:92
int frame_bits
number of bits used for the previously encoded frame
Definition: avcodec.h:2487
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
uint8_t * intra_chroma_ac_vlc_last_length
Definition: mpegvideo.h:380
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
main external API structure.
Definition: avcodec.h:1241
ScanTable intra_scantable
Definition: mpegvideo.h:160
int pre_me
prepass for motion estimation
Definition: avcodec.h:1685
int qmin
minimum quantizer
Definition: avcodec.h:2270
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:169
static void write_slice_end(MpegEncContext *s)
int64_t dts_delta
pts difference between the first and second input frame, used for calculating dts of the first frame ...
Definition: mpegvideo.h:213
int64_t user_specified_pts
last non-zero pts from AVFrame which was passed into avcodec_encode_video2()
Definition: mpegvideo.h:209
FDCTDSPContext fdsp
Definition: mpegvideo.h:296
static void denoise_dct_c(MpegEncContext *s, int16_t *block)
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
uint8_t * buf_end
Definition: put_bits.h:38
static int frame_start(MpegEncContext *s)
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
Definition: avcodec.h:1577
float rc_qmod_amp
Definition: mpegvideo.h:594
int luma_elim_threshold
Definition: mpegvideo.h:185
GLint GLenum type
Definition: opengl_enc.c:105
void ff_fix_long_p_mvs(MpegEncContext *s)
Definition: motion_est.c:1674
Picture * picture
main picture buffer
Definition: mpegvideo.h:205
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:467
uint8_t * inter_ac_vlc_length
Definition: mpegvideo.h:381
int progressive_sequence
Definition: mpegvideo.h:517
uint16_t * intra_matrix
custom intra quantization matrix
Definition: avcodec.h:1787
h261codec.
void ff_h263_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
Definition: ituh263enc.c:240
uint8_t * buf_ptr
Definition: put_bits.h:38
Describe the class of an AVClass context structure.
Definition: log.h:67
int stuffing_bits
bits used for stuffing
Definition: mpegvideo.h:405
op_pixels_func put_no_rnd_pixels_tab[4][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:80
int16_t(*[2][2][2] b_field_mv_table)[2]
MV table (4MV per MB) interlaced b-frame encoding.
Definition: mpegvideo.h:324
int(* pix_norm1)(uint8_t *pix, int line_size)
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegvideo.h:129
int index
Definition: gxfenc.c:89
#define CANDIDATE_MB_TYPE_DIRECT
Definition: mpegutils.h:105
#define FF_DEFAULT_QUANT_BIAS
Definition: avcodec.h:1743
struct AVFrame * f
Definition: mpegvideo.h:90
static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type)
const uint16_t ff_mpeg1_default_intra_matrix[256]
Definition: mpeg12data.c:30
int input_picture_number
used to set pic->display_picture_number, should not be used for/by anything else
Definition: mpegvideo.h:194
AVCodec ff_wmv1_encoder
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:117
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
Definition: pixblockdsp.c:54
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:204
int mb_info
interval for outputting info about mb offsets as side data
Definition: mpegvideo.h:433
void ff_set_mpeg4_time(MpegEncContext *s)
static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type)
#define STRIDE_ALIGN
Definition: internal.h:73
av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
Definition: mjpegenc.c:116
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1330
int frame_skip_exp
frame skip exponent
Definition: avcodec.h:2422
#define CANDIDATE_MB_TYPE_BIDIR_I
Definition: mpegutils.h:113
const int16_t ff_mpeg4_default_intra_matrix[64]
Definition: mpeg4data.h:337
int f_code
forward MV resolution
Definition: mpegvideo.h:307
int ff_pre_estimate_p_frame_motion(MpegEncContext *s, int mb_x, int mb_y)
Definition: motion_est.c:1074
#define CANDIDATE_MB_TYPE_DIRECT0
Definition: mpegutils.h:115
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
Definition: mpeg4video.c:117
#define MAX_FCODE
Definition: mpegvideo.h:61
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV *2+1]
Definition: mpegvideo_enc.c:75
#define CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:764
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1298
#define MV_DIR_FORWARD
Definition: mpegvideo.h:329
uint16_t * inter_matrix
custom inter quantization matrix
Definition: avcodec.h:1794
int max_b_frames
max number of b-frames for encoding
Definition: mpegvideo.h:184
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:281
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
int bit_rate
wanted bit rate
Definition: mpegvideo.h:172
static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
int last_mv_dir
last mv_dir, used for b frame encoding
Definition: mpegvideo.h:513
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:265
int h263_pred
use mpeg4/h263 ac/dc predictions
Definition: mpegvideo.h:174
int16_t(* b_bidir_back_mv_table)[2]
MV table (1MV per MB) bidir mode b-frame encoding.
Definition: mpegvideo.h:321
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:462
#define EDGE_WIDTH
Definition: mpegvideo.h:74
float dark_masking
darkness masking (0-> disabled)
Definition: avcodec.h:1591
static int64_t pts
Global timestamp for the audio frames.
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
Definition: avcodec.h:1570
int ff_init_me(MpegEncContext *s)
Definition: motion_est.c:306
uint8_t *[2] p_field_select_table
Definition: mpegvideo.h:325
int16_t(* b_direct_mv_table)[2]
MV table (1MV per MB) direct mode b-frame encoding.
Definition: mpegvideo.h:322
AAN (Arai, Agui and Nakajima) (I)DCT tables.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:258
uint8_t level
Definition: svq3.c:150
#define FF_MB_DECISION_SIMPLE
uses mb_cmp
Definition: avcodec.h:1778
me_cmp_func sad[6]
Definition: me_cmp.h:56
int64_t mc_mb_var_sum_temp
Definition: motion_est.h:75
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:343
int16_t(* b_forw_mv_table)[2]
MV table (1MV per MB) forward mode b-frame encoding.
Definition: mpegvideo.h:318
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:200
me_cmp_func sse[6]
Definition: me_cmp.h:57
int noise_reduction
noise reduction strength
Definition: avcodec.h:1809
static int estimate_motion_thread(AVCodecContext *c, void *arg)
#define BASIS_SHIFT
MpegEncContext.
Definition: mpegvideo.h:150
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:252
char * rc_eq
Definition: mpegvideo.h:601
int8_t * qscale_table
Definition: mpegvideo.h:94
#define MAX_RUN
Definition: rl.h:34
struct AVCodecContext * avctx
Definition: mpegvideo.h:167
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1435
PutBitContext pb
bit output
Definition: mpegvideo.h:220
static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
Definition: mpegvideo.c:1577
volatile int error_count
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static void update_qscale(MpegEncContext *s)
int mb_cmp
macroblock comparison function (not supported yet)
Definition: avcodec.h:1642
int quantizer_noise_shaping
Definition: mpegvideo.h:587
int(* dct_error_sum)[64]
Definition: mpegvideo.h:398
MECmpContext mecc
Definition: mpegvideo.h:300
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
void ff_msmpeg4_encode_ext_header(MpegEncContext *s)
Definition: msmpeg4enc.c:284
float rc_initial_cplx
Definition: mpegvideo.h:596
static int pre_estimate_motion_thread(AVCodecContext *c, void *arg)
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
static const int32_t qmat16[MAT_SIZE]
Definition: hq_hqadata.c:342
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:1780
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:870
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:199
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define CANDIDATE_MB_TYPE_FORWARD
Definition: mpegutils.h:106
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:738
const uint8_t ff_h263_chroma_qscale_table[32]
Definition: h263data.h:262
uint8_t * dest[3]
Definition: mpegvideo.h:362
int shared
Definition: mpegvideo.h:135
static double c[64]
int last_pict_type
Definition: mpegvideo.h:283
#define COPY(a)
void ff_h263_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: ituh263enc.c:447
int adaptive_quant
use adaptive quantization
Definition: mpegvideo.h:278
static int16_t basis[64][64]
attribute_deprecated float border_masking
Definition: avcodec.h:1851
static int score_tab[256]
Definition: zmbvenc.c:59
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:231
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:251
Bi-dir predicted.
Definition: avutil.h:269
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
float rc_qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
Definition: mpegvideo.h:593
int64_t reordered_pts
reordered pts to be used as dts for the next output frame when there's a delay
Definition: mpegvideo.h:217
int ff_vbv_update(MpegEncContext *s, int frame_size)
Definition: ratecontrol.c:317
#define H263_GOB_HEIGHT(h)
Definition: h263.h:43
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
denominator
Definition: rational.h:45
attribute_deprecated float rc_qmod_amp
Definition: avcodec.h:2294
const uint8_t * chroma_qscale_table
qscale -> chroma_qscale (h263)
Definition: mpegvideo.h:259
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
AVCodec ff_msmpeg4v3_encoder
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
Definition: mpegvideo.c:852
int trellis
trellis RD quantization
Definition: avcodec.h:2436
void(* dct_unquantize_mpeg1_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:568
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: mpeg12enc.c:426
void ff_mpeg4_stuffing(PutBitContext *pbc)
add mpeg4 stuffing bits (01...1)
#define CANDIDATE_MB_TYPE_INTRA
Definition: mpegutils.h:100
int16_t(* blocks)[12][64]
Definition: mpegvideo.h:559
#define STOP_TIMER(id)
Definition: timer.h:93
int slices
Number of slices.
Definition: avcodec.h:1976
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
void * priv_data
Definition: avcodec.h:1283
const AVOption ff_mpv_generic_options[]
Definition: mpegvideo_enc.c:78
#define PICT_FRAME
Definition: mpegutils.h:35
int last_bits
temp var used for calculating the above vars
Definition: mpegvideo.h:419
void ff_mpeg4_init_partitions(MpegEncContext *s)
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:1234
void(* diff_pixels)(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride)
Definition: pixblockdsp.h:30
int picture_structure
Definition: mpegvideo.h:521
int dia_size
ME diamond size & shape.
Definition: avcodec.h:1671
#define av_free(p)
int b_sensitivity
Adjust sensitivity of b_frame_strategy 1.
Definition: avcodec.h:1932
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2793
VideoDSPContext vdsp
Definition: mpegvideo.h:305
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
#define VE
static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src)
void(* draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides)
#define av_log2
Definition: intmath.h:105
int ff_get_best_fcode(MpegEncContext *s, int16_t(*mv_table)[2], int type)
Definition: motion_est.c:1621
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:423
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1291
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:558
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mjpegenc.c:179
void ff_rv20_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: rv20enc.c:33
PutBitContext tex_pb
used for data partitioned VOPs
Definition: mpegvideo.h:471
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:237
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
attribute_deprecated int error_rate
Definition: avcodec.h:2966
static void set_frame_distances(MpegEncContext *s)
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:220
#define EDGE_BOTTOM
void ff_fix_long_mvs(MpegEncContext *s, uint8_t *field_select_table, int field_select, int16_t(*mv_table)[2], int f_code, int type, int truncate)
Definition: motion_est.c:1724
Picture ** reordered_input_picture
pointer to the next pictures in codedorder for encoding
Definition: mpegvideo.h:207
static const struct twinvq_data tab
unsigned int byte_buffer_size
Definition: internal.h:149
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
int ff_rv10_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: rv10enc.c:31
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1161
void(* dct_unquantize_mpeg2_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:570
static int encode_thread(AVCodecContext *c, void *arg)
void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding). ...
Definition: mpegvideo.c:1031
int height
Definition: frame.h:220
int(* fast_dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:583
void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:367
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:120
static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src)
#define av_freep(p)
static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src)
void INT64 start
Definition: avisynth_c.h:553
#define av_always_inline
Definition: attributes.h:37
#define M_PI
Definition: mathematics.h:46
Floating point AAN DCT
int inter_quant_bias
bias for the quantizer
Definition: mpegvideo.h:373
av_cold void ff_qpeldsp_init(QpelDSPContext *c)
Definition: qpeldsp.c:783
attribute_deprecated int lmin
Definition: avcodec.h:2394
#define CANDIDATE_MB_TYPE_BACKWARD
Definition: mpegutils.h:107
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1453
#define stride
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:299
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:582
#define MV_TYPE_8X8
4 vectors (h263, mpeg4 4MV)
Definition: mpegvideo.h:334
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2334
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:308
void ff_msmpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: msmpeg4enc.c:381
#define CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:713
void ff_h261_encode_init(MpegEncContext *s)
Definition: h261enc.c:365
int dct_count[2]
Definition: mpegvideo.h:399
int64_t mb_var_sum
sum of MB variance for current frame
Definition: mpegvideo.h:128
static int encode_frame(AVCodecContext *c, AVFrame *frame)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
Definition: internal.h:148
int delay
Codec delay.
Definition: avcodec.h:1402
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2543
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
int ff_check_alignment(void)
Definition: me_cmp.c:915
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:138
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
Definition: utils.c:3535
me_cmp_func ildct_cmp[6]
Definition: me_cmp.h:75
#define FFMAX3(a, b, c)
Definition: common.h:65
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
static void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], int *dmin, int *next_block, int motion_x, int motion_y)
Predicted.
Definition: avutil.h:268
unsigned int lambda
lagrange multipler used in rate distortion
Definition: mpegvideo.h:275
AVCodec ff_msmpeg4v2_encoder
uint16_t pb_time
time distance between the last b and p,s,i frame
Definition: mpegvideo.h:456
enum idct_permutation_type perm_type
Definition: idctdsp.h:95
HpelDSPContext hdsp
Definition: mpegvideo.h:298
static const uint8_t sp5x_quant_table[20][64]
Definition: sp5x.h:135
int next_lambda
next lambda used for retrying to encode a frame
Definition: mpegvideo.h:406
static int16_t block[64]
Definition: dct-test.c:110