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