rawdec: add sample_rate/channels private options.
[libav.git] / libavformat / rawdec.c
1 /*
2 * RAW demuxers
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2005 Alex Beregszaszi
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "rawdec.h"
26 #include "libavutil/opt.h"
27
28 /* raw input */
29 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
30 {
31 AVStream *st;
32 enum CodecID id;
33
34 st = av_new_stream(s, 0);
35 if (!st)
36 return AVERROR(ENOMEM);
37
38 id = s->iformat->value;
39 if (id == CODEC_ID_RAWVIDEO) {
40 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
41 } else {
42 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
43 }
44 st->codec->codec_id = id;
45
46 switch(st->codec->codec_type) {
47 case AVMEDIA_TYPE_AUDIO: {
48 RawAudioDemuxerContext *s1 = s->priv_data;
49
50 if (ap->sample_rate)
51 st->codec->sample_rate = ap->sample_rate;
52 if (ap->channels)
53 st->codec->channels = ap->channels;
54 else st->codec->channels = 1;
55
56 if (s1->sample_rate)
57 st->codec->sample_rate = s1->sample_rate;
58 if (s1->channels)
59 st->codec->channels = s1->channels;
60
61 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
62 assert(st->codec->bits_per_coded_sample > 0);
63 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
64 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
65 break;
66 }
67 case AVMEDIA_TYPE_VIDEO:
68 if(ap->time_base.num)
69 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
70 else
71 av_set_pts_info(st, 64, 1, 25);
72 st->codec->width = ap->width;
73 st->codec->height = ap->height;
74 st->codec->pix_fmt = ap->pix_fmt;
75 if(st->codec->pix_fmt == PIX_FMT_NONE)
76 st->codec->pix_fmt= PIX_FMT_YUV420P;
77 break;
78 default:
79 return -1;
80 }
81 return 0;
82 }
83
84 #define RAW_PACKET_SIZE 1024
85
86 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
87 {
88 int ret, size;
89
90 size = RAW_PACKET_SIZE;
91
92 if (av_new_packet(pkt, size) < 0)
93 return AVERROR(ENOMEM);
94
95 pkt->pos= avio_tell(s->pb);
96 pkt->stream_index = 0;
97 ret = ffio_read_partial(s->pb, pkt->data, size);
98 if (ret < 0) {
99 av_free_packet(pkt);
100 return ret;
101 }
102 pkt->size = ret;
103 return ret;
104 }
105
106 int ff_raw_audio_read_header(AVFormatContext *s,
107 AVFormatParameters *ap)
108 {
109 AVStream *st = av_new_stream(s, 0);
110 if (!st)
111 return AVERROR(ENOMEM);
112 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
113 st->codec->codec_id = s->iformat->value;
114 st->need_parsing = AVSTREAM_PARSE_FULL;
115 /* the parameters will be extracted from the compressed bitstream */
116
117 return 0;
118 }
119
120 /* MPEG-1/H.263 input */
121 int ff_raw_video_read_header(AVFormatContext *s,
122 AVFormatParameters *ap)
123 {
124 AVStream *st;
125
126 st = av_new_stream(s, 0);
127 if (!st)
128 return AVERROR(ENOMEM);
129
130 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
131 st->codec->codec_id = s->iformat->value;
132 st->need_parsing = AVSTREAM_PARSE_FULL;
133
134 /* for MJPEG, specify frame rate */
135 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
136 if (ap->time_base.num) {
137 st->codec->time_base= ap->time_base;
138 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
139 st->codec->codec_id == CODEC_ID_MPEG4 ||
140 st->codec->codec_id == CODEC_ID_DIRAC ||
141 st->codec->codec_id == CODEC_ID_DNXHD ||
142 st->codec->codec_id == CODEC_ID_VC1 ||
143 st->codec->codec_id == CODEC_ID_H264) {
144 st->codec->time_base= (AVRational){1,25};
145 }
146 av_set_pts_info(st, 64, 1, 1200000);
147
148 return 0;
149 }
150
151 /* Note: Do not forget to add new entries to the Makefile as well. */
152
153 static const AVOption audio_options[] = {
154 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
155 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
156 { NULL },
157 };
158
159 const AVClass ff_rawaudio_demuxer_class = {
160 .class_name = "rawaudio demuxer",
161 .item_name = av_default_item_name,
162 .option = audio_options,
163 .version = LIBAVUTIL_VERSION_INT,
164 };
165
166 #if CONFIG_G722_DEMUXER
167 AVInputFormat ff_g722_demuxer = {
168 "g722",
169 NULL_IF_CONFIG_SMALL("raw G.722"),
170 sizeof(RawAudioDemuxerContext),
171 NULL,
172 ff_raw_read_header,
173 ff_raw_read_partial_packet,
174 .flags= AVFMT_GENERIC_INDEX,
175 .extensions = "g722,722",
176 .value = CODEC_ID_ADPCM_G722,
177 .priv_class = &ff_rawaudio_demuxer_class,
178 };
179 #endif
180
181 #if CONFIG_GSM_DEMUXER
182 AVInputFormat ff_gsm_demuxer = {
183 "gsm",
184 NULL_IF_CONFIG_SMALL("raw GSM"),
185 0,
186 NULL,
187 ff_raw_audio_read_header,
188 ff_raw_read_partial_packet,
189 .flags= AVFMT_GENERIC_INDEX,
190 .extensions = "gsm",
191 .value = CODEC_ID_GSM,
192 };
193 #endif
194
195 #if CONFIG_MJPEG_DEMUXER
196 AVInputFormat ff_mjpeg_demuxer = {
197 "mjpeg",
198 NULL_IF_CONFIG_SMALL("raw MJPEG video"),
199 0,
200 NULL,
201 ff_raw_video_read_header,
202 ff_raw_read_partial_packet,
203 .flags= AVFMT_GENERIC_INDEX,
204 .extensions = "mjpg,mjpeg",
205 .value = CODEC_ID_MJPEG,
206 };
207 #endif
208
209 #if CONFIG_MLP_DEMUXER
210 AVInputFormat ff_mlp_demuxer = {
211 "mlp",
212 NULL_IF_CONFIG_SMALL("raw MLP"),
213 0,
214 NULL,
215 ff_raw_audio_read_header,
216 ff_raw_read_partial_packet,
217 .flags= AVFMT_GENERIC_INDEX,
218 .extensions = "mlp",
219 .value = CODEC_ID_MLP,
220 };
221 #endif
222
223 #if CONFIG_TRUEHD_DEMUXER
224 AVInputFormat ff_truehd_demuxer = {
225 "truehd",
226 NULL_IF_CONFIG_SMALL("raw TrueHD"),
227 0,
228 NULL,
229 ff_raw_audio_read_header,
230 ff_raw_read_partial_packet,
231 .flags= AVFMT_GENERIC_INDEX,
232 .extensions = "thd",
233 .value = CODEC_ID_TRUEHD,
234 };
235 #endif
236
237 #if CONFIG_SHORTEN_DEMUXER
238 AVInputFormat ff_shorten_demuxer = {
239 "shn",
240 NULL_IF_CONFIG_SMALL("raw Shorten"),
241 0,
242 NULL,
243 ff_raw_audio_read_header,
244 ff_raw_read_partial_packet,
245 .flags= AVFMT_GENERIC_INDEX,
246 .extensions = "shn",
247 .value = CODEC_ID_SHORTEN,
248 };
249 #endif
250
251 #if CONFIG_VC1_DEMUXER
252 AVInputFormat ff_vc1_demuxer = {
253 "vc1",
254 NULL_IF_CONFIG_SMALL("raw VC-1"),
255 0,
256 NULL /* vc1_probe */,
257 ff_raw_video_read_header,
258 ff_raw_read_partial_packet,
259 .extensions = "vc1",
260 .value = CODEC_ID_VC1,
261 };
262 #endif