Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ b50eef3a

History | View | Annotate | Download (88.4 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
#include "avcodec.h"
22
#include "dsputil.h"
23
#include "mpegvideo.h"
24

    
25
#ifdef USE_FASTMEMCPY
26
#include "fastmemcpy.h"
27
#endif
28

    
29
static void encode_picture(MpegEncContext *s, int picture_number);
30
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
31
                                   DCTELEM *block, int n, int qscale);
32
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
33
                                   DCTELEM *block, int n, int qscale);
34
static void dct_unquantize_h263_c(MpegEncContext *s, 
35
                                  DCTELEM *block, int n, int qscale);
36
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
37
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
38

    
39
int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
40
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
41

    
42
#define EDGE_WIDTH 16
43

    
44
/* enable all paranoid tests for rounding, overflows, etc... */
45
//#define PARANOID
46

    
47
//#define DEBUG
48

    
49

    
50
/* for jpeg fast DCT */
51
#define CONST_BITS 14
52

    
53
static const unsigned short aanscales[64] = {
54
    /* precomputed values scaled up by 14 bits */
55
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
56
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
57
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
58
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
59
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
60
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
61
    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
62
    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
63
};
64

    
65
static UINT8 h263_chroma_roundtab[16] = {
66
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
67
};
68

    
69
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
70
static UINT8 default_fcode_tab[MAX_MV*2+1];
71

    
72
extern UINT8 zigzag_end[64];
73

    
74
/* default motion estimation */
75
int motion_estimation_method = ME_EPZS;
76

    
77
static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
78
                           const UINT16 *quant_matrix, int bias)
79
{
80
    int qscale;
81

    
82
    for(qscale=1; qscale<32; qscale++){
83
        int i;
84
        if (av_fdct == fdct_ifast) {
85
            for(i=0;i<64;i++) {
86
                const int j= block_permute_op(i);
87
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
88
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
89
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
90
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
91
                
92
                qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / 
93
                                (aanscales[i] * qscale * quant_matrix[j]));
94
            }
95
        } else {
96
            for(i=0;i<64;i++) {
97
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
98
                   So 16           <= qscale * quant_matrix[i]             <= 7905
99
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
100
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
101
                */
102
                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
103
                qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
104

    
105
                if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
106

    
107
                qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
108
            }
109
        }
110
    }
111
}
112
// move into common.c perhaps 
113
#define CHECKED_ALLOCZ(p, size)\
114
{\
115
    p= av_mallocz(size);\
116
    if(p==NULL){\
117
        perror("malloc");\
118
        goto fail;\
119
    }\
120
}
121

    
122
/* init common structure for both encoder and decoder */
123
int MPV_common_init(MpegEncContext *s)
124
{
125
    int c_size, i;
126
    UINT8 *pict;
127

    
128
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
129
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
130
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
131
        
132
#ifdef HAVE_MMX
133
    MPV_common_init_mmx(s);
134
#endif
135
    //setup default unquantizers (mpeg4 might change it later)
136
    if(s->out_format == FMT_H263)
137
        s->dct_unquantize = s->dct_unquantize_h263;
138
    else
139
        s->dct_unquantize = s->dct_unquantize_mpeg1;
140
    
141
    s->mb_width = (s->width + 15) / 16;
142
    s->mb_height = (s->height + 15) / 16;
143
    s->mb_num = s->mb_width * s->mb_height;
144
    s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
145

    
146
    for(i=0;i<3;i++) {
147
        int w, h, shift, pict_start;
148

    
149
        w = s->linesize;
150
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
151
        shift = (i == 0) ? 0 : 1;
152
        c_size = (w >> shift) * (h >> shift);
153
        pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
154

    
155
        CHECKED_ALLOCZ(pict, c_size)
156
        s->last_picture_base[i] = pict;
157
        s->last_picture[i] = pict + pict_start;
158
        if(i>0) memset(s->last_picture_base[i], 128, c_size);
159
    
160
        CHECKED_ALLOCZ(pict, c_size)
161
        s->next_picture_base[i] = pict;
162
        s->next_picture[i] = pict + pict_start;
163
        if(i>0) memset(s->next_picture_base[i], 128, c_size);
164
        
165
        if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
166
        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but 
167
           do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
168
            CHECKED_ALLOCZ(pict, c_size)
169
            s->aux_picture_base[i] = pict;
170
            s->aux_picture[i] = pict + pict_start;
171
            if(i>0) memset(s->aux_picture_base[i], 128, c_size);
172
        }
173
    }
174
    
175
    if (s->encoding) {
176
        int j;
177
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
178
        
179
        CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))
180
        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
181

    
182
        /* Allocate MV tables */
183
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))
184
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))
185
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))
186
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
187
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
188
        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
189
        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
190
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))
191

    
192
        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))
193
        
194
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
195
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
196

    
197
        if(s->max_b_frames){
198
            for(j=0; j<REORDER_BUFFER_SIZE; j++){
199
                int i;
200
                for(i=0;i<3;i++) {
201
                    int w, h, shift;
202

    
203
                    w = s->linesize;
204
                    h = s->mb_height * 16;
205
                    shift = (i == 0) ? 0 : 1;
206
                    c_size = (w >> shift) * (h >> shift);
207

    
208
                    CHECKED_ALLOCZ(pict, c_size);
209
                    s->picture_buffer[j][i] = pict;
210
                }
211
            }
212
        }
213

    
214
        if(s->codec_id==CODEC_ID_MPEG4){
215
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
216
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
217
        }
218
    }
219
    
220
    if (s->out_format == FMT_H263 || s->encoding) {
221
        int size;
222
        /* Allocate MB type table */
223
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))
224

    
225
        /* MV prediction */
226
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
227
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
228
    }
229

    
230
    if (s->h263_pred || s->h263_plus) {
231
        int y_size, c_size, i, size;
232
        
233
        /* dc values */
234

    
235
        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
236
        c_size = (s->mb_width + 2) * (s->mb_height + 2);
237
        size = y_size + 2 * c_size;
238
        CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
239
        s->dc_val[1] = s->dc_val[0] + y_size;
240
        s->dc_val[2] = s->dc_val[1] + c_size;
241
        for(i=0;i<size;i++)
242
            s->dc_val[0][i] = 1024;
243

    
244
        /* ac values */
245
        CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
246
        s->ac_val[1] = s->ac_val[0] + y_size;
247
        s->ac_val[2] = s->ac_val[1] + c_size;
248
        
249
        /* cbp values */
250
        CHECKED_ALLOCZ(s->coded_block, y_size);
251

    
252
        /* which mb is a intra block */
253
        CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
254
        memset(s->mbintra_table, 1, s->mb_num);
255
        
256
        /* divx501 bitstream reorder buffer */
257
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
258
        
259
        /* cbp, ac_pred, pred_dir */
260
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
261
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
262
        
263
        CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))
264
    }
265
    /* default structure is frame */
266
    s->picture_structure = PICT_FRAME;
267

    
268
    /* init macroblock skip table */
269
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
270
    
271
    s->block= s->blocks[0];
272

    
273
    s->context_initialized = 1;
274
    return 0;
275
 fail:
276
    MPV_common_end(s);
277
    return -1;
