From patchwork Sat Jun 25 09:57:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 36430 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp825400pzh; Sat, 25 Jun 2022 02:59:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vEbRR5kg0Yy//ekg2vEE2Hhk0OKGVhdABkhp6RbEHKj+ykqHZVFgpFs0VOJ7Vu2j2LevTb X-Received: by 2002:a17:907:72c5:b0:722:e515:fcc6 with SMTP id du5-20020a17090772c500b00722e515fcc6mr2958411ejc.533.1656151140617; Sat, 25 Jun 2022 02:59:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656151140; cv=none; d=google.com; s=arc-20160816; b=HZloQvK1n47BuIj3bnRr9IBs5QKxgC7jeqUkDEbT/hYh5NFYNB1fOFpihFaYEqVa7r 5ni8ij7inTpO70gLFMFBj3zBQAeW9awmsfv4gxwMEbVcRV6ExUDc+QsW01Bue5UmQBeV wEL3b8jW7mhxqsb3Nqm3yfVqm06AF1xgKLAJirinGfrcMY+H7X+6ETPl/9wh3VG/ff+t nOZKbByFMwvjTxEAO4MiIjKFz4J3/x7/axEXFrukKzyloRhcL5VYSTXbeOWeY+7JYtMu PxzhquLSy6PGtv2U52o5nH9NOBrbVl/Wgq3YTjeritynrk+ca5Myya4KHt16jwVfrOBo HCPQ== 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=eSr98XqusLhmpL5Tw8JGrxKuj6pkBAy17NwZqnNpFuIhhrSla41oOkl7TY+HIEqFP1 9lNwpRys8POWYmfDgxX5z312t/j8Ndo+mDjgwpnNhePcARWWTeGDwR8AN3gv/SwEVXkM VZO+FzEYYRrPHGe1VcUVGj/MWSRj4nSQRxJUXY6feWwPHDWKi5JynNWE98uvsSSGN3nH q42PFldXBWQF6RqHWK0HiX0srronu5azvXR8cD0IclnNzZCZwBZRG+goXsvNDeRm3xIC ltdjw4hu7fzxnkuuAsbfLuCOfP+zEgVmBxGqpJ9mF8JV95MV/IU2pkB60fFqVoBlubxX bnkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=FkbbSLgO; 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 h27-20020a170906399b00b0072629670b92si4947567eje.448.2022.06.25.02.59.00; Sat, 25 Jun 2022 02:59:00 -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=FkbbSLgO; 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 0AC5B68B814; Sat, 25 Jun 2022 12:58:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2303468B7E8 for ; Sat, 25 Jun 2022 12:58:08 +0300 (EEST) Received: by mail-pf1-f169.google.com with SMTP id k127so4614523pfd.10 for ; Sat, 25 Jun 2022 02:58:08 -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=FkbbSLgO4Bbz/dUnAc0ip926815f0KQwUgsw4vDs10jxqWPH82/g3EqtzKI1lnQU4h Shrtq05cfInTGWTIx07+Aw5tB31XDbBrw2cx1FmMzjZ66zKCqPiXcHbpkWIkWKL8dAgH 1818gfqWcKJZtXxPuV0GbDckP/B/aW+W93FDC+5VJHFqKcx3RRbGx/gHzhF6PxHeMAUx 7SpXnJDKNutkuDhFouU941rzzoBu+qEd6rriOIJDJ7j4NMW2Tl+jmaVUtxSeVEXbnAOW Ol8zenkl0/PCKB+h4l69eYZ5yeyysRsz3u7giY+gCXZBqTmuY0CghFJM6WBjId9Smf5v +ZNg== 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=ZRrfTCIzqvyFCae/4C2ObFduryuRWX8/fa57VGc4Ymj+api8/AbGJpBR5SXWxAY0BC v+oBre1uzW1i+CuPSMJD6BQTuy1S6mCxv1LPNIaS6gYb9Vj8rI16TBtFBB22Ixpsr+Zr L44xRTzqyby6u4f728pTByvnZJ1EbfwsZTlrE8ra13uR969Im/lCxlFh9B4S7PXteYJl ai3JJIkFH1BE7XywSmrj0KDH7CLjC0d7K88SE/tP6YA9yttwbPHtxWefqNzS0F+0H6k1 EHCyGCw2DyRA1syKdruuLpWXmdQnp97pcStwNn8copmp0GYEU56U2aHFaULav3DdAQlM TLDw== X-Gm-Message-State: AJIora/JPYGw0AA+9YDuSfdFoC3DbQc3qKI7wXqPvE/jevWhWrX2JHDs Isai5zYXydWkfjV4fpxM1CV755j5Z79INw== X-Received: by 2002:a63:3308:0:b0:40c:7cb4:fe05 with SMTP id z8-20020a633308000000b0040c7cb4fe05mr3069341pgz.604.1656151086506; Sat, 25 Jun 2022 02:58:06 -0700 (PDT) Received: from [127.0.0.1] (master.gitmailbox.com. [34.83.118.50]) by smtp.gmail.com with ESMTPSA id a27-20020aa794bb000000b005252a06750esm3251683pfl.182.2022.06.25.02.58.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Jun 2022 02:58:06 -0700 (PDT) From: softworkz X-Google-Original-From: softworkz Message-Id: <0c2091e57cd464969fcfbc36fc3004a112910620.1656151077.git.ffmpegagent@gmail.com> In-Reply-To: References: Date: Sat, 25 Jun 2022 09:57:37 +0000 Fcc: Sent MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v5 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: ViRmeZzOXVey 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: