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
21 #ifdef CONFIG_ENCODERS
23 static int raw_write_header(struct AVFormatContext *s)
28 static int raw_write_packet(struct AVFormatContext *s, int stream_index,
29 const uint8_t *buf, int size, int64_t pts)
31 put_buffer(&s->pb, buf, size);
32 put_flush_packet(&s->pb);
36 static int raw_write_trailer(struct AVFormatContext *s)
40 #endif //CONFIG_ENCODERS
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;
65 case CODEC_TYPE_VIDEO:
66 st->codec.frame_rate = ap->frame_rate;
67 st->codec.frame_rate_base = ap->frame_rate_base;
68 st->codec.width = ap->width;
69 st->codec.height = ap->height;
70 st->codec.pix_fmt = ap->pix_fmt;
81 #define RAW_PACKET_SIZE 1024
83 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
86 // AVStream *st = s->streams[0];
88 size= RAW_PACKET_SIZE;
90 if (av_new_packet(pkt, size) < 0)
93 pkt->stream_index = 0;
94 ret = get_buffer(&s->pb, pkt->data, size);
99 /* note: we need to modify the packet size here to handle the last
105 static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
109 size = RAW_PACKET_SIZE;
111 if (av_new_packet(pkt, size) < 0)
114 pkt->stream_index = 0;
115 ret = get_partial_buffer(&s->pb, pkt->data, size);
124 static int raw_read_close(AVFormatContext *s)
129 int pcm_read_seek(AVFormatContext *s,
130 int stream_index, int64_t timestamp)
133 int block_align, byte_rate;
137 switch(st->codec.codec_id) {
138 case CODEC_ID_PCM_S16LE:
139 case CODEC_ID_PCM_S16BE:
140 case CODEC_ID_PCM_U16LE:
141 case CODEC_ID_PCM_U16BE:
142 block_align = 2 * st->codec.channels;
143 byte_rate = block_align * st->codec.sample_rate;
145 case CODEC_ID_PCM_S8:
146 case CODEC_ID_PCM_U8:
147 case CODEC_ID_PCM_MULAW:
148 case CODEC_ID_PCM_ALAW:
149 block_align = st->codec.channels;
150 byte_rate = block_align * st->codec.sample_rate;
153 block_align = st->codec.block_align;
154 byte_rate = st->codec.bit_rate / 8;
158 if (block_align <= 0 || byte_rate <= 0)
161 /* compute the position by aligning it to block_align */
162 pos = av_rescale(timestamp * byte_rate, st->time_base.num, st->time_base.den);
163 pos = (pos / block_align) * block_align;
165 /* recompute exact position */
166 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
167 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
172 static int ac3_read_header(AVFormatContext *s,
173 AVFormatParameters *ap)
177 st = av_new_stream(s, 0);
179 return AVERROR_NOMEM;
181 st->codec.codec_type = CODEC_TYPE_AUDIO;
182 st->codec.codec_id = CODEC_ID_AC3;
183 st->need_parsing = 1;
184 /* the parameters will be extracted from the compressed bitstream */
188 /* mpeg1/h263 input */
189 static int video_read_header(AVFormatContext *s,
190 AVFormatParameters *ap)
194 st = av_new_stream(s, 0);
196 return AVERROR_NOMEM;
198 st->codec.codec_type = CODEC_TYPE_VIDEO;
199 st->codec.codec_id = s->iformat->value;
200 st->need_parsing = 1;
202 /* for mjpeg, specify frame rate */
203 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
204 if (st->codec.codec_id == CODEC_ID_MJPEG ||
205 st->codec.codec_id == CODEC_ID_MPEG4) {
206 if (ap && ap->frame_rate) {
207 st->codec.frame_rate = ap->frame_rate;
208 st->codec.frame_rate_base = ap->frame_rate_base;
210 st->codec.frame_rate = 25;
211 st->codec.frame_rate_base = 1;
217 #define SEQ_START_CODE 0x000001b3
218 #define GOP_START_CODE 0x000001b8
219 #define PICTURE_START_CODE 0x00000100
221 /* XXX: improve that by looking at several start codes */
222 static int mpegvideo_probe(AVProbeData *p)
227 /* we search the first start code. If it is a sequence, gop or
228 picture start code then we decide it is an mpeg video
229 stream. We do not send highest value to give a chance to mpegts */
230 /* NOTE: the search range was restricted to avoid too many false
236 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
237 if ((code & 0xffffff00) == 0x100) {
238 if (code == SEQ_START_CODE ||
239 code == GOP_START_CODE ||
240 code == PICTURE_START_CODE)
248 static int h263_probe(AVProbeData *p)
256 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
263 AVInputFormat ac3_iformat = {
269 raw_read_partial_packet,
274 #ifdef CONFIG_ENCODERS
275 AVOutputFormat ac3_oformat = {
287 #endif //CONFIG_ENCODERS
289 AVInputFormat h263_iformat = {
295 raw_read_partial_packet,
297 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
298 .value = CODEC_ID_H263,
301 #ifdef CONFIG_ENCODERS
302 AVOutputFormat h263_oformat = {
314 #endif //CONFIG_ENCODERS
316 AVInputFormat m4v_iformat = {
318 "raw MPEG4 video format",
320 NULL /*mpegvideo_probe*/,
322 raw_read_partial_packet,
324 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
325 .value = CODEC_ID_MPEG4,
328 #ifdef CONFIG_ENCODERS
329 AVOutputFormat m4v_oformat = {
331 "raw MPEG4 video format",
341 #endif //CONFIG_ENCODERS
343 AVInputFormat h264_iformat = {
345 "raw H264 video format",
347 NULL /*mpegvideo_probe*/,
349 raw_read_partial_packet,
351 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
352 .value = CODEC_ID_H264,
355 #ifdef CONFIG_ENCODERS
356 AVOutputFormat h264_oformat = {
358 "raw H264 video format",
368 #endif //CONFIG_ENCODERS
370 AVInputFormat mpegvideo_iformat = {
376 raw_read_partial_packet,
378 .value = CODEC_ID_MPEG1VIDEO,
381 #ifdef CONFIG_ENCODERS
382 AVOutputFormat mpeg1video_oformat = {
394 #endif //CONFIG_ENCODERS
396 AVInputFormat mjpeg_iformat = {
402 raw_read_partial_packet,
404 .extensions = "mjpg,mjpeg",
405 .value = CODEC_ID_MJPEG,
408 #ifdef CONFIG_ENCODERS
409 AVOutputFormat mjpeg_oformat = {
421 #endif //CONFIG_ENCODERS
425 #define PCMINPUTDEF(name, long_name, ext, codec) \
426 AVInputFormat pcm_ ## name ## _iformat = {\
439 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
441 #define PCMDEF(name, long_name, ext, codec) \
442 PCMINPUTDEF(name, long_name, ext, codec)
446 #define PCMDEF(name, long_name, ext, codec) \
447 PCMINPUTDEF(name, long_name, ext, codec)\
449 AVOutputFormat pcm_ ## name ## _oformat = {\
461 #endif //CONFIG_ENCODERS
463 #ifdef WORDS_BIGENDIAN
465 #define LE_DEF(s) NULL
467 #define BE_DEF(s) NULL
472 PCMDEF(s16le, "pcm signed 16 bit little endian format",
473 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
475 PCMDEF(s16be, "pcm signed 16 bit big endian format",
476 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
478 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
479 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
481 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
482 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
484 PCMDEF(s8, "pcm signed 8 bit format",
485 "sb", CODEC_ID_PCM_S8)
487 PCMDEF(u8, "pcm unsigned 8 bit format",
488 "ub", CODEC_ID_PCM_U8)
490 PCMDEF(mulaw, "pcm mu law format",
491 "ul", CODEC_ID_PCM_MULAW)
493 PCMDEF(alaw, "pcm A law format",
494 "al", CODEC_ID_PCM_ALAW)
496 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
498 int packet_size, ret, width, height;
499 AVStream *st = s->streams[0];
501 width = st->codec.width;
502 height = st->codec.height;
504 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
508 if (av_new_packet(pkt, packet_size) < 0)
511 pkt->stream_index = 0;
513 /* bypass buffered I/O */
514 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
516 ret = get_buffer(&s->pb, pkt->data, pkt->size);
518 if (ret != pkt->size) {
526 AVInputFormat rawvideo_iformat = {
532 rawvideo_read_packet,
535 .value = CODEC_ID_RAWVIDEO,
538 #ifdef CONFIG_ENCODERS
539 AVOutputFormat rawvideo_oformat = {
551 #endif //CONFIG_ENCODERS
553 #ifdef CONFIG_ENCODERS
554 static int null_write_packet(struct AVFormatContext *s,
556 const uint8_t *buf, int size, int64_t pts)
561 AVOutputFormat null_oformat = {
567 #ifdef WORDS_BIGENDIAN
576 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
578 #endif //CONFIG_ENCODERS
580 #ifndef CONFIG_ENCODERS
581 #define av_register_output_format(format)
583 #ifndef CONFIG_DECODERS
584 #define av_register_input_format(format)
589 av_register_input_format(&ac3_iformat);
590 av_register_output_format(&ac3_oformat);
592 av_register_input_format(&h263_iformat);
593 av_register_output_format(&h263_oformat);
595 av_register_input_format(&m4v_iformat);
596 av_register_output_format(&m4v_oformat);
598 av_register_input_format(&h264_iformat);
599 av_register_output_format(&h264_oformat);
601 av_register_input_format(&mpegvideo_iformat);
602 av_register_output_format(&mpeg1video_oformat);
604 av_register_input_format(&mjpeg_iformat);
605 av_register_output_format(&mjpeg_oformat);
607 av_register_input_format(&pcm_s16le_iformat);
608 av_register_output_format(&pcm_s16le_oformat);
609 av_register_input_format(&pcm_s16be_iformat);
610 av_register_output_format(&pcm_s16be_oformat);
611 av_register_input_format(&pcm_u16le_iformat);
612 av_register_output_format(&pcm_u16le_oformat);
613 av_register_input_format(&pcm_u16be_iformat);
614 av_register_output_format(&pcm_u16be_oformat);
615 av_register_input_format(&pcm_s8_iformat);
616 av_register_output_format(&pcm_s8_oformat);
617 av_register_input_format(&pcm_u8_iformat);
618 av_register_output_format(&pcm_u8_oformat);
619 av_register_input_format(&pcm_mulaw_iformat);
620 av_register_output_format(&pcm_mulaw_oformat);
621 av_register_input_format(&pcm_alaw_iformat);
622 av_register_output_format(&pcm_alaw_oformat);
624 av_register_input_format(&rawvideo_iformat);
625 av_register_output_format(&rawvideo_oformat);
627 av_register_output_format(&null_oformat);