| @@ -37,37 +37,6 @@ · 36 unmodified lines |
| 37 | #define IS_OPAQUE_MEMORY(mode) (mode & MFX_MEMTYPE_OPAQUE_FRAME) | 37 | #define IS_OPAQUE_MEMORY(mode) (mode & MFX_MEMTYPE_OPAQUE_FRAME) |
| 38 | #define IS_SYSTEM_MEMORY(mode) (mode & MFX_MEMTYPE_SYSTEM_MEMORY) | 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 | static const mfxHandleType handle_types[] = { | 40 | static const mfxHandleType handle_types[] = { |
| 72 | MFX_HANDLE_VA_DISPLAY, | 41 | MFX_HANDLE_VA_DISPLAY, |
| 73 | MFX_HANDLE_D3D9_DEVICE_MANAGER, | 42 | MFX_HANDLE_D3D9_DEVICE_MANAGER, |
| @@ -336,9 +305,11 @@ static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link) · 262 unmodified lines |
| 336 | static void clear_unused_frames(QSVFrame *list) | 305 | static void clear_unused_frames(QSVFrame *list) |
| 337 | { | 306 | { |
| 338 | while (list) { | 307 | while (list) { |
| 339 | if (list->surface && !list->surface->Data.Locked) { | 308 | /* list->queued==1 means the frame is not cached in VPP |
| 340 | list->surface = NULL; | 309 | * process any more, it can be released to pool. */ |
| | 310 | if ((list->queued == 1) && !list->surface.Data.Locked) { |
| 341 | av_frame_free(&list->frame); | 311 | av_frame_free(&list->frame); |
| | 312 | list->queued = 0; |
| 342 | } | 313 | } |
| 343 | list = list->next; | 314 | list = list->next; |
| 344 | } | 315 | } |
| @@ -361,8 +332,10 @@ static QSVFrame *get_free_frame(QSVFrame **list) · 16 unmodified lines |
| 361 | QSVFrame *out = *list; | 332 | QSVFrame *out = *list; |
| 362 | | 333 | |
| 363 | for (; out; out = out->next) { | 334 | for (; out; out = out->next) { |
| 364 | if (!out->surface) | 335 | if (!out->queued) { |
| | 336 | out->queued = 1; |
| 365 | break; | 337 | break; |
| | 338 | } |
| 366 | } | 339 | } |
| 367 | | 340 | |
| 368 | if (!out) { | 341 | if (!out) { |
| @@ -371,8 +344,9 @@ static QSVFrame *get_free_frame(QSVFrame **list) · 2 unmodified lines |
| 371 | av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n"); | 344 | av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n"); |
| 372 | return NULL; | 345 | return NULL; |
| 373 | } | 346 | } |
| 374 | out->next = *list; | 347 | out->queued = 1; |
| 375 | *list = out; | 348 | out->next = *list; |
| | 349 | *list = out; |
| 376 | } | 350 | } |
| 377 | | 351 | |
| 378 | return out; | 352 | return out; |
| @@ -402,7 +376,7 @@ static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *p · 23 unmodified lines |
| 402 | return NULL; | 376 | return NULL; |
| 403 | } | 377 | } |
| 404 | qsv_frame->frame = av_frame_clone(picref); | 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 | } else { | 380 | } else { |
| 407 | /* make a copy if the input is not padded as libmfx requires */ | 381 | /* make a copy if the input is not padded as libmfx requires */ |
| 408 | if (picref->height & 31 || picref->linesize[0] & 31) { | 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 | qsv_frame->frame = av_frame_clone(picref); | 399 | qsv_frame->frame = av_frame_clone(picref); |
| 426 | | 400 | |
| 427 | if (map_frame_to_surface(qsv_frame->frame, | 401 | if (map_frame_to_surface(qsv_frame->frame, |
| 428 | &qsv_frame->surface_internal) < 0) { | 402 | &qsv_frame->surface) < 0) { |
| 429 | av_log(ctx, AV_LOG_ERROR, "Unsupported frame.\n"); | 403 | av_log(ctx, AV_LOG_ERROR, "Unsupported frame.\n"); |
| 430 | return NULL; | 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)]; | 408 | qsv_frame->surface.Info = s->frame_infos[FF_INLINK_IDX(inlink)]; |
| 436 | qsv_frame->surface->Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts, | 409 | qsv_frame->surface.Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts, |
| 437 | inlink->time_base, default_tb); | 410 | inlink->time_base, default_tb); |
| 438 | | 411 | |
| 439 | qsv_frame->surface->Info.PicStruct = | 412 | qsv_frame->surface.Info.PicStruct = |
| 440 | !qsv_frame->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE : | 413 | !qsv_frame->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE : |
| 441 | (qsv_frame->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF : | 414 | (qsv_frame->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF : |
| 442 | MFX_PICSTRUCT_FIELD_BFF); | 415 | MFX_PICSTRUCT_FIELD_BFF); |
| 443 | if (qsv_frame->frame->repeat_pict == 1) | 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 | else if (qsv_frame->frame->repeat_pict == 2) | 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 | else if (qsv_frame->frame->repeat_pict == 4) | 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 | return qsv_frame; | 423 | return qsv_frame; |
| 451 | } | 424 | } |
| @@ -476,7 +449,7 @@ static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink) · 24 unmodified lines |
| 476 | return NULL; | 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 | } else { | 453 | } else { |
| 481 | /* Get a frame with aligned dimensions. | 454 | /* Get a frame with aligned dimensions. |
| 482 | * Libmfx need system memory being 128x64 aligned */ | 455 | * Libmfx need system memory being 128x64 aligned */ |
| @@ -490,14 +463,12 @@ static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink) · 7 unmodified lines |
| 490 | out_frame->frame->height = outlink->h; | 463 | out_frame->frame->height = outlink->h; |
| 491 | | 464 | |
| 492 | ret = map_frame_to_surface(out_frame->frame, | 465 | ret = map_frame_to_surface(out_frame->frame, |
| 493 | &out_frame->surface_internal); | 466 | &out_frame->surface); |
| 494 | if (ret < 0) | 467 | if (ret < 0) |
| 495 | return NULL; | 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 | return out_frame; | 473 | return out_frame; |
| 503 | } | 474 | } |
| @@ -666,6 +637,16 @@ static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s) · 162 unmodified lines |
| 666 | return 0; | 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 | int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param) | 650 | int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param) |
| 670 | { | 651 | { |
| 671 | int i; | 652 | int i; |
| @@ -738,7 +719,17 @@ int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *p · 66 unmodified lines |
| 738 | s->vpp_param.ExtParam = param->ext_buf; | 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 | if (IS_SYSTEM_MEMORY(s->in_mem_mode)) | 734 | if (IS_SYSTEM_MEMORY(s->in_mem_mode)) |
| 744 | s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY; | 735 | s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY; |
| @@ -793,6 +784,7 @@ int ff_qsvvpp_free(QSVVPPContext **vpp) · 48 unmodified lines |
| 793 | av_freep(&s->surface_ptrs_out); | 784 | av_freep(&s->surface_ptrs_out); |
| 794 | av_freep(&s->ext_buffers); | 785 | av_freep(&s->ext_buffers); |
| 795 | av_freep(&s->frame_infos); | 786 | av_freep(&s->frame_infos); |
| | 787 | av_fifo_free(s->async_fifo); |
| 796 | av_freep(vpp); | 788 | av_freep(vpp); |
| 797 | | 789 | |
| 798 | return 0; | 790 | return 0; |
| @@ -803,9 +795,29 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr · 4 unmodified lines |
| 803 | AVFilterContext *ctx = inlink->dst; | 795 | AVFilterContext *ctx = inlink->dst; |
| 804 | AVFilterLink *outlink = ctx->outputs[0]; | 796 | AVFilterLink *outlink = ctx->outputs[0]; |
| 805 | mfxSyncPoint sync; | 797 | mfxSyncPoint sync; |
| 806 | QSVFrame *in_frame, *out_frame; | 798 | QSVFrame *in_frame, *out_frame, *tmp; |
| 807 | int ret, filter_ret; | 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 | in_frame = submit_frame(s, inlink, picref); | 821 | in_frame = submit_frame(s, inlink, picref); |
| 810 | if (!in_frame) { | 822 | if (!in_frame) { |
| 811 | av_log(ctx, AV_LOG_ERROR, "Failed to submit frame on input[%d]\n", | 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 | do { | 835 | do { |
| 824 | ret = MFXVideoVPP_RunFrameVPPAsync(s->session, in_frame->surface, | 836 | ret = MFXVideoVPP_RunFrameVPPAsync(s->session, &in_frame->surface, |
| 825 | out_frame->surface, NULL, &sync); | 837 | &out_frame->surface, NULL, &sync); |
| 826 | if (ret == MFX_WRN_DEVICE_BUSY) | 838 | if (ret == MFX_WRN_DEVICE_BUSY) |
| 827 | av_usleep(500); | 839 | av_usleep(500); |
| 828 | } while (ret == MFX_WRN_DEVICE_BUSY); | 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 | ret = AVERROR(EAGAIN); | 845 | ret = AVERROR(EAGAIN); |
| 834 | break; | 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) | 851 | out_frame->queued++; |
| 838 | av_log(ctx, AV_LOG_WARNING, "Sync failed.\n"); | 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); | 856 | if (qsv_fifo_size(s->async_fifo) > s->async_depth) { |
| 844 | if (filter_ret < 0) { | 857 | av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL); |
| 845 | av_frame_free(&out_frame->frame); | 858 | av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL); |
| 846 | ret = filter_ret; | 859 | |
| 847 | break; | 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 | } while(ret == MFX_ERR_MORE_SURFACE); | 875 | } while(ret == MFX_ERR_MORE_SURFACE); |
| 851 | | 876 | |
| 852 | return ret; | 877 | return ret; |