278
}
279

    
280

    
281
//extern int sads;
282

    
283
/* init common structure for both encoder and decoder */
284
void MPV_common_end(MpegEncContext *s)
285
{
286
    int i;
287

    
288
    av_freep(&s->mb_type);
289
    av_freep(&s->mb_var);
290
    av_freep(&s->mc_mb_var);
291
    av_freep(&s->p_mv_table);
292
    av_freep(&s->b_forw_mv_table);
293
    av_freep(&s->b_back_mv_table);
294
    av_freep(&s->b_bidir_forw_mv_table);
295
    av_freep(&s->b_bidir_back_mv_table);
296
    av_freep(&s->b_direct_forw_mv_table);
297
    av_freep(&s->b_direct_back_mv_table);
298
    av_freep(&s->b_direct_mv_table);
299
    av_freep(&s->motion_val);
300
    av_freep(&s->dc_val[0]);
301
    av_freep(&s->ac_val[0]);
302
    av_freep(&s->coded_block);
303
    av_freep(&s->mbintra_table);
304
    av_freep(&s->cbp_table);
305
    av_freep(&s->pred_dir_table);
306
    av_freep(&s->qscale_table);
307
    av_freep(&s->me_scratchpad);
308
    av_freep(&s->me_map);
309
    av_freep(&s->me_score_map);
310
    
311
    av_freep(&s->mbskip_table);
312
    av_freep(&s->bitstream_buffer);
313
    av_freep(&s->tex_pb_buffer);
314
    av_freep(&s->pb2_buffer);
315
    for(i=0;i<3;i++) {
316
        int j;
317
        av_freep(&s->last_picture_base[i]);
318
        av_freep(&s->next_picture_base[i]);
319
        av_freep(&s->aux_picture_base[i]);
320
        for(j=0; j<REORDER_BUFFER_SIZE; j++){
321
            av_freep(&s->picture_buffer[j][i]);
322
        }
323
    }
324
    s->context_initialized = 0;
325
}
326

    
327
/* init video encoder */
328
int MPV_encode_init(AVCodecContext *avctx)
329
{
330
    MpegEncContext *s = avctx->priv_data;
331
    int i;
332

    
333
    avctx->pix_fmt = PIX_FMT_YUV420P;
334

    
335
    s->bit_rate = avctx->bit_rate;
336
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
337
    s->frame_rate = avctx->frame_rate;
338
    s->width = avctx->width;
339
    s->height = avctx->height;
340
    if(avctx->gop_size > 600){
341
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
342
        avctx->gop_size=600;
343
    }
344
    s->gop_size = avctx->gop_size;
345
    s->rtp_mode = avctx->rtp_mode;
346
    s->rtp_payload_size = avctx->rtp_payload_size;
347
    if (avctx->rtp_callback)
348
        s->rtp_callback = avctx->rtp_callback;
349
    s->qmin= avctx->qmin;
350
    s->qmax= avctx->qmax;
351
    s->max_qdiff= avctx->max_qdiff;
352
    s->qcompress= avctx->qcompress;
353
    s->qblur= avctx->qblur;
354
    s->b_quant_factor= avctx->b_quant_factor;
355
    s->b_quant_offset= avctx->b_quant_offset;
356
    s->avctx = avctx;
357
    s->aspect_ratio_info= avctx->aspect_ratio_info;
358
    s->flags= avctx->flags;
359
    s->max_b_frames= avctx->max_b_frames;
360
    s->rc_strategy= avctx->rc_strategy;
361
    s->b_frame_strategy= avctx->b_frame_strategy;
362
    s->codec_id= avctx->codec->id;
363
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
364
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
365
    s->strict_std_compliance= avctx->strict_std_compliance;
366
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
367

    
368
    if (s->gop_size <= 1) {
369
        s->intra_only = 1;
370
        s->gop_size = 12;
371
    } else {
372
        s->intra_only = 0;
373
    }
374
    
375
    /* ME algorithm */
376
    if (avctx->me_method == 0)
377
        /* For compatibility */
378
        s->me_method = motion_estimation_method;
379
    else
380
        s->me_method = avctx->me_method;
381
        
382
    /* Fixed QSCALE */
383
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
384
    
385
    switch(avctx->codec->id) {
386
    case CODEC_ID_MPEG1VIDEO:
387
        s->out_format = FMT_MPEG1;
388
        avctx->delay=0; //FIXME not sure, should check the spec
389
        break;
390
    case CODEC_ID_MJPEG:
391
        s->out_format = FMT_MJPEG;
392
        s->intra_only = 1; /* force intra only for jpeg */
393
        s->mjpeg_write_tables = 1; /* write all tables */
394
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
395
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
396
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
397
        s->mjpeg_vsample[2] = 1; 
398
        s->mjpeg_hsample[0] = 2;
399
        s->mjpeg_hsample[1] = 1; 
400
        s->mjpeg_hsample[2] = 1; 
401
        if (mjpeg_init(s) < 0)
402
            return -1;
403
        avctx->delay=0;
404
        break;
405
    case CODEC_ID_H263:
406
        if (h263_get_picture_format(s->width, s->height) == 7) {
407
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
408
            return -1;
409
        }
410
        s->out_format = FMT_H263;
411
        avctx->delay=0;
412
        break;
413
    case CODEC_ID_H263P:
414
        s->out_format = FMT_H263;
415
        s->rtp_mode = 1;
416
        s->rtp_payload_size = 1200; 
417
        s->h263_plus = 1;
418
        s->unrestricted_mv = 1;
419
        s->h263_aic = 1;
420
        
421
        /* These are just to be sure */
422
        s->umvplus = 0;
423
        s->umvplus_dec = 0;
424
        avctx->delay=0;
425
        break;
426
    case CODEC_ID_RV10:
427
        s->out_format = FMT_H263;
428
        s->h263_rv10 = 1;
429
        avctx->delay=0;
430
        break;
431
    case CODEC_ID_MPEG4:
432
        s->out_format = FMT_H263;
433
        s->h263_pred = 1;
434
        s->unrestricted_mv = 1;
435
        s->has_b_frames= s->max_b_frames ? 1 : 0;
436
        s->low_delay=0;
437
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); 
438
        break;
439
    case CODEC_ID_MSMPEG4V1:
440
        s->out_format = FMT_H263;
441
        s->h263_msmpeg4 = 1;
442
        s->h263_pred = 1;
443
        s->unrestricted_mv = 1;
444
        s->msmpeg4_version= 1;
445
        avctx->delay=0;
446
        break;
447
    case CODEC_ID_MSMPEG4V2:
448
        s->out_format = FMT_H263;
449
        s->h263_msmpeg4 = 1;
450
        s->h263_pred = 1;
451
        s->unrestricted_mv = 1;
452
        s->msmpeg4_version= 2;
453
        avctx->delay=0;
454
        break;
455
    case CODEC_ID_MSMPEG4V3:
456
        s->out_format = FMT_H263;
457
        s->h263_msmpeg4 = 1;
458
        s->h263_pred = 1;
459
        s->unrestricted_mv = 1;
460
        s->msmpeg4_version= 3;
461
        avctx->delay=0;
462
        break;
463
    default:
464
        return -1;
465
    }
466
    
467
    { /* set up some save defaults, some codecs might override them later */
468
        static int done=0;
469
        if(!done){
470
            int i;
471
            done=1;
472
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
473
            memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
474

    
475
            for(i=-16; i<16; i++){
476
                default_fcode_tab[i + MAX_MV]= 1;
477
            }
478
        }
479
    }
480
    s->mv_penalty= default_mv_penalty;
481
    s->fcode_tab= default_fcode_tab;
482

    
483
    if (s->out_format == FMT_H263)
484
        h263_encode_init(s);
485
    else if (s->out_format == FMT_MPEG1)
486
        mpeg1_encode_init(s);
487

    
488
    /* dont use mv_penalty table for crap MV as it would be confused */
489
    if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
490

    
491
    s->encoding = 1;
492

    
493
    /* init */
494
    if (MPV_common_init(s) < 0)
495
        return -1;
496
    
497
    /* init default q matrix */
498
    for(i=0;i<64;i++) {
499
        if(s->out_format == FMT_H263)
500
            s->intra_matrix[i] = default_non_intra_matrix[i];
501
        else
502
            s->intra_matrix[i] = default_intra_matrix[i];
503

    
504
        s->inter_matrix[i] = default_non_intra_matrix[i];
505
    }
506

    
507
    /* precompute matrix */
508
    /* for mjpeg, we do include qscale in the matrix */
509
    if (s->out_format != FMT_MJPEG) {
510
        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
511
                       s->intra_matrix, s->intra_quant_bias);
512
        convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
513
                       s->inter_matrix, s->inter_quant_bias);
514
    }
515

    
516
    if(ff_rate_control_init(s) < 0)
517
        return -1;
518

    
519
    s->picture_number = 0;
520
    s->picture_in_gop_number = 0;
521
    s->fake_picture_number = 0;
522
    /* motion detector init */
523
    s->f_code = 1;
524
    s->b_code = 1;
525

    
526
    return 0;
527
}
528

    
529
int MPV_encode_end(AVCodecContext *avctx)
530
{
531
    MpegEncContext *s = avctx->priv_data;
532

    
533
#ifdef STATS
534
    print_stats();
535
#endif
536

    
537
    ff_rate_control_uninit(s);
538

    
539
    MPV_common_end(s);
540
    if (s->out_format == FMT_MJPEG)
541
        mjpeg_close(s);
542
      
543
    return 0;
544
}
545

    
546
/* draw the edges of width 'w' of an image of size width, height */
547
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
548
{
549
    UINT8 *ptr, *last_line;
550
    int i;
551

    
552
    last_line = buf + (height - 1) * wrap;
553
    for(i=0;i<w;i++) {
554
        /* top and bottom */
555
        memcpy(buf - (i + 1) * wrap, buf, width);
556
        memcpy(last_line + (i + 1) * wrap, last_line, width);
557
    }
558
    /* left and right */
559
    ptr = buf;
560
    for(i=0;i<height;i++) {
561
        memset(ptr - w, ptr[0], w);
562
        memset(ptr + width, ptr[width-1], w);
563
        ptr += wrap;
564
    }
565
    /* corners */
566
    for(i=0;i<w;i++) {
567
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
568
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
569
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
570
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
571
    }
572
}
573

    
574
/* generic function for encode/decode called before a frame is coded/decoded */
575
void MPV_frame_start(MpegEncContext *s)
576
{
577
    int i;
578
    UINT8 *tmp;
579

    
580
    s->mb_skiped = 0;
581
    s->decoding_error=0;
582

    
583
    if (s->pict_type == B_TYPE) {
584
        for(i=0;i<3;i++) {
585
            s->current_picture[i] = s->aux_picture[i];
586
        }
587
    } else {
588
        for(i=0;i<3;i++) {
589
            /* swap next and last */
590
            tmp = s->last_picture[i];
591
            s->last_picture[i] = s->next_picture[i];
592
            s->next_picture[i] = tmp;
593
            s->current_picture[i] = tmp;
594
        }
595
    }
596
}
597

    
598
/* generic function for encode/decode called after a frame has been coded/decoded */
599
void MPV_frame_end(MpegEncContext *s)
600
{
601
//    if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
602

    
603
    /* draw edge for correct motion prediction if outside */
604
    if (s->pict_type != B_TYPE && !s->intra_only) {
605
      if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version>=500){
606
        draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
607
        draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
608
        draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
609
      }else{
610
        /* mpeg4? / opendivx / xvid */
611
        draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
612
        draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
613
        draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
614
      }
615
    }
616
    emms_c();
617
    
618
    if(s->pict_type!=B_TYPE){
619
        s->last_non_b_pict_type= s->pict_type;
620
        s->last_non_b_qscale= s->qscale;
621
        s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
622
        s->num_available_buffers++;
623
        if(s->num_available_buffers>2) s->num_available_buffers= 2;
624
    }
625
}
626

    
627
/* reorder input for encoding */
628
void reorder_input(MpegEncContext *s, AVPicture *pict)
629
{
630
    int i, j, index;
631
            
632
    if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
633

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

    
636
    for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
637
        s->coded_order[j]= s->coded_order[j+1];
638
    }
639
    s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
640
    s->coded_order[j].pict_type=0;
641

    
642
    switch(s->input_pict_type){
643
    default: 
644
    case I_TYPE:
645
    case S_TYPE:
646
    case P_TYPE:
647
        index= s->max_b_frames - s->b_frames_since_non_b;
648
        s->b_frames_since_non_b=0;
649
        break;            
650
    case B_TYPE:
651
        index= s->max_b_frames + 1;
652
        s->b_frames_since_non_b++;
653
        break;          
654
    }
655
//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
656
    if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
657
       && pict->linesize[0] == s->linesize
658
       && pict->linesize[1] == s->linesize>>1
659
       && pict->linesize[2] == s->linesize>>1){
660
//printf("ptr\n");
661
        for(i=0; i<3; i++){
662
            s->coded_order[index].picture[i]= pict->data[i];
663
        }
664
    }else{
665
//printf("copy\n");
666
        for(i=0; i<3; i++){
667
            uint8_t *src = pict->data[i];
668
            uint8_t *dest;
669
            int src_wrap = pict->linesize[i];
670
            int dest_wrap = s->linesize;
671
            int w = s->width;
672
            int h = s->height;
673

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

    
677
            if (i >= 1) {
678
                dest_wrap >>= 1;
679
                w >>= 1;
680
                h >>= 1;
681
            }
682

    
683
            s->coded_order[index].picture[i]= dest;
684
            for(j=0;j<h;j++) {
685
                memcpy(dest, src, w);
686
                dest += dest_wrap;
687
                src += src_wrap;
688
            }
689
        }
690
        if(index!=0){
691
            s->picture_buffer_index++;
692
            if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
693
        }
694
    }
695
    s->coded_order[index].pict_type = s->input_pict_type;
696
    s->coded_order[index].qscale    = s->input_qscale;
697
    s->coded_order[index].force_type= s->force_input_type;
698
    s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
699
    s->coded_order[index].picture_number= s->input_picture_number;
