Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 5e5c247a

History | View | Annotate | Download (211 KB)

1
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
20
 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
21
 */
22
 
23
/**
24
 * @file mpegvideo.c
25
 * The simplest mpeg encoder (well, it was the simplest!).
26
 */ 
27
 
28
#include <limits.h>
29
#include <math.h> //for PI
30
#include "avcodec.h"
31
#include "dsputil.h"
32
#include "mpegvideo.h"
33
#include "faandct.h"
34

    
35
#ifdef USE_FASTMEMCPY
36
#include "fastmemcpy.h"
37
#endif
38

    
39
//#undef NDEBUG
40
//#include <assert.h>
41

    
42
#ifdef CONFIG_ENCODERS
43
static void encode_picture(MpegEncContext *s, int picture_number);
44
#endif //CONFIG_ENCODERS
45
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, 
46
                                   DCTELEM *block, int n, int qscale);
47
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, 
48
                                   DCTELEM *block, int n, int qscale);
49
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
50
                                   DCTELEM *block, int n, int qscale);
51
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
52
                                   DCTELEM *block, int n, int qscale);
53
static void dct_unquantize_h263_intra_c(MpegEncContext *s, 
54
                                  DCTELEM *block, int n, int qscale);
55
static void dct_unquantize_h263_inter_c(MpegEncContext *s, 
56
                                  DCTELEM *block, int n, int qscale);
57
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
58
#ifdef CONFIG_ENCODERS
59
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
60
static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
61
static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
62
static int sse_mb(MpegEncContext *s);
63
static void  denoise_dct_c(MpegEncContext *s, DCTELEM *block);
64
#endif //CONFIG_ENCODERS
65

    
66
#ifdef HAVE_XVMC
67
extern int  XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
68
extern void XVMC_field_end(MpegEncContext *s);
69
extern void XVMC_decode_mb(MpegEncContext *s);
70
#endif
71

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

    
74

    
75
/* enable all paranoid tests for rounding, overflows, etc... */
76
//#define PARANOID
77

    
78
//#define DEBUG
79

    
80

    
81
/* for jpeg fast DCT */
82
#define CONST_BITS 14
83

    
84
static const uint16_t aanscales[64] = {
85
    /* precomputed values scaled up by 14 bits */
86
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
87
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
88
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
89
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
90
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
91
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
92
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
93
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
94
};
95

    
96
static const uint8_t h263_chroma_roundtab[16] = {
97
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
98
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
99
};
100

    
101
static const uint8_t ff_default_chroma_qscale_table[32]={
102
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
103
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
104
};
105

    
106
#ifdef CONFIG_ENCODERS
107
static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
108
static uint8_t default_fcode_tab[MAX_MV*2+1];
109

    
110
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
111

    
112
static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
113
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax)
114
{
115
    int qscale;
116

    
117
    for(qscale=qmin; qscale<=qmax; qscale++){
118
        int i;
119
        if (dsp->fdct == ff_jpeg_fdct_islow 
120
#ifdef FAAN_POSTSCALE
121
            || dsp->fdct == ff_faandct
122
#endif
123
            ) {
124
            for(i=0;i<64;i++) {
125
                const int j= dsp->idct_permutation[i];
126
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
127
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
128
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
129
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
130
                
131
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
132
                                (qscale * quant_matrix[j]));
133
            }
134
        } else if (dsp->fdct == fdct_ifast
135
#ifndef FAAN_POSTSCALE
136
                   || dsp->fdct == ff_faandct
137
#endif
138
                   ) {
139
            for(i=0;i<64;i++) {
140
                const int j= dsp->idct_permutation[i];
141
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
142
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
143
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
144
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
145
                
146
                qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 
147
                                (aanscales[i] * qscale * quant_matrix[j]));
148
            }
149
        } else {
150
            for(i=0;i<64;i++) {
151
                const int j= dsp->idct_permutation[i];
152
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
153
                   So 16           <= qscale * quant_matrix[i]             <= 7905
154
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
155
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
156
                */
157
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
158
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
159
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
160

    
161
                if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
162
                qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
163
            }
164
        }
165
    }
166
}
167

    
168
static inline void update_qscale(MpegEncContext *s){
169
    s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
170
    s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
171
    
172
    s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
173
}
174
#endif //CONFIG_ENCODERS
175

    
176
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
177
    int i;
178
    int end;
179
    
180
    st->scantable= src_scantable;
181

    
182
    for(i=0; i<64; i++){
183
        int j;
184
        j = src_scantable[i];
185
        st->permutated[i] = permutation[j];
186
#ifdef ARCH_POWERPC
187
        st->inverse[j] = i;
188
#endif
189
    }
190
    
191
    end=-1;
192
    for(i=0; i<64; i++){
193
        int j;
194
        j = st->permutated[i];
195
        if(j>end) end=j;
196
        st->raster_end[i]= end;
197
    }
198
}
199

    
200
#ifdef CONFIG_ENCODERS
201
void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
202
    int i;
203

    
204
    if(matrix){
205
        put_bits(pb, 1, 1);
206
        for(i=0;i<64;i++) {
207
            put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
208
        }
209
    }else
210
        put_bits(pb, 1, 0);
211
}
212
#endif //CONFIG_ENCODERS
213

    
214
/* init common dct for both encoder and decoder */
215
int DCT_common_init(MpegEncContext *s)
216
{
217
    s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
218
    s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
219
    s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
220
    s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
221
    s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
222
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
223

    
224
#ifdef CONFIG_ENCODERS
225
    s->dct_quantize= dct_quantize_c;
226
    s->denoise_dct= denoise_dct_c;
227
#endif
228
        
229
#ifdef HAVE_MMX
230
    MPV_common_init_mmx(s);
231
#endif
232
#ifdef ARCH_ALPHA
233
    MPV_common_init_axp(s);
234
#endif
235
#ifdef HAVE_MLIB
236
    MPV_common_init_mlib(s);
237
#endif
238
#ifdef HAVE_MMI
239
    MPV_common_init_mmi(s);
240
#endif
241
#ifdef ARCH_ARMV4L
242
    MPV_common_init_armv4l(s);
243
#endif
244
#ifdef ARCH_POWERPC
245
    MPV_common_init_ppc(s);
246
#endif
247

    
248
#ifdef CONFIG_ENCODERS
249
    s->fast_dct_quantize= s->dct_quantize;
250

    
251
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
252
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
253
    }
254

    
255
#endif //CONFIG_ENCODERS
256

    
257
    /* load & permutate scantables
258
       note: only wmv uses differnt ones 
259
    */
260
    if(s->alternate_scan){
261
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
262
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
263
    }else{
264
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
265
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
266
    }
267
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
268
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
269

    
270
    return 0;
271
}
272

    
273
static void copy_picture(Picture *dst, Picture *src){
274
    *dst = *src;
275
    dst->type= FF_BUFFER_TYPE_COPY;
276
}
277

    
278
static void copy_picture_attributes(AVFrame *dst, AVFrame *src){
279
    dst->pict_type              = src->pict_type;
280
    dst->quality                = src->quality;
281
    dst->coded_picture_number   = src->coded_picture_number;
282
    dst->display_picture_number = src->display_picture_number;
283
//    dst->reference              = src->reference;
284
    dst->pts                    = src->pts;
285
    dst->interlaced_frame       = src->interlaced_frame;
286
    dst->top_field_first        = src->top_field_first;
287
}
288

    
289
/**
290
 * allocates a Picture
291
 * The pixels are allocated/set by calling get_buffer() if shared=0
292
 */
293
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
294
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
295
    const int mb_array_size= s->mb_stride*s->mb_height;
296
    const int b8_array_size= s->b8_stride*s->mb_height*2;
297
    const int b4_array_size= s->b4_stride*s->mb_height*4;
298
    int i;
299
    
300
    if(shared){
301
        assert(pic->data[0]);
302
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
303
        pic->type= FF_BUFFER_TYPE_SHARED;
304
    }else{
305
        int r;
306
        
307
        assert(!pic->data[0]);
308
        
309
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
310
        
311
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
312
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
313
            return -1;
314
        }
315

    
316
        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
317
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
318
            return -1;
319
        }
320

    
321
        if(pic->linesize[1] != pic->linesize[2]){
322
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
323
            return -1;
324
        }
325

    
326
        s->linesize  = pic->linesize[0];
327
        s->uvlinesize= pic->linesize[1];
328
    }
329
    
330
    if(pic->qscale_table==NULL){
331
        if (s->encoding) {        
332
            CHECKED_ALLOCZ(pic->mb_var   , mb_array_size * sizeof(int16_t))
333
            CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
334
            CHECKED_ALLOCZ(pic->mb_mean  , mb_array_size * sizeof(int8_t))
335
        }
336

    
337
        CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
338
        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
339
        CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(uint32_t))
340
        pic->mb_type= pic->mb_type_base + s->mb_stride+1;
341
        if(s->out_format == FMT_H264){
342
            for(i=0; i<2; i++){
343
                CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1)  * sizeof(int16_t))
344
                pic->motion_val[i]= pic->motion_val_base[i]+1;
345
                CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t))
346
            }
347
            pic->motion_subsample_log2= 2;
348
        }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
349
            for(i=0; i<2; i++){
350
                CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(int16_t)*2) //FIXME
351
                pic->motion_val[i]= pic->motion_val_base[i]+1;
352
            }
353
            pic->motion_subsample_log2= 3;
354
        }
355
        if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
356
            CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
357
        }
358
        pic->qstride= s->mb_stride;
359
        CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
360
    }
361

    
362
    //it might be nicer if the application would keep track of these but it would require a API change
363
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
364
    s->prev_pict_types[0]= s->pict_type;
365
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
366
        pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
367
    
368
    return 0;
369
fail: //for the CHECKED_ALLOCZ macro
370
    return -1;
371
}
372

    
373
/**
374
 * deallocates a picture
375
 */
376
static void free_picture(MpegEncContext *s, Picture *pic){
377
    int i;
378

    
379
    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
380
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
381
    }
382

    
383
    av_freep(&pic->mb_var);
384
    av_freep(&pic->mc_mb_var);
385
    av_freep(&pic->mb_mean);
386
    av_freep(&pic->mbskip_table);
387
    av_freep(&pic->qscale_table);
388
    av_freep(&pic->mb_type_base);
389
    av_freep(&pic->dct_coeff);
390
    av_freep(&pic->pan_scan);
391
    pic->mb_type= NULL;
392
    for(i=0; i<2; i++){
393
        av_freep(&pic->motion_val_base[i]);
394
        av_freep(&pic->ref_index[i]);
395
    }
396
    
397
    if(pic->type == FF_BUFFER_TYPE_SHARED){
398
        for(i=0; i<4; i++){
399
            pic->base[i]=
400
            pic->data[i]= NULL;
401
        }
402
        pic->type= 0;        
403
    }
404
}
405

    
406
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
407
    int i;
408

    
409
    // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264) 
410
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
411
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
412

    
413
     //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
414
    CHECKED_ALLOCZ(s->me.scratchpad,  (s->width+64)*2*16*2*sizeof(uint8_t)) 
415
    s->rd_scratchpad=   s->me.scratchpad;
416
    s->b_scratchpad=    s->me.scratchpad;
417
    s->obmc_scratchpad= s->me.scratchpad + 16;
418
    if (s->encoding) {
419
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
420
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
421
        if(s->avctx->noise_reduction){
422
            CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
423
        }
424
    }   
425
    CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
426
    s->block= s->blocks[0];
427

    
428
    for(i=0;i<12;i++){
429
        s->pblocks[i] = (short *)(&s->block[i]);
430
    }
431
    return 0;
432
fail:
433
    return -1; //free() through MPV_common_end()
434
}
435

    
436
static void free_duplicate_context(MpegEncContext *s){
437
    if(s==NULL) return;
438

    
439
    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
440
    av_freep(&s->me.scratchpad);
441
    s->rd_scratchpad=   
442
    s->b_scratchpad=    
443
    s->obmc_scratchpad= NULL;
444
    
445
    av_freep(&s->dct_error_sum);
446
    av_freep(&s->me.map);
447
    av_freep(&s->me.score_map);
448
    av_freep(&s->blocks);
449
    s->block= NULL;
450
}
451

    
452
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
453
#define COPY(a) bak->a= src->a
454
    COPY(allocated_edge_emu_buffer);
455
    COPY(edge_emu_buffer);
456
    COPY(me.scratchpad);
457
    COPY(rd_scratchpad);
458
    COPY(b_scratchpad);
459
    COPY(obmc_scratchpad);
460
    COPY(me.map);
461
    COPY(me.score_map);
462
    COPY(blocks);
463
    COPY(block);
464
    COPY(start_mb_y);
465
    COPY(end_mb_y);
466
    COPY(me.map_generation);
467
    COPY(pb);
468
    COPY(dct_error_sum);
469
    COPY(dct_count[0]);
470
    COPY(dct_count[1]);
471
#undef COPY
472
}
473

    
474
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
475
    MpegEncContext bak;
476
    int i;
477
    //FIXME copy only needed parts
478
//START_TIMER
479
    backup_duplicate_context(&bak, dst);
480
    memcpy(dst, src, sizeof(MpegEncContext));
481
    backup_duplicate_context(dst, &bak);
482
    for(i=0;i<12;i++){
483
        dst->pblocks[i] = (short *)(&dst->block[i]);
484
    }
485
//STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
486
}
487

    
488
static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
489
#define COPY(a) dst->a= src->a
490
    COPY(pict_type);
491
    COPY(current_picture);
492
    COPY(f_code);
493
    COPY(b_code);
494
    COPY(qscale);
495
    COPY(lambda);
496
    COPY(lambda2);
497
    COPY(picture_in_gop_number);
498
    COPY(gop_picture_number);
499
    COPY(frame_pred_frame_dct); //FIXME dont set in encode_header
500
    COPY(progressive_frame); //FIXME dont set in encode_header
501
    COPY(partitioned_frame); //FIXME dont set in encode_header
502
#undef COPY
503
}
504

    
505
/* init common structure for both encoder and decoder */
506
int MPV_common_init(MpegEncContext *s)
507
{
508
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
509

    
510
    dsputil_init(&s->dsp, s->avctx);
511
    DCT_common_init(s);
512

    
513
    s->flags= s->avctx->flags;
514
    s->flags2= s->avctx->flags2;
515

    
516
    s->mb_width  = (s->width  + 15) / 16;
517
    s->mb_height = (s->height + 15) / 16;
518
    s->mb_stride = s->mb_width + 1;
519
    s->b8_stride = s->mb_width*2 + 1;
520
    s->b4_stride = s->mb_width*4 + 1;
521
    mb_array_size= s->mb_height * s->mb_stride;
522
    mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
523

    
524
    /* set default edge pos, will be overriden in decode_header if needed */
525
    s->h_edge_pos= s->mb_width*16;
526
    s->v_edge_pos= s->mb_height*16;
527

    
528
    s->mb_num = s->mb_width * s->mb_height;
529
    
530
    s->block_wrap[0]=
531
    s->block_wrap[1]=
532
    s->block_wrap[2]=
533
    s->block_wrap[3]= s->mb_width*2 + 2;
534
    s->block_wrap[4]=
535
    s->block_wrap[5]= s->mb_width + 2;
536

    
537
    s->y_dc_scale_table=
538
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
539
    s->chroma_qscale_table= ff_default_chroma_qscale_table;
540
    if( s->codec_id != CODEC_ID_MPEG1VIDEO && 
541
        s->codec_id != CODEC_ID_MPEG2VIDEO) 
542
    {
543
        /* default structure is frame */
544
        s->progressive_frame= 1;
545
        s->picture_structure= PICT_FRAME;
546

    
547
        s->y_dc_scale_table=
548
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
549
        if (!s->encoding)
550
            s->progressive_sequence= 1;
551
    }
552
    s->coded_picture_number = 0;
553

    
554
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
555
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
556
    yc_size = y_size + 2 * c_size;
557

    
558
    /* convert fourcc to upper case */
559
    s->avctx->codec_tag=   toupper( s->avctx->codec_tag     &0xFF)          
560
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
561
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 
562
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
563

    
564
    s->avctx->stream_codec_tag=   toupper( s->avctx->stream_codec_tag     &0xFF)          
565
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
566
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16) 
567
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
568

    
569
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
570

    
571
    CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
572
    for(y=0; y<s->mb_height; y++){
573
        for(x=0; x<s->mb_width; x++){
574
            s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
575
        }
576
    }
577
    s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
578
    
579
    if (s->encoding) {
580
        /* Allocate MV tables */
581
        CHECKED_ALLOCZ(s->p_mv_table_base            , mv_table_size * 2 * sizeof(int16_t))
582
        CHECKED_ALLOCZ(s->b_forw_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
583
        CHECKED_ALLOCZ(s->b_back_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
584
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
585
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
586
        CHECKED_ALLOCZ(s->b_direct_mv_table_base     , mv_table_size * 2 * sizeof(int16_t))
587
        s->p_mv_table           = s->p_mv_table_base            + s->mb_stride + 1;
588
        s->b_forw_mv_table      = s->b_forw_mv_table_base       + s->mb_stride + 1;
589
        s->b_back_mv_table      = s->b_back_mv_table_base       + s->mb_stride + 1;
590
        s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
591
        s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
592
        s->b_direct_mv_table    = s->b_direct_mv_table_base     + s->mb_stride + 1;
593

    
594
        if(s->msmpeg4_version){
595
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
596
        }
597
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
598

    
599
        /* Allocate MB type table */
600
        CHECKED_ALLOCZ(s->mb_type  , mb_array_size * sizeof(uint16_t)) //needed for encoding
601
        
602
        CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
603
        
604
        CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
605
        CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
606
        CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
607
        CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
608
        CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
609
        CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
610
        
611
        if(s->avctx->noise_reduction){
612
            CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
613
        }
614
    }
615
    CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
616

    
617
    CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
618
    
619
    if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
620
        /* interlaced direct mode decoding tables */
621
            for(i=0; i<2; i++){
622
                int j, k;
623
                for(j=0; j<2; j++){
624
                    for(k=0; k<2; k++){
625
                        CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k]     , mv_table_size * 2 * sizeof(int16_t))
626
                        s->b_field_mv_table[i][j][k]    = s->b_field_mv_table_base[i][j][k]     + s->mb_stride + 1;
627
                    }
628
                    CHECKED_ALLOCZ(s->b_field_select_table[i][j]     , mb_array_size * 2 * sizeof(uint8_t))
629
                    CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j]     , mv_table_size * 2 * sizeof(int16_t))
630
                    s->p_field_mv_table[i][j]    = s->p_field_mv_table_base[i][j]     + s->mb_stride + 1;
631
                }
632
                CHECKED_ALLOCZ(s->p_field_select_table[i]      , mb_array_size * 2 * sizeof(uint8_t))
633
            }
634
    }
635
    if (s->out_format == FMT_H263) {
636
        /* ac values */
637
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
638
        s->ac_val[1] = s->ac_val[0] + y_size;
639
        s->ac_val[2] = s->ac_val[1] + c_size;
640
        
641
        /* cbp values */
642
        CHECKED_ALLOCZ(s->coded_block, y_size);
643
        
644
        /* divx501 bitstream reorder buffer */
645
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
646

    
647
        /* cbp, ac_pred, pred_dir */
648
        CHECKED_ALLOCZ(s->cbp_table  , mb_array_size * sizeof(uint8_t))
649
        CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
650
    }
