1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "va_private.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ciconst int reverse_inverse_zscan[] =
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   /* Reverse inverse z scan pattern */
33bf215546Sopenharmony_ci    0,  2,  3,  9, 10, 20, 21, 35,
34bf215546Sopenharmony_ci    1,  4,  8, 11, 19, 22, 34, 36,
35bf215546Sopenharmony_ci    5,  7, 12, 18, 23, 33, 37, 48,
36bf215546Sopenharmony_ci    6, 13, 17, 24, 32, 38, 47, 49,
37bf215546Sopenharmony_ci   14, 16, 25, 31, 39, 46, 50, 57,
38bf215546Sopenharmony_ci   15, 26, 30, 40, 45, 51, 56, 58,
39bf215546Sopenharmony_ci   27, 29, 41, 44, 52, 55, 59, 62,
40bf215546Sopenharmony_ci   28, 42, 43, 53, 54, 60, 61, 63,
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_civoid vlVaHandlePictureParameterBufferMPEG12(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   VAPictureParameterBufferMPEG2 *mpeg2 = buf->data;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAPictureParameterBufferMPEG2) && buf->num_elements == 1);
48bf215546Sopenharmony_ci   context->desc.mpeg12.num_slices = 0;
49bf215546Sopenharmony_ci   /*horizontal_size;*/
50bf215546Sopenharmony_ci   /*vertical_size;*/
51bf215546Sopenharmony_ci   vlVaGetReferenceFrame(drv, mpeg2->forward_reference_picture, &context->desc.mpeg12.ref[0]);
52bf215546Sopenharmony_ci   vlVaGetReferenceFrame(drv, mpeg2->backward_reference_picture, &context->desc.mpeg12.ref[1]);
53bf215546Sopenharmony_ci   context->desc.mpeg12.picture_coding_type = mpeg2->picture_coding_type;
54bf215546Sopenharmony_ci   context->desc.mpeg12.f_code[0][0] = ((mpeg2->f_code >> 12) & 0xf) - 1;
55bf215546Sopenharmony_ci   context->desc.mpeg12.f_code[0][1] = ((mpeg2->f_code >> 8) & 0xf) - 1;
56bf215546Sopenharmony_ci   context->desc.mpeg12.f_code[1][0] = ((mpeg2->f_code >> 4) & 0xf) - 1;
57bf215546Sopenharmony_ci   context->desc.mpeg12.f_code[1][1] = (mpeg2->f_code & 0xf) - 1;
58bf215546Sopenharmony_ci   context->desc.mpeg12.intra_dc_precision =
59bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.intra_dc_precision;
60bf215546Sopenharmony_ci   context->desc.mpeg12.picture_structure =
61bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.picture_structure;
62bf215546Sopenharmony_ci   context->desc.mpeg12.top_field_first =
63bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.top_field_first;
64bf215546Sopenharmony_ci   context->desc.mpeg12.frame_pred_frame_dct =
65bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.frame_pred_frame_dct;
66bf215546Sopenharmony_ci   context->desc.mpeg12.concealment_motion_vectors =
67bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.concealment_motion_vectors;
68bf215546Sopenharmony_ci   context->desc.mpeg12.q_scale_type =
69bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.q_scale_type;
70bf215546Sopenharmony_ci   context->desc.mpeg12.intra_vlc_format =
71bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.intra_vlc_format;
72bf215546Sopenharmony_ci   context->desc.mpeg12.alternate_scan =
73bf215546Sopenharmony_ci      mpeg2->picture_coding_extension.bits.alternate_scan;
74bf215546Sopenharmony_ci   /*repeat_first_field*/
75bf215546Sopenharmony_ci   /*progressive_frame*/
76bf215546Sopenharmony_ci   /*is_first_field*/
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_civoid vlVaHandleIQMatrixBufferMPEG12(vlVaContext *context, vlVaBuffer *buf)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   VAIQMatrixBufferMPEG2 *mpeg2 = buf->data;
82bf215546Sopenharmony_ci   static uint8_t temp_intra_matrix[64];
83bf215546Sopenharmony_ci   static uint8_t temp_nonintra_matrix[64];
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAIQMatrixBufferMPEG2) && buf->num_elements == 1);
86bf215546Sopenharmony_ci   if (mpeg2->load_intra_quantiser_matrix) {
87bf215546Sopenharmony_ci      /* The quantiser matrix that VAAPI provides has been applied
88bf215546Sopenharmony_ci         with inverse z-scan. However, what we expect in MPEG2
89bf215546Sopenharmony_ci         picture description is the original order. Therefore,
90bf215546Sopenharmony_ci         we need to reverse it back to its original order.
91bf215546Sopenharmony_ci      */
92bf215546Sopenharmony_ci      for (int i = 0; i < 64; i++)
93bf215546Sopenharmony_ci         temp_intra_matrix[i] =
94bf215546Sopenharmony_ci            mpeg2->intra_quantiser_matrix[reverse_inverse_zscan[i]];
95bf215546Sopenharmony_ci      context->desc.mpeg12.intra_matrix = temp_intra_matrix;
96bf215546Sopenharmony_ci   } else
97bf215546Sopenharmony_ci      context->desc.mpeg12.intra_matrix = NULL;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   if (mpeg2->load_non_intra_quantiser_matrix) {
100bf215546Sopenharmony_ci      for (int i = 0; i < 64; i++)
101bf215546Sopenharmony_ci         temp_nonintra_matrix[i] =
102bf215546Sopenharmony_ci            mpeg2->non_intra_quantiser_matrix[reverse_inverse_zscan[i]];
103bf215546Sopenharmony_ci      context->desc.mpeg12.non_intra_matrix = temp_nonintra_matrix;
104bf215546Sopenharmony_ci   } else
105bf215546Sopenharmony_ci      context->desc.mpeg12.non_intra_matrix = NULL;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_civoid vlVaHandleSliceParameterBufferMPEG12(vlVaContext *context, vlVaBuffer *buf)
109bf215546Sopenharmony_ci{
110bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VASliceParameterBufferMPEG2));
111bf215546Sopenharmony_ci   context->desc.mpeg12.num_slices += buf->num_elements;
112bf215546Sopenharmony_ci}
113