use parsers
[libav.git] / libavformat / raw.c
CommitLineData
de6d9b64
FB
1/*
2 * RAW encoder and decoder
19720f15 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20
764ef400 21#ifdef CONFIG_ENCODERS
de6d9b64 22/* simple formats */
5c91a675 23static int raw_write_header(struct AVFormatContext *s)
de6d9b64
FB
24{
25 return 0;
26}
27
5c91a675 28static int raw_write_packet(struct AVFormatContext *s, int stream_index,
49057904 29 const uint8_t *buf, int size, int64_t pts)
de6d9b64
FB
30{
31 put_buffer(&s->pb, buf, size);
32 put_flush_packet(&s->pb);
33 return 0;
34}
35
5c91a675 36static int raw_write_trailer(struct AVFormatContext *s)
de6d9b64
FB
37{
38 return 0;
39}
764ef400 40#endif //CONFIG_ENCODERS
de6d9b64
FB
41
42/* raw input */
0c1a9eda 43static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
44{
45 AVStream *st;
c9a65ca8 46 int id;
de6d9b64 47
c9a65ca8 48 st = av_new_stream(s, 0);
de6d9b64 49 if (!st)
c9a65ca8 50 return AVERROR_NOMEM;
de6d9b64 51 if (ap) {
c9a65ca8
FB
52 id = s->iformat->value;
53 if (id == CODEC_ID_RAWVIDEO) {
de6d9b64 54 st->codec.codec_type = CODEC_TYPE_VIDEO;
de6d9b64 55 } else {
c9a65ca8 56 st->codec.codec_type = CODEC_TYPE_AUDIO;
de6d9b64 57 }
c9a65ca8
FB
58 st->codec.codec_id = id;
59
de6d9b64
FB
60 switch(st->codec.codec_type) {
61 case CODEC_TYPE_AUDIO:
62 st->codec.sample_rate = ap->sample_rate;
63 st->codec.channels = ap->channels;
de6d9b64
FB
64 break;
65 case CODEC_TYPE_VIDEO:
14bea432
MN
66 st->codec.frame_rate = ap->frame_rate;
67 st->codec.frame_rate_base = ap->frame_rate_base;
de6d9b64
FB
68 st->codec.width = ap->width;
69 st->codec.height = ap->height;
63167088 70 st->codec.pix_fmt = ap->pix_fmt;
de6d9b64
FB
71 break;
72 default:
27e084bd 73 return -1;
de6d9b64
FB
74 }
75 } else {
27e084bd 76 return -1;
de6d9b64
FB
77 }
78 return 0;
79}
80
2e93e3aa 81#define RAW_PACKET_SIZE 1024
de6d9b64 82
5c91a675 83static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 84{
7866eeff 85 int ret, size;
2a6874fd 86 // AVStream *st = s->streams[0];
7866eeff 87
231dd3f3 88 size= RAW_PACKET_SIZE;
de6d9b64 89
7866eeff 90 if (av_new_packet(pkt, size) < 0)
de6d9b64
FB
91 return -EIO;
92
93 pkt->stream_index = 0;
7866eeff 94 ret = get_buffer(&s->pb, pkt->data, size);
2e93e3aa 95 if (ret <= 0) {
de6d9b64 96 av_free_packet(pkt);
2e93e3aa
FB
97 return -EIO;
98 }
99 /* note: we need to modify the packet size here to handle the last
100 packet */
101 pkt->size = ret;
de6d9b64
FB
102 return ret;
103}
104
5c91a675 105static int raw_read_close(AVFormatContext *s)
de6d9b64
FB
106{
107 return 0;
108}
109
cd66005d
FB
110/* ac3 read */
111static int ac3_read_header(AVFormatContext *s,
112 AVFormatParameters *ap)
113{
114 AVStream *st;
115
116 st = av_new_stream(s, 0);
117 if (!st)
118 return AVERROR_NOMEM;
119
120 st->codec.codec_type = CODEC_TYPE_AUDIO;
121 st->codec.codec_id = CODEC_ID_AC3;
122 /* the parameters will be extracted from the compressed bitstream */
123 return 0;
124}
125
de6d9b64
FB
126/* mpeg1/h263 input */
127static int video_read_header(AVFormatContext *s,
128 AVFormatParameters *ap)
129{
130 AVStream *st;
131
c9a65ca8 132 st = av_new_stream(s, 0);
de6d9b64 133 if (!st)
c9a65ca8 134 return AVERROR_NOMEM;
de6d9b64
FB
135
136 st->codec.codec_type = CODEC_TYPE_VIDEO;
c9a65ca8 137 st->codec.codec_id = s->iformat->value;
27e084bd 138 /* for mjpeg, specify frame rate */
7866eeff
MN
139 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
140 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
27e084bd 141 if (ap) {
14bea432
MN
142 st->codec.frame_rate = ap->frame_rate;
143 st->codec.frame_rate_base = ap->frame_rate_base;
27e084bd 144 } else {
14bea432
MN
145 st->codec.frame_rate = 25;
146 st->codec.frame_rate_base = 1;
27e084bd
FB
147 }
148 }
de6d9b64
FB
149 return 0;
150}
151
c9a65ca8
FB
152#define SEQ_START_CODE 0x000001b3
153#define GOP_START_CODE 0x000001b8
154#define PICTURE_START_CODE 0x00000100
155
156/* XXX: improve that by looking at several start codes */
157static int mpegvideo_probe(AVProbeData *p)
158{
fa777321
FB
159 int code;
160 const uint8_t *d;
c9a65ca8
FB
161
162 /* we search the first start code. If it is a sequence, gop or
163 picture start code then we decide it is an mpeg video
164 stream. We do not send highest value to give a chance to mpegts */
fa777321
FB
165 /* NOTE: the search range was restricted to avoid too many false
166 detections */
167
168 if (p->buf_size < 6)
169 return 0;
170 d = p->buf;
171 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
172 if ((code & 0xffffff00) == 0x100) {
173 if (code == SEQ_START_CODE ||
174 code == GOP_START_CODE ||
175 code == PICTURE_START_CODE)
176 return 50 - 1;
177 else
178 return 0;
c9a65ca8
FB
179 }
180 return 0;
181}
182
d07f9043
MN
183static int h263_probe(AVProbeData *p)
184{
185 int code;
186 const uint8_t *d;
187
188 if (p->buf_size < 6)
189 return 0;
190 d = p->buf;
191 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
192 if (code == 0x20) {
193 return 50;
194 }
195 return 0;
196}
197
c9a65ca8
FB
198AVInputFormat ac3_iformat = {
199 "ac3",
200 "raw ac3",
201 0,
202 NULL,
cd66005d 203 ac3_read_header,
de6d9b64
FB
204 raw_read_packet,
205 raw_read_close,
bb76a117 206 .extensions = "ac3",
de6d9b64
FB
207};
208
764ef400 209#ifdef CONFIG_ENCODERS
c9a65ca8 210AVOutputFormat ac3_oformat = {
de6d9b64
FB
211 "ac3",
212 "raw ac3",
213 "audio/x-ac3",
214 "ac3",
c9a65ca8 215 0,
de6d9b64
FB
216 CODEC_ID_AC3,
217 0,
218 raw_write_header,
219 raw_write_packet,
220 raw_write_trailer,
221};
764ef400 222#endif //CONFIG_ENCODERS
de6d9b64 223
d07f9043
MN
224AVInputFormat h263_iformat = {
225 "h263",
226 "raw h263",
227 0,
228 h263_probe,
229 video_read_header,
230 raw_read_packet,
231 raw_read_close,
232// .extensions = "h263", //FIXME remove after writing mpeg4_probe
233 .value = CODEC_ID_H263,
234};
235
764ef400 236#ifdef CONFIG_ENCODERS
c9a65ca8 237AVOutputFormat h263_oformat = {
de6d9b64
FB
238 "h263",
239 "raw h263",
240 "video/x-h263",
241 "h263",
242 0,
c9a65ca8 243 0,
de6d9b64
FB
244 CODEC_ID_H263,
245 raw_write_header,
246 raw_write_packet,
247 raw_write_trailer,
c9a65ca8 248};
764ef400 249#endif //CONFIG_ENCODERS
c9a65ca8 250
7866eeff
MN
251AVInputFormat m4v_iformat = {
252 "m4v",
253 "raw MPEG4 video format",
254 0,
2f0f5b20 255 NULL /*mpegvideo_probe*/,
7866eeff
MN
256 video_read_header,
257 raw_read_packet,
258 raw_read_close,
bb76a117
MR
259 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
260 .value = CODEC_ID_MPEG4,
7866eeff
MN
261};
262
764ef400 263#ifdef CONFIG_ENCODERS
89b3d7c9
MK
264AVOutputFormat m4v_oformat = {
265 "m4v",
266 "raw MPEG4 video format",
267 NULL,
268 "m4v",
269 0,
270 CODEC_ID_NONE,
271 CODEC_ID_MPEG4,
272 raw_write_header,
273 raw_write_packet,
274 raw_write_trailer,
275};
764ef400 276#endif //CONFIG_ENCODERS
89b3d7c9 277
0da71265
MN
278AVInputFormat h264_iformat = {
279 "h264",
280 "raw H264 video format",
281 0,
282 NULL /*mpegvideo_probe*/,
283 video_read_header,
284 raw_read_packet,
285 raw_read_close,
286 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
287 .value = CODEC_ID_H264,
288};
289
764ef400 290#ifdef CONFIG_ENCODERS
0da71265
MN
291AVOutputFormat h264_oformat = {
292 "h264",
293 "raw H264 video format",
294 NULL,
295 "h264",
296 0,
297 CODEC_ID_NONE,
298 CODEC_ID_H264,
299 raw_write_header,
300 raw_write_packet,
301 raw_write_trailer,
302};
764ef400 303#endif //CONFIG_ENCODERS
0da71265 304
c9a65ca8
FB
305AVInputFormat mpegvideo_iformat = {
306 "mpegvideo",
307 "MPEG video",
308 0,
309 mpegvideo_probe,
de6d9b64
FB
310 video_read_header,
311 raw_read_packet,
312 raw_read_close,
bb76a117 313 .value = CODEC_ID_MPEG1VIDEO,
de6d9b64
FB
314};
315
764ef400 316#ifdef CONFIG_ENCODERS
c9a65ca8
FB
317AVOutputFormat mpeg1video_oformat = {
318 "mpeg1video",
de6d9b64
FB
319 "MPEG video",
320 "video/x-mpeg",
321 "mpg,mpeg",
322 0,
c9a65ca8 323 0,
de6d9b64
FB
324 CODEC_ID_MPEG1VIDEO,
325 raw_write_header,
326 raw_write_packet,
327 raw_write_trailer,
de6d9b64 328};
764ef400 329#endif //CONFIG_ENCODERS
de6d9b64 330
c9a65ca8 331AVInputFormat mjpeg_iformat = {
27e084bd
FB
332 "mjpeg",
333 "MJPEG video",
27e084bd 334 0,
c9a65ca8 335 NULL,
27e084bd
FB
336 video_read_header,
337 raw_read_packet,
338 raw_read_close,
bb76a117
MR
339 .extensions = "mjpg,mjpeg",
340 .value = CODEC_ID_MJPEG,
27e084bd
FB
341};
342
764ef400 343#ifdef CONFIG_ENCODERS
c9a65ca8
FB
344AVOutputFormat mjpeg_oformat = {
345 "mjpeg",
346 "MJPEG video",
347 "video/x-mjpeg",
348 "mjpg,mjpeg",
5ed8fafc 349 0,
5ed8fafc 350 0,
c9a65ca8 351 CODEC_ID_MJPEG,
5ed8fafc
FB
352 raw_write_header,
353 raw_write_packet,
354 raw_write_trailer,
5ed8fafc 355};
764ef400 356#endif //CONFIG_ENCODERS
5ed8fafc 357
c9a65ca8 358/* pcm formats */
764ef400
MM
359#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
360
361#define PCMDEF(name, long_name, ext, codec) \
362AVInputFormat pcm_ ## name ## _iformat = {\
363 #name,\
364 long_name,\
365 0,\
366 NULL,\
367 raw_read_header,\
368 raw_read_packet,\
369 raw_read_close,\
370 .extensions = ext,\
371 .value = codec,\
372};
373
374#else
5ed8fafc 375
c9a65ca8
FB
376#define PCMDEF(name, long_name, ext, codec) \
377AVInputFormat pcm_ ## name ## _iformat = {\
378 #name,\
379 long_name,\
380 0,\
381 NULL,\
382 raw_read_header,\
383 raw_read_packet,\
384 raw_read_close,\
bb76a117
MR
385 .extensions = ext,\
386 .value = codec,\
c9a65ca8
FB
387};\
388\
389AVOutputFormat pcm_ ## name ## _oformat = {\
390 #name,\
391 long_name,\
392 NULL,\
393 ext,\
394 0,\
395 codec,\
396 0,\
397 raw_write_header,\
398 raw_write_packet,\
399 raw_write_trailer,\
5ed8fafc 400};
764ef400 401#endif //CONFIG_ENCODERS
5ed8fafc 402
5ed8fafc 403#ifdef WORDS_BIGENDIAN
c9a65ca8
FB
404#define BE_DEF(s) s
405#define LE_DEF(s) NULL
5ed8fafc 406#else
c9a65ca8
FB
407#define BE_DEF(s) NULL
408#define LE_DEF(s) s
5ed8fafc 409#endif
5ed8fafc 410
5ed8fafc 411
c9a65ca8
FB
412PCMDEF(s16le, "pcm signed 16 bit little endian format",
413 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
5ed8fafc 414
c9a65ca8
FB
415PCMDEF(s16be, "pcm signed 16 bit big endian format",
416 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
5ed8fafc 417
c9a65ca8
FB
418PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
419 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
5ed8fafc 420
c9a65ca8
FB
421PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
422 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
5ed8fafc 423
c9a65ca8
FB
424PCMDEF(s8, "pcm signed 8 bit format",
425 "sb", CODEC_ID_PCM_S8)
5ed8fafc 426
c9a65ca8
FB
427PCMDEF(u8, "pcm unsigned 8 bit format",
428 "ub", CODEC_ID_PCM_U8)
5ed8fafc 429
c9a65ca8
FB
430PCMDEF(mulaw, "pcm mu law format",
431 "ul", CODEC_ID_PCM_MULAW)
de6d9b64 432
c9a65ca8
FB
433PCMDEF(alaw, "pcm A law format",
434 "al", CODEC_ID_PCM_ALAW)
de6d9b64 435
5c91a675 436static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
437{
438 int packet_size, ret, width, height;
439 AVStream *st = s->streams[0];
440
441 width = st->codec.width;
442 height = st->codec.height;
443
63167088
RS
444 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
445 if (packet_size < 0)
42343f7e 446 av_abort();
de6d9b64
FB
447
448 if (av_new_packet(pkt, packet_size) < 0)
449 return -EIO;
450
451 pkt->stream_index = 0;
c2c2cd2d 452#if 0
de6d9b64
FB
453 /* bypass buffered I/O */
454 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
c2c2cd2d
MN
455#else
456 ret = get_buffer(&s->pb, pkt->data, pkt->size);
457#endif
de6d9b64
FB
458 if (ret != pkt->size) {
459 av_free_packet(pkt);
460 return -EIO;
461 } else {
462 return 0;
463 }
464}
465
c9a65ca8
FB
466AVInputFormat rawvideo_iformat = {
467 "rawvideo",
468 "raw video format",
469 0,
470 NULL,
471 raw_read_header,
472 rawvideo_read_packet,
473 raw_read_close,
bb76a117
MR
474 .extensions = "yuv",
475 .value = CODEC_ID_RAWVIDEO,
c9a65ca8
FB
476};
477
764ef400 478#ifdef CONFIG_ENCODERS
c9a65ca8 479AVOutputFormat rawvideo_oformat = {
de6d9b64
FB
480 "rawvideo",
481 "raw video format",
482 NULL,
483 "yuv",
c9a65ca8 484 0,
de6d9b64
FB
485 CODEC_ID_NONE,
486 CODEC_ID_RAWVIDEO,
487 raw_write_header,
488 raw_write_packet,
489 raw_write_trailer,
de6d9b64 490};
764ef400 491#endif //CONFIG_ENCODERS
c9a65ca8 492
764ef400 493#ifdef CONFIG_ENCODERS
87bdd3e5
FB
494static int null_write_packet(struct AVFormatContext *s,
495 int stream_index,
49057904 496 const uint8_t *buf, int size, int64_t pts)
87bdd3e5
FB
497{
498 return 0;
499}
500
501AVOutputFormat null_oformat = {
502 "null",
503 "null video format",
504 NULL,
505 NULL,
506 0,
507#ifdef WORDS_BIGENDIAN
508 CODEC_ID_PCM_S16BE,
509#else
510 CODEC_ID_PCM_S16LE,
511#endif
512 CODEC_ID_RAWVIDEO,
513 raw_write_header,
514 null_write_packet,
515 raw_write_trailer,
b1d89f82 516 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
87bdd3e5 517};
764ef400
MM
518#endif //CONFIG_ENCODERS
519
520#ifndef CONFIG_ENCODERS
521#define av_register_output_format(format)
522#endif
523#ifndef CONFIG_DECODERS
524#define av_register_input_format(format)
525#endif
87bdd3e5 526
c9a65ca8
FB
527int raw_init(void)
528{
c9a65ca8
FB
529 av_register_input_format(&ac3_iformat);
530 av_register_output_format(&ac3_oformat);
531
d07f9043 532 av_register_input_format(&h263_iformat);
c9a65ca8 533 av_register_output_format(&h263_oformat);
89b3d7c9 534
7866eeff 535 av_register_input_format(&m4v_iformat);
89b3d7c9 536 av_register_output_format(&m4v_oformat);
0da71265
MN
537
538 av_register_input_format(&h264_iformat);
539 av_register_output_format(&h264_oformat);
c9a65ca8
FB
540
541 av_register_input_format(&mpegvideo_iformat);
542 av_register_output_format(&mpeg1video_oformat);
543
544 av_register_input_format(&mjpeg_iformat);
545 av_register_output_format(&mjpeg_oformat);
546
547 av_register_input_format(&pcm_s16le_iformat);
548 av_register_output_format(&pcm_s16le_oformat);
549 av_register_input_format(&pcm_s16be_iformat);
550 av_register_output_format(&pcm_s16be_oformat);
551 av_register_input_format(&pcm_u16le_iformat);
552 av_register_output_format(&pcm_u16le_oformat);
553 av_register_input_format(&pcm_u16be_iformat);
554 av_register_output_format(&pcm_u16be_oformat);
555 av_register_input_format(&pcm_s8_iformat);
556 av_register_output_format(&pcm_s8_oformat);
557 av_register_input_format(&pcm_u8_iformat);
558 av_register_output_format(&pcm_u8_oformat);
559 av_register_input_format(&pcm_mulaw_iformat);
560 av_register_output_format(&pcm_mulaw_oformat);
561 av_register_input_format(&pcm_alaw_iformat);
562 av_register_output_format(&pcm_alaw_oformat);
563
564 av_register_input_format(&rawvideo_iformat);
565 av_register_output_format(&rawvideo_oformat);
87bdd3e5
FB
566
567 av_register_output_format(&null_oformat);
c9a65ca8
FB
568 return 0;
569}