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