Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ d6db1c9c

History | View | Annotate | Download (114 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
#include <ctype.h>
23
#include "avcodec.h"
24
#include "dsputil.h"
25
#include "mpegvideo.h"
26
#include "simple_idct.h"
27

    
28
#ifdef USE_FASTMEMCPY
29
#include "fastmemcpy.h"
30
#endif
31

    
32
//#undef NDEBUG
33
//#include <assert.h>
34

    
35
static void encode_picture(MpegEncContext *s, int picture_number);
36
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
37
                                   DCTELEM *block, int n, int qscale);
38
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
39
                                   DCTELEM *block, int n, int qscale);
40
static void dct_unquantize_h263_c(MpegEncContext *s, 
41
                                  DCTELEM *block, int n, int qscale);
42
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
43
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
44

    
45
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
46
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
47
                                    int src_x, int src_y, int w, int h);
48

    
49
#define EDGE_WIDTH 16
50

    
51
/* enable all paranoid tests for rounding, overflows, etc... */
52
//#define PARANOID
53

    
54
//#define DEBUG
55

    
56

    
57
/* for jpeg fast DCT */
58
#define CONST_BITS 14
59

    
60
static const unsigned short aanscales[64] = {
61
    /* precomputed values scaled up by 14 bits */
62
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
63
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
64
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
65
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
66
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
67
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
68
    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
69
    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
70
};
71

    
72
/* Input permutation for the simple_idct_mmx */
73
static const UINT8 simple_mmx_permutation[64]={
74
        0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, 
75
        0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, 
76
        0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, 
77
        0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, 
78
        0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, 
79
        0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, 
80
        0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, 
81
        0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
82
};
83

    
84
static UINT8 h263_chroma_roundtab[16] = {
85
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
86
};
87

    
88
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
89
static UINT8 default_fcode_tab[MAX_MV*2+1];
90

    
91
/* default motion estimation */
92
int motion_estimation_method = ME_EPZS;
93

    
94
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
95
                           const UINT16 *quant_matrix, int bias, int qmin, int qmax)
96
{
97
    int qscale;
98

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

    
134
                if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
135
                qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
136
            }
137
        }
138
    }
139
}
140
// move into common.c perhaps 
141
#define CHECKED_ALLOCZ(p, size)\
142
{\
143
    p= av_mallocz(size);\
144
    if(p==NULL){\
145
        perror("malloc");\
146
        goto fail;\
147
    }\
148
}
149

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

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

    
171
/* XXX: those functions should be suppressed ASAP when all IDCTs are
172
   converted */
173
static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
174
{
175
    j_rev_dct (block);
176
    put_pixels_clamped(block, dest, line_size);
177
}
178
static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
179
{
180
    j_rev_dct (block);
181
    add_pixels_clamped(block, dest, line_size);
182
}
183

    
184
/* init common dct for both encoder and decoder */
185
int DCT_common_init(MpegEncContext *s)
186
{
187
    int i;
188

    
189
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
190
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
191
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
192
    s->dct_quantize= dct_quantize_c;
193

    
194
    if(s->avctx->dct_algo==FF_DCT_FASTINT)
195
        s->fdct = fdct_ifast;
196
    else
197
        s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default
198

    
199
    if(s->avctx->idct_algo==FF_IDCT_INT){
200
        s->idct_put= ff_jref_idct_put;
201
        s->idct_add= ff_jref_idct_add;
202
        s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
203
    }else{ //accurate/default
204
        s->idct_put= simple_idct_put;
205
        s->idct_add= simple_idct_add;
206
        s->idct_permutation_type= FF_NO_IDCT_PERM;
207
    }
208
        
209
#ifdef HAVE_MMX
210
    MPV_common_init_mmx(s);
211
#endif
212
#ifdef ARCH_ALPHA
213
    MPV_common_init_axp(s);
214
#endif
215
#ifdef HAVE_MLIB
216
    MPV_common_init_mlib(s);
217
#endif
218
#ifdef HAVE_MMI
219
    MPV_common_init_mmi(s);
220
#endif
221
#ifdef ARCH_ARMV4L
222
    MPV_common_init_armv4l();
223
#endif
224

    
225
    switch(s->idct_permutation_type){
226
    case FF_NO_IDCT_PERM:
227
        for(i=0; i<64; i++)
228
            s->idct_permutation[i]= i;
229
        break;
230
    case FF_LIBMPEG2_IDCT_PERM:
231
        for(i=0; i<64; i++)
232
            s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
233
        break;
234
    case FF_SIMPLE_IDCT_PERM:
235
        for(i=0; i<64; i++)
236
            s->idct_permutation[i]= simple_mmx_permutation[i];
237
        break;
238
    case FF_TRANSPOSE_IDCT_PERM:
239
        for(i=0; i<64; i++)
240
            s->idct_permutation[i]= ((i&7)<<3) | (i>>3);
241
        break;
242
    default:
243
        fprintf(stderr, "Internal error, IDCT permutation not set\n");
244
        return -1;
245
    }
246

    
247

    
248
    /* load & permutate scantables
249
       note: only wmv uses differnt ones 
250
    */
251
    ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
252
    ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
253
    ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
254
    ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
255

    
256
    return 0;
257
}
258

    
259
/* init common structure for both encoder and decoder */
260
int MPV_common_init(MpegEncContext *s)
261
{
262
    UINT8 *pict;
263
    int y_size, c_size, yc_size, i;
264

    
265
    DCT_common_init(s);
266
    
267
    s->flags= s->avctx->flags;
268

    
269
    s->mb_width = (s->width + 15) / 16;
270
    s->mb_height = (s->height + 15) / 16;
271
    
272
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
273
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
274
    yc_size = y_size + 2 * c_size;
275
    
276
    /* set default edge pos, will be overriden in decode_header if needed */
277
    s->h_edge_pos= s->mb_width*16;
278
    s->v_edge_pos= s->mb_height*16;
279
    
280
    /* convert fourcc to upper case */
281
    s->avctx->fourcc=   toupper( s->avctx->fourcc     &0xFF)          
282
                     + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
283
                     + (toupper((s->avctx->fourcc>>16)&0xFF)<<16) 
284
                     + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
285

    
286
    s->mb_num = s->mb_width * s->mb_height;
287
    
288
    if(!(s->flags&CODEC_FLAG_DR1)){
289
      s->linesize   = s->mb_width * 16 + 2 * EDGE_WIDTH;
290
      s->uvlinesize = s->mb_width * 8  +     EDGE_WIDTH;
291

    
292
      for(i=0;i<3;i++) {
293
        int w, h, shift, pict_start, size;
294

    
295
        w = s->linesize;
296
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
297
        shift = (i == 0) ? 0 : 1;
298
        size = (s->linesize>>shift) * (h >> shift);
299
        pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
300

    
301
        CHECKED_ALLOCZ(pict, size)
302
        s->last_picture_base[i] = pict;
303
        s->last_picture[i] = pict + pict_start;
304
        if(i>0) memset(s->last_picture_base[i], 128, size);
305
    
306
        CHECKED_ALLOCZ(pict, size)
307
        s->next_picture_base[i] = pict;
308
        s->next_picture[i] = pict + pict_start;
309
        if(i>0) memset(s->next_picture_base[i], 128, size);
310
        
311
        if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
312
        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but 
313
           do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
314
            CHECKED_ALLOCZ(pict, size)
315
            s->aux_picture_base[i] = pict;
316
            s->aux_picture[i] = pict + pict_start;
317
            if(i>0) memset(s->aux_picture_base[i], 128, size);
318
        }
319
      }
320
      s->ip_buffer_count= 2;
321
    }
322
    
323
    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
324
    
325
    if (s->encoding) {
326
        int j;
327
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
328
        
329
        CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))
330
        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
331
        CHECKED_ALLOCZ(s->mb_mean  , s->mb_num * sizeof(INT8))
332

    
333
        /* Allocate MV tables */
334
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))
335
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))
336
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))
337
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
338
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
339
        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
340
        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
341
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))
342

    
343
        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))
344
        
345
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
346
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
347

    
348
        if(s->max_b_frames){
349
            for(j=0; j<REORDER_BUFFER_SIZE; j++){
350
                int i;
351
                for(i=0;i<3;i++) {
352
                    int w, h, shift, size;
353

    
354
                    w = s->linesize;
355
                    h = s->mb_height * 16;
356
                    shift = (i == 0) ? 0 : 1;
357
                    size = (w >> shift) * (h >> shift);
358

    
359
                    CHECKED_ALLOCZ(pict, size);
360
                    s->picture_buffer[j][i] = pict;
361
                }
362
            }
363
        }
364

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

    
383
        /* MV prediction */
384
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
385
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
386
    }
387

    
388
    if(s->codec_id==CODEC_ID_MPEG4){
389
        /* interlaced direct mode decoding tables */
390
        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
391
        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
392
    }
393
    /* 4mv b frame decoding table */
394
    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
395
    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
396
    if (s->out_format == FMT_H263) {
397
        /* ac values */
398
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(INT16) * 16);
399
        s->ac_val[1] = s->ac_val[0] + y_size;
400
        s->ac_val[2] = s->ac_val[1] + c_size;
401
        
402
        /* cbp values */
403
        CHECKED_ALLOCZ(s->coded_block, y_size);
404
        
405
        /* divx501 bitstream reorder buffer */
406
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
407
        
408
        /* cbp, ac_pred, pred_dir */
409
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
410
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
411
    }
412
    
413
    if (s->h263_pred || s->h263_plus || !s->encoding) {
414
        /* dc values */
415
        //MN: we need these for error resilience of intra-frames
416
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(INT16));
417
        s->dc_val[1] = s->dc_val[0] + y_size;
418
        s->dc_val[2] = s->dc_val[1] + c_size;
419
        for(i=0;i<yc_size;i++)
420
            s->dc_val[0][i] = 1024;
421
    }
422

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

    
438
    s->context_initialized = 1;
439
    return 0;
440
 fail:
441
    MPV_common_end(s);
442
    return -1;
443
}
444

    
445

    
446
//extern int sads;
447

    
448
/* init common structure for both encoder and decoder */
449
void MPV_common_end(MpegEncContext *s)
450
{
451
    int i;
452

    
453
    av_freep(&s->mb_type);
454
    av_freep(&s->mb_var);
455
    av_freep(&s->mc_mb_var);
456
    av_freep(&s->mb_mean);
457
    av_freep(&s->p_mv_table);
458
    av_freep(&s->b_forw_mv_table);
459
    av_freep(&s->b_back_mv_table);
460
    av_freep(&s->b_bidir_forw_mv_table);
461
    av_freep(&s->b_bidir_back_mv_table);
462
    av_freep(&s->b_direct_forw_mv_table);
463
    av_freep(&s->b_direct_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->qscale_table);
473
    av_freep(&s->me_scratchpad);
474
    av_freep(&s->me_map);
475
    av_freep(&s->me_score_map);
476
    
477
    av_freep(&s->mbskip_table);
478
    av_freep(&s->bitstream_buffer);
479
    av_freep(&s->tex_pb_buffer);
480
    av_freep(&s->pb2_buffer);
481
    av_freep(&s->edge_emu_buffer);
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<3;i++) {
490
        int j;
491
        if(!(s->flags&CODEC_FLAG_DR1)){
492
            av_freep(&s->last_picture_base[i]);
493
            av_freep(&s->next_picture_base[i]);
494
            av_freep(&s->aux_picture_base[i]);
495
        }
496
        s->last_picture_base[i]=
497
        s->next_picture_base[i]=
498
        s->aux_picture_base [i] = NULL;
499
        s->last_picture[i]=
500
        s->next_picture[i]=
501
        s->aux_picture [i] = NULL;
502

    
503
        for(j=0; j<REORDER_BUFFER_SIZE; j++){
504
            av_freep(&s->picture_buffer[j][i]);
505
        }
506
    }
507
    s->context_initialized = 0;
