Libav
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 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;
31  char *video_size;
32  char *pixel_format;
33  char *framerate;
35 
36 
38 {
40  int width = 0, height = 0, ret = 0;
43  AVStream *st;
44 
45  st = avformat_new_stream(ctx, NULL);
46  if (!st)
47  return AVERROR(ENOMEM);
48 
50 
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 
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 
112  .class_name = "rawvideo demuxer",
113  .item_name = av_default_item_name,
114  .option = rawvideo_options,
115  .version = LIBAVUTIL_VERSION_INT,
116 };
117 
119  .name = "rawvideo",
120  .long_name = NULL_IF_CONFIG_SMALL("raw video"),
121  .priv_data_size = sizeof(RawVideoDemuxerContext),
125  .extensions = "yuv,cif,qcif,rgb",
126  .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
127  .priv_class = &rawvideo_demuxer_class,
128 };
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:127
AVOption.
Definition: opt.h:234
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:100
misc image utilities
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
static const AVOption rawvideo_options[]
Definition: rawvideodec.c:104
static const AVClass rawvideo_demuxer_class
Definition: rawvideodec.c:111
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawvideodec.c:81
Format I/O context.
Definition: avformat.h:940
#define OFFSET(x)
Definition: rawvideodec.c:102
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:39
int width
Video only.
Definition: avcodec.h:3525
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
static int flags
Definition: log.c:50
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:117
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:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
static int rawvideo_read_header(AVFormatContext *ctx)
Definition: rawvideodec.c:37
char * video_size
String describing video size, set by a private option.
Definition: rawvideodec.c:31
AVFormatContext * ctx
Definition: movenc.c:48
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
AVIOContext * pb
I/O context.
Definition: avformat.h:982
av_default_item_name
Definition: dnxhdenc.c:55
AVInputFormat ff_rawvideo_demuxer
Definition: rawvideodec.c:118
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:34
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:421
rational number numerator/denominator
Definition: rational.h:43
char * pixel_format
Set by a private option.
Definition: rawvideodec.c:32
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:575
int height
Definition: gxfenc.c:72
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:952
#define DEC
Definition: rawvideodec.c:103
void * priv_data
Format private data.
Definition: avformat.h:968
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1716
int stream_index
Definition: avcodec.h:1348
char * framerate
String describing framerate, set by a private option.
Definition: rawvideodec.c:33
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339