| @@ -37,37 +37,6 @@ · 36 unmodified lines |
| 37 | 37 | #define IS_OPAQUE_MEMORY(mode) (mode & MFX_MEMTYPE_OPAQUE_FRAME) |
| 38 | 38 | #define IS_SYSTEM_MEMORY(mode) (mode & MFX_MEMTYPE_SYSTEM_MEMORY) |
| 39 | 39 | |
| 40 | | −typedef struct QSVFrame { |
| 41 | | − AVFrame *frame; |
| 42 | | − mfxFrameSurface1 *surface; |
| 43 | | − mfxFrameSurface1 surface_internal; /* for system memory */ |
| 44 | | − struct QSVFrame *next; |
| 45 | | −} QSVFrame; |
| 46 | | − |
| 47 | | −/* abstract struct for all QSV filters */ |
| 48 | | −struct QSVVPPContext { |
| 49 | | − mfxSession session; |
| 50 | | − int (*filter_frame) (AVFilterLink *outlink, AVFrame *frame);/* callback */ |
| 51 | | − enum AVPixelFormat out_sw_format; /* Real output format */ |
| 52 | | − mfxVideoParam vpp_param; |
| 53 | | − mfxFrameInfo *frame_infos; /* frame info for each input */ |
| 54 | | − |
| 55 | | − /* members related to the input/output surface */ |
| 56 | | − int in_mem_mode; |
| 57 | | − int out_mem_mode; |
| 58 | | − QSVFrame *in_frame_list; |
| 59 | | − QSVFrame *out_frame_list; |
| 60 | | − int nb_surface_ptrs_in; |
| 61 | | − int nb_surface_ptrs_out; |
| 62 | | − mfxFrameSurface1 **surface_ptrs_in; |
| 63 | | − mfxFrameSurface1 **surface_ptrs_out; |
| 64 | | − |
| 65 | | − /* MFXVPP extern parameters */ |
| 66 | | − mfxExtOpaqueSurfaceAlloc opaque_alloc; |
| 67 | | − mfxExtBuffer **ext_buffers; |
| 68 | | − int nb_ext_buffers; |
| 69 | | −}; |
| 70 | | − |
| 71 | 40 | static const mfxHandleType handle_types[] = { |
| 72 | 41 | MFX_HANDLE_VA_DISPLAY, |
| 73 | 42 | MFX_HANDLE_D3D9_DEVICE_MANAGER, |
| @@ -336,9 +305,11 @@ static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link) · 262 unmodified lines |
| 336 | 305 | static void clear_unused_frames(QSVFrame *list) |
| 337 | 306 | { |
| 338 | 307 | while (list) { |
| 339 | | − if (list->surface && !list->surface->Data.Locked) { |
| 340 | | − list->surface = NULL; |
| 308 | + /* list->queued==1 means the frame is not cached in VPP |
| 309 | + * process any more, it can be released to pool. */ |
| 310 | + if ((list->queued == 1) && !list->surface.Data.Locked) { |
| 341 | 311 | av_frame_free(&list->frame); |
| 312 | + list->queued = 0; |
| 342 | 313 | } |
| 343 | 314 | list = list->next; |
| 344 | 315 | } |
| @@ -361,8 +332,10 @@ static QSVFrame *get_free_frame(QSVFrame **list) · 16 unmodified lines |
| 361 | 332 | QSVFrame *out = *list; |
| 362 | 333 | |
| 363 | 334 | for (; out; out = out->next) { |
| 364 | | − if (!out->surface) |
| 335 | + if (!out->queued) { |
| 336 | + out->queued = 1; |
| 365 | 337 | break; |
| 338 | + } |
| 366 | 339 | } |
| 367 | 340 | |
| 368 | 341 | if (!out) { |
| @@ -371,8 +344,9 @@ static QSVFrame *get_free_frame(QSVFrame **list) · 2 unmodified lines |
| 371 | 344 | av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n"); |
| 372 | 345 | return NULL; |
| 373 | 346 | } |
| 374 | | − out->next = *list; |
| 375 | | − *list = out; |
| 347 | + out->queued = 1; |
| 348 | + out->next = *list; |
| 349 | + *list = out; |
| 376 | 350 | } |
| 377 | 351 | |
| 378 | 352 | return out; |
| @@ -402,7 +376,7 @@ static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *p · 23 unmodified lines |
| 402 | 376 | return NULL; |
| 403 | 377 | } |
| 404 | 378 | qsv_frame->frame = av_frame_clone(picref); |
| 405 | | − qsv_frame->surface = (mfxFrameSurface1 *)qsv_frame->frame->data[3]; |
| 379 | + qsv_frame->surface = *(mfxFrameSurface1 *)qsv_frame->frame->data[3]; |
| 406 | 380 | } else { |
| 407 | 381 | /* make a copy if the input is not padded as libmfx requires */ |
| 408 | 382 | if (picref->height & 31 || picref->linesize[0] & 31) { |
| @@ -425,27 +399,26 @@ static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *p · 16 unmodified lines |
| 425 | 399 | qsv_frame->frame = av_frame_clone(picref); |
| 426 | 400 | |
| 427 | 401 | if (map_frame_to_surface(qsv_frame->frame, |
| 428 | | − &qsv_frame->surface_internal) < 0) { |
| 402 | + &qsv_frame->surface) < 0) { |
| 429 | 403 | av_log(ctx, AV_LOG_ERROR, "Unsupported frame.\n"); |
| 430 | 404 | return NULL; |
| 431 | 405 | } |
| 432 | | − qsv_frame->surface = &qsv_frame->surface_internal; |
| 433 | 406 | } |
| 434 | 407 | |
| 435 | | − qsv_frame->surface->Info = s->frame_infos[FF_INLINK_IDX(inlink)]; |
| 436 | | − qsv_frame->surface->Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts, |
| 408 | + qsv_frame->surface.Info = s->frame_infos[FF_INLINK_IDX(inlink)]; |
| 409 | + qsv_frame->surface.Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts, |
| 437 | 410 | inlink->time_base, default_tb); |
| 438 | 411 | |
| 439 | | − qsv_frame->surface->Info.PicStruct = |
| 412 | + qsv_frame->surface.Info.PicStruct = |
| 440 | 413 | !qsv_frame->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE : |
| 441 | 414 | (qsv_frame->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF : |
| 442 | 415 | MFX_PICSTRUCT_FIELD_BFF); |
| 443 | 416 | if (qsv_frame->frame->repeat_pict == 1) |
| 444 | | − qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED; |
| 417 | + qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED; |
| 445 | 418 | else if (qsv_frame->frame->repeat_pict == 2) |
| 446 | | − qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING; |
| 419 | + qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING; |
| 447 | 420 | else if (qsv_frame->frame->repeat_pict == 4) |
| 448 | | − qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING; |
| 421 | + qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING; |
| 449 | 422 | |
| 450 | 423 | return qsv_frame; |
| 451 | 424 | } |
| @@ -476,7 +449,7 @@ static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink) · 24 unmodified lines |
| 476 | 449 | return NULL; |
| 477 | 450 | } |
| 478 | 451 | |
| 479 | | − out_frame->surface = (mfxFrameSurface1 *)out_frame->frame->data[3]; |
| 452 | + out_frame->surface = *(mfxFrameSurface1 *)out_frame->frame->data[3]; |
| 480 | 453 | } else { |
| 481 | 454 | /* Get a frame with aligned dimensions. |
| 482 | 455 | * Libmfx need system memory being 128x64 aligned */ |
| @@ -490,14 +463,12 @@ static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink) · 7 unmodified lines |
| 490 | 463 | out_frame->frame->height = outlink->h; |
| 491 | 464 | |
| 492 | 465 | ret = map_frame_to_surface(out_frame->frame, |
| 493 | | − &out_frame->surface_internal); |
| 466 | + &out_frame->surface); |
| 494 | 467 | if (ret < 0) |
| 495 | 468 | return NULL; |
| 496 | | − |
| 497 | | − out_frame->surface = &out_frame->surface_internal; |
| 498 | 469 | } |
| 499 | 470 | |
| 500 | | − out_frame->surface->Info = s->vpp_param.vpp.Out; |
| 471 | + out_frame->surface.Info = s->vpp_param.vpp.Out; |
| 501 | 472 | |
| 502 | 473 | return out_frame; |
| 503 | 474 | } |
| @@ -666,6 +637,16 @@ static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s) · 162 unmodified lines |
| 666 | 637 | return 0; |
| 667 | 638 | } |
| 668 | 639 | |
| 640 | +static unsigned int qsv_fifo_item_size(void) |
| 641 | +{ |
| 642 | + return sizeof(mfxSyncPoint) + sizeof(QSVFrame*); |
| 643 | +} |
| 644 | + |
| 645 | +static unsigned int qsv_fifo_size(const AVFifoBuffer* fifo) |
| 646 | +{ |
| 647 | + return av_fifo_size(fifo)/qsv_fifo_item_size(); |
| 648 | +} |
| 649 | + |
| 669 | 650 | int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param) |
| 670 | 651 | { |
| 671 | 652 | int i; |
| @@ -738,7 +719,17 @@ int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *p · 66 unmodified lines |
| 738 | 719 | s->vpp_param.ExtParam = param->ext_buf; |
| 739 | 720 | } |
| 740 | 721 | |
| 741 | | − s->vpp_param.AsyncDepth = 1; |
| 722 | + s->got_frame = 0; |
| 723 | + |
| 724 | + /** keep fifo size at least 1. Even when async_depth is 0, fifo is used. */ |
| 725 | + s->async_fifo = av_fifo_alloc((param->async_depth + 1) * qsv_fifo_item_size()); |
| 726 | + s->async_depth = param->async_depth; |
| 727 | + if (!s->async_fifo) { |
| 728 | + ret = AVERROR(ENOMEM); |
| 729 | + goto failed; |
| 730 | + } |
| 731 | + |
| 732 | + s->vpp_param.AsyncDepth = param->async_depth; |
| 742 | 733 | |
| 743 | 734 | if (IS_SYSTEM_MEMORY(s->in_mem_mode)) |
| 744 | 735 | s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY; |
| @@ -793,6 +784,7 @@ int ff_qsvvpp_free(QSVVPPContext **vpp) · 48 unmodified lines |
| 793 | 784 | av_freep(&s->surface_ptrs_out); |
| 794 | 785 | av_freep(&s->ext_buffers); |
| 795 | 786 | av_freep(&s->frame_infos); |
| 787 | + av_fifo_free(s->async_fifo); |
| 796 | 788 | av_freep(vpp); |
| 797 | 789 | |
| 798 | 790 | return 0; |
| @@ -803,9 +795,29 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr · 4 unmodified lines |
| 803 | 795 | AVFilterContext *ctx = inlink->dst; |
| 804 | 796 | AVFilterLink *outlink = ctx->outputs[0]; |
| 805 | 797 | mfxSyncPoint sync; |
| 806 | | − QSVFrame *in_frame, *out_frame; |
| 798 | + QSVFrame *in_frame, *out_frame, *tmp; |
| 807 | 799 | int ret, filter_ret; |
| 808 | 800 | |
| 801 | + while (s->eof && qsv_fifo_size(s->async_fifo)) { |
| 802 | + av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL); |
| 803 | + av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL); |
| 804 | + if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0) |
| 805 | + av_log(ctx, AV_LOG_WARNING, "Sync failed.\n"); |
| 806 | + |
| 807 | + filter_ret = s->filter_frame(outlink, tmp->frame); |
| 808 | + if (filter_ret < 0) { |
| 809 | + av_frame_free(&tmp->frame); |
| 810 | + ret = filter_ret; |
| 811 | + break; |
| 812 | + } |
| 813 | + tmp->queued--; |
| 814 | + s->got_frame = 1; |
| 815 | + tmp->frame = NULL; |
| 816 | + }; |
| 817 | + |
| 818 | + if (!picref) |
| 819 | + return 0; |
| 820 | + |
| 809 | 821 | in_frame = submit_frame(s, inlink, picref); |
| 810 | 822 | if (!in_frame) { |
| 811 | 823 | av_log(ctx, AV_LOG_ERROR, "Failed to submit frame on input[%d]\n", |
| @@ -821,8 +833,8 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr · 9 unmodified lines |
| 821 | 833 | } |
| 822 | 834 | |
| 823 | 835 | do { |
| 824 | | − ret = MFXVideoVPP_RunFrameVPPAsync(s->session, in_frame->surface, |
| 825 | | − out_frame->surface, NULL, &sync); |
| 836 | + ret = MFXVideoVPP_RunFrameVPPAsync(s->session, &in_frame->surface, |
| 837 | + &out_frame->surface, NULL, &sync); |
| 826 | 838 | if (ret == MFX_WRN_DEVICE_BUSY) |
| 827 | 839 | av_usleep(500); |
| 828 | 840 | } while (ret == MFX_WRN_DEVICE_BUSY); |
| @@ -833,20 +845,33 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr · 4 unmodified lines |
| 833 | 845 | ret = AVERROR(EAGAIN); |
| 834 | 846 | break; |
| 835 | 847 | } |
| 848 | + out_frame->frame->pts = av_rescale_q(out_frame->surface.Data.TimeStamp, |
| 849 | + default_tb, outlink->time_base); |
| 836 | 850 | |
| 837 | | − if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0) |
| 838 | | − av_log(ctx, AV_LOG_WARNING, "Sync failed.\n"); |
| 851 | + out_frame->queued++; |
| 852 | + av_fifo_generic_write(s->async_fifo, &out_frame, sizeof(out_frame), NULL); |
| 853 | + av_fifo_generic_write(s->async_fifo, &sync, sizeof(sync), NULL); |
| 839 | 854 | |
| 840 | | − out_frame->frame->pts = av_rescale_q(out_frame->surface->Data.TimeStamp, |
| 841 | | − default_tb, outlink->time_base); |
| 842 | 855 | |
| 843 | | − filter_ret = s->filter_frame(outlink, out_frame->frame); |
| 844 | | − if (filter_ret < 0) { |
| 845 | | − av_frame_free(&out_frame->frame); |
| 846 | | − ret = filter_ret; |
| 847 | | − break; |
| 856 | + if (qsv_fifo_size(s->async_fifo) > s->async_depth) { |
| 857 | + av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL); |
| 858 | + av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL); |
| 859 | + |
| 860 | + do { |
| 861 | + ret = MFXVideoCORE_SyncOperation(s->session, sync, 1000); |
| 862 | + } while (ret == MFX_WRN_IN_EXECUTION); |
| 863 | + |
| 864 | + filter_ret = s->filter_frame(outlink, tmp->frame); |
| 865 | + if (filter_ret < 0) { |
| 866 | + av_frame_free(&tmp->frame); |
| 867 | + ret = filter_ret; |
| 868 | + break; |
| 869 | + } |
| 870 | + |
| 871 | + tmp->queued--; |
| 872 | + s->got_frame = 1; |
| 873 | + tmp->frame = NULL; |
| 848 | 874 | } |
| 849 | | − out_frame->frame = NULL; |
| 850 | 875 | } while(ret == MFX_ERR_MORE_SURFACE); |
| 851 | 876 | |
| 852 | 877 | return ret; |