From patchwork Mon Jun 11 11:22:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 9363 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:11c:0:0:0:0:0 with SMTP id c28-v6csp3993343jad; Mon, 11 Jun 2018 04:22:53 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKk1VYZihJqcdKL+43TkhExmhvGDEpZUguiIrTz4KZBE3JheaU49hbEoAfAQ1RhzWDQx3my X-Received: by 2002:a1c:978f:: with SMTP id z137-v6mr5378512wmd.40.1528716173596; Mon, 11 Jun 2018 04:22:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528716173; cv=none; d=google.com; s=arc-20160816; b=UQIWxGN338EgJrKvgAdW8U4skiBT1aVq8L46f6ATZCpHn5pQfcpPV9YLNfvGQBdoS1 baonkxftpcSMj2uryQZ2citEaSc51jqlVEuqAjYJfqWfT4NAC6HREwacR7XsaYOeHx1Q Gu1bjGJFoBYAMZJiIWSv8Ka1neebr+K7WzzG1JxYqKOLG0AFnWh9mpI2tTSPsVkuyX/8 DKxs4vojaeiztp9KernmCyUv9swh65JiYycTvyABBLudtTTvPj3mDqG0yO79K8beA99t VvIxwo9g4meQRldZwl9kqstiRhR4V7hGLfjjIEdp/YfzG9ViCH+5reU/Y5m5GjesDdqt tGpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=4Gfmz/Pe5nGTeoeDTJciy/+hsSJI0th8RREprGlDUxI=; b=HT5Eh3RfOdCgs+eQvALpTNXYrDeQ5J86t0ZWnOZ1/H5919pXyHg/FA2e2YCBU1SyF9 IyI14b6+klPU5fJ4hKUSK7Uq1Lnbza2PccwKJgYGob8OvfznqoItiKbNHh+AgguE2TH+ vCgBwRKYaxFUT5axWmH9r4M7CNW7C4dsabMW5yvIIFcqanEwQ2T4sGdQUm01Y8REi3Rc hIBYVOi1kJDQMuZ/iln1nDr/rDcWJV0Z26BhsLBytIb48iTWk3zsH5gHIdIbHxY6x8ks G2T08k0vpIuJKTFTfW2+6/KaIfIPnnQjXjCYwQXaSDXvstpK92Ife1Du2uXpjfdKep5T OSaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=skXcverv; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h17-v6si19332445wrm.212.2018.06.11.04.22.52; Mon, 11 Jun 2018 04:22:53 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=skXcverv; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E3AB868ACDF; Mon, 11 Jun 2018 14:22:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f41.google.com (mail-pl0-f41.google.com [209.85.160.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1A3E068AC12 for ; Mon, 11 Jun 2018 14:21:55 +0300 (EEST) Received: by mail-pl0-f41.google.com with SMTP id b14-v6so12171811pls.5 for ; Mon, 11 Jun 2018 04:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=w7NeDQKTniz82gC+MNFeuw/dQpQpFNoqj+mZ1YPhcXU=; b=skXcvervPE/NE5f0+e5s9/+5IuPdWvWWqjPRdamKyKm8Flhq18GxFH4nt1zAtejo6/ RDWAbfPlmLB/EqUYi6xysVcsg6cuaRx6gWG+dfb1xA5Q2kEh7Rhc9t1aEfUw3HK0ATp/ bW5ml85NpYFCk4HIjp0ZCe83SJwl6HJ95NDZdtwYVbgn5HWICwoUb/ANqCXR3Z3NveRI 0mcSiXuVbPrxtJR2F3bpXWWoRU+0oD2C0A0s5RJGl8pqdaHTi9T9c4KIszdc0y7nggeP RoA9s+pxeeyShvyNts5/CCL2uL0UVPRG+FEUkVvgJ1odCvz7uPNv3cH9h6e3qxK/eMyY 3qIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=w7NeDQKTniz82gC+MNFeuw/dQpQpFNoqj+mZ1YPhcXU=; b=sICOppR2NHB+SwzHw8ILVOXKNVivS8LDRHR1Ts6lT/W+esK8iAjCXJKn7RZvmqPjgT d+sk3HipDzfVE0m6ISKFy4qKlQnsyZ3+bpqkpV+wn4iihjiTSSyKk+DJe4juHvk1e1Va SnD3R9WxRUUAAVQ684KBmvvgZ6ZGfScgb2YwgzpaZawhlxDdIMm5OabFdPGDckuoQxQK n37uTcT2Ne43dHU8D04PGk3FC3S4h92Bm6sN7vIg4gx7aPhGQ/7cjHv1vfjm/H3mE5xr TIrxewCTr2tbiydFeyFtm3JHwAwAjIfuybdKK3ltOnz7bHykKrtIQ/KYkzfsgxSu8JN7 T2yw== X-Gm-Message-State: APt69E3bm0nL5ebkW/ciLTQxmdzBnGczMaucqWbizyb2fuwnjlrH6bq1 5WvWl1gBmJEQE5OBZxwIr+dkfg== X-Received: by 2002:a17:902:24c7:: with SMTP id l7-v6mr18227328plg.170.1528716162349; Mon, 11 Jun 2018 04:22:42 -0700 (PDT) Received: from localhost.localdomain ([47.88.54.25]) by smtp.gmail.com with ESMTPSA id b89-v6sm141063149pfd.85.2018.06.11.04.22.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 11 Jun 2018 04:22:41 -0700 (PDT) From: Jun Zhao To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Jun 2018 19:22:34 +0800 Message-Id: <1528716154-27702-1-git-send-email-mypopydev@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH] examples/vaapi_dec_scaling: init export X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Jun Zhao MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" add a vaapi decoding/scaling sample. Signed-off-by: Jun Zhao --- configure | 2 + doc/examples/Makefile | 1 + doc/examples/vaapi_dec_scaling.c | 375 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 doc/examples/vaapi_dec_scaling.c diff --git a/configure b/configure index 473be31..d28dc37 100755 --- a/configure +++ b/configure @@ -1610,6 +1610,7 @@ EXAMPLE_LIST=" scaling_video_example transcode_aac_example transcoding_example + vaapi_dec_scaling_example vaapi_encode_example vaapi_transcode_example " @@ -3445,6 +3446,7 @@ resampling_audio_example_deps="avutil swresample" scaling_video_example_deps="avutil swscale" transcode_aac_example_deps="avcodec avformat swresample" transcoding_example_deps="avfilter avcodec avformat avutil" +vaapi_dec_scaling_example_deps="avfilter avcodec avformat avutil" vaapi_encode_example_deps="avcodec avutil h264_vaapi_encoder" vaapi_transcode_example_deps="avcodec avformat avutil h264_vaapi_encoder" diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 928ff30..3ea6899 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -19,6 +19,7 @@ EXAMPLES-$(CONFIG_RESAMPLING_AUDIO_EXAMPLE) += resampling_audio EXAMPLES-$(CONFIG_SCALING_VIDEO_EXAMPLE) += scaling_video EXAMPLES-$(CONFIG_TRANSCODE_AAC_EXAMPLE) += transcode_aac EXAMPLES-$(CONFIG_TRANSCODING_EXAMPLE) += transcoding +EXAMPLES-$(CONFIG_VAAPI_DEC_SCALING_EXAMPLE) += vaapi_dec_scaling EXAMPLES-$(CONFIG_VAAPI_ENCODE_EXAMPLE) += vaapi_encode EXAMPLES-$(CONFIG_VAAPI_TRANSCODE_EXAMPLE) += vaapi_transcode diff --git a/doc/examples/vaapi_dec_scaling.c b/doc/examples/vaapi_dec_scaling.c new file mode 100644 index 0000000..6ec65da --- /dev/null +++ b/doc/examples/vaapi_dec_scaling.c @@ -0,0 +1,375 @@ +/* + * Copyright (c) 2018 Jun Zhao + * + * VA-API Acceleration API (video decoding/scaling) sample + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * VA-API-Accelerated decoding/scaling example. + * + * @example vaapi_dec_scaling.c + * This example shows how to do VA-API-accelerated decoding/scaling with output + * frames from the HW video surfaces. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static AVBufferRef *hw_device_ctx = NULL; +static enum AVPixelFormat hw_pix_fmt; +static FILE *output_file = NULL; +AVFilterContext *buffersink_ctx; +AVFilterContext *buffersrc_ctx; +AVFilterGraph *filter_graph; +const char *filter_descr = + "format=vaapi,scale_vaapi=w=iw/2:h=ih/2,hwdownload,format=nv12"; +int init_filter = 0; + +static int init_hw_decode(AVCodecContext *ctx, const enum AVHWDeviceType type) +{ + int err = 0; + + if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type, + NULL, NULL, 0)) < 0) { + fprintf(stderr, "Failed to create specified HW device.\n"); + return err; + } + ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); + + return err; +} + +static int init_filters(AVFormatContext *fmt_ctx, AVCodecContext *dec_ctx, + int video_stream_index, const char *filters_descr) +{ + char args[512]; + int ret = 0; + const AVFilter *buffersrc = avfilter_get_by_name("buffer"); + const AVFilter *buffersink = avfilter_get_by_name("buffersink"); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base; + AVBufferSrcParameters *par = av_buffersrc_parameters_alloc(); + + filter_graph = avfilter_graph_alloc(); + if (!outputs || !inputs || !filter_graph || !par) { + ret = AVERROR(ENOMEM); + goto end; + } + + /* buffer video source: the decoded frames from the decoder will be inserted here. */ + snprintf(args, sizeof(args), + "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", + dec_ctx->width, dec_ctx->height, AV_PIX_FMT_VAAPI, + time_base.num, time_base.den, + dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den); + + ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", + args, NULL, filter_graph); + if (ret < 0) { + fprintf(stderr, "Cannot create buffer source\n"); + goto end; + } + par->hw_frames_ctx = dec_ctx->hw_frames_ctx; + ret = av_buffersrc_parameters_set(buffersrc_ctx, par); + if (ret < 0) + goto end; + av_freep(&par); + + /* buffer video sink: to terminate the filter chain. */ + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, NULL, filter_graph); + if (ret < 0) { + fprintf(stderr, "Cannot create buffer sink\n"); + goto end; + } + + /* + * Set the endpoints for the filter graph. The filter_graph will + * be linked to the graph described by filters_descr. + */ + + /* + * The buffer source output must be connected to the input pad of + * the first filter described by filters_descr; since the first + * filter input label is not specified, it is set to "in" by + * default. + */ + outputs->name = av_strdup("in"); + outputs->filter_ctx = buffersrc_ctx; + outputs->pad_idx = 0; + outputs->next = NULL; + + /* + * The buffer sink input must be connected to the output pad of + * the last filter described by filters_descr; since the last + * filter output label is not specified, it is set to "out" by + * default. + */ + inputs->name = av_strdup("out"); + inputs->filter_ctx = buffersink_ctx; + inputs->pad_idx = 0; + inputs->next = NULL; + + if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr, + &inputs, &outputs, NULL)) < 0) + goto end; + + for (int i = 0; i < filter_graph->nb_filters; i++) { + filter_graph->filters[i]->hw_device_ctx = av_buffer_ref(hw_device_ctx); + if (!filter_graph->filters[i]->hw_device_ctx) { + ret = AVERROR(ENOMEM); + goto end; + } + } + + if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) + goto end; + +end: + avfilter_inout_free(&inputs); + avfilter_inout_free(&outputs); + + return ret; +} + +static enum AVPixelFormat get_hw_format(AVCodecContext *ctx, + const enum AVPixelFormat *pix_fmts) +{ + const enum AVPixelFormat *p; + + for (p = pix_fmts; *p != -1; p++) { + if (*p == hw_pix_fmt) + return *p; + } + + fprintf(stderr, "Failed to get HW surface format.\n"); + return AV_PIX_FMT_NONE; +} + +static int decode_filte(AVFormatContext *input_ctx, AVCodecContext *avctx, + int video_stream, AVPacket *packet) +{ + AVFrame *frame = NULL; + AVFrame *filt_frame = NULL; + uint8_t *buffer = NULL; + int size; + int ret = 0; + + ret = avcodec_send_packet(avctx, packet); + if (ret < 0) { + fprintf(stderr, "Error during decoding\n"); + return ret; + } + + frame = av_frame_alloc(); + filt_frame = av_frame_alloc(); + if (!frame || !filt_frame) { + fprintf(stderr, "Can not alloc frame\n"); + ret = AVERROR(ENOMEM); + return ret; + } + + while (1) { + ret = avcodec_receive_frame(avctx, frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + av_frame_free(&frame); + av_frame_free(&filt_frame); + return 0; + } else if (ret < 0) { + fprintf(stderr, "Error while decoding\n"); + goto fail; + } + + if (frame->format == hw_pix_fmt) { + if (!init_filter) { + if ((ret = init_filters(input_ctx, avctx, + video_stream, filter_descr)) < 0) { + fprintf(stderr, "Failed to init filter\n"); + goto fail; + } + init_filter = 1; + } + + /* push the decoded frame into the filtergraph */ + if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n"); + goto fail; + } + + /* pull filtered frames from the filtergraph */ + while (1) { + ret = av_buffersink_get_frame(buffersink_ctx, filt_frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + if (ret < 0) + goto fail; + + size = av_image_get_buffer_size(filt_frame->format, filt_frame->width, + filt_frame->height, 1); + buffer = av_malloc(size); + if (!buffer) { + fprintf(stderr, "Can not alloc buffer\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + ret = av_image_copy_to_buffer(buffer, size, + (const uint8_t * const *)filt_frame->data, + (const int *)filt_frame->linesize, filt_frame->format, + filt_frame->width, filt_frame->height, 1); + if (ret < 0) { + fprintf(stderr, "Can not copy image to buffer\n"); + goto fail; + } + + if ((ret = fwrite(buffer, 1, size, output_file)) < 0) { + fprintf(stderr, "Failed to dump filted raw data.\n"); + goto fail; + } + + av_frame_unref(filt_frame); + av_freep(&buffer); + } + av_frame_unref(frame); + } + } + +fail: + av_frame_free(&frame); + av_frame_free(&filt_frame); + av_freep(&buffer); + + return ret; +} + +int main(int argc, char *argv[]) +{ + AVFormatContext *input_ctx = NULL; + int video_stream, ret; + AVStream *video = NULL; + AVCodecContext *decoder_ctx = NULL; + AVCodec *decoder = NULL; + AVPacket packet; + enum AVHWDeviceType type; + int i; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + type = av_hwdevice_find_type_by_name("vaapi"); + if (type == AV_HWDEVICE_TYPE_NONE) { + fprintf(stderr, "Device type vaapi is not supported.\n"); + fprintf(stderr, "Available device types:"); + while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE) + fprintf(stderr, " %s", av_hwdevice_get_type_name(type)); + fprintf(stderr, "\n"); + return -1; + } + + /* open the input file */ + if (avformat_open_input(&input_ctx, argv[1], NULL, NULL) != 0) { + fprintf(stderr, "Cannot open input file '%s'\n", argv[2]); + return -1; + } + + if (avformat_find_stream_info(input_ctx, NULL) < 0) { + fprintf(stderr, "Cannot find input stream information.\n"); + return -1; + } + + /* find the video stream information */ + ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0); + if (ret < 0) { + fprintf(stderr, "Cannot find a video stream in the input file\n"); + return -1; + } + video_stream = ret; + + for (i = 0;; i++) { + const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i); + if (!config) { + fprintf(stderr, "Decoder %s does not support device type %s.\n", + decoder->name, av_hwdevice_get_type_name(type)); + return -1; + } + if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && + config->device_type == type) { + hw_pix_fmt = config->pix_fmt; + break; + } + } + + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) + return AVERROR(ENOMEM); + + video = input_ctx->streams[video_stream]; + if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) + return -1; + + decoder_ctx->get_format = get_hw_format; + + if (init_hw_decode(decoder_ctx, type) < 0) + return -1; + + if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { + fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream); + return -1; + } + + /* open the file to dump scaled data */ + output_file = fopen(argv[2], "w+"); + + /* actual decoding/scaling and dump the raw data */ + while (ret >= 0) { + if ((ret = av_read_frame(input_ctx, &packet)) < 0) + break; + + if (video_stream == packet.stream_index) + ret = decode_filte(input_ctx, decoder_ctx, video_stream, &packet); + + av_packet_unref(&packet); + } + + /* flush the decoder/avfilter */ + packet.data = NULL; + packet.size = 0; + ret = decode_filte(input_ctx, decoder_ctx, video_stream, &packet); + av_packet_unref(&packet); + + if (output_file) + fclose(output_file); + avfilter_graph_free(&filter_graph); + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + av_buffer_unref(&hw_device_ctx); + + return 0; +}