3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/parseutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
28 typedef struct RawVideoDemuxerContext
{
29 const AVClass
*class; /**< Class for private options. */
30 int width
, height
; /**< Integers describing video size, set by a private option. */
31 char *pixel_format
; /**< Set by a private option. */
32 AVRational framerate
; /**< AVRational describing framerate, set by a private option. */
33 } RawVideoDemuxerContext
;
36 static int rawvideo_read_header(AVFormatContext
*ctx
)
38 RawVideoDemuxerContext
*s
= ctx
->priv_data
;
39 enum AVPixelFormat pix_fmt
;
42 st
= avformat_new_stream(ctx
, NULL
);
44 return AVERROR(ENOMEM
);
46 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
48 st
->codec
->codec_id
= ctx
->iformat
->raw_codec_id
;
50 if ((pix_fmt
= av_get_pix_fmt(s
->pixel_format
)) == AV_PIX_FMT_NONE
) {
51 av_log(ctx
, AV_LOG_ERROR
, "No such pixel format: %s.\n",
53 return AVERROR(EINVAL
);
56 avpriv_set_pts_info(st
, 64, s
->framerate
.den
, s
->framerate
.num
);
58 st
->codec
->width
= s
->width
;
59 st
->codec
->height
= s
->height
;
60 st
->codec
->pix_fmt
= pix_fmt
;
61 st
->codec
->bit_rate
= av_rescale_q(avpicture_get_size(st
->codec
->pix_fmt
, s
->width
, s
->height
),
62 (AVRational
){8,1}, st
->time_base
);
68 static int rawvideo_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
70 int packet_size
, ret
, width
, height
;
71 AVStream
*st
= s
->streams
[0];
73 width
= st
->codec
->width
;
74 height
= st
->codec
->height
;
76 packet_size
= avpicture_get_size(st
->codec
->pix_fmt
, width
, height
);
80 ret
= av_get_packet(s
->pb
, pkt
, packet_size
);
81 pkt
->pts
= pkt
->dts
= pkt
->pos
/ packet_size
;
83 pkt
->stream_index
= 0;
89 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
90 #define DEC AV_OPT_FLAG_DECODING_PARAM
91 static const AVOption rawvideo_options
[] = {
92 { "video_size", "set frame size", OFFSET(width
), AV_OPT_TYPE_IMAGE_SIZE
, {.str
= NULL
}, 0, 0, DEC
},
93 { "pixel_format", "set pixel format", OFFSET(pixel_format
), AV_OPT_TYPE_STRING
, {.str
= "yuv420p"}, 0, 0, DEC
},
94 { "framerate", "set frame rate", OFFSET(framerate
), AV_OPT_TYPE_VIDEO_RATE
, {.str
= "25"}, 0, 0, DEC
},
98 static const AVClass rawvideo_demuxer_class
= {
99 .class_name
= "rawvideo demuxer",
100 .item_name
= av_default_item_name
,
101 .option
= rawvideo_options
,
102 .version
= LIBAVUTIL_VERSION_INT
,
105 AVInputFormat ff_rawvideo_demuxer
= {
107 .long_name
= NULL_IF_CONFIG_SMALL("raw video"),
108 .priv_data_size
= sizeof(RawVideoDemuxerContext
),
109 .read_header
= rawvideo_read_header
,
110 .read_packet
= rawvideo_read_packet
,
111 .flags
= AVFMT_GENERIC_INDEX
,
112 .extensions
= "yuv,cif,qcif,rgb",
113 .raw_codec_id
= AV_CODEC_ID_RAWVIDEO
,
114 .priv_class
= &rawvideo_demuxer_class
,