651
    
652
    if (s->h263_pred || s->h263_plus || !s->encoding) {
653
        /* dc values */
654
        //MN: we need these for error resilience of intra-frames
655
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
656
        s->dc_val[1] = s->dc_val[0] + y_size;
657
        s->dc_val[2] = s->dc_val[1] + c_size;
658
        for(i=0;i<yc_size;i++)
659
            s->dc_val[0][i] = 1024;
660
    }
661

    
662
    /* which mb is a intra block */
663
    CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
664
    memset(s->mbintra_table, 1, mb_array_size);
665
    
666
    /* default structure is frame */
667
    s->picture_structure = PICT_FRAME;
668
    
669
    /* init macroblock skip table */
670
    CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
671
    //Note the +1 is for a quicker mpeg4 slice_end detection
672
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
673
    
674
    s->parse_context.state= -1;
675
    if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
676
       s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
677
       s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
678
       s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
679
    }
680

    
681
    s->context_initialized = 1;
682

    
683
    s->thread_context[0]= s;
684
    for(i=1; i<s->avctx->thread_count; i++){
685
        s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
686
        memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
687
    }
688

    
689
    for(i=0; i<s->avctx->thread_count; i++){
690
        if(init_duplicate_context(s->thread_context[i], s) < 0)
691
           goto fail;
692
        s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
693
        s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
694
    }
695

    
696
    return 0;
697
 fail:
698
    MPV_common_end(s);
699
    return -1;
700
}
701

    
702
/* init common structure for both encoder and decoder */
703
void MPV_common_end(MpegEncContext *s)
704
{
705
    int i, j, k;
706

    
707
    for(i=0; i<s->avctx->thread_count; i++){
708
        free_duplicate_context(s->thread_context[i]);
709
    }
710
    for(i=1; i<s->avctx->thread_count; i++){
711
        av_freep(&s->thread_context[i]);
712
    }
713

    
714
    av_freep(&s->parse_context.buffer);
715
    s->parse_context.buffer_size=0;
716

    
717
    av_freep(&s->mb_type);
718
    av_freep(&s->p_mv_table_base);
719
    av_freep(&s->b_forw_mv_table_base);
720
    av_freep(&s->b_back_mv_table_base);
721
    av_freep(&s->b_bidir_forw_mv_table_base);
722
    av_freep(&s->b_bidir_back_mv_table_base);
723
    av_freep(&s->b_direct_mv_table_base);
724
    s->p_mv_table= NULL;
725
    s->b_forw_mv_table= NULL;
726
    s->b_back_mv_table= NULL;
727
    s->b_bidir_forw_mv_table= NULL;
728
    s->b_bidir_back_mv_table= NULL;
729
    s->b_direct_mv_table= NULL;
730
    for(i=0; i<2; i++){
731
        for(j=0; j<2; j++){
732
            for(k=0; k<2; k++){
733
                av_freep(&s->b_field_mv_table_base[i][j][k]);
734
                s->b_field_mv_table[i][j][k]=NULL;
735
            }
736
            av_freep(&s->b_field_select_table[i][j]);
737
            av_freep(&s->p_field_mv_table_base[i][j]);
738
            s->p_field_mv_table[i][j]=NULL;
739
        }
740
        av_freep(&s->p_field_select_table[i]);
741
    }
742
    
743
    av_freep(&s->dc_val[0]);
744
    av_freep(&s->ac_val[0]);
745
    av_freep(&s->coded_block);
746
    av_freep(&s->mbintra_table);
747
    av_freep(&s->cbp_table);
748
    av_freep(&s->pred_dir_table);
749
    
750
    av_freep(&s->mbskip_table);
751
    av_freep(&s->prev_pict_types);
752
    av_freep(&s->bitstream_buffer);
753
    av_freep(&s->avctx->stats_out);
754
    av_freep(&s->ac_stats);
755
    av_freep(&s->error_status_table);
756
    av_freep(&s->mb_index2xy);
757
    av_freep(&s->lambda_table);
758
    av_freep(&s->q_intra_matrix);
759
    av_freep(&s->q_inter_matrix);
760
    av_freep(&s->q_intra_matrix16);
761
    av_freep(&s->q_inter_matrix16);
762
    av_freep(&s->input_picture);
763
    av_freep(&s->reordered_input_picture);
764
    av_freep(&s->dct_offset);
765

    
766
    if(s->picture){
767
        for(i=0; i<MAX_PICTURE_COUNT; i++){
768
            free_picture(s, &s->picture[i]);
769
        }
770
    }
771
    av_freep(&s->picture);
772
    avcodec_default_free_buffers(s->avctx);
773
    s->context_initialized = 0;
774
    s->last_picture_ptr=
775
    s->next_picture_ptr=
776
    s->current_picture_ptr= NULL;
777
    for(i=0; i<3; i++)
778
        if (s->visualization_buffer[i])
779
            av_free(s->visualization_buffer[i]);
780
}
781

    
782
#ifdef CONFIG_ENCODERS
783

    
784
/* init video encoder */
785
int MPV_encode_init(AVCodecContext *avctx)
786
{
787
    MpegEncContext *s = avctx->priv_data;
788
    int i, dummy;
789
    int chroma_h_shift, chroma_v_shift;
790

    
791
    avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
792

    
793
    s->bit_rate = avctx->bit_rate;
794
    s->width = avctx->width;
795
    s->height = avctx->height;
796
    if(avctx->gop_size > 600){
797
        av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
798
        avctx->gop_size=600;
799
    }
800
    s->gop_size = avctx->gop_size;
801
    s->avctx = avctx;
802
    s->flags= avctx->flags;
803
    s->flags2= avctx->flags2;
804
    s->max_b_frames= avctx->max_b_frames;
805
    s->codec_id= avctx->codec->id;
806
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
807
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
808
    s->strict_std_compliance= avctx->strict_std_compliance;
809
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
810
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
811
    s->mpeg_quant= avctx->mpeg_quant;
812
    s->rtp_mode= !!avctx->rtp_payload_size;
813

    
814
    if (s->gop_size <= 1) {
815
        s->intra_only = 1;
816
        s->gop_size = 12;
817
    } else {
818
        s->intra_only = 0;
819
    }
820

    
821
    s->me_method = avctx->me_method;
822

    
823
    /* Fixed QSCALE */
824
    s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
825
    
826
    s->adaptive_quant= (   s->avctx->lumi_masking
827
                        || s->avctx->dark_masking
828
                        || s->avctx->temporal_cplx_masking 
829
                        || s->avctx->spatial_cplx_masking
830
                        || s->avctx->p_masking
831
                        || (s->flags&CODEC_FLAG_QP_RD))
832
                       && !s->fixed_qscale;
833
    
834
    s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
835
    s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
836
    s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
837

    
838
    if(avctx->rc_max_rate && !avctx->rc_buffer_size){
839
        av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
840
        return -1;
841
    }    
842

    
843
    if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
844
        av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isnt recommanded!\n");
845
    }    
846
        
847
    if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 
848
       && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
849
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
850
        return -1;
851
    }
852
        
853
    if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
854
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
855
        return -1;
856
    }
857
    
858
    if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
859
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
860
        return -1;
861
    }
862
    
863
    if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
864
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
865
        return -1;
866
    }
867

    
868
    if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
869
        av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
870
        return -1;
871
    }
872
    
873
    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
874
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
875
        return -1;
876
    }
877
    
878
    if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
879
        av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
880
        return -1;
881
    }
882
        
883
    if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
884
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
885
        return -1;
886
    }
887

    
888
    if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
889
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
890
        return -1;
891
    }
892
    
893
    if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
894
        av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n");
895
        return -1;
896
    }
897
    
898
    if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4 
899
       && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO 
900
       && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
901
        av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
902
        return -1;
903
    }
904
    
905
    if(s->avctx->thread_count > MAX_THREADS || 16*s->avctx->thread_count > s->height){
906
        av_log(avctx, AV_LOG_ERROR, "too many threads\n");
907
        return -1;
908
    }
909
    
910
    if(s->avctx->thread_count > 1)
911
        s->rtp_mode= 1;
912

    
913
    i= ff_gcd(avctx->frame_rate, avctx->frame_rate_base);
914
    if(i > 1){
915
        av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
916
        avctx->frame_rate /= i;
917
        avctx->frame_rate_base /= i;
918
//        return -1;
919
    }
920
    
921
    if(s->codec_id==CODEC_ID_MJPEG){
922
        s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
923
        s->inter_quant_bias= 0;
924
    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
925
        s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
926
        s->inter_quant_bias= 0;
927
    }else{
928
        s->intra_quant_bias=0;
929
        s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
930
    }
931
    
932
    if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
933
        s->intra_quant_bias= avctx->intra_quant_bias;
934
    if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
935
        s->inter_quant_bias= avctx->inter_quant_bias;
936
        
937
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
938

    
939
    av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
940
    s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
941

    
942
    switch(avctx->codec->id) {
943
    case CODEC_ID_MPEG1VIDEO:
944
        s->out_format = FMT_MPEG1;
945
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
946
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
947
        break;
948
    case CODEC_ID_MPEG2VIDEO:
949
        s->out_format = FMT_MPEG1;
950
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
951
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
952
        s->rtp_mode= 1;
953
        break;
954
    case CODEC_ID_LJPEG:
955
    case CODEC_ID_MJPEG:
956
        s->out_format = FMT_MJPEG;
957
        s->intra_only = 1; /* force intra only for jpeg */
958
        s->mjpeg_write_tables = 1; /* write all tables */
959
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
960
        s->mjpeg_vsample[0] = 1<<chroma_v_shift;
961
        s->mjpeg_vsample[1] = 1;
962
        s->mjpeg_vsample[2] = 1; 
963
        s->mjpeg_hsample[0] = 1<<chroma_h_shift;
964
        s->mjpeg_hsample[1] = 1; 
965
        s->mjpeg_hsample[2] = 1; 
966
        if (mjpeg_init(s) < 0)
967
            return -1;
968
        avctx->delay=0;
969
        s->low_delay=1;
970
        break;
971
#ifdef CONFIG_RISKY
972
    case CODEC_ID_H263:
973
        if (h263_get_picture_format(s->width, s->height) == 7) {
974
            av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
975
            return -1;
976
        }
977
        s->out_format = FMT_H263;
978
        s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
979
        avctx->delay=0;
980
        s->low_delay=1;
981
        break;
982
    case CODEC_ID_H263P:
983
        s->out_format = FMT_H263;
984
        s->h263_plus = 1;
985
        /* Fx */
986
        s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
987
        s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
988
        s->modified_quant= s->h263_aic;
989
        s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
990
        s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
991
        s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
992
        s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
993
        s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
994

    
995
        /* /Fx */
996
        /* These are just to be sure */
997
        avctx->delay=0;
998
        s->low_delay=1;
999
        break;
1000
    case CODEC_ID_FLV1:
1001
        s->out_format = FMT_H263;
1002
        s->h263_flv = 2; /* format = 1; 11-bit codes */
1003
        s->unrestricted_mv = 1;
1004
        s->rtp_mode=0; /* don't allow GOB */
1005
        avctx->delay=0;
1006
        s->low_delay=1;
1007
        break;
1008
    case CODEC_ID_RV10:
1009
        s->out_format = FMT_H263;
1010
        avctx->delay=0;
1011
        s->low_delay=1;
1012
        break;
1013
    case CODEC_ID_MPEG4:
1014
        s->out_format = FMT_H263;
1015
        s->h263_pred = 1;
1016
        s->unrestricted_mv = 1;
1017
        s->low_delay= s->max_b_frames ? 0 : 1;
1018
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
1019
        break;
1020
    case CODEC_ID_MSMPEG4V1:
1021
        s->out_format = FMT_H263;
1022
        s->h263_msmpeg4 = 1;
1023
        s->h263_pred = 1;
1024
        s->unrestricted_mv = 1;
1025
        s->msmpeg4_version= 1;
1026
        avctx->delay=0;
1027
        s->low_delay=1;
1028
        break;
1029
    case CODEC_ID_MSMPEG4V2:
1030
        s->out_format = FMT_H263;
1031
        s->h263_msmpeg4 = 1;
1032
        s->h263_pred = 1;
1033
        s->unrestricted_mv = 1;
1034
        s->msmpeg4_version= 2;
1035
        avctx->delay=0;
1036
        s->low_delay=1;
1037
        break;
1038
    case CODEC_ID_MSMPEG4V3:
1039
        s->out_format = FMT_H263;
1040
        s->h263_msmpeg4 = 1;
1041
        s->h263_pred = 1;
1042
        s->unrestricted_mv = 1;
1043
        s->msmpeg4_version= 3;
1044
        s->flipflop_rounding=1;
1045
        avctx->delay=0;
1046
        s->low_delay=1;
1047
        break;
1048
    case CODEC_ID_WMV1:
1049
        s->out_format = FMT_H263;
1050
        s->h263_msmpeg4 = 1;
1051
        s->h263_pred = 1;
1052
        s->unrestricted_mv = 1;
1053
        s->msmpeg4_version= 4;
1054
        s->flipflop_rounding=1;
1055
        avctx->delay=0;
1056
        s->low_delay=1;
1057
        break;
1058
    case CODEC_ID_WMV2:
1059
        s->out_format = FMT_H263;
1060
        s->h263_msmpeg4 = 1;
1061
        s->h263_pred = 1;
1062
        s->unrestricted_mv = 1;
1063
        s->msmpeg4_version= 5;
1064
        s->flipflop_rounding=1;
1065
        avctx->delay=0;
1066
        s->low_delay=1;
1067
        break;
1068
#endif
1069
    default:
1070
        return -1;
1071
    }
1072

    
1073
    { /* set up some save defaults, some codecs might override them later */
1074
        static int done=0;
1075
        if(!done){
1076
            int i;
1077
            done=1;
1078

    
1079
            default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
1080
            memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
1081
            memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
1082

    
1083
            for(i=-16; i<16; i++){
1084
                default_fcode_tab[i + MAX_MV]= 1;
1085
            }
1086
        }
1087
    }
1088
    s->me.mv_penalty= default_mv_penalty;
1089
    s->fcode_tab= default_fcode_tab;
1090
 
1091
    /* dont use mv_penalty table for crap MV as it would be confused */
1092
    //FIXME remove after fixing / removing old ME
1093
    if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
1094

    
1095
    s->encoding = 1;
1096

    
1097
    /* init */
1098
    if (MPV_common_init(s) < 0)
1099
        return -1;
1100

    
1101
    if(s->modified_quant)
1102
        s->chroma_qscale_table= ff_h263_chroma_qscale_table;
1103
    s->progressive_frame= 
1104
    s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
1105
    s->quant_precision=5;
1106
    
1107
    ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
1108
    
1109
    ff_init_me(s);
1110

    
1111
#ifdef CONFIG_ENCODERS
1112
#ifdef CONFIG_RISKY
1113
    if (s->out_format == FMT_H263)
1114
        h263_encode_init(s);
1115
    if(s->msmpeg4_version)
1116
        ff_msmpeg4_encode_init(s);
1117
#endif
1118
    if (s->out_format == FMT_MPEG1)
1119
        ff_mpeg1_encode_init(s);
1120
#endif
1121

    
1122
    /* init default q matrix */
1123
    for(i=0;i<64;i++) {
1124
        int j= s->dsp.idct_permutation[i];
1125
#ifdef CONFIG_RISKY
1126
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
1127
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
1128
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
1129
        }else if(s->out_format == FMT_H263){
1130
            s->intra_matrix[j] =
1131
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1132
        }else
1133
#endif
1134
        { /* mpeg1/2 */
1135
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
1136
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1137
        }
1138
        if(s->avctx->intra_matrix)
1139
            s->intra_matrix[j] = s->avctx->intra_matrix[i];
1140
        if(s->avctx->inter_matrix)
1141
            s->inter_matrix[j] = s->avctx->inter_matrix[i];
1142
    }
1143

    
1144
    /* precompute matrix */
1145
    /* for mjpeg, we do include qscale in the matrix */
1146
    if (s->out_format != FMT_MJPEG) {
1147
        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 
1148
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
1149
        convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, 
1150
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
1151
    }
1152

    
1153
    if(ff_rate_control_init(s) < 0)
1154
        return -1;
1155

    
1156
    s->picture_number = 0;
1157
    s->input_picture_number = 0;
1158
    s->picture_in_gop_number = 0;
1159
    /* motion detector init */
1160
    s->f_code = 1;
1161
    s->b_code = 1;
1162

    
1163
    return 0;
1164
}
1165

    
1166
int MPV_encode_end(AVCodecContext *avctx)
1167
{
1168
    MpegEncContext *s = avctx->priv_data;
1169

    
1170
#ifdef STATS
1171
    print_stats();
1172
#endif
1173

    
1174
    ff_rate_control_uninit(s);
1175

    
1176
    MPV_common_end(s);
1177
    if (s->out_format == FMT_MJPEG)
1178
        mjpeg_close(s);
1179

    
1180
    av_freep(&avctx->extradata);
1181
      
1182
    return 0;
1183
}
1184

    
1185
#endif //CONFIG_ENCODERS
1186

    
1187
void init_rl(RLTable *rl)
1188
{
1189
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1190
    uint8_t index_run[MAX_RUN+1];
1191
    int last, run, level, start, end, i;
1192

    
1193
    /* compute max_level[], max_run[] and index_run[] */
1194
    for(last=0;last<2;last++) {
1195
        if (last == 0) {
1196
            start = 0;
1197
            end = rl->last;
1198
        } else {
1199
            start = rl->last;
1200
            end = rl->n;
1201
        }
1202

    
1203
        memset(max_level, 0, MAX_RUN + 1);
1204
        memset(max_run, 0, MAX_LEVEL + 1);
1205
        memset(index_run, rl->n, MAX_RUN + 1);
1206
        for(i=start;i<end;i++) {
1207
            run = rl->table_run[i];
1208
            level = rl->table_level[i];
1209
            if (index_run[run] == rl->n)
1210
                index_run[run] = i;
1211
            if (level > max_level[run])
1212
                max_level[run] = level;
1213
            if (run > max_run[level])
1214
                max_run[level] = run;
1215
        }
1216
        rl->max_level[last] = av_malloc(MAX_RUN + 1);
1217
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1218
        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1219
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1220
        rl->index_run[last] = av_malloc(MAX_RUN + 1);
1221
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1222
    }
1223
}
1224

    
1225
/* draw the edges of width 'w' of an image of size width, height */
1226
//FIXME check that this is ok for mpeg4 interlaced
1227
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
1228
{
1229
    uint8_t *ptr, *last_line;
1230
    int i;
1231

    
1232
    last_line = buf + (height - 1) * wrap;
1233
    for(i=0;i<w;i++) {
1234
        /* top and bottom */
1235
        memcpy(buf - (i + 1) * wrap, buf, width);
1236
        memcpy(last_line + (i + 1) * wrap, last_line, width);
1237
    }
1238
    /* left and right */
1239
    ptr = buf;
1240
    for(i=0;i<height;i++) {
1241
        memset(ptr - w, ptr[0], w);
1242
        memset(ptr + width, ptr[width-1], w);
1243
        ptr += wrap;
1244
    }
1245
    /* corners */
1246
    for(i=0;i<w;i++) {
1247
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1248
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1249
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1250
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1251
    }
1252
}
1253

    
1254
int ff_find_unused_picture(MpegEncContext *s, int shared){
1255
    int i;
1256
    
1257
    if(shared){
1258
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1259
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
1260
        }
1261
    }else{
1262
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1263
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
1264
        }
