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