Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 46b4feec

History | View | Annotate | Download (142 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 <ctype.h>
28
#include <limits.h>
29
#include "avcodec.h"
30
#include "dsputil.h"
31
#include "mpegvideo.h"
32

    
33
#ifdef USE_FASTMEMCPY
34
#include "fastmemcpy.h"
35
#endif
36

    
37
//#undef NDEBUG
38
//#include <assert.h>
39

    
40
#ifdef CONFIG_ENCODERS
41
static void encode_picture(MpegEncContext *s, int picture_number);
42
#endif //CONFIG_ENCODERS
43
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
44
                                   DCTELEM *block, int n, int qscale);
45
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46
                                   DCTELEM *block, int n, int qscale);
47
static void dct_unquantize_h263_c(MpegEncContext *s, 
48
                                  DCTELEM *block, int n, int qscale);
49
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50
#ifdef CONFIG_ENCODERS
51
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52
static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
53
#endif //CONFIG_ENCODERS
54

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

    
57

    
58
/* enable all paranoid tests for rounding, overflows, etc... */
59
//#define PARANOID
60

    
61
//#define DEBUG
62

    
63

    
64
/* for jpeg fast DCT */
65
#define CONST_BITS 14
66

    
67
static const uint16_t aanscales[64] = {
68
    /* precomputed values scaled up by 14 bits */
69
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
70
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
71
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
72
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
73
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
74
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
75
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
76
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
77
};
78

    
79
static const uint8_t h263_chroma_roundtab[16] = {
80
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
81
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
82
};
83

    
84
#ifdef CONFIG_ENCODERS
85
static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
86
static uint8_t default_fcode_tab[MAX_MV*2+1];
87

    
88
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
89

    
90
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
91
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax)
92
{
93
    int qscale;
94

    
95
    for(qscale=qmin; qscale<=qmax; qscale++){
96
        int i;
97
        if (s->dsp.fdct == ff_jpeg_fdct_islow) {
98
            for(i=0;i<64;i++) {
99
                const int j= s->dsp.idct_permutation[i];
100
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
101
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
102
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
103
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
104
                
105
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
106
                                (qscale * quant_matrix[j]));
107
            }
108
        } else if (s->dsp.fdct == fdct_ifast) {
109
            for(i=0;i<64;i++) {
110
                const int j= s->dsp.idct_permutation[i];
111
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
112
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
113
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
115
                
116
                qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 
117
                                (aanscales[i] * qscale * quant_matrix[j]));
118
            }
119
        } else {
120
            for(i=0;i<64;i++) {
121
                const int j= s->dsp.idct_permutation[i];
122
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
123
                   So 16           <= qscale * quant_matrix[i]             <= 7905
124
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
125
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
126
                */
127
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
128
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
129
                qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
130

    
131
                if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
132
                qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
133
            }
134
        }
135
    }
136
}
137
#endif //CONFIG_ENCODERS
138

    
139
// move into common.c perhaps 
140
#define CHECKED_ALLOCZ(p, size)\
141
{\
142
    p= av_mallocz(size);\
143
    if(p==NULL){\
144
        perror("malloc");\
145
        goto fail;\
146
    }\
147
}
148

    
149
void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
150
    int i;
151
    int end;
152
    
153
    st->scantable= src_scantable;
154

    
155
    for(i=0; i<64; i++){
156
        int j;
157
        j = src_scantable[i];
158
        st->permutated[i] = s->dsp.idct_permutation[j];
159
#ifdef ARCH_POWERPC
160
        st->inverse[j] = i;
161
#endif
162
    }
163
    
164
    end=-1;
165
    for(i=0; i<64; i++){
166
        int j;
167
        j = st->permutated[i];
168
        if(j>end) end=j;
169
        st->raster_end[i]= end;
170
    }
171
}
172

    
173
/* init common dct for both encoder and decoder */
174
int DCT_common_init(MpegEncContext *s)
175
{
176
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
177
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
178
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
179

    
180
#ifdef CONFIG_ENCODERS
181
    s->dct_quantize= dct_quantize_c;
182
#endif
183
        
184
#ifdef HAVE_MMX
185
    MPV_common_init_mmx(s);
186
#endif
187
#ifdef ARCH_ALPHA
188
    MPV_common_init_axp(s);
189
#endif
190
#ifdef HAVE_MLIB
191
    MPV_common_init_mlib(s);
192
#endif
193
#ifdef HAVE_MMI
194
    MPV_common_init_mmi(s);
195
#endif
196
#ifdef ARCH_ARMV4L
197
    MPV_common_init_armv4l(s);
198
#endif
199
#ifdef ARCH_POWERPC
200
    MPV_common_init_ppc(s);
201
#endif
202

    
203
#ifdef CONFIG_ENCODERS
204
    s->fast_dct_quantize= s->dct_quantize;
205

    
206
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
207
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
208
    }
209

    
210
#endif //CONFIG_ENCODERS
211

    
212
    /* load & permutate scantables
213
       note: only wmv uses differnt ones 
214
    */
215
    ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
216
    ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
217
    ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218
    ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
219

    
220
    s->picture_structure= PICT_FRAME;
221
    
222
    return 0;
223
}
224

    
225
/**
226
 * allocates a Picture
227
 * The pixels are allocated/set by calling get_buffer() if shared=0
228
 */
229
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
230
    
231
    if(shared){
232
        assert(pic->data[0]);
233
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
234
        pic->type= FF_BUFFER_TYPE_SHARED;
235
    }else{
236
        int r;
237
        
238
        assert(!pic->data[0]);
239
        
240
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
241
        
242
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
243
            fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
244
            return -1;
245
        }
246

    
247
        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
248
            fprintf(stderr, "get_buffer() failed (stride changed)\n");
249
            return -1;
250
        }
251

    
252
        if(pic->linesize[1] != pic->linesize[2]){
253
            fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
254
            return -1;
255
        }
256

    
257
        s->linesize  = pic->linesize[0];
258
        s->uvlinesize= pic->linesize[1];
259
    }
260
    
261
    if(pic->qscale_table==NULL){
262
        if (s->encoding) {        
263
            CHECKED_ALLOCZ(pic->mb_var   , s->mb_num * sizeof(int16_t))
264
            CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
265
            CHECKED_ALLOCZ(pic->mb_mean  , s->mb_num * sizeof(int8_t))
266
            CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
267
        }
268

    
269
        CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
270
        CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
271
        pic->qstride= s->mb_width;
272
    }
273
    
274
    //it might be nicer if the application would keep track of these but it would require a API change
275
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
276
    s->prev_pict_types[0]= s->pict_type;
277
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
278
        pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
279
    
280
    return 0;
281
fail: //for the CHECKED_ALLOCZ macro
282
    return -1;
283
}
284

    
285
/**
286
 * deallocates a picture
287
 */
288
static void free_picture(MpegEncContext *s, Picture *pic){
289
    int i;
290

    
291
    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
292
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
293
    }
294

    
295
    av_freep(&pic->mb_var);
296
    av_freep(&pic->mc_mb_var);
297
    av_freep(&pic->mb_mean);
298
    av_freep(&pic->mb_cmp_score);
299
    av_freep(&pic->mbskip_table);
300
    av_freep(&pic->qscale_table);
301
    
302
    if(pic->type == FF_BUFFER_TYPE_INTERNAL){
303
        for(i=0; i<4; i++){
304
            av_freep(&pic->base[i]);
305
            pic->data[i]= NULL;
306
        }
307
        av_freep(&pic->opaque);
308
        pic->type= 0;
309
    }else if(pic->type == FF_BUFFER_TYPE_SHARED){
310
        for(i=0; i<4; i++){
311
            pic->base[i]=
312
            pic->data[i]= NULL;
313
        }
314
        pic->type= 0;        
315
    }
316
}
317

    
318
/* init common structure for both encoder and decoder */
319
int MPV_common_init(MpegEncContext *s)
320
{
321
    int y_size, c_size, yc_size, i;
322

    
323
    dsputil_init(&s->dsp, s->avctx);
324
    DCT_common_init(s);
325

    
326
    s->flags= s->avctx->flags;
327

    
328
    s->mb_width  = (s->width  + 15) / 16;
329
    s->mb_height = (s->height + 15) / 16;
330

    
331
    /* set default edge pos, will be overriden in decode_header if needed */
332
    s->h_edge_pos= s->mb_width*16;
333
    s->v_edge_pos= s->mb_height*16;
334

    
335
    s->mb_num = s->mb_width * s->mb_height;
336

    
337
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
338
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
339
    yc_size = y_size + 2 * c_size;
340

    
341
    /* convert fourcc to upper case */
342
    s->avctx->codec_tag=   toupper( s->avctx->codec_tag     &0xFF)          
343
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
344
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 
345
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
346

    
347
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
348
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
349

    
350
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
351

    
352
    if (s->encoding) {
353
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
354

    
355
        /* Allocate MV tables */
356
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(int16_t))
357
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(int16_t))
358
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(int16_t))
359
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
360
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
361
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(int16_t))
362

    
363
        //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
364
        CHECKED_ALLOCZ(s->me.scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
365
        
366
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
367
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
368

    
369
        if(s->codec_id==CODEC_ID_MPEG4){
370
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
371
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
372
        }
373
        
374
        if(s->msmpeg4_version){
375
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
376
        }
377
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
378
    }
379
        
380
    CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
381
    
382
    if (s->out_format == FMT_H263 || s->encoding) {
383
        int size;
384
        /* Allocate MB type table */
385
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(uint8_t))
386

    
387
        /* MV prediction */
388
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
389
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
390
    }
391

    
392
    if(s->codec_id==CODEC_ID_MPEG4){
393
        /* interlaced direct mode decoding tables */
394
        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
395
        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
396
    }
397
    /* 4mv b frame decoding table */
398
    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
399
    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
400
    if (s->out_format == FMT_H263) {
401
        /* ac values */
402
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
403
        s->ac_val[1] = s->ac_val[0] + y_size;
404
        s->ac_val[2] = s->ac_val[1] + c_size;
405
        
406
        /* cbp values */
407
        CHECKED_ALLOCZ(s->coded_block, y_size);
408
        
409
        /* divx501 bitstream reorder buffer */
410
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
411

    
412
        /* cbp, ac_pred, pred_dir */
413
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(uint8_t))
414
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
415
    }
416
    
417
    if (s->h263_pred || s->h263_plus || !s->encoding) {
418
        /* dc values */
419
        //MN: we need these for error resilience of intra-frames
420
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
421
        s->dc_val[1] = s->dc_val[0] + y_size;
422
        s->dc_val[2] = s->dc_val[1] + c_size;
423
        for(i=0;i<yc_size;i++)
424
            s->dc_val[0][i] = 1024;
425
    }
426

    
427
    /* which mb is a intra block */
428
    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
429
    memset(s->mbintra_table, 1, s->mb_num);
430
    
431
    /* default structure is frame */
432
    s->picture_structure = PICT_FRAME;
433
    
434
    /* init macroblock skip table */
435
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
436
    //Note the +1 is for a quicker mpeg4 slice_end detection
437
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
438
    
439
    s->block= s->blocks[0];
440

    
441
    s->parse_context.state= -1;
442

    
443
    s->context_initialized = 1;
444
    return 0;
445
 fail:
446
    MPV_common_end(s);
447
    return -1;
448
}
449

    
450

    
451
//extern int sads;
452

    
453
/* init common structure for both encoder and decoder */
454
void MPV_common_end(MpegEncContext *s)
455
{
456
    int i;
457

    
458
    av_freep(&s->mb_type);
459
    av_freep(&s->p_mv_table);
460
    av_freep(&s->b_forw_mv_table);
461
    av_freep(&s->b_back_mv_table);
462
    av_freep(&s->b_bidir_forw_mv_table);
463
    av_freep(&s->b_bidir_back_mv_table);
464
    av_freep(&s->b_direct_mv_table);
465
    av_freep(&s->motion_val);
466
    av_freep(&s->dc_val[0]);
467
    av_freep(&s->ac_val[0]);
468
    av_freep(&s->coded_block);
469
    av_freep(&s->mbintra_table);
470
    av_freep(&s->cbp_table);
471
    av_freep(&s->pred_dir_table);
472
    av_freep(&s->me.scratchpad);
473
    av_freep(&s->me.map);
474
    av_freep(&s->me.score_map);
475
    
476
    av_freep(&s->mbskip_table);
477
    av_freep(&s->prev_pict_types);
478
    av_freep(&s->bitstream_buffer);
479
    av_freep(&s->tex_pb_buffer);
480
    av_freep(&s->pb2_buffer);
481
    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
482
    av_freep(&s->co_located_type_table);
483
    av_freep(&s->field_mv_table);
484
    av_freep(&s->field_select_table);
485
    av_freep(&s->avctx->stats_out);
486
    av_freep(&s->ac_stats);
487
    av_freep(&s->error_status_table);
488

    
489
    for(i=0; i<MAX_PICTURE_COUNT; i++){
490
        free_picture(s, &s->picture[i]);
491
    }
492
    s->context_initialized = 0;
493
}
494

    
495
#ifdef CONFIG_ENCODERS
496

    
497
/* init video encoder */
498
int MPV_encode_init(AVCodecContext *avctx)
499
{
500
    MpegEncContext *s = avctx->priv_data;
501
    int i;
502

    
503
    avctx->pix_fmt = PIX_FMT_YUV420P;
504

    
505
    s->bit_rate = avctx->bit_rate;
506
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
507
    s->width = avctx->width;
508
    s->height = avctx->height;
509
    if(avctx->gop_size > 600){
510
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
511
        avctx->gop_size=600;
512
    }
513
    s->gop_size = avctx->gop_size;
514
    s->rtp_mode = avctx->rtp_mode;
515
    s->rtp_payload_size = avctx->rtp_payload_size;
516
    if (avctx->rtp_callback)
517
        s->rtp_callback = avctx->rtp_callback;
518
    s->max_qdiff= avctx->max_qdiff;
519
    s->qcompress= avctx->qcompress;
520
    s->qblur= avctx->qblur;
521
    s->avctx = avctx;
522
    s->flags= avctx->flags;
523
    s->max_b_frames= avctx->max_b_frames;
524
    s->b_frame_strategy= avctx->b_frame_strategy;
525
    s->codec_id= avctx->codec->id;
526
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
527
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
528
    s->strict_std_compliance= avctx->strict_std_compliance;
529
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
530
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
531
    s->mpeg_quant= avctx->mpeg_quant;
532

    
533
    if (s->gop_size <= 1) {
534
        s->intra_only = 1;
535
        s->gop_size = 12;
536
    } else {
537
        s->intra_only = 0;
538
    }
539

    
540
    s->me_method = avctx->me_method;
541

    
542
    /* Fixed QSCALE */
543
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
544
    
545
    s->adaptive_quant= (   s->avctx->lumi_masking
546
                        || s->avctx->dark_masking
547
                        || s->avctx->temporal_cplx_masking 
548
                        || s->avctx->spatial_cplx_masking
549
                        || s->avctx->p_masking)
550
                       && !s->fixed_qscale;
551
    
552
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
553

    
554
    switch(avctx->codec->id) {
555
    case CODEC_ID_MPEG1VIDEO:
556
        s->out_format = FMT_MPEG1;
557
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
558
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
559
        break;
560
    case CODEC_ID_MJPEG:
561
        s->out_format = FMT_MJPEG;
562
        s->intra_only = 1; /* force intra only for jpeg */
563
        s->mjpeg_write_tables = 1; /* write all tables */
564
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
565
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
566
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
567
        s->mjpeg_vsample[2] = 1; 
568
        s->mjpeg_hsample[0] = 2;
569
        s->mjpeg_hsample[1] = 1; 
570
        s->mjpeg_hsample[2] = 1; 
571
        if (mjpeg_init(s) < 0)
572
            return -1;
573
        avctx->delay=0;
574
        s->low_delay=1;
575
        break;
576
#ifdef CONFIG_RISKY
577
    case CODEC_ID_H263:
578
        if (h263_get_picture_format(s->width, s->height) == 7) {
579
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
580
            return -1;
581
        }
582
        s->out_format = FMT_H263;
583
        avctx->delay=0;
584
        s->low_delay=1;
585
        break;
586
    case CODEC_ID_H263P:
587
        s->out_format = FMT_H263;
588
        s->h263_plus = 1;
589
        /* Fx */
590
        s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
591
        s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
592
        /* /Fx */
593
        /* These are just to be sure */
594
        s->umvplus = 1;
595
        avctx->delay=0;
596
        s->low_delay=1;
597
        break;
598
    case CODEC_ID_RV10:
599
        s->out_format = FMT_H263;
600
        s->h263_rv10 = 1;
601
        avctx->delay=0;
602
        s->low_delay=1;
603
        break;
604
    case CODEC_ID_MPEG4:
605
        s->out_format = FMT_H263;
606
        s->h263_pred = 1;
607
        s->unrestricted_mv = 1;
608
        s->low_delay= s->max_b_frames ? 0 : 1;
609
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
610
        break;
611
    case CODEC_ID_MSMPEG4V1:
612
        s->out_format = FMT_H263;
613
        s->h263_msmpeg4 = 1;
614
        s->h263_pred = 1;
615
        s->unrestricted_mv = 1;
616
        s->msmpeg4_version= 1;
617
        avctx->delay=0;
618
        s->low_delay=1;
619
        break;
620
    case CODEC_ID_MSMPEG4V2:
621
        s->out_format = FMT_H263;
622
        s->h263_msmpeg4 = 1;
623
        s->h263_pred = 1;
624
        s->unrestricted_mv = 1;
625
        s->msmpeg4_version= 2;
626
        avctx->delay=0;
627
        s->low_delay=1;
628
        break;
629
    case CODEC_ID_MSMPEG4V3:
630
        s->out_format = FMT_H263;
631
        s->h263_msmpeg4 = 1;
632
        s->h263_pred = 1;
633
        s->unrestricted_mv = 1;
634
        s->msmpeg4_version= 3;
635
        avctx->delay=0;
636
        s->low_delay=1;
637
        break;
638
    case CODEC_ID_WMV1:
639
        s->out_format = FMT_H263;
640
        s->h263_msmpeg4 = 1;
641
        s->h263_pred = 1;
642
        s->unrestricted_mv = 1;
643
        s->msmpeg4_version= 4;
644
        avctx->delay=0;
645
        s->low_delay=1;
646
        break;
647
    case CODEC_ID_WMV2:
648
        s->out_format = FMT_H263;
649
        s->h263_msmpeg4 = 1;
650
        s->h263_pred = 1;
651
        s->unrestricted_mv = 1;
652
        s->msmpeg4_version= 5;
653
        avctx->delay=0;
654
        s->low_delay=1;
655
        break;
656
#endif
657
    default:
658
        return -1;
659
    }
660
    
661
    { /* set up some save defaults, some codecs might override them later */
662
        static int done=0;
663
        if(!done){
664
            int i;
665
            done=1;
666

    
667
            default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
668
            memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
669
            memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
670

    
671
            for(i=-16; i<16; i++){
672
                default_fcode_tab[i + MAX_MV]= 1;
673
            }
674
        }
675
    }
676
    s->me.mv_penalty= default_mv_penalty;
677
    s->fcode_tab= default_fcode_tab;
678
    s->y_dc_scale_table=
679
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
680
 
681
    /* dont use mv_penalty table for crap MV as it would be confused */
682
    //FIXME remove after fixing / removing old ME
683
    if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
684

    
685
    s->encoding = 1;
686

    
687
    /* init */
688
    if (MPV_common_init(s) < 0)
689
        return -1;
690
    
691
    ff_init_me(s);
692

    
693
#ifdef CONFIG_ENCODERS
694
#ifdef CONFIG_RISKY
695
    if (s->out_format == FMT_H263)
696
        h263_encode_init(s);
697
    if(s->msmpeg4_version)
698
        ff_msmpeg4_encode_init(s);
699
#endif
700
    if (s->out_format == FMT_MPEG1)
701
        ff_mpeg1_encode_init(s);
702
#endif
703

    
704
    /* init default q matrix */
705
    for(i=0;i<64;i++) {
706
        int j= s->dsp.idct_permutation[i];
707
#ifdef CONFIG_RISKY
708
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
709
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
710
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
711
        }else if(s->out_format == FMT_H263){
712
            s->intra_matrix[j] =
713
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
714
        }else
715
#endif
716
        { /* mpeg1 */
717
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
718
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
719
        }
720
    }
