From patchwork Wed Jul 26 01:27: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: 4457 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp217264vsb; Tue, 25 Jul 2017 18:35:38 -0700 (PDT) X-Received: by 10.223.148.129 with SMTP id 1mr21086591wrr.28.1501032937978; Tue, 25 Jul 2017 18:35:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501032937; cv=none; d=google.com; s=arc-20160816; b=baDl54GavU1p/2VFqvoTvLL+rXY7MayRX4A6aih66FZBSVcnYbeGY44GoudPkRvN3I f8nO+k/jXf/DWYt4bamy7rB4sZQOFJvQoSQoEkm3Y7if3HB9XiZq9F2zhwFNuZnrIoVl oLA488ftJcq9I+J3fTvWVVi8A2EMEVfB9EtSf4ouQh0rKhj4gKWb6eHaNI4XL4NZWupK +v+xAdpltTy0/qxV9qJhLvfHsZBbo+CHjnLVH4UEbLpb+wfHUZck8AAQlRg65pdQfdOo Ilb0hDkT5/QVHb2+VmrzzFw/HdOYjk/GlJZsFsyAE8+QS7b+UWF4cblp0FfJPB0j1sWm 10iw== 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=3Ul/rz+dbrcsJaC88s/o/E7drkLD+rGLiXPVJ9jpG3c=; b=o0dx1gUm2oFys6VjDP0FwuRdKDMWo6yv2v/C79on4IDNN6H+t+q/wQf0wacM3fps7k Oz92st/25DVVx3KfEL3BrfChyFLCFkCzgzx5jKJKF+VoSI9vonqxilzwYI1tiSXr2V6i jZpaamBjqFSXeuIp6v2S1id/PLE36IzPPdhGD8DvYd2KOKeAEzQedKXwHIT63TiEiVAp alQEmA6qSgcQKtgCaBztRFudZQ/ciM7+C+qSb+i9UqtINxc5DN6fLOS6BRE84jz3TWey BvkOrnwhUBNYNhKIc67EmuouJYMQUYUewkZy23CmVhzWv3L9PEgezRuRd1xhi1dnIF3q mzCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=OzBguAVp; 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 u92si16124193wrc.79.2017.07.25.18.35.37; Tue, 25 Jul 2017 18:35:37 -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=OzBguAVp; 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 D9469688295; Wed, 26 Jul 2017 04:35:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f193.google.com (mail-pf0-f193.google.com [209.85.192.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C3516680B28 for ; Wed, 26 Jul 2017 04:35:27 +0300 (EEST) Received: by mail-pf0-f193.google.com with SMTP id 1so6061946pfi.3 for ; Tue, 25 Jul 2017 18:35:28 -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=+R5KfT3Q3kwUDinUTPF6sKhHq1sd86qKFdeU6DWFbPM=; b=OzBguAVpooQLdDmYZVtSn5Nm4VzurXEnO4tAblgmSK8Lr10BS+iviNE0Oa6mR5mks+ 2wEib9ROaCQ3OU9bxZfb2+Izy6TdmU28THjRZGfF/cEnKUTWDl+emLisB6z5e9SBIWQ7 YnByiU7TKjkAQeunzIWLkH97IqIwTHoH/1qQIopeaGp/yyqg6Rn8inXf5IULR+zsi86x Ktf2tlm4qVtSVa2Iytb2jvMcjKjuDMtzdx4hl0qhC026O+t6Nx2CzsnAPjNzvJF+jl63 6nu1soM6g02zQLyHg/lB2UnE3NaUGK05ww7aQ1W3CWpPEp9S0M9KM6z925/Ooa5sGTzk 4+zA== 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=+R5KfT3Q3kwUDinUTPF6sKhHq1sd86qKFdeU6DWFbPM=; b=O+Am0RLBP9pobLhkG878wQmqHgbWSGCNCooJVFY9Y1PVGrNyEl5zmzAgRbFYp2rkOw iVckgN/3hQm3igF6s/L5Q+7vaswrCZ0/EYmamFrovDHZodEngygL8h6AmXUVJ+8xqBq/ jta6Fy6NeBG1xKTHzpvxjr8EMvuBgDUfRu5TOwciliz4snbdC2UXq10dCaqyyMeHfue6 RUDLi9ySaYpTApFm5Arrgs0n+9AVjMQdnZZdjIY3DSCnFiKzKlnoJySa/zZoldlm1Zdp YQ+yPrXCm3SJuO2vsIu2laVstPou+V3GLJuZeiEEbzI0jF6CVktqOD20jgdwywpmsh10 yjoQ== X-Gm-Message-State: AIVw111+fl88jNwdmRQT09tOV6rAmGj+P1bBtsjoz2yc9Rx0c0NHw1tj 7lgTaBF4r31Vxw== X-Received: by 10.99.116.84 with SMTP id e20mr20923777pgn.62.1501032475987; Tue, 25 Jul 2017 18:27:55 -0700 (PDT) Received: from [10.67.68.29] (fmdmzpr03-ext.fm.intel.com. [192.55.54.38]) by smtp.gmail.com with ESMTPSA id z29sm30069864pff.50.2017.07.25.18.27.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Jul 2017 18:27:54 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson , wm4 , "Liu, Kaixuan" From: Jun Zhao Message-ID: <5a7c1bf4-393f-256a-6674-6cf5078fa410@gmail.com> Date: Wed, 26 Jul 2017 09:27: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 V6] 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" V6: fix memory leak issue when get EAGAIN/EOF for avcodec_receive_frame. 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 dc36e9daf2e03b4f896a462f83ee38a4929a6fc2 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 20 Jul 2017 00:58:56 -0400 Subject: [PATCH V6] 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 | 266 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 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..06cc7462fa --- /dev/null +++ b/doc/examples/hw_decode.c @@ -0,0 +1,266 @@ +/* + * 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) { + av_frame_free(&frame); + av_frame_free(&sw_frame); + return 0; + } 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; +}