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