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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "parser.h"
26 #include "dca.h"
27 #include "dca_parser.h"
28 #include "get_bits.h"
29 #include "put_bits.h"
30 
31 typedef struct DCAParseContext {
33  uint32_t lastmarker;
34  int size;
35  int framesize;
36  int hd_pos;
38 
39 #define IS_MARKER(state, i, buf, buf_size) \
40  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
41  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
42  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
43 
48 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
49  int buf_size)
50 {
51  int start_found, i;
52  uint32_t state;
53  ParseContext *pc = &pc1->pc;
54 
55  start_found = pc->frame_start_found;
56  state = pc->state;
57 
58  i = 0;
59  if (!start_found) {
60  for (i = 0; i < buf_size; i++) {
61  state = (state << 8) | buf[i];
62  if (IS_MARKER(state, i, buf, buf_size)) {
63  if (pc1->lastmarker && state == pc1->lastmarker) {
64  start_found = 1;
65  break;
66  } else if (!pc1->lastmarker) {
67  start_found = 1;
68  pc1->lastmarker = state;
69  break;
70  }
71  }
72  }
73  }
74  if (start_found) {
75  for (; i < buf_size; i++) {
76  pc1->size++;
77  state = (state << 8) | buf[i];
78  if (state == DCA_HD_MARKER && !pc1->hd_pos)
79  pc1->hd_pos = pc1->size;
80  if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
81  if(pc1->framesize > pc1->size)
82  continue;
83  if(!pc1->framesize){
84  pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
85  }
86  pc->frame_start_found = 0;
87  pc->state = -1;
88  pc1->size = 0;
89  return i - 3;
90  }
91  }
92  }
93  pc->frame_start_found = start_found;
94  pc->state = state;
95  return END_NOT_FOUND;
96 }
97 
99 {
100  DCAParseContext *pc1 = s->priv_data;
101 
102  pc1->lastmarker = 0;
103  return 0;
104 }
105 
106 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
107  int max_size)
108 {
109  uint32_t mrk;
110  int i, tmp;
111  const uint16_t *ssrc = (const uint16_t *) src;
112  uint16_t *sdst = (uint16_t *) dst;
113  PutBitContext pb;
114 
115  if ((unsigned) src_size > (unsigned) max_size)
116  src_size = max_size;
117 
118  mrk = AV_RB32(src);
119  switch (mrk) {
120  case DCA_MARKER_RAW_BE:
121  memcpy(dst, src, src_size);
122  return src_size;
123  case DCA_MARKER_RAW_LE:
124  for (i = 0; i < (src_size + 1) >> 1; i++)
125  *sdst++ = av_bswap16(*ssrc++);
126  return src_size;
127  case DCA_MARKER_14B_BE:
128  case DCA_MARKER_14B_LE:
129  init_put_bits(&pb, dst, max_size);
130  for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
131  tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
132  put_bits(&pb, 14, tmp);
133  }
134  flush_put_bits(&pb);
135  return (put_bits_count(&pb) + 7) >> 3;
136  default:
137  return AVERROR_INVALIDDATA;
138  }
139 }
140 
141 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
142  int *sample_rate)
143 {
144  GetBitContext gb;
145  uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
146  int ret, sample_blocks, sr_code;
147 
148  if (buf_size < 12)
149  return AVERROR_INVALIDDATA;
150 
151  if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
152  return ret;
153 
154  init_get_bits(&gb, hdr, 96);
155 
156  skip_bits_long(&gb, 39);
157  sample_blocks = get_bits(&gb, 7) + 1;
158  if (sample_blocks < 8)
159  return AVERROR_INVALIDDATA;
160  *duration = 256 * (sample_blocks / 8);
161 
162  skip_bits(&gb, 20);
163  sr_code = get_bits(&gb, 4);
164  *sample_rate = avpriv_dca_sample_rates[sr_code];
165  if (*sample_rate == 0)
166  return AVERROR_INVALIDDATA;
167 
168  return 0;
169 }
170 
172  AVCodecContext * avctx,
173  const uint8_t ** poutbuf, int *poutbuf_size,
174  const uint8_t * buf, int buf_size)
175 {
176  DCAParseContext *pc1 = s->priv_data;
177  ParseContext *pc = &pc1->pc;
178  int next, duration, sample_rate;
179 
181  next = buf_size;
182  } else {
183  next = dca_find_frame_end(pc1, buf, buf_size);
184 
185  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
186  *poutbuf = NULL;
187  *poutbuf_size = 0;
188  return buf_size;
189  }
190  }
191 
192  /* read the duration and sample rate from the frame header */
193  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
194  s->duration = duration;
195  avctx->sample_rate = sample_rate;
196  } else
197  s->duration = 0;
198 
199  *poutbuf = buf;
200  *poutbuf_size = buf_size;
201  return next;
202 }
203 
205  .codec_ids = { AV_CODEC_ID_DTS },
206  .priv_data_size = sizeof(DCAParseContext),
207  .parser_init = dca_parse_init,
208  .parser_parse = dca_parse,
209  .parser_close = ff_parse_close,
210 };
static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration, int *sample_rate)
Definition: dca_parser.c:141
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition: dca_parser.c:98
ParseContext pc
Definition: dca_parser.c:32
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int ff_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_parser.c:106
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
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:48
int codec_ids[5]
Definition: avcodec.h:3855
#define av_bswap16
Definition: bswap.h:31
#define AV_RL16
Definition: intreadwrite.h:42
int duration
Duration of the current frame.
Definition: avcodec.h:3851
int frame_start_found
Definition: parser.h:34
static int64_t duration
Definition: avplay.c:249
#define DCA_HD_MARKER
DCA-HD specific block starts with this marker.
Definition: dca.h:38
uint8_t
#define AV_RB32
Definition: intreadwrite.h:130
bitstream reader API header.
AVCodecParser ff_dca_parser
Definition: dca_parser.c:204
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:215
#define AV_RB16
Definition: intreadwrite.h:53
#define IS_MARKER(state, i, buf, buf_size)
Definition: dca_parser.c:39
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:280
#define DCA_MARKER_14B_LE
Definition: dca.h:35
struct DCAParseContext DCAParseContext
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:25
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:171
NULL
Definition: eval.c:52
#define DCA_MARKER_RAW_LE
Definition: dca.h:33
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
#define END_NOT_FOUND
Definition: parser.h:40
static uint32_t state
Definition: trasher.c:27
#define DCA_MARKER_14B_BE
Definition: dca.h:34
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
uint32_t lastmarker
Definition: dca_parser.c:33
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
#define DCA_MARKER_RAW_BE
DCA syncwords, also used for bitstream type detection.
Definition: dca.h:32
bitstream writer API