]> git.sesse.net Git - ffmpeg/blob - libavcodec/mlp_parser.c
9ba3c53be1072f55250a0572a66f4877f6fe65c4
[ffmpeg] / libavcodec / mlp_parser.c
1 /*
2  * MLP parser
3  * Copyright (c) 2007 Ian Caulfield
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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
24  * MLP parser
25  */
26
27 #include <stdint.h>
28
29 #include "libavutil/crc.h"
30 #include "get_bits.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 uint64_t mlp_layout[32] = {
46     AV_CH_LAYOUT_MONO,
47     AV_CH_LAYOUT_STEREO,
48     AV_CH_LAYOUT_2_1,
49     AV_CH_LAYOUT_2_2,
50     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
51     AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
52     AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
53     AV_CH_LAYOUT_SURROUND,
54     AV_CH_LAYOUT_4POINT0,
55     AV_CH_LAYOUT_5POINT0,
56     AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
57     AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
58     AV_CH_LAYOUT_5POINT1,
59     AV_CH_LAYOUT_4POINT0,
60     AV_CH_LAYOUT_5POINT0,
61     AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
62     AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
63     AV_CH_LAYOUT_5POINT1,
64     AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
65     AV_CH_LAYOUT_5POINT0,
66     AV_CH_LAYOUT_5POINT1,
67     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
68 };
69
70 static const uint8_t thd_chancount[13] = {
71 //  LR    C   LFE  LRs LRvh  LRc LRrs  Cs   Ts  LRsd  LRw  Cvh  LFE2
72      2,   1,   1,   2,   2,   2,   2,   1,   1,   2,   2,   1,   1
73 };
74
75 static const uint64_t thd_layout[13] = {
76     AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT,                     // LR
77     AV_CH_FRONT_CENTER,                                     // C
78     AV_CH_LOW_FREQUENCY,                                    // LFE
79     AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,                       // LRs
80     AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT,             // LRvh
81     AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,                       // LRc
82     AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,                       // LRrs
83     AV_CH_BACK_CENTER,                                      // Cs
84     AV_CH_TOP_BACK_CENTER,                                  // Ts
85     AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,                       // LRsd
86     AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRw
87     AV_CH_TOP_BACK_CENTER,                                  // Cvh
88     AV_CH_LOW_FREQUENCY                                     // LFE2
89 };
90
91 static int mlp_samplerate(int in)
92 {
93     if (in == 0xF)
94         return 0;
95
96     return (in & 8 ? 44100 : 48000) << (in & 7) ;
97 }
98
99 static int truehd_channels(int chanmap)
100 {
101     int channels = 0, i;
102
103     for (i = 0; i < 13; i++)
104         channels += thd_chancount[i] * ((chanmap >> i) & 1);
105
106     return channels;
107 }
108
109 static int64_t truehd_layout(int chanmap)
110 {
111     int layout = 0, i;
112
113     for (i = 0; i < 13; i++)
114         layout |= thd_layout[i] * ((chanmap >> i) & 1);
115
116     return layout;
117 }
118
119 /** Read a major sync info header - contains high level information about
120  *  the stream - sample rate, channel arrangement etc. Most of this
121  *  information is not actually necessary for decoding, only for playback.
122  *  gb must be a freshly initialized GetBitContext with no bits read.
123  */
124
125 int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
126 {
127     int ratebits;
128     uint16_t checksum;
129
130     assert(get_bits_count(gb) == 0);
131
132     if (gb->size_in_bits < 28 << 3) {
133         av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
134         return -1;
135     }
136
137     checksum = ff_mlp_checksum16(gb->buffer, 26);
138     if (checksum != AV_RL16(gb->buffer+26)) {
139         av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
140         return -1;
141     }
142
143     if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
144         return -1;
145
146     mh->stream_type = get_bits(gb, 8);
147
148     if (mh->stream_type == 0xbb) {
149         mh->group1_bits = mlp_quants[get_bits(gb, 4)];
150         mh->group2_bits = mlp_quants[get_bits(gb, 4)];
151
152         ratebits = get_bits(gb, 4);
153         mh->group1_samplerate = mlp_samplerate(ratebits);
154         mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
155
156         skip_bits(gb, 11);
157
158         mh->channels_mlp = get_bits(gb, 5);
159     } else if (mh->stream_type == 0xba) {
160         mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
161         mh->group2_bits = 0;
162
163         ratebits = get_bits(gb, 4);
164         mh->group1_samplerate = mlp_samplerate(ratebits);
165         mh->group2_samplerate = 0;
166
167         skip_bits(gb, 8);
168
169         mh->channels_thd_stream1 = get_bits(gb, 5);
170
171         skip_bits(gb, 2);
172
173         mh->channels_thd_stream2 = get_bits(gb, 13);
174     } else
175         return -1;
176
177     mh->access_unit_size = 40 << (ratebits & 7);
178     mh->access_unit_size_pow2 = 64 << (ratebits & 7);
179
180     skip_bits_long(gb, 48);
181
182     mh->is_vbr = get_bits1(gb);
183
184     mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
185
186     mh->num_substreams = get_bits(gb, 4);
187
188     skip_bits_long(gb, 4 + 11 * 8);
189
190     return 0;
191 }
192
193 typedef struct MLPParseContext
194 {
195     ParseContext pc;
196
197     int bytes_left;
198
199     int in_sync;
200
201     int num_substreams;
202 } MLPParseContext;
203
204 static av_cold int mlp_init(AVCodecParserContext *s)
205 {
206     ff_mlp_init_crc();
207     return 0;
208 }
209
210 static int mlp_parse(AVCodecParserContext *s,
211                      AVCodecContext *avctx,
212                      const uint8_t **poutbuf, int *poutbuf_size,
213                      const uint8_t *buf, int buf_size)
214 {
215     MLPParseContext *mp = s->priv_data;
216     int sync_present;
217     uint8_t parity_bits;
218     int next;
219     int i, p = 0;
220
221     *poutbuf_size = 0;
222     if (buf_size == 0)
223         return 0;
224
225     if (!mp->in_sync) {
226         // Not in sync - find a major sync header
227
228         for (i = 0; i < buf_size; i++) {
229             mp->pc.state = (mp->pc.state << 8) | buf[i];
230             if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
231                 // ignore if we do not have the data for the start of header
232                 mp->pc.index + i >= 7) {
233                 mp->in_sync = 1;
234                 mp->bytes_left = 0;
235                 break;
236             }
237         }
238
239         if (!mp->in_sync) {
240             ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
241             return buf_size;
242         }
243
244         ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
245
246         return i - 7;
247     }
248
249     if (mp->bytes_left == 0) {
250         // Find length of this packet
251
252         /* Copy overread bytes from last frame into buffer. */
253         for(; mp->pc.overread>0; mp->pc.overread--) {
254             mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
255         }
256
257         if (mp->pc.index + buf_size < 2) {
258             ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
259             return buf_size;
260         }
261
262         mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
263                        |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
264         mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
265         mp->bytes_left -= mp->pc.index;
266     }
267
268     next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
269
270     if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
271         mp->bytes_left -= buf_size;
272         return buf_size;
273     }
274
275     mp->bytes_left = 0;
276
277     sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
278
279     if (!sync_present) {
280         /* The first nibble of a frame is a parity check of the 4-byte
281          * access unit header and all the 2- or 4-byte substream headers. */
282         // Only check when this isn't a sync frame - syncs have a checksum.
283
284         parity_bits = 0;
285         for (i = -1; i < mp->num_substreams; i++) {
286             parity_bits ^= buf[p++];
287             parity_bits ^= buf[p++];
288
289             if (i < 0 || buf[p-2] & 0x80) {
290                 parity_bits ^= buf[p++];
291                 parity_bits ^= buf[p++];
292             }
293         }
294
295         if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
296             av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
297             goto lost_sync;
298         }
299     } else {
300         GetBitContext gb;
301         MLPHeaderInfo mh;
302
303         init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
304         if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
305             goto lost_sync;
306
307         avctx->bits_per_raw_sample = mh.group1_bits;
308         if (avctx->bits_per_raw_sample > 16)
309             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
310         else
311             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
312         avctx->sample_rate = mh.group1_samplerate;
313         avctx->frame_size = mh.access_unit_size;
314
315         if (mh.stream_type == 0xbb) {
316             /* MLP stream */
317             avctx->channels = mlp_channels[mh.channels_mlp];
318             avctx->channel_layout = mlp_layout[mh.channels_mlp];
319         } else { /* mh.stream_type == 0xba */
320             /* TrueHD stream */
321             if (mh.channels_thd_stream2) {
322                 avctx->channels = truehd_channels(mh.channels_thd_stream2);
323                 avctx->channel_layout = truehd_layout(mh.channels_thd_stream2);
324             } else {
325                 avctx->channels = truehd_channels(mh.channels_thd_stream1);
326                 avctx->channel_layout = truehd_layout(mh.channels_thd_stream1);
327             }
328         }
329
330         if (!mh.is_vbr) /* Stream is CBR */
331             avctx->bit_rate = mh.peak_bitrate;
332
333         mp->num_substreams = mh.num_substreams;
334     }
335
336     *poutbuf = buf;
337     *poutbuf_size = buf_size;
338
339     return next;
340
341 lost_sync:
342     mp->in_sync = 0;
343     return 1;
344 }
345
346 AVCodecParser ff_mlp_parser = {
347     { CODEC_ID_MLP, CODEC_ID_TRUEHD },
348     sizeof(MLPParseContext),
349     mlp_init,
350     mlp_parse,
351     ff_parse_close,
352 };