]> git.sesse.net Git - ffmpeg/blob - libavformat/rawdec.c
81dd8b49e58067aefef1cea67c42cdd2938233f7
[ffmpeg] / 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 "internal.h"
25 #include "avio_internal.h"
26 #include "rawdec.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
30
31 /* raw input */
32 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
33 {
34     AVStream *st;
35     enum CodecID id;
36
37     st = avformat_new_stream(s, NULL);
38     if (!st)
39         return AVERROR(ENOMEM);
40
41         id = s->iformat->value;
42         if (id == CODEC_ID_RAWVIDEO) {
43             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
44         } else {
45             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
46         }
47         st->codec->codec_id = id;
48
49         switch(st->codec->codec_type) {
50         case AVMEDIA_TYPE_AUDIO: {
51             RawAudioDemuxerContext *s1 = s->priv_data;
52
53             st->codec->channels = 1;
54
55             if (id == CODEC_ID_ADPCM_G722)
56                 st->codec->sample_rate = 16000;
57
58             if (s1 && s1->sample_rate)
59                 st->codec->sample_rate = s1->sample_rate;
60             if (s1 && 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             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
67             break;
68             }
69         case AVMEDIA_TYPE_VIDEO: {
70             FFRawVideoDemuxerContext *s1 = s->priv_data;
71             int width = 0, height = 0, ret = 0;
72             enum PixelFormat pix_fmt;
73             AVRational framerate;
74
75             if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
76                 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
77                 goto fail;
78             }
79             if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
80                 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
81                 ret = AVERROR(EINVAL);
82                 goto fail;
83             }
84             if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
85                 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
86                 goto fail;
87             }
88             avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
89             st->codec->width  = width;
90             st->codec->height = height;
91             st->codec->pix_fmt = pix_fmt;
92 fail:
93             return ret;
94             }
95         default:
96             return -1;
97         }
98     return 0;
99 }
100
101 #define RAW_PACKET_SIZE 1024
102
103 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
104 {
105     int ret, size;
106
107     size = RAW_PACKET_SIZE;
108
109     if (av_new_packet(pkt, size) < 0)
110         return AVERROR(ENOMEM);
111
112     pkt->pos= avio_tell(s->pb);
113     pkt->stream_index = 0;
114     ret = ffio_read_partial(s->pb, pkt->data, size);
115     if (ret < 0) {
116         av_free_packet(pkt);
117         return ret;
118     }
119     pkt->size = ret;
120     return ret;
121 }
122
123 int ff_raw_audio_read_header(AVFormatContext *s,
124                              AVFormatParameters *ap)
125 {
126     AVStream *st = avformat_new_stream(s, NULL);
127     if (!st)
128         return AVERROR(ENOMEM);
129     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
130     st->codec->codec_id = s->iformat->value;
131     st->need_parsing = AVSTREAM_PARSE_FULL;
132     /* the parameters will be extracted from the compressed bitstream */
133
134     return 0;
135 }
136
137 /* MPEG-1/H.263 input */
138 int ff_raw_video_read_header(AVFormatContext *s,
139                              AVFormatParameters *ap)
140 {
141     AVStream *st;
142     FFRawVideoDemuxerContext *s1 = s->priv_data;
143     AVRational framerate;
144     int ret = 0;
145
146
147     st = avformat_new_stream(s, NULL);
148     if (!st) {
149         ret = AVERROR(ENOMEM);
150         goto fail;
151     }
152
153     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
154     st->codec->codec_id = s->iformat->value;
155     st->need_parsing = AVSTREAM_PARSE_FULL;
156
157     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
158         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
159         goto fail;
160     }
161
162     st->codec->time_base = (AVRational){framerate.den, framerate.num};
163     avpriv_set_pts_info(st, 64, 1, 1200000);
164
165 fail:
166     return ret;
167 }
168
169 /* Note: Do not forget to add new entries to the Makefile as well. */
170
171 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
172 #define DEC AV_OPT_FLAG_DECODING_PARAM
173 const AVOption ff_rawvideo_options[] = {
174     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
175     { NULL },
176 };
177
178 #if CONFIG_G722_DEMUXER
179 AVInputFormat ff_g722_demuxer = {
180     .name           = "g722",
181     .long_name      = NULL_IF_CONFIG_SMALL("raw G.722"),
182     .read_header    = ff_raw_read_header,
183     .read_packet    = ff_raw_read_partial_packet,
184     .flags= AVFMT_GENERIC_INDEX,
185     .extensions = "g722,722",
186     .value = CODEC_ID_ADPCM_G722,
187 };
188 #endif
189
190 #if CONFIG_LATM_DEMUXER
191 AVInputFormat ff_latm_demuxer = {
192     .name           = "latm",
193     .long_name      = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
194     .read_header    = ff_raw_audio_read_header,
195     .read_packet    = ff_raw_read_partial_packet,
196     .flags= AVFMT_GENERIC_INDEX,
197     .extensions = "latm",
198     .value = CODEC_ID_AAC_LATM,
199 };
200 #endif
201
202 #if CONFIG_MJPEG_DEMUXER
203 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
204 #endif
205
206 #if CONFIG_MLP_DEMUXER
207 AVInputFormat ff_mlp_demuxer = {
208     .name           = "mlp",
209     .long_name      = NULL_IF_CONFIG_SMALL("raw MLP"),
210     .read_header    = ff_raw_audio_read_header,
211     .read_packet    = ff_raw_read_partial_packet,
212     .flags= AVFMT_GENERIC_INDEX,
213     .extensions = "mlp",
214     .value = CODEC_ID_MLP,
215 };
216 #endif
217
218 #if CONFIG_TRUEHD_DEMUXER
219 AVInputFormat ff_truehd_demuxer = {
220     .name           = "truehd",
221     .long_name      = NULL_IF_CONFIG_SMALL("raw TrueHD"),
222     .read_header    = ff_raw_audio_read_header,
223     .read_packet    = ff_raw_read_partial_packet,
224     .flags= AVFMT_GENERIC_INDEX,
225     .extensions = "thd",
226     .value = CODEC_ID_TRUEHD,
227 };
228 #endif
229
230 #if CONFIG_SHORTEN_DEMUXER
231 AVInputFormat ff_shorten_demuxer = {
232     .name           = "shn",
233     .long_name      = NULL_IF_CONFIG_SMALL("raw Shorten"),
234     .read_header    = ff_raw_audio_read_header,
235     .read_packet    = ff_raw_read_partial_packet,
236     .flags          = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
237     .extensions = "shn",
238     .value = CODEC_ID_SHORTEN,
239 };
240 #endif
241
242 #if CONFIG_VC1_DEMUXER
243 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
244 #endif