FFmpeg
Loading...
Searching...
No Matches
rawvideodec.c
Go to the documentation of this file.
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 <stdbool.h>
25
26#include "libavutil/imgutils.h"
28#include "libavutil/pixdesc.h"
29#include "libavutil/opt.h"
30#include "demux.h"
31#include "internal.h"
32#include "avformat.h"
33
34typedef struct RawVideoDemuxerContext {
35 const AVClass *class; /**< Class for private options. */
36 int width, height; /**< Integers describing video size, set by a private option. */
38 AVRational framerate; /**< AVRational describing framerate, set by a private option. */
39
41 /* We could derive linesize[1 to N] from linesize[0] for multiplane formats,
42 * but having users explicitly specify linesize for each plane can reduce
43 * unexpected results and support more use cases.
44 */
46 unsigned nb_linesize;
47 // with padding
48 size_t frame_size;
49 // linesize without padding
50 int raw_bytes[4];
52
53// v210 frame width is padded to multiples of 48
54#define GET_PACKET_SIZE(w, h) (((w + 47) / 48) * 48 * h * 8 / 3)
55
57{
58 RawVideoDemuxerContext *s = ctx->priv_data;
59 enum AVPixelFormat pix_fmt = s->pix_fmt;
60 AVStream *st;
61 int packet_size;
62 int ret;
63
65 if (!st)
66 return AVERROR(ENOMEM);
67
69
70 st->codecpar->codec_id = ffifmt(ctx->iformat)->raw_codec_id;
71 avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
72
73 ret = av_image_check_size(s->width, s->height, 0, ctx);
74 if (ret < 0)
75 return ret;
76
77 st->codecpar->width = s->width;
78 st->codecpar->height = s->height;
79
80 if (s->nb_linesize) {
82 if (s->nb_linesize != n) {
83 av_log(ctx, AV_LOG_ERROR, "Invalid number of stride %u, "
84 "pixel format has %d plane\n",
85 s->nb_linesize, n);
86 return AVERROR(EINVAL);
87 }
88
89 ret = av_image_fill_linesizes(s->raw_bytes, pix_fmt, s->width);
90 if (ret < 0)
91 return ret;
92
93 size_t linesize[4] = {0};
94 for (int i = 0; i < n; i++) {
95 if (s->linesize[i] < s->raw_bytes[i]) {
96 av_log(ctx, AV_LOG_ERROR, "Invalid stride %u of plane %d, "
97 "minimum required size is %d for width %d\n",
98 s->linesize[i], i, s->raw_bytes[i], s->width);
99 return AVERROR(EINVAL);
100 }
101 if (s->linesize[i] > s->raw_bytes[i])
102 s->has_padding = true;
103 linesize[i] = s->linesize[i];
104 }
105
106 size_t plane_size[4] = {0};
107 av_image_fill_plane_sizes(plane_size, pix_fmt, s->height, linesize);
108 s->frame_size = plane_size[0] + plane_size[1] + plane_size[2] + plane_size[3];
109 }
110
111 if (ffifmt(ctx->iformat)->raw_codec_id == AV_CODEC_ID_BITPACKED) {
112 unsigned int pgroup; /* size of the pixel group in bytes */
113 unsigned int xinc;
115 int tag;
116
120 tag = MKTAG('U', 'Y', 'V', 'Y');
121 pgroup = 5;
122 xinc = 2;
123 } else if (pix_fmt == AV_PIX_FMT_UYVY422) {
124 tag = MKTAG('U', 'Y', 'V', 'Y');
125 pgroup = 4;
126 xinc = 2;
128 } else {
129 av_log(ctx, AV_LOG_ERROR, "unsupported format: %s for bitpacked.\n",
130 desc->name);
131 return AVERROR(EINVAL);
132 }
133 st->codecpar->codec_tag = tag;
134 packet_size = s->width * s->height * pgroup / xinc;
135 } else if ((ffifmt(ctx->iformat)->raw_codec_id == AV_CODEC_ID_V210) ||
136 (ffifmt(ctx->iformat)->raw_codec_id == AV_CODEC_ID_V210X)) {
139
140 packet_size = GET_PACKET_SIZE(s->width, s->height);
141 } else {
142 packet_size = av_image_get_buffer_size(pix_fmt, s->width, s->height, 1);
143 if (packet_size < 0)
144 return packet_size;
145 }
146 if (packet_size == 0)
147 return AVERROR(EINVAL);
148
149 st->codecpar->format = pix_fmt;
150 ctx->packet_size = packet_size;
151 st->codecpar->bit_rate = av_rescale_q(ctx->packet_size,
152 (AVRational){8,1}, st->time_base);
153
154 return 0;
155}
156
157
159{
160 int ret;
161 RawVideoDemuxerContext *s = ctx->priv_data;
162
163 if (!s->has_padding) {
164 ret = av_get_packet(ctx->pb, pkt, ctx->packet_size);
165 if (ret < 0)
166 return ret;
167 pkt->pts = pkt->dts = pkt->pos / ctx->packet_size;
168
169 return 0;
170 }
171
172 ret = av_new_packet(pkt, ctx->packet_size);
173 if (ret < 0)
174 return ret;
175
176 pkt->pos = avio_tell(ctx->pb);
177 pkt->pts = pkt->dts = pkt->pos / s->frame_size;
178
179 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
180 uint8_t *p = pkt->data;
181 for (int i = 0; i < s->nb_linesize; i++) {
182 int shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
183 int h = AV_CEIL_RSHIFT(s->height, shift);
184 int skip_bytes = s->linesize[i] - s->raw_bytes[i];
185
186 for (int j = 0; j < h; j++) {
187 ret = avio_read(ctx->pb, p, s->raw_bytes[i]);
188 if (ret != s->raw_bytes[i]) {
189 if (ret < 0 && ret != AVERROR_EOF)
190 return ret;
191
192 if (ret == AVERROR_EOF && p == pkt->data)
193 return AVERROR_EOF;
194
195 memset(p, 0, pkt->size - (p - pkt->data));
196 pkt->flags |= AV_PKT_FLAG_CORRUPT;
197
198 return 0;
199 }
200
201 p += s->raw_bytes[i];
203 }
204 }
205
206 return 0;
207}
208
209#define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
210#define DEC AV_OPT_FLAG_DECODING_PARAM
211static const AVOption rawvideo_options[] = {
212 // Only supported by rawvideo demuxer
213 {"stride", "frame line size in bytes", OFFSET(linesize), AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, {.arr = NULL}, 0, INT_MAX, DEC },
214#define BITPACKED_OPTION_OFFSET 1 // skip stride option
215 /* pixel_format is not used by the v210 demuxers. */
216 { "pixel_format", "set pixel format", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_YUV420P}, AV_PIX_FMT_YUV420P, INT_MAX, DEC },
217#define V210_OPTION_OFFSET 2 // skip stride and pixel_format option
218 { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
219 { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
220 { NULL },
221};
222
224 .class_name = "rawvideo demuxer",
225 .item_name = av_default_item_name,
226 .option = rawvideo_options,
227 .version = LIBAVUTIL_VERSION_INT,
228};
229
231 .p.name = "rawvideo",
232 .p.long_name = NULL_IF_CONFIG_SMALL("raw video"),
233 .p.flags = AVFMT_GENERIC_INDEX,
234 .p.extensions = "yuv,cif,qcif,rgb",
235 .p.priv_class = &rawvideo_demuxer_class,
236 .priv_data_size = sizeof(RawVideoDemuxerContext),
239 .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
240};
241
243 .class_name = "bitpacked demuxer",
244 .item_name = av_default_item_name,
246 .version = LIBAVUTIL_VERSION_INT,
247};
248
249#if CONFIG_BITPACKED_DEMUXER
251 .p.name = "bitpacked",
252 .p.long_name = NULL_IF_CONFIG_SMALL("Bitpacked"),
253 .p.flags = AVFMT_GENERIC_INDEX,
254 .p.extensions = "bitpacked",
255 .p.priv_class = &bitpacked_demuxer_class,
256 .priv_data_size = sizeof(RawVideoDemuxerContext),
259 .raw_codec_id = AV_CODEC_ID_BITPACKED,
260};
261#endif // CONFIG_BITPACKED_DEMUXER
262
264 .class_name = "v210(x) demuxer",
265 .item_name = av_default_item_name,
267 .version = LIBAVUTIL_VERSION_INT,
268};
269
270#if CONFIG_V210_DEMUXER
272 .p.name = "v210",
273 .p.long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
274 .p.flags = AVFMT_GENERIC_INDEX,
275 .p.extensions = "v210",
276 .p.priv_class = &v210_demuxer_class,
277 .priv_data_size = sizeof(RawVideoDemuxerContext),
280 .raw_codec_id = AV_CODEC_ID_V210,
281};
282#endif // CONFIG_V210_DEMUXER
283
284#if CONFIG_V210X_DEMUXER
286 .p.name = "v210x",
287 .p.long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
288 .p.flags = AVFMT_GENERIC_INDEX,
289 .p.extensions = "yuv10",
290 .p.priv_class = &v210_demuxer_class,
291 .priv_data_size = sizeof(RawVideoDemuxerContext),
294 .raw_codec_id = AV_CODEC_ID_V210X,
295};
296#endif // CONFIG_V210X_DEMUXER
const FFInputFormat ff_rawvideo_demuxer
const FFInputFormat ff_v210x_demuxer
const FFInputFormat ff_v210_demuxer
const FFInputFormat ff_bitpacked_demuxer
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
Definition demux.h:186
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition opt.h:306
@ AV_OPT_TYPE_FLAG_ARRAY
May be combined with another regular option type to declare an array option.
Definition opt.h:345
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_V210
Definition codec_id.h:177
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_BITPACKED
Definition codec_id.h:276
@ AV_CODEC_ID_V210X
Definition codec_id.h:175
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition imgutils.c:466
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition imgutils.c:111
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
misc image utilities
static int shift(int a, int b)
Definition bonk.c:261
static const AVOption rawvideo_options[]
Definition rawdec.c:121
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define DEC
Definition librsvgdec.c:149
const char * desc
Definition libsvtav1.c:83
#define MKTAG(a, b, c, d)
Definition macros.h:55
uint32_t tag
Definition movenc.c:2073
AVOptions.
misc parsing utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition pixdesc.c:3412
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
static int rawvideo_read_header(AVFormatContext *ctx)
Definition rawvideodec.c:56
#define BITPACKED_OPTION_OFFSET
static int rawvideo_read_packet(AVFormatContext *ctx, AVPacket *pkt)
static const AVClass rawvideo_demuxer_class
#define V210_OPTION_OFFSET
static const AVClass v210_demuxer_class
#define OFFSET(x)
#define GET_PACKET_SIZE(w, h)
Definition rawvideodec.c:54
static const AVClass bitpacked_demuxer_class
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
enum AVCodecID raw_codec_id
Raw demuxers store their codec ID here.
Definition demux.h:75
AVRational framerate
AVRational describing framerate, set by a private option.
Definition rawvideodec.c:38
enum AVPixelFormat pix_fmt
Definition rawvideodec.c:37
int height
Integers describing video size, set by a private option.
Definition rawvideodec.c:36
#define av_log(a,...)
float framerate
Definition av1_levels.c:29
static AVFormatContext * ctx
Definition movenc.c:49
#define width
Definition dsp.h:89