Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 9bc8b386

History | View | Annotate | Download (172 KB)

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 <limits.h>
28
#include "avcodec.h"
29
#include "dsputil.h"
30
#include "mpegvideo.h"
31
#include "faandct.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
static int sse_mb(MpegEncContext *s);
54
#endif //CONFIG_ENCODERS
55

    
56
#ifdef HAVE_XVMC
57
extern int  XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
58
extern void XVMC_field_end(MpegEncContext *s);
59
extern void XVMC_decode_mb(MpegEncContext *s);
60
#endif
61

    
62
void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
63

    
64

    
65
/* enable all paranoid tests for rounding, overflows, etc... */
66
//#define PARANOID
67

    
68
//#define DEBUG
69

    
70

    
71
/* for jpeg fast DCT */
72
#define CONST_BITS 14
73

    
74
static const uint16_t aanscales[64] = {
75
    /* precomputed values scaled up by 14 bits */
76
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
77
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
78
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
79
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
80
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
81
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
82
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
83
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
84
};
85

    
86
static const uint8_t h263_chroma_roundtab[16] = {
87
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
88
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
89
};
90

    
91
#ifdef CONFIG_ENCODERS
92
static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
93
static uint8_t default_fcode_tab[MAX_MV*2+1];
94

    
95
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
96

    
97
static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
98
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax)
99
{
100
    int qscale;
101

    
102
    for(qscale=qmin; qscale<=qmax; qscale++){
103
        int i;
104
        if (dsp->fdct == ff_jpeg_fdct_islow 
105
#ifdef FAAN_POSTSCALE
106
            || dsp->fdct == ff_faandct
107
#endif
108
            ) {
109
            for(i=0;i<64;i++) {
110
                const int j= dsp->idct_permutation[i];
111
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
112
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
113
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
115
                
116
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
117
                                (qscale * quant_matrix[j]));
118
            }
119
        } else if (dsp->fdct == fdct_ifast
120
#ifndef FAAN_POSTSCALE
121
                   || dsp->fdct == ff_faandct
122
#endif
123
                   ) {
124
            for(i=0;i<64;i++) {
125
                const int j= dsp->idct_permutation[i];
126
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
127
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
128
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
129
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
130
                
131
                qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 
132
                                (aanscales[i] * qscale * quant_matrix[j]));
133
            }
134
        } else {
135
            for(i=0;i<64;i++) {
136
                const int j= dsp->idct_permutation[i];
137
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
138
                   So 16           <= qscale * quant_matrix[i]             <= 7905
139
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
140
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
141
                */
142
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
143
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
144
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
145

    
146
                if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
147
                qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
148
            }
149
        }
150
    }
151
}
152

    
153
static inline void update_qscale(MpegEncContext *s){
154
    s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
155
    s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
156
    
157
    s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
158
}
159
#endif //CONFIG_ENCODERS
160

    
161
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
162
    int i;
163
    int end;
164
    
165
    st->scantable= src_scantable;
166

    
167
    for(i=0; i<64; i++){
168
        int j;
169
        j = src_scantable[i];
170
        st->permutated[i] = permutation[j];
171
#ifdef ARCH_POWERPC
172
        st->inverse[j] = i;
173
#endif
174
    }
175
    
176
    end=-1;
177
    for(i=0; i<64; i++){
178
        int j;
179
        j = st->permutated[i];
180
        if(j>end) end=j;
181
        st->raster_end[i]= end;
182
    }
183
}
184

    
185
#ifdef CONFIG_ENCODERS
186
void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
187
    int i;
188

    
189
    if(matrix){
190
        put_bits(pb, 1, 1);
191
        for(i=0;i<64;i++) {
192
            put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
193
        }
194
    }else
195
        put_bits(pb, 1, 0);
196
}
197
#endif //CONFIG_ENCODERS
198

    
199
/* init common dct for both encoder and decoder */
200
int DCT_common_init(MpegEncContext *s)
201
{
202
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
203
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
204
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
205

    
206
#ifdef CONFIG_ENCODERS
207
    s->dct_quantize= dct_quantize_c;
208
#endif
209
        
210
#ifdef HAVE_MMX
211
    MPV_common_init_mmx(s);
212
#endif
213
#ifdef ARCH_ALPHA
214
    MPV_common_init_axp(s);
215
#endif
216
#ifdef HAVE_MLIB
217
    MPV_common_init_mlib(s);
218
#endif
219
#ifdef HAVE_MMI
220
    MPV_common_init_mmi(s);
221
#endif
222
#ifdef ARCH_ARMV4L
223
    MPV_common_init_armv4l(s);
224
#endif
225
#ifdef ARCH_POWERPC
226
    MPV_common_init_ppc(s);
227
#endif
228

    
229
#ifdef CONFIG_ENCODERS
230
    s->fast_dct_quantize= s->dct_quantize;
231

    
232
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
233
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
234
    }
235

    
236
#endif //CONFIG_ENCODERS
237

    
238
    /* load & permutate scantables
239
       note: only wmv uses differnt ones 
240
    */
241
    ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
242
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
243
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
244
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
245

    
246
    s->picture_structure= PICT_FRAME;
247
    
248
    return 0;
249
}
250

    
251
static void copy_picture(Picture *dst, Picture *src){
252
    *dst = *src;
253
    dst->type= FF_BUFFER_TYPE_COPY;
254
}
255

    
256
/**
257
 * allocates a Picture
258
 * The pixels are allocated/set by calling get_buffer() if shared=0
259
 */
260
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
261
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
262
    const int mb_array_size= s->mb_stride*s->mb_height;
263
    int i;
264
    
265
    if(shared){
266
        assert(pic->data[0]);
267
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
268
        pic->type= FF_BUFFER_TYPE_SHARED;
269
    }else{
270
        int r;
271
        
272
        assert(!pic->data[0]);
273
        
274
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
275
        
276
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
277
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
278
            return -1;
279
        }
280

    
281
        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
282
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
283
            return -1;
284
        }
285

    
286
        if(pic->linesize[1] != pic->linesize[2]){
287
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
288
            return -1;
289
        }
290

    
291
        s->linesize  = pic->linesize[0];
292
        s->uvlinesize= pic->linesize[1];
293
    }
294
    
295
    if(pic->qscale_table==NULL){
296
        if (s->encoding) {        
297
            CHECKED_ALLOCZ(pic->mb_var   , mb_array_size * sizeof(int16_t))
298
            CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
299
            CHECKED_ALLOCZ(pic->mb_mean  , mb_array_size * sizeof(int8_t))
300
            CHECKED_ALLOCZ(pic->mb_cmp_score, mb_array_size * sizeof(int32_t))
301
        }
302

    
303
        CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
304
        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
305
        CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(int))
306
        pic->mb_type= pic->mb_type_base + s->mb_stride+1;
307
        if(s->out_format == FMT_H264){
308
            for(i=0; i<2; i++){
309
                CHECKED_ALLOCZ(pic->motion_val[i], 2 * 16 * s->mb_num * sizeof(uint16_t))
310
                CHECKED_ALLOCZ(pic->ref_index[i] , 4 * s->mb_num * sizeof(uint8_t))
311
            }
312
        }
313
        pic->qstride= s->mb_stride;
314
        CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
315
    }
316

    
317
    //it might be nicer if the application would keep track of these but it would require a API change
318
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
319
    s->prev_pict_types[0]= s->pict_type;
320
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
321
        pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
322
    
323
    return 0;
324
fail: //for the CHECKED_ALLOCZ macro
325
    return -1;
326
}
327

    
328
/**
329
 * deallocates a picture
330
 */
331
static void free_picture(MpegEncContext *s, Picture *pic){
332
    int i;
333

    
334
    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
335
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
336
    }
337

    
338
    av_freep(&pic->mb_var);
339
    av_freep(&pic->mc_mb_var);
340
    av_freep(&pic->mb_mean);
341
    av_freep(&pic->mb_cmp_score);
342
    av_freep(&pic->mbskip_table);
343
    av_freep(&pic->qscale_table);
344
    av_freep(&pic->mb_type_base);
345
    av_freep(&pic->pan_scan);
346
    pic->mb_type= NULL;
347
    for(i=0; i<2; i++){
348
        av_freep(&pic->motion_val[i]);
349
        av_freep(&pic->ref_index[i]);
350
    }
351
    
352
    if(pic->type == FF_BUFFER_TYPE_SHARED){
353
        for(i=0; i<4; i++){
354
            pic->base[i]=
355
            pic->data[i]= NULL;
356
        }
357
        pic->type= 0;        
358
    }
359
}
360

    
361
/* init common structure for both encoder and decoder */
362
int MPV_common_init(MpegEncContext *s)
363
{
364
    int y_size, c_size, yc_size, i, mb_array_size, x, y;
365

    
366
    dsputil_init(&s->dsp, s->avctx);
367
    DCT_common_init(s);
368

    
369
    s->flags= s->avctx->flags;
370

    
371
    s->mb_width  = (s->width  + 15) / 16;
372
    s->mb_height = (s->height + 15) / 16;
373
    s->mb_stride = s->mb_width + 1;
374
    mb_array_size= s->mb_height * s->mb_stride;
375

    
376
    /* set default edge pos, will be overriden in decode_header if needed */
377
    s->h_edge_pos= s->mb_width*16;
378
    s->v_edge_pos= s->mb_height*16;
379

    
380
    s->mb_num = s->mb_width * s->mb_height;
381
    
382
    s->block_wrap[0]=
383
    s->block_wrap[1]=
384
    s->block_wrap[2]=
385
    s->block_wrap[3]= s->mb_width*2 + 2;
386
    s->block_wrap[4]=
387
    s->block_wrap[5]= s->mb_width + 2;
388

    
389
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
390
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
391
    yc_size = y_size + 2 * c_size;
392

    
393
    /* convert fourcc to upper case */
394
    s->avctx->codec_tag=   toupper( s->avctx->codec_tag     &0xFF)          
395
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
396
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 
397
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
398

    
399
    s->avctx->stream_codec_tag=   toupper( s->avctx->stream_codec_tag     &0xFF)          
400
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
401
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16) 
402
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
403

    
404
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
405
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
406

    
407
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
408

    
409
    CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
410
    for(y=0; y<s->mb_height; y++){
411
        for(x=0; x<s->mb_width; x++){
412
            s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
413
        }
414
    }
415
    s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
416
    
417
    if (s->encoding) {
418
        int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
419

    
420
        /* Allocate MV tables */
421
        CHECKED_ALLOCZ(s->p_mv_table_base            , mv_table_size * 2 * sizeof(int16_t))
422
        CHECKED_ALLOCZ(s->b_forw_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
423
        CHECKED_ALLOCZ(s->b_back_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
424
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
425
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
426
        CHECKED_ALLOCZ(s->b_direct_mv_table_base     , mv_table_size * 2 * sizeof(int16_t))
427
        s->p_mv_table           = s->p_mv_table_base            + s->mb_stride + 1;
428
        s->b_forw_mv_table      = s->b_forw_mv_table_base       + s->mb_stride + 1;
429
        s->b_back_mv_table      = s->b_back_mv_table_base       + s->mb_stride + 1;
430
        s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
431
        s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
432
        s->b_direct_mv_table    = s->b_direct_mv_table_base     + s->mb_stride + 1;
433

    
434
        //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
435
        CHECKED_ALLOCZ(s->me.scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
436
        
437
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
438
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
439

    
440
        if(s->codec_id==CODEC_ID_MPEG4){
441
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
442
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
443
        }
444
        
445
        if(s->msmpeg4_version){
446
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
447
        }
448
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
449

    
450
        /* Allocate MB type table */
451
        CHECKED_ALLOCZ(s->mb_type  , mb_array_size * sizeof(uint8_t)) //needed for encoding
452
        
453
        CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
454
        
455
        CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
456
        CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
457
        CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
458
        CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
459
        CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
460
        CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
461
        
462
        if(s->avctx->noise_reduction){
463
            CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
464
            CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
465
        }
466
    }
467
    CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
468
        
469
    CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
470

    
471
    CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
472
    
473
    if (s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_VIS_MV)) {
474
        int size;
475

    
476
        /* MV prediction */
477
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
478
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
479
    }
480

    
481
    if(s->codec_id==CODEC_ID_MPEG4){
482
        /* interlaced direct mode decoding tables */
483
        CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
484
        CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
485
    }
486
    if (s->out_format == FMT_H263) {
487
        /* ac values */
488
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
489
        s->ac_val[1] = s->ac_val[0] + y_size;
490
        s->ac_val[2] = s->ac_val[1] + c_size;
491
        
492
        /* cbp values */
493
        CHECKED_ALLOCZ(s->coded_block, y_size);
494
        
495
        /* divx501 bitstream reorder buffer */
496
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
497

    
498
        /* cbp, ac_pred, pred_dir */
499
        CHECKED_ALLOCZ(s->cbp_table  , mb_array_size * sizeof(uint8_t))
500
        CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
501
    }
502
    
503
    if (s->h263_pred || s->h263_plus || !s->encoding) {
504
        /* dc values */
505
        //MN: we need these for error resilience of intra-frames
506
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
507
        s->dc_val[1] = s->dc_val[0] + y_size;
508
        s->dc_val[2] = s->dc_val[1] + c_size;
509
        for(i=0;i<yc_size;i++)
510
            s->dc_val[0][i] = 1024;
511
    }
512

    
513
    /* which mb is a intra block */
514
    CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
515
    memset(s->mbintra_table, 1, mb_array_size);
516
    
517
    /* default structure is frame */
518
    s->picture_structure = PICT_FRAME;
519
    
520
    /* init macroblock skip table */
521
    CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
522
    //Note the +1 is for a quicker mpeg4 slice_end detection
523
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
524
    
525
    s->block= s->blocks[0];
526

    
527
    for(i=0;i<12;i++){
528
        s->pblocks[i] = (short *)(&s->block[i]);
529
    }
530

    
531
    s->parse_context.state= -1;
532

    
533
    s->context_initialized = 1;
534
    return 0;
535
 fail:
536
    MPV_common_end(s);
537
    return -1;
538
}
539

    
540

    
541
//extern int sads;
542

    
543
/* init common structure for both encoder and decoder */
544
void MPV_common_end(MpegEncContext *s)
545
{
546
    int i;
547

    
548
    av_freep(&s->parse_context.buffer);
549
    s->parse_context.buffer_size=0;
550

    
551
    av_freep(&s->mb_type);
552
    av_freep(&s->p_mv_table_base);
553
    av_freep(&s->b_forw_mv_table_base);
554
    av_freep(&s->b_back_mv_table_base);
555
    av_freep(&s->b_bidir_forw_mv_table_base);
556
    av_freep(&s->b_bidir_back_mv_table_base);
557
    av_freep(&s->b_direct_mv_table_base);
558
    s->p_mv_table= NULL;
559
    s->b_forw_mv_table= NULL;
560
    s->b_back_mv_table= NULL;
561
    s->b_bidir_forw_mv_table= NULL;
562
    s->b_bidir_back_mv_table= NULL;
563
    s->b_direct_mv_table= NULL;
564
    
565
    av_freep(&s->motion_val);
566
    av_freep(&s->dc_val[0]);
567
    av_freep(&s->ac_val[0]);
568
    av_freep(&s->coded_block);
569
    av_freep(&s->mbintra_table);
570
    av_freep(&s->cbp_table);
571
    av_freep(&s->pred_dir_table);
572
    av_freep(&s->me.scratchpad);
573
    av_freep(&s->me.map);
574
    av_freep(&s->me.score_map);
575
    
576
    av_freep(&s->mbskip_table);
577
    av_freep(&s->prev_pict_types);
578
    av_freep(&s->bitstream_buffer);
579
    av_freep(&s->tex_pb_buffer);
580
    av_freep(&s->pb2_buffer);
581
    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
582
    av_freep(&s->field_mv_table);
583
    av_freep(&s->field_select_table);
584
    av_freep(&s->avctx->stats_out);
585
    av_freep(&s->ac_stats);
586
    av_freep(&s->error_status_table);
587
    av_freep(&s->mb_index2xy);
588
    av_freep(&s->lambda_table);
589
    av_freep(&s->q_intra_matrix);
590
    av_freep(&s->q_inter_matrix);
591
    av_freep(&s->q_intra_matrix16);
592
    av_freep(&s->q_inter_matrix16);
593
    av_freep(&s->blocks);
594
    av_freep(&s->input_picture);
595
    av_freep(&s->reordered_input_picture);
596
    av_freep(&s->dct_error_sum);
597
    av_freep(&s->dct_offset);
598

    
599
    if(s->picture){
600
        for(i=0; i<MAX_PICTURE_COUNT; i++){
601
            free_picture(s, &s->picture[i]);
602
        }
603
    }
604
    av_freep(&s->picture);
605
    avcodec_default_free_buffers(s->avctx);
606
    s->context_initialized = 0;
607
    s->last_picture_ptr=
608
    s->next_picture_ptr=
609
    s->current_picture_ptr= NULL;
610
}
611

    
612
#ifdef CONFIG_ENCODERS
613

    
614
/* init video encoder */
615
int MPV_encode_init(AVCodecContext *avctx)
616
{
617
    MpegEncContext *s = avctx->priv_data;
618
    int i, dummy;
619
    int chroma_h_shift, chroma_v_shift;
620

    
621
    avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
622

    
623
    s->bit_rate = avctx->bit_rate;
624
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
625
    s->width = avctx->width;
626
    s->height = avctx->height;
627
    if(avctx->gop_size > 600){
628
        av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
629
        avctx->gop_size=600;
630
    }
631
    s->gop_size = avctx->gop_size;
632
    s->rtp_mode = avctx->rtp_mode;
633
    s->rtp_payload_size = avctx->rtp_payload_size;
634
    if (avctx->rtp_callback)
635
        s->rtp_callback = avctx->rtp_callback;
636
    s->max_qdiff= avctx->max_qdiff;
637
    s->qcompress= avctx->qcompress;
638
    s->qblur= avctx->qblur;
639
    s->avctx = avctx;
640
    s->flags= avctx->flags;
641
    s->max_b_frames= avctx->max_b_frames;
642
    s->b_frame_strategy= avctx->b_frame_strategy;
643
    s->codec_id= avctx->codec->id;
644
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
645
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
646
    s->strict_std_compliance= avctx->strict_std_compliance;
647
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
648
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
649
    s->mpeg_quant= avctx->mpeg_quant;
650

    
651
    if (s->gop_size <= 1) {
652
        s->intra_only = 1;
653
        s->gop_size = 12;
654
    } else {
655
        s->intra_only = 0;
656
    }
657

    
658
    s->me_method = avctx->me_method;
659

    
660
    /* Fixed QSCALE */
661
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
662
    
663
    s->adaptive_quant= (   s->avctx->lumi_masking
664
                        || s->avctx->dark_masking
665
                        || s->avctx->temporal_cplx_masking 
666
                        || s->avctx->spatial_cplx_masking
667
                        || s->avctx->p_masking
668
                        || (s->flags&CODEC_FLAG_QP_RD))
669
                       && !s->fixed_qscale;
670
    
671
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
672

    
673
    if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4){
674
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
675
        return -1;
676
    }
677
    
678
    if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
679
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
680
        return -1;
681
    }
