summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapiutils.c
blob: 02a45350613efa5844ef2d5e00c72656a623cbae (plain)
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
39
40
41
42
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
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
/*
 *  gstvaapiutils.c - VA-API utilities
 *
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
 *  Copyright (C) 2011-2014 Intel Corporation
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "sysdeps.h"
#include "gstvaapicompat.h"
#include "gstvaapiutils.h"
#include "gstvaapisurface.h"
#include "gstvaapisubpicture.h"
#include "gstvaapifilter.h"
#include <stdio.h>
#include <stdarg.h>

#define DEBUG 1
#include "gstvaapidebug.h"

#define CONCAT(a, b)    CONCAT_(a, b)
#define CONCAT_(a, b)   a##b
#define STRINGIFY(x)    STRINGIFY_(x)
#define STRINGIFY_(x)   #x
#define STRCASEP(p, x)  STRCASE(CONCAT(p, x))
#define STRCASE(x)      case x: return STRINGIFY(x)

#if VA_CHECK_VERSION (0,40,0)
static gchar *
strip_msg (const char *message)
{
  gchar *msg;

  msg = g_strdup (message);
  if (!msg)
    return NULL;
  return g_strstrip (msg);
}

#if VA_CHECK_VERSION (1,0,0)
static void
gst_vaapi_err (void *data, const char *message)
{
  gchar *msg;

  msg = strip_msg (message);
  if (!msg)
    return;
  GST_ERROR ("%s", msg);
  g_free (msg);
}

static void
gst_vaapi_warning (void *data, const char *message)
{
  gchar *msg;

  msg = strip_msg (message);
  if (!msg)
    return;
  GST_WARNING ("%s", msg);
  g_free (msg);
}
#endif

static void
gst_vaapi_log (
#if VA_CHECK_VERSION (1,0,0)
    void *data,
#endif
    const char *message)
{
  gchar *msg;

  msg = strip_msg (message);
  if (!msg)
    return;
  GST_INFO ("%s", msg);
  g_free (msg);
}
#endif

gboolean
vaapi_initialize (VADisplay dpy)
{
  gint major_version, minor_version;
  VAStatus status;

#if VA_CHECK_VERSION (1,0,0)
  vaSetErrorCallback (dpy, gst_vaapi_warning, NULL);
  vaSetInfoCallback (dpy, gst_vaapi_log, NULL);
#elif VA_CHECK_VERSION (0,40,0)
  vaSetInfoCallback (gst_vaapi_log);
#endif

  status = vaInitialize (dpy, &major_version, &minor_version);

#if VA_CHECK_VERSION (1,0,0)
  vaSetErrorCallback (dpy, gst_vaapi_err, NULL);
#endif

  if (!vaapi_check_status (status, "vaInitialize()"))
    return FALSE;

  GST_INFO ("VA-API version %d.%d", major_version, minor_version);
  return TRUE;
}

/* Check VA status for success or print out an error */
gboolean
vaapi_check_status (VAStatus status, const gchar * msg)
{
  if (status != VA_STATUS_SUCCESS) {
    GST_DEBUG ("%s: %s", msg, vaErrorStr (status));
    return FALSE;
  }
  return TRUE;
}

/* Maps VA buffer */
gpointer
vaapi_map_buffer (VADisplay dpy, VABufferID buf_id)
{
  VAStatus status;
  gpointer data = NULL;

  status = vaMapBuffer (dpy, buf_id, &data);
  if (!vaapi_check_status (status, "vaMapBuffer()"))
    return NULL;
  return data;
}

/* Unmaps VA buffer */
void
vaapi_unmap_buffer (VADisplay dpy, VABufferID buf_id, gpointer * pbuf)
{
  VAStatus status;

  if (pbuf)
    *pbuf = NULL;

  status = vaUnmapBuffer (dpy, buf_id);
  if (!vaapi_check_status (status, "vaUnmapBuffer()"))
    return;
}

