From patchwork Tue Mar 28 21:25:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 40900 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:7a30:b0:df:834d:2c1a with SMTP id t48csp2503621pzh; Tue, 28 Mar 2023 14:26:35 -0700 (PDT) X-Google-Smtp-Source: AKy350b0/Bo9QAIiHsnSJflF15wyYA7ULAFerrpGZ/PEkqZjLoaKFd03Fp2v2iAA9fG+ixzS07lC X-Received: by 2002:aa7:db83:0:b0:502:3376:46bc with SMTP id u3-20020aa7db83000000b00502337646bcmr108965edt.8.1680038795512; Tue, 28 Mar 2023 14:26:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680038795; cv=none; d=google.com; s=arc-20160816; b=Um4OE8AYuVnQ5RSm+dZPi5bQ+NM6Raj4/K4aKoD9gXkBIFJBxnU973Pvjj773vm/6D +JkeWnNbFuBGx80sjDo5cyLvr++C0uAB0Rs8k+6Z9qHPOlGTxmMYj6RXAO2i6ojitVgR JbQwelI9sDOP8DuBurkyQ82f9jXltRebmrLbjmzpv95Uv8+GmRYSKHClWLAGuM5i5Zkv bUk8OpW5BebP+mHlHhxBkPA2P4KbJV2H838E5+I50IK+xQ4xh2CGcEYKSWc/o0MRI9EW mWFurDB1ry0mh3eoQAEjoAzJp8VIRJQXkMysN/ze3kE6dwb0yK59h0s0R89g6mTp3xE/ wXxw== 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:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=vgxeJg1ZGb1M+AJlUrC4zJWbpSQ4FVxVaWNgn0fvqCc=; b=XZ/0xB28amU/AvP4D3xghNUaP/rgUag53InXMgR73Tg2vksszClM32VjxlgdLrSDsY HZyOebCbttJvI1fKwWpIddeFUYNN09rpgruq3AUZpaN/jYy4nbtf7KrLMQRYRzbxmf3i Ob/NHU+kA86hEFgt84T2dO62tkPbYQESC2UMBmFAUC3SKWhH05uaIb5vg8eZftGS4/XT wQINMMVMNbk3uCecbytP25HhNwDNZEmUjznkHjjehc9EwXCvqoTa+ubeselY89xr3fO9 1VFH/5B64D1keHe4Qiqq1uQfiT/m4h45F9eYP6JUj/XPP347W1x2dIS4WLrclwba3uf2 ABmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=ZVA6xEGm; 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=QUARANTINE 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 u20-20020a05640207d400b004fc4b73c1b8si13133506edy.300.2023.03.28.14.26.16; Tue, 28 Mar 2023 14:26:35 -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=@gmail.com header.s=20210112 header.b=ZVA6xEGm; 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=QUARANTINE 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 1BBE968CC8B; Wed, 29 Mar 2023 00:26:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 739F668CC84 for ; Wed, 29 Mar 2023 00:26:07 +0300 (EEST) Received: by mail-oi1-f179.google.com with SMTP id bm2so10102804oib.4 for ; Tue, 28 Mar 2023 14:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680038765; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=bPy8duI8Jj6gPsapEzrVO/tzYa2RMZYoS8dPLYoRT5I=; b=ZVA6xEGmABLU058x4qhQWLj4+zDiTia6ECmW+J1KVN8VUvmhct1Y3I2GVSc7civAG0 x4w6upy4xNpJCWRi4qwTiPbVcyK0JUk23MLXFNt/U0IC45iw/qnTE6lztaAQu/rumAfZ +duyLRWXjOJ6fh5LsSITNIzRj0v2AbiH0waAhaTe2I0GwkFB+hKY8Omxvq86soGL8cp+ Ds60nyvcV4ixBFBl+tUKG99XxTUQlru3urzTEZ2eimQA3dyW1bcEUjJsofOgrwkEh/3k YnkZAV4QC/Ahg+ZEGRaaUmNiWPA2u5CpqQWtEpZBcg9ohwDlRUPBy6FEvCJ82jswh7we GUpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680038765; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bPy8duI8Jj6gPsapEzrVO/tzYa2RMZYoS8dPLYoRT5I=; b=gW5/bMBaiXKDwpO5Hn4gI1Hy+FjaXRp+EUKheDhZfXBZEjbnCe6gP4oVz6I9jRu/jB JPbULGJg9Ji2XzuQolwF0hXH3t0mUc9eD3Kjh8lFek8WWdVgsWWhmBBuSrRy7XoNRS4L vdVFaN6jTrSr+w6rPm88teFN/un0w0lDAjyWM3XwiCTfG9AU71Zv8tBheOwddpeG79ST DwR7nt+2yX6osp6TPE3iZlK0SSHPqzjCuhKNdA65Y9CGWJqaE6QRqqmRCTk57H9io4sM /OtISxMLlnQr/pLiN6c8Boi+wlj9+D6vOIcY+kJCTsftCKC6MiPgOotqvz/ocBOHqxgt ZTWg== X-Gm-Message-State: AAQBX9doriMGYfjsT6+QtlUFTm/wB6z+dPy60CXtPL6bPaQWriINzrYD 8b1D81iE4RBXCbv2u1UBlRfsmMm0bcc= X-Received: by 2002:a05:6808:4093:b0:389:4ecf:20ab with SMTP id db19-20020a056808409300b003894ecf20abmr2018013oib.49.1680038765257; Tue, 28 Mar 2023 14:26:05 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id w2-20020acadf02000000b00383e0bec93bsm12887915oig.49.2023.03.28.14.26.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 14:26:04 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Mar 2023 18:25:31 -0300 Message-Id: <20230328212531.3460-1-jamrial@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230328211243.GC375355@pb2> References: <20230328211243.GC375355@pb2> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/2] avcodec/libx264: add a flush callback X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: CACykE19BwDw Signed-off-by: James Almer --- libavcodec/libx264.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index 92828fabc3..cfdd422236 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -151,7 +151,7 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, { X264Context *x4 = ctx->priv_data; uint8_t *p; - uint64_t size = x4->sei_size; + uint64_t size = FFMAX(x4->sei_size, 0); int ret; if (!nnal) @@ -178,8 +178,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, memcpy(p, x4->sei, x4->sei_size); p += x4->sei_size; size -= x4->sei_size; - x4->sei_size = 0; - av_freep(&x4->sei); + /* Keep the value around in case of flush */ + x4->sei_size = -x4->sei_size; } /* x264 guarantees the payloads of the NALs @@ -648,6 +648,24 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; } +static void X264_flush(AVCodecContext *avctx) +{ + X264Context *x4 = avctx->priv_data; + x264_nal_t *nal; + int nnal, ret; + x264_picture_t pic_out = {0}; + + do { + ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out); + } while (ret > 0 && x264_encoder_delayed_frames(x4->enc)); + + for (int i = 0; i < x4->nb_reordered_opaque; i++) + opaque_uninit(&x4->reordered_opaque[i]); + + if (x4->sei_size < 0) + x4->sei_size = -x4->sei_size; +} + static av_cold int X264_close(AVCodecContext *avctx) { X264Context *x4 = avctx->priv_data; @@ -1335,12 +1353,14 @@ FFCodec ff_libx264_encoder = { .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE | + AV_CODEC_CAP_ENCODER_FLUSH | AV_CODEC_CAP_ENCODER_RECON_FRAME, .p.priv_class = &x264_class, .p.wrapper_name = "libx264", .priv_data_size = sizeof(X264Context), .init = X264_init, FF_CODEC_ENCODE_CB(X264_frame), + .flush = X264_flush, .close = X264_close, .defaults = x264_defaults, #if X264_BUILD < 153