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);
66 static int raw_write_trailer(struct AVFormatContext *s)
70 #endif //CONFIG_MUXERS
73 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
78 st = av_new_stream(s, 0);
82 id = s->iformat->value;
83 if (id == CODEC_ID_RAWVIDEO) {
84 st->codec->codec_type = CODEC_TYPE_VIDEO;
86 st->codec->codec_type = CODEC_TYPE_AUDIO;
88 st->codec->codec_id = id;
90 switch(st->codec->codec_type) {
91 case CODEC_TYPE_AUDIO:
92 st->codec->sample_rate = ap->sample_rate;
93 st->codec->channels = ap->channels;
94 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
96 case CODEC_TYPE_VIDEO:
97 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
98 st->codec->width = ap->width;
99 st->codec->height = ap->height;
100 st->codec->pix_fmt = ap->pix_fmt;
101 if(st->codec->pix_fmt == PIX_FMT_NONE)
102 st->codec->pix_fmt= PIX_FMT_YUV420P;
110 #define RAW_PACKET_SIZE 1024
112 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
115 // AVStream *st = s->streams[0];
117 size= RAW_PACKET_SIZE;
119 ret= av_get_packet(&s->pb, pkt, size);
121 pkt->stream_index = 0;
125 /* note: we need to modify the packet size here to handle the last
131 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
135 size = RAW_PACKET_SIZE;
137 if (av_new_packet(pkt, size) < 0)
140 pkt->pos= url_ftell(&s->pb);
141 pkt->stream_index = 0;
142 ret = get_partial_buffer(&s->pb, pkt->data, size);
151 // http://www.artificis.hu/files/texts/ingenient.txt
152 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
154 int ret, size, w, h, unk1, unk2;
156 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
157 return AVERROR_IO; // FIXME
159 size = get_le32(&s->pb);
161 w = get_le16(&s->pb);
162 h = get_le16(&s->pb);
164 url_fskip(&s->pb, 8); // zero + size (padded?)
165 url_fskip(&s->pb, 2);
166 unk1 = get_le16(&s->pb);
167 unk2 = get_le16(&s->pb);
168 url_fskip(&s->pb, 22); // ascii timestamp
170 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
171 size, w, h, unk1, unk2);
173 if (av_new_packet(pkt, size) < 0)
176 pkt->pos = url_ftell(&s->pb);
177 pkt->stream_index = 0;
178 ret = get_buffer(&s->pb, pkt->data, size);
187 static int raw_read_close(AVFormatContext *s)
192 int pcm_read_seek(AVFormatContext *s,
193 int stream_index, int64_t timestamp, int flags)
196 int block_align, byte_rate;
201 block_align = st->codec->block_align ? st->codec->block_align :
202 (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
203 byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
204 block_align * st->codec->sample_rate;
206 if (block_align <= 0 || byte_rate <= 0)
209 /* compute the position by aligning it to block_align */
210 pos = av_rescale_rnd(timestamp * byte_rate,
212 st->time_base.den * (int64_t)block_align,
213 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
216 /* recompute exact position */
217 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
218 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
223 static int ac3_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_AUDIO;
233 st->codec->codec_id = CODEC_ID_AC3;
234 st->need_parsing = AVSTREAM_PARSE_FULL;
235 /* the parameters will be extracted from the compressed bitstream */
239 static int shorten_read_header(AVFormatContext *s,
240 AVFormatParameters *ap)
244 st = av_new_stream(s, 0);
246 return AVERROR_NOMEM;
247 st->codec->codec_type = CODEC_TYPE_AUDIO;
248 st->codec->codec_id = CODEC_ID_SHORTEN;
249 st->need_parsing = AVSTREAM_PARSE_FULL;
250 /* the parameters will be extracted from the compressed bitstream */
255 static int flac_read_header(AVFormatContext *s,
256 AVFormatParameters *ap)
260 st = av_new_stream(s, 0);
262 return AVERROR_NOMEM;
263 st->codec->codec_type = CODEC_TYPE_AUDIO;
264 st->codec->codec_id = CODEC_ID_FLAC;
265 st->need_parsing = AVSTREAM_PARSE_FULL;
266 /* the parameters will be extracted from the compressed bitstream */
271 static int dts_read_header(AVFormatContext *s,
272 AVFormatParameters *ap)
276 st = av_new_stream(s, 0);
278 return AVERROR_NOMEM;
280 st->codec->codec_type = CODEC_TYPE_AUDIO;
281 st->codec->codec_id = CODEC_ID_DTS;
282 st->need_parsing = AVSTREAM_PARSE_FULL;
283 /* the parameters will be extracted from the compressed bitstream */
288 static int aac_read_header(AVFormatContext *s,
289 AVFormatParameters *ap)
293 st = av_new_stream(s, 0);
295 return AVERROR_NOMEM;
297 st->codec->codec_type = CODEC_TYPE_AUDIO;
298 st->codec->codec_id = CODEC_ID_AAC;
299 st->need_parsing = AVSTREAM_PARSE_FULL;
300 /* the parameters will be extracted from the compressed bitstream */
304 /* mpeg1/h263 input */
305 static int video_read_header(AVFormatContext *s,
306 AVFormatParameters *ap)
310 st = av_new_stream(s, 0);
312 return AVERROR_NOMEM;
314 st->codec->codec_type = CODEC_TYPE_VIDEO;
315 st->codec->codec_id = s->iformat->value;
316 st->need_parsing = AVSTREAM_PARSE_FULL;
318 /* for mjpeg, specify frame rate */
319 /* for mpeg4 specify it too (most mpeg4 streams do not have the fixed_vop_rate set ...)*/
320 if (ap->time_base.num) {
321 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
322 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
323 st->codec->codec_id == CODEC_ID_MPEG4 ||
324 st->codec->codec_id == CODEC_ID_H264) {
325 av_set_pts_info(st, 64, 1, 25);
331 #define SEQ_START_CODE 0x000001b3
332 #define GOP_START_CODE 0x000001b8
333 #define PICTURE_START_CODE 0x00000100
334 #define SLICE_START_CODE 0x00000101
335 #define PACK_START_CODE 0x000001ba
336 #define VIDEO_ID 0x000001e0
337 #define AUDIO_ID 0x000001c0
339 static int mpegvideo_probe(AVProbeData *p)
342 int pic=0, seq=0, slice=0, pspack=0, pes=0;
345 for(i=0; i<p->buf_size; i++){
346 code = (code<<8) + p->buf[i];
347 if ((code & 0xffffff00) == 0x100) {
349 case SEQ_START_CODE: seq++; break;
350 case PICTURE_START_CODE: pic++; break;
351 case SLICE_START_CODE: slice++; break;
352 case PACK_START_CODE: pspack++; break;
354 if ((code & 0x1f0) == VIDEO_ID) pes++;
355 else if((code & 0x1e0) == AUDIO_ID) pes++;
358 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
359 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
363 #define VISUAL_OBJECT_START_CODE 0x000001b5
364 #define VOP_START_CODE 0x000001b6
366 static int mpeg4video_probe(AVProbeData *probe_packet)
368 uint32_t temp_buffer= -1;
369 int VO=0, VOL=0, VOP = 0, VISO = 0, res=0;
372 for(i=0; i<probe_packet->buf_size; i++){
373 temp_buffer = (temp_buffer<<8) + probe_packet->buf[i];
374 if ((temp_buffer & 0xffffff00) == 0x100) {
376 case VOP_START_CODE: VOP++; break;
377 case VISUAL_OBJECT_START_CODE: VISO++; break;
378 case 0x100 ... 0x11F: VO++; break;
379 case 0x120 ... 0x12F: VOL++; break;
380 case 0x130 ... 0x1AF:
381 case 0x1B7 ... 0x1B9:
382 case 0x1C4 ... 0x1FF: res++; break;
387 if ( VOP >= VISO && VOP >= VOL && VO >= VOL && VOL > 0 && res==0)
388 return AVPROBE_SCORE_MAX/2;
392 static int h263_probe(AVProbeData *p)
398 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
405 static int h261_probe(AVProbeData *p)
411 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
418 static int ac3_probe(AVProbeData *p)
420 int max_frames, first_frames, frames;
421 uint8_t *buf, *buf2, *end;
429 end = buf + FFMIN(4096, p->buf_size - 7);
431 for(; buf < end; buf++) {
434 for(frames = 0; buf2 < end; frames++) {
435 if(ff_ac3_parse_header(buf2, &hdr) < 0)
437 buf2 += hdr.frame_size;
439 max_frames = FFMAX(max_frames, frames);
441 first_frames = frames;
443 if (first_frames>=3) return AVPROBE_SCORE_MAX * 3 / 4;
444 else if(max_frames>=3) return AVPROBE_SCORE_MAX / 2;
445 else if(max_frames>=1) return 1;
449 AVInputFormat shorten_demuxer = {
455 raw_read_partial_packet,
457 .flags= AVFMT_GENERIC_INDEX,
461 AVInputFormat flac_demuxer = {
467 raw_read_partial_packet,
469 .flags= AVFMT_GENERIC_INDEX,
470 .extensions = "flac",
474 AVOutputFormat flac_muxer = {
485 .flags= AVFMT_NOTIMESTAMPS,
487 #endif //CONFIG_MUXERS
489 #ifdef CONFIG_AC3_DEMUXER
490 AVInputFormat ac3_demuxer = {
496 raw_read_partial_packet,
498 .flags= AVFMT_GENERIC_INDEX,
504 AVOutputFormat ac3_muxer = {
515 .flags= AVFMT_NOTIMESTAMPS,
517 #endif //CONFIG_MUXERS
519 AVInputFormat dts_demuxer = {
525 raw_read_partial_packet,
527 .flags= AVFMT_GENERIC_INDEX,
531 AVInputFormat aac_demuxer = {
537 raw_read_partial_packet,
539 .flags= AVFMT_GENERIC_INDEX,
543 #ifdef CONFIG_ROQ_MUXER
544 AVOutputFormat roq_muxer =
557 #endif //CONFIG_ROQ_MUXER
559 AVInputFormat h261_demuxer = {
565 raw_read_partial_packet,
567 .flags= AVFMT_GENERIC_INDEX,
568 .extensions = "h261",
569 .value = CODEC_ID_H261,
573 AVOutputFormat h261_muxer = {
584 .flags= AVFMT_NOTIMESTAMPS,
586 #endif //CONFIG_MUXERS
588 AVInputFormat h263_demuxer = {
594 raw_read_partial_packet,
596 .flags= AVFMT_GENERIC_INDEX,
597 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
598 .value = CODEC_ID_H263,
602 AVOutputFormat h263_muxer = {
613 .flags= AVFMT_NOTIMESTAMPS,
615 #endif //CONFIG_MUXERS
617 AVInputFormat m4v_demuxer = {
619 "raw MPEG4 video format",
621 mpeg4video_probe, /** probing for mpeg4 data */
623 raw_read_partial_packet,
625 .flags= AVFMT_GENERIC_INDEX,
626 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
627 .value = CODEC_ID_MPEG4,
631 AVOutputFormat m4v_muxer = {
633 "raw MPEG4 video format",
642 .flags= AVFMT_NOTIMESTAMPS,
644 #endif //CONFIG_MUXERS
646 AVInputFormat h264_demuxer = {
648 "raw H264 video format",
650 NULL /*mpegvideo_probe*/,
652 raw_read_partial_packet,
654 .flags= AVFMT_GENERIC_INDEX,
655 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
656 .value = CODEC_ID_H264,
660 AVOutputFormat h264_muxer = {
662 "raw H264 video format",
671 .flags= AVFMT_NOTIMESTAMPS,
673 #endif //CONFIG_MUXERS
675 AVInputFormat mpegvideo_demuxer = {
681 raw_read_partial_packet,
683 .flags= AVFMT_GENERIC_INDEX,
684 .value = CODEC_ID_MPEG1VIDEO,
688 AVOutputFormat mpeg1video_muxer = {
699 .flags= AVFMT_NOTIMESTAMPS,
701 #endif //CONFIG_MUXERS
704 AVOutputFormat mpeg2video_muxer = {
715 .flags= AVFMT_NOTIMESTAMPS,
717 #endif //CONFIG_MUXERS
719 AVInputFormat mjpeg_demuxer = {
725 raw_read_partial_packet,
727 .flags= AVFMT_GENERIC_INDEX,
728 .extensions = "mjpg,mjpeg",
729 .value = CODEC_ID_MJPEG,
732 AVInputFormat ingenient_demuxer = {
738 ingenient_read_packet,
740 .flags= AVFMT_GENERIC_INDEX,
741 .extensions = "cgi", // FIXME
742 .value = CODEC_ID_MJPEG,
746 AVOutputFormat mjpeg_muxer = {
757 .flags= AVFMT_NOTIMESTAMPS,
759 #endif //CONFIG_MUXERS
761 AVInputFormat vc1_demuxer = {
765 NULL /* vc1_probe */,
767 raw_read_partial_packet,
770 .value = CODEC_ID_VC1,
775 #define PCMINPUTDEF(name, long_name, ext, codec) \
776 AVInputFormat pcm_ ## name ## _demuxer = {\
785 .flags= AVFMT_GENERIC_INDEX,\
790 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
791 AVOutputFormat pcm_ ## name ## _muxer = {\
802 .flags= AVFMT_NOTIMESTAMPS,\
806 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
807 #define PCMDEF(name, long_name, ext, codec) \
808 PCMINPUTDEF(name, long_name, ext, codec)
809 #elif defined(CONFIG_MUXERS) && !defined(CONFIG_DEMUXERS)
810 #define PCMDEF(name, long_name, ext, codec) \
811 PCMOUTPUTDEF(name, long_name, ext, codec)
812 #elif defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
813 #define PCMDEF(name, long_name, ext, codec) \
814 PCMINPUTDEF(name, long_name, ext, codec)\
815 PCMOUTPUTDEF(name, long_name, ext, codec)
817 #define PCMDEF(name, long_name, ext, codec)
820 #ifdef WORDS_BIGENDIAN
822 #define LE_DEF(s) NULL
824 #define BE_DEF(s) NULL
829 PCMDEF(s16le, "pcm signed 16 bit little endian format",
830 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
832 PCMDEF(s16be, "pcm signed 16 bit big endian format",
833 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
835 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
836 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
838 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
839 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
841 PCMDEF(s8, "pcm signed 8 bit format",
842 "sb", CODEC_ID_PCM_S8)
844 PCMDEF(u8, "pcm unsigned 8 bit format",
845 "ub", CODEC_ID_PCM_U8)
847 PCMDEF(mulaw, "pcm mu law format",
848 "ul", CODEC_ID_PCM_MULAW)
850 PCMDEF(alaw, "pcm A law format",
851 "al", CODEC_ID_PCM_ALAW)
853 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
855 int packet_size, ret, width, height;
856 AVStream *st = s->streams[0];
858 width = st->codec->width;
859 height = st->codec->height;
861 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
865 ret= av_get_packet(&s->pb, pkt, packet_size);
867 pkt->stream_index = 0;
868 if (ret != packet_size) {
875 AVInputFormat rawvideo_demuxer = {
881 rawvideo_read_packet,
883 .flags= AVFMT_GENERIC_INDEX,
884 .extensions = "yuv,cif,qcif,rgb",
885 .value = CODEC_ID_RAWVIDEO,
889 AVOutputFormat rawvideo_muxer = {
900 .flags= AVFMT_NOTIMESTAMPS,
902 #endif //CONFIG_MUXERS
905 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
910 AVOutputFormat null_muxer = {
916 #ifdef WORDS_BIGENDIAN
925 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE | AVFMT_NOTIMESTAMPS,
927 #endif //CONFIG_MUXERS