Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 68ca24e6

History | View | Annotate | Download (175 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 cf8039b2 Michael Niedermayer
 *
19 9dbf1ddd Michael Niedermayer
 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
20 de6d9b64 Fabrice Bellard
 */
21 202ef8b8 Michael Niedermayer
 
22 983e3246 Michael Niedermayer
/**
23
 * @file mpegvideo.c
24
 * The simplest mpeg encoder (well, it was the simplest!).
25
 */ 
26
 
27 f943e138 Michael Niedermayer
#include <limits.h>
28 de6d9b64 Fabrice Bellard
#include "avcodec.h"
29
#include "dsputil.h"
30
#include "mpegvideo.h"
31 65e4c8c9 Michael Niedermayer
#include "faandct.h"
32 de6d9b64 Fabrice Bellard
33 54329dd5 Nick Kurshev
#ifdef USE_FASTMEMCPY
34
#include "fastmemcpy.h"
35
#endif
36
37 e4eadb4b Michael Niedermayer
//#undef NDEBUG
38
//#include <assert.h>
39 2ad1516a Michael Niedermayer
40 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
41 21af69f7 Fabrice Bellard
static void encode_picture(MpegEncContext *s, int picture_number);
42 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
43 21af69f7 Fabrice Bellard
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
44
                                   DCTELEM *block, int n, int qscale);
45 9dbf1ddd Michael Niedermayer
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46
                                   DCTELEM *block, int n, int qscale);
47 21af69f7 Fabrice Bellard
static void dct_unquantize_h263_c(MpegEncContext *s, 
48
                                  DCTELEM *block, int n, int qscale);
49 0c1a9eda Zdenek Kabelac
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
51 d7e9533a Michael Niedermayer
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52 477ab036 Michael Niedermayer
static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
53 7d1c3fc1 Michael Niedermayer
static int sse_mb(MpegEncContext *s);
54 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
55 3d9fccbf Michael Niedermayer
56 2e7b4c84 Ivan Kalvachev
#ifdef HAVE_XVMC
57
extern int  XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
58
extern void XVMC_field_end(MpegEncContext *s);
59 a579db0c Ivan Kalvachev
extern void XVMC_decode_mb(MpegEncContext *s);
60 2e7b4c84 Ivan Kalvachev
#endif
61
62 0c1a9eda Zdenek Kabelac
void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
63 3d9fccbf Michael Niedermayer
64 de6d9b64 Fabrice Bellard
65
/* enable all paranoid tests for rounding, overflows, etc... */
66
//#define PARANOID
67
68
//#define DEBUG
69
70 101bea5f Juanjo
71 de6d9b64 Fabrice Bellard
/* for jpeg fast DCT */
72
#define CONST_BITS 14
73
74 eb4b3dd3 Zdenek Kabelac
static const uint16_t aanscales[64] = {
75 de6d9b64 Fabrice Bellard
    /* precomputed values scaled up by 14 bits */
76
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
77
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
78
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
79
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
80
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
81
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
82 1457ab52 Michael Niedermayer
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
83
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
84 de6d9b64 Fabrice Bellard
};
85
86 eb4b3dd3 Zdenek Kabelac
static const uint8_t h263_chroma_roundtab[16] = {
87 67725183 Michael Niedermayer
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
88 de6d9b64 Fabrice Bellard
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
89
};
90
91 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
92 30952237 Michael Niedermayer
static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
93 0c1a9eda Zdenek Kabelac
static uint8_t default_fcode_tab[MAX_MV*2+1];
94 45870f57 Michael Niedermayer
95 a33c7159 Michael Niedermayer
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
96
97 6b56c616 Michael Niedermayer
static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
98 0c1a9eda Zdenek Kabelac
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax)
99 de6d9b64 Fabrice Bellard
{
100 d7e9533a Michael Niedermayer
    int qscale;
101
102 cc7ac888 Michael Niedermayer
    for(qscale=qmin; qscale<=qmax; qscale++){
103 d7e9533a Michael Niedermayer
        int i;
104 6b56c616 Michael Niedermayer
        if (dsp->fdct == ff_jpeg_fdct_islow 
105 b4c3816c Michael Niedermayer
#ifdef FAAN_POSTSCALE
106 6b56c616 Michael Niedermayer
            || dsp->fdct == ff_faandct
107 b4c3816c Michael Niedermayer
#endif
108
            ) {
109 28db7fce Michael Niedermayer
            for(i=0;i<64;i++) {
110 6b56c616 Michael Niedermayer
                const int j= dsp->idct_permutation[i];
111 28db7fce Michael Niedermayer
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
112
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
113
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
115
                
116 0c1a9eda Zdenek Kabelac
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
117 28db7fce Michael Niedermayer
                                (qscale * quant_matrix[j]));
118
            }
119 6b56c616 Michael Niedermayer
        } else if (dsp->fdct == fdct_ifast
120 b4c3816c Michael Niedermayer
#ifndef FAAN_POSTSCALE
121 6b56c616 Michael Niedermayer
                   || dsp->fdct == ff_faandct
122 b4c3816c Michael Niedermayer
#endif
123
                   ) {
124 d7e9533a Michael Niedermayer
            for(i=0;i<64;i++) {
125 6b56c616 Michael Niedermayer
                const int j= dsp->idct_permutation[i];
126 d7e9533a Michael Niedermayer
                /* 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 0c1a9eda Zdenek Kabelac
                qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 
132 d7e9533a Michael Niedermayer
                                (aanscales[i] * qscale * quant_matrix[j]));
133
            }
134
        } else {
135
            for(i=0;i<64;i++) {
136 6b56c616 Michael Niedermayer
                const int j= dsp->idct_permutation[i];
137 d7e9533a Michael Niedermayer
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
138
                   So 16           <= qscale * quant_matrix[i]             <= 7905
139
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
140
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
141
                */
142 0c1a9eda Zdenek Kabelac
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
143 477ab036 Michael Niedermayer
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
144 642ccefb Michael Niedermayer
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
145 d7e9533a Michael Niedermayer
146 642ccefb Michael Niedermayer
                if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
147
                qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
148 d7e9533a Michael Niedermayer
            }
149 de6d9b64 Fabrice Bellard
        }
150
    }
151
}
152 158c7f05 Michael Niedermayer
153
static inline void update_qscale(MpegEncContext *s){
154
    s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
155
    s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
156
    
157
    s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
158
}
159 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
160
161 3d2e8cce Michael Niedermayer
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
162 2ad1516a Michael Niedermayer
    int i;
163
    int end;
164 7801d21d Michael Niedermayer
    
165
    st->scantable= src_scantable;
166 2ad1516a Michael Niedermayer
167
    for(i=0; i<64; i++){
168
        int j;
169
        j = src_scantable[i];
170 3d2e8cce Michael Niedermayer
        st->permutated[i] = permutation[j];
171 05c4072b Michael Niedermayer
#ifdef ARCH_POWERPC
172
        st->inverse[j] = i;
173
#endif
174 2ad1516a Michael Niedermayer
    }
175
    
176
    end=-1;
177
    for(i=0; i<64; i++){
178
        int j;
179
        j = st->permutated[i];
180
        if(j>end) end=j;
181
        st->raster_end[i]= end;
182
    }
183
}
184
185 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
186 d6eb3c50 Michael Niedermayer
void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
187
    int i;
188
189
    if(matrix){
190
        put_bits(pb, 1, 1);
191
        for(i=0;i<64;i++) {
192
            put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
193
        }
194
    }else
195
        put_bits(pb, 1, 0);
196
}
197 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
198 d6eb3c50 Michael Niedermayer
199 defdfc9a Alex Beregszaszi
/* init common dct for both encoder and decoder */
200
int DCT_common_init(MpegEncContext *s)
201 de6d9b64 Fabrice Bellard
{
202 3bf43d42 Michael Niedermayer
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
203 9dbf1ddd Michael Niedermayer
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
204
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
205 b0368839 Michael Niedermayer
206 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
207 28db7fce Michael Niedermayer
    s->dct_quantize= dct_quantize_c;
208 b0368839 Michael Niedermayer
#endif
209 21af69f7 Fabrice Bellard
        
210
#ifdef HAVE_MMX
211 e4eadb4b Michael Niedermayer
    MPV_common_init_mmx(s);
212 a9b3f630 Nick Kurshev
#endif
213 e0580f8c Falk H├╝ffner
#ifdef ARCH_ALPHA
214
    MPV_common_init_axp(s);
215
#endif
216 c7e07931 Martin Olschewski
#ifdef HAVE_MLIB
217
    MPV_common_init_mlib(s);
218
#endif
219 5917d17c Leon van Stuivenberg
#ifdef HAVE_MMI
220
    MPV_common_init_mmi(s);
221
#endif
222 676e200c Michael Niedermayer
#ifdef ARCH_ARMV4L
223 83f238cb Zdenek Kabelac
    MPV_common_init_armv4l(s);
224 676e200c Michael Niedermayer
#endif
225 05c4072b Michael Niedermayer
#ifdef ARCH_POWERPC
226
    MPV_common_init_ppc(s);
227
#endif
228 676e200c Michael Niedermayer
229 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
230 3a87ac94 Michael Niedermayer
    s->fast_dct_quantize= s->dct_quantize;
231
232 477ab036 Michael Niedermayer
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
233
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
234
    }
235
236 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
237
238 2ad1516a Michael Niedermayer
    /* load & permutate scantables
239
       note: only wmv uses differnt ones 
240
    */
241 3d2e8cce Michael Niedermayer
    ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
242
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
243
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
244
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
245 d930ef19 Michael Niedermayer
246 dfb476cb Michael Niedermayer
    s->picture_structure= PICT_FRAME;
247
    
248 defdfc9a Alex Beregszaszi
    return 0;
249
}
250
251 6571e41d Michael Niedermayer
static void copy_picture(Picture *dst, Picture *src){
252
    *dst = *src;
253
    dst->type= FF_BUFFER_TYPE_COPY;
254
}
255
256 1e491e29 Michael Niedermayer
/**
257 4e00e76b Michael Niedermayer
 * allocates a Picture
258
 * The pixels are allocated/set by calling get_buffer() if shared=0
259 1e491e29 Michael Niedermayer
 */
260 4e00e76b Michael Niedermayer
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
261 7bc9090a Michael Niedermayer
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
262
    const int mb_array_size= s->mb_stride*s->mb_height;
263 0da71265 Michael Niedermayer
    int i;
264 7bc9090a Michael Niedermayer
    
265 4e00e76b Michael Niedermayer
    if(shared){
266
        assert(pic->data[0]);
267
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
268
        pic->type= FF_BUFFER_TYPE_SHARED;
269
    }else{
270
        int r;
271
        
272
        assert(!pic->data[0]);
273
        
274 492cd3a9 Michael Niedermayer
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
275 4e00e76b Michael Niedermayer
        
276
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
277 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
278 4e00e76b Michael Niedermayer
            return -1;
279
        }
280
281
        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
282 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
283 4e00e76b Michael Niedermayer
            return -1;
284
        }
285
286
        if(pic->linesize[1] != pic->linesize[2]){
287 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
288 4e00e76b Michael Niedermayer
            return -1;
289
        }
290
291
        s->linesize  = pic->linesize[0];
292
        s->uvlinesize= pic->linesize[1];
293 1e491e29 Michael Niedermayer
    }
294 4e00e76b Michael Niedermayer
    
295
    if(pic->qscale_table==NULL){
296
        if (s->encoding) {        
297 7bc9090a Michael Niedermayer
            CHECKED_ALLOCZ(pic->mb_var   , mb_array_size * sizeof(int16_t))
298
            CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
299
            CHECKED_ALLOCZ(pic->mb_mean  , mb_array_size * sizeof(int8_t))
300 4e00e76b Michael Niedermayer
        }
301 1e491e29 Michael Niedermayer
302 7bc9090a Michael Niedermayer
        CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
303 e4eadb4b Michael Niedermayer
        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
304 7bc9090a Michael Niedermayer
        CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(int))
305
        pic->mb_type= pic->mb_type_base + s->mb_stride+1;
306 0da71265 Michael Niedermayer
        if(s->out_format == FMT_H264){
307
            for(i=0; i<2; i++){
308
                CHECKED_ALLOCZ(pic->motion_val[i], 2 * 16 * s->mb_num * sizeof(uint16_t))
309
                CHECKED_ALLOCZ(pic->ref_index[i] , 4 * s->mb_num * sizeof(uint8_t))
310
            }
311
        }
312 7bc9090a Michael Niedermayer
        pic->qstride= s->mb_stride;
313 fa384dcc Michael Niedermayer
        CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
314 4e00e76b Michael Niedermayer
    }
315 0da71265 Michael Niedermayer
316 f943e138 Michael Niedermayer
    //it might be nicer if the application would keep track of these but it would require a API change
317
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
318
    s->prev_pict_types[0]= s->pict_type;
319
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
320
        pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
321
    
322 1e491e29 Michael Niedermayer
    return 0;
323
fail: //for the CHECKED_ALLOCZ macro
324
    return -1;
325
}
326
327 4e00e76b Michael Niedermayer
/**
328
 * deallocates a picture
329
 */
330 1e491e29 Michael Niedermayer
static void free_picture(MpegEncContext *s, Picture *pic){
331
    int i;
332 4e00e76b Michael Niedermayer
333
    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
334 492cd3a9 Michael Niedermayer
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
335 4e00e76b Michael Niedermayer
    }
336
337 1e491e29 Michael Niedermayer
    av_freep(&pic->mb_var);
338
    av_freep(&pic->mc_mb_var);
339
    av_freep(&pic->mb_mean);
340
    av_freep(&pic->mbskip_table);
341
    av_freep(&pic->qscale_table);
342 0da71265 Michael Niedermayer
    av_freep(&pic->mb_type_base);
343 fa384dcc Michael Niedermayer
    av_freep(&pic->pan_scan);
344 0da71265 Michael Niedermayer
    pic->mb_type= NULL;
345
    for(i=0; i<2; i++){
346
        av_freep(&pic->motion_val[i]);
347
        av_freep(&pic->ref_index[i]);
348
    }
349 d90cf87b Michael Niedermayer
    
350
    if(pic->type == FF_BUFFER_TYPE_SHARED){
351 4e00e76b Michael Niedermayer
        for(i=0; i<4; i++){
352
            pic->base[i]=
353
            pic->data[i]= NULL;
354
        }
355
        pic->type= 0;        
356 1e491e29 Michael Niedermayer
    }
