lavf: deprecate AVFormatParameters.{channels,sample_rate}.
[ffmpeg.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 FF_API_FORMAT_PARAMETERS
51             if (ap->sample_rate)
52                 st->codec->sample_rate = ap->sample_rate;
53             if (ap->channels)
54                 st->codec->channels    = ap->channels;
55             else st->codec->channels   = 1;
56 #endif
57
58             if (s1->sample_rate)
59                 st->codec->sample_rate = s1->sample_rate;
60             if (s1->channels)
61                 st->codec->channels    = s1->channels;
62
63             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
64             assert(st->codec->bits_per_coded_sample > 0);
65             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
66             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
67             break;
68             }
69         case AVMEDIA_TYPE_VIDEO:
70             if(ap->time_base.num)
71                 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
72             else
73                 av_set_pts_info(st, 64, 1, 25);
74             st->codec->width = ap->width;
75             st->codec->height = ap->height;
76             st->codec->pix_fmt = ap->pix_fmt;
77             if(st->codec->pix_fmt == PIX_FMT_NONE)
78                 st->codec->pix_fmt= PIX_FMT_YUV420P;
79             break;
80         default:
81             return -1;
82         }
83     return 0;
84 }
85
86 #define RAW_PACKET_SIZE 1024
87
88 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
89 {
90     int ret, size;
91
92     size = RAW_PACKET_SIZE;
93
94     if (av_new_packet(pkt, size) < 0)
95         return AVERROR(ENOMEM);
96
97     pkt->pos= avio_tell(s->pb);
98     pkt->stream_index = 0;
99     ret = ffio_read_partial(s->pb, pkt->data, size);
100     if (ret < 0) {
101         av_free_packet(pkt);
102         return ret;
103     }
104     pkt->size = ret;
105     return ret;
106 }
107
108 int ff_raw_audio_read_header(AVFormatContext *s,
109                              AVFormatParameters *ap)
110 {
111     AVStream *st = av_new_stream(s, 0);
112     if (!st)
113         return AVERROR(ENOMEM);
114     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
115     st->codec->codec_id = s->iformat->value;
116     st->need_parsing = AVSTREAM_PARSE_FULL;
117     /* the parameters will be extracted from the compressed bitstream */
118
119     return 0;
120 }
121
122 /* MPEG-1/H.263 input */
123 int ff_raw_video_read_header(AVFormatContext *s,
124                              AVFormatParameters *ap)
125 {
126     AVStream *st;
127
128     st = av_new_stream(s, 0);
129     if (!st)
130         return AVERROR(ENOMEM);
131
132     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
133     st->codec->codec_id = s->iformat->value;
134     st->need_parsing = AVSTREAM_PARSE_FULL;
135
136     /* for MJPEG, specify frame rate */
137     /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
138     if (ap->time_base.num) {
139         st->codec->time_base= ap->time_base;
140     } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
141                 st->codec->codec_id == CODEC_ID_MPEG4 ||
142                 st->codec->codec_id == CODEC_ID_DIRAC ||
143                 st->codec->codec_id == CODEC_ID_DNXHD ||
144                 st->codec->codec_id == CODEC_ID_VC1   ||
145                 st->codec->codec_id == CODEC_ID_H264) {
146         st->codec->time_base= (AVRational){1,25};
147     }
148     av_set_pts_info(st, 64, 1, 1200000);
149
150     return 0;
151 }
152
153 /* Note: Do not forget to add new entries to the Makefile as well. */
154
155 static const AVOption audio_options[] = {
156     { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
157     { "channels",    "", offsetof(RawAudioDemuxerContext, channels),    FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
158     { NULL },
159 };
160
161 const AVClass ff_rawaudio_demuxer_class = {
162     .class_name     = "rawaudio demuxer",
163     .item_name      = av_default_item_name,
164     .option         = audio_options,
165     .version        = LIBAVUTIL_VERSION_INT,
166 };
167
168 #if CONFIG_G722_DEMUXER
169 AVInputFormat ff_g722_demuxer = {
170     "g722",
171     NULL_IF_CONFIG_SMALL("raw G.722"),
172     sizeof(RawAudioDemuxerContext),
173     NULL,
174     ff_raw_read_header,
175     ff_raw_read_partial_packet,
176     .flags= AVFMT_GENERIC_INDEX,
177     .extensions = "g722,722",
178     .value = CODEC_ID_ADPCM_G722,
179     .priv_class = &ff_rawaudio_demuxer_class,
180 };
181 #endif
182
183 #if CONFIG_GSM_DEMUXER
184 AVInputFormat ff_gsm_demuxer = {
185     "gsm",
186     NULL_IF_CONFIG_SMALL("raw GSM"),
187     0,
188     NULL,
189     ff_raw_audio_read_header,
190     ff_raw_read_partial_packet,
191     .flags= AVFMT_GENERIC_INDEX,
192     .extensions = "gsm",
193     .value = CODEC_ID_GSM,
194 };
195 #endif
196
197 #if CONFIG_MJPEG_DEMUXER
198 AVInputFormat ff_mjpeg_demuxer = {
199     "mjpeg",
200     NULL_IF_CONFIG_SMALL("raw MJPEG video"),
201     0,
202     NULL,
203     ff_raw_video_read_header,
204     ff_raw_read_partial_packet,
205     .flags= AVFMT_GENERIC_INDEX,
206     .extensions = "mjpg,mjpeg",
207     .value = CODEC_ID_MJPEG,
208 };
209 #endif
210
211 #if CONFIG_MLP_DEMUXER
212 AVInputFormat ff_mlp_demuxer = {
213     "mlp",
214     NULL_IF_CONFIG_SMALL("raw MLP"),
215     0,
216     NULL,
217     ff_raw_audio_read_header,
218     ff_raw_read_partial_packet,
219     .flags= AVFMT_GENERIC_INDEX,
220     .extensions = "mlp",
221     .value = CODEC_ID_MLP,
222 };
223 #endif
224
225 #if CONFIG_TRUEHD_DEMUXER
226 AVInputFormat ff_truehd_demuxer = {
227     "truehd",
228     NULL_IF_CONFIG_SMALL("raw TrueHD"),
229     0,
230     NULL,
231     ff_raw_audio_read_header,
232     ff_raw_read_partial_packet,
233     .flags= AVFMT_GENERIC_INDEX,
234     .extensions = "thd",
235     .value = CODEC_ID_TRUEHD,
236 };
237 #endif
238
239 #if CONFIG_SHORTEN_DEMUXER
240 AVInputFormat ff_shorten_demuxer = {
241     "shn",
242     NULL_IF_CONFIG_SMALL("raw Shorten"),
243     0,
244     NULL,
245     ff_raw_audio_read_header,
246     ff_raw_read_partial_packet,
247     .flags= AVFMT_GENERIC_INDEX,
248     .extensions = "shn",
249     .value = CODEC_ID_SHORTEN,
250 };
251 #endif
252
253 #if CONFIG_VC1_DEMUXER
254 AVInputFormat ff_vc1_demuxer = {
255     "vc1",
256     NULL_IF_CONFIG_SMALL("raw VC-1"),
257     0,
258     NULL /* vc1_probe */,
259     ff_raw_video_read_header,
260     ff_raw_read_partial_packet,
261     .extensions = "vc1",
262     .value = CODEC_ID_VC1,
263 };
264 #endif