Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263.c @ c009df3f

History | View | Annotate | Download (167 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 6dbd39fe Juanjo
 * H263+ support.
5 de6d9b64 Fabrice Bellard
 * Copyright (c) 2001 Juan J. Sierralta P.
6
 *
7 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 45870f57 Michael Niedermayer
 *
21 66370d3f Michael Niedermayer
 * ac prediction encoding, b-frame support, error resilience, optimizations,
22
 * qpel decoding, gmc decoding, interlaced decoding, 
23
 * by Michael Niedermayer <michaelni@gmx.at>
24 de6d9b64 Fabrice Bellard
 */
25 2ba8f6b8 Michael Niedermayer
26
/**
27
 * @file h263.c
28 983e3246 Michael Niedermayer
 * h263/mpeg4 codec.
29 2ba8f6b8 Michael Niedermayer
 */
30 1e209222 Juanjo
 
31
//#define DEBUG
32 de6d9b64 Fabrice Bellard
#include "common.h"
33
#include "dsputil.h"
34
#include "avcodec.h"
35
#include "mpegvideo.h"
36
#include "h263data.h"
37
#include "mpeg4data.h"
38
39 c5d309f2 Michael Niedermayer
//#undef NDEBUG
40
//#include <assert.h>
41
42 edcf9890 Michael Niedermayer
#define INTRA_MCBPC_VLC_BITS 6
43
#define INTER_MCBPC_VLC_BITS 6
44
#define CBPY_VLC_BITS 6
45
#define MV_VLC_BITS 9
46
#define DC_VLC_BITS 9
47
#define SPRITE_TRAJ_VLC_BITS 6
48
#define MB_TYPE_B_VLC_BITS 4
49
#define TEX_VLC_BITS 9
50
51 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
52 de6d9b64 Fabrice Bellard
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
53
                              int n);
54 9dbcbd92 Michael Niedermayer
static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
55 6dbd39fe Juanjo
static void h263p_encode_umotion(MpegEncContext * s, int val);
56 3db48188 Michael Niedermayer
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
57 0c1a9eda Zdenek Kabelac
                               int n, int dc, uint8_t *scan_table, 
58 92073d01 Michael Niedermayer
                               PutBitContext *dc_pb, PutBitContext *ac_pb);
59 bbed9259 Michael Niedermayer
#endif
60
61 6f91bcd1 Michael Niedermayer
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
62 6dbd39fe Juanjo
static int h263p_decode_umotion(MpegEncContext * s, int pred);
63 de6d9b64 Fabrice Bellard
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
64
                             int n, int coded);
65 92073d01 Michael Niedermayer
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
66
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
67 a4e8b587 Michael Niedermayer
                              int n, int coded, int intra, int rvlc);
68 0c1a9eda Zdenek Kabelac
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);
69 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
70 0e15384d Michael Niedermayer
static void mpeg4_inv_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
71 d6231b9e Michael Niedermayer
                              int dir);
72 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
73 49092244 Michael Niedermayer
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
74 0c1a9eda Zdenek Kabelac
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr);
75 4d2858de Michael Niedermayer
76 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
77 0c1a9eda Zdenek Kabelac
static uint8_t uni_DCtab_lum_len[512];
78
static uint8_t uni_DCtab_chrom_len[512];
79
static uint16_t uni_DCtab_lum_bits[512];
80
static uint16_t uni_DCtab_chrom_bits[512];
81 bbed9259 Michael Niedermayer
82 30952237 Michael Niedermayer
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
83 0c1a9eda Zdenek Kabelac
static uint8_t fcode_tab[MAX_MV*2+1];
84
static uint8_t umv_fcode_tab[MAX_MV*2+1];
85 45870f57 Michael Niedermayer
86 477ab036 Michael Niedermayer
static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
87
static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];
88
static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
89
static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];
90
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
91 3db48188 Michael Niedermayer
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
92 477ab036 Michael Niedermayer
#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
93 3db48188 Michael Niedermayer
94
/* mpeg4
95
inter
96
max level: 24/6
97
max run: 53/63
98

99
intra
100
max level: 53/16
101
max run: 29/41
102
*/
103 bbed9259 Michael Niedermayer
#endif
104
105 6d71b382 Michael Niedermayer
#if 0 //3IV1 is quite rare and tis slows things down a tiny bit
106
#define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")
107
#else 
108
#define IS_3IV1 0
109
#endif
110 2a164b25 Michael Niedermayer
111 de6d9b64 Fabrice Bellard
int h263_get_picture_format(int width, int height)
112
{
113
    int format;
114
115
    if (width == 128 && height == 96)
116 1e209222 Juanjo
        format = 1;
117 de6d9b64 Fabrice Bellard
    else if (width == 176 && height == 144)
118 1e209222 Juanjo
        format = 2;
119 de6d9b64 Fabrice Bellard
    else if (width == 352 && height == 288)
120 1e209222 Juanjo
        format = 3;
121 de6d9b64 Fabrice Bellard
    else if (width == 704 && height == 576)
122 1e209222 Juanjo
        format = 4;
123 de6d9b64 Fabrice Bellard
    else if (width == 1408 && height == 1152)
124 1e209222 Juanjo
        format = 5;
125 de6d9b64 Fabrice Bellard
    else
126
        format = 7;
127
    return format;
128
}
129
130 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
131
132 58b0b0dd Michael Niedermayer
static void float_aspect_to_info(MpegEncContext * s, float aspect){
133
    int i;
134
135
    aspect*= s->height/(double)s->width;
136
//printf("%f\n", aspect);
137 5d3cea3a Michael Niedermayer
    
138 58b0b0dd Michael Niedermayer
    if(aspect==0) aspect= 1.0;
139
140 5d3cea3a Michael Niedermayer
    ff_float2fraction(&s->aspected_width, &s->aspected_height, aspect, 255);
141 58b0b0dd Michael Niedermayer
142
//printf("%d %d\n", s->aspected_width, s->aspected_height);
143
    for(i=1; i<6; i++){
144
        if(s->aspected_width == pixel_aspect[i][0] && s->aspected_height== pixel_aspect[i][1]){
145
            s->aspect_ratio_info=i;
146
            return;
147
        }
148
    }
149 5d3cea3a Michael Niedermayer
    
150 58b0b0dd Michael Niedermayer
    s->aspect_ratio_info= FF_ASPECT_EXTENDED;
151 5d3cea3a Michael Niedermayer
}
152
153 de6d9b64 Fabrice Bellard
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
154
{
155 6dbd39fe Juanjo
    int format;
156 de6d9b64 Fabrice Bellard
157
    align_put_bits(&s->pb);
158 81401c1f Juanjo
159
    /* Update the pointer to last GOB */
160 17592475 Michael Niedermayer
    s->ptr_lastgob = pbBufPtr(&s->pb);
161 81401c1f Juanjo
    s->gob_number = 0;
162
163
    put_bits(&s->pb, 22, 0x20); /* PSC */
164 14bea432 Michael Niedermayer
    put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) / 
165
                         s->avctx->frame_rate) & 0xff);
166 de6d9b64 Fabrice Bellard
167
    put_bits(&s->pb, 1, 1);        /* marker */
168
    put_bits(&s->pb, 1, 0);        /* h263 id */
169
    put_bits(&s->pb, 1, 0);        /* split screen off */
170
    put_bits(&s->pb, 1, 0);        /* camera  off */
171
    put_bits(&s->pb, 1, 0);        /* freeze picture release off */
172 6dbd39fe Juanjo
    
173
    format = h263_get_picture_format(s->width, s->height);
174 de6d9b64 Fabrice Bellard
    if (!s->h263_plus) {
175
        /* H.263v1 */
176
        put_bits(&s->pb, 3, format);
177
        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
178
        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
179
        of H.263v1 UMV implies to check the predicted MV after
180
        calculation of the current MB to see if we're on the limits */
181
        put_bits(&s->pb, 1, 0);        /* unrestricted motion vector: off */
182
        put_bits(&s->pb, 1, 0);        /* SAC: off */
183
        put_bits(&s->pb, 1, 0);        /* advanced prediction mode: off */
184
        put_bits(&s->pb, 1, 0);        /* not PB frame */
185
        put_bits(&s->pb, 5, s->qscale);
186
        put_bits(&s->pb, 1, 0);        /* Continuous Presence Multipoint mode: off */
187
    } else {
188
        /* H.263v2 */
189
        /* H.263 Plus PTYPE */
190
        put_bits(&s->pb, 3, 7);
191
        put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
192 6dbd39fe Juanjo
        if (format == 7)
193
            put_bits(&s->pb,3,6); /* Custom Source Format */
194
        else
195
            put_bits(&s->pb, 3, format);
196
            
197 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* Custom PCF: off */
198 e51d6d27 Michael Niedermayer
        s->umvplus = s->unrestricted_mv;
199 544286b3 Juanjo
        put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
200 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* SAC: off */
201
        put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
202 ef5b1b5a Juanjo
        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
203 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* Deblocking Filter: off */
204
        put_bits(&s->pb,1,0); /* Slice Structured: off */
205
        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
206
        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
207
        put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
208
        put_bits(&s->pb,1,0); /* Modified Quantization: off */
209
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
210
        put_bits(&s->pb,3,0); /* Reserved */
211
                
212
        put_bits(&s->pb, 3, s->pict_type == P_TYPE);
213
                
214
        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
215
        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
216 ef5b1b5a Juanjo
        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
217 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,2,0); /* Reserved */
218
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
219
                
220
        /* This should be here if PLUSPTYPE */
221
        put_bits(&s->pb, 1, 0);        /* Continuous Presence Multipoint mode: off */
222
                
223 6dbd39fe Juanjo
                if (format == 7) {
224
            /* Custom Picture Format (CPFMT) */
225 58b0b0dd Michael Niedermayer
            float_aspect_to_info(s, s->avctx->aspect_ratio);
226 5d3cea3a Michael Niedermayer
227
            put_bits(&s->pb,4,s->aspect_ratio_info);
228 6dbd39fe Juanjo
            put_bits(&s->pb,9,(s->width >> 2) - 1);
229
            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
230
            put_bits(&s->pb,9,(s->height >> 2));
231 050fe8ba Alex Beregszaszi
            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
232
            {
233
                put_bits(&s->pb, 8, s->aspected_width);
234
                put_bits(&s->pb, 8, s->aspected_height);
235
            }
236 6dbd39fe Juanjo
        }
237
        
238 de6d9b64 Fabrice Bellard
        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
239 544286b3 Juanjo
        if (s->umvplus)
240 e51d6d27 Michael Niedermayer
//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
241
            put_bits(&s->pb,2,1); /* unlimited */
242
243 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 5, s->qscale);
244
    }
245
246
    put_bits(&s->pb, 1, 0);        /* no PEI */
247 8f8402e4 Michael Niedermayer
248
    if(s->h263_aic){
249
         s->y_dc_scale_table= 
250
         s->c_dc_scale_table= h263_aic_dc_scale_table;
251
    }else{
252
        s->y_dc_scale_table=
253
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
254
    }
255 de6d9b64 Fabrice Bellard
}
256
257 945eeee1 Michael Niedermayer
/**
258
 * Encodes a group of blocks header.
259
 */
260 644d98a4 Juanjo
int h263_encode_gob_header(MpegEncContext * s, int mb_line)
261
{
262
           align_put_bits(&s->pb);
263 81401c1f Juanjo
           flush_put_bits(&s->pb);
264
           /* Call the RTP callback to send the last GOB */
265
           if (s->rtp_callback) {
266 945eeee1 Michael Niedermayer
               int pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
267 81401c1f Juanjo
               s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
268
           }
269 644d98a4 Juanjo
           put_bits(&s->pb, 17, 1); /* GBSC */
270 81401c1f Juanjo
           s->gob_number = mb_line / s->gob_index;
271 644d98a4 Juanjo
           put_bits(&s->pb, 5, s->gob_number); /* GN */
272 81401c1f Juanjo
           put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
273 644d98a4 Juanjo
           put_bits(&s->pb, 5, s->qscale); /* GQUANT */
274 81401c1f Juanjo
           //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
275 945eeee1 Michael Niedermayer
    return 0;
276 644d98a4 Juanjo
}
277 d6231b9e Michael Niedermayer
278
static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6])
279
{
280
    int score0=0, score1=0;
281
    int i, n;
282 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
283 d6231b9e Michael Niedermayer
284
    for(n=0; n<6; n++){
285 0c1a9eda Zdenek Kabelac
        int16_t *ac_val, *ac_val1;
286 d6231b9e Michael Niedermayer
287 4278e7a6 Michael Niedermayer
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
288 d6231b9e Michael Niedermayer
        ac_val1= ac_val;
289
        if(dir[n]){
290 7bc9090a Michael Niedermayer
            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
291 99609036 Michael Niedermayer
            /* top prediction */
292 4278e7a6 Michael Niedermayer
            ac_val-= s->block_wrap[n]*16;
293 1e491e29 Michael Niedermayer
            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
294 99609036 Michael Niedermayer
                /* same qscale */
295
                for(i=1; i<8; i++){
296 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
297 99609036 Michael Niedermayer
                    score0+= ABS(level);
298
                    score1+= ABS(level - ac_val[i+8]);
299 b0368839 Michael Niedermayer
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
300 99609036 Michael Niedermayer
                    ac_val1[i+8]= level;
301
                }
302
            }else{
303
                /* different qscale, we must rescale */
304
                for(i=1; i<8; i++){
305 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
306 99609036 Michael Niedermayer
                    score0+= ABS(level);
307 1e491e29 Michael Niedermayer
                    score1+= ABS(level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale));
308 b0368839 Michael Niedermayer
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
309 99609036 Michael Niedermayer
                    ac_val1[i+8]= level;
310
                }
311 d6231b9e Michael Niedermayer
            }
312
        }else{
313 7bc9090a Michael Niedermayer
            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
314 99609036 Michael Niedermayer
            /* left prediction */
315 d6231b9e Michael Niedermayer
            ac_val-= 16;
316 1e491e29 Michael Niedermayer
            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
317 99609036 Michael Niedermayer
                /* same qscale */
318
                for(i=1; i<8; i++){
319 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
320 99609036 Michael Niedermayer
                    score0+= ABS(level);
321
                    score1+= ABS(level - ac_val[i]);
322
                    ac_val1[i  ]= level;
323 b0368839 Michael Niedermayer
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
324 99609036 Michael Niedermayer
                }
325
            }else{
326
                /* different qscale, we must rescale */
327
                for(i=1; i<8; i++){
328 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
329 99609036 Michael Niedermayer
                    score0+= ABS(level);
330 1e491e29 Michael Niedermayer
                    score1+= ABS(level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale));
331 99609036 Michael Niedermayer
                    ac_val1[i  ]= level;
332 b0368839 Michael Niedermayer
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
333 99609036 Michael Niedermayer
                }
334 d6231b9e Michael Niedermayer
            }
335
        }
336
    }
337
338
    return score0 > score1 ? 1 : 0;    
339
}
340
341 4d2858de Michael Niedermayer
/**
342
 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
343
 */
344 9e4c6948 Michael Niedermayer
void ff_clean_h263_qscales(MpegEncContext *s){
345 c5d309f2 Michael Niedermayer
    int i;
346 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
347 c5d309f2 Michael Niedermayer
    
348
    for(i=1; i<s->mb_num; i++){
349 7bc9090a Michael Niedermayer
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
350
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
351 c5d309f2 Michael Niedermayer
    }
352
    for(i=s->mb_num-2; i>=0; i--){
353 7bc9090a Michael Niedermayer
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
354
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
355 c5d309f2 Michael Niedermayer
    }
356 9e4c6948 Michael Niedermayer
}
357
358 4d2858de Michael Niedermayer
/**
359
 * modify mb_type & qscale so that encoding is acually possible in mpeg4
360
 */