357
}
358
359 defdfc9a Alex Beregszaszi
/* init common structure for both encoder and decoder */
360
int MPV_common_init(MpegEncContext *s)
361
{
362 7bc9090a Michael Niedermayer
    int y_size, c_size, yc_size, i, mb_array_size, x, y;
363 defdfc9a Alex Beregszaszi
364 b0368839 Michael Niedermayer
    dsputil_init(&s->dsp, s->avctx);
365 defdfc9a Alex Beregszaszi
    DCT_common_init(s);
366 eb4b3dd3 Zdenek Kabelac
367 9fee1e23 Michael Niedermayer
    s->flags= s->avctx->flags;
368 defdfc9a Alex Beregszaszi
369 1e491e29 Michael Niedermayer
    s->mb_width  = (s->width  + 15) / 16;
370 de6d9b64 Fabrice Bellard
    s->mb_height = (s->height + 15) / 16;
371 7bc9090a Michael Niedermayer
    s->mb_stride = s->mb_width + 1;
372
    mb_array_size= s->mb_height * s->mb_stride;
373 eb4b3dd3 Zdenek Kabelac
374 b5a093b3 Michael Niedermayer
    /* set default edge pos, will be overriden in decode_header if needed */
375
    s->h_edge_pos= s->mb_width*16;
376
    s->v_edge_pos= s->mb_height*16;
377 eb4b3dd3 Zdenek Kabelac
378
    s->mb_num = s->mb_width * s->mb_height;
379 7bc9090a Michael Niedermayer
    
380
    s->block_wrap[0]=
381
    s->block_wrap[1]=
382
    s->block_wrap[2]=
383
    s->block_wrap[3]= s->mb_width*2 + 2;
384
    s->block_wrap[4]=
385
    s->block_wrap[5]= s->mb_width + 2;
386 eb4b3dd3 Zdenek Kabelac
387
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
388
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
389
    yc_size = y_size + 2 * c_size;
390
391 202ef8b8 Michael Niedermayer
    /* convert fourcc to upper case */
392 7004ffb3 Michael Niedermayer
    s->avctx->codec_tag=   toupper( s->avctx->codec_tag     &0xFF)          
393
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
394
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 
395
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
396 b5a093b3 Michael Niedermayer
397 541ae140 Michael Niedermayer
    s->avctx->stream_codec_tag=   toupper( s->avctx->stream_codec_tag     &0xFF)          
398
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
399
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16) 
400
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
401
402 f7b47594 Michael Niedermayer
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
403
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
404 1e491e29 Michael Niedermayer
405 492cd3a9 Michael Niedermayer
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
406 1e491e29 Michael Niedermayer
407 7bc9090a Michael Niedermayer
    CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
408
    for(y=0; y<s->mb_height; y++){
409
        for(x=0; x<s->mb_width; x++){
410
            s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
411
        }
412
    }
413
    s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
414
    
415 37fbfd0a Juanjo
    if (s->encoding) {
416 7bc9090a Michael Niedermayer
        int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
417 9dbcbd92 Michael Niedermayer
418
        /* Allocate MV tables */
419 7bc9090a Michael Niedermayer
        CHECKED_ALLOCZ(s->p_mv_table_base            , mv_table_size * 2 * sizeof(int16_t))
420
        CHECKED_ALLOCZ(s->b_forw_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
421
        CHECKED_ALLOCZ(s->b_back_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
422
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
423
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
424
        CHECKED_ALLOCZ(s->b_direct_mv_table_base     , mv_table_size * 2 * sizeof(int16_t))
425
        s->p_mv_table           = s->p_mv_table_base            + s->mb_stride + 1;
426
        s->b_forw_mv_table      = s->b_forw_mv_table_base       + s->mb_stride + 1;
427
        s->b_back_mv_table      = s->b_back_mv_table_base       + s->mb_stride + 1;
428
        s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
429
        s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
430
        s->b_direct_mv_table    = s->b_direct_mv_table_base     + s->mb_stride + 1;
431 7f2fe444 Michael Niedermayer
432 1e491e29 Michael Niedermayer
        //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
433 1457ab52 Michael Niedermayer
        CHECKED_ALLOCZ(s->me.scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
434 7f2fe444 Michael Niedermayer
        
435 1457ab52 Michael Niedermayer
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
436
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
437 91029be7 Michael Niedermayer
438 7f2fe444 Michael Niedermayer
        if(s->codec_id==CODEC_ID_MPEG4){
439
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
440
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
441
        }
442 3aa102be Michael Niedermayer
        
443 6b460aa3 Michael Niedermayer
        if(s->msmpeg4_version){
444
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
445
        }
446 3aa102be Michael Niedermayer
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
447 7bc9090a Michael Niedermayer
448
        /* Allocate MB type table */
449
        CHECKED_ALLOCZ(s->mb_type  , mb_array_size * sizeof(uint8_t)) //needed for encoding
450 158c7f05 Michael Niedermayer
        
451
        CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
452 7e4995c3 Michael Niedermayer
        
453
        CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
454
        CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
455 642ccefb Michael Niedermayer
        CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
456
        CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
457 9d9e3172 Michael Niedermayer
        CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
458
        CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
459 821cb11f Michael Niedermayer
        
460
        if(s->avctx->noise_reduction){
461
            CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
462
            CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
463
        }
464 37fbfd0a Juanjo
    }
465 45686da2 Michael Niedermayer
    CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
466 4d2858de Michael Niedermayer
        
467 b465449e Michael Niedermayer
    CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
468
469 7bc9090a Michael Niedermayer
    CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
470 37fbfd0a Juanjo
    
471 bc1e3646 Michael Niedermayer
    if (s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_VIS_MV)) {
472 de6d9b64 Fabrice Bellard
        int size;
473 7f2fe444 Michael Niedermayer
474 de6d9b64 Fabrice Bellard
        /* MV prediction */
475
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
476 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
477 bea669e5 Michael Niedermayer
    }
478
479
    if(s->codec_id==CODEC_ID_MPEG4){
480 4d2858de Michael Niedermayer
        /* interlaced direct mode decoding tables */
481 7bc9090a Michael Niedermayer
        CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
482
        CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
483 de6d9b64 Fabrice Bellard
    }
484 6e2d5f1a Michael Niedermayer
    if (s->out_format == FMT_H263) {
485 de6d9b64 Fabrice Bellard
        /* ac values */
486 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
487 de6d9b64 Fabrice Bellard
        s->ac_val[1] = s->ac_val[0] + y_size;
488
        s->ac_val[2] = s->ac_val[1] + c_size;
489
        
490
        /* cbp values */
491 7f2fe444 Michael Niedermayer
        CHECKED_ALLOCZ(s->coded_block, y_size);
492 eec1c6b9 Michael Niedermayer
        
493
        /* divx501 bitstream reorder buffer */
494 7f2fe444 Michael Niedermayer
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
495 f01a1894 Michael Niedermayer
496 7f2fe444 Michael Niedermayer
        /* cbp, ac_pred, pred_dir */
497 7bc9090a Michael Niedermayer
        CHECKED_ALLOCZ(s->cbp_table  , mb_array_size * sizeof(uint8_t))
498
        CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
499 5b3438c6 Michael Niedermayer
    }
500 8b32880c Michael Niedermayer
    
501
    if (s->h263_pred || s->h263_plus || !s->encoding) {
502
        /* dc values */
503
        //MN: we need these for error resilience of intra-frames
504 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
505 8b32880c Michael Niedermayer
        s->dc_val[1] = s->dc_val[0] + y_size;
506
        s->dc_val[2] = s->dc_val[1] + c_size;
507
        for(i=0;i<yc_size;i++)
508
            s->dc_val[0][i] = 1024;
509
    }
510
511 7806197d Michael Niedermayer
    /* which mb is a intra block */
512 7bc9090a Michael Niedermayer
    CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
513
    memset(s->mbintra_table, 1, mb_array_size);
514 7806197d Michael Niedermayer
    
515 de6d9b64 Fabrice Bellard
    /* default structure is frame */
516
    s->picture_structure = PICT_FRAME;
517 93a21abd Michael Niedermayer
    
518 3bb4e23a Fabrice Bellard
    /* init macroblock skip table */
519 7bc9090a Michael Niedermayer
    CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
520 4d2858de Michael Niedermayer
    //Note the +1 is for a quicker mpeg4 slice_end detection
521 f943e138 Michael Niedermayer
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
522 ba6802de Michael Niedermayer
    
523 91029be7 Michael Niedermayer
    s->block= s->blocks[0];
524 3bb4e23a Fabrice Bellard
525 a579db0c Ivan Kalvachev
    for(i=0;i<12;i++){
526
        s->pblocks[i] = (short *)(&s->block[i]);
527
    }
528
529 d7425f59 Michael Niedermayer
    s->parse_context.state= -1;
530
531 de6d9b64 Fabrice Bellard
    s->context_initialized = 1;
532
    return 0;
533
 fail:
534 8257bf05 Zdenek Kabelac
    MPV_common_end(s);
535 de6d9b64 Fabrice Bellard
    return -1;
536
}
537
538 7f2fe444 Michael Niedermayer
539
//extern int sads;
540
541 de6d9b64 Fabrice Bellard
/* init common structure for both encoder and decoder */
542
void MPV_common_end(MpegEncContext *s)
543
{
544
    int i;
545
546 147e5200 Michael Niedermayer
    av_freep(&s->parse_context.buffer);
547
    s->parse_context.buffer_size=0;
548
549 6000abfa Fabrice Bellard
    av_freep(&s->mb_type);
550 7bc9090a Michael Niedermayer
    av_freep(&s->p_mv_table_base);
551
    av_freep(&s->b_forw_mv_table_base);
552
    av_freep(&s->b_back_mv_table_base);
553
    av_freep(&s->b_bidir_forw_mv_table_base);
554
    av_freep(&s->b_bidir_back_mv_table_base);
555
    av_freep(&s->b_direct_mv_table_base);
556
    s->p_mv_table= NULL;
557
    s->b_forw_mv_table= NULL;
558
    s->b_back_mv_table= NULL;
559
    s->b_bidir_forw_mv_table= NULL;
560
    s->b_bidir_back_mv_table= NULL;
561
    s->b_direct_mv_table= NULL;
562
    
563 6000abfa Fabrice Bellard
    av_freep(&s->motion_val);
564
    av_freep(&s->dc_val[0]);
565
    av_freep(&s->ac_val[0]);
566
    av_freep(&s->coded_block);
567
    av_freep(&s->mbintra_table);
568 7f2fe444 Michael Niedermayer
    av_freep(&s->cbp_table);
569
    av_freep(&s->pred_dir_table);
570 1457ab52 Michael Niedermayer
    av_freep(&s->me.scratchpad);
571
    av_freep(&s->me.map);
572
    av_freep(&s->me.score_map);
573 7f2fe444 Michael Niedermayer
    
574 6000abfa Fabrice Bellard
    av_freep(&s->mbskip_table);
575 f943e138 Michael Niedermayer
    av_freep(&s->prev_pict_types);
576 6000abfa Fabrice Bellard
    av_freep(&s->bitstream_buffer);
577 7f2fe444 Michael Niedermayer
    av_freep(&s->tex_pb_buffer);
578
    av_freep(&s->pb2_buffer);
579 f7b47594 Michael Niedermayer
    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
580 bea669e5 Michael Niedermayer
    av_freep(&s->field_mv_table);
581
    av_freep(&s->field_select_table);
582 3aa102be Michael Niedermayer
    av_freep(&s->avctx->stats_out);
583 6b460aa3 Michael Niedermayer
    av_freep(&s->ac_stats);
584 4d2858de Michael Niedermayer
    av_freep(&s->error_status_table);
585 7bc9090a Michael Niedermayer
    av_freep(&s->mb_index2xy);
586 158c7f05 Michael Niedermayer
    av_freep(&s->lambda_table);
587 7e4995c3 Michael Niedermayer
    av_freep(&s->q_intra_matrix);
588
    av_freep(&s->q_inter_matrix);
589 642ccefb Michael Niedermayer
    av_freep(&s->q_intra_matrix16);
590
    av_freep(&s->q_inter_matrix16);
591 45686da2 Michael Niedermayer
    av_freep(&s->blocks);
592 9d9e3172 Michael Niedermayer
    av_freep(&s->input_picture);
593
    av_freep(&s->reordered_input_picture);
594 821cb11f Michael Niedermayer
    av_freep(&s->dct_error_sum);
595
    av_freep(&s->dct_offset);
596 1e491e29 Michael Niedermayer
597 9b4b6e09 Michael Niedermayer
    if(s->picture){
598
        for(i=0; i<MAX_PICTURE_COUNT; i++){
599
            free_picture(s, &s->picture[i]);
600
        }
601 de6d9b64 Fabrice Bellard
    }
602 b465449e Michael Niedermayer
    av_freep(&s->picture);
603 d90cf87b Michael Niedermayer
    avcodec_default_free_buffers(s->avctx);
604 de6d9b64 Fabrice Bellard
    s->context_initialized = 0;
605 431f2172 Michael Niedermayer
    s->last_picture_ptr=
606
    s->next_picture_ptr=
607
    s->current_picture_ptr= NULL;
608 de6d9b64 Fabrice Bellard
}
609
610 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
611
612 de6d9b64 Fabrice Bellard
/* init video encoder */
613
int MPV_encode_init(AVCodecContext *avctx)
614
{
615
    MpegEncContext *s = avctx->priv_data;
616 22ddd60b Michael Niedermayer
    int i, dummy;
617 b1e6b355 Michael Niedermayer
    int chroma_h_shift, chroma_v_shift;
618 bc657ac3 Zdenek Kabelac
619 36b58e85 Roman Shaposhnik
    avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
620
621 de6d9b64 Fabrice Bellard
    s->bit_rate = avctx->bit_rate;
622 9cdd6a24 Michael Niedermayer
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
623 de6d9b64 Fabrice Bellard
    s->width = avctx->width;
624
    s->height = avctx->height;
625 7f2fe444 Michael Niedermayer
    if(avctx->gop_size > 600){
626 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
627 7f2fe444 Michael Niedermayer
        avctx->gop_size=600;
628
    }
629 de6d9b64 Fabrice Bellard
    s->gop_size = avctx->gop_size;
630 644d98a4 Juanjo
    s->rtp_mode = avctx->rtp_mode;
631
    s->rtp_payload_size = avctx->rtp_payload_size;
632 81401c1f Juanjo
    if (avctx->rtp_callback)
633
        s->rtp_callback = avctx->rtp_callback;
634 9cdd6a24 Michael Niedermayer
    s->max_qdiff= avctx->max_qdiff;
635
    s->qcompress= avctx->qcompress;
636
    s->qblur= avctx->qblur;
637 477c35a9 Arpi
    s->avctx = avctx;
638 ba6802de Michael Niedermayer
    s->flags= avctx->flags;
639 9dbcbd92 Michael Niedermayer
    s->max_b_frames= avctx->max_b_frames;
640 8b4c7dbc Michael Niedermayer
    s->b_frame_strategy= avctx->b_frame_strategy;
641 d7e9533a Michael Niedermayer
    s->codec_id= avctx->codec->id;
642 7f2fe444 Michael Niedermayer
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
643
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
644
    s->strict_std_compliance= avctx->strict_std_compliance;
645
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
646 1457ab52 Michael Niedermayer
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
647 87f8cab4 Michael Niedermayer
    s->mpeg_quant= avctx->mpeg_quant;
648 e4eadb4b Michael Niedermayer
649 de6d9b64 Fabrice Bellard
    if (s->gop_size <= 1) {
650
        s->intra_only = 1;
651
        s->gop_size = 12;
652
    } else {
653
        s->intra_only = 0;
654
    }
655 5e746b99 Michael Niedermayer
656 1457ab52 Michael Niedermayer
    s->me_method = avctx->me_method;
657 5e746b99 Michael Niedermayer
658 e4986da9 Juanjo
    /* Fixed QSCALE */
659 de6d9b64 Fabrice Bellard
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
660 37fbfd0a Juanjo
    
661 c5d309f2 Michael Niedermayer
    s->adaptive_quant= (   s->avctx->lumi_masking
662 5e746b99 Michael Niedermayer
                        || s->avctx->dark_masking
663 c5d309f2 Michael Niedermayer
                        || s->avctx->temporal_cplx_masking 
664
                        || s->avctx->spatial_cplx_masking
665 1f26c6f3 Michael Niedermayer
                        || s->avctx->p_masking
666
                        || (s->flags&CODEC_FLAG_QP_RD))
667 c5d309f2 Michael Niedermayer
                       && !s->fixed_qscale;
668 fcb48651 Michael Niedermayer
    
669
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
670 f7190f73 Michael Niedermayer
    
671
    s->obmc=    (s->codec_id == CODEC_ID_H263 || s->codec_id == CODEC_ID_H263P) 
672
             && (s->flags & CODEC_FLAG_4MV);
673 5e746b99 Michael Niedermayer
674 f7190f73 Michael Niedermayer
    if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 
675
       && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
676 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
677 9acbbd16 Michael Niedermayer
        return -1;
678
    }
679
    
680 f7190f73 Michael Niedermayer
    if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
681
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
682
        return -1;
683
    }
