1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2010 Thomas Balling Sørensen.
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 VMWARE AND/OR ITS SUPPLIERS 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 "util/u_memory.h"
29bf215546Sopenharmony_ci#include "util/u_math.h"
30bf215546Sopenharmony_ci#include "util/u_debug.h"
31bf215546Sopenharmony_ci#include "util/u_video.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "util/vl_vlc.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "vl/vl_codec.h"
36bf215546Sopenharmony_ci#include "vdpau_private.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci/**
39bf215546Sopenharmony_ci * Create a VdpDecoder.
40bf215546Sopenharmony_ci */
41bf215546Sopenharmony_ciVdpStatus
42bf215546Sopenharmony_civlVdpDecoderCreate(VdpDevice device,
43bf215546Sopenharmony_ci                   VdpDecoderProfile profile,
44bf215546Sopenharmony_ci                   uint32_t width, uint32_t height,
45bf215546Sopenharmony_ci                   uint32_t max_references,
46bf215546Sopenharmony_ci                   VdpDecoder *decoder)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   struct pipe_video_codec templat = {};
49bf215546Sopenharmony_ci   struct pipe_context *pipe;
50bf215546Sopenharmony_ci   struct pipe_screen *screen;
51bf215546Sopenharmony_ci   vlVdpDevice *dev;
52bf215546Sopenharmony_ci   vlVdpDecoder *vldecoder;
53bf215546Sopenharmony_ci   VdpStatus ret;
54bf215546Sopenharmony_ci   bool supported;
55bf215546Sopenharmony_ci   uint32_t maxwidth, maxheight;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   if (!decoder)
58bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_POINTER;
59bf215546Sopenharmony_ci   *decoder = 0;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   if (!(width && height))
62bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_VALUE;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   templat.profile = ProfileToPipe(profile);
65bf215546Sopenharmony_ci   if (templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN)
66bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_DECODER_PROFILE;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   dev = vlGetDataHTAB(device);
69bf215546Sopenharmony_ci   if (!dev)
70bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   pipe = dev->context;
73bf215546Sopenharmony_ci   screen = dev->vscreen->pscreen;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   mtx_lock(&dev->mutex);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   supported = vl_codec_supported(screen, templat.profile, false);
78bf215546Sopenharmony_ci   if (!supported) {
79bf215546Sopenharmony_ci      mtx_unlock(&dev->mutex);
80bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_DECODER_PROFILE;
81bf215546Sopenharmony_ci   }
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   maxwidth = screen->get_video_param
84bf215546Sopenharmony_ci   (
85bf215546Sopenharmony_ci      screen,
86bf215546Sopenharmony_ci      templat.profile,
87bf215546Sopenharmony_ci      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
88bf215546Sopenharmony_ci      PIPE_VIDEO_CAP_MAX_WIDTH
89bf215546Sopenharmony_ci   );
90bf215546Sopenharmony_ci   maxheight = screen->get_video_param
91bf215546Sopenharmony_ci   (
92bf215546Sopenharmony_ci      screen,
93bf215546Sopenharmony_ci      templat.profile,
94bf215546Sopenharmony_ci      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
95bf215546Sopenharmony_ci      PIPE_VIDEO_CAP_MAX_HEIGHT
96bf215546Sopenharmony_ci   );
97bf215546Sopenharmony_ci   if (width > maxwidth || height > maxheight) {
98bf215546Sopenharmony_ci      mtx_unlock(&dev->mutex);
99bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_SIZE;
100bf215546Sopenharmony_ci   }
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
103bf215546Sopenharmony_ci   if (!vldecoder) {
104bf215546Sopenharmony_ci      mtx_unlock(&dev->mutex);
105bf215546Sopenharmony_ci      return VDP_STATUS_RESOURCES;
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   DeviceReference(&vldecoder->device, dev);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
111bf215546Sopenharmony_ci   templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
112bf215546Sopenharmony_ci   templat.width = width;
113bf215546Sopenharmony_ci   templat.height = height;
114bf215546Sopenharmony_ci   templat.max_references = max_references;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   if (u_reduce_video_profile(templat.profile) ==
117bf215546Sopenharmony_ci       PIPE_VIDEO_FORMAT_MPEG4_AVC)
118bf215546Sopenharmony_ci      templat.level = u_get_h264_level(templat.width, templat.height,
119bf215546Sopenharmony_ci                            &templat.max_references);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   vldecoder->decoder = pipe->create_video_codec(pipe, &templat);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   if (!vldecoder->decoder) {
124bf215546Sopenharmony_ci      ret = VDP_STATUS_ERROR;
125bf215546Sopenharmony_ci      goto error_decoder;
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   *decoder = vlAddDataHTAB(vldecoder);
129bf215546Sopenharmony_ci   if (*decoder == 0) {
130bf215546Sopenharmony_ci      ret = VDP_STATUS_ERROR;
131bf215546Sopenharmony_ci      goto error_handle;
132bf215546Sopenharmony_ci   }
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   (void) mtx_init(&vldecoder->mutex, mtx_plain);
135bf215546Sopenharmony_ci   mtx_unlock(&dev->mutex);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   return VDP_STATUS_OK;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cierror_handle:
140bf215546Sopenharmony_ci   vldecoder->decoder->destroy(vldecoder->decoder);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_cierror_decoder:
143bf215546Sopenharmony_ci   mtx_unlock(&dev->mutex);
144bf215546Sopenharmony_ci   DeviceReference(&vldecoder->device, NULL);
145bf215546Sopenharmony_ci   FREE(vldecoder);
146bf215546Sopenharmony_ci   return ret;
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci/**
150bf215546Sopenharmony_ci * Destroy a VdpDecoder.
151bf215546Sopenharmony_ci */
152bf215546Sopenharmony_ciVdpStatus
153bf215546Sopenharmony_civlVdpDecoderDestroy(VdpDecoder decoder)
154bf215546Sopenharmony_ci{
155bf215546Sopenharmony_ci   vlVdpDecoder *vldecoder;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
158bf215546Sopenharmony_ci   if (!vldecoder)
159bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   mtx_lock(&vldecoder->mutex);
162bf215546Sopenharmony_ci   vldecoder->decoder->destroy(vldecoder->decoder);
163bf215546Sopenharmony_ci   mtx_unlock(&vldecoder->mutex);
164bf215546Sopenharmony_ci   mtx_destroy(&vldecoder->mutex);
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   vlRemoveDataHTAB(decoder);
167bf215546Sopenharmony_ci   DeviceReference(&vldecoder->device, NULL);
168bf215546Sopenharmony_ci   FREE(vldecoder);
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   return VDP_STATUS_OK;
171bf215546Sopenharmony_ci}
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci/**
174bf215546Sopenharmony_ci * Retrieve the parameters used to create a VdpDecoder.
175bf215546Sopenharmony_ci */
176bf215546Sopenharmony_ciVdpStatus
177bf215546Sopenharmony_civlVdpDecoderGetParameters(VdpDecoder decoder,
178bf215546Sopenharmony_ci                          VdpDecoderProfile *profile,
179bf215546Sopenharmony_ci                          uint32_t *width,
180bf215546Sopenharmony_ci                          uint32_t *height)
181bf215546Sopenharmony_ci{
182bf215546Sopenharmony_ci   vlVdpDecoder *vldecoder;
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
185bf215546Sopenharmony_ci   if (!vldecoder)
186bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   *profile = PipeToProfile(vldecoder->decoder->profile);
189bf215546Sopenharmony_ci   *width = vldecoder->decoder->width;
190bf215546Sopenharmony_ci   *height = vldecoder->decoder->height;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   return VDP_STATUS_OK;
193bf215546Sopenharmony_ci}
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_cistatic VdpStatus
196bf215546Sopenharmony_civlVdpGetReferenceFrame(VdpVideoSurface handle, struct pipe_video_buffer **ref_frame)
197bf215546Sopenharmony_ci{
198bf215546Sopenharmony_ci   vlVdpSurface *surface;
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
201bf215546Sopenharmony_ci   if (handle ==  VDP_INVALID_HANDLE) {
202bf215546Sopenharmony_ci      *ref_frame = NULL;
203bf215546Sopenharmony_ci      return VDP_STATUS_OK;
204bf215546Sopenharmony_ci   }
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   surface = vlGetDataHTAB(handle);
207bf215546Sopenharmony_ci   if (!surface)
208bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   *ref_frame = surface->video_buffer;
211bf215546Sopenharmony_ci   if (!*ref_frame)
212bf215546Sopenharmony_ci         return VDP_STATUS_INVALID_HANDLE;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   return VDP_STATUS_OK;
215bf215546Sopenharmony_ci}
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci/**
218bf215546Sopenharmony_ci * Decode a mpeg 1/2 video.
219bf215546Sopenharmony_ci */
220bf215546Sopenharmony_cistatic VdpStatus
221bf215546Sopenharmony_civlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc *picture,
222bf215546Sopenharmony_ci                         VdpPictureInfoMPEG1Or2 *picture_info)
223bf215546Sopenharmony_ci{
224bf215546Sopenharmony_ci   VdpStatus r;
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
229bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
230bf215546Sopenharmony_ci      return r;
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
233bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
234bf215546Sopenharmony_ci      return r;
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci   picture->picture_coding_type = picture_info->picture_coding_type;
237bf215546Sopenharmony_ci   picture->picture_structure = picture_info->picture_structure;
238bf215546Sopenharmony_ci   picture->frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
239bf215546Sopenharmony_ci   picture->q_scale_type = picture_info->q_scale_type;
240bf215546Sopenharmony_ci   picture->alternate_scan = picture_info->alternate_scan;
241bf215546Sopenharmony_ci   picture->intra_vlc_format = picture_info->intra_vlc_format;
242bf215546Sopenharmony_ci   picture->concealment_motion_vectors = picture_info->concealment_motion_vectors;
243bf215546Sopenharmony_ci   picture->intra_dc_precision = picture_info->intra_dc_precision;
244bf215546Sopenharmony_ci   picture->f_code[0][0] = picture_info->f_code[0][0] - 1;
245bf215546Sopenharmony_ci   picture->f_code[0][1] = picture_info->f_code[0][1] - 1;
246bf215546Sopenharmony_ci   picture->f_code[1][0] = picture_info->f_code[1][0] - 1;
247bf215546Sopenharmony_ci   picture->f_code[1][1] = picture_info->f_code[1][1] - 1;
248bf215546Sopenharmony_ci   picture->num_slices = picture_info->slice_count;
249bf215546Sopenharmony_ci   picture->top_field_first = picture_info->top_field_first;
250bf215546Sopenharmony_ci   picture->full_pel_forward_vector = picture_info->full_pel_forward_vector;
251bf215546Sopenharmony_ci   picture->full_pel_backward_vector = picture_info->full_pel_backward_vector;
252bf215546Sopenharmony_ci   picture->intra_matrix = picture_info->intra_quantizer_matrix;
253bf215546Sopenharmony_ci   picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   return VDP_STATUS_OK;
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci/**
259bf215546Sopenharmony_ci * Decode a mpeg 4 video.
260bf215546Sopenharmony_ci */
261bf215546Sopenharmony_cistatic VdpStatus
262bf215546Sopenharmony_civlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc *picture,
263bf215546Sopenharmony_ci                        VdpPictureInfoMPEG4Part2 *picture_info)
264bf215546Sopenharmony_ci{
265bf215546Sopenharmony_ci   VdpStatus r;
266bf215546Sopenharmony_ci   unsigned i;
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG4\n");
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
271bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
272bf215546Sopenharmony_ci      return r;
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
275bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
276bf215546Sopenharmony_ci      return r;
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i) {
279bf215546Sopenharmony_ci      picture->trd[i] = picture_info->trd[i];
280bf215546Sopenharmony_ci      picture->trb[i] = picture_info->trb[i];
281bf215546Sopenharmony_ci   }
282bf215546Sopenharmony_ci   picture->vop_time_increment_resolution = picture_info->vop_time_increment_resolution;
283bf215546Sopenharmony_ci   picture->vop_coding_type = picture_info->vop_coding_type;
284bf215546Sopenharmony_ci   picture->vop_fcode_forward = picture_info->vop_fcode_forward;
285bf215546Sopenharmony_ci   picture->vop_fcode_backward = picture_info->vop_fcode_backward;
286bf215546Sopenharmony_ci   picture->resync_marker_disable = picture_info->resync_marker_disable;
287bf215546Sopenharmony_ci   picture->interlaced = picture_info->interlaced;
288bf215546Sopenharmony_ci   picture->quant_type = picture_info->quant_type;
289bf215546Sopenharmony_ci   picture->quarter_sample = picture_info->quarter_sample;
290bf215546Sopenharmony_ci   picture->short_video_header = picture_info->short_video_header;
291bf215546Sopenharmony_ci   picture->rounding_control = picture_info->rounding_control;
292bf215546Sopenharmony_ci   picture->alternate_vertical_scan_flag = picture_info->alternate_vertical_scan_flag;
293bf215546Sopenharmony_ci   picture->top_field_first = picture_info->top_field_first;
294bf215546Sopenharmony_ci   picture->intra_matrix = picture_info->intra_quantizer_matrix;
295bf215546Sopenharmony_ci   picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   return VDP_STATUS_OK;
298bf215546Sopenharmony_ci}
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_cistatic VdpStatus
301bf215546Sopenharmony_civlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc *picture,
302bf215546Sopenharmony_ci                      VdpPictureInfoVC1 *picture_info)
303bf215546Sopenharmony_ci{
304bf215546Sopenharmony_ci   VdpStatus r;
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding VC-1\n");
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
309bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
310bf215546Sopenharmony_ci      return r;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
313bf215546Sopenharmony_ci   if (r != VDP_STATUS_OK)
314bf215546Sopenharmony_ci      return r;
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   picture->slice_count = picture_info->slice_count;
317bf215546Sopenharmony_ci   picture->picture_type = picture_info->picture_type;
318bf215546Sopenharmony_ci   picture->frame_coding_mode = picture_info->frame_coding_mode;
319bf215546Sopenharmony_ci   picture->postprocflag = picture_info->postprocflag;
320bf215546Sopenharmony_ci   picture->pulldown = picture_info->pulldown;
321bf215546Sopenharmony_ci   picture->interlace = picture_info->interlace;
322bf215546Sopenharmony_ci   picture->tfcntrflag = picture_info->tfcntrflag;
323bf215546Sopenharmony_ci   picture->finterpflag = picture_info->finterpflag;
324bf215546Sopenharmony_ci   picture->psf = picture_info->psf;
325bf215546Sopenharmony_ci   picture->dquant = picture_info->dquant;
326bf215546Sopenharmony_ci   picture->panscan_flag = picture_info->panscan_flag;
327bf215546Sopenharmony_ci   picture->refdist_flag = picture_info->refdist_flag;
328bf215546Sopenharmony_ci   picture->quantizer = picture_info->quantizer;
329bf215546Sopenharmony_ci   picture->extended_mv = picture_info->extended_mv;
330bf215546Sopenharmony_ci   picture->extended_dmv = picture_info->extended_dmv;
331bf215546Sopenharmony_ci   picture->overlap = picture_info->overlap;
332bf215546Sopenharmony_ci   picture->vstransform = picture_info->vstransform;
333bf215546Sopenharmony_ci   picture->loopfilter = picture_info->loopfilter;
334bf215546Sopenharmony_ci   picture->fastuvmc = picture_info->fastuvmc;
335bf215546Sopenharmony_ci   picture->range_mapy_flag = picture_info->range_mapy_flag;
336bf215546Sopenharmony_ci   picture->range_mapy = picture_info->range_mapy;
337bf215546Sopenharmony_ci   picture->range_mapuv_flag = picture_info->range_mapuv_flag;
338bf215546Sopenharmony_ci   picture->range_mapuv = picture_info->range_mapuv;
339bf215546Sopenharmony_ci   picture->multires = picture_info->multires;
340bf215546Sopenharmony_ci   picture->syncmarker = picture_info->syncmarker;
341bf215546Sopenharmony_ci   picture->rangered = picture_info->rangered;
342bf215546Sopenharmony_ci   picture->maxbframes = picture_info->maxbframes;
343bf215546Sopenharmony_ci   picture->deblockEnable = picture_info->deblockEnable;
344bf215546Sopenharmony_ci   picture->pquant = picture_info->pquant;
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci   return VDP_STATUS_OK;
347bf215546Sopenharmony_ci}
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_cistatic VdpStatus
350bf215546Sopenharmony_civlVdpDecoderRenderH264(struct pipe_h264_picture_desc *picture,
351bf215546Sopenharmony_ci                       VdpPictureInfoH264 *picture_info, unsigned level_idc)
352bf215546Sopenharmony_ci{
353bf215546Sopenharmony_ci   unsigned i;
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding H264\n");
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci   picture->pps->sps->mb_adaptive_frame_field_flag = picture_info->mb_adaptive_frame_field_flag;
358bf215546Sopenharmony_ci   picture->pps->sps->frame_mbs_only_flag = picture_info->frame_mbs_only_flag;
359bf215546Sopenharmony_ci   picture->pps->sps->log2_max_frame_num_minus4 = picture_info->log2_max_frame_num_minus4;
360bf215546Sopenharmony_ci   picture->pps->sps->pic_order_cnt_type = picture_info->pic_order_cnt_type;
361bf215546Sopenharmony_ci   picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;
362bf215546Sopenharmony_ci   picture->pps->sps->delta_pic_order_always_zero_flag = picture_info->delta_pic_order_always_zero_flag;
363bf215546Sopenharmony_ci   picture->pps->sps->direct_8x8_inference_flag = picture_info->direct_8x8_inference_flag;
364bf215546Sopenharmony_ci   picture->pps->sps->level_idc = level_idc;
365bf215546Sopenharmony_ci   picture->pps->sps->MinLumaBiPredSize8x8 = (level_idc >= 31); /* See section A.3.3.2 of H264 spec */;
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   picture->pps->transform_8x8_mode_flag = picture_info->transform_8x8_mode_flag;
368bf215546Sopenharmony_ci   picture->pps->chroma_qp_index_offset = picture_info->chroma_qp_index_offset;
369bf215546Sopenharmony_ci   picture->pps->second_chroma_qp_index_offset = picture_info->second_chroma_qp_index_offset;
370bf215546Sopenharmony_ci   picture->pps->pic_init_qp_minus26 = picture_info->pic_init_qp_minus26;
371bf215546Sopenharmony_ci   /*picture->pps-> pic_init_qs_minus26 not passed in VdpPictureInfoH264*/
372bf215546Sopenharmony_ci   picture->pps->entropy_coding_mode_flag = picture_info->entropy_coding_mode_flag;
373bf215546Sopenharmony_ci   picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;
374bf215546Sopenharmony_ci   picture->pps->redundant_pic_cnt_present_flag = picture_info->redundant_pic_cnt_present_flag;
375bf215546Sopenharmony_ci   picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;
376bf215546Sopenharmony_ci   picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;
377bf215546Sopenharmony_ci   picture->pps->weighted_bipred_idc = picture_info->weighted_bipred_idc;
378bf215546Sopenharmony_ci   picture->pps->bottom_field_pic_order_in_frame_present_flag = picture_info->pic_order_present_flag;
379bf215546Sopenharmony_ci   memcpy(picture->pps->ScalingList4x4, picture_info->scaling_lists_4x4, 6*16);
380bf215546Sopenharmony_ci   memcpy(picture->pps->ScalingList8x8, picture_info->scaling_lists_8x8, 2*64);
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci   picture->slice_count = picture_info->slice_count;
383bf215546Sopenharmony_ci   picture->field_order_cnt[0] = picture_info->field_order_cnt[0];
384bf215546Sopenharmony_ci   picture->field_order_cnt[1] = picture_info->field_order_cnt[1];
385bf215546Sopenharmony_ci   picture->is_reference = picture_info->is_reference;
386bf215546Sopenharmony_ci   picture->frame_num = picture_info->frame_num;
387bf215546Sopenharmony_ci   picture->field_pic_flag = picture_info->field_pic_flag;
388bf215546Sopenharmony_ci   picture->bottom_field_flag = picture_info->bottom_field_flag;
389bf215546Sopenharmony_ci   picture->num_ref_frames = picture_info->num_ref_frames;
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci   picture->num_ref_idx_l0_active_minus1 = picture_info->num_ref_idx_l0_active_minus1;
392bf215546Sopenharmony_ci   picture->num_ref_idx_l1_active_minus1 = picture_info->num_ref_idx_l1_active_minus1;
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci   for (i = 0; i < 16; ++i) {
395bf215546Sopenharmony_ci      VdpStatus ret = vlVdpGetReferenceFrame
396bf215546Sopenharmony_ci      (
397bf215546Sopenharmony_ci         picture_info->referenceFrames[i].surface,
398bf215546Sopenharmony_ci         &picture->ref[i]
399bf215546Sopenharmony_ci      );
400bf215546Sopenharmony_ci      if (ret != VDP_STATUS_OK)
401bf215546Sopenharmony_ci         return ret;
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci      picture->is_long_term[i] = picture_info->referenceFrames[i].is_long_term;
404bf215546Sopenharmony_ci      picture->top_is_reference[i] = picture_info->referenceFrames[i].top_is_reference;
405bf215546Sopenharmony_ci      picture->bottom_is_reference[i] = picture_info->referenceFrames[i].bottom_is_reference;
406bf215546Sopenharmony_ci      picture->field_order_cnt_list[i][0] = picture_info->referenceFrames[i].field_order_cnt[0];
407bf215546Sopenharmony_ci      picture->field_order_cnt_list[i][1] = picture_info->referenceFrames[i].field_order_cnt[1];
408bf215546Sopenharmony_ci      picture->frame_num_list[i] = picture_info->referenceFrames[i].frame_idx;
409bf215546Sopenharmony_ci   }
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci   return VDP_STATUS_OK;
412bf215546Sopenharmony_ci}
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_cistatic VdpStatus
415bf215546Sopenharmony_civlVdpDecoderRenderH265(struct pipe_h265_picture_desc *picture,
416bf215546Sopenharmony_ci                       VdpPictureInfoHEVC *picture_info)
417bf215546Sopenharmony_ci{
418bf215546Sopenharmony_ci   unsigned i;
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   picture->pps->sps->chroma_format_idc = picture_info->chroma_format_idc;
421bf215546Sopenharmony_ci   picture->pps->sps->separate_colour_plane_flag = picture_info->separate_colour_plane_flag;
422bf215546Sopenharmony_ci   picture->pps->sps->pic_width_in_luma_samples = picture_info->pic_width_in_luma_samples;
423bf215546Sopenharmony_ci   picture->pps->sps->pic_height_in_luma_samples = picture_info->pic_height_in_luma_samples;
424bf215546Sopenharmony_ci   picture->pps->sps->bit_depth_luma_minus8 = picture_info->bit_depth_luma_minus8;
425bf215546Sopenharmony_ci   picture->pps->sps->bit_depth_chroma_minus8 = picture_info->bit_depth_chroma_minus8;
426bf215546Sopenharmony_ci   picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;
427bf215546Sopenharmony_ci   picture->pps->sps->sps_max_dec_pic_buffering_minus1 = picture_info->sps_max_dec_pic_buffering_minus1;
428bf215546Sopenharmony_ci   picture->pps->sps->log2_min_luma_coding_block_size_minus3 = picture_info->log2_min_luma_coding_block_size_minus3;
429bf215546Sopenharmony_ci   picture->pps->sps->log2_diff_max_min_luma_coding_block_size = picture_info->log2_diff_max_min_luma_coding_block_size;
430bf215546Sopenharmony_ci   picture->pps->sps->log2_min_transform_block_size_minus2 = picture_info->log2_min_transform_block_size_minus2;
431bf215546Sopenharmony_ci   picture->pps->sps->log2_diff_max_min_transform_block_size = picture_info->log2_diff_max_min_transform_block_size;
432bf215546Sopenharmony_ci   picture->pps->sps->max_transform_hierarchy_depth_inter = picture_info->max_transform_hierarchy_depth_inter;
433bf215546Sopenharmony_ci   picture->pps->sps->max_transform_hierarchy_depth_intra = picture_info->max_transform_hierarchy_depth_intra;
434bf215546Sopenharmony_ci   picture->pps->sps->scaling_list_enabled_flag = picture_info->scaling_list_enabled_flag;
435bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingList4x4, picture_info->ScalingList4x4, 6*16);
436bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingList8x8, picture_info->ScalingList8x8, 6*64);
437bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingList16x16, picture_info->ScalingList16x16, 6*64);
438bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingList32x32, picture_info->ScalingList32x32, 2*64);
439bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingListDCCoeff16x16, picture_info->ScalingListDCCoeff16x16, 6);
440bf215546Sopenharmony_ci   memcpy(picture->pps->sps->ScalingListDCCoeff32x32, picture_info->ScalingListDCCoeff32x32, 2);
441bf215546Sopenharmony_ci   picture->pps->sps->amp_enabled_flag = picture_info->amp_enabled_flag;
442bf215546Sopenharmony_ci   picture->pps->sps->sample_adaptive_offset_enabled_flag = picture_info->sample_adaptive_offset_enabled_flag;
443bf215546Sopenharmony_ci   picture->pps->sps->pcm_enabled_flag = picture_info->pcm_enabled_flag;
444bf215546Sopenharmony_ci   picture->pps->sps->pcm_sample_bit_depth_luma_minus1 = picture_info->pcm_sample_bit_depth_luma_minus1;
445bf215546Sopenharmony_ci   picture->pps->sps->pcm_sample_bit_depth_chroma_minus1 = picture_info->pcm_sample_bit_depth_chroma_minus1;
446bf215546Sopenharmony_ci   picture->pps->sps->log2_min_pcm_luma_coding_block_size_minus3 = picture_info->log2_min_pcm_luma_coding_block_size_minus3;
447bf215546Sopenharmony_ci   picture->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size = picture_info->log2_diff_max_min_pcm_luma_coding_block_size;
448bf215546Sopenharmony_ci   picture->pps->sps->pcm_loop_filter_disabled_flag = picture_info->pcm_loop_filter_disabled_flag;
449bf215546Sopenharmony_ci   picture->pps->sps->num_short_term_ref_pic_sets = picture_info->num_short_term_ref_pic_sets;
450bf215546Sopenharmony_ci   picture->pps->sps->long_term_ref_pics_present_flag = picture_info->long_term_ref_pics_present_flag;
451bf215546Sopenharmony_ci   picture->pps->sps->num_long_term_ref_pics_sps = picture_info->num_long_term_ref_pics_sps;
452bf215546Sopenharmony_ci   picture->pps->sps->sps_temporal_mvp_enabled_flag = picture_info->sps_temporal_mvp_enabled_flag;
453bf215546Sopenharmony_ci   picture->pps->sps->strong_intra_smoothing_enabled_flag = picture_info->strong_intra_smoothing_enabled_flag;
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci   picture->pps->dependent_slice_segments_enabled_flag = picture_info->dependent_slice_segments_enabled_flag;
456bf215546Sopenharmony_ci   picture->pps->output_flag_present_flag = picture_info->output_flag_present_flag;
457bf215546Sopenharmony_ci   picture->pps->num_extra_slice_header_bits = picture_info->num_extra_slice_header_bits;
458bf215546Sopenharmony_ci   picture->pps->sign_data_hiding_enabled_flag = picture_info->sign_data_hiding_enabled_flag;
459bf215546Sopenharmony_ci   picture->pps->cabac_init_present_flag = picture_info->cabac_init_present_flag;
460bf215546Sopenharmony_ci   picture->pps->num_ref_idx_l0_default_active_minus1 = picture_info->num_ref_idx_l0_default_active_minus1;
461bf215546Sopenharmony_ci   picture->pps->num_ref_idx_l1_default_active_minus1 = picture_info->num_ref_idx_l1_default_active_minus1;
462bf215546Sopenharmony_ci   picture->pps->init_qp_minus26 = picture_info->init_qp_minus26;
463bf215546Sopenharmony_ci   picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;
464bf215546Sopenharmony_ci   picture->pps->transform_skip_enabled_flag = picture_info->transform_skip_enabled_flag;
465bf215546Sopenharmony_ci   picture->pps->cu_qp_delta_enabled_flag = picture_info->cu_qp_delta_enabled_flag;
466bf215546Sopenharmony_ci   picture->pps->diff_cu_qp_delta_depth = picture_info->diff_cu_qp_delta_depth;
467bf215546Sopenharmony_ci   picture->pps->pps_cb_qp_offset = picture_info->pps_cb_qp_offset;
468bf215546Sopenharmony_ci   picture->pps->pps_cr_qp_offset = picture_info->pps_cr_qp_offset;
469bf215546Sopenharmony_ci   picture->pps->pps_slice_chroma_qp_offsets_present_flag = picture_info->pps_slice_chroma_qp_offsets_present_flag;
470bf215546Sopenharmony_ci   picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;
471bf215546Sopenharmony_ci   picture->pps->weighted_bipred_flag = picture_info->weighted_bipred_flag;
472bf215546Sopenharmony_ci   picture->pps->transquant_bypass_enabled_flag = picture_info->transquant_bypass_enabled_flag;
473bf215546Sopenharmony_ci   picture->pps->tiles_enabled_flag = picture_info->tiles_enabled_flag;
474bf215546Sopenharmony_ci   picture->pps->entropy_coding_sync_enabled_flag = picture_info->entropy_coding_sync_enabled_flag;
475bf215546Sopenharmony_ci   picture->pps->num_tile_columns_minus1 = picture_info->num_tile_columns_minus1;
476bf215546Sopenharmony_ci   picture->pps->num_tile_rows_minus1 = picture_info->num_tile_rows_minus1;
477bf215546Sopenharmony_ci   picture->pps->uniform_spacing_flag = picture_info->uniform_spacing_flag;
478bf215546Sopenharmony_ci   memcpy(picture->pps->column_width_minus1, picture_info->column_width_minus1, 20 * 2);
479bf215546Sopenharmony_ci   memcpy(picture->pps->row_height_minus1, picture_info->row_height_minus1, 22 * 2);
480bf215546Sopenharmony_ci   picture->pps->loop_filter_across_tiles_enabled_flag = picture_info->loop_filter_across_tiles_enabled_flag;
481bf215546Sopenharmony_ci   picture->pps->pps_loop_filter_across_slices_enabled_flag = picture_info->pps_loop_filter_across_slices_enabled_flag;
482bf215546Sopenharmony_ci   picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;
483bf215546Sopenharmony_ci   picture->pps->deblocking_filter_override_enabled_flag = picture_info->deblocking_filter_override_enabled_flag;
484bf215546Sopenharmony_ci   picture->pps->pps_deblocking_filter_disabled_flag = picture_info->pps_deblocking_filter_disabled_flag;
485bf215546Sopenharmony_ci   picture->pps->pps_beta_offset_div2 = picture_info->pps_beta_offset_div2;
486bf215546Sopenharmony_ci   picture->pps->pps_tc_offset_div2 = picture_info->pps_tc_offset_div2;
487bf215546Sopenharmony_ci   picture->pps->lists_modification_present_flag = picture_info->lists_modification_present_flag;
488bf215546Sopenharmony_ci   picture->pps->log2_parallel_merge_level_minus2 = picture_info->log2_parallel_merge_level_minus2;
489bf215546Sopenharmony_ci   picture->pps->slice_segment_header_extension_present_flag = picture_info->slice_segment_header_extension_present_flag;
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci   picture->IDRPicFlag = picture_info->IDRPicFlag;
492bf215546Sopenharmony_ci   picture->RAPPicFlag = picture_info->RAPPicFlag;
493bf215546Sopenharmony_ci   picture->CurrRpsIdx = picture_info->CurrRpsIdx;
494bf215546Sopenharmony_ci   picture->NumPocTotalCurr = picture_info->NumPocTotalCurr;
495bf215546Sopenharmony_ci   picture->NumDeltaPocsOfRefRpsIdx = picture_info->NumDeltaPocsOfRefRpsIdx;
496bf215546Sopenharmony_ci   picture->NumShortTermPictureSliceHeaderBits = picture_info->NumShortTermPictureSliceHeaderBits;
497bf215546Sopenharmony_ci   picture->NumLongTermPictureSliceHeaderBits = picture_info->NumLongTermPictureSliceHeaderBits;
498bf215546Sopenharmony_ci   picture->CurrPicOrderCntVal = picture_info->CurrPicOrderCntVal;
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   for (i = 0; i < 16; ++i) {
501bf215546Sopenharmony_ci      VdpStatus ret = vlVdpGetReferenceFrame
502bf215546Sopenharmony_ci      (
503bf215546Sopenharmony_ci         picture_info->RefPics[i],
504bf215546Sopenharmony_ci         &picture->ref[i]
505bf215546Sopenharmony_ci      );
506bf215546Sopenharmony_ci      if (ret != VDP_STATUS_OK)
507bf215546Sopenharmony_ci         return ret;
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci      picture->PicOrderCntVal[i] = picture_info->PicOrderCntVal[i];
510bf215546Sopenharmony_ci      picture->IsLongTerm[i] = picture_info->IsLongTerm[i];
511bf215546Sopenharmony_ci   }
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ci   picture->NumPocStCurrBefore = picture_info->NumPocStCurrBefore;
514bf215546Sopenharmony_ci   picture->NumPocStCurrAfter = picture_info->NumPocStCurrAfter;
515bf215546Sopenharmony_ci   picture->NumPocLtCurr = picture_info->NumPocLtCurr;
516bf215546Sopenharmony_ci   memcpy(picture->RefPicSetStCurrBefore, picture_info->RefPicSetStCurrBefore, 8);
517bf215546Sopenharmony_ci   memcpy(picture->RefPicSetStCurrAfter, picture_info->RefPicSetStCurrAfter, 8);
518bf215546Sopenharmony_ci   memcpy(picture->RefPicSetLtCurr, picture_info->RefPicSetLtCurr, 8);
519bf215546Sopenharmony_ci   picture->UseRefPicList = false;
520bf215546Sopenharmony_ci   picture->UseStRpsBits = false;
521bf215546Sopenharmony_ci
522bf215546Sopenharmony_ci   return VDP_STATUS_OK;
523bf215546Sopenharmony_ci}
524bf215546Sopenharmony_ci
525bf215546Sopenharmony_cistatic void
526bf215546Sopenharmony_civlVdpDecoderFixVC1Startcode(uint32_t *num_buffers, const void *buffers[], unsigned sizes[])
527bf215546Sopenharmony_ci{
528bf215546Sopenharmony_ci   static const uint8_t vc1_startcode[] = { 0x00, 0x00, 0x01, 0x0D };
529bf215546Sopenharmony_ci   struct vl_vlc vlc = {};
530bf215546Sopenharmony_ci   unsigned i;
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci   /* search the first 64 bytes for a startcode */
533bf215546Sopenharmony_ci   vl_vlc_init(&vlc, *num_buffers, buffers, sizes);
534bf215546Sopenharmony_ci   while (vl_vlc_search_byte(&vlc, 64*8, 0x00) && vl_vlc_bits_left(&vlc) >= 32) {
535bf215546Sopenharmony_ci      uint32_t value = vl_vlc_peekbits(&vlc, 32);
536bf215546Sopenharmony_ci      if (value == 0x0000010D ||
537bf215546Sopenharmony_ci          value == 0x0000010C ||
538bf215546Sopenharmony_ci          value == 0x0000010B)
539bf215546Sopenharmony_ci         return;
540bf215546Sopenharmony_ci      vl_vlc_eatbits(&vlc, 8);
541bf215546Sopenharmony_ci   }
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci   /* none found, ok add one manually */
544bf215546Sopenharmony_ci   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Manually adding VC-1 startcode\n");
545bf215546Sopenharmony_ci   for (i = *num_buffers; i > 0; --i) {
546bf215546Sopenharmony_ci      buffers[i] = buffers[i - 1];
547bf215546Sopenharmony_ci      sizes[i] = sizes[i - 1];
548bf215546Sopenharmony_ci   }
549bf215546Sopenharmony_ci   ++(*num_buffers);
550bf215546Sopenharmony_ci   buffers[0] = vc1_startcode;
551bf215546Sopenharmony_ci   sizes[0] = 4;
552bf215546Sopenharmony_ci}
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci/**
555bf215546Sopenharmony_ci * Decode a compressed field/frame and render the result into a VdpVideoSurface.
556bf215546Sopenharmony_ci */
557bf215546Sopenharmony_ciVdpStatus
558bf215546Sopenharmony_civlVdpDecoderRender(VdpDecoder decoder,
559bf215546Sopenharmony_ci                   VdpVideoSurface target,
560bf215546Sopenharmony_ci                   VdpPictureInfo const *picture_info,
561bf215546Sopenharmony_ci                   uint32_t bitstream_buffer_count,
562bf215546Sopenharmony_ci                   VdpBitstreamBuffer const *bitstream_buffers)
563bf215546Sopenharmony_ci{
564bf215546Sopenharmony_ci   const void * buffers[bitstream_buffer_count + 1];
565bf215546Sopenharmony_ci   unsigned sizes[bitstream_buffer_count + 1];
566bf215546Sopenharmony_ci   vlVdpDecoder *vldecoder;
567bf215546Sopenharmony_ci   vlVdpSurface *vlsurf;
568bf215546Sopenharmony_ci   VdpStatus ret;
569bf215546Sopenharmony_ci   struct pipe_screen *screen;
570bf215546Sopenharmony_ci   struct pipe_video_codec *dec;
571bf215546Sopenharmony_ci   bool buffer_support[2];
572bf215546Sopenharmony_ci   unsigned i;
573bf215546Sopenharmony_ci   struct pipe_h264_sps sps_h264 = {};
574bf215546Sopenharmony_ci   struct pipe_h264_pps pps_h264 = { &sps_h264 };
575bf215546Sopenharmony_ci   struct pipe_h265_sps sps_h265 = {};
576bf215546Sopenharmony_ci   struct pipe_h265_pps pps_h265 = { &sps_h265 };
577bf215546Sopenharmony_ci   union {
578bf215546Sopenharmony_ci      struct pipe_picture_desc base;
579bf215546Sopenharmony_ci      struct pipe_mpeg12_picture_desc mpeg12;
580bf215546Sopenharmony_ci      struct pipe_mpeg4_picture_desc mpeg4;
581bf215546Sopenharmony_ci      struct pipe_vc1_picture_desc vc1;
582bf215546Sopenharmony_ci      struct pipe_h264_picture_desc h264;
583bf215546Sopenharmony_ci      struct pipe_h265_picture_desc h265;
584bf215546Sopenharmony_ci   } desc;
585bf215546Sopenharmony_ci
586bf215546Sopenharmony_ci   if (!(picture_info && bitstream_buffers))
587bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_POINTER;
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_ci   vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
590bf215546Sopenharmony_ci   if (!vldecoder)
591bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
592bf215546Sopenharmony_ci   dec = vldecoder->decoder;
593bf215546Sopenharmony_ci   screen = dec->context->screen;
594bf215546Sopenharmony_ci
595bf215546Sopenharmony_ci   vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
596bf215546Sopenharmony_ci   if (!vlsurf)
597bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_HANDLE;
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   if (vlsurf->device != vldecoder->device)
600bf215546Sopenharmony_ci      return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci   if (vlsurf->video_buffer != NULL &&
603bf215546Sopenharmony_ci       pipe_format_to_chroma_format(vlsurf->video_buffer->buffer_format) != dec->chroma_format)
604bf215546Sopenharmony_ci      // TODO: Recreate decoder with correct chroma
605bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_CHROMA_TYPE;
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_ci   buffer_support[0] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
608bf215546Sopenharmony_ci                                               PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);
609bf215546Sopenharmony_ci   buffer_support[1] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
610bf215546Sopenharmony_ci                                               PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_ci   if (vlsurf->video_buffer == NULL ||
613bf215546Sopenharmony_ci       !screen->is_video_format_supported(screen, vlsurf->video_buffer->buffer_format,
614bf215546Sopenharmony_ci                                          dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
615bf215546Sopenharmony_ci       !buffer_support[vlsurf->video_buffer->interlaced]) {
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci      mtx_lock(&vlsurf->device->mutex);
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci      /* destroy the old one */
620bf215546Sopenharmony_ci      if (vlsurf->video_buffer)
621bf215546Sopenharmony_ci         vlsurf->video_buffer->destroy(vlsurf->video_buffer);
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci      /* set the buffer format to the prefered one */
624bf215546Sopenharmony_ci      vlsurf->templat.buffer_format = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
625bf215546Sopenharmony_ci                                                              PIPE_VIDEO_CAP_PREFERED_FORMAT);
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_ci      /* also set interlacing to decoders preferences */
628bf215546Sopenharmony_ci      vlsurf->templat.interlaced = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
629bf215546Sopenharmony_ci                                                           PIPE_VIDEO_CAP_PREFERS_INTERLACED);
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci      /* and recreate the video buffer */
632bf215546Sopenharmony_ci      vlsurf->video_buffer = dec->context->create_video_buffer(dec->context, &vlsurf->templat);
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci      /* still no luck? get me out of here... */
635bf215546Sopenharmony_ci      if (!vlsurf->video_buffer) {
636bf215546Sopenharmony_ci         mtx_unlock(&vlsurf->device->mutex);
637bf215546Sopenharmony_ci         return VDP_STATUS_NO_IMPLEMENTATION;
638bf215546Sopenharmony_ci      }
639bf215546Sopenharmony_ci      vlVdpVideoSurfaceClear(vlsurf);
640bf215546Sopenharmony_ci      mtx_unlock(&vlsurf->device->mutex);
641bf215546Sopenharmony_ci   }
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci   for (i = 0; i < bitstream_buffer_count; ++i) {
644bf215546Sopenharmony_ci      buffers[i] = bitstream_buffers[i].bitstream;
645bf215546Sopenharmony_ci      sizes[i] = bitstream_buffers[i].bitstream_bytes;
646bf215546Sopenharmony_ci   }
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci   memset(&desc, 0, sizeof(desc));
649bf215546Sopenharmony_ci   desc.base.profile = dec->profile;
650bf215546Sopenharmony_ci   switch (u_reduce_video_profile(dec->profile)) {
651bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG12:
652bf215546Sopenharmony_ci      ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info);
653bf215546Sopenharmony_ci      break;
654bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4:
655bf215546Sopenharmony_ci      ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info);
656bf215546Sopenharmony_ci      break;
657bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_VC1:
658bf215546Sopenharmony_ci      if (dec->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED)
659bf215546Sopenharmony_ci         vlVdpDecoderFixVC1Startcode(&bitstream_buffer_count, buffers, sizes);
660bf215546Sopenharmony_ci      ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info);
661bf215546Sopenharmony_ci      break;
662bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_MPEG4_AVC:
663bf215546Sopenharmony_ci      desc.h264.pps = &pps_h264;
664bf215546Sopenharmony_ci      ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info, dec->level);
665bf215546Sopenharmony_ci      break;
666bf215546Sopenharmony_ci   case PIPE_VIDEO_FORMAT_HEVC:
667bf215546Sopenharmony_ci      desc.h265.pps = &pps_h265;
668bf215546Sopenharmony_ci      ret = vlVdpDecoderRenderH265(&desc.h265, (VdpPictureInfoHEVC *)picture_info);
669bf215546Sopenharmony_ci      break;
670bf215546Sopenharmony_ci   default:
671bf215546Sopenharmony_ci      return VDP_STATUS_INVALID_DECODER_PROFILE;
672bf215546Sopenharmony_ci   }
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci   if (ret != VDP_STATUS_OK)
675bf215546Sopenharmony_ci      return ret;
676bf215546Sopenharmony_ci
677bf215546Sopenharmony_ci   mtx_lock(&vldecoder->mutex);
678bf215546Sopenharmony_ci   dec->begin_frame(dec, vlsurf->video_buffer, &desc.base);
679bf215546Sopenharmony_ci   dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes);
680bf215546Sopenharmony_ci   dec->end_frame(dec, vlsurf->video_buffer, &desc.base);
681bf215546Sopenharmony_ci   mtx_unlock(&vldecoder->mutex);
682bf215546Sopenharmony_ci   return ret;
683bf215546Sopenharmony_ci}
684