Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dv.c @ 7458ccbb

History | View | Annotate | Download (17.9 KB)

1
/*
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

    
20
/**
21
 * @file dv.c
22
 * DV decoder.
23
 */
24
#include "avcodec.h"
25
#include "dsputil.h"
26
#include "mpegvideo.h"
27
#include "simple_idct.h"
28
#include "dvdata.h"
29

    
30
typedef struct DVVideoDecodeContext {
31
    const DVprofile* sys;
32
    GetBitContext gb;
33
    AVFrame picture;
34
    DCTELEM block[5*6][64] __align8;
35
    
36
    /* FIXME: the following is extracted from DSP */
37
    uint8_t dv_zigzag[2][64];
38
    uint8_t idct_permutation[64];
39
    void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
40
    void (*fdct)(DCTELEM *block);
41
    
42
    /* XXX: move it to static storage ? */
43
    uint8_t dv_shift[2][22][64];
44
    void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
45
} DVVideoDecodeContext;
46

    
47
#define TEX_VLC_BITS 9
48
/* XXX: also include quantization */
49
static RL_VLC_ELEM *dv_rl_vlc[1];
50
static VLC_TYPE dv_vlc_codes[15][23];
51

    
52
static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
53
{
54
    int i, q, j;
55

    
56
    /* NOTE: max left shift is 6 */
57
    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
        }
72
    }
73
}
74

    
75
static int dvvideo_decode_init(AVCodecContext *avctx)
76
{
77
    DVVideoDecodeContext *s = avctx->priv_data;
78
    MpegEncContext s2;
79
    static int done=0;
80

    
81
    if (!done) {
82
        int i;
83
        VLC dv_vlc;
84

    
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

    
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
    }
120

    
121
    /* ugly way to get the idct & scantable */
122
    /* XXX: fix it */
123
    memset(&s2, 0, sizeof(MpegEncContext));
124
    s2.avctx = avctx;
125
    dsputil_init(&s2.dsp, avctx);
126
    if (DCT_common_init(&s2) < 0)
127
       return -1;
128

    
129
    s->get_pixels = s2.dsp.get_pixels;
130
    s->fdct = s2.dsp.fdct;
131
    
132
    s->idct_put[0] = s2.dsp.idct_put;
133
    memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
134
    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
    /* XXX: do it only for constant case */
141
    dv_build_unquantize_tables(s);
142

    
143
    return 0;
144
}
145

    
146
// #define VLC_DEBUG
147

    
148
typedef struct BlockInfo {
149
    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
    int shift_offset;
157
} BlockInfo;
158

    
159
/* block size in bits */
160
static const uint16_t block_sizes[6] = {
161
    112, 112, 112, 112, 80, 80
162
};
163

    
164
#ifndef ALT_BITSTREAM_READER
165
#warning only works with ALT_BITSTREAM_READER
166
#endif
167

    
168
/* decode ac coefs */
169
static void dv_decode_ac(DVVideoDecodeContext *s, 
170
                         BlockInfo *mb, DCTELEM *block, int last_index)
171
{
172
    int last_re_index;
173
    int shift_offset = mb->shift_offset;
174
    const uint8_t *scan_table = mb->scan_table;
175
    const uint8_t *shift_table = mb->shift_table;
176
    int pos = mb->pos;
177
    int level, pos1, sign, run;
178
    int partial_bit_count;
179
#ifndef ALT_BITSTREAM_READER //FIXME
180
    int re_index=0; 
181
    int re1_index=0;
182
#endif
183
    OPEN_READER(re, &s->gb);
184
    
185
#ifdef VLC_DEBUG
186
    printf("start\n");
187
#endif
188

    
189
    /* if we must parse a partial vlc, we do it here */
190
    partial_bit_count = mb->partial_bit_count;
191
    if (partial_bit_count > 0) {
192
        uint8_t buf[4];
193
        uint32_t v;
194
        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
        v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
204
        buf[0] = v >> 8;
205
        buf[1] = v;
206
#ifdef VLC_DEBUG
207
        printf("v=%04x cnt=%d %04x\n", 
208
               v, partial_bit_count, (mb->partial_bit_buffer << l));
209
#endif
210
        /* try to read the codeword */
211
        init_get_bits(&gb1, buf, 4*8);
212
        {
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
        /* by definition, if we can read the vlc, all partial bits
233
           will be read (otherwise we could have read the vlc before) */
234
        mb->partial_bit_count = 0;
235
        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
                mb->eob_reached = 0;
259
                break;
260
            }
261
            /* EOB */
262
            mb->eob_reached = 1;
263
            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
            level = level << (shift_table[pos1] + shift_offset);
277
            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
                fprintf(stderr, "error pos=%d\n", pos);
290
#endif
291
                /* for errors, we consider the eob is reached */
292
                mb->eob_reached = 1;
293
                break;
294
            }
295
        }
296
    }
297
    CLOSE_READER(re, &s->gb);
298
    mb->pos = pos;
299
}
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
                                           uint8_t *buf_ptr1, 
315
                                           const uint16_t *mb_pos_ptr)
