FROMGIT: [Encode] Fix for flags in PAK_OBJECT_INSERT command

During HW JPEG encode on devices using iHD media driver, whenever,
application data is split into several payloads for AddMfxInsertObject
and appDataSizeResidue is 0, first payload is marked as end of slice
irrespective of pending application data so, when the next payload
requests AddMfxInsertObject, gpu hang is observed. This patch ensures
that only last payload is marked as last header/end of slice so, gpu
hang issue will be resolved.

cherry picked from commit - https://github.com/intel/media-driver/commit/1f70472c765c086aef77d7ca9604d2754c333ec2

BUG=b:171340559, b:171369066
TEST=After reverting https://chromium-review.googlesource.com/c/chromium/src/+/2554180 and https://chromium-review.googlesource.com/c/chromium/src/+/2566722, capture pictures continuously using camera application for few minutes

Signed-off-by: Sushma Venkatesh Reddy <sushma.venkatesh.reddy@intel.com>
Change-Id: Ibd19e3135ea569524f1743b0e63bc6b4c6ac6a0c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/2566891
Commit-Queue: Andres Calderon Jaramillo <andrescj@chromium.org>
Reviewed-by: Andres Calderon Jaramillo <andrescj@google.com>
diff --git a/x11-libs/libva-intel-media-driver/files/0006-Encode-Fix-for-flags-in-PAK_OBJECT_INSERT-command.patch b/x11-libs/libva-intel-media-driver/files/0006-Encode-Fix-for-flags-in-PAK_OBJECT_INSERT-command.patch
new file mode 100644
index 0000000..a69b6fb
--- /dev/null
+++ b/x11-libs/libva-intel-media-driver/files/0006-Encode-Fix-for-flags-in-PAK_OBJECT_INSERT-command.patch
@@ -0,0 +1,44 @@
+From 3a36cb9ce094dbab76ff6239959d5c73c4c20ea1 Mon Sep 17 00:00:00 2001
+From: AlexeiLeonenko <alexei.leonenko@intel.com>
+Date: Wed, 25 Nov 2020 22:45:34 +0800
+Subject: [PATCH] Fix for flags in PAK_OBJECT_INSERT command
+
+If app_data is split into several payloads for PAK_OBJECT_INSERT
+command only last one should be marked as last header/end of slice.
+---
+ .../agnostic/common/codec/hal/codechal_encode_jpeg.cpp        | 4 ++--
+ .../agnostic/gen11/codec/hal/codechal_encode_jpeg_g11.cpp     | 3 ++-
+ 2 files changed, 4 insertions(+), 3 deletions(-)
+
+diff --git a/media_driver/agnostic/common/codec/hal/codechal_encode_jpeg.cpp b/media_driver/agnostic/common/codec/hal/codechal_encode_jpeg.cpp
+index 031c43e692ac..5c94f7376221 100644
+--- a/media_driver/agnostic/common/codec/hal/codechal_encode_jpeg.cpp
++++ b/media_driver/agnostic/common/codec/hal/codechal_encode_jpeg.cpp
+@@ -966,8 +966,8 @@ MOS_STATUS CodechalEncodeJpegState::ExecuteSliceLevel()
+                 }
+                 pakInsertObjectParams.dwOffset                      = 0;
+                 pakInsertObjectParams.dwBitSize                     = pakInsertObjectParams.pBsBuffer->BufferSize;
+-                //if full header is included in application data, it will be the last insert headers
+-                if((appDataCmdSizeResidue == 0) && m_fullHeaderInAppData)
++                //if full header is included in application data, it will be the last header to insert and last chunk of it should be marked with EndOfSlice
++                if((appDataCmdSizeResidue == 0) && m_fullHeaderInAppData && (i == numAppDataCmdsNeeded - 1))
+                 {
+                     pakInsertObjectParams.bLastHeader                   = true;
+                     pakInsertObjectParams.bEndOfSlice                   = true;
+diff --git a/media_driver/agnostic/gen11/codec/hal/codechal_encode_jpeg_g11.cpp b/media_driver/agnostic/gen11/codec/hal/codechal_encode_jpeg_g11.cpp
+index 6195243deb3d..8a8b63da9819 100644
+--- a/media_driver/agnostic/gen11/codec/hal/codechal_encode_jpeg_g11.cpp
++++ b/media_driver/agnostic/gen11/codec/hal/codechal_encode_jpeg_g11.cpp
+@@ -420,7 +420,8 @@ MOS_STATUS CodechalEncodeJpegStateG11::ExecuteSliceLevel()
+                 }
+                 pakInsertObjectParams.dwOffset = 0;
+                 pakInsertObjectParams.dwBitSize = pakInsertObjectParams.pBsBuffer->BufferSize;
+-                if((appDataCmdSizeResidue == 0) && m_fullHeaderInAppData)
++                //if full header is included in application data, it will be the last header to insert and last chunk of it should be marked with EndOfSlice
++                if ((appDataCmdSizeResidue == 0) && m_fullHeaderInAppData && (i == numAppDataCmdsNeeded - 1))
+                 {
+                     pakInsertObjectParams.bLastHeader = true;
+                     pakInsertObjectParams.bEndOfSlice = true;
+-- 
+2.25.1
+
diff --git a/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0-r6.ebuild b/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0-r7.ebuild
similarity index 100%
rename from x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0-r6.ebuild
rename to x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0-r7.ebuild
diff --git a/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0.ebuild b/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0.ebuild
index c00f108..8c81954 100644
--- a/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0.ebuild
+++ b/x11-libs/libva-intel-media-driver/libva-intel-media-driver-20.3.0.ebuild
@@ -26,6 +26,7 @@
 	"${FILESDIR}"/0003-Explicitly-disable-media-compression.patch
 	"${FILESDIR}"/0004-Decode-Driver-will-derive-width-height_in_sbs_minus_.patch
 	"${FILESDIR}"/0005-Decode-Add-robustness-check-for-superresScaleDenomin.patch
+	"${FILESDIR}"/0006-Encode-Fix-for-flags-in-PAK_OBJECT_INSERT-command.patch
 )
 
 src_configure() {