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 Libav.
10 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "dca_parser.h"
31 typedef struct DCAParseContext {
39 #define IS_MARKER(state, i, buf, buf_size) \
40 ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
41 || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
42 || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
45 * Find the end of the current frame in the bitstream.
46 * @return the position of the first byte of the next frame, or -1
48 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
53 ParseContext *pc = &pc1->pc;
55 start_found = pc->frame_start_found;
60 for (i = 0; i < buf_size; i++) {
61 state = (state << 8) | buf[i];
62 if (IS_MARKER(state, i, buf, buf_size)) {
63 if (pc1->lastmarker && state == pc1->lastmarker) {
66 } else if (!pc1->lastmarker) {
68 pc1->lastmarker = state;
75 for (; i < buf_size; i++) {
77 state = (state << 8) | buf[i];
78 if (state == DCA_HD_MARKER && !pc1->hd_pos)
79 pc1->hd_pos = pc1->size;
80 if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
81 if(pc1->framesize > pc1->size)
84 pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
86 pc->frame_start_found = 0;
93 pc->frame_start_found = start_found;
98 static av_cold int dca_parse_init(AVCodecParserContext * s)
100 DCAParseContext *pc1 = s->priv_data;
106 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
111 const uint16_t *ssrc = (const uint16_t *) src;
112 uint16_t *sdst = (uint16_t *) dst;
115 if ((unsigned) src_size > (unsigned) max_size)
120 case DCA_MARKER_RAW_BE:
121 memcpy(dst, src, src_size);
123 case DCA_MARKER_RAW_LE:
124 for (i = 0; i < (src_size + 1) >> 1; i++)
125 *sdst++ = av_bswap16(*ssrc++);
127 case DCA_MARKER_14B_BE:
128 case DCA_MARKER_14B_LE:
129 init_put_bits(&pb, dst, max_size);
130 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
131 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
132 put_bits(&pb, 14, tmp);
135 return (put_bits_count(&pb) + 7) >> 3;
137 return AVERROR_INVALIDDATA;
141 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
145 uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
146 int ret, sample_blocks, sr_code;
149 return AVERROR_INVALIDDATA;
151 if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
154 init_get_bits(&gb, hdr, 96);
156 skip_bits_long(&gb, 39);
157 sample_blocks = get_bits(&gb, 7) + 1;
158 if (sample_blocks < 8)
159 return AVERROR_INVALIDDATA;
160 *duration = 256 * (sample_blocks / 8);
163 sr_code = get_bits(&gb, 4);
164 *sample_rate = avpriv_dca_sample_rates[sr_code];
165 if (*sample_rate == 0)
166 return AVERROR_INVALIDDATA;
171 static int dca_parse(AVCodecParserContext * s,
172 AVCodecContext * avctx,
173 const uint8_t ** poutbuf, int *poutbuf_size,
174 const uint8_t * buf, int buf_size)
176 DCAParseContext *pc1 = s->priv_data;
177 ParseContext *pc = &pc1->pc;
178 int next, duration, sample_rate;
180 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
183 next = dca_find_frame_end(pc1, buf, buf_size);
185 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
192 /* read the duration and sample rate from the frame header */
193 if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
194 s->duration = duration;
195 avctx->sample_rate = sample_rate;
200 *poutbuf_size = buf_size;
204 AVCodecParser ff_dca_parser = {
205 .codec_ids = { AV_CODEC_ID_DTS },
206 .priv_data_size = sizeof(DCAParseContext),
207 .parser_init = dca_parse_init,
208 .parser_parse = dca_parse,
209 .parser_close = ff_parse_close,