00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/intfloat.h"
00034 #include "libavutil/dict.h"
00035 #include "avformat.h"
00036 #include "internal.h"
00037 #include "pcm.h"
00038 #include "sox.h"
00039
00040 static int sox_probe(AVProbeData *p)
00041 {
00042 if (AV_RL32(p->buf) == SOX_TAG || AV_RB32(p->buf) == SOX_TAG)
00043 return AVPROBE_SCORE_MAX;
00044 return 0;
00045 }
00046
00047 static int sox_read_header(AVFormatContext *s)
00048 {
00049 AVIOContext *pb = s->pb;
00050 unsigned header_size, comment_size;
00051 double sample_rate, sample_rate_frac;
00052 AVStream *st;
00053
00054 st = avformat_new_stream(s, NULL);
00055 if (!st)
00056 return AVERROR(ENOMEM);
00057
00058 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00059
00060 if (avio_rl32(pb) == SOX_TAG) {
00061 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00062 header_size = avio_rl32(pb);
00063 avio_skip(pb, 8);
00064 sample_rate = av_int2double(avio_rl64(pb));
00065 st->codec->channels = avio_rl32(pb);
00066 comment_size = avio_rl32(pb);
00067 } else {
00068 st->codec->codec_id = CODEC_ID_PCM_S32BE;
00069 header_size = avio_rb32(pb);
00070 avio_skip(pb, 8);
00071 sample_rate = av_int2double(avio_rb64(pb));
00072 st->codec->channels = avio_rb32(pb);
00073 comment_size = avio_rb32(pb);
00074 }
00075
00076 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) {
00077 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size);
00078 return -1;
00079 }
00080
00081 if (sample_rate <= 0 || sample_rate > INT_MAX) {
00082 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate);
00083 return -1;
00084 }
00085
00086 sample_rate_frac = sample_rate - floor(sample_rate);
00087 if (sample_rate_frac)
00088 av_log(s, AV_LOG_WARNING,
00089 "truncating fractional part of sample rate (%f)\n",
00090 sample_rate_frac);
00091
00092 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size
00093 || st->codec->channels > 65535) {
00094 av_log(s, AV_LOG_ERROR, "invalid header\n");
00095 return -1;
00096 }
00097
00098 if (comment_size && comment_size < UINT_MAX) {
00099 char *comment = av_malloc(comment_size+1);
00100 if(!comment)
00101 return AVERROR(ENOMEM);
00102 if (avio_read(pb, comment, comment_size) != comment_size) {
00103 av_freep(&comment);
00104 return AVERROR(EIO);
00105 }
00106 comment[comment_size] = 0;
00107
00108 av_dict_set(&s->metadata, "comment", comment,
00109 AV_DICT_DONT_STRDUP_VAL);
00110 }
00111
00112 avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size);
00113
00114 st->codec->sample_rate = sample_rate;
00115 st->codec->bits_per_coded_sample = 32;
00116 st->codec->bit_rate = st->codec->sample_rate *
00117 st->codec->bits_per_coded_sample *
00118 st->codec->channels;
00119 st->codec->block_align = st->codec->bits_per_coded_sample *
00120 st->codec->channels / 8;
00121
00122 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00123
00124 return 0;
00125 }
00126
00127 #define SOX_SAMPLES 1024
00128
00129 static int sox_read_packet(AVFormatContext *s,
00130 AVPacket *pkt)
00131 {
00132 int ret, size;
00133
00134 if (url_feof(s->pb))
00135 return AVERROR_EOF;
00136
00137 size = SOX_SAMPLES*s->streams[0]->codec->block_align;
00138 ret = av_get_packet(s->pb, pkt, size);
00139 if (ret < 0)
00140 return AVERROR(EIO);
00141 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
00142 pkt->stream_index = 0;
00143
00144 return 0;
00145 }
00146
00147 AVInputFormat ff_sox_demuxer = {
00148 .name = "sox",
00149 .long_name = NULL_IF_CONFIG_SMALL("SoX native format"),
00150 .read_probe = sox_probe,
00151 .read_header = sox_read_header,
00152 .read_packet = sox_read_packet,
00153 .read_seek = ff_pcm_read_seek,
00154 };