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