]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
avcodec/dca_parser: export profile information
[ffmpeg] / libavcodec / dca_parser.c
1 /*
2  * DCA parser
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "dca.h"
26 #include "dca_core.h"
27 #include "dca_exss.h"
28 #include "dca_syncwords.h"
29 #include "get_bits.h"
30 #include "parser.h"
31
32 typedef struct DCAParseContext {
33     ParseContext pc;
34     uint32_t lastmarker;
35     int size;
36     int framesize;
37     unsigned int startpos;
38     DCAExssParser exss;
39     unsigned int sr_code;
40 } DCAParseContext;
41
42 #define IS_CORE_MARKER(state) \
43     (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
44      ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
45      ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE     << 16) | 0x00FC)) || \
46      ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE     << 16) | 0xFC00)))
47
48 #define IS_EXSS_MARKER(state)   ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
49
50 #define IS_MARKER(state)        (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
51
52 #define CORE_MARKER(state)      ((state >> 16) & 0xFFFFFFFF)
53 #define EXSS_MARKER(state)      (state & 0xFFFFFFFF)
54
55 #define STATE_LE(state)     (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
56 #define STATE_14(state)     (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
57
58 #define CORE_FRAMESIZE(state)   (((state >> 4) & 0x3FFF) + 1)
59 #define EXSS_FRAMESIZE(state)   ((state & 0x2000000000) ? \
60                                  ((state >>  5) & 0xFFFFF) + 1 : \
61                                  ((state >> 13) & 0x0FFFF) + 1)
62
63 /**
64  * Find the end of the current frame in the bitstream.
65  * @return the position of the first byte of the next frame, or -1
66  */
67 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
68                               int buf_size)
69 {
70     int start_found, size, i;
71     uint64_t state;
72     ParseContext *pc = &pc1->pc;
73
74     start_found = pc->frame_start_found;
75     state       = pc->state64;
76     size        = pc1->size;
77
78     i = 0;
79     if (!start_found) {
80         for (; i < buf_size; i++) {
81             size++;
82             state = (state << 8) | buf[i];
83
84             if (IS_MARKER(state) &&
85                 (!pc1->lastmarker ||
86                   pc1->lastmarker == CORE_MARKER(state) ||
87                   pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
88                 if (!pc1->lastmarker)
89                     pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
90
91                 if (IS_EXSS_MARKER(state))
92                     pc1->lastmarker = EXSS_MARKER(state);
93                 else
94                     pc1->lastmarker = CORE_MARKER(state);
95
96                 start_found = 1;
97                 size        = 0;
98
99                 i++;
100                 break;
101             }
102         }
103     }
104
105     if (start_found) {
106         for (; i < buf_size; i++) {
107             size++;
108             state = (state << 8) | buf[i];
109
110             if (start_found == 1) {
111                 switch (pc1->lastmarker) {
112                 case DCA_SYNCWORD_CORE_BE:
113                     if (size == 2) {
114                         pc1->framesize = CORE_FRAMESIZE(state);
115                         start_found    = 2;
116                     }
117                     break;
118                 case DCA_SYNCWORD_CORE_LE:
119                     if (size == 2) {
120                         pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
121                         start_found    = 4;
122                     }
123                     break;
124                 case DCA_SYNCWORD_CORE_14B_BE:
125                     if (size == 4) {
126                         pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
127                         start_found    = 4;
128                     }
129                     break;
130                 case DCA_SYNCWORD_CORE_14B_LE:
131                     if (size == 4) {
132                         pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
133                         start_found    = 4;
134                     }
135                     break;
136                 case DCA_SYNCWORD_SUBSTREAM:
137                     if (size == 6) {
138                         pc1->framesize = EXSS_FRAMESIZE(state);
139                         start_found    = 4;
140                     }
141                     break;
142                 default:
143                     av_assert0(0);
144                 }
145                 continue;
146             }
147
148             if (start_found == 2 && IS_EXSS_MARKER(state) &&
149                 pc1->framesize <= size + 2) {
150                 pc1->framesize  = size + 2;
151                 start_found     = 3;
152                 continue;
153             }
154
155             if (start_found == 3) {
156                 if (size == pc1->framesize + 4) {
157                     pc1->framesize += EXSS_FRAMESIZE(state);
158                     start_found     = 4;
159                 }
160                 continue;
161             }
162
163             if (pc1->framesize > size)
164                 continue;
165
166             if (IS_MARKER(state) &&
167                 (pc1->lastmarker == CORE_MARKER(state) ||
168                  pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
169                 pc->frame_start_found = 0;
170                 pc->state64           = -1;
171                 pc1->size             = 0;
172                 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
173             }
174         }
175     }
176
177     pc->frame_start_found = start_found;
178     pc->state64           = state;
179     pc1->size             = size;
180     return END_NOT_FOUND;
181 }
182
183 static av_cold int dca_parse_init(AVCodecParserContext *s)
184 {
185     DCAParseContext *pc1 = s->priv_data;
186
187     pc1->lastmarker = 0;
188     pc1->sr_code = -1;
189     return 0;
190 }
191
192 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
193                             int buf_size, int *duration, int *sample_rate,
194                             int *profile)
195 {
196     DCAExssAsset *asset = &pc1->exss.assets[0];
197     GetBitContext gb;
198     DCACoreFrameHeader h;
199     uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
200     int ret, frame_size;
201
202     if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
203         return AVERROR_INVALIDDATA;
204
205     if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
206         if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
207             return ret;
208
209         if (asset->extension_mask & DCA_EXSS_LBR) {
210             if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
211                 return ret;
212
213             if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
214                 return AVERROR_INVALIDDATA;
215
216             switch (get_bits(&gb, 8)) {
217             case 2:
218                 pc1->sr_code = get_bits(&gb, 8);
219             case 1:
220                 break;
221             default:
222                 return AVERROR_INVALIDDATA;
223             }
224
225             if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
226                 return AVERROR_INVALIDDATA;
227
228             *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
229             *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
230             *profile = FF_PROFILE_DTS_EXPRESS;
231             return 0;
232         }
233
234         if (asset->extension_mask & DCA_EXSS_XLL) {
235             int nsamples_log2;
236
237             if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
238                 return ret;
239
240             if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
241                 return AVERROR_INVALIDDATA;
242
243             if (get_bits(&gb, 4))
244                 return AVERROR_INVALIDDATA;
245
246             skip_bits(&gb, 8);
247             skip_bits_long(&gb, get_bits(&gb, 5) + 1);
248             skip_bits(&gb, 4);
249             nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
250             if (nsamples_log2 > 24)
251                 return AVERROR_INVALIDDATA;
252
253             *sample_rate = asset->max_sample_rate;
254             *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
255             *profile = FF_PROFILE_DTS_HD_MA;
256             return 0;
257         }
258
259         return AVERROR_INVALIDDATA;
260     }
261
262     if ((ret = avpriv_dca_convert_bitstream(buf, DCA_CORE_FRAME_HEADER_SIZE,
263                                             hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
264         return ret;
265     if ((ret = init_get_bits8(&gb, hdr, ret)) < 0)
266         return ret;
267     if (avpriv_dca_parse_core_frame_header(&gb, &h) < 0)
268         return AVERROR_INVALIDDATA;
269
270     *duration = 256 * (h.npcmblocks / 8);
271     *sample_rate = avpriv_dca_sample_rates[h.sr_code];
272     if (*profile != FF_PROFILE_UNKNOWN)
273         return 0;
274
275     *profile = FF_PROFILE_DTS;
276     if (h.ext_audio_present) {
277         switch (h.ext_audio_type) {
278         case DCA_EXT_AUDIO_XCH:
279         case DCA_EXT_AUDIO_XXCH:
280             *profile = FF_PROFILE_DTS_ES;
281             break;
282         case DCA_EXT_AUDIO_X96:
283             *profile = FF_PROFILE_DTS_96_24;
284             break;
285         }
286     }
287
288     frame_size = FFALIGN(h.frame_size, 4);
289     if (buf_size - 4 < frame_size)
290         return 0;
291
292     buf      += frame_size;
293     buf_size -= frame_size;
294     if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
295         return 0;
296     if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
297         return 0;
298
299     if (asset->extension_mask & DCA_EXSS_XLL)
300         *profile = FF_PROFILE_DTS_HD_MA;
301     else if (asset->extension_mask & (DCA_EXSS_XBR | DCA_EXSS_XXCH | DCA_EXSS_X96))
302         *profile = FF_PROFILE_DTS_HD_HRA;
303
304     return 0;
305 }
306
307 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
308                      const uint8_t **poutbuf, int *poutbuf_size,
309                      const uint8_t *buf, int buf_size)
310 {
311     DCAParseContext *pc1 = s->priv_data;
312     ParseContext *pc = &pc1->pc;
313     int next, duration, sample_rate;
314
315     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
316         next = buf_size;
317     } else {
318         next = dca_find_frame_end(pc1, buf, buf_size);
319
320         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
321             *poutbuf      = NULL;
322             *poutbuf_size = 0;
323             return buf_size;
324         }
325
326         /* skip initial padding */
327         if (buf_size  > pc1->startpos) {
328             buf      += pc1->startpos;
329             buf_size -= pc1->startpos;
330         }
331         pc1->startpos = 0;
332     }
333
334     /* read the duration and sample rate from the frame header */
335     if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
336         if (!avctx->sample_rate)
337             avctx->sample_rate = sample_rate;
338         s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
339     } else
340         s->duration = 0;
341
342     *poutbuf      = buf;
343     *poutbuf_size = buf_size;
344     return next;
345 }
346
347 AVCodecParser ff_dca_parser = {
348     .codec_ids      = { AV_CODEC_ID_DTS },
349     .priv_data_size = sizeof(DCAParseContext),
350     .parser_init    = dca_parse_init,
351     .parser_parse   = dca_parse,
352     .parser_close   = ff_parse_close,
353 };