Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ 46b4feec

History | View | Annotate | Download (142 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 202ef8b8 Michael Niedermayer
#include <ctype.h>
28 f943e138 Michael Niedermayer
#include <limits.h>
29 de6d9b64 Fabrice Bellard
#include "avcodec.h"
30
#include "dsputil.h"
31
#include "mpegvideo.h"
32
33 54329dd5 Nick Kurshev
#ifdef USE_FASTMEMCPY
34
#include "fastmemcpy.h"
35
#endif
36
37 2ad1516a Michael Niedermayer
//#undef NDEBUG
38
//#include <assert.h>
39
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 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
54 3d9fccbf Michael Niedermayer
55 0c1a9eda Zdenek Kabelac
void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
56 3d9fccbf Michael Niedermayer
57 de6d9b64 Fabrice Bellard
58
/* enable all paranoid tests for rounding, overflows, etc... */
59
//#define PARANOID
60
61
//#define DEBUG
62
63 101bea5f Juanjo
64 de6d9b64 Fabrice Bellard
/* for jpeg fast DCT */
65
#define CONST_BITS 14
66
67 eb4b3dd3 Zdenek Kabelac
static const uint16_t aanscales[64] = {
68 de6d9b64 Fabrice Bellard
    /* precomputed values scaled up by 14 bits */
69
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
70
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
71
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
72
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
73
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
74
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
75 1457ab52 Michael Niedermayer
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
76
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
77 de6d9b64 Fabrice Bellard
};
78
79 eb4b3dd3 Zdenek Kabelac
static const uint8_t h263_chroma_roundtab[16] = {
80 67725183 Michael Niedermayer
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
81 de6d9b64 Fabrice Bellard
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
82
};
83
84 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
85 0c1a9eda Zdenek Kabelac
static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
86
static uint8_t default_fcode_tab[MAX_MV*2+1];
87 45870f57 Michael Niedermayer
88 a33c7159 Michael Niedermayer
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
89
90 28db7fce Michael Niedermayer
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
91 0c1a9eda Zdenek Kabelac
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax)
92 de6d9b64 Fabrice Bellard
{
93 d7e9533a Michael Niedermayer
    int qscale;
94
95 cc7ac888 Michael Niedermayer
    for(qscale=qmin; qscale<=qmax; qscale++){
96 d7e9533a Michael Niedermayer
        int i;
97 b0368839 Michael Niedermayer
        if (s->dsp.fdct == ff_jpeg_fdct_islow) {
98 28db7fce Michael Niedermayer
            for(i=0;i<64;i++) {
99 b0368839 Michael Niedermayer
                const int j= s->dsp.idct_permutation[i];
100 28db7fce Michael Niedermayer
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
101
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
102
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
103
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
104
                
105 0c1a9eda Zdenek Kabelac
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 
106 28db7fce Michael Niedermayer
                                (qscale * quant_matrix[j]));
107
            }
108 b0368839 Michael Niedermayer
        } else if (s->dsp.fdct == fdct_ifast) {
109 d7e9533a Michael Niedermayer
            for(i=0;i<64;i++) {
110 b0368839 Michael Niedermayer
                const int j= s->dsp.idct_permutation[i];
111 d7e9533a 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 + 14)) / 
117 d7e9533a Michael Niedermayer
                                (aanscales[i] * qscale * quant_matrix[j]));
118
            }
119
        } else {
120
            for(i=0;i<64;i++) {
121 b0368839 Michael Niedermayer
                const int j= s->dsp.idct_permutation[i];
122 d7e9533a Michael Niedermayer
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
123
                   So 16           <= qscale * quant_matrix[i]             <= 7905
124
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
125
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
126
                */
127 0c1a9eda Zdenek Kabelac
                qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
128 477ab036 Michael Niedermayer
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
129 2ad1516a Michael Niedermayer
                qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
130 d7e9533a Michael Niedermayer
131
                if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
132
                qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
133
            }
134 de6d9b64 Fabrice Bellard
        }
135
    }
136
}
137 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
138
139 7f2fe444 Michael Niedermayer
// move into common.c perhaps 
140
#define CHECKED_ALLOCZ(p, size)\
141
{\
142
    p= av_mallocz(size);\
143
    if(p==NULL){\
144
        perror("malloc");\
145
        goto fail;\
146
    }\
147
}
148 4d2858de Michael Niedermayer
149 0c1a9eda Zdenek Kabelac
void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
150 2ad1516a Michael Niedermayer
    int i;
151
    int end;
152 7801d21d Michael Niedermayer
    
153
    st->scantable= src_scantable;
154 2ad1516a Michael Niedermayer
155
    for(i=0; i<64; i++){
156
        int j;
157
        j = src_scantable[i];
158 b0368839 Michael Niedermayer
        st->permutated[i] = s->dsp.idct_permutation[j];
159 05c4072b Michael Niedermayer
#ifdef ARCH_POWERPC
160
        st->inverse[j] = i;
161
#endif
162 2ad1516a Michael Niedermayer
    }
163
    
164
    end=-1;
165
    for(i=0; i<64; i++){
166
        int j;
167
        j = st->permutated[i];
168
        if(j>end) end=j;
169
        st->raster_end[i]= end;
170
    }
171
}
172
173 defdfc9a Alex Beregszaszi
/* init common dct for both encoder and decoder */
174
int DCT_common_init(MpegEncContext *s)
175 de6d9b64 Fabrice Bellard
{
176 3bf43d42 Michael Niedermayer
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
177 9dbf1ddd Michael Niedermayer
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
178
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
179 b0368839 Michael Niedermayer
180 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
181 28db7fce Michael Niedermayer
    s->dct_quantize= dct_quantize_c;
182 b0368839 Michael Niedermayer
#endif
183 21af69f7 Fabrice Bellard
        
184
#ifdef HAVE_MMX
185
    MPV_common_init_mmx(s);
186 a9b3f630 Nick Kurshev
#endif
187 e0580f8c Falk Hüffner
#ifdef ARCH_ALPHA
188
    MPV_common_init_axp(s);
189
#endif
190 c7e07931 Martin Olschewski
#ifdef HAVE_MLIB
191
    MPV_common_init_mlib(s);
192
#endif
193 5917d17c Leon van Stuivenberg
#ifdef HAVE_MMI
194
    MPV_common_init_mmi(s);
195
#endif
196 676e200c Michael Niedermayer
#ifdef ARCH_ARMV4L
197 83f238cb Zdenek Kabelac
    MPV_common_init_armv4l(s);
198 676e200c Michael Niedermayer
#endif
199 05c4072b Michael Niedermayer
#ifdef ARCH_POWERPC
200
    MPV_common_init_ppc(s);
201
#endif
202 676e200c Michael Niedermayer
203 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
204 3a87ac94 Michael Niedermayer
    s->fast_dct_quantize= s->dct_quantize;
205
206 477ab036 Michael Niedermayer
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
207
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
208
    }
209
210 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
211
212 2ad1516a Michael Niedermayer
    /* load & permutate scantables
213
       note: only wmv uses differnt ones 
214
    */
215
    ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
216
    ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
217
    ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218
    ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
219 d930ef19 Michael Niedermayer
220 dfb476cb Michael Niedermayer
    s->picture_structure= PICT_FRAME;
221
    
222 defdfc9a Alex Beregszaszi
    return 0;
223
}
224
225 1e491e29 Michael Niedermayer
/**
226 4e00e76b Michael Niedermayer
 * allocates a Picture
227
 * The pixels are allocated/set by calling get_buffer() if shared=0
228 1e491e29 Michael Niedermayer
 */
229 4e00e76b Michael Niedermayer
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
230
    
231
    if(shared){
232
        assert(pic->data[0]);
233
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
234
        pic->type= FF_BUFFER_TYPE_SHARED;
235
    }else{
236
        int r;
237
        
238
        assert(!pic->data[0]);
239
        
240 492cd3a9 Michael Niedermayer
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
241 4e00e76b Michael Niedermayer
        
242
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
243 ef9f7306 Måns Rullgård
            fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
244 4e00e76b Michael Niedermayer
            return -1;
245
        }
246
247
        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
248
            fprintf(stderr, "get_buffer() failed (stride changed)\n");
249
            return -1;
250
        }
251
252
        if(pic->linesize[1] != pic->linesize[2]){
253
            fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
254
            return -1;
255
        }
256
257
        s->linesize  = pic->linesize[0];
258
        s->uvlinesize= pic->linesize[1];
259 1e491e29 Michael Niedermayer
    }
260 4e00e76b Michael Niedermayer
    
261
    if(pic->qscale_table==NULL){
262
        if (s->encoding) {        
263 0c1a9eda Zdenek Kabelac
            CHECKED_ALLOCZ(pic->mb_var   , s->mb_num * sizeof(int16_t))
264
            CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
265
            CHECKED_ALLOCZ(pic->mb_mean  , s->mb_num * sizeof(int8_t))
266 67725183 Michael Niedermayer
            CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
267 4e00e76b Michael Niedermayer
        }
268 1e491e29 Michael Niedermayer
269 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
270
        CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
271 4e00e76b Michael Niedermayer
        pic->qstride= s->mb_width;
272
    }
273 1e491e29 Michael Niedermayer
    
274 f943e138 Michael Niedermayer
    //it might be nicer if the application would keep track of these but it would require a API change
275
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
276
    s->prev_pict_types[0]= s->pict_type;
277
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
278
        pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
279
    
280 1e491e29 Michael Niedermayer
    return 0;
281
fail: //for the CHECKED_ALLOCZ macro
282
    return -1;
283
}
284
285 4e00e76b Michael Niedermayer
/**
286
 * deallocates a picture
287
 */
288 1e491e29 Michael Niedermayer
static void free_picture(MpegEncContext *s, Picture *pic){
289
    int i;
290 4e00e76b Michael Niedermayer
291
    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
292 492cd3a9 Michael Niedermayer
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
293 4e00e76b Michael Niedermayer
    }
294
295 1e491e29 Michael Niedermayer
    av_freep(&pic->mb_var);
296
    av_freep(&pic->mc_mb_var);
297
    av_freep(&pic->mb_mean);
298 67725183 Michael Niedermayer
    av_freep(&pic->mb_cmp_score);
299 1e491e29 Michael Niedermayer
    av_freep(&pic->mbskip_table);
300
    av_freep(&pic->qscale_table);
301
    
302 4e00e76b Michael Niedermayer
    if(pic->type == FF_BUFFER_TYPE_INTERNAL){
303 1e491e29 Michael Niedermayer
        for(i=0; i<4; i++){
304
            av_freep(&pic->base[i]);
305
            pic->data[i]= NULL;
306
        }
307
        av_freep(&pic->opaque);
308 4e00e76b Michael Niedermayer
        pic->type= 0;
309
    }else if(pic->type == FF_BUFFER_TYPE_SHARED){
310
        for(i=0; i<4; i++){
311
            pic->base[i]=
312
            pic->data[i]= NULL;
313
        }
314
        pic->type= 0;        
315 1e491e29 Michael Niedermayer
    }
316
}
317
318 defdfc9a Alex Beregszaszi
/* init common structure for both encoder and decoder */
319
int MPV_common_init(MpegEncContext *s)
320
{
321 8b32880c Michael Niedermayer
    int y_size, c_size, yc_size, i;
322 defdfc9a Alex Beregszaszi
323 b0368839 Michael Niedermayer
    dsputil_init(&s->dsp, s->avctx);
324 defdfc9a Alex Beregszaszi
    DCT_common_init(s);
325 eb4b3dd3 Zdenek Kabelac
326 9fee1e23 Michael Niedermayer
    s->flags= s->avctx->flags;
327 defdfc9a Alex Beregszaszi
328 1e491e29 Michael Niedermayer
    s->mb_width  = (s->width  + 15) / 16;
329 de6d9b64 Fabrice Bellard
    s->mb_height = (s->height + 15) / 16;
330 eb4b3dd3 Zdenek Kabelac
331 b5a093b3 Michael Niedermayer
    /* set default edge pos, will be overriden in decode_header if needed */
332
    s->h_edge_pos= s->mb_width*16;
333
    s->v_edge_pos= s->mb_height*16;
334 eb4b3dd3 Zdenek Kabelac
335
    s->mb_num = s->mb_width * s->mb_height;
336
337
    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
338
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
339
    yc_size = y_size + 2 * c_size;
340
341 202ef8b8 Michael Niedermayer
    /* convert fourcc to upper case */
342 7004ffb3 Michael Niedermayer
    s->avctx->codec_tag=   toupper( s->avctx->codec_tag     &0xFF)          
343
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
344
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 
345
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
346 b5a093b3 Michael Niedermayer
347 f7b47594 Michael Niedermayer
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
348
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
349 1e491e29 Michael Niedermayer
350 492cd3a9 Michael Niedermayer
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
351 1e491e29 Michael Niedermayer
352 37fbfd0a Juanjo
    if (s->encoding) {
353 9dbcbd92 Michael Niedermayer
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
354
355
        /* Allocate MV tables */
356 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(int16_t))
357
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(int16_t))
358
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(int16_t))
359
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
360
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
361
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(int16_t))
362 7f2fe444 Michael Niedermayer
363 1e491e29 Michael Niedermayer
        //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
364 1457ab52 Michael Niedermayer
        CHECKED_ALLOCZ(s->me.scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
365 7f2fe444 Michael Niedermayer
        
366 1457ab52 Michael Niedermayer
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
367
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
368 91029be7 Michael Niedermayer
369 7f2fe444 Michael Niedermayer
        if(s->codec_id==CODEC_ID_MPEG4){
370
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
371
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
372
        }
373 3aa102be Michael Niedermayer
        
374 6b460aa3 Michael Niedermayer
        if(s->msmpeg4_version){
375
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
376
        }
377 3aa102be Michael Niedermayer
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
378 37fbfd0a Juanjo
    }
379 4d2858de Michael Niedermayer
        
380 0c1a9eda Zdenek Kabelac
    CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
381 37fbfd0a Juanjo
    
382 174489bd Michael Niedermayer
    if (s->out_format == FMT_H263 || s->encoding) {
383 de6d9b64 Fabrice Bellard
        int size;
384 7f2fe444 Michael Niedermayer
        /* Allocate MB type table */
385 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(uint8_t))
386 7f2fe444 Michael Niedermayer
387 de6d9b64 Fabrice Bellard
        /* MV prediction */
388
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
389 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
390 bea669e5 Michael Niedermayer
    }
391
392
    if(s->codec_id==CODEC_ID_MPEG4){
393 4d2858de Michael Niedermayer
        /* interlaced direct mode decoding tables */
394 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
395
        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
396 de6d9b64 Fabrice Bellard
    }
397 4d2858de Michael Niedermayer
    /* 4mv b frame decoding table */
398
    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
399 0c1a9eda Zdenek Kabelac
    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
