Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dv.c @ 7458ccbb

History | View | Annotate | Download (17.9 KB)

1 5d2231ee Fabrice Bellard
/*
2
 * DV decoder
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19 983e3246 Michael Niedermayer
20
/**
21
 * @file dv.c
22
 * DV decoder.
23
 */
24 5d2231ee Fabrice Bellard
#include "avcodec.h"
25
#include "dsputil.h"
26
#include "mpegvideo.h"
27
#include "simple_idct.h"
28 7458ccbb Roman Shaposhnik
#include "dvdata.h"
29 5d2231ee Fabrice Bellard
30
typedef struct DVVideoDecodeContext {
31 7458ccbb Roman Shaposhnik
    const DVprofile* sys;
32 5d2231ee Fabrice Bellard
    GetBitContext gb;
33 492cd3a9 Michael Niedermayer
    AVFrame picture;
34 5d2231ee Fabrice Bellard
    DCTELEM block[5*6][64] __align8;
35 7458ccbb Roman Shaposhnik
    
36
    /* FIXME: the following is extracted from DSP */
37 0c1a9eda Zdenek Kabelac
    uint8_t dv_zigzag[2][64];
38
    uint8_t idct_permutation[64];
39 7458ccbb Roman Shaposhnik
    void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
40
    void (*fdct)(DCTELEM *block);
41
    
42 5d2231ee Fabrice Bellard
    /* XXX: move it to static storage ? */
43 0c1a9eda Zdenek Kabelac
    uint8_t dv_shift[2][22][64];
44
    void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
45 5d2231ee Fabrice Bellard
} DVVideoDecodeContext;
46
47 7458ccbb Roman Shaposhnik
#define TEX_VLC_BITS 9
48 5d2231ee Fabrice Bellard
/* XXX: also include quantization */
49
static RL_VLC_ELEM *dv_rl_vlc[1];
50 7458ccbb Roman Shaposhnik
static VLC_TYPE dv_vlc_codes[15][23];
51 5d2231ee Fabrice Bellard
52
static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
53
{
54 7be166e4 Fabrice Bellard
    int i, q, j;
55 5d2231ee Fabrice Bellard
56
    /* NOTE: max left shift is 6 */
57 7be166e4 Fabrice Bellard
    for(q = 0; q < 22; q++) {
58
        /* 88 unquant */
59
        for(i = 1; i < 64; i++) {
60
            /* 88 table */
61
            j = s->idct_permutation[i];
62
            s->dv_shift[0][q][j] =
63
                dv_quant_shifts[q][dv_88_areas[i]] + 1;
64
        }
65
        
66
        /* 248 unquant */
67
        for(i = 1; i < 64; i++) {
68
            /* 248 table */
69
            s->dv_shift[1][q][i] =  
70
                    dv_quant_shifts[q][dv_248_areas[i]] + 1;
71 5d2231ee Fabrice Bellard
        }
72
    }
73
}
74
75
static int dvvideo_decode_init(AVCodecContext *avctx)
76
{
77
    DVVideoDecodeContext *s = avctx->priv_data;
78 7be166e4 Fabrice Bellard
    MpegEncContext s2;
79 7604246d Wolfgang Hesseler
    static int done=0;
80 5d2231ee Fabrice Bellard
81
    if (!done) {
82
        int i;
83 7458ccbb Roman Shaposhnik
        VLC dv_vlc;
84 5d2231ee Fabrice Bellard
85
        done = 1;
86
87
        /* NOTE: as a trick, we use the fact the no codes are unused
88
           to accelerate the parsing of partial codes */
89
        init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, 
90
                 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
91
92
        dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
93
        for(i = 0; i < dv_vlc.table_size; i++){
94
            int code= dv_vlc.table[i][0];
95
            int len = dv_vlc.table[i][1];
96
            int level, run;
97
        
98
            if(len<0){ //more bits needed
99
                run= 0;
100
                level= code;
101
            } else if (code == (NB_DV_VLC - 1)) {
102
                /* EOB */
103
                run = 0;
104
                level = 256;
105
            } else {
106
                run=   dv_vlc_run[code] + 1;
107
                level= dv_vlc_level[code];
108
            }
109
            dv_rl_vlc[0][i].len = len;
110
            dv_rl_vlc[0][i].level = level;
111
            dv_rl_vlc[0][i].run = run;
112
        }
113 7458ccbb Roman Shaposhnik
114
        memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
115
        for (i = 0; i < NB_DV_VLC - 1; i++) {
116
           if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
117
               dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
118
        }
119 5d2231ee Fabrice Bellard
    }
120 7be166e4 Fabrice Bellard
121
    /* ugly way to get the idct & scantable */
122
    /* XXX: fix it */
123
    memset(&s2, 0, sizeof(MpegEncContext));
124
    s2.avctx = avctx;
125 b0368839 Michael Niedermayer
    dsputil_init(&s2.dsp, avctx);
126 defdfc9a Alex Beregszaszi
    if (DCT_common_init(&s2) < 0)
127 7be166e4 Fabrice Bellard
       return -1;
128
129 7458ccbb Roman Shaposhnik
    s->get_pixels = s2.dsp.get_pixels;
130
    s->fdct = s2.dsp.fdct;
131
    
132 b0368839 Michael Niedermayer
    s->idct_put[0] = s2.dsp.idct_put;
133
    memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
134 7be166e4 Fabrice Bellard
    memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
135
136
    /* XXX: use MMX also for idct248 */
137
    s->idct_put[1] = simple_idct248_put;
138
    memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
139
140 5d2231ee Fabrice Bellard
    /* XXX: do it only for constant case */
141
    dv_build_unquantize_tables(s);
142 7458ccbb Roman Shaposhnik
143 5d2231ee Fabrice Bellard
    return 0;
144
}
145
146 7458ccbb Roman Shaposhnik
// #define VLC_DEBUG
147 5d2231ee Fabrice Bellard
148 7be166e4 Fabrice Bellard
typedef struct BlockInfo {
149 0c1a9eda Zdenek Kabelac
    const uint8_t *shift_table;
150
    const uint8_t *scan_table;
151
    uint8_t pos; /* position in block */
152
    uint8_t eob_reached; /* true if EOB has been reached */
153
    uint8_t dct_mode;
154
    uint8_t partial_bit_count;
155
    uint16_t partial_bit_buffer;
156 7be166e4 Fabrice Bellard
    int shift_offset;
157
} BlockInfo;
158 5d2231ee Fabrice Bellard
159
/* block size in bits */
160 0c1a9eda Zdenek Kabelac
static const uint16_t block_sizes[6] = {
161 5d2231ee Fabrice Bellard
    112, 112, 112, 112, 80, 80
162
};
163
164
#ifndef ALT_BITSTREAM_READER
165 924311cd Michael Niedermayer
#warning only works with ALT_BITSTREAM_READER
166 5d2231ee Fabrice Bellard
#endif
167
168
/* decode ac coefs */
169
static void dv_decode_ac(DVVideoDecodeContext *s, 
170 0e15384d Michael Niedermayer
                         BlockInfo *mb, DCTELEM *block, int last_index)
