blob: e5bea3347cc7cf94088cb0c0afae6f5fe7926150
1 | /* |
2 | * DCA parser |
3 | * Copyright (C) 2004 Gildas Bazin |
4 | * Copyright (C) 2004 Benjamin Zores |
5 | * Copyright (C) 2006 Benjamin Larsson |
6 | * Copyright (C) 2007 Konstantin Shishkov |
7 | * |
8 | * This file is part of FFmpeg. |
9 | * |
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. |
14 | * |
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. |
19 | * |
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 |
23 | */ |
24 | |
25 | #include "dca.h" |
26 | #include "dca_exss.h" |
27 | #include "dca_syncwords.h" |
28 | #include "get_bits.h" |
29 | #include "parser.h" |
30 | |
31 | typedef struct DCAParseContext { |
32 | ParseContext pc; |
33 | uint32_t lastmarker; |
34 | int size; |
35 | int framesize; |
36 | unsigned int startpos; |
37 | DCAExssParser exss; |
38 | unsigned int sr_code; |
39 | } DCAParseContext; |
40 | |
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))) |
46 | |
47 | #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM) |
48 | |
49 | #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state)) |
50 | |
51 | #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF) |
52 | #define EXSS_MARKER(state) (state & 0xFFFFFFFF) |
53 | |
54 | #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8)) |
55 | #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6)) |
56 | |
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) |
61 | |
62 | /** |
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 |
65 | */ |
66 | static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf, |
67 | int buf_size) |
68 | { |
69 | int start_found, size, i; |
70 | uint64_t state; |
71 | ParseContext *pc = &pc1->pc; |
72 | |
73 | start_found = pc->frame_start_found; |
74 | state = pc->state64; |
75 | size = pc1->size; |
76 | |
77 | i = 0; |
78 | if (!start_found) { |
79 | for (; i < buf_size; i++) { |
80 | size++; |
81 | state = (state << 8) | buf[i]; |
82 | |
83 | if (IS_MARKER(state) && |
84 | (!pc1->lastmarker || |
85 | pc1->lastmarker == CORE_MARKER(state) || |
86 | pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) { |
87 | if (!pc1->lastmarker) |
88 | pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6; |
89 | |
90 | if (IS_EXSS_MARKER(state)) |
91 | pc1->lastmarker = EXSS_MARKER(state); |
92 | else |
93 | pc1->lastmarker = CORE_MARKER(state); |
94 | |
95 | start_found = 1; |
96 | size = 0; |
97 | |
98 | i++; |
99 | break; |
100 | } |
101 | } |
102 | } |
103 | |
104 | if (start_found) { |
105 | for (; i < buf_size; i++) { |
106 | size++; |
107 | state = (state << 8) | buf[i]; |
108 | |
109 | if (start_found == 1) { |
110 | switch (pc1->lastmarker) { |
111 | case DCA_SYNCWORD_CORE_BE: |
112 | if (size == 2) { |
113 | pc1->framesize = CORE_FRAMESIZE(state); |
114 | start_found = 2; |
115 | } |
116 | break; |
117 | case DCA_SYNCWORD_CORE_LE: |
118 | if (size == 2) { |
119 | pc1->framesize = CORE_FRAMESIZE(STATE_LE(state)); |
120 | start_found = 4; |
121 | } |
122 | break; |
123 | case DCA_SYNCWORD_CORE_14B_BE: |
124 | if (size == 4) { |
125 | pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2; |
126 | start_found = 4; |
127 | } |
128 | break; |
129 | case DCA_SYNCWORD_CORE_14B_LE: |
130 | if (size == 4) { |
131 | pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2; |
132 | start_found = 4; |
133 | } |
134 | break; |
135 | case DCA_SYNCWORD_SUBSTREAM: |
136 | if (size == 6) { |
137 | pc1->framesize = EXSS_FRAMESIZE(state); |
138 | start_found = 4; |
139 | } |
140 | break; |
141 | default: |
142 | av_assert0(0); |
143 | } |
144 | continue; |
145 | } |
146 | |
147 | if (start_found == 2 && IS_EXSS_MARKER(state) && |
148 | pc1->framesize <= size + 2) { |
149 | pc1->framesize = size + 2; |
150 | start_found = 3; |
151 | continue; |
152 | } |
153 | |
154 | if (start_found == 3) { |
155 | if (size == pc1->framesize + 4) { |
156 | pc1->framesize += EXSS_FRAMESIZE(state); |
157 | start_found = 4; |
158 | } |
159 | continue; |
160 | } |
161 | |
162 | if (pc1->framesize > size) |
163 | continue; |
164 | |
165 | if (IS_MARKER(state) && |
166 | (pc1->lastmarker == CORE_MARKER(state) || |
167 | pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) { |
168 | pc->frame_start_found = 0; |
169 | pc->state64 = -1; |
170 | pc1->size = 0; |
171 | return IS_EXSS_MARKER(state) ? i - 3 : i - 5; |
172 | } |
173 | } |
174 | } |
175 | |
176 | pc->frame_start_found = start_found; |
177 | pc->state64 = state; |
178 | pc1->size = size; |
179 | return END_NOT_FOUND; |
180 | } |
181 | |
182 | static av_cold int dca_parse_init(AVCodecParserContext *s) |
183 | { |
184 | DCAParseContext *pc1 = s->priv_data; |
185 | |
186 | pc1->lastmarker = 0; |
187 | pc1->sr_code = -1; |
188 | return 0; |
189 | } |
190 | |
191 | static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf, |
192 | int buf_size, int *duration, int *sample_rate) |
193 | { |
194 | GetBitContext gb; |
195 | uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 }; |
196 | int ret, sample_blocks; |
197 | |
198 | if (buf_size < 12) |
199 | return AVERROR_INVALIDDATA; |
200 | |
201 | if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) { |
202 | DCAExssAsset *asset = &pc1->exss.assets[0]; |
203 | |
204 | if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0) |
205 | return ret; |
206 | |
207 | if (asset->extension_mask & DCA_EXSS_LBR) { |
208 | if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0) |
209 | return ret; |
210 | |
211 | if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR) |
212 | return AVERROR_INVALIDDATA; |
213 | |
214 | switch (get_bits(&gb, 8)) { |
215 | case 2: |
216 | pc1->sr_code = get_bits(&gb, 8); |
217 | case 1: |
218 | break; |
219 | default: |
220 | return AVERROR_INVALIDDATA; |
221 | } |
222 | |
223 | if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) |
224 | return AVERROR_INVALIDDATA; |
225 | |
226 | *sample_rate = ff_dca_sampling_freqs[pc1->sr_code]; |
227 | *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code]; |
228 | return 0; |
229 | } |
230 | |
231 | if (asset->extension_mask & DCA_EXSS_XLL) { |
232 | int nsamples_log2; |
233 | |
234 | if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0) |
235 | return ret; |
236 | |
237 | if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL) |
238 | return AVERROR_INVALIDDATA; |
239 | |
240 | if (get_bits(&gb, 4)) |
241 | return AVERROR_INVALIDDATA; |
242 | |
243 | skip_bits(&gb, 8); |
244 | skip_bits_long(&gb, get_bits(&gb, 5) + 1); |
245 | skip_bits(&gb, 4); |
246 | nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4); |
247 | if (nsamples_log2 > 24) |
248 | return AVERROR_INVALIDDATA; |
249 | |
250 | *sample_rate = asset->max_sample_rate; |
251 | *duration = (1 + (*sample_rate > 96000)) << nsamples_log2; |
252 | return 0; |
253 | } |
254 | |
255 | return AVERROR_INVALIDDATA; |
256 | } |
257 | |
258 | if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0) |
259 | return ret; |
260 | |
261 | init_get_bits(&gb, hdr, 96); |
262 | |
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); |
268 | |
269 | skip_bits(&gb, 20); |
270 | *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)]; |
271 | if (*sample_rate == 0) |
272 | return AVERROR_INVALIDDATA; |
273 | |
274 | return 0; |
275 | } |
276 | |
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) |
280 | { |
281 | DCAParseContext *pc1 = s->priv_data; |
282 | ParseContext *pc = &pc1->pc; |
283 | int next, duration, sample_rate; |
284 | |
285 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
286 | next = buf_size; |
287 | } else { |
288 | next = dca_find_frame_end(pc1, buf, buf_size); |
289 | |
290 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
291 | *poutbuf = NULL; |
292 | *poutbuf_size = 0; |
293 | return buf_size; |
294 | } |
295 | |
296 | /* skip initial padding */ |
297 | if (buf_size > pc1->startpos) { |
298 | buf += pc1->startpos; |
299 | buf_size -= pc1->startpos; |
300 | } |
301 | pc1->startpos = 0; |
302 | } |
303 | |
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); |
309 | } else |
310 | s->duration = 0; |
311 | |
312 | *poutbuf = buf; |
313 | *poutbuf_size = buf_size; |
314 | return next; |
315 | } |
316 | |
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, |
323 | }; |
324 |