Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpeg12.c @ 2d974017

History | View | Annotate | Download (113 KB)

1 de6d9b64 Fabrice Bellard
/*
2 983e3246 Michael Niedermayer
 * MPEG1 codec / MPEG2 decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 115329f1 Diego Biurrun
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 de6d9b64 Fabrice Bellard
 *
6 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10 de6d9b64 Fabrice Bellard
 *
11 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
12 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15 de6d9b64 Fabrice Bellard
 *
16 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 de6d9b64 Fabrice Bellard
 */
20 115329f1 Diego Biurrun
21 983e3246 Michael Niedermayer
/**
22
 * @file mpeg12.c
23 029911d1 Michael Niedermayer
 * MPEG1/2 codec
24 983e3246 Michael Niedermayer
 */
25 115329f1 Diego Biurrun
26 a74127c0 Fabrice Bellard
//#define DEBUG
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30
31
#include "mpeg12data.h"
32
33 bb198e19 Michael Niedermayer
//#undef NDEBUG
34
//#include <assert.h>
35
36 d87c0267 Michael Niedermayer
37 de6d9b64 Fabrice Bellard
/* Start codes. */
38 bb270c08 Diego Biurrun
#define SEQ_END_CODE            0x000001b7
39
#define SEQ_START_CODE          0x000001b3
40
#define GOP_START_CODE          0x000001b8
41
#define PICTURE_START_CODE      0x00000100
42
#define SLICE_MIN_START_CODE    0x00000101
43
#define SLICE_MAX_START_CODE    0x000001af
44
#define EXT_START_CODE          0x000001b5
45
#define USER_START_CODE         0x000001b2
46 de6d9b64 Fabrice Bellard
47 4f68b084 Michael Niedermayer
#define DC_VLC_BITS 9
48
#define MV_VLC_BITS 9
49
#define MBINCR_VLC_BITS 9
50
#define MB_PAT_VLC_BITS 9
51
#define MB_PTYPE_VLC_BITS 6
52
#define MB_BTYPE_VLC_BITS 6
53
#define TEX_VLC_BITS 9
54
55 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
56 115329f1 Diego Biurrun
static void mpeg1_encode_block(MpegEncContext *s,
57
                         DCTELEM *block,
58 de6d9b64 Fabrice Bellard
                         int component);
59 63b15e55 Michael Niedermayer
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code);    // RAL: f_code parameter added
60 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
61 115329f1 Diego Biurrun
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
62
                              DCTELEM *block,
63 a0201736 Michael Niedermayer
                              int n);
64 115329f1 Diego Biurrun
static inline int mpeg1_decode_block_intra(MpegEncContext *s,
65
                              DCTELEM *block,
66 de6d9b64 Fabrice Bellard
                              int n);
67 628b210f Michael Niedermayer
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
68 115329f1 Diego Biurrun
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
69
                                        DCTELEM *block,
70 de6d9b64 Fabrice Bellard
                                        int n);
71 115329f1 Diego Biurrun
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
72
                                    DCTELEM *block,
73 de6d9b64 Fabrice Bellard
                                    int n);
74 6fc5b059 Michael Niedermayer
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
75 dff0f035 Michael Niedermayer
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
76 de6d9b64 Fabrice Bellard
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
77 57489bc5 Ivan Kalvachev
static void exchange_uv(MpegEncContext *s);
78 de6d9b64 Fabrice Bellard
79 2e7b4c84 Ivan Kalvachev
#ifdef HAVE_XVMC
80
extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
81
extern int XVMC_field_end(MpegEncContext *s);
82 a579db0c Ivan Kalvachev
extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
83 57489bc5 Ivan Kalvachev
extern void XVMC_init_block(MpegEncContext *s);//set s->block
84 2e7b4c84 Ivan Kalvachev
#endif
85
86 530d5740 Ivan Kalvachev
const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
87
const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
88
const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
89
const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
90
                                           PIX_FMT_XVMC_MPEG2_IDCT,
91
                                           PIX_FMT_XVMC_MPEG2_MC,
92 bb270c08 Diego Biurrun
                                           -1};
93 c442d75c Michael Niedermayer
#ifdef CONFIG_ENCODERS
94 30952237 Michael Niedermayer
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
95 0c1a9eda Zdenek Kabelac
static uint8_t fcode_tab[MAX_MV*2+1];
96 11ce8834 Michael Niedermayer
97 c442d75c Michael Niedermayer
static uint8_t  uni_mpeg1_ac_vlc_len [64*64*2];
98 307eb248 Baptiste Coudurier
static uint8_t  uni_mpeg2_ac_vlc_len [64*64*2];
99 2a250222 Michael Niedermayer
100
/* simple include everything table for dc, first byte is bits number next 3 are code*/
101
static uint32_t mpeg1_lum_dc_uni[512];
102
static uint32_t mpeg1_chr_dc_uni[512];
103
104
static uint8_t mpeg1_index_run[2][64];
105
static int8_t mpeg1_max_level[2][64];
106 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
107 c442d75c Michael Niedermayer
108 073c2593 Burkhard Plaum
static void init_2d_vlc_rl(RLTable *rl, int use_static)
109 4f68b084 Michael Niedermayer
{
110 07787186 Dominik Mierzejewski
    int i;
111 115329f1 Diego Biurrun
112
    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
113 4f68b084 Michael Niedermayer
             &rl->table_vlc[0][1], 4, 2,
114 073c2593 Burkhard Plaum
             &rl->table_vlc[0][0], 4, 2, use_static);
115
116 115329f1 Diego Biurrun
    if(use_static)
117 073c2593 Burkhard Plaum
        rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
118
    else
119
        rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
120 4f68b084 Michael Niedermayer
121
    for(i=0; i<rl->vlc.table_size; i++){
122
        int code= rl->vlc.table[i][0];
123
        int len = rl->vlc.table[i][1];
124
        int level, run;
125 115329f1 Diego Biurrun
126 4f68b084 Michael Niedermayer
        if(len==0){ // illegal code
127
            run= 65;
128
            level= MAX_LEVEL;
129
        }else if(len<0){ //more bits needed
130
            run= 0;
131
            level= code;
132
        }else{
133
            if(code==rl->n){ //esc
134
                run= 65;
135
                level= 0;
136
            }else if(code==rl->n+1){ //eob
137 a0201736 Michael Niedermayer
                run= 0;
138
                level= 127;
139 4f68b084 Michael Niedermayer
            }else{
140
                run=   rl->table_run  [code] + 1;
141
                level= rl->table_level[code];
142
            }
143
        }
144
        rl->rl_vlc[0][i].len= len;
145
        rl->rl_vlc[0][i].level= level;
146
        rl->rl_vlc[0][i].run= run;
147
    }
148
}
149
150 2a250222 Michael Niedermayer
#ifdef CONFIG_ENCODERS
151 d634aa20 Baptiste Coudurier
static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
152 c442d75c Michael Niedermayer
    int i;
153
154
    for(i=0; i<128; i++){
155
        int level= i-64;
156
        int run;
157
        for(run=0; run<64; run++){
158
            int len, bits, code;
159 115329f1 Diego Biurrun
160 c442d75c Michael Niedermayer
            int alevel= ABS(level);
161
            int sign= (level>>31)&1;
162
163
            if (alevel > rl->max_level[0][run])
164
                code= 111; /*rl->n*/
165
            else
166
                code= rl->index_run[0][run] + alevel - 1;
167
168
            if (code < 111 /* rl->n */) {
169 bb270c08 Diego Biurrun
                /* store the vlc & sign at once */
170 307eb248 Baptiste Coudurier
                len=   rl->table_vlc[code][1]+1;
171
                bits= (rl->table_vlc[code][0]<<1) + sign;
172 c442d75c Michael Niedermayer
            } else {
173 307eb248 Baptiste Coudurier
                len=  rl->table_vlc[111/*rl->n*/][1]+6;
174
                bits= rl->table_vlc[111/*rl->n*/][0]<<6;
175 c442d75c Michael Niedermayer
176
                bits|= run;
177
                if (alevel < 128) {
178
                    bits<<=8; len+=8;
179
                    bits|= level & 0xff;
180
                } else {
181
                    bits<<=16; len+=16;
182
                    bits|= level & 0xff;
183
                    if (level < 0) {
184
                        bits|= 0x8001 + level + 255;
185
                    } else {
186
                        bits|= level & 0xffff;
187
                    }
188
                }
189
            }
190
191
            uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
192
        }
193
    }
194
}
195 4f68b084 Michael Niedermayer
196 2c492e94 Michael Niedermayer
197
static int find_frame_rate_index(MpegEncContext *s){
198
    int i;
199
    int64_t dmin= INT64_MAX;
200
    int64_t d;
201
202
    for(i=1;i<14;i++) {
203 c0df9d75 Michael Niedermayer
        int64_t n0= 1001LL/frame_rate_tab[i].den*frame_rate_tab[i].num*s->avctx->time_base.num;
204
        int64_t n1= 1001LL*s->avctx->time_base.den;
205 9cd81798 Michael Niedermayer
        if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
206 8d52ec7e Michael Niedermayer
207
        d = ABS(n0 - n1);
208 2c492e94 Michael Niedermayer
        if(d < dmin){
209
            dmin=d;
210
            s->frame_rate_index= i;
211
        }
212
    }
213
    if(dmin)
214
        return -1;
215
    else
216
        return 0;
217
}
218
219
static int encode_init(AVCodecContext *avctx)
220
{
221
    MpegEncContext *s = avctx->priv_data;
222
223
    if(MPV_encode_init(avctx) < 0)
224
        return -1;
225
226
    if(find_frame_rate_index(s) < 0){
227 9cd81798 Michael Niedermayer
        if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
228 c0df9d75 Michael Niedermayer
            av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
229 2c492e94 Michael Niedermayer
            return -1;
230
        }else{
231 c0df9d75 Michael Niedermayer
            av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
232 2c492e94 Michael Niedermayer
        }
233
    }
234 115329f1 Diego Biurrun
235 2d974017 Baptiste Coudurier
    if(avctx->profile == FF_PROFILE_UNKNOWN)
236
        avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
237
238
    if(avctx->level == FF_LEVEL_UNKNOWN)
239
        avctx->level = s->chroma_format == CHROMA_420 ? 8 : 5;
240
241 2c492e94 Michael Niedermayer
    return 0;
242
}
243
244 de6d9b64 Fabrice Bellard
static void put_header(MpegEncContext *s, int header)
245
{
246
    align_put_bits(&s->pb);
247 80feb2a2 Michael Niedermayer
    put_bits(&s->pb, 16, header>>16);
248
    put_bits(&s->pb, 16, header&0xFFFF);
249 de6d9b64 Fabrice Bellard
}
250
251
/* put sequence header if needed */
252
static void mpeg1_encode_sequence_header(MpegEncContext *s)
253
{
254
        unsigned int vbv_buffer_size;
255 d78647e8 Fabrice Bellard
        unsigned int fps, v;
256 7906085f Falk Hüffner
        int i;
257 0c1a9eda Zdenek Kabelac
        uint64_t time_code;
258 426b8061 Michael Niedermayer
        float best_aspect_error= 1E10;
259 5ff85f1d Michael Niedermayer
        float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
260 98b0858e Michael Niedermayer
        int constraint_parameter_flag;
261 115329f1 Diego Biurrun
262 5ff85f1d Michael Niedermayer
        if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
263 115329f1 Diego Biurrun
264 1e491e29 Michael Niedermayer
        if (s->current_picture.key_frame) {
265 8d52ec7e Michael Niedermayer
            AVRational framerate= frame_rate_tab[s->frame_rate_index];
266
267 de6d9b64 Fabrice Bellard
            /* mpeg1 header repeated every gop */
268
            put_header(s, SEQ_START_CODE);
269 115329f1 Diego Biurrun
270 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 12, s->width);
271
            put_bits(&s->pb, 12, s->height);
272 115329f1 Diego Biurrun
273 426b8061 Michael Niedermayer
            for(i=1; i<15; i++){
274 5c9e4723 Michael Niedermayer
                float error= aspect_ratio;
275
                if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
276 f259747b Michael Niedermayer
                    error-= 1.0/mpeg1_aspect[i];
277 5c9e4723 Michael Niedermayer
                else
278
                    error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
279 115329f1 Diego Biurrun
280 426b8061 Michael Niedermayer
                error= ABS(error);
281 115329f1 Diego Biurrun
282 426b8061 Michael Niedermayer
                if(error < best_aspect_error){
283
                    best_aspect_error= error;
284
                    s->aspect_ratio_info= i;
285
                }
286
            }
287 115329f1 Diego Biurrun
288 426b8061 Michael Niedermayer
            put_bits(&s->pb, 4, s->aspect_ratio_info);
289 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 4, s->frame_rate_index);
290 115329f1 Diego Biurrun
291 12dccd4e Michael Niedermayer
            if(s->avctx->rc_max_rate){
292
                v = (s->avctx->rc_max_rate + 399) / 400;
293
                if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
294
                    v = 0x3ffff;
295
            }else{
296
                v= 0x3FFFF;
297
            }
298 ecfd40b3 Henry Mason
299
            if(s->avctx->rc_buffer_size)
300
                vbv_buffer_size = s->avctx->rc_buffer_size;
301
            else
302
                /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
303 12dccd4e Michael Niedermayer
                vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
304
            vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
305
306
            put_bits(&s->pb, 18, v & 0x3FFFF);
307
            put_bits(&s->pb, 1, 1); /* marker */
308 98b0858e Michael Niedermayer
            put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
309
310 115329f1 Diego Biurrun
            constraint_parameter_flag=
311
                s->width <= 768 && s->height <= 576 &&
312 98b0858e Michael Niedermayer
                s->mb_width * s->mb_height <= 396 &&
313 8d52ec7e Michael Niedermayer
                s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
314
                framerate.num <= framerate.den*30 &&
315 7494cac0 Michael Niedermayer
                s->avctx->me_range && s->avctx->me_range < 128 &&
316 98b0858e Michael Niedermayer
                vbv_buffer_size <= 20 &&
317
                v <= 1856000/400 &&
318
                s->codec_id == CODEC_ID_MPEG1VIDEO;
319 115329f1 Diego Biurrun
320 98b0858e Michael Niedermayer
            put_bits(&s->pb, 1, constraint_parameter_flag);
321 115329f1 Diego Biurrun
322 d6eb3c50 Michael Niedermayer
            ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
323
            ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
324 de6d9b64 Fabrice Bellard
325 029911d1 Michael Niedermayer
            if(s->codec_id == CODEC_ID_MPEG2VIDEO){
326
                put_header(s, EXT_START_CODE);
327
                put_bits(&s->pb, 4, 1); //seq ext
328 115329f1 Diego Biurrun
329 2d974017 Baptiste Coudurier
                put_bits(&s->pb, 1, s->chroma_format == CHROMA_422); //escx
330 baced9f5 Michael Niedermayer
331 2d974017 Baptiste Coudurier
                put_bits(&s->pb, 3, s->avctx->profile); //profile
332
                put_bits(&s->pb, 4, s->avctx->level); //level
333 baced9f5 Michael Niedermayer
334 3ab5b8cc Fabrice Bellard
                put_bits(&s->pb, 1, s->progressive_sequence);
335 2d974017 Baptiste Coudurier
                put_bits(&s->pb, 2, s->chroma_format);
336 029911d1 Michael Niedermayer
                put_bits(&s->pb, 2, 0); //horizontal size ext
337
                put_bits(&s->pb, 2, 0); //vertical size ext
338
                put_bits(&s->pb, 12, v>>18); //bitrate ext
339
                put_bits(&s->pb, 1, 1); //marker
340
                put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
341
                put_bits(&s->pb, 1, s->low_delay);
342
                put_bits(&s->pb, 2, 0); // frame_rate_ext_n
343
                put_bits(&s->pb, 5, 0); // frame_rate_ext_d
344
            }