1265
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1266
            if(s->picture[i].data[0]==NULL) return i;
1267
        }
1268
    }
1269

    
1270
    assert(0);
1271
    return -1;
1272
}
1273

    
1274
static void update_noise_reduction(MpegEncContext *s){
1275
    int intra, i;
1276

    
1277
    for(intra=0; intra<2; intra++){
1278
        if(s->dct_count[intra] > (1<<16)){
1279
            for(i=0; i<64; i++){
1280
                s->dct_error_sum[intra][i] >>=1;
1281
            }
1282
            s->dct_count[intra] >>= 1;
1283
        }
1284
        
1285
        for(i=0; i<64; i++){
1286
            s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1287
        }
1288
    }
1289
}
1290

    
1291
/**
1292
 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1293
 */
1294
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1295
{
1296
    int i;
1297
    AVFrame *pic;
1298
    s->mb_skiped = 0;
1299

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

    
1302
    /* mark&release old frames */
1303
    if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
1304
        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1305

    
1306
        /* release forgotten pictures */
1307
        /* if(mpeg124/h263) */
1308
        if(!s->encoding){
1309
            for(i=0; i<MAX_PICTURE_COUNT; i++){
1310
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1311
                    av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
1312
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
1313
                }
1314
            }
1315
        }
1316
    }
1317
alloc:
1318
    if(!s->encoding){
1319
        /* release non refernce frames */
1320
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1321
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1322
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1323
            }
1324
        }
1325

    
1326
        if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1327
            pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1328
        else{
1329
            i= ff_find_unused_picture(s, 0);
1330
            pic= (AVFrame*)&s->picture[i];
1331
        }
1332

    
1333
        pic->reference= s->pict_type != B_TYPE ? 3 : 0;
1334

    
1335
        pic->coded_picture_number= s->coded_picture_number++;
1336
        
1337
        if( alloc_picture(s, (Picture*)pic, 0) < 0)
1338
            return -1;
1339

    
1340
        s->current_picture_ptr= (Picture*)pic;
1341
        s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
1342
        s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
1343
    }
1344

    
1345
    s->current_picture_ptr->pict_type= s->pict_type;
1346
//    if(s->flags && CODEC_FLAG_QSCALE) 
1347
  //      s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1348
    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1349

    
1350
    copy_picture(&s->current_picture, s->current_picture_ptr);
1351
  
1352
  if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1353
    if (s->pict_type != B_TYPE) {
1354
        s->last_picture_ptr= s->next_picture_ptr;
1355
        s->next_picture_ptr= s->current_picture_ptr;
1356
    }
1357
    
1358
    if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1359
    if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1360
    
1361
    if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1362
        av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
1363
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1364
        goto alloc;
1365
    }
1366

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

    
1369
    if(s->picture_structure!=PICT_FRAME){
1370
        int i;
1371
        for(i=0; i<4; i++){
1372
            if(s->picture_structure == PICT_BOTTOM_FIELD){
1373
                 s->current_picture.data[i] += s->current_picture.linesize[i];
1374
            } 
1375
            s->current_picture.linesize[i] *= 2;
1376
            s->last_picture.linesize[i] *=2;
1377
            s->next_picture.linesize[i] *=2;
1378
        }
1379
    }
1380
  }
1381
   
1382
    s->hurry_up= s->avctx->hurry_up;
1383
    s->error_resilience= avctx->error_resilience;
1384

    
1385
    /* set dequantizer, we cant do it during init as it might change for mpeg4
1386
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1387
    if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
1388
        s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1389
        s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1390
    }else if(s->out_format == FMT_H263){
1391
        s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1392
        s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1393
    }else{
1394
        s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1395
        s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1396
    }
1397

    
1398
    if(s->dct_error_sum){
1399
        assert(s->avctx->noise_reduction && s->encoding);
1400

    
1401
        update_noise_reduction(s);
1402
    }
1403
        
1404
#ifdef HAVE_XVMC
1405
    if(s->avctx->xvmc_acceleration)
1406
        return XVMC_field_start(s, avctx);
1407
#endif
1408
    return 0;
1409
}
1410

    
1411
/* generic function for encode/decode called after a frame has been coded/decoded */
1412
void MPV_frame_end(MpegEncContext *s)
1413
{
1414
    int i;
1415
    /* draw edge for correct motion prediction if outside */
1416
#ifdef HAVE_XVMC
1417
//just to make sure that all data is rendered.
1418
    if(s->avctx->xvmc_acceleration){
1419
        XVMC_field_end(s);
1420
    }else
1421
#endif
1422
    if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1423
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
1424
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1425
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1426
    }
1427
    emms_c();
1428
    
1429
    s->last_pict_type    = s->pict_type;
1430
    if(s->pict_type!=B_TYPE){
1431
        s->last_non_b_pict_type= s->pict_type;
1432
    }
1433
#if 0
1434
        /* copy back current_picture variables */
1435
    for(i=0; i<MAX_PICTURE_COUNT; i++){
1436
        if(s->picture[i].data[0] == s->current_picture.data[0]){
1437
            s->picture[i]= s->current_picture;
1438
            break;
1439
        }    
1440
    }
1441
    assert(i<MAX_PICTURE_COUNT);
1442
#endif    
1443

    
1444
    if(s->encoding){
1445
        /* release non refernce frames */
1446
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1447
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1448
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1449
            }
1450
        }
1451
    }
1452
    // clear copies, to avoid confusion
1453
#if 0
1454
    memset(&s->last_picture, 0, sizeof(Picture));
1455
    memset(&s->next_picture, 0, sizeof(Picture));
1456
    memset(&s->current_picture, 0, sizeof(Picture));
1457
#endif
1458
}
1459

    
1460
/**
1461
 * draws an line from (ex, ey) -> (sx, sy).
1462
 * @param w width of the image
1463
 * @param h height of the image
1464
 * @param stride stride/linesize of the image
1465
 * @param color color of the arrow
1466
 */
1467
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1468
    int t, x, y, f;
1469
    
1470
    sx= clip(sx, 0, w-1);
1471
    sy= clip(sy, 0, h-1);
1472
    ex= clip(ex, 0, w-1);
1473
    ey= clip(ey, 0, h-1);
1474
    
1475
    buf[sy*stride + sx]+= color;
1476
    
1477
    if(ABS(ex - sx) > ABS(ey - sy)){
1478
        if(sx > ex){
1479
            t=sx; sx=ex; ex=t;
1480
            t=sy; sy=ey; ey=t;
1481
        }
1482
        buf+= sx + sy*stride;
1483
        ex-= sx;
1484
        f= ((ey-sy)<<16)/ex;
1485
        for(x= 0; x <= ex; x++){
1486
            y= ((x*f) + (1<<15))>>16;
1487
            buf[y*stride + x]+= color;
1488
        }
1489
    }else{
1490
        if(sy > ey){
1491
            t=sx; sx=ex; ex=t;
1492
            t=sy; sy=ey; ey=t;
1493
        }
1494
        buf+= sx + sy*stride;
1495
        ey-= sy;
1496
        if(ey) f= ((ex-sx)<<16)/ey;
1497
        else   f= 0;
1498
        for(y= 0; y <= ey; y++){
1499
            x= ((y*f) + (1<<15))>>16;
1500
            buf[y*stride + x]+= color;
1501
        }
1502
    }
1503
}
1504

    
1505
/**
1506
 * draws an arrow from (ex, ey) -> (sx, sy).
1507
 * @param w width of the image
1508
 * @param h height of the image
1509
 * @param stride stride/linesize of the image
1510
 * @param color color of the arrow
1511
 */
1512
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ 
1513
    int dx,dy;
1514

    
1515
    sx= clip(sx, -100, w+100);
1516
    sy= clip(sy, -100, h+100);
1517
    ex= clip(ex, -100, w+100);
1518
    ey= clip(ey, -100, h+100);
1519
    
1520
    dx= ex - sx;
1521
    dy= ey - sy;
1522
    
1523
    if(dx*dx + dy*dy > 3*3){
1524
        int rx=  dx + dy;
1525
        int ry= -dx + dy;
1526
        int length= ff_sqrt((rx*rx + ry*ry)<<8);
1527
        
1528
        //FIXME subpixel accuracy
1529
        rx= ROUNDED_DIV(rx*3<<4, length);
1530
        ry= ROUNDED_DIV(ry*3<<4, length);
1531
        
1532
        draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1533
        draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1534
    }
1535
    draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1536
}
1537

    
1538
/**
1539
 * prints debuging info for the given picture.
1540
 */
1541
void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
1542

    
1543
    if(!pict || !pict->mb_type) return;
1544

    
1545
    if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1546
        int x,y;
1547
        
1548
        av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1549
        switch (pict->pict_type) {
1550
            case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
1551
            case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
1552
            case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
1553
            case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
1554
            case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
1555
            case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;            
1556
        }
1557
        for(y=0; y<s->mb_height; y++){
1558
            for(x=0; x<s->mb_width; x++){
1559
                if(s->avctx->debug&FF_DEBUG_SKIP){
1560
                    int count= s->mbskip_table[x + y*s->mb_stride];
1561
                    if(count>9) count=9;
1562
                    av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1563
                }
1564
                if(s->avctx->debug&FF_DEBUG_QP){
1565
                    av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
1566
                }
1567
                if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1568
                    int mb_type= pict->mb_type[x + y*s->mb_stride];
1569
                    //Type & MV direction
1570
                    if(IS_PCM(mb_type))
1571
                        av_log(s->avctx, AV_LOG_DEBUG, "P");
1572
                    else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1573
                        av_log(s->avctx, AV_LOG_DEBUG, "A");
1574
                    else if(IS_INTRA4x4(mb_type))
1575
                        av_log(s->avctx, AV_LOG_DEBUG, "i");
1576
                    else if(IS_INTRA16x16(mb_type))
1577
                        av_log(s->avctx, AV_LOG_DEBUG, "I");
1578
                    else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1579
                        av_log(s->avctx, AV_LOG_DEBUG, "d");
1580
                    else if(IS_DIRECT(mb_type))
1581
                        av_log(s->avctx, AV_LOG_DEBUG, "D");
1582
                    else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1583
                        av_log(s->avctx, AV_LOG_DEBUG, "g");
1584
                    else if(IS_GMC(mb_type))
1585
                        av_log(s->avctx, AV_LOG_DEBUG, "G");
1586
                    else if(IS_SKIP(mb_type))
1587
                        av_log(s->avctx, AV_LOG_DEBUG, "S");
1588
                    else if(!USES_LIST(mb_type, 1))
1589
                        av_log(s->avctx, AV_LOG_DEBUG, ">");
1590
                    else if(!USES_LIST(mb_type, 0))
1591
                        av_log(s->avctx, AV_LOG_DEBUG, "<");
1592
                    else{
1593
                        assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1594
                        av_log(s->avctx, AV_LOG_DEBUG, "X");
1595
                    }
1596
                    
1597
                    //segmentation
1598
                    if(IS_8X8(mb_type))
1599
                        av_log(s->avctx, AV_LOG_DEBUG, "+");
1600
                    else if(IS_16X8(mb_type))
1601
                        av_log(s->avctx, AV_LOG_DEBUG, "-");
1602
                    else if(IS_8X16(mb_type))
1603
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1604
                    else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1605
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1606
                    else
1607
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1608
                    
1609
                        
1610
                    if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1611
                        av_log(s->avctx, AV_LOG_DEBUG, "=");
1612
                    else
1613
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1614
                }
1615
//                av_log(s->avctx, AV_LOG_DEBUG, " ");
1616
            }
1617
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
1618
        }
1619
    }
1620

    
1621
    if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
1622
        const int shift= 1 + s->quarter_sample;
1623
        int mb_y;
1624
        uint8_t *ptr;
1625
        int i;
1626
        int h_chroma_shift, v_chroma_shift;
1627
        s->low_delay=0; //needed to see the vectors without trashing the buffers
1628

    
1629
        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1630
        for(i=0; i<3; i++){
1631
            memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*s->height:pict->linesize[i]*s->height >> v_chroma_shift);
1632
            pict->data[i]= s->visualization_buffer[i];
1633
        }
1634
        pict->type= FF_BUFFER_TYPE_COPY;
1635
        ptr= pict->data[0];
1636

    
1637
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
1638
            int mb_x;
1639
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
1640
                const int mb_index= mb_x + mb_y*s->mb_stride;
1641
                if((s->avctx->debug_mv) && pict->motion_val){
1642
                  int type;
1643
                  for(type=0; type<3; type++){
1644
                    int direction;
1645
                    switch (type) {
1646
                      case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
1647
                                continue;
1648
                              direction = 0;
1649
                              break;
1650
                      case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
1651
                                continue;
1652
                              direction = 0;
1653
                              break;
1654
                      case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
1655
                                continue;
1656
                              direction = 1;
1657
                              break;
1658
                    }
1659
                    if(!USES_LIST(pict->mb_type[mb_index], direction))
1660
                        continue;
1661

    
1662
                    if(IS_8X8(pict->mb_type[mb_index])){
1663
                      int i;
1664
                      for(i=0; i<4; i++){
1665
                        int sx= mb_x*16 + 4 + 8*(i&1);
1666
                        int sy= mb_y*16 + 4 + 8*(i>>1);
1667
                        int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1668
                        int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1669
                        int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1670
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1671
                      }
1672
                    }else if(IS_16X8(pict->mb_type[mb_index])){
1673
                      int i;
1674
                      for(i=0; i<2; i++){
1675
                        int sx=mb_x*16 + 8;
1676
                        int sy=mb_y*16 + 4 + 8*i;
1677
                        int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
1678
                        int mx=(pict->motion_val[direction][xy][0]>>shift) + sx;
1679
                        int my=(pict->motion_val[direction][xy][1]>>shift) + sy;
1680
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1681
                      }
1682
                    }else{
1683
                      int sx= mb_x*16 + 8;
1684
                      int sy= mb_y*16 + 8;
1685
                      int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1686
                      int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1687
                      int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1688
                      draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1689
                    }
1690
                  }                  
1691
                }
1692
                if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
1693
                    uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
1694
                    int y;
1695
                    for(y=0; y<8; y++){
1696
                        *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
1697
                        *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
1698
                    }
1699
                }
1700
                if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
1701
                    int mb_type= pict->mb_type[mb_index];
1702
                    uint64_t u,v;
1703
                    int y;
1704
#define COLOR(theta, r)\
1705
u= (int)(128 + r*cos(theta*3.141592/180));\
1706
v= (int)(128 + r*sin(theta*3.141592/180));
1707

    
1708
                    
1709
                    u=v=128;
1710
                    if(IS_PCM(mb_type)){
1711
                        COLOR(120,48)
1712
                    }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
1713
                        COLOR(30,48)
1714
                    }else if(IS_INTRA4x4(mb_type)){
1715
                        COLOR(90,48)
1716
                    }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
1717
//                        COLOR(120,48)
1718
                    }else if(IS_DIRECT(mb_type)){
1719
                        COLOR(150,48)
1720
                    }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
1721
                        COLOR(170,48)
1722
                    }else if(IS_GMC(mb_type)){
1723
                        COLOR(190,48)
1724
                    }else if(IS_SKIP(mb_type)){
1725
//                        COLOR(180,48)
1726
                    }else if(!USES_LIST(mb_type, 1)){
1727
                        COLOR(240,48)
1728
                    }else if(!USES_LIST(mb_type, 0)){
1729
                        COLOR(0,48)
1730
                    }else{
1731
                        assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1732
                        COLOR(300,48)
1733
                    }
1734

    
1735
                    u*= 0x0101010101010101ULL;
1736
                    v*= 0x0101010101010101ULL;
1737
                    for(y=0; y<8; y++){
1738
                        *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
1739
                        *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
1740
                    }
1741

    
1742
                    //segmentation
1743
                    if(IS_8X8(mb_type) || IS_16X8(mb_type)){
1744
                        *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
1745
                        *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
1746
                    }
1747
                    if(IS_8X8(mb_type) || IS_8X16(mb_type)){
1748
                        for(y=0; y<16; y++)
1749
                            pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
1750
                    }
1751
                        
1752
                    if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
1753
                        // hmm
1754
                    }
1755
                }
1756
                s->mbskip_table[mb_index]=0;
1757
            }
1758
        }
1759
    }
1760
}
1761

    
1762
#ifdef CONFIG_ENCODERS
1763

    
1764
static int get_sae(uint8_t *src, int ref, int stride){
1765
    int x,y;
1766
    int acc=0;
1767
    
1768
    for(y=0; y<16; y++){
1769
        for(x=0; x<16; x++){
1770
            acc+= ABS(src[x+y*stride] - ref);
1771
        }
1772
    }
1773
    
1774
    return acc;
1775
}
1776

    
1777
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1778
    int x, y, w, h;
1779
    int acc=0;
1780
    
1781
    w= s->width &~15;
1782
    h= s->height&~15;
1783
    
1784
    for(y=0; y<h; y+=16){
1785
        for(x=0; x<w; x+=16){
1786
            int offset= x + y*stride;
1787
            int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
1788
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1789
            int sae = get_sae(src + offset, mean, stride);
1790
            
1791
            acc+= sae + 500 < sad;
1792
        }
1793
    }
1794
    return acc;
1795
}
1796

    
1797

    
1798
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1799
    AVFrame *pic=NULL;
1800
    int i;
1801
    const int encoding_delay= s->max_b_frames;
1802
    int direct=1;
1803
    
1804
  if(pic_arg){
1805
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1806
    if(pic_arg->linesize[0] != s->linesize) direct=0;
1807
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1808
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1809
  
1810
//    av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1811
    
1812
    if(direct){
1813
        i= ff_find_unused_picture(s, 1);
1814

    
1815
        pic= (AVFrame*)&s->picture[i];
1816
        pic->reference= 3;
1817
    
1818
        for(i=0; i<4; i++){
1819
            pic->data[i]= pic_arg->data[i];
1820
            pic->linesize[i]= pic_arg->linesize[i];
1821
        }
1822
        alloc_picture(s, (Picture*)pic, 1);
1823
    }else{
1824
        int offset= 16;
1825
        i= ff_find_unused_picture(s, 0);
1826

    
1827
        pic= (AVFrame*)&s->picture[i];
1828
        pic->reference= 3;
1829

    
1830
        alloc_picture(s, (Picture*)pic, 0);
1831

    
1832
        if(   pic->data[0] + offset == pic_arg->data[0] 
1833
           && pic->data[1] + offset == pic_arg->data[1]
1834
           && pic->data[2] + offset == pic_arg->data[2]){
1835
       // empty
1836
        }else{
1837
            int h_chroma_shift, v_chroma_shift;
1838
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1839
        
1840
            for(i=0; i<3; i++){
1841
                int src_stride= pic_arg->linesize[i];
1842
                int dst_stride= i ? s->uvlinesize : s->linesize;
1843
                int h_shift= i ? h_chroma_shift : 0;
1844
                int v_shift= i ? v_chroma_shift : 0;
1845
                int w= s->width >>h_shift;
1846
                int h= s->height>>v_shift;
1847
                uint8_t *src= pic_arg->data[i];
1848
                uint8_t *dst= pic->data[i] + offset;
1849
            
1850
                if(src_stride==dst_stride)
1851
                    memcpy(dst, src, src_stride*h);
1852
                else{
1853
                    while(h--){
1854
                        memcpy(dst, src, w);
1855
                        dst += dst_stride;
1856
                        src += src_stride;
1857
                    }
1858
                }
1859
            }
1860
        }
1861
    }
1862
    copy_picture_attributes(pic, pic_arg);
1863
    
1864
    pic->display_picture_number= s->input_picture_number++;
1865
    if(pic->pts != AV_NOPTS_VALUE){ 
1866
        s->user_specified_pts= pic->pts;
1867
    }else{
1868
        if(s->user_specified_pts){
1869
            pic->pts= s->user_specified_pts + AV_TIME_BASE*(int64_t)s->avctx->frame_rate_base / s->avctx->frame_rate;
1870
            av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%Ld)\n", pic->pts);
1871
        }else{
1872
            pic->pts= av_rescale(pic->display_picture_number*(int64_t)s->avctx->frame_rate_base, AV_TIME_BASE, s->avctx->frame_rate);
1873
        }
1874
    }