682

    
683
    if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
684
        av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
685
        return -1;
686
    }
687
    
688
    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
689
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
690
        return -1;
691
    }
692
    
693
    if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
694
        av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
695
        return -1;
696
    }
697
        
698
    if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
699
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
700
        return -1;
701
    }
702

    
703
    if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
704
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
705
        return -1;
706
    }
707
    
708
    if(s->codec_id==CODEC_ID_MJPEG){
709
        s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
710
        s->inter_quant_bias= 0;
711
    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
712
        s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
713
        s->inter_quant_bias= 0;
714
    }else{
715
        s->intra_quant_bias=0;
716
        s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
717
    }
718
    
719
    if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
720
        s->intra_quant_bias= avctx->intra_quant_bias;
721
    if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
722
        s->inter_quant_bias= avctx->inter_quant_bias;
723
        
724
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
725

    
726
    av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
727
    s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
728

    
729
    switch(avctx->codec->id) {
730
    case CODEC_ID_MPEG1VIDEO:
731
        s->out_format = FMT_MPEG1;
732
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
733
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
734
        break;
735
    case CODEC_ID_MPEG2VIDEO:
736
        s->out_format = FMT_MPEG1;
737
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
738
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
739
        s->rtp_mode= 1; // mpeg2 must have slices
740
        if(s->rtp_payload_size == 0) s->rtp_payload_size= 256*256*256;
741
        break;
742
    case CODEC_ID_LJPEG:
743
    case CODEC_ID_MJPEG:
744
        s->out_format = FMT_MJPEG;
745
        s->intra_only = 1; /* force intra only for jpeg */
746
        s->mjpeg_write_tables = 1; /* write all tables */
747
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
748
        s->mjpeg_vsample[0] = 1<<chroma_v_shift;
749
        s->mjpeg_vsample[1] = 1;
750
        s->mjpeg_vsample[2] = 1; 
751
        s->mjpeg_hsample[0] = 1<<chroma_h_shift;
752
        s->mjpeg_hsample[1] = 1; 
753
        s->mjpeg_hsample[2] = 1; 
754
        if (mjpeg_init(s) < 0)
755
            return -1;
756
        avctx->delay=0;
757
        s->low_delay=1;
758
        break;
759
#ifdef CONFIG_RISKY
760
    case CODEC_ID_H263:
761
        if (h263_get_picture_format(s->width, s->height) == 7) {
762
            av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
763
            return -1;
764
        }
765
        s->out_format = FMT_H263;
766
        avctx->delay=0;
767
        s->low_delay=1;
768
        break;
769
    case CODEC_ID_H263P:
770
        s->out_format = FMT_H263;
771
        s->h263_plus = 1;
772
        /* Fx */
773
        s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
774
        s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
775
        /* /Fx */
776
        /* These are just to be sure */
777
        s->umvplus = 1;
778
        avctx->delay=0;
779
        s->low_delay=1;
780
        break;
781
    case CODEC_ID_FLV1:
782
        s->out_format = FMT_H263;
783
        s->h263_flv = 2; /* format = 1; 11-bit codes */
784
        s->unrestricted_mv = 1;
785
        s->rtp_mode=0; /* don't allow GOB */
786
        avctx->delay=0;
787
        s->low_delay=1;
788
        break;
789
    case CODEC_ID_RV10:
790
        s->out_format = FMT_H263;
791
        s->h263_rv10 = 1;
792
        avctx->delay=0;
793
        s->low_delay=1;
794
        break;
795
    case CODEC_ID_MPEG4:
796
        s->out_format = FMT_H263;
797
        s->h263_pred = 1;
798
        s->unrestricted_mv = 1;
799
        s->low_delay= s->max_b_frames ? 0 : 1;
800
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
801
        break;
802
    case CODEC_ID_MSMPEG4V1:
803
        s->out_format = FMT_H263;
804
        s->h263_msmpeg4 = 1;
805
        s->h263_pred = 1;
806
        s->unrestricted_mv = 1;
807
        s->msmpeg4_version= 1;
808
        avctx->delay=0;
809
        s->low_delay=1;
810
        break;
811
    case CODEC_ID_MSMPEG4V2:
812
        s->out_format = FMT_H263;
813
        s->h263_msmpeg4 = 1;
814
        s->h263_pred = 1;
815
        s->unrestricted_mv = 1;
816
        s->msmpeg4_version= 2;
817
        avctx->delay=0;
818
        s->low_delay=1;
819
        break;
820
    case CODEC_ID_MSMPEG4V3:
821
        s->out_format = FMT_H263;
822
        s->h263_msmpeg4 = 1;
823
        s->h263_pred = 1;
824
        s->unrestricted_mv = 1;
825
        s->msmpeg4_version= 3;
826
        s->flipflop_rounding=1;
827
        avctx->delay=0;
828
        s->low_delay=1;
829
        break;
830
    case CODEC_ID_WMV1:
831
        s->out_format = FMT_H263;
832
        s->h263_msmpeg4 = 1;
833
        s->h263_pred = 1;
834
        s->unrestricted_mv = 1;
835
        s->msmpeg4_version= 4;
836
        s->flipflop_rounding=1;
837
        avctx->delay=0;
838
        s->low_delay=1;
839
        break;
840
    case CODEC_ID_WMV2:
841
        s->out_format = FMT_H263;
842
        s->h263_msmpeg4 = 1;
843
        s->h263_pred = 1;
844
        s->unrestricted_mv = 1;
845
        s->msmpeg4_version= 5;
846
        s->flipflop_rounding=1;
847
        avctx->delay=0;
848
        s->low_delay=1;
849
        break;
850
#endif
851
    default:
852
        return -1;
853
    }
854
    
855
    { /* set up some save defaults, some codecs might override them later */
856
        static int done=0;
857
        if(!done){
858
            int i;
859
            done=1;
860

    
861
            default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
862
            memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
863
            memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
864

    
865
            for(i=-16; i<16; i++){
866
                default_fcode_tab[i + MAX_MV]= 1;
867
            }
868
        }
869
    }
870
    s->me.mv_penalty= default_mv_penalty;
871
    s->fcode_tab= default_fcode_tab;
872
    s->y_dc_scale_table=
873
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
874
 
875
    /* dont use mv_penalty table for crap MV as it would be confused */
876
    //FIXME remove after fixing / removing old ME
877
    if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
878

    
879
    s->encoding = 1;
880

    
881
    /* init */
882
    if (MPV_common_init(s) < 0)
883
        return -1;
884
    
885
    ff_init_me(s);
886

    
887
#ifdef CONFIG_ENCODERS
888
#ifdef CONFIG_RISKY
889
    if (s->out_format == FMT_H263)
890
        h263_encode_init(s);
891
    if(s->msmpeg4_version)
892
        ff_msmpeg4_encode_init(s);
893
#endif
894
    if (s->out_format == FMT_MPEG1)
895
        ff_mpeg1_encode_init(s);
896
#endif
897

    
898
    /* init default q matrix */
899
    for(i=0;i<64;i++) {
900
        int j= s->dsp.idct_permutation[i];
901
#ifdef CONFIG_RISKY
902
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
903
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
904
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
905
        }else if(s->out_format == FMT_H263){
906
            s->intra_matrix[j] =
907
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
908
        }else
909
#endif
910
        { /* mpeg1/2 */
911
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
912
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
913
        }
914
        if(s->avctx->intra_matrix)
915
            s->intra_matrix[j] = s->avctx->intra_matrix[i];
916
        if(s->avctx->inter_matrix)
917
            s->inter_matrix[j] = s->avctx->inter_matrix[i];
918
    }
919

    
920
    /* precompute matrix */
921
    /* for mjpeg, we do include qscale in the matrix */
922
    if (s->out_format != FMT_MJPEG) {
923
        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 
924
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
925
        convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, 
926
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
927
    }
928

    
929
    if(ff_rate_control_init(s) < 0)
930
        return -1;
931

    
932
    s->picture_number = 0;
933
    s->picture_in_gop_number = 0;
934
    s->fake_picture_number = 0;
935
    /* motion detector init */
936
    s->f_code = 1;
937
    s->b_code = 1;
938

    
939
    return 0;
940
}
941

    
942
int MPV_encode_end(AVCodecContext *avctx)
943
{
944
    MpegEncContext *s = avctx->priv_data;
945

    
946
#ifdef STATS
947
    print_stats();
948
#endif
949

    
950
    ff_rate_control_uninit(s);
951

    
952
    MPV_common_end(s);
953
    if (s->out_format == FMT_MJPEG)
954
        mjpeg_close(s);
955

    
956
    av_freep(&avctx->extradata);
957
      
958
    return 0;
959
}
960

    
961
#endif //CONFIG_ENCODERS
962

    
963
void init_rl(RLTable *rl)
964
{
965
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
966
    uint8_t index_run[MAX_RUN+1];
967
    int last, run, level, start, end, i;
968

    
969
    /* compute max_level[], max_run[] and index_run[] */
970
    for(last=0;last<2;last++) {
971
        if (last == 0) {
972
            start = 0;
973
            end = rl->last;
974
        } else {
975
            start = rl->last;
976
            end = rl->n;
977
        }
978

    
979
        memset(max_level, 0, MAX_RUN + 1);
980
        memset(max_run, 0, MAX_LEVEL + 1);
981
        memset(index_run, rl->n, MAX_RUN + 1);
982
        for(i=start;i<end;i++) {
983
            run = rl->table_run[i];
984
            level = rl->table_level[i];
985
            if (index_run[run] == rl->n)
986
                index_run[run] = i;
987
            if (level > max_level[run])
988
                max_level[run] = level;
989
            if (run > max_run[level])
990
                max_run[level] = run;
991
        }
992
        rl->max_level[last] = av_malloc(MAX_RUN + 1);
993
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
994
        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
995
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
996
        rl->index_run[last] = av_malloc(MAX_RUN + 1);
997
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
998
    }
999
}
1000

    
1001
/* draw the edges of width 'w' of an image of size width, height */
1002
//FIXME check that this is ok for mpeg4 interlaced
1003
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
1004
{
1005
    uint8_t *ptr, *last_line;
1006
    int i;
1007

    
1008
    last_line = buf + (height - 1) * wrap;
1009
    for(i=0;i<w;i++) {
1010
        /* top and bottom */
1011
        memcpy(buf - (i + 1) * wrap, buf, width);
1012
        memcpy(last_line + (i + 1) * wrap, last_line, width);
1013
    }
1014
    /* left and right */
1015
    ptr = buf;
1016
    for(i=0;i<height;i++) {
1017
        memset(ptr - w, ptr[0], w);
1018
        memset(ptr + width, ptr[width-1], w);
1019
        ptr += wrap;
1020
    }
1021
    /* corners */
1022
    for(i=0;i<w;i++) {
1023
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1024
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1025
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1026
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1027
    }
1028
}
1029

    
1030
int ff_find_unused_picture(MpegEncContext *s, int shared){
1031
    int i;
1032
    
1033
    if(shared){
1034
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1035
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
1036
        }
1037
    }else{
1038
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1039
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
1040
        }
1041
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1042
            if(s->picture[i].data[0]==NULL) return i;
1043
        }
1044
    }
1045

    
1046
    assert(0);
1047
    return -1;
1048
}
1049

    
1050
static void update_noise_reduction(MpegEncContext *s){
1051
    int intra, i;
1052

    
1053
    for(intra=0; intra<2; intra++){
1054
        if(s->dct_count[intra] > (1<<16)){
1055
            for(i=0; i<64; i++){
1056
                s->dct_error_sum[intra][i] >>=1;
1057
            }
1058
            s->dct_count[intra] >>= 1;
1059
        }
1060
        
1061
        for(i=0; i<64; i++){
1062
            s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1063
        }
1064
    }
1065
}
1066

    
1067
/**
1068
 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1069
 */
1070
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1071
{
1072
    int i;
1073
    AVFrame *pic;
1074
    s->mb_skiped = 0;
1075

    
1076
    assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
1077

    
1078
    /* mark&release old frames */
1079
    if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
1080
        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1081

    
1082
        /* release forgotten pictures */
1083
        /* if(mpeg124/h263) */
1084
        if(!s->encoding){
1085
            for(i=0; i<MAX_PICTURE_COUNT; i++){
1086
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1087
                    av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
1088
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
1089
                }
1090
            }
1091
        }
1092
    }
1093
alloc:
1094
    if(!s->encoding){
1095
        /* release non refernce frames */
1096
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1097
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1098
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1099
            }
1100
        }
1101

    
1102
        if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1103
            pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1104
        else{
1105
            i= ff_find_unused_picture(s, 0);
1106
            pic= (AVFrame*)&s->picture[i];
1107
        }
1108

    
1109
        pic->reference= s->pict_type != B_TYPE ? 3 : 0;
1110

    
1111
        if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
1112
            pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
1113
        
1114
        if( alloc_picture(s, (Picture*)pic, 0) < 0)
1115
            return -1;
1116

    
1117
        s->current_picture_ptr= (Picture*)pic;
1118
    }
1119

    
1120
    s->current_picture_ptr->pict_type= s->pict_type;
1121
//    if(s->flags && CODEC_FLAG_QSCALE) 
1122
  //      s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1123
    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1124

    
1125
    copy_picture(&s->current_picture, s->current_picture_ptr);
1126
  
1127
  if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1128
    if (s->pict_type != B_TYPE) {
1129
        s->last_picture_ptr= s->next_picture_ptr;
1130
        s->next_picture_ptr= s->current_picture_ptr;
1131
    }
1132
    
1133
    if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1134
    if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1135
    
1136
    if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1137
        av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
1138
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1139
        goto alloc;
1140
    }
1141

    
1142
    assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1143

    
1144
    if(s->picture_structure!=PICT_FRAME){
1145
        int i;
1146
        for(i=0; i<4; i++){
1147
            if(s->picture_structure == PICT_BOTTOM_FIELD){
1148
                 s->current_picture.data[i] += s->current_picture.linesize[i];
1149
            } 
1150
            s->current_picture.linesize[i] *= 2;
1151
            s->last_picture.linesize[i] *=2;
1152
            s->next_picture.linesize[i] *=2;
1153
        }
1154
    }
1155
  }
1156
   
1157
    s->hurry_up= s->avctx->hurry_up;
1158
    s->error_resilience= avctx->error_resilience;
1159

    
1160
    /* set dequantizer, we cant do it during init as it might change for mpeg4
1161
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1162
    if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) 
1163
        s->dct_unquantize = s->dct_unquantize_mpeg2;
1164
    else if(s->out_format == FMT_H263)
1165
        s->dct_unquantize = s->dct_unquantize_h263;
1166
    else 
1167
        s->dct_unquantize = s->dct_unquantize_mpeg1;
1168

    
1169
    if(s->dct_error_sum){
1170
        assert(s->avctx->noise_reduction && s->encoding);
1171

    
1172
        update_noise_reduction(s);
1173
    }
1174
        
1175
#ifdef HAVE_XVMC
1176
    if(s->avctx->xvmc_acceleration)
1177
        return XVMC_field_start(s, avctx);
1178
#endif
1179
    return 0;
1180
}
1181

    
1182
/* generic function for encode/decode called after a frame has been coded/decoded */
1183
void MPV_frame_end(MpegEncContext *s)
1184
{
1185
    int i;
1186
    /* draw edge for correct motion prediction if outside */
1187
#ifdef HAVE_XVMC
1188
//just to make sure that all data is rendered.
1189
    if(s->avctx->xvmc_acceleration){
1190
        XVMC_field_end(s);
1191
    }else
1192
#endif
1193
    if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1194
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
1195
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1196
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1197
    }
1198
    emms_c();
1199
    
1200
    s->last_pict_type    = s->pict_type;
1201
    if(s->pict_type!=B_TYPE){
1202
        s->last_non_b_pict_type= s->pict_type;
1203
    }
1204
#if 0
1205
        /* copy back current_picture variables */
1206
    for(i=0; i<MAX_PICTURE_COUNT; i++){
1207
        if(s->picture[i].data[0] == s->current_picture.data[0]){
1208
            s->picture[i]= s->current_picture;
1209
            break;
1210
        }    
1211
    }
1212
    assert(i<MAX_PICTURE_COUNT);
1213
#endif    
1214

    
1215
    if(s->encoding){
1216
        /* release non refernce frames */
1217
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1218
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1219
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1220
            }
1221
        }
1222
    }
1223
    // clear copies, to avoid confusion
1224
#if 0
1225
    memset(&s->last_picture, 0, sizeof(Picture));
1226
    memset(&s->next_picture, 0, sizeof(Picture));
1227
    memset(&s->current_picture, 0, sizeof(Picture));
1228
#endif
1229
}
1230

    
1231
/**
1232
 * draws an line from (ex, ey) -> (sx, sy).
1233
 * @param w width of the image
1234
 * @param h height of the image
1235
 * @param stride stride/linesize of the image
1236
 * @param color color of the arrow
1237
 */
1238
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1239
    int t, x, y, f;
1240
    
1241
    sx= clip(sx, 0, w-1);
1242
    sy= clip(sy, 0, h-1);
1243
    ex= clip(ex, 0, w-1);
1244
    ey= clip(ey, 0, h-1);
1245
    
1246
    buf[sy*stride + sx]+= color;
1247
    
1248
    if(ABS(ex - sx) > ABS(ey - sy)){
1249
        if(sx > ex){
1250
            t=sx; sx=ex; ex=t;
1251
            t=sy; sy=ey; ey=t;
1252
        }
1253
        buf+= sx + sy*stride;
1254
        ex-= sx;
1255
        f= ((ey-sy)<<16)/ex;
1256
        for(x= 0; x <= ex; x++){
1257
            y= ((x*f) + (1<<15))>>16;
1258
            buf[y*stride + x]+= color;
1259
        }
1260
    }else{
1261
        if(sy > ey){
1262
            t=sx; sx=ex; ex=t;
1263
            t=sy; sy=ey; ey=t;
1264
        }
1265
        buf+= sx + sy*stride;
1266
        ey-= sy;
1267
        if(ey) f= ((ex-sx)<<16)/ey;
1268
        else   f= 0;
1269
        for(y= 0; y <= ey; y++){
1270
            x= ((y*f) + (1<<15))>>16;
1271
            buf[y*stride + x]+= color;
1272
        }
1273
    }
1274
}
1275

    
1276
/**
1277
 * draws an arrow from (ex, ey) -> (sx, sy).
1278
 * @param w width of the image
1279
 * @param h height of the image
1280
 * @param stride stride/linesize of the image
1281
 * @param color color of the arrow
1282
 */