684
    
685 9acbbd16 Michael Niedermayer
    if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
686 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
687 9acbbd16 Michael Niedermayer
        return -1;
688
    }
689
690
    if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
691 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
692 9acbbd16 Michael Niedermayer
        return -1;
693
    }
694
    
695 029911d1 Michael Niedermayer
    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
696 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
697 9acbbd16 Michael Niedermayer
        return -1;
698
    }
699 e4eadb4b Michael Niedermayer
    
700 9acbbd16 Michael Niedermayer
    if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
701 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
702 9acbbd16 Michael Niedermayer
        return -1;
703
    }
704 e4eadb4b Michael Niedermayer
        
705 f2f6134b Michael Niedermayer
    if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
706 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
707 f2f6134b Michael Niedermayer
        return -1;
708
    }
709
710 dd3e415e Michael Niedermayer
    if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
711
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
712
        return -1;
713
    }
714
    
715 1984f635 Michael Niedermayer
    if(s->codec_id==CODEC_ID_MJPEG){
716
        s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
717
        s->inter_quant_bias= 0;
718 029911d1 Michael Niedermayer
    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
719 1984f635 Michael Niedermayer
        s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
720
        s->inter_quant_bias= 0;
721
    }else{
722
        s->intra_quant_bias=0;
723
        s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
724
    }
725
    
726
    if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
727
        s->intra_quant_bias= avctx->intra_quant_bias;
728
    if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
729
        s->inter_quant_bias= avctx->inter_quant_bias;
730 b1e6b355 Michael Niedermayer
        
731
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
732
733 22ddd60b Michael Niedermayer
    av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
734
    s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
735
736 de6d9b64 Fabrice Bellard
    switch(avctx->codec->id) {
737
    case CODEC_ID_MPEG1VIDEO:
738
        s->out_format = FMT_MPEG1;
739 14bea432 Michael Niedermayer
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
740
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
741 de6d9b64 Fabrice Bellard
        break;
742 029911d1 Michael Niedermayer
    case CODEC_ID_MPEG2VIDEO:
743
        s->out_format = FMT_MPEG1;
744
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
745
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
746
        s->rtp_mode= 1; // mpeg2 must have slices
747
        if(s->rtp_payload_size == 0) s->rtp_payload_size= 256*256*256;
748
        break;
749 b1e6b355 Michael Niedermayer
    case CODEC_ID_LJPEG:
750 de6d9b64 Fabrice Bellard
    case CODEC_ID_MJPEG:
751
        s->out_format = FMT_MJPEG;
752
        s->intra_only = 1; /* force intra only for jpeg */
753 37fbfd0a Juanjo
        s->mjpeg_write_tables = 1; /* write all tables */
754 a69b930c Alex Beregszaszi
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
755 b1e6b355 Michael Niedermayer
        s->mjpeg_vsample[0] = 1<<chroma_v_shift;
756
        s->mjpeg_vsample[1] = 1;
757 37fbfd0a Juanjo
        s->mjpeg_vsample[2] = 1; 
758 b1e6b355 Michael Niedermayer
        s->mjpeg_hsample[0] = 1<<chroma_h_shift;
759 37fbfd0a Juanjo
        s->mjpeg_hsample[1] = 1; 
760
        s->mjpeg_hsample[2] = 1; 
761 de6d9b64 Fabrice Bellard
        if (mjpeg_init(s) < 0)
762
            return -1;
763 1ff662cc Michael Niedermayer
        avctx->delay=0;
764 4e00e76b Michael Niedermayer
        s->low_delay=1;
765 de6d9b64 Fabrice Bellard
        break;
766 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
767 de6d9b64 Fabrice Bellard
    case CODEC_ID_H263:
768 37fbfd0a Juanjo
        if (h263_get_picture_format(s->width, s->height) == 7) {
769 9b879566 Michel Bardiaux
            av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
770 de6d9b64 Fabrice Bellard
            return -1;
771 37fbfd0a Juanjo
        }
772 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
773 1ff662cc Michael Niedermayer
        avctx->delay=0;
774 4e00e76b Michael Niedermayer
        s->low_delay=1;
775 de6d9b64 Fabrice Bellard
        break;
776
    case CODEC_ID_H263P:
777
        s->out_format = FMT_H263;
778
        s->h263_plus = 1;
779 21e59552 Michael Niedermayer
        /* Fx */
780
        s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
781
        s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
782
        /* /Fx */
783 544286b3 Juanjo
        /* These are just to be sure */
784 e51d6d27 Michael Niedermayer
        s->umvplus = 1;
785 1ff662cc Michael Niedermayer
        avctx->delay=0;
786 4e00e76b Michael Niedermayer
        s->low_delay=1;
787 de6d9b64 Fabrice Bellard
        break;
788 d4f5d74a Garrick Meeker
    case CODEC_ID_FLV1:
789
        s->out_format = FMT_H263;
790
        s->h263_flv = 2; /* format = 1; 11-bit codes */
791
        s->unrestricted_mv = 1;
792
        s->rtp_mode=0; /* don't allow GOB */
793
        avctx->delay=0;
794
        s->low_delay=1;
795
        break;
796 de6d9b64 Fabrice Bellard
    case CODEC_ID_RV10:
797
        s->out_format = FMT_H263;
798
        s->h263_rv10 = 1;
799 1ff662cc Michael Niedermayer
        avctx->delay=0;
800 4e00e76b Michael Niedermayer
        s->low_delay=1;
801 de6d9b64 Fabrice Bellard
        break;
802 58f26ba9 Fabrice Bellard
    case CODEC_ID_MPEG4:
803 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
804
        s->h263_pred = 1;
805
        s->unrestricted_mv = 1;
806 4e00e76b Michael Niedermayer
        s->low_delay= s->max_b_frames ? 0 : 1;
807 4d2858de Michael Niedermayer
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
808 de6d9b64 Fabrice Bellard
        break;
809 84afee34 Michael Niedermayer
    case CODEC_ID_MSMPEG4V1:
810 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
811
        s->h263_msmpeg4 = 1;
812
        s->h263_pred = 1;
813
        s->unrestricted_mv = 1;
814 84afee34 Michael Niedermayer
        s->msmpeg4_version= 1;
815 1ff662cc Michael Niedermayer
        avctx->delay=0;
816 4e00e76b Michael Niedermayer
        s->low_delay=1;
817 84afee34 Michael Niedermayer
        break;
818
    case CODEC_ID_MSMPEG4V2:
819
        s->out_format = FMT_H263;
820
        s->h263_msmpeg4 = 1;
821
        s->h263_pred = 1;
822
        s->unrestricted_mv = 1;
823
        s->msmpeg4_version= 2;
824 1ff662cc Michael Niedermayer
        avctx->delay=0;
825 4e00e76b Michael Niedermayer
        s->low_delay=1;
826 84afee34 Michael Niedermayer
        break;
827
    case CODEC_ID_MSMPEG4V3:
828
        s->out_format = FMT_H263;
829
        s->h263_msmpeg4 = 1;
830
        s->h263_pred = 1;
831
        s->unrestricted_mv = 1;
832
        s->msmpeg4_version= 3;
833 1f9aea9b Michael Niedermayer
        s->flipflop_rounding=1;
834 1ff662cc Michael Niedermayer
        avctx->delay=0;
835 4e00e76b Michael Niedermayer
        s->low_delay=1;
836 de6d9b64 Fabrice Bellard
        break;
837 f5957f3f Michael Niedermayer
    case CODEC_ID_WMV1:
838
        s->out_format = FMT_H263;
839
        s->h263_msmpeg4 = 1;
840
        s->h263_pred = 1;
841
        s->unrestricted_mv = 1;
842
        s->msmpeg4_version= 4;
843 1f9aea9b Michael Niedermayer
        s->flipflop_rounding=1;
844 f5957f3f Michael Niedermayer
        avctx->delay=0;
845 4e00e76b Michael Niedermayer
        s->low_delay=1;
846 f5957f3f Michael Niedermayer
        break;
847
    case CODEC_ID_WMV2:
848
        s->out_format = FMT_H263;
849
        s->h263_msmpeg4 = 1;
850
        s->h263_pred = 1;
851
        s->unrestricted_mv = 1;
852
        s->msmpeg4_version= 5;
853 1f9aea9b Michael Niedermayer
        s->flipflop_rounding=1;
854 f5957f3f Michael Niedermayer
        avctx->delay=0;
855 4e00e76b Michael Niedermayer
        s->low_delay=1;
856 f5957f3f Michael Niedermayer
        break;
857 1d0d55da Michael Niedermayer
#endif
858 de6d9b64 Fabrice Bellard
    default:
859
        return -1;
860
    }
861 cf8039b2 Michael Niedermayer
    
862 45870f57 Michael Niedermayer
    { /* set up some save defaults, some codecs might override them later */
863
        static int done=0;
864
        if(!done){
865
            int i;
866
            done=1;
867 bbed9259 Michael Niedermayer
868 30952237 Michael Niedermayer
            default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
869
            memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
870 0c1a9eda Zdenek Kabelac
            memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
871 45870f57 Michael Niedermayer
872
            for(i=-16; i<16; i++){
873
                default_fcode_tab[i + MAX_MV]= 1;
874
            }
875
        }
876
    }
877 1457ab52 Michael Niedermayer
    s->me.mv_penalty= default_mv_penalty;
878 45870f57 Michael Niedermayer
    s->fcode_tab= default_fcode_tab;
879 a406617f Michael Niedermayer
    s->y_dc_scale_table=
880
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
881
 
882 9d2a0355 Michael Niedermayer
    /* dont use mv_penalty table for crap MV as it would be confused */
883 1457ab52 Michael Niedermayer
    //FIXME remove after fixing / removing old ME
884
    if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
885 9d2a0355 Michael Niedermayer
886 3bb4e23a Fabrice Bellard
    s->encoding = 1;
887
888 de6d9b64 Fabrice Bellard
    /* init */
889
    if (MPV_common_init(s) < 0)
890
        return -1;
891
    
892 1457ab52 Michael Niedermayer
    ff_init_me(s);
893
894 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
895 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
896 2ad1516a Michael Niedermayer
    if (s->out_format == FMT_H263)
897
        h263_encode_init(s);
898
    if(s->msmpeg4_version)
899
        ff_msmpeg4_encode_init(s);
900 bbed9259 Michael Niedermayer
#endif
901 1d0d55da Michael Niedermayer
    if (s->out_format == FMT_MPEG1)
902
        ff_mpeg1_encode_init(s);
903
#endif
904 2ad1516a Michael Niedermayer
905 519c2b6d Fabrice Bellard
    /* init default q matrix */
906
    for(i=0;i<64;i++) {
907 b0368839 Michael Niedermayer
        int j= s->dsp.idct_permutation[i];
908 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
909 87f8cab4 Michael Niedermayer
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
910 2ad1516a Michael Niedermayer
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
911
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
912 87f8cab4 Michael Niedermayer
        }else if(s->out_format == FMT_H263){
913 2ad1516a Michael Niedermayer
            s->intra_matrix[j] =
914
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
915 1d0d55da Michael Niedermayer
        }else
916
#endif
917 029911d1 Michael Niedermayer
        { /* mpeg1/2 */
918 2ad1516a Michael Niedermayer
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
919
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
920 87f8cab4 Michael Niedermayer
        }
921 d6eb3c50 Michael Niedermayer
        if(s->avctx->intra_matrix)
922
            s->intra_matrix[j] = s->avctx->intra_matrix[i];
923
        if(s->avctx->inter_matrix)
924
            s->inter_matrix[j] = s->avctx->inter_matrix[i];
925 d7e9533a Michael Niedermayer
    }
926
927
    /* precompute matrix */
928 ef5b1b5a Juanjo
    /* for mjpeg, we do include qscale in the matrix */
929 d7e9533a Michael Niedermayer
    if (s->out_format != FMT_MJPEG) {
930 6b56c616 Michael Niedermayer
        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 
931 cc7ac888 Michael Niedermayer
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
932 6b56c616 Michael Niedermayer
        convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, 
933 cc7ac888 Michael Niedermayer
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
934 519c2b6d Fabrice Bellard
    }
935
936 8b4c7dbc Michael Niedermayer
    if(ff_rate_control_init(s) < 0)
937
        return -1;
938 de6d9b64 Fabrice Bellard
939
    s->picture_number = 0;
940 45870f57 Michael Niedermayer
    s->picture_in_gop_number = 0;
941 de6d9b64 Fabrice Bellard
    s->fake_picture_number = 0;
942
    /* motion detector init */
943
    s->f_code = 1;
944 9dbcbd92 Michael Niedermayer
    s->b_code = 1;
945 de6d9b64 Fabrice Bellard
946
    return 0;
