suppressed mpglib
[libav.git] / libav / raw.c
CommitLineData
de6d9b64
FB
1/*
2 * RAW encoder and decoder
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
de6d9b64
FB
19#include "avformat.h"
20
21/* simple formats */
22int raw_write_header(struct AVFormatContext *s)
23{
24 return 0;
25}
26
27int raw_write_packet(struct AVFormatContext *s,
28 int stream_index,
29 unsigned char *buf, int size)
30{
31 put_buffer(&s->pb, buf, size);
32 put_flush_packet(&s->pb);
33 return 0;
34}
35
36int raw_write_trailer(struct AVFormatContext *s)
37{
38 return 0;
39}
40
41/* raw input */
42static int raw_read_header(AVFormatContext *s,
43 AVFormatParameters *ap)
44{
45 AVStream *st;
46
47 st = malloc(sizeof(AVStream));
48 if (!st)
49 return -1;
50 s->nb_streams = 1;
51 s->streams[0] = st;
52
53 st->id = 0;
54
55 if (ap) {
56 if (s->format->audio_codec != CODEC_ID_NONE) {
57 st->codec.codec_type = CODEC_TYPE_AUDIO;
58 st->codec.codec_id = s->format->audio_codec;
59 } else if (s->format->video_codec != CODEC_ID_NONE) {
60 st->codec.codec_type = CODEC_TYPE_VIDEO;
61 st->codec.codec_id = s->format->video_codec;
62 } else {
63 free(st);
64 return -1;
65 }
66
67 switch(st->codec.codec_type) {
68 case CODEC_TYPE_AUDIO:
69 st->codec.sample_rate = ap->sample_rate;
70 st->codec.channels = ap->channels;
71 /* XXX: endianness */
72 break;
73 case CODEC_TYPE_VIDEO:
74 st->codec.frame_rate = ap->frame_rate;
75 st->codec.width = ap->width;
76 st->codec.height = ap->height;
77 break;
78 default:
27e084bd 79 return -1;
de6d9b64
FB
80 }
81 } else {
27e084bd 82 return -1;
de6d9b64
FB
83 }
84 return 0;
85}
86
2e93e3aa 87#define RAW_PACKET_SIZE 1024
de6d9b64
FB
88
89int raw_read_packet(AVFormatContext *s,
90 AVPacket *pkt)
91{
2e93e3aa 92 int ret;
de6d9b64 93
2e93e3aa 94 if (av_new_packet(pkt, RAW_PACKET_SIZE) < 0)
de6d9b64
FB
95 return -EIO;
96
97 pkt->stream_index = 0;
2e93e3aa
FB
98 ret = get_buffer(&s->pb, pkt->data, RAW_PACKET_SIZE);
99 if (ret <= 0) {
de6d9b64 100 av_free_packet(pkt);
2e93e3aa
FB
101 return -EIO;
102 }
103 /* note: we need to modify the packet size here to handle the last
104 packet */
105 pkt->size = ret;
de6d9b64
FB
106 return ret;
107}
108
109int raw_read_close(AVFormatContext *s)
110{
111 return 0;
112}
113
114/* mp3 read */
115static int mp3_read_header(AVFormatContext *s,
116 AVFormatParameters *ap)
117{
118 AVStream *st;
119
120 st = malloc(sizeof(AVStream));
121 if (!st)
122 return -1;
123 s->nb_streams = 1;
124 s->streams[0] = st;
125
126 st->id = 0;
127
128 st->codec.codec_type = CODEC_TYPE_AUDIO;
129 st->codec.codec_id = CODEC_ID_MP2;
27e084bd 130 /* the parameters will be extracted from the compressed bitstream */
de6d9b64
FB
131 return 0;
132}
133
134/* mpeg1/h263 input */
135static int video_read_header(AVFormatContext *s,
136 AVFormatParameters *ap)
137{
138 AVStream *st;
139
140 st = av_mallocz(sizeof(AVStream));
141 if (!st)
142 return -1;
143 s->nb_streams = 1;
144 s->streams[0] = st;
145
146 st->codec.codec_type = CODEC_TYPE_VIDEO;
147 st->codec.codec_id = s->format->video_codec;
27e084bd
FB
148 /* for mjpeg, specify frame rate */
149 if (st->codec.codec_id == CODEC_ID_MJPEG) {
150 if (ap) {
151 st->codec.frame_rate = ap->frame_rate;
152 } else {
153 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
154 }
155 }
de6d9b64
FB
156 return 0;
157}
158
159AVFormat mp2_format = {
160 "mp2",
161 "MPEG audio",
162 "audio/x-mpeg",
163 "mp2,mp3",
164 CODEC_ID_MP2,
165 0,
166 raw_write_header,
167 raw_write_packet,
168 raw_write_trailer,
169
170 mp3_read_header,
171 raw_read_packet,
172 raw_read_close,
173};
174
175AVFormat ac3_format = {
176 "ac3",
177 "raw ac3",
178 "audio/x-ac3",
179 "ac3",
180 CODEC_ID_AC3,
181 0,
182 raw_write_header,
183 raw_write_packet,
184 raw_write_trailer,
185};
186
187AVFormat h263_format = {
188 "h263",
189 "raw h263",
190 "video/x-h263",
191 "h263",
192 0,
193 CODEC_ID_H263,
194 raw_write_header,
195 raw_write_packet,
196 raw_write_trailer,
197 video_read_header,
198 raw_read_packet,
199 raw_read_close,
200};
201
202AVFormat mpeg1video_format = {
203 "mpegvideo",
204 "MPEG video",
205 "video/x-mpeg",
206 "mpg,mpeg",
207 0,
208 CODEC_ID_MPEG1VIDEO,
209 raw_write_header,
210 raw_write_packet,
211 raw_write_trailer,
212 video_read_header,
213 raw_read_packet,
214 raw_read_close,
215};
216
27e084bd
FB
217AVFormat mjpeg_format = {
218 "mjpeg",
219 "MJPEG video",
220 "video/x-mjpeg",
221 "mjpg,mjpeg",
222 0,
223 CODEC_ID_MJPEG,
224 raw_write_header,
225 raw_write_packet,
226 raw_write_trailer,
227 video_read_header,
228 raw_read_packet,
229 raw_read_close,
230};
231
de6d9b64
FB
232AVFormat pcm_format = {
233 "pcm",
234 "pcm raw format",
235 NULL,
236 "sw",
237 CODEC_ID_PCM,
238 0,
239 raw_write_header,
240 raw_write_packet,
241 raw_write_trailer,
242
243 raw_read_header,
244 raw_read_packet,
245 raw_read_close,
246};
247
248int rawvideo_read_packet(AVFormatContext *s,
249 AVPacket *pkt)
250{
251 int packet_size, ret, width, height;
252 AVStream *st = s->streams[0];
253
254 width = st->codec.width;
255 height = st->codec.height;
256
257 switch(st->codec.pix_fmt) {
258 case PIX_FMT_YUV420P:
259 packet_size = (width * height * 3) / 2;
260 break;
261 case PIX_FMT_YUV422:
262 packet_size = (width * height * 2);
263 break;
264 case PIX_FMT_BGR24:
265 case PIX_FMT_RGB24:
266 packet_size = (width * height * 3);
267 break;
268 default:
269 abort();
270 break;
271 }
272
273 if (av_new_packet(pkt, packet_size) < 0)
274 return -EIO;
275
276 pkt->stream_index = 0;
277 /* bypass buffered I/O */
278 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
279 if (ret != pkt->size) {
280 av_free_packet(pkt);
281 return -EIO;
282 } else {
283 return 0;
284 }
285}
286
287AVFormat rawvideo_format = {
288 "rawvideo",
289 "raw video format",
290 NULL,
291 "yuv",
292 CODEC_ID_NONE,
293 CODEC_ID_RAWVIDEO,
294 raw_write_header,
295 raw_write_packet,
296 raw_write_trailer,
297
298 raw_read_header,
299 rawvideo_read_packet,
300 raw_read_close,
301};