From patchwork Mon Nov 6 06:56:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 5883 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.90 with SMTP id m26csp2576605jah; Sun, 5 Nov 2017 22:56:31 -0800 (PST) X-Google-Smtp-Source: ABhQp+QBQNmFUWJvDe+8N96pYwv0Nx+te7UosvZWdMXlI72RXK5/XVfErS0NKw2kqaERwnJ3ozA9 X-Received: by 10.28.111.19 with SMTP id k19mr3862636wmc.84.1509951391307; Sun, 05 Nov 2017 22:56:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509951391; cv=none; d=google.com; s=arc-20160816; b=zubVz1mYKBShyr3I+EeAeOf/6nsr7GRPZr9VOSrJaAk1/Wk5CCgko7QFNHfYhr+9kS WL59FQXvOj5LTO02Li013dUTE/oPOMWXx3pM33nTNQoB9UaU9depebMMV/9IQ9hToyzS SJrxUy2tbBklSVnkUnSOldSN+y33KWKB9/MKuZER2C0eYPq/43lXqhkk7+5y0F0cDq0V eKU7mZpwfMg1KonspTmii8ay+lOxCfrrPiCDvnwD2d84B3R83E9ag0GzKbl8foPF9Rg/ l5uf2OS6s3TBvVFY1dvFV3z0yDj9rEJpYL0FReQ9PnDmp3LUueGuM4IdZTKdRZq2FuHA g6dQ== 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=+AjXtTCwSPjVOP/LyOjOmhm27awR4NWSCWzDs3kfufs=; b=Z2CeC/1ti90INpuy/nrvmruB6rk+XXZS1/VPqDi/vipO+0OtrZS1e2tSdzTu1PSahd 9EoE7cWxxfie19x7jdvjZRUlLHqHlIQ/rBOfucWoIES2blANvO2eqANMRd/JonN+8KqB EjaZ31g4KVJY+BKfF8fr1FSNGsjQprTfYrrFMPo6KCS+gOo+Z7HuTbusEvNd9Es28sm0 ieEN4dmYNBeYX4RqdoT8ynA2rt/lIGxiCxjZ13kOp5iYW31fCHVo0DgZHbGRQce+dkux cAxXgnZPP7X0Tnv5XpuSBKDfh9+X9KmCqVWfN91j+LGX1tH2EVPCrD4ow55e4tfcpq+f aq3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=NqpPGU75; 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 i186si6259658wmg.19.2017.11.05.22.56.30; Sun, 05 Nov 2017 22:56:31 -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=NqpPGU75; 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 222CC6882F6; Mon, 6 Nov 2017 08:56:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f180.google.com (mail-pf0-f180.google.com [209.85.192.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C3A406882C2 for ; Mon, 6 Nov 2017 08:56:10 +0200 (EET) Received: by mail-pf0-f180.google.com with SMTP id b79so7066256pfk.5 for ; Sun, 05 Nov 2017 22:56:22 -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=EFITiL0YeeXts1V1N5c2We4KB1WodmyQgDwkofF1x+4=; b=NqpPGU75XLIFt0q/mVBJVtZwD9A5pEBlePOrDUCNmbpoCDPNhE2kSooXzsUnpH7ztX 18UHnv+dHLVkfBEbJ2fDtGSOvJfFWAHZkLhZF8X5djdZ6RRAaACco0v1t77pu9qZ8SIr q1R5f014L2Xa6CebMobVF2YpChheCVFLM0DUIRfpuL877TUpmU4mk1tovvkMGbN7OO2l bV1UtQR2Wz+NlZ8vNlwqE4CsyfzmxKBjtPQzby18wfiky3MbmlIO9cLwP4L9i8GlYpT/ 9DhGqoZ1Kega8kXAre1G5eliTyDRSFb4uqnblyu88cFyWQU+vRz+GXhZRZQWm4N4M0Dj efAg== 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=EFITiL0YeeXts1V1N5c2We4KB1WodmyQgDwkofF1x+4=; b=qWfWHXiVDeQ22DFOKKeU1zMa1qwRk9uur5aNQMi7CJ5tueRUFnF1OvdN6bEP0dhdiy ECvbnWqVQHCHhACraR2Pp1JFOpqb+m+3u5G7pYmb5oZZ5W+O3VhJcQlacY7owLiEms4T sKPwSu4lorWRmcF35tZcdNmBk6oiKfYIBD98k00cAddNhWTgnaaQIwHY5Vq/kmn67Zzr sn4lAs97GP71AWsSsz3Y7c5jWC/uymAEkOJ+UMXmKEn7dQwkE6XzkBidlr1J0zCBhW1g p01wRcK7Q1bEFW1BUJYB1x6XxcWcq7OVV8TexWc+pwKV5BTZOml+ft1CcamQ0W5zF/Tq v9dg== X-Gm-Message-State: AMCzsaXjMOhHKwb8+1tq7t+UMgILGmm6uqz1oRDUCesURiMGNlfWx8kK iugs44Lv3rYWi6dHnAP1L0I= X-Received: by 10.101.68.129 with SMTP id l1mr14293324pgq.4.1509951380778; Sun, 05 Nov 2017 22:56:20 -0800 (PST) Received: from [10.67.68.22] ([192.55.54.40]) by smtp.gmail.com with ESMTPSA id n29sm18227679pgf.44.2017.11.05.22.56.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Nov 2017 22:56:20 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <1eeaeac2-bbd3-b6ca-1a55-6918ca58a588@gmail.com> Date: Mon, 6 Nov 2017 14:56:16 +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] 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" From d16f766363d9ecc240b0f8e025c2a8f91ea4775e Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 6 Nov 2017 14:45:27 +0800 Subject: [PATCH] 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 1b0f064607..2df0205e09 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..02b1ac5a4a --- /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; +}