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