345 115329f1 Diego Biurrun
346 de6d9b64 Fabrice Bellard
            put_header(s, GOP_START_CODE);
347
            put_bits(&s->pb, 1, 0); /* do drop frame */
348
            /* time code : we must convert from the real frame rate to a
349
               fake mpeg frame rate in case of low frame rate */
350 8d52ec7e Michael Niedermayer
            fps = (framerate.num + framerate.den/2)/ framerate.den;
351 2c492e94 Michael Niedermayer
            time_code = s->current_picture_ptr->coded_picture_number;
352
353
            s->gop_picture_number = time_code;
354 0c1a9eda Zdenek Kabelac
            put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
355
            put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
356 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 1, 1);
357 0c1a9eda Zdenek Kabelac
            put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
358 b1b8f9fc Michael Niedermayer
            put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
359 303e50e6 Michael Niedermayer
            put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
360 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 1, 0); /* broken link */
361
        }
362
}
363
364 9b8709d1 Michael Niedermayer
static inline void encode_mb_skip_run(MpegEncContext *s, int run){
365
    while (run >= 33) {
366
        put_bits(&s->pb, 11, 0x008);
367
        run -= 33;
368
    }
369 115329f1 Diego Biurrun
    put_bits(&s->pb, mbAddrIncrTable[run][1],
370 9b8709d1 Michael Niedermayer
             mbAddrIncrTable[run][0]);
371
}
372 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
373 de6d9b64 Fabrice Bellard
374 8f8402e4 Michael Niedermayer
static void common_init(MpegEncContext *s)
375 de6d9b64 Fabrice Bellard
{
376 a6b9ffbf Ivan Kalvachev
377 8f8402e4 Michael Niedermayer
    s->y_dc_scale_table=
378 bf266e19 Michael Niedermayer
    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
379 8fe2c60e Michael Niedermayer
380 8f8402e4 Michael Niedermayer
}
381 f004ca1c Michael Niedermayer
382 2a250222 Michael Niedermayer
void ff_mpeg1_clean_buffers(MpegEncContext *s){
383
    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
384
    s->last_dc[1] = s->last_dc[0];
385
    s->last_dc[2] = s->last_dc[0];
386
    memset(s->last_mv, 0, sizeof(s->last_mv));
387
}
388
389 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
390 9b8709d1 Michael Niedermayer
391
void ff_mpeg1_encode_slice_header(MpegEncContext *s){
392
    put_header(s, SLICE_MIN_START_CODE + s->mb_y);
393
    put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
394
    put_bits(&s->pb, 1, 0); /* slice extra information */
395
}
396
397 8f8402e4 Michael Niedermayer
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
398
{
399 de6d9b64 Fabrice Bellard
    mpeg1_encode_sequence_header(s);
400
401
    /* mpeg1 picture header */
402
    put_header(s, PICTURE_START_CODE);
403
    /* temporal reference */
404 63b15e55 Michael Niedermayer
405
    // RAL: s->picture_number instead of s->fake_picture_number
406 115329f1 Diego Biurrun
    put_bits(&s->pb, 10, (s->picture_number -
407
                          s->gop_picture_number) & 0x3ff);
408 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 3, s->pict_type);
409 d60a8f85 Michael Niedermayer
410 fe455f33 Alex Beregszaszi
    s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
411 d60a8f85 Michael Niedermayer
    put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
412 115329f1 Diego Biurrun
413 63b15e55 Michael Niedermayer
    // RAL: Forward f_code also needed for B frames
414
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
415 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
416 cb231c48 Michael Niedermayer
        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
417
            put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
418
        else
419
            put_bits(&s->pb, 3, 7); /* forward_f_code */
420 de6d9b64 Fabrice Bellard
    }
421 115329f1 Diego Biurrun
422 63b15e55 Michael Niedermayer
    // RAL: Backward f_code necessary for B frames
423
    if (s->pict_type == B_TYPE) {
424
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
425 cb231c48 Michael Niedermayer
        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
426
            put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
427
        else
428
            put_bits(&s->pb, 3, 7); /* backward_f_code */
429 029911d1 Michael Niedermayer
    }
430 63b15e55 Michael Niedermayer
431 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 1, 0); /* extra bit picture */
432 3ab5b8cc Fabrice Bellard
433
    s->frame_pred_frame_dct = 1;
434 029911d1 Michael Niedermayer
    if(s->codec_id == CODEC_ID_MPEG2VIDEO){
435
        put_header(s, EXT_START_CODE);
436
        put_bits(&s->pb, 4, 8); //pic ext
437 cb231c48 Michael Niedermayer
        if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
438
            put_bits(&s->pb, 4, s->f_code);
439
            put_bits(&s->pb, 4, s->f_code);
440
        }else{
441
            put_bits(&s->pb, 8, 255);
442
        }
443
        if (s->pict_type == B_TYPE) {
444
            put_bits(&s->pb, 4, s->b_code);
445
            put_bits(&s->pb, 4, s->b_code);
446
        }else{
447
            put_bits(&s->pb, 8, 255);
448
        }
449 029911d1 Michael Niedermayer
        put_bits(&s->pb, 2, s->intra_dc_precision);
450 115329f1 Diego Biurrun
451 9c3d33d6 Michael Niedermayer
        assert(s->picture_structure == PICT_FRAME);
452
        put_bits(&s->pb, 2, s->picture_structure);
453 7a0f9d7e Fabrice Bellard
        if (s->progressive_sequence) {
454
            put_bits(&s->pb, 1, 0); /* no repeat */
455
        } else {
456
            put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
457
        }
458 3ab5b8cc Fabrice Bellard
        /* XXX: optimize the generation of this flag with entropy
459
           measures */
460
        s->frame_pred_frame_dct = s->progressive_sequence;
461 115329f1 Diego Biurrun
462 3ab5b8cc Fabrice Bellard
        put_bits(&s->pb, 1, s->frame_pred_frame_dct);
463 029911d1 Michael Niedermayer
        put_bits(&s->pb, 1, s->concealment_motion_vectors);
464
        put_bits(&s->pb, 1, s->q_scale_type);
465
        put_bits(&s->pb, 1, s->intra_vlc_format);
466
        put_bits(&s->pb, 1, s->alternate_scan);
467
        put_bits(&s->pb, 1, s->repeat_first_field);
468 3ab5b8cc Fabrice Bellard
        s->progressive_frame = s->progressive_sequence;
469 2d974017 Baptiste Coudurier
        put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
470 3ab5b8cc Fabrice Bellard
        put_bits(&s->pb, 1, s->progressive_frame);
471 029911d1 Michael Niedermayer
        put_bits(&s->pb, 1, 0); //composite_display_flag
472
    }
473 baaf3f46 Michael Niedermayer
    if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
474
        int i;
475
476
        put_header(s, USER_START_CODE);
477
        for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
478
            put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
479
        }
480
    }
481 115329f1 Diego Biurrun
482 9b8709d1 Michael Niedermayer
    s->mb_y=0;
483
    ff_mpeg1_encode_slice_header(s);
484 de6d9b64 Fabrice Bellard
}
485
486 115329f1 Diego Biurrun
static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
487 bb198e19 Michael Niedermayer
                                int has_mv, int field_motion)
488 3ab5b8cc Fabrice Bellard
{
489
    put_bits(&s->pb, n, bits);
490
    if (!s->frame_pred_frame_dct) {
491 115329f1 Diego Biurrun
        if (has_mv)
492 bb198e19 Michael Niedermayer
            put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
493 3ab5b8cc Fabrice Bellard
        put_bits(&s->pb, 1, s->interlaced_dct);
494
    }
495
}
496
497 2d974017 Baptiste Coudurier
static always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
498
                                                   DCTELEM block[6][64],
499
                                                   int motion_x, int motion_y,
500
                                                   int mb_block_count)
501 de6d9b64 Fabrice Bellard
{
502 9b8709d1 Michael Niedermayer
    int i, cbp;
503
    const int mb_x = s->mb_x;
504
    const int mb_y = s->mb_y;
505
    const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
506 de6d9b64 Fabrice Bellard
507
    /* compute cbp */
508
    cbp = 0;
509 2d974017 Baptiste Coudurier
    for(i=0;i<mb_block_count;i++) {
510 de6d9b64 Fabrice Bellard
        if (s->block_last_index[i] >= 0)
511 2d974017 Baptiste Coudurier
            cbp |= 1 << (mb_block_count - 1 - i);
512 de6d9b64 Fabrice Bellard
    }
513 115329f1 Diego Biurrun
514 86807410 Michael Niedermayer
    if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
515 115329f1 Diego Biurrun
        (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
516 86807410 Michael Niedermayer
        ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
517 63b15e55 Michael Niedermayer
        (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
518
        ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
519 9b8709d1 Michael Niedermayer
        s->mb_skip_run++;
520 d87c0267 Michael Niedermayer
        s->qscale -= s->dquant;
521 49733979 Michael Niedermayer
        s->skip_count++;
522
        s->misc_bits++;
523
        s->last_bits++;
524 bb198e19 Michael Niedermayer
        if(s->pict_type == P_TYPE){
525 115329f1 Diego Biurrun
            s->last_mv[0][1][0]= s->last_mv[0][0][0]=
526 bb198e19 Michael Niedermayer
            s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
527
        }
528 de6d9b64 Fabrice Bellard
    } else {
529 9b8709d1 Michael Niedermayer
        if(first_mb){
530
            assert(s->mb_skip_run == 0);
531
            encode_mb_skip_run(s, s->mb_x);
532
        }else{
533
            encode_mb_skip_run(s, s->mb_skip_run);
534 de6d9b64 Fabrice Bellard
        }
535 115329f1 Diego Biurrun
536 de6d9b64 Fabrice Bellard
        if (s->pict_type == I_TYPE) {
537 d87c0267 Michael Niedermayer
            if(s->dquant && cbp){
538 bb198e19 Michael Niedermayer
                put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
539 d87c0267 Michael Niedermayer
                put_bits(&s->pb, 5, s->qscale);
540
            }else{
541 bb198e19 Michael Niedermayer
                put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
542 d87c0267 Michael Niedermayer
                s->qscale -= s->dquant;
543
            }
544 49733979 Michael Niedermayer
            s->misc_bits+= get_bits_diff(s);
545
            s->i_count++;
546 63b15e55 Michael Niedermayer
        } else if (s->mb_intra) {
547
            if(s->dquant && cbp){
548 bb198e19 Michael Niedermayer
                put_mb_modes(s, 6, 0x01, 0, 0);
549 63b15e55 Michael Niedermayer
                put_bits(&s->pb, 5, s->qscale);
550
            }else{
551 bb198e19 Michael Niedermayer
                put_mb_modes(s, 5, 0x03, 0, 0);
552 63b15e55 Michael Niedermayer
                s->qscale -= s->dquant;
553
            }
554
            s->misc_bits+= get_bits_diff(s);
555
            s->i_count++;
556 bb198e19 Michael Niedermayer
            memset(s->last_mv, 0, sizeof(s->last_mv));
557 115329f1 Diego Biurrun
        } else if (s->pict_type == P_TYPE) {
558 bb198e19 Michael Niedermayer
            if(s->mv_type == MV_TYPE_16X16){
559 de6d9b64 Fabrice Bellard
                if (cbp != 0) {
560 bb198e19 Michael Niedermayer
                    if ((motion_x|motion_y) == 0) {
561 d87c0267 Michael Niedermayer
                        if(s->dquant){
562 bb198e19 Michael Niedermayer
                            put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
563 d87c0267 Michael Niedermayer
                            put_bits(&s->pb, 5, s->qscale);
564
                        }else{
565 bb198e19 Michael Niedermayer
                            put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
566 d87c0267 Michael Niedermayer
                        }
567 49733979 Michael Niedermayer
                        s->misc_bits+= get_bits_diff(s);
568 de6d9b64 Fabrice Bellard
                    } else {
569 d87c0267 Michael Niedermayer
                        if(s->dquant){
570 bb198e19 Michael Niedermayer
                            put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
571 d87c0267 Michael Niedermayer
                            put_bits(&s->pb, 5, s->qscale);
572
                        }else{
573 bb198e19 Michael Niedermayer
                            put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
574 d87c0267 Michael Niedermayer
                        }
575 49733979 Michael Niedermayer
                        s->misc_bits+= get_bits_diff(s);
576 63b15e55 Michael Niedermayer
                        mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
577
                        mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
578 49733979 Michael Niedermayer
                        s->mv_bits+= get_bits_diff(s);
579 de6d9b64 Fabrice Bellard
                    }
580
                } else {
581
                    put_bits(&s->pb, 3, 1); /* motion only */
582 3ab5b8cc Fabrice Bellard
                    if (!s->frame_pred_frame_dct)
583
                        put_bits(&s->pb, 2, 2); /* motion_type: frame */
584 bb198e19 Michael Niedermayer
                    s->misc_bits+= get_bits_diff(s);
585 63b15e55 Michael Niedermayer
                    mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
586
                    mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
587 d87c0267 Michael Niedermayer
                    s->qscale -= s->dquant;
588 49733979 Michael Niedermayer
                    s->mv_bits+= get_bits_diff(s);
589 de6d9b64 Fabrice Bellard
                }
590 bb198e19 Michael Niedermayer
                s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
591
                s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
592
            }else{
593
                assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
594
595
                if (cbp) {
596
                    if(s->dquant){
597
                        put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
598
                        put_bits(&s->pb, 5, s->qscale);
599
                    }else{
600
                        put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
601 63b15e55 Michael Niedermayer
                    }
602 bb198e19 Michael Niedermayer
                } else {
603
                    put_bits(&s->pb, 3, 1); /* motion only */
604
                    put_bits(&s->pb, 2, 1); /* motion_type: field */
605 63b15e55 Michael Niedermayer
                    s->qscale -= s->dquant;
606 bb198e19 Michael Niedermayer
                }
607
                s->misc_bits+= get_bits_diff(s);
608
                for(i=0; i<2; i++){
609
                    put_bits(&s->pb, 1, s->field_select[0][i]);
610
                    mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
611
                    mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
612
                    s->last_mv[0][i][0]=   s->mv[0][i][0];
613
                    s->last_mv[0][i][1]= 2*s->mv[0][i][1];
614
                }
615
                s->mv_bits+= get_bits_diff(s);
616
            }
617 2d974017 Baptiste Coudurier
            if(cbp) {
618
                if (s->chroma_y_shift) {
619
                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
620
                } else {
621
                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
622
                    put_bits(&s->pb, 2, cbp & 3);
623
                }
624
            }
625 bb198e19 Michael Niedermayer
            s->f_count++;
626 115329f1 Diego Biurrun
        } else{
627 bb198e19 Michael Niedermayer
            static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
628
629
            if(s->mv_type == MV_TYPE_16X16){
630
                if (cbp){    // With coded bloc pattern
631
                    if (s->dquant) {
632
                        if(s->mv_dir == MV_DIR_FORWARD)
633
                            put_mb_modes(s, 6, 3, 1, 0);
634
                        else
635
                            put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
636
                        put_bits(&s->pb, 5, s->qscale);
637
                    } else {
638
                        put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
639
                    }
640
                }else{    // No coded bloc pattern
641
                    put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
642
                    if (!s->frame_pred_frame_dct)
643
                        put_bits(&s->pb, 2, 2); /* motion_type: frame */
644
                    s->qscale -= s->dquant;
645
                }
646
                s->misc_bits += get_bits_diff(s);
647
                if (s->mv_dir&MV_DIR_FORWARD){
648 115329f1 Diego Biurrun
                    mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
649
                    mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
650 bb198e19 Michael Niedermayer
                    s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
651
                    s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
652
                    s->f_count++;
653
                }
654
                if (s->mv_dir&MV_DIR_BACKWARD){
655 115329f1 Diego Biurrun
                    mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
656
                    mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
657 bb198e19 Michael Niedermayer
                    s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
658
                    s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
659
                    s->b_count++;
660
                }
661
            }else{
662
                assert(s->mv_type == MV_TYPE_FIELD);
663
                assert(!s->frame_pred_frame_dct);
664
                if (cbp){    // With coded bloc pattern
665
                    if (s->dquant) {
666
                        if(s->mv_dir == MV_DIR_FORWARD)
667
                            put_mb_modes(s, 6, 3, 1, 1);
668
                        else
669
                            put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
670
                        put_bits(&s->pb, 5, s->qscale);
671
                    } else {
672
                        put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
673 63b15e55 Michael Niedermayer
                    }
674 bb198e19 Michael Niedermayer
                }else{    // No coded bloc pattern
675
                    put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
676
                    put_bits(&s->pb, 2, 1); /* motion_type: field */
677
                    s->qscale -= s->dquant;
678
                }
679
                s->misc_bits += get_bits_diff(s);
680
                if (s->mv_dir&MV_DIR_FORWARD){
681
                    for(i=0; i<2; i++){
682
                        put_bits(&s->pb, 1, s->field_select[0][i]);
683
                        mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
684
                        mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
685
                        s->last_mv[0][i][0]=   s->mv[0][i][0];
686
                        s->last_mv[0][i][1]= 2*s->mv[0][i][1];
687
                    }
688
                    s->f_count++;
689
                }
690
                if (s->mv_dir&MV_DIR_BACKWARD){
691
                    for(i=0; i<2; i++){
692
                        put_bits(&s->pb, 1, s->field_select[1][i]);
693
                        mpeg1_encode_motion(s, s->mv[1][i][0] -  s->last_mv[1][i][0]    , s->b_code);
694
                        mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
695
                        s->last_mv[1][i][0]=   s->mv[1][i][0];
696
                        s->last_mv[1][i][1]= 2*s->mv[1][i][1];
697
                    }
698
                    s->b_count++;
699
                }
700 de6d9b64 Fabrice Bellard
            }
701 bb198e19 Michael Niedermayer
            s->mv_bits += get_bits_diff(s);
702 2d974017 Baptiste Coudurier
            if(cbp) {
703
                if (s->chroma_y_shift) {
704
                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
705
                } else {
706
                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
707
                    put_bits(&s->pb, 2, cbp & 3);
708
                }
709
            }
710 bb198e19 Michael Niedermayer
        }
711 2d974017 Baptiste Coudurier
        for(i=0;i<mb_block_count;i++) {
712
            if (cbp & (1 << (mb_block_count - 1 - i))) {
713 de6d9b64 Fabrice Bellard
                mpeg1_encode_block(s, block[i], i);
714
            }
715
        }
716 9b8709d1 Michael Niedermayer
        s->mb_skip_run = 0;
717 49733979 Michael Niedermayer
        if(s->mb_intra)
718
            s->i_tex_bits+= get_bits_diff(s);
719
        else
720
            s->p_tex_bits+= get_bits_diff(s);
721 de6d9b64 Fabrice Bellard
    }
722
}
723
724 2d974017 Baptiste Coudurier
void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
725
{
726
    if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
727
    else                                mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
728
}
729
730 63b15e55 Michael Niedermayer
// RAL: Parameter added: f_or_b_code
731
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
732 de6d9b64 Fabrice Bellard
{
733 0ff93477 Michael Niedermayer
    int code, bit_size, l, bits, range, sign;
734 de6d9b64 Fabrice Bellard
735
    if (val == 0) {
736
        /* zero vector */
737
        code = 0;
738
        put_bits(&s->pb,
739 115329f1 Diego Biurrun
                 mbMotionVectorTable[0][1],
740
                 mbMotionVectorTable[0][0]);
741 de6d9b64 Fabrice Bellard
    } else {
742 63b15e55 Michael Niedermayer
        bit_size = f_or_b_code - 1;
743 de6d9b64 Fabrice Bellard
        range = 1 << bit_size;
744
        /* modulo encoding */
745 0ff93477 Michael Niedermayer
        l= INT_BIT - 5 - bit_size;
746
        val= (val<<l)>>l;
747 de6d9b64 Fabrice Bellard
748
        if (val >= 0) {
749
            val--;
750
            code = (val >> bit_size) + 1;
751
            bits = val & (range - 1);
752
            sign = 0;
753
        } else {
754
            val = -val;
755
            val--;
756
            code = (val >> bit_size) + 1;
757
            bits = val & (range - 1);
758
            sign = 1;
759
        }
760 63b15e55 Michael Niedermayer
761
        assert(code > 0 && code <= 16);
762
763 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,
764 115329f1 Diego Biurrun
                 mbMotionVectorTable[code][1],
765
                 mbMotionVectorTable[code][0]);
766 63b15e55 Michael Niedermayer
767 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 1, sign);
768
        if (bit_size > 0) {
769
            put_bits(&s->pb, bit_size, bits);
770
        }
771
    }