721

    
722
    /* precompute matrix */
723
    /* for mjpeg, we do include qscale in the matrix */
724
    if (s->out_format != FMT_MJPEG) {
725
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
726
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
727
        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
728
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
729
    }
730

    
731
    if(ff_rate_control_init(s) < 0)
732
        return -1;
733

    
734
    s->picture_number = 0;
735
    s->picture_in_gop_number = 0;
736
    s->fake_picture_number = 0;
737
    /* motion detector init */
738
    s->f_code = 1;
739
    s->b_code = 1;
740

    
741
    return 0;
742
}
743

    
744
int MPV_encode_end(AVCodecContext *avctx)
745
{
746
    MpegEncContext *s = avctx->priv_data;
747

    
748
#ifdef STATS
749
    print_stats();
750
#endif
751

    
752
    ff_rate_control_uninit(s);
753

    
754
    MPV_common_end(s);
755
    if (s->out_format == FMT_MJPEG)
756
        mjpeg_close(s);
757
      
758
    return 0;
759
}
760

    
761
#endif //CONFIG_ENCODERS
762

    
763
void init_rl(RLTable *rl)
764
{
765
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
766
    uint8_t index_run[MAX_RUN+1];
767
    int last, run, level, start, end, i;
768

    
769
    /* compute max_level[], max_run[] and index_run[] */
770
    for(last=0;last<2;last++) {
771
        if (last == 0) {
772
            start = 0;
773
            end = rl->last;
774
        } else {
775
            start = rl->last;
776
            end = rl->n;
777
        }
778

    
779
        memset(max_level, 0, MAX_RUN + 1);
780
        memset(max_run, 0, MAX_LEVEL + 1);
781
        memset(index_run, rl->n, MAX_RUN + 1);
782
        for(i=start;i<end;i++) {
783
            run = rl->table_run[i];
784
            level = rl->table_level[i];
785
            if (index_run[run] == rl->n)
786
                index_run[run] = i;
787
            if (level > max_level[run])
788
                max_level[run] = level;
789
            if (run > max_run[level])
790
                max_run[level] = run;
791
        }
792
        rl->max_level[last] = av_malloc(MAX_RUN + 1);
793
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
794
        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
795
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
796
        rl->index_run[last] = av_malloc(MAX_RUN + 1);
797
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
798
    }
799
}
800

    
801
/* draw the edges of width 'w' of an image of size width, height */
802
//FIXME check that this is ok for mpeg4 interlaced
803
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
804
{
805
    uint8_t *ptr, *last_line;
806
    int i;
807

    
808
    last_line = buf + (height - 1) * wrap;
809
    for(i=0;i<w;i++) {
810
        /* top and bottom */
811
        memcpy(buf - (i + 1) * wrap, buf, width);
812
        memcpy(last_line + (i + 1) * wrap, last_line, width);
813
    }
814
    /* left and right */
815
    ptr = buf;
816
    for(i=0;i<height;i++) {
817
        memset(ptr - w, ptr[0], w);
818
        memset(ptr + width, ptr[width-1], w);
819
        ptr += wrap;
820
    }
821
    /* corners */
822
    for(i=0;i<w;i++) {
823
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
824
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
825
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
826
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
827
    }
828
}
829

    
830
static int find_unused_picture(MpegEncContext *s, int shared){
831
    int i;
832
    
833
    if(shared){
834
        for(i=0; i<MAX_PICTURE_COUNT; i++){
835
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
836
        }
837
    }else{
838
        for(i=0; i<MAX_PICTURE_COUNT; i++){
839
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
840
        }
841
        for(i=0; i<MAX_PICTURE_COUNT; i++){
842
            if(s->picture[i].data[0]==NULL) break;
843
        }
844
    }
845

    
846
    assert(i<MAX_PICTURE_COUNT);
847
    return i;
848
}
849

    
850
/* generic function for encode/decode called before a frame is coded/decoded */
851
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
852
{
853
    int i;
854
    AVFrame *pic;
855

    
856
    s->mb_skiped = 0;
857
    
858
    /* mark&release old frames */
859
    if (s->pict_type != B_TYPE && s->last_picture_ptr) {
860
        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
861

    
862
        /* release forgotten pictures */
863
        /* if(mpeg124/h263) */
864
        if(!s->encoding){
865
            for(i=0; i<MAX_PICTURE_COUNT; i++){
866
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
867
                    fprintf(stderr, "releasing zombie picture\n");
868
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
869
                }
870
            }
871
        }
872
    }
873
    
874
alloc:
875
    if(!s->encoding){
876
        i= find_unused_picture(s, 0);
877
    
878
        pic= (AVFrame*)&s->picture[i];
879
        pic->reference= s->pict_type != B_TYPE;
880

    
881
        if(s->current_picture_ptr)
882
            pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
883
        
884
        alloc_picture(s, (Picture*)pic, 0);
885

    
886
        s->current_picture_ptr= &s->picture[i];
887
    }
888

    
889
    if (s->pict_type != B_TYPE) {
890
        s->last_picture_ptr= s->next_picture_ptr;
891
        s->next_picture_ptr= s->current_picture_ptr;
892
    }
893
    s->current_picture= *s->current_picture_ptr;
894
    if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
895
    if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
896
    if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
897
    
898
    if(s->picture_structure!=PICT_FRAME){
899
        int i;
900
        for(i=0; i<4; i++){
901
            if(s->picture_structure == PICT_BOTTOM_FIELD){
902
                 s->current_picture.data[i] += s->current_picture.linesize[i];
903
            } 
904
            s->current_picture.linesize[i] *= 2;
905
            s->last_picture.linesize[i] *=2;
906
            s->next_picture.linesize[i] *=2;
907
        }
908
    }
909
    
910
    if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
911
        fprintf(stderr, "warning: first frame is no keyframe\n");
912
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
913
        goto alloc;
914
    }
915
   
916
    s->hurry_up= s->avctx->hurry_up;
917
    s->error_resilience= avctx->error_resilience;
918

    
919
    /* set dequantizer, we cant do it during init as it might change for mpeg4
920
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
921
    if(s->out_format == FMT_H263){
922
        if(s->mpeg_quant)
923
            s->dct_unquantize = s->dct_unquantize_mpeg2;
924
        else
925
            s->dct_unquantize = s->dct_unquantize_h263;
926
    }else 
927
        s->dct_unquantize = s->dct_unquantize_mpeg1;
928

    
929
    return 0;
930
}
931

    
932
/* generic function for encode/decode called after a frame has been coded/decoded */
933
void MPV_frame_end(MpegEncContext *s)
934
{
935
    int i;
936
    /* draw edge for correct motion prediction if outside */
937
    if(s->codec_id!=CODEC_ID_SVQ1){
938
        if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
939
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
940
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
941
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
942
        }
943
    }
944
    emms_c();
945
    
946
    s->last_pict_type    = s->pict_type;
947
    if(s->pict_type!=B_TYPE){
948
        s->last_non_b_pict_type= s->pict_type;
949
    }
950
#if 0
951
        /* copy back current_picture variables */
952
    for(i=0; i<MAX_PICTURE_COUNT; i++){
953
        if(s->picture[i].data[0] == s->current_picture.data[0]){
954
            s->picture[i]= s->current_picture;
955
            break;
956
        }    
957
    }
958
    assert(i<MAX_PICTURE_COUNT);
959
#endif    
960
    s->current_picture_ptr->quality= s->qscale; //FIXME get average of qscale_table
961
    s->current_picture_ptr->pict_type= s->pict_type;
962
    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
963

    
964
    /* release non refernce frames */
965
    for(i=0; i<MAX_PICTURE_COUNT; i++){
966
        if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
967
            s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
968
    }
969
    if(s->avctx->debug&FF_DEBUG_SKIP){
970
        int x,y;        
971
        for(y=0; y<s->mb_height; y++){
972
            for(x=0; x<s->mb_width; x++){
973
                int count= s->mbskip_table[x + y*s->mb_width];
974
                if(count>9) count=9;
975
                printf(" %1d", count);
976
            }
977
            printf("\n");
978
        }
979
        printf("pict type: %d\n", s->pict_type);
980
    }
981

    
982
    // clear copies, to avoid confusion
983
#if 0
984
    memset(&s->last_picture, 0, sizeof(Picture));
985
    memset(&s->next_picture, 0, sizeof(Picture));
986
    memset(&s->current_picture, 0, sizeof(Picture));
987
#endif
988
}
989

    
990
#ifdef CONFIG_ENCODERS
991

    
992
static int get_sae(uint8_t *src, int ref, int stride){
993
    int x,y;
994
    int acc=0;
995
    
996
    for(y=0; y<16; y++){
997
        for(x=0; x<16; x++){
998
            acc+= ABS(src[x+y*stride] - ref);
999
        }
1000
    }
1001
    
1002
    return acc;
1003
}
1004

    
1005
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1006
    int x, y, w, h;
1007
    int acc=0;
1008
    
1009
    w= s->width &~15;
1010
    h= s->height&~15;
1011
    
1012
    for(y=0; y<h; y+=16){
1013
        for(x=0; x<w; x+=16){
1014
            int offset= x + y*stride;
1015
            int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1016
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1017
            int sae = get_sae(src + offset, mean, stride);
1018
            
1019
            acc+= sae + 500 < sad;
1020
        }
1021
    }
1022
    return acc;
1023
}
1024

    
1025

    
1026
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1027
    AVFrame *pic;
1028
    int i;
1029
    const int encoding_delay= s->max_b_frames;
1030
    int direct=1;
1031

    
1032
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1033
    if(pic_arg->linesize[0] != s->linesize) direct=0;
1034
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1035
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1036
  
1037
//    printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1038
    
1039
    if(direct){
1040
        i= find_unused_picture(s, 1);
1041

    
1042
        pic= (AVFrame*)&s->picture[i];
1043
        pic->reference= 1;
1044
    
1045
        for(i=0; i<4; i++){
1046
            pic->data[i]= pic_arg->data[i];
1047
            pic->linesize[i]= pic_arg->linesize[i];
1048
        }
1049
        alloc_picture(s, (Picture*)pic, 1);
1050
    }else{
1051
        i= find_unused_picture(s, 0);
1052

    
1053
        pic= (AVFrame*)&s->picture[i];
1054
        pic->reference= 1;
1055

    
1056
        alloc_picture(s, (Picture*)pic, 0);
1057
        for(i=0; i<4; i++){
1058
            /* the input will be 16 pixels to the right relative to the actual buffer start
1059
             * and the current_pic, so the buffer can be reused, yes its not beatifull 
1060
             */
1061
            pic->data[i]+= 16; 
1062
        }
1063

    
1064
        if(   pic->data[0] == pic_arg->data[0] 
1065
           && pic->data[1] == pic_arg->data[1]
1066
           && pic->data[2] == pic_arg->data[2]){
1067
       // empty
1068
        }else{
1069
            int h_chroma_shift, v_chroma_shift;
1070
        
1071
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1072
        
1073
            for(i=0; i<3; i++){
1074
                int src_stride= pic_arg->linesize[i];
1075
                int dst_stride= i ? s->uvlinesize : s->linesize;
1076
                int h_shift= i ? h_chroma_shift : 0;
1077
                int v_shift= i ? v_chroma_shift : 0;
1078
                int w= s->width >>h_shift;
1079
                int h= s->height>>v_shift;
1080
                uint8_t *src= pic_arg->data[i];
1081
                uint8_t *dst= pic->data[i];
1082
            
1083
                if(src_stride==dst_stride)
1084
                    memcpy(dst, src, src_stride*h);
1085
                else{
1086
                    while(h--){
1087
                        memcpy(dst, src, w);
1088
                        dst += dst_stride;
1089
                        src += src_stride;
1090
                    }
1091
                }
1092
            }
1093
        }
1094
    }