1283
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ 
1284
    int dx,dy;
1285

    
1286
    sx= clip(sx, -100, w+100);
1287
    sy= clip(sy, -100, h+100);
1288
    ex= clip(ex, -100, w+100);
1289
    ey= clip(ey, -100, h+100);
1290
    
1291
    dx= ex - sx;
1292
    dy= ey - sy;
1293
    
1294
    if(dx*dx + dy*dy > 3*3){
1295
        int rx=  dx + dy;
1296
        int ry= -dx + dy;
1297
        int length= ff_sqrt((rx*rx + ry*ry)<<8);
1298
        
1299
        //FIXME subpixel accuracy
1300
        rx= ROUNDED_DIV(rx*3<<4, length);
1301
        ry= ROUNDED_DIV(ry*3<<4, length);
1302
        
1303
        draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1304
        draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1305
    }
1306
    draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1307
}
1308

    
1309
/**
1310
 * prints debuging info for the given picture.
1311
 */
1312
void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1313

    
1314
    if(!pict || !pict->mb_type) return;
1315

    
1316
    if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1317
        int x,y;
1318

    
1319
        for(y=0; y<s->mb_height; y++){
1320
            for(x=0; x<s->mb_width; x++){
1321
                if(s->avctx->debug&FF_DEBUG_SKIP){
1322
                    int count= s->mbskip_table[x + y*s->mb_stride];
1323
                    if(count>9) count=9;
1324
                    av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1325
                }
1326
                if(s->avctx->debug&FF_DEBUG_QP){
1327
                    av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
1328
                }
1329
                if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1330
                    int mb_type= pict->mb_type[x + y*s->mb_stride];
1331
                    
1332
                    //Type & MV direction
1333
                    if(IS_PCM(mb_type))
1334
                        av_log(s->avctx, AV_LOG_DEBUG, "P");
1335
                    else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1336
                        av_log(s->avctx, AV_LOG_DEBUG, "A");
1337
                    else if(IS_INTRA4x4(mb_type))
1338
                        av_log(s->avctx, AV_LOG_DEBUG, "i");
1339
                    else if(IS_INTRA16x16(mb_type))
1340
                        av_log(s->avctx, AV_LOG_DEBUG, "I");
1341
                    else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1342
                        av_log(s->avctx, AV_LOG_DEBUG, "d");
1343
                    else if(IS_DIRECT(mb_type))
1344
                        av_log(s->avctx, AV_LOG_DEBUG, "D");
1345
                    else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1346
                        av_log(s->avctx, AV_LOG_DEBUG, "g");
1347
                    else if(IS_GMC(mb_type))
1348
                        av_log(s->avctx, AV_LOG_DEBUG, "G");
1349
                    else if(IS_SKIP(mb_type))
1350
                        av_log(s->avctx, AV_LOG_DEBUG, "S");
1351
                    else if(!USES_LIST(mb_type, 1))
1352
                        av_log(s->avctx, AV_LOG_DEBUG, ">");
1353
                    else if(!USES_LIST(mb_type, 0))
1354
                        av_log(s->avctx, AV_LOG_DEBUG, "<");
1355
                    else{
1356
                        assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1357
                        av_log(s->avctx, AV_LOG_DEBUG, "X");
1358
                    }
1359
                    
1360
                    //segmentation
1361
                    if(IS_8X8(mb_type))
1362
                        av_log(s->avctx, AV_LOG_DEBUG, "+");
1363
                    else if(IS_16X8(mb_type))
1364
                        av_log(s->avctx, AV_LOG_DEBUG, "-");
1365
                    else if(IS_8X16(mb_type))
1366
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1367
                    else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1368
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1369
                    else
1370
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1371
                    
1372
                        
1373
                    if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1374
                        av_log(s->avctx, AV_LOG_DEBUG, "=");
1375
                    else
1376
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1377
                }
1378
//                av_log(s->avctx, AV_LOG_DEBUG, " ");
1379
            }
1380
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
1381
        }
1382
    }
1383
    
1384
    if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
1385
        const int shift= 1 + s->quarter_sample;
1386
        int mb_y;
1387
        uint8_t *ptr= pict->data[0];
1388
        s->low_delay=0; //needed to see the vectors without trashing the buffers
1389

    
1390
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
1391
            int mb_x;
1392
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
1393
                const int mb_index= mb_x + mb_y*s->mb_stride;
1394
                if(IS_8X8(s->current_picture.mb_type[mb_index])){
1395
                    int i;
1396
                    for(i=0; i<4; i++){
1397
                        int sx= mb_x*16 + 4 + 8*(i&1);
1398
                        int sy= mb_y*16 + 4 + 8*(i>>1);
1399
                        int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1400
                        int mx= (s->motion_val[xy][0]>>shift) + sx;
1401
                        int my= (s->motion_val[xy][1]>>shift) + sy;
1402
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1403
                    }
1404
                }else if(IS_16X8(s->current_picture.mb_type[mb_index])){
1405
                    int i;
1406
                    for(i=0; i<2; i++){
1407
                        int sx=mb_x*16 + 8;
1408
                        int sy=mb_y*16 + 4 + 8*i;
1409
                        int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
1410
                        int mx=(s->motion_val[xy][0]>>shift) + sx;
1411
                        int my=(s->motion_val[xy][1]>>shift) + sy;
1412
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1413
                    }
1414
                }else{
1415
                    int sx= mb_x*16 + 8;
1416
                    int sy= mb_y*16 + 8;
1417
                    int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1418
                    int mx= (s->motion_val[xy][0]>>shift) + sx;
1419
                    int my= (s->motion_val[xy][1]>>shift) + sy;
1420
                    draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1421
                }
1422
                s->mbskip_table[mb_index]=0;
1423
            }
1424
        }
1425
    }
1426
}
1427

    
1428
#ifdef CONFIG_ENCODERS
1429

    
1430
static int get_sae(uint8_t *src, int ref, int stride){
1431
    int x,y;
1432
    int acc=0;
1433
    
1434
    for(y=0; y<16; y++){
1435
        for(x=0; x<16; x++){
1436
            acc+= ABS(src[x+y*stride] - ref);
1437
        }
1438
    }
1439
    
1440
    return acc;
1441
}
1442

    
1443
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1444
    int x, y, w, h;
1445
    int acc=0;
1446
    
1447
    w= s->width &~15;
1448
    h= s->height&~15;
1449
    
1450
    for(y=0; y<h; y+=16){
1451
        for(x=0; x<w; x+=16){
1452
            int offset= x + y*stride;
1453
            int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1454
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1455
            int sae = get_sae(src + offset, mean, stride);
1456
            
1457
            acc+= sae + 500 < sad;
1458
        }
1459
    }
1460
    return acc;
1461
}
1462

    
1463

    
1464
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1465
    AVFrame *pic=NULL;
1466
    int i;
1467
    const int encoding_delay= s->max_b_frames;
1468
    int direct=1;
1469
    
1470
  if(pic_arg){
1471
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1472
    if(pic_arg->linesize[0] != s->linesize) direct=0;
1473
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1474
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1475
  
1476
//    av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1477
    
1478
    if(direct){
1479
        i= ff_find_unused_picture(s, 1);
1480

    
1481
        pic= (AVFrame*)&s->picture[i];
1482
        pic->reference= 3;
1483
    
1484
        for(i=0; i<4; i++){
1485
            pic->data[i]= pic_arg->data[i];
1486
            pic->linesize[i]= pic_arg->linesize[i];
1487
        }
1488
        alloc_picture(s, (Picture*)pic, 1);
1489
    }else{
1490
        int offset= 16;
1491
        i= ff_find_unused_picture(s, 0);
1492

    
1493
        pic= (AVFrame*)&s->picture[i];
1494
        pic->reference= 3;
1495

    
1496
        alloc_picture(s, (Picture*)pic, 0);
1497

    
1498
        if(   pic->data[0] + offset == pic_arg->data[0] 
1499
           && pic->data[1] + offset == pic_arg->data[1]
1500
           && pic->data[2] + offset == pic_arg->data[2]){
1501
       // empty
1502
        }else{
1503
            int h_chroma_shift, v_chroma_shift;
1504
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1505
        
1506
            for(i=0; i<3; i++){
1507
                int src_stride= pic_arg->linesize[i];
1508
                int dst_stride= i ? s->uvlinesize : s->linesize;
1509
                int h_shift= i ? h_chroma_shift : 0;
1510
                int v_shift= i ? v_chroma_shift : 0;
1511
                int w= s->width >>h_shift;
1512
                int h= s->height>>v_shift;
1513
                uint8_t *src= pic_arg->data[i];
1514
                uint8_t *dst= pic->data[i] + offset;
1515
            
1516
                if(src_stride==dst_stride)
1517
                    memcpy(dst, src, src_stride*h);
1518
                else{
1519
                    while(h--){
1520
                        memcpy(dst, src, w);
1521
                        dst += dst_stride;
1522
                        src += src_stride;
1523
                    }
1524
                }
1525
            }
1526
        }
1527
    }
1528
    pic->quality= pic_arg->quality;
1529
    pic->pict_type= pic_arg->pict_type;
1530
    pic->pts = pic_arg->pts;
1531
    
1532
    if(s->input_picture[encoding_delay])
1533
        pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1534
    
1535
  }
1536

    
1537
    /* shift buffer entries */
1538
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1539
        s->input_picture[i-1]= s->input_picture[i];
1540
        
1541
    s->input_picture[encoding_delay]= (Picture*)pic;
1542

    
1543
    return 0;
1544
}
1545

    
1546
static void select_input_picture(MpegEncContext *s){
1547
    int i;
1548
    int coded_pic_num=0;    
1549

    
1550
    if(s->reordered_input_picture[0])
1551
        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1552

    
1553
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1554
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1555
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1556

    
1557
    /* set next picture types & ordering */
1558
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1559
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1560
            s->reordered_input_picture[0]= s->input_picture[0];
1561
            s->reordered_input_picture[0]->pict_type= I_TYPE;
1562
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1563
        }else{
1564
            int b_frames;
1565
            
1566
            if(s->flags&CODEC_FLAG_PASS2){
1567
                for(i=0; i<s->max_b_frames+1; i++){
1568
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1569
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1570
                    s->input_picture[i]->pict_type= pict_type;
1571
                    
1572
                    if(i + 1 >= s->rc_context.num_entries) break;
1573
                }
1574
            }
1575

    
1576
            if(s->input_picture[0]->pict_type){
1577
                /* user selected pict_type */
1578
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1579
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1580
                }
1581
            
1582
                if(b_frames > s->max_b_frames){
1583
                    av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
1584
                    b_frames = s->max_b_frames;
1585
                }
1586
            }else if(s->b_frame_strategy==0){
1587
                b_frames= s->max_b_frames;
1588
                while(b_frames && !s->input_picture[b_frames]) b_frames--;
1589
            }else if(s->b_frame_strategy==1){
1590
                for(i=1; i<s->max_b_frames+1; i++){
1591
                    if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1592
                        s->input_picture[i]->b_frame_score= 
1593
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
1594
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1595
                    }
1596
                }
1597
                for(i=0; i<s->max_b_frames; i++){
1598
                    if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1599
                }
1600
                                
1601
                b_frames= FFMAX(0, i-1);
1602
                
1603
                /* reset scores */
1604
                for(i=0; i<b_frames+1; i++){
1605
                    s->input_picture[i]->b_frame_score=0;
1606
                }
1607
            }else{
1608
                av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1609
                b_frames=0;
1610
            }
1611

    
1612
            emms_c();
1613
//static int b_count=0;
1614
//b_count+= b_frames;
1615
//av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1616
                        
1617
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1618
            if(   s->picture_in_gop_number + b_frames >= s->gop_size 
1619
               || s->reordered_input_picture[0]->pict_type== I_TYPE)
1620
                s->reordered_input_picture[0]->pict_type= I_TYPE;
1621
            else
1622
                s->reordered_input_picture[0]->pict_type= P_TYPE;
1623
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1624
            for(i=0; i<b_frames; i++){
1625
                coded_pic_num++;
1626
                s->reordered_input_picture[i+1]= s->input_picture[i];
1627
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1628
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1629
            }
1630
        }
1631
    }
1632
    
1633
    if(s->reordered_input_picture[0]){
1634
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1635

    
1636
        copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1637

    
1638
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1639
            // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1640
        
1641
            int i= ff_find_unused_picture(s, 0);
1642
            Picture *pic= &s->picture[i];
1643

    
1644
            /* mark us unused / free shared pic */
1645
            for(i=0; i<4; i++)
1646
                s->reordered_input_picture[0]->data[i]= NULL;
1647
            s->reordered_input_picture[0]->type= 0;
1648
            
1649
            //FIXME bad, copy * except
1650
            pic->pict_type = s->reordered_input_picture[0]->pict_type;
1651
            pic->quality   = s->reordered_input_picture[0]->quality;
1652
            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1653
            pic->reference = s->reordered_input_picture[0]->reference;
1654
            pic->pts = s->reordered_input_picture[0]->pts;
1655
            
1656
            alloc_picture(s, pic, 0);
1657

    
1658
            s->current_picture_ptr= pic;
1659
        }else{
1660
            // input is not a shared pix -> reuse buffer for current_pix
1661

    
1662
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
1663
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1664
            
1665
            s->current_picture_ptr= s->reordered_input_picture[0];
1666
            for(i=0; i<4; i++){
1667
                s->new_picture.data[i]+=16;
1668
            }
1669
        }
1670
        copy_picture(&s->current_picture, s->current_picture_ptr);
1671
    
1672
        s->picture_number= s->new_picture.display_picture_number;
1673
//printf("dpn:%d\n", s->picture_number);
1674
    }else{
1675
       memset(&s->new_picture, 0, sizeof(Picture));
1676
    }
1677
}
1678

    
1679
int MPV_encode_picture(AVCodecContext *avctx,
1680
                       unsigned char *buf, int buf_size, void *data)
1681
{
1682
    MpegEncContext *s = avctx->priv_data;
1683
    AVFrame *pic_arg = data;
1684
    int i;
1685

    
1686
    if(avctx->pix_fmt != PIX_FMT_YUV420P){
1687
        av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
1688
        return -1;
1689
    }
1690
    
1691
    init_put_bits(&s->pb, buf, buf_size);
1692

    
1693
    s->picture_in_gop_number++;
1694

    
1695
    load_input_picture(s, pic_arg);
1696
    
1697
    select_input_picture(s);
1698
    
1699
    /* output? */
1700
    if(s->new_picture.data[0]){
1701

    
1702
        s->pict_type= s->new_picture.pict_type;
1703
//emms_c();
1704
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1705
        MPV_frame_start(s, avctx);
1706

    
1707
        encode_picture(s, s->picture_number);
1708
        
1709
        avctx->real_pict_num  = s->picture_number;
1710
        avctx->header_bits = s->header_bits;
1711
        avctx->mv_bits     = s->mv_bits;
1712
        avctx->misc_bits   = s->misc_bits;
1713
        avctx->i_tex_bits  = s->i_tex_bits;
1714
        avctx->p_tex_bits  = s->p_tex_bits;
1715
        avctx->i_count     = s->i_count;
1716
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1717
        avctx->skip_count  = s->skip_count;
1718

    
1719
        MPV_frame_end(s);
1720

    
1721
        if (s->out_format == FMT_MJPEG)
1722
            mjpeg_picture_trailer(s);
1723
        
1724
        if(s->flags&CODEC_FLAG_PASS1)
1725
            ff_write_pass1_stats(s);
1726

    
1727
        for(i=0; i<4; i++){
1728
            avctx->error[i] += s->current_picture_ptr->error[i];
1729
        }
1730
    }
1731

    
1732
    s->input_picture_number++;
1733

    
1734
    flush_put_bits(&s->pb);
1735
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1736
    
1737
    s->total_bits += s->frame_bits;
1738
    avctx->frame_bits  = s->frame_bits;
1739
    
1740
    return pbBufPtr(&s->pb) - s->pb.buf;
1741
}
1742

    
1743
#endif //CONFIG_ENCODERS
1744

    
1745
static inline void gmc1_motion(MpegEncContext *s,
1746
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1747
                               int dest_offset,
1748
                               uint8_t **ref_picture, int src_offset)
1749
{
1750
    uint8_t *ptr;
1751
    int offset, src_x, src_y, linesize, uvlinesize;
1752
    int motion_x, motion_y;
1753
    int emu=0;
1754

    
1755
    motion_x= s->sprite_offset[0][0];
1756
    motion_y= s->sprite_offset[0][1];
1757
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1758
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1759
    motion_x<<=(3-s->sprite_warping_accuracy);
1760
    motion_y<<=(3-s->sprite_warping_accuracy);
1761
    src_x = clip(src_x, -16, s->width);
1762
    if (src_x == s->width)
1763
        motion_x =0;
1764
    src_y = clip(src_y, -16, s->height);
1765
    if (src_y == s->height)
1766
        motion_y =0;
1767

    
1768
    linesize = s->linesize;
1769
    uvlinesize = s->uvlinesize;
1770
    
1771
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1772

    
1773
    dest_y+=dest_offset;
1774
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1775
        if(   (unsigned)src_x >= s->h_edge_pos - 17
1776
           || (unsigned)src_y >= s->v_edge_pos - 17){
1777
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1778
            ptr= s->edge_emu_buffer;
1779
        }
1780
    }
1781
    
1782
    if((motion_x|motion_y)&7){
1783
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1784
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1785
    }else{
1786
        int dxy;
1787
        
1788
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1789
        if (s->no_rounding){
1790
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1791
        }else{
1792
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1793
        }
1794
    }
1795
    
1796
    if(s->flags&CODEC_FLAG_GRAY) return;
1797

    
1798
    motion_x= s->sprite_offset[1][0];
1799
    motion_y= s->sprite_offset[1][1];
1800
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1801
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1802
    motion_x<<=(3-s->sprite_warping_accuracy);
1803
    motion_y<<=(3-s->sprite_warping_accuracy);
1804
    src_x = clip(src_x, -8, s->width>>1);
1805
    if (src_x == s->width>>1)
1806
        motion_x =0;
1807
    src_y = clip(src_y, -8, s->height>>1);
1808
    if (src_y == s->height>>1)
1809
        motion_y =0;
1810

    
1811
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1812
    ptr = ref_picture[1] + offset;
1813
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1814
        if(   (unsigned)src_x >= (s->h_edge_pos>>1) - 9
1815
           || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
1816
            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);
1817
            ptr= s->edge_emu_buffer;
1818
            emu=1;
1819
        }
1820
    }
1821
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1822
    
1823
    ptr = ref_picture[2] + offset;
1824
    if(emu){
1825
        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);
1826
        ptr= s->edge_emu_buffer;
1827
    }
1828
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1829
    
1830
    return;