772
}
773
774 8f8402e4 Michael Niedermayer
void ff_mpeg1_encode_init(MpegEncContext *s)
775 11ce8834 Michael Niedermayer
{
776
    static int done=0;
777 8f8402e4 Michael Niedermayer
778
    common_init(s);
779
780 11ce8834 Michael Niedermayer
    if(!done){
781
        int f_code;
782
        int mv;
783 bb270c08 Diego Biurrun
        int i;
784 11ce8834 Michael Niedermayer
785
        done=1;
786 073c2593 Burkhard Plaum
        init_rl(&rl_mpeg1, 1);
787 307eb248 Baptiste Coudurier
        if(s->intra_vlc_format)
788
            init_rl(&rl_mpeg2, 1);
789 c442d75c Michael Niedermayer
790 bb270c08 Diego Biurrun
        for(i=0; i<64; i++)
791
        {
792
                mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
793
                mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
794
        }
795 115329f1 Diego Biurrun
796 d634aa20 Baptiste Coudurier
        init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
797 307eb248 Baptiste Coudurier
        if(s->intra_vlc_format)
798
            init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
799 8f8402e4 Michael Niedermayer
800 bb270c08 Diego Biurrun
        /* build unified dc encoding tables */
801
        for(i=-255; i<256; i++)
802
        {
803
                int adiff, index;
804
                int bits, code;
805
                int diff=i;
806 8f8402e4 Michael Niedermayer
807 bb270c08 Diego Biurrun
                adiff = ABS(diff);
808
                if(diff<0) diff--;
809
                index = av_log2(2*adiff);
810 8f8402e4 Michael Niedermayer
811 bb270c08 Diego Biurrun
                bits= vlc_dc_lum_bits[index] + index;
812
                code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
813
                mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
814 115329f1 Diego Biurrun
815 bb270c08 Diego Biurrun
                bits= vlc_dc_chroma_bits[index] + index;
816
                code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
817
                mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
818
        }
819 8f8402e4 Michael Niedermayer
820 30952237 Michael Niedermayer
        mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
821
822 11ce8834 Michael Niedermayer
        for(f_code=1; f_code<=MAX_FCODE; f_code++){
823
            for(mv=-MAX_MV; mv<=MAX_MV; mv++){
824
                int len;
825
826
                if(mv==0) len= mbMotionVectorTable[0][1];
827
                else{
828
                    int val, bit_size, range, code;
829
830 7813d6a3 Michael Niedermayer
                    bit_size = f_code - 1;
831 11ce8834 Michael Niedermayer
                    range = 1 << bit_size;
832
833
                    val=mv;
834 115329f1 Diego Biurrun
                    if (val < 0)
835 11ce8834 Michael Niedermayer
                        val = -val;
836
                    val--;
837
                    code = (val >> bit_size) + 1;
838
                    if(code<17){
839
                        len= mbMotionVectorTable[code][1] + 1 + bit_size;
840
                    }else{
841
                        len= mbMotionVectorTable[16][1] + 2 + bit_size;
842
                    }
843
                }
844
845
                mv_penalty[f_code][mv+MAX_MV]= len;
846
            }
847
        }
848 115329f1 Diego Biurrun
849 11ce8834 Michael Niedermayer
850
        for(f_code=MAX_FCODE; f_code>0; f_code--){
851
            for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
852
                fcode_tab[mv+MAX_MV]= f_code;
853
            }
854
        }
855
    }
856 1457ab52 Michael Niedermayer
    s->me.mv_penalty= mv_penalty;
857 11ce8834 Michael Niedermayer
    s->fcode_tab= fcode_tab;
858 029911d1 Michael Niedermayer
    if(s->codec_id == CODEC_ID_MPEG1VIDEO){
859
        s->min_qcoeff=-255;
860
        s->max_qcoeff= 255;
861
    }else{
862
        s->min_qcoeff=-2047;
863
        s->max_qcoeff= 2047;
864
    }
865 307eb248 Baptiste Coudurier
    if (s->intra_vlc_format) {
866
        s->intra_ac_vlc_length=
867
        s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
868
    } else {
869
        s->intra_ac_vlc_length=
870
        s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
871
    }
872 e5021fff Michael Niedermayer
    s->inter_ac_vlc_length=
873
    s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
874 11ce8834 Michael Niedermayer
}
875 8f8402e4 Michael Niedermayer
876 de6d9b64 Fabrice Bellard
static inline void encode_dc(MpegEncContext *s, int diff, int component)
877
{
878 bf266e19 Michael Niedermayer
  if(((unsigned) (diff+255)) >= 511){
879
        int index;
880
881
        if(diff<0){
882
            index= av_log2_16bit(-2*diff);
883
            diff--;
884
        }else{
885
            index= av_log2_16bit(2*diff);
886
        }
887
        if (component == 0) {
888
            put_bits(
889 115329f1 Diego Biurrun
                &s->pb,
890 bf266e19 Michael Niedermayer
                vlc_dc_lum_bits[index] + index,
891
                (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
892
        }else{
893
            put_bits(
894 115329f1 Diego Biurrun
                &s->pb,
895 bf266e19 Michael Niedermayer
                vlc_dc_chroma_bits[index] + index,
896
                (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
897
        }
898
  }else{
899 de6d9b64 Fabrice Bellard
    if (component == 0) {
900 f004ca1c Michael Niedermayer
        put_bits(
901 bb270c08 Diego Biurrun
            &s->pb,
902
            mpeg1_lum_dc_uni[diff+255]&0xFF,
903
            mpeg1_lum_dc_uni[diff+255]>>8);
904 de6d9b64 Fabrice Bellard
    } else {
905 f004ca1c Michael Niedermayer
        put_bits(
906 115329f1 Diego Biurrun
            &s->pb,
907 bb270c08 Diego Biurrun
            mpeg1_chr_dc_uni[diff+255]&0xFF,
908
            mpeg1_chr_dc_uni[diff+255]>>8);
909 de6d9b64 Fabrice Bellard
    }
910 bf266e19 Michael Niedermayer
  }
911 de6d9b64 Fabrice Bellard
}
912
913 115329f1 Diego Biurrun
static void mpeg1_encode_block(MpegEncContext *s,
914
                               DCTELEM *block,
915 de6d9b64 Fabrice Bellard
                               int n)
916
{
917
    int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
918
    int code, component;
919 307eb248 Baptiste Coudurier
    const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
920 de6d9b64 Fabrice Bellard
921
    last_index = s->block_last_index[n];
922
923
    /* DC coef */
924
    if (s->mb_intra) {
925 2d974017 Baptiste Coudurier
        component = (n <= 3 ? 0 : (n&1) + 1);
926 de6d9b64 Fabrice Bellard
        dc = block[0]; /* overflow is impossible */
927
        diff = dc - s->last_dc[component];
928
        encode_dc(s, diff, component);
929
        s->last_dc[component] = dc;
930
        i = 1;
931 029911d1 Michael Niedermayer
        if (s->intra_vlc_format)
932 307eb248 Baptiste Coudurier
            table_vlc = rl_mpeg2.table_vlc;
933 de6d9b64 Fabrice Bellard
    } else {
934
        /* encode the first coefficient : needs to be done here because
935
           it is handled slightly differently */
936
        level = block[0];
937
        if (abs(level) == 1) {
938 0c1a9eda Zdenek Kabelac
                code = ((uint32_t)level >> 31); /* the sign bit */
939 de6d9b64 Fabrice Bellard
                put_bits(&s->pb, 2, code | 0x02);
940
                i = 1;
941
        } else {
942
            i = 0;
943
            last_non_zero = -1;
944
            goto next_coef;
945
        }
946
    }
947
948
    /* now quantify & encode AC coefs */
949
    last_non_zero = i - 1;
950 80feb2a2 Michael Niedermayer
951 de6d9b64 Fabrice Bellard
    for(;i<=last_index;i++) {
952 2ad1516a Michael Niedermayer
        j = s->intra_scantable.permutated[i];
953 de6d9b64 Fabrice Bellard
        level = block[j];
954
    next_coef:
955
#if 0
956
        if (level != 0)
957
            dprintf("level[%d]=%d\n", i, level);
958 115329f1 Diego Biurrun
#endif
959 de6d9b64 Fabrice Bellard
        /* encode using VLC */
960
        if (level != 0) {
961
            run = i - last_non_zero - 1;
962 115329f1 Diego Biurrun
963 2ad1516a Michael Niedermayer
            alevel= level;
964
            MASK_ABS(sign, alevel)
965
            sign&=1;
966
967 c442d75c Michael Niedermayer
            if (alevel <= mpeg1_max_level[0][run]){
968 80feb2a2 Michael Niedermayer
                code= mpeg1_index_run[0][run] + alevel - 1;
969 bb270c08 Diego Biurrun
                /* store the vlc & sign at once */
970 307eb248 Baptiste Coudurier
                put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
971 de6d9b64 Fabrice Bellard
            } else {
972 bb270c08 Diego Biurrun
                /* escape seems to be pretty rare <5% so i dont optimize it */
973 307eb248 Baptiste Coudurier
                put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
974 de6d9b64 Fabrice Bellard
                /* escape: only clip in this case */
975
                put_bits(&s->pb, 6, run);
976 029911d1 Michael Niedermayer
                if(s->codec_id == CODEC_ID_MPEG1VIDEO){
977
                    if (alevel < 128) {
978
                        put_bits(&s->pb, 8, level & 0xff);
979 de6d9b64 Fabrice Bellard
                    } else {
980 029911d1 Michael Niedermayer
                        if (level < 0) {
981
                            put_bits(&s->pb, 16, 0x8001 + level + 255);
982
                        } else {
983
                            put_bits(&s->pb, 16, level & 0xffff);
984
                        }
985 de6d9b64 Fabrice Bellard
                    }
986 029911d1 Michael Niedermayer
                }else{
987
                    put_bits(&s->pb, 12, level & 0xfff);
988 de6d9b64 Fabrice Bellard
                }
989
            }
990
            last_non_zero = i;
991
        }
992
    }
993
    /* end of block */
994 307eb248 Baptiste Coudurier
    put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
995 de6d9b64 Fabrice Bellard
}
996 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
997 de6d9b64 Fabrice Bellard
998
/******************************************/
999
/* decoding */
1000
1001
static VLC dc_lum_vlc;
1002
static VLC dc_chroma_vlc;
1003
static VLC mv_vlc;
1004
static VLC mbincr_vlc;
1005
static VLC mb_ptype_vlc;
1006
static VLC mb_btype_vlc;
1007
static VLC mb_pat_vlc;
1008
1009 7906085f Falk Hüffner
static void init_vlcs(void)
1010 de6d9b64 Fabrice Bellard
{
1011
    static int done = 0;
1012
1013
    if (!done) {
1014 915bbac6 Fabrice Bellard
        done = 1;
1015 de6d9b64 Fabrice Bellard
1016 115329f1 Diego Biurrun
        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
1017 de6d9b64 Fabrice Bellard
                 vlc_dc_lum_bits, 1, 1,
1018 073c2593 Burkhard Plaum
                 vlc_dc_lum_code, 2, 2, 1);
1019 115329f1 Diego Biurrun
        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12,
1020 de6d9b64 Fabrice Bellard
                 vlc_dc_chroma_bits, 1, 1,
1021 073c2593 Burkhard Plaum
                 vlc_dc_chroma_code, 2, 2, 1);
1022 115329f1 Diego Biurrun
        init_vlc(&mv_vlc, MV_VLC_BITS, 17,
1023 de6d9b64 Fabrice Bellard
                 &mbMotionVectorTable[0][1], 2, 1,
1024 073c2593 Burkhard Plaum
                 &mbMotionVectorTable[0][0], 2, 1, 1);
1025 115329f1 Diego Biurrun
        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
1026 de6d9b64 Fabrice Bellard
                 &mbAddrIncrTable[0][1], 2, 1,
1027 073c2593 Burkhard Plaum
                 &mbAddrIncrTable[0][0], 2, 1, 1);
1028 461cd5bf Ivan Kalvachev
        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
1029 de6d9b64 Fabrice Bellard
                 &mbPatTable[0][1], 2, 1,
1030 073c2593 Burkhard Plaum
                 &mbPatTable[0][0], 2, 1, 1);
1031 115329f1 Diego Biurrun
1032
        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
1033 de6d9b64 Fabrice Bellard
                 &table_mb_ptype[0][1], 2, 1,
1034 073c2593 Burkhard Plaum
                 &table_mb_ptype[0][0], 2, 1, 1);
1035 115329f1 Diego Biurrun
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
1036 de6d9b64 Fabrice Bellard
                 &table_mb_btype[0][1], 2, 1,
1037 073c2593 Burkhard Plaum
                 &table_mb_btype[0][0], 2, 1, 1);