700

    
701
    for(i=0; i<3; i++){
702
        s->new_picture[i]= s->coded_order[0].picture[i];
703
    }
704
}
705

    
706
int MPV_encode_picture(AVCodecContext *avctx,
707
                       unsigned char *buf, int buf_size, void *data)
708
{
709
    MpegEncContext *s = avctx->priv_data;
710
    AVPicture *pict = data;
711

    
712
    s->input_qscale = avctx->quality;
713

    
714
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
715

    
716
    if(avctx->flags&CODEC_FLAG_TYPE){
717
        s->input_pict_type=
718
        s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
719
    }else if(s->flags&CODEC_FLAG_PASS2){
720
        s->input_pict_type=
721
        s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
722
    }else{
723
        s->force_input_type=0;
724
        if (!s->intra_only) {
725
            /* first picture of GOP is intra */
726
            if (s->input_picture_in_gop_number % s->gop_size==0){
727
                s->input_pict_type = I_TYPE;
728
            }else if(s->max_b_frames==0){
729
                s->input_pict_type = P_TYPE;
730
            }else{
731
                if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
732
                    s->input_pict_type = B_TYPE;
733
                else
734
                    s->input_pict_type = P_TYPE;
735
            }
736
        } else {
737
            s->input_pict_type = I_TYPE;
738
        }
739
    }
740

    
741
    if(s->input_pict_type==I_TYPE)
742
        s->input_picture_in_gop_number=0;
743
    
744
    reorder_input(s, pict);
745
    
746
    /* output? */
747
    if(s->coded_order[0].picture[0]){
748

    
749
        s->pict_type= s->coded_order[0].pict_type;
750
        if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
751
            s->qscale= s->coded_order[0].qscale;
752
        s->force_type= s->coded_order[0].force_type;
753
        s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
754
        s->picture_number= s->coded_order[0].picture_number;
755

    
756
        MPV_frame_start(s);
757

    
758
        encode_picture(s, s->picture_number);
759
        avctx->key_frame   = (s->pict_type == I_TYPE);
760
        avctx->pict_type   = s->pict_type;
761
        avctx->real_pict_num  = s->picture_number;
762
        avctx->header_bits = s->header_bits;
763
        avctx->mv_bits     = s->mv_bits;
764
        avctx->misc_bits   = s->misc_bits;
765
        avctx->i_tex_bits  = s->i_tex_bits;
766
        avctx->p_tex_bits  = s->p_tex_bits;
767
        avctx->i_count     = s->i_count;
768
        avctx->p_count     = s->p_count;
769
        avctx->skip_count  = s->skip_count;
770

    
771
        MPV_frame_end(s);
772

    
773
        if (s->out_format == FMT_MJPEG)
774
            mjpeg_picture_trailer(s);
775

    
776
        avctx->quality = s->qscale;
777
        
778
        if(s->flags&CODEC_FLAG_PASS1)
779
            ff_write_pass1_stats(s);
780
    
781
    }
782

    
783
    s->input_picture_number++;
784
    s->input_picture_in_gop_number++;
785

    
786
    flush_put_bits(&s->pb);
787
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
788
    if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
789
    else                     s->pb_frame_bits= s->frame_bits;
790

    
791
    s->total_bits += s->frame_bits;
792
    avctx->frame_bits  = s->frame_bits;
793
//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
794
//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);
795

    
796
    if (avctx->get_psnr) {
797
        /* At this point pict->data should have the original frame   */
798
        /* an s->current_picture should have the coded/decoded frame */
799
        get_psnr(pict->data, s->current_picture,
800
                 pict->linesize, s->linesize, avctx);
801
//        printf("%f\n", avctx->psnr_y);
802
    }
803
    return pbBufPtr(&s->pb) - s->pb.buf;
804
}
805

    
806
static inline void gmc1_motion(MpegEncContext *s,
807
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
808
                               int dest_offset,
809
                               UINT8 **ref_picture, int src_offset,
810
                               int h)
811
{
812
    UINT8 *ptr;
813
    int offset, src_x, src_y, linesize;
814
    int motion_x, motion_y;
815

    
816
    if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
817
    motion_x= s->sprite_offset[0][0];
818
    motion_y= s->sprite_offset[0][1];
819
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
820
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
821
    motion_x<<=(3-s->sprite_warping_accuracy);
822
    motion_y<<=(3-s->sprite_warping_accuracy);
823
    src_x = clip(src_x, -16, s->width);
824
    if (src_x == s->width)
825
        motion_x =0;
826
    src_y = clip(src_y, -16, s->height);
827
    if (src_y == s->height)
828
        motion_y =0;
829
    
830
    linesize = s->linesize;
831
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
832

    
833
    dest_y+=dest_offset;
834
    gmc1(dest_y  , ptr  , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
835
    gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
836

    
837
    motion_x= s->sprite_offset[1][0];
838
    motion_y= s->sprite_offset[1][1];
839
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
840
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
841
    motion_x<<=(3-s->sprite_warping_accuracy);
842
    motion_y<<=(3-s->sprite_warping_accuracy);
843
    src_x = clip(src_x, -8, s->width>>1);
844
    if (src_x == s->width>>1)
845
        motion_x =0;
846
    src_y = clip(src_y, -8, s->height>>1);
847
    if (src_y == s->height>>1)
848
        motion_y =0;
849

    
850
    offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
851
    ptr = ref_picture[1] + offset;
852
    gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
853
    ptr = ref_picture[2] + offset;
854
    gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
855
    
856
    return;
857
}
858

    
859
/* apply one mpeg motion vector to the three components */
860
static inline void mpeg_motion(MpegEncContext *s,
861
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
862
                               int dest_offset,
863
                               UINT8 **ref_picture, int src_offset,
864
                               int field_based, op_pixels_func *pix_op,
865
                               int motion_x, int motion_y, int h)
866
{
867
    UINT8 *ptr;
868
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
869
if(s->quarter_sample)
870
{
871
    motion_x>>=1;
872
    motion_y>>=1;
873
}
874
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
875
    src_x = s->mb_x * 16 + (motion_x >> 1);
876
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
877
                
878
    /* WARNING: do no forget half pels */
879
    height = s->height >> field_based;
880
    src_x = clip(src_x, -16, s->width);
881
    if (src_x == s->width)
882
        dxy &= ~1;
883
    src_y = clip(src_y, -16, height);
884
    if (src_y == height)
885
        dxy &= ~2;
886
    linesize = s->linesize << field_based;
887
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
888
    dest_y += dest_offset;
889
    pix_op[dxy](dest_y, ptr, linesize, h);
890
    pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
891

    
892
    if(s->flags&CODEC_FLAG_GRAY) return;
893

    
894
    if (s->out_format == FMT_H263) {
895
        dxy = 0;
896
        if ((motion_x & 3) != 0)
897
            dxy |= 1;
898
        if ((motion_y & 3) != 0)
899
            dxy |= 2;
900
        mx = motion_x >> 2;
901
        my = motion_y >> 2;
902
    } else {
903
        mx = motion_x / 2;
904
        my = motion_y / 2;
905
        dxy = ((my & 1) << 1) | (mx & 1);
906
        mx >>= 1;
907
        my >>= 1;
908
    }
909
    
910
    src_x = s->mb_x * 8 + mx;
911
    src_y = s->mb_y * (8 >> field_based) + my;
912
    src_x = clip(src_x, -8, s->width >> 1);
913
    if (src_x == (s->width >> 1))
914
        dxy &= ~1;
915
    src_y = clip(src_y, -8, height >> 1);
916
    if (src_y == (height >> 1))
917
        dxy &= ~2;
918

    
919
    offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
920
    ptr = ref_picture[1] + offset;
921
    pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
922
    ptr = ref_picture[2] + offset;
923
    pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
924
}
925

    
926
static inline void qpel_motion(MpegEncContext *s,
927
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
928
                               int dest_offset,
929
                               UINT8 **ref_picture, int src_offset,
930
                               int field_based, op_pixels_func *pix_op,
931
                               qpel_mc_func *qpix_op,
932
                               int motion_x, int motion_y, int h)
933
{
934
    UINT8 *ptr;
935
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
936

    
937
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
938
    src_x = s->mb_x * 16 + (motion_x >> 2);
939
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
940

    
941
    height = s->height >> field_based;
942
    src_x = clip(src_x, -16, s->width);
943
    if (src_x == s->width)
944
        dxy &= ~3;
945
    src_y = clip(src_y, -16, height);
946
    if (src_y == height)
947
        dxy &= ~12;
948
    linesize = s->linesize << field_based;
949
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
950
    dest_y += dest_offset;
951
//printf("%d %d %d\n", src_x, src_y, dxy);
952
    qpix_op[dxy](dest_y                 , ptr                 , linesize, linesize, motion_x&3, motion_y&3);
953
    qpix_op[dxy](dest_y              + 8, ptr              + 8, linesize, linesize, motion_x&3, motion_y&3);
954
    qpix_op[dxy](dest_y + linesize*8    , ptr + linesize*8    , linesize, linesize, motion_x&3, motion_y&3);
955
    qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
956
    
957
    if(s->flags&CODEC_FLAG_GRAY) return;
958

    
959
    mx= (motion_x>>1) | (motion_x&1);
960
    my= (motion_y>>1) | (motion_y&1);
961

    
962
    dxy = 0;
963
    if ((mx & 3) != 0)
964
        dxy |= 1;
965
    if ((my & 3) != 0)
966
        dxy |= 2;
967
    mx = mx >> 2;
968
    my = my >> 2;
969
    
970
    src_x = s->mb_x * 8 + mx;
971
    src_y = s->mb_y * (8 >> field_based) + my;
972
    src_x = clip(src_x, -8, s->width >> 1);
973
    if (src_x == (s->width >> 1))
974
        dxy &= ~1;
975
    src_y = clip(src_y, -8, height >> 1);
976
    if (src_y == (height >> 1))
977
        dxy &= ~2;
978

    
979
    offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
980
    ptr = ref_picture[1] + offset;
981
    pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
982
    ptr = ref_picture[2] + offset;
983
    pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
984
}
985

    
986

    
987
static inline void MPV_motion(MpegEncContext *s, 
988
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
989
                              int dir, UINT8 **ref_picture, 
990
                              op_pixels_func *pix_op, qpel_mc_func *qpix_op)
991
{
992
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
993
    int mb_x, mb_y, i;
994
    UINT8 *ptr, *dest;
995

    
996
    mb_x = s->mb_x;
997
    mb_y = s->mb_y;
998

    
999
    switch(s->mv_type) {
1000
    case MV_TYPE_16X16:
1001
        if(s->mcsel){
1002
#if 0
1003
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1004
                        ref_picture, 0,
1005
                        0, pix_op,
1006
                        s->sprite_offset[0][0]>>3,
1007
                        s->sprite_offset[0][1]>>3,
1008
                        16);
1009
#else
1010
            gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1011
                        ref_picture, 0,
1012
                        16);
1013
#endif
1014
        }else if(s->quarter_sample && dir==0){ //FIXME
1015
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1016
                        ref_picture, 0,
1017
                        0, pix_op, qpix_op,
1018
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1019
        }else{
1020
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1021
                        ref_picture, 0,
1022
                        0, pix_op,
1023
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1024
        }           
1025
        break;
1026
    case MV_TYPE_8X8:
1027
        for(i=0;i<4;i++) {
1028
            motion_x = s->mv[dir][i][0];
1029
            motion_y = s->mv[dir][i][1];
1030

    
1031
            dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1032
            src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1033
            src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1034
                    
1035
            /* WARNING: do no forget half pels */
1036
            src_x = clip(src_x, -16, s->width);
1037
            if (src_x == s->width)
1038
                dxy &= ~1;
1039
            src_y = clip(src_y, -16, s->height);
1040
            if (src_y == s->height)
1041
                dxy &= ~2;
1042
                    
1043
            ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1044
            dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1045
            pix_op[dxy](dest, ptr, s->linesize, 8);
1046
        }
1047
    
1048
        if(s->flags&CODEC_FLAG_GRAY) break;
1049
        /* In case of 8X8, we construct a single chroma motion vector
1050
           with a special rounding */
1051
        mx = 0;
1052
        my = 0;
1053
        for(i=0;i<4;i++) {
1054
            mx += s->mv[dir][i][0];
1055
            my += s->mv[dir][i][1];
1056
        }
1057
        if (mx >= 0)
1058
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1059
        else {
1060
            mx = -mx;
1061
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1062
        }
1063
        if (my >= 0)
1064
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1065
        else {
1066
            my = -my;
1067
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1068
        }
1069
        dxy = ((my & 1) << 1) | (mx & 1);
1070
        mx >>= 1;
1071
        my >>= 1;
1072

    
1073
        src_x = mb_x * 8 + mx;
1074
        src_y = mb_y * 8 + my;
1075
        src_x = clip(src_x, -8, s->width/2);
1076
        if (src_x == s->width/2)
1077
            dxy &= ~1;
1078
        src_y = clip(src_y, -8, s->height/2);
1079
        if (src_y == s->height/2)
1080
            dxy &= ~2;
1081
        
1082
        offset = (src_y * (s->linesize >> 1)) + src_x;
1083
        ptr = ref_picture[1] + offset;
1084
        pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
1085
        ptr = ref_picture[2] + offset;
1086
        pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
1087
        break;
1088
    case MV_TYPE_FIELD:
1089
        if (s->picture_structure == PICT_FRAME) {
1090
            /* top field */
1091
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1092
                        ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1093
                        1, pix_op,
1094
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
1095
            /* bottom field */
1096
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1097
                        ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1098
                        1, pix_op,
1099
                        s->mv[dir][1][0], s->mv[dir][1][1], 8);
1100
        } else {
1101
            
1102

    
1103
        }
