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
23 #include "ac3_parser.h"
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);
47 static int roq_write_header(struct AVFormatContext *s)
49 static const uint8_t header[] = {
50 0x84, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x1E, 0x00
53 put_buffer(&s->pb, header, 8);
54 put_flush_packet(&s->pb);
59 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
61 put_buffer(&s->pb, pkt->data, pkt->size);
62 put_flush_packet(&s->pb);
65 #endif //CONFIG_MUXERS
68 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
73 st = av_new_stream(s, 0);
77 id = s->iformat->value;
78 if (id == CODEC_ID_RAWVIDEO) {
79 st->codec->codec_type = CODEC_TYPE_VIDEO;
81 st->codec->codec_type = CODEC_TYPE_AUDIO;
83 st->codec->codec_id = id;
85 switch(st->codec->codec_type) {
86 case CODEC_TYPE_AUDIO:
87 st->codec->sample_rate = ap->sample_rate;
88 st->codec->channels = ap->channels;
89 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
91 case CODEC_TYPE_VIDEO:
92 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
93 st->codec->width = ap->width;
94 st->codec->height = ap->height;
95 st->codec->pix_fmt = ap->pix_fmt;
96 if(st->codec->pix_fmt == PIX_FMT_NONE)
97 st->codec->pix_fmt= PIX_FMT_YUV420P;
105 #define RAW_PACKET_SIZE 1024
107 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
110 // AVStream *st = s->streams[0];
112 size= RAW_PACKET_SIZE;
114 ret= av_get_packet(&s->pb, pkt, size);
116 pkt->stream_index = 0;
120 /* note: we need to modify the packet size here to handle the last
126 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
130 size = RAW_PACKET_SIZE;
132 if (av_new_packet(pkt, size) < 0)
135 pkt->pos= url_ftell(&s->pb);
136 pkt->stream_index = 0;
137 ret = get_partial_buffer(&s->pb, pkt->data, size);
146 // http://www.artificis.hu/files/texts/ingenient.txt
147 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
149 int ret, size, w, h, unk1, unk2;
151 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
152 return AVERROR_IO; // FIXME
154 size = get_le32(&s->pb);
156 w = get_le16(&s->pb);
157 h = get_le16(&s->pb);
159 url_fskip(&s->pb, 8); // zero + size (padded?)
160 url_fskip(&s->pb, 2);
161 unk1 = get_le16(&s->pb);
162 unk2 = get_le16(&s->pb);
163 url_fskip(&s->pb, 22); // ascii timestamp
165 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
166 size, w, h, unk1, unk2);
168 if (av_new_packet(pkt, size) < 0)
171 pkt->pos = url_ftell(&s->pb);
172 pkt->stream_index = 0;
173 ret = get_buffer(&s->pb, pkt->data, size);
182 static int raw_read_close(AVFormatContext *s)
187 int pcm_read_seek(AVFormatContext *s,
188 int stream_index, int64_t timestamp, int flags)
191 int block_align, byte_rate;
196 block_align = st->codec->block_align ? st->codec->block_align :
197 (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
198 byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
199 block_align * st->codec->sample_rate;
201 if (block_align <= 0 || byte_rate <= 0)
204 /* compute the position by aligning it to block_align */
205 pos = av_rescale_rnd(timestamp * byte_rate,
207 st->time_base.den * (int64_t)block_align,
208 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
211 /* recompute exact position */
212 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
213 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
218 static int ac3_read_header(AVFormatContext *s,
219 AVFormatParameters *ap)
223 st = av_new_stream(s, 0);
225 return AVERROR_NOMEM;
227 st->codec->codec_type = CODEC_TYPE_AUDIO;
228 st->codec->codec_id = CODEC_ID_AC3;
229 st->need_parsing = AVSTREAM_PARSE_FULL;
230 /* the parameters will be extracted from the compressed bitstream */
234 static int shorten_read_header(AVFormatContext *s,
235 AVFormatParameters *ap)
239 st = av_new_stream(s, 0);
241 return AVERROR_NOMEM;
242 st->codec->codec_type = CODEC_TYPE_AUDIO;
243 st->codec->codec_id = CODEC_ID_SHORTEN;
244 st->need_parsing = AVSTREAM_PARSE_FULL;
245 /* the parameters will be extracted from the compressed bitstream */
250 static int flac_read_header(AVFormatContext *s,
251 AVFormatParameters *ap)
255 st = av_new_stream(s, 0);
257 return AVERROR_NOMEM;
258 st->codec->codec_type = CODEC_TYPE_AUDIO;
259 st->codec->codec_id = CODEC_ID_FLAC;
260 st->need_parsing = AVSTREAM_PARSE_FULL;
261 /* the parameters will be extracted from the compressed bitstream */
266 static int dts_read_header(AVFormatContext *s,
267 AVFormatParameters *ap)
271 st = av_new_stream(s, 0);
273 return AVERROR_NOMEM;
275 st->codec->codec_type = CODEC_TYPE_AUDIO;
276 st->codec->codec_id = CODEC_ID_DTS;
277 st->need_parsing = AVSTREAM_PARSE_FULL;
278 /* the parameters will be extracted from the compressed bitstream */
283 static int aac_read_header(AVFormatContext *s,
284 AVFormatParameters *ap)
288 st = av_new_stream(s, 0);
290 return AVERROR_NOMEM;
292 st->codec->codec_type = CODEC_TYPE_AUDIO;
293 st->codec->codec_id = CODEC_ID_AAC;
294 st->need_parsing = AVSTREAM_PARSE_FULL;
295 /* the parameters will be extracted from the compressed bitstream */
299 /* mpeg1/h263 input */
300 static int video_read_header(AVFormatContext *s,
301 AVFormatParameters *ap)
305 st = av_new_stream(s, 0);
307 return AVERROR_NOMEM;
309 st->codec->codec_type = CODEC_TYPE_VIDEO;
310 st->codec->codec_id = s->iformat->value;
311 st->need_parsing = AVSTREAM_PARSE_FULL;
313 /* for mjpeg, specify frame rate */
314 /* for mpeg4 specify it too (most mpeg4 streams do not have the fixed_vop_rate set ...)*/
315 if (ap->time_base.num) {
316 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
317 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
318 st->codec->codec_id == CODEC_ID_MPEG4 ||
319 st->codec->codec_id == CODEC_ID_H264) {
320 av_set_pts_info(st, 64, 1, 25);
326 #define SEQ_START_CODE 0x000001b3
327 #define GOP_START_CODE 0x000001b8
328 #define PICTURE_START_CODE 0x00000100
329 #define SLICE_START_CODE 0x00000101
330 #define PACK_START_CODE 0x000001ba
331 #define VIDEO_ID 0x000001e0
332 #define AUDIO_ID 0x000001c0
334 static int mpegvideo_probe(AVProbeData *p)
337 int pic=0, seq=0, slice=0, pspack=0, pes=0;
340 for(i=0; i<p->buf_size; i++){
341 code = (code<<8) + p->buf[i];
342 if ((code & 0xffffff00) == 0x100) {
344 case SEQ_START_CODE: seq++; break;
345 case PICTURE_START_CODE: pic++; break;
346 case SLICE_START_CODE: slice++; break;
347 case PACK_START_CODE: pspack++; break;
349 if ((code & 0x1f0) == VIDEO_ID) pes++;
350 else if((code & 0x1e0) == AUDIO_ID) pes++;
353 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
354 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
358 #define VISUAL_OBJECT_START_CODE 0x000001b5
359 #define VOP_START_CODE 0x000001b6
361 static int mpeg4video_probe(AVProbeData *probe_packet)
363 uint32_t temp_buffer= -1;
364 int VO=0, VOL=0, VOP = 0, VISO = 0, res=0;
367 for(i=0; i<probe_packet->buf_size; i++){
368 temp_buffer = (temp_buffer<<8) + probe_packet->buf[i];
369 if ((temp_buffer & 0xffffff00) != 0x100)
372 if (temp_buffer == VOP_START_CODE) VOP++;
373 else if (temp_buffer == VISUAL_OBJECT_START_CODE) VISO++;
374 else if (temp_buffer < 0x120) VO++;
375 else if (temp_buffer < 0x130) VOL++;
376 else if ( !(0x1AF < temp_buffer && temp_buffer < 0x1B7)
377 && !(0x1B9 < temp_buffer && temp_buffer < 0x1C4)) res++;
380 if ( VOP >= VISO && VOP >= VOL && VO >= VOL && VOL > 0 && res==0)
381 return AVPROBE_SCORE_MAX/2;
385 static int h263_probe(AVProbeData *p)
391 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
398 static int h261_probe(AVProbeData *p)
404 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
411 static int ac3_probe(AVProbeData *p)
413 int max_frames, first_frames = 0, frames;
414 uint8_t *buf, *buf2, *end;
422 end = buf + FFMIN(4096, p->buf_size - 7);
424 for(; buf < end; buf++) {
427 for(frames = 0; buf2 < end; frames++) {
428 if(ff_ac3_parse_header(buf2, &hdr) < 0)
430 buf2 += hdr.frame_size;
432 max_frames = FFMAX(max_frames, frames);
434 first_frames = frames;
436 if (first_frames>=3) return AVPROBE_SCORE_MAX * 3 / 4;
437 else if(max_frames>=3) return AVPROBE_SCORE_MAX / 2;
438 else if(max_frames>=1) return 1;
442 AVInputFormat shorten_demuxer = {
448 raw_read_partial_packet,
450 .flags= AVFMT_GENERIC_INDEX,
454 AVInputFormat flac_demuxer = {
460 raw_read_partial_packet,
462 .flags= AVFMT_GENERIC_INDEX,
463 .extensions = "flac",
467 AVOutputFormat flac_muxer = {
477 .flags= AVFMT_NOTIMESTAMPS,
479 #endif //CONFIG_MUXERS
481 #ifdef CONFIG_AC3_DEMUXER
482 AVInputFormat ac3_demuxer = {
488 raw_read_partial_packet,
490 .flags= AVFMT_GENERIC_INDEX,
496 AVOutputFormat ac3_muxer = {
506 .flags= AVFMT_NOTIMESTAMPS,
508 #endif //CONFIG_MUXERS
510 AVInputFormat dts_demuxer = {
516 raw_read_partial_packet,
518 .flags= AVFMT_GENERIC_INDEX,
522 AVInputFormat aac_demuxer = {
528 raw_read_partial_packet,
530 .flags= AVFMT_GENERIC_INDEX,
534 #ifdef CONFIG_ROQ_MUXER
535 AVOutputFormat roq_muxer =
547 #endif //CONFIG_ROQ_MUXER
549 AVInputFormat h261_demuxer = {
555 raw_read_partial_packet,
557 .flags= AVFMT_GENERIC_INDEX,
558 .extensions = "h261",
559 .value = CODEC_ID_H261,
563 AVOutputFormat h261_muxer = {
573 .flags= AVFMT_NOTIMESTAMPS,
575 #endif //CONFIG_MUXERS
577 AVInputFormat h263_demuxer = {
583 raw_read_partial_packet,
585 .flags= AVFMT_GENERIC_INDEX,
586 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
587 .value = CODEC_ID_H263,
591 AVOutputFormat h263_muxer = {
601 .flags= AVFMT_NOTIMESTAMPS,
603 #endif //CONFIG_MUXERS
605 AVInputFormat m4v_demuxer = {
607 "raw MPEG4 video format",
609 mpeg4video_probe, /** probing for mpeg4 data */
611 raw_read_partial_packet,
613 .flags= AVFMT_GENERIC_INDEX,
614 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
615 .value = CODEC_ID_MPEG4,
619 AVOutputFormat m4v_muxer = {
621 "raw MPEG4 video format",
629 .flags= AVFMT_NOTIMESTAMPS,
631 #endif //CONFIG_MUXERS
633 AVInputFormat h264_demuxer = {
635 "raw H264 video format",
637 NULL /*mpegvideo_probe*/,
639 raw_read_partial_packet,
641 .flags= AVFMT_GENERIC_INDEX,
642 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
643 .value = CODEC_ID_H264,
647 AVOutputFormat h264_muxer = {
649 "raw H264 video format",
657 .flags= AVFMT_NOTIMESTAMPS,
659 #endif //CONFIG_MUXERS
661 AVInputFormat mpegvideo_demuxer = {
667 raw_read_partial_packet,
669 .flags= AVFMT_GENERIC_INDEX,
670 .value = CODEC_ID_MPEG1VIDEO,
674 AVOutputFormat mpeg1video_muxer = {
684 .flags= AVFMT_NOTIMESTAMPS,
686 #endif //CONFIG_MUXERS
689 AVOutputFormat mpeg2video_muxer = {
699 .flags= AVFMT_NOTIMESTAMPS,
701 #endif //CONFIG_MUXERS
703 AVInputFormat mjpeg_demuxer = {
709 raw_read_partial_packet,
711 .flags= AVFMT_GENERIC_INDEX,
712 .extensions = "mjpg,mjpeg",
713 .value = CODEC_ID_MJPEG,
716 AVInputFormat ingenient_demuxer = {
722 ingenient_read_packet,
724 .flags= AVFMT_GENERIC_INDEX,
725 .extensions = "cgi", // FIXME
726 .value = CODEC_ID_MJPEG,
730 AVOutputFormat mjpeg_muxer = {
740 .flags= AVFMT_NOTIMESTAMPS,
742 #endif //CONFIG_MUXERS
744 AVInputFormat vc1_demuxer = {
748 NULL /* vc1_probe */,
750 raw_read_partial_packet,
753 .value = CODEC_ID_VC1,
758 #define PCMINPUTDEF(name, long_name, ext, codec) \
759 AVInputFormat pcm_ ## name ## _demuxer = {\
768 .flags= AVFMT_GENERIC_INDEX,\
773 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
774 AVOutputFormat pcm_ ## name ## _muxer = {\
784 .flags= AVFMT_NOTIMESTAMPS,\
788 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
789 #define PCMDEF(name, long_name, ext, codec) \
790 PCMINPUTDEF(name, long_name, ext, codec)
791 #elif defined(CONFIG_MUXERS) && !defined(CONFIG_DEMUXERS)
792 #define PCMDEF(name, long_name, ext, codec) \
793 PCMOUTPUTDEF(name, long_name, ext, codec)
794 #elif defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
795 #define PCMDEF(name, long_name, ext, codec) \
796 PCMINPUTDEF(name, long_name, ext, codec)\
797 PCMOUTPUTDEF(name, long_name, ext, codec)
799 #define PCMDEF(name, long_name, ext, codec)
802 #ifdef WORDS_BIGENDIAN
804 #define LE_DEF(s) NULL
806 #define BE_DEF(s) NULL
811 PCMDEF(s16le, "pcm signed 16 bit little endian format",
812 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
814 PCMDEF(s16be, "pcm signed 16 bit big endian format",
815 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
817 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
818 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
820 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
821 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
823 PCMDEF(s8, "pcm signed 8 bit format",
824 "sb", CODEC_ID_PCM_S8)
826 PCMDEF(u8, "pcm unsigned 8 bit format",
827 "ub", CODEC_ID_PCM_U8)
829 PCMDEF(mulaw, "pcm mu law format",
830 "ul", CODEC_ID_PCM_MULAW)
832 PCMDEF(alaw, "pcm A law format",
833 "al", CODEC_ID_PCM_ALAW)
835 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
837 int packet_size, ret, width, height;
838 AVStream *st = s->streams[0];
840 width = st->codec->width;
841 height = st->codec->height;
843 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
847 ret= av_get_packet(&s->pb, pkt, packet_size);
849 pkt->stream_index = 0;
850 if (ret != packet_size) {
857 AVInputFormat rawvideo_demuxer = {
863 rawvideo_read_packet,
865 .flags= AVFMT_GENERIC_INDEX,
866 .extensions = "yuv,cif,qcif,rgb",
867 .value = CODEC_ID_RAWVIDEO,
871 AVOutputFormat rawvideo_muxer = {
881 .flags= AVFMT_NOTIMESTAMPS,
883 #endif //CONFIG_MUXERS
886 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
891 AVOutputFormat null_muxer = {
897 #ifdef WORDS_BIGENDIAN
905 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE | AVFMT_NOTIMESTAMPS,
907 #endif //CONFIG_MUXERS