361 9e4c6948 Michael Niedermayer
void ff_clean_mpeg4_qscales(MpegEncContext *s){
362
    int i;
363 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
364
365 9e4c6948 Michael Niedermayer
    ff_clean_h263_qscales(s);
366 c5d309f2 Michael Niedermayer
    
367
    for(i=1; i<s->mb_num; i++){
368 7bc9090a Michael Niedermayer
        int mb_xy= s->mb_index2xy[i];
369
    
370
        if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_INTER4V)){
371
            s->mb_type[mb_xy]&= ~MB_TYPE_INTER4V;
372
            s->mb_type[mb_xy]|= MB_TYPE_INTER;
373 c5d309f2 Michael Niedermayer
        }
374
    }
375
376
    if(s->pict_type== B_TYPE){
377
        int odd=0;
378
        /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than
379
           for the actual adaptive quantization */
380
        
381
        for(i=0; i<s->mb_num; i++){
382 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
383
            odd += qscale_table[mb_xy]&1;
384 c5d309f2 Michael Niedermayer
        }
385
        
386
        if(2*odd > s->mb_num) odd=1;
387
        else                  odd=0;
388
        
389
        for(i=0; i<s->mb_num; i++){
390 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
391
            if((qscale_table[mb_xy]&1) != odd)
392
                qscale_table[mb_xy]++;
393
            if(qscale_table[mb_xy] > 31)
394
                qscale_table[mb_xy]= 31;
395 c5d309f2 Michael Niedermayer
        }            
396
    
397
        for(i=1; i<s->mb_num; i++){
398 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
399
            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_DIRECT)){
400
                s->mb_type[mb_xy]&= ~MB_TYPE_DIRECT;
401
                s->mb_type[mb_xy]|= MB_TYPE_BIDIR;
402 c5d309f2 Michael Niedermayer
            }
403
        }
404
    }
405
}
406
407 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
408 7bc9090a Michael Niedermayer
/**
409
 *
410
 * @return the mb_type
411
 */
412
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
413
    const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
414
    const int colocated_mb_type= s->next_picture.mb_type[mb_index]; //FIXME or next?
415 1457ab52 Michael Niedermayer
    int xy= s->block_index[0];
416
    uint16_t time_pp= s->pp_time;
417
    uint16_t time_pb= s->pb_time;
418
    int i;
419 c40c3482 Michael Niedermayer
    
420 1457ab52 Michael Niedermayer
    //FIXME avoid divides
421 7bc9090a Michael Niedermayer
    
422
    if(IS_8X8(colocated_mb_type)){
423 1457ab52 Michael Niedermayer
        s->mv_type = MV_TYPE_8X8;
424
        for(i=0; i<4; i++){
425
            xy= s->block_index[i];
426
            s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
427
            s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
428
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0]
429
                                : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
430
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1] 
431
                                : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
432
        }
433 7bc9090a Michael Niedermayer
        return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
434
    } else if(IS_INTERLACED(colocated_mb_type)){
435 1457ab52 Michael Niedermayer
        s->mv_type = MV_TYPE_FIELD;
436
        for(i=0; i<2; i++){
437
            if(s->top_field_first){
438
                time_pp= s->pp_field_time - s->field_select_table[mb_index][i] + i;
439
                time_pb= s->pb_field_time - s->field_select_table[mb_index][i] + i;
440
            }else{
441
                time_pp= s->pp_field_time + s->field_select_table[mb_index][i] - i;
442
                time_pb= s->pb_field_time + s->field_select_table[mb_index][i] - i;
443
            }
444
            s->mv[0][i][0] = s->field_mv_table[mb_index][i][0]*time_pb/time_pp + mx;
445
            s->mv[0][i][1] = s->field_mv_table[mb_index][i][1]*time_pb/time_pp + my;
446
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->field_mv_table[mb_index][i][0]
447
                                : s->field_mv_table[mb_index][i][0]*(time_pb - time_pp)/time_pp;
448
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->field_mv_table[mb_index][i][1] 
449
                                : s->field_mv_table[mb_index][i][1]*(time_pb - time_pp)/time_pp;
450
        }
451 7bc9090a Michael Niedermayer
        return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
452
    }else{
453
        s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
454
        s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
455
        s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
456
                            : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
457
        s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] 
458
                            : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
459
        if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
460
            s->mv_type= MV_TYPE_16X16;
461
        else
462
            s->mv_type= MV_TYPE_8X8;
463
        return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
464 1457ab52 Michael Niedermayer
    }
465
}
466
467 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
468 d6231b9e Michael Niedermayer
void mpeg4_encode_mb(MpegEncContext * s,
469
                    DCTELEM block[6][64],
470
                    int motion_x, int motion_y)
471
{
472 5c91a675 Zdenek Kabelac
    int cbpc, cbpy, pred_x, pred_y;
473 098eefe1 Michael Niedermayer
    int bits;
474 92073d01 Michael Niedermayer
    PutBitContext * const pb2    = s->data_partitioning                         ? &s->pb2    : &s->pb;
475
    PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
476
    PutBitContext * const dc_pb  = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2    : &s->pb;
477
    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
478 c5d309f2 Michael Niedermayer
    const int dquant_code[5]= {1,0,9,2,3};
479 644d98a4 Juanjo
    
480 d6231b9e Michael Niedermayer
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
481
    if (!s->mb_intra) {
482
        /* compute cbp */
483 5c91a675 Zdenek Kabelac
        int i, cbp = 0;
484 d6231b9e Michael Niedermayer
        for (i = 0; i < 6; i++) {
485 9dbcbd92 Michael Niedermayer
            if (s->block_last_index[i] >= 0)
486
                cbp |= 1 << (5 - i);
487 d6231b9e Michael Niedermayer
        }
488 9dbcbd92 Michael Niedermayer
489
        if(s->pict_type==B_TYPE){
490
            static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
491
            int mb_type=  mb_type_table[s->mv_dir];
492
            
493
            if(s->mb_x==0){
494
                s->last_mv[0][0][0]= 
495
                s->last_mv[0][0][1]= 
496
                s->last_mv[1][0][0]= 
497
                s->last_mv[1][0][1]= 0;
498
            }
499 c5d309f2 Michael Niedermayer
            
500
            assert(s->dquant>=-2 && s->dquant<=2);
501
            assert((s->dquant&1)==0);
502
            assert(mb_type>=0);
503 9dbcbd92 Michael Niedermayer
504
            /* nothing to do if this MB was skiped in the next P Frame */
505 7bc9090a Michael Niedermayer
            if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
506 9dbcbd92 Michael Niedermayer
                s->skip_count++;
507
                s->mv[0][0][0]= 
508
                s->mv[0][0][1]= 
509
                s->mv[1][0][0]= 
510
                s->mv[1][0][1]= 0;
511 91029be7 Michael Niedermayer
                s->mv_dir= MV_DIR_FORWARD; //doesnt matter
512 c5d309f2 Michael Niedermayer
                s->qscale -= s->dquant;
513 1e491e29 Michael Niedermayer
//                s->mb_skiped=1;
514
515 9dbcbd92 Michael Niedermayer
                return;
516
            }
517 1457ab52 Michael Niedermayer
            
518 9dbcbd92 Michael Niedermayer
            if ((cbp | motion_x | motion_y | mb_type) ==0) {
519
                /* direct MB with MV={0,0} */
520 c5d309f2 Michael Niedermayer
                assert(s->dquant==0);
521
                
522 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
523 92073d01 Michael Niedermayer
524
                if(interleaved_stats){
525
                    s->misc_bits++;
526
                    s->last_bits++;
527
                }
528 9dbcbd92 Michael Niedermayer
                s->skip_count++;
529
                return;
530
            }
531 1e491e29 Michael Niedermayer
            
532 9dbcbd92 Michael Niedermayer
            put_bits(&s->pb, 1, 0);        /* mb coded modb1=0 */
533
            put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
534
            put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
535
            if(cbp) put_bits(&s->pb, 6, cbp);
536
            
537 c5d309f2 Michael Niedermayer
            if(cbp && mb_type){
538
                if(s->dquant)
539
                    put_bits(&s->pb, 2, (s->dquant>>2)+3);
540
                else
541
                    put_bits(&s->pb, 1, 0);
542
            }else
543
                s->qscale -= s->dquant;
544 fcb48651 Michael Niedermayer
            
545
            if(!s->progressive_sequence){
546
                if(cbp)
547
                    put_bits(&s->pb, 1, s->interlaced_dct);
548
                if(mb_type) // not diect mode
549
                    put_bits(&s->pb, 1, 0); // no interlaced ME yet
550
            }
551 9dbcbd92 Michael Niedermayer
552 92073d01 Michael Niedermayer
            if(interleaved_stats){
553 4d2a4834 Michael Niedermayer
                s->misc_bits+= get_bits_diff(s);
554 92073d01 Michael Niedermayer
            }
555 cf8039b2 Michael Niedermayer
556 9dbcbd92 Michael Niedermayer
            switch(mb_type)
557
            {
558
            case 0: /* direct */
559
                h263_encode_motion(s, motion_x, 1);
560
                h263_encode_motion(s, motion_y, 1);                
561 66370d3f Michael Niedermayer
                s->b_count++;
562
                s->f_count++;
563 9dbcbd92 Michael Niedermayer
                break;
564
            case 1: /* bidir */
565
                h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
566
                h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
567
                h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
568
                h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
569
                s->last_mv[0][0][0]= s->mv[0][0][0];
570
                s->last_mv[0][0][1]= s->mv[0][0][1];
571
                s->last_mv[1][0][0]= s->mv[1][0][0];
572
                s->last_mv[1][0][1]= s->mv[1][0][1];
573 66370d3f Michael Niedermayer
                s->b_count++;
574
                s->f_count++;
575 9dbcbd92 Michael Niedermayer
                break;
576
            case 2: /* backward */
577
                h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
578
                h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
579
                s->last_mv[1][0][0]= motion_x;
580
                s->last_mv[1][0][1]= motion_y;
581 66370d3f Michael Niedermayer
                s->b_count++;
582 9dbcbd92 Michael Niedermayer
                break;
583
            case 3: /* forward */
584
                h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
585
                h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
586
                s->last_mv[0][0][0]= motion_x;
587
                s->last_mv[0][0][1]= motion_y;
588 66370d3f Michael Niedermayer
                s->f_count++;
589 9dbcbd92 Michael Niedermayer
                break;
590 91029be7 Michael Niedermayer
            default:
591
                printf("unknown mb type\n");
592 9dbcbd92 Michael Niedermayer
                return;
593
            }
594 92073d01 Michael Niedermayer
595
            if(interleaved_stats){
596 4d2a4834 Michael Niedermayer
                s->mv_bits+= get_bits_diff(s);
597 92073d01 Michael Niedermayer
            }
598 d6231b9e Michael Niedermayer
599 9dbcbd92 Michael Niedermayer
            /* encode each block */
600
            for (i = 0; i < 6; i++) {
601 2ad1516a Michael Niedermayer
                mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, &s->pb);
602 92073d01 Michael Niedermayer
            }
603
604
            if(interleaved_stats){
605 4d2a4834 Michael Niedermayer
                s->p_tex_bits+= get_bits_diff(s);
606 9dbcbd92 Michael Niedermayer
            }
607
        }else{ /* s->pict_type==B_TYPE */
608 c5d309f2 Michael Niedermayer
            if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
609 ee6f7861 Michael Niedermayer
                /* check if the B frames can skip it too, as we must skip it if we skip here 
610
                   why didnt they just compress the skip-mb bits instead of reusing them ?! */
611
                if(s->max_b_frames>0){
612
                    int i;
613 e71a4f74 Michael Niedermayer
                    int x,y, offset;
614
                    uint8_t *p_pic;
615
616
                    x= s->mb_x*16;
617
                    y= s->mb_y*16;
618
                    if(x+16 > s->width)  x= s->width-16;
619
                    if(y+16 > s->height) y= s->height-16;
620
621
                    offset= x + y*s->linesize;
622 1e491e29 Michael Niedermayer
                    p_pic= s->new_picture.data[0] + offset;
623 e71a4f74 Michael Niedermayer
                    
624 ee6f7861 Michael Niedermayer
                    s->mb_skiped=1;
625
                    for(i=0; i<s->max_b_frames; i++){
626 d697b89b Michael Niedermayer
                        uint8_t *b_pic;
627
                        int diff;
628 1e491e29 Michael Niedermayer
                        Picture *pic= s->reordered_input_picture[i+1];
629 d697b89b Michael Niedermayer
630 1e491e29 Michael Niedermayer
                        if(pic==NULL || pic->pict_type!=B_TYPE) break;
631 d697b89b Michael Niedermayer
632 1e491e29 Michael Niedermayer
                        b_pic= pic->data[0] + offset + 16; //FIXME +16
633 eb4b3dd3 Zdenek Kabelac
                        diff= s->dsp.pix_abs16x16(p_pic, b_pic, s->linesize);
634 e71a4f74 Michael Niedermayer
                        if(diff>s->qscale*70){ //FIXME check that 70 is optimal
635 ee6f7861 Michael Niedermayer
                            s->mb_skiped=0;
636
                            break;
637
                        }
638
                    }
639
                }else
640
                    s->mb_skiped=1; 
641
642
                if(s->mb_skiped==1){
643
                    /* skip macroblock */
644
                    put_bits(&s->pb, 1, 1);
645 92073d01 Michael Niedermayer
646
                    if(interleaved_stats){
647
                        s->misc_bits++;
648
                        s->last_bits++;
649
                    }
650 ee6f7861 Michael Niedermayer
                    s->skip_count++;
651 6d71b382 Michael Niedermayer
                    
652 ee6f7861 Michael Niedermayer
                    return;
653
                }
654 9dbcbd92 Michael Niedermayer
            }
655 e71a4f74 Michael Niedermayer
656 9dbcbd92 Michael Niedermayer
            put_bits(&s->pb, 1, 0);        /* mb coded */
657
            if(s->mv_type==MV_TYPE_16X16){
658
                cbpc = cbp & 3;
659 c5d309f2 Michael Niedermayer
                if(s->dquant) cbpc+= 8;
660 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb,
661
                        inter_MCBPC_bits[cbpc],
662
                        inter_MCBPC_code[cbpc]);
663 c5d309f2 Michael Niedermayer
664 9dbcbd92 Michael Niedermayer
                cbpy = cbp >> 2;
665
                cbpy ^= 0xf;
666 92073d01 Michael Niedermayer
                put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
667 c5d309f2 Michael Niedermayer
                if(s->dquant)
668
                    put_bits(pb2, 2, dquant_code[s->dquant+2]);
669 fcb48651 Michael Niedermayer
670
                if(!s->progressive_sequence){
671
                    if(cbp)
672
                        put_bits(pb2, 1, s->interlaced_dct);
673
                    put_bits(pb2, 1, 0); // no interlaced ME yet
674
                }
675 9dbcbd92 Michael Niedermayer
                    
676 92073d01 Michael Niedermayer
                if(interleaved_stats){
677 4d2a4834 Michael Niedermayer
                    s->misc_bits+= get_bits_diff(s);
678 92073d01 Michael Niedermayer
                }
679 d6231b9e Michael Niedermayer
680 9dbcbd92 Michael Niedermayer
                /* motion vectors: 16x16 mode */
681
                h263_pred_motion(s, 0, &pred_x, &pred_y);
682
            
683
                h263_encode_motion(s, motion_x - pred_x, s->f_code);
684
                h263_encode_motion(s, motion_y - pred_y, s->f_code);
685
            }else{
686
                cbpc = (cbp & 3)+16;
687
                put_bits(&s->pb,
688
                        inter_MCBPC_bits[cbpc],
689
                        inter_MCBPC_code[cbpc]);
690
                cbpy = cbp >> 2;
691
                cbpy ^= 0xf;
692 92073d01 Michael Niedermayer
                put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
693 9dbcbd92 Michael Niedermayer
694 49943a18 Michael Niedermayer
                if(!s->progressive_sequence){
695
                    if(cbp)
696
                        put_bits(pb2, 1, s->interlaced_dct);
697
                }
698
    
699 92073d01 Michael Niedermayer
                if(interleaved_stats){
700 4d2a4834 Michael Niedermayer
                    s->misc_bits+= get_bits_diff(s);
701 92073d01 Michael Niedermayer
                }
702 9dbcbd92 Michael Niedermayer
703
                for(i=0; i<4; i++){
704
                    /* motion vectors: 8x8 mode*/
705
                    h263_pred_motion(s, i, &pred_x, &pred_y);
706
707
                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
708
                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
709
                }
710 cf8039b2 Michael Niedermayer
            }