/* Creates and maps VA buffer */
gboolean
vaapi_create_buffer (VADisplay dpy, VAContextID ctx, int type, guint size,
    gconstpointer buf, VABufferID * buf_id_ptr, gpointer * mapped_data)
{
  VABufferID buf_id;
  VAStatus status;
  gpointer data = (gpointer) buf;

  status = vaCreateBuffer (dpy, ctx, type, size, 1, data, &buf_id);
  if (!vaapi_check_status (status, "vaCreateBuffer()"))
    return FALSE;

  if (mapped_data) {
    data = vaapi_map_buffer (dpy, buf_id);
    if (!data)
      goto error;
    *mapped_data = data;
  }

  *buf_id_ptr = buf_id;
  return TRUE;

  /* ERRORS */
error:
  {
    vaapi_destroy_buffer (dpy, &buf_id);
    return FALSE;
  }
}

/* Destroy VA buffer */
void
vaapi_destroy_buffer (VADisplay dpy, VABufferID * buf_id_ptr)
{
  if (!buf_id_ptr || *buf_id_ptr == VA_INVALID_ID)
    return;

  vaDestroyBuffer (dpy, *buf_id_ptr);
  *buf_id_ptr = VA_INVALID_ID;
}

/* Return a string representation of a VAProfile */
const gchar *
string_of_VAProfile (VAProfile profile)
{
  switch (profile) {
#define MAP(profile) \
        STRCASEP(VAProfile, profile)
      MAP (MPEG2Simple);
      MAP (MPEG2Main);
      MAP (MPEG4Simple);
      MAP (MPEG4AdvancedSimple);
      MAP (MPEG4Main);
      MAP (JPEGBaseline);
      MAP (H263Baseline);
      MAP (H264ConstrainedBaseline);
#if !VA_CHECK_VERSION(1,0,0)
      MAP (H264Baseline);
#endif
      MAP (H264Main);
      MAP (H264High);
      MAP (H264MultiviewHigh);
      MAP (H264StereoHigh);
#if VA_CHECK_VERSION(1,2,0)
      MAP (HEVCMain422_10);
      MAP (HEVCMain444);
#endif
      MAP (HEVCMain);
      MAP (HEVCMain10);
      MAP (VC1Simple);
      MAP (VC1Main);
      MAP (VC1Advanced);
      MAP (VP8Version0_3);
      MAP (VP9Profile0);
      MAP (VP9Profile1);
      MAP (VP9Profile2);
      MAP (VP9Profile3);
#undef MAP
    default:
      break;
  }
  return "<unknown>";
}

/* Return a string representation of a VAEntrypoint */
const gchar *
string_of_VAEntrypoint (VAEntrypoint entrypoint)
{
  switch (entrypoint) {
#define MAP(entrypoint) \
        STRCASEP(VAEntrypoint, entrypoint)
      MAP (VLD);
      MAP (IZZ);
      MAP (IDCT);
      MAP (MoComp);
      MAP (Deblocking);
#undef MAP
    default:
      break;
  }
  return "<unknown>";
}

/* Return a string representation of a VADisplayAttributeType */
const gchar *
string_of_VADisplayAttributeType (VADisplayAttribType attribute_type)
{
  switch (attribute_type) {
#define MAP(attribute_type) \
        STRCASEP(VADisplayAttrib, attribute_type)
      MAP (Brightness);
      MAP (Contrast);
      MAP (Hue);
      MAP (Saturation);
      MAP (BackgroundColor);
      MAP (Rotation);
      MAP (OutofLoopDeblock);
      MAP (CSCMatrix);
      MAP (BlendColor);
      MAP (OverlayAutoPaintColorKey);
      MAP (OverlayColorKey);
      MAP (RenderMode);
      MAP (RenderDevice);
      MAP (RenderRect);
#undef MAP
    default:
      break;
  }
  return "<unknown>";
}

/* Return a string representation of a VA chroma format */
const gchar *
string_of_va_chroma_format (guint chroma_format)
{
  switch (chroma_format) {
#define MAP(value) \
        STRCASEP(VA_RT_FORMAT_, value)
      MAP (YUV420);
      MAP (YUV422);
      MAP (YUV444);
      MAP (YUV400);
      MAP (RGB16);
      MAP (RGB32);
      MAP (RGBP);
      MAP (YUV420_10BPP);
#if VA_CHECK_VERSION(1,2,0)
      MAP (YUV422_10);
      MAP (YUV444_10);
      MAP (YUV420_12);
      MAP (YUV422_12);
      MAP (YUV444_12);
      MAP (RGB32_10);
#endif
#undef MAP
    default:
      break;
  }
  return "<unknown>";
}

