From patchwork Fri Jan 26 13:11:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 7421 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.156.27 with SMTP id q27csp668156jak; Fri, 26 Jan 2018 05:11:34 -0800 (PST) X-Google-Smtp-Source: AH8x2248fjCux1k019xlJnRLzUFReTCxLlFokh5DatK8oF9Ab9AYmoQ2m5nzp8fqEJbbnQ3LtsjU X-Received: by 10.223.189.12 with SMTP id j12mr11638591wrh.52.1516972294116; Fri, 26 Jan 2018 05:11:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516972294; cv=none; d=google.com; s=arc-20160816; b=EYuQ1hEXb2IN6H16ccpvMzMBI4CCm37AELNoWy0uPKK86wZ+13CzG5rxcWu4dRITWL 1InVomqLP2ZOl2cVPKbuAg0NOqVhZZ0KsqKrkOAfhUPKf4ymWI6YcWMHg+tR/0nJcgUD t+t4PqsAx1hnJQ7l+0we8wXz3j45Ol9S4Y0eQwnqih4DcNHodw9tjA/DRisvIQ9CNmMt RBMtEjHAIRfohB2eSFtgDnHYqex8Vj6p4sb5tUT/pphGa72/dj2IQLTyfK5rpyCkbYRt UQJXN5x+Lcx+9kd1Ni7mFZtwHOaJrA/tF7tsRB4ypw+QFCZDPnQ89qGP1PyWtVfeQxEP JkXw== 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=Bn3kq6Iz7nQyPPHDRzeDdRsJs0NsqoEfl/P+3Roas0w=; b=uqjM+om1UpzrMozllZf5ySHWnpJ+5kk8xm5SwAZ3FO4DkPC10txUb/rPiUfOelEkTQ wa1VrK0U/pgbui9BQ9v6UVw4wIMbE7BPWxrMTISTqCB3JQBP9kDWnF3yM5FUqUzPPger vn35YcKYrs2O00oQYFGb0SA/bNetFgYOkq3mANRfNIBul8z9MjsZbc5r2+TRb8GiqSpZ hiYgAMNdjF129742bNKqeX9ggT0VPTmLPad6CJU1Py6i9iES2jbv0q5YYNGpuoZF9W8P uRZJO4htQua3EHXOK3UQR+niAfYg3rnAHxJntUYPBHLLww7rjZKypIlsRA9QtiOCX4dz 31ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Yp/dHnWw; 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 u2si5642837wru.419.2018.01.26.05.11.32; Fri, 26 Jan 2018 05:11:34 -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=Yp/dHnWw; 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 2E659689A8B; Fri, 26 Jan 2018 15:11:28 +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 DD63B68092E for ; Fri, 26 Jan 2018 15:11:21 +0200 (EET) Received: by mail-pf0-f179.google.com with SMTP id b25so225958pfd.9 for ; Fri, 26 Jan 2018 05:11:25 -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=IG3WtmEMciSz8XGvEKqYjZF4nTfF/zgfQPHMDxW0Iwc=; b=Yp/dHnWweHViYc76H/Kem+MM30heZaRdQP2VAT8hBK+7rFBZc3RGSjLxuwBuEDKC3R p/JFJjmWPWQIeRePlUMBKolIHgJ+cSdquuWqtgBTDbAVd584xNuFZvlBDFaMeMBa+kw/ 6hxaHZC54Scp+To4st+Z9DbuT/RD2gzEGlXSwq91cvPKF3KC4JrnSQvoJl2fnYY1fifi cX9WTbTVWLrwgoyogfAvgymFDC3p2zWOBE3om9dmOWDUL+Hat4Gsuk99WFZRJB1VQrCX e/unMkbQeYqowlaje23LB/vzvHgwuHso4/UphBSA0vGJ1NHXYyrOc7eXI9oIeE2K1mZU ILHA== 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=IG3WtmEMciSz8XGvEKqYjZF4nTfF/zgfQPHMDxW0Iwc=; b=Gf8CmV/8KCTirM4fg4HfuOp6fSWG3fxTGmz8kLYEWu7biXez9pstRYJa6jEn5VkNnz wP1kYV0Aw2TXF7Xvg6wc9RCQtWwgQ14LkJ2u1S9Voa7DKWUGP9/J5Ehenklri+MdEwRd cooKE7mCC2FRASsLOUruLYfGv6/KE6KpjB5DBsVzfP3PYLP1oAP1Fa7RjKzJAiyOYywI ld2uog7jBu6nBO4Uk3wI2V9tuliw/KN+S1NdPlN06EJCMtwH9HK/VFMzFSXlSff/vyeP kHWQ8EZxo8YpI8ddqAjYh+4apKyiRyPMITsrQhNWKQnUoP7yYwgBdDDBPKDS1zYZDWld F9KA== X-Gm-Message-State: AKwxytcq7UTRNVUIu+hC4nJNPYXSb2t/iZjiTVCkGAP915jcsl9+TXvr sqOdAddyMkmWIUtYVJFXAxIAlzHi X-Received: by 10.101.88.130 with SMTP id d2mr15663028pgu.110.1516972283291; Fri, 26 Jan 2018 05:11:23 -0800 (PST) Received: from [10.255.25.84] (fmdmzpr04-ext.fm.intel.com. [192.55.54.39]) by smtp.gmail.com with ESMTPSA id k13sm18676937pfj.38.2018.01.26.05.11.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jan 2018 05:11:22 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <54b29ce3-ca3a-58cf-0c44-adbaf4ef9541@gmail.com> Date: Fri, 26 Jan 2018 21:11:11 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH V2] 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" V2: - deduce output format from file extension, because VP8/VP9 encoder need to a muxer (e,g ivf or/and mp4). From 9828a8889d5279202c2e8ef9429eb06ad869842b Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 11 Jan 2018 15:00:30 +0800 Subject: [PATCH V2] example/vaapi_transcode: Add a VA-API transcode example. Add VA-API transcoding example, useage like: ./vaapi_transcode input_stream h264_vaapi output_stream, and the example can't handle resolution change case. test case like: - ./vaapi_transcode input.mp4 h264_vaapi output_h264.mp4 - ./vaapi_transcode input.mp4 vp8_vaapi output_vp8.ivf Signed-off-by: Jun Zhao Signed-off-by: Liu, Kaixuan --- configure | 2 + doc/examples/Makefile | 1 + doc/examples/vaapi_transcode.c | 301 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 304 insertions(+) create mode 100644 doc/examples/vaapi_transcode.c diff --git a/configure b/configure index fcfa7aa442..92aef31cbe 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=" @@ -3320,6 +3321,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 h264_vaapi_encoder" +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..af1550f5f2 --- /dev/null +++ b/doc/examples/vaapi_transcode.c @@ -0,0 +1,301 @@ +/* + * 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 + * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4 + * - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf + */ + +#include +#include + +#include +#include +#include + +static AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL; +static AVBufferRef *hw_device_ctx = NULL; +static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL; +static int video_stream = -1; +static AVStream *ost; +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 != AV_PIX_FMT_NONE; p++) { + if (*p == AV_PIX_FMT_VAAPI) + return *p; + } + + fprintf(stderr, "Unable to decode this file using VA-API.\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; + av_packet_rescale_ts(&enc_pkt, ifmt_ctx->streams[video_stream]->time_base, + ofmt_ctx->streams[0]->time_base); + ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt); + if (ret < 0) { + fprintf(stderr, "Error during writing data to output file. " + "Error code: %s\n", av_err2str(ret)); + return -1; + } + } + +end: + if (ret == AVERROR_EOF) + return 0; + ret = ((ret == AVERROR(EAGAIN)) ? 0:-1); + return ret; +} + +static int dec_enc(AVPacket *pkt, AVCodec *enc_codec, const char *file) +{ + 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) { + /* 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; + } + /* set AVCodecContext Parameters for encoder, here we keep them stay + * the same as decoder. + * xxx: now the the sample can't handle resolution change case. + */ + encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate); + encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI; + encoder_ctx->width = decoder_ctx->width; + encoder_ctx->height = decoder_ctx->height; + + if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) { + fprintf(stderr, "Failed to open encode codec. Error code: %s\n", + av_err2str(ret)); + goto fail; + } + + if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) { + fprintf(stderr, "Failed to allocate stream for output format.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + + ost->time_base = encoder_ctx->time_base; + ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx); + if (ret < 0) { + fprintf(stderr, "Failed to copy the stream parameters. " + "Error code: %s\n", av_err2str(ret)); + goto fail; + } + + ret = avio_open(&ofmt_ctx->pb, file, AVIO_FLAG_WRITE); + if (ret < 0) { + fprintf(stderr, "Cannot open output file. " + "Error code: %s\n", av_err2str(ret)); + goto fail; + } + + /* write the stream header */ + if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0) { + fprintf(stderr, "Error while writing stream header. " + "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" + "The output format is guessed according to the file extension.\n" + "\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 ((ret = (avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, argv[3]))) < 0) { + fprintf(stderr, "Failed to deduce output format from file extension. Error code: " + "%s\n", av_err2str(ret)); + goto end; + } + + if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) { + ret = AVERROR(ENOMEM); + goto end; + } + + /* 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, argv[3]); + + av_packet_unref(&dec_pkt); + } + + /* flush decoder */ + dec_pkt.data = NULL; + dec_pkt.size = 0; + ret = dec_enc(&dec_pkt, enc_codec, argv[3]); + av_packet_unref(&dec_pkt); + + /* flush encoder */ + ret = encode_write(NULL); + + /* write the trailer for output stream */ + av_write_trailer(ofmt_ctx); + +end: + avformat_close_input(&ifmt_ctx); + avformat_close_input(&ofmt_ctx); + avcodec_free_context(&decoder_ctx); + avcodec_free_context(&encoder_ctx); + av_buffer_unref(&hw_device_ctx); + return ret; +}