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 ret= av_get_packet(&s->pb, pkt, size);
93 pkt->stream_index = 0;
97 /* note: we need to modify the packet size here to handle the last
103 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
107 size = RAW_PACKET_SIZE;
109 if (av_new_packet(pkt, size) < 0)
112 pkt->pos= url_ftell(&s->pb);
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 */
190 static int shorten_read_header(AVFormatContext *s,
191 AVFormatParameters *ap)
195 st = av_new_stream(s, 0);
197 return AVERROR_NOMEM;
198 st->codec.codec_type = CODEC_TYPE_AUDIO;
199 st->codec.codec_id = CODEC_ID_SHORTEN;
200 st->need_parsing = 1;
201 /* the parameters will be extracted from the compressed bitstream */
206 static int dts_read_header(AVFormatContext *s,
207 AVFormatParameters *ap)
211 st = av_new_stream(s, 0);
213 return AVERROR_NOMEM;
215 st->codec.codec_type = CODEC_TYPE_AUDIO;
216 st->codec.codec_id = CODEC_ID_DTS;
217 st->need_parsing = 1;
218 /* the parameters will be extracted from the compressed bitstream */
222 /* mpeg1/h263 input */
223 static int video_read_header(AVFormatContext *s,
224 AVFormatParameters *ap)
228 st = av_new_stream(s, 0);
230 return AVERROR_NOMEM;
232 st->codec.codec_type = CODEC_TYPE_VIDEO;
233 st->codec.codec_id = s->iformat->value;
234 st->need_parsing = 1;
236 /* for mjpeg, specify frame rate */
237 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
238 if (ap && ap->time_base.num) {
239 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
240 } else if ( st->codec.codec_id == CODEC_ID_MJPEG ||
241 st->codec.codec_id == CODEC_ID_MPEG4 ||
242 st->codec.codec_id == CODEC_ID_H264) {
243 av_set_pts_info(st, 64, 1, 25);
249 #define SEQ_START_CODE 0x000001b3
250 #define GOP_START_CODE 0x000001b8
251 #define PICTURE_START_CODE 0x00000100
253 /* XXX: improve that by looking at several start codes */
254 static int mpegvideo_probe(AVProbeData *p)
259 /* we search the first start code. If it is a sequence, gop or
260 picture start code then we decide it is an mpeg video
261 stream. We do not send highest value to give a chance to mpegts */
262 /* NOTE: the search range was restricted to avoid too many false
268 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
269 if ((code & 0xffffff00) == 0x100) {
270 if (code == SEQ_START_CODE ||
271 code == GOP_START_CODE ||
272 code == PICTURE_START_CODE)
280 static int h263_probe(AVProbeData *p)
288 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
295 static int h261_probe(AVProbeData *p)
303 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
310 AVInputFormat shorten_iformat = {
316 raw_read_partial_packet,
321 AVInputFormat ac3_iformat = {
327 raw_read_partial_packet,
332 #ifdef CONFIG_ENCODERS
333 AVOutputFormat ac3_oformat = {
345 #endif //CONFIG_ENCODERS
347 AVInputFormat dts_iformat = {
353 raw_read_partial_packet,
358 AVInputFormat h261_iformat = {
364 raw_read_partial_packet,
366 .extensions = "h261",
367 .value = CODEC_ID_H261,
370 #ifdef CONFIG_ENCODERS
371 AVOutputFormat h261_oformat = {
383 #endif //CONFIG_ENCODERS
385 AVInputFormat h263_iformat = {
391 raw_read_partial_packet,
393 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
394 .value = CODEC_ID_H263,
397 #ifdef CONFIG_ENCODERS
398 AVOutputFormat h263_oformat = {
410 #endif //CONFIG_ENCODERS
412 AVInputFormat m4v_iformat = {
414 "raw MPEG4 video format",
416 NULL /*mpegvideo_probe*/,
418 raw_read_partial_packet,
420 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
421 .value = CODEC_ID_MPEG4,
424 #ifdef CONFIG_ENCODERS
425 AVOutputFormat m4v_oformat = {
427 "raw MPEG4 video format",
437 #endif //CONFIG_ENCODERS
439 AVInputFormat h264_iformat = {
441 "raw H264 video format",
443 NULL /*mpegvideo_probe*/,
445 raw_read_partial_packet,
447 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
448 .value = CODEC_ID_H264,
451 #ifdef CONFIG_ENCODERS
452 AVOutputFormat h264_oformat = {
454 "raw H264 video format",
464 #endif //CONFIG_ENCODERS
466 AVInputFormat mpegvideo_iformat = {
472 raw_read_partial_packet,
474 .value = CODEC_ID_MPEG1VIDEO,
477 #ifdef CONFIG_ENCODERS
478 AVOutputFormat mpeg1video_oformat = {
490 #endif //CONFIG_ENCODERS
492 #ifdef CONFIG_ENCODERS
493 AVOutputFormat mpeg2video_oformat = {
505 #endif //CONFIG_ENCODERS
507 AVInputFormat mjpeg_iformat = {
513 raw_read_partial_packet,
515 .extensions = "mjpg,mjpeg",
516 .value = CODEC_ID_MJPEG,
519 #ifdef CONFIG_ENCODERS
520 AVOutputFormat mjpeg_oformat = {
532 #endif //CONFIG_ENCODERS
536 #define PCMINPUTDEF(name, long_name, ext, codec) \
537 AVInputFormat pcm_ ## name ## _iformat = {\
550 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
552 #define PCMDEF(name, long_name, ext, codec) \
553 PCMINPUTDEF(name, long_name, ext, codec)
557 #define PCMDEF(name, long_name, ext, codec) \
558 PCMINPUTDEF(name, long_name, ext, codec)\
560 AVOutputFormat pcm_ ## name ## _oformat = {\
572 #endif //CONFIG_ENCODERS
574 #ifdef WORDS_BIGENDIAN
576 #define LE_DEF(s) NULL
578 #define BE_DEF(s) NULL
583 PCMDEF(s16le, "pcm signed 16 bit little endian format",
584 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
586 PCMDEF(s16be, "pcm signed 16 bit big endian format",
587 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
589 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
590 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
592 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
593 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
595 PCMDEF(s8, "pcm signed 8 bit format",
596 "sb", CODEC_ID_PCM_S8)
598 PCMDEF(u8, "pcm unsigned 8 bit format",
599 "ub", CODEC_ID_PCM_U8)
601 PCMDEF(mulaw, "pcm mu law format",
602 "ul", CODEC_ID_PCM_MULAW)
604 PCMDEF(alaw, "pcm A law format",
605 "al", CODEC_ID_PCM_ALAW)
607 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
609 int packet_size, ret, width, height;
610 AVStream *st = s->streams[0];
612 width = st->codec.width;
613 height = st->codec.height;
615 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
619 ret= av_get_packet(&s->pb, pkt, packet_size);
621 pkt->stream_index = 0;
622 if (ret != packet_size) {
629 AVInputFormat rawvideo_iformat = {
635 rawvideo_read_packet,
637 .extensions = "yuv,cif,qcif",
638 .value = CODEC_ID_RAWVIDEO,
641 #ifdef CONFIG_ENCODERS
642 AVOutputFormat rawvideo_oformat = {
654 #endif //CONFIG_ENCODERS
656 #ifdef CONFIG_ENCODERS
657 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
662 AVOutputFormat null_oformat = {
668 #ifdef WORDS_BIGENDIAN
677 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
679 #endif //CONFIG_ENCODERS
681 #ifndef CONFIG_ENCODERS
682 #define av_register_output_format(format)
684 #ifndef CONFIG_DECODERS
685 #define av_register_input_format(format)
691 av_register_input_format(&shorten_iformat);
693 av_register_input_format(&ac3_iformat);
694 av_register_output_format(&ac3_oformat);
696 av_register_input_format(&dts_iformat);
698 av_register_input_format(&h261_iformat);
699 av_register_output_format(&h261_oformat);
701 av_register_input_format(&h263_iformat);
702 av_register_output_format(&h263_oformat);
704 av_register_input_format(&m4v_iformat);
705 av_register_output_format(&m4v_oformat);
707 av_register_input_format(&h264_iformat);
708 av_register_output_format(&h264_oformat);
710 av_register_input_format(&mpegvideo_iformat);
711 av_register_output_format(&mpeg1video_oformat);
713 av_register_output_format(&mpeg2video_oformat);
715 av_register_input_format(&mjpeg_iformat);
716 av_register_output_format(&mjpeg_oformat);
718 av_register_input_format(&pcm_s16le_iformat);
719 av_register_output_format(&pcm_s16le_oformat);
720 av_register_input_format(&pcm_s16be_iformat);
721 av_register_output_format(&pcm_s16be_oformat);
722 av_register_input_format(&pcm_u16le_iformat);
723 av_register_output_format(&pcm_u16le_oformat);
724 av_register_input_format(&pcm_u16be_iformat);
725 av_register_output_format(&pcm_u16be_oformat);
726 av_register_input_format(&pcm_s8_iformat);
727 av_register_output_format(&pcm_s8_oformat);
728 av_register_input_format(&pcm_u8_iformat);
729 av_register_output_format(&pcm_u8_oformat);
730 av_register_input_format(&pcm_mulaw_iformat);
731 av_register_output_format(&pcm_mulaw_oformat);
732 av_register_input_format(&pcm_alaw_iformat);
733 av_register_output_format(&pcm_alaw_oformat);
735 av_register_input_format(&rawvideo_iformat);
736 av_register_output_format(&rawvideo_oformat);
738 av_register_output_format(&null_oformat);