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