From patchwork Thu Jul 20 06:17:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Steven Liu X-Patchwork-Id: 4378 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp1734759vsb; Wed, 19 Jul 2017 23:25:06 -0700 (PDT) X-Received: by 10.223.128.42 with SMTP id 39mr6277971wrk.175.1500531906306; Wed, 19 Jul 2017 23:25:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500531906; cv=none; d=google.com; s=arc-20160816; b=zrfiMmS/F9bPsLwfZp6aUJfid1zIxiin0T4L6rSfCg5SGZMGDX0KqXuWjhOO2RvUB6 KZFA7ve0o9GWJCPs+KN6Q1z9MlLm/IJGMtmo1GKgPV/C/Z/Q7B1CuP39TsPDwdv2n/sw PimFlKMeLOZDfEThUbInhEkjokbl02VXjoL5ch5AvU+Nkik3q7OpcgmufJqC+Ji6+4W1 CTLnzjEwInmg2nG0otfpzIDpOjLLnaJNz/ICgva8/r4OPsAu8klnm9TGhl21UteCSjQa rJr6bj9dXIPJ4oQjNws52DGu8oHrgjIZ5cxYYXxfl/yZ5GPpT9P9bRYn3LRhWtpSrFUs wfjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:message-id:date:from:references :in-reply-to:mime-version:dkim-signature:delivered-to :arc-authentication-results; bh=exgRlvoWR54yjH+24/QjZEnE1PfwWZYlC+vmDwRIEoE=; b=zXq75NmATfvkFVNqbxFwjgn4u/5xDqQGy0KZIOp4dh2do1iHgCjeTZXKpQkDXAWMDb qOyLVxZMgW1ZJbrNL3UJ5jAOVrFvgJY8IB1Z4MCv0Bd3KpDlCdfLBAtBFRM0M0NuQ3nE YfaN/BX5yW9qFywqLg8kYhLoqxVxCXxkQa+tJZ0HHZIWJs70tPSiA/aQ4P3VZbXPNePM bime4U+DI0edw/5o7EO53VROyR1thqAE4lsAbSlRcZdtpRrnEkygoMwzbPzSFe2fbUKJ KgfW+5COZCwva8teoHhXhfzPdUlsEfBynEiobVNbcM/PUKc0ZeDQ8Xtzw1CF82lBNGu/ qTOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=p8bEUao4; 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 54si5485358wru.27.2017.07.19.23.25.05; Wed, 19 Jul 2017 23:25:06 -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=p8bEUao4; 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 B8E00688366; Thu, 20 Jul 2017 09:24:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yb0-f195.google.com (mail-yb0-f195.google.com [209.85.213.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 23530688242 for ; Thu, 20 Jul 2017 09:24:48 +0300 (EEST) Received: by mail-yb0-f195.google.com with SMTP id f135so580698yba.2 for ; Wed, 19 Jul 2017 23:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=W4oc9dU999uScm10IAcIOoVfRvAFeOS0VOlid4VIFE8=; b=p8bEUao4xcmQWV5qXhpDqD1BpRpVkR240bjIpELbECFSyML6YA6N3HpntdNS91lkHF VTzuNEOn60KuboFDs93Yh7L1zT5n7Kj/1fc98dxkCtKxf0Ikam93bVVB0UZ3eNDSe3Ne wMEONd7M4088GLC0YxXjjmFoKPXZ+0tNrXAzCjkzBa7dnXNeBgU6VWh24e7b53505Yal dm8vOPyudobCoU6DDarGyEXAzoD57IaGd1eRMsypW5PdOrROkxolB9B7GGmzFmMApc37 XpDKaaiIEASndtuPlAb4xileh5kAo7ab/rKMsvJKjUbDv0VMQDjEOxemTPhc7QJ2+pep +kag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=W4oc9dU999uScm10IAcIOoVfRvAFeOS0VOlid4VIFE8=; b=QChnLS2KG8z58okHNLguNNKND5XRKzaGPYrPvRDOsbzpj9K1lnpPRkT2q9R4YC+/1M R6dP5OZ/ytcNhttRWR2j1ju7IOif0M3bVEND2krrHfrgU6d6i5AT/vHSkQF633gQ+2i4 mwPT4MTf+HuF9aPEBgG1+4e5AswFqe61PzwpDfLr1cCVimbeFKxQwx1uYqrGCAKp3A7B JxXZNU+p9QoweM9uGiQ+Iu3+jYaiiy8iWahEarXahR3K+6G9xu70ne/AhlJzKh8Wd4JW f9zoOurENx0JVfsXYAW2yZO1wATGL8OZsTCBtbahvs1FQktwctluGhc+5GZjMY6LeZaX wncQ== X-Gm-Message-State: AIVw11394mj4b1JLV3aQt9VQ0Uv1nU8G9c6jIZ+LKXgYxPCbZn7Oa1UC a3g8VDsa2c+YBBRHQ3vkd76kRMJZ1UrH X-Received: by 10.37.172.83 with SMTP id r19mr2564933ybd.174.1500531430071; Wed, 19 Jul 2017 23:17:10 -0700 (PDT) MIME-Version: 1.0 Received: by 10.13.203.149 with HTTP; Wed, 19 Jul 2017 23:17:09 -0700 (PDT) In-Reply-To: References: From: Steven Liu Date: Thu, 20 Jul 2017 14:17:09 +0800 Message-ID: To: FFmpeg development discussions and patches Subject: Re: [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: Mark Thompson , "Liu, Kaixuan" , wm4 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 2017-07-20 13:29 GMT+08:00 Jun Zhao : > 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. > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel > 已打开会话。包含1封未读邮件。 跳至内容 通过屏幕阅读器使用 Gmail 搜索 Gmail 写邮件 标签 收件箱 (1) 已加星标 重要邮件 聊天记录 已发邮件 草稿 所有邮件 垃圾邮件 (27) 已删除邮件 [Gmail]/Gluster-review (493) [Gmail]Gluster-review [Imap]/Drafts [Imap]/Trash 工作邮件 旅行相关 收据 私人邮件 arm-linux (2) btrfs (25) Deleted Messages ffmpeg (5,060) ffmpeg-devel (16,285) glusterfs (945) kernel-cn libav (2,049) Notes OpenBSD_ARM OpenBSD-announce (1) rfc (567) Sent Messages SMS 显示更多标签 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); If the frame alloc success and the sw_frame alloc failure, maybe it is memleak? or if it is just a sample, that is not the impotant point? What about use ret = AVERROR(ENOMEM), goto fail, and add if(frame) av_frame_free(&frame); if (sw_frame) av_frame_free(&sw_frame); + + 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);