From patchwork Wed Jul 26 02:29:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Liu X-Patchwork-Id: 4458 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp253150vsb; Tue, 25 Jul 2017 19:29:52 -0700 (PDT) X-Received: by 10.223.158.133 with SMTP id a5mr20308853wrf.188.1501036192432; Tue, 25 Jul 2017 19:29:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501036192; cv=none; d=google.com; s=arc-20160816; b=PPay2ycz/Ue9Rj3N0fMkFrp9egPsW1fUqmQE85WtANNUvpG1ZQSMJMCMXqQwsz79q9 ufGyoqNO1jD9u+tyTwp3QFoEfz/bRMtaHxKrC9zV2tjkzJjfAMZ2WlIZ1qe1ZKvLblP/ bTME+lZZ3oiWemvNWN14uJcZGXsM6GDofwd+b0tm4Y40ceEQnuT3lOwi/xftlG2QuW3G hgMB1L9AA9upS+7hBB9jRKMsP5XhcS4lYR6mfOEFyBMPqBZX2jJyXJcoY+ZgxLp9NVo3 hHEgYPgqv18SCfnztQCRIfCgzohgmV2/fqjVxz/ynSCCaKV9jAwPcbIJBmJpfzI/uSjW FFHA== 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=MNV4zIJE6/NPFNqkjZ2Y0LnMqYrNIpqEr+fG2IwnynY=; b=oopyl4NUhEfEw1p6IkcgyocztoWoVW3NwFJkGFlxN/nKlO3p2+ueROfcopd9kFV8VI rrn8ie7fRS8O/vu5H0sQhDtOmMU0fTUYYnUnsreMHIWEsBm6RE9hlTt9kE1In49521+7 KMWV+CvWhPP8EW1pR4NIS5L5/conahnEW8tOb0CHkGRjPuFAPHPWBINMAV9+4UVWrUch ZZ05YOipqGmFqZZzU8LhBTgz0Ru8RjkXGV9wCmyHD77jlyxmGKJcxMbWI6tGsHRza1oJ peC6BQcIvyCPEY8oIKyaz0DxNOA2HsjF5SnIczUOJcIN8IJVd+OFk60W+AZRzneP8m8M uxQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=dsV+zp0o; 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 p193si8477441wmg.212.2017.07.25.19.29.52; Tue, 25 Jul 2017 19:29: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=dsV+zp0o; 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 41CF868981B; Wed, 26 Jul 2017 05:29:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw0-f196.google.com (mail-yw0-f196.google.com [209.85.161.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 237A96883EA for ; Wed, 26 Jul 2017 05:29:43 +0300 (EEST) Received: by mail-yw0-f196.google.com with SMTP id u207so3934189ywc.0 for ; Tue, 25 Jul 2017 19:29:43 -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; bh=tk3iLM4o8X4yhKFsd5l81t6VtRAiUGhwzmFNC1ZnKz0=; b=dsV+zp0o4VpEVO9R+M1ozLmUCcU/Fi64buf6ra0G0P/28Unz6kWKhHRU+40bFXr8M1 z2Q6jzIQues8p6c3zaKJVTZPRI7RZGb4uJ4E+ixy/x0HP4UjQ2wD+CM34JV803ofPiAE wL0eXA2p0Cr4Cv1BRuNiwLIrp9xO5iMTHnjF7ZR67T8muYVNM/A2CsnjmhV+7E6GKEtE 61MquGL/kOI0cRyLP+pMMYEiIf4rpbIge9fOrJ6nupnMq25hCZGjtXE0exH7gHllbaCy tyiT0TCmiHHAFkIKzSwtKjMYj+u1Cb70OzJqDb0TdPhe1yIEUBe5GmfwXRlqwn8/aMo6 xm5g== 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; bh=tk3iLM4o8X4yhKFsd5l81t6VtRAiUGhwzmFNC1ZnKz0=; b=ORrmgZ3SC9NFRglxjrOR8IREqQc7fbckT4v5Qe4MbTut5MU+CWnnBw+kuyiuZST6UV ooG7QeFeRCKBHVY07R9NlrZO8lxI34W0R4se8Qg+gaXIf9tLIJJxJ746dWwzHOxqfKYN 8siN0g8JIPadCKkB/vWdR2mmmavBru3gKrcA81pYm+Rurq5exZhppTKiikiTsQp/TRe7 Wokj5OFkicBOLLIbt3CwU0fTvcyRE7BDJ79xwXioy5IHwtyDLpMcG074adSCW347vVyB RpDA6de4+OddYLohBSvIqtFbQupJ53pr8pbsqK9bWkAveVRkkIMmiFczPp+Vb1T8Wg4R A8Cg== X-Gm-Message-State: AIVw110fAU80vplQm4eylY4qQXocTA39jBLSucvOVLe6yHTRC/dY3bqZ 2Qhrew1bLY33UZL6+XIFCaynTNo1DrtR X-Received: by 10.37.114.194 with SMTP id n185mr4449286ybc.349.1501036181771; Tue, 25 Jul 2017 19:29:41 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.160.134 with HTTP; Tue, 25 Jul 2017 19:29:41 -0700 (PDT) In-Reply-To: <5a7c1bf4-393f-256a-6674-6cf5078fa410@gmail.com> References: <5a7c1bf4-393f-256a-6674-6cf5078fa410@gmail.com> From: Steven Liu Date: Wed, 26 Jul 2017 10:29:41 +0800 Message-ID: To: FFmpeg development discussions and patches Subject: Re: [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 Cc: Mark Thompson , "Liu, Kaixuan" , wm4 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 2017-07-26 9:27 GMT+08:00 Jun Zhao : > 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. > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel > 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 + av_buffer_unref(&hw_device_ctx); + + return 0; +} 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);