1875
  }
1876
  
1877
    /* shift buffer entries */
1878
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1879
        s->input_picture[i-1]= s->input_picture[i];
1880
        
1881
    s->input_picture[encoding_delay]= (Picture*)pic;
1882

    
1883
    return 0;
1884
}
1885

    
1886
static void select_input_picture(MpegEncContext *s){
1887
    int i;
1888

    
1889
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1890
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1891
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1892

    
1893
    /* set next picture types & ordering */
1894
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1895
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1896
            s->reordered_input_picture[0]= s->input_picture[0];
1897
            s->reordered_input_picture[0]->pict_type= I_TYPE;
1898
            s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1899
        }else{
1900
            int b_frames;
1901
            
1902
            if(s->flags&CODEC_FLAG_PASS2){
1903
                for(i=0; i<s->max_b_frames+1; i++){
1904
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1905
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1906
                    s->input_picture[i]->pict_type= pict_type;
1907
                    
1908
                    if(i + 1 >= s->rc_context.num_entries) break;
1909
                }
1910
            }
1911

    
1912
            if(s->input_picture[0]->pict_type){
1913
                /* user selected pict_type */
1914
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1915
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1916
                }
1917
            
1918
                if(b_frames > s->max_b_frames){
1919
                    av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
1920
                    b_frames = s->max_b_frames;
1921
                }
1922
            }else if(s->avctx->b_frame_strategy==0){
1923
                b_frames= s->max_b_frames;
1924
                while(b_frames && !s->input_picture[b_frames]) b_frames--;
1925
            }else if(s->avctx->b_frame_strategy==1){
1926
                for(i=1; i<s->max_b_frames+1; i++){
1927
                    if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1928
                        s->input_picture[i]->b_frame_score= 
1929
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
1930
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1931
                    }
1932
                }
1933
                for(i=0; i<s->max_b_frames; i++){
1934
                    if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1935
                }
1936
                                
1937
                b_frames= FFMAX(0, i-1);
1938
                
1939
                /* reset scores */
1940
                for(i=0; i<b_frames+1; i++){
1941
                    s->input_picture[i]->b_frame_score=0;
1942
                }
1943
            }else{
1944
                av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1945
                b_frames=0;
1946
            }
1947

    
1948
            emms_c();
1949
//static int b_count=0;
1950
//b_count+= b_frames;
1951
//av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1952
            if(s->picture_in_gop_number + b_frames >= s->gop_size){
1953
                if(s->flags & CODEC_FLAG_CLOSED_GOP)
1954
                    b_frames=0;
1955
                s->input_picture[b_frames]->pict_type= I_TYPE;
1956
            }
1957
            
1958
            if(   (s->flags & CODEC_FLAG_CLOSED_GOP)
1959
               && b_frames
1960
               && s->input_picture[b_frames]->pict_type== I_TYPE)
1961
                b_frames--;
1962

    
1963
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1964
            if(s->reordered_input_picture[0]->pict_type != I_TYPE)
1965
                s->reordered_input_picture[0]->pict_type= P_TYPE;
1966
            s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1967
            for(i=0; i<b_frames; i++){
1968
                s->reordered_input_picture[i+1]= s->input_picture[i];
1969
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1970
                s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
1971
            }
1972
        }
1973
    }
1974
    
1975
    if(s->reordered_input_picture[0]){
1976
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1977

    
1978
        copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1979

    
1980
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1981
            // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1982
        
1983
            int i= ff_find_unused_picture(s, 0);
1984
            Picture *pic= &s->picture[i];
1985

    
1986
            /* mark us unused / free shared pic */
1987
            for(i=0; i<4; i++)
1988
                s->reordered_input_picture[0]->data[i]= NULL;
1989
            s->reordered_input_picture[0]->type= 0;
1990
            
1991
            copy_picture_attributes((AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
1992
            pic->reference              = s->reordered_input_picture[0]->reference;
1993
            
1994
            alloc_picture(s, pic, 0);
1995

    
1996
            s->current_picture_ptr= pic;
1997
        }else{
1998
            // input is not a shared pix -> reuse buffer for current_pix
1999

    
2000
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
2001
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
2002
            
2003
            s->current_picture_ptr= s->reordered_input_picture[0];
2004
            for(i=0; i<4; i++){
2005
                s->new_picture.data[i]+=16;
2006
            }
2007
        }
2008
        copy_picture(&s->current_picture, s->current_picture_ptr);
2009
    
2010
        s->picture_number= s->new_picture.display_picture_number;
2011
//printf("dpn:%d\n", s->picture_number);
2012
    }else{
2013
       memset(&s->new_picture, 0, sizeof(Picture));
2014
    }
2015
}
2016

    
2017
int MPV_encode_picture(AVCodecContext *avctx,
2018
                       unsigned char *buf, int buf_size, void *data)
2019
{
2020
    MpegEncContext *s = avctx->priv_data;
2021
    AVFrame *pic_arg = data;
2022
    int i, stuffing_count;
2023

    
2024
    if(avctx->pix_fmt != PIX_FMT_YUV420P){
2025
        av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
2026
        return -1;
2027
    }
2028
    
2029
    for(i=0; i<avctx->thread_count; i++){
2030
        int start_y= s->thread_context[i]->start_mb_y;
2031
        int   end_y= s->thread_context[i]->  end_mb_y;
2032
        int h= s->mb_height;
2033
        uint8_t *start= buf + buf_size*start_y/h;
2034
        uint8_t *end  = buf + buf_size*  end_y/h;
2035

    
2036
        init_put_bits(&s->thread_context[i]->pb, start, end - start);
2037
    }
2038

    
2039
    s->picture_in_gop_number++;
2040

    
2041
    load_input_picture(s, pic_arg);
2042
    
2043
    select_input_picture(s);
2044
    
2045
    /* output? */
2046
    if(s->new_picture.data[0]){
2047
        s->pict_type= s->new_picture.pict_type;
2048
//emms_c();
2049
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
2050
        MPV_frame_start(s, avctx);
2051

    
2052
        encode_picture(s, s->picture_number);
2053
        
2054
        avctx->real_pict_num  = s->picture_number;
2055
        avctx->header_bits = s->header_bits;
2056
        avctx->mv_bits     = s->mv_bits;
2057
        avctx->misc_bits   = s->misc_bits;
2058
        avctx->i_tex_bits  = s->i_tex_bits;
2059
        avctx->p_tex_bits  = s->p_tex_bits;
2060
        avctx->i_count     = s->i_count;
2061
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
2062
        avctx->skip_count  = s->skip_count;
2063

    
2064
        MPV_frame_end(s);
2065

    
2066
        if (s->out_format == FMT_MJPEG)
2067
            mjpeg_picture_trailer(s);
2068
        
2069
        if(s->flags&CODEC_FLAG_PASS1)
2070
            ff_write_pass1_stats(s);
2071

    
2072
        for(i=0; i<4; i++){
2073
            avctx->error[i] += s->current_picture_ptr->error[i];
2074
        }
2075

    
2076
        flush_put_bits(&s->pb);
2077
        s->frame_bits  = put_bits_count(&s->pb);
2078

    
2079
        stuffing_count= ff_vbv_update(s, s->frame_bits);
2080
        if(stuffing_count){
2081
            switch(s->codec_id){
2082
            case CODEC_ID_MPEG1VIDEO:
2083
            case CODEC_ID_MPEG2VIDEO:
2084
                while(stuffing_count--){
2085
                    put_bits(&s->pb, 8, 0);
2086
                }
2087
            break;
2088
            case CODEC_ID_MPEG4:
2089
                put_bits(&s->pb, 16, 0);
2090
                put_bits(&s->pb, 16, 0x1C3);
2091
                stuffing_count -= 4;
2092
                while(stuffing_count--){
2093
                    put_bits(&s->pb, 8, 0xFF);
2094
                }
2095
            break;
2096
            default:
2097
                av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
2098
            }
2099
            flush_put_bits(&s->pb);
2100
            s->frame_bits  = put_bits_count(&s->pb);
2101
        }
2102

    
2103
        /* update mpeg1/2 vbv_delay for CBR */    
2104
        if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate){
2105
            int vbv_delay;
2106

    
2107
            assert(s->repeat_first_field==0);
2108
            
2109
            vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
2110
            assert(vbv_delay < 0xFFFF);
2111

    
2112
            s->vbv_delay_ptr[0] &= 0xF8;
2113
            s->vbv_delay_ptr[0] |= vbv_delay>>13;
2114
            s->vbv_delay_ptr[1]  = vbv_delay>>5;
2115
            s->vbv_delay_ptr[2] &= 0x07;
2116
            s->vbv_delay_ptr[2] |= vbv_delay<<3;
2117
        }
2118
        s->total_bits += s->frame_bits;
2119
        avctx->frame_bits  = s->frame_bits;
2120
    }else{
2121
        assert((pbBufPtr(&s->pb) == s->pb.buf));
2122
        s->frame_bits=0;
2123
    }
2124
    assert((s->frame_bits&7)==0);
2125
    
2126
    return s->frame_bits/8;
2127
}
2128

    
2129
#endif //CONFIG_ENCODERS
2130

    
2131
static inline void gmc1_motion(MpegEncContext *s,
2132
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2133
                               uint8_t **ref_picture)
2134
{
2135
    uint8_t *ptr;
2136
    int offset, src_x, src_y, linesize, uvlinesize;
2137
    int motion_x, motion_y;
2138
    int emu=0;
2139

    
2140
    motion_x= s->sprite_offset[0][0];
2141
    motion_y= s->sprite_offset[0][1];
2142
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
2143
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
2144
    motion_x<<=(3-s->sprite_warping_accuracy);
2145
    motion_y<<=(3-s->sprite_warping_accuracy);
2146
    src_x = clip(src_x, -16, s->width);
2147
    if (src_x == s->width)
2148
        motion_x =0;
2149
    src_y = clip(src_y, -16, s->height);
2150
    if (src_y == s->height)
2151
        motion_y =0;
2152

    
2153
    linesize = s->linesize;
2154
    uvlinesize = s->uvlinesize;
2155
    
2156
    ptr = ref_picture[0] + (src_y * linesize) + src_x;
2157

    
2158
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2159
        if(   (unsigned)src_x >= s->h_edge_pos - 17
2160
           || (unsigned)src_y >= s->v_edge_pos - 17){
2161
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2162
            ptr= s->edge_emu_buffer;
2163
        }
2164
    }
2165
    
2166
    if((motion_x|motion_y)&7){
2167
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
2168
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
2169
    }else{
2170
        int dxy;
2171
        
2172
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
2173
        if (s->no_rounding){
2174
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
2175
        }else{
2176
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
2177
        }
2178
    }
2179
    
2180
    if(s->flags&CODEC_FLAG_GRAY) return;
2181

    
2182
    motion_x= s->sprite_offset[1][0];
2183
    motion_y= s->sprite_offset[1][1];
2184
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
2185
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
2186
    motion_x<<=(3-s->sprite_warping_accuracy);
2187
    motion_y<<=(3-s->sprite_warping_accuracy);
2188
    src_x = clip(src_x, -8, s->width>>1);
2189
    if (src_x == s->width>>1)
2190
        motion_x =0;
2191
    src_y = clip(src_y, -8, s->height>>1);
2192
    if (src_y == s->height>>1)
2193
        motion_y =0;
2194

    
2195
    offset = (src_y * uvlinesize) + src_x;
2196
    ptr = ref_picture[1] + offset;
2197
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2198
        if(   (unsigned)src_x >= (s->h_edge_pos>>1) - 9
2199
           || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
2200
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2201
            ptr= s->edge_emu_buffer;
2202
            emu=1;
2203
        }
2204
    }
2205
    s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2206
    
2207
    ptr = ref_picture[2] + offset;
2208
    if(emu){
2209
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2210
        ptr= s->edge_emu_buffer;
2211
    }
2212
    s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2213
    
2214
    return;
2215
}
2216

    
2217
static inline void gmc_motion(MpegEncContext *s,
2218
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2219
                               uint8_t **ref_picture)
2220
{
2221
    uint8_t *ptr;
2222
    int linesize, uvlinesize;
2223
    const int a= s->sprite_warping_accuracy;
2224
    int ox, oy;
2225

    
2226
    linesize = s->linesize;
2227
    uvlinesize = s->uvlinesize;
2228

    
2229
    ptr = ref_picture[0];
2230

    
2231
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
2232
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
2233

    
2234
    s->dsp.gmc(dest_y, ptr, linesize, 16,
2235
           ox, 
2236
           oy, 
2237
           s->sprite_delta[0][0], s->sprite_delta[0][1],
2238
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
2239
           a+1, (1<<(2*a+1)) - s->no_rounding,
2240
           s->h_edge_pos, s->v_edge_pos);
2241
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
2242
           ox + s->sprite_delta[0][0]*8, 
2243
           oy + s->sprite_delta[1][0]*8, 
2244
           s->sprite_delta[0][0], s->sprite_delta[0][1],
2245
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
2246
           a+1, (1<<(2*a+1)) - s->no_rounding,
2247
           s->h_edge_pos, s->v_edge_pos);
2248

    
2249
    if(s->flags&CODEC_FLAG_GRAY) return;
2250

    
2251
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
2252
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
2253

    
2254
    ptr = ref_picture[1];
2255
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
2256
           ox, 
2257
           oy, 
2258
           s->sprite_delta[0][0], s->sprite_delta[0][1],
2259
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
2260
           a+1, (1<<(2*a+1)) - s->no_rounding,
2261
           s->h_edge_pos>>1, s->v_edge_pos>>1);
2262
    
2263
    ptr = ref_picture[2];
2264
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
2265
           ox, 
2266
           oy, 
2267
           s->sprite_delta[0][0], s->sprite_delta[0][1],
2268
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
2269
           a+1, (1<<(2*a+1)) - s->no_rounding,
2270
           s->h_edge_pos>>1, s->v_edge_pos>>1);
2271
}
2272

    
2273
/**
2274
 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
2275
 * @param buf destination buffer
2276
 * @param src source buffer
2277
 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
2278
 * @param block_w width of block
2279
 * @param block_h height of block
2280
 * @param src_x x coordinate of the top left sample of the block in the source buffer
2281
 * @param src_y y coordinate of the top left sample of the block in the source buffer
2282
 * @param w width of the source buffer
2283
 * @param h height of the source buffer
2284
 */
2285
void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 
2286
                                    int src_x, int src_y, int w, int h){
2287
    int x, y;
2288
    int start_y, start_x, end_y, end_x;
2289

    
2290
    if(src_y>= h){
2291
        src+= (h-1-src_y)*linesize;
2292
        src_y=h-1;
2293
    }else if(src_y<=-block_h){
2294
        src+= (1-block_h-src_y)*linesize;
2295
        src_y=1-block_h;
2296
    }
2297
    if(src_x>= w){
2298
        src+= (w-1-src_x);
2299
        src_x=w-1;
2300
    }else if(src_x<=-block_w){
2301
        src+= (1-block_w-src_x);
2302
        src_x=1-block_w;
2303
    }
2304

    
2305
    start_y= FFMAX(0, -src_y);
2306
    start_x= FFMAX(0, -src_x);
2307
    end_y= FFMIN(block_h, h-src_y);
2308
    end_x= FFMIN(block_w, w-src_x);
2309

    
2310
    // copy existing part
2311
    for(y=start_y; y<end_y; y++){
2312
        for(x=start_x; x<end_x; x++){
2313
            buf[x + y*linesize]= src[x + y*linesize];
2314
        }
2315
    }
2316

    
2317
    //top
2318
    for(y=0; y<start_y; y++){
2319
        for(x=start_x; x<end_x; x++){
2320
            buf[x + y*linesize]= buf[x + start_y*linesize];
2321
        }
2322
    }
2323

    
2324
    //bottom
2325
    for(y=end_y; y<block_h; y++){
2326
        for(x=start_x; x<end_x; x++){
2327
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
2328
        }
2329
    }
2330
                                    
2331
    for(y=0; y<block_h; y++){
2332
       //left
2333
        for(x=0; x<start_x; x++){
2334
            buf[x + y*linesize]= buf[start_x + y*linesize];
2335
        }
2336
       
2337
       //right
2338
        for(x=end_x; x<block_w; x++){
2339
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
2340
        }
2341
    }
2342
}
2343

    
2344
static inline int hpel_motion(MpegEncContext *s, 
2345
                                  uint8_t *dest, uint8_t *src,
2346
                                  int field_based, int field_select,
2347
                                  int src_x, int src_y,
2348
                                  int width, int height, int stride,
2349
                                  int h_edge_pos, int v_edge_pos,
2350
                                  int w, int h, op_pixels_func *pix_op,
2351
                                  int motion_x, int motion_y)
2352
{
2353
    int dxy;
2354
    int emu=0;
2355

    
2356
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2357
    src_x += motion_x >> 1;
2358
    src_y += motion_y >> 1;
2359
                
2360
    /* WARNING: do no forget half pels */
2361
    src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
2362
    if (src_x == width)
2363
        dxy &= ~1;
2364
    src_y = clip(src_y, -16, height);
2365
    if (src_y == height)
2366
        dxy &= ~2;
2367
    src += src_y * stride + src_x;
2368

    
2369
    if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
2370
        if(   (unsigned)src_x > h_edge_pos - (motion_x&1) - w
2371
           || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
2372
            ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
2373
                             src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
2374
            src= s->edge_emu_buffer;
2375
            emu=1;
2376
        }
2377
    }
2378
    if(field_select)
2379
        src += s->linesize;
2380
    pix_op[dxy](dest, src, stride, h);
2381
    return emu;
2382
}
2383

    
2384
/* apply one mpeg motion vector to the three components */
2385
static inline void mpeg_motion(MpegEncContext *s,
2386
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2387
                               int field_based, int bottom_field, int field_select,
2388
                               uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
2389
                               int motion_x, int motion_y, int h)
