Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 18f77016

History | View | Annotate | Download (115 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 uint16_t 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_t 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 const uint8_t 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_MV*2+1]=NULL;
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
#ifdef ARCH_POWERPC
161
        st->inverse[j] = i;
162
#endif
163
    }
164
    
165
    end=-1;
166
    for(i=0; i<64; i++){
167
        int j;
168
        j = st->permutated[i];
169
        if(j>end) end=j;
170
        st->raster_end[i]= end;
171
    }
172
}
173

    
174
/* XXX: those functions should be suppressed ASAP when all IDCTs are
175
 converted */
176
// *FIXME* this is ugly hack using local static
177
static void (*ff_put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
178
static void (*ff_add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
179
static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
180
{
181
    j_rev_dct (block);
182
    ff_put_pixels_clamped(block, dest, line_size);
183
}
184
static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
185
{
186
    j_rev_dct (block);
187
    ff_add_pixels_clamped(block, dest, line_size);
188
}
189

    
190
/* init common dct for both encoder and decoder */
191
int DCT_common_init(MpegEncContext *s)
192
{
193
    int i;
194

    
195
    ff_put_pixels_clamped = s->dsp.put_pixels_clamped;
196
    ff_add_pixels_clamped = s->dsp.add_pixels_clamped;
197

    
198
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
199
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
200
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
201
    s->dct_quantize= dct_quantize_c;
202

    
203
    if(s->avctx->dct_algo==FF_DCT_FASTINT)
204
        s->fdct = fdct_ifast;
205
    else
206
        s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default
207

    
208
    if(s->avctx->idct_algo==FF_IDCT_INT){
209
        s->idct_put= ff_jref_idct_put;
210
        s->idct_add= ff_jref_idct_add;
211
        s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
212
    }else{ //accurate/default
213
        s->idct_put= simple_idct_put;
214
        s->idct_add= simple_idct_add;
215
        s->idct_permutation_type= FF_NO_IDCT_PERM;
216
    }
217
        
218
#ifdef HAVE_MMX
219
    MPV_common_init_mmx(s);
220
#endif
221
#ifdef ARCH_ALPHA
222
    MPV_common_init_axp(s);
223
#endif
224
#ifdef HAVE_MLIB
225
    MPV_common_init_mlib(s);
226
#endif
227
#ifdef HAVE_MMI
228
    MPV_common_init_mmi(s);
229
#endif
230
#ifdef ARCH_ARMV4L
231
    MPV_common_init_armv4l();
232
#endif
233
#ifdef ARCH_POWERPC
234
    MPV_common_init_ppc(s);
235
#endif
236

    
237
    switch(s->idct_permutation_type){
238
    case FF_NO_IDCT_PERM:
239
        for(i=0; i<64; i++)
240
            s->idct_permutation[i]= i;
241
        break;
242
    case FF_LIBMPEG2_IDCT_PERM:
243
        for(i=0; i<64; i++)
244
            s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
245
        break;
246
    case FF_SIMPLE_IDCT_PERM:
247
        for(i=0; i<64; i++)
248
            s->idct_permutation[i]= simple_mmx_permutation[i];
249
        break;
250
    case FF_TRANSPOSE_IDCT_PERM:
251
        for(i=0; i<64; i++)
252
            s->idct_permutation[i]= ((i&7)<<3) | (i>>3);
253
        break;
254
    default:
255
        fprintf(stderr, "Internal error, IDCT permutation not set\n");
256
        return -1;
257
    }
258

    
259

    
260
    /* load & permutate scantables
261
       note: only wmv uses differnt ones 
262
    */
263
    ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
264
    ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
265
    ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
266
    ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
267

    
268
    return 0;
269
}
270

    
271
/* init common structure for both encoder and decoder */
272
int MPV_common_init(MpegEncContext *s)
273
{
274
    UINT8 *pict;
275
    int y_size, c_size, yc_size, i;
276

    
277
    dsputil_init(&s->dsp, s->avctx->dsp_mask);
278
    DCT_common_init(s);
279

    
280
    s->flags= s->avctx->flags;
281

    
282
    s->mb_width = (s->width + 15) / 16;
283
    s->mb_height = (s->height + 15) / 16;
284

    
285
    /* set default edge pos, will be overriden in decode_header if needed */
286
    s->h_edge_pos= s->mb_width*16;
287
    s->v_edge_pos= s->mb_height*16;
288

    
289
    s->mb_num = s->mb_width * s->mb_height;
290

    
291
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
292
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
293
    yc_size = y_size + 2 * c_size;
294

    
295
    /* convert fourcc to upper case */
296
    s->avctx->fourcc=   toupper( s->avctx->fourcc     &0xFF)          
297
                     + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
298
                     + (toupper((s->avctx->fourcc>>16)&0xFF)<<16) 
299
                     + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
300

    
301
    if(!(s->flags&CODEC_FLAG_DR1)){
302
      s->linesize   = s->mb_width * 16 + 2 * EDGE_WIDTH;
303
      s->uvlinesize = s->mb_width * 8  +     EDGE_WIDTH;
304

    
305
      for(i=0;i<3;i++) {
306
        int w, h, shift, pict_start;
307
        unsigned size;
308

    
309
        w = s->linesize;
310
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
311
        shift = (i == 0) ? 0 : 1;
312
        size = (s->linesize>>shift) * (h >> shift);
313
        pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
314

    
315
        CHECKED_ALLOCZ(pict, size)
316
        s->last_picture_base[i] = pict;
317
        s->last_picture[i] = pict + pict_start;
318
        if(i>0) memset(s->last_picture_base[i], 128, size);
319
    
320
        CHECKED_ALLOCZ(pict, size)
321
        s->next_picture_base[i] = pict;
322
        s->next_picture[i] = pict + pict_start;
323
        if(i>0) memset(s->next_picture_base[i], 128, size);
324
        
325
        if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
326
        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but 
327
           do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
328
            CHECKED_ALLOCZ(pict, size)
329
            s->aux_picture_base[i] = pict;
330
            s->aux_picture[i] = pict + pict_start;
331
            if(i>0) memset(s->aux_picture_base[i], 128, size);
332
        }
333
      }
334
      s->ip_buffer_count= 2;
335
    }
336
    
337
    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
338
    
339
    if (s->encoding) {
340
        int j;
341
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
342
        
343
        CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))
344
        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
345
        CHECKED_ALLOCZ(s->mb_mean  , s->mb_num * sizeof(INT8))
346

    
347
        /* Allocate MV tables */
348
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))
349
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))
350
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))
351
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
352
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
353
        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
354
        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
355
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))
356

    
357
        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))
358
        
359
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
360
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
361

    
362
        if(s->max_b_frames){
363
            for(j=0; j<REORDER_BUFFER_SIZE; j++){
364
                int i;
365
                for(i=0;i<3;i++) {
366
                    int w, h, shift, size;
367

    
368
                    w = s->linesize;
369
                    h = s->mb_height * 16;
370
                    shift = (i == 0) ? 0 : 1;
371
                    size = (w >> shift) * (h >> shift);
372

    
373
                    CHECKED_ALLOCZ(pict, size);
374
                    s->picture_buffer[j][i] = pict;
375
                }
376
            }
377
        }
378

    
379
        if(s->codec_id==CODEC_ID_MPEG4){
380
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
381
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
382
        }
383
        
384
        if(s->msmpeg4_version){
385
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
386
        }
387
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
388
    }
389
        
390
    CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))
391
    
392
    if (s->out_format == FMT_H263 || s->encoding) {
393
        int size;
394
        /* Allocate MB type table */
395
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))
396

    
397
        /* MV prediction */
398
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
399
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
400
    }
401

    
402
    if(s->codec_id==CODEC_ID_MPEG4){
403
        /* interlaced direct mode decoding tables */
404
        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
405
        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
406
    }
407
    /* 4mv b frame decoding table */
408
    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
409
    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
410
    if (s->out_format == FMT_H263) {
411
        /* ac values */
412
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(INT16) * 16);
413
        s->ac_val[1] = s->ac_val[0] + y_size;
414
        s->ac_val[2] = s->ac_val[1] + c_size;
415
        
416
        /* cbp values */
417
        CHECKED_ALLOCZ(s->coded_block, y_size);
418
        
419
        /* divx501 bitstream reorder buffer */
420
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
421

    
422
        /* cbp, ac_pred, pred_dir */
423
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
424
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
425
    }
426
    
427
    if (s->h263_pred || s->h263_plus || !s->encoding) {
428
        /* dc values */
429
        //MN: we need these for error resilience of intra-frames
430
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(INT16));
431
        s->dc_val[1] = s->dc_val[0] + y_size;
432
        s->dc_val[2] = s->dc_val[1] + c_size;
433
        for(i=0;i<yc_size;i++)
434
            s->dc_val[0][i] = 1024;
435
    }
436

    
437
    CHECKED_ALLOCZ(s->next_qscale_table  , s->mb_num * sizeof(UINT8))
438
    CHECKED_ALLOCZ(s->last_qscale_table  , s->mb_num * sizeof(UINT8))
439
    CHECKED_ALLOCZ(s->aux_qscale_table   , s->mb_num * sizeof(UINT8))
440
    s->qscale_table= s->next_qscale_table;
441
    s->avctx->qstride= s->mb_width;
442
    
443
    /* which mb is a intra block */
444
    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
445
    memset(s->mbintra_table, 1, s->mb_num);
446
    
447
    /* default structure is frame */
448
    s->picture_structure = PICT_FRAME;
449
    
450
    /* init macroblock skip table */
451
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
452
    //Note the +1 is for a quicker mpeg4 slice_end detection
453
    
454
    s->block= s->blocks[0];
455

    
456
    s->parse_context.state= -1;
457

    
458
    s->context_initialized = 1;
459
    return 0;
460
 fail:
461
    MPV_common_end(s);
462
    return -1;
