FFmpeg
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 
33 typedef struct DCAParseContext {
35  uint32_t lastmarker;
36  int size;
37  int framesize;
38  unsigned int startpos;
40  unsigned int sr_code;
42 
43 #define IS_CORE_MARKER(state) \
44  (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
45  ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
46  ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
47  ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
48 
49 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
50 
51 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
52 
53 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
54 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
55 
56 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
57 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
58 
59 #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
60 #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
61  ((state >> 5) & 0xFFFFF) + 1 : \
62  ((state >> 13) & 0x0FFFF) + 1)
63 
64 /**
65  * Find the end of the current frame in the bitstream.
66  * @return the position of the first byte of the next frame, or -1
67  */
68 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
69  int buf_size)
70 {
71  int start_found, size, i;
72  uint64_t state;
73  ParseContext *pc = &pc1->pc;
74 
75  start_found = pc->frame_start_found;
76  state = pc->state64;
77  size = pc1->size;
78 
79  i = 0;
80  if (!start_found) {
81  for (; i < buf_size; i++) {
82  size++;
83  state = (state << 8) | buf[i];
84 
85  if (IS_MARKER(state) &&
86  (!pc1->lastmarker ||
87  pc1->lastmarker == CORE_MARKER(state) ||
89  if (!pc1->lastmarker)
90  pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
91 
92  if (IS_EXSS_MARKER(state))
94  else
96 
97  start_found = 1;
98  size = 0;
99 
100  i++;
101  break;
102  }
103  }
104  }
105 
106  if (start_found) {
107  for (; i < buf_size; i++) {
108  size++;
109  state = (state << 8) | buf[i];
110 
111  if (start_found == 1) {
112  switch (pc1->lastmarker) {
114  if (size == 2) {
116  start_found = 2;
117  }
118  break;
120  if (size == 2) {
122  start_found = 4;
123  }
124  break;
126  if (size == 4) {
128  start_found = 4;
129  }
130  break;
132  if (size == 4) {
134  start_found = 4;
135  }
136  break;
138  if (size == 6) {
140  start_found = 4;
141  }
142  break;
143  default:
144  av_assert0(0);
145  }
146  continue;
147  }
148 
149  if (start_found == 2 && IS_EXSS_MARKER(state) &&
150  pc1->framesize <= size + 2) {
151  pc1->framesize = size + 2;
152  start_found = 3;
153  continue;
154  }
155 
156  if (start_found == 3) {
157  if (size == pc1->framesize + 4) {
158  pc1->framesize += EXSS_FRAMESIZE(state);
159  start_found = 4;
160  }
161  continue;
162  }
163 
164  if (pc1->framesize > size)
165  continue;
166 
167  if (IS_MARKER(state) &&
168  (pc1->lastmarker == CORE_MARKER(state) ||
170  pc->frame_start_found = 0;
171  pc->state64 = -1;
172  pc1->size = 0;
173  return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
174  }
175  }
176  }
177 
178  pc->frame_start_found = start_found;
179  pc->state64 = state;
180  pc1->size = size;
181  return END_NOT_FOUND;
182 }
183 
185 {
186  DCAParseContext *pc1 = s->priv_data;
187 
188  pc1->lastmarker = 0;
189  pc1->sr_code = -1;
190  return 0;
191 }
192 
193 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
194  int buf_size, int *duration, int *sample_rate,
195  int *profile)
196 {
197  DCAExssAsset *asset = &pc1->exss.assets[0];
198  GetBitContext gb;
201  int ret, frame_size;
202 
203  if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
204  return AVERROR_INVALIDDATA;
205 
206  if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
207  if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
208  return ret;
209 
210  if (asset->extension_mask & DCA_EXSS_LBR) {
211  if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
212  return ret;
213 
214  if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
215  return AVERROR_INVALIDDATA;
216 
217  switch (get_bits(&gb, 8)) {
219  pc1->sr_code = get_bits(&gb, 8);
221  break;
222  default:
223  return AVERROR_INVALIDDATA;
224  }
225 
227  return AVERROR_INVALIDDATA;
228 
230  *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
232  return 0;
233  }
234 
235  if (asset->extension_mask & DCA_EXSS_XLL) {
236  int nsamples_log2;
237 
238  if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
239  return ret;
240 
241  if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
242  return AVERROR_INVALIDDATA;
243 
244  if (get_bits(&gb, 4))
245  return AVERROR_INVALIDDATA;
246 
247  skip_bits(&gb, 8);
248  skip_bits_long(&gb, get_bits(&gb, 5) + 1);
249  skip_bits(&gb, 4);
250  nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
251  if (nsamples_log2 > 24)
252  return AVERROR_INVALIDDATA;
253 
254  *sample_rate = asset->max_sample_rate;
255  *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
257  return 0;
258  }
259 
260  return AVERROR_INVALIDDATA;
261  }
262 
264  hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
265  return ret;
266  if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
267  return AVERROR_INVALIDDATA;
268 
269  *duration = h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
270  *sample_rate = ff_dca_sample_rates[h.sr_code];
271  if (*profile != FF_PROFILE_UNKNOWN)
272  return 0;
273 
275  if (h.ext_audio_present) {
276  switch (h.ext_audio_type) {
277  case DCA_EXT_AUDIO_XCH:
278  case DCA_EXT_AUDIO_XXCH:
280  break;
281  case DCA_EXT_AUDIO_X96:
283  break;
284  }
285  }
286 
287  frame_size = FFALIGN(h.frame_size, 4);
288  if (buf_size - 4 < frame_size)
289  return 0;
290 
291  buf += frame_size;
292  buf_size -= frame_size;
293  if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
294  return 0;
295  if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
296  return 0;
297 
298  if (asset->extension_mask & DCA_EXSS_XLL)
300  else if (asset->extension_mask & (DCA_EXSS_XBR | DCA_EXSS_XXCH | DCA_EXSS_X96))
302 
303  return 0;
304 }
305 
307  const uint8_t **poutbuf, int *poutbuf_size,
308  const uint8_t *buf, int buf_size)
309 {
310  DCAParseContext *pc1 = s->priv_data;
311  ParseContext *pc = &pc1->pc;
312  int next, duration, sample_rate;
313 
314  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
315  next = buf_size;
316  } else {
317  next = dca_find_frame_end(pc1, buf, buf_size);
318 
319  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
320  *poutbuf = NULL;
321  *poutbuf_size = 0;
322  return buf_size;
323  }
324 
325  /* skip initial padding */
326  if (buf_size > pc1->startpos) {
327  buf += pc1->startpos;
328  buf_size -= pc1->startpos;
329  }
330  pc1->startpos = 0;
331  }
332 
333  /* read the duration and sample rate from the frame header */
334  if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
335  if (!avctx->sample_rate)
336  avctx->sample_rate = sample_rate;
337  s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
338  } else
339  s->duration = 0;
340 
341  *poutbuf = buf;
342  *poutbuf_size = buf_size;
343  return next;
344 }
345 
347  .codec_ids = { AV_CODEC_ID_DTS },
348  .priv_data_size = sizeof(DCAParseContext),
349  .parser_init = dca_parse_init,
350  .parser_parse = dca_parse,
351  .parser_close = ff_parse_close,
352 };
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:292
DCA_EXT_AUDIO_X96
@ DCA_EXT_AUDIO_X96
Definition: dca_core.h:76
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
dca.h
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:547
DCA_SYNCWORD_CORE_14B_BE
#define DCA_SYNCWORD_CORE_14B_BE
Definition: dca_syncwords.h:24
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:286
DCA_EXSS_XXCH
@ DCA_EXSS_XXCH
Definition: dca.h:176
DCAParseContext::size
int size
Definition: dca_parser.c:36
DCAExssParser
Definition: dca_exss.h:71
DCAExssAsset::xll_size
int xll_size
Size of XLL data in extension substream.
Definition: dca_exss.h:63
ff_dca_parser
const AVCodecParser ff_dca_parser
Definition: dca_parser.c:346
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
DCAExssAsset
Definition: dca_exss.h:29
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
DCAExssAsset::lbr_offset
int lbr_offset
Offset to LBR component from start of substream.
Definition: dca_exss.h:59
CORE_MARKER
#define CORE_MARKER(state)
Definition: dca_parser.c:53
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
avpriv_dca_convert_bitstream
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
ParseContext
Definition: parser.h:28
DCA_EXSS_XLL
@ DCA_EXSS_XLL
Definition: dca.h:179
FF_PROFILE_DTS_HD_HRA
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:1550
GetBitContext
Definition: get_bits.h:62
DCA_EXT_AUDIO_XXCH
@ DCA_EXT_AUDIO_XXCH
Definition: dca_core.h:77
dca_core.h
IS_EXSS_MARKER
#define IS_EXSS_MARKER(state)
Definition: dca_parser.c:49
DCA_SYNCWORD_XLL
#define DCA_SYNCWORD_XLL
Definition: dca_syncwords.h:31
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
duration
int64_t duration
Definition: movenc.c:64
s
#define s(width, name)
Definition: cbs_vp9.c:257
frame_size
int frame_size
Definition: mxfenc.c:2199
dca_parse_params
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:193
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1526
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
dca_find_frame_end
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:68
get_bits.h
dca_exss.h
DCAParseContext::framesize
int framesize
Definition: dca_parser.c:37
ff_dca_sample_rates
const uint32_t ff_dca_sample_rates[16]
Definition: dca_sample_rate_tab.h:29
dca_syncwords.h
DCA_EXSS_XBR
@ DCA_EXSS_XBR
Definition: dca.h:175
NULL
#define NULL
Definition: coverity.c:32
DCAExssAsset::lbr_size
int lbr_size
Size of LBR component in extension substream.
Definition: dca_exss.h:60
EXSS_MARKER
#define EXSS_MARKER(state)
Definition: dca_parser.c:54
FF_PROFILE_DTS_ES
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:1548
DCAParseContext::sr_code
unsigned int sr_code
Definition: dca_parser.c:40
IS_MARKER
#define IS_MARKER(state)
Definition: dca_parser.c:51
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
DCA_EXSS_X96
@ DCA_EXSS_X96
Definition: dca.h:177
DCAParseContext::exss
DCAExssParser exss
Definition: dca_parser.c:39
DCAExssAsset::xll_offset
int xll_offset
Offset to XLL data from start of substream.
Definition: dca_exss.h:62
DCA_LBR_HEADER_SYNC_ONLY
@ DCA_LBR_HEADER_SYNC_ONLY
Definition: dca_lbr.h:45
DCA_SYNCWORD_CORE_BE
#define DCA_SYNCWORD_CORE_BE
Definition: dca_syncwords.h:22
DCACoreFrameHeader
Definition: dca.h:50
state
static struct @320 state
DCAParseContext::startpos
unsigned int startpos
Definition: dca_parser.c:38
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2935
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:427
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
DCA_SYNCWORD_CORE_14B_LE
#define DCA_SYNCWORD_CORE_14B_LE
Definition: dca_syncwords.h:25
FF_PROFILE_DTS
#define FF_PROFILE_DTS
Definition: avcodec.h:1547
STATE_LE
#define STATE_LE(state)
Definition: dca_parser.c:56
DCAExssAsset::max_sample_rate
int max_sample_rate
Maximum sample rate.
Definition: dca_exss.h:35
DCA_LBR_HEADER_DECODER_INIT
@ DCA_LBR_HEADER_DECODER_INIT
Definition: dca_lbr.h:46
ff_combine_frame
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:201
ff_dca_exss_parse
int ff_dca_exss_parse(DCAExssParser *s, const uint8_t *data, int size)
Definition: dca_exss.c:378
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2809
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
parser.h
profile
int profile
Definition: mxfenc.c:2003
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
DCA_SYNCWORD_SUBSTREAM
#define DCA_SYNCWORD_SUBSTREAM
Definition: dca_syncwords.h:32
DCAExssParser::assets
DCAExssAsset assets[1]
Audio asset descriptors.
Definition: dca_exss.h:87
dca_parse
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:306
DCA_EXT_AUDIO_XCH
@ DCA_EXT_AUDIO_XCH
Definition: dca_core.h:75
AVCodecParserContext
Definition: avcodec.h:2775
DCA_PCMBLOCK_SAMPLES
#define DCA_PCMBLOCK_SAMPLES
Definition: dca_core.h:46
DCA_SYNCWORD_LBR
#define DCA_SYNCWORD_LBR
Definition: dca_syncwords.h:30
ret
ret
Definition: filter_design.txt:187
DCA_CORE_FRAME_HEADER_SIZE
#define DCA_CORE_FRAME_HEADER_SIZE
Definition: dca.h:36
FF_PROFILE_DTS_96_24
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:1549
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:383
ff_dca_sampling_freqs
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:36
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1525
DCAParseContext
Definition: dca_parser.c:33
ParseContext::state64
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
DCA_SYNCWORD_CORE_LE
#define DCA_SYNCWORD_CORE_LE
Definition: dca_syncwords.h:23
avpriv_dca_parse_core_frame_header
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
ff_dca_freq_ranges
const uint8_t ff_dca_freq_ranges[16]
Definition: dca.c:41
FF_PROFILE_DTS_HD_MA
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:1551
DCAExssAsset::extension_mask
int extension_mask
Coding components used in asset.
Definition: dca_exss.h:45
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParser
Definition: avcodec.h:2934
DCAParseContext::pc
ParseContext pc
Definition: dca_parser.c:34
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
EXSS_FRAMESIZE
#define EXSS_FRAMESIZE(state)
Definition: dca_parser.c:60
h
h
Definition: vp9dsp_template.c:2038
STATE_14
#define STATE_14(state)
Definition: dca_parser.c:57
dca_lbr.h
dca_parse_init
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition: dca_parser.c:184
DCAParseContext::lastmarker
uint32_t lastmarker
Definition: dca_parser.c:35
CORE_FRAMESIZE
#define CORE_FRAMESIZE(state)
Definition: dca_parser.c:59
DCA_EXSS_LBR
@ DCA_EXSS_LBR
Definition: dca.h:178
FF_PROFILE_DTS_EXPRESS
#define FF_PROFILE_DTS_EXPRESS
Definition: avcodec.h:1552