]> git.sesse.net Git - ffmpeg/blob - libavcodec/v4l2_fmt.c
Merge commit '5c8a5765dc5f4e29afb85b95be393c30f45412a8'
[ffmpeg] / libavcodec / v4l2_fmt.c
1 /*
2  * V4L2 format helper functions
3  *
4  * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
5  * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <linux/videodev2.h>
25 #include <search.h>
26 #include "v4l2_fmt.h"
27
28 #define V4L2_FMT(x) V4L2_PIX_FMT_##x
29 #define AV_CODEC(x) AV_CODEC_ID_##x
30 #define AV_FMT(x)   AV_PIX_FMT_##x
31
32 static const struct fmt_conversion {
33     enum AVPixelFormat avfmt;
34     enum AVCodecID avcodec;
35     uint32_t v4l2_fmt;
36 } fmt_map[] = {
37     { AV_FMT(RGB555LE),    AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB555) },
38     { AV_FMT(RGB555BE),    AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB555X) },
39     { AV_FMT(RGB565LE),    AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB565) },
40     { AV_FMT(RGB565BE),    AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB565X) },
41     { AV_FMT(BGR24),       AV_CODEC(RAWVIDEO),    V4L2_FMT(BGR24) },
42     { AV_FMT(RGB24),       AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB24) },
43     { AV_FMT(BGR0),        AV_CODEC(RAWVIDEO),    V4L2_FMT(BGR32) },
44     { AV_FMT(0RGB),        AV_CODEC(RAWVIDEO),    V4L2_FMT(RGB32) },
45     { AV_FMT(GRAY8),       AV_CODEC(RAWVIDEO),    V4L2_FMT(GREY) },
46     { AV_FMT(YUV420P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUV420) },
47     { AV_FMT(YUYV422),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUYV) },
48     { AV_FMT(UYVY422),     AV_CODEC(RAWVIDEO),    V4L2_FMT(UYVY) },
49     { AV_FMT(YUV422P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUV422P) },
50     { AV_FMT(YUV411P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUV411P) },
51     { AV_FMT(YUV410P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUV410) },
52     { AV_FMT(YUV410P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YVU410) },
53     { AV_FMT(NV12),        AV_CODEC(RAWVIDEO),    V4L2_FMT(NV12) },
54     { AV_FMT(NONE),        AV_CODEC(MJPEG),       V4L2_FMT(MJPEG) },
55     { AV_FMT(NONE),        AV_CODEC(MJPEG),       V4L2_FMT(JPEG) },
56 #ifdef V4L2_PIX_FMT_SRGGB8
57     { AV_FMT(BAYER_BGGR8), AV_CODEC(RAWVIDEO),    V4L2_FMT(SBGGR8) },
58     { AV_FMT(BAYER_GBRG8), AV_CODEC(RAWVIDEO),    V4L2_FMT(SGBRG8) },
59     { AV_FMT(BAYER_GRBG8), AV_CODEC(RAWVIDEO),    V4L2_FMT(SGRBG8) },
60     { AV_FMT(BAYER_RGGB8), AV_CODEC(RAWVIDEO),    V4L2_FMT(SRGGB8) },
61 #endif
62 #ifdef V4L2_PIX_FMT_Y16
63     { AV_FMT(GRAY16LE),    AV_CODEC(RAWVIDEO),    V4L2_FMT(Y16) },
64 #endif
65 #ifdef V4L2_PIX_FMT_NV12M
66     { AV_FMT(NV12),        AV_CODEC(RAWVIDEO),    V4L2_FMT(NV12M) },
67 #endif
68 #ifdef V4L2_PIX_FMT_NV21M
69     { AV_FMT(NV21),        AV_CODEC(RAWVIDEO),    V4L2_FMT(NV21M) },
70 #endif
71 #ifdef V4L2_PIX_FMT_YUV420M
72     { AV_FMT(YUV420P),     AV_CODEC(RAWVIDEO),    V4L2_FMT(YUV420M) },
73 #endif
74 #ifdef V4L2_PIX_FMT_NV16M
75     { AV_FMT(NV16),        AV_CODEC(RAWVIDEO),    V4L2_FMT(NV16M) },
76 #endif
77 #ifdef V4L2_PIX_FMT_H263
78     { AV_FMT(NONE),        AV_CODEC(H263),        V4L2_FMT(H263) },
79 #endif
80 #ifdef V4L2_PIX_FMT_H264
81     { AV_FMT(NONE),        AV_CODEC(H264),        V4L2_FMT(H264) },
82 #endif
83 #ifdef V4L2_PIX_FMT_MPEG4
84     { AV_FMT(NONE),        AV_CODEC(MPEG4),       V4L2_FMT(MPEG4) },
85 #endif
86 #ifdef V4L2_PIX_FMT_CPIA1
87     { AV_FMT(NONE),        AV_CODEC(CPIA),        V4L2_FMT(CPIA1) },
88 #endif
89 #ifdef V4L2_PIX_FMT_DV
90     { AV_FMT(NONE),        AV_CODEC(DVVIDEO),     V4L2_FMT(DV) },
91 #endif
92 #ifdef V4L2_PIX_FMT_MPEG1
93     { AV_FMT(NONE),        AV_CODEC(MPEG1VIDEO),  V4L2_FMT(MPEG1) },
94 #endif
95 #ifdef V4L2_PIX_FMT_MPEG2
96     { AV_FMT(NONE),        AV_CODEC(MPEG2VIDEO),  V4L2_FMT(MPEG2) },
97 #endif
98 #ifdef V4L2_PIX_FMT_VP8
99     { AV_FMT(NONE),        AV_CODEC(VP8),         V4L2_FMT(VP8) },
100 #endif
101 #ifdef V4L2_PIX_FMT_VP9
102     { AV_FMT(NONE),        AV_CODEC(VP9),         V4L2_FMT(VP9) },
103 #endif
104 #ifdef V4L2_PIX_FMT_HEVC
105     { AV_FMT(NONE),        AV_CODEC(HEVC),        V4L2_FMT(HEVC) },
106 #endif
107 #ifdef V4L2_PIX_FMT_VC1_ANNEX_G
108     { AV_FMT(NONE),        AV_CODEC(VC1),         V4L2_FMT(VC1_ANNEX_G) },
109 #endif
110 };
111
112 static int match_codec(const void *a, const void *b)
113 {
114     if (*(enum AVCodecID *)a == ((struct fmt_conversion *)b)->avcodec)
115         return 0;
116
117     return 1;
118 }
119
120 uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
121 {
122     size_t len = FF_ARRAY_ELEMS(fmt_map);
123     struct fmt_conversion *item;
124
125     item = lfind(&avcodec, fmt_map, &len, sizeof(fmt_map[0]), match_codec);
126     if (item)
127         return item->v4l2_fmt;
128
129     return 0;
130 }
131
132 static int match_fmt(const void *a, const void *b)
133 {
134     if ( *(enum AVPixelFormat *)a == ((struct fmt_conversion *)b)->avfmt)
135         return 0;
136
137     return 1;
138 }
139
140 uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
141 {
142     size_t len = FF_ARRAY_ELEMS(fmt_map);
143     struct fmt_conversion *item;
144
145     item = lfind(&avfmt, fmt_map, &len, sizeof(fmt_map[0]), match_fmt);
146     if (item)
147         return item->v4l2_fmt;
148
149     return 0;
150 }
151
152 struct v4l2fmt_avcodec_pair {
153     enum AVCodecID avcodec;
154     uint32_t v4l2_fmt;
155 };
156
157 static int match_codecfmt(const void *a, const void *b)
158 {
159     struct v4l2fmt_avcodec_pair *key = (struct v4l2fmt_avcodec_pair *) a;
160     struct fmt_conversion *item = (struct fmt_conversion *) b;
161
162     if (key->avcodec == item->avcodec && key->v4l2_fmt == item->v4l2_fmt)
163         return 0;
164
165     return 1;
166 }
167
168 enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
169 {
170     struct v4l2fmt_avcodec_pair const key = {
171         .v4l2_fmt = v4l2_fmt,
172         .avcodec = avcodec,
173     };
174     size_t len = FF_ARRAY_ELEMS(fmt_map);
175     struct fmt_conversion *item;
176
177     item = lfind(&key, fmt_map, &len, sizeof(fmt_map[0]), match_codecfmt);
178     if (item)
179         return item->avfmt;
180
181     return AV_PIX_FMT_NONE;
182 }