From patchwork Tue Jul 11 04:49:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?gb18030?B?bGl5b3ViZHU=?= X-Patchwork-Id: 4295 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp4491620vsb; Mon, 10 Jul 2017 21:49:54 -0700 (PDT) X-Received: by 10.28.4.145 with SMTP id 139mr982153wme.19.1499748593922; Mon, 10 Jul 2017 21:49:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499748593; cv=none; d=google.com; s=arc-20160816; b=DTAhTyy0A5iqINhiacEitJsnUb9oYNP381FF/G+bR0PLCWOYPtzmeuBZKJv9BJ0AK/ ohGXnh4ME3dq+RG1qqF9cG7wu4a7TVj55iXC70OS3SqSw79AX60FoVqU/u7IiPMJC0Nr iPDr++v2E6JMK+8xjXthdaNmIR9mavogfdjXdhzanrbri92ifkXO90MYpOXTar6kGP7d k/L9j00C/n42/sF+JdEZRn8u+aOR26OlIDj5eUAzD7KGXZuYfetkCU2BV4tzP+k3+YtK Z4rR/VkQXwYOuzLwa8axtkHefMmaTKz2S7dY2bt/OEm3DSFJ+c7hjNq2L/yHDplFBJL1 0lNQ== 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:feedback-id:message-id:date:mime-version:to:from :dkim-signature:delivered-to:arc-authentication-results; bh=pZEWk/mb62pve6rz0rjOC2d8fwpGZTXe3TuuzxhwI8s=; b=ne9H4vwAeyXGPmgIg3T+1X5+wLRZzHWTLAyYteP63spcdo7XjmRaUE5vDZ1m0/kwoZ 3oeo/18SYGpSm80JBYrmcKpzLGWcLmij7D31J2MdydPusWbxtmRYo/rGQym17y0aucnU TBSu08paDhROcmvPKSPP9X3NTLm7O0tU9VrCGKkXLWEc+awL5sb3bEKYFD2jICdy21Tl VQLL7lDO9oHOQtVWQAQFkq+i/cgF3yBYydm1FPWjGUlcIZIWEEUERo4TUYgtWElzhFmz b3KC3UcULNoeyBzWTpft8rIX+K35nG83t954Au1oKmK13XCxYG/sp1KE/ZIPFq5S5CgE zPSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@qq.com header.b=xq7I57ST; 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=qq.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d20si9363312wrc.300.2017.07.10.21.49.53; Mon, 10 Jul 2017 21:49:53 -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=@qq.com header.b=xq7I57ST; 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=qq.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 076AD689E63; Tue, 11 Jul 2017 07:49:46 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbg337.qq.com (smtpbg337.qq.com [14.17.44.32]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 71D50689E34 for ; Tue, 11 Jul 2017 07:49:37 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1499748544; bh=/nBO2rnQC8nN/mp03fqwa49uyeYrndhalTjimgQd+hQ=; h=From:To:Subject:Mime-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID; b=xq7I57STnOZnBCZnUHHNWCInpQ1+a5qWFJUStG+sgqec9hOwWj3FJmi0Mmk/yyq4r BYFivEvPjhiVW9M/vqT0sb8UPqKzOxezKd0SjiJ7EfFE3H41fyJowhuuIEWjQVRr3R aQXMbxSzt4CWdhDrrBVcK5nWAUEm+vzadBoqD5Dc= X-QQ-FEAT: geW31bdliPkOd07OguhP7ainAAg1NOFfG49UXfGoR+kRr0NjBEqR6GwsCUauS NmIlVLxNPxlaecPiAN18x05iZwVjRQ6q+xq7/TeNkh20qHbb6raxzsCXFcUALhkK/r7ekAE m4vYSIyVdYmRvgNAMJ7HsTueIG1ZbHIvm3gFirHBAVL4Lcd0YXvrbAvOX/TQhAAAgb7/lvi l5JVcsaeXH96e/ZRz2bbVAv9U5Kj9UhTXzRX4+u+9DRAjRpu6MgPOOgfkbo2Qpj0hvHwYh0 ZlQoBgWO7OKJapH+pO6xTRdMHGGHM2GCormg== X-QQ-SSF: 00000000000000F0 X-QQ-WAPMAIL: 1 X-QQ-BUSINESS-ORIGIN: 2 X-Originating-IP: 116.30.222.205 X-QQ-STYLE: X-QQ-mid: riamail99t1499748542t6689142 From: "=?gb18030?B?bGl5b3ViZHU=?=" To: "=?gb18030?B?RkZtcGVnIGRldmVsb3BtZW50IGRpc2N1c3Npb25zIGFuZCBwYXRjaGVz?=" Mime-Version: 1.0 Date: Tue, 11 Jul 2017 12:49:02 +0800 X-Priority: 3 Message-ID: X-QQ-MIME: TCMime 1.0 by Tencent X-Mailer: QQMail 2.x X-QQ-Mailer: QQMail 2.x X-QQ-ReplyHash: 3692466651 X-QQ-SENDSIZE: 520 Received: from qq.com (unknown [10.222.130.248]) by smtp.qq.com (ESMTP) with SMTP id ; Tue, 11 Jul 2017 12:49:03 +0800 (CST) Feedback-ID: riamail:qq.com:bgforeign:bgforeign1 X-QQ-Bgrelay: 1 X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: Re: [FFmpeg-devel] [PATCH V2] examples/vaapi_dec: Add a VA-APIhwaccel decoding 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" where is your media file for testinfg ---Original--- From: "刘歧" Date: 2017/7/11 12:39:31 To: "and patches development discussions FFmpeg"; Cc: "wm4";"Mark Thompson";"刘歧";"Liu, Kaixuan"; Subject: Re: [FFmpeg-devel] [PATCH V2] examples/vaapi_dec: Add a VA-APIhwaccel decoding example From 0e4d230ae4c98949a962c6bbdad31d216b54bb6a Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Tue, 21 Mar 2017 11:04:41 +0800 Subject: [V2] examples/vaapi_dec: Add a VA-API hwaccel decoding example. Add a VA-API hwaccel decoding example. Signed-off-by: Liu, Kaixuan Signed-off-by: Jun Zhao --- doc/examples/vaapi_dec.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 doc/examples/vaapi_dec.c +} -- 2.11.0 > On Jul 11, 2017, at 12:06 PM, Jun Zhao wrote: > > V2: re-work with new hw decoding API. > <0001-examples-vaapi_dec-Add-a-VA-API-hwaccel-decoding-exa.patch>_______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel diff --git a/doc/examples/vaapi_dec.c b/doc/examples/vaapi_dec.c new file mode 100644 index 0000000000..01320a3b71 --- /dev/null +++ b/doc/examples/vaapi_dec.c @@ -0,0 +1,266 @@ +/* + * Video Acceleration API (video decoding) decode 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 decoding example. + * + * @example vaapi_dec.c + * This example shows how to do VAAPI-accelerated decoding with output + * frames from the VAAPI video surfaces. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static AVBufferRef *hw_device_ctx = NULL; +FILE *output_file = NULL; + +int decoder_init_vaapi(AVCodecContext *ctx) +{ + int err = 0; + const char *dev_name = "/dev/dri/renderD128"; + + if ((err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, + dev_name, NULL, 0)) < 0) { + fprintf(stderr, "Failed to create a VAAPI device.\n"); + return err; + } + ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); + + return err; +} + +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; + } + + return AV_PIX_FMT_NONE; +} + +int retrieve_data(AVFrame *input) +{ + AVFrame *output = NULL; + int err; + av_assert0(input->format == AV_PIX_FMT_VAAPI); + + if (!(output = av_frame_alloc())) + return AVERROR(ENOMEM); + /* default output nv12 */ + output->format = AV_PIX_FMT_NV12; + if ((err = av_hwframe_transfer_data(output, input, 0)) < 0) { + fprintf(stderr, "Failed to transfer data to output frame: %d.\n", err); + goto fail; + } + + if ((err = av_frame_copy_props(output, input)) < 0) { + av_frame_unref(output); + goto fail; + } + + av_frame_unref(input); + av_frame_move_ref(input, output); + av_frame_free(&output); + return 0; + +fail: + av_frame_free(&output); + return err; +} + +int write_frame(AVFrame *frame) +{ + int idx, size; + int width = frame->width; + int height = frame->height; + + av_assert0(frame && frame->data[0] && output_file); + + for (idx = 0; idx < height; idx++) { + if ((size = fwrite(frame->data[0] + idx*frame->linesize[0], + 1, width, output_file)) < 0) { + fprintf(stderr, "Dump Y to file error.\n"); + return -1; + } + } + + height >>= 1; + for (idx = 0; idx < height; idx++) { + if ((size = fwrite(frame->data[1] + idx*frame->linesize[1], + 1, width, output_file)) < 0) { + fprintf(stderr, "Dump UV to file error.\n"); + return -1; + } + } + + return 0; +} + +int decode_write(AVCodecContext *avctx, AVPacket packet, int flush) +{ + AVFrame *frame = NULL; + int ret = 0; + + ret = avcodec_send_packet(avctx, &packet); + if (ret < 0 && ret != AVERROR_EOF) + return 0; + + if (!(frame = av_frame_alloc())) + return AVERROR(ENOMEM); + + ret = avcodec_receive_frame(avctx, frame); + if (ret >= 0) { + /* retrieve data from GPU to CPU */ + if ((ret = retrieve_data(frame)) < 0) + goto fail; + + if ((ret = write_frame(frame)) < 0) + goto fail; + } else if (flush == 0) { What about !flush? or small than 0? + ret = 0; + } + +fail: + av_frame_free(&frame); + return ret; +} + +/* flush the decoder */ +int flush(AVCodecContext *avctx) +{ + AVPacket packet; + int ret = 0; + + av_init_packet(&packet); + packet.data = NULL; + packet.size = 0; + + while (1) { + if ((ret = decode_write(avctx, packet, 1)) < 0) + break; + } + av_packet_unref(&packet); + + return 0; +} + +int main(int argc, char *argv[]) +{ + AVFormatContext *input_ctx = NULL; + int i, video_stream, ret; + AVCodecContext *decoder_ctx = NULL; + AVCodec *decoder = NULL; + AVPacket packet; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return -1; + } + + av_register_all(); + + /* open the input file */ + if (avformat_open_input(&input_ctx, argv[1], NULL, NULL) != 0) { + fprintf(stderr, "Cannot open input file '%s'\n", argv[1]); + return -1; + } + + if (avformat_find_stream_info(input_ctx, NULL) < 0) { + fprintf(stderr, "Couldn't find input stream information.\n"); + goto fail; + } + + /* find the video stream information */ + video_stream = -1; + for (i = 0; i < input_ctx->nb_streams; i++) { + AVStream *st = input_ctx->streams[i]; + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + video_stream = i; /* NOTE: just handle the first video stream */ + break; + } + } + + if (video_stream == -1) { + fprintf(stderr, "No find a video stream in the input file.\n"); + goto fail; + } + + /* initialize the video decoder */ + if (!(decoder = avcodec_find_decoder(input_ctx->streams[video_stream]->codecpar->codec_id))) { + fprintf(stderr, "Could not find input codec\n"); + goto fail; + } + + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) + return AVERROR(ENOMEM); + + if (avcodec_parameters_to_context(decoder_ctx, + input_ctx->streams[video_stream]->codecpar) < 0) + goto fail; + + decoder_ctx->get_format = get_vaapi_format; + av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0); + + if (decoder_init_vaapi(decoder_ctx) < 0) + goto fail; + + if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { + fprintf(stderr, "Failed to open codec for stream #%u\n", i); + goto fail; + } + + /* open the output file to dump YUV */ + output_file = fopen(argv[2], "w+"); + + /* actual decoding and dump the YUV data */ + while (av_read_frame(input_ctx, &packet) >= 0) { + if (video_stream == packet.stream_index) + ret = decode_write(decoder_ctx, packet, 0); + av_packet_unref(&packet); + + if (ret < 0) + break; + } + + /* flush the decoder */ + flush(decoder_ctx); + +fail: + if (output_file) + fclose(output_file); + avcodec_free_context(&decoder_ctx); + avformat_close_input(&input_ctx); + av_buffer_unref(&hw_device_ctx); + + return ret;