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/parseutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "internal.h"
26 #include "avformat.h"
27 
28 typedef struct RawVideoDemuxerContext {
29  const AVClass *class;
30  char *video_size;
31  char *pixel_format;
32  char *framerate;
34 
35 
37 {
39  int width = 0, height = 0, ret = 0;
41  AVRational framerate;
42  AVStream *st;
43 
44  st = avformat_new_stream(ctx, NULL);
45  if (!st)
46  return AVERROR(ENOMEM);
47 
49 
50  st->codec->codec_id = ctx->iformat->raw_codec_id;
51 
52  if (s->video_size &&
53  (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
54  av_log(ctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
55  return ret;
56  }
57 
58  if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
59  av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
60  s->pixel_format);
61  return AVERROR(EINVAL);
62  }
63 
64  if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
65  av_log(ctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
66  s->framerate);
67  return ret;
68  }
69 
70  avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
71 
72  st->codec->width = width;
73  st->codec->height = height;
74  st->codec->pix_fmt = pix_fmt;
75 
76  return 0;
77 }
78 
79 
81 {
82  int packet_size, ret, width, height;
83  AVStream *st = s->streams[0];
84 
85  width = st->codec->width;
86  height = st->codec->height;
87 
88  packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
89  if (packet_size < 0)
90  return -1;
91 
92  ret = av_get_packet(s->pb, pkt, packet_size);
93  pkt->pts = pkt->dts = pkt->pos / packet_size;
94 
95  pkt->stream_index = 0;
96  if (ret < 0)
97  return ret;
98  return 0;
99 }
100 
101 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
102 #define DEC AV_OPT_FLAG_DECODING_PARAM
103 static const AVOption rawvideo_options[] = {
104  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
105  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
106  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
107  { NULL },
108 };
109 
111  .class_name = "rawvideo demuxer",
112  .item_name = av_default_item_name,
113  .option = rawvideo_options,
114  .version = LIBAVUTIL_VERSION_INT,
115 };
116 
118  .name = "rawvideo",
119  .long_name = NULL_IF_CONFIG_SMALL("raw video"),
120  .priv_data_size = sizeof(RawVideoDemuxerContext),
124  .extensions = "yuv,cif,qcif,rgb",
125  .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
126  .priv_class = &rawvideo_demuxer_class,
127 };
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
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:95
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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:2829
int num
numerator
Definition: rational.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
static const AVOption rawvideo_options[]
Definition: rawvideodec.c:103
static const AVClass rawvideo_demuxer_class
Definition: rawvideodec.c:110
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawvideodec.c:80
Format I/O context.
Definition: avformat.h:922
#define OFFSET(x)
Definition: rawvideodec.c:101
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:38
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static int flags
Definition: log.c:44
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int width
picture width / height.
Definition: avcodec.h:1224
static int rawvideo_read_header(AVFormatContext *ctx)
Definition: rawvideodec.c:36
char * video_size
String describing video size, set by a private option.
Definition: rawvideodec.c:30
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
av_default_item_name
Definition: dnxhdenc.c:52
AVInputFormat ff_rawvideo_demuxer
Definition: rawvideodec.c:117
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:33
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:417
rational number numerator/denominator
Definition: rational.h:43
char * pixel_format
Set by a private option.
Definition: rawvideodec.c:31
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:571
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:934
#define DEC
Definition: rawvideodec.c:102
void * priv_data
Format private data.
Definition: avformat.h:950
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:85
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1552
int stream_index
Definition: avcodec.h:975
char * framerate
String describing framerate, set by a private option.
Definition: rawvideodec.c:32
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966