FFmpeg  2.1.1
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  i++;
65  break;
66  }
67  }
68  }
69  }
70  if (start_found) {
71  for (; i < buf_size; i++) {
72  pc1->size++;
73  state = (state << 8) | buf[i];
74  if (state == DCA_HD_MARKER && !pc1->hd_pos)
75  pc1->hd_pos = pc1->size;
76  if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
77  if(pc1->framesize > pc1->size)
78  continue;
79  pc->frame_start_found = 0;
80  pc->state = -1;
81  pc1->size = 0;
82  return i - 3;
83  }
84  }
85  }
86  pc->frame_start_found = start_found;
87  pc->state = state;
88  return END_NOT_FOUND;
89 }
90 
92 {
93  DCAParseContext *pc1 = s->priv_data;
94 
95  pc1->lastmarker = 0;
96  return 0;
97 }
98 
99 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
100  int *sample_rate, int *framesize)
101 {
102  GetBitContext gb;
103  uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
104  int ret, sample_blocks, sr_code;
105 
106  if (buf_size < 12)
107  return AVERROR_INVALIDDATA;
108 
109  if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
110  return ret;
111 
112  init_get_bits(&gb, hdr, 96);
113 
114  skip_bits_long(&gb, 39);
115  sample_blocks = get_bits(&gb, 7) + 1;
116  if (sample_blocks < 8)
117  return AVERROR_INVALIDDATA;
118  *duration = 256 * (sample_blocks / 8);
119 
120  *framesize = get_bits(&gb, 14) + 1;
121  if (*framesize < 95)
122  return AVERROR_INVALIDDATA;
123 
124  skip_bits(&gb, 6);
125  sr_code = get_bits(&gb, 4);
126  *sample_rate = avpriv_dca_sample_rates[sr_code];
127  if (*sample_rate == 0)
128  return AVERROR_INVALIDDATA;
129 
130  return 0;
131 }
132 
134  AVCodecContext * avctx,
135  const uint8_t ** poutbuf, int *poutbuf_size,
136  const uint8_t * buf, int buf_size)
137 {
138  DCAParseContext *pc1 = s->priv_data;
139  ParseContext *pc = &pc1->pc;
140  int next, duration, sample_rate;
141 
143  next = buf_size;
144  } else {
145  next = dca_find_frame_end(pc1, buf, buf_size);
146 
147  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
148  *poutbuf = NULL;
149  *poutbuf_size = 0;
150  return buf_size;
151  }
152  }
153 
154  /* read the duration and sample rate from the frame header */
155  if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
156  s->duration = duration;
157  avctx->sample_rate = sample_rate;
158  } else
159  s->duration = 0;
160 
161  *poutbuf = buf;
162  *poutbuf_size = buf_size;
163  return next;
164 }
165 
167  .codec_ids = { AV_CODEC_ID_DTS },
168  .priv_data_size = sizeof(DCAParseContext),
169  .parser_init = dca_parse_init,
170  .parser_parse = dca_parse,
171  .parser_close = ff_parse_close,
172 };
static av_cold int dca_parse_init(AVCodecParserContext *s)
Definition: dca_parser.c:91
const char * s
Definition: avisynth_c.h:668
ParseContext pc
Definition: dca_parser.c:30
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:212
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:46
int duration
Duration of the current frame.
Definition: avcodec.h:4060
#define av_cold
Definition: avcodec.h:653
int frame_start_found
Definition: parser.h:34
uint8_t
av_export const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:31
bitstream reader API header.
static int64_t duration
Definition: ffplay.c:306
AVCodecParser ff_dca_parser
Definition: dca_parser.c:166
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:216
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3961
#define IS_MARKER(state, i, buf, buf_size)
Definition: dca_parser.c:37
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:285
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
ret
Definition: avfilter.c:961
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.c:37
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:133
sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1873
int codec_ids[5]
Definition: avcodec.h:4084
main external API structure.
Definition: avcodec.h:1146
void * buf
Definition: avisynth_c.h:594
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:292
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
#define END_NOT_FOUND
Definition: parser.h:40
static uint32_t state
Definition: trasher.c:27
uint32_t lastmarker
Definition: dca_parser.c:31
#define AVERROR_INVALIDDATA
#define DCA_HD_MARKER
DCA-HD specific block starts with this marker.
Definition: dca.h:38
static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration, int *sample_rate, int *framesize)
Definition: dca_parser.c:99