gstmsdkdec.c 32.4 KB
Newer Older
Scott D Phillips's avatar
Scott D Phillips committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* GStreamer Intel MSDK plugin
 * Copyright (c) 2016, Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h>

#include "gstmsdkdec.h"
Hyunjun Ko's avatar
Hyunjun Ko committed
39
40
41
#include "gstmsdkbufferpool.h"
#include "gstmsdkvideomemory.h"
#include "gstmsdksystemmemory.h"
42
#include "gstmsdkcontextutil.h"
Scott D Phillips's avatar
Scott D Phillips committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

GST_DEBUG_CATEGORY_EXTERN (gst_msdkdec_debug);
#define GST_CAT_DEFAULT gst_msdkdec_debug

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw, "
        "format = (string) { NV12 }, "
        "framerate = (fraction) [0, MAX], "
        "width = (int) [ 16, MAX ], height = (int) [ 16, MAX ],"
        "interlace-mode = (string) progressive")
    );

enum
{
  PROP_0,
  PROP_HARDWARE,
  PROP_ASYNC_DEPTH,
};

#define PROP_HARDWARE_DEFAULT            TRUE
#define PROP_ASYNC_DEPTH_DEFAULT         4

#define gst_msdkdec_parent_class parent_class
G_DEFINE_TYPE (GstMsdkDec, gst_msdkdec, GST_TYPE_VIDEO_DECODER);

typedef struct _MsdkSurface
{
Hyunjun Ko's avatar
Hyunjun Ko committed
72
73
  mfxFrameSurface1 *surface;
  GstBuffer *buf;
Scott D Phillips's avatar
Scott D Phillips committed
74
75
76
77
  GstVideoFrame data;
  GstVideoFrame copy;
} MsdkSurface;

78
79
static gboolean gst_msdkdec_flush (GstVideoDecoder * decoder);

80
81
82
83
84
85
86
87
88
89
90
91
92
93
static GstFlowReturn
allocate_output_buffer (GstMsdkDec * thiz, GstBuffer ** buffer)
{
  GstFlowReturn flow;
  GstVideoCodecFrame *frame;
  GstVideoDecoder *decoder = GST_VIDEO_DECODER (thiz);

  frame = gst_video_decoder_get_oldest_frame (decoder);
  if (!frame) {
    if (GST_PAD_IS_FLUSHING (decoder->srcpad))
      return GST_FLOW_FLUSHING;
    else
      return GST_FLOW_ERROR;
  }
Hyunjun Ko's avatar
Hyunjun Ko committed
94

95
96
97
98
99
100
101
  if (!frame->output_buffer) {
    flow = gst_video_decoder_allocate_output_frame (decoder, frame);
    if (flow != GST_FLOW_OK) {
      gst_video_codec_frame_unref (frame);
      return flow;
    }
  }
Hyunjun Ko's avatar
Hyunjun Ko committed
102

103
104
105
106
107
108
  *buffer = gst_buffer_ref (frame->output_buffer);
  gst_buffer_replace (&frame->output_buffer, NULL);
  gst_video_codec_frame_unref (frame);
  return GST_FLOW_OK;
}

Scott D Phillips's avatar
Scott D Phillips committed
109
static void
Hyunjun Ko's avatar
Hyunjun Ko committed
110
free_surface (GstMsdkDec * thiz, MsdkSurface * s)
Scott D Phillips's avatar
Scott D Phillips committed
111
112
113
114
115
116
{
  if (s->copy.buffer) {
    gst_video_frame_unmap (&s->copy);
    gst_buffer_unref (s->copy.buffer);
  }

Hyunjun Ko's avatar
Hyunjun Ko committed
117
  if (s->data.buffer)
Scott D Phillips's avatar
Scott D Phillips committed
118
119
    gst_video_frame_unmap (&s->data);

Hyunjun Ko's avatar
Hyunjun Ko committed
120
121
122
123
  gst_buffer_unref (s->buf);

  g_slice_free (MsdkSurface, s);
  thiz->decoded_msdk_surfaces = g_list_remove (thiz->decoded_msdk_surfaces, s);
124
125
}

