lavf: add avformat_new_stream as a replacement for av_new_stream.
[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
c14fe6bc 52 st->codec->channels = 1;
5b3865fc 53
d906f49a
AK
54 if (id == CODEC_ID_ADPCM_G722)
55 st->codec->sample_rate = 16000;
56
57 if (s1 && s1->sample_rate)
5b3865fc 58 st->codec->sample_rate = s1->sample_rate;
d906f49a 59 if (s1 && s1->channels)
5b3865fc
AK
60 st->codec->channels = s1->channels;
61
a3d23e15
BC
62 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
63 assert(st->codec->bits_per_coded_sample > 0);
64 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
01f4895c 65 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
de6d9b64 66 break;
5b3865fc 67 }
973f686a
AK
68 case AVMEDIA_TYPE_VIDEO: {
69 FFRawVideoDemuxerContext *s1 = s->priv_data;
1f94c31f 70 int width = 0, height = 0, ret = 0;
2a85f218 71 enum PixelFormat pix_fmt;
e762b1ce 72 AVRational framerate;
2a85f218 73
2a85f218
AK
74 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
75 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
76 goto fail;
77 }
78 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
79 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
80 ret = AVERROR(EINVAL);
81 goto fail;
973f686a 82 }
e762b1ce
AK
83 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
84 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
85 goto fail;
86 }
e762b1ce 87 av_set_pts_info(st, 64, framerate.den, framerate.num);
973f686a
AK
88 st->codec->width = width;
89 st->codec->height = height;
2a85f218 90 st->codec->pix_fmt = pix_fmt;
2a85f218 91fail:
2a85f218 92 return ret;
973f686a 93 }
de6d9b64 94 default:
27e084bd 95 return -1;
de6d9b64 96 }
de6d9b64
FB
97 return 0;
98}
99
2e93e3aa 100#define RAW_PACKET_SIZE 1024
de6d9b64 101
81f052cb 102int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
e15dec10
LS
103{
104 int ret, size;
105
106 size = RAW_PACKET_SIZE;
107
108 if (av_new_packet(pkt, size) < 0)
c67031e7 109 return AVERROR(ENOMEM);
115329f1 110
a2704c97 111 pkt->pos= avio_tell(s->pb);
e15dec10 112 pkt->stream_index = 0;
b3db9cee 113 ret = ffio_read_partial(s->pb, pkt->data, size);
c3db0bc6 114 if (ret < 0) {
e15dec10 115 av_free_packet(pkt);
c3db0bc6 116 return ret;
e15dec10
LS
117 }
118 pkt->size = ret;
119 return ret;
120}
76d32428 121
6d0678d1 122int ff_raw_audio_read_header(AVFormatContext *s,
a0af2fa4 123 AVFormatParameters *ap)
fda885c7 124{
a0af2fa4 125 AVStream *st = av_new_stream(s, 0);
fda885c7 126 if (!st)
769e10f0 127 return AVERROR(ENOMEM);
72415b2a 128 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
a0af2fa4 129 st->codec->codec_id = s->iformat->value;
57004ff1 130 st->need_parsing = AVSTREAM_PARSE_FULL;
fda885c7 131 /* the parameters will be extracted from the compressed bitstream */
6cde949a 132
fda885c7
MR
133 return 0;
134}
135
fb9f1117 136/* MPEG-1/H.263 input */
b47a5a95 137int ff_raw_video_read_header(AVFormatContext *s,
de6d9b64
FB
138 AVFormatParameters *ap)
139{
140 AVStream *st;
e762b1ce
AK
141 FFRawVideoDemuxerContext *s1 = s->priv_data;
142 AVRational framerate;
143 int ret = 0;
144
de6d9b64 145
c9a65ca8 146 st = av_new_stream(s, 0);
e762b1ce
AK
147 if (!st) {
148 ret = AVERROR(ENOMEM);
149 goto fail;
150 }
de6d9b64 151
72415b2a 152 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 153 st->codec->codec_id = s->iformat->value;
57004ff1 154 st->need_parsing = AVSTREAM_PARSE_FULL;
4986a429 155
e762b1ce
AK
156 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
157 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
158 goto fail;
27e084bd 159 }
e762b1ce
AK
160
161 st->codec->time_base = (AVRational){framerate.den, framerate.num};
4022fe01 162 av_set_pts_info(st, 64, 1, 1200000);
80ce3254 163
e762b1ce 164fail:
e762b1ce 165 return ret;
de6d9b64
FB
166}
167
900eb63d
DB
168/* Note: Do not forget to add new entries to the Makefile as well. */
169
973f686a
AK
170#define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
171#define DEC AV_OPT_FLAG_DECODING_PARAM
85d982f1 172const AVOption ff_rawvideo_options[] = {
145f741e 173 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
973f686a
AK
174 { NULL },
175};
973f686a 176
9013560f 177#if CONFIG_G722_DEMUXER
c6610a21 178AVInputFormat ff_g722_demuxer = {
dfc2c4d9
AK
179 .name = "g722",
180 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
dfc2c4d9
AK
181 .read_header = ff_raw_read_header,
182 .read_packet = ff_raw_read_partial_packet,
9013560f
MS
183 .flags= AVFMT_GENERIC_INDEX,
184 .extensions = "g722,722",
185 .value = CODEC_ID_ADPCM_G722,
186};
187#endif
188
b250f9c6 189#if CONFIG_GSM_DEMUXER
c6610a21 190AVInputFormat ff_gsm_demuxer = {
dfc2c4d9
AK
191 .name = "gsm",
192 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
193 .read_header = ff_raw_audio_read_header,
194 .read_packet = ff_raw_read_partial_packet,
60711e95
MN
195 .flags= AVFMT_GENERIC_INDEX,
196 .extensions = "gsm",
197 .value = CODEC_ID_GSM,
198};
7402ee23 199#endif
60711e95 200
dcb9f6a2
JG
201#if CONFIG_LATM_DEMUXER
202AVInputFormat ff_latm_demuxer = {
203 .name = "latm",
204 .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
205 .read_header = ff_raw_audio_read_header,
206 .read_packet = ff_raw_read_partial_packet,
207 .flags= AVFMT_GENERIC_INDEX,
208 .extensions = "latm",
209 .value = CODEC_ID_AAC_LATM,
210};
211#endif
212
b250f9c6 213#if CONFIG_MJPEG_DEMUXER
fa4924a3 214FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
7402ee23 215#endif
0da71265 216
b250f9c6 217#if CONFIG_MLP_DEMUXER
c6610a21 218AVInputFormat ff_mlp_demuxer = {
dfc2c4d9
AK
219 .name = "mlp",
220 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
221 .read_header = ff_raw_audio_read_header,
222 .read_packet = ff_raw_read_partial_packet,
e9b78eeb 223 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
224 .extensions = "mlp",
225 .value = CODEC_ID_MLP,
de6d9b64 226};
7402ee23 227#endif
de6d9b64 228
23d9cc45 229#if CONFIG_TRUEHD_DEMUXER
c6610a21 230AVInputFormat ff_truehd_demuxer = {
dfc2c4d9
AK
231 .name = "truehd",
232 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
233 .read_header = ff_raw_audio_read_header,
234 .read_packet = ff_raw_read_partial_packet,
23d9cc45
RP
235 .flags= AVFMT_GENERIC_INDEX,
236 .extensions = "thd",
237 .value = CODEC_ID_TRUEHD,
238};
239#endif
240
b250f9c6 241#if CONFIG_SHORTEN_DEMUXER
c6610a21 242AVInputFormat ff_shorten_demuxer = {
dfc2c4d9
AK
243 .name = "shn",
244 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
245 .read_header = ff_raw_audio_read_header,
246 .read_packet = ff_raw_read_partial_packet,
e32dfdaa 247 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
76d32428
DB
248 .extensions = "shn",
249 .value = CODEC_ID_SHORTEN,
250};
7402ee23 251#endif
76d32428 252
b250f9c6 253#if CONFIG_VC1_DEMUXER
fa4924a3 254FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
7402ee23 255#endif