508
}
509

    
510
/* init video encoder */
511
int MPV_encode_init(AVCodecContext *avctx)
512
{
513
    MpegEncContext *s = avctx->priv_data;
514
    int i;
515

    
516
    avctx->pix_fmt = PIX_FMT_YUV420P;
517

    
518
    s->bit_rate = avctx->bit_rate;
519
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
520
    s->frame_rate = avctx->frame_rate;
521
    s->width = avctx->width;
522
    s->height = avctx->height;
523
    if(avctx->gop_size > 600){
524
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
525
        avctx->gop_size=600;
526
    }
527
    s->gop_size = avctx->gop_size;
528
    s->rtp_mode = avctx->rtp_mode;
529
    s->rtp_payload_size = avctx->rtp_payload_size;
530
    if (avctx->rtp_callback)
531
        s->rtp_callback = avctx->rtp_callback;
532
    s->qmin= avctx->qmin;
533
    s->qmax= avctx->qmax;
534
    s->max_qdiff= avctx->max_qdiff;
535
    s->qcompress= avctx->qcompress;
536
    s->qblur= avctx->qblur;
537
    s->avctx = avctx;
538
    s->aspect_ratio_info= avctx->aspect_ratio_info;
539
    if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
540
    {
541
        s->aspected_width = avctx->aspected_width;
542
        s->aspected_height = avctx->aspected_height;
543
    }
544
    s->flags= avctx->flags;
545
    s->max_b_frames= avctx->max_b_frames;
546
    s->b_frame_strategy= avctx->b_frame_strategy;
547
    s->codec_id= avctx->codec->id;
548
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
549
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
550
    s->strict_std_compliance= avctx->strict_std_compliance;
551
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
552
    s->mpeg_quant= avctx->mpeg_quant;
553

    
554
    if (s->gop_size <= 1) {
555
        s->intra_only = 1;
556
        s->gop_size = 12;
557
    } else {
558
        s->intra_only = 0;
559
    }
560

    
561
    /* ME algorithm */
562
    if (avctx->me_method == 0)
563
        /* For compatibility */
564
        s->me_method = motion_estimation_method;
565
    else
566
        s->me_method = avctx->me_method;
567

    
568
    /* Fixed QSCALE */
569
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
570
    
571
    s->adaptive_quant= (   s->avctx->lumi_masking
572
                        || s->avctx->dark_masking
573
                        || s->avctx->temporal_cplx_masking 
574
                        || s->avctx->spatial_cplx_masking
575
                        || s->avctx->p_masking)
576
                       && !s->fixed_qscale;
577
    
578
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
579

    
580
    switch(avctx->codec->id) {
581
    case CODEC_ID_MPEG1VIDEO:
582
        s->out_format = FMT_MPEG1;
583
        avctx->delay=0; //FIXME not sure, should check the spec
584
        break;
585
    case CODEC_ID_MJPEG:
586
        s->out_format = FMT_MJPEG;
587
        s->intra_only = 1; /* force intra only for jpeg */
588
        s->mjpeg_write_tables = 1; /* write all tables */
589
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
590
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
591
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
592
        s->mjpeg_vsample[2] = 1; 
593
        s->mjpeg_hsample[0] = 2;
594
        s->mjpeg_hsample[1] = 1; 
595
        s->mjpeg_hsample[2] = 1; 
596
        if (mjpeg_init(s) < 0)
597
            return -1;
598
        avctx->delay=0;
599
        break;
600
    case CODEC_ID_H263:
601
        if (h263_get_picture_format(s->width, s->height) == 7) {
602
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
603
            return -1;
604
        }
605
        s->out_format = FMT_H263;
606
        avctx->delay=0;
607
        break;
608
    case CODEC_ID_H263P:
609
        s->out_format = FMT_H263;
610
        s->h263_plus = 1;
611
        s->unrestricted_mv = 1;
612
        s->h263_aic = 1;
613
        
614
        /* These are just to be sure */
615
        s->umvplus = 0;
616
        s->umvplus_dec = 0;
617
        avctx->delay=0;
618
        break;
619
    case CODEC_ID_RV10:
620
        s->out_format = FMT_H263;
621
        s->h263_rv10 = 1;
622
        avctx->delay=0;
623
        break;
624
    case CODEC_ID_MPEG4:
625
        s->out_format = FMT_H263;
626
        s->h263_pred = 1;
627
        s->unrestricted_mv = 1;
628
        s->has_b_frames= s->max_b_frames ? 1 : 0;
629
        s->low_delay= !s->has_b_frames;
630
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
631
        break;
632
    case CODEC_ID_MSMPEG4V1:
633
        s->out_format = FMT_H263;
634
        s->h263_msmpeg4 = 1;
635
        s->h263_pred = 1;
636
        s->unrestricted_mv = 1;
637
        s->msmpeg4_version= 1;
638
        avctx->delay=0;
639
        break;
640
    case CODEC_ID_MSMPEG4V2:
641
        s->out_format = FMT_H263;
642
        s->h263_msmpeg4 = 1;
643
        s->h263_pred = 1;
644
        s->unrestricted_mv = 1;
645
        s->msmpeg4_version= 2;
646
        avctx->delay=0;
647
        break;
648
    case CODEC_ID_MSMPEG4V3:
649
        s->out_format = FMT_H263;
650
        s->h263_msmpeg4 = 1;
651
        s->h263_pred = 1;
652
        s->unrestricted_mv = 1;
653
        s->msmpeg4_version= 3;
654
        avctx->delay=0;
655
        break;
656
    case CODEC_ID_WMV1:
657
        s->out_format = FMT_H263;
658
        s->h263_msmpeg4 = 1;
659
        s->h263_pred = 1;
660
        s->unrestricted_mv = 1;
661
        s->msmpeg4_version= 4;
662
        avctx->delay=0;
663
        break;
664
    case CODEC_ID_WMV2:
665
        s->out_format = FMT_H263;
666
        s->h263_msmpeg4 = 1;
667
        s->h263_pred = 1;
668
        s->unrestricted_mv = 1;
669
        s->msmpeg4_version= 5;
670
        avctx->delay=0;
671
        break;
672
    default:
673
        return -1;
674
    }
675
    
676
    { /* set up some save defaults, some codecs might override them later */
677
        static int done=0;
678
        if(!done){
679
            int i;
680
            done=1;
681
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
682
            memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
683

    
684
            for(i=-16; i<16; i++){
685
                default_fcode_tab[i + MAX_MV]= 1;
686
            }
687
        }
688
    }
689
    s->mv_penalty= default_mv_penalty;
690
    s->fcode_tab= default_fcode_tab;
691
    s->y_dc_scale_table=
692
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
693
 
694
    /* dont use mv_penalty table for crap MV as it would be confused */
695
    if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
696

    
697
    s->encoding = 1;
698

    
699
    /* init */
700
    if (MPV_common_init(s) < 0)
701
        return -1;
702
    
703
    if (s->out_format == FMT_H263)
704
        h263_encode_init(s);
705
    else if (s->out_format == FMT_MPEG1)
706
        ff_mpeg1_encode_init(s);
707
    if(s->msmpeg4_version)
708
        ff_msmpeg4_encode_init(s);
709

    
710
    /* init default q matrix */
711
    for(i=0;i<64;i++) {
712
        int j= s->idct_permutation[i];
713
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
714
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
715
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
716
        }else if(s->out_format == FMT_H263){
717
            s->intra_matrix[j] =
718
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
719
        }else{ /* mpeg1 */
720
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
721
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
722
        }
723
    }
724

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

    
734
    if(ff_rate_control_init(s) < 0)
735
        return -1;
736

    
737
    s->picture_number = 0;
738
    s->picture_in_gop_number = 0;
739
    s->fake_picture_number = 0;
740
    /* motion detector init */
741
    s->f_code = 1;
742
    s->b_code = 1;
743

    
744
    return 0;
745
}
746

    
747
int MPV_encode_end(AVCodecContext *avctx)
748
{
749
    MpegEncContext *s = avctx->priv_data;
750

    
751
#ifdef STATS
752
    print_stats();
753
#endif
754

    
755
    ff_rate_control_uninit(s);
756

    
757
    MPV_common_end(s);
758
    if (s->out_format == FMT_MJPEG)
759
        mjpeg_close(s);
760
      
761
    return 0;
762
}
763

    
764
/* draw the edges of width 'w' of an image of size width, height */
765
//FIXME check that this is ok for mpeg4 interlaced
766
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
767
{
768
    UINT8 *ptr, *last_line;
769
    int i;
770

    
771
    last_line = buf + (height - 1) * wrap;
772
    for(i=0;i<w;i++) {
773
        /* top and bottom */
774
        memcpy(buf - (i + 1) * wrap, buf, width);
775
        memcpy(last_line + (i + 1) * wrap, last_line, width);
776
    }
777
    /* left and right */
778
    ptr = buf;
779
    for(i=0;i<height;i++) {
780
        memset(ptr - w, ptr[0], w);
781
        memset(ptr + width, ptr[width-1], w);
782
        ptr += wrap;
783
    }
784
    /* corners */
785
    for(i=0;i<w;i++) {
786
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
787
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
788
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
789
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
790
    }
791
}
792

    
793
/* generic function for encode/decode called before a frame is coded/decoded */
794
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
795
{
796
    int i;
797
    UINT8 *tmp;
798

    
799
    s->mb_skiped = 0;
800
    avctx->mbskip_table= s->mbskip_table;
801

    
802
    if(avctx->flags&CODEC_FLAG_DR1){
803
        if(avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type) < 0){
804
            fprintf(stderr, "get_buffer() failed\n");
805
            return -1;
806
        }
807

    
808
        s->linesize  = avctx->dr_stride;
809
        s->uvlinesize= avctx->dr_uvstride;
810
        s->ip_buffer_count= avctx->dr_ip_buffer_count;
811
    }
812
    avctx->dr_ip_buffer_count= s->ip_buffer_count;
813
    
814
    if (s->pict_type == B_TYPE) {
815
        for(i=0;i<3;i++) {
816
            if(avctx->flags&CODEC_FLAG_DR1)
817
                s->aux_picture[i]= avctx->dr_buffer[i];
818
            
819
            //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
820
            if(s->next_picture[i]==NULL)
821
                s->next_picture[i]= s->aux_picture[i];
822
            if(s->last_picture[i]==NULL)
823
                s->last_picture[i]= s->next_picture[i];
824

    
825
            s->current_picture[i] = s->aux_picture[i];
826
        }
827
    } else {
828
        for(i=0;i<3;i++) {
829
            /* swap next and last */
830
            if(avctx->flags&CODEC_FLAG_DR1)
831
                tmp= avctx->dr_buffer[i];
832
            else
833
                tmp = s->last_picture[i];
834

    
835
            s->last_picture[i] = s->next_picture[i];
836
            s->next_picture[i] = tmp;
837
            s->current_picture[i] = tmp;
838

    
839
            if(s->last_picture[i]==NULL)
840
                s->last_picture[i]= s->next_picture[i];
841

    
842
            s->last_dr_opaque= s->next_dr_opaque;
843
            s->next_dr_opaque= avctx->dr_opaque_frame;
844

    
845
            if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
846
                avctx->dr_opaque_frame= s->last_dr_opaque;
847
            else
848
                avctx->dr_opaque_frame= s->next_dr_opaque;
849
        }
850
    }
851
    /* set dequantizer, we cant do it during init as it might change for mpeg4
852
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
853
    if(s->out_format == FMT_H263){
854
        if(s->mpeg_quant)
855
            s->dct_unquantize = s->dct_unquantize_mpeg2;
856
        else
857
            s->dct_unquantize = s->dct_unquantize_h263;
858
    }else 
859
        s->dct_unquantize = s->dct_unquantize_mpeg1;
860

    
861
    return 0;
862
}
863

    
864
/* generic function for encode/decode called after a frame has been coded/decoded */
865
void MPV_frame_end(MpegEncContext *s)
866
{
867
    s->avctx->key_frame   = (s->pict_type == I_TYPE);
868
    s->avctx->pict_type   = s->pict_type;
869

    
870
    /* draw edge for correct motion prediction if outside */
871
    if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
872
        draw_edges(s->current_picture[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
873
        draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
874
        draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
875
    }
876
    emms_c();
877
    
878
    s->last_pict_type    = s->pict_type;
879
    if(s->pict_type!=B_TYPE){
880
        s->last_non_b_pict_type= s->pict_type;
881
        s->num_available_buffers++;
882
        if(s->num_available_buffers>2) s->num_available_buffers= 2;
883
    }
884
}
885

    
886
/* reorder input for encoding */
887
void reorder_input(MpegEncContext *s, AVPicture *pict)
888
{
889
    int i, j, index;
890
            
891
    if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
892

    
893
//        delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
894

    
895
    for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
896
        s->coded_order[j]= s->coded_order[j+1];
897
    }
898
    s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
899
    s->coded_order[j].pict_type=0;
900

    
901
    switch(s->input_pict_type){
902
    default: 
903
    case I_TYPE:
904
    case S_TYPE:
905
    case P_TYPE:
906
        index= s->max_b_frames - s->b_frames_since_non_b;
907
        s->b_frames_since_non_b=0;
908
        break;            
909
    case B_TYPE:
910
        index= s->max_b_frames + 1;
911
        s->b_frames_since_non_b++;
912
        break;          
913
    }
914
//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
915
    if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
916
       && pict->linesize[0] == s->linesize
917
       && pict->linesize[1] == s->uvlinesize
918
       && pict->linesize[2] == s->uvlinesize){
919
//printf("ptr\n");
920
        for(i=0; i<3; i++){
921
            s->coded_order[index].picture[i]= pict->data[i];
922
        }
923
    }else{
924
//printf("copy\n");
925
        for(i=0; i<3; i++){
926
            uint8_t *src = pict->data[i];
927
            uint8_t *dest;
928
            int src_wrap = pict->linesize[i];
929
            int dest_wrap = s->linesize;
930
            int w = s->width;
931
            int h = s->height;
932

    
933
            if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
934
            else         dest= s->picture_buffer[s->picture_buffer_index][i];
935

    
936
            if (i >= 1) {
937
                dest_wrap >>= 1;
938
                w >>= 1;
939
                h >>= 1;
940
            }
941

    
942
            s->coded_order[index].picture[i]= dest;
943
            for(j=0;j<h;j++) {
944
                memcpy(dest, src, w);
945
                dest += dest_wrap;
946
                src += src_wrap;
947
            }
948
        }
949
        if(index!=0){
950
            s->picture_buffer_index++;
951
            if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;
952
        }
953
    }
954
    s->coded_order[index].pict_type = s->input_pict_type;
955
    s->coded_order[index].qscale    = s->input_qscale;
956
    s->coded_order[index].force_type= s->force_input_type;
957
    s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
958
    s->coded_order[index].picture_number= s->input_picture_number;
959

    
960
    for(i=0; i<3; i++){
961
        s->new_picture[i]= s->coded_order[0].picture[i];
962
    }
963
}
964

    
965
int MPV_encode_picture(AVCodecContext *avctx,
966
                       unsigned char *buf, int buf_size, void *data)
967
{
968
    MpegEncContext *s = avctx->priv_data;
969
    AVPicture *pict = data;
970

    
971
    s->input_qscale = avctx->quality;
972

    
973
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
974

    
975
    if(avctx->flags&CODEC_FLAG_TYPE){
976
        s->input_pict_type=
977
        s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
978
    }else if(s->flags&CODEC_FLAG_PASS2){
979
        s->input_pict_type=
980
        s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
981
    }else{
982
        s->force_input_type=0;
983
        if (!s->intra_only) {
984
            /* first picture of GOP is intra */
985
            if (s->input_picture_in_gop_number % s->gop_size==0){
986
                s->input_pict_type = I_TYPE;
987
            }else if(s->max_b_frames==0){
988
                s->input_pict_type = P_TYPE;
989
            }else{
990
                if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
991
                    s->input_pict_type = B_TYPE;
992
                else
993
                    s->input_pict_type = P_TYPE;
994
            }
995
        } else {
996
            s->input_pict_type = I_TYPE;
997
        }
998
    }
999

    
1000
    if(s->input_pict_type==I_TYPE)
1001
        s->input_picture_in_gop_number=0;
1002
    
1003
    reorder_input(s, pict);
1004
    
1005
    /* output? */
1006
    if(s->coded_order[0].picture[0]){
1007

    
1008
        s->pict_type= s->coded_order[0].pict_type;
1009
        if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1010
            s->qscale= s->coded_order[0].qscale;
1011
        s->force_type= s->coded_order[0].force_type;
1012
        s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
1013
        s->picture_number= s->coded_order[0].picture_number;
1014

    
1015
        MPV_frame_start(s, avctx);
1016

    
1017
        encode_picture(s, s->picture_number);
1018
        
1019
        avctx->real_pict_num  = s->picture_number;
1020
        avctx->header_bits = s->header_bits;
1021
        avctx->mv_bits     = s->mv_bits;
1022
        avctx->misc_bits   = s->misc_bits;
1023
        avctx->i_tex_bits  = s->i_tex_bits;
1024
        avctx->p_tex_bits  = s->p_tex_bits;
1025
        avctx->i_count     = s->i_count;
1026
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1027
        avctx->skip_count  = s->skip_count;
1028

    
1029
        MPV_frame_end(s);
1030

    
1031
        if (s->out_format == FMT_MJPEG)
1032
            mjpeg_picture_trailer(s);
1033

    
1034
        if(!s->fixed_qscale)
1035
            avctx->quality = s->qscale;
1036
        
1037
        if(s->flags&CODEC_FLAG_PASS1)
1038
            ff_write_pass1_stats(s);
1039
    
1040
    }
1041

    
1042
    s->input_picture_number++;
1043
    s->input_picture_in_gop_number++;
1044

    
1045
    flush_put_bits(&s->pb);
1046
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1047
    
1048
    s->total_bits += s->frame_bits;
1049
    avctx->frame_bits  = s->frame_bits;
1050
//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
1051
//s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
1052
#if 0 //dump some stats to stats.txt for testing/debuging
1053
if(s->max_b_frames==0)
1054
{
1055
    static FILE *f=NULL;
1056
    if(!f) f= fopen("stats.txt", "wb");
1057
    get_psnr(pict->data, s->current_picture,
1058
             pict->linesize, s->linesize, avctx);
1059
    fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
1060
}
1061
#endif
1062

    
1063
    if (avctx->get_psnr) {
1064
        /* At this point pict->data should have the original frame   */
1065
        /* an s->current_picture should have the coded/decoded frame */
1066
        get_psnr(pict->data, s->current_picture,
1067
                 pict->linesize, s->linesize, avctx);
1068
//        printf("%f\n", avctx->psnr_y);
1069
    }
1070
    return pbBufPtr(&s->pb) - s->pb.buf;
1071
}
1072

    
1073
static inline void gmc1_motion(MpegEncContext *s,
1074
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1075
                               int dest_offset,
1076
                               UINT8 **ref_picture, int src_offset)
1077
{
1078
    UINT8 *ptr;
1079
    int offset, src_x, src_y, linesize, uvlinesize;
1080
    int motion_x, motion_y;
1081
    int emu=0;
1082

    
1083
    motion_x= s->sprite_offset[0][0];
1084
    motion_y= s->sprite_offset[0][1];
1085
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1086
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1087
    motion_x<<=(3-s->sprite_warping_accuracy);
1088
    motion_y<<=(3-s->sprite_warping_accuracy);
1089
    src_x = clip(src_x, -16, s->width);
1090
    if (src_x == s->width)
1091
        motion_x =0;
1092
    src_y = clip(src_y, -16, s->height);
1093
    if (src_y == s->height)
1094
        motion_y =0;
1095

    
1096
    linesize = s->linesize;
1097
    uvlinesize = s->uvlinesize;
1098
    
1099
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1100

    
1101
    dest_y+=dest_offset;
1102
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1103
        if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
1104
                              || src_y + (motion_y&15) + 16 > s->v_edge_pos){
1105
            emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1106
            ptr= s->edge_emu_buffer;
1107
            emu=1;
1108
        }
1109
    }
1110
    
1111
    if((motion_x|motion_y)&7){
1112
        ff_gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1113
        ff_gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1114
    }else{
1115
        int dxy;
1116
        
1117
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1118
        if (s->no_rounding){
1119
            put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1120
        }else{
1121
            put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1122
        }        
1123
    }
1124
    
1125
    if(s->flags&CODEC_FLAG_GRAY) return;
1126

    
1127
    motion_x= s->sprite_offset[1][0];
1128
    motion_y= s->sprite_offset[1][1];
1129
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1130
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1131
    motion_x<<=(3-s->sprite_warping_accuracy);
1132
    motion_y<<=(3-s->sprite_warping_accuracy);
1133
    src_x = clip(src_x, -8, s->width>>1);
1134
    if (src_x == s->width>>1)
1135
        motion_x =0;
1136
    src_y = clip(src_y, -8, s->height>>1);
1137
    if (src_y == s->height>>1)
1138
        motion_y =0;
1139

    
1140
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1141
    ptr = ref_picture[1] + offset;
1142
    if(emu){
1143
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1144
        ptr= s->edge_emu_buffer;
1145
    }
1146
    ff_gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1147
    
1148
    ptr = ref_picture[2] + offset;
1149
    if(emu){
1150
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1151
        ptr= s->edge_emu_buffer;
1152
    }
1153
    ff_gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1154
    
1155
    return;
1156
}
1157

    
1158
static inline void gmc_motion(MpegEncContext *s,
1159
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1160
                               int dest_offset,
1161
                               UINT8 **ref_picture, int src_offset)
1162
{
1163
    UINT8 *ptr;
1164
    int linesize, uvlinesize;
1165
    const int a= s->sprite_warping_accuracy;
1166
    int ox, oy;
1167

    
1168
    linesize = s->linesize;
1169
    uvlinesize = s->uvlinesize;
1170

    
1171
    ptr = ref_picture[0] + src_offset;
1172

    
1173
    dest_y+=dest_offset;
1174
    
1175
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1176
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1177

    
1178
    ff_gmc(dest_y, ptr, linesize, 16, 
1179
           ox, 
1180
           oy, 
1181
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1182
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1183
           a+1, (1<<(2*a+1)) - s->no_rounding,
1184
           s->h_edge_pos, s->v_edge_pos);
1185
    ff_gmc(dest_y+8, ptr, linesize, 16, 
1186
           ox + s->sprite_delta[0][0]*8, 
1187
           oy + s->sprite_delta[1][0]*8, 
1188
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1189
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1190
           a+1, (1<<(2*a+1)) - s->no_rounding,
1191
           s->h_edge_pos, s->v_edge_pos);
1192

    
1193
    if(s->flags&CODEC_FLAG_GRAY) return;
1194

    
1195

    
1196
    dest_cb+=dest_offset>>1;
1197
    dest_cr+=dest_offset>>1;
1198
    
1199
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1200
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1201

    
1202
    ptr = ref_picture[1] + (src_offset>>1);
1203
    ff_gmc(dest_cb, ptr, uvlinesize, 8, 
1204
           ox, 
1205
           oy, 
1206
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1207
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1208
           a+1, (1<<(2*a+1)) - s->no_rounding,
1209
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1210
    
1211
    ptr = ref_picture[2] + (src_offset>>1);
1212
    ff_gmc(dest_cr, ptr, uvlinesize, 8, 
1213
           ox, 
1214
           oy, 
1215
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1216
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1217
           a+1, (1<<(2*a+1)) - s->no_rounding,
1218
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1219
}
1220

    
1221

    
1222
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
1223
                                    int src_x, int src_y, int w, int h){
1224
    int x, y;
1225
    int start_y, start_x, end_y, end_x;
1226
    UINT8 *buf= s->edge_emu_buffer;
1227
    
1228
    if(src_y>= h){
1229
        src+= (h-1-src_y)*linesize;
1230
        src_y=h-1;
1231
    }else if(src_y<=-block_h){
1232
        src+= (1-block_h-src_y)*linesize;
1233
        src_y=1-block_h;
1234
    }
1235
    if(src_x>= w){
1236
        src+= (w-1-src_x);
1237
        src_x=w-1;
1238
    }else if(src_x<=-block_w){
1239
        src+= (1-block_w-src_x);
1240
        src_x=1-block_w;
1241
    }
1242

    
1243
    start_y= MAX(0, -src_y);
1244
    start_x= MAX(0, -src_x);
1245
    end_y= MIN(block_h, h-src_y);
1246
    end_x= MIN(block_w, w-src_x);
1247

    
1248
    // copy existing part
1249
    for(y=start_y; y<end_y; y++){
1250
        for(x=start_x; x<end_x; x++){
1251
            buf[x + y*linesize]= src[x + y*linesize];
1252
        }
1253
    }
1254

    
1255
    //top
1256
    for(y=0; y<start_y; y++){
1257
        for(x=start_x; x<end_x; x++){
1258
            buf[x + y*linesize]= buf[x + start_y*linesize];
1259
        }
1260
    }
1261

    
1262
    //bottom
1263
    for(y=end_y; y<block_h; y++){
1264
        for(x=start_x; x<end_x; x++){
1265
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1266
        }
1267
    }
1268
                                    
1269
    for(y=0; y<block_h; y++){
1270
       //left
1271
        for(x=0; x<start_x; x++){
1272
            buf[x + y*linesize]= buf[start_x + y*linesize];
1273
        }
1274
       
1275
       //right
1276
        for(x=end_x; x<block_w; x++){
1277
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1278
        }
1279
    }
1280
}
1281

    
1282

    
1283
/* apply one mpeg motion vector to the three components */
1284
static inline void mpeg_motion(MpegEncContext *s,
1285
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1286
                               int dest_offset,
1287
                               UINT8 **ref_picture, int src_offset,
1288
                               int field_based, op_pixels_func (*pix_op)[4],
1289
                               int motion_x, int motion_y, int h)
1290
{
1291
    UINT8 *ptr;
1292
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1293
    int emu=0;
1294
#if 0    
1295
if(s->quarter_sample)
1296
{
1297
    motion_x>>=1;
1298
    motion_y>>=1;
1299
}
1300
#endif
1301
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1302
    src_x = s->mb_x * 16 + (motion_x >> 1);
1303
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1304
                
1305
    /* WARNING: do no forget half pels */
1306
    height = s->height >> field_based;
1307
    v_edge_pos = s->v_edge_pos >> field_based;
1308
    src_x = clip(src_x, -16, s->width);
1309
    if (src_x == s->width)
1310
        dxy &= ~1;
1311
    src_y = clip(src_y, -16, height);
1312
    if (src_y == height)
1313
        dxy &= ~2;
1314
    linesize   = s->linesize << field_based;
1315
    uvlinesize = s->uvlinesize << field_based;
1316
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1317
    dest_y += dest_offset;
1318

    
1319
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1320
        if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1321
                              || src_y + (motion_y&1) + h  > v_edge_pos){
1322
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1323
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1324
            ptr= s->edge_emu_buffer + src_offset;
1325
            emu=1;
1326
        }
1327
    }
1328
    pix_op[0][dxy](dest_y, ptr, linesize, h);
1329

    
1330
    if(s->flags&CODEC_FLAG_GRAY) return;
1331

    
1332
    if (s->out_format == FMT_H263) {
1333
        dxy = 0;
1334
        if ((motion_x & 3) != 0)
1335
            dxy |= 1;
1336
        if ((motion_y & 3) != 0)
1337
            dxy |= 2;
1338
        mx = motion_x >> 2;
1339
        my = motion_y >> 2;
1340
    } else {
1341
        mx = motion_x / 2;
1342
        my = motion_y / 2;
1343
        dxy = ((my & 1) << 1) | (mx & 1);
1344
        mx >>= 1;
1345
        my >>= 1;
1346
    }
1347
    
1348
    src_x = s->mb_x * 8 + mx;
1349
    src_y = s->mb_y * (8 >> field_based) + my;
1350
    src_x = clip(src_x, -8, s->width >> 1);
1351
    if (src_x == (s->width >> 1))
1352
        dxy &= ~1;
1353
    src_y = clip(src_y, -8, height >> 1);
1354
    if (src_y == (height >> 1))
1355
        dxy &= ~2;
1356
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1357
    ptr = ref_picture[1] + offset;
1358
    if(emu){
1359
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1360
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1361
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1362
    }
1363
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1364

    
1365
    ptr = ref_picture[2] + offset;
1366
    if(emu){
1367
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1368
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1369
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1370
    }
1371
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1372
}
1373

    
1374
static inline void qpel_motion(MpegEncContext *s,
1375
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1376
                               int dest_offset,
1377
                               UINT8 **ref_picture, int src_offset,
1378
                               int field_based, op_pixels_func (*pix_op)[4],
1379
                               qpel_mc_func (*qpix_op)[16],
1380
                               int motion_x, int motion_y, int h)
1381
{
1382
    UINT8 *ptr;
1383
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1384
    int emu=0;
1385

    
1386
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1387
    src_x = s->mb_x * 16 + (motion_x >> 2);
1388
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1389

    
1390
    height = s->height >> field_based;
1391
    v_edge_pos = s->v_edge_pos >> field_based;
1392
    src_x = clip(src_x, -16, s->width);
1393
    if (src_x == s->width)
1394
        dxy &= ~3;
1395
    src_y = clip(src_y, -16, height);
1396
    if (src_y == height)
1397
        dxy &= ~12;
1398
    linesize = s->linesize << field_based;
1399
    uvlinesize = s->uvlinesize << field_based;
1400
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1401
    dest_y += dest_offset;
1402
//printf("%d %d %d\n", src_x, src_y, dxy);
1403
    
1404
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1405
        if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1406
                              || src_y + (motion_y&3) + h  > v_edge_pos){
1407
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1408
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1409
            ptr= s->edge_emu_buffer + src_offset;
1410
            emu=1;
1411
        }
1412
    }
1413
    if(!field_based)
1414
        qpix_op[0][dxy](dest_y, ptr, linesize);
1415
    else{
1416
        //damn interlaced mode
1417
        //FIXME boundary mirroring is not exactly correct here
1418
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
1419
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1420
    }
1421

    
1422
    if(s->flags&CODEC_FLAG_GRAY) return;
1423

    
1424
    if(field_based){
1425
        mx= motion_x/2;
1426
        my= motion_y>>1;
1427
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1428
        mx= (motion_x>>1)|(motion_x&1);
1429
        my= (motion_y>>1)|(motion_y&1);
1430
    }else{
1431
        mx= motion_x/2;
1432
        my= motion_y/2;
1433
    }
1434
    mx= (mx>>1)|(mx&1);
1435
    my= (my>>1)|(my&1);
1436
    dxy= (mx&1) | ((my&1)<<1);
1437
    mx>>=1;
1438
    my>>=1;
1439

    
1440
    src_x = s->mb_x * 8 + mx;
1441
    src_y = s->mb_y * (8 >> field_based) + my;
1442
    src_x = clip(src_x, -8, s->width >> 1);
1443
    if (src_x == (s->width >> 1))
1444
        dxy &= ~1;
1445
    src_y = clip(src_y, -8, height >> 1);
1446
    if (src_y == (height >> 1))
1447
        dxy &= ~2;
1448

    
1449
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1450
    ptr = ref_picture[1] + offset;
1451
    if(emu){
1452
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1453
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1454
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1455
    }
1456
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1457
    
1458
    ptr = ref_picture[2] + offset;
1459
    if(emu){
1460
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1461
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1462
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1463
    }
1464
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1465
}
1466

    
1467

    
1468
static inline void MPV_motion(MpegEncContext *s, 
1469
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1470
                              int dir, UINT8 **ref_picture, 
1471
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1472
{
1473
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1474
    int mb_x, mb_y, i;
1475
    UINT8 *ptr, *dest;
1476
    int emu=0;
1477

    
1478
    mb_x = s->mb_x;
1479
    mb_y = s->mb_y;
1480

    
1481
    switch(s->mv_type) {
1482
    case MV_TYPE_16X16:
1483
        if(s->mcsel){
1484
            if(s->real_sprite_warping_points==1){
1485
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1486
                            ref_picture, 0);
1487
            }else{
1488
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1489
                            ref_picture, 0);
1490
            }
1491
        }else if(s->quarter_sample){
1492
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1493
                        ref_picture, 0,
1494
                        0, pix_op, qpix_op,
1495
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1496
        }else{
1497
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1498
                        ref_picture, 0,
1499
                        0, pix_op,
1500
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1501
        }           
1502
        break;
1503
    case MV_TYPE_8X8:
1504
        mx = 0;
1505
        my = 0;
1506
        if(s->quarter_sample){
1507
            for(i=0;i<4;i++) {
1508
                motion_x = s->mv[dir][i][0];
1509
                motion_y = s->mv[dir][i][1];
1510

    
1511
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1512
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1513
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1514
                    
1515
                /* WARNING: do no forget half pels */
1516
                src_x = clip(src_x, -16, s->width);
1517
                if (src_x == s->width)
1518
                    dxy &= ~3;
1519
                src_y = clip(src_y, -16, s->height);
1520
                if (src_y == s->height)
1521
                    dxy &= ~12;
1522
                    
1523
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1524
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1525
                    if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1526
                                          || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1527
                        emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1528
                        ptr= s->edge_emu_buffer;
1529
                    }
1530
                }
1531
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1532
                qpix_op[1][dxy](dest, ptr, s->linesize);
1533

    
1534
                mx += s->mv[dir][i][0]/2;
1535
                my += s->mv[dir][i][1]/2;
1536
            }
1537
        }else{
1538
            for(i=0;i<4;i++) {
1539
                motion_x = s->mv[dir][i][0];
1540
                motion_y = s->mv[dir][i][1];
1541

    
1542
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1543
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1544
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1545
                    
1546
                /* WARNING: do no forget half pels */
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, s->height);
1551
                if (src_y == s->height)
1552
                    dxy &= ~2;
1553
                    
1554
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1555
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1556
                    if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1557
                                          || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1558
                        emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1559
                        ptr= s->edge_emu_buffer;
1560
                    }
1561
                }
1562
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1563
                pix_op[1][dxy](dest, ptr, s->linesize, 8);
1564

    
1565
                mx += s->mv[dir][i][0];
1566
                my += s->mv[dir][i][1];
1567
            }
1568
        }
1569

    
1570
        if(s->flags&CODEC_FLAG_GRAY) break;
1571
        /* In case of 8X8, we construct a single chroma motion vector
1572
           with a special rounding */
1573
        for(i=0;i<4;i++) {
1574
        }
1575
        if (mx >= 0)
1576
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1577
        else {
1578
            mx = -mx;
1579
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1580
        }
1581
        if (my >= 0)
1582
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1583
        else {
1584
            my = -my;
1585
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1586
        }
1587
        dxy = ((my & 1) << 1) | (mx & 1);
1588
        mx >>= 1;
1589
        my >>= 1;
1590

    
1591
        src_x = mb_x * 8 + mx;
1592
        src_y = mb_y * 8 + my;
1593
        src_x = clip(src_x, -8, s->width/2);
1594
        if (src_x == s->width/2)
1595
            dxy &= ~1;
1596
        src_y = clip(src_y, -8, s->height/2);
1597
        if (src_y == s->height/2)
1598
            dxy &= ~2;
1599
        
1600
        offset = (src_y * (s->uvlinesize)) + src_x;
1601
        ptr = ref_picture[1] + offset;
1602
        if(s->flags&CODEC_FLAG_EMU_EDGE){
1603
                if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1604
                                      || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1605
                    emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1606
                    ptr= s->edge_emu_buffer;
1607
                    emu=1;
1608
                }
1609
            }
1610
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1611

    
1612
        ptr = ref_picture[2] + offset;
1613
        if(emu){
1614
            emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1615
            ptr= s->edge_emu_buffer;
1616
        }
1617
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1618
        break;
1619
    case MV_TYPE_FIELD:
1620
        if (s->picture_structure == PICT_FRAME) {
1621
            if(s->quarter_sample){
1622
                /* top field */
1623
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1624
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1625
                            1, pix_op, qpix_op,
1626
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1627
                /* bottom field */
1628
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1629
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1630
                            1, pix_op, qpix_op,
1631
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1632
            }else{
1633
                /* top field */       
1634
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1635
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1636
                            1, pix_op,
1637
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1638
                /* bottom field */
1639
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1640
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1641
                            1, pix_op,
1642
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1643
            }
1644
        } else {
1645
            
1646

    
1647
        }
1648
        break;
1649
    }
1650
}
1651

    
1652

    
1653
/* put block[] to dest[] */
1654
static inline void put_dct(MpegEncContext *s, 
1655
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1656
{
1657
    s->dct_unquantize(s, block, i, s->qscale);
1658
    s->idct_put (dest, line_size, block);
1659
}
1660

    
1661
/* add block[] to dest[] */
1662
static inline void add_dct(MpegEncContext *s, 
1663
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1664
{
1665
    if (s->block_last_index[i] >= 0) {
1666
        s->idct_add (dest, line_size, block);
1667
    }
1668
}
1669

    
1670
static inline void add_dequant_dct(MpegEncContext *s, 
1671
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1672
{
1673
    if (s->block_last_index[i] >= 0) {
1674
        s->dct_unquantize(s, block, i, s->qscale);
1675

    
1676
        s->idct_add (dest, line_size, block);
1677
    }
1678
}
1679

    
1680
/**
1681
 * cleans dc, ac, coded_block for the current non intra MB
1682
 */
1683
void ff_clean_intra_table_entries(MpegEncContext *s)
1684
{
1685
    int wrap = s->block_wrap[0];
1686
    int xy = s->block_index[0];
1687
    
1688
    s->dc_val[0][xy           ] = 
1689
    s->dc_val[0][xy + 1       ] = 
1690
    s->dc_val[0][xy     + wrap] =
1691
    s->dc_val[0][xy + 1 + wrap] = 1024;
1692
    /* ac pred */
1693
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(INT16));
1694
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
1695
    if (s->msmpeg4_version>=3) {
1696
        s->coded_block[xy           ] =
1697
        s->coded_block[xy + 1       ] =
1698
        s->coded_block[xy     + wrap] =
1699
        s->coded_block[xy + 1 + wrap] = 0;
1700
    }
1701
    /* chroma */
1702
    wrap = s->block_wrap[4];
1703
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1704
    s->dc_val[1][xy] =
1705
    s->dc_val[2][xy] = 1024;
1706
    /* ac pred */
1707
    memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1708
    memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1709
    
1710
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1711
}
1712

    
1713
/* generic function called after a macroblock has been parsed by the
1714
   decoder or after it has been encoded by the encoder.
1715

1716
   Important variables used:
1717
   s->mb_intra : true if intra macroblock
1718
   s->mv_dir   : motion vector direction
1719
   s->mv_type  : motion vector type
1720
   s->mv       : motion vector
1721
   s->interlaced_dct : true if interlaced dct used (mpeg2)
1722
 */
1723
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1724
{
1725
    int mb_x, mb_y;
1726
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1727

    
1728
    mb_x = s->mb_x;
1729
    mb_y = s->mb_y;
1730

    
1731
#ifdef FF_POSTPROCESS
1732
    /* Obsolete. Exists for compatibility with mplayer only. */
1733
    quant_store[mb_y][mb_x]=s->qscale;
1734
    //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1735
#else
1736
    /* even more obsolete, exists for mplayer xp only */
1737
    if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
1738
#endif
1739
    s->qscale_table[mb_xy]= s->qscale;
1740

    
1741
    /* update DC predictors for P macroblocks */
1742
    if (!s->mb_intra) {
1743
        if (s->h263_pred || s->h263_aic) {
1744
            if(s->mbintra_table[mb_xy])
1745
                ff_clean_intra_table_entries(s);
1746
        } else {
1747
            s->last_dc[0] =
1748
            s->last_dc[1] =
1749
            s->last_dc[2] = 128 << s->intra_dc_precision;
1750
        }
1751
    }
1752
    else if (s->h263_pred || s->h263_aic)
1753
        s->mbintra_table[mb_xy]=1;
1754

    
1755
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1756
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1757
        
1758
        const int wrap = s->block_wrap[0];
1759
        const int xy = s->block_index[0];
1760
        const int mb_index= s->mb_x + s->mb_y*s->mb_width;
1761
        if(s->mv_type == MV_TYPE_8X8){
1762
            s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
1763
        } else {
1764
            int motion_x, motion_y;
1765
            if (s->mb_intra) {
1766
                motion_x = 0;
1767
                motion_y = 0;
1768
                if(s->co_located_type_table)
1769
                    s->co_located_type_table[mb_index]= 0;
1770
            } else if (s->mv_type == MV_TYPE_16X16) {
1771
                motion_x = s->mv[0][0][0];
1772
                motion_y = s->mv[0][0][1];
1773
                if(s->co_located_type_table)
1774
                    s->co_located_type_table[mb_index]= 0;
1775
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1776
                int i;
1777
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
1778
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
1779
                motion_x = (motion_x>>1) | (motion_x&1);
1780
                for(i=0; i<2; i++){
1781
                    s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
1782
                    s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
1783
                    s->field_select_table[mb_index][i]= s->field_select[0][i];
1784
                }
1785
                s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
1786
            }
1787
            /* no update if 8X8 because it has been done during parsing */
1788
            s->motion_val[xy][0] = motion_x;
1789
            s->motion_val[xy][1] = motion_y;
1790
            s->motion_val[xy + 1][0] = motion_x;
1791
            s->motion_val[xy + 1][1] = motion_y;
1792
            s->motion_val[xy + wrap][0] = motion_x;
1793
            s->motion_val[xy + wrap][1] = motion_y;
1794
            s->motion_val[xy + 1 + wrap][0] = motion_x;
1795
            s->motion_val[xy + 1 + wrap][1] = motion_y;
1796
        }
1797
    }
1798
    
1799
    if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1800
        UINT8 *dest_y, *dest_cb, *dest_cr;
1801
        int dct_linesize, dct_offset;
1802
        op_pixels_func (*op_pix)[4];
1803
        qpel_mc_func (*op_qpix)[16];
1804

    
1805
        /* avoid copy if macroblock skipped in last frame too 
1806
           dont touch it for B-frames as they need the skip info from the next p-frame */
1807
        if (s->pict_type != B_TYPE) {
1808
            UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
1809
            if (s->mb_skiped) {
1810
                s->mb_skiped = 0;
1811

    
1812
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
1813
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
1814

    
1815
                /* if previous was skipped too, then nothing to do ! 
1816
                   skip only during decoding as we might trash the buffers during encoding a bit */
1817
                if (*mbskip_ptr >= s->ip_buffer_count  && !s->encoding) 
1818
                    goto the_end;
1819
            } else {
1820
                *mbskip_ptr = 0; /* not skipped */
1821
            }
1822
        }
1823

    
1824
        if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
1825
            dest_y = s->current_picture [0] + mb_x * 16;
1826
            dest_cb = s->current_picture[1] + mb_x * 8;
1827
            dest_cr = s->current_picture[2] + mb_x * 8;
1828
        }else{
1829
            dest_y = s->current_picture [0] + (mb_y * 16* s->linesize  ) + mb_x * 16;
1830
            dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
1831
            dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
1832
        }
1833

    
1834
        if (s->interlaced_dct) {
1835
            dct_linesize = s->linesize * 2;
1836
            dct_offset = s->linesize;
1837
        } else {
1838
            dct_linesize = s->linesize;
1839
            dct_offset = s->linesize * 8;
1840
        }
1841

    
1842
        if (!s->mb_intra) {
1843
            /* motion handling */
1844
            /* decoding or more than one mb_type (MC was allready done otherwise) */
1845
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
1846
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
1847
                    op_pix = put_pixels_tab;
1848
                    op_qpix= put_qpel_pixels_tab;
1849
                }else{
1850
                    op_pix = put_no_rnd_pixels_tab;
1851
                    op_qpix= put_no_rnd_qpel_pixels_tab;
1852
                }
1853

    
1854
                if (s->mv_dir & MV_DIR_FORWARD) {
1855
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1856
                    op_pix = avg_pixels_tab;
1857
                    op_qpix= avg_qpel_pixels_tab;
1858
                }
1859
                if (s->mv_dir & MV_DIR_BACKWARD) {
1860
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1861
                }
1862
            }
1863

    
1864
            /* skip dequant / idct if we are really late ;) */
1865
            if(s->hurry_up>1) goto the_end;
1866

    
1867
            /* add dct residue */
1868
            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
1869
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
1870
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
1871
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1872
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1873
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1874

    
1875
                if(!(s->flags&CODEC_FLAG_GRAY)){
1876
                    add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
1877
                    add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
1878
                }
1879
            } else {
1880
                add_dct(s, block[0], 0, dest_y, dct_linesize);
1881
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1882
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1883
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1884

    
1885
                if(!(s->flags&CODEC_FLAG_GRAY)){
1886
                    add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
1887
                    add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
1888
                }
1889
            }
1890
        } else {
1891
            /* dct only in intra block */
1892
            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
1893
                put_dct(s, block[0], 0, dest_y, dct_linesize);
1894
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1895
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1896
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1897

    
1898
                if(!(s->flags&CODEC_FLAG_GRAY)){
1899
                    put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
1900
                    put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
1901
                }
1902
            }else{
1903
                s->idct_put(dest_y                 , dct_linesize, block[0]);
1904
                s->idct_put(dest_y              + 8, dct_linesize, block[1]);
1905
                s->idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
1906
                s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
1907

    
1908
                if(!(s->flags&CODEC_FLAG_GRAY)){
1909
                    s->idct_put(dest_cb, s->uvlinesize, block[4]);
1910
                    s->idct_put(dest_cr, s->uvlinesize, block[5]);
1911
                }
1912
            }
1913
        }
1914
    }
1915
 the_end:
1916
    emms_c(); //FIXME remove
1917
}
1918

    
1919
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
1920
{
1921
    static const char tab[64]=
1922
        {3,2,2,1,1,1,1,1,
1923
         1,1,1,1,1,1,1,1,
1924
         1,1,1,1,1,1,1,1,
1925
         0,0,0,0,0,0,0,0,
1926
         0,0,0,0,0,0,0,0,
1927
         0,0,0,0,0,0,0,0,
1928
         0,0,0,0,0,0,0,0,
1929
         0,0,0,0,0,0,0,0};
1930
    int score=0;
1931
    int run=0;
1932
    int i;
1933
    DCTELEM *block= s->block[n];
1934
    const int last_index= s->block_last_index[n];
1935
    int skip_dc;
1936

    
1937
    if(threshold<0){
1938
        skip_dc=0;
1939
        threshold= -threshold;
1940
    }else
1941
        skip_dc=1;
1942

    
1943
    /* are all which we could set to zero are allready zero? */
1944
    if(last_index<=skip_dc - 1) return;
1945

    
1946
    for(i=0; i<=last_index; i++){
1947
        const int j = s->intra_scantable.permutated[i];
1948
        const int level = ABS(block[j]);
1949
        if(level==1){
1950
            if(skip_dc && i==0) continue;
1951
            score+= tab[run];
1952
            run=0;
1953
        }else if(level>1){
1954
            return;
1955
        }else{
1956
            run++;
1957
        }
1958
    }
1959
    if(score >= threshold) return;
1960
    for(i=skip_dc; i<=last_index; i++){
1961
        const int j = s->intra_scantable.permutated[i];
1962
        block[j]=0;
1963
    }
1964
    if(block[0]) s->block_last_index[n]= 0;
1965
    else         s->block_last_index[n]= -1;
1966
}
1967

    
1968
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1969
{
1970
    int i;
1971
    const int maxlevel= s->max_qcoeff;
1972
    const int minlevel= s->min_qcoeff;
1973
    
1974
    if(s->mb_intra){
1975
        i=1; //skip clipping of intra dc
1976
    }else
1977
        i=0;
1978
    
1979
    for(;i<=last_index; i++){
1980
        const int j= s->intra_scantable.permutated[i];
1981
        int level = block[j];
1982
       
1983
        if     (level>maxlevel) level=maxlevel;
1984
        else if(level<minlevel) level=minlevel;
1985
        block[j]= level;
1986
    }
1987
}
1988

    
1989
static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
1990
{
1991
    int i;
1992

    
1993
    if(s->mb_intra){
1994
        i=1; //skip clipping of intra dc
1995
         //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
1996
    }else
1997
        i=0;
1998
    
1999
    for(;i<=s->block_last_index[n]; i++){
2000
        const int j = s->intra_scantable.permutated[i];
2001
        int level = block[j];
2002
        
2003
        block[j]= ROUNDED_DIV(level*oldq, newq);
2004
    }
2005

    
2006
    for(i=s->block_last_index[n]; i>=0; i--){
2007
        const int j = s->intra_scantable.permutated[i];
2008
        if(block[j]) break;
2009
    }
2010
    s->block_last_index[n]= i;
2011
}
2012

    
2013
static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
2014
{
2015
    int i,n, newq;
2016
    const int maxlevel= s->max_qcoeff;
2017
    const int minlevel= s->min_qcoeff;
2018
    int largest=0, smallest=0;
2019

    
2020
    assert(s->adaptive_quant);
2021
    
2022
    for(n=0; n<6; n++){
2023
        if(s->mb_intra){
2024
            i=1; //skip clipping of intra dc
2025
             //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
2026
        }else
2027
            i=0;
2028

    
2029
        for(;i<=s->block_last_index[n]; i++){
2030
            const int j = s->intra_scantable.permutated[i];
2031
            int level = block[n][j];
2032
            if(largest  < level) largest = level;
2033
            if(smallest > level) smallest= level;
2034
        }
2035
    }
2036
    
2037
    for(newq=s->qscale+1; newq<32; newq++){
2038
        if(   ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
2039
           && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel) 
2040
            break;
2041
    }
2042
        
2043
    if(s->out_format==FMT_H263){
2044
        /* h263 like formats cannot change qscale by more than 2 easiely */
2045
        if(s->avctx->qmin + 2 < newq)
2046
            newq= s->avctx->qmin + 2;
2047
    }
2048

    
2049
    for(n=0; n<6; n++){
2050
        requantize_coeffs(s, block[n], s->qscale, newq, n);
2051
        clip_coeffs(s, block[n], s->block_last_index[n]);
2052
    }
2053
     
2054
    s->dquant+= newq - s->qscale;
2055
    s->qscale= newq;
2056
}
2057
#if 0
2058
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
2059
    int score=0;
2060
    int x,y;
2061
    
2062
    for(y=0; y<7; y++){
2063
        for(x=0; x<16; x+=4){
2064
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2065
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2066
        }
2067
        s+= stride;
2068
    }
2069
    
2070
    return score;
2071
}
2072

2073
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
2074
    int score=0;
2075
    int x,y;
2076
    
2077
    for(y=0; y<7; y++){
2078
        for(x=0; x<16; x++){
2079
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2080
        }
2081
        s1+= stride;
2082
        s2+= stride;
2083
    }
2084
    
2085
    return score;
2086
}
2087
#else
2088
#define SQ(a) ((a)*(a))
2089

    
2090
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
2091
    int score=0;
2092
    int x,y;
2093
    
2094
    for(y=0; y<7; y++){
2095
        for(x=0; x<16; x+=4){
2096
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2097
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2098
        }
2099
        s+= stride;
2100
    }
2101
    
2102
    return score;
2103
}
2104

    
2105
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
2106
    int score=0;
2107
    int x,y;
2108
    
2109
    for(y=0; y<7; y++){
2110
        for(x=0; x<16; x++){
2111
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2112
        }
2113
        s1+= stride;
2114
        s2+= stride;
2115
    }
2116
    
2117
    return score;
2118
}
2119

    
2120
#endif
2121

    
2122
void ff_draw_horiz_band(MpegEncContext *s){
2123
    if (    s->avctx->draw_horiz_band 
2124
        && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
2125
        UINT8 *src_ptr[3];
2126
        int y, h, offset;
2127
        y = s->mb_y * 16;
2128
        h = s->height - y;
2129
        if (h > 16)
2130
            h = 16;
2131

    
2132
        if(s->pict_type==B_TYPE)
2133
            offset = 0;
2134
        else
2135
            offset = y * s->linesize;
2136

    
2137
        if(s->pict_type==B_TYPE || (!s->has_b_frames)){
2138
            src_ptr[0] = s->current_picture[0] + offset;
2139
            src_ptr[1] = s->current_picture[1] + (offset >> 2);
2140
            src_ptr[2] = s->current_picture[2] + (offset >> 2);
2141
        } else {
2142
            src_ptr[0] = s->last_picture[0] + offset;
2143
            src_ptr[1] = s->last_picture[1] + (offset >> 2);
2144
            src_ptr[2] = s->last_picture[2] + (offset >> 2);
2145
        }
2146
        s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2147
                               y, s->width, h);
2148
    }
2149
}
2150

    
2151
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2152
{
2153
    const int mb_x= s->mb_x;
2154
    const int mb_y= s->mb_y;
2155
    int i;
2156
    int skip_dct[6];
2157
    int dct_offset   = s->linesize*8; //default for progressive frames
2158
    
2159
    for(i=0; i<6; i++) skip_dct[i]=0;
2160
    
2161
    if(s->adaptive_quant){
2162
        s->dquant= s->qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2163

    
2164
        if(s->out_format==FMT_H263){
2165
            if     (s->dquant> 2) s->dquant= 2;
2166
            else if(s->dquant<-2) s->dquant=-2;
2167
        }
2168
            
2169
        if(s->codec_id==CODEC_ID_MPEG4){        
2170
            if(!s->mb_intra){
2171
                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2172

    
2173
                if(s->mv_dir&MV_DIRECT)
2174
                    s->dquant=0;
2175
            }
2176
        }
2177
        s->qscale+= s->dquant;
2178
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2179
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2180
    }
2181

    
2182
    if (s->mb_intra) {
2183
        UINT8 *ptr;
2184
        int wrap_y;
2185
        int emu=0;
2186

    
2187
        wrap_y = s->linesize;
2188
        ptr = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2189

    
2190
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2191
            emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2192
            ptr= s->edge_emu_buffer;
2193
            emu=1;
2194
        }
2195
        
2196
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2197
            int progressive_score, interlaced_score;
2198
            
2199
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2200
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2201
            
2202
            if(progressive_score > interlaced_score + 100){
2203
                s->interlaced_dct=1;
2204
            
2205
                dct_offset= wrap_y;
2206
                wrap_y<<=1;
2207
            }else
2208
                s->interlaced_dct=0;
2209
        }
2210
        
2211
        get_pixels(s->block[0], ptr                 , wrap_y);
2212
        get_pixels(s->block[1], ptr              + 8, wrap_y);
2213
        get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2214
        get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2215

    
2216
        if(s->flags&CODEC_FLAG_GRAY){
2217
            skip_dct[4]= 1;
2218
            skip_dct[5]= 1;
2219
        }else{
2220
            int wrap_c = s->uvlinesize;
2221
            ptr = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2222
            if(emu){
2223
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2224
                ptr= s->edge_emu_buffer;
2225
            }
2226
            get_pixels(s->block[4], ptr, wrap_c);
2227

    
2228
            ptr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2229
            if(emu){
2230
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2231
                ptr= s->edge_emu_buffer;
2232
            }
2233
            get_pixels(s->block[5], ptr, wrap_c);
2234
        }
2235
    }else{
2236
        op_pixels_func (*op_pix)[4];
2237
        qpel_mc_func (*op_qpix)[16];
2238
        UINT8 *dest_y, *dest_cb, *dest_cr;
2239
        UINT8 *ptr_y, *ptr_cb, *ptr_cr;
2240
        int wrap_y, wrap_c;
2241
        int emu=0;
2242

    
2243
        dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
2244
        dest_cb = s->current_picture[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2245
        dest_cr = s->current_picture[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2246
        wrap_y = s->linesize;
2247
        wrap_c = s->uvlinesize;
2248
        ptr_y  = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2249
        ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2250
        ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2251

    
2252
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
2253
            op_pix = put_pixels_tab;
2254
            op_qpix= put_qpel_pixels_tab;
2255
        }else{
2256
            op_pix = put_no_rnd_pixels_tab;
2257
            op_qpix= put_no_rnd_qpel_pixels_tab;
2258
        }
2259

    
2260
        if (s->mv_dir & MV_DIR_FORWARD) {
2261
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
2262
            op_pix = avg_pixels_tab;
2263
            op_qpix= avg_qpel_pixels_tab;
2264
        }
2265
        if (s->mv_dir & MV_DIR_BACKWARD) {
2266
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
2267
        }
2268

    
2269
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2270
            emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2271
            ptr_y= s->edge_emu_buffer;
2272
            emu=1;
2273
        }
2274
        
2275
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2276
            int progressive_score, interlaced_score;
2277
            
2278
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
2279
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
2280
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
2281
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
2282
            
2283
            if(progressive_score > interlaced_score + 600){
2284
                s->interlaced_dct=1;
2285
            
2286
                dct_offset= wrap_y;
2287
                wrap_y<<=1;
2288
            }else
2289
                s->interlaced_dct=0;
2290
        }
2291
        
2292
        diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
2293
        diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
2294
        diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
2295
        diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2296
        
2297
        if(s->flags&CODEC_FLAG_GRAY){
2298
            skip_dct[4]= 1;
2299
            skip_dct[5]= 1;
2300
        }else{
2301
            if(emu){
2302
                emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2303
                ptr_cb= s->edge_emu_buffer;
2304
            }
2305
            diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2306
            if(emu){
2307
                emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2308
                ptr_cr= s->edge_emu_buffer;
2309
            }
2310
            diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2311
        }
2312

    
2313
        /* pre quantization */         
2314
        if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2315
            //FIXME optimize
2316
            if(pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2317
            if(pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2318
            if(pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2319
            if(pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2320
            if(pix_abs8x8(ptr_cb              , dest_cb              , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
2321
            if(pix_abs8x8(ptr_cr              , dest_cr              , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
2322
#if 0
2323
{
2324
 static int stat[7];
2325
 int num=0;
2326
 for(i=0; i<6; i++)
2327
  if(skip_dct[i]) num++;
2328
 stat[num]++;
2329
 
2330
 if(s->mb_x==0 && s->mb_y==0){
2331
  for(i=0; i<7; i++){
2332
   printf("%6d %1d\n", stat[i], i);
2333
  }
2334
 }
2335
}
2336
#endif
2337
        }
2338

    
2339
    }
2340
            
2341
#if 0
2342
            {
2343
                float adap_parm;
2344
                
2345
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2346
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2347
            
2348
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
2349
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
2350
                        s->qscale, adap_parm, s->qscale*adap_parm,
2351
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2352
            }
2353
#endif
2354
    /* DCT & quantize */
2355
    if(s->out_format==FMT_MJPEG){
2356
        for(i=0;i<6;i++) {
2357
            int overflow;
2358
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2359
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2360
        }
2361
    }else{
2362
        for(i=0;i<6;i++) {
2363
            if(!skip_dct[i]){
2364
                int overflow;
2365
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2366
            // FIXME we could decide to change to quantizer instead of clipping
2367
            // JS: I don't think that would be a good idea it could lower quality instead
2368
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
2369
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2370
            }else
2371
                s->block_last_index[i]= -1;
2372
        }
2373
        if(s->luma_elim_threshold && !s->mb_intra)
2374
            for(i=0; i<4; i++)
2375
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2376
        if(s->chroma_elim_threshold && !s->mb_intra)
2377
            for(i=4; i<6; i++)
2378
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2379
    }
2380

    
2381
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2382
        s->block_last_index[4]=
2383
        s->block_last_index[5]= 0;
2384
        s->block[4][0]=
2385
        s->block[5][0]= 128;
2386
    }
2387

    
2388
    /* huffman encode */
2389
    switch(s->out_format) {
2390
    case FMT_MPEG1:
2391
        mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2392
        break;
2393
    case FMT_H263:
2394
        if (s->h263_msmpeg4)
2395
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2396
        else if(s->h263_pred)
2397
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2398
        else
2399
            h263_encode_mb(s, s->block, motion_x, motion_y);
2400
        break;
2401
    case FMT_MJPEG:
2402
        mjpeg_encode_mb(s, s->block);
2403
        break;
2404
    }
2405
}
2406

    
2407
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
2408
{
2409
    int bytes= length>>4;
2410
    int bits= length&15;
2411
    int i;
2412

    
2413
    if(length==0) return;
2414

    
2415
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2416
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2417
}
2418

    
2419
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2420
    int i;
2421

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

    
2424
    /* mpeg1 */
2425
    d->mb_incr= s->mb_incr;
2426
    for(i=0; i<3; i++)
2427
        d->last_dc[i]= s->last_dc[i];
2428
    
2429
    /* statistics */
2430
    d->mv_bits= s->mv_bits;
2431
    d->i_tex_bits= s->i_tex_bits;
2432
    d->p_tex_bits= s->p_tex_bits;
2433
    d->i_count= s->i_count;
2434
    d->f_count= s->f_count;
2435
    d->b_count= s->b_count;
2436
    d->skip_count= s->skip_count;
2437
    d->misc_bits= s->misc_bits;
2438
    d->last_bits= 0;
2439

    
2440
    d->mb_skiped= s->mb_skiped;
2441
}
2442

    
2443
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2444
    int i;
2445

    
2446
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
2447
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2448
    
2449
    /* mpeg1 */
2450
    d->mb_incr= s->mb_incr;
2451
    for(i=0; i<3; i++)
2452
        d->last_dc[i]= s->last_dc[i];
2453
    
2454
    /* statistics */
2455
    d->mv_bits= s->mv_bits;
2456
    d->i_tex_bits= s->i_tex_bits;
2457
    d->p_tex_bits= s->p_tex_bits;
2458
    d->i_count= s->i_count;
2459
    d->f_count= s->f_count;
2460
    d->b_count= s->b_count;
2461
    d->skip_count= s->skip_count;
2462
    d->misc_bits= s->misc_bits;
2463

    
2464
    d->mb_intra= s->mb_intra;
2465
    d->mb_skiped= s->mb_skiped;
2466
    d->mv_type= s->mv_type;
2467
    d->mv_dir= s->mv_dir;
2468
    d->pb= s->pb;
2469
    if(s->data_partitioning){
2470
        d->pb2= s->pb2;
2471
        d->tex_pb= s->tex_pb;
2472
    }
2473
    d->block= s->block;
2474
    for(i=0; i<6; i++)
2475
        d->block_last_index[i]= s->block_last_index[i];
2476
    d->interlaced_dct= s->interlaced_dct;
2477
}
2478

    
2479
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
2480
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2481
                           int *dmin, int *next_block, int motion_x, int motion_y)
2482
{
2483
    int bits_count;
2484
    
2485
    copy_context_before_encode(s, backup, type);
2486

    
2487
    s->block= s->blocks[*next_block];
2488
    s->pb= pb[*next_block];
2489
    if(s->data_partitioning){
2490
        s->pb2   = pb2   [*next_block];
2491
        s->tex_pb= tex_pb[*next_block];
2492
    }
2493

    
2494
    encode_mb(s, motion_x, motion_y);
2495

    
2496
    bits_count= get_bit_count(&s->pb);
2497
    if(s->data_partitioning){
2498
        bits_count+= get_bit_count(&s->pb2);
2499
        bits_count+= get_bit_count(&s->tex_pb);
2500
    }
2501

    
2502
    if(bits_count<*dmin){
2503
        *dmin= bits_count;
2504
        *next_block^=1;
2505

    
2506
        copy_context_after_encode(best, s, type);
2507
    }
2508
}
2509

    
2510
static void encode_picture(MpegEncContext *s, int picture_number)
2511
{
2512
    int mb_x, mb_y, pdif = 0;
2513
    int i;
2514
    int bits;
2515
    MpegEncContext best_s, backup_s;
2516
    UINT8 bit_buf[2][3000];
2517
    UINT8 bit_buf2[2][3000];
2518
    UINT8 bit_buf_tex[2][3000];
2519
    PutBitContext pb[2], pb2[2], tex_pb[2];
2520

    
2521
    for(i=0; i<2; i++){
2522
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
2523
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
2524
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2525
    }
2526

    
2527
    s->picture_number = picture_number;
2528

    
2529
    s->block_wrap[0]=
2530
    s->block_wrap[1]=
2531
    s->block_wrap[2]=
2532
    s->block_wrap[3]= s->mb_width*2 + 2;
2533
    s->block_wrap[4]=
2534
    s->block_wrap[5]= s->mb_width + 2;
2535
    
2536
    /* Reset the average MB variance */
2537
    s->mb_var_sum = 0;
2538
    s->mc_mb_var_sum = 0;
2539

    
2540
    /* we need to initialize some time vars before we can encode b-frames */
2541
    if (s->h263_pred && !s->h263_msmpeg4)
2542
        ff_set_mpeg4_time(s, s->picture_number); 
2543

    
2544
    s->scene_change_score=0;
2545
    
2546
    s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2547

    
2548
    /* Estimate motion for every MB */
2549
    if(s->pict_type != I_TYPE){
2550
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2551
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2552
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2553
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2554
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2555
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2556
                s->mb_x = mb_x;
2557
                s->mb_y = mb_y;
2558
                s->block_index[0]+=2;
2559
                s->block_index[1]+=2;
2560
                s->block_index[2]+=2;
2561
                s->block_index[3]+=2;
2562

    
2563
                /* compute motion vector & mb_type and store in context */
2564
                if(s->pict_type==B_TYPE)
2565
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
2566
                else
2567
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
2568
//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
2569
            }
2570
        }
2571
        emms_c();
2572
    }else /* if(s->pict_type == I_TYPE) */{
2573
        /* I-Frame */
2574
        //FIXME do we need to zero them?
2575
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2576
        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
2577
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2578
        
2579
        if(!s->fixed_qscale){
2580
            /* finding spatial complexity for I-frame rate control */
2581
            for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2582
                for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2583
                    int xx = mb_x * 16;
2584
                    int yy = mb_y * 16;
2585
                    uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
2586
                    int varc;
2587
                    int sum = pix_sum(pix, s->linesize);
2588
    
2589
                    sum= (sum+8)>>4;
2590
                    varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
2591

    
2592
                    s->mb_var [s->mb_width * mb_y + mb_x] = varc;
2593
                    s->mb_mean[s->mb_width * mb_y + mb_x] = (sum+7)>>4;
2594
                    s->mb_var_sum    += varc;
2595
                }
2596
            }
