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
|
/* vadxva2.c
Copyright (c) 2003-2014 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
Authors: Peng Gao <peng@multicorewareinc.com> <http://www.multicorewareinc.com/>
Li Cao <li@multicorewareinc.com> <http://www.multicorewareinc.com/>
*/
#ifdef USE_HWD
#include "vadxva2.h"
#include "extras/cl.h"
#include "oclnv12toyuv.h"
static int hb_va_setup( hb_va_dxva2_t *dxva2, void **hw, int width, int height );
static int hb_va_get( hb_va_dxva2_t *dxva2, AVFrame *frame );
static int hb_d3d_create_device( hb_va_dxva2_t *dxva2 );
static void hb_d3d_destroy_device( hb_va_dxva2_t *dxvva2 );
static int hb_d3d_create_device_manager( hb_va_dxva2_t *dxva2 );
static void hb_d3d_destroy_device_manager( hb_va_dxva2_t *dxva2 );
static int hb_dx_create_video_service( hb_va_dxva2_t *dxva2 );
static void hb_dx_destroy_video_service( hb_va_dxva2_t *dxva2 );
static int hb_dx_find_video_service_conversion( hb_va_dxva2_t *dxva2, GUID *input, D3DFORMAT *output );
static int hb_dx_create_video_decoder( hb_va_dxva2_t *dxva2, int codec_id, const hb_title_t* fmt );
static void hb_dx_create_video_conversion( hb_va_dxva2_t *dxva2 );
static const hb_d3d_format_t *hb_d3d_find_format( D3DFORMAT format );
static const hb_dx_mode_t *hb_dx_find_mode( const GUID *guid );
static void hb_dx_destroy_video_decoder( hb_va_dxva2_t *dxva2 );
/**
* It destroys a Direct3D device manager
*/
static void hb_d3d_destroy_device_manager( hb_va_dxva2_t *dxva2 )
{
if( dxva2->devmng )
IDirect3DDeviceManager9_Release( dxva2->devmng );
}
/**
* It releases a Direct3D device and its resources.
*/
static void hb_d3d_destroy_device( hb_va_dxva2_t *dxva2 )
{
if( dxva2->d3ddev )
IDirect3DDevice9_Release( dxva2->d3ddev );
if( dxva2->d3dobj )
IDirect3D9_Release( dxva2->d3dobj );
}
/**
* It destroys a DirectX video service
*/
static void hb_dx_destroy_video_service( hb_va_dxva2_t *dxva2 )
{
if( dxva2->device )
IDirect3DDeviceManager9_CloseDeviceHandle( dxva2->devmng, dxva2->device );
if( dxva2->vs )
IDirectXVideoDecoderService_Release( dxva2->vs );
}
static const hb_d3d_format_t *hb_d3d_find_format( D3DFORMAT format )
{
unsigned i;
for( i = 0; d3d_formats[i].name; i++ )
{
if( d3d_formats[i].format == format )
return &d3d_formats[i];
}
return NULL;
}
static void hb_dx_create_video_conversion( hb_va_dxva2_t *dxva2 )
{
switch( dxva2->render )
{
case MAKEFOURCC( 'N', 'V', '1', '2' ):
dxva2->output = MAKEFOURCC( 'Y', 'V', '1', '2' );
break;
default:
dxva2->output = dxva2->render;
break;
}
}
void hb_va_release( hb_va_dxva2_t *dxva2, AVFrame *frame )
{
LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)frame->data[3];
unsigned i;
for( i = 0; i < dxva2->surface_count; i++ )
{
hb_va_surface_t *surface = &dxva2->surface[i];
if( surface->d3d == d3d )
surface->refcount--;
}
}
void hb_va_close( hb_va_dxva2_t *dxva2 )
{
hb_dx_destroy_video_decoder( dxva2 );
hb_dx_destroy_video_service( dxva2 );
hb_d3d_destroy_device_manager( dxva2 );
hb_d3d_destroy_device( dxva2 );
if( dxva2->hdxva2_dll )
FreeLibrary( dxva2->hdxva2_dll );
if( dxva2->hd3d9_dll )
FreeLibrary( dxva2->hd3d9_dll );
if ( dxva2->nv12toyuv_tmp_in )
free( dxva2->nv12toyuv_tmp_in );
if ( dxva2->nv12toyuv_tmp_out )
free( dxva2->nv12toyuv_tmp_out );
dxva2->description = NULL;
free( dxva2 );
}
/**
* It creates a DXVA2 decoder using the given video format
*/
static int hb_dx_create_video_decoder( hb_va_dxva2_t *dxva2, int codec_id, const hb_title_t* fmt )
{
dxva2->width = fmt->width;
dxva2->height = fmt->height;
dxva2->surface_width = (fmt->width + 15) & ~15;
dxva2->surface_height = (fmt->height + 15) & ~15;
switch( codec_id )
{
case AV_CODEC_ID_H264:
dxva2->surface_count = 16 + 1;
break;
default:
dxva2->surface_count = 2 + 1;
break;
}
LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
if( FAILED( IDirectXVideoDecoderService_CreateSurface( dxva2->vs,
dxva2->surface_width,
dxva2->surface_height,
dxva2->surface_count - 1,
dxva2->render,
D3DPOOL_DEFAULT,
0,
DXVA2_VideoDecoderRenderTarget,
surface_list, NULL )))
{
hb_log( "dxva2:IDirectXVideoAccelerationService_CreateSurface failed" );
dxva2->surface_count = 0;
return HB_WORK_ERROR;
}
unsigned i;
for( i = 0; i<dxva2->surface_count; i++ )
{
hb_va_surface_t *surface = &dxva2->surface[i];
surface->d3d = surface_list[i];
surface->refcount = 0;
surface->order = 0;
}
hb_log( "dxva2:CreateSurface succeed with %d, fmt (%dx%d) surfaces (%dx%d)", dxva2->surface_count,
fmt->width, fmt->height, dxva2->surface_width, dxva2->surface_height );
DXVA2_VideoDesc dsc;
memset( &dsc, 0, sizeof(dsc));
dsc.SampleWidth = fmt->width;
dsc.SampleHeight = fmt->height;
dsc.Format = dxva2->render;
if( fmt->rate> 0 && fmt->rate_base> 0 )
{
dsc.InputSampleFreq.Numerator = fmt->rate;
dsc.InputSampleFreq.Denominator = fmt->rate_base;
}
else
{
dsc.InputSampleFreq.Numerator = 0;
dsc.InputSampleFreq.Denominator = 0;
}
dsc.OutputFrameFreq = dsc.InputSampleFreq;
dsc.UABProtectionLevel = FALSE;
dsc.Reserved = 0;
/* FIXME I am unsure we can let unknown everywhere */
DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
ext->SampleFormat = 0; //DXVA2_SampleUnknown;
ext->VideoChromaSubsampling = 0; //DXVA2_VideoChromaSubsampling_Unknown;
ext->NominalRange = 0; //DXVA2_NominalRange_Unknown;
ext->VideoTransferMatrix = 0; //DXVA2_VideoTransferMatrix_Unknown;
ext->VideoLighting = 0; //DXVA2_VideoLighting_Unknown;
ext->VideoPrimaries = 0; //DXVA2_VideoPrimaries_Unknown;
ext->VideoTransferFunction = 0; //DXVA2_VideoTransFunc_Unknown;
/* List all configurations available for the decoder */
UINT cfg_count = 0;
DXVA2_ConfigPictureDecode *cfg_list = NULL;
if( FAILED( IDirectXVideoDecoderService_GetDecoderConfigurations( dxva2->vs, &dxva2->input, &dsc, NULL, &cfg_count, &cfg_list )))
{
hb_log( "dxva2:IDirectXVideoDecoderService_GetDecoderConfigurations failed" );
return HB_WORK_ERROR;
}
hb_log( "dxva2:we got %d decoder configurations", cfg_count );
/* Select the best decoder configuration */
int cfg_score = 0;
for( i = 0; i < cfg_count; i++ )
{
const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
hb_log( "dxva2:configuration[%d] ConfigBitstreamRaw %d", i, cfg->ConfigBitstreamRaw );
int score;
if( cfg->ConfigBitstreamRaw == 1 )
score = 1;
else if( codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2 )
score = 2;
else
continue;
if( IsEqualGUID( &cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt ))
score += 16;
if( cfg_score < score )
{
dxva2->cfg = *cfg;
cfg_score = score;
}
}
//my_release(cfg_list);
if( cfg_score <= 0 )
{
hb_log( "dxva2:Failed to find a supported decoder configuration" );
return HB_WORK_ERROR;
}
/* Create the decoder */
IDirectXVideoDecoder *decoder;
if( FAILED( IDirectXVideoDecoderService_CreateVideoDecoder( dxva2->vs, &dxva2->input, &dsc, &dxva2->cfg, surface_list, dxva2->surface_count, &decoder )))
{
hb_log( "dxva2:IDirectXVideoDecoderService_CreateVideoDecoder failed" );
return HB_WORK_ERROR;
}
dxva2->decoder = decoder;
hb_log( "dxva2:IDirectXVideoDecoderService_CreateVideoDecoder succeed" );
return HB_WORK_OK;
}
typedef HWND (WINAPI *PROCGETSHELLWND)();
/**
* It creates a DirectX video service
*/
static int hb_d3d_create_device( hb_va_dxva2_t *dxva2 )
{
LPDIRECT3D9 (WINAPI *Create9)( UINT SDKVersion );
Create9 = (void*)GetProcAddress( dxva2->hd3d9_dll, TEXT( "Direct3DCreate9" ));
if( !Create9 )
{
hb_log( "dxva2:Cannot locate reference to Direct3DCreate9 ABI in DLL" );
return HB_WORK_ERROR;
}
LPDIRECT3D9 d3dobj;
d3dobj = Create9( D3D_SDK_VERSION );
if( !d3dobj )
{
hb_log( "dxva2:Direct3DCreate9 failed" );
return HB_WORK_ERROR;
}
dxva2->d3dobj = d3dobj;
D3DADAPTER_IDENTIFIER9 *d3dai = &dxva2->d3dai;
if( FAILED( IDirect3D9_GetAdapterIdentifier( dxva2->d3dobj, D3DADAPTER_DEFAULT, 0, d3dai )))
{
hb_log( "dxva2:IDirect3D9_GetAdapterIdentifier failed" );
memset( d3dai, 0, sizeof(*d3dai));
}
PROCGETSHELLWND GetShellWindow;
HMODULE hUser32 = GetModuleHandle( "user32" );
GetShellWindow = (PROCGETSHELLWND)
GetProcAddress( hUser32, "GetShellWindow" );
D3DPRESENT_PARAMETERS *d3dpp = &dxva2->d3dpp;
memset( d3dpp, 0, sizeof(*d3dpp));
d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
d3dpp->Windowed = TRUE;
d3dpp->hDeviceWindow = NULL;
d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp->BackBufferCount = 0; /* FIXME what to put here */
d3dpp->BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
d3dpp->BackBufferWidth = 0;
d3dpp->BackBufferHeight = 0;
d3dpp->EnableAutoDepthStencil = FALSE;
LPDIRECT3DDEVICE9 d3ddev;
//if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetShellWindow(), D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED, d3dpp, &d3ddev)))
if( FAILED( IDirect3D9_CreateDevice( d3dobj,
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
GetShellWindow(),
D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,
d3dpp,
&d3ddev )))
{
hb_log( "dxva2:IDirect3D9_CreateDevice failed" );
return HB_WORK_ERROR;
}
dxva2->d3ddev = d3ddev;
return HB_WORK_OK;
}
/**
* It creates a Direct3D device manager
*/
static int hb_d3d_create_device_manager( hb_va_dxva2_t *dxva2 )
{
HRESULT(WINAPI *CreateDeviceManager9)( UINT *pResetToken, IDirect3DDeviceManager9 ** );
CreateDeviceManager9 = (void*)GetProcAddress( dxva2->hdxva2_dll, TEXT( "DXVA2CreateDirect3DDeviceManager9" ));
if( !CreateDeviceManager9 )
{
hb_log( "dxva2:cannot load function" );
return HB_WORK_ERROR;
}
UINT token;
IDirect3DDeviceManager9 *devmng;
if( FAILED( CreateDeviceManager9( &token, &devmng )))
{
hb_log( "dxva2:OurDirect3DCreateDeviceManager9 failed" );
return HB_WORK_ERROR;
}
dxva2->token = token;
dxva2->devmng = devmng;
long hr = IDirect3DDeviceManager9_ResetDevice( devmng, dxva2->d3ddev, token );
if( FAILED( hr ))
{
hb_log( "dxva2:IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr );
return HB_WORK_ERROR;
}
return HB_WORK_OK;
}
/**
* It creates a DirectX video service
*/
static int hb_dx_create_video_service( hb_va_dxva2_t *dxva2 )
{
HRESULT (WINAPI *CreateVideoService)( IDirect3DDevice9 *, REFIID riid, void **ppService );
CreateVideoService = (void*)GetProcAddress( dxva2->hdxva2_dll, TEXT( "DXVA2CreateVideoService" ));
if( !CreateVideoService )
{
hb_log( "dxva2:cannot load function" );
return HB_WORK_ERROR;
}
HRESULT hr;
HANDLE device;
hr = IDirect3DDeviceManager9_OpenDeviceHandle( dxva2->devmng, &device );
if( FAILED( hr ))
{
hb_log( "dxva2:OpenDeviceHandle failed" );
return HB_WORK_ERROR;
}
dxva2->device = device;
IDirectXVideoDecoderService *vs;
hr = IDirect3DDeviceManager9_GetVideoService( dxva2->devmng, device, &IID_IDirectXVideoDecoderService, (void*)&vs );
if( FAILED( hr ))
{
hb_log( "dxva2:GetVideoService failed" );
return HB_WORK_ERROR;
}
dxva2->vs = vs;
return HB_WORK_OK;
}
/**
* Find the best suited decoder mode GUID and render format.
*/
static int hb_dx_find_video_service_conversion( hb_va_dxva2_t *dxva2, GUID *input, D3DFORMAT *output )
{
unsigned int input_count = 0;
GUID *input_list = NULL;
if( FAILED( IDirectXVideoDecoderService_GetDecoderDeviceGuids( dxva2->vs, &input_count, &input_list )))
{
hb_log( "dxva2:IDirectXVideoDecoderService_GetDecoderDeviceGuids failed" );
return HB_WORK_ERROR;
}
unsigned i, j;
for( i = 0; i < input_count; i++ )
{
const GUID *g = &input_list[i];
const hb_dx_mode_t *mode = hb_dx_find_mode( g );
}
for( i = 0; dxva2_modes[i].name; i++ )
{
const hb_dx_mode_t *mode = &dxva2_modes[i];
if( !mode->codec || mode->codec != dxva2->codec_id )
continue;
int is_suported = 0;
const GUID *g;
for( g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++ )
{
is_suported = IsEqualGUID( mode->guid, g );
}
if( !is_suported )
continue;
unsigned int output_count = 0;
D3DFORMAT *output_list = NULL;
if( FAILED( IDirectXVideoDecoderService_GetDecoderRenderTargets( dxva2->vs, mode->guid, &output_count, &output_list )))
{
hb_log( "dxva2:IDirectXVideoDecoderService_GetDecoderRenderTargets failed" );
continue;
}
for( j = 0; j < output_count; j++ )
{
const D3DFORMAT f = output_list[j];
const hb_d3d_format_t *format = hb_d3d_find_format( f );
if( format )
{
//hb_log( "dxva2:%s is supported for output", format->name );
}
else
{
hb_log( "dxvar2:%d is supported for output (%4.4s)", f, (const char*)&f );
}
}
for( j = 0; d3d_formats[j].name; j++ )
{
const hb_d3d_format_t *format = &d3d_formats[j];
int is_suported = 0;
unsigned k;
for( k = 0; !is_suported && k < output_count; k++ )
{
is_suported = format->format == output_list[k];
}
if( !is_suported )
continue;
*input = *mode->guid;
*output = format->format;
return HB_WORK_OK;
}
}
return HB_WORK_ERROR;
}
static const hb_dx_mode_t *hb_dx_find_mode( const GUID *guid )
{
unsigned i;
for( i = 0; dxva2_modes[i].name; i++ )
{
if( IsEqualGUID( dxva2_modes[i].guid, guid ))
return &dxva2_modes[i];
}
return NULL;
}
static void hb_dx_destroy_video_decoder( hb_va_dxva2_t *dxva2 )
{
if( dxva2->decoder )
IDirectXVideoDecoder_Release( dxva2->decoder );
dxva2->decoder = NULL;
unsigned i;
for( i = 0; i<dxva2->surface_count; i++ )
IDirect3DSurface9_Release( dxva2->surface[i].d3d );
dxva2->surface_count = 0;
}
/**
* setup dxva2
*/
static int hb_va_setup( hb_va_dxva2_t *dxva2, void **hw, int width, int height )
{
if( dxva2->width == width && dxva2->height == height && dxva2->decoder )
goto ok;
hb_dx_destroy_video_decoder( dxva2 );
*hw = NULL;
dxva2->i_chroma = 0;
if( width <= 0 || height <= 0 )
return HB_WORK_ERROR;
hb_title_t fmt;
memset( &fmt, 0, sizeof(fmt));
fmt.width = width;
fmt.height = height;
if( hb_dx_create_video_decoder( dxva2, dxva2->codec_id, &fmt ) == HB_WORK_ERROR )
return HB_WORK_ERROR;
dxva2->hw.decoder = dxva2->decoder;
dxva2->hw.cfg = &dxva2->cfg;
dxva2->hw.surface_count = dxva2->surface_count;
dxva2->hw.surface = dxva2->hw_surface;
unsigned i;
for( i = 0; i < dxva2->surface_count; i++ )
dxva2->hw.surface[i] = dxva2->surface[i].d3d;
hb_dx_create_video_conversion( dxva2 );
ok:
*hw = &dxva2->hw;
const hb_d3d_format_t *output = hb_d3d_find_format( dxva2->output );
dxva2->i_chroma = output->codec;
return HB_WORK_OK;
}
static int hb_va_get( hb_va_dxva2_t *dxva2, AVFrame *frame )
{
unsigned i, old;
for( i = 0, old = 0; i < dxva2->surface_count; i++ )
{
hb_va_surface_t *surface = &dxva2->surface[i];
if( !surface->refcount )
break;
if( surface->order < dxva2->surface[old].order )
old = i;
}
if( i >= dxva2->surface_count )
i = old;
hb_va_surface_t *surface = &dxva2->surface[i];
surface->refcount = 1;
surface->order = dxva2->surface_order++;
for( i = 0; i < 4; i++ )
{
frame->data[i] = NULL;
frame->linesize[i] = 0;
if( i == 0 || i == 3 )
frame->data[i] = (void*)surface->d3d;
}
return HB_WORK_OK;
}
/**
* nv12 to yuv of c reference
*/
static void hb_copy_from_nv12( uint8_t *dst, uint8_t *src[2], size_t src_pitch[2], unsigned width, unsigned height )
{
unsigned int i, j;
uint8_t *dstU, *dstV;
dstU = dst + width*height;
dstV = dstU + width*height/4;
unsigned int heithtUV, widthUV;
heithtUV = height/2;
widthUV = width/2;
for( i = 0; i < height; i++ ) //Y
{
memcpy( dst + i * width, src[0] + i * src_pitch[0], width );
}
for( i = 0; i < heithtUV; i++ )
{
for( j = 0; j < widthUV; j++ )
{
dstU[i * widthUV + j] = *(src[1] + i * src_pitch[1] + 2 * j);
dstV[i *widthUV + j] = *(src[1] + i * src_pitch[1] + 2 * j + 1);
}
}
}
/**
* lock frame data form surface.
* nv12 to yuv with opencl and with C reference
* scale with opencl
*/
int hb_va_extract( hb_va_dxva2_t *dxva2, uint8_t *dst, AVFrame *frame, int job_w, int job_h, int *crop, hb_oclscale_t *os, int use_opencl, int use_decomb, int use_detelecine )
{
LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)frame->data[3];
D3DLOCKED_RECT lock;
if( FAILED( IDirect3DSurface9_LockRect( d3d, &lock, NULL, D3DLOCK_READONLY )))
{
hb_log( "dxva2:Failed to lock surface" );
return HB_WORK_ERROR;
}
if( dxva2->render == MAKEFOURCC( 'N', 'V', '1', '2' ))
{
uint8_t *plane[2] =
{
lock.pBits,
(uint8_t*)lock.pBits + lock.Pitch * dxva2->surface_height
};
size_t pitch[2] =
{
lock.Pitch,
lock.Pitch,
};
hb_copy_from_nv12( dst, plane, pitch, dxva2->width, dxva2->height );
}
IDirect3DSurface9_UnlockRect( d3d );
return HB_WORK_OK;
}
/**
* create dxva2 service
* load library D3D9.dll
*/
hb_va_dxva2_t * hb_va_create_dxva2( hb_va_dxva2_t *dxva2, int codec_id )
{
if( dxva2 )
{
hb_va_close( dxva2 );
dxva2 = NULL;
}
hb_va_dxva2_t *dxva = calloc( 1, sizeof(*dxva) );
if( !dxva ) return NULL;
dxva->codec_id = codec_id;
dxva->hd3d9_dll = LoadLibrary( TEXT( "D3D9.DLL" ) );
if( !dxva->hd3d9_dll )
{
hb_log( "dxva2:cannot load d3d9.dll" );
goto error;
}
dxva->hdxva2_dll = LoadLibrary( TEXT( "DXVA2.DLL" ) );
if( !dxva->hdxva2_dll )
{
hb_log( "dxva2:cannot load DXVA2.dll" );
goto error;
}
if( hb_d3d_create_device( dxva ) == HB_WORK_ERROR )
{
hb_log( "dxva2:Failed to create Direct3D device" );
goto error;
}
if( hb_d3d_create_device_manager( dxva ) == HB_WORK_ERROR )
{
hb_log( "dxva2:D3dCreateDeviceManager failed" );
goto error;
}
if( hb_dx_create_video_service( dxva ) == HB_WORK_ERROR )
{
hb_log( "dxva2:DxCreateVideoService failed" );
goto error;
}
if( hb_dx_find_video_service_conversion( dxva, &dxva->input, &dxva->render ) == HB_WORK_ERROR )
{
hb_log( "dxva2:DxFindVideoServiceConversion failed" );
goto error;
}
dxva->do_job = HB_WORK_OK;
dxva->description = "DXVA2";
return dxva;
error:
hb_va_close( dxva );
return NULL;
}
void hb_va_new_dxva2( hb_va_dxva2_t *dxva2, AVCodecContext *p_context )
{
if( p_context->width > 0 && p_context->height > 0 )
{
if( hb_va_setup( dxva2, &p_context->hwaccel_context, p_context->width, p_context->height ) == HB_WORK_ERROR )
{
hb_log( "dxva2:hb_va_Setup failed" );
hb_va_close( dxva2 );
dxva2 = NULL;
}
}
if( dxva2 )
{
dxva2->input_pts[0] = 0;
dxva2->input_pts[1] = 0;
if( dxva2->description )
hb_log( "dxva2:Using %s for hardware decoding", dxva2->description );
p_context->draw_horiz_band = NULL;
}
}
char* hb_get_pix_fmt_name( int pix_fmt )
{
static const char *ppsz_name[AV_PIX_FMT_NB] =
{
[AV_PIX_FMT_VDPAU_H264] = "AV_PIX_FMT_VDPAU_H264",
[AV_PIX_FMT_VAAPI_IDCT] = "AV_PIX_FMT_VAAPI_IDCT",
[AV_PIX_FMT_VAAPI_VLD] = "AV_PIX_FMT_VAAPI_VLD",
[AV_PIX_FMT_VAAPI_MOCO] = "AV_PIX_FMT_VAAPI_MOCO",
[AV_PIX_FMT_DXVA2_VLD] = "AV_PIX_FMT_DXVA2_VLD",
[AV_PIX_FMT_YUYV422] = "AV_PIX_FMT_YUYV422",
[AV_PIX_FMT_YUV420P] = "AV_PIX_FMT_YUV420P",
};
return ppsz_name[pix_fmt];
}
enum PixelFormat hb_ffmpeg_get_format( AVCodecContext *p_context, const enum PixelFormat *pi_fmt )
{
int i;
for( i = 0; pi_fmt[i] != AV_PIX_FMT_NONE; i++ )
{
hb_log( "dxva2:Available decoder output format %d (%s)", pi_fmt[i], hb_get_pix_fmt_name(pi_fmt[i]) ? : "Unknown" );
if( pi_fmt[i] == AV_PIX_FMT_DXVA2_VLD )
{
return pi_fmt[i];
}
}
return avcodec_default_get_format( p_context, pi_fmt );
}
int hb_va_get_frame_buf( hb_va_dxva2_t *dxva2, AVCodecContext *p_context, AVFrame *frame )
{
frame->type = FF_BUFFER_TYPE_USER;
if( hb_va_get( dxva2, frame ) == HB_WORK_ERROR )
{
hb_log( "VaGrabSurface failed" );
return HB_WORK_ERROR;
}
return HB_WORK_OK;
}
int hb_check_hwd_fmt( int fmt )
{
int result = 1;
switch ( fmt )
{
case AV_PIX_FMT_YUV420P16LE:
case AV_PIX_FMT_YUV420P16BE:
case AV_PIX_FMT_YUV422P16LE:
case AV_PIX_FMT_YUV422P16BE:
case AV_PIX_FMT_YUV444P16LE:
case AV_PIX_FMT_YUV444P16BE:
case AV_PIX_FMT_YUV420P9BE:
case AV_PIX_FMT_YUV420P9LE:
case AV_PIX_FMT_YUV420P10BE:
case AV_PIX_FMT_YUV420P10LE:
case AV_PIX_FMT_YUV422P10BE:
case AV_PIX_FMT_YUV422P10LE:
case AV_PIX_FMT_YUV444P9BE:
case AV_PIX_FMT_YUV444P9LE:
case AV_PIX_FMT_YUV444P10BE:
case AV_PIX_FMT_YUV444P10LE:
case AV_PIX_FMT_YUV422P9BE:
case AV_PIX_FMT_YUV422P9LE:
case AV_PIX_FMT_GBRP9BE:
case AV_PIX_FMT_GBRP9LE:
case AV_PIX_FMT_GBRP10BE:
case AV_PIX_FMT_GBRP10LE:
case AV_PIX_FMT_GBRP16BE:
case AV_PIX_FMT_GBRP16LE:
case AV_PIX_FMT_YUVA420P9BE:
case AV_PIX_FMT_YUVA420P9LE:
case AV_PIX_FMT_YUVA422P9BE:
case AV_PIX_FMT_YUVA422P9LE:
case AV_PIX_FMT_YUVA444P9BE:
case AV_PIX_FMT_YUVA444P9LE:
case AV_PIX_FMT_YUVA420P10BE:
case AV_PIX_FMT_YUVA420P10LE:
result = 0;
}
return result;
}
#endif // USE_HWD
|