1095
    pic->quality= pic_arg->quality;
1096
    pic->pict_type= pic_arg->pict_type;
1097
    pic->pts = pic_arg->pts;
1098
    
1099
    if(s->input_picture[encoding_delay])
1100
        pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1101

    
1102
    /* shift buffer entries */
1103
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1104
        s->input_picture[i-1]= s->input_picture[i];
1105
        
1106
    s->input_picture[encoding_delay]= (Picture*)pic;
1107

    
1108
    return 0;
1109
}
1110

    
1111
static void select_input_picture(MpegEncContext *s){
1112
    int i;
1113
    const int encoding_delay= s->max_b_frames;
1114
    int coded_pic_num=0;    
1115

    
1116
    if(s->reordered_input_picture[0])
1117
        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1118

    
1119
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1120
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1121
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1122

    
1123
    /* set next picture types & ordering */
1124
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1125
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1126
            s->reordered_input_picture[0]= s->input_picture[0];
1127
            s->reordered_input_picture[0]->pict_type= I_TYPE;
1128
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1129
        }else{
1130
            int b_frames;
1131
            
1132
            if(s->flags&CODEC_FLAG_PASS2){
1133
                for(i=0; i<s->max_b_frames+1; i++){
1134
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1135
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1136
                    s->input_picture[i]->pict_type= pict_type;
1137
                    
1138
                    if(i + 1 >= s->rc_context.num_entries) break;
1139
                }
1140
            }
1141

    
1142
            if(s->input_picture[0]->pict_type){
1143
                /* user selected pict_type */
1144
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1145
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1146
                }
1147
            
1148
                if(b_frames > s->max_b_frames){
1149
                    fprintf(stderr, "warning, too many bframes in a row\n");
1150
                    b_frames = s->max_b_frames;
1151
                }
1152
            }else if(s->b_frame_strategy==0){
1153
                b_frames= s->max_b_frames;
1154
            }else if(s->b_frame_strategy==1){
1155
                for(i=1; i<s->max_b_frames+1; i++){
1156
                    if(s->input_picture[i]->b_frame_score==0){
1157
                        s->input_picture[i]->b_frame_score= 
1158
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
1159
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1160
                    }
1161
                }
1162
                for(i=0; i<s->max_b_frames; i++){
1163
                    if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1164
                }
1165
                                
1166
                b_frames= FFMAX(0, i-1);
1167
                
1168
                /* reset scores */
1169
                for(i=0; i<b_frames+1; i++){
1170
                    s->input_picture[i]->b_frame_score=0;
1171
                }
1172
            }else{
1173
                fprintf(stderr, "illegal b frame strategy\n");
1174
                b_frames=0;
1175
            }
1176

    
1177
            emms_c();
1178
//static int b_count=0;
1179
//b_count+= b_frames;
1180
//printf("b_frames: %d\n", b_count);
1181
                        
1182
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1183
            if(   s->picture_in_gop_number + b_frames >= s->gop_size 
1184
               || s->reordered_input_picture[0]->pict_type== I_TYPE)
1185
                s->reordered_input_picture[0]->pict_type= I_TYPE;
1186
            else
1187
                s->reordered_input_picture[0]->pict_type= P_TYPE;
1188
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1189
            for(i=0; i<b_frames; i++){
1190
                coded_pic_num++;
1191
                s->reordered_input_picture[i+1]= s->input_picture[i];
1192
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1193
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1194
            }
1195
        }
1196
    }
1197
    
1198
    if(s->reordered_input_picture[0]){
1199
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
1200

    
1201
        s->new_picture= *s->reordered_input_picture[0];
1202

    
1203
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1204
            // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1205
        
1206
            int i= find_unused_picture(s, 0);
1207
            Picture *pic= &s->picture[i];
1208

    
1209
            /* mark us unused / free shared pic */
1210
            for(i=0; i<4; i++)
1211
                s->reordered_input_picture[0]->data[i]= NULL;
1212
            s->reordered_input_picture[0]->type= 0;
1213
            
1214
            //FIXME bad, copy * except
1215
            pic->pict_type = s->reordered_input_picture[0]->pict_type;
1216
            pic->quality   = s->reordered_input_picture[0]->quality;
1217
            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1218
            pic->reference = s->reordered_input_picture[0]->reference;
1219
            
1220
            alloc_picture(s, pic, 0);
1221

    
1222
            s->current_picture_ptr= pic;
1223
        }else{
1224
            // input is not a shared pix -> reuse buffer for current_pix
1225

    
1226
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
1227
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1228
            
1229
            s->current_picture_ptr= s->reordered_input_picture[0];
1230
            for(i=0; i<4; i++){
1231
                //reverse the +16 we did before storing the input
1232
                s->current_picture_ptr->data[i]-=16;
1233
            }
1234
        }
1235
        s->current_picture= *s->current_picture_ptr;
1236
    
1237
        s->picture_number= s->new_picture.display_picture_number;
1238
//printf("dpn:%d\n", s->picture_number);
1239
    }else{
1240
       memset(&s->new_picture, 0, sizeof(Picture));
1241
    }
1242
}
1243

    
1244
int MPV_encode_picture(AVCodecContext *avctx,
1245
                       unsigned char *buf, int buf_size, void *data)
1246
{
1247
    MpegEncContext *s = avctx->priv_data;
1248
    AVFrame *pic_arg = data;
1249
    int i;
1250

    
1251
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1252

    
1253
    s->picture_in_gop_number++;
1254

    
1255
    load_input_picture(s, pic_arg);
1256
    
1257
    select_input_picture(s);
1258
    
1259
    /* output? */
1260
    if(s->new_picture.data[0]){
1261

    
1262
        s->pict_type= s->new_picture.pict_type;
1263
        if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1264
            s->qscale= (int)(s->new_picture.quality+0.5);
1265
            assert(s->qscale);
1266
        }
1267
//emms_c();
1268
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1269
        MPV_frame_start(s, avctx);
1270

    
1271
        encode_picture(s, s->picture_number);
1272
        
1273
        avctx->real_pict_num  = s->picture_number;
1274
        avctx->header_bits = s->header_bits;
1275
        avctx->mv_bits     = s->mv_bits;
1276
        avctx->misc_bits   = s->misc_bits;
1277
        avctx->i_tex_bits  = s->i_tex_bits;
1278
        avctx->p_tex_bits  = s->p_tex_bits;
1279
        avctx->i_count     = s->i_count;
1280
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1281
        avctx->skip_count  = s->skip_count;
1282

    
1283
        MPV_frame_end(s);
1284

    
1285
        if (s->out_format == FMT_MJPEG)
1286
            mjpeg_picture_trailer(s);
1287
        
1288
        if(s->flags&CODEC_FLAG_PASS1)
1289
            ff_write_pass1_stats(s);
1290

    
1291
        for(i=0; i<4; i++){
1292
            avctx->error[i] += s->current_picture_ptr->error[i];
1293
        }
1294
    }
1295

    
1296
    s->input_picture_number++;
1297

    
1298
    flush_put_bits(&s->pb);
1299
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1300
    
1301
    s->total_bits += s->frame_bits;
1302
    avctx->frame_bits  = s->frame_bits;
1303
    
1304
    return pbBufPtr(&s->pb) - s->pb.buf;
1305
}
1306

    
1307
#endif //CONFIG_ENCODERS
1308

    
1309
static inline void gmc1_motion(MpegEncContext *s,
1310
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1311
                               int dest_offset,
1312
                               uint8_t **ref_picture, int src_offset)
1313
{
1314
    uint8_t *ptr;
1315
    int offset, src_x, src_y, linesize, uvlinesize;
1316
    int motion_x, motion_y;
1317
    int emu=0;
1318

    
1319
    motion_x= s->sprite_offset[0][0];
1320
    motion_y= s->sprite_offset[0][1];
1321
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1322
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1323
    motion_x<<=(3-s->sprite_warping_accuracy);
1324
    motion_y<<=(3-s->sprite_warping_accuracy);
1325
    src_x = clip(src_x, -16, s->width);
1326
    if (src_x == s->width)
1327
        motion_x =0;
1328
    src_y = clip(src_y, -16, s->height);
1329
    if (src_y == s->height)
1330
        motion_y =0;
1331

    
1332
    linesize = s->linesize;
1333
    uvlinesize = s->uvlinesize;
1334
    
1335
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1336

    
1337
    dest_y+=dest_offset;
1338
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1339
        if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1340
                              || src_y + 17 >= s->v_edge_pos){
1341
            ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1342
            ptr= s->edge_emu_buffer;
1343
        }
1344
    }
1345
    
1346
    if((motion_x|motion_y)&7){
1347
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1348
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1349
    }else{
1350
        int dxy;
1351
        
1352
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1353
        if (s->no_rounding){
1354
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1355
        }else{
1356
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1357
        }
1358
    }
1359
    
1360
    if(s->flags&CODEC_FLAG_GRAY) return;
1361

    
1362
    motion_x= s->sprite_offset[1][0];
1363
    motion_y= s->sprite_offset[1][1];
1364
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1365
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1366
    motion_x<<=(3-s->sprite_warping_accuracy);
1367
    motion_y<<=(3-s->sprite_warping_accuracy);
1368
    src_x = clip(src_x, -8, s->width>>1);
1369
    if (src_x == s->width>>1)
1370
        motion_x =0;
1371
    src_y = clip(src_y, -8, s->height>>1);
1372
    if (src_y == s->height>>1)
1373
        motion_y =0;
1374

    
1375
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1376
    ptr = ref_picture[1] + offset;
1377
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1378
        if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1379
                              || src_y + 9 >= s->v_edge_pos>>1){
1380
            ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1381
            ptr= s->edge_emu_buffer;
1382
            emu=1;
1383
        }
1384
    }
1385
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1386
    
1387
    ptr = ref_picture[2] + offset;
1388
    if(emu){
1389
        ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1390
        ptr= s->edge_emu_buffer;
1391
    }
1392
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1393
    
1394
    return;
1395
}
1396

    
1397
static inline void gmc_motion(MpegEncContext *s,
1398
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1399
                               int dest_offset,
1400
                               uint8_t **ref_picture, int src_offset)
1401
{
1402
    uint8_t *ptr;
1403
    int linesize, uvlinesize;
1404
    const int a= s->sprite_warping_accuracy;
1405
    int ox, oy;
1406

    
1407
    linesize = s->linesize;
1408
    uvlinesize = s->uvlinesize;
1409

    
1410
    ptr = ref_picture[0] + src_offset;
1411

    
1412
    dest_y+=dest_offset;
1413
    
1414
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1415
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1416

    
1417
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1418
           ox, 
1419
           oy, 
1420
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1421
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1422
           a+1, (1<<(2*a+1)) - s->no_rounding,
1423
           s->h_edge_pos, s->v_edge_pos);
1424
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1425
           ox + s->sprite_delta[0][0]*8, 
1426
           oy + s->sprite_delta[1][0]*8, 
1427
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1428
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1429
           a+1, (1<<(2*a+1)) - s->no_rounding,
1430
           s->h_edge_pos, s->v_edge_pos);
1431

    
1432
    if(s->flags&CODEC_FLAG_GRAY) return;
1433

    
1434

    
1435
    dest_cb+=dest_offset>>1;
1436
    dest_cr+=dest_offset>>1;
1437
    
1438
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1439
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1440

    
1441
    ptr = ref_picture[1] + (src_offset>>1);
1442
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1443
           ox, 
1444
           oy, 
1445
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1446
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1447
           a+1, (1<<(2*a+1)) - s->no_rounding,
1448
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1449
    
1450
    ptr = ref_picture[2] + (src_offset>>1);
1451
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1452
           ox, 
1453
           oy, 
1454
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1455
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1456
           a+1, (1<<(2*a+1)) - s->no_rounding,
1457
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1458
}
1459

    
1460

    
1461
void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h, 
1462
                                    int src_x, int src_y, int w, int h){
1463
    int x, y;
1464
    int start_y, start_x, end_y, end_x;
1465
    uint8_t *buf= s->edge_emu_buffer;
1466

    
1467
    if(src_y>= h){
1468
        src+= (h-1-src_y)*linesize;
1469
        src_y=h-1;
1470
    }else if(src_y<=-block_h){
1471
        src+= (1-block_h-src_y)*linesize;
1472
        src_y=1-block_h;
1473
    }
1474
    if(src_x>= w){
1475
        src+= (w-1-src_x);
1476
        src_x=w-1;
1477
    }else if(src_x<=-block_w){
1478
        src+= (1-block_w-src_x);
1479
        src_x=1-block_w;
1480
    }
1481

    
1482
    start_y= FFMAX(0, -src_y);
1483
    start_x= FFMAX(0, -src_x);
1484
    end_y= FFMIN(block_h, h-src_y);
1485
    end_x= FFMIN(block_w, w-src_x);
1486

    
1487
    // copy existing part
1488
    for(y=start_y; y<end_y; y++){
1489
        for(x=start_x; x<end_x; x++){
1490
            buf[x + y*linesize]= src[x + y*linesize];
1491
        }
1492
    }
1493

    
1494
    //top
1495
    for(y=0; y<start_y; y++){
1496
        for(x=start_x; x<end_x; x++){
1497
            buf[x + y*linesize]= buf[x + start_y*linesize];
1498
        }
1499
    }
1500

    
1501
    //bottom
1502
    for(y=end_y; y<block_h; y++){
1503
        for(x=start_x; x<end_x; x++){
1504
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1505
        }
1506
    }
1507
                                    
1508
    for(y=0; y<block_h; y++){
1509
       //left
1510
        for(x=0; x<start_x; x++){
1511
            buf[x + y*linesize]= buf[start_x + y*linesize];
1512
        }
1513
       
1514
       //right
1515
        for(x=end_x; x<block_w; x++){
1516
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1517
        }
1518
    }
1519
}
1520

    
1521

    
1522
/* apply one mpeg motion vector to the three components */
1523
static inline void mpeg_motion(MpegEncContext *s,
1524
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1525
                               int dest_offset,
1526
                               uint8_t **ref_picture, int src_offset,
1527
                               int field_based, op_pixels_func (*pix_op)[4],
1528
                               int motion_x, int motion_y, int h)
1529
{
1530
    uint8_t *ptr;
1531
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1532
    int emu=0;
1533
#if 0    
1534
if(s->quarter_sample)
1535
{
1536
    motion_x>>=1;
1537
    motion_y>>=1;
1538
}
1539
#endif
1540
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1541
    src_x = s->mb_x * 16 + (motion_x >> 1);
1542
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1543
                
1544
    /* WARNING: do no forget half pels */
1545
    height = s->height >> field_based;
1546
    v_edge_pos = s->v_edge_pos >> field_based;
1547
    src_x = clip(src_x, -16, s->width);
1548
    if (src_x == s->width)
1549
        dxy &= ~1;
1550
    src_y = clip(src_y, -16, height);
1551
    if (src_y == height)
1552
        dxy &= ~2;
1553
    linesize   = s->current_picture.linesize[0] << field_based;
1554
    uvlinesize = s->current_picture.linesize[1] << field_based;
1555
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1556
    dest_y += dest_offset;
1557

    
1558
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1559
        if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1560
                              || src_y + (motion_y&1) + h  > v_edge_pos){
1561
            ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,  //FIXME linesize? and uv below
1562
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1563
            ptr= s->edge_emu_buffer + src_offset;
1564
            emu=1;
1565
        }
1566
    }
1567
    pix_op[0][dxy](dest_y, ptr, linesize, h);
1568

    
1569
    if(s->flags&CODEC_FLAG_GRAY) return;
1570

    
1571
    if (s->out_format == FMT_H263) {
1572
        dxy = 0;
1573
        if ((motion_x & 3) != 0)
1574
            dxy |= 1;
1575
        if ((motion_y & 3) != 0)
1576
            dxy |= 2;
1577
        mx = motion_x >> 2;
1578
        my = motion_y >> 2;
1579
    } else {
1580
        mx = motion_x / 2;
1581
        my = motion_y / 2;
1582
        dxy = ((my & 1) << 1) | (mx & 1);
1583
        mx >>= 1;
1584
        my >>= 1;
1585
    }