1038
        init_rl(&rl_mpeg1, 1);
1039
        init_rl(&rl_mpeg2, 1);
1040 4f68b084 Michael Niedermayer
1041 073c2593 Burkhard Plaum
        init_2d_vlc_rl(&rl_mpeg1, 1);
1042
        init_2d_vlc_rl(&rl_mpeg2, 1);
1043 de6d9b64 Fabrice Bellard
    }
1044
}
1045
1046
static inline int get_dmv(MpegEncContext *s)
1047
{
1048 115329f1 Diego Biurrun
    if(get_bits1(&s->gb))
1049 612476ef Arpi
        return 1 - (get_bits1(&s->gb) << 1);
1050 de6d9b64 Fabrice Bellard
    else
1051
        return 0;
1052
}
1053
1054 0ee50938 Fabrice Bellard
static inline int get_qscale(MpegEncContext *s)
1055
{
1056 7062fad6 BERO
    int qscale = get_bits(&s->gb, 5);
1057 51929fd3 Michael Niedermayer
    if (s->q_scale_type) {
1058
        return non_linear_qscale[qscale];
1059
    } else {
1060
        return qscale << 1;
1061 0ee50938 Fabrice Bellard
    }
1062
}
1063
1064 de6d9b64 Fabrice Bellard
/* motion type (for mpeg2) */
1065
#define MT_FIELD 1
1066
#define MT_FRAME 2
1067
#define MT_16X8  2
1068
#define MT_DMV   3
1069
1070
static int mpeg_decode_mb(MpegEncContext *s,
1071 5e5c247a Ivan Kalvachev
                          DCTELEM block[12][64])
1072 de6d9b64 Fabrice Bellard
{
1073 d2975f8d Michael Niedermayer
    int i, j, k, cbp, val, mb_type, motion_type;
1074 a622dc43 avcoder
    const int mb_block_count = 4 + (1<< s->chroma_format);
1075 715731a3 Ivan Kalvachev
1076 de6d9b64 Fabrice Bellard
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1077
1078 160d679c Mike Melanson
    assert(s->mb_skipped==0);
1079 f943e138 Michael Niedermayer
1080 9b8709d1 Michael Niedermayer
    if (s->mb_skip_run-- != 0) {
1081 7bc9090a Michael Niedermayer
        if(s->pict_type == I_TYPE){
1082 160d679c Mike Melanson
            av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1083 7bc9090a Michael Niedermayer
            return -1;
1084
        }
1085 115329f1 Diego Biurrun
1086 de6d9b64 Fabrice Bellard
        /* skip mb */
1087
        s->mb_intra = 0;
1088 5e5c247a Ivan Kalvachev
        for(i=0;i<12;i++)
1089 de6d9b64 Fabrice Bellard
            s->block_last_index[i] = -1;
1090 4c9544d0 Michael Niedermayer
        if(s->picture_structure == PICT_FRAME)
1091
            s->mv_type = MV_TYPE_16X16;
1092
        else
1093
            s->mv_type = MV_TYPE_FIELD;
1094 de6d9b64 Fabrice Bellard
        if (s->pict_type == P_TYPE) {
1095
            /* if P type, zero motion vector is implied */
1096
            s->mv_dir = MV_DIR_FORWARD;
1097
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
1098
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1099 25ed7f92 Fabrice Bellard
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1100 4c9544d0 Michael Niedermayer
            s->field_select[0][0]= s->picture_structure - 1;
1101 160d679c Mike Melanson
            s->mb_skipped = 1;
1102 7bc9090a Michael Niedermayer
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1103 de6d9b64 Fabrice Bellard
        } else {
1104 71434945 Michael Niedermayer
            int mb_type;
1105 115329f1 Diego Biurrun
1106 71434945 Michael Niedermayer
            if(s->mb_x)
1107
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
1108
            else
1109 115329f1 Diego Biurrun
                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all,
1110 71434945 Michael Niedermayer
            if(IS_INTRA(mb_type))
1111
                return -1;
1112 115329f1 Diego Biurrun
1113 de6d9b64 Fabrice Bellard
            /* if B type, reuse previous vectors and directions */
1114
            s->mv[0][0][0] = s->last_mv[0][0][0];
1115
            s->mv[0][0][1] = s->last_mv[0][0][1];
1116
            s->mv[1][0][0] = s->last_mv[1][0][0];
1117
            s->mv[1][0][1] = s->last_mv[1][0][1];
1118 f943e138 Michael Niedermayer
1119 115329f1 Diego Biurrun
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
1120 71434945 Michael Niedermayer
                mb_type | MB_TYPE_SKIP;
1121 7bc9090a Michael Niedermayer
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1122
1123 115329f1 Diego Biurrun
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
1124 160d679c Mike Melanson
                s->mb_skipped = 1;
1125 de6d9b64 Fabrice Bellard
        }
1126 59b571c1 Michael Niedermayer
1127 de6d9b64 Fabrice Bellard
        return 0;
1128
    }
1129
1130
    switch(s->pict_type) {
1131
    default:
1132
    case I_TYPE:
1133 612476ef Arpi
        if (get_bits1(&s->gb) == 0) {
1134 e94bc100 Michael Niedermayer
            if (get_bits1(&s->gb) == 0){
1135 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1136 de6d9b64 Fabrice Bellard
                return -1;
1137 e94bc100 Michael Niedermayer
            }
1138 7bc9090a Michael Niedermayer
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1139 de6d9b64 Fabrice Bellard
        } else {
1140 7bc9090a Michael Niedermayer
            mb_type = MB_TYPE_INTRA;
1141 de6d9b64 Fabrice Bellard
        }
1142
        break;
1143
    case P_TYPE:
1144 8ed2ddb2 Michael Niedermayer
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1145 9ac7ecd6 Michael Niedermayer
        if (mb_type < 0){
1146 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1147 de6d9b64 Fabrice Bellard
            return -1;
1148 9ac7ecd6 Michael Niedermayer
        }
1149 7bc9090a Michael Niedermayer
        mb_type = ptype2mb_type[ mb_type ];
1150 de6d9b64 Fabrice Bellard
        break;
1151
    case B_TYPE:
1152 8ed2ddb2 Michael Niedermayer
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1153 9ac7ecd6 Michael Niedermayer
        if (mb_type < 0){
1154 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1155 de6d9b64 Fabrice Bellard
            return -1;
1156 9ac7ecd6 Michael Niedermayer
        }
1157 7bc9090a Michael Niedermayer
        mb_type = btype2mb_type[ mb_type ];
1158 de6d9b64 Fabrice Bellard
        break;
1159
    }
1160
    dprintf("mb_type=%x\n", mb_type);
1161 7bc9090a Michael Niedermayer
//    motion_type = 0; /* avoid warning */
1162
    if (IS_INTRA(mb_type)) {
1163 2de4ba7f Michael Niedermayer
        s->dsp.clear_blocks(s->block[0]);
1164 115329f1 Diego Biurrun
1165 2de4ba7f Michael Niedermayer
        if(!s->chroma_y_shift){
1166
            s->dsp.clear_blocks(s->block[6]);
1167
        }
1168 115329f1 Diego Biurrun
1169 7bc9090a Michael Niedermayer
        /* compute dct type */
1170
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1171
            !s->frame_pred_frame_dct) {
1172
            s->interlaced_dct = get_bits1(&s->gb);
1173
        }
1174 de6d9b64 Fabrice Bellard
1175 7bc9090a Michael Niedermayer
        if (IS_QUANT(mb_type))
1176
            s->qscale = get_qscale(s);
1177 115329f1 Diego Biurrun
1178 de6d9b64 Fabrice Bellard
        if (s->concealment_motion_vectors) {
1179
            /* just parse them */
1180 115329f1 Diego Biurrun
            if (s->picture_structure != PICT_FRAME)
1181 612476ef Arpi
                skip_bits1(&s->gb); /* field select */
1182 115329f1 Diego Biurrun
1183
            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
1184 daab3296 Michael Niedermayer
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1185 115329f1 Diego Biurrun
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
1186 daab3296 Michael Niedermayer
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1187
1188 7bc9090a Michael Niedermayer
            skip_bits1(&s->gb); /* marker */
1189 daab3296 Michael Niedermayer
        }else
1190
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1191 de6d9b64 Fabrice Bellard
        s->mb_intra = 1;
1192 a579db0c Ivan Kalvachev
#ifdef HAVE_XVMC
1193
        //one 1 we memcpy blocks in xvmcvideo
1194
        if(s->avctx->xvmc_acceleration > 1){
1195
            XVMC_pack_pblocks(s,-1);//inter are always full blocks
1196
            if(s->swap_uv){
1197
                exchange_uv(s);
1198
            }
1199
        }
1200
#endif
1201 7bc9090a Michael Niedermayer
1202 029911d1 Michael Niedermayer
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1203 dff0f035 Michael Niedermayer
            if(s->flags2 & CODEC_FLAG2_FAST){
1204
                for(i=0;i<6;i++) {
1205
                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
1206
                }
1207
            }else{
1208
                for(i=0;i<mb_block_count;i++) {
1209
                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1210
                        return -1;
1211
                }
1212 7bc9090a Michael Niedermayer
            }
1213
        } else {
1214
            for(i=0;i<6;i++) {
1215 a579db0c Ivan Kalvachev
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1216 7bc9090a Michael Niedermayer
                    return -1;
1217
            }
1218
        }
1219 de6d9b64 Fabrice Bellard
    } else {
1220 7bc9090a Michael Niedermayer
        if (mb_type & MB_TYPE_ZERO_MV){
1221 b40cd4e0 Michael Niedermayer
            assert(mb_type & MB_TYPE_CBP);
1222 7bc9090a Michael Niedermayer
1223
            /* compute dct type */
1224
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1225
                !s->frame_pred_frame_dct) {
1226
                s->interlaced_dct = get_bits1(&s->gb);
1227
            }
1228
1229
            if (IS_QUANT(mb_type))
1230
                s->qscale = get_qscale(s);
1231
1232
            s->mv_dir = MV_DIR_FORWARD;
1233 4c9544d0 Michael Niedermayer
            if(s->picture_structure == PICT_FRAME)
1234
                s->mv_type = MV_TYPE_16X16;
1235
            else{
1236
                s->mv_type = MV_TYPE_FIELD;
1237
                mb_type |= MB_TYPE_INTERLACED;
1238
                s->field_select[0][0]= s->picture_structure - 1;
1239
            }
1240 7bc9090a Michael Niedermayer
            s->last_mv[0][0][0] = 0;
1241
            s->last_mv[0][0][1] = 0;
1242
            s->last_mv[0][1][0] = 0;
1243
            s->last_mv[0][1][1] = 0;
1244
            s->mv[0][0][0] = 0;
1245
            s->mv[0][0][1] = 0;
1246
        }else{
1247
            assert(mb_type & MB_TYPE_L0L1);
1248
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1249
            /* get additionnal motion vector type */
1250 115329f1 Diego Biurrun
            if (s->frame_pred_frame_dct)
1251 7bc9090a Michael Niedermayer
                motion_type = MT_FRAME;
1252
            else{
1253
                motion_type = get_bits(&s->gb, 2);
1254
            }
1255
1256
            /* compute dct type */
1257
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1258 b40cd4e0 Michael Niedermayer
                !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1259 7bc9090a Michael Niedermayer
                s->interlaced_dct = get_bits1(&s->gb);
1260
            }
1261
1262
            if (IS_QUANT(mb_type))
1263
                s->qscale = get_qscale(s);
1264
1265
            /* motion vectors */
1266
            s->mv_dir = 0;
1267
            for(i=0;i<2;i++) {
1268
                if (USES_LIST(mb_type, i)) {
1269
                    s->mv_dir |= (MV_DIR_FORWARD >> i);
1270
                    dprintf("motion_type=%d\n", motion_type);
1271
                    switch(motion_type) {
1272
                    case MT_FRAME: /* or MT_16X8 */
1273
                        if (s->picture_structure == PICT_FRAME) {
1274
                            /* MT_FRAME */
1275 115329f1 Diego Biurrun
                            mb_type |= MB_TYPE_16x16;
1276 7bc9090a Michael Niedermayer
                            s->mv_type = MV_TYPE_16X16;
1277 115329f1 Diego Biurrun
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
1278 7bc9090a Michael Niedermayer
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1279 115329f1 Diego Biurrun
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
1280 7bc9090a Michael Niedermayer
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1281 de6d9b64 Fabrice Bellard
                            /* full_pel: only for mpeg1 */
1282 7bc9090a Michael Niedermayer
                            if (s->full_pel[i]){
1283
                                s->mv[i][0][0] <<= 1;
1284
                                s->mv[i][0][1] <<= 1;
1285
                            }
1286
                        } else {
1287
                            /* MT_16X8 */
1288 115329f1 Diego Biurrun
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1289 7bc9090a Michael Niedermayer
                            s->mv_type = MV_TYPE_16X8;
1290
                            for(j=0;j<2;j++) {
1291
                                s->field_select[i][j] = get_bits1(&s->gb);
1292
                                for(k=0;k<2;k++) {
1293
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1294
                                                             s->last_mv[i][j][k]);
1295
                                    s->last_mv[i][j][k] = val;
1296
                                    s->mv[i][j][k] = val;
1297
                                }
1298
                            }
1299 de6d9b64 Fabrice Bellard
                        }
1300 7bc9090a Michael Niedermayer
                        break;
1301
                    case MT_FIELD:
1302
                        s->mv_type = MV_TYPE_FIELD;
1303
                        if (s->picture_structure == PICT_FRAME) {
1304 115329f1 Diego Biurrun
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1305 7bc9090a Michael Niedermayer
                            for(j=0;j<2;j++) {
1306
                                s->field_select[i][j] = get_bits1(&s->gb);
1307
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1308
                                                         s->last_mv[i][j][0]);
1309
                                s->last_mv[i][j][0] = val;
1310
                                s->mv[i][j][0] = val;
1311
                                dprintf("fmx=%d\n", val);
1312
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1313
                                                         s->last_mv[i][j][1] >> 1);
1314
                                s->last_mv[i][j][1] = val << 1;
1315
                                s->mv[i][j][1] = val;
1316
                                dprintf("fmy=%d\n", val);
1317
                            }
1318
                        } else {
1319 115329f1 Diego Biurrun
                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1320 7bc9090a Michael Niedermayer
                            s->field_select[i][0] = get_bits1(&s->gb);
1321 de6d9b64 Fabrice Bellard
                            for(k=0;k<2;k++) {
1322
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1323 7bc9090a Michael Niedermayer
                                                         s->last_mv[i][0][k]);
1324
                                s->last_mv[i][0][k] = val;
1325
                                s->last_mv[i][1][k] = val;
1326
                                s->mv[i][0][k] = val;
1327 de6d9b64 Fabrice Bellard
                            }