463
}
464

    
465

    
466
//extern int sads;
467

    
468
/* init common structure for both encoder and decoder */
469
void MPV_common_end(MpegEncContext *s)
470
{
471
    int i;
472

    
473
    av_freep(&s->mb_type);
474
    av_freep(&s->mb_var);
475
    av_freep(&s->mc_mb_var);
476
    av_freep(&s->mb_mean);
477
    av_freep(&s->p_mv_table);
478
    av_freep(&s->b_forw_mv_table);
479
    av_freep(&s->b_back_mv_table);
480
    av_freep(&s->b_bidir_forw_mv_table);
481
    av_freep(&s->b_bidir_back_mv_table);
482
    av_freep(&s->b_direct_forw_mv_table);
483
    av_freep(&s->b_direct_back_mv_table);
484
    av_freep(&s->b_direct_mv_table);
485
    av_freep(&s->motion_val);
486
    av_freep(&s->dc_val[0]);
487
    av_freep(&s->ac_val[0]);
488
    av_freep(&s->coded_block);
489
    av_freep(&s->mbintra_table);
490
    av_freep(&s->cbp_table);
491
    av_freep(&s->pred_dir_table);
492
    av_freep(&s->next_qscale_table);
493
    av_freep(&s->last_qscale_table);
494
    av_freep(&s->aux_qscale_table);
495
    av_freep(&s->me_scratchpad);
496
    av_freep(&s->me_map);
497
    av_freep(&s->me_score_map);
498
    
499
    av_freep(&s->mbskip_table);
500
    av_freep(&s->bitstream_buffer);
501
    av_freep(&s->tex_pb_buffer);
502
    av_freep(&s->pb2_buffer);
503
    av_freep(&s->edge_emu_buffer);
504
    av_freep(&s->co_located_type_table);
505
    av_freep(&s->field_mv_table);
506
    av_freep(&s->field_select_table);
507
    av_freep(&s->avctx->stats_out);
508
    av_freep(&s->ac_stats);
509
    av_freep(&s->error_status_table);
510
    
511
    for(i=0;i<3;i++) {
512
        int j;
513
        if(!(s->flags&CODEC_FLAG_DR1)){
514
            av_freep(&s->last_picture_base[i]);
515
            av_freep(&s->next_picture_base[i]);
516
            av_freep(&s->aux_picture_base[i]);
517
        }
518
        s->last_picture_base[i]=
519
        s->next_picture_base[i]=
520
        s->aux_picture_base [i] = NULL;
521
        s->last_picture[i]=
522
        s->next_picture[i]=
523
        s->aux_picture [i] = NULL;
524

    
525
        for(j=0; j<REORDER_BUFFER_SIZE; j++){
526
            av_freep(&s->picture_buffer[j][i]);
527
        }
528
    }
529
    s->context_initialized = 0;
530
}
531

    
532
/* init video encoder */
533
int MPV_encode_init(AVCodecContext *avctx)
534
{
535
    MpegEncContext *s = avctx->priv_data;
536
    int i;
537

    
538
    avctx->pix_fmt = PIX_FMT_YUV420P;
539

    
540
    s->bit_rate = avctx->bit_rate;
541
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
542
    s->frame_rate = avctx->frame_rate;
543
    s->width = avctx->width;
544
    s->height = avctx->height;
545
    if(avctx->gop_size > 600){
546
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
547
        avctx->gop_size=600;
548
    }
549
    s->gop_size = avctx->gop_size;
550
    s->rtp_mode = avctx->rtp_mode;
551
    s->rtp_payload_size = avctx->rtp_payload_size;
552
    if (avctx->rtp_callback)
553
        s->rtp_callback = avctx->rtp_callback;
554
    s->qmin= avctx->qmin;
555
    s->qmax= avctx->qmax;
556
    s->max_qdiff= avctx->max_qdiff;
557
    s->qcompress= avctx->qcompress;
558
    s->qblur= avctx->qblur;
559
    s->avctx = avctx;
560
    s->aspect_ratio_info= avctx->aspect_ratio_info;
561
    if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
562
    {
563
        s->aspected_width = avctx->aspected_width;
564
        s->aspected_height = avctx->aspected_height;
565
    }
566
    s->flags= avctx->flags;
567
    s->max_b_frames= avctx->max_b_frames;
568
    s->b_frame_strategy= avctx->b_frame_strategy;
569
    s->codec_id= avctx->codec->id;
570
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
571
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
572
    s->strict_std_compliance= avctx->strict_std_compliance;
573
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
574
    s->mpeg_quant= avctx->mpeg_quant;
575

    
576
    if (s->gop_size <= 1) {
577
        s->intra_only = 1;
578
        s->gop_size = 12;
579
    } else {
580
        s->intra_only = 0;
581
    }
582

    
583
    /* ME algorithm */
584
    if (avctx->me_method == 0)
585
        /* For compatibility */
586
        s->me_method = motion_estimation_method;
587
    else
588
        s->me_method = avctx->me_method;
589

    
590
    /* Fixed QSCALE */
591
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
592
    
593
    s->adaptive_quant= (   s->avctx->lumi_masking
594
                        || s->avctx->dark_masking
595
                        || s->avctx->temporal_cplx_masking 
596
                        || s->avctx->spatial_cplx_masking
597
                        || s->avctx->p_masking)
598
                       && !s->fixed_qscale;
599
    
600
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
601

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

    
704
            default_mv_penalty= av_mallocz( sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1) );
705
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
706
            memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
707

    
708
            for(i=-16; i<16; i++){
709
                default_fcode_tab[i + MAX_MV]= 1;
710
            }
711
        }
712
    }
713
    s->mv_penalty= default_mv_penalty;
714
    s->fcode_tab= default_fcode_tab;
715
    s->y_dc_scale_table=
716
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
717
 
718
    /* dont use mv_penalty table for crap MV as it would be confused */
719
    if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
720

    
721
    s->encoding = 1;
722

    
723
    /* init */
724
    if (MPV_common_init(s) < 0)
725
        return -1;
726
    
727
#ifdef CONFIG_ENCODERS
728
    if (s->out_format == FMT_H263)
729
        h263_encode_init(s);
730
    else if (s->out_format == FMT_MPEG1)
731
        ff_mpeg1_encode_init(s);
732
    if(s->msmpeg4_version)
733
        ff_msmpeg4_encode_init(s);
734
#endif
735

    
736
    /* init default q matrix */
737
    for(i=0;i<64;i++) {
738
        int j= s->idct_permutation[i];
739
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
740
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
741
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
742
        }else if(s->out_format == FMT_H263){
743
            s->intra_matrix[j] =
744
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
745
        }else{ /* mpeg1 */
746
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
747
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
748
        }
749
    }
750

    
751
    /* precompute matrix */
752
    /* for mjpeg, we do include qscale in the matrix */
753
    if (s->out_format != FMT_MJPEG) {
754
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
755
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
756
        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
757
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
758
    }
759

    
760
    if(ff_rate_control_init(s) < 0)
761
        return -1;
762

    
763
    s->picture_number = 0;
764
    s->picture_in_gop_number = 0;
765
    s->fake_picture_number = 0;
766
    /* motion detector init */
767
    s->f_code = 1;
768
    s->b_code = 1;
769

    
770
    return 0;
771
}
772

    
773
int MPV_encode_end(AVCodecContext *avctx)
774
{
775
    MpegEncContext *s = avctx->priv_data;
776

    
777
#ifdef STATS
778
    print_stats();
779
#endif
780

    
781
    ff_rate_control_uninit(s);
782

    
783
    MPV_common_end(s);
784
    if (s->out_format == FMT_MJPEG)
785
        mjpeg_close(s);
786
      
787
    return 0;
788
}
789

    
790
/* draw the edges of width 'w' of an image of size width, height */
791
//FIXME check that this is ok for mpeg4 interlaced
792
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
793
{
794
    UINT8 *ptr, *last_line;
795
    int i;
796

    
797
    last_line = buf + (height - 1) * wrap;
798
    for(i=0;i<w;i++) {
799
        /* top and bottom */
800
        memcpy(buf - (i + 1) * wrap, buf, width);
801
        memcpy(last_line + (i + 1) * wrap, last_line, width);
802
    }
803
    /* left and right */
804
    ptr = buf;
805
    for(i=0;i<height;i++) {
806
        memset(ptr - w, ptr[0], w);
807
        memset(ptr + width, ptr[width-1], w);
808
        ptr += wrap;
809
    }
810
    /* corners */
811
    for(i=0;i<w;i++) {
812
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
813
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
814
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
815
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
816
    }
817
}
818

    
819
/* generic function for encode/decode called before a frame is coded/decoded */
820
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
821
{
822
    int i;
823
    UINT8 *tmp;
824

    
825
    s->mb_skiped = 0;
826
    avctx->mbskip_table= s->mbskip_table;
827

    
828
    if(avctx->flags&CODEC_FLAG_DR1){
829
        if(avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type) < 0){
830
            fprintf(stderr, "get_buffer() failed\n");
831
            return -1;
832
        }
833

    
834
        s->linesize  = avctx->dr_stride;
835
        s->uvlinesize= avctx->dr_uvstride;
836
        s->ip_buffer_count= avctx->dr_ip_buffer_count;
837
    }
838
    avctx->dr_ip_buffer_count= s->ip_buffer_count;
839
    
840
    if (s->pict_type == B_TYPE) {
841
        for(i=0;i<3;i++) {
842
            if(avctx->flags&CODEC_FLAG_DR1)
843
                s->aux_picture[i]= avctx->dr_buffer[i];
844
            
845
            //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
846
            if(s->next_picture[i]==NULL)
847
                s->next_picture[i]= s->aux_picture[i];
848
            if(s->last_picture[i]==NULL)
849
                s->last_picture[i]= s->next_picture[i];
850

    
851
            s->current_picture[i] = s->aux_picture[i];
852
        }
853
        s->avctx->display_qscale_table=
854
        s->avctx->current_qscale_table= 
855
        s->qscale_table= s->aux_qscale_table;
856
    } else {
857
        for(i=0;i<3;i++) {
858
            /* swap next and last */
859
            if(avctx->flags&CODEC_FLAG_DR1)
860
                tmp= avctx->dr_buffer[i];
861
            else
862
                tmp = s->last_picture[i];
863

    
864
            s->last_picture[i] = s->next_picture[i];
865
            s->next_picture[i] = tmp;
866
            s->current_picture[i] = tmp;
867

    
868
            if(s->last_picture[i]==NULL)
869
                s->last_picture[i]= s->next_picture[i];
870

    
871
            s->last_dr_opaque= s->next_dr_opaque;
872
            s->next_dr_opaque= avctx->dr_opaque_frame;
873

    
874
            if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
875
                avctx->dr_opaque_frame= s->last_dr_opaque;
876
            else
877
                avctx->dr_opaque_frame= s->next_dr_opaque;
878
        }
879
        s->avctx->current_qscale_table= s->qscale_table      = s->last_qscale_table;
880
        s->avctx->display_qscale_table= s->last_qscale_table = s->next_qscale_table;
881
        s->next_qscale_table= s->qscale_table;
882
    }
883
    /* set dequantizer, we cant do it during init as it might change for mpeg4
884
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
885
    if(s->out_format == FMT_H263){
886
        if(s->mpeg_quant)
887
            s->dct_unquantize = s->dct_unquantize_mpeg2;
888
        else
889
            s->dct_unquantize = s->dct_unquantize_h263;
890
    }else 
891
        s->dct_unquantize = s->dct_unquantize_mpeg1;
892

    
893
    return 0;
894
}
895

    
896
/* generic function for encode/decode called after a frame has been coded/decoded */
897
void MPV_frame_end(MpegEncContext *s)
898
{
899
    s->avctx->key_frame   = (s->pict_type == I_TYPE);
900
    s->avctx->pict_type   = s->pict_type;
901

    
902
    /* draw edge for correct motion prediction if outside */
903
    if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
904
        draw_edges(s->current_picture[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
905
        draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
906
        draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
907
    }
908
    emms_c();
909
    
910
    s->last_pict_type    = s->pict_type;
911
    if(s->pict_type!=B_TYPE){
912
        s->last_non_b_pict_type= s->pict_type;
913
        s->num_available_buffers++;
914
        if(s->num_available_buffers>2) s->num_available_buffers= 2;
915
    }
916
}
917

    
918
/* reorder input for encoding */
919
void reorder_input(MpegEncContext *s, AVPicture *pict)
920
{
921
    int i, j, index;
922
            
923
    if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
924

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

    
927
    for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
928
        s->coded_order[j]= s->coded_order[j+1];
929
    }
930
    s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
931
    s->coded_order[j].pict_type=0;
932

    
933
    switch(s->input_pict_type){
934
    default: 
935
    case I_TYPE:
936
    case S_TYPE:
937
    case P_TYPE:
938
        index= s->max_b_frames - s->b_frames_since_non_b;
939
        s->b_frames_since_non_b=0;
940
        break;            
941
    case B_TYPE:
942
        index= s->max_b_frames + 1;
943
        s->b_frames_since_non_b++;
944
        break;          
945
    }
946
//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
947
    if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
948
       && pict->linesize[0] == s->linesize
949
       && pict->linesize[1] == s->uvlinesize
950
       && pict->linesize[2] == s->uvlinesize){
951
//printf("ptr\n");
952
        for(i=0; i<3; i++){
953
            s->coded_order[index].picture[i]= pict->data[i];
954
        }
955
    }else{
956
//printf("copy\n");
957
        for(i=0; i<3; i++){
958
            uint8_t *src = pict->data[i];
959
            uint8_t *dest;
960
            int src_wrap = pict->linesize[i];
961
            int dest_wrap = s->linesize;
962
            int w = s->width;
963
            int h = s->height;
964

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

    
968
            if (i >= 1) {
969
                dest_wrap >>= 1;
970
                w >>= 1;
971
                h >>= 1;
972
            }
973

    
974
            s->coded_order[index].picture[i]= dest;
975
            for(j=0;j<h;j++) {
976
                memcpy(dest, src, w);
977
                dest += dest_wrap;
978
                src += src_wrap;
979
            }
980
        }
981
        if(index!=0){
982
            s->picture_buffer_index++;
983
            if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;
984
        }
985
    }
