FFmpeg
Loading...
Searching...
No Matches
dca_parser.c
Go to the documentation of this file.
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_core.h"
27#include "dca_exss.h"
28#include "dca_lbr.h"
29#include "dca_syncwords.h"
30#include "get_bits.h"
31#include "parser.h"
32#include "parser_internal.h"
33
34typedef struct DCAParseContext {
36 uint32_t lastmarker;
37 int size;
39 unsigned int startpos;
41 unsigned int sr_code;
43
44#define IS_CORE_MARKER(state) \
45 (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
46 ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
47 ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
48 ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
49
50#define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
51
52#define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
53
54#define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
55#define EXSS_MARKER(state) (state & 0xFFFFFFFF)
56
57#define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
58#define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
59
60#define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
61#define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
62 ((state >> 5) & 0xFFFFF) + 1 : \
63 ((state >> 13) & 0x0FFFF) + 1)
64
65/**
66 * Find the end of the current frame in the bitstream.
67 * @return the position of the first byte of the next frame, or -1
68 */
69static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
70 int buf_size)
71{
72 int start_found, size, i;
73 uint64_t state;
74 ParseContext *pc = &pc1->pc;
75
76 start_found = pc->frame_start_found;
77 state = pc->state64;
78 size = pc1->size;
79
80 i = 0;
81 if (!start_found) {
82 for (; i < buf_size; i++) {
83 size++;
84 state = (state << 8) | buf[i];
85
86 if (IS_MARKER(state) &&
87 (!pc1->lastmarker ||
88 pc1->lastmarker == CORE_MARKER(state) ||
90 if (!pc1->lastmarker)
91 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
92
95 else
97
98 start_found = 1;
99 size = 0;
100
101 i++;
102 break;
103 }
104 }
105 }
106
107 if (start_found) {
108 for (; i < buf_size; i++) {
109 size++;
110 state = (state << 8) | buf[i];
111
112 if (start_found == 1) {
113 switch (pc1->lastmarker) {
115 if (size == 2) {
117 start_found = 2;
118 }
119 break;
121 if (size == 2) {
123 start_found = 4;
124 }
125 break;
127 if (size == 4) {
129 start_found = 4;
130 }
131 break;
133 if (size == 4) {
135 start_found = 4;
136 }
137 break;
139 if (size == 6) {
141 start_found = 4;
142 }
143 break;
144 default:
145 av_assert0(0);
146 }
147 continue;
148 }
149
150 if (start_found == 2 && IS_EXSS_MARKER(state) &&
151 pc1->framesize <= size + 2) {
152 pc1->framesize = size + 2;
153 start_found = 3;
154 continue;
155 }
156
157 if (start_found == 3) {
158 if (size == pc1->framesize + 4) {
160 start_found = 4;
161 }
162 continue;
163 }
164
165 if (pc1->framesize > size)
166 continue;
167
168 if (IS_MARKER(state) &&
169 (pc1->lastmarker == CORE_MARKER(state) ||
171 pc->frame_start_found = 0;
172 pc->state64 = -1;
173 pc1->size = 0;
174 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
175 }
176 }
177 }
178
179 pc->frame_start_found = start_found;
180 pc->state64 = state;
181 pc1->size = size;
182 return END_NOT_FOUND;
183}
184
186{
187 DCAParseContext *pc1 = s->priv_data;
188
189 pc1->lastmarker = 0;
190 pc1->sr_code = -1;
191 return 0;
192}
193
194static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
195 int buf_size, int *duration, int *sample_rate,
196 int *profile)
197{
198 DCAExssAsset *asset = &pc1->exss.assets[0];
199 GetBitContext gb;
202 int ret, frame_size;
203
204 if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
205 return AVERROR_INVALIDDATA;
206
207 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
208 if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
209 return ret;
210
211 if (asset->extension_mask & DCA_EXSS_LBR) {
212 if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
213 return ret;
214
215 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
216 return AVERROR_INVALIDDATA;
217
218 switch (get_bits(&gb, 8)) {
220 pc1->sr_code = get_bits(&gb, 8);
221 break;
223 break;
224 default:
225 return AVERROR_INVALIDDATA;
226 }
227
229 return AVERROR_INVALIDDATA;
230
231 *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
232 *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
234 return 0;
235 }
236
237 if (asset->extension_mask & DCA_EXSS_XLL) {
238 int nsamples_log2;
239
240 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
241 return ret;
242
243 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
244 return AVERROR_INVALIDDATA;
245
246 if (get_bits(&gb, 4))
247 return AVERROR_INVALIDDATA;
248
249 skip_bits(&gb, 8);
250 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
251 skip_bits(&gb, 4);
252 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
253 if (nsamples_log2 > 24)
254 return AVERROR_INVALIDDATA;
255
256 *sample_rate = asset->max_sample_rate;
257 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
259 return 0;
260 }
261
262 return AVERROR_INVALIDDATA;
263 }
264
267 return ret;
268 if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
269 return AVERROR_INVALIDDATA;
270
271 *duration = h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
272 *sample_rate = ff_dca_sample_rates[h.sr_code];
274 return 0;
275
277 if (h.ext_audio_present) {
278 switch (h.ext_audio_type) {
282 break;
285 break;
286 }
287 }
288
289 frame_size = FFALIGN(h.frame_size, 4);
290 if (buf_size - 4 < frame_size)
291 return 0;
292
293 buf += frame_size;
294 buf_size -= frame_size;
296 return 0;
297 if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
298 return 0;
299
300 if (asset->extension_mask & DCA_EXSS_XLL)
304
305 return 0;
306}
307
309 const uint8_t **poutbuf, int *poutbuf_size,
310 const uint8_t *buf, int buf_size)
311{
312 DCAParseContext *pc1 = s->priv_data;
313 ParseContext *pc = &pc1->pc;
314 int next, duration, sample_rate;
315
316 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
317 next = buf_size;
318 } else {
319 next = dca_find_frame_end(pc1, buf, buf_size);
320
321 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
322 *poutbuf = NULL;
323 *poutbuf_size = 0;
324 return buf_size;
325 }
326
327 /* skip initial padding */
328 if (buf_size > pc1->startpos) {
329 buf += pc1->startpos;
330 buf_size -= pc1->startpos;
331 }
332 pc1->startpos = 0;
333 }
334
335 /* read the duration and sample rate from the frame header */
336 if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
337 if (!avctx->sample_rate)
338 avctx->sample_rate = sample_rate;
339 s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
340 } else
341 s->duration = 0;
342
343 *poutbuf = buf;
344 *poutbuf_size = buf_size;
345 return next;
346}
347
350 .priv_data_size = sizeof(DCAParseContext),
352 .parse = dca_parse,
354};
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
const uint8_t ff_dca_freq_ranges[16]
Definition dca.c:41
int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size)
Parse and validate core frame header.
Definition dca.c:144
const uint32_t ff_dca_sampling_freqs[16]
Definition dca.c:36
int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition dca.c:49
@ DCA_EXSS_XXCH
Definition dca.h:176
@ DCA_EXSS_LBR
Definition dca.h:178
@ DCA_EXSS_X96
Definition dca.h:177
@ DCA_EXSS_XLL
Definition dca.h:179
@ DCA_EXSS_XBR
Definition dca.h:175
const uint32_t ff_dca_sample_rates[16]
#define DCA_CORE_FRAME_HEADER_SIZE
Definition dca.h:36
@ DCA_EXT_AUDIO_X96
Definition dca_core.h:74
@ DCA_EXT_AUDIO_XXCH
Definition dca_core.h:75
@ DCA_EXT_AUDIO_XCH
Definition dca_core.h:73
#define DCA_PCMBLOCK_SAMPLES
Definition dca_core.h:44
int ff_dca_exss_parse(DCAExssParser *s, const uint8_t *data, int size)
Definition dca_exss.c:378
@ DCA_LBR_HEADER_SYNC_ONLY
Definition dca_lbr.h:43
@ DCA_LBR_HEADER_DECODER_INIT
Definition dca_lbr.h:44
#define IS_EXSS_MARKER(state)
Definition dca_parser.c:50
#define STATE_14(state)
Definition dca_parser.c:58
#define CORE_MARKER(state)
Definition dca_parser.c:54
static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition dca_parser.c:69
static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition dca_parser.c:308
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition dca_parser.c:185
static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf, int buf_size, int *duration, int *sample_rate, int *profile)
Definition dca_parser.c:194
#define CORE_FRAMESIZE(state)
Definition dca_parser.c:60
#define EXSS_FRAMESIZE(state)
Definition dca_parser.c:61
#define IS_MARKER(state)
Definition dca_parser.c:52
#define STATE_LE(state)
Definition dca_parser.c:57
#define EXSS_MARKER(state)
Definition dca_parser.c:55
const FFCodecParser ff_dca_parser
Definition dca_parser.c:348
#define DCA_SYNCWORD_SUBSTREAM
#define DCA_SYNCWORD_CORE_14B_BE
#define DCA_SYNCWORD_LBR
#define DCA_SYNCWORD_XLL
#define DCA_SYNCWORD_CORE_LE
#define DCA_SYNCWORD_CORE_BE
#define DCA_SYNCWORD_CORE_14B_LE
#define AV_PROFILE_DTS_EXPRESS
Definition defs.h:92
#define AV_PROFILE_DTS_HD_MA
Definition defs.h:91
#define AV_PROFILE_DTS
Definition defs.h:87
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_DTS_HD_HRA
Definition defs.h:90
#define AV_PROFILE_DTS_96_24
Definition defs.h:89
#define AV_PROFILE_DTS_ES
Definition defs.h:88
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
static int64_t duration
Definition ffplay.c:330
static const uint8_t frame_size[4]
Definition g723_1.h:222
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_CODEC_ID_DTS
Definition codec_id.h:457
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_RB32(p)
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
int profile
Definition mxfenc.c:2299
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
int profile
profile
Definition avcodec.h:1636
int sample_rate
samples per second
Definition avcodec.h:1040
int lbr_offset
Offset to LBR component from start of substream.
Definition dca_exss.h:59
int xll_offset
Offset to XLL data from start of substream.
Definition dca_exss.h:62
int extension_mask
Coding components used in asset.
Definition dca_exss.h:45
int xll_size
Size of XLL data in extension substream.
Definition dca_exss.h:63
int lbr_size
Size of LBR component in extension substream.
Definition dca_exss.h:60
int max_sample_rate
Maximum sample rate.
Definition dca_exss.h:35
DCAExssAsset assets[1]
Audio asset descriptors.
Definition dca_exss.h:87
ParseContext pc
Definition dca_parser.c:35
DCAExssParser exss
Definition dca_parser.c:40
unsigned int startpos
Definition dca_parser.c:39
uint32_t lastmarker
Definition dca_parser.c:36
unsigned int sr_code
Definition dca_parser.c:41
uint64_t state64
contains the last 8 bytes in MSB order
Definition parser.h:37
int frame_start_found
Definition parser.h:34
int size