171 5d2231ee Fabrice Bellard
{
172
    int last_re_index;
173 7be166e4 Fabrice Bellard
    int shift_offset = mb->shift_offset;
174 0c1a9eda Zdenek Kabelac
    const uint8_t *scan_table = mb->scan_table;
175
    const uint8_t *shift_table = mb->shift_table;
176 7be166e4 Fabrice Bellard
    int pos = mb->pos;
177 5d2231ee Fabrice Bellard
    int level, pos1, sign, run;
178
    int partial_bit_count;
179 924311cd Michael Niedermayer
#ifndef ALT_BITSTREAM_READER //FIXME
180
    int re_index=0; 
181
    int re1_index=0;
182
#endif
183 5d2231ee Fabrice Bellard
    OPEN_READER(re, &s->gb);
184
    
185
#ifdef VLC_DEBUG
186 7be166e4 Fabrice Bellard
    printf("start\n");
187 5d2231ee Fabrice Bellard
#endif
188
189
    /* if we must parse a partial vlc, we do it here */
190 7be166e4 Fabrice Bellard
    partial_bit_count = mb->partial_bit_count;
191 5d2231ee Fabrice Bellard
    if (partial_bit_count > 0) {
192 0c1a9eda Zdenek Kabelac
        uint8_t buf[4];
193
        uint32_t v;
194 5d2231ee Fabrice Bellard
        int l, l1;
195
        GetBitContext gb1;
196
197
        /* build the dummy bit buffer */
198
        l = 16 - partial_bit_count;
199
        UPDATE_CACHE(re, &s->gb);
200
#ifdef VLC_DEBUG
201
        printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
202
#endif
203 7be166e4 Fabrice Bellard
        v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
204 5d2231ee Fabrice Bellard
        buf[0] = v >> 8;
205
        buf[1] = v;
206
#ifdef VLC_DEBUG
207
        printf("v=%04x cnt=%d %04x\n", 
208 7be166e4 Fabrice Bellard
               v, partial_bit_count, (mb->partial_bit_buffer << l));
209 5d2231ee Fabrice Bellard
#endif
210
        /* try to read the codeword */
211 68f593b4 Michael Niedermayer
        init_get_bits(&gb1, buf, 4*8);
212 5d2231ee Fabrice Bellard
        {
213
            OPEN_READER(re1, &gb1);
214
            UPDATE_CACHE(re1, &gb1);
215
            GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], 
216
                       TEX_VLC_BITS, 2);
217
            l = re1_index;
218
            CLOSE_READER(re1, &gb1);
219
        }