2390
{
2391
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2392
    int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
2393
    
2394
#if 0    
2395
if(s->quarter_sample)
2396
{
2397
    motion_x>>=1;
2398
    motion_y>>=1;
2399
}
2400
#endif
2401

    
2402
    v_edge_pos = s->v_edge_pos >> field_based;
2403
    linesize   = s->current_picture.linesize[0] << field_based;
2404
    uvlinesize = s->current_picture.linesize[1] << field_based;
2405

    
2406
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2407
    src_x = s->mb_x* 16               + (motion_x >> 1);
2408
    src_y = s->mb_y*(16>>field_based) + (motion_y >> 1);
2409

    
2410
    if (s->out_format == FMT_H263) {
2411
        uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
2412
        uvsrc_x = src_x>>1;
2413
        uvsrc_y = src_y>>1;
2414
    } else {
2415
        mx = motion_x / 2;
2416
        my = motion_y / 2;
2417
        uvdxy = ((my & 1) << 1) | (mx & 1);
2418
        uvsrc_x = s->mb_x* 8               + (mx >> 1);
2419
        uvsrc_y = s->mb_y*(8>>field_based) + (my >> 1);
2420
    }
2421

    
2422
    ptr_y  = ref_picture[0] + src_y * linesize + src_x;
2423
    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2424
    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2425

    
2426
    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
2427
       || (unsigned)src_y >    v_edge_pos - (motion_y&1) - h){
2428
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
2429
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2430
            ptr_y = s->edge_emu_buffer;
2431
            if(!(s->flags&CODEC_FLAG_GRAY)){
2432
                uint8_t *uvbuf= s->edge_emu_buffer+17*s->linesize;
2433
                ff_emulated_edge_mc(uvbuf  , ptr_cb, s->uvlinesize, 9, 9+field_based, 
2434
                                 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2435
                ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based, 
2436
                                 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2437
                ptr_cb= uvbuf;
2438
                ptr_cr= uvbuf+16;
2439
            }
2440
    }
2441

    
2442
    if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
2443
        dest_y += s->linesize;
2444
        dest_cb+= s->uvlinesize;
2445
        dest_cr+= s->uvlinesize;
2446
    }
2447

    
2448
    if(field_select){
2449
        ptr_y += s->linesize;
2450
        ptr_cb+= s->uvlinesize;
2451
        ptr_cr+= s->uvlinesize;
2452
    }
2453

    
2454
    pix_op[0][dxy](dest_y, ptr_y, linesize, h);
2455
    
2456
    if(!(s->flags&CODEC_FLAG_GRAY)){
2457
        pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
2458
        pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
2459
    }
2460
}
2461
//FIXME move to dsputil, avg variant, 16x16 version
2462
static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
2463
    int x;
2464
    uint8_t * const top   = src[1];
2465
    uint8_t * const left  = src[2];
2466
    uint8_t * const mid   = src[0];
2467
    uint8_t * const right = src[3];
2468
    uint8_t * const bottom= src[4];
2469
#define OBMC_FILTER(x, t, l, m, r, b)\
2470
    dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
2471
#define OBMC_FILTER4(x, t, l, m, r, b)\
2472
    OBMC_FILTER(x         , t, l, m, r, b);\
2473
    OBMC_FILTER(x+1       , t, l, m, r, b);\
2474
    OBMC_FILTER(x  +stride, t, l, m, r, b);\
2475
    OBMC_FILTER(x+1+stride, t, l, m, r, b);
2476
    
2477
    x=0;
2478
    OBMC_FILTER (x  , 2, 2, 4, 0, 0);
2479
    OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
2480
    OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
2481
    OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
2482
    OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
2483
    OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
2484
    x+= stride;
2485
    OBMC_FILTER (x  , 1, 2, 5, 0, 0);
2486
    OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
2487
    OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
2488
    OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
2489
    x+= stride;
2490
    OBMC_FILTER4(x  , 1, 2, 5, 0, 0);
2491
    OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
2492
    OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
2493
    OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
2494
    x+= 2*stride;
2495
    OBMC_FILTER4(x  , 0, 2, 5, 0, 1);
2496
    OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
2497
    OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
2498
    OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
2499
    x+= 2*stride;
2500
    OBMC_FILTER (x  , 0, 2, 5, 0, 1);
2501
    OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
2502
    OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
2503
    OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
2504
    OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
2505
    OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
2506
    x+= stride;
2507
    OBMC_FILTER (x  , 0, 2, 4, 0, 2);
2508
    OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
2509
    OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
2510
    OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
2511
}
2512

    
2513
/* obmc for 1 8x8 luma block */
2514
static inline void obmc_motion(MpegEncContext *s,
2515
                               uint8_t *dest, uint8_t *src,
2516
                               int src_x, int src_y,
2517
                               op_pixels_func *pix_op,
2518
                               int16_t mv[5][2]/* mid top left right bottom*/)
2519
#define MID    0
2520
{
2521
    int i;
2522
    uint8_t *ptr[5];
2523
    
2524
    assert(s->quarter_sample==0);
2525
    
2526
    for(i=0; i<5; i++){
2527
        if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
2528
            ptr[i]= ptr[MID];
2529
        }else{
2530
            ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
2531
            hpel_motion(s, ptr[i], src, 0, 0,
2532
                        src_x, src_y,
2533
                        s->width, s->height, s->linesize,
2534
                        s->h_edge_pos, s->v_edge_pos,
2535
                        8, 8, pix_op,
2536
                        mv[i][0], mv[i][1]);
2537
        }
2538
    }
2539

    
2540
    put_obmc(dest, ptr, s->linesize);                
2541
}
2542

    
2543
static inline void qpel_motion(MpegEncContext *s,
2544
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2545
                               int field_based, int bottom_field, int field_select,
2546
                               uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
2547
                               qpel_mc_func (*qpix_op)[16],
2548
                               int motion_x, int motion_y, int h)
2549
{
2550
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2551
    int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
2552

    
2553
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2554
    src_x = s->mb_x *  16                 + (motion_x >> 2);
2555
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
2556

    
2557
    v_edge_pos = s->v_edge_pos >> field_based;
2558
    linesize = s->linesize << field_based;
2559
    uvlinesize = s->uvlinesize << field_based;
2560
    
2561
    if(field_based){
2562
        mx= motion_x/2;
2563
        my= motion_y>>1;
2564
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
2565
        static const int rtab[8]= {0,0,1,1,0,0,0,1};
2566
        mx= (motion_x>>1) + rtab[motion_x&7];
2567
        my= (motion_y>>1) + rtab[motion_y&7];
2568
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
2569
        mx= (motion_x>>1)|(motion_x&1);
2570
        my= (motion_y>>1)|(motion_y&1);
2571
    }else{
2572
        mx= motion_x/2;
2573
        my= motion_y/2;
2574
    }
2575
    mx= (mx>>1)|(mx&1);
2576
    my= (my>>1)|(my&1);
2577

    
2578
    uvdxy= (mx&1) | ((my&1)<<1);
2579
    mx>>=1;
2580
    my>>=1;
2581

    
2582
    uvsrc_x = s->mb_x *  8                 + mx;
2583
    uvsrc_y = s->mb_y * (8 >> field_based) + my;
2584

    
2585
    ptr_y  = ref_picture[0] +   src_y *   linesize +   src_x;
2586
    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2587
    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2588

    
2589
    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16 
2590
       || (unsigned)src_y >    v_edge_pos - (motion_y&3) - h  ){
2591
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, 
2592
                         src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2593
        ptr_y= s->edge_emu_buffer;
2594
        if(!(s->flags&CODEC_FLAG_GRAY)){
2595
            uint8_t *uvbuf= s->edge_emu_buffer + 17*s->linesize;
2596
            ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based, 
2597
                             uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2598
            ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based, 
2599
                             uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2600
            ptr_cb= uvbuf;
2601
            ptr_cr= uvbuf + 16;
2602
        }
2603
    }
2604

    
2605
    if(!field_based)
2606
        qpix_op[0][dxy](dest_y, ptr_y, linesize);
2607
    else{
2608
        if(bottom_field){
2609
            dest_y += s->linesize;
2610
            dest_cb+= s->uvlinesize;
2611
            dest_cr+= s->uvlinesize;
2612
        }
2613

    
2614
        if(field_select){
2615
            ptr_y  += s->linesize;
2616
            ptr_cb += s->uvlinesize;
2617
            ptr_cr += s->uvlinesize;
2618
        }
2619
        //damn interlaced mode
2620
        //FIXME boundary mirroring is not exactly correct here
2621
        qpix_op[1][dxy](dest_y  , ptr_y  , linesize);
2622
        qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
2623
    }
2624
    if(!(s->flags&CODEC_FLAG_GRAY)){
2625
        pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
2626
        pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
2627
    }
2628
}
2629

    
2630
inline int ff_h263_round_chroma(int x){
2631
    if (x >= 0)
2632
        return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2633
    else {
2634
        x = -x;
2635
        return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2636
    }
2637
}
2638

    
2639
/**
2640
 * h263 chorma 4mv motion compensation.
2641
 */
2642
static inline void chroma_4mv_motion(MpegEncContext *s,
2643
                                     uint8_t *dest_cb, uint8_t *dest_cr,
2644
                                     uint8_t **ref_picture,
2645
                                     op_pixels_func *pix_op,
2646
                                     int mx, int my){
2647
    int dxy, emu=0, src_x, src_y, offset;
2648
    uint8_t *ptr;
2649
    
2650
    /* In case of 8X8, we construct a single chroma motion vector
2651
       with a special rounding */
2652
    mx= ff_h263_round_chroma(mx);
2653
    my= ff_h263_round_chroma(my);
2654
    
2655
    dxy = ((my & 1) << 1) | (mx & 1);
2656
    mx >>= 1;
2657
    my >>= 1;
2658

    
2659
    src_x = s->mb_x * 8 + mx;
2660
    src_y = s->mb_y * 8 + my;
2661
    src_x = clip(src_x, -8, s->width/2);
2662
    if (src_x == s->width/2)
2663
        dxy &= ~1;
2664
    src_y = clip(src_y, -8, s->height/2);
2665
    if (src_y == s->height/2)
2666
        dxy &= ~2;
2667
    
2668
    offset = (src_y * (s->uvlinesize)) + src_x;
2669
    ptr = ref_picture[1] + offset;
2670
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2671
        if(   (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
2672
           || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
2673
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2674
            ptr= s->edge_emu_buffer;
2675
            emu=1;
2676
        }
2677
    }
2678
    pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
2679

    
2680
    ptr = ref_picture[2] + offset;
2681
    if(emu){
2682
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2683
        ptr= s->edge_emu_buffer;
2684
    }
2685
    pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
2686
}
2687

    
2688
/**
2689
 * motion compesation of a single macroblock
2690
 * @param s context
2691
 * @param dest_y luma destination pointer
2692
 * @param dest_cb chroma cb/u destination pointer
2693
 * @param dest_cr chroma cr/v destination pointer
2694
 * @param dir direction (0->forward, 1->backward)
2695
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2696
 * @param pic_op halfpel motion compensation function (average or put normally)
2697
 * @param pic_op qpel motion compensation function (average or put normally)
2698
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2699
 */
2700
static inline void MPV_motion(MpegEncContext *s, 
2701
                              uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2702
                              int dir, uint8_t **ref_picture, 
2703
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2704
{
2705
    int dxy, mx, my, src_x, src_y, motion_x, motion_y;
2706
    int mb_x, mb_y, i;
2707
    uint8_t *ptr, *dest;
2708

    
2709
    mb_x = s->mb_x;
2710
    mb_y = s->mb_y;
2711

    
2712
    if(s->obmc && s->pict_type != B_TYPE){
2713
        int16_t mv_cache[4][4][2];
2714
        const int xy= s->mb_x + s->mb_y*s->mb_stride;
2715
        const int mot_stride= s->mb_width*2 + 2;
2716
        const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
2717

    
2718
        assert(!s->mb_skiped);
2719
                
2720
        memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy           ], sizeof(int16_t)*4);
2721
        memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
2722
        memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
2723

    
2724
        if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
2725
            memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
2726
        }else{
2727
            memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
2728
        }
2729

    
2730
        if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
2731
            *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
2732
            *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
2733
        }else{
2734
            *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
2735
            *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
2736
        }
2737

    
2738
        if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
2739
            *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
2740
            *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
2741
        }else{
2742
            *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
2743
            *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
2744
        }
2745
        
2746
        mx = 0;
2747
        my = 0;
