1 /*
2  * RAW video demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "config_components.h"
23 
24 #include "libavutil/imgutils.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/opt.h"
28 #include "internal.h"
29 #include "avformat.h"
30 
31 typedef struct RawVideoDemuxerContext {
32     const AVClass *class;     /**< Class for private options. */
33     int width, height;        /**< Integers describing video size, set by a private option. */
34     char *pixel_format;       /**< Set by a private option. */
35     AVRational framerate;     /**< AVRational describing framerate, set by a private option. */
36 } RawVideoDemuxerContext;
37 
38 // v210 frame width is padded to multiples of 48
39 #define GET_PACKET_SIZE(w, h) (((w + 47) / 48) * 48 * h * 8 / 3)
40 
rawvideo_read_header(AVFormatContext *ctx)41 static int rawvideo_read_header(AVFormatContext *ctx)
42 {
43     RawVideoDemuxerContext *s = ctx->priv_data;
44     enum AVPixelFormat pix_fmt;
45     AVStream *st;
46     int packet_size;
47     int ret;
48 
49     st = avformat_new_stream(ctx, NULL);
50     if (!st)
51         return AVERROR(ENOMEM);
52 
53     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
54 
55     st->codecpar->codec_id = ctx->iformat->raw_codec_id;
56 
57     if ((ctx->iformat->raw_codec_id != AV_CODEC_ID_V210) &&
58         (ctx->iformat->raw_codec_id != AV_CODEC_ID_V210X)) {
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 
66     avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
67 
68     ret = av_image_check_size(s->width, s->height, 0, ctx);
69     if (ret < 0)
70         return ret;
71 
72     st->codecpar->width  = s->width;
73     st->codecpar->height = s->height;
74 
75     if (ctx->iformat->raw_codec_id == AV_CODEC_ID_BITPACKED) {
76         unsigned int pgroup; /* size of the pixel group in bytes */
77         unsigned int xinc;
78         const AVPixFmtDescriptor *desc;
79         int tag;
80 
81         desc = av_pix_fmt_desc_get(pix_fmt);
82         st->codecpar->bits_per_coded_sample = av_get_bits_per_pixel(desc);
83         if (pix_fmt == AV_PIX_FMT_YUV422P10) {
84             tag = MKTAG('U', 'Y', 'V', 'Y');
85             pgroup = 5;
86             xinc   = 2;
87         } else if (pix_fmt == AV_PIX_FMT_UYVY422) {
88             tag = MKTAG('U', 'Y', 'V', 'Y');
89             pgroup = 4;
90             xinc   = 2;
91             st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
92         } else {
93             av_log(ctx, AV_LOG_ERROR, "unsupported format: %s for bitpacked.\n",
94                     s->pixel_format);
95             return AVERROR(EINVAL);
96         }
97         st->codecpar->codec_tag = tag;
98         packet_size  = s->width * s->height * pgroup / xinc;
99     } else if ((ctx->iformat->raw_codec_id == AV_CODEC_ID_V210) ||
100                (ctx->iformat->raw_codec_id == AV_CODEC_ID_V210X)) {
101         pix_fmt = ctx->iformat->raw_codec_id == AV_CODEC_ID_V210 ?
102                   AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV422P16;
103 
104         packet_size = GET_PACKET_SIZE(s->width, s->height);
105     } else {
106         packet_size = av_image_get_buffer_size(pix_fmt, s->width, s->height, 1);
107         if (packet_size < 0)
108             return packet_size;
109     }
110     if (packet_size == 0)
111         return AVERROR(EINVAL);
112 
113     st->codecpar->format = pix_fmt;
114     ctx->packet_size = packet_size;
115     st->codecpar->bit_rate = av_rescale_q(ctx->packet_size,
116                                        (AVRational){8,1}, st->time_base);
117 
118     return 0;
119 }
120 
121 
rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)122 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
123 {
124     int ret;
125 
126     ret = av_get_packet(s->pb, pkt, s->packet_size);
127     pkt->pts = pkt->dts = pkt->pos / s->packet_size;
128 
129     pkt->stream_index = 0;
130     if (ret < 0)
131         return ret;
132     return 0;
133 }
134 
135 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
136 #define DEC AV_OPT_FLAG_DECODING_PARAM
137 static const AVOption rawvideo_options[] = {
138     /* pixel_format is not used by the v210 demuxers. */
139     { "pixel_format", "set pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
140     { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
141     { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
142     { NULL },
143 };
144 
145 static const AVClass rawvideo_demuxer_class = {
146     .class_name = "rawvideo demuxer",
147     .item_name  = av_default_item_name,
148     .option     = rawvideo_options,
149     .version    = LIBAVUTIL_VERSION_INT,
150 };
151 
152 const AVInputFormat ff_rawvideo_demuxer = {
153     .name           = "rawvideo",
154     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
155     .priv_data_size = sizeof(RawVideoDemuxerContext),
156     .read_header    = rawvideo_read_header,
157     .read_packet    = rawvideo_read_packet,
158     .flags          = AVFMT_GENERIC_INDEX,
159     .extensions     = "yuv,cif,qcif,rgb",
160     .raw_codec_id   = AV_CODEC_ID_RAWVIDEO,
161     .priv_class     = &rawvideo_demuxer_class,
162 };
163 
164 static const AVClass bitpacked_demuxer_class = {
165     .class_name = "bitpacked demuxer",
166     .item_name  = av_default_item_name,
167     .option     = rawvideo_options,
168     .version    = LIBAVUTIL_VERSION_INT,
169 };
170 
171 #if CONFIG_BITPACKED_DEMUXER
172 const AVInputFormat ff_bitpacked_demuxer = {
173     .name           = "bitpacked",
174     .long_name      = NULL_IF_CONFIG_SMALL("Bitpacked"),
175     .priv_data_size = sizeof(RawVideoDemuxerContext),
176     .read_header    = rawvideo_read_header,
177     .read_packet    = rawvideo_read_packet,
178     .flags          = AVFMT_GENERIC_INDEX,
179     .extensions     = "bitpacked",
180     .raw_codec_id   = AV_CODEC_ID_BITPACKED,
181     .priv_class     = &bitpacked_demuxer_class,
182 };
183 #endif // CONFIG_BITPACKED_DEMUXER
184 
185 static const AVClass v210_demuxer_class = {
186     .class_name = "v210(x) demuxer",
187     .item_name  = av_default_item_name,
188     .option     = rawvideo_options + 1,
189     .version    = LIBAVUTIL_VERSION_INT,
190 };
191 
192 #if CONFIG_V210_DEMUXER
193 const AVInputFormat ff_v210_demuxer = {
194     .name           = "v210",
195     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
196     .priv_data_size = sizeof(RawVideoDemuxerContext),
197     .read_header    = rawvideo_read_header,
198     .read_packet    = rawvideo_read_packet,
199     .flags          = AVFMT_GENERIC_INDEX,
200     .extensions     = "v210",
201     .raw_codec_id   = AV_CODEC_ID_V210,
202     .priv_class     = &v210_demuxer_class,
203 };
204 #endif // CONFIG_V210_DEMUXER
205 
206 #if CONFIG_V210X_DEMUXER
207 const AVInputFormat ff_v210x_demuxer = {
208     .name           = "v210x",
209     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
210     .priv_data_size = sizeof(RawVideoDemuxerContext),
211     .read_header    = rawvideo_read_header,
212     .read_packet    = rawvideo_read_packet,
213     .flags          = AVFMT_GENERIC_INDEX,
214     .extensions     = "yuv10",
215     .raw_codec_id   = AV_CODEC_ID_V210X,
216     .priv_class     = &v210_demuxer_class,
217 };
218 #endif // CONFIG_V210X_DEMUXER
219