2 * RAW encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
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.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
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
21 #ifdef CONFIG_ENCODERS
23 static int raw_write_header(struct AVFormatContext *s)
28 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
30 put_buffer(&s->pb, pkt->data, pkt->size);
31 put_flush_packet(&s->pb);
35 static int raw_write_trailer(struct AVFormatContext *s)
39 #endif //CONFIG_ENCODERS
42 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
47 st = av_new_stream(s, 0);
51 id = s->iformat->value;
52 if (id == CODEC_ID_RAWVIDEO) {
53 st->codec.codec_type = CODEC_TYPE_VIDEO;
55 st->codec.codec_type = CODEC_TYPE_AUDIO;
57 st->codec.codec_id = id;
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;
64 case CODEC_TYPE_VIDEO:
65 st->codec.frame_rate = ap->frame_rate;
66 st->codec.frame_rate_base = ap->frame_rate_base;
67 st->codec.width = ap->width;
68 st->codec.height = ap->height;
69 st->codec.pix_fmt = ap->pix_fmt;
80 #define RAW_PACKET_SIZE 1024
82 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
85 // AVStream *st = s->streams[0];
87 size= RAW_PACKET_SIZE;
89 if (av_new_packet(pkt, size) < 0)
92 pkt->stream_index = 0;
93 ret = get_buffer(&s->pb, pkt->data, size);
98 /* note: we need to modify the packet size here to handle the last
104 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
108 size = RAW_PACKET_SIZE;
110 if (av_new_packet(pkt, size) < 0)
113 pkt->stream_index = 0;
114 ret = get_partial_buffer(&s->pb, pkt->data, size);
123 static int raw_read_close(AVFormatContext *s)
128 int pcm_read_seek(AVFormatContext *s,
129 int stream_index, int64_t timestamp, int flags)
132 int block_align, byte_rate;
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;
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;
152 block_align = st->codec.block_align;
153 byte_rate = st->codec.bit_rate / 8;
157 if (block_align <= 0 || byte_rate <= 0)
160 /* compute the position by aligning it to block_align */
161 pos = av_rescale_rnd(timestamp * byte_rate,
163 st->time_base.den * (int64_t)block_align,
164 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
167 /* recompute exact position */
168 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
169 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
174 static int ac3_read_header(AVFormatContext *s,
175 AVFormatParameters *ap)
179 st = av_new_stream(s, 0);
181 return AVERROR_NOMEM;
183 st->codec.codec_type = CODEC_TYPE_AUDIO;
184 st->codec.codec_id = CODEC_ID_AC3;
185 st->need_parsing = 1;
186 /* the parameters will be extracted from the compressed bitstream */
191 static int dts_read_header(AVFormatContext *s,
192 AVFormatParameters *ap)
196 st = av_new_stream(s, 0);
198 return AVERROR_NOMEM;
200 st->codec.codec_type = CODEC_TYPE_AUDIO;
201 st->codec.codec_id = CODEC_ID_DTS;
202 st->need_parsing = 1;
203 /* the parameters will be extracted from the compressed bitstream */
207 /* mpeg1/h263 input */
208 static int video_read_header(AVFormatContext *s,
209 AVFormatParameters *ap)
213 st = av_new_stream(s, 0);
215 return AVERROR_NOMEM;
217 st->codec.codec_type = CODEC_TYPE_VIDEO;
218 st->codec.codec_id = s->iformat->value;
219 st->need_parsing = 1;
221 /* for mjpeg, specify frame rate */
222 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
223 if (st->codec.codec_id == CODEC_ID_MJPEG ||
224 st->codec.codec_id == CODEC_ID_MPEG4) {
225 if (ap && ap->frame_rate) {
226 st->codec.frame_rate = ap->frame_rate;
227 st->codec.frame_rate_base = ap->frame_rate_base;
229 st->codec.frame_rate = 25;
230 st->codec.frame_rate_base = 1;
236 #define SEQ_START_CODE 0x000001b3
237 #define GOP_START_CODE 0x000001b8
238 #define PICTURE_START_CODE 0x00000100
240 /* XXX: improve that by looking at several start codes */
241 static int mpegvideo_probe(AVProbeData *p)
246 /* we search the first start code. If it is a sequence, gop or
247 picture start code then we decide it is an mpeg video
248 stream. We do not send highest value to give a chance to mpegts */
249 /* NOTE: the search range was restricted to avoid too many false
255 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
256 if ((code & 0xffffff00) == 0x100) {
257 if (code == SEQ_START_CODE ||
258 code == GOP_START_CODE ||
259 code == PICTURE_START_CODE)
267 static int h263_probe(AVProbeData *p)
275 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
282 static int h261_probe(AVProbeData *p)
290 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
297 AVInputFormat ac3_iformat = {
303 raw_read_partial_packet,
308 #ifdef CONFIG_ENCODERS
309 AVOutputFormat ac3_oformat = {
321 #endif //CONFIG_ENCODERS
323 AVInputFormat dts_iformat = {
329 raw_read_partial_packet,
334 AVInputFormat h261_iformat = {
340 raw_read_partial_packet,
342 .extensions = "h261",
343 .value = CODEC_ID_H261,
346 AVInputFormat h263_iformat = {
352 raw_read_partial_packet,
354 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
355 .value = CODEC_ID_H263,
358 #ifdef CONFIG_ENCODERS
359 AVOutputFormat h263_oformat = {
371 #endif //CONFIG_ENCODERS
373 AVInputFormat m4v_iformat = {
375 "raw MPEG4 video format",
377 NULL /*mpegvideo_probe*/,
379 raw_read_partial_packet,
381 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
382 .value = CODEC_ID_MPEG4,
385 #ifdef CONFIG_ENCODERS
386 AVOutputFormat m4v_oformat = {
388 "raw MPEG4 video format",
398 #endif //CONFIG_ENCODERS
400 AVInputFormat h264_iformat = {
402 "raw H264 video format",
404 NULL /*mpegvideo_probe*/,
406 raw_read_partial_packet,
408 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
409 .value = CODEC_ID_H264,
412 #ifdef CONFIG_ENCODERS
413 AVOutputFormat h264_oformat = {
415 "raw H264 video format",
425 #endif //CONFIG_ENCODERS
427 AVInputFormat mpegvideo_iformat = {
433 raw_read_partial_packet,
435 .value = CODEC_ID_MPEG1VIDEO,
438 #ifdef CONFIG_ENCODERS
439 AVOutputFormat mpeg1video_oformat = {
451 #endif //CONFIG_ENCODERS
453 AVInputFormat mjpeg_iformat = {
459 raw_read_partial_packet,
461 .extensions = "mjpg,mjpeg",
462 .value = CODEC_ID_MJPEG,
465 #ifdef CONFIG_ENCODERS
466 AVOutputFormat mjpeg_oformat = {
478 #endif //CONFIG_ENCODERS
482 #define PCMINPUTDEF(name, long_name, ext, codec) \
483 AVInputFormat pcm_ ## name ## _iformat = {\
496 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
498 #define PCMDEF(name, long_name, ext, codec) \
499 PCMINPUTDEF(name, long_name, ext, codec)
503 #define PCMDEF(name, long_name, ext, codec) \
504 PCMINPUTDEF(name, long_name, ext, codec)\
506 AVOutputFormat pcm_ ## name ## _oformat = {\
518 #endif //CONFIG_ENCODERS
520 #ifdef WORDS_BIGENDIAN
522 #define LE_DEF(s) NULL
524 #define BE_DEF(s) NULL
529 PCMDEF(s16le, "pcm signed 16 bit little endian format",
530 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
532 PCMDEF(s16be, "pcm signed 16 bit big endian format",
533 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
535 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
536 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
538 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
539 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
541 PCMDEF(s8, "pcm signed 8 bit format",
542 "sb", CODEC_ID_PCM_S8)
544 PCMDEF(u8, "pcm unsigned 8 bit format",
545 "ub", CODEC_ID_PCM_U8)
547 PCMDEF(mulaw, "pcm mu law format",
548 "ul", CODEC_ID_PCM_MULAW)
550 PCMDEF(alaw, "pcm A law format",
551 "al", CODEC_ID_PCM_ALAW)
553 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
555 int packet_size, ret, width, height;
556 AVStream *st = s->streams[0];
558 width = st->codec.width;
559 height = st->codec.height;
561 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
565 if (av_new_packet(pkt, packet_size) < 0)
568 pkt->stream_index = 0;
570 /* bypass buffered I/O */
571 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
573 ret = get_buffer(&s->pb, pkt->data, pkt->size);
575 if (ret != pkt->size) {
583 AVInputFormat rawvideo_iformat = {
589 rawvideo_read_packet,
592 .value = CODEC_ID_RAWVIDEO,
595 #ifdef CONFIG_ENCODERS
596 AVOutputFormat rawvideo_oformat = {
608 #endif //CONFIG_ENCODERS
610 #ifdef CONFIG_ENCODERS
611 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
616 AVOutputFormat null_oformat = {
622 #ifdef WORDS_BIGENDIAN
631 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
633 #endif //CONFIG_ENCODERS
635 #ifndef CONFIG_ENCODERS
636 #define av_register_output_format(format)
638 #ifndef CONFIG_DECODERS
639 #define av_register_input_format(format)
644 av_register_input_format(&ac3_iformat);
645 av_register_output_format(&ac3_oformat);
647 av_register_input_format(&dts_iformat);
649 av_register_input_format(&h261_iformat);
651 av_register_input_format(&h263_iformat);
652 av_register_output_format(&h263_oformat);
654 av_register_input_format(&m4v_iformat);
655 av_register_output_format(&m4v_oformat);
657 av_register_input_format(&h264_iformat);
658 av_register_output_format(&h264_oformat);
660 av_register_input_format(&mpegvideo_iformat);
661 av_register_output_format(&mpeg1video_oformat);
663 av_register_input_format(&mjpeg_iformat);
664 av_register_output_format(&mjpeg_oformat);
666 av_register_input_format(&pcm_s16le_iformat);
667 av_register_output_format(&pcm_s16le_oformat);
668 av_register_input_format(&pcm_s16be_iformat);
669 av_register_output_format(&pcm_s16be_oformat);
670 av_register_input_format(&pcm_u16le_iformat);
671 av_register_output_format(&pcm_u16le_oformat);
672 av_register_input_format(&pcm_u16be_iformat);
673 av_register_output_format(&pcm_u16be_oformat);
674 av_register_input_format(&pcm_s8_iformat);
675 av_register_output_format(&pcm_s8_oformat);
676 av_register_input_format(&pcm_u8_iformat);
677 av_register_output_format(&pcm_u8_oformat);
678 av_register_input_format(&pcm_mulaw_iformat);
679 av_register_output_format(&pcm_mulaw_oformat);
680 av_register_input_format(&pcm_alaw_iformat);
681 av_register_output_format(&pcm_alaw_oformat);
683 av_register_input_format(&rawvideo_iformat);
684 av_register_output_format(&rawvideo_oformat);
686 av_register_output_format(&null_oformat);