const gchar *
string_of_VARateControl (guint rate_control)
{
  switch (rate_control) {
    case VA_RC_NONE:
      return "None";
#ifdef VA_RC_CQP
    case VA_RC_CQP:
      return "CQP";
#endif
    case VA_RC_CBR:
      return "CBR";
    case VA_RC_VCM:
      return "VCM";
    case VA_RC_VBR:
      return "VBR";
#ifdef VA_RC_VBR_CONSTRAINED
    case VA_RC_VBR_CONSTRAINED:
      return "VBR-Constrained";
#endif
    default:
      break;
  }
  return "<unknown>";
}

/**
 * to_GstVaapiChromaType:
 * @va_rt_format: the value of VAConfigAttribRTFormat
 *
 * Converts the VA_RT_FORMAT_* to #GstVaapiChromaType
 *
 * Returns: the #GstVaapiChromaType associated to @va_rt_format or
 * zero.
 **/
guint
to_GstVaapiChromaType (guint va_rt_format)
{
  guint chroma_type;

  switch (va_rt_format) {
    case VA_RT_FORMAT_YUV420:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
      break;
    case VA_RT_FORMAT_YUV422:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422;
      break;
    case VA_RT_FORMAT_YUV444:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444;
      break;
    case VA_RT_FORMAT_YUV411:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV411;
      break;
    case VA_RT_FORMAT_YUV400:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV400;
      break;
    case VA_RT_FORMAT_RGB32:
      chroma_type = GST_VAAPI_CHROMA_TYPE_RGB32;
      break;
    case VA_RT_FORMAT_RGB16:
      chroma_type = GST_VAAPI_CHROMA_TYPE_RGB16;
      break;
    case VA_RT_FORMAT_RGBP:
      chroma_type = GST_VAAPI_CHROMA_TYPE_RGBP;
      break;
    case VA_RT_FORMAT_YUV420_10BPP:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420_10BPP;
      break;
#if VA_CHECK_VERSION(1,2,0)
    case VA_RT_FORMAT_YUV422_10:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422_10BPP;
      break;
    case VA_RT_FORMAT_YUV444_10:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444_10BPP;
      break;
    case VA_RT_FORMAT_YUV420_12:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420_12BPP;
      break;
    case VA_RT_FORMAT_YUV422_12:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422_12BPP;
      break;
    case VA_RT_FORMAT_YUV444_12:
      chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444_12BPP;
      break;
    case VA_RT_FORMAT_RGB32_10:
      chroma_type = GST_VAAPI_CHROMA_TYPE_RGB32_10BPP;
      break;
#endif
    default:
      chroma_type = 0;
      break;
  }
  return chroma_type;
}

/**
 * from_GstVaapiChromaType:
 * @chroma_type: the #GstVaapiChromaType
 *
 * Converts #GstVaapiChromaType to a chroma format suitable for
 * vaCreateSurfaces().
 */
guint
from_GstVaapiChromaType (guint chroma_type)
{
  guint format;

  switch (chroma_type) {
    case GST_VAAPI_CHROMA_TYPE_YUV420:
      format = VA_RT_FORMAT_YUV420;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV422:
      format = VA_RT_FORMAT_YUV422;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV444:
      format = VA_RT_FORMAT_YUV444;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV411:
      format = VA_RT_FORMAT_YUV411;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV400:
      format = VA_RT_FORMAT_YUV400;
      break;
    case GST_VAAPI_CHROMA_TYPE_RGB32:
      format = VA_RT_FORMAT_RGB32;
      break;
    case GST_VAAPI_CHROMA_TYPE_RGB16:
      format = VA_RT_FORMAT_RGB16;
      break;
    case GST_VAAPI_CHROMA_TYPE_RGBP:
      format = VA_RT_FORMAT_RGBP;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV420_10BPP:
      format = VA_RT_FORMAT_YUV420_10BPP;
      break;
#if VA_CHECK_VERSION(1,2,0)
    case GST_VAAPI_CHROMA_TYPE_YUV422_10BPP:
      format = VA_RT_FORMAT_YUV422_10;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV444_10BPP:
      format = VA_RT_FORMAT_YUV444_10;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV420_12BPP:
      format = VA_RT_FORMAT_YUV420_12;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV422_12BPP:
      format = VA_RT_FORMAT_YUV422_12;
      break;
    case GST_VAAPI_CHROMA_TYPE_YUV444_12BPP:
      format = VA_RT_FORMAT_YUV444_12;
      break;
    case GST_VAAPI_CHROMA_TYPE_RGB32_10BPP:
      format = VA_RT_FORMAT_RGB32_10;
      break;
#endif
    default:
      format = 0;
      break;
  }
  return format;
}

