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