2 * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "gstv4l2format.h"
22 #define GST_CAT_DEFAULT gstv4l2codecs_debug
23 GST_DEBUG_CATEGORY_EXTERN (gstv4l2codecs_debug);
29 GstVideoFormat gst_fmt;
34 static struct FormatEntry format_map[] = {
35 {V4L2_PIX_FMT_NV12, 1, GST_VIDEO_FORMAT_NV12, 8, 420},
36 {V4L2_PIX_FMT_YUYV, 1, GST_VIDEO_FORMAT_YUY2, 8, 422},
37 {V4L2_PIX_FMT_SUNXI_TILED_NV12, 1, GST_VIDEO_FORMAT_NV12_32L32, 8, 422},
38 {V4L2_PIX_FMT_NV12_4L4, 1, GST_VIDEO_FORMAT_NV12_4L4, 8, 420},
39 {V4L2_PIX_FMT_MM21, 2, GST_VIDEO_FORMAT_NV12_16L32S, 8, 420},
40 {V4L2_PIX_FMT_YUV420M, 3, GST_VIDEO_FORMAT_I420, 8, 420},
41 {V4L2_PIX_FMT_P010, 1, GST_VIDEO_FORMAT_P010_10LE, 16, 420},
45 static struct FormatEntry *
46 lookup_v4l2_fmt (guint v4l2_pix_fmt)
49 struct FormatEntry *ret = NULL;
51 for (i = 0; format_map[i].v4l2_pix_fmt; i++) {
52 if (format_map[i].v4l2_pix_fmt == v4l2_pix_fmt) {
61 static struct FormatEntry *
62 lookup_gst_fmt (GstVideoFormat gst_fmt)
65 struct FormatEntry *ret = NULL;
67 for (i = 0; format_map[i].v4l2_pix_fmt; i++) {
68 if (format_map[i].gst_fmt == gst_fmt) {
78 set_stride (GstVideoInfo * info, gint plane, gint stride)
80 const GstVideoFormatInfo *finfo = info->finfo;
82 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
83 guint x_tiles, y_tiles, tile_height, padded_height;
85 tile_height = GST_VIDEO_FORMAT_INFO_TILE_HEIGHT (finfo, plane);
87 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
90 x_tiles = stride / GST_VIDEO_FORMAT_INFO_TILE_STRIDE (finfo, plane);
91 y_tiles = (padded_height + tile_height - 1) / tile_height;
92 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
94 info->stride[plane] = stride;
99 gst_v4l2_format_to_video_info (struct v4l2_format *fmt, GstVideoInfo * out_info)
101 struct FormatEntry *entry = lookup_v4l2_fmt (fmt->fmt.pix_mp.pixelformat);
102 struct v4l2_pix_format_mplane *pix_mp = &fmt->fmt.pix_mp;
103 struct v4l2_pix_format *pix = &fmt->fmt.pix;
106 gboolean extrapolate = FALSE;
111 /* validate the entry against the format */
112 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
113 if (entry->num_planes != pix_mp->num_planes) {
114 GST_ERROR ("Miss-matched number of planes in internal entry "
115 "(%i != %i)", entry->num_planes, pix_mp->num_planes);
118 } else if (entry->num_planes != 1) {
119 GST_ERROR ("Miss-matched number of planes in internal entry "
120 "(must be 1 for non-multiplanar, got %i)", entry->num_planes);
124 if (!gst_video_info_set_format (out_info, entry->gst_fmt,
125 pix_mp->width, pix_mp->height))
128 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
130 for (plane = 0; plane < pix_mp->num_planes; plane++)
131 out_info->size += pix_mp->plane_fmt[plane].sizeimage;
133 out_info->size = pix->sizeimage;
137 * When single allocation formats are used for planar formats we need to
138 * extrapolate the per-plane stride. Do this check once to prevent
139 * complex inner loop.
141 if (entry->num_planes == 1 && out_info->finfo->n_planes != entry->num_planes)
144 for (plane = 0; plane < GST_VIDEO_INFO_N_PLANES (out_info); plane++) {
147 if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
149 stride = gst_video_format_info_extrapolate_stride (out_info->finfo,
150 plane, pix_mp->plane_fmt[0].bytesperline);
152 stride = pix_mp->plane_fmt[plane].bytesperline;
155 stride = gst_video_format_info_extrapolate_stride (out_info->finfo,
156 plane, pix->bytesperline);
158 stride = pix->bytesperline;
161 set_stride (out_info, plane, stride);
162 out_info->offset[plane] = offset;
164 if ((V4L2_TYPE_IS_MULTIPLANAR (fmt->type) && !extrapolate))
165 offset += pix_mp->plane_fmt[plane].sizeimage;
167 offset += stride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (out_info->finfo,
168 plane, pix_mp->height);
171 /* Check that the extrapolation didn't overflow the reported sizeimage */
172 if (extrapolate && offset > out_info->size) {
173 GST_ERROR ("Extrapolated plane offset overflow the image size.");
181 gst_v4l2_format_to_video_format (guint32 pix_fmt, GstVideoFormat * out_format)
183 struct FormatEntry *entry = lookup_v4l2_fmt (pix_fmt);
188 *out_format = entry->gst_fmt;
193 gst_v4l2_format_from_video_format (GstVideoFormat format, guint32 * out_pix_fmt)
195 struct FormatEntry *entry = lookup_gst_fmt (format);
200 *out_pix_fmt = entry->v4l2_pix_fmt;