v4l2codecs: Fix tiled formats stride conversion
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / v4l2codecs / gstv4l2format.c
1 /* GStreamer
2  * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "gstv4l2format.h"
21
22 #define GST_CAT_DEFAULT gstv4l2codecs_debug
23 GST_DEBUG_CATEGORY_EXTERN (gstv4l2codecs_debug);
24
25 struct FormatEntry
26 {
27   guint32 v4l2_pix_fmt;
28   gint num_planes;
29   GstVideoFormat gst_fmt;
30   guint bitdepth;
31   gint subsampling;
32 };
33
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},
42   {0,}
43 };
44
45 static struct FormatEntry *
46 lookup_v4l2_fmt (guint v4l2_pix_fmt)
47 {
48   gint i;
49   struct FormatEntry *ret = NULL;
50
51   for (i = 0; format_map[i].v4l2_pix_fmt; i++) {
52     if (format_map[i].v4l2_pix_fmt == v4l2_pix_fmt) {
53       ret = format_map + i;
54       break;
55     }
56   }
57
58   return ret;
59 }
60
61 static struct FormatEntry *
62 lookup_gst_fmt (GstVideoFormat gst_fmt)
63 {
64   gint i;
65   struct FormatEntry *ret = NULL;
66
67   for (i = 0; format_map[i].v4l2_pix_fmt; i++) {
68     if (format_map[i].gst_fmt == gst_fmt) {
69       ret = format_map + i;
70       break;
71     }
72   }
73
74   return ret;
75 }
76
77 static void
78 set_stride (GstVideoInfo * info, gint plane, gint stride)
79 {
80   const GstVideoFormatInfo *finfo = info->finfo;
81
82   if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
83     guint x_tiles, y_tiles, tile_height, padded_height;
84
85     tile_height = GST_VIDEO_FORMAT_INFO_TILE_HEIGHT (finfo, plane);
86
87     padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, plane,
88         info->height);
89
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);
93   } else {
94     info->stride[plane] = stride;
95   }
96 }
97
98 gboolean
99 gst_v4l2_format_to_video_info (struct v4l2_format *fmt, GstVideoInfo * out_info)
100 {
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;
104   gint plane;
105   gsize offset = 0;
106   gboolean extrapolate = FALSE;
107
108   if (!entry)
109     return FALSE;
110
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);
116       return FALSE;
117     }
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);
121     return FALSE;
122   }
123
124   if (!gst_video_info_set_format (out_info, entry->gst_fmt,
125           pix_mp->width, pix_mp->height))
126     return FALSE;
127
128   if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
129     out_info->size = 0;
130     for (plane = 0; plane < pix_mp->num_planes; plane++)
131       out_info->size += pix_mp->plane_fmt[plane].sizeimage;
132   } else {
133     out_info->size = pix->sizeimage;
134   }
135
136   /*
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.
140    */
141   if (entry->num_planes == 1 && out_info->finfo->n_planes != entry->num_planes)
142     extrapolate = TRUE;
143
144   for (plane = 0; plane < GST_VIDEO_INFO_N_PLANES (out_info); plane++) {
145     gint stride;
146
147     if (V4L2_TYPE_IS_MULTIPLANAR (fmt->type)) {
148       if (extrapolate)
149         stride = gst_video_format_info_extrapolate_stride (out_info->finfo,
150             plane, pix_mp->plane_fmt[0].bytesperline);
151       else
152         stride = pix_mp->plane_fmt[plane].bytesperline;
153     } else {
154       if (extrapolate)
155         stride = gst_video_format_info_extrapolate_stride (out_info->finfo,
156             plane, pix->bytesperline);
157       else
158         stride = pix->bytesperline;
159     }
160
161     set_stride (out_info, plane, stride);
162     out_info->offset[plane] = offset;
163
164     if ((V4L2_TYPE_IS_MULTIPLANAR (fmt->type) && !extrapolate))
165       offset += pix_mp->plane_fmt[plane].sizeimage;
166     else
167       offset += stride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (out_info->finfo,
168           plane, pix_mp->height);
169   }
170
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.");
174     return FALSE;
175   }
176
177   return TRUE;
178 }
179
180 gboolean
181 gst_v4l2_format_to_video_format (guint32 pix_fmt, GstVideoFormat * out_format)
182 {
183   struct FormatEntry *entry = lookup_v4l2_fmt (pix_fmt);
184
185   if (!entry)
186     return FALSE;
187
188   *out_format = entry->gst_fmt;
189   return TRUE;
190 }
191
192 gboolean
193 gst_v4l2_format_from_video_format (GstVideoFormat format, guint32 * out_pix_fmt)
194 {
195   struct FormatEntry *entry = lookup_gst_fmt (format);
196
197   if (!entry)
198     return FALSE;
199
200   *out_pix_fmt = entry->v4l2_pix_fmt;
201   return TRUE;
202 }