1104
        break;
1105
    }
1106
}
1107

    
1108

    
1109
/* put block[] to dest[] */
1110
static inline void put_dct(MpegEncContext *s, 
1111
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1112
{
1113
    if (!s->mpeg2)
1114
        s->dct_unquantize(s, block, i, s->qscale);
1115
    ff_idct_put (dest, line_size, block);
1116
}
1117

    
1118
/* add block[] to dest[] */
1119
static inline void add_dct(MpegEncContext *s, 
1120
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1121
{
1122
    if (s->block_last_index[i] >= 0) {
1123
        ff_idct_add (dest, line_size, block);
1124
    }
1125
}
1126

    
1127
static inline void add_dequant_dct(MpegEncContext *s, 
1128
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
1129
{
1130
    if (s->block_last_index[i] >= 0) {
1131
        s->dct_unquantize(s, block, i, s->qscale);
1132

    
1133
        ff_idct_add (dest, line_size, block);
1134
    }
1135
}
1136

    
1137
/**
1138
 * cleans dc, ac, coded_block for the current non intra MB
1139
 */
1140
void ff_clean_intra_table_entries(MpegEncContext *s)
1141
{
1142
    int wrap = s->block_wrap[0];
1143
    int xy = s->block_index[0];
1144
    
1145
    s->dc_val[0][xy           ] = 
1146
    s->dc_val[0][xy + 1       ] = 
1147
    s->dc_val[0][xy     + wrap] =
1148
    s->dc_val[0][xy + 1 + wrap] = 1024;
1149
    /* ac pred */
1150
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(INT16));
1151
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
1152
    if (s->msmpeg4_version>=3) {
1153
        s->coded_block[xy           ] =
1154
        s->coded_block[xy + 1       ] =
1155
        s->coded_block[xy     + wrap] =
1156
        s->coded_block[xy + 1 + wrap] = 0;
1157
    }
1158
    /* chroma */
1159
    wrap = s->block_wrap[4];
1160
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1161
    s->dc_val[1][xy] =
1162
    s->dc_val[2][xy] = 1024;
1163
    /* ac pred */
1164
    memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1165
    memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1166
    
1167
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1168
}
1169

    
1170
/* generic function called after a macroblock has been parsed by the
1171
   decoder or after it has been encoded by the encoder.
1172

1173
   Important variables used:
1174
   s->mb_intra : true if intra macroblock
1175
   s->mv_dir   : motion vector direction
1176
   s->mv_type  : motion vector type
1177
   s->mv       : motion vector
1178
   s->interlaced_dct : true if interlaced dct used (mpeg2)
1179
 */
1180
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1181
{
1182
    int mb_x, mb_y;
1183
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1184

    
1185
    mb_x = s->mb_x;
1186
    mb_y = s->mb_y;
1187

    
1188
#ifdef FF_POSTPROCESS
1189
    /* Obsolete. Exists for compatibility with mplayer only. */
1190
    quant_store[mb_y][mb_x]=s->qscale;
1191
    //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1192
#else
1193
    if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
1194
#endif
1195

    
1196
    /* update DC predictors for P macroblocks */
1197
    if (!s->mb_intra) {
1198
        if (s->h263_pred || s->h263_aic) {
1199
            if(s->mbintra_table[mb_xy])
1200
                ff_clean_intra_table_entries(s);
1201
        } else {
1202
            s->last_dc[0] =
1203
            s->last_dc[1] =
1204
            s->last_dc[2] = 128 << s->intra_dc_precision;
1205
        }
1206
    }
1207
    else if (s->h263_pred || s->h263_aic)
1208
        s->mbintra_table[mb_xy]=1;
1209

    
1210
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1211
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1212
        int motion_x, motion_y;
1213
        
1214
        const int wrap = s->block_wrap[0];
1215
        const int xy = s->block_index[0];
1216
        if (s->mb_intra) {
1217
            motion_x = 0;
1218
            motion_y = 0;
1219
            goto motion_init;
1220
        } else if (s->mv_type == MV_TYPE_16X16) {
1221
            motion_x = s->mv[0][0][0];
1222
            motion_y = s->mv[0][0][1];
1223
        motion_init:
1224
            /* no update if 8X8 because it has been done during parsing */
1225
            s->motion_val[xy][0] = motion_x;
1226
            s->motion_val[xy][1] = motion_y;
1227
            s->motion_val[xy + 1][0] = motion_x;
1228
            s->motion_val[xy + 1][1] = motion_y;
1229
            s->motion_val[xy + wrap][0] = motion_x;
1230
            s->motion_val[xy + wrap][1] = motion_y;
1231
            s->motion_val[xy + 1 + wrap][0] = motion_x;
1232
            s->motion_val[xy + 1 + wrap][1] = motion_y;
1233
        }
1234
    }
1235
    
1236
    if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1237
        UINT8 *dest_y, *dest_cb, *dest_cr;
1238
        int dct_linesize, dct_offset;
1239
        op_pixels_func *op_pix;
1240
        qpel_mc_func *op_qpix;
1241

    
1242
        /* avoid copy if macroblock skipped in last frame too 
1243
           dont touch it for B-frames as they need the skip info from the next p-frame */
1244
        if (s->pict_type != B_TYPE) {
1245
            UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
1246
            if (s->mb_skiped) {
1247
                s->mb_skiped = 0;
1248
                /* if previous was skipped too, then nothing to do ! 
1249
                   skip only during decoding as we might trash the buffers during encoding a bit */
1250
                if (*mbskip_ptr != 0 && !s->encoding) 
1251
                    goto the_end;
1252
                *mbskip_ptr = 1; /* indicate that this time we skiped it */
1253
            } else {
1254
                *mbskip_ptr = 0; /* not skipped */
1255
            }
1256
        }
1257

    
1258
        dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
1259
        dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1260
        dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1261

    
1262
        if (s->interlaced_dct) {
1263
            dct_linesize = s->linesize * 2;
1264
            dct_offset = s->linesize;
1265
        } else {
1266
            dct_linesize = s->linesize;
1267
            dct_offset = s->linesize * 8;
1268
        }
1269

    
1270
        if (!s->mb_intra) {
1271
            /* motion handling */
1272
            /* decoding or more than one mb_type (MC was allready done otherwise) */
1273
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
1274
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
1275
                    op_pix = put_pixels_tab;
1276
                    op_qpix= qpel_mc_rnd_tab;
1277
                }else{
1278
                    op_pix = put_no_rnd_pixels_tab;
1279
                    op_qpix= qpel_mc_no_rnd_tab;
1280
                }
1281

    
1282
                if (s->mv_dir & MV_DIR_FORWARD) {
1283
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1284
                    if ((!s->no_rounding) || s->pict_type==B_TYPE)
1285
                        op_pix = avg_pixels_tab;
1286
                    else
1287
                        op_pix = avg_no_rnd_pixels_tab;
1288
                }
1289
                if (s->mv_dir & MV_DIR_BACKWARD) {
1290
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1291
                }
1292
            }
1293

    
1294
            /* skip dequant / idct if we are really late ;) */
1295
            if(s->hurry_up>1) goto the_end;
1296

    
1297
            /* add dct residue */
1298
            if(!s->mpeg2 && (s->encoding || (!s->h263_msmpeg4))){
1299
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
1300
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1301
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1302
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1303

    
1304
                if(!(s->flags&CODEC_FLAG_GRAY)){
1305
                    add_dequant_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1306
                    add_dequant_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1307
                }
1308
            } else {
1309
                add_dct(s, block[0], 0, dest_y, dct_linesize);
1310
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1311
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1312
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1313

    
1314
                if(!(s->flags&CODEC_FLAG_GRAY)){
1315
                    add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1316
                    add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1317
                }
1318
            }
1319
        } else {
1320
            /* dct only in intra block */
1321
            put_dct(s, block[0], 0, dest_y, dct_linesize);
1322
            put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1323
            put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1324
            put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1325

    
1326
            if(!(s->flags&CODEC_FLAG_GRAY)){
1327
                put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1328
                put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1329
            }
1330
        }
