summaryrefslogtreecommitdiffstats
path: root/libhb/qsv_libav.c
blob: f439c0ffe6d9f33daa6df353f545a6d944eb13e6 (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
/* ********************************************************************* *\

Copyright (C) 2013 Intel Corporation.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

\* ********************************************************************* */

#ifdef USE_QSV

#include "hbffmpeg.h"
#include "qsv_libav.h"

int av_qsv_get_free_encode_task(av_qsv_list * tasks)
{
    int ret = MFX_ERR_NOT_FOUND;
    int i = 0;
    if (tasks)
        for (i = 0; i < av_qsv_list_count(tasks); i++) {
            av_qsv_task *task = av_qsv_list_item(tasks, i);
            if (task->stage && task->stage->out.sync)
                if (!(*task->stage->out.sync->p_sync)) {
                    ret = i;
                    break;
                }
        }
    return ret;
}

int av_qsv_get_free_sync(av_qsv_space * space, av_qsv_context * qsv)
{
    int ret = -1;
    int counter = 0;

    while (1) {
        for (int i = 0; i < space->sync_num; i++) {
            if (!(*(space->p_syncp[i]->p_sync)) &&
                0 == space->p_syncp[i]->in_use ) {
                if (i > space->sync_num_max_used)
                    space->sync_num_max_used = i;
                ff_qsv_atomic_inc(&space->p_syncp[i]->in_use);
                return i;
            }
        }
        if (++counter >= AV_QSV_REPEAT_NUM_DEFAULT) {
            hb_error("QSV: not enough to have %d sync point(s) allocated", space->sync_num);
            break;
        }
        av_qsv_sleep(5);
    }
    return ret;
}

int av_qsv_get_free_surface(av_qsv_space * space, av_qsv_context * qsv,
                     mfxFrameInfo * info, av_qsv_split part)
{
    int ret = -1;
    int from = 0;
    int up = space->surface_num;
    int counter = 0;

    while (1) {
        from = 0;
        up = space->surface_num;
        if (part == QSV_PART_LOWER)
            up /= 2;
        if (part == QSV_PART_UPPER)
            from = up / 2;

        for (int i = from; i < up; i++) {
            if (0 == space->p_surfaces[i]->Data.Locked) {
                memcpy(&(space->p_surfaces[i]->Info), info,
                       sizeof(mfxFrameInfo));
                if (i > space->surface_num_max_used)
                    space->surface_num_max_used = i;
                return i;
            }
        }
        if (++counter >= AV_QSV_REPEAT_NUM_DEFAULT) {
            hb_error("QSV: not enough to have %d surface(s) allocated", up);
            break;
        }
        av_qsv_sleep(5);
    }
    return ret;
}

int ff_qsv_is_surface_in_pipe(mfxFrameSurface1 * p_surface, av_qsv_context * qsv)
{
    int ret = 0;
    int a, b;
    av_qsv_list *list = 0;
    av_qsv_stage *stage = 0;

    if (!p_surface)
        return ret;
    if (!qsv->pipes)
        return ret;

    for (a = 0; a < av_qsv_list_count(qsv->pipes); a++) {
        list = av_qsv_list_item(qsv->pipes, a);
        for (b = 0; b < av_qsv_list_count(list); b++) {
            stage = av_qsv_list_item(list, b);
            if (p_surface == stage->out.p_surface)
                return (stage->type << 16) | 2;
            if (p_surface == stage->in.p_surface)
                return (stage->type << 16) | 1;
        }
    }
    return ret;
}

int ff_qsv_is_sync_in_pipe(mfxSyncPoint * sync, av_qsv_context * qsv)
{
    int ret = 0;
    int a, b;
    av_qsv_list *list = 0;
    av_qsv_stage *stage = 0;

    if (!sync)
        return ret;
    if (!qsv->pipes)
        return ret;

    for (a = 0; a < av_qsv_list_count(qsv->pipes); a++) {
        list = av_qsv_list_item(qsv->pipes, a);
        for (b = 0; b < av_qsv_list_count(list); b++) {
            stage = av_qsv_list_item(list, b);
            if (sync == stage->out.sync->p_sync) {
                return 1;
            }
        }
    }
    return ret;
}

av_qsv_stage *av_qsv_stage_init(void)
{
    av_qsv_stage *stage = av_mallocz(sizeof(av_qsv_stage));
    return stage;
}

void av_qsv_stage_clean(av_qsv_stage ** stage)
{
    if ((*stage)->out.sync) {
        if ((*stage)->out.sync->p_sync)
            *(*stage)->out.sync->p_sync = 0;
        if ((*stage)->out.sync->in_use > 0)
            ff_qsv_atomic_dec(&(*stage)->out.sync->in_use);
        (*stage)->out.sync = 0;
    }
    if ((*stage)->out.p_surface) {
        (*stage)->out.p_surface = 0;

    }
    if ((*stage)->in.p_surface) {
        (*stage)->in.p_surface = 0;
    }

    av_freep(stage);
}

void av_qsv_add_context_usage(av_qsv_context * qsv, int is_threaded)
{
    int is_active = 0;
    int mut_ret = 0;

    is_active = ff_qsv_atomic_inc(&qsv->is_context_active);
    if (is_active == 1) {
        memset(&qsv->mfx_session, 0, sizeof(mfxSession));
        av_qsv_pipe_list_create(&qsv->pipes, is_threaded);

        qsv->dts_seq = av_qsv_list_init(is_threaded);

        if (is_threaded) {
            qsv->qts_seq_mutex = av_mallocz(sizeof(pthread_mutex_t));
            if (qsv->qts_seq_mutex){
                mut_ret = pthread_mutex_init(qsv->qts_seq_mutex, NULL);
                if(mut_ret)
                    hb_log("QSV: pthread_mutex_init issue[%d] at %s", mut_ret, __FUNCTION__);
            }

        } else
            qsv->qts_seq_mutex = 0;
    }
}

int av_qsv_context_clean(av_qsv_context * qsv)
{
    int is_active = 0;
    mfxStatus sts = MFX_ERR_NONE;
    int mut_ret = 0;

    is_active = ff_qsv_atomic_dec(&qsv->is_context_active);

    // spaces would have to be cleaned on the own,
    // here we care about the rest, common stuff
    if (is_active == 0) {

        if (qsv->dts_seq) {
            while (av_qsv_list_count(qsv->dts_seq))
                av_qsv_dts_pop(qsv);

            av_qsv_list_close(&qsv->dts_seq);
        }
        if (qsv->qts_seq_mutex) {
            mut_ret = pthread_mutex_destroy(qsv->qts_seq_mutex);
            if(mut_ret)
                hb_log("QSV: pthread_mutex_destroy issue[%d] at %s", mut_ret, __FUNCTION__);
            qsv->qts_seq_mutex = 0;
        }

        if (qsv->pipes)
            av_qsv_pipe_list_clean(&qsv->pipes);

        if (qsv->mfx_session) {
            sts = MFXClose(qsv->mfx_session);
            AV_QSV_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
            qsv->mfx_session = 0;
        }
    }
    return 0;
}

void av_qsv_pipe_list_create(av_qsv_list ** list, int is_threaded)
{
    if (!*list)
        *list = av_qsv_list_init(is_threaded);
}

void av_qsv_pipe_list_clean(av_qsv_list ** list)
{
    av_qsv_list *stage;
    int i = 0;
    if (*list) {
        for (i = av_qsv_list_count(*list); i > 0; i--) {
            stage = av_qsv_list_item(*list, i - 1);
            av_qsv_flush_stages(*list, &stage);
        }
        av_qsv_list_close(list);
    }
}

void av_qsv_add_stagee(av_qsv_list ** list, av_qsv_stage * stage, int is_threaded)
{
    if (!*list)
        *list = av_qsv_list_init(is_threaded);
    av_qsv_list_add(*list, stage);
}

av_qsv_stage *av_qsv_get_last_stage(av_qsv_list * list)
{
    av_qsv_stage *stage = 0;
    int size = 0;

    av_qsv_list_lock(list);
    size = av_qsv_list_count(list);
    if (size > 0)
        stage = av_qsv_list_item(list, size - 1);
    av_qsv_list_unlock(list);

    return stage;
}

void av_qsv_flush_stages(av_qsv_list * list, av_qsv_list ** item)
{
    int i = 0;
    int x = 0;
    av_qsv_stage *stage = 0;
    av_qsv_list *to_remove_list = 0;
    av_qsv_list *to_remove_atom_list = 0;
    av_qsv_list *to_remove_atom = 0;

    for (i = 0; i < av_qsv_list_count(*item); i++) {
        stage = av_qsv_list_item(*item, i);
        if(stage->pending){
            if(!to_remove_list)
                to_remove_list = av_qsv_list_init(0);
            av_qsv_list_add(to_remove_list, stage->pending);
        }
        av_qsv_stage_clean(&stage);
        // should actually remove from the list but ok...
    }
    av_qsv_list_rem(list, *item);
    av_qsv_list_close(item);

    if(to_remove_list){
        for (i = av_qsv_list_count(to_remove_list); i > 0; i--){
            to_remove_atom_list = av_qsv_list_item(to_remove_list, i-1);
            for (x = av_qsv_list_count(to_remove_atom_list); x > 0; x--){
                to_remove_atom = av_qsv_list_item(to_remove_atom_list, x-1);
                av_qsv_flush_stages(list,&to_remove_atom);
            }
        }
        av_qsv_list_close(&to_remove_list);
    }
}

av_qsv_list *av_qsv_pipe_by_stage(av_qsv_list * list, av_qsv_stage * stage)
{
    av_qsv_list *item = 0;
    av_qsv_stage *cur_stage = 0;
    int i = 0;
    int a = 0;
    for (i = 0; i < av_qsv_list_count(list); i++) {
        item = av_qsv_list_item(list, i);
        for (a = 0; a < av_qsv_list_count(item); a++) {
            cur_stage = av_qsv_list_item(item, a);
            if (cur_stage == stage)
                return item;
        }
    }
    return 0;
}

// no duplicate of the same value, if end == 0 : working over full length
void av_qsv_dts_ordered_insert(av_qsv_context * qsv, int start, int end,
                            int64_t dts, int iter)
{
    av_qsv_dts *cur_dts = 0;
    av_qsv_dts *new_dts = 0;
    int i = 0;
    int mut_ret = 0;


    if (iter == 0 && qsv->qts_seq_mutex){
        mut_ret = pthread_mutex_lock(qsv->qts_seq_mutex);
        if(mut_ret)
            hb_log("QSV: pthread_mutex_lock issue[%d] at %s", mut_ret, __FUNCTION__);
    }

    if (end == 0)
        end = av_qsv_list_count(qsv->dts_seq);

    if (end <= start) {
        new_dts = av_mallocz(sizeof(av_qsv_dts));
        if( new_dts ) {
            new_dts->dts = dts;
            av_qsv_list_add(qsv->dts_seq, new_dts);
        }
    } else
        for (i = end; i > start; i--) {
            cur_dts = av_qsv_list_item(qsv->dts_seq, i - 1);
            if (cur_dts->dts < dts) {
                new_dts = av_mallocz(sizeof(av_qsv_dts));
                if( new_dts ) {
                    new_dts->dts = dts;
                    av_qsv_list_insert(qsv->dts_seq, i, new_dts);
                }
                break;
            } else if (cur_dts->dts == dts)
                break;
        }
    if (iter == 0 && qsv->qts_seq_mutex){
        mut_ret = pthread_mutex_unlock(qsv->qts_seq_mutex);
        if(mut_ret)
            hb_log("QSV: pthread_mutex_unlock issue[%d] at %s", mut_ret, __FUNCTION__);
    }
}

void av_qsv_dts_pop(av_qsv_context * qsv)
{
    av_qsv_dts *item = 0;
    int mut_ret = 0;

    if (qsv && qsv->qts_seq_mutex){
        mut_ret = pthread_mutex_lock(qsv->qts_seq_mutex);
        if(mut_ret)
            hb_log("QSV: pthread_mutex_lock issue[%d] at %s", mut_ret, __FUNCTION__);
    }

    if (av_qsv_list_count(qsv->dts_seq)) {
        item = av_qsv_list_item(qsv->dts_seq, 0);
        av_qsv_list_rem(qsv->dts_seq, item);
        av_free(item);
    }
    if (qsv && qsv->qts_seq_mutex){
        mut_ret = pthread_mutex_unlock(qsv->qts_seq_mutex);
        if(mut_ret)
            hb_log("QSV: pthread_mutex_lock issue[%d] at %s", mut_ret, __FUNCTION__);
        }
}


av_qsv_list *av_qsv_list_init(int is_threaded)
{
    av_qsv_list *l;
    int mut_ret;

    l = av_mallocz(sizeof(av_qsv_list));
    if (!l)
        return 0;
    l->items = av_mallocz(AV_QSV_JOB_SIZE_DEFAULT * sizeof(void *));
    if (!l->items)
        return 0;
    l->items_alloc = AV_QSV_JOB_SIZE_DEFAULT;

    if (is_threaded) {
        l->mutex = av_mallocz(sizeof(pthread_mutex_t));
        if (l->mutex){
            mut_ret = pthread_mutexattr_init(&l->mta);
            if( mut_ret )
                hb_log("QSV: pthread_mutexattr_init issue[%d] at %s", mut_ret, __FUNCTION__);
            mut_ret = pthread_mutexattr_settype(&l->mta, PTHREAD_MUTEX_RECURSIVE /*PTHREAD_MUTEX_ERRORCHECK*/);
            if( mut_ret )
                hb_log("QSV: pthread_mutexattr_settype issue[%d] at %s", mut_ret, __FUNCTION__);
            mut_ret = pthread_mutex_init(l->mutex, &l->mta);
            if( mut_ret )
                hb_log("QSV: pthread_mutex_init issue[%d] at %s", mut_ret, __FUNCTION__);
        }
    } else
        l->mutex = 0;
    return l;
}

int av_qsv_list_count(av_qsv_list * l)
{
    int count;

    av_qsv_list_lock(l);
    count = l->items_count;
    av_qsv_list_unlock(l);
    return count;
}

int av_qsv_list_add(av_qsv_list * l, void *p)
{
    int pos = -1;

    if (!p) {
        return pos;
    }

    av_qsv_list_lock(l);

    if (l->items_count == l->items_alloc) {
        /* We need a bigger boat */
        l->items_alloc += AV_QSV_JOB_SIZE_DEFAULT;
        l->items = av_realloc(l->items, l->items_alloc * sizeof(void *));
    }

    l->items[l->items_count] = p;
    pos = (l->items_count);
    l->items_count++;

    av_qsv_list_unlock(l);

    return pos;
}

void av_qsv_list_rem(av_qsv_list * l, void *p)
{
    int i;

    av_qsv_list_lock(l);

    /* Find the item in the list */
    for (i = 0; i < l->items_count; i++) {
        if (l->items[i] == p) {
            /* Shift all items after it sizeof( void * ) bytes earlier */
            memmove(&l->items[i], &l->items[i + 1],
                    (l->items_count - i - 1) * sizeof(void *));

            l->items_count--;
            break;
        }
    }

    av_qsv_list_unlock(l);
}

void *av_qsv_list_item(av_qsv_list * l, int i)
{
    void *ret = NULL;

    if (i < 0)
        return NULL;

    av_qsv_list_lock(l);
    if( i < l->items_count)
        ret = l->items[i];
    av_qsv_list_unlock(l);
    return ret;
}

void av_qsv_list_insert(av_qsv_list * l, int pos, void *p)
{

    if (!p)
        return;

    av_qsv_list_lock(l);

    if (l->items_count == l->items_alloc) {
        l->items_alloc += AV_QSV_JOB_SIZE_DEFAULT;
        l->items = av_realloc(l->items, l->items_alloc * sizeof(void *));
    }

    if (l->items_count != pos) {
        memmove(&l->items[pos + 1], &l->items[pos],
                (l->items_count - pos) * sizeof(void *));
    }

    l->items[pos] = p;
    l->items_count--;

    av_qsv_list_unlock(l);
}

void av_qsv_list_close(av_qsv_list ** _l)
{
    av_qsv_list *l = *_l;
    int mut_ret;

    av_qsv_list_lock(l);

    av_free(l->items);

    if (l->mutex){
        mut_ret = pthread_mutex_unlock(l->mutex);
        if (mut_ret)
            hb_log("QSV: pthread_mutex_unlock issue[%d] at %s", mut_ret, __FUNCTION__);
        mut_ret = pthread_mutex_destroy(l->mutex);
        if (mut_ret)
            hb_log("QSV: pthread_mutex_destroy issue[%d] at %s", mut_ret, __FUNCTION__);
        mut_ret = pthread_mutexattr_destroy(&l->mta);
        if (mut_ret)
            hb_log("QSV: pthread_mutexattr_destroy issue[%d] at %s", mut_ret, __FUNCTION__);
    }
    av_freep(_l);
}

int av_qsv_list_lock(av_qsv_list *l){
    int ret = 0;
    if (l->mutex){
        ret = pthread_mutex_lock(l->mutex);
        if( ret )
            hb_log("QSV: pthread_mutex_lock issue[%d] at %s", ret, __FUNCTION__);
    }
    return ret;
}

int av_qsv_list_unlock(av_qsv_list *l){
    int ret = 0;
    if (l->mutex){
        ret = pthread_mutex_unlock(l->mutex);
        if( ret )
            hb_log("QSV: pthread_mutex_unlock issue[%d] at %s", ret, __FUNCTION__);
    }
    return ret;
}

int av_is_qsv_available(mfxIMPL impl, mfxVersion * ver)
{
    mfxStatus sts = MFX_ERR_NONE;
    mfxSession mfx_session;

    memset(&mfx_session, 0, sizeof(mfxSession));
    sts = MFXInit(impl, ver, &mfx_session);
    if (sts >= 0)
        MFXClose(mfx_session);
    return sts;
}

void av_qsv_wait_on_sync(av_qsv_context *qsv, av_qsv_stage *stage)
{
    int iter = 0;
    mfxStatus sts = MFX_ERR_NONE;
    if( stage )
        if(*stage->out.sync->p_sync){
            while(1){
                iter++;
                sts = MFXVideoCORE_SyncOperation(qsv->mfx_session,*stage->out.sync->p_sync, AV_QSV_SYNC_TIME_DEFAULT);
                if(MFX_WRN_IN_EXECUTION == sts){

                    if(iter>20)
                        AV_QSV_DEBUG_ASSERT(1, "Sync failed");

                    av_qsv_sleep(10);
                    continue;
                }
                AV_QSV_CHECK_RET(sts, MFX_ERR_NONE, sts);
                break;
            }
        }
}

#endif // USE_QSV