711 92073d01 Michael Niedermayer
712
            if(interleaved_stats){ 
713 4d2a4834 Michael Niedermayer
                s->mv_bits+= get_bits_diff(s);
714 92073d01 Michael Niedermayer
            }
715 098eefe1 Michael Niedermayer
716 9dbcbd92 Michael Niedermayer
            /* encode each block */
717
            for (i = 0; i < 6; i++) {
718 2ad1516a Michael Niedermayer
                mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, tex_pb);
719 92073d01 Michael Niedermayer
            }
720
721
            if(interleaved_stats){
722 4d2a4834 Michael Niedermayer
                s->p_tex_bits+= get_bits_diff(s);
723 9dbcbd92 Michael Niedermayer
            }
724 66370d3f Michael Niedermayer
            s->f_count++;
725 d6231b9e Michael Niedermayer
        }
726
    } else {
727 9dbcbd92 Michael Niedermayer
        int cbp;
728 d6231b9e Michael Niedermayer
        int dc_diff[6];   //dc values with the dc prediction subtracted 
729
        int dir[6];  //prediction direction
730
        int zigzag_last_index[6];
731 0c1a9eda Zdenek Kabelac
        uint8_t *scan_table[6];
732 5c91a675 Zdenek Kabelac
        int i;
733 d6231b9e Michael Niedermayer
734
        for(i=0; i<6; i++){
735
            const int level= block[i][0];
736 0c1a9eda Zdenek Kabelac
            uint16_t *dc_ptr;
737 d6231b9e Michael Niedermayer
738 8f8402e4 Michael Niedermayer
            dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
739 d6231b9e Michael Niedermayer
            if (i < 4) {
740
                *dc_ptr = level * s->y_dc_scale;
741
            } else {
742
                *dc_ptr = level * s->c_dc_scale;
743
            }
744
        }
745
746
        s->ac_pred= decide_ac_pred(s, block, dir);
747
748
        if(s->ac_pred){
749
            for(i=0; i<6; i++){
750 0c1a9eda Zdenek Kabelac
                uint8_t *st;
751 d6231b9e Michael Niedermayer
                int last_index;
752
753
                mpeg4_inv_pred_ac(s, block[i], i, dir[i]);
754 2ad1516a Michael Niedermayer
                if (dir[i]==0) st = s->intra_v_scantable.permutated; /* left */
755
                else           st = s->intra_h_scantable.permutated; /* top */
756 d6231b9e Michael Niedermayer
757
                for(last_index=63; last_index>=0; last_index--) //FIXME optimize
758
                    if(block[i][st[last_index]]) break;
759
                zigzag_last_index[i]= s->block_last_index[i];
760
                s->block_last_index[i]= last_index;
761
                scan_table[i]= st;
762
            }
763
        }else{
764
            for(i=0; i<6; i++)
765 2ad1516a Michael Niedermayer
                scan_table[i]= s->intra_scantable.permutated;
766 d6231b9e Michael Niedermayer
        }
767
768
        /* compute cbp */
769
        cbp = 0;
770
        for (i = 0; i < 6; i++) {
771
            if (s->block_last_index[i] >= 1)
772
                cbp |= 1 << (5 - i);
773
        }
774
775
        cbpc = cbp & 3;
776
        if (s->pict_type == I_TYPE) {
777 c5d309f2 Michael Niedermayer
            if(s->dquant) cbpc+=4;
778 d6231b9e Michael Niedermayer
            put_bits(&s->pb,
779
                intra_MCBPC_bits[cbpc],
780
                intra_MCBPC_code[cbpc]);
781
        } else {
782 c5d309f2 Michael Niedermayer
            if(s->dquant) cbpc+=8;
783 d6231b9e Michael Niedermayer
            put_bits(&s->pb, 1, 0);        /* mb coded */
784
            put_bits(&s->pb,
785
                inter_MCBPC_bits[cbpc + 4],
786
                inter_MCBPC_code[cbpc + 4]);
787
        }
788 92073d01 Michael Niedermayer
        put_bits(pb2, 1, s->ac_pred);
789 d6231b9e Michael Niedermayer
        cbpy = cbp >> 2;
790 92073d01 Michael Niedermayer
        put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
791 c5d309f2 Michael Niedermayer
        if(s->dquant)
792
            put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
793 d6231b9e Michael Niedermayer
794 fcb48651 Michael Niedermayer
        if(!s->progressive_sequence){
795
            put_bits(dc_pb, 1, s->interlaced_dct);
796
        }
797
798 92073d01 Michael Niedermayer
        if(interleaved_stats){
799 4d2a4834 Michael Niedermayer
            s->misc_bits+= get_bits_diff(s);
800 92073d01 Michael Niedermayer
        }
801 098eefe1 Michael Niedermayer
802 d6231b9e Michael Niedermayer
        /* encode each block */
803
        for (i = 0; i < 6; i++) {
804 92073d01 Michael Niedermayer
            mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
805 d6231b9e Michael Niedermayer
        }
806
807 92073d01 Michael Niedermayer
        if(interleaved_stats){
808 4d2a4834 Michael Niedermayer
            s->i_tex_bits+= get_bits_diff(s);
809 92073d01 Michael Niedermayer
        }
810 098eefe1 Michael Niedermayer
        s->i_count++;
811
812 d6231b9e Michael Niedermayer
        /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
813
        if(s->ac_pred){
814
            for(i=0; i<6; i++){
815
                int j;    
816 0c1a9eda Zdenek Kabelac
                int16_t *ac_val;
817 d6231b9e Michael Niedermayer
818 4278e7a6 Michael Niedermayer
                ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
819 d6231b9e Michael Niedermayer
820
                if(dir[i]){
821
                    for(j=1; j<8; j++) 
822 b0368839 Michael Niedermayer
                        block[i][s->dsp.idct_permutation[j   ]]= ac_val[j+8];
823 d6231b9e Michael Niedermayer
                }else{
824
                    for(j=1; j<8; j++) 
825 b0368839 Michael Niedermayer
                        block[i][s->dsp.idct_permutation[j<<3]]= ac_val[j  ];
826 d6231b9e Michael Niedermayer
                }
827
                s->block_last_index[i]= zigzag_last_index[i];
828
            }
829
        }
830
    }
831
}
832
833 de6d9b64 Fabrice Bellard
void h263_encode_mb(MpegEncContext * s,
834
                    DCTELEM block[6][64],
835
                    int motion_x, int motion_y)
836
{
837
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
838 0c1a9eda Zdenek Kabelac
    int16_t pred_dc;
839
    int16_t rec_intradc[6];
840
    uint16_t *dc_ptr[6];
841 9e4c6948 Michael Niedermayer
    const int dquant_code[5]= {1,0,9,2,3};
842 ef5b1b5a Juanjo
           
843
    //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
844 9dbcbd92 Michael Niedermayer
    if (!s->mb_intra) {
845 ef5b1b5a Juanjo
        /* compute cbp */
846 9dbcbd92 Michael Niedermayer
        cbp = 0;
847
        for (i = 0; i < 6; i++) {
848
            if (s->block_last_index[i] >= 0)
849
                cbp |= 1 << (5 - i);
850
        }
851 9e4c6948 Michael Niedermayer
        if ((cbp | motion_x | motion_y | s->dquant) == 0) {
852 9dbcbd92 Michael Niedermayer
            /* skip macroblock */
853
            put_bits(&s->pb, 1, 1);
854
            return;
855
        }
856 ef5b1b5a Juanjo
        put_bits(&s->pb, 1, 0);        /* mb coded */
857 9dbcbd92 Michael Niedermayer
        cbpc = cbp & 3;
858 9e4c6948 Michael Niedermayer
        if(s->dquant) cbpc+= 8;
859 9dbcbd92 Michael Niedermayer
        put_bits(&s->pb,
860 ef5b1b5a Juanjo
                    inter_MCBPC_bits[cbpc],
861
                    inter_MCBPC_code[cbpc]);
862 9dbcbd92 Michael Niedermayer
        cbpy = cbp >> 2;
863
        cbpy ^= 0xf;
864
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
865 9e4c6948 Michael Niedermayer
        if(s->dquant)
866
            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
867 9dbcbd92 Michael Niedermayer
868 ef5b1b5a Juanjo
        /* motion vectors: 16x16 mode only now */
869 9dbcbd92 Michael Niedermayer
        h263_pred_motion(s, 0, &pred_x, &pred_y);
870 6dbd39fe Juanjo
      
871 9dbcbd92 Michael Niedermayer
        if (!s->umvplus) {  
872
            h263_encode_motion(s, motion_x - pred_x, s->f_code);
873
            h263_encode_motion(s, motion_y - pred_y, s->f_code);
874
        }
875
        else {
876
            h263p_encode_umotion(s, motion_x - pred_x);
877
            h263p_encode_umotion(s, motion_y - pred_y);
878
            if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
879 ef5b1b5a Juanjo
                /* To prevent Start Code emulation */
880 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb,1,1);
881
        }
882 ef5b1b5a Juanjo
    } else {
883
        int li = s->h263_aic ? 0 : 1;
884
        
885
        cbp = 0;
886
        for(i=0; i<6; i++) {
887
            /* Predict DC */
888
            if (s->h263_aic && s->mb_intra) {
889 0c1a9eda Zdenek Kabelac
                int16_t level = block[i][0];
890 ef5b1b5a Juanjo
            
891
                pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
892
                level -= pred_dc;
893
                /* Quant */
894
                if (level < 0)
895
                    level = (level + (s->qscale >> 1))/(s->y_dc_scale);
896
                else
897
                    level = (level - (s->qscale >> 1))/(s->y_dc_scale);
898
                    
899
                /* AIC can change CBP */
900
                if (level == 0 && s->block_last_index[i] == 0)
901
                    s->block_last_index[i] = -1;
902
                else if (level < -127)
903
                    level = -127;
904
                else if (level > 127)
905
                    level = 127;
906
                
907
                block[i][0] = level;
908
                /* Reconstruction */ 
909
                rec_intradc[i] = (s->y_dc_scale*level) + pred_dc;
910
                /* Oddify */
911
                rec_intradc[i] |= 1;
912
                //if ((rec_intradc[i] % 2) == 0)
913
                //    rec_intradc[i]++;
914
                /* Clipping */
915
                if (rec_intradc[i] < 0)
916
                    rec_intradc[i] = 0;
917
                else if (rec_intradc[i] > 2047)
918
                    rec_intradc[i] = 2047;
919
                                
920
                /* Update AC/DC tables */
921
                *dc_ptr[i] = rec_intradc[i];
922
            }
923
            /* compute cbp */
924
            if (s->block_last_index[i] >= li)
925
                cbp |= 1 << (5 - i);
926
        }
927 de6d9b64 Fabrice Bellard
928 ef5b1b5a Juanjo
        cbpc = cbp & 3;
929
        if (s->pict_type == I_TYPE) {
930 9e4c6948 Michael Niedermayer
            if(s->dquant) cbpc+=4;
931 ef5b1b5a Juanjo
            put_bits(&s->pb,
932
                intra_MCBPC_bits[cbpc],
933
                intra_MCBPC_code[cbpc]);
934
        } else {
935 9e4c6948 Michael Niedermayer
            if(s->dquant) cbpc+=8;
936 ef5b1b5a Juanjo
            put_bits(&s->pb, 1, 0);        /* mb coded */
937
            put_bits(&s->pb,
938
                inter_MCBPC_bits[cbpc + 4],
939
                inter_MCBPC_code[cbpc + 4]);
940
        }
941
        if (s->h263_aic) {
942
            /* XXX: currently, we do not try to use ac prediction */
943
            put_bits(&s->pb, 1, 0);        /* no AC prediction */
944
        }
945
        cbpy = cbp >> 2;
946
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
947 9e4c6948 Michael Niedermayer
        if(s->dquant)
948
            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
949 de6d9b64 Fabrice Bellard
    }
950
951 ef5b1b5a Juanjo
    for(i=0; i<6; i++) {
952
        /* encode each block */
953 ba6802de Michael Niedermayer
        h263_encode_block(s, block[i], i);
954 ef5b1b5a Juanjo
    
955
        /* Update INTRADC for decoding */
956
        if (s->h263_aic && s->mb_intra) {
957
            block[i][0] = rec_intradc[i];
958
            
959
        }
960
    }
961
}
962 bbed9259 Michael Niedermayer
#endif
963 ef5b1b5a Juanjo
964 0c1a9eda Zdenek Kabelac
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr)
965 ef5b1b5a Juanjo
{
966
    int x, y, wrap, a, c, pred_dc, scale;
967 0c1a9eda Zdenek Kabelac
    int16_t *dc_val, *ac_val;
968 ef5b1b5a Juanjo
969
    /* find prediction */
970
    if (n < 4) {
971
        x = 2 * s->mb_x + 1 + (n & 1);
972
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
973
        wrap = s->mb_width * 2 + 2;
974
        dc_val = s->dc_val[0];
975
        ac_val = s->ac_val[0][0];
976
        scale = s->y_dc_scale;
977
    } else {
978
        x = s->mb_x + 1;
979
        y = s->mb_y + 1;
980
        wrap = s->mb_width + 2;
981
        dc_val = s->dc_val[n - 4 + 1];
982
        ac_val = s->ac_val[n - 4 + 1][0];
983
        scale = s->c_dc_scale;
984 de6d9b64 Fabrice Bellard
    }
985 ef5b1b5a Juanjo
    /* B C
986
     * A X 
987
     */
988
    a = dc_val[(x - 1) + (y) * wrap];
989
    c = dc_val[(x) + (y - 1) * wrap];
990
    
991
    /* No prediction outside GOB boundary */
992 92073d01 Michael Niedermayer
    if (s->first_slice_line && ((n < 2) || (n > 3)))
993 ef5b1b5a Juanjo
        c = 1024;
994
    pred_dc = 1024;
995
    /* just DC prediction */
996
    if (a != 1024 && c != 1024)
997
        pred_dc = (a + c) >> 1;
998
    else if (a != 1024)
999
        pred_dc = a;
1000
    else
1001
        pred_dc = c;
1002
    
1003
    /* we assume pred is positive */
1004
    //pred_dc = (pred_dc + (scale >> 1)) / scale;
1005
    *dc_val_ptr = &dc_val[x + y * wrap];
1006
    return pred_dc;
1007 de6d9b64 Fabrice Bellard
}
1008
1009 5c91a675 Zdenek Kabelac
static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
1010 d140623f Juanjo
{
1011 9e15ad28 Juanjo
    int x, y, wrap, a, c, pred_dc, scale, i;
1012 0c1a9eda Zdenek Kabelac
    int16_t *dc_val, *ac_val, *ac_val1;
1013 d140623f Juanjo
1014
    /* find prediction */
1015
    if (n < 4) {
1016
        x = 2 * s->mb_x + 1 + (n & 1);
1017
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
1018
        wrap = s->mb_width * 2 + 2;
1019
        dc_val = s->dc_val[0];
1020 9e15ad28 Juanjo
        ac_val = s->ac_val[0][0];
1021 d140623f Juanjo
        scale = s->y_dc_scale;
1022
    } else {
1023
        x = s->mb_x + 1;
1024
        y = s->mb_y + 1;
1025
        wrap = s->mb_width + 2;
1026
        dc_val = s->dc_val[n - 4 + 1];
1027 9e15ad28 Juanjo
        ac_val = s->ac_val[n - 4 + 1][0];
1028 d140623f Juanjo
        scale = s->c_dc_scale;
1029
    }
1030 9e15ad28 Juanjo
    
1031
    ac_val += ((y) * wrap + (x)) * 16;
1032
    ac_val1 = ac_val;
1033
    
1034 d140623f Juanjo
    /* B C
1035
     * A X 
1036
     */
1037
    a = dc_val[(x - 1) + (y) * wrap];
1038
    c = dc_val[(x) + (y - 1) * wrap];
1039
    
1040 ef5b1b5a Juanjo
    /* No prediction outside GOB boundary */
1041 92073d01 Michael Niedermayer
    if (s->first_slice_line && ((n < 2) || (n > 3)))
1042 ef5b1b5a Juanjo
        c = 1024;
1043 9e15ad28 Juanjo
    pred_dc = 1024;
1044 d140623f Juanjo
    if (s->ac_pred) {
1045
        if (s->h263_aic_dir) {
1046
            /* left prediction */
1047 9e15ad28 Juanjo
            if (a != 1024) {
1048
                ac_val -= 16;
1049
                for(i=1;i<8;i++) {
1050 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
1051 9e15ad28 Juanjo
                }
1052
                pred_dc = a;
1053 d140623f Juanjo
            }
1054
        } else {
1055
            /* top prediction */
1056 9e15ad28 Juanjo
            if (c != 1024) {
1057
                ac_val -= 16 * wrap;
1058
                for(i=1;i<8;i++) {
1059 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i   ]] += ac_val[i + 8];
1060 9e15ad28 Juanjo
                }
1061
                pred_dc = c;
1062 d140623f Juanjo
            }
1063
        }
