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