]> git.sesse.net Git - ffmpeg/blob - libavcodec/flac.c
vaapi_h265: Add support for AUD NAL units
[ffmpeg] / libavcodec / flac.c
1 /*
2  * FLAC common code
3  * Copyright (c) 2009 Justin Ruggles
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/log.h"
25
26 #include "bitstream.h"
27 #include "bytestream.h"
28 #include "flac.h"
29 #include "flacdata.h"
30
31 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
32
33 static const uint64_t flac_channel_layouts[8] = {
34     AV_CH_LAYOUT_MONO,
35     AV_CH_LAYOUT_STEREO,
36     AV_CH_LAYOUT_SURROUND,
37     AV_CH_LAYOUT_QUAD,
38     AV_CH_LAYOUT_5POINT0,
39     AV_CH_LAYOUT_5POINT1,
40     AV_CH_LAYOUT_6POINT1,
41     AV_CH_LAYOUT_7POINT1
42 };
43
44 static int64_t get_utf8(BitstreamContext *bc)
45 {
46     int64_t val;
47     GET_UTF8(val, bitstream_read(bc, 8), return -1;)
48     return val;
49 }
50
51 int ff_flac_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
52                                 FLACFrameInfo *fi, int log_level_offset)
53 {
54     int bs_code, sr_code, bps_code;
55
56     /* frame sync code */
57     if ((bitstream_read(bc, 15) & 0x7FFF) != 0x7FFC) {
58         av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
59         return AVERROR_INVALIDDATA;
60     }
61
62     /* variable block size stream code */
63     fi->is_var_size = bitstream_read_bit(bc);
64
65     /* block size and sample rate codes */
66     bs_code = bitstream_read(bc, 4);
67     sr_code = bitstream_read(bc, 4);
68
69     /* channels and decorrelation */
70     fi->ch_mode = bitstream_read(bc, 4);
71     if (fi->ch_mode < FLAC_MAX_CHANNELS) {
72         fi->channels = fi->ch_mode + 1;
73         fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
74     } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
75         fi->channels = 2;
76         fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
77     } else {
78         av_log(avctx, AV_LOG_ERROR + log_level_offset,
79                "invalid channel mode: %d\n", fi->ch_mode);
80         return AVERROR_INVALIDDATA;
81     }
82
83     /* bits per sample */
84     bps_code = bitstream_read(bc, 3);
85     if (bps_code == 3 || bps_code == 7) {
86         av_log(avctx, AV_LOG_ERROR + log_level_offset,
87                "invalid sample size code (%d)\n",
88                bps_code);
89         return AVERROR_INVALIDDATA;
90     }
91     fi->bps = sample_size_table[bps_code];
92
93     /* reserved bit */
94     if (bitstream_read_bit(bc)) {
95         av_log(avctx, AV_LOG_ERROR + log_level_offset,
96                "broken stream, invalid padding\n");
97         return AVERROR_INVALIDDATA;
98     }
99
100     /* sample or frame count */
101     fi->frame_or_sample_num = get_utf8(bc);
102     if (fi->frame_or_sample_num < 0) {
103         av_log(avctx, AV_LOG_ERROR + log_level_offset,
104                "sample/frame number invalid; utf8 fscked\n");
105         return AVERROR_INVALIDDATA;
106     }
107
108     /* blocksize */
109     if (bs_code == 0) {
110         av_log(avctx, AV_LOG_ERROR + log_level_offset,
111                "reserved blocksize code: 0\n");
112         return AVERROR_INVALIDDATA;
113     } else if (bs_code == 6) {
114         fi->blocksize = bitstream_read(bc, 8) + 1;
115     } else if (bs_code == 7) {
116         fi->blocksize = bitstream_read(bc, 16) + 1;
117     } else {
118         fi->blocksize = ff_flac_blocksize_table[bs_code];
119     }
120
121     /* sample rate */
122     if (sr_code < 12) {
123         fi->samplerate = ff_flac_sample_rate_table[sr_code];
124     } else if (sr_code == 12) {
125         fi->samplerate = bitstream_read(bc, 8) * 1000;
126     } else if (sr_code == 13) {
127         fi->samplerate = bitstream_read(bc, 16);
128     } else if (sr_code == 14) {
129         fi->samplerate = bitstream_read(bc, 16) * 10;
130     } else {
131         av_log(avctx, AV_LOG_ERROR + log_level_offset,
132                "illegal sample rate code %d\n",
133                sr_code);
134         return AVERROR_INVALIDDATA;
135     }
136
137     /* header CRC-8 check */
138     bitstream_skip(bc, 8);
139     if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, bc->buffer,
140                bitstream_tell(bc) / 8)) {
141         av_log(avctx, AV_LOG_ERROR + log_level_offset,
142                "header crc mismatch\n");
143         return AVERROR_INVALIDDATA;
144     }
145
146     return 0;
147 }
148
149 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
150 {
151     /* Technically, there is no limit to FLAC frame size, but an encoder
152        should not write a frame that is larger than if verbatim encoding mode
153        were to be used. */
154
155     int count;
156
157     count = 16;                  /* frame header */
158     count += ch * ((7+bps+7)/8); /* subframe headers */
159     if (ch == 2) {
160         /* for stereo, need to account for using decorrelation */
161         count += (( 2*bps+1) * blocksize + 7) / 8;
162     } else {
163         count += ( ch*bps    * blocksize + 7) / 8;
164     }
165     count += 2; /* frame footer */
166
167     return count;
168 }
169
170 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
171                                enum FLACExtradataFormat *format,
172                                uint8_t **streaminfo_start)
173 {
174     if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
175         av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
176         return 0;
177     }
178     if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
179         /* extradata contains STREAMINFO only */
180         if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
181             av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
182                    FLAC_STREAMINFO_SIZE-avctx->extradata_size);
183         }
184         *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
185         *streaminfo_start = avctx->extradata;
186     } else {
187         if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
188             av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
189             return 0;
190         }
191         *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
192         *streaminfo_start = &avctx->extradata[8];
193     }
194     return 1;
195 }
196
197 void ff_flac_set_channel_layout(AVCodecContext *avctx)
198 {
199     if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
200         avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
201     else
202         avctx->channel_layout = 0;
203 }
204
205 void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
206                               const uint8_t *buffer)
207 {
208     BitstreamContext bc;
209     bitstream_init8(&bc, buffer, FLAC_STREAMINFO_SIZE);
210
211     bitstream_skip(&bc, 16); /* skip min blocksize */
212     s->max_blocksize = bitstream_read(&bc, 16);
213     if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
214         av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
215                s->max_blocksize);
216         s->max_blocksize = 16;
217     }
218
219     bitstream_skip(&bc, 24); /* skip min frame size */
220     s->max_framesize = bitstream_read(&bc, 24);
221     s->samplerate    = bitstream_read(&bc, 20);
222     s->channels      = bitstream_read(&bc, 3) + 1;
223     s->bps           = bitstream_read(&bc, 5) + 1;
224
225     avctx->channels = s->channels;
226     avctx->sample_rate = s->samplerate;
227     avctx->bits_per_raw_sample = s->bps;
228
229     if (!avctx->channel_layout ||
230         av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
231         ff_flac_set_channel_layout(avctx);
232
233     s->samples  = bitstream_read(&bc, 32) << 4;
234     s->samples |= bitstream_read(&bc, 4);
235
236     bitstream_skip(&bc, 64); /* md5 sum */
237     bitstream_skip(&bc, 64); /* md5 sum */
238 }
239
240 #if LIBAVCODEC_VERSION_MAJOR < 57
241 void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
242                               const uint8_t *buffer)
243 {
244     ff_flac_parse_streaminfo(avctx, s, buffer);
245 }
246
247 int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
248                                enum FLACExtradataFormat *format,
249                                uint8_t **streaminfo_start)
250 {
251     return ff_flac_is_extradata_valid(avctx, format, streaminfo_start);
252 }
253 #endif