decoder: h264: improve AVC_REF_IDX_STATE for MVC.

Each Reference List Entry has Bit 6 set to one if the reference
picture is to be used as a long-term reference picture. However,
the H.264 standard, and subsequently the VA-API specs, makes it
possible to mark the picture as "used for short-term reference",
as "used for long-term reference", or even none of those flags.

This means we have to handle a minimum of 3 states. This doesn't
fit the range of a single bit. Let's examine how this could be
fixed from known practices.

There are cases where the picture is added to RefPicListX[] even
if it is not marked as "used for short-term reference" or "used
for long-term reference": MVC with inter-view reference components
or inter-view only reference components [H.8.4]. Ultimately, this
has an incidence on the value of colZeroFlag (8.4.1.2.2). Since
there is no way to program that, and that it depends on the picture
to be marked as "used for short-term reference" or not, then it
looks reasonable to imply Bit 6 (LongTermPicFlag) as a picture
that is *not* "used for short-term reference", i.e. thus including
genuine long-term reference pictures, and those that are neither
long-term reference nor short-term reference pictures.

In practice, this fixes MVCNV-2.264.

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
decoder: h264: fix frame store logic for MVC.

In strict MVC decoding mode, when only the necessary set of inter-view
reference pictures are passed to the ReferenceFrames array for decoding
the current picture, we should not re-use a frame store id that might
be needed for decoding another view component in the same access unit.

One way to solve this problem is to track when the VA surface in a
specified frame store id was last referenced. So, a "ref_age" field
is introduced to the GenFrameStore struct and is updated whenever
the surface is being referenced.

Additionally, the list of retired refs candidates (free_refs) is kept
ordered by increasing ref_age. That way, we can immediately know what
is the oldest frame store id to recycle.

Let deltaAge = CurrAge - RefAge:
If deltaAge > 1, we know for sure that the VA surface is gone ;
If deltaAge = 1, the surface could be re-used for inter prediction ;
If deltaAge = 0, the surface could be re-used for inter-view prediction.

The ref_age in each Frame Store entry is always current, i.e. it is
the same for all reference frames that intervened in the decoding
process of all inter view components of the previous access unit. The
age tracks access units.

v2: used a more correct representation of age, instead of POC [Yakui]
v3: minor optimization for detecting changes of access unit [Haihao]

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
Encoding: Reinitialize CBR bit rate-control parameter to support switch of bitrate under CBR

Tested-By: Sean V Kelley <sean.v.kelley@intel.com>
Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Add old version vme shaders

This old vme shaders come from release version 1.0.20, and binding
table indexs in gen7_vme_header.inc are updated to match lastest driver.
Old version vme shaders have lower gpu usage and better performance,
but worse encoding quality.
They can be used for encoding quality configuration.

Signed-off-by: Zhong Li <zhong.li@intel.com>
Add multi quality levels encoding support for GEN7

Two encoding quality levels are support on GEN7.
Default quality level is set to be 1, which has better quality,
but higher gpu usage.
The second quality level is set to be 2, which has worse quality but
it has lower gpu usage.

Other platforms support for multi-quality-level will be added later.

v1->v2: 1. follow haihao's comments to init and check quality_level.
        2. remove CBR limitation for low quality level.

Signed-off-by: Zhong Li <zhong.li@intel.com>
dec/mpeg2: ignore slices which aren't in raster scan order on SNB

Sometimes codec layer incorrectly fills slice parameters due to
the corrupted video

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=71276
Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
(cherry picked from commit c4063375dd864cc90739603a3547b8b37b78e461)

Conflicts:
	src/i965_decoder_utils.c
	src/i965_decoder_utils.h
Fix over assigned callback "QueryConfigEntrypoints".

(cherry picked from commit 520752f97b8878a82f03aa40971928cf49bdcbd2)
SNB doesn't support MPEG-2 encoding

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72016
Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
(cherry picked from commit 4a0f76c5b706fccbc85fadaeee9d785cd7b57d5a)

Conflicts:
	src/i965_drv_video.c
Encoding: Update the comment for the data structure about the packed rawdata/slice_header data

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Encoding: Use the different delimeter to pass packed_rawdata and slice_header based on VAConfigAttribEncPackedHeaders attribute

Currently the packed_slice_header is optional. And it uses the VAEncSliceParameterBuffer
as the delimeter to decide how to insert the packed rawdata/slice_header
for one slice. This is not convenient under some scenario. For example: some
user hope to be more flexible. When the user is responsible for generating the
packed slice_header, it hopes to use the packed slice_header as the delimeter
to determine how to inser the packed rawdata/slice_header for the given slice.
So the VAConfigAttribEncPackedHeaders attriburation of encoding_context is
used to decide which kind of delimeter.
a. When the VAEncPackedSlice is set when calling vaCreateConfig, it will use
the packed slice_header as delimeter. Of course the packed rawdata should be
parsed before the packed slice_header for one given slice. For exmaple:
for the slice 0: the packed rawdata should be parsed before paring the first
packed slice_header. After one packed slice_header is parsed, it will start
to parse the corresponding data for a new slice.

b. When the VAEncPackedSlice is not set when calling vaCreateConfig, it will
use the VAEncSliceParameterBuffer as delimeter.

V1->V2: Return an error instead of only complaining warning message when packed
slice_header is missing for some slice under the VAEncPackedSlice mode. This
is the suggestion from Gwenole and Sreerenj Balachandran.

Signed-off-by: Zhao, Yakui <yakui.zhao@intel.com>
remove fixed uses of inte-gen4asm tool

In the gen7 and gen8 post processing Makefiles the GEN4ASM
variable is not honored when calling intel-gen4asm. This causes
build errors when GEN4ASM is set to a different value

This was discovered when using intel-gpu-tools version 1.7 where
the shaders are actually compiled.

Signed-off-by: Daniel Charles <daniel.charles@intel.com>
Reviewed-by: Zhao, Yakui <yakui.zhao@intel.com>
decoder: Add struct gen_codec_surface

It is the base of struct gen_avc_surface, and move frame_store_id
from gen_avc_surface to gen_codec_surface

Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
decoder: Remove the dependency on H.264 picture parameter

Only POC is needed in the algorithm

Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
decoder: Remove the dependency on the fixed length frame store array

Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
check that intel-gen4asm tool is actually present

intel-gen4asm tool has to be present on the host tools so that
it can actually be used. Cross compiling shows problems when the
gpu-tools version is met but the tool is not available on the PATH

So, first check that the tool is available and later check for version
before defining HAVE_GEN4ASM.

Signed-off-by: Daniel Charles <daniel.charles@intel.com>
Add more check of H264 slice param to avoid GPU hang caused by the incorrect parameter

This is to fix the GPU hang in https://bugs.freedesktop.org/show_bug.cgi?id=76363

V1->V2: Use the new check based on Haihao's comment. Discard the current frame with the error
slice_param instead of smart fix. In such case it can prompt that the error slice_param
can be fixed by the upper-middle.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Tested-by:  ValdikSS <iam@valdikss.org.ru>
Reviewed-by: Xiang Haihao <haihao.xiang@intel.com>
Add support for VAConfigAttribDecSliceMode

So far the driver only supports Normal mode for decoding

Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
decoder: h264: fix RefPicList0/1 without frame in DPB.

Some bitstreams (e.g. truncated, or non conformant), or bugs in codec
layers, would incorrectly make decoders to fill in the RefPicList0/1
lists with surfaces that have not received any content yet.

There is no reason for the driver to crash in such cases.

https://bugs.freedesktop.org/show_bug.cgi?id=82466

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>