2748
        for(i=0;i<4;i++) {
2749
            const int x= (i&1)+1;
2750
            const int y= (i>>1)+1;
2751
            int16_t mv[5][2]= {
2752
                {mv_cache[y][x  ][0], mv_cache[y][x  ][1]},
2753
                {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
2754
                {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
2755
                {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
2756
                {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
2757
            //FIXME cleanup
2758
            obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2759
                        ref_picture[0],
2760
                        mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2761
                        pix_op[1],
2762
                        mv);
2763

    
2764
            mx += mv[0][0];
2765
            my += mv[0][1];
2766
        }
2767
        if(!(s->flags&CODEC_FLAG_GRAY))
2768
            chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2769

    
2770
        return;
2771
    }
2772
   
2773
    switch(s->mv_type) {
2774
    case MV_TYPE_16X16:
2775
#ifdef CONFIG_RISKY
2776
        if(s->mcsel){
2777
            if(s->real_sprite_warping_points==1){
2778
                gmc1_motion(s, dest_y, dest_cb, dest_cr,
2779
                            ref_picture);
2780
            }else{
2781
                gmc_motion(s, dest_y, dest_cb, dest_cr,
2782
                            ref_picture);
2783
            }
2784
        }else if(s->quarter_sample){
2785
            qpel_motion(s, dest_y, dest_cb, dest_cr, 
2786
                        0, 0, 0,
2787
                        ref_picture, pix_op, qpix_op,
2788
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2789
        }else if(s->mspel){
2790
            ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2791
                        ref_picture, pix_op,
2792
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2793
        }else
2794
#endif
2795
        {
2796
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 
2797
                        0, 0, 0,
2798
                        ref_picture, pix_op,
2799
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2800
        }           
2801
        break;
2802
    case MV_TYPE_8X8:
2803
        mx = 0;
2804
        my = 0;
2805
        if(s->quarter_sample){
2806
            for(i=0;i<4;i++) {
2807
                motion_x = s->mv[dir][i][0];
2808
                motion_y = s->mv[dir][i][1];
2809

    
2810
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2811
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2812
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2813
                    
2814
                /* WARNING: do no forget half pels */
2815
                src_x = clip(src_x, -16, s->width);
2816
                if (src_x == s->width)
2817
                    dxy &= ~3;
2818
                src_y = clip(src_y, -16, s->height);
2819
                if (src_y == s->height)
2820
                    dxy &= ~12;
2821
                    
2822
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2823
                if(s->flags&CODEC_FLAG_EMU_EDGE){
2824
                    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8 
2825
                       || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
2826
                        ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2827
                        ptr= s->edge_emu_buffer;
2828
                    }
2829
                }
2830
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2831
                qpix_op[1][dxy](dest, ptr, s->linesize);
2832

    
2833
                mx += s->mv[dir][i][0]/2;
2834
                my += s->mv[dir][i][1]/2;
2835
            }
2836
        }else{
2837
            for(i=0;i<4;i++) {
2838
                hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2839
                            ref_picture[0], 0, 0,
2840
                            mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2841
                            s->width, s->height, s->linesize,
2842
                            s->h_edge_pos, s->v_edge_pos,
2843
                            8, 8, pix_op[1],
2844
                            s->mv[dir][i][0], s->mv[dir][i][1]);
2845

    
2846
                mx += s->mv[dir][i][0];
2847
                my += s->mv[dir][i][1];
2848
            }
2849
        }
2850

    
2851
        if(!(s->flags&CODEC_FLAG_GRAY))
2852
            chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2853
        break;
2854
    case MV_TYPE_FIELD:
2855
        if (s->picture_structure == PICT_FRAME) {
2856
            if(s->quarter_sample){
2857
                for(i=0; i<2; i++){
2858
                    qpel_motion(s, dest_y, dest_cb, dest_cr,
2859
                                1, i, s->field_select[dir][i],
2860
                                ref_picture, pix_op, qpix_op,
2861
                                s->mv[dir][i][0], s->mv[dir][i][1], 8);
2862
                }
2863
            }else{
2864
                /* top field */       
2865
                mpeg_motion(s, dest_y, dest_cb, dest_cr,
2866
                            1, 0, s->field_select[dir][0],
2867
                            ref_picture, pix_op,
2868
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
2869
                /* bottom field */
2870
                mpeg_motion(s, dest_y, dest_cb, dest_cr,
2871
                            1, 1, s->field_select[dir][1],
2872
                            ref_picture, pix_op,
2873
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
2874
            }
2875
        } else {
2876
            if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
2877
                ref_picture= s->current_picture_ptr->data;
2878
            } 
2879

    
2880
            mpeg_motion(s, dest_y, dest_cb, dest_cr,
2881
                        0, 0, s->field_select[dir][0],
2882
                        ref_picture, pix_op,
2883
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2884
        }
2885
        break;
2886
    case MV_TYPE_16X8:
2887
        for(i=0; i<2; i++){
2888
            uint8_t ** ref2picture;
2889

    
2890
            if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
2891
                ref2picture= ref_picture;
2892
            }else{
2893
                ref2picture= s->current_picture_ptr->data;
2894
            } 
2895

    
2896
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 
2897
                        0, 0, s->field_select[dir][i],
2898
                        ref2picture, pix_op,
2899
                        s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
2900
                
2901
            dest_y += 16*s->linesize;
2902
            dest_cb+=  8*s->uvlinesize;
2903
            dest_cr+=  8*s->uvlinesize;
2904
        }        
2905
        break;
2906
    case MV_TYPE_DMV:
2907
        if(s->picture_structure == PICT_FRAME){
2908
            for(i=0; i<2; i++){
2909
                int j;
2910
                for(j=0; j<2; j++){
2911
                    mpeg_motion(s, dest_y, dest_cb, dest_cr,
2912
                                1, j, j^i,
2913
                                ref_picture, pix_op,
2914
                                s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
2915
                }
2916
                pix_op = s->dsp.avg_pixels_tab; 
2917
            }
2918
        }else{
2919
            for(i=0; i<2; i++){
2920
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 
2921
                            0, 0, s->picture_structure != i+1,
2922
                            ref_picture, pix_op,
2923
                            s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
2924

    
2925
                // after put we make avg of the same block
2926
                pix_op=s->dsp.avg_pixels_tab; 
2927

    
2928
                //opposite parity is always in the same frame if this is second field
2929
                if(!s->first_field){
2930
                    ref_picture = s->current_picture_ptr->data;    
2931
                }
2932
            }
2933
        }
2934
    break;
2935
    default: assert(0);
2936
    }
2937
}
2938

    
2939

    
2940
/* put block[] to dest[] */
2941
static inline void put_dct(MpegEncContext *s, 
2942
                           DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
2943
{
2944
    s->dct_unquantize_intra(s, block, i, qscale);
2945
    s->dsp.idct_put (dest, line_size, block);
2946
}
2947

    
2948
/* add block[] to dest[] */
2949
static inline void add_dct(MpegEncContext *s, 
2950
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2951
{
2952
    if (s->block_last_index[i] >= 0) {
2953
        s->dsp.idct_add (dest, line_size, block);
2954
    }
2955
}
2956

    
2957
static inline void add_dequant_dct(MpegEncContext *s, 
2958
                           DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
2959
{
2960
    if (s->block_last_index[i] >= 0) {
2961
        s->dct_unquantize_inter(s, block, i, qscale);
2962

    
2963
        s->dsp.idct_add (dest, line_size, block);
2964
    }
2965
}
2966

    
2967
/**
2968
 * cleans dc, ac, coded_block for the current non intra MB
2969
 */
2970
void ff_clean_intra_table_entries(MpegEncContext *s)
2971
{
2972
    int wrap = s->block_wrap[0];
2973
    int xy = s->block_index[0];
2974
    
2975
    s->dc_val[0][xy           ] = 
2976
    s->dc_val[0][xy + 1       ] = 
2977
    s->dc_val[0][xy     + wrap] =
2978
    s->dc_val[0][xy + 1 + wrap] = 1024;
2979
    /* ac pred */
2980
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
2981
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2982
    if (s->msmpeg4_version>=3) {
2983
        s->coded_block[xy           ] =
2984
        s->coded_block[xy + 1       ] =
2985
        s->coded_block[xy     + wrap] =
2986
        s->coded_block[xy + 1 + wrap] = 0;
2987
    }
2988
    /* chroma */
2989
    wrap = s->block_wrap[4];
2990
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
2991
    s->dc_val[1][xy] =
2992
    s->dc_val[2][xy] = 1024;
2993
    /* ac pred */
2994
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2995
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2996
    
2997
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
2998
}
2999

    
3000
/* generic function called after a macroblock has been parsed by the
3001
   decoder or after it has been encoded by the encoder.
3002

3003
   Important variables used:
3004
   s->mb_intra : true if intra macroblock
3005
   s->mv_dir   : motion vector direction
3006
   s->mv_type  : motion vector type
3007
   s->mv       : motion vector
3008
   s->interlaced_dct : true if interlaced dct used (mpeg2)
3009
 */
3010
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
3011
{
3012
    int mb_x, mb_y;
3013
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
3014
#ifdef HAVE_XVMC
3015
    if(s->avctx->xvmc_acceleration){
3016
        XVMC_decode_mb(s);//xvmc uses pblocks
3017
        return;
3018
    }
3019
#endif
3020

    
3021
    mb_x = s->mb_x;
3022
    mb_y = s->mb_y;
3023

    
3024
    if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
3025
       /* save DCT coefficients */
3026
       int i,j;
3027
       DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
3028
       for(i=0; i<6; i++)
3029
           for(j=0; j<64; j++)
3030
               *dct++ = block[i][s->dsp.idct_permutation[j]];
3031
    }
3032

    
3033
    s->current_picture.qscale_table[mb_xy]= s->qscale;
3034

    
3035
    /* update DC predictors for P macroblocks */
3036
    if (!s->mb_intra) {
3037
        if (s->h263_pred || s->h263_aic) {
3038
            if(s->mbintra_table[mb_xy])
3039
                ff_clean_intra_table_entries(s);
3040
        } else {
3041
            s->last_dc[0] =
3042
            s->last_dc[1] =
3043
            s->last_dc[2] = 128 << s->intra_dc_precision;
3044
        }
3045
    }
3046
    else if (s->h263_pred || s->h263_aic)
3047
        s->mbintra_table[mb_xy]=1;
3048

    
3049
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
3050
        uint8_t *dest_y, *dest_cb, *dest_cr;
3051
        int dct_linesize, dct_offset;
3052
        op_pixels_func (*op_pix)[4];
3053
        qpel_mc_func (*op_qpix)[16];
3054
        const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
3055
        const int uvlinesize= s->current_picture.linesize[1];
3056
        const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
3057

    
3058
        /* avoid copy if macroblock skipped in last frame too */
3059
        /* skip only during decoding as we might trash the buffers during encoding a bit */
3060
        if(!s->encoding){
3061
            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
3062
            const int age= s->current_picture.age;
3063

    
3064
            assert(age);
3065

    
3066
            if (s->mb_skiped) {
3067
                s->mb_skiped= 0;
3068
                assert(s->pict_type!=I_TYPE);
3069
 
3070
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
3071
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
3072

    
3073
                /* if previous was skipped too, then nothing to do !  */
3074
                if (*mbskip_ptr >= age && s->current_picture.reference){
3075
                    return;
3076
                }
3077
            } else if(!s->current_picture.reference){
3078
                (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
3079
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
3080
            } else{
3081
                *mbskip_ptr = 0; /* not skipped */
3082
            }
3083
        }
3084

    
3085
        if (s->interlaced_dct) {
3086
            dct_linesize = linesize * 2;
3087
            dct_offset = linesize;
3088
        } else {
3089
            dct_linesize = linesize;
3090
            dct_offset = linesize * 8;
3091
        }
3092
        if(readable){
3093
            dest_y=  s->dest[0];
3094
            dest_cb= s->dest[1];
3095
            dest_cr= s->dest[2];
3096
        }else{
3097
            dest_y = s->b_scratchpad;
3098
            dest_cb= s->b_scratchpad+16*linesize;
3099
            dest_cr= s->b_scratchpad+16*linesize+8;
3100
        }
3101
        if (!s->mb_intra) {
3102
            /* motion handling */
3103
            /* decoding or more than one mb_type (MC was allready done otherwise) */
3104
            if(!s->encoding){
3105
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
3106
                    op_pix = s->dsp.put_pixels_tab;
3107
                    op_qpix= s->dsp.put_qpel_pixels_tab;
3108
                }else{
3109
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
3110
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
3111
                }
3112

    
3113
                if (s->mv_dir & MV_DIR_FORWARD) {
3114
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
3115
                    op_pix = s->dsp.avg_pixels_tab;
3116
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
3117
                }
3118
                if (s->mv_dir & MV_DIR_BACKWARD) {
3119
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
3120
                }
3121
            }
3122

    
3123
            /* skip dequant / idct if we are really late ;) */
3124
            if(s->hurry_up>1) return;
3125

    
3126
            /* add dct residue */
3127
            if(s->encoding || !(   s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
3128
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
3129
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
3130
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
3131
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
3132
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
3133

    
3134
                if(!(s->flags&CODEC_FLAG_GRAY)){
3135
                    add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3136
                    add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3137
                }
3138
            } else if(s->codec_id != CODEC_ID_WMV2){
3139
                add_dct(s, block[0], 0, dest_y, dct_linesize);
3140
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
3141
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
3142
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
3143

    
3144
                if(!(s->flags&CODEC_FLAG_GRAY)){
3145
                    add_dct(s, block[4], 4, dest_cb, uvlinesize);
3146
                    add_dct(s, block[5], 5, dest_cr, uvlinesize);
3147
                }
3148
            } 
3149
#ifdef CONFIG_RISKY
3150
            else{
3151
                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
3152
            }
3153
#endif
3154
        } else {
3155
            /* dct only in intra block */
3156
            if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
3157
                put_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
3158
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
3159
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
3160
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
3161

    
3162
                if(!(s->flags&CODEC_FLAG_GRAY)){
3163
                    put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3164
                    put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3165
                }
3166
            }else{
3167
                s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
3168
                s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
3169
                s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
3170
                s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
3171

    
3172
                if(!(s->flags&CODEC_FLAG_GRAY)){
3173
                    s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
3174
                    s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
3175
                }
3176
            }
3177
        }
3178
        if(!readable){
3179
            s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
3180
            s->dsp.put_pixels_tab[1][0](s->dest[1], dest_cb, uvlinesize, 8);
3181
            s->dsp.put_pixels_tab[1][0](s->dest[2], dest_cr, uvlinesize, 8);
3182
        }
3183
    }
3184
}
3185

    
3186
#ifdef CONFIG_ENCODERS
3187

    
3188
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
3189
{
3190
    static const char tab[64]=
3191
        {3,2,2,1,1,1,1,1,
3192
         1,1,1,1,1,1,1,1,
3193
         1,1,1,1,1,1,1,1,
3194
         0,0,0,0,0,0,0,0,
3195
         0,0,0,0,0,0,0,0,
3196
         0,0,0,0,0,0,0,0,
3197
         0,0,0,0,0,0,0,0,
3198
         0,0,0,0,0,0,0,0};
3199
    int score=0;
3200
    int run=0;
3201
    int i;
3202
    DCTELEM *block= s->block[n];
3203
    const int last_index= s->block_last_index[n];
3204
    int skip_dc;
3205

    
3206
    if(threshold<0){
3207
        skip_dc=0;
3208
        threshold= -threshold;
3209
    }else
3210
        skip_dc=1;
3211

    
3212
    /* are all which we could set to zero are allready zero? */
3213
    if(last_index<=skip_dc - 1) return;
3214

    
3215
    for(i=0; i<=last_index; i++){
3216
        const int j = s->intra_scantable.permutated[i];
3217
        const int level = ABS(block[j]);
3218
        if(level==1){
3219
            if(skip_dc && i==0) continue;
3220
            score+= tab[run];
3221
            run=0;
3222
        }else if(level>1){
3223
            return;
3224
        }else{
3225
            run++;
3226
        }
3227
    }
3228
    if(score >= threshold) return;
3229
    for(i=skip_dc; i<=last_index; i++){
3230
        const int j = s->intra_scantable.permutated[i];
3231
        block[j]=0;
3232
    }
3233
    if(block[0]) s->block_last_index[n]= 0;
3234
    else         s->block_last_index[n]= -1;
3235
}
3236

    
3237
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
3238
{
3239
    int i;
3240
    const int maxlevel= s->max_qcoeff;
3241
    const int minlevel= s->min_qcoeff;
3242
    int overflow=0;
3243
    
3244
    if(s->mb_intra){
3245
        i=1; //skip clipping of intra dc
3246
    }else
3247
        i=0;
3248
    
3249
    for(;i<=last_index; i++){
3250
        const int j= s->intra_scantable.permutated[i];
3251
        int level = block[j];
3252
       
3253
        if     (level>maxlevel){
3254
            level=maxlevel;
3255
            overflow++;
3256
        }else if(level<minlevel){
3257
            level=minlevel;
3258
            overflow++;
3259
        }
3260
        
3261
        block[j]= level;
3262
    }
3263
    
3264
    if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
3265
        av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%d\n", overflow, minlevel, maxlevel);
3266
}
3267

    
3268
#endif //CONFIG_ENCODERS
3269

    
3270
/**
3271
 *
3272
 * @param h is the normal height, this will be reduced automatically if needed for the last row
3273
 */
3274
void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
3275
    if (s->avctx->draw_horiz_band) {
3276
        AVFrame *src;
3277
        int offset[4];
3278
        
3279
        if(s->picture_structure != PICT_FRAME){
3280
            h <<= 1;
3281
            y <<= 1;
3282
            if(s->first_field  && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
3283
        }
3284

    
3285
        h= FFMIN(h, s->height - y);
3286

    
3287
        if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER)) 
3288
            src= (AVFrame*)s->current_picture_ptr;
3289
        else if(s->last_picture_ptr)
3290
            src= (AVFrame*)s->last_picture_ptr;
3291
        else
3292
            return;
3293
            
3294
        if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
3295
            offset[0]=
3296
            offset[1]=
3297
            offset[2]=
3298
            offset[3]= 0;
3299
        }else{
3300
            offset[0]= y * s->linesize;;
3301
            offset[1]= 
3302
            offset[2]= (y>>1) * s->uvlinesize;;
3303
            offset[3]= 0;
3304
        }
3305

    
3306
        emms_c();
3307

    
3308
        s->avctx->draw_horiz_band(s->avctx, src, offset,
3309
                                  y, s->picture_structure, h);
3310
    }
3311
}
3312

    
3313
void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3314
    const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
3315
    const int uvlinesize= s->current_picture.linesize[1];
3316
        
3317
    s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3318
    s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1)     + s->mb_x*2;
3319
    s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
3320
    s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2)     + s->mb_x*2;
3321
    s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
3322
    s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
3323
    
3324
    if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
3325
        s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
3326
        s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
3327
        s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
3328
    }else{
3329
        s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize  ) + s->mb_x * 16 - 16;
3330
        s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
3331
        s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
3332
    }    
3333
}
3334

    
3335
#ifdef CONFIG_ENCODERS
3336

    
3337
static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
3338
    int x, y;
3339
//FIXME optimize
3340
    for(y=0; y<8; y++){
3341
        for(x=0; x<8; x++){
3342
            int x2, y2;
3343
            int sum=0;
3344
            int sqr=0;
3345
            int count=0;
3346

    
3347
            for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
3348
                for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
3349
                    int v= ptr[x2 + y2*stride];
3350
                    sum += v;
3351
                    sqr += v*v;
3352
                    count++;
3353
                }
3354
            }
3355
            weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
3356
        }
3357
    }
3358
}
3359

    
3360
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
3361
{
3362
    int16_t weight[6][64];
3363
    DCTELEM orig[6][64];
3364
    const int mb_x= s->mb_x;
3365
    const int mb_y= s->mb_y;
3366
    int i;
3367
    int skip_dct[6];
3368
    int dct_offset   = s->linesize*8; //default for progressive frames
3369
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
3370
    int wrap_y, wrap_c;
3371
    int emu=0;
3372
    
3373
    for(i=0; i<6; i++) skip_dct[i]=0;
3374
    
3375
    if(s->adaptive_quant){
3376
        const int last_qp= s->qscale;
3377
        const int mb_xy= mb_x + mb_y*s->mb_stride;
3378

    
3379
        s->lambda= s->lambda_table[mb_xy];
3380
        update_qscale(s);
3381
    
3382
        if(!(s->flags&CODEC_FLAG_QP_RD)){
3383
            s->dquant= s->qscale - last_qp;
3384

    
3385
            if(s->out_format==FMT_H263){
3386
                s->dquant= clip(s->dquant, -2, 2); //FIXME RD
3387
            
3388
                if(s->codec_id==CODEC_ID_MPEG4){        
3389
                    if(!s->mb_intra){
3390
                        if(s->pict_type == B_TYPE){
3391
                            if(s->dquant&1) 
3392
                                s->dquant= (s->dquant/2)*2;
3393
                            if(s->mv_dir&MV_DIRECT)
3394
                                s->dquant= 0;
3395
                        }
3396
                        if(s->mv_type==MV_TYPE_8X8)
3397
                            s->dquant=0;
3398
                    }
3399
                }
3400
            }
3401
        }
3402
        ff_set_qscale(s, last_qp + s->dquant);
3403
    }
3404

    
3405
    wrap_y = s->linesize;
3406
    wrap_c = s->uvlinesize;
3407
    ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
3408
    ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
3409
    ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
3410

    
3411
    if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
3412
        ff_emulated_edge_mc(s->edge_emu_buffer            , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width   , s->height);
3413
        ptr_y= s->edge_emu_buffer;
3414
        ff_emulated_edge_mc(s->edge_emu_buffer+16*wrap_y  , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
3415
        ptr_cb= s->edge_emu_buffer+16*wrap_y;
3416
        ff_emulated_edge_mc(s->edge_emu_buffer+16*wrap_y+8, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
3417
        ptr_cr= s->edge_emu_buffer+16*wrap_y+8;
3418
    }
3419

    
3420
    if (s->mb_intra) {
3421
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
3422
            int progressive_score, interlaced_score;
3423

    
3424
            s->interlaced_dct=0;
3425
            progressive_score= s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y, 8) 
3426
                              +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
3427

    
3428
            if(progressive_score > 0){
3429
                interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y*2, 8) 
3430
                                  +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y  , NULL, wrap_y*2, 8);
3431
                if(progressive_score > interlaced_score){
3432
                    s->interlaced_dct=1;
3433
            
3434
                    dct_offset= wrap_y;
3435
                    wrap_y<<=1;
3436
                }
3437
            }
3438
        }
3439
        
3440
        s->dsp.get_pixels(s->block[0], ptr_y                 , wrap_y);
3441
        s->dsp.get_pixels(s->block[1], ptr_y              + 8, wrap_y);
3442
        s->dsp.get_pixels(s->block[2], ptr_y + dct_offset    , wrap_y);
3443
        s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
3444

    
3445
        if(s->flags&CODEC_FLAG_GRAY){
3446
            skip_dct[4]= 1;
3447
            skip_dct[5]= 1;
3448
        }else{
3449
            s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
3450
            s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
3451
        }
3452
    }else{
3453
        op_pixels_func (*op_pix)[4];
3454
        qpel_mc_func (*op_qpix)[16];
3455
        uint8_t *dest_y, *dest_cb, *dest_cr;
3456

    
3457
        dest_y  = s->dest[0];
3458
        dest_cb = s->dest[1];
3459
        dest_cr = s->dest[2];
3460

    
3461
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
3462
            op_pix = s->dsp.put_pixels_tab;
3463
            op_qpix= s->dsp.put_qpel_pixels_tab;
3464
        }else{
3465
            op_pix = s->dsp.put_no_rnd_pixels_tab;
3466
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
3467
        }
3468

    
3469
        if (s->mv_dir & MV_DIR_FORWARD) {
3470
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
3471
            op_pix = s->dsp.avg_pixels_tab;
3472
            op_qpix= s->dsp.avg_qpel_pixels_tab;
3473
        }
3474
        if (s->mv_dir & MV_DIR_BACKWARD) {
3475
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
3476
        }
3477

    
3478
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
3479
            int progressive_score, interlaced_score;
3480

    
3481
            s->interlaced_dct=0;
3482
            progressive_score= s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y, 8) 
3483
                              +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
3484
            
3485
            if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
3486

    
3487
            if(progressive_score>0){
3488
                interlaced_score = s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y*2, 8) 
3489
                                  +s->dsp.ildct_cmp[0](s, dest_y + wrap_y  , ptr_y + wrap_y  , wrap_y*2, 8);
3490
            
3491
                if(progressive_score > interlaced_score){
3492
                    s->interlaced_dct=1;
3493
            
3494
                    dct_offset= wrap_y;
3495
                    wrap_y<<=1;
3496
                }
3497
            }
3498
        }
3499
        
3500
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
3501
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
3502
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
3503
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
3504
        
3505
        if(s->flags&CODEC_FLAG_GRAY){
3506
            skip_dct[4]= 1;
3507
            skip_dct[5]= 1;
3508
        }else{
3509
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
3510
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
3511
        }
3512
        /* pre quantization */         
3513
        if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
3514
            //FIXME optimize