1331
    }
1332
 the_end:
1333
    emms_c(); //FIXME remove
1334
}
1335

    
1336
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
1337
{
1338
    static const char tab[64]=
1339
        {3,2,2,1,1,1,1,1,
1340
         1,1,1,1,1,1,1,1,
1341
         1,1,1,1,1,1,1,1,
1342
         0,0,0,0,0,0,0,0,
1343
         0,0,0,0,0,0,0,0,
1344
         0,0,0,0,0,0,0,0,
1345
         0,0,0,0,0,0,0,0,
1346
         0,0,0,0,0,0,0,0};
1347
    int score=0;
1348
    int run=0;
1349
    int i;
1350
    DCTELEM *block= s->block[n];
1351
    const int last_index= s->block_last_index[n];
1352

    
1353
    if(skip_dc) skip_dc=1;
1354
    
1355
    /* are all which we could set to zero are allready zero? */
1356
    if(last_index<=skip_dc - 1) return;
1357

    
1358
    for(i=0; i<=last_index; i++){
1359
        const int j = zigzag_direct[i];
1360
        const int level = ABS(block[j]);
1361
        if(level==1){
1362
            if(skip_dc && i==0) continue;
1363
            score+= tab[run];
1364
            run=0;
1365
        }else if(level>1){
1366
            return;
1367
        }else{
1368
            run++;
1369
        }
1370
    }
1371
    if(score >= threshold) return;
1372
    for(i=skip_dc; i<=last_index; i++){
1373
        const int j = zigzag_direct[i];
1374
        block[j]=0;
1375
    }
1376
    if(block[0]) s->block_last_index[n]= 0;
1377
    else         s->block_last_index[n]= -1;
1378
}
1379

    
1380
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1381
{
1382
    int i;
1383
    const int maxlevel= s->max_qcoeff;
1384
    const int minlevel= s->min_qcoeff;
1385
        
1386
    for(i=0;i<=last_index; i++){
1387
        const int j = zigzag_direct[i];
1388
        int level = block[j];
1389
       
1390
        if     (level>maxlevel) level=maxlevel;
1391
        else if(level<minlevel) level=minlevel;
1392
        block[j]= level;
1393
    }
1394
}
1395

    
1396
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1397
{
1398
    const int mb_x= s->mb_x;
1399
    const int mb_y= s->mb_y;
1400
    int i;
1401
    int skip_dct[6];
1402
#if 0
1403
        if (s->interlaced_dct) {
1404
            dct_linesize = s->linesize * 2;
1405
            dct_offset = s->linesize;
1406
        } else {
1407
            dct_linesize = s->linesize;
1408
            dct_offset = s->linesize * 8;
1409
        }
1410
#endif
1411
    for(i=0; i<6; i++) skip_dct[i]=0;
1412

    
1413
    if (s->mb_intra) {
1414
        UINT8 *ptr;
1415
        int wrap;
1416

    
1417
        wrap = s->linesize;
1418
        ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1419
        get_pixels(s->block[0], ptr               , wrap);
1420
        get_pixels(s->block[1], ptr            + 8, wrap);
1421
        get_pixels(s->block[2], ptr + 8 * wrap    , wrap);
1422
        get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1423

    
1424
        wrap >>=1;
1425
        ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1426
        get_pixels(s->block[4], ptr, wrap);
1427

    
1428
        ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1429
        get_pixels(s->block[5], ptr, wrap);
1430
    }else{
1431
        op_pixels_func *op_pix;
1432
        qpel_mc_func *op_qpix;
1433
        UINT8 *dest_y, *dest_cb, *dest_cr;
1434
        UINT8 *ptr_y, *ptr_cb, *ptr_cr;
1435
        int wrap_y, wrap_c;
1436

    
1437
        dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
1438
        dest_cb = s->current_picture[1] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1439
        dest_cr = s->current_picture[2] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1440
        wrap_y = s->linesize;
1441
        wrap_c = wrap_y>>1;
1442
        ptr_y  = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
1443
        ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
1444
        ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
1445

    
1446
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
1447
            op_pix = put_pixels_tab;
1448
            op_qpix= qpel_mc_rnd_tab;
1449
        }else{
1450
            op_pix = put_no_rnd_pixels_tab;
1451
            op_qpix= qpel_mc_no_rnd_tab;
1452
        }
1453

    
1454
        if (s->mv_dir & MV_DIR_FORWARD) {
1455
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1456
           if ((!s->no_rounding) || s->pict_type==B_TYPE)
1457
                op_pix = avg_pixels_tab;
1458
            else
1459
                op_pix = avg_no_rnd_pixels_tab;
1460
        }
1461
        if (s->mv_dir & MV_DIR_BACKWARD) {
1462
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1463
        }
1464

    
1465
        diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
1466
        diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
1467
        diff_pixels(s->block[2], ptr_y + 8 * wrap_y    , dest_y + 8 * wrap_y    , wrap_y);
1468
        diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
1469
        diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1470
        diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1471
    
1472
        /* pre quantization */         
1473
        if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
1474
            if(pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
1475
            if(pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
1476
            if(pix_abs8x8(ptr_y + 8*wrap_y    , dest_y + 8*wrap_y    , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
1477
            if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
1478
            if(pix_abs8x8(ptr_cb              , dest_cb              , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
1479
            if(pix_abs8x8(ptr_cr              , dest_cr              , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
1480
#if 0
1481
{
1482
 static int stat[7];
1483
 int num=0;
1484
 for(i=0; i<6; i++)
1485
  if(skip_dct[i]) num++;
1486
 stat[num]++;
1487
 
1488
 if(s->mb_x==0 && s->mb_y==0){
1489
  for(i=0; i<7; i++){
1490
   printf("%6d %1d\n", stat[i], i);
1491
  }
1492
 }
1493
}
1494
#endif
1495
        }
1496

    
1497
    }
1498
            
1499
#if 0
1500
            {
1501
                float adap_parm;
1502
                
1503
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1504
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1505
            
1506
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
1507
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
1508
                        s->qscale, adap_parm, s->qscale*adap_parm,
1509
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1510
            }
1511
#endif
1512
    /* DCT & quantize */
1513
    if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
1514
        h263_dc_scale(s);
1515
    } else if (s->h263_aic) {
1516
        s->y_dc_scale = 2*s->qscale;
1517
        s->c_dc_scale = 2*s->qscale;
1518
    } else {
1519
        /* default quantization values */
1520
        s->y_dc_scale = 8;
1521
        s->c_dc_scale = 8;
1522
    }
1523
    if(s->out_format==FMT_MJPEG){
1524
        for(i=0;i<6;i++) {
1525
            int overflow;
1526
            s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
1527
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1528
        }
1529
    }else{
1530
        for(i=0;i<6;i++) {
1531
            if(!skip_dct[i]){
1532
                int overflow;
1533
                s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1534
            // FIXME we could decide to change to quantizer instead of clipping
1535
            // JS: I don't think that would be a good idea it could lower quality instead
1536
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
1537
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1538
            }else
1539
                s->block_last_index[i]= -1;
1540
        }
1541
        if(s->luma_elim_threshold && !s->mb_intra)
1542
            for(i=0; i<4; i++)
1543
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
1544
        if(s->chroma_elim_threshold && !s->mb_intra)
1545
            for(i=4; i<6; i++)
1546
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
1547
    }
1548

    
1549
    /* huffman encode */
1550
    switch(s->out_format) {
1551
    case FMT_MPEG1:
1552
        mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1553
        break;
1554
    case FMT_H263:
1555
        if (s->h263_msmpeg4)
1556
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1557
        else if(s->h263_pred)
1558
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1559
        else
1560
            h263_encode_mb(s, s->block, motion_x, motion_y);
1561
        break;
1562
    case FMT_MJPEG:
1563
        mjpeg_encode_mb(s, s->block);
1564
        break;
1565
    }
1566
}
1567

    
1568
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
1569
{
1570
#if 1
1571
    int bytes= length>>4;
1572
    int bits= length&15;
1573
    int i;
1574

    
1575
    if(length==0) return;
1576

    
1577
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
1578
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
1579
#else
1580
    int bytes= length>>3;
1581
    int bits= length&7;
1582
    int i;
1583

    
1584
    for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
1585
    put_bits(pb, bits, src[i]>>(8-bits));
1586
#endif
1587
}
1588

    
1589
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1590
    int i;
1591

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

    
1594
    /* mpeg1 */
1595
    d->mb_incr= s->mb_incr;
1596
    for(i=0; i<3; i++)
1597
        d->last_dc[i]= s->last_dc[i];
1598
    
1599
    /* statistics */
1600
    d->mv_bits= s->mv_bits;
1601
    d->i_tex_bits= s->i_tex_bits;
1602
    d->p_tex_bits= s->p_tex_bits;
1603
    d->i_count= s->i_count;
1604
    d->p_count= s->p_count;
1605
    d->skip_count= s->skip_count;
1606
    d->misc_bits= s->misc_bits;
1607
    d->last_bits= 0;
1608

    
1609
    d->mb_skiped= s->mb_skiped;
1610
}
1611

    
1612
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1613
    int i;
1614

    
1615
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
1616
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1617
    
1618
    /* mpeg1 */
1619
    d->mb_incr= s->mb_incr;
1620
    for(i=0; i<3; i++)
1621
        d->last_dc[i]= s->last_dc[i];
1622
    
1623
    /* statistics */
1624
    d->mv_bits= s->mv_bits;
1625
    d->i_tex_bits= s->i_tex_bits;
1626
    d->p_tex_bits= s->p_tex_bits;
1627
    d->i_count= s->i_count;
1628
    d->p_count= s->p_count;
1629
    d->skip_count= s->skip_count;
1630
    d->misc_bits= s->misc_bits;
1631

    
1632
    d->mb_intra= s->mb_intra;
1633
    d->mb_skiped= s->mb_skiped;
1634
    d->mv_type= s->mv_type;
1635
    d->mv_dir= s->mv_dir;
1636
    d->pb= s->pb;
1637
    if(s->data_partitioning){
1638
        d->pb2= s->pb2;
1639
        d->tex_pb= s->tex_pb;
1640
    }
1641
    d->block= s->block;
1642
    for(i=0; i<6; i++)
1643
        d->block_last_index[i]= s->block_last_index[i];
1644
}
1645

    
1646
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
1647
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
1648
                           int *dmin, int *next_block, int motion_x, int motion_y)
1649
{
1650
    int bits_count;
1651
    
1652
    copy_context_before_encode(s, backup, type);
1653

    
1654
    s->block= s->blocks[*next_block];
1655
    s->pb= pb[*next_block];
1656
    if(s->data_partitioning){
1657
        s->pb2   = pb2   [*next_block];
1658
        s->tex_pb= tex_pb[*next_block];
1659
    }
1660

    
1661
    encode_mb(s, motion_x, motion_y);
1662

    
1663
    bits_count= get_bit_count(&s->pb);
1664
    if(s->data_partitioning){
1665
        bits_count+= get_bit_count(&s->pb2);
1666
        bits_count+= get_bit_count(&s->tex_pb);
1667
    }
1668

    
1669
    if(bits_count<*dmin){
1670
        *dmin= bits_count;
1671
        *next_block^=1;
1672

    
1673
        copy_context_after_encode(best, s, type);
1674
    }
1675
}
1676

    
1677
static void encode_picture(MpegEncContext *s, int picture_number)
1678
{
1679
    int mb_x, mb_y, last_gob, pdif = 0;
1680
    int i;
1681
    int bits;
1682
    MpegEncContext best_s, backup_s;
1683
    UINT8 bit_buf[2][3000];
1684
    UINT8 bit_buf2[2][3000];
1685
    UINT8 bit_buf_tex[2][3000];
1686
    PutBitContext pb[2], pb2[2], tex_pb[2];
1687

    
1688
    for(i=0; i<2; i++){
1689
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
1690
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
1691
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
1692
    }
1693

    
1694
    s->picture_number = picture_number;
1695

    
1696
    s->block_wrap[0]=
1697
    s->block_wrap[1]=
1698
    s->block_wrap[2]=
1699
    s->block_wrap[3]= s->mb_width*2 + 2;
1700
    s->block_wrap[4]=
1701
    s->block_wrap[5]= s->mb_width + 2;
1702
    
1703
    /* Reset the average MB variance */
1704
    s->mb_var_sum = 0;
1705
    s->mc_mb_var_sum = 0;
1706

    
1707
    /* we need to initialize some time vars before we can encode b-frames */
1708
    if (s->h263_pred && !s->h263_msmpeg4)
1709
        ff_set_mpeg4_time(s, s->picture_number); 
1710

    
1711
    /* Estimate motion for every MB */
1712
    if(s->pict_type != I_TYPE){
1713
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1714
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1715
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1716
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1717
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1718
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1719
                s->mb_x = mb_x;
1720
                s->mb_y = mb_y;
1721
                s->block_index[0]+=2;
1722
                s->block_index[1]+=2;
1723
                s->block_index[2]+=2;
1724
                s->block_index[3]+=2;
1725

    
1726
                /* compute motion vector & mb_type and store in context */
1727
                if(s->pict_type==B_TYPE)
1728
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
1729
                else
1730
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
1731
//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1732
            }
1733
        }
1734
        emms_c();
1735
    }else /* if(s->pict_type == I_TYPE) */{
1736
        /* I-Frame */
1737
        //FIXME do we need to zero them?
1738
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1739
        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1740
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1741
    }
1742

    
1743
    if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1744
        s->pict_type= I_TYPE;
1745
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1746
        if(s->max_b_frames==0){
1747
            s->input_pict_type= I_TYPE;
1748
            s->input_picture_in_gop_number=0;
1749
        }
1750
//printf("Scene change detected, encoding as I Frame\n");
1751
    }
