From patchwork Fri Jul 28 06:01: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: 4480 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp2894809vsb; Thu, 27 Jul 2017 23:08:07 -0700 (PDT) X-Received: by 10.223.150.101 with SMTP id c34mr4874738wra.266.1501222087345; Thu, 27 Jul 2017 23:08:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501222087; cv=none; d=google.com; s=arc-20160816; b=hhWLP/8wPejUnDBaQixbVlwasG+BeDAfkhjdpbSpNf1l/qJTtnI4JOrjilStb4Mvpa FooyJ2Gv7R4bkSflUVzYj5LCaKMBhycpNpn6Zh6Oa+rugAoRATcZhNoKge2jRY9D+klr cyNOBr9KPMHjhIgmKw9AJYpxYRPLGCS76VLsI1IXOaWL/uY+aKV5wFRXahfNO/0bbatn rEzuo86aDPS3S/k1tawdJzbTLFUbhPnhlMbeIoTAluxkntyBJi+MibjlHs+kQrl7hI+y /VqgeOBQAKd4boG5nkCEC8LE/AOq7yP/QXjgXDd1XiP3oRoQG0vt72fPi0/4sZSRdLpf K7UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-disposition:content-language :mime-version:user-agent:date:message-id:from:to:dkim-signature :delivered-to:arc-authentication-results; bh=E1eRzld44bXWMlVlkY7rAXxZ0CGOnBOGrKtJFHR8hyA=; b=MI6kJ6bO7AFdPBjKslN0u+d6xZCEqubKWynMkteUqV9euJHS5ZVLzGNe42cEwCHBxL BzHl2fLTIGTy154m0yRw3VgIVQpfYL96NNL2r0YLAHMhh7d9wcL8xkqUBoHLafmABck0 r+cmP6LFCn7m6gVOo1HrmvFzSzkkKg4YggU7eui9T3HwOyiwLgs9VQN27TDmo9DM4snJ LAGwZL7Kf1OnLubKmxMq/1NFEO02GKXLcug3mAHcp3KAJ/q8OxxA5RS1DPg5J1Ht+3GZ 3PExwnpV/P4vAnQaZ1tdq/ZibV/N0lUNrOyTU1V/Fn+f7TZOYT5oK1nyuA7e5RKU31Kk /FlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=tw2SJl5u; 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 c72si2944060wmh.244.2017.07.27.23.07.58; Thu, 27 Jul 2017 23:08:07 -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 header.b=tw2SJl5u; 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 57BC36898D1; Fri, 28 Jul 2017 09:07:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f194.google.com (mail-pf0-f194.google.com [209.85.192.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DE44A68066B for ; Fri, 28 Jul 2017 09:07:46 +0300 (EEST) Received: by mail-pf0-f194.google.com with SMTP id y25so12540395pfk.4 for ; Thu, 27 Jul 2017 23:07:48 -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 :content-language; bh=se1kAL1FxH+igJEOm1t114VyzpPSP6tyVf2mTcTwdgg=; b=tw2SJl5ux3kC6oKdeDRuuhypsdUpftGo3IOnBPb8WFbkXN1Z9Zu2V7EizZ7l5ZLKJe NDj0gjMCRmh1HB5il+Rf8aT5qrSkwbcM3/T3XTLYIRUah/B2+/2+XbX6HBiy90kZI/vj +E8z2rJLYdW+/HGySsylCU0//K7lbfKQtTFtDY4/4RsxkwUsxY9QpL11Hs7iqBmGGWvE D5jQ4uO8gtPYuMXefvHoJk+b6C8c+NB4y6u398mQHMbGXoz0O9jC2HytvRAGja9GWTfx ffoRWkQkoXRkFkpyNDzmQgYOMUDMUR8EwyjZbHeTU7ToOX7NDFzXlrNaA+/1ORBaQI+n URVQ== 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=se1kAL1FxH+igJEOm1t114VyzpPSP6tyVf2mTcTwdgg=; b=aMDTC59njJIADNAOwWNTkJq/MUhU3yBh2tF2SychTV9mDsmj/KbP5Uf2itIDIrqHMx 2PFKLf0pkQOujhtV7peawguS15mm5LzLhft/yodoUY9mkRvny7mkGe/V7VjDpLn5cUZY QXRFotCJS0j8szcF4lErsCWige+pHr/YJHY48NylAFJhjpKn3D1Bjn1sAJRj8IrienAH KAZzxHjHN8IBDB67GXSOJUqAn/NYgDyWSAaNNBXMr/qyEco/HR9PkDXyOBbLjjS/TC7g VX47I5BfC4x6dOhbRij02GD/++aSD89RE1uSyBdyFBV0UTrxdDVHmeBy6n6KiT6erNMD h4Ow== X-Gm-Message-State: AIVw1118qs9WWWQ0KJc9qB8AlLKC6o5/DOE4RQ2UhI411cUGLXzLqPUm QfACR9naf69kQA== X-Received: by 10.98.93.87 with SMTP id r84mr6356179pfb.292.1501221701898; Thu, 27 Jul 2017 23:01:41 -0700 (PDT) Received: from [10.67.68.30] (fmdmzpr04-ext.fm.intel.com. [192.55.54.39]) by smtp.gmail.com with ESMTPSA id 79sm39145288pfn.51.2017.07.27.23.01.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Jul 2017 23:01:40 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson , wm4 , "Liu, Kaixuan" From: Jun Zhao Message-ID: <246b70d9-9c44-557b-4f9c-80a60894b316@gmail.com> Date: Fri, 28 Jul 2017 14:01:37 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 Content-Language: en-US Content-Disposition: attachment; filename*0="0001-examples-vaapi_enc-Add-a-VAAPI-encoding-example.patch" X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] examples/vaapi_enc: Add a VAAPI encoding 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 d5414b451012b3a0169740a26f452785eb44cce5 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Fri, 28 Jul 2017 01:39:27 -0400 Subject: [PATCH] examples/vaapi_enc: Add a VAAPI encoding example. Add a VAAPI encoding example. Use hwupload loading the raw date in HW surface, usage like this: ./vaapi_enc 1920 1080 input.yuv test.h264 Signed-off-by: Liu, Kaixuan Signed-off-by: Jun Zhao --- doc/examples/vaapi_enc.c | 291 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 doc/examples/vaapi_enc.c diff --git a/doc/examples/vaapi_enc.c b/doc/examples/vaapi_enc.c new file mode 100644 index 0000000000..e26db0e343 --- /dev/null +++ b/doc/examples/vaapi_enc.c @@ -0,0 +1,291 @@ +/* + * 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_enc.c + * This example shows how to do VAAPI-accelerated encoding. now only support NV12 + * raw file, usage: vaapi_enc 1920 1080 input.yuv output.h264 + */ + +#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; + 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\n"); + 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.\n"); + 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) + 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) + 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) + goto end; + while (1) { + ret = avcodec_receive_packet(avctx, &enc_pkt); + if (!ret) { + enc_pkt.stream_index = 0; + ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout); + } else { + break; + } + } + +end: + ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1); + return ret; +} + +int main(int argc, char *argv[]) +{ + int ret, size; + FILE *fin, *fout; + AVFrame *sw_frame, *hw_frame; + AVCodecContext *avctx = NULL; + FilterContext *filter_ctx; + AVCodec *codec = NULL; + uint8_t *frame_buf; + const char *input_file, *output_file; + const char *enc_name = "h264_vaapi"; + char args[512]; + int count = 0; + + width = atoi(argv[1]); + height = atoi(argv[2]); + input_file = argv[3]; + output_file = argv[4]; + + size = width * height; + frame_buf = malloc((size * 3) / 2); /* size for nv12 frame */ + fin = fopen(input_file, "r"); + fout = fopen(output_file, "w+b"); + if (!fin || !fout) { + fprintf(stderr, "Fail to open input or output file.\n"); + 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.\n"); + 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->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 ((ret = avcodec_open2(avctx, codec, NULL)) < 0) + fprintf(stderr, "Cannot open video encoder codec.\n"); + + while (fread(frame_buf, (size * 3)/2, 1, fin) > 0) { + if (!(sw_frame = av_frame_alloc())) { + ret = AVERROR(ENOMEM); + goto close; + } + sw_frame->data[0] = frame_buf; + sw_frame->data[1] = frame_buf + size; + sw_frame->linesize[0] = width; + sw_frame->linesize[1] = width; + sw_frame->width = width; + sw_frame->height = height; + sw_frame->format = AV_PIX_FMT_NV12; + /* 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.\n"); + 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; + } + count++; + if (count % avctx->gop_size == 1) { + hw_frame->pict_type = AV_PICTURE_TYPE_I; + } else { + hw_frame->pict_type = AV_PICTURE_TYPE_B; + } + + 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: + fclose(fin); + fclose(fout); + av_frame_free(&sw_frame); + av_frame_free(&hw_frame); + 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); + } + av_buffer_unref(&hw_device_ctx); + free(frame_buf); + + return ret; +}