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
27 #include "dca_syncwords.h"
31 typedef struct DCAParseContext {
36 unsigned int startpos;
41 #define IS_CORE_MARKER(state) \
42 (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
43 ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
44 ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
45 ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
47 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
49 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
51 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
52 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
54 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
55 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
57 #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
58 #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
59 ((state >> 5) & 0xFFFFF) + 1 : \
60 ((state >> 13) & 0x0FFFF) + 1)
63 * Find the end of the current frame in the bitstream.
64 * @return the position of the first byte of the next frame, or -1
66 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
69 int start_found, size, i;
71 ParseContext *pc = &pc1->pc;
73 start_found = pc->frame_start_found;
79 for (; i < buf_size; i++) {
81 state = (state << 8) | buf[i];
83 if (IS_MARKER(state) &&
85 pc1->lastmarker == CORE_MARKER(state) ||
86 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
88 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
90 if (IS_EXSS_MARKER(state))
91 pc1->lastmarker = EXSS_MARKER(state);
93 pc1->lastmarker = CORE_MARKER(state);
105 for (; i < buf_size; i++) {
107 state = (state << 8) | buf[i];
109 if (start_found == 1) {
110 switch (pc1->lastmarker) {
111 case DCA_SYNCWORD_CORE_BE:
113 pc1->framesize = CORE_FRAMESIZE(state);
117 case DCA_SYNCWORD_CORE_LE:
119 pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
123 case DCA_SYNCWORD_CORE_14B_BE:
125 pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
129 case DCA_SYNCWORD_CORE_14B_LE:
131 pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
135 case DCA_SYNCWORD_SUBSTREAM:
137 pc1->framesize = EXSS_FRAMESIZE(state);
147 if (start_found == 2 && IS_EXSS_MARKER(state) &&
148 pc1->framesize <= size + 2) {
149 pc1->framesize = size + 2;
154 if (start_found == 3) {
155 if (size == pc1->framesize + 4) {
156 pc1->framesize += EXSS_FRAMESIZE(state);
162 if (pc1->framesize > size)
165 if (IS_MARKER(state) &&
166 (pc1->lastmarker == CORE_MARKER(state) ||
167 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
168 pc->frame_start_found = 0;
171 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
176 pc->frame_start_found = start_found;
179 return END_NOT_FOUND;
182 static av_cold int dca_parse_init(AVCodecParserContext *s)
184 DCAParseContext *pc1 = s->priv_data;
191 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
192 int buf_size, int *duration, int *sample_rate)
195 uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
196 int ret, sample_blocks;
199 return AVERROR_INVALIDDATA;
201 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
202 DCAExssAsset *asset = &pc1->exss.assets[0];
204 if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
207 if (asset->extension_mask & DCA_EXSS_LBR) {
208 if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
211 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
212 return AVERROR_INVALIDDATA;
214 switch (get_bits(&gb, 8)) {
216 pc1->sr_code = get_bits(&gb, 8);
220 return AVERROR_INVALIDDATA;
223 if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
224 return AVERROR_INVALIDDATA;
226 *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
227 *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
231 if (asset->extension_mask & DCA_EXSS_XLL) {
234 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
237 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
238 return AVERROR_INVALIDDATA;
240 if (get_bits(&gb, 4))
241 return AVERROR_INVALIDDATA;
244 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
246 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
247 if (nsamples_log2 > 24)
248 return AVERROR_INVALIDDATA;
250 *sample_rate = asset->max_sample_rate;
251 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
255 return AVERROR_INVALIDDATA;
258 if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
261 init_get_bits(&gb, hdr, 96);
263 skip_bits_long(&gb, 39);
264 sample_blocks = get_bits(&gb, 7) + 1;
265 if (sample_blocks < 8)
266 return AVERROR_INVALIDDATA;
267 *duration = 256 * (sample_blocks / 8);
270 *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)];
271 if (*sample_rate == 0)
272 return AVERROR_INVALIDDATA;
277 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
278 const uint8_t **poutbuf, int *poutbuf_size,
279 const uint8_t *buf, int buf_size)
281 DCAParseContext *pc1 = s->priv_data;
282 ParseContext *pc = &pc1->pc;
283 int next, duration, sample_rate;
285 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
288 next = dca_find_frame_end(pc1, buf, buf_size);
290 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
296 /* skip initial padding */
297 if (buf_size > pc1->startpos) {
298 buf += pc1->startpos;
299 buf_size -= pc1->startpos;
304 /* read the duration and sample rate from the frame header */
305 if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate)) {
306 if (!avctx->sample_rate)
307 avctx->sample_rate = sample_rate;
308 s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
313 *poutbuf_size = buf_size;
317 AVCodecParser ff_dca_parser = {
318 .codec_ids = { AV_CODEC_ID_DTS },
319 .priv_data_size = sizeof(DCAParseContext),
320 .parser_init = dca_parse_init,
321 .parser_parse = dca_parse,
322 .parser_close = ff_parse_close,