fb3336d37c004751a45456859fdbbd9a72426911
[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 #include "libavutil/parseutils.h"
28 #include "libavutil/pixdesc.h"
29
30 /* raw input */
31 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
32 {
33 AVStream *st;
34 enum CodecID id;
35
36 st = avformat_new_stream(s, NULL);
37 if (!st)
38 return AVERROR(ENOMEM);
39
40 id = s->iformat->value;
41 if (id == CODEC_ID_RAWVIDEO) {
42 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
43 } else {
44 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
45 }
46 st->codec->codec_id = id;
47
48 switch(st->codec->codec_type) {
49 case AVMEDIA_TYPE_AUDIO: {
50 RawAudioDemuxerContext *s1 = s->priv_data;
51
52 st->codec->channels = 1;
53
54 if (id == CODEC_ID_ADPCM_G722)
55 st->codec->sample_rate = 16000;
56
57 if (s1 && s1->sample_rate)
58 st->codec->sample_rate = s1->sample_rate;
59 if (s1 && s1->channels)
60 st->codec->channels = s1->channels;
61
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;
65 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
66 break;
67 }
68 case AVMEDIA_TYPE_VIDEO: {
69 FFRawVideoDemuxerContext *s1 = s->priv_data;
70 int width = 0, height = 0, ret = 0;
71 enum PixelFormat pix_fmt;
72 AVRational framerate;
73
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;
82 }
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 }
87 av_set_pts_info(st, 64, framerate.den, framerate.num);
88 st->codec->width = width;
89 st->codec->height = height;
90 st->codec->pix_fmt = pix_fmt;
91 fail:
92 return ret;
93 }
94 default:
95 return -1;
96 }
97 return 0;
98 }
99
100 #define RAW_PACKET_SIZE 1024
101
102 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
103 {
104 int ret, size;
105
106 size = RAW_PACKET_SIZE;
107
108 if (av_new_packet(pkt, size) < 0)
109 return AVERROR(ENOMEM);
110
111 pkt->pos= avio_tell(s->pb);
112 pkt->stream_index = 0;
113 ret = ffio_read_partial(s->pb, pkt->data, size);
114 if (ret < 0) {
115 av_free_packet(pkt);
116 return ret;
117 }
118 pkt->size = ret;
119 return ret;
120 }
121
122 int ff_raw_audio_read_header(AVFormatContext *s,
123 AVFormatParameters *ap)
124 {
125 AVStream *st = avformat_new_stream(s, NULL);
126 if (!st)
127 return AVERROR(ENOMEM);
128 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
129 st->codec->codec_id = s->iformat->value;
130 st->need_parsing = AVSTREAM_PARSE_FULL;
131 /* the parameters will be extracted from the compressed bitstream */
132
133 return 0;
134 }
135
136 /* MPEG-1/H.263 input */
137 int ff_raw_video_read_header(AVFormatContext *s,
138 AVFormatParameters *ap)
139 {
140 AVStream *st;
141 FFRawVideoDemuxerContext *s1 = s->priv_data;
142 AVRational framerate;
143 int ret = 0;
144
145
146 st = avformat_new_stream(s, NULL);
147 if (!st) {
148 ret = AVERROR(ENOMEM);
149 goto fail;
150 }
151
152 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
153 st->codec->codec_id = s->iformat->value;
154 st->need_parsing = AVSTREAM_PARSE_FULL;
155
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;
159 }
160
161 st->codec->time_base = (AVRational){framerate.den, framerate.num};
162 av_set_pts_info(st, 64, 1, 1200000);
163
164 fail:
165 return ret;
166 }
167
168 /* Note: Do not forget to add new entries to the Makefile as well. */
169
170 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
171 #define DEC AV_OPT_FLAG_DECODING_PARAM
172 const AVOption ff_rawvideo_options[] = {
173 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
174 { NULL },
175 };
176
177 #if CONFIG_G722_DEMUXER
178 AVInputFormat ff_g722_demuxer = {
179 .name = "g722",
180 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
181 .read_header = ff_raw_read_header,
182 .read_packet = ff_raw_read_partial_packet,
183 .flags= AVFMT_GENERIC_INDEX,
184 .extensions = "g722,722",
185 .value = CODEC_ID_ADPCM_G722,
186 };
187 #endif
188
189 #if CONFIG_GSM_DEMUXER
190 AVInputFormat ff_gsm_demuxer = {
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,
195 .flags= AVFMT_GENERIC_INDEX,
196 .extensions = "gsm",
197 .value = CODEC_ID_GSM,
198 };
199 #endif
200
201 #if CONFIG_LATM_DEMUXER
202 AVInputFormat 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
213 #if CONFIG_MJPEG_DEMUXER
214 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
215 #endif
216
217 #if CONFIG_MLP_DEMUXER
218 AVInputFormat ff_mlp_demuxer = {
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,
223 .flags= AVFMT_GENERIC_INDEX,
224 .extensions = "mlp",
225 .value = CODEC_ID_MLP,
226 };
227 #endif
228
229 #if CONFIG_TRUEHD_DEMUXER
230 AVInputFormat ff_truehd_demuxer = {
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,
235 .flags= AVFMT_GENERIC_INDEX,
236 .extensions = "thd",
237 .value = CODEC_ID_TRUEHD,
238 };
239 #endif
240
241 #if CONFIG_SHORTEN_DEMUXER
242 AVInputFormat ff_shorten_demuxer = {
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,
247 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
248 .extensions = "shn",
249 .value = CODEC_ID_SHORTEN,
250 };
251 #endif
252
253 #if CONFIG_VC1_DEMUXER
254 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
255 #endif