220
#ifdef VLC_DEBUG
221
        printf("****run=%d level=%d size=%d\n", run, level, l);
222
#endif
223
        /* compute codeword length */
224
        l1 = (level != 256 && level != 0);
225
        /* if too long, we cannot parse */
226
        l -= partial_bit_count;
227
        if ((re_index + l + l1) > last_index)
228
            return;
229
        /* skip read bits */
230
        last_re_index = 0; /* avoid warning */
231
        re_index += l;
232 7be166e4 Fabrice Bellard
        /* by definition, if we can read the vlc, all partial bits
233 5d2231ee Fabrice Bellard
           will be read (otherwise we could have read the vlc before) */
234 7be166e4 Fabrice Bellard
        mb->partial_bit_count = 0;
235 5d2231ee Fabrice Bellard
        UPDATE_CACHE(re, &s->gb);
236
        goto handle_vlc;
237
    }
238
239
    /* get the AC coefficients until last_index is reached */
240
    for(;;) {
241
        UPDATE_CACHE(re, &s->gb);
242
#ifdef VLC_DEBUG
243
        printf("%2d: bits=%04x index=%d\n", 
244
               pos, SHOW_UBITS(re, &s->gb, 16), re_index);
245
#endif
246
        last_re_index = re_index;
247
        GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], 
248
                   TEX_VLC_BITS, 2);
249
    handle_vlc:
250
#ifdef VLC_DEBUG
251
        printf("run=%d level=%d\n", run, level);