986
    s->coded_order[index].pict_type = s->input_pict_type;
987
    s->coded_order[index].qscale    = s->input_qscale;
988
    s->coded_order[index].force_type= s->force_input_type;
989
    s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
990
    s->coded_order[index].picture_number= s->input_picture_number;
991

    
992
    for(i=0; i<3; i++){
993
        s->new_picture[i]= s->coded_order[0].picture[i];
994
    }
995
}
996

    
997
int MPV_encode_picture(AVCodecContext *avctx,
998
                       unsigned char *buf, int buf_size, void *data)
999
{
1000
    MpegEncContext *s = avctx->priv_data;
1001
    AVPicture *pict = data;
1002

    
1003
    s->input_qscale = avctx->quality;
1004

    
1005
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1006

    
1007
    if(avctx->force_type){
1008
        s->input_pict_type=
1009
        s->force_input_type= avctx->force_type;
1010
    }else if(s->flags&CODEC_FLAG_PASS2){
1011
        s->input_pict_type=
1012
        s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
1013
    }else{
1014
        s->force_input_type=0;
1015
        if (!s->intra_only) {
1016
            /* first picture of GOP is intra */
1017
            if (s->input_picture_in_gop_number % s->gop_size==0){
1018
                s->input_pict_type = I_TYPE;
1019
            }else if(s->max_b_frames==0){
1020
                s->input_pict_type = P_TYPE;
1021
            }else{
1022
                if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
1023
                    s->input_pict_type = B_TYPE;
1024
                else
1025
                    s->input_pict_type = P_TYPE;
1026
            }
1027
        } else {
1028
            s->input_pict_type = I_TYPE;
1029
        }
1030
    }
1031

    
1032
    if(s->input_pict_type==I_TYPE)
1033
        s->input_picture_in_gop_number=0;
1034
    
1035
    reorder_input(s, pict);
1036
    
1037
    /* output? */
1038
    if(s->coded_order[0].picture[0]){
1039

    
1040
        s->pict_type= s->coded_order[0].pict_type;
1041
        if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1042
            s->qscale= s->coded_order[0].qscale;
1043
        s->force_type= s->coded_order[0].force_type;
1044
        s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
1045
        s->picture_number= s->coded_order[0].picture_number;
1046

    
1047
        MPV_frame_start(s, avctx);
1048

    
1049
        encode_picture(s, s->picture_number);
1050
        
1051
        avctx->real_pict_num  = s->picture_number;
1052
        avctx->header_bits = s->header_bits;
1053
        avctx->mv_bits     = s->mv_bits;
1054
        avctx->misc_bits   = s->misc_bits;
1055
        avctx->i_tex_bits  = s->i_tex_bits;
1056
        avctx->p_tex_bits  = s->p_tex_bits;
1057
        avctx->i_count     = s->i_count;
1058
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1059
        avctx->skip_count  = s->skip_count;
1060

    
1061
        MPV_frame_end(s);
1062

    
1063
        if (s->out_format == FMT_MJPEG)
1064
            mjpeg_picture_trailer(s);
1065

    
1066
        if(!s->fixed_qscale)
1067
            avctx->quality = s->qscale;
1068
        
1069
        if(s->flags&CODEC_FLAG_PASS1)
1070
            ff_write_pass1_stats(s);
1071
    
1072
    }
1073

    
1074
    s->input_picture_number++;
1075
    s->input_picture_in_gop_number++;
1076

    
1077
    flush_put_bits(&s->pb);
1078
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1079
    
1080
    s->total_bits += s->frame_bits;
1081
    avctx->frame_bits  = s->frame_bits;
1082
//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
1083
//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);
1084
#if 0 //dump some stats to stats.txt for testing/debuging
1085
if(s->max_b_frames==0)
1086
{
1087
    static FILE *f=NULL;
1088
    if(!f) f= fopen("stats.txt", "wb");
1089
    get_psnr(pict->data, s->current_picture,
1090
             pict->linesize, s->linesize, avctx);
1091
    fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
1092
}
1093
#endif
1094

    
1095
    if (avctx->get_psnr) {
1096
        /* At this point pict->data should have the original frame   */
1097
        /* an s->current_picture should have the coded/decoded frame */
1098
        get_psnr(pict->data, s->current_picture,
1099
                 pict->linesize, s->linesize, avctx);
1100
//        printf("%f\n", avctx->psnr_y);
1101
    }
1102
    return pbBufPtr(&s->pb) - s->pb.buf;
1103
}
1104

    
1105
static inline void gmc1_motion(MpegEncContext *s,
1106
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1107
                               int dest_offset,
1108
                               UINT8 **ref_picture, int src_offset)
1109
{
1110
    UINT8 *ptr;
1111
    int offset, src_x, src_y, linesize, uvlinesize;
1112
    int motion_x, motion_y;
1113
    int emu=0;
1114

    
1115
    motion_x= s->sprite_offset[0][0];
1116
    motion_y= s->sprite_offset[0][1];
1117
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1118
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1119
    motion_x<<=(3-s->sprite_warping_accuracy);
1120
    motion_y<<=(3-s->sprite_warping_accuracy);
1121
    src_x = clip(src_x, -16, s->width);
1122
    if (src_x == s->width)
1123
        motion_x =0;
1124
    src_y = clip(src_y, -16, s->height);
1125
    if (src_y == s->height)
1126
        motion_y =0;
1127

    
1128
    linesize = s->linesize;
1129
    uvlinesize = s->uvlinesize;
1130
    
1131
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1132

    
1133
    dest_y+=dest_offset;
1134
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1135
        if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
1136
                              || src_y + (motion_y&15) + 16 > s->v_edge_pos){
1137
            emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1138
            ptr= s->edge_emu_buffer;
1139
            emu=1;
1140
        }
1141
    }
1142
    
1143
    if((motion_x|motion_y)&7){
1144
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1145
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1146
    }else{
1147
        int dxy;
1148
        
1149
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1150
        if (s->no_rounding){
1151
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1152
        }else{
1153
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1154
        }
1155
    }
1156
    
1157
    if(s->flags&CODEC_FLAG_GRAY) return;
1158

    
1159
    motion_x= s->sprite_offset[1][0];
1160
    motion_y= s->sprite_offset[1][1];
1161
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1162
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1163
    motion_x<<=(3-s->sprite_warping_accuracy);
1164
    motion_y<<=(3-s->sprite_warping_accuracy);
1165
    src_x = clip(src_x, -8, s->width>>1);
1166
    if (src_x == s->width>>1)
1167
        motion_x =0;
1168
    src_y = clip(src_y, -8, s->height>>1);
1169
    if (src_y == s->height>>1)
1170
        motion_y =0;
1171

    
1172
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1173
    ptr = ref_picture[1] + offset;
1174
    if(emu){
1175
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1176
        ptr= s->edge_emu_buffer;
1177
    }
1178
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1179
    
1180
    ptr = ref_picture[2] + offset;
1181
    if(emu){
1182
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1183
        ptr= s->edge_emu_buffer;
1184
    }
1185
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1186
    
1187
    return;
1188
}
1189

    
1190
static inline void gmc_motion(MpegEncContext *s,
1191
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1192
                               int dest_offset,
1193
                               UINT8 **ref_picture, int src_offset)
1194
{
1195
    UINT8 *ptr;
1196
    int linesize, uvlinesize;
1197
    const int a= s->sprite_warping_accuracy;
1198
    int ox, oy;
1199

    
1200
    linesize = s->linesize;
1201
    uvlinesize = s->uvlinesize;
1202

    
1203
    ptr = ref_picture[0] + src_offset;
1204

    
1205
    dest_y+=dest_offset;
1206
    
1207
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1208
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1209

    
1210
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1211
           ox, 
1212
           oy, 
1213
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1214
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1215
           a+1, (1<<(2*a+1)) - s->no_rounding,
1216
           s->h_edge_pos, s->v_edge_pos);
1217
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1218
           ox + s->sprite_delta[0][0]*8, 
1219
           oy + s->sprite_delta[1][0]*8, 
1220
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1221
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1222
           a+1, (1<<(2*a+1)) - s->no_rounding,
1223
           s->h_edge_pos, s->v_edge_pos);
1224

    
1225
    if(s->flags&CODEC_FLAG_GRAY) return;
1226

    
1227

    
1228
    dest_cb+=dest_offset>>1;
1229
    dest_cr+=dest_offset>>1;
1230
    
1231
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1232
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1233

    
1234
    ptr = ref_picture[1] + (src_offset>>1);
1235
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1236
           ox, 
1237
           oy, 
1238
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1239
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1240
           a+1, (1<<(2*a+1)) - s->no_rounding,
1241
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1242
    
1243
    ptr = ref_picture[2] + (src_offset>>1);
1244
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1245
           ox, 
1246
           oy, 
1247
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1248
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1249
           a+1, (1<<(2*a+1)) - s->no_rounding,
1250
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1251
}
1252

    
1253

    
1254
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
1255
                                    int src_x, int src_y, int w, int h){
1256
    int x, y;
1257
    int start_y, start_x, end_y, end_x;
1258
    UINT8 *buf= s->edge_emu_buffer;
1259

    
1260
    if(src_y>= h){
1261
        src+= (h-1-src_y)*linesize;
1262
        src_y=h-1;
1263
    }else if(src_y<=-block_h){
1264
        src+= (1-block_h-src_y)*linesize;
1265
        src_y=1-block_h;
1266
    }
1267
    if(src_x>= w){
1268
        src+= (w-1-src_x);
1269
        src_x=w-1;
1270
    }else if(src_x<=-block_w){
1271
        src+= (1-block_w-src_x);
1272
        src_x=1-block_w;
1273
    }
1274

    
1275
    start_y= FFMAX(0, -src_y);
1276
    start_x= FFMAX(0, -src_x);
1277
    end_y= FFMIN(block_h, h-src_y);
1278
    end_x= FFMIN(block_w, w-src_x);
1279

    
1280
    // copy existing part
1281
    for(y=start_y; y<end_y; y++){
1282
        for(x=start_x; x<end_x; x++){
1283
            buf[x + y*linesize]= src[x + y*linesize];
1284
        }
1285
    }
1286

    
1287
    //top
1288
    for(y=0; y<start_y; y++){
1289
        for(x=start_x; x<end_x; x++){
1290
            buf[x + y*linesize]= buf[x + start_y*linesize];
1291
        }
1292
    }
1293

    
1294
    //bottom
1295
    for(y=end_y; y<block_h; y++){
1296
        for(x=start_x; x<end_x; x++){
1297
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1298
        }
1299
    }
1300
                                    
1301
    for(y=0; y<block_h; y++){
1302
       //left
1303
        for(x=0; x<start_x; x++){
1304
            buf[x + y*linesize]= buf[start_x + y*linesize];
1305
        }
1306
       
1307
       //right
1308
        for(x=end_x; x<block_w; x++){
1309
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1310
        }
1311
    }
1312
}
1313

    
1314

    
1315
/* apply one mpeg motion vector to the three components */
1316
static inline void mpeg_motion(MpegEncContext *s,
1317
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1318
                               int dest_offset,
1319
                               UINT8 **ref_picture, int src_offset,
1320
                               int field_based, op_pixels_func (*pix_op)[4],
1321
                               int motion_x, int motion_y, int h)
1322
{
1323
    UINT8 *ptr;
1324
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1325
    int emu=0;
1326
#if 0    
1327
if(s->quarter_sample)
1328
{
1329
    motion_x>>=1;
1330
    motion_y>>=1;
1331
}
1332
#endif
1333
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1334
    src_x = s->mb_x * 16 + (motion_x >> 1);
1335
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1336
                
1337
    /* WARNING: do no forget half pels */
1338
    height = s->height >> field_based;
1339
    v_edge_pos = s->v_edge_pos >> field_based;
1340
    src_x = clip(src_x, -16, s->width);
1341
    if (src_x == s->width)
1342
        dxy &= ~1;
1343
    src_y = clip(src_y, -16, height);
1344
    if (src_y == height)
1345
        dxy &= ~2;
1346
    linesize   = s->linesize << field_based;
1347
    uvlinesize = s->uvlinesize << field_based;
1348
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1349
    dest_y += dest_offset;
1350

    
1351
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1352
        if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1353
                              || src_y + (motion_y&1) + h  > v_edge_pos){
1354
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1355
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1356
            ptr= s->edge_emu_buffer + src_offset;
1357
            emu=1;
1358
        }
1359
    }
1360
    pix_op[0][dxy](dest_y, ptr, linesize, h);
1361

    
1362
    if(s->flags&CODEC_FLAG_GRAY) return;
1363

    
1364
    if (s->out_format == FMT_H263) {
1365
        dxy = 0;
1366
        if ((motion_x & 3) != 0)
1367
            dxy |= 1;
1368
        if ((motion_y & 3) != 0)
1369
            dxy |= 2;
1370
        mx = motion_x >> 2;
1371
        my = motion_y >> 2;
1372
    } else {
1373
        mx = motion_x / 2;
1374
        my = motion_y / 2;
1375
        dxy = ((my & 1) << 1) | (mx & 1);
1376
        mx >>= 1;
1377
        my >>= 1;
1378
    }
1379
    
1380
    src_x = s->mb_x * 8 + mx;
1381
    src_y = s->mb_y * (8 >> field_based) + my;
1382
    src_x = clip(src_x, -8, s->width >> 1);
1383
    if (src_x == (s->width >> 1))
1384
        dxy &= ~1;
1385
    src_y = clip(src_y, -8, height >> 1);
1386
    if (src_y == (height >> 1))
1387
        dxy &= ~2;
1388
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1389
    ptr = ref_picture[1] + offset;
1390
    if(emu){
1391
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1392
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1393
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1394
    }
1395
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1396

    
1397
    ptr = ref_picture[2] + offset;
1398
    if(emu){
1399
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1400
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1401
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1402
    }
1403
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1404
}
1405

    
1406
static inline void qpel_motion(MpegEncContext *s,
1407
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1408
                               int dest_offset,
1409
                               UINT8 **ref_picture, int src_offset,
1410
                               int field_based, op_pixels_func (*pix_op)[4],
1411
                               qpel_mc_func (*qpix_op)[16],
1412
                               int motion_x, int motion_y, int h)
1413
{
1414
    UINT8 *ptr;
1415
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1416
    int emu=0;
1417

    
1418
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1419
    src_x = s->mb_x * 16 + (motion_x >> 2);
1420
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1421

    
1422
    height = s->height >> field_based;
1423
    v_edge_pos = s->v_edge_pos >> field_based;
1424
    src_x = clip(src_x, -16, s->width);
1425
    if (src_x == s->width)
1426
        dxy &= ~3;
1427
    src_y = clip(src_y, -16, height);
1428
    if (src_y == height)
1429
        dxy &= ~12;
1430
    linesize = s->linesize << field_based;
1431
    uvlinesize = s->uvlinesize << field_based;
1432
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1433
    dest_y += dest_offset;
1434
//printf("%d %d %d\n", src_x, src_y, dxy);
1435
    
1436
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1437
        if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1438
                              || src_y + (motion_y&3) + h  > v_edge_pos){
1439
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1440
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1441
            ptr= s->edge_emu_buffer + src_offset;
1442
            emu=1;
1443
        }
1444
    }
1445
    if(!field_based)
1446
        qpix_op[0][dxy](dest_y, ptr, linesize);
1447
    else{
1448
        //damn interlaced mode
1449
        //FIXME boundary mirroring is not exactly correct here
1450
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
1451
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1452
    }
1453

    
1454
    if(s->flags&CODEC_FLAG_GRAY) return;
1455

    
1456
    if(field_based){
1457
        mx= motion_x/2;
1458
        my= motion_y>>1;
1459
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1460
        mx= (motion_x>>1)|(motion_x&1);
1461
        my= (motion_y>>1)|(motion_y&1);
1462
    }else{
1463
        mx= motion_x/2;
1464
        my= motion_y/2;
1465
    }
1466
    mx= (mx>>1)|(mx&1);
1467
    my= (my>>1)|(my&1);
1468
    dxy= (mx&1) | ((my&1)<<1);
1469
    mx>>=1;
1470
    my>>=1;
1471

    
1472
    src_x = s->mb_x * 8 + mx;
1473
    src_y = s->mb_y * (8 >> field_based) + my;
1474
    src_x = clip(src_x, -8, s->width >> 1);
1475
    if (src_x == (s->width >> 1))
1476
        dxy &= ~1;
1477
    src_y = clip(src_y, -8, height >> 1);
1478
    if (src_y == (height >> 1))
1479
        dxy &= ~2;
1480

    
1481
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1482
    ptr = ref_picture[1] + offset;
1483
    if(emu){
1484
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1485
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1486
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1487
    }
1488
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1489
    
1490
    ptr = ref_picture[2] + offset;
1491
    if(emu){
1492
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1493
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1494
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1495
    }
1496
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1497
}
1498

    
1499

    
1500
static inline void MPV_motion(MpegEncContext *s, 
1501
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1502
                              int dir, UINT8 **ref_picture, 
1503
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1504
{
1505
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1506
    int mb_x, mb_y, i;
1507
    UINT8 *ptr, *dest;
1508
    int emu=0;
1509

    
1510
    mb_x = s->mb_x;
1511
    mb_y = s->mb_y;
1512

    
1513
    switch(s->mv_type) {
1514
    case MV_TYPE_16X16:
1515
        if(s->mcsel){
1516
            if(s->real_sprite_warping_points==1){
1517
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1518
                            ref_picture, 0);
1519
            }else{
1520
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1521
                            ref_picture, 0);
1522
            }
1523
        }else if(s->quarter_sample){
1524
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1525
                        ref_picture, 0,
1526
                        0, pix_op, qpix_op,
1527
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1528
        }else{
1529
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1530
                        ref_picture, 0,
1531
                        0, pix_op,
1532
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1533
        }           
1534
        break;
1535
    case MV_TYPE_8X8:
1536
        mx = 0;
1537
        my = 0;
1538
        if(s->quarter_sample){
1539
            for(i=0;i<4;i++) {
1540
                motion_x = s->mv[dir][i][0];
1541
                motion_y = s->mv[dir][i][1];
1542

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

    
1566
                mx += s->mv[dir][i][0]/2;
1567
                my += s->mv[dir][i][1]/2;
1568
            }
1569
        }else{
1570
            for(i=0;i<4;i++) {
1571
                motion_x = s->mv[dir][i][0];
1572
                motion_y = s->mv[dir][i][1];
1573

    
1574
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1575
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1576
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1577
                    
1578
                /* WARNING: do no forget half pels */
1579
                src_x = clip(src_x, -16, s->width);
1580
                if (src_x == s->width)
1581
                    dxy &= ~1;
1582
                src_y = clip(src_y, -16, s->height);
1583
                if (src_y == s->height)
1584
                    dxy &= ~2;
1585
                    
1586
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1587
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1588
                    if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1589
                                          || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1590
                        emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1591
                        ptr= s->edge_emu_buffer;
1592
                    }
1593
                }
1594
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1595
                pix_op[1][dxy](dest, ptr, s->linesize, 8);
1596

    
1597
                mx += s->mv[dir][i][0];
1598
                my += s->mv[dir][i][1];
1599
            }
1600
        }
1601

    
1602
        if(s->flags&CODEC_FLAG_GRAY) break;
1603
        /* In case of 8X8, we construct a single chroma motion vector
1604
           with a special rounding */
1605
        for(i=0;i<4;i++) {
1606
        }
1607
        if (mx >= 0)
1608
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1609
        else {
1610
            mx = -mx;
1611
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1612
        }
1613
        if (my >= 0)
1614
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1615
        else {
1616
            my = -my;
1617
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1618
        }
1619
        dxy = ((my & 1) << 1) | (mx & 1);
1620
        mx >>= 1;
1621
        my >>= 1;
1622

    
1623
        src_x = mb_x * 8 + mx;
1624
        src_y = mb_y * 8 + my;
1625
        src_x = clip(src_x, -8, s->width/2);
1626
        if (src_x == s->width/2)
1627
            dxy &= ~1;
1628
        src_y = clip(src_y, -8, s->height/2);
1629
        if (src_y == s->height/2)
1630
            dxy &= ~2;
1631
        
1632
        offset = (src_y * (s->uvlinesize)) + src_x;
1633
        ptr = ref_picture[1] + offset;
1634
        if(s->flags&CODEC_FLAG_EMU_EDGE){
1635
                if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1636
                                      || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1637
                    emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1638
                    ptr= s->edge_emu_buffer;
1639
                    emu=1;
1640
                }
1641
            }
1642
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1643

    
1644
        ptr = ref_picture[2] + offset;
1645
        if(emu){
1646
            emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1647
            ptr= s->edge_emu_buffer;
1648
        }
1649
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1650
        break;
1651
    case MV_TYPE_FIELD:
1652
        if (s->picture_structure == PICT_FRAME) {
1653
            if(s->quarter_sample){
1654
                /* top field */
1655
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1656
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1657
                            1, pix_op, qpix_op,
1658
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1659
                /* bottom field */
1660
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1661
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1662
                            1, pix_op, qpix_op,
1663
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1664
            }else{
1665
                /* top field */       
1666
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1667
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1668
                            1, pix_op,
1669
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1670
                /* bottom field */
1671
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1672
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1673
                            1, pix_op,
1674
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1675
            }
1676
        } else {
1677
            
1678

    
1679
        }
1680
        break;
1681
    }
