From patchwork Thu Jul 20 05:29:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 4376 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp1706188vsb; Wed, 19 Jul 2017 22:53:52 -0700 (PDT) X-Received: by 10.223.145.97 with SMTP id j88mr5932334wrj.15.1500530032271; Wed, 19 Jul 2017 22:53:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500530032; cv=none; d=google.com; s=arc-20160816; b=WzAPhdQjXwKDEHQiscEXUDAy7Vwl3kDOsM/DCi4zXWJVhO0BlgPgbW12fM4m8nyZva 190f9KnQPRlOmZdCAmp+FNdyheU7iN1763pt4eIwEaknC2067GPzp/CJokjyDQP5cuSO Gzl/F40WWYIsVovwUa6KjSseyrMsR8VnspYg2jM4SYAyvb1R7FRCU2U2qCRkxnrf4MK4 BsvAmI7vSbKO1X2NNx5B5o9DuzLwRMPZ8Dz27mPYh0tzyhXZRxgSxdS0lrmG0fXecQfy nhZuuUIMSa1z2Rf9hauI47OQaQ7xqvwhOrP1MADaXxglsan0P3yqbFVRoPJ3MBZRifRQ bPMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=2aAuXqBxFobaFgz85BDLKuDaqg59agYZMLvW7K4uU2I=; b=ZMxSqn8HCW4V44qkKAaAhTCNZAGV19J1+NeGEepg5RcJ80nS7tOJNVHXSaAFE3mbcR 83GIm24PvhrGD0PqstuXOqwWy46jY6+QuhSW/IjzLDcmLQKkOSaxa/HkIXXaPF0b7YoL zlnDLw9znExa61LCJqug9mPX1p7TnFUBNvH938FRMLCPofpni5is1YoeWXP1c48bHn66 BVNe+djoOG24PkuinKsn6bDEvy5l6mupGkmcRBN8+4sTkL+4SZYRAhfn5ZZvpKkILiBI JiWlHQdMcK/RGUcjjRbzwdrkwE3WblgXZmlXZwRVt1PntZ1PD1AS50M5rGMq+1Az3VFn Qj6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=L0/6ly1N; 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 d9si5158845wrc.290.2017.07.19.22.53.51; Wed, 19 Jul 2017 22:53:52 -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.b=L0/6ly1N; 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 39CFB6882DB; Thu, 20 Jul 2017 08:53:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f66.google.com (mail-pg0-f66.google.com [74.125.83.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 812996882A7 for ; Thu, 20 Jul 2017 08:53:33 +0300 (EEST) Received: by mail-pg0-f66.google.com with SMTP id y129so1844864pgy.3 for ; Wed, 19 Jul 2017 22:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:cc:message-id:date:user-agent:mime-version :content-language; bh=vRCLnQZiyXaI3RwaI16rN4/l/423+H+3JGAYcNkJBLM=; b=L0/6ly1NcWQzcApiDYyUH9+XFcCyXd9H3oEdqsDFIiJoe7z0IvNhwV6EpeCAG5UO2B 84mmG3GF12vN86vSRZiawx0U5F5FjEm4vWRmS8P7I38/uTsb6hq35n0Tdo/RMhwhlaKS P8ospO2B72catH7vD2O5CX5lJAiKOr+uAVthyC7qogxya8kSpuffW699tIZUj0HRCrNk dfii9G8Ql5Ica3CkF9u7aG3ooOOI/9wMEgXKb3tTUx9UYOVA2HatRJNcyOLq3t/YPx8D 1ExGJMwoQfbjRWuTkVFoSPerhJLW/vJKI0+K94jNx8sxsI3jbShnka7gLkANfbIBVQv5 I8Ig== 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:cc:message-id:date:user-agent :mime-version:content-language; bh=vRCLnQZiyXaI3RwaI16rN4/l/423+H+3JGAYcNkJBLM=; b=Ik1rKaF9fa9ESSrYz6Tdxs5WIIfsiASPiPZfcXALPfg7JKH/4HilthJvzALBn4pXnd UVGMUEzphMzw/Nf+vACwUi0fOg+3ey3LmCInRsPrj8wp6KVqr6SKpySjgYLTaZaSequP aF+zLtKlIu1L5PQiDR+DZlcg/VKZn0j0BhbiyTmVhthy/qB6ItQ49sBapsWA6agjsVBM x/5J0KNQx+TaZv77e8p4zDMsKqjAgL1MYyW/x8z68C4Lw18ofKTIG1kriM8P0vsNvdf6 FH3v7RwOLHyTyUFkA/wz1kfQsWsxlYEaSCsxGRYNQF+lmxQlnfFjpZj8z3nQnXVFnNJP dN1g== X-Gm-Message-State: AIVw112xQnacRE1jLl9+5A2UOqdkHfA6LjZogPE0d+MyV7UTnIjMTrSf b+CGPjjPJvYfGQ== X-Received: by 10.98.74.92 with SMTP id x89mr2514191pfa.123.1500528560375; Wed, 19 Jul 2017 22:29:20 -0700 (PDT) Received: from [10.67.68.84] ([192.55.54.42]) by smtp.gmail.com with ESMTPSA id b13sm2331265pfj.141.2017.07.19.22.29.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Jul 2017 22:29:18 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson , wm4 From: Jun Zhao Message-ID: Date: Thu, 20 Jul 2017 13:29:15 +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 V3] 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 Cc: "Liu, Kaixuan" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" V3: re-work to support the other hwaccels, just test with vaapi, dxva2|d3d11va|videotoolbox might work as well. V2: re-work with new hw decoding API. From bd8cbd5c16be3001b950f0c4ae3548909a396bc6 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 20 Jul 2017 00:58:56 -0400 Subject: [PATCH V3] examples/hw_decode: Add a HWAccel decoding example. Add a HWAccel decoding example. Just test with vaapi, dxva2|d3d11va|videotoolbox might work as well. Signed-off-by: Liu, Kaixuan Signed-off-by: Jun Zhao --- doc/examples/hw_decode.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 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..4070a60cba --- /dev/null +++ b/doc/examples/hw_decode.c @@ -0,0 +1,246 @@ +/* + * 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 hw_pix_fmts[] = { + [AV_HWDEVICE_TYPE_CUDA] = AV_PIX_FMT_CUDA, + [AV_HWDEVICE_TYPE_DXVA2] = AV_PIX_FMT_DXVA2_VLD, + [AV_HWDEVICE_TYPE_D3D11VA] = AV_PIX_FMT_D3D11VA_VLD, + [AV_HWDEVICE_TYPE_QSV] = AV_PIX_FMT_QSV, + [AV_HWDEVICE_TYPE_VAAPI] = AV_PIX_FMT_VAAPI, + [AV_HWDEVICE_TYPE_VDPAU] = AV_PIX_FMT_VDPAU, + [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = AV_PIX_FMT_VIDEOTOOLBOX, +}; + +static enum AVPixelFormat find_fmt_by_hw_type(const enum AVHWDeviceType type) +{ + if (type >= 0 && type < FF_ARRAY_ELEMS(hw_pix_fmts)) + return hw_pix_fmts[type]; + else + return AV_PIX_FMT_NONE; +} + +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())) + return AVERROR(ENOMEM); + + 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; + 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); + + /* open the input file */ + if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) { + fprintf(stderr, "Cannot open input file '%s'\n", argv[1]); + 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); + + AVStream *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; +}