1586
    
1587
    src_x = s->mb_x * 8 + mx;
1588
    src_y = s->mb_y * (8 >> field_based) + my;
1589
    src_x = clip(src_x, -8, s->width >> 1);
1590
    if (src_x == (s->width >> 1))
1591
        dxy &= ~1;
1592
    src_y = clip(src_y, -8, height >> 1);
1593
    if (src_y == (height >> 1))
1594
        dxy &= ~2;
1595
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1596
    ptr = ref_picture[1] + offset;
1597
    if(emu){
1598
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1599
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1600
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1601
    }
1602
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1603

    
1604
    ptr = ref_picture[2] + offset;
1605
    if(emu){
1606
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1607
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1608
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1609
    }
1610
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1611
}
1612

    
1613
static inline void qpel_motion(MpegEncContext *s,
1614
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1615
                               int dest_offset,
1616
                               uint8_t **ref_picture, int src_offset,
1617
                               int field_based, op_pixels_func (*pix_op)[4],
1618
                               qpel_mc_func (*qpix_op)[16],
1619
                               int motion_x, int motion_y, int h)
1620
{
1621
    uint8_t *ptr;
1622
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1623
    int emu=0;
1624

    
1625
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1626
    src_x = s->mb_x * 16 + (motion_x >> 2);
1627
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1628

    
1629
    height = s->height >> field_based;
1630
    v_edge_pos = s->v_edge_pos >> field_based;
1631
    src_x = clip(src_x, -16, s->width);
1632
    if (src_x == s->width)
1633
        dxy &= ~3;
1634
    src_y = clip(src_y, -16, height);
1635
    if (src_y == height)
1636
        dxy &= ~12;
1637
    linesize = s->linesize << field_based;
1638
    uvlinesize = s->uvlinesize << field_based;
1639
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1640
    dest_y += dest_offset;
1641
//printf("%d %d %d\n", src_x, src_y, dxy);
1642
    
1643
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1644
        if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1645
                              || src_y + (motion_y&3) + h  > v_edge_pos){
1646
            ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1647
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1648
            ptr= s->edge_emu_buffer + src_offset;
1649
            emu=1;
1650
        }
1651
    }
1652
    if(!field_based)
1653
        qpix_op[0][dxy](dest_y, ptr, linesize);
1654
    else{
1655
        //damn interlaced mode
1656
        //FIXME boundary mirroring is not exactly correct here
1657
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
1658
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1659
    }
1660

    
1661
    if(s->flags&CODEC_FLAG_GRAY) return;
1662

    
1663
    if(field_based){
1664
        mx= motion_x/2;
1665
        my= motion_y>>1;
1666
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1667
        static const int rtab[8]= {0,0,1,1,0,0,0,1};
1668
        mx= (motion_x>>1) + rtab[motion_x&7];
1669
        my= (motion_y>>1) + rtab[motion_y&7];
1670
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1671
        mx= (motion_x>>1)|(motion_x&1);
1672
        my= (motion_y>>1)|(motion_y&1);
1673
    }else{
1674
        mx= motion_x/2;
1675
        my= motion_y/2;
1676
    }
1677
    mx= (mx>>1)|(mx&1);
1678
    my= (my>>1)|(my&1);
1679

    
1680
    dxy= (mx&1) | ((my&1)<<1);
1681
    mx>>=1;
1682
    my>>=1;
1683

    
1684
    src_x = s->mb_x * 8 + mx;
1685
    src_y = s->mb_y * (8 >> field_based) + my;
1686
    src_x = clip(src_x, -8, s->width >> 1);
1687
    if (src_x == (s->width >> 1))
1688
        dxy &= ~1;
1689
    src_y = clip(src_y, -8, height >> 1);
1690
    if (src_y == (height >> 1))
1691
        dxy &= ~2;
1692

    
1693
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1694
    ptr = ref_picture[1] + offset;
1695
    if(emu){
1696
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1697
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1698
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1699
    }
1700
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1701
    
1702
    ptr = ref_picture[2] + offset;
1703
    if(emu){
1704
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1705
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1706
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1707
    }
1708
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1709
}
1710

    
1711
inline int ff_h263_round_chroma(int x){
1712
    if (x >= 0)
1713
        return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1714
    else {
1715
        x = -x;
1716
        return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1717
    }
1718
}
1719

    
1720
static inline void MPV_motion(MpegEncContext *s, 
1721
                              uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1722
                              int dir, uint8_t **ref_picture, 
1723
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1724
{
1725
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1726
    int mb_x, mb_y, i;
1727
    uint8_t *ptr, *dest;
1728
    int emu=0;
1729

    
1730
    mb_x = s->mb_x;
1731
    mb_y = s->mb_y;
1732

    
1733
    switch(s->mv_type) {
1734
    case MV_TYPE_16X16:
1735
#ifdef CONFIG_RISKY
1736
        if(s->mcsel){
1737
            if(s->real_sprite_warping_points==1){
1738
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1739
                            ref_picture, 0);
1740
            }else{
1741
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1742
                            ref_picture, 0);
1743
            }
1744
        }else if(s->quarter_sample){
1745
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1746
                        ref_picture, 0,
1747
                        0, pix_op, qpix_op,
1748
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1749
        }else if(s->mspel){
1750
            ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
1751
                        ref_picture, pix_op,
1752
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1753
        }else
1754
#endif
1755
        {
1756
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1757
                        ref_picture, 0,
1758
                        0, pix_op,
1759
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1760
        }           
1761
        break;
1762
    case MV_TYPE_8X8:
1763
        mx = 0;
1764
        my = 0;
1765
        if(s->quarter_sample){
1766
            for(i=0;i<4;i++) {
1767
                motion_x = s->mv[dir][i][0];
1768
                motion_y = s->mv[dir][i][1];
1769

    
1770
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1771
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1772
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1773
                    
1774
                /* WARNING: do no forget half pels */
1775
                src_x = clip(src_x, -16, s->width);
1776
                if (src_x == s->width)
1777
                    dxy &= ~3;
1778
                src_y = clip(src_y, -16, s->height);
1779
                if (src_y == s->height)
1780
                    dxy &= ~12;
1781
                    
1782
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1783
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1784
                    if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1785
                                          || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1786
                        ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1787
                        ptr= s->edge_emu_buffer;
1788
                    }
1789
                }
1790
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1791
                qpix_op[1][dxy](dest, ptr, s->linesize);
1792

    
1793
                mx += s->mv[dir][i][0]/2;
1794
                my += s->mv[dir][i][1]/2;
1795
            }
1796
        }else{
1797
            for(i=0;i<4;i++) {
1798
                motion_x = s->mv[dir][i][0];
1799
                motion_y = s->mv[dir][i][1];
1800

    
1801
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1802
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1803
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1804
                    
1805
                /* WARNING: do no forget half pels */
1806
                src_x = clip(src_x, -16, s->width);
1807
                if (src_x == s->width)
1808
                    dxy &= ~1;
1809
                src_y = clip(src_y, -16, s->height);
1810
                if (src_y == s->height)
1811
                    dxy &= ~2;
1812
                    
1813
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1814
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1815
                    if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1816
                                          || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1817
                        ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1818
                        ptr= s->edge_emu_buffer;
1819
                    }
1820
                }
1821
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1822
                pix_op[1][dxy](dest, ptr, s->linesize, 8);
1823

    
1824
                mx += s->mv[dir][i][0];
1825
                my += s->mv[dir][i][1];
1826
            }
1827
        }
1828

    
1829
        if(s->flags&CODEC_FLAG_GRAY) break;
1830
        /* In case of 8X8, we construct a single chroma motion vector
1831
           with a special rounding */
1832
        mx= ff_h263_round_chroma(mx);
1833
        my= ff_h263_round_chroma(my);
1834
        dxy = ((my & 1) << 1) | (mx & 1);
1835
        mx >>= 1;
1836
        my >>= 1;
1837

    
1838
        src_x = mb_x * 8 + mx;
1839
        src_y = mb_y * 8 + my;
1840
        src_x = clip(src_x, -8, s->width/2);
1841
        if (src_x == s->width/2)
1842
            dxy &= ~1;
1843
        src_y = clip(src_y, -8, s->height/2);
1844
        if (src_y == s->height/2)
1845
            dxy &= ~2;
1846
        
1847
        offset = (src_y * (s->uvlinesize)) + src_x;
1848
        ptr = ref_picture[1] + offset;
1849
        if(s->flags&CODEC_FLAG_EMU_EDGE){
1850
                if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1851
                                      || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1852
                    ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1853
                    ptr= s->edge_emu_buffer;
1854
                    emu=1;
1855
                }
1856
            }
1857
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1858

    
1859
        ptr = ref_picture[2] + offset;
1860
        if(emu){
1861
            ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1862
            ptr= s->edge_emu_buffer;
1863
        }
1864
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1865
        break;
1866
    case MV_TYPE_FIELD:
1867
        if (s->picture_structure == PICT_FRAME) {
1868
            if(s->quarter_sample){
1869
                /* top field */
1870
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1871
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1872
                            1, pix_op, qpix_op,
1873
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1874
                /* bottom field */
1875
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1876
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1877
                            1, pix_op, qpix_op,
1878
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1879
            }else{
1880
                /* top field */       
1881
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1882
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1883
                            1, pix_op,
1884
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1885
                /* bottom field */
1886
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1887
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1888
                            1, pix_op,
1889
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1890
            }
1891
        } else {
1892
            int offset;
1893
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
1894
                offset= s->field_select[dir][0] ? s->linesize : 0;
1895
            }else{
1896
                ref_picture= s->current_picture.data;
1897
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
1898
            } 
1899

    
1900
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1901
                        ref_picture, offset,
1902
                        0, pix_op,
1903
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1904
        }
1905
        break;
1906
    }
1907
}
1908

    
1909

    
1910
/* put block[] to dest[] */
1911
static inline void put_dct(MpegEncContext *s, 
1912
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1913
{
1914
    s->dct_unquantize(s, block, i, s->qscale);
1915
    s->dsp.idct_put (dest, line_size, block);
1916
}
1917

    
1918
/* add block[] to dest[] */
1919
static inline void add_dct(MpegEncContext *s, 
1920
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1921
{
1922
    if (s->block_last_index[i] >= 0) {
1923
        s->dsp.idct_add (dest, line_size, block);
1924
    }
1925
}
1926

    
1927
static inline void add_dequant_dct(MpegEncContext *s, 
1928
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1929
{
1930
    if (s->block_last_index[i] >= 0) {
1931
        s->dct_unquantize(s, block, i, s->qscale);
1932

    
1933
        s->dsp.idct_add (dest, line_size, block);
1934
    }
1935
}
1936

    
1937
/**
1938
 * cleans dc, ac, coded_block for the current non intra MB
1939
 */
1940
void ff_clean_intra_table_entries(MpegEncContext *s)
1941
{
1942
    int wrap = s->block_wrap[0];
1943
    int xy = s->block_index[0];
1944
    
1945
    s->dc_val[0][xy           ] = 
1946
    s->dc_val[0][xy + 1       ] = 
1947
    s->dc_val[0][xy     + wrap] =
1948
    s->dc_val[0][xy + 1 + wrap] = 1024;
1949
    /* ac pred */
1950
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
1951
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1952
    if (s->msmpeg4_version>=3) {
1953
        s->coded_block[xy           ] =
1954
        s->coded_block[xy + 1       ] =
1955
        s->coded_block[xy     + wrap] =
1956
        s->coded_block[xy + 1 + wrap] = 0;
1957
    }
1958
    /* chroma */
1959
    wrap = s->block_wrap[4];
1960
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1961
    s->dc_val[1][xy] =
1962
    s->dc_val[2][xy] = 1024;
1963
    /* ac pred */
1964
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1965
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1966
    
1967
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1968
}
1969

    
1970
/* generic function called after a macroblock has been parsed by the
1971
   decoder or after it has been encoded by the encoder.
1972

1973
   Important variables used:
1974
   s->mb_intra : true if intra macroblock
1975
   s->mv_dir   : motion vector direction
1976
   s->mv_type  : motion vector type
1977
   s->mv       : motion vector
1978
   s->interlaced_dct : true if interlaced dct used (mpeg2)
1979
 */
1980
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1981
{
1982
    int mb_x, mb_y;
1983
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1984

    
1985
    mb_x = s->mb_x;
1986
    mb_y = s->mb_y;
1987

    
1988
    s->current_picture.qscale_table[mb_xy]= s->qscale;
1989

    
1990
    /* update DC predictors for P macroblocks */
1991
    if (!s->mb_intra) {
1992
        if (s->h263_pred || s->h263_aic) {
1993
            if(s->mbintra_table[mb_xy])
1994
                ff_clean_intra_table_entries(s);
1995
        } else {
1996
            s->last_dc[0] =
1997
            s->last_dc[1] =
1998
            s->last_dc[2] = 128 << s->intra_dc_precision;
1999
        }
2000
    }
2001
    else if (s->h263_pred || s->h263_aic)
2002
        s->mbintra_table[mb_xy]=1;
2003

    
2004
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
2005
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
2006
        //FIXME a lot of thet is only needed for !low_delay
2007
        const int wrap = s->block_wrap[0];
2008
        const int xy = s->block_index[0];
2009
        const int mb_index= s->mb_x + s->mb_y*s->mb_width;
2010
        if(s->mv_type == MV_TYPE_8X8){
2011
            s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
2012
        } else {
2013
            int motion_x, motion_y;
2014
            if (s->mb_intra) {
2015
                motion_x = 0;
2016
                motion_y = 0;
2017
                if(s->co_located_type_table)
2018
                    s->co_located_type_table[mb_index]= 0;
2019
            } else if (s->mv_type == MV_TYPE_16X16) {
2020
                motion_x = s->mv[0][0][0];
2021
                motion_y = s->mv[0][0][1];
2022
                if(s->co_located_type_table)
2023
                    s->co_located_type_table[mb_index]= 0;
2024
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
2025
                int i;
2026
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2027
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2028
                motion_x = (motion_x>>1) | (motion_x&1);
2029
                for(i=0; i<2; i++){
2030
                    s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
2031
                    s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
2032
                    s->field_select_table[mb_index][i]= s->field_select[0][i];
2033
                }
2034
                s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
2035
            }
2036
            /* no update if 8X8 because it has been done during parsing */
2037
            s->motion_val[xy][0] = motion_x;
2038
            s->motion_val[xy][1] = motion_y;
2039
            s->motion_val[xy + 1][0] = motion_x;
2040
            s->motion_val[xy + 1][1] = motion_y;
2041
            s->motion_val[xy + wrap][0] = motion_x;
2042
            s->motion_val[xy + wrap][1] = motion_y;
2043
            s->motion_val[xy + 1 + wrap][0] = motion_x;
2044
            s->motion_val[xy + 1 + wrap][1] = motion_y;
2045
        }
2046
    }
2047
    
2048
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2049
        uint8_t *dest_y, *dest_cb, *dest_cr;
2050
        int dct_linesize, dct_offset;
2051
        op_pixels_func (*op_pix)[4];
2052
        qpel_mc_func (*op_qpix)[16];
2053
        const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2054
        const int uvlinesize= s->current_picture.linesize[1];
2055

    
2056
        /* avoid copy if macroblock skipped in last frame too */
2057
        if (s->pict_type != B_TYPE) {
2058
            s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2059
        }
2060

    
2061
        /* skip only during decoding as we might trash the buffers during encoding a bit */
2062
        if(!s->encoding){
2063
            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2064
            const int age= s->current_picture.age;
2065

    
2066
            assert(age);
2067

    
2068
            if (s->mb_skiped) {
2069
                s->mb_skiped= 0;
2070
                assert(s->pict_type!=I_TYPE);
2071
 
2072
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2073
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2074

    
2075
                /* if previous was skipped too, then nothing to do !  */
2076
                if (*mbskip_ptr >= age && s->current_picture.reference){
2077
                    return;
2078
                }
2079
            } else if(!s->current_picture.reference){
2080
                (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2081
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2082
            } else{
2083
                *mbskip_ptr = 0; /* not skipped */
2084
            }
2085
        }else
2086
            s->mb_skiped= 0;
2087

    
2088
        if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2089
            dest_y  = s->current_picture.data[0] + mb_x * 16;
2090
            dest_cb = s->current_picture.data[1] + mb_x * 8;
2091
            dest_cr = s->current_picture.data[2] + mb_x * 8;
2092
        }else{
2093
            dest_y  = s->current_picture.data[0] + (mb_y * 16* linesize  ) + mb_x * 16;
2094
            dest_cb = s->current_picture.data[1] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2095
            dest_cr = s->current_picture.data[2] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2096
        }
2097

    
2098
        if (s->interlaced_dct) {
2099
            dct_linesize = linesize * 2;
2100
            dct_offset = linesize;
2101
        } else {
2102
            dct_linesize = linesize;
2103
            dct_offset = linesize * 8;
2104
        }
2105

    
2106
        if (!s->mb_intra) {
2107
            /* motion handling */
2108
            /* decoding or more than one mb_type (MC was allready done otherwise) */
2109
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2110
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
2111
                    op_pix = s->dsp.put_pixels_tab;
2112
                    op_qpix= s->dsp.put_qpel_pixels_tab;
2113
                }else{
2114
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
2115
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2116
                }
2117

    
2118
                if (s->mv_dir & MV_DIR_FORWARD) {
2119
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2120
                    op_pix = s->dsp.avg_pixels_tab;
2121
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
2122
                }
2123
                if (s->mv_dir & MV_DIR_BACKWARD) {
2124
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2125
                }
2126
            }
2127

    
2128
            /* skip dequant / idct if we are really late ;) */
2129
            if(s->hurry_up>1) return;
2130

    
2131
            /* add dct residue */
2132
            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
2133
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2134
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2135
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2136
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2137
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2138

    
2139
                if(!(s->flags&CODEC_FLAG_GRAY)){
2140
                    add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
2141
                    add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
2142
                }
2143
            } else if(s->codec_id != CODEC_ID_WMV2){
2144
                add_dct(s, block[0], 0, dest_y, dct_linesize);
2145
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2146
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2147
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2148

    
2149
                if(!(s->flags&CODEC_FLAG_GRAY)){
2150
                    add_dct(s, block[4], 4, dest_cb, uvlinesize);
2151
                    add_dct(s, block[5], 5, dest_cr, uvlinesize);
2152
                }
2153
            } 
2154
#ifdef CONFIG_RISKY
2155
            else{
2156
                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2157
            }
2158
#endif
2159
        } else {
2160
            /* dct only in intra block */
2161
            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2162
                put_dct(s, block[0], 0, dest_y, dct_linesize);
2163
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2164
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2165
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2166

    
2167
                if(!(s->flags&CODEC_FLAG_GRAY)){
2168
                    put_dct(s, block[4], 4, dest_cb, uvlinesize);
2169
                    put_dct(s, block[5], 5, dest_cr, uvlinesize);
2170
                }
2171
            }else{
2172
                s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
2173
                s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
2174
                s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
2175
                s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2176

    
2177
                if(!(s->flags&CODEC_FLAG_GRAY)){
2178
                    s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2179
                    s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2180
                }
2181
            }
