Revision 55736cfb libavcodec/ac3_parser.c

View differences:

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;

Also available in: Unified diff