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 static int raw_write_header(struct AVFormatContext *s)
27 static int raw_write_packet(struct AVFormatContext *s, int stream_index,
28 const uint8_t *buf, int size, int64_t pts)
30 put_buffer(&s->pb, buf, size);
31 put_flush_packet(&s->pb);
35 static int raw_write_trailer(struct AVFormatContext *s)
41 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
46 st = av_new_stream(s, 0);
50 id = s->iformat->value;
51 if (id == CODEC_ID_RAWVIDEO) {
52 st->codec.codec_type = CODEC_TYPE_VIDEO;
54 st->codec.codec_type = CODEC_TYPE_AUDIO;
56 st->codec.codec_id = id;
58 switch(st->codec.codec_type) {
59 case CODEC_TYPE_AUDIO:
60 st->codec.sample_rate = ap->sample_rate;
61 st->codec.channels = ap->channels;
63 case CODEC_TYPE_VIDEO:
64 st->codec.frame_rate = ap->frame_rate;
65 st->codec.frame_rate_base = ap->frame_rate_base;
66 st->codec.width = ap->width;
67 st->codec.height = ap->height;
68 st->codec.pix_fmt = ap->pix_fmt;
79 #define RAW_PACKET_SIZE 1024
81 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
84 // AVStream *st = s->streams[0];
86 size= RAW_PACKET_SIZE;
88 if (av_new_packet(pkt, size) < 0)
91 pkt->stream_index = 0;
92 ret = get_buffer(&s->pb, pkt->data, size);
97 /* note: we need to modify the packet size here to handle the last
103 static int raw_read_close(AVFormatContext *s)
109 static int ac3_read_header(AVFormatContext *s,
110 AVFormatParameters *ap)
114 st = av_new_stream(s, 0);
116 return AVERROR_NOMEM;
118 st->codec.codec_type = CODEC_TYPE_AUDIO;
119 st->codec.codec_id = CODEC_ID_AC3;
120 /* the parameters will be extracted from the compressed bitstream */
124 /* mpeg1/h263 input */
125 static int video_read_header(AVFormatContext *s,
126 AVFormatParameters *ap)
130 st = av_new_stream(s, 0);
132 return AVERROR_NOMEM;
134 st->codec.codec_type = CODEC_TYPE_VIDEO;
135 st->codec.codec_id = s->iformat->value;
136 /* for mjpeg, specify frame rate */
137 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
138 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
140 st->codec.frame_rate = ap->frame_rate;
141 st->codec.frame_rate_base = ap->frame_rate_base;
143 st->codec.frame_rate = 25;
144 st->codec.frame_rate_base = 1;
150 #define SEQ_START_CODE 0x000001b3
151 #define GOP_START_CODE 0x000001b8
152 #define PICTURE_START_CODE 0x00000100
154 /* XXX: improve that by looking at several start codes */
155 static int mpegvideo_probe(AVProbeData *p)
160 /* we search the first start code. If it is a sequence, gop or
161 picture start code then we decide it is an mpeg video
162 stream. We do not send highest value to give a chance to mpegts */
163 /* NOTE: the search range was restricted to avoid too many false
169 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
170 if ((code & 0xffffff00) == 0x100) {
171 if (code == SEQ_START_CODE ||
172 code == GOP_START_CODE ||
173 code == PICTURE_START_CODE)
181 static int h263_probe(AVProbeData *p)
189 code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
196 AVInputFormat ac3_iformat = {
207 AVOutputFormat ac3_oformat = {
220 AVInputFormat h263_iformat = {
228 // .extensions = "h263", //FIXME remove after writing mpeg4_probe
229 .value = CODEC_ID_H263,
232 AVOutputFormat h263_oformat = {
245 AVInputFormat m4v_iformat = {
247 "raw MPEG4 video format",
249 NULL /*mpegvideo_probe*/,
253 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
254 .value = CODEC_ID_MPEG4,
257 AVOutputFormat m4v_oformat = {
259 "raw MPEG4 video format",
270 AVInputFormat h264_iformat = {
272 "raw H264 video format",
274 NULL /*mpegvideo_probe*/,
278 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
279 .value = CODEC_ID_H264,
282 AVOutputFormat h264_oformat = {
284 "raw H264 video format",
295 AVInputFormat mpegvideo_iformat = {
303 .value = CODEC_ID_MPEG1VIDEO,
306 AVOutputFormat mpeg1video_oformat = {
319 AVInputFormat mjpeg_iformat = {
327 .extensions = "mjpg,mjpeg",
328 .value = CODEC_ID_MJPEG,
331 AVOutputFormat mjpeg_oformat = {
346 #define PCMDEF(name, long_name, ext, codec) \
347 AVInputFormat pcm_ ## name ## _iformat = {\
359 AVOutputFormat pcm_ ## name ## _oformat = {\
372 #ifdef WORDS_BIGENDIAN
374 #define LE_DEF(s) NULL
376 #define BE_DEF(s) NULL
381 PCMDEF(s16le, "pcm signed 16 bit little endian format",
382 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
384 PCMDEF(s16be, "pcm signed 16 bit big endian format",
385 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
387 PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
388 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
390 PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
391 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
393 PCMDEF(s8, "pcm signed 8 bit format",
394 "sb", CODEC_ID_PCM_S8)
396 PCMDEF(u8, "pcm unsigned 8 bit format",
397 "ub", CODEC_ID_PCM_U8)
399 PCMDEF(mulaw, "pcm mu law format",
400 "ul", CODEC_ID_PCM_MULAW)
402 PCMDEF(alaw, "pcm A law format",
403 "al", CODEC_ID_PCM_ALAW)
405 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
407 int packet_size, ret, width, height;
408 AVStream *st = s->streams[0];
410 width = st->codec.width;
411 height = st->codec.height;
413 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
417 if (av_new_packet(pkt, packet_size) < 0)
420 pkt->stream_index = 0;
422 /* bypass buffered I/O */
423 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
425 ret = get_buffer(&s->pb, pkt->data, pkt->size);
427 if (ret != pkt->size) {
435 AVInputFormat rawvideo_iformat = {
441 rawvideo_read_packet,
444 .value = CODEC_ID_RAWVIDEO,
447 AVOutputFormat rawvideo_oformat = {
460 static int null_write_packet(struct AVFormatContext *s,
462 const uint8_t *buf, int size, int64_t pts)
467 AVOutputFormat null_oformat = {
473 #ifdef WORDS_BIGENDIAN
482 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
487 av_register_input_format(&ac3_iformat);
488 av_register_output_format(&ac3_oformat);
490 av_register_input_format(&h263_iformat);
491 av_register_output_format(&h263_oformat);
493 av_register_input_format(&m4v_iformat);
494 av_register_output_format(&m4v_oformat);
496 av_register_input_format(&h264_iformat);
497 av_register_output_format(&h264_oformat);
499 av_register_input_format(&mpegvideo_iformat);
500 av_register_output_format(&mpeg1video_oformat);
502 av_register_input_format(&mjpeg_iformat);
503 av_register_output_format(&mjpeg_oformat);
505 av_register_input_format(&pcm_s16le_iformat);
506 av_register_output_format(&pcm_s16le_oformat);
507 av_register_input_format(&pcm_s16be_iformat);
508 av_register_output_format(&pcm_s16be_oformat);
509 av_register_input_format(&pcm_u16le_iformat);
510 av_register_output_format(&pcm_u16le_oformat);
511 av_register_input_format(&pcm_u16be_iformat);
512 av_register_output_format(&pcm_u16be_oformat);
513 av_register_input_format(&pcm_s8_iformat);
514 av_register_output_format(&pcm_s8_oformat);
515 av_register_input_format(&pcm_u8_iformat);
516 av_register_output_format(&pcm_u8_oformat);
517 av_register_input_format(&pcm_mulaw_iformat);
518 av_register_output_format(&pcm_mulaw_oformat);
519 av_register_input_format(&pcm_alaw_iformat);
520 av_register_output_format(&pcm_alaw_oformat);
522 av_register_input_format(&rawvideo_iformat);
523 av_register_output_format(&rawvideo_oformat);
525 av_register_output_format(&null_oformat);