400 6e2d5f1a Michael Niedermayer
    if (s->out_format == FMT_H263) {
401 de6d9b64 Fabrice Bellard
        /* ac values */
402 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
403 de6d9b64 Fabrice Bellard
        s->ac_val[1] = s->ac_val[0] + y_size;
404
        s->ac_val[2] = s->ac_val[1] + c_size;
405
        
406
        /* cbp values */
407 7f2fe444 Michael Niedermayer
        CHECKED_ALLOCZ(s->coded_block, y_size);
408 eec1c6b9 Michael Niedermayer
        
409
        /* divx501 bitstream reorder buffer */
410 7f2fe444 Michael Niedermayer
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
411 f01a1894 Michael Niedermayer
412 7f2fe444 Michael Niedermayer
        /* cbp, ac_pred, pred_dir */
413 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(uint8_t))
414
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
415 5b3438c6 Michael Niedermayer
    }
416 8b32880c Michael Niedermayer
    
417
    if (s->h263_pred || s->h263_plus || !s->encoding) {
418
        /* dc values */
419
        //MN: we need these for error resilience of intra-frames
420 0c1a9eda Zdenek Kabelac
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
421 8b32880c Michael Niedermayer
        s->dc_val[1] = s->dc_val[0] + y_size;
422
        s->dc_val[2] = s->dc_val[1] + c_size;
423
        for(i=0;i<yc_size;i++)
424
            s->dc_val[0][i] = 1024;
425
    }
426
427 7806197d Michael Niedermayer
    /* which mb is a intra block */
428
    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
429
    memset(s->mbintra_table, 1, s->mb_num);
430
    
431 de6d9b64 Fabrice Bellard
    /* default structure is frame */
432
    s->picture_structure = PICT_FRAME;
433 93a21abd Michael Niedermayer
    
434 3bb4e23a Fabrice Bellard
    /* init macroblock skip table */
435 4d2858de Michael Niedermayer
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
436
    //Note the +1 is for a quicker mpeg4 slice_end detection
437 f943e138 Michael Niedermayer
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
438 ba6802de Michael Niedermayer
    
439 91029be7 Michael Niedermayer
    s->block= s->blocks[0];
440 3bb4e23a Fabrice Bellard
441 d7425f59 Michael Niedermayer
    s->parse_context.state= -1;
442
443 de6d9b64 Fabrice Bellard
    s->context_initialized = 1;
444
    return 0;
445
 fail:
446 8257bf05 Zdenek Kabelac
    MPV_common_end(s);
447 de6d9b64 Fabrice Bellard
    return -1;
448
}
449
450 7f2fe444 Michael Niedermayer
451
//extern int sads;
452
453 de6d9b64 Fabrice Bellard
/* init common structure for both encoder and decoder */
454
void MPV_common_end(MpegEncContext *s)
455
{
456
    int i;
457
458 6000abfa Fabrice Bellard
    av_freep(&s->mb_type);
459
    av_freep(&s->p_mv_table);
460
    av_freep(&s->b_forw_mv_table);
461
    av_freep(&s->b_back_mv_table);
462
    av_freep(&s->b_bidir_forw_mv_table);
463
    av_freep(&s->b_bidir_back_mv_table);
464
    av_freep(&s->b_direct_mv_table);
465
    av_freep(&s->motion_val);
466
    av_freep(&s->dc_val[0]);
467
    av_freep(&s->ac_val[0]);
468
    av_freep(&s->coded_block);
469
    av_freep(&s->mbintra_table);
470 7f2fe444 Michael Niedermayer
    av_freep(&s->cbp_table);
471
    av_freep(&s->pred_dir_table);
472 1457ab52 Michael Niedermayer
    av_freep(&s->me.scratchpad);
473
    av_freep(&s->me.map);
474
    av_freep(&s->me.score_map);
475 7f2fe444 Michael Niedermayer
    
476 6000abfa Fabrice Bellard
    av_freep(&s->mbskip_table);
477 f943e138 Michael Niedermayer
    av_freep(&s->prev_pict_types);
478 6000abfa Fabrice Bellard
    av_freep(&s->bitstream_buffer);
479 7f2fe444 Michael Niedermayer
    av_freep(&s->tex_pb_buffer);
480
    av_freep(&s->pb2_buffer);
481 f7b47594 Michael Niedermayer
    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
482 bea669e5 Michael Niedermayer
    av_freep(&s->co_located_type_table);
483
    av_freep(&s->field_mv_table);
484
    av_freep(&s->field_select_table);
485 3aa102be Michael Niedermayer
    av_freep(&s->avctx->stats_out);
486 6b460aa3 Michael Niedermayer
    av_freep(&s->ac_stats);
487 4d2858de Michael Niedermayer
    av_freep(&s->error_status_table);
488 1e491e29 Michael Niedermayer
489
    for(i=0; i<MAX_PICTURE_COUNT; i++){
490
        free_picture(s, &s->picture[i]);
491 de6d9b64 Fabrice Bellard
    }
492
    s->context_initialized = 0;
493
}
494
495 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
496
497 de6d9b64 Fabrice Bellard
/* init video encoder */
498
int MPV_encode_init(AVCodecContext *avctx)
499
{
500
    MpegEncContext *s = avctx->priv_data;
501 519c2b6d Fabrice Bellard
    int i;
502 de6d9b64 Fabrice Bellard
503 bc657ac3 Zdenek Kabelac
    avctx->pix_fmt = PIX_FMT_YUV420P;
504
505 de6d9b64 Fabrice Bellard
    s->bit_rate = avctx->bit_rate;
506 9cdd6a24 Michael Niedermayer
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
507 de6d9b64 Fabrice Bellard
    s->width = avctx->width;
508
    s->height = avctx->height;
509 7f2fe444 Michael Niedermayer
    if(avctx->gop_size > 600){
510 333547be Michael Niedermayer
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
511 7f2fe444 Michael Niedermayer
        avctx->gop_size=600;
512
    }
513 de6d9b64 Fabrice Bellard
    s->gop_size = avctx->gop_size;
514 644d98a4 Juanjo
    s->rtp_mode = avctx->rtp_mode;
515
    s->rtp_payload_size = avctx->rtp_payload_size;
516 81401c1f Juanjo
    if (avctx->rtp_callback)
517
        s->rtp_callback = avctx->rtp_callback;
518 9cdd6a24 Michael Niedermayer
    s->max_qdiff= avctx->max_qdiff;
519
    s->qcompress= avctx->qcompress;
520
    s->qblur= avctx->qblur;
521 477c35a9 Arpi
    s->avctx = avctx;
522 ba6802de Michael Niedermayer
    s->flags= avctx->flags;
523 9dbcbd92 Michael Niedermayer
    s->max_b_frames= avctx->max_b_frames;
524 8b4c7dbc Michael Niedermayer
    s->b_frame_strategy= avctx->b_frame_strategy;
525 d7e9533a Michael Niedermayer
    s->codec_id= avctx->codec->id;
526 7f2fe444 Michael Niedermayer
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
527
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
528
    s->strict_std_compliance= avctx->strict_std_compliance;
529
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
530 1457ab52 Michael Niedermayer
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
531 87f8cab4 Michael Niedermayer
    s->mpeg_quant= avctx->mpeg_quant;
532 d7e9533a Michael Niedermayer
533 de6d9b64 Fabrice Bellard
    if (s->gop_size <= 1) {
534
        s->intra_only = 1;
535
        s->gop_size = 12;
536
    } else {
537
        s->intra_only = 0;
538
    }
539 5e746b99 Michael Niedermayer
540 1457ab52 Michael Niedermayer
    s->me_method = avctx->me_method;
541 5e746b99 Michael Niedermayer
542 e4986da9 Juanjo
    /* Fixed QSCALE */
543 de6d9b64 Fabrice Bellard
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
544 37fbfd0a Juanjo
    
545 c5d309f2 Michael Niedermayer
    s->adaptive_quant= (   s->avctx->lumi_masking
546 5e746b99 Michael Niedermayer
                        || s->avctx->dark_masking
547 c5d309f2 Michael Niedermayer
                        || s->avctx->temporal_cplx_masking 
548
                        || s->avctx->spatial_cplx_masking
549
                        || s->avctx->p_masking)
550
                       && !s->fixed_qscale;
551 fcb48651 Michael Niedermayer
    
552
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
553 5e746b99 Michael Niedermayer
554 de6d9b64 Fabrice Bellard
    switch(avctx->codec->id) {
555
    case CODEC_ID_MPEG1VIDEO:
556
        s->out_format = FMT_MPEG1;
557 14bea432 Michael Niedermayer
        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
558
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
559 de6d9b64 Fabrice Bellard
        break;
560
    case CODEC_ID_MJPEG:
561
        s->out_format = FMT_MJPEG;
562
        s->intra_only = 1; /* force intra only for jpeg */
563 37fbfd0a Juanjo
        s->mjpeg_write_tables = 1; /* write all tables */
564 a69b930c Alex Beregszaszi
        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
565 37fbfd0a Juanjo
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
566
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
567
        s->mjpeg_vsample[2] = 1; 
568 a69b930c Alex Beregszaszi
        s->mjpeg_hsample[0] = 2;
569 37fbfd0a Juanjo
        s->mjpeg_hsample[1] = 1; 
570
        s->mjpeg_hsample[2] = 1; 
571 de6d9b64 Fabrice Bellard
        if (mjpeg_init(s) < 0)
572
            return -1;
573 1ff662cc Michael Niedermayer
        avctx->delay=0;
574 4e00e76b Michael Niedermayer
        s->low_delay=1;
575 de6d9b64 Fabrice Bellard
        break;
576 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
577 de6d9b64 Fabrice Bellard
    case CODEC_ID_H263:
578 37fbfd0a Juanjo
        if (h263_get_picture_format(s->width, s->height) == 7) {
579
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
580 de6d9b64 Fabrice Bellard
            return -1;
581 37fbfd0a Juanjo
        }
582 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
583 1ff662cc Michael Niedermayer
        avctx->delay=0;
584 4e00e76b Michael Niedermayer
        s->low_delay=1;
585 de6d9b64 Fabrice Bellard
        break;
586
    case CODEC_ID_H263P:
587
        s->out_format = FMT_H263;
588
        s->h263_plus = 1;
589 21e59552 Michael Niedermayer
        /* Fx */
590
        s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
591
        s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
592
        /* /Fx */
593 544286b3 Juanjo
        /* These are just to be sure */
594 e51d6d27 Michael Niedermayer
        s->umvplus = 1;
595 1ff662cc Michael Niedermayer
        avctx->delay=0;
596 4e00e76b Michael Niedermayer
        s->low_delay=1;
597 de6d9b64 Fabrice Bellard
        break;
598
    case CODEC_ID_RV10:
599
        s->out_format = FMT_H263;
600
        s->h263_rv10 = 1;
601 1ff662cc Michael Niedermayer
        avctx->delay=0;
602 4e00e76b Michael Niedermayer
        s->low_delay=1;
603 de6d9b64 Fabrice Bellard
        break;
604 58f26ba9 Fabrice Bellard
    case CODEC_ID_MPEG4:
605 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
606
        s->h263_pred = 1;
607
        s->unrestricted_mv = 1;
608 4e00e76b Michael Niedermayer
        s->low_delay= s->max_b_frames ? 0 : 1;
609 4d2858de Michael Niedermayer
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
610 de6d9b64 Fabrice Bellard
        break;
611 84afee34 Michael Niedermayer
    case CODEC_ID_MSMPEG4V1:
612 de6d9b64 Fabrice Bellard
        s->out_format = FMT_H263;
613
        s->h263_msmpeg4 = 1;
614
        s->h263_pred = 1;
615
        s->unrestricted_mv = 1;
616 84afee34 Michael Niedermayer
        s->msmpeg4_version= 1;
617 1ff662cc Michael Niedermayer
        avctx->delay=0;
618 4e00e76b Michael Niedermayer
        s->low_delay=1;
619 84afee34 Michael Niedermayer
        break;
620
    case CODEC_ID_MSMPEG4V2:
621
        s->out_format = FMT_H263;
622
        s->h263_msmpeg4 = 1;
623
        s->h263_pred = 1;
624
        s->unrestricted_mv = 1;
625
        s->msmpeg4_version= 2;
626 1ff662cc Michael Niedermayer
        avctx->delay=0;
627 4e00e76b Michael Niedermayer
        s->low_delay=1;
628 84afee34 Michael Niedermayer
        break;
629
    case CODEC_ID_MSMPEG4V3:
630
        s->out_format = FMT_H263;
631
        s->h263_msmpeg4 = 1;
632
        s->h263_pred = 1;
633
        s->unrestricted_mv = 1;
634
        s->msmpeg4_version= 3;
635 1ff662cc Michael Niedermayer
        avctx->delay=0;
636 4e00e76b Michael Niedermayer
        s->low_delay=1;
637 de6d9b64 Fabrice Bellard
        break;
638 f5957f3f Michael Niedermayer
    case CODEC_ID_WMV1:
639
        s->out_format = FMT_H263;
640
        s->h263_msmpeg4 = 1;
641
        s->h263_pred = 1;
642
        s->unrestricted_mv = 1;
643
        s->msmpeg4_version= 4;
644
        avctx->delay=0;
645 4e00e76b Michael Niedermayer
        s->low_delay=1;
646 f5957f3f Michael Niedermayer
        break;
647
    case CODEC_ID_WMV2:
648
        s->out_format = FMT_H263;
649
        s->h263_msmpeg4 = 1;
650
        s->h263_pred = 1;
651
        s->unrestricted_mv = 1;
652
        s->msmpeg4_version= 5;
653
        avctx->delay=0;
654 4e00e76b Michael Niedermayer
        s->low_delay=1;
655 f5957f3f Michael Niedermayer
        break;
656 1d0d55da Michael Niedermayer
#endif
657 de6d9b64 Fabrice Bellard
    default:
658
        return -1;
659
    }
660 cf8039b2 Michael Niedermayer
    
661 45870f57 Michael Niedermayer
    { /* set up some save defaults, some codecs might override them later */
662
        static int done=0;
663
        if(!done){
664
            int i;
665
            done=1;
666 bbed9259 Michael Niedermayer
667 0c1a9eda Zdenek Kabelac
            default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
668
            memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
669
            memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
670 45870f57 Michael Niedermayer
671
            for(i=-16; i<16; i++){
672
                default_fcode_tab[i + MAX_MV]= 1;
673
            }
674
        }
675
    }
676 1457ab52 Michael Niedermayer
    s->me.mv_penalty= default_mv_penalty;
677 45870f57 Michael Niedermayer
    s->fcode_tab= default_fcode_tab;
678 a406617f Michael Niedermayer
    s->y_dc_scale_table=
679
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
680
 
681 9d2a0355 Michael Niedermayer
    /* dont use mv_penalty table for crap MV as it would be confused */
682 1457ab52 Michael Niedermayer
    //FIXME remove after fixing / removing old ME
683
    if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
684 9d2a0355 Michael Niedermayer
685 3bb4e23a Fabrice Bellard
    s->encoding = 1;
686
687 de6d9b64 Fabrice Bellard
    /* init */
688
    if (MPV_common_init(s) < 0)
689
        return -1;
690
    
691 1457ab52 Michael Niedermayer
    ff_init_me(s);
692
693 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
694 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
695 2ad1516a Michael Niedermayer
    if (s->out_format == FMT_H263)
696
        h263_encode_init(s);
697
    if(s->msmpeg4_version)
698
        ff_msmpeg4_encode_init(s);
699 bbed9259 Michael Niedermayer
#endif
700 1d0d55da Michael Niedermayer
    if (s->out_format == FMT_MPEG1)
701
        ff_mpeg1_encode_init(s);
702
#endif
703 2ad1516a Michael Niedermayer
704 519c2b6d Fabrice Bellard
    /* init default q matrix */
705
    for(i=0;i<64;i++) {
706 b0368839 Michael Niedermayer
        int j= s->dsp.idct_permutation[i];
707 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
708 87f8cab4 Michael Niedermayer
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
709 2ad1516a Michael Niedermayer
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
710
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
711 87f8cab4 Michael Niedermayer
        }else if(s->out_format == FMT_H263){
712 2ad1516a Michael Niedermayer
            s->intra_matrix[j] =
713
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
714 1d0d55da Michael Niedermayer
        }else
715
#endif
716
        { /* mpeg1 */
717 2ad1516a Michael Niedermayer
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
718
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
719 87f8cab4 Michael Niedermayer
        }
720 d7e9533a Michael Niedermayer
    }
