4f830e3c207b54f52761a16107ac3a8e2243acb0
[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 FFmpeg.
7 *
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "rawdec.h"
25
26 /* raw input */
27 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
28 {
29 AVStream *st;
30 enum CodecID id;
31
32 st = av_new_stream(s, 0);
33 if (!st)
34 return AVERROR(ENOMEM);
35
36 id = s->iformat->value;
37 if (id == CODEC_ID_RAWVIDEO) {
38 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
39 } else {
40 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
41 }
42 st->codec->codec_id = id;
43
44 switch(st->codec->codec_type) {
45 case AVMEDIA_TYPE_AUDIO:
46 st->codec->sample_rate = ap->sample_rate;
47 if(ap->channels) st->codec->channels = ap->channels;
48 else st->codec->channels = 1;
49 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
50 assert(st->codec->bits_per_coded_sample > 0);
51 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
52 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
53 break;
54 case AVMEDIA_TYPE_VIDEO:
55 if(ap->time_base.num)
56 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
57 else
58 av_set_pts_info(st, 64, 1, 25);
59 st->codec->width = ap->width;
60 st->codec->height = ap->height;
61 st->codec->pix_fmt = ap->pix_fmt;
62 if(st->codec->pix_fmt == PIX_FMT_NONE)
63 st->codec->pix_fmt= PIX_FMT_YUV420P;
64 break;
65 default:
66 return -1;
67 }
68 return 0;
69 }
70
71 #define RAW_PACKET_SIZE 1024
72
73 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
74 {
75 int ret, size;
76
77 size = RAW_PACKET_SIZE;
78
79 if (av_new_packet(pkt, size) < 0)
80 return AVERROR(ENOMEM);
81
82 pkt->pos= url_ftell(s->pb);
83 pkt->stream_index = 0;
84 ret = get_partial_buffer(s->pb, pkt->data, size);
85 if (ret < 0) {
86 av_free_packet(pkt);
87 return ret;
88 }
89 pkt->size = ret;
90 return ret;
91 }
92
93 int ff_raw_audio_read_header(AVFormatContext *s,
94 AVFormatParameters *ap)
95 {
96 AVStream *st = av_new_stream(s, 0);
97 if (!st)
98 return AVERROR(ENOMEM);
99 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
100 st->codec->codec_id = s->iformat->value;
101 st->need_parsing = AVSTREAM_PARSE_FULL;
102 /* the parameters will be extracted from the compressed bitstream */
103
104 return 0;
105 }
106
107 /* MPEG-1/H.263 input */
108 int ff_raw_video_read_header(AVFormatContext *s,
109 AVFormatParameters *ap)
110 {
111 AVStream *st;
112
113 st = av_new_stream(s, 0);
114 if (!st)
115 return AVERROR(ENOMEM);
116
117 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
118 st->codec->codec_id = s->iformat->value;
119 st->need_parsing = AVSTREAM_PARSE_FULL;
120
121 /* for MJPEG, specify frame rate */
122 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
123 if (ap->time_base.num) {
124 st->codec->time_base= ap->time_base;
125 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
126 st->codec->codec_id == CODEC_ID_MPEG4 ||
127 st->codec->codec_id == CODEC_ID_DIRAC ||
128 st->codec->codec_id == CODEC_ID_DNXHD ||
129 st->codec->codec_id == CODEC_ID_VC1 ||
130 st->codec->codec_id == CODEC_ID_H264) {
131 st->codec->time_base= (AVRational){1,25};
132 }
133 av_set_pts_info(st, 64, 1, 1200000);
134
135 return 0;
136 }
137
138 /* Note: Do not forget to add new entries to the Makefile as well. */
139
140 #if CONFIG_G722_DEMUXER
141 AVInputFormat ff_g722_demuxer = {
142 "g722",
143 NULL_IF_CONFIG_SMALL("raw G.722"),
144 0,
145 NULL,
146 ff_raw_read_header,
147 ff_raw_read_partial_packet,
148 .flags= AVFMT_GENERIC_INDEX,
149 .extensions = "g722,722",
150 .value = CODEC_ID_ADPCM_G722,
151 };
152 #endif
153
154 #if CONFIG_GSM_DEMUXER
155 AVInputFormat ff_gsm_demuxer = {
156 "gsm",
157 NULL_IF_CONFIG_SMALL("raw GSM"),
158 0,
159 NULL,
160 ff_raw_audio_read_header,
161 ff_raw_read_partial_packet,
162 .flags= AVFMT_GENERIC_INDEX,
163 .extensions = "gsm",
164 .value = CODEC_ID_GSM,
165 };
166 #endif
167
168 #if CONFIG_MJPEG_DEMUXER
169 AVInputFormat ff_mjpeg_demuxer = {
170 "mjpeg",
171 NULL_IF_CONFIG_SMALL("raw MJPEG video"),
172 0,
173 NULL,
174 ff_raw_video_read_header,
175 ff_raw_read_partial_packet,
176 .flags= AVFMT_GENERIC_INDEX,
177 .extensions = "mjpg,mjpeg",
178 .value = CODEC_ID_MJPEG,
179 };
180 #endif
181
182 #if CONFIG_MLP_DEMUXER
183 AVInputFormat ff_mlp_demuxer = {
184 "mlp",
185 NULL_IF_CONFIG_SMALL("raw MLP"),
186 0,
187 NULL,
188 ff_raw_audio_read_header,
189 ff_raw_read_partial_packet,
190 .flags= AVFMT_GENERIC_INDEX,
191 .extensions = "mlp",
192 .value = CODEC_ID_MLP,
193 };
194 #endif
195
196 #if CONFIG_TRUEHD_DEMUXER
197 AVInputFormat ff_truehd_demuxer = {
198 "truehd",
199 NULL_IF_CONFIG_SMALL("raw TrueHD"),
200 0,
201 NULL,
202 ff_raw_audio_read_header,
203 ff_raw_read_partial_packet,
204 .flags= AVFMT_GENERIC_INDEX,
205 .extensions = "thd",
206 .value = CODEC_ID_TRUEHD,
207 };
208 #endif
209
210 #if CONFIG_SHORTEN_DEMUXER
211 AVInputFormat ff_shorten_demuxer = {
212 "shn",
213 NULL_IF_CONFIG_SMALL("raw Shorten"),
214 0,
215 NULL,
216 ff_raw_audio_read_header,
217 ff_raw_read_partial_packet,
218 .flags= AVFMT_GENERIC_INDEX,
219 .extensions = "shn",
220 .value = CODEC_ID_SHORTEN,
221 };
222 #endif
223
224 #if CONFIG_VC1_DEMUXER
225 AVInputFormat ff_vc1_demuxer = {
226 "vc1",
227 NULL_IF_CONFIG_SMALL("raw VC-1"),
228 0,
229 NULL /* vc1_probe */,
230 ff_raw_video_read_header,
231 ff_raw_read_partial_packet,
232 .extensions = "vc1",
233 .value = CODEC_ID_VC1,
234 };
235 #endif