/**
 * from_GstVaapiSubpictureFlags:
 * @flags: the #GstVaapiSubpictureFlags
 *
 * Converts #GstVaapiSubpictureFlags to flags suitable for
 * vaAssociateSubpicture().
 */
guint
from_GstVaapiSubpictureFlags (guint flags)
{
  guint va_flags = 0;

  if (flags & GST_VAAPI_SUBPICTURE_FLAG_GLOBAL_ALPHA)
    va_flags |= VA_SUBPICTURE_GLOBAL_ALPHA;
#ifdef VA_SUBPICTURE_PREMULTIPLIED_ALPHA
  if (flags & GST_VAAPI_SUBPICTURE_FLAG_PREMULTIPLIED_ALPHA)
    flags |= VA_SUBPICTURE_PREMULTIPLIED_ALPHA;
#endif
  return va_flags;
}

/**
 * to_GstVaapiSubpictureFlags:
 * @flags: the #GstVaapiSubpictureFlags flags to translate
 *
 * Converts vaQuerySubpictureFormats() @flags to #GstVaapiSubpictureFlags
 * flags.
 *
 * Return value: the #GstVaapiSubpictureFlags flags
 */
guint
to_GstVaapiSubpictureFlags (guint va_flags)
{
  guint flags = 0;

  if (va_flags & VA_SUBPICTURE_GLOBAL_ALPHA)
    flags |= GST_VAAPI_SUBPICTURE_FLAG_GLOBAL_ALPHA;
#ifdef VA_SUBPICTURE_PREMULTIPLIED_ALPHA
  if (va_flags & VA_SUBPICTURE_PREMULTIPLIED_ALPHA)
    flags |= GST_VAAPI_SUBPICTURE_FLAG_PREMULTIPLIED_ALPHA;
#endif
  return flags;
}

/**
 * from_GstVideoOverlayFormatFlags:
 * @flags: the #GstVideoOverlayFormatFlags flags to translate
 *
 * Converts #GstVaapiSubpictureFlags to #GstVaapiSubpictureFlags.
 *
 * Return value: the #GstVaapiSubpictureFlags flags
 */