947
}
948
949
int MPV_encode_end(AVCodecContext *avctx)
950
{
951
    MpegEncContext *s = avctx->priv_data;
952
953
#ifdef STATS
954
    print_stats();
955
#endif
956 8b4c7dbc Michael Niedermayer
957
    ff_rate_control_uninit(s);
958
959 de6d9b64 Fabrice Bellard
    MPV_common_end(s);
960
    if (s->out_format == FMT_MJPEG)
961
        mjpeg_close(s);
962 541ae140 Michael Niedermayer
963 22ddd60b Michael Niedermayer
    av_freep(&avctx->extradata);
964 37fbfd0a Juanjo
      
965 de6d9b64 Fabrice Bellard
    return 0;
966
}
967
968 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
969
970 1d0d55da Michael Niedermayer
void init_rl(RLTable *rl)
971
{
972 0c1a9eda Zdenek Kabelac
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
973
    uint8_t index_run[MAX_RUN+1];
974 1d0d55da Michael Niedermayer
    int last, run, level, start, end, i;
975
976
    /* compute max_level[], max_run[] and index_run[] */
977
    for(last=0;last<2;last++) {
978
        if (last == 0) {
979
            start = 0;
980
            end = rl->last;
981
        } else {
982
            start = rl->last;
983
            end = rl->n;
984
        }
985
986
        memset(max_level, 0, MAX_RUN + 1);
987
        memset(max_run, 0, MAX_LEVEL + 1);
988
        memset(index_run, rl->n, MAX_RUN + 1);
989
        for(i=start;i<end;i++) {
990
            run = rl->table_run[i];
991
            level = rl->table_level[i];
992
            if (index_run[run] == rl->n)
993
                index_run[run] = i;
994
            if (level > max_level[run])
995
                max_level[run] = level;
996
            if (run > max_run[level])
997
                max_run[level] = run;
998
        }
999
        rl->max_level[last] = av_malloc(MAX_RUN + 1);
1000
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1001
        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1002
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1003
        rl->index_run[last] = av_malloc(MAX_RUN + 1);
1004
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1005
    }
1006
}
1007
1008 de6d9b64 Fabrice Bellard
/* draw the edges of width 'w' of an image of size width, height */
1009 fd7db0fd Michael Niedermayer
//FIXME check that this is ok for mpeg4 interlaced
1010 0c1a9eda Zdenek Kabelac
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
1011 de6d9b64 Fabrice Bellard
{
1012 0c1a9eda Zdenek Kabelac
    uint8_t *ptr, *last_line;
1013 de6d9b64 Fabrice Bellard
    int i;
1014
1015
    last_line = buf + (height - 1) * wrap;
1016
    for(i=0;i<w;i++) {
1017
        /* top and bottom */
1018
        memcpy(buf - (i + 1) * wrap, buf, width);
1019
        memcpy(last_line + (i + 1) * wrap, last_line, width);
1020
    }
1021
    /* left and right */
1022
    ptr = buf;
1023
    for(i=0;i<height;i++) {
1024
        memset(ptr - w, ptr[0], w);
1025
        memset(ptr + width, ptr[width-1], w);
1026
        ptr += wrap;
1027
    }
1028
    /* corners */
1029
    for(i=0;i<w;i++) {
1030
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1031
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1032
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1033
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1034
    }
1035
}
1036
1037 5f194811 Michael Niedermayer
int ff_find_unused_picture(MpegEncContext *s, int shared){
1038 4e00e76b Michael Niedermayer
    int i;
1039
    
1040
    if(shared){
1041
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1042 5f194811 Michael Niedermayer
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
1043 4e00e76b Michael Niedermayer
        }
1044
    }else{
1045
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1046 5f194811 Michael Niedermayer
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
1047 4e00e76b Michael Niedermayer
        }
1048
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1049 5f194811 Michael Niedermayer
            if(s->picture[i].data[0]==NULL) return i;
1050 4e00e76b Michael Niedermayer
        }
1051
    }
1052
1053 5f194811 Michael Niedermayer
    assert(0);
1054
    return -1;
1055 4e00e76b Michael Niedermayer
}
1056
1057 821cb11f Michael Niedermayer
static void update_noise_reduction(MpegEncContext *s){
1058
    int intra, i;
1059
1060
    for(intra=0; intra<2; intra++){
1061
        if(s->dct_count[intra] > (1<<16)){
1062
            for(i=0; i<64; i++){
1063
                s->dct_error_sum[intra][i] >>=1;
1064
            }
1065
            s->dct_count[intra] >>= 1;
1066
        }
1067
        
1068
        for(i=0; i<64; i++){
1069
            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);
1070
        }
1071
    }
1072
}
1073
1074 5f194811 Michael Niedermayer
/**
1075
 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1076
 */
1077 d6db1c9c Michael Niedermayer
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1078 de6d9b64 Fabrice Bellard
{
1079 4e00e76b Michael Niedermayer
    int i;
1080 492cd3a9 Michael Niedermayer
    AVFrame *pic;
1081 425dddb7 Fabrice Bellard
    s->mb_skiped = 0;
1082 0da71265 Michael Niedermayer
1083 8b82a956 Michael Niedermayer
    assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
1084 e20c4069 Michael Niedermayer
1085 1e491e29 Michael Niedermayer
    /* mark&release old frames */
1086 59c673d5 Michael Niedermayer
    if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
1087 b536d0aa Michael Niedermayer
        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1088 1e491e29 Michael Niedermayer
1089
        /* release forgotten pictures */
1090
        /* if(mpeg124/h263) */
1091
        if(!s->encoding){
1092
            for(i=0; i<MAX_PICTURE_COUNT; i++){
1093 b536d0aa Michael Niedermayer
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1094 9b879566 Michel Bardiaux
                    av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
1095 492cd3a9 Michael Niedermayer
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
1096 1e491e29 Michael Niedermayer
                }
1097
            }
1098 d6db1c9c Michael Niedermayer
        }
1099 93a21abd Michael Niedermayer
    }
1100 aa388dba Michael Niedermayer
alloc:
1101
    if(!s->encoding){
1102 e20c4069 Michael Niedermayer
        /* release non refernce frames */
1103
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1104
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1105
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1106
            }
1107
        }
1108
1109 5f194811 Michael Niedermayer
        if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1110
            pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1111
        else{
1112
            i= ff_find_unused_picture(s, 0);
1113
            pic= (AVFrame*)&s->picture[i];
1114
        }
1115
1116 0da71265 Michael Niedermayer
        pic->reference= s->pict_type != B_TYPE ? 3 : 0;
1117 b536d0aa Michael Niedermayer
1118 5f194811 Michael Niedermayer
        if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
1119 b536d0aa Michael Niedermayer
            pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
1120 1e491e29 Michael Niedermayer
        
1121 f23a68df Ivan Kalvachev
        if( alloc_picture(s, (Picture*)pic, 0) < 0)
1122
            return -1;
1123 93a21abd Michael Niedermayer
1124 5f194811 Michael Niedermayer
        s->current_picture_ptr= (Picture*)pic;
1125 1e491e29 Michael Niedermayer
    }
1126 b7adc711 Michael Niedermayer
1127 9f2e61b6 Michael Niedermayer
    s->current_picture_ptr->pict_type= s->pict_type;
1128 158c7f05 Michael Niedermayer
//    if(s->flags && CODEC_FLAG_QSCALE) 
1129
  //      s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1130 7bc9090a Michael Niedermayer
    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1131 9f2e61b6 Michael Niedermayer
1132 6571e41d Michael Niedermayer
    copy_picture(&s->current_picture, s->current_picture_ptr);
1133 9f2e61b6 Michael Niedermayer
  
1134 8b82a956 Michael Niedermayer
  if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1135 1e491e29 Michael Niedermayer
    if (s->pict_type != B_TYPE) {
1136 b536d0aa Michael Niedermayer
        s->last_picture_ptr= s->next_picture_ptr;
1137
        s->next_picture_ptr= s->current_picture_ptr;
1138 de6d9b64 Fabrice Bellard
    }
1139 d90cf87b Michael Niedermayer
    
1140 6571e41d Michael Niedermayer
    if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1141
    if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1142 aa388dba Michael Niedermayer
    
1143 6571e41d Michael Niedermayer
    if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1144 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
1145 ffba1dc0 Michael Niedermayer
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1146
        goto alloc;
1147
    }
1148
1149
    assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1150
1151 b536d0aa Michael Niedermayer
    if(s->picture_structure!=PICT_FRAME){
1152
        int i;
1153
        for(i=0; i<4; i++){
1154
            if(s->picture_structure == PICT_BOTTOM_FIELD){
1155
                 s->current_picture.data[i] += s->current_picture.linesize[i];
1156
            } 
1157
            s->current_picture.linesize[i] *= 2;
1158
            s->last_picture.linesize[i] *=2;
1159
            s->next_picture.linesize[i] *=2;
1160
        }
1161
    }
1162 0da71265 Michael Niedermayer
  }
1163 1e491e29 Michael Niedermayer
   
1164 aa388dba Michael Niedermayer
    s->hurry_up= s->avctx->hurry_up;
1165
    s->error_resilience= avctx->error_resilience;
1166
1167 d930ef19 Michael Niedermayer
    /* set dequantizer, we cant do it during init as it might change for mpeg4
1168
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1169 029911d1 Michael Niedermayer
    if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) 
1170
        s->dct_unquantize = s->dct_unquantize_mpeg2;
1171
    else if(s->out_format == FMT_H263)
1172
        s->dct_unquantize = s->dct_unquantize_h263;
1173
    else 
1174 d930ef19 Michael Niedermayer
        s->dct_unquantize = s->dct_unquantize_mpeg1;
1175 d6db1c9c Michael Niedermayer
1176 821cb11f Michael Niedermayer
    if(s->dct_error_sum){
1177
        assert(s->avctx->noise_reduction && s->encoding);
1178
1179
        update_noise_reduction(s);
1180
    }
1181
        
1182 2e7b4c84 Ivan Kalvachev
#ifdef HAVE_XVMC
1183
    if(s->avctx->xvmc_acceleration)
1184
        return XVMC_field_start(s, avctx);
1185
#endif
1186 d6db1c9c Michael Niedermayer
    return 0;
1187 de6d9b64 Fabrice Bellard
}
1188 21af69f7 Fabrice Bellard
1189 de6d9b64 Fabrice Bellard
/* generic function for encode/decode called after a frame has been coded/decoded */
1190
void MPV_frame_end(MpegEncContext *s)
1191
{
1192 1e491e29 Michael Niedermayer
    int i;
1193 de6d9b64 Fabrice Bellard
    /* draw edge for correct motion prediction if outside */
1194 2e7b4c84 Ivan Kalvachev
#ifdef HAVE_XVMC
1195
//just to make sure that all data is rendered.
1196
    if(s->avctx->xvmc_acceleration){
1197
        XVMC_field_end(s);
1198
    }else
1199
#endif
1200 a573cc27 Michael Niedermayer
    if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1201 1e491e29 Michael Niedermayer
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
1202
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1203
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1204 de6d9b64 Fabrice Bellard
    }
1205 5975626d Michael Niedermayer
    emms_c();
1206 8b4c7dbc Michael Niedermayer
    
1207 3aa102be Michael Niedermayer
    s->last_pict_type    = s->pict_type;
1208 8b4c7dbc Michael Niedermayer
    if(s->pict_type!=B_TYPE){
1209
        s->last_non_b_pict_type= s->pict_type;
1210
    }
1211 b536d0aa Michael Niedermayer
#if 0
1212
        /* copy back current_picture variables */
1213 1e491e29 Michael Niedermayer
    for(i=0; i<MAX_PICTURE_COUNT; i++){
1214
        if(s->picture[i].data[0] == s->current_picture.data[0]){
1215
            s->picture[i]= s->current_picture;
1216
            break;
1217
        }    
1218
    }
1219
    assert(i<MAX_PICTURE_COUNT);
1220 b536d0aa Michael Niedermayer
#endif    
1221 1e491e29 Michael Niedermayer
1222 e20c4069 Michael Niedermayer
    if(s->encoding){
1223
        /* release non refernce frames */
1224
        for(i=0; i<MAX_PICTURE_COUNT; i++){
1225
            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1226
                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1227
            }
1228
        }
1229 1e491e29 Michael Niedermayer
    }
1230 b536d0aa Michael Niedermayer
    // clear copies, to avoid confusion
1231
#if 0
1232
    memset(&s->last_picture, 0, sizeof(Picture));
1233
    memset(&s->next_picture, 0, sizeof(Picture));
1234
    memset(&s->current_picture, 0, sizeof(Picture));
1235
#endif
1236 de6d9b64 Fabrice Bellard
}
1237
1238 7bc9090a Michael Niedermayer
/**
1239 db6e7795 Michael Niedermayer
 * draws an line from (ex, ey) -> (sx, sy).
1240
 * @param w width of the image
1241
 * @param h height of the image
1242
 * @param stride stride/linesize of the image
1243
 * @param color color of the arrow
1244
 */
1245
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1246
    int t, x, y, f;
1247
    
1248
    sx= clip(sx, 0, w-1);
1249
    sy= clip(sy, 0, h-1);
1250
    ex= clip(ex, 0, w-1);
1251
    ey= clip(ey, 0, h-1);
1252
    
1253
    buf[sy*stride + sx]+= color;
1254
    
1255
    if(ABS(ex - sx) > ABS(ey - sy)){
1256
        if(sx > ex){
1257
            t=sx; sx=ex; ex=t;
1258
            t=sy; sy=ey; ey=t;
1259
        }
1260
        buf+= sx + sy*stride;
1261
        ex-= sx;
1262
        f= ((ey-sy)<<16)/ex;
1263
        for(x= 0; x <= ex; x++){
1264
            y= ((x*f) + (1<<15))>>16;
1265
            buf[y*stride + x]+= color;
1266
        }
1267
    }else{
1268
        if(sy > ey){
1269
            t=sx; sx=ex; ex=t;
1270
            t=sy; sy=ey; ey=t;
1271
        }
1272
        buf+= sx + sy*stride;
1273
        ey-= sy;
1274
        if(ey) f= ((ex-sx)<<16)/ey;
1275
        else   f= 0;
1276
        for(y= 0; y <= ey; y++){
1277
            x= ((y*f) + (1<<15))>>16;
1278
            buf[y*stride + x]+= color;
1279
        }
1280
    }
1281
}
1282
1283
/**
1284
 * draws an arrow from (ex, ey) -> (sx, sy).
1285
 * @param w width of the image
1286
 * @param h height of the image
1287
 * @param stride stride/linesize of the image
1288
 * @param color color of the arrow
1289
 */
1290
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ 
1291
    int dx,dy;
1292
1293
    sx= clip(sx, -100, w+100);
1294
    sy= clip(sy, -100, h+100);
1295
    ex= clip(ex, -100, w+100);
1296
    ey= clip(ey, -100, h+100);
1297
    
1298
    dx= ex - sx;
1299
    dy= ey - sy;
1300
    
1301
    if(dx*dx + dy*dy > 3*3){
1302
        int rx=  dx + dy;
1303
        int ry= -dx + dy;
1304
        int length= ff_sqrt((rx*rx + ry*ry)<<8);
1305
        
1306
        //FIXME subpixel accuracy
1307
        rx= ROUNDED_DIV(rx*3<<4, length);
1308
        ry= ROUNDED_DIV(ry*3<<4, length);
1309
        
1310
        draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1311
        draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1312
    }