Scott D Phillips's avatar
Scott D Phillips committed
126
static MsdkSurface *
127
get_surface (GstMsdkDec * thiz, GstBuffer * buffer)
Scott D Phillips's avatar
Scott D Phillips committed
128
129
130
{
  MsdkSurface *i;

Hyunjun Ko's avatar
Hyunjun Ko committed
131
  i = g_slice_new0 (MsdkSurface);
Scott D Phillips's avatar
Scott D Phillips committed
132

Hyunjun Ko's avatar
Hyunjun Ko committed
133
134
135
  if (gst_msdk_is_msdk_buffer (buffer)) {
    i->surface = gst_msdk_get_surface_from_buffer (buffer);
    i->buf = buffer;
Scott D Phillips's avatar
Scott D Phillips committed
136
  } else {
Hyunjun Ko's avatar
Hyunjun Ko committed
137
138
139
140
141
142
143
    /* Confirm to activate the side pool */
    if (!gst_buffer_pool_is_active (thiz->pool) &&
        !gst_buffer_pool_set_active (thiz->pool, TRUE)) {
      g_slice_free (MsdkSurface, i);
      return NULL;
    }

Scott D Phillips's avatar
Scott D Phillips committed
144
145
146
    if (!gst_video_frame_map (&i->copy, &thiz->output_info, buffer,
            GST_MAP_WRITE))
      goto failed_unref_buffer;
Hyunjun Ko's avatar
Hyunjun Ko committed
147

Scott D Phillips's avatar
Scott D Phillips committed
148
149
150
    if (gst_buffer_pool_acquire_buffer (thiz->pool, &buffer,
            NULL) != GST_FLOW_OK)
      goto failed_unmap_copy;
Hyunjun Ko's avatar
Hyunjun Ko committed
151

152
153
    i->surface = gst_msdk_get_surface_from_buffer (buffer);
    i->buf = buffer;
Hyunjun Ko's avatar
Hyunjun Ko committed
154

Scott D Phillips's avatar
Scott D Phillips committed
155
156
157
158
159
    if (!gst_video_frame_map (&i->data, &thiz->pool_info, buffer,
            GST_MAP_READWRITE))
      goto failed_unref_buffer2;
  }

Hyunjun Ko's avatar
Hyunjun Ko committed
160
  thiz->decoded_msdk_surfaces = g_list_append (thiz->decoded_msdk_surfaces, i);
Scott D Phillips's avatar
Scott D Phillips committed
161
162
163
164
165
166
167
168
169
  return i;

failed_unref_buffer2:
  gst_buffer_unref (buffer);
  buffer = i->data.buffer;
failed_unmap_copy:
  gst_video_frame_unmap (&i->copy);
failed_unref_buffer:
  gst_buffer_unref (buffer);
Hyunjun Ko's avatar
Hyunjun Ko committed
170
  g_slice_free (MsdkSurface, i);
Scott D Phillips's avatar
Scott D Phillips committed
171

Hyunjun Ko's avatar
Hyunjun Ko committed
172
  GST_ERROR_OBJECT (thiz, "failed to handle buffer");
Scott D Phillips's avatar
Scott D Phillips committed
173
174
175
176
177
178
179
180
  return NULL;
}

static void
gst_msdkdec_close_decoder (GstMsdkDec * thiz)
{
  mfxStatus status;

181
  if (!thiz->context || !thiz->initialized)
Scott D Phillips's avatar
Scott D Phillips committed
182
183
    return;

184
185
  GST_DEBUG_OBJECT (thiz, "Closing decoder with context %" GST_PTR_FORMAT,
      thiz->context);
Scott D Phillips's avatar
Scott D Phillips committed
186

Hyunjun Ko's avatar
Hyunjun Ko committed
187
188
189
  if (thiz->use_video_memory)
    gst_msdk_frame_free (thiz->context, &thiz->alloc_resp);

Hyunjun Ko's avatar
Hyunjun Ko committed
190
  status = MFXVideoDECODE_Close (gst_msdk_context_get_session (thiz->context));
Scott D Phillips's avatar
Scott D Phillips committed
191
192
193
194
195
196
197
198
199
  if (status != MFX_ERR_NONE && status != MFX_ERR_NOT_INITIALIZED) {
    GST_WARNING_OBJECT (thiz, "Decoder close failed (%s)",
        msdk_status_to_string (status));
  }

  g_array_set_size (thiz->tasks, 0);
  g_ptr_array_set_size (thiz->extra_params, 0);

  memset (&thiz->param, 0, sizeof (thiz->param));
200
  thiz->initialized = FALSE;
201
  gst_adapter_clear (thiz->adapter);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}

static void
gst_msdkdec_set_context (GstElement * element, GstContext * context)
{
  GstMsdkContext *msdk_context = NULL;
  GstMsdkDec *thiz = GST_MSDKDEC (element);

  if (gst_msdk_context_get_context (context, &msdk_context)) {
    gst_object_replace ((GstObject **) & thiz->context,
        (GstObject *) msdk_context);
    gst_object_unref (msdk_context);
  }

  GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
Scott D Phillips's avatar
Scott D Phillips committed
217
218
219
220
221
222
223
224
225
226
227
}

