msdk.c 14.2 KB
Newer Older
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
/* GStreamer Intel MSDK plugin
 * Copyright (c) 2016, Oblong Industries, Inc.
 * 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.
 */

#include "msdk.h"
Hyunjun Ko's avatar
Hyunjun Ko committed
33
34
#include "gstmsdkvideomemory.h"
#include "gstmsdksystemmemory.h"
35
36
37
38
39

GST_DEBUG_CATEGORY_EXTERN (gst_msdkenc_debug);
#define GST_CAT_DEFAULT gst_msdkenc_debug

#define INVALID_INDEX         ((guint) -1)
40
#define GST_MSDK_ALIGNMENT_PADDING(num) (32 - ((num) & 31))
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
struct map
{
  GstVideoFormat format;
  mfxU16 mfx_chroma_format;
  mfxU32 mfx_fourcc;
};

#define GST_VIDEO_INFO_TO_MFX_MAP(FORMAT, CHROMA, FOURCC) \
    { GST_VIDEO_FORMAT_##FORMAT, MFX_CHROMAFORMAT_##CHROMA, MFX_FOURCC_##FOURCC }

static const struct map gst_msdk_video_format_to_mfx_map[] = {
  GST_VIDEO_INFO_TO_MFX_MAP (NV12, YUV420, NV12),
  GST_VIDEO_INFO_TO_MFX_MAP (YV12, YUV420, YV12),
  GST_VIDEO_INFO_TO_MFX_MAP (I420, YUV420, YV12),
  GST_VIDEO_INFO_TO_MFX_MAP (YUY2, YUV422, YUY2),
  GST_VIDEO_INFO_TO_MFX_MAP (UYVY, YUV422, UYVY),
  GST_VIDEO_INFO_TO_MFX_MAP (BGRA, YUV444, RGB4),
  {0, 0, 0}
};

62
63
64
65
static inline guint
msdk_get_free_surface_index (mfxFrameSurface1 * surfaces, guint size)
{
  if (surfaces) {
66
67
68
    guint i;

    for (i = 0; i < size; i++) {
69
70
71
72
73
74
75
76
77
78
79
80
      if (!surfaces[i].Data.Locked)
        return i;
    }
  }

  return INVALID_INDEX;
}

mfxFrameSurface1 *
msdk_get_free_surface (mfxFrameSurface1 * surfaces, guint size)
{
  guint idx = INVALID_INDEX;
81
  guint i;
82
83

  /* Poll the pool for a maximum of 20 milisecnds */
84
  for (i = 0; i < 2000; i++) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    idx = msdk_get_free_surface_index (surfaces, size);

    if (idx != INVALID_INDEX)
      break;

    g_usleep (10);
  }

  return (idx == INVALID_INDEX ? NULL : &surfaces[idx]);
}

/* FIXME: Only NV12 is supported by now, add other YUV formats */
void
msdk_frame_to_surface (GstVideoFrame * frame, mfxFrameSurface1 * surface)
{
  guint8 *src, *dst;
  guint sstride, dstride;
  guint width, height;
103
  guint i, p;
104
105

  if (!surface->Data.MemId) {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    switch (frame->info.finfo->format) {
      case GST_VIDEO_FORMAT_NV12:
        surface->Data.Y = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
        surface->Data.UV = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
        surface->Data.Pitch = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
        break;

      case GST_VIDEO_FORMAT_YV12:
      case GST_VIDEO_FORMAT_I420:
        surface->Data.Y = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
        surface->Data.U = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
        surface->Data.V = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
        surface->Data.Pitch = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
        break;

      case GST_VIDEO_FORMAT_YUY2:
122
123
124
125
126
        surface->Data.Y = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
        surface->Data.Pitch = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
        surface->Data.U = surface->Data.Y + 1;
        surface->Data.V = surface->Data.Y + 3;
        break;
127
128
129
      case GST_VIDEO_FORMAT_UYVY:
        surface->Data.Y = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
        surface->Data.Pitch = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
130
131
132
        surface->Data.U = surface->Data.Y;
        surface->Data.Y = surface->Data.U + 1;
        surface->Data.V = surface->Data.U + 2;
133
134
135
136
        break;

      case GST_VIDEO_FORMAT_BGRA:
        surface->Data.R = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
137
138
        surface->Data.G = surface->Data.R - 1;
        surface->Data.B = surface->Data.R - 2;
139
140
141
142
143
144
145
146
        surface->Data.Pitch = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
        break;

      default:
        g_assert_not_reached ();
        break;
    }

147
148
149
150
    return;
  }


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  switch (frame->info.finfo->format) {
    case GST_VIDEO_FORMAT_NV12:
      width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
      for (p = 0; p < 2; p++) {
        height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, p);
        src = GST_VIDEO_FRAME_COMP_DATA (frame, p);
        sstride = GST_VIDEO_FRAME_COMP_STRIDE (frame, p);
        dst = p == 0 ? surface->Data.Y : surface->Data.UV;
        dstride = surface->Data.Pitch;

        for (i = 0; i < height; i++) {
          memcpy (dst, src, width);
          src += sstride;
          dst += dstride;
        }
      }
      break;

    case GST_VIDEO_FORMAT_YV12:
    case GST_VIDEO_FORMAT_I420:
      for (p = 0; p < 3; p++) {
        width = GST_VIDEO_FRAME_COMP_WIDTH (frame, p);
        height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, p);
        src = GST_VIDEO_FRAME_COMP_DATA (frame, p);
        sstride = GST_VIDEO_FRAME_COMP_STRIDE (frame, p);
        switch (p) {
          case 0:
            dst = surface->Data.Y;
            break;
          case 1:
            dst = surface->Data.U;
            break;
          case 2:
            dst = surface->Data.V;
            break;
          default:
            g_assert_not_reached ();
            break;
        }
        dstride = surface->Data.Pitch;
        if (p > 0)
          dstride = dstride / 2;

        for (i = 0; i < height; i++) {
          memcpy (dst, src, width);
          src += sstride;
          dst += dstride;
        }
      }
      break;

    case GST_VIDEO_FORMAT_YUY2:
    case GST_VIDEO_FORMAT_UYVY:
      width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
      height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
      src = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
      sstride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
      dst = surface->Data.Y;
      dstride = surface->Data.Pitch;

      width *= 2;
      width = MIN (sstride, width);

      for (i = 0; i < height; i++) {
        memcpy (dst, src, width);
        src += sstride;
        dst += dstride;
      }
      break;
220

221
222
223
224
225
    case GST_VIDEO_FORMAT_BGRA:
      width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
      height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
      src = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
      sstride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
226
      dst = surface->Data.B;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      dstride = surface->Data.Pitch;

      width *= 4;

      for (i = 0; i < height; i++) {
        memcpy (dst, src, width);
        src += sstride;
        dst += dstride;
      }
      break;

    default:
      g_assert_not_reached ();
      break;
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
310
311
312
313
314
315
316
317
318
319
320
321
322
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  }
}

