FFmpeg
 All Data Structures 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 "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 || state == DCA_HD_MARKER)
43 
44 /**
45  * Find the end of the current frame in the bitstream.
46  * @return the position of the first byte of the next frame, or -1
47  */
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 || pc1->lastmarker == DCA_HD_MARKER) {
64  start_found = 1;
65  pc1->lastmarker = state;
66  break;
67  }
68  }
69  }
70  }
71  if (start_found) {
72  for (; i < buf_size; i++) {
73  pc1->size++;
74  state = (state << 8) | buf[i];
75  if (state == DCA_HD_MARKER && !pc1->hd_pos)
76  pc1->hd_pos = pc1->size;
77  if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
78  if(pc1->framesize > pc1->size)
79  continue;
80  // We have to check that we really read a full frame here, and that it isn't a pure HD frame, because their size is not constant.
81  if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
82  pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
83  }
84  pc->frame_start_found = 0;
85  pc->state = -1;
86  pc1->size = 0;
87  return i - 3;
88  }
89  }
90  }
91  pc->frame_start_found = start_found;
92  pc->state = state;
93  return END_NOT_FOUND;
94 }
95 
97 {
98  DCAParseContext *pc1 = s->priv_data;
99 
100  pc1->lastmarker = 0;
101  return 0;
102 }
103 
104 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
105  int max_size)
106 {
107  uint32_t mrk;
108  int i, tmp;
109  const uint16_t *ssrc = (const uint16_t *) src;
110  uint16_t *sdst = (uint16_t *) dst;
111  PutBitContext pb;
112 
113  if ((unsigned) src_size > (unsigned) max_size)
114  src_size = max_size;
115 
116  mrk = AV_RB32(src);
117  switch (mrk) {
118  case DCA_MARKER_RAW_BE:
119  memcpy(dst, src, src_size);
120  return src_size;
121  case DCA_MARKER_RAW_LE:
122  for (i = 0; i < (src_size + 1) >> 1; i++)
123  *sdst++ = av_bswap16(*ssrc++);
124  return src_size;
125  case DCA_MARKER_14B_BE:
126  case DCA_MARKER_14B_LE:
127  init_put_bits(&pb, dst, max_size);
128  for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
129  tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
130  put_bits(&pb, 14, tmp);
131  }
132  flush_put_bits(&pb);
133  return (put_bits_count(&pb) + 7) >> 3;
134  default:
135  return AVERROR_INVALIDDATA;
136  }
137 }
138 
139 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
140  int *sample_rate)
141 {
142  GetBitContext gb;
143  uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
144  int ret, sample_blocks, sr_code;
145 
146  if (buf_size < 12)
147  return AVERROR_INVALIDDATA;
148 
149  if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
150  return ret;
151 
152  init_get_bits(&gb, hdr, 96);
153 
154  skip_bits_long(&gb, 39);
155  sample_blocks = get_bits(&gb, 7) + 1;
156  if (sample_blocks < 8)
157  return AVERROR_INVALIDDATA;
158  *duration = 256 * (sample_blocks / 8);
159 
160  skip_bits(&gb, 20);
161  sr_code = get_bits(&gb, 4);
162  *sample_rate = avpriv_dca_sample_rates[sr_code];
163  if (*sample_rate == 0)
164  return AVERROR_INVALIDDATA;
165 
166  return 0;
167 }
168 
170  AVCodecContext * avctx,
171  const uint8_t ** poutbuf, int *poutbuf_size,
172  const uint8_t * buf, int buf_size)
173 {
174  DCAParseContext *pc1 = s->priv_data;
175  ParseContext *pc = &pc1->pc;
176  int next, duration, sample_rate;
177 
179  next = buf_size;
180  } else {
181  next = dca_find_frame_end(pc1, buf, buf_size);
182 
183  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
184  *poutbuf = NULL;
185  *poutbuf_size = 0;
186  return buf_size;
187  }
188  }
189 
190  /* read the duration and sample rate from the frame header */
191  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
192  s->duration = duration;
193  avctx->sample_rate = sample_rate;
194  } else
195  s->duration = 0;
196 
197  *poutbuf = buf;
198  *poutbuf_size = buf_size;
199  return next;
200 }
201 
203  .codec_ids = { AV_CODEC_ID_DTS },
204  .priv_data_size = sizeof(DCAParseContext),
205  .parser_init = dca_parse_init,
206  .parser_parse = dca_parse,
207  .parser_close = ff_parse_close,
208 };