static gboolean
gst_msdkdec_init_decoder (GstMsdkDec * thiz)
{
  GstMsdkDecClass *klass = GST_MSDKDEC_GET_CLASS (thiz);
  GstVideoInfo *info;
  mfxSession session;
  mfxStatus status;
  mfxFrameAllocRequest request;

228
229
230
231
232
  if (thiz->initialized)
    return TRUE;

  if (!thiz->context) {
    GST_WARNING_OBJECT (thiz, "No MSDK Context");
Scott D Phillips's avatar
Scott D Phillips committed
233
234
235
    return FALSE;
  }

236
237
  if (!thiz->input_state) {
    GST_DEBUG_OBJECT (thiz, "Have no input state yet");
Scott D Phillips's avatar
Scott D Phillips committed
238
239
    return FALSE;
  }
240
  info = &thiz->input_state->info;
Scott D Phillips's avatar
Scott D Phillips committed
241
242
243

  GST_OBJECT_LOCK (thiz);

Hyunjun Ko's avatar
Hyunjun Ko committed
244
245
246
247
248
249
  if (thiz->use_video_memory) {
    gst_msdk_set_frame_allocator (thiz->context);
    thiz->param.IOPattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  } else {
    thiz->param.IOPattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  }
Scott D Phillips's avatar
Scott D Phillips committed
250

Hyunjun Ko's avatar
Hyunjun Ko committed
251
252
253
254
  GST_INFO_OBJECT (thiz, "This MSDK decoder uses %s memory",
      thiz->use_video_memory ? "video" : "system");

  thiz->param.AsyncDepth = thiz->async_depth;
Scott D Phillips's avatar
Scott D Phillips committed
255
256
257
258
  thiz->param.mfx.FrameInfo.Width = GST_ROUND_UP_32 (info->width);
  thiz->param.mfx.FrameInfo.Height = GST_ROUND_UP_32 (info->height);
  thiz->param.mfx.FrameInfo.CropW = info->width;
  thiz->param.mfx.FrameInfo.CropH = info->height;
259
260
261
262
263
264
265

  /* Set framerate only if provided.
   * If not, framerate will be assumed inside the driver */
  if (info->fps_n > 0 && info->fps_d > 0) {
    thiz->param.mfx.FrameInfo.FrameRateExtN = info->fps_n;
    thiz->param.mfx.FrameInfo.FrameRateExtD = info->fps_d;
  }
Scott D Phillips's avatar
Scott D Phillips committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  thiz->param.mfx.FrameInfo.AspectRatioW = info->par_n;
  thiz->param.mfx.FrameInfo.AspectRatioH = info->par_d;
  thiz->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  thiz->param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  thiz->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  thiz->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;

  /* allow subclass configure further */
  if (klass->configure) {
    if (!klass->configure (thiz))
      goto failed;
  }

  thiz->param.NumExtParam = thiz->extra_params->len;
  thiz->param.ExtParam = (mfxExtBuffer **) thiz->extra_params->pdata;

Hyunjun Ko's avatar
Hyunjun Ko committed
282
  session = gst_msdk_context_get_session (thiz->context);
Scott D Phillips's avatar
Scott D Phillips committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  /* validate parameters and allow the Media SDK to make adjustments */
  status = MFXVideoDECODE_Query (session, &thiz->param, &thiz->param);
  if (status < MFX_ERR_NONE) {
    GST_ERROR_OBJECT (thiz, "Video Decode Query failed (%s)",
        msdk_status_to_string (status));
    goto failed;
  } else if (status > MFX_ERR_NONE) {
    GST_WARNING_OBJECT (thiz, "Video Decode Query returned: %s",
        msdk_status_to_string (status));
  }

  status = MFXVideoDECODE_QueryIOSurf (session, &thiz->param, &request);
  if (status < MFX_ERR_NONE) {
    GST_ERROR_OBJECT (thiz, "Query IO surfaces failed (%s)",
        msdk_status_to_string (status));
    goto failed;
  } else if (status > MFX_ERR_NONE) {
    GST_WARNING_OBJECT (thiz, "Query IO surfaces returned: %s",
        msdk_status_to_string (status));
  }

  if (request.NumFrameSuggested < thiz->param.AsyncDepth) {
    GST_ERROR_OBJECT (thiz, "Required %d surfaces (%d suggested), async %d",
        request.NumFrameMin, request.NumFrameSuggested, thiz->param.AsyncDepth);
    goto failed;
  }

Hyunjun Ko's avatar
Hyunjun Ko committed
310
  if (thiz->use_video_memory) {
311
312
313
314
315
316
    gint shared_async_depth;

    shared_async_depth =
        gst_msdk_context_get_shared_async_depth (thiz->context);
    request.NumFrameSuggested += shared_async_depth;

Hyunjun Ko's avatar
Hyunjun Ko committed
317
318
    request.Type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
    gst_msdk_frame_alloc (thiz->context, &request, &thiz->alloc_resp);
Scott D Phillips's avatar
Scott D Phillips committed
319
320
  }

Hyunjun Ko's avatar
Hyunjun Ko committed
321
322
  GST_DEBUG_OBJECT (thiz, "Required %d surfaces (%d suggested)",
      request.NumFrameMin, request.NumFrameSuggested);
Scott D Phillips's avatar
Scott D Phillips committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

  status = MFXVideoDECODE_Init (session, &thiz->param);
  if (status < MFX_ERR_NONE) {
    GST_ERROR_OBJECT (thiz, "Init failed (%s)", msdk_status_to_string (status));
    goto failed;
  } else if (status > MFX_ERR_NONE) {
    GST_WARNING_OBJECT (thiz, "Init returned: %s",
        msdk_status_to_string (status));
  }

  status = MFXVideoDECODE_GetVideoParam (session, &thiz->param);
  if (status < MFX_ERR_NONE) {
    GST_ERROR_OBJECT (thiz, "Get Video Parameters failed (%s)",
        msdk_status_to_string (status));
    goto failed;
  } else if (status > MFX_ERR_NONE) {
    GST_WARNING_OBJECT (thiz, "Get Video Parameters returned: %s",
        msdk_status_to_string (status));
  }

  g_array_set_size (thiz->tasks, 0);
  g_array_set_size (thiz->tasks, thiz->param.AsyncDepth);
  thiz->next_task = 0;

  GST_OBJECT_UNLOCK (thiz);

349
  thiz->initialized = TRUE;
Scott D Phillips's avatar
Scott D Phillips committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  return TRUE;

failed:
  GST_OBJECT_UNLOCK (thiz);
  return FALSE;
}

static gboolean
gst_msdkdec_set_src_caps (GstMsdkDec * thiz)
{
  GstVideoCodecState *output_state;
  GstVideoAlignment align;
  guint width, height;

  width = GST_VIDEO_INFO_WIDTH (&thiz->input_state->info);
  height = GST_VIDEO_INFO_HEIGHT (&thiz->input_state->info);

  output_state =
      gst_video_decoder_set_output_state (GST_VIDEO_DECODER (thiz),
      GST_VIDEO_FORMAT_NV12, width, height, thiz->input_state);

371
  gst_msdk_set_video_alignment (&output_state->info, &align);
Scott D Phillips's avatar
Scott D Phillips committed
372
  gst_video_info_align (&output_state->info, &align);
Hyunjun Ko's avatar
Hyunjun Ko committed
373
  thiz->output_info = output_state->info;
Scott D Phillips's avatar
Scott D Phillips committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  if (output_state->caps)
    gst_caps_unref (output_state->caps);
  output_state->caps = gst_video_info_to_caps (&output_state->info);
  gst_video_codec_state_unref (output_state);

  return TRUE;
}

