FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "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; /**< Class for private options. */
30  char *video_size; /**< String describing video size, set by a private option. */
31  char *pixel_format; /**< Set by a private option. */
32  char *framerate; /**< String describing framerate, set by a private option. */
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;
76  (AVRational){8,1}, st->time_base);
77 
78  return 0;
79 }
80 
81 
83 {
84  int packet_size, ret, width, height;
85  AVStream *st = s->streams[0];
86 
87  width = st->codec->width;
88  height = st->codec->height;
89 
90  packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
91  if (packet_size < 0)
92  return -1;
93 
94  ret = av_get_packet(s->pb, pkt, packet_size);
95  pkt->pts = pkt->dts = pkt->pos / packet_size;
96 
97  pkt->stream_index = 0;
98  if (ret < 0)
99  return ret;
100  return 0;
101 }
102 
103 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
104 #define DEC AV_OPT_FLAG_DECODING_PARAM
105 static const AVOption rawvideo_options[] = {
106  { "video_size", "set frame size", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
107  { "pixel_format", "set pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
108  { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
109  { NULL },
110 };
111 
113  .class_name = "rawvideo demuxer",
114  .item_name = av_default_item_name,
115  .option = rawvideo_options,
116  .version = LIBAVUTIL_VERSION_INT,
117 };
118 
120  .name = "rawvideo",
121  .long_name = NULL_IF_CONFIG_SMALL("raw video"),
122  .priv_data_size = sizeof(RawVideoDemuxerContext),
126  .extensions = "yuv,cif,qcif,rgb",
127  .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
128  .priv_class = &rawvideo_demuxer_class,
129 };