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 AVFormatParameters *ap)
00049 {
00050 AVIOContext *pb = s->pb;
00051 unsigned header_size, comment_size;
00052 double sample_rate, sample_rate_frac;
00053 AVStream *st;
00054
00055 st = avformat_new_stream(s, NULL);
00056 if (!st)
00057 return AVERROR(ENOMEM);
00058
00059 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00060
00061 if (avio_rl32(pb) == SOX_TAG) {
00062 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00063 header_size = avio_rl32(pb);
00064 avio_skip(pb, 8);
00065 sample_rate = av_int2double(avio_rl64(pb));
00066 st->codec->channels = avio_rl32(pb);
00067 comment_size = avio_rl32(pb);
00068 } else {
00069 st->codec->codec_id = CODEC_ID_PCM_S32BE;
00070 header_size = avio_rb32(pb);
00071 avio_skip(pb, 8);
00072 sample_rate = av_int2double(avio_rb64(pb));
00073 st->codec->channels = avio_rb32(pb);
00074 comment_size = avio_rb32(pb);
00075 }
00076
00077 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) {
00078 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size);
00079 return -1;
00080 }
00081
00082 if (sample_rate <= 0 || sample_rate > INT_MAX) {
00083 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate);
00084 return -1;
00085 }
00086
00087 sample_rate_frac = sample_rate - floor(sample_rate);
00088 if (sample_rate_frac)
00089 av_log(s, AV_LOG_WARNING,
00090 "truncating fractional part of sample rate (%f)\n",
00091 sample_rate_frac);
00092
00093 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size
00094 || st->codec->channels > 65535) {
00095 av_log(s, AV_LOG_ERROR, "invalid header\n");
00096 return -1;
00097 }
00098
00099 if (comment_size && comment_size < UINT_MAX) {
00100 char *comment = av_malloc(comment_size+1);
00101 if(!comment)
00102 return AVERROR(ENOMEM);
00103 if (avio_read(pb, comment, comment_size) != comment_size) {
00104 av_freep(&comment);
00105 return AVERROR(EIO);
00106 }
00107 comment[comment_size] = 0;
00108
00109 av_dict_set(&s->metadata, "comment", comment,
00110 AV_DICT_DONT_STRDUP_VAL);
00111 }
00112
00113 avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size);
00114
00115 st->codec->sample_rate = sample_rate;
00116 st->codec->bits_per_coded_sample = 32;
00117 st->codec->bit_rate = st->codec->sample_rate *
00118 st->codec->bits_per_coded_sample *
00119 st->codec->channels;
00120 st->codec->block_align = st->codec->bits_per_coded_sample *
00121 st->codec->channels / 8;
00122
00123 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00124
00125 return 0;
00126 }
00127
00128 #define SOX_SAMPLES 1024
00129
00130 static int sox_read_packet(AVFormatContext *s,
00131 AVPacket *pkt)
00132 {
00133 int ret, size;
00134
00135 if (url_feof(s->pb))
00136 return AVERROR_EOF;
00137
00138 size = SOX_SAMPLES*s->streams[0]->codec->block_align;
00139 ret = av_get_packet(s->pb, pkt, size);
00140 if (ret < 0)
00141 return AVERROR(EIO);
00142 pkt->stream_index = 0;
00143 pkt->size = ret;
00144
00145 return 0;
00146 }
00147
00148 AVInputFormat ff_sox_demuxer = {
00149 .name = "sox",
00150 .long_name = NULL_IF_CONFIG_SMALL("SoX native format"),
00151 .read_probe = sox_probe,
00152 .read_header = sox_read_header,
00153 .read_packet = sox_read_packet,
00154 .read_seek = pcm_read_seek,
00155 };