static void
gst_msdkdec_set_latency (GstMsdkDec * thiz)
{
  GstVideoInfo *info = &thiz->input_state->info;
  gint min_delayed_frames;
  GstClockTime latency;

  min_delayed_frames = thiz->tasks->len;

  if (info->fps_n) {
    latency = gst_util_uint64_scale_ceil (GST_SECOND * info->fps_d,
        min_delayed_frames, info->fps_n);
  } else {
    /* FIXME: Assume 25fps. This is better than reporting no latency at
     * all and then later failing in live pipelines
     */
    latency = gst_util_uint64_scale_ceil (GST_SECOND * 1,
        min_delayed_frames, 25);
  }

  GST_INFO_OBJECT (thiz,
      "Updating latency to %" GST_TIME_FORMAT " (%d frames)",
      GST_TIME_ARGS (latency), min_delayed_frames);

  gst_video_decoder_set_latency (GST_VIDEO_DECODER (thiz), latency, latency);
}

Hyunjun Ko's avatar
Hyunjun Ko committed
409
410
411
412
413
414
415
416
417
static gint
_find_msdk_surface (gconstpointer msdk_surface, gconstpointer comp_surface)
{
  MsdkSurface *cached_surface = (MsdkSurface *) msdk_surface;
  mfxFrameSurface1 *_surface = (mfxFrameSurface1 *) comp_surface;

  return cached_surface ? cached_surface->surface != _surface : -1;
}

418
static GstFlowReturn
Scott D Phillips's avatar
Scott D Phillips committed
419
420
421
gst_msdkdec_finish_task (GstMsdkDec * thiz, MsdkDecTask * task)
{
  GstVideoDecoder *decoder = GST_VIDEO_DECODER (thiz);
422
  GstFlowReturn flow;
Scott D Phillips's avatar
Scott D Phillips committed
423
424
425
  GstVideoCodecFrame *frame;
  MsdkSurface *surface;
  mfxStatus status;
Hyunjun Ko's avatar
Hyunjun Ko committed
426
  GList *l;
Scott D Phillips's avatar
Scott D Phillips committed
427
428
429

  if (G_LIKELY (task->sync_point)) {
    status =
Hyunjun Ko's avatar
Hyunjun Ko committed
430
        MFXVideoCORE_SyncOperation (gst_msdk_context_get_session
Hyunjun Ko's avatar
Hyunjun Ko committed
431
432
433
        (thiz->context), task->sync_point, 300000);
    if (status != MFX_ERR_NONE) {
      GST_ERROR_OBJECT (thiz, "failed to do sync operation");
434
      return GST_FLOW_ERROR;
Hyunjun Ko's avatar
Hyunjun Ko committed
435
    }
Scott D Phillips's avatar
Scott D Phillips committed
436

Hyunjun Ko's avatar
Hyunjun Ko committed
437
    frame = gst_video_decoder_get_oldest_frame (decoder);
Scott D Phillips's avatar
Scott D Phillips committed
438
    task->sync_point = NULL;
Hyunjun Ko's avatar
Hyunjun Ko committed
439
440
441
442
443
444
445
446
447

    l = g_list_find_custom (thiz->decoded_msdk_surfaces, task->surface,
        _find_msdk_surface);
    if (l) {
      surface = l->data;
    } else {
      GST_ERROR_OBJECT (thiz, "Couldn't find the cached MSDK surface");
      return GST_FLOW_ERROR;
    }
448
449
450

    if (G_LIKELY (frame)) {
      if (G_LIKELY (surface->copy.buffer == NULL)) {
Hyunjun Ko's avatar
Hyunjun Ko committed
451
        frame->output_buffer = gst_buffer_ref (surface->buf);
452
453
454
455
      } else {
        gst_video_frame_copy (&surface->copy, &surface->data);
        frame->output_buffer = gst_buffer_ref (surface->copy.buffer);
      }
Scott D Phillips's avatar
Scott D Phillips committed
456
    }
457

Hyunjun Ko's avatar
Hyunjun Ko committed
458
    free_surface (thiz, surface);
Scott D Phillips's avatar
Scott D Phillips committed
459

460
461
    if (!frame)
      return GST_FLOW_FLUSHING;
Scott D Phillips's avatar
Scott D Phillips committed
462
    gst_video_codec_frame_unref (frame);
Hyunjun Ko's avatar
Hyunjun Ko committed
463
464

    flow = gst_video_decoder_finish_frame (decoder, frame);
465
    return flow;
Scott D Phillips's avatar
Scott D Phillips committed
466
  }
467
  return GST_FLOW_OK;
Scott D Phillips's avatar
Scott D Phillips committed
468
469
}

