Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mlp_parser.c @ ce15710f

History | View | Annotate | Download (7.83 KB)

1
/*
2
 * MLP parser
3
 * Copyright (c) 2007 Ian Caulfield
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file mlp_parser.c
24
 * MLP parser
25
 */
26

    
27
#include <stdint.h>
28

    
29
#include "libavutil/crc.h"
30
#include "bitstream.h"
31
#include "parser.h"
32
#include "mlp_parser.h"
33
#include "mlp.h"
34

    
35
static const uint8_t mlp_quants[16] = {
36
    16, 20, 24, 0, 0, 0, 0, 0,
37
     0,  0,  0, 0, 0, 0, 0, 0,
38
};
39

    
40
static const uint8_t mlp_channels[32] = {
41
    1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
42
    5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43
};
44

    
45
static const uint8_t thd_chancount[13] = {
46
//  LR    C   LFE  LRs LRvh  LRc LRrs  Cs   Ts  LRsd  LRw  Cvh  LFE2
47
     2,   1,   1,   2,   2,   2,   2,   1,   1,   2,   2,   1,   1
48
};
49

    
50
static int mlp_samplerate(int in)
51
{
52
    if (in == 0xF)
53
        return 0;
54

    
55
    return (in & 8 ? 44100 : 48000) << (in & 7) ;
56
}
57

    
58
static int truehd_channels(int chanmap)
59
{
60
    int channels = 0, i;
61

    
62
    for (i = 0; i < 13; i++)
63
        channels += thd_chancount[i] * ((chanmap >> i) & 1);
64

    
65
    return channels;
66
}
67

    
68
/** Read a major sync info header - contains high level information about
69
 *  the stream - sample rate, channel arrangement etc. Most of this
70
 *  information is not actually necessary for decoding, only for playback.
71
 *  gb must be a freshly initialized GetBitContext with no bits read.
72
 */
73

    
74
int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
75
{
76
    int ratebits;
77
    uint16_t checksum;
78

    
79
    assert(get_bits_count(gb) == 0);
80

    
81
    if (gb->size_in_bits < 28 << 3) {
82
        av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
83
        return -1;
84
    }
85

    
86
    checksum = ff_mlp_checksum16(gb->buffer, 26);
87
    if (checksum != AV_RL16(gb->buffer+26)) {
88
        av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
89
        return -1;
90
    }
91

    
92
    if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
93
        return -1;
94

    
95
    mh->stream_type = get_bits(gb, 8);
96

    
97
    if (mh->stream_type == 0xbb) {
98
        mh->group1_bits = mlp_quants[get_bits(gb, 4)];
99
        mh->group2_bits = mlp_quants[get_bits(gb, 4)];
100

    
101
        ratebits = get_bits(gb, 4);
102
        mh->group1_samplerate = mlp_samplerate(ratebits);
103
        mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
104

    
105
        skip_bits(gb, 11);
106

    
107
        mh->channels_mlp = get_bits(gb, 5);
108
    } else if (mh->stream_type == 0xba) {
109
        mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
110
        mh->group2_bits = 0;
111

    
112
        ratebits = get_bits(gb, 4);
113
        mh->group1_samplerate = mlp_samplerate(ratebits);
114
        mh->group2_samplerate = 0;
115

    
116
        skip_bits(gb, 8);
117

    
118
        mh->channels_thd_stream1 = get_bits(gb, 5);
119

    
120
        skip_bits(gb, 2);
121

    
122
        mh->channels_thd_stream2 = get_bits(gb, 13);
123
    } else
124
        return -1;
125

    
126
    mh->access_unit_size = 40 << (ratebits & 7);
127
    mh->access_unit_size_pow2 = 64 << (ratebits & 7);
128

    
129
    skip_bits_long(gb, 48);
130

    
131
    mh->is_vbr = get_bits1(gb);
132

    
133
    mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
134

    
135
    mh->num_substreams = get_bits(gb, 4);
136

    
137
    skip_bits_long(gb, 4 + 11 * 8);
138

    
139
    return 0;
140
}
141

    
142
typedef struct MLPParseContext
143
{
144
    ParseContext pc;
145

    
146
    int bytes_left;
147

    
148
    int in_sync;
149

    
150
    int num_substreams;
151
} MLPParseContext;
152

    
153
static int mlp_parse(AVCodecParserContext *s,
154
                     AVCodecContext *avctx,
155
                     const uint8_t **poutbuf, int *poutbuf_size,
156
                     const uint8_t *buf, int buf_size)