1831
}
1832

    
1833
static inline void gmc_motion(MpegEncContext *s,
1834
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1835
                               int dest_offset,
1836
                               uint8_t **ref_picture, int src_offset)
1837
{
1838
    uint8_t *ptr;
1839
    int linesize, uvlinesize;
1840
    const int a= s->sprite_warping_accuracy;
1841
    int ox, oy;
1842

    
1843
    linesize = s->linesize;
1844
    uvlinesize = s->uvlinesize;
1845

    
1846
    ptr = ref_picture[0] + src_offset;
1847

    
1848
    dest_y+=dest_offset;
1849
    
1850
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1851
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1852

    
1853
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1854
           ox, 
1855
           oy, 
1856
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1857
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1858
           a+1, (1<<(2*a+1)) - s->no_rounding,
1859
           s->h_edge_pos, s->v_edge_pos);
1860
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1861
           ox + s->sprite_delta[0][0]*8, 
1862
           oy + s->sprite_delta[1][0]*8, 
1863
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1864
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1865
           a+1, (1<<(2*a+1)) - s->no_rounding,
1866
           s->h_edge_pos, s->v_edge_pos);
1867

    
1868
    if(s->flags&CODEC_FLAG_GRAY) return;
1869

    
1870

    
1871
    dest_cb+=dest_offset>>1;
1872
    dest_cr+=dest_offset>>1;
1873
    
1874
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1875
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1876

    
1877
    ptr = ref_picture[1] + (src_offset>>1);
1878
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1879
           ox, 
1880
           oy, 
1881
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1882
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1883
           a+1, (1<<(2*a+1)) - s->no_rounding,
1884
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1885
    
1886
    ptr = ref_picture[2] + (src_offset>>1);
1887
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1888
           ox, 
1889
           oy, 
1890
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1891
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1892
           a+1, (1<<(2*a+1)) - s->no_rounding,
1893
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1894
}
1895

    
1896
/**
1897
 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
1898
 * @param buf destination buffer
1899
 * @param src source buffer
1900
 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
1901
 * @param block_w width of block
1902
 * @param block_h height of block
1903
 * @param src_x x coordinate of the top left sample of the block in the source buffer
1904
 * @param src_y y coordinate of the top left sample of the block in the source buffer
1905
 * @param w width of the source buffer
1906
 * @param h height of the source buffer
1907
 */
1908
void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 
1909
                                    int src_x, int src_y, int w, int h){
1910
    int x, y;
1911
    int start_y, start_x, end_y, end_x;
1912

    
1913
    if(src_y>= h){
1914
        src+= (h-1-src_y)*linesize;
1915
        src_y=h-1;
1916
    }else if(src_y<=-block_h){
1917
        src+= (1-block_h-src_y)*linesize;
1918
        src_y=1-block_h;
1919
    }
1920
    if(src_x>= w){
1921
        src+= (w-1-src_x);
1922
        src_x=w-1;
1923
    }else if(src_x<=-block_w){
1924
        src+= (1-block_w-src_x);
1925
        src_x=1-block_w;
1926
    }
1927

    
1928
    start_y= FFMAX(0, -src_y);
1929
    start_x= FFMAX(0, -src_x);
1930
    end_y= FFMIN(block_h, h-src_y);
1931
    end_x= FFMIN(block_w, w-src_x);
1932

    
1933
    // copy existing part
1934
    for(y=start_y; y<end_y; y++){
1935
        for(x=start_x; x<end_x; x++){
1936
            buf[x + y*linesize]= src[x + y*linesize];
1937
        }
1938
    }
1939

    
1940
    //top
1941
    for(y=0; y<start_y; y++){
1942
        for(x=start_x; x<end_x; x++){
1943
            buf[x + y*linesize]= buf[x + start_y*linesize];
1944
        }
1945
    }
1946

    
1947
    //bottom
1948
    for(y=end_y; y<block_h; y++){
1949
        for(x=start_x; x<end_x; x++){
1950
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1951
        }
1952
    }
1953
                                    
1954
    for(y=0; y<block_h; y++){
1955
       //left
1956
        for(x=0; x<start_x; x++){
1957
            buf[x + y*linesize]= buf[start_x + y*linesize];
1958
        }
1959
       
1960
       //right
1961
        for(x=end_x; x<block_w; x++){
1962
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1963
        }
1964
    }
1965
}
1966

    
1967

    
1968
/* apply one mpeg motion vector to the three components */
1969
static inline void mpeg_motion(MpegEncContext *s,
1970
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1971
                               int dest_offset,
1972
                               uint8_t **ref_picture, int src_offset,
1973
                               int field_based, op_pixels_func (*pix_op)[4],
1974
                               int motion_x, int motion_y, int h)
1975
{
1976
    uint8_t *ptr;
1977
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1978
    int emu=0;
1979
#if 0    
1980
if(s->quarter_sample)
1981
{
1982
    motion_x>>=1;
1983
    motion_y>>=1;
1984
}
1985
#endif
1986
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1987
    src_x = s->mb_x * 16 + (motion_x >> 1);
1988
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1989
                
1990
    /* WARNING: do no forget half pels */
1991
    height = s->height >> field_based;
1992
    v_edge_pos = s->v_edge_pos >> field_based;
1993
    src_x = clip(src_x, -16, s->width);
1994
    if (src_x == s->width)
1995
        dxy &= ~1;
1996
    src_y = clip(src_y, -16, height);
1997
    if (src_y == height)
1998
        dxy &= ~2;
1999
    linesize   = s->current_picture.linesize[0] << field_based;
2000
    uvlinesize = s->current_picture.linesize[1] << field_based;
2001
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
2002
    dest_y += dest_offset;
2003

    
2004
    if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
2005
        if(   (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
2006
           || (unsigned)src_y >    v_edge_pos - (motion_y&1) - h){
2007
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,  //FIXME linesize? and uv below
2008
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2009
            ptr= s->edge_emu_buffer + src_offset;
2010
            emu=1;
2011
        }
2012
    }
2013
    pix_op[0][dxy](dest_y, ptr, linesize, h);
2014

    
2015
    if(s->flags&CODEC_FLAG_GRAY) return;
2016

    
2017
    if (s->out_format == FMT_H263) {
2018
        dxy = 0;
2019
        if ((motion_x & 3) != 0)
2020
            dxy |= 1;
2021
        if ((motion_y & 3) != 0)
2022
            dxy |= 2;
2023
        mx = motion_x >> 2;
2024
        my = motion_y >> 2;
2025
    } else {
2026
        mx = motion_x / 2;
2027
        my = motion_y / 2;
2028
        dxy = ((my & 1) << 1) | (mx & 1);
2029
        mx >>= 1;
2030
        my >>= 1;
2031
    }
2032
    
2033
    src_x = s->mb_x * 8 + mx;
2034
    src_y = s->mb_y * (8 >> field_based) + my;
2035
    src_x = clip(src_x, -8, s->width >> 1);
2036
    if (src_x == (s->width >> 1))
2037
        dxy &= ~1;
2038
    src_y = clip(src_y, -8, height >> 1);
2039
    if (src_y == (height >> 1))
2040
        dxy &= ~2;
2041
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2042
    ptr = ref_picture[1] + offset;
2043
    if(emu){
2044
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
2045
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2046
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2047
    }
2048
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2049

    
2050
    ptr = ref_picture[2] + offset;
2051
    if(emu){
2052
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
2053
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2054
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2055
    }
2056
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2057
}
2058

    
2059
static inline void qpel_motion(MpegEncContext *s,
2060
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2061
                               int dest_offset,
2062
                               uint8_t **ref_picture, int src_offset,
2063
                               int field_based, op_pixels_func (*pix_op)[4],
2064
                               qpel_mc_func (*qpix_op)[16],
2065
                               int motion_x, int motion_y, int h)
2066
{
2067
    uint8_t *ptr;
2068
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
2069
    int emu=0;
2070

    
2071
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2072
    src_x = s->mb_x * 16 + (motion_x >> 2);
2073
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
2074

    
2075
    height = s->height >> field_based;
2076
    v_edge_pos = s->v_edge_pos >> field_based;
2077
    src_x = clip(src_x, -16, s->width);
2078
    if (src_x == s->width)
2079
        dxy &= ~3;
2080
    src_y = clip(src_y, -16, height);
2081
    if (src_y == height)
2082
        dxy &= ~12;
2083
    linesize = s->linesize << field_based;
2084
    uvlinesize = s->uvlinesize << field_based;
2085
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
2086
    dest_y += dest_offset;
2087
//printf("%d %d %d\n", src_x, src_y, dxy);
2088
    
2089
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2090
        if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16 
2091
           || (unsigned)src_y >    v_edge_pos - (motion_y&3) - h  ){
2092
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based, 
2093
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2094
            ptr= s->edge_emu_buffer + src_offset;
2095
            emu=1;
2096
        }
2097
    }
2098
    if(!field_based)
2099
        qpix_op[0][dxy](dest_y, ptr, linesize);
2100
    else{
2101
        //damn interlaced mode
2102
        //FIXME boundary mirroring is not exactly correct here
2103
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
2104
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
2105
    }
2106

    
2107
    if(s->flags&CODEC_FLAG_GRAY) return;
2108

    
2109
    if(field_based){
2110
        mx= motion_x/2;
2111
        my= motion_y>>1;
2112
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
2113
        static const int rtab[8]= {0,0,1,1,0,0,0,1};
2114
        mx= (motion_x>>1) + rtab[motion_x&7];
2115
        my= (motion_y>>1) + rtab[motion_y&7];
2116
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
2117
        mx= (motion_x>>1)|(motion_x&1);
2118
        my= (motion_y>>1)|(motion_y&1);
2119
    }else{
2120
        mx= motion_x/2;
2121
        my= motion_y/2;
2122
    }
2123
    mx= (mx>>1)|(mx&1);
2124
    my= (my>>1)|(my&1);
2125

    
2126
    dxy= (mx&1) | ((my&1)<<1);
2127
    mx>>=1;
2128
    my>>=1;
2129

    
2130
    src_x = s->mb_x * 8 + mx;
2131
    src_y = s->mb_y * (8 >> field_based) + my;
2132
    src_x = clip(src_x, -8, s->width >> 1);
2133
    if (src_x == (s->width >> 1))
2134
        dxy &= ~1;
2135
    src_y = clip(src_y, -8, height >> 1);
2136
    if (src_y == (height >> 1))
2137
        dxy &= ~2;
2138

    
2139
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2140
    ptr = ref_picture[1] + offset;
2141
    if(emu){
2142
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
2143
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2144
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2145
    }
2146
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
2147
    
2148
    ptr = ref_picture[2] + offset;
2149
    if(emu){
2150
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
2151
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2152
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2153
    }
2154
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
2155
}
2156

    
2157
inline int ff_h263_round_chroma(int x){
2158
    if (x >= 0)
2159
        return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2160
    else {
2161
        x = -x;
2162
        return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2163
    }
2164
}
2165

    
2166
/**
2167
 * motion compesation of a single macroblock
2168
 * @param s context
2169
 * @param dest_y luma destination pointer
2170
 * @param dest_cb chroma cb/u destination pointer
2171
 * @param dest_cr chroma cr/v destination pointer
2172
 * @param dir direction (0->forward, 1->backward)
2173
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2174
 * @param pic_op halfpel motion compensation function (average or put normally)
2175
 * @param pic_op qpel motion compensation function (average or put normally)
2176
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2177
 */
2178
static inline void MPV_motion(MpegEncContext *s, 
2179
                              uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2180
                              int dir, uint8_t **ref_picture, 
2181
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2182
{
2183
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
2184
    int mb_x, mb_y, i;
2185
    uint8_t *ptr, *dest;
2186
    int emu=0;
2187

    
2188
    mb_x = s->mb_x;
2189
    mb_y = s->mb_y;
2190

    
2191
    switch(s->mv_type) {
2192
    case MV_TYPE_16X16:
2193
#ifdef CONFIG_RISKY
2194
        if(s->mcsel){
2195
            if(s->real_sprite_warping_points==1){
2196
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
2197
                            ref_picture, 0);
2198
            }else{
2199
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
2200
                            ref_picture, 0);
2201
            }
2202
        }else if(s->quarter_sample){
2203
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2204
                        ref_picture, 0,
2205
                        0, pix_op, qpix_op,
2206
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2207
        }else if(s->mspel){
2208
            ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2209
                        ref_picture, pix_op,
2210
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2211
        }else
2212
#endif
2213
        {
2214
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2215
                        ref_picture, 0,
2216
                        0, pix_op,
2217
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2218
        }           
2219
        break;
2220
    case MV_TYPE_8X8:
2221
        mx = 0;
2222
        my = 0;
2223
        if(s->quarter_sample){
2224
            for(i=0;i<4;i++) {
2225
                motion_x = s->mv[dir][i][0];
2226
                motion_y = s->mv[dir][i][1];
2227

    
2228
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2229
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2230
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2231
                    
2232
                /* WARNING: do no forget half pels */
2233
                src_x = clip(src_x, -16, s->width);
2234
                if (src_x == s->width)
2235
                    dxy &= ~3;
2236
                src_y = clip(src_y, -16, s->height);
2237
                if (src_y == s->height)
2238
                    dxy &= ~12;
2239
                    
2240
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2241
                if(s->flags&CODEC_FLAG_EMU_EDGE){
2242
                    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8 
2243
                       || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
2244
                        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);
2245
                        ptr= s->edge_emu_buffer;
2246
                    }
2247
                }
2248
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2249
                qpix_op[1][dxy](dest, ptr, s->linesize);
2250

    
2251
                mx += s->mv[dir][i][0]/2;
2252
                my += s->mv[dir][i][1]/2;
2253
            }
2254
        }else{
2255
            for(i=0;i<4;i++) {
2256
                motion_x = s->mv[dir][i][0];
2257
                motion_y = s->mv[dir][i][1];
2258

    
2259
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2260
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
2261
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
2262
                    
2263
                /* WARNING: do no forget half pels */
2264
                src_x = clip(src_x, -16, s->width);
2265
                if (src_x == s->width)
2266
                    dxy &= ~1;
2267
                src_y = clip(src_y, -16, s->height);
2268
                if (src_y == s->height)
2269
                    dxy &= ~2;
2270
                    
2271
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2272
                if(s->flags&CODEC_FLAG_EMU_EDGE){
2273
                    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 8
2274
                       || (unsigned)src_y > s->v_edge_pos - (motion_y&1) - 8){
2275
                        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);
2276
                        ptr= s->edge_emu_buffer;
2277
                    }
2278
                }
2279
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2280
                pix_op[1][dxy](dest, ptr, s->linesize, 8);
2281

    
2282
                mx += s->mv[dir][i][0];
2283
                my += s->mv[dir][i][1];
2284
            }
2285
        }
2286

    
2287
        if(s->flags&CODEC_FLAG_GRAY) break;
2288
        /* In case of 8X8, we construct a single chroma motion vector
2289
           with a special rounding */
2290
        mx= ff_h263_round_chroma(mx);
2291
        my= ff_h263_round_chroma(my);
2292
        dxy = ((my & 1) << 1) | (mx & 1);
2293
        mx >>= 1;
2294
        my >>= 1;
2295

    
2296
        src_x = mb_x * 8 + mx;
2297
        src_y = mb_y * 8 + my;
2298
        src_x = clip(src_x, -8, s->width/2);
2299
        if (src_x == s->width/2)
2300
            dxy &= ~1;
2301
        src_y = clip(src_y, -8, s->height/2);
2302
        if (src_y == s->height/2)
2303
            dxy &= ~2;
2304
        
2305
        offset = (src_y * (s->uvlinesize)) + src_x;
2306
        ptr = ref_picture[1] + offset;
2307
        if(s->flags&CODEC_FLAG_EMU_EDGE){
2308
                if(   (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
2309
                   || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
2310
                    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);
2311
                    ptr= s->edge_emu_buffer;
2312
                    emu=1;
2313
                }
2314
            }
2315
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
2316

    
2317
        ptr = ref_picture[2] + offset;
2318
        if(emu){
2319
            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);
2320
            ptr= s->edge_emu_buffer;
2321
        }
2322
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
2323
        break;
2324
    case MV_TYPE_FIELD:
2325
        if (s->picture_structure == PICT_FRAME) {
2326
            if(s->quarter_sample){
2327
                /* top field */
2328
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2329
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2330
                            1, pix_op, qpix_op,
2331
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
2332
                /* bottom field */
2333
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2334
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2335
                            1, pix_op, qpix_op,
2336
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
2337
            }else{
2338
                /* top field */       
2339
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2340
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2341
                            1, pix_op,
2342
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
2343
                /* bottom field */
2344
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2345
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2346
                            1, pix_op,
2347
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
2348
            }
2349
        } else {
2350
            int offset;
2351
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2352
                offset= s->field_select[dir][0] ? s->linesize : 0;
2353
            }else{
2354
                ref_picture= s->current_picture.data;
2355
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
2356
            } 
2357

    
2358
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2359
                        ref_picture, offset,
2360
                        0, pix_op,
2361
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2362
        }
2363
        break;
2364
    case MV_TYPE_16X8:{
2365
        int offset;
2366
         uint8_t ** ref2picture;
2367

    
2368
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2369
                ref2picture= ref_picture;
2370
                offset= s->field_select[dir][0] ? s->linesize : 0;
2371
            }else{
2372
                ref2picture= s->current_picture.data;
2373
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
2374
            } 
2375

    
2376
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2377
                        ref2picture, offset,
2378
                        0, pix_op,
2379
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2380

    
2381

    
2382
            if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
2383
                ref2picture= ref_picture;
2384
                offset= s->field_select[dir][1] ? s->linesize : 0;
2385
            }else{
2386
                ref2picture= s->current_picture.data;
2387
                offset= s->field_select[dir][1] ? s->linesize : -s->linesize; 
2388
            } 
2389
            // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
2390
            mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
2391
                        0,
2392
                        ref2picture, offset,
2393
                        0, pix_op,
2394
                        s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
2395
        }
2396
        
2397
        break;
2398
    case MV_TYPE_DMV:
2399
    {
2400
    op_pixels_func (*dmv_pix_op)[4];
2401
    int offset;
2402

    
2403
        dmv_pix_op = s->dsp.put_pixels_tab;
2404

    
2405
        if(s->picture_structure == PICT_FRAME){
2406
            //put top field from top field
2407
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2408
                        ref_picture, 0,
2409
                        1, dmv_pix_op,
2410
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2411
            //put bottom field from bottom field
2412
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2413
                        ref_picture, s->linesize,
2414
                        1, dmv_pix_op,
2415
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2416

    
2417
            dmv_pix_op = s->dsp.avg_pixels_tab; 
2418
        
2419
            //avg top field from bottom field
2420
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2421
                        ref_picture, s->linesize,
2422
                        1, dmv_pix_op,
2423
                        s->mv[dir][2][0], s->mv[dir][2][1], 8);
2424
            //avg bottom field from top field
2425
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2426
                        ref_picture, 0,
2427
                        1, dmv_pix_op,
2428
                        s->mv[dir][3][0], s->mv[dir][3][1], 8);
