FFmpeg
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 "libavutil/imgutils.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/opt.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "avformat.h"
31 
32 typedef struct RawVideoDemuxerContext {
33  const AVClass *class; /**< Class for private options. */
34  int width, height; /**< Integers describing video size, set by a private option. */
35  char *pixel_format; /**< Set by a private option. */
36  AVRational framerate; /**< AVRational describing framerate, set by a private option. */
38 
39 // v210 frame width is padded to multiples of 48
40 #define GET_PACKET_SIZE(w, h) (((w + 47) / 48) * 48 * h * 8 / 3)
41 
43 {
46  AVStream *st;
47  int packet_size;
48  int ret;
49 
51  if (!st)
52  return AVERROR(ENOMEM);
53 
55 
57 
60  if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
61  av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
62  s->pixel_format);
63  return AVERROR(EINVAL);
64  }
65  }
66 
67  avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
68 
69  ret = av_image_check_size(s->width, s->height, 0, ctx);
70  if (ret < 0)
71  return ret;
72 
73  st->codecpar->width = s->width;
74  st->codecpar->height = s->height;
75 
77  unsigned int pgroup; /* size of the pixel group in bytes */
78  unsigned int xinc;
79  const AVPixFmtDescriptor *desc;
80  int tag;
81 
85  tag = MKTAG('U', 'Y', 'V', 'Y');
86  pgroup = 5;
87  xinc = 2;
88  } else if (pix_fmt == AV_PIX_FMT_UYVY422) {
89  tag = MKTAG('U', 'Y', 'V', 'Y');
90  pgroup = 4;
91  xinc = 2;
93  } else {
94  av_log(ctx, AV_LOG_ERROR, "unsupported format: %s for bitpacked.\n",
95  s->pixel_format);
96  return AVERROR(EINVAL);
97  }
98  st->codecpar->codec_tag = tag;
99  packet_size = s->width * s->height * pgroup / xinc;
100  } else if ((ffifmt(ctx->iformat)->raw_codec_id == AV_CODEC_ID_V210) ||
104 
105  packet_size = GET_PACKET_SIZE(s->width, s->height);
106  } else {
107  packet_size = av_image_get_buffer_size(pix_fmt, s->width, s->height, 1);
108  if (packet_size < 0)
109  return packet_size;
110  }
111  if (packet_size == 0)
112  return AVERROR(EINVAL);
113 
114  st->codecpar->format = pix_fmt;
115  ctx->packet_size = packet_size;
117  (AVRational){8,1}, st->time_base);
118 
119  return 0;
120 }
121 
122 
124 {
125  int ret;
126 
127  ret = av_get_packet(s->pb, pkt, s->packet_size);
128  pkt->pts = pkt->dts = pkt->pos / s->packet_size;
129 
130  pkt->stream_index = 0;
131  if (ret < 0)
132  return ret;
133  return 0;
134 }
135 
136 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
137 #define DEC AV_OPT_FLAG_DECODING_PARAM
138 static const AVOption rawvideo_options[] = {
139  /* pixel_format is not used by the v210 demuxers. */
140  { "pixel_format", "set pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
141  { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
142  { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
143  { NULL },
144 };
145 
147  .class_name = "rawvideo demuxer",
148  .item_name = av_default_item_name,
149  .option = rawvideo_options,
150  .version = LIBAVUTIL_VERSION_INT,
151 };
152 
154  .p.name = "rawvideo",
155  .p.long_name = NULL_IF_CONFIG_SMALL("raw video"),
156  .p.flags = AVFMT_GENERIC_INDEX,
157  .p.extensions = "yuv,cif,qcif,rgb",
158  .p.priv_class = &rawvideo_demuxer_class,
159  .priv_data_size = sizeof(RawVideoDemuxerContext),
162  .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
163 };
164 
166  .class_name = "bitpacked demuxer",
167  .item_name = av_default_item_name,
168  .option = rawvideo_options,
169  .version = LIBAVUTIL_VERSION_INT,
170 };
171 
172 #if CONFIG_BITPACKED_DEMUXER
174  .p.name = "bitpacked",
175  .p.long_name = NULL_IF_CONFIG_SMALL("Bitpacked"),
176  .p.flags = AVFMT_GENERIC_INDEX,
177  .p.extensions = "bitpacked",
178  .p.priv_class = &bitpacked_demuxer_class,
179  .priv_data_size = sizeof(RawVideoDemuxerContext),
182  .raw_codec_id = AV_CODEC_ID_BITPACKED,
183 };
184 #endif // CONFIG_BITPACKED_DEMUXER
185 
186 static const AVClass v210_demuxer_class = {
187  .class_name = "v210(x) demuxer",
188  .item_name = av_default_item_name,
189  .option = rawvideo_options + 1,
190  .version = LIBAVUTIL_VERSION_INT,
191 };
192 
193 #if CONFIG_V210_DEMUXER
195  .p.name = "v210",
196  .p.long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
197  .p.flags = AVFMT_GENERIC_INDEX,
198  .p.extensions = "v210",
199  .p.priv_class = &v210_demuxer_class,
200  .priv_data_size = sizeof(RawVideoDemuxerContext),
203  .raw_codec_id = AV_CODEC_ID_V210,
204 };
205 #endif // CONFIG_V210_DEMUXER
206 
207 #if CONFIG_V210X_DEMUXER
209  .p.name = "v210x",
210  .p.long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
211  .p.flags = AVFMT_GENERIC_INDEX,
212  .p.extensions = "yuv10",
213  .p.priv_class = &v210_demuxer_class,
214  .priv_data_size = sizeof(RawVideoDemuxerContext),
217  .raw_codec_id = AV_CODEC_ID_V210X,
218 };
219 #endif // CONFIG_V210X_DEMUXER
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:248
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
pixdesc.h
AVOption
AVOption.
Definition: opt.h:346
av_get_bits_per_pixel
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:2914
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
RawVideoDemuxerContext::framerate
AVRational framerate
AVRational describing framerate, set by a private option.
Definition: rawvideodec.c:36
rawvideo_options
static const AVOption rawvideo_options[]
Definition: rawvideodec.c:138
RawVideoDemuxerContext
Definition: rawvideodec.c:32
avpriv_set_pts_info
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:853
ff_rawvideo_demuxer
const FFInputFormat ff_rawvideo_demuxer
Definition: rawvideodec.c:153
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
rawvideo_read_header
static int rawvideo_read_header(AVFormatContext *ctx)
Definition: rawvideodec.c:42
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:490
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVFormatContext::iformat
const struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1267
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
OFFSET
#define OFFSET(x)
Definition: rawvideodec.c:136
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
framerate
float framerate
Definition: av1_levels.c:29
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
ff_bitpacked_demuxer
const FFInputFormat ff_bitpacked_demuxer
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:782
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:245
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
parseutils.h
ff_v210_demuxer
const FFInputFormat ff_v210_demuxer
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:479
GET_PACKET_SIZE
#define GET_PACKET_SIZE(w, h)
Definition: rawvideodec.c:40
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
RawVideoDemuxerContext::pixel_format
char * pixel_format
Set by a private option.
Definition: rawvideodec.c:35
RawVideoDemuxerContext::height
int height
Integers describing video size, set by a private option.
Definition: rawvideodec.c:34
rawvideo_read_packet
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawvideodec.c:123
AV_CODEC_ID_V210
@ AV_CODEC_ID_V210
Definition: codec_id.h:179
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:521
av_image_get_buffer_size
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
ff_v210x_demuxer
const FFInputFormat ff_v210x_demuxer
AV_CODEC_ID_V210X
@ AV_CODEC_ID_V210X
Definition: codec_id.h:177
DEC
#define DEC
Definition: rawvideodec.c:137
AV_CODEC_ID_BITPACKED
@ AV_CODEC_ID_BITPACKED
Definition: codec_id.h:281
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
AVCodecParameters::height
int height
Definition: codec_par.h:135
FFInputFormat::raw_codec_id
enum AVCodecID raw_codec_id
Raw demuxers store their codec ID here.
Definition: demux.h:46
demux.h
av_get_packet
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:103
tag
uint32_t tag
Definition: movenc.c:1786
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
v210_demuxer_class
static const AVClass v210_demuxer_class
Definition: rawvideodec.c:186
avformat.h
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:88
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2894
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
ffifmt
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
Definition: demux.h:133
AVPacket::stream_index
int stream_index
Definition: packet.h:524
AVFormatContext::packet_size
unsigned int packet_size
Definition: avformat.h:1399
desc
const char * desc
Definition: libsvtav1.c:75
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
RawVideoDemuxerContext::width
int width
Definition: rawvideodec.c:34
FFInputFormat
Definition: demux.h:37
imgutils.h
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
bitpacked_demuxer_class
static const AVClass bitpacked_demuxer_class
Definition: rawvideodec.c:165
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
rawvideo_demuxer_class
static const AVClass rawvideo_demuxer_class
Definition: rawvideodec.c:146
av_image_check_size
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
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:239
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283