2 * RAW encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2005 Alex Beregszaszi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 static int raw_write_header(struct AVFormatContext *s)
29 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
31 put_buffer(&s->pb, pkt->data, pkt->size);
32 put_flush_packet(&s->pb);
36 static int raw_write_trailer(struct AVFormatContext *s)
40 #endif //CONFIG_MUXERS
43 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
48 st = av_new_stream(s, 0);
52 id = s->iformat->value;
53 if (id == CODEC_ID_RAWVIDEO) {
54 st->codec->codec_type = CODEC_TYPE_VIDEO;
56 st->codec->codec_type = CODEC_TYPE_AUDIO;
58 st->codec->codec_id = id;
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;
64 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
66 case CODEC_TYPE_VIDEO:
67 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
68 st->codec->width = ap->width;
69 st->codec->height = ap->height;
70 st->codec->pix_fmt = ap->pix_fmt;
71 if(st->codec->pix_fmt == PIX_FMT_NONE)
72 st->codec->pix_fmt= PIX_FMT_YUV420P;
83 #define RAW_PACKET_SIZE 1024
85 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
88 // AVStream *st = s->streams[0];
90 size= RAW_PACKET_SIZE;
92 ret= av_get_packet(&s->pb, pkt, size);
94 pkt->stream_index = 0;
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->pos= url_ftell(&s->pb);
114 pkt->stream_index = 0;
115 ret = get_partial_buffer(&s->pb, pkt->data, size);
124 // http://www.artificis.hu/files/texts/ingenient.txt
125 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
127 int ret, size, w, h, unk1, unk2;
129 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
130 return AVERROR_IO; // FIXME
132 size = get_le32(&s->pb);
134 w = get_le16(&s->pb);
135 h = get_le16(&s->pb);
137 url_fskip(&s->pb, 8); // zero + size (padded?)
138 url_fskip(&s->pb, 2);
139 unk1 = get_le16(&s->pb);
140 unk2 = get_le16(&s->pb);
141 url_fskip(&s->pb, 22); // ascii timestamp
143 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
144 size, w, h, unk1, unk2);
146 if (av_new_packet(pkt, size) < 0)
149 pkt->pos = url_ftell(&s->pb);
150 pkt->stream_index = 0;
151 ret = get_buffer(&s->pb, pkt->data, size);
160 static int raw_read_close(AVFormatContext *s)
165 int pcm_read_seek(AVFormatContext *s,
166 int stream_index, int64_t timestamp, int flags)
169 int block_align, byte_rate;
173 switch(st->codec->codec_id) {
174 case CODEC_ID_PCM_S16LE:
175 case CODEC_ID_PCM_S16BE:
176 case CODEC_ID_PCM_U16LE:
177 case CODEC_ID_PCM_U16BE:
178 block_align = 2 * st->codec->channels;
179 byte_rate = block_align * st->codec->sample_rate;
181 case CODEC_ID_PCM_S8:
182 case CODEC_ID_PCM_U8:
183 case CODEC_ID_PCM_MULAW:
184 case CODEC_ID_PCM_ALAW:
185 block_align = st->codec->channels;
186 byte_rate = block_align * st->codec->sample_rate;
189 block_align = st->codec->block_align;
190 byte_rate = st->codec->bit_rate / 8;
194 if (block_align <= 0 || byte_rate <= 0)
197 /* compute the position by aligning it to block_align */
198 pos = av_rescale_rnd(timestamp * byte_rate,
200 st->time_base.den * (int64_t)block_align,
201 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
204 /* recompute exact position */
205 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
206 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
211 static int ac3_read_header(AVFormatContext *s,
212 AVFormatParameters *ap)
216 st = av_new_stream(s, 0);
218 return AVERROR_NOMEM;
220 st->codec->codec_type = CODEC_TYPE_AUDIO;
221 st->codec->codec_id = CODEC_ID_AC3;
222 st->need_parsing = 1;
223 /* the parameters will be extracted from the compressed bitstream */
227 static int shorten_read_header(AVFormatContext *s,
228 AVFormatParameters *ap)
232 st = av_new_stream(s, 0);
234 return AVERROR_NOMEM;
235 st->codec->codec_type = CODEC_TYPE_AUDIO;
236 st->codec->codec_id = CODEC_ID_SHORTEN;
237 st->need_parsing = 1;
238 /* the parameters will be extracted from the compressed bitstream */
243 static int dts_read_header(AVFormatContext *s,
244 AVFormatParameters *ap)
248 st = av_new_stream(s, 0);
250 return AVERROR_NOMEM;
252 st->codec->codec_type = CODEC_TYPE_AUDIO;
253 st->codec->codec_id = CODEC_ID_DTS;
254 st->need_parsing = 1;
255 /* the parameters will be extracted from the compressed bitstream */
259 /* mpeg1/h263 input */
260 static int video_read_header(AVFormatContext *s,
261 AVFormatParameters *ap)
265 st = av_new_stream(s, 0);
267 return AVERROR_NOMEM;
269 st->codec->codec_type = CODEC_TYPE_VIDEO;
270 st->codec->codec_id = s->iformat->value;
271 st->need_parsing = 1;
273 /* for mjpeg, specify frame rate */
274 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
275 if (ap && ap->time_base.num) {
276 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
277 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
278 st->codec->codec_id == CODEC_ID_MPEG4 ||
279 st->codec->codec_id == CODEC_ID_H264) {
280 av_set_pts_info(st, 64, 1, 25);
286 #define SEQ_START_CODE 0x000001b3
287 #define GOP_START_CODE 0x000001b8
288 #define PICTURE_START_CODE 0x00000100
289 #define SLICE_START_CODE 0x00000101
290 #define PACK_START_CODE 0x000001ba
292 static int mpegvideo_probe(AVProbeData *p)
295 int pic=0, seq=0, slice=0, pspack=0;
298 for(i=0; i<p->buf_size; i++){
299 code = (code<<8) + p->buf[i];
300 if ((code & 0xffffff00) == 0x100) {
302 case SEQ_START_CODE: seq++; break;
303 case PICTURE_START_CODE: pic++; break;
304 case SLICE_START_CODE: slice++; break;
305 case PACK_START_CODE: pspack++; break;
309 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack)
310 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
314 static int h263_probe(AVProbeData *p)
322 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
329 static int h261_probe(AVProbeData *p)
337 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
344 AVInputFormat shorten_iformat = {
350 raw_read_partial_packet,
355 AVInputFormat ac3_iformat = {
361 raw_read_partial_packet,
367 AVOutputFormat ac3_oformat = {
379 #endif //CONFIG_MUXERS
381 AVInputFormat dts_iformat = {
387 raw_read_partial_packet,
392 AVInputFormat h261_iformat = {
398 raw_read_partial_packet,
400 .extensions = "h261",
401 .value = CODEC_ID_H261,
405 AVOutputFormat h261_oformat = {
417 #endif //CONFIG_MUXERS
419 AVInputFormat h263_iformat = {
425 raw_read_partial_packet,
427 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
428 .value = CODEC_ID_H263,
432 AVOutputFormat h263_oformat = {
444 #endif //CONFIG_MUXERS
446 AVInputFormat m4v_iformat = {
448 "raw MPEG4 video format",
450 NULL /*mpegvideo_probe*/,
452 raw_read_partial_packet,
454 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
455 .value = CODEC_ID_MPEG4,
459 AVOutputFormat m4v_oformat = {
461 "raw MPEG4 video format",
471 #endif //CONFIG_MUXERS
473 AVInputFormat h264_iformat = {
475 "raw H264 video format",
477 NULL /*mpegvideo_probe*/,
479 raw_read_partial_packet,
481 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
482 .value = CODEC_ID_H264,
486 AVOutputFormat h264_oformat = {
488 "raw H264 video format",
498 #endif //CONFIG_MUXERS
500 AVInputFormat mpegvideo_iformat = {
506 raw_read_partial_packet,
508 .value = CODEC_ID_MPEG1VIDEO,
512 AVOutputFormat mpeg1video_oformat = {
524 #endif //CONFIG_MUXERS
527 AVOutputFormat mpeg2video_oformat = {
539 #endif //CONFIG_MUXERS
541 AVInputFormat mjpeg_iformat = {
547 raw_read_partial_packet,
549 .extensions = "mjpg,mjpeg",
550 .value = CODEC_ID_MJPEG,
553 AVInputFormat ingenient_iformat = {
559 ingenient_read_packet,
561 .extensions = "cgi", // FIXME
562 .value = CODEC_ID_MJPEG,
566 AVOutputFormat mjpeg_oformat = {
578 #endif //CONFIG_MUXERS
582 #define PCMINPUTDEF(name, long_name, ext, codec) \
583 AVInputFormat pcm_ ## name ## _iformat = {\
596 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
598 #define PCMDEF(name, long_name, ext, codec) \
599 PCMINPUTDEF(name, long_name, ext, codec)
603 #define PCMDEF(name, long_name, ext, codec) \
604 PCMINPUTDEF(name, long_name, ext, codec)\
606 AVOutputFormat pcm_ ## name ## _oformat = {\
618 #endif //CONFIG_MUXERS
620 #ifdef WORDS_BIGENDIAN
622 #define LE_DEF(s) NULL
624 #define BE_DEF(s) NULL
629 PCMDEF(s16le, "pcm signed 16 bit little endian format",
630 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
632 PCMDEF(s16be, "pcm signed 16 bit big endian format",
633 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
635 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
636 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
638 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
639 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
641 PCMDEF(s8, "pcm signed 8 bit format",
642 "sb", CODEC_ID_PCM_S8)
644 PCMDEF(u8, "pcm unsigned 8 bit format",
645 "ub", CODEC_ID_PCM_U8)
647 PCMDEF(mulaw, "pcm mu law format",
648 "ul", CODEC_ID_PCM_MULAW)
650 PCMDEF(alaw, "pcm A law format",
651 "al", CODEC_ID_PCM_ALAW)
653 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
655 int packet_size, ret, width, height;
656 AVStream *st = s->streams[0];
658 width = st->codec->width;
659 height = st->codec->height;
661 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
665 ret= av_get_packet(&s->pb, pkt, packet_size);
667 pkt->stream_index = 0;
668 if (ret != packet_size) {
675 AVInputFormat rawvideo_iformat = {
681 rawvideo_read_packet,
683 .extensions = "yuv,cif,qcif",
684 .value = CODEC_ID_RAWVIDEO,
688 AVOutputFormat rawvideo_oformat = {
700 #endif //CONFIG_MUXERS
703 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
708 AVOutputFormat null_oformat = {
714 #ifdef WORDS_BIGENDIAN
723 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
725 #endif //CONFIG_MUXERS
727 #ifndef CONFIG_MUXERS
728 #define av_register_output_format(format)
730 #ifndef CONFIG_DEMUXERS
731 #define av_register_input_format(format)
737 av_register_input_format(&shorten_iformat);
739 av_register_input_format(&ac3_iformat);
740 av_register_output_format(&ac3_oformat);
742 av_register_input_format(&dts_iformat);
744 av_register_input_format(&h261_iformat);
745 av_register_output_format(&h261_oformat);
747 av_register_input_format(&h263_iformat);
748 av_register_output_format(&h263_oformat);
750 av_register_input_format(&m4v_iformat);
751 av_register_output_format(&m4v_oformat);
753 av_register_input_format(&h264_iformat);
754 av_register_output_format(&h264_oformat);
756 av_register_input_format(&mpegvideo_iformat);
757 av_register_output_format(&mpeg1video_oformat);
759 av_register_output_format(&mpeg2video_oformat);
761 av_register_input_format(&mjpeg_iformat);
762 av_register_output_format(&mjpeg_oformat);
764 av_register_input_format(&ingenient_iformat);
766 av_register_input_format(&pcm_s16le_iformat);
767 av_register_output_format(&pcm_s16le_oformat);
768 av_register_input_format(&pcm_s16be_iformat);
769 av_register_output_format(&pcm_s16be_oformat);
770 av_register_input_format(&pcm_u16le_iformat);
771 av_register_output_format(&pcm_u16le_oformat);
772 av_register_input_format(&pcm_u16be_iformat);
773 av_register_output_format(&pcm_u16be_oformat);
774 av_register_input_format(&pcm_s8_iformat);
775 av_register_output_format(&pcm_s8_oformat);
776 av_register_input_format(&pcm_u8_iformat);
777 av_register_output_format(&pcm_u8_oformat);
778 av_register_input_format(&pcm_mulaw_iformat);
779 av_register_output_format(&pcm_mulaw_oformat);
780 av_register_input_format(&pcm_alaw_iformat);
781 av_register_output_format(&pcm_alaw_oformat);
783 av_register_input_format(&rawvideo_iformat);
784 av_register_output_format(&rawvideo_oformat);
786 av_register_output_format(&null_oformat);