From patchwork Mon Jan 29 02:59:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 7439 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.156.27 with SMTP id q27csp3452422jak; Sun, 28 Jan 2018 19:00:12 -0800 (PST) X-Google-Smtp-Source: AH8x227BRBHN5D9ZNgnSyAK/sX2xobWdOsxnB3DgcrMr61cfsqyL5vJc6dRAZz64zFw+/BSjy2oJ X-Received: by 10.223.176.109 with SMTP id g42mr5593309wra.129.1517194812559; Sun, 28 Jan 2018 19:00:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517194812; cv=none; d=google.com; s=arc-20160816; b=s7yR1qA2Hk3LUz4t1VIeNcT8o8fhZf6/vMb3kE6RwuwKeX6I302lggNAaqqxSVXs9j ooUJmLxQr57BmRl7saKOSOaWWHdwtRSC39QbC4LyZyulccw/w1pMub34ZlZWYimv659r KnM5kudYvBGnARgxsvG/QKcwseVQT4Pq5IlZeUcyUaINOBbOZwdHzCdcUwqOZCC58SSq ykU2gLLCqKed1zshvrv1zGEPWmNRtAxiZgKGei2OCIvM9JUmvUDhWskSeuyKt+YcNs3u 3URIUSYTpDeLXimgC0aMGf//JUUL//lPfbTgxWK8+0Ji8ZNCxW2QKA5WKuhrL7vKtnsv PPfg== 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=euC/EVRW2dPYoHPFhuOQri/cOHY00C0CAOdgFIx1USA=; b=k91i9+ZfPSyZlfOAQtDqj85Bwk3ynf+8TWA4JoWH4PbXP8vy+LPrNtu9YJaRqhZaOv W+0Y4p7k/Xnyh2vzXlvbtEH4e3GiksY5v64ARlFLcCN7z2f/gAjsE0/jCtJtI2sd5f12 lkHcqxKGcbkWEwo5nH5nGScXQrMoB/Mu0jyyKBAHnErTOM8iwHQt27RRpx/xQXBHezn5 jukN+pkrR3lpiTlmCwJzu8JzaV7OtQpmslcTrNHrGhHxxe/MuwVeP2GENEsjt/Hd5J95 mv/uTfT2WjaUuskbZ+9PH94ls5KXU6yd2tfmeXOkRMWDz5o+ct7cyWmNwJjt+4Lg4433 OpZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=u5BPEPjh; 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 h81si7206417wma.137.2018.01.28.19.00.11; Sun, 28 Jan 2018 19:00:12 -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=u5BPEPjh; 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 2DBB76805C3; Mon, 29 Jan 2018 05:00:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f169.google.com (mail-pf0-f169.google.com [209.85.192.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F10C5680485 for ; Mon, 29 Jan 2018 04:59:59 +0200 (EET) Received: by mail-pf0-f169.google.com with SMTP id t5so3676783pfi.0 for ; Sun, 28 Jan 2018 19:00:04 -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=QtLFxvL+cjgOXQamHXkAmVM3lDamsWb+w+bB0DgMeiQ=; b=u5BPEPjhmJDkJkF5JCI8C3YyrIzoNN00CDTMDgl9zg8klBbo6J/NfJzGu486YeJIDF t0krVGTeyMoq25yCtULcgR1UNPHSHFCrHGYGldDi3IrThqzE5SscU5AAB4oSyRN21cAM 9T5j1DPkOFa4HVvjYX95vjCchdxR9jSD8xJ1LFu+fp0y1IJeLcJC580GmjcJyE5v3DBh AAcqh8FV14pQKyl22K9z48ynX9ECRSz4evpfsa2DGwptBnJuFh9XQUMfva7dOAFgEog2 pYxtqWwOhHCig2X9e5yMdP0FZFPxeaI1GVRQjbvm8KIr37a7qPtSRySNC+JRdbp6hjY+ Tc5Q== 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=QtLFxvL+cjgOXQamHXkAmVM3lDamsWb+w+bB0DgMeiQ=; b=DGMD7mRc4xAq1vBRjufv5IdncIURmzCGUZ6XE/wbHWVW0oxsBoShl+rd/RW/Htrfgc 1RgHF8jk02R70z3uNRLqcgySweEprw4qL3IzW9leNNdGGwCHRmORaqOhdt0EF5RnL/Mr 2cH6nKaZoDnSLpuyFRTolnCDSVwcVoBNZbXvNUzVsZiOr7fQi9+sTDRmFfwjAPyBFiBU q46hDquFbokCBfG+pdFa+MtAJYtjRAewTvseSmC1v22ngDU5hjVYpm+0+8nwc5mOljcx xRbUKfcjVcfkbx7AI93mT9kJyc5tmo0csbjjLNUoWmpTdwXvJA16WJFwdXAzYMahI4C/ ZMlA== X-Gm-Message-State: AKwxytdU0OIjGBkWF6ZeI4b/6IuJ8BtiLb7+6J7RI2vdMvG1AesfKUJh Oi7sUUyFujInD3alIoR0h5I= X-Received: by 10.99.167.15 with SMTP id d15mr20662160pgf.408.1517194802700; Sun, 28 Jan 2018 19:00:02 -0800 (PST) Received: from [10.67.68.20] ([192.55.54.40]) by smtp.gmail.com with ESMTPSA id p69sm29233670pfk.7.2018.01.28.19.00.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jan 2018 19:00:01 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: Date: Mon, 29 Jan 2018 10:59:58 +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 V3] 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" V3: - Add error check when av_buffer_ref fail - Fix exec encode/write but output file hasn't been open lead to crash issue - Add build check V2: - deduce output format from file extension, because VP8/VP9 encoder need to a muxer (e,g ivf or/and mp4). From 1f9dcc65093e05a0a7a931352aaa9c1e67be5d2f Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Thu, 11 Jan 2018 15:00:30 +0800 Subject: [PATCH V3] 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 | 305 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 doc/examples/vaapi_transcode.c diff --git a/configure b/configure index fcfa7aa442..c4cf32e731 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 h264_vaapi_encoder" # 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..45eeef0a96 --- /dev/null +++ b/doc/examples/vaapi_transcode.c @@ -0,0 +1,305 @@ +/* + * 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); + if (!decoder_ctx->hw_device_ctx) { + fprintf(stderr, "A hardware device reference create failed.\n"); + return AVERROR(ENOMEM); + } + 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) +{ + 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; + } + + /* 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; + } + + ret = avio_open(&ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE); + if (ret < 0) { + fprintf(stderr, "Cannot open output file. " + "Error code: %s\n", av_err2str(ret)); + 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); + + 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); + + /* 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; +}