1328
                        }
1329 7bc9090a Michael Niedermayer
                        break;
1330
                    case MT_DMV:
1331
                        {
1332
                            int dmx, dmy, mx, my, m;
1333
1334 115329f1 Diego Biurrun
                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1335 7bc9090a Michael Niedermayer
                                                    s->last_mv[i][0][0]);
1336
                            s->last_mv[i][0][0] = mx;
1337
                            s->last_mv[i][1][0] = mx;
1338
                            dmx = get_dmv(s);
1339 115329f1 Diego Biurrun
                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1340 7bc9090a Michael Niedermayer
                                                    s->last_mv[i][0][1] >> 1);
1341
                            dmy = get_dmv(s);
1342
                            s->mv_type = MV_TYPE_DMV;
1343 1dff7d56 Ivan Kalvachev
1344
1345
                            s->last_mv[i][0][1] = my<<1;
1346
                            s->last_mv[i][1][1] = my<<1;
1347
1348
                            s->mv[i][0][0] = mx;
1349
                            s->mv[i][0][1] = my;
1350
                            s->mv[i][1][0] = mx;//not used
1351
                            s->mv[i][1][1] = my;//not used
1352
1353 7bc9090a Michael Niedermayer
                            if (s->picture_structure == PICT_FRAME) {
1354 115329f1 Diego Biurrun
                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1355 7bc9090a Michael Niedermayer
1356 1dff7d56 Ivan Kalvachev
                                //m = 1 + 2 * s->top_field_first;
1357 7bc9090a Michael Niedermayer
                                m = s->top_field_first ? 1 : 3;
1358 1dff7d56 Ivan Kalvachev
1359 7bc9090a Michael Niedermayer
                                /* top -> top pred */
1360 1dff7d56 Ivan Kalvachev
                                s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1361
                                s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1362 7bc9090a Michael Niedermayer
                                m = 4 - m;
1363
                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1364
                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1365
                            } else {
1366
                                mb_type |= MB_TYPE_16x16;
1367
1368 1dff7d56 Ivan Kalvachev
                                s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1369
                                s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1370
                                if(s->picture_structure == PICT_TOP_FIELD)
1371
                                    s->mv[i][2][1]--;
1372 115329f1 Diego Biurrun
                                else
1373 1dff7d56 Ivan Kalvachev
                                    s->mv[i][2][1]++;
1374 7bc9090a Michael Niedermayer
                            }
1375 de6d9b64 Fabrice Bellard
                        }
1376 7bc9090a Michael Niedermayer
                        break;
1377 26f548a7 Michael Niedermayer
                    default:
1378
                        av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1379
                        return -1;
1380 de6d9b64 Fabrice Bellard
                    }
1381
                }
1382
            }
1383
        }
1384 115329f1 Diego Biurrun
1385 7bc9090a Michael Niedermayer
        s->mb_intra = 0;
1386 b40cd4e0 Michael Niedermayer
        if (HAS_CBP(mb_type)) {
1387 2de4ba7f Michael Niedermayer
            s->dsp.clear_blocks(s->block[0]);
1388 115329f1 Diego Biurrun
1389 2de4ba7f Michael Niedermayer
            if(!s->chroma_y_shift){
1390
                s->dsp.clear_blocks(s->block[6]);
1391
            }
1392
1393 7bc9090a Michael Niedermayer
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1394 494c56d3 Ivan Kalvachev
            if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1395 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1396 7bc9090a Michael Niedermayer
                return -1;
1397 de6d9b64 Fabrice Bellard
            }
1398 715731a3 Ivan Kalvachev
            if(mb_block_count > 6){
1399 bb270c08 Diego Biurrun
                 cbp<<= mb_block_count-6;
1400
                 cbp |= get_bits(&s->gb, mb_block_count-6);
1401 5e5c247a Ivan Kalvachev
            }
1402 7bc9090a Michael Niedermayer
1403 a579db0c Ivan Kalvachev
#ifdef HAVE_XVMC
1404
            //on 1 we memcpy blocks in xvmcvideo
1405
            if(s->avctx->xvmc_acceleration > 1){
1406
                XVMC_pack_pblocks(s,cbp);
1407
                if(s->swap_uv){
1408
                    exchange_uv(s);
1409
                }
1410 115329f1 Diego Biurrun
            }
1411 a579db0c Ivan Kalvachev
#endif
1412
1413 029911d1 Michael Niedermayer
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1414 6fc5b059 Michael Niedermayer
                if(s->flags2 & CODEC_FLAG2_FAST){
1415
                    for(i=0;i<6;i++) {
1416
                        if(cbp & 32) {
1417
                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
1418
                        } else {
1419
                            s->block_last_index[i] = -1;
1420
                        }
1421
                        cbp+=cbp;
1422
                    }
1423
                }else{
1424
                    cbp<<= 12-mb_block_count;
1425 115329f1 Diego Biurrun
1426 6fc5b059 Michael Niedermayer
                    for(i=0;i<mb_block_count;i++) {
1427
                        if ( cbp & (1<<11) ) {
1428
                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1429
                                return -1;
1430
                        } else {
1431
                            s->block_last_index[i] = -1;
1432
                        }
1433
                        cbp+=cbp;
1434 7bc9090a Michael Niedermayer
                    }
1435 de6d9b64 Fabrice Bellard
                }
1436 7bc9090a Michael Niedermayer
            } else {
1437 628b210f Michael Niedermayer
                if(s->flags2 & CODEC_FLAG2_FAST){
1438
                    for(i=0;i<6;i++) {
1439
                        if (cbp & 32) {
1440
                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
1441
                        } else {
1442
                            s->block_last_index[i] = -1;
1443
                        }
1444
                        cbp+=cbp;
1445
                    }
1446
                }else{
1447
                    for(i=0;i<6;i++) {
1448
                        if (cbp & 32) {
1449
                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1450
                                return -1;
1451
                        } else {
1452
                            s->block_last_index[i] = -1;
1453
                        }
1454
                        cbp+=cbp;
1455 7bc9090a Michael Niedermayer
                    }
1456 a0201736 Michael Niedermayer
                }
1457 de6d9b64 Fabrice Bellard
            }
1458 7bc9090a Michael Niedermayer
        }else{
1459 31a78b71 Ivan Kalvachev
            for(i=0;i<12;i++)
1460 7bc9090a Michael Niedermayer
                s->block_last_index[i] = -1;
1461 de6d9b64 Fabrice Bellard
        }
1462
    }
1463 7bc9090a Michael Niedermayer
1464
    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1465
1466 de6d9b64 Fabrice Bellard
    return 0;
1467
}
1468
1469
/* as h263, but only 17 codes */
1470
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1471
{
1472 05858889 BERO
    int code, sign, val, l, shift;
1473 de6d9b64 Fabrice Bellard
1474 8ed2ddb2 Michael Niedermayer
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1475 de6d9b64 Fabrice Bellard
    if (code == 0) {
1476
        return pred;
1477
    }
1478 7bc9090a Michael Niedermayer
    if (code < 0) {
1479
        return 0xffff;
1480
    }
1481
1482 612476ef Arpi
    sign = get_bits1(&s->gb);
1483 de6d9b64 Fabrice Bellard
    shift = fcode - 1;
1484 05858889 BERO
    val = code;
1485
    if (shift) {
1486
        val = (val - 1) << shift;
1487 de6d9b64 Fabrice Bellard
        val |= get_bits(&s->gb, shift);
1488 05858889 BERO
        val++;
1489
    }
1490 de6d9b64 Fabrice Bellard
    if (sign)
1491
        val = -val;
1492
    val += pred;
1493 115329f1 Diego Biurrun
1494 de6d9b64 Fabrice Bellard
    /* modulo decoding */
1495 0ff93477 Michael Niedermayer
    l= INT_BIT - 5 - shift;
1496
    val = (val<<l)>>l;
1497 de6d9b64 Fabrice Bellard
    return val;
1498
}
1499
1500 c3bf0288 Michael Niedermayer
static inline int decode_dc(GetBitContext *gb, int component)
1501 de6d9b64 Fabrice Bellard
{
1502
    int code, diff;
1503
1504
    if (component == 0) {
1505 c3bf0288 Michael Niedermayer
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1506 de6d9b64 Fabrice Bellard
    } else {
1507 c3bf0288 Michael Niedermayer
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1508 de6d9b64 Fabrice Bellard
    }
1509 9ac7ecd6 Michael Niedermayer
    if (code < 0){
1510 9b879566 Michel Bardiaux
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1511 de6d9b64 Fabrice Bellard
        return 0xffff;
1512 9ac7ecd6 Michael Niedermayer
    }
1513 de6d9b64 Fabrice Bellard
    if (code == 0) {
1514
        diff = 0;
1515
    } else {
1516 c3bf0288 Michael Niedermayer
        diff = get_xbits(gb, code);
1517 de6d9b64 Fabrice Bellard
    }
1518
    return diff;
1519
}
1520
1521 115329f1 Diego Biurrun
static inline int mpeg1_decode_block_intra(MpegEncContext *s,
1522
                               DCTELEM *block,
1523 de6d9b64 Fabrice Bellard
                               int n)
1524
{
1525
    int level, dc, diff, i, j, run;
1526 a0201736 Michael Niedermayer
    int component;
1527 de6d9b64 Fabrice Bellard
    RLTable *rl = &rl_mpeg1;
1528 0c1a9eda Zdenek Kabelac
    uint8_t * const scantable= s->intra_scantable.permutated;
1529
    const uint16_t *quant_matrix= s->intra_matrix;
1530 a0201736 Michael Niedermayer
    const int qscale= s->qscale;
1531 de6d9b64 Fabrice Bellard
1532 a0201736 Michael Niedermayer
    /* DC coef */
1533
    component = (n <= 3 ? 0 : n - 4 + 1);
1534 c3bf0288 Michael Niedermayer
    diff = decode_dc(&s->gb, component);
1535 a0201736 Michael Niedermayer
    if (diff >= 0xffff)
1536
        return -1;
1537
    dc = s->last_dc[component];
1538
    dc += diff;
1539
    s->last_dc[component] = dc;
1540
    block[0] = dc<<3;
1541
    dprintf("dc=%d diff=%d\n", dc, diff);
1542
    i = 0;
1543
    {
1544 115329f1 Diego Biurrun
        OPEN_READER(re, &s->gb);
1545 a0201736 Michael Niedermayer
        /* now quantify & encode AC coefs */
1546
        for(;;) {
1547
            UPDATE_CACHE(re, &s->gb);
1548 e91f4bf1 Michael Niedermayer
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1549 115329f1 Diego Biurrun
1550 a0201736 Michael Niedermayer
            if(level == 127){
1551
                break;
1552
            } else if(level != 0) {
1553
                i += run;
1554
                j = scantable[i];
1555 51929fd3 Michael Niedermayer
                level= (level*qscale*quant_matrix[j])>>4;
1556 a0201736 Michael Niedermayer
                level= (level-1)|1;
1557
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1558
                LAST_SKIP_BITS(re, &s->gb, 1);
1559
            } else {
1560
                /* escape */
1561
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1562
                UPDATE_CACHE(re, &s->gb);
1563
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1564
                if (level == -128) {
1565
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1566
                } else if (level == 0) {
1567
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1568
                }
1569
                i += run;
1570
                j = scantable[i];
1571
                if(level<0){
1572
                    level= -level;
1573 51929fd3 Michael Niedermayer
                    level= (level*qscale*quant_matrix[j])>>4;
1574 a0201736 Michael Niedermayer
                    level= (level-1)|1;
1575
                    level= -level;
1576
                }else{
1577 51929fd3 Michael Niedermayer
                    level= (level*qscale*quant_matrix[j])>>4;
1578 a0201736 Michael Niedermayer
                    level= (level-1)|1;
1579
                }
1580
            }
1581
            if (i > 63){
1582 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1583 a0201736 Michael Niedermayer
                return -1;
1584
            }
1585
1586
            block[j] = level;
1587
        }
1588
        CLOSE_READER(re, &s->gb);
1589
    }
1590
    s->block_last_index[n] = i;
1591
   return 0;
1592
}
1593
1594 115329f1 Diego Biurrun
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
1595
                               DCTELEM *block,
1596 a0201736 Michael Niedermayer
                               int n)
1597
{
1598
    int level, i, j, run;
1599
    RLTable *rl = &rl_mpeg1;
1600 0c1a9eda Zdenek Kabelac
    uint8_t * const scantable= s->intra_scantable.permutated;
1601
    const uint16_t *quant_matrix= s->inter_matrix;
1602 a0201736 Michael Niedermayer
    const int qscale= s->qscale;
1603
1604
    {
1605 8db1a1dd Michael Niedermayer
        OPEN_READER(re, &s->gb);
1606 a0201736 Michael Niedermayer
        i = -1;
1607 de6d9b64 Fabrice Bellard
        /* special case for the first coef. no need to add a second vlc table */
1608 8db1a1dd Michael Niedermayer
        UPDATE_CACHE(re, &s->gb);
1609 29df2599 Michael Niedermayer
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1610 51929fd3 Michael Niedermayer
            level= (3*qscale*quant_matrix[0])>>5;
1611 a0201736 Michael Niedermayer
            level= (level-1)|1;
1612 29df2599 Michael Niedermayer
            if(GET_CACHE(re, &s->gb)&0x40000000)
1613 a0201736 Michael Niedermayer
                level= -level;
1614 3729c912 Michael Niedermayer
            block[0] = level;
1615 a0201736 Michael Niedermayer
            i++;
1616 29df2599 Michael Niedermayer
            SKIP_BITS(re, &s->gb, 2);
1617
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1618
                goto end;
1619 de6d9b64 Fabrice Bellard
        }
1620 3729c912 Michael Niedermayer
1621 a0201736 Michael Niedermayer
        /* now quantify & encode AC coefs */
1622
        for(;;) {
1623 e91f4bf1 Michael Niedermayer
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1624 115329f1 Diego Biurrun
1625 29df2599 Michael Niedermayer
            if(level != 0) {
1626 a0201736 Michael Niedermayer
                i += run;
1627
                j = scantable[i];
1628 51929fd3 Michael Niedermayer
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1629 a0201736 Michael Niedermayer
                level= (level-1)|1;
1630
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1631 29df2599 Michael Niedermayer
                SKIP_BITS(re, &s->gb, 1);
1632 a0201736 Michael Niedermayer
            } else {
1633
                /* escape */
1634
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1635
                UPDATE_CACHE(re, &s->gb);
1636
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1637
                if (level == -128) {
1638 29df2599 Michael Niedermayer
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1639 a0201736 Michael Niedermayer
                } else if (level == 0) {
1640 29df2599 Michael Niedermayer
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1641 a0201736 Michael Niedermayer
                }
1642
                i += run;
1643
                j = scantable[i];
1644
                if(level<0){
1645
                    level= -level;
1646 51929fd3 Michael Niedermayer
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1647 a0201736 Michael Niedermayer
                    level= (level-1)|1;
1648
                    level= -level;
1649
                }else{
1650 51929fd3 Michael Niedermayer
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1651 a0201736 Michael Niedermayer
                    level= (level-1)|1;
1652
                }
1653 de6d9b64 Fabrice Bellard
            }
1654 a0201736 Michael Niedermayer
            if (i > 63){
1655 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1656 a0201736 Michael Niedermayer
                return -1;
1657
            }
1658
1659
            block[j] = level;
1660 29df2599 Michael Niedermayer
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1661
                break;
1662
            UPDATE_CACHE(re, &s->gb);
1663 de6d9b64 Fabrice Bellard
        }
1664 29df2599 Michael Niedermayer
end:
1665
        LAST_SKIP_BITS(re, &s->gb, 2);
1666 a0201736 Michael Niedermayer
        CLOSE_READER(re, &s->gb);
1667 de6d9b64 Fabrice Bellard
    }