470
471
472
473
474
475
476
477
static gboolean
gst_msdkdec_start (GstVideoDecoder * decoder)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);

  if (gst_msdk_context_prepare (GST_ELEMENT_CAST (thiz), &thiz->context)) {
    GST_INFO_OBJECT (thiz, "Found context %" GST_PTR_FORMAT " from neighbour",
        thiz->context);
478
    thiz->use_video_memory = TRUE;
479
480
481
482
483
484

    if (gst_msdk_context_get_job_type (thiz->context) & GST_MSDK_JOB_DECODER) {
      GstMsdkContext *parent_context;

      parent_context = thiz->context;
      thiz->context = gst_msdk_context_new_with_parent (parent_context);
485
486
487

      gst_msdk_context_add_shared_async_depth (thiz->context,
          gst_msdk_context_get_shared_async_depth (parent_context));
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
      gst_object_unref (parent_context);

      GST_INFO_OBJECT (thiz,
          "Creating new context %" GST_PTR_FORMAT " with joined session",
          thiz->context);
    } else {
      gst_msdk_context_add_job_type (thiz->context, GST_MSDK_JOB_DECODER);
    }
  } else {
    gst_msdk_context_ensure_context (GST_ELEMENT_CAST (thiz), thiz->hardware,
        GST_MSDK_JOB_DECODER);
    GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT,
        thiz->context);
  }

503
504
  gst_msdk_context_add_shared_async_depth (thiz->context, thiz->async_depth);

505
506
507
  return TRUE;
}

Scott D Phillips's avatar
Scott D Phillips committed
508
509
510
511
static gboolean
gst_msdkdec_close (GstVideoDecoder * decoder)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);
Hyunjun Ko's avatar
Hyunjun Ko committed
512
513
514
515

  if (thiz->context)
    gst_object_replace ((GstObject **) & thiz->context, NULL);

Scott D Phillips's avatar
Scott D Phillips committed
516
517
518
519
520
521
522
  return TRUE;
}

static gboolean
gst_msdkdec_stop (GstVideoDecoder * decoder)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);
523
524
525

  gst_msdkdec_flush (decoder);

Scott D Phillips's avatar
Scott D Phillips committed
526
527
528
529
530
531
  if (thiz->input_state) {
    gst_video_codec_state_unref (thiz->input_state);
    thiz->input_state = NULL;
  }
  if (thiz->pool) {
    gst_object_unref (thiz->pool);
Hyunjun Ko's avatar
Hyunjun Ko committed
532
    thiz->pool = NULL;
Scott D Phillips's avatar
Scott D Phillips committed
533
534
535
  }
  gst_video_info_init (&thiz->output_info);
  gst_video_info_init (&thiz->pool_info);
536
537

  gst_msdkdec_close_decoder (thiz);
Scott D Phillips's avatar
Scott D Phillips committed
538
539
540
541
542
543
544
545
546
547
548
549
  return TRUE;
}

static gboolean
gst_msdkdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);

  if (thiz->input_state)
    gst_video_codec_state_unref (thiz->input_state);
  thiz->input_state = gst_video_codec_state_ref (state);

Hyunjun Ko's avatar
Hyunjun Ko committed
550
  if (!gst_msdkdec_set_src_caps (thiz))
Scott D Phillips's avatar
Scott D Phillips committed
551
552
553
554
555
556
    return FALSE;

  gst_msdkdec_set_latency (thiz);
  return TRUE;
}

Hyunjun Ko's avatar
Hyunjun Ko committed
557
558
559
560
561
562
563
564
565
566
567
568
static void
release_msdk_surfaces (GstMsdkDec * thiz)
{
  GList *l;
  MsdkSurface *surface;

  for (l = thiz->decoded_msdk_surfaces; l; l = l->next) {
    surface = l->data;
    free_surface (thiz, surface);
  }
}

Scott D Phillips's avatar
Scott D Phillips committed
569
570
571
572
static GstFlowReturn
gst_msdkdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);
573
574
  GstFlowReturn flow;
  GstBuffer *buffer;
Scott D Phillips's avatar
Scott D Phillips committed
575
576
  MsdkDecTask *task = NULL;
  mfxBitstream bitstream;
Hyunjun Ko's avatar
Hyunjun Ko committed
577
  MsdkSurface *surface = NULL;
Scott D Phillips's avatar
Scott D Phillips committed
578
579
580
581
  mfxSession session;
  mfxStatus status;
  GstMapInfo map_info;
  guint i;
582
  gsize data_size;
Scott D Phillips's avatar
Scott D Phillips committed
583

584
585
586
  if (!thiz->initialized)
    gst_video_decoder_negotiate (decoder);

Scott D Phillips's avatar
Scott D Phillips committed
587
588
  if (!gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ))
    return GST_FLOW_ERROR;
Hyunjun Ko's avatar
Hyunjun Ko committed
589

Scott D Phillips's avatar
Scott D Phillips committed
590
  memset (&bitstream, 0, sizeof (bitstream));
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609

  if (thiz->is_packetized) {
    /* Packetized stream: We prefer to have a parser as connected upstream
     * element to the decoder */
    bitstream.Data = map_info.data;
    bitstream.DataLength = map_info.size;
    bitstream.MaxLength = map_info.size;
  } else {
    /* Non packetized streams: eg: vc1 advanced profile with per buffer bdu */
    gst_adapter_push (thiz->adapter, gst_buffer_ref (frame->input_buffer));
    data_size = gst_adapter_available (thiz->adapter);

    bitstream.Data = (mfxU8 *) gst_adapter_map (thiz->adapter, data_size);
    bitstream.DataLength = (mfxU32) data_size;
    bitstream.MaxLength = bitstream.DataLength;
  }
  GST_INFO_OBJECT (thiz,
      "mfxBitStream=> DataLength:%d DataOffset:%d MaxLength:%d",
      bitstream.DataLength, bitstream.DataOffset, bitstream.MaxLength);
Scott D Phillips's avatar
Scott D Phillips committed
610

Hyunjun Ko's avatar
Hyunjun Ko committed
611
  session = gst_msdk_context_get_session (thiz->context);