guint
from_GstVideoOverlayFormatFlags (guint ovl_flags)
{
  guint flags = 0;

  if (ovl_flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
    flags |= GST_VAAPI_SUBPICTURE_FLAG_PREMULTIPLIED_ALPHA;
  if (ovl_flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
    flags |= GST_VAAPI_SUBPICTURE_FLAG_GLOBAL_ALPHA;
  return flags;
}

/**
 * to_GstVideoOverlayFormatFlags:
 * @flags: the #GstVaapiSubpictureFlags flags to translate
 *
 * Converts #GstVaapiSubpictureFlags to #GstVideoOverlayFormatFlags.
 *
 * Return value: the #GstVideoOverlayFormatFlags flags
 */
guint
to_GstVideoOverlayFormatFlags (guint flags)
{
  guint ovl_flags = 0;

  if (flags & GST_VAAPI_SUBPICTURE_FLAG_PREMULTIPLIED_ALPHA)
    ovl_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
  if (flags & GST_VAAPI_SUBPICTURE_FLAG_GLOBAL_ALPHA)
    ovl_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
  return ovl_flags;
}

/**
 * from_GstVaapiSurfaceRenderFlags:
 * @flags: the #GstVaapiSurfaceRenderFlags
 *
 * Converts #GstVaapiSurfaceRenderFlags to flags suitable for
 * vaPutSurface().
 */
guint
from_GstVaapiSurfaceRenderFlags (guint flags)
{
  guint va_fields, va_csc;

  /* Picture structure */
  switch (flags & GST_VAAPI_PICTURE_STRUCTURE_MASK) {
    case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
      va_fields = VA_TOP_FIELD;
      break;
    case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
      va_fields = VA_BOTTOM_FIELD;
      break;
    default:
      va_fields = VA_FRAME_PICTURE;
      break;
  }

  /* Color standard */
  switch (flags & GST_VAAPI_COLOR_STANDARD_MASK) {
#ifdef VA_SRC_BT601
    case GST_VAAPI_COLOR_STANDARD_ITUR_BT_601:
      va_csc = VA_SRC_BT601;
      break;
#endif
#ifdef VA_SRC_BT709
    case GST_VAAPI_COLOR_STANDARD_ITUR_BT_709:
      va_csc = VA_SRC_BT709;
      break;
#endif
#ifdef VA_SRC_SMPTE_240
    case GST_VAAPI_COLOR_STANDARD_SMPTE_240M:
      va_csc = VA_SRC_SMPTE_240;
      break;
#endif
    default:
      va_csc = 0;
      break;
  }
  return va_fields | va_csc;
}

/**
 * to_GstVaapiSurfaceStatus:
 * @flags: the #GstVaapiSurfaceStatus flags to translate
 *
 * Converts vaQuerySurfaceStatus() @flags to #GstVaapiSurfaceStatus
 * flags.
 *
 * Return value: the #GstVaapiSurfaceStatus flags
 */
guint
to_GstVaapiSurfaceStatus (guint va_flags)
{
  guint flags;
  const guint va_flags_mask = (VASurfaceReady |
      VASurfaceRendering | VASurfaceDisplaying);

  /* Check for core status */
  switch (va_flags & va_flags_mask) {
    case VASurfaceReady:
      flags = GST_VAAPI_SURFACE_STATUS_IDLE;
      break;
    case VASurfaceRendering:
      flags = GST_VAAPI_SURFACE_STATUS_RENDERING;
      break;
    case VASurfaceDisplaying:
      flags = GST_VAAPI_SURFACE_STATUS_DISPLAYING;
      break;
    default:
      flags = 0;
      break;
  }

  /* Check for encoder status */
  if (va_flags & VASurfaceSkipped)
    flags |= GST_VAAPI_SURFACE_STATUS_SKIPPED;
  return flags;
}

/* Translate GstVaapiRotation value to VA-API rotation value */
guint
from_GstVaapiRotation (guint value)
{
  switch (value) {
    case GST_VAAPI_ROTATION_0:
      return VA_ROTATION_NONE;
    case GST_VAAPI_ROTATION_90:
      return VA_ROTATION_90;
    case GST_VAAPI_ROTATION_180:
      return VA_ROTATION_180;
    case GST_VAAPI_ROTATION_270:
      return VA_ROTATION_270;
  }
  GST_ERROR ("unsupported GstVaapiRotation value %d", value);
  return VA_ROTATION_NONE;
}

/* Translate VA-API rotation value to GstVaapiRotation value */
guint
to_GstVaapiRotation (guint value)
{
  switch (value) {
    case VA_ROTATION_NONE:
      return GST_VAAPI_ROTATION_0;
    case VA_ROTATION_90:
      return GST_VAAPI_ROTATION_90;
    case VA_ROTATION_180:
      return GST_VAAPI_ROTATION_180;
    case VA_ROTATION_270:
      return GST_VAAPI_ROTATION_270;
  }
  GST_ERROR ("unsupported VA-API rotation value %d", value);
  return GST_VAAPI_ROTATION_0;
}

guint
from_GstVaapiRateControl (guint value)
{
  switch (value) {
    case GST_VAAPI_RATECONTROL_NONE:
      return VA_RC_NONE;
#ifdef VA_RC_CQP
    case GST_VAAPI_RATECONTROL_CQP:
      return VA_RC_CQP;
#endif
    case GST_VAAPI_RATECONTROL_CBR:
      return VA_RC_CBR;
    case GST_VAAPI_RATECONTROL_VCM:
      return VA_RC_VCM;
    case GST_VAAPI_RATECONTROL_VBR:
      return VA_RC_VBR;
#ifdef VA_RC_VBR_CONSTRAINED
    case GST_VAAPI_RATECONTROL_VBR_CONSTRAINED:
      return VA_RC_VBR_CONSTRAINED;
#endif
#ifdef VA_RC_MB
    case GST_VAAPI_RATECONTROL_MB:
      return VA_RC_MB;
#endif
  }
  GST_ERROR ("unsupported GstVaapiRateControl value %u", value);
  return VA_RC_NONE;
}

guint
to_GstVaapiRateControl (guint value)
{
  switch (value) {
    case VA_RC_NONE:
      return GST_VAAPI_RATECONTROL_NONE;
#ifdef VA_RC_CQP
    case VA_RC_CQP:
      return GST_VAAPI_RATECONTROL_CQP;
#endif
    case VA_RC_CBR:
      return GST_VAAPI_RATECONTROL_CBR;
    case VA_RC_VCM:
      return GST_VAAPI_RATECONTROL_VCM;
    case VA_RC_VBR:
      return GST_VAAPI_RATECONTROL_VBR;
#ifdef VA_RC_VBR_CONSTRAINED
    case VA_RC_VBR_CONSTRAINED:
      return GST_VAAPI_RATECONTROL_VBR_CONSTRAINED;
#endif
#ifdef VA_RC_MB
    case VA_RC_MB:
      return GST_VAAPI_RATECONTROL_MB;
#endif
  }
  GST_ERROR ("unsupported VA-API Rate Control value %u", value);
  return GST_VAAPI_RATECONTROL_NONE;
}

/* VPP: translate GstVaapiDeinterlaceMethod to VA deinterlacing algorithm */
guint
from_GstVaapiDeinterlaceMethod (guint value)
{
  switch (value) {
    case GST_VAAPI_DEINTERLACE_METHOD_NONE:
      return 0;
#if USE_VA_VPP
    case GST_VAAPI_DEINTERLACE_METHOD_BOB:
      return VAProcDeinterlacingBob;
    case GST_VAAPI_DEINTERLACE_METHOD_WEAVE:
      return VAProcDeinterlacingWeave;
    case GST_VAAPI_DEINTERLACE_METHOD_MOTION_ADAPTIVE:
      return VAProcDeinterlacingMotionAdaptive;
    case GST_VAAPI_DEINTERLACE_METHOD_MOTION_COMPENSATED:
      return VAProcDeinterlacingMotionCompensated;
#endif
  }
  GST_ERROR ("unsupported GstVaapiDeinterlaceMethod value %d", value);
  return 0;
}

/* VPP: translate GstVaapiDeinterlaceFlags into VA deinterlacing flags */
guint
from_GstVaapiDeinterlaceFlags (guint flags)
{
  guint va_flags = 0;

#if USE_VA_VPP
  if (!(flags & GST_VAAPI_DEINTERLACE_FLAG_TFF))
    va_flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;

  if (flags & GST_VAAPI_DEINTERLACE_FLAG_ONEFIELD)
    va_flags |= VA_DEINTERLACING_ONE_FIELD;

  if (!(flags & GST_VAAPI_DEINTERLACE_FLAG_TOPFIELD))
    va_flags |= VA_DEINTERLACING_BOTTOM_FIELD;
#endif
  return va_flags;
}

/* VPP: translate GstVaapiScaleMethod into VA scaling flags */
guint
from_GstVaapiScaleMethod (guint value)
{
  guint va_flags;

  switch (value) {
    case GST_VAAPI_SCALE_METHOD_DEFAULT:
      va_flags = VA_FILTER_SCALING_DEFAULT;
      break;
    case GST_VAAPI_SCALE_METHOD_FAST:
      va_flags = VA_FILTER_SCALING_FAST;
      break;
    case GST_VAAPI_SCALE_METHOD_HQ:
      va_flags = VA_FILTER_SCALING_HQ;
      break;
    default:
      va_flags = 0;
      break;
  }
  return va_flags;
}

/* VPP: translate VA scaling flags into GstVaapiScale Method */
guint
to_GstVaapiScaleMethod (guint flags)
{
  GstVaapiScaleMethod method;

  switch (flags) {
    case VA_FILTER_SCALING_FAST:
      method = GST_VAAPI_SCALE_METHOD_FAST;
      break;
    case VA_FILTER_SCALING_HQ:
      method = GST_VAAPI_SCALE_METHOD_HQ;
      break;
    default:
      method = GST_VAAPI_SCALE_METHOD_DEFAULT;
      break;
  }
  return method;
}