1668 a0201736 Michael Niedermayer
    s->block_last_index[n] = i;
1669 de6d9b64 Fabrice Bellard
    return 0;
1670
}
1671
1672 628b210f Michael Niedermayer
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
1673
{
1674
    int level, i, j, run;
1675
    RLTable *rl = &rl_mpeg1;
1676
    uint8_t * const scantable= s->intra_scantable.permutated;
1677
    const int qscale= s->qscale;
1678
1679
    {
1680
        OPEN_READER(re, &s->gb);
1681
        i = -1;
1682
        /* special case for the first coef. no need to add a second vlc table */
1683
        UPDATE_CACHE(re, &s->gb);
1684 29df2599 Michael Niedermayer
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1685 3cb01872 Michael Niedermayer
            level= (3*qscale)>>1;
1686 628b210f Michael Niedermayer
            level= (level-1)|1;
1687 29df2599 Michael Niedermayer
            if(GET_CACHE(re, &s->gb)&0x40000000)
1688 628b210f Michael Niedermayer
                level= -level;
1689
            block[0] = level;
1690
            i++;
1691 29df2599 Michael Niedermayer
            SKIP_BITS(re, &s->gb, 2);
1692
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1693
                goto end;
1694 628b210f Michael Niedermayer
        }
1695
1696
        /* now quantify & encode AC coefs */
1697
        for(;;) {
1698 e91f4bf1 Michael Niedermayer
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1699 115329f1 Diego Biurrun
1700 29df2599 Michael Niedermayer
            if(level != 0) {
1701 628b210f Michael Niedermayer
                i += run;
1702
                j = scantable[i];
1703
                level= ((level*2+1)*qscale)>>1;
1704
                level= (level-1)|1;
1705
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1706 29df2599 Michael Niedermayer
                SKIP_BITS(re, &s->gb, 1);
1707 628b210f Michael Niedermayer
            } else {
1708
                /* escape */
1709
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1710
                UPDATE_CACHE(re, &s->gb);
1711
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1712
                if (level == -128) {
1713 29df2599 Michael Niedermayer
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1714 628b210f Michael Niedermayer
                } else if (level == 0) {
1715 29df2599 Michael Niedermayer
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1716 628b210f Michael Niedermayer
                }
1717
                i += run;
1718
                j = scantable[i];
1719
                if(level<0){
1720
                    level= -level;
1721
                    level= ((level*2+1)*qscale)>>1;
1722
                    level= (level-1)|1;
1723
                    level= -level;
1724
                }else{
1725
                    level= ((level*2+1)*qscale)>>1;
1726
                    level= (level-1)|1;
1727
                }
1728
            }
1729
1730
            block[j] = level;
1731 29df2599 Michael Niedermayer
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1732
                break;
1733
            UPDATE_CACHE(re, &s->gb);
1734 628b210f Michael Niedermayer
        }
1735 29df2599 Michael Niedermayer
end:
1736
        LAST_SKIP_BITS(re, &s->gb, 2);
1737 628b210f Michael Niedermayer
        CLOSE_READER(re, &s->gb);
1738
    }
1739
    s->block_last_index[n] = i;
1740
    return 0;
1741
}
1742
1743
1744 115329f1 Diego Biurrun
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
1745
                               DCTELEM *block,
1746 3729c912 Michael Niedermayer
                               int n)
1747 de6d9b64 Fabrice Bellard
{
1748
    int level, i, j, run;
1749
    RLTable *rl = &rl_mpeg1;
1750 0c1a9eda Zdenek Kabelac
    uint8_t * const scantable= s->intra_scantable.permutated;
1751
    const uint16_t *quant_matrix;
1752 3729c912 Michael Niedermayer
    const int qscale= s->qscale;
1753 de6d9b64 Fabrice Bellard
    int mismatch;
1754
1755
    mismatch = 1;
1756
1757
    {
1758 8db1a1dd Michael Niedermayer
        OPEN_READER(re, &s->gb);
1759 3729c912 Michael Niedermayer
        i = -1;
1760 8db1a1dd Michael Niedermayer
        if (n < 4)
1761 3729c912 Michael Niedermayer
            quant_matrix = s->inter_matrix;
1762 de6d9b64 Fabrice Bellard
        else
1763 3729c912 Michael Niedermayer
            quant_matrix = s->chroma_inter_matrix;
1764 8db1a1dd Michael Niedermayer
1765 de6d9b64 Fabrice Bellard
        /* special case for the first coef. no need to add a second vlc table */
1766 8db1a1dd Michael Niedermayer
        UPDATE_CACHE(re, &s->gb);
1767 29df2599 Michael Niedermayer
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1768 3729c912 Michael Niedermayer
            level= (3*qscale*quant_matrix[0])>>5;
1769 29df2599 Michael Niedermayer
            if(GET_CACHE(re, &s->gb)&0x40000000)
1770 3729c912 Michael Niedermayer
                level= -level;
1771
            block[0] = level;
1772
            mismatch ^= level;
1773
            i++;
1774 29df2599 Michael Niedermayer
            SKIP_BITS(re, &s->gb, 2);
1775
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1776
                goto end;
1777 de6d9b64 Fabrice Bellard
        }
1778
1779 3729c912 Michael Niedermayer
        /* now quantify & encode AC coefs */
1780
        for(;;) {
1781 e91f4bf1 Michael Niedermayer
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1782 115329f1 Diego Biurrun
1783 29df2599 Michael Niedermayer
            if(level != 0) {
1784 3729c912 Michael Niedermayer
                i += run;
1785
                j = scantable[i];
1786
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1787
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1788 29df2599 Michael Niedermayer
                SKIP_BITS(re, &s->gb, 1);
1789 3729c912 Michael Niedermayer
            } else {
1790
                /* escape */
1791
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1792
                UPDATE_CACHE(re, &s->gb);
1793
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1794
1795
                i += run;
1796
                j = scantable[i];
1797
                if(level<0){
1798
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1799
                    level= -level;
1800
                }else{
1801
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1802
                }
1803
            }
1804
            if (i > 63){
1805 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1806 3729c912 Michael Niedermayer
                return -1;
1807
            }
1808 115329f1 Diego Biurrun
1809 3729c912 Michael Niedermayer
            mismatch ^= level;
1810
            block[j] = level;
1811 29df2599 Michael Niedermayer
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1812
                break;
1813
            UPDATE_CACHE(re, &s->gb);
1814 25ed7f92 Fabrice Bellard
        }
1815 29df2599 Michael Niedermayer
end:
1816
        LAST_SKIP_BITS(re, &s->gb, 2);
1817 3729c912 Michael Niedermayer
        CLOSE_READER(re, &s->gb);
1818 de6d9b64 Fabrice Bellard
    }
1819
    block[63] ^= (mismatch & 1);
1820 115329f1 Diego Biurrun
1821 de6d9b64 Fabrice Bellard
    s->block_last_index[n] = i;
1822
    return 0;
1823
}
1824
1825 115329f1 Diego Biurrun
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
1826
                               DCTELEM *block,
1827 6fc5b059 Michael Niedermayer
                               int n)
1828
{
1829
    int level, i, j, run;
1830
    RLTable *rl = &rl_mpeg1;
1831
    uint8_t * const scantable= s->intra_scantable.permutated;
1832
    const int qscale= s->qscale;
1833
    OPEN_READER(re, &s->gb);
1834
    i = -1;
1835
1836
    /* special case for the first coef. no need to add a second vlc table */
1837
    UPDATE_CACHE(re, &s->gb);
1838 29df2599 Michael Niedermayer
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1839 6fc5b059 Michael Niedermayer
        level= (3*qscale)>>1;
1840 29df2599 Michael Niedermayer
        if(GET_CACHE(re, &s->gb)&0x40000000)
1841 6fc5b059 Michael Niedermayer
            level= -level;
1842
        block[0] = level;
1843
        i++;
1844 29df2599 Michael Niedermayer
        SKIP_BITS(re, &s->gb, 2);
1845
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1846
            goto end;
1847 6fc5b059 Michael Niedermayer
    }
1848
1849
    /* now quantify & encode AC coefs */
1850
    for(;;) {
1851 e91f4bf1 Michael Niedermayer
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1852 115329f1 Diego Biurrun
1853 29df2599 Michael Niedermayer
        if(level != 0) {
1854 6fc5b059 Michael Niedermayer
            i += run;
1855
            j = scantable[i];
1856
            level= ((level*2+1)*qscale)>>1;
1857
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1858 29df2599 Michael Niedermayer
            SKIP_BITS(re, &s->gb, 1);
1859 6fc5b059 Michael Niedermayer
        } else {
1860
            /* escape */
1861
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1862
            UPDATE_CACHE(re, &s->gb);
1863
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1864
1865
            i += run;
1866
            j = scantable[i];
1867
            if(level<0){
1868
                level= ((-level*2+1)*qscale)>>1;
1869
                level= -level;
1870
            }else{
1871
                level= ((level*2+1)*qscale)>>1;
1872
            }
1873
        }
1874 115329f1 Diego Biurrun
1875 6fc5b059 Michael Niedermayer
        block[j] = level;
1876 29df2599 Michael Niedermayer
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1877
            break;
1878
        UPDATE_CACHE(re, &s->gb);
1879 6fc5b059 Michael Niedermayer
    }
1880 29df2599 Michael Niedermayer
end:
1881 115329f1 Diego Biurrun
    LAST_SKIP_BITS(re, &s->gb, 2);
1882 6fc5b059 Michael Niedermayer
    CLOSE_READER(re, &s->gb);
1883
    s->block_last_index[n] = i;
1884
    return 0;
1885
}
1886
1887
1888 115329f1 Diego Biurrun
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1889
                               DCTELEM *block,
1890 3729c912 Michael Niedermayer
                               int n)
1891 de6d9b64 Fabrice Bellard
{
1892
    int level, dc, diff, i, j, run;
1893 3729c912 Michael Niedermayer
    int component;
1894 de6d9b64 Fabrice Bellard
    RLTable *rl;
1895 0c1a9eda Zdenek Kabelac
    uint8_t * const scantable= s->intra_scantable.permutated;
1896
    const uint16_t *quant_matrix;
1897 3729c912 Michael Niedermayer
    const int qscale= s->qscale;
1898 de6d9b64 Fabrice Bellard
    int mismatch;
1899
1900
    /* DC coef */
1901 3729c912 Michael Niedermayer
    if (n < 4){
1902
        quant_matrix = s->intra_matrix;
1903 115329f1 Diego Biurrun
        component = 0;
1904 3729c912 Michael Niedermayer
    }else{
1905
        quant_matrix = s->chroma_intra_matrix;
1906 5e5c247a Ivan Kalvachev
        component = (n&1) + 1;
1907 3729c912 Michael Niedermayer
    }
1908 c3bf0288 Michael Niedermayer
    diff = decode_dc(&s->gb, component);
1909 de6d9b64 Fabrice Bellard
    if (diff >= 0xffff)
1910
        return -1;
1911
    dc = s->last_dc[component];
1912
    dc += diff;
1913
    s->last_dc[component] = dc;
1914
    block[0] = dc << (3 - s->intra_dc_precision);
1915
    dprintf("dc=%d\n", block[0]);
1916 d753173a Fabrice Bellard
    mismatch = block[0] ^ 1;
1917 3729c912 Michael Niedermayer
    i = 0;
1918 de6d9b64 Fabrice Bellard
    if (s->intra_vlc_format)
1919
        rl = &rl_mpeg2;
1920
    else
1921
        rl = &rl_mpeg1;
1922 25ed7f92 Fabrice Bellard
1923 3729c912 Michael Niedermayer
    {
1924 115329f1 Diego Biurrun
        OPEN_READER(re, &s->gb);
1925 3729c912 Michael Niedermayer
        /* now quantify & encode AC coefs */
1926
        for(;;) {
1927
            UPDATE_CACHE(re, &s->gb);
1928 e91f4bf1 Michael Niedermayer
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1929 115329f1 Diego Biurrun
1930 3729c912 Michael Niedermayer
            if(level == 127){
1931
                break;
1932
            } else if(level != 0) {
1933
                i += run;
1934
                j = scantable[i];
1935
                level= (level*qscale*quant_matrix[j])>>4;
1936
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1937
                LAST_SKIP_BITS(re, &s->gb, 1);
1938
            } else {
1939
                /* escape */
1940
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1941
                UPDATE_CACHE(re, &s->gb);
1942
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1943
                i += run;
1944
                j = scantable[i];
1945
                if(level<0){
1946
                    level= (-level*qscale*quant_matrix[j])>>4;
1947
                    level= -level;
1948
                }else{
1949
                    level= (level*qscale*quant_matrix[j])>>4;
1950
                }
1951
            }
1952
            if (i > 63){
1953 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1954 3729c912 Michael Niedermayer
                return -1;
1955
            }
1956 115329f1 Diego Biurrun
1957 3729c912 Michael Niedermayer
            mismatch^= level;
1958
            block[j] = level;
1959 9ac7ecd6 Michael Niedermayer
        }
1960 3729c912 Michael Niedermayer
        CLOSE_READER(re, &s->gb);
1961 de6d9b64 Fabrice Bellard
    }
1962 3729c912 Michael Niedermayer
    block[63]^= mismatch&1;
1963 115329f1 Diego Biurrun
1964 de6d9b64 Fabrice Bellard
    s->block_last_index[n] = i;
1965
    return 0;
1966
}
1967
1968 115329f1 Diego Biurrun
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1969
                               DCTELEM *block,
1970 dff0f035 Michael Niedermayer
                               int n)
