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