1682
}
1683

    
1684

    
1685
/* put block[] to dest[] */
1686
static inline void put_dct(MpegEncContext *s, 
1687
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1688
{
1689
    s->dct_unquantize(s, block, i, s->qscale);
1690
    s->idct_put (dest, line_size, block);
1691
}
1692

    
1693
/* add block[] to dest[] */
1694
static inline void add_dct(MpegEncContext *s, 
1695
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1696
{
1697
    if (s->block_last_index[i] >= 0) {
1698
        s->idct_add (dest, line_size, block);
1699
    }
1700
}
1701

    
1702
static inline void add_dequant_dct(MpegEncContext *s, 
1703
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1704
{
1705
    if (s->block_last_index[i] >= 0) {
1706
        s->dct_unquantize(s, block, i, s->qscale);
1707

    
1708
        s->idct_add (dest, line_size, block);
1709
    }
1710
}
1711

    
1712
/**
1713
 * cleans dc, ac, coded_block for the current non intra MB
1714
 */
1715
void ff_clean_intra_table_entries(MpegEncContext *s)
1716
{
1717
    int wrap = s->block_wrap[0];
1718
    int xy = s->block_index[0];
1719
    
1720
    s->dc_val[0][xy           ] = 
1721
    s->dc_val[0][xy + 1       ] = 
1722
    s->dc_val[0][xy     + wrap] =
1723
    s->dc_val[0][xy + 1 + wrap] = 1024;
1724
    /* ac pred */
1725
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(INT16));
1726
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
1727
    if (s->msmpeg4_version>=3) {
1728
        s->coded_block[xy           ] =
1729
        s->coded_block[xy + 1       ] =
1730
        s->coded_block[xy     + wrap] =
1731
        s->coded_block[xy + 1 + wrap] = 0;
1732
    }
1733
    /* chroma */
1734
    wrap = s->block_wrap[4];
1735
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1736
    s->dc_val[1][xy] =
1737
    s->dc_val[2][xy] = 1024;
1738
    /* ac pred */
1739
    memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1740
    memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1741
    
1742
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1743
}
1744

    
1745
/* generic function called after a macroblock has been parsed by the
1746
   decoder or after it has been encoded by the encoder.
1747

1748
   Important variables used:
1749
   s->mb_intra : true if intra macroblock
1750
   s->mv_dir   : motion vector direction
1751
   s->mv_type  : motion vector type
1752
   s->mv       : motion vector
1753
   s->interlaced_dct : true if interlaced dct used (mpeg2)
1754
 */
1755
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1756
{
1757
    int mb_x, mb_y;
1758
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1759

    
1760
    mb_x = s->mb_x;
1761
    mb_y = s->mb_y;
1762

    
1763
    s->qscale_table[mb_xy]= s->qscale;
1764

    
1765
    /* update DC predictors for P macroblocks */
1766
    if (!s->mb_intra) {
1767
        if (s->h263_pred || s->h263_aic) {
1768
            if(s->mbintra_table[mb_xy])
1769
                ff_clean_intra_table_entries(s);
1770
        } else {
1771
            s->last_dc[0] =
1772
            s->last_dc[1] =
1773
            s->last_dc[2] = 128 << s->intra_dc_precision;
1774
        }
1775
    }
1776
    else if (s->h263_pred || s->h263_aic)
1777
        s->mbintra_table[mb_xy]=1;
1778

    
1779
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1780
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1781
        
1782
        const int wrap = s->block_wrap[0];
1783
        const int xy = s->block_index[0];
1784
        const int mb_index= s->mb_x + s->mb_y*s->mb_width;
1785
        if(s->mv_type == MV_TYPE_8X8){
1786
            s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
1787
        } else {
1788
            int motion_x, motion_y;
1789
            if (s->mb_intra) {
1790
                motion_x = 0;
1791
                motion_y = 0;
1792
                if(s->co_located_type_table)
1793
                    s->co_located_type_table[mb_index]= 0;
1794
            } else if (s->mv_type == MV_TYPE_16X16) {
1795
                motion_x = s->mv[0][0][0];
1796
                motion_y = s->mv[0][0][1];
1797
                if(s->co_located_type_table)
1798
                    s->co_located_type_table[mb_index]= 0;
1799
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1800
                int i;
1801
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
1802
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
1803
                motion_x = (motion_x>>1) | (motion_x&1);
1804
                for(i=0; i<2; i++){
1805
                    s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
1806
                    s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
1807
                    s->field_select_table[mb_index][i]= s->field_select[0][i];
1808
                }
1809
                s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
1810
            }
1811
            /* no update if 8X8 because it has been done during parsing */
1812
            s->motion_val[xy][0] = motion_x;
1813
            s->motion_val[xy][1] = motion_y;
1814
            s->motion_val[xy + 1][0] = motion_x;
1815
            s->motion_val[xy + 1][1] = motion_y;
1816
            s->motion_val[xy + wrap][0] = motion_x;
1817
            s->motion_val[xy + wrap][1] = motion_y;
1818
            s->motion_val[xy + 1 + wrap][0] = motion_x;
1819
            s->motion_val[xy + 1 + wrap][1] = motion_y;
1820
        }
1821
    }
1822
    
1823
    if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1824
        UINT8 *dest_y, *dest_cb, *dest_cr;
1825
        int dct_linesize, dct_offset;
1826
        op_pixels_func (*op_pix)[4];
1827
        qpel_mc_func (*op_qpix)[16];
1828

    
1829
        /* avoid copy if macroblock skipped in last frame too 
1830
           dont touch it for B-frames as they need the skip info from the next p-frame */
1831
        if (s->pict_type != B_TYPE) {
1832
            UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
1833
            if (s->mb_skiped) {
1834
                s->mb_skiped = 0;
1835

    
1836
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
1837
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
1838

    
1839
                /* if previous was skipped too, then nothing to do ! 
1840
                   skip only during decoding as we might trash the buffers during encoding a bit */
1841
                if (*mbskip_ptr >= s->ip_buffer_count  && !s->encoding) 
1842
                    return;
1843
            } else {
1844
                *mbskip_ptr = 0; /* not skipped */
1845
            }
1846
        }
1847

    
1848
        if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
1849
            dest_y = s->current_picture [0] + mb_x * 16;
1850
            dest_cb = s->current_picture[1] + mb_x * 8;
1851
            dest_cr = s->current_picture[2] + mb_x * 8;
1852
        }else{
1853
            dest_y = s->current_picture [0] + (mb_y * 16* s->linesize  ) + mb_x * 16;
1854
            dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
1855
            dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
1856
        }
1857

    
1858
        if (s->interlaced_dct) {
1859
            dct_linesize = s->linesize * 2;
1860
            dct_offset = s->linesize;
1861
        } else {
1862
            dct_linesize = s->linesize;
1863
            dct_offset = s->linesize * 8;
1864
        }
1865

    
1866
        if (!s->mb_intra) {
1867
            /* motion handling */
1868
            /* decoding or more than one mb_type (MC was allready done otherwise) */
1869
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
1870
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
1871
                    op_pix = s->dsp.put_pixels_tab;
1872
                    op_qpix= s->dsp.put_qpel_pixels_tab;
1873
                }else{
1874
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
1875
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
1876
                }
1877

    
1878
                if (s->mv_dir & MV_DIR_FORWARD) {
1879
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1880
                    op_pix = s->dsp.avg_pixels_tab;
1881
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
1882
                }
1883
                if (s->mv_dir & MV_DIR_BACKWARD) {
1884
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1885
                }
1886
            }
1887

    
1888
            /* skip dequant / idct if we are really late ;) */
1889
            if(s->hurry_up>1) return;
1890

    
1891
            /* add dct residue */
1892
            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
1893
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
1894
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
1895
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1896
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1897
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1898

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

    
1909
                if(!(s->flags&CODEC_FLAG_GRAY)){
1910
                    add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
1911
                    add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
1912
                }
1913
            }
1914
        } else {
1915
            /* dct only in intra block */
1916
            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
1917
                put_dct(s, block[0], 0, dest_y, dct_linesize);
1918
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1919
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1920
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1921

    
1922
                if(!(s->flags&CODEC_FLAG_GRAY)){
1923
                    put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
1924
                    put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
1925
                }
1926
            }else{
1927
                s->idct_put(dest_y                 , dct_linesize, block[0]);
1928
                s->idct_put(dest_y              + 8, dct_linesize, block[1]);
1929
                s->idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
1930
                s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
1931

    
1932
                if(!(s->flags&CODEC_FLAG_GRAY)){
1933
                    s->idct_put(dest_cb, s->uvlinesize, block[4]);
1934
                    s->idct_put(dest_cr, s->uvlinesize, block[5]);
1935
                }
1936
            }
1937
        }
1938
    }
1939
}
1940

    
1941
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
1942
{
1943
    static const char tab[64]=
1944
        {3,2,2,1,1,1,1,1,
1945
         1,1,1,1,1,1,1,1,
1946
         1,1,1,1,1,1,1,1,
1947
         0,0,0,0,0,0,0,0,
1948
         0,0,0,0,0,0,0,0,
1949
         0,0,0,0,0,0,0,0,
1950
         0,0,0,0,0,0,0,0,
1951
         0,0,0,0,0,0,0,0};
1952
    int score=0;
1953
    int run=0;
1954
    int i;
1955
    DCTELEM *block= s->block[n];
1956
    const int last_index= s->block_last_index[n];
1957
    int skip_dc;
1958

    
1959
    if(threshold<0){
1960
        skip_dc=0;
1961
        threshold= -threshold;
1962
    }else
1963
        skip_dc=1;
1964

    
1965
    /* are all which we could set to zero are allready zero? */
1966
    if(last_index<=skip_dc - 1) return;
1967

    
1968
    for(i=0; i<=last_index; i++){
1969
        const int j = s->intra_scantable.permutated[i];
1970
        const int level = ABS(block[j]);
1971
        if(level==1){
1972
            if(skip_dc && i==0) continue;
1973
            score+= tab[run];
1974
            run=0;
1975
        }else if(level>1){
1976
            return;
1977
        }else{
1978
            run++;
1979
        }
1980
    }
1981
    if(score >= threshold) return;
1982
    for(i=skip_dc; i<=last_index; i++){
1983
        const int j = s->intra_scantable.permutated[i];
1984
        block[j]=0;
1985
    }
1986
    if(block[0]) s->block_last_index[n]= 0;
1987
    else         s->block_last_index[n]= -1;
1988
}
1989

    
1990
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1991
{
1992
    int i;
1993
    const int maxlevel= s->max_qcoeff;
1994
    const int minlevel= s->min_qcoeff;
1995
    
1996
    if(s->mb_intra){
1997
        i=1; //skip clipping of intra dc
1998
    }else
1999
        i=0;
2000
    
2001
    for(;i<=last_index; i++){
2002
        const int j= s->intra_scantable.permutated[i];
2003
        int level = block[j];
2004
       
2005
        if     (level>maxlevel) level=maxlevel;
2006
        else if(level<minlevel) level=minlevel;
2007
        block[j]= level;
2008
    }
2009
}
2010

    
2011
static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
2012
{
2013
    int i;
2014

    
2015
    if(s->mb_intra){
2016
        i=1; //skip clipping of intra dc
2017
         //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
2018
    }else
2019
        i=0;
2020
    
2021
    for(;i<=s->block_last_index[n]; i++){
2022
        const int j = s->intra_scantable.permutated[i];
2023
        int level = block[j];
2024
        
2025
        block[j]= ROUNDED_DIV(level*oldq, newq);
2026
    }
2027

    
2028
    for(i=s->block_last_index[n]; i>=0; i--){
2029
        const int j = s->intra_scantable.permutated[i];
2030
        if(block[j]) break;
2031
    }
2032
    s->block_last_index[n]= i;
2033
}
2034

    
2035
static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
2036
{
2037
    int i,n, newq;
2038
    const int maxlevel= s->max_qcoeff;
2039
    const int minlevel= s->min_qcoeff;
2040
    int largest=0, smallest=0;
2041

    
2042
    assert(s->adaptive_quant);
2043
    
2044
    for(n=0; n<6; n++){
2045
        if(s->mb_intra){
2046
            i=1; //skip clipping of intra dc
2047
             //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
2048
        }else
2049
            i=0;
2050

    
2051
        for(;i<=s->block_last_index[n]; i++){
2052
            const int j = s->intra_scantable.permutated[i];
2053
            int level = block[n][j];
2054
            if(largest  < level) largest = level;
2055
            if(smallest > level) smallest= level;
2056
        }
2057
    }
2058
    
2059
    for(newq=s->qscale+1; newq<32; newq++){
2060
        if(   ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
2061
           && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel) 
2062
            break;
2063
    }
2064
        
2065
    if(s->out_format==FMT_H263){
2066
        /* h263 like formats cannot change qscale by more than 2 easiely */
2067
        if(s->avctx->qmin + 2 < newq)
2068
            newq= s->avctx->qmin + 2;
2069
    }
2070

    
2071
    for(n=0; n<6; n++){
2072
        requantize_coeffs(s, block[n], s->qscale, newq, n);
2073
        clip_coeffs(s, block[n], s->block_last_index[n]);
2074
    }
2075
     
2076
    s->dquant+= newq - s->qscale;
2077
    s->qscale= newq;
2078
}
2079
#if 0
2080
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
2081
    int score=0;
