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