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_syncwords.h"
27 #include "get_bits.h"
28 #include "parser.h"
29 
30 typedef struct DCAParseContext {
32  uint32_t lastmarker;
33  int size;
34  int framesize;
36 
37 #define IS_CORE_MARKER(state) \
38  (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
39  ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
40  ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
41  ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
42 
43 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
44 
45 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
46 
47 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
48 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
49 
50 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
51 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
52 
53 #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
54 #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
55  ((state >> 5) & 0xFFFFF) + 1 : \
56  ((state >> 13) & 0x0FFFF) + 1)
57 
58 /**
59  * Find the end of the current frame in the bitstream.
60  * @return the position of the first byte of the next frame, or -1
61  */
63  int buf_size)
64 {
65  int start_found, size, i;
66  uint64_t state;
67  ParseContext *pc = &pc1->pc;
68 
69  start_found = pc->frame_start_found;
70  state = pc->state64;
71  size = pc1->size;
72 
73  i = 0;
74  if (!start_found) {
75  for (i = 0; i < buf_size; i++) {
76  state = (state << 8) | buf[i];
77  if (IS_MARKER(state)) {
78  if (!pc1->lastmarker ||
79  pc1->lastmarker == CORE_MARKER(state) ||
81  start_found = 1;
82  if (IS_EXSS_MARKER(state))
83  pc1->lastmarker = EXSS_MARKER(state);
84  else
85  pc1->lastmarker = CORE_MARKER(state);
86  i++;
87  break;
88  }
89  }
90  }
91  }
92 
93  if (start_found) {
94  for (; i < buf_size; i++) {
95  size++;
96  state = (state << 8) | buf[i];
97 
98  if (start_found == 1) {
99  switch (pc1->lastmarker) {
101  if (size == 2) {
102  pc1->framesize = CORE_FRAMESIZE(state);
103  start_found = 2;
104  }
105  break;
107  if (size == 2) {
108  pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
109  start_found = 2;
110  }
111  break;
113  if (size == 4) {
114  pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
115  start_found = 2;
116  }
117  break;
119  if (size == 4) {
120  pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
121  start_found = 2;
122  }
123  break;
125  if (size == 6) {
126  pc1->framesize = EXSS_FRAMESIZE(state);
127  start_found = 2;
128  }
129  break;
130  default:
131  av_assert0(0);
132  }
133  continue;
134  }
135 
136  if (pc1->lastmarker == DCA_SYNCWORD_CORE_BE) {
137  if (pc1->framesize > size + 2)
138  continue;
139 
140  if (start_found == 2 && IS_EXSS_MARKER(state)) {
141  pc1->framesize = size + 2;
142  start_found = 3;
143  continue;
144  }
145 
146  if (start_found == 3) {
147  if (size == pc1->framesize + 4) {
148  pc1->framesize += EXSS_FRAMESIZE(state);
149  start_found = 4;
150  }
151  continue;
152  }
153  }
154 
155  if (pc1->framesize > size)
156  continue;
157 
158  if (IS_MARKER(state) &&
159  (pc1->lastmarker == CORE_MARKER(state) ||
161  pc->frame_start_found = 0;
162  pc->state64 = -1;
163  pc1->size = 0;
164  return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
165  }
166  }
167  }
168 
169  pc->frame_start_found = start_found;
170  pc->state64 = state;
171  pc1->size = size;
172  return END_NOT_FOUND;
173 }
174 
176 {
177  DCAParseContext *pc1 = s->priv_data;
178 
179  pc1->lastmarker = 0;
180  return 0;
181 }
182 
183 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
184  int *sample_rate)
185 {
186  GetBitContext gb;
187  uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
188  int ret, sample_blocks;
189 
190  if (buf_size < 12)
191  return AVERROR_INVALIDDATA;
192 
193  if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
194  return ret;
195 
196  init_get_bits(&gb, hdr, 96);
197 
198  skip_bits_long(&gb, 39);
199  sample_blocks = get_bits(&gb, 7) + 1;
200  if (sample_blocks < 8)
201  return AVERROR_INVALIDDATA;
202  *duration = 256 * (sample_blocks / 8);
203 
204  skip_bits(&gb, 20);
205  *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)];
206  if (*sample_rate == 0)
207  return AVERROR_INVALIDDATA;
208 
209  return 0;
210 }
211 
213  const uint8_t **poutbuf, int *poutbuf_size,
214  const uint8_t *buf, int buf_size)
215 {
216  DCAParseContext *pc1 = s->priv_data;
217  ParseContext *pc = &pc1->pc;
218  int next, duration, sample_rate;
219 
221  next = buf_size;
222  } else {
223  next = dca_find_frame_end(pc1, buf, buf_size);
224 
225  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
226  *poutbuf = NULL;
227  *poutbuf_size = 0;
228  return buf_size;
229  }
230  }
231 
232  /* read the duration and sample rate from the frame header */
233  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
234  if (!avctx->sample_rate)
235  avctx->sample_rate = sample_rate;
236  s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
237  } else
238  s->duration = 0;
239 
240  *poutbuf = buf;
241  *poutbuf_size = buf_size;
242  return next;
243 }
244 
246  .codec_ids = { AV_CODEC_ID_DTS },
247  .priv_data_size = sizeof(DCAParseContext),
248  .parser_init = dca_parse_init,
249  .parser_parse = dca_parse,
250  .parser_close = ff_parse_close,
251 };
static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration, int *sample_rate)
Definition: dca_parser.c:183
#define NULL
Definition: coverity.c:32
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition: dca_parser.c:175
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ParseContext pc
Definition: dca_parser.c:31
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
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:62
int codec_ids[5]
Definition: avcodec.h:5075
int duration
Duration of the current frame.
Definition: avcodec.h:5029
int frame_start_found
Definition: parser.h:34
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define IS_EXSS_MARKER(state)
Definition: dca_parser.c:43
uint8_t
#define av_cold
Definition: attributes.h:82
#define DCA_SYNCWORD_CORE_14B_BE
Definition: dca_syncwords.h:24
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
AVCodecParser ff_dca_parser
Definition: dca_parser.c:245
#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:245
#define IS_MARKER(state)
Definition: dca_parser.c:45
#define STATE_14(state)
Definition: dca_parser.c:51
#define DCA_SYNCWORD_CORE_14B_LE
Definition: dca_syncwords.h:25
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:321
static struct @222 state
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
int64_t duration
Definition: movenc-test.c:63
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:39
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:34
#define EXSS_MARKER(state)
Definition: dca_parser.c:48
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:212
sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2403
#define DCA_SYNCWORD_CORE_LE
Definition: dca_syncwords.h:23
main external API structure.
Definition: avcodec.h:1642
#define CORE_FRAMESIZE(state)
Definition: dca_parser.c:53
void * buf
Definition: avisynth_c.h:553
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:305
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:418
#define CORE_MARKER(state)
Definition: dca_parser.c:47
#define END_NOT_FOUND
Definition: parser.h:40
#define EXSS_FRAMESIZE(state)
Definition: dca_parser.c:54
uint32_t lastmarker
Definition: dca_parser.c:32
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4941
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:728
#define DCA_SYNCWORD_SUBSTREAM
Definition: dca_syncwords.h:32
#define STATE_LE(state)
Definition: dca_parser.c:50