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