per file doxy
[ffmpeg.git] / libavcodec / mpegvideo.c
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
20  */
21  
22 /**
23  * @file mpegvideo.c
24  * The simplest mpeg encoder (well, it was the simplest!).
25  */ 
26  
27 #include <ctype.h>
28 #include <limits.h>
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "mpegvideo.h"
32
33 #ifdef USE_FASTMEMCPY
34 #include "fastmemcpy.h"
35 #endif
36
37 //#undef NDEBUG
38 //#include <assert.h>
39
40 #ifdef CONFIG_ENCODERS
41 static void encode_picture(MpegEncContext *s, int picture_number);
42 #endif //CONFIG_ENCODERS
43 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
44                                    DCTELEM *block, int n, int qscale);
45 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46                                    DCTELEM *block, int n, int qscale);
47 static void dct_unquantize_h263_c(MpegEncContext *s, 
48                                   DCTELEM *block, int n, int qscale);
49 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50 #ifdef CONFIG_ENCODERS
51 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
53 #endif //CONFIG_ENCODERS
54
55 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
56
57
58 /* enable all paranoid tests for rounding, overflows, etc... */
59 //#define PARANOID
60
61 //#define DEBUG
62
63
64 /* for jpeg fast DCT */
65 #define CONST_BITS 14
66
67 static const uint16_t aanscales[64] = {
68     /* precomputed values scaled up by 14 bits */
69     16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
70     22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
71     21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
72     19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
73     16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
74     12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
75     8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
76     4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
77 };
78
79 static const uint8_t h263_chroma_roundtab[16] = {
80 //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
81     0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
82 };
83
84 #ifdef CONFIG_ENCODERS
85 static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
86 static uint8_t default_fcode_tab[MAX_MV*2+1];
87
88 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
89
90 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
91                            const uint16_t *quant_matrix, int bias, int qmin, int qmax)
92 {
93     int qscale;
94
95     for(qscale=qmin; qscale<=qmax; qscale++){
96         int i;
97         if (s->dsp.fdct == ff_jpeg_fdct_islow) {
98             for(i=0;i<64;i++) {
99                 const int j= s->dsp.idct_permutation[i];
100                 /* 16 <= qscale * quant_matrix[i] <= 7905 */
101                 /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
102                 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
103                 /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
104                 
105                 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
106                                 (qscale * quant_matrix[j]));
107             }
108         } else if (s->dsp.fdct == fdct_ifast) {
109             for(i=0;i<64;i++) {
110                 const int j= s->dsp.idct_permutation[i];
111                 /* 16 <= qscale * quant_matrix[i] <= 7905 */
112                 /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
113                 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114                 /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
115                 
116                 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 
117                                 (aanscales[i] * qscale * quant_matrix[j]));
118             }
119         } else {
120             for(i=0;i<64;i++) {
121                 const int j= s->dsp.idct_permutation[i];
122                 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
123                    So 16           <= qscale * quant_matrix[i]             <= 7905
124                    so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
125                    so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
126                 */
127                 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
128 //                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
129                 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
130
131                 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
132                 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
133             }
134         }
135     }
136 }
137 #endif //CONFIG_ENCODERS
138
139 // move into common.c perhaps 
140 #define CHECKED_ALLOCZ(p, size)\
141 {\
142     p= av_mallocz(size);\
143     if(p==NULL){\
144         perror("malloc");\
145         goto fail;\
146     }\
147 }
148
149 void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
150     int i;
151     int end;
152     
153     st->scantable= src_scantable;
154
155     for(i=0; i<64; i++){
156         int j;
157         j = src_scantable[i];
158         st->permutated[i] = s->dsp.idct_permutation[j];
159 #ifdef ARCH_POWERPC
160         st->inverse[j] = i;
161 #endif
162     }
163     
164     end=-1;
165     for(i=0; i<64; i++){
166         int j;
167         j = st->permutated[i];
168         if(j>end) end=j;
169         st->raster_end[i]= end;
170     }
171 }
172
173 /* init common dct for both encoder and decoder */
174 int DCT_common_init(MpegEncContext *s)
175 {
176     s->dct_unquantize_h263 = dct_unquantize_h263_c;
177     s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
178     s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
179
180 #ifdef CONFIG_ENCODERS
181     s->dct_quantize= dct_quantize_c;
182 #endif
183         
184 #ifdef HAVE_MMX
185     MPV_common_init_mmx(s);
186 #endif
187 #ifdef ARCH_ALPHA
188     MPV_common_init_axp(s);
189 #endif
190 #ifdef HAVE_MLIB
191     MPV_common_init_mlib(s);
192 #endif
193 #ifdef HAVE_MMI
194     MPV_common_init_mmi(s);
195 #endif
196 #ifdef ARCH_ARMV4L
197     MPV_common_init_armv4l(s);
198 #endif
199 #ifdef ARCH_POWERPC
200     MPV_common_init_ppc(s);
201 #endif
202
203 #ifdef CONFIG_ENCODERS
204     s->fast_dct_quantize= s->dct_quantize;
205
206     if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
207         s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
208     }
209
210 #endif //CONFIG_ENCODERS
211
212     /* load & permutate scantables
213        note: only wmv uses differnt ones 
214     */
215     ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
216     ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
217     ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218     ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
219
220     s->picture_structure= PICT_FRAME;
221     
222     return 0;
223 }
224
225 /**
226  * allocates a Picture
227  * The pixels are allocated/set by calling get_buffer() if shared=0
228  */
229 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
230     
231     if(shared){
232         assert(pic->data[0]);
233         assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
234         pic->type= FF_BUFFER_TYPE_SHARED;
235     }else{
236         int r;
237         
238         assert(!pic->data[0]);
239         
240         r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
241         
242         if(r<0 || !pic->age || !pic->type || !pic->data[0]){
243             fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
244             return -1;
245         }
246
247         if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
248             fprintf(stderr, "get_buffer() failed (stride changed)\n");
249             return -1;
250         }
251
252         if(pic->linesize[1] != pic->linesize[2]){
253             fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
254             return -1;
255         }
256
257         s->linesize  = pic->linesize[0];
258         s->uvlinesize= pic->linesize[1];
259     }
260     
261     if(pic->qscale_table==NULL){
262         if (s->encoding) {        
263             CHECKED_ALLOCZ(pic->mb_var   , s->mb_num * sizeof(int16_t))
264             CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
265             CHECKED_ALLOCZ(pic->mb_mean  , s->mb_num * sizeof(int8_t))
266             CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
267         }
268
269         CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
270         CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
271         pic->qstride= s->mb_width;
272     }
273     
274     //it might be nicer if the application would keep track of these but it would require a API change
275     memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
276     s->prev_pict_types[0]= s->pict_type;
277     if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
278         pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
279     
280     return 0;
281 fail: //for the CHECKED_ALLOCZ macro
282     return -1;
283 }
284
285 /**
286  * deallocates a picture
287  */
288 static void free_picture(MpegEncContext *s, Picture *pic){
289     int i;
290
291     if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
292         s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
293     }
294
295     av_freep(&pic->mb_var);
296     av_freep(&pic->mc_mb_var);
297     av_freep(&pic->mb_mean);
298     av_freep(&pic->mb_cmp_score);
299     av_freep(&pic->mbskip_table);
300     av_freep(&pic->qscale_table);
301     
302     if(pic->type == FF_BUFFER_TYPE_INTERNAL){
303         for(i=0; i<4; i++){
304             av_freep(&pic->base[i]);
305             pic->data[i]= NULL;
306         }
307         av_freep(&pic->opaque);
308         pic->type= 0;
309     }else if(pic->type == FF_BUFFER_TYPE_SHARED){
310         for(i=0; i<4; i++){
311             pic->base[i]=
312             pic->data[i]= NULL;
313         }
314         pic->type= 0;        
315     }
316 }
317
318 /* init common structure for both encoder and decoder */
319 int MPV_common_init(MpegEncContext *s)
320 {
321     int y_size, c_size, yc_size, i;
322
323     dsputil_init(&s->dsp, s->avctx);
324     DCT_common_init(s);
325
326     s->flags= s->avctx->flags;
327
328     s->mb_width  = (s->width  + 15) / 16;
329     s->mb_height = (s->height + 15) / 16;
330
331     /* set default edge pos, will be overriden in decode_header if needed */
332     s->h_edge_pos= s->mb_width*16;
333     s->v_edge_pos= s->mb_height*16;
334
335     s->mb_num = s->mb_width * s->mb_height;
336
337     y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
338     c_size = (s->mb_width + 2) * (s->mb_height + 2);
339     yc_size = y_size + 2 * c_size;
340
341     /* convert fourcc to upper case */
342     s->avctx->fourcc=   toupper( s->avctx->fourcc     &0xFF)          
343                      + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
344                      + (toupper((s->avctx->fourcc>>16)&0xFF)<<16) 
345                      + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
346
347     CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
348     s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
349
350     s->avctx->coded_frame= (AVFrame*)&s->current_picture;
351
352     if (s->encoding) {
353         int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
354
355         /* Allocate MV tables */
356         CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(int16_t))
357         CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(int16_t))
358         CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(int16_t))
359         CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
360         CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
361         CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(int16_t))
362
363         //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
364         CHECKED_ALLOCZ(s->me.scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
365         
366         CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
367         CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
368
369         if(s->codec_id==CODEC_ID_MPEG4){
370             CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
371             CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
372         }
373         
374         if(s->msmpeg4_version){
375             CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
376         }
377         CHECKED_ALLOCZ(s->avctx->stats_out, 256);
378     }
379         
380     CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
381     
382     if (s->out_format == FMT_H263 || s->encoding) {
383         int size;
384         /* Allocate MB type table */
385         CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(uint8_t))
386
387         /* MV prediction */
388         size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
389         CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
390     }
391
392     if(s->codec_id==CODEC_ID_MPEG4){
393         /* interlaced direct mode decoding tables */
394         CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
395         CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
396     }
397     /* 4mv b frame decoding table */
398     //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
399     CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
400     if (s->out_format == FMT_H263) {
401         /* ac values */
402         CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
403         s->ac_val[1] = s->ac_val[0] + y_size;
404         s->ac_val[2] = s->ac_val[1] + c_size;
405         
406         /* cbp values */
407         CHECKED_ALLOCZ(s->coded_block, y_size);
408         
409         /* divx501 bitstream reorder buffer */
410         CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
411
412         /* cbp, ac_pred, pred_dir */
413         CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(uint8_t))
414         CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
415     }
416     
417     if (s->h263_pred || s->h263_plus || !s->encoding) {
418         /* dc values */
419         //MN: we need these for error resilience of intra-frames
420         CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
421         s->dc_val[1] = s->dc_val[0] + y_size;
422         s->dc_val[2] = s->dc_val[1] + c_size;
423         for(i=0;i<yc_size;i++)
424             s->dc_val[0][i] = 1024;
425     }
426
427     /* which mb is a intra block */
428     CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
429     memset(s->mbintra_table, 1, s->mb_num);
430     
431     /* default structure is frame */
432     s->picture_structure = PICT_FRAME;
433     
434     /* init macroblock skip table */
435     CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
436     //Note the +1 is for a quicker mpeg4 slice_end detection
437     CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
438     
439     s->block= s->blocks[0];
440
441     s->parse_context.state= -1;
442
443     s->context_initialized = 1;
444     return 0;
445  fail:
446     MPV_common_end(s);
447     return -1;
448 }
449
450
451 //extern int sads;
452
453 /* init common structure for both encoder and decoder */
454 void MPV_common_end(MpegEncContext *s)
455 {
456     int i;
457
458     av_freep(&s->mb_type);
459     av_freep(&s->p_mv_table);
460     av_freep(&s->b_forw_mv_table);
461     av_freep(&s->b_back_mv_table);
462     av_freep(&s->b_bidir_forw_mv_table);
463     av_freep(&s->b_bidir_back_mv_table);
464     av_freep(&s->b_direct_mv_table);
465     av_freep(&s->motion_val);
466     av_freep(&s->dc_val[0]);
467     av_freep(&s->ac_val[0]);
468     av_freep(&s->coded_block);
469     av_freep(&s->mbintra_table);
470     av_freep(&s->cbp_table);
471     av_freep(&s->pred_dir_table);
472     av_freep(&s->me.scratchpad);
473     av_freep(&s->me.map);
474     av_freep(&s->me.score_map);
475     
476     av_freep(&s->mbskip_table);
477     av_freep(&s->prev_pict_types);
478     av_freep(&s->bitstream_buffer);
479     av_freep(&s->tex_pb_buffer);
480     av_freep(&s->pb2_buffer);
481     av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
482     av_freep(&s->co_located_type_table);
483     av_freep(&s->field_mv_table);
484     av_freep(&s->field_select_table);
485     av_freep(&s->avctx->stats_out);
486     av_freep(&s->ac_stats);
487     av_freep(&s->error_status_table);
488
489     for(i=0; i<MAX_PICTURE_COUNT; i++){
490         free_picture(s, &s->picture[i]);
491     }
492     s->context_initialized = 0;
493 }
494
495 #ifdef CONFIG_ENCODERS
496
497 /* init video encoder */
498 int MPV_encode_init(AVCodecContext *avctx)
499 {
500     MpegEncContext *s = avctx->priv_data;
501     int i;
502
503     avctx->pix_fmt = PIX_FMT_YUV420P;
504
505     s->bit_rate = avctx->bit_rate;
506     s->bit_rate_tolerance = avctx->bit_rate_tolerance;
507     s->frame_rate = avctx->frame_rate;
508     s->width = avctx->width;
509     s->height = avctx->height;
510     if(avctx->gop_size > 600){
511         fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
512         avctx->gop_size=600;
513     }
514     s->gop_size = avctx->gop_size;
515     s->rtp_mode = avctx->rtp_mode;
516     s->rtp_payload_size = avctx->rtp_payload_size;
517     if (avctx->rtp_callback)
518         s->rtp_callback = avctx->rtp_callback;
519     s->qmin= avctx->qmin;
520     s->qmax= avctx->qmax;
521     s->max_qdiff= avctx->max_qdiff;
522     s->qcompress= avctx->qcompress;
523     s->qblur= avctx->qblur;
524     s->avctx = avctx;
525     s->flags= avctx->flags;
526     s->max_b_frames= avctx->max_b_frames;
527     s->b_frame_strategy= avctx->b_frame_strategy;
528     s->codec_id= avctx->codec->id;
529     s->luma_elim_threshold  = avctx->luma_elim_threshold;
530     s->chroma_elim_threshold= avctx->chroma_elim_threshold;
531     s->strict_std_compliance= avctx->strict_std_compliance;
532     s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
533     s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
534     s->mpeg_quant= avctx->mpeg_quant;
535
536     if (s->gop_size <= 1) {
537         s->intra_only = 1;
538         s->gop_size = 12;
539     } else {
540         s->intra_only = 0;
541     }
542
543     s->me_method = avctx->me_method;
544
545     /* Fixed QSCALE */
546     s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
547     
548     s->adaptive_quant= (   s->avctx->lumi_masking
549                         || s->avctx->dark_masking
550                         || s->avctx->temporal_cplx_masking 
551                         || s->avctx->spatial_cplx_masking
552                         || s->avctx->p_masking)
553                        && !s->fixed_qscale;
554     
555     s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
556
557     switch(avctx->codec->id) {
558     case CODEC_ID_MPEG1VIDEO:
559         s->out_format = FMT_MPEG1;
560         avctx->delay=0; //FIXME not sure, should check the spec
561         break;
562     case CODEC_ID_MJPEG:
563         s->out_format = FMT_MJPEG;
564         s->intra_only = 1; /* force intra only for jpeg */
565         s->mjpeg_write_tables = 1; /* write all tables */
566         s->mjpeg_data_only_frames = 0; /* write all the needed headers */
567         s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
568         s->mjpeg_vsample[1] = 1; /* the only currently supported values */
569         s->mjpeg_vsample[2] = 1; 
570         s->mjpeg_hsample[0] = 2;
571         s->mjpeg_hsample[1] = 1; 
572         s->mjpeg_hsample[2] = 1; 
573         if (mjpeg_init(s) < 0)
574             return -1;
575         avctx->delay=0;
576         s->low_delay=1;
577         break;
578 #ifdef CONFIG_RISKY
579     case CODEC_ID_H263:
580         if (h263_get_picture_format(s->width, s->height) == 7) {
581             printf("Input picture size isn't suitable for h263 codec! try h263+\n");
582             return -1;
583         }
584         s->out_format = FMT_H263;
585         avctx->delay=0;
586         s->low_delay=1;
587         break;
588     case CODEC_ID_H263P:
589         s->out_format = FMT_H263;
590         s->h263_plus = 1;
591         /* Fx */
592         s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
593         s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
594         /* /Fx */
595         /* These are just to be sure */
596         s->umvplus = 1;
597         avctx->delay=0;
598         s->low_delay=1;
599         break;
600     case CODEC_ID_RV10:
601         s->out_format = FMT_H263;
602         s->h263_rv10 = 1;
603         avctx->delay=0;
604         s->low_delay=1;
605         break;
606     case CODEC_ID_MPEG4:
607         s->out_format = FMT_H263;
608         s->h263_pred = 1;
609         s->unrestricted_mv = 1;
610         s->low_delay= s->max_b_frames ? 0 : 1;
611         avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
612         break;
613     case CODEC_ID_MSMPEG4V1:
614         s->out_format = FMT_H263;
615         s->h263_msmpeg4 = 1;
616         s->h263_pred = 1;
617         s->unrestricted_mv = 1;
618         s->msmpeg4_version= 1;
619         avctx->delay=0;
620         s->low_delay=1;
621         break;
622     case CODEC_ID_MSMPEG4V2:
623         s->out_format = FMT_H263;
624         s->h263_msmpeg4 = 1;
625         s->h263_pred = 1;
626         s->unrestricted_mv = 1;
627         s->msmpeg4_version= 2;
628         avctx->delay=0;
629         s->low_delay=1;
630         break;
631     case CODEC_ID_MSMPEG4V3:
632         s->out_format = FMT_H263;
633         s->h263_msmpeg4 = 1;
634         s->h263_pred = 1;
635         s->unrestricted_mv = 1;
636         s->msmpeg4_version= 3;
637         avctx->delay=0;
638         s->low_delay=1;
639         break;
640     case CODEC_ID_WMV1:
641         s->out_format = FMT_H263;
642         s->h263_msmpeg4 = 1;
643         s->h263_pred = 1;
644         s->unrestricted_mv = 1;
645         s->msmpeg4_version= 4;
646         avctx->delay=0;
647         s->low_delay=1;
648         break;
649     case CODEC_ID_WMV2:
650         s->out_format = FMT_H263;
651         s->h263_msmpeg4 = 1;
652         s->h263_pred = 1;
653         s->unrestricted_mv = 1;
654         s->msmpeg4_version= 5;
655         avctx->delay=0;
656         s->low_delay=1;
657         break;
658 #endif
659     default:
660         return -1;
661     }
662     
663     { /* set up some save defaults, some codecs might override them later */
664         static int done=0;
665         if(!done){
666             int i;
667             done=1;
668
669             default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
670             memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
671             memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
672
673             for(i=-16; i<16; i++){
674                 default_fcode_tab[i + MAX_MV]= 1;
675             }
676         }
677     }
678     s->me.mv_penalty= default_mv_penalty;
679     s->fcode_tab= default_fcode_tab;
680     s->y_dc_scale_table=
681     s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
682  
683     /* dont use mv_penalty table for crap MV as it would be confused */
684     //FIXME remove after fixing / removing old ME
685     if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
686
687     s->encoding = 1;
688
689     /* init */
690     if (MPV_common_init(s) < 0)
691         return -1;
692     
693     ff_init_me(s);
694
695 #ifdef CONFIG_ENCODERS
696 #ifdef CONFIG_RISKY
697     if (s->out_format == FMT_H263)
698         h263_encode_init(s);
699     if(s->msmpeg4_version)
700         ff_msmpeg4_encode_init(s);
701 #endif
702     if (s->out_format == FMT_MPEG1)
703         ff_mpeg1_encode_init(s);
704 #endif
705
706     /* init default q matrix */
707     for(i=0;i<64;i++) {
708         int j= s->dsp.idct_permutation[i];
709 #ifdef CONFIG_RISKY
710         if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
711             s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
712             s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
713         }else if(s->out_format == FMT_H263){
714             s->intra_matrix[j] =
715             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
716         }else
717 #endif
718         { /* mpeg1 */
719             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
720             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
721         }
722     }
723
724     /* precompute matrix */
725     /* for mjpeg, we do include qscale in the matrix */
726     if (s->out_format != FMT_MJPEG) {
727         convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
728                        s->intra_matrix, s->intra_quant_bias, 1, 31);
729         convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
730                        s->inter_matrix, s->inter_quant_bias, 1, 31);
731     }
732
733     if(ff_rate_control_init(s) < 0)
734         return -1;
735
736     s->picture_number = 0;
737     s->picture_in_gop_number = 0;
738     s->fake_picture_number = 0;
739     /* motion detector init */
740     s->f_code = 1;
741     s->b_code = 1;
742
743     return 0;
744 }
745
746 int MPV_encode_end(AVCodecContext *avctx)
747 {
748     MpegEncContext *s = avctx->priv_data;
749
750 #ifdef STATS
751     print_stats();
752 #endif
753
754     ff_rate_control_uninit(s);
755
756     MPV_common_end(s);
757     if (s->out_format == FMT_MJPEG)
758         mjpeg_close(s);
759       
760     return 0;
761 }
762
763 #endif //CONFIG_ENCODERS
764
765 void init_rl(RLTable *rl)
766 {
767     int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
768     uint8_t index_run[MAX_RUN+1];
769     int last, run, level, start, end, i;
770
771     /* compute max_level[], max_run[] and index_run[] */
772     for(last=0;last<2;last++) {
773         if (last == 0) {
774             start = 0;
775             end = rl->last;
776         } else {
777             start = rl->last;
778             end = rl->n;
779         }
780
781         memset(max_level, 0, MAX_RUN + 1);
782         memset(max_run, 0, MAX_LEVEL + 1);
783         memset(index_run, rl->n, MAX_RUN + 1);
784         for(i=start;i<end;i++) {
785             run = rl->table_run[i];
786             level = rl->table_level[i];
787             if (index_run[run] == rl->n)
788                 index_run[run] = i;
789             if (level > max_level[run])
790                 max_level[run] = level;
791             if (run > max_run[level])
792                 max_run[level] = run;
793         }
794         rl->max_level[last] = av_malloc(MAX_RUN + 1);
795         memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
796         rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
797         memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
798         rl->index_run[last] = av_malloc(MAX_RUN + 1);
799         memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
800     }
801 }
802
803 /* draw the edges of width 'w' of an image of size width, height */
804 //FIXME check that this is ok for mpeg4 interlaced
805 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
806 {
807     uint8_t *ptr, *last_line;
808     int i;
809
810     last_line = buf + (height - 1) * wrap;
811     for(i=0;i<w;i++) {
812         /* top and bottom */
813         memcpy(buf - (i + 1) * wrap, buf, width);
814         memcpy(last_line + (i + 1) * wrap, last_line, width);
815     }
816     /* left and right */
817     ptr = buf;
818     for(i=0;i<height;i++) {
819         memset(ptr - w, ptr[0], w);
820         memset(ptr + width, ptr[width-1], w);
821         ptr += wrap;
822     }
823     /* corners */
824     for(i=0;i<w;i++) {
825         memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
826         memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
827         memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
828         memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
829     }
830 }
831
832 static int find_unused_picture(MpegEncContext *s, int shared){
833     int i;
834     
835     if(shared){
836         for(i=0; i<MAX_PICTURE_COUNT; i++){
837             if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
838         }
839     }else{
840         for(i=0; i<MAX_PICTURE_COUNT; i++){
841             if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
842         }
843         for(i=0; i<MAX_PICTURE_COUNT; i++){
844             if(s->picture[i].data[0]==NULL) break;
845         }
846     }
847
848     assert(i<MAX_PICTURE_COUNT);
849     return i;
850 }
851
852 /* generic function for encode/decode called before a frame is coded/decoded */
853 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
854 {
855     int i;
856     AVFrame *pic;
857
858     s->mb_skiped = 0;
859     
860     /* mark&release old frames */
861     if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
862         for(i=0; i<MAX_PICTURE_COUNT; i++){
863 //printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
864             if(s->picture[i].data[0] == s->last_picture.data[0]){
865 //                s->picture[i].reference=0;
866                 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
867                 break;
868             }    
869         }
870         assert(i<MAX_PICTURE_COUNT);
871
872         /* release forgotten pictures */
873         /* if(mpeg124/h263) */
874         if(!s->encoding){
875             for(i=0; i<MAX_PICTURE_COUNT; i++){
876                 if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
877                     fprintf(stderr, "releasing zombie picture\n");
878                     avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
879                 }
880             }
881         }
882     }
883 alloc:
884     if(!s->encoding){
885         i= find_unused_picture(s, 0);
886     
887         pic= (AVFrame*)&s->picture[i];
888         pic->reference= s->pict_type != B_TYPE;
889         pic->coded_picture_number= s->current_picture.coded_picture_number+1;
890         
891         alloc_picture(s, (Picture*)pic, 0);
892
893         s->current_picture= s->picture[i];
894     }
895
896     if (s->pict_type != B_TYPE) {
897         s->last_picture= s->next_picture;
898         s->next_picture= s->current_picture;
899     }
900     
901     if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
902         fprintf(stderr, "warning: first frame is no keyframe\n");
903         assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
904         goto alloc;
905     }
906    
907     s->hurry_up= s->avctx->hurry_up;
908     s->error_resilience= avctx->error_resilience;
909
910     /* set dequantizer, we cant do it during init as it might change for mpeg4
911        and we cant do it in the header decode as init isnt called for mpeg4 there yet */
912     if(s->out_format == FMT_H263){
913         if(s->mpeg_quant)
914             s->dct_unquantize = s->dct_unquantize_mpeg2;
915         else
916             s->dct_unquantize = s->dct_unquantize_h263;
917     }else 
918         s->dct_unquantize = s->dct_unquantize_mpeg1;
919
920     return 0;
921 }
922
923 /* generic function for encode/decode called after a frame has been coded/decoded */
924 void MPV_frame_end(MpegEncContext *s)
925 {
926     int i;
927     /* draw edge for correct motion prediction if outside */
928     if(s->codec_id!=CODEC_ID_SVQ1){
929         if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
930             draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
931             draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
932             draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
933         }
934     }
935     emms_c();
936     
937     s->last_pict_type    = s->pict_type;
938     if(s->pict_type!=B_TYPE){
939         s->last_non_b_pict_type= s->pict_type;
940     }
941     
942     s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
943     s->current_picture.pict_type= s->pict_type;
944     s->current_picture.key_frame= s->pict_type == I_TYPE;
945     
946     /* copy back current_picture variables */
947     for(i=0; i<MAX_PICTURE_COUNT; i++){
948         if(s->picture[i].data[0] == s->current_picture.data[0]){
949             s->picture[i]= s->current_picture;
950             break;
951         }    
952     }
953     assert(i<MAX_PICTURE_COUNT);
954
955     /* release non refernce frames */
956     for(i=0; i<MAX_PICTURE_COUNT; i++){
957         if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
958             s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
959     }
960     if(s->avctx->debug&FF_DEBUG_SKIP){
961         int x,y;        
962         for(y=0; y<s->mb_height; y++){
963             for(x=0; x<s->mb_width; x++){
964                 int count= s->mbskip_table[x + y*s->mb_width];
965                 if(count>9) count=9;
966                 printf(" %1d", count);
967             }
968             printf("\n");
969         }
970         printf("pict type: %d\n", s->pict_type);
971     }
972 }
973
974 #ifdef CONFIG_ENCODERS
975
976 static int get_sae(uint8_t *src, int ref, int stride){
977     int x,y;
978     int acc=0;
979     
980     for(y=0; y<16; y++){
981         for(x=0; x<16; x++){
982             acc+= ABS(src[x+y*stride] - ref);
983         }
984     }
985     
986     return acc;
987 }
988
989 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
990     int x, y, w, h;
991     int acc=0;
992     
993     w= s->width &~15;
994     h= s->height&~15;
995     
996     for(y=0; y<h; y+=16){
997         for(x=0; x<w; x+=16){
998             int offset= x + y*stride;
999             int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1000             int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1001             int sae = get_sae(src + offset, mean, stride);
1002             
1003             acc+= sae + 500 < sad;
1004         }
1005     }
1006     return acc;
1007 }
1008
1009
1010 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1011     AVFrame *pic;
1012     int i;
1013     const int encoding_delay= s->max_b_frames;
1014     int direct=1;
1015
1016     if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1017     if(pic_arg->linesize[0] != s->linesize) direct=0;
1018     if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1019     if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1020   
1021 //    printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1022     
1023     if(direct){
1024         i= find_unused_picture(s, 1);
1025
1026         pic= (AVFrame*)&s->picture[i];
1027         pic->reference= 1;
1028     
1029         for(i=0; i<4; i++){
1030             pic->data[i]= pic_arg->data[i];
1031             pic->linesize[i]= pic_arg->linesize[i];
1032         }
1033         alloc_picture(s, (Picture*)pic, 1);
1034     }else{
1035         i= find_unused_picture(s, 0);
1036
1037         pic= (AVFrame*)&s->picture[i];
1038         pic->reference= 1;
1039
1040         alloc_picture(s, (Picture*)pic, 0);
1041
1042         if(   pic->data[0] == pic_arg->data[0] 
1043            && pic->data[1] == pic_arg->data[1]
1044            && pic->data[2] == pic_arg->data[2]){
1045        // empty
1046         }else{
1047             int h_chroma_shift, v_chroma_shift;
1048         
1049             avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1050         
1051             for(i=0; i<3; i++){
1052                 int src_stride= pic_arg->linesize[i];
1053                 int dst_stride= i ? s->uvlinesize : s->linesize;
1054                 int h_shift= i ? h_chroma_shift : 0;
1055                 int v_shift= i ? v_chroma_shift : 0;
1056                 int w= s->width >>h_shift;
1057                 int h= s->height>>v_shift;
1058                 uint8_t *src= pic_arg->data[i];
1059                 uint8_t *dst= pic->data[i];
1060             
1061                 if(src_stride==dst_stride)
1062                     memcpy(dst, src, src_stride*h);
1063                 else{
1064                     while(h--){
1065                         memcpy(dst, src, w);
1066                         dst += dst_stride;
1067                         src += src_stride;
1068                     }
1069                 }
1070             }
1071         }
1072     }
1073     pic->quality= pic_arg->quality;
1074     pic->pict_type= pic_arg->pict_type;
1075     pic->pts = pic_arg->pts;
1076     
1077     if(s->input_picture[encoding_delay])
1078         pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1079
1080     /* shift buffer entries */
1081     for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1082         s->input_picture[i-1]= s->input_picture[i];
1083         
1084     s->input_picture[encoding_delay]= (Picture*)pic;
1085
1086     return 0;
1087 }
1088
1089 static void select_input_picture(MpegEncContext *s){
1090     int i;
1091     const int encoding_delay= s->max_b_frames;
1092     int coded_pic_num=0;    
1093
1094     if(s->reordered_input_picture[0])
1095         coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1096
1097     for(i=1; i<MAX_PICTURE_COUNT; i++)
1098         s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1099     s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1100
1101     /* set next picture types & ordering */
1102     if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1103         if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
1104             s->reordered_input_picture[0]= s->input_picture[0];
1105             s->reordered_input_picture[0]->pict_type= I_TYPE;
1106             s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1107         }else{
1108             int b_frames;
1109             
1110             if(s->flags&CODEC_FLAG_PASS2){
1111                 for(i=0; i<s->max_b_frames+1; i++){
1112                     int pict_num= s->input_picture[0]->display_picture_number + i;
1113                     int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1114                     s->input_picture[i]->pict_type= pict_type;
1115                     
1116                     if(i + 1 >= s->rc_context.num_entries) break;
1117                 }
1118             }
1119
1120             if(s->input_picture[0]->pict_type){
1121                 /* user selected pict_type */
1122                 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1123                     if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1124                 }
1125             
1126                 if(b_frames > s->max_b_frames){
1127                     fprintf(stderr, "warning, too many bframes in a row\n");
1128                     b_frames = s->max_b_frames;
1129                 }
1130             }else if(s->b_frame_strategy==0){
1131                 b_frames= s->max_b_frames;
1132             }else if(s->b_frame_strategy==1){
1133                 for(i=1; i<s->max_b_frames+1; i++){
1134                     if(s->input_picture[i]->b_frame_score==0){
1135                         s->input_picture[i]->b_frame_score= 
1136                             get_intra_count(s, s->input_picture[i  ]->data[0], 
1137                                                s->input_picture[i-1]->data[0], s->linesize) + 1;
1138                     }
1139                 }
1140                 for(i=0; i<s->max_b_frames; i++){
1141                     if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1142                 }
1143                                 
1144                 b_frames= FFMAX(0, i-1);
1145                 
1146                 /* reset scores */
1147                 for(i=0; i<b_frames+1; i++){
1148                     s->input_picture[i]->b_frame_score=0;
1149                 }
1150             }else{
1151                 fprintf(stderr, "illegal b frame strategy\n");
1152                 b_frames=0;
1153             }
1154
1155             emms_c();
1156 //static int b_count=0;
1157 //b_count+= b_frames;
1158 //printf("b_frames: %d\n", b_count);
1159                         
1160             s->reordered_input_picture[0]= s->input_picture[b_frames];
1161             if(   s->picture_in_gop_number + b_frames >= s->gop_size 
1162                || s->reordered_input_picture[0]->pict_type== I_TYPE)
1163                 s->reordered_input_picture[0]->pict_type= I_TYPE;
1164             else
1165                 s->reordered_input_picture[0]->pict_type= P_TYPE;
1166             s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1167             for(i=0; i<b_frames; i++){
1168                 coded_pic_num++;
1169                 s->reordered_input_picture[i+1]= s->input_picture[i];
1170                 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1171                 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1172             }
1173         }
1174     }
1175     
1176     if(s->reordered_input_picture[0]){
1177        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
1178
1179         if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1180             int i= find_unused_picture(s, 0);
1181             Picture *pic= &s->picture[i];
1182
1183             s->new_picture= *s->reordered_input_picture[0];
1184
1185             /* mark us unused / free shared pic */
1186             for(i=0; i<4; i++)
1187                 s->reordered_input_picture[0]->data[i]= NULL;
1188             s->reordered_input_picture[0]->type= 0;
1189             
1190             pic->pict_type = s->reordered_input_picture[0]->pict_type;
1191             pic->quality   = s->reordered_input_picture[0]->quality;
1192             pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1193             pic->reference = s->reordered_input_picture[0]->reference;
1194             
1195             alloc_picture(s, pic, 0);
1196
1197             s->current_picture= *pic;
1198         }else{
1199             assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
1200                    || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1201             
1202             s->new_picture= *s->reordered_input_picture[0];
1203
1204             for(i=0; i<4; i++){
1205                 s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
1206             }
1207             s->current_picture= *s->reordered_input_picture[0];
1208         }
1209     
1210         s->picture_number= s->new_picture.display_picture_number;
1211 //printf("dpn:%d\n", s->picture_number);
1212     }else{
1213        memset(&s->new_picture, 0, sizeof(Picture));
1214     }
1215 }
1216
1217 int MPV_encode_picture(AVCodecContext *avctx,
1218                        unsigned char *buf, int buf_size, void *data)
1219 {
1220     MpegEncContext *s = avctx->priv_data;
1221     AVFrame *pic_arg = data;
1222     int i;
1223
1224     init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1225
1226     s->picture_in_gop_number++;
1227
1228     load_input_picture(s, pic_arg);
1229     
1230     select_input_picture(s);
1231     
1232     /* output? */
1233     if(s->new_picture.data[0]){
1234
1235         s->pict_type= s->new_picture.pict_type;
1236         if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1237             s->qscale= (int)(s->new_picture.quality+0.5);
1238             assert(s->qscale);
1239         }
1240 //emms_c();
1241 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1242         MPV_frame_start(s, avctx);
1243
1244         encode_picture(s, s->picture_number);
1245         
1246         avctx->real_pict_num  = s->picture_number;
1247         avctx->header_bits = s->header_bits;
1248         avctx->mv_bits     = s->mv_bits;
1249         avctx->misc_bits   = s->misc_bits;
1250         avctx->i_tex_bits  = s->i_tex_bits;
1251         avctx->p_tex_bits  = s->p_tex_bits;
1252         avctx->i_count     = s->i_count;
1253         avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1254         avctx->skip_count  = s->skip_count;
1255
1256         MPV_frame_end(s);
1257
1258         if (s->out_format == FMT_MJPEG)
1259             mjpeg_picture_trailer(s);
1260         
1261         if(s->flags&CODEC_FLAG_PASS1)
1262             ff_write_pass1_stats(s);
1263     }
1264
1265     s->input_picture_number++;
1266
1267     flush_put_bits(&s->pb);
1268     s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1269     
1270     s->total_bits += s->frame_bits;
1271     avctx->frame_bits  = s->frame_bits;
1272
1273     for(i=0; i<4; i++){
1274         avctx->error[i] += s->current_picture.error[i];
1275     }
1276     
1277     return pbBufPtr(&s->pb) - s->pb.buf;
1278 }
1279
1280 #endif //CONFIG_ENCODERS
1281
1282 static inline void gmc1_motion(MpegEncContext *s,
1283                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1284                                int dest_offset,
1285                                uint8_t **ref_picture, int src_offset)
1286 {
1287     uint8_t *ptr;
1288     int offset, src_x, src_y, linesize, uvlinesize;
1289     int motion_x, motion_y;
1290     int emu=0;
1291
1292     motion_x= s->sprite_offset[0][0];
1293     motion_y= s->sprite_offset[0][1];
1294     src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1295     src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1296     motion_x<<=(3-s->sprite_warping_accuracy);
1297     motion_y<<=(3-s->sprite_warping_accuracy);
1298     src_x = clip(src_x, -16, s->width);
1299     if (src_x == s->width)
1300         motion_x =0;
1301     src_y = clip(src_y, -16, s->height);
1302     if (src_y == s->height)
1303         motion_y =0;
1304
1305     linesize = s->linesize;
1306     uvlinesize = s->uvlinesize;
1307     
1308     ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1309
1310     dest_y+=dest_offset;
1311     if(s->flags&CODEC_FLAG_EMU_EDGE){
1312         if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1313                               || src_y + 17 >= s->v_edge_pos){
1314             ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1315             ptr= s->edge_emu_buffer;
1316         }
1317     }
1318     
1319     if((motion_x|motion_y)&7){
1320         s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1321         s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1322     }else{
1323         int dxy;
1324         
1325         dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1326         if (s->no_rounding){
1327             s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1328         }else{
1329             s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1330         }
1331     }
1332     
1333     if(s->flags&CODEC_FLAG_GRAY) return;
1334
1335     motion_x= s->sprite_offset[1][0];
1336     motion_y= s->sprite_offset[1][1];
1337     src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1338     src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1339     motion_x<<=(3-s->sprite_warping_accuracy);
1340     motion_y<<=(3-s->sprite_warping_accuracy);
1341     src_x = clip(src_x, -8, s->width>>1);
1342     if (src_x == s->width>>1)
1343         motion_x =0;
1344     src_y = clip(src_y, -8, s->height>>1);
1345     if (src_y == s->height>>1)
1346         motion_y =0;
1347
1348     offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1349     ptr = ref_picture[1] + offset;
1350     if(s->flags&CODEC_FLAG_EMU_EDGE){
1351         if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1352                               || src_y + 9 >= s->v_edge_pos>>1){
1353             ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1354             ptr= s->edge_emu_buffer;
1355             emu=1;
1356         }
1357     }
1358     s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1359     
1360     ptr = ref_picture[2] + offset;
1361     if(emu){
1362         ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1363         ptr= s->edge_emu_buffer;
1364     }
1365     s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1366     
1367     return;
1368 }
1369
1370 static inline void gmc_motion(MpegEncContext *s,
1371                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1372                                int dest_offset,
1373                                uint8_t **ref_picture, int src_offset)
1374 {
1375     uint8_t *ptr;
1376     int linesize, uvlinesize;
1377     const int a= s->sprite_warping_accuracy;
1378     int ox, oy;
1379
1380     linesize = s->linesize;
1381     uvlinesize = s->uvlinesize;
1382
1383     ptr = ref_picture[0] + src_offset;
1384
1385     dest_y+=dest_offset;
1386     
1387     ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1388     oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1389
1390     s->dsp.gmc(dest_y, ptr, linesize, 16,
1391            ox, 
1392            oy, 
1393            s->sprite_delta[0][0], s->sprite_delta[0][1],
1394            s->sprite_delta[1][0], s->sprite_delta[1][1], 
1395            a+1, (1<<(2*a+1)) - s->no_rounding,
1396            s->h_edge_pos, s->v_edge_pos);
1397     s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1398            ox + s->sprite_delta[0][0]*8, 
1399            oy + s->sprite_delta[1][0]*8, 
1400            s->sprite_delta[0][0], s->sprite_delta[0][1],
1401            s->sprite_delta[1][0], s->sprite_delta[1][1], 
1402            a+1, (1<<(2*a+1)) - s->no_rounding,
1403            s->h_edge_pos, s->v_edge_pos);
1404
1405     if(s->flags&CODEC_FLAG_GRAY) return;
1406
1407
1408     dest_cb+=dest_offset>>1;
1409     dest_cr+=dest_offset>>1;
1410     
1411     ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1412     oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1413
1414     ptr = ref_picture[1] + (src_offset>>1);
1415     s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1416            ox, 
1417            oy, 
1418            s->sprite_delta[0][0], s->sprite_delta[0][1],
1419            s->sprite_delta[1][0], s->sprite_delta[1][1], 
1420            a+1, (1<<(2*a+1)) - s->no_rounding,
1421            s->h_edge_pos>>1, s->v_edge_pos>>1);
1422     
1423     ptr = ref_picture[2] + (src_offset>>1);
1424     s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1425            ox, 
1426            oy, 
1427            s->sprite_delta[0][0], s->sprite_delta[0][1],
1428            s->sprite_delta[1][0], s->sprite_delta[1][1], 
1429            a+1, (1<<(2*a+1)) - s->no_rounding,
1430            s->h_edge_pos>>1, s->v_edge_pos>>1);
1431 }
1432
1433
1434 void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h, 
1435                                     int src_x, int src_y, int w, int h){
1436     int x, y;
1437     int start_y, start_x, end_y, end_x;
1438     uint8_t *buf= s->edge_emu_buffer;
1439
1440     if(src_y>= h){
1441         src+= (h-1-src_y)*linesize;
1442         src_y=h-1;
1443     }else if(src_y<=-block_h){
1444         src+= (1-block_h-src_y)*linesize;
1445         src_y=1-block_h;
1446     }
1447     if(src_x>= w){
1448         src+= (w-1-src_x);
1449         src_x=w-1;
1450     }else if(src_x<=-block_w){
1451         src+= (1-block_w-src_x);
1452         src_x=1-block_w;
1453     }
1454
1455     start_y= FFMAX(0, -src_y);
1456     start_x= FFMAX(0, -src_x);
1457     end_y= FFMIN(block_h, h-src_y);
1458     end_x= FFMIN(block_w, w-src_x);
1459
1460     // copy existing part
1461     for(y=start_y; y<end_y; y++){
1462         for(x=start_x; x<end_x; x++){
1463             buf[x + y*linesize]= src[x + y*linesize];
1464         }
1465     }
1466
1467     //top
1468     for(y=0; y<start_y; y++){
1469         for(x=start_x; x<end_x; x++){
1470             buf[x + y*linesize]= buf[x + start_y*linesize];
1471         }
1472     }
1473
1474     //bottom
1475     for(y=end_y; y<block_h; y++){
1476         for(x=start_x; x<end_x; x++){
1477             buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1478         }
1479     }
1480                                     
1481     for(y=0; y<block_h; y++){
1482        //left
1483         for(x=0; x<start_x; x++){
1484             buf[x + y*linesize]= buf[start_x + y*linesize];
1485         }
1486        
1487        //right
1488         for(x=end_x; x<block_w; x++){
1489             buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1490         }
1491     }
1492 }
1493
1494
1495 /* apply one mpeg motion vector to the three components */
1496 static inline void mpeg_motion(MpegEncContext *s,
1497                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1498                                int dest_offset,
1499                                uint8_t **ref_picture, int src_offset,
1500                                int field_based, op_pixels_func (*pix_op)[4],
1501                                int motion_x, int motion_y, int h)
1502 {
1503     uint8_t *ptr;
1504     int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1505     int emu=0;
1506 #if 0    
1507 if(s->quarter_sample)
1508 {
1509     motion_x>>=1;
1510     motion_y>>=1;
1511 }
1512 #endif
1513     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1514     src_x = s->mb_x * 16 + (motion_x >> 1);
1515     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1516                 
1517     /* WARNING: do no forget half pels */
1518     height = s->height >> field_based;
1519     v_edge_pos = s->v_edge_pos >> field_based;
1520     src_x = clip(src_x, -16, s->width);
1521     if (src_x == s->width)
1522         dxy &= ~1;
1523     src_y = clip(src_y, -16, height);
1524     if (src_y == height)
1525         dxy &= ~2;
1526     linesize   = s->linesize << field_based;
1527     uvlinesize = s->uvlinesize << field_based;
1528     ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1529     dest_y += dest_offset;
1530
1531     if(s->flags&CODEC_FLAG_EMU_EDGE){
1532         if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1533                               || src_y + (motion_y&1) + h  > v_edge_pos){
1534             ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1535                              src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1536             ptr= s->edge_emu_buffer + src_offset;
1537             emu=1;
1538         }
1539     }
1540     pix_op[0][dxy](dest_y, ptr, linesize, h);
1541
1542     if(s->flags&CODEC_FLAG_GRAY) return;
1543
1544     if (s->out_format == FMT_H263) {
1545         dxy = 0;
1546         if ((motion_x & 3) != 0)
1547             dxy |= 1;
1548         if ((motion_y & 3) != 0)
1549             dxy |= 2;
1550         mx = motion_x >> 2;
1551         my = motion_y >> 2;
1552     } else {
1553         mx = motion_x / 2;
1554         my = motion_y / 2;
1555         dxy = ((my & 1) << 1) | (mx & 1);
1556         mx >>= 1;
1557         my >>= 1;
1558     }
1559     
1560     src_x = s->mb_x * 8 + mx;
1561     src_y = s->mb_y * (8 >> field_based) + my;
1562     src_x = clip(src_x, -8, s->width >> 1);
1563     if (src_x == (s->width >> 1))
1564         dxy &= ~1;
1565     src_y = clip(src_y, -8, height >> 1);
1566     if (src_y == (height >> 1))
1567         dxy &= ~2;
1568     offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1569     ptr = ref_picture[1] + offset;
1570     if(emu){
1571         ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1572                          src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1573         ptr= s->edge_emu_buffer + (src_offset >> 1);
1574     }
1575     pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1576
1577     ptr = ref_picture[2] + offset;
1578     if(emu){
1579         ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1580                          src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1581         ptr= s->edge_emu_buffer + (src_offset >> 1);
1582     }
1583     pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1584 }
1585
1586 static inline void qpel_motion(MpegEncContext *s,
1587                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1588                                int dest_offset,
1589                                uint8_t **ref_picture, int src_offset,
1590                                int field_based, op_pixels_func (*pix_op)[4],
1591                                qpel_mc_func (*qpix_op)[16],
1592                                int motion_x, int motion_y, int h)
1593 {
1594     uint8_t *ptr;
1595     int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1596     int emu=0;
1597
1598     dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1599     src_x = s->mb_x * 16 + (motion_x >> 2);
1600     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1601
1602     height = s->height >> field_based;
1603     v_edge_pos = s->v_edge_pos >> field_based;
1604     src_x = clip(src_x, -16, s->width);
1605     if (src_x == s->width)
1606         dxy &= ~3;
1607     src_y = clip(src_y, -16, height);
1608     if (src_y == height)
1609         dxy &= ~12;
1610     linesize = s->linesize << field_based;
1611     uvlinesize = s->uvlinesize << field_based;
1612     ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1613     dest_y += dest_offset;
1614 //printf("%d %d %d\n", src_x, src_y, dxy);
1615     
1616     if(s->flags&CODEC_FLAG_EMU_EDGE){
1617         if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1618                               || src_y + (motion_y&3) + h  > v_edge_pos){
1619             ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1620                              src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1621             ptr= s->edge_emu_buffer + src_offset;
1622             emu=1;
1623         }
1624     }
1625     if(!field_based)
1626         qpix_op[0][dxy](dest_y, ptr, linesize);
1627     else{
1628         //damn interlaced mode
1629         //FIXME boundary mirroring is not exactly correct here
1630         qpix_op[1][dxy](dest_y  , ptr  , linesize);
1631         qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1632     }
1633
1634     if(s->flags&CODEC_FLAG_GRAY) return;
1635
1636     if(field_based){
1637         mx= motion_x/2;
1638         my= motion_y>>1;
1639     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1640         static const int rtab[8]= {0,0,1,1,0,0,0,1};
1641         mx= (motion_x>>1) + rtab[motion_x&7];
1642         my= (motion_y>>1) + rtab[motion_y&7];
1643     }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1644         mx= (motion_x>>1)|(motion_x&1);
1645         my= (motion_y>>1)|(motion_y&1);
1646     }else{
1647         mx= motion_x/2;
1648         my= motion_y/2;
1649     }
1650     mx= (mx>>1)|(mx&1);
1651     my= (my>>1)|(my&1);
1652
1653     dxy= (mx&1) | ((my&1)<<1);
1654     mx>>=1;
1655     my>>=1;
1656
1657     src_x = s->mb_x * 8 + mx;
1658     src_y = s->mb_y * (8 >> field_based) + my;
1659     src_x = clip(src_x, -8, s->width >> 1);
1660     if (src_x == (s->width >> 1))
1661         dxy &= ~1;
1662     src_y = clip(src_y, -8, height >> 1);
1663     if (src_y == (height >> 1))
1664         dxy &= ~2;
1665
1666     offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1667     ptr = ref_picture[1] + offset;
1668     if(emu){
1669         ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1670                          src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1671         ptr= s->edge_emu_buffer + (src_offset >> 1);
1672     }
1673     pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1674     
1675     ptr = ref_picture[2] + offset;
1676     if(emu){
1677         ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1678                          src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1679         ptr= s->edge_emu_buffer + (src_offset >> 1);
1680     }
1681     pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1682 }
1683
1684 inline int ff_h263_round_chroma(int x){
1685     if (x >= 0)
1686         return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1687     else {
1688         x = -x;
1689         return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1690     }
1691 }
1692
1693 static inline void MPV_motion(MpegEncContext *s, 
1694                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1695                               int dir, uint8_t **ref_picture, 
1696                               op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1697 {
1698     int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1699     int mb_x, mb_y, i;
1700     uint8_t *ptr, *dest;
1701     int emu=0;
1702
1703     mb_x = s->mb_x;
1704     mb_y = s->mb_y;
1705
1706     switch(s->mv_type) {
1707     case MV_TYPE_16X16:
1708 #ifdef CONFIG_RISKY
1709         if(s->mcsel){
1710             if(s->real_sprite_warping_points==1){
1711                 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1712                             ref_picture, 0);
1713             }else{
1714                 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1715                             ref_picture, 0);
1716             }
1717         }else if(s->quarter_sample){
1718             qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1719                         ref_picture, 0,
1720                         0, pix_op, qpix_op,
1721                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1722         }else if(s->mspel){
1723             ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
1724                         ref_picture, pix_op,
1725                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1726         }else
1727 #endif
1728         {
1729             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1730                         ref_picture, 0,
1731                         0, pix_op,
1732                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1733         }           
1734         break;
1735     case MV_TYPE_8X8:
1736         mx = 0;
1737         my = 0;
1738         if(s->quarter_sample){
1739             for(i=0;i<4;i++) {
1740                 motion_x = s->mv[dir][i][0];
1741                 motion_y = s->mv[dir][i][1];
1742
1743                 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1744                 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1745                 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1746                     
1747                 /* WARNING: do no forget half pels */
1748                 src_x = clip(src_x, -16, s->width);
1749                 if (src_x == s->width)
1750                     dxy &= ~3;
1751                 src_y = clip(src_y, -16, s->height);
1752                 if (src_y == s->height)
1753                     dxy &= ~12;
1754                     
1755                 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1756                 if(s->flags&CODEC_FLAG_EMU_EDGE){
1757                     if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1758                                           || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1759                         ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1760                         ptr= s->edge_emu_buffer;
1761                     }
1762                 }
1763                 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1764                 qpix_op[1][dxy](dest, ptr, s->linesize);
1765
1766                 mx += s->mv[dir][i][0]/2;
1767                 my += s->mv[dir][i][1]/2;
1768             }
1769         }else{
1770             for(i=0;i<4;i++) {
1771                 motion_x = s->mv[dir][i][0];
1772                 motion_y = s->mv[dir][i][1];
1773
1774                 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1775                 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1776                 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1777                     
1778                 /* WARNING: do no forget half pels */
1779                 src_x = clip(src_x, -16, s->width);
1780                 if (src_x == s->width)
1781                     dxy &= ~1;
1782                 src_y = clip(src_y, -16, s->height);
1783                 if (src_y == s->height)
1784                     dxy &= ~2;
1785                     
1786                 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1787                 if(s->flags&CODEC_FLAG_EMU_EDGE){
1788                     if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1789                                           || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1790                         ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1791                         ptr= s->edge_emu_buffer;
1792                     }
1793                 }
1794                 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1795                 pix_op[1][dxy](dest, ptr, s->linesize, 8);
1796
1797                 mx += s->mv[dir][i][0];
1798                 my += s->mv[dir][i][1];
1799             }
1800         }
1801
1802         if(s->flags&CODEC_FLAG_GRAY) break;
1803         /* In case of 8X8, we construct a single chroma motion vector
1804            with a special rounding */
1805         mx= ff_h263_round_chroma(mx);
1806         my= ff_h263_round_chroma(my);
1807         dxy = ((my & 1) << 1) | (mx & 1);
1808         mx >>= 1;
1809         my >>= 1;
1810
1811         src_x = mb_x * 8 + mx;
1812         src_y = mb_y * 8 + my;
1813         src_x = clip(src_x, -8, s->width/2);
1814         if (src_x == s->width/2)
1815             dxy &= ~1;
1816         src_y = clip(src_y, -8, s->height/2);
1817         if (src_y == s->height/2)
1818             dxy &= ~2;
1819         
1820         offset = (src_y * (s->uvlinesize)) + src_x;
1821         ptr = ref_picture[1] + offset;
1822         if(s->flags&CODEC_FLAG_EMU_EDGE){
1823                 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1824                                       || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1825                     ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1826                     ptr= s->edge_emu_buffer;
1827                     emu=1;
1828                 }
1829             }
1830         pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1831
1832         ptr = ref_picture[2] + offset;
1833         if(emu){
1834             ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1835             ptr= s->edge_emu_buffer;
1836         }
1837         pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1838         break;
1839     case MV_TYPE_FIELD:
1840         if (s->picture_structure == PICT_FRAME) {
1841             if(s->quarter_sample){
1842                 /* top field */
1843                 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1844                             ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1845                             1, pix_op, qpix_op,
1846                             s->mv[dir][0][0], s->mv[dir][0][1], 8);
1847                 /* bottom field */
1848                 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1849                             ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1850                             1, pix_op, qpix_op,
1851                             s->mv[dir][1][0], s->mv[dir][1][1], 8);
1852             }else{
1853                 /* top field */       
1854                 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1855                             ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1856                             1, pix_op,
1857                             s->mv[dir][0][0], s->mv[dir][0][1], 8);
1858                 /* bottom field */
1859                 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1860                             ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1861                             1, pix_op,
1862                             s->mv[dir][1][0], s->mv[dir][1][1], 8);
1863             }
1864         } else {
1865             int offset;
1866             if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
1867                 offset= s->field_select[dir][0] ? s->linesize/2 : 0;
1868             }else{
1869                 ref_picture= s->current_picture.data;
1870                 offset= s->field_select[dir][0] ? s->linesize/2 : -s->linesize/2; 
1871             } 
1872
1873             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1874                         ref_picture, offset,
1875                         0, pix_op,
1876                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1877         }
1878         break;
1879     }
1880 }
1881
1882
1883 /* put block[] to dest[] */
1884 static inline void put_dct(MpegEncContext *s, 
1885                            DCTELEM *block, int i, uint8_t *dest, int line_size)
1886 {
1887     s->dct_unquantize(s, block, i, s->qscale);
1888     s->dsp.idct_put (dest, line_size, block);
1889 }
1890
1891 /* add block[] to dest[] */
1892 static inline void add_dct(MpegEncContext *s, 
1893                            DCTELEM *block, int i, uint8_t *dest, int line_size)
1894 {
1895     if (s->block_last_index[i] >= 0) {
1896         s->dsp.idct_add (dest, line_size, block);
1897     }
1898 }
1899
1900 static inline void add_dequant_dct(MpegEncContext *s, 
1901                            DCTELEM *block, int i, uint8_t *dest, int line_size)
1902 {
1903     if (s->block_last_index[i] >= 0) {
1904         s->dct_unquantize(s, block, i, s->qscale);
1905
1906         s->dsp.idct_add (dest, line_size, block);
1907     }
1908 }
1909
1910 /**
1911  * cleans dc, ac, coded_block for the current non intra MB
1912  */
1913 void ff_clean_intra_table_entries(MpegEncContext *s)
1914 {
1915     int wrap = s->block_wrap[0];
1916     int xy = s->block_index[0];
1917     
1918     s->dc_val[0][xy           ] = 
1919     s->dc_val[0][xy + 1       ] = 
1920     s->dc_val[0][xy     + wrap] =
1921     s->dc_val[0][xy + 1 + wrap] = 1024;
1922     /* ac pred */
1923     memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
1924     memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1925     if (s->msmpeg4_version>=3) {
1926         s->coded_block[xy           ] =
1927         s->coded_block[xy + 1       ] =
1928         s->coded_block[xy     + wrap] =
1929         s->coded_block[xy + 1 + wrap] = 0;
1930     }
1931     /* chroma */
1932     wrap = s->block_wrap[4];
1933     xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1934     s->dc_val[1][xy] =
1935     s->dc_val[2][xy] = 1024;
1936     /* ac pred */
1937     memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1938     memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1939     
1940     s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1941 }
1942
1943 /* generic function called after a macroblock has been parsed by the
1944    decoder or after it has been encoded by the encoder.
1945
1946    Important variables used:
1947    s->mb_intra : true if intra macroblock
1948    s->mv_dir   : motion vector direction
1949    s->mv_type  : motion vector type
1950    s->mv       : motion vector
1951    s->interlaced_dct : true if interlaced dct used (mpeg2)
1952  */
1953 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1954 {
1955     int mb_x, mb_y;
1956     const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1957
1958     mb_x = s->mb_x;
1959     mb_y = s->mb_y;
1960
1961     s->current_picture.qscale_table[mb_xy]= s->qscale;
1962
1963     /* update DC predictors for P macroblocks */
1964     if (!s->mb_intra) {
1965         if (s->h263_pred || s->h263_aic) {
1966             if(s->mbintra_table[mb_xy])
1967                 ff_clean_intra_table_entries(s);
1968         } else {
1969             s->last_dc[0] =
1970             s->last_dc[1] =
1971             s->last_dc[2] = 128 << s->intra_dc_precision;
1972         }
1973     }
1974     else if (s->h263_pred || s->h263_aic)
1975         s->mbintra_table[mb_xy]=1;
1976
1977     /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1978     if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1979         //FIXME a lot of thet is only needed for !low_delay
1980         const int wrap = s->block_wrap[0];
1981         const int xy = s->block_index[0];
1982         const int mb_index= s->mb_x + s->mb_y*s->mb_width;
1983         if(s->mv_type == MV_TYPE_8X8){
1984             s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
1985         } else {
1986             int motion_x, motion_y;
1987             if (s->mb_intra) {
1988                 motion_x = 0;
1989                 motion_y = 0;
1990                 if(s->co_located_type_table)
1991                     s->co_located_type_table[mb_index]= 0;
1992             } else if (s->mv_type == MV_TYPE_16X16) {
1993                 motion_x = s->mv[0][0][0];
1994                 motion_y = s->mv[0][0][1];
1995                 if(s->co_located_type_table)
1996                     s->co_located_type_table[mb_index]= 0;
1997             } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1998                 int i;
1999                 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2000                 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2001                 motion_x = (motion_x>>1) | (motion_x&1);
2002                 for(i=0; i<2; i++){
2003                     s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
2004                     s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
2005                     s->field_select_table[mb_index][i]= s->field_select[0][i];
2006                 }
2007                 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
2008             }
2009             /* no update if 8X8 because it has been done during parsing */
2010             s->motion_val[xy][0] = motion_x;
2011             s->motion_val[xy][1] = motion_y;
2012             s->motion_val[xy + 1][0] = motion_x;
2013             s->motion_val[xy + 1][1] = motion_y;
2014             s->motion_val[xy + wrap][0] = motion_x;
2015             s->motion_val[xy + wrap][1] = motion_y;
2016             s->motion_val[xy + 1 + wrap][0] = motion_x;
2017             s->motion_val[xy + 1 + wrap][1] = motion_y;
2018         }
2019     }
2020     
2021     if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2022         uint8_t *dest_y, *dest_cb, *dest_cr;
2023         int dct_linesize, dct_offset;
2024         op_pixels_func (*op_pix)[4];
2025         qpel_mc_func (*op_qpix)[16];
2026
2027         /* avoid copy if macroblock skipped in last frame too */
2028         if (s->pict_type != B_TYPE) {
2029             s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2030         }
2031
2032         /* skip only during decoding as we might trash the buffers during encoding a bit */
2033         if(!s->encoding){
2034             uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2035             const int age= s->current_picture.age;
2036
2037             assert(age);
2038
2039             if (s->mb_skiped) {
2040                 s->mb_skiped= 0;
2041                 assert(s->pict_type!=I_TYPE);
2042  
2043                 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2044                 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2045
2046                 /* if previous was skipped too, then nothing to do !  */
2047                 if (*mbskip_ptr >= age && s->current_picture.reference){
2048                     return;
2049                 }
2050             } else if(!s->current_picture.reference){
2051                 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2052                 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2053             } else{
2054                 *mbskip_ptr = 0; /* not skipped */
2055             }
2056         }else
2057             s->mb_skiped= 0;
2058
2059         if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2060             dest_y  = s->current_picture.data[0] + mb_x * 16;
2061             dest_cb = s->current_picture.data[1] + mb_x * 8;
2062             dest_cr = s->current_picture.data[2] + mb_x * 8;
2063         }else{
2064             dest_y  = s->current_picture.data[0] + (mb_y * 16* s->linesize  ) + mb_x * 16;
2065             dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2066             dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2067         }
2068
2069         if (s->interlaced_dct) {
2070             dct_linesize = s->linesize * 2;
2071             dct_offset = s->linesize;
2072         } else {
2073             dct_linesize = s->linesize;
2074             dct_offset = s->linesize * 8;
2075         }
2076
2077         if (!s->mb_intra) {
2078             /* motion handling */
2079             /* decoding or more than one mb_type (MC was allready done otherwise) */
2080             if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2081                 if ((!s->no_rounding) || s->pict_type==B_TYPE){                
2082                     op_pix = s->dsp.put_pixels_tab;
2083                     op_qpix= s->dsp.put_qpel_pixels_tab;
2084                 }else{
2085                     op_pix = s->dsp.put_no_rnd_pixels_tab;
2086                     op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2087                 }
2088
2089                 if (s->mv_dir & MV_DIR_FORWARD) {
2090                     MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2091                     op_pix = s->dsp.avg_pixels_tab;
2092                     op_qpix= s->dsp.avg_qpel_pixels_tab;
2093                 }
2094                 if (s->mv_dir & MV_DIR_BACKWARD) {
2095                     MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2096                 }
2097             }
2098
2099             /* skip dequant / idct if we are really late ;) */
2100             if(s->hurry_up>1) return;
2101
2102             /* add dct residue */
2103             if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
2104                                 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2105                 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2106                 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2107                 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2108                 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2109
2110                 if(!(s->flags&CODEC_FLAG_GRAY)){
2111                     add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2112                     add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2113                 }
2114             } else if(s->codec_id != CODEC_ID_WMV2){
2115                 add_dct(s, block[0], 0, dest_y, dct_linesize);
2116                 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2117                 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2118                 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2119
2120                 if(!(s->flags&CODEC_FLAG_GRAY)){
2121                     add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2122                     add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2123                 }
2124             } 
2125 #ifdef CONFIG_RISKY
2126             else{
2127                 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2128             }
2129 #endif
2130         } else {
2131             /* dct only in intra block */
2132             if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2133                 put_dct(s, block[0], 0, dest_y, dct_linesize);
2134                 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2135                 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2136                 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2137
2138                 if(!(s->flags&CODEC_FLAG_GRAY)){
2139                     put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2140                     put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2141                 }
2142             }else{
2143                 s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
2144                 s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
2145                 s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
2146                 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2147
2148                 if(!(s->flags&CODEC_FLAG_GRAY)){
2149                     s->dsp.idct_put(dest_cb, s->uvlinesize, block[4]);
2150                     s->dsp.idct_put(dest_cr, s->uvlinesize, block[5]);
2151                 }
2152             }
2153         }
2154     }
2155 }
2156
2157 #ifdef CONFIG_ENCODERS
2158
2159 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2160 {
2161     static const char tab[64]=
2162         {3,2,2,1,1,1,1,1,
2163          1,1,1,1,1,1,1,1,
2164          1,1,1,1,1,1,1,1,
2165          0,0,0,0,0,0,0,0,
2166          0,0,0,0,0,0,0,0,
2167          0,0,0,0,0,0,0,0,
2168          0,0,0,0,0,0,0,0,
2169          0,0,0,0,0,0,0,0};
2170     int score=0;
2171     int run=0;
2172     int i;
2173     DCTELEM *block= s->block[n];
2174     const int last_index= s->block_last_index[n];
2175     int skip_dc;
2176
2177     if(threshold<0){
2178         skip_dc=0;
2179         threshold= -threshold;
2180     }else
2181         skip_dc=1;
2182
2183     /* are all which we could set to zero are allready zero? */
2184     if(last_index<=skip_dc - 1) return;
2185
2186     for(i=0; i<=last_index; i++){
2187         const int j = s->intra_scantable.permutated[i];
2188         const int level = ABS(block[j]);
2189         if(level==1){
2190             if(skip_dc && i==0) continue;
2191             score+= tab[run];
2192             run=0;
2193         }else if(level>1){
2194             return;
2195         }else{
2196             run++;
2197         }
2198     }
2199     if(score >= threshold) return;
2200     for(i=skip_dc; i<=last_index; i++){
2201         const int j = s->intra_scantable.permutated[i];
2202         block[j]=0;
2203     }
2204     if(block[0]) s->block_last_index[n]= 0;
2205     else         s->block_last_index[n]= -1;
2206 }
2207
2208 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2209 {
2210     int i;
2211     const int maxlevel= s->max_qcoeff;
2212     const int minlevel= s->min_qcoeff;
2213     
2214     if(s->mb_intra){
2215         i=1; //skip clipping of intra dc
2216     }else
2217         i=0;
2218     
2219     for(;i<=last_index; i++){
2220         const int j= s->intra_scantable.permutated[i];
2221         int level = block[j];
2222        
2223         if     (level>maxlevel) level=maxlevel;
2224         else if(level<minlevel) level=minlevel;
2225
2226         block[j]= level;
2227     }
2228 }
2229
2230 #if 0
2231 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2232     int score=0;
2233     int x,y;
2234     
2235     for(y=0; y<7; y++){
2236         for(x=0; x<16; x+=4){
2237             score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2238                    +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2239         }
2240         s+= stride;
2241     }
2242     
2243     return score;
2244 }
2245
2246 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2247     int score=0;
2248     int x,y;
2249     
2250     for(y=0; y<7; y++){
2251         for(x=0; x<16; x++){
2252             score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2253         }
2254         s1+= stride;
2255         s2+= stride;
2256     }
2257     
2258     return score;
2259 }
2260 #else
2261 #define SQ(a) ((a)*(a))
2262
2263 static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2264     int score=0;
2265     int x,y;
2266     
2267     for(y=0; y<7; y++){
2268         for(x=0; x<16; x+=4){
2269             score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2270                    +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2271         }
2272         s+= stride;
2273     }
2274     
2275     return score;
2276 }
2277
2278 static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2279     int score=0;
2280     int x,y;
2281     
2282     for(y=0; y<7; y++){
2283         for(x=0; x<16; x++){
2284             score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2285         }
2286         s1+= stride;
2287         s2+= stride;
2288     }
2289     
2290     return score;
2291 }
2292
2293 #endif
2294
2295 #endif //CONFIG_ENCODERS
2296
2297 /**
2298  *
2299  * @param h is the normal height, this will be reduced automatically if needed for the last row
2300  */
2301 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2302     if (    s->avctx->draw_horiz_band 
2303         && (s->last_picture.data[0] || s->low_delay) ) {
2304         uint8_t *src_ptr[3];
2305         int offset;
2306         h= FFMIN(h, s->height - y);
2307
2308         if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
2309             offset = 0;
2310         else
2311             offset = y * s->linesize;
2312
2313         if(s->pict_type==B_TYPE || s->low_delay){
2314             src_ptr[0] = s->current_picture.data[0] + offset;
2315             src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
2316             src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
2317         } else {
2318             src_ptr[0] = s->last_picture.data[0] + offset;
2319             src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
2320             src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
2321         }
2322         emms_c();
2323
2324         s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2325                                y, s->width, h);
2326     }
2327 }
2328
2329 #ifdef CONFIG_ENCODERS
2330
2331 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2332 {
2333     const int mb_x= s->mb_x;
2334     const int mb_y= s->mb_y;
2335     int i;
2336     int skip_dct[6];
2337     int dct_offset   = s->linesize*8; //default for progressive frames
2338     
2339     for(i=0; i<6; i++) skip_dct[i]=0;
2340     
2341     if(s->adaptive_quant){
2342         s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2343
2344         if(s->out_format==FMT_H263){
2345             if     (s->dquant> 2) s->dquant= 2;
2346             else if(s->dquant<-2) s->dquant=-2;
2347         }
2348             
2349         if(s->codec_id==CODEC_ID_MPEG4){        
2350             if(!s->mb_intra){
2351                 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2352
2353                 if(s->mv_dir&MV_DIRECT)
2354                     s->dquant=0;
2355             }
2356         }
2357         s->qscale+= s->dquant;
2358         s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2359         s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2360     }
2361
2362     if (s->mb_intra) {
2363         uint8_t *ptr;
2364         int wrap_y;
2365         int emu=0;
2366
2367         wrap_y = s->linesize;
2368         ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2369
2370         if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2371             ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2372             ptr= s->edge_emu_buffer;
2373             emu=1;
2374         }
2375         
2376         if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2377             int progressive_score, interlaced_score;
2378             
2379             progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2380             interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2381             
2382             if(progressive_score > interlaced_score + 100){
2383                 s->interlaced_dct=1;
2384             
2385                 dct_offset= wrap_y;
2386                 wrap_y<<=1;
2387             }else
2388                 s->interlaced_dct=0;
2389         }
2390         
2391         s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
2392         s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
2393         s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2394         s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2395
2396         if(s->flags&CODEC_FLAG_GRAY){
2397             skip_dct[4]= 1;
2398             skip_dct[5]= 1;
2399         }else{
2400             int wrap_c = s->uvlinesize;
2401             ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2402             if(emu){
2403                 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2404                 ptr= s->edge_emu_buffer;
2405             }
2406             s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2407
2408             ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2409             if(emu){
2410                 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2411                 ptr= s->edge_emu_buffer;
2412             }
2413             s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2414         }
2415     }else{
2416         op_pixels_func (*op_pix)[4];
2417         qpel_mc_func (*op_qpix)[16];
2418         uint8_t *dest_y, *dest_cb, *dest_cr;
2419         uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2420         int wrap_y, wrap_c;
2421         int emu=0;
2422
2423         dest_y  = s->current_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
2424         dest_cb = s->current_picture.data[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2425         dest_cr = s->current_picture.data[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2426         wrap_y = s->linesize;
2427         wrap_c = s->uvlinesize;
2428         ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2429         ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2430         ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2431
2432         if ((!s->no_rounding) || s->pict_type==B_TYPE){
2433             op_pix = s->dsp.put_pixels_tab;
2434             op_qpix= s->dsp.put_qpel_pixels_tab;
2435         }else{
2436             op_pix = s->dsp.put_no_rnd_pixels_tab;
2437             op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2438         }
2439
2440         if (s->mv_dir & MV_DIR_FORWARD) {
2441             MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2442             op_pix = s->dsp.avg_pixels_tab;
2443             op_qpix= s->dsp.avg_qpel_pixels_tab;
2444         }
2445         if (s->mv_dir & MV_DIR_BACKWARD) {
2446             MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2447         }
2448
2449         if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2450             ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2451             ptr_y= s->edge_emu_buffer;
2452             emu=1;
2453         }
2454         
2455         if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2456             int progressive_score, interlaced_score;
2457             
2458             progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
2459                              + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
2460             interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
2461                              + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
2462             
2463             if(progressive_score > interlaced_score + 600){
2464                 s->interlaced_dct=1;
2465             
2466                 dct_offset= wrap_y;
2467                 wrap_y<<=1;
2468             }else
2469                 s->interlaced_dct=0;
2470         }
2471         
2472         s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
2473         s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
2474         s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
2475         s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2476         
2477         if(s->flags&CODEC_FLAG_GRAY){
2478             skip_dct[4]= 1;
2479             skip_dct[5]= 1;
2480         }else{
2481             if(emu){
2482                 ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2483                 ptr_cb= s->edge_emu_buffer;
2484             }
2485             s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2486             if(emu){
2487                 ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2488                 ptr_cr= s->edge_emu_buffer;
2489             }
2490             s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2491         }
2492         /* pre quantization */         
2493         if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2494             //FIXME optimize
2495             if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2496             if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2497             if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2498             if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2499             if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2500             if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2501 #if 0
2502 {
2503  static int stat[7];
2504  int num=0;
2505  for(i=0; i<6; i++)
2506   if(skip_dct[i]) num++;
2507  stat[num]++;
2508  
2509  if(s->mb_x==0 && s->mb_y==0){
2510   for(i=0; i<7; i++){
2511    printf("%6d %1d\n", stat[i], i);
2512   }
2513  }
2514 }
2515 #endif
2516         }
2517
2518     }
2519             
2520 #if 0
2521             {
2522                 float adap_parm;
2523                 
2524                 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2525                             ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2526             
2527                 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
2528                         (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
2529                         s->qscale, adap_parm, s->qscale*adap_parm,
2530                         s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2531             }
2532 #endif
2533     /* DCT & quantize */
2534     if(s->out_format==FMT_MJPEG){
2535         for(i=0;i<6;i++) {
2536             int overflow;
2537             s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2538             if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2539         }
2540     }else{
2541         for(i=0;i<6;i++) {
2542             if(!skip_dct[i]){
2543                 int overflow;
2544                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2545             // FIXME we could decide to change to quantizer instead of clipping
2546             // JS: I don't think that would be a good idea it could lower quality instead
2547             //     of improve it. Just INTRADC clipping deserves changes in quantizer
2548                 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2549             }else
2550                 s->block_last_index[i]= -1;
2551         }
2552         if(s->luma_elim_threshold && !s->mb_intra)
2553             for(i=0; i<4; i++)
2554                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2555         if(s->chroma_elim_threshold && !s->mb_intra)
2556             for(i=4; i<6; i++)
2557                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2558     }
2559
2560     if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2561         s->block_last_index[4]=
2562         s->block_last_index[5]= 0;
2563         s->block[4][0]=
2564         s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
2565     }
2566
2567     /* huffman encode */
2568     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2569     case CODEC_ID_MPEG1VIDEO:
2570         mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
2571 #ifdef CONFIG_RISKY
2572     case CODEC_ID_MPEG4:
2573         mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2574     case CODEC_ID_MSMPEG4V2:
2575     case CODEC_ID_MSMPEG4V3:
2576     case CODEC_ID_WMV1:
2577         msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2578     case CODEC_ID_WMV2:
2579          ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
2580     case CODEC_ID_H263:
2581     case CODEC_ID_H263P:
2582     case CODEC_ID_RV10:
2583         h263_encode_mb(s, s->block, motion_x, motion_y); break;
2584 #endif
2585     case CODEC_ID_MJPEG:
2586         mjpeg_encode_mb(s, s->block); break;
2587     default:
2588         assert(0);
2589     }
2590 }
2591
2592 #endif //CONFIG_ENCODERS
2593
2594 /**
2595  * combines the (truncated) bitstream to a complete frame
2596  * @returns -1 if no complete frame could be created
2597  */
2598 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
2599     ParseContext *pc= &s->parse_context;
2600         
2601     pc->last_index= pc->index;
2602
2603     if(next==-1){
2604         pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2605
2606         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
2607         pc->index += *buf_size;
2608         return -1;
2609     }
2610
2611     if(pc->index){
2612         pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2613
2614         memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
2615         pc->index = 0;
2616         *buf= pc->buffer;
2617         *buf_size= pc->last_index + next;
2618     }
2619
2620     return 0;
2621 }
2622
2623 #ifdef CONFIG_ENCODERS
2624 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
2625 {
2626     int bytes= length>>4;
2627     int bits= length&15;
2628     int i;
2629
2630     if(length==0) return;
2631
2632     for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2633     put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2634 }
2635
2636 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2637     int i;
2638
2639     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2640
2641     /* mpeg1 */
2642     d->mb_incr= s->mb_incr;
2643     for(i=0; i<3; i++)
2644         d->last_dc[i]= s->last_dc[i];
2645     
2646     /* statistics */
2647     d->mv_bits= s->mv_bits;
2648     d->i_tex_bits= s->i_tex_bits;
2649     d->p_tex_bits= s->p_tex_bits;
2650     d->i_count= s->i_count;
2651     d->f_count= s->f_count;
2652     d->b_count= s->b_count;
2653     d->skip_count= s->skip_count;
2654     d->misc_bits= s->misc_bits;
2655     d->last_bits= 0;
2656
2657     d->mb_skiped= s->mb_skiped;
2658     d->qscale= s->qscale;
2659 }
2660
2661 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2662     int i;
2663
2664     memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
2665     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2666     
2667     /* mpeg1 */
2668     d->mb_incr= s->mb_incr;
2669     for(i=0; i<3; i++)
2670         d->last_dc[i]= s->last_dc[i];
2671     
2672     /* statistics */
2673     d->mv_bits= s->mv_bits;
2674     d->i_tex_bits= s->i_tex_bits;
2675     d->p_tex_bits= s->p_tex_bits;
2676     d->i_count= s->i_count;
2677     d->f_count= s->f_count;
2678     d->b_count= s->b_count;
2679     d->skip_count= s->skip_count;
2680     d->misc_bits= s->misc_bits;
2681
2682     d->mb_intra= s->mb_intra;
2683     d->mb_skiped= s->mb_skiped;
2684     d->mv_type= s->mv_type;
2685     d->mv_dir= s->mv_dir;
2686     d->pb= s->pb;
2687     if(s->data_partitioning){
2688         d->pb2= s->pb2;
2689         d->tex_pb= s->tex_pb;
2690     }
2691     d->block= s->block;
2692     for(i=0; i<6; i++)
2693         d->block_last_index[i]= s->block_last_index[i];
2694     d->interlaced_dct= s->interlaced_dct;
2695     d->qscale= s->qscale;
2696 }
2697
2698 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
2699                            PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2700                            int *dmin, int *next_block, int motion_x, int motion_y)
2701 {
2702     int bits_count;
2703     
2704     copy_context_before_encode(s, backup, type);
2705
2706     s->block= s->blocks[*next_block];
2707     s->pb= pb[*next_block];
2708     if(s->data_partitioning){
2709         s->pb2   = pb2   [*next_block];
2710         s->tex_pb= tex_pb[*next_block];
2711     }
2712
2713     encode_mb(s, motion_x, motion_y);
2714
2715     bits_count= get_bit_count(&s->pb);
2716     if(s->data_partitioning){
2717         bits_count+= get_bit_count(&s->pb2);
2718         bits_count+= get_bit_count(&s->tex_pb);
2719     }
2720
2721     if(bits_count<*dmin){
2722         *dmin= bits_count;
2723         *next_block^=1;
2724
2725         copy_context_after_encode(best, s, type);
2726     }
2727 }
2728                 
2729 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2730     uint32_t *sq = squareTbl + 256;
2731     int acc=0;
2732     int x,y;
2733     
2734     if(w==16 && h==16) 
2735         return s->dsp.sse[0](NULL, src1, src2, stride);
2736     else if(w==8 && h==8)
2737         return s->dsp.sse[1](NULL, src1, src2, stride);
2738     
2739     for(y=0; y<h; y++){
2740         for(x=0; x<w; x++){
2741             acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2742         } 
2743     }
2744     
2745     assert(acc>=0);
2746     
2747     return acc;
2748 }
2749
2750 static void encode_picture(MpegEncContext *s, int picture_number)
2751 {
2752     int mb_x, mb_y, pdif = 0;
2753     int i;
2754     int bits;
2755     MpegEncContext best_s, backup_s;
2756     uint8_t bit_buf[2][3000];
2757     uint8_t bit_buf2[2][3000];
2758     uint8_t bit_buf_tex[2][3000];
2759     PutBitContext pb[2], pb2[2], tex_pb[2];
2760
2761     for(i=0; i<2; i++){
2762         init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
2763         init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
2764         init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2765     }
2766
2767     s->picture_number = picture_number;
2768
2769     s->block_wrap[0]=
2770     s->block_wrap[1]=
2771     s->block_wrap[2]=
2772     s->block_wrap[3]= s->mb_width*2 + 2;
2773     s->block_wrap[4]=
2774     s->block_wrap[5]= s->mb_width + 2;
2775     
2776     /* Reset the average MB variance */
2777     s->current_picture.mb_var_sum = 0;
2778     s->current_picture.mc_mb_var_sum = 0;
2779
2780 #ifdef CONFIG_RISKY
2781     /* we need to initialize some time vars before we can encode b-frames */
2782     // RAL: Condition added for MPEG1VIDEO
2783     if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
2784         ff_set_mpeg4_time(s, s->picture_number); 
2785 #endif
2786         
2787     s->scene_change_score=0;
2788     
2789     s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2790     
2791     if(s->pict_type==I_TYPE){
2792         if(s->msmpeg4_version) s->no_rounding=1;
2793         else                   s->no_rounding=0;
2794     }else if(s->pict_type!=B_TYPE){
2795         if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2796             s->no_rounding ^= 1;          
2797     }
2798     
2799     /* Estimate motion for every MB */
2800     s->mb_intra=0; //for the rate distoration & bit compare functions
2801     if(s->pict_type != I_TYPE){
2802         if(s->pict_type != B_TYPE){
2803             if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
2804                 s->me.pre_pass=1;
2805                 s->me.dia_size= s->avctx->pre_dia_size;
2806
2807                 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
2808                     for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
2809                         s->mb_x = mb_x;
2810                         s->mb_y = mb_y;
2811                         ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
2812                     }
2813                 }
2814                 s->me.pre_pass=0;
2815             }
2816         }
2817
2818         s->me.dia_size= s->avctx->dia_size;
2819         for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2820             s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2821             s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2822             s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2823             s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2824             for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2825                 s->mb_x = mb_x;
2826                 s->mb_y = mb_y;
2827                 s->block_index[0]+=2;
2828                 s->block_index[1]+=2;
2829                 s->block_index[2]+=2;
2830                 s->block_index[3]+=2;
2831                 
2832                 /* compute motion vector & mb_type and store in context */
2833                 if(s->pict_type==B_TYPE)
2834                     ff_estimate_b_frame_motion(s, mb_x, mb_y);
2835                 else
2836                     ff_estimate_p_frame_motion(s, mb_x, mb_y);
2837             }
2838         }
2839     }else /* if(s->pict_type == I_TYPE) */{
2840         /* I-Frame */
2841         //FIXME do we need to zero them?
2842         memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2843         memset(s->p_mv_table   , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
2844         memset(s->mb_type      , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2845         
2846         if(!s->fixed_qscale){
2847             /* finding spatial complexity for I-frame rate control */
2848             for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2849                 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2850                     int xx = mb_x * 16;
2851                     int yy = mb_y * 16;
2852                     uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
2853                     int varc;
2854                     int sum = s->dsp.pix_sum(pix, s->linesize);
2855     
2856                     varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
2857
2858                     s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
2859                     s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
2860                     s->current_picture.mb_var_sum    += varc;
2861                 }
2862             }
2863         }
2864     }
2865     emms_c();
2866
2867     if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2868         s->pict_type= I_TYPE;
2869         memset(s->mb_type   , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2870 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2871     }
2872
2873     if(!s->umvplus){
2874         if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
2875             s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2876         
2877             ff_fix_long_p_mvs(s);
2878         }
2879
2880         if(s->pict_type==B_TYPE){
2881             int a, b;
2882
2883             a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2884             b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
2885             s->f_code = FFMAX(a, b);
2886
2887             a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2888             b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
2889             s->b_code = FFMAX(a, b);
2890
2891             ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2892             ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2893             ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2894             ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2895         }
2896     }
2897     
2898     if (s->fixed_qscale) 
2899         s->frame_qscale = s->current_picture.quality;
2900     else
2901         s->frame_qscale = ff_rate_estimate_qscale(s);
2902
2903     if(s->adaptive_quant){
2904 #ifdef CONFIG_RISKY
2905         switch(s->codec_id){
2906         case CODEC_ID_MPEG4:
2907             ff_clean_mpeg4_qscales(s);
2908             break;
2909         case CODEC_ID_H263:
2910         case CODEC_ID_H263P:
2911             ff_clean_h263_qscales(s);
2912             break;
2913         }
2914 #endif
2915
2916         s->qscale= s->current_picture.qscale_table[0];
2917     }else
2918         s->qscale= (int)(s->frame_qscale + 0.5);
2919         
2920     if (s->out_format == FMT_MJPEG) {
2921         /* for mjpeg, we do include qscale in the matrix */
2922         s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2923         for(i=1;i<64;i++){
2924             int j= s->dsp.idct_permutation[i];
2925
2926             s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2927         }
2928         convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, 
2929                        s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
2930     }
2931     
2932     //FIXME var duplication
2933     s->current_picture.key_frame= s->pict_type == I_TYPE;
2934     s->current_picture.pict_type= s->pict_type;
2935
2936     if(s->current_picture.key_frame)
2937         s->picture_in_gop_number=0;
2938
2939     s->last_bits= get_bit_count(&s->pb);
2940     switch(s->out_format) {
2941     case FMT_MJPEG:
2942         mjpeg_picture_header(s);
2943         break;
2944 #ifdef CONFIG_RISKY
2945     case FMT_H263:
2946         if (s->codec_id == CODEC_ID_WMV2) 
2947             ff_wmv2_encode_picture_header(s, picture_number);
2948         else if (s->h263_msmpeg4) 
2949             msmpeg4_encode_picture_header(s, picture_number);
2950         else if (s->h263_pred)
2951             mpeg4_encode_picture_header(s, picture_number);
2952         else if (s->h263_rv10) 
2953             rv10_encode_picture_header(s, picture_number);
2954         else
2955             h263_encode_picture_header(s, picture_number);
2956         break;
2957 #endif
2958     case FMT_MPEG1:
2959         mpeg1_encode_picture_header(s, picture_number);
2960         break;
2961     }
2962     bits= get_bit_count(&s->pb);
2963     s->header_bits= bits - s->last_bits;
2964     s->last_bits= bits;
2965     s->mv_bits=0;
2966     s->misc_bits=0;
2967     s->i_tex_bits=0;
2968     s->p_tex_bits=0;
2969     s->i_count=0;
2970     s->f_count=0;
2971     s->b_count=0;
2972     s->skip_count=0;
2973
2974     for(i=0; i<3; i++){
2975         /* init last dc values */
2976         /* note: quant matrix value (8) is implied here */
2977         s->last_dc[i] = 128;
2978         
2979         s->current_picture.error[i] = 0;
2980     }
2981     s->mb_incr = 1;
2982     s->last_mv[0][0][0] = 0;
2983     s->last_mv[0][0][1] = 0;
2984     s->last_mv[1][0][0] = 0;
2985     s->last_mv[1][0][1] = 0;
2986      
2987     s->last_mv_dir = 0;
2988
2989 #ifdef CONFIG_RISKY
2990     if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
2991         s->gob_index = ff_h263_get_gob_height(s);
2992
2993     if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2994         ff_mpeg4_init_partitions(s);
2995 #endif
2996
2997     s->resync_mb_x=0;
2998     s->resync_mb_y=0;
2999     s->first_slice_line = 1;
3000     s->ptr_lastgob = s->pb.buf;
3001     s->ptr_last_mb_line = s->pb.buf;
3002     for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3003         s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3004         s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3005         
3006         s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3007         s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3008         s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3009         s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3010         s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
3011         s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
3012         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3013             int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
3014             const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
3015 //            int d;
3016             int dmin=10000000;
3017
3018             s->mb_x = mb_x;
3019             s->mb_y = mb_y;
3020             s->block_index[0]+=2;
3021             s->block_index[1]+=2;
3022             s->block_index[2]+=2;
3023             s->block_index[3]+=2;
3024             s->block_index[4]++;
3025             s->block_index[5]++;
3026
3027             /* write gob / video packet header  */
3028 #ifdef CONFIG_RISKY
3029             if(s->rtp_mode){
3030                 int current_packet_size, is_gob_start;
3031                 
3032                 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3033                 is_gob_start=0;
3034                 
3035                 if(s->codec_id==CODEC_ID_MPEG4){
3036                     if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
3037                        && s->mb_y + s->mb_x>0){
3038
3039                         if(s->partitioned_frame){
3040                             ff_mpeg4_merge_partitions(s);
3041                             ff_mpeg4_init_partitions(s);
3042                         }
3043                         ff_mpeg4_encode_video_packet_header(s);
3044
3045                         if(s->flags&CODEC_FLAG_PASS1){
3046                             int bits= get_bit_count(&s->pb);
3047                             s->misc_bits+= bits - s->last_bits;
3048                             s->last_bits= bits;
3049                         }
3050                         ff_mpeg4_clean_buffers(s);
3051                         is_gob_start=1;
3052                     }
3053                 }else{
3054                     if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
3055                        && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3056                        
3057                         h263_encode_gob_header(s, mb_y);                       
3058                         is_gob_start=1;
3059                     }
3060                 }
3061
3062                 if(is_gob_start){
3063                     s->ptr_lastgob = pbBufPtr(&s->pb);
3064                     s->first_slice_line=1;
3065                     s->resync_mb_x=mb_x;
3066                     s->resync_mb_y=mb_y;
3067                 }
3068             }
3069 #endif
3070
3071             if(  (s->resync_mb_x   == s->mb_x)
3072                && s->resync_mb_y+1 == s->mb_y){
3073                 s->first_slice_line=0; 
3074             }
3075
3076             if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3077                 int next_block=0;
3078                 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3079
3080                 copy_context_before_encode(&backup_s, s, -1);
3081                 backup_s.pb= s->pb;
3082                 best_s.data_partitioning= s->data_partitioning;
3083                 best_s.partitioned_frame= s->partitioned_frame;
3084                 if(s->data_partitioning){
3085                     backup_s.pb2= s->pb2;
3086                     backup_s.tex_pb= s->tex_pb;
3087                 }
3088
3089                 if(mb_type&MB_TYPE_INTER){
3090                     s->mv_dir = MV_DIR_FORWARD;
3091                     s->mv_type = MV_TYPE_16X16;
3092                     s->mb_intra= 0;
3093                     s->mv[0][0][0] = s->p_mv_table[xy][0];
3094                     s->mv[0][0][1] = s->p_mv_table[xy][1];
3095                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
3096                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3097                 }
3098                 if(mb_type&MB_TYPE_INTER4V){                 
3099                     s->mv_dir = MV_DIR_FORWARD;
3100                     s->mv_type = MV_TYPE_8X8;
3101                     s->mb_intra= 0;
3102                     for(i=0; i<4; i++){
3103                         s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3104                         s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3105                     }
3106                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
3107                                  &dmin, &next_block, 0, 0);
3108                 }
3109                 if(mb_type&MB_TYPE_FORWARD){
3110                     s->mv_dir = MV_DIR_FORWARD;
3111                     s->mv_type = MV_TYPE_16X16;
3112                     s->mb_intra= 0;
3113                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3114                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3115                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
3116                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3117                 }
3118                 if(mb_type&MB_TYPE_BACKWARD){
3119                     s->mv_dir = MV_DIR_BACKWARD;
3120                     s->mv_type = MV_TYPE_16X16;
3121                     s->mb_intra= 0;
3122                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3123                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3124                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
3125                                  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3126                 }
3127                 if(mb_type&MB_TYPE_BIDIR){
3128                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3129                     s->mv_type = MV_TYPE_16X16;
3130                     s->mb_intra= 0;
3131                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3132                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3133                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3134                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3135                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
3136                                  &dmin, &next_block, 0, 0);
3137                 }
3138                 if(mb_type&MB_TYPE_DIRECT){
3139                     int mx= s->b_direct_mv_table[xy][0];
3140                     int my= s->b_direct_mv_table[xy][1];
3141                     
3142                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3143                     s->mb_intra= 0;
3144 #ifdef CONFIG_RISKY
3145                     ff_mpeg4_set_direct_mv(s, mx, my);
3146 #endif
3147                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
3148                                  &dmin, &next_block, mx, my);
3149                 }
3150                 if(mb_type&MB_TYPE_INTRA){
3151                     s->mv_dir = 0;
3152                     s->mv_type = MV_TYPE_16X16;
3153                     s->mb_intra= 1;
3154                     s->mv[0][0][0] = 0;
3155                     s->mv[0][0][1] = 0;
3156                     encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
3157                                  &dmin, &next_block, 0, 0);
3158                     /* force cleaning of ac/dc pred stuff if needed ... */
3159                     if(s->h263_pred || s->h263_aic)
3160                         s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
3161                 }
3162                 copy_context_after_encode(s, &best_s, -1);
3163                 
3164                 pb_bits_count= get_bit_count(&s->pb);
3165                 flush_put_bits(&s->pb);
3166                 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3167                 s->pb= backup_s.pb;
3168                 
3169                 if(s->data_partitioning){
3170                     pb2_bits_count= get_bit_count(&s->pb2);
3171                     flush_put_bits(&s->pb2);
3172                     ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3173                     s->pb2= backup_s.pb2;
3174                     
3175                     tex_pb_bits_count= get_bit_count(&s->tex_pb);
3176                     flush_put_bits(&s->tex_pb);
3177                     ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3178                     s->tex_pb= backup_s.tex_pb;
3179                 }
3180                 s->last_bits= get_bit_count(&s->pb);
3181             } else {
3182                 int motion_x, motion_y;
3183                 int intra_score;
3184                 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
3185                 
3186               if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
3187                 /* get luma score */
3188                 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3189                     intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_width]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3190                 }else{
3191                     uint8_t *dest_y;
3192
3193                     int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
3194                     mean*= 0x01010101;
3195                     
3196                     dest_y  = s->new_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
3197                 
3198                     for(i=0; i<16; i++){
3199                         *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3200                         *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3201                         *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3202                         *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3203                     }
3204
3205                     s->mb_intra=1;
3206                     intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3207                                         
3208 /*                    printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8, 
3209                         s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
3210                         s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
3211                 }
3212                 
3213                 /* get chroma score */
3214                 if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3215                     int i;
3216                     
3217                     s->mb_intra=1;
3218                     for(i=1; i<3; i++){
3219                         uint8_t *dest_c;
3220                         int mean;
3221                         
3222                         if(s->out_format == FMT_H263){
3223                             mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3224                         }else{
3225                             mean= (s->last_dc[i] + 4)>>3;
3226                         }
3227                         dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
3228                         
3229                         mean*= 0x01010101;
3230                         for(i=0; i<8; i++){
3231                             *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3232                             *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3233                         }
3234                         
3235                         intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3236                     }                
3237                 }
3238
3239                 /* bias */
3240                 switch(s->avctx->mb_cmp&0xFF){
3241                 default:
3242                 case FF_CMP_SAD:
3243                     intra_score+= 32*s->qscale;
3244                     break;
3245                 case FF_CMP_SSE:
3246                     intra_score+= 24*s->qscale*s->qscale;
3247                     break;
3248                 case FF_CMP_SATD:
3249                     intra_score+= 96*s->qscale;
3250                     break;
3251                 case FF_CMP_DCT:
3252                     intra_score+= 48*s->qscale;
3253                     break;
3254                 case FF_CMP_BIT:
3255                     intra_score+= 16;
3256                     break;
3257                 case FF_CMP_PSNR:
3258                 case FF_CMP_RD:
3259                     intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
3260                     break;
3261                 }
3262
3263                 if(intra_score < inter_score)
3264                     mb_type= MB_TYPE_INTRA;
3265               }  
3266                 
3267                 s->mv_type=MV_TYPE_16X16;
3268                 // only one MB-Type possible
3269                 
3270                 switch(mb_type){
3271                 case MB_TYPE_INTRA:
3272                     s->mv_dir = 0;
3273                     s->mb_intra= 1;
3274                     motion_x= s->mv[0][0][0] = 0;
3275                     motion_y= s->mv[0][0][1] = 0;
3276                     break;
3277                 case MB_TYPE_INTER:
3278                     s->mv_dir = MV_DIR_FORWARD;
3279                     s->mb_intra= 0;
3280                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3281                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3282                     break;
3283                 case MB_TYPE_INTER4V:
3284                     s->mv_dir = MV_DIR_FORWARD;
3285                     s->mv_type = MV_TYPE_8X8;
3286                     s->mb_intra= 0;
3287                     for(i=0; i<4; i++){
3288                         s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3289                         s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3290                     }
3291                     motion_x= motion_y= 0;
3292                     break;
3293                 case MB_TYPE_DIRECT:
3294                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3295                     s->mb_intra= 0;
3296                     motion_x=s->b_direct_mv_table[xy][0];
3297                     motion_y=s->b_direct_mv_table[xy][1];
3298 #ifdef CONFIG_RISKY
3299                     ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3300 #endif
3301                     break;
3302                 case MB_TYPE_BIDIR:
3303                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3304                     s->mb_intra= 0;
3305                     motion_x=0;
3306                     motion_y=0;
3307                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3308                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3309                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3310                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3311                     break;
3312                 case MB_TYPE_BACKWARD:
3313                     s->mv_dir = MV_DIR_BACKWARD;
3314                     s->mb_intra= 0;
3315                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3316                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3317                     break;
3318                 case MB_TYPE_FORWARD:
3319                     s->mv_dir = MV_DIR_FORWARD;
3320                     s->mb_intra= 0;
3321                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3322                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3323 //                    printf(" %d %d ", motion_x, motion_y);
3324                     break;
3325                 default:
3326                     motion_x=motion_y=0; //gcc warning fix
3327                     printf("illegal MB type\n");
3328                 }
3329
3330                 encode_mb(s, motion_x, motion_y);
3331
3332                 // RAL: Update last macrobloc type
3333                 s->last_mv_dir = s->mv_dir;
3334             }
3335
3336             /* clean the MV table in IPS frames for direct mode in B frames */
3337             if(s->mb_intra /* && I,P,S_TYPE */){
3338                 s->p_mv_table[xy][0]=0;
3339                 s->p_mv_table[xy][1]=0;
3340             }
3341
3342             MPV_decode_mb(s, s->block);
3343             
3344             if(s->flags&CODEC_FLAG_PSNR){
3345                 int w= 16;
3346                 int h= 16;
3347
3348                 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3349                 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3350
3351                 s->current_picture.error[0] += sse(
3352                     s,
3353                     s->new_picture    .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3354                     s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3355                     w, h, s->linesize);
3356                 s->current_picture.error[1] += sse(
3357                     s,
3358                     s->new_picture    .data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3359                     s->current_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3360                     w>>1, h>>1, s->uvlinesize);
3361                 s->current_picture.error[2] += sse(
3362                     s,
3363                     s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3364                     s->current_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3365                     w>>1, h>>1, s->uvlinesize);
3366             }
3367 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
3368         }
3369
3370
3371         /* Obtain average mb_row size for RTP */
3372         if (s->rtp_mode) {
3373             if (mb_y==0)
3374                 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
3375             else {    
3376                 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
3377             }
3378             s->ptr_last_mb_line = pbBufPtr(&s->pb);
3379         }
3380     }
3381     emms_c();
3382
3383 #ifdef CONFIG_RISKY
3384     if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3385         ff_mpeg4_merge_partitions(s);
3386
3387     if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3388         msmpeg4_encode_ext_header(s);
3389
3390     if(s->codec_id==CODEC_ID_MPEG4) 
3391         ff_mpeg4_stuffing(&s->pb);
3392 #endif
3393
3394     //if (s->gob_number)
3395     //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3396     
3397     /* Send the last GOB if RTP */    
3398     if (s->rtp_mode) {
3399         flush_put_bits(&s->pb);
3400         pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3401         /* Call the RTP callback to send the last GOB */
3402         if (s->rtp_callback)
3403             s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3404         s->ptr_lastgob = pbBufPtr(&s->pb);
3405         //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3406     }
3407 }
3408
3409 static int dct_quantize_trellis_c(MpegEncContext *s, 
3410                         DCTELEM *block, int n,
3411                         int qscale, int *overflow){
3412     const int *qmat;
3413     const uint8_t *scantable= s->intra_scantable.scantable;
3414     int max=0;
3415     unsigned int threshold1, threshold2;
3416     int bias=0;
3417     int run_tab[65];
3418     int level_tab[65];
3419     int score_tab[65];
3420     int last_run=0;
3421     int last_level=0;
3422     int last_score= 0;
3423     int last_i= 0;
3424     int coeff[3][64];
3425     int coeff_count[64];
3426     int lambda, qmul, qadd, start_i, last_non_zero, i;
3427     const int esc_length= s->ac_esc_length;
3428     uint8_t * length;
3429     uint8_t * last_length;
3430     int score_limit=0;
3431     int left_limit= 0;
3432         
3433     s->dsp.fdct (block);
3434
3435     qmul= qscale*16;
3436     qadd= ((qscale-1)|1)*8;
3437
3438     if (s->mb_intra) {
3439         int q;
3440         if (!s->h263_aic) {
3441             if (n < 4)
3442                 q = s->y_dc_scale;
3443             else
3444                 q = s->c_dc_scale;
3445             q = q << 3;
3446         } else{
3447             /* For AIC we skip quant/dequant of INTRADC */
3448             q = 1 << 3;
3449             qadd=0;
3450         }
3451             
3452         /* note: block[0] is assumed to be positive */
3453         block[0] = (block[0] + (q >> 1)) / q;
3454         start_i = 1;
3455         last_non_zero = 0;
3456         qmat = s->q_intra_matrix[qscale];
3457         if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
3458             bias= 1<<(QMAT_SHIFT-1);
3459         length     = s->intra_ac_vlc_length;
3460         last_length= s->intra_ac_vlc_last_length;
3461     } else {
3462         start_i = 0;
3463         last_non_zero = -1;
3464         qmat = s->q_inter_matrix[qscale];
3465         length     = s->inter_ac_vlc_length;
3466         last_length= s->inter_ac_vlc_last_length;
3467     }
3468
3469     threshold1= (1<<QMAT_SHIFT) - bias - 1;
3470     threshold2= (threshold1<<1);
3471
3472     for(i=start_i; i<64; i++) {
3473         const int j = scantable[i];
3474         const int k= i-start_i;
3475         int level = block[j];
3476         level = level * qmat[j];
3477
3478 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3479 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3480         if(((unsigned)(level+threshold1))>threshold2){
3481             if(level>0){
3482                 level= (bias + level)>>QMAT_SHIFT;
3483                 coef