Add MT21 support for v4l2convert

v4l2convert now can be used to convert the MTK proprietary format from
the HW video decoder to other fourcc formats

Bug: b/151393184

Change-Id: I4aeb58da74db94c9a93375fffadbb1a094ccc649
diff --git a/debian/patches/0003-fix-wrong-format-set-for-GRAY16_BE.patch b/debian/patches/0003-fix-wrong-format-set-for-GRAY16_BE.patch
new file mode 100644
index 0000000..0a06b2b
--- /dev/null
+++ b/debian/patches/0003-fix-wrong-format-set-for-GRAY16_BE.patch
@@ -0,0 +1,28 @@
+From e98a97499d9796bbfe1ddb840e6c8b209770f77e Mon Sep 17 00:00:00 2001
+From: Fabien Parent <fparent@baylibre.com>
+Date: Wed, 26 Aug 2020 19:09:50 +0200
+Subject: [PATCH 3/5] fix wrong format set for GRAY16_BE
+
+Because of a missing break, a wrong format will be set when trying
+to use GRAY16_BE
+
+Signed-off-by: Fabien Parent <fparent@baylibre.com>
+---
+ sys/v4l2/gstv4l2object.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/sys/v4l2/gstv4l2object.c b/sys/v4l2/gstv4l2object.c
+index 94ed123142c7..4b4b29a830fd 100644
+--- a/sys/v4l2/gstv4l2object.c
++++ b/sys/v4l2/gstv4l2object.c
+@@ -1848,6 +1848,7 @@ gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
+         break;
+       case GST_VIDEO_FORMAT_GRAY16_BE:
+         fourcc = V4L2_PIX_FMT_Y16_BE;
++        break;
+       case GST_VIDEO_FORMAT_MT21:
+         fourcc = V4L2_PIX_FMT_MT21;
+ 		break;
+-- 
+2.28.0
+
diff --git a/debian/patches/0004-Add-support-for-non-continuous-MT21-format.patch b/debian/patches/0004-Add-support-for-non-continuous-MT21-format.patch
new file mode 100644
index 0000000..95b89c5
--- /dev/null
+++ b/debian/patches/0004-Add-support-for-non-continuous-MT21-format.patch
@@ -0,0 +1,28 @@
+From 251ce6860c63eecb636778e5d109e11650f0d220 Mon Sep 17 00:00:00 2001
+From: Fabien Parent <fparent@baylibre.com>
+Date: Wed, 26 Aug 2020 19:10:57 +0200
+Subject: [PATCH 4/5] Add support for non-continuous MT21 format
+
+In order to use non-continuous MT21 planes, we need to set correctly
+its fourcc_nc.
+
+Signed-off-by: Fabien Parent <fparent@baylibre.com>
+---
+ sys/v4l2/gstv4l2object.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/sys/v4l2/gstv4l2object.c b/sys/v4l2/gstv4l2object.c
+index 4b4b29a830fd..886372789536 100644
+--- a/sys/v4l2/gstv4l2object.c
++++ b/sys/v4l2/gstv4l2object.c
+@@ -1851,6 +1851,7 @@ gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
+         break;
+       case GST_VIDEO_FORMAT_MT21:
+         fourcc = V4L2_PIX_FMT_MT21;
++        fourcc_nc = V4L2_PIX_FMT_MT21;
+ 		break;
+       default:
+         break;
+-- 
+2.28.0
+
diff --git a/debian/patches/0005-use-display_-width-height-for-cropping-instead-of-wi.patch b/debian/patches/0005-use-display_-width-height-for-cropping-instead-of-wi.patch
new file mode 100644
index 0000000..816a871
--- /dev/null
+++ b/debian/patches/0005-use-display_-width-height-for-cropping-instead-of-wi.patch
@@ -0,0 +1,84 @@
+From 693b42bc08df5a3e79d21c060910feaa7a830f96 Mon Sep 17 00:00:00 2001
+From: Fabien Parent <fparent@baylibre.com>
+Date: Wed, 26 Aug 2020 19:16:53 +0200
+Subject: [PATCH 5/5] use display_{width,height} for cropping instead of
+ width,height
+
+Use display_width and display_height to do the cropping if there are
+part of the caps. Also always set the cropping.
+
+Signed-off-by: Fabien Parent <fparent@baylibre.com>
+---
+ sys/v4l2/gstv4l2object.c    | 14 +++++++++-----
+ sys/v4l2/gstv4l2object.h    |  2 +-
+ sys/v4l2/gstv4l2transform.c |  4 ++--
+ 3 files changed, 12 insertions(+), 8 deletions(-)
+
+diff --git a/sys/v4l2/gstv4l2object.c b/sys/v4l2/gstv4l2object.c
+index 886372789536..0fefbfb38686 100644
+--- a/sys/v4l2/gstv4l2object.c
++++ b/sys/v4l2/gstv4l2object.c
+@@ -4640,10 +4640,11 @@ gst_v4l2_object_poll (GstV4l2Object * v4l2object, gint timeout)
+ }
+ 
+ gboolean
+-gst_v4l2_object_set_crop (GstV4l2Object * obj)
++gst_v4l2_object_set_crop (GstV4l2Object * obj, GstCaps * caps)
+ {
+   struct v4l2_selection sel = { 0 };
+   struct v4l2_crop crop = { 0 };
++  GstStructure *st;
+ 
+   sel.type = obj->type;
+   sel.target = V4L2_SEL_TGT_CROP;
+@@ -4656,10 +4657,13 @@ gst_v4l2_object_set_crop (GstV4l2Object * obj)
+   crop.type = obj->type;
+   crop.c = sel.r;
+ 
+-  if (obj->align.padding_left + obj->align.padding_top +
+-      obj->align.padding_right + obj->align.padding_bottom == 0) {
+-    GST_DEBUG_OBJECT (obj->dbg_obj, "no cropping needed");
+-    return TRUE;
++  st = gst_caps_get_structure (caps, 0);
++  if (gst_structure_has_field (st, "display_width")) {
++    gst_structure_get_int(st, "display_width", &crop.c.width);
++  }
++
++  if (gst_structure_has_field (st, "display_height")) {
++    gst_structure_get_int(st, "display_height", &crop.c.height);
+   }
+ 
+   GST_DEBUG_OBJECT (obj->dbg_obj,
+diff --git a/sys/v4l2/gstv4l2object.h b/sys/v4l2/gstv4l2object.h
+index 92f22a74cac0..4462fdeba644 100644
+--- a/sys/v4l2/gstv4l2object.h
++++ b/sys/v4l2/gstv4l2object.h
+@@ -303,7 +303,7 @@ GstCaps *    gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object, GstCaps *
+ 
+ gboolean     gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info);
+ 
+-gboolean     gst_v4l2_object_set_crop    (GstV4l2Object * obj);
++gboolean     gst_v4l2_object_set_crop    (GstV4l2Object * obj, GstCaps * caps);
+ 
+ gboolean     gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object, GstQuery * query);
+ 
+diff --git a/sys/v4l2/gstv4l2transform.c b/sys/v4l2/gstv4l2transform.c
+index a9294e233b5b..b8990632a60d 100644
+--- a/sys/v4l2/gstv4l2transform.c
++++ b/sys/v4l2/gstv4l2transform.c
+@@ -220,10 +220,10 @@ gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
+     goto outcaps_failed;
+ 
+   /* FIXME implement fallback if crop not supported */
+-  if (!gst_v4l2_object_set_crop (self->v4l2output))
++  if (!gst_v4l2_object_set_crop (self->v4l2output, outcaps))
+     goto failed;
+ 
+-  if (!gst_v4l2_object_set_crop (self->v4l2capture))
++  if (!gst_v4l2_object_set_crop (self->v4l2capture, incaps))
+     goto failed;
+ 
+   return TRUE;
+-- 
+2.28.0
+
diff --git a/debian/patches/series b/debian/patches/series
index 84d54cd..e505e64 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -1,2 +1,5 @@
 0001-mtk.patch
 0002-v4l2convert.patch
+0003-fix-wrong-format-set-for-GRAY16_BE.patch
+0004-Add-support-for-non-continuous-MT21-format.patch
+0005-use-display_-width-height-for-cropping-instead-of-wi.patch