316
{
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
    uint8_t *y_ptr;
322
    BlockInfo mb_data[5 * 6], *mb, *mb1;
323
    void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
324
    uint8_t *buf_ptr;
325
    PutBitContext pb, vs_pb;
326
    uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
327
    int mb_bit_count;
328
    uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
329
    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
    mb1 = mb_data;
337
    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
        init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
344
        mb_bit_count = 0;
345
        mb = mb1;
346
        block = block1;
347
        for(j = 0;j < 6; j++) {
348
            /* NOTE: size is not important here */
349
            init_get_bits(&s->gb, buf_ptr, 14*8);
350
            
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
            mb->dct_mode = dct_mode;
356
            mb->scan_table = s->dv_zigzag[dct_mode];
357
            class1 = get_bits(&s->gb, 2);
358
            mb->shift_offset = (class1 == 3);
359
            mb->shift_table = s->dv_shift[dct_mode]
360
                [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
            mb->pos = 0;
369
            mb->partial_bit_count = 0;
370

    
371
            dv_decode_ac(s, mb, block, last_index);
372

    
373
            /* write the remaining bits  in a new buffer only if the
374
               block is finished */
375
            bits_left = last_index - get_bits_count(&s->gb);
376
            if (mb->eob_reached) {
377
                mb->partial_bit_count = 0;
378
                mb_bit_count += bits_left;
379
                bit_copy(&pb, &s->gb, bits_left);
380
            } else {
381
                /* should be < 16 bits otherwise a codeword could have
382
                   been parsed */
383
                mb->partial_bit_count = bits_left;
384
                mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
385
            }
386
            block += 64;
387
            mb++;
388
        }
389
        
390
        flush_put_bits(&pb);
391

    
392
        /* pass 2 : we can do it just after */
393
#ifdef VLC_DEBUG
394
        printf("***pass 2 size=%d\n", mb_bit_count);
395
#endif
396
        block = block1;
397
        mb = mb1;
398
        init_get_bits(&s->gb, mb_bit_buffer, 80*8);
399
        for(j = 0;j < 6; j++) {
400
            if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
401
                dv_decode_ac(s, mb, block, mb_bit_count);
402
                /* if still not finished, no need to parse other blocks */
403
                if (!mb->eob_reached) {
404
                    /* we could not parse the current AC coefficient,
405
                       so we add the remaining bytes */
406
                    bits_left = mb_bit_count - get_bits_count(&s->gb);
407
                    if (bits_left > 0) {
408
                        mb->partial_bit_count += bits_left;
409
                        mb->partial_bit_buffer = 
410
                            (mb->partial_bit_buffer << bits_left) | 
411
                            get_bits(&s->gb, bits_left);
412
                    }
413
                    goto next_mb;
414
                }
415
            }
416
            block += 64;
417
            mb++;
418
        }
419
        /* all blocks are finished, so the extra bytes can be used at
420
           the video segment level */
421
        bits_left = mb_bit_count - get_bits_count(&s->gb);
422
        vs_bit_count += bits_left;
423
        bit_copy(&vs_pb, &s->gb, bits_left);
424
    next_mb:
425
        mb1 += 6;
426
        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
    init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
438
    for(mb_index = 0; mb_index < 5; mb_index++) {
439
        for(j = 0;j < 6; j++) {
440
            if (!mb->eob_reached) {
441
#ifdef VLC_DEBUG
442
                printf("start %d:%d\n", mb_index, j);
443
#endif
444
                dv_decode_ac(s, mb, block, vs_bit_count);
445
            }
446
            block += 64;
447
            mb++;
448
        }
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
        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
        else
462
            c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
463
        for(j = 0;j < 6; j++) {
464
            idct_put = s->idct_put[mb->dct_mode];
465
            if (j < 4) {
466
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
467
                    /* NOTE: at end of line, the macroblock is handled as 420 */
468
                    idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
469
                } else {
470
                    idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
471
                             s->picture.linesize[0], block);
472
                }
473
            } else {
474
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
475
                    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
                    linesize = s->picture.linesize[6 - j];
480
                    c_ptr = s->picture.data[6 - j] + c_offset;
481
                    ptr = pixels;
482
                    for(y = 0;y < 8; y++) {
483
                        /* convert to 411P */
484
                        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
                        ptr += 8;
491
                    }
492
                } else {
493
                    /* don't ask me why they inverted Cb and Cr ! */
494
                    idct_put(s->picture.data[6 - j] + c_offset, 
495
                             s->picture.linesize[6 - j], block);
496
                }
497
            }
498
            block += 64;
499
            mb++;
500
        }
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
                                 uint8_t *buf, int buf_size)
509
{
510
    DVVideoDecodeContext *s = avctx->priv_data;
511
    int ds, vs;
512
    const uint16_t *mb_pos_ptr;
513
    
514
    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

    
518
        
519
    if(s->picture.data[0])
520
        avctx->release_buffer(avctx, &s->picture);
521
    
522
    s->picture.reference = 0;
523
    avctx->pix_fmt = s->sys->pix_fmt;
524
    if(avctx->get_buffer(avctx, &s->picture) < 0) {
525
        fprintf(stderr, "get_buffer() failed\n");
526
        return -1;
527
    }
528

    
529
    /* for each DIF segment */
530
    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
        
534
        for(vs = 0; vs < 27; vs++) {
535
            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
            mb_pos_ptr += 5;
541
        }
542
    }
543

    
544
    emms_c();
545

    
546
    /* return image */
547
    *data_size = sizeof(AVFrame);
548
    *(AVFrame*)data= s->picture;
549
    
550
    return s->sys->frame_size;
551
}
552

    
553
static int dvvideo_decode_end(AVCodecContext *avctx)
554
{
555
    avcodec_default_free_buffers(avctx);    
556

    
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
    CODEC_CAP_DR1,
570
    NULL
571
};