From patchwork Thu Jan 11 07:07:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 7254 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp269176jaa; Wed, 10 Jan 2018 23:07:40 -0800 (PST) X-Google-Smtp-Source: ACJfBoukqyb8tGcZyn2A0yNTrSnVBUnz7TbjmVn+5ed1TnUDNyO9N2lg2S3CcmQuuZmJTx9ZfX1M X-Received: by 10.223.146.198 with SMTP id 64mr14666874wrn.41.1515654460130; Wed, 10 Jan 2018 23:07:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515654460; cv=none; d=google.com; s=arc-20160816; b=Hbyn8gNMuA5ZQyuaDWUYssvH+O4AMAOD41eJzVwduEEth6MVPHCE6MmDa6XprgNuhv MwopmQr0wpALsgJD8ZuLsPNcDRH4U+fqtE4rggkejpnLTtPEz89xYUaJ/UT92lk5zp2N wkepyLWY1javNU0k0apknQFSmSc1kmEKwLvFfrG/7kQoIDHSM4jWZiyRrYTfov1keocI oOGXPNNpoEh+EA8cHsYBOSKBC+S25A+lZSAD9taI6UppzmIN8yI9gPQu0PDt49Kzt6hh KUo2Bxg5JzIDIm+fafH+MRoXdAalIhisyqR2Knr55UBT8hQmFJmSjwLdwGAUKdOMGmsb qDCA== 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=GF+7azS1qev1XbVACMsvCuzAHf+3dfkZnAI5TlGEP4E=; b=NYV1KPW10a08Gr/wf8BsPE3KuTWLjOwQtoSSi4ufQWIE/ivuvrVOzObqSvv3IEsOvD lO6QQVmExVlxchvv2l0t/pV1uQc1A0k3pwedIgjBWkkAzR6NxI+h7z81HTyxxompBex7 NYLvfItxdAfT5giTcKy0AMvcm1w/sNmwiYm04jGPKhSOIPZvCdg7l32ygbT4D9vmGzPW SVmjvkdMfzrcRYK67X/xt24vLw7WwVC1R3Sx2UZkYSciHpptM2ODd6LHn37d+PQWfyTB eCVw7YWnG/KsjyazOiXe5WIAIyIC+baALQfzIWp81qNYcWSirJA9zQBrdOKlqzaMlctW qFLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=fNs8V4qL; 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 d7si14089086wrh.157.2018.01.10.23.07.39; Wed, 10 Jan 2018 23:07:40 -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=fNs8V4qL; 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 28665689F6C; Thu, 11 Jan 2018 09:07:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f182.google.com (mail-pf0-f182.google.com [209.85.192.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D060E68994B for ; Thu, 11 Jan 2018 09:07:29 +0200 (EET) Received: by mail-pf0-f182.google.com with SMTP id m26so940841pfj.11 for ; Wed, 10 Jan 2018 23:07:31 -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=VDQ7GJXqsRk+rS8p7h3PGtfroUdrO3QZqnC9HLRSQos=; b=fNs8V4qLXMx1bxCipUxqXmkUKSeKzXkLKaB9YT0blEC8ksNGzerfycsEhSkpl5Kmrp +T4gpQ+dgOUx+LoaLZ5EPocf/dVq+p2I6ETeKgqRSlJgKYdbHKfrGeKofEhOyB1quZqc h20vEPgHqXtG3nVPtG2jsmOW33c+31lAEKCwgG1rSiqoTOT2kIi2dI344B95uOa/k/Xu XCjfVL1u6nDHmwRGUadGf2BRrG6odzpIazssvS+YrMeKT5apFLg+k5be9+p3AXxmgRoq QsHfXf2jNaRsU1/mfcifwUgPjdg/OJ+11ZLcDdlLRURcPGelE9wU84dCGRchFney/Csa ojWQ== 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=VDQ7GJXqsRk+rS8p7h3PGtfroUdrO3QZqnC9HLRSQos=; b=bGz0nUDAob1GR9JDqKlrBSBTej8XJmDjMgytkJxxf93D57xraect5ClfkhB6+pXzrr cnvzhs+JUUdtAkOXLvx8GO93idvlj04X70S9efrWbkPDXyoM9+DoJparfqQTMtXo8zXY hHpG7IH340Kuql1NUIuqHsrPwLUNVg4i73E3+wxsGF4XRQkBRfgh7WAjb9Z3pO2LOJyG 19tVxCBmQoNeJoNQWxW0RV3VqSi3757SevoeS8k+Pb0pXJKYzsLTPnKVzZs5wgDAcvPN 9JmyEFOPd9/xIriImCQOzSAuqXnJqPDYUQJx3ScSZ+bky9nsh4zBYdZYXbz3m3CDqmm4 GQqw== X-Gm-Message-State: AKGB3mKJwxGqZXUcpk+9bheleoNTghwpdA8J0lWEh7+sS/EDGwL7oxym IchQE7VKsmajg7lsy+lg2Bs= X-Received: by 10.99.94.69 with SMTP id s66mr17201206pgb.145.1515654450101; Wed, 10 Jan 2018 23:07:30 -0800 (PST) Received: from [10.67.68.23] (fmdmzpr04-ext.fm.intel.com. [192.55.54.39]) by smtp.gmail.com with ESMTPSA id j6sm38082657pfg.85.2018.01.10.23.07.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jan 2018 23:07:29 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <2fd1530e-43db-37f5-562d-18b3bcb420df@gmail.com> Date: Thu, 11 Jan 2018 15:07:27 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH 2/2] example/vaapi_transcode: Add a VA-API transcode 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 b76a2f790814df326d7d93c54b14b3c8e74ab759 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 11 Jan 2018 15:00:30 +0800 Subject: [PATCH 2/2] example/vaapi_transcode: Add a VA-API transcode example. Add VA-API transcoding example, useage like: ./vaapi_transcode input_stream h264_vaapi output_stream Signed-off-by: Jun Zhao Signed-off-by: Liu, Kaixuan --- configure | 2 + doc/examples/Makefile | 1 + doc/examples/vaapi_transcode.c | 256 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 doc/examples/vaapi_transcode.c diff --git a/configure b/configure index b8985ba542..6d08c0fbbb 100755 --- a/configure +++ b/configure @@ -1526,6 +1526,7 @@ EXAMPLE_LIST=" transcode_aac_example transcoding_example vaapi_encode_example + vaapi_transcode_example " EXTERNAL_AUTODETECT_LIBRARY_LIST=" @@ -3319,6 +3320,7 @@ scaling_video_example_deps="avutil swscale" transcode_aac_example_deps="avcodec avformat swresample" transcoding_example_deps="avfilter avcodec avformat avutil" vaapi_encode_example_deps="avcodec avutil" +vaapi_transcode_example_deps="avcodec avformat avutil" # EXTRALIBS_LIST cpu_init_extralibs="pthreads_extralibs" diff --git a/doc/examples/Makefile b/doc/examples/Makefile index da5af36532..928ff306b3 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -20,6 +20,7 @@ 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-$(CONFIG_VAAPI_TRANSCODE_EXAMPLE) += vaapi_transcode EXAMPLES := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)$(EXESUF)) EXAMPLES_G := $(EXAMPLES-yes:%=doc/examples/%$(PROGSSUF)_g$(EXESUF)) diff --git a/doc/examples/vaapi_transcode.c b/doc/examples/vaapi_transcode.c new file mode 100644 index 0000000000..b92a387f2f --- /dev/null +++ b/doc/examples/vaapi_transcode.c @@ -0,0 +1,256 @@ +/* + * Video Acceleration API (video transcoding) transcode 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 transcoding example. + * + * @example vaapi_transcode.c + * This example shows how to do VAAPI-accelerated transcoding. + * Usage: vaapi_transcode input_stream h264_vaapi output_stream + */ + +#include +#include +#include +#include +#include +#include + +static AVFormatContext *ifmt_ctx = NULL; +static AVBufferRef *hw_device_ctx = NULL; +static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL; +static int video_stream = -1; +static FILE *fout; +static int initialized = 0; + +static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx, + const enum AVPixelFormat *pix_fmts) +{ + const enum AVPixelFormat *p; + + for (p = pix_fmts; *p != -1; p++) { + if (*p == AV_PIX_FMT_VAAPI) + return *p; + } + + fprintf(stderr, "Failed to get vaapi pix format\n"); + return AV_PIX_FMT_NONE; +} + +static int open_input_file(const char *filename) +{ + int ret; + AVCodec *decoder = NULL; + AVStream *video = NULL; + + if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) { + fprintf(stderr, "Cannot open input file '%s', Error code: %s\n", + filename, av_err2str(ret)); + return ret; + } + + if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) { + fprintf(stderr, "Cannot find input stream information. Error code: %s\n", + av_err2str(ret)); + return ret; + } + + ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0); + if (ret < 0) { + fprintf(stderr, "Cannot find a video stream in the input file.\ + Error code: %s\n", av_err2str(ret)); + return ret; + } + video_stream = ret; + + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) + return AVERROR(ENOMEM); + + video = ifmt_ctx->streams[video_stream]; + if ((ret = avcodec_parameters_to_context(decoder_ctx, video->codecpar)) < 0) { + fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n", + av_err2str(ret)); + return ret; + } + + decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); + decoder_ctx->get_format = get_vaapi_format; + + if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) + fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n", + av_err2str(ret)); + + return ret; +} + +static int encode_write(AVFrame *frame) +{ + int ret = 0; + AVPacket enc_pkt; + + av_init_packet(&enc_pkt); + enc_pkt.data = NULL; + enc_pkt.size = 0; + + if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) { + fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret)); + goto end; + } + while (1) { + ret = avcodec_receive_packet(encoder_ctx, &enc_pkt); + if (ret) + break; + + enc_pkt.stream_index = 0; + ret = fwrite(enc_pkt.data, 1, enc_pkt.size, fout); + } + +end: + if (ret == AVERROR_EOF) + return 0; + ret = ((ret == AVERROR(EAGAIN)) ? 0:-1); + return ret; +} + +static int dec_enc(AVPacket *pkt, AVCodec *enc_codec) +{ + AVFrame *frame; + int ret = 0; + + ret = avcodec_send_packet(decoder_ctx, pkt); + if (ret < 0) { + fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret)); + return ret; + } + + while (ret >= 0) { + if (!(frame = av_frame_alloc())) + return AVERROR(ENOMEM); + + ret = avcodec_receive_frame(decoder_ctx, frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + av_frame_free(&frame); + return 0; + } else if (ret < 0) { + fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret)); + goto fail; + } + + if (!initialized) { + /* Here we need to ref hw_frames_ctx of decoder to initialize encoder's codec. + Only after we get a decoded frame, can we obtain its hw_frames_ctx */ + encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx); + if (!encoder_ctx->hw_frames_ctx) { + ret = AVERROR(ENOMEM); + goto fail; + } + if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) { + fprintf(stderr, "Error while open encode codec. Error code: %s\n", + av_err2str(ret)); + goto fail; + } + initialized = 1; + } + + if ((ret = encode_write(frame)) < 0) + fprintf(stderr, "Error during encoding and writing.\n"); + +fail: + av_frame_free(&frame); + if (ret < 0) + return ret; + } + return 0; +} + +int main(int argc, char **argv) { + int ret = 0; + AVPacket dec_pkt; + AVCodec *enc_codec; + + if (argc != 4) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + av_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)); + return -1; + } + + if ((ret = open_input_file(argv[1])) < 0) + goto end; + + if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) { + fprintf(stderr, "Could not find encoder '%s'\n", argv[2]); + ret = -1; + goto end; + } + + if (!(fout = fopen(argv[3], "w+b"))) { + fprintf(stderr, "Failed to open output file: %s\n", strerror(errno)); + ret = -1; + goto end; + } + + if (!(encoder_ctx= avcodec_alloc_context3(enc_codec))) { + ret = AVERROR(ENOMEM); + goto end; + } + + /* set AVCodecContext Parameters */ + encoder_ctx->width = decoder_ctx->width; + encoder_ctx->height = decoder_ctx->height; + encoder_ctx->time_base = (AVRational){1, 25}; + encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI; + + /* read all packets and only transcoding video */ + while (ret >= 0) { + if ((ret = av_read_frame(ifmt_ctx, &dec_pkt)) < 0) + break; + + if (video_stream == dec_pkt.stream_index) + ret = dec_enc(&dec_pkt, enc_codec); + + av_packet_unref(&dec_pkt); + } + + /* flush decoder */ + dec_pkt.data = NULL; + dec_pkt.size = 0; + ret = dec_enc(&dec_pkt, enc_codec); + av_packet_unref(&dec_pkt); + + /* flush encoder */ + ret = encode_write(NULL); + +end: + avformat_close_input(&ifmt_ctx); + if (fout) + fclose(fout); + avcodec_free_context(&decoder_ctx); + avcodec_free_context(&encoder_ctx); + av_buffer_unref(&hw_device_ctx); + return ret; +}