From patchwork Tue Oct 25 09:13:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 38981 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp2684472pzb; Tue, 25 Oct 2022 02:14:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6vAACZJwW9PGxo1VX/zaP17LKYGcJ29FcR5jBt1BMPs1wNOB6QLwl+s4Nr/2DMeFIzn8JX X-Received: by 2002:a05:6402:a46:b0:461:ed76:cb56 with SMTP id bt6-20020a0564020a4600b00461ed76cb56mr5934849edb.264.1666689290422; Tue, 25 Oct 2022 02:14:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666689290; cv=none; d=google.com; s=arc-20160816; b=saTYD6zG2e0WwHK1APwCSwd/BA+DS6QWB/UHOuKTLKzVHcWHLvFDYqvXXAoa2c+TpS e1pEEMnUs9f13tUOpiDJ87FVm/Wvyhquh9NtIMq1JC3RZvGTnwP7j3KPYfeeVvcfRq07 jRvTEDk4QpXgb+b2IntZCok7qgWu6Xpqn5O6lhtird6GvLClMnYc5EpJS42WzUwy89zd 5gYid8O0h+sbcWXH+nuYGn4L3fPMuwuDwTS2jHLzvl6F4Uj9JAe2k7KnXyeUJ2LWt9J7 HgngA9jkQaPqmU0m5Ki2UCLtubrVfU58+38pgEQxPKcYRQiLNLnM20IRFW3IbybXqouU Halw== 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=llJSN6pzX5lidOMH+m2oqLAcx5KBJlyuERzx+Eia3LLXlQ38wcVtlc3LY6LXpwgKKN G4FV9h3tAjgrjk7lIQeZk6Ya7TrjGjXOOGAugiEIxl87IaJV77W5cjFZAZHkd5a8vcu1 XZ5B9562/KAa1yqVcUHSh96+TPEpcREvFcQi6vuQwZUDowac3hXUJXR3OO6WLQPT1kN5 bnpqkbzEgCnXKI59Xc3Wr/Y1aWDYiBlc670CW7g5ElujKflqBN6DASowv7LA7hjI7zEN gzC2Ujyhde6zjvATP9LQBg0Iw7wbVIHBSFBPsaCik9H2O1zXJ8eN90XcEDWVI6tBzh3j kSNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=gXKT20bh; 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 b24-20020a17090636d800b0078200f886bbsi1988323ejc.361.2022.10.25.02.14.50; Tue, 25 Oct 2022 02:14:50 -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=gXKT20bh; 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 DCDFB68BCBB; Tue, 25 Oct 2022 12:14:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6B90E68B85C for ; Tue, 25 Oct 2022 12:13:57 +0300 (EEST) Received: by mail-pf1-f177.google.com with SMTP id y1so11255842pfr.3 for ; Tue, 25 Oct 2022 02:13:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date :references:in-reply-to:message-id:from:from:to:cc:subject:date :message-id:reply-to; bh=OwAbOvynqRhMS61zCf3wNHPYflVi3DT6Sol+N98Ic8k=; b=gXKT20bhQyI9Rw/XPamQZPgmwLyFaoqxRaz5nArcXrHDdwEbD8UZYhpicszcXiW2a1 rYgqcGDyqZTwmIWSdmKDy9yq6ZLynhppPcInQpAMnTAB2H+0lwDoyWNLhlUvQcOZTs4I 4gIDKMbMQz2Ycl5yWaHUiF4uJeGI2cyvecfuvTyM0nhj0dtCXquLmL2mhftkDZUdlqJ7 nltTMitQnhBfyFi/6f7aH6F4iouiiwP5r0Gm+S+pcQy+0IPP/1UZ87VoKUoxl5s3a7eW EQNaCbAxl41t9tztPXdXquWtJyJ2nGkADWC7lCBqi0y0PCqWOI78ni7+Pls0zgw/M8kl 5b/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date :references:in-reply-to:message-id:from:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=OwAbOvynqRhMS61zCf3wNHPYflVi3DT6Sol+N98Ic8k=; b=INM60YNM4JYLmXJo+XfpAIBl/8N7IAfIBc5OLU6EeUjKyUHgc3nmQmVzJUxPFyDShu GyKzShw2ZKQd5L2ErJ8xqFnLnYWwWpJnE95rwmrZbOdTye7nwyuTLtKIwGWh5rZr2WND SYWgtAaaGEpfeGqvZiW7KEtlw8S0u+Yj+Z9aK0VzpWu/C2QaYv6kUDpH37wgEVjL1Ugi yr5VzqdyFSHoO+/Oyfa40ZzrD1dKVOvmCWcFkr2EPo7+kfe9PfZsp8dfQhkXYIWu5d6e fH4cMxDTghfFeH6bJNYVcjM99Vb4Ndzuul5+98djWg9d67PoI52eAYQKra8GlnxIwJHH 4Mtw== X-Gm-Message-State: ACrzQf3Y9hzkXpO5rz2U4JWmxsZUV82ULCxPoInuAIqpAUnDJekS0p70 Gj9ULiltAU04WoQ5q//2EocEX3dUXZw= X-Received: by 2002:a63:8bc8:0:b0:461:ff70:7546 with SMTP id j191-20020a638bc8000000b00461ff707546mr32400819pge.70.1666689235478; Tue, 25 Oct 2022 02:13:55 -0700 (PDT) Received: from [127.0.0.1] (master.gitmailbox.com. [34.83.118.50]) by smtp.gmail.com with ESMTPSA id w20-20020a170902ca1400b001714e7608fdsm864094pld.256.2022.10.25.02.13.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Oct 2022 02:13:54 -0700 (PDT) From: softworkz X-Google-Original-From: softworkz Message-Id: <20430719d3d5db8e23d96a6c416182e20da86264.1666689226.git.ffmpegagent@gmail.com> In-Reply-To: References: Date: Tue, 25 Oct 2022 09:13:26 +0000 Fcc: Sent MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v9 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: softworkz Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: VASkyWdu9yw1 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: