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