2429

    
2430
        }else{
2431
            offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2432
                         s->linesize : 0;
2433

    
2434
            //put field from the same parity
2435
            //same parity is never in the same frame
2436
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2437
                        ref_picture,offset,
2438
                        0,dmv_pix_op,
2439
                        s->mv[dir][0][0],s->mv[dir][0][1],16);
2440

    
2441
            // after put we make avg of the same block
2442
            dmv_pix_op=s->dsp.avg_pixels_tab; 
2443

    
2444
            //opposite parity is always in the same frame if this is second field
2445
            if(!s->first_field){
2446
                ref_picture = s->current_picture.data;    
2447
                //top field is one linesize from frame beginig
2448
                offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2449
                        -s->linesize : s->linesize;
2450
            }else 
2451
                offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2452
                        0 : s->linesize;
2453

    
2454
            //avg field from the opposite parity
2455
            mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
2456
                        ref_picture, offset,
2457
                        0,dmv_pix_op,
2458
                        s->mv[dir][2][0],s->mv[dir][2][1],16);
2459
        }
2460
    }
2461
    break;
2462

    
2463
    }
2464
}
2465

    
2466

    
2467
/* put block[] to dest[] */
2468
static inline void put_dct(MpegEncContext *s, 
2469
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2470
{
2471
    s->dct_unquantize(s, block, i, s->qscale);
2472
    s->dsp.idct_put (dest, line_size, block);
2473
}
2474

    
2475
/* add block[] to dest[] */
2476
static inline void add_dct(MpegEncContext *s, 
2477
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2478
{
2479
    if (s->block_last_index[i] >= 0) {
2480
        s->dsp.idct_add (dest, line_size, block);
2481
    }
2482
}
2483

    
2484
static inline void add_dequant_dct(MpegEncContext *s, 
2485
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2486
{
2487
    if (s->block_last_index[i] >= 0) {
2488
        s->dct_unquantize(s, block, i, s->qscale);
2489

    
2490
        s->dsp.idct_add (dest, line_size, block);
2491
    }
2492
}
2493

    
2494
/**
2495
 * cleans dc, ac, coded_block for the current non intra MB
2496
 */
2497
void ff_clean_intra_table_entries(MpegEncContext *s)
2498
{
2499
    int wrap = s->block_wrap[0];
2500
    int xy = s->block_index[0];
2501
    
2502
    s->dc_val[0][xy           ] = 
2503
    s->dc_val[0][xy + 1       ] = 
2504
    s->dc_val[0][xy     + wrap] =
2505
    s->dc_val[0][xy + 1 + wrap] = 1024;
2506
    /* ac pred */
2507
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
2508
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2509
    if (s->msmpeg4_version>=3) {
2510
        s->coded_block[xy           ] =
2511
        s->coded_block[xy + 1       ] =
2512
        s->coded_block[xy     + wrap] =
2513
        s->coded_block[xy + 1 + wrap] = 0;
2514
    }
2515
    /* chroma */
2516
    wrap = s->block_wrap[4];
2517
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
2518
    s->dc_val[1][xy] =
2519
    s->dc_val[2][xy] = 1024;
2520
    /* ac pred */
2521
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2522
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2523
    
2524
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
2525
}
2526

    
2527
/* generic function called after a macroblock has been parsed by the
2528
   decoder or after it has been encoded by the encoder.
2529

2530
   Important variables used:
2531
   s->mb_intra : true if intra macroblock
2532
   s->mv_dir   : motion vector direction
2533
   s->mv_type  : motion vector type
2534
   s->mv       : motion vector
2535
   s->interlaced_dct : true if interlaced dct used (mpeg2)
2536
 */
2537
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
2538
{
2539
    int mb_x, mb_y;
2540
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2541
#ifdef HAVE_XVMC
2542
    if(s->avctx->xvmc_acceleration){
2543
        XVMC_decode_mb(s);//xvmc uses pblocks
2544
        return;
2545
    }
2546
#endif
2547

    
2548
    mb_x = s->mb_x;
2549
    mb_y = s->mb_y;
2550

    
2551
    s->current_picture.qscale_table[mb_xy]= s->qscale;
2552

    
2553
    /* update DC predictors for P macroblocks */
2554
    if (!s->mb_intra) {
2555
        if (s->h263_pred || s->h263_aic) {
2556
            if(s->mbintra_table[mb_xy])
2557
                ff_clean_intra_table_entries(s);
2558
        } else {
2559
            s->last_dc[0] =
2560
            s->last_dc[1] =
2561
            s->last_dc[2] = 128 << s->intra_dc_precision;
2562
        }
2563
    }
2564
    else if (s->h263_pred || s->h263_aic)
2565
        s->mbintra_table[mb_xy]=1;
2566

    
2567
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2568
        uint8_t *dest_y, *dest_cb, *dest_cr;
2569
        int dct_linesize, dct_offset;
2570
        op_pixels_func (*op_pix)[4];
2571
        qpel_mc_func (*op_qpix)[16];
2572
        const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2573
        const int uvlinesize= s->current_picture.linesize[1];
2574

    
2575
        /* avoid copy if macroblock skipped in last frame too */
2576
        /* skip only during decoding as we might trash the buffers during encoding a bit */
2577
        if(!s->encoding){
2578
            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2579
            const int age= s->current_picture.age;
2580

    
2581
            assert(age);
2582

    
2583
            if (s->mb_skiped) {
2584
                s->mb_skiped= 0;
2585
                assert(s->pict_type!=I_TYPE);
2586
 
2587
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2588
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2589

    
2590
                /* if previous was skipped too, then nothing to do !  */
2591
                if (*mbskip_ptr >= age && s->current_picture.reference){
2592
                    return;
2593
                }
2594
            } else if(!s->current_picture.reference){
2595
                (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2596
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2597
            } else{
2598
                *mbskip_ptr = 0; /* not skipped */
2599
            }
2600
        }
2601

    
2602
        if (s->interlaced_dct) {
2603
            dct_linesize = linesize * 2;
2604
            dct_offset = linesize;
2605
        } else {
2606
            dct_linesize = linesize;
2607
            dct_offset = linesize * 8;
2608
        }
2609
        
2610
        dest_y=  s->dest[0];
2611
        dest_cb= s->dest[1];
2612
        dest_cr= s->dest[2];
2613

    
2614
        if (!s->mb_intra) {
2615
            /* motion handling */
2616
            /* decoding or more than one mb_type (MC was allready done otherwise) */
2617
            if(!s->encoding){
2618
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
2619
                    op_pix = s->dsp.put_pixels_tab;
2620
                    op_qpix= s->dsp.put_qpel_pixels_tab;
2621
                }else{
2622
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
2623
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2624
                }
2625

    
2626
                if (s->mv_dir & MV_DIR_FORWARD) {
2627
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2628
                    op_pix = s->dsp.avg_pixels_tab;
2629
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
2630
                }
2631
                if (s->mv_dir & MV_DIR_BACKWARD) {
2632
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2633
                }
2634
            }
2635

    
2636
            /* skip dequant / idct if we are really late ;) */
2637
            if(s->hurry_up>1) return;
2638

    
2639
            /* add dct residue */
2640
            if(s->encoding || !(   s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
2641
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2642
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2643
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2644
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2645
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2646

    
2647
                if(!(s->flags&CODEC_FLAG_GRAY)){
2648
                    add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
2649
                    add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
2650
                }
2651
            } else if(s->codec_id != CODEC_ID_WMV2){
2652
                add_dct(s, block[0], 0, dest_y, dct_linesize);
2653
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2654
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2655
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2656

    
2657
                if(!(s->flags&CODEC_FLAG_GRAY)){
2658
                    add_dct(s, block[4], 4, dest_cb, uvlinesize);
2659
                    add_dct(s, block[5], 5, dest_cr, uvlinesize);
2660
                }
2661
            } 
2662
#ifdef CONFIG_RISKY
2663
            else{
2664
                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2665
            }
2666
#endif
2667
        } else {
2668
            /* dct only in intra block */
2669
            if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
2670
                put_dct(s, block[0], 0, dest_y, dct_linesize);
2671
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2672
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2673
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2674

    
2675
                if(!(s->flags&CODEC_FLAG_GRAY)){
2676
                    put_dct(s, block[4], 4, dest_cb, uvlinesize);
2677
                    put_dct(s, block[5], 5, dest_cr, uvlinesize);
2678
                }
2679
            }else{
2680
                s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
2681
                s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
2682
                s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
2683
                s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2684

    
2685
                if(!(s->flags&CODEC_FLAG_GRAY)){
2686
                    s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2687
                    s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2688
                }
2689
            }
2690
        }
2691
    }
2692
}
2693

    
2694
#ifdef CONFIG_ENCODERS
2695

    
2696
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2697
{
2698
    static const char tab[64]=
2699
        {3,2,2,1,1,1,1,1,
2700
         1,1,1,1,1,1,1,1,
2701
         1,1,1,1,1,1,1,1,
2702
         0,0,0,0,0,0,0,0,
2703
         0,0,0,0,0,0,0,0,
2704
         0,0,0,0,0,0,0,0,
2705
         0,0,0,0,0,0,0,0,
2706
         0,0,0,0,0,0,0,0};
2707
    int score=0;
2708
    int run=0;
2709
    int i;
2710
    DCTELEM *block= s->block[n];
2711
    const int last_index= s->block_last_index[n];
2712
    int skip_dc;
2713

    
2714
    if(threshold<0){
2715
        skip_dc=0;
2716
        threshold= -threshold;
2717
    }else
2718
        skip_dc=1;
2719

    
2720
    /* are all which we could set to zero are allready zero? */
2721
    if(last_index<=skip_dc - 1) return;
2722

    
2723
    for(i=0; i<=last_index; i++){
2724
        const int j = s->intra_scantable.permutated[i];
2725
        const int level = ABS(block[j]);
2726
        if(level==1){
2727
            if(skip_dc && i==0) continue;
2728
            score+= tab[run];
2729
            run=0;
2730
        }else if(level>1){
2731
            return;
2732
        }else{
2733
            run++;
2734
        }
2735
    }
2736
    if(score >= threshold) return;
2737
    for(i=skip_dc; i<=last_index; i++){
2738
        const int j = s->intra_scantable.permutated[i];
2739
        block[j]=0;
2740
    }
2741
    if(block[0]) s->block_last_index[n]= 0;
2742
    else         s->block_last_index[n]= -1;
2743
}
2744

    
2745
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2746
{
2747
    int i;
2748
    const int maxlevel= s->max_qcoeff;
2749
    const int minlevel= s->min_qcoeff;
2750
    
2751
    if(s->mb_intra){
2752
        i=1; //skip clipping of intra dc
2753
    }else
2754
        i=0;
2755
    
2756
    for(;i<=last_index; i++){
2757
        const int j= s->intra_scantable.permutated[i];
2758
        int level = block[j];
2759
       
2760
        if     (level>maxlevel) level=maxlevel;
2761
        else if(level<minlevel) level=minlevel;
2762

    
2763
        block[j]= level;
2764
    }
2765
}
2766

    
2767
#if 0
2768
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2769
    int score=0;
2770
    int x,y;
2771
    
2772
    for(y=0; y<7; y++){
2773
        for(x=0; x<16; x+=4){
2774
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2775
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2776
        }
2777
        s+= stride;
2778
    }
2779
    
2780
    return score;
2781
}
2782

2783
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2784
    int score=0;
2785
    int x,y;
2786
    
2787
    for(y=0; y<7; y++){
2788
        for(x=0; x<16; x++){
2789
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2790
        }
2791
        s1+= stride;
2792
        s2+= stride;
2793
    }
2794
    
2795
    return score;
2796
}
2797
#else
2798
#define SQ(a) ((a)*(a))
2799

    
2800
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2801
    int score=0;
2802
    int x,y;
2803
    
2804
    for(y=0; y<7; y++){
2805
        for(x=0; x<16; x+=4){
2806
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2807
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2808
        }
2809
        s+= stride;
2810
    }
2811
    
2812
    return score;
2813
}
2814

    
2815
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2816
    int score=0;
2817
    int x,y;
2818
    
2819
    for(y=0; y<7; y++){
2820
        for(x=0; x<16; x++){
2821
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2822
        }
2823
        s1+= stride;
2824
        s2+= stride;
2825
    }
2826
    
2827
    return score;
2828
}
2829

    
2830
#endif
2831

    
2832
#endif //CONFIG_ENCODERS
2833

    
2834
/**
2835
 *
2836
 * @param h is the normal height, this will be reduced automatically if needed for the last row
2837
 */
2838
void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2839
    if (s->avctx->draw_horiz_band) {
2840
        AVFrame *src;
2841
        int offset[4];
2842
        
2843
        if(s->picture_structure != PICT_FRAME){
2844
            h <<= 1;
2845
            y <<= 1;
2846
            if(s->first_field  && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2847
        }
2848

    
2849
        h= FFMIN(h, s->height - y);
2850

    
2851
        if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER)) 
2852
            src= (AVFrame*)s->current_picture_ptr;
2853
        else if(s->last_picture_ptr)
2854
            src= (AVFrame*)s->last_picture_ptr;
2855
        else
2856
            return;
2857
            
2858
        if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
2859
            offset[0]=
2860
            offset[1]=
2861
            offset[2]=
2862
            offset[3]= 0;
2863
        }else{
2864
            offset[0]= y * s->linesize;;
2865
            offset[1]= 
2866
            offset[2]= (y>>1) * s->uvlinesize;;
2867
            offset[3]= 0;
2868
        }
2869

    
2870
        emms_c();
2871

    
2872
        s->avctx->draw_horiz_band(s->avctx, src, offset,
2873
                                  y, s->picture_structure, h);
2874
    }
2875
}
2876

    
2877
void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2878
    const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2879
    const int uvlinesize= s->current_picture.linesize[1];
2880
        
2881
    s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2882
    s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1)     + s->mb_x*2;
2883
    s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
2884
    s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2)     + s->mb_x*2;
2885
    s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2886
    s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2887
    
2888
    if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
2889
        s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
2890
        s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
2891
        s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
2892
    }else{
2893
        s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize  ) + s->mb_x * 16 - 16;
2894
        s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2895
        s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2896
    }    
2897
}
2898

    
2899
#ifdef CONFIG_ENCODERS
2900

    
2901
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2902
{
2903
    const int mb_x= s->mb_x;
2904
    const int mb_y= s->mb_y;
2905
    int i;
2906
    int skip_dct[6];
2907
    int dct_offset   = s->linesize*8; //default for progressive frames
2908
    
2909
    for(i=0; i<6; i++) skip_dct[i]=0;
2910
    
2911
    if(s->adaptive_quant){
2912
        const int last_qp= s->qscale;
2913
        const int mb_xy= mb_x + mb_y*s->mb_stride;
2914

    
2915
        s->lambda= s->lambda_table[mb_xy];
2916
        update_qscale(s);
2917
    
2918
        if(!(s->flags&CODEC_FLAG_QP_RD)){
2919
            s->dquant= s->qscale - last_qp;
2920

    
2921
            if(s->out_format==FMT_H263)
2922
                s->dquant= clip(s->dquant, -2, 2); //FIXME RD
2923
            
2924
            if(s->codec_id==CODEC_ID_MPEG4){        
2925
                if(!s->mb_intra){
2926
                    if((s->mv_dir&MV_DIRECT) || s->mv_type==MV_TYPE_8X8)
2927
                        s->dquant=0;
2928
                }
2929
            }
2930
        }
2931
        s->qscale= last_qp + s->dquant;
2932
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2933
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2934
    }
2935

    
2936
    if (s->mb_intra) {
2937
        uint8_t *ptr;
2938
        int wrap_y;
2939
        int emu=0;
2940

    
2941
        wrap_y = s->linesize;
2942
        ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2943

    
2944
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2945
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2946
            ptr= s->edge_emu_buffer;
2947
            emu=1;
2948
        }
2949
        
2950
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2951
            int progressive_score, interlaced_score;
2952
            
2953
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2954
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2955
            
2956
            if(progressive_score > interlaced_score + 100){
2957
                s->interlaced_dct=1;
2958
            
2959
                dct_offset= wrap_y;
2960
                wrap_y<<=1;
2961
            }else
2962
                s->interlaced_dct=0;
2963
        }
2964
        
2965
        s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
2966
        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
2967
        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2968
        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2969

    
2970
        if(s->flags&CODEC_FLAG_GRAY){
2971
            skip_dct[4]= 1;
2972
            skip_dct[5]= 1;
2973
        }else{
2974
            int wrap_c = s->uvlinesize;
2975
            ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2976
            if(emu){
2977
                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);
2978
                ptr= s->edge_emu_buffer;
2979
            }
2980
            s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2981

    
2982
            ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2983
            if(emu){
2984
                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);
2985
                ptr= s->edge_emu_buffer;
2986
            }
2987
            s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2988
        }
2989
    }else{
2990
        op_pixels_func (*op_pix)[4];
2991
        qpel_mc_func (*op_qpix)[16];
2992
        uint8_t *dest_y, *dest_cb, *dest_cr;
2993
        uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2994
        int wrap_y, wrap_c;
2995
        int emu=0;
2996

    
2997
        dest_y  = s->dest[0];
2998
        dest_cb = s->dest[1];
2999
        dest_cr = s->dest[2];
3000
        wrap_y = s->linesize;
3001
        wrap_c = s->uvlinesize;
3002
        ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
3003
        ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
3004
        ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
3005

    
3006
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
3007
            op_pix = s->dsp.put_pixels_tab;
3008
            op_qpix= s->dsp.put_qpel_pixels_tab;
3009
        }else{
3010
            op_pix = s->dsp.put_no_rnd_pixels_tab;
3011
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
3012
        }
3013

    
3014
        if (s->mv_dir & MV_DIR_FORWARD) {
3015
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
3016
            op_pix = s->dsp.avg_pixels_tab;
3017
            op_qpix= s->dsp.avg_qpel_pixels_tab;
3018
        }
3019
        if (s->mv_dir & MV_DIR_BACKWARD) {
3020
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
3021
        }
3022

    
3023
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
3024
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
3025
            ptr_y= s->edge_emu_buffer;
3026
            emu=1;
3027
        }
3028
        
3029
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
3030
            int progressive_score, interlaced_score;
3031
            
3032
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
3033
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
3034
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
3035
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
3036
            
3037
            if(progressive_score > interlaced_score + 600){
3038
                s->interlaced_dct=1;
3039
            
3040
                dct_offset= wrap_y;
3041
                wrap_y<<=1;
3042
            }else
3043
                s->interlaced_dct=0;
3044
        }
3045
        
3046
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
3047
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
3048
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
3049
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
3050
        
