wav: remove a use of deprecated AV_METADATA_ macro
[libav.git] / libavformat / rawdec.c
CommitLineData
115329f1 1/*
4ca31edc 2 * RAW demuxers
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
84c63c01 4 * Copyright (c) 2005 Alex Beregszaszi
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
19720f15
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
19720f15 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
245976da 22
de6d9b64 23#include "avformat.h"
b3db9cee 24#include "avio_internal.h"
4ca31edc 25#include "rawdec.h"
5b3865fc 26#include "libavutil/opt.h"
973f686a 27#include "libavutil/parseutils.h"
2a85f218 28#include "libavutil/pixdesc.h"
de6d9b64 29
de6d9b64 30/* raw input */
e94204df 31int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
32{
33 AVStream *st;
0f87b771 34 enum CodecID id;
de6d9b64 35
c9a65ca8 36 st = av_new_stream(s, 0);
de6d9b64 37 if (!st)
769e10f0 38 return AVERROR(ENOMEM);
c04c3282 39
c9a65ca8
FB
40 id = s->iformat->value;
41 if (id == CODEC_ID_RAWVIDEO) {
72415b2a 42 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
de6d9b64 43 } else {
72415b2a 44 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
de6d9b64 45 }
01f4895c 46 st->codec->codec_id = id;
c9a65ca8 47
01f4895c 48 switch(st->codec->codec_type) {
5b3865fc
AK
49 case AVMEDIA_TYPE_AUDIO: {
50 RawAudioDemuxerContext *s1 = s->priv_data;
51
bffd4dd1 52#if FF_API_FORMAT_PARAMETERS
5b3865fc
AK
53 if (ap->sample_rate)
54 st->codec->sample_rate = ap->sample_rate;
55 if (ap->channels)
56 st->codec->channels = ap->channels;
57 else st->codec->channels = 1;
bffd4dd1 58#endif
5b3865fc
AK
59
60 if (s1->sample_rate)
61 st->codec->sample_rate = s1->sample_rate;
62 if (s1->channels)
63 st->codec->channels = s1->channels;
64
a3d23e15
BC
65 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
66 assert(st->codec->bits_per_coded_sample > 0);
67 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
01f4895c 68 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
de6d9b64 69 break;
5b3865fc 70 }
973f686a
AK
71 case AVMEDIA_TYPE_VIDEO: {
72 FFRawVideoDemuxerContext *s1 = s->priv_data;
1f94c31f 73 int width = 0, height = 0, ret = 0;
2a85f218 74 enum PixelFormat pix_fmt;
e762b1ce 75 AVRational framerate;
2a85f218 76
2a85f218
AK
77 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
78 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
79 goto fail;
80 }
81 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
82 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
83 ret = AVERROR(EINVAL);
84 goto fail;
973f686a 85 }
e762b1ce
AK
86 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
87 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
88 goto fail;
89 }
973f686a
AK
90#if FF_API_FORMAT_PARAMETERS
91 if (ap->width > 0)
92 width = ap->width;
93 if (ap->height > 0)
94 height = ap->height;
2a85f218
AK
95 if (ap->pix_fmt)
96 pix_fmt = ap->pix_fmt;
e762b1ce
AK
97 if (ap->time_base.num)
98 framerate = (AVRational){ap->time_base.den, ap->time_base.num};
973f686a 99#endif
e762b1ce 100 av_set_pts_info(st, 64, framerate.den, framerate.num);
973f686a
AK
101 st->codec->width = width;
102 st->codec->height = height;
2a85f218 103 st->codec->pix_fmt = pix_fmt;
2a85f218 104fail:
2a85f218 105 return ret;
973f686a 106 }
de6d9b64 107 default:
27e084bd 108 return -1;
de6d9b64 109 }
de6d9b64
FB
110 return 0;
111}
112
2e93e3aa 113#define RAW_PACKET_SIZE 1024
de6d9b64 114
81f052cb 115int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
e15dec10
LS
116{
117 int ret, size;
118
119 size = RAW_PACKET_SIZE;
120
121 if (av_new_packet(pkt, size) < 0)
c67031e7 122 return AVERROR(ENOMEM);
115329f1 123
a2704c97 124 pkt->pos= avio_tell(s->pb);
e15dec10 125 pkt->stream_index = 0;
b3db9cee 126 ret = ffio_read_partial(s->pb, pkt->data, size);
c3db0bc6 127 if (ret < 0) {
e15dec10 128 av_free_packet(pkt);
c3db0bc6 129 return ret;
e15dec10
LS
130 }
131 pkt->size = ret;
132 return ret;
133}
76d32428 134
6d0678d1 135int ff_raw_audio_read_header(AVFormatContext *s,
a0af2fa4 136 AVFormatParameters *ap)
fda885c7 137{
a0af2fa4 138 AVStream *st = av_new_stream(s, 0);
fda885c7 139 if (!st)
769e10f0 140 return AVERROR(ENOMEM);
72415b2a 141 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
a0af2fa4 142 st->codec->codec_id = s->iformat->value;
57004ff1 143 st->need_parsing = AVSTREAM_PARSE_FULL;
fda885c7 144 /* the parameters will be extracted from the compressed bitstream */
6cde949a 145
fda885c7
MR
146 return 0;
147}
148
fb9f1117 149/* MPEG-1/H.263 input */
b47a5a95 150int ff_raw_video_read_header(AVFormatContext *s,
de6d9b64
FB
151 AVFormatParameters *ap)
152{
153 AVStream *st;
e762b1ce
AK
154 FFRawVideoDemuxerContext *s1 = s->priv_data;
155 AVRational framerate;
156 int ret = 0;
157
de6d9b64 158
c9a65ca8 159 st = av_new_stream(s, 0);
e762b1ce
AK
160 if (!st) {
161 ret = AVERROR(ENOMEM);
162 goto fail;
163 }
de6d9b64 164
72415b2a 165 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 166 st->codec->codec_id = s->iformat->value;
57004ff1 167 st->need_parsing = AVSTREAM_PARSE_FULL;
4986a429 168
e762b1ce
AK
169 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
170 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
171 goto fail;
27e084bd 172 }
e762b1ce
AK
173#if FF_API_FORMAT_PARAMETERS
174 if (ap->time_base.num)
175 framerate = (AVRational){ap->time_base.den, ap->time_base.num};
176#endif
177
178 st->codec->time_base = (AVRational){framerate.den, framerate.num};
4022fe01 179 av_set_pts_info(st, 64, 1, 1200000);
80ce3254 180
e762b1ce 181fail:
e762b1ce 182 return ret;
de6d9b64
FB
183}
184
900eb63d
DB
185/* Note: Do not forget to add new entries to the Makefile as well. */
186
5b3865fc
AK
187static const AVOption audio_options[] = {
188 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
189 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
190 { NULL },
191};
192
193const AVClass ff_rawaudio_demuxer_class = {
194 .class_name = "rawaudio demuxer",
195 .item_name = av_default_item_name,
196 .option = audio_options,
197 .version = LIBAVUTIL_VERSION_INT,
198};
199
973f686a
AK
200#define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
201#define DEC AV_OPT_FLAG_DECODING_PARAM
202static const AVOption video_options[] = {
203 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
2a85f218 204 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
e762b1ce 205 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
973f686a
AK
206 { NULL },
207};
208#undef OFFSET
209#undef DEC
210
211const AVClass ff_rawvideo_demuxer_class = {
212 .class_name = "rawvideo demuxer",
213 .item_name = av_default_item_name,
214 .option = video_options,
215 .version = LIBAVUTIL_VERSION_INT,
216};
217
9013560f 218#if CONFIG_G722_DEMUXER
c6610a21 219AVInputFormat ff_g722_demuxer = {
9013560f
MS
220 "g722",
221 NULL_IF_CONFIG_SMALL("raw G.722"),
5b3865fc 222 sizeof(RawAudioDemuxerContext),
9013560f
MS
223 NULL,
224 ff_raw_read_header,
225 ff_raw_read_partial_packet,
226 .flags= AVFMT_GENERIC_INDEX,
227 .extensions = "g722,722",
228 .value = CODEC_ID_ADPCM_G722,
5b3865fc 229 .priv_class = &ff_rawaudio_demuxer_class,
9013560f
MS
230};
231#endif
232
b250f9c6 233#if CONFIG_GSM_DEMUXER
c6610a21 234AVInputFormat ff_gsm_demuxer = {
60711e95 235 "gsm",
b4ee1d39 236 NULL_IF_CONFIG_SMALL("raw GSM"),
60711e95
MN
237 0,
238 NULL,
6d0678d1 239 ff_raw_audio_read_header,
81f052cb 240 ff_raw_read_partial_packet,
60711e95
MN
241 .flags= AVFMT_GENERIC_INDEX,
242 .extensions = "gsm",
243 .value = CODEC_ID_GSM,
244};
7402ee23 245#endif
60711e95 246
b250f9c6 247#if CONFIG_MJPEG_DEMUXER
fa4924a3 248FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
7402ee23 249#endif
0da71265 250
b250f9c6 251#if CONFIG_MLP_DEMUXER
c6610a21 252AVInputFormat ff_mlp_demuxer = {
76d32428
DB
253 "mlp",
254 NULL_IF_CONFIG_SMALL("raw MLP"),
c9a65ca8 255 0,
76d32428 256 NULL,
6d0678d1 257 ff_raw_audio_read_header,
81f052cb 258 ff_raw_read_partial_packet,
e9b78eeb 259 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
260 .extensions = "mlp",
261 .value = CODEC_ID_MLP,
de6d9b64 262};
7402ee23 263#endif
de6d9b64 264
23d9cc45 265#if CONFIG_TRUEHD_DEMUXER
c6610a21 266AVInputFormat ff_truehd_demuxer = {
23d9cc45
RP
267 "truehd",
268 NULL_IF_CONFIG_SMALL("raw TrueHD"),
269 0,
270 NULL,
6d0678d1 271 ff_raw_audio_read_header,
23d9cc45
RP
272 ff_raw_read_partial_packet,
273 .flags= AVFMT_GENERIC_INDEX,
274 .extensions = "thd",
275 .value = CODEC_ID_TRUEHD,
276};
277#endif
278
b250f9c6 279#if CONFIG_SHORTEN_DEMUXER
c6610a21 280AVInputFormat ff_shorten_demuxer = {
76d32428
DB
281 "shn",
282 NULL_IF_CONFIG_SMALL("raw Shorten"),
283 0,
284 NULL,
6d0678d1 285 ff_raw_audio_read_header,
81f052cb 286 ff_raw_read_partial_packet,
76d32428
DB
287 .flags= AVFMT_GENERIC_INDEX,
288 .extensions = "shn",
289 .value = CODEC_ID_SHORTEN,
290};
7402ee23 291#endif
76d32428 292
b250f9c6 293#if CONFIG_VC1_DEMUXER
fa4924a3 294FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
7402ee23 295#endif