Libav
|
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 FFmpeg. 00009 * 00010 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "avformat.h" 00034 #include "raw.h" 00035 #include "sox.h" 00036 00037 static int sox_probe(AVProbeData *p) 00038 { 00039 if (AV_RL32(p->buf) == SOX_TAG || AV_RB32(p->buf) == SOX_TAG) 00040 return AVPROBE_SCORE_MAX; 00041 return 0; 00042 } 00043 00044 static int sox_read_header(AVFormatContext *s, 00045 AVFormatParameters *ap) 00046 { 00047 ByteIOContext *pb = s->pb; 00048 unsigned header_size, comment_size; 00049 double sample_rate, sample_rate_frac; 00050 AVStream *st; 00051 00052 st = av_new_stream(s, 0); 00053 if (!st) 00054 return AVERROR(ENOMEM); 00055 00056 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00057 00058 if (get_le32(pb) == SOX_TAG) { 00059 st->codec->codec_id = CODEC_ID_PCM_S32LE; 00060 header_size = get_le32(pb); 00061 url_fskip(pb, 8); /* sample count */ 00062 sample_rate = av_int2dbl(get_le64(pb)); 00063 st->codec->channels = get_le32(pb); 00064 comment_size = get_le32(pb); 00065 } else { 00066 st->codec->codec_id = CODEC_ID_PCM_S32BE; 00067 header_size = get_be32(pb); 00068 url_fskip(pb, 8); /* sample count */ 00069 sample_rate = av_int2dbl(get_be64(pb)); 00070 st->codec->channels = get_be32(pb); 00071 comment_size = get_be32(pb); 00072 } 00073 00074 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) { 00075 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size); 00076 return -1; 00077 } 00078 00079 if (sample_rate <= 0 || sample_rate > INT_MAX) { 00080 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate); 00081 return -1; 00082 } 00083 00084 sample_rate_frac = sample_rate - floor(sample_rate); 00085 if (sample_rate_frac) 00086 av_log(s, AV_LOG_WARNING, 00087 "truncating fractional part of sample rate (%f)\n", 00088 sample_rate_frac); 00089 00090 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size 00091 || st->codec->channels > 65535) /* Reserve top 16 bits */ { 00092 av_log(s, AV_LOG_ERROR, "invalid header\n"); 00093 return -1; 00094 } 00095 00096 if (comment_size && comment_size < UINT_MAX) { 00097 char *comment = av_malloc(comment_size+1); 00098 if (get_buffer(pb, comment, comment_size) != comment_size) { 00099 av_freep(&comment); 00100 return AVERROR(EIO); 00101 } 00102 comment[comment_size] = 0; 00103 00104 av_metadata_set2(&s->metadata, "comment", comment, 00105 AV_METADATA_DONT_STRDUP_VAL); 00106 } 00107 00108 url_fskip(pb, header_size - SOX_FIXED_HDR - comment_size); 00109 00110 st->codec->sample_rate = sample_rate; 00111 st->codec->bits_per_coded_sample = 32; 00112 st->codec->bit_rate = st->codec->sample_rate * 00113 st->codec->bits_per_coded_sample * 00114 st->codec->channels; 00115 st->codec->block_align = st->codec->bits_per_coded_sample * 00116 st->codec->channels / 8; 00117 00118 av_set_pts_info(st, 64, 1, st->codec->sample_rate); 00119 00120 return 0; 00121 } 00122 00123 #define SOX_SAMPLES 1024 00124 00125 static int sox_read_packet(AVFormatContext *s, 00126 AVPacket *pkt) 00127 { 00128 int ret, size; 00129 00130 if (url_feof(s->pb)) 00131 return AVERROR_EOF; 00132 00133 size = SOX_SAMPLES*s->streams[0]->codec->block_align; 00134 ret = av_get_packet(s->pb, pkt, size); 00135 if (ret < 0) 00136 return AVERROR(EIO); 00137 pkt->stream_index = 0; 00138 pkt->size = ret; 00139 00140 return 0; 00141 } 00142 00143 AVInputFormat sox_demuxer = { 00144 "sox", 00145 NULL_IF_CONFIG_SMALL("SoX native format"), 00146 0, 00147 sox_probe, 00148 sox_read_header, 00149 sox_read_packet, 00150 NULL, 00151 pcm_read_seek, 00152 };