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;
63 av_set_pts_info(st, 64, 1, st->codec.sample_rate);
65 case CODEC_TYPE_VIDEO:
66 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
67 st->codec.width = ap->width;
68 st->codec.height = ap->height;
69 st->codec.pix_fmt = ap->pix_fmt;
70 if(st->codec.pix_fmt == PIX_FMT_NONE)
71 st->codec.pix_fmt= PIX_FMT_YUV420P;
82 #define RAW_PACKET_SIZE 1024
84 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
87 // AVStream *st = s->streams[0];
89 size= RAW_PACKET_SIZE;
91 if (av_new_packet(pkt, size) < 0)
94 pkt->stream_index = 0;
95 ret = get_buffer(&s->pb, pkt->data, size);
100 /* note: we need to modify the packet size here to handle the last
106 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
110 size = RAW_PACKET_SIZE;
112 if (av_new_packet(pkt, size) < 0)
115 pkt->stream_index = 0;
116 ret = get_partial_buffer(&s->pb, pkt->data, size);
125 static int raw_read_close(AVFormatContext *s)
130 int pcm_read_seek(AVFormatContext *s,
131 int stream_index, int64_t timestamp, int flags)
134 int block_align, byte_rate;
138 switch(st->codec.codec_id) {
139 case CODEC_ID_PCM_S16LE:
140 case CODEC_ID_PCM_S16BE:
141 case CODEC_ID_PCM_U16LE:
142 case CODEC_ID_PCM_U16BE:
143 block_align = 2 * st->codec.channels;
144 byte_rate = block_align * st->codec.sample_rate;
146 case CODEC_ID_PCM_S8:
147 case CODEC_ID_PCM_U8:
148 case CODEC_ID_PCM_MULAW:
149 case CODEC_ID_PCM_ALAW:
150 block_align = st->codec.channels;
151 byte_rate = block_align * st->codec.sample_rate;
154 block_align = st->codec.block_align;
155 byte_rate = st->codec.bit_rate / 8;
159 if (block_align <= 0 || byte_rate <= 0)
162 /* compute the position by aligning it to block_align */
163 pos = av_rescale_rnd(timestamp * byte_rate,
165 st->time_base.den * (int64_t)block_align,
166 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
169 /* recompute exact position */
170 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
171 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
176 static int ac3_read_header(AVFormatContext *s,
177 AVFormatParameters *ap)
181 st = av_new_stream(s, 0);
183 return AVERROR_NOMEM;
185 st->codec.codec_type = CODEC_TYPE_AUDIO;
186 st->codec.codec_id = CODEC_ID_AC3;
187 st->need_parsing = 1;
188 /* the parameters will be extracted from the compressed bitstream */
192 static int shorten_read_header(AVFormatContext *s,
193 AVFormatParameters *ap)
197 st = av_new_stream(s, 0);
199 return AVERROR_NOMEM;
200 st->codec.codec_type = CODEC_TYPE_AUDIO;
201 st->codec.codec_id = CODEC_ID_SHORTEN;
202 st->need_parsing = 1;
203 /* the parameters will be extracted from the compressed bitstream */
208 static int dts_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_AUDIO;
218 st->codec.codec_id = CODEC_ID_DTS;
219 st->need_parsing = 1;
220 /* the parameters will be extracted from the compressed bitstream */
224 /* mpeg1/h263 input */
225 static int video_read_header(AVFormatContext *s,
226 AVFormatParameters *ap)
230 st = av_new_stream(s, 0);
232 return AVERROR_NOMEM;
234 st->codec.codec_type = CODEC_TYPE_VIDEO;
235 st->codec.codec_id = s->iformat->value;
236 st->need_parsing = 1;
238 /* for mjpeg, specify frame rate */
239 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
240 if (ap && ap->time_base.num) {
241 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
242 } else if ( st->codec.codec_id == CODEC_ID_MJPEG ||
243 st->codec.codec_id == CODEC_ID_MPEG4 ||
244 st->codec.codec_id == CODEC_ID_H264) {
245 av_set_pts_info(st, 64, 1, 25);
251 #define SEQ_START_CODE 0x000001b3
252 #define GOP_START_CODE 0x000001b8
253 #define PICTURE_START_CODE 0x00000100
255 /* XXX: improve that by looking at several start codes */
256 static int mpegvideo_probe(AVProbeData *p)
261 /* we search the first start code. If it is a sequence, gop or
262 picture start code then we decide it is an mpeg video
263 stream. We do not send highest value to give a chance to mpegts */
264 /* NOTE: the search range was restricted to avoid too many false
270 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
271 if ((code & 0xffffff00) == 0x100) {
272 if (code == SEQ_START_CODE ||
273 code == GOP_START_CODE ||
274 code == PICTURE_START_CODE)
282 static int h263_probe(AVProbeData *p)
290 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
297 static int h261_probe(AVProbeData *p)
305 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
312 AVInputFormat shorten_iformat = {
318 raw_read_partial_packet,
323 AVInputFormat ac3_iformat = {
329 raw_read_partial_packet,
334 #ifdef CONFIG_ENCODERS
335 AVOutputFormat ac3_oformat = {
347 #endif //CONFIG_ENCODERS
349 AVInputFormat dts_iformat = {
355 raw_read_partial_packet,
360 AVInputFormat h261_iformat = {
366 raw_read_partial_packet,
368 .extensions = "h261",
369 .value = CODEC_ID_H261,
372 #ifdef CONFIG_ENCODERS
373 AVOutputFormat h261_oformat = {
385 #endif //CONFIG_ENCODERS
387 AVInputFormat h263_iformat = {
393 raw_read_partial_packet,
395 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
396 .value = CODEC_ID_H263,
399 #ifdef CONFIG_ENCODERS
400 AVOutputFormat h263_oformat = {
412 #endif //CONFIG_ENCODERS
414 AVInputFormat m4v_iformat = {
416 "raw MPEG4 video format",
418 NULL /*mpegvideo_probe*/,
420 raw_read_partial_packet,
422 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
423 .value = CODEC_ID_MPEG4,
426 #ifdef CONFIG_ENCODERS
427 AVOutputFormat m4v_oformat = {
429 "raw MPEG4 video format",
439 #endif //CONFIG_ENCODERS
441 AVInputFormat h264_iformat = {
443 "raw H264 video format",
445 NULL /*mpegvideo_probe*/,
447 raw_read_partial_packet,
449 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
450 .value = CODEC_ID_H264,
453 #ifdef CONFIG_ENCODERS
454 AVOutputFormat h264_oformat = {
456 "raw H264 video format",
466 #endif //CONFIG_ENCODERS
468 AVInputFormat mpegvideo_iformat = {
474 raw_read_partial_packet,
476 .value = CODEC_ID_MPEG1VIDEO,
479 #ifdef CONFIG_ENCODERS
480 AVOutputFormat mpeg1video_oformat = {
492 #endif //CONFIG_ENCODERS
494 #ifdef CONFIG_ENCODERS
495 AVOutputFormat mpeg2video_oformat = {
507 #endif //CONFIG_ENCODERS
509 AVInputFormat mjpeg_iformat = {
515 raw_read_partial_packet,
517 .extensions = "mjpg,mjpeg",
518 .value = CODEC_ID_MJPEG,
521 #ifdef CONFIG_ENCODERS
522 AVOutputFormat mjpeg_oformat = {
534 #endif //CONFIG_ENCODERS
538 #define PCMINPUTDEF(name, long_name, ext, codec) \
539 AVInputFormat pcm_ ## name ## _iformat = {\
552 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
554 #define PCMDEF(name, long_name, ext, codec) \
555 PCMINPUTDEF(name, long_name, ext, codec)
559 #define PCMDEF(name, long_name, ext, codec) \
560 PCMINPUTDEF(name, long_name, ext, codec)\
562 AVOutputFormat pcm_ ## name ## _oformat = {\
574 #endif //CONFIG_ENCODERS
576 #ifdef WORDS_BIGENDIAN
578 #define LE_DEF(s) NULL
580 #define BE_DEF(s) NULL
585 PCMDEF(s16le, "pcm signed 16 bit little endian format",
586 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
588 PCMDEF(s16be, "pcm signed 16 bit big endian format",
589 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
591 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
592 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
594 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
595 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
597 PCMDEF(s8, "pcm signed 8 bit format",
598 "sb", CODEC_ID_PCM_S8)
600 PCMDEF(u8, "pcm unsigned 8 bit format",
601 "ub", CODEC_ID_PCM_U8)
603 PCMDEF(mulaw, "pcm mu law format",
604 "ul", CODEC_ID_PCM_MULAW)
606 PCMDEF(alaw, "pcm A law format",
607 "al", CODEC_ID_PCM_ALAW)
609 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
611 int packet_size, ret, width, height;
612 AVStream *st = s->streams[0];
614 width = st->codec.width;
615 height = st->codec.height;
617 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
621 if (av_new_packet(pkt, packet_size) < 0)
624 pkt->stream_index = 0;
626 /* bypass buffered I/O */
627 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
629 ret = get_buffer(&s->pb, pkt->data, pkt->size);
631 if (ret != pkt->size) {
639 AVInputFormat rawvideo_iformat = {
645 rawvideo_read_packet,
647 .extensions = "yuv,cif,qcif",
648 .value = CODEC_ID_RAWVIDEO,
651 #ifdef CONFIG_ENCODERS
652 AVOutputFormat rawvideo_oformat = {
664 #endif //CONFIG_ENCODERS
666 #ifdef CONFIG_ENCODERS
667 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
672 AVOutputFormat null_oformat = {
678 #ifdef WORDS_BIGENDIAN
687 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
689 #endif //CONFIG_ENCODERS
691 #ifndef CONFIG_ENCODERS
692 #define av_register_output_format(format)
694 #ifndef CONFIG_DECODERS
695 #define av_register_input_format(format)
701 av_register_input_format(&shorten_iformat);
703 av_register_input_format(&ac3_iformat);
704 av_register_output_format(&ac3_oformat);
706 av_register_input_format(&dts_iformat);
708 av_register_input_format(&h261_iformat);
709 av_register_output_format(&h261_oformat);
711 av_register_input_format(&h263_iformat);
712 av_register_output_format(&h263_oformat);
714 av_register_input_format(&m4v_iformat);
715 av_register_output_format(&m4v_oformat);
717 av_register_input_format(&h264_iformat);
718 av_register_output_format(&h264_oformat);
720 av_register_input_format(&mpegvideo_iformat);
721 av_register_output_format(&mpeg1video_oformat);
723 av_register_output_format(&mpeg2video_oformat);
725 av_register_input_format(&mjpeg_iformat);
726 av_register_output_format(&mjpeg_oformat);
728 av_register_input_format(&pcm_s16le_iformat);
729 av_register_output_format(&pcm_s16le_oformat);
730 av_register_input_format(&pcm_s16be_iformat);
731 av_register_output_format(&pcm_s16be_oformat);
732 av_register_input_format(&pcm_u16le_iformat);
733 av_register_output_format(&pcm_u16le_oformat);
734 av_register_input_format(&pcm_u16be_iformat);
735 av_register_output_format(&pcm_u16be_oformat);
736 av_register_input_format(&pcm_s8_iformat);
737 av_register_output_format(&pcm_s8_oformat);
738 av_register_input_format(&pcm_u8_iformat);
739 av_register_output_format(&pcm_u8_oformat);
740 av_register_input_format(&pcm_mulaw_iformat);
741 av_register_output_format(&pcm_mulaw_oformat);
742 av_register_input_format(&pcm_alaw_iformat);
743 av_register_output_format(&pcm_alaw_oformat);
745 av_register_input_format(&rawvideo_iformat);
746 av_register_output_format(&rawvideo_oformat);
748 av_register_output_format(&null_oformat);