2 * RAW encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static int raw_write_header(struct AVFormatContext *s)
27 static int raw_write_packet(struct AVFormatContext *s, int stream_index,
28 unsigned char *buf, int size, int force_pts)
30 put_buffer(&s->pb, buf, size);
31 put_flush_packet(&s->pb);
35 static int raw_write_trailer(struct AVFormatContext *s)
41 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
46 st = av_new_stream(s, 0);
50 id = s->iformat->value;
51 if (id == CODEC_ID_RAWVIDEO) {
52 st->codec.codec_type = CODEC_TYPE_VIDEO;
54 st->codec.codec_type = CODEC_TYPE_AUDIO;
56 st->codec.codec_id = id;
58 switch(st->codec.codec_type) {
59 case CODEC_TYPE_AUDIO:
60 st->codec.sample_rate = ap->sample_rate;
61 st->codec.channels = ap->channels;
63 case CODEC_TYPE_VIDEO:
64 st->codec.frame_rate = ap->frame_rate;
65 st->codec.frame_rate_base = ap->frame_rate_base;
66 st->codec.width = ap->width;
67 st->codec.height = ap->height;
68 st->codec.pix_fmt = ap->pix_fmt;
79 #define RAW_PACKET_SIZE 1024
81 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
84 // AVStream *st = s->streams[0];
86 size= RAW_PACKET_SIZE;
88 if (av_new_packet(pkt, size) < 0)
91 pkt->stream_index = 0;
92 ret = get_buffer(&s->pb, pkt->data, size);
97 /* note: we need to modify the packet size here to handle the last
103 static int raw_read_close(AVFormatContext *s)
109 static int mp3_read_header(AVFormatContext *s,
110 AVFormatParameters *ap)
115 st = av_new_stream(s, 0);
117 return AVERROR_NOMEM;
119 st->codec.codec_type = CODEC_TYPE_AUDIO;
120 st->codec.codec_id = CODEC_ID_MP2;
122 /* looking for 11111111 111MMLLC - MPEG synchronization tag
123 MM: 00 - MPEG-2.5, 10 - MPEG-2, 11 - MPEG-1
124 LL: 11 - Layer I, 10 - Layer II, 01 - Layer III
125 XXX: this code does not read more bytes from file
126 so if ID3 (or other stuff) length > IO_BUFFER_SIZE it fails back to CODEC_ID_MP2 */
127 for(pos=0; pos < s->pb.buffer_size-1; pos++)
128 if( s->pb.buffer[pos] == 0xFF && (s->pb.buffer[pos] & 0xE0) == 0xE0 )
131 if( pos < s->pb.buffer_size-1 && (s->pb.buffer[pos+1] & 6) == 2 )
132 st->codec.codec_id = CODEC_ID_MP3LAME;
134 /* the parameters will be extracted from the compressed bitstream */
139 static int ac3_read_header(AVFormatContext *s,
140 AVFormatParameters *ap)
144 st = av_new_stream(s, 0);
146 return AVERROR_NOMEM;
148 st->codec.codec_type = CODEC_TYPE_AUDIO;
149 st->codec.codec_id = CODEC_ID_AC3;
150 /* the parameters will be extracted from the compressed bitstream */
154 /* mpeg1/h263 input */
155 static int video_read_header(AVFormatContext *s,
156 AVFormatParameters *ap)
160 st = av_new_stream(s, 0);
162 return AVERROR_NOMEM;
164 st->codec.codec_type = CODEC_TYPE_VIDEO;
165 st->codec.codec_id = s->iformat->value;
166 /* for mjpeg, specify frame rate */
167 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
168 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
170 st->codec.frame_rate = ap->frame_rate;
171 st->codec.frame_rate_base = ap->frame_rate_base;
173 st->codec.frame_rate = 25;
174 st->codec.frame_rate_base = 1;
180 #define SEQ_START_CODE 0x000001b3
181 #define GOP_START_CODE 0x000001b8
182 #define PICTURE_START_CODE 0x00000100
184 /* XXX: improve that by looking at several start codes */
185 static int mpegvideo_probe(AVProbeData *p)
190 /* we search the first start code. If it is a sequence, gop or
191 picture start code then we decide it is an mpeg video
192 stream. We do not send highest value to give a chance to mpegts */
193 /* NOTE: the search range was restricted to avoid too many false
199 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
200 if ((code & 0xffffff00) == 0x100) {
201 if (code == SEQ_START_CODE ||
202 code == GOP_START_CODE ||
203 code == PICTURE_START_CODE)
211 static int h263_probe(AVProbeData *p)
219 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
227 AVInputFormat mp3_iformat = {
235 .extensions = "mp2,mp3", /* XXX: use probe */
238 AVOutputFormat mp2_oformat = {
240 "MPEG audio layer 2",
242 #ifdef CONFIG_MP3LAME
255 #ifdef CONFIG_MP3LAME
256 AVOutputFormat mp3_oformat = {
258 "MPEG audio layer 3",
270 AVInputFormat ac3_iformat = {
281 AVOutputFormat ac3_oformat = {
294 AVInputFormat h263_iformat = {
302 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
303 .value = CODEC_ID_H263,
306 AVOutputFormat h263_oformat = {
319 AVInputFormat m4v_iformat = {
321 "raw MPEG4 video format",
323 NULL /*mpegvideo_probe*/,
327 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
328 .value = CODEC_ID_MPEG4,
331 AVOutputFormat m4v_oformat = {
333 "raw MPEG4 video format",
344 AVInputFormat h264_iformat = {
346 "raw H264 video format",
348 NULL /*mpegvideo_probe*/,
352 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
353 .value = CODEC_ID_H264,
356 AVOutputFormat h264_oformat = {
358 "raw H264 video format",
369 AVInputFormat mpegvideo_iformat = {
377 .value = CODEC_ID_MPEG1VIDEO,
380 AVOutputFormat mpeg1video_oformat = {
393 AVInputFormat mjpeg_iformat = {
401 .extensions = "mjpg,mjpeg",
402 .value = CODEC_ID_MJPEG,
405 AVOutputFormat mjpeg_oformat = {
420 #define PCMDEF(name, long_name, ext, codec) \
421 AVInputFormat pcm_ ## name ## _iformat = {\
433 AVOutputFormat pcm_ ## name ## _oformat = {\
446 #ifdef WORDS_BIGENDIAN
448 #define LE_DEF(s) NULL
450 #define BE_DEF(s) NULL
455 PCMDEF(s16le, "pcm signed 16 bit little endian format",
456 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
458 PCMDEF(s16be, "pcm signed 16 bit big endian format",
459 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
461 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
462 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
464 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
465 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
467 PCMDEF(s8, "pcm signed 8 bit format",
468 "sb", CODEC_ID_PCM_S8)
470 PCMDEF(u8, "pcm unsigned 8 bit format",
471 "ub", CODEC_ID_PCM_U8)
473 PCMDEF(mulaw, "pcm mu law format",
474 "ul", CODEC_ID_PCM_MULAW)
476 PCMDEF(alaw, "pcm A law format",
477 "al", CODEC_ID_PCM_ALAW)
479 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
481 int packet_size, ret, width, height;
482 AVStream *st = s->streams[0];
484 width = st->codec.width;
485 height = st->codec.height;
487 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
491 if (av_new_packet(pkt, packet_size) < 0)
494 pkt->stream_index = 0;
496 /* bypass buffered I/O */
497 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
499 ret = get_buffer(&s->pb, pkt->data, pkt->size);
501 if (ret != pkt->size) {
509 AVInputFormat rawvideo_iformat = {
515 rawvideo_read_packet,
518 .value = CODEC_ID_RAWVIDEO,
521 AVOutputFormat rawvideo_oformat = {
534 static int null_write_packet(struct AVFormatContext *s,
536 unsigned char *buf, int size, int force_pts)
541 AVOutputFormat null_oformat = {
547 #ifdef WORDS_BIGENDIAN
556 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
561 av_register_input_format(&mp3_iformat);
562 av_register_output_format(&mp2_oformat);
563 #ifdef CONFIG_MP3LAME
564 av_register_output_format(&mp3_oformat);
566 av_register_input_format(&ac3_iformat);
567 av_register_output_format(&ac3_oformat);
569 av_register_input_format(&h263_iformat);
570 av_register_output_format(&h263_oformat);
572 av_register_input_format(&m4v_iformat);
573 av_register_output_format(&m4v_oformat);
575 av_register_input_format(&h264_iformat);
576 av_register_output_format(&h264_oformat);
578 av_register_input_format(&mpegvideo_iformat);
579 av_register_output_format(&mpeg1video_oformat);
581 av_register_input_format(&mjpeg_iformat);
582 av_register_output_format(&mjpeg_oformat);
584 av_register_input_format(&pcm_s16le_iformat);
585 av_register_output_format(&pcm_s16le_oformat);
586 av_register_input_format(&pcm_s16be_iformat);
587 av_register_output_format(&pcm_s16be_oformat);
588 av_register_input_format(&pcm_u16le_iformat);
589 av_register_output_format(&pcm_u16le_oformat);
590 av_register_input_format(&pcm_u16be_iformat);
591 av_register_output_format(&pcm_u16be_oformat);
592 av_register_input_format(&pcm_s8_iformat);
593 av_register_output_format(&pcm_s8_oformat);
594 av_register_input_format(&pcm_u8_iformat);
595 av_register_output_format(&pcm_u8_oformat);
596 av_register_input_format(&pcm_mulaw_iformat);
597 av_register_output_format(&pcm_mulaw_oformat);
598 av_register_input_format(&pcm_alaw_iformat);
599 av_register_output_format(&pcm_alaw_oformat);
601 av_register_input_format(&rawvideo_iformat);
602 av_register_output_format(&rawvideo_oformat);
604 av_register_output_format(&null_oformat);