721
722
    /* precompute matrix */
723 ef5b1b5a Juanjo
    /* for mjpeg, we do include qscale in the matrix */
724 d7e9533a Michael Niedermayer
    if (s->out_format != FMT_MJPEG) {
725 28db7fce Michael Niedermayer
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
726 cc7ac888 Michael Niedermayer
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
727 28db7fce Michael Niedermayer
        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
728 cc7ac888 Michael Niedermayer
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
729 519c2b6d Fabrice Bellard
    }
730
731 8b4c7dbc Michael Niedermayer
    if(ff_rate_control_init(s) < 0)
732
        return -1;
733 de6d9b64 Fabrice Bellard
734
    s->picture_number = 0;
735 45870f57 Michael Niedermayer
    s->picture_in_gop_number = 0;
736 de6d9b64 Fabrice Bellard
    s->fake_picture_number = 0;
737
    /* motion detector init */
738
    s->f_code = 1;
739 9dbcbd92 Michael Niedermayer
    s->b_code = 1;
740 de6d9b64 Fabrice Bellard
741
    return 0;
742
}
743
744
int MPV_encode_end(AVCodecContext *avctx)
745
{
746
    MpegEncContext *s = avctx->priv_data;
747
748
#ifdef STATS
749
    print_stats();
750
#endif
751 8b4c7dbc Michael Niedermayer
752
    ff_rate_control_uninit(s);
753
754 de6d9b64 Fabrice Bellard
    MPV_common_end(s);
755
    if (s->out_format == FMT_MJPEG)
756
        mjpeg_close(s);
757 37fbfd0a Juanjo
      
758 de6d9b64 Fabrice Bellard
    return 0;
759
}
760
761 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
762
763 1d0d55da Michael Niedermayer
void init_rl(RLTable *rl)
764
{
765 0c1a9eda Zdenek Kabelac
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
766
    uint8_t index_run[MAX_RUN+1];
767 1d0d55da Michael Niedermayer
    int last, run, level, start, end, i;
768
769
    /* compute max_level[], max_run[] and index_run[] */
770
    for(last=0;last<2;last++) {
771
        if (last == 0) {
772
            start = 0;
773
            end = rl->last;
774
        } else {
775
            start = rl->last;
776
            end = rl->n;
777
        }
778
779
        memset(max_level, 0, MAX_RUN + 1);
780
        memset(max_run, 0, MAX_LEVEL + 1);
781
        memset(index_run, rl->n, MAX_RUN + 1);
782
        for(i=start;i<end;i++) {
783
            run = rl->table_run[i];
784
            level = rl->table_level[i];
785
            if (index_run[run] == rl->n)
786
                index_run[run] = i;
787
            if (level > max_level[run])
788
                max_level[run] = level;
789
            if (run > max_run[level])
790
                max_run[level] = run;
791
        }
792
        rl->max_level[last] = av_malloc(MAX_RUN + 1);
793
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
794
        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
795
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
796
        rl->index_run[last] = av_malloc(MAX_RUN + 1);
797
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
798
    }
799
}
800
801 de6d9b64 Fabrice Bellard
/* draw the edges of width 'w' of an image of size width, height */
802 fd7db0fd Michael Niedermayer
//FIXME check that this is ok for mpeg4 interlaced
803 0c1a9eda Zdenek Kabelac
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
804 de6d9b64 Fabrice Bellard
{
805 0c1a9eda Zdenek Kabelac
    uint8_t *ptr, *last_line;
806 de6d9b64 Fabrice Bellard
    int i;
807
808
    last_line = buf + (height - 1) * wrap;
809
    for(i=0;i<w;i++) {
810
        /* top and bottom */
811
        memcpy(buf - (i + 1) * wrap, buf, width);
812
        memcpy(last_line + (i + 1) * wrap, last_line, width);
813
    }
814
    /* left and right */
815
    ptr = buf;
816
    for(i=0;i<height;i++) {
817
        memset(ptr - w, ptr[0], w);
818
        memset(ptr + width, ptr[width-1], w);
819
        ptr += wrap;
820
    }
821
    /* corners */
822
    for(i=0;i<w;i++) {
823
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
824
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
825
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
826
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
827
    }
828
}
829
830 4e00e76b Michael Niedermayer
static int find_unused_picture(MpegEncContext *s, int shared){
831
    int i;
832
    
833
    if(shared){
834
        for(i=0; i<MAX_PICTURE_COUNT; i++){
835
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
836
        }
837
    }else{
838
        for(i=0; i<MAX_PICTURE_COUNT; i++){
839
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
840
        }
841
        for(i=0; i<MAX_PICTURE_COUNT; i++){
842
            if(s->picture[i].data[0]==NULL) break;
843
        }
844
    }
845
846
    assert(i<MAX_PICTURE_COUNT);
847
    return i;
848
}
849
850 de6d9b64 Fabrice Bellard
/* generic function for encode/decode called before a frame is coded/decoded */
851 d6db1c9c Michael Niedermayer
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
852 de6d9b64 Fabrice Bellard
{
853 4e00e76b Michael Niedermayer
    int i;
854 492cd3a9 Michael Niedermayer
    AVFrame *pic;
855 de6d9b64 Fabrice Bellard
856 425dddb7 Fabrice Bellard
    s->mb_skiped = 0;
857 1e491e29 Michael Niedermayer
    
858
    /* mark&release old frames */
859 b536d0aa Michael Niedermayer
    if (s->pict_type != B_TYPE && s->last_picture_ptr) {
860
        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
861 1e491e29 Michael Niedermayer
862
        /* release forgotten pictures */
863
        /* if(mpeg124/h263) */
864
        if(!s->encoding){
865
            for(i=0; i<MAX_PICTURE_COUNT; i++){
866 b536d0aa Michael Niedermayer
                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
867 1e491e29 Michael Niedermayer
                    fprintf(stderr, "releasing zombie picture\n");
868 492cd3a9 Michael Niedermayer
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
869 1e491e29 Michael Niedermayer
                }
870
            }
871 d6db1c9c Michael Niedermayer
        }
872 93a21abd Michael Niedermayer
    }
873 b536d0aa Michael Niedermayer
    
874 aa388dba Michael Niedermayer
alloc:
875
    if(!s->encoding){
876 4e00e76b Michael Niedermayer
        i= find_unused_picture(s, 0);
877 1e491e29 Michael Niedermayer
    
878 492cd3a9 Michael Niedermayer
        pic= (AVFrame*)&s->picture[i];
879 1e491e29 Michael Niedermayer
        pic->reference= s->pict_type != B_TYPE;
880 b536d0aa Michael Niedermayer
881
        if(s->current_picture_ptr)
882
            pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
883 1e491e29 Michael Niedermayer
        
884 4e00e76b Michael Niedermayer
        alloc_picture(s, (Picture*)pic, 0);
885 93a21abd Michael Niedermayer
886 b536d0aa Michael Niedermayer
        s->current_picture_ptr= &s->picture[i];
887 1e491e29 Michael Niedermayer
    }
888 b7adc711 Michael Niedermayer
889 1e491e29 Michael Niedermayer
    if (s->pict_type != B_TYPE) {
890 b536d0aa Michael Niedermayer
        s->last_picture_ptr= s->next_picture_ptr;
891
        s->next_picture_ptr= s->current_picture_ptr;
892 de6d9b64 Fabrice Bellard
    }
893 b536d0aa Michael Niedermayer
    s->current_picture= *s->current_picture_ptr;
894
    if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
895
    if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
896
    if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
897 aa388dba Michael Niedermayer
    
898 b536d0aa Michael Niedermayer
    if(s->picture_structure!=PICT_FRAME){
899
        int i;
900
        for(i=0; i<4; i++){
901
            if(s->picture_structure == PICT_BOTTOM_FIELD){
902
                 s->current_picture.data[i] += s->current_picture.linesize[i];
903
            } 
904
            s->current_picture.linesize[i] *= 2;
905
            s->last_picture.linesize[i] *=2;
906
            s->next_picture.linesize[i] *=2;
907
        }
908
    }
909
    
910
    if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
911 aa388dba Michael Niedermayer
        fprintf(stderr, "warning: first frame is no keyframe\n");
912
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
913
        goto alloc;
914
    }
915 1e491e29 Michael Niedermayer
   
916 aa388dba Michael Niedermayer
    s->hurry_up= s->avctx->hurry_up;
917
    s->error_resilience= avctx->error_resilience;
918
919 d930ef19 Michael Niedermayer
    /* set dequantizer, we cant do it during init as it might change for mpeg4
920
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
921
    if(s->out_format == FMT_H263){
922
        if(s->mpeg_quant)
923
            s->dct_unquantize = s->dct_unquantize_mpeg2;
924
        else
925
            s->dct_unquantize = s->dct_unquantize_h263;
926
    }else 
927
        s->dct_unquantize = s->dct_unquantize_mpeg1;
928 d6db1c9c Michael Niedermayer
929
    return 0;
930 de6d9b64 Fabrice Bellard
}
931 21af69f7 Fabrice Bellard
932 de6d9b64 Fabrice Bellard
/* generic function for encode/decode called after a frame has been coded/decoded */
933
void MPV_frame_end(MpegEncContext *s)
934
{
935 1e491e29 Michael Niedermayer
    int i;
936 de6d9b64 Fabrice Bellard
    /* draw edge for correct motion prediction if outside */
937 1e491e29 Michael Niedermayer
    if(s->codec_id!=CODEC_ID_SVQ1){
938
        if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
939
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
940
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
941
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
942
        }
943 de6d9b64 Fabrice Bellard
    }
944 5975626d Michael Niedermayer
    emms_c();
945 8b4c7dbc Michael Niedermayer
    
946 3aa102be Michael Niedermayer
    s->last_pict_type    = s->pict_type;
947 8b4c7dbc Michael Niedermayer
    if(s->pict_type!=B_TYPE){
948
        s->last_non_b_pict_type= s->pict_type;
949
    }
950 b536d0aa Michael Niedermayer
#if 0
951
        /* copy back current_picture variables */
952 1e491e29 Michael Niedermayer
    for(i=0; i<MAX_PICTURE_COUNT; i++){
953
        if(s->picture[i].data[0] == s->current_picture.data[0]){
954
            s->picture[i]= s->current_picture;
955
            break;
956
        }    
957
    }
958
    assert(i<MAX_PICTURE_COUNT);
959 b536d0aa Michael Niedermayer
#endif    
960
    s->current_picture_ptr->quality= s->qscale; //FIXME get average of qscale_table
961
    s->current_picture_ptr->pict_type= s->pict_type;
962
    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
963 1e491e29 Michael Niedermayer
964
    /* release non refernce frames */
965
    for(i=0; i<MAX_PICTURE_COUNT; i++){
966 4e00e76b Michael Niedermayer
        if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
967 492cd3a9 Michael Niedermayer
            s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
968 1e491e29 Michael Niedermayer
    }
969 1457ab52 Michael Niedermayer
    if(s->avctx->debug&FF_DEBUG_SKIP){
970
        int x,y;        
971
        for(y=0; y<s->mb_height; y++){
972
            for(x=0; x<s->mb_width; x++){
973
                int count= s->mbskip_table[x + y*s->mb_width];
974
                if(count>9) count=9;
975
                printf(" %1d", count);
976
            }
977
            printf("\n");
978
        }
979
        printf("pict type: %d\n", s->pict_type);
980
    }
981 b536d0aa Michael Niedermayer
982
    // clear copies, to avoid confusion
983
#if 0
984
    memset(&s->last_picture, 0, sizeof(Picture));
985
    memset(&s->next_picture, 0, sizeof(Picture));
986
    memset(&s->current_picture, 0, sizeof(Picture));
987
#endif
988 de6d9b64 Fabrice Bellard
}
989
990 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
991
992 0d1e9246 Michael Niedermayer
static int get_sae(uint8_t *src, int ref, int stride){
993
    int x,y;
994
    int acc=0;
995
    
996
    for(y=0; y<16; y++){
997
        for(x=0; x<16; x++){
998
            acc+= ABS(src[x+y*stride] - ref);
999
        }
1000
    }
1001
    
1002
    return acc;
1003
}
1004
1005
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1006
    int x, y, w, h;
1007
    int acc=0;
1008
    
1009
    w= s->width &~15;
1010
    h= s->height&~15;
1011
    
1012
    for(y=0; y<h; y+=16){
1013
        for(x=0; x<w; x+=16){
1014
            int offset= x + y*stride;
1015
            int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1016
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1017
            int sae = get_sae(src + offset, mean, stride);
1018
            
1019
            acc+= sae + 500 < sad;
1020
        }
1021
    }
1022
    return acc;
1023
}
1024
1025 4e00e76b Michael Niedermayer
1026 492cd3a9 Michael Niedermayer
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1027
    AVFrame *pic;
1028 4e00e76b Michael Niedermayer
    int i;
1029 1e491e29 Michael Niedermayer
    const int encoding_delay= s->max_b_frames;
1030 4e00e76b Michael Niedermayer
    int direct=1;
1031 1e491e29 Michael Niedermayer
1032 4e00e76b Michael Niedermayer
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1033
    if(pic_arg->linesize[0] != s->linesize) direct=0;
1034
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1035
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1036
  
1037
//    printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1038 1e491e29 Michael Niedermayer
    