2182
        }
2183
    }
2184
}
2185

    
2186
#ifdef CONFIG_ENCODERS
2187

    
2188
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2189
{
2190
    static const char tab[64]=
2191
        {3,2,2,1,1,1,1,1,
2192
         1,1,1,1,1,1,1,1,
2193
         1,1,1,1,1,1,1,1,
2194
         0,0,0,0,0,0,0,0,
2195
         0,0,0,0,0,0,0,0,
2196
         0,0,0,0,0,0,0,0,
2197
         0,0,0,0,0,0,0,0,
2198
         0,0,0,0,0,0,0,0};
2199
    int score=0;
2200
    int run=0;
2201
    int i;
2202
    DCTELEM *block= s->block[n];
2203
    const int last_index= s->block_last_index[n];
2204
    int skip_dc;
2205

    
2206
    if(threshold<0){
2207
        skip_dc=0;
2208
        threshold= -threshold;
2209
    }else
2210
        skip_dc=1;
2211

    
2212
    /* are all which we could set to zero are allready zero? */
2213
    if(last_index<=skip_dc - 1) return;
2214

    
2215
    for(i=0; i<=last_index; i++){
2216
        const int j = s->intra_scantable.permutated[i];
2217
        const int level = ABS(block[j]);
2218
        if(level==1){
2219
            if(skip_dc && i==0) continue;
2220
            score+= tab[run];
2221
            run=0;
2222
        }else if(level>1){
2223
            return;
2224
        }else{
2225
            run++;
2226
        }
2227
    }
2228
    if(score >= threshold) return;
2229
    for(i=skip_dc; i<=last_index; i++){
2230
        const int j = s->intra_scantable.permutated[i];
2231
        block[j]=0;
2232
    }
2233
    if(block[0]) s->block_last_index[n]= 0;
2234
    else         s->block_last_index[n]= -1;
2235
}
2236

    
2237
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2238
{
2239
    int i;
2240
    const int maxlevel= s->max_qcoeff;
2241
    const int minlevel= s->min_qcoeff;
2242
    
2243
    if(s->mb_intra){
2244
        i=1; //skip clipping of intra dc
2245
    }else
2246
        i=0;
2247
    
2248
    for(;i<=last_index; i++){
2249
        const int j= s->intra_scantable.permutated[i];
2250
        int level = block[j];
2251
       
2252
        if     (level>maxlevel) level=maxlevel;
2253
        else if(level<minlevel) level=minlevel;
2254

    
2255
        block[j]= level;
2256
    }
2257
}
2258

    
2259
#if 0
2260
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2261
    int score=0;
2262
    int x,y;
2263
    
2264
    for(y=0; y<7; y++){
2265
        for(x=0; x<16; x+=4){
2266
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2267
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2268
        }
2269
        s+= stride;
2270
    }
2271
    
2272
    return score;
2273
}
2274

2275
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2276
    int score=0;
2277
    int x,y;
2278
    
2279
    for(y=0; y<7; y++){
2280
        for(x=0; x<16; x++){
2281
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2282
        }
2283
        s1+= stride;
2284
        s2+= stride;
2285
    }
2286
    
2287
    return score;
2288
}
2289
#else
2290
#define SQ(a) ((a)*(a))
2291

    
2292
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2293
    int score=0;
2294
    int x,y;
2295
    
2296
    for(y=0; y<7; y++){
2297
        for(x=0; x<16; x+=4){
2298
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2299
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2300
        }
2301
        s+= stride;
2302
    }
2303
    
2304
    return score;
2305
}
2306

    
2307
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2308
    int score=0;
2309
    int x,y;
2310
    
2311
    for(y=0; y<7; y++){
2312
        for(x=0; x<16; x++){
2313
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2314
        }
2315
        s1+= stride;
2316
        s2+= stride;
2317
    }
2318
    
2319
    return score;
2320
}
2321

    
2322
#endif
2323

    
2324
#endif //CONFIG_ENCODERS
2325

    
2326
/**
2327
 *
2328
 * @param h is the normal height, this will be reduced automatically if needed for the last row
2329
 */
2330
void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2331
    if (    s->avctx->draw_horiz_band 
2332
        && (s->last_picture_ptr || s->low_delay) ) {
2333
        uint8_t *src_ptr[3];
2334
        int offset;
2335
        h= FFMIN(h, s->height - y);
2336

    
2337
        if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
2338
            offset = 0;
2339
        else
2340
            offset = y * s->linesize;
2341

    
2342
        if(s->pict_type==B_TYPE || s->low_delay){
2343
            src_ptr[0] = s->current_picture.data[0] + offset;
2344
            src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
2345
            src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
2346
        } else {
2347
            src_ptr[0] = s->last_picture.data[0] + offset;
2348
            src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
2349
            src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
2350
        }
2351
        emms_c();
2352

    
2353
        s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2354
                               y, s->width, h);
2355
    }
2356
}
2357

    
2358
#ifdef CONFIG_ENCODERS
2359

    
2360
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2361
{
2362
    const int mb_x= s->mb_x;
2363
    const int mb_y= s->mb_y;
2364
    int i;
2365
    int skip_dct[6];
2366
    int dct_offset   = s->linesize*8; //default for progressive frames
2367
    
2368
    for(i=0; i<6; i++) skip_dct[i]=0;
2369
    
2370
    if(s->adaptive_quant){
2371
        s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2372

    
2373
        if(s->out_format==FMT_H263){
2374
            if     (s->dquant> 2) s->dquant= 2;
2375
            else if(s->dquant<-2) s->dquant=-2;
2376
        }
2377
            
2378
        if(s->codec_id==CODEC_ID_MPEG4){        
2379
            if(!s->mb_intra){
2380
                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2381

    
2382
                if(s->mv_dir&MV_DIRECT)
2383
                    s->dquant=0;
2384
            }
2385
        }
2386
        s->qscale+= s->dquant;
2387
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2388
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2389
    }
2390

    
2391
    if (s->mb_intra) {
2392
        uint8_t *ptr;
2393
        int wrap_y;
2394
        int emu=0;
2395

    
2396
        wrap_y = s->linesize;
2397
        ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2398

    
2399
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2400
            ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2401
            ptr= s->edge_emu_buffer;
2402
            emu=1;
2403
        }
2404
        
2405
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2406
            int progressive_score, interlaced_score;
2407
            
2408
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2409
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2410
            
2411
            if(progressive_score > interlaced_score + 100){
2412
                s->interlaced_dct=1;
2413
            
2414
                dct_offset= wrap_y;
2415
                wrap_y<<=1;
2416
            }else
2417
                s->interlaced_dct=0;
2418
        }
2419
        
2420
        s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
2421
        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
2422
        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2423
        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2424

    
2425
        if(s->flags&CODEC_FLAG_GRAY){
2426
            skip_dct[4]= 1;
2427
            skip_dct[5]= 1;
2428
        }else{
2429
            int wrap_c = s->uvlinesize;
2430
            ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2431
            if(emu){
2432
                ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2433
                ptr= s->edge_emu_buffer;
2434
            }
2435
            s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2436

    
2437
            ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2438
            if(emu){
2439
                ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2440
                ptr= s->edge_emu_buffer;
2441
            }
2442
            s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2443
        }
2444
    }else{
2445
        op_pixels_func (*op_pix)[4];
2446
        qpel_mc_func (*op_qpix)[16];
2447
        uint8_t *dest_y, *dest_cb, *dest_cr;
2448
        uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2449
        int wrap_y, wrap_c;
2450
        int emu=0;
2451

    
2452
        dest_y  = s->current_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
2453
        dest_cb = s->current_picture.data[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2454
        dest_cr = s->current_picture.data[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2455
        wrap_y = s->linesize;
2456
        wrap_c = s->uvlinesize;
2457
        ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2458
        ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2459
        ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2460

    
2461
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
2462
            op_pix = s->dsp.put_pixels_tab;
2463
            op_qpix= s->dsp.put_qpel_pixels_tab;
2464
        }else{
2465
            op_pix = s->dsp.put_no_rnd_pixels_tab;
2466
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2467
        }
2468

    
2469
        if (s->mv_dir & MV_DIR_FORWARD) {
2470
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2471
            op_pix = s->dsp.avg_pixels_tab;
2472
            op_qpix= s->dsp.avg_qpel_pixels_tab;
2473
        }
2474
        if (s->mv_dir & MV_DIR_BACKWARD) {
2475
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2476
        }
2477

    
2478
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2479
            ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2480
            ptr_y= s->edge_emu_buffer;
2481
            emu=1;
2482
        }
2483
        
2484
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2485
            int progressive_score, interlaced_score;
2486
            
2487
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
2488
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
2489
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
2490
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
2491
            
2492
            if(progressive_score > interlaced_score + 600){
2493
                s->interlaced_dct=1;
2494
            
2495
                dct_offset= wrap_y;
2496
                wrap_y<<=1;
2497
            }else
2498
                s->interlaced_dct=0;
2499
        }
2500
        
2501
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
2502
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
2503
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
2504
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2505
        
2506
        if(s->flags&CODEC_FLAG_GRAY){
2507
            skip_dct[4]= 1;
2508
            skip_dct[5]= 1;
2509
        }else{
2510
            if(emu){
2511
                ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2512
                ptr_cb= s->edge_emu_buffer;
2513
            }
2514
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2515
            if(emu){
2516
                ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2517
                ptr_cr= s->edge_emu_buffer;
2518
            }
2519
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2520
        }
2521
        /* pre quantization */         
2522
        if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2523
            //FIXME optimize
2524
            if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2525
            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2526
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2527
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2528
            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2529
            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2530
#if 0
2531
{
2532
 static int stat[7];
2533
 int num=0;
2534
 for(i=0; i<6; i++)
2535
  if(skip_dct[i]) num++;
2536
 stat[num]++;
2537
 
2538
 if(s->mb_x==0 && s->mb_y==0){
2539
  for(i=0; i<7; i++){
2540
   printf("%6d %1d\n", stat[i], i);
2541
  }
2542
 }
2543
}
2544
#endif
2545
        }
2546

    
2547
    }
2548
            
2549
#if 0
2550
            {
2551
                float adap_parm;
2552
                
2553
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2554
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2555
            
2556
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
2557
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
2558
                        s->qscale, adap_parm, s->qscale*adap_parm,
2559
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2560
            }
2561
#endif
2562
    /* DCT & quantize */
2563
    if(s->out_format==FMT_MJPEG){
2564
        for(i=0;i<6;i++) {
2565
            int overflow;
2566
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2567
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2568
        }
2569
    }else{
2570
        for(i=0;i<6;i++) {
2571
            if(!skip_dct[i]){
2572
                int overflow;
2573
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2574
            // FIXME we could decide to change to quantizer instead of clipping
2575
            // JS: I don't think that would be a good idea it could lower quality instead
2576
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
2577
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2578
            }else
2579
                s->block_last_index[i]= -1;
2580
        }
2581
        if(s->luma_elim_threshold && !s->mb_intra)
2582
            for(i=0; i<4; i++)
2583
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2584
        if(s->chroma_elim_threshold && !s->mb_intra)
2585
            for(i=4; i<6; i++)
2586
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2587
    }
2588

    
2589
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2590
        s->block_last_index[4]=
2591
        s->block_last_index[5]= 0;
2592
        s->block[4][0]=
2593
        s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
2594
    }
2595

    
2596
    /* huffman encode */
2597
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2598
    case CODEC_ID_MPEG1VIDEO:
2599
        mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
2600
#ifdef CONFIG_RISKY
2601
    case CODEC_ID_MPEG4:
2602
        mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2603
    case CODEC_ID_MSMPEG4V2:
2604
    case CODEC_ID_MSMPEG4V3:
2605
    case CODEC_ID_WMV1:
2606
        msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2607
    case CODEC_ID_WMV2:
2608
         ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
2609
    case CODEC_ID_H263:
2610
    case CODEC_ID_H263P:
2611
    case CODEC_ID_RV10:
2612
        h263_encode_mb(s, s->block, motion_x, motion_y); break;
2613
#endif
2614
    case CODEC_ID_MJPEG:
2615
        mjpeg_encode_mb(s, s->block); break;
2616
    default:
2617
        assert(0);
2618
    }
2619
}
2620

    
2621
#endif //CONFIG_ENCODERS
2622

    
2623
/**
2624
 * combines the (truncated) bitstream to a complete frame
2625
 * @returns -1 if no complete frame could be created
2626
 */
2627
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
2628
    ParseContext *pc= &s->parse_context;
2629
        
2630
    pc->last_index= pc->index;
2631

    
2632
    if(next==-1){
2633
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2634

    
2635
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
2636
        pc->index += *buf_size;
2637
        return -1;
2638
    }
2639

    
2640
    if(pc->index){
2641
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2642

    
2643
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
2644
        pc->index = 0;
2645
        *buf= pc->buffer;
2646
        *buf_size= pc->last_index + next;
2647
    }
2648

    
2649
    return 0;