Scott D Phillips's avatar
Scott D Phillips committed
612
613
  for (;;) {
    task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
614
615
    flow = gst_msdkdec_finish_task (thiz, task);
    if (flow != GST_FLOW_OK)
616
      goto error;
Scott D Phillips's avatar
Scott D Phillips committed
617
    if (!surface) {
618
619
      flow = allocate_output_buffer (thiz, &buffer);
      if (flow != GST_FLOW_OK)
620
        goto error;
621
      surface = get_surface (thiz, buffer);
Scott D Phillips's avatar
Scott D Phillips committed
622
623
624
625
626
627
      if (!surface) {
        /* Can't get a surface for some reason, finish tasks to see if
           a surface becomes available. */
        for (i = 0; i < thiz->tasks->len - 1; i++) {
          thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
          task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
628
629
          flow = gst_msdkdec_finish_task (thiz, task);
          if (flow != GST_FLOW_OK)
630
            goto error;
631
          surface = get_surface (thiz, buffer);
Scott D Phillips's avatar
Scott D Phillips committed
632
633
634
635
636
          if (surface)
            break;
        }
        if (!surface) {
          GST_ERROR_OBJECT (thiz, "Couldn't get a surface");
637
          flow = GST_FLOW_ERROR;
638
          goto error;
Scott D Phillips's avatar
Scott D Phillips committed
639
640
641
642
643
        }
      }
    }

    status =
Hyunjun Ko's avatar
Hyunjun Ko committed
644
        MFXVideoDECODE_DecodeFrameAsync (session, &bitstream, surface->surface,
Scott D Phillips's avatar
Scott D Phillips committed
645
646
647
        &task->surface, &task->sync_point);
    if (G_LIKELY (status == MFX_ERR_NONE)) {
      thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
Hyunjun Ko's avatar
Hyunjun Ko committed
648
649
650
651

      if (surface->surface->Data.Locked > 0 || !thiz->use_video_memory)
        surface = NULL;

Scott D Phillips's avatar
Scott D Phillips committed
652
      if (bitstream.DataLength == 0) {
653
        flow = GST_FLOW_OK;
Scott D Phillips's avatar
Scott D Phillips committed
654
655
656
        break;
      }
    } else if (status == MFX_ERR_MORE_DATA) {
Hyunjun Ko's avatar
Hyunjun Ko committed
657
658
      if (surface->surface->Data.Locked > 0)
        surface = NULL;
659
      flow = GST_FLOW_OK;
Scott D Phillips's avatar
Scott D Phillips committed
660
661
662
663
      break;
    } else if (status == MFX_ERR_MORE_SURFACE) {
      surface = NULL;
      continue;
Hyunjun Ko's avatar
Hyunjun Ko committed
664
    } else if (status == MFX_WRN_DEVICE_BUSY) {
Scott D Phillips's avatar
Scott D Phillips committed
665
666
      /* If device is busy, wait 1ms and retry, as per MSDK's recomendation */
      g_usleep (1000);
Hyunjun Ko's avatar
Hyunjun Ko committed
667
668
669
670
671
672

      /* If the current surface is still busy, we should do sync oepration
       * then tries to decode again
       */
      thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
    } else if (status < MFX_ERR_NONE) {
Scott D Phillips's avatar
Scott D Phillips committed
673
674
      GST_ERROR_OBJECT (thiz, "DecodeFrameAsync failed (%s)",
          msdk_status_to_string (status));
675
      flow = GST_FLOW_ERROR;
Scott D Phillips's avatar
Scott D Phillips committed
676
677
678
679
      break;
    }
  }

680
681
682
683
684
685
  if (!thiz->is_packetized) {
    /* flush out the data which is already consumed by msdk */
    gst_adapter_flush (thiz->adapter, bitstream.DataOffset);
    flow = GST_FLOW_OK;
  }

Scott D Phillips's avatar
Scott D Phillips committed
686
  if (surface)
Hyunjun Ko's avatar
Hyunjun Ko committed
687
    free_surface (thiz, surface);
Scott D Phillips's avatar
Scott D Phillips committed
688
689

  gst_buffer_unmap (frame->input_buffer, &map_info);
690
  return flow;
691
692
693
694
695
696

error:
  gst_buffer_unmap (frame->input_buffer, &map_info);
  gst_video_decoder_drop_frame (decoder, frame);

  return flow;
Scott D Phillips's avatar
Scott D Phillips committed
697
698
}

Hyunjun Ko's avatar
Hyunjun Ko committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
static GstBufferPool *
gst_msdkdec_create_buffer_pool (GstMsdkDec * thiz, GstCaps * caps,
    guint num_buffers)
{
  GstBufferPool *pool = NULL;
  GstStructure *config;
  GstAllocator *allocator = NULL;
  GstVideoInfo info;
  GstVideoAlignment align;
  GstAllocationParams params = { 0, 31, 0, 0, };
  mfxFrameAllocResponse *alloc_resp = NULL;

  alloc_resp = &thiz->alloc_resp;

  pool = gst_msdk_buffer_pool_new (thiz->context, alloc_resp);
  if (!pool)
    goto error_no_pool;

  if (!gst_video_info_from_caps (&info, caps)) {
    GST_INFO_OBJECT (thiz, "failed to get video info");
    return FALSE;
  }

  gst_msdk_set_video_alignment (&info, &align);
  gst_video_info_align (&info, &align);

  if (thiz->use_video_memory)
    allocator = gst_msdk_video_allocator_new (thiz->context, &info, alloc_resp);
  else
    allocator = gst_msdk_system_allocator_new (&info);

  if (!allocator)
    goto error_no_allocator;

  config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
  gst_buffer_pool_config_set_params (config, caps, info.size, num_buffers, 0);
  gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
  gst_buffer_pool_config_add_option (config,
      GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);

  if (thiz->use_video_memory)
    gst_buffer_pool_config_add_option (config,
        GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);

  gst_buffer_pool_config_set_video_alignment (config, &align);
  gst_buffer_pool_config_set_allocator (config, allocator, &params);
  gst_object_unref (allocator);

  if (!gst_buffer_pool_set_config (pool, config))
    goto error_pool_config;

  return pool;

error_no_pool:
  {
    GST_INFO_OBJECT (thiz, "failed to create bufferpool");
    return NULL;
  }
error_no_allocator:
  {
    GST_INFO_OBJECT (thiz, "failed to create allocator");
    return NULL;
  }
error_pool_config:
  {
    GST_INFO_OBJECT (thiz, "failed to set config");
    return NULL;
  }
}

