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 int raw_write_header(struct AVFormatContext *s)
27 int raw_write_packet(struct AVFormatContext *s,
29 unsigned char *buf, int size, int force_pts)
31 put_buffer(&s->pb, buf, size);
32 put_flush_packet(&s->pb);
36 int raw_write_trailer(struct AVFormatContext *s)
42 static int raw_read_header(AVFormatContext *s,
43 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;
65 case CODEC_TYPE_VIDEO:
66 st->codec.frame_rate = ap->frame_rate;
67 st->codec.width = ap->width;
68 st->codec.height = ap->height;
79 #define RAW_PACKET_SIZE 1024
81 int raw_read_packet(AVFormatContext *s,
85 AVStream *st = s->streams[0];
87 if(st->codec.codec_id == CODEC_ID_MPEG4)
88 size= 1024*1024; //cant handle partial frames
90 size= RAW_PACKET_SIZE;
92 if (av_new_packet(pkt, size) < 0)
95 pkt->stream_index = 0;
96 ret = get_buffer(&s->pb, pkt->data, size);
101 /* note: we need to modify the packet size here to handle the last
107 int raw_read_close(AVFormatContext *s)
113 static int mp3_read_header(AVFormatContext *s,
114 AVFormatParameters *ap)
118 st = av_new_stream(s, 0);
120 return AVERROR_NOMEM;
122 st->codec.codec_type = CODEC_TYPE_AUDIO;
123 st->codec.codec_id = CODEC_ID_MP2;
124 /* the parameters will be extracted from the compressed bitstream */
128 /* mpeg1/h263 input */
129 static int video_read_header(AVFormatContext *s,
130 AVFormatParameters *ap)
134 st = av_new_stream(s, 0);
136 return AVERROR_NOMEM;
138 st->codec.codec_type = CODEC_TYPE_VIDEO;
139 st->codec.codec_id = s->iformat->value;
140 /* for mjpeg, specify frame rate */
141 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
142 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
144 st->codec.frame_rate = ap->frame_rate;
146 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
152 #define SEQ_START_CODE 0x000001b3
153 #define GOP_START_CODE 0x000001b8
154 #define PICTURE_START_CODE 0x00000100
156 /* XXX: improve that by looking at several start codes */
157 static int mpegvideo_probe(AVProbeData *p)
162 /* we search the first start code. If it is a sequence, gop or
163 picture start code then we decide it is an mpeg video
164 stream. We do not send highest value to give a chance to mpegts */
165 for(i=0;i<p->buf_size;i++) {
167 code = (code << 8) | c;
168 if ((code & 0xffffff00) == 0x100) {
169 if (code == SEQ_START_CODE ||
170 code == GOP_START_CODE ||
171 code == PICTURE_START_CODE)
172 return AVPROBE_SCORE_MAX - 1;
180 AVInputFormat mp3_iformat = {
188 extensions: "mp2,mp3", /* XXX: use probe */
191 AVOutputFormat mp2_oformat = {
193 "MPEG audio layer 2",
205 AVInputFormat ac3_iformat = {
217 AVOutputFormat ac3_oformat = {
230 AVOutputFormat h263_oformat = {
243 AVInputFormat m4v_iformat = {
245 "raw MPEG4 video format",
247 NULL /*mpegvideo_probe*/,
251 extensions: "m4v", //FIXME remove after writing mpeg4_probe
252 value: CODEC_ID_MPEG4,
255 AVOutputFormat m4v_oformat = {
257 "raw MPEG4 video format",
268 AVInputFormat mpegvideo_iformat = {
276 value: CODEC_ID_MPEG1VIDEO,
279 AVOutputFormat mpeg1video_oformat = {
292 AVInputFormat mjpeg_iformat = {
300 extensions: "mjpg,mjpeg",
301 value: CODEC_ID_MJPEG,
304 AVOutputFormat mjpeg_oformat = {
319 #define PCMDEF(name, long_name, ext, codec) \
320 AVInputFormat pcm_ ## name ## _iformat = {\
332 AVOutputFormat pcm_ ## name ## _oformat = {\
345 #ifdef WORDS_BIGENDIAN
347 #define LE_DEF(s) NULL
349 #define BE_DEF(s) NULL
354 PCMDEF(s16le, "pcm signed 16 bit little endian format",
355 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
357 PCMDEF(s16be, "pcm signed 16 bit big endian format",
358 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
360 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
361 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
363 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
364 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
366 PCMDEF(s8, "pcm signed 8 bit format",
367 "sb", CODEC_ID_PCM_S8)
369 PCMDEF(u8, "pcm unsigned 8 bit format",
370 "ub", CODEC_ID_PCM_U8)
372 PCMDEF(mulaw, "pcm mu law format",
373 "ul", CODEC_ID_PCM_MULAW)
375 PCMDEF(alaw, "pcm A law format",
376 "al", CODEC_ID_PCM_ALAW)
378 int rawvideo_read_packet(AVFormatContext *s,
381 int packet_size, ret, width, height;
382 AVStream *st = s->streams[0];
384 width = st->codec.width;
385 height = st->codec.height;
387 switch(st->codec.pix_fmt) {
388 case PIX_FMT_YUV420P:
389 packet_size = (width * height * 3) / 2;
392 packet_size = (width * height * 2);
396 packet_size = (width * height * 3);
403 if (av_new_packet(pkt, packet_size) < 0)
406 pkt->stream_index = 0;
408 /* bypass buffered I/O */
409 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
411 ret = get_buffer(&s->pb, pkt->data, pkt->size);
413 if (ret != pkt->size) {
421 AVInputFormat rawvideo_iformat = {
427 rawvideo_read_packet,
430 value: CODEC_ID_RAWVIDEO,
433 AVOutputFormat rawvideo_oformat = {
448 av_register_input_format(&mp3_iformat);
449 av_register_output_format(&mp2_oformat);
451 av_register_input_format(&ac3_iformat);
452 av_register_output_format(&ac3_oformat);
454 av_register_output_format(&h263_oformat);
456 av_register_input_format(&m4v_iformat);
457 av_register_output_format(&m4v_oformat);
459 av_register_input_format(&mpegvideo_iformat);
460 av_register_output_format(&mpeg1video_oformat);
462 av_register_input_format(&mjpeg_iformat);
463 av_register_output_format(&mjpeg_oformat);
465 av_register_input_format(&pcm_s16le_iformat);
466 av_register_output_format(&pcm_s16le_oformat);
467 av_register_input_format(&pcm_s16be_iformat);
468 av_register_output_format(&pcm_s16be_oformat);
469 av_register_input_format(&pcm_u16le_iformat);
470 av_register_output_format(&pcm_u16le_oformat);
471 av_register_input_format(&pcm_u16be_iformat);
472 av_register_output_format(&pcm_u16be_oformat);
473 av_register_input_format(&pcm_s8_iformat);
474 av_register_output_format(&pcm_s8_oformat);
475 av_register_input_format(&pcm_u8_iformat);
476 av_register_output_format(&pcm_u8_oformat);
477 av_register_input_format(&pcm_mulaw_iformat);
478 av_register_output_format(&pcm_mulaw_oformat);
479 av_register_input_format(&pcm_alaw_iformat);
480 av_register_output_format(&pcm_alaw_oformat);
482 av_register_input_format(&rawvideo_iformat);
483 av_register_output_format(&rawvideo_oformat);