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
28 #include "dca_syncwords.h"
32 typedef struct DCAParseContext {
37 unsigned int startpos;
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)))
48 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
50 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
52 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
53 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
55 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
56 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
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)
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
67 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
70 int start_found, size, i;
72 ParseContext *pc = &pc1->pc;
74 start_found = pc->frame_start_found;
80 for (; i < buf_size; i++) {
82 state = (state << 8) | buf[i];
84 if (IS_MARKER(state) &&
86 pc1->lastmarker == CORE_MARKER(state) ||
87 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
89 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
91 if (IS_EXSS_MARKER(state))
92 pc1->lastmarker = EXSS_MARKER(state);
94 pc1->lastmarker = CORE_MARKER(state);
106 for (; i < buf_size; i++) {
108 state = (state << 8) | buf[i];
110 if (start_found == 1) {
111 switch (pc1->lastmarker) {
112 case DCA_SYNCWORD_CORE_BE:
114 pc1->framesize = CORE_FRAMESIZE(state);
118 case DCA_SYNCWORD_CORE_LE:
120 pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
124 case DCA_SYNCWORD_CORE_14B_BE:
126 pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
130 case DCA_SYNCWORD_CORE_14B_LE:
132 pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
136 case DCA_SYNCWORD_SUBSTREAM:
138 pc1->framesize = EXSS_FRAMESIZE(state);
148 if (start_found == 2 && IS_EXSS_MARKER(state) &&
149 pc1->framesize <= size + 2) {
150 pc1->framesize = size + 2;
155 if (start_found == 3) {
156 if (size == pc1->framesize + 4) {
157 pc1->framesize += EXSS_FRAMESIZE(state);
163 if (pc1->framesize > size)
166 if (IS_MARKER(state) &&
167 (pc1->lastmarker == CORE_MARKER(state) ||
168 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
169 pc->frame_start_found = 0;
172 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
177 pc->frame_start_found = start_found;
180 return END_NOT_FOUND;
183 static av_cold int dca_parse_init(AVCodecParserContext *s)
185 DCAParseContext *pc1 = s->priv_data;
192 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
193 int buf_size, int *duration, int *sample_rate,
196 DCAExssAsset *asset = &pc1->exss.assets[0];
198 DCACoreFrameHeader h;
199 uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
202 if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
203 return AVERROR_INVALIDDATA;
205 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
206 if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
209 if (asset->extension_mask & DCA_EXSS_LBR) {
210 if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
213 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
214 return AVERROR_INVALIDDATA;
216 switch (get_bits(&gb, 8)) {
218 pc1->sr_code = get_bits(&gb, 8);
222 return AVERROR_INVALIDDATA;
225 if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
226 return AVERROR_INVALIDDATA;
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;
234 if (asset->extension_mask & DCA_EXSS_XLL) {
237 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
240 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
241 return AVERROR_INVALIDDATA;
243 if (get_bits(&gb, 4))
244 return AVERROR_INVALIDDATA;
247 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
249 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
250 if (nsamples_log2 > 24)
251 return AVERROR_INVALIDDATA;
253 *sample_rate = asset->max_sample_rate;
254 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
255 *profile = FF_PROFILE_DTS_HD_MA;
259 return AVERROR_INVALIDDATA;
262 if ((ret = avpriv_dca_convert_bitstream(buf, DCA_CORE_FRAME_HEADER_SIZE,
263 hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
265 if ((ret = init_get_bits8(&gb, hdr, ret)) < 0)
267 if (avpriv_dca_parse_core_frame_header(&gb, &h) < 0)
268 return AVERROR_INVALIDDATA;
270 *duration = 256 * (h.npcmblocks / 8);
271 *sample_rate = avpriv_dca_sample_rates[h.sr_code];
272 if (*profile != FF_PROFILE_UNKNOWN)
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;
282 case DCA_EXT_AUDIO_X96:
283 *profile = FF_PROFILE_DTS_96_24;
288 frame_size = FFALIGN(h.frame_size, 4);
289 if (buf_size - 4 < frame_size)
293 buf_size -= frame_size;
294 if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
296 if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
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;
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)
311 DCAParseContext *pc1 = s->priv_data;
312 ParseContext *pc = &pc1->pc;
313 int next, duration, sample_rate;
315 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
318 next = dca_find_frame_end(pc1, buf, buf_size);
320 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
326 /* skip initial padding */
327 if (buf_size > pc1->startpos) {
328 buf += pc1->startpos;
329 buf_size -= pc1->startpos;
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);
343 *poutbuf_size = buf_size;
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,