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 "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 || state == DCA_HD_MARKER)
41 
42 /**
43  * Find the end of the current frame in the bitstream.
44  * @return the position of the first byte of the next frame, or -1
45  */
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 || pc1->lastmarker == DCA_HD_MARKER) {
62  start_found = 1;
63  pc1->lastmarker = state;
64  break;
65  }
66  }
67  }
68  }
69  if (start_found) {
70  for (; i < buf_size; i++) {
71  pc1->size++;
72  state = (state << 8) | buf[i];
73  if (state == DCA_HD_MARKER && !pc1->hd_pos)
74  pc1->hd_pos = pc1->size;
75  if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
76  if(pc1->framesize > pc1->size)
77  continue;
78  // 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.
79  if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
80  pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
81  }
82  pc->frame_start_found = 0;
83  pc->state = -1;
84  pc1->size = 0;
85  return i - 3;
86  }
87  }
88  }
89  pc->frame_start_found = start_found;
90  pc->state = state;
91  return END_NOT_FOUND;
92 }
93 
95 {
96  DCAParseContext *pc1 = s->priv_data;
97 
98  pc1->lastmarker = 0;
99  return 0;
100 }
101 
102 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
103  int *sample_rate)
104 {
105  GetBitContext gb;
106  uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
107  int ret, sample_blocks, sr_code;
108 
109  if (buf_size < 12)
110  return AVERROR_INVALIDDATA;
111 
112  if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
113  return ret;
114 
115  init_get_bits(&gb, hdr, 96);
116 
117  skip_bits_long(&gb, 39);
118  sample_blocks = get_bits(&gb, 7) + 1;
119  if (sample_blocks < 8)
120  return AVERROR_INVALIDDATA;
121  *duration = 256 * (sample_blocks / 8);
122 
123  skip_bits(&gb, 20);
124  sr_code = get_bits(&gb, 4);
125  *sample_rate = avpriv_dca_sample_rates[sr_code];
126  if (*sample_rate == 0)
127  return AVERROR_INVALIDDATA;
128 
129  return 0;
130 }
131 
133  AVCodecContext * avctx,
134  const uint8_t ** poutbuf, int *poutbuf_size,
135  const uint8_t * buf, int buf_size)
136 {
137  DCAParseContext *pc1 = s->priv_data;
138  ParseContext *pc = &pc1->pc;
139  int next, duration, sample_rate;
140 
142  next = buf_size;
143  } else {
144  next = dca_find_frame_end(pc1, buf, buf_size);
145 
146  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
147  *poutbuf = NULL;
148  *poutbuf_size = 0;
149  return buf_size;
150  }
151  }
152 
153  /* read the duration and sample rate from the frame header */
154  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
155  s->duration = duration;
156  avctx->sample_rate = sample_rate;
157  } else
158  s->duration = 0;
159 
160  *poutbuf = buf;
161  *poutbuf_size = buf_size;
162  return next;
163 }
164 
166  .codec_ids = { AV_CODEC_ID_DTS },
167  .priv_data_size = sizeof(DCAParseContext),
168  .parser_init = dca_parse_init,
169  .parser_parse = dca_parse,
170  .parser_close = ff_parse_close,
171 };