1752
    
1753
    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 
1754
        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1755
        ff_fix_long_p_mvs(s);
1756
    if(s->pict_type==B_TYPE){
1757
        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1758
        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1759

    
1760
        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
1761
        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
1762
        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
1763
        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
1764
    }
1765
    
1766
//printf("f_code %d ///\n", s->f_code);
1767

    
1768
//    printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1769

    
1770
    if(s->flags&CODEC_FLAG_PASS2)
1771
        s->qscale = ff_rate_estimate_qscale_pass2(s);
1772
    else if (!s->fixed_qscale) 
1773
        s->qscale = ff_rate_estimate_qscale(s);
1774

    
1775
    if (s->out_format == FMT_MJPEG) {
1776
        /* for mjpeg, we do include qscale in the matrix */
1777
        s->intra_matrix[0] = default_intra_matrix[0];
1778
        for(i=1;i<64;i++)
1779
            s->intra_matrix[i] = CLAMP_TO_8BIT((default_intra_matrix[i] * s->qscale) >> 3);
1780
        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, 
1781
                       s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
1782
    }
1783

    
1784
    s->last_bits= get_bit_count(&s->pb);
1785
    switch(s->out_format) {
1786
    case FMT_MJPEG:
1787
        mjpeg_picture_header(s);
1788
        break;
1789
    case FMT_H263:
1790
        if (s->h263_msmpeg4) 
1791
            msmpeg4_encode_picture_header(s, picture_number);
1792
        else if (s->h263_pred)
1793
            mpeg4_encode_picture_header(s, picture_number);
1794
        else if (s->h263_rv10) 
1795
            rv10_encode_picture_header(s, picture_number);
1796
        else
1797
            h263_encode_picture_header(s, picture_number);
1798
        break;
1799
    case FMT_MPEG1:
1800
        mpeg1_encode_picture_header(s, picture_number);
1801
        break;
1802
    }
1803
    bits= get_bit_count(&s->pb);
1804
    s->header_bits= bits - s->last_bits;
1805
    s->last_bits= bits;
1806
    s->mv_bits=0;
1807
    s->misc_bits=0;
1808
    s->i_tex_bits=0;
1809
    s->p_tex_bits=0;
1810
    s->i_count=0;
1811
    s->p_count=0;
1812
    s->skip_count=0;
1813

    
1814
    /* init last dc values */
1815
    /* note: quant matrix value (8) is implied here */
1816
    s->last_dc[0] = 128;
1817
    s->last_dc[1] = 128;
1818
    s->last_dc[2] = 128;
1819
    s->mb_incr = 1;
1820
    s->last_mv[0][0][0] = 0;
1821
    s->last_mv[0][0][1] = 0;
1822

    
1823
    /* Get the GOB height based on picture height */
1824
    if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
1825
        if (s->height <= 400)
1826
            s->gob_index = 1;
1827
        else if (s->height <= 800)
1828
            s->gob_index = 2;
1829
        else
1830
            s->gob_index = 4;
1831
    }else if(s->codec_id==CODEC_ID_MPEG4){
1832
        s->gob_index = 1;
1833
    }
1834

    
1835
    if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
1836
        ff_mpeg4_init_partitions(s);
1837

    
1838
    s->resync_mb_x=0;
1839
    s->resync_mb_y=0;
1840
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1841
        /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
1842
        /* TODO: Put all this stuff in a separate generic function */
1843
        if (s->rtp_mode) {
1844
            if (!mb_y) {
1845
                s->ptr_lastgob = s->pb.buf;
1846
                s->ptr_last_mb_line = s->pb.buf;
1847
            } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1848
                // MN: we could move the space check from h263 -> here, as its not h263 specific
1849
                last_gob = h263_encode_gob_header(s, mb_y);
1850
                if (last_gob) {
1851
                    s->first_slice_line = 1;
1852
                }else{
1853
                    /*MN: we reset it here instead at the end of each line cuz mpeg4 can have 
1854
                          slice lines starting & ending in the middle*/
1855
                    s->first_slice_line = 0;
1856
                }
1857
            }
1858
        }
1859
        
1860
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1861
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1862
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1863
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1864
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
1865
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1866
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1867
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1868
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1869
//            int d;
1870
            int dmin=10000000;
1871

    
1872
            s->mb_x = mb_x;
1873
            s->mb_y = mb_y;
1874
            s->block_index[0]+=2;
1875
            s->block_index[1]+=2;
1876
            s->block_index[2]+=2;
1877
            s->block_index[3]+=2;
1878
            s->block_index[4]++;
1879
            s->block_index[5]++;
1880
            
1881
            /* write gob / video packet header for formats which support it at any MB (MPEG4) */
1882
            if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
1883
                int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
1884

    
1885
                //the *2 is there so we stay below the requested size
1886
                if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ 
1887
                    if(s->codec_id==CODEC_ID_MPEG4){
1888
                        if(s->data_partitioning && s->pict_type!=B_TYPE){
1889
                            ff_mpeg4_merge_partitions(s);
1890
                            ff_mpeg4_init_partitions(s);
1891
                        }
1892
                        ff_mpeg4_encode_video_packet_header(s);
1893

    
1894
                        if(s->flags&CODEC_FLAG_PASS1){
1895
                            int bits= get_bit_count(&s->pb);
1896
                            s->misc_bits+= bits - s->last_bits;
1897
                            s->last_bits= bits;
1898
                        }
1899
                        ff_mpeg4_clean_buffers(s);
1900
                    }
1901
                    s->ptr_lastgob = pbBufPtr(&s->pb);
1902
                    s->first_slice_line=1;
1903
                    s->resync_mb_x=mb_x;
1904
                    s->resync_mb_y=mb_y;
1905
                }
1906

    
1907
                if(  (s->resync_mb_x   == s->mb_x)
1908
                   && s->resync_mb_y+1 == s->mb_y){
1909
                    s->first_slice_line=0; 
1910
                }
1911
            }
1912

    
1913
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1914
                int next_block=0;
1915
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
1916

    
1917
                copy_context_before_encode(&backup_s, s, -1);
1918
                backup_s.pb= s->pb;
1919
                best_s.data_partitioning= s->data_partitioning;
1920
                if(s->data_partitioning){
1921
                    backup_s.pb2= s->pb2;
1922
                    backup_s.tex_pb= s->tex_pb;
1923
                }
1924

    
1925
                if(mb_type&MB_TYPE_INTER){
1926
                    s->mv_dir = MV_DIR_FORWARD;
1927
                    s->mv_type = MV_TYPE_16X16;
1928
                    s->mb_intra= 0;
1929
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
1930
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
1931
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
1932
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
1933
                }
1934
                if(mb_type&MB_TYPE_INTER4V){                 
1935
                    s->mv_dir = MV_DIR_FORWARD;
1936
                    s->mv_type = MV_TYPE_8X8;
1937
                    s->mb_intra= 0;
1938
                    for(i=0; i<4; i++){
1939
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1940
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1941
                    }
1942
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
1943
                                 &dmin, &next_block, 0, 0);
1944
                }