1971
{
1972
    int level, dc, diff, j, run;
1973
    int component;
1974
    RLTable *rl;
1975
    uint8_t * scantable= s->intra_scantable.permutated;
1976
    const uint16_t *quant_matrix;
1977
    const int qscale= s->qscale;
1978
1979
    /* DC coef */
1980
    if (n < 4){
1981
        quant_matrix = s->intra_matrix;
1982 115329f1 Diego Biurrun
        component = 0;
1983 dff0f035 Michael Niedermayer
    }else{
1984
        quant_matrix = s->chroma_intra_matrix;
1985
        component = (n&1) + 1;
1986
    }
1987
    diff = decode_dc(&s->gb, component);
1988
    if (diff >= 0xffff)
1989
        return -1;
1990
    dc = s->last_dc[component];
1991
    dc += diff;
1992
    s->last_dc[component] = dc;
1993
    block[0] = dc << (3 - s->intra_dc_precision);
1994
    if (s->intra_vlc_format)
1995
        rl = &rl_mpeg2;
1996
    else
1997
        rl = &rl_mpeg1;
1998
1999
    {
2000 115329f1 Diego Biurrun
        OPEN_READER(re, &s->gb);
2001 dff0f035 Michael Niedermayer
        /* now quantify & encode AC coefs */
2002
        for(;;) {
2003
            UPDATE_CACHE(re, &s->gb);
2004
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
2005 115329f1 Diego Biurrun
2006 dff0f035 Michael Niedermayer
            if(level == 127){
2007
                break;
2008
            } else if(level != 0) {
2009
                scantable += run;
2010
                j = *scantable;
2011
                level= (level*qscale*quant_matrix[j])>>4;
2012
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
2013
                LAST_SKIP_BITS(re, &s->gb, 1);
2014
            } else {
2015
                /* escape */
2016
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
2017
                UPDATE_CACHE(re, &s->gb);
2018
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
2019
                scantable += run;
2020
                j = *scantable;
2021
                if(level<0){
2022
                    level= (-level*qscale*quant_matrix[j])>>4;
2023
                    level= -level;
2024
                }else{
2025
                    level= (level*qscale*quant_matrix[j])>>4;
2026
                }
2027
            }
2028 115329f1 Diego Biurrun
2029 dff0f035 Michael Niedermayer
            block[j] = level;
2030
        }
2031
        CLOSE_READER(re, &s->gb);
2032
    }
2033 115329f1 Diego Biurrun
2034 dff0f035 Michael Niedermayer
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
2035
    return 0;
2036
}
2037
2038 de6d9b64 Fabrice Bellard
typedef struct Mpeg1Context {
2039
    MpegEncContext mpeg_enc_ctx;
2040
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
2041 1cb0edb4 Juanjo
    int repeat_field; /* true if we must repeat the field */
2042 fa384dcc Michael Niedermayer
    AVPanScan pan_scan; /** some temporary storage for the panscan */
2043 c62c07d3 Michael Niedermayer
    int slice_count;
2044 a6b9ffbf Ivan Kalvachev
    int swap_uv;//indicate VCR2
2045
    int save_aspect_info;
2046 c32b14bb Michael Niedermayer
    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
2047 a6b9ffbf Ivan Kalvachev
2048 de6d9b64 Fabrice Bellard
} Mpeg1Context;
2049
2050
static int mpeg_decode_init(AVCodecContext *avctx)
2051
{
2052
    Mpeg1Context *s = avctx->priv_data;
2053 3edcacde Michael Niedermayer
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
2054 d1700ead Ivan Kalvachev
    int i;
2055 115329f1 Diego Biurrun
2056 d1700ead Ivan Kalvachev
    //we need some parmutation to store
2057
    //matrixes, until MPV_common_init()
2058 115329f1 Diego Biurrun
    //set the real permutatuon
2059 d1700ead Ivan Kalvachev
    for(i=0;i<64;i++)
2060
       s2->dsp.idct_permutation[i]=i;
2061
2062 3edcacde Michael Niedermayer
    MPV_decode_defaults(s2);
2063 115329f1 Diego Biurrun
2064 303aebf9 Michael Niedermayer
    s->mpeg_enc_ctx.avctx= avctx;
2065 94aec31f Michael Niedermayer
    s->mpeg_enc_ctx.flags= avctx->flags;
2066 303e50e6 Michael Niedermayer
    s->mpeg_enc_ctx.flags2= avctx->flags2;
2067 8f8402e4 Michael Niedermayer
    common_init(&s->mpeg_enc_ctx);
2068 c3bf0288 Michael Niedermayer
    init_vlcs();
2069 de6d9b64 Fabrice Bellard
2070
    s->mpeg_enc_ctx_allocated = 0;
2071
    s->mpeg_enc_ctx.picture_number = 0;
2072 1cb0edb4 Juanjo
    s->repeat_field = 0;
2073 d7e9533a Michael Niedermayer
    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
2074 de6d9b64 Fabrice Bellard
    return 0;
2075
}
2076
2077 115329f1 Diego Biurrun
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
2078 a6b9ffbf Ivan Kalvachev
                                     const uint8_t *new_perm){
2079 c32b14bb Michael Niedermayer
    uint16_t temp_matrix[64];
2080
    int i;
2081 a6b9ffbf Ivan Kalvachev
2082
    memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
2083 115329f1 Diego Biurrun
2084 a6b9ffbf Ivan Kalvachev
    for(i=0;i<64;i++){
2085
        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
2086 115329f1 Diego Biurrun
    }
2087 a6b9ffbf Ivan Kalvachev
}
2088
2089
//Call this function when we know all parameters
2090
//it may be called in different places for mpeg1 and mpeg2
2091
static int mpeg_decode_postinit(AVCodecContext *avctx){
2092 c32b14bb Michael Niedermayer
    Mpeg1Context *s1 = avctx->priv_data;
2093
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2094
    uint8_t old_permutation[64];
2095 a6b9ffbf Ivan Kalvachev
2096
    if (
2097 bb270c08 Diego Biurrun
        (s1->mpeg_enc_ctx_allocated == 0)||
2098 21adafec Michael Niedermayer
        avctx->coded_width  != s->width ||
2099
        avctx->coded_height != s->height||
2100 aa26aa78 Ivan Kalvachev
        s1->save_aspect_info != s->aspect_ratio_info||
2101 a6b9ffbf Ivan Kalvachev
        0)
2102
    {
2103 115329f1 Diego Biurrun
2104 a6b9ffbf Ivan Kalvachev
        if (s1->mpeg_enc_ctx_allocated) {
2105 89ba9eed Michael Niedermayer
            ParseContext pc= s->parse_context;
2106
            s->parse_context.buffer=0;
2107 a6b9ffbf Ivan Kalvachev
            MPV_common_end(s);
2108 89ba9eed Michael Niedermayer
            s->parse_context= pc;
2109 a6b9ffbf Ivan Kalvachev
        }
2110
2111 bb270c08 Diego Biurrun
        if( (s->width == 0 )||(s->height == 0))
2112
            return -2;
2113 a6b9ffbf Ivan Kalvachev
2114 21adafec Michael Niedermayer
        avcodec_set_dimensions(avctx, s->width, s->height);
2115 a6b9ffbf Ivan Kalvachev
        avctx->bit_rate = s->bit_rate;
2116
        s1->save_aspect_info = s->aspect_ratio_info;
2117
2118
     //low_delay may be forced, in this case we will have B frames
2119
     //that behave like P frames
2120
        avctx->has_b_frames = !(s->low_delay);
2121
2122
        if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
2123
            //mpeg1 fps
2124 c0df9d75 Michael Niedermayer
            avctx->time_base.den     = frame_rate_tab[s->frame_rate_index].num;
2125
            avctx->time_base.num= frame_rate_tab[s->frame_rate_index].den;
2126 a6b9ffbf Ivan Kalvachev
            //mpeg1 aspect
2127
            avctx->sample_aspect_ratio= av_d2q(
2128
                    1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
2129
2130
        }else{//mpeg2
2131
        //mpeg2 fps
2132
            av_reduce(
2133 115329f1 Diego Biurrun
                &s->avctx->time_base.den,
2134
                &s->avctx->time_base.num,
2135 c32b14bb Michael Niedermayer
                frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
2136
                frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
2137 a6b9ffbf Ivan Kalvachev
                1<<30);
2138
        //mpeg2 aspect
2139
            if(s->aspect_ratio_info > 1){
2140 6fe1a1a5 Ivan Kalvachev
                if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
2141 115329f1 Diego Biurrun
                    s->avctx->sample_aspect_ratio=
2142 5ac47683 Ivan Kalvachev
                        av_div_q(
2143 115329f1 Diego Biurrun
                         mpeg2_aspect[s->aspect_ratio_info],
2144 5ac47683 Ivan Kalvachev
                         (AVRational){s->width, s->height}
2145
                         );
2146 a6b9ffbf Ivan Kalvachev
                }else{
2147 115329f1 Diego Biurrun
                    s->avctx->sample_aspect_ratio=
2148 a6b9ffbf Ivan Kalvachev
                        av_div_q(
2149 115329f1 Diego Biurrun
                         mpeg2_aspect[s->aspect_ratio_info],
2150 6fe1a1a5 Ivan Kalvachev
                         (AVRational){s1->pan_scan.width, s1->pan_scan.height}
2151 a6b9ffbf Ivan Kalvachev
                        );
2152 bb270c08 Diego Biurrun
                }
2153 a6b9ffbf Ivan Kalvachev
            }else{
2154 115329f1 Diego Biurrun
                s->avctx->sample_aspect_ratio=
2155 5ac47683 Ivan Kalvachev
                    mpeg2_aspect[s->aspect_ratio_info];
2156 a6b9ffbf Ivan Kalvachev
            }
2157
        }//mpeg2
2158
2159
        if(avctx->xvmc_acceleration){
2160
            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2161
        }else{
2162
            if(s->chroma_format <  2){
2163
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
2164
            }else
2165
            if(s->chroma_format == 2){
2166
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
2167
            }else
2168
            if(s->chroma_format >  2){
2169
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
2170
            }
2171
        }
2172
        //until then pix_fmt may be changed right after codec init
2173
        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2174
            if( avctx->idct_algo == FF_IDCT_AUTO )
2175
                avctx->idct_algo = FF_IDCT_SIMPLE;
2176
2177 115329f1 Diego Biurrun
        //quantization matrixes may need reordering
2178 a6b9ffbf Ivan Kalvachev
        //if dct permutation is changed
2179
        memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
2180
2181
        if (MPV_common_init(s) < 0)
2182
            return -2;
2183
2184
        quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
2185
        quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
2186
        quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
2187
        quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
2188
2189
        s1->mpeg_enc_ctx_allocated = 1;
2190
    }
2191
    return 0;
2192
}
2193
2194 115329f1 Diego Biurrun
static int mpeg1_decode_picture(AVCodecContext *avctx,
2195 5f0f7713 Zdenek Kabelac
                                const uint8_t *buf, int buf_size)
2196 de6d9b64 Fabrice Bellard
{
2197
    Mpeg1Context *s1 = avctx->priv_data;
2198
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2199 d60a8f85 Michael Niedermayer
    int ref, f_code, vbv_delay;
2200 de6d9b64 Fabrice Bellard
2201 115329f1 Diego Biurrun
    if(mpeg_decode_postinit(s->avctx) < 0)
2202 a6b9ffbf Ivan Kalvachev
       return -2;
2203
2204 68f593b4 Michael Niedermayer
    init_get_bits(&s->gb, buf, buf_size*8);
2205 de6d9b64 Fabrice Bellard
2206
    ref = get_bits(&s->gb, 10); /* temporal ref */
2207
    s->pict_type = get_bits(&s->gb, 3);
2208 71434945 Michael Niedermayer
    if(s->pict_type == 0 || s->pict_type > 3)
2209
        return -1;
2210 58bfafbe Michael Niedermayer
2211 d60a8f85 Michael Niedermayer
    vbv_delay= get_bits(&s->gb, 16);
2212 de6d9b64 Fabrice Bellard
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
2213 612476ef Arpi
        s->full_pel[0] = get_bits1(&s->gb);
2214 de6d9b64 Fabrice Bellard
        f_code = get_bits(&s->gb, 3);
2215 02fb0d7c Michael Niedermayer
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2216 de6d9b64 Fabrice Bellard
            return -1;
2217
        s->mpeg_f_code[0][0] = f_code;
2218
        s->mpeg_f_code[0][1] = f_code;
2219
    }
2220
    if (s->pict_type == B_TYPE) {
2221 612476ef Arpi
        s->full_pel[1] = get_bits1(&s->gb);
2222 de6d9b64 Fabrice Bellard
        f_code = get_bits(&s->gb, 3);
2223 02fb0d7c Michael Niedermayer
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
2224 de6d9b64 Fabrice Bellard
            return -1;
2225
        s->mpeg_f_code[1][0] = f_code;
2226
        s->mpeg_f_code[1][1] = f_code;
2227
    }
2228 1e491e29 Michael Niedermayer
    s->current_picture.pict_type= s->pict_type;
2229
    s->current_picture.key_frame= s->pict_type == I_TYPE;
2230 115329f1 Diego Biurrun
2231 71434945 Michael Niedermayer
    if(avctx->debug & FF_DEBUG_PICT_INFO)
2232
        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
2233 115329f1 Diego Biurrun
2234 de6d9b64 Fabrice Bellard
    s->y_dc_scale = 8;
2235
    s->c_dc_scale = 8;
2236
    s->first_slice = 1;
2237
    return 0;
2238
}
2239
2240 c32b14bb Michael Niedermayer
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
2241 de6d9b64 Fabrice Bellard
{
2242 c32b14bb Michael Niedermayer
    MpegEncContext *s= &s1->mpeg_enc_ctx;
2243 de6d9b64 Fabrice Bellard
    int horiz_size_ext, vert_size_ext;
2244 baaf3f46 Michael Niedermayer
    int bit_rate_ext;
2245 de6d9b64 Fabrice Bellard
2246 029911d1 Michael Niedermayer
    skip_bits(&s->gb, 1); /* profil and level esc*/
2247 baced9f5 Michael Niedermayer
    s->avctx->profile= get_bits(&s->gb, 3);
2248
    s->avctx->level= get_bits(&s->gb, 4);
2249 1cb0edb4 Juanjo
    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
2250 5e5c247a Ivan Kalvachev
    s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
2251 de6d9b64 Fabrice Bellard
    horiz_size_ext = get_bits(&s->gb, 2);
2252
    vert_size_ext = get_bits(&s->gb, 2);
2253
    s->width |= (horiz_size_ext << 12);
2254
    s->height |= (vert_size_ext << 12);
2255
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
2256 27ef18d1 Michael Niedermayer
    s->bit_rate += (bit_rate_ext << 18) * 400;
2257 612476ef Arpi
    skip_bits1(&s->gb); /* marker */
2258 0368c72d Michael Niedermayer
    s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
2259 e0560448 Michael Niedermayer
2260 945f15b7 Michael Niedermayer
    s->low_delay = get_bits1(&s->gb);
2261 e0560448 Michael Niedermayer
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2262
2263 c32b14bb Michael Niedermayer
    s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
2264
    s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
2265 14bea432 Michael Niedermayer
2266 de6d9b64 Fabrice Bellard
    dprintf("sequence extension\n");
2267 029911d1 Michael Niedermayer
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2268 fb4a4a56 Fabrice Bellard
    s->avctx->sub_id = 2; /* indicates mpeg2 found */
2269 945f15b7 Michael Niedermayer
2270 029911d1 Michael Niedermayer
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2271 115329f1 Diego Biurrun
        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
2272 baced9f5 Michael Niedermayer
               s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
2273 a6b9ffbf Ivan Kalvachev
2274 de6d9b64 Fabrice Bellard
}
2275
2276 fa384dcc Michael Niedermayer
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
2277
{
2278
    MpegEncContext *s= &s1->mpeg_enc_ctx;
2279
    int color_description, w, h;
2280
2281
    skip_bits(&s->gb, 3); /* video format */
2282
    color_description= get_bits1(&s->gb);
2283
    if(color_description){
2284
        skip_bits(&s->gb, 8); /* color primaries */
2285
        skip_bits(&s->gb, 8); /* transfer_characteristics */
2286
        skip_bits(&s->gb, 8); /* matrix_coefficients */
2287
    }
2288
    w= get_bits(&s->gb, 14);
2289
    skip_bits(&s->gb, 1); //marker
2290
    h= get_bits(&s->gb, 14);
2291
    skip_bits(&s->gb, 1); //marker
2292 115329f1 Diego Biurrun
2293 fa384dcc Michael Niedermayer
    s1->pan_scan.width= 16*w;
2294
    s1->pan_scan.height=16*h;
2295 115329f1 Diego Biurrun
2296 fa384dcc Michael Niedermayer
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2297 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
2298 fa384dcc Michael Niedermayer
}
2299
2300
static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
2301
{
2302
    MpegEncContext *s= &s1->mpeg_enc_ctx;
2303 6fe1a1a5 Ivan Kalvachev
    int i,nofco;
2304
2305
    nofco = 1;
2306
    if(s->progressive_sequence){
2307
        if(s->repeat_first_field){
2308 bb270c08 Diego Biurrun
            nofco++;
2309
            if(s->top_field_first)
2310
                nofco++;
2311
        }
2312 6fe1a1a5 Ivan Kalvachev
    }else{
2313
        if(s->picture_structure == PICT_FRAME){
2314
            nofco++;
2315 bb270c08 Diego Biurrun
            if(s->repeat_first_field)
2316
                nofco++;
2317
        }
2318 6fe1a1a5 Ivan Kalvachev
    }
2319
    for(i=0; i<nofco; i++){
2320 fa384dcc Michael Niedermayer
        s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
2321
        skip_bits(&s->gb, 1); //marker
2322
        s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
2323
        skip_bits(&s->gb, 1); //marker
2324
    }
2325 115329f1 Diego Biurrun
2326 fa384dcc Michael Niedermayer
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2327 115329f1 Diego Biurrun
        av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
2328
            s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
2329
            s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
2330 fa384dcc Michael Niedermayer
            s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
2331
        );