1064 9e15ad28 Juanjo
    } else {
1065
        /* just DC prediction */
1066
        if (a != 1024 && c != 1024)
1067
            pred_dc = (a + c) >> 1;
1068
        else if (a != 1024)
1069
            pred_dc = a;
1070
        else
1071
            pred_dc = c;
1072 d140623f Juanjo
    }
1073 9e15ad28 Juanjo
    
1074
    /* we assume pred is positive */
1075
    block[0]=block[0]*scale + pred_dc;
1076
    
1077
    if (block[0] < 0)
1078
        block[0] = 0;
1079
    else if (!(block[0] & 1))
1080
        block[0]++;
1081
    
1082
    /* Update AC/DC tables */
1083
    dc_val[(x) + (y) * wrap] = block[0];
1084
    
1085 d140623f Juanjo
    /* left copy */
1086
    for(i=1;i<8;i++)
1087 b0368839 Michael Niedermayer
        ac_val1[i    ] = block[s->dsp.idct_permutation[i<<3]];
1088 d140623f Juanjo
    /* top copy */
1089
    for(i=1;i<8;i++)
1090 b0368839 Michael Niedermayer
        ac_val1[8 + i] = block[s->dsp.idct_permutation[i   ]];
1091 d140623f Juanjo
}
1092
1093 0c1a9eda Zdenek Kabelac
int16_t *h263_pred_motion(MpegEncContext * s, int block, 
1094 de6d9b64 Fabrice Bellard
                        int *px, int *py)
1095
{
1096 4278e7a6 Michael Niedermayer
    int xy, wrap;
1097 0c1a9eda Zdenek Kabelac
    int16_t *A, *B, *C, *mot_val;
1098 49c9325f Michael Niedermayer
    static const int off[4]= {2, 1, 1, -1};
1099 de6d9b64 Fabrice Bellard
1100 4278e7a6 Michael Niedermayer
    wrap = s->block_wrap[0];
1101
    xy = s->block_index[block];
1102 de6d9b64 Fabrice Bellard
1103 af8793ae Zdenek Kabelac
    mot_val = s->motion_val[xy];
1104 de6d9b64 Fabrice Bellard
1105 92073d01 Michael Niedermayer
    A = s->motion_val[xy - 1];
1106
    /* special case for first (slice) line */
1107 4d2858de Michael Niedermayer
    if (s->first_slice_line && block<3) {
1108 92073d01 Michael Niedermayer
        // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
1109
        // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1110
        if(block==0){ //most common case
1111
            if(s->mb_x  == s->resync_mb_x){ //rare
1112
                *px= *py = 0;
1113
            }else if(s->mb_x + 1 == s->resync_mb_x){ //rare
1114
                C = s->motion_val[xy + off[block] - wrap];
1115
                if(s->mb_x==0){
1116
                    *px = C[0];
1117
                    *py = C[1];
1118
                }else{
1119
                    *px = mid_pred(A[0], 0, C[0]);
1120
                    *py = mid_pred(A[1], 0, C[1]);
1121
                }
1122
            }else{
1123
                *px = A[0];
1124
                *py = A[1];
1125
            }
1126
        }else if(block==1){
1127
            if(s->mb_x + 1 == s->resync_mb_x){ //rare
1128
                C = s->motion_val[xy + off[block] - wrap];
1129
                *px = mid_pred(A[0], 0, C[0]);
1130
                *py = mid_pred(A[1], 0, C[1]);
1131
            }else{
1132
                *px = A[0];
1133
                *py = A[1];
1134
            }
1135
        }else{ /* block==2*/
1136
            B = s->motion_val[xy - wrap];
1137
            C = s->motion_val[xy + off[block] - wrap];
1138
            if(s->mb_x == s->resync_mb_x) //rare
1139
                A[0]=A[1]=0;
1140
    
1141
            *px = mid_pred(A[0], B[0], C[0]);
1142
            *py = mid_pred(A[1], B[1], C[1]);
1143
        }
1144 de6d9b64 Fabrice Bellard
    } else {
1145 49c9325f Michael Niedermayer
        B = s->motion_val[xy - wrap];
1146
        C = s->motion_val[xy + off[block] - wrap];
1147 de6d9b64 Fabrice Bellard
        *px = mid_pred(A[0], B[0], C[0]);
1148
        *py = mid_pred(A[1], B[1], C[1]);
1149
    }
1150
    return mot_val;
1151
}
1152
1153 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
1154 9dbcbd92 Michael Niedermayer
static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
1155 de6d9b64 Fabrice Bellard
{
1156 a9c68306 Michael Niedermayer
    int range, l, bit_size, sign, code, bits;
1157 de6d9b64 Fabrice Bellard
1158
    if (val == 0) {
1159
        /* zero vector */
1160
        code = 0;
1161
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1162
    } else {
1163 9dbcbd92 Michael Niedermayer
        bit_size = f_code - 1;
1164 de6d9b64 Fabrice Bellard
        range = 1 << bit_size;
1165
        /* modulo encoding */
1166
        l = range * 32;
1167 a9c68306 Michael Niedermayer
#if 1
1168
        val+= l;
1169
        val&= 2*l-1;
1170
        val-= l;
1171
        sign = val>>31;
1172
        val= (val^sign)-sign;
1173
        sign&=1;
1174
#else
1175 de6d9b64 Fabrice Bellard
        if (val < -l) {
1176 a9c68306 Michael Niedermayer
            val += 2*l;
1177 de6d9b64 Fabrice Bellard
        } else if (val >= l) {
1178 a9c68306 Michael Niedermayer
            val -= 2*l;
1179 de6d9b64 Fabrice Bellard
        }
1180 a9c68306 Michael Niedermayer
1181 c5d309f2 Michael Niedermayer
        assert(val>=-l && val<l);
1182 de6d9b64 Fabrice Bellard
1183
        if (val >= 0) {
1184
            sign = 0;
1185
        } else {
1186
            val = -val;
1187
            sign = 1;
1188
        }
1189 a9c68306 Michael Niedermayer
#endif
1190 3bf43d42 Michael Niedermayer
        val--;
1191
        code = (val >> bit_size) + 1;
1192
        bits = val & (range - 1);
1193 de6d9b64 Fabrice Bellard
1194
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1195
        if (bit_size > 0) {
1196
            put_bits(&s->pb, bit_size, bits);
1197
        }
1198
    }
1199 3a3d3a42 Michael Niedermayer
1200 de6d9b64 Fabrice Bellard
}
1201
1202 6dbd39fe Juanjo
/* Encode MV differences on H.263+ with Unrestricted MV mode */
1203
static void h263p_encode_umotion(MpegEncContext * s, int val)
1204
{
1205
    short sval = 0; 
1206
    short i = 0;
1207
    short n_bits = 0;
1208
    short temp_val;
1209
    int code = 0;
1210
    int tcode;
1211
    
1212
    if ( val == 0)
1213
        put_bits(&s->pb, 1, 1);
1214
    else if (val == 1)
1215
        put_bits(&s->pb, 3, 0);
1216
    else if (val == -1)
1217
        put_bits(&s->pb, 3, 2);
1218
    else {
1219
        
1220
        sval = ((val < 0) ? (short)(-val):(short)val);
1221
        temp_val = sval;
1222
        
1223
        while (temp_val != 0) {
1224
            temp_val = temp_val >> 1;
1225
            n_bits++;
1226
        }
1227
        
1228
        i = n_bits - 1;
1229
        while (i > 0) {
1230
            tcode = (sval & (1 << (i-1))) >> (i-1);
1231
            tcode = (tcode << 1) | 1;
1232
            code = (code << 2) | tcode;
1233
            i--;
1234
        }
1235
        code = ((code << 1) | (val < 0)) << 1;
1236
        put_bits(&s->pb, (2*n_bits)+1, code);
1237
        //printf("\nVal = %d\tCode = %d", sval, code);
1238
    }
1239
}
1240
1241 45870f57 Michael Niedermayer
static void init_mv_penalty_and_fcode(MpegEncContext *s)
1242
{
1243
    int f_code;
1244
    int mv;
1245 bbed9259 Michael Niedermayer
    
1246
    if(mv_penalty==NULL)
1247 30952237 Michael Niedermayer
        mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
1248 bbed9259 Michael Niedermayer
    
1249 45870f57 Michael Niedermayer
    for(f_code=1; f_code<=MAX_FCODE; f_code++){
1250
        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1251
            int len;
1252
1253
            if(mv==0) len= mvtab[0][1];
1254
            else{
1255
                int val, bit_size, range, code;
1256
1257
                bit_size = s->f_code - 1;
1258
                range = 1 << bit_size;
1259
1260
                val=mv;
1261
                if (val < 0) 
1262
                    val = -val;
1263
                val--;
1264
                code = (val >> bit_size) + 1;
1265
                if(code<33){
1266
                    len= mvtab[code][1] + 1 + bit_size;
1267
                }else{
1268
                    len= mvtab[32][1] + 2 + bit_size;
1269
                }
1270
            }
1271
1272
            mv_penalty[f_code][mv+MAX_MV]= len;
1273
        }
1274
    }
1275
1276
    for(f_code=MAX_FCODE; f_code>0; f_code--){
1277
        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1278
            fcode_tab[mv+MAX_MV]= f_code;
1279
        }
1280
    }
1281 9d2a0355 Michael Niedermayer
1282
    for(mv=0; mv<MAX_MV*2+1; mv++){
1283
        umv_fcode_tab[mv]= 1;
1284
    }
1285 45870f57 Michael Niedermayer
}
1286 bbed9259 Michael Niedermayer
#endif
1287 45870f57 Michael Niedermayer
1288 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1289
1290 20695ec9 Falk H├╝ffner
static void init_uni_dc_tab(void)
1291 2a164b25 Michael Niedermayer
{
1292
    int level, uni_code, uni_len;
1293
1294 3bf43d42 Michael Niedermayer
    for(level=-256; level<256; level++){
1295 2a164b25 Michael Niedermayer
        int size, v, l;
1296
        /* find number of bits */
1297
        size = 0;
1298
        v = abs(level);
1299
        while (v) {
1300
            v >>= 1;
1301
            size++;
1302
        }
1303
1304
        if (level < 0)
1305
            l= (-level) ^ ((1 << size) - 1);
1306
        else
1307
            l= level;
1308
1309
        /* luminance */
1310
        uni_code= DCtab_lum[size][0];
1311
        uni_len = DCtab_lum[size][1];
1312
1313
        if (size > 0) {
1314
            uni_code<<=size; uni_code|=l;
1315
            uni_len+=size;
1316
            if (size > 8){
1317
                uni_code<<=1; uni_code|=1;
1318
                uni_len++;
1319
            }
1320
        }
1321 67725183 Michael Niedermayer
        uni_DCtab_lum_bits[level+256]= uni_code;
1322
        uni_DCtab_lum_len [level+256]= uni_len;
1323 2a164b25 Michael Niedermayer
1324
        /* chrominance */
1325
        uni_code= DCtab_chrom[size][0];
1326
        uni_len = DCtab_chrom[size][1];
1327
        
1328
        if (size > 0) {
1329
            uni_code<<=size; uni_code|=l;
1330
            uni_len+=size;
1331
            if (size > 8){
1332
                uni_code<<=1; uni_code|=1;
1333
                uni_len++;
1334
            }
1335
        }
1336 67725183 Michael Niedermayer
        uni_DCtab_chrom_bits[level+256]= uni_code;
1337
        uni_DCtab_chrom_len [level+256]= uni_len;
1338 2a164b25 Michael Niedermayer
1339
    }
1340
}
1341
1342 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1343
1344 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
1345 0c1a9eda Zdenek Kabelac
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1346 3db48188 Michael Niedermayer
    int slevel, run, last;
1347
    
1348
    assert(MAX_LEVEL >= 64);
1349
    assert(MAX_RUN   >= 63);
1350
1351
    for(slevel=-64; slevel<64; slevel++){
1352
        if(slevel==0) continue;
1353
        for(run=0; run<64; run++){
1354
            for(last=0; last<=1; last++){
1355
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1356
                int level= slevel < 0 ? -slevel : slevel;
1357
                int sign= slevel < 0 ? 1 : 0;
1358
                int bits, len, code;
1359
                int level1, run1;
1360
                
1361
                len_tab[index]= 100;
1362
                     
1363
                /* ESC0 */
1364
                code= get_rl_index(rl, last, run, level);
1365
                bits= rl->table_vlc[code][0];
1366
                len=  rl->table_vlc[code][1];
1367
                bits=bits*2+sign; len++;
1368
                
1369
                if(code!=rl->n && len < len_tab[index]){
1370
                    bits_tab[index]= bits;
1371
                    len_tab [index]= len;
1372
                }
1373
#if 1
1374
                /* ESC1 */
1375
                bits= rl->table_vlc[rl->n][0];
1376
                len=  rl->table_vlc[rl->n][1];
1377
                bits=bits*2;    len++; //esc1
1378
                level1= level - rl->max_level[last][run];
1379
                if(level1>0){
1380
                    code= get_rl_index(rl, last, run, level1);
1381
                    bits<<= rl->table_vlc[code][1];
1382
                    len  += rl->table_vlc[code][1];
1383
                    bits += rl->table_vlc[code][0];
1384
                    bits=bits*2+sign; len++;
1385
                
1386
                    if(code!=rl->n && len < len_tab[index]){
1387
                        bits_tab[index]= bits;
1388
                        len_tab [index]= len;
1389
                    }
1390
                }
1391
#endif 
1392
#if 1
1393
                /* ESC2 */
1394
                bits= rl->table_vlc[rl->n][0];
1395
                len=  rl->table_vlc[rl->n][1];
1396
                bits=bits*4+2;    len+=2; //esc2
1397
                run1 = run - rl->max_run[last][level] - 1;
1398
                if(run1>=0){
1399
                    code= get_rl_index(rl, last, run1, level);
1400
                    bits<<= rl->table_vlc[code][1];
1401
                    len  += rl->table_vlc[code][1];
1402
                    bits += rl->table_vlc[code][0];
1403
                    bits=bits*2+sign; len++;
1404
                
1405
                    if(code!=rl->n && len < len_tab[index]){
1406
                        bits_tab[index]= bits;
1407
                        len_tab [index]= len;
1408
                    }
1409
                }
1410
#endif           
1411
                /* ESC3 */        
1412
                bits= rl->table_vlc[rl->n][0];
1413
                len = rl->table_vlc[rl->n][1];
1414
                bits=bits*4+3;    len+=2; //esc3
1415
                bits=bits*2+last; len++;
1416
                bits=bits*64+run; len+=6;
1417
                bits=bits*2+1;    len++;  //marker
1418
                bits=bits*4096+(slevel&0xfff); len+=12;
1419
                bits=bits*2+1;    len++;  //marker
1420
                
1421
                if(len < len_tab[index]){
1422
                    bits_tab[index]= bits;
1423
                    len_tab [index]= len;
1424
                }
1425
            }
1426
        }
1427
    }