1945
                if(mb_type&MB_TYPE_FORWARD){
1946
                    s->mv_dir = MV_DIR_FORWARD;
1947
                    s->mv_type = MV_TYPE_16X16;
1948
                    s->mb_intra= 0;
1949
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1950
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1951
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
1952
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
1953
                }
1954
                if(mb_type&MB_TYPE_BACKWARD){
1955
                    s->mv_dir = MV_DIR_BACKWARD;
1956
                    s->mv_type = MV_TYPE_16X16;
1957
                    s->mb_intra= 0;
1958
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1959
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1960
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
1961
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
1962
                }
1963
                if(mb_type&MB_TYPE_BIDIR){
1964
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1965
                    s->mv_type = MV_TYPE_16X16;
1966
                    s->mb_intra= 0;
1967
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1968
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1969
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1970
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1971
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
1972
                                 &dmin, &next_block, 0, 0);
1973
                }
1974
                if(mb_type&MB_TYPE_DIRECT){
1975
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1976
                    s->mv_type = MV_TYPE_16X16; //FIXME
1977
                    s->mb_intra= 0;
1978
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1979
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1980
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1981
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1982
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
1983
                                 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1984
                }
1985
                if(mb_type&MB_TYPE_INTRA){
1986
                    s->mv_dir = MV_DIR_FORWARD;
1987
                    s->mv_type = MV_TYPE_16X16;
1988
                    s->mb_intra= 1;
1989
                    s->mv[0][0][0] = 0;
1990
                    s->mv[0][0][1] = 0;
1991
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
1992
                                 &dmin, &next_block, 0, 0);
1993
                    /* force cleaning of ac/dc pred stuff if needed ... */
1994
                    if(s->h263_pred || s->h263_aic)
1995
                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1996
                }
1997
                copy_context_after_encode(s, &best_s, -1);
1998
                
1999
                pb_bits_count= get_bit_count(&s->pb);
2000
                flush_put_bits(&s->pb);
2001
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2002
                s->pb= backup_s.pb;
2003
                
2004
                if(s->data_partitioning){
2005
                    pb2_bits_count= get_bit_count(&s->pb2);
2006
                    flush_put_bits(&s->pb2);
2007
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2008
                    s->pb2= backup_s.pb2;
2009
                    
2010
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
2011
                    flush_put_bits(&s->tex_pb);
2012
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2013
                    s->tex_pb= backup_s.tex_pb;
2014
                }
2015
                s->last_bits= get_bit_count(&s->pb);
2016
            } else {
2017
                int motion_x, motion_y;
2018
                s->mv_type=MV_TYPE_16X16;
2019
                // only one MB-Type possible
2020
                switch(mb_type){
2021
                case MB_TYPE_INTRA:
2022
                    s->mv_dir = MV_DIR_FORWARD;
2023
                    s->mb_intra= 1;
2024
                    motion_x= s->mv[0][0][0] = 0;
2025
                    motion_y= s->mv[0][0][1] = 0;
2026
                    break;
2027
                case MB_TYPE_INTER:
2028
                    s->mv_dir = MV_DIR_FORWARD;
2029
                    s->mb_intra= 0;
2030
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2031
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2032
                    break;
2033
                case MB_TYPE_INTER4V:
2034
                    s->mv_dir = MV_DIR_FORWARD;
2035
                    s->mv_type = MV_TYPE_8X8;
2036
                    s->mb_intra= 0;
2037
                    for(i=0; i<4; i++){
2038
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
2039
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
2040
                    }
2041
                    motion_x= motion_y= 0;
2042
                    break;
2043
                case MB_TYPE_DIRECT:
2044
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2045
                    s->mb_intra= 0;
2046
                    motion_x=s->b_direct_mv_table[xy][0];
2047
                    motion_y=s->b_direct_mv_table[xy][1];
2048
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
2049
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
2050
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
2051
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
2052
                    break;
2053
                case MB_TYPE_BIDIR:
2054
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2055
                    s->mb_intra= 0;
2056
                    motion_x=0;
2057
                    motion_y=0;
2058
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2059
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2060
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2061
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2062
                    break;
2063
                case MB_TYPE_BACKWARD:
2064
                    s->mv_dir = MV_DIR_BACKWARD;
2065
                    s->mb_intra= 0;
2066
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2067
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2068
                    break;
2069
                case MB_TYPE_FORWARD:
2070
                    s->mv_dir = MV_DIR_FORWARD;
2071
                    s->mb_intra= 0;
2072
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2073
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2074
//                    printf(" %d %d ", motion_x, motion_y);
2075
                    break;
2076
                default:
2077
                    motion_x=motion_y=0; //gcc warning fix
2078
                    printf("illegal MB type\n");
2079
                }
2080
                encode_mb(s, motion_x, motion_y);
2081
            }
2082
            /* clean the MV table in IPS frames for direct mode in B frames */
2083
            if(s->mb_intra /* && I,P,S_TYPE */){
2084
                s->p_mv_table[xy][0]=0;
2085
                s->p_mv_table[xy][1]=0;
2086
            }
2087

    
2088
            MPV_decode_mb(s, s->block);
2089
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
2090
        }
2091

    
2092

    
2093
        /* Obtain average GOB size for RTP */
2094
        if (s->rtp_mode) {
2095
            if (!mb_y)
2096
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
2097
            else if (!(mb_y % s->gob_index)) {    
2098
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
2099
                s->ptr_last_mb_line = pbBufPtr(&s->pb);
2100
            }
2101
            //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 
2102
            //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
2103
            if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
2104
        }
2105
    }
2106
    emms_c();
2107

    
2108
    if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
2109
        ff_mpeg4_merge_partitions(s);
2110

    
2111
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
2112
        msmpeg4_encode_ext_header(s);
2113

    
2114
    if(s->codec_id==CODEC_ID_MPEG4) 
2115
        ff_mpeg4_stuffing(&s->pb);
2116

    
2117
    //if (s->gob_number)
2118
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
2119
    
2120
    /* Send the last GOB if RTP */    
2121
    if (s->rtp_mode) {
2122
        flush_put_bits(&s->pb);
2123
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
2124
        /* Call the RTP callback to send the last GOB */
2125
        if (s->rtp_callback)
2126
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
2127
        s->ptr_lastgob = pbBufPtr(&s->pb);
2128
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
2129
    }
2130
}
2131

    
2132
static int dct_quantize_c(MpegEncContext *s, 
2133
                        DCTELEM *block, int n,
2134
                        int qscale, int *overflow)
2135
{
2136
    int i, j, level, last_non_zero, q;
2137
    const int *qmat;
2138
    int bias;
2139
    int max=0;
2140
    unsigned int threshold1, threshold2;
2141
    
2142
    av_fdct (block);
2143

    
2144
    /* we need this permutation so that we correct the IDCT
2145
       permutation. will be moved into DCT code */
2146
    block_permute(block);
2147

    
2148
    if (s->mb_intra) {
2149
        if (!s->h263_aic) {
2150
            if (n < 4)
2151
                q = s->y_dc_scale;
2152
            else
2153
                q = s->c_dc_scale;
2154
            q = q << 3;
2155
        } else
2156
            /* For AIC we skip quant/dequant of INTRADC */
2157
            q = 1 << 3;
2158
            
2159
        /* note: block[0] is assumed to be positive */
2160
        block[0] = (block[0] + (q >> 1)) / q;
2161
        i = 1;
2162
        last_non_zero = 0;
2163
        qmat = s->q_intra_matrix[qscale];
2164
        bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2165
    } else {
2166
        i = 0;
2167
        last_non_zero = -1;
2168
        qmat = s->q_inter_matrix[qscale];
2169
        bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2170
    }
2171
    threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
2172
    threshold2= threshold1<<1;
2173

    
2174
    for(;i<64;i++) {
2175
        j = zigzag_direct[i];
2176
        level = block[j];
2177
        level = level * qmat[j];
2178

    
2179
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
2180
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
2181
        if(((unsigned)(level+threshold1))>threshold2){
2182
            if(level>0){
2183
                level= (bias + level)>>(QMAT_SHIFT - 3);
2184
                block[j]= level;
2185
            }else{
2186
                level= (bias - level)>>(QMAT_SHIFT - 3);
2187
                block[j]= -level;
2188
            }
2189
            max |=level;
2190
            last_non_zero = i;
2191
        }else{
2192
            block[j]=0;
2193
        }
2194
    }
2195
    *overflow= s->max_qcoeff < max; //overflow might have happend
2196
    
2197
    return last_non_zero;
2198
}
2199

    
2200
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
2201
                                   DCTELEM *block, int n, int qscale)
2202
{
2203
    int i, level, nCoeffs;
2204
    const UINT16 *quant_matrix;
2205

    
2206
    if(s->alternate_scan) nCoeffs= 64;
2207
    else nCoeffs= s->block_last_index[n]+1;
2208
    
2209
    if (s->mb_intra) {
2210
        if (n < 4) 
2211
            block[0] = block[0] * s->y_dc_scale;
2212
        else
2213
            block[0] = block[0] * s->c_dc_scale;
2214
        /* XXX: only mpeg1 */
2215
        quant_matrix = s->intra_matrix;
2216
        for(i=1;i<nCoeffs;i++) {
2217
            int j= zigzag_direct[i];
2218
            level = block[j];
2219
            if (level) {
2220
                if (level < 0) {
2221
                    level = -level;
2222
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
2223
                    level = (level - 1) | 1;
2224
                    level = -level;
2225
                } else {
2226
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
2227
                    level = (level - 1) | 1;
2228
                }
2229
#ifdef PARANOID
2230
                if (level < -2048 || level > 2047)
2231
                    fprintf(stderr, "unquant error %d %d\n", i, level);
2232
#endif
2233
                block[j] = level;
2234
            }
2235
        }
2236
    } else {
2237
        i = 0;
2238
        quant_matrix = s->inter_matrix;
2239
        for(;i<nCoeffs;i++) {
2240
            int j= zigzag_direct[i];
2241
            level = block[j];
2242
            if (level) {
2243
                if (level < 0) {
2244
                    level = -level;
2245
                    level = (((level << 1) + 1) * qscale *
2246
                             ((int) (quant_matrix[j]))) >> 4;
2247
                    level = (level - 1) | 1;
2248
                    level = -level;
2249
                } else {
2250
                    level = (((level << 1) + 1) * qscale *
2251
                             ((int) (quant_matrix[j]))) >> 4;
2252
                    level = (level - 1) | 1;
2253
                }
2254
#ifdef PARANOID
2255
                if (level < -2048 || level > 2047)
2256
                    fprintf(stderr, "unquant error %d %d\n", i, level);
2257
#endif
2258
                block[j] = level;
2259
            }
2260
        }
2261
    }
2262
}
2263

    
2264
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
2265
                                   DCTELEM *block, int n, int qscale)
