]> git.sesse.net Git - ffmpeg/blob - libavformat/v210.c
Merge commit 'bef3b1f59f036aba4a5fe599b2480f6bd9e6b280'
[ffmpeg] / libavformat / v210.c
1 /*
2  * Raw v210 video demuxer
3  * Copyright (c) 2015 Tiancheng "Timothy" Gu
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/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 V210DemuxerContext {
30     const AVClass *class;     /**< Class for private options. */
31     int width, height;        /**< Integers describing video size, set by a private option. */
32     AVRational framerate;     /**< AVRational describing framerate, set by a private option. */
33 } V210DemuxerContext;
34
35 // v210 frame width is padded to multiples of 48
36 #define GET_PACKET_SIZE(w, h) (((w + 47) / 48) * 48 * h * 8 / 3)
37
38 static int v210_read_header(AVFormatContext *ctx)
39 {
40     V210DemuxerContext *s = ctx->priv_data;
41     AVStream *st;
42
43     st = avformat_new_stream(ctx, NULL);
44     if (!st)
45         return AVERROR(ENOMEM);
46
47     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
48
49     st->codec->codec_id = ctx->iformat->raw_codec_id;
50
51     avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
52
53     st->codec->width    = s->width;
54     st->codec->height   = s->height;
55     st->codec->pix_fmt  = ctx->iformat->raw_codec_id == AV_CODEC_ID_V210 ?
56                           AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV422P16;
57     st->codec->bit_rate = av_rescale_q(GET_PACKET_SIZE(s->width, s->height),
58                                        (AVRational){8,1}, st->time_base);
59
60     return 0;
61 }
62
63
64 static int v210_read_packet(AVFormatContext *s, AVPacket *pkt)
65 {
66     int packet_size, ret, width, height;
67     AVStream *st = s->streams[0];
68
69     width = st->codec->width;
70     height = st->codec->height;
71
72     packet_size = GET_PACKET_SIZE(width, height);
73     if (packet_size < 0)
74         return -1;
75
76     ret = av_get_packet(s->pb, pkt, packet_size);
77     pkt->pts = pkt->dts = pkt->pos / packet_size;
78
79     pkt->stream_index = 0;
80     if (ret < 0)
81         return ret;
82     return 0;
83 }
84
85 #define OFFSET(x) offsetof(V210DemuxerContext, x)
86 #define DEC AV_OPT_FLAG_DECODING_PARAM
87 static const AVOption v210_options[] = {
88     { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
89     { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
90     { NULL },
91 };
92
93 #if CONFIG_V210_DEMUXER
94 static const AVClass v210_demuxer_class = {
95     .class_name = "v210 demuxer",
96     .item_name  = av_default_item_name,
97     .option     = v210_options,
98     .version    = LIBAVUTIL_VERSION_INT,
99 };
100
101 AVInputFormat ff_v210_demuxer = {
102     .name           = "v210",
103     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
104     .priv_data_size = sizeof(V210DemuxerContext),
105     .read_header    = v210_read_header,
106     .read_packet    = v210_read_packet,
107     .flags          = AVFMT_GENERIC_INDEX,
108     .extensions     = "v210",
109     .raw_codec_id   = AV_CODEC_ID_V210,
110     .priv_class     = &v210_demuxer_class,
111 };
112 #endif // CONFIG_V210_DEMUXER
113
114 #if CONFIG_V210X_DEMUXER
115 static const AVClass v210x_demuxer_class = {
116     .class_name = "v210x demuxer",
117     .item_name  = av_default_item_name,
118     .option     = v210_options,
119     .version    = LIBAVUTIL_VERSION_INT,
120 };
121
122 AVInputFormat ff_v210x_demuxer = {
123     .name           = "v210x",
124     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
125     .priv_data_size = sizeof(V210DemuxerContext),
126     .read_header    = v210_read_header,
127     .read_packet    = v210_read_packet,
128     .flags          = AVFMT_GENERIC_INDEX,
129     .extensions     = "yuv10",
130     .raw_codec_id   = AV_CODEC_ID_V210X,
131     .priv_class     = &v210x_demuxer_class,
132 };
133 #endif // CONFIG_V210X_DEMUXER