2332
}
2333
2334 de6d9b64 Fabrice Bellard
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
2335
{
2336 60832448 Fabrice Bellard
    int i, v, j;
2337 de6d9b64 Fabrice Bellard
2338 25ed7f92 Fabrice Bellard
    dprintf("matrix extension\n");
2339
2340 612476ef Arpi
    if (get_bits1(&s->gb)) {
2341 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
2342
            v = get_bits(&s->gb, 8);
2343 b0368839 Michael Niedermayer
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2344 60832448 Fabrice Bellard
            s->intra_matrix[j] = v;
2345
            s->chroma_intra_matrix[j] = v;
2346 de6d9b64 Fabrice Bellard
        }
2347
    }
2348 612476ef Arpi
    if (get_bits1(&s->gb)) {
2349 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
2350
            v = get_bits(&s->gb, 8);
2351 b0368839 Michael Niedermayer
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2352 d7e9533a Michael Niedermayer
            s->inter_matrix[j] = v;
2353
            s->chroma_inter_matrix[j] = v;
2354 de6d9b64 Fabrice Bellard
        }
2355
    }
2356 612476ef Arpi
    if (get_bits1(&s->gb)) {
2357 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
2358
            v = get_bits(&s->gb, 8);
2359 b0368839 Michael Niedermayer
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2360 60832448 Fabrice Bellard
            s->chroma_intra_matrix[j] = v;
2361 de6d9b64 Fabrice Bellard
        }
2362
    }
2363 612476ef Arpi
    if (get_bits1(&s->gb)) {
2364 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
2365
            v = get_bits(&s->gb, 8);
2366 b0368839 Michael Niedermayer
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2367 d7e9533a Michael Niedermayer
            s->chroma_inter_matrix[j] = v;
2368 de6d9b64 Fabrice Bellard
        }
2369
    }
2370
}
2371
2372
static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
2373
{
2374
    s->full_pel[0] = s->full_pel[1] = 0;
2375
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
2376
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
2377
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
2378
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
2379
    s->intra_dc_precision = get_bits(&s->gb, 2);
2380
    s->picture_structure = get_bits(&s->gb, 2);
2381 612476ef Arpi
    s->top_field_first = get_bits1(&s->gb);
2382
    s->frame_pred_frame_dct = get_bits1(&s->gb);
2383
    s->concealment_motion_vectors = get_bits1(&s->gb);
2384
    s->q_scale_type = get_bits1(&s->gb);
2385
    s->intra_vlc_format = get_bits1(&s->gb);
2386
    s->alternate_scan = get_bits1(&s->gb);
2387
    s->repeat_first_field = get_bits1(&s->gb);
2388
    s->chroma_420_type = get_bits1(&s->gb);
2389
    s->progressive_frame = get_bits1(&s->gb);
2390 bb198e19 Michael Niedermayer
2391 dfb476cb Michael Niedermayer
    if(s->picture_structure == PICT_FRAME)
2392
        s->first_field=0;
2393
    else{
2394
        s->first_field ^= 1;
2395 7bc9090a Michael Niedermayer
        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
2396 dfb476cb Michael Niedermayer
    }
2397 115329f1 Diego Biurrun
2398 acf44abb Michael Niedermayer
    if(s->alternate_scan){
2399 3d2e8cce Michael Niedermayer
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
2400
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
2401 acf44abb Michael Niedermayer
    }else{
2402 3d2e8cce Michael Niedermayer
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
2403
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
2404 acf44abb Michael Niedermayer
    }
2405 115329f1 Diego Biurrun
2406 de6d9b64 Fabrice Bellard
    /* composite display not parsed */
2407 1cb0edb4 Juanjo
    dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
2408 25ed7f92 Fabrice Bellard
    dprintf("picture_structure=%d\n", s->picture_structure);
2409 e0a3d744 Juanjo
    dprintf("top field first=%d\n", s->top_field_first);
2410
    dprintf("repeat first field=%d\n", s->repeat_first_field);
2411 de6d9b64 Fabrice Bellard
    dprintf("conceal=%d\n", s->concealment_motion_vectors);
2412 25ed7f92 Fabrice Bellard
    dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
2413
    dprintf("alternate_scan=%d\n", s->alternate_scan);
2414 de6d9b64 Fabrice Bellard
    dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
2415 1cb0edb4 Juanjo
    dprintf("progressive_frame=%d\n", s->progressive_frame);
2416 de6d9b64 Fabrice Bellard
}
2417
2418 115329f1 Diego Biurrun
static void mpeg_decode_extension(AVCodecContext *avctx,
2419 5f0f7713 Zdenek Kabelac
                                  const uint8_t *buf, int buf_size)
2420 de6d9b64 Fabrice Bellard
{
2421
    Mpeg1Context *s1 = avctx->priv_data;
2422
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2423
    int ext_type;
2424
2425 68f593b4 Michael Niedermayer
    init_get_bits(&s->gb, buf, buf_size*8);
2426 115329f1 Diego Biurrun
2427 de6d9b64 Fabrice Bellard
    ext_type = get_bits(&s->gb, 4);
2428
    switch(ext_type) {
2429
    case 0x1:
2430 c32b14bb Michael Niedermayer
        mpeg_decode_sequence_extension(s1);
2431 de6d9b64 Fabrice Bellard
        break;
2432 fa384dcc Michael Niedermayer
    case 0x2:
2433
        mpeg_decode_sequence_display_extension(s1);
2434
        break;
2435 de6d9b64 Fabrice Bellard
    case 0x3:
2436
        mpeg_decode_quant_matrix_extension(s);
2437
        break;
2438 fa384dcc Michael Niedermayer
    case 0x7:
2439
        mpeg_decode_picture_display_extension(s1);
2440
        break;
2441 de6d9b64 Fabrice Bellard
    case 0x8:
2442
        mpeg_decode_picture_coding_extension(s);
2443
        break;
2444
    }
2445
}
2446
2447 a579db0c Ivan Kalvachev
static void exchange_uv(MpegEncContext *s){
2448 c32b14bb Michael Niedermayer
    short * tmp = s->pblocks[4];
2449 a579db0c Ivan Kalvachev
    s->pblocks[4] = s->pblocks[5];
2450
    s->pblocks[5] = tmp;
2451 ff862be5 Michael Niedermayer
}
2452
2453 c62c07d3 Michael Niedermayer
static int mpeg_field_start(MpegEncContext *s){
2454
    AVCodecContext *avctx= s->avctx;
2455
    Mpeg1Context *s1 = (Mpeg1Context*)s;
2456 de6d9b64 Fabrice Bellard
2457
    /* start frame decoding */
2458 c62c07d3 Michael Niedermayer
    if(s->first_field || s->picture_structure==PICT_FRAME){
2459 d6db1c9c Michael Niedermayer
        if(MPV_frame_start(s, avctx) < 0)
2460 c62c07d3 Michael Niedermayer
            return -1;
2461 7bc9090a Michael Niedermayer
2462
        ff_er_frame_start(s);
2463
2464 2ec23b6d Michael Niedermayer
        /* first check if we must repeat the frame */
2465 9ee2c20e Fabrice Bellard
        s->current_picture_ptr->repeat_pict = 0;
2466 2ec23b6d Michael Niedermayer
        if (s->repeat_first_field) {
2467
            if (s->progressive_sequence) {
2468
                if (s->top_field_first)
2469 9ee2c20e Fabrice Bellard
                    s->current_picture_ptr->repeat_pict = 4;
2470 2ec23b6d Michael Niedermayer
                else
2471 9ee2c20e Fabrice Bellard
                    s->current_picture_ptr->repeat_pict = 2;
2472 2ec23b6d Michael Niedermayer
            } else if (s->progressive_frame) {
2473 9ee2c20e Fabrice Bellard
                s->current_picture_ptr->repeat_pict = 1;
2474 2ec23b6d Michael Niedermayer
            }
2475 115329f1 Diego Biurrun
        }
2476 fa384dcc Michael Niedermayer
2477
        *s->current_picture_ptr->pan_scan= s1->pan_scan;
2478 c62c07d3 Michael Niedermayer
    }else{ //second field
2479 b536d0aa Michael Niedermayer
            int i;
2480 115329f1 Diego Biurrun
2481 37b787f1 Michael Niedermayer
            if(!s->current_picture_ptr){
2482 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
2483 37b787f1 Michael Niedermayer
                return -1;
2484
            }
2485 115329f1 Diego Biurrun
2486 b536d0aa Michael Niedermayer
            for(i=0; i<4; i++){
2487
                s->current_picture.data[i] = s->current_picture_ptr->data[i];
2488
                if(s->picture_structure == PICT_BOTTOM_FIELD){
2489
                    s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
2490 115329f1 Diego Biurrun
                }
2491 b536d0aa Michael Niedermayer
            }
2492 c62c07d3 Michael Niedermayer
    }
2493 2e7b4c84 Ivan Kalvachev
#ifdef HAVE_XVMC
2494
// MPV_frame_start will call this function too,
2495
// but we need to call it on every field
2496 c62c07d3 Michael Niedermayer
    if(s->avctx->xvmc_acceleration)
2497 2e7b4c84 Ivan Kalvachev
         XVMC_field_start(s,avctx);
2498
#endif
2499 de6d9b64 Fabrice Bellard
2500 c62c07d3 Michael Niedermayer
    return 0;
2501
}
2502
2503
#define DECODE_SLICE_ERROR -1
2504
#define DECODE_SLICE_OK 0
2505
2506
/**
2507
 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
2508
 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
2509
 *         DECODE_SLICE_OK if this slice is ok<br>
2510
 */
2511
static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
2512 5f0f7713 Zdenek Kabelac
                             const uint8_t **buf, int buf_size)
2513 c62c07d3 Michael Niedermayer
{
2514
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2515
    AVCodecContext *avctx= s->avctx;
2516
    int ret;
2517
    const int field_pic= s->picture_structure != PICT_FRAME;
2518 178fcca8 Michael Niedermayer
    const int lowres= s->avctx->lowres;
2519 c62c07d3 Michael Niedermayer
2520
    s->resync_mb_x=
2521
    s->resync_mb_y= -1;
2522
2523 25ef43bb Michael Niedermayer
    if (mb_y<<field_pic >= s->mb_height){
2524
        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
2525 c62c07d3 Michael Niedermayer
        return -1;
2526
    }
2527 115329f1 Diego Biurrun
2528 80097bbf Michael Niedermayer
    init_get_bits(&s->gb, *buf, buf_size*8);
2529 de6d9b64 Fabrice Bellard
2530 c62c07d3 Michael Niedermayer
    ff_mpeg1_clean_buffers(s);
2531
    s->interlaced_dct = 0;
2532
2533 0ee50938 Fabrice Bellard
    s->qscale = get_qscale(s);
2534 a4337a51 Michael Niedermayer
2535 7bc9090a Michael Niedermayer
    if(s->qscale == 0){
2536 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
2537 db6e7795 Michael Niedermayer
        return -1;
2538 7bc9090a Michael Niedermayer
    }
2539 115329f1 Diego Biurrun
2540 de6d9b64 Fabrice Bellard
    /* extra slice info */
2541 612476ef Arpi
    while (get_bits1(&s->gb) != 0) {
2542
        skip_bits(&s->gb, 8);
2543 de6d9b64 Fabrice Bellard
    }
2544 115329f1 Diego Biurrun
2545 ce5b7c5e Michael Niedermayer
    s->mb_x=0;
2546 7bc9090a Michael Niedermayer
2547 ce5b7c5e Michael Niedermayer
    for(;;) {
2548
        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2549 7bc9090a Michael Niedermayer
        if (code < 0){
2550 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
2551 db6e7795 Michael Niedermayer
            return -1;
2552 7bc9090a Michael Niedermayer
        }
2553 ce5b7c5e Michael Niedermayer
        if (code >= 33) {
2554
            if (code == 33) {
2555
                s->mb_x += 33;
2556
            }
2557
            /* otherwise, stuffing, nothing to do */
2558
        } else {
2559
            s->mb_x += code;
2560
            break;
2561
        }
2562
    }
2563 c62c07d3 Michael Niedermayer
2564 7bc9090a Michael Niedermayer
    s->resync_mb_x= s->mb_x;
2565 c62c07d3 Michael Niedermayer
    s->resync_mb_y= s->mb_y= mb_y;
2566 9b8709d1 Michael Niedermayer
    s->mb_skip_run= 0;
2567 7d1c3fc1 Michael Niedermayer
    ff_init_block_index(s);
2568 ce5b7c5e Michael Niedermayer
2569 c62c07d3 Michael Niedermayer
    if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
2570
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2571 115329f1 Diego Biurrun
             av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
2572 c62c07d3 Michael Niedermayer
                 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
2573 115329f1 Diego Biurrun
                 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
2574
                 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
2575 c62c07d3 Michael Niedermayer
                 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
2576
                 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
2577
        }
2578 115329f1 Diego Biurrun
    }
2579
2580 de6d9b64 Fabrice Bellard
    for(;;) {
2581 a579db0c Ivan Kalvachev
#ifdef HAVE_XVMC
2582
        //one 1 we memcpy blocks in xvmcvideo
2583
        if(s->avctx->xvmc_acceleration > 1)
2584
            XVMC_init_block(s);//set s->block
2585
#endif
2586
2587 b7ec19d3 Fabrice Bellard
        ret = mpeg_decode_mb(s, s->block);
2588 332f9ac4 Michael Niedermayer
        s->chroma_qscale= s->qscale;
2589 7bc9090a Michael Niedermayer
2590 de6d9b64 Fabrice Bellard
        dprintf("ret=%d\n", ret);
2591
        if (ret < 0)
2592
            return -1;
2593 8d7ec294 Wolfgang Hesseler
2594 31b1ec5d Michael Niedermayer
        if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
2595 137c8468 Michael Niedermayer
            const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
2596
            int xy = s->mb_x*2 + s->mb_y*2*wrap;
2597 4c9544d0 Michael Niedermayer
            int motion_x, motion_y, dir, i;
2598 31b1ec5d Michael Niedermayer
            if(field_pic && !s->first_field)
2599
                xy += wrap/2;
2600
2601 50c93f74 Michael Niedermayer
            for(i=0; i<2; i++){
2602
                for(dir=0; dir<2; dir++){
2603 4c9544d0 Michael Niedermayer
                    if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
2604
                        motion_x = motion_y = 0;
2605 0c352cad Michael Niedermayer
                    }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
2606 4c9544d0 Michael Niedermayer
                        motion_x = s->mv[dir][0][0];
2607
                        motion_y = s->mv[dir][0][1];
2608
                    } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
2609
                        motion_x = s->mv[dir][i][0];
2610
                        motion_y = s->mv[dir][i][1];
2611
                    }
2612 7c4f71c4 Michael Niedermayer
2613 4c9544d0 Michael Niedermayer
                    s->current_picture.motion_val[dir][xy    ][0] = motion_x;
2614
                    s->current_picture.motion_val[dir][xy    ][1] = motion_y;
2615
                    s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
2616
                    s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
2617 7c4f71c4 Michael Niedermayer
                    s->current_picture.ref_index [dir][xy    ]=
2618
                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
2619 4ad8ecd1 Michael Niedermayer
                    assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
2620 4c9544d0 Michael Niedermayer
                }
2621 50c93f74 Michael Niedermayer
                xy += wrap;
2622 7bc9090a Michael Niedermayer
            }
2623
        }
2624 8d7ec294 Wolfgang Hesseler
2625 178fcca8 Michael Niedermayer
        s->dest[0] += 16 >> lowres;
2626
        s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
2627
        s->dest[2] += 16 >> (s->chroma_x_shift + lowres);