const gchar *
msdk_status_to_string (mfxStatus status)
{
  switch (status) {
      /* no error */
    case MFX_ERR_NONE:
      return "no error";
      /* reserved for unexpected errors */
    case MFX_ERR_UNKNOWN:
      return "unknown error";
      /* error codes <0 */
    case MFX_ERR_NULL_PTR:
      return "null pointer";
    case MFX_ERR_UNSUPPORTED:
      return "undeveloped feature";
    case MFX_ERR_MEMORY_ALLOC:
      return "failed to allocate memory";
    case MFX_ERR_NOT_ENOUGH_BUFFER:
      return "insufficient buffer at input/output";
    case MFX_ERR_INVALID_HANDLE:
      return "invalid handle";
    case MFX_ERR_LOCK_MEMORY:
      return "failed to lock the memory block";
    case MFX_ERR_NOT_INITIALIZED:
      return "member function called before initialization";
    case MFX_ERR_NOT_FOUND:
      return "the specified object is not found";
    case MFX_ERR_MORE_DATA:
      return "expect more data at input";
    case MFX_ERR_MORE_SURFACE:
      return "expect more surface at output";
    case MFX_ERR_ABORTED:
      return "operation aborted";
    case MFX_ERR_DEVICE_LOST:
      return "lose the HW acceleration device";
    case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
      return "incompatible video parameters";
    case MFX_ERR_INVALID_VIDEO_PARAM:
      return "invalid video parameters";
    case MFX_ERR_UNDEFINED_BEHAVIOR:
      return "undefined behavior";
    case MFX_ERR_DEVICE_FAILED:
      return "device operation failure";
    case MFX_ERR_MORE_BITSTREAM:
      return "expect more bitstream buffers at output";
    case MFX_ERR_INCOMPATIBLE_AUDIO_PARAM:
      return "incompatible audio parameters";
    case MFX_ERR_INVALID_AUDIO_PARAM:
      return "invalid audio parameters";
      /* warnings >0 */
    case MFX_WRN_IN_EXECUTION:
      return "the previous asynchronous operation is in execution";
    case MFX_WRN_DEVICE_BUSY:
      return "the HW acceleration device is busy";
    case MFX_WRN_VIDEO_PARAM_CHANGED:
      return "the video parameters are changed during decoding";
    case MFX_WRN_PARTIAL_ACCELERATION:
      return "SW is used";
    case MFX_WRN_INCOMPATIBLE_VIDEO_PARAM:
      return "incompatible video parameters";
    case MFX_WRN_VALUE_NOT_CHANGED:
      return "the value is saturated based on its valid range";
    case MFX_WRN_OUT_OF_RANGE:
      return "the value is out of valid range";
    case MFX_WRN_FILTER_SKIPPED:
      return "one of requested filters has been skipped";
    case MFX_WRN_INCOMPATIBLE_AUDIO_PARAM:
      return "incompatible audio parameters";
    default:
      break;
  }
  return "undefiend error";
}

void
msdk_close_session (mfxSession session)
{
  mfxStatus status;

  if (!session)
    return;

  status = MFXClose (session);
  if (status != MFX_ERR_NONE)
    GST_ERROR ("Close failed (%s)", msdk_status_to_string (status));
}

mfxSession
msdk_open_session (gboolean hardware)
{
  mfxSession session = NULL;
  mfxVersion version = { {1, 1}
  };
  mfxIMPL implementation;
  mfxStatus status;

  static const gchar *implementation_names[] = {
    "AUTO", "SOFTWARE", "HARDWARE", "AUTO_ANY", "HARDWARE_ANY", "HARDWARE2",
    "HARDWARE3", "HARDWARE4", "RUNTIME"
  };

  status = MFXInit (hardware ? MFX_IMPL_HARDWARE_ANY : MFX_IMPL_SOFTWARE,
      &version, &session);
  if (status != MFX_ERR_NONE) {
    GST_ERROR ("Intel Media SDK not available (%s)",
        msdk_status_to_string (status));
    goto failed;
  }

  MFXQueryIMPL (session, &implementation);
  if (status != MFX_ERR_NONE) {
    GST_ERROR ("Query implementation failed (%s)",
        msdk_status_to_string (status));
    goto failed;
  }

  MFXQueryVersion (session, &version);
  if (status != MFX_ERR_NONE) {
    GST_ERROR ("Query version failed (%s)", msdk_status_to_string (status));
    goto failed;
  }

  GST_INFO ("MSDK implementation: 0x%04x (%s)", implementation,
      implementation_names[MFX_IMPL_BASETYPE (implementation)]);
  GST_INFO ("MSDK version: %d.%d", version.Major, version.Minor);

  return session;

failed:
  msdk_close_session (session);
  return NULL;
}