1428
}
1429
1430 45870f57 Michael Niedermayer
void h263_encode_init(MpegEncContext *s)
1431 de6d9b64 Fabrice Bellard
{
1432
    static int done = 0;
1433
1434
    if (!done) {
1435
        done = 1;
1436 2a164b25 Michael Niedermayer
1437
        init_uni_dc_tab();
1438
1439 de6d9b64 Fabrice Bellard
        init_rl(&rl_inter);
1440
        init_rl(&rl_intra);
1441 ef5b1b5a Juanjo
        init_rl(&rl_intra_aic);
1442 3db48188 Michael Niedermayer
        
1443
        init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1444
        init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1445 45870f57 Michael Niedermayer
1446
        init_mv_penalty_and_fcode(s);
1447 de6d9b64 Fabrice Bellard
    }
1448 1457ab52 Michael Niedermayer
    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1449 45870f57 Michael Niedermayer
    
1450 9d2a0355 Michael Niedermayer
    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1451 d7e9533a Michael Niedermayer
    switch(s->codec_id){
1452
    case CODEC_ID_MPEG4:
1453
        s->fcode_tab= fcode_tab;
1454
        s->min_qcoeff= -2048;
1455
        s->max_qcoeff=  2047;
1456 477ab036 Michael Niedermayer
        s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
1457
        s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
1458
        s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
1459
        s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1460 67725183 Michael Niedermayer
        s->luma_dc_vlc_length= uni_DCtab_lum_len;
1461
        s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
1462 477ab036 Michael Niedermayer
        s->ac_esc_length= 7+2+1+6+1+12+1;
1463 d7e9533a Michael Niedermayer
        break;
1464
    case CODEC_ID_H263P:
1465
        s->fcode_tab= umv_fcode_tab;
1466 e51d6d27 Michael Niedermayer
        s->min_qcoeff= -127;
1467 d7e9533a Michael Niedermayer
        s->max_qcoeff=  127;
1468
        break;
1469 8f8402e4 Michael Niedermayer
        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later 
1470 d7e9533a Michael Niedermayer
    default: //nothing needed default table allready set in mpegvideo.c
1471 e51d6d27 Michael Niedermayer
        s->min_qcoeff= -127;
1472 d7e9533a Michael Niedermayer
        s->max_qcoeff=  127;
1473 8f8402e4 Michael Niedermayer
        s->y_dc_scale_table=
1474
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1475 d7e9533a Michael Niedermayer
    }
1476 de6d9b64 Fabrice Bellard
}
1477
1478 2ba8f6b8 Michael Niedermayer
/**
1479
 * encodes a 8x8 block.
1480
 * @param block the 8x8 block
1481
 * @param n block index (0-3 are luma, 4-5 are chroma)
1482
 */
1483 de6d9b64 Fabrice Bellard
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1484
{
1485 ef5b1b5a Juanjo
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1486
    RLTable *rl;
1487 de6d9b64 Fabrice Bellard
1488 ef5b1b5a Juanjo
    rl = &rl_inter;
1489
    if (s->mb_intra && !s->h263_aic) {
1490 81401c1f Juanjo
        /* DC coef */
1491 e51d6d27 Michael Niedermayer
        level = block[0];
1492 de6d9b64 Fabrice Bellard
        /* 255 cannot be represented, so we clamp */
1493
        if (level > 254) {
1494
            level = 254;
1495
            block[0] = 254;
1496
        }
1497 81401c1f Juanjo
        /* 0 cannot be represented also */
1498 e51d6d27 Michael Niedermayer
        else if (level < 1) {
1499 81401c1f Juanjo
            level = 1;
1500
            block[0] = 1;
1501
        }
1502 17938602 Michael Niedermayer
        if (level == 128) //FIXME check rv10
1503 e51d6d27 Michael Niedermayer
            put_bits(&s->pb, 8, 0xff);
1504
        else
1505
            put_bits(&s->pb, 8, level & 0xff);
1506
        i = 1;
1507 de6d9b64 Fabrice Bellard
    } else {
1508 e51d6d27 Michael Niedermayer
        i = 0;
1509
        if (s->h263_aic && s->mb_intra)
1510
            rl = &rl_intra_aic;
1511 de6d9b64 Fabrice Bellard
    }
1512 ef5b1b5a Juanjo
   
1513 de6d9b64 Fabrice Bellard
    /* AC coefs */
1514
    last_index = s->block_last_index[n];
1515
    last_non_zero = i - 1;
1516
    for (; i <= last_index; i++) {
1517 2ad1516a Michael Niedermayer
        j = s->intra_scantable.permutated[i];
1518 ef5b1b5a Juanjo
        level = block[j];
1519
        if (level) {
1520
            run = i - last_non_zero - 1;
1521
            last = (i == last_index);
1522
            sign = 0;
1523
            slevel = level;
1524
            if (level < 0) {
1525
                sign = 1;
1526
                level = -level;
1527
            }
1528 de6d9b64 Fabrice Bellard
            code = get_rl_index(rl, last, run, level);
1529
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1530
            if (code == rl->n) {
1531
                put_bits(&s->pb, 1, last);
1532
                put_bits(&s->pb, 6, run);
1533 e51d6d27 Michael Niedermayer
                
1534
                assert(slevel != 0);
1535
1536
                if(slevel < 128 && slevel > -128) 
1537
                    put_bits(&s->pb, 8, slevel & 0xff);
1538
                else{
1539
                    put_bits(&s->pb, 8, 128);
1540
                    put_bits(&s->pb, 5, slevel & 0x1f);
1541
                    put_bits(&s->pb, 6, (slevel>>5)&0x3f);
1542
                }
1543 de6d9b64 Fabrice Bellard
            } else {
1544
                put_bits(&s->pb, 1, sign);
1545
            }
1546 ef5b1b5a Juanjo
                last_non_zero = i;
1547
            }
1548 de6d9b64 Fabrice Bellard
    }
1549
}
1550 bbed9259 Michael Niedermayer
#endif
1551 de6d9b64 Fabrice Bellard
1552 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1553
1554 de6d9b64 Fabrice Bellard
/***************************************************/
1555 4d2858de Michael Niedermayer
/**
1556
 * add mpeg4 stuffing bits (01...1)
1557
 */
1558 92073d01 Michael Niedermayer
void ff_mpeg4_stuffing(PutBitContext * pbc)
1559 6f91bcd1 Michael Niedermayer
{
1560
    int length;
1561
    put_bits(pbc, 1, 0);
1562
    length= (-get_bit_count(pbc))&7;
1563 92073d01 Michael Niedermayer
    if(length) put_bits(pbc, length, (1<<length)-1);
1564 6f91bcd1 Michael Niedermayer
}
1565
1566 91029be7 Michael Niedermayer
/* must be called before writing the header */
1567
void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
1568
    int time_div, time_mod;
1569
1570
    if(s->pict_type==I_TYPE){ //we will encode a vol header
1571 14bea432 Michael Niedermayer
        int dummy;
1572
        av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
1573
        
1574 91029be7 Michael Niedermayer
        s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
1575
    }
1576 15415af4 Michael Niedermayer
    
1577 1e491e29 Michael Niedermayer
    if(s->current_picture.pts)
1578
        s->time= (s->current_picture.pts*s->time_increment_resolution + 500*1000)/(1000*1000);
1579 15415af4 Michael Niedermayer
    else
1580 14bea432 Michael Niedermayer
        s->time= av_rescale(picture_number*(int64_t)s->avctx->frame_rate_base, s->time_increment_resolution, s->avctx->frame_rate);
1581 91029be7 Michael Niedermayer
    time_div= s->time/s->time_increment_resolution;
1582
    time_mod= s->time%s->time_increment_resolution;
1583
1584
    if(s->pict_type==B_TYPE){
1585 bea669e5 Michael Niedermayer
        s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
1586 91029be7 Michael Niedermayer
    }else{
1587
        s->last_time_base= s->time_base;
1588
        s->time_base= time_div;
1589
        s->pp_time= s->time - s->last_non_b_time;
1590
        s->last_non_b_time= s->time;
1591
    }
1592
}
1593
1594 41773b73 Michael Niedermayer
static void mpeg4_encode_gop_header(MpegEncContext * s){
1595
    int hours, minutes, seconds;
1596
    
1597
    put_bits(&s->pb, 16, 0);
1598
    put_bits(&s->pb, 16, GOP_STARTCODE);
1599
    
1600
    seconds= s->time/s->time_increment_resolution;
1601
    minutes= seconds/60; seconds %= 60;
1602
    hours= minutes/60; minutes %= 60;
1603
    hours%=24;
1604
1605
    put_bits(&s->pb, 5, hours);
1606
    put_bits(&s->pb, 6, minutes);
1607
    put_bits(&s->pb, 1, 1);
1608
    put_bits(&s->pb, 6, seconds);
1609
    
1610
    put_bits(&s->pb, 1, 0); //closed gov == NO
1611
    put_bits(&s->pb, 1, 0); //broken link == NO
1612
1613
    ff_mpeg4_stuffing(&s->pb);
1614
}
1615
1616
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
1617
    int profile_and_level_indication;
1618
    int vo_ver_id;
1619
    
1620
    if(s->max_b_frames || s->quarter_sample){
1621
        profile_and_level_indication= 0xF1; // adv simple level 1
1622
        vo_ver_id= 5;
1623
    }else{
1624
        profile_and_level_indication= 0x01; // simple level 1
1625
        vo_ver_id= 1;
1626
    }
1627
    //FIXME levels
1628
1629
    put_bits(&s->pb, 16, 0);
1630
    put_bits(&s->pb, 16, VOS_STARTCODE);
1631
    
1632
    put_bits(&s->pb, 8, profile_and_level_indication);
1633
    
1634
    put_bits(&s->pb, 16, 0);
1635
    put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
1636
    
1637
    put_bits(&s->pb, 1, 1);
1638
        put_bits(&s->pb, 4, vo_ver_id);
1639
        put_bits(&s->pb, 3, 1); //priority
1640
 
1641
    put_bits(&s->pb, 4, 1); //visual obj type== video obj
1642
    
1643
    put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
1644
1645
    ff_mpeg4_stuffing(&s->pb);
1646
}
1647
1648
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
1649 de6d9b64 Fabrice Bellard
{
1650 41773b73 Michael Niedermayer
    int vo_ver_id;
1651 1ff662cc Michael Niedermayer
1652 41773b73 Michael Niedermayer
    if(s->max_b_frames || s->quarter_sample){
1653
        vo_ver_id= 5;
1654 f8af5cb5 Michael Niedermayer
        s->vo_type= ADV_SIMPLE_VO_TYPE;
1655
    }else{
1656 41773b73 Michael Niedermayer
        vo_ver_id= 1;
1657 f8af5cb5 Michael Niedermayer
        s->vo_type= SIMPLE_VO_TYPE;
1658
    }
1659 1ff662cc Michael Niedermayer
1660 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
1661 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
1662 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
1663 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
1664 10cd5815 Michael Niedermayer
1665
    put_bits(&s->pb, 1, 0);                /* random access vol */
1666 1ff662cc Michael Niedermayer
    put_bits(&s->pb, 8, s->vo_type);        /* video obj type indication */
1667 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 1, 1);                /* is obj layer id= yes */
1668
      put_bits(&s->pb, 4, vo_ver_id);        /* is obj layer ver id */
1669
      put_bits(&s->pb, 3, 1);                /* is obj layer priority */
1670 5d3cea3a Michael Niedermayer
    
1671 58b0b0dd Michael Niedermayer
    float_aspect_to_info(s, s->avctx->aspect_ratio);
1672 5d3cea3a Michael Niedermayer
1673
    put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
1674 050fe8ba Alex Beregszaszi
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
1675
    {
1676
        put_bits(&s->pb, 8, s->aspected_width);
1677
        put_bits(&s->pb, 8, s->aspected_height);
1678
    }
1679 1ff662cc Michael Niedermayer
1680
    if(s->low_delay){
1681
        put_bits(&s->pb, 1, 1);                /* vol control parameters= yes */
1682 1c2a8c7f Michael Niedermayer
        put_bits(&s->pb, 2, 1);                /* chroma format YUV 420/YV12 */
1683 1ff662cc Michael Niedermayer
        put_bits(&s->pb, 1, s->low_delay);
1684
        put_bits(&s->pb, 1, 0);                /* vbv parameters= no */
1685
    }else{
1686
        put_bits(&s->pb, 1, 0);                /* vol control parameters= no */
1687
    }
1688
1689 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 2, RECT_SHAPE);        /* vol shape= rectangle */
1690
    put_bits(&s->pb, 1, 1);                /* marker bit */
1691 9dbcbd92 Michael Niedermayer
    
1692
    put_bits(&s->pb, 16, s->time_increment_resolution);
1693 10cd5815 Michael Niedermayer
    if (s->time_increment_bits < 1)
1694
        s->time_increment_bits = 1;
1695
    put_bits(&s->pb, 1, 1);                /* marker bit */
1696
    put_bits(&s->pb, 1, 0);                /* fixed vop rate=no */
1697
    put_bits(&s->pb, 1, 1);                /* marker bit */
1698
    put_bits(&s->pb, 13, s->width);        /* vol width */
1699
    put_bits(&s->pb, 1, 1);                /* marker bit */
1700
    put_bits(&s->pb, 13, s->height);        /* vol height */
1701
    put_bits(&s->pb, 1, 1);                /* marker bit */
1702 fcb48651 Michael Niedermayer
    put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1703 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 1, 1);                /* obmc disable */
1704
    if (vo_ver_id == 1) {
1705
        put_bits(&s->pb, 1, s->vol_sprite_usage=0);                /* sprite enable */
1706 41773b73 Michael Niedermayer
    }else{
1707 10cd5815 Michael Niedermayer
        put_bits(&s->pb, 2, s->vol_sprite_usage=0);                /* sprite enable */
1708
    }
1709 4d2858de Michael Niedermayer
    
1710
    s->quant_precision=5;
1711
    put_bits(&s->pb, 1, 0);                /* not 8 bit == false */
1712 87f8cab4 Michael Niedermayer
    put_bits(&s->pb, 1, s->mpeg_quant);        /* quant type= (0=h263 style)*/
1713
    if(s->mpeg_quant) put_bits(&s->pb, 2, 0); /* no custom matrixes */
1714
1715 10cd5815 Michael Niedermayer
    if (vo_ver_id != 1)
1716 1457ab52 Michael Niedermayer
        put_bits(&s->pb, 1, s->quarter_sample);
1717 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 1, 1);                /* complexity estimation disable */
1718 92073d01 Michael Niedermayer
    s->resync_marker= s->rtp_mode;
1719
    put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
1720
    put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1721
    if(s->data_partitioning){
1722
        put_bits(&s->pb, 1, 0);                /* no rvlc */
1723
    }
1724
1725 10cd5815 Michael Niedermayer
    if (vo_ver_id != 1){
1726
        put_bits(&s->pb, 1, 0);                /* newpred */
1727
        put_bits(&s->pb, 1, 0);                /* reduced res vop */
1728
    }
1729
    put_bits(&s->pb, 1, 0);                /* scalability */
1730 5596c60c Michael Niedermayer
    
1731 92073d01 Michael Niedermayer
    ff_mpeg4_stuffing(&s->pb);
1732 49c9325f Michael Niedermayer
1733 5596c60c Michael Niedermayer
    /* user data */
1734 b0368839 Michael Niedermayer
    if(!(s->flags & CODEC_FLAG_BITEXACT)){
1735 5596c60c Michael Niedermayer
        put_bits(&s->pb, 16, 0);
1736
        put_bits(&s->pb, 16, 0x1B2);        /* user_data */
1737 ce2749d2 Alex Beregszaszi
        put_string(&s->pb, LIBAVCODEC_IDENT);
1738 5596c60c Michael Niedermayer
        ff_mpeg4_stuffing(&s->pb);
1739
    }
