From patchwork Tue Mar 21 04:45:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 3041 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.50.79 with SMTP id y76csp1696563vsy; Mon, 20 Mar 2017 21:45:42 -0700 (PDT) X-Received: by 10.223.170.70 with SMTP id q6mr32261421wrd.162.1490071542007; Mon, 20 Mar 2017 21:45:42 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r5si26042123wra.223.2017.03.20.21.45.41; Mon, 20 Mar 2017 21:45:41 -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; 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 62AD3688396; Tue, 21 Mar 2017 06:45:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f41.google.com (mail-pg0-f41.google.com [74.125.83.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A8126882B4 for ; Tue, 21 Mar 2017 06:45:13 +0200 (EET) Received: by mail-pg0-f41.google.com with SMTP id g2so87531806pge.3 for ; Mon, 20 Mar 2017 21:45:32 -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; bh=nH7q5IuJabgLHDtvwaqnLADDZsMARHLo7cGLndnKpV0=; b=lqjLVdpbK9gbseMu4L9w+xcztjlHo0fJQJ/y9lP5MkyX1wHRN08O97cj8v5HGaRsnq s45Co4BUJpnsP3eBJ/3CZi3a10abAJI1fmml9tJBLdYGo2xeRAzRsTNvLgpqeZi+efUR +xxgx32CvAPo8GKcsN4FCrUOb2eACvoIBrToBkTRX9FI8Zi4R3YAa2gAfcWORuDnwRxg 9rKRgp13BPER4P+CI6uV7FOuNrxP7M9ECTsrkLANxE0/bODpiosWmyw2TdixVdY+EOqO Vuab/7Bw1hKQQRCfrbVZU0dpBcXbH/i2/jYZi2twschwm2G2sCHaGiVyu5puvBaKDGgQ Gniw== 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; bh=nH7q5IuJabgLHDtvwaqnLADDZsMARHLo7cGLndnKpV0=; b=BcNDZ0vBwvueM9ATdiT6LYZ0oFTUC9XpSwqP4BmngCM2vTd/vAIARFaPR70TjMZSIg POqN0GoOt/ZUnbyyuubAp8qoMPAKJlx+Zb+AqnWNFaWFohva/8wWvOTyaRTF+gk2l3nB kxc0Pf6QpiyrCo+iTHXixEvxtZPrGkoNucevk36n/V3ysN/ZEOfTY1O10f1RglNn1TPn z3+sUwKAWKX4lqQdhK1sCuuUVlHedwsO6qklGSx/BdWS/ZSBf260ag9Vqz/SQOGbe2vK BwCICFCYN8GXRBdXcaNQ/G3SNBReWEKqdtYuC2DNxFT8cjoBF31TvhFxcp3mW4X7aEXU 8ipw== X-Gm-Message-State: AFeK/H1q00G1cjU35Sr1XmH6rMOqOkSOweFhtR1uwfHtbzLdtwixkNcpuCPhya/JPz3b3A== X-Received: by 10.84.233.199 with SMTP id m7mr31974pln.25.1490071530246; Mon, 20 Mar 2017 21:45:30 -0700 (PDT) Received: from [10.239.204.195] ([134.134.139.78]) by smtp.gmail.com with ESMTPSA id 194sm36062119pfx.134.2017.03.20.21.45.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Mar 2017 21:45:28 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <1dcb2297-f0af-908f-fbac-557d92e64f66@gmail.com> Date: Tue, 21 Mar 2017 12:45:26 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Content-Disposition: attachment; filename*0="0001-examples-vaapi_dec-Add-a-VA-API-hwaccel-decoding-exa.pa"; filename*1="tch" X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] 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" From 6b56c541ed7dd271ad0aa6eb6412a8427f009525 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Tue, 21 Mar 2017 11:04:41 +0800 Subject: [PATCH] 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 | 346 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 346 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 0000000..f975d86 --- /dev/null +++ b/doc/examples/vaapi_dec.c @@ -0,0 +1,346 @@ +/* + * 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 +#include +#include +#include +#include + +#define DEFAULT_SURFACES 20 +static enum AVPixelFormat decode_format = AV_PIX_FMT_NV12; +static enum AVPixelFormat output_format = AV_PIX_FMT_NV12; /* default output format nv12 */ +static AVBufferRef *frames_ref = NULL; +static AVBufferRef *device_ref = NULL; +FILE *output_file = NULL; + +void decoder_uninit_vaapi(AVCodecContext *avctx, AVVAAPIDeviceContext *hwctx) +{ + if (hwctx) { + av_buffer_unref(&frames_ref); + av_buffer_unref(&device_ref); + } + return; +} + +int decoder_init_vaapi(AVCodecContext *ctx) +{ + AVBufferRef *hw_device_ctx = NULL; + AVHWFramesContext *frames; + int err = 0; + char *dev_name = "/dev/dri/renderD128"; + AVBufferRef *device_ref = NULL; + AVHWDeviceContext *device = NULL; + AVVAAPIDeviceContext *hwctx = NULL; + + 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; + } + + device_ref = av_buffer_ref(hw_device_ctx); + device = (AVHWDeviceContext *)device_ref->data; + hwctx = (AVVAAPIDeviceContext *)device->hwctx; + + ctx->pix_fmt = output_format; + if (!(frames_ref = av_hwframe_ctx_alloc(device_ref))) { + fprintf(stderr, "Failed to create VAAPI frame context.\n"); + err = AVERROR(ENOMEM); + goto fail; + } + frames = (AVHWFramesContext *)frames_ref->data; + frames->format = AV_PIX_FMT_VAAPI; + frames->sw_format = decode_format; + frames->width = ctx->coded_width; + frames->height = ctx->coded_height; + frames->initial_pool_size = DEFAULT_SURFACES; + if ((err = av_hwframe_ctx_init(frames_ref)) < 0) { + fprintf(stderr, "Failed to initialize VAAPI frame.\n"); + goto fail; + } + + ctx->hw_frames_ctx = av_buffer_ref(frames_ref); + if (!ctx->hw_frames_ctx) { + err = AVERROR(ENOMEM); + fprintf(stderr, "Failed to ref VAAPI frame.\n"); + goto fail; + } + +fail: + if (err < 0) + decoder_uninit_vaapi(ctx, hwctx); + return err; +} + +static enum AVPixelFormat get_hw_vaapi_format(AVCodecContext *ctx, + const enum AVPixelFormat *pix_fmts) +{ + const enum AVPixelFormat *p; + int ret; + + for (p = pix_fmts; *p != -1; p++) { + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); + if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) + break; + + if (!(strstr(desc->name, "vaapi"))) + continue; + + if ((ret = decoder_init_vaapi(ctx)) < 0) { + fprintf(stderr, "hwaccel for decoder(VAAPI) cannot be initialized.\n"); + return AV_PIX_FMT_NONE; + } + break; + } + return *p; +} + +static int get_hw_vaapi_buffer(AVCodecContext *ctx, AVFrame *frame, int flags) +{ + int err; + if ((err = av_hwframe_get_buffer(frames_ref, frame, 0)) < 0) + av_log(NULL, AV_LOG_ERROR, "Failed to allocate decoder surface.\n"); + + return err; +} + +int retrieve_data(AVFrame *input) +{ + AVFrame *output = 0; + int err; + av_assert0(input->format == AV_PIX_FMT_VAAPI); + + if (output_format == AV_PIX_FMT_VAAPI) + return 0; + + if (!(output = av_frame_alloc())) + return AVERROR(ENOMEM); + output->format = output_format; + 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, got_frame = 0; + + if (!(frame = av_frame_alloc())) + return AVERROR(ENOMEM); + + if ((ret = avcodec_decode_video2(avctx, frame, &got_frame, &packet)) < 0) { + fprintf(stderr, "Error during decoding\n"); + goto fail; + } + + if (got_frame) { + if (AV_PIX_FMT_VAAPI == frame->format) { + /* 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 == 1) { + ret = -1; + } + +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"); + return -1; + } + + /* 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) { + /* NOTE: just handle the first video stream */ + video_stream = i; + break; + } + } + + if (video_stream == -1) { + fprintf(stderr, "No find a video stream in the input file.\n"); + return -1; + } + + /* 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"); + avformat_close_input(&input_ctx); + return -1; + } + + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) + return AVERROR(ENOMEM); + + if (avcodec_parameters_to_context(decoder_ctx, + input_ctx->streams[video_stream]->codecpar) < 0) { + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + return -1; + } + + decoder_ctx->get_format = get_hw_vaapi_format; + decoder_ctx->get_buffer2 = get_hw_vaapi_buffer; + av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0); + + if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { + fprintf(stderr, "Failed to open codec for stream #%u\n", i); + return ret; + } + + /* 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); + + if (output_file) + fclose(output_file); + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + + av_buffer_unref(&frames_ref); + av_buffer_unref(&device_ref); + + return ret; +} +