Scott D Phillips's avatar
Scott D Phillips committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
static gboolean
gst_msdkdec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);
  GstVideoInfo info_from_caps, info_aligned;
  GstVideoAlignment alignment;
  GstBufferPool *pool = NULL;
  GstStructure *pool_config = NULL;
  GstCaps *pool_caps;
  guint size, min_buffers, max_buffers;

  if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
          query))
    return FALSE;

  /* Get the buffer pool config decided by the base class. The base
     class ensures that there will always be at least a 0th pool in
     the query. */
  gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
  pool_config = gst_buffer_pool_get_config (pool);

790
791
792
793
  /* If downstream's pool is MSDK bufferpool, decoder is using video memory */
  if (GST_IS_MSDK_BUFFER_POOL (pool))
    thiz->use_video_memory = TRUE;

Hyunjun Ko's avatar
Hyunjun Ko committed
794
795
  /* Get the caps of pool and increase the min and max buffers by async_depth,
   * we will always have that number of decode operations in-flight */
Scott D Phillips's avatar
Scott D Phillips committed
796
797
798
799
800
801
  gst_buffer_pool_config_get_params (pool_config, &pool_caps, &size,
      &min_buffers, &max_buffers);
  min_buffers += thiz->async_depth;
  if (max_buffers)
    max_buffers += thiz->async_depth;

Hyunjun Ko's avatar
Hyunjun Ko committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)
      && gst_buffer_pool_has_option (pool,
          GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
    GstStructure *config;
    GstAllocator *allocator;

    /* If downstream supports video meta and video alignment,
     * we can replace our own msdk bufferpool and use it
     */
    GST_INFO_OBJECT (decoder, "create new MSDK bufferpool");

    /* Remove downstream's pool */
    gst_structure_free (pool_config);
    gst_object_unref (pool);

    /* FIXME: this might break renegotiation.
     * We should re-create msdk bufferpool, but it breaks decoding. */
    if (!thiz->pool) {
      thiz->pool =
          gst_msdkdec_create_buffer_pool (thiz, pool_caps, min_buffers);
      if (!thiz->pool)
        goto failed_to_create_pool;
    }
    pool = gst_object_ref (thiz->pool);

    /* Set the allocator of new msdk bufferpool */
    config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));

    if (gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
      gst_query_set_nth_allocation_param (query, 0, allocator, NULL);
    gst_structure_free (config);
  } else {
    /* If not, we just make a side-pool that will be decoded into and
     * the copied from.
     */
    GST_INFO_OBJECT (decoder, "create new MSDK bufferpool as a side-pool");
    thiz->pool =
        gst_msdkdec_create_buffer_pool (thiz, pool_caps, thiz->async_depth);
    if (!thiz->pool)
      goto failed_to_create_pool;

    /* Update params to downstream's pool */
Scott D Phillips's avatar
Scott D Phillips committed
844
845
    gst_buffer_pool_config_set_params (pool_config, pool_caps, size,
        min_buffers, max_buffers);
Hyunjun Ko's avatar
Hyunjun Ko committed
846
847
    if (!gst_buffer_pool_set_config (pool, pool_config))
      goto error_set_config;
Scott D Phillips's avatar
Scott D Phillips committed
848

Hyunjun Ko's avatar
Hyunjun Ko committed
849
850
851
852
853
854
855
856
857
858
    /* Check if the pool's caps will meet msdk's alignment
     * requirements by default and get aligned video info.
     */
    gst_video_info_from_caps (&info_from_caps, pool_caps);
    info_aligned = info_from_caps;
    gst_msdk_set_video_alignment (&info_from_caps, &alignment);
    gst_video_info_align (&info_aligned, &alignment);

    thiz->output_info = info_from_caps;
    thiz->pool_info = info_aligned;
Scott D Phillips's avatar
Scott D Phillips committed
859
860
861
862
863
  }

  gst_query_set_nth_allocation_pool (query, 0, pool, size, min_buffers,
      max_buffers);

864
865
866
  if (pool)
    gst_object_unref (pool);

867
868
869
870
871
872
  /* Initialize MSDK decoder before new bufferpool tries to alloc each buffer,
   * which requires information of frame allocation.
   */
  if (!gst_msdkdec_init_decoder (thiz))
    return FALSE;

Scott D Phillips's avatar
Scott D Phillips committed
873
  return TRUE;
874

Hyunjun Ko's avatar
Hyunjun Ko committed
875
failed_to_create_pool:
876
877
878
879
  GST_ERROR_OBJECT (decoder, "failed to set buffer pool config");
  if (pool)
    gst_object_unref (pool);
  return FALSE;
880

