From patchwork Mon Dec 12 20:17:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: facefunk X-Patchwork-Id: 39685 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp580620pzb; Mon, 12 Dec 2022 12:18:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf5EiYmEiLL9aanLddjjjFzXyfAknUOCg+oPD23lH1QEucA+X/hano3AiVlPOmsxc5ui14XH X-Received: by 2002:a05:6402:294d:b0:45c:cd16:aeae with SMTP id ed13-20020a056402294d00b0045ccd16aeaemr15010171edb.13.1670876296321; Mon, 12 Dec 2022 12:18:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670876296; cv=none; d=google.com; s=arc-20160816; b=AD/w2A5sEPudQn8tYOZqoThN11Sp2FpJOrbKQnpqkx/d/AXwzQ3g3DoaQJs83FRy8/ Eb6eNAogG5JoQuEqjQHDcNTv8dO7RksCWfjIvL1KK1baoBd+zZzFMnAGz2NnAxFuO3Cn kS26vWfdJsJzEYS6PoYyUlY1IW4Smp0qZfwiiyOH5Pt0HVJHeLBtgNpvMfv98QIGD9E0 SRrkVyYwTv5QaaQFbhICSiF3M/peHCjB8gSwk5fW5vFKJENS4CrEQ/+RDDV0BHoMNvCd pxDJEKZL1wtDwVg7+qyBEmIj7xp/MyxiX3gep1V8Yf4HOSf/R1u7G14KvoZZ6IVriL7L +g4A== 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:message-id:date:to:from :delivered-to; bh=EWJHjA4Y6prmmKILvunxC/noh36U5saqBOVA7EBtayY=; b=nfTREAhnxgQWbP181lfwx+IDN5xFAtadcB5kI+C+BcAwZji5Ju7N2S81MLl+lRjOpc INPV9Xd+n7SW39bljAyOF9+HlUtKoLJihECFNCMbF+3CCc3bVzIVmrDMHNV9A/4kbZ37 2kTac9WhDVHorBrkDMks2rRqqLSgpqUajlSpYfYXto59dxM/tgws4OX0w4K1/TaWR1lF 3nkzjA5oOJqH4IbVMJLQaDNPn07ZlirXfOtNTCBRmmU5KCSrfJAT84NCZ72D1xcHchZt zppNj5DshLruPZKQlvXPXAcHpxg6nVwUICHjZlTc3NtUDUV5ZXezpZLgJH1htTcP/BKH Vvfg== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s17-20020a056402521100b0046b1abd7876si9374224edd.535.2022.12.12.12.18.14; Mon, 12 Dec 2022 12:18:16 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 02B1668BDDA; Mon, 12 Dec 2022 22:18:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3C5CB68B43E for ; Mon, 12 Dec 2022 22:18:05 +0200 (EET) Received: by mail-wr1-f45.google.com with SMTP id h16so4442542wrz.12 for ; Mon, 12 Dec 2022 12:18:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Iv5yLVLLQIy/Wq7pDH37XpZavr2g3mUXxg8ueetZSfs=; b=bT5VDrfzcRAB2IfqfV2u15Ts02o/gtemFXBZ7DDPO5BLHUvAEkrAZG9y6MgYGYy1Jp NLnrd86UfvZUcWpPaseSW9ovy3z1+oWXgKIW0/Ly5eo9TTDM7No9eG6NEGYA0yBx+Jlu iK7fYpeQL0WBzIMTovUCb7YjK6YMT72JLHLFoKpGotEbhO+41DHd6n/+hYGn1H7U8ug+ 4qAZcfhFB/0zMj/+8To/E6XzBBfFf8mNbpVpCUXhWS7OVnCtD+kGAs7miwPoBjMzS4N5 SMa254LmNLjBx653yJuERpzvafgCmV9mhtGGvyy9OTh4bTqnj3VU+nhxysRALa6ZfUYM NtDA== X-Gm-Message-State: ANoB5plq64rTkjH25vVZACTzP9akBuRdpL5tFXn0OGE3xG8p/hBvqzJB KKowwZbK2lx9NXAuZtM58IoIZJwxdTE= X-Received: by 2002:adf:f54a:0:b0:242:2244:b15d with SMTP id j10-20020adff54a000000b002422244b15dmr11820108wrp.23.1670876284309; Mon, 12 Dec 2022 12:18:04 -0800 (PST) Received: from localhost.localdomain ([90.243.4.231]) by smtp.gmail.com with ESMTPSA id a18-20020adffb92000000b002424b695f7esm9725429wrr.46.2022.12.12.12.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 12:18:03 -0800 (PST) From: facefunk To: ffmpeg-devel@ffmpeg.org Date: Mon, 12 Dec 2022 20:17:48 +0000 Message-Id: <20221212201748.120037-1-facefunk@fcfnk.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavcodec: Make disposition forced work with mov_text subtitles. 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: aVPCN9GwXAny We are not currently able to force mov_text subtitles by setting -disposition:s:0 +forced or equivalent. By setting the forced flags in movtextenc as specifid in https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html subtitles can be forced as expected in VLC and similar players. Copy AVStream disposition to AVCodecContext and use to set DISPLAY_FLAG_ALL_SAMPLES_FORCED in movtextenc.c. Signed-off-by: facefunk --- fftools/ffmpeg_mux_init.c | 5 +++++ libavcodec/avcodec.h | 11 +++++++++++ libavcodec/movtextenc.c | 41 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 0280759b05..86cc304b71 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -1717,6 +1717,11 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o) if (ret < 0) goto finish; + + // For output streams, Set subtitle flags to forced when stream + // disposition is forced. + if(ost->enc_ctx && ost->st->disposition & AV_DISPOSITION_FORCED) + ost->enc_ctx->subtitle_flags |= AV_SUBTITLE_FLAG_FORCED; } } else { // For each media type with more than one stream, find a suitable stream to diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 0ac581d660..2735a1c847 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2057,6 +2057,17 @@ typedef struct AVCodecContext { * The decoder can then override during decoding as needed. */ AVChannelLayout ch_layout; + + /** + * Subtitle codecs only. Bit set of AV_SUBTITLE_FLAG_*. + * + * Some subtitle codecs may use this field to determine the subtitle + * display flags to encode. + * + * - encoding: set by user + * - decoding: unused + */ + int subtitle_flags; } AVCodecContext; /** diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 7aa74d7c9d..875e003fc4 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -50,6 +50,40 @@ #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5) #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size) +/** + * https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html + * + * Display flags + * A 32-bit integer containing flags that describe how the subtitle text should + * be drawn. + * The following flags are defined: + * + * Vertical placement + * Controls vertical placement of the subtitle text. + * If this flag is set, the subtitle media handler uses the top coordinate of + * the display bounds of the override 'tbox' text box to determine the + * subtitle’s vertical placement as described in Subtitle Track Header Size + * and Placement. Otherwise, the subtitle displays at the bottom of the video. + */ +#define DISPLAY_FLAG_VERTICAL_PLACEMENT_TOP 0x20000000 + +/** + * Some samples are forced + * Indicates whether any subtitle samples contain forced atoms. If this flag is + * set, at least one sample contains a forced ('frcd') atom as described in + * Subtitle Sample Data. + */ +#define DISPLAY_FLAG_SOME_SAMPLES_FORCED 0x40000000 + +/** + * All samples are forced + * If this flag is set, the subtitle media handler treats all samples as forced + * subtitles, regardless of the presence or absence of a 'frcd' atom. + * If this flag is set, the Some Samples Are Forced flag must also be set + * (making 0xC0000000). + */ +#define DISPLAY_FLAG_ALL_SAMPLES_FORCED 0x80000000 + typedef struct { uint16_t style_start; uint16_t style_end; @@ -183,6 +217,7 @@ static int encode_sample_description(AVCodecContext *avctx) int font_names_total_len = 0; MovTextContext *s = avctx->priv_data; uint8_t buf[30], *p = buf; + uint32_t display_flags = 0; // 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags // 0x01, // int8_t horizontal-justification @@ -241,7 +276,11 @@ static int encode_sample_description(AVCodecContext *avctx) (255 - ((uint32_t)style->back_color >> 24)); } - bytestream_put_be32(&p, 0); // displayFlags + if (avctx->subtitle_flags & AV_SUBTITLE_FLAG_FORCED) + display_flags = DISPLAY_FLAG_SOME_SAMPLES_FORCED | + DISPLAY_FLAG_ALL_SAMPLES_FORCED; + + bytestream_put_be32(&p, display_flags); // displayFlags bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t) bytestream_put_be32(&p, back_color); bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right