2082
    int x,y;
2083
    
2084
    for(y=0; y<7; y++){
2085
        for(x=0; x<16; x+=4){
2086
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2087
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2088
        }
2089
        s+= stride;
2090
    }
2091
    
2092
    return score;
2093
}
2094

2095
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
2096
    int score=0;
2097
    int x,y;
2098
    
2099
    for(y=0; y<7; y++){
2100
        for(x=0; x<16; x++){
2101
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2102
        }
2103
        s1+= stride;
2104
        s2+= stride;
2105
    }
2106
    
2107
    return score;
2108
}
2109
#else
2110
#define SQ(a) ((a)*(a))
2111

    
2112
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
2113
    int score=0;
2114
    int x,y;
2115
    
2116
    for(y=0; y<7; y++){
2117
        for(x=0; x<16; x+=4){
2118
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2119
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2120
        }
2121
        s+= stride;
2122
    }
2123
    
2124
    return score;
2125
}
2126

    
2127
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
2128
    int score=0;
2129
    int x,y;
2130
    
2131
    for(y=0; y<7; y++){
2132
        for(x=0; x<16; x++){
2133
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2134
        }
2135
        s1+= stride;
2136
        s2+= stride;
2137
    }
2138
    
2139
    return score;
2140
}
2141

    
2142
#endif
2143

    
2144
void ff_draw_horiz_band(MpegEncContext *s){
2145
    if (    s->avctx->draw_horiz_band 
2146
        && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
2147
        UINT8 *src_ptr[3];
2148
        int y, h, offset;
2149
        y = s->mb_y * 16;
2150
        h = s->height - y;
2151
        if (h > 16)
2152
            h = 16;
2153

    
2154
        if(s->pict_type==B_TYPE)
2155
            offset = 0;
2156
        else
2157
            offset = y * s->linesize;
2158

    
2159
        if(s->pict_type==B_TYPE || (!s->has_b_frames)){
2160
            src_ptr[0] = s->current_picture[0] + offset;
2161
            src_ptr[1] = s->current_picture[1] + (offset >> 2);
2162
            src_ptr[2] = s->current_picture[2] + (offset >> 2);
2163
        } else {
2164
            src_ptr[0] = s->last_picture[0] + offset;
2165
            src_ptr[1] = s->last_picture[1] + (offset >> 2);
2166
            src_ptr[2] = s->last_picture[2] + (offset >> 2);
2167
        }
2168
        emms_c();
2169

    
2170
        s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2171
                               y, s->width, h);
2172
    }
2173
}
2174

    
2175
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2176
{
2177
    const int mb_x= s->mb_x;
2178
    const int mb_y= s->mb_y;
2179
    int i;
2180
    int skip_dct[6];
2181
    int dct_offset   = s->linesize*8; //default for progressive frames
2182
    
2183
    for(i=0; i<6; i++) skip_dct[i]=0;
2184
    
2185
    if(s->adaptive_quant){
2186
        s->dquant= s->qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2187

    
2188
        if(s->out_format==FMT_H263){
2189
            if     (s->dquant> 2) s->dquant= 2;
2190
            else if(s->dquant<-2) s->dquant=-2;
2191
        }
2192
            
2193
        if(s->codec_id==CODEC_ID_MPEG4){        
2194
            if(!s->mb_intra){
2195
                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2196

    
2197
                if(s->mv_dir&MV_DIRECT)
2198
                    s->dquant=0;
2199
            }
2200
        }
2201
        s->qscale+= s->dquant;
2202
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2203
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2204
    }
2205

    
2206
    if (s->mb_intra) {
2207
        UINT8 *ptr;
2208
        int wrap_y;
2209
        int emu=0;
2210

    
2211
        wrap_y = s->linesize;
2212
        ptr = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2213

    
2214
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2215
            emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2216
            ptr= s->edge_emu_buffer;
2217
            emu=1;
2218
        }
2219
        
2220
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2221
            int progressive_score, interlaced_score;
2222
            
2223
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2224
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2225
            
2226
            if(progressive_score > interlaced_score + 100){
2227
                s->interlaced_dct=1;
2228
            
2229
                dct_offset= wrap_y;
2230
                wrap_y<<=1;
2231
            }else
2232
                s->interlaced_dct=0;
2233
        }
2234
        
2235
        s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
2236
        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
2237
        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2238
        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2239

    
2240
        if(s->flags&CODEC_FLAG_GRAY){
2241
            skip_dct[4]= 1;
2242
            skip_dct[5]= 1;
2243
        }else{
2244
            int wrap_c = s->uvlinesize;
2245
            ptr = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2246
            if(emu){
2247
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2248
                ptr= s->edge_emu_buffer;
2249
            }
2250
            s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2251

    
2252
            ptr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2253
            if(emu){
2254
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2255
                ptr= s->edge_emu_buffer;
2256
            }
2257
            s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2258
        }
2259
    }else{
2260
        op_pixels_func (*op_pix)[4];
2261
        qpel_mc_func (*op_qpix)[16];
2262
        UINT8 *dest_y, *dest_cb, *dest_cr;
2263
        UINT8 *ptr_y, *ptr_cb, *ptr_cr;
2264
        int wrap_y, wrap_c;
2265
        int emu=0;
2266

    
2267
        dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
2268
        dest_cb = s->current_picture[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2269
        dest_cr = s->current_picture[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2270
        wrap_y = s->linesize;
2271
        wrap_c = s->uvlinesize;
2272
        ptr_y  = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2273
        ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2274
        ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2275

    
2276
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
2277
            op_pix = s->dsp.put_pixels_tab;
2278
            op_qpix= s->dsp.put_qpel_pixels_tab;
2279
        }else{
2280
            op_pix = s->dsp.put_no_rnd_pixels_tab;
2281
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2282
        }
2283

    
2284
        if (s->mv_dir & MV_DIR_FORWARD) {
2285
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
2286
            op_pix = s->dsp.avg_pixels_tab;
2287
            op_qpix= s->dsp.avg_qpel_pixels_tab;
2288
        }
2289
        if (s->mv_dir & MV_DIR_BACKWARD) {
2290
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
2291
        }
2292

    
2293
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2294
            emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2295
            ptr_y= s->edge_emu_buffer;
2296
            emu=1;
2297
        }
2298
        
2299
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2300
            int progressive_score, interlaced_score;
2301
            
2302
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
2303
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
2304
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
2305
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
2306
            
2307
            if(progressive_score > interlaced_score + 600){
2308
                s->interlaced_dct=1;
2309
            
2310
                dct_offset= wrap_y;
2311
                wrap_y<<=1;
2312
            }else
2313
                s->interlaced_dct=0;
2314
        }
2315
        
2316
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
2317
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
2318
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
2319
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2320
        
2321
        if(s->flags&CODEC_FLAG_GRAY){
2322
            skip_dct[4]= 1;
2323
            skip_dct[5]= 1;
2324
        }else{
2325
            if(emu){
2326
                emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2327
                ptr_cb= s->edge_emu_buffer;
2328
            }
2329
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2330
            if(emu){
2331
                emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2332
                ptr_cr= s->edge_emu_buffer;
2333
            }
2334
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2335
        }
2336

    
2337
        /* pre quantization */         
2338
        if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2339
            //FIXME optimize
2340
            if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2341
            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2342
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2343
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2344
            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
2345
            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
2346
#if 0
2347
{
2348
 static int stat[7];
2349
 int num=0;
2350
 for(i=0; i<6; i++)
2351
  if(skip_dct[i]) num++;
2352
 stat[num]++;
2353
 
2354
 if(s->mb_x==0 && s->mb_y==0){
2355
  for(i=0; i<7; i++){
2356
   printf("%6d %1d\n", stat[i], i);
2357
  }
2358
 }
2359
}
2360
#endif
2361
        }
2362

    
2363
    }
2364
            
2365
#if 0
2366
            {
2367
                float adap_parm;
2368
                
2369
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2370
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2371
            
2372
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
2373
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
2374
                        s->qscale, adap_parm, s->qscale*adap_parm,
2375
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2376
            }
2377
#endif
2378
    /* DCT & quantize */
2379
    if(s->out_format==FMT_MJPEG){
2380
        for(i=0;i<6;i++) {
2381
            int overflow;
2382
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2383
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2384
        }
2385
    }else{
2386
        for(i=0;i<6;i++) {
2387
            if(!skip_dct[i]){
2388
                int overflow;
2389
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2390
            // FIXME we could decide to change to quantizer instead of clipping
2391
            // JS: I don't think that would be a good idea it could lower quality instead
2392
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
2393
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2394
            }else
2395
                s->block_last_index[i]= -1;
2396
        }
2397
        if(s->luma_elim_threshold && !s->mb_intra)
2398
            for(i=0; i<4; i++)
2399
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2400
        if(s->chroma_elim_threshold && !s->mb_intra)
2401
            for(i=4; i<6; i++)
2402
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2403
    }
2404

    
2405
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2406
        s->block_last_index[4]=
2407
        s->block_last_index[5]= 0;
2408
        s->block[4][0]=
2409
        s->block[5][0]= 128;
2410
    }
2411

    
2412
#ifdef CONFIG_ENCODERS
2413
    /* huffman encode */
2414
    switch(s->out_format) {
2415
    case FMT_MPEG1:
2416
        mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2417
        break;
2418
    case FMT_H263:
2419
        if (s->h263_msmpeg4)
2420
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2421
        else if(s->h263_pred)
2422
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2423
        else
2424
            h263_encode_mb(s, s->block, motion_x, motion_y);
2425
        break;
2426
    case FMT_MJPEG:
2427
        mjpeg_encode_mb(s, s->block);
2428
        break;
2429
    }
2430
#endif
2431
}
2432

    
2433
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
2434
{
2435
    int bytes= length>>4;
2436
    int bits= length&15;
2437
    int i;
2438

    
2439
    if(length==0) return;
2440

    
2441
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2442
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2443
}
2444

    
2445
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2446
    int i;
2447

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

    
2450
    /* mpeg1 */
2451
    d->mb_incr= s->mb_incr;
2452
    for(i=0; i<3; i++)
2453
        d->last_dc[i]= s->last_dc[i];
2454
    
2455
    /* statistics */
2456
    d->mv_bits= s->mv_bits;
2457
    d->i_tex_bits= s->i_tex_bits;
2458
    d->p_tex_bits= s->p_tex_bits;
2459
    d->i_count= s->i_count;
2460
    d->f_count= s->f_count;
2461
    d->b_count= s->b_count;
2462
    d->skip_count= s->skip_count;
2463
    d->misc_bits= s->misc_bits;
2464
    d->last_bits= 0;
2465

    
2466
    d->mb_skiped= s->mb_skiped;
2467
}
2468

    
2469
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2470
    int i;
2471

    
2472
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
2473
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2474
    
2475
    /* mpeg1 */
2476
    d->mb_incr= s->mb_incr;
2477
    for(i=0; i<3; i++)
2478
        d->last_dc[i]= s->last_dc[i];
2479
    
2480
    /* statistics */
2481
    d->mv_bits= s->mv_bits;
2482
    d->i_tex_bits= s->i_tex_bits;
2483
    d->p_tex_bits= s->p_tex_bits;
2484
    d->i_count= s->i_count;
2485
    d->f_count= s->f_count;
2486
    d->b_count= s->b_count;
2487
    d->skip_count= s->skip_count;
2488
    d->misc_bits= s->misc_bits;
2489

    
2490
    d->mb_intra= s->mb_intra;
2491
    d->mb_skiped= s->mb_skiped;
2492
    d->mv_type= s->mv_type;
2493
    d->mv_dir= s->mv_dir;
2494
    d->pb= s->pb;
2495
    if(s->data_partitioning){
2496
        d->pb2= s->pb2;
2497
        d->tex_pb= s->tex_pb;
2498
    }
2499
    d->block= s->block;
2500
    for(i=0; i<6; i++)
2501
        d->block_last_index[i]= s->block_last_index[i];
2502
    d->interlaced_dct= s->interlaced_dct;
2503
}
2504

    
2505
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
2506
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2507
                           int *dmin, int *next_block, int motion_x, int motion_y)
2508
{
2509
    int bits_count;
2510
    
2511
    copy_context_before_encode(s, backup, type);
2512

    
2513
    s->block= s->blocks[*next_block];
2514
    s->pb= pb[*next_block];
2515
    if(s->data_partitioning){
2516
        s->pb2   = pb2   [*next_block];
2517
        s->tex_pb= tex_pb[*next_block];
2518
    }
2519

    
2520
    encode_mb(s, motion_x, motion_y);
2521

    
2522
    bits_count= get_bit_count(&s->pb);
2523
    if(s->data_partitioning){
2524
        bits_count+= get_bit_count(&s->pb2);
2525
        bits_count+= get_bit_count(&s->tex_pb);
2526
    }
2527

    
2528
    if(bits_count<*dmin){
2529
        *dmin= bits_count;
2530
        *next_block^=1;
2531

    
2532
        copy_context_after_encode(best, s, type);
2533
    }
2534
}
2535

    
2536
static void encode_picture(MpegEncContext *s, int picture_number)
2537
{
2538
    int mb_x, mb_y, pdif = 0;
2539
    int i;
2540
    int bits;
2541
    MpegEncContext best_s, backup_s;
2542
    UINT8 bit_buf[2][3000];
2543
    UINT8 bit_buf2[2][3000];
2544
    UINT8 bit_buf_tex[2][3000];
2545
    PutBitContext pb[2], pb2[2], tex_pb[2];
2546

    
2547
    for(i=0; i<2; i++){
2548
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
2549
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
2550
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2551
    }
2552

    
2553
    s->picture_number = picture_number;
2554

    
2555
    s->block_wrap[0]=
2556
    s->block_wrap[1]=
2557
    s->block_wrap[2]=
2558
    s->block_wrap[3]= s->mb_width*2 + 2;
2559
    s->block_wrap[4]=
2560
    s->block_wrap[5]= s->mb_width + 2;
2561
    
2562
    /* Reset the average MB variance */
2563
    s->mb_var_sum = 0;
2564
    s->mc_mb_var_sum = 0;
2565

    
2566
    /* we need to initialize some time vars before we can encode b-frames */
2567
    if (s->h263_pred && !s->h263_msmpeg4)
2568
        ff_set_mpeg4_time(s, s->picture_number); 
2569

    
2570
    s->scene_change_score=0;
2571
    
2572
    s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2573

    
2574
    /* Estimate motion for every MB */
2575
    if(s->pict_type != I_TYPE){
2576
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2577
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2578
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2579
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2580
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2581
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2582
                s->mb_x = mb_x;
2583
                s->mb_y = mb_y;
2584
                s->block_index[0]+=2;
2585
                s->block_index[1]+=2;
2586
                s->block_index[2]+=2;
2587
                s->block_index[3]+=2;
2588

    
2589
                /* compute motion vector & mb_type and store in context */
2590
                if(s->pict_type==B_TYPE)
2591
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
2592
                else
2593
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
2594
//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
2595
            }
2596
        }
2597
    }else /* if(s->pict_type == I_TYPE) */{
2598
        /* I-Frame */
2599
        //FIXME do we need to zero them?
2600
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2601
        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
2602
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2603
        
2604
        if(!s->fixed_qscale){
2605
            /* finding spatial complexity for I-frame rate control */
2606
            for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2607
                for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2608
                    int xx = mb_x * 16;
2609
                    int yy = mb_y * 16;
2610
                    uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
2611
                    int varc;
2612
                    int sum = s->dsp.pix_sum(pix, s->linesize);
2613
    
2614
                    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
2615

    
2616
                    s->mb_var [s->mb_width * mb_y + mb_x] = varc;
2617
                    s->mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
2618
                    s->mb_var_sum    += varc;
2619
                }
2620
            }
2621
        }
2622
    }
2623
    emms_c();
2624

    
2625
    if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2626
        s->pict_type= I_TYPE;
2627
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2628
        if(s->max_b_frames==0){
2629
            s->input_pict_type= I_TYPE;
2630
            s->input_picture_in_gop_number=0;
2631
        }
2632
//printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
2633
    }
2634
    
2635
    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 
2636
        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2637
        ff_fix_long_p_mvs(s);
2638
    if(s->pict_type==B_TYPE){
2639
        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2640
        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2641

    
2642
        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2643
        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2644
        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2645
        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2646
    }
2647
    
2648
    if (s->fixed_qscale) 
2649
        s->frame_qscale = s->avctx->quality;
2650
    else
2651
        s->frame_qscale = ff_rate_estimate_qscale(s);
2652

    
2653
    if(s->adaptive_quant){
2654
        switch(s->codec_id){
2655
        case CODEC_ID_MPEG4:
2656
            ff_clean_mpeg4_qscales(s);
2657
            break;
2658
        case CODEC_ID_H263:
2659
        case CODEC_ID_H263P:
2660
            ff_clean_h263_qscales(s);
2661
            break;
2662
        }
2663

    
2664
        s->qscale= s->qscale_table[0];
2665
    }else
2666
        s->qscale= (int)(s->frame_qscale + 0.5);
2667
        
2668
    if (s->out_format == FMT_MJPEG) {
2669
        /* for mjpeg, we do include qscale in the matrix */
2670
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2671
        for(i=1;i<64;i++){
2672
            int j= s->idct_permutation[i];
2673

    
2674
            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2675
        }
2676
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, 
2677
                       s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
2678
    }
2679

    
2680
    s->last_bits= get_bit_count(&s->pb);
2681
    switch(s->out_format) {
2682
    case FMT_MJPEG:
2683
        mjpeg_picture_header(s);
2684
        break;
2685
    case FMT_H263:
2686
        if (s->h263_msmpeg4) 
2687
            msmpeg4_encode_picture_header(s, picture_number);
2688
        else if (s->h263_pred)
2689
            mpeg4_encode_picture_header(s, picture_number);
2690
        else if (s->h263_rv10) 
2691
            rv10_encode_picture_header(s, picture_number);
2692
        else
2693
            h263_encode_picture_header(s, picture_number);
2694
        break;
2695
    case FMT_MPEG1:
2696
        mpeg1_encode_picture_header(s, picture_number);
2697
        break;
2698
    }
2699
    bits= get_bit_count(&s->pb);
2700
    s->header_bits= bits - s->last_bits;
2701
    s->last_bits= bits;
2702
    s->mv_bits=0;
2703
    s->misc_bits=0;
2704
    s->i_tex_bits=0;
2705
    s->p_tex_bits=0;
2706
    s->i_count=0;
2707
    s->f_count=0;
2708
    s->b_count=0;
2709
    s->skip_count=0;
2710

    
2711
    /* init last dc values */
2712
    /* note: quant matrix value (8) is implied here */
2713
    s->last_dc[0] = 128;
2714
    s->last_dc[1] = 128;
2715
    s->last_dc[2] = 128;
2716
    s->mb_incr = 1;
2717
    s->last_mv[0][0][0] = 0;
2718
    s->last_mv[0][0][1] = 0;
2719

    
2720
    if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
2721
        s->gob_index = ff_h263_get_gob_height(s);
2722

    
2723
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2724
        ff_mpeg4_init_partitions(s);
2725

    
2726
    s->resync_mb_x=0;
2727
    s->resync_mb_y=0;
2728
    s->first_slice_line = 1;
2729
    s->ptr_lastgob = s->pb.buf;
2730
    s->ptr_last_mb_line = s->pb.buf;
2731
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2732
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2733
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2734
        
2735
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2736
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2737
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2738
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2739
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
2740
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
2741
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2742
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
2743
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
2744
//            int d;
2745
            int dmin=10000000;
2746

    
2747
            s->mb_x = mb_x;
2748
            s->mb_y = mb_y;
2749
            s->block_index[0]+=2;
2750
            s->block_index[1]+=2;
2751
            s->block_index[2]+=2;
2752
            s->block_index[3]+=2;
2753
            s->block_index[4]++;
2754
            s->block_index[5]++;
2755

    
2756
            /* write gob / video packet header  */
2757
            if(s->rtp_mode){
2758
                int current_packet_size, is_gob_start;
2759
                
2760
                current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
2761
                is_gob_start=0;
2762
                
2763
                if(s->codec_id==CODEC_ID_MPEG4){
2764
                    if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
2765
                       && s->mb_y + s->mb_x>0){
2766

    
2767
                        if(s->partitioned_frame){
2768
                            ff_mpeg4_merge_partitions(s);
2769
                            ff_mpeg4_init_partitions(s);
2770
                        }
2771
                        ff_mpeg4_encode_video_packet_header(s);
2772

    
2773
                        if(s->flags&CODEC_FLAG_PASS1){
2774
                            int bits= get_bit_count(&s->pb);
2775
                            s->misc_bits+= bits - s->last_bits;
2776
                            s->last_bits= bits;
2777
                        }
2778
                        ff_mpeg4_clean_buffers(s);
2779
                        is_gob_start=1;
2780
                    }
2781
                }else{
2782
                    if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
2783
                       && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
2784
                       
2785
                        h263_encode_gob_header(s, mb_y);                       
2786
                        is_gob_start=1;
2787
                    }
2788
                }
2789

    
2790
                if(is_gob_start){
2791
                    s->ptr_lastgob = pbBufPtr(&s->pb);
2792
                    s->first_slice_line=1;
2793
                    s->resync_mb_x=mb_x;
2794
                    s->resync_mb_y=mb_y;
2795
                }
2796
            }
2797

    
2798
            if(  (s->resync_mb_x   == s->mb_x)
2799
               && s->resync_mb_y+1 == s->mb_y){
2800
                s->first_slice_line=0; 
2801
            }
2802

    
2803
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
2804
                int next_block=0;
2805
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2806

    
2807
                copy_context_before_encode(&backup_s, s, -1);
2808
                backup_s.pb= s->pb;
2809
                best_s.data_partitioning= s->data_partitioning;
2810
                best_s.partitioned_frame= s->partitioned_frame;
2811
                if(s->data_partitioning){
2812
                    backup_s.pb2= s->pb2;
2813
                    backup_s.tex_pb= s->tex_pb;
2814
                }
2815

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

    
2979
            MPV_decode_mb(s, s->block);
2980
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
2981
        }
2982

    
2983

    
2984
        /* Obtain average mb_row size for RTP */
2985
        if (s->rtp_mode) {
2986
            if (mb_y==0)
2987
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
2988
            else {    
2989
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
2990
            }
2991
            s->ptr_last_mb_line = pbBufPtr(&s->pb);
2992
        }
2993
    }
2994
    emms_c();
2995

    
2996
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2997
        ff_mpeg4_merge_partitions(s);
2998

    
2999
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
3000
        msmpeg4_encode_ext_header(s);
3001

    
3002
    if(s->codec_id==CODEC_ID_MPEG4) 
3003
        ff_mpeg4_stuffing(&s->pb);
3004

    
3005
    //if (s->gob_number)
3006
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3007
    
3008
    /* Send the last GOB if RTP */    
