Revision 55736cfb

View differences:

libavcodec/ac3.h
95 95
    uint32_t bit_rate;
96 96
    uint8_t channels;
97 97
    uint16_t frame_size;
98
    int center_mix_level;                   ///< Center mix level index
99
    int surround_mix_level;                 ///< Surround mix level index
98 100
    /** @} */
99 101
} AC3HeaderInfo;
100 102

  
libavcodec/ac3_parser.c
33 33
    1, 2, 3, 6
34 34
};
35 35

  
36
/**
37
 * Table for center mix levels
38
 * reference: Section 5.4.2.4 cmixlev
39
 */
40
static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
41

  
42
/**
43
 * Table for surround mix levels
44
 * reference: Section 5.4.2.5 surmixlev
45
 */
46
static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
36 47

  
37
int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
48

  
49
int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
38 50
{
39
    GetBitContext gbc;
40 51
    int frame_size_code;
41 52
    int num_blocks;
42 53

  
43 54
    memset(hdr, 0, sizeof(*hdr));
44 55

  
45
    init_get_bits(&gbc, buf, 54);
46

  
47
    hdr->sync_word = get_bits(&gbc, 16);
56
    hdr->sync_word = get_bits(gbc, 16);
48 57
    if(hdr->sync_word != 0x0B77)
49 58
        return AC3_PARSE_ERROR_SYNC;
50 59

  
51 60
    /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
52
    hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F;
61
    hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
53 62
    if(hdr->bitstream_id > 16)
54 63
        return AC3_PARSE_ERROR_BSID;
55 64

  
56 65
    if(hdr->bitstream_id <= 10) {
57 66
        /* Normal AC-3 */
58
        hdr->crc1 = get_bits(&gbc, 16);
59
        hdr->sr_code = get_bits(&gbc, 2);
67
        hdr->crc1 = get_bits(gbc, 16);
68
        hdr->sr_code = get_bits(gbc, 2);
60 69
        if(hdr->sr_code == 3)
61 70
            return AC3_PARSE_ERROR_SAMPLE_RATE;
62 71

  
63
        frame_size_code = get_bits(&gbc, 6);
72
        frame_size_code = get_bits(gbc, 6);
64 73
        if(frame_size_code > 37)
65 74
            return AC3_PARSE_ERROR_FRAME_SIZE;
66 75

  
67
        skip_bits(&gbc, 5); // skip bsid, already got it
76
        skip_bits(gbc, 5); // skip bsid, already got it
77

  
78
        skip_bits(gbc, 3); // skip bitstream mode
79
        hdr->channel_mode = get_bits(gbc, 3);
80

  
81
        /* set default mix levels */
82
        hdr->center_mix_level   = 3;  // -4.5dB
83
        hdr->surround_mix_level = 4;  // -6.0dB
68 84

  
69
        skip_bits(&gbc, 3); // skip bitstream mode
70
        hdr->channel_mode = get_bits(&gbc, 3);
71
        if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) {
72
            skip_bits(&gbc, 2); // skip center mix level
73
        }
74
        if(hdr->channel_mode & 4) {
75
            skip_bits(&gbc, 2); // skip surround mix level
76
        }
77 85
        if(hdr->channel_mode == AC3_CHMODE_STEREO) {
78
            skip_bits(&gbc, 2); // skip dolby surround mode
86
            skip_bits(gbc, 2); // skip dsurmod
87
        } else {
88
            if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
89
                hdr->center_mix_level = center_levels[get_bits(gbc, 2)];
90
            if(hdr->channel_mode & 4)
91
                hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
79 92
        }
80
        hdr->lfe_on = get_bits1(&gbc);
93
        hdr->lfe_on = get_bits1(gbc);
81 94

  
82 95
        hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
83 96
        hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
......
88 101
    } else {
89 102
        /* Enhanced AC-3 */
90 103
        hdr->crc1 = 0;
91
        hdr->frame_type = get_bits(&gbc, 2);
104
        hdr->frame_type = get_bits(gbc, 2);
92 105
        if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
93 106
            return AC3_PARSE_ERROR_FRAME_TYPE;
94 107

  
95
        skip_bits(&gbc, 3); // skip substream id
108
        skip_bits(gbc, 3); // skip substream id
96 109

  
97
        hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1;
110
        hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
98 111
        if(hdr->frame_size < AC3_HEADER_SIZE)
99 112
            return AC3_PARSE_ERROR_FRAME_SIZE;
100 113

  
101
        hdr->sr_code = get_bits(&gbc, 2);
114
        hdr->sr_code = get_bits(gbc, 2);
102 115
        if (hdr->sr_code == 3) {
103
            int sr_code2 = get_bits(&gbc, 2);
116
            int sr_code2 = get_bits(gbc, 2);
104 117
            if(sr_code2 == 3)
105 118
                return AC3_PARSE_ERROR_SAMPLE_RATE;
106 119
            hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
107 120
            hdr->sr_shift = 1;
108 121
            num_blocks = 6;
109 122
        } else {
110
            num_blocks = eac3_blocks[get_bits(&gbc, 2)];
123
            num_blocks = eac3_blocks[get_bits(gbc, 2)];
111 124
            hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
112 125
            hdr->sr_shift = 0;
113 126
        }
114 127

  
115
        hdr->channel_mode = get_bits(&gbc, 3);
116
        hdr->lfe_on = get_bits1(&gbc);
128
        hdr->channel_mode = get_bits(gbc, 3);
129
        hdr->lfe_on = get_bits1(gbc);
117 130

  
118 131
        hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
119 132
                        (num_blocks * 256.0));