1039 4e00e76b Michael Niedermayer
    if(direct){
1040
        i= find_unused_picture(s, 1);
1041 1e491e29 Michael Niedermayer
1042 492cd3a9 Michael Niedermayer
        pic= (AVFrame*)&s->picture[i];
1043 4e00e76b Michael Niedermayer
        pic->reference= 1;
1044 1e491e29 Michael Niedermayer
    
1045 4e00e76b Michael Niedermayer
        for(i=0; i<4; i++){
1046
            pic->data[i]= pic_arg->data[i];
1047
            pic->linesize[i]= pic_arg->linesize[i];
1048
        }
1049
        alloc_picture(s, (Picture*)pic, 1);
1050
    }else{
1051
        i= find_unused_picture(s, 0);
1052 1e491e29 Michael Niedermayer
1053 492cd3a9 Michael Niedermayer
        pic= (AVFrame*)&s->picture[i];
1054 4e00e76b Michael Niedermayer
        pic->reference= 1;
1055 1e491e29 Michael Niedermayer
1056 4e00e76b Michael Niedermayer
        alloc_picture(s, (Picture*)pic, 0);
1057 b536d0aa Michael Niedermayer
        for(i=0; i<4; i++){
1058
            /* the input will be 16 pixels to the right relative to the actual buffer start
1059
             * and the current_pic, so the buffer can be reused, yes its not beatifull 
1060
             */
1061
            pic->data[i]+= 16; 
1062
        }
1063 4e00e76b Michael Niedermayer
1064
        if(   pic->data[0] == pic_arg->data[0] 
1065
           && pic->data[1] == pic_arg->data[1]
1066
           && pic->data[2] == pic_arg->data[2]){
1067 1e491e29 Michael Niedermayer
       // empty
1068 4e00e76b Michael Niedermayer
        }else{
1069
            int h_chroma_shift, v_chroma_shift;
1070 1e491e29 Michael Niedermayer
        
1071 4e00e76b Michael Niedermayer
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1072 1e491e29 Michael Niedermayer
        
1073 4e00e76b Michael Niedermayer
            for(i=0; i<3; i++){
1074
                int src_stride= pic_arg->linesize[i];
1075
                int dst_stride= i ? s->uvlinesize : s->linesize;
1076
                int h_shift= i ? h_chroma_shift : 0;
1077
                int v_shift= i ? v_chroma_shift : 0;
1078
                int w= s->width >>h_shift;
1079
                int h= s->height>>v_shift;
1080
                uint8_t *src= pic_arg->data[i];
1081
                uint8_t *dst= pic->data[i];
1082 1e491e29 Michael Niedermayer
            
1083 4e00e76b Michael Niedermayer
                if(src_stride==dst_stride)
1084
                    memcpy(dst, src, src_stride*h);
1085
                else{
1086
                    while(h--){
1087
                        memcpy(dst, src, w);
1088
                        dst += dst_stride;
1089
                        src += src_stride;
1090
                    }
1091 1e491e29 Michael Niedermayer
                }
1092 9dbcbd92 Michael Niedermayer
            }
1093 1e491e29 Michael Niedermayer
        }
1094
    }
1095 4e00e76b Michael Niedermayer
    pic->quality= pic_arg->quality;
1096
    pic->pict_type= pic_arg->pict_type;
1097 369fe599 Thomas Jarosch
    pic->pts = pic_arg->pts;
1098 4e00e76b Michael Niedermayer
    
1099
    if(s->input_picture[encoding_delay])
1100
        pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1101
1102
    /* shift buffer entries */
1103
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1104
        s->input_picture[i-1]= s->input_picture[i];
1105
        
1106
    s->input_picture[encoding_delay]= (Picture*)pic;
1107 1e491e29 Michael Niedermayer
1108
    return 0;
1109
}
1110 9dbcbd92 Michael Niedermayer
1111 1e491e29 Michael Niedermayer
static void select_input_picture(MpegEncContext *s){
1112
    int i;
1113
    const int encoding_delay= s->max_b_frames;
1114
    int coded_pic_num=0;    
1115
1116
    if(s->reordered_input_picture[0])
1117
        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
1118 4e00e76b Michael Niedermayer
1119 1e491e29 Michael Niedermayer
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1120
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1121
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1122
1123
    /* set next picture types & ordering */
1124
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1125 b536d0aa Michael Niedermayer
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1126 0d1e9246 Michael Niedermayer
            s->reordered_input_picture[0]= s->input_picture[0];
1127
            s->reordered_input_picture[0]->pict_type= I_TYPE;
1128
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1129
        }else{
1130
            int b_frames;
1131
            
1132
            if(s->flags&CODEC_FLAG_PASS2){
1133
                for(i=0; i<s->max_b_frames+1; i++){
1134
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1135
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1136
                    s->input_picture[i]->pict_type= pict_type;
1137
                    
1138
                    if(i + 1 >= s->rc_context.num_entries) break;
1139
                }
1140
            }
1141 4e00e76b Michael Niedermayer
1142 0d1e9246 Michael Niedermayer
            if(s->input_picture[0]->pict_type){
1143
                /* user selected pict_type */
1144 140cb663 Michael Niedermayer
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1145
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1146
                }
1147 0d1e9246 Michael Niedermayer
            
1148 140cb663 Michael Niedermayer
                if(b_frames > s->max_b_frames){
1149
                    fprintf(stderr, "warning, too many bframes in a row\n");
1150
                    b_frames = s->max_b_frames;
1151
                }
1152 0d1e9246 Michael Niedermayer
            }else if(s->b_frame_strategy==0){
1153
                b_frames= s->max_b_frames;
1154
            }else if(s->b_frame_strategy==1){
1155
                for(i=1; i<s->max_b_frames+1; i++){
1156
                    if(s->input_picture[i]->b_frame_score==0){
1157
                        s->input_picture[i]->b_frame_score= 
1158 4e00e76b Michael Niedermayer
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
1159
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1160 0d1e9246 Michael Niedermayer
                    }
1161
                }
1162
                for(i=0; i<s->max_b_frames; i++){
1163
                    if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1164
                }
1165
                                
1166
                b_frames= FFMAX(0, i-1);
1167 140cb663 Michael Niedermayer
                
1168 0d1e9246 Michael Niedermayer
                /* reset scores */
1169
                for(i=0; i<b_frames+1; i++){
1170
                    s->input_picture[i]->b_frame_score=0;
1171
                }
1172
            }else{
1173
                fprintf(stderr, "illegal b frame strategy\n");
1174
                b_frames=0;
1175 140cb663 Michael Niedermayer
            }
1176 0d1e9246 Michael Niedermayer
1177
            emms_c();
1178
//static int b_count=0;
1179
//b_count+= b_frames;
1180
//printf("b_frames: %d\n", b_count);
1181
                        
1182
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1183
            if(   s->picture_in_gop_number + b_frames >= s->gop_size 
1184
               || s->reordered_input_picture[0]->pict_type== I_TYPE)
1185 1e491e29 Michael Niedermayer
                s->reordered_input_picture[0]->pict_type= I_TYPE;
1186 0d1e9246 Michael Niedermayer
            else
1187
                s->reordered_input_picture[0]->pict_type= P_TYPE;
1188
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1189
            for(i=0; i<b_frames; i++){
1190
                coded_pic_num++;
1191
                s->reordered_input_picture[i+1]= s->input_picture[i];
1192
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1193
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1194 9dbcbd92 Michael Niedermayer
            }
1195
        }
1196
    }
1197 1e491e29 Michael Niedermayer
    
1198
    if(s->reordered_input_picture[0]){
1199 b536d0aa Michael Niedermayer
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
1200
1201
        s->new_picture= *s->reordered_input_picture[0];
1202 4e00e76b Michael Niedermayer
1203
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1204 b536d0aa Michael Niedermayer
            // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1205
        
1206 4e00e76b Michael Niedermayer
            int i= find_unused_picture(s, 0);
1207
            Picture *pic= &s->picture[i];
1208
1209
            /* mark us unused / free shared pic */
1210
            for(i=0; i<4; i++)
1211
                s->reordered_input_picture[0]->data[i]= NULL;
1212
            s->reordered_input_picture[0]->type= 0;
1213
            
1214 b536d0aa Michael Niedermayer
            //FIXME bad, copy * except
1215 4e00e76b Michael Niedermayer
            pic->pict_type = s->reordered_input_picture[0]->pict_type;
1216
            pic->quality   = s->reordered_input_picture[0]->quality;
1217
            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1218
            pic->reference = s->reordered_input_picture[0]->reference;
1219
            
1220
            alloc_picture(s, pic, 0);
1221
1222 b536d0aa Michael Niedermayer
            s->current_picture_ptr= pic;
1223 4e00e76b Michael Niedermayer
        }else{
1224 b536d0aa Michael Niedermayer
            // input is not a shared pix -> reuse buffer for current_pix
1225
1226 4e00e76b Michael Niedermayer
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
1227
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1228
            
1229 b536d0aa Michael Niedermayer
            s->current_picture_ptr= s->reordered_input_picture[0];
1230 4e00e76b Michael Niedermayer
            for(i=0; i<4; i++){
1231 b536d0aa Michael Niedermayer
                //reverse the +16 we did before storing the input
1232
                s->current_picture_ptr->data[i]-=16;
1233 4e00e76b Michael Niedermayer
            }
1234 1e491e29 Michael Niedermayer
        }
1235 b536d0aa Michael Niedermayer
        s->current_picture= *s->current_picture_ptr;
1236 1e491e29 Michael Niedermayer
    
1237
        s->picture_number= s->new_picture.display_picture_number;
1238
//printf("dpn:%d\n", s->picture_number);
1239
    }else{
1240
       memset(&s->new_picture, 0, sizeof(Picture));
1241 9dbcbd92 Michael Niedermayer
    }
1242
}
1243
1244 de6d9b64 Fabrice Bellard
int MPV_encode_picture(AVCodecContext *avctx,
1245
                       unsigned char *buf, int buf_size, void *data)
1246
{
1247
    MpegEncContext *s = avctx->priv_data;
1248 492cd3a9 Michael Niedermayer
    AVFrame *pic_arg = data;
1249 140cb663 Michael Niedermayer
    int i;
1250 de6d9b64 Fabrice Bellard
1251
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1252
1253 1e491e29 Michael Niedermayer
    s->picture_in_gop_number++;
1254 de6d9b64 Fabrice Bellard
1255 1e491e29 Michael Niedermayer
    load_input_picture(s, pic_arg);
1256 8b4c7dbc Michael Niedermayer
    
1257 1e491e29 Michael Niedermayer
    select_input_picture(s);
1258 9dbcbd92 Michael Niedermayer
    
1259
    /* output? */
1260 1e491e29 Michael Niedermayer
    if(s->new_picture.data[0]){
1261 9dbcbd92 Michael Niedermayer
1262 1e491e29 Michael Niedermayer
        s->pict_type= s->new_picture.pict_type;
1263
        if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1264
            s->qscale= (int)(s->new_picture.quality+0.5);
1265
            assert(s->qscale);
1266
        }
1267
//emms_c();
1268
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1269 93a21abd Michael Niedermayer
        MPV_frame_start(s, avctx);
1270 9dbcbd92 Michael Niedermayer
1271
        encode_picture(s, s->picture_number);
1272 208d3ddf Michael Niedermayer
        
1273 9cd3766f Juanjo
        avctx->real_pict_num  = s->picture_number;
1274 9dbcbd92 Michael Niedermayer
        avctx->header_bits = s->header_bits;
1275
        avctx->mv_bits     = s->mv_bits;
1276
        avctx->misc_bits   = s->misc_bits;
1277
        avctx->i_tex_bits  = s->i_tex_bits;
1278
        avctx->p_tex_bits  = s->p_tex_bits;
1279
        avctx->i_count     = s->i_count;
1280 66370d3f Michael Niedermayer
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1281 9dbcbd92 Michael Niedermayer
        avctx->skip_count  = s->skip_count;
1282
1283
        MPV_frame_end(s);
1284
1285
        if (s->out_format == FMT_MJPEG)
1286
            mjpeg_picture_trailer(s);
1287 8b4c7dbc Michael Niedermayer
        
1288
        if(s->flags&CODEC_FLAG_PASS1)
1289
            ff_write_pass1_stats(s);
1290 b536d0aa Michael Niedermayer
1291
        for(i=0; i<4; i++){
1292
            avctx->error[i] += s->current_picture_ptr->error[i];
1293
        }
1294 de6d9b64 Fabrice Bellard
    }
1295
1296 9dbcbd92 Michael Niedermayer
    s->input_picture_number++;
1297 de6d9b64 Fabrice Bellard
1298
    flush_put_bits(&s->pb);
1299 9cdd6a24 Michael Niedermayer
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1300 3aa102be Michael Niedermayer
    
1301 9cdd6a24 Michael Niedermayer
    s->total_bits += s->frame_bits;
1302 098eefe1 Michael Niedermayer
    avctx->frame_bits  = s->frame_bits;
1303 140cb663 Michael Niedermayer
    
1304 17592475 Michael Niedermayer
    return pbBufPtr(&s->pb) - s->pb.buf;
1305 de6d9b64 Fabrice Bellard
}
1306
1307 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1308
1309 44eb4951 Michael Niedermayer
static inline void gmc1_motion(MpegEncContext *s,
1310 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1311 44eb4951 Michael Niedermayer
                               int dest_offset,
1312 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset)
1313 44eb4951 Michael Niedermayer
{
1314 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1315 0fd90455 Michael Niedermayer
    int offset, src_x, src_y, linesize, uvlinesize;
1316 44eb4951 Michael Niedermayer
    int motion_x, motion_y;
1317 54993774 Michael Niedermayer
    int emu=0;
1318 44eb4951 Michael Niedermayer
1319
    motion_x= s->sprite_offset[0][0];
1320
    motion_y= s->sprite_offset[0][1];
1321
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1322
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1323
    motion_x<<=(3-s->sprite_warping_accuracy);
1324
    motion_y<<=(3-s->sprite_warping_accuracy);
1325
    src_x = clip(src_x, -16, s->width);
1326
    if (src_x == s->width)
1327
        motion_x =0;
1328
    src_y = clip(src_y, -16, s->height);
1329
    if (src_y == s->height)
1330
        motion_y =0;
1331 073b013d Michael Niedermayer
1332 44eb4951 Michael Niedermayer
    linesize = s->linesize;
1333 0fd90455 Michael Niedermayer
    uvlinesize = s->uvlinesize;
1334 073b013d Michael Niedermayer
    
1335 44eb4951 Michael Niedermayer
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1336
1337
    dest_y+=dest_offset;
1338 54993774 Michael Niedermayer
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1339 741235eb Michael Niedermayer
        if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1340
                              || src_y + 17 >= s->v_edge_pos){
1341 1457ab52 Michael Niedermayer
            ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1342 54993774 Michael Niedermayer
            ptr= s->edge_emu_buffer;
1343
        }
