From patchwork Tue Jul 11 04:06:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 4293 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp4459259vsb; Mon, 10 Jul 2017 21:12:12 -0700 (PDT) X-Received: by 10.28.91.141 with SMTP id p135mr9766324wmb.19.1499746332195; Mon, 10 Jul 2017 21:12:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499746332; cv=none; d=google.com; s=arc-20160816; b=sWhKEfxC8d5xzTkyxS4nm3NILZ4CjxQb88n3+XZ5ZpvDy3mugYxE/eqcmK10amlqMD czxJ6pKR9ikGRTKgPnbRAYp4Iqnl8JvtnBfvCRhPIIxfdI5Z/pbqti6g2dmBHPQReVjq 02nJXijsbq/vN9+OhSnbu5HPlC/ry3UeHmY9pMnhvn31L5KvZw9kQiODsb6PS2wEa1Ur eCShquLznsecR8acCd6UT5GlN6UEAtsuDe5P2RRT9DkzhUQTAO0z3C9BTIkgXQjcngA2 Wys2Zp2pa6Dd8mMlhUFjnloMDM4YN9QaQVzUMHdLMUewN+H8nVMPqQ59rxYyNevVc30v s6nA== 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=RIjp8BBwiMfnh8uKEbVmVtD0djPWt6jueVZ8Z9/8T4c=; b=DQox35GBuGwtY4v+tGx4q6KI4Z/EvP3hPdVQU8uNrQtsKXGqz5n61dQ5kvFTy1atu9 OIBan/TRWHC3brYV2708f/yIoAltwK5VLz+o8NufD62gJYd6KRytTuYfYTZI1AzpoXJA s8BpsUWvuKaQ3msm5Z0J0WQBheRCS2w6e603bB4Ac1AAFkg6ciQ6ZT+p9JD8tqRzk1Ol 5STTvHSJPSVvll6MaInpIMaZh3gICW7by+23DX+C1IKHATAgxH2SQykIzFfcgkPOAwrh JtDP34y25lnAEFUM6Z/X3bYpwYk1pWYxMqgwx2x1OGXORW2lHpVzVh7g/eWi8RTF9vlC D5Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=dHRVhNPd; 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 198si8293214wmm.132.2017.07.10.21.12.11; Mon, 10 Jul 2017 21:12:12 -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=dHRVhNPd; 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 989B7689E53; Tue, 11 Jul 2017 07:12:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 45D3F689DE3 for ; Tue, 11 Jul 2017 07:11:56 +0300 (EEST) Received: by mail-pf0-f196.google.com with SMTP id z6so17163217pfk.3 for ; Mon, 10 Jul 2017 21:12:00 -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=PzeJRBGp8sNbfoiVe38BXoZw0tm54Aoag49EeU3WbRA=; b=dHRVhNPdnRE/NZDt4bDets8ABRxMPNMSUWjT4Z8mJHDnr9XMSn5mwFFMj+sDwGufOk WnAW8WQzA1XogTLDvP0bBd7VRBDGcrdR7yb87JgV7lmOyyomUHmRZnZfidZ7tOAgFYsp Ana6GgEYhd3NOF5lydHzelNy/7TPSdIoT64RCx+aF9dyyPXxzqck2pyMt5bW9w41cfoD XavN3VrMXrTDedabX5NP4X541w10QJ3XIwsNquhHjAmyRB6BryCcOt9+LJRA3eJbIPbX jckoQ1gqWflaN4Hd7Vtz6CTTFYOg5/J8JcheP/OzQehe4yG2YoaCvhhW0uJckHraPAZ0 Pptg== 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=PzeJRBGp8sNbfoiVe38BXoZw0tm54Aoag49EeU3WbRA=; b=Qw9pxlc1Jzno402tKpP1XM4Nk80KagmSCGzVefOTgJdcL3vFntlHIdd2jJeJcVyDHV vihInSLOwPIescD4FLo9Q31+BY/XlIYSjv0zBzgYrT40ttVYz4A4juV7BOW9Cx3dU6rW ZQ103n8VCuhcW7QgLKaNHaHaEwLJ9HfXSiQ/1cfPaX+REhfozU8OC0kywKlw8XTdtKC2 qwLqnbWek7Ct9x1sitsmrkeLRz+G9bg1CnXoaUfWE0PyjBMWRJHWdEOIIwXP8HseAMU/ j+35sMSVqwdVJl0CndA33FtGB0n34iBzyjgXuuN/Gpm8yKuCKKNQ4FV3MaBVy+6CbWTe qXqA== X-Gm-Message-State: AIVw112x+wtbWvW5dtjylywXRzAVRMQCDLgR05gUdNmqXN3D3QBfbcXU 3inT0KxpfqFFLQ== X-Received: by 10.98.155.4 with SMTP id r4mr48575298pfd.186.1499745976865; Mon, 10 Jul 2017 21:06:16 -0700 (PDT) Received: from [10.67.68.84] ([192.55.55.41]) by smtp.gmail.com with ESMTPSA id r84sm27206638pfa.57.2017.07.10.21.06.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Jul 2017 21:06:15 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson , wm4 , "Liu, Kaixuan" From: Jun Zhao Message-ID: <09386466-2472-4f2d-27c6-362b61543828@gmail.com> Date: Tue, 11 Jul 2017 12:06:10 +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 V2] examples/vaapi_dec: Add a VA-API 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" V2: re-work with new hw decoding API. From 0e4d230ae4c98949a962c6bbdad31d216b54bb6a Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Tue, 21 Mar 2017 11:04:41 +0800 Subject: [V2] examples/vaapi_dec: Add a VA-API hwaccel decoding example. Add a VA-API hwaccel decoding example. Signed-off-by: Liu, Kaixuan Signed-off-by: Jun Zhao --- doc/examples/vaapi_dec.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 doc/examples/vaapi_dec.c diff --git a/doc/examples/vaapi_dec.c b/doc/examples/vaapi_dec.c new file mode 100644 index 0000000000..01320a3b71 --- /dev/null +++ b/doc/examples/vaapi_dec.c @@ -0,0 +1,266 @@ +/* + * Video 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 + * Intel VAAPI-accelerated decoding example. + * + * @example vaapi_dec.c + * This example shows how to do VAAPI-accelerated decoding with output + * frames from the VAAPI video surfaces. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static AVBufferRef *hw_device_ctx = NULL; +FILE *output_file = NULL; + +int decoder_init_vaapi(AVCodecContext *ctx) +{ + int err = 0; + const char *dev_name = "/dev/dri/renderD128"; + + if ((err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, + dev_name, NULL, 0)) < 0) { + fprintf(stderr, "Failed to create a VAAPI device.\n"); + return err; + } + ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); + + return err; +} + +static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx, + const enum AVPixelFormat *pix_fmts) +{ + const enum AVPixelFormat *p; + + for (p = pix_fmts; *p != -1; p++) { + if (*p == AV_PIX_FMT_VAAPI) + return *p; + } + + return AV_PIX_FMT_NONE; +} + +int retrieve_data(AVFrame *input) +{ + AVFrame *output = NULL; + int err; + av_assert0(input->format == AV_PIX_FMT_VAAPI); + + if (!(output = av_frame_alloc())) + return AVERROR(ENOMEM); + /* default output nv12 */ + output->format = AV_PIX_FMT_NV12; + if ((err = av_hwframe_transfer_data(output, input, 0)) < 0) { + fprintf(stderr, "Failed to transfer data to output frame: %d.\n", err); + goto fail; + } + + if ((err = av_frame_copy_props(output, input)) < 0) { + av_frame_unref(output); + goto fail; + } + + av_frame_unref(input); + av_frame_move_ref(input, output); + av_frame_free(&output); + return 0; + +fail: + av_frame_free(&output); + return err; +} + +int write_frame(AVFrame *frame) +{ + int idx, size; + int width = frame->width; + int height = frame->height; + + av_assert0(frame && frame->data[0] && output_file); + + for (idx = 0; idx < height; idx++) { + if ((size = fwrite(frame->data[0] + idx*frame->linesize[0], + 1, width, output_file)) < 0) { + fprintf(stderr, "Dump Y to file error.\n"); + return -1; + } + } + + height >>= 1; + for (idx = 0; idx < height; idx++) { + if ((size = fwrite(frame->data[1] + idx*frame->linesize[1], + 1, width, output_file)) < 0) { + fprintf(stderr, "Dump UV to file error.\n"); + return -1; + } + } + + return 0; +} + +int decode_write(AVCodecContext *avctx, AVPacket packet, int flush) +{ + AVFrame *frame = NULL; + int ret = 0; + + ret = avcodec_send_packet(avctx, &packet); + if (ret < 0 && ret != AVERROR_EOF) + return 0; + + if (!(frame = av_frame_alloc())) + return AVERROR(ENOMEM); + + ret = avcodec_receive_frame(avctx, frame); + if (ret >= 0) { + /* retrieve data from GPU to CPU */ + if ((ret = retrieve_data(frame)) < 0) + goto fail; + + if ((ret = write_frame(frame)) < 0) + goto fail; + } else if (flush == 0) { + ret = 0; + } + +fail: + av_frame_free(&frame); + return ret; +} + +/* flush the decoder */ +int flush(AVCodecContext *avctx) +{ + AVPacket packet; + int ret = 0; + + av_init_packet(&packet); + packet.data = NULL; + packet.size = 0; + + while (1) { + if ((ret = decode_write(avctx, packet, 1)) < 0) + break; + } + av_packet_unref(&packet); + + return 0; +} + +int main(int argc, char *argv[]) +{ + AVFormatContext *input_ctx = NULL; + int i, video_stream, ret; + AVCodecContext *decoder_ctx = NULL; + AVCodec *decoder = NULL; + AVPacket packet; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + av_register_all(); + + /* open the input file */ + if (avformat_open_input(&input_ctx, argv[1], 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, "Couldn't find input stream information.\n"); + goto fail; + } + + /* find the video stream information */ + video_stream = -1; + for (i = 0; i < input_ctx->nb_streams; i++) { + AVStream *st = input_ctx->streams[i]; + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + video_stream = i; /* NOTE: just handle the first video stream */ + break; + } + } + + if (video_stream == -1) { + fprintf(stderr, "No find a video stream in the input file.\n"); + goto fail; + } + + /* initialize the video decoder */ + if (!(decoder = avcodec_find_decoder(input_ctx->streams[video_stream]->codecpar->codec_id))) { + fprintf(stderr, "Could not find input codec\n"); + goto fail; + } + + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) + return AVERROR(ENOMEM); + + if (avcodec_parameters_to_context(decoder_ctx, + input_ctx->streams[video_stream]->codecpar) < 0) + goto fail; + + decoder_ctx->get_format = get_vaapi_format; + av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0); + + if (decoder_init_vaapi(decoder_ctx) < 0) + goto fail; + + if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { + fprintf(stderr, "Failed to open codec for stream #%u\n", i); + goto fail; + } + + /* open the output file to dump YUV */ + output_file = fopen(argv[2], "w+"); + + /* actual decoding and dump the YUV data */ + while (av_read_frame(input_ctx, &packet) >= 0) { + if (video_stream == packet.stream_index) + ret = decode_write(decoder_ctx, packet, 0); + av_packet_unref(&packet); + + if (ret < 0) + break; + } + + /* flush the decoder */ + flush(decoder_ctx); + +fail: + if (output_file) + fclose(output_file); + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + av_buffer_unref(&hw_device_ctx); + + return ret; +}