FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
flac.c
Go to the documentation of this file.
1 /*
2  * FLAC common code
3  * Copyright (c) 2009 Justin Ruggles
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
23 #include "libavutil/crc.h"
24 #include "libavutil/log.h"
25 #include "bytestream.h"
26 #include "get_bits.h"
27 #include "flac.h"
28 #include "flacdata.h"
29 
30 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
31 
32 static const int64_t flac_channel_layouts[6] = {
39 };
40 
41 static int64_t get_utf8(GetBitContext *gb)
42 {
43  int64_t val;
44  GET_UTF8(val, get_bits(gb, 8), return -1;)
45  return val;
46 }
47 
49  FLACFrameInfo *fi, int log_level_offset)
50 {
51  int bs_code, sr_code, bps_code;
52 
53  /* frame sync code */
54  if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
55  av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
56  return -1;
57  }
58 
59  /* variable block size stream code */
60  fi->is_var_size = get_bits1(gb);
61 
62  /* block size and sample rate codes */
63  bs_code = get_bits(gb, 4);
64  sr_code = get_bits(gb, 4);
65 
66  /* channels and decorrelation */
67  fi->ch_mode = get_bits(gb, 4);
68  if (fi->ch_mode < FLAC_MAX_CHANNELS) {
69  fi->channels = fi->ch_mode + 1;
71  } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
72  fi->channels = 2;
73  fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
74  } else {
75  av_log(avctx, AV_LOG_ERROR + log_level_offset,
76  "invalid channel mode: %d\n", fi->ch_mode);
77  return -1;
78  }
79 
80  /* bits per sample */
81  bps_code = get_bits(gb, 3);
82  if (bps_code == 3 || bps_code == 7) {
83  av_log(avctx, AV_LOG_ERROR + log_level_offset,
84  "invalid sample size code (%d)\n",
85  bps_code);
86  return -1;
87  }
88  fi->bps = sample_size_table[bps_code];
89 
90  /* reserved bit */
91  if (get_bits1(gb)) {
92  av_log(avctx, AV_LOG_ERROR + log_level_offset,
93  "broken stream, invalid padding\n");
94  return -1;
95  }
96 
97  /* sample or frame count */
98  fi->frame_or_sample_num = get_utf8(gb);
99  if (fi->frame_or_sample_num < 0) {
100  av_log(avctx, AV_LOG_ERROR + log_level_offset,
101  "sample/frame number invalid; utf8 fscked\n");
102  return -1;
103  }
104 
105  /* blocksize */
106  if (bs_code == 0) {
107  av_log(avctx, AV_LOG_ERROR + log_level_offset,
108  "reserved blocksize code: 0\n");
109  return -1;
110  } else if (bs_code == 6) {
111  fi->blocksize = get_bits(gb, 8) + 1;
112  } else if (bs_code == 7) {
113  fi->blocksize = get_bits(gb, 16) + 1;
114  } else {
115  fi->blocksize = ff_flac_blocksize_table[bs_code];
116  }
117 
118  /* sample rate */
119  if (sr_code < 12) {
120  fi->samplerate = ff_flac_sample_rate_table[sr_code];
121  } else if (sr_code == 12) {
122  fi->samplerate = get_bits(gb, 8) * 1000;
123  } else if (sr_code == 13) {
124  fi->samplerate = get_bits(gb, 16);
125  } else if (sr_code == 14) {
126  fi->samplerate = get_bits(gb, 16) * 10;
127  } else {
128  av_log(avctx, AV_LOG_ERROR + log_level_offset,
129  "illegal sample rate code %d\n",
130  sr_code);
131  return -1;
132  }
133 
134  /* header CRC-8 check */
135  skip_bits(gb, 8);
137  get_bits_count(gb)/8)) {
138  av_log(avctx, AV_LOG_ERROR + log_level_offset,
139  "header crc mismatch\n");
140  return -1;
141  }
142 
143  return 0;
144 }
145 
146 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
147 {
148  /* Technically, there is no limit to FLAC frame size, but an encoder
149  should not write a frame that is larger than if verbatim encoding mode
150  were to be used. */
151 
152  int count;
153 
154  count = 16; /* frame header */
155  count += ch * ((7+bps+7)/8); /* subframe headers */
156  if (ch == 2) {
157  /* for stereo, need to account for using decorrelation */
158  count += (( 2*bps+1) * blocksize + 7) / 8;
159  } else {
160  count += ( ch*bps * blocksize + 7) / 8;
161  }
162  count += 2; /* frame footer */
163 
164  return count;
165 }
166 
168  enum FLACExtradataFormat *format,
169  uint8_t **streaminfo_start)
170 {
171  if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
172  av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
173  return 0;
174  }
175  if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
176  /* extradata contains STREAMINFO only */
177  if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
178  av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
180  }
182  *streaminfo_start = avctx->extradata;
183  } else {
184  if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
185  av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
186  return 0;
187  }
189  *streaminfo_start = &avctx->extradata[8];
190  }
191  return 1;
192 }
193 
195 {
197  avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
198  else
199  avctx->channel_layout = 0;
200 }
201 
203  const uint8_t *buffer)
204 {
205  GetBitContext gb;
206  init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
207 
208  skip_bits(&gb, 16); /* skip min blocksize */
209  s->max_blocksize = get_bits(&gb, 16);
210  if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
211  av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
212  s->max_blocksize);
213  s->max_blocksize = 16;
214  }
215 
216  skip_bits(&gb, 24); /* skip min frame size */
217  s->max_framesize = get_bits_long(&gb, 24);
218 
219  s->samplerate = get_bits_long(&gb, 20);
220  s->channels = get_bits(&gb, 3) + 1;
221  s->bps = get_bits(&gb, 5) + 1;
222 
223  avctx->channels = s->channels;
224  avctx->sample_rate = s->samplerate;
225  avctx->bits_per_raw_sample = s->bps;
227 
228  s->samples = get_bits64(&gb, 36);
229 
230  skip_bits_long(&gb, 64); /* md5 sum */
231  skip_bits_long(&gb, 64); /* md5 sum */
232 }
233 
234 void avpriv_flac_parse_block_header(const uint8_t *block_header,
235  int *last, int *type, int *size)
236 {
237  int tmp = bytestream_get_byte(&block_header);
238  if (last)
239  *last = tmp & 0x80;
240  if (type)
241  *type = tmp & 0x7F;
242  if (size)
243  *size = bytestream_get_be24(&block_header);
244 }