FFmpeg
Loading...
Searching...
No Matches
soxdec.c
Go to the documentation of this file.
1/*
2 * SoX native format demuxer
3 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
4 *
5 * Based on libSoX sox-fmt.c
6 * Copyright (c) 2008 robs@users.sourceforge.net
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25/**
26 * @file
27 * SoX native format demuxer
28 * @author Daniel Verkamp
29 * @see http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
30 */
31
33#include "libavutil/intfloat.h"
34#include "libavutil/dict.h"
35#include "libavutil/mem.h"
36#include "avformat.h"
37#include "avio_internal.h"
38#include "demux.h"
39#include "internal.h"
40#include "pcm.h"
41#include "sox.h"
42
43static int sox_probe(const AVProbeData *p)
44{
45 if (AV_RL32(p->buf) != SOX_TAG && AV_RB32(p->buf) != SOX_TAG)
46 return 0;
47 if (AV_RN32(p->buf+4) == 0)
48 return 0;
49 if (AV_RN32(p->buf+24) == 0)
50 return 0;
51 return AVPROBE_SCORE_MAX;
52}
53
55{
56 AVIOContext *pb = s->pb;
57 unsigned header_size, comment_size;
58 double sample_rate, sample_rate_frac;
59 int channels;
60 AVStream *st;
61
63 if (!st)
64 return AVERROR(ENOMEM);
65
67
68 if (avio_rl32(pb) == SOX_TAG) {
70 header_size = avio_rl32(pb);
71 avio_skip(pb, 8); /* sample count */
72 sample_rate = av_int2double(avio_rl64(pb));
73 channels = avio_rl32(pb);
74 comment_size = avio_rl32(pb);
75 } else {
77 header_size = avio_rb32(pb);
78 avio_skip(pb, 8); /* sample count */
79 sample_rate = av_int2double(avio_rb64(pb));
80 channels = avio_rb32(pb);
81 comment_size = avio_rb32(pb);
82 }
83
85
86 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) {
87 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size);
89 }
90
91 if (sample_rate <= 0 || sample_rate > INT_MAX || isnan(sample_rate)) {
92 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate);
94 }
95
96 sample_rate_frac = sample_rate - floor(sample_rate);
97 if (sample_rate_frac)
99 "truncating fractional part of sample rate (%f)\n",
100 sample_rate_frac);
101
102 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size
103 || channels > 65535 || channels <= 0) /* Reserve top 16 bits */ {
104 av_log(s, AV_LOG_ERROR, "invalid header\n");
105 return AVERROR_INVALIDDATA;
106 }
107
108 if (comment_size && comment_size < UINT_MAX) {
109 char *comment = av_malloc(comment_size+1);
110 int ret;
111 if(!comment)
112 return AVERROR(ENOMEM);
113 if ((ret = ffio_read_size(pb, comment, comment_size)) < 0) {
115 return ret;
116 }
117 comment[comment_size] = 0;
118
119 av_dict_set(&s->metadata, "comment", comment,
121 }
122
123 avio_skip(pb, header_size - SOX_FIXED_HDR - comment_size);
124
125 st->codecpar->sample_rate = sample_rate;
129 channels;
131 channels / 8;
132
134
135 return 0;
136}
137
139 .p.name = "sox",
140 .p.long_name = NULL_IF_CONFIG_SMALL("SoX (Sound eXchange) native"),
141 .read_probe = sox_probe,
142 .read_header = sox_read_header,
143 .read_packet = ff_pcm_read_packet,
144 .read_seek = ff_pcm_read_seek,
145};
const FFInputFormat ff_sox_demuxer
Definition soxdec.c:138
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
uint64_t avio_rb64(AVIOContext *s)
Definition aviobuf.c:911
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int FUNC comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float floor(float a)
Public dictionary API.
@ AV_CODEC_ID_PCM_S32LE
Definition codec_id.h:338
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:339
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition intfloat.h:60
#define AV_RN32(p)
#define AV_RL32(p)
#define AV_RB32(p)
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition pcm.c:57
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition pcm.c:73
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define isnan(x)
Definition libm.h:342
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
#define SOX_TAG
Definition sox.h:27
#define SOX_FIXED_HDR
Size of fixed header without magic.
Definition sox.h:25
static int sox_read_header(AVFormatContext *s)
Definition soxdec.c:54
static int sox_probe(const AVProbeData *p)
Definition soxdec.c:43
int nb_channels
Number of channels in this layout.
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_freep(p)
#define av_log(a,...)