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, AVPacket *pkt)
30 put_buffer(&s->pb, pkt->data, pkt->size);
31 put_flush_packet(&s->pb);
35 static int raw_write_trailer(struct AVFormatContext *s)
39 #endif //CONFIG_ENCODERS
42 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
47 st = av_new_stream(s, 0);
51 id = s->iformat->value;
52 if (id == CODEC_ID_RAWVIDEO) {
53 st->codec.codec_type = CODEC_TYPE_VIDEO;
55 st->codec.codec_type = CODEC_TYPE_AUDIO;
57 st->codec.codec_id = id;
59 switch(st->codec.codec_type) {
60 case CODEC_TYPE_AUDIO:
61 st->codec.sample_rate = ap->sample_rate;
62 st->codec.channels = ap->channels;
64 case CODEC_TYPE_VIDEO:
65 st->codec.frame_rate = ap->frame_rate;
66 st->codec.frame_rate_base = ap->frame_rate_base;
67 st->codec.width = ap->width;
68 st->codec.height = ap->height;
69 st->codec.pix_fmt = ap->pix_fmt;
80 #define RAW_PACKET_SIZE 1024
82 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
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 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->stream_index = 0;
114 ret = get_partial_buffer(&s->pb, pkt->data, size);
123 static int raw_read_close(AVFormatContext *s)
128 int pcm_read_seek(AVFormatContext *s,
129 int stream_index, int64_t timestamp)
132 int block_align, byte_rate;
136 switch(st->codec.codec_id) {
137 case CODEC_ID_PCM_S16LE:
138 case CODEC_ID_PCM_S16BE:
139 case CODEC_ID_PCM_U16LE:
140 case CODEC_ID_PCM_U16BE:
141 block_align = 2 * st->codec.channels;
142 byte_rate = block_align * st->codec.sample_rate;
144 case CODEC_ID_PCM_S8:
145 case CODEC_ID_PCM_U8:
146 case CODEC_ID_PCM_MULAW:
147 case CODEC_ID_PCM_ALAW:
148 block_align = st->codec.channels;
149 byte_rate = block_align * st->codec.sample_rate;
152 block_align = st->codec.block_align;
153 byte_rate = st->codec.bit_rate / 8;
157 if (block_align <= 0 || byte_rate <= 0)
160 /* compute the position by aligning it to block_align */
161 pos = av_rescale(timestamp * byte_rate, st->time_base.num, st->time_base.den);
162 pos = (pos / block_align) * block_align;
164 /* recompute exact position */
165 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
166 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
171 static int ac3_read_header(AVFormatContext *s,
172 AVFormatParameters *ap)
176 st = av_new_stream(s, 0);
178 return AVERROR_NOMEM;
180 st->codec.codec_type = CODEC_TYPE_AUDIO;
181 st->codec.codec_id = CODEC_ID_AC3;
182 st->need_parsing = 1;
183 /* the parameters will be extracted from the compressed bitstream */
187 /* mpeg1/h263 input */
188 static int video_read_header(AVFormatContext *s,
189 AVFormatParameters *ap)
193 st = av_new_stream(s, 0);
195 return AVERROR_NOMEM;
197 st->codec.codec_type = CODEC_TYPE_VIDEO;
198 st->codec.codec_id = s->iformat->value;
199 st->need_parsing = 1;
201 /* for mjpeg, specify frame rate */
202 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
203 if (st->codec.codec_id == CODEC_ID_MJPEG ||
204 st->codec.codec_id == CODEC_ID_MPEG4) {
205 if (ap && ap->frame_rate) {
206 st->codec.frame_rate = ap->frame_rate;
207 st->codec.frame_rate_base = ap->frame_rate_base;
209 st->codec.frame_rate = 25;
210 st->codec.frame_rate_base = 1;
216 #define SEQ_START_CODE 0x000001b3
217 #define GOP_START_CODE 0x000001b8
218 #define PICTURE_START_CODE 0x00000100
220 /* XXX: improve that by looking at several start codes */
221 static int mpegvideo_probe(AVProbeData *p)
226 /* we search the first start code. If it is a sequence, gop or
227 picture start code then we decide it is an mpeg video
228 stream. We do not send highest value to give a chance to mpegts */
229 /* NOTE: the search range was restricted to avoid too many false
235 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
236 if ((code & 0xffffff00) == 0x100) {
237 if (code == SEQ_START_CODE ||
238 code == GOP_START_CODE ||
239 code == PICTURE_START_CODE)
247 static int h263_probe(AVProbeData *p)
255 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
262 static int h261_probe(AVProbeData *p)
270 code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
277 AVInputFormat ac3_iformat = {
283 raw_read_partial_packet,
288 #ifdef CONFIG_ENCODERS
289 AVOutputFormat ac3_oformat = {
301 #endif //CONFIG_ENCODERS
303 AVInputFormat h261_iformat = {
309 raw_read_partial_packet,
311 .extensions = "h261",
312 .value = CODEC_ID_H261,
315 AVInputFormat h263_iformat = {
321 raw_read_partial_packet,
323 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
324 .value = CODEC_ID_H263,
327 #ifdef CONFIG_ENCODERS
328 AVOutputFormat h263_oformat = {
340 #endif //CONFIG_ENCODERS
342 AVInputFormat m4v_iformat = {
344 "raw MPEG4 video format",
346 NULL /*mpegvideo_probe*/,
348 raw_read_partial_packet,
350 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
351 .value = CODEC_ID_MPEG4,
354 #ifdef CONFIG_ENCODERS
355 AVOutputFormat m4v_oformat = {
357 "raw MPEG4 video format",
367 #endif //CONFIG_ENCODERS
369 AVInputFormat h264_iformat = {
371 "raw H264 video format",
373 NULL /*mpegvideo_probe*/,
375 raw_read_partial_packet,
377 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
378 .value = CODEC_ID_H264,
381 #ifdef CONFIG_ENCODERS
382 AVOutputFormat h264_oformat = {
384 "raw H264 video format",
394 #endif //CONFIG_ENCODERS
396 AVInputFormat mpegvideo_iformat = {
402 raw_read_partial_packet,
404 .value = CODEC_ID_MPEG1VIDEO,
407 #ifdef CONFIG_ENCODERS
408 AVOutputFormat mpeg1video_oformat = {
420 #endif //CONFIG_ENCODERS
422 AVInputFormat mjpeg_iformat = {
428 raw_read_partial_packet,
430 .extensions = "mjpg,mjpeg",
431 .value = CODEC_ID_MJPEG,
434 #ifdef CONFIG_ENCODERS
435 AVOutputFormat mjpeg_oformat = {
447 #endif //CONFIG_ENCODERS
451 #define PCMINPUTDEF(name, long_name, ext, codec) \
452 AVInputFormat pcm_ ## name ## _iformat = {\
465 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
467 #define PCMDEF(name, long_name, ext, codec) \
468 PCMINPUTDEF(name, long_name, ext, codec)
472 #define PCMDEF(name, long_name, ext, codec) \
473 PCMINPUTDEF(name, long_name, ext, codec)\
475 AVOutputFormat pcm_ ## name ## _oformat = {\
487 #endif //CONFIG_ENCODERS
489 #ifdef WORDS_BIGENDIAN
491 #define LE_DEF(s) NULL
493 #define BE_DEF(s) NULL
498 PCMDEF(s16le, "pcm signed 16 bit little endian format",
499 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
501 PCMDEF(s16be, "pcm signed 16 bit big endian format",
502 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
504 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
505 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
507 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
508 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
510 PCMDEF(s8, "pcm signed 8 bit format",
511 "sb", CODEC_ID_PCM_S8)
513 PCMDEF(u8, "pcm unsigned 8 bit format",
514 "ub", CODEC_ID_PCM_U8)
516 PCMDEF(mulaw, "pcm mu law format",
517 "ul", CODEC_ID_PCM_MULAW)
519 PCMDEF(alaw, "pcm A law format",
520 "al", CODEC_ID_PCM_ALAW)
522 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
524 int packet_size, ret, width, height;
525 AVStream *st = s->streams[0];
527 width = st->codec.width;
528 height = st->codec.height;
530 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
534 if (av_new_packet(pkt, packet_size) < 0)
537 pkt->stream_index = 0;
539 /* bypass buffered I/O */
540 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
542 ret = get_buffer(&s->pb, pkt->data, pkt->size);
544 if (ret != pkt->size) {
552 AVInputFormat rawvideo_iformat = {
558 rawvideo_read_packet,
561 .value = CODEC_ID_RAWVIDEO,
564 #ifdef CONFIG_ENCODERS
565 AVOutputFormat rawvideo_oformat = {
577 #endif //CONFIG_ENCODERS
579 #ifdef CONFIG_ENCODERS
580 static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
585 AVOutputFormat null_oformat = {
591 #ifdef WORDS_BIGENDIAN
600 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
602 #endif //CONFIG_ENCODERS
604 #ifndef CONFIG_ENCODERS
605 #define av_register_output_format(format)
607 #ifndef CONFIG_DECODERS
608 #define av_register_input_format(format)
613 av_register_input_format(&ac3_iformat);
614 av_register_output_format(&ac3_oformat);
616 av_register_input_format(&h261_iformat);
618 av_register_input_format(&h263_iformat);
619 av_register_output_format(&h263_oformat);
621 av_register_input_format(&m4v_iformat);
622 av_register_output_format(&m4v_oformat);
624 av_register_input_format(&h264_iformat);
625 av_register_output_format(&h264_oformat);
627 av_register_input_format(&mpegvideo_iformat);
628 av_register_output_format(&mpeg1video_oformat);
630 av_register_input_format(&mjpeg_iformat);
631 av_register_output_format(&mjpeg_oformat);
633 av_register_input_format(&pcm_s16le_iformat);
634 av_register_output_format(&pcm_s16le_oformat);
635 av_register_input_format(&pcm_s16be_iformat);
636 av_register_output_format(&pcm_s16be_oformat);
637 av_register_input_format(&pcm_u16le_iformat);
638 av_register_output_format(&pcm_u16le_oformat);
639 av_register_input_format(&pcm_u16be_iformat);
640 av_register_output_format(&pcm_u16be_oformat);
641 av_register_input_format(&pcm_s8_iformat);
642 av_register_output_format(&pcm_s8_oformat);
643 av_register_input_format(&pcm_u8_iformat);
644 av_register_output_format(&pcm_u8_oformat);
645 av_register_input_format(&pcm_mulaw_iformat);
646 av_register_output_format(&pcm_mulaw_oformat);
647 av_register_input_format(&pcm_alaw_iformat);
648 av_register_output_format(&pcm_alaw_oformat);
650 av_register_input_format(&rawvideo_iformat);
651 av_register_output_format(&rawvideo_oformat);
653 av_register_output_format(&null_oformat);