157
{
158
    MLPParseContext *mp = s->priv_data;
159
    int sync_present;
160
    uint8_t parity_bits;
161
    int next;
162
    int i, p = 0;
163

    
164
    *poutbuf_size = 0;
165
    if (buf_size == 0)
166
        return 0;
167

    
168
    if (!mp->in_sync) {
169
        // Not in sync - find a major sync header
170

    
171
        for (i = 0; i < buf_size; i++) {
172
            mp->pc.state = (mp->pc.state << 8) | buf[i];
173
            if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) {
174
                mp->in_sync = 1;
175
                mp->bytes_left = 0;
176
                break;
177
            }
178
        }
179

    
180
        if (!mp->in_sync) {
181
            ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
182
            return buf_size;
183
        }
184

    
185
        ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
186

    
187
        return i - 7;
188
    }
189

    
190
    if (mp->bytes_left == 0) {
191
        // Find length of this packet
192

    
193
        /* Copy overread bytes from last frame into buffer. */
194
        for(; mp->pc.overread>0; mp->pc.overread--) {
195
            mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
196
        }
197

    
198
        if (mp->pc.index + buf_size < 2) {
199
            ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
200
            return buf_size;
201
        }
202

    
203
        mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
204
                       |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
205
        mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
206
        mp->bytes_left -= mp->pc.index;
207
    }
208

    
209
    next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
210

    
211
    if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
212
        mp->bytes_left -= buf_size;
213
        return buf_size;
214
    }
215

    
216
    mp->bytes_left = 0;
217

    
218
    sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
219

    
220
    if (!sync_present) {
221
        /* The first nibble of a frame is a parity check of the 4-byte
222
         * access unit header and all the 2- or 4-byte substream headers. */
223
        // Only check when this isn't a sync frame - syncs have a checksum.
224

    
225
        parity_bits = 0;
226
        for (i = -1; i < mp->num_substreams; i++) {
227
            parity_bits ^= buf[p++];
228
            parity_bits ^= buf[p++];
229

    
230
            if (i < 0 || buf[p-2] & 0x80) {
231
                parity_bits ^= buf[p++];
232
                parity_bits ^= buf[p++];
233
            }
234
        }
235

    
236
        if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
237
            av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
238
            goto lost_sync;
239
        }
240
    } else {
241
        GetBitContext gb;
242
        MLPHeaderInfo mh;
243

    
244
        init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
245
        if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
246
            goto lost_sync;
247

    
248
#ifdef CONFIG_AUDIO_NONSHORT
249
        avctx->bits_per_sample = mh.group1_bits;
250
        if (avctx->bits_per_sample > 16)
251
            avctx->sample_fmt = SAMPLE_FMT_S32;
252
#endif
253
        avctx->sample_rate = mh.group1_samplerate;
254
        avctx->frame_size = mh.access_unit_size;
255

    
256
        if (mh.stream_type == 0xbb) {
257
            /* MLP stream */
258
            avctx->channels = mlp_channels[mh.channels_mlp];
259
        } else { /* mh.stream_type == 0xba */
260
            /* TrueHD stream */
261
            if (mh.channels_thd_stream2)
262
                avctx->channels = truehd_channels(mh.channels_thd_stream2);
263
            else
264
                avctx->channels = truehd_channels(mh.channels_thd_stream1);
265
        }
266

    
267
        if (!mh.is_vbr) /* Stream is CBR */
268
            avctx->bit_rate = mh.peak_bitrate;
269

    
270
        mp->num_substreams = mh.num_substreams;
271
    }
272

    
273
    *poutbuf = buf;
274
    *poutbuf_size = buf_size;
275

    
276
    return next;
277

    
278
lost_sync:
279
    mp->in_sync = 0;
280
    return 1;
281
}
282

    
283
AVCodecParser mlp_parser = {
284
    { CODEC_ID_MLP },
285
    sizeof(MLPParseContext),
286
    ff_mlp_init_crc2D,
287
    mlp_parse,
288
    NULL,
289
};