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