forked from i-rinat/libvdpau-va-gl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdpau-soft.c
3015 lines (2621 loc) · 113 KB
/
vdpau-soft.c
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
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
/*
* Copyright 2013 Rinat Ibragimov
*
* This file is part of libvdpau-va-gl
*
* libvdpau-va-gl distributed under the terms of LGPLv3. See COPYING for details.
*/
#define _XOPEN_SOURCE
#define GL_GLEXT_PROTOTYPES
#include <assert.h>
#include <glib.h>
#include <libswscale/swscale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <va/va.h>
#include <va/va_glx.h>
#include <vdpau/vdpau.h>
#include <vdpau/vdpau_x11.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include "bitstream.h"
#include "ctx-stack.h"
#include "h264-parse.h"
#include "reverse-constant.h"
#include "handle-storage.h"
#include "vdpau-trace.h"
#include "vdpau-locking.h"
#include "watermark.h"
#include "globals.h"
#define MAX_RENDER_TARGETS 21
#define NUM_RENDER_TARGETS_H264 21
#define DESCRIBE(xparam, format) fprintf(stderr, #xparam " = %" #format "\n", xparam)
static char const *
implemetation_description_string = "OpenGL/VAAPI/libswscale backend for VDPAU";
/** @brief VdpPresentationQueueTarget object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
int refcount;
Drawable drawable; ///< X drawable to output to
GLXContext glc; ///< GL context used for output
} VdpPresentationQueueTargetData;
/** @brief VdpPresentationQueue object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
VdpPresentationQueueTargetData *target;
VdpColor bg_color; ///< background color
} VdpPresentationQueueData;
/** @brief VdpVideoMixer object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
} VdpVideoMixerData;
/** @brief VdpOutputSurface object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
VdpRGBAFormat rgba_format; ///< RGBA format of data stored
GLuint tex_id; ///< associated GL texture id
GLuint fbo_id; ///< framebuffer object id
uint32_t width;
uint32_t height;
GLuint gl_internal_format; ///< GL texture format: internal format
GLuint gl_format; ///< GL texture format: preferred external format
GLuint gl_type; ///< GL texture format: pixel type
unsigned int bytes_per_pixel; ///< number of bytes per pixel
} VdpOutputSurfaceData;
/** @brief VdpVideoSurface object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
VdpChromaType chroma_type; ///< video chroma type
uint32_t width;
uint32_t stride; ///< distance between first pixels of two consecutive rows (in pixels)
uint32_t height;
void *y_plane; ///< luma data (software)
void *v_plane; ///< chroma data (software)
void *u_plane; ///< chroma data (software)
VASurfaceID va_surf; ///< VA-API surface
void *va_glx; ///< handle for VA-API/GLX interaction
GLuint tex_id; ///< GL texture id (RGBA)
} VdpVideoSurfaceData;
/** @brief VdpBitmapSurface object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
VdpRGBAFormat rgba_format; ///< RGBA format of data stored
GLuint tex_id; ///< GL texture id
uint32_t width;
uint32_t height;
VdpBool frequently_accessed;///< 1 if surface should be optimized for frequent access
unsigned int bytes_per_pixel; ///< number of bytes per bitmap pixel
GLuint gl_internal_format; ///< GL texture format: internal format
GLuint gl_format; ///< GL texture format: preferred external format
GLuint gl_type; ///< GL texture format: pixel type
char *bitmap_data; ///< system-memory buffer for frequently accessed bitmaps
int dirty; ///< dirty flag. True if system-memory buffer contains data
///< newer than GPU texture contents
} VdpBitmapSurfaceData;
/** @brief VdpDecoder object parameters */
typedef struct {
HandleType type; ///< handle type
VdpDeviceData *device; ///< link to parent
VdpDecoderProfile profile; ///< decoder profile
uint32_t width;
uint32_t height;
uint32_t max_references; ///< maximum count of reference frames
VAConfigID config_id; ///< VA-API config id
VASurfaceID render_targets[MAX_RENDER_TARGETS]; ///< spare VA surfaces
uint32_t num_render_targets;
uint32_t next_surface_idx; ///< next free surface in render_targets
VAContextID context_id; ///< VA-API context id
} VdpDecoderData;
static
uint32_t
chroma_storage_size_divider(VdpChromaType chroma_type)
{
switch (chroma_type) {
case VDP_CHROMA_TYPE_420: return 4;
case VDP_CHROMA_TYPE_422: return 2;
case VDP_CHROMA_TYPE_444: return 1;
default: return 1;
}
}
static
const char *
softVdpGetErrorString(VdpStatus status)
{
return reverse_status(status);
}
VdpStatus
softVdpGetApiVersion(uint32_t *api_version)
{
*api_version = VDPAU_VERSION;
return VDP_STATUS_OK;
}
VdpStatus
softVdpDecoderQueryCapabilities(VdpDevice device, VdpDecoderProfile profile, VdpBool *is_supported,
uint32_t *max_level, uint32_t *max_macroblocks,
uint32_t *max_width, uint32_t *max_height)
{
VdpDeviceData *deviceData = handlestorage_get(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
if (NULL == is_supported || NULL == max_level || NULL == max_macroblocks ||
NULL == max_width || NULL == max_height)
{
return VDP_STATUS_INVALID_POINTER;
}
*max_level = 0;
*max_macroblocks = 0;
*max_width = 0;
*max_height = 0;
if (! deviceData->va_available) {
*is_supported = 0;
return VDP_STATUS_OK;
}
VAProfile *va_profile_list = malloc(sizeof(VAProfile) * vaMaxNumProfiles(deviceData->va_dpy));
if (NULL == va_profile_list)
return VDP_STATUS_RESOURCES;
int num_profiles;
VAStatus status = vaQueryConfigProfiles(deviceData->va_dpy, va_profile_list, &num_profiles);
if (VA_STATUS_SUCCESS != status) {
free(va_profile_list);
return VDP_STATUS_ERROR;
}
struct {
int mpeg2_simple;
int mpeg2_main;
int h264_baseline;
int h264_main;
int h264_high;
int vc1_simple;
int vc1_main;
int vc1_advanced;
} available_profiles = {0, 0, 0, 0, 0, 0, 0, 0};
for (int k = 0; k < num_profiles; k ++) {
switch (va_profile_list[k]) {
case VAProfileMPEG2Main:
available_profiles.mpeg2_main = 1;
/* fall through */
case VAProfileMPEG2Simple:
available_profiles.mpeg2_simple = 1;
break;
case VAProfileH264High:
available_profiles.h264_high = 1;
/* fall through */
case VAProfileH264Main:
available_profiles.h264_main = 1;
/* fall through */
case VAProfileH264Baseline:
available_profiles.h264_baseline = 1;
/* fall though */
case VAProfileH264ConstrainedBaseline:
break;
case VAProfileVC1Advanced:
available_profiles.vc1_advanced = 1;
/* fall though */
case VAProfileVC1Main:
available_profiles.vc1_main = 1;
/* fall though */
case VAProfileVC1Simple:
available_profiles.vc1_simple = 1;
break;
// unhandled profiles
case VAProfileH263Baseline:
case VAProfileJPEGBaseline:
default:
// do nothing
break;
}
}
free(va_profile_list);
*is_supported = 0;
// TODO: How to determine max width and height width libva?
*max_width = 2048;
*max_height = 2048;
*max_macroblocks = 16384;
switch (profile) {
case VDP_DECODER_PROFILE_MPEG2_SIMPLE:
*is_supported = available_profiles.mpeg2_simple;
*max_level = VDP_DECODER_LEVEL_MPEG2_HL;
break;
case VDP_DECODER_PROFILE_MPEG2_MAIN:
*is_supported = available_profiles.mpeg2_main;
*max_level = VDP_DECODER_LEVEL_MPEG2_HL;
break;
case VDP_DECODER_PROFILE_H264_BASELINE:
*is_supported = available_profiles.h264_baseline;
// TODO: Do underlying libva really support 5.1?
*max_level = VDP_DECODER_LEVEL_H264_5_1;
break;
case VDP_DECODER_PROFILE_H264_MAIN:
*is_supported = available_profiles.h264_main;
*max_level = VDP_DECODER_LEVEL_H264_5_1;
break;
case VDP_DECODER_PROFILE_H264_HIGH:
*is_supported = available_profiles.h264_high;
*max_level = VDP_DECODER_LEVEL_H264_5_1;
break;
case VDP_DECODER_PROFILE_VC1_SIMPLE:
*is_supported = available_profiles.vc1_simple;
*max_level = VDP_DECODER_LEVEL_VC1_SIMPLE_MEDIUM;
break;
case VDP_DECODER_PROFILE_VC1_MAIN:
*is_supported = available_profiles.vc1_main;
*max_level = VDP_DECODER_LEVEL_VC1_MAIN_HIGH;
break;
case VDP_DECODER_PROFILE_VC1_ADVANCED:
*is_supported = available_profiles.vc1_advanced;
*max_level = VDP_DECODER_LEVEL_VC1_ADVANCED_L4;
break;
// unsupported
case VDP_DECODER_PROFILE_MPEG1:
case VDP_DECODER_PROFILE_MPEG4_PART2_SP:
case VDP_DECODER_PROFILE_MPEG4_PART2_ASP:
case VDP_DECODER_PROFILE_DIVX4_QMOBILE:
case VDP_DECODER_PROFILE_DIVX4_MOBILE:
case VDP_DECODER_PROFILE_DIVX4_HOME_THEATER:
case VDP_DECODER_PROFILE_DIVX4_HD_1080P:
case VDP_DECODER_PROFILE_DIVX5_QMOBILE:
case VDP_DECODER_PROFILE_DIVX5_MOBILE:
case VDP_DECODER_PROFILE_DIVX5_HOME_THEATER:
case VDP_DECODER_PROFILE_DIVX5_HD_1080P:
default:
break;
}
return VDP_STATUS_OK;
}
VdpStatus
softVdpDecoderCreate(VdpDevice device, VdpDecoderProfile profile, uint32_t width, uint32_t height,
uint32_t max_references, VdpDecoder *decoder)
{
VdpStatus retval = VDP_STATUS_ERROR;
VdpDeviceData *deviceData = handlestorage_get(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
if (!deviceData->va_available)
return VDP_STATUS_INVALID_DECODER_PROFILE;
VADisplay va_dpy = deviceData->va_dpy;
VdpDecoderData *data = calloc(1, sizeof(VdpDecoderData));
if (NULL == data)
return VDP_STATUS_RESOURCES;
data->type = HANDLETYPE_DECODER;
data->device = deviceData;
data->profile = profile;
data->width = width;
data->height = height;
data->max_references = max_references;
data->next_surface_idx = 0;
VAProfile va_profile;
VAStatus status;
int final_try = 0;
VdpDecoderProfile next_profile = profile;
// Try to create decoder for asked profile. On failure try to create more advanced one
while (! final_try) {
profile = next_profile;
switch (profile) {
case VDP_DECODER_PROFILE_H264_BASELINE:
va_profile = VAProfileH264Baseline;
data->num_render_targets = NUM_RENDER_TARGETS_H264;
next_profile = VDP_DECODER_PROFILE_H264_MAIN;
break;
case VDP_DECODER_PROFILE_H264_MAIN:
va_profile = VAProfileH264Main;
data->num_render_targets = NUM_RENDER_TARGETS_H264;
next_profile = VDP_DECODER_PROFILE_H264_HIGH;
break;
case VDP_DECODER_PROFILE_H264_HIGH:
va_profile = VAProfileH264High;
data->num_render_targets = NUM_RENDER_TARGETS_H264;
// there is no more advanced profile, so it's final try
final_try = 1;
break;
default:
traceError("error (softVdpDecoderCreate): decoder %s not implemented\n",
reverse_decoder_profile(profile));
retval = VDP_STATUS_INVALID_DECODER_PROFILE;
goto error;
}
status = vaCreateConfig(va_dpy, va_profile, VAEntrypointVLD, NULL, 0, &data->config_id);
if (VA_STATUS_SUCCESS == status) // break loop if decoder created
break;
}
if (VA_STATUS_SUCCESS != status)
goto error;
// Create surfaces. All video surfaces created here, rather than in VdpVideoSurfaceCreate.
// VAAPI requires surfaces to be bound with context on its creation time, while VDPAU allows
// to do it later. So here is a trick: VDP video surfaces get their va_surf dynamically in
// DecoderRender.
// TODO: check format of surfaces created
status = vaCreateSurfaces(va_dpy, width, height, VA_RT_FORMAT_YUV420,
data->num_render_targets, data->render_targets);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaCreateContext(va_dpy, data->config_id, width, height, VA_PROGRESSIVE,
data->render_targets, data->num_render_targets, &data->context_id);
if (VA_STATUS_SUCCESS != status)
goto error;
deviceData->refcount ++;
*decoder = handlestorage_add(data);
return VDP_STATUS_OK;
error:
free(data);
return retval;
}
VdpStatus
softVdpDecoderDestroy(VdpDecoder decoder)
{
VdpDecoderData *decoderData = handlestorage_get(decoder, HANDLETYPE_DECODER);
if (NULL == decoderData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = decoderData->device;
if (deviceData->va_available) {
VADisplay va_dpy = deviceData->va_dpy;
vaDestroySurfaces(va_dpy, decoderData->render_targets, decoderData->num_render_targets);
vaDestroyContext(va_dpy, decoderData->context_id);
vaDestroyConfig(va_dpy, decoderData->config_id);
}
handlestorage_expunge(decoder);
deviceData->refcount --;
free(decoderData);
return VDP_STATUS_OK;
}
VdpStatus
softVdpDecoderGetParameters(VdpDecoder decoder, VdpDecoderProfile *profile,
uint32_t *width, uint32_t *height)
{
VdpDecoderData *decoderData = handlestorage_get(decoder, HANDLETYPE_DECODER);
if (NULL == decoderData)
return VDP_STATUS_INVALID_HANDLE;
if (NULL == profile || NULL == width || NULL == height)
return VDP_STATUS_INVALID_HANDLE;
*profile = decoderData->profile;
*width = decoderData->width;
*height = decoderData->height;
return VDP_STATUS_OK;
}
static
VdpStatus
h264_translate_reference_frames(VdpVideoSurfaceData *dstSurfData, VdpDecoderData *decoderData,
VAPictureParameterBufferH264 *pic_param,
const VdpPictureInfoH264 *vdppi)
{
// take new VA surface from buffer if needed
if (VA_INVALID_SURFACE == dstSurfData->va_surf) {
if (decoderData->next_surface_idx >= decoderData->num_render_targets)
return VDP_STATUS_RESOURCES;
dstSurfData->va_surf = decoderData->render_targets[decoderData->next_surface_idx];
decoderData->next_surface_idx ++;
}
// current frame
pic_param->CurrPic.picture_id = dstSurfData->va_surf;
pic_param->CurrPic.frame_idx = vdppi->frame_num;
pic_param->CurrPic.flags = vdppi->is_reference ? VA_PICTURE_H264_SHORT_TERM_REFERENCE : 0;
if (vdppi->field_pic_flag) {
pic_param->CurrPic.flags |=
vdppi->bottom_field_flag ? VA_PICTURE_H264_BOTTOM_FIELD : VA_PICTURE_H264_TOP_FIELD;
}
pic_param->CurrPic.TopFieldOrderCnt = vdppi->field_order_cnt[0];
pic_param->CurrPic.BottomFieldOrderCnt = vdppi->field_order_cnt[1];
// mark all pictures invalid preliminary
for (int k = 0; k < 16; k ++)
reset_va_picture_h264(&pic_param->ReferenceFrames[k]);
// reference frames
for (int k = 0; k < vdppi->num_ref_frames; k ++) {
if (VDP_INVALID_HANDLE == vdppi->referenceFrames[k].surface) {
reset_va_picture_h264(&pic_param->ReferenceFrames[k]);
continue;
}
VdpReferenceFrameH264 const *vdp_ref = &(vdppi->referenceFrames[k]);
VdpVideoSurfaceData *vdpSurfData =
handlestorage_get(vdp_ref->surface, HANDLETYPE_VIDEO_SURFACE);
VAPictureH264 *va_ref = &(pic_param->ReferenceFrames[k]);
if (NULL == vdpSurfData) {
traceError("error (h264_translate_reference_frames): NULL == vdpSurfData");
return VDP_STATUS_ERROR;
}
// take new VA surface from buffer if needed
if (VA_INVALID_SURFACE == vdpSurfData->va_surf) {
if (decoderData->next_surface_idx >= decoderData->num_render_targets)
return VDP_STATUS_RESOURCES;
vdpSurfData->va_surf = decoderData->render_targets[decoderData->next_surface_idx];
decoderData->next_surface_idx ++;
}
va_ref->picture_id = vdpSurfData->va_surf;
va_ref->frame_idx = vdp_ref->frame_idx;
va_ref->flags = vdp_ref->is_long_term ? VA_PICTURE_H264_LONG_TERM_REFERENCE
: VA_PICTURE_H264_SHORT_TERM_REFERENCE;
if (vdp_ref->top_is_reference && vdp_ref->bottom_is_reference) {
// Full frame. This block intentionally left blank. No flags set.
} else {
if (vdp_ref->top_is_reference)
va_ref->flags |= VA_PICTURE_H264_TOP_FIELD;
else
va_ref->flags |= VA_PICTURE_H264_BOTTOM_FIELD;
}
va_ref->TopFieldOrderCnt = vdp_ref->field_order_cnt[0];
va_ref->BottomFieldOrderCnt = vdp_ref->field_order_cnt[1];
}
return VDP_STATUS_OK;
}
static
void
h264_translate_pic_param(VAPictureParameterBufferH264 *pic_param, uint32_t width, uint32_t height,
const VdpPictureInfoH264 *vdppi, uint32_t level)
{
pic_param->picture_width_in_mbs_minus1 = (width - 1) / 16;
pic_param->picture_height_in_mbs_minus1 = (height - 1) / 16;
pic_param->bit_depth_luma_minus8 = 0; // TODO: deal with more than 8 bits
pic_param->bit_depth_chroma_minus8 = 0; // same for luma
pic_param->num_ref_frames = vdppi->num_ref_frames;
#define SEQ_FIELDS(fieldname) pic_param->seq_fields.bits.fieldname
#define PIC_FIELDS(fieldname) pic_param->pic_fields.bits.fieldname
SEQ_FIELDS(chroma_format_idc) = 1; // TODO: not only YUV420
SEQ_FIELDS(residual_colour_transform_flag) = 0;
SEQ_FIELDS(gaps_in_frame_num_value_allowed_flag)= 0;
SEQ_FIELDS(frame_mbs_only_flag) = vdppi->frame_mbs_only_flag;
SEQ_FIELDS(mb_adaptive_frame_field_flag) = vdppi->mb_adaptive_frame_field_flag;
SEQ_FIELDS(direct_8x8_inference_flag) = vdppi->direct_8x8_inference_flag;
SEQ_FIELDS(MinLumaBiPredSize8x8) = (level >= 31);
SEQ_FIELDS(log2_max_frame_num_minus4) = vdppi->log2_max_frame_num_minus4;
SEQ_FIELDS(pic_order_cnt_type) = vdppi->pic_order_cnt_type;
SEQ_FIELDS(log2_max_pic_order_cnt_lsb_minus4) = vdppi->log2_max_pic_order_cnt_lsb_minus4;
SEQ_FIELDS(delta_pic_order_always_zero_flag) = vdppi->delta_pic_order_always_zero_flag;
pic_param->num_slice_groups_minus1 = vdppi->slice_count - 1; // ???
pic_param->slice_group_map_type = 0; // ???
pic_param->slice_group_change_rate_minus1 = 0; // ???
pic_param->pic_init_qp_minus26 = vdppi->pic_init_qp_minus26;
pic_param->pic_init_qs_minus26 = 0; // ???
pic_param->chroma_qp_index_offset = vdppi->chroma_qp_index_offset;
pic_param->second_chroma_qp_index_offset = vdppi->second_chroma_qp_index_offset;
PIC_FIELDS(entropy_coding_mode_flag) = vdppi->entropy_coding_mode_flag;
PIC_FIELDS(weighted_pred_flag) = vdppi->weighted_pred_flag;
PIC_FIELDS(weighted_bipred_idc) = vdppi->weighted_bipred_idc;
PIC_FIELDS(transform_8x8_mode_flag) = vdppi->transform_8x8_mode_flag;
PIC_FIELDS(field_pic_flag) = vdppi->field_pic_flag;
PIC_FIELDS(constrained_intra_pred_flag) = vdppi->constrained_intra_pred_flag;
PIC_FIELDS(pic_order_present_flag) = vdppi->pic_order_present_flag;
PIC_FIELDS(deblocking_filter_control_present_flag) = vdppi->deblocking_filter_control_present_flag;
PIC_FIELDS(redundant_pic_cnt_present_flag) = vdppi->redundant_pic_cnt_present_flag;
PIC_FIELDS(reference_pic_flag) = vdppi->is_reference;
pic_param->frame_num = vdppi->frame_num;
#undef SEQ_FIELDS
#undef PIC_FIELDS
}
static
void
h264_translate_iq_matrix(VAIQMatrixBufferH264 *iq_matrix, const VdpPictureInfoH264 *vdppi)
{
for (int j = 0; j < 6; j ++)
for (int k = 0; k < 16; k ++)
iq_matrix->ScalingList4x4[j][k] = vdppi->scaling_lists_4x4[j][k];
for (int j = 0; j < 2; j ++)
for (int k = 0; k < 64; k ++)
iq_matrix->ScalingList8x8[j][k] = vdppi->scaling_lists_8x8[j][k];
}
VdpStatus
softVdpDecoderRender(VdpDecoder decoder, VdpVideoSurface target,
VdpPictureInfo const *picture_info, uint32_t bitstream_buffer_count,
VdpBitstreamBuffer const *bitstream_buffers)
{
VdpDecoderData *decoderData = handlestorage_get(decoder, HANDLETYPE_DECODER);
VdpVideoSurfaceData *dstSurfData = handlestorage_get(target, HANDLETYPE_VIDEO_SURFACE);
if (NULL == decoderData || NULL == dstSurfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = decoderData->device;
VADisplay va_dpy = deviceData->va_dpy;
VAStatus status;
VdpStatus vs;
if (VDP_DECODER_PROFILE_H264_BASELINE == decoderData->profile ||
VDP_DECODER_PROFILE_H264_MAIN == decoderData->profile ||
VDP_DECODER_PROFILE_H264_HIGH == decoderData->profile)
{
VdpPictureInfoH264 const *vdppi = (void *)picture_info;
// TODO: figure out where to get level
uint32_t level = 41;
// preparing picture parameters
VABufferID pic_param_buf;
VAPictureParameterBufferH264 *pic_param;
status = vaCreateBuffer(va_dpy, decoderData->context_id, VAPictureParameterBufferType,
sizeof(VAPictureParameterBufferH264), 1, NULL, &pic_param_buf);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaMapBuffer(va_dpy, pic_param_buf, (void **)&pic_param);
if (VA_STATUS_SUCCESS != status)
goto error;
vs = h264_translate_reference_frames(dstSurfData, decoderData, pic_param, vdppi);
if (VDP_STATUS_RESOURCES == vs)
goto error_no_surfaces_left;
if (VDP_STATUS_OK != vs)
goto error;
h264_translate_pic_param(pic_param, decoderData->width, decoderData->height, vdppi, level);
vaUnmapBuffer(va_dpy, pic_param_buf);
// IQ Matrix
VABufferID iq_matrix_buf;
VAIQMatrixBufferH264 *iq_matrix;
status = vaCreateBuffer(va_dpy, decoderData->context_id, VAIQMatrixBufferType,
sizeof(VAIQMatrixBufferH264), 1, NULL, &iq_matrix_buf);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaMapBuffer(va_dpy, iq_matrix_buf, (void **)&iq_matrix);
if (VA_STATUS_SUCCESS != status)
goto error;
h264_translate_iq_matrix(iq_matrix, vdppi);
vaUnmapBuffer(va_dpy, iq_matrix_buf);
// send data to decoding hardware
status = vaBeginPicture(va_dpy, decoderData->context_id, dstSurfData->va_surf);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaRenderPicture(va_dpy, decoderData->context_id, &pic_param_buf, 1);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaRenderPicture(va_dpy, decoderData->context_id, &iq_matrix_buf, 1);
if (VA_STATUS_SUCCESS != status)
goto error;
vaDestroyBuffer(va_dpy, pic_param_buf);
vaDestroyBuffer(va_dpy, iq_matrix_buf);
// merge bitstream buffers
int total_bitstream_bytes = 0;
for (unsigned int k = 0; k < bitstream_buffer_count; k ++)
total_bitstream_bytes += bitstream_buffers[k].bitstream_bytes;
uint8_t *merged_bitstream = malloc(total_bitstream_bytes);
if (NULL == merged_bitstream)
goto error_resources;
do {
unsigned char *ptr = merged_bitstream;
for (unsigned int k = 0; k < bitstream_buffer_count; k ++) {
memcpy(ptr, bitstream_buffers[k].bitstream, bitstream_buffers[k].bitstream_bytes);
ptr += bitstream_buffers[k].bitstream_bytes;
}
} while(0);
// Slice parameters
// All slice data have been merged into one continuous buffer. But we must supply
// slices one by one to the hardware decoder, so we need to delimit them. VDPAU
// requires bitstream buffers to include slice start code (0x00 0x00 0x01). Those
// will be used to calculate offsets and sizes of slice data in code below.
rbsp_state_t st_g; // reference, global state
rbsp_attach_buffer(&st_g, merged_bitstream, total_bitstream_bytes);
int nal_offset = rbsp_navigate_to_nal_unit(&st_g);
if (nal_offset < 0)
goto error_no_nal_header;
do {
VASliceParameterBufferH264 sp_h264;
memset(&sp_h264, 0, sizeof(VASliceParameterBufferH264));
// make a copy of global rbsp state for using in slice header parser
rbsp_state_t st = rbsp_copy_state(&st_g);
rbsp_reset_bit_counter(&st);
int nal_offset_next = rbsp_navigate_to_nal_unit(&st_g);
// calculate end of current slice. Note (-3). It's slice start code length.
const unsigned int end_pos = (nal_offset_next > 0) ? (nal_offset_next - 3)
: total_bitstream_bytes;
sp_h264.slice_data_size = end_pos - nal_offset;
sp_h264.slice_data_offset = 0;
sp_h264.slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
// TODO: this may be not entirely true for YUV444
// but if we limiting to YUV420, that's ok
int ChromaArrayType = pic_param->seq_fields.bits.chroma_format_idc;
// parse slice header and use its data to fill slice parameter buffer
parse_slice_header(&st, pic_param, ChromaArrayType, vdppi->num_ref_idx_l0_active_minus1,
vdppi->num_ref_idx_l1_active_minus1, &sp_h264);
VABufferID slice_parameters_buf;
status = vaCreateBuffer(va_dpy, decoderData->context_id, VASliceParameterBufferType,
sizeof(VASliceParameterBufferH264), 1, &sp_h264, &slice_parameters_buf);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaRenderPicture(va_dpy, decoderData->context_id, &slice_parameters_buf, 1);
if (VA_STATUS_SUCCESS != status)
goto error;
VABufferID slice_buf;
status = vaCreateBuffer(va_dpy, decoderData->context_id, VASliceDataBufferType,
sp_h264.slice_data_size, 1, merged_bitstream + nal_offset, &slice_buf);
if (VA_STATUS_SUCCESS != status)
goto error;
status = vaRenderPicture(va_dpy, decoderData->context_id, &slice_buf, 1);
if (VA_STATUS_SUCCESS != status)
goto error;
vaDestroyBuffer(va_dpy, slice_parameters_buf);
vaDestroyBuffer(va_dpy, slice_buf);
if (nal_offset_next < 0) // nal_offset_next equals -1 when there is no slice
break; // start code found. Thus that was the final slice.
nal_offset = nal_offset_next;
} while (1);
status = vaEndPicture(va_dpy, decoderData->context_id);
if (VA_STATUS_SUCCESS != status)
goto error;
free(merged_bitstream);
} else {
traceError("error (softVdpDecoderRender): no implementation for profile %s\n",
reverse_decoder_profile(decoderData->profile));
return VDP_STATUS_NO_IMPLEMENTATION;
}
return VDP_STATUS_OK;
error:
traceError("error (softVdpDecoderRender): something gone wrong\n");
return VDP_STATUS_ERROR;
error_no_nal_header:
traceError("error (softVdpDecoderRender): no NAL header\n");
return VDP_STATUS_ERROR;
error_resources:
return VDP_STATUS_RESOURCES;
error_no_surfaces_left:
traceError("error (softVdpDecoderRender): no surfaces left in buffer\n");
return VDP_STATUS_ERROR;
}
VdpStatus
softVdpOutputSurfaceQueryCapabilities(VdpDevice device, VdpRGBAFormat surface_rgba_format,
VdpBool *is_supported, uint32_t *max_width,
uint32_t *max_height)
{
VdpDeviceData *deviceData = handlestorage_get(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
if (NULL == is_supported || NULL == max_width || NULL == max_height)
return VDP_STATUS_INVALID_POINTER;
switch (surface_rgba_format) {
case VDP_RGBA_FORMAT_B8G8R8A8:
case VDP_RGBA_FORMAT_R8G8B8A8:
case VDP_RGBA_FORMAT_R10G10B10A2:
case VDP_RGBA_FORMAT_B10G10R10A2:
case VDP_RGBA_FORMAT_A8:
*is_supported = 1; // All these formats should be supported by OpenGL
break; // implementation.
default:
*is_supported = 0;
break;
}
GLint max_texture_size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
GLenum gl_error = glGetError();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceQueryCapabilities): gl error %d\n", gl_error);
return VDP_STATUS_ERROR;
}
*max_width = max_texture_size;
*max_height = max_texture_size;
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceQueryGetPutBitsNativeCapabilities(VdpDevice device,
VdpRGBAFormat surface_rgba_format,
VdpBool *is_supported)
{
(void)device; (void)surface_rgba_format; (void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpOutputSurfaceQueryPutBitsIndexedCapabilities(VdpDevice device,
VdpRGBAFormat surface_rgba_format,
VdpIndexedFormat bits_indexed_format,
VdpColorTableFormat color_table_format,
VdpBool *is_supported)
{
(void)device; (void)surface_rgba_format; (void)bits_indexed_format; (void)color_table_format;
(void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpOutputSurfaceQueryPutBitsYCbCrCapabilities(VdpDevice device,
VdpRGBAFormat surface_rgba_format,
VdpYCbCrFormat bits_ycbcr_format,
VdpBool *is_supported)
{
(void)device; (void)surface_rgba_format; (void)bits_ycbcr_format; (void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpOutputSurfaceCreate(VdpDevice device, VdpRGBAFormat rgba_format, uint32_t width,
uint32_t height, VdpOutputSurface *surface)
{
VdpDeviceData *deviceData = handlestorage_get(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
//TODO: figure out reasonable limits
if (width > 4096 || height > 4096)
return VDP_STATUS_INVALID_SIZE;
VdpOutputSurfaceData *data = (VdpOutputSurfaceData *)calloc(1, sizeof(VdpOutputSurfaceData));
if (NULL == data)
return VDP_STATUS_RESOURCES;
switch (rgba_format) {
case VDP_RGBA_FORMAT_B8G8R8A8:
data->gl_internal_format = GL_RGBA;
data->gl_format = GL_BGRA;
data->gl_type = GL_UNSIGNED_BYTE;
data->bytes_per_pixel = 4;
break;
case VDP_RGBA_FORMAT_R8G8B8A8:
data->gl_internal_format = GL_RGBA;
data->gl_format = GL_RGBA;
data->gl_type = GL_UNSIGNED_BYTE;
data->bytes_per_pixel = 4;
break;
case VDP_RGBA_FORMAT_R10G10B10A2:
data->gl_internal_format = GL_RGB10_A2;
data->gl_format = GL_RGBA;
data->gl_type = GL_UNSIGNED_INT_10_10_10_2;
data->bytes_per_pixel = 4;
break;
case VDP_RGBA_FORMAT_B10G10R10A2:
data->gl_internal_format = GL_RGB10_A2;
data->gl_format = GL_BGRA;
data->gl_type = GL_UNSIGNED_INT_10_10_10_2;
data->bytes_per_pixel = 4;
break;
case VDP_RGBA_FORMAT_A8:
data->gl_internal_format = GL_RGBA;
data->gl_format = GL_RED;
data->gl_type = GL_UNSIGNED_BYTE;
data->bytes_per_pixel = 1;
break;
default:
traceError("error (VdpOutputSurfaceCreate): %s is not implemented\n",
reverse_rgba_format(rgba_format));
free(data);
return VDP_STATUS_INVALID_RGBA_FORMAT;
}
data->type = HANDLETYPE_OUTPUT_SURFACE;
data->width = width;
data->height = height;
data->device = deviceData;
data->rgba_format = rgba_format;
glx_context_push_thread_local(deviceData);
glGenTextures(1, &data->tex_id);
glBindTexture(GL_TEXTURE_2D, data->tex_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// reserve texture
glTexImage2D(GL_TEXTURE_2D, 0, data->gl_internal_format, width, height, 0, data->gl_format,
data->gl_type, NULL);
glGenFramebuffers(1, &data->fbo_id);
glBindFramebuffer(GL_FRAMEBUFFER, data->fbo_id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, data->tex_id, 0);
GLenum gl_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (GL_FRAMEBUFFER_COMPLETE != gl_status) {
traceError("error (VdpOutputSurfaceCreate): "
"framebuffer not ready, %d, %s\n", gl_status, gluErrorString(gl_status));
glx_context_pop();
free(data);
return VDP_STATUS_ERROR;
}
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceCreate): gl error %d\n", gl_error);
free(data);
return VDP_STATUS_ERROR;
}
deviceData->refcount ++;
*surface = handlestorage_add(data);
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceDestroy(VdpOutputSurface surface)
{
VdpOutputSurfaceData *data = handlestorage_get(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == data)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = data->device;
glx_context_push_thread_local(deviceData);
glDeleteTextures(1, &data->tex_id);
glDeleteFramebuffers(1, &data->fbo_id);
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceDestroy): gl error %d\n", gl_error);
return VDP_STATUS_ERROR;
}
handlestorage_expunge(surface);
deviceData->refcount --;
free(data);
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceGetParameters(VdpOutputSurface surface, VdpRGBAFormat *rgba_format,
uint32_t *width, uint32_t *height)
{
VdpOutputSurfaceData *surfData = handlestorage_get(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == surfData)
return VDP_STATUS_INVALID_HANDLE;
if (NULL == rgba_format || NULL == width || NULL == height)
return VDP_STATUS_INVALID_HANDLE;
*rgba_format = surfData->rgba_format;
*width = surfData->width;
*height = surfData->height;
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceGetBitsNative(VdpOutputSurface surface, VdpRect const *source_rect,
void *const *destination_data,
uint32_t const *destination_pitches)
{
VdpOutputSurfaceData *srcSurfData = handlestorage_get(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == srcSurfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = srcSurfData->device;
VdpRect srcRect = {0, 0, srcSurfData->width, srcSurfData->height};
if (source_rect)
srcRect = *source_rect;
glx_context_push_thread_local(deviceData);
glBindFramebuffer(GL_FRAMEBUFFER, srcSurfData->fbo_id);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, destination_pitches[0] / srcSurfData->bytes_per_pixel);
if (4 != srcSurfData->bytes_per_pixel)
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(srcRect.x0, srcRect.y0, srcRect.x1 - srcRect.x0, srcRect.y1 - srcRect.y0,
srcSurfData->gl_format, srcSurfData->gl_type, destination_data[0]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
if (4 != srcSurfData->bytes_per_pixel)
glPixelStorei(GL_PACK_ALIGNMENT, 4);
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceGetBitsNative): gl error %d\n", gl_error);
return VDP_STATUS_ERROR;
}
return VDP_STATUS_OK;
}