1313
    draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1314
}
1315
1316
/**
1317 7bc9090a Michael Niedermayer
 * prints debuging info for the given picture.
1318
 */
1319
void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1320
1321
    if(!pict || !pict->mb_type) return;
1322
1323
    if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1324
        int x,y;
1325
1326
        for(y=0; y<s->mb_height; y++){
1327
            for(x=0; x<s->mb_width; x++){
1328
                if(s->avctx->debug&FF_DEBUG_SKIP){
1329
                    int count= s->mbskip_table[x + y*s->mb_stride];
1330
                    if(count>9) count=9;
1331 9b879566 Michel Bardiaux
                    av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1332 7bc9090a Michael Niedermayer
                }
1333
                if(s->avctx->debug&FF_DEBUG_QP){
1334 9b879566 Michel Bardiaux
                    av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
1335 7bc9090a Michael Niedermayer
                }
1336
                if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1337
                    int mb_type= pict->mb_type[x + y*s->mb_stride];
1338
                    
1339
                    //Type & MV direction
1340
                    if(IS_PCM(mb_type))
1341 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "P");
1342 7bc9090a Michael Niedermayer
                    else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1343 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "A");
1344 7bc9090a Michael Niedermayer
                    else if(IS_INTRA4x4(mb_type))
1345 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "i");
1346 7bc9090a Michael Niedermayer
                    else if(IS_INTRA16x16(mb_type))
1347 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "I");
1348 7bc9090a Michael Niedermayer
                    else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1349 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "d");
1350 7bc9090a Michael Niedermayer
                    else if(IS_DIRECT(mb_type))
1351 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "D");
1352 7bc9090a Michael Niedermayer
                    else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1353 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "g");
1354 7bc9090a Michael Niedermayer
                    else if(IS_GMC(mb_type))
1355 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "G");
1356 7bc9090a Michael Niedermayer
                    else if(IS_SKIP(mb_type))
1357 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "S");
1358 7bc9090a Michael Niedermayer
                    else if(!USES_LIST(mb_type, 1))
1359 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, ">");
1360 7bc9090a Michael Niedermayer
                    else if(!USES_LIST(mb_type, 0))
1361 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "<");
1362 7bc9090a Michael Niedermayer
                    else{
1363
                        assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1364 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "X");
1365 7bc9090a Michael Niedermayer
                    }
1366
                    
1367
                    //segmentation
1368
                    if(IS_8X8(mb_type))
1369 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "+");
1370 7bc9090a Michael Niedermayer
                    else if(IS_16X8(mb_type))
1371 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "-");
1372 7bc9090a Michael Niedermayer
                    else if(IS_8X16(mb_type))
1373 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1374 7bc9090a Michael Niedermayer
                    else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1375 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1376 7bc9090a Michael Niedermayer
                    else
1377 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "?");
1378 7bc9090a Michael Niedermayer
                    
1379
                        
1380
                    if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1381 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, "=");
1382 7bc9090a Michael Niedermayer
                    else
1383 9b879566 Michel Bardiaux
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1384 7bc9090a Michael Niedermayer
                }
1385 9b879566 Michel Bardiaux
//                av_log(s->avctx, AV_LOG_DEBUG, " ");
1386 7bc9090a Michael Niedermayer
            }
1387 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
1388 7bc9090a Michael Niedermayer
        }
1389
    }
1390 db6e7795 Michael Niedermayer
    
1391
    if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
1392
        const int shift= 1 + s->quarter_sample;
1393
        int mb_y;
1394
        uint8_t *ptr= pict->data[0];
1395
        s->low_delay=0; //needed to see the vectors without trashing the buffers
1396
1397
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
1398
            int mb_x;
1399
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
1400
                const int mb_index= mb_x + mb_y*s->mb_stride;
1401
                if(IS_8X8(s->current_picture.mb_type[mb_index])){
1402
                    int i;
1403
                    for(i=0; i<4; i++){
1404
                        int sx= mb_x*16 + 4 + 8*(i&1);
1405
                        int sy= mb_y*16 + 4 + 8*(i>>1);
1406
                        int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1407
                        int mx= (s->motion_val[xy][0]>>shift) + sx;
1408
                        int my= (s->motion_val[xy][1]>>shift) + sy;
1409
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1410
                    }
1411 9bc8b386 Michael Niedermayer
                }else if(IS_16X8(s->current_picture.mb_type[mb_index])){
1412
                    int i;
1413
                    for(i=0; i<2; i++){
1414
                        int sx=mb_x*16 + 8;
1415
                        int sy=mb_y*16 + 4 + 8*i;
1416
                        int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
1417
                        int mx=(s->motion_val[xy][0]>>shift) + sx;
1418
                        int my=(s->motion_val[xy][1]>>shift) + sy;
1419
                        draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1420
                    }
1421 db6e7795 Michael Niedermayer
                }else{
1422
                    int sx= mb_x*16 + 8;
1423
                    int sy= mb_y*16 + 8;
1424
                    int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1425
                    int mx= (s->motion_val[xy][0]>>shift) + sx;
1426
                    int my= (s->motion_val[xy][1]>>shift) + sy;
1427
                    draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1428
                }
1429
                s->mbskip_table[mb_index]=0;
1430
            }
1431
        }
1432
    }
1433 7bc9090a Michael Niedermayer
}
1434
1435 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1436
1437 0d1e9246 Michael Niedermayer
static int get_sae(uint8_t *src, int ref, int stride){
1438
    int x,y;
1439
    int acc=0;
1440
    
1441
    for(y=0; y<16; y++){
1442
        for(x=0; x<16; x++){
1443
            acc+= ABS(src[x+y*stride] - ref);
1444
        }
1445
    }
1446
    
1447
    return acc;
1448
}
1449
1450
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1451
    int x, y, w, h;
1452
    int acc=0;
1453
    
1454
    w= s->width &~15;
1455
    h= s->height&~15;
1456
    
1457
    for(y=0; y<h; y+=16){
1458
        for(x=0; x<w; x+=16){
1459
            int offset= x + y*stride;
1460
            int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1461
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1462
            int sae = get_sae(src + offset, mean, stride);
1463
            
1464
            acc+= sae + 500 < sad;
1465
        }
1466
    }
1467
    return acc;
1468
}
1469
1470 4e00e76b Michael Niedermayer
1471 492cd3a9 Michael Niedermayer
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1472 17d71fb9 Michael Niedermayer
    AVFrame *pic=NULL;
1473 4e00e76b Michael Niedermayer
    int i;
1474 1e491e29 Michael Niedermayer
    const int encoding_delay= s->max_b_frames;
1475 4e00e76b Michael Niedermayer
    int direct=1;
1476 17d71fb9 Michael Niedermayer
    
1477
  if(pic_arg){
1478 4e00e76b Michael Niedermayer
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1479
    if(pic_arg->linesize[0] != s->linesize) direct=0;
1480
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1481
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1482
  
1483 9b879566 Michel Bardiaux
//    av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1484 1e491e29 Michael Niedermayer
    
1485 4e00e76b Michael Niedermayer
    if(direct){
1486 5f194811 Michael Niedermayer
        i= ff_find_unused_picture(s, 1);
1487 1e491e29 Michael Niedermayer
1488 492cd3a9 Michael Niedermayer
        pic= (AVFrame*)&s->picture[i];
1489 0da71265 Michael Niedermayer
        pic->reference= 3;
1490 1e491e29 Michael Niedermayer
    
1491 4e00e76b Michael Niedermayer
        for(i=0; i<4; i++){
1492
            pic->data[i]= pic_arg->data[i];
1493
            pic->linesize[i]= pic_arg->linesize[i];
1494
        }
1495
        alloc_picture(s, (Picture*)pic, 1);
1496
    }else{
1497 6571e41d Michael Niedermayer
        int offset= 16;
1498 5f194811 Michael Niedermayer
        i= ff_find_unused_picture(s, 0);
1499 1e491e29 Michael Niedermayer
1500 492cd3a9 Michael Niedermayer
        pic= (AVFrame*)&s->picture[i];
1501 0da71265 Michael Niedermayer
        pic->reference= 3;
1502 1e491e29 Michael Niedermayer
1503 4e00e76b Michael Niedermayer
        alloc_picture(s, (Picture*)pic, 0);
1504
1505 6571e41d Michael Niedermayer
        if(   pic->data[0] + offset == pic_arg->data[0] 
1506
           && pic->data[1] + offset == pic_arg->data[1]
1507
           && pic->data[2] + offset == pic_arg->data[2]){
1508 1e491e29 Michael Niedermayer
       // empty
1509 4e00e76b Michael Niedermayer
        }else{
1510
            int h_chroma_shift, v_chroma_shift;
1511
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1512 1e491e29 Michael Niedermayer
        
1513 4e00e76b Michael Niedermayer
            for(i=0; i<3; i++){
1514
                int src_stride= pic_arg->linesize[i];
1515
                int dst_stride= i ? s->uvlinesize : s->linesize;
1516
                int h_shift= i ? h_chroma_shift : 0;
1517
                int v_shift= i ? v_chroma_shift : 0;
1518
                int w= s->width >>h_shift;
1519
                int h= s->height>>v_shift;
1520
                uint8_t *src= pic_arg->data[i];
1521 6571e41d Michael Niedermayer
                uint8_t *dst= pic->data[i] + offset;
1522 1e491e29 Michael Niedermayer
            
1523 4e00e76b Michael Niedermayer
                if(src_stride==dst_stride)
1524
                    memcpy(dst, src, src_stride*h);
1525
                else{
1526
                    while(h--){
1527
                        memcpy(dst, src, w);
1528
                        dst += dst_stride;
1529
                        src += src_stride;
1530
                    }
1531 1e491e29 Michael Niedermayer
                }
1532 9dbcbd92 Michael Niedermayer
            }
1533 1e491e29 Michael Niedermayer
        }
1534
    }
1535 4e00e76b Michael Niedermayer
    pic->quality= pic_arg->quality;
1536
    pic->pict_type= pic_arg->pict_type;
1537 369fe599 Thomas Jarosch
    pic->pts = pic_arg->pts;
1538 4e00e76b Michael Niedermayer
    
1539
    if(s->input_picture[encoding_delay])
1540
        pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1541 17d71fb9 Michael Niedermayer
    
1542
  }
1543 4e00e76b Michael Niedermayer
1544
    /* shift buffer entries */
1545
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1546
        s->input_picture[i-1]= s->input_picture[i];
1547
        
1548
    s->input_picture[encoding_delay]= (Picture*)pic;
1549 1e491e29 Michael Niedermayer
1550
    return 0;
1551
}
1552 9dbcbd92 Michael Niedermayer
1553 1e491e29 Michael Niedermayer
static void select_input_picture(MpegEncContext *s){
1554
    int i;
1555
    int coded_pic_num=0;    
1556
1557
    if(s->reordered_input_picture[0])
1558
        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1559 4e00e76b Michael Niedermayer
1560 1e491e29 Michael Niedermayer
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1561
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1562
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1563
1564
    /* set next picture types & ordering */
1565
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1566 b536d0aa Michael Niedermayer
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1567 0d1e9246 Michael Niedermayer
            s->reordered_input_picture[0]= s->input_picture[0];
1568
            s->reordered_input_picture[0]->pict_type= I_TYPE;
1569
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1570
        }else{
1571
            int b_frames;
1572
            
1573
            if(s->flags&CODEC_FLAG_PASS2){
1574
                for(i=0; i<s->max_b_frames+1; i++){
1575
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1576
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1577
                    s->input_picture[i]->pict_type= pict_type;
1578
                    
1579
                    if(i + 1 >= s->rc_context.num_entries) break;
1580
                }
1581
            }
1582 4e00e76b Michael Niedermayer
1583 0d1e9246 Michael Niedermayer
            if(s->input_picture[0]->pict_type){
1584
                /* user selected pict_type */
1585 140cb663 Michael Niedermayer
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1586
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1587
                }
1588 0d1e9246 Michael Niedermayer
            
1589 140cb663 Michael Niedermayer
                if(b_frames > s->max_b_frames){
1590 9b879566 Michel Bardiaux
                    av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
1591 140cb663 Michael Niedermayer
                    b_frames = s->max_b_frames;
1592
                }
1593 0d1e9246 Michael Niedermayer
            }else if(s->b_frame_strategy==0){
1594
                b_frames= s->max_b_frames;
1595 17d71fb9 Michael Niedermayer
                while(b_frames && !s->input_picture[b_frames]) b_frames--;
1596 0d1e9246 Michael Niedermayer
            }else if(s->b_frame_strategy==1){
1597
                for(i=1; i<s->max_b_frames+1; i++){
1598 17d71fb9 Michael Niedermayer
                    if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1599 0d1e9246 Michael Niedermayer
                        s->input_picture[i]->b_frame_score= 
1600 4e00e76b Michael Niedermayer
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
1601
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1602 0d1e9246 Michael Niedermayer
                    }
1603
                }
1604
                for(i=0; i<s->max_b_frames; i++){
1605 17d71fb9 Michael Niedermayer
                    if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1606 0d1e9246 Michael Niedermayer
                }
1607
                                
1608
                b_frames= FFMAX(0, i-1);
1609 140cb663 Michael Niedermayer
                
1610 0d1e9246 Michael Niedermayer
                /* reset scores */
1611
                for(i=0; i<b_frames+1; i++){
1612
                    s->input_picture[i]->b_frame_score=0;
1613
                }
1614
            }else{
1615 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1616 0d1e9246 Michael Niedermayer
                b_frames=0;
1617 140cb663 Michael Niedermayer
            }
1618 0d1e9246 Michael Niedermayer
1619
            emms_c();
1620
//static int b_count=0;
1621
//b_count+= b_frames;
1622 9b879566 Michel Bardiaux
//av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1623 0d1e9246 Michael Niedermayer
                        
1624
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1625
            if(   s->picture_in_gop_number + b_frames >= s->gop_size 
1626
               || s->reordered_input_picture[0]->pict_type== I_TYPE)
1627 1e491e29 Michael Niedermayer
                s->reordered_input_picture[0]->pict_type= I_TYPE;
1628 0d1e9246 Michael Niedermayer
            else
1629
                s->reordered_input_picture[0]->pict_type= P_TYPE;
1630
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1631
            for(i=0; i<b_frames; i++){
1632
                coded_pic_num++;
1633
                s->reordered_input_picture[i+1]= s->input_picture[i];
1634
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1635
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1636 9dbcbd92 Michael Niedermayer
            }
1637
        }
1638
    }
1639 1e491e29 Michael Niedermayer
    