......
129 142
    int err;
130 143
    uint64_t tmp = be2me_64(state);
131 144
    AC3HeaderInfo hdr;
145
    GetBitContext gbc;
132 146

  
133
    err = ff_ac3_parse_header(((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, &hdr);
147
    init_get_bits(&gbc, ((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, 54);
148
    err = ff_ac3_parse_header(&gbc, &hdr);
134 149

  
135 150
    if(err < 0)
136 151
        return 0;
libavcodec/ac3_parser.h
24 24
#define FFMPEG_AC3_PARSER_H
25 25

  
26 26
#include "ac3.h"
27
#include "bitstream.h"
27 28

  
28 29
typedef enum {
29 30
    AC3_PARSE_ERROR_SYNC        = -1,
......
37 38
 * Parses AC-3 frame header.
38 39
 * Parses the header up to the lfeon element, which is the first 52 or 54 bits
39 40
 * depending on the audio coding mode.
40
 * @param buf[in] Array containing the first 7 bytes of the frame.
41
 * @param gbc[in] BitContext containing the first 54 bits of the frame.
41 42
 * @param hdr[out] Pointer to struct where header info is written.
42 43
 * @return Returns 0 on success, -1 if there is a sync word mismatch,
43 44
 * -2 if the bsid (version) element is invalid, -3 if the fscod (sample rate)
44 45
 * element is invalid, or -4 if the frmsizecod (bit rate) element is invalid.
45 46
 */
46
int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr);
47
int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr);
47 48

  
48 49
#endif /* FFMPEG_AC3_PARSER_H */
libavcodec/ac3dec.c
89 89
};
90 90

  
91 91
/**
92
 * Table for center mix levels
93
 * reference: Section 5.4.2.4 cmixlev
94
 */
95
static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
96

  
97
/**
98
 * Table for surround mix levels
99
 * reference: Section 5.4.2.5 surmixlev
100
 */
101
static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
102

  
103
/**
104 92
 * Table for default stereo downmixing coefficients
105 93
 * reference: Section 7.8.2 Downmixing Into Two Channels
106 94
 */
......
315 303
    GetBitContext *gbc = &s->gbc;
316 304
    int err, i;
317 305

  
318
    err = ff_ac3_parse_header(gbc->buffer, &hdr);
306
    err = ff_ac3_parse_header(gbc, &hdr);
319 307
    if(err)
320 308
        return err;
321 309

  
......
333 321
    s->fbw_channels                 = s->channels - s->lfe_on;
334 322
    s->lfe_ch                       = s->fbw_channels + 1;
335 323
    s->frame_size                   = hdr.frame_size;
324
    s->center_mix_level             = hdr.center_mix_level;
325
    s->surround_mix_level           = hdr.surround_mix_level;
336 326

  
337 327
    /* set default output to all source channels */
338 328
    s->out_channels = s->channels;
......
340 330
    if(s->lfe_on)
341 331
        s->output_mode |= AC3_OUTPUT_LFEON;
342 332

  
343
    /* set default mix levels */
344
    s->center_mix_level   = 3;  // -4.5dB
345
    s->surround_mix_level = 4;  // -6.0dB
346

  
347
    /* skip over portion of header which has already been read */
348
    skip_bits(gbc, 16); // skip the sync_word
349
    skip_bits(gbc, 16); // skip crc1
350
    skip_bits(gbc, 8);  // skip fscod and frmsizecod
351
    skip_bits(gbc, 11); // skip bsid, bsmod, and acmod
352
    if(s->channel_mode == AC3_CHMODE_STEREO) {
353
        skip_bits(gbc, 2); // skip dsurmod
354
    } else {
355
        if((s->channel_mode & 1) && s->channel_mode != AC3_CHMODE_MONO)
356
            s->center_mix_level = center_levels[get_bits(gbc, 2)];
357
        if(s->channel_mode & 4)
358
            s->surround_mix_level = surround_levels[get_bits(gbc, 2)];
359
    }
360
    skip_bits1(gbc); // skip lfeon
361

  
362 333
    /* read the rest of the bsi. read twice for dual mono mode. */
363 334
    i = !(s->channel_mode);
364 335
    do {
libavformat/raw.c
23 23
#include "ac3_parser.h"
24 24
#include "raw.h"
25 25
#include "crc.h"
26
#include "bitstream.h"
26 27

  
27 28
#ifdef CONFIG_MUXERS
28 29
/* simple formats */
......
419 420
    int max_frames, first_frames = 0, frames;
420 421
    uint8_t *buf, *buf2, *end;
421 422
    AC3HeaderInfo hdr;
423
    GetBitContext gbc;
422 424

  
423 425
    max_frames = 0;
424 426
    buf = p->buf;
......
428 430
        buf2 = buf;
429 431

  
430 432
        for(frames = 0; buf2 < end; frames++) {
431
            if(ff_ac3_parse_header(buf2, &hdr) < 0)
433
            init_get_bits(&gbc, buf2, 54);
434
            if(ff_ac3_parse_header(&gbc, &hdr) < 0)
432 435
                break;
433 436
            if(buf2 + hdr.frame_size > end ||
434 437
               av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))

Also available in: Unified diff