gboolean
msdk_is_available (void)
{
  mfxSession session = msdk_open_session (FALSE);
  if (!session) {
    return FALSE;
  }

  msdk_close_session (session);
  return TRUE;
}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

void
gst_msdk_set_video_alignment (GstVideoInfo * info,
    GstVideoAlignment * alignment)
{
  guint i, width, height;

  width = GST_VIDEO_INFO_WIDTH (info);
  height = GST_VIDEO_INFO_HEIGHT (info);

  gst_video_alignment_reset (alignment);
  for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++)
    alignment->stride_align[i] = 31;    /* 32-byte alignment */

  if (width & 31)
    alignment->padding_right = GST_MSDK_ALIGNMENT_PADDING (width);
  if (height & 31)
    alignment->padding_bottom = GST_MSDK_ALIGNMENT_PADDING (height);
}
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

static const struct map *
_map_lookup_format (GstVideoFormat format)
{
  const struct map *m = gst_msdk_video_format_to_mfx_map;

  for (; m->format != 0; m++) {
    if (m->format == format)
      return m;
  }
  return NULL;
}

gint
gst_msdk_get_mfx_chroma_from_format (GstVideoFormat format)
{
  const struct map *const m = _map_lookup_format (format);

  return m ? m->mfx_chroma_format : -1;
}

gint
gst_msdk_get_mfx_fourcc_from_format (GstVideoFormat format)
{
  const struct map *const m = _map_lookup_format (format);

  return m ? m->mfx_fourcc : -1;
}

void
gst_msdk_set_mfx_frame_info_from_video_info (mfxFrameInfo * mfx_info,
    GstVideoInfo * info)
{
  g_return_if_fail (info && mfx_info);

  mfx_info->Width = GST_ROUND_UP_32 (GST_VIDEO_INFO_WIDTH (info));
  mfx_info->Height = GST_ROUND_UP_32 (GST_VIDEO_INFO_HEIGHT (info));
  mfx_info->CropW = GST_VIDEO_INFO_WIDTH (info);
  mfx_info->CropH = GST_VIDEO_INFO_HEIGHT (info);
  mfx_info->FrameRateExtN = GST_VIDEO_INFO_FPS_N (info);
  mfx_info->FrameRateExtD = GST_VIDEO_INFO_FPS_D (info);
  mfx_info->AspectRatioW = GST_VIDEO_INFO_PAR_N (info);
  mfx_info->AspectRatioH = GST_VIDEO_INFO_PAR_D (info);
  mfx_info->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;      /* this is by default */
  mfx_info->FourCC =
      gst_msdk_get_mfx_fourcc_from_format (GST_VIDEO_INFO_FORMAT (info));
  mfx_info->ChromaFormat =
      gst_msdk_get_mfx_chroma_from_format (GST_VIDEO_INFO_FORMAT (info));

  return;
}
Hyunjun Ko's avatar
Hyunjun Ko committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

gboolean
gst_msdk_is_msdk_buffer (GstBuffer * buf)
{
  GstAllocator *allocator;
  GstMemory *mem = gst_buffer_peek_memory (buf, 0);

  allocator = GST_MEMORY_CAST (mem)->allocator;

  if (allocator && (GST_IS_MSDK_VIDEO_ALLOCATOR (allocator) ||
          GST_IS_MSDK_SYSTEM_ALLOCATOR (allocator)))
    return TRUE;
  else
    return FALSE;
}

mfxFrameSurface1 *
gst_msdk_get_surface_from_buffer (GstBuffer * buf)
{
  GstAllocator *allocator;
  GstMemory *mem = gst_buffer_peek_memory (buf, 0);

  allocator = GST_MEMORY_CAST (mem)->allocator;

  if (GST_IS_MSDK_VIDEO_ALLOCATOR (allocator))
    return GST_MSDK_VIDEO_MEMORY_CAST (mem)->surface;
  else
    return GST_MSDK_SYSTEM_MEMORY_CAST (mem)->surface;
}