252
#endif
253
        if (level == 256) {
254
            if (re_index > last_index) {
255
            cannot_read:
256
                /* put position before read code */
257
                re_index = last_re_index;
258 7be166e4 Fabrice Bellard
                mb->eob_reached = 0;
259 5d2231ee Fabrice Bellard
                break;
260
            }
261
            /* EOB */
262 7be166e4 Fabrice Bellard
            mb->eob_reached = 1;
263 5d2231ee Fabrice Bellard
            break;
264
        } else if (level != 0) {
265
            if ((re_index + 1) > last_index)
266
                goto cannot_read;
267
            sign = SHOW_SBITS(re, &s->gb, 1);
268
            level = (level ^ sign) - sign;
269
            LAST_SKIP_BITS(re, &s->gb, 1);
270
            pos += run;
271
            /* error */
272
            if (pos >= 64) {
273
                goto read_error;
274
            }
275
            pos1 = scan_table[pos];
276 7be166e4 Fabrice Bellard
            level = level << (shift_table[pos1] + shift_offset);
277 5d2231ee Fabrice Bellard
            block[pos1] = level;
278
            //            printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
279
        } else {
280
            if (re_index > last_index)
281
                goto cannot_read;
282
            /* level is zero: means run without coding. No
283
               sign is coded */
284
            pos += run;
285
            /* error */
286
            if (pos >= 64) {
287
            read_error:
288
#if defined(VLC_DEBUG) || 1
289 ed543377 Roman Shaposhnik
                fprintf(stderr, "error pos=%d\n", pos);
290 5d2231ee Fabrice Bellard
#endif
291
                /* for errors, we consider the eob is reached */
292 7be166e4 Fabrice Bellard
                mb->eob_reached = 1;
293 5d2231ee Fabrice Bellard
                break;
294
            }
295
        }
296
    }
297
    CLOSE_READER(re, &s->gb);
298 7be166e4 Fabrice Bellard
    mb->pos = pos;
299 5d2231ee Fabrice Bellard
}
300
301
static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
302
{
303
    while (bits_left >= 16) {
304
        put_bits(pb, 16, get_bits(gb, 16));
305
        bits_left -= 16;
306
    }
307
    if (bits_left > 0) {
308
        put_bits(pb, bits_left, get_bits(gb, bits_left));
309
    }
310
}
311
312
/* mb_x and mb_y are in units of 8 pixels */
313
static inline void dv_decode_video_segment(DVVideoDecodeContext *s, 
314 0c1a9eda Zdenek Kabelac
                                           uint8_t *buf_ptr1, 
315
                                           const uint16_t *mb_pos_ptr)