1640
    if(s->reordered_input_picture[0]){
1641 0da71265 Michael Niedermayer
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1642 b536d0aa Michael Niedermayer
1643 6571e41d Michael Niedermayer
        copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1644 4e00e76b Michael Niedermayer
1645
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1646 b536d0aa Michael Niedermayer
            // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1647
        
1648 5f194811 Michael Niedermayer
            int i= ff_find_unused_picture(s, 0);
1649 4e00e76b Michael Niedermayer
            Picture *pic= &s->picture[i];
1650
1651
            /* mark us unused / free shared pic */
1652
            for(i=0; i<4; i++)
1653
                s->reordered_input_picture[0]->data[i]= NULL;
1654
            s->reordered_input_picture[0]->type= 0;
1655
            
1656 b536d0aa Michael Niedermayer
            //FIXME bad, copy * except
1657 4e00e76b Michael Niedermayer
            pic->pict_type = s->reordered_input_picture[0]->pict_type;
1658
            pic->quality   = s->reordered_input_picture[0]->quality;
1659
            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1660
            pic->reference = s->reordered_input_picture[0]->reference;
1661 36b58e85 Roman Shaposhnik
            pic->pts = s->reordered_input_picture[0]->pts;
1662 4e00e76b Michael Niedermayer
            
1663
            alloc_picture(s, pic, 0);
1664
1665 b536d0aa Michael Niedermayer
            s->current_picture_ptr= pic;
1666 4e00e76b Michael Niedermayer
        }else{
1667 b536d0aa Michael Niedermayer
            // input is not a shared pix -> reuse buffer for current_pix
1668
1669 4e00e76b Michael Niedermayer
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
1670
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1671
            
1672 b536d0aa Michael Niedermayer
            s->current_picture_ptr= s->reordered_input_picture[0];
1673 4e00e76b Michael Niedermayer
            for(i=0; i<4; i++){
1674 6571e41d Michael Niedermayer
                s->new_picture.data[i]+=16;
1675 4e00e76b Michael Niedermayer
            }
1676 1e491e29 Michael Niedermayer
        }
1677 6571e41d Michael Niedermayer
        copy_picture(&s->current_picture, s->current_picture_ptr);
1678 1e491e29 Michael Niedermayer
    
1679
        s->picture_number= s->new_picture.display_picture_number;
1680
//printf("dpn:%d\n", s->picture_number);
1681
    }else{
1682
       memset(&s->new_picture, 0, sizeof(Picture));
1683 9dbcbd92 Michael Niedermayer
    }
1684
}
1685
1686 de6d9b64 Fabrice Bellard
int MPV_encode_picture(AVCodecContext *avctx,
1687
                       unsigned char *buf, int buf_size, void *data)
1688
{
1689
    MpegEncContext *s = avctx->priv_data;
1690 492cd3a9 Michael Niedermayer
    AVFrame *pic_arg = data;
1691 140cb663 Michael Niedermayer
    int i;
1692 de6d9b64 Fabrice Bellard
1693 6e230912 Michael Niedermayer
    if(avctx->pix_fmt != PIX_FMT_YUV420P){
1694 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
1695 6e230912 Michael Niedermayer
        return -1;
1696
    }
1697
    
1698 ed7debda Alex Beregszaszi
    init_put_bits(&s->pb, buf, buf_size);
1699 de6d9b64 Fabrice Bellard
1700 1e491e29 Michael Niedermayer
    s->picture_in_gop_number++;
1701 de6d9b64 Fabrice Bellard
1702 1e491e29 Michael Niedermayer
    load_input_picture(s, pic_arg);
1703 8b4c7dbc Michael Niedermayer
    
1704 1e491e29 Michael Niedermayer
    select_input_picture(s);
1705 9dbcbd92 Michael Niedermayer
    
1706
    /* output? */
1707 1e491e29 Michael Niedermayer
    if(s->new_picture.data[0]){
1708 9dbcbd92 Michael Niedermayer
1709 1e491e29 Michael Niedermayer
        s->pict_type= s->new_picture.pict_type;
1710
//emms_c();
1711
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1712 93a21abd Michael Niedermayer
        MPV_frame_start(s, avctx);
1713 9dbcbd92 Michael Niedermayer
1714
        encode_picture(s, s->picture_number);
1715 208d3ddf Michael Niedermayer
        
1716 9cd3766f Juanjo
        avctx->real_pict_num  = s->picture_number;
1717 9dbcbd92 Michael Niedermayer
        avctx->header_bits = s->header_bits;
1718
        avctx->mv_bits     = s->mv_bits;
1719
        avctx->misc_bits   = s->misc_bits;
1720
        avctx->i_tex_bits  = s->i_tex_bits;
1721
        avctx->p_tex_bits  = s->p_tex_bits;
1722
        avctx->i_count     = s->i_count;
1723 66370d3f Michael Niedermayer
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1724 9dbcbd92 Michael Niedermayer
        avctx->skip_count  = s->skip_count;
1725
1726
        MPV_frame_end(s);
1727
1728
        if (s->out_format == FMT_MJPEG)
1729
            mjpeg_picture_trailer(s);
1730 8b4c7dbc Michael Niedermayer
        
1731
        if(s->flags&CODEC_FLAG_PASS1)
1732
            ff_write_pass1_stats(s);
1733 b536d0aa Michael Niedermayer
1734
        for(i=0; i<4; i++){
1735
            avctx->error[i] += s->current_picture_ptr->error[i];
1736
        }
1737 de6d9b64 Fabrice Bellard
    }
1738
1739 9dbcbd92 Michael Niedermayer
    s->input_picture_number++;
1740 de6d9b64 Fabrice Bellard
1741
    flush_put_bits(&s->pb);
1742 9cdd6a24 Michael Niedermayer
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1743 3aa102be Michael Niedermayer
    
1744 9cdd6a24 Michael Niedermayer
    s->total_bits += s->frame_bits;
1745 098eefe1 Michael Niedermayer
    avctx->frame_bits  = s->frame_bits;
1746 140cb663 Michael Niedermayer
    
1747 17592475 Michael Niedermayer
    return pbBufPtr(&s->pb) - s->pb.buf;
1748 de6d9b64 Fabrice Bellard
}
1749
1750 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1751
1752 44eb4951 Michael Niedermayer
static inline void gmc1_motion(MpegEncContext *s,
1753 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1754 44eb4951 Michael Niedermayer
                               int dest_offset,
1755 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset)
1756 44eb4951 Michael Niedermayer
{
1757 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1758 0fd90455 Michael Niedermayer
    int offset, src_x, src_y, linesize, uvlinesize;
1759 44eb4951 Michael Niedermayer
    int motion_x, motion_y;
1760 54993774 Michael Niedermayer
    int emu=0;
1761 44eb4951 Michael Niedermayer
1762
    motion_x= s->sprite_offset[0][0];
1763
    motion_y= s->sprite_offset[0][1];
1764
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1765
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1766
    motion_x<<=(3-s->sprite_warping_accuracy);
1767
    motion_y<<=(3-s->sprite_warping_accuracy);
1768
    src_x = clip(src_x, -16, s->width);
1769
    if (src_x == s->width)
1770
        motion_x =0;
1771
    src_y = clip(src_y, -16, s->height);
1772
    if (src_y == s->height)
1773
        motion_y =0;
1774 073b013d Michael Niedermayer
1775 44eb4951 Michael Niedermayer
    linesize = s->linesize;
1776 0fd90455 Michael Niedermayer
    uvlinesize = s->uvlinesize;
1777 073b013d Michael Niedermayer
    
1778 44eb4951 Michael Niedermayer
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1779
1780
    dest_y+=dest_offset;
1781 54993774 Michael Niedermayer
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1782 a573cc27 Michael Niedermayer
        if(   (unsigned)src_x >= s->h_edge_pos - 17
1783
           || (unsigned)src_y >= s->v_edge_pos - 17){
1784 c009df3f Michael Niedermayer
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1785 54993774 Michael Niedermayer
            ptr= s->edge_emu_buffer;
1786
        }
1787
    }
1788 073b013d Michael Niedermayer
    
1789
    if((motion_x|motion_y)&7){
1790 eb4b3dd3 Zdenek Kabelac
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1791
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1792 073b013d Michael Niedermayer
    }else{
1793
        int dxy;
1794
        
1795
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1796
        if (s->no_rounding){
1797 eb4b3dd3 Zdenek Kabelac
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1798 073b013d Michael Niedermayer
        }else{
1799 eb4b3dd3 Zdenek Kabelac
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1800
        }
1801 073b013d Michael Niedermayer
    }
1802
    
1803
    if(s->flags&CODEC_FLAG_GRAY) return;
1804 44eb4951 Michael Niedermayer
1805
    motion_x= s->sprite_offset[1][0];
1806
    motion_y= s->sprite_offset[1][1];
1807
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1808
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1809
    motion_x<<=(3-s->sprite_warping_accuracy);
1810
    motion_y<<=(3-s->sprite_warping_accuracy);
1811
    src_x = clip(src_x, -8, s->width>>1);
1812
    if (src_x == s->width>>1)
1813
        motion_x =0;
1814
    src_y = clip(src_y, -8, s->height>>1);
1815
    if (src_y == s->height>>1)
1816
        motion_y =0;
1817
1818 0fd90455 Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1819 44eb4951 Michael Niedermayer
    ptr = ref_picture[1] + offset;
1820 741235eb Michael Niedermayer
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1821 a573cc27 Michael Niedermayer
        if(   (unsigned)src_x >= (s->h_edge_pos>>1) - 9
1822
           || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
1823 c009df3f Michael Niedermayer
            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);
1824 741235eb Michael Niedermayer
            ptr= s->edge_emu_buffer;
1825
            emu=1;
1826
        }
1827 54993774 Michael Niedermayer
    }
1828 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1829 54993774 Michael Niedermayer
    
1830 44eb4951 Michael Niedermayer
    ptr = ref_picture[2] + offset;
1831 54993774 Michael Niedermayer
    if(emu){
1832 c009df3f Michael Niedermayer
        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);
1833 54993774 Michael Niedermayer
        ptr= s->edge_emu_buffer;
1834
    }
1835 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1836 44eb4951 Michael Niedermayer
    
1837
    return;
1838
}
1839
1840 073b013d Michael Niedermayer
static inline void gmc_motion(MpegEncContext *s,
1841 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1842 073b013d Michael Niedermayer
                               int dest_offset,
1843 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset)
1844 073b013d Michael Niedermayer
{
1845 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1846 073b013d Michael Niedermayer
    int linesize, uvlinesize;
1847
    const int a= s->sprite_warping_accuracy;
1848
    int ox, oy;
1849
1850
    linesize = s->linesize;
1851
    uvlinesize = s->uvlinesize;
1852
1853
    ptr = ref_picture[0] + src_offset;
1854
1855
    dest_y+=dest_offset;
1856
    
1857
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1858
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1859
1860 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1861 073b013d Michael Niedermayer
           ox, 
1862
           oy, 
1863
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1864
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1865
           a+1, (1<<(2*a+1)) - s->no_rounding,
1866
           s->h_edge_pos, s->v_edge_pos);
1867 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1868 073b013d Michael Niedermayer
           ox + s->sprite_delta[0][0]*8, 
1869
           oy + s->sprite_delta[1][0]*8, 
1870
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1871
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1872
           a+1, (1<<(2*a+1)) - s->no_rounding,
1873
           s->h_edge_pos, s->v_edge_pos);
1874
1875
    if(s->flags&CODEC_FLAG_GRAY) return;
1876
1877
1878
    dest_cb+=dest_offset>>1;
1879
    dest_cr+=dest_offset>>1;
1880
    
1881
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1882
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1883
1884
    ptr = ref_picture[1] + (src_offset>>1);
1885 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1886 073b013d Michael Niedermayer
           ox, 
1887
           oy, 
1888
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1889
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1890
           a+1, (1<<(2*a+1)) - s->no_rounding,
1891
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1892
    
1893
    ptr = ref_picture[2] + (src_offset>>1);
1894 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1895 073b013d Michael Niedermayer
           ox, 
1896
           oy, 
1897
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1898
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1899
           a+1, (1<<(2*a+1)) - s->no_rounding,
1900
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1901
}
1902
1903 c009df3f Michael Niedermayer
/**
1904
 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
1905
 * @param buf destination buffer
1906
 * @param src source buffer
1907
 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
1908
 * @param block_w width of block
1909
 * @param block_h height of block
1910
 * @param src_x x coordinate of the top left sample of the block in the source buffer
1911
 * @param src_y y coordinate of the top left sample of the block in the source buffer
1912
 * @param w width of the source buffer
1913
 * @param h height of the source buffer
1914
 */
1915
void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 
1916 93a21abd Michael Niedermayer
                                    int src_x, int src_y, int w, int h){
1917
    int x, y;
1918
    int start_y, start_x, end_y, end_x;
1919 eb4b3dd3 Zdenek Kabelac
1920 93a21abd Michael Niedermayer
    if(src_y>= h){
1921
        src+= (h-1-src_y)*linesize;
1922
        src_y=h-1;
1923 225f9c44 Michael Niedermayer
    }else if(src_y<=-block_h){
1924
        src+= (1-block_h-src_y)*linesize;
1925
        src_y=1-block_h;
1926 93a21abd Michael Niedermayer
    }
1927
    if(src_x>= w){
1928
        src+= (w-1-src_x);
1929
        src_x=w-1;
1930 225f9c44 Michael Niedermayer
    }else if(src_x<=-block_w){
1931
        src+= (1-block_w-src_x);
1932
        src_x=1-block_w;
1933 93a21abd Michael Niedermayer
    }
1934
1935 b8a78f41 Michael Niedermayer
    start_y= FFMAX(0, -src_y);
1936
    start_x= FFMAX(0, -src_x);
1937
    end_y= FFMIN(block_h, h-src_y);
1938
    end_x= FFMIN(block_w, w-src_x);
1939 54993774 Michael Niedermayer
1940 93a21abd Michael Niedermayer
    // copy existing part
1941
    for(y=start_y; y<end_y; y++){
1942
        for(x=start_x; x<end_x; x++){
1943
            buf[x + y*linesize]= src[x + y*linesize];
1944
        }
1945
    }
1946
1947
    //top
1948
    for(y=0; y<start_y; y++){
1949
        for(x=start_x; x<end_x; x++){
1950
            buf[x + y*linesize]= buf[x + start_y*linesize];
1951
        }
1952
    }
1953
1954
    //bottom
1955
    for(y=end_y; y<block_h; y++){
1956
        for(x=start_x; x<end_x; x++){
1957
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1958
        }
1959
    }
1960
                                    
1961
    for(y=0; y<block_h; y++){
1962
       //left
1963
        for(x=0; x<start_x; x++){
1964
            buf[x + y*linesize]= buf[start_x + y*linesize];
1965
        }
1966
       
1967
       //right
1968
        for(x=end_x; x<block_w; x++){
1969
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1970
        }
1971
    }
1972
}
1973
1974 f7190f73 Michael Niedermayer
static inline int hpel_motion(MpegEncContext *s, 
1975
                                  uint8_t *dest, uint8_t *src, 
1976
                                  int src_x, int src_y,
1977
                                  int width, int height, int stride,
1978
                                  int h_edge_pos, int v_edge_pos,
1979
                                  int w, int h, op_pixels_func *pix_op,
1980
                                  int motion_x, int motion_y)
