From patchwork Mon Jan 9 06:19:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 2136 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp6917131vsb; Sun, 8 Jan 2017 22:19:21 -0800 (PST) X-Received: by 10.195.18.201 with SMTP id go9mr65648533wjd.200.1483942761807; Sun, 08 Jan 2017 22:19:21 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z39si7421215wrb.289.2017.01.08.22.19.21; Sun, 08 Jan 2017 22:19:21 -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; 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 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 154E368A55D; Mon, 9 Jan 2017 08:19:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f170.google.com (mail-pf0-f170.google.com [209.85.192.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BD56E68A4E3 for ; Mon, 9 Jan 2017 08:19:05 +0200 (EET) Received: by mail-pf0-f170.google.com with SMTP id 189so15825180pfu.3 for ; Sun, 08 Jan 2017 22:19:12 -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; bh=KMakgOLfERBBwzVK3ERN/dN1u2tigzWQOKms6pq5/m4=; b=c6boXFZjHEkgr5lFkXLFwwt9W7iHPjpt5XKKgrdYG4PNbPM3jqUHVHuuMswSC/Ekf7 oNgfM/RgUbOasQB8vfz0wUTfQeo60h4VFr1npeYTTtSVawEtAX9mGLWKV7Imhvn2IARs UiXcelojGBk1eY813o2F9pa9jECd9TGJG+ambN6ahV3S/bNYtYdtiMMa6q1fBwl8KLKa cFnYS1wj6WB+09QroBC4cm/Hqc5eN2TWR2ckOfUNE5esM3vd6JgEvPUPgHVum/J5A8Un Gy7eq1fzTlxgUvNagC8yTughP3QUUbc/vdO6ecPpHzGdxobJzMI1vFbdKS7j0hlrhIgR 4rLA== 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; bh=KMakgOLfERBBwzVK3ERN/dN1u2tigzWQOKms6pq5/m4=; b=XUY3DPeU5cGj1uKIXH8BM38TVekuI8KLbdb5BizxVEMTRoFe3cq5SN76TcHrqGuxvt D1rvsJwLUUKvBWdP4e3pWMaX6UE2AAg+TjePnu11LeW9kolv7jvpD5zEp2fedWm0gc2b ntFESbA664A+YncchAvKjaebl81mrPgFqd4EZkOiOOGduAP5vKlIqZAsbG/aglTr6qa+ DldeLPmtXME+2LoINdgvS+xi5f8uvz/ieuQHv0ci11ZNxWWP0HTkex6zrfunmthxp8m/ g+X5gaPE1rXxyVH/evfiT4j1XZjAocnYJ+w4NkwVmDCmiCjO6ZfW+4SqmWzsytfH1opW uVJg== X-Gm-Message-State: AIkVDXLAU+tfep85hSiICa+70iKZJyeb0ix2HRELfD5FZdlBhoxzDPQBsH5o8LUF7Ykxmw== X-Received: by 10.99.150.10 with SMTP id c10mr161537440pge.121.1483942751070; Sun, 08 Jan 2017 22:19:11 -0800 (PST) Received: from [10.239.204.194] ([192.55.54.44]) by smtp.gmail.com with ESMTPSA id a2sm153837890pgn.24.2017.01.08.22.19.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Jan 2017 22:19:10 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson , Yi Wang From: Jun Zhao Message-ID: Date: Mon, 9 Jan 2017 14:19:07 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 Content-Disposition: attachment; filename*0="0001-lavc-vaapi_encode-enable-force_key_frames-option-in-.pa"; filename*1="tch" X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] lavc/vaapi_encode: enable "force_key_frames" option in vaapi_encode 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 40804a849556e1303f6351f5560d5552e2588b12 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 9 Jan 2017 14:05:59 +0800 Subject: [PATCH] lavc/vaapi_encode: enable "force_key_frames" option in vaapi_encode enable the opt "force_key_frame" to force IDR output in vaapi_encode, and this option can take a expression as an argument, allowing functionality such as forcing a key frame(IDR frame) every 5 seconds like: -force_key_frames 'expr:gte(t,n_forced*5)' Signed-off-by: Wang, Yi A Signed-off-by: Jun Zhao --- libavcodec/vaapi_encode.c | 24 +++++++++++++++--------- libavcodec/vaapi_encode.h | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index a3c9991..26694aa 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -28,7 +28,7 @@ #include "avcodec.h" static const char *picture_type_name[] = { "IDR", "I", "P", "B" }; - +static int vaapi_encode_mangle_end(AVCodecContext *avctx); static int vaapi_encode_make_packed_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, int type, char *data, size_t bit_len) @@ -626,12 +626,16 @@ static int vaapi_encode_get_next(AVCodecContext *avctx, VAAPIEncodePicture *start, *end, *pic; int i; - for (pic = ctx->pic_start; pic; pic = pic->next) { - if (pic->next) - av_assert0(pic->display_order + 1 == pic->next->display_order); - if (pic->display_order == ctx->input_order) { - *pic_out = pic; - return 0; + if (ctx->force_idr_frame) { + vaapi_encode_mangle_end(avctx); + } else { + for (pic = ctx->pic_start; pic; pic = pic->next) { + if (pic->next) + av_assert0(pic->display_order + 1 == pic->next->display_order); + if (pic->display_order == ctx->input_order) { + *pic_out = pic; + return 0; + } } } @@ -657,14 +661,15 @@ static int vaapi_encode_get_next(AVCodecContext *avctx, if (!pic) return AVERROR(ENOMEM); - if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) { - if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) { + if (ctx->force_idr_frame == 1 || ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) { + if (ctx->force_idr_frame == 1 || ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) { pic->type = PICTURE_TYPE_IDR; ctx->i_counter = 0; } else { pic->type = PICTURE_TYPE_I; ++ctx->i_counter; } + ctx->force_idr_frame = 0; ctx->p_counter = 0; } else { pic->type = PICTURE_TYPE_P; @@ -834,6 +839,7 @@ int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n", input_image->width, input_image->height, input_image->pts); + ctx->force_idr_frame = (input_image->pict_type == AV_PICTURE_TYPE_I ? 1 : 0) ; err = vaapi_encode_get_next(avctx, &pic); if (err) { av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err); diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index cc05ac8..63b3e51 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -197,6 +197,7 @@ typedef struct VAAPIEncodeContext { int i_counter; int p_counter; int end_of_stream; + int force_idr_frame; // Codec-local options are allocated to follow this structure in // memory (in the AVCodec definition, set priv_data_size to