• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)112 uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
113 {
114     int i;
115     for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
116         if (fmt_map[i].avcodec == avcodec)
117             return fmt_map[i].v4l2_fmt;
118     }
119     return 0;
120 }
121 
ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)122 uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
123 {
124     int i;
125     for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
126         if (fmt_map[i].avfmt == avfmt)
127             return fmt_map[i].v4l2_fmt;
128     }
129     return 0;
130 }
131 
ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt,enum AVCodecID avcodec)132 enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
133 {
134     int i;
135     for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
136         if (fmt_map[i].avcodec  == avcodec &&
137             fmt_map[i].v4l2_fmt == v4l2_fmt)
138             return fmt_map[i].avfmt;
139     }
140     return AV_PIX_FMT_NONE;
141 }
142