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
26 #include "dca_syncwords.h"
30 typedef struct DCAParseContext {
38 #define IS_MARKER(state, i, buf, buf_size) \
39 ((state == DCA_SYNCWORD_CORE_14B_LE && (i < buf_size - 2) && (buf[i + 1] & 0xF0) == 0xF0 && buf[i + 2] == 0x07) || \
40 (state == DCA_SYNCWORD_CORE_14B_BE && (i < buf_size - 2) && buf[i + 1] == 0x07 && (buf[i + 2] & 0xF0) == 0xF0) || \
41 state == DCA_SYNCWORD_CORE_LE || state == DCA_SYNCWORD_CORE_BE || state == DCA_SYNCWORD_SUBSTREAM)
44 * Find the end of the current frame in the bitstream.
45 * @return the position of the first byte of the next frame, or -1
47 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
52 ParseContext *pc = &pc1->pc;
54 start_found = pc->frame_start_found;
59 for (i = 0; i < buf_size; i++) {
60 state = (state << 8) | buf[i];
61 if (IS_MARKER(state, i, buf, buf_size)) {
62 if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM) {
64 pc1->lastmarker = state;
72 for (; i < buf_size; i++) {
74 state = (state << 8) | buf[i];
75 if (state == DCA_SYNCWORD_SUBSTREAM && !pc1->hd_pos)
76 pc1->hd_pos = pc1->size;
77 if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
78 if (pc1->framesize > pc1->size)
80 pc->frame_start_found = 0;
87 pc->frame_start_found = start_found;
92 static av_cold int dca_parse_init(AVCodecParserContext *s)
94 DCAParseContext *pc1 = s->priv_data;
100 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
101 int *sample_rate, int *framesize)
104 uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
105 int ret, sample_blocks, sr_code;
108 return AVERROR_INVALIDDATA;
110 if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
113 init_get_bits(&gb, hdr, 96);
115 skip_bits_long(&gb, 39);
116 sample_blocks = get_bits(&gb, 7) + 1;
117 if (sample_blocks < 8)
118 return AVERROR_INVALIDDATA;
119 *duration = 256 * (sample_blocks / 8);
121 *framesize = get_bits(&gb, 14) + 1;
123 return AVERROR_INVALIDDATA;
126 sr_code = get_bits(&gb, 4);
127 *sample_rate = avpriv_dca_sample_rates[sr_code];
128 if (*sample_rate == 0)
129 return AVERROR_INVALIDDATA;
134 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
135 const uint8_t **poutbuf, int *poutbuf_size,
136 const uint8_t *buf, int buf_size)
138 DCAParseContext *pc1 = s->priv_data;
139 ParseContext *pc = &pc1->pc;
140 int next, duration, sample_rate;
142 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
145 next = dca_find_frame_end(pc1, buf, buf_size);
147 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
154 /* read the duration and sample rate from the frame header */
155 if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
156 s->duration = duration;
157 avctx->sample_rate = sample_rate;
162 *poutbuf_size = buf_size;
166 AVCodecParser ff_dca_parser = {
167 .codec_ids = { AV_CODEC_ID_DTS },
168 .priv_data_size = sizeof(DCAParseContext),
169 .parser_init = dca_parse_init,
170 .parser_parse = dca_parse,
171 .parser_close = ff_parse_close,