Merge commit '2a113695238e7675ab75c5fb9a5f59fc92f2ddd9'
[ffmpeg.git] / 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  * @file
27  * The simplest mpeg encoder (well, it was the simplest!).
28  */
29
30 #include "libavutil/internal.h"
31 #include "libavutil/intmath.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/opt.h"
35 #include "avcodec.h"
36 #include "dct.h"
37 #include "dsputil.h"
38 #include "mpegvideo.h"
39 #include "h263.h"
40 #include "mathops.h"
41 #include "mjpegenc.h"
42 #include "msmpeg4.h"
43 #include "faandct.h"
44 #include "thread.h"
45 #include "aandcttab.h"
46 #include "flv.h"
47 #include "mpeg4video.h"
48 #include "internal.h"
49 #include "bytestream.h"
50 #include <limits.h>
51 #include "sp5x.h"
52
53 //#undef NDEBUG
54 //#include <assert.h>
55
56 static int encode_picture(MpegEncContext *s, int picture_number);
57 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
58 static int sse_mb(MpegEncContext *s);
59 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
60 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
61
62 //#define DEBUG
63
64 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
65 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
66
67 const AVOption ff_mpv_generic_options[] = {
68     FF_MPV_COMMON_OPTS
69     { NULL },
70 };
71
72 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
73                        uint16_t (*qmat16)[2][64],
74                        const uint16_t *quant_matrix,
75                        int bias, int qmin, int qmax, int intra)
76 {
77     int qscale;
78     int shift = 0;
79
80     for (qscale = qmin; qscale <= qmax; qscale++) {
81         int i;
82         if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
83             dsp->fdct == ff_jpeg_fdct_islow_10 ||
84             dsp->fdct == ff_faandct) {
85             for (i = 0; i < 64; i++) {
86                 const int j = dsp->idct_permutation[i];
87                 /* 16 <= qscale * quant_matrix[i] <= 7905
88                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
89                  *             19952 <=              x  <= 249205026
90                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
91                  *           3444240 >= (1 << 36) / (x) >= 275 */
92
93                 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
94                                         (qscale * quant_matrix[j]));
95             }
96         } else if (dsp->fdct == ff_fdct_ifast) {
97             for (i = 0; i < 64; i++) {
98                 const int j = dsp->idct_permutation[i];
99                 /* 16 <= qscale * quant_matrix[i] <= 7905
100                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
101                  *             19952 <=              x  <= 249205026
102                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
103                  *           3444240 >= (1 << 36) / (x) >= 275 */
104
105                 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
106                                         (ff_aanscales[i] * (int64_t)qscale * quant_matrix[j]));
107             }
108         } else {
109             for (i = 0; i < 64; i++) {
110                 const int j = dsp->idct_permutation[i];
111                 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
112                  * Assume x = qscale * quant_matrix[i]
113                  * So             16 <=              x  <= 7905
114                  * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
115                  * so          32768 >= (1 << 19) / (x) >= 67 */
116                 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
117                                         (qscale * quant_matrix[j]));
118                 //qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) /
119                 //                    (qscale * quant_matrix[i]);
120                 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
121                                        (qscale * quant_matrix[j]);
122
123                 if (qmat16[qscale][0][i] == 0 ||
124                     qmat16[qscale][0][i] == 128 * 256)
125                     qmat16[qscale][0][i] = 128 * 256 - 1;
126                 qmat16[qscale][1][i] =
127                     ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
128                                 qmat16[qscale][0][i]);
129             }
130         }
131
132         for (i = intra; i < 64; i++) {
133             int64_t max = 8191;
134             if (dsp->fdct == ff_fdct_ifast) {
135                 max = (8191LL * ff_aanscales[i]) >> 14;
136             }
137             while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
138                 shift++;
139             }
140         }
141     }
142     if (shift) {
143         av_log(NULL, AV_LOG_INFO,
144                "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
145                QMAT_SHIFT - shift);
146     }
147 }
148
149 static inline void update_qscale(MpegEncContext *s)
150 {
151     s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
152                 (FF_LAMBDA_SHIFT + 7);
153     s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
154
155     s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
156                  FF_LAMBDA_SHIFT;
157 }
158
159 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
160 {
161     int i;
162
163     if (matrix) {
164         put_bits(pb, 1, 1);
165         for (i = 0; i < 64; i++) {
166             put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
167         }
168     } else
169         put_bits(pb, 1, 0);
170 }
171
172 /**
173  * init s->current_picture.qscale_table from s->lambda_table
174  */
175 void ff_init_qscale_tab(MpegEncContext *s)
176 {
177     int8_t * const qscale_table = s->current_picture.qscale_table;
178     int i;
179
180     for (i = 0; i < s->mb_num; i++) {
181         unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
182         int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
183         qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
184                                                   s->avctx->qmax);
185     }
186 }
187
188 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst,
189                                     const AVFrame *src)
190 {
191     dst->pict_type              = src->pict_type;
192     dst->quality                = src->quality;
193     dst->coded_picture_number   = src->coded_picture_number;
194     dst->display_picture_number = src->display_picture_number;
195     //dst->reference              = src->reference;
196     dst->pts                    = src->pts;
197     dst->interlaced_frame       = src->interlaced_frame;
198     dst->top_field_first        = src->top_field_first;
199 }
200
201 static void update_duplicate_context_after_me(MpegEncContext *dst,
202                                               MpegEncContext *src)
203 {
204 #define COPY(a) dst->a= src->a
205     COPY(pict_type);
206     COPY(current_picture);
207     COPY(f_code);
208     COPY(b_code);
209     COPY(qscale);
210     COPY(lambda);
211     COPY(lambda2);
212     COPY(picture_in_gop_number);
213     COPY(gop_picture_number);
214     COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
215     COPY(progressive_frame);    // FIXME don't set in encode_header
216     COPY(partitioned_frame);    // FIXME don't set in encode_header
217 #undef COPY
218 }
219
220 /**
221  * Set the given MpegEncContext to defaults for encoding.
222  * the changed fields will not depend upon the prior state of the MpegEncContext.
223  */
224 static void MPV_encode_defaults(MpegEncContext *s)
225 {
226     int i;
227     ff_MPV_common_defaults(s);
228
229     for (i = -16; i < 16; i++) {
230         default_fcode_tab[i + MAX_MV] = 1;
231     }
232     s->me.mv_penalty = default_mv_penalty;
233     s->fcode_tab     = default_fcode_tab;
234 }
235
236 av_cold int ff_dct_encode_init(MpegEncContext *s) {
237     if (ARCH_X86)
238         ff_dct_encode_init_x86(s);
239
240     if (!s->dct_quantize)
241         s->dct_quantize = ff_dct_quantize_c;
242     if (!s->denoise_dct)
243         s->denoise_dct  = denoise_dct_c;
244     s->fast_dct_quantize = s->dct_quantize;
245     if (s->avctx->trellis)
246         s->dct_quantize  = dct_quantize_trellis_c;
247
248     return 0;
249 }
250
251 /* init video encoder */
252 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
253 {
254     MpegEncContext *s = avctx->priv_data;
255     int i;
256     int chroma_h_shift, chroma_v_shift;
257
258     MPV_encode_defaults(s);
259
260     switch (avctx->codec_id) {
261     case AV_CODEC_ID_MPEG2VIDEO:
262         if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
263             avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
264             av_log(avctx, AV_LOG_ERROR,
265                    "only YUV420 and YUV422 are supported\n");
266             return -1;
267         }
268         break;
269     case AV_CODEC_ID_LJPEG:
270         if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
271             avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
272             avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
273             avctx->pix_fmt != AV_PIX_FMT_BGR0     &&
274             avctx->pix_fmt != AV_PIX_FMT_BGRA     &&
275             avctx->pix_fmt != AV_PIX_FMT_BGR24    &&
276             ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
277               avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
278               avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
279              avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
280             av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
281             return -1;
282         }
283         break;
284     case AV_CODEC_ID_MJPEG:
285     case AV_CODEC_ID_AMV:
286         if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
287             avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
288             avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
289             ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
290               avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
291               avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
292              avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
293             av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
294             return -1;
295         }
296         break;
297     default:
298         if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
299             av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
300             return -1;
301         }
302     }
303
304     switch (avctx->pix_fmt) {
305     case AV_PIX_FMT_YUVJ444P:
306     case AV_PIX_FMT_YUV444P:
307         s->chroma_format = CHROMA_444;
308         break;
309     case AV_PIX_FMT_YUVJ422P:
310     case AV_PIX_FMT_YUV422P:
311         s->chroma_format = CHROMA_422;
312         break;
313     case AV_PIX_FMT_YUVJ420P:
314     case AV_PIX_FMT_YUV420P:
315     default:
316         s->chroma_format = CHROMA_420;
317         break;
318     }
319
320     s->bit_rate = avctx->bit_rate;
321     s->width    = avctx->width;
322     s->height   = avctx->height;
323     if (avctx->gop_size > 600 &&
324         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
325         av_log(avctx, AV_LOG_WARNING,
326                "keyframe interval too large!, reducing it from %d to %d\n",
327                avctx->gop_size, 600);
328         avctx->gop_size = 600;
329     }
330     s->gop_size     = avctx->gop_size;
331     s->avctx        = avctx;
332     s->flags        = avctx->flags;
333     s->flags2       = avctx->flags2;
334     s->max_b_frames = avctx->max_b_frames;
335     s->codec_id     = avctx->codec->id;
336     s->strict_std_compliance = avctx->strict_std_compliance;
337     s->quarter_sample     = (avctx->flags & CODEC_FLAG_QPEL) != 0;
338     s->mpeg_quant         = avctx->mpeg_quant;
339     s->rtp_mode           = !!avctx->rtp_payload_size;
340     s->intra_dc_precision = avctx->intra_dc_precision;
341     s->user_specified_pts = AV_NOPTS_VALUE;
342
343     if (s->gop_size <= 1) {
344         s->intra_only = 1;
345         s->gop_size   = 12;
346     } else {
347         s->intra_only = 0;
348     }
349
350     s->me_method = avctx->me_method;
351
352     /* Fixed QSCALE */
353     s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
354
355     s->adaptive_quant = (s->avctx->lumi_masking ||
356                          s->avctx->dark_masking ||
357                          s->avctx->temporal_cplx_masking ||
358                          s->avctx->spatial_cplx_masking  ||
359                          s->avctx->p_masking      ||
360                          s->avctx->border_masking ||
361                          (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
362                         !s->fixed_qscale;
363
364     s->loop_filter      = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
365
366     if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
367         switch(avctx->codec_id) {
368         case AV_CODEC_ID_MPEG1VIDEO:
369         case AV_CODEC_ID_MPEG2VIDEO:
370             avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112L / 15000000 * 16384;
371             break;
372         case AV_CODEC_ID_MPEG4:
373         case AV_CODEC_ID_MSMPEG4V1:
374         case AV_CODEC_ID_MSMPEG4V2:
375         case AV_CODEC_ID_MSMPEG4V3:
376             if       (avctx->rc_max_rate >= 15000000) {
377                 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000L) * (760-320) / (38400000 - 15000000);
378             } else if(avctx->rc_max_rate >=  2000000) {
379                 avctx->rc_buffer_size =  80 + (avctx->rc_max_rate -  2000000L) * (320- 80) / (15000000 -  2000000);
380             } else if(avctx->rc_max_rate >=   384000) {
381                 avctx->rc_buffer_size =  40 + (avctx->rc_max_rate -   384000L) * ( 80- 40) / ( 2000000 -   384000);
382             } else
383                 avctx->rc_buffer_size = 40;
384             avctx->rc_buffer_size *= 16384;
385             break;
386         }
387         if (avctx->rc_buffer_size) {
388             av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
389         }
390     }
391
392     if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
393         av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
394         if (avctx->rc_max_rate && !avctx->rc_buffer_size)
395             return -1;
396     }
397
398     if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
399         av_log(avctx, AV_LOG_INFO,
400                "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
401     }
402
403     if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
404         av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
405         return -1;
406     }
407
408     if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
409         av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
410         return -1;
411     }
412
413     if (avctx->rc_max_rate &&
414         avctx->rc_max_rate == avctx->bit_rate &&
415         avctx->rc_max_rate != avctx->rc_min_rate) {
416         av_log(avctx, AV_LOG_INFO,
417                "impossible bitrate constraints, this will fail\n");
418     }
419
420     if (avctx->rc_buffer_size &&
421         avctx->bit_rate * (int64_t)avctx->time_base.num >
422             avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
423         av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
424         return -1;
425     }
426
427     if (!s->fixed_qscale &&
428         avctx->bit_rate * av_q2d(avctx->time_base) >
429             avctx->bit_rate_tolerance) {
430         av_log(avctx, AV_LOG_ERROR,
431                "bitrate tolerance too small for bitrate\n");
432         return -1;
433     }
434
435     if (s->avctx->rc_max_rate &&
436         s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
437         (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
438          s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
439         90000LL * (avctx->rc_buffer_size - 1) >
440             s->avctx->rc_max_rate * 0xFFFFLL) {
441         av_log(avctx, AV_LOG_INFO,
442                "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
443                "specified vbv buffer is too large for the given bitrate!\n");
444     }
445
446     if ((s->flags & CODEC_FLAG_4MV)  && s->codec_id != AV_CODEC_ID_MPEG4 &&
447         s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
448         s->codec_id != AV_CODEC_ID_FLV1) {
449         av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
450         return -1;
451     }
452
453     if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
454         av_log(avctx, AV_LOG_ERROR,
455                "OBMC is only supported with simple mb decision\n");
456         return -1;
457     }
458
459     if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
460         av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
461         return -1;
462     }
463
464     if (s->max_b_frames                    &&
465         s->codec_id != AV_CODEC_ID_MPEG4      &&
466         s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
467         s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
468         av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
469         return -1;
470     }
471
472     if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
473          s->codec_id == AV_CODEC_ID_H263  ||
474          s->codec_id == AV_CODEC_ID_H263P) &&
475         (avctx->sample_aspect_ratio.num > 255 ||
476          avctx->sample_aspect_ratio.den > 255)) {
477         av_log(avctx, AV_LOG_WARNING,
478                "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
479                avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
480         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
481                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den, 255);
482     }
483
484     if ((s->codec_id == AV_CODEC_ID_H263  ||
485          s->codec_id == AV_CODEC_ID_H263P) &&
486         (avctx->width  > 2048 ||
487          avctx->height > 1152 )) {
488         av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
489         return -1;
490     }
491     if ((s->codec_id == AV_CODEC_ID_H263  ||
492          s->codec_id == AV_CODEC_ID_H263P) &&
493         ((avctx->width &3) ||
494          (avctx->height&3) )) {
495         av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
496         return -1;
497     }
498
499     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
500         (avctx->width  > 4095 ||
501          avctx->height > 4095 )) {
502         av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
503         return -1;
504     }
505
506     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
507         (avctx->width  > 16383 ||
508          avctx->height > 16383 )) {
509         av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
510         return -1;
511     }
512
513     if (s->codec_id == AV_CODEC_ID_RV10 &&
514         (avctx->width &15 ||
515          avctx->height&15 )) {
516         av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
517         return AVERROR(EINVAL);
518     }
519
520     if (s->codec_id == AV_CODEC_ID_RV20 &&
521         (avctx->width &3 ||
522          avctx->height&3 )) {
523         av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
524         return AVERROR(EINVAL);
525     }
526
527     if ((s->codec_id == AV_CODEC_ID_WMV1 ||
528          s->codec_id == AV_CODEC_ID_WMV2) &&
529          avctx->width & 1) {
530          av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
531          return -1;
532     }
533
534     if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
535         s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
536         av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
537         return -1;
538     }
539
540     // FIXME mpeg2 uses that too
541     if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
542         av_log(avctx, AV_LOG_ERROR,
543                "mpeg2 style quantization not supported by codec\n");
544         return -1;
545     }
546
547     if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
548         av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
549         return -1;
550     }
551
552     if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
553         s->avctx->mb_decision != FF_MB_DECISION_RD) {
554         av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
555         return -1;
556     }
557
558     if (s->avctx->scenechange_threshold < 1000000000 &&
559         (s->flags & CODEC_FLAG_CLOSED_GOP)) {
560         av_log(avctx, AV_LOG_ERROR,
561                "closed gop with scene change detection are not supported yet, "
562                "set threshold to 1000000000\n");
563         return -1;
564     }
565
566     if (s->flags & CODEC_FLAG_LOW_DELAY) {
567         if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
568             av_log(avctx, AV_LOG_ERROR,
569                   "low delay forcing is only available for mpeg2\n");
570             return -1;
571         }
572         if (s->max_b_frames != 0) {
573             av_log(avctx, AV_LOG_ERROR,
574                    "b frames cannot be used with low delay\n");
575             return -1;
576         }
577     }
578
579     if (s->q_scale_type == 1) {
580         if (avctx->qmax > 12) {
581             av_log(avctx, AV_LOG_ERROR,
582                    "non linear quant only supports qmax <= 12 currently\n");
583             return -1;
584         }
585     }
586
587     if (s->avctx->thread_count > 1         &&
588         s->codec_id != AV_CODEC_ID_MPEG4      &&
589         s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
590         s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
591         s->codec_id != AV_CODEC_ID_MJPEG      &&
592         (s->codec_id != AV_CODEC_ID_H263P)) {
593         av_log(avctx, AV_LOG_ERROR,
594                "multi threaded encoding not supported by codec\n");
595         return -1;
596     }
597
598     if (s->avctx->thread_count < 1) {
599         av_log(avctx, AV_LOG_ERROR,
600                "automatic thread number detection not supported by codec, "
601                "patch welcome\n");
602         return -1;
603     }
604
605     if (s->avctx->slices > 1 || s->avctx->thread_count > 1)
606         s->rtp_mode = 1;
607
608     if (s->avctx->thread_count > 1 && s->codec_id == AV_CODEC_ID_H263P)
609         s->h263_slice_structured = 1;
610
611     if (!avctx->time_base.den || !avctx->time_base.num) {
612         av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
613         return -1;
614     }
615
616     i = (INT_MAX / 2 + 128) >> 8;
617     if (avctx->mb_threshold >= i) {
618         av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
619                i - 1);
620         return -1;
621     }
622
623     if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
624         av_log(avctx, AV_LOG_INFO,
625                "notice: b_frame_strategy only affects the first pass\n");
626         avctx->b_frame_strategy = 0;
627     }
628
629     i = av_gcd(avctx->time_base.den, avctx->time_base.num);
630     if (i > 1) {
631         av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
632         avctx->time_base.den /= i;
633         avctx->time_base.num /= i;
634         //return -1;
635     }
636
637     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) {
638         // (a + x * 3 / 8) / x
639         s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
640         s->inter_quant_bias = 0;
641     } else {
642         s->intra_quant_bias = 0;
643         // (a - x / 4) / x
644         s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
645     }
646
647     if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
648         s->intra_quant_bias = avctx->intra_quant_bias;
649     if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
650         s->inter_quant_bias = avctx->inter_quant_bias;
651
652     av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
653
654     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
655
656     if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
657         s->avctx->time_base.den > (1 << 16) - 1) {
658         av_log(avctx, AV_LOG_ERROR,
659                "timebase %d/%d not supported by MPEG 4 standard, "
660                "the maximum admitted value for the timebase denominator "
661                "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
662                (1 << 16) - 1);
663         return -1;
664     }
665     s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
666
667     switch (avctx->codec->id) {
668     case AV_CODEC_ID_MPEG1VIDEO:
669         s->out_format = FMT_MPEG1;
670         s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
671         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
672         break;
673     case AV_CODEC_ID_MPEG2VIDEO:
674         s->out_format = FMT_MPEG1;
675         s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
676         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
677         s->rtp_mode   = 1;
678         break;
679     case AV_CODEC_ID_LJPEG:
680     case AV_CODEC_ID_MJPEG:
681     case AV_CODEC_ID_AMV:
682         s->out_format = FMT_MJPEG;
683         s->intra_only = 1; /* force intra only for jpeg */
684         if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
685             (avctx->pix_fmt == AV_PIX_FMT_BGR0
686              || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
687              || s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
688             s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
689             s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
690             s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
691         } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
692             s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
693             s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
694         } else {
695             s->mjpeg_vsample[0] = 2;
696             s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
697             s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
698             s->mjpeg_hsample[0] = 2;
699             s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
700             s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
701         }
702         if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
703             ff_mjpeg_encode_init(s) < 0)
704             return -1;
705         avctx->delay = 0;
706         s->low_delay = 1;
707         break;
708     case AV_CODEC_ID_H261:
709         if (!CONFIG_H261_ENCODER)
710             return -1;
711         if (ff_h261_get_picture_format(s->width, s->height) < 0) {
712             av_log(avctx, AV_LOG_ERROR,
713                    "The specified picture size of %dx%d is not valid for the "
714                    "H.261 codec.\nValid sizes are 176x144, 352x288\n",
715                     s->width, s->height);
716             return -1;
717         }
718         s->out_format = FMT_H261;
719         avctx->delay  = 0;
720         s->low_delay  = 1;
721         break;
722     case AV_CODEC_ID_H263:
723         if (!CONFIG_H263_ENCODER)
724             return -1;
725         if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
726                              s->width, s->height) == 8) {
727             av_log(avctx, AV_LOG_ERROR,
728                    "The specified picture size of %dx%d is not valid for "
729                    "the H.263 codec.\nValid sizes are 128x96, 176x144, "
730                    "352x288, 704x576, and 1408x1152. "
731                    "Try H.263+.\n", s->width, s->height);
732             return -1;
733         }
734         s->out_format = FMT_H263;
735         avctx->delay  = 0;
736         s->low_delay  = 1;
737         break;
738     case AV_CODEC_ID_H263P:
739         s->out_format = FMT_H263;
740         s->h263_plus  = 1;
741         /* Fx */
742         s->h263_aic        = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
743         s->modified_quant  = s->h263_aic;
744         s->loop_filter     = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
745         s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
746
747         /* /Fx */
748         /* These are just to be sure */
749         avctx->delay = 0;
750         s->low_delay = 1;
751         break;
752     case AV_CODEC_ID_FLV1:
753         s->out_format      = FMT_H263;
754         s->h263_flv        = 2; /* format = 1; 11-bit codes */
755         s->unrestricted_mv = 1;
756         s->rtp_mode  = 0; /* don't allow GOB */
757         avctx->delay = 0;
758         s->low_delay = 1;
759         break;
760     case AV_CODEC_ID_RV10:
761         s->out_format = FMT_H263;
762         avctx->delay  = 0;
763         s->low_delay  = 1;
764         break;
765     case AV_CODEC_ID_RV20:
766         s->out_format      = FMT_H263;
767         avctx->delay       = 0;
768         s->low_delay       = 1;
769         s->modified_quant  = 1;
770         s->h263_aic        = 1;
771         s->h263_plus       = 1;
772         s->loop_filter     = 1;
773         s->unrestricted_mv = 0;
774         break;
775     case AV_CODEC_ID_MPEG4:
776         s->out_format      = FMT_H263;
777         s->h263_pred       = 1;
778         s->unrestricted_mv = 1;
779         s->low_delay       = s->max_b_frames ? 0 : 1;
780         avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
781         break;
782     case AV_CODEC_ID_MSMPEG4V2:
783         s->out_format      = FMT_H263;
784         s->h263_pred       = 1;
785         s->unrestricted_mv = 1;
786         s->msmpeg4_version = 2;
787         avctx->delay       = 0;
788         s->low_delay       = 1;
789         break;
790     case AV_CODEC_ID_MSMPEG4V3:
791         s->out_format        = FMT_H263;
792         s->h263_pred         = 1;
793         s->unrestricted_mv   = 1;
794         s->msmpeg4_version   = 3;
795         s->flipflop_rounding = 1;
796         avctx->delay         = 0;
797         s->low_delay         = 1;
798         break;
799     case AV_CODEC_ID_WMV1:
800         s->out_format        = FMT_H263;
801         s->h263_pred         = 1;
802         s->unrestricted_mv   = 1;
803         s->msmpeg4_version   = 4;
804         s->flipflop_rounding = 1;
805         avctx->delay         = 0;
806         s->low_delay         = 1;
807         break;
808     case AV_CODEC_ID_WMV2:
809         s->out_format        = FMT_H263;
810         s->h263_pred         = 1;
811         s->unrestricted_mv   = 1;
812         s->msmpeg4_version   = 5;
813         s->flipflop_rounding = 1;
814         avctx->delay         = 0;
815         s->low_delay         = 1;
816         break;
817     default:
818         return -1;
819     }
820
821     avctx->has_b_frames = !s->low_delay;
822
823     s->encoding = 1;
824
825     s->progressive_frame    =
826     s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
827                                                 CODEC_FLAG_INTERLACED_ME) ||
828                                 s->alternate_scan);
829
830     /* init */
831     if (ff_MPV_common_init(s) < 0)
832         return -1;
833
834     ff_dct_encode_init(s);
835
836     if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
837         s->chroma_qscale_table = ff_h263_chroma_qscale_table;
838
839     s->quant_precision = 5;
840
841     ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
842     ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
843
844     if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
845         ff_h261_encode_init(s);
846     if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
847         ff_h263_encode_init(s);
848     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
849         ff_msmpeg4_encode_init(s);
850     if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
851         && s->out_format == FMT_MPEG1)
852         ff_mpeg1_encode_init(s);
853
854     /* init q matrix */
855     for (i = 0; i < 64; i++) {
856         int j = s->dsp.idct_permutation[i];
857         if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
858             s->mpeg_quant) {
859             s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
860             s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
861         } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
862             s->intra_matrix[j] =
863             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
864         } else {
865             /* mpeg1/2 */
866             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
867             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
868         }
869         if (s->avctx->intra_matrix)
870             s->intra_matrix[j] = s->avctx->intra_matrix[i];
871         if (s->avctx->inter_matrix)
872             s->inter_matrix[j] = s->avctx->inter_matrix[i];
873     }
874
875     /* precompute matrix */
876     /* for mjpeg, we do include qscale in the matrix */
877     if (s->out_format != FMT_MJPEG) {
878         ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
879                           s->intra_matrix, s->intra_quant_bias, avctx->qmin,
880                           31, 1);
881         ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
882                           s->inter_matrix, s->inter_quant_bias, avctx->qmin,
883                           31, 0);
884     }
885
886     if (ff_rate_control_init(s) < 0)
887         return -1;
888
889     return 0;
890 }
891
892 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
893 {
894     MpegEncContext *s = avctx->priv_data;
895
896     ff_rate_control_uninit(s);
897
898     ff_MPV_common_end(s);
899     if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
900         s->out_format == FMT_MJPEG)
901         ff_mjpeg_encode_close(s);
902
903     av_freep(&avctx->extradata);
904
905     return 0;
906 }
907
908 static int get_sae(uint8_t *src, int ref, int stride)
909 {
910     int x,y;
911     int acc = 0;
912
913     for (y = 0; y < 16; y++) {
914         for (x = 0; x < 16; x++) {
915             acc += FFABS(src[x + y * stride] - ref);
916         }
917     }
918
919     return acc;
920 }
921
922 static int get_intra_count(MpegEncContext *s, uint8_t *src,
923                            uint8_t *ref, int stride)
924 {
925     int x, y, w, h;
926     int acc = 0;
927
928     w = s->width  & ~15;
929     h = s->height & ~15;
930
931     for (y = 0; y < h; y += 16) {
932         for (x = 0; x < w; x += 16) {
933             int offset = x + y * stride;
934             int sad  = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
935                                      16);
936             int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
937             int sae  = get_sae(src + offset, mean, stride);
938
939             acc += sae + 500 < sad;
940         }
941     }
942     return acc;
943 }
944
945
946 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
947 {
948     Picture *pic = NULL;
949     int64_t pts;
950     int i, display_picture_number = 0, ret;
951     const int encoding_delay = s->max_b_frames ? s->max_b_frames :
952                                                  (s->low_delay ? 0 : 1);
953     int direct = 1;
954
955     if (pic_arg) {
956         pts = pic_arg->pts;
957         display_picture_number = s->input_picture_number++;
958
959         if (pts != AV_NOPTS_VALUE) {
960             if (s->user_specified_pts != AV_NOPTS_VALUE) {
961                 int64_t time = pts;
962                 int64_t last = s->user_specified_pts;
963
964                 if (time <= last) {
965                     av_log(s->avctx, AV_LOG_ERROR,
966                            "Error, Invalid timestamp=%"PRId64", "
967                            "last=%"PRId64"\n", pts, s->user_specified_pts);
968                     return -1;
969                 }
970
971                 if (!s->low_delay && display_picture_number == 1)
972                     s->dts_delta = time - last;
973             }
974             s->user_specified_pts = pts;
975         } else {
976             if (s->user_specified_pts != AV_NOPTS_VALUE) {
977                 s->user_specified_pts =
978                 pts = s->user_specified_pts + 1;
979                 av_log(s->avctx, AV_LOG_INFO,
980                        "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
981                        pts);
982             } else {
983                 pts = display_picture_number;
984             }
985         }
986     }
987
988     if (pic_arg) {
989         if (!pic_arg->buf[0]);
990             direct = 0;
991         if (pic_arg->linesize[0] != s->linesize)
992             direct = 0;
993         if (pic_arg->linesize[1] != s->uvlinesize)
994             direct = 0;
995         if (pic_arg->linesize[2] != s->uvlinesize)
996             direct = 0;
997
998         av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
999                 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1000
1001         if (direct) {
1002             i = ff_find_unused_picture(s, 1);
1003             if (i < 0)
1004                 return i;
1005
1006             pic = &s->picture[i];
1007             pic->reference = 3;
1008
1009             if ((ret = av_frame_ref(&pic->f, pic_arg)) < 0)
1010                 return ret;
1011             if (ff_alloc_picture(s, pic, 1) < 0) {
1012                 return -1;
1013             }
1014         } else {
1015             i = ff_find_unused_picture(s, 0);
1016             if (i < 0)
1017                 return i;
1018
1019             pic = &s->picture[i];
1020             pic->reference = 3;
1021
1022             if (ff_alloc_picture(s, pic, 0) < 0) {
1023                 return -1;
1024             }
1025
1026             if (pic->f.data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1027                 pic->f.data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1028                 pic->f.data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1029                 // empty
1030             } else {
1031                 int h_chroma_shift, v_chroma_shift;
1032                 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1033                                                  &h_chroma_shift,
1034                                                  &v_chroma_shift);
1035
1036                 for (i = 0; i < 3; i++) {
1037                     int src_stride = pic_arg->linesize[i];
1038                     int dst_stride = i ? s->uvlinesize : s->linesize;
1039                     int h_shift = i ? h_chroma_shift : 0;
1040                     int v_shift = i ? v_chroma_shift : 0;
1041                     int w = s->width  >> h_shift;
1042                     int h = s->height >> v_shift;
1043                     uint8_t *src = pic_arg->data[i];
1044                     uint8_t *dst = pic->f.data[i];
1045
1046                     if (s->codec_id == AV_CODEC_ID_AMV && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1047                         h = ((s->height + 15)/16*16) >> v_shift;
1048                     }
1049
1050                     if (!s->avctx->rc_buffer_size)
1051                         dst += INPLACE_OFFSET;
1052
1053                     if (src_stride == dst_stride)
1054                         memcpy(dst, src, src_stride * h);
1055                     else {
1056                         int h2 = h;
1057                         uint8_t *dst2 = dst;
1058                         while (h2--) {
1059                             memcpy(dst2, src, w);
1060                             dst2 += dst_stride;
1061                             src += src_stride;
1062                         }
1063                     }
1064                     if ((s->width & 15) || (s->height & 15)) {
1065                         s->dsp.draw_edges(dst, dst_stride,
1066                                           w, h,
1067                                           16>>h_shift,
1068                                           16>>v_shift,
1069                                           EDGE_BOTTOM);
1070                     }
1071                 }
1072             }
1073         }
1074         copy_picture_attributes(s, &pic->f, pic_arg);
1075         pic->f.display_picture_number = display_picture_number;
1076         pic->f.pts = pts; // we set this here to avoid modifiying pic_arg
1077     }
1078
1079     /* shift buffer entries */
1080     for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1081         s->input_picture[i - 1] = s->input_picture[i];
1082
1083     s->input_picture[encoding_delay] = (Picture*) pic;
1084
1085     return 0;
1086 }
1087
1088 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1089 {
1090     int x, y, plane;
1091     int score = 0;
1092     int64_t score64 = 0;
1093
1094     for (plane = 0; plane < 3; plane++) {
1095         const int stride = p->f.linesize[plane];
1096         const int bw = plane ? 1 : 2;
1097         for (y = 0; y < s->mb_height * bw; y++) {
1098             for (x = 0; x < s->mb_width * bw; x++) {
1099                 int off = p->shared ? 0 : 16;
1100                 uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
1101                 uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1102                 int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1103
1104                 switch (s->avctx->frame_skip_exp) {
1105                 case 0: score    =  FFMAX(score, v);          break;
1106                 case 1: score   += FFABS(v);                  break;
1107                 case 2: score   += v * v;                     break;
1108                 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1109                 case 4: score64 += v * v * (int64_t)(v * v);  break;
1110                 }
1111             }
1112         }
1113     }
1114
1115     if (score)
1116         score64 = score;
1117
1118     if (score64 < s->avctx->frame_skip_threshold)
1119         return 1;
1120     if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1121         return 1;
1122     return 0;
1123 }
1124
1125 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1126 {
1127     AVPacket pkt = { 0 };
1128     int ret, got_output;
1129
1130     av_init_packet(&pkt);
1131     ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1132     if (ret < 0)
1133         return ret;
1134
1135     ret = pkt.size;
1136     av_free_packet(&pkt);
1137     return ret;
1138 }
1139
1140 static int estimate_best_b_count(MpegEncContext *s)
1141 {
1142     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1143     AVCodecContext *c = avcodec_alloc_context3(NULL);
1144     AVFrame input[FF_MAX_B_FRAMES + 2];
1145     const int scale = s->avctx->brd_scale;
1146     int i, j, out_size, p_lambda, b_lambda, lambda2;
1147     int64_t best_rd  = INT64_MAX;
1148     int best_b_count = -1;
1149
1150     av_assert0(scale >= 0 && scale <= 3);
1151
1152     //emms_c();
1153     //s->next_picture_ptr->quality;
1154     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1155     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1156     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1157     if (!b_lambda) // FIXME we should do this somewhere else
1158         b_lambda = p_lambda;
1159     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1160                FF_LAMBDA_SHIFT;
1161
1162     c->width        = s->width  >> scale;
1163     c->height       = s->height >> scale;
1164     c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
1165                       CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
1166     c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
1167     c->mb_decision  = s->avctx->mb_decision;
1168     c->me_cmp       = s->avctx->me_cmp;
1169     c->mb_cmp       = s->avctx->mb_cmp;
1170     c->me_sub_cmp   = s->avctx->me_sub_cmp;
1171     c->pix_fmt      = AV_PIX_FMT_YUV420P;
1172     c->time_base    = s->avctx->time_base;
1173     c->max_b_frames = s->max_b_frames;
1174
1175     if (avcodec_open2(c, codec, NULL) < 0)
1176         return -1;
1177
1178     for (i = 0; i < s->max_b_frames + 2; i++) {
1179         int ysize = c->width * c->height;
1180         int csize = (c->width / 2) * (c->height / 2);
1181         Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1182                                                 s->next_picture_ptr;
1183
1184         avcodec_get_frame_defaults(&input[i]);
1185         input[i].data[0]     = av_malloc(ysize + 2 * csize);
1186         input[i].data[1]     = input[i].data[0] + ysize;
1187         input[i].data[2]     = input[i].data[1] + csize;
1188         input[i].linesize[0] = c->width;
1189         input[i].linesize[1] =
1190         input[i].linesize[2] = c->width / 2;
1191
1192         if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1193             pre_input = *pre_input_ptr;
1194
1195             if (!pre_input.shared && i) {
1196                 pre_input.f.data[0] += INPLACE_OFFSET;
1197                 pre_input.f.data[1] += INPLACE_OFFSET;
1198                 pre_input.f.data[2] += INPLACE_OFFSET;
1199             }
1200
1201             s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
1202                                  pre_input.f.data[0], pre_input.f.linesize[0],
1203                                  c->width,      c->height);
1204             s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
1205                                  pre_input.f.data[1], pre_input.f.linesize[1],
1206                                  c->width >> 1, c->height >> 1);
1207             s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
1208                                  pre_input.f.data[2], pre_input.f.linesize[2],
1209                                  c->width >> 1, c->height >> 1);
1210         }
1211     }
1212
1213     for (j = 0; j < s->max_b_frames + 1; j++) {
1214         int64_t rd = 0;
1215
1216         if (!s->input_picture[j])
1217             break;
1218
1219         c->error[0] = c->error[1] = c->error[2] = 0;
1220
1221         input[0].pict_type = AV_PICTURE_TYPE_I;
1222         input[0].quality   = 1 * FF_QP2LAMBDA;
1223
1224         out_size = encode_frame(c, &input[0]);
1225
1226         //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1227
1228         for (i = 0; i < s->max_b_frames + 1; i++) {
1229             int is_p = i % (j + 1) == j || i == s->max_b_frames;
1230
1231             input[i + 1].pict_type = is_p ?
1232                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1233             input[i + 1].quality   = is_p ? p_lambda : b_lambda;
1234
1235             out_size = encode_frame(c, &input[i + 1]);
1236
1237             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1238         }
1239
1240         /* get the delayed frames */
1241         while (out_size) {
1242             out_size = encode_frame(c, NULL);
1243             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1244         }
1245
1246         rd += c->error[0] + c->error[1] + c->error[2];
1247
1248         if (rd < best_rd) {
1249             best_rd = rd;
1250             best_b_count = j;
1251         }
1252     }
1253
1254     avcodec_close(c);
1255     av_freep(&c);
1256
1257     for (i = 0; i < s->max_b_frames + 2; i++) {
1258         av_freep(&input[i].data[0]);
1259     }
1260
1261     return best_b_count;
1262 }
1263
1264 static int select_input_picture(MpegEncContext *s)
1265 {
1266     int i, ret;
1267
1268     for (i = 1; i < MAX_PICTURE_COUNT; i++)
1269         s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1270     s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1271
1272     /* set next picture type & ordering */
1273     if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1274         if (/*s->picture_in_gop_number >= s->gop_size ||*/
1275             s->next_picture_ptr == NULL || s->intra_only) {
1276             s->reordered_input_picture[0] = s->input_picture[0];
1277             s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
1278             s->reordered_input_picture[0]->f.coded_picture_number =
1279                 s->coded_picture_number++;
1280         } else {
1281             int b_frames;
1282
1283             if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1284                 if (s->picture_in_gop_number < s->gop_size &&
1285                     skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1286                     // FIXME check that te gop check above is +-1 correct
1287                     av_frame_unref(&s->input_picture[0]->f);
1288
1289                     emms_c();
1290                     ff_vbv_update(s, 0);
1291
1292                     goto no_output_pic;
1293                 }
1294             }
1295
1296             if (s->flags & CODEC_FLAG_PASS2) {
1297                 for (i = 0; i < s->max_b_frames + 1; i++) {
1298                     int pict_num = s->input_picture[0]->f.display_picture_number + i;
1299
1300                     if (pict_num >= s->rc_context.num_entries)
1301                         break;
1302                     if (!s->input_picture[i]) {
1303                         s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1304                         break;
1305                     }
1306
1307                     s->input_picture[i]->f.pict_type =
1308                         s->rc_context.entry[pict_num].new_pict_type;
1309                 }
1310             }
1311
1312             if (s->avctx->b_frame_strategy == 0) {
1313                 b_frames = s->max_b_frames;
1314                 while (b_frames && !s->input_picture[b_frames])
1315                     b_frames--;
1316             } else if (s->avctx->b_frame_strategy == 1) {
1317                 for (i = 1; i < s->max_b_frames + 1; i++) {
1318                     if (s->input_picture[i] &&
1319                         s->input_picture[i]->b_frame_score == 0) {
1320                         s->input_picture[i]->b_frame_score =
1321                             get_intra_count(s,
1322                                             s->input_picture[i    ]->f.data[0],
1323                                             s->input_picture[i - 1]->f.data[0],
1324                                             s->linesize) + 1;
1325                     }
1326                 }
1327                 for (i = 0; i < s->max_b_frames + 1; i++) {
1328                     if (s->input_picture[i] == NULL ||
1329                         s->input_picture[i]->b_frame_score - 1 >
1330                             s->mb_num / s->avctx->b_sensitivity)
1331                         break;
1332                 }
1333
1334                 b_frames = FFMAX(0, i - 1);
1335
1336                 /* reset scores */
1337                 for (i = 0; i < b_frames + 1; i++) {
1338                     s->input_picture[i]->b_frame_score = 0;
1339                 }
1340             } else if (s->avctx->b_frame_strategy == 2) {
1341                 b_frames = estimate_best_b_count(s);
1342             } else {
1343                 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1344                 b_frames = 0;
1345             }
1346
1347             emms_c();
1348
1349             for (i = b_frames - 1; i >= 0; i--) {
1350                 int type = s->input_picture[i]->f.pict_type;
1351                 if (type && type != AV_PICTURE_TYPE_B)
1352                     b_frames = i;
1353             }
1354             if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B &&
1355                 b_frames == s->max_b_frames) {
1356                 av_log(s->avctx, AV_LOG_ERROR,
1357                        "warning, too many b frames in a row\n");
1358             }
1359
1360             if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1361                 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1362                     s->gop_size > s->picture_in_gop_number) {
1363                     b_frames = s->gop_size - s->picture_in_gop_number - 1;
1364                 } else {
1365                     if (s->flags & CODEC_FLAG_CLOSED_GOP)
1366                         b_frames = 0;
1367                     s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
1368                 }
1369             }
1370
1371             if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1372                 s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_I)
1373                 b_frames--;
1374
1375             s->reordered_input_picture[0] = s->input_picture[b_frames];
1376             if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1377                 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
1378             s->reordered_input_picture[0]->f.coded_picture_number =
1379                 s->coded_picture_number++;
1380             for (i = 0; i < b_frames; i++) {
1381                 s->reordered_input_picture[i + 1] = s->input_picture[i];
1382                 s->reordered_input_picture[i + 1]->f.pict_type =
1383                     AV_PICTURE_TYPE_B;
1384                 s->reordered_input_picture[i + 1]->f.coded_picture_number =
1385                     s->coded_picture_number++;
1386             }
1387         }
1388     }
1389 no_output_pic:
1390     if (s->reordered_input_picture[0]) {
1391         s->reordered_input_picture[0]->reference =
1392            s->reordered_input_picture[0]->f.pict_type !=
1393                AV_PICTURE_TYPE_B ? 3 : 0;
1394
1395         ff_mpeg_unref_picture(s, &s->new_picture);
1396         if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
1397             return ret;
1398
1399         if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1400             // input is a shared pix, so we can't modifiy it -> alloc a new
1401             // one & ensure that the shared one is reuseable
1402
1403             Picture *pic;
1404             int i = ff_find_unused_picture(s, 0);
1405             if (i < 0)
1406                 return i;
1407             pic = &s->picture[i];
1408
1409             pic->reference = s->reordered_input_picture[0]->reference;
1410             if (ff_alloc_picture(s, pic, 0) < 0) {
1411                 return -1;
1412             }
1413
1414             copy_picture_attributes(s, &pic->f,
1415                                     &s->reordered_input_picture[0]->f);
1416
1417             /* mark us unused / free shared pic */
1418             av_frame_unref(&s->reordered_input_picture[0]->f);
1419             s->reordered_input_picture[0]->shared = 0;
1420
1421             s->current_picture_ptr = pic;
1422         } else {
1423             // input is not a shared pix -> reuse buffer for current_pix
1424             s->current_picture_ptr = s->reordered_input_picture[0];
1425             for (i = 0; i < 4; i++) {
1426                 s->new_picture.f.data[i] += INPLACE_OFFSET;
1427             }
1428         }
1429         ff_mpeg_unref_picture(s, &s->current_picture);
1430         if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1431                                        s->current_picture_ptr)) < 0)
1432             return ret;
1433
1434         s->picture_number = s->new_picture.f.display_picture_number;
1435     } else {
1436         ff_mpeg_unref_picture(s, &s->new_picture);
1437     }
1438     return 0;
1439 }
1440
1441 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1442                           AVFrame *pic_arg, int *got_packet)
1443 {
1444     MpegEncContext *s = avctx->priv_data;
1445     int i, stuffing_count, ret;
1446     int context_count = s->slice_context_count;
1447
1448     s->picture_in_gop_number++;
1449
1450     if (load_input_picture(s, pic_arg) < 0)
1451         return -1;
1452
1453     if (select_input_picture(s) < 0) {
1454         return -1;
1455     }
1456
1457     /* output? */
1458     if (s->new_picture.f.data[0]) {
1459         if ((ret = ff_alloc_packet2(avctx, pkt, s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000)) < 0)
1460             return ret;
1461         if (s->mb_info) {
1462             s->mb_info_ptr = av_packet_new_side_data(pkt,
1463                                  AV_PKT_DATA_H263_MB_INFO,
1464                                  s->mb_width*s->mb_height*12);
1465             s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1466         }
1467
1468         for (i = 0; i < context_count; i++) {
1469             int start_y = s->thread_context[i]->start_mb_y;
1470             int   end_y = s->thread_context[i]->  end_mb_y;
1471             int h       = s->mb_height;
1472             uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1473             uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
1474
1475             init_put_bits(&s->thread_context[i]->pb, start, end - start);
1476         }
1477
1478         s->pict_type = s->new_picture.f.pict_type;
1479         //emms_c();
1480         if (ff_MPV_frame_start(s, avctx) < 0)
1481             return -1;
1482 vbv_retry:
1483         if (encode_picture(s, s->picture_number) < 0)
1484             return -1;
1485
1486         avctx->header_bits = s->header_bits;
1487         avctx->mv_bits     = s->mv_bits;
1488         avctx->misc_bits   = s->misc_bits;
1489         avctx->i_tex_bits  = s->i_tex_bits;
1490         avctx->p_tex_bits  = s->p_tex_bits;
1491         avctx->i_count     = s->i_count;
1492         // FIXME f/b_count in avctx
1493         avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1494         avctx->skip_count  = s->skip_count;
1495
1496         ff_MPV_frame_end(s);
1497
1498         if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1499             ff_mjpeg_encode_picture_trailer(s);
1500
1501         if (avctx->rc_buffer_size) {
1502             RateControlContext *rcc = &s->rc_context;
1503             int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1504
1505             if (put_bits_count(&s->pb) > max_size &&
1506                 s->lambda < s->avctx->lmax) {
1507                 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1508                                        (s->qscale + 1) / s->qscale);
1509                 if (s->adaptive_quant) {
1510                     int i;
1511                     for (i = 0; i < s->mb_height * s->mb_stride; i++)
1512                         s->lambda_table[i] =
1513                             FFMAX(s->lambda_table[i] + 1,
1514                                   s->lambda_table[i] * (s->qscale + 1) /
1515                                   s->qscale);
1516                 }
1517                 s->mb_skipped = 0;        // done in MPV_frame_start()
1518                 // done in encode_picture() so we must undo it
1519                 if (s->pict_type == AV_PICTURE_TYPE_P) {
1520                     if (s->flipflop_rounding          ||
1521                         s->codec_id == AV_CODEC_ID_H263P ||
1522                         s->codec_id == AV_CODEC_ID_MPEG4)
1523                         s->no_rounding ^= 1;
1524                 }
1525                 if (s->pict_type != AV_PICTURE_TYPE_B) {
1526                     s->time_base       = s->last_time_base;
1527                     s->last_non_b_time = s->time - s->pp_time;
1528                 }
1529                 for (i = 0; i < context_count; i++) {
1530                     PutBitContext *pb = &s->thread_context[i]->pb;
1531                     init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1532                 }
1533                 goto vbv_retry;
1534             }
1535
1536             assert(s->avctx->rc_max_rate);
1537         }
1538
1539         if (s->flags & CODEC_FLAG_PASS1)
1540             ff_write_pass1_stats(s);
1541
1542         for (i = 0; i < 4; i++) {
1543             s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1544             avctx->error[i] += s->current_picture_ptr->f.error[i];
1545         }
1546
1547         if (s->flags & CODEC_FLAG_PASS1)
1548             assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1549                    avctx->i_tex_bits + avctx->p_tex_bits ==
1550                        put_bits_count(&s->pb));
1551         flush_put_bits(&s->pb);
1552         s->frame_bits  = put_bits_count(&s->pb);
1553
1554         stuffing_count = ff_vbv_update(s, s->frame_bits);
1555         s->stuffing_bits = 8*stuffing_count;
1556         if (stuffing_count) {
1557             if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1558                     stuffing_count + 50) {
1559                 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1560                 return -1;
1561             }
1562
1563             switch (s->codec_id) {
1564             case AV_CODEC_ID_MPEG1VIDEO:
1565             case AV_CODEC_ID_MPEG2VIDEO:
1566                 while (stuffing_count--) {
1567                     put_bits(&s->pb, 8, 0);
1568                 }
1569             break;
1570             case AV_CODEC_ID_MPEG4:
1571                 put_bits(&s->pb, 16, 0);
1572                 put_bits(&s->pb, 16, 0x1C3);
1573                 stuffing_count -= 4;
1574                 while (stuffing_count--) {
1575                     put_bits(&s->pb, 8, 0xFF);
1576                 }
1577             break;
1578             default:
1579                 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1580             }
1581             flush_put_bits(&s->pb);
1582             s->frame_bits  = put_bits_count(&s->pb);
1583         }
1584
1585         /* update mpeg1/2 vbv_delay for CBR */
1586         if (s->avctx->rc_max_rate                          &&
1587             s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1588             s->out_format == FMT_MPEG1                     &&
1589             90000LL * (avctx->rc_buffer_size - 1) <=
1590                 s->avctx->rc_max_rate * 0xFFFFLL) {
1591             int vbv_delay, min_delay;
1592             double inbits  = s->avctx->rc_max_rate *
1593                              av_q2d(s->avctx->time_base);
1594             int    minbits = s->frame_bits - 8 *
1595                              (s->vbv_delay_ptr - s->pb.buf - 1);
1596             double bits    = s->rc_context.buffer_index + minbits - inbits;
1597
1598             if (bits < 0)
1599                 av_log(s->avctx, AV_LOG_ERROR,
1600                        "Internal error, negative bits\n");
1601
1602             assert(s->repeat_first_field == 0);
1603
1604             vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1605             min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1606                         s->avctx->rc_max_rate;
1607
1608             vbv_delay = FFMAX(vbv_delay, min_delay);
1609
1610             av_assert0(vbv_delay < 0xFFFF);
1611
1612             s->vbv_delay_ptr[0] &= 0xF8;
1613             s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1614             s->vbv_delay_ptr[1]  = vbv_delay >> 5;
1615             s->vbv_delay_ptr[2] &= 0x07;
1616             s->vbv_delay_ptr[2] |= vbv_delay << 3;
1617             avctx->vbv_delay     = vbv_delay * 300;
1618         }
1619         s->total_bits     += s->frame_bits;
1620         avctx->frame_bits  = s->frame_bits;
1621
1622         pkt->pts = s->current_picture.f.pts;
1623         if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1624             if (!s->current_picture.f.coded_picture_number)
1625                 pkt->dts = pkt->pts - s->dts_delta;
1626             else
1627                 pkt->dts = s->reordered_pts;
1628             s->reordered_pts = pkt->pts;
1629         } else
1630             pkt->dts = pkt->pts;
1631         if (s->current_picture.f.key_frame)
1632             pkt->flags |= AV_PKT_FLAG_KEY;
1633         if (s->mb_info)
1634             av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1635     } else {
1636         s->frame_bits = 0;
1637     }
1638     assert((s->frame_bits & 7) == 0);
1639
1640     pkt->size = s->frame_bits / 8;
1641     *got_packet = !!pkt->size;
1642     return 0;
1643 }
1644
1645 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1646                                                 int n, int threshold)
1647 {
1648     static const char tab[64] = {
1649         3, 2, 2, 1, 1, 1, 1, 1,
1650         1, 1, 1, 1, 1, 1, 1, 1,
1651         1, 1, 1, 1, 1, 1, 1, 1,
1652         0, 0, 0, 0, 0, 0, 0, 0,
1653         0, 0, 0, 0, 0, 0, 0, 0,
1654         0, 0, 0, 0, 0, 0, 0, 0,
1655         0, 0, 0, 0, 0, 0, 0, 0,
1656         0, 0, 0, 0, 0, 0, 0, 0
1657     };
1658     int score = 0;
1659     int run = 0;
1660     int i;
1661     int16_t *block = s->block[n];
1662     const int last_index = s->block_last_index[n];
1663     int skip_dc;
1664
1665     if (threshold < 0) {
1666         skip_dc = 0;
1667         threshold = -threshold;
1668     } else
1669         skip_dc = 1;
1670
1671     /* Are all we could set to zero already zero? */
1672     if (last_index <= skip_dc - 1)
1673         return;
1674
1675     for (i = 0; i <= last_index; i++) {
1676         const int j = s->intra_scantable.permutated[i];
1677         const int level = FFABS(block[j]);
1678         if (level == 1) {
1679             if (skip_dc && i == 0)
1680                 continue;
1681             score += tab[run];
1682             run = 0;
1683         } else if (level > 1) {
1684             return;
1685         } else {
1686             run++;
1687         }
1688     }
1689     if (score >= threshold)
1690         return;
1691     for (i = skip_dc; i <= last_index; i++) {
1692         const int j = s->intra_scantable.permutated[i];
1693         block[j] = 0;
1694     }
1695     if (block[0])
1696         s->block_last_index[n] = 0;
1697     else
1698         s->block_last_index[n] = -1;
1699 }
1700
1701 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1702                                int last_index)
1703 {
1704     int i;
1705     const int maxlevel = s->max_qcoeff;
1706     const int minlevel = s->min_qcoeff;
1707     int overflow = 0;
1708
1709     if (s->mb_intra) {
1710         i = 1; // skip clipping of intra dc
1711     } else
1712         i = 0;
1713
1714     for (; i <= last_index; i++) {
1715         const int j = s->intra_scantable.permutated[i];
1716         int level = block[j];
1717
1718         if (level > maxlevel) {
1719             level = maxlevel;
1720             overflow++;
1721         } else if (level < minlevel) {
1722             level = minlevel;
1723             overflow++;
1724         }
1725
1726         block[j] = level;
1727     }
1728
1729     if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1730         av_log(s->avctx, AV_LOG_INFO,
1731                "warning, clipping %d dct coefficients to %d..%d\n",
1732                overflow, minlevel, maxlevel);
1733 }
1734
1735 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1736 {
1737     int x, y;
1738     // FIXME optimize
1739     for (y = 0; y < 8; y++) {
1740         for (x = 0; x < 8; x++) {
1741             int x2, y2;
1742             int sum = 0;
1743             int sqr = 0;
1744             int count = 0;
1745
1746             for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1747                 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1748                     int v = ptr[x2 + y2 * stride];
1749                     sum += v;
1750                     sqr += v * v;
1751                     count++;
1752                 }
1753             }
1754             weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1755         }
1756     }
1757 }
1758
1759 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1760                                                 int motion_x, int motion_y,
1761                                                 int mb_block_height,
1762                                                 int mb_block_width,
1763                                                 int mb_block_count)
1764 {
1765     int16_t weight[12][64];
1766     int16_t orig[12][64];
1767     const int mb_x = s->mb_x;
1768     const int mb_y = s->mb_y;
1769     int i;
1770     int skip_dct[12];
1771     int dct_offset = s->linesize * 8; // default for progressive frames
1772     int uv_dct_offset = s->uvlinesize * 8;
1773     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1774     int wrap_y, wrap_c;
1775
1776     for (i = 0; i < mb_block_count; i++)
1777         skip_dct[i] = s->skipdct;
1778
1779     if (s->adaptive_quant) {
1780         const int last_qp = s->qscale;
1781         const int mb_xy = mb_x + mb_y * s->mb_stride;
1782
1783         s->lambda = s->lambda_table[mb_xy];
1784         update_qscale(s);
1785
1786         if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1787             s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
1788             s->dquant = s->qscale - last_qp;
1789
1790             if (s->out_format == FMT_H263) {
1791                 s->dquant = av_clip(s->dquant, -2, 2);
1792
1793                 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1794                     if (!s->mb_intra) {
1795                         if (s->pict_type == AV_PICTURE_TYPE_B) {
1796                             if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1797                                 s->dquant = 0;
1798                         }
1799                         if (s->mv_type == MV_TYPE_8X8)
1800                             s->dquant = 0;
1801                     }
1802                 }
1803             }
1804         }
1805         ff_set_qscale(s, last_qp + s->dquant);
1806     } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1807         ff_set_qscale(s, s->qscale + s->dquant);
1808
1809     wrap_y = s->linesize;
1810     wrap_c = s->uvlinesize;
1811     ptr_y  = s->new_picture.f.data[0] +
1812              (mb_y * 16 * wrap_y)              + mb_x * 16;
1813     ptr_cb = s->new_picture.f.data[1] +
1814              (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1815     ptr_cr = s->new_picture.f.data[2] +
1816              (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1817
1818     if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
1819         uint8_t *ebuf = s->edge_emu_buffer + 32;
1820         int cw = (s->width  + s->chroma_x_shift) >> s->chroma_x_shift;
1821         int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
1822         s->vdsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
1823                                  mb_y * 16, s->width, s->height);
1824         ptr_y = ebuf;
1825         s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, mb_block_width,
1826                                  mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1827                                  cw, ch);
1828         ptr_cb = ebuf + 18 * wrap_y;
1829         s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr, wrap_c, mb_block_width,
1830                                  mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1831                                  cw, ch);
1832         ptr_cr = ebuf + 18 * wrap_y + 16;
1833     }
1834
1835     if (s->mb_intra) {
1836         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1837             int progressive_score, interlaced_score;
1838
1839             s->interlaced_dct = 0;
1840             progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1841                                                     NULL, wrap_y, 8) +
1842                                 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1843                                                     NULL, wrap_y, 8) - 400;
1844
1845             if (progressive_score > 0) {
1846                 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1847                                                        NULL, wrap_y * 2, 8) +
1848                                    s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1849                                                        NULL, wrap_y * 2, 8);
1850                 if (progressive_score > interlaced_score) {
1851                     s->interlaced_dct = 1;
1852
1853                     dct_offset = wrap_y;
1854                     uv_dct_offset = wrap_c;
1855                     wrap_y <<= 1;
1856                     if (s->chroma_format == CHROMA_422 ||
1857                         s->chroma_format == CHROMA_444)
1858                         wrap_c <<= 1;
1859                 }
1860             }
1861         }
1862
1863         s->dsp.get_pixels(s->block[0], ptr_y                  , wrap_y);
1864         s->dsp.get_pixels(s->block[1], ptr_y              + 8 , wrap_y);
1865         s->dsp.get_pixels(s->block[2], ptr_y + dct_offset     , wrap_y);
1866         s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
1867
1868         if (s->flags & CODEC_FLAG_GRAY) {
1869             skip_dct[4] = 1;
1870             skip_dct[5] = 1;
1871         } else {
1872             s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1873             s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1874             if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
1875                 s->dsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
1876                 s->dsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
1877             } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
1878                 s->dsp.get_pixels(s->block[6], ptr_cb + 8, wrap_c);
1879                 s->dsp.get_pixels(s->block[7], ptr_cr + 8, wrap_c);
1880                 s->dsp.get_pixels(s->block[8], ptr_cb + uv_dct_offset, wrap_c);
1881                 s->dsp.get_pixels(s->block[9], ptr_cr + uv_dct_offset, wrap_c);
1882                 s->dsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
1883                 s->dsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
1884             }
1885         }
1886     } else {
1887         op_pixels_func (*op_pix)[4];
1888         qpel_mc_func (*op_qpix)[16];
1889         uint8_t *dest_y, *dest_cb, *dest_cr;
1890
1891         dest_y  = s->dest[0];
1892         dest_cb = s->dest[1];
1893         dest_cr = s->dest[2];
1894
1895         if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1896             op_pix  = s->dsp.put_pixels_tab;
1897             op_qpix = s->dsp.put_qpel_pixels_tab;
1898         } else {
1899             op_pix  = s->dsp.put_no_rnd_pixels_tab;
1900             op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
1901         }
1902
1903         if (s->mv_dir & MV_DIR_FORWARD) {
1904             ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1905                           s->last_picture.f.data,
1906                           op_pix, op_qpix);
1907             op_pix  = s->dsp.avg_pixels_tab;
1908             op_qpix = s->dsp.avg_qpel_pixels_tab;
1909         }
1910         if (s->mv_dir & MV_DIR_BACKWARD) {
1911             ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1912                           s->next_picture.f.data,
1913                           op_pix, op_qpix);
1914         }
1915
1916         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1917             int progressive_score, interlaced_score;
1918
1919             s->interlaced_dct = 0;
1920             progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1921                                                     ptr_y,              wrap_y,
1922                                                     8) +
1923                                 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1924                                                     ptr_y + wrap_y * 8, wrap_y,
1925                                                     8) - 400;
1926
1927             if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1928                 progressive_score -= 400;
1929
1930             if (progressive_score > 0) {
1931                 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1932                                                        ptr_y,
1933                                                        wrap_y * 2, 8) +
1934                                    s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
1935                                                        ptr_y + wrap_y,
1936                                                        wrap_y * 2, 8);
1937
1938                 if (progressive_score > interlaced_score) {
1939                     s->interlaced_dct = 1;
1940
1941                     dct_offset = wrap_y;
1942                     uv_dct_offset = wrap_c;
1943                     wrap_y <<= 1;
1944                     if (s->chroma_format == CHROMA_422)
1945                         wrap_c <<= 1;
1946                 }
1947             }
1948         }
1949
1950         s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
1951         s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1952         s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
1953                            dest_y + dct_offset, wrap_y);
1954         s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
1955                            dest_y + dct_offset + 8, wrap_y);
1956
1957         if (s->flags & CODEC_FLAG_GRAY) {
1958             skip_dct[4] = 1;
1959             skip_dct[5] = 1;
1960         } else {
1961             s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1962             s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1963             if (!s->chroma_y_shift) { /* 422 */
1964                 s->dsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
1965                                    dest_cb + uv_dct_offset, wrap_c);
1966                 s->dsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
1967                                    dest_cr + uv_dct_offset, wrap_c);
1968             }
1969         }
1970         /* pre quantization */
1971         if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
1972                 2 * s->qscale * s->qscale) {
1973             // FIXME optimize
1974             if (s->dsp.sad[1](NULL, ptr_y , dest_y,
1975                               wrap_y, 8) < 20 * s->qscale)
1976                 skip_dct[0] = 1;
1977             if (s->dsp.sad[1](NULL, ptr_y + 8,
1978                               dest_y + 8, wrap_y, 8) < 20 * s->qscale)
1979                 skip_dct[1] = 1;
1980             if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
1981                               dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
1982                 skip_dct[2] = 1;
1983             if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
1984                               dest_y + dct_offset + 8,
1985                               wrap_y, 8) < 20 * s->qscale)
1986                 skip_dct[3] = 1;
1987             if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
1988                               wrap_c, 8) < 20 * s->qscale)
1989                 skip_dct[4] = 1;
1990             if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
1991                               wrap_c, 8) < 20 * s->qscale)
1992                 skip_dct[5] = 1;
1993             if (!s->chroma_y_shift) { /* 422 */
1994                 if (s->dsp.sad[1](NULL, ptr_cb + uv_dct_offset,
1995                                   dest_cb + uv_dct_offset,
1996                                   wrap_c, 8) < 20 * s->qscale)
1997                     skip_dct[6] = 1;
1998                 if (s->dsp.sad[1](NULL, ptr_cr + uv_dct_offset,
1999                                   dest_cr + uv_dct_offset,
2000                                   wrap_c, 8) < 20 * s->qscale)
2001                     skip_dct[7] = 1;
2002             }
2003         }
2004     }
2005
2006     if (s->quantizer_noise_shaping) {
2007         if (!skip_dct[0])
2008             get_visual_weight(weight[0], ptr_y                 , wrap_y);
2009         if (!skip_dct[1])
2010             get_visual_weight(weight[1], ptr_y              + 8, wrap_y);
2011         if (!skip_dct[2])
2012             get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
2013         if (!skip_dct[3])
2014             get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2015         if (!skip_dct[4])
2016             get_visual_weight(weight[4], ptr_cb                , wrap_c);
2017         if (!skip_dct[5])
2018             get_visual_weight(weight[5], ptr_cr                , wrap_c);
2019         if (!s->chroma_y_shift) { /* 422 */
2020             if (!skip_dct[6])
2021                 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2022                                   wrap_c);
2023             if (!skip_dct[7])
2024                 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2025                                   wrap_c);
2026         }
2027         memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2028     }
2029
2030     /* DCT & quantize */
2031     av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2032     {
2033         for (i = 0; i < mb_block_count; i++) {
2034             if (!skip_dct[i]) {
2035                 int overflow;
2036                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2037                 // FIXME we could decide to change to quantizer instead of
2038                 // clipping
2039                 // JS: I don't think that would be a good idea it could lower
2040                 //     quality instead of improve it. Just INTRADC clipping
2041                 //     deserves changes in quantizer
2042                 if (overflow)
2043                     clip_coeffs(s, s->block[i], s->block_last_index[i]);
2044             } else
2045                 s->block_last_index[i] = -1;
2046         }
2047         if (s->quantizer_noise_shaping) {
2048             for (i = 0; i < mb_block_count; i++) {
2049                 if (!skip_dct[i]) {
2050                     s->block_last_index[i] =
2051                         dct_quantize_refine(s, s->block[i], weight[i],
2052                                             orig[i], i, s->qscale);
2053                 }
2054             }
2055         }
2056
2057         if (s->luma_elim_threshold && !s->mb_intra)
2058             for (i = 0; i < 4; i++)
2059                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2060         if (s->chroma_elim_threshold && !s->mb_intra)
2061             for (i = 4; i < mb_block_count; i++)
2062                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2063
2064         if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2065             for (i = 0; i < mb_block_count; i++) {
2066                 if (s->block_last_index[i] == -1)
2067                     s->coded_score[i] = INT_MAX / 256;
2068             }
2069         }
2070     }
2071
2072     if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2073         s->block_last_index[4] =
2074         s->block_last_index[5] = 0;
2075         s->block[4][0] =
2076         s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2077         if (!s->chroma_y_shift) { /* 422 / 444 */
2078             for (i=6; i<12; i++) {
2079                 s->block_last_index[i] = 0;
2080                 s->block[i][0] = s->block[4][0];
2081             }
2082         }
2083     }
2084
2085     // non c quantize code returns incorrect block_last_index FIXME
2086     if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2087         for (i = 0; i < mb_block_count; i++) {
2088             int j;
2089             if (s->block_last_index[i] > 0) {
2090                 for (j = 63; j > 0; j--) {
2091                     if (s->block[i][s->intra_scantable.permutated[j]])
2092                         break;
2093                 }
2094                 s->block_last_index[i] = j;
2095             }
2096         }
2097     }
2098
2099     /* huffman encode */
2100     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2101     case AV_CODEC_ID_MPEG1VIDEO:
2102     case AV_CODEC_ID_MPEG2VIDEO:
2103         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2104             ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2105         break;
2106     case AV_CODEC_ID_MPEG4:
2107         if (CONFIG_MPEG4_ENCODER)
2108             ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2109         break;
2110     case AV_CODEC_ID_MSMPEG4V2:
2111     case AV_CODEC_ID_MSMPEG4V3:
2112     case AV_CODEC_ID_WMV1:
2113         if (CONFIG_MSMPEG4_ENCODER)
2114             ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2115         break;
2116     case AV_CODEC_ID_WMV2:
2117         if (CONFIG_WMV2_ENCODER)
2118             ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2119         break;
2120     case AV_CODEC_ID_H261:
2121         if (CONFIG_H261_ENCODER)
2122             ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2123         break;
2124     case AV_CODEC_ID_H263:
2125     case AV_CODEC_ID_H263P:
2126     case AV_CODEC_ID_FLV1:
2127     case AV_CODEC_ID_RV10:
2128     case AV_CODEC_ID_RV20:
2129         if (CONFIG_H263_ENCODER)
2130             ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2131         break;
2132     case AV_CODEC_ID_MJPEG:
2133     case AV_CODEC_ID_AMV:
2134         if (CONFIG_MJPEG_ENCODER)
2135             ff_mjpeg_encode_mb(s, s->block);
2136         break;
2137     default:
2138         av_assert1(0);
2139     }
2140 }
2141
2142 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2143 {
2144     if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 8, 6);
2145     else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
2146     else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
2147 }
2148
2149 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2150     int i;
2151
2152     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2153
2154     /* mpeg1 */
2155     d->mb_skip_run= s->mb_skip_run;
2156     for(i=0; i<3; i++)
2157         d->last_dc[i] = s->last_dc[i];
2158
2159     /* statistics */
2160     d->mv_bits= s->mv_bits;
2161     d->i_tex_bits= s->i_tex_bits;
2162     d->p_tex_bits= s->p_tex_bits;
2163     d->i_count= s->i_count;
2164     d->f_count= s->f_count;
2165     d->b_count= s->b_count;
2166     d->skip_count= s->skip_count;
2167     d->misc_bits= s->misc_bits;
2168     d->last_bits= 0;
2169
2170     d->mb_skipped= 0;
2171     d->qscale= s->qscale;
2172     d->dquant= s->dquant;
2173
2174     d->esc3_level_length= s->esc3_level_length;
2175 }
2176
2177 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2178     int i;
2179
2180     memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2181     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2182
2183     /* mpeg1 */
2184     d->mb_skip_run= s->mb_skip_run;
2185     for(i=0; i<3; i++)
2186         d->last_dc[i] = s->last_dc[i];
2187
2188     /* statistics */
2189     d->mv_bits= s->mv_bits;
2190     d->i_tex_bits= s->i_tex_bits;
2191     d->p_tex_bits= s->p_tex_bits;
2192     d->i_count= s->i_count;
2193     d->f_count= s->f_count;
2194     d->b_count= s->b_count;
2195     d->skip_count= s->skip_count;
2196     d->misc_bits= s->misc_bits;
2197
2198     d->mb_intra= s->mb_intra;
2199     d->mb_skipped= s->mb_skipped;
2200     d->mv_type= s->mv_type;
2201     d->mv_dir= s->mv_dir;
2202     d->pb= s->pb;
2203     if(s->data_partitioning){
2204         d->pb2= s->pb2;
2205         d->tex_pb= s->tex_pb;
2206     }
2207     d->block= s->block;
2208     for(i=0; i<8; i++)
2209         d->block_last_index[i]= s->block_last_index[i];
2210     d->interlaced_dct= s->interlaced_dct;
2211     d->qscale= s->qscale;
2212
2213     d->esc3_level_length= s->esc3_level_length;
2214 }
2215
2216 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2217                            PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2218                            int *dmin, int *next_block, int motion_x, int motion_y)
2219 {
2220     int score;
2221     uint8_t *dest_backup[3];
2222
2223     copy_context_before_encode(s, backup, type);
2224
2225     s->block= s->blocks[*next_block];
2226     s->pb= pb[*next_block];
2227     if(s->data_partitioning){
2228         s->pb2   = pb2   [*next_block];
2229         s->tex_pb= tex_pb[*next_block];
2230     }
2231
2232     if(*next_block){
2233         memcpy(dest_backup, s->dest, sizeof(s->dest));
2234         s->dest[0] = s->rd_scratchpad;
2235         s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2236         s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2237         assert(s->linesize >= 32); //FIXME
2238     }
2239
2240     encode_mb(s, motion_x, motion_y);
2241
2242     score= put_bits_count(&s->pb);
2243     if(s->data_partitioning){
2244         score+= put_bits_count(&s->pb2);
2245         score+= put_bits_count(&s->tex_pb);
2246     }
2247
2248     if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2249         ff_MPV_decode_mb(s, s->block);
2250
2251         score *= s->lambda2;
2252         score += sse_mb(s) << FF_LAMBDA_SHIFT;
2253     }
2254
2255     if(*next_block){
2256         memcpy(s->dest, dest_backup, sizeof(s->dest));
2257     }
2258
2259     if(score<*dmin){
2260         *dmin= score;
2261         *next_block^=1;
2262
2263         copy_context_after_encode(best, s, type);
2264     }
2265 }
2266
2267 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2268     uint32_t *sq = ff_squareTbl + 256;
2269     int acc=0;
2270     int x,y;
2271
2272     if(w==16 && h==16)
2273         return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2274     else if(w==8 && h==8)
2275         return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2276
2277     for(y=0; y<h; y++){
2278         for(x=0; x<w; x++){
2279             acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2280         }
2281     }
2282
2283     av_assert2(acc>=0);
2284
2285     return acc;
2286 }
2287
2288 static int sse_mb(MpegEncContext *s){
2289     int w= 16;
2290     int h= 16;
2291
2292     if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2293     if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2294
2295     if(w==16 && h==16)
2296       if(s->avctx->mb_cmp == FF_CMP_NSSE){
2297         return  s->dsp.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)
2298                +s->dsp.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)
2299                +s->dsp.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);
2300       }else{
2301         return  s->dsp.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)
2302                +s->dsp.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)
2303                +s->dsp.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);
2304       }
2305     else
2306         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)
2307                +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)
2308                +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);
2309 }
2310
2311 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2312     MpegEncContext *s= *(void**)arg;
2313
2314
2315     s->me.pre_pass=1;
2316     s->me.dia_size= s->avctx->pre_dia_size;
2317     s->first_slice_line=1;
2318     for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2319         for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2320             ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2321         }
2322         s->first_slice_line=0;
2323     }
2324
2325     s->me.pre_pass=0;
2326
2327     return 0;
2328 }
2329
2330 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2331     MpegEncContext *s= *(void**)arg;
2332
2333     ff_check_alignment();
2334
2335     s->me.dia_size= s->avctx->dia_size;
2336     s->first_slice_line=1;
2337     for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2338         s->mb_x=0; //for block init below
2339         ff_init_block_index(s);
2340         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2341             s->block_index[0]+=2;
2342             s->block_index[1]+=2;
2343             s->block_index[2]+=2;
2344             s->block_index[3]+=2;
2345
2346             /* compute motion vector & mb_type and store in context */
2347             if(s->pict_type==AV_PICTURE_TYPE_B)
2348                 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2349             else
2350                 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2351         }
2352         s->first_slice_line=0;
2353     }
2354     return 0;
2355 }
2356
2357 static int mb_var_thread(AVCodecContext *c, void *arg){
2358     MpegEncContext *s= *(void**)arg;
2359     int mb_x, mb_y;
2360
2361     ff_check_alignment();
2362
2363     for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2364         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2365             int xx = mb_x * 16;
2366             int yy = mb_y * 16;
2367             uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
2368             int varc;
2369             int sum = s->dsp.pix_sum(pix, s->linesize);
2370
2371             varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2372
2373             s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2374             s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2375             s->me.mb_var_sum_temp    += varc;
2376         }
2377     }
2378     return 0;
2379 }
2380
2381 static void write_slice_end(MpegEncContext *s){
2382     if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2383         if(s->partitioned_frame){
2384             ff_mpeg4_merge_partitions(s);
2385         }
2386
2387         ff_mpeg4_stuffing(&s->pb);
2388     }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2389         ff_mjpeg_encode_stuffing(s);
2390     }
2391
2392     avpriv_align_put_bits(&s->pb);
2393     flush_put_bits(&s->pb);
2394
2395     if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2396         s->misc_bits+= get_bits_diff(s);
2397 }
2398
2399 static void write_mb_info(MpegEncContext *s)
2400 {
2401     uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2402     int offset = put_bits_count(&s->pb);
2403     int mba  = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2404     int gobn = s->mb_y / s->gob_index;
2405     int pred_x, pred_y;
2406     if (CONFIG_H263_ENCODER)
2407         ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2408     bytestream_put_le32(&ptr, offset);
2409     bytestream_put_byte(&ptr, s->qscale);
2410     bytestream_put_byte(&ptr, gobn);
2411     bytestream_put_le16(&ptr, mba);
2412     bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2413     bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2414     /* 4MV not implemented */
2415     bytestream_put_byte(&ptr, 0); /* hmv2 */
2416     bytestream_put_byte(&ptr, 0); /* vmv2 */
2417 }
2418
2419 static void update_mb_info(MpegEncContext *s, int startcode)
2420 {
2421     if (!s->mb_info)
2422         return;
2423     if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2424         s->mb_info_size += 12;
2425         s->prev_mb_info = s->last_mb_info;
2426     }
2427     if (startcode) {
2428         s->prev_mb_info = put_bits_count(&s->pb)/8;
2429         /* This might have incremented mb_info_size above, and we return without
2430          * actually writing any info into that slot yet. But in that case,
2431          * this will be called again at the start of the after writing the
2432          * start code, actually writing the mb info. */
2433         return;
2434     }
2435
2436     s->last_mb_info = put_bits_count(&s->pb)/8;
2437     if (!s->mb_info_size)
2438         s->mb_info_size += 12;
2439     write_mb_info(s);
2440 }
2441
2442 static int encode_thread(AVCodecContext *c, void *arg){
2443     MpegEncContext *s= *(void**)arg;
2444     int mb_x, mb_y, pdif = 0;
2445     int chr_h= 16>>s->chroma_y_shift;
2446     int i, j;
2447     MpegEncContext best_s, backup_s;
2448     uint8_t bit_buf[2][MAX_MB_BYTES];
2449     uint8_t bit_buf2[2][MAX_MB_BYTES];
2450     uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2451     PutBitContext pb[2], pb2[2], tex_pb[2];
2452
2453     ff_check_alignment();
2454
2455     for(i=0; i<2; i++){
2456         init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2457         init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2458         init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2459     }
2460
2461     s->last_bits= put_bits_count(&s->pb);
2462     s->mv_bits=0;
2463     s->misc_bits=0;
2464     s->i_tex_bits=0;
2465     s->p_tex_bits=0;
2466     s->i_count=0;
2467     s->f_count=0;
2468     s->b_count=0;
2469     s->skip_count=0;
2470
2471     for(i=0; i<3; i++){
2472         /* init last dc values */
2473         /* note: quant matrix value (8) is implied here */
2474         s->last_dc[i] = 128 << s->intra_dc_precision;
2475
2476         s->current_picture.f.error[i] = 0;
2477     }
2478     if(s->codec_id==AV_CODEC_ID_AMV){
2479         s->last_dc[0] = 128*8/13;
2480         s->last_dc[1] = 128*8/14;
2481         s->last_dc[2] = 128*8/14;
2482     }
2483     s->mb_skip_run = 0;
2484     memset(s->last_mv, 0, sizeof(s->last_mv));
2485
2486     s->last_mv_dir = 0;
2487
2488     switch(s->codec_id){
2489     case AV_CODEC_ID_H263:
2490     case AV_CODEC_ID_H263P:
2491     case AV_CODEC_ID_FLV1:
2492         if (CONFIG_H263_ENCODER)
2493             s->gob_index = ff_h263_get_gob_height(s);
2494         break;
2495     case AV_CODEC_ID_MPEG4:
2496         if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2497             ff_mpeg4_init_partitions(s);
2498         break;
2499     }
2500
2501     s->resync_mb_x=0;
2502     s->resync_mb_y=0;
2503     s->first_slice_line = 1;
2504     s->ptr_lastgob = s->pb.buf;
2505     for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2506         s->mb_x=0;
2507         s->mb_y= mb_y;
2508
2509         ff_set_qscale(s, s->qscale);
2510         ff_init_block_index(s);
2511
2512         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2513             int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2514             int mb_type= s->mb_type[xy];
2515 //            int d;
2516             int dmin= INT_MAX;
2517             int dir;
2518
2519             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2520                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2521                 return -1;
2522             }
2523             if(s->data_partitioning){
2524                 if(   s->pb2   .buf_end - s->pb2   .buf - (put_bits_count(&s->    pb2)>>3) < MAX_MB_BYTES
2525                    || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2526                     av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2527                     return -1;
2528                 }
2529             }
2530
2531             s->mb_x = mb_x;
2532             s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2533             ff_update_block_index(s);
2534
2535             if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2536                 ff_h261_reorder_mb_index(s);
2537                 xy= s->mb_y*s->mb_stride + s->mb_x;
2538                 mb_type= s->mb_type[xy];
2539             }
2540
2541             /* write gob / video packet header  */
2542             if(s->rtp_mode){
2543                 int current_packet_size, is_gob_start;
2544
2545                 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2546
2547                 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2548
2549                 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2550
2551                 switch(s->codec_id){
2552                 case AV_CODEC_ID_H263:
2553                 case AV_CODEC_ID_H263P:
2554                     if(!s->h263_slice_structured)
2555                         if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2556                     break;
2557                 case AV_CODEC_ID_MPEG2VIDEO:
2558                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2559                 case AV_CODEC_ID_MPEG1VIDEO:
2560                     if(s->mb_skip_run) is_gob_start=0;
2561                     break;
2562                 case AV_CODEC_ID_MJPEG:
2563                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2564                     break;
2565                 }
2566
2567                 if(is_gob_start){
2568                     if(s->start_mb_y != mb_y || mb_x!=0){
2569                         write_slice_end(s);
2570                         if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2571                             ff_mpeg4_init_partitions(s);
2572                         }
2573                     }
2574
2575                     av_assert2((put_bits_count(&s->pb)&7) == 0);
2576                     current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2577
2578                     if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2579                         int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2580                         int d= 100 / s->avctx->error_rate;
2581                         if(r % d == 0){
2582                             current_packet_size=0;
2583                             s->pb.buf_ptr= s->ptr_lastgob;
2584                             assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2585                         }
2586                     }
2587
2588                     if (s->avctx->rtp_callback){
2589                         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2590                         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2591                     }
2592                     update_mb_info(s, 1);
2593
2594                     switch(s->codec_id){
2595                     case AV_CODEC_ID_MPEG4:
2596                         if (CONFIG_MPEG4_ENCODER) {
2597                             ff_mpeg4_encode_video_packet_header(s);
2598                             ff_mpeg4_clean_buffers(s);
2599                         }
2600                     break;
2601                     case AV_CODEC_ID_MPEG1VIDEO:
2602                     case AV_CODEC_ID_MPEG2VIDEO:
2603                         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2604                             ff_mpeg1_encode_slice_header(s);
2605                             ff_mpeg1_clean_buffers(s);
2606                         }
2607                     break;
2608                     case AV_CODEC_ID_H263:
2609                     case AV_CODEC_ID_H263P:
2610                         if (CONFIG_H263_ENCODER)
2611                             ff_h263_encode_gob_header(s, mb_y);
2612                     break;
2613                     }
2614
2615                     if(s->flags&CODEC_FLAG_PASS1){
2616                         int bits= put_bits_count(&s->pb);
2617                         s->misc_bits+= bits - s->last_bits;
2618                         s->last_bits= bits;
2619                     }
2620
2621                     s->ptr_lastgob += current_packet_size;
2622                     s->first_slice_line=1;
2623                     s->resync_mb_x=mb_x;
2624                     s->resync_mb_y=mb_y;
2625                 }
2626             }
2627
2628             if(  (s->resync_mb_x   == s->mb_x)
2629                && s->resync_mb_y+1 == s->mb_y){
2630                 s->first_slice_line=0;
2631             }
2632
2633             s->mb_skipped=0;
2634             s->dquant=0; //only for QP_RD
2635
2636             update_mb_info(s, 0);
2637
2638             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
2639                 int next_block=0;
2640                 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2641
2642                 copy_context_before_encode(&backup_s, s, -1);
2643                 backup_s.pb= s->pb;
2644                 best_s.data_partitioning= s->data_partitioning;
2645                 best_s.partitioned_frame= s->partitioned_frame;
2646                 if(s->data_partitioning){
2647                     backup_s.pb2= s->pb2;
2648                     backup_s.tex_pb= s->tex_pb;
2649                 }
2650
2651                 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2652                     s->mv_dir = MV_DIR_FORWARD;
2653                     s->mv_type = MV_TYPE_16X16;
2654                     s->mb_intra= 0;
2655                     s->mv[0][0][0] = s->p_mv_table[xy][0];
2656                     s->mv[0][0][1] = s->p_mv_table[xy][1];
2657                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2658                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2659                 }
2660                 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2661                     s->mv_dir = MV_DIR_FORWARD;
2662                     s->mv_type = MV_TYPE_FIELD;
2663                     s->mb_intra= 0;
2664                     for(i=0; i<2; i++){
2665                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2666                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2667                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2668                     }
2669                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2670                                  &dmin, &next_block, 0, 0);
2671                 }
2672                 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2673                     s->mv_dir = MV_DIR_FORWARD;
2674                     s->mv_type = MV_TYPE_16X16;
2675                     s->mb_intra= 0;
2676                     s->mv[0][0][0] = 0;
2677                     s->mv[0][0][1] = 0;
2678                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2679                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2680                 }
2681                 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2682                     s->mv_dir = MV_DIR_FORWARD;
2683                     s->mv_type = MV_TYPE_8X8;
2684                     s->mb_intra= 0;
2685                     for(i=0; i<4; i++){
2686                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2687                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2688                     }
2689                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2690                                  &dmin, &next_block, 0, 0);
2691                 }
2692                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2693                     s->mv_dir = MV_DIR_FORWARD;
2694                     s->mv_type = MV_TYPE_16X16;
2695                     s->mb_intra= 0;
2696                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2697                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2698                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2699                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2700                 }
2701                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2702                     s->mv_dir = MV_DIR_BACKWARD;
2703                     s->mv_type = MV_TYPE_16X16;
2704                     s->mb_intra= 0;
2705                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2706                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2707                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2708                                  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2709                 }
2710                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2711                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2712                     s->mv_type = MV_TYPE_16X16;
2713                     s->mb_intra= 0;
2714                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2715                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2716                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2717                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2718                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2719                                  &dmin, &next_block, 0, 0);
2720                 }
2721                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2722                     s->mv_dir = MV_DIR_FORWARD;
2723                     s->mv_type = MV_TYPE_FIELD;
2724                     s->mb_intra= 0;
2725                     for(i=0; i<2; i++){
2726                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2727                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2728                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2729                     }
2730                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2731                                  &dmin, &next_block, 0, 0);
2732                 }
2733                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2734                     s->mv_dir = MV_DIR_BACKWARD;
2735                     s->mv_type = MV_TYPE_FIELD;
2736                     s->mb_intra= 0;
2737                     for(i=0; i<2; i++){
2738                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2739                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2740                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2741                     }
2742                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2743                                  &dmin, &next_block, 0, 0);
2744                 }
2745                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2746                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2747                     s->mv_type = MV_TYPE_FIELD;
2748                     s->mb_intra= 0;
2749                     for(dir=0; dir<2; dir++){
2750                         for(i=0; i<2; i++){
2751                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2752                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2753                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2754                         }
2755                     }
2756                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2757                                  &dmin, &next_block, 0, 0);
2758                 }
2759                 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2760                     s->mv_dir = 0;
2761                     s->mv_type = MV_TYPE_16X16;
2762                     s->mb_intra= 1;
2763                     s->mv[0][0][0] = 0;
2764                     s->mv[0][0][1] = 0;
2765                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2766                                  &dmin, &next_block, 0, 0);
2767                     if(s->h263_pred || s->h263_aic){
2768                         if(best_s.mb_intra)
2769                             s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2770                         else
2771                             ff_clean_intra_table_entries(s); //old mode?
2772                     }
2773                 }
2774
2775                 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2776                     if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2777                         const int last_qp= backup_s.qscale;
2778                         int qpi, qp, dc[6];
2779                         int16_t ac[6][16];
2780                         const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2781                         static const int dquant_tab[4]={-1,1,-2,2};
2782                         int storecoefs = s->mb_intra && s->dc_val[0];
2783
2784                         av_assert2(backup_s.dquant == 0);
2785
2786                         //FIXME intra
2787                         s->mv_dir= best_s.mv_dir;
2788                         s->mv_type = MV_TYPE_16X16;
2789                         s->mb_intra= best_s.mb_intra;
2790                         s->mv[0][0][0] = best_s.mv[0][0][0];
2791                         s->mv[0][0][1] = best_s.mv[0][0][1];
2792                         s->mv[1][0][0] = best_s.mv[1][0][0];
2793                         s->mv[1][0][1] = best_s.mv[1][0][1];
2794
2795                         qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2796                         for(; qpi<4; qpi++){
2797                             int dquant= dquant_tab[qpi];
2798                             qp= last_qp + dquant;
2799                             if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2800                                 continue;
2801                             backup_s.dquant= dquant;
2802                             if(storecoefs){
2803                                 for(i=0; i<6; i++){
2804                                     dc[i]= s->dc_val[0][ s->block_index[i] ];
2805                                     memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2806                                 }
2807                             }
2808
2809                             encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2810                                          &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2811                             if(best_s.qscale != qp){
2812                                 if(storecoefs){
2813                                     for(i=0; i<6; i++){
2814                                         s->dc_val[0][ s->block_index[i] ]= dc[i];
2815                                         memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2816                                     }
2817                                 }
2818                             }
2819                         }
2820                     }
2821                 }
2822                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2823                     int mx= s->b_direct_mv_table[xy][0];
2824                     int my= s->b_direct_mv_table[xy][1];
2825
2826                     backup_s.dquant = 0;
2827                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2828                     s->mb_intra= 0;
2829                     ff_mpeg4_set_direct_mv(s, mx, my);
2830                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2831                                  &dmin, &next_block, mx, my);
2832                 }
2833                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2834                     backup_s.dquant = 0;
2835                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2836                     s->mb_intra= 0;
2837                     ff_mpeg4_set_direct_mv(s, 0, 0);
2838                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2839                                  &dmin, &next_block, 0, 0);
2840                 }
2841                 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2842                     int coded=0;
2843                     for(i=0; i<6; i++)
2844                         coded |= s->block_last_index[i];
2845                     if(coded){
2846                         int mx,my;
2847                         memcpy(s->mv, best_s.mv, sizeof(s->mv));
2848                         if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2849                             mx=my=0; //FIXME find the one we actually used
2850                             ff_mpeg4_set_direct_mv(s, mx, my);
2851                         }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2852                             mx= s->mv[1][0][0];
2853                             my= s->mv[1][0][1];
2854                         }else{
2855                             mx= s->mv[0][0][0];
2856                             my= s->mv[0][0][1];
2857                         }
2858
2859                         s->mv_dir= best_s.mv_dir;
2860                         s->mv_type = best_s.mv_type;
2861                         s->mb_intra= 0;
2862 /*                        s->mv[0][0][0] = best_s.mv[0][0][0];
2863                         s->mv[0][0][1] = best_s.mv[0][0][1];
2864                         s->mv[1][0][0] = best_s.mv[1][0][0];
2865                         s->mv[1][0][1] = best_s.mv[1][0][1];*/
2866                         backup_s.dquant= 0;
2867                         s->skipdct=1;
2868                         encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2869                                         &dmin, &next_block, mx, my);
2870                         s->skipdct=0;
2871                     }
2872                 }
2873
2874                 s->current_picture.qscale_table[xy] = best_s.qscale;
2875
2876                 copy_context_after_encode(s, &best_s, -1);
2877
2878                 pb_bits_count= put_bits_count(&s->pb);
2879                 flush_put_bits(&s->pb);
2880                 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2881                 s->pb= backup_s.pb;
2882
2883                 if(s->data_partitioning){
2884                     pb2_bits_count= put_bits_count(&s->pb2);
2885                     flush_put_bits(&s->pb2);
2886                     avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2887                     s->pb2= backup_s.pb2;
2888
2889                     tex_pb_bits_count= put_bits_count(&s->tex_pb);
2890                     flush_put_bits(&s->tex_pb);
2891                     avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2892                     s->tex_pb= backup_s.tex_pb;
2893                 }
2894                 s->last_bits= put_bits_count(&s->pb);
2895
2896                 if (CONFIG_H263_ENCODER &&
2897                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2898                     ff_h263_update_motion_val(s);
2899
2900                 if(next_block==0){ //FIXME 16 vs linesize16
2901                     s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
2902                     s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
2903                     s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2904                 }
2905
2906                 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2907                     ff_MPV_decode_mb(s, s->block);
2908             } else {
2909                 int motion_x = 0, motion_y = 0;
2910                 s->mv_type=MV_TYPE_16X16;
2911                 // only one MB-Type possible
2912
2913                 switch(mb_type){
2914                 case CANDIDATE_MB_TYPE_INTRA:
2915                     s->mv_dir = 0;
2916                     s->mb_intra= 1;
2917                     motion_x= s->mv[0][0][0] = 0;
2918                     motion_y= s->mv[0][0][1] = 0;
2919                     break;
2920                 case CANDIDATE_MB_TYPE_INTER:
2921                     s->mv_dir = MV_DIR_FORWARD;
2922                     s->mb_intra= 0;
2923                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2924                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2925                     break;
2926                 case CANDIDATE_MB_TYPE_INTER_I:
2927                     s->mv_dir = MV_DIR_FORWARD;
2928                     s->mv_type = MV_TYPE_FIELD;
2929                     s->mb_intra= 0;
2930                     for(i=0; i<2; i++){
2931                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2932                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2933                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2934                     }
2935                     break;
2936                 case CANDIDATE_MB_TYPE_INTER4V:
2937                     s->mv_dir = MV_DIR_FORWARD;
2938                     s->mv_type = MV_TYPE_8X8;
2939                     s->mb_intra= 0;
2940                     for(i=0; i<4; i++){
2941                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2942                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2943                     }
2944                     break;
2945                 case CANDIDATE_MB_TYPE_DIRECT:
2946                     if (CONFIG_MPEG4_ENCODER) {
2947                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2948                         s->mb_intra= 0;
2949                         motion_x=s->b_direct_mv_table[xy][0];
2950                         motion_y=s->b_direct_mv_table[xy][1];
2951                         ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2952                     }
2953                     break;
2954                 case CANDIDATE_MB_TYPE_DIRECT0:
2955                     if (CONFIG_MPEG4_ENCODER) {
2956                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2957                         s->mb_intra= 0;
2958                         ff_mpeg4_set_direct_mv(s, 0, 0);
2959                     }
2960                     break;
2961                 case CANDIDATE_MB_TYPE_BIDIR:
2962                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2963                     s->mb_intra= 0;
2964                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2965                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2966                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2967                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2968                     break;
2969                 case CANDIDATE_MB_TYPE_BACKWARD:
2970                     s->mv_dir = MV_DIR_BACKWARD;
2971                     s->mb_intra= 0;
2972                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2973                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2974                     break;
2975                 case CANDIDATE_MB_TYPE_FORWARD:
2976                     s->mv_dir = MV_DIR_FORWARD;
2977                     s->mb_intra= 0;
2978                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2979                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2980                     break;
2981                 case CANDIDATE_MB_TYPE_FORWARD_I:
2982                     s->mv_dir = MV_DIR_FORWARD;
2983                     s->mv_type = MV_TYPE_FIELD;
2984                     s->mb_intra= 0;
2985                     for(i=0; i<2; i++){
2986                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2987                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2988                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2989                     }
2990                     break;
2991                 case CANDIDATE_MB_TYPE_BACKWARD_I:
2992                     s->mv_dir = MV_DIR_BACKWARD;
2993                     s->mv_type = MV_TYPE_FIELD;
2994                     s->mb_intra= 0;
2995                     for(i=0; i<2; i++){
2996                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2997                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2998                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2999                     }
3000                     break;
3001                 case CANDIDATE_MB_TYPE_BIDIR_I:
3002                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3003                     s->mv_type = MV_TYPE_FIELD;
3004                     s->mb_intra= 0;
3005                     for(dir=0; dir<2; dir++){
3006                         for(i=0; i<2; i++){
3007                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3008                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3009                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3010                         }
3011                     }
3012                     break;
3013                 default:
3014                     av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3015                 }
3016
3017                 encode_mb(s, motion_x, motion_y);
3018
3019                 // RAL: Update last macroblock type
3020                 s->last_mv_dir = s->mv_dir;
3021
3022                 if (CONFIG_H263_ENCODER &&
3023                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3024                     ff_h263_update_motion_val(s);
3025
3026                 ff_MPV_decode_mb(s, s->block);
3027             }
3028
3029             /* clean the MV table in IPS frames for direct mode in B frames */
3030             if(s->mb_intra /* && I,P,S_TYPE */){
3031                 s->p_mv_table[xy][0]=0;
3032                 s->p_mv_table[xy][1]=0;
3033             }
3034
3035             if(s->flags&CODEC_FLAG_PSNR){
3036                 int w= 16;
3037                 int h= 16;
3038
3039                 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3040                 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3041
3042                 s->current_picture.f.error[0] += sse(
3043                     s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3044                     s->dest[0], w, h, s->linesize);
3045                 s->current_picture.f.error[1] += sse(
3046                     s, s->new_picture.f.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3047                     s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3048                 s->current_picture.f.error[2] += sse(
3049                     s, s->new_picture.f.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3050                     s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3051             }
3052             if(s->loop_filter){
3053                 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3054                     ff_h263_loop_filter(s);
3055             }
3056             av_dlog(s->avctx, "MB %d %d bits\n",
3057                     s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3058         }
3059     }
3060
3061     //not beautiful here but we must write it before flushing so it has to be here
3062     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3063         ff_msmpeg4_encode_ext_header(s);
3064
3065     write_slice_end(s);
3066
3067     /* Send the last GOB if RTP */
3068     if (s->avctx->rtp_callback) {
3069         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3070         pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3071         /* Call the RTP callback to send the last GOB */
3072         emms_c();
3073         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3074     }
3075
3076     return 0;
3077 }
3078
3079 #define MERGE(field) dst->field += src->field; src->field=0
3080 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3081     MERGE(me.scene_change_score);
3082     MERGE(me.mc_mb_var_sum_temp);
3083     MERGE(me.mb_var_sum_temp);
3084 }
3085
3086 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3087     int i;
3088
3089     MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3090     MERGE(dct_count[1]);
3091     MERGE(mv_bits);
3092     MERGE(i_tex_bits);
3093     MERGE(p_tex_bits);
3094     MERGE(i_count);
3095     MERGE(f_count);
3096     MERGE(b_count);
3097     MERGE(skip_count);
3098     MERGE(misc_bits);
3099     MERGE(er.error_count);
3100     MERGE(padding_bug_score);
3101     MERGE(current_picture.f.error[0]);
3102     MERGE(current_picture.f.error[1]);
3103     MERGE(current_picture.f.error[2]);
3104
3105     if(dst->avctx->noise_reduction){
3106         for(i=0; i<64; i++){
3107             MERGE(dct_error_sum[0][i]);
3108             MERGE(dct_error_sum[1][i]);
3109         }
3110     }
3111
3112     assert(put_bits_count(&src->pb) % 8 ==0);
3113     assert(put_bits_count(&dst->pb) % 8 ==0);
3114     avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3115     flush_put_bits(&dst->pb);
3116 }
3117
3118 static int estimate_qp(MpegEncContext *s, int dry_run){
3119     if (s->next_lambda){
3120         s->current_picture_ptr->f.quality =
3121         s->current_picture.f.quality = s->next_lambda;
3122         if(!dry_run) s->next_lambda= 0;
3123     } else if (!s->fixed_qscale) {
3124         s->current_picture_ptr->f.quality =
3125         s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
3126         if (s->current_picture.f.quality < 0)
3127             return -1;
3128     }
3129
3130     if(s->adaptive_quant){
3131         switch(s->codec_id){
3132         case AV_CODEC_ID_MPEG4:
3133             if (CONFIG_MPEG4_ENCODER)
3134                 ff_clean_mpeg4_qscales(s);
3135             break;
3136         case AV_CODEC_ID_H263:
3137         case AV_CODEC_ID_H263P:
3138         case AV_CODEC_ID_FLV1:
3139             if (CONFIG_H263_ENCODER)
3140                 ff_clean_h263_qscales(s);
3141             break;
3142         default:
3143             ff_init_qscale_tab(s);
3144         }
3145
3146         s->lambda= s->lambda_table[0];
3147         //FIXME broken
3148     }else
3149         s->lambda = s->current_picture.f.quality;
3150     update_qscale(s);
3151     return 0;
3152 }
3153
3154 /* must be called before writing the header */
3155 static void set_frame_distances(MpegEncContext * s){
3156     assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
3157     s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
3158
3159     if(s->pict_type==AV_PICTURE_TYPE_B){
3160         s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3161         assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3162     }else{
3163         s->pp_time= s->time - s->last_non_b_time;
3164         s->last_non_b_time= s->time;
3165         assert(s->picture_number==0 || s->pp_time > 0);
3166     }
3167 }
3168
3169 static int encode_picture(MpegEncContext *s, int picture_number)
3170 {
3171     int i, ret;
3172     int bits;
3173     int context_count = s->slice_context_count;
3174
3175     s->picture_number = picture_number;
3176
3177     /* Reset the average MB variance */
3178     s->me.mb_var_sum_temp    =
3179     s->me.mc_mb_var_sum_temp = 0;
3180
3181     /* we need to initialize some time vars before we can encode b-frames */
3182     // RAL: Condition added for MPEG1VIDEO
3183     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3184         set_frame_distances(s);
3185     if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3186         ff_set_mpeg4_time(s);
3187
3188     s->me.scene_change_score=0;
3189
3190 //    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3191
3192     if(s->pict_type==AV_PICTURE_TYPE_I){
3193         if(s->msmpeg4_version >= 3) s->no_rounding=1;
3194         else                        s->no_rounding=0;
3195     }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3196         if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3197             s->no_rounding ^= 1;
3198     }
3199
3200     if(s->flags & CODEC_FLAG_PASS2){
3201         if (estimate_qp(s,1) < 0)
3202             return -1;
3203         ff_get_2pass_fcode(s);
3204     }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3205         if(s->pict_type==AV_PICTURE_TYPE_B)
3206             s->lambda= s->last_lambda_for[s->pict_type];
3207         else
3208             s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3209         update_qscale(s);
3210     }
3211
3212     if(s->codec_id != AV_CODEC_ID_AMV){
3213         if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
3214         if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3215         s->q_chroma_intra_matrix   = s->q_intra_matrix;
3216         s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3217     }
3218
3219     s->mb_intra=0; //for the rate distortion & bit compare functions
3220     for(i=1; i<context_count; i++){
3221         ret = ff_update_duplicate_context(s->thread_context[i], s);
3222         if (ret < 0)
3223             return ret;
3224     }
3225
3226     if(ff_init_me(s)<0)
3227         return -1;
3228
3229     /* Estimate motion for every MB */
3230     if(s->pict_type != AV_PICTURE_TYPE_I){
3231         s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3232         s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3233         if (s->pict_type != AV_PICTURE_TYPE_B) {
3234             if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3235                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3236             }
3237         }
3238
3239         s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3240     }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3241         /* I-Frame */
3242         for(i=0; i<s->mb_stride*s->mb_height; i++)
3243             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3244
3245         if(!s->fixed_qscale){
3246             /* finding spatial complexity for I-frame rate control */
3247             s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3248         }
3249     }
3250     for(i=1; i<context_count; i++){
3251         merge_context_after_me(s, s->thread_context[i]);
3252     }
3253     s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3254     s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
3255     emms_c();
3256
3257     if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3258         s->pict_type= AV_PICTURE_TYPE_I;
3259         for(i=0; i<s->mb_stride*s->mb_height; i++)
3260             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3261         if(s->msmpeg4_version >= 3)
3262             s->no_rounding=1;
3263         av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3264                 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3265     }
3266
3267     if(!s->umvplus){
3268         if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3269             s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3270
3271             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3272                 int a,b;
3273                 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3274                 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3275                 s->f_code= FFMAX3(s->f_code, a, b);
3276             }
3277
3278             ff_fix_long_p_mvs(s);
3279             ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3280             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3281                 int j;
3282                 for(i=0; i<2; i++){
3283                     for(j=0; j<2; j++)
3284                         ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3285                                         s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3286                 }
3287             }
3288         }
3289
3290         if(s->pict_type==AV_PICTURE_TYPE_B){
3291             int a, b;
3292
3293             a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3294             b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);