1740 10cd5815 Michael Niedermayer
}
1741
1742
/* write mpeg4 VOP header */
1743
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1744
{
1745 9dbcbd92 Michael Niedermayer
    int time_incr;
1746
    int time_div, time_mod;
1747
    
1748 92073d01 Michael Niedermayer
    if(s->pict_type==I_TYPE){
1749 70ac76c0 Michael Niedermayer
        if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
1750 41773b73 Michael Niedermayer
            mpeg4_encode_visual_object_header(s);
1751
            mpeg4_encode_vol_header(s, 0, 0);
1752
        }
1753
        mpeg4_encode_gop_header(s);
1754 92073d01 Michael Niedermayer
    }
1755 9dbcbd92 Michael Niedermayer
    
1756 4d2858de Michael Niedermayer
    s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE;
1757
1758 9dbcbd92 Michael Niedermayer
//printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1759
    
1760 17592475 Michael Niedermayer
    put_bits(&s->pb, 16, 0);                /* vop header */
1761 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, VOP_STARTCODE);        /* vop header */
1762 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 2, s->pict_type - 1);        /* pict type: I = 0 , P = 1 */
1763 9dbcbd92 Michael Niedermayer
1764 91029be7 Michael Niedermayer
    time_div= s->time/s->time_increment_resolution;
1765
    time_mod= s->time%s->time_increment_resolution;
1766 9dbcbd92 Michael Niedermayer
    time_incr= time_div - s->last_time_base;
1767
    while(time_incr--)
1768
        put_bits(&s->pb, 1, 1);
1769
        
1770 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 1, 0);
1771
1772
    put_bits(&s->pb, 1, 1);        /* marker */
1773 9dbcbd92 Michael Niedermayer
    put_bits(&s->pb, s->time_increment_bits, time_mod);        /* time increment */
1774 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 1, 1);        /* marker */
1775
    put_bits(&s->pb, 1, 1);        /* vop coded */
1776 10cd5815 Michael Niedermayer
    if (    s->pict_type == P_TYPE 
1777
        || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
1778 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 1, s->no_rounding);        /* rounding type */
1779
    }
1780
    put_bits(&s->pb, 3, 0);        /* intra dc VLC threshold */
1781 fcb48651 Michael Niedermayer
    if(!s->progressive_sequence){
1782
         put_bits(&s->pb, 1, s->top_field_first);
1783
         put_bits(&s->pb, 1, s->alternate_scan);
1784
    }
1785 10cd5815 Michael Niedermayer
    //FIXME sprite stuff
1786 de6d9b64 Fabrice Bellard
1787
    put_bits(&s->pb, 5, s->qscale);
1788
1789
    if (s->pict_type != I_TYPE)
1790
        put_bits(&s->pb, 3, s->f_code);        /* fcode_for */
1791 10cd5815 Michael Niedermayer
    if (s->pict_type == B_TYPE)
1792
        put_bits(&s->pb, 3, s->b_code);        /* fcode_back */
1793 de6d9b64 Fabrice Bellard
    //    printf("****frame %d\n", picture_number);
1794 8f8402e4 Michael Niedermayer
1795
     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support 
1796
     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1797 b5a093b3 Michael Niedermayer
     s->h_edge_pos= s->width;
1798
     s->v_edge_pos= s->height;
1799 de6d9b64 Fabrice Bellard
}
1800
1801 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1802
1803 4d2858de Michael Niedermayer
/**
1804
 * change qscale by given dquant and update qscale dependant variables.
1805
 */
1806 5482970b Michael Niedermayer
static void change_qscale(MpegEncContext * s, int dquant)
1807 de6d9b64 Fabrice Bellard
{
1808 5482970b Michael Niedermayer
    s->qscale += dquant;
1809
1810
    if (s->qscale < 1)
1811
        s->qscale = 1;
1812
    else if (s->qscale > 31)
1813
        s->qscale = 31;
1814
1815 8f8402e4 Michael Niedermayer
    s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
1816
    s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
1817 de6d9b64 Fabrice Bellard
}
1818
1819 2ba8f6b8 Michael Niedermayer
/**
1820
 * predicts the dc.
1821
 * @param n block index (0-3 are luma, 4-5 are chroma)
1822
 * @param dc_val_ptr a pointer to the dc_val entry for the current MB will be stored here
1823
 * @param dir_ptr pointer to an integer where the prediction direction will be stored
1824
 * @return the quantized predicted dc
1825
 */
1826 0c1a9eda Zdenek Kabelac
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr)
1827 de6d9b64 Fabrice Bellard
{
1828 4278e7a6 Michael Niedermayer
    int a, b, c, wrap, pred, scale;
1829 0c1a9eda Zdenek Kabelac
    uint16_t *dc_val;
1830 de6d9b64 Fabrice Bellard
1831
    /* find prediction */
1832
    if (n < 4) {
1833
        scale = s->y_dc_scale;
1834
    } else {
1835
        scale = s->c_dc_scale;
1836
    }
1837 6d71b382 Michael Niedermayer
    if(IS_3IV1)
1838
        scale= 8;
1839
1840 4278e7a6 Michael Niedermayer
    wrap= s->block_wrap[n];
1841
    dc_val = s->dc_val[0] + s->block_index[n];
1842 de6d9b64 Fabrice Bellard
1843
    /* B C
1844
     * A X 
1845
     */
1846 4278e7a6 Michael Niedermayer
    a = dc_val[ - 1];
1847
    b = dc_val[ - 1 - wrap];
1848
    c = dc_val[ - wrap];
1849 de6d9b64 Fabrice Bellard
1850 4d2858de Michael Niedermayer
    /* outside slice handling (we cant do that by memset as we need the dc for error resilience) */
1851
    if(s->first_slice_line && n!=3){
1852
        if(n!=2) b=c= 1024;
1853
        if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024;
1854
    }
1855
    if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){
1856
        if(n==0 || n==4 || n==5)
1857
            b=1024;
1858
    }
1859
1860 de6d9b64 Fabrice Bellard
    if (abs(a - b) < abs(b - c)) {
1861
        pred = c;
1862
        *dir_ptr = 1; /* top */
1863
    } else {
1864
        pred = a;
1865
        *dir_ptr = 0; /* left */
1866
    }
1867
    /* we assume pred is positive */
1868 d4961b35 BERO
    pred = FASTDIV((pred + (scale >> 1)), scale);
1869 de6d9b64 Fabrice Bellard
1870
    /* prepare address for prediction update */
1871 4278e7a6 Michael Niedermayer
    *dc_val_ptr = &dc_val[0];
1872 de6d9b64 Fabrice Bellard
1873
    return pred;
1874
}
1875
1876 2ba8f6b8 Michael Niedermayer
/**
1877
 * predicts the ac.
1878
 * @param n block index (0-3 are luma, 4-5 are chroma)
1879
 * @param dir the ac prediction direction
1880
 */
1881 0e15384d Michael Niedermayer
void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
1882 de6d9b64 Fabrice Bellard
                   int dir)
1883
{
1884 4278e7a6 Michael Niedermayer
    int i;
1885 0c1a9eda Zdenek Kabelac
    int16_t *ac_val, *ac_val1;
1886 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
1887 de6d9b64 Fabrice Bellard
1888
    /* find prediction */
1889 4278e7a6 Michael Niedermayer
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1890 de6d9b64 Fabrice Bellard
    ac_val1 = ac_val;
1891
    if (s->ac_pred) {
1892
        if (dir == 0) {
1893 7bc9090a Michael Niedermayer
            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
1894 de6d9b64 Fabrice Bellard
            /* left prediction */
1895
            ac_val -= 16;
1896 3e6a20a0 Michael Niedermayer
            
1897 1e491e29 Michael Niedermayer
            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
1898 3e6a20a0 Michael Niedermayer
                /* same qscale */
1899
                for(i=1;i<8;i++) {
1900 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
1901 3e6a20a0 Michael Niedermayer
                }
1902
            }else{
1903
                /* different qscale, we must rescale */
1904
                for(i=1;i<8;i++) {
1905 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
1906 3e6a20a0 Michael Niedermayer
                }
1907 de6d9b64 Fabrice Bellard
            }
1908
        } else {
1909 7bc9090a Michael Niedermayer
            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
1910 de6d9b64 Fabrice Bellard
            /* top prediction */
1911 4278e7a6 Michael Niedermayer
            ac_val -= 16 * s->block_wrap[n];
1912 3e6a20a0 Michael Niedermayer
1913 1e491e29 Michael Niedermayer
            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
1914 3e6a20a0 Michael Niedermayer
                /* same qscale */
1915
                for(i=1;i<8;i++) {
1916 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
1917 3e6a20a0 Michael Niedermayer
                }
1918
            }else{
1919
                /* different qscale, we must rescale */
1920
                for(i=1;i<8;i++) {
1921 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
1922 3e6a20a0 Michael Niedermayer
                }
1923 de6d9b64 Fabrice Bellard
            }
1924
        }
1925
    }
1926
    /* left copy */
1927
    for(i=1;i<8;i++)
1928 b0368839 Michael Niedermayer
        ac_val1[i    ] = block[s->dsp.idct_permutation[i<<3]];
1929 d930ef19 Michael Niedermayer
1930 de6d9b64 Fabrice Bellard
    /* top copy */
1931
    for(i=1;i<8;i++)
1932 b0368839 Michael Niedermayer
        ac_val1[8 + i] = block[s->dsp.idct_permutation[i   ]];
1933 d930ef19 Michael Niedermayer
1934 de6d9b64 Fabrice Bellard
}
1935
1936 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1937
1938 0e15384d Michael Niedermayer
static void mpeg4_inv_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
1939 d6231b9e Michael Niedermayer
                              int dir)
1940 de6d9b64 Fabrice Bellard
{
1941 4278e7a6 Michael Niedermayer
    int i;
1942 0c1a9eda Zdenek Kabelac
    int16_t *ac_val;
1943 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
1944 de6d9b64 Fabrice Bellard
1945 d6231b9e Michael Niedermayer
    /* find prediction */
1946 4278e7a6 Michael Niedermayer
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1947 d6231b9e Michael Niedermayer
 
1948
    if (dir == 0) {
1949 7bc9090a Michael Niedermayer
        const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
1950 d6231b9e Michael Niedermayer
        /* left prediction */
1951
        ac_val -= 16;
1952 1e491e29 Michael Niedermayer
        if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
1953 99609036 Michael Niedermayer
            /* same qscale */
1954
            for(i=1;i<8;i++) {
1955 b0368839 Michael Niedermayer
                block[s->dsp.idct_permutation[i<<3]] -= ac_val[i];
1956 99609036 Michael Niedermayer
            }
1957
        }else{
1958
            /* different qscale, we must rescale */
1959
            for(i=1;i<8;i++) {
1960 b0368839 Michael Niedermayer
                block[s->dsp.idct_permutation[i<<3]] -= ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
1961 99609036 Michael Niedermayer
            }
1962 d6231b9e Michael Niedermayer
        }
1963
    } else {
1964 7bc9090a Michael Niedermayer
        const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
1965 d6231b9e Michael Niedermayer
        /* top prediction */
1966 4278e7a6 Michael Niedermayer
        ac_val -= 16 * s->block_wrap[n];
1967 1e491e29 Michael Niedermayer
        if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
1968 99609036 Michael Niedermayer
            /* same qscale */
1969
            for(i=1;i<8;i++) {
1970 b0368839 Michael Niedermayer
                block[s->dsp.idct_permutation[i]] -= ac_val[i + 8];
1971 99609036 Michael Niedermayer
            }
1972
        }else{
1973
            /* different qscale, we must rescale */
1974
            for(i=1;i<8;i++) {
1975 b0368839 Michael Niedermayer
                block[s->dsp.idct_permutation[i]] -= ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
1976 99609036 Michael Niedermayer
            }
1977 d6231b9e Michael Niedermayer
        }
1978 de6d9b64 Fabrice Bellard
    }
1979 d6231b9e Michael Niedermayer
}
1980
1981 2ba8f6b8 Michael Niedermayer
/**
1982
 * encodes the dc value.
1983
 * @param n block index (0-3 are luma, 4-5 are chroma)
1984
 */
1985 92073d01 Michael Niedermayer
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
1986 d6231b9e Michael Niedermayer
{
1987 2a164b25 Michael Niedermayer
#if 1
1988 92073d01 Michael Niedermayer
//    if(level<-255 || level>255) printf("dc overflow\n");
1989 2a164b25 Michael Niedermayer
    level+=256;
1990
    if (n < 4) {
1991
        /* luminance */
1992 67725183 Michael Niedermayer
        put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
1993 2a164b25 Michael Niedermayer
    } else {
1994
        /* chrominance */
1995 67725183 Michael Niedermayer
        put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
1996 2a164b25 Michael Niedermayer
    }
1997
#else
1998 d6231b9e Michael Niedermayer
    int size, v;
1999 de6d9b64 Fabrice Bellard
    /* find number of bits */
2000
    size = 0;
2001
    v = abs(level);
2002
    while (v) {
2003
        v >>= 1;
2004
        size++;
2005
    }
2006
2007
    if (n < 4) {
2008
        /* luminance */
2009
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
2010
    } else {
2011
        /* chrominance */
2012
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
2013
    }
2014
2015
    /* encode remaining bits */
2016
    if (size > 0) {
2017
        if (level < 0)
2018
            level = (-level) ^ ((1 << size) - 1);
2019
        put_bits(&s->pb, size, level);
2020
        if (size > 8)
2021
            put_bits(&s->pb, 1, 1);
2022
    }
2023 2a164b25 Michael Niedermayer
#endif
2024 de6d9b64 Fabrice Bellard
}
2025 7604246d Wolfgang Hesseler
2026 2ba8f6b8 Michael Niedermayer
/**
2027
 * encodes a 8x8 block
2028
 * @param n block index (0-3 are luma, 4-5 are chroma)
2029
 */
2030 3db48188 Michael Niedermayer
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, 
2031 0c1a9eda Zdenek Kabelac
                               uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
2032 de6d9b64 Fabrice Bellard
{
2033 d2975f8d Michael Niedermayer
    int i, last_non_zero;
2034
#if 0 //variables for the outcommented version
2035
    int code, sign, last;
2036
#endif
2037 de6d9b64 Fabrice Bellard
    const RLTable *rl;
2038 0c1a9eda Zdenek Kabelac
    uint32_t *bits_tab;
2039
    uint8_t *len_tab;
2040 3db48188 Michael Niedermayer
    const int last_index = s->block_last_index[n];
2041 de6d9b64 Fabrice Bellard
2042 3db48188 Michael Niedermayer
    if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2043 de6d9b64 Fabrice Bellard
        /* mpeg4 based DC predictor */
2044 92073d01 Michael Niedermayer
        mpeg4_encode_dc(dc_pb, intra_dc, n);
2045 3db48188 Michael Niedermayer
        if(last_index<1) return;
2046 de6d9b64 Fabrice Bellard
        i = 1;
2047
        rl = &rl_intra;
2048 3db48188 Michael Niedermayer
        bits_tab= uni_mpeg4_intra_rl_bits;
2049
        len_tab = uni_mpeg4_intra_rl_len;
2050 de6d9b64 Fabrice Bellard
    } else {
2051 3db48188 Michael Niedermayer
        if(last_index<0) return;
2052 de6d9b64 Fabrice Bellard
        i = 0;
2053
        rl = &rl_inter;
2054 3db48188 Michael Niedermayer
        bits_tab= uni_mpeg4_inter_rl_bits;
2055
        len_tab = uni_mpeg4_inter_rl_len;
2056 de6d9b64 Fabrice Bellard
    }
2057
2058
    /* AC coefs */
2059
    last_non_zero = i - 1;
2060 3db48188 Michael Niedermayer
#if 1
2061
    for (; i < last_index; i++) {
2062
        int level = block[ scan_table[i] ];
2063 de6d9b64 Fabrice Bellard
        if (level) {
2064 3db48188 Michael Niedermayer
            int run = i - last_non_zero - 1;
2065
            level+=64;
2066
            if((level&(~127)) == 0){
2067
                const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2068
                put_bits(ac_pb, len_tab[index], bits_tab[index]);
2069
            }else{ //ESC3
2070
                put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2071
            }
2072
            last_non_zero = i;
2073
        }
2074
    }
