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