• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/soxdec.c

Go to the documentation of this file.
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/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); /* sample count */
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); /* sample count */
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) /* Reserve top 16 bits */ {
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 (avio_read(pb, comment, comment_size) != comment_size) {
00102             av_freep(&comment);
00103             return AVERROR(EIO);
00104         }
00105         comment[comment_size] = 0;
00106 
00107         av_dict_set(&s->metadata, "comment", comment,
00108                                AV_DICT_DONT_STRDUP_VAL);
00109     }
00110 
00111     avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size);
00112 
00113     st->codec->sample_rate           = sample_rate;
00114     st->codec->bits_per_coded_sample = 32;
00115     st->codec->bit_rate              = st->codec->sample_rate *
00116                                        st->codec->bits_per_coded_sample *
00117                                        st->codec->channels;
00118     st->codec->block_align           = st->codec->bits_per_coded_sample *
00119                                        st->codec->channels / 8;
00120 
00121     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00122 
00123     return 0;
00124 }
00125 
00126 #define SOX_SAMPLES 1024
00127 
00128 static int sox_read_packet(AVFormatContext *s,
00129                            AVPacket *pkt)
00130 {
00131     int ret, size;
00132 
00133     if (s->pb->eof_reached)
00134         return AVERROR_EOF;
00135 
00136     size = SOX_SAMPLES*s->streams[0]->codec->block_align;
00137     ret = av_get_packet(s->pb, pkt, size);
00138     if (ret < 0)
00139         return AVERROR(EIO);
00140     pkt->stream_index = 0;
00141     pkt->size = ret;
00142 
00143     return 0;
00144 }
00145 
00146 AVInputFormat ff_sox_demuxer = {
00147     .name           = "sox",
00148     .long_name      = NULL_IF_CONFIG_SMALL("SoX native format"),
00149     .read_probe     = sox_probe,
00150     .read_header    = sox_read_header,
00151     .read_packet    = sox_read_packet,
00152     .read_seek      = pcm_read_seek,
00153 };
Generated on Thu Jul 11 2013 15:38:24 for Libav by doxygen 1.7.1