annotate soxdec.c @ 5927:17226679fd6c libavformat

ETIME -> ETIMEDOUT. Patch by Sam Gerstein <sgerstein bluefinlab com>.
author rbultje
date Fri, 02 Apr 2010 20:14:55 +0000
parents 536e5527c1e0
children 178de7695c6c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
1 /*
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
2 * SoX native format demuxer
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
3 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
4 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
5 * Based on libSoX sox-fmt.c
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
6 * Copyright (c) 2008 robs@users.sourceforge.net
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
7 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
8 * This file is part of FFmpeg.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
9 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
10 * FFmpeg is free software; you can redistribute it and/or
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
11 * modify it under the terms of the GNU Lesser General Public
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
12 * License as published by the Free Software Foundation; either
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
13 * version 2.1 of the License, or (at your option) any later version.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
14 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
15 * FFmpeg is distributed in the hope that it will be useful,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
18 * Lesser General Public License for more details.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
19 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
20 * You should have received a copy of the GNU Lesser General Public
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
21 * License along with FFmpeg; if not, write to the Free Software
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
23 */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
24
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
25 /**
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
26 * SoX native format demuxer
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
27 * @file libavformat/soxdec.c
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
28 * @author Daniel Verkamp
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
29 * @sa http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
30 */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
31
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
32 #include "libavutil/intreadwrite.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
33 #include "avformat.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
34 #include "raw.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
35 #include "sox.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
36
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
37 static int sox_probe(AVProbeData *p)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
38 {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
39 if (AV_RL32(p->buf) == SOX_TAG || AV_RB32(p->buf) == SOX_TAG)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
40 return AVPROBE_SCORE_MAX;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
41 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
42 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
43
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
44 static int sox_read_header(AVFormatContext *s,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
45 AVFormatParameters *ap)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
46 {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
47 ByteIOContext *pb = s->pb;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
48 unsigned header_size, comment_size;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
49 double sample_rate, sample_rate_frac;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
50 AVStream *st;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
51
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
52 st = av_new_stream(s, 0);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
53 if (!st)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
54 return AVERROR(ENOMEM);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
55
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5834
diff changeset
56 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
57
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
58 if (get_le32(pb) == SOX_TAG) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
59 st->codec->codec_id = CODEC_ID_PCM_S32LE;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
60 header_size = get_le32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
61 url_fskip(pb, 8); /* sample count */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
62 sample_rate = av_int2dbl(get_le64(pb));
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
63 st->codec->channels = get_le32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
64 comment_size = get_le32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
65 } else {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
66 st->codec->codec_id = CODEC_ID_PCM_S32BE;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
67 header_size = get_be32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
68 url_fskip(pb, 8); /* sample count */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
69 sample_rate = av_int2dbl(get_be64(pb));
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
70 st->codec->channels = get_be32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
71 comment_size = get_be32(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
72 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
73
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
74 if (comment_size > 0xFFFFFFFFU - SOX_FIXED_HDR - 4U) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
75 av_log(s, AV_LOG_ERROR, "invalid comment size (%u)\n", comment_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
76 return -1;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
77 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
78
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
79 if (sample_rate <= 0 || sample_rate > INT_MAX) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
80 av_log(s, AV_LOG_ERROR, "invalid sample rate (%f)\n", sample_rate);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
81 return -1;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
82 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
83
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
84 sample_rate_frac = sample_rate - floor(sample_rate);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
85 if (sample_rate_frac)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
86 av_log(s, AV_LOG_WARNING,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
87 "truncating fractional part of sample rate (%f)\n",
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
88 sample_rate_frac);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
89
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
90 if ((header_size + 4) & 7 || header_size < SOX_FIXED_HDR + comment_size
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
91 || st->codec->channels > 65535) /* Reserve top 16 bits */ {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
92 av_log(s, AV_LOG_ERROR, "invalid header\n");
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
93 return -1;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
94 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
95
5446
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
96 if (comment_size && comment_size < UINT_MAX) {
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
97 char *comment = av_malloc(comment_size+1);
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
98 if (get_buffer(pb, comment, comment_size) != comment_size) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
99 av_freep(&comment);
5834
134741dc8327 Replace all the occurrences of AVERROR_EIO with AVERROR(EIO), and mark
stefano
parents: 5446
diff changeset
100 return AVERROR(EIO);
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
101 }
5446
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
102 comment[comment_size] = 0;
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
103
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
104 av_metadata_set2(&s->metadata, "comment", comment,
4211f91f69b1 Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
michael
parents: 5355
diff changeset
105 AV_METADATA_DONT_STRDUP_VAL);
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
106 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
107
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
108 url_fskip(pb, header_size - SOX_FIXED_HDR - comment_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
109
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
110 st->codec->sample_rate = sample_rate;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
111 st->codec->bits_per_coded_sample = 32;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
112 st->codec->bit_rate = st->codec->sample_rate *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
113 st->codec->bits_per_coded_sample *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
114 st->codec->channels;
5099
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
115 st->codec->block_align = st->codec->bits_per_coded_sample *
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
116 st->codec->channels / 8;
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
117
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
118 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
119
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
120 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
121 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
122
5099
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
123 #define SOX_SAMPLES 1024
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
124
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
125 static int sox_read_packet(AVFormatContext *s,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
126 AVPacket *pkt)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
127 {
5099
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
128 int ret, size;
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
129
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
130 if (url_feof(s->pb))
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
131 return AVERROR_EOF;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
132
5099
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
133 size = SOX_SAMPLES*s->streams[0]->codec->block_align;
f4119afe7cd9 Add block_align to sox input.
benoit
parents: 4974
diff changeset
134 ret = av_get_packet(s->pb, pkt, size);
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
135 if (ret < 0)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
136 return AVERROR(EIO);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
137 pkt->stream_index = 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
138 pkt->size = ret;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
139
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
140 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
141 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
142
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
143 AVInputFormat sox_demuxer = {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
144 "sox",
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
145 NULL_IF_CONFIG_SMALL("SoX native format"),
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
146 0,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
147 sox_probe,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
148 sox_read_header,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
149 sox_read_packet,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
150 NULL,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
151 pcm_read_seek,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
152 };