3051
        if(s->flags&CODEC_FLAG_GRAY){
3052
            skip_dct[4]= 1;
3053
            skip_dct[5]= 1;
3054
        }else{
3055
            if(emu){
3056
                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);
3057
                ptr_cb= s->edge_emu_buffer;
3058
            }
3059
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
3060
            if(emu){
3061
                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);
3062
                ptr_cr= s->edge_emu_buffer;
3063
            }
3064
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
3065
        }
3066
        /* pre quantization */         
3067
        if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
3068
            //FIXME optimize
3069
            if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
3070
            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
3071
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
3072
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
3073
            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
3074
            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
3075
#if 0
3076
{
3077
 static int stat[7];
3078
 int num=0;
3079
 for(i=0; i<6; i++)
3080
  if(skip_dct[i]) num++;
3081
 stat[num]++;
3082
 
3083
 if(s->mb_x==0 && s->mb_y==0){
3084
  for(i=0; i<7; i++){
3085
   printf("%6d %1d\n", stat[i], i);
3086
  }
3087
 }
3088
}
3089
#endif
3090
        }
3091

    
3092
    }
3093
            
3094
    /* DCT & quantize */
3095
    if(s->out_format==FMT_MJPEG){
3096
        for(i=0;i<6;i++) {
3097
            int overflow;
3098
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
3099
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
3100
        }
3101
    }else{
3102
        for(i=0;i<6;i++) {
3103
            if(!skip_dct[i]){
3104
                int overflow;
3105
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
3106
            // FIXME we could decide to change to quantizer instead of clipping
3107
            // JS: I don't think that would be a good idea it could lower quality instead
3108
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
3109
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
3110
            }else
3111
                s->block_last_index[i]= -1;
3112
        }
3113
        
3114
        if(s->luma_elim_threshold && !s->mb_intra)
3115
            for(i=0; i<4; i++)
3116
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
3117
        if(s->chroma_elim_threshold && !s->mb_intra)
3118
            for(i=4; i<6; i++)
3119
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
3120

    
3121
        if(s->flags & CODEC_FLAG_CBP_RD){
3122
            for(i=0;i<6;i++) {
3123
                if(s->block_last_index[i] == -1)
3124
                    s->coded_score[i]= INT_MAX/256;
3125
            }
3126
        }
3127
    }
3128

    
3129
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
3130
        s->block_last_index[4]=
3131
        s->block_last_index[5]= 0;
3132
        s->block[4][0]=
3133
        s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
3134
    }
3135

    
3136
    /* huffman encode */
3137
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
3138
    case CODEC_ID_MPEG1VIDEO:
3139
    case CODEC_ID_MPEG2VIDEO:
3140
        mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
3141
#ifdef CONFIG_RISKY
3142
    case CODEC_ID_MPEG4:
3143
        mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3144
    case CODEC_ID_MSMPEG4V2:
3145
    case CODEC_ID_MSMPEG4V3:
3146
    case CODEC_ID_WMV1:
3147
        msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3148
    case CODEC_ID_WMV2:
3149
         ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
3150
    case CODEC_ID_H263:
3151
    case CODEC_ID_H263P:
3152
    case CODEC_ID_FLV1:
3153
    case CODEC_ID_RV10:
3154
        h263_encode_mb(s, s->block, motion_x, motion_y); break;
3155
#endif
3156
    case CODEC_ID_MJPEG:
3157
        mjpeg_encode_mb(s, s->block); break;
3158
    default:
3159
        assert(0);
3160
    }
3161
}
3162

    
3163
#endif //CONFIG_ENCODERS
3164

    
3165
/**
3166
 * combines the (truncated) bitstream to a complete frame
3167
 * @returns -1 if no complete frame could be created
3168
 */
3169
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
3170
    ParseContext *pc= &s->parse_context;
3171

    
3172
#if 0
3173
    if(pc->overread){
3174
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3175
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3176
    }
3177
#endif
3178

    
3179
    /* copy overreaded byes from last frame into buffer */
3180
    for(; pc->overread>0; pc->overread--){
3181
        pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
3182
    }
3183
    
3184
    pc->last_index= pc->index;
3185

    
3186
    /* copy into buffer end return */
3187
    if(next == END_NOT_FOUND){
3188
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3189

    
3190
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
3191
        pc->index += *buf_size;
3192
        return -1;
3193
    }
3194

    
3195
    *buf_size=
3196
    pc->overread_index= pc->index + next;
3197
    
3198
    /* append to buffer */
3199
    if(pc->index){
3200
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3201

    
3202
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
3203
        pc->index = 0;
3204
        *buf= pc->buffer;
3205
    }
3206

    
3207
    /* store overread bytes */
3208
    for(;next < 0; next++){
3209
        pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
3210
        pc->overread++;
3211
    }
3212

    
3213
#if 0
3214
    if(pc->overread){
3215
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3216
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3217
    }
3218
#endif
3219

    
3220
    return 0;
3221
}
3222

    
3223
void ff_mpeg_flush(AVCodecContext *avctx){
3224
    int i;
3225
    MpegEncContext *s = avctx->priv_data;
3226
    
3227
    for(i=0; i<MAX_PICTURE_COUNT; i++){
3228
       if(s->picture[i].data[0] && (   s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
3229
                                    || s->picture[i].type == FF_BUFFER_TYPE_USER))
3230
        avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
3231
    }
3232
    s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3233
    
3234
    s->parse_context.state= -1;
3235
    s->parse_context.frame_start_found= 0;
3236
    s->parse_context.overread= 0;
3237
    s->parse_context.overread_index= 0;
3238
    s->parse_context.index= 0;
3239
    s->parse_context.last_index= 0;
3240
}
3241

    
3242
#ifdef CONFIG_ENCODERS
3243
void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
3244
{
3245
    int bytes= length>>4;
3246
    int bits= length&15;
3247
    int i;
3248

    
3249
    if(length==0) return;
3250

    
3251
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
3252
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
3253
}
3254

    
3255
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
3256
    int i;
3257

    
3258
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3259

    
3260
    /* mpeg1 */
3261
    d->mb_skip_run= s->mb_skip_run;
3262
    for(i=0; i<3; i++)
3263
        d->last_dc[i]= s->last_dc[i];
3264
    
3265
    /* statistics */
3266
    d->mv_bits= s->mv_bits;
3267
    d->i_tex_bits= s->i_tex_bits;
3268
    d->p_tex_bits= s->p_tex_bits;
3269
    d->i_count= s->i_count;
3270
    d->f_count= s->f_count;
3271
    d->b_count= s->b_count;
3272
    d->skip_count= s->skip_count;
3273
    d->misc_bits= s->misc_bits;
3274
    d->last_bits= 0;
3275

    
3276
    d->mb_skiped= 0;
3277
    d->qscale= s->qscale;
3278
    d->dquant= s->dquant;
3279
}
3280

    
3281
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
3282
    int i;
3283

    
3284
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
3285
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3286
    
3287
    /* mpeg1 */
3288
    d->mb_skip_run= s->mb_skip_run;
3289
    for(i=0; i<3; i++)
3290
        d->last_dc[i]= s->last_dc[i];
3291
    
3292
    /* statistics */
3293
    d->mv_bits= s->mv_bits;
3294
    d->i_tex_bits= s->i_tex_bits;
3295
    d->p_tex_bits= s->p_tex_bits;
3296
    d->i_count= s->i_count;
3297
    d->f_count= s->f_count;
3298
    d->b_count= s->b_count;
3299
    d->skip_count= s->skip_count;
3300
    d->misc_bits= s->misc_bits;
3301

    
3302
    d->mb_intra= s->mb_intra;
3303
    d->mb_skiped= s->mb_skiped;
3304
    d->mv_type= s->mv_type;
3305
    d->mv_dir= s->mv_dir;
3306
    d->pb= s->pb;
3307
    if(s->data_partitioning){
3308
        d->pb2= s->pb2;
3309
        d->tex_pb= s->tex_pb;
3310
    }
3311
    d->block= s->block;
3312
    for(i=0; i<6; i++)
3313
        d->block_last_index[i]= s->block_last_index[i];
3314
    d->interlaced_dct= s->interlaced_dct;
3315
    d->qscale= s->qscale;
3316
}
3317

    
3318
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
3319
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
3320
                           int *dmin, int *next_block, int motion_x, int motion_y)
3321
{
3322
    int score;
3323
    uint8_t *dest_backup[3];
3324
    
3325
    copy_context_before_encode(s, backup, type);
3326

    
3327
    s->block= s->blocks[*next_block];
3328
    s->pb= pb[*next_block];
3329
    if(s->data_partitioning){
3330
        s->pb2   = pb2   [*next_block];
3331
        s->tex_pb= tex_pb[*next_block];
3332
    }
3333
    
3334
    if(*next_block){
3335
        memcpy(dest_backup, s->dest, sizeof(s->dest));
3336
        s->dest[0] = s->me.scratchpad;
3337
        s->dest[1] = s->me.scratchpad + 16;
3338
        s->dest[2] = s->me.scratchpad + 16 + 8;
3339
        assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
3340
        assert(s->linesize >= 64); //FIXME
3341
    }
3342

    
3343
    encode_mb(s, motion_x, motion_y);
3344
    
3345
    score= get_bit_count(&s->pb);
3346
    if(s->data_partitioning){
3347
        score+= get_bit_count(&s->pb2);
3348
        score+= get_bit_count(&s->tex_pb);
3349
    }
3350
   
3351
    if(s->avctx->mb_decision == FF_MB_DECISION_RD){
3352
        MPV_decode_mb(s, s->block);
3353

    
3354
        score *= s->lambda2;
3355
        score += sse_mb(s) << FF_LAMBDA_SHIFT;
3356
    }
3357
    
3358
    if(*next_block){
3359
        memcpy(s->dest, dest_backup, sizeof(s->dest));
3360
    }
3361

    
3362
    if(score<*dmin){
3363
        *dmin= score;
3364
        *next_block^=1;
3365

    
3366
        copy_context_after_encode(best, s, type);
3367
    }
3368
}
3369
                
3370
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
3371
    uint32_t *sq = squareTbl + 256;
3372
    int acc=0;
3373
    int x,y;
3374
    
3375
    if(w==16 && h==16) 
3376
        return s->dsp.sse[0](NULL, src1, src2, stride);
3377
    else if(w==8 && h==8)
3378
        return s->dsp.sse[1](NULL, src1, src2, stride);
3379
    
3380
    for(y=0; y<h; y++){
3381
        for(x=0; x<w; x++){
3382
            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
3383
        } 
3384
    }
3385
    
3386
    assert(acc>=0);
3387
    
3388
    return acc;
3389
}
3390

    
3391
static int sse_mb(MpegEncContext *s){
3392
    int w= 16;
3393
    int h= 16;
3394

    
3395
    if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3396
    if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3397

    
3398
    if(w==16 && h==16)
3399
        return  s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize)
3400
               +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize)
3401
               +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize);
3402
    else
3403
        return  sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
3404
               +sse(s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
3405
               +sse(s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
3406
}
3407

    
3408
static void encode_picture(MpegEncContext *s, int picture_number)
3409
{
3410
    int mb_x, mb_y, pdif = 0;
3411
    int i;
3412
    int bits;
3413
    MpegEncContext best_s, backup_s;
3414
    uint8_t bit_buf[2][3000];
3415
    uint8_t bit_buf2[2][3000];
3416
    uint8_t bit_buf_tex[2][3000];
3417
    PutBitContext pb[2], pb2[2], tex_pb[2];
3418

    
3419
    for(i=0; i<2; i++){
3420
        init_put_bits(&pb    [i], bit_buf    [i], 3000);
3421
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000);
3422
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
3423
    }
3424

    
3425
    s->picture_number = picture_number;
3426
    
3427
    /* Reset the average MB variance */
3428
    s->current_picture.mb_var_sum = 0;
3429
    s->current_picture.mc_mb_var_sum = 0;
3430

    
3431
#ifdef CONFIG_RISKY
3432
    /* we need to initialize some time vars before we can encode b-frames */
3433
    // RAL: Condition added for MPEG1VIDEO
3434
    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
3435
        ff_set_mpeg4_time(s, s->picture_number); 
3436
#endif
3437
        
3438
    s->scene_change_score=0;
3439
    
3440
    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
3441
    
3442
    if(s->pict_type==I_TYPE){
3443
        if(s->msmpeg4_version >= 3) s->no_rounding=1;
3444
        else                        s->no_rounding=0;
3445
    }else if(s->pict_type!=B_TYPE){
3446
        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
3447
            s->no_rounding ^= 1;          
3448
    }
3449
    
3450
    /* Estimate motion for every MB */
3451
    s->mb_intra=0; //for the rate distoration & bit compare functions
3452
    if(s->pict_type != I_TYPE){
3453
        if(s->pict_type != B_TYPE){
3454
            if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
3455
                s->me.pre_pass=1;
3456
                s->me.dia_size= s->avctx->pre_dia_size;
3457

    
3458
                for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
3459
                    for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
3460
                        s->mb_x = mb_x;
3461
                        s->mb_y = mb_y;
3462
                        ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
3463
                    }
3464
                }
3465
                s->me.pre_pass=0;
3466
            }
3467
        }
3468

    
3469
        s->me.dia_size= s->avctx->dia_size;
3470
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3471
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3472
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3473
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3474
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3475
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3476
                s->mb_x = mb_x;
3477
                s->mb_y = mb_y;
3478
                s->block_index[0]+=2;
3479
                s->block_index[1]+=2;
3480
                s->block_index[2]+=2;
3481
                s->block_index[3]+=2;
3482
                
3483
                /* compute motion vector & mb_type and store in context */
3484
                if(s->pict_type==B_TYPE)
3485
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
3486
                else
3487
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
3488
            }
3489
        }
3490
    }else /* if(s->pict_type == I_TYPE) */{
3491
        /* I-Frame */
3492
        //FIXME do we need to zero them?
3493
        memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
3494
        memset(s->p_mv_table   , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
3495
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3496
        
3497
        if(!s->fixed_qscale){
3498
            /* finding spatial complexity for I-frame rate control */
3499
            for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3500
                for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3501
                    int xx = mb_x * 16;
3502
                    int yy = mb_y * 16;
3503
                    uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
3504
                    int varc;
3505
                    int sum = s->dsp.pix_sum(pix, s->linesize);
3506
    
3507
                    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
3508

    
3509
                    s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
3510
                    s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
3511
                    s->current_picture.mb_var_sum    += varc;
3512
                }
3513
            }
3514
        }
3515
    }
3516
    emms_c();
3517

    
3518
    if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
3519
        s->pict_type= I_TYPE;
3520
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
3521
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3522
    }
3523

    
3524
    if(!s->umvplus){
3525
        if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
3526
            s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
3527
        
3528
            ff_fix_long_p_mvs(s);
3529
        }
3530

    
3531
        if(s->pict_type==B_TYPE){
3532
            int a, b;
3533

    
3534
            a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
3535
            b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
3536
            s->f_code = FFMAX(a, b);
3537

    
3538
            a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
3539
            b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
3540
            s->b_code = FFMAX(a, b);
3541

    
3542
            ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
3543
            ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
3544
            ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
3545
            ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
3546
        }
3547
    }
3548
    
3549
    if (!s->fixed_qscale) 
3550
        s->current_picture.quality = ff_rate_estimate_qscale(s);
3551

    
3552
    if(s->adaptive_quant){
3553
#ifdef CONFIG_RISKY
3554
        switch(s->codec_id){
3555
        case CODEC_ID_MPEG4:
3556
            ff_clean_mpeg4_qscales(s);
3557
            break;
3558
        case CODEC_ID_H263:
3559
        case CODEC_ID_H263P:
3560
        case CODEC_ID_FLV1:
3561
            ff_clean_h263_qscales(s);
3562
            break;
3563
        }
3564
#endif
3565

    
3566
        s->lambda= s->lambda_table[0];
3567
        //FIXME broken
3568
    }else
3569
        s->lambda= s->current_picture.quality;
3570
//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
3571
    update_qscale(s);
3572
    
3573
    if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE)) 
3574
        s->qscale= 3; //reduce cliping problems
3575
        
3576
    if (s->out_format == FMT_MJPEG) {
3577
        /* for mjpeg, we do include qscale in the matrix */
3578
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
3579
        for(i=1;i<64;i++){
3580
            int j= s->dsp.idct_permutation[i];
3581

    
3582
            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3583
        }
3584
        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 
3585
                       s->intra_matrix, s->intra_quant_bias, 8, 8);
3586
    }
3587
    
3588
    //FIXME var duplication
3589
    s->current_picture.key_frame= s->pict_type == I_TYPE;
3590
    s->current_picture.pict_type= s->pict_type;
3591

    
3592
    if(s->current_picture.key_frame)
3593
        s->picture_in_gop_number=0;
3594

    
3595
    s->last_bits= get_bit_count(&s->pb);
3596
    switch(s->out_format) {
3597
    case FMT_MJPEG:
3598
        mjpeg_picture_header(s);
3599
        break;
3600
#ifdef CONFIG_RISKY
3601
    case FMT_H263:
3602
        if (s->codec_id == CODEC_ID_WMV2) 
3603
            ff_wmv2_encode_picture_header(s, picture_number);
3604
        else if (s->h263_msmpeg4) 
3605
            msmpeg4_encode_picture_header(s, picture_number);
3606
        else if (s->h263_pred)
3607
            mpeg4_encode_picture_header(s, picture_number);
3608
        else if (s->h263_rv10) 
3609
            rv10_encode_picture_header(s, picture_number);
3610
        else if (s->codec_id == CODEC_ID_FLV1)
3611
            ff_flv_encode_picture_header(s, picture_number);
3612
        else
3613
            h263_encode_picture_header(s, picture_number);
3614
        break;
3615
#endif
3616
    case FMT_MPEG1:
3617
        mpeg1_encode_picture_header(s, picture_number);
3618
        break;
3619
    case FMT_H264:
3620
        break;
3621
    }
3622
    bits= get_bit_count(&s->pb);
3623
    s->header_bits= bits - s->last_bits;
3624
    s->last_bits= bits;
3625
    s->mv_bits=0;
3626
    s->misc_bits=0;
3627
    s->i_tex_bits=0;
3628
    s->p_tex_bits=0;
3629
    s->i_count=0;
3630
    s->f_count=0;
3631
    s->b_count=0;
3632
    s->skip_count=0;
3633

    
3634
    for(i=0; i<3; i++){
3635
        /* init last dc values */
3636
        /* note: quant matrix value (8) is implied here */
3637
        s->last_dc[i] = 128;
3638
        
3639
        s->current_picture_ptr->error[i] = 0;
3640
    }
3641
    s->mb_skip_run = 0;
3642
    s->last_mv[0][0][0] = 0;
3643
    s->last_mv[0][0][1] = 0;
3644
    s->last_mv[1][0][0] = 0;