2597
        }
2598
    }
2599
    if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2600
        s->pict_type= I_TYPE;
2601
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2602
        if(s->max_b_frames==0){
2603
            s->input_pict_type= I_TYPE;
2604
            s->input_picture_in_gop_number=0;
2605
        }
2606
//printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
2607
    }
2608
    
2609
    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 
2610
        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2611
        ff_fix_long_p_mvs(s);
2612
    if(s->pict_type==B_TYPE){
2613
        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2614
        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2615

    
2616
        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2617
        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2618
        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2619
        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2620
    }
2621
    
2622
    if (s->fixed_qscale) 
2623
        s->frame_qscale = s->avctx->quality;
2624
    else
2625
        s->frame_qscale = ff_rate_estimate_qscale(s);
2626

    
2627
    if(s->adaptive_quant){
2628
        switch(s->codec_id){
2629
        case CODEC_ID_MPEG4:
2630
            ff_clean_mpeg4_qscales(s);
2631
            break;
2632
        case CODEC_ID_H263:
2633
        case CODEC_ID_H263P:
2634
            ff_clean_h263_qscales(s);
2635
            break;
2636
        }
2637

    
2638
        s->qscale= s->qscale_table[0];
2639
    }else
2640
        s->qscale= (int)(s->frame_qscale + 0.5);
2641
        
2642
    if (s->out_format == FMT_MJPEG) {
2643
        /* for mjpeg, we do include qscale in the matrix */
2644
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2645
        for(i=1;i<64;i++){
2646
            int j= s->idct_permutation[i];
2647

    
2648
            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2649
        }
2650
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, 
2651
                       s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
2652
    }
2653

    
2654
    s->last_bits= get_bit_count(&s->pb);
2655
    switch(s->out_format) {
2656
    case FMT_MJPEG:
2657
        mjpeg_picture_header(s);
2658
        break;
2659
    case FMT_H263:
2660
        if (s->h263_msmpeg4) 
2661
            msmpeg4_encode_picture_header(s, picture_number);
2662
        else if (s->h263_pred)
2663
            mpeg4_encode_picture_header(s, picture_number);
2664
        else if (s->h263_rv10) 
2665
            rv10_encode_picture_header(s, picture_number);
2666
        else
2667
            h263_encode_picture_header(s, picture_number);
2668
        break;
2669
    case FMT_MPEG1:
2670
        mpeg1_encode_picture_header(s, picture_number);
2671
        break;
2672
    }
2673
    bits= get_bit_count(&s->pb);
2674
    s->header_bits= bits - s->last_bits;
2675
    s->last_bits= bits;
2676
    s->mv_bits=0;
2677
    s->misc_bits=0;
2678
    s->i_tex_bits=0;
2679
    s->p_tex_bits=0;
2680
    s->i_count=0;
2681
    s->f_count=0;
2682
    s->b_count=0;
2683
    s->skip_count=0;
2684

    
2685
    /* init last dc values */
2686
    /* note: quant matrix value (8) is implied here */
2687
    s->last_dc[0] = 128;
2688
    s->last_dc[1] = 128;
2689
    s->last_dc[2] = 128;
2690
    s->mb_incr = 1;
2691
    s->last_mv[0][0][0] = 0;
2692
    s->last_mv[0][0][1] = 0;
2693

    
2694
    if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
2695
        s->gob_index = ff_h263_get_gob_height(s);
2696

    
2697
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2698
        ff_mpeg4_init_partitions(s);
2699

    
2700
    s->resync_mb_x=0;
2701
    s->resync_mb_y=0;
2702
    s->first_slice_line = 1;
2703
    s->ptr_lastgob = s->pb.buf;
2704
    s->ptr_last_mb_line = s->pb.buf;
2705
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2706
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2707
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2708
        
2709
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2710
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2711
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2712
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2713
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
2714
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
2715
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2716
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
2717
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
2718
//            int d;
2719
            int dmin=10000000;
2720

    
2721
            s->mb_x = mb_x;
2722
            s->mb_y = mb_y;
2723
            s->block_index[0]+=2;
2724
            s->block_index[1]+=2;
2725
            s->block_index[2]+=2;
2726
            s->block_index[3]+=2;
2727
            s->block_index[4]++;
2728
            s->block_index[5]++;
2729

    
2730
            /* write gob / video packet header  */
2731
            if(s->rtp_mode){
2732
                int current_packet_size, is_gob_start;
2733
                
2734
                current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
2735
                is_gob_start=0;
2736
                
2737
                if(s->codec_id==CODEC_ID_MPEG4){
2738
                    if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
2739
                       && s->mb_y + s->mb_x>0){
2740

    
2741
                        if(s->partitioned_frame){
2742
                            ff_mpeg4_merge_partitions(s);
2743
                            ff_mpeg4_init_partitions(s);
2744
                        }
2745
                        ff_mpeg4_encode_video_packet_header(s);
2746

    
2747
                        if(s->flags&CODEC_FLAG_PASS1){
2748
                            int bits= get_bit_count(&s->pb);
2749
                            s->misc_bits+= bits - s->last_bits;
2750
                            s->last_bits= bits;
2751
                        }
2752
                        ff_mpeg4_clean_buffers(s);
2753
                        is_gob_start=1;
2754
                    }
2755
                }else{
2756
                    if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
2757
                       && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
2758
                       
2759
                        h263_encode_gob_header(s, mb_y);                       
2760
                        is_gob_start=1;
2761
                    }
2762
                }
2763

    
2764
                if(is_gob_start){
2765
                    s->ptr_lastgob = pbBufPtr(&s->pb);
2766
                    s->first_slice_line=1;
2767
                    s->resync_mb_x=mb_x;
2768
                    s->resync_mb_y=mb_y;
2769
                }
2770
            }
2771

    
2772
            if(  (s->resync_mb_x   == s->mb_x)
2773
               && s->resync_mb_y+1 == s->mb_y){
2774
                s->first_slice_line=0; 
2775
            }
2776

    
2777
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
2778
                int next_block=0;
2779
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2780

    
2781
                copy_context_before_encode(&backup_s, s, -1);
2782
                backup_s.pb= s->pb;
2783
                best_s.data_partitioning= s->data_partitioning;
2784
                best_s.partitioned_frame= s->partitioned_frame;
2785
                if(s->data_partitioning){
2786
                    backup_s.pb2= s->pb2;
2787
                    backup_s.tex_pb= s->tex_pb;
2788
                }
2789

    
2790
                if(mb_type&MB_TYPE_INTER){
2791
                    s->mv_dir = MV_DIR_FORWARD;
2792
                    s->mv_type = MV_TYPE_16X16;
2793
                    s->mb_intra= 0;
2794
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
2795
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
2796
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
2797
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2798
                }
2799
                if(mb_type&MB_TYPE_INTER4V){                 
2800
                    s->mv_dir = MV_DIR_FORWARD;
2801
                    s->mv_type = MV_TYPE_8X8;
2802
                    s->mb_intra= 0;
2803
                    for(i=0; i<4; i++){
2804
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
2805
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
2806
                    }
2807
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
2808
                                 &dmin, &next_block, 0, 0);
2809
                }
2810
                if(mb_type&MB_TYPE_FORWARD){
2811
                    s->mv_dir = MV_DIR_FORWARD;
2812
                    s->mv_type = MV_TYPE_16X16;
2813
                    s->mb_intra= 0;
2814
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2815
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2816
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
2817
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2818
                }
2819
                if(mb_type&MB_TYPE_BACKWARD){
2820
                    s->mv_dir = MV_DIR_BACKWARD;
2821
                    s->mv_type = MV_TYPE_16X16;
2822
                    s->mb_intra= 0;
2823
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2824
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2825
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
2826
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2827
                }
2828
                if(mb_type&MB_TYPE_BIDIR){
2829
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2830
                    s->mv_type = MV_TYPE_16X16;
2831
                    s->mb_intra= 0;
2832
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2833
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2834
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2835
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2836
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
2837
                                 &dmin, &next_block, 0, 0);
2838
                }
2839
                if(mb_type&MB_TYPE_DIRECT){
2840
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2841
                    s->mv_type = MV_TYPE_16X16; //FIXME
2842
                    s->mb_intra= 0;
2843
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
2844
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
2845
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
2846
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
2847
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
2848
                                 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
2849
                }
2850
                if(mb_type&MB_TYPE_INTRA){
2851
                    s->mv_dir = MV_DIR_FORWARD;
2852
                    s->mv_type = MV_TYPE_16X16;
2853
                    s->mb_intra= 1;
2854
                    s->mv[0][0][0] = 0;
2855
                    s->mv[0][0][1] = 0;
2856
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
2857
                                 &dmin, &next_block, 0, 0);
2858
                    /* force cleaning of ac/dc pred stuff if needed ... */
2859
                    if(s->h263_pred || s->h263_aic)
2860
                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
2861
                }
2862
                copy_context_after_encode(s, &best_s, -1);
2863
                
2864
                pb_bits_count= get_bit_count(&s->pb);
2865
                flush_put_bits(&s->pb);
2866
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2867
                s->pb= backup_s.pb;
2868
                
2869
                if(s->data_partitioning){
2870
                    pb2_bits_count= get_bit_count(&s->pb2);
2871
                    flush_put_bits(&s->pb2);
2872
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2873
                    s->pb2= backup_s.pb2;
2874
                    
2875
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
2876
                    flush_put_bits(&s->tex_pb);
2877
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2878
                    s->tex_pb= backup_s.tex_pb;
2879
                }
2880
                s->last_bits= get_bit_count(&s->pb);
2881
            } else {
2882
                int motion_x, motion_y;
2883
                s->mv_type=MV_TYPE_16X16;
2884
                // only one MB-Type possible
2885
                switch(mb_type){
2886
                case MB_TYPE_INTRA:
2887
                    s->mv_dir = MV_DIR_FORWARD;
2888
                    s->mb_intra= 1;
2889
                    motion_x= s->mv[0][0][0] = 0;
2890
                    motion_y= s->mv[0][0][1] = 0;
2891
                    break;
2892
                case MB_TYPE_INTER:
2893
                    s->mv_dir = MV_DIR_FORWARD;
2894
                    s->mb_intra= 0;
2895
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2896
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2897
                    break;
2898
                case MB_TYPE_INTER4V:
2899
                    s->mv_dir = MV_DIR_FORWARD;
2900
                    s->mv_type = MV_TYPE_8X8;
2901
                    s->mb_intra= 0;
2902
                    for(i=0; i<4; i++){
2903
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
2904
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
2905
                    }
2906
                    motion_x= motion_y= 0;
2907
                    break;
2908
                case MB_TYPE_DIRECT:
2909
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2910
                    s->mb_intra= 0;
2911
                    motion_x=s->b_direct_mv_table[xy][0];
2912
                    motion_y=s->b_direct_mv_table[xy][1];
2913
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
2914
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
2915
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
2916
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
2917
                    break;
2918
                case MB_TYPE_BIDIR:
2919
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2920
                    s->mb_intra= 0;
2921
                    motion_x=0;
2922
                    motion_y=0;
2923
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2924
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2925
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2926
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2927
                    break;
2928
                case MB_TYPE_BACKWARD:
2929
                    s->mv_dir = MV_DIR_BACKWARD;
2930
                    s->mb_intra= 0;
2931
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2932
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2933
                    break;
2934
                case MB_TYPE_FORWARD:
2935
                    s->mv_dir = MV_DIR_FORWARD;
2936
                    s->mb_intra= 0;
2937
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2938
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2939
//                    printf(" %d %d ", motion_x, motion_y);
2940
                    break;
2941
                default:
2942
                    motion_x=motion_y=0; //gcc warning fix
2943
                    printf("illegal MB type\n");
2944
                }
2945
                encode_mb(s, motion_x, motion_y);
2946
            }
