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