3515
            if(s->dsp.sad[1](NULL, ptr_y               , dest_y               , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
3516
            if(s->dsp.sad[1](NULL, ptr_y            + 8, dest_y            + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
3517
            if(s->dsp.sad[1](NULL, ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
3518
            if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
3519
            if(s->dsp.sad[1](NULL, ptr_cb              , dest_cb              , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
3520
            if(s->dsp.sad[1](NULL, ptr_cr              , dest_cr              , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
3521
        }
3522
    }
3523

    
3524
    if(s->avctx->quantizer_noise_shaping){
3525
        if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y                 , wrap_y);
3526
        if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y              + 8, wrap_y);
3527
        if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
3528
        if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
3529
        if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb                , wrap_c);
3530
        if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr                , wrap_c);
3531
        memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
3532
    }
3533
            
3534
    /* DCT & quantize */
3535
    assert(s->out_format!=FMT_MJPEG || s->qscale==8);
3536
    {
3537
        for(i=0;i<6;i++) {
3538
            if(!skip_dct[i]){
3539
                int overflow;
3540
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
3541
            // FIXME we could decide to change to quantizer instead of clipping
3542
            // JS: I don't think that would be a good idea it could lower quality instead
3543
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
3544
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
3545
            }else
3546
                s->block_last_index[i]= -1;
3547
        }
3548
        if(s->avctx->quantizer_noise_shaping){
3549
            for(i=0;i<6;i++) {
3550
                if(!skip_dct[i]){
3551
                    s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
3552
                }
3553
            }
3554
        }
3555
        
3556
        if(s->luma_elim_threshold && !s->mb_intra)
3557
            for(i=0; i<4; i++)
3558
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
3559
        if(s->chroma_elim_threshold && !s->mb_intra)
3560
            for(i=4; i<6; i++)
3561
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
3562

    
3563
        if(s->flags & CODEC_FLAG_CBP_RD){
3564
            for(i=0;i<6;i++) {
3565
                if(s->block_last_index[i] == -1)
3566
                    s->coded_score[i]= INT_MAX/256;
3567
            }
3568
        }
3569
    }
3570

    
3571
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
3572
        s->block_last_index[4]=
3573
        s->block_last_index[5]= 0;
3574
        s->block[4][0]=
3575
        s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
3576
    }
3577

    
3578
    //non c quantize code returns incorrect block_last_index FIXME
3579
    if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
3580
        for(i=0; i<6; i++){
3581
            int j;
3582
            if(s->block_last_index[i]>0){
3583
                for(j=63; j>0; j--){
3584
                    if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
3585
                }
3586
                s->block_last_index[i]= j;
3587
            }
3588
        }
3589
    }
3590

    
3591
    /* huffman encode */
3592
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
3593
    case CODEC_ID_MPEG1VIDEO:
3594
    case CODEC_ID_MPEG2VIDEO:
3595
        mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
3596
#ifdef CONFIG_RISKY
3597
    case CODEC_ID_MPEG4:
3598
        mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3599
    case CODEC_ID_MSMPEG4V2:
3600
    case CODEC_ID_MSMPEG4V3:
3601
    case CODEC_ID_WMV1:
3602
        msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
3603
    case CODEC_ID_WMV2:
3604
         ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
3605
    case CODEC_ID_H263:
3606
    case CODEC_ID_H263P:
3607
    case CODEC_ID_FLV1:
3608
    case CODEC_ID_RV10:
3609
        h263_encode_mb(s, s->block, motion_x, motion_y); break;
3610
#endif
3611
    case CODEC_ID_MJPEG:
3612
        mjpeg_encode_mb(s, s->block); break;
3613
    default:
3614
        assert(0);
3615
    }
3616
}
3617

    
3618
#endif //CONFIG_ENCODERS
3619

    
3620
/**
3621
 * combines the (truncated) bitstream to a complete frame
3622
 * @returns -1 if no complete frame could be created
3623
 */
3624
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
3625
    ParseContext *pc= &s->parse_context;
3626

    
3627
#if 0
3628
    if(pc->overread){
3629
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3630
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3631
    }
3632
#endif
3633

    
3634
    /* copy overreaded byes from last frame into buffer */
3635
    for(; pc->overread>0; pc->overread--){
3636
        pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
3637
    }
3638
    
3639
    pc->last_index= pc->index;
3640

    
3641
    /* copy into buffer end return */
3642
    if(next == END_NOT_FOUND){
3643
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3644

    
3645
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
3646
        pc->index += *buf_size;
3647
        return -1;
3648
    }
3649

    
3650
    *buf_size=
3651
    pc->overread_index= pc->index + next;
3652
    
3653
    /* append to buffer */
3654
    if(pc->index){
3655
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3656

    
3657
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
3658
        pc->index = 0;
3659
        *buf= pc->buffer;
3660
    }
3661

    
3662
    /* store overread bytes */
3663
    for(;next < 0; next++){
3664
        pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
3665
        pc->overread++;
3666
    }
3667

    
3668
#if 0
3669
    if(pc->overread){
3670
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3671
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3672
    }
3673
#endif
3674

    
3675
    return 0;
3676
}
3677

    
3678
void ff_mpeg_flush(AVCodecContext *avctx){
3679
    int i;
3680
    MpegEncContext *s = avctx->priv_data;
3681
    
3682
    if(s==NULL || s->picture==NULL) 
3683
        return;
3684
    
3685
    for(i=0; i<MAX_PICTURE_COUNT; i++){
3686
       if(s->picture[i].data[0] && (   s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
3687
                                    || s->picture[i].type == FF_BUFFER_TYPE_USER))
3688
        avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
3689
    }
3690
    s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3691
    
3692
    s->parse_context.state= -1;
3693
    s->parse_context.frame_start_found= 0;
3694
    s->parse_context.overread= 0;
3695
    s->parse_context.overread_index= 0;
3696
    s->parse_context.index= 0;
3697
    s->parse_context.last_index= 0;
3698
}
3699

    
3700
#ifdef CONFIG_ENCODERS
3701
void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
3702
{
3703
    const uint16_t *srcw= (uint16_t*)src;
3704
    int words= length>>4;
3705
    int bits= length&15;
3706
    int i;
3707

    
3708
    if(length==0) return;
3709
    
3710
    if(words < 16){
3711
        for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
3712
    }else if(put_bits_count(pb)&7){
3713
        for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
3714
    }else{
3715
        for(i=0; put_bits_count(pb)&31; i++)
3716
            put_bits(pb, 8, src[i]);
3717
        flush_put_bits(pb);
3718
        memcpy(pbBufPtr(pb), src+i, 2*words-i);
3719
        skip_put_bytes(pb, 2*words-i);
3720
    }
3721
        
3722
    put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
3723
}
3724

    
3725
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
3726
    int i;
3727

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

    
3730
    /* mpeg1 */
3731
    d->mb_skip_run= s->mb_skip_run;
3732
    for(i=0; i<3; i++)
3733
        d->last_dc[i]= s->last_dc[i];
3734
    
3735
    /* statistics */
3736
    d->mv_bits= s->mv_bits;
3737
    d->i_tex_bits= s->i_tex_bits;
3738
    d->p_tex_bits= s->p_tex_bits;
3739
    d->i_count= s->i_count;
3740
    d->f_count= s->f_count;
3741
    d->b_count= s->b_count;
3742
    d->skip_count= s->skip_count;
3743
    d->misc_bits= s->misc_bits;
3744
    d->last_bits= 0;
3745

    
3746
    d->mb_skiped= 0;
3747
    d->qscale= s->qscale;
3748
    d->dquant= s->dquant;
3749
}
3750

    
3751
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
3752
    int i;
3753

    
3754
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
3755
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
3756
    
3757
    /* mpeg1 */
3758
    d->mb_skip_run= s->mb_skip_run;
3759
    for(i=0; i<3; i++)
3760
        d->last_dc[i]= s->last_dc[i];
3761
    
3762
    /* statistics */
3763
    d->mv_bits= s->mv_bits;
3764
    d->i_tex_bits= s->i_tex_bits;
3765
    d->p_tex_bits= s->p_tex_bits;
3766
    d->i_count= s->i_count;
3767
    d->f_count= s->f_count;
3768
    d->b_count= s->b_count;
3769
    d->skip_count= s->skip_count;
3770
    d->misc_bits= s->misc_bits;
3771

    
3772
    d->mb_intra= s->mb_intra;
3773
    d->mb_skiped= s->mb_skiped;
3774
    d->mv_type= s->mv_type;
3775
    d->mv_dir= s->mv_dir;
3776
    d->pb= s->pb;
3777
    if(s->data_partitioning){
3778
        d->pb2= s->pb2;
3779
        d->tex_pb= s->tex_pb;
3780
    }
3781
    d->block= s->block;
3782
    for(i=0; i<6; i++)
3783
        d->block_last_index[i]= s->block_last_index[i];
3784
    d->interlaced_dct= s->interlaced_dct;
3785
    d->qscale= s->qscale;
3786
}
3787

    
3788
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
3789
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
3790
                           int *dmin, int *next_block, int motion_x, int motion_y)
3791
{
3792
    int score;
3793
    uint8_t *dest_backup[3];
3794
    
3795
    copy_context_before_encode(s, backup, type);
3796

    
3797
    s->block= s->blocks[*next_block];
3798
    s->pb= pb[*next_block];
3799
    if(s->data_partitioning){
3800
        s->pb2   = pb2   [*next_block];
3801
        s->tex_pb= tex_pb[*next_block];
3802
    }
3803
    
3804
    if(*next_block){
3805
        memcpy(dest_backup, s->dest, sizeof(s->dest));
3806
        s->dest[0] = s->rd_scratchpad;
3807
        s->dest[1] = s->rd_scratchpad + 16*s->linesize;
3808
        s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
3809
        assert(s->linesize >= 32); //FIXME
3810
    }
3811

    
3812
    encode_mb(s, motion_x, motion_y);
3813
    
3814
    score= put_bits_count(&s->pb);
3815
    if(s->data_partitioning){
3816
        score+= put_bits_count(&s->pb2);
3817
        score+= put_bits_count(&s->tex_pb);
3818
    }
3819
   
3820
    if(s->avctx->mb_decision == FF_MB_DECISION_RD){
3821
        MPV_decode_mb(s, s->block);
3822

    
3823
        score *= s->lambda2;
3824
        score += sse_mb(s) << FF_LAMBDA_SHIFT;
3825
    }
3826
    
3827
    if(*next_block){
3828
        memcpy(s->dest, dest_backup, sizeof(s->dest));
3829
    }
3830

    
3831
    if(score<*dmin){
3832
        *dmin= score;
3833
        *next_block^=1;
3834

    
3835
        copy_context_after_encode(best, s, type);
3836
    }
3837
}
3838
                
3839
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
3840
    uint32_t *sq = squareTbl + 256;
3841
    int acc=0;
3842
    int x,y;
3843
    
3844
    if(w==16 && h==16) 
3845
        return s->dsp.sse[0](NULL, src1, src2, stride, 16);
3846
    else if(w==8 && h==8)
3847
        return s->dsp.sse[1](NULL, src1, src2, stride, 8);
3848
    
3849
    for(y=0; y<h; y++){
3850
        for(x=0; x<w; x++){
3851
            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
3852
        } 
3853
    }
3854
    
3855
    assert(acc>=0);
3856
    
3857
    return acc;
3858
}
3859

    
3860
static int sse_mb(MpegEncContext *s){
3861
    int w= 16;
3862
    int h= 16;
3863

    
3864
    if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3865
    if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3866

    
3867
    if(w==16 && h==16)
3868
        return  s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
3869
               +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
3870
               +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
3871
    else
3872
        return  sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
3873
               +sse(s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
3874
               +sse(s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
3875
}
3876

    
3877
static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
3878
    MpegEncContext *s= arg;
3879

    
3880
    
3881
    s->me.pre_pass=1;
3882
    s->me.dia_size= s->avctx->pre_dia_size;
3883
    s->first_slice_line=1;
3884
    for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
3885
        for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
3886
            ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
3887
        }
3888
        s->first_slice_line=0;
3889
    }
3890
    
3891
    s->me.pre_pass=0;
3892
    
3893
    return 0;
3894
}
3895

    
3896
static int estimate_motion_thread(AVCodecContext *c, void *arg){
3897
    MpegEncContext *s= arg;
3898

    
3899
    s->me.dia_size= s->avctx->dia_size;
3900
    s->first_slice_line=1;
3901
    for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
3902
        s->mb_x=0; //for block init below
3903
        ff_init_block_index(s);
3904
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
3905
            s->block_index[0]+=2;
3906
            s->block_index[1]+=2;
3907
            s->block_index[2]+=2;
3908
            s->block_index[3]+=2;
3909
            
3910
            /* compute motion vector & mb_type and store in context */
3911
            if(s->pict_type==B_TYPE)
3912
                ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
3913
            else
3914
                ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
3915
        }
3916
        s->first_slice_line=0;
3917
    }
3918
    return 0;
3919
}
3920

    
3921
static int mb_var_thread(AVCodecContext *c, void *arg){
3922
    MpegEncContext *s= arg;
3923
    int mb_x, mb_y;
3924

    
3925
    for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
3926
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3927
            int xx = mb_x * 16;
3928
            int yy = mb_y * 16;
3929
            uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
3930
            int varc;
3931
            int sum = s->dsp.pix_sum(pix, s->linesize);
3932
    
3933
            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
3934

    
3935
            s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
3936
            s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
3937
            s->mb_var_sum_temp    += varc;
3938
        }
3939
    }
3940
    return 0;
3941
}
3942

    
3943
static void write_slice_end(MpegEncContext *s){
3944
    if(s->codec_id==CODEC_ID_MPEG4){
3945
        if(s->partitioned_frame){
3946
            ff_mpeg4_merge_partitions(s);
3947
        }
3948
    
3949
        ff_mpeg4_stuffing(&s->pb);
3950
    }else if(s->out_format == FMT_MJPEG){
3951
        ff_mjpeg_stuffing(&s->pb);
3952
    }
3953

    
3954
    align_put_bits(&s->pb);
3955
    flush_put_bits(&s->pb);
3956
}
3957

    
3958
static int encode_thread(AVCodecContext *c, void *arg){
3959
    MpegEncContext *s= arg;
3960
    int mb_x, mb_y, pdif = 0;
3961
    int i, j;
3962
    MpegEncContext best_s, backup_s;
3963
    uint8_t bit_buf[2][3000];
3964
    uint8_t bit_buf2[2][3000];
3965
    uint8_t bit_buf_tex[2][3000];
3966
    PutBitContext pb[2], pb2[2], tex_pb[2];
3967
//printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
3968

    
3969
    for(i=0; i<2; i++){
3970
        init_put_bits(&pb    [i], bit_buf    [i], 3000);
3971
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000);
3972
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
3973
    }
3974

    
3975
    s->last_bits= put_bits_count(&s->pb);
3976
    s->mv_bits=0;
3977
    s->misc_bits=0;
3978
    s->i_tex_bits=0;
3979
    s->p_tex_bits=0;
3980
    s->i_count=0;
3981
    s->f_count=0;
3982
    s->b_count=0;
3983
    s->skip_count=0;
3984

    
3985
    for(i=0; i<3; i++){
3986
        /* init last dc values */
3987
        /* note: quant matrix value (8) is implied here */
3988
        s->last_dc[i] = 128;
3989
        
3990
        s->current_picture_ptr->error[i] = 0;
3991
    }
3992
    s->mb_skip_run = 0;
3993
    memset(s->last_mv, 0, sizeof(s->last_mv));
3994
     
3995
    s->last_mv_dir = 0;
3996

    
3997
#ifdef CONFIG_RISKY
3998
    switch(s->codec_id){
3999
    case CODEC_ID_H263:
4000
    case CODEC_ID_H263P:
4001
    case CODEC_ID_FLV1:
4002
        s->gob_index = ff_h263_get_gob_height(s);
4003
        break;
4004
    case CODEC_ID_MPEG4:
4005
        if(s->partitioned_frame)
4006
            ff_mpeg4_init_partitions(s);
4007
        break;
4008
    }
4009
#endif
4010

    
4011
    s->resync_mb_x=0;
4012
    s->resync_mb_y=0; 
4013
    s->first_slice_line = 1;
4014
    s->ptr_lastgob = s->pb.buf;
4015
    for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
4016
//    printf("row %d at %X\n", s->mb_y, (int)s);
4017
        s->mb_x=0;
4018
        s->mb_y= mb_y;
4019

    
4020
        ff_set_qscale(s, s->qscale);
4021
        ff_init_block_index(s);
4022
        
4023
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
4024
            const int xy= mb_y*s->mb_stride + mb_x;
4025
            int mb_type= s->mb_type[xy];
4026
//            int d;
4027
            int dmin= INT_MAX;
4028
            int dir;
4029

    
4030
            s->mb_x = mb_x;
4031
            ff_update_block_index(s);
4032

    
4033
            /* write gob / video packet header  */
4034
#ifdef CONFIG_RISKY
4035
            if(s->rtp_mode){
4036
                int current_packet_size, is_gob_start;
4037
                
4038
                current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
4039
                
4040
                is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0; 
4041
                
4042
                if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
4043
                
4044
                switch(s->codec_id){
4045
                case CODEC_ID_H263:
4046
                case CODEC_ID_H263P:
4047
                    if(!s->h263_slice_structured)
4048
                        if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
4049
                    break;
4050
                case CODEC_ID_MPEG2VIDEO:
4051
                    if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
4052
                case CODEC_ID_MPEG1VIDEO:
4053
                    if(s->mb_skip_run) is_gob_start=0;
4054
                    break;
4055
                }
4056

    
4057
                if(is_gob_start){
4058
                    if(s->start_mb_y != mb_y || mb_x!=0){
4059
                        write_slice_end(s);
4060

    
4061
                        if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
4062
                            ff_mpeg4_init_partitions(s);
4063
                        }
4064
                    }
4065
                
4066
                    assert((put_bits_count(&s->pb)&7) == 0);
4067
                    current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
4068
                    
4069
                    if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
4070
                        int r= put_bits_count(&s->pb)/8 + s->picture_number + s->codec_id + s->mb_x + s->mb_y;
4071
                        int d= 100 / s->avctx->error_rate;
4072
                        if(r % d == 0){
4073
                            current_packet_size=0;
4074
#ifndef ALT_BITSTREAM_WRITER
4075
                            s->pb.buf_ptr= s->ptr_lastgob;
4076
#endif
4077
                            assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
4078
                        }
4079
                    }
4080
        
4081
                    if (s->avctx->rtp_callback)
4082
                        s->avctx->rtp_callback(s->ptr_lastgob, current_packet_size, 0);
4083
                    
4084
                    switch(s->codec_id){
4085
                    case CODEC_ID_MPEG4:
4086
                        ff_mpeg4_encode_video_packet_header(s);
4087
                        ff_mpeg4_clean_buffers(s);
4088
                    break;
4089
                    case CODEC_ID_MPEG1VIDEO:
4090
                    case CODEC_ID_MPEG2VIDEO:
4091
                        ff_mpeg1_encode_slice_header(s);
4092
                        ff_mpeg1_clean_buffers(s);
4093
                    break;
4094
                    case CODEC_ID_H263:
4095
                    case CODEC_ID_H263P:
4096
                        h263_encode_gob_header(s, mb_y);                       
4097
                    break;
4098
                    }
4099

    
4100
                    if(s->flags&CODEC_FLAG_PASS1){
4101
                        int bits= put_bits_count(&s->pb);
4102
                        s->misc_bits+= bits - s->last_bits;
4103
                        s->last_bits= bits;
4104
                    }
4105
    
4106
                    s->ptr_lastgob += current_packet_size;
4107
                    s->first_slice_line=1;
4108
                    s->resync_mb_x=mb_x;
4109
                    s->resync_mb_y=mb_y;
4110
                }
4111
            }
4112
#endif
4113

    
4114
            if(  (s->resync_mb_x   == s->mb_x)
4115
               && s->resync_mb_y+1 == s->mb_y){
4116
                s->first_slice_line=0; 
4117
            }
4118

    
4119
            s->mb_skiped=0;