1344
    }
1345 073b013d Michael Niedermayer
    
1346
    if((motion_x|motion_y)&7){
1347 eb4b3dd3 Zdenek Kabelac
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1348
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1349 073b013d Michael Niedermayer
    }else{
1350
        int dxy;
1351
        
1352
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1353
        if (s->no_rounding){
1354 eb4b3dd3 Zdenek Kabelac
            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1355 073b013d Michael Niedermayer
        }else{
1356 eb4b3dd3 Zdenek Kabelac
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
1357
        }
1358 073b013d Michael Niedermayer
    }
1359
    
1360
    if(s->flags&CODEC_FLAG_GRAY) return;
1361 44eb4951 Michael Niedermayer
1362
    motion_x= s->sprite_offset[1][0];
1363
    motion_y= s->sprite_offset[1][1];
1364
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1365
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1366
    motion_x<<=(3-s->sprite_warping_accuracy);
1367
    motion_y<<=(3-s->sprite_warping_accuracy);
1368
    src_x = clip(src_x, -8, s->width>>1);
1369
    if (src_x == s->width>>1)
1370
        motion_x =0;
1371
    src_y = clip(src_y, -8, s->height>>1);
1372
    if (src_y == s->height>>1)
1373
        motion_y =0;
1374
1375 0fd90455 Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
1376 44eb4951 Michael Niedermayer
    ptr = ref_picture[1] + offset;
1377 741235eb Michael Niedermayer
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1378
        if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1379
                              || src_y + 9 >= s->v_edge_pos>>1){
1380
            ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1381
            ptr= s->edge_emu_buffer;
1382
            emu=1;
1383
        }
1384 54993774 Michael Niedermayer
    }
1385 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1386 54993774 Michael Niedermayer
    
1387 44eb4951 Michael Niedermayer
    ptr = ref_picture[2] + offset;
1388 54993774 Michael Niedermayer
    if(emu){
1389 1457ab52 Michael Niedermayer
        ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1390 54993774 Michael Niedermayer
        ptr= s->edge_emu_buffer;
1391
    }
1392 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
1393 44eb4951 Michael Niedermayer
    
1394
    return;
1395
}
1396
1397 073b013d Michael Niedermayer
static inline void gmc_motion(MpegEncContext *s,
1398 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1399 073b013d Michael Niedermayer
                               int dest_offset,
1400 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset)
1401 073b013d Michael Niedermayer
{
1402 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1403 073b013d Michael Niedermayer
    int linesize, uvlinesize;
1404
    const int a= s->sprite_warping_accuracy;
1405
    int ox, oy;
1406
1407
    linesize = s->linesize;
1408
    uvlinesize = s->uvlinesize;
1409
1410
    ptr = ref_picture[0] + src_offset;
1411
1412
    dest_y+=dest_offset;
1413
    
1414
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1415
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1416
1417 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1418 073b013d Michael Niedermayer
           ox, 
1419
           oy, 
1420
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1421
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1422
           a+1, (1<<(2*a+1)) - s->no_rounding,
1423
           s->h_edge_pos, s->v_edge_pos);
1424 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1425 073b013d Michael Niedermayer
           ox + s->sprite_delta[0][0]*8, 
1426
           oy + s->sprite_delta[1][0]*8, 
1427
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1428
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1429
           a+1, (1<<(2*a+1)) - s->no_rounding,
1430
           s->h_edge_pos, s->v_edge_pos);
1431
1432
    if(s->flags&CODEC_FLAG_GRAY) return;
1433
1434
1435
    dest_cb+=dest_offset>>1;
1436
    dest_cr+=dest_offset>>1;
1437
    
1438
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1439
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1440
1441
    ptr = ref_picture[1] + (src_offset>>1);
1442 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1443 073b013d Michael Niedermayer
           ox, 
1444
           oy, 
1445
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1446
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1447
           a+1, (1<<(2*a+1)) - s->no_rounding,
1448
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1449
    
1450
    ptr = ref_picture[2] + (src_offset>>1);
1451 eb4b3dd3 Zdenek Kabelac
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1452 073b013d Michael Niedermayer
           ox, 
1453
           oy, 
1454
           s->sprite_delta[0][0], s->sprite_delta[0][1],
1455
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
1456
           a+1, (1<<(2*a+1)) - s->no_rounding,
1457
           s->h_edge_pos>>1, s->v_edge_pos>>1);
1458
}
1459
1460
1461 0c1a9eda Zdenek Kabelac
void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h, 
1462 93a21abd Michael Niedermayer
                                    int src_x, int src_y, int w, int h){
1463
    int x, y;
1464
    int start_y, start_x, end_y, end_x;
1465 0c1a9eda Zdenek Kabelac
    uint8_t *buf= s->edge_emu_buffer;
1466 eb4b3dd3 Zdenek Kabelac
1467 93a21abd Michael Niedermayer
    if(src_y>= h){
1468
        src+= (h-1-src_y)*linesize;
1469
        src_y=h-1;
1470 225f9c44 Michael Niedermayer
    }else if(src_y<=-block_h){
1471
        src+= (1-block_h-src_y)*linesize;
1472
        src_y=1-block_h;
1473 93a21abd Michael Niedermayer
    }
1474
    if(src_x>= w){
1475
        src+= (w-1-src_x);
1476
        src_x=w-1;
1477 225f9c44 Michael Niedermayer
    }else if(src_x<=-block_w){
1478
        src+= (1-block_w-src_x);
1479
        src_x=1-block_w;
1480 93a21abd Michael Niedermayer
    }
1481
1482 b8a78f41 Michael Niedermayer
    start_y= FFMAX(0, -src_y);
1483
    start_x= FFMAX(0, -src_x);
1484
    end_y= FFMIN(block_h, h-src_y);
1485
    end_x= FFMIN(block_w, w-src_x);
1486 54993774 Michael Niedermayer
1487 93a21abd Michael Niedermayer
    // copy existing part
1488
    for(y=start_y; y<end_y; y++){
1489
        for(x=start_x; x<end_x; x++){
1490
            buf[x + y*linesize]= src[x + y*linesize];
1491
        }
1492
    }
1493
1494
    //top
1495
    for(y=0; y<start_y; y++){
1496
        for(x=start_x; x<end_x; x++){
1497
            buf[x + y*linesize]= buf[x + start_y*linesize];
1498
        }
1499
    }
1500
1501
    //bottom
1502
    for(y=end_y; y<block_h; y++){
1503
        for(x=start_x; x<end_x; x++){
1504
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1505
        }
1506
    }
1507
                                    
1508
    for(y=0; y<block_h; y++){
1509
       //left
1510
        for(x=0; x<start_x; x++){
1511
            buf[x + y*linesize]= buf[start_x + y*linesize];
1512
        }
1513
       
1514
       //right
1515
        for(x=end_x; x<block_w; x++){
1516
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1517
        }
1518
    }
1519
}
1520
1521
1522 de6d9b64 Fabrice Bellard
/* apply one mpeg motion vector to the three components */
1523
static inline void mpeg_motion(MpegEncContext *s,
1524 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1525 de6d9b64 Fabrice Bellard
                               int dest_offset,
1526 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset,
1527 b3184779 Michael Niedermayer
                               int field_based, op_pixels_func (*pix_op)[4],
1528 de6d9b64 Fabrice Bellard
                               int motion_x, int motion_y, int h)
1529
{
1530 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1531 b5a093b3 Michael Niedermayer
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1532 93a21abd Michael Niedermayer
    int emu=0;
1533 b3184779 Michael Niedermayer
#if 0    
1534 44eb4951 Michael Niedermayer
if(s->quarter_sample)
1535
{
1536
    motion_x>>=1;
1537
    motion_y>>=1;
1538
}
1539 b3184779 Michael Niedermayer
#endif
1540 de6d9b64 Fabrice Bellard
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1541
    src_x = s->mb_x * 16 + (motion_x >> 1);
1542
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1543
                
1544
    /* WARNING: do no forget half pels */
1545
    height = s->height >> field_based;
1546 b5a093b3 Michael Niedermayer
    v_edge_pos = s->v_edge_pos >> field_based;
1547 de6d9b64 Fabrice Bellard
    src_x = clip(src_x, -16, s->width);
1548
    if (src_x == s->width)
1549
        dxy &= ~1;
1550
    src_y = clip(src_y, -16, height);
1551
    if (src_y == height)
1552
        dxy &= ~2;
1553 b536d0aa Michael Niedermayer
    linesize   = s->current_picture.linesize[0] << field_based;
1554
    uvlinesize = s->current_picture.linesize[1] << field_based;
1555 de6d9b64 Fabrice Bellard
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1556
    dest_y += dest_offset;
1557 93a21abd Michael Niedermayer
1558
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1559 b5a093b3 Michael Niedermayer
        if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1560
                              || src_y + (motion_y&1) + h  > v_edge_pos){
1561 b536d0aa Michael Niedermayer
            ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,  //FIXME linesize? and uv below
1562 4a3d7fbc Michael Niedermayer
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1563
            ptr= s->edge_emu_buffer + src_offset;
1564 93a21abd Michael Niedermayer
            emu=1;
1565
        }
1566
    }
1567 b3184779 Michael Niedermayer
    pix_op[0][dxy](dest_y, ptr, linesize, h);
1568 de6d9b64 Fabrice Bellard
1569 b50eef3a Michael Niedermayer
    if(s->flags&CODEC_FLAG_GRAY) return;
1570
1571 de6d9b64 Fabrice Bellard
    if (s->out_format == FMT_H263) {
1572
        dxy = 0;
1573
        if ((motion_x & 3) != 0)
1574
            dxy |= 1;
1575
        if ((motion_y & 3) != 0)
1576
            dxy |= 2;
1577
        mx = motion_x >> 2;
1578
        my = motion_y >> 2;
1579
    } else {
1580
        mx = motion_x / 2;
1581
        my = motion_y / 2;
1582
        dxy = ((my & 1) << 1) | (mx & 1);
1583
        mx >>= 1;
1584
        my >>= 1;
1585
    }
1586
    
1587
    src_x = s->mb_x * 8 + mx;
1588
    src_y = s->mb_y * (8 >> field_based) + my;
1589
    src_x = clip(src_x, -8, s->width >> 1);
1590
    if (src_x == (s->width >> 1))
1591
        dxy &= ~1;
1592
    src_y = clip(src_y, -8, height >> 1);
1593
    if (src_y == (height >> 1))
1594
        dxy &= ~2;
1595 4c9ede2e Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1596 de6d9b64 Fabrice Bellard
    ptr = ref_picture[1] + offset;
1597 93a21abd Michael Niedermayer
    if(emu){
1598 1457ab52 Michael Niedermayer
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1599 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1600
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1601 93a21abd Michael Niedermayer
    }
1602 b3184779 Michael Niedermayer
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1603 93a21abd Michael Niedermayer
1604 de6d9b64 Fabrice Bellard
    ptr = ref_picture[2] + offset;
1605 93a21abd Michael Niedermayer
    if(emu){
1606 1457ab52 Michael Niedermayer
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
1607 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1608
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1609 93a21abd Michael Niedermayer
    }
1610 b3184779 Michael Niedermayer
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1611 de6d9b64 Fabrice Bellard
}
1612
1613 44eb4951 Michael Niedermayer
static inline void qpel_motion(MpegEncContext *s,
1614 0c1a9eda Zdenek Kabelac
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1615 44eb4951 Michael Niedermayer
                               int dest_offset,
1616 0c1a9eda Zdenek Kabelac
                               uint8_t **ref_picture, int src_offset,
1617 b3184779 Michael Niedermayer
                               int field_based, op_pixels_func (*pix_op)[4],
1618
                               qpel_mc_func (*qpix_op)[16],
1619 44eb4951 Michael Niedermayer
                               int motion_x, int motion_y, int h)
1620
{
1621 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
1622 590a6358 Michael Niedermayer
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1623 225f9c44 Michael Niedermayer
    int emu=0;
1624 44eb4951 Michael Niedermayer
1625
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1626
    src_x = s->mb_x * 16 + (motion_x >> 2);
1627
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1628
1629
    height = s->height >> field_based;
1630 b5a093b3 Michael Niedermayer
    v_edge_pos = s->v_edge_pos >> field_based;
1631 44eb4951 Michael Niedermayer
    src_x = clip(src_x, -16, s->width);
1632
    if (src_x == s->width)
1633
        dxy &= ~3;
1634
    src_y = clip(src_y, -16, height);
1635
    if (src_y == height)
1636
        dxy &= ~12;
1637
    linesize = s->linesize << field_based;
1638 590a6358 Michael Niedermayer
    uvlinesize = s->uvlinesize << field_based;
1639 44eb4951 Michael Niedermayer
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1640
    dest_y += dest_offset;
1641
//printf("%d %d %d\n", src_x, src_y, dxy);
1642 225f9c44 Michael Niedermayer
    
1643
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1644 b5a093b3 Michael Niedermayer
        if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1645
                              || src_y + (motion_y&3) + h  > v_edge_pos){
1646 1457ab52 Michael Niedermayer
            ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
1647 4a3d7fbc Michael Niedermayer
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1648
            ptr= s->edge_emu_buffer + src_offset;
1649 225f9c44 Michael Niedermayer
            emu=1;
1650
        }
1651
    }
1652 590a6358 Michael Niedermayer
    if(!field_based)
1653
        qpix_op[0][dxy](dest_y, ptr, linesize);
1654
    else{
1655
        //damn interlaced mode
1656
        //FIXME boundary mirroring is not exactly correct here
1657
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
1658
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1659
    }
1660 b3184779 Michael Niedermayer
1661 b50eef3a Michael Niedermayer
    if(s->flags&CODEC_FLAG_GRAY) return;
1662
1663 590a6358 Michael Niedermayer
    if(field_based){
1664
        mx= motion_x/2;
1665
        my= motion_y>>1;
1666 36df8805 Michael Niedermayer
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1667
        static const int rtab[8]= {0,0,1,1,0,0,0,1};
1668
        mx= (motion_x>>1) + rtab[motion_x&7];
1669
        my= (motion_y>>1) + rtab[motion_y&7];
1670 03e93d35 Michael Niedermayer
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1671 590a6358 Michael Niedermayer
        mx= (motion_x>>1)|(motion_x&1);
1672
        my= (motion_y>>1)|(motion_y&1);
1673
    }else{
1674
        mx= motion_x/2;
1675
        my= motion_y/2;
1676
    }
1677
    mx= (mx>>1)|(mx&1);
1678
    my= (my>>1)|(my&1);
1679 36df8805 Michael Niedermayer
1680 590a6358 Michael Niedermayer
    dxy= (mx&1) | ((my&1)<<1);
1681
    mx>>=1;
1682
    my>>=1;
1683 44eb4951 Michael Niedermayer
1684
    src_x = s->mb_x * 8 + mx;
1685
    src_y = s->mb_y * (8 >> field_based) + my;
