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