316 5d2231ee Fabrice Bellard
{
317
    int quant, dc, dct_mode, class1, j;
318
    int mb_index, mb_x, mb_y, v, last_index;
319
    DCTELEM *block, *block1;
320
    int c_offset, bits_left;
321 0c1a9eda Zdenek Kabelac
    uint8_t *y_ptr;
322 7be166e4 Fabrice Bellard
    BlockInfo mb_data[5 * 6], *mb, *mb1;
323 0c1a9eda Zdenek Kabelac
    void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
324
    uint8_t *buf_ptr;
325 5d2231ee Fabrice Bellard
    PutBitContext pb, vs_pb;
326 0c1a9eda Zdenek Kabelac
    uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
327 7be166e4 Fabrice Bellard
    int mb_bit_count;
328 0c1a9eda Zdenek Kabelac
    uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
329 5d2231ee Fabrice Bellard
    int vs_bit_count;
330
    
331
    memset(s->block, 0, sizeof(s->block));
332
333
    /* pass 1 : read DC and AC coefficients in blocks */
334
    buf_ptr = buf_ptr1;
335
    block1 = &s->block[0][0];
336 7be166e4 Fabrice Bellard
    mb1 = mb_data;
337 5d2231ee Fabrice Bellard
    init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
338
    vs_bit_count = 0;
339
    for(mb_index = 0; mb_index < 5; mb_index++) {
340
        /* skip header */
341
        quant = buf_ptr[3] & 0x0f;
342
        buf_ptr += 4;
343 7be166e4 Fabrice Bellard
        init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
344
        mb_bit_count = 0;
345
        mb = mb1;
346 5d2231ee Fabrice Bellard
        block = block1;
347
        for(j = 0;j < 6; j++) {
348
            /* NOTE: size is not important here */
349 68f593b4 Michael Niedermayer
            init_get_bits(&s->gb, buf_ptr, 14*8);
350 5d2231ee Fabrice Bellard
            
351
            /* get the dc */
352
            dc = get_bits(&s->gb, 9);
353
            dc = (dc << (32 - 9)) >> (32 - 9);
354
            dct_mode = get_bits1(&s->gb);
355 7be166e4 Fabrice Bellard
            mb->dct_mode = dct_mode;
356
            mb->scan_table = s->dv_zigzag[dct_mode];
357 5d2231ee Fabrice Bellard
            class1 = get_bits(&s->gb, 2);
358 7be166e4 Fabrice Bellard
            mb->shift_offset = (class1 == 3);
359
            mb->shift_table = s->dv_shift[dct_mode]
360 5d2231ee Fabrice Bellard
                [quant + dv_quant_offset[class1]];
361
            dc = dc << 2;
362
            /* convert to unsigned because 128 is not added in the
363
               standard IDCT */
364
            dc += 1024;
365
            block[0] = dc;
366
            last_index = block_sizes[j];
367
            buf_ptr += last_index >> 3;
368 7be166e4 Fabrice Bellard
            mb->pos = 0;
369
            mb->partial_bit_count = 0;
370 5d2231ee Fabrice Bellard
371 7be166e4 Fabrice Bellard
            dv_decode_ac(s, mb, block, last_index);
372 5d2231ee Fabrice Bellard
373
            /* write the remaining bits  in a new buffer only if the
374
               block is finished */
375 924311cd Michael Niedermayer
            bits_left = last_index - get_bits_count(&s->gb);
376 7be166e4 Fabrice Bellard
            if (mb->eob_reached) {
377
                mb->partial_bit_count = 0;
378
                mb_bit_count += bits_left;
379 5d2231ee Fabrice Bellard
                bit_copy(&pb, &s->gb, bits_left);
380
            } else {
381
                /* should be < 16 bits otherwise a codeword could have
382
                   been parsed */
383 7be166e4 Fabrice Bellard
                mb->partial_bit_count = bits_left;
384
                mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
385 5d2231ee Fabrice Bellard
            }
386
            block += 64;
387 7be166e4 Fabrice Bellard
            mb++;
388 5d2231ee Fabrice Bellard
        }
389
        
390
        flush_put_bits(&pb);
391
392
        /* pass 2 : we can do it just after */
393
#ifdef VLC_DEBUG
394 7be166e4 Fabrice Bellard
        printf("***pass 2 size=%d\n", mb_bit_count);
395 5d2231ee Fabrice Bellard
#endif
396
        block = block1;
397 7be166e4 Fabrice Bellard
        mb = mb1;
398 68f593b4 Michael Niedermayer
        init_get_bits(&s->gb, mb_bit_buffer, 80*8);
399 5d2231ee Fabrice Bellard
        for(j = 0;j < 6; j++) {
400 924311cd Michael Niedermayer
            if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
401 7be166e4 Fabrice Bellard
                dv_decode_ac(s, mb, block, mb_bit_count);
402 5d2231ee Fabrice Bellard
                /* if still not finished, no need to parse other blocks */
403 7be166e4 Fabrice Bellard
                if (!mb->eob_reached) {
404 5d2231ee Fabrice Bellard
                    /* we could not parse the current AC coefficient,
405
                       so we add the remaining bytes */
406 924311cd Michael Niedermayer
                    bits_left = mb_bit_count - get_bits_count(&s->gb);
407 5d2231ee Fabrice Bellard
                    if (bits_left > 0) {
408 7be166e4 Fabrice Bellard
                        mb->partial_bit_count += bits_left;
409
                        mb->partial_bit_buffer = 
410
                            (mb->partial_bit_buffer << bits_left) | 
411 5d2231ee Fabrice Bellard
                            get_bits(&s->gb, bits_left);
412
                    }
413
                    goto next_mb;
414
                }
415
            }
416
            block += 64;
417 7be166e4 Fabrice Bellard
            mb++;
418 5d2231ee Fabrice Bellard
        }
419
        /* all blocks are finished, so the extra bytes can be used at
420
           the video segment level */
421 924311cd Michael Niedermayer
        bits_left = mb_bit_count - get_bits_count(&s->gb);
422 5d2231ee Fabrice Bellard
        vs_bit_count += bits_left;
423
        bit_copy(&vs_pb, &s->gb, bits_left);
424
    next_mb:
425 7be166e4 Fabrice Bellard
        mb1 += 6;
426 5d2231ee Fabrice Bellard
        block1 += 6 * 64;
427
    }
