From patchwork Fri Jul 21 01:52:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 4404 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp317093vsb; Thu, 20 Jul 2017 18:53:09 -0700 (PDT) X-Received: by 10.28.109.218 with SMTP id b87mr3935063wmi.114.1500601989440; Thu, 20 Jul 2017 18:53:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500601989; cv=none; d=google.com; s=arc-20160816; b=OZMtqghDBexaXvul7VtmcH4DPQwuaAPg/TI+c6vyqZYMMNWVW21EAw3sVZWMybI8WW d3l+t3N/sdgYLkSn2hcNO8ktcjEvR5ih743Z+lJgQkbi99Tm7S68/zBA+rPWxA/h2BRJ hflkSxI1FEuMTHg0pOW2nJMNzq1lG/rPub7EizTbMxiMWEvUKpG4Ke51jHKMGGYTZCPq YcAmGmZ71mjA56uspPl2nZRB3WZyCwk3GUXS4Pm2ps+1kDGfQGUwr5CKyq6kNWLGWqzE B4BjpAlAJHAGGUnKnNsaVD4rLTC10Zi0iUy0dYeb5fR0AY/rcGLqWoOD6VvzNJ0abyS0 tOmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :content-language:mime-version:user-agent:date:message-id:from:to :dkim-signature:delivered-to:arc-authentication-results; bh=Z88FWgC/znyRoDJUTuVSWyQe0lIUpNhF8gvPNNmvDM0=; b=v7Mw6ZUdI37YQ66fGYW25WcuPPi1xMrYW91jKrR+AFq1/O1JTpvuVCMxh8QJNOl7C+ 3dlZMnt1PZCOK4tg1dtijtXjrylugncCAcg3+xdRiki+5JBKo2g3o2Ipm3EPoqSCzQi+ WCaMIOKK8WpBog7UL9ShxyGUQTYDkoE/OQurxpJ2+Ns7qxu9dFeeOVf5qgmsB+8v/dVx CIH4XgkN1I9nMDUkJ0JGyQPk0r9Bu4Dv4OxaCqf+jdqidK9gst0CZvz5L4jnSiajMYGJ OOyoaOA9UjeeBamx/qrXwmtej8lbBVwG3poALtF+r1fRD7SuoMciys2ZSJiB3zXiGlY3 UM1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=qGmm/Cwm; 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=NONE 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 x127si189100wmg.64.2017.07.20.18.53.08; Thu, 20 Jul 2017 18:53:09 -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=qGmm/Cwm; 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=NONE 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 153C7689A83; Fri, 21 Jul 2017 04:52:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0D4168982B for ; Fri, 21 Jul 2017 04:52:49 +0300 (EEST) Received: by mail-pf0-f195.google.com with SMTP id q85so3642521pfq.2 for ; Thu, 20 Jul 2017 18:52:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=wQ2Xcv0kwu+54LnYAx4+vCO6rBvRnS+nNMVfRZIygr0=; b=qGmm/CwmuyhsPJNWlDt4zs+0PsUTyggKXOFIwNpNF1VO6ZkUs/xGVFcYNYMtSXHM01 dXJwS4Kr6ncrnAoXKzRalndmqwxpv1jKY2iJgfg54kTYA+d5vP9/W4tExci6smDiY+uv xZ9cu9pwh52C0NMU5oHw2WtowUO9t02CjPozyd6C46KGGYmIW6oC1q58b4FelfcoM0ic P7Ph05EFj5S587RFW038wZjQh1n7cBm40OBDVnnBP1Oy7nRAJSNMY+ypqPcZPeQl5iGU H2EDdKSSChR/RnXf6Rv+xrYWf4ZRpdnkMGylminCguGY6A42M+htfmwdvRqIec1oGw6y iFfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=wQ2Xcv0kwu+54LnYAx4+vCO6rBvRnS+nNMVfRZIygr0=; b=ToGPhahOuh4xEXhYD/xo7p2pv1QSUFuxJsrYXjmKJnXuqoLVsLLwACUsMvX1X6itak LGLSopT/xy3diSQQ7LJsuLyJcHHfRo1T4eyJWGJJOEN4J8CjKyob+wrH0247xz9egMpH z8a3FEzd6yruJeqUO/gerzW69Q0vkvWEti1NKjskgApx8ZJVGjr2+YHgK79eNCpRktQj +Od7tuXRywL8319LPqEXner5arwzNLoM9MCvNS80EHrFp9X9OBngk2PcSPHqDyPDAFBz KglOMlU/YqAEitWdJ5x/0C9tIsobQvHynaFE/P/DUBmUjm3Zh5Hz0zPmCI3KCenhearJ 6oog== X-Gm-Message-State: AIVw1107Z+mpwHKIZVhxw2AfrFOPidkwk7oVjj5eejkskmCdjivmgeLr 9Y6Fpmm3Y39fVg== X-Received: by 10.99.56.68 with SMTP id h4mr5845240pgn.52.1500601976264; Thu, 20 Jul 2017 18:52:56 -0700 (PDT) Received: from [10.67.68.84] ([192.55.54.45]) by smtp.gmail.com with ESMTPSA id q79sm5817140pfi.99.2017.07.20.18.52.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jul 2017 18:52:54 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson , wm4 , "Liu, Kaixuan" From: Jun Zhao Message-ID: <4410ce34-2015-822d-149d-ec751732c576@gmail.com> Date: Fri, 21 Jul 2017 09:52:51 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH V5] examples/hw_decode: Add a HWAccel decoding example. 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" V5: remove qsv/cuda in the example and Mark have test dxva2|d3d11va, videotoolbox might work as well. V4: fix potential memory leak issue base on Steven Liu's review. V3: re-work to support the other hwaccels, rename from vaapi_dec.c to hw_decode.c. just test with vaapi, dxva2|d3d11va|videotoolbox might work as well. V2: re-work with new hw decoding API. From df532eb81df163eb11f3d6c9c8bdc13fdc96ed32 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 20 Jul 2017 00:58:56 -0400 Subject: [PATCH V5] examples/hw_decode: Add a HWAccel decoding example. Add a HWAccel decoding example. Test with vaapi|dxva2|d3d11va (Mark help to test dxva2|d3d11va), videotoolbox might work as well. Signed-off-by: Liu, Kaixuan Signed-off-by: Jun Zhao --- doc/examples/hw_decode.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 doc/examples/hw_decode.c diff --git a/doc/examples/hw_decode.c b/doc/examples/hw_decode.c new file mode 100644 index 0000000000..c27e0d33e6 --- /dev/null +++ b/doc/examples/hw_decode.c @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2017 Jun Zhao + * Copyright (c) 2017 Kaixuan Liu + * + * HW Acceleration API (video decoding) decode 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 + * HW-Accelerated decoding example. + * + * @example hw_decode.c + * This example shows how to do HW-accelerated decoding with output + * frames from the HW video surfaces. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +static AVBufferRef *hw_device_ctx = NULL; +static enum AVPixelFormat hw_pix_fmt; +FILE *output_file = NULL; + +static enum AVPixelFormat find_fmt_by_hw_type(const enum AVHWDeviceType type) +{ + enum AVPixelFormat fmt; + + switch (type) { + case AV_HWDEVICE_TYPE_VAAPI: + fmt = AV_PIX_FMT_VAAPI; + break; + case AV_HWDEVICE_TYPE_DXVA2: + fmt = AV_PIX_FMT_DXVA2_VLD; + break; + case AV_HWDEVICE_TYPE_D3D11VA: + fmt = AV_PIX_FMT_D3D11; + break; + case AV_HWDEVICE_TYPE_VDPAU: + fmt = AV_PIX_FMT_VDPAU; + break; + case AV_HWDEVICE_TYPE_VIDEOTOOLBOX: + fmt = AV_PIX_FMT_VIDEOTOOLBOX; + break; + default: + fmt = AV_PIX_FMT_NONE; + break; + } + + return fmt; +} + +static int hw_decoder_init(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 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_write(AVCodecContext *avctx, AVPacket *packet) +{ + AVFrame *frame = NULL, *sw_frame = NULL; + AVFrame *tmp_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; + } + + while (ret >= 0) { + if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) { + fprintf(stderr, "Can not alloc frame\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = avcodec_receive_frame(avctx, frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + else if (ret < 0) { + fprintf(stderr, "Error while decoding\n"); + goto fail; + } + + if (frame->format == hw_pix_fmt) { + /* retrieve data from GPU to CPU */ + if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) { + fprintf(stderr, "Error transferring the data to system memory\n"); + goto fail; + } + tmp_frame = sw_frame; + } else + tmp_frame = frame; + + size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width, + tmp_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 *)tmp_frame->data, + (const int *)tmp_frame->linesize, tmp_frame->format, + tmp_frame->width, tmp_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 raw data.\n"); + goto fail; + } + + fail: + av_frame_free(&frame); + av_frame_free(&sw_frame); + if (buffer) + av_freep(&buffer); + if (ret < 0) + return ret; + } + + return 0; +} + +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; + + if (argc < 4) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + av_register_all(); + + type = av_hwdevice_find_type_by_name(argv[1]); + hw_pix_fmt = find_fmt_by_hw_type(type); + if (hw_pix_fmt == -1) { + fprintf(stderr, "Cannot support '%s' in this example.\n", argv[1]); + return -1; + } + + /* open the input file */ + if (avformat_open_input(&input_ctx, argv[2], 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; + + 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; + av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0); + + if (hw_decoder_init(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 raw data */ + output_file = fopen(argv[3], "w+"); + + /* actual decoding 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_write(decoder_ctx, &packet); + + av_packet_unref(&packet); + } + + /* flush the decoder */ + packet.data = NULL; + packet.size = 0; + ret = decode_write(decoder_ctx, &packet); + av_packet_unref(&packet); + + if (output_file) + fclose(output_file); + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + av_buffer_unref(&hw_device_ctx); + + return 0; +}