Libav
|
00001 /* 00002 * Raw FLAC demuxer 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavcodec/flac.h" 00023 #include "avformat.h" 00024 #include "raw.h" 00025 #include "id3v2.h" 00026 #include "oggdec.h" 00027 #include "vorbiscomment.h" 00028 00029 static int flac_read_header(AVFormatContext *s, 00030 AVFormatParameters *ap) 00031 { 00032 uint8_t buf[ID3v2_HEADER_SIZE]; 00033 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0; 00034 uint8_t header[4]; 00035 uint8_t *buffer=NULL; 00036 AVStream *st = av_new_stream(s, 0); 00037 if (!st) 00038 return AVERROR(ENOMEM); 00039 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00040 st->codec->codec_id = CODEC_ID_FLAC; 00041 st->need_parsing = AVSTREAM_PARSE_FULL; 00042 /* the parameters will be extracted from the compressed bitstream */ 00043 00044 /* skip ID3v2 header if found */ 00045 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE); 00046 if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf)) { 00047 int len = ff_id3v2_tag_len(buf); 00048 url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR); 00049 } else { 00050 url_fseek(s->pb, 0, SEEK_SET); 00051 } 00052 00053 /* if fLaC marker is not found, assume there is no header */ 00054 if (get_le32(s->pb) != MKTAG('f','L','a','C')) { 00055 url_fseek(s->pb, -4, SEEK_CUR); 00056 return 0; 00057 } 00058 00059 /* process metadata blocks */ 00060 while (!url_feof(s->pb) && !metadata_last) { 00061 get_buffer(s->pb, header, 4); 00062 ff_flac_parse_block_header(header, &metadata_last, &metadata_type, 00063 &metadata_size); 00064 switch (metadata_type) { 00065 /* allocate and read metadata block for supported types */ 00066 case FLAC_METADATA_TYPE_STREAMINFO: 00067 case FLAC_METADATA_TYPE_VORBIS_COMMENT: 00068 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE); 00069 if (!buffer) { 00070 return AVERROR(ENOMEM); 00071 } 00072 if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) { 00073 av_freep(&buffer); 00074 return AVERROR(EIO); 00075 } 00076 break; 00077 /* skip metadata block for unsupported types */ 00078 default: 00079 ret = url_fseek(s->pb, metadata_size, SEEK_CUR); 00080 if (ret < 0) 00081 return ret; 00082 } 00083 00084 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) { 00085 FLACStreaminfo si; 00086 /* STREAMINFO can only occur once */ 00087 if (found_streaminfo) { 00088 av_freep(&buffer); 00089 return AVERROR_INVALIDDATA; 00090 } 00091 if (metadata_size != FLAC_STREAMINFO_SIZE) { 00092 av_freep(&buffer); 00093 return AVERROR_INVALIDDATA; 00094 } 00095 found_streaminfo = 1; 00096 st->codec->extradata = buffer; 00097 st->codec->extradata_size = metadata_size; 00098 buffer = NULL; 00099 00100 /* get codec params from STREAMINFO header */ 00101 ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata); 00102 00103 /* set time base and duration */ 00104 if (si.samplerate > 0) { 00105 av_set_pts_info(st, 64, 1, si.samplerate); 00106 if (si.samples > 0) 00107 st->duration = si.samples; 00108 } 00109 } else { 00110 /* STREAMINFO must be the first block */ 00111 if (!found_streaminfo) { 00112 av_freep(&buffer); 00113 return AVERROR_INVALIDDATA; 00114 } 00115 /* process supported blocks other than STREAMINFO */ 00116 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) { 00117 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) { 00118 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n"); 00119 } 00120 } 00121 av_freep(&buffer); 00122 } 00123 } 00124 00125 return 0; 00126 } 00127 00128 static int flac_probe(AVProbeData *p) 00129 { 00130 uint8_t *bufptr = p->buf; 00131 uint8_t *end = p->buf + p->buf_size; 00132 00133 if(ff_id3v2_match(bufptr)) 00134 bufptr += ff_id3v2_tag_len(bufptr); 00135 00136 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0; 00137 else return AVPROBE_SCORE_MAX/2; 00138 } 00139 00140 AVInputFormat flac_demuxer = { 00141 "flac", 00142 NULL_IF_CONFIG_SMALL("raw FLAC"), 00143 0, 00144 flac_probe, 00145 flac_read_header, 00146 ff_raw_read_partial_packet, 00147 .flags= AVFMT_GENERIC_INDEX, 00148 .extensions = "flac", 00149 .value = CODEC_ID_FLAC, 00150 .metadata_conv = ff_vorbiscomment_metadata_conv, 00151 };