428
429
    /* we need a pass other the whole video segment */
430
    flush_put_bits(&vs_pb);
431
        
432
#ifdef VLC_DEBUG
433
    printf("***pass 3 size=%d\n", vs_bit_count);
434
#endif
435
    block = &s->block[0][0];
436
    mb = mb_data;
437 68f593b4 Michael Niedermayer
    init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
438 5d2231ee Fabrice Bellard
    for(mb_index = 0; mb_index < 5; mb_index++) {
439
        for(j = 0;j < 6; j++) {
440 7be166e4 Fabrice Bellard
            if (!mb->eob_reached) {
441 5d2231ee Fabrice Bellard
#ifdef VLC_DEBUG
442
                printf("start %d:%d\n", mb_index, j);
443
#endif
444 7be166e4 Fabrice Bellard
                dv_decode_ac(s, mb, block, vs_bit_count);
445 5d2231ee Fabrice Bellard
            }
446
            block += 64;
447 7be166e4 Fabrice Bellard
            mb++;
448 5d2231ee Fabrice Bellard
        }
449
    }
450
    
451
    /* compute idct and place blocks */
452
    block = &s->block[0][0];
453
    mb = mb_data;
454
    for(mb_index = 0; mb_index < 5; mb_index++) {
455
        v = *mb_pos_ptr++;
456
        mb_x = v & 0xff;
457
        mb_y = v >> 8;
458 7458ccbb Roman Shaposhnik
        y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
459
        if (s->sys->pix_fmt == PIX_FMT_YUV411P)
460
            c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
461 5d2231ee Fabrice Bellard
        else
462 7458ccbb Roman Shaposhnik
            c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
463 5d2231ee Fabrice Bellard
        for(j = 0;j < 6; j++) {
464 7be166e4 Fabrice Bellard
            idct_put = s->idct_put[mb->dct_mode];
465 5d2231ee Fabrice Bellard
            if (j < 4) {
466 7458ccbb Roman Shaposhnik
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
467 1c05e11d Fabrice Bellard
                    /* NOTE: at end of line, the macroblock is handled as 420 */
468 7458ccbb Roman Shaposhnik
                    idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
469 5d2231ee Fabrice Bellard
                } else {
470 7458ccbb Roman Shaposhnik
                    idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
471
                             s->picture.linesize[0], block);
472 5d2231ee Fabrice Bellard
                }
473
            } else {
474 7458ccbb Roman Shaposhnik
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
475 1c05e11d Fabrice Bellard
                    uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
476
                    int y, linesize;
477
                    /* NOTE: at end of line, the macroblock is handled as 420 */
478
                    idct_put(pixels, 8, block);
479 7458ccbb Roman Shaposhnik
                    linesize = s->picture.linesize[6 - j];
480
                    c_ptr = s->picture.data[6 - j] + c_offset;
481 1c05e11d Fabrice Bellard
                    ptr = pixels;
482
                    for(y = 0;y < 8; y++) {
483
                        /* convert to 411P */
484 62cf114b Michael Niedermayer
                        c_ptr1 = c_ptr + 8*linesize;
485
                        c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
486
                        c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
487
                        c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
488
                        c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
489
                        c_ptr += linesize;
490 1c05e11d Fabrice Bellard
                        ptr += 8;
491
                    }
492
                } else {
493
                    /* don't ask me why they inverted Cb and Cr ! */
494 7458ccbb Roman Shaposhnik
                    idct_put(s->picture.data[6 - j] + c_offset, 
495
                             s->picture.linesize[6 - j], block);
496 1c05e11d Fabrice Bellard
                }
