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 flac_write_header(struct AVFormatContext *s)
31 static const uint8_t header[8] = {
32 0x66, 0x4C, 0x61, 0x43, 0x80, 0x00, 0x00, 0x22
34 uint8_t *streaminfo = s->streams[0]->codec->extradata;
35 int len = s->streams[0]->codec->extradata_size;
36 if(streaminfo != NULL && len > 0) {
37 put_buffer(&s->pb, header, 8);
38 put_buffer(&s->pb, streaminfo, len);
43 static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
45 put_buffer(&s->pb, pkt->data, pkt->size);
46 put_flush_packet(&s->pb);
50 static int raw_write_trailer(struct AVFormatContext *s)
54 #endif //CONFIG_MUXERS
57 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
62 st = av_new_stream(s, 0);
66 id = s->iformat->value;
67 if (id == CODEC_ID_RAWVIDEO) {
68 st->codec->codec_type = CODEC_TYPE_VIDEO;
70 st->codec->codec_type = CODEC_TYPE_AUDIO;
72 st->codec->codec_id = id;
74 switch(st->codec->codec_type) {
75 case CODEC_TYPE_AUDIO:
76 st->codec->sample_rate = ap->sample_rate;
77 st->codec->channels = ap->channels;
78 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
80 case CODEC_TYPE_VIDEO:
81 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
82 st->codec->width = ap->width;
83 st->codec->height = ap->height;
84 st->codec->pix_fmt = ap->pix_fmt;
85 if(st->codec->pix_fmt == PIX_FMT_NONE)
86 st->codec->pix_fmt= PIX_FMT_YUV420P;
94 #define RAW_PACKET_SIZE 1024
96 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
99 // AVStream *st = s->streams[0];
101 size= RAW_PACKET_SIZE;
103 ret= av_get_packet(&s->pb, pkt, size);
105 pkt->stream_index = 0;
109 /* note: we need to modify the packet size here to handle the last
115 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
119 size = RAW_PACKET_SIZE;
121 if (av_new_packet(pkt, size) < 0)
124 pkt->pos= url_ftell(&s->pb);
125 pkt->stream_index = 0;
126 ret = get_partial_buffer(&s->pb, pkt->data, size);
135 // http://www.artificis.hu/files/texts/ingenient.txt
136 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
138 int ret, size, w, h, unk1, unk2;
140 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
141 return AVERROR_IO; // FIXME
143 size = get_le32(&s->pb);
145 w = get_le16(&s->pb);
146 h = get_le16(&s->pb);
148 url_fskip(&s->pb, 8); // zero + size (padded?)
149 url_fskip(&s->pb, 2);
150 unk1 = get_le16(&s->pb);
151 unk2 = get_le16(&s->pb);
152 url_fskip(&s->pb, 22); // ascii timestamp
154 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
155 size, w, h, unk1, unk2);
157 if (av_new_packet(pkt, size) < 0)
160 pkt->pos = url_ftell(&s->pb);
161 pkt->stream_index = 0;
162 ret = get_buffer(&s->pb, pkt->data, size);
171 static int raw_read_close(AVFormatContext *s)
176 int pcm_read_seek(AVFormatContext *s,
177 int stream_index, int64_t timestamp, int flags)
180 int block_align, byte_rate;
184 switch(st->codec->codec_id) {
185 case CODEC_ID_PCM_S16LE:
186 case CODEC_ID_PCM_S16BE:
187 case CODEC_ID_PCM_U16LE:
188 case CODEC_ID_PCM_U16BE:
189 block_align = 2 * st->codec->channels;
190 byte_rate = block_align * st->codec->sample_rate;
192 case CODEC_ID_PCM_S8:
193 case CODEC_ID_PCM_U8:
194 case CODEC_ID_PCM_MULAW:
195 case CODEC_ID_PCM_ALAW:
196 block_align = st->codec->channels;
197 byte_rate = block_align * st->codec->sample_rate;
200 block_align = st->codec->block_align;
201 byte_rate = st->codec->bit_rate / 8;
205 if (block_align <= 0 || byte_rate <= 0)
208 /* compute the position by aligning it to block_align */
209 pos = av_rescale_rnd(timestamp * byte_rate,
211 st->time_base.den * (int64_t)block_align,
212 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
215 /* recompute exact position */
216 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
217 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
222 static int ac3_read_header(AVFormatContext *s,
223 AVFormatParameters *ap)
227 st = av_new_stream(s, 0);
229 return AVERROR_NOMEM;
231 st->codec->codec_type = CODEC_TYPE_AUDIO;
232 st->codec->codec_id = CODEC_ID_AC3;
233 st->need_parsing = 1;
234 /* the parameters will be extracted from the compressed bitstream */
238 static int shorten_read_header(AVFormatContext *s,
239 AVFormatParameters *ap)
243 st = av_new_stream(s, 0);
245 return AVERROR_NOMEM;
246 st->codec->codec_type = CODEC_TYPE_AUDIO;
247 st->codec->codec_id = CODEC_ID_SHORTEN;
248 st->need_parsing = 1;
249 /* the parameters will be extracted from the compressed bitstream */
254 static int flac_read_header(AVFormatContext *s,
255 AVFormatParameters *ap)
259 st = av_new_stream(s, 0);
261 return AVERROR_NOMEM;
262 st->codec->codec_type = CODEC_TYPE_AUDIO;
263 st->codec->codec_id = CODEC_ID_FLAC;
264 st->need_parsing = 1;
265 /* the parameters will be extracted from the compressed bitstream */
270 static int dts_read_header(AVFormatContext *s,
271 AVFormatParameters *ap)
275 st = av_new_stream(s, 0);
277 return AVERROR_NOMEM;
279 st->codec->codec_type = CODEC_TYPE_AUDIO;
280 st->codec->codec_id = CODEC_ID_DTS;
281 st->need_parsing = 1;
282 /* the parameters will be extracted from the compressed bitstream */
287 static int aac_read_header(AVFormatContext *s,
288 AVFormatParameters *ap)
292 st = av_new_stream(s, 0);
294 return AVERROR_NOMEM;
296 st->codec->codec_type = CODEC_TYPE_AUDIO;
297 st->codec->codec_id = CODEC_ID_AAC;
298 st->need_parsing = 1;
299 /* the parameters will be extracted from the compressed bitstream */
303 /* mpeg1/h263 input */
304 static int video_read_header(AVFormatContext *s,
305 AVFormatParameters *ap)
309 st = av_new_stream(s, 0);
311 return AVERROR_NOMEM;
313 st->codec->codec_type = CODEC_TYPE_VIDEO;
314 st->codec->codec_id = s->iformat->value;
315 st->need_parsing = 1;
317 /* for mjpeg, specify frame rate */
318 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
319 if (ap->time_base.num) {
320 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
321 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
322 st->codec->codec_id == CODEC_ID_MPEG4 ||
323 st->codec->codec_id == CODEC_ID_H264) {
324 av_set_pts_info(st, 64, 1, 25);
330 #define SEQ_START_CODE 0x000001b3
331 #define GOP_START_CODE 0x000001b8
332 #define PICTURE_START_CODE 0x00000100
333 #define SLICE_START_CODE 0x00000101
334 #define PACK_START_CODE 0x000001ba
335 #define VIDEO_ID 0x000001e0
336 #define AUDIO_ID 0x000001c0
338 static int mpegvideo_probe(AVProbeData *p)
341 int pic=0, seq=0, slice=0, pspack=0, pes=0;
344 for(i=0; i<p->buf_size; i++){
345 code = (code<<8) + p->buf[i];
346 if ((code & 0xffffff00) == 0x100) {
348 case SEQ_START_CODE: seq++; break;
349 case PICTURE_START_CODE: pic++; break;
350 case SLICE_START_CODE: slice++; break;
351 case PACK_START_CODE: pspack++; break;
353 case AUDIO_ID: pes++; break;
357 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack && !pes)
358 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
362 static int h263_probe(AVProbeData *p)
370 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
377 static int h261_probe(AVProbeData *p)
385 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
392 AVInputFormat shorten_demuxer = {
398 raw_read_partial_packet,
403 AVInputFormat flac_demuxer = {
409 raw_read_partial_packet,
411 .extensions = "flac",
415 AVOutputFormat flac_muxer = {
426 .flags= AVFMT_NOTIMESTAMPS,
428 #endif //CONFIG_MUXERS
430 AVInputFormat ac3_demuxer = {
436 raw_read_partial_packet,
442 AVOutputFormat ac3_muxer = {
453 .flags= AVFMT_NOTIMESTAMPS,
455 #endif //CONFIG_MUXERS
457 AVInputFormat dts_demuxer = {
463 raw_read_partial_packet,
468 AVInputFormat aac_demuxer = {
474 raw_read_partial_packet,
479 AVInputFormat h261_demuxer = {
485 raw_read_partial_packet,
487 .extensions = "h261",
488 .value = CODEC_ID_H261,
492 AVOutputFormat h261_muxer = {
503 .flags= AVFMT_NOTIMESTAMPS,
505 #endif //CONFIG_MUXERS
507 AVInputFormat h263_demuxer = {
513 raw_read_partial_packet,
515 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
516 .value = CODEC_ID_H263,
520 AVOutputFormat h263_muxer = {
531 .flags= AVFMT_NOTIMESTAMPS,
533 #endif //CONFIG_MUXERS
535 AVInputFormat m4v_demuxer = {
537 "raw MPEG4 video format",
539 NULL /*mpegvideo_probe*/,
541 raw_read_partial_packet,
543 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
544 .value = CODEC_ID_MPEG4,
548 AVOutputFormat m4v_muxer = {
550 "raw MPEG4 video format",
559 .flags= AVFMT_NOTIMESTAMPS,
561 #endif //CONFIG_MUXERS
563 AVInputFormat h264_demuxer = {
565 "raw H264 video format",
567 NULL /*mpegvideo_probe*/,
569 raw_read_partial_packet,
571 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
572 .value = CODEC_ID_H264,
576 AVOutputFormat h264_muxer = {
578 "raw H264 video format",
587 .flags= AVFMT_NOTIMESTAMPS,
589 #endif //CONFIG_MUXERS
591 AVInputFormat mpegvideo_demuxer = {
597 raw_read_partial_packet,
599 .value = CODEC_ID_MPEG1VIDEO,
603 AVOutputFormat mpeg1video_muxer = {
614 .flags= AVFMT_NOTIMESTAMPS,
616 #endif //CONFIG_MUXERS
619 AVOutputFormat mpeg2video_muxer = {
630 .flags= AVFMT_NOTIMESTAMPS,
632 #endif //CONFIG_MUXERS
634 AVInputFormat mjpeg_demuxer = {
640 raw_read_partial_packet,
642 .extensions = "mjpg,mjpeg",
643 .value = CODEC_ID_MJPEG,
646 AVInputFormat ingenient_demuxer = {
652 ingenient_read_packet,
654 .extensions = "cgi", // FIXME
655 .value = CODEC_ID_MJPEG,
659 AVOutputFormat mjpeg_muxer = {
670 .flags= AVFMT_NOTIMESTAMPS,
672 #endif //CONFIG_MUXERS
676 #define PCMINPUTDEF(name, long_name, ext, codec) \
677 AVInputFormat pcm_ ## name ## _demuxer = {\
690 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
691 AVOutputFormat pcm_ ## name ## _muxer = {\
702 .flags= AVFMT_NOTIMESTAMPS,\
706 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
707 #define PCMDEF(name, long_name, ext, codec) \
708 PCMINPUTDEF(name, long_name, ext, codec)
709 #elif defined(CONFIG_MUXERS) && !defined(CONFIG_DEMUXERS)
710 #define PCMDEF(name, long_name, ext, codec) \
711 PCMOUTPUTDEF(name, long_name, ext, codec)
712 #elif defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
713 #define PCMDEF(name, long_name, ext, codec) \
714 PCMINPUTDEF(name, long_name, ext, codec)\
715 PCMOUTPUTDEF(name, long_name, ext, codec)
717 #define PCMDEF(name, long_name, ext, codec)
720 #ifdef WORDS_BIGENDIAN
722 #define LE_DEF(s) NULL
724 #define BE_DEF(s) NULL
729 PCMDEF(s16le, "pcm signed 16 bit little endian format",
730 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
732 PCMDEF(s16be, "pcm signed 16 bit big endian format",
733 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
735 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
736 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
738 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
739 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
741 PCMDEF(s8, "pcm signed 8 bit format",
742 "sb", CODEC_ID_PCM_S8)
744 PCMDEF(u8, "pcm unsigned 8 bit format",
745 "ub", CODEC_ID_PCM_U8)
747 PCMDEF(mulaw, "pcm mu law format",
748 "ul", CODEC_ID_PCM_MULAW)
750 PCMDEF(alaw, "pcm A law format",
751 "al", CODEC_ID_PCM_ALAW)
753 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
755 int packet_size, ret, width, height;
756 AVStream *st = s->streams[0];
758 width = st->codec->width;
759 height = st->codec->height;
761 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
765 ret= av_get_packet(&s->pb, pkt, packet_size);
767 pkt->stream_index = 0;
768 if (ret != packet_size) {
775 AVInputFormat rawvideo_demuxer = {
781 rawvideo_read_packet,
783 .extensions = "yuv,cif,qcif",
784 .value = CODEC_ID_RAWVIDEO,
788 AVOutputFormat rawvideo_muxer = {
799 .flags= AVFMT_NOTIMESTAMPS,
801 #endif //CONFIG_MUXERS
804 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
809 AVOutputFormat null_muxer = {
815 #ifdef WORDS_BIGENDIAN
824 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE | AVFMT_NOTIMESTAMPS,
826 #endif //CONFIG_MUXERS