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_close(AVFormatContext *s)
110 int pcm_read_seek(AVFormatContext *s,
111 int stream_index, int64_t timestamp)
114 int block_align, byte_rate;
118 switch(st->codec.codec_id) {
119 case CODEC_ID_PCM_S16LE:
120 case CODEC_ID_PCM_S16BE:
121 case CODEC_ID_PCM_U16LE:
122 case CODEC_ID_PCM_U16BE:
123 block_align = 2 * st->codec.channels;
124 byte_rate = block_align * st->codec.sample_rate;
126 case CODEC_ID_PCM_S8:
127 case CODEC_ID_PCM_U8:
128 case CODEC_ID_PCM_MULAW:
129 case CODEC_ID_PCM_ALAW:
130 block_align = st->codec.channels;
131 byte_rate = block_align * st->codec.sample_rate;
134 block_align = st->codec.block_align;
135 byte_rate = st->codec.bit_rate / 8;
139 if (block_align <= 0 || byte_rate <= 0)
142 /* compute the position by aligning it to block_align */
143 pos = (timestamp * byte_rate) / AV_TIME_BASE;
144 pos = (pos / block_align) * block_align;
146 /* recompute exact position */
147 st->cur_dts = (pos * AV_TIME_BASE) / byte_rate;
148 url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
153 static int ac3_read_header(AVFormatContext *s,
154 AVFormatParameters *ap)
158 st = av_new_stream(s, 0);
160 return AVERROR_NOMEM;
162 st->codec.codec_type = CODEC_TYPE_AUDIO;
163 st->codec.codec_id = CODEC_ID_AC3;
164 st->need_parsing = 1;
165 /* the parameters will be extracted from the compressed bitstream */
169 /* mpeg1/h263 input */
170 static int video_read_header(AVFormatContext *s,
171 AVFormatParameters *ap)
175 st = av_new_stream(s, 0);
177 return AVERROR_NOMEM;
179 st->codec.codec_type = CODEC_TYPE_VIDEO;
180 st->codec.codec_id = s->iformat->value;
181 st->need_parsing = 1;
183 /* for mjpeg, specify frame rate */
184 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
185 if (st->codec.codec_id == CODEC_ID_MJPEG ||
186 st->codec.codec_id == CODEC_ID_MPEG4) {
187 if (ap && ap->frame_rate) {
188 st->codec.frame_rate = ap->frame_rate;
189 st->codec.frame_rate_base = ap->frame_rate_base;
191 st->codec.frame_rate = 25;
192 st->codec.frame_rate_base = 1;
198 #define SEQ_START_CODE 0x000001b3
199 #define GOP_START_CODE 0x000001b8
200 #define PICTURE_START_CODE 0x00000100
202 /* XXX: improve that by looking at several start codes */
203 static int mpegvideo_probe(AVProbeData *p)
208 /* we search the first start code. If it is a sequence, gop or
209 picture start code then we decide it is an mpeg video
210 stream. We do not send highest value to give a chance to mpegts */
211 /* NOTE: the search range was restricted to avoid too many false
217 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
218 if ((code & 0xffffff00) == 0x100) {
219 if (code == SEQ_START_CODE ||
220 code == GOP_START_CODE ||
221 code == PICTURE_START_CODE)
229 static int h263_probe(AVProbeData *p)
237 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
244 AVInputFormat ac3_iformat = {
255 #ifdef CONFIG_ENCODERS
256 AVOutputFormat ac3_oformat = {
268 #endif //CONFIG_ENCODERS
270 AVInputFormat h263_iformat = {
278 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
279 .value = CODEC_ID_H263,
282 #ifdef CONFIG_ENCODERS
283 AVOutputFormat h263_oformat = {
295 #endif //CONFIG_ENCODERS
297 AVInputFormat m4v_iformat = {
299 "raw MPEG4 video format",
301 NULL /*mpegvideo_probe*/,
305 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
306 .value = CODEC_ID_MPEG4,
309 #ifdef CONFIG_ENCODERS
310 AVOutputFormat m4v_oformat = {
312 "raw MPEG4 video format",
322 #endif //CONFIG_ENCODERS
324 AVInputFormat h264_iformat = {
326 "raw H264 video format",
328 NULL /*mpegvideo_probe*/,
332 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
333 .value = CODEC_ID_H264,
336 #ifdef CONFIG_ENCODERS
337 AVOutputFormat h264_oformat = {
339 "raw H264 video format",
349 #endif //CONFIG_ENCODERS
351 AVInputFormat mpegvideo_iformat = {
359 .value = CODEC_ID_MPEG1VIDEO,
362 #ifdef CONFIG_ENCODERS
363 AVOutputFormat mpeg1video_oformat = {
375 #endif //CONFIG_ENCODERS
377 AVInputFormat mjpeg_iformat = {
385 .extensions = "mjpg,mjpeg",
386 .value = CODEC_ID_MJPEG,
389 #ifdef CONFIG_ENCODERS
390 AVOutputFormat mjpeg_oformat = {
402 #endif //CONFIG_ENCODERS
406 #define PCMINPUTDEF(name, long_name, ext, codec) \
407 AVInputFormat pcm_ ## name ## _iformat = {\
420 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
422 #define PCMDEF(name, long_name, ext, codec) \
423 PCMINPUTDEF(name, long_name, ext, codec)
427 #define PCMDEF(name, long_name, ext, codec) \
428 PCMINPUTDEF(name, long_name, ext, codec)\
430 AVOutputFormat pcm_ ## name ## _oformat = {\
442 #endif //CONFIG_ENCODERS
444 #ifdef WORDS_BIGENDIAN
446 #define LE_DEF(s) NULL
448 #define BE_DEF(s) NULL
453 PCMDEF(s16le, "pcm signed 16 bit little endian format",
454 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
456 PCMDEF(s16be, "pcm signed 16 bit big endian format",
457 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
459 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
460 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
462 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
463 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
465 PCMDEF(s8, "pcm signed 8 bit format",
466 "sb", CODEC_ID_PCM_S8)
468 PCMDEF(u8, "pcm unsigned 8 bit format",
469 "ub", CODEC_ID_PCM_U8)
471 PCMDEF(mulaw, "pcm mu law format",
472 "ul", CODEC_ID_PCM_MULAW)
474 PCMDEF(alaw, "pcm A law format",
475 "al", CODEC_ID_PCM_ALAW)
477 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
479 int packet_size, ret, width, height;
480 AVStream *st = s->streams[0];
482 width = st->codec.width;
483 height = st->codec.height;
485 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
489 if (av_new_packet(pkt, packet_size) < 0)
492 pkt->stream_index = 0;
494 /* bypass buffered I/O */
495 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
497 ret = get_buffer(&s->pb, pkt->data, pkt->size);
499 if (ret != pkt->size) {
507 AVInputFormat rawvideo_iformat = {
513 rawvideo_read_packet,
516 .value = CODEC_ID_RAWVIDEO,
519 #ifdef CONFIG_ENCODERS
520 AVOutputFormat rawvideo_oformat = {
532 #endif //CONFIG_ENCODERS
534 #ifdef CONFIG_ENCODERS
535 static int null_write_packet(struct AVFormatContext *s,
537 const uint8_t *buf, int size, int64_t pts)
542 AVOutputFormat null_oformat = {
548 #ifdef WORDS_BIGENDIAN
557 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
559 #endif //CONFIG_ENCODERS
561 #ifndef CONFIG_ENCODERS
562 #define av_register_output_format(format)
564 #ifndef CONFIG_DECODERS
565 #define av_register_input_format(format)
570 av_register_input_format(&ac3_iformat);
571 av_register_output_format(&ac3_oformat);
573 av_register_input_format(&h263_iformat);
574 av_register_output_format(&h263_oformat);
576 av_register_input_format(&m4v_iformat);
577 av_register_output_format(&m4v_oformat);
579 av_register_input_format(&h264_iformat);
580 av_register_output_format(&h264_oformat);
582 av_register_input_format(&mpegvideo_iformat);
583 av_register_output_format(&mpeg1video_oformat);
585 av_register_input_format(&mjpeg_iformat);
586 av_register_output_format(&mjpeg_oformat);
588 av_register_input_format(&pcm_s16le_iformat);
589 av_register_output_format(&pcm_s16le_oformat);
590 av_register_input_format(&pcm_s16be_iformat);
591 av_register_output_format(&pcm_s16be_oformat);
592 av_register_input_format(&pcm_u16le_iformat);
593 av_register_output_format(&pcm_u16le_oformat);
594 av_register_input_format(&pcm_u16be_iformat);
595 av_register_output_format(&pcm_u16be_oformat);
596 av_register_input_format(&pcm_s8_iformat);
597 av_register_output_format(&pcm_s8_oformat);
598 av_register_input_format(&pcm_u8_iformat);
599 av_register_output_format(&pcm_u8_oformat);
600 av_register_input_format(&pcm_mulaw_iformat);
601 av_register_output_format(&pcm_mulaw_oformat);
602 av_register_input_format(&pcm_alaw_iformat);
603 av_register_output_format(&pcm_alaw_oformat);
605 av_register_input_format(&rawvideo_iformat);
606 av_register_output_format(&rawvideo_oformat);
608 av_register_output_format(&null_oformat);