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_iformat = {
398 raw_read_partial_packet,
403 AVInputFormat flac_iformat = {
409 raw_read_partial_packet,
411 .extensions = "flac",
415 AVOutputFormat flac_oformat = {
427 #endif //CONFIG_MUXERS
429 AVInputFormat ac3_iformat = {
435 raw_read_partial_packet,
441 AVOutputFormat ac3_oformat = {
453 #endif //CONFIG_MUXERS
455 AVInputFormat dts_iformat = {
461 raw_read_partial_packet,
466 AVInputFormat aac_iformat = {
472 raw_read_partial_packet,
477 AVInputFormat h261_iformat = {
483 raw_read_partial_packet,
485 .extensions = "h261",
486 .value = CODEC_ID_H261,
490 AVOutputFormat h261_oformat = {
502 #endif //CONFIG_MUXERS
504 AVInputFormat h263_iformat = {
510 raw_read_partial_packet,
512 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
513 .value = CODEC_ID_H263,
517 AVOutputFormat h263_oformat = {
529 #endif //CONFIG_MUXERS
531 AVInputFormat m4v_iformat = {
533 "raw MPEG4 video format",
535 NULL /*mpegvideo_probe*/,
537 raw_read_partial_packet,
539 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
540 .value = CODEC_ID_MPEG4,
544 AVOutputFormat m4v_oformat = {
546 "raw MPEG4 video format",
556 #endif //CONFIG_MUXERS
558 AVInputFormat h264_iformat = {
560 "raw H264 video format",
562 NULL /*mpegvideo_probe*/,
564 raw_read_partial_packet,
566 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
567 .value = CODEC_ID_H264,
571 AVOutputFormat h264_oformat = {
573 "raw H264 video format",
583 #endif //CONFIG_MUXERS
585 AVInputFormat mpegvideo_iformat = {
591 raw_read_partial_packet,
593 .value = CODEC_ID_MPEG1VIDEO,
597 AVOutputFormat mpeg1video_oformat = {
609 #endif //CONFIG_MUXERS
612 AVOutputFormat mpeg2video_oformat = {
624 #endif //CONFIG_MUXERS
626 AVInputFormat mjpeg_iformat = {
632 raw_read_partial_packet,
634 .extensions = "mjpg,mjpeg",
635 .value = CODEC_ID_MJPEG,
638 AVInputFormat ingenient_iformat = {
644 ingenient_read_packet,
646 .extensions = "cgi", // FIXME
647 .value = CODEC_ID_MJPEG,
651 AVOutputFormat mjpeg_oformat = {
663 #endif //CONFIG_MUXERS
667 #define PCMINPUTDEF(name, long_name, ext, codec) \
668 AVInputFormat pcm_ ## name ## _iformat = {\
681 #define PCMOUTPUTDEF(name, long_name, ext, codec) \
682 AVOutputFormat pcm_ ## name ## _oformat = {\
696 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
697 #define PCMDEF(name, long_name, ext, codec) \
698 PCMINPUTDEF(name, long_name, ext, codec)
699 #elif defined(CONFIG_MUXERS) && !defined(CONFIG_DEMUXERS)
700 #define PCMDEF(name, long_name, ext, codec) \
701 PCMOUTPUTDEF(name, long_name, ext, codec)
702 #elif defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
703 #define PCMDEF(name, long_name, ext, codec) \
704 PCMINPUTDEF(name, long_name, ext, codec)\
705 PCMOUTPUTDEF(name, long_name, ext, codec)
707 #define PCMDEF(name, long_name, ext, codec)
710 #ifdef WORDS_BIGENDIAN
712 #define LE_DEF(s) NULL
714 #define BE_DEF(s) NULL
719 PCMDEF(s16le, "pcm signed 16 bit little endian format",
720 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
722 PCMDEF(s16be, "pcm signed 16 bit big endian format",
723 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
725 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
726 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
728 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
729 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
731 PCMDEF(s8, "pcm signed 8 bit format",
732 "sb", CODEC_ID_PCM_S8)
734 PCMDEF(u8, "pcm unsigned 8 bit format",
735 "ub", CODEC_ID_PCM_U8)
737 PCMDEF(mulaw, "pcm mu law format",
738 "ul", CODEC_ID_PCM_MULAW)
740 PCMDEF(alaw, "pcm A law format",
741 "al", CODEC_ID_PCM_ALAW)
743 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
745 int packet_size, ret, width, height;
746 AVStream *st = s->streams[0];
748 width = st->codec->width;
749 height = st->codec->height;
751 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
755 ret= av_get_packet(&s->pb, pkt, packet_size);
757 pkt->stream_index = 0;
758 if (ret != packet_size) {
765 AVInputFormat rawvideo_iformat = {
771 rawvideo_read_packet,
773 .extensions = "yuv,cif,qcif",
774 .value = CODEC_ID_RAWVIDEO,
778 AVOutputFormat rawvideo_oformat = {
790 #endif //CONFIG_MUXERS
793 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
798 AVOutputFormat null_oformat = {
804 #ifdef WORDS_BIGENDIAN
813 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
815 #endif //CONFIG_MUXERS
817 #ifndef CONFIG_MUXERS
818 #define av_register_output_format(format)
820 #ifndef CONFIG_DEMUXERS
821 #define av_register_input_format(format)
827 av_register_input_format(&shorten_iformat);
828 av_register_input_format(&flac_iformat);
829 av_register_output_format(&flac_oformat);
831 av_register_input_format(&ac3_iformat);
832 av_register_output_format(&ac3_oformat);
834 av_register_input_format(&aac_iformat);
836 av_register_input_format(&dts_iformat);
838 av_register_input_format(&h261_iformat);
839 av_register_output_format(&h261_oformat);
841 av_register_input_format(&h263_iformat);
842 av_register_output_format(&h263_oformat);
844 av_register_input_format(&m4v_iformat);
845 av_register_output_format(&m4v_oformat);
847 av_register_input_format(&h264_iformat);
848 av_register_output_format(&h264_oformat);
850 av_register_input_format(&mpegvideo_iformat);
851 av_register_output_format(&mpeg1video_oformat);
853 av_register_output_format(&mpeg2video_oformat);
855 av_register_input_format(&mjpeg_iformat);
856 av_register_output_format(&mjpeg_oformat);
858 av_register_input_format(&ingenient_iformat);
860 av_register_input_format(&pcm_s16le_iformat);
861 av_register_output_format(&pcm_s16le_oformat);
862 av_register_input_format(&pcm_s16be_iformat);
863 av_register_output_format(&pcm_s16be_oformat);
864 av_register_input_format(&pcm_u16le_iformat);
865 av_register_output_format(&pcm_u16le_oformat);
866 av_register_input_format(&pcm_u16be_iformat);
867 av_register_output_format(&pcm_u16be_oformat);
868 av_register_input_format(&pcm_s8_iformat);
869 av_register_output_format(&pcm_s8_oformat);
870 av_register_input_format(&pcm_u8_iformat);
871 av_register_output_format(&pcm_u8_oformat);
872 av_register_input_format(&pcm_mulaw_iformat);
873 av_register_output_format(&pcm_mulaw_oformat);
874 av_register_input_format(&pcm_alaw_iformat);
875 av_register_output_format(&pcm_alaw_oformat);
877 av_register_input_format(&rawvideo_iformat);
878 av_register_output_format(&rawvideo_oformat);
880 av_register_output_format(&null_oformat);