]> git.sesse.net Git - ffmpeg/blob - libavformat/rawvideodec.c
yop: initialize palette to 0
[ffmpeg] / libavformat / rawvideodec.c
1 /*
2  * RAW video demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/parseutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "internal.h"
26 #include "avformat.h"
27
28 typedef struct RawVideoDemuxerContext {
29     const AVClass *class;     /**< Class for private options. */
30     char *video_size;         /**< String describing video size, set by a private option. */
31     char *pixel_format;       /**< Set by a private option. */
32     char *framerate;          /**< String describing framerate, set by a private option. */
33 } RawVideoDemuxerContext;
34
35
36 static int rawvideo_read_header(AVFormatContext *ctx)
37 {
38     RawVideoDemuxerContext *s = ctx->priv_data;
39     int width = 0, height = 0, ret = 0;
40     enum AVPixelFormat pix_fmt;
41     AVRational framerate;
42     AVStream *st;
43
44     st = avformat_new_stream(ctx, NULL);
45     if (!st)
46         return AVERROR(ENOMEM);
47
48     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
49
50     st->codec->codec_id = ctx->iformat->raw_codec_id;
51
52     if (s->video_size &&
53         (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
54         av_log(ctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
55         return ret;
56     }
57
58     if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
59         av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
60                s->pixel_format);
61         return AVERROR(EINVAL);
62     }
63
64     if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
65         av_log(ctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
66                s->framerate);
67         return ret;
68     }
69
70     avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
71
72     st->codec->width  = width;
73     st->codec->height = height;
74     st->codec->pix_fmt = pix_fmt;
75
76     return 0;
77 }
78
79
80 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
81 {
82     int packet_size, ret, width, height;
83     AVStream *st = s->streams[0];
84
85     width = st->codec->width;
86     height = st->codec->height;
87
88     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
89     if (packet_size < 0)
90         return -1;
91
92     ret = av_get_packet(s->pb, pkt, packet_size);
93     pkt->pts = pkt->dts = pkt->pos / packet_size;
94
95     pkt->stream_index = 0;
96     if (ret < 0)
97         return ret;
98     return 0;
99 }
100
101 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
102 #define DEC AV_OPT_FLAG_DECODING_PARAM
103 static const AVOption rawvideo_options[] = {
104     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
105     { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
106     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
107     { NULL },
108 };
109
110 static const AVClass rawvideo_demuxer_class = {
111     .class_name = "rawvideo demuxer",
112     .item_name  = av_default_item_name,
113     .option     = rawvideo_options,
114     .version    = LIBAVUTIL_VERSION_INT,
115 };
116
117 AVInputFormat ff_rawvideo_demuxer = {
118     .name           = "rawvideo",
119     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
120     .priv_data_size = sizeof(RawVideoDemuxerContext),
121     .read_header    = rawvideo_read_header,
122     .read_packet    = rawvideo_read_packet,
123     .flags          = AVFMT_GENERIC_INDEX,
124     .extensions     = "yuv,cif,qcif,rgb",
125     .raw_codec_id   = AV_CODEC_ID_RAWVIDEO,
126     .priv_class     = &rawvideo_demuxer_class,
127 };