2947
            /* clean the MV table in IPS frames for direct mode in B frames */
2948
            if(s->mb_intra /* && I,P,S_TYPE */){
2949
                s->p_mv_table[xy][0]=0;
2950
                s->p_mv_table[xy][1]=0;
2951
            }
2952

    
2953
            MPV_decode_mb(s, s->block);
2954
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
2955
        }
2956

    
2957

    
2958
        /* Obtain average mb_row size for RTP */
2959
        if (s->rtp_mode) {
2960
            if (mb_y==0)
2961
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
2962
            else {    
2963
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
2964
            }
2965
            s->ptr_last_mb_line = pbBufPtr(&s->pb);
2966
        }
2967
    }
2968
    emms_c();
2969

    
2970
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2971
        ff_mpeg4_merge_partitions(s);
2972

    
2973
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
2974
        msmpeg4_encode_ext_header(s);
2975

    
2976
    if(s->codec_id==CODEC_ID_MPEG4) 
2977
        ff_mpeg4_stuffing(&s->pb);
2978

    
2979
    //if (s->gob_number)
2980
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
2981
    
2982
    /* Send the last GOB if RTP */    
2983
    if (s->rtp_mode) {
2984
        flush_put_bits(&s->pb);
2985
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
2986
        /* Call the RTP callback to send the last GOB */
2987
        if (s->rtp_callback)
2988
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
2989
        s->ptr_lastgob = pbBufPtr(&s->pb);
2990
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
2991
    }
2992
}
2993

    
2994
static int dct_quantize_c(MpegEncContext *s, 
2995
                        DCTELEM *block, int n,
2996
                        int qscale, int *overflow)
2997
{
2998
    int i, j, level, last_non_zero, q;
2999
    const int *qmat;
3000
    const UINT8 *scantable= s->intra_scantable.scantable;
3001
    int bias;
3002
    int max=0;
3003
    unsigned int threshold1, threshold2;
3004
    
3005
    s->fdct (block);
3006

    
3007
    if (s->mb_intra) {
3008
        if (!s->h263_aic) {
3009
            if (n < 4)
3010
                q = s->y_dc_scale;
3011
            else
3012
                q = s->c_dc_scale;
3013
            q = q << 3;
3014
        } else
3015
            /* For AIC we skip quant/dequant of INTRADC */
3016
            q = 1 << 3;
3017
            
3018
        /* note: block[0] is assumed to be positive */
3019
        block[0] = (block[0] + (q >> 1)) / q;
3020
        i = 1;
3021
        last_non_zero = 0;
3022
        qmat = s->q_intra_matrix[qscale];
3023
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3024
    } else {
3025
        i = 0;
3026
        last_non_zero = -1;
3027
        qmat = s->q_inter_matrix[qscale];
3028
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3029
    }
3030
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3031
    threshold2= (threshold1<<1);
3032

    
3033
    for(;i<64;i++) {
3034
        j = scantable[i];
3035
        level = block[j];
3036
        level = level * qmat[j];
3037

    
3038
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3039
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3040
        if(((unsigned)(level+threshold1))>threshold2){
3041
            if(level>0){
3042
                level= (bias + level)>>QMAT_SHIFT;
3043
                block[j]= level;
3044
            }else{
3045
                level= (bias - level)>>QMAT_SHIFT;
3046
                block[j]= -level;
3047
            }
3048
            max |=level;
3049
            last_non_zero = i;
3050
        }else{
3051
            block[j]=0;
3052
        }
3053
    }
3054
    *overflow= s->max_qcoeff < max; //overflow might have happend
3055
    
3056
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3057
    ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
3058

    
3059
    return last_non_zero;
3060
}
3061

    
3062
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
3063
                                   DCTELEM *block, int n, int qscale)
3064
{
3065
    int i, level, nCoeffs;
3066
    const UINT16 *quant_matrix;
3067

    
3068
    nCoeffs= s->block_last_index[n];
3069
    
3070
    if (s->mb_intra) {
3071
        if (n < 4) 
3072
            block[0] = block[0] * s->y_dc_scale;
3073
        else
3074
            block[0] = block[0] * s->c_dc_scale;
3075
        /* XXX: only mpeg1 */
3076
        quant_matrix = s->intra_matrix;
3077
        for(i=1;i<=nCoeffs;i++) {
3078
            int j= s->intra_scantable.permutated[i];
3079
            level = block[j];
3080
            if (level) {
3081
                if (level < 0) {
3082
                    level = -level;
3083
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3084
                    level = (level - 1) | 1;
3085
                    level = -level;
3086
                } else {
3087
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3088
                    level = (level - 1) | 1;
3089
                }
3090
#ifdef PARANOID
3091
                if (level < -2048 || level > 2047)
3092
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3093
#endif
3094
                block[j] = level;
3095
            }
3096
        }
3097
    } else {
3098
        i = 0;
3099
        quant_matrix = s->inter_matrix;
3100
        for(;i<=nCoeffs;i++) {
3101
            int j= s->intra_scantable.permutated[i];
3102
            level = block[j];
3103
            if (level) {
3104
                if (level < 0) {
3105
                    level = -level;
3106
                    level = (((level << 1) + 1) * qscale *
3107
                             ((int) (quant_matrix[j]))) >> 4;
3108
                    level = (level - 1) | 1;
3109
                    level = -level;
3110
                } else {
3111
                    level = (((level << 1) + 1) * qscale *
3112
                             ((int) (quant_matrix[j]))) >> 4;
3113
                    level = (level - 1) | 1;
3114
                }
3115
#ifdef PARANOID
3116
                if (level < -2048 || level > 2047)
3117
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3118
#endif
3119
                block[j] = level;
3120
            }
3121
        }
3122
    }
3123
}
3124

    
3125
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
3126
                                   DCTELEM *block, int n, int qscale)
3127
{
3128
    int i, level, nCoeffs;
3129
    const UINT16 *quant_matrix;
3130

    
3131
    if(s->alternate_scan) nCoeffs= 63;
3132
    else nCoeffs= s->block_last_index[n];
3133
    
3134
    if (s->mb_intra) {
3135
        if (n < 4) 
3136
            block[0] = block[0] * s->y_dc_scale;
3137
        else
3138
            block[0] = block[0] * s->c_dc_scale;
3139
        quant_matrix = s->intra_matrix;
3140
        for(i=1;i<=nCoeffs;i++) {
3141
            int j= s->intra_scantable.permutated[i];
3142
            level = block[j];
3143
            if (level) {
3144
                if (level < 0) {
3145
                    level = -level;
3146
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3147
                    level = -level;
3148
                } else {
3149
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3150
                }
3151
#ifdef PARANOID
3152
                if (level < -2048 || level > 2047)
3153
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3154
#endif
3155
                block[j] = level;
3156
            }
3157
        }
3158
    } else {
3159
        int sum=-1;
3160
        i = 0;
3161
        quant_matrix = s->inter_matrix;
3162
        for(;i<=nCoeffs;i++) {
3163
            int j= s->intra_scantable.permutated[i];
3164
            level = block[j];
3165
            if (level) {
3166
                if (level < 0) {
3167
                    level = -level;
3168
                    level = (((level << 1) + 1) * qscale *
3169
                             ((int) (quant_matrix[j]))) >> 4;
3170
                    level = -level;
3171
                } else {
3172
                    level = (((level << 1) + 1) * qscale *
3173
                             ((int) (quant_matrix[j]))) >> 4;
3174
                }
3175
#ifdef PARANOID
3176
                if (level < -2048 || level > 2047)
3177
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3178
#endif
3179
                block[j] = level;
3180
                sum+=level;
3181
            }
3182
        }
3183
        block[63]^=sum&1;
3184
    }
3185
}
3186

    
3187

    
3188
static void dct_unquantize_h263_c(MpegEncContext *s, 
3189
                                  DCTELEM *block, int n, int qscale)
3190
{
3191
    int i, level, qmul, qadd;
3192
    int nCoeffs;
3193
    
3194
    assert(s->block_last_index[n]>=0);
3195
    
3196
    qadd = (qscale - 1) | 1;
3197
    qmul = qscale << 1;
3198
    
3199
    if (s->mb_intra) {
3200
        if (!s->h263_aic) {
3201
            if (n < 4) 
3202
                block[0] = block[0] * s->y_dc_scale;
3203
            else
3204
                block[0] = block[0] * s->c_dc_scale;
3205
        }else
3206
            qadd = 0;
3207
        i = 1;
3208
        nCoeffs= 63; //does not allways use zigzag table 
3209
    } else {
3210
        i = 0;
3211
        nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
3212
    }
3213

    
3214
    for(;i<=nCoeffs;i++) {
3215
        level = block[i];
3216
        if (level) {
3217
            if (level < 0) {
3218
                level = level * qmul - qadd;
3219
            } else {
3220
                level = level * qmul + qadd;
3221
            }
3222
#ifdef PARANOID
3223
                if (level < -2048 || level > 2047)
3224
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3225
#endif
3226
            block[i] = level;
3227
        }
3228
    }
3229
}
3230

    
3231
AVCodec mpeg1video_encoder = {
3232
    "mpeg1video",
3233
    CODEC_TYPE_VIDEO,
3234
    CODEC_ID_MPEG1VIDEO,
3235
    sizeof(MpegEncContext),
3236
    MPV_encode_init,
3237
    MPV_encode_picture,
3238
    MPV_encode_end,
3239
};
3240

    
3241
AVCodec h263_encoder = {
3242
    "h263",
3243
    CODEC_TYPE_VIDEO,
3244
    CODEC_ID_H263,
3245
    sizeof(MpegEncContext),
3246
    MPV_encode_init,
3247
    MPV_encode_picture,
3248
    MPV_encode_end,
3249
};
3250

    
3251
AVCodec h263p_encoder = {
3252
    "h263p",
3253
    CODEC_TYPE_VIDEO,
3254
    CODEC_ID_H263P,
3255
    sizeof(MpegEncContext),
3256
    MPV_encode_init,
3257
    MPV_encode_picture,
3258
    MPV_encode_end,
3259
};
3260

    
3261
AVCodec rv10_encoder = {
3262
    "rv10",
3263
    CODEC_TYPE_VIDEO,
3264
    CODEC_ID_RV10,
3265
    sizeof(MpegEncContext),
3266
    MPV_encode_init,
3267
    MPV_encode_picture,
3268
    MPV_encode_end,
3269
};
3270

    
3271
AVCodec mjpeg_encoder = {
3272
    "mjpeg",
3273
    CODEC_TYPE_VIDEO,
3274
    CODEC_ID_MJPEG,
3275
    sizeof(MpegEncContext),
3276
    MPV_encode_init,
3277
    MPV_encode_picture,
3278
    MPV_encode_end,
3279
};
3280

    
3281
AVCodec mpeg4_encoder = {
3282
    "mpeg4",
3283
    CODEC_TYPE_VIDEO,
3284
    CODEC_ID_MPEG4,
3285
    sizeof(MpegEncContext),
3286
    MPV_encode_init,
3287
    MPV_encode_picture,
3288
    MPV_encode_end,
3289
};
3290

    
3291
AVCodec msmpeg4v1_encoder = {
3292
    "msmpeg4v1",
3293
    CODEC_TYPE_VIDEO,
3294
    CODEC_ID_MSMPEG4V1,
3295
    sizeof(MpegEncContext),
3296
    MPV_encode_init,
3297
    MPV_encode_picture,
3298
    MPV_encode_end,
3299
};
3300

    
3301
AVCodec msmpeg4v2_encoder = {
3302
    "msmpeg4v2",
3303
    CODEC_TYPE_VIDEO,
3304
    CODEC_ID_MSMPEG4V2,
3305
    sizeof(MpegEncContext),
3306
    MPV_encode_init,
3307
    MPV_encode_picture,
3308
    MPV_encode_end,
3309
};
3310

    
3311
AVCodec msmpeg4v3_encoder = {
3312
    "msmpeg4",
3313
    CODEC_TYPE_VIDEO,
3314
    CODEC_ID_MSMPEG4V3,
3315
    sizeof(MpegEncContext),
3316
    MPV_encode_init,
3317
    MPV_encode_picture,
3318
    MPV_encode_end,
3319
};
3320

    
3321
AVCodec wmv1_encoder = {
3322
    "wmv1",
3323
    CODEC_TYPE_VIDEO,
3324
    CODEC_ID_WMV1,
3325
    sizeof(MpegEncContext),
3326
    MPV_encode_init,
3327
    MPV_encode_picture,
3328
    MPV_encode_end,
3329
};
3330

    
3331
AVCodec wmv2_encoder = {
3332
    "wmv2",
3333
    CODEC_TYPE_VIDEO,
3334
    CODEC_ID_WMV2,
3335
    sizeof(MpegEncContext),
3336
    MPV_encode_init,
3337
    MPV_encode_picture,
3338
    MPV_encode_end,
3339
};