2 * RAW muxer and demuxer
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2005 Alex Beregszaszi
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 static int raw_write_header(struct AVFormatContext *s)
32 static int flac_write_header(struct AVFormatContext *s)
34 static const uint8_t header[8] = {
35 0x66, 0x4C, 0x61, 0x43, 0x80, 0x00, 0x00, 0x22
37 uint8_t *streaminfo = s->streams[0]->codec->extradata;
38 int len = s->streams[0]->codec->extradata_size;
39 if(streaminfo != NULL && len > 0) {
40 put_buffer(&s->pb, header, 8);
41 put_buffer(&s->pb, streaminfo, len);
46 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
48 put_buffer(&s->pb, pkt->data, pkt->size);
49 put_flush_packet(&s->pb);
53 static int raw_write_trailer(struct AVFormatContext *s)
57 #endif //CONFIG_MUXERS
60 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
65 st = av_new_stream(s, 0);
69 id = s->iformat->value;
70 if (id == CODEC_ID_RAWVIDEO) {
71 st->codec->codec_type = CODEC_TYPE_VIDEO;
73 st->codec->codec_type = CODEC_TYPE_AUDIO;
75 st->codec->codec_id = id;
77 switch(st->codec->codec_type) {
78 case CODEC_TYPE_AUDIO:
79 st->codec->sample_rate = ap->sample_rate;
80 st->codec->channels = ap->channels;
81 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
83 case CODEC_TYPE_VIDEO:
84 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
85 st->codec->width = ap->width;
86 st->codec->height = ap->height;
87 st->codec->pix_fmt = ap->pix_fmt;
88 if(st->codec->pix_fmt == PIX_FMT_NONE)
89 st->codec->pix_fmt= PIX_FMT_YUV420P;
97 #define RAW_PACKET_SIZE 1024
99 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
102 // AVStream *st = s->streams[0];
104 size= RAW_PACKET_SIZE;
106 ret= av_get_packet(&s->pb, pkt, size);
108 pkt->stream_index = 0;
112 /* note: we need to modify the packet size here to handle the last
118 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
122 size = RAW_PACKET_SIZE;
124 if (av_new_packet(pkt, size) < 0)
127 pkt->pos= url_ftell(&s->pb);
128 pkt->stream_index = 0;
129 ret = get_partial_buffer(&s->pb, pkt->data, size);
138 // http://www.artificis.hu/files/texts/ingenient.txt
139 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
141 int ret, size, w, h, unk1, unk2;
143 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
144 return AVERROR_IO; // FIXME
146 size = get_le32(&s->pb);
148 w = get_le16(&s->pb);
149 h = get_le16(&s->pb);
151 url_fskip(&s->pb, 8); // zero + size (padded?)
152 url_fskip(&s->pb, 2);
153 unk1 = get_le16(&s->pb);
154 unk2 = get_le16(&s->pb);
155 url_fskip(&s->pb, 22); // ascii timestamp
157 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
158 size, w, h, unk1, unk2);
160 if (av_new_packet(pkt, size) < 0)
163 pkt->pos = url_ftell(&s->pb);
164 pkt->stream_index = 0;
165 ret = get_buffer(&s->pb, pkt->data, size);
174 static int raw_read_close(AVFormatContext *s)
179 int pcm_read_seek(AVFormatContext *s,
180 int stream_index, int64_t timestamp, int flags)
183 int block_align, byte_rate;
188 block_align = st->codec->block_align ? st->codec->block_align :
189 (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
190 byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
191 block_align * st->codec->sample_rate;
193 if (block_align <= 0 || byte_rate <= 0)
196 /* compute the position by aligning it to block_align */
197 pos = av_rescale_rnd(timestamp * byte_rate,
199 st->time_base.den * (int64_t)block_align,
200 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
203 /* recompute exact position */
204 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
205 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
210 static int ac3_read_header(AVFormatContext *s,
211 AVFormatParameters *ap)
215 st = av_new_stream(s, 0);
217 return AVERROR_NOMEM;
219 st->codec->codec_type = CODEC_TYPE_AUDIO;
220 st->codec->codec_id = CODEC_ID_AC3;
221 st->need_parsing = 1;
222 /* the parameters will be extracted from the compressed bitstream */
226 static int shorten_read_header(AVFormatContext *s,
227 AVFormatParameters *ap)
231 st = av_new_stream(s, 0);
233 return AVERROR_NOMEM;
234 st->codec->codec_type = CODEC_TYPE_AUDIO;
235 st->codec->codec_id = CODEC_ID_SHORTEN;
236 st->need_parsing = 1;
237 /* the parameters will be extracted from the compressed bitstream */
242 static int flac_read_header(AVFormatContext *s,
243 AVFormatParameters *ap)
247 st = av_new_stream(s, 0);
249 return AVERROR_NOMEM;
250 st->codec->codec_type = CODEC_TYPE_AUDIO;
251 st->codec->codec_id = CODEC_ID_FLAC;
252 st->need_parsing = 1;
253 /* the parameters will be extracted from the compressed bitstream */
258 static int dts_read_header(AVFormatContext *s,
259 AVFormatParameters *ap)
263 st = av_new_stream(s, 0);
265 return AVERROR_NOMEM;
267 st->codec->codec_type = CODEC_TYPE_AUDIO;
268 st->codec->codec_id = CODEC_ID_DTS;
269 st->need_parsing = 1;
270 /* the parameters will be extracted from the compressed bitstream */
275 static int aac_read_header(AVFormatContext *s,
276 AVFormatParameters *ap)
280 st = av_new_stream(s, 0);
282 return AVERROR_NOMEM;
284 st->codec->codec_type = CODEC_TYPE_AUDIO;
285 st->codec->codec_id = CODEC_ID_AAC;
286 st->need_parsing = 1;
287 /* the parameters will be extracted from the compressed bitstream */
291 /* mpeg1/h263 input */
292 static int video_read_header(AVFormatContext *s,
293 AVFormatParameters *ap)
297 st = av_new_stream(s, 0);
299 return AVERROR_NOMEM;
301 st->codec->codec_type = CODEC_TYPE_VIDEO;
302 st->codec->codec_id = s->iformat->value;
303 st->need_parsing = 1;
305 /* for mjpeg, specify frame rate */
306 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
307 if (ap->time_base.num) {
308 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
309 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
310 st->codec->codec_id == CODEC_ID_MPEG4 ||
311 st->codec->codec_id == CODEC_ID_H264) {
312 av_set_pts_info(st, 64, 1, 25);
318 #define SEQ_START_CODE 0x000001b3
319 #define GOP_START_CODE 0x000001b8
320 #define PICTURE_START_CODE 0x00000100
321 #define SLICE_START_CODE 0x00000101
322 #define PACK_START_CODE 0x000001ba
323 #define VIDEO_ID 0x000001e0
324 #define AUDIO_ID 0x000001c0
326 static int mpegvideo_probe(AVProbeData *p)
329 int pic=0, seq=0, slice=0, pspack=0, pes=0;
332 for(i=0; i<p->buf_size; i++){
333 code = (code<<8) + p->buf[i];
334 if ((code & 0xffffff00) == 0x100) {
336 case SEQ_START_CODE: seq++; break;
337 case PICTURE_START_CODE: pic++; break;
338 case SLICE_START_CODE: slice++; break;
339 case PACK_START_CODE: pspack++; break;
341 if ((code & 0x1f0) == VIDEO_ID) pes++;
342 else if((code & 0x1e0) == AUDIO_ID) pes++;
345 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
346 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
350 #define VIDEO_OBJECT_START_CODE 0x00000100
351 #define VIDEO_OBJECT_LAYER_START_CODE 0x00000120
352 #define VISUAL_OBJECT_START_CODE 0x000001b5
353 #define VOP_START_CODE 0x000001b6
355 static int mpeg4video_probe(AVProbeData *probe_packet)
357 uint32_t temp_buffer= -1;
358 int VO=0, VOL=0, VOP = 0, VISO = 0;
361 for(i=0; i<probe_packet->buf_size; i++){
362 temp_buffer = (temp_buffer<<8) + probe_packet->buf[i];
363 if ((temp_buffer & 0xffffff00) == 0x100) {
365 case VOP_START_CODE: VOP++; break;
366 case VISUAL_OBJECT_START_CODE: VISO++; break;
368 switch(temp_buffer & 0xfffffff0){
369 case VIDEO_OBJECT_START_CODE: VO++; break;
370 case VIDEO_OBJECT_LAYER_START_CODE: VOL++; break;
375 if ( VOP >= VISO && VOP >= VOL && VO >= VOL && VOL > 0)
376 return AVPROBE_SCORE_MAX/2;
380 static int h263_probe(AVProbeData *p)
388 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
395 static int h261_probe(AVProbeData *p)
403 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
410 static int ac3_probe(AVProbeData *p)
412 int max_frames, first_frames, frames;
413 uint8_t *buf, *buf2, *end;
421 end = buf + FFMIN(4096, p->buf_size - 7);
423 for(; buf < end; buf++) {
426 for(frames = 0; buf2 < end; frames++) {
427 if(ff_ac3_parse_header(buf2, &hdr) < 0)
429 buf2 += hdr.frame_size;
431 max_frames = FFMAX(max_frames, frames);
433 first_frames = frames;
435 if (first_frames>=3) return AVPROBE_SCORE_MAX * 3 / 4;
436 else if(max_frames>=3) return AVPROBE_SCORE_MAX / 2;
437 else if(max_frames>=1) return 1;
441 AVInputFormat shorten_demuxer = {
447 raw_read_partial_packet,
449 .flags= AVFMT_GENERIC_INDEX,
453 AVInputFormat flac_demuxer = {
459 raw_read_partial_packet,
461 .flags= AVFMT_GENERIC_INDEX,
462 .extensions = "flac",
466 AVOutputFormat flac_muxer = {
477 .flags= AVFMT_NOTIMESTAMPS,
479 #endif //CONFIG_MUXERS
481 AVInputFormat ac3_demuxer = {
487 raw_read_partial_packet,
489 .flags= AVFMT_GENERIC_INDEX,
494 AVOutputFormat ac3_muxer = {
505 .flags= AVFMT_NOTIMESTAMPS,
507 #endif //CONFIG_MUXERS
509 AVInputFormat dts_demuxer = {
515 raw_read_partial_packet,
517 .flags= AVFMT_GENERIC_INDEX,
521 AVInputFormat aac_demuxer = {
527 raw_read_partial_packet,
529 .flags= AVFMT_GENERIC_INDEX,
533 AVInputFormat h261_demuxer = {
539 raw_read_partial_packet,
541 .flags= AVFMT_GENERIC_INDEX,
542 .extensions = "h261",
543 .value = CODEC_ID_H261,
547 AVOutputFormat h261_muxer = {
558 .flags= AVFMT_NOTIMESTAMPS,
560 #endif //CONFIG_MUXERS
562 AVInputFormat h263_demuxer = {
568 raw_read_partial_packet,
570 .flags= AVFMT_GENERIC_INDEX,
571 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
572 .value = CODEC_ID_H263,
576 AVOutputFormat h263_muxer = {
587 .flags= AVFMT_NOTIMESTAMPS,
589 #endif //CONFIG_MUXERS
591 AVInputFormat m4v_demuxer = {
593 "raw MPEG4 video format",
595 mpeg4video_probe, /** probing for mpeg4 data */
597 raw_read_partial_packet,
599 .flags= AVFMT_GENERIC_INDEX,
600 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
601 .value = CODEC_ID_MPEG4,
605 AVOutputFormat m4v_muxer = {
607 "raw MPEG4 video format",
616 .flags= AVFMT_NOTIMESTAMPS,
618 #endif //CONFIG_MUXERS
620 AVInputFormat h264_demuxer = {
622 "raw H264 video format",
624 NULL /*mpegvideo_probe*/,
626 raw_read_partial_packet,
628 .flags= AVFMT_GENERIC_INDEX,
629 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
630 .value = CODEC_ID_H264,
634 AVOutputFormat h264_muxer = {
636 "raw H264 video format",
645 .flags= AVFMT_NOTIMESTAMPS,
647 #endif //CONFIG_MUXERS
649 AVInputFormat mpegvideo_demuxer = {
655 raw_read_partial_packet,
657 .flags= AVFMT_GENERIC_INDEX,
658 .value = CODEC_ID_MPEG1VIDEO,
662 AVOutputFormat mpeg1video_muxer = {
673 .flags= AVFMT_NOTIMESTAMPS,
675 #endif //CONFIG_MUXERS
678 AVOutputFormat mpeg2video_muxer = {
689 .flags= AVFMT_NOTIMESTAMPS,
691 #endif //CONFIG_MUXERS
693 AVInputFormat mjpeg_demuxer = {
699 raw_read_partial_packet,
701 .flags= AVFMT_GENERIC_INDEX,
702 .extensions = "mjpg,mjpeg",
703 .value = CODEC_ID_MJPEG,
706 AVInputFormat ingenient_demuxer = {
712 ingenient_read_packet,
714 .flags= AVFMT_GENERIC_INDEX,
715 .extensions = "cgi", // FIXME
716 .value = CODEC_ID_MJPEG,
720 AVOutputFormat mjpeg_muxer = {
731 .flags= AVFMT_NOTIMESTAMPS,
733 #endif //CONFIG_MUXERS
735 AVInputFormat vc1_demuxer = {
739 NULL /* vc1_probe */,
741 raw_read_partial_packet,
744 .value = CODEC_ID_VC1,
749 #define PCMINPUTDEF(name, long_name, ext, codec) \
750 AVInputFormat pcm_ ## name ## _demuxer = {\
759 .flags= AVFMT_GENERIC_INDEX,\
764 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
765 AVOutputFormat pcm_ ## name ## _muxer = {\
776 .flags= AVFMT_NOTIMESTAMPS,\
780 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
781 #define PCMDEF(name, long_name, ext, codec) \
782 PCMINPUTDEF(name, long_name, ext, codec)
783 #elif defined(CONFIG_MUXERS) && !defined(CONFIG_DEMUXERS)
784 #define PCMDEF(name, long_name, ext, codec) \
785 PCMOUTPUTDEF(name, long_name, ext, codec)
786 #elif defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
787 #define PCMDEF(name, long_name, ext, codec) \
788 PCMINPUTDEF(name, long_name, ext, codec)\
789 PCMOUTPUTDEF(name, long_name, ext, codec)
791 #define PCMDEF(name, long_name, ext, codec)
794 #ifdef WORDS_BIGENDIAN
796 #define LE_DEF(s) NULL
798 #define BE_DEF(s) NULL
803 PCMDEF(s16le, "pcm signed 16 bit little endian format",
804 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
806 PCMDEF(s16be, "pcm signed 16 bit big endian format",
807 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
809 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
810 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
812 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
813 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
815 PCMDEF(s8, "pcm signed 8 bit format",
816 "sb", CODEC_ID_PCM_S8)
818 PCMDEF(u8, "pcm unsigned 8 bit format",
819 "ub", CODEC_ID_PCM_U8)
821 PCMDEF(mulaw, "pcm mu law format",
822 "ul", CODEC_ID_PCM_MULAW)
824 PCMDEF(alaw, "pcm A law format",
825 "al", CODEC_ID_PCM_ALAW)
827 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
829 int packet_size, ret, width, height;
830 AVStream *st = s->streams[0];
832 width = st->codec->width;
833 height = st->codec->height;
835 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
839 ret= av_get_packet(&s->pb, pkt, packet_size);
841 pkt->stream_index = 0;
842 if (ret != packet_size) {
849 AVInputFormat rawvideo_demuxer = {
855 rawvideo_read_packet,
857 .flags= AVFMT_GENERIC_INDEX,
858 .extensions = "yuv,cif,qcif",
859 .value = CODEC_ID_RAWVIDEO,
863 AVOutputFormat rawvideo_muxer = {
874 .flags= AVFMT_NOTIMESTAMPS,
876 #endif //CONFIG_MUXERS
879 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
884 AVOutputFormat null_muxer = {
890 #ifdef WORDS_BIGENDIAN
899 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE | AVFMT_NOTIMESTAMPS,
901 #endif //CONFIG_MUXERS