Libav 0.7.1
|
00001 /* 00002 * SoX native format demuxer 00003 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu> 00004 * 00005 * Based on libSoX sox-fmt.c 00006 * Copyright (c) 2008 robs@users.sourceforge.net 00007 * 00008 * This file is part of Libav. 00009 * 00010 * Libav is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * Libav is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with Libav; if not, write to the Free Software 00022 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00023 */ 00024 00032 #include "libavutil/intreadwrite.h" 00033 #include "libavutil/dict.h" 00034 #include "avformat.h" 00035 #include "pcm.h" 00036 #include "sox.h" 00037 00038 static int sox_probe(AVProbeData *p) 00039 { 00040 if (AV_RL32(p->buf) == SOX_TAG || AV_RB32(p->buf) == SOX_TAG) 00041 return AVPROBE_SCORE_MAX; 00042 return 0; 00043 } 00044 00045 static int sox_read_header(AVFormatContext *s, 00046 AVFormatParameters *ap) 00047 { 00048 AVIOContext *pb = s->pb; 00049 unsigned header_size, comment_size; 00050 double sample_rate, sample_rate_frac; 00051 AVStream *st; 00052 00053 st = av_new_stream(s, 0); 00054 if (!st) 00055 return AVERROR(ENOMEM); 00056 00057 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00058 00059 if (avio_rl32(pb) == SOX_TAG) { 00060 st->codec->codec_id = CODEC_ID_PCM_S32LE; 00061 header_size = avio_rl32(pb); 00062 avio_skip(pb, 8); /* sample count */ 00063 sample_rate = av_int2dbl(avio_rl64(pb)); 00064 st->codec->channels = avio_rl32(pb); 00065 comment_size = avio_rl32(pb); 00066 } else { 00067 st->codec->codec_id = CODEC_ID_PCM_S32BE; 00068 header_size = avio_rb32(pb); 00069 avio_skip(pb, 8); /* sample count */ 00070 sample_rate = av_int2dbl(avio_rb64(pb)); 00071 st->codec->channels = avio_rb32(pb); 00072 comment_size = avio_rb32(pb); 00073 } 00074 00075 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) { 00076 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size); 00077 return -1; 00078 } 00079 00080 if (sample_rate <= 0 || sample_rate > INT_MAX) { 00081 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate); 00082 return -1; 00083 } 00084 00085 sample_rate_frac = sample_rate - floor(sample_rate); 00086 if (sample_rate_frac) 00087 av_log(s, AV_LOG_WARNING, 00088 "truncating fractional part of sample rate (%f)\n", 00089 sample_rate_frac); 00090 00091 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size 00092 || st->codec->channels > 65535) /* Reserve top 16 bits */ { 00093 av_log(s, AV_LOG_ERROR, "invalid header\n"); 00094 return -1; 00095 } 00096 00097 if (comment_size && comment_size < UINT_MAX) { 00098 char *comment = av_malloc(comment_size+1); 00099 if (avio_read(pb, comment, comment_size) != comment_size) { 00100 av_freep(&comment); 00101 return AVERROR(EIO); 00102 } 00103 comment[comment_size] = 0; 00104 00105 av_dict_set(&s->metadata, "comment", comment, 00106 AV_DICT_DONT_STRDUP_VAL); 00107 } 00108 00109 avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size); 00110 00111 st->codec->sample_rate = sample_rate; 00112 st->codec->bits_per_coded_sample = 32; 00113 st->codec->bit_rate = st->codec->sample_rate * 00114 st->codec->bits_per_coded_sample * 00115 st->codec->channels; 00116 st->codec->block_align = st->codec->bits_per_coded_sample * 00117 st->codec->channels / 8; 00118 00119 av_set_pts_info(st, 64, 1, st->codec->sample_rate); 00120 00121 return 0; 00122 } 00123 00124 #define SOX_SAMPLES 1024 00125 00126 static int sox_read_packet(AVFormatContext *s, 00127 AVPacket *pkt) 00128 { 00129 int ret, size; 00130 00131 if (s->pb->eof_reached) 00132 return AVERROR_EOF; 00133 00134 size = SOX_SAMPLES*s->streams[0]->codec->block_align; 00135 ret = av_get_packet(s->pb, pkt, size); 00136 if (ret < 0) 00137 return AVERROR(EIO); 00138 pkt->stream_index = 0; 00139 pkt->size = ret; 00140 00141 return 0; 00142 } 00143 00144 AVInputFormat ff_sox_demuxer = { 00145 "sox", 00146 NULL_IF_CONFIG_SMALL("SoX native format"), 00147 0, 00148 sox_probe, 00149 sox_read_header, 00150 sox_read_packet, 00151 NULL, 00152 pcm_read_seek, 00153 };