1686
    src_x = clip(src_x, -8, s->width >> 1);
1687
    if (src_x == (s->width >> 1))
1688
        dxy &= ~1;
1689
    src_y = clip(src_y, -8, height >> 1);
1690
    if (src_y == (height >> 1))
1691
        dxy &= ~2;
1692
1693 590a6358 Michael Niedermayer
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
1694 44eb4951 Michael Niedermayer
    ptr = ref_picture[1] + offset;
1695 225f9c44 Michael Niedermayer
    if(emu){
1696 1457ab52 Michael Niedermayer
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1697 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1698
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1699 225f9c44 Michael Niedermayer
    }
1700 590a6358 Michael Niedermayer
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1701 225f9c44 Michael Niedermayer
    
1702 44eb4951 Michael Niedermayer
    ptr = ref_picture[2] + offset;
1703 225f9c44 Michael Niedermayer
    if(emu){
1704 1457ab52 Michael Niedermayer
        ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
1705 4a3d7fbc Michael Niedermayer
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1706
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1707 225f9c44 Michael Niedermayer
    }
1708 590a6358 Michael Niedermayer
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
1709 44eb4951 Michael Niedermayer
}
1710
1711 67725183 Michael Niedermayer
inline int ff_h263_round_chroma(int x){
1712
    if (x >= 0)
1713
        return  (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1714
    else {
1715
        x = -x;
1716
        return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1717
    }
1718
}
1719 44eb4951 Michael Niedermayer
1720 de6d9b64 Fabrice Bellard
static inline void MPV_motion(MpegEncContext *s, 
1721 0c1a9eda Zdenek Kabelac
                              uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1722
                              int dir, uint8_t **ref_picture, 
1723 b3184779 Michael Niedermayer
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
1724 de6d9b64 Fabrice Bellard
{
1725
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1726
    int mb_x, mb_y, i;
1727 0c1a9eda Zdenek Kabelac
    uint8_t *ptr, *dest;
1728 225f9c44 Michael Niedermayer
    int emu=0;
1729 de6d9b64 Fabrice Bellard
1730
    mb_x = s->mb_x;
1731
    mb_y = s->mb_y;
1732
1733
    switch(s->mv_type) {
1734
    case MV_TYPE_16X16:
1735 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
1736 44eb4951 Michael Niedermayer
        if(s->mcsel){
1737 073b013d Michael Niedermayer
            if(s->real_sprite_warping_points==1){
1738
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1739
                            ref_picture, 0);
1740
            }else{
1741
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1742
                            ref_picture, 0);
1743
            }
1744 b3184779 Michael Niedermayer
        }else if(s->quarter_sample){
1745 44eb4951 Michael Niedermayer
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1746
                        ref_picture, 0,
1747
                        0, pix_op, qpix_op,
1748
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1749 1457ab52 Michael Niedermayer
        }else if(s->mspel){
1750
            ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
1751
                        ref_picture, pix_op,
1752
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1753 1d0d55da Michael Niedermayer
        }else
1754
#endif
1755
        {
1756 44eb4951 Michael Niedermayer
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1757
                        ref_picture, 0,
1758
                        0, pix_op,
1759
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1760
        }           
1761 de6d9b64 Fabrice Bellard
        break;
1762
    case MV_TYPE_8X8:
1763 1e7bfebe Michael Niedermayer
        mx = 0;
1764
        my = 0;
1765
        if(s->quarter_sample){
1766
            for(i=0;i<4;i++) {
1767
                motion_x = s->mv[dir][i][0];
1768
                motion_y = s->mv[dir][i][1];
1769
1770
                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1771
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1772
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1773
                    
1774
                /* WARNING: do no forget half pels */
1775
                src_x = clip(src_x, -16, s->width);
1776
                if (src_x == s->width)
1777
                    dxy &= ~3;
1778
                src_y = clip(src_y, -16, s->height);
1779
                if (src_y == s->height)
1780
                    dxy &= ~12;
1781
                    
1782
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1783
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1784
                    if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1785
                                          || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1786 1457ab52 Michael Niedermayer
                        ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1787 1e7bfebe Michael Niedermayer
                        ptr= s->edge_emu_buffer;
1788
                    }
1789
                }
1790
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1791
                qpix_op[1][dxy](dest, ptr, s->linesize);
1792
1793
                mx += s->mv[dir][i][0]/2;
1794
                my += s->mv[dir][i][1]/2;
1795
            }
1796
        }else{
1797
            for(i=0;i<4;i++) {
1798
                motion_x = s->mv[dir][i][0];
1799
                motion_y = s->mv[dir][i][1];
1800 de6d9b64 Fabrice Bellard
1801 1e7bfebe Michael Niedermayer
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1802
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1803
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1804 de6d9b64 Fabrice Bellard
                    
1805 1e7bfebe Michael Niedermayer
                /* WARNING: do no forget half pels */
1806
                src_x = clip(src_x, -16, s->width);
1807
                if (src_x == s->width)
1808
                    dxy &= ~1;
1809
                src_y = clip(src_y, -16, s->height);
1810
                if (src_y == s->height)
1811
                    dxy &= ~2;
1812 de6d9b64 Fabrice Bellard
                    
1813 1e7bfebe Michael Niedermayer
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1814
                if(s->flags&CODEC_FLAG_EMU_EDGE){
1815
                    if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1816
                                          || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1817 1457ab52 Michael Niedermayer
                        ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1818 1e7bfebe Michael Niedermayer
                        ptr= s->edge_emu_buffer;
1819
                    }
1820 225f9c44 Michael Niedermayer
                }
1821 1e7bfebe Michael Niedermayer
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1822
                pix_op[1][dxy](dest, ptr, s->linesize, 8);
1823
1824
                mx += s->mv[dir][i][0];
1825
                my += s->mv[dir][i][1];
1826 225f9c44 Michael Niedermayer
            }
1827 de6d9b64 Fabrice Bellard
        }
1828 1e7bfebe Michael Niedermayer
1829 b50eef3a Michael Niedermayer
        if(s->flags&CODEC_FLAG_GRAY) break;
1830 de6d9b64 Fabrice Bellard
        /* In case of 8X8, we construct a single chroma motion vector
1831
           with a special rounding */
1832 67725183 Michael Niedermayer
        mx= ff_h263_round_chroma(mx);
1833
        my= ff_h263_round_chroma(my);
1834 de6d9b64 Fabrice Bellard
        dxy = ((my & 1) << 1) | (mx & 1);
1835
        mx >>= 1;
1836
        my >>= 1;
1837
1838
        src_x = mb_x * 8 + mx;
1839
        src_y = mb_y * 8 + my;
1840
        src_x = clip(src_x, -8, s->width/2);
1841
        if (src_x == s->width/2)
1842
            dxy &= ~1;
1843
        src_y = clip(src_y, -8, s->height/2);
1844
        if (src_y == s->height/2)
1845
            dxy &= ~2;
1846
        
1847 0fd90455 Michael Niedermayer
        offset = (src_y * (s->uvlinesize)) + src_x;
1848 de6d9b64 Fabrice Bellard
        ptr = ref_picture[1] + offset;
1849 225f9c44 Michael Niedermayer
        if(s->flags&CODEC_FLAG_EMU_EDGE){
1850 b5a093b3 Michael Niedermayer
                if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1851
                                      || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1852 1457ab52 Michael Niedermayer
                    ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1853 225f9c44 Michael Niedermayer
                    ptr= s->edge_emu_buffer;
1854
                    emu=1;
1855
                }
1856
            }
1857 b3184779 Michael Niedermayer
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
1858 225f9c44 Michael Niedermayer
1859 de6d9b64 Fabrice Bellard
        ptr = ref_picture[2] + offset;
1860 225f9c44 Michael Niedermayer
        if(emu){
1861 1457ab52 Michael Niedermayer
            ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1862 225f9c44 Michael Niedermayer
            ptr= s->edge_emu_buffer;
1863
        }
1864 b3184779 Michael Niedermayer
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
1865 de6d9b64 Fabrice Bellard
        break;
1866
    case MV_TYPE_FIELD:
1867
        if (s->picture_structure == PICT_FRAME) {
1868 590a6358 Michael Niedermayer
            if(s->quarter_sample){
1869
                /* top field */
1870
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1871
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1872
                            1, pix_op, qpix_op,
1873
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1874
                /* bottom field */
1875
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1876
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1877
                            1, pix_op, qpix_op,
1878
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1879
            }else{
1880
                /* top field */       
1881
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1882
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1883
                            1, pix_op,
1884
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
1885
                /* bottom field */
1886
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1887
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1888
                            1, pix_op,
1889
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
1890
            }
1891 de6d9b64 Fabrice Bellard
        } else {
1892 dfb476cb Michael Niedermayer
            int offset;
1893
            if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
1894 b536d0aa Michael Niedermayer
                offset= s->field_select[dir][0] ? s->linesize : 0;
1895 dfb476cb Michael Niedermayer
            }else{
1896
                ref_picture= s->current_picture.data;
1897 b536d0aa Michael Niedermayer
                offset= s->field_select[dir][0] ? s->linesize : -s->linesize; 
1898 dfb476cb Michael Niedermayer
            } 
1899 de6d9b64 Fabrice Bellard
1900 dfb476cb Michael Niedermayer
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1901
                        ref_picture, offset,
1902
                        0, pix_op,
1903
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
1904 de6d9b64 Fabrice Bellard
        }
1905
        break;
1906
    }
