From patchwork Mon Nov 20 01:38:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 6214 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp3333889jah; Sun, 19 Nov 2017 17:39:00 -0800 (PST) X-Google-Smtp-Source: AGs4zMYUGHCOVW/HT0C6agxWfQMDMjxCHL/wK1bJWHnoVN2cuTotsT2CkQm9PQjkNQO95C2nN8uh X-Received: by 10.28.7.133 with SMTP id 127mr3044449wmh.31.1511141940776; Sun, 19 Nov 2017 17:39:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511141940; cv=none; d=google.com; s=arc-20160816; b=dEcv0O+4F6JE4mfHaVSWGjHb0UyL8UQQ1g0YHtgm5qqkgr6Y3Z+5lYNncO/yAXtemI CB2yr278/IqEUDdZ6bbUnRmDKAu032kHsUfqvRoBrXoNn2QS2SXGD5OB/fGNbZnRn6/A YKYUPDHdptFsBe2PmZ/XlZqGgAUbj6zkoAWFH2F7lyjev3VdrO9L8clhcD9y2QkpomZA 3uxszz5F6TzbUS+63UC06jF3mWsRd85d7ooBBHAJgRqZJtA9yPpUwQb5ME/erT9rpJTL llIBOsBZVILuMkxKQh2CZiHao0ukMUkJOzXaT/U1Pff0eVVMd3V9hBlMD1OVpp54VqE2 4++g== 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=J6M6mul6CeWItTWfVNN8mCqPafOmkquhoGFTxf3/VE8=; b=GBOnKWTE3O6LvQIZWWDjO2TV5xPnOT+pByDDnoQgRc5jN/scmNQ0CBnXeP8RzzoHrs tbqLNCA00z42l+t31qXStxzTtlh1M/nipefsVuY0055DZlhpv6o7SMDiqYKay4dYobSc puRsbnliQxiyaabg4f/luCUZbEHRcGRA4MxDdFDMrBmfOuXCk1T8czyKwXF7cUSyoERn uZ3JJ5pBVaBpsejxXvsq5V/EBcHwM13YGUu7pSYBq+28NqW1TZO9Gvp8XVC3ma0jPIGM YI+S4V1uTh6L6ZgsLDSmGNXH4gd0aQ6FyMVWl0pMzF1IXle7z4HqEAd448ux57o69mFT /ZIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Vzu2+st2; 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 l54si7276699wrl.470.2017.11.19.17.39.00; Sun, 19 Nov 2017 17:39:00 -0800 (PST) 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.s=20161025 header.b=Vzu2+st2; 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 1EE7E689925; Mon, 20 Nov 2017 03:38:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f179.google.com (mail-pf0-f179.google.com [209.85.192.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3A648680981 for ; Mon, 20 Nov 2017 03:38:35 +0200 (EET) Received: by mail-pf0-f179.google.com with SMTP id m88so6078855pfi.9 for ; Sun, 19 Nov 2017 17:38:52 -0800 (PST) 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=KDfTPHGKCGvxT2/WAip02bcFB/JM5ZZJV9TOW5NJCY0=; b=Vzu2+st29jnLhMaO+tEgAdkZ9U6KDC6ojCUcRLSVicehhqAgPG3VAtiSTv3W9eJuAu pvxLEM/qK6FfY6JuqUnph4Eqj9y0fdsX7+dVdURr3LX2Kq+0XEkAE0J5EyIYn5dlhDR9 10VgIlOftkduqgQQNVcoY96xAKR3ll5BUfSdYe5v7Msl2SwMq5D9O9Fr7yvN40cxa7av APtNHfzakvMQvAsJWOSKvZR/PIPuVIf5XfsFH6Nto1iqxPNJtdC0gsSy3WAFO1MjOIHD TlMIh/42j1DpspuH0AZpIlkAOrNgVi6i7FziJYdm7gIlu5ecKmZZfm+wRIvn+69S3Ikb gTpg== 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=KDfTPHGKCGvxT2/WAip02bcFB/JM5ZZJV9TOW5NJCY0=; b=K1iy86h2vGhVyz3WBXxnp3EsVkRWaZZI7ShVxEB+mGoNAkH1sRaAIcqe2n079WE0Py rn36VXN5Q19xnKj43J2T/IcwscWlasH0rKcIakUJTZ639e1I9sTUYrIx0+XYU8kSUlRP +oVHfh9Q5ty11PeXxMzuby4Rl8VcZ/7zLqJ+pQoF9cU+BVjHY40djMvDY6g4QiuDahVr Xjl51X8gej5/QazPWUynf3l5369uAdAh0U63LQqxR0ohAiAsGtQgvF0Q8+0VSlCgegE1 dytbO/YoAuvGogu1dzvlRd0gYyXO1bz6gNYFVUa1jjOKggcT6CUc2eXdHZK1fAqeJdtP UmGg== X-Gm-Message-State: AJaThX6FsqI/HQhqXYw+Upm798R8OT6eZeqd9mJ9gu58584hKSAxYZUp pDzqcNALXpFUQcPiol+3Y13c+KlH X-Received: by 10.84.214.2 with SMTP id h2mr1929945pli.142.1511141930928; Sun, 19 Nov 2017 17:38:50 -0800 (PST) Received: from [10.67.68.27] ([192.55.54.40]) by smtp.gmail.com with ESMTPSA id x9sm17782539pfk.120.2017.11.19.17.38.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Nov 2017 17:38:50 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson , kaixuan.liu@intel.com From: Jun Zhao Message-ID: Date: Mon, 20 Nov 2017 09:38:47 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH V3] examples/vaapi_encode: Add a VA-API encode 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" V3: Use av_hwframe_transfer_date load YUV to HW surface From c0edef97f4f123e74e62396b45db01236ae6477d Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 6 Nov 2017 14:45:27 +0800 Subject: [PATCH V3] examples/vaapi_encode: Add a VA-API encode example. add a VA-API encode example, only support NV12 input, usage like: ./vaapi_encode 1920 1080 test.yuv test.h264 Signed-off-by: Jun Zhao Signed-off-by: Liu, Kaixuan --- configure | 2 + doc/examples/Makefile | 1 + doc/examples/vaapi_encode.c | 225 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 doc/examples/vaapi_encode.c diff --git a/configure b/configure index 8262358138..ea029750c7 100755 --- a/configure +++ b/configure @@ -1521,6 +1521,7 @@ EXAMPLE_LIST=" scaling_video_example transcode_aac_example transcoding_example + vaapi_encode_example " EXTERNAL_AUTODETECT_LIBRARY_LIST=" @@ -3308,6 +3309,7 @@ resampling_audio_example_deps="avutil swresample" scaling_video_example_deps="avutil swscale" transcode_aac_example_deps="avcodec avformat swresample" transcoding_example_deps="avfilter avcodec avformat avutil" +vaapi_encode_example_deps="avfilter avcodec avformat avutil" # EXTRALIBS_LIST cpu_init_extralibs="pthreads_extralibs" diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 58afd71b85..da5af36532 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -19,6 +19,7 @@ EXAMPLES-$(CONFIG_RESAMPLING_AUDIO_EXAMPLE) += resampling_audio EXAMPLES-$(CONFIG_SCALING_VIDEO_EXAMPLE) += scaling_video EXAMPLES-$(CONFIG_TRANSCODE_AAC_EXAMPLE) += transcode_aac EXAMPLES-$(CONFIG_TRANSCODING_EXAMPLE) += transcoding +EXAMPLES-$(CONFIG_VAAPI_ENCODE_EXAMPLE) += vaapi_encode EXAMPLES := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)$(EXESUF)) EXAMPLES_G := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)_g$(EXESUF)) diff --git a/doc/examples/vaapi_encode.c b/doc/examples/vaapi_encode.c new file mode 100644 index 0000000000..c4b763773d --- /dev/null +++ b/doc/examples/vaapi_encode.c @@ -0,0 +1,225 @@ +/* + * Video Acceleration API (video encoding) encode 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 encoding example. + * + * @example vaapi_encode.c + * This example shows how to do VAAPI-accelerated encoding. now only support NV12 + * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264 + * + */ + +#include +#include +#include + +#include +#include +#include +#include + +static int width, height; +static AVBufferRef *hw_device_ctx = NULL; + +static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx) +{ + AVBufferRef *hw_frames_ref; + AVHWFramesContext *frames_ctx = NULL; + int err = 0; + + if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) { + fprintf(stderr, "Failed to create VAAPI frame context.\n"); + return -1; + } + frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data); + frames_ctx->format = AV_PIX_FMT_VAAPI; + frames_ctx->sw_format = AV_PIX_FMT_NV12; + frames_ctx->width = width; + frames_ctx->height = height; + frames_ctx->initial_pool_size = 20; + if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) { + fprintf(stderr, "Failed to initialize VAAPI frame context." + "Error code: %s\n",av_err2str(err)); + return err; + } + ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref); + if (!ctx->hw_frames_ctx) + err = AVERROR(ENOMEM); + + av_buffer_unref(&hw_frames_ref); + return err; +} + +static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout) +{ + int ret = 0; + AVPacket enc_pkt; + + av_init_packet(&enc_pkt); + enc_pkt.data = NULL; + enc_pkt.size = 0; + + if ((ret = avcodec_send_frame(avctx, frame)) < 0) { + fprintf(stderr, "Error code: %s\n", av_err2str(ret)); + goto end; + } + while (1) { + ret = avcodec_receive_packet(avctx, &enc_pkt); + if (ret) + break; + + enc_pkt.stream_index = 0; + ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout); + av_packet_unref(&enc_pkt); + } + +end: + ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1); + return ret; +} + +int main(int argc, char *argv[]) +{ + int size, err; + FILE *fin = NULL, *fout = NULL; + AVFrame *sw_frame = NULL, *hw_frame = NULL; + AVCodecContext *avctx = NULL; + AVCodec *codec = NULL; + const char *enc_name = "h264_vaapi"; + + if (argc < 5) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + width = atoi(argv[1]); + height = atoi(argv[2]); + size = width * height; + + if (!(fin = fopen(argv[3], "r"))) { + fprintf(stderr, "Fail to open input file : %s\n", strerror(errno)); + return -1; + } + if (!(fout = fopen(argv[4], "w+b"))) { + fprintf(stderr, "Fail to open output file : %s\n", strerror(errno)); + err = -1; + goto close; + } + + av_register_all(); + + err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, + NULL, NULL, 0); + if (err < 0) { + fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err)); + goto close; + } + + if (!(codec = avcodec_find_encoder_by_name(enc_name))) { + fprintf(stderr, "Could not find encoder.\n"); + err = -1; + goto close; + } + + if (!(avctx = avcodec_alloc_context3(codec))) { + err = AVERROR(ENOMEM); + goto close; + } + + avctx->width = width; + avctx->height = height; + avctx->time_base = (AVRational){1, 25}; + avctx->framerate = (AVRational){25, 1}; + avctx->sample_aspect_ratio = (AVRational){1, 1}; + avctx->pix_fmt = AV_PIX_FMT_VAAPI; + + /* set hw_frames_ctx for encoder's AVCodecContext */ + if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) { + fprintf(stderr, "Failed to set hwframe context.\n"); + goto close; + } + + if ((err = avcodec_open2(avctx, codec, NULL)) < 0) { + fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err)); + goto close; + } + + while (1) { + if (!(sw_frame = av_frame_alloc())) { + err = AVERROR(ENOMEM); + goto close; + } + /* read data into software frame, and transfer them into hw frame */ + sw_frame->width = width; + sw_frame->height = height; + sw_frame->format = AV_PIX_FMT_NV12; + if ((err = av_frame_get_buffer(sw_frame, 32)) < 0) + goto close; + if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0) + break; + if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0) + break; + + if (!(hw_frame = av_frame_alloc())) { + err = AVERROR(ENOMEM); + goto close; + } + if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) { + fprintf(stderr, "Error code: %s.\n", av_err2str(err)); + goto close; + } + if (!hw_frame->hw_frames_ctx) { + err = AVERROR(ENOMEM); + goto close; + } + if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) { + fprintf(stderr, "Error while transferring frame data to surface." + "Error code: %s.\n", av_err2str(err)); + goto close; + } + + if ((err = (encode_write(avctx, hw_frame, fout))) < 0) { + fprintf(stderr, "Failed to encode.\n"); + goto close; + } + av_frame_free(&hw_frame); + av_frame_free(&sw_frame); + } + + /* flush encoder */ + err = encode_write(avctx, NULL, fout); + if (err == AVERROR_EOF) + err = 0; + +close: + if (fin) + fclose(fin); + if (fout) + fclose(fout); + av_frame_free(&sw_frame); + av_frame_free(&hw_frame); + if (avctx) + avcodec_free_context(&avctx); + av_buffer_unref(&hw_device_ctx); + + return err; +}