2266
{
2267
    int i, level, nCoeffs;
2268
    const UINT16 *quant_matrix;
2269

    
2270
    if(s->alternate_scan) nCoeffs= 64;
2271
    else nCoeffs= s->block_last_index[n]+1;
2272
    
2273
    if (s->mb_intra) {
2274
        if (n < 4) 
2275
            block[0] = block[0] * s->y_dc_scale;
2276
        else
2277
            block[0] = block[0] * s->c_dc_scale;
2278
        quant_matrix = s->intra_matrix;
2279
        for(i=1;i<nCoeffs;i++) {
2280
            int j= zigzag_direct[i];
2281
            level = block[j];
2282
            if (level) {
2283
                if (level < 0) {
2284
                    level = -level;
2285
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
2286
                    level = -level;
2287
                } else {
2288
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
2289
                }
2290
#ifdef PARANOID
2291
                if (level < -2048 || level > 2047)
2292
                    fprintf(stderr, "unquant error %d %d\n", i, level);
2293
#endif
2294
                block[j] = level;
2295
            }
2296
        }
2297
    } else {
2298
        int sum=-1;
2299
        i = 0;
2300
        quant_matrix = s->inter_matrix;
2301
        for(;i<nCoeffs;i++) {
2302
            int j= zigzag_direct[i];
2303
            level = block[j];
2304
            if (level) {
2305
                if (level < 0) {
2306
                    level = -level;
2307
                    level = (((level << 1) + 1) * qscale *
2308
                             ((int) (quant_matrix[j]))) >> 4;
2309
                    level = -level;
2310
                } else {
2311
                    level = (((level << 1) + 1) * qscale *
2312
                             ((int) (quant_matrix[j]))) >> 4;
2313
                }
2314
#ifdef PARANOID
2315
                if (level < -2048 || level > 2047)
2316
                    fprintf(stderr, "unquant error %d %d\n", i, level);
2317
#endif
2318
                block[j] = level;
2319
                sum+=level;
2320
            }
2321
        }
2322
        block[63]^=sum&1;
2323
    }
2324
}
2325

    
2326

    
2327
static void dct_unquantize_h263_c(MpegEncContext *s, 
2328
                                  DCTELEM *block, int n, int qscale)
2329
{
2330
    int i, level, qmul, qadd;
2331
    int nCoeffs;
2332
    
2333
    if (s->mb_intra) {
2334
        if (!s->h263_aic) {
2335
            if (n < 4) 
2336
                block[0] = block[0] * s->y_dc_scale;
2337
            else
2338
                block[0] = block[0] * s->c_dc_scale;
2339
        }
2340
        i = 1;
2341
        nCoeffs= 64; //does not allways use zigzag table 
2342
    } else {
2343
        i = 0;
2344
        nCoeffs= zigzag_end[ s->block_last_index[n] ];
2345
    }
2346

    
2347
    qmul = s->qscale << 1;
2348
    if (s->h263_aic && s->mb_intra)
2349
        qadd = 0;
2350
    else
2351
        qadd = (s->qscale - 1) | 1;
2352

    
2353
    for(;i<nCoeffs;i++) {
2354
        level = block[i];
2355
        if (level) {
2356
            if (level < 0) {
2357
                level = level * qmul - qadd;
2358
            } else {
2359
                level = level * qmul + qadd;
2360
            }
2361
#ifdef PARANOID
2362
                if (level < -2048 || level > 2047)
2363
                    fprintf(stderr, "unquant error %d %d\n", i, level);
2364
#endif
2365
            block[i] = level;
2366
        }
2367
    }
2368
}
2369

    
2370
static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
2371
{
2372
    int dc, dcb, dcr, y, i;
2373
    for(i=0; i<4; i++){
2374
        dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
2375
        for(y=0; y<8; y++){
2376
            int x;
2377
            for(x=0; x<8; x++){
2378
                dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
2379
            }
2380
        }
2381
    }
2382
    dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
2383
    dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
2384
    for(y=0; y<8; y++){
2385
        int x;
2386
        for(x=0; x<8; x++){
2387
            dest_cb[x + y*(s->linesize>>1)]= dcb/8;
2388
            dest_cr[x + y*(s->linesize>>1)]= dcr/8;
2389
        }
2390
    }
2391
}
2392

    
2393
/**
2394
 * will conceal past errors, and allso drop b frames if needed
2395
 *
2396
 */
2397
void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
2398
{
2399
    int mb_x= s->mb_x;
2400
    int mb_y= s->mb_y;
2401
    int mb_dist=0;
2402
    int i, intra_count=0, inter_count=0;
2403
    int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
2404
    int inter_conceal= s->msmpeg4_version ? 50 : 50;
2405
    
2406
    // for last block
2407
    if(mb_x>=s->mb_width)  mb_x= s->mb_width -1;
2408
    if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
2409

    
2410
    if(s->decoding_error==0 && unknown_pos){
2411
        if(s->data_partitioning && s->pict_type!=B_TYPE)
2412
                s->decoding_error= DECODING_AC_LOST;
2413
        else
2414
                s->decoding_error= DECODING_DESYNC;
2415
    }
2416

    
2417
    if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
2418

    
2419
    for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
2420
        if(s->mbintra_table[i]) intra_count++;
2421
        else                    inter_count++;
2422
    }
2423
    
2424
    if(s->decoding_error==DECODING_AC_LOST){
2425
        intra_conceal*=2;
2426
        inter_conceal*=2;
2427
    }else if(s->decoding_error==DECODING_ACDC_LOST){
2428
        intra_conceal*=2;
2429
        inter_conceal*=2;
2430
    }
2431

    
2432
    if(unknown_pos && (intra_count<inter_count)){
2433
        intra_conceal= inter_conceal= s->mb_num; 
2434
//        printf("%d %d\n",intra_count, inter_count);
2435
    }
2436

    
2437
    fprintf(stderr, "concealing errors\n");
2438

    
2439
    /* for all MBs from the current one back until the last resync marker */
2440
    for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
2441
        for(; mb_x>=0; mb_x--){
2442
            uint8_t *dest_y  = s->current_picture[0] + (mb_y * 16*  s->linesize      ) + mb_x * 16;
2443
            uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
2444
            uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
2445
            int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
2446
            int mb_y_backup= s->mb_y;
2447
            s->mb_x=mb_x;
2448
            s->mb_y=mb_y;
2449
            if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
2450
                if(s->decoding_error==DECODING_AC_LOST){
2451
                    remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
2452
//                    printf("remove ac to %d %d\n", mb_x, mb_y);
2453
                }else{
2454
                    mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, 
2455
                                s->last_picture, 0, 0, put_pixels_tab,
2456
                                0/*mx*/, 0/*my*/, 16);
2457
                }
2458
            }
2459
            else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
2460
                int mx=0;
2461
                int my=0;
2462

    
2463
                if(s->decoding_error!=DECODING_DESYNC){
2464
                    int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
2465
                    mx= s->motion_val[ xy ][0];
2466
                    my= s->motion_val[ xy ][1];
2467
                }
2468

    
2469
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, 
2470
                            s->last_picture, 0, 0, put_pixels_tab,
2471
                            mx, my, 16);
2472
            }
2473
            s->mb_x= mb_x_backup;
2474
            s->mb_y= mb_y_backup;
2475

    
2476
            if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
2477
            if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
2478
        }
2479
        mb_x=s->mb_width-1;
2480
    }
2481
}
2482

    
2483
AVCodec mpeg1video_encoder = {
2484
    "mpeg1video",
2485
    CODEC_TYPE_VIDEO,
2486
    CODEC_ID_MPEG1VIDEO,
2487
    sizeof(MpegEncContext),
2488
    MPV_encode_init,
2489
    MPV_encode_picture,
2490
    MPV_encode_end,
2491
};
2492

    
2493
AVCodec h263_encoder = {
2494
    "h263",
2495
    CODEC_TYPE_VIDEO,
2496
    CODEC_ID_H263,
2497
    sizeof(MpegEncContext),
2498
    MPV_encode_init,
2499
    MPV_encode_picture,
2500
    MPV_encode_end,
2501
};
2502

    
2503
AVCodec h263p_encoder = {
2504
    "h263p",
2505
    CODEC_TYPE_VIDEO,
2506
    CODEC_ID_H263P,
2507
    sizeof(MpegEncContext),
2508
    MPV_encode_init,
2509
    MPV_encode_picture,
2510
    MPV_encode_end,
2511
};
2512

    
2513
AVCodec rv10_encoder = {
2514
    "rv10",
2515
    CODEC_TYPE_VIDEO,
2516
    CODEC_ID_RV10,
2517
    sizeof(MpegEncContext),
2518
    MPV_encode_init,
2519
    MPV_encode_picture,
2520
    MPV_encode_end,
2521
};
2522

    
2523
AVCodec mjpeg_encoder = {
2524
    "mjpeg",
2525
    CODEC_TYPE_VIDEO,
2526
    CODEC_ID_MJPEG,
2527
    sizeof(MpegEncContext),
2528
    MPV_encode_init,
2529
    MPV_encode_picture,
2530
    MPV_encode_end,
2531
};
2532

    
2533
AVCodec mpeg4_encoder = {
2534
    "mpeg4",
2535
    CODEC_TYPE_VIDEO,
2536
    CODEC_ID_MPEG4,
2537
    sizeof(MpegEncContext),
2538
    MPV_encode_init,
2539
    MPV_encode_picture,
2540
    MPV_encode_end,
2541
};
2542

    
2543
AVCodec msmpeg4v1_encoder = {
2544
    "msmpeg4v1",
2545
    CODEC_TYPE_VIDEO,
2546
    CODEC_ID_MSMPEG4V1,
2547
    sizeof(MpegEncContext),
2548
    MPV_encode_init,
2549
    MPV_encode_picture,
2550
    MPV_encode_end,
2551
};
2552

    
2553
AVCodec msmpeg4v2_encoder = {
2554
    "msmpeg4v2",
2555
    CODEC_TYPE_VIDEO,
2556
    CODEC_ID_MSMPEG4V2,
2557
    sizeof(MpegEncContext),
2558
    MPV_encode_init,
2559
    MPV_encode_picture,
2560
    MPV_encode_end,
2561
};
2562

    
2563
AVCodec msmpeg4v3_encoder = {
2564
    "msmpeg4",
2565
    CODEC_TYPE_VIDEO,
2566
    CODEC_ID_MSMPEG4V3,
2567
    sizeof(MpegEncContext),
2568
    MPV_encode_init,
2569
    MPV_encode_picture,
2570
    MPV_encode_end,
2571
};