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