1981
{
1982
    int dxy;
1983
    int emu=0;
1984
1985
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1986
    src_x += motion_x >> 1;
1987
    src_y += motion_y >> 1;
1988
                
1989
    /* WARNING: do no forget half pels */
1990
    src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
1991
    if (src_x == width)
1992
        dxy &= ~1;
1993
    src_y = clip(src_y, -16, height);
1994
    if (src_y == height)
1995
        dxy &= ~2;
1996
    src += src_y * stride + src_x;
1997
1998
    if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
1999
        if(   (unsigned)src_x > h_edge_pos - (motion_x&1) - w
2000
           || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
2001
            ff_emulated_edge_mc(s->edge_emu_buffer, src, stride, w+1, h+1,
2002
                             src_x, src_y, h_edge_pos, v_edge_pos);
2003
            src= s->edge_emu_buffer;
2004
            emu=1;
2005
        }
2006
    }
2007
    pix_op[dxy](dest, src, stride, h);
2008
    return emu;
2009
}
2010 93a21abd Michael Niedermayer
2011 de6d9b64 Fabrice Bellard
/* apply one mpeg motion vector to the three components */
2012
static inline void mpeg_motion(MpegEncContext *s,
2013 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2014 de6d9b64 Fabrice Bellard
                               int dest_offset,
2015 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset,
2016 b3184779 Michael Niedermayer
                               int field_based, op_pixels_func (*pix_op)[4],
2017 de6d9b64 Fabrice Bellard
                               int motion_x, int motion_y, int h)
2018
{
2019 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
2020 f7190f73 Michael Niedermayer
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, uvlinesize;
2021 93a21abd Michael Niedermayer
    int emu=0;
2022 b3184779 Michael Niedermayer
#if 0    
2023 44eb4951 Michael Niedermayer
if(s->quarter_sample)
2024
{
2025
    motion_x>>=1;
2026
    motion_y>>=1;
2027
}
2028 b3184779 Michael Niedermayer
#endif
2029 f7190f73 Michael Niedermayer
2030 de6d9b64 Fabrice Bellard
    height = s->height >> field_based;
2031 b5a093b3 Michael Niedermayer
    v_edge_pos = s->v_edge_pos >> field_based;
2032 b536d0aa Michael Niedermayer
    uvlinesize = s->current_picture.linesize[1] << field_based;
2033 93a21abd Michael Niedermayer
2034 f7190f73 Michael Niedermayer
    emu= hpel_motion(s, 
2035
                dest_y + dest_offset, ref_picture[0] + src_offset,
2036
                s->mb_x * 16, s->mb_y * (16 >> field_based),
2037
                s->width, height, s->current_picture.linesize[0] << field_based,
2038
                s->h_edge_pos, v_edge_pos,
2039
                16, h, pix_op[0],
2040
                motion_x, motion_y);
2041
2042 de6d9b64 Fabrice Bellard
2043 b50eef3a Michael Niedermayer
    if(s->flags&CODEC_FLAG_GRAY) return;
2044
2045 de6d9b64 Fabrice Bellard
    if (s->out_format == FMT_H263) {
2046
        dxy = 0;
2047
        if ((motion_x & 3) != 0)
2048
            dxy |= 1;
2049
        if ((motion_y & 3) != 0)
2050
            dxy |= 2;
2051
        mx = motion_x >> 2;
2052
        my = motion_y >> 2;
2053
    } else {
2054
        mx = motion_x / 2;
2055
        my = motion_y / 2;
2056
        dxy = ((my & 1) << 1) | (mx & 1);
2057
        mx >>= 1;
2058
        my >>= 1;
2059
    }
2060
    
2061
    src_x = s->mb_x * 8 + mx;
2062
    src_y = s->mb_y * (8 >> field_based) + my;
2063
    src_x = clip(src_x, -8, s->width >> 1);
2064
    if (src_x == (s->width >> 1))
2065
        dxy &= ~1;
2066
    src_y = clip(src_y, -8, height >> 1);
2067
    if (src_y == (height >> 1))
2068
        dxy &= ~2;
2069 4c9ede2e Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2070 de6d9b64 Fabrice Bellard
    ptr = ref_picture[1] + offset;
2071 93a21abd Michael Niedermayer
    if(emu){
2072 c009df3f Michael Niedermayer
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
2073 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2074
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2075 93a21abd Michael Niedermayer
    }
2076 b3184779 Michael Niedermayer
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2077 93a21abd Michael Niedermayer
2078 de6d9b64 Fabrice Bellard
    ptr = ref_picture[2] + offset;
2079 93a21abd Michael Niedermayer
    if(emu){
2080 c009df3f Michael Niedermayer
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
2081 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2082
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2083 93a21abd Michael Niedermayer
    }
2084 b3184779 Michael Niedermayer
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2085 de6d9b64 Fabrice Bellard
}
2086 f7190f73 Michael Niedermayer
//FIXME move to dsputil, avg variant, 16x16 version
2087
static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
2088
    int x;
2089
    uint8_t * const top   = src[1];
2090
    uint8_t * const left  = src[2];
2091
    uint8_t * const mid   = src[0];
2092
    uint8_t * const right = src[3];
2093
    uint8_t * const bottom= src[4];
2094
#define OBMC_FILTER(x, t, l, m, r, b)\
2095
    dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
2096
#define OBMC_FILTER4(x, t, l, m, r, b)\
2097
    OBMC_FILTER(x         , t, l, m, r, b);\
2098
    OBMC_FILTER(x+1       , t, l, m, r, b);\
2099
    OBMC_FILTER(x  +stride, t, l, m, r, b);\
2100
    OBMC_FILTER(x+1+stride, t, l, m, r, b);
2101
    
2102
    x=0;
2103
    OBMC_FILTER (x  , 2, 2, 4, 0, 0);
2104
    OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
2105
    OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
2106
    OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
2107
    OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
2108
    OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
2109
    x+= stride;
2110
    OBMC_FILTER (x  , 1, 2, 5, 0, 0);
2111
    OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
2112
    OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
2113
    OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
2114
    x+= stride;
2115
    OBMC_FILTER4(x  , 1, 2, 5, 0, 0);
2116
    OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
2117
    OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
2118
    OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
2119
    x+= 2*stride;
2120
    OBMC_FILTER4(x  , 0, 2, 5, 0, 1);
2121
    OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
2122
    OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
2123
    OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
2124
    x+= 2*stride;
2125
    OBMC_FILTER (x  , 0, 2, 5, 0, 1);
2126
    OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
2127
    OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
2128
    OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
2129
    OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
2130
    OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
2131
    x+= stride;
2132
    OBMC_FILTER (x  , 0, 2, 4, 0, 2);
2133
    OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
2134
    OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
2135
    OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
2136
}
2137
2138
/* obmc for 1 8x8 luma block */
2139
static inline void obmc_motion(MpegEncContext *s,
2140
                               uint8_t *dest, uint8_t *src,
2141
                               int src_x, int src_y,
2142
                               op_pixels_func *pix_op,
2143
                               int16_t mv[5][2]/* mid top left right bottom*/)
2144
#define MID    0
2145
{
2146
    int i;
2147
    uint8_t *ptr[5];
2148
    
2149
    assert(s->quarter_sample==0);
2150
    
2151
    for(i=0; i<5; i++){
2152
        if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
2153
            ptr[i]= ptr[MID];
2154
        }else{
2155
            ptr[i]= s->edge_emu_buffer + 16 + 8*(i&1) + s->linesize*8*(i>>1);
2156
            hpel_motion(s, ptr[i], src,
2157
                        src_x, src_y,
2158
                        s->width, s->height, s->linesize,
2159
                        s->h_edge_pos, s->v_edge_pos,
2160
                        8, 8, pix_op,
2161
                        mv[i][0], mv[i][1]);
2162
        }
2163
    }
2164
2165
    put_obmc(dest, ptr, s->linesize);                
2166
}
2167 de6d9b64 Fabrice Bellard
2168 44eb4951 Michael Niedermayer
static inline void qpel_motion(MpegEncContext *s,
2169 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2170 44eb4951 Michael Niedermayer
                               int dest_offset,
2171 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset,
2172 b3184779 Michael Niedermayer
                               int field_based, op_pixels_func (*pix_op)[4],
2173
                               qpel_mc_func (*qpix_op)[16],
2174 44eb4951 Michael Niedermayer
                               int motion_x, int motion_y, int h)
2175
{
2176 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
2177 590a6358 Michael Niedermayer
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
2178 225f9c44 Michael Niedermayer
    int emu=0;
2179 44eb4951 Michael Niedermayer
2180
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2181
    src_x = s->mb_x * 16 + (motion_x >> 2);
2182
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
2183
2184
    height = s->height >> field_based;
2185 b5a093b3 Michael Niedermayer
    v_edge_pos = s->v_edge_pos >> field_based;
2186 44eb4951 Michael Niedermayer
    src_x = clip(src_x, -16, s->width);
2187
    if (src_x == s->width)
2188
        dxy &= ~3;
2189
    src_y = clip(src_y, -16, height);
2190
    if (src_y == height)
2191
        dxy &= ~12;
2192
    linesize = s->linesize << field_based;
2193 590a6358 Michael Niedermayer
    uvlinesize = s->uvlinesize << field_based;
2194 44eb4951 Michael Niedermayer
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
2195
    dest_y += dest_offset;
2196
//printf("%d %d %d\n", src_x, src_y, dxy);
2197 225f9c44 Michael Niedermayer
    
2198
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2199 a573cc27 Michael Niedermayer
        if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16 
2200
           || (unsigned)src_y >    v_edge_pos - (motion_y&3) - h  ){
2201 c009df3f Michael Niedermayer
            ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based, 
2202 4a3d7fbc Michael Niedermayer
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2203
            ptr= s->edge_emu_buffer + src_offset;
2204 225f9c44 Michael Niedermayer
            emu=1;
2205
        }
2206
    }
2207 590a6358 Michael Niedermayer
    if(!field_based)
2208
        qpix_op[0][dxy](dest_y, ptr, linesize);
2209
    else{
2210
        //damn interlaced mode
2211
        //FIXME boundary mirroring is not exactly correct here
2212
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
2213
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
2214
    }
2215 b3184779 Michael Niedermayer
2216 b50eef3a Michael Niedermayer
    if(s->flags&CODEC_FLAG_GRAY) return;
2217
2218 590a6358 Michael Niedermayer
    if(field_based){
2219
        mx= motion_x/2;
2220
        my= motion_y>>1;
2221 36df8805 Michael Niedermayer
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
2222
        static const int rtab[8]= {0,0,1,1,0,0,0,1};
2223
        mx= (motion_x>>1) + rtab[motion_x&7];
2224
        my= (motion_y>>1) + rtab[motion_y&7];
2225 03e93d35 Michael Niedermayer
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
2226 590a6358 Michael Niedermayer
        mx= (motion_x>>1)|(motion_x&1);
2227
        my= (motion_y>>1)|(motion_y&1);
2228
    }else{
2229
        mx= motion_x/2;
2230
        my= motion_y/2;
2231
    }
2232
    mx= (mx>>1)|(mx&1);
2233
    my= (my>>1)|(my&1);
2234 36df8805 Michael Niedermayer
2235 590a6358 Michael Niedermayer
    dxy= (mx&1) | ((my&1)<<1);
2236
    mx>>=1;
2237
    my>>=1;
2238 44eb4951 Michael Niedermayer
2239
    src_x = s->mb_x * 8 + mx;
2240
    src_y = s->mb_y * (8 >> field_based) + my;
2241
    src_x = clip(src_x, -8, s->width >> 1);
2242
    if (src_x == (s->width >> 1))
2243
        dxy &= ~1;
2244
    src_y = clip(src_y, -8, height >> 1);
2245
    if (src_y == (height >> 1))
2246
        dxy &= ~2;
2247
2248 590a6358 Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2249 44eb4951 Michael Niedermayer
    ptr = ref_picture[1] + offset;
2250 225f9c44 Michael Niedermayer
    if(emu){
2251 c009df3f Michael Niedermayer
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
2252 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2253
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2254 225f9c44 Michael Niedermayer
    }
2255 590a6358 Michael Niedermayer
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
2256 225f9c44 Michael Niedermayer
    
2257 44eb4951 Michael Niedermayer
    ptr = ref_picture[2] + offset;
2258 225f9c44 Michael Niedermayer
    if(emu){
2259 c009df3f Michael Niedermayer
        ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
2260 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2261
        ptr= s->edge_emu_buffer + (src_offset >> 1);
2262 225f9c44 Michael Niedermayer
    }
2263 590a6358 Michael Niedermayer
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
2264 44eb4951 Michael Niedermayer
}
2265
2266 67725183 Michael Niedermayer
inline int ff_h263_round_chroma(int x){
2267
    if (x >= 0)
2268
        return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2269
    else {
2270
        x = -x;
2271
        return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2272
    }
2273
}
2274 44eb4951 Michael Niedermayer
2275 eb14c713 Michael Niedermayer
/**
2276 f7190f73 Michael Niedermayer
 * h263 chorma 4mv motion compensation.
2277
 */
2278
static inline void chroma_4mv_motion(MpegEncContext *s,
2279
                                     uint8_t *dest_cb, uint8_t *dest_cr,
2280
                                     uint8_t **ref_picture,
2281
                                     op_pixels_func *pix_op,
2282
                                     int mx, int my){
2283
    int dxy, emu=0, src_x, src_y, offset;
2284
    uint8_t *ptr;
2285
    
2286
    /* In case of 8X8, we construct a single chroma motion vector
2287
       with a special rounding */
2288
    mx= ff_h263_round_chroma(mx);
2289
    my= ff_h263_round_chroma(my);
2290
    
2291
    dxy = ((my & 1) << 1) | (mx & 1);
2292
    mx >>= 1;
2293
    my >>= 1;
2294
2295
    src_x = s->mb_x * 8 + mx;
2296
    src_y = s->mb_y * 8 + my;
2297
    src_x = clip(src_x, -8, s->width/2);
2298
    if (src_x == s->width/2)
2299
        dxy &= ~1;
2300
    src_y = clip(src_y, -8, s->height/2);
2301
    if (src_y == s->height/2)
2302
        dxy &= ~2;
2303
    
2304
    offset = (src_y * (s->uvlinesize)) + src_x;
2305
    ptr = ref_picture[1] + offset;
2306
    if(s->flags&CODEC_FLAG_EMU_EDGE){
2307
        if(   (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
2308
           || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
2309
            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);
2310
            ptr= s->edge_emu_buffer;
2311
            emu=1;
2312
        }
2313
    }
2314
    pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
2315
2316
    ptr = ref_picture[2] + offset;
2317
    if(emu){
2318
        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);
2319
        ptr= s->edge_emu_buffer;
2320
    }
2321
    pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
