FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_exss.h"
27 #include "dca_syncwords.h"
28 #include "get_bits.h"
29 #include "parser.h"
30 
31 typedef struct DCAParseContext {
33  uint32_t lastmarker;
34  int size;
35  int framesize;
36  unsigned int startpos;
38  unsigned int sr_code;
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  */
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) ||
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) {
112  if (size == 2) {
113  pc1->framesize = CORE_FRAMESIZE(state);
114  start_found = 2;
115  }
116  break;
118  if (size == 2) {
119  pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
120  start_found = 4;
121  }
122  break;
124  if (size == 4) {
125  pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
126  start_found = 4;
127  }
128  break;
130  if (size == 4) {
131  pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
132  start_found = 4;
133  }
134  break;
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) ||
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 
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 
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 
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 
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 
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 };
#define NULL
Definition: coverity.c:32
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition: dca_parser.c:182
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ParseContext pc
Definition: dca_parser.c:32
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
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:66
int codec_ids[5]
Definition: avcodec.h:5243
unsigned int sr_code
Definition: dca_parser.c:38
DCAExssParser exss
Definition: dca_parser.c:37
int duration
Duration of the current frame.
Definition: avcodec.h:5197
int frame_start_found
Definition: parser.h:34
const uint8_t ff_dca_freq_ranges[16]
Definition: dca.c:44
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define IS_EXSS_MARKER(state)
Definition: dca_parser.c:47
uint8_t
#define av_cold
Definition: attributes.h:82
#define DCA_SYNCWORD_CORE_14B_BE
Definition: dca_syncwords.h:24
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:87
int64_t duration
Definition: movenc.c:63
int extension_mask
Coding components used in asset.
Definition: dca_exss.h:45
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
AVCodecParser ff_dca_parser
Definition: dca_parser.c:317
#define DCA_SYNCWORD_CORE_BE
Definition: dca_syncwords.h:22
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:250
static struct @253 state
#define IS_MARKER(state)
Definition: dca_parser.c:49
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:39
#define STATE_14(state)
Definition: dca_parser.c:55
int lbr_offset
Offset to LBR component from start of substream.
Definition: dca_exss.h:59
#define DCA_SYNCWORD_CORE_14B_LE
Definition: dca_syncwords.h:25
unsigned int startpos
Definition: dca_parser.c:36
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:326
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:129
int xll_offset
Offset to XLL data from start of substream.
Definition: dca_exss.h:62
#define DCA_SYNCWORD_XLL
Definition: dca_syncwords.h:31
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:48
#define DCA_SYNCWORD_LBR
Definition: dca_syncwords.h:30
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:34
DCAExssAsset assets[1]
Audio asset descriptors.
Definition: dca_exss.h:87
int xll_size
Size of XLL data in extension substream.
Definition: dca_exss.h:63
#define FF_ARRAY_ELEMS(a)
#define EXSS_MARKER(state)
Definition: dca_parser.c:52
int max_sample_rate
Maximum sample rate.
Definition: dca_exss.h:35
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:277
sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2494
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
#define DCA_SYNCWORD_CORE_LE
Definition: dca_syncwords.h:23
main external API structure.
Definition: avcodec.h:1732
#define CORE_FRAMESIZE(state)
Definition: dca_parser.c:57
int lbr_size
Size of LBR component in extension substream.
Definition: dca_exss.h:60
void * buf
Definition: avisynth_c.h:690
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
#define CORE_MARKER(state)
Definition: dca_parser.c:51
#define END_NOT_FOUND
Definition: parser.h:40
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:346
#define EXSS_FRAMESIZE(state)
Definition: dca_parser.c:58
uint32_t lastmarker
Definition: dca_parser.c:33
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5109
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
#define DCA_SYNCWORD_SUBSTREAM
Definition: dca_syncwords.h:32
static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf, int buf_size, int *duration, int *sample_rate)
Definition: dca_parser.c:191
int ff_dca_exss_parse(DCAExssParser *s, const uint8_t *data, int size)
Definition: dca_exss.c:378
#define STATE_LE(state)
Definition: dca_parser.c:54