2650
}
2651

    
2652
#ifdef CONFIG_ENCODERS
2653
void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
2654
{
2655
    int bytes= length>>4;
2656
    int bits= length&15;
2657
    int i;
2658

    
2659
    if(length==0) return;
2660

    
2661
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2662
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2663
}
2664

    
2665
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2666
    int i;
2667

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

    
2670
    /* mpeg1 */
2671
    d->mb_incr= s->mb_incr;
2672
    for(i=0; i<3; i++)
2673
        d->last_dc[i]= s->last_dc[i];
2674
    
2675
    /* statistics */
2676
    d->mv_bits= s->mv_bits;
2677
    d->i_tex_bits= s->i_tex_bits;
2678
    d->p_tex_bits= s->p_tex_bits;
2679
    d->i_count= s->i_count;
2680
    d->f_count= s->f_count;
2681
    d->b_count= s->b_count;
2682
    d->skip_count= s->skip_count;
2683
    d->misc_bits= s->misc_bits;
2684
    d->last_bits= 0;
2685

    
2686
    d->mb_skiped= s->mb_skiped;
2687
    d->qscale= s->qscale;
2688
}
2689

    
2690
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2691
    int i;
2692

    
2693
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
2694
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2695
    
2696
    /* mpeg1 */
2697
    d->mb_incr= s->mb_incr;
2698
    for(i=0; i<3; i++)
2699
        d->last_dc[i]= s->last_dc[i];
2700
    
2701
    /* statistics */
2702
    d->mv_bits= s->mv_bits;
2703
    d->i_tex_bits= s->i_tex_bits;
2704
    d->p_tex_bits= s->p_tex_bits;
2705
    d->i_count= s->i_count;
2706
    d->f_count= s->f_count;
2707
    d->b_count= s->b_count;
2708
    d->skip_count= s->skip_count;
2709
    d->misc_bits= s->misc_bits;
2710

    
2711
    d->mb_intra= s->mb_intra;
2712
    d->mb_skiped= s->mb_skiped;
2713
    d->mv_type= s->mv_type;
2714
    d->mv_dir= s->mv_dir;
2715
    d->pb= s->pb;
2716
    if(s->data_partitioning){
2717
        d->pb2= s->pb2;
2718
        d->tex_pb= s->tex_pb;
2719
    }
2720
    d->block= s->block;
2721
    for(i=0; i<6; i++)
2722
        d->block_last_index[i]= s->block_last_index[i];
2723
    d->interlaced_dct= s->interlaced_dct;
2724
    d->qscale= s->qscale;
2725
}
2726

    
2727
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
2728
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2729
                           int *dmin, int *next_block, int motion_x, int motion_y)
2730
{
2731
    int bits_count;
2732
    
2733
    copy_context_before_encode(s, backup, type);
2734

    
2735
    s->block= s->blocks[*next_block];
2736
    s->pb= pb[*next_block];
2737
    if(s->data_partitioning){
2738
        s->pb2   = pb2   [*next_block];
2739
        s->tex_pb= tex_pb[*next_block];
2740
    }
2741

    
2742
    encode_mb(s, motion_x, motion_y);
2743

    
2744
    bits_count= get_bit_count(&s->pb);
2745
    if(s->data_partitioning){
2746
        bits_count+= get_bit_count(&s->pb2);
2747
        bits_count+= get_bit_count(&s->tex_pb);
2748
    }
2749

    
2750
    if(bits_count<*dmin){
2751
        *dmin= bits_count;
2752
        *next_block^=1;
2753

    
2754
        copy_context_after_encode(best, s, type);
2755
    }
2756
}
2757
                
2758
static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2759
    uint32_t *sq = squareTbl + 256;
2760
    int acc=0;
2761
    int x,y;
2762
    
2763
    if(w==16 && h==16) 
2764
        return s->dsp.sse[0](NULL, src1, src2, stride);
2765
    else if(w==8 && h==8)
2766
        return s->dsp.sse[1](NULL, src1, src2, stride);
2767
    
2768
    for(y=0; y<h; y++){
2769
        for(x=0; x<w; x++){
2770
            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2771
        } 
2772
    }
2773
    
2774
    assert(acc>=0);
2775
    
2776
    return acc;
2777
}
2778

    
2779
static void encode_picture(MpegEncContext *s, int picture_number)
2780
{
2781
    int mb_x, mb_y, pdif = 0;
2782
    int i;
2783
    int bits;
2784
    MpegEncContext best_s, backup_s;
2785
    uint8_t bit_buf[2][3000];
2786
    uint8_t bit_buf2[2][3000];
2787
    uint8_t bit_buf_tex[2][3000];
2788
    PutBitContext pb[2], pb2[2], tex_pb[2];
2789

    
2790
    for(i=0; i<2; i++){
2791
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
2792
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
2793
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2794
    }
2795

    
2796
    s->picture_number = picture_number;
2797

    
2798
    s->block_wrap[0]=
2799
    s->block_wrap[1]=
2800
    s->block_wrap[2]=
2801
    s->block_wrap[3]= s->mb_width*2 + 2;
2802
    s->block_wrap[4]=
2803
    s->block_wrap[5]= s->mb_width + 2;
2804
    
2805
    /* Reset the average MB variance */
2806
    s->current_picture.mb_var_sum = 0;
2807
    s->current_picture.mc_mb_var_sum = 0;
2808

    
2809
#ifdef CONFIG_RISKY
2810
    /* we need to initialize some time vars before we can encode b-frames */
2811
    // RAL: Condition added for MPEG1VIDEO
2812
    if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
2813
        ff_set_mpeg4_time(s, s->picture_number); 
2814
#endif
2815
        
2816
    s->scene_change_score=0;
2817
    
2818
    s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2819
    
2820
    if(s->pict_type==I_TYPE){
2821
        if(s->msmpeg4_version) s->no_rounding=1;
2822
        else                   s->no_rounding=0;
2823
    }else if(s->pict_type!=B_TYPE){
2824
        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2825
            s->no_rounding ^= 1;          
2826
    }
2827
    
2828
    /* Estimate motion for every MB */
2829
    s->mb_intra=0; //for the rate distoration & bit compare functions
2830
    if(s->pict_type != I_TYPE){
2831
        if(s->pict_type != B_TYPE){
2832
            if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
2833
                s->me.pre_pass=1;
2834
                s->me.dia_size= s->avctx->pre_dia_size;
2835

    
2836
                for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
2837
                    for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
2838
                        s->mb_x = mb_x;
2839
                        s->mb_y = mb_y;
2840
                        ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
2841
                    }
2842
                }
2843
                s->me.pre_pass=0;
2844
            }
2845
        }
2846

    
2847
        s->me.dia_size= s->avctx->dia_size;
2848
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2849
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2850
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2851
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2852
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2853
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2854
                s->mb_x = mb_x;
2855
                s->mb_y = mb_y;
2856
                s->block_index[0]+=2;
2857
                s->block_index[1]+=2;
2858
                s->block_index[2]+=2;
2859
                s->block_index[3]+=2;
2860
                
2861
                /* compute motion vector & mb_type and store in context */
2862
                if(s->pict_type==B_TYPE)
2863
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
2864
                else
2865
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
2866
            }
2867
        }
2868
    }else /* if(s->pict_type == I_TYPE) */{
2869
        /* I-Frame */
2870
        //FIXME do we need to zero them?
2871
        memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2872
        memset(s->p_mv_table   , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
2873
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2874
        
2875
        if(!s->fixed_qscale){
2876
            /* finding spatial complexity for I-frame rate control */
2877
            for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2878
                for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2879
                    int xx = mb_x * 16;
2880
                    int yy = mb_y * 16;
2881
                    uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
2882
                    int varc;
2883
                    int sum = s->dsp.pix_sum(pix, s->linesize);
2884
    
2885
                    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
2886

    
2887
                    s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
2888
                    s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
2889
                    s->current_picture.mb_var_sum    += varc;
2890
                }
2891
            }
2892
        }
2893
    }
2894
    emms_c();
2895

    
2896
    if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2897
        s->pict_type= I_TYPE;
2898
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
2899
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2900
    }
2901

    
2902
    if(!s->umvplus){
2903
        if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
2904
            s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2905
        
2906
            ff_fix_long_p_mvs(s);
2907
        }
2908

    
2909
        if(s->pict_type==B_TYPE){
2910
            int a, b;
2911

    
2912
            a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2913
            b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
2914
            s->f_code = FFMAX(a, b);
2915

    
2916
            a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2917
            b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
2918
            s->b_code = FFMAX(a, b);
2919

    
2920
            ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2921
            ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2922
            ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2923
            ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2924
        }
2925
    }
2926
    
2927
    if (s->fixed_qscale) 
2928
        s->frame_qscale = s->current_picture.quality;
2929
    else
2930
        s->frame_qscale = ff_rate_estimate_qscale(s);
2931

    
2932
    if(s->adaptive_quant){
2933
#ifdef CONFIG_RISKY
2934
        switch(s->codec_id){
2935
        case CODEC_ID_MPEG4:
2936
            ff_clean_mpeg4_qscales(s);
2937
            break;
2938
        case CODEC_ID_H263:
2939
        case CODEC_ID_H263P:
2940
            ff_clean_h263_qscales(s);
2941
            break;
2942
        }
2943
#endif
2944

    
2945
        s->qscale= s->current_picture.qscale_table[0];
2946
    }else
2947
        s->qscale= (int)(s->frame_qscale + 0.5);
2948
        
2949
    if (s->out_format == FMT_MJPEG) {
2950
        /* for mjpeg, we do include qscale in the matrix */
2951
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2952
        for(i=1;i<64;i++){
2953
            int j= s->dsp.idct_permutation[i];
2954

    
2955
            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2956
        }
2957
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, 
2958
                       s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
2959
    }
2960
    
2961
    //FIXME var duplication
2962
    s->current_picture.key_frame= s->pict_type == I_TYPE;
2963
    s->current_picture.pict_type= s->pict_type;
2964

    
2965
    if(s->current_picture.key_frame)
2966
        s->picture_in_gop_number=0;
2967

    
2968
    s->last_bits= get_bit_count(&s->pb);
2969
    switch(s->out_format) {
2970
    case FMT_MJPEG:
2971
        mjpeg_picture_header(s);
2972
        break;
2973
#ifdef CONFIG_RISKY
2974
    case FMT_H263:
2975
        if (s->codec_id == CODEC_ID_WMV2) 
2976
            ff_wmv2_encode_picture_header(s, picture_number);
2977
        else if (s->h263_msmpeg4) 
2978
            msmpeg4_encode_picture_header(s, picture_number);
2979
        else if (s->h263_pred)
2980
            mpeg4_encode_picture_header(s, picture_number);
2981
        else if (s->h263_rv10) 
2982
            rv10_encode_picture_header(s, picture_number);
2983
        else
2984
            h263_encode_picture_header(s, picture_number);
2985
        break;
2986
#endif
2987
    case FMT_MPEG1:
2988
        mpeg1_encode_picture_header(s, picture_number);
2989
        break;
2990
    }
2991
    bits= get_bit_count(&s->pb);
2992
    s->header_bits= bits - s->last_bits;
2993
    s->last_bits= bits;
2994
    s->mv_bits=0;
2995
    s->misc_bits=0;
2996
    s->i_tex_bits=0;
2997
    s->p_tex_bits=0;
2998
    s->i_count=0;
2999
    s->f_count=0;
3000
    s->b_count=0;
3001
    s->skip_count=0;
3002

    
3003
    for(i=0; i<3; i++){
3004
        /* init last dc values */
3005
        /* note: quant matrix value (8) is implied here */
3006
        s->last_dc[i] = 128;
3007
        
3008
        s->current_picture.error[i] = 0;
3009
    }
3010
    s->mb_incr = 1;
3011
    s->last_mv[0][0][0] = 0;
3012
    s->last_mv[0][0][1] = 0;
3013
    s->last_mv[1][0][0] = 0;
3014
    s->last_mv[1][0][1] = 0;
3015
     
3016
    s->last_mv_dir = 0;
3017

    
3018
#ifdef CONFIG_RISKY
3019
    if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
3020
        s->gob_index = ff_h263_get_gob_height(s);
3021

    
3022
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3023
        ff_mpeg4_init_partitions(s);
3024
#endif
3025

    
3026
    s->resync_mb_x=0;
3027
    s->resync_mb_y=0;
3028
    s->first_slice_line = 1;
3029
    s->ptr_lastgob = s->pb.buf;
3030
    s->ptr_last_mb_line = s->pb.buf;
3031
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3032
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3033
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3034
        
3035
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3036
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3037
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3038
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3039
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
3040
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
3041
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3042
            int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
3043
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
3044
//            int d;
3045
            int dmin=10000000;
3046

    
3047
            s->mb_x = mb_x;
3048
            s->mb_y = mb_y;
3049
            s->block_index[0]+=2;
3050
            s->block_index[1]+=2;
3051
            s->block_index[2]+=2;
3052
            s->block_index[3]+=2;
3053
            s->block_index[4]++;
3054
            s->block_index[5]++;
3055

    
3056
            /* write gob / video packet header  */
3057
#ifdef CONFIG_RISKY
3058
            if(s->rtp_mode){
3059
                int current_packet_size, is_gob_start;
3060
                
3061
                current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3062
                is_gob_start=0;
3063
                
3064
                if(s->codec_id==CODEC_ID_MPEG4){
3065
                    if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
3066
                       && s->mb_y + s->mb_x>0){
3067

    
3068
                        if(s->partitioned_frame){
3069
                            ff_mpeg4_merge_partitions(s);
3070
                            ff_mpeg4_init_partitions(s);
3071
                        }
3072
                        ff_mpeg4_encode_video_packet_header(s);
3073

    
3074
                        if(s->flags&CODEC_FLAG_PASS1){
3075
                            int bits= get_bit_count(&s->pb);
3076
                            s->misc_bits+= bits - s->last_bits;
3077
                            s->last_bits= bits;
3078
                        }
3079
                        ff_mpeg4_clean_buffers(s);
3080
                        is_gob_start=1;
3081
                    }
3082
                }else{
3083
                    if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
3084
                       && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3085
                       
3086
                        h263_encode_gob_header(s, mb_y);                       
3087
                        is_gob_start=1;
3088
                    }
3089
                }
3090

    
3091
                if(is_gob_start){
3092
                    s->ptr_lastgob = pbBufPtr(&s->pb);
3093
                    s->first_slice_line=1;
3094
                    s->resync_mb_x=mb_x;
3095
                    s->resync_mb_y=mb_y;
3096
                }
3097
            }
3098
#endif
3099

    
3100
            if(  (s->resync_mb_x   == s->mb_x)
3101
               && s->resync_mb_y+1 == s->mb_y){
3102
                s->first_slice_line=0; 
3103
            }
3104

    
3105
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3106
                int next_block=0;
3107
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3108

    
3109
                copy_context_before_encode(&backup_s, s, -1);
3110
                backup_s.pb= s->pb;
3111
                best_s.data_partitioning= s->data_partitioning;
3112
                best_s.partitioned_frame= s->partitioned_frame;
3113
                if(s->data_partitioning){
3114
                    backup_s.pb2= s->pb2;
3115
                    backup_s.tex_pb= s->tex_pb;
3116
                }
3117

    
3118
                if(mb_type&MB_TYPE_INTER){
3119
                    s->mv_dir = MV_DIR_FORWARD;
3120
                    s->mv_type = MV_TYPE_16X16;
3121
                    s->mb_intra= 0;
3122
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
3123
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
3124
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
3125
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3126
                }
3127
                if(mb_type&MB_TYPE_INTER4V){                 
3128
                    s->mv_dir = MV_DIR_FORWARD;
3129
                    s->mv_type = MV_TYPE_8X8;
3130
                    s->mb_intra= 0;
3131
                    for(i=0; i<4; i++){
3132
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3133
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3134
                    }
3135
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
3136
                                 &dmin, &next_block, 0, 0);
3137
                }
3138
                if(mb_type&MB_TYPE_FORWARD){
3139
                    s->mv_dir = MV_DIR_FORWARD;
3140
                    s->mv_type = MV_TYPE_16X16;
3141
                    s->mb_intra= 0;
3142
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3143
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3144
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
3145
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3146
                }
