forked from i-rinat/libvdpau-va-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdpau-soft.c
2318 lines (2062 loc) · 82.8 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 is distributed under the terms of the LGPLv3. See COPYING for details.
*/
#define _XOPEN_SOURCE
#define GL_GLEXT_PROTOTYPES
#include <assert.h>
#include <malloc.h>
#include <libswscale/swscale.h>
#include <string.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 "watermark.h"
#include "globals.h"
#define DESCRIBE(xparam, format) fprintf(stderr, #xparam " = %" #format "\n", xparam)
static char const *
implemetation_description_string = "OpenGL/VAAPI/libswscale backend for VDPAU";
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)
{
if (!api_version)
return VDP_STATUS_INVALID_POINTER;
*api_version = VDPAU_VERSION;
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceQueryCapabilities(VdpDevice device, VdpRGBAFormat surface_rgba_format,
VdpBool *is_supported, uint32_t *max_width,
uint32_t *max_height)
{
VdpStatus err_code;
if (!is_supported || !max_width || !max_height)
return VDP_STATUS_INVALID_POINTER;
VdpDeviceData *deviceData = handle_acquire(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
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);
err_code = VDP_STATUS_ERROR;
goto quit;
}
*max_width = max_texture_size;
*max_height = max_texture_size;
err_code = VDP_STATUS_OK;
quit:
handle_release(device);
return err_code;
}
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)
{
VdpStatus err_code;
if (!surface)
return VDP_STATUS_INVALID_POINTER;
VdpDeviceData *deviceData = handle_acquire(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
//TODO: figure out reasonable limits
if (width > 4096 || height > 4096) {
err_code = VDP_STATUS_INVALID_SIZE;
goto quit;
}
VdpOutputSurfaceData *data = calloc(1, sizeof(VdpOutputSurfaceData));
if (NULL == data) {
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
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);
err_code = VDP_STATUS_INVALID_RGBA_FORMAT;
goto quit;
}
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);
err_code = VDP_STATUS_ERROR;
goto quit;
}
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceCreate): gl error %d\n", gl_error);
free(data);
err_code = VDP_STATUS_ERROR;
goto quit;
}
deviceData->refcount ++;
*surface = handle_insert(data);
err_code = VDP_STATUS_OK;
quit:
handle_release(device);
return err_code;
}
VdpStatus
softVdpOutputSurfaceDestroy(VdpOutputSurface surface)
{
VdpStatus err_code;
VdpOutputSurfaceData *data = handle_acquire(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);
err_code = VDP_STATUS_ERROR;
goto quit;
}
handle_expunge(surface);
deviceData->refcount --;
free(data);
return VDP_STATUS_OK;
quit:
handle_release(surface);
return err_code;
}
VdpStatus
softVdpOutputSurfaceGetParameters(VdpOutputSurface surface, VdpRGBAFormat *rgba_format,
uint32_t *width, uint32_t *height)
{
if (!rgba_format || !width || !height)
return VDP_STATUS_INVALID_POINTER;
VdpOutputSurfaceData *surfData = handle_acquire(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == surfData)
return VDP_STATUS_INVALID_HANDLE;
// TODO: check surfData validity again
*rgba_format = surfData->rgba_format;
*width = surfData->width;
*height = surfData->height;
handle_release(surface);
return VDP_STATUS_OK;
}
VdpStatus
softVdpOutputSurfaceGetBitsNative(VdpOutputSurface surface, VdpRect const *source_rect,
void *const *destination_data,
uint32_t const *destination_pitches)
{
VdpStatus err_code;
if (!destination_data || !destination_pitches)
return VDP_STATUS_INVALID_POINTER;
VdpOutputSurfaceData *srcSurfData = handle_acquire(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);
glFinish();
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfaceGetBitsNative): gl error %d\n", gl_error);
err_code = VDP_STATUS_ERROR;
goto quit;
}
err_code = VDP_STATUS_OK;
quit:
handle_release(surface);
return err_code;
}
VdpStatus
softVdpOutputSurfacePutBitsNative(VdpOutputSurface surface, void const *const *source_data,
uint32_t const *source_pitches, VdpRect const *destination_rect)
{
VdpStatus err_code;
if (!source_data || !source_pitches)
return VDP_STATUS_INVALID_POINTER;
VdpOutputSurfaceData *dstSurfData = handle_acquire(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == dstSurfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = dstSurfData->device;
VdpRect dstRect = {0, 0, dstSurfData->width, dstSurfData->height};
if (destination_rect)
dstRect = *destination_rect;
glx_context_push_thread_local(deviceData);
glBindTexture(GL_TEXTURE_2D, dstSurfData->tex_id);
glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[0] / dstSurfData->bytes_per_pixel);
if (4 != dstSurfData->bytes_per_pixel)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, dstRect.x0, dstRect.y0,
dstRect.x1 - dstRect.x0, dstRect.y1 - dstRect.y0,
dstSurfData->gl_format, dstSurfData->gl_type, source_data[0]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
if (4 != dstSurfData->bytes_per_pixel)
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glFinish();
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfacePutBitsNative): gl error %d\n", gl_error);
err_code = VDP_STATUS_ERROR;
goto quit;
}
err_code = VDP_STATUS_OK;
quit:
handle_release(surface);
return err_code;
}
VdpStatus
softVdpOutputSurfacePutBitsIndexed(VdpOutputSurface surface, VdpIndexedFormat source_indexed_format,
void const *const *source_data, uint32_t const *source_pitch,
VdpRect const *destination_rect,
VdpColorTableFormat color_table_format, void const *color_table)
{
VdpStatus err_code;
if (!source_data || !source_pitch || !color_table)
return VDP_STATUS_INVALID_POINTER;
VdpOutputSurfaceData *surfData = handle_acquire(surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == surfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = surfData->device;
VdpRect dstRect = {0, 0, surfData->width, surfData->height};
if (destination_rect)
dstRect = *destination_rect;
// there is no other formats anyway
if (VDP_COLOR_TABLE_FORMAT_B8G8R8X8 != color_table_format) {
err_code = VDP_STATUS_INVALID_COLOR_TABLE_FORMAT;
goto quit;
}
const uint32_t *color_table32 = color_table;
glx_context_push_thread_local(deviceData);
switch (source_indexed_format) {
case VDP_INDEXED_FORMAT_I8A8:
// TODO: use shader?
do {
const uint32_t dstRectWidth = dstRect.x1 - dstRect.x0;
const uint32_t dstRectHeight = dstRect.y1 - dstRect.y0;
uint32_t *unpacked_buf = malloc(4 * dstRectWidth * dstRectHeight);
if (NULL == unpacked_buf) {
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
for (unsigned int y = 0; y < dstRectHeight; y ++) {
const uint8_t *src_ptr = source_data[0];
src_ptr += y * source_pitch[0];
uint32_t *dst_ptr = unpacked_buf + y * dstRectWidth;
for (unsigned int x = 0; x < dstRectWidth; x ++) {
const uint8_t i = *src_ptr++;
const uint32_t a = (*src_ptr++) << 24;
dst_ptr[x] = (color_table32[i] & 0x00ffffff) + a;
}
}
glBindTexture(GL_TEXTURE_2D, surfData->tex_id);
glTexSubImage2D(GL_TEXTURE_2D, 0, dstRect.x0, dstRect.y0,
dstRect.x1 - dstRect.x0, dstRect.y1 - dstRect.y0,
GL_BGRA, GL_UNSIGNED_BYTE, unpacked_buf);
glFinish();
free(unpacked_buf);
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpOutputSurfacePutBitsIndexed): gl error %d\n", gl_error);
err_code = VDP_STATUS_ERROR;
goto quit;
}
err_code = VDP_STATUS_OK;
goto quit;
} while (0);
break;
default:
traceError("error (VdpOutputSurfacePutBitsIndexed): unsupported indexed format %s\n",
reverse_indexed_format(source_indexed_format));
err_code = VDP_STATUS_INVALID_INDEXED_FORMAT;
goto quit;
}
quit:
handle_release(surface);
return err_code;
}
VdpStatus
softVdpOutputSurfacePutBitsYCbCr(VdpOutputSurface surface, VdpYCbCrFormat source_ycbcr_format,
void const *const *source_data, uint32_t const *source_pitches,
VdpRect const *destination_rect, VdpCSCMatrix const *csc_matrix)
{
(void)surface; (void)source_ycbcr_format; (void)source_data; (void)source_pitches;
(void)destination_rect; (void)csc_matrix;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerQueryFeatureSupport(VdpDevice device, VdpVideoMixerFeature feature,
VdpBool *is_supported)
{
(void)device; (void)feature; (void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerQueryParameterSupport(VdpDevice device, VdpVideoMixerParameter parameter,
VdpBool *is_supported)
{
(void)device; (void)parameter; (void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerQueryAttributeSupport(VdpDevice device, VdpVideoMixerAttribute attribute,
VdpBool *is_supported)
{
(void)device; (void)attribute; (void)is_supported;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerQueryParameterValueRange(VdpDevice device, VdpVideoMixerParameter parameter,
void *min_value, void *max_value)
{
(void)device; (void)parameter; (void)min_value; (void)max_value;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerQueryAttributeValueRange(VdpDevice device, VdpVideoMixerAttribute attribute,
void *min_value, void *max_value)
{
(void)device; (void)attribute; (void)min_value; (void)max_value;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerCreate(VdpDevice device, uint32_t feature_count,
VdpVideoMixerFeature const *features, uint32_t parameter_count,
VdpVideoMixerParameter const *parameters,
void const *const *parameter_values, VdpVideoMixer *mixer)
{
VdpStatus err_code;
if (!mixer)
return VDP_STATUS_INVALID_POINTER;
(void)feature_count; (void)features; // TODO: mixer features
(void)parameter_count; (void)parameters; (void)parameter_values; // TODO: mixer parameters
VdpDeviceData *deviceData = handle_acquire(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
VdpVideoMixerData *data = calloc(1, sizeof(VdpVideoMixerData));
if (NULL == data) {
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
data->type = HANDLETYPE_VIDEO_MIXER;
data->device = deviceData;
deviceData->refcount ++;
*mixer = handle_insert(data);
err_code = VDP_STATUS_OK;
quit:
handle_release(device);
return err_code;
}
VdpStatus
softVdpVideoMixerSetFeatureEnables(VdpVideoMixer mixer, uint32_t feature_count,
VdpVideoMixerFeature const *features,
VdpBool const *feature_enables)
{
(void)mixer; (void)feature_count; (void)features; (void)feature_enables;
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoMixerSetAttributeValues(VdpVideoMixer mixer, uint32_t attribute_count,
VdpVideoMixerAttribute const *attributes,
void const *const *attribute_values)
{
(void)mixer; (void)attribute_count; (void)attributes; (void)attribute_values;
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoMixerGetFeatureSupport(VdpVideoMixer mixer, uint32_t feature_count,
VdpVideoMixerFeature const *features, VdpBool *feature_supports)
{
(void)mixer; (void)feature_count; (void)features; (void)feature_supports;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerGetFeatureEnables(VdpVideoMixer mixer, uint32_t feature_count,
VdpVideoMixerFeature const *features, VdpBool *feature_enables)
{
(void)mixer; (void)feature_count; (void)features; (void)feature_enables;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerGetParameterValues(VdpVideoMixer mixer, uint32_t parameter_count,
VdpVideoMixerParameter const *parameters,
void *const *parameter_values)
{
(void)mixer; (void)parameter_count; (void)parameters; (void)parameter_values;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerGetAttributeValues(VdpVideoMixer mixer, uint32_t attribute_count,
VdpVideoMixerAttribute const *attributes,
void *const *attribute_values)
{
(void)mixer; (void)attribute_count; (void)attributes; (void)attribute_values;
return VDP_STATUS_NO_IMPLEMENTATION;
}
VdpStatus
softVdpVideoMixerDestroy(VdpVideoMixer mixer)
{
VdpVideoMixerData *videoMixerData = handle_acquire(mixer, HANDLETYPE_VIDEO_MIXER);
if (NULL == videoMixerData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = videoMixerData->device;
deviceData->refcount --;
handle_expunge(mixer);
free(videoMixerData);
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoMixerRender(VdpVideoMixer mixer, VdpOutputSurface background_surface,
VdpRect const *background_source_rect,
VdpVideoMixerPictureStructure current_picture_structure,
uint32_t video_surface_past_count,
VdpVideoSurface const *video_surface_past,
VdpVideoSurface video_surface_current, uint32_t video_surface_future_count,
VdpVideoSurface const *video_surface_future,
VdpRect const *video_source_rect, VdpOutputSurface destination_surface,
VdpRect const *destination_rect, VdpRect const *destination_video_rect,
uint32_t layer_count, VdpLayer const *layers)
{
VdpStatus err_code;
(void)mixer; // TODO: mixer should be used to get mixing parameters
// TODO: current implementation ignores previous and future surfaces, using only current.
// Is that acceptable for interlaced video? Will VAAPI handle deinterlacing?
(void)background_surface; // TODO: background_surface. Is it safe to just ignore it?
(void)background_source_rect;
(void)current_picture_structure;
(void)video_surface_past_count; (void)video_surface_past;
(void)video_surface_future_count; (void)video_surface_future;
(void)layer_count; (void)layers;
VdpVideoSurfaceData *srcSurfData =
handle_acquire(video_surface_current, HANDLETYPE_VIDEO_SURFACE);
VdpOutputSurfaceData *dstSurfData =
handle_acquire(destination_surface, HANDLETYPE_OUTPUT_SURFACE);
if (NULL == srcSurfData || NULL == dstSurfData) {
err_code = VDP_STATUS_INVALID_HANDLE;
goto quit;
}
if (srcSurfData->device != dstSurfData->device) {
err_code = VDP_STATUS_HANDLE_DEVICE_MISMATCH;
goto quit;
}
VdpDeviceData *deviceData = srcSurfData->device;
VdpRect srcVideoRect = {0, 0, srcSurfData->width, srcSurfData->height};
if (video_source_rect)
srcVideoRect = *video_source_rect;
VdpRect dstRect = {0, 0, dstSurfData->width, dstSurfData->height};
if (destination_rect)
dstRect = *destination_rect;
VdpRect dstVideoRect = srcVideoRect;
if (destination_video_rect)
dstVideoRect = *destination_video_rect;
// TODO: dstRect should clip dstVideoRect
glx_context_push_thread_local(deviceData);
if (deviceData->va_available) {
VAStatus status;
if (NULL == srcSurfData->va_glx) {
status = vaCreateSurfaceGLX(deviceData->va_dpy, GL_TEXTURE_2D, srcSurfData->tex_id,
&srcSurfData->va_glx);
if (VA_STATUS_SUCCESS != status) {
glx_context_pop();
err_code = VDP_STATUS_ERROR;
goto quit;
}
}
status = vaCopySurfaceGLX(deviceData->va_dpy, srcSurfData->va_glx, srcSurfData->va_surf, 0);
// TODO: check result of previous call
glBindFramebuffer(GL_FRAMEBUFFER, dstSurfData->fbo_id);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, dstSurfData->width, 0, dstSurfData->height, -1.0f, 1.0f);
glViewport(0, 0, dstSurfData->width, dstSurfData->height);
glDisable(GL_BLEND);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(1.0f/srcSurfData->width, 1.0f/srcSurfData->height, 1.0f);
// Clear dstRect area
glDisable(GL_TEXTURE_2D);
glColor4f(0, 0, 0, 1);
glBegin(GL_QUADS);
glVertex2f(dstRect.x0, dstRect.y0);
glVertex2f(dstRect.x1, dstRect.y0);
glVertex2f(dstRect.x1, dstRect.y1);
glVertex2f(dstRect.x0, dstRect.y1);
glEnd();
// Render (maybe scaled) data from video surface
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, srcSurfData->tex_id);
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2i(srcVideoRect.x0, srcVideoRect.y0);
glVertex2f(dstVideoRect.x0, dstVideoRect.y0);
glTexCoord2i(srcVideoRect.x1, srcVideoRect.y0);
glVertex2f(dstVideoRect.x1, dstVideoRect.y0);
glTexCoord2i(srcVideoRect.x1, srcVideoRect.y1);
glVertex2f(dstVideoRect.x1, dstVideoRect.y1);
glTexCoord2i(srcVideoRect.x0, srcVideoRect.y1);
glVertex2f(dstVideoRect.x0, dstVideoRect.y1);
glEnd();
} else {
// fall back to software convertion
// TODO: make sure not to do scaling in software, only colorspace conversion
// TODO: use GL shaders to do colorspace conversion job
// TODO: handle all three kind of rectangles and clipping
const uint32_t dstVideoWidth = dstVideoRect.x1 - dstVideoRect.x0;
const uint32_t dstVideoHeight = dstVideoRect.y1 - dstVideoRect.y0;
const uint32_t dstVideoStride = (dstVideoWidth & 3) ? (dstVideoWidth & ~3u) + 4
: dstVideoWidth;
uint8_t *img_buf = malloc(dstVideoStride * dstVideoHeight * 4);
if (NULL == img_buf) {
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
struct SwsContext *sws_ctx =
sws_getContext(srcSurfData->width, srcSurfData->height, PIX_FMT_YUV420P,
dstVideoWidth, dstVideoHeight,
PIX_FMT_RGBA, SWS_POINT, NULL, NULL, NULL);
uint8_t const * const src_planes[] =
{ srcSurfData->y_plane, srcSurfData->v_plane, srcSurfData->u_plane, NULL };
int src_strides[] = {srcSurfData->stride, srcSurfData->stride/2, srcSurfData->stride/2, 0};
uint8_t *dst_planes[] = {img_buf, NULL, NULL, NULL};
int dst_strides[] = {dstVideoStride * 4, 0, 0, 0};
int res = sws_scale(sws_ctx,
src_planes, src_strides, 0, srcSurfData->height,
dst_planes, dst_strides);
sws_freeContext(sws_ctx);
if (res != (int)dstVideoHeight) {
traceError("error (softVdpVideoMixerRender): libswscale scaling failed\n");
glx_context_pop();
err_code = VDP_STATUS_ERROR;
goto quit;
}
// copy converted image to texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, dstVideoStride);
glBindTexture(GL_TEXTURE_2D, dstSurfData->tex_id);
glTexSubImage2D(GL_TEXTURE_2D, 0,
dstVideoRect.x0, dstVideoRect.y0,
dstVideoRect.x1 - dstVideoRect.x0, dstVideoRect.y1 - dstVideoRect.y0,
GL_BGRA, GL_UNSIGNED_BYTE, img_buf);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
free(img_buf);
}
glFinish();
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpVideoMixerRender): gl error %d\n", gl_error);
err_code = VDP_STATUS_ERROR;
goto quit;
}
err_code = VDP_STATUS_OK;
quit:
handle_release(video_surface_current);
handle_release(destination_surface);
return err_code;
}
VdpStatus
softVdpVideoSurfaceQueryCapabilities(VdpDevice device, VdpChromaType surface_chroma_type,
VdpBool *is_supported, uint32_t *max_width,
uint32_t *max_height)
{
if (!is_supported || !max_width || !max_height)
return VDP_STATUS_INVALID_POINTER;
(void)device; (void)surface_chroma_type;
// TODO: implement
*is_supported = 1;
*max_width = 1920;
*max_height = 1080;
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities(VdpDevice device,
VdpChromaType surface_chroma_type,
VdpYCbCrFormat bits_ycbcr_format,
VdpBool *is_supported)
{
if (!is_supported)
return VDP_STATUS_INVALID_POINTER;
(void)device; (void)surface_chroma_type; (void)bits_ycbcr_format;
// TODO: implement
*is_supported = 1;
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type, uint32_t width,
uint32_t height, VdpVideoSurface *surface)
{
VdpStatus err_code;
if (!surface)
return VDP_STATUS_INVALID_POINTER;
VdpDeviceData *deviceData = handle_acquire(device, HANDLETYPE_DEVICE);
if (NULL == deviceData)
return VDP_STATUS_INVALID_HANDLE;
VdpVideoSurfaceData *data = calloc(1, sizeof(VdpVideoSurfaceData));
if (NULL == data) {
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
uint32_t const stride = (width % 4 == 0) ? width : (width & ~0x3UL) + 4;
data->type = HANDLETYPE_VIDEO_SURFACE;
data->device = deviceData;
data->chroma_type = chroma_type;
data->width = width;
data->stride = stride;
data->height = height;
data->va_surf = VA_INVALID_SURFACE;
data->va_glx = NULL;
data->tex_id = 0;
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->width, data->height, 0,
GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glFinish();
GLenum gl_error = glGetError();
glx_context_pop();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpVideoSurfaceCreate): gl error %d\n", gl_error);
free(data);
err_code = VDP_STATUS_ERROR;
goto quit;
}
if (deviceData->va_available) {
// no VA surface creation here. Actual pool of VA surfaces should be allocated already
// by VdpDecoderCreate. VdpDecoderCreate will update ->va_surf field as needed.
data->y_plane = NULL;
data->v_plane = NULL;
data->u_plane = NULL;
} else {
//TODO: find valid storage size for chroma_type
data->y_plane = malloc(stride * height);
data->v_plane = malloc(stride * height / chroma_storage_size_divider(chroma_type));
data->u_plane = malloc(stride * height / chroma_storage_size_divider(chroma_type));
if (NULL == data->y_plane || NULL == data->v_plane || NULL == data->u_plane) {
if (data->y_plane) free(data->y_plane);
if (data->v_plane) free(data->v_plane);
if (data->u_plane) free(data->u_plane);
free(data);
err_code = VDP_STATUS_RESOURCES;
goto quit;
}
}
deviceData->refcount ++;
*surface = handle_insert(data);
err_code = VDP_STATUS_OK;
quit:
handle_release(device);
return err_code;
}
VdpStatus
softVdpVideoSurfaceDestroy(VdpVideoSurface surface)
{
VdpVideoSurfaceData *videoSurfData = handle_acquire(surface, HANDLETYPE_VIDEO_SURFACE);
if (NULL == videoSurfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = videoSurfData->device;
glx_context_push_thread_local(deviceData);
glDeleteTextures(1, &videoSurfData->tex_id);
GLenum gl_error = glGetError();
if (GL_NO_ERROR != gl_error) {
traceError("error (VdpVideoSurfaceDestroy): gl error %d\n", gl_error);
glx_context_pop();
handle_release(surface);
return VDP_STATUS_ERROR;
}
if (videoSurfData->va_glx) {
vaDestroySurfaceGLX(deviceData->va_dpy, videoSurfData->va_glx);
}
if (deviceData->va_available) {
// .va_surf will be freed in VdpDecoderDestroy
} else {
free(videoSurfData->y_plane);
free(videoSurfData->v_plane);
free(videoSurfData->u_plane);
}
glx_context_pop();
deviceData->refcount --;
handle_expunge(surface);
free(videoSurfData);
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoSurfaceGetParameters(VdpVideoSurface surface, VdpChromaType *chroma_type,
uint32_t *width, uint32_t *height)
{
if (!chroma_type || !width || !height)
return VDP_STATUS_INVALID_POINTER;
VdpVideoSurfaceData *videoSurf = handle_acquire(surface, HANDLETYPE_VIDEO_SURFACE);
if (NULL == videoSurf)
return VDP_STATUS_INVALID_HANDLE;
*chroma_type = videoSurf->chroma_type;
*width = videoSurf->width;
*height = videoSurf->height;
handle_release(surface);
return VDP_STATUS_OK;
}
VdpStatus
softVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface, VdpYCbCrFormat destination_ycbcr_format,
void *const *destination_data, uint32_t const *destination_pitches)
{
VdpStatus err_code;
if (!destination_data || !destination_pitches)
return VDP_STATUS_INVALID_POINTER;
VdpVideoSurfaceData *srcSurfData = handle_acquire(surface, HANDLETYPE_VIDEO_SURFACE);
if (NULL == srcSurfData)
return VDP_STATUS_INVALID_HANDLE;
VdpDeviceData *deviceData = srcSurfData->device;
VADisplay va_dpy = deviceData->va_dpy;
if (deviceData->va_available) {
VAImage q;
vaDeriveImage(va_dpy, srcSurfData->va_surf, &q);
if (VA_FOURCC('N', 'V', '1', '2') == q.format.fourcc &&
VDP_YCBCR_FORMAT_NV12 == destination_ycbcr_format)
{
uint8_t *img_data;
vaMapBuffer(va_dpy, q.buf, (void **)&img_data);
if (destination_pitches[0] == q.pitches[0] &&
destination_pitches[1] == q.pitches[1])
{
memcpy(destination_data[0], img_data + q.offsets[0], q.width * q.height);
memcpy(destination_data[1], img_data + q.offsets[1], q.width * q.height / 2);
} else {
uint8_t *src = img_data + q.offsets[0];
uint8_t *dst = destination_data[0];
for (unsigned int y = 0; y < q.height; y ++) { // Y plane
memcpy (dst, src, q.width);
src += q.pitches[0];
dst += destination_pitches[0];
}
src = img_data + q.offsets[1];
dst = destination_data[1];
for (unsigned int y = 0; y < q.height / 2; y ++) { // UV plane
memcpy(dst, src, q.width); // q.width/2 samples of U and V each, hence q.width