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 raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
31 put_buffer(&s->pb, pkt->data, pkt->size);
32 put_flush_packet(&s->pb);
36 static int raw_write_trailer(struct AVFormatContext *s)
40 #endif //CONFIG_MUXERS
43 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
48 st = av_new_stream(s, 0);
52 id = s->iformat->value;
53 if (id == CODEC_ID_RAWVIDEO) {
54 st->codec->codec_type = CODEC_TYPE_VIDEO;
56 st->codec->codec_type = CODEC_TYPE_AUDIO;
58 st->codec->codec_id = id;
60 switch(st->codec->codec_type) {
61 case CODEC_TYPE_AUDIO:
62 st->codec->sample_rate = ap->sample_rate;
63 st->codec->channels = ap->channels;
64 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
66 case CODEC_TYPE_VIDEO:
67 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
68 st->codec->width = ap->width;
69 st->codec->height = ap->height;
70 st->codec->pix_fmt = ap->pix_fmt;
71 if(st->codec->pix_fmt == PIX_FMT_NONE)
72 st->codec->pix_fmt= PIX_FMT_YUV420P;
83 #define RAW_PACKET_SIZE 1024
85 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
88 // AVStream *st = s->streams[0];
90 size= RAW_PACKET_SIZE;
92 ret= av_get_packet(&s->pb, pkt, size);
94 pkt->stream_index = 0;
98 /* note: we need to modify the packet size here to handle the last
104 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
108 size = RAW_PACKET_SIZE;
110 if (av_new_packet(pkt, size) < 0)
113 pkt->pos= url_ftell(&s->pb);
114 pkt->stream_index = 0;
115 ret = get_partial_buffer(&s->pb, pkt->data, size);
124 // http://www.artificis.hu/files/texts/ingenient.txt
125 static int ingenient_read_packet(AVFormatContext *s, AVPacket *pkt)
127 int ret, size, w, h, unk1, unk2;
129 if (get_le32(&s->pb) != MKTAG('M', 'J', 'P', 'G'))
130 return AVERROR_IO; // FIXME
132 size = get_le32(&s->pb);
134 w = get_le16(&s->pb);
135 h = get_le16(&s->pb);
137 url_fskip(&s->pb, 8); // zero + size (padded?)
138 url_fskip(&s->pb, 2);
139 unk1 = get_le16(&s->pb);
140 unk2 = get_le16(&s->pb);
141 url_fskip(&s->pb, 22); // ascii timestamp
143 av_log(NULL, AV_LOG_DEBUG, "Ingenient packet: size=%d, width=%d, height=%d, unk1=%d unk2=%d\n",
144 size, w, h, unk1, unk2);
146 if (av_new_packet(pkt, size) < 0)
149 pkt->pos = url_ftell(&s->pb);
150 pkt->stream_index = 0;
151 ret = get_buffer(&s->pb, pkt->data, size);
160 static int raw_read_close(AVFormatContext *s)
165 int pcm_read_seek(AVFormatContext *s,
166 int stream_index, int64_t timestamp, int flags)
169 int block_align, byte_rate;
173 switch(st->codec->codec_id) {
174 case CODEC_ID_PCM_S16LE:
175 case CODEC_ID_PCM_S16BE:
176 case CODEC_ID_PCM_U16LE:
177 case CODEC_ID_PCM_U16BE:
178 block_align = 2 * st->codec->channels;
179 byte_rate = block_align * st->codec->sample_rate;
181 case CODEC_ID_PCM_S8:
182 case CODEC_ID_PCM_U8:
183 case CODEC_ID_PCM_MULAW:
184 case CODEC_ID_PCM_ALAW:
185 block_align = st->codec->channels;
186 byte_rate = block_align * st->codec->sample_rate;
189 block_align = st->codec->block_align;
190 byte_rate = st->codec->bit_rate / 8;
194 if (block_align <= 0 || byte_rate <= 0)
197 /* compute the position by aligning it to block_align */
198 pos = av_rescale_rnd(timestamp * byte_rate,
200 st->time_base.den * (int64_t)block_align,
201 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
204 /* recompute exact position */
205 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
206 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
211 static int ac3_read_header(AVFormatContext *s,
212 AVFormatParameters *ap)
216 st = av_new_stream(s, 0);
218 return AVERROR_NOMEM;
220 st->codec->codec_type = CODEC_TYPE_AUDIO;
221 st->codec->codec_id = CODEC_ID_AC3;
222 st->need_parsing = 1;
223 /* the parameters will be extracted from the compressed bitstream */
227 static int shorten_read_header(AVFormatContext *s,
228 AVFormatParameters *ap)
232 st = av_new_stream(s, 0);
234 return AVERROR_NOMEM;
235 st->codec->codec_type = CODEC_TYPE_AUDIO;
236 st->codec->codec_id = CODEC_ID_SHORTEN;
237 st->need_parsing = 1;
238 /* the parameters will be extracted from the compressed bitstream */
243 static int dts_read_header(AVFormatContext *s,
244 AVFormatParameters *ap)
248 st = av_new_stream(s, 0);
250 return AVERROR_NOMEM;
252 st->codec->codec_type = CODEC_TYPE_AUDIO;
253 st->codec->codec_id = CODEC_ID_DTS;
254 st->need_parsing = 1;
255 /* the parameters will be extracted from the compressed bitstream */
260 static int aac_read_header(AVFormatContext *s,
261 AVFormatParameters *ap)
265 st = av_new_stream(s, 0);
267 return AVERROR_NOMEM;
269 st->codec->codec_type = CODEC_TYPE_AUDIO;
270 st->codec->codec_id = CODEC_ID_AAC;
271 st->need_parsing = 1;
272 /* the parameters will be extracted from the compressed bitstream */
276 /* mpeg1/h263 input */
277 static int video_read_header(AVFormatContext *s,
278 AVFormatParameters *ap)
282 st = av_new_stream(s, 0);
284 return AVERROR_NOMEM;
286 st->codec->codec_type = CODEC_TYPE_VIDEO;
287 st->codec->codec_id = s->iformat->value;
288 st->need_parsing = 1;
290 /* for mjpeg, specify frame rate */
291 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
292 if (ap && ap->time_base.num) {
293 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
294 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
295 st->codec->codec_id == CODEC_ID_MPEG4 ||
296 st->codec->codec_id == CODEC_ID_H264) {
297 av_set_pts_info(st, 64, 1, 25);
303 #define SEQ_START_CODE 0x000001b3
304 #define GOP_START_CODE 0x000001b8
305 #define PICTURE_START_CODE 0x00000100
306 #define SLICE_START_CODE 0x00000101
307 #define PACK_START_CODE 0x000001ba
309 static int mpegvideo_probe(AVProbeData *p)
312 int pic=0, seq=0, slice=0, pspack=0;
315 for(i=0; i<p->buf_size; i++){
316 code = (code<<8) + p->buf[i];
317 if ((code & 0xffffff00) == 0x100) {
319 case SEQ_START_CODE: seq++; break;
320 case PICTURE_START_CODE: pic++; break;
321 case SLICE_START_CODE: slice++; break;
322 case PACK_START_CODE: pspack++; break;
326 if(seq && seq*9<=pic*10 && pic*9<=slice*10 && !pspack)
327 return AVPROBE_SCORE_MAX/2+1; // +1 for .mpg
331 static int h263_probe(AVProbeData *p)
339 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
346 static int h261_probe(AVProbeData *p)
354 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
361 AVInputFormat shorten_iformat = {
367 raw_read_partial_packet,
372 AVInputFormat ac3_iformat = {
378 raw_read_partial_packet,
384 AVOutputFormat ac3_oformat = {
396 #endif //CONFIG_MUXERS
398 AVInputFormat dts_iformat = {
404 raw_read_partial_packet,
409 AVInputFormat aac_iformat = {
415 raw_read_partial_packet,
420 AVInputFormat h261_iformat = {
426 raw_read_partial_packet,
428 .extensions = "h261",
429 .value = CODEC_ID_H261,
433 AVOutputFormat h261_oformat = {
445 #endif //CONFIG_MUXERS
447 AVInputFormat h263_iformat = {
453 raw_read_partial_packet,
455 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
456 .value = CODEC_ID_H263,
460 AVOutputFormat h263_oformat = {
472 #endif //CONFIG_MUXERS
474 AVInputFormat m4v_iformat = {
476 "raw MPEG4 video format",
478 NULL /*mpegvideo_probe*/,
480 raw_read_partial_packet,
482 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
483 .value = CODEC_ID_MPEG4,
487 AVOutputFormat m4v_oformat = {
489 "raw MPEG4 video format",
499 #endif //CONFIG_MUXERS
501 AVInputFormat h264_iformat = {
503 "raw H264 video format",
505 NULL /*mpegvideo_probe*/,
507 raw_read_partial_packet,
509 .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
510 .value = CODEC_ID_H264,
514 AVOutputFormat h264_oformat = {
516 "raw H264 video format",
526 #endif //CONFIG_MUXERS
528 AVInputFormat mpegvideo_iformat = {
534 raw_read_partial_packet,
536 .value = CODEC_ID_MPEG1VIDEO,
540 AVOutputFormat mpeg1video_oformat = {
552 #endif //CONFIG_MUXERS
555 AVOutputFormat mpeg2video_oformat = {
567 #endif //CONFIG_MUXERS
569 AVInputFormat mjpeg_iformat = {
575 raw_read_partial_packet,
577 .extensions = "mjpg,mjpeg",
578 .value = CODEC_ID_MJPEG,
581 AVInputFormat ingenient_iformat = {
587 ingenient_read_packet,
589 .extensions = "cgi", // FIXME
590 .value = CODEC_ID_MJPEG,
594 AVOutputFormat mjpeg_oformat = {
606 #endif //CONFIG_MUXERS
610 #define PCMINPUTDEF(name, long_name, ext, codec) \
611 AVInputFormat pcm_ ## name ## _iformat = {\
624 #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
626 #define PCMDEF(name, long_name, ext, codec) \
627 PCMINPUTDEF(name, long_name, ext, codec)
631 #define PCMDEF(name, long_name, ext, codec) \
632 PCMINPUTDEF(name, long_name, ext, codec)\
634 AVOutputFormat pcm_ ## name ## _oformat = {\
646 #endif //CONFIG_MUXERS
648 #ifdef WORDS_BIGENDIAN
650 #define LE_DEF(s) NULL
652 #define BE_DEF(s) NULL
657 PCMDEF(s16le, "pcm signed 16 bit little endian format",
658 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
660 PCMDEF(s16be, "pcm signed 16 bit big endian format",
661 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
663 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
664 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
666 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
667 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
669 PCMDEF(s8, "pcm signed 8 bit format",
670 "sb", CODEC_ID_PCM_S8)
672 PCMDEF(u8, "pcm unsigned 8 bit format",
673 "ub", CODEC_ID_PCM_U8)
675 PCMDEF(mulaw, "pcm mu law format",
676 "ul", CODEC_ID_PCM_MULAW)
678 PCMDEF(alaw, "pcm A law format",
679 "al", CODEC_ID_PCM_ALAW)
681 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
683 int packet_size, ret, width, height;
684 AVStream *st = s->streams[0];
686 width = st->codec->width;
687 height = st->codec->height;
689 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
693 ret= av_get_packet(&s->pb, pkt, packet_size);
695 pkt->stream_index = 0;
696 if (ret != packet_size) {
703 AVInputFormat rawvideo_iformat = {
709 rawvideo_read_packet,
711 .extensions = "yuv,cif,qcif",
712 .value = CODEC_ID_RAWVIDEO,
716 AVOutputFormat rawvideo_oformat = {
728 #endif //CONFIG_MUXERS
731 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
736 AVOutputFormat null_oformat = {
742 #ifdef WORDS_BIGENDIAN
751 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
753 #endif //CONFIG_MUXERS
755 #ifndef CONFIG_MUXERS
756 #define av_register_output_format(format)
758 #ifndef CONFIG_DEMUXERS
759 #define av_register_input_format(format)
765 av_register_input_format(&shorten_iformat);
767 av_register_input_format(&ac3_iformat);
768 av_register_output_format(&ac3_oformat);
770 av_register_input_format(&aac_iformat);
772 av_register_input_format(&dts_iformat);
774 av_register_input_format(&h261_iformat);
775 av_register_output_format(&h261_oformat);
777 av_register_input_format(&h263_iformat);
778 av_register_output_format(&h263_oformat);
780 av_register_input_format(&m4v_iformat);
781 av_register_output_format(&m4v_oformat);
783 av_register_input_format(&h264_iformat);
784 av_register_output_format(&h264_oformat);
786 av_register_input_format(&mpegvideo_iformat);
787 av_register_output_format(&mpeg1video_oformat);
789 av_register_output_format(&mpeg2video_oformat);
791 av_register_input_format(&mjpeg_iformat);
792 av_register_output_format(&mjpeg_oformat);
794 av_register_input_format(&ingenient_iformat);
796 av_register_input_format(&pcm_s16le_iformat);
797 av_register_output_format(&pcm_s16le_oformat);
798 av_register_input_format(&pcm_s16be_iformat);
799 av_register_output_format(&pcm_s16be_oformat);
800 av_register_input_format(&pcm_u16le_iformat);
801 av_register_output_format(&pcm_u16le_oformat);
802 av_register_input_format(&pcm_u16be_iformat);
803 av_register_output_format(&pcm_u16be_oformat);
804 av_register_input_format(&pcm_s8_iformat);
805 av_register_output_format(&pcm_s8_oformat);
806 av_register_input_format(&pcm_u8_iformat);
807 av_register_output_format(&pcm_u8_oformat);
808 av_register_input_format(&pcm_mulaw_iformat);
809 av_register_output_format(&pcm_mulaw_oformat);
810 av_register_input_format(&pcm_alaw_iformat);
811 av_register_output_format(&pcm_alaw_oformat);
813 av_register_input_format(&rawvideo_iformat);
814 av_register_output_format(&rawvideo_oformat);
816 av_register_output_format(&null_oformat);