1907
}
1908
1909
1910
/* put block[] to dest[] */
1911
static inline void put_dct(MpegEncContext *s, 
1912 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1913 de6d9b64 Fabrice Bellard
{
1914 a0201736 Michael Niedermayer
    s->dct_unquantize(s, block, i, s->qscale);
1915 b0368839 Michael Niedermayer
    s->dsp.idct_put (dest, line_size, block);
1916 de6d9b64 Fabrice Bellard
}
1917
1918
/* add block[] to dest[] */
1919
static inline void add_dct(MpegEncContext *s, 
1920 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1921 de6d9b64 Fabrice Bellard
{
1922 0f440e02 Michael Niedermayer
    if (s->block_last_index[i] >= 0) {
1923 b0368839 Michael Niedermayer
        s->dsp.idct_add (dest, line_size, block);
1924 0f440e02 Michael Niedermayer
    }
1925
}
1926 2417652e Michael Niedermayer
1927 0f440e02 Michael Niedermayer
static inline void add_dequant_dct(MpegEncContext *s, 
1928 0c1a9eda Zdenek Kabelac
                           DCTELEM *block, int i, uint8_t *dest, int line_size)
1929 0f440e02 Michael Niedermayer
{
1930 de6d9b64 Fabrice Bellard
    if (s->block_last_index[i] >= 0) {
1931 0f440e02 Michael Niedermayer
        s->dct_unquantize(s, block, i, s->qscale);
1932 9dbcbd92 Michael Niedermayer
1933 b0368839 Michael Niedermayer
        s->dsp.idct_add (dest, line_size, block);
1934 de6d9b64 Fabrice Bellard
    }
1935
}
1936
1937 7f2fe444 Michael Niedermayer
/**
1938
 * cleans dc, ac, coded_block for the current non intra MB
1939
 */
1940
void ff_clean_intra_table_entries(MpegEncContext *s)
1941
{
1942
    int wrap = s->block_wrap[0];
1943
    int xy = s->block_index[0];
1944
    
1945
    s->dc_val[0][xy           ] = 
1946
    s->dc_val[0][xy + 1       ] = 
1947
    s->dc_val[0][xy     + wrap] =
1948
    s->dc_val[0][xy + 1 + wrap] = 1024;
1949
    /* ac pred */
1950 0c1a9eda Zdenek Kabelac
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
1951
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1952 7f2fe444 Michael Niedermayer
    if (s->msmpeg4_version>=3) {
1953
        s->coded_block[xy           ] =
1954
        s->coded_block[xy + 1       ] =
1955
        s->coded_block[xy     + wrap] =
1956
        s->coded_block[xy + 1 + wrap] = 0;
1957
    }
1958
    /* chroma */
1959
    wrap = s->block_wrap[4];
1960
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1961
    s->dc_val[1][xy] =
1962
    s->dc_val[2][xy] = 1024;
1963
    /* ac pred */
1964 0c1a9eda Zdenek Kabelac
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1965
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1966 7f2fe444 Michael Niedermayer
    
1967
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1968
}
1969
1970 de6d9b64 Fabrice Bellard
/* generic function called after a macroblock has been parsed by the
1971
   decoder or after it has been encoded by the encoder.
1972

1973
   Important variables used:
1974
   s->mb_intra : true if intra macroblock
1975
   s->mv_dir   : motion vector direction
1976
   s->mv_type  : motion vector type
1977
   s->mv       : motion vector
1978
   s->interlaced_dct : true if interlaced dct used (mpeg2)
1979
 */
1980
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1981
{
1982 8257bf05 Zdenek Kabelac
    int mb_x, mb_y;
1983 0f440e02 Michael Niedermayer
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
1984 de6d9b64 Fabrice Bellard
1985
    mb_x = s->mb_x;
1986
    mb_y = s->mb_y;
1987
1988 1e491e29 Michael Niedermayer
    s->current_picture.qscale_table[mb_xy]= s->qscale;
1989 79e7b305 Arpi
1990 de6d9b64 Fabrice Bellard
    /* update DC predictors for P macroblocks */
1991
    if (!s->mb_intra) {
1992 9e15ad28 Juanjo
        if (s->h263_pred || s->h263_aic) {
1993 0f440e02 Michael Niedermayer
            if(s->mbintra_table[mb_xy])
1994 7f2fe444 Michael Niedermayer
                ff_clean_intra_table_entries(s);
1995 de6d9b64 Fabrice Bellard
        } else {
1996 7f2fe444 Michael Niedermayer
            s->last_dc[0] =
1997
            s->last_dc[1] =
1998 de6d9b64 Fabrice Bellard
            s->last_dc[2] = 128 << s->intra_dc_precision;
1999
        }
2000
    }
2001 9e15ad28 Juanjo
    else if (s->h263_pred || s->h263_aic)
2002 0f440e02 Michael Niedermayer
        s->mbintra_table[mb_xy]=1;
2003 bff6ecaa Michael Niedermayer
2004 c5b1c10a Michael Niedermayer
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
2005 0f440e02 Michael Niedermayer
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
2006 1457ab52 Michael Niedermayer
        //FIXME a lot of thet is only needed for !low_delay
2007 0f440e02 Michael Niedermayer
        const int wrap = s->block_wrap[0];
2008
        const int xy = s->block_index[0];
2009 bea669e5 Michael Niedermayer
        const int mb_index= s->mb_x + s->mb_y*s->mb_width;
2010 fd7db0fd Michael Niedermayer
        if(s->mv_type == MV_TYPE_8X8){
2011 bea669e5 Michael Niedermayer
            s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
2012 fd7db0fd Michael Niedermayer
        } else {
2013
            int motion_x, motion_y;
2014
            if (s->mb_intra) {
2015
                motion_x = 0;
2016
                motion_y = 0;
2017 bea669e5 Michael Niedermayer
                if(s->co_located_type_table)
2018
                    s->co_located_type_table[mb_index]= 0;
2019 fd7db0fd Michael Niedermayer
            } else if (s->mv_type == MV_TYPE_16X16) {
2020
                motion_x = s->mv[0][0][0];
2021
                motion_y = s->mv[0][0][1];
2022 bea669e5 Michael Niedermayer
                if(s->co_located_type_table)
2023
                    s->co_located_type_table[mb_index]= 0;
2024 fd7db0fd Michael Niedermayer
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
2025 bea669e5 Michael Niedermayer
                int i;
2026 fd7db0fd Michael Niedermayer
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2027
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2028
                motion_x = (motion_x>>1) | (motion_x&1);
2029 bea669e5 Michael Niedermayer
                for(i=0; i<2; i++){
2030
                    s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
2031
                    s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
2032
                    s->field_select_table[mb_index][i]= s->field_select[0][i];
2033
                }
2034
                s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
2035 fd7db0fd Michael Niedermayer
            }
2036 de6d9b64 Fabrice Bellard
            /* no update if 8X8 because it has been done during parsing */
2037 8257bf05 Zdenek Kabelac
            s->motion_val[xy][0] = motion_x;
2038
            s->motion_val[xy][1] = motion_y;
2039
            s->motion_val[xy + 1][0] = motion_x;
2040
            s->motion_val[xy + 1][1] = motion_y;
2041
            s->motion_val[xy + wrap][0] = motion_x;
2042
            s->motion_val[xy + wrap][1] = motion_y;
2043
            s->motion_val[xy + 1 + wrap][0] = motion_x;
2044
            s->motion_val[xy + 1 + wrap][1] = motion_y;
2045 de6d9b64 Fabrice Bellard
        }
2046
    }
2047
    
2048 140cb663 Michael Niedermayer
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2049 0c1a9eda Zdenek Kabelac
        uint8_t *dest_y, *dest_cb, *dest_cr;
2050 0f440e02 Michael Niedermayer
        int dct_linesize, dct_offset;
2051 b3184779 Michael Niedermayer
        op_pixels_func (*op_pix)[4];
2052
        qpel_mc_func (*op_qpix)[16];
2053 b536d0aa Michael Niedermayer
        const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2054
        const int uvlinesize= s->current_picture.linesize[1];
2055 3bb4e23a Fabrice Bellard
2056 1e491e29 Michael Niedermayer
        /* avoid copy if macroblock skipped in last frame too */
2057 9dbcbd92 Michael Niedermayer
        if (s->pict_type != B_TYPE) {
2058 1e491e29 Michael Niedermayer
            s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2059
        }
2060
2061
        /* skip only during decoding as we might trash the buffers during encoding a bit */
2062
        if(!s->encoding){
2063 0c1a9eda Zdenek Kabelac
            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2064 1e491e29 Michael Niedermayer
            const int age= s->current_picture.age;
2065 0fd90455 Michael Niedermayer
2066 1e491e29 Michael Niedermayer
            assert(age);
2067
2068
            if (s->mb_skiped) {
2069
                s->mb_skiped= 0;
2070
                assert(s->pict_type!=I_TYPE);
2071
 
2072 0fd90455 Michael Niedermayer
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2073
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2074
2075 1e491e29 Michael Niedermayer
                /* if previous was skipped too, then nothing to do !  */
2076 f943e138 Michael Niedermayer
                if (*mbskip_ptr >= age && s->current_picture.reference){
2077
                    return;
2078 1e491e29 Michael Niedermayer
                }
2079 f943e138 Michael Niedermayer
            } else if(!s->current_picture.reference){
2080
                (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2081
                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2082
            } else{
2083 3bb4e23a Fabrice Bellard
                *mbskip_ptr = 0; /* not skipped */
2084
            }
2085 1e491e29 Michael Niedermayer
        }else
2086
            s->mb_skiped= 0;
2087 de6d9b64 Fabrice Bellard
2088 640950c7 Michael Niedermayer
        if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2089 1e491e29 Michael Niedermayer
            dest_y  = s->current_picture.data[0] + mb_x * 16;
2090
            dest_cb = s->current_picture.data[1] + mb_x * 8;
2091
            dest_cr = s->current_picture.data[2] + mb_x * 8;
2092 3994623d Michael Niedermayer
        }else{
2093 b536d0aa Michael Niedermayer
            dest_y  = s->current_picture.data[0] + (mb_y * 16* linesize  ) + mb_x * 16;
2094
            dest_cb = s->current_picture.data[1] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2095
            dest_cr = s->current_picture.data[2] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2096 3994623d Michael Niedermayer
        }
2097 de6d9b64 Fabrice Bellard
2098
        if (s->interlaced_dct) {
2099 b536d0aa Michael Niedermayer
            dct_linesize = linesize * 2;
2100
            dct_offset = linesize;
2101 de6d9b64 Fabrice Bellard
        } else {
2102 b536d0aa Michael Niedermayer
            dct_linesize = linesize;
2103
            dct_offset = linesize * 8;
2104 de6d9b64 Fabrice Bellard
        }
2105
2106
        if (!s->mb_intra) {
2107
            /* motion handling */
2108 7f2fe444 Michael Niedermayer
            /* decoding or more than one mb_type (MC was allready done otherwise) */
2109 0f440e02 Michael Niedermayer
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2110 91029be7 Michael Niedermayer
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
2111 eb4b3dd3 Zdenek Kabelac
                    op_pix = s->dsp.put_pixels_tab;
2112
                    op_qpix= s->dsp.put_qpel_pixels_tab;
2113 9dbcbd92 Michael Niedermayer
                }else{
2114 eb4b3dd3 Zdenek Kabelac
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
2115
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2116 9dbcbd92 Michael Niedermayer
                }
2117 de6d9b64 Fabrice Bellard
2118 9dbcbd92 Michael Niedermayer
                if (s->mv_dir & MV_DIR_FORWARD) {
2119 1e491e29 Michael Niedermayer
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2120 eb4b3dd3 Zdenek Kabelac
                    op_pix = s->dsp.avg_pixels_tab;
2121
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
2122 9dbcbd92 Michael Niedermayer
                }
2123
                if (s->mv_dir & MV_DIR_BACKWARD) {
2124 1e491e29 Michael Niedermayer
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2125 9dbcbd92 Michael Niedermayer
                }
2126 de6d9b64 Fabrice Bellard
            }
2127
2128 0f440e02 Michael Niedermayer
            /* skip dequant / idct if we are really late ;) */
2129 6bb925f4 Michael Niedermayer
            if(s->hurry_up>1) return;
2130 0f440e02 Michael Niedermayer
2131 de6d9b64 Fabrice Bellard
            /* add dct residue */
2132 a0201736 Michael Niedermayer
            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
2133
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
2134 0f440e02 Michael Niedermayer
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2135
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2136
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2137
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2138
2139 b50eef3a Michael Niedermayer
                if(!(s->flags&CODEC_FLAG_GRAY)){
2140 b536d0aa Michael Niedermayer
                    add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
2141
                    add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
2142 b50eef3a Michael Niedermayer
                }
2143 1457ab52 Michael Niedermayer
            } else if(s->codec_id != CODEC_ID_WMV2){
2144 0f440e02 Michael Niedermayer
                add_dct(s, block[0], 0, dest_y, dct_linesize);
2145
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2146
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2147
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2148 de6d9b64 Fabrice Bellard
2149 b50eef3a Michael Niedermayer
                if(!(s->flags&CODEC_FLAG_GRAY)){
2150 b536d0aa Michael Niedermayer
                    add_dct(s, block[4], 4, dest_cb, uvlinesize);
2151
                    add_dct(s, block[5], 5, dest_cr, uvlinesize);
2152 b50eef3a Michael Niedermayer
                }
2153 1d0d55da Michael Niedermayer
            } 
2154
#ifdef CONFIG_RISKY
2155
            else{
2156 1457ab52 Michael Niedermayer
                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2157 0f440e02 Michael Niedermayer
            }
2158 1d0d55da Michael Niedermayer
#endif
2159 de6d9b64 Fabrice Bellard
        } else {
2160
            /* dct only in intra block */
2161 a0201736 Michael Niedermayer
            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2162
                put_dct(s, block[0], 0, dest_y, dct_linesize);
2163
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2164
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2165
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2166
2167
                if(!(s->flags&CODEC_FLAG_GRAY)){
2168 b536d0aa Michael Niedermayer
                    put_dct(s, block[4], 4, dest_cb, uvlinesize);
2169
                    put_dct(s, block[5], 5, dest_cr, uvlinesize);
2170 a0201736 Michael Niedermayer
                }
2171
            }else{
2172 b0368839 Michael Niedermayer
                s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
2173
                s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
2174
                s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
2175
                s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
2176 a0201736 Michael Niedermayer
2177
                if(!(s->flags&CODEC_FLAG_GRAY)){
2178 b536d0aa Michael Niedermayer
                    s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2179
                    s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2180 a0201736 Michael Niedermayer
                }
2181 b50eef3a Michael Niedermayer
            }
2182 de6d9b64 Fabrice Bellard
        }
2183
    }
2184
}
2185
2186 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2187
2188 c9178d86 Michael Niedermayer
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2189 7f2fe444 Michael Niedermayer
{
2190
    static const char tab[64]=
2191
        {3,2,2,1,1,1,1,1,
2192
         1,1,1,1,1,1,1,1,
2193
         1,1,1,1,1,1,1,1,
2194
         0,0,0,0,0,0,0,0,
2195
         0,0,0,0,0,0,0,0,
2196
         0,0,0,0,0,0,0,0,
2197
         0,0,0,0,0,0,0,0,
2198
         0,0,0,0,0,0,0,0};
2199
    int score=0;
2200
    int run=0;
2201
    int i;
2202
    DCTELEM *block= s->block[n];
2203
    const int last_index= s->block_last_index[n];
2204 c9178d86 Michael Niedermayer
    int skip_dc;
2205 7f2fe444 Michael Niedermayer
2206 f5866a03 Michael Niedermayer
    if(threshold<0){
2207
        skip_dc=0;
2208
        threshold= -threshold;
2209 c9178d86 Michael Niedermayer
    }else
2210
        skip_dc=1;
2211 f5866a03 Michael Niedermayer
2212 7f2fe444 Michael Niedermayer
    /* are all which we could set to zero are allready zero? */
2213
    if(last_index<=skip_dc - 1) return;
2214
2215
    for(i=0; i<=last_index; i++){
2216 2ad1516a Michael Niedermayer
        const int j = s->intra_scantable.permutated[i];
2217 7f2fe444 Michael Niedermayer
        const int level = ABS(block[j]);
2218
        if(level==1){
2219
            if(skip_dc && i==0) continue;
2220
            score+= tab[run];
2221
            run=0;
2222
        }else if(level>1){
2223
            return;
2224
        }else{
2225
            run++;
2226
        }
2227
    }
2228
    if(score >= threshold) return;
2229
    for(i=skip_dc; i<=last_index; i++){
2230 2ad1516a Michael Niedermayer
        const int j = s->intra_scantable.permutated[i];
2231 7f2fe444 Michael Niedermayer
        block[j]=0;
2232
    }
2233
    if(block[0]) s->block_last_index[n]= 0;
2234
    else         s->block_last_index[n]= -1;
2235
}
2236
2237 d7e9533a Michael Niedermayer
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2238
{
2239
    int i;
2240
    const int maxlevel= s->max_qcoeff;
2241
    const int minlevel= s->min_qcoeff;
2242 2ad1516a Michael Niedermayer
    
2243
    if(s->mb_intra){
2244
        i=1; //skip clipping of intra dc
2245
    }else
2246
        i=0;
2247
    
2248
    for(;i<=last_index; i++){
2249
        const int j= s->intra_scantable.permutated[i];
2250 d7e9533a Michael Niedermayer
        int level = block[j];
2251
       
2252
        if     (level>maxlevel) level=maxlevel;
2253
        else if(level<minlevel) level=minlevel;
2254 fcb48651 Michael Niedermayer
2255 3c5ce6dc Michael Niedermayer
        block[j]= level;
2256 fcb48651 Michael Niedermayer
    }
2257
}
2258
2259
#if 0
2260 0c1a9eda Zdenek Kabelac
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2261 fcb48651 Michael Niedermayer
    int score=0;
2262
    int x,y;
2263
    
2264
    for(y=0; y<7; y++){
2265
        for(x=0; x<16; x+=4){
2266
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
2267
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2268
        }
2269
        s+= stride;
2270
    }
2271
    
2272
    return score;
2273
}
2274

2275 0c1a9eda Zdenek Kabelac
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2276 fcb48651 Michael Niedermayer
    int score=0;
2277
    int x,y;
2278
    
2279
    for(y=0; y<7; y++){
2280
        for(x=0; x<16; x++){
2281
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2282
        }
2283
        s1+= stride;
2284
        s2+= stride;
2285
    }
2286
    
2287
    return score;
2288
}
2289
#else
2290
#define SQ(a) ((a)*(a))
2291
2292 0c1a9eda Zdenek Kabelac
static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
2293 fcb48651 Michael Niedermayer
    int score=0;
2294
    int x,y;
2295
    
2296
    for(y=0; y<7; y++){
2297
        for(x=0; x<16; x+=4){
2298
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
2299
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2300
        }
2301
        s+= stride;
2302
    }
2303
    
2304
    return score;
2305
}
2306
2307 0c1a9eda Zdenek Kabelac
static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
2308 fcb48651 Michael Niedermayer
    int score=0;
2309
    int x,y;
2310
    
2311
    for(y=0; y<7; y++){
2312
        for(x=0; x<16; x++){
2313
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
2314
        }
2315
        s1+= stride;
2316
        s2+= stride;
2317
    }
2318
    
2319
    return score;
2320
}
2321
2322
#endif
2323 4d2858de Michael Niedermayer
2324 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
2325
2326 640950c7 Michael Niedermayer
/**
2327
 *
2328
 * @param h is the normal height, this will be reduced automatically if needed for the last row
2329
 */
2330
void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
2331 4d2858de Michael Niedermayer
    if (    s->avctx->draw_horiz_band 
2332 b536d0aa Michael Niedermayer
        && (s->last_picture_ptr || s->low_delay) ) {
2333 0c1a9eda Zdenek Kabelac
        uint8_t *src_ptr[3];
2334 640950c7 Michael Niedermayer
        int offset;
2335
        h= FFMIN(h, s->height - y);
2336 4d2858de Michael Niedermayer
2337 640950c7 Michael Niedermayer
        if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
2338 4d2858de Michael Niedermayer
            offset = 0;
2339
        else
2340
            offset = y * s->linesize;
2341
2342 4e00e76b Michael Niedermayer
        if(s->pict_type==B_TYPE || s->low_delay){
2343 1e491e29 Michael Niedermayer
            src_ptr[0] = s->current_picture.data[0] + offset;
2344
            src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
2345
            src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
2346 4d2858de Michael Niedermayer
        } else {
2347 1e491e29 Michael Niedermayer
            src_ptr[0] = s->last_picture.data[0] + offset;
2348
            src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
2349
            src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
2350 4d2858de Michael Niedermayer
        }
2351 0aa7875a Michael Niedermayer
        emms_c();
2352
2353 4d2858de Michael Niedermayer
        s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2354
                               y, s->width, h);
