• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  * NOTE: some of implementations are copied/modified from Chromium code
20  *
21  * Copyright 2015 The Chromium Authors. All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are
25  * met:
26  *
27  *    * Redistributions of source code must retain the above copyright
28  * notice, this list of conditions and the following disclaimer.
29  *    * Redistributions in binary form must reproduce the above
30  * copyright notice, this list of conditions and the following disclaimer
31  * in the documentation and/or other materials provided with the
32  * distribution.
33  *    * Neither the name of Google Inc. nor the names of its
34  * contributors may be used to endorse or promote products derived from
35  * this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 
50 /**
51  * SECTION:element-d3d11vp9dec
52  * @title: d3d11vp9dec
53  *
54  * A Direct3D11/DXVA based VP9 video decoder
55  *
56  * ## Example launch line
57  * ```
58  * gst-launch-1.0 filesrc location=/path/to/vp9/file ! parsebin ! d3d11vp9dec ! d3d11videosink
59  * ```
60  *
61  * Since: 1.18
62  *
63  */
64 
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68 
69 #include "gstd3d11vp9dec.h"
70 #include "gstd3d11pluginutils.h"
71 
72 #include <gst/codecs/gstvp9decoder.h>
73 #include <string.h>
74 #include <vector>
75 
76 /* HACK: to expose dxva data structure on UWP */
77 #ifdef WINAPI_PARTITION_DESKTOP
78 #undef WINAPI_PARTITION_DESKTOP
79 #endif
80 #define WINAPI_PARTITION_DESKTOP 1
81 #include <d3d9.h>
82 #include <dxva.h>
83 
84 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_vp9_dec_debug);
85 #define GST_CAT_DEFAULT gst_d3d11_vp9_dec_debug
86 
87 /* reference list 8 + 4 margin */
88 #define NUM_OUTPUT_VIEW 12
89 
90 /* *INDENT-OFF* */
91 typedef struct _GstD3D11Vp9DecInner
92 {
93   GstD3D11Device *device = nullptr;
94   GstD3D11Decoder *d3d11_decoder = nullptr;
95 
96   DXVA_PicParams_VP9 pic_params;
97   DXVA_Slice_VPx_Short slice;
98 
99   /* In case of VP9, there's only one slice per picture so we don't
100    * need this bitstream buffer, but this will be used for 128 bytes alignment */
101   std::vector<guint8> bitstream_buffer;
102 
103   /* To calculate use_prev_in_find_mv_refs */
104   guint last_frame_width = 0;
105   guint last_frame_height = 0;
106   gboolean last_show_frame = FALSE;
107 } GstD3D11Vp9DecInner;
108 /* *INDENT-ON* */
109 
110 typedef struct _GstD3D11Vp9Dec
111 {
112   GstVp9Decoder parent;
113   GstD3D11Vp9DecInner *inner;
114 } GstD3D11Vp9Dec;
115 
116 typedef struct _GstD3D11Vp9DecClass
117 {
118   GstVp9DecoderClass parent_class;
119   GstD3D11DecoderSubClassData class_data;
120 } GstD3D11Vp9DecClass;
121 
122 static GstElementClass *parent_class = NULL;
123 
124 #define GST_D3D11_VP9_DEC(object) ((GstD3D11Vp9Dec *) (object))
125 #define GST_D3D11_VP9_DEC_GET_CLASS(object) \
126     (G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object),GstD3D11Vp9DecClass))
127 
128 static void gst_d3d11_vp9_dec_get_property (GObject * object,
129     guint prop_id, GValue * value, GParamSpec * pspec);
130 static void gst_d3d11_vp9_dec_finalize (GObject * object);
131 static void gst_d3d11_vp9_dec_set_context (GstElement * element,
132     GstContext * context);
133 
134 static gboolean gst_d3d11_vp9_dec_open (GstVideoDecoder * decoder);
135 static gboolean gst_d3d11_vp9_dec_close (GstVideoDecoder * decoder);
136 static gboolean gst_d3d11_vp9_dec_negotiate (GstVideoDecoder * decoder);
137 static gboolean gst_d3d11_vp9_dec_decide_allocation (GstVideoDecoder *
138     decoder, GstQuery * query);
139 static gboolean gst_d3d11_vp9_dec_src_query (GstVideoDecoder * decoder,
140     GstQuery * query);
141 static gboolean gst_d3d11_vp9_dec_sink_event (GstVideoDecoder * decoder,
142     GstEvent * event);
143 
144 /* GstVp9Decoder */
145 static GstFlowReturn gst_d3d11_vp9_dec_new_sequence (GstVp9Decoder * decoder,
146     const GstVp9FrameHeader * frame_hdr);
147 static GstFlowReturn gst_d3d11_vp9_dec_new_picture (GstVp9Decoder * decoder,
148     GstVideoCodecFrame * frame, GstVp9Picture * picture);
149 static GstVp9Picture *gst_d3d11_vp9_dec_duplicate_picture (GstVp9Decoder *
150     decoder, GstVideoCodecFrame * frame, GstVp9Picture * picture);
151 static GstFlowReturn gst_d3d11_vp9_dec_start_picture (GstVp9Decoder * decoder,
152     GstVp9Picture * picture);
153 static GstFlowReturn gst_d3d11_vp9_dec_decode_picture (GstVp9Decoder * decoder,
154     GstVp9Picture * picture, GstVp9Dpb * dpb);
155 static GstFlowReturn gst_d3d11_vp9_dec_end_picture (GstVp9Decoder * decoder,
156     GstVp9Picture * picture);
157 static GstFlowReturn gst_d3d11_vp9_dec_output_picture (GstVp9Decoder *
158     decoder, GstVideoCodecFrame * frame, GstVp9Picture * picture);
159 
160 static void
gst_d3d11_vp9_dec_class_init(GstD3D11Vp9DecClass * klass,gpointer data)161 gst_d3d11_vp9_dec_class_init (GstD3D11Vp9DecClass * klass, gpointer data)
162 {
163   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
166   GstVp9DecoderClass *vp9decoder_class = GST_VP9_DECODER_CLASS (klass);
167   GstD3D11DecoderClassData *cdata = (GstD3D11DecoderClassData *) data;
168 
169   gobject_class->get_property = gst_d3d11_vp9_dec_get_property;
170   gobject_class->finalize = gst_d3d11_vp9_dec_finalize;
171 
172   element_class->set_context =
173       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_set_context);
174 
175   parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
176   gst_d3d11_decoder_class_data_fill_subclass_data (cdata, &klass->class_data);
177 
178   /**
179    * GstD3D11Vp9Dec:adapter-luid:
180    *
181    * DXGI Adapter LUID for this element
182    *
183    * Since: 1.20
184    */
185 
186   gst_d3d11_decoder_proxy_class_init (element_class, cdata,
187       "Seungha Yang <seungha.yang@navercorp.com>");
188 
189   decoder_class->open = GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_open);
190   decoder_class->close = GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_close);
191   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_negotiate);
192   decoder_class->decide_allocation =
193       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_decide_allocation);
194   decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_src_query);
195   decoder_class->sink_event = GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_sink_event);
196 
197   vp9decoder_class->new_sequence =
198       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_new_sequence);
199   vp9decoder_class->new_picture =
200       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_new_picture);
201   vp9decoder_class->duplicate_picture =
202       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_duplicate_picture);
203   vp9decoder_class->start_picture =
204       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_start_picture);
205   vp9decoder_class->decode_picture =
206       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_decode_picture);
207   vp9decoder_class->end_picture =
208       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_end_picture);
209   vp9decoder_class->output_picture =
210       GST_DEBUG_FUNCPTR (gst_d3d11_vp9_dec_output_picture);
211 }
212 
213 static void
gst_d3d11_vp9_dec_init(GstD3D11Vp9Dec * self)214 gst_d3d11_vp9_dec_init (GstD3D11Vp9Dec * self)
215 {
216   self->inner = new GstD3D11Vp9DecInner ();
217 }
218 
219 static void
gst_d3d11_vp9_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)220 gst_d3d11_vp9_dec_get_property (GObject * object, guint prop_id,
221     GValue * value, GParamSpec * pspec)
222 {
223   GstD3D11Vp9DecClass *klass = GST_D3D11_VP9_DEC_GET_CLASS (object);
224   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
225 
226   gst_d3d11_decoder_proxy_get_property (object, prop_id, value, pspec, cdata);
227 }
228 
229 static void
gst_d3d11_vp9_dec_finalize(GObject * object)230 gst_d3d11_vp9_dec_finalize (GObject * object)
231 {
232   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (object);
233 
234   delete self->inner;
235 
236   G_OBJECT_CLASS (parent_class)->finalize (object);
237 }
238 
239 static void
gst_d3d11_vp9_dec_set_context(GstElement * element,GstContext * context)240 gst_d3d11_vp9_dec_set_context (GstElement * element, GstContext * context)
241 {
242   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (element);
243   GstD3D11Vp9DecInner *inner = self->inner;
244   GstD3D11Vp9DecClass *klass = GST_D3D11_VP9_DEC_GET_CLASS (self);
245   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
246 
247   gst_d3d11_handle_set_context_for_adapter_luid (element,
248       context, cdata->adapter_luid, &inner->device);
249 
250   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
251 }
252 
253 static gboolean
gst_d3d11_vp9_dec_open(GstVideoDecoder * decoder)254 gst_d3d11_vp9_dec_open (GstVideoDecoder * decoder)
255 {
256   GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
257   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
258   GstD3D11Vp9DecInner *inner = self->inner;
259   GstD3D11Vp9DecClass *klass = GST_D3D11_VP9_DEC_GET_CLASS (self);
260   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
261 
262   if (!gst_d3d11_decoder_proxy_open (decoder,
263           cdata, &inner->device, &inner->d3d11_decoder)) {
264     GST_ERROR_OBJECT (self, "Failed to open decoder");
265     return FALSE;
266   }
267 
268   /* XXX: ConfigDecoderSpecific bit 12 indicates whether accelerator can
269    * support non-keyframe format change or not, but it doesn't seem to be
270    * reliable, since 1b means that it's supported and 0b indicates it may not be
271    * supported. Because some GPUs can support it even if the bit 12 is not
272    * set, do filtering by vendor for now (AMD and Intel looks fine) */
273   if (gst_d3d11_get_device_vendor (inner->device) ==
274       GST_D3D11_DEVICE_VENDOR_NVIDIA) {
275     gst_vp9_decoder_set_non_keyframe_format_change_support (vp9dec, FALSE);
276   }
277 
278   return TRUE;
279 }
280 
281 static gboolean
gst_d3d11_vp9_dec_close(GstVideoDecoder * decoder)282 gst_d3d11_vp9_dec_close (GstVideoDecoder * decoder)
283 {
284   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
285   GstD3D11Vp9DecInner *inner = self->inner;
286 
287   gst_clear_object (&inner->d3d11_decoder);
288   gst_clear_object (&inner->device);
289 
290   return TRUE;
291 }
292 
293 static gboolean
gst_d3d11_vp9_dec_negotiate(GstVideoDecoder * decoder)294 gst_d3d11_vp9_dec_negotiate (GstVideoDecoder * decoder)
295 {
296   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
297   GstD3D11Vp9DecInner *inner = self->inner;
298 
299   if (!gst_d3d11_decoder_negotiate (inner->d3d11_decoder, decoder))
300     return FALSE;
301 
302   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
303 }
304 
305 static gboolean
gst_d3d11_vp9_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)306 gst_d3d11_vp9_dec_decide_allocation (GstVideoDecoder * decoder,
307     GstQuery * query)
308 {
309   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
310   GstD3D11Vp9DecInner *inner = self->inner;
311 
312   if (!gst_d3d11_decoder_decide_allocation (inner->d3d11_decoder,
313           decoder, query)) {
314     return FALSE;
315   }
316 
317   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
318       (decoder, query);
319 }
320 
321 static gboolean
gst_d3d11_vp9_dec_src_query(GstVideoDecoder * decoder,GstQuery * query)322 gst_d3d11_vp9_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
323 {
324   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
325   GstD3D11Vp9DecInner *inner = self->inner;
326 
327   switch (GST_QUERY_TYPE (query)) {
328     case GST_QUERY_CONTEXT:
329       if (gst_d3d11_handle_context_query (GST_ELEMENT (decoder),
330               query, inner->device)) {
331         return TRUE;
332       }
333       break;
334     default:
335       break;
336   }
337 
338   return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
339 }
340 
341 static gboolean
gst_d3d11_vp9_dec_sink_event(GstVideoDecoder * decoder,GstEvent * event)342 gst_d3d11_vp9_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
343 {
344   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
345   GstD3D11Vp9DecInner *inner = self->inner;
346 
347   switch (GST_EVENT_TYPE (event)) {
348     case GST_EVENT_FLUSH_START:
349       if (inner->d3d11_decoder)
350         gst_d3d11_decoder_set_flushing (inner->d3d11_decoder, decoder, TRUE);
351       break;
352     case GST_EVENT_FLUSH_STOP:
353       if (inner->d3d11_decoder)
354         gst_d3d11_decoder_set_flushing (inner->d3d11_decoder, decoder, FALSE);
355     default:
356       break;
357   }
358 
359   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
360 }
361 
362 static GstFlowReturn
gst_d3d11_vp9_dec_new_sequence(GstVp9Decoder * decoder,const GstVp9FrameHeader * frame_hdr)363 gst_d3d11_vp9_dec_new_sequence (GstVp9Decoder * decoder,
364     const GstVp9FrameHeader * frame_hdr)
365 {
366   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
367   GstD3D11Vp9DecInner *inner = self->inner;
368   GstVideoInfo info;
369   GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
370 
371   GST_LOG_OBJECT (self, "new sequence");
372 
373   if (frame_hdr->profile == GST_VP9_PROFILE_0)
374     out_format = GST_VIDEO_FORMAT_NV12;
375   else if (frame_hdr->profile == GST_VP9_PROFILE_2)
376     out_format = GST_VIDEO_FORMAT_P010_10LE;
377 
378   if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
379     GST_ERROR_OBJECT (self, "Could not support profile %d", frame_hdr->profile);
380     return GST_FLOW_NOT_NEGOTIATED;
381   }
382 
383   gst_video_info_set_format (&info,
384       out_format, frame_hdr->width, frame_hdr->height);
385 
386   if (!gst_d3d11_decoder_configure (inner->d3d11_decoder,
387           decoder->input_state, &info, (gint) frame_hdr->width,
388           (gint) frame_hdr->height, NUM_OUTPUT_VIEW)) {
389     GST_ERROR_OBJECT (self, "Failed to create decoder");
390     return GST_FLOW_NOT_NEGOTIATED;
391   }
392 
393   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
394     GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
395     return GST_FLOW_NOT_NEGOTIATED;
396   }
397 
398   /* Will be updated per decode_picture */
399   inner->last_frame_width = inner->last_frame_height = 0;
400   inner->last_show_frame = FALSE;
401 
402   return GST_FLOW_OK;
403 }
404 
405 static GstFlowReturn
gst_d3d11_vp9_dec_new_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)406 gst_d3d11_vp9_dec_new_picture (GstVp9Decoder * decoder,
407     GstVideoCodecFrame * frame, GstVp9Picture * picture)
408 {
409   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
410   GstD3D11Vp9DecInner *inner = self->inner;
411   GstBuffer *view_buffer;
412 
413   view_buffer = gst_d3d11_decoder_get_output_view_buffer (inner->d3d11_decoder,
414       GST_VIDEO_DECODER (decoder));
415   if (!view_buffer) {
416     GST_DEBUG_OBJECT (self, "No available output view buffer");
417     return GST_FLOW_FLUSHING;
418   }
419 
420   GST_LOG_OBJECT (self, "New output view buffer %" GST_PTR_FORMAT, view_buffer);
421 
422   gst_vp9_picture_set_user_data (picture,
423       view_buffer, (GDestroyNotify) gst_buffer_unref);
424 
425   GST_LOG_OBJECT (self, "New VP9 picture %p", picture);
426 
427   return GST_FLOW_OK;
428 }
429 
430 static GstVp9Picture *
gst_d3d11_vp9_dec_duplicate_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)431 gst_d3d11_vp9_dec_duplicate_picture (GstVp9Decoder * decoder,
432     GstVideoCodecFrame * frame, GstVp9Picture * picture)
433 {
434   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
435   GstBuffer *view_buffer;
436   GstVp9Picture *new_picture;
437 
438   view_buffer = (GstBuffer *) gst_vp9_picture_get_user_data (picture);
439 
440   if (!view_buffer) {
441     GST_ERROR_OBJECT (self, "Parent picture does not have output view buffer");
442     return NULL;
443   }
444 
445   new_picture = gst_vp9_picture_new ();
446   new_picture->frame_hdr = picture->frame_hdr;
447 
448   GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT,
449       view_buffer);
450 
451   gst_vp9_picture_set_user_data (new_picture,
452       gst_buffer_ref (view_buffer), (GDestroyNotify) gst_buffer_unref);
453 
454   return new_picture;
455 }
456 
457 static GstFlowReturn
gst_d3d11_vp9_dec_start_picture(GstVp9Decoder * decoder,GstVp9Picture * picture)458 gst_d3d11_vp9_dec_start_picture (GstVp9Decoder * decoder,
459     GstVp9Picture * picture)
460 {
461   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
462   GstD3D11Vp9DecInner *inner = self->inner;
463 
464   inner->bitstream_buffer.resize (0);
465 
466   return GST_FLOW_OK;
467 }
468 
469 static ID3D11VideoDecoderOutputView *
gst_d3d11_vp9_dec_get_output_view_from_picture(GstD3D11Vp9Dec * self,GstVp9Picture * picture,guint8 * view_id)470 gst_d3d11_vp9_dec_get_output_view_from_picture (GstD3D11Vp9Dec * self,
471     GstVp9Picture * picture, guint8 * view_id)
472 {
473   GstD3D11Vp9DecInner *inner = self->inner;
474   GstBuffer *view_buffer;
475   ID3D11VideoDecoderOutputView *view;
476 
477   view_buffer = (GstBuffer *) gst_vp9_picture_get_user_data (picture);
478   if (!view_buffer) {
479     GST_DEBUG_OBJECT (self, "current picture does not have output view buffer");
480     return NULL;
481   }
482 
483   view =
484       gst_d3d11_decoder_get_output_view_from_buffer (inner->d3d11_decoder,
485       view_buffer, view_id);
486   if (!view) {
487     GST_DEBUG_OBJECT (self, "current picture does not have output view handle");
488     return NULL;
489   }
490 
491   return view;
492 }
493 
494 static void
gst_d3d11_vp9_dec_copy_frame_params(GstD3D11Vp9Dec * self,GstVp9Picture * picture,DXVA_PicParams_VP9 * params)495 gst_d3d11_vp9_dec_copy_frame_params (GstD3D11Vp9Dec * self,
496     GstVp9Picture * picture, DXVA_PicParams_VP9 * params)
497 {
498   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
499 
500   params->profile = frame_hdr->profile;
501   params->frame_type = frame_hdr->frame_type;
502   params->show_frame = frame_hdr->show_frame;
503   params->error_resilient_mode = frame_hdr->error_resilient_mode;
504   params->subsampling_x = frame_hdr->subsampling_x;
505   params->subsampling_y = frame_hdr->subsampling_y;
506   params->refresh_frame_context = frame_hdr->refresh_frame_context;
507   params->frame_parallel_decoding_mode =
508       frame_hdr->frame_parallel_decoding_mode;
509   params->intra_only = frame_hdr->intra_only;
510   params->frame_context_idx = frame_hdr->frame_context_idx;
511   params->reset_frame_context = frame_hdr->reset_frame_context;
512   if (frame_hdr->frame_type == GST_VP9_KEY_FRAME)
513     params->allow_high_precision_mv = 0;
514   else
515     params->allow_high_precision_mv = frame_hdr->allow_high_precision_mv;
516 
517   params->width = frame_hdr->width;
518   params->height = frame_hdr->height;
519   params->BitDepthMinus8Luma = frame_hdr->bit_depth - 8;
520   params->BitDepthMinus8Chroma = frame_hdr->bit_depth - 8;
521 
522   params->interp_filter = frame_hdr->interpolation_filter;
523   params->log2_tile_cols = frame_hdr->tile_cols_log2;
524   params->log2_tile_rows = frame_hdr->tile_rows_log2;
525 }
526 
527 static void
gst_d3d11_vp9_dec_copy_reference_frames(GstD3D11Vp9Dec * self,GstVp9Picture * picture,GstVp9Dpb * dpb,DXVA_PicParams_VP9 * params)528 gst_d3d11_vp9_dec_copy_reference_frames (GstD3D11Vp9Dec * self,
529     GstVp9Picture * picture, GstVp9Dpb * dpb, DXVA_PicParams_VP9 * params)
530 {
531   gint i;
532 
533   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
534     if (dpb->pic_list[i]) {
535       GstVp9Picture *other_pic = dpb->pic_list[i];
536       ID3D11VideoDecoderOutputView *view;
537       guint8 view_id = 0xff;
538 
539       view = gst_d3d11_vp9_dec_get_output_view_from_picture (self, other_pic,
540           &view_id);
541       if (!view) {
542         GST_ERROR_OBJECT (self, "picture does not have output view handle");
543         return;
544       }
545 
546       params->ref_frame_map[i].Index7Bits = view_id;
547       params->ref_frame_coded_width[i] = picture->frame_hdr.width;
548       params->ref_frame_coded_height[i] = picture->frame_hdr.height;
549     } else {
550       params->ref_frame_map[i].bPicEntry = 0xff;
551       params->ref_frame_coded_width[i] = 0;
552       params->ref_frame_coded_height[i] = 0;
553     }
554   }
555 }
556 
557 static void
gst_d3d11_vp9_dec_copy_frame_refs(GstD3D11Vp9Dec * self,GstVp9Picture * picture,DXVA_PicParams_VP9 * params)558 gst_d3d11_vp9_dec_copy_frame_refs (GstD3D11Vp9Dec * self,
559     GstVp9Picture * picture, DXVA_PicParams_VP9 * params)
560 {
561   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
562   gint i;
563 
564   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
565     params->frame_refs[i] = params->ref_frame_map[frame_hdr->ref_frame_idx[i]];
566   }
567 
568   G_STATIC_ASSERT (G_N_ELEMENTS (params->ref_frame_sign_bias) ==
569       G_N_ELEMENTS (frame_hdr->ref_frame_sign_bias));
570   G_STATIC_ASSERT (sizeof (params->ref_frame_sign_bias) ==
571       sizeof (frame_hdr->ref_frame_sign_bias));
572   memcpy (params->ref_frame_sign_bias,
573       frame_hdr->ref_frame_sign_bias, sizeof (frame_hdr->ref_frame_sign_bias));
574 }
575 
576 static void
gst_d3d11_vp9_dec_copy_loop_filter_params(GstD3D11Vp9Dec * self,GstVp9Picture * picture,DXVA_PicParams_VP9 * params)577 gst_d3d11_vp9_dec_copy_loop_filter_params (GstD3D11Vp9Dec * self,
578     GstVp9Picture * picture, DXVA_PicParams_VP9 * params)
579 {
580   GstD3D11Vp9DecInner *inner = self->inner;
581   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
582   const GstVp9LoopFilterParams *lfp = &frame_hdr->loop_filter_params;
583 
584   params->filter_level = lfp->loop_filter_level;
585   params->sharpness_level = lfp->loop_filter_sharpness;
586   params->mode_ref_delta_enabled = lfp->loop_filter_delta_enabled;
587   params->mode_ref_delta_update = lfp->loop_filter_delta_update;
588   params->use_prev_in_find_mv_refs =
589       inner->last_show_frame && !frame_hdr->error_resilient_mode;
590 
591   if (frame_hdr->frame_type != GST_VP9_KEY_FRAME && !frame_hdr->intra_only) {
592     params->use_prev_in_find_mv_refs &=
593         (frame_hdr->width == inner->last_frame_width &&
594         frame_hdr->height == inner->last_frame_height);
595   }
596 
597   G_STATIC_ASSERT (G_N_ELEMENTS (params->ref_deltas) ==
598       G_N_ELEMENTS (lfp->loop_filter_ref_deltas));
599   G_STATIC_ASSERT (sizeof (params->ref_deltas) ==
600       sizeof (lfp->loop_filter_ref_deltas));
601   memcpy (params->ref_deltas, lfp->loop_filter_ref_deltas,
602       sizeof (lfp->loop_filter_ref_deltas));
603 
604   G_STATIC_ASSERT (G_N_ELEMENTS (params->mode_deltas) ==
605       G_N_ELEMENTS (lfp->loop_filter_mode_deltas));
606   G_STATIC_ASSERT (sizeof (params->mode_deltas) ==
607       sizeof (lfp->loop_filter_mode_deltas));
608   memcpy (params->mode_deltas, lfp->loop_filter_mode_deltas,
609       sizeof (lfp->loop_filter_mode_deltas));
610 }
611 
612 static void
gst_d3d11_vp9_dec_copy_quant_params(GstD3D11Vp9Dec * self,GstVp9Picture * picture,DXVA_PicParams_VP9 * params)613 gst_d3d11_vp9_dec_copy_quant_params (GstD3D11Vp9Dec * self,
614     GstVp9Picture * picture, DXVA_PicParams_VP9 * params)
615 {
616   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
617   const GstVp9QuantizationParams *qp = &frame_hdr->quantization_params;
618 
619   params->base_qindex = qp->base_q_idx;
620   params->y_dc_delta_q = qp->delta_q_y_dc;
621   params->uv_dc_delta_q = qp->delta_q_uv_dc;
622   params->uv_ac_delta_q = qp->delta_q_uv_ac;
623 }
624 
625 static void
gst_d3d11_vp9_dec_copy_segmentation_params(GstD3D11Vp9Dec * self,GstVp9Picture * picture,DXVA_PicParams_VP9 * params)626 gst_d3d11_vp9_dec_copy_segmentation_params (GstD3D11Vp9Dec * self,
627     GstVp9Picture * picture, DXVA_PicParams_VP9 * params)
628 {
629   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
630   const GstVp9SegmentationParams *sp = &frame_hdr->segmentation_params;
631   gint i, j;
632 
633   params->stVP9Segments.enabled = sp->segmentation_enabled;
634   params->stVP9Segments.update_map = sp->segmentation_update_map;
635   params->stVP9Segments.temporal_update = sp->segmentation_temporal_update;
636   params->stVP9Segments.abs_delta = sp->segmentation_abs_or_delta_update;
637 
638   G_STATIC_ASSERT (G_N_ELEMENTS (params->stVP9Segments.tree_probs) ==
639       G_N_ELEMENTS (sp->segmentation_tree_probs));
640   G_STATIC_ASSERT (sizeof (params->stVP9Segments.tree_probs) ==
641       sizeof (sp->segmentation_tree_probs));
642   memcpy (params->stVP9Segments.tree_probs, sp->segmentation_tree_probs,
643       sizeof (sp->segmentation_tree_probs));
644 
645   G_STATIC_ASSERT (G_N_ELEMENTS (params->stVP9Segments.pred_probs) ==
646       G_N_ELEMENTS (sp->segmentation_pred_prob));
647   G_STATIC_ASSERT (sizeof (params->stVP9Segments.pred_probs) ==
648       sizeof (sp->segmentation_pred_prob));
649 
650   if (sp->segmentation_temporal_update) {
651     memcpy (params->stVP9Segments.pred_probs, sp->segmentation_pred_prob,
652         sizeof (params->stVP9Segments.pred_probs));
653   } else {
654     memset (params->stVP9Segments.pred_probs, 255,
655         sizeof (params->stVP9Segments.pred_probs));
656   }
657 
658   for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
659     params->stVP9Segments.feature_mask[i] =
660         (sp->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q] << 0) |
661         (sp->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L] << 1) |
662         (sp->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME] << 2) |
663         (sp->feature_enabled[i][GST_VP9_SEG_SEG_LVL_SKIP] << 3);
664 
665     for (j = 0; j < 3; j++)
666       params->stVP9Segments.feature_data[i][j] = sp->feature_data[i][j];
667     params->stVP9Segments.feature_data[i][3] = 0;
668   }
669 }
670 
671 static GstFlowReturn
gst_d3d11_vp9_dec_decode_picture(GstVp9Decoder * decoder,GstVp9Picture * picture,GstVp9Dpb * dpb)672 gst_d3d11_vp9_dec_decode_picture (GstVp9Decoder * decoder,
673     GstVp9Picture * picture, GstVp9Dpb * dpb)
674 {
675   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
676   GstD3D11Vp9DecInner *inner = self->inner;
677   DXVA_PicParams_VP9 *pic_params = &inner->pic_params;
678   DXVA_Slice_VPx_Short *slice = &inner->slice;
679   ID3D11VideoDecoderOutputView *view;
680   guint8 view_id = 0xff;
681 
682   view = gst_d3d11_vp9_dec_get_output_view_from_picture (self, picture,
683       &view_id);
684   if (!view) {
685     GST_ERROR_OBJECT (self, "current picture does not have output view handle");
686     return GST_FLOW_ERROR;
687   }
688 
689   memset (pic_params, 0, sizeof (DXVA_PicParams_VP9));
690 
691   pic_params->CurrPic.Index7Bits = view_id;
692   pic_params->uncompressed_header_size_byte_aligned =
693       picture->frame_hdr.frame_header_length_in_bytes;
694   pic_params->first_partition_size = picture->frame_hdr.header_size_in_bytes;
695   pic_params->StatusReportFeedbackNumber = 1;
696 
697   gst_d3d11_vp9_dec_copy_frame_params (self, picture, pic_params);
698   gst_d3d11_vp9_dec_copy_reference_frames (self, picture, dpb, pic_params);
699   gst_d3d11_vp9_dec_copy_frame_refs (self, picture, pic_params);
700   gst_d3d11_vp9_dec_copy_loop_filter_params (self, picture, pic_params);
701   gst_d3d11_vp9_dec_copy_quant_params (self, picture, pic_params);
702   gst_d3d11_vp9_dec_copy_segmentation_params (self, picture, pic_params);
703 
704   inner->bitstream_buffer.resize (picture->size);
705   memcpy (&inner->bitstream_buffer[0], picture->data, picture->size);
706 
707   slice->BSNALunitDataLocation = 0;
708   slice->SliceBytesInBuffer = inner->bitstream_buffer.size ();
709   slice->wBadSliceChopping = 0;
710 
711   inner->last_frame_width = pic_params->width;
712   inner->last_frame_height = pic_params->height;
713   inner->last_show_frame = pic_params->show_frame;
714 
715   return GST_FLOW_OK;
716 }
717 
718 static GstFlowReturn
gst_d3d11_vp9_dec_end_picture(GstVp9Decoder * decoder,GstVp9Picture * picture)719 gst_d3d11_vp9_dec_end_picture (GstVp9Decoder * decoder, GstVp9Picture * picture)
720 {
721   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
722   GstD3D11Vp9DecInner *inner = self->inner;
723   ID3D11VideoDecoderOutputView *view;
724   guint8 view_id = 0xff;
725   size_t bitstream_buffer_size;
726   size_t bitstream_pos;
727   GstD3D11DecodeInputStreamArgs input_args;
728 
729   if (inner->bitstream_buffer.empty ()) {
730     GST_ERROR_OBJECT (self, "No bitstream buffer to submit");
731     return GST_FLOW_ERROR;
732   }
733 
734   view = gst_d3d11_vp9_dec_get_output_view_from_picture (self,
735       picture, &view_id);
736   if (!view) {
737     GST_ERROR_OBJECT (self, "current picture does not have output view handle");
738     return GST_FLOW_ERROR;
739   }
740 
741   memset (&input_args, 0, sizeof (GstD3D11DecodeInputStreamArgs));
742 
743   bitstream_pos = inner->bitstream_buffer.size ();
744   bitstream_buffer_size = GST_ROUND_UP_128 (bitstream_pos);
745 
746   if (bitstream_buffer_size > bitstream_pos) {
747     size_t padding = bitstream_buffer_size - bitstream_pos;
748 
749     /* As per DXVA spec, total amount of bitstream buffer size should be
750      * 128 bytes aligned. If actual data is not multiple of 128 bytes,
751      * the last slice data needs to be zero-padded */
752     inner->bitstream_buffer.resize (bitstream_buffer_size, 0);
753 
754     inner->slice.SliceBytesInBuffer += padding;
755   }
756 
757   input_args.picture_params = &inner->pic_params;
758   input_args.picture_params_size = sizeof (DXVA_PicParams_VP9);
759   input_args.slice_control = &inner->slice;
760   input_args.slice_control_size = sizeof (DXVA_Slice_VPx_Short);
761   input_args.bitstream = &inner->bitstream_buffer[0];
762   input_args.bitstream_size = inner->bitstream_buffer.size ();
763 
764   if (!gst_d3d11_decoder_decode_frame (inner->d3d11_decoder, view, &input_args))
765     return GST_FLOW_ERROR;
766 
767   return GST_FLOW_OK;
768 }
769 
770 static GstFlowReturn
gst_d3d11_vp9_dec_output_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)771 gst_d3d11_vp9_dec_output_picture (GstVp9Decoder * decoder,
772     GstVideoCodecFrame * frame, GstVp9Picture * picture)
773 {
774   GstD3D11Vp9Dec *self = GST_D3D11_VP9_DEC (decoder);
775   GstD3D11Vp9DecInner *inner = self->inner;
776   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
777   GstBuffer *view_buffer;
778 
779   GST_LOG_OBJECT (self, "Outputting picture %p", picture);
780 
781   view_buffer = (GstBuffer *) gst_vp9_picture_get_user_data (picture);
782 
783   if (!view_buffer) {
784     GST_ERROR_OBJECT (self, "Could not get output view");
785     goto error;
786   }
787 
788   if (!gst_d3d11_decoder_process_output (inner->d3d11_decoder, vdec,
789           picture->frame_hdr.width, picture->frame_hdr.height, view_buffer,
790           &frame->output_buffer)) {
791     GST_ERROR_OBJECT (self, "Failed to copy buffer");
792     goto error;
793   }
794 
795   gst_vp9_picture_unref (picture);
796 
797   return gst_video_decoder_finish_frame (vdec, frame);
798 
799 error:
800   gst_vp9_picture_unref (picture);
801   gst_video_decoder_release_frame (vdec, frame);
802 
803   return GST_FLOW_ERROR;
804 }
805 
806 void
gst_d3d11_vp9_dec_register(GstPlugin * plugin,GstD3D11Device * device,guint rank)807 gst_d3d11_vp9_dec_register (GstPlugin * plugin, GstD3D11Device * device,
808     guint rank)
809 {
810   GType type;
811   gchar *type_name;
812   gchar *feature_name;
813   guint index = 0;
814   guint i;
815   const GUID *profile;
816   GTypeInfo type_info = {
817     sizeof (GstD3D11Vp9DecClass),
818     NULL,
819     NULL,
820     (GClassInitFunc) gst_d3d11_vp9_dec_class_init,
821     NULL,
822     NULL,
823     sizeof (GstD3D11Vp9Dec),
824     0,
825     (GInstanceInitFunc) gst_d3d11_vp9_dec_init,
826   };
827   const GUID *profile2_guid = NULL;
828   const GUID *profile0_guid = NULL;
829   GstCaps *sink_caps = NULL;
830   GstCaps *src_caps = NULL;
831   guint max_width = 0;
832   guint max_height = 0;
833   guint resolution;
834   gboolean have_profile2 = FALSE;
835   gboolean have_profile0 = FALSE;
836   DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
837   GValue vp9_profiles = G_VALUE_INIT;
838 
839   have_profile2 = gst_d3d11_decoder_get_supported_decoder_profile (device,
840       GST_DXVA_CODEC_VP9, GST_VIDEO_FORMAT_P010_10LE, &profile2_guid);
841   if (!have_profile2) {
842     GST_DEBUG_OBJECT (device,
843         "decoder does not support VP9_VLD_10BIT_PROFILE2");
844   } else {
845     have_profile2 &=
846         gst_d3d11_decoder_supports_format (device,
847         profile2_guid, DXGI_FORMAT_P010);
848     if (!have_profile2) {
849       GST_FIXME_OBJECT (device, "device does not support P010 format");
850     }
851   }
852 
853   have_profile0 = gst_d3d11_decoder_get_supported_decoder_profile (device,
854       GST_DXVA_CODEC_VP9, GST_VIDEO_FORMAT_NV12, &profile0_guid);
855   if (!have_profile0) {
856     GST_DEBUG_OBJECT (device, "decoder does not support VP9_VLD_PROFILE0");
857   } else {
858     have_profile0 =
859         gst_d3d11_decoder_supports_format (device, profile0_guid,
860         DXGI_FORMAT_NV12);
861     if (!have_profile0) {
862       GST_FIXME_OBJECT (device, "device does not support NV12 format");
863     }
864   }
865 
866   if (!have_profile2 && !have_profile0) {
867     GST_INFO_OBJECT (device, "device does not support VP9 decoding");
868     return;
869   }
870 
871   if (have_profile0) {
872     profile = profile0_guid;
873     format = DXGI_FORMAT_NV12;
874   } else {
875     profile = profile2_guid;
876     format = DXGI_FORMAT_P010;
877   }
878 
879   for (i = 0; i < G_N_ELEMENTS (gst_dxva_resolutions); i++) {
880     if (gst_d3d11_decoder_supports_resolution (device, profile,
881             format, gst_dxva_resolutions[i].width,
882             gst_dxva_resolutions[i].height)) {
883       max_width = gst_dxva_resolutions[i].width;
884       max_height = gst_dxva_resolutions[i].height;
885 
886       GST_DEBUG_OBJECT (device,
887           "device support resolution %dx%d", max_width, max_height);
888     } else {
889       break;
890     }
891   }
892 
893   if (max_width == 0 || max_height == 0) {
894     GST_WARNING_OBJECT (device, "Couldn't query supported resolution");
895     return;
896   }
897 
898   sink_caps = gst_caps_from_string ("video/x-vp9, alignment = (string) frame");
899   src_caps = gst_caps_from_string ("video/x-raw("
900       GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY "); video/x-raw");
901 
902   g_value_init (&vp9_profiles, GST_TYPE_LIST);
903 
904   if (have_profile0) {
905     GValue vp9_profile_val = G_VALUE_INIT;
906 
907     g_value_init (&vp9_profile_val, G_TYPE_STRING);
908     g_value_set_string (&vp9_profile_val, "0");
909     gst_value_list_append_and_take_value (&vp9_profiles, &vp9_profile_val);
910   }
911 
912   if (have_profile2) {
913     GValue format_list = G_VALUE_INIT;
914     GValue format_value = G_VALUE_INIT;
915     GValue vp9_profile_val = G_VALUE_INIT;
916 
917     g_value_init (&format_list, GST_TYPE_LIST);
918 
919     g_value_init (&format_value, G_TYPE_STRING);
920     g_value_set_string (&format_value, "NV12");
921     gst_value_list_append_and_take_value (&format_list, &format_value);
922 
923     g_value_init (&format_value, G_TYPE_STRING);
924     g_value_set_string (&format_value, "P010_10LE");
925     gst_value_list_append_and_take_value (&format_list, &format_value);
926 
927     gst_caps_set_value (src_caps, "format", &format_list);
928     g_value_unset (&format_list);
929 
930     g_value_init (&vp9_profile_val, G_TYPE_STRING);
931     g_value_set_string (&vp9_profile_val, "2");
932     gst_value_list_append_and_take_value (&vp9_profiles, &vp9_profile_val);
933   } else {
934     gst_caps_set_simple (src_caps, "format", G_TYPE_STRING, "NV12", NULL);
935   }
936 
937   gst_caps_set_value (sink_caps, "profile", &vp9_profiles);
938   g_value_unset (&vp9_profiles);
939 
940   /* To cover both landscape and portrait, select max value */
941   resolution = MAX (max_width, max_height);
942   gst_caps_set_simple (sink_caps,
943       "width", GST_TYPE_INT_RANGE, 1, resolution,
944       "height", GST_TYPE_INT_RANGE, 1, resolution, NULL);
945   gst_caps_set_simple (src_caps,
946       "width", GST_TYPE_INT_RANGE, 1, resolution,
947       "height", GST_TYPE_INT_RANGE, 1, resolution, NULL);
948 
949   type_info.class_data =
950       gst_d3d11_decoder_class_data_new (device, GST_DXVA_CODEC_VP9,
951       sink_caps, src_caps);
952 
953   type_name = g_strdup ("GstD3D11Vp9Dec");
954   feature_name = g_strdup ("d3d11vp9dec");
955 
956   while (g_type_from_name (type_name)) {
957     index++;
958     g_free (type_name);
959     g_free (feature_name);
960     type_name = g_strdup_printf ("GstD3D11Vp9Device%dDec", index);
961     feature_name = g_strdup_printf ("d3d11vp9device%ddec", index);
962   }
963 
964   type = g_type_register_static (GST_TYPE_VP9_DECODER,
965       type_name, &type_info, (GTypeFlags) 0);
966 
967   /* make lower rank than default device */
968   if (rank > 0 && index != 0)
969     rank--;
970 
971   if (index != 0)
972     gst_element_type_set_skip_documentation (type);
973 
974   if (!gst_element_register (plugin, feature_name, rank, type))
975     GST_WARNING ("Failed to register plugin '%s'", type_name);
976 
977   g_free (type_name);
978   g_free (feature_name);
979 }
980