From patchwork Tue Jul 11 04:38:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 4294 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp4482666vsb; Mon, 10 Jul 2017 21:39:22 -0700 (PDT) X-Received: by 10.223.176.133 with SMTP id i5mr9698514wra.53.1499747962231; Mon, 10 Jul 2017 21:39:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499747962; cv=none; d=google.com; s=arc-20160816; b=DCV522S5hMv757ahkM/4CyjuiSr7sZf04pqxRRaukPbEMwcBBkw7FM9X+sTu0YHv3g dq5bMeoO+x8cXR84LZg02hBg5NB9W6yTPI7VIZyVC6h1FBO2j5M1Fu4kSRs3t0eeSMQ1 RGldjvtsASQtdaRDmz0p48BaS4wnhgTkAOfWmVXQNay0n4XmlGg5p8aGmRzvWGd72gR6 qhjlsyE7HqvmU5TuUC0ZaqAgnGoM5iv5KTNYuAotFx7AOiLoOMOj9aJXa6ekNotIOj6/ Qw5q5RIo3RBRSs5rneNyCDRrvYM+Q6zd95tM1FV8ivRj+LYgx5QsArY8OHr6XUs3q0/L BXgw== 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:feedback-id:to:references:message-id :date:in-reply-to:from:mime-version:delivered-to :arc-authentication-results; bh=Df3UcNpNGNmhzyYpcFVuvG7lEnIjAKUv+wblCYhdkQ8=; b=vT+HELv8arWEsfzAzMCrZqCufbgzR8yBqYOlIP6yfpi89NdIAGuY5/T4dnSxBcACel x/MyLna1RqOj4kkVVwzTAdqfpeQKwjAzzj0rNeSMou6feC2drljJNVQJ4vlMXfoqIvW7 1xvgxfoAH9uQ6WGaM9vGayYh5gKjRtpx13mTw/3yhqgByVx+RynV1u23f+/YuA9RasuP Evq93WcEv/DG+iy1+etCOsTiQsVTmSxl7D3soh3G3Z58Iedn/kolYaqsDUtYKlrYyc1Z J/NbOO+B2LMH2s+RF4yfTaVEhx2P4OhVMBsOR51U9o/8cUrZLyOPRqwWNN0Gtfsx0hXp w82Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u74si502632wmf.54.2017.07.10.21.39.21; Mon, 10 Jul 2017 21:39:22 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A2DC3689E56; Tue, 11 Jul 2017 07:39:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbgsg2.qq.com (smtpbgsg2.qq.com [54.254.200.128]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2DEEB689CFA for ; Tue, 11 Jul 2017 07:39:05 +0300 (EEST) X-QQ-mid: bizesmtp14t1499747921tqhvxy2x Received: from [10.10.5.3] (unknown [124.202.244.90]) by esmtp4.qq.com (ESMTP) with id ; Tue, 11 Jul 2017 12:38:39 +0800 (CST) X-QQ-SSF: 01100000004000F0FG50000A0000000 X-QQ-FEAT: NCurqkplqhN27R/sgqS78328R9Jm6GvsSjBxZIcvqS1dHZ+m8X5sHx3Wd/UBk dBYck+Z7FL4Ba3clg1G/27z8QKKfztr/CWGACOQc7ak4vZrB/HpQcbFRNsTH1KItIevCbgS Fmy/X/HAh0qKHASIgdeu/M0z2mO/TIL11fELt/RCk84dlbZQfRYpgBS2eG93hTZ3MDGsntR noB7rSPBtPZvEqd11UpkEgEiLDlMfj9v8pM8dylihLdArV1sGovKKQzARo9KwXNBsKXWl8S tt9IaLFTlzXrbxHsIwh3/Nx+k= X-QQ-GoodBg: 0 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) From: =?utf-8?B?5YiY5q2n?= In-Reply-To: <09386466-2472-4f2d-27c6-362b61543828@gmail.com> Date: Tue, 11 Jul 2017 12:38:42 +0800 Message-Id: References: <09386466-2472-4f2d-27c6-362b61543828@gmail.com> To: and patches development discussions FFmpeg X-Mailer: Apple Mail (2.3273) X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:chinaffmpeg.org:qybgforeign:qybgforeign2 X-QQ-Bgrelay: 1 Subject: Re: [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 Cc: =?utf-8?B?5YiY5q2n?= , wm4 , "Liu, Kaixuan" , Mark Thompson Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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) { What about !flush? or small than 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;