cbfe5bee |
/*
* SoX native format demuxer
* Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
*
* Based on libSoX sox-fmt.c
* Copyright (c) 2008 robs@users.sourceforge.net
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** |
ba87f080 |
* @file |
f6f95d4e |
* SoX native format demuxer |
cbfe5bee |
* @author Daniel Verkamp |
bee6d2fd |
* @see http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format |
cbfe5bee |
*/
#include "libavutil/intreadwrite.h" |
3383a53e |
#include "libavutil/intfloat.h" |
d2d67e42 |
#include "libavutil/dict.h" |
cbfe5bee |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
e94204df |
#include "pcm.h" |
cbfe5bee |
#include "sox.h"
static int sox_probe(AVProbeData *p)
{
if (AV_RL32(p->buf) == SOX_TAG || AV_RB32(p->buf) == SOX_TAG)
return AVPROBE_SCORE_MAX;
return 0;
}
|
6e9651d1 |
static int sox_read_header(AVFormatContext *s) |
cbfe5bee |
{ |
471fe57e |
AVIOContext *pb = s->pb; |
cbfe5bee |
unsigned header_size, comment_size;
double sample_rate, sample_rate_frac;
AVStream *st;
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
cbfe5bee |
if (!st)
return AVERROR(ENOMEM);
|
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
cbfe5bee |
|
e63a3628 |
if (avio_rl32(pb) == SOX_TAG) { |
9200514a |
st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE; |
e63a3628 |
header_size = avio_rl32(pb); |
45a8a02a |
avio_skip(pb, 8); /* sample count */ |
3383a53e |
sample_rate = av_int2double(avio_rl64(pb)); |
9200514a |
st->codecpar->channels = avio_rl32(pb); |
e63a3628 |
comment_size = avio_rl32(pb); |
cbfe5bee |
} else { |
9200514a |
st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE; |
e63a3628 |
header_size = avio_rb32(pb); |
45a8a02a |
avio_skip(pb, 8); /* sample count */ |
3383a53e |
sample_rate = av_int2double(avio_rb64(pb)); |
9200514a |
st->codecpar->channels = avio_rb32(pb); |
e63a3628 |
comment_size = avio_rb32(pb); |
cbfe5bee |
}
if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) {
av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size); |
a4b62f36 |
return AVERROR_INVALIDDATA; |
cbfe5bee |
}
if (sample_rate <= 0 || sample_rate > INT_MAX) {
av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate); |
a4b62f36 |
return AVERROR_INVALIDDATA; |
cbfe5bee |
}
sample_rate_frac = sample_rate - floor(sample_rate);
if (sample_rate_frac)
av_log(s, AV_LOG_WARNING,
"truncating fractional part of sample rate (%f)\n",
sample_rate_frac);
if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size |
9200514a |
|| st->codecpar->channels > 65535) /* Reserve top 16 bits */ { |
cbfe5bee |
av_log(s, AV_LOG_ERROR, "invalid header\n"); |
a4b62f36 |
return AVERROR_INVALIDDATA; |
cbfe5bee |
}
|
12ad6671 |
if (comment_size && comment_size < UINT_MAX) {
char *comment = av_malloc(comment_size+1); |
1dcce49e |
if(!comment)
return AVERROR(ENOMEM); |
e63a3628 |
if (avio_read(pb, comment, comment_size) != comment_size) { |
cbfe5bee |
av_freep(&comment); |
5ae092ee |
return AVERROR(EIO); |
cbfe5bee |
} |
12ad6671 |
comment[comment_size] = 0;
|
d2d67e42 |
av_dict_set(&s->metadata, "comment", comment,
AV_DICT_DONT_STRDUP_VAL); |
cbfe5bee |
}
|
45a8a02a |
avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size); |
cbfe5bee |
|
9200514a |
st->codecpar->sample_rate = sample_rate;
st->codecpar->bits_per_coded_sample = 32; |
ad5807f8 |
st->codecpar->bit_rate = (int64_t)st->codecpar->sample_rate * |
9200514a |
st->codecpar->bits_per_coded_sample *
st->codecpar->channels;
st->codecpar->block_align = st->codecpar->bits_per_coded_sample *
st->codecpar->channels / 8; |
cbfe5bee |
|
9200514a |
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
cbfe5bee |
return 0;
}
|
66355be3 |
AVInputFormat ff_sox_demuxer = { |
dfc2c4d9 |
.name = "sox", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("SoX native"), |
dfc2c4d9 |
.read_probe = sox_probe,
.read_header = sox_read_header, |
acac16b5 |
.read_packet = ff_pcm_read_packet, |
167f3b8d |
.read_seek = ff_pcm_read_seek, |
cbfe5bee |
}; |