3147
                if(mb_type&MB_TYPE_BACKWARD){
3148
                    s->mv_dir = MV_DIR_BACKWARD;
3149
                    s->mv_type = MV_TYPE_16X16;
3150
                    s->mb_intra= 0;
3151
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3152
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3153
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
3154
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3155
                }
3156
                if(mb_type&MB_TYPE_BIDIR){
3157
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3158
                    s->mv_type = MV_TYPE_16X16;
3159
                    s->mb_intra= 0;
3160
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3161
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3162
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3163
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3164
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
3165
                                 &dmin, &next_block, 0, 0);
3166
                }
3167
                if(mb_type&MB_TYPE_DIRECT){
3168
                    int mx= s->b_direct_mv_table[xy][0];
3169
                    int my= s->b_direct_mv_table[xy][1];
3170
                    
3171
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3172
                    s->mb_intra= 0;
3173
#ifdef CONFIG_RISKY
3174
                    ff_mpeg4_set_direct_mv(s, mx, my);
3175
#endif
3176
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
3177
                                 &dmin, &next_block, mx, my);
3178
                }
3179
                if(mb_type&MB_TYPE_INTRA){
3180
                    s->mv_dir = 0;
3181
                    s->mv_type = MV_TYPE_16X16;
3182
                    s->mb_intra= 1;
3183
                    s->mv[0][0][0] = 0;
3184
                    s->mv[0][0][1] = 0;
3185
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
3186
                                 &dmin, &next_block, 0, 0);
3187
                    /* force cleaning of ac/dc pred stuff if needed ... */
3188
                    if(s->h263_pred || s->h263_aic)
3189
                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
3190
                }
3191
                copy_context_after_encode(s, &best_s, -1);
3192
                
3193
                pb_bits_count= get_bit_count(&s->pb);
3194
                flush_put_bits(&s->pb);
3195
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3196
                s->pb= backup_s.pb;
3197
                
3198
                if(s->data_partitioning){
3199
                    pb2_bits_count= get_bit_count(&s->pb2);
3200
                    flush_put_bits(&s->pb2);
3201
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3202
                    s->pb2= backup_s.pb2;
3203
                    
3204
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
3205
                    flush_put_bits(&s->tex_pb);
3206
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3207
                    s->tex_pb= backup_s.tex_pb;
3208
                }
3209
                s->last_bits= get_bit_count(&s->pb);
3210
            } else {
3211
                int motion_x, motion_y;
3212
                int intra_score;
3213
                int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
3214
                
3215
              if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
3216
                /* get luma score */
3217
                if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3218
                    intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_width]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3219
                }else{
3220
                    uint8_t *dest_y;
3221

    
3222
                    int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
3223
                    mean*= 0x01010101;
3224
                    
3225
                    dest_y  = s->new_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
3226
                
3227
                    for(i=0; i<16; i++){
3228
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
3229
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
3230
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
3231
                        *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
3232
                    }
3233

    
3234
                    s->mb_intra=1;
3235
                    intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
3236
                                        
3237
/*                    printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8, 
3238
                        s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
3239
                        s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
3240
                }
3241
                
3242
                /* get chroma score */
3243
                if(s->avctx->mb_cmp&FF_CMP_CHROMA){
3244
                    int i;
3245
                    
3246
                    s->mb_intra=1;
3247
                    for(i=1; i<3; i++){
3248
                        uint8_t *dest_c;
3249
                        int mean;
3250
                        
3251
                        if(s->out_format == FMT_H263){
3252
                            mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
3253
                        }else{
3254
                            mean= (s->last_dc[i] + 4)>>3;
3255
                        }
3256
                        dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
3257
                        
3258
                        mean*= 0x01010101;
3259
                        for(i=0; i<8; i++){
3260
                            *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
3261
                            *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
3262
                        }
3263
                        
3264
                        intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
3265
                    }                
3266
                }
3267

    
3268
                /* bias */
3269
                switch(s->avctx->mb_cmp&0xFF){
3270
                default:
3271
                case FF_CMP_SAD:
3272
                    intra_score+= 32*s->qscale;
3273
                    break;
3274
                case FF_CMP_SSE:
3275
                    intra_score+= 24*s->qscale*s->qscale;
3276
                    break;
3277
                case FF_CMP_SATD:
3278
                    intra_score+= 96*s->qscale;
3279
                    break;
3280
                case FF_CMP_DCT:
3281
                    intra_score+= 48*s->qscale;
3282
                    break;
3283
                case FF_CMP_BIT:
3284
                    intra_score+= 16;
3285
                    break;
3286
                case FF_CMP_PSNR:
3287
                case FF_CMP_RD:
3288
                    intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
3289
                    break;
3290
                }
3291

    
3292
                if(intra_score < inter_score)
3293
                    mb_type= MB_TYPE_INTRA;
3294
              }  
3295
                
3296
                s->mv_type=MV_TYPE_16X16;
3297
                // only one MB-Type possible
3298
                
3299
                switch(mb_type){
3300
                case MB_TYPE_INTRA:
3301
                    s->mv_dir = 0;
3302
                    s->mb_intra= 1;
3303
                    motion_x= s->mv[0][0][0] = 0;
3304
                    motion_y= s->mv[0][0][1] = 0;
3305
                    break;
3306
                case MB_TYPE_INTER:
3307
                    s->mv_dir = MV_DIR_FORWARD;
3308
                    s->mb_intra= 0;
3309
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3310
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3311
                    break;
3312
                case MB_TYPE_INTER4V:
3313
                    s->mv_dir = MV_DIR_FORWARD;
3314
                    s->mv_type = MV_TYPE_8X8;
3315
                    s->mb_intra= 0;
3316
                    for(i=0; i<4; i++){
3317
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3318
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3319
                    }
3320
                    motion_x= motion_y= 0;
3321
                    break;
3322
                case MB_TYPE_DIRECT:
3323
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3324
                    s->mb_intra= 0;
3325
                    motion_x=s->b_direct_mv_table[xy][0];
3326
                    motion_y=s->b_direct_mv_table[xy][1];
3327
#ifdef CONFIG_RISKY
3328
                    ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3329
#endif
3330
                    break;
3331
                case MB_TYPE_BIDIR:
3332
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3333
                    s->mb_intra= 0;
3334
                    motion_x=0;
3335
                    motion_y=0;
3336
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3337
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3338
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3339
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3340
                    break;
3341
                case MB_TYPE_BACKWARD:
3342
                    s->mv_dir = MV_DIR_BACKWARD;
3343
                    s->mb_intra= 0;
3344
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3345
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3346
                    break;
3347
                case MB_TYPE_FORWARD:
3348
                    s->mv_dir = MV_DIR_FORWARD;
3349
                    s->mb_intra= 0;
3350
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3351
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3352
//                    printf(" %d %d ", motion_x, motion_y);
3353
                    break;
3354
                default:
3355
                    motion_x=motion_y=0; //gcc warning fix
3356
                    printf("illegal MB type\n");
3357
                }
3358

    
3359
                encode_mb(s, motion_x, motion_y);
3360

    
3361
                // RAL: Update last macrobloc type
3362
                s->last_mv_dir = s->mv_dir;
3363
            }
3364

    
3365
            /* clean the MV table in IPS frames for direct mode in B frames */
3366
            if(s->mb_intra /* && I,P,S_TYPE */){
3367
                s->p_mv_table[xy][0]=0;
3368
                s->p_mv_table[xy][1]=0;
3369
            }
3370

    
3371
            MPV_decode_mb(s, s->block);
3372
            
3373
            if(s->flags&CODEC_FLAG_PSNR){
3374
                int w= 16;
3375
                int h= 16;
3376

    
3377
                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3378
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3379

    
3380
                s->current_picture.error[0] += sse(
3381
                    s,
3382
                    s->new_picture    .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3383
                    s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3384
                    w, h, s->linesize);
3385
                s->current_picture.error[1] += sse(
3386
                    s,
3387
                    s->new_picture    .data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3388
                    s->current_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3389
                    w>>1, h>>1, s->uvlinesize);
3390
                s->current_picture.error[2] += sse(
3391
                    s,
3392
                    s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3393
                    s->current_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
3394
                    w>>1, h>>1, s->uvlinesize);
3395
            }
3396
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
3397
        }
3398

    
3399

    
3400
        /* Obtain average mb_row size for RTP */
3401
        if (s->rtp_mode) {
3402
            if (mb_y==0)
3403
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
3404
            else {    
3405
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
3406
            }
3407
            s->ptr_last_mb_line = pbBufPtr(&s->pb);
3408
        }
3409
    }
3410
    emms_c();
3411

    
3412
#ifdef CONFIG_RISKY
3413
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3414
        ff_mpeg4_merge_partitions(s);
3415

    
3416
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3417
        msmpeg4_encode_ext_header(s);
3418

    
3419
    if(s->codec_id==CODEC_ID_MPEG4) 
3420
        ff_mpeg4_stuffing(&s->pb);
3421
#endif
3422

    
3423
    //if (s->gob_number)
3424
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3425
    
3426
    /* Send the last GOB if RTP */    
3427
    if (s->rtp_mode) {
3428
        flush_put_bits(&s->pb);
3429
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3430
        /* Call the RTP callback to send the last GOB */
3431
        if (s->rtp_callback)
3432
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3433
        s->ptr_lastgob = pbBufPtr(&s->pb);
3434
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3435
    }
3436
}
3437

    
3438
static int dct_quantize_trellis_c(MpegEncContext *s, 
3439
                        DCTELEM *block, int n,
3440
                        int qscale, int *overflow){
3441
    const int *qmat;
3442
    const uint8_t *scantable= s->intra_scantable.scantable;
3443
    int max=0;
3444
    unsigned int threshold1, threshold2;
3445
    int bias=0;
3446
    int run_tab[65];
3447
    int level_tab[65];
3448
    int score_tab[65];
3449
    int last_run=0;
3450
    int last_level=0;
3451
    int last_score= 0;
3452
    int last_i= 0;
3453
    int coeff[3][64];
3454
    int coeff_count[64];
3455
    int lambda, qmul, qadd, start_i, last_non_zero, i;
3456
    const int esc_length= s->ac_esc_length;
3457
    uint8_t * length;
3458
    uint8_t * last_length;
3459
    int score_limit=0;
3460
    int left_limit= 0;
3461
        
3462
    s->dsp.fdct (block);
3463

    
3464
    qmul= qscale*16;
3465
    qadd= ((qscale-1)|1)*8;
3466

    
3467
    if (s->mb_intra) {
3468
        int q;
3469
        if (!s->h263_aic) {
3470
            if (n < 4)
3471
                q = s->y_dc_scale;
3472
            else
3473
                q = s->c_dc_scale;
3474
            q = q << 3;
3475
        } else{
3476
            /* For AIC we skip quant/dequant of INTRADC */
3477
            q = 1 << 3;
3478
            qadd=0;
3479
        }
3480
            
3481
        /* note: block[0] is assumed to be positive */
3482
        block[0] = (block[0] + (q >> 1)) / q;
3483
        start_i = 1;
3484
        last_non_zero = 0;
3485
        qmat = s->q_intra_matrix[qscale];
3486
        if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
3487
            bias= 1<<(QMAT_SHIFT-1);
3488
        length     = s->intra_ac_vlc_length;
3489
        last_length= s->intra_ac_vlc_last_length;
3490
    } else {
3491
        start_i = 0;
3492
        last_non_zero = -1;
3493
        qmat = s->q_inter_matrix[qscale];
3494
        length     = s->inter_ac_vlc_length;
3495
        last_length= s->inter_ac_vlc_last_length;
3496
    }
3497

    
3498
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3499
    threshold2= (threshold1<<1);
3500

    
3501
    for(i=start_i; i<64; i++) {
3502
        const int j = scantable[i];
3503
        const int k= i-start_i;
3504
        int level = block[j];
3505
        level = level * qmat[j];
3506

    
3507
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3508
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3509
        if(((unsigned)(level+threshold1))>threshold2){
3510
            if(level>0){
3511
                level= (bias + level)>>QMAT_SHIFT;
3512
                coeff[0][k]= level;
3513
                coeff[1][k]= level-1;
3514
//                coeff[2][k]= level-2;
3515
            }else{
3516
                level= (bias - level)>>QMAT_SHIFT;
3517
                coeff[0][k]= -level;
3518
                coeff[1][k]= -level+1;
3519
//                coeff[2][k]= -level+2;
3520
            }
3521
            coeff_count[k]= FFMIN(level, 2);
3522
            max |=level;
3523
            last_non_zero = i;
3524
        }else{
3525
            coeff[0][k]= (level>>31)|1;
3526
            coeff_count[k]= 1;
3527
        }
3528
    }
3529
    
3530
    *overflow= s->max_qcoeff < max; //overflow might have happend
3531
    
3532
    if(last_non_zero < start_i){
3533
        memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3534
        return last_non_zero;
3535
    }
3536

    
3537
    lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
3538
        
3539
    score_tab[0]= 0;
3540
    for(i=0; i<=last_non_zero - start_i; i++){
3541
        int level_index, run, j;
3542
        const int dct_coeff= block[ scantable[i + start_i] ];
3543
        const int zero_distoration= dct_coeff*dct_coeff;
3544
        int best_score=256*256*256*120;
3545

    
3546
        last_score += zero_distoration;
3547
        for(level_index=0; level_index < coeff_count[i]; level_index++){
3548
            int distoration;
3549
            int level= coeff[level_index][i];
3550
            int unquant_coeff;
3551
            
3552
            assert(level);
3553

    
3554
            if(s->out_format == FMT_H263){
3555
                if(level>0){
3556
                    unquant_coeff= level*qmul + qadd;
3557
                }else{
3558
                    unquant_coeff= level*qmul - qadd;
3559
                }
3560
            }else{ //MPEG1
3561
                j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
3562
                if(s->mb_intra){
3563
                    if (level < 0) {
3564
                        unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
3565
                        unquant_coeff = -((unquant_coeff - 1) | 1);
3566
                    } else {
3567
                        unquant_coeff = (int)(  level  * qscale * s->intra_matrix[j]) >> 3;
3568
                        unquant_coeff =   (unquant_coeff - 1) | 1;
3569
                    }
3570
                }else{
3571
                    if (level < 0) {
3572
                        unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3573
                        unquant_coeff = -((unquant_coeff - 1) | 1);
3574
                    } else {
3575
                        unquant_coeff = (((  level  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3576
                        unquant_coeff =   (unquant_coeff - 1) | 1;
3577
                    }
3578
                }
3579
                unquant_coeff<<= 3;
3580
            }
3581

    
3582
            distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
3583
            level+=64;
3584
            if((level&(~127)) == 0){
3585
                for(run=0; run<=i - left_limit; run++){
3586
                    int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3587
                    score += score_tab[i-run];
3588
                    
3589
                    if(score < best_score){
3590
                        best_score= 
3591
                        score_tab[i+1]= score;
3592
                        run_tab[i+1]= run;
3593
                        level_tab[i+1]= level-64;
3594
                    }
3595
                }
3596

    
3597
                if(s->out_format == FMT_H263){
3598
                    for(run=0; run<=i - left_limit; run++){
3599
                        int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3600
                        score += score_tab[i-run];
3601
                        if(score < last_score){
3602
                            last_score= score;
3603
                            last_run= run;
3604
                            last_level= level-64;
3605
                            last_i= i+1;
3606
                        }
3607
                    }
3608
                }
3609
            }else{
3610
                distoration += esc_length*lambda;
3611
                for(run=0; run<=i - left_limit; run++){
3612
                    int score= distoration + score_tab[i-run];
3613
                    
3614
                    if(score < best_score){
3615
                        best_score= 
3616
                        score_tab[i+1]= score;
3617
                        run_tab[i+1]= run;
3618
                        level_tab[i+1]= level-64;
3619
                    }
3620
                }
3621

    
3622
                if(s->out_format == FMT_H263){
3623
                    for(run=0; run<=i - left_limit; run++){
3624
                        int score= distoration + score_tab[i-run];
3625
                        if(score < last_score){
3626
                            last_score= score;
3627
                            last_run= run;
3628
                            last_level= level-64;
3629
                            last_i= i+1;
3630
                        }
3631
                    }
3632
                }
3633
            }
3634
        }
3635

    
3636
        for(j=left_limit; j<=i; j++){
3637
            score_tab[j] += zero_distoration;
3638
        }
3639
        score_limit+= zero_distoration;
3640
        if(score_tab[i+1] < score_limit)
3641
            score_limit= score_tab[i+1];
3642
        
3643
        //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3644
        while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
3645
    }
3646

    
3647
        //FIXME add some cbp penalty
3648

    
3649
    if(s->out_format != FMT_H263){
3650
        last_score= 256*256*256*120;
3651
        for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
3652
            int score= score_tab[i];
3653
            if(i) score += lambda*2; //FIXME exacter?
3654

    
3655
            if(score < last_score){
3656
                last_score= score;
3657
                last_i= i;
3658
                last_level= level_tab[i];
3659
                last_run= run_tab[i];
3660
            }
3661
        }
3662
    }
3663
    
3664
    last_non_zero= last_i - 1 + start_i;
3665
    memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3666
    
3667
    if(last_non_zero < start_i)
3668
        return last_non_zero;
3669
    
3670
    i= last_i;
3671
    assert(last_level);
3672
//FIXME use permutated scantable
3673
    block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
3674
    i -= last_run + 1;
3675
    
3676
    for(;i>0 ; i -= run_tab[i] + 1){
3677
        const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
3678
    
3679
        block[j]= level_tab[i];
3680
        assert(block[j]);
3681
    }
3682

    
3683
    return last_non_zero;
3684
}
3685

    
3686
static int dct_quantize_c(MpegEncContext *s, 
3687
                        DCTELEM *block, int n,
3688
                        int qscale, int *overflow)
3689
{
3690
    int i, j, level, last_non_zero, q;
3691
    const int *qmat;
3692
    const uint8_t *scantable= s->intra_scantable.scantable;
3693
    int bias;
3694
    int max=0;
3695
    unsigned int threshold1, threshold2;
3696

    
3697
    s->dsp.fdct (block);
3698

    
3699
    if (s->mb_intra) {
3700
        if (!s->h263_aic) {
3701
            if (n < 4)
3702
                q = s->y_dc_scale;
3703
            else
3704
                q = s->c_dc_scale;
3705
            q = q << 3;
3706
        } else
3707
            /* For AIC we skip quant/dequant of INTRADC */
3708
            q = 1 << 3;
3709
            
3710
        /* note: block[0] is assumed to be positive */
3711
        block[0] = (block[0] + (q >> 1)) / q;
3712
        i = 1;
3713
        last_non_zero = 0;
3714
        qmat = s->q_intra_matrix[qscale];
3715
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3716
    } else {
3717
        i = 0;
3718
        last_non_zero = -1;
3719
        qmat = s->q_inter_matrix[qscale];
3720
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3721
    }
3722
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3723
    threshold2= (threshold1<<1);
3724

    
3725
    for(;i<64;i++) {
3726
        j = scantable[i];
3727
        level = block[j];
3728
        level = level * qmat[j];
3729

    
3730
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3731
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3732
        if(((unsigned)(level+threshold1))>threshold2){
3733
            if(level>0){
3734
                level= (bias + level)>>QMAT_SHIFT;
3735
                block[j]= level;
3736
            }else{
3737
                level= (bias - level)>>QMAT_SHIFT;
3738
                block[j]= -level;
3739
            }
3740
            max |=level;
3741
            last_non_zero = i;
3742
        }else{
3743
            block[j]=0;
3744
        }
3745
    }
3746
    *overflow= s->max_qcoeff < max; //overflow might have happend
3747
    
3748
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3749
    if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
3750
        ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
3751

    
3752
    return last_non_zero;
3753
}
3754

    
3755
#endif //CONFIG_ENCODERS
3756

    
3757
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
3758
                                   DCTELEM *block, int n, int qscale)
3759
{
3760
    int i, level, nCoeffs;
3761
    const uint16_t *quant_matrix;
3762

    
3763
    nCoeffs= s->block_last_index[n];
3764
    
3765
    if (s->mb_intra) {
3766
        if (n < 4) 
3767
            block[0] = block[0] * s->y_dc_scale;
3768
        else
3769
            block[0] = block[0] * s->c_dc_scale;
3770
        /* XXX: only mpeg1 */
3771
        quant_matrix = s->intra_matrix;
3772
        for(i=1;i<=nCoeffs;i++) {
3773
            int j= s->intra_scantable.permutated[i];
3774
            level = block[j];
3775
            if (level) {
3776
                if (level < 0) {
3777
                    level = -level;
3778
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3779
                    level = (level - 1) | 1;
3780
                    level = -level;
3781
                } else {
3782
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3783
                    level = (level - 1) | 1;
3784
                }
3785
#ifdef PARANOID
3786
                if (level < -2048 || level > 2047)
3787
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3788
#endif
3789
                block[j] = level;
3790
            }
3791
        }
3792
    } else {
3793
        i = 0;
3794
        quant_matrix = s->inter_matrix;
3795
        for(;i<=nCoeffs;i++) {
3796
            int j= s->intra_scantable.permutated[i];
3797
            level = block[j];
3798
            if (level) {
3799
                if (level < 0) {
3800
                    level = -level;
3801
                    level = (((level << 1) + 1) * qscale *
3802
                             ((int) (quant_matrix[j]))) >> 4;
3803
                    level = (level - 1) | 1;
3804
                    level = -level;
3805
                } else {
3806
                    level = (((level << 1) + 1) * qscale *
3807
                             ((int) (quant_matrix[j]))) >> 4;
3808
                    level = (level - 1) | 1;
3809
                }
3810
#ifdef PARANOID
3811
                if (level < -2048 || level > 2047)
3812
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3813
#endif
3814
                block[j] = level;
3815
            }
3816
        }
3817
    }
3818
}
3819

    
3820
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
3821
                                   DCTELEM *block, int n, int qscale)