2075
    /*if(i<=last_index)*/{
2076
        int level = block[ scan_table[i] ];
2077
        int run = i - last_non_zero - 1;
2078
        level+=64;
2079
        if((level&(~127)) == 0){
2080
            const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2081
            put_bits(ac_pb, len_tab[index], bits_tab[index]);
2082
        }else{ //ESC3
2083
            put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2084
        }
2085
    }
2086
#else
2087
    for (; i <= last_index; i++) {
2088
        const int slevel = block[ scan_table[i] ];
2089
        if (slevel) {
2090
            int level;
2091
            int run = i - last_non_zero - 1;
2092 de6d9b64 Fabrice Bellard
            last = (i == last_index);
2093
            sign = 0;
2094 3db48188 Michael Niedermayer
            level = slevel;
2095 de6d9b64 Fabrice Bellard
            if (level < 0) {
2096
                sign = 1;
2097
                level = -level;
2098
            }
2099
            code = get_rl_index(rl, last, run, level);
2100 92073d01 Michael Niedermayer
            put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2101 de6d9b64 Fabrice Bellard
            if (code == rl->n) {
2102
                int level1, run1;
2103
                level1 = level - rl->max_level[last][run];
2104
                if (level1 < 1) 
2105
                    goto esc2;
2106
                code = get_rl_index(rl, last, run, level1);
2107
                if (code == rl->n) {
2108
                esc2:
2109 92073d01 Michael Niedermayer
                    put_bits(ac_pb, 1, 1);
2110 de6d9b64 Fabrice Bellard
                    if (level > MAX_LEVEL)
2111
                        goto esc3;
2112
                    run1 = run - rl->max_run[last][level] - 1;
2113
                    if (run1 < 0)
2114
                        goto esc3;
2115
                    code = get_rl_index(rl, last, run1, level);
2116
                    if (code == rl->n) {
2117
                    esc3:
2118
                        /* third escape */
2119 92073d01 Michael Niedermayer
                        put_bits(ac_pb, 1, 1);
2120
                        put_bits(ac_pb, 1, last);
2121
                        put_bits(ac_pb, 6, run);
2122
                        put_bits(ac_pb, 1, 1);
2123
                        put_bits(ac_pb, 12, slevel & 0xfff);
2124
                        put_bits(ac_pb, 1, 1);
2125 de6d9b64 Fabrice Bellard
                    } else {
2126
                        /* second escape */
2127 92073d01 Michael Niedermayer
                        put_bits(ac_pb, 1, 0);
2128
                        put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2129
                        put_bits(ac_pb, 1, sign);
2130 de6d9b64 Fabrice Bellard
                    }
2131
                } else {
2132
                    /* first escape */
2133 92073d01 Michael Niedermayer
                    put_bits(ac_pb, 1, 0);
2134
                    put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2135
                    put_bits(ac_pb, 1, sign);
2136 de6d9b64 Fabrice Bellard
                }
2137
            } else {
2138 92073d01 Michael Niedermayer
                put_bits(ac_pb, 1, sign);
2139 de6d9b64 Fabrice Bellard
            }
2140
            last_non_zero = i;
2141
        }
2142
    }
2143 3db48188 Michael Niedermayer
#endif
2144 de6d9b64 Fabrice Bellard
}
2145 1457ab52 Michael Niedermayer
2146
static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, 
2147 0c1a9eda Zdenek Kabelac
                               uint8_t *scan_table)
2148 1457ab52 Michael Niedermayer
{
2149
    int i, last_non_zero;
2150
    const RLTable *rl;
2151 0c1a9eda Zdenek Kabelac
    uint8_t *len_tab;
2152 1457ab52 Michael Niedermayer
    const int last_index = s->block_last_index[n];
2153
    int len=0;
2154
2155
    if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2156
        /* mpeg4 based DC predictor */
2157
        //mpeg4_encode_dc(dc_pb, intra_dc, n); //FIXME
2158
        if(last_index<1) return len;
2159
        i = 1;
2160
        rl = &rl_intra;
2161
        len_tab = uni_mpeg4_intra_rl_len;
2162
    } else {
2163
        if(last_index<0) return 0;
2164
        i = 0;
2165
        rl = &rl_inter;
2166
        len_tab = uni_mpeg4_inter_rl_len;
2167
    }
2168
2169
    /* AC coefs */
2170
    last_non_zero = i - 1;
2171
    for (; i < last_index; i++) {
2172
        int level = block[ scan_table[i] ];
2173
        if (level) {
2174
            int run = i - last_non_zero - 1;
2175
            level+=64;
2176
            if((level&(~127)) == 0){
2177
                const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2178
                len += len_tab[index];
2179
            }else{ //ESC3
2180
                len += 7+2+1+6+1+12+1;
2181
            }
2182
            last_non_zero = i;
2183
        }
2184
    }
2185
    /*if(i<=last_index)*/{
2186
        int level = block[ scan_table[i] ];
2187
        int run = i - last_non_zero - 1;
2188
        level+=64;
2189
        if((level&(~127)) == 0){
2190
            const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2191
            len += len_tab[index];
2192
        }else{ //ESC3
2193
            len += 7+2+1+6+1+12+1;
2194
        }
2195
    }
2196
    
2197
    return len;
2198
}
2199
2200 bbed9259 Michael Niedermayer
#endif
2201 de6d9b64 Fabrice Bellard
2202
2203
/***********************************************/
2204
/* decoding */
2205
2206
static VLC intra_MCBPC_vlc;
2207
static VLC inter_MCBPC_vlc;
2208
static VLC cbpy_vlc;
2209
static VLC mv_vlc;
2210
static VLC dc_lum, dc_chrom;
2211 73c8e514 Michael Niedermayer
static VLC sprite_trajectory;
2212 6f91bcd1 Michael Niedermayer
static VLC mb_type_b_vlc;
2213 de6d9b64 Fabrice Bellard
2214
void init_vlc_rl(RLTable *rl)
2215
{
2216 45a82edb Michael Niedermayer
    int i, q;
2217
    
2218 de6d9b64 Fabrice Bellard
    init_vlc(&rl->vlc, 9, rl->n + 1, 
2219
             &rl->table_vlc[0][1], 4, 2,
2220
             &rl->table_vlc[0][0], 4, 2);
2221 45a82edb Michael Niedermayer
2222
    
2223
    for(q=0; q<32; q++){
2224
        int qmul= q*2;
2225
        int qadd= (q-1)|1;
2226
        
2227
        if(q==0){
2228
            qmul=1;
2229
            qadd=0;
2230
        }
2231
        
2232
        rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
2233
        for(i=0; i<rl->vlc.table_size; i++){
2234
            int code= rl->vlc.table[i][0];
2235
            int len = rl->vlc.table[i][1];
2236
            int level, run;
2237
        
2238
            if(len==0){ // illegal code
2239 1a013246 Michael Niedermayer
                run= 66;
2240 45a82edb Michael Niedermayer
                level= MAX_LEVEL;
2241
            }else if(len<0){ //more bits needed
2242
                run= 0;
2243
                level= code;
2244
            }else{
2245
                if(code==rl->n){ //esc
2246 1a013246 Michael Niedermayer
                    run= 66;
2247 45a82edb Michael Niedermayer
                    level= 0;
2248
                }else{
2249
                    run=   rl->table_run  [code] + 1;
2250
                    level= rl->table_level[code] * qmul + qadd;
2251
                    if(code >= rl->last) run+=192;
2252
                }
2253
            }
2254
            rl->rl_vlc[q][i].len= len;
2255
            rl->rl_vlc[q][i].level= level;
2256
            rl->rl_vlc[q][i].run= run;
2257
        }
2258
    }
2259 de6d9b64 Fabrice Bellard
}
2260
2261
/* init vlcs */
2262
2263
/* XXX: find a better solution to handle static init */
2264
void h263_decode_init_vlc(MpegEncContext *s)
2265
{
2266
    static int done = 0;
2267
2268
    if (!done) {
2269
        done = 1;
2270
2271 edcf9890 Michael Niedermayer
        init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8, 
2272 de6d9b64 Fabrice Bellard
                 intra_MCBPC_bits, 1, 1,
2273
                 intra_MCBPC_code, 1, 1);
2274 edcf9890 Michael Niedermayer
        init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25, 
2275 de6d9b64 Fabrice Bellard
                 inter_MCBPC_bits, 1, 1,
2276
                 inter_MCBPC_code, 1, 1);
2277 edcf9890 Michael Niedermayer
        init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
2278 de6d9b64 Fabrice Bellard
                 &cbpy_tab[0][1], 2, 1,
2279
                 &cbpy_tab[0][0], 2, 1);
2280 edcf9890 Michael Niedermayer
        init_vlc(&mv_vlc, MV_VLC_BITS, 33,
2281 de6d9b64 Fabrice Bellard
                 &mvtab[0][1], 2, 1,
2282
                 &mvtab[0][0], 2, 1);
2283
        init_rl(&rl_inter);
2284
        init_rl(&rl_intra);
2285 a4e8b587 Michael Niedermayer
        init_rl(&rvlc_rl_inter);
2286
        init_rl(&rvlc_rl_intra);
2287 d140623f Juanjo
        init_rl(&rl_intra_aic);
2288 de6d9b64 Fabrice Bellard
        init_vlc_rl(&rl_inter);
2289
        init_vlc_rl(&rl_intra);
2290 a4e8b587 Michael Niedermayer
        init_vlc_rl(&rvlc_rl_inter);
2291
        init_vlc_rl(&rvlc_rl_intra);
2292 d140623f Juanjo
        init_vlc_rl(&rl_intra_aic);
2293 c03a7172 Michael Niedermayer
        init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2294 de6d9b64 Fabrice Bellard
                 &DCtab_lum[0][1], 2, 1,
2295
                 &DCtab_lum[0][0], 2, 1);
2296 c03a7172 Michael Niedermayer
        init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2297 de6d9b64 Fabrice Bellard
                 &DCtab_chrom[0][1], 2, 1,
2298
                 &DCtab_chrom[0][0], 2, 1);
2299 edcf9890 Michael Niedermayer
        init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2300 73c8e514 Michael Niedermayer
                 &sprite_trajectory_tab[0][1], 4, 2,
2301
                 &sprite_trajectory_tab[0][0], 4, 2);
2302 edcf9890 Michael Niedermayer
        init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2303 6f91bcd1 Michael Niedermayer
                 &mb_type_b_tab[0][1], 2, 1,
2304
                 &mb_type_b_tab[0][0], 2, 1);
2305 de6d9b64 Fabrice Bellard
    }
2306 4d2858de Michael Niedermayer
}
2307 fd7db0fd Michael Niedermayer
2308 4d2858de Michael Niedermayer
/**
2309
 * Get the GOB height based on picture height.
2310
 */
2311
int ff_h263_get_gob_height(MpegEncContext *s){
2312
    if (s->height <= 400)
2313
        return 1;
2314
    else if (s->height <= 800)
2315
        return  2;
2316
    else
2317
        return 4;
2318 de6d9b64 Fabrice Bellard
}
2319
2320 4d2858de Michael Niedermayer
/**
2321
 * decodes the group of blocks header.
2322
 * @return <0 if an error occured
2323
 */
2324
static int h263_decode_gob_header(MpegEncContext *s)
2325 de6d9b64 Fabrice Bellard
{
2326 644d98a4 Juanjo
    unsigned int val, gfid;
2327 4d2858de Michael Niedermayer
    int left;
2328 102d3908 Juanjo
    
2329 4949028f Juanjo
    /* Check for GOB Start Code */
2330 644d98a4 Juanjo
    val = show_bits(&s->gb, 16);
2331 4d2858de Michael Niedermayer
    if(val)
2332
        return -1;
2333
2334 644d98a4 Juanjo
        /* We have a GBSC probably with GSTUFF */
2335 4d2858de Michael Niedermayer
    skip_bits(&s->gb, 16); /* Drop the zeros */
2336 68f593b4 Michael Niedermayer
    left= s->gb.size_in_bits - get_bits_count(&s->gb);
2337 4d2858de Michael Niedermayer
    //MN: we must check the bits left or we might end in a infinite loop (or segfault)
2338
    for(;left>13; left--){
2339
        if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
2340
    }
2341
    if(left<=13) 
2342
        return -1;
2343
2344 4949028f Juanjo
#ifdef DEBUG
2345 4d2858de Michael Niedermayer
    fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
2346 102d3908 Juanjo
#endif
2347 4d2858de Michael Niedermayer
    s->gob_number = get_bits(&s->gb, 5); /* GN */
2348
    gfid = get_bits(&s->gb, 2); /* GFID */
2349
    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
2350
    if(s->qscale==0) 
2351
        return -1;
2352
    s->mb_x= 0;
2353
    s->mb_y= s->gob_index* s->gob_number;
2354 4949028f Juanjo
#ifdef DEBUG
2355 4d2858de Michael Niedermayer
    fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
2356 4949028f Juanjo
#endif
2357 644d98a4 Juanjo
    return 0;
2358
}
2359
2360 49092244 Michael Niedermayer
static inline void memsetw(short *tab, int val, int n)
2361
{
2362
    int i;
2363
    for(i=0;i<n;i++)
2364
        tab[i] = val;
2365
}
2366
2367 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2368
2369 92073d01 Michael Niedermayer
void ff_mpeg4_init_partitions(MpegEncContext *s)
2370
{
2371
    init_put_bits(&s->tex_pb, s->tex_pb_buffer, PB_BUFFER_SIZE, NULL, NULL);
2372
    init_put_bits(&s->pb2   , s->pb2_buffer   , PB_BUFFER_SIZE, NULL, NULL);
2373
}
2374
2375
void ff_mpeg4_merge_partitions(MpegEncContext *s)
2376
{
2377
    const int pb2_len   = get_bit_count(&s->pb2   );
2378
    const int tex_pb_len= get_bit_count(&s->tex_pb);
2379
    const int bits= get_bit_count(&s->pb);
2380
2381
    if(s->pict_type==I_TYPE){
2382
        put_bits(&s->pb, 19, DC_MARKER);
2383
        s->misc_bits+=19 + pb2_len + bits - s->last_bits;
2384
        s->i_tex_bits+= tex_pb_len;
2385
    }else{
2386
        put_bits(&s->pb, 17, MOTION_MARKER);
2387 4d2858de Michael Niedermayer
        s->misc_bits+=17 + pb2_len;
2388 92073d01 Michael Niedermayer
        s->mv_bits+= bits - s->last_bits;
2389
        s->p_tex_bits+= tex_pb_len;
2390
    }
2391
2392
    flush_put_bits(&s->pb2);
2393
    flush_put_bits(&s->tex_pb);
2394
2395
    ff_copy_bits(&s->pb, s->pb2_buffer   , pb2_len);
2396
    ff_copy_bits(&s->pb, s->tex_pb_buffer, tex_pb_len);
2397
    s->last_bits= get_bit_count(&s->pb);
2398
}
2399
2400 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
2401
2402 4d2858de Michael Niedermayer
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){
2403
    switch(s->pict_type){
2404
        case I_TYPE:
2405
            return 16;
2406
        case P_TYPE:
2407
        case S_TYPE:
2408
            return s->f_code+15;
2409
        case B_TYPE:
2410 b8a78f41 Michael Niedermayer
            return FFMAX(FFMAX(s->f_code, s->b_code)+15, 17);
2411 4d2858de Michael Niedermayer
        default:
2412
            return -1;
2413
    }
2414
}
2415
2416 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2417
2418 92073d01 Michael Niedermayer
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
2419 49092244 Michael Niedermayer
{
2420
    int mb_num_bits= av_log2(s->mb_num - 1) + 1;
2421
2422 92073d01 Michael Niedermayer
    ff_mpeg4_stuffing(&s->pb);
2423 4d2858de Michael Niedermayer
    put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
2424 92073d01 Michael Niedermayer
    put_bits(&s->pb, 1, 1);
2425
    
2426
    put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
2427 4d2858de Michael Niedermayer
    put_bits(&s->pb, s->quant_precision, s->qscale);
2428 92073d01 Michael Niedermayer
    put_bits(&s->pb, 1, 0); /* no HEC */
2429
}
2430 49092244 Michael Niedermayer
2431 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
2432
2433 92073d01 Michael Niedermayer
/**
2434 4d2858de Michael Niedermayer
 * check if the next stuff is a resync marker or the end.
2435
 * @return 0 if not
2436 92073d01 Michael Niedermayer
 */
