suppressed mpeg demux mess - use now dynamic stream creation api
[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,
10bb7023 29 unsigned char *buf, int size, int force_pts)
de6d9b64
FB
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
06542267 47 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
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 {
06542267 63 av_free(st);
de6d9b64
FB
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;
de6d9b64
FB
71 break;
72 case CODEC_TYPE_VIDEO:
73 st->codec.frame_rate = ap->frame_rate;
74 st->codec.width = ap->width;
75 st->codec.height = ap->height;
76 break;
77 default:
27e084bd 78 return -1;
de6d9b64
FB
79 }
80 } else {
27e084bd 81 return -1;
de6d9b64
FB
82 }
83 return 0;
84}
85
2e93e3aa 86#define RAW_PACKET_SIZE 1024
de6d9b64
FB
87
88int raw_read_packet(AVFormatContext *s,
89 AVPacket *pkt)
90{
2e93e3aa 91 int ret;
de6d9b64 92
2e93e3aa 93 if (av_new_packet(pkt, RAW_PACKET_SIZE) < 0)
de6d9b64
FB
94 return -EIO;
95
96 pkt->stream_index = 0;
2e93e3aa
FB
97 ret = get_buffer(&s->pb, pkt->data, RAW_PACKET_SIZE);
98 if (ret <= 0) {
de6d9b64 99 av_free_packet(pkt);
2e93e3aa
FB
100 return -EIO;
101 }
102 /* note: we need to modify the packet size here to handle the last
103 packet */
104 pkt->size = ret;
de6d9b64
FB
105 return ret;
106}
107
108int raw_read_close(AVFormatContext *s)
109{
110 return 0;
111}
112
113/* mp3 read */
114static int mp3_read_header(AVFormatContext *s,
115 AVFormatParameters *ap)
116{
117 AVStream *st;
118
06542267 119 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
120 if (!st)
121 return -1;
122 s->nb_streams = 1;
123 s->streams[0] = st;
124
125 st->id = 0;
126
127 st->codec.codec_type = CODEC_TYPE_AUDIO;
128 st->codec.codec_id = CODEC_ID_MP2;
27e084bd 129 /* the parameters will be extracted from the compressed bitstream */
de6d9b64
FB
130 return 0;
131}
132
133/* mpeg1/h263 input */
134static int video_read_header(AVFormatContext *s,
135 AVFormatParameters *ap)
136{
137 AVStream *st;
138
139 st = av_mallocz(sizeof(AVStream));
140 if (!st)
141 return -1;
142 s->nb_streams = 1;
143 s->streams[0] = st;
144
145 st->codec.codec_type = CODEC_TYPE_VIDEO;
146 st->codec.codec_id = s->format->video_codec;
27e084bd
FB
147 /* for mjpeg, specify frame rate */
148 if (st->codec.codec_id == CODEC_ID_MJPEG) {
149 if (ap) {
150 st->codec.frame_rate = ap->frame_rate;
151 } else {
152 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
153 }
154 }
de6d9b64
FB
155 return 0;
156}
157
158AVFormat mp2_format = {
159 "mp2",
160 "MPEG audio",
161 "audio/x-mpeg",
162 "mp2,mp3",
163 CODEC_ID_MP2,
164 0,
165 raw_write_header,
166 raw_write_packet,
167 raw_write_trailer,
168
169 mp3_read_header,
170 raw_read_packet,
171 raw_read_close,
172};
173
174AVFormat ac3_format = {
175 "ac3",
176 "raw ac3",
177 "audio/x-ac3",
178 "ac3",
179 CODEC_ID_AC3,
180 0,
181 raw_write_header,
182 raw_write_packet,
183 raw_write_trailer,
30b68f33
ZK
184 raw_read_header,
185 raw_read_packet,
186 raw_read_close,
de6d9b64
FB
187};
188
189AVFormat h263_format = {
190 "h263",
191 "raw h263",
192 "video/x-h263",
193 "h263",
194 0,
195 CODEC_ID_H263,
196 raw_write_header,
197 raw_write_packet,
198 raw_write_trailer,
199 video_read_header,
200 raw_read_packet,
201 raw_read_close,
202};
203
204AVFormat mpeg1video_format = {
205 "mpegvideo",
206 "MPEG video",
207 "video/x-mpeg",
208 "mpg,mpeg",
209 0,
210 CODEC_ID_MPEG1VIDEO,
211 raw_write_header,
212 raw_write_packet,
213 raw_write_trailer,
214 video_read_header,
215 raw_read_packet,
216 raw_read_close,
217};
218
27e084bd
FB
219AVFormat mjpeg_format = {
220 "mjpeg",
221 "MJPEG video",
222 "video/x-mjpeg",
223 "mjpg,mjpeg",
224 0,
225 CODEC_ID_MJPEG,
226 raw_write_header,
227 raw_write_packet,
228 raw_write_trailer,
229 video_read_header,
230 raw_read_packet,
231 raw_read_close,
232};
233
5ed8fafc
FB
234/* pcm formats */
235
236AVFormat pcm_s16le_format = {
237 "s16le",
238 "pcm signed 16 bit little endian format",
de6d9b64 239 NULL,
5ed8fafc
FB
240#ifdef WORDS_BIGENDIAN
241 "",
242#else
de6d9b64 243 "sw",
5ed8fafc
FB
244#endif
245 CODEC_ID_PCM_S16LE,
246 0,
247 raw_write_header,
248 raw_write_packet,
249 raw_write_trailer,
250
06542267 251 raw_read_header,
5ed8fafc
FB
252 raw_read_packet,
253 raw_read_close,
254};
255
256AVFormat pcm_s16be_format = {
257 "s16be",
258 "pcm signed 16 bit big endian format",
259 NULL,
260#ifdef WORDS_BIGENDIAN
261 "sw",
262#else
263 "",
264#endif
265 CODEC_ID_PCM_S16BE,
266 0,
267 raw_write_header,
268 raw_write_packet,
269 raw_write_trailer,
270
06542267 271 raw_read_header,
5ed8fafc
FB
272 raw_read_packet,
273 raw_read_close,
274};
275
276AVFormat pcm_u16le_format = {
277 "u16le",
278 "pcm unsigned 16 bit little endian format",
279 NULL,
280#ifdef WORDS_BIGENDIAN
281 "",
282#else
283 "uw",
284#endif
285 CODEC_ID_PCM_U16LE,
286 0,
287 raw_write_header,
288 raw_write_packet,
289 raw_write_trailer,
290
06542267 291 raw_read_header,
5ed8fafc
FB
292 raw_read_packet,
293 raw_read_close,
294};
295
296AVFormat pcm_u16be_format = {
297 "u16be",
298 "pcm unsigned 16 bit big endian format",
299 NULL,
300#ifdef WORDS_BIGENDIAN
301 "uw",
302#else
303 "",
304#endif
305 CODEC_ID_PCM_U16BE,
306 0,
307 raw_write_header,
308 raw_write_packet,
309 raw_write_trailer,
310
06542267 311 raw_read_header,
5ed8fafc
FB
312 raw_read_packet,
313 raw_read_close,
314};
315
316AVFormat pcm_s8_format = {
317 "s8",
318 "pcm signed 8 bit format",
319 NULL,
320 "sb",
321 CODEC_ID_PCM_S8,
322 0,
323 raw_write_header,
324 raw_write_packet,
325 raw_write_trailer,
326
06542267 327 raw_read_header,
5ed8fafc
FB
328 raw_read_packet,
329 raw_read_close,
330};
331
332AVFormat pcm_u8_format = {
333 "u8",
334 "pcm unsigned 8 bit format",
335 NULL,
336 "ub",
337 CODEC_ID_PCM_U8,
338 0,
339 raw_write_header,
340 raw_write_packet,
341 raw_write_trailer,
342
06542267 343 raw_read_header,
5ed8fafc
FB
344 raw_read_packet,
345 raw_read_close,
346};
347
348AVFormat pcm_mulaw_format = {
349 "mulaw",
350 "pcm mu law format",
351 NULL,
352 "ul",
353 CODEC_ID_PCM_MULAW,
354 0,
355 raw_write_header,
356 raw_write_packet,
357 raw_write_trailer,
358
06542267 359 raw_read_header,
5ed8fafc
FB
360 raw_read_packet,
361 raw_read_close,
362};
363
364AVFormat pcm_alaw_format = {
365 "alaw",
366 "pcm A law format",
367 NULL,
368 "al",
369 CODEC_ID_PCM_ALAW,
de6d9b64
FB
370 0,
371 raw_write_header,
372 raw_write_packet,
373 raw_write_trailer,
374
06542267 375 raw_read_header,
de6d9b64
FB
376 raw_read_packet,
377 raw_read_close,
378};
379
380int rawvideo_read_packet(AVFormatContext *s,
381 AVPacket *pkt)
382{
383 int packet_size, ret, width, height;
384 AVStream *st = s->streams[0];
385
386 width = st->codec.width;
387 height = st->codec.height;
388
389 switch(st->codec.pix_fmt) {
390 case PIX_FMT_YUV420P:
391 packet_size = (width * height * 3) / 2;
392 break;
393 case PIX_FMT_YUV422:
394 packet_size = (width * height * 2);
395 break;
396 case PIX_FMT_BGR24:
397 case PIX_FMT_RGB24:
398 packet_size = (width * height * 3);
399 break;
400 default:
401 abort();
402 break;
403 }
404
405 if (av_new_packet(pkt, packet_size) < 0)
406 return -EIO;
407
408 pkt->stream_index = 0;
409 /* bypass buffered I/O */
410 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
411 if (ret != pkt->size) {
412 av_free_packet(pkt);
413 return -EIO;
414 } else {
415 return 0;
416 }
417}
418
419AVFormat rawvideo_format = {
420 "rawvideo",
421 "raw video format",
422 NULL,
423 "yuv",
424 CODEC_ID_NONE,
425 CODEC_ID_RAWVIDEO,
426 raw_write_header,
427 raw_write_packet,
428 raw_write_trailer,
429
430 raw_read_header,
431 rawvideo_read_packet,
432 raw_read_close,
433};