2322
}
2323
2324
/**
2325 eb14c713 Michael Niedermayer
 * motion compesation of a single macroblock
2326
 * @param s context
2327
 * @param dest_y luma destination pointer
2328
 * @param dest_cb chroma cb/u destination pointer
2329
 * @param dest_cr chroma cr/v destination pointer
2330
 * @param dir direction (0->forward, 1->backward)
2331
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2332
 * @param pic_op halfpel motion compensation function (average or put normally)
2333
 * @param pic_op qpel motion compensation function (average or put normally)
2334
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2335
 */
2336 de6d9b64 Fabrice Bellard
static inline void MPV_motion(MpegEncContext *s, 
2337 0c1a9eda Zdenek Kabelac
                              uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2338
                              int dir, uint8_t **ref_picture, 
2339 b3184779 Michael Niedermayer
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2340 de6d9b64 Fabrice Bellard
{
2341 f7190f73 Michael Niedermayer
    int dxy, mx, my, src_x, src_y, motion_x, motion_y;
2342 de6d9b64 Fabrice Bellard
    int mb_x, mb_y, i;
2343 0c1a9eda Zdenek Kabelac
    uint8_t *ptr, *dest;
2344 de6d9b64 Fabrice Bellard
2345
    mb_x = s->mb_x;
2346
    mb_y = s->mb_y;
2347
2348 f7190f73 Michael Niedermayer
    if(s->obmc){
2349
        int16_t mv_cache[4][4][2];
2350
        const int xy= s->mb_x + s->mb_y*s->mb_stride;
2351
        const int mot_stride= s->mb_width*2 + 2;
2352
        const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
2353
2354
        assert(!s->mb_skiped);
2355
                
2356
        memcpy(mv_cache[1][1], s->motion_val[mot_xy           ], sizeof(int16_t)*4);
2357
        memcpy(mv_cache[2][1], s->motion_val[mot_xy+mot_stride], sizeof(int16_t)*4);
2358
        memcpy(mv_cache[3][1], s->motion_val[mot_xy+mot_stride], sizeof(int16_t)*4);
2359
2360
        if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
2361
            memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
2362
        }else{
2363
            memcpy(mv_cache[0][1], s->motion_val[mot_xy-mot_stride], sizeof(int16_t)*4);
2364
        }
2365
2366
        if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
2367
            *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
2368
            *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
2369
        }else{
2370
            *(int32_t*)mv_cache[1][0]= *(int32_t*)s->motion_val[mot_xy-1];
2371
            *(int32_t*)mv_cache[2][0]= *(int32_t*)s->motion_val[mot_xy-1+mot_stride];
2372
        }
2373
2374
        if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
2375
            *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
2376
            *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
2377
        }else{
2378
            *(int32_t*)mv_cache[1][3]= *(int32_t*)s->motion_val[mot_xy+2];
2379
            *(int32_t*)mv_cache[2][3]= *(int32_t*)s->motion_val[mot_xy+2+mot_stride];
2380
        }
2381
        
2382
        mx = 0;
2383
        my = 0;
2384
        for(i=0;i<4;i++) {
2385
            const int x= (i&1)+1;
2386
            const int y= (i>>1)+1;
2387
            int16_t mv[5][2]= {
2388
                {mv_cache[y][x  ][0], mv_cache[y][x  ][1]},
2389
                {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
2390
                {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
2391
                {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
2392
                {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
2393
            //FIXME cleanup
2394
            obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2395
                        ref_picture[0],
2396
                        mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2397
                        pix_op[1],
2398
                        mv);
2399
2400
            mx += mv[0][0];
2401
            my += mv[0][1];
2402
        }
2403
        if(!(s->flags&CODEC_FLAG_GRAY))
2404
            chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2405
2406
        return;
2407
    }
2408
   
2409 de6d9b64 Fabrice Bellard
    switch(s->mv_type) {
2410
    case MV_TYPE_16X16:
2411 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
2412 44eb4951 Michael Niedermayer
        if(s->mcsel){
2413 073b013d Michael Niedermayer
            if(s->real_sprite_warping_points==1){
2414
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
2415
                            ref_picture, 0);
2416
            }else{
2417
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
2418
                            ref_picture, 0);
2419
            }
2420 b3184779 Michael Niedermayer
        }else if(s->quarter_sample){
2421 44eb4951 Michael Niedermayer
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2422
                        ref_picture, 0,
2423
                        0, pix_op, qpix_op,
2424
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2425 1457ab52 Michael Niedermayer
        }else if(s->mspel){
2426
            ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2427
                        ref_picture, pix_op,
2428
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2429 1d0d55da Michael Niedermayer
        }else
2430
#endif
2431
        {
2432 44eb4951 Michael Niedermayer
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2433
                        ref_picture, 0,
2434
                        0, pix_op,
2435
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2436
        }           
2437 de6d9b64 Fabrice Bellard
        break;
2438
    case MV_TYPE_8X8:
2439 1e7bfebe Michael Niedermayer
        mx = 0;
2440
        my = 0;
2441
        if(s->quarter_sample){
2442
            for(i=0;i<4;i++) {
2443
                motion_x = s->mv[dir][i][0];
2444
                motion_y = s->mv[dir][i][1];
2445
2446
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2447
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2448
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2449
                    
2450
                /* WARNING: do no forget half pels */
2451
                src_x = clip(src_x, -16, s->width);
2452
                if (src_x == s->width)
2453
                    dxy &= ~3;
2454
                src_y = clip(src_y, -16, s->height);
2455
                if (src_y == s->height)
2456
                    dxy &= ~12;
2457
                    
2458
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2459
                if(s->flags&CODEC_FLAG_EMU_EDGE){
2460 a573cc27 Michael Niedermayer
                    if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8 
2461
                       || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
2462 c009df3f Michael Niedermayer
                        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);
2463 1e7bfebe Michael Niedermayer
                        ptr= s->edge_emu_buffer;
2464
                    }
2465
                }
2466
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2467
                qpix_op[1][dxy](dest, ptr, s->linesize);
2468
2469
                mx += s->mv[dir][i][0]/2;
2470
                my += s->mv[dir][i][1]/2;
2471
            }
2472
        }else{
2473
            for(i=0;i<4;i++) {
2474 f7190f73 Michael Niedermayer
                hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2475
                            ref_picture[0],
2476
                            mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2477
                            s->width, s->height, s->linesize,
2478
                            s->h_edge_pos, s->v_edge_pos,
2479
                            8, 8, pix_op[1],
2480
                            s->mv[dir][i][0], s->mv[dir][i][1]);
2481 1e7bfebe Michael Niedermayer
2482
                mx += s->mv[dir][i][0];
2483
                my += s->mv[dir][i][1];
2484 225f9c44 Michael Niedermayer
            }
2485 de6d9b64 Fabrice Bellard
        }
2486 1e7bfebe Michael Niedermayer
2487 f7190f73 Michael Niedermayer
        if(!(s->flags&CODEC_FLAG_GRAY))
2488
            chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2489 de6d9b64 Fabrice Bellard
        break;
2490
    case MV_TYPE_FIELD:
2491
        if (s->picture_structure == PICT_FRAME) {
2492 590a6358 Michael Niedermayer
            if(s->quarter_sample){
2493
                /* top field */
2494
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2495
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2496
                            1, pix_op, qpix_op,
2497
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
2498
                /* bottom field */
2499
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2500
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2501
                            1, pix_op, qpix_op,
2502
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
2503
            }else{
2504
                /* top field */       
2505
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2506
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2507
                            1, pix_op,
2508
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
2509
                /* bottom field */
2510
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2511
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2512
                            1, pix_op,
2513
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
2514
            }
2515 de6d9b64 Fabrice Bellard
        } else {
2516 dfb476cb Michael Niedermayer
            int offset;
2517
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2518 b536d0aa Michael Niedermayer
                offset= s->field_select[dir][0] ? s->linesize : 0;
2519 dfb476cb Michael Niedermayer
            }else{
2520
                ref_picture= s->current_picture.data;
2521 b536d0aa Michael Niedermayer
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
2522 dfb476cb Michael Niedermayer
            } 
2523 de6d9b64 Fabrice Bellard
2524 dfb476cb Michael Niedermayer
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2525
                        ref_picture, offset,
2526
                        0, pix_op,
2527
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
2528 de6d9b64 Fabrice Bellard
        }
2529
        break;
2530 d55e93e4 Ivan Kalvachev
    case MV_TYPE_16X8:{
2531
        int offset;
2532
         uint8_t ** ref2picture;
2533
2534
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2535
                ref2picture= ref_picture;
2536
                offset= s->field_select[dir][0] ? s->linesize : 0;
2537
            }else{
2538
                ref2picture= s->current_picture.data;
2539
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
2540
            } 
2541
2542
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2543
                        ref2picture, offset,
2544
                        0, pix_op,
2545
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2546
2547
2548
            if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
2549
                ref2picture= ref_picture;
2550
                offset= s->field_select[dir][1] ? s->linesize : 0;
2551
            }else{
2552
                ref2picture= s->current_picture.data;
2553
                offset= s->field_select[dir][1] ? s->linesize : -s->linesize; 
2554
            } 
2555
            // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
2556
            mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
2557
                        0,
2558
                        ref2picture, offset,
2559
                        0, pix_op,
2560
                        s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
2561
        }
2562
        
2563
        break;
2564 1dff7d56 Ivan Kalvachev
    case MV_TYPE_DMV:
2565
    {
2566
    op_pixels_func (*dmv_pix_op)[4];
2567
    int offset;
2568
2569
        dmv_pix_op = s->dsp.put_pixels_tab;
2570
2571
        if(s->picture_structure == PICT_FRAME){
2572
            //put top field from top field
2573
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2574
                        ref_picture, 0,
2575
                        1, dmv_pix_op,
2576
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2577
            //put bottom field from bottom field
2578
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2579
                        ref_picture, s->linesize,
2580
                        1, dmv_pix_op,
2581
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
2582
2583
            dmv_pix_op = s->dsp.avg_pixels_tab; 
2584
        
2585
            //avg top field from bottom field
2586
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2587
                        ref_picture, s->linesize,
2588
                        1, dmv_pix_op,
2589
                        s->mv[dir][2][0], s->mv[dir][2][1], 8);
2590
            //avg bottom field from top field
2591
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2592
                        ref_picture, 0,
2593
                        1, dmv_pix_op,
2594
                        s->mv[dir][3][0], s->mv[dir][3][1], 8);
2595
2596
        }else{
2597
            offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2598
                         s->linesize : 0;
2599
2600
            //put field from the same parity
2601
            //same parity is never in the same frame
2602
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2603
                        ref_picture,offset,
2604
                        0,dmv_pix_op,
2605
                        s->mv[dir][0][0],s->mv[dir][0][1],16);
2606
2607
            // after put we make avg of the same block
2608
            dmv_pix_op=s->dsp.avg_pixels_tab; 
2609
2610
            //opposite parity is always in the same frame if this is second field
2611
            if(!s->first_field){
2612
                ref_picture = s->current_picture.data;    
2613
                //top field is one linesize from frame beginig
2614
                offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2615
                        -s->linesize : s->linesize;
2616
            }else 
2617
                offset=(s->picture_structure == PICT_BOTTOM_FIELD)? 
2618
                        0 : s->linesize;
2619
2620
            //avg field from the opposite parity
2621
            mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
2622
                        ref_picture, offset,
2623
                        0,dmv_pix_op,
2624
                        s->mv[dir][2][0],s->mv[dir][2][1],16);
2625
        }
2626
    }
2627
    break;
2628 f7190f73 Michael Niedermayer
    default: assert(0);
2629 de6d9b64 Fabrice Bellard
    }
2630
}
2631
2632
2633
/* put block[] to dest[] */
2634
static inline void put_dct(MpegEncContext *s, 
2635 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2636 de6d9b64 Fabrice Bellard
{
2637 a0201736 Michael Niedermayer
    s->dct_unquantize(s, block, i, s->qscale);
2638 b0368839 Michael Niedermayer
    s->dsp.idct_put (dest, line_size, block);
2639 de6d9b64 Fabrice Bellard
}
2640
2641
/* add block[] to dest[] */
2642
static inline void add_dct(MpegEncContext *s, 
2643 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2644 de6d9b64 Fabrice Bellard
{
2645 0f440e02 Michael Niedermayer
    if (s->block_last_index[i] >= 0) {
2646 b0368839 Michael Niedermayer
        s->dsp.idct_add (dest, line_size, block);
2647 0f440e02 Michael Niedermayer
    }
2648
}
2649 2417652e Michael Niedermayer
2650 0f440e02 Michael Niedermayer
static inline void add_dequant_dct(MpegEncContext *s, 
2651 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
2652 0f440e02 Michael Niedermayer
{
2653 de6d9b64 Fabrice Bellard
    if (s->block_last_index[i] >= 0) {
2654 0f440e02 Michael Niedermayer
        s->dct_unquantize(s, block, i, s->qscale);
2655 9dbcbd92 Michael Niedermayer
2656 b0368839 Michael Niedermayer
        s->dsp.idct_add (dest, line_size, block);
2657 de6d9b64 Fabrice Bellard
    }
2658
}
2659
2660 7f2fe444 Michael Niedermayer
/**
2661
 * cleans dc, ac, coded_block for the current non intra MB
2662
 */
2663
void ff_clean_intra_table_entries(MpegEncContext *s)
2664
{
2665
    int wrap = s->block_wrap[0];
2666
    int xy = s->block_index[0];
2667
    
2668
    s->dc_val[0][xy           ] = 
2669
    s->dc_val[0][xy + 1       ] = 
2670
    s->dc_val[0][xy     + wrap] =
2671
    s->dc_val[0][xy + 1 + wrap] = 1024;
2672
    /* ac pred */
2673 0c1a9eda Zdenek Kabelac
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
2674
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2675 7f2fe444 Michael Niedermayer
    if (s->msmpeg4_version>=3) {
2676
        s->coded_block[xy           ] =
2677
        s->coded_block[xy + 1       ] =
2678
        s->coded_block[xy     + wrap] =
2679
        s->coded_block[xy + 1 + wrap] = 0;
2680
    }
2681
    /* chroma */
2682
    wrap = s->block_wrap[4];
2683
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
2684
    s->dc_val[1][xy] =
2685
    s->dc_val[2][xy] = 1024;
2686
    /* ac pred */
2687 0c1a9eda Zdenek Kabelac
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2688
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2689 7f2fe444 Michael Niedermayer
    
2690 7bc9090a Michael Niedermayer
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
2691 7f2fe444 Michael Niedermayer
}
2692
2693 de6d9b64 Fabrice Bellard
/* generic function called after a macroblock has been parsed by the
2694
   decoder or after it has been encoded by the encoder.
2695

2696
   Important variables used:
2697
   s->mb_intra : true if intra macroblock
2698
   s->mv_dir   : motion vector direction
2699
   s->mv_type  : motion vector type
2700
   s->mv       : motion vector
2701
   s->interlaced_dct : true if interlaced dct used (mpeg2)
2702
 */
2703
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
2704