3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
8 * This file is part of FFmpeg.
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.
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.
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
29 #include "dca_syncwords.h"
33 typedef struct DCAParseContext {
38 unsigned int startpos;
43 #define IS_CORE_MARKER(state) \
44 (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
45 ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
46 ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
47 ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
49 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
51 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
53 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
54 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
56 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
57 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
59 #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
60 #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
61 ((state >> 5) & 0xFFFFF) + 1 : \
62 ((state >> 13) & 0x0FFFF) + 1)
65 * Find the end of the current frame in the bitstream.
66 * @return the position of the first byte of the next frame, or -1
68 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
71 int start_found, size, i;
73 ParseContext *pc = &pc1->pc;
75 start_found = pc->frame_start_found;
81 for (; i < buf_size; i++) {
83 state = (state << 8) | buf[i];
85 if (IS_MARKER(state) &&
87 pc1->lastmarker == CORE_MARKER(state) ||
88 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
90 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
92 if (IS_EXSS_MARKER(state))
93 pc1->lastmarker = EXSS_MARKER(state);
95 pc1->lastmarker = CORE_MARKER(state);
107 for (; i < buf_size; i++) {
109 state = (state << 8) | buf[i];
111 if (start_found == 1) {
112 switch (pc1->lastmarker) {
113 case DCA_SYNCWORD_CORE_BE:
115 pc1->framesize = CORE_FRAMESIZE(state);
119 case DCA_SYNCWORD_CORE_LE:
121 pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
125 case DCA_SYNCWORD_CORE_14B_BE:
127 pc1->framesize = CORE_FRAMESIZE(STATE_14(state));
131 case DCA_SYNCWORD_CORE_14B_LE:
133 pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state)));
137 case DCA_SYNCWORD_SUBSTREAM:
139 pc1->framesize = EXSS_FRAMESIZE(state);
149 if (start_found == 2 && IS_EXSS_MARKER(state) &&
150 pc1->framesize <= size + 2) {
151 pc1->framesize = size + 2;
156 if (start_found == 3) {
157 if (size == pc1->framesize + 4) {
158 pc1->framesize += EXSS_FRAMESIZE(state);
164 if (pc1->framesize > size)
167 if (IS_MARKER(state) &&
168 (pc1->lastmarker == CORE_MARKER(state) ||
169 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
170 pc->frame_start_found = 0;
173 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
178 pc->frame_start_found = start_found;
181 return END_NOT_FOUND;
184 static av_cold int dca_parse_init(AVCodecParserContext *s)
186 DCAParseContext *pc1 = s->priv_data;
193 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
194 int buf_size, int *duration, int *sample_rate,
197 DCAExssAsset *asset = &pc1->exss.assets[0];
199 DCACoreFrameHeader h;
200 uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
203 if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
204 return AVERROR_INVALIDDATA;
206 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
207 if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
210 if (asset->extension_mask & DCA_EXSS_LBR) {
211 if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
214 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
215 return AVERROR_INVALIDDATA;
217 switch (get_bits(&gb, 8)) {
218 case DCA_LBR_HEADER_DECODER_INIT:
219 pc1->sr_code = get_bits(&gb, 8);
220 case DCA_LBR_HEADER_SYNC_ONLY:
223 return AVERROR_INVALIDDATA;
226 if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
227 return AVERROR_INVALIDDATA;
229 *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
230 *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
231 *profile = FF_PROFILE_DTS_EXPRESS;
235 if (asset->extension_mask & DCA_EXSS_XLL) {
238 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
241 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
242 return AVERROR_INVALIDDATA;
244 if (get_bits(&gb, 4))
245 return AVERROR_INVALIDDATA;
248 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
250 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
251 if (nsamples_log2 > 24)
252 return AVERROR_INVALIDDATA;
254 *sample_rate = asset->max_sample_rate;
255 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
256 *profile = FF_PROFILE_DTS_HD_MA;
260 return AVERROR_INVALIDDATA;
263 if ((ret = avpriv_dca_convert_bitstream(buf, DCA_CORE_FRAME_HEADER_SIZE,
264 hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
266 if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
267 return AVERROR_INVALIDDATA;
269 *duration = h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
270 *sample_rate = avpriv_dca_sample_rates[h.sr_code];
271 if (*profile != FF_PROFILE_UNKNOWN)
274 *profile = FF_PROFILE_DTS;
275 if (h.ext_audio_present) {
276 switch (h.ext_audio_type) {
277 case DCA_EXT_AUDIO_XCH:
278 case DCA_EXT_AUDIO_XXCH:
279 *profile = FF_PROFILE_DTS_ES;
281 case DCA_EXT_AUDIO_X96:
282 *profile = FF_PROFILE_DTS_96_24;
287 frame_size = FFALIGN(h.frame_size, 4);
288 if (buf_size - 4 < frame_size)
292 buf_size -= frame_size;
293 if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
295 if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
298 if (asset->extension_mask & DCA_EXSS_XLL)
299 *profile = FF_PROFILE_DTS_HD_MA;
300 else if (asset->extension_mask & (DCA_EXSS_XBR | DCA_EXSS_XXCH | DCA_EXSS_X96))
301 *profile = FF_PROFILE_DTS_HD_HRA;
306 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
307 const uint8_t **poutbuf, int *poutbuf_size,
308 const uint8_t *buf, int buf_size)
310 DCAParseContext *pc1 = s->priv_data;
311 ParseContext *pc = &pc1->pc;
312 int next, duration, sample_rate;
314 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
317 next = dca_find_frame_end(pc1, buf, buf_size);
319 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
325 /* skip initial padding */
326 if (buf_size > pc1->startpos) {
327 buf += pc1->startpos;
328 buf_size -= pc1->startpos;
333 /* read the duration and sample rate from the frame header */
334 if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
335 if (!avctx->sample_rate)
336 avctx->sample_rate = sample_rate;
337 s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
342 *poutbuf_size = buf_size;
346 AVCodecParser ff_dca_parser = {
347 .codec_ids = { AV_CODEC_ID_DTS },
348 .priv_data_size = sizeof(DCAParseContext),
349 .parser_init = dca_parse_init,
350 .parser_parse = dca_parse,
351 .parser_close = ff_parse_close,