3822
{
3823
    int i, level, nCoeffs;
3824
    const uint16_t *quant_matrix;
3825

    
3826
    if(s->alternate_scan) nCoeffs= 63;
3827
    else nCoeffs= s->block_last_index[n];
3828
    
3829
    if (s->mb_intra) {
3830
        if (n < 4) 
3831
            block[0] = block[0] * s->y_dc_scale;
3832
        else
3833
            block[0] = block[0] * s->c_dc_scale;
3834
        quant_matrix = s->intra_matrix;
3835
        for(i=1;i<=nCoeffs;i++) {
3836
            int j= s->intra_scantable.permutated[i];
3837
            level = block[j];
3838
            if (level) {
3839
                if (level < 0) {
3840
                    level = -level;
3841
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3842
                    level = -level;
3843
                } else {
3844
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3845
                }
3846
#ifdef PARANOID
3847
                if (level < -2048 || level > 2047)
3848
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3849
#endif
3850
                block[j] = level;
3851
            }
3852
        }
3853
    } else {
3854
        int sum=-1;
3855
        i = 0;
3856
        quant_matrix = s->inter_matrix;
3857
        for(;i<=nCoeffs;i++) {
3858
            int j= s->intra_scantable.permutated[i];
3859
            level = block[j];
3860
            if (level) {
3861
                if (level < 0) {
3862
                    level = -level;
3863
                    level = (((level << 1) + 1) * qscale *
3864
                             ((int) (quant_matrix[j]))) >> 4;
3865
                    level = -level;
3866
                } else {
3867
                    level = (((level << 1) + 1) * qscale *
3868
                             ((int) (quant_matrix[j]))) >> 4;
3869
                }
3870
#ifdef PARANOID
3871
                if (level < -2048 || level > 2047)
3872
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3873
#endif
3874
                block[j] = level;
3875
                sum+=level;
3876
            }
3877
        }
3878
        block[63]^=sum&1;
3879
    }
3880
}
3881

    
3882

    
3883
static void dct_unquantize_h263_c(MpegEncContext *s, 
3884
                                  DCTELEM *block, int n, int qscale)
3885
{
3886
    int i, level, qmul, qadd;
3887
    int nCoeffs;
3888
    
3889
    assert(s->block_last_index[n]>=0);
3890
    
3891
    qadd = (qscale - 1) | 1;
3892
    qmul = qscale << 1;
3893
    
3894
    if (s->mb_intra) {
3895
        if (!s->h263_aic) {
3896
            if (n < 4) 
3897
                block[0] = block[0] * s->y_dc_scale;
3898
            else
3899
                block[0] = block[0] * s->c_dc_scale;
3900
        }else
3901
            qadd = 0;
3902
        i = 1;
3903
        nCoeffs= 63; //does not allways use zigzag table 
3904
    } else {
3905
        i = 0;
3906
        nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3907
    }
3908

    
3909
    for(;i<=nCoeffs;i++) {
3910
        level = block[i];
3911
        if (level) {
3912
            if (level < 0) {
3913
                level = level * qmul - qadd;
3914
            } else {
3915
                level = level * qmul + qadd;
3916
            }
3917
#ifdef PARANOID
3918
                if (level < -2048 || level > 2047)
3919
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3920
#endif
3921
            block[i] = level;
3922
        }
3923
    }
3924
}
3925

    
3926

    
3927
char ff_get_pict_type_char(int pict_type){
3928
    switch(pict_type){
3929
    case I_TYPE: return 'I'; 
3930
    case P_TYPE: return 'P'; 
3931
    case B_TYPE: return 'B'; 
3932
    case S_TYPE: return 'S'; 
3933
    default:     return '?';
3934
    }
3935
}
3936

    
3937
static const AVOption mpeg4_options[] =
3938
{
3939
    AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
3940
    AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
3941
    AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
3942
                       "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
3943
                       bit_rate_tolerance, 4, 240000000, 8000),
3944
    AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
3945
    AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
3946
    AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
3947
                          rc_eq, "tex^qComp,option1,options2", 0),
3948
    AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
3949
                       rc_min_rate, 4, 24000000, 0),
3950
    AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
3951
                       rc_max_rate, 4, 24000000, 0),
3952
    AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
3953
                          rc_buffer_aggressivity, 4, 24000000, 0),
3954
    AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
3955
                          rc_initial_cplx, 0., 9999999., 0),
3956
    AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
3957
                          i_quant_factor, 0., 0., 0),
3958
    AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
3959
                          i_quant_factor, -999999., 999999., 0),
3960
    AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
3961
                       dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
3962
    AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
3963
                          lumi_masking, 0., 999999., 0),
3964
    AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
3965
                          temporal_cplx_masking, 0., 999999., 0),
3966
    AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
3967
                          spatial_cplx_masking, 0., 999999., 0),
3968
    AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
3969
                          p_masking, 0., 999999., 0),
3970
    AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
3971
                          dark_masking, 0., 999999., 0),
3972
    AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
3973
                       idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
3974

    
3975
    AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
3976
                       mb_qmin, 0, 8, 0),
3977
    AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
3978
                       mb_qmin, 0, 8, 0),
3979

    
3980
    AVOPTION_CODEC_INT("me_cmp", "ME compare function",
3981
                       me_cmp, 0, 24000000, 0),
3982
    AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
3983
                       me_sub_cmp, 0, 24000000, 0),
3984

    
3985

    
3986
    AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
3987
                       dia_size, 0, 24000000, 0),
3988
    AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
3989
                       last_predictor_count, 0, 24000000, 0),
3990

    
3991
    AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
3992
                       pre_me, 0, 24000000, 0),
3993
    AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
3994
                       me_pre_cmp, 0, 24000000, 0),
3995

    
3996
    AVOPTION_CODEC_INT("me_range", "maximum ME search range",
3997
                       me_range, 0, 24000000, 0),
3998
    AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
3999
                       pre_dia_size, 0, 24000000, 0),
4000
    AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
4001
                       me_subpel_quality, 0, 24000000, 0),
4002
    AVOPTION_CODEC_INT("me_range", "maximum ME search range",
4003
                       me_range, 0, 24000000, 0),
4004
    AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
4005
                        flags, CODEC_FLAG_PSNR, 0),
4006
    AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
4007
                              rc_override),
4008
    AVOPTION_SUB(avoptions_common),
4009
    AVOPTION_END()
4010
};
4011

    
4012
#ifdef CONFIG_ENCODERS
4013

    
4014
AVCodec mpeg1video_encoder = {
4015
    "mpeg1video",
4016
    CODEC_TYPE_VIDEO,
4017
    CODEC_ID_MPEG1VIDEO,
4018
    sizeof(MpegEncContext),
4019
    MPV_encode_init,
4020
    MPV_encode_picture,
4021
    MPV_encode_end,
4022
};
4023

    
4024
#ifdef CONFIG_RISKY
4025

    
4026
AVCodec h263_encoder = {
4027
    "h263",
4028
    CODEC_TYPE_VIDEO,
4029
    CODEC_ID_H263,
4030
    sizeof(MpegEncContext),
4031
    MPV_encode_init,
4032
    MPV_encode_picture,
4033
    MPV_encode_end,
4034
};
4035

    
4036
AVCodec h263p_encoder = {
4037
    "h263p",
4038
    CODEC_TYPE_VIDEO,
4039
    CODEC_ID_H263P,
4040
    sizeof(MpegEncContext),
4041
    MPV_encode_init,
4042
    MPV_encode_picture,
4043
    MPV_encode_end,
4044
};
4045

    
4046
AVCodec rv10_encoder = {
4047
    "rv10",
4048
    CODEC_TYPE_VIDEO,
4049
    CODEC_ID_RV10,
4050
    sizeof(MpegEncContext),
4051
    MPV_encode_init,
4052
    MPV_encode_picture,
4053
    MPV_encode_end,
4054
};
4055

    
4056
AVCodec mpeg4_encoder = {
4057
    "mpeg4",
4058
    CODEC_TYPE_VIDEO,
4059
    CODEC_ID_MPEG4,
4060
    sizeof(MpegEncContext),
4061
    MPV_encode_init,
4062
    MPV_encode_picture,
4063
    MPV_encode_end,
4064
    .options = mpeg4_options,
4065
};
4066

    
4067
AVCodec msmpeg4v1_encoder = {
4068
    "msmpeg4v1",
4069
    CODEC_TYPE_VIDEO,
4070
    CODEC_ID_MSMPEG4V1,
4071
    sizeof(MpegEncContext),
4072
    MPV_encode_init,
4073
    MPV_encode_picture,
4074
    MPV_encode_end,
4075
    .options = mpeg4_options,
4076
};
4077

    
4078
AVCodec msmpeg4v2_encoder = {
4079
    "msmpeg4v2",
4080
    CODEC_TYPE_VIDEO,
4081
    CODEC_ID_MSMPEG4V2,
4082
    sizeof(MpegEncContext),
4083
    MPV_encode_init,
4084
    MPV_encode_picture,
4085
    MPV_encode_end,
4086
    .options = mpeg4_options,
4087
};
4088

    
4089
AVCodec msmpeg4v3_encoder = {
4090
    "msmpeg4",
4091
    CODEC_TYPE_VIDEO,
4092
    CODEC_ID_MSMPEG4V3,
4093
    sizeof(MpegEncContext),
4094
    MPV_encode_init,
4095
    MPV_encode_picture,
4096
    MPV_encode_end,
4097
    .options = mpeg4_options,
4098
};
4099

    
4100
AVCodec wmv1_encoder = {
4101
    "wmv1",
4102
    CODEC_TYPE_VIDEO,
4103
    CODEC_ID_WMV1,
4104
    sizeof(MpegEncContext),
4105
    MPV_encode_init,
4106
    MPV_encode_picture,
4107
    MPV_encode_end,
4108
    .options = mpeg4_options,
4109
};
4110

    
4111
#endif
4112

    
4113
AVCodec mjpeg_encoder = {
4114
    "mjpeg",
4115
    CODEC_TYPE_VIDEO,
4116
    CODEC_ID_MJPEG,
4117
    sizeof(MpegEncContext),
4118
    MPV_encode_init,
4119
    MPV_encode_picture,
4120
    MPV_encode_end,
4121
};
4122

    
4123
#endif //CONFIG_ENCODERS
4124