2355
    }
2356
}
2357
2358 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2359
2360 9dbcbd92 Michael Niedermayer
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2361 de6d9b64 Fabrice Bellard
{
2362 ba6802de Michael Niedermayer
    const int mb_x= s->mb_x;
2363
    const int mb_y= s->mb_y;
2364
    int i;
2365 7f2fe444 Michael Niedermayer
    int skip_dct[6];
2366 fcb48651 Michael Niedermayer
    int dct_offset   = s->linesize*8; //default for progressive frames
2367
    
2368 7f2fe444 Michael Niedermayer
    for(i=0; i<6; i++) skip_dct[i]=0;
2369 c5d309f2 Michael Niedermayer
    
2370
    if(s->adaptive_quant){
2371 1e491e29 Michael Niedermayer
        s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2372 fcb48651 Michael Niedermayer
2373
        if(s->out_format==FMT_H263){
2374 c5d309f2 Michael Niedermayer
            if     (s->dquant> 2) s->dquant= 2;
2375
            else if(s->dquant<-2) s->dquant=-2;
2376 fcb48651 Michael Niedermayer
        }
2377
            
2378
        if(s->codec_id==CODEC_ID_MPEG4){        
2379 c5d309f2 Michael Niedermayer
            if(!s->mb_intra){
2380
                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2381
2382
                if(s->mv_dir&MV_DIRECT)
2383
                    s->dquant=0;
2384
            }
2385
        }
2386
        s->qscale+= s->dquant;
2387
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2388
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2389
    }
2390 ba6802de Michael Niedermayer
2391 9dbcbd92 Michael Niedermayer
    if (s->mb_intra) {
2392 0c1a9eda Zdenek Kabelac
        uint8_t *ptr;
2393 fcb48651 Michael Niedermayer
        int wrap_y;
2394 79c0c785 Michael Niedermayer
        int emu=0;
2395 9dbcbd92 Michael Niedermayer
2396 fcb48651 Michael Niedermayer
        wrap_y = s->linesize;
2397 1e491e29 Michael Niedermayer
        ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2398 fcb48651 Michael Niedermayer
2399 79c0c785 Michael Niedermayer
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2400 1457ab52 Michael Niedermayer
            ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2401 79c0c785 Michael Niedermayer
            ptr= s->edge_emu_buffer;
2402
            emu=1;
2403
        }
2404 fcb48651 Michael Niedermayer
        
2405
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2406
            int progressive_score, interlaced_score;
2407
            
2408
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2409
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
2410
            
2411
            if(progressive_score > interlaced_score + 100){
2412
                s->interlaced_dct=1;
2413
            
2414
                dct_offset= wrap_y;
2415
                wrap_y<<=1;
2416
            }else
2417
                s->interlaced_dct=0;
2418
        }
2419
        
2420 eb4b3dd3 Zdenek Kabelac
        s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
2421
        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
2422
        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
2423
        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2424 9dbcbd92 Michael Niedermayer
2425 6282185e Michael Niedermayer
        if(s->flags&CODEC_FLAG_GRAY){
2426
            skip_dct[4]= 1;
2427
            skip_dct[5]= 1;
2428
        }else{
2429 fcb48651 Michael Niedermayer
            int wrap_c = s->uvlinesize;
2430 1e491e29 Michael Niedermayer
            ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2431 79c0c785 Michael Niedermayer
            if(emu){
2432 1457ab52 Michael Niedermayer
                ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2433 79c0c785 Michael Niedermayer
                ptr= s->edge_emu_buffer;
2434
            }
2435 eb4b3dd3 Zdenek Kabelac
            s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2436 9dbcbd92 Michael Niedermayer
2437 1e491e29 Michael Niedermayer
            ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2438 79c0c785 Michael Niedermayer
            if(emu){
2439 1457ab52 Michael Niedermayer
                ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2440 79c0c785 Michael Niedermayer
                ptr= s->edge_emu_buffer;
2441
            }
2442 eb4b3dd3 Zdenek Kabelac
            s->dsp.get_pixels(s->block[5], ptr, wrap_c);
2443 6282185e Michael Niedermayer
        }
2444 9dbcbd92 Michael Niedermayer
    }else{
2445 b3184779 Michael Niedermayer
        op_pixels_func (*op_pix)[4];
2446
        qpel_mc_func (*op_qpix)[16];
2447 0c1a9eda Zdenek Kabelac
        uint8_t *dest_y, *dest_cb, *dest_cr;
2448
        uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2449 7f2fe444 Michael Niedermayer
        int wrap_y, wrap_c;
2450 79c0c785 Michael Niedermayer
        int emu=0;
2451 9dbcbd92 Michael Niedermayer
2452 1e491e29 Michael Niedermayer
        dest_y  = s->current_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
2453
        dest_cb = s->current_picture.data[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2454
        dest_cr = s->current_picture.data[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2455 7f2fe444 Michael Niedermayer
        wrap_y = s->linesize;
2456 fcb48651 Michael Niedermayer
        wrap_c = s->uvlinesize;
2457 1e491e29 Michael Niedermayer
        ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2458
        ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2459
        ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2460 9dbcbd92 Michael Niedermayer
2461 91029be7 Michael Niedermayer
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
2462 eb4b3dd3 Zdenek Kabelac
            op_pix = s->dsp.put_pixels_tab;
2463
            op_qpix= s->dsp.put_qpel_pixels_tab;
2464 cf8039b2 Michael Niedermayer
        }else{
2465 eb4b3dd3 Zdenek Kabelac
            op_pix = s->dsp.put_no_rnd_pixels_tab;
2466
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2467 9dbcbd92 Michael Niedermayer
        }
2468
2469
        if (s->mv_dir & MV_DIR_FORWARD) {
2470 1e491e29 Michael Niedermayer
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2471 eb4b3dd3 Zdenek Kabelac
            op_pix = s->dsp.avg_pixels_tab;
2472
            op_qpix= s->dsp.avg_qpel_pixels_tab;
2473 9dbcbd92 Michael Niedermayer
        }
2474
        if (s->mv_dir & MV_DIR_BACKWARD) {
2475 1e491e29 Michael Niedermayer
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2476 ba6802de Michael Niedermayer
        }
2477 9dbcbd92 Michael Niedermayer
2478 79c0c785 Michael Niedermayer
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2479 1457ab52 Michael Niedermayer
            ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2480 79c0c785 Michael Niedermayer
            ptr_y= s->edge_emu_buffer;
2481
            emu=1;
2482
        }
2483 fcb48651 Michael Niedermayer
        
2484
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2485
            int progressive_score, interlaced_score;
2486
            
2487
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
2488
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
2489
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
2490
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
2491
            
2492
            if(progressive_score > interlaced_score + 600){
2493
                s->interlaced_dct=1;
2494
            
2495
                dct_offset= wrap_y;
2496
                wrap_y<<=1;
2497
            }else
2498
                s->interlaced_dct=0;
2499
        }
2500
        
2501 eb4b3dd3 Zdenek Kabelac
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
2502
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
2503
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
2504
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
2505 6282185e Michael Niedermayer
        
2506
        if(s->flags&CODEC_FLAG_GRAY){
2507
            skip_dct[4]= 1;
2508
            skip_dct[5]= 1;
2509
        }else{
2510 79c0c785 Michael Niedermayer
            if(emu){
2511 1457ab52 Michael Niedermayer
                ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2512 79c0c785 Michael Niedermayer
                ptr_cb= s->edge_emu_buffer;
2513
            }
2514 eb4b3dd3 Zdenek Kabelac
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2515 79c0c785 Michael Niedermayer
            if(emu){
2516 1457ab52 Michael Niedermayer
                ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2517 79c0c785 Michael Niedermayer
                ptr_cr= s->edge_emu_buffer;
2518
            }
2519 eb4b3dd3 Zdenek Kabelac
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2520 6282185e Michael Niedermayer
        }
2521 7f2fe444 Michael Niedermayer
        /* pre quantization */         
2522 1e491e29 Michael Niedermayer
        if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2523 fcb48651 Michael Niedermayer
            //FIXME optimize
2524 eb4b3dd3 Zdenek Kabelac
            if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2525
            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2526
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2527
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2528 2571e3b6 Michael Niedermayer
            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2529
            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2530 7f2fe444 Michael Niedermayer
#if 0
2531
{
2532
 static int stat[7];
2533
 int num=0;
2534
 for(i=0; i<6; i++)
2535
  if(skip_dct[i]) num++;
2536
 stat[num]++;
2537
 
2538
 if(s->mb_x==0 && s->mb_y==0){
2539
  for(i=0; i<7; i++){
2540
   printf("%6d %1d\n", stat[i], i);
2541
  }
2542
 }
2543
}
2544
#endif
2545
        }
2546 9dbcbd92 Michael Niedermayer
2547 ba6802de Michael Niedermayer
    }
2548
            
2549
#if 0
2550
            {
2551
                float adap_parm;
2552
                
2553
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2554
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2555
            
2556
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
2557
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
2558
                        s->qscale, adap_parm, s->qscale*adap_parm,
2559
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2560
            }
2561
#endif
2562
    /* DCT & quantize */
2563 d7e9533a Michael Niedermayer
    if(s->out_format==FMT_MJPEG){
2564
        for(i=0;i<6;i++) {
2565
            int overflow;
2566 28db7fce Michael Niedermayer
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2567 ef5b1b5a Juanjo
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2568 d7e9533a Michael Niedermayer
        }
2569
    }else{
2570
        for(i=0;i<6;i++) {
2571 7f2fe444 Michael Niedermayer
            if(!skip_dct[i]){
2572
                int overflow;
2573 28db7fce Michael Niedermayer
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2574 d7e9533a Michael Niedermayer
            // FIXME we could decide to change to quantizer instead of clipping
2575 ef5b1b5a Juanjo
            // JS: I don't think that would be a good idea it could lower quality instead
2576
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
2577 7f2fe444 Michael Niedermayer
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2578
            }else
2579
                s->block_last_index[i]= -1;
2580 d7e9533a Michael Niedermayer
        }
2581 7f2fe444 Michael Niedermayer
        if(s->luma_elim_threshold && !s->mb_intra)
2582
            for(i=0; i<4; i++)
2583 c9178d86 Michael Niedermayer
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2584 7f2fe444 Michael Niedermayer
        if(s->chroma_elim_threshold && !s->mb_intra)
2585
            for(i=4; i<6; i++)
2586 c9178d86 Michael Niedermayer
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2587 ba6802de Michael Niedermayer
    }
2588
2589 6282185e Michael Niedermayer
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2590
        s->block_last_index[4]=
2591
        s->block_last_index[5]= 0;
2592
        s->block[4][0]=
2593 1d0eab1d Michael Niedermayer
        s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
2594 6282185e Michael Niedermayer
    }
2595
2596 ba6802de Michael Niedermayer
    /* huffman encode */
2597 1457ab52 Michael Niedermayer
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2598
    case CODEC_ID_MPEG1VIDEO:
2599
        mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
2600 1d0d55da Michael Niedermayer
#ifdef CONFIG_RISKY
2601 1457ab52 Michael Niedermayer
    case CODEC_ID_MPEG4:
2602
        mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2603
    case CODEC_ID_MSMPEG4V2:
2604
    case CODEC_ID_MSMPEG4V3:
2605
    case CODEC_ID_WMV1:
2606
        msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2607
    case CODEC_ID_WMV2:
2608
         ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
2609
    case CODEC_ID_H263:
2610
    case CODEC_ID_H263P:
2611
    case CODEC_ID_RV10:
2612
        h263_encode_mb(s, s->block, motion_x, motion_y); break;
2613 1d0d55da Michael Niedermayer
#endif
2614
    case CODEC_ID_MJPEG:
2615
        mjpeg_encode_mb(s, s->block); break;
2616 1457ab52 Michael Niedermayer
    default:
2617
        assert(0);
2618 ba6802de Michael Niedermayer
    }
2619
}
2620
2621 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
2622
2623 b7a22d84 Michael Niedermayer
/**
2624
 * combines the (truncated) bitstream to a complete frame
2625
 * @returns -1 if no complete frame could be created
2626
 */
2627
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
2628
    ParseContext *pc= &s->parse_context;
2629
        
2630
    pc->last_index= pc->index;
2631
2632
    if(next==-1){
2633
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2634
2635
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
2636
        pc->index += *buf_size;
2637
        return -1;
2638
    }
2639
2640
    if(pc->index){
2641
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2642
2643
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
2644
        pc->index = 0;
2645
        *buf= pc->buffer;
2646
        *buf_size= pc->last_index + next;
2647
    }
2648
2649
    return 0;
2650
}
2651
2652 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2653 0c1a9eda Zdenek Kabelac
void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
2654 ba6802de Michael Niedermayer
{
2655 1f0cd30f Michael Niedermayer
    int bytes= length>>4;
2656
    int bits= length&15;
2657
    int i;
2658
2659 7f2fe444 Michael Niedermayer
    if(length==0) return;
2660
2661 1f0cd30f Michael Niedermayer
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2662
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2663 ba6802de Michael Niedermayer
}
2664
2665 7f2fe444 Michael Niedermayer
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2666 1f0cd30f Michael Niedermayer
    int i;
2667
2668
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2669
2670
    /* mpeg1 */
2671
    d->mb_incr= s->mb_incr;
2672
    for(i=0; i<3; i++)
2673
        d->last_dc[i]= s->last_dc[i];
2674
    
2675
    /* statistics */
2676
    d->mv_bits= s->mv_bits;
2677
    d->i_tex_bits= s->i_tex_bits;
2678
    d->p_tex_bits= s->p_tex_bits;
2679
    d->i_count= s->i_count;
2680 66370d3f Michael Niedermayer
    d->f_count= s->f_count;
2681
    d->b_count= s->b_count;
2682 1f0cd30f Michael Niedermayer
    d->skip_count= s->skip_count;
2683
    d->misc_bits= s->misc_bits;
2684 8b4c7dbc Michael Niedermayer
    d->last_bits= 0;
2685 91029be7 Michael Niedermayer
2686
    d->mb_skiped= s->mb_skiped;
2687 5f3ed165 Michael Niedermayer
    d->qscale= s->qscale;
2688 1f0cd30f Michael Niedermayer
}
2689
2690 7f2fe444 Michael Niedermayer
static inline