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 */
260 static int aac_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_AUDIO;
270 st->codec->codec_id = CODEC_ID_AAC;
271 st->need_parsing = 1;
272 /* the parameters will be extracted from the compressed bitstream */
276 /* mpeg1/h263 input */
277 static int video_read_header(AVFormatContext *s,
278 AVFormatParameters *ap)
282 st = av_new_stream(s, 0);
284 return AVERROR_NOMEM;
286 st->codec->codec_type = CODEC_TYPE_VIDEO;
287 st->codec->codec_id = s->iformat->value;
288 st->need_parsing = 1;
290 /* for mjpeg, specify frame rate */
291 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
292 if (ap && ap->time_base.num) {
293 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
294 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
295 st->codec->codec_id == CODEC_ID_MPEG4 ||
296 st->codec->codec_id == CODEC_ID_H264) {
297 av_set_pts_info(st, 64, 1, 25);
303 #define SEQ_START_CODE 0x000001b3
304 #define GOP_START_CODE 0x000001b8
305 #define PICTURE_START_CODE 0x00000100
306 #define SLICE_START_CODE 0x00000101
307 #define PACK_START_CODE 0x000001ba
308 #define VIDEO_ID 0x000001e0
309 #define AUDIO_ID 0x000001c0
311 static int mpegvideo_probe(AVProbeData *p)
314 int pic=0, seq=0, slice=0, pspack=0, pes=0;
317 for(i=0; i<p->buf_size; i++){
318 code = (code<<8) + p->buf[i];
319 if ((code & 0xffffff00) == 0x100) {
321 case SEQ_START_CODE: seq++; break;
322 case PICTURE_START_CODE: pic++; break;
323 case SLICE_START_CODE: slice++; break;
324 case PACK_START_CODE: pspack++; break;
326 case AUDIO_ID: pes++; break;
330 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
331 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
335 static int h263_probe(AVProbeData *p)
343 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
350 static int h261_probe(AVProbeData *p)
358 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
365 AVInputFormat shorten_iformat = {
371 raw_read_partial_packet,
376 AVInputFormat ac3_iformat = {
382 raw_read_partial_packet,
388 AVOutputFormat ac3_oformat = {
400 #endif //CONFIG_MUXERS
402 AVInputFormat dts_iformat = {
408 raw_read_partial_packet,
413 AVInputFormat aac_iformat = {
419 raw_read_partial_packet,
424 AVInputFormat h261_iformat = {
430 raw_read_partial_packet,
432 .extensions = "h261",
433 .value = CODEC_ID_H261,
437 AVOutputFormat h261_oformat = {
449 #endif //CONFIG_MUXERS
451 AVInputFormat h263_iformat = {
457 raw_read_partial_packet,
459 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
460 .value = CODEC_ID_H263,
464 AVOutputFormat h263_oformat = {
476 #endif //CONFIG_MUXERS
478 AVInputFormat m4v_iformat = {
480 "raw MPEG4 video format",
482 NULL /*mpegvideo_probe*/,
484 raw_read_partial_packet,
486 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
487 .value = CODEC_ID_MPEG4,
491 AVOutputFormat m4v_oformat = {
493 "raw MPEG4 video format",
503 #endif //CONFIG_MUXERS
505 AVInputFormat h264_iformat = {
507 "raw H264 video format",
509 NULL /*mpegvideo_probe*/,
511 raw_read_partial_packet,
513 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
514 .value = CODEC_ID_H264,
518 AVOutputFormat h264_oformat = {
520 "raw H264 video format",
530 #endif //CONFIG_MUXERS
532 AVInputFormat mpegvideo_iformat = {
538 raw_read_partial_packet,
540 .value = CODEC_ID_MPEG1VIDEO,
544 AVOutputFormat mpeg1video_oformat = {
556 #endif //CONFIG_MUXERS
559 AVOutputFormat mpeg2video_oformat = {
571 #endif //CONFIG_MUXERS
573 AVInputFormat mjpeg_iformat = {
579 raw_read_partial_packet,
581 .extensions = "mjpg,mjpeg",
582 .value = CODEC_ID_MJPEG,
585 AVInputFormat ingenient_iformat = {
591 ingenient_read_packet,
593 .extensions = "cgi", // FIXME
594 .value = CODEC_ID_MJPEG,
598 AVOutputFormat mjpeg_oformat = {
610 #endif //CONFIG_MUXERS
614 #define PCMINPUTDEF(name, long_name, ext, codec) \
615 AVInputFormat pcm_ ## name ## _iformat = {\
628 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
630 #define PCMDEF(name, long_name, ext, codec) \
631 PCMINPUTDEF(name, long_name, ext, codec)
635 #define PCMDEF(name, long_name, ext, codec) \
636 PCMINPUTDEF(name, long_name, ext, codec)\
638 AVOutputFormat pcm_ ## name ## _oformat = {\
650 #endif //CONFIG_MUXERS
652 #ifdef WORDS_BIGENDIAN
654 #define LE_DEF(s) NULL
656 #define BE_DEF(s) NULL
661 PCMDEF(s16le, "pcm signed 16 bit little endian format",
662 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
664 PCMDEF(s16be, "pcm signed 16 bit big endian format",
665 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
667 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
668 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
670 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
671 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
673 PCMDEF(s8, "pcm signed 8 bit format",
674 "sb", CODEC_ID_PCM_S8)
676 PCMDEF(u8, "pcm unsigned 8 bit format",
677 "ub", CODEC_ID_PCM_U8)
679 PCMDEF(mulaw, "pcm mu law format",
680 "ul", CODEC_ID_PCM_MULAW)
682 PCMDEF(alaw, "pcm A law format",
683 "al", CODEC_ID_PCM_ALAW)
685 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
687 int packet_size, ret, width, height;
688 AVStream *st = s->streams[0];
690 width = st->codec->width;
691 height = st->codec->height;
693 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
697 ret= av_get_packet(&s->pb, pkt, packet_size);
699 pkt->stream_index = 0;
700 if (ret != packet_size) {
707 AVInputFormat rawvideo_iformat = {
713 rawvideo_read_packet,
715 .extensions = "yuv,cif,qcif",
716 .value = CODEC_ID_RAWVIDEO,
720 AVOutputFormat rawvideo_oformat = {
732 #endif //CONFIG_MUXERS
735 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
740 AVOutputFormat null_oformat = {
746 #ifdef WORDS_BIGENDIAN
755 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
757 #endif //CONFIG_MUXERS
759 #ifndef CONFIG_MUXERS
760 #define av_register_output_format(format)
762 #ifndef CONFIG_DEMUXERS
763 #define av_register_input_format(format)
769 av_register_input_format(&shorten_iformat);
771 av_register_input_format(&ac3_iformat);
772 av_register_output_format(&ac3_oformat);
774 av_register_input_format(&aac_iformat);
776 av_register_input_format(&dts_iformat);
778 av_register_input_format(&h261_iformat);
779 av_register_output_format(&h261_oformat);
781 av_register_input_format(&h263_iformat);
782 av_register_output_format(&h263_oformat);
784 av_register_input_format(&m4v_iformat);
785 av_register_output_format(&m4v_oformat);
787 av_register_input_format(&h264_iformat);
788 av_register_output_format(&h264_oformat);
790 av_register_input_format(&mpegvideo_iformat);
791 av_register_output_format(&mpeg1video_oformat);
793 av_register_output_format(&mpeg2video_oformat);
795 av_register_input_format(&mjpeg_iformat);
796 av_register_output_format(&mjpeg_oformat);
798 av_register_input_format(&ingenient_iformat);
800 av_register_input_format(&pcm_s16le_iformat);
801 av_register_output_format(&pcm_s16le_oformat);
802 av_register_input_format(&pcm_s16be_iformat);
803 av_register_output_format(&pcm_s16be_oformat);
804 av_register_input_format(&pcm_u16le_iformat);
805 av_register_output_format(&pcm_u16le_oformat);
806 av_register_input_format(&pcm_u16be_iformat);
807 av_register_output_format(&pcm_u16be_oformat);
808 av_register_input_format(&pcm_s8_iformat);
809 av_register_output_format(&pcm_s8_oformat);
810 av_register_input_format(&pcm_u8_iformat);
811 av_register_output_format(&pcm_u8_oformat);
812 av_register_input_format(&pcm_mulaw_iformat);
813 av_register_output_format(&pcm_mulaw_oformat);
814 av_register_input_format(&pcm_alaw_iformat);
815 av_register_output_format(&pcm_alaw_oformat);
817 av_register_input_format(&rawvideo_iformat);
818 av_register_output_format(&rawvideo_oformat);
820 av_register_output_format(&null_oformat);