3009
    if (s->rtp_mode) {
3010
        flush_put_bits(&s->pb);
3011
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3012
        /* Call the RTP callback to send the last GOB */
3013
        if (s->rtp_callback)
3014
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3015
        s->ptr_lastgob = pbBufPtr(&s->pb);
3016
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
3017
    }
3018
}
3019

    
3020
static int dct_quantize_c(MpegEncContext *s, 
3021
                        DCTELEM *block, int n,
3022
                        int qscale, int *overflow)
3023
{
3024
    int i, j, level, last_non_zero, q;
3025
    const int *qmat;
3026
    const UINT8 *scantable= s->intra_scantable.scantable;
3027
    int bias;
3028
    int max=0;
3029
    unsigned int threshold1, threshold2;
3030

    
3031
    s->fdct (block);
3032

    
3033
    if (s->mb_intra) {
3034
        if (!s->h263_aic) {
3035
            if (n < 4)
3036
                q = s->y_dc_scale;
3037
            else
3038
                q = s->c_dc_scale;
3039
            q = q << 3;
3040
        } else
3041
            /* For AIC we skip quant/dequant of INTRADC */
3042
            q = 1 << 3;
3043
            
3044
        /* note: block[0] is assumed to be positive */
3045
        block[0] = (block[0] + (q >> 1)) / q;
3046
        i = 1;
3047
        last_non_zero = 0;
3048
        qmat = s->q_intra_matrix[qscale];
3049
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3050
    } else {
3051
        i = 0;
3052
        last_non_zero = -1;
3053
        qmat = s->q_inter_matrix[qscale];
3054
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3055
    }
3056
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3057
    threshold2= (threshold1<<1);
3058

    
3059
    for(;i<64;i++) {
3060
        j = scantable[i];
3061
        level = block[j];
3062
        level = level * qmat[j];
3063

    
3064
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3065
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3066
        if(((unsigned)(level+threshold1))>threshold2){
3067
            if(level>0){
3068
                level= (bias + level)>>QMAT_SHIFT;
3069
                block[j]= level;
3070
            }else{
3071
                level= (bias - level)>>QMAT_SHIFT;
3072
                block[j]= -level;
3073
            }
3074
            max |=level;
3075
            last_non_zero = i;
3076
        }else{
3077
            block[j]=0;
3078
        }
3079
    }
3080
    *overflow= s->max_qcoeff < max; //overflow might have happend
3081
    
3082
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3083
    ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
3084

    
3085
    return last_non_zero;
3086
}
3087

    
3088
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
3089
                                   DCTELEM *block, int n, int qscale)
3090
{
3091
    int i, level, nCoeffs;
3092
    const UINT16 *quant_matrix;
3093

    
3094
    nCoeffs= s->block_last_index[n];
3095
    
3096
    if (s->mb_intra) {
3097
        if (n < 4) 
3098
            block[0] = block[0] * s->y_dc_scale;
3099
        else
3100
            block[0] = block[0] * s->c_dc_scale;
3101
        /* XXX: only mpeg1 */
3102
        quant_matrix = s->intra_matrix;
3103
        for(i=1;i<=nCoeffs;i++) {
3104
            int j= s->intra_scantable.permutated[i];
3105
            level = block[j];
3106
            if (level) {
3107
                if (level < 0) {
3108
                    level = -level;
3109
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3110
                    level = (level - 1) | 1;
3111
                    level = -level;
3112
                } else {
3113
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3114
                    level = (level - 1) | 1;
3115
                }
3116
#ifdef PARANOID
3117
                if (level < -2048 || level > 2047)
3118
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3119
#endif
3120
                block[j] = level;
3121
            }
3122
        }
3123
    } else {
3124
        i = 0;
3125
        quant_matrix = s->inter_matrix;
3126
        for(;i<=nCoeffs;i++) {
3127
            int j= s->intra_scantable.permutated[i];
3128
            level = block[j];
3129
            if (level) {
3130
                if (level < 0) {
3131
                    level = -level;
3132
                    level = (((level << 1) + 1) * qscale *
3133
                             ((int) (quant_matrix[j]))) >> 4;
3134
                    level = (level - 1) | 1;
3135
                    level = -level;
3136
                } else {
3137
                    level = (((level << 1) + 1) * qscale *
3138
                             ((int) (quant_matrix[j]))) >> 4;
3139
                    level = (level - 1) | 1;
3140
                }
3141
#ifdef PARANOID
3142
                if (level < -2048 || level > 2047)
3143
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3144
#endif
3145
                block[j] = level;
3146
            }
3147
        }
3148
    }
3149
}
3150

    
3151
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
3152
                                   DCTELEM *block, int n, int qscale)
3153
{
3154
    int i, level, nCoeffs;
3155
    const UINT16 *quant_matrix;
3156

    
3157
    if(s->alternate_scan) nCoeffs= 63;
3158
    else nCoeffs= s->block_last_index[n];
3159
    
3160
    if (s->mb_intra) {
3161
        if (n < 4) 
3162
            block[0] = block[0] * s->y_dc_scale;
3163
        else
3164
            block[0] = block[0] * s->c_dc_scale;
3165
        quant_matrix = s->intra_matrix;
3166
        for(i=1;i<=nCoeffs;i++) {
3167
            int j= s->intra_scantable.permutated[i];
3168
            level = block[j];
3169
            if (level) {
3170
                if (level < 0) {
3171
                    level = -level;
3172
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3173
                    level = -level;
3174
                } else {
3175
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
3176
                }
3177
#ifdef PARANOID
3178
                if (level < -2048 || level > 2047)
3179
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3180
#endif
3181
                block[j] = level;
3182
            }
3183
        }
3184
    } else {
3185
        int sum=-1;
3186
        i = 0;
3187
        quant_matrix = s->inter_matrix;
3188
        for(;i<=nCoeffs;i++) {
3189
            int j= s->intra_scantable.permutated[i];
3190
            level = block[j];
3191
            if (level) {
3192
                if (level < 0) {
3193
                    level = -level;
3194
                    level = (((level << 1) + 1) * qscale *
3195
                             ((int) (quant_matrix[j]))) >> 4;
3196
                    level = -level;
3197
                } else {
3198
                    level = (((level << 1) + 1) * qscale *
3199
                             ((int) (quant_matrix[j]))) >> 4;
3200
                }
3201
#ifdef PARANOID
3202
                if (level < -2048 || level > 2047)
3203
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3204
#endif
3205
                block[j] = level;
3206
                sum+=level;
3207
            }
3208
        }
3209
        block[63]^=sum&1;
3210
    }
3211
}
3212

    
3213

    
3214
static void dct_unquantize_h263_c(MpegEncContext *s, 
3215
                                  DCTELEM *block, int n, int qscale)
3216
{
3217
    int i, level, qmul, qadd;
3218
    int nCoeffs;
3219
    
3220
    assert(s->block_last_index[n]>=0);
3221
    
3222
    qadd = (qscale - 1) | 1;
3223
    qmul = qscale << 1;
3224
    
3225
    if (s->mb_intra) {
3226
        if (!s->h263_aic) {
3227
            if (n < 4) 
3228
                block[0] = block[0] * s->y_dc_scale;
3229
            else
3230
                block[0] = block[0] * s->c_dc_scale;
3231
        }else
3232
            qadd = 0;
3233
        i = 1;
3234
        nCoeffs= 63; //does not allways use zigzag table 
3235
    } else {
3236
        i = 0;
3237
        nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
3238
    }
3239

    
3240
    for(;i<=nCoeffs;i++) {
3241
        level = block[i];
3242
        if (level) {
3243
            if (level < 0) {
3244
                level = level * qmul - qadd;
3245
            } else {
3246
                level = level * qmul + qadd;
3247
            }
3248
#ifdef PARANOID
3249
                if (level < -2048 || level > 2047)
3250
                    fprintf(stderr, "unquant error %d %d\n", i, level);
3251
#endif
3252
            block[i] = level;
3253
        }
3254
    }
3255
}
3256

    
3257
AVCodec mpeg1video_encoder = {
3258
    "mpeg1video",
3259
    CODEC_TYPE_VIDEO,
3260
    CODEC_ID_MPEG1VIDEO,
3261
    sizeof(MpegEncContext),
3262
    MPV_encode_init,
3263
    MPV_encode_picture,
3264
    MPV_encode_end,
3265
};
3266

    
3267
AVCodec h263_encoder = {
3268
    "h263",
3269
    CODEC_TYPE_VIDEO,
3270
    CODEC_ID_H263,
3271
    sizeof(MpegEncContext),
3272
    MPV_encode_init,
3273
    MPV_encode_picture,
3274
    MPV_encode_end,
3275
};
3276

    
3277
AVCodec h263p_encoder = {
3278
    "h263p",
3279
    CODEC_TYPE_VIDEO,
3280
    CODEC_ID_H263P,
3281
    sizeof(MpegEncContext),
3282
    MPV_encode_init,
3283
    MPV_encode_picture,
3284
    MPV_encode_end,
3285
};
3286

    
3287
AVCodec rv10_encoder = {
3288
    "rv10",
3289
    CODEC_TYPE_VIDEO,
3290
    CODEC_ID_RV10,
3291
    sizeof(MpegEncContext),
3292
    MPV_encode_init,
3293
    MPV_encode_picture,
3294
    MPV_encode_end,
3295
};
3296

    
3297
AVCodec mjpeg_encoder = {
3298
    "mjpeg",
3299
    CODEC_TYPE_VIDEO,
3300
    CODEC_ID_MJPEG,
3301
    sizeof(MpegEncContext),
3302
    MPV_encode_init,
3303
    MPV_encode_picture,
3304
    MPV_encode_end,
3305
};
3306

    
3307
AVCodec mpeg4_encoder = {
3308
    "mpeg4",
3309
    CODEC_TYPE_VIDEO,
3310
    CODEC_ID_MPEG4,
3311
    sizeof(MpegEncContext),
3312
    MPV_encode_init,
3313
    MPV_encode_picture,
3314
    MPV_encode_end,
3315
};
3316

    
3317
AVCodec msmpeg4v1_encoder = {
3318
    "msmpeg4v1",
3319
    CODEC_TYPE_VIDEO,
3320
    CODEC_ID_MSMPEG4V1,
3321
    sizeof(MpegEncContext),
3322
    MPV_encode_init,
3323
    MPV_encode_picture,
3324
    MPV_encode_end,
3325
};
3326

    
3327
AVCodec msmpeg4v2_encoder = {
3328
    "msmpeg4v2",
3329
    CODEC_TYPE_VIDEO,
3330
    CODEC_ID_MSMPEG4V2,
3331
    sizeof(MpegEncContext),
3332
    MPV_encode_init,
3333
    MPV_encode_picture,
3334
    MPV_encode_end,
3335
};
3336

    
3337
AVCodec msmpeg4v3_encoder = {
3338
    "msmpeg4",
3339
    CODEC_TYPE_VIDEO,
3340
    CODEC_ID_MSMPEG4V3,
3341
    sizeof(MpegEncContext),
3342
    MPV_encode_init,
3343
    MPV_encode_picture,
3344
    MPV_encode_end,
3345
};
3346

    
3347
AVCodec wmv1_encoder = {
3348
    "wmv1",
3349
    CODEC_TYPE_VIDEO,
3350
    CODEC_ID_WMV1,
3351
    sizeof(MpegEncContext),
3352
    MPV_encode_init,
3353
    MPV_encode_picture,
3354
    MPV_encode_end,
3355
};
3356

    
3357
AVCodec wmv2_encoder = {
3358
    "wmv2",
3359
    CODEC_TYPE_VIDEO,
3360
    CODEC_ID_WMV2,
3361
    sizeof(MpegEncContext),
3362
    MPV_encode_init,
3363
    MPV_encode_picture,
3364
    MPV_encode_end,
3365
};