2437 4d2858de Michael Niedermayer
static inline int mpeg4_is_resync(MpegEncContext *s){
2438
    const int bits_count= get_bits_count(&s->gb);
2439
    
2440
    if(s->workaround_bugs&FF_BUG_NO_PADDING){
2441
        return 0;
2442
    }
2443
2444 68f593b4 Michael Niedermayer
    if(bits_count + 8 >= s->gb.size_in_bits){
2445 4d2858de Michael Niedermayer
        int v= show_bits(&s->gb, 8);
2446
        v|= 0x7F >> (7-(bits_count&7));
2447
                
2448
        if(v==0x7F)
2449
            return 1;
2450
    }else{
2451
        if(show_bits(&s->gb, 16) == ff_mpeg4_resync_prefix[bits_count&7]){
2452
            int len;
2453
            GetBitContext gb= s->gb;
2454
        
2455
            skip_bits(&s->gb, 1);
2456
            align_get_bits(&s->gb);
2457
        
2458
            for(len=0; len<32; len++){
2459
                if(get_bits1(&s->gb)) break;
2460
            }
2461
2462
            s->gb= gb;
2463
2464
            if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
2465
                return 1;
2466
        }
2467
    }
2468
    return 0;
2469
}
2470
2471
/**
2472
 * decodes the next video packet.
2473
 * @return <0 if something went wrong
2474
 */
2475
static int mpeg4_decode_video_packet_header(MpegEncContext *s)
2476 92073d01 Michael Niedermayer
{
2477
    int mb_num_bits= av_log2(s->mb_num - 1) + 1;
2478 4d2858de Michael Niedermayer
    int header_extension=0, mb_num, len;
2479 92073d01 Michael Niedermayer
    
2480
    /* is there enough space left for a video packet + header */
2481 68f593b4 Michael Niedermayer
    if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
2482 92073d01 Michael Niedermayer
2483 4d2858de Michael Niedermayer
    for(len=0; len<32; len++){
2484
        if(get_bits1(&s->gb)) break;
2485 49092244 Michael Niedermayer
    }
2486 92073d01 Michael Niedermayer
2487 4d2858de Michael Niedermayer
    if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
2488
        printf("marker does not match f_code\n");
2489 92073d01 Michael Niedermayer
        return -1;
2490
    }
2491 4d2858de Michael Niedermayer
    
2492 49092244 Michael Niedermayer
    if(s->shape != RECT_SHAPE){
2493 4d2858de Michael Niedermayer
        header_extension= get_bits1(&s->gb);
2494 49092244 Michael Niedermayer
        //FIXME more stuff here
2495
    }
2496
2497 4d2858de Michael Niedermayer
    mb_num= get_bits(&s->gb, mb_num_bits);
2498
    if(mb_num>=s->mb_num){
2499
        fprintf(stderr, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
2500 92073d01 Michael Niedermayer
        return -1;
2501
    }
2502 84876d36 Michael Niedermayer
    if(s->pict_type == B_TYPE){
2503 7bc9090a Michael Niedermayer
        while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) mb_num++;
2504 84876d36 Michael Niedermayer
        if(mb_num >= s->mb_num) return -1; // slice contains just skiped MBs which where allready decoded
2505
    }
2506
    
2507 4d2858de Michael Niedermayer
    s->mb_x= mb_num % s->mb_width;
2508
    s->mb_y= mb_num / s->mb_width;
2509 49092244 Michael Niedermayer
2510
    if(s->shape != BIN_ONLY_SHAPE){
2511 4d2858de Michael Niedermayer
        int qscale= get_bits(&s->gb, s->quant_precision); 
2512
        if(qscale)
2513
            s->qscale= qscale;
2514 49092244 Michael Niedermayer
    }
2515
2516
    if(s->shape == RECT_SHAPE){
2517 4d2858de Michael Niedermayer
        header_extension= get_bits1(&s->gb);
2518 49092244 Michael Niedermayer
    }
2519
    if(header_extension){
2520 92073d01 Michael Niedermayer
        int time_increment;
2521 49092244 Michael Niedermayer
        int time_incr=0;
2522 92073d01 Michael Niedermayer
2523 4d2858de Michael Niedermayer
        while (get_bits1(&s->gb) != 0) 
2524 49092244 Michael Niedermayer
            time_incr++;
2525
2526 4d2858de Michael Niedermayer
        check_marker(&s->gb, "before time_increment in video packed header");
2527
        time_increment= get_bits(&s->gb, s->time_increment_bits);
2528
        check_marker(&s->gb, "before vop_coding_type in video packed header");
2529 49092244 Michael Niedermayer
        
2530 4d2858de Michael Niedermayer
        skip_bits(&s->gb, 2); /* vop coding type */
2531 49092244 Michael Niedermayer
        //FIXME not rect stuff here
2532
2533
        if(s->shape != BIN_ONLY_SHAPE){
2534 4d2858de Michael Niedermayer
            skip_bits(&s->gb, 3); /* intra dc vlc threshold */
2535
//FIXME dont just ignore everything
2536 f56dd362 Michael Niedermayer
            if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2537 49092244 Michael Niedermayer
                mpeg4_decode_sprite_trajectory(s);
2538 f3cfadd5 Michael Niedermayer
                fprintf(stderr, "untested\n");
2539 49092244 Michael Niedermayer
            }
2540
2541
            //FIXME reduced res stuff here
2542
            
2543
            if (s->pict_type != I_TYPE) {
2544 4d2858de Michael Niedermayer
                int f_code = get_bits(&s->gb, 3);        /* fcode_for */
2545 f3cfadd5 Michael Niedermayer
                if(f_code==0){
2546
                    printf("Error, video packet header damaged (f_code=0)\n");
2547 49092244 Michael Niedermayer
                }
2548
            }
2549
            if (s->pict_type == B_TYPE) {
2550 4d2858de Michael Niedermayer
                int b_code = get_bits(&s->gb, 3);
2551 f3cfadd5 Michael Niedermayer
                if(b_code==0){
2552
                    printf("Error, video packet header damaged (b_code=0)\n");
2553
                }
2554 49092244 Michael Niedermayer
            }       
2555
        }
2556
    }
2557
    //FIXME new-pred stuff
2558 92073d01 Michael Niedermayer
    
2559
//printf("parse ok %d %d %d %d\n", mb_num, s->mb_x + s->mb_y*s->mb_width, get_bits_count(gb), get_bits_count(&s->gb));
2560
2561 4d2858de Michael Niedermayer
    return 0;
2562 92073d01 Michael Niedermayer
}
2563
2564
void ff_mpeg4_clean_buffers(MpegEncContext *s)
2565
{
2566
    int c_wrap, c_xy, l_wrap, l_xy;
2567 49092244 Michael Niedermayer
2568
    l_wrap= s->block_wrap[0];
2569 92073d01 Michael Niedermayer
    l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
2570 49092244 Michael Niedermayer
    c_wrap= s->block_wrap[4];
2571 92073d01 Michael Niedermayer
    c_xy= s->mb_y*c_wrap + s->mb_x;
2572 49092244 Michael Niedermayer
2573 4d2858de Michael Niedermayer
#if 0
2574 49092244 Michael Niedermayer
    /* clean DC */
2575 92073d01 Michael Niedermayer
    memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
2576
    memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
2577
    memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
2578 4d2858de Michael Niedermayer
#endif
2579 49092244 Michael Niedermayer
2580
    /* clean AC */
2581 0c1a9eda Zdenek Kabelac
    memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t));
2582
    memset(s->ac_val[1] + c_xy, 0, (c_wrap  +1)*16*sizeof(int16_t));
2583
    memset(s->ac_val[2] + c_xy, 0, (c_wrap  +1)*16*sizeof(int16_t));
2584 49092244 Michael Niedermayer
2585
    /* clean MV */
2586 92073d01 Michael Niedermayer
    // we cant clear the MVs as they might be needed by a b frame
2587 0c1a9eda Zdenek Kabelac
//    memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t));
2588
//    memset(s->motion_val, 0, 2*sizeof(int16_t)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
2589 92073d01 Michael Niedermayer
    s->last_mv[0][0][0]=
2590
    s->last_mv[0][0][1]=
2591
    s->last_mv[1][0][0]=
2592
    s->last_mv[1][0][1]= 0;
2593
}
2594
2595 4d2858de Michael Niedermayer
/**
2596
 * decodes the group of blocks / video packet header.
2597
 * @return <0 if no resync found
2598
 */
2599
int ff_h263_resync(MpegEncContext *s){
2600
    int left, ret;
2601 92073d01 Michael Niedermayer
    
2602 4d2858de Michael Niedermayer
    if(s->codec_id==CODEC_ID_MPEG4)
2603
        skip_bits1(&s->gb);
2604 92073d01 Michael Niedermayer
    
2605 4d2858de Michael Niedermayer
    align_get_bits(&s->gb);
2606 92073d01 Michael Niedermayer
2607 4d2858de Michael Niedermayer
    if(show_bits(&s->gb, 16)==0){
2608
        if(s->codec_id==CODEC_ID_MPEG4)
2609
            ret= mpeg4_decode_video_packet_header(s);
2610
        else
2611
            ret= h263_decode_gob_header(s);
2612
        if(ret>=0)
2613
            return 0;
2614 92073d01 Michael Niedermayer
    }
2615 4d2858de Michael Niedermayer
    //ok, its not where its supposed to be ...
2616
    s->gb= s->last_resync_gb;
2617
    align_get_bits(&s->gb);
2618 68f593b4 Michael Niedermayer
    left= s->gb.size_in_bits - get_bits_count(&s->gb);
2619 92073d01 Michael Niedermayer
    
2620 4d2858de Michael Niedermayer
    for(;left>16+1+5+5; left-=8){ 
2621
        if(show_bits(&s->gb, 16)==0){
2622
            GetBitContext bak= s->gb;
2623 92073d01 Michael Niedermayer
2624 4d2858de Michael Niedermayer
            if(s->codec_id==CODEC_ID_MPEG4)
2625
                ret= mpeg4_decode_video_packet_header(s);
2626
            else
2627
                ret= h263_decode_gob_header(s);
2628
            if(ret>=0)
2629
                return 0;
2630 92073d01 Michael Niedermayer
2631 4d2858de Michael Niedermayer
            s->gb= bak;
2632
        }
2633
        skip_bits(&s->gb, 8);
2634
    }
2635
    
2636
    return -1;
2637 92073d01 Michael Niedermayer
}
2638
2639
/**
2640 2ba8f6b8 Michael Niedermayer
 * gets the average motion vector for a GMC MB.
2641 073b013d Michael Niedermayer
 * @param n either 0 for the x component or 1 for y
2642
 * @returns the average MV for a GMC MB
2643
 */
2644
static inline int get_amv(MpegEncContext *s, int n){
2645
    int x, y, mb_v, sum, dx, dy, shift;
2646
    int len = 1 << (s->f_code + 4);
2647
    const int a= s->sprite_warping_accuracy;
2648
2649
    if(s->real_sprite_warping_points==1){
2650
        if(s->divx_version==500 && s->divx_build==413)
2651
            sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
2652
        else
2653
            sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
2654
    }else{
2655
        dx= s->sprite_delta[n][0];
2656
        dy= s->sprite_delta[n][1];
2657
        shift= s->sprite_shift[0];
2658
        if(n) dy -= 1<<(shift + a + 1);
2659
        else  dx -= 1<<(shift + a + 1);
2660
        mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
2661
2662
        sum=0;
2663
        for(y=0; y<16; y++){
2664
            int v;
2665
        
2666
            v= mb_v + dy*y;
2667
            //XXX FIXME optimize
2668
            for(x=0; x<16; x++){
2669
                sum+= v>>shift;
2670
                v+= dx;
2671
            }
2672
        }
2673 92148550 Michael Niedermayer
        sum= RSHIFT(sum, a+8-s->quarter_sample);
2674 073b013d Michael Niedermayer
    }
2675
2676
    if      (sum < -len) sum= -len;
2677
    else if (sum >= len) sum= len-1;
2678
2679
    return sum;
2680
}
2681
2682
/**
2683 4d2858de Michael Niedermayer
 * decodes first partition.
2684
 * @return number of MBs decoded or <0 if an error occured
2685 92073d01 Michael Niedermayer
 */
2686 4d2858de Michael Niedermayer
static int mpeg4_decode_partition_a(MpegEncContext *s){
2687 92073d01 Michael Niedermayer
    int mb_num;
2688 0c1a9eda Zdenek Kabelac
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
2689 92073d01 Michael Niedermayer
    
2690
    /* decode first partition */
2691
    mb_num=0;
2692 49092244 Michael Niedermayer
    s->first_slice_line=1;
2693 4d2858de Michael Niedermayer
    for(; s->mb_y<s->mb_height; s->mb_y++){
2694
        ff_init_block_index(s);
2695
        for(; s->mb_x<s->mb_width; s->mb_x++){
2696 7bc9090a Michael Niedermayer
            const int xy= s->mb_x + s->mb_y*s->mb_stride;
2697 92073d01 Michael Niedermayer
            int cbpc;
2698
            int dir=0;
2699
            
2700
            mb_num++;
2701 4d2858de Michael Niedermayer
            ff_update_block_index(s);
2702 92073d01 Michael Niedermayer
            if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
2703
                s->first_slice_line=0;
2704
            
2705
            if(s->pict_type==I_TYPE){
2706
                int i;
2707
2708 17fb5fd3 Michael Niedermayer
                if(show_bits_long(&s->gb, 19)==DC_MARKER){
2709 4d2858de Michael Niedermayer
                    return mb_num-1;
2710
                }
2711
2712 edcf9890 Michael Niedermayer
                cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2713 92073d01 Michael Niedermayer
                if (cbpc < 0){
2714 4d2858de Michael Niedermayer
2715 92073d01 Michael Niedermayer
                    fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2716 4d2858de Michael Niedermayer
                    return -1;
2717 92073d01 Michael Niedermayer
                }
2718
                s->cbp_table[xy]= cbpc & 3;
2719 7bc9090a Michael Niedermayer
                s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
2720 92073d01 Michael Niedermayer
                s->mb_intra = 1;
2721
2722
                if(cbpc & 4) {
2723 5482970b Michael Niedermayer
                    change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
2724 92073d01 Michael Niedermayer
                }
2725 1e491e29 Michael Niedermayer
                s->current_picture.qscale_table[xy]= s->qscale;
2726 92073d01 Michael Niedermayer
2727
                s->mbintra_table[xy]= 1;
2728
                for(i=0; i<6; i++){
2729
                    int dc_pred_dir;
2730
                    int dc= mpeg4_decode_dc(s, i, &dc_pred_dir); 
2731
                    if(dc < 0){
2732
                        fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2733 4d2858de Michael Niedermayer
                        return -1;
2734 92073d01 Michael Niedermayer
                    }
2735
                    dir<<=1;
2736
                    if(dc_pred_dir) dir|=1;
2737
                }
2738
                s->pred_dir_table[xy]= dir;
2739
            }else{ /* P/S_TYPE */
2740 4d2858de Michael Niedermayer
                int mx, my, pred_x, pred_y, bits;
2741 0c1a9eda Zdenek Kabelac
                int16_t * const mot_val= s->motion_val[s->block_index[0]];
2742 92073d01 Michael Niedermayer
                const int stride= s->block_wrap[0]*2;
2743
2744 4d2858de Michael Niedermayer
                bits= show_bits(&s->gb, 17);
2745
                if(bits==MOTION_MARKER){
2746
                    return mb_num-1;
2747
                }
2748
                skip_bits1(&s->gb);
2749
                if(bits&0x10000){
2750 92073d01 Michael Niedermayer
                    /* skip mb */
2751
                    if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2752 7bc9090a Michael Niedermayer
                        s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
2753 073b013d Michael Niedermayer
                        mx= get_amv(s, 0);
2754
                        my= get_amv(s, 1);
2755 92073d01 Michael Niedermayer
                    }