4120
            s->dquant=0; //only for QP_RD
4121

    
4122
            if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
4123
                int next_block=0;
4124
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
4125

    
4126
                copy_context_before_encode(&backup_s, s, -1);
4127
                backup_s.pb= s->pb;
4128
                best_s.data_partitioning= s->data_partitioning;
4129
                best_s.partitioned_frame= s->partitioned_frame;
4130
                if(s->data_partitioning){
4131
                    backup_s.pb2= s->pb2;
4132
                    backup_s.tex_pb= s->tex_pb;
4133
                }
4134

    
4135
                if(mb_type&CANDIDATE_MB_TYPE_INTER){
4136
                    s->mv_dir = MV_DIR_FORWARD;
4137
                    s->mv_type = MV_TYPE_16X16;
4138
                    s->mb_intra= 0;
4139
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
4140
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
4141
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb, 
4142
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4143
                }
4144
                if(mb_type&CANDIDATE_MB_TYPE_INTER_I){ 
4145
                    s->mv_dir = MV_DIR_FORWARD;
4146
                    s->mv_type = MV_TYPE_FIELD;
4147
                    s->mb_intra= 0;
4148
                    for(i=0; i<2; i++){
4149
                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
4150
                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
4151
                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
4152
                    }
4153
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb, 
4154
                                 &dmin, &next_block, 0, 0);
4155
                }
4156
                if(mb_type&CANDIDATE_MB_TYPE_SKIPED){
4157
                    s->mv_dir = MV_DIR_FORWARD;
4158
                    s->mv_type = MV_TYPE_16X16;
4159
                    s->mb_intra= 0;
4160
                    s->mv[0][0][0] = 0;
4161
                    s->mv[0][0][1] = 0;
4162
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb, 
4163
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4164
                }
4165
                if(mb_type&CANDIDATE_MB_TYPE_INTER4V){                 
4166
                    s->mv_dir = MV_DIR_FORWARD;
4167
                    s->mv_type = MV_TYPE_8X8;
4168
                    s->mb_intra= 0;
4169
                    for(i=0; i<4; i++){
4170
                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
4171
                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
4172
                    }
4173
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb, 
4174
                                 &dmin, &next_block, 0, 0);
4175
                }
4176
                if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
4177
                    s->mv_dir = MV_DIR_FORWARD;
4178
                    s->mv_type = MV_TYPE_16X16;
4179
                    s->mb_intra= 0;
4180
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
4181
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
4182
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb, 
4183
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4184
                }
4185
                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
4186
                    s->mv_dir = MV_DIR_BACKWARD;
4187
                    s->mv_type = MV_TYPE_16X16;
4188
                    s->mb_intra= 0;
4189
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
4190
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
4191
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
4192
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
4193
                }
4194
                if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
4195
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4196
                    s->mv_type = MV_TYPE_16X16;
4197
                    s->mb_intra= 0;
4198
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
4199
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
4200
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
4201
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
4202
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb, 
4203
                                 &dmin, &next_block, 0, 0);
4204
                }
4205
                if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
4206
                    int mx= s->b_direct_mv_table[xy][0];
4207
                    int my= s->b_direct_mv_table[xy][1];
4208
                    
4209
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4210
                    s->mb_intra= 0;
4211
#ifdef CONFIG_RISKY
4212
                    ff_mpeg4_set_direct_mv(s, mx, my);
4213
#endif
4214
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, 
4215
                                 &dmin, &next_block, mx, my);
4216
                }
4217
                if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){ 
4218
                    s->mv_dir = MV_DIR_FORWARD;
4219
                    s->mv_type = MV_TYPE_FIELD;
4220
                    s->mb_intra= 0;
4221
                    for(i=0; i<2; i++){
4222
                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
4223
                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
4224
                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
4225
                    }
4226
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb, 
4227
                                 &dmin, &next_block, 0, 0);
4228
                }
4229
                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){ 
4230
                    s->mv_dir = MV_DIR_BACKWARD;
4231
                    s->mv_type = MV_TYPE_FIELD;
4232
                    s->mb_intra= 0;
4233
                    for(i=0; i<2; i++){
4234
                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
4235
                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
4236
                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
4237
                    }
4238
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb, 
4239
                                 &dmin, &next_block, 0, 0);
4240
                }
4241
                if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){ 
4242
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4243
                    s->mv_type = MV_TYPE_FIELD;
4244
                    s->mb_intra= 0;
4245
                    for(dir=0; dir<2; dir++){
4246
                        for(i=0; i<2; i++){
4247
                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
4248
                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
4249
                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
4250
                        }
4251
                    }
4252
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb, 
4253
                                 &dmin, &next_block, 0, 0);
4254
                }
4255
                if(mb_type&CANDIDATE_MB_TYPE_INTRA){
4256
                    s->mv_dir = 0;
4257
                    s->mv_type = MV_TYPE_16X16;
4258
                    s->mb_intra= 1;
4259
                    s->mv[0][0][0] = 0;
4260
                    s->mv[0][0][1] = 0;
4261
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb, 
4262
                                 &dmin, &next_block, 0, 0);
4263
                    if(s->h263_pred || s->h263_aic){
4264
                        if(best_s.mb_intra)
4265
                            s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
4266
                        else
4267
                            ff_clean_intra_table_entries(s); //old mode?
4268
                    }
4269
                }
4270

    
4271
                if(s->flags & CODEC_FLAG_QP_RD){
4272
                    if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
4273
                        const int last_qp= backup_s.qscale;
4274
                        int dquant, dir, qp, dc[6];
4275
                        DCTELEM ac[6][16];
4276
                        const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
4277
                        
4278
                        assert(backup_s.dquant == 0);
4279

    
4280
                        //FIXME intra
4281
                        s->mv_dir= best_s.mv_dir;
4282
                        s->mv_type = MV_TYPE_16X16;
4283
                        s->mb_intra= best_s.mb_intra;
4284
                        s->mv[0][0][0] = best_s.mv[0][0][0];
4285
                        s->mv[0][0][1] = best_s.mv[0][0][1];
4286
                        s->mv[1][0][0] = best_s.mv[1][0][0];
4287
                        s->mv[1][0][1] = best_s.mv[1][0][1];
4288
                        
4289
                        dir= s->pict_type == B_TYPE ? 2 : 1;
4290
                        if(last_qp + dir > s->avctx->qmax) dir= -dir;
4291
                        for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
4292
                            qp= last_qp + dquant;
4293
                            if(qp < s->avctx->qmin || qp > s->avctx->qmax)
4294
                                break;
4295
                            backup_s.dquant= dquant;
4296
                            if(s->mb_intra){
4297
                                for(i=0; i<6; i++){
4298
                                    dc[i]= s->dc_val[0][ s->block_index[i] ];
4299
                                    memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
4300
                                }
4301
                            }
4302

    
4303
                            encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, 
4304
                                         &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
4305
                            if(best_s.qscale != qp){
4306
                                if(s->mb_intra){
4307
                                    for(i=0; i<6; i++){
4308
                                        s->dc_val[0][ s->block_index[i] ]= dc[i];
4309
                                        memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
4310
                                    }
4311
                                }
4312
                                if(dir > 0 && dquant==dir){
4313
                                    dquant= 0;
4314
                                    dir= -dir;
4315
                                }else
4316
                                    break;
4317
                            }
4318
                        }
4319
                        qp= best_s.qscale;
4320
                        s->current_picture.qscale_table[xy]= qp;
4321
                    }
4322
                }
4323

    
4324
                copy_context_after_encode(s, &best_s, -1);
4325
                
4326
                pb_bits_count= put_bits_count(&s->pb);
4327
                flush_put_bits(&s->pb);
4328
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
4329
                s->pb= backup_s.pb;
4330
                
4331
                if(s->data_partitioning){
4332
                    pb2_bits_count= put_bits_count(&s->pb2);
4333
                    flush_put_bits(&s->pb2);
4334
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
4335
                    s->pb2= backup_s.pb2;
4336
                    
4337
                    tex_pb_bits_count= put_bits_count(&s->tex_pb);
4338
                    flush_put_bits(&s->tex_pb);
4339
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
4340
                    s->tex_pb= backup_s.tex_pb;
4341
                }
4342
                s->last_bits= put_bits_count(&s->pb);
4343
               
4344
#ifdef CONFIG_RISKY
4345
                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
4346
                    ff_h263_update_motion_val(s);
4347
#endif
4348
        
4349
                if(next_block==0){ //FIXME 16 vs linesize16
4350
                    s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
4351
                    s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
4352
                    s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
4353
                }
4354

    
4355
                if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
4356
                    MPV_decode_mb(s, s->block);
4357
            } else {
4358
                int motion_x, motion_y;
4359
                s->mv_type=MV_TYPE_16X16;
4360
                // only one MB-Type possible
4361
                
4362
                switch(mb_type){
4363
                case CANDIDATE_MB_TYPE_INTRA:
4364
                    s->mv_dir = 0;
4365
                    s->mb_intra= 1;
4366
                    motion_x= s->mv[0][0][0] = 0;
4367
                    motion_y= s->mv[0][0][1] = 0;
4368
                    break;
4369
                case CANDIDATE_MB_TYPE_INTER:
4370
                    s->mv_dir = MV_DIR_FORWARD;
4371
                    s->mb_intra= 0;
4372
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
4373
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
4374
                    break;
4375
                case CANDIDATE_MB_TYPE_INTER_I:
4376
                    s->mv_dir = MV_DIR_FORWARD;
4377
                    s->mv_type = MV_TYPE_FIELD;
4378
                    s->mb_intra= 0;
4379
                    for(i=0; i<2; i++){
4380
                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
4381
                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
4382
                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
4383
                    }
4384
                    motion_x = motion_y = 0;
4385
                    break;
4386
                case CANDIDATE_MB_TYPE_INTER4V:
4387
                    s->mv_dir = MV_DIR_FORWARD;
4388
                    s->mv_type = MV_TYPE_8X8;
4389
                    s->mb_intra= 0;
4390
                    for(i=0; i<4; i++){
4391
                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
4392
                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
4393
                    }
4394
                    motion_x= motion_y= 0;
4395
                    break;
4396
                case CANDIDATE_MB_TYPE_DIRECT:
4397
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4398
                    s->mb_intra= 0;
4399
                    motion_x=s->b_direct_mv_table[xy][0];
4400
                    motion_y=s->b_direct_mv_table[xy][1];
4401
#ifdef CONFIG_RISKY
4402
                    ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
4403
#endif
4404
                    break;
4405
                case CANDIDATE_MB_TYPE_BIDIR:
4406
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4407
                    s->mb_intra= 0;
4408
                    motion_x=0;
4409
                    motion_y=0;
4410
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
4411
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
4412
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
4413
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
4414
                    break;
4415
                case CANDIDATE_MB_TYPE_BACKWARD:
4416
                    s->mv_dir = MV_DIR_BACKWARD;
4417
                    s->mb_intra= 0;
4418
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
4419
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
4420
                    break;
4421
                case CANDIDATE_MB_TYPE_FORWARD:
4422
                    s->mv_dir = MV_DIR_FORWARD;
4423
                    s->mb_intra= 0;
4424
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
4425
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
4426
//                    printf(" %d %d ", motion_x, motion_y);
4427
                    break;
4428
                case CANDIDATE_MB_TYPE_FORWARD_I:
4429
                    s->mv_dir = MV_DIR_FORWARD;
4430
                    s->mv_type = MV_TYPE_FIELD;
4431
                    s->mb_intra= 0;
4432
                    for(i=0; i<2; i++){
4433
                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
4434
                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
4435
                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
4436
                    }
4437
                    motion_x=motion_y=0;
4438
                    break;
4439
                case CANDIDATE_MB_TYPE_BACKWARD_I:
4440
                    s->mv_dir = MV_DIR_BACKWARD;
4441
                    s->mv_type = MV_TYPE_FIELD;
4442
                    s->mb_intra= 0;
4443
                    for(i=0; i<2; i++){
4444
                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
4445
                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
4446
                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
4447
                    }
4448
                    motion_x=motion_y=0;
4449
                    break;
4450
                case CANDIDATE_MB_TYPE_BIDIR_I:
4451
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4452
                    s->mv_type = MV_TYPE_FIELD;
4453
                    s->mb_intra= 0;
4454
                    for(dir=0; dir<2; dir++){
4455
                        for(i=0; i<2; i++){
4456
                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
4457
                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
4458
                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
4459
                        }
4460
                    }
4461
                    motion_x=motion_y=0;
4462
                    break;
4463
                default:
4464
                    motion_x=motion_y=0; //gcc warning fix
4465
                    av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
4466
                }
4467

    
4468
                encode_mb(s, motion_x, motion_y);
4469

    
4470
                // RAL: Update last macrobloc type
4471
                s->last_mv_dir = s->mv_dir;
4472
            
4473
#ifdef CONFIG_RISKY
4474
                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
4475
                    ff_h263_update_motion_val(s);
4476
#endif
4477
                
4478
                MPV_decode_mb(s, s->block);
4479
            }
4480

    
4481
            /* clean the MV table in IPS frames for direct mode in B frames */
4482
            if(s->mb_intra /* && I,P,S_TYPE */){
4483
                s->p_mv_table[xy][0]=0;
4484
                s->p_mv_table[xy][1]=0;
4485
            }
4486
            
4487
            if(s->flags&CODEC_FLAG_PSNR){
4488
                int w= 16;
4489
                int h= 16;
4490

    
4491
                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
4492
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
4493

    
4494
                s->current_picture_ptr->error[0] += sse(
4495
                    s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
4496
                    s->dest[0], w, h, s->linesize);
4497
                s->current_picture_ptr->error[1] += sse(
4498
                    s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
4499
                    s->dest[1], w>>1, h>>1, s->uvlinesize);
4500
                s->current_picture_ptr->error[2] += sse(
4501
                    s, s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
4502
                    s->dest[2], w>>1, h>>1, s->uvlinesize);
4503
            }
4504
            if(s->loop_filter)
4505
                ff_h263_loop_filter(s);
4506
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
4507
        }
4508
    }
4509

    
4510
#ifdef CONFIG_RISKY
4511
    //not beautifull here but we must write it before flushing so it has to be here
4512
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
4513
        msmpeg4_encode_ext_header(s);
4514
#endif
4515

    
4516
    write_slice_end(s);
4517

    
4518
    /* Send the last GOB if RTP */    
4519
    if (s->avctx->rtp_callback) {
4520
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
4521
        /* Call the RTP callback to send the last GOB */
4522
        emms_c();
4523
        s->avctx->rtp_callback(s->ptr_lastgob, pdif, 0);
4524
    }
4525

    
4526
    return 0;
4527
}
4528

    
4529
#define MERGE(field) dst->field += src->field; src->field=0
4530
static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
4531
    MERGE(scene_change_score);
4532
    MERGE(mc_mb_var_sum_temp);
4533
    MERGE(mb_var_sum_temp);
4534
}
4535

    
4536
static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
4537
    int i;
4538

    
4539
    MERGE(dct_count[0]); //note, the other dct vars are not part of the context
4540
    MERGE(dct_count[1]);
4541
    MERGE(mv_bits);
4542
    MERGE(header_bits);
4543
    MERGE(i_tex_bits);
4544
    MERGE(p_tex_bits);
4545
    MERGE(i_count);
4546
    MERGE(f_count);
4547
    MERGE(b_count);
4548
    MERGE(skip_count);
4549
    MERGE(misc_bits);
4550
    MERGE(error_count);
4551
    MERGE(padding_bug_score);
4552

    
4553
    if(dst->avctx->noise_reduction){
4554
        for(i=0; i<64; i++){
4555
            MERGE(dct_error_sum[0][i]);
4556
            MERGE(dct_error_sum[1][i]);
4557
        }
4558
    }
4559
    
4560
    assert(put_bits_count(&src->pb) % 8 ==0);
4561
    assert(put_bits_count(&dst->pb) % 8 ==0);
4562
    ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
4563
    flush_put_bits(&dst->pb);
4564
}
4565

    
4566
static void encode_picture(MpegEncContext *s, int picture_number)
4567
{
4568
    int mb_x, mb_y;
4569
    int i, j;
4570
    int bits;
4571

    
4572
    s->picture_number = picture_number;
4573
    
4574
    /* Reset the average MB variance */
4575
    s->mb_var_sum_temp    =
4576
    s->mc_mb_var_sum_temp = 0;
4577

    
4578
#ifdef CONFIG_RISKY
4579
    /* we need to initialize some time vars before we can encode b-frames */
4580
    // RAL: Condition added for MPEG1VIDEO
4581
    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
4582
        ff_set_mpeg4_time(s, s->picture_number);  //FIXME rename and use has_b_frames or similar
4583
#endif
4584
        
4585
    s->scene_change_score=0;
4586
    
4587
    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
4588
    
4589
    if(s->pict_type==I_TYPE){
4590
        if(s->msmpeg4_version >= 3) s->no_rounding=1;
4591
        else                        s->no_rounding=0;
4592
    }else if(s->pict_type!=B_TYPE){
4593
        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
4594
            s->no_rounding ^= 1;          
4595
    }
4596
    
4597
    s->mb_intra=0; //for the rate distoration & bit compare functions
4598
    for(i=1; i<s->avctx->thread_count; i++){
4599
        ff_update_duplicate_context(s->thread_context[i], s);
4600
    }
4601
    
4602
    /* Estimate motion for every MB */
4603
    if(s->pict_type != I_TYPE){
4604
        if(s->pict_type != B_TYPE){
4605
            if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
4606
                s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
4607
            }
4608
        }
4609

    
4610
        s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
4611
    }else /* if(s->pict_type == I_TYPE) */{
4612
        /* I-Frame */
4613
        for(i=0; i<s->mb_stride*s->mb_height; i++)
4614
            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
4615
        
4616
        if(!s->fixed_qscale){
4617
            /* finding spatial complexity for I-frame rate control */
4618
            s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
4619
        }
4620
    }
4621
    for(i=1; i<s->avctx->thread_count; i++){
4622
        merge_context_after_me(s, s->thread_context[i]);
4623
    }
4624
    s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->mc_mb_var_sum_temp;
4625
    s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->   mb_var_sum_temp;
4626
    emms_c();
4627

    
4628
    if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
4629
        s->pict_type= I_TYPE;
4630
        for(i=0; i<s->mb_stride*s->mb_height; i++)
4631
            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
4632
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
4633
    }
4634

    
4635
    if(!s->umvplus){
4636
        if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
4637
            s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
4638

    
4639
            if(s->flags & CODEC_FLAG_INTERLACED_ME){
4640
                int a,b;
4641
                a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
4642
                b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
4643
                s->f_code= FFMAX(s->f_code, FFMAX(a,b));
4644
            }
4645
                    
4646
            ff_fix_long_p_mvs(s);
4647
            ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
4648
            if(s->flags & CODEC_FLAG_INTERLACED_ME){
4649
                for(i=0; i<2; i++){
4650
                    for(j=0; j<2; j++)
4651
                        ff_fix_long_mvs(s, s->p_field_select_table[i], j, 
4652
                                        s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
4653
                }
4654
            }
4655
        }
4656

    
4657
        if(s->pict_type==B_TYPE){
4658
            int a, b;
4659

    
4660
            a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
4661
            b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
4662
            s->f_code = FFMAX(a, b);
4663

    
4664