From patchwork Tue Nov 7 03:15:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 5929 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.90 with SMTP id m26csp3652996jah; Mon, 6 Nov 2017 19:42:58 -0800 (PST) X-Google-Smtp-Source: ABhQp+QFz6IsYYtLfwJMGF1FI1Lpbcwf7QTPSEusTYgwYWq8iO25By5JMyISEFAmCPh4IfkqVL0B X-Received: by 10.28.225.214 with SMTP id y205mr248501wmg.12.1510026178290; Mon, 06 Nov 2017 19:42:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510026178; cv=none; d=google.com; s=arc-20160816; b=H6Lz/5tVlLiZIJl/y2YtV8lELfP4G8AcXeC1K/aV1rO/Xb8zhS09p8vctVnxy+q4i4 z2QK7efmSGg5y7+3lgsSqoOjGVeECeFgS7edkc5uBN3yQQ0Q0wN2SutsidiwuH3FIqJ0 xOd3I1Ok/ewtbBkgJ5MriclZ4SQd/YMrwKxGYnGueYq0EynWpxMpMt1SqURRs/w0ybfz X6jKCf52iK65bf7fNvUp5Hd03gZwa7dFvE7ogQIK/bNWGqVUbB3Bc1hl5h53v+fPFXZw gguttXThLgMgQV/CfcCLe/6yst6+5WA+Rck9G+5tZhRLplDPlWbxAzg/C6p2BmBkh0tj F1Cw== 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=t1FgkVuRLJojQh1NMZaxAyX8o22s+mFj5xEQhnjDJR8=; b=Bm7e2arfvks8tc0DWvs4B6bDC1c6FUT3p5ztQorIW9mdWWCzqVUs+rTKTZfmwcKNgQ dpXhGM+iagt3gSjbYqIR2Cn2B1TrBVM0WGRKPwdFwF7OvtDSzmexDA/jljhlBEyVpWZu indF7l6cbSv4zEchSp886QADyEL6PAI7oMc+zhogjcsIqYGg7ByUv5vyLlJ3fd4U6qzx VfaW8qoNe7mnklCEgM1bnzWcGW2uKcqRoa73oVl7++YWnip4oa1czRakBD9mRJqJg3+C HKQDlk9dtbEeQHG5XH7diGaQ3dBtaG31hrpvtbFqm+Z4Ma26uCWxa1C4cILCPQ27QRMB rJyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=FnsjkzZx; 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 f199si385148wmd.2.2017.11.06.19.42.57; Mon, 06 Nov 2017 19:42:58 -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=FnsjkzZx; 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 AAFCC689CCD; Tue, 7 Nov 2017 05:42:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f66.google.com (mail-pg0-f66.google.com [74.125.83.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 52C486883C3 for ; Tue, 7 Nov 2017 05:42:36 +0200 (EET) Received: by mail-pg0-f66.google.com with SMTP id 15so9933388pgc.12 for ; Mon, 06 Nov 2017 19:42:48 -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=TL0B8ITCphMFAawUMfwpkBkdzLmAE7XMZ7uhJW5bmHA=; b=FnsjkzZxc0KArasZeanhr1UgRHsO63edP6TME3P/6zLuQaH/1TdRr6HqjCyFRnALHk DNtRFjeP76HyZX1nd/dt3iNAJYwYu4oNOBjslCUWM+3w9DfKNXJwGISe/n+0imkNUUz+ jSDLHx2INsWnXaCBpwrY7m1fEMmesayL92EmO4JdloS9pSGXGZyVgFMVyILMv9Pkr5Kc t5qGtLTYBCewZdL+21ILCUPw/ebaUU3JalgDTtIUpn1uSvGDZ6byMcU+UEq7FcIXsPqh b2gizgJiStPBanPe3RNPXqVlyRA35NAdn3ydRYVuKDx6CILyRzpSUXQ0C5DLxvOzRxlt RUWw== 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=TL0B8ITCphMFAawUMfwpkBkdzLmAE7XMZ7uhJW5bmHA=; b=UsdRYoo/cyFP82pqb48aBt0nZ4PlGXFJH86l9wEJzMAcJr9g1BTDQ2/hS1Jxb3+whc bky5kTaIHPXrimmbwa9GOEkUdnfykYK1Ygyz8ahIYh7wcpYzARZYlYVTcLUMLjIWcYK2 iLJG9NZgfIOCpa3nrmrWyrSj2qM5/w8qxsA2w/3TbQSMJMb1jmenkT9wnhMqt/k+YVS2 93pIou0KNIsvghBAz5roHjTjFa2mVNflLRoPrPP0aVUxKZX3RWDTOlg9R8T1gB9sdryr q+8RAAkQc1MRjWQ4SOV6hjDPdYjmfia+pHo5xSwcqJ8rXWzR3PsuOcJp6ep/dZAXK8wT 1Q5g== X-Gm-Message-State: AJaThX7pR4FhEtGgHPIuRR7Cteat3cXhd8MZbHZcvF9oHYu72GlNYvL1 xPGFHaf5heV0MM7uyosmgf8= X-Received: by 10.99.111.72 with SMTP id k69mr872222pgc.21.1510024541664; Mon, 06 Nov 2017 19:15:41 -0800 (PST) Received: from [10.67.68.22] ([192.55.54.42]) by smtp.gmail.com with ESMTPSA id f4sm252328pgs.30.2017.11.06.19.15.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Nov 2017 19:15:40 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson , Michael Niedermayer From: Jun Zhao Message-ID: <1846846f-57e2-0654-9e76-b757affc8c70@gmail.com> Date: Tue, 7 Nov 2017 11:15:37 +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 V2] 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" V2: Fix the build error (avfiltergraph.h have been remove with merge 99ba85a0). From 48fa48129de4f81812c2c11faf30fff7312640d4 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 6 Nov 2017 14:45:27 +0800 Subject: [PATCH V2] 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 | 308 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 doc/examples/vaapi_encode.c diff --git a/configure b/configure index 83b4cc9364..a4d39712c3 100755 --- a/configure +++ b/configure @@ -1524,6 +1524,7 @@ EXAMPLE_LIST=" scaling_video_example transcode_aac_example transcoding_example + vaapi_encode_example " EXTERNAL_AUTODETECT_LIBRARY_LIST=" @@ -3294,6 +3295,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..462ae47b5a --- /dev/null +++ b/doc/examples/vaapi_encode.c @@ -0,0 +1,308 @@ +/* + * 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_enc 1920 1080 input.yuv output.h264 + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +typedef struct FilterContext { + AVFilterContext *buffersink_ctx; + AVFilterContext *buffersrc_ctx; + AVFilterGraph *filter_graph; +} FilterContext; + +static int width, height; +static AVBufferRef *hw_device_ctx = NULL; + +static int init_filter(FilterContext *filter_ctx, char *args, AVBufferRef *hw_device_ctx) +{ + char filter_spec[] = "format=nv12,hwupload"; + int ret = 0, i = 0; + const AVFilter *buffersrc, *buffersink; + AVFilterContext *buffersrc_ctx, *buffersink_ctx; + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + AVFilterGraph *filter_graph = avfilter_graph_alloc(); + + buffersrc = avfilter_get_by_name("buffer"); + buffersink = avfilter_get_by_name("buffersink"); + if (!buffersrc || !buffersink) { + av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n"); + ret = AVERROR_UNKNOWN; + goto fail; + } + + ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", + args, NULL, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source. Error code:%s\n", av_err2str(ret)); + goto fail; + } + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, NULL, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink. Error code:%s\n", av_err2str(ret)); + goto fail; + } + + outputs->name = av_strdup("in"); + outputs->filter_ctx = buffersrc_ctx; + outputs->pad_idx = 0; + outputs->next = NULL; + inputs->name = av_strdup("out"); + inputs->filter_ctx = buffersink_ctx; + inputs->pad_idx = 0; + inputs->next = NULL; + if (!outputs->name || !inputs->name) { + ret = AVERROR(ENOMEM); + goto fail; + } + + if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec, + &inputs, &outputs, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error code: %s\n", av_err2str(ret)); + goto fail; + } + + if (hw_device_ctx) { + for (i = 0; i < filter_graph->nb_filters; i++) { + filter_graph->filters[i]->hw_device_ctx = av_buffer_ref(hw_device_ctx); + } + } + + if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Fail to config filter graph. Error code: %s\n", av_err2str(ret)); + goto fail; + } + + filter_ctx->buffersrc_ctx = buffersrc_ctx; + filter_ctx->buffersink_ctx = buffersink_ctx; + filter_ctx->filter_graph = filter_graph; + +fail: + avfilter_inout_free(&inputs); + avfilter_inout_free(&outputs); + return ret; +} + +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); + } + +end: + ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1); + return ret; +} + +int main(int argc, char *argv[]) +{ + int ret, size; + FILE *fin = NULL, *fout = NULL; + AVFrame *sw_frame = NULL, *hw_frame = NULL; + AVCodecContext *avctx = NULL; + FilterContext *filter_ctx = NULL; + AVCodec *codec = NULL; + const char *enc_name = "h264_vaapi"; + char args[512]; + + if (argc < 4) { + 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)); + ret = -1; + goto close; + } + if (!(fout = fopen(argv[4], "w+b"))) { + fprintf(stderr, "Fail to open output file : %s\n", strerror(errno)); + ret = -1; + goto close; + } + + av_register_all(); + avfilter_register_all(); + + ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, + NULL, NULL, 0); + if (ret < 0) { + fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(ret)); + goto close; + } + + if (!(codec = avcodec_find_encoder_by_name(enc_name))) { + fprintf(stderr, "Could not find encoder.\n"); + ret = -1; + goto close; + } + + if (!(avctx = avcodec_alloc_context3(codec))) { + ret = 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; + + /* create filters and binding HWDevice */ + snprintf(args, sizeof(args), + "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:frame_rate=%d/%d", + avctx->width, avctx->height, AV_PIX_FMT_NV12, + avctx->time_base.num, avctx->time_base.den, + avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, + avctx->framerate.num, avctx->framerate.den); + + if (!(filter_ctx = av_malloc(sizeof(*filter_ctx)))) { + ret = AVERROR(ENOMEM); + goto close; + } + + if ((ret = init_filter(filter_ctx, args, hw_device_ctx)) < 0) { + fprintf(stderr, "Failed to initialize the filtering context.\n"); + goto close; + } + + avctx->hw_frames_ctx = av_buffer_ref(av_buffersink_get_hw_frames_ctx + (filter_ctx->buffersink_ctx)); + if (!avctx->hw_frames_ctx) { + ret = AVERROR(ENOMEM); + goto close; + } + + if ((ret = avcodec_open2(avctx, codec, NULL)) < 0) { + fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(ret)); + goto close; + } + + while (1) { + if (!(sw_frame = av_frame_alloc())) { + ret = AVERROR(ENOMEM); + goto close; + } + sw_frame->width = width; + sw_frame->height = height; + sw_frame->format = AV_PIX_FMT_NV12; + if ((ret = av_frame_get_buffer(sw_frame, 32)) < 0) + goto close; + if ((ret = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0) + break; + if ((ret = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0) + break; + /* push the sw frame into the filtergraph */ + ret = av_buffersrc_add_frame_flags(filter_ctx->buffersrc_ctx, sw_frame, 0); + if (ret < 0) { + fprintf(stderr, "Error while feeding the filtergraph. Error code: %s\n", av_err2str(ret)); + goto close; + } + /* pull hw frames from the filtergraph */ + while (1) { + if (!(hw_frame = av_frame_alloc())) { + ret = AVERROR(ENOMEM); + goto close; + } + if ((ret = (av_buffersink_get_frame(filter_ctx->buffersink_ctx, hw_frame))) < 0) { + /* if no more frames for output - returns AVERROR(EAGAIN) + * if flushed and no more frames for output - returns AVERROR_EOF + * rewrite retcode to 0 to show it as normal procedure completion + */ + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + ret = 0; + av_frame_free(&hw_frame); + break; + } + + if ((ret = (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 encode */ + ret = encode_write(avctx, NULL, fout); + +close: + if (fin) + fclose(fin); + if (fout) + fclose(fout); + if (sw_frame) + av_frame_free(&sw_frame); + if (hw_frame) + av_frame_free(&hw_frame); + if (avctx) + avcodec_free_context(&avctx); + if (filter_ctx) { + avfilter_free(filter_ctx->buffersrc_ctx); + avfilter_free(filter_ctx->buffersink_ctx); + avfilter_graph_free(&(filter_ctx->filter_graph)); + av_free(filter_ctx); + } + if (hw_device_ctx) + av_buffer_unref(&hw_device_ctx); + + return ret; +}