497 5d2231ee Fabrice Bellard
            }
498
            block += 64;
499 7be166e4 Fabrice Bellard
            mb++;
500 5d2231ee Fabrice Bellard
        }
501
    }
502
}
503
504
/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
505
   144000 bytes for PAL) */
506
static int dvvideo_decode_frame(AVCodecContext *avctx, 
507
                                 void *data, int *data_size,
508 0c1a9eda Zdenek Kabelac
                                 uint8_t *buf, int buf_size)
509 5d2231ee Fabrice Bellard
{
510
    DVVideoDecodeContext *s = avctx->priv_data;
511 7458ccbb Roman Shaposhnik
    int ds, vs;
512 0c1a9eda Zdenek Kabelac
    const uint16_t *mb_pos_ptr;
513 5d2231ee Fabrice Bellard
    
514 7458ccbb Roman Shaposhnik
    s->sys = dv_frame_profile(buf);
515
    if (!s->sys || buf_size < s->sys->frame_size)
516
        return -1; /* NOTE: we only accept several full frames */
517 e9feea59 Arpi
518 7458ccbb Roman Shaposhnik
        
519 e20c4069 Michael Niedermayer
    if(s->picture.data[0])
520
        avctx->release_buffer(avctx, &s->picture);
521
    
522 7458ccbb Roman Shaposhnik
    s->picture.reference = 0;
523
    avctx->pix_fmt = s->sys->pix_fmt;
524 1e491e29 Michael Niedermayer
    if(avctx->get_buffer(avctx, &s->picture) < 0) {
525
        fprintf(stderr, "get_buffer() failed\n");
526
        return -1;
527 b0397e0e Zdenek Kabelac
    }
528
529 5d2231ee Fabrice Bellard
    /* for each DIF segment */
530 7458ccbb Roman Shaposhnik
    mb_pos_ptr = s->sys->video_place;
531
    for (ds = 0; ds < s->sys->difseg_size; ds++) {
532
        buf += 6 * 80; /* skip DIF segment header */
533 5d2231ee Fabrice Bellard
        
534
        for(vs = 0; vs < 27; vs++) {
535 7458ccbb Roman Shaposhnik
            if ((vs % 3) == 0)
536
                buf += 80; /* skip audio block */
537
            
538
            dv_decode_video_segment(s, buf, mb_pos_ptr);
539
            buf += 5 * 80;
540 5d2231ee Fabrice Bellard
            mb_pos_ptr += 5;
541
        }
542
    }
543
544 9e398782 Arpi
    emms_c();
545
546 5d2231ee Fabrice Bellard
    /* return image */
547 492cd3a9 Michael Niedermayer
    *data_size = sizeof(AVFrame);
548
    *(AVFrame*)data= s->picture;
549 1e491e29 Michael Niedermayer
    
550 7458ccbb Roman Shaposhnik
    return s->sys->frame_size;
551 5d2231ee Fabrice Bellard
}
552
553
static int dvvideo_decode_end(AVCodecContext *avctx)
554
{
555 d90cf87b Michael Niedermayer
    avcodec_default_free_buffers(avctx);    
556 5d2231ee Fabrice Bellard
557
    return 0;
558
}
559
560
AVCodec dvvideo_decoder = {
561
    "dvvideo",
562
    CODEC_TYPE_VIDEO,
563
    CODEC_ID_DVVIDEO,
564
    sizeof(DVVideoDecodeContext),
565
    dvvideo_decode_init,
566
    NULL,
567
    dvvideo_decode_end,
568
    dvvideo_decode_frame,
569 b0397e0e Zdenek Kabelac
    CODEC_CAP_DR1,
570 5d2231ee Fabrice Bellard
    NULL
571
};