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