3645
    s->last_mv[1][0][1] = 0;
3646
     
3647
    s->last_mv_dir = 0;
3648

    
3649
#ifdef CONFIG_RISKY
3650
    switch(s->codec_id){
3651
    case CODEC_ID_H263:
3652
    case CODEC_ID_H263P:
3653
    case CODEC_ID_FLV1:
3654
        s->gob_index = ff_h263_get_gob_height(s);
3655
        break;
3656
    case CODEC_ID_MPEG4:
3657
        if(s->partitioned_frame)
3658
            ff_mpeg4_init_partitions(s);
3659
        break;
3660
    }
3661
#endif
3662

    
3663
    s->resync_mb_x=0;
3664
    s->resync_mb_y=0;
3665
    s->first_slice_line = 1;
3666
    s->ptr_lastgob = s->pb.buf;
3667
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3668
        s->mb_x=0;
3669
        s->mb_y= mb_y;
3670

    
3671
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3672
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3673
        ff_init_block_index(s);
3674
        
3675
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3676
            const int xy= mb_y*s->mb_stride + mb_x;
3677
            int mb_type= s->mb_type[xy];
3678
//            int d;
3679
            int dmin= INT_MAX;
3680

    
3681
            s->mb_x = mb_x;
3682
            ff_update_block_index(s);
3683

    
3684
            /* write gob / video packet header  */
3685
#ifdef CONFIG_RISKY
3686
            if(s->rtp_mode && mb_y + mb_x>0){
3687
                int current_packet_size, is_gob_start;
3688
                
3689
                current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3690
                is_gob_start=0;
3691
                
3692
                if(s->codec_id==CODEC_ID_MPEG4){
3693
                    if(current_packet_size >= s->rtp_payload_size){
3694

    
3695
                        if(s->partitioned_frame){
3696
                            ff_mpeg4_merge_partitions(s);
3697
                            ff_mpeg4_init_partitions(s);
3698
                        }
3699
                        ff_mpeg4_encode_video_packet_header(s);
3700

    
3701
                        if(s->flags&CODEC_FLAG_PASS1){
3702
                            int bits= get_bit_count(&s->pb);
3703
                            s->misc_bits+= bits - s->last_bits;
3704
                            s->last_bits= bits;
3705
                        }
3706
                        ff_mpeg4_clean_buffers(s);
3707
                        is_gob_start=1;
3708
                    }
3709
                }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
3710
                    if(   current_packet_size >= s->rtp_payload_size 
3711
                       && s->mb_skip_run==0){
3712
                        ff_mpeg1_encode_slice_header(s);
3713
                        ff_mpeg1_clean_buffers(s);
3714
                        is_gob_start=1;
3715
                    }
3716
                }else if(s->codec_id==CODEC_ID_MPEG2VIDEO){
3717
                    if(   (   current_packet_size >= s->rtp_payload_size || mb_x==0)
3718
                       && s->mb_skip_run==0){
3719
                        ff_mpeg1_encode_slice_header(s);
3720
                        ff_mpeg1_clean_buffers(s);
3721
                        is_gob_start=1;
3722
                    }
3723
                }else{
3724
                    if(current_packet_size >= s->rtp_payload_size
3725
                       && s->mb_x==0 && s->mb_y%s->gob_index==0){
3726
                       
3727
                        h263_encode_gob_header(s, mb_y);                       
3728
                        is_gob_start=1;
3729
                    }
3730
                }
3731

    
3732
                if(is_gob_start){
3733
                    s->ptr_lastgob = pbBufPtr(&s->pb);
3734
                    s->first_slice_line=1;
3735
                    s->resync_mb_x=mb_x;
3736
                    s->resync_mb_y=mb_y;
3737
                }
3738
            }
3739
#endif
3740

    
3741
            if(  (s->resync_mb_x   == s->mb_x)
3742
               && s->resync_mb_y+1 == s->mb_y){
3743
                s->first_slice_line=0; 
3744
            }
3745

    
3746
            s->mb_skiped=0;
3747
            s->dquant=0; //only for QP_RD
3748

    
3749
            if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
3750
                int next_block=0;
3751
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3752

    
3753
                copy_context_before_encode(&backup_s, s, -1);
3754
                backup_s.pb= s->pb;
3755
                best_s.data_partitioning= s->data_partitioning;
3756
                best_s.partitioned_frame= s->partitioned_frame;
3757
                if(s->data_partitioning){
3758
                    backup_s.pb2= s->pb2;
3759
                    backup_s.tex_pb= s->tex_pb;
3760
                }
3761

    
3762
                if(mb_type&MB_TYPE_INTER){
3763
                    s->mv_dir = MV_DIR_FORWARD;
3764
                    s->mv_type = MV_TYPE_16X16;
3765
                    s->mb_intra= 0;
3766
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
3767
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
3768
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
3769
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3770
                }
3771
                if(mb_type&MB_TYPE_SKIPED){
3772
                    s->mv_dir = MV_DIR_FORWARD;
3773
                    s->mv_type = MV_TYPE_16X16;
3774
                    s->mb_intra= 0;
3775
                    s->mv[0][0][0] = 0;
3776
                    s->mv[0][0][1] = 0;
3777
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_SKIPED, pb, pb2, tex_pb, 
3778
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3779
                }
3780
                if(mb_type&MB_TYPE_INTER4V){                 
3781
                    s->mv_dir = MV_DIR_FORWARD;
3782
                    s->mv_type = MV_TYPE_8X8;
3783
                    s->mb_intra= 0;
3784
                    for(i=0; i<4; i++){
3785
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3786
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3787
                    }
3788
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
3789
                                 &dmin, &next_block, 0, 0);
3790
                }
3791
                if(mb_type&MB_TYPE_FORWARD){
3792
                    s->mv_dir = MV_DIR_FORWARD;
3793
                    s->mv_type = MV_TYPE_16X16;
3794
                    s->mb_intra= 0;
3795
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3796
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3797
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
3798
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3799
                }
3800
                if(mb_type&MB_TYPE_BACKWARD){
3801
                    s->mv_dir = MV_DIR_BACKWARD;
3802
                    s->mv_type = MV_TYPE_16X16;
3803
                    s->mb_intra= 0;
3804
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3805
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3806
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
3807
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3808
                }
3809
                if(mb_type&MB_TYPE_BIDIR){
3810
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3811
                    s->mv_type = MV_TYPE_16X16;
3812
                    s->mb_intra= 0;
3813
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3814
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3815
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3816
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3817
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
3818
                                 &dmin, &next_block, 0, 0);
3819
                }
3820
                if(mb_type&MB_TYPE_DIRECT){
3821
                    int mx= s->b_direct_mv_table[xy][0];
3822
                    int my= s->b_direct_mv_table[xy][1];
3823
                    
3824
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3825
                    s->mb_intra= 0;
3826
#ifdef CONFIG_RISKY
3827
                    ff_mpeg4_set_direct_mv(s, mx, my);
3828
#endif
3829
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
3830
                                 &dmin, &next_block, mx, my);
3831
                }
3832
                if(mb_type&MB_TYPE_INTRA){
3833
                    s->mv_dir = 0;
3834
                    s->mv_type = MV_TYPE_16X16;
3835
                    s->mb_intra= 1;
3836
                    s->mv[0][0][0] = 0;
3837
                    s->mv[0][0][1] = 0;
3838
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
3839
                                 &dmin, &next_block, 0, 0);
3840
                    if(s->h263_pred || s->h263_aic){
3841
                        if(best_s.mb_intra)
3842
                            s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3843
                        else
3844
                            ff_clean_intra_table_entries(s); //old mode?
3845
                    }
3846
                }
3847

    
3848
                if(s->flags & CODEC_FLAG_QP_RD){
3849
                    if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
3850
                        const int last_qp= backup_s.qscale;
3851
                        int dquant, dir, qp, dc[6];
3852
                        DCTELEM ac[6][16];
3853
                        const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3854
                        
3855
                        assert(backup_s.dquant == 0);
3856

    
3857
                        //FIXME intra
3858
                        s->mv_dir= best_s.mv_dir;
3859
                        s->mv_type = MV_TYPE_16X16;
3860
                        s->mb_intra= best_s.mb_intra;
3861
                        s->mv[0][0][0] = best_s.mv[0][0][0];
3862
                        s->mv[0][0][1] = best_s.mv[0][0][1];
3863
                        s->mv[1][0][0] = best_s.mv[1][0][0];
3864
                        s->mv[1][0][1] = best_s.mv[1][0][1];
3865
                        
3866
                        dir= s->pict_type == B_TYPE ? 2 : 1;
3867
                        if(last_qp + dir > s->avctx->qmax) dir= -dir;
3868
                        for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
3869
                            qp= last_qp + dquant;
3870
                            if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3871
                                break;
3872
                            backup_s.dquant= dquant;
3873
                            if(s->mb_intra){
3874
                                for(i=0; i<6; i++){
3875
                                    dc[i]= s->dc_val[0][ s->block_index[i] ];
3876
                                    memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
3877
                                }
3878
                            }
3879

    
3880
                            encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, 
3881
                                         &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3882
                            if(best_s.qscale != qp){
3883
                                if(s->mb_intra){
3884
                                    for(i=0; i<6; i++){
3885
                                        s->dc_val[0][ s->block_index[i] ]= dc[i];
3886
                                        memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
3887
                                    }
3888
                                }
3889
                                if(dir > 0 && dquant==dir){
3890
                                    dquant= 0;
3891
                                    dir= -dir;
3892
                                }else
3893
                                    break;
3894
                            }
3895
                        }
3896
                        qp= best_s.qscale;
3897
                        s->current_picture.qscale_table[xy]= qp;
3898
                    }
3899
                }
3900

    
3901
                copy_context_after_encode(s, &best_s, -1);
3902
                
3903
                pb_bits_count= get_bit_count(&s->pb);
3904
                flush_put_bits(&s->pb);
3905
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3906
                s->pb= backup_s.pb;
3907
                
3908
                if(s->data_partitioning){
3909
                    pb2_bits_count= get_bit_count(&s->pb2);
3910
                    flush_put_bits(&s->pb2);
3911
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3912
                    s->pb2= backup_s.pb2;
3913
                    
3914
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
3915
                    flush_put_bits(&s->tex_pb);
3916
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3917
                    s->tex_pb= backup_s.tex_pb;
3918
                }
3919
                s->last_bits= get_bit_count(&s->pb);
3920
               
3921
#ifdef CONFIG_RISKY
3922
                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3923
                    ff_h263_update_motion_val(s);
3924
#endif
3925
        
3926
                if(next_block==0){
3927
                    s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad     , s->linesize  ,16);
3928
                    s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
3929
                    s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
3930
                }
3931

    
3932
                if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3933
                    MPV_decode_mb(s, s->block);
3934
            } else {
3935
                int motion_x, motion_y;
3936
                int intra_score;
3937
                int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride];
3938
                
3939
              if(s->avctx->mb_decision==FF_MB_DECISION_SIMPLE && s->pict_type==P_TYPE){ //FIXME check if the mess is usefull at all
3940
                /* get luma score */
3941
                if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3942
                    intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_stride]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3943
                }else{
3944
                    uint8_t *dest_y;
3945

    
3946
                    int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_stride]; //FIXME
3947
                    mean*= 0x01010101;
3948
                    
3949
                    dest_y  = s->new_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
3950
                
3951
                    for(i=0; i<16; i++){
3952
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3953
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3954
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3955
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3956
                    }
3957

    
3958
                    s->mb_intra=1;
3959
                    intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3960
                                        
3961
/*                    printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8, 
3962
                        s->current_picture.mb_var[mb_x + mb_y*s->mb_stride],
3963
                        s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_stride]);*/
3964
                }
3965
                
3966
                /* get chroma score */
3967
                if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3968
                    int i;
3969
                    
3970
                    s->mb_intra=1;
3971
                    for(i=1; i<3; i++){
3972
                        uint8_t *dest_c;
3973
                        int mean;
3974
                        
3975
                        if(s->out_format == FMT_H263){
3976
                            mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3977
                        }else{
3978
                            mean= (s->last_dc[i] + 4)>>3;
3979
                        }
3980
                        dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
3981
                        
3982
                        mean*= 0x01010101;
3983
                        for(i=0; i<8; i++){
3984
                            *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3985
                            *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3986
                        }
3987
                        
3988
                        intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3989
                    }                
3990
                }
3991

    
3992
                /* bias */
3993
                switch(s->avctx->mb_cmp&0xFF){
3994
                default:
3995
                case FF_CMP_SAD:
3996
                    intra_score+= 32*s->qscale;
3997
                    break;
3998
                case FF_CMP_SSE:
3999
                    intra_score+= 24*s->qscale*s->qscale;
4000
                    break;
4001
                case FF_CMP_SATD:
4002
                    intra_score+= 96*s->qscale;
4003
                    break;
4004
                case FF_CMP_DCT:
4005
                    intra_score+= 48*s->qscale;
4006
                    break;
4007
                case FF_CMP_BIT:
4008
                    intra_score+= 16;
4009
                    break;
4010
                case FF_CMP_PSNR:
4011
                case FF_CMP_RD:
4012
                    intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
4013
                    break;
4014
                }
4015

    
4016
                if(intra_score < inter_score)
4017
                    mb_type= MB_TYPE_INTRA;
4018
              }  
4019
                
4020
                s->mv_type=MV_TYPE_16X16;
4021
                // only one MB-Type possible
4022
                
4023
                switch(mb_type){
4024
                case MB_TYPE_INTRA:
4025
                    s->mv_dir = 0;
4026
                    s->mb_intra= 1;
4027
                    motion_x= s->mv[0][0][0] = 0;
4028
                    motion_y= s->mv[0][0][1] = 0;
4029
                    break;
4030
                case MB_TYPE_INTER:
4031
                    s->mv_dir = MV_DIR_FORWARD;
4032
                    s->mb_intra= 0;
4033
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
4034
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
4035
                    break;
4036
                case MB_TYPE_INTER4V:
4037
                    s->mv_dir = MV_DIR_FORWARD;
4038
                    s->mv_type = MV_TYPE_8X8;
4039
                    s->mb_intra= 0;
4040
                    for(i=0; i<4; i++){
4041
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
4042
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
4043
                    }
4044
                    motion_x= motion_y= 0;
4045
                    break;
4046
                case MB_TYPE_DIRECT:
4047
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4048
                    s->mb_intra= 0;
4049
                    motion_x=s->b_direct_mv_table[xy][0];
4050
                    motion_y=s->b_direct_mv_table[xy][1];
4051
#ifdef CONFIG_RISKY
4052
                    ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
4053
#endif
4054
                    break;
4055
                case MB_TYPE_BIDIR:
4056
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4057
                    s->mb_intra= 0;
4058
                    motion_x=0;
4059
                    motion_y=0;
4060
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
4061
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
4062
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
4063
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
4064
                    break;
4065
                case MB_TYPE_BACKWARD:
4066
                    s->mv_dir = MV_DIR_BACKWARD;
4067
                    s->mb_intra= 0;
4068
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
4069
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
4070
                    break;
4071
                case MB_TYPE_FORWARD:
4072
                    s->mv_dir = MV_DIR_FORWARD;
4073
                    s->mb_intra= 0;
4074
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
4075
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
4076
//                    printf(" %d %d ", motion_x, motion_y);
4077
                    break;
4078
                default:
4079
                    motion_x=motion_y=0; //gcc warning fix
4080
                    av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
4081
                }
4082

    
4083
                encode_mb(s, motion_x, motion_y);
4084

    
4085
                // RAL: Update last macrobloc type
4086
                s->last_mv_dir = s->mv_dir;
4087
            
4088
#ifdef CONFIG_RISKY
4089
                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
4090
                    ff_h263_update_motion_val(s);
4091
#endif
4092
                
4093
                MPV_decode_mb(s, s->block);
4094
            }
4095

    
4096
            /* clean the MV table in IPS frames for direct mode in B frames */
4097
            if(s->mb_intra /* && I,P,S_TYPE */){
4098
                s->p_mv_table[xy][0]=0;
4099
                s->p_mv_table[xy][1]=0;
4100
            }
4101
            
4102
            if(s->flags&CODEC_FLAG_PSNR){
4103
                int w= 16;
4104
                int h= 16;
4105

    
4106
                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
4107
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
4108

    
4109
                s->current_picture_ptr->error[0] += sse(
4110
                    s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
4111
                    s->dest[0], w, h, s->linesize);
4112
                s->current_picture_ptr->error[1] += sse(
4113
                    s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
4114
                    s->dest[1], w>>1, h>>1, s->uvlinesize);
4115
                s->current_picture_ptr->error[2] += sse(
4116
                    s, s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
4117
                    s->dest[2], w>>1, h>>1, s->uvlinesize);
4118
            }
4119
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
4120
        }
4121
    }
4122
    emms_c();
4123

    
4124
#ifdef CONFIG_RISKY
4125
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
4126
        ff_mpeg4_merge_partitions(s);
4127

    
4128
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
4129
        msmpeg4_encode_ext_header(s);
4130

    
4131
    if(s->codec_id==CODEC_ID_MPEG4) 
4132
        ff_mpeg4_stuffing(&s->pb);
4133
#endif
4134

    
4135
    //if (s->gob_number)
4136
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
4137
    
4138
    /* Send the last GOB if RTP */    
4139
    if (s->rtp_mode) {
4140
        flush_put_bits(&s->pb);
4141
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
4142
        /* Call the RTP callback to send the last GOB */
4143
        if (s->rtp_callback)
4144
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
4145
        s->ptr_lastgob = pbBufPtr(&s->pb);
4146
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
4147
    }
