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 size= RAW_PACKET_SIZE;
89 if (av_new_packet(pkt, size) < 0)
92 pkt->stream_index = 0;
93 ret = get_buffer(&s->pb, pkt->data, size);
98 /* note: we need to modify the packet size here to handle the last
104 int raw_read_close(AVFormatContext *s)
110 static int mp3_read_header(AVFormatContext *s,
111 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;
121 /* the parameters will be extracted from the compressed bitstream */
125 /* mpeg1/h263 input */
126 static int video_read_header(AVFormatContext *s,
127 AVFormatParameters *ap)
131 st = av_new_stream(s, 0);
133 return AVERROR_NOMEM;
135 st->codec.codec_type = CODEC_TYPE_VIDEO;
136 st->codec.codec_id = s->iformat->value;
137 /* for mjpeg, specify frame rate */
138 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
139 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
141 st->codec.frame_rate = ap->frame_rate;
143 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
149 #define SEQ_START_CODE 0x000001b3
150 #define GOP_START_CODE 0x000001b8
151 #define PICTURE_START_CODE 0x00000100
153 /* XXX: improve that by looking at several start codes */
154 static int mpegvideo_probe(AVProbeData *p)
159 /* we search the first start code. If it is a sequence, gop or
160 picture start code then we decide it is an mpeg video
161 stream. We do not send highest value to give a chance to mpegts */
162 for(i=0;i<p->buf_size;i++) {
164 code = (code << 8) | c;
165 if ((code & 0xffffff00) == 0x100) {
166 if (code == SEQ_START_CODE ||
167 code == GOP_START_CODE ||
168 code == PICTURE_START_CODE)
177 AVInputFormat mp3_iformat = {
185 .extensions = "mp2,mp3", /* XXX: use probe */
188 AVOutputFormat mp2_oformat = {
190 "MPEG audio layer 2",
202 AVInputFormat ac3_iformat = {
211 .value = CODEC_ID_AC3,
214 AVOutputFormat ac3_oformat = {
227 AVOutputFormat h263_oformat = {
240 AVInputFormat m4v_iformat = {
242 "raw MPEG4 video format",
244 NULL /*mpegvideo_probe*/,
248 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
249 .value = CODEC_ID_MPEG4,
252 AVOutputFormat m4v_oformat = {
254 "raw MPEG4 video format",
265 AVInputFormat mpegvideo_iformat = {
273 .value = CODEC_ID_MPEG1VIDEO,
276 AVOutputFormat mpeg1video_oformat = {
289 AVInputFormat mjpeg_iformat = {
297 .extensions = "mjpg,mjpeg",
298 .value = CODEC_ID_MJPEG,
301 AVOutputFormat mjpeg_oformat = {
316 #define PCMDEF(name, long_name, ext, codec) \
317 AVInputFormat pcm_ ## name ## _iformat = {\
329 AVOutputFormat pcm_ ## name ## _oformat = {\
342 #ifdef WORDS_BIGENDIAN
344 #define LE_DEF(s) NULL
346 #define BE_DEF(s) NULL
351 PCMDEF(s16le, "pcm signed 16 bit little endian format",
352 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
354 PCMDEF(s16be, "pcm signed 16 bit big endian format",
355 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
357 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
358 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
360 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
361 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
363 PCMDEF(s8, "pcm signed 8 bit format",
364 "sb", CODEC_ID_PCM_S8)
366 PCMDEF(u8, "pcm unsigned 8 bit format",
367 "ub", CODEC_ID_PCM_U8)
369 PCMDEF(mulaw, "pcm mu law format",
370 "ul", CODEC_ID_PCM_MULAW)
372 PCMDEF(alaw, "pcm A law format",
373 "al", CODEC_ID_PCM_ALAW)
375 int rawvideo_read_packet(AVFormatContext *s,
378 int packet_size, ret, width, height;
379 AVStream *st = s->streams[0];
381 width = st->codec.width;
382 height = st->codec.height;
384 switch(st->codec.pix_fmt) {
385 case PIX_FMT_YUV420P:
386 packet_size = (width * height * 3) / 2;
389 packet_size = (width * height * 2);
393 packet_size = (width * height * 3);
400 if (av_new_packet(pkt, packet_size) < 0)
403 pkt->stream_index = 0;
405 /* bypass buffered I/O */
406 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
408 ret = get_buffer(&s->pb, pkt->data, pkt->size);
410 if (ret != pkt->size) {
418 AVInputFormat rawvideo_iformat = {
424 rawvideo_read_packet,
427 .value = CODEC_ID_RAWVIDEO,
430 AVOutputFormat rawvideo_oformat = {
443 static int null_write_packet(struct AVFormatContext *s,
445 unsigned char *buf, int size, int force_pts)
450 AVOutputFormat null_oformat = {
456 #ifdef WORDS_BIGENDIAN
465 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
470 av_register_input_format(&mp3_iformat);
471 av_register_output_format(&mp2_oformat);
473 av_register_input_format(&ac3_iformat);
474 av_register_output_format(&ac3_oformat);
476 av_register_output_format(&h263_oformat);
478 av_register_input_format(&m4v_iformat);
479 av_register_output_format(&m4v_oformat);
481 av_register_input_format(&mpegvideo_iformat);
482 av_register_output_format(&mpeg1video_oformat);
484 av_register_input_format(&mjpeg_iformat);
485 av_register_output_format(&mjpeg_oformat);
487 av_register_input_format(&pcm_s16le_iformat);
488 av_register_output_format(&pcm_s16le_oformat);
489 av_register_input_format(&pcm_s16be_iformat);
490 av_register_output_format(&pcm_s16be_oformat);
491 av_register_input_format(&pcm_u16le_iformat);
492 av_register_output_format(&pcm_u16le_oformat);
493 av_register_input_format(&pcm_u16be_iformat);
494 av_register_output_format(&pcm_u16be_oformat);
495 av_register_input_format(&pcm_s8_iformat);
496 av_register_output_format(&pcm_s8_oformat);
497 av_register_input_format(&pcm_u8_iformat);
498 av_register_output_format(&pcm_u8_oformat);
499 av_register_input_format(&pcm_mulaw_iformat);
500 av_register_output_format(&pcm_mulaw_oformat);
501 av_register_input_format(&pcm_alaw_iformat);
502 av_register_output_format(&pcm_alaw_oformat);
504 av_register_input_format(&rawvideo_iformat);
505 av_register_output_format(&rawvideo_oformat);
507 av_register_output_format(&null_oformat);