Libav
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 "get_bits.h"
28 
29 typedef struct DCAParseContext {
31  uint32_t lastmarker;
32  int size;
33  int framesize;
34  int hd_pos;
36 
37 #define IS_MARKER(state, i, buf, buf_size) \
38  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
39  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
40  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
41 
46 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
47  int buf_size)
48 {
49  int start_found, i;
50  uint32_t state;
51  ParseContext *pc = &pc1->pc;
52 
53  start_found = pc->frame_start_found;
54  state = pc->state;
55 
56  i = 0;
57  if (!start_found) {
58  for (i = 0; i < buf_size; i++) {
59  state = (state << 8) | buf[i];
60  if (IS_MARKER(state, i, buf, buf_size)) {
61  if (pc1->lastmarker && state == pc1->lastmarker) {
62  start_found = 1;
63  i++;
64  break;
65  } else if (!pc1->lastmarker) {
66  start_found = 1;
67  pc1->lastmarker = state;
68  i++;
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  pc->frame_start_found = 0;
84  pc->state = -1;
85  pc1->size = 0;
86  return i - 3;
87  }
88  }
89  }
90  pc->frame_start_found = start_found;
91  pc->state = state;
92  return END_NOT_FOUND;
93 }
94 
96 {
97  DCAParseContext *pc1 = s->priv_data;
98 
99  pc1->lastmarker = 0;
100  return 0;
101 }
102 
103 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
104  int *sample_rate, int *framesize)
105 {
106  GetBitContext gb;
107  uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
108  int ret, sample_blocks, sr_code;
109 
110  if (buf_size < 12)
111  return AVERROR_INVALIDDATA;
112 
113  if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
114  return ret;
115 
116  init_get_bits(&gb, hdr, 96);
117 
118  skip_bits_long(&gb, 39);
119  sample_blocks = get_bits(&gb, 7) + 1;
120  if (sample_blocks < 8)
121  return AVERROR_INVALIDDATA;
122  *duration = 256 * (sample_blocks / 8);
123 
124  *framesize = get_bits(&gb, 14) + 1;
125  if (*framesize < 95)
126  return AVERROR_INVALIDDATA;
127 
128  skip_bits(&gb, 6);
129  sr_code = get_bits(&gb, 4);
130  *sample_rate = avpriv_dca_sample_rates[sr_code];
131  if (*sample_rate == 0)
132  return AVERROR_INVALIDDATA;
133 
134  return 0;
135 }
136 
138  AVCodecContext * avctx,
139  const uint8_t ** poutbuf, int *poutbuf_size,
140  const uint8_t * buf, int buf_size)
141 {
142  DCAParseContext *pc1 = s->priv_data;
143  ParseContext *pc = &pc1->pc;
144  int next, duration, sample_rate;
145 
147  next = buf_size;
148  } else {
149  next = dca_find_frame_end(pc1, buf, buf_size);
150 
151  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
152  *poutbuf = NULL;
153  *poutbuf_size = 0;
154  return buf_size;
155  }
156  }
157 
158  /* read the duration and sample rate from the frame header */
159  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
160  s->duration = duration;
161  avctx->sample_rate = sample_rate;
162  } else
163  s->duration = 0;
164 
165  *poutbuf = buf;
166  *poutbuf_size = buf_size;
167  return next;
168 }
169 
171  .codec_ids = { AV_CODEC_ID_DTS },
172  .priv_data_size = sizeof(DCAParseContext),
173  .parser_init = dca_parse_init,
174  .parser_parse = dca_parse,
175  .parser_close = ff_parse_close,
176 };