From f5eb4faa5914f3745820e557ac2401a7d738be66 Mon Sep 17 00:00:00 2001
From: Tianhao Liu <tianhao.liu@intel.com>
Date: Wed, 4 Jul 2018 12:51:10 +0800
Subject: [PATCH] libs: encoder: jpeg: set component id and Tqi

This change is due a problem encoding JPEGs with Intel's
media-driver: green/black image when playback jpeg

This patch sets component identifier and quantization table
destination selector in frame header to support packing headers
by Intel's media-driver that does not accept packed header
in AP level.

https://bugzilla.gnome.org/show_bug.cgi?id=796705

Upstream-Status: Backport [https://gitlab.freedesktop.org/gstreamer/gstreamer-vaapi/commit/f5eb4faa5914f3745820e557ac2401a7d738be66]
Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
---
 gst-libs/gst/vaapi/gstvaapiencoder_jpeg.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/gst-libs/gst/vaapi/gstvaapiencoder_jpeg.c b/gst-libs/gst/vaapi/gstvaapiencoder_jpeg.c
index b3f409d..8491fbc 100644
--- a/gst-libs/gst/vaapi/gstvaapiencoder_jpeg.c
+++ b/gst-libs/gst/vaapi/gstvaapiencoder_jpeg.c
@@ -205,6 +205,7 @@ fill_picture (GstVaapiEncoderJpeg * encoder,
     GstVaapiEncPicture * picture,
     GstVaapiCodedBuffer * codedbuf, GstVaapiSurfaceProxy * surface)
 {
+  guint i;
   VAEncPictureParameterBufferJPEG *const pic_param = picture->param;
 
   memset (pic_param, 0, sizeof (VAEncPictureParameterBufferJPEG));
@@ -224,6 +225,11 @@ fill_picture (GstVaapiEncoderJpeg * encoder,
   pic_param->num_scan = 1;
   pic_param->num_components = encoder->n_components;
   pic_param->quality = encoder->quality;
+  for (i = 0; i < pic_param->num_components; i++) {
+    pic_param->component_id[i] = i + 1;
+    if (i != 0)
+      pic_param->quantiser_table_selector[i] = 1;
+  }
   return TRUE;
 }
 
@@ -437,13 +443,11 @@ generate_frame_hdr (GstJpegFrameHdr * frame_hdr, GstVaapiEncoderJpeg * encoder,
   frame_hdr->num_components = pic_param->num_components;
 
   for (i = 0; i < frame_hdr->num_components; i++) {
-    frame_hdr->components[i].identifier = i + 1;
+    frame_hdr->components[i].identifier = pic_param->component_id[i];
     frame_hdr->components[i].horizontal_factor = encoder->h_samp[i];
     frame_hdr->components[i].vertical_factor = encoder->v_samp[i];
-    if (i == 0)
-      frame_hdr->components[i].quant_table_selector = 0;
-    else
-      frame_hdr->components[i].quant_table_selector = 1;
+    frame_hdr->components[i].quant_table_selector =
+        pic_param->quantiser_table_selector[i];
   }
 }
 
-- 
2.7.4

