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