4148
}
4149

    
4150
void ff_denoise_dct(MpegEncContext *s, DCTELEM *block){
4151
    const int intra= s->mb_intra;
4152
    int i;
4153

    
4154
    s->dct_count[intra]++;
4155

    
4156
    for(i=0; i<64; i++){
4157
        int level= block[i];
4158

    
4159
        if(level){
4160
            if(level>0){
4161
                s->dct_error_sum[intra][i] += level;
4162
                level -= s->dct_offset[intra][i];
4163
                if(level<0) level=0;
4164
            }else{
4165
                s->dct_error_sum[intra][i] -= level;
4166
                level += s->dct_offset[intra][i];
4167
                if(level>0) level=0;
4168
            }
4169
            block[i]= level;
4170
        }
4171
    }
4172
}
4173

    
4174
static int dct_quantize_trellis_c(MpegEncContext *s, 
4175
                        DCTELEM *block, int n,
4176
                        int qscale, int *overflow){
4177
    const int *qmat;
4178
    const uint8_t *scantable= s->intra_scantable.scantable;
4179
    int max=0;
4180
    unsigned int threshold1, threshold2;
4181
    int bias=0;
4182
    int run_tab[65];
4183
    int level_tab[65];
4184
    int score_tab[65];
4185
    int last_run=0;
4186
    int last_level=0;
4187
    int last_score= 0;
4188
    int last_i= 0;
4189
    int not_coded_score= 0;
4190
    int coeff[3][64];
4191
    int coeff_count[64];
4192
    int qmul, qadd, start_i, last_non_zero, i, dc;
4193
    const int esc_length= s->ac_esc_length;
4194
    uint8_t * length;
4195
    uint8_t * last_length;
4196
    int score_limit=0;
4197
    int left_limit= 0;
4198
    const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
4199
    const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra;
4200
        
4201
    s->dsp.fdct (block);
4202
    
4203
    if(s->dct_error_sum)
4204
        ff_denoise_dct(s, block);
4205
    
4206
    qmul= qscale*16;
4207
    qadd= ((qscale-1)|1)*8;
4208

    
4209
    if (s->mb_intra) {
4210
        int q;
4211
        if (!s->h263_aic) {
4212
            if (n < 4)
4213
                q = s->y_dc_scale;
4214
            else
4215
                q = s->c_dc_scale;
4216
            q = q << 3;
4217
        } else{
4218
            /* For AIC we skip quant/dequant of INTRADC */
4219
            q = 1 << 3;
4220
            qadd=0;
4221
        }
4222
            
4223
        /* note: block[0] is assumed to be positive */
4224
        block[0] = (block[0] + (q >> 1)) / q;
4225
        start_i = 1;
4226
        last_non_zero = 0;
4227
        qmat = s->q_intra_matrix[qscale];
4228
        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
4229
            bias= 1<<(QMAT_SHIFT-1);
4230
        length     = s->intra_ac_vlc_length;
4231
        last_length= s->intra_ac_vlc_last_length;
4232
    } else {
4233
        start_i = 0;
4234
        last_non_zero = -1;
4235
        qmat = s->q_inter_matrix[qscale];
4236
        length     = s->inter_ac_vlc_length;
4237
        last_length= s->inter_ac_vlc_last_length;
4238
    }
4239

    
4240
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
4241
    threshold2= (threshold1<<1);
4242

    
4243
    for(i=start_i; i<64; i++) {
4244
        const int j = scantable[i];
4245
        const int k= i-start_i;
4246
        int level = block[j];
4247
        level = level * qmat[j];
4248

    
4249
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
4250
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
4251
        if(((unsigned)(level+threshold1))>threshold2){
4252
            if(level>0){
4253
                level= (bias + level)>>QMAT_SHIFT;
4254
                coeff[0][k]= level;
4255
                coeff[1][k]= level-1;
4256
//                coeff[2][k]= level-2;
4257
            }else{
4258
                level= (bias - level)>>QMAT_SHIFT;
4259
                coeff[0][k]= -level;
4260
                coeff[1][k]= -level+1;
4261
//                coeff[2][k]= -level+2;
4262
            }
4263
            coeff_count[k]= FFMIN(level, 2);
4264
            assert(coeff_count[k]);
4265
            max |=level;
4266
            last_non_zero = i;
4267
        }else{
4268
            coeff[0][k]= (level>>31)|1;
4269
            coeff_count[k]= 1;
4270
        }
4271
    }
4272
    
4273
    *overflow= s->max_qcoeff < max; //overflow might have happend
4274
    
4275
    if(last_non_zero < start_i){
4276
        memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4277
        return last_non_zero;
4278
    }
4279

    
4280
    score_tab[0]= 0;
4281
    
4282
    if(patch_table){
4283
//        length[UNI_AC_ENC_INDEX(0, 63)]=
4284
//        length[UNI_AC_ENC_INDEX(0, 65)]= 2;
4285
    }
4286

    
4287
    for(i=0; i<=last_non_zero - start_i; i++){
4288
        int level_index, run, j;
4289
        const int dct_coeff= block[ scantable[i + start_i] ];
4290
        const int zero_distoration= dct_coeff*dct_coeff;
4291
        int best_score=256*256*256*120;
4292

    
4293
        last_score += zero_distoration;
4294
        not_coded_score += zero_distoration;
4295
        for(level_index=0; level_index < coeff_count[i]; level_index++){
4296
            int distoration;
4297
            int level= coeff[level_index][i];
4298
            int unquant_coeff;
4299
            
4300
            assert(level);
4301

    
4302
            if(s->out_format == FMT_H263){
4303
                if(level>0){
4304
                    unquant_coeff= level*qmul + qadd;
4305
                }else{
4306
                    unquant_coeff= level*qmul - qadd;
4307
                }
4308
            }else{ //MPEG1
4309
                j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
4310
                if(s->mb_intra){
4311
                    if (level < 0) {
4312
                        unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
4313
                        unquant_coeff = -((unquant_coeff - 1) | 1);
4314
                    } else {
4315
                        unquant_coeff = (int)(  level  * qscale * s->intra_matrix[j]) >> 3;
4316
                        unquant_coeff =   (unquant_coeff - 1) | 1;
4317
                    }
4318
                }else{
4319
                    if (level < 0) {
4320
                        unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4321
                        unquant_coeff = -((unquant_coeff - 1) | 1);
4322
                    } else {
4323
                        unquant_coeff = (((  level  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
4324
                        unquant_coeff =   (unquant_coeff - 1) | 1;
4325
                    }
4326
                }
4327
                unquant_coeff<<= 3;
4328
            }
4329

    
4330
            distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
4331
            level+=64;
4332
            if((level&(~127)) == 0){
4333
                for(run=0; run<=i - left_limit; run++){
4334
                    int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4335
                    score += score_tab[i-run];
4336
                    
4337
                    if(score < best_score){
4338
                        best_score= 
4339
                        score_tab[i+1]= score;
4340
                        run_tab[i+1]= run;
4341
                        level_tab[i+1]= level-64;
4342
                    }
4343
                }
4344

    
4345
                if(s->out_format == FMT_H263){
4346
                    for(run=0; run<=i - left_limit; run++){
4347
                        int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4348
                        score += score_tab[i-run];
4349
                        if(score < last_score){
4350
                            last_score= score;
4351
                            last_run= run;
4352
                            last_level= level-64;
4353
                            last_i= i+1;
4354
                        }
4355
                    }
4356
                }
4357
            }else{
4358
                distoration += esc_length*lambda;
4359
                for(run=0; run<=i - left_limit; run++){
4360
                    int score= distoration + score_tab[i-run];
4361
                    
4362
                    if(score < best_score){
4363
                        best_score= 
4364
                        score_tab[i+1]= score;
4365
                        run_tab[i+1]= run;
4366
                        level_tab[i+1]= level-64;
4367
                    }
4368
                }
4369

    
4370
                if(s->out_format == FMT_H263){
4371
                    for(run=0; run<=i - left_limit; run++){
4372
                        int score= distoration + score_tab[i-run];
4373
                        if(score < last_score){
4374
                            last_score= score;
4375
                            last_run= run;
4376
                            last_level= level-64;
4377
                            last_i= i+1;
4378
                        }
4379
                    }
4380
                }
4381
            }
4382
        }
4383

    
4384
        for(j=left_limit; j<=i; j++){
4385
            score_tab[j] += zero_distoration;
4386
        }
4387
        score_limit+= zero_distoration;
4388
        if(score_tab[i+1] < score_limit)
4389
            score_limit= score_tab[i+1];
4390
        
4391
        //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
4392
        while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
4393
    
4394
        if(patch_table){
4395
//            length[UNI_AC_ENC_INDEX(0, 63)]=
4396
//            length[UNI_AC_ENC_INDEX(0, 65)]= 3;
4397
        }
4398
    }
4399

    
4400
    if(s->out_format != FMT_H263){
4401
        last_score= 256*256*256*120;
4402
        for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
4403
            int score= score_tab[i];
4404
            if(i) score += lambda*2; //FIXME exacter?
4405

    
4406
            if(score < last_score){
4407
                last_score= score;
4408
                last_i= i;
4409
                last_level= level_tab[i];
4410
                last_run= run_tab[i];
4411
            }
4412
        }
4413
    }
4414

    
4415
    s->coded_score[n] = last_score - not_coded_score;
4416
    
4417
    dc= block[0];
4418
    last_non_zero= last_i - 1 + start_i;
4419
    memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
4420
    
4421
    if(last_non_zero < start_i)
4422
        return last_non_zero;
4423

    
4424
    if(last_non_zero == 0 && start_i == 0){
4425
        int best_level= 0;
4426
        int best_score= dc * dc;
4427
        
4428
        for(i=0; i<coeff_count[0]; i++){
4429
            int level= coeff[i][0];
4430
            int unquant_coeff, score, distoration;
4431

    
4432
            if(s->out_format == FMT_H263){
4433
                if(level>0){
4434
                    unquant_coeff= (level*qmul + qadd)>>3;
4435
                }else{
4436
                    unquant_coeff= (level*qmul - qadd)>>3;
4437
                }
4438
            }else{ //MPEG1
4439
                    if (level < 0) {
4440
                        unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
4441
                        unquant_coeff = -((unquant_coeff - 1) | 1);
4442
                    } else {
4443
                        unquant_coeff = (((  level  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
4444
                        unquant_coeff =   (unquant_coeff - 1) | 1;
4445
                    }
4446
            }
4447
            unquant_coeff = (unquant_coeff + 4) >> 3;
4448
            unquant_coeff<<= 3 + 3;
4449

    
4450
            distoration= (unquant_coeff - dc) * (unquant_coeff - dc);
4451
            level+=64;
4452
            if((level&(~127)) == 0)
4453
                score= distoration + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4454
            else
4455
                score= distoration + esc_length*lambda;
4456

    
4457
            if(score < best_score){
4458
                best_score= score;
4459
                best_level= level - 64;
4460
            }
4461
        }
4462
        block[0]= best_level;
4463
        s->coded_score[n] = best_score - dc*dc;
4464
        if(best_level == 0) return -1;
4465
        else                return last_non_zero;
4466
    }
4467

    
4468
    i= last_i;
4469
    assert(last_level);
4470
//FIXME use permutated scantable
4471
    block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
4472
    i -= last_run + 1;
4473
    
4474
    for(;i>0 ; i -= run_tab[i] + 1){
4475
        const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
4476
    
4477
        block[j]= level_tab[i];
4478
        assert(block[j]);
4479
    }
4480

    
4481
    return last_non_zero;
4482
}
4483

    
4484
static int dct_quantize_c(MpegEncContext *s, 
4485
                        DCTELEM *block, int n,
4486
                        int qscale, int *overflow)
4487
{
4488
    int i, j, level, last_non_zero, q;
4489
    const int *qmat;
4490
    const uint8_t *scantable= s->intra_scantable.scantable;
4491
    int bias;
4492
    int max=0;
4493
    unsigned int threshold1, threshold2;
4494

    
4495
    s->dsp.fdct (block);
4496

    
4497
    if(s->dct_error_sum)
4498
        ff_denoise_dct(s, block);
4499

    
4500
    if (s->mb_intra) {
4501
        if (!s->h263_aic) {
4502
            if (n < 4)
4503
                q = s->y_dc_scale;
4504
            else
4505
                q = s->c_dc_scale;
4506
            q = q << 3;
4507
        } else
4508
            /* For AIC we skip quant/dequant of INTRADC */
4509
            q = 1 << 3;
4510
            
4511
        /* note: block[0] is assumed to be positive */
4512
        block[0] = (block[0] + (q >> 1)) / q;
4513
        i = 1;
4514
        last_non_zero = 0;
4515
        qmat = s->q_intra_matrix[qscale];
4516
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4517
    } else {
4518
        i = 0;
4519
        last_non_zero = -1;
4520
        qmat = s->q_inter_matrix[qscale];
4521
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4522
    }
4523
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
4524
    threshold2= (threshold1<<1);
4525

    
4526
    for(;i<64;i++) {
4527
        j = scantable[i];
4528
        level = block[j];
4529
        level = level * qmat[j];
4530

    
4531
//        if(   bias+level >= (1<<QMAT_SHIFT)
4532
//           || bias-level >= (1<<QMAT_SHIFT)){
4533
        if(((unsigned)(level+threshold1))>threshold2){
4534
            if(level>0){
4535
                level= (bias + level)>>QMAT_SHIFT;
4536
                block[j]= level;
4537
            }else{
4538
                level= (bias - level)>>QMAT_SHIFT;
4539
                block[j]= -level;
4540
            }
4541
            max |=level;
4542
            last_non_zero = i;
4543
        }else{
4544
            block[j]=0;
4545
        }
4546
    }
4547
    *overflow= s->max_qcoeff < max; //overflow might have happend
4548
    
4549
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4550
    if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4551
        ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
4552

    
4553
    return last_non_zero;
4554
}
4555

    
4556
#endif //CONFIG_ENCODERS
4557

    
4558
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
4559
                                   DCTELEM *block, int n, int qscale)
4560
{
4561
    int i, level, nCoeffs;
4562
    const uint16_t *quant_matrix;
4563

    
4564
    nCoeffs= s->block_last_index[n];
4565
    
4566
    if (s->mb_intra) {
4567
        if (n < 4) 
4568
            block[0] = block[0] * s->y_dc_scale;
4569
        else
4570
            block[0] = block[0] * s->c_dc_scale;
4571
        /* XXX: only mpeg1 */
4572
        quant_matrix = s->intra_matrix;
4573
        for(i=1;i<=nCoeffs;i++) {
4574
            int j= s->intra_scantable.permutated[i];
4575
            level = block[j];
4576
            if (level) {
4577
                if (level < 0) {
4578
                    level = -level;
4579
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
4580
                    level = (level - 1) | 1;
4581
                    level = -level;
4582
                } else {
4583
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
4584
                    level = (level - 1) | 1;
4585
                }
4586
#ifdef PARANOID
4587
                if (level < -2048 || level > 2047)
4588
                    fprintf(stderr, "unquant error %d %d\n", i, level);
4589
#endif
4590
                block[j] = level;
4591
            }
4592
        }
4593
    } else {
4594
        i = 0;
4595
        quant_matrix = s->inter_matrix;
4596
        for(;i<=nCoeffs;i++) {
4597
            int j= s->intra_scantable.permutated[i];
4598
            level = block[j];
4599
            if (level) {
4600
                if (level < 0) {
4601
                    level = -level;
4602
                    level = (((level << 1) + 1) * qscale *
4603
                             ((int) (quant_matrix[j]))) >> 4;
4604
                    level = (level - 1) | 1;
4605
                    level = -level;
4606
                } else {
4607
                    level = (((level << 1) + 1) * qscale *
4608
                             ((int) (quant_matrix[j]))) >> 4;
4609
                    level = (level - 1) | 1;
4610
                }
4611
#ifdef PARANOID
4612
                if (level < -2048 || level > 2047)
4613
                    fprintf(stderr, "unquant error %d %d\n", i, level);
4614
#endif
4615
                block[j] = level;
4616
            }
4617
        }
4618
    }
4619
}
4620

    
4621
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
4622
                                   DCTELEM *block, int n, int qscale)
4623
{
4624
    int i, level, nCoeffs;
4625
    const uint16_t *quant_matrix;
4626

    
4627
    if(s->alternate_scan) nCoeffs= 63;
4628
    else nCoeffs= s->block_last_index[n];
4629
    
4630
    if (s->mb_intra) {
4631
        if (n < 4) 
4632
            block[0] = block[0] * s->y_dc_scale;
4633
        else
4634
            block[0] = block[0] * s->c_dc_scale;
4635
        quant_matrix = s->intra_matrix;
4636
        for(i=1;i<=nCoeffs;i++) {
4637
            int j= s->intra_scantable.permutated[i];
4638
            level = block[j];
4639
            if (level) {
4640
                if (level < 0) {
4641
                    level = -level;
4642
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
4643
                    level = -level;
4644
                } else {
4645
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
4646
                }
4647
#ifdef PARANOID
4648
                if (level < -2048 || level > 2047)
4649
                    fprintf(stderr, "unquant error %d %d\n", i, level);
4650
#endif
4651
                block[j] = level;
4652
            }
4653
        }
4654
    } else {
4655
        int sum=-1;
4656
        i = 0;
4657
        quant_matrix = s->inter_matrix;
4658
        for(;i<=nCoeffs;i++) {
4659
            int j= s->intra_scantable.permutated[i];
4660
            level = block[j];
4661
            if (level) {
4662
                if (level < 0) {
4663
                    level = -level;
4664
                    level = (((level << 1) + 1) * qscale *
4665
                             ((int) (quant_matrix[j]))) >> 4;
4666
                    level = -level;
4667
                } else {
4668
                    level = (((level << 1) + 1) * qscale *
4669
                             ((int) (quant_matrix[j]))) >> 4;
4670
                }
4671
#ifdef PARANOID
4672
                if (level < -2048 || level > 2047)
4673
                    fprintf(stderr, "unquant error %d %d\n", i, level);
4674
#endif
4675
                block[j] = level;
4676
                sum+=level;
4677
            }
4678
        }
4679
        block[63]^=sum&1;
4680
    }
4681
}
4682

    
4683

    
4684
static void dct_unquantize_h263_c(MpegEncContext *s, 
4685
                                  DCTELEM *block, int n, int qscale)
4686
{
4687
    int i, level, qmul, qadd;
4688
    int nCoeffs;
4689
    
4690
    assert(s->block_last_index[n]>=0);
4691
    
4692
    qadd = (qscale - 1) | 1;
4693
    qmul = qscale << 1;
4694
    
4695
    if (s->mb_intra) {
4696
        if (!s->h263_aic) {
4697
            if (n < 4) 
4698
                block[0] = block[0] * s->y_dc_scale;
4699
            else
4700
                block[0] = block[0] * s->c_dc_scale;
4701
        }else
4702
            qadd = 0;
4703
        i = 1;
4704
        nCoeffs= 63; //does not allways use zigzag table 
4705
    } else {
4706
        i = 0;
4707
        nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
4708
    }
4709

    
4710
    for(;i<=nCoeffs;i++) {
4711
        level = block[i];
4712
        if (level) {
4713
            if (level < 0) {
4714
                level = level * qmul - qadd;
4715
            } else {
4716
                level = level * qmul + qadd;
4717
            }
4718
#ifdef PARANOID
4719
                if (level < -2048 || level > 2047)
4720
                    fprintf(stderr, "unquant error %d %d\n", i, level);
4721
#endif
4722
            block[i] = level;
4723
        }
4724
    }
4725
}
4726

    
4727

    
4728
static const AVOption mpeg4_options[] =
4729
{
4730
    AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
4731
    AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
4732
                       "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
4733
                       bit_rate_tolerance, 4, 240000000, 8000),
4734
    AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
4735
    AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
4736
    AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
4737
                          rc_eq, "tex^qComp,option1,options2", 0),
4738
    AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
4739
                       rc_min_rate, 4, 24000000, 0),
4740
    AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
4741
                       rc_max_rate, 4, 24000000, 0),
4742
    AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",