Hyunjun Ko's avatar
Hyunjun Ko committed
881
882
883
884
885
error_set_config:
  GST_ERROR_OBJECT (decoder, "failed to set buffer pool config");
  if (pool)
    gst_object_unref (pool);
  return FALSE;
886
887
}

Scott D Phillips's avatar
Scott D Phillips committed
888
889
890
891
static GstFlowReturn
gst_msdkdec_drain (GstVideoDecoder * decoder)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);
892
893
  GstFlowReturn flow;
  GstBuffer *buffer;
Scott D Phillips's avatar
Scott D Phillips committed
894
895
896
897
898
899
  MsdkDecTask *task;
  MsdkSurface *surface = NULL;
  mfxSession session;
  mfxStatus status;
  guint i;

900
  if (!thiz->initialized)
Scott D Phillips's avatar
Scott D Phillips committed
901
    return GST_FLOW_OK;
Hyunjun Ko's avatar
Hyunjun Ko committed
902
  session = gst_msdk_context_get_session (thiz->context);
Scott D Phillips's avatar
Scott D Phillips committed
903
904
905

  for (;;) {
    task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
Hyunjun Ko's avatar
Hyunjun Ko committed
906
907
908
909
910
    if ((flow = gst_msdkdec_finish_task (thiz, task)) != GST_FLOW_OK) {
      if (flow != GST_FLOW_FLUSHING)
        GST_WARNING_OBJECT (decoder,
            "failed to finish the task %p, but keep draining for the remaining frames",
            task);
911
    }
912

Scott D Phillips's avatar
Scott D Phillips committed
913
    if (!surface) {
914
915
916
917
      flow = allocate_output_buffer (thiz, &buffer);
      if (flow != GST_FLOW_OK)
        return flow;
      surface = get_surface (thiz, buffer);
Scott D Phillips's avatar
Scott D Phillips committed
918
919
920
921
922
      if (!surface)
        return GST_FLOW_ERROR;
    }

    status =
Hyunjun Ko's avatar
Hyunjun Ko committed
923
        MFXVideoDECODE_DecodeFrameAsync (session, NULL, surface->surface,
Scott D Phillips's avatar
Scott D Phillips committed
924
925
926
        &task->surface, &task->sync_point);
    if (G_LIKELY (status == MFX_ERR_NONE)) {
      thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
Hyunjun Ko's avatar
Hyunjun Ko committed
927
928
929

      if (surface->surface->Data.Locked == 0)
        free_surface (thiz, surface);
Scott D Phillips's avatar
Scott D Phillips committed
930
      surface = NULL;
Hyunjun Ko's avatar
Hyunjun Ko committed
931
    } else if (status == MFX_WRN_VIDEO_PARAM_CHANGED) {
Scott D Phillips's avatar
Scott D Phillips committed
932
      continue;
Hyunjun Ko's avatar
Hyunjun Ko committed
933
    } else if (status == MFX_WRN_DEVICE_BUSY) {
Scott D Phillips's avatar
Scott D Phillips committed
934
935
      /* If device is busy, wait 1ms and retry, as per MSDK's recomendation */
      g_usleep (1000);
Hyunjun Ko's avatar
Hyunjun Ko committed
936
937
938
939
940
941

      /* If the current surface is still busy, we should do sync oepration
       * then tries to decode again
       */
      thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
    } else if (status == MFX_ERR_MORE_DATA) {
Scott D Phillips's avatar
Scott D Phillips committed
942
      break;
Hyunjun Ko's avatar
Hyunjun Ko committed
943
944
945
946
    } else if (status == MFX_ERR_MORE_SURFACE) {
      surface = NULL;
      continue;
    } else if (status < MFX_ERR_NONE)
Scott D Phillips's avatar
Scott D Phillips committed
947
948
949
      return GST_FLOW_ERROR;
  }
  if (surface)
Hyunjun Ko's avatar
Hyunjun Ko committed
950
    free_surface (thiz, surface);
Scott D Phillips's avatar
Scott D Phillips committed
951
952
953

  for (i = 0; i < thiz->tasks->len; i++) {
    task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
954
    gst_msdkdec_finish_task (thiz, task);
Scott D Phillips's avatar
Scott D Phillips committed
955
956
    thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
  }
Hyunjun Ko's avatar
Hyunjun Ko committed
957
958
959

  release_msdk_surfaces (thiz);

Scott D Phillips's avatar
Scott D Phillips committed
960
961
962
  return GST_FLOW_OK;
}

Hyunjun Ko's avatar
Hyunjun Ko committed
963
964
965
966
967
968
969
970
static gboolean
gst_msdkdec_flush (GstVideoDecoder * decoder)
{
  GstMsdkDec *thiz = GST_MSDKDEC (decoder);

  return gst_msdkdec_drain (GST_VIDEO_DECODER_CAST (thiz));
}

Scott D Phillips's avatar
Scott D Phillips committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static GstFlowReturn
gst_msdkdec_finish (GstVideoDecoder * decoder)
{
  return gst_msdkdec_drain (decoder);
}

static void
gst_msdkdec_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
{
  GstMsdkDec *thiz = GST_MSDKDEC (object);
  GstState state;

  GST_OBJECT_LOCK (thiz);

  state = GST_STATE (thiz);
  if ((state != GST_STATE_READY && state != GST_STATE_NULL) &&
      !(pspec->flags & GST_PARAM_MUTABLE_PLAYING))
    goto wrong_state;

  switch (prop_id) {
    case PROP_HARDWARE:
      thiz->hardware = g_value_get_boolean (value);
      break;
    case PROP_ASYNC_DEPTH:
      thiz->async_depth = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;