From patchwork Sun Jun 26 16:35:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 36462 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp1539941pzh; Sun, 26 Jun 2022 09:36:42 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sTHOPi+LUpqSOfULbOZwQ4x2O8+ggJ2/x0fXm6OBvB/2dcq+N7tOtiP4bjawtCHPRzA0hY X-Received: by 2002:a17:906:5d14:b0:722:f288:f19d with SMTP id g20-20020a1709065d1400b00722f288f19dmr8688514ejt.323.1656261402367; Sun, 26 Jun 2022 09:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656261402; cv=none; d=google.com; s=arc-20160816; b=kbsFKFGMja8SWcQ1Rt959PiCSaIQWQRakH/PntW5/QPAV/+RQa79MtMd2aeh6T+qYM PZXvKNuAs7i2S+WzqqK7TEBoR72tvG49erIOicsU7DgZNqjqF94atoJgEzI89JYWok7R 5khCu19K2YxhMxCmwGv8InytL9ecvl2Q8IY2Q/uSj53Lll2VaMUbWXrd3mevjcNejo0Y r6H8yYfhPl4C4vgTkHjJ5KSexo8mjwal21gXCjH5fq4sWTTCFG22WHfTbO7m61X4S0KM wvFMCG2sF7EhRFSKfyTXg1vpd+tBtsOxJt3pWxNL26h/K1qRxC0fdvB2BUNAlcEPDxr2 x3Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:mime-version:fcc:date:references :in-reply-to:message-id:from:dkim-signature:delivered-to; bh=77pS+/jtHJYjMI6CvOVvrbGk3iHeJem4CBImfLKA2gQ=; b=steh7DXFV45ySbUfyPD9hcOvKelVIUa/d9wHe325P65V1G1chZfPPsMjrURkYjiJIy HoPGK7V65Z5PhGv5ackw1lKIxOvk/aA7oswTZiCPpVbKu424vYhi3ExKAB9lQn1s4k7d HAW1aWDXpBXdA0Duk+voh4eMyZVtDKkfXdsxe4D6lvH8vDKz3t+jllQdKVPrGQAdIkIF uPOchH+OXVLDl3Uee4zwVe6vscKkyvt5LEZaFUS6I8JtTbEvr9311px7MTxTx7QUT2ee g8DhRtqQyDmEY6PCCtDVn8oIUZ7SpOFQP3IAxwc0L0xydr4Yb4GffjJatz7WrmzRWRMY S7Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=fBN9Q5fW; 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-20020aa7d0d4000000b004355e601557si9927895edo.170.2022.06.26.09.36.42; Sun, 26 Jun 2022 09:36:42 -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=fBN9Q5fW; 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 73FDC68B84E; Sun, 26 Jun 2022 19:35:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BDEC868B7FC for ; Sun, 26 Jun 2022 19:35:30 +0300 (EEST) Received: by mail-pg1-f172.google.com with SMTP id 23so6948428pgc.8 for ; Sun, 26 Jun 2022 09:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:message-id:in-reply-to:references:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=OwAbOvynqRhMS61zCf3wNHPYflVi3DT6Sol+N98Ic8k=; b=fBN9Q5fWEwg617vhRotoNUYdbEl3XCfoYCfwjnVRMfFRKVwvqq8IkhhoBDMD5AT1jo mblUvmm4Lfp5n9MNsR9yH021E4rP6U3eJoj1SWoZ9C/RrI4IV6JYFeGEe2ZXl9O4TXUU 5yhtqPpLL4kXnLBiV2aPjOuia9R4QbZWnxNFPFn+Ela382GaRU+tZuyu/XZhqGHFkuv9 h5icBW/N2ecIOlyyHr4LpWvY62j1UEFKztgStKmJ/ixcrKptFnZW/cQVZgEWuRPq7NJk WH7/5/mSuAQAyXa3aI+luJkkP9rdXbdNGUFb6GPrBzhB7Zn8yUhErYn6COtSFsbDiWqg gB0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:in-reply-to:references:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=OwAbOvynqRhMS61zCf3wNHPYflVi3DT6Sol+N98Ic8k=; b=wQwmgIZSDErojIVqns0JAJ3hmndbLHJLDxYEfE72zoQvH6ivA7YYkHfmxVfNGLE2DO jUhZFvpvPosX+X7E/yZ3jIYtVO7Ab2oJIa/JFI2wktBzqORDqKkNLuQOUTzgw+i1760I ueIvVi8QRVynWIG6EIVhC/G7Fi0QdHYKdls9+Qb3I5ePQmJGp7wu98G4falL4lg2AqCL /zDVrmZxWeB3Fk8/tigkRyU8YVZTfKZIdAsFDHp8ea6O7eHV8I8Hj6gs2LnAB5T6W3xo R51qVt21V4OhVbCzeGho6XVylPSVbJuJhE5y9SCYcQBK0ilh7GJVhNhzHISvw5ym1ytQ THqQ== X-Gm-Message-State: AJIora+gwl8s7F0+Vc5mt16Jyi8db+M9qZs0dWQSg64TSUE1n9Ia9lKh XztyBPEIEpyCZilUqZCUfaJoM4Y4rlWodA== X-Received: by 2002:a63:8549:0:b0:40d:2864:efff with SMTP id u70-20020a638549000000b0040d2864efffmr8847363pgd.146.1656261328921; Sun, 26 Jun 2022 09:35:28 -0700 (PDT) Received: from [127.0.0.1] (master.gitmailbox.com. [34.83.118.50]) by smtp.gmail.com with ESMTPSA id k22-20020a170902761600b00167942e0ee9sm5420914pll.61.2022.06.26.09.35.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Jun 2022 09:35:28 -0700 (PDT) From: softworkz X-Google-Original-From: softworkz Message-Id: <0c2091e57cd464969fcfbc36fc3004a112910620.1656261322.git.ffmpegagent@gmail.com> In-Reply-To: References: Date: Sun, 26 Jun 2022 16:35:02 +0000 Fcc: Sent MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v6 05/25] avfilter/subtitles: Update vf_subtitles to use new decoding api 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 Cc: Michael Niedermayer , softworkz , Andriy Gelman , Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: fvZVAkzakS3N From: softworkz Signed-off-by: softworkz --- libavfilter/vf_subtitles.c | 67 ++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c index 82e140e986..0ae156ad07 100644 --- a/libavfilter/vf_subtitles.c +++ b/libavfilter/vf_subtitles.c @@ -36,14 +36,12 @@ # include "libavformat/avformat.h" #endif #include "libavutil/avstring.h" -#include "libavutil/imgutils.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "drawutils.h" #include "avfilter.h" #include "internal.h" #include "formats.h" -#include "video.h" typedef struct AssContext { const AVClass *class; @@ -304,8 +302,42 @@ static int attachment_is_font(AVStream * st) return 0; } +static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt) +{ + int ret; + + *got_frame = 0; + + if (pkt) { + ret = avcodec_send_packet(avctx, pkt); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0 && ret != AVERROR_EOF) + return ret; + } + + ret = avcodec_receive_frame(avctx, frame); + if (ret < 0 && ret != AVERROR(EAGAIN)) + return ret; + if (ret >= 0) + *got_frame = 1; + + return 0; +} + AVFILTER_DEFINE_CLASS(subtitles); +static enum AVSubtitleType get_subtitle_format(const AVCodecDescriptor *codec_descriptor) +{ + if(codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) + return AV_SUBTITLE_FMT_BITMAP; + + if(codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB) + return AV_SUBTITLE_FMT_ASS; + + return AV_SUBTITLE_FMT_UNKNOWN; +} + static av_cold int init_subtitles(AVFilterContext *ctx) { int j, ret, sid; @@ -318,6 +350,7 @@ static av_cold int init_subtitles(AVFilterContext *ctx) AVStream *st; AVPacket pkt; AssContext *ass = ctx->priv; + enum AVSubtitleType subtitle_format; /* Init libass */ ret = init(ctx); @@ -398,13 +431,17 @@ static av_cold int init_subtitles(AVFilterContext *ctx) ret = AVERROR_DECODER_NOT_FOUND; goto end; } + dec_desc = avcodec_descriptor_get(st->codecpar->codec_id); - if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) { + subtitle_format = get_subtitle_format(dec_desc); + + if (subtitle_format != AV_SUBTITLE_FMT_ASS) { av_log(ctx, AV_LOG_ERROR, - "Only text based subtitles are currently supported\n"); - ret = AVERROR_PATCHWELCOME; + "Only text based subtitles are supported by this filter\n"); + ret = AVERROR_INVALIDDATA; goto end; } + if (ass->charenc) av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0); @@ -460,27 +497,31 @@ static av_cold int init_subtitles(AVFilterContext *ctx) dec_ctx->subtitle_header_size); while (av_read_frame(fmt, &pkt) >= 0) { int i, got_subtitle; - AVSubtitle sub = {0}; + AVFrame *sub = av_frame_alloc(); + if (!sub) { + ret = AVERROR(ENOMEM); + goto end; + } if (pkt.stream_index == sid) { - ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt); + ret = decode(dec_ctx, sub, &got_subtitle, &pkt); if (ret < 0) { av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", av_err2str(ret)); } else if (got_subtitle) { - const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); - const int64_t duration = sub.end_display_time; - for (i = 0; i < sub.num_rects; i++) { - char *ass_line = sub.rects[i]->ass; + const int64_t start_time = av_rescale_q(sub->subtitle_timing.start_pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); + const int64_t duration = av_rescale_q(sub->subtitle_timing.duration, AV_TIME_BASE_Q, av_make_q(1, 1000)); + for (i = 0; i < sub->num_subtitle_areas; i++) { + char *ass_line = sub->subtitle_areas[i]->ass; if (!ass_line) - break; + continue; ass_process_chunk(ass->track, ass_line, strlen(ass_line), start_time, duration); } } } av_packet_unref(&pkt); - avsubtitle_free(&sub); + av_frame_free(&sub); } end: