From patchwork Sat May 21 16:09:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: TADANO Tokumei X-Patchwork-Id: 35883 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:9992:b0:82:461d:f3b with SMTP id ve18csp1126609pzb; Sat, 21 May 2022 09:11:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzx6xtvLAmTSK/ADluXO/fGxCpPgKlsrZZPRlmh38T6ULf2hmNUd2e8zr8rZK8oByDF4ZC8 X-Received: by 2002:a05:6402:190a:b0:42b:60b:592b with SMTP id e10-20020a056402190a00b0042b060b592bmr13107451edz.127.1653149514234; Sat, 21 May 2022 09:11:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653149514; cv=none; d=google.com; s=arc-20160816; b=Vq9gKG3U+/d05Axwfj0VyY6FsFy+UThCSoLadr+9sh2rR+66X507nK7iRbqnAGS7d9 O6ISeZe5MXur0Rnk7J9g8jnA+woZuyen8VYgV2A8bm1/ZmyQqxrUXj8HeRltmYFospDo V2YHnafQxmBnhvHRTLLXdowHADxKqeFSyy/uSTK4spB/r/qbwfSY1rY1YoJc/mFQVfI9 hhmsiTGsPFCCn0ySCepF00cMj6bBJLjKNmlpssUn8zi+Y2zAfwE2IG+USkJnHQjDec3C GN9d9zU/EsWZRiB0MiUoz8b7E2g6hqzKluab8nB21Ws7U6hM6bbQmaHLcyyzttDlBcPE Vaug== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=czl8LfJvwdIfhj087UsEh5p7UdlhLfqkjFC+pg7Co6I=; b=TbnaqHYP7lmC+n2N5krazAlbHBkWH8Y2i/wUw15Sl3RIMdfqUSt4FbBAnpxcNEVGB7 FFKSqHXkM0LLILrkyG78d3Gr0c0DbJ3MoGY/9LY49Dru9xUFH9++T8brRgfOyNQ3eMU/ zf8KK+8MBpHasow0hCZpzI9ROEsDw0IC5Am+ZgljeygJYD0+XGHBAeW2zumislTL5Bx0 nei+IpL/nCFa7XPX6vnhSngHl3iNXypF6XTZkKDoymMjue4yttNEIdPsbBiMSeCEIKSv Sx8rtCBGBoUBvtymFEn2nywnOSlN/8qPDmZdhP+nxqt0mJOji9zwU+nUK/EusZZ9eo9A Ap+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=ajubNzbc; 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 di7-20020a170906730700b006fe02712b76si13353757ejc.607.2022.05.21.09.11.53; Sat, 21 May 2022 09:11:54 -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=@nifty.com header.s=dec2015msa header.b=ajubNzbc; 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 C2BDC68B4E4; Sat, 21 May 2022 19:11:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from conssluserg-06.nifty.com (conssluserg-06.nifty.com [210.131.2.91]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7E92B68B42E for ; Sat, 21 May 2022 19:11:01 +0300 (EEST) Received: from osamu-pc.kimura.local (M106073025034.v4.enabler.ne.jp [106.73.25.34]) (authenticated) by conssluserg-06.nifty.com with ESMTP id 24LGAf7W026265; Sun, 22 May 2022 01:10:41 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-06.nifty.com 24LGAf7W026265 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1653149442; bh=0VjaQOJ8xot/YwHpQ5kdFnwYQtnoVusukdQWsmBj0wE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ajubNzbcDFIxOQDK02nXLoGYB+WtNjFow1pdHVUbEXenpEPObnV4iOO56kQLFBdDV ZPDoj0oF1lVsr9NtuNsau4j1l3+oYhaLxKVJXXAQdzYVv9vXlOYf6DLLv9yF33PHm7 QpvBRKIl6EZ1JKlfvbLZ20E6Wo9rSAHBXMdamVaAOCHGbiF2KNdWsNSpCWD88NLPpB 08aRr7HaZQd68b/jueEppD9yZAtc6oabqhF+kzZP9DZ3vPVGVS8iDTQ3UzEk6TdtPV b9SYvw+QvT/bSKCqo8JHUX1V6FEgZrdDDUhUEMkZG4S+0HsGLIUChTg+MmpEg+iniU /V3ZvJ4t4/w1A== X-Nifty-SrcIP: [106.73.25.34] From: TADANO Tokumei To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 May 2022 01:09:41 +0900 Message-Id: <20220521160943.582994-2-aimingoff@pc.nifty.jp> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220521160943.582994-1-aimingoff@pc.nifty.jp> References: <20220521160943.582994-1-aimingoff@pc.nifty.jp> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/4] lavc/libaribcaption.c: add ARIB caption decoder using libaribcaption 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: TADANO Tokumei Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Bbjj81/C77hx This patch add another ARIB caption decoder using libaribcaption external library: https://github.com/xqq/libaribcaption Unlike libaribb24, it supports 3 types of subtitle outputs: * text: plain text * ass: ASS formatted text * bitmap: bitmap image Default subtitle type is ass as same as libaribb24. Some present players (e.g., mpv) expect ASS format for ARIB caption. Subtitle type can be specified by `-sub_type` option. For example, MPEG-TS file with ARIB caption can be displayed by ffplay tool like: ffplay -sub_type bitmap MPEG.TS Since some players (e.g., mpv) can't handle multi-rectangle ASS subtitle properly, default behavior is all the texts are displayed in a single-rectangle at a time. It causes some texts may not be displayed at expected position as described in ARIB STD-B24. If a player can handle multi-rectangle ASS subtitle and you prefer more suitable positioning, set `-ass_workaround false` option or define `ASS_WORKAROUND=0` at compilation. Signed-off-by: TADANO Tokumei --- libavcodec/allcodecs.c | 1 + libavcodec/libaribcaption.c | 1097 +++++++++++++++++++++++++++++++++++ 2 files changed, 1098 insertions(+) create mode 100644 libavcodec/libaribcaption.c diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index c47133aa18..acd83fe762 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -737,6 +737,7 @@ extern const FFCodec ff_pcm_mulaw_at_decoder; extern const FFCodec ff_qdmc_at_decoder; extern const FFCodec ff_qdm2_at_decoder; extern FFCodec ff_libaom_av1_encoder; +extern const FFCodec ff_aribcaption_decoder; extern const FFCodec ff_libaribb24_decoder; extern const FFCodec ff_libcelt_decoder; extern const FFCodec ff_libcodec2_encoder; diff --git a/libavcodec/libaribcaption.c b/libavcodec/libaribcaption.c new file mode 100644 index 0000000000..1706503dce --- /dev/null +++ b/libavcodec/libaribcaption.c @@ -0,0 +1,1097 @@ +/* + * ARIB STD-B24 caption decoder using the libaribcaption library + * Copyright (c) 2022 TADANO Tokumei + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "codec_internal.h" +#include "internal.h" +#include "libavcodec/ass.h" +#include "libavutil/avstring.h" +#include "libavutil/avutil.h" +#include "libavutil/thread.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" + +#include + +#define ARIBC_BPRINT_SIZE_INIT 64 +#define ARIBC_BPRINT_SIZE_MAX (8 * 1024) +#define ARIBC_ALPHA_MAX_NUM 4 +#define ARIBC_ALPHA_DEFAULT_FRONT 0xFF +#define ARIBC_ALPHA_DEFAULT_BACK 0x80 + +#define ARIBCC_COLOR_RGB(c) ((c) & 0xFFFFFF) +#define ARIBCC_COLOR_DIFF_RGB(c1,c2) (((c1) ^ (c2)) & 0x00FFFFFF) +#define ARIBCC_COLOR_DIFF_A(c1,c2) (((c1) ^ (c2)) & 0xFF000000) + +#define CLUT_RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) +#define CLUT_A(c) (((c) >> 24) & 0xFF) +#define CLUT_R(c) (((c) >> 16) & 0xFF) +#define CLUT_G(c) (((c) >> 8) & 0xFF) +#define CLUT_B(c) ( (c) & 0xFF) + +#define ARIBCC_COLOR_TO_CLUT_RGBA(c,a) (((ARIBCC_COLOR_A(c) ? ARIBCC_COLOR_A(c) : (a)) << 24) | \ + (ARIBCC_COLOR_R(c) << 16) | \ + (ARIBCC_COLOR_G(c) << 8) | \ + (ARIBCC_COLOR_B(c))) + +typedef struct ARIBCaptionContext { + AVClass *class; + AVCodecContext *avctx; + const AVPacket *avpkt; + AVSubtitle *sub; + + aribcc_context_t *context; + aribcc_decoder_t *decoder; + aribcc_renderer_t *renderer; + + enum AVSubtitleType subtitle_type; + bool ass_workaround; + char *font_name; + bool replace_fullwidth_ascii; + bool force_stroke_text; + bool ignore_background; + bool ignore_ruby; + bool fadeout; + float stroke_width; + bool replace_drcs; + aribcc_textrenderer_type_t rendering_backend; + + int64_t pts; + AVRational time_base; + int plane_width; + int plane_height; + int frame_width; + int frame_height; + int font_size; + int charstyle; + int border_style; + int readorder; + + aribcc_caption_t caption; + aribcc_render_result_t render_result; + uint32_t *clut; + int clut_idx; + int clut_overflow; + uint8_t clut_alpha[ARIBC_ALPHA_MAX_NUM]; +} ARIBCaptionContext; + +static void hex_dump_debug(void *ctx, const char *buf, int buf_size) +{ + int i; + + for (i=0; i < buf_size; i++) { + ff_dlog(ctx, "%02hhx ", buf[i]); + if (i % 16 == 15) + ff_dlog(ctx, "\n"); + } + if (i % 16) + ff_dlog(ctx, "\n"); +} + +static void logcat_callback(aribcc_loglevel_t level, const char* message, void* userdata) +{ + ARIBCaptionContext *ctx = userdata; + int lvl; + + if (ctx->decoder != NULL) { + switch (level) { + case ARIBCC_LOGLEVEL_ERROR: + lvl = AV_LOG_ERROR; + break; + case ARIBCC_LOGLEVEL_WARNING: + lvl = AV_LOG_WARNING; + break; + default: + lvl = AV_LOG_INFO; + } + + av_log(ctx, lvl, "%s\n", message); + } +} + +static void estimate_video_frame_size(ARIBCaptionContext *ctx) +{ + if (ctx->avctx->width > 0 && ctx->avctx->height > 0) { + ctx->frame_width = ctx->avctx->width; + ctx->frame_height = ctx->avctx->height; + } else if (ctx->plane_width == 960) { + /* ARIB TR-B14 Fascicle 2 Volume 3 [Section 2] 4.3.1 */ + /* ARIB TR-B14 Fascicle 2 Volume 3 [Section 2] Appendix-4 */ + ctx->frame_width = 1440; + ctx->frame_height = 1080; + } else { + ctx->frame_width = ctx->plane_width; + ctx->frame_height = ctx->plane_height; + } +} + +static void clut_set_alpha(ARIBCaptionContext *ctx, uint8_t a) +{ + int i; + + for (i = 0; i < ARIBC_ALPHA_MAX_NUM; i++) { + if (ctx->clut_alpha[i] == 0) { + ctx->clut_alpha[i] = a; + return; + } + if (ctx->clut_alpha[i] == a) + return; + } + return; +} + +static uint8_t clut_find_nearlest_alpha(ARIBCaptionContext *ctx, uint8_t a) +{ + int i, j, d; + + if (a == 0) + return a; + d = 256; + j = 0; + for (i = 0; i < ARIBC_ALPHA_MAX_NUM; i++) { + if (ctx->clut_alpha[i] == a) + return a; + if (ctx->clut_alpha[i] == 0) + break; + if (abs((int)a - (int)ctx->clut_alpha[i]) < d) { + d = abs((int)a - (int)ctx->clut_alpha[i]); + j = i; + } + } + return ctx->clut_alpha[j]; +} + +static int clut_find(ARIBCaptionContext *ctx, uint32_t rgba) +{ + int i; + + for (i = 0; i < ctx->clut_idx; i++) { + if (ctx->clut[i] == rgba) + return i; + } + return -1; +} + +static inline int clut_color_distance(uint32_t rgba1, uint32_t rgba2) +{ + return abs((int)CLUT_R(rgba1) - (int)CLUT_R(rgba2)) + + abs((int)CLUT_G(rgba1) - (int)CLUT_G(rgba2)) + + abs((int)CLUT_B(rgba1) - (int)CLUT_B(rgba2)); +} + +static uint8_t clut_pick_or_set(ARIBCaptionContext *ctx, int r, int g, int b, int a) +{ + int c, i, d, d_min; + uint32_t rgba; + + a = clut_find_nearlest_alpha(ctx, a); + if (a == 0) + return 0; /* transparent */ + rgba = CLUT_RGBA(r,g,b,a); + + d_min = 256 * 3; + c = 0; + for (i = 0; i < ctx->clut_idx; i++) { + if (ctx->clut[i] == rgba) + return i; + if (CLUT_A(ctx->clut[i]) != a) + continue; + d = clut_color_distance(ctx->clut[i], rgba); + if (d < d_min) { + d_min = d; + c = i; + } + } + if (d_min > 3) { + if (ctx->clut_idx >= AVPALETTE_COUNT) + ctx->clut_overflow++; + else { + c = ctx->clut_idx; + ctx->clut[ctx->clut_idx++] = rgba; + } + } + return c; +} + +/* initialiaze CLUT with each character colors */ +static void clut_init(ARIBCaptionContext *ctx, aribcc_caption_region_t *region) +{ + aribcc_color_t text_color, back_color, stroke_color; + uint32_t rgba; + + ctx->clut[0] = CLUT_RGBA(0,0,0,0); /* transparent */ + ctx->clut_alpha[0] = 0xFF; + ctx->clut_idx = 1; + ctx->clut_overflow = 0; + text_color = region->chars[0].text_color; + back_color = region->chars[0].back_color; + stroke_color = region->chars[0].stroke_color; + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(text_color, ARIBC_ALPHA_DEFAULT_FRONT); + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(back_color, ARIBC_ALPHA_DEFAULT_BACK); + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(stroke_color, ARIBC_ALPHA_DEFAULT_FRONT); + if (clut_find(ctx, rgba) < 0) { + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + } + + for (int i = 1; i < region->char_count; i++) { + if (region->chars[i].text_color != text_color) { + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(region->chars[i].text_color, + ARIBC_ALPHA_DEFAULT_FRONT); + if (clut_find(ctx, rgba) < 0) { + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + } + } + if (region->chars[i].back_color != back_color) { + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(region->chars[i].back_color, + ARIBC_ALPHA_DEFAULT_BACK); + if (clut_find(ctx, rgba) < 0) { + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + } + } + if (region->chars[i].stroke_color != stroke_color) { + rgba = ARIBCC_COLOR_TO_CLUT_RGBA(region->chars[i].stroke_color, + ARIBC_ALPHA_DEFAULT_FRONT); + if (clut_find(ctx, rgba) < 0) { + ctx->clut[ctx->clut_idx++] = rgba; + clut_set_alpha(ctx, CLUT_A(rgba)); + } + } + } +} + +/** + * aribcaption_trans_{bitmap|ass|text}_subtitle() + * + * Transfer decoded subtitle to AVSubtitle with corresponding subtitle type. + * + * @param ctx pointer to the ARIBCaptionContext + * @return > 0 number of rectangles to be displayed + * = 0 no subtitle + * < 0 error code + */ +static int aribcaption_trans_bitmap_subtitle(ARIBCaptionContext *ctx) +{ + int ret = 0; + AVSubtitle *sub = ctx->sub; + int status, rect_idx; + int old_width = ctx->frame_width; + int old_height = ctx->frame_height; + + if (ctx->caption.plane_width > 0 && ctx->caption.plane_height > 0) { + ctx->plane_width = ctx->caption.plane_width; + ctx->plane_height = ctx->caption.plane_height; + } + estimate_video_frame_size(ctx); + if (ctx->frame_width != old_width || ctx->frame_height != old_height) { + if (!aribcc_renderer_set_frame_size(ctx->renderer, + ctx->avctx->width, ctx->avctx->height)) { + av_log(ctx, AV_LOG_ERROR, + "aribcc_renderer_set_frame_size() returned with error.\n"); + return AVERROR_EXTERNAL; + } + } + + status = aribcc_renderer_append_caption(ctx->renderer, &ctx->caption); + if (!status) { + av_log(ctx, AV_LOG_ERROR, + "aribcc_renderer_append_caption() returned with error.\n"); + return AVERROR_EXTERNAL; + } + + status = aribcc_renderer_render(ctx->renderer, ctx->pts, &ctx->render_result); + switch (status) { + case ARIBCC_RENDER_STATUS_GOT_IMAGE: + break; + + case ARIBCC_RENDER_STATUS_GOT_IMAGE_UNCHANGED: + aribcc_render_result_cleanup(&ctx->render_result); + ff_dlog(ctx, "got image unchanged\n"); + return 0; + + case ARIBCC_RENDER_STATUS_NO_IMAGE: + ff_dlog(ctx, "no image\n"); + return 0; + + case ARIBCC_RENDER_STATUS_ERROR: + av_log(ctx, AV_LOG_ERROR, + "aribcc_renderer_render() returned with error.\n"); + return AVERROR_EXTERNAL; + + default: + aribcc_render_result_cleanup(&ctx->render_result); + av_log(ctx, AV_LOG_ERROR, + "aribcc_renderer_render() returned unknown status: %d\n", status); + return AVERROR_EXTERNAL; + } + + if (!ctx->render_result.image_count || ctx->render_result.images == NULL) { + aribcc_render_result_cleanup(&ctx->render_result); + ff_dlog(ctx, "no image (%d)\n", ctx->render_result.image_count); + return 0; + } + + sub->format = 0; /* graphic */ + sub->rects = av_calloc(ctx->render_result.image_count, sizeof(*sub->rects)); + if (!sub->rects) { + ret = AVERROR(ENOMEM); + goto fail; + } + for (int i = 0; i < ctx->render_result.image_count; i++) { + sub->rects[i] = av_mallocz(sizeof(*sub->rects[i])); + if (!sub->rects[i]) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + + for (rect_idx = 0; rect_idx < ctx->caption.region_count; rect_idx++) { + AVSubtitleRect *rect = ctx->sub->rects[rect_idx]; + aribcc_image_t *image = &ctx->render_result.images[rect_idx]; + int w, h, src_idx, dst_idx; + + clut_init(ctx, &ctx->caption.regions[rect_idx]); + + rect->data[0] = av_mallocz(image->bitmap_size / 4); + if (!rect->data[0]) { + ret = AVERROR(ENOMEM); + goto fail; + } + + src_idx = 0; + dst_idx = 0; + for (h = 0; h < image->height; h++) { + for (w = 0; w < image->width; w++) { + int r, g, b, a; + r = image->bitmap[src_idx++]; + g = image->bitmap[src_idx++]; + b = image->bitmap[src_idx++]; + a = image->bitmap[src_idx++]; + if (src_idx > image->bitmap_size) { + av_log(ctx, AV_LOG_ERROR, "Bug: unexpectedly reach end of image\n"); + ret = AVERROR_EXTERNAL; + goto fail; + } + rect->data[0][dst_idx++] = clut_pick_or_set(ctx, r, g, b, a); + } + if (w * 4 < image->stride) { + src_idx += image->stride - w * 4; + dst_idx += image->stride / 4 - w; + } + } + rect->data[1] = av_memdup(ctx->clut, AVPALETTE_SIZE); + if (!rect->data[1]) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ff_dlog(ctx, "BITMAP subtitle%s (%d,%d) %d(%d) x %d [%d]: %d colors\n", + (ctx->caption.regions[rect_idx].is_ruby) ? " (ruby)" : "", + image->dst_x, image->dst_y, + image->width, image->stride / 4, image->height, + rect_idx, ctx->clut_idx); + if (ctx->clut_overflow) + av_log(ctx, AV_LOG_WARNING, "CLUT overflow (%d).\n", ctx->clut_overflow); + + rect->x = image->dst_x; + rect->y = image->dst_y; + rect->w = image->width; + rect->h = image->height; + rect->type = SUBTITLE_BITMAP; + rect->linesize[0] = image->stride / 4; + rect->nb_colors = 256; + } + sub->num_rects = rect_idx; + + return rect_idx; + +fail: + if (sub->rects) { + for (int i = 0; i < ctx->caption.region_count; i++) { + if (sub->rects[i]) { + av_freep(&sub->rects[i]->data[0]); + av_freep(&sub->rects[i]->data[1]); + av_freep(&sub->rects[i]); + } + } + av_freep(&sub->rects); + } + sub->num_rects = 0; + + return ret; +} + +static int set_ass_header(ARIBCaptionContext *ctx) +{ + AVCodecContext *avctx = ctx->avctx; + int outline, shadow; + + if (ctx->border_style == 4) { + outline = 0; + shadow = 4; + } else { + outline = 1; + shadow = 0; + } + if (ctx->force_stroke_text) + outline = (int)(ctx->stroke_width * 4.0 / 3.0); + + if (avctx->subtitle_header) + av_freep(&avctx->subtitle_header); + avctx->subtitle_header = av_asprintf( + "[Script Info]\r\n" + "ScriptType: v4.00+\r\n" + "PlayResX: %d\r\n" + "PlayResY: %d\r\n" + "WrapStyle: 2\r\n" /* 2: no word wrapping */ + "\r\n" + + "[V4+ Styles]\r\n" + "Format: Name, " + "Fontname, Fontsize, " + "PrimaryColour, SecondaryColour, OutlineColour, BackColour, " + "Bold, Italic, Underline, StrikeOut, " + "ScaleX, ScaleY, " + "Spacing, Angle, " + "BorderStyle, Outline, Shadow, " + "Alignment, MarginL, MarginR, MarginV, " + "Encoding\r\n" + + "Style: " + "Default," /* Name */ + "%s,%d," /* Font{name,size} */ + "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */ + "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */ + "100,100," /* Scale{X,Y} */ + "0,0," /* Spacing, Angle */ + "%d,%d,%d," /* BorderStyle, Outline, Shadow */ + "%d,10,10,10," /* Alignment, Margin[LRV] */ + "0\r\n" /* Encoding */ + "\r\n" + + "[Events]\r\n" + "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n", + ctx->plane_width, ctx->plane_height, + ctx->font_name, ctx->font_size, + ASS_DEFAULT_COLOR, ASS_DEFAULT_COLOR, + ASS_DEFAULT_BACK_COLOR, ASS_DEFAULT_BACK_COLOR, + -ASS_DEFAULT_BOLD, -ASS_DEFAULT_ITALIC, -ASS_DEFAULT_UNDERLINE, + ctx->border_style, outline, shadow, ASS_DEFAULT_ALIGNMENT); + + if (!avctx->subtitle_header) + return AVERROR(ENOMEM); + avctx->subtitle_header_size = strlen(avctx->subtitle_header); + return 0; +} + +static void set_ass_color(AVBPrint *buf, int color_num, + aribcc_color_t new_color, aribcc_color_t old_color) +{ + if (ARIBCC_COLOR_DIFF_RGB(new_color, old_color)) + av_bprintf(buf, "{\\%dc&H%06x&}", color_num, + ARIBCC_COLOR_RGB(new_color)); + if (ARIBCC_COLOR_DIFF_A(new_color, old_color) && ARIBCC_COLOR_A(new_color)) + av_bprintf(buf, "{\\%da&H%02x&}", color_num, + ARIBCC_COLOR_A(new_color)); +} + +static int aribcaption_trans_ass_subtitle(ARIBCaptionContext *ctx) +{ + AVSubtitle *sub = ctx->sub; + AVBPrint buf; + int ret = 0, rect_idx; + + if (ctx->caption.plane_width > 0 && ctx->caption.plane_height > 0 && + (ctx->caption.plane_width != ctx->plane_width || + ctx->caption.plane_height != ctx->plane_height)) { + if ((ret = set_ass_header(ctx)) < 0) + return ret; + ctx->plane_width = ctx->caption.plane_width; + ctx->plane_height = ctx->caption.plane_height; + } + + sub->format = 1; /* text */ + if (ctx->caption.region_count == 0) { + /* clear previous caption for indefinite duration */ + ff_ass_add_rect(sub, "{\\r}", ctx->readorder++, 0, NULL, NULL); + return 1; + } + + av_bprint_init(&buf, ARIBC_BPRINT_SIZE_INIT, ARIBC_BPRINT_SIZE_MAX); + + if (ctx->ass_workaround) { + int x, y; + x = ctx->plane_width; + y = ctx->plane_height; + for (int i = 0; i < ctx->caption.region_count; i++) { + if (ctx->caption.regions[i].x < x) + x = ctx->caption.regions[i].x; + if (ctx->caption.regions[i].y < y) + y = ctx->caption.regions[i].y; + } + av_bprintf(&buf, "{\\an4}"); + if (x > 0 || y >0) + av_bprintf(&buf, "{\\pos(%d,%d)}", x, y); + } + + rect_idx = 0; + for (int i = 0; i < ctx->caption.region_count; i++) { + aribcc_caption_region_t *region = &ctx->caption.regions[i]; + aribcc_color_t text_color = ARIBCC_MAKE_RGBA(0xFF, 0xFF, 0xFF, + ARIBC_ALPHA_DEFAULT_FRONT); + aribcc_color_t stroke_color = ARIBCC_MAKE_RGBA(0, 0, 0, + ARIBC_ALPHA_DEFAULT_FRONT); + aribcc_color_t back_color = ARIBCC_MAKE_RGBA(0, 0, 0, + ARIBC_ALPHA_DEFAULT_BACK); + aribcc_charstyle_t charstyle = ctx->charstyle; + int char_width = ctx->font_size; + int char_height = ctx->font_size; + int char_horizontal_spacing = 0; + + if (region->is_ruby && ctx->ignore_ruby) + continue; + + if (!ctx->ass_workaround) { + av_bprint_clear(&buf); + av_bprintf(&buf, "{\\an4}"); + if (region->x != 0 || region->y != 0) + av_bprintf(&buf, "{\\pos(%d,%d)}", region->x, region->y); + } + if (ctx->fadeout) + av_bprintf(&buf, "{\\fad(0,500)}"); + if (region->is_ruby) + av_bprintf(&buf, "{\\fs%d}", char_height / 2); + + for (int j = 0; j < region->char_count; j++) { + aribcc_caption_char_t *ch = ®ion->chars[j]; + + if (ch->char_horizontal_spacing != char_horizontal_spacing) { + av_bprintf(&buf, "{\\fsp%d}", (region->is_ruby) ? + ch->char_horizontal_spacing / 2 : + ch->char_horizontal_spacing); + char_horizontal_spacing = ch->char_horizontal_spacing; + } + if (ch->char_width != char_width) { + av_bprintf(&buf, "{\\fscx%"PRId64"}", + av_rescale(ch->char_width, 100, ctx->font_size)); + char_width = ch->char_width; + } + if (ch->char_height != char_height) { + av_bprintf(&buf, "{\\fscy%"PRId64"}", + av_rescale(ch->char_height, 100, ctx->font_size)); + char_height = ch->char_height; + } + if (ch->style != charstyle) { + aribcc_charstyle_t diff = ch->style ^ charstyle; + if (diff & ARIBCC_CHARSTYLE_STROKE) { + if (charstyle & ARIBCC_CHARSTYLE_STROKE) { + if (ctx->force_stroke_text) + av_bprintf(&buf, "{\\bord%d}", + (int)(ctx->stroke_width * 4.0 / 3.0)); + else + av_bprintf(&buf, "{\\bord0}"); + } else + av_bprintf(&buf, "{\\bord3}"); + } + if (diff & ARIBCC_CHARSTYLE_BOLD) { + if (charstyle & ARIBCC_CHARSTYLE_BOLD) + av_bprintf(&buf, "{\\b0}"); + else + av_bprintf(&buf, "{\\b1}"); + } + if (diff & ARIBCC_CHARSTYLE_ITALIC) { + if (charstyle & ARIBCC_CHARSTYLE_ITALIC) + av_bprintf(&buf, "{\\i0}"); + else + av_bprintf(&buf, "{\\i1}"); + } + if (diff & ARIBCC_CHARSTYLE_UNDERLINE) { + if (charstyle & ARIBCC_CHARSTYLE_UNDERLINE) + av_bprintf(&buf, "{\\u0}"); + else + av_bprintf(&buf, "{\\u1}"); + } + charstyle = ch->style; + } + if (ch->text_color != text_color) { + set_ass_color(&buf, 1, ch->text_color, text_color); + text_color = ch->text_color; + } + if (ch->stroke_color != stroke_color) { + set_ass_color(&buf, 3, ch->stroke_color, stroke_color); + stroke_color = ch->stroke_color; + } + if (ch->back_color != back_color) { + if (ctx->border_style == 4) + set_ass_color(&buf, 4, ch->back_color, back_color); + else + set_ass_color(&buf, 3, ch->back_color, back_color); + back_color = ch->back_color; + } + if (region->chars[j].type == ARIBCC_CHARTYPE_DRCS) + av_bprintf(&buf, "\xe3\x80\x93"); /* "〓" */ + else + ff_ass_bprint_text_event(&buf, ch->u8str, strlen(ch->u8str), "", 0); + } + av_bprintf(&buf, "{\\r}%s", + (i + 1 < ctx->caption.region_count) ? "\\N" : ""); + + if (ctx->ass_workaround) { + ff_dlog(ctx, "ASS subtitle%s (%d,%d) %dx%d [%d]\n", + (region->is_ruby) ? " (ruby)" : "", + region->x, region->y, region->width, region->height, + rect_idx); + } else { + if (!av_bprint_is_complete(&buf)) { + ret = AVERROR(ENOMEM); + goto fail; + } + ff_dlog(ctx, "ASS subtitle%s (%d,%d) %dx%d [%d]: %s\n", + (region->is_ruby) ? " (ruby)" : "", + region->x, region->y, region->width, region->height, + rect_idx, buf.str); + + ret = ff_ass_add_rect(sub, buf.str, ctx->readorder++, 0 , NULL, NULL); + if (ret != 0) + goto fail; + rect_idx++; + } + } + if (ctx->ass_workaround) { + if (!av_bprint_is_complete(&buf)) { + ret = AVERROR(ENOMEM); + goto fail; + } + ff_dlog(ctx, "ASS subtitle: %s\n", buf.str); + + ret = ff_ass_add_rect(sub, buf.str, ctx->readorder++, 0 , NULL, NULL); + if (ret != 0) + goto fail; + rect_idx++; + } + + av_bprint_finalize(&buf, NULL); + return rect_idx; + +fail: + if (sub->rects) { + for (int i = 0; i < ctx->caption.region_count; i++) { + if (sub->rects[i]) { + av_freep(&sub->rects[i]->ass); + av_freep(&sub->rects[i]); + } + } + av_freep(&sub->rects); + } + sub->num_rects = 0; + av_bprint_finalize(&buf, NULL); + + return ret; +} + +static int aribcaption_trans_text_subtitle(ARIBCaptionContext *ctx) +{ + AVSubtitle *sub = ctx->sub; + AVSubtitleRect *rect; + int ret = 0; + const char *text; + + sub->rects = av_calloc(ctx->caption.region_count, sizeof(*sub->rects)); + if (!sub->rects) { + ret = AVERROR(ENOMEM); + goto fail; + } + sub->num_rects = 1; + + sub->rects[0] = av_mallocz(sizeof(*sub->rects[0])); + if (!sub->rects[0]) { + ret = AVERROR(ENOMEM); + goto fail; + } + rect = sub->rects[0]; + + if (ctx->caption.region_count == 0) { + text = ""; /* clear previous caption */ + } else { + text = ctx->caption.text; + ff_dlog(ctx, "TEXT subtitle: %s\n", text); + } + rect->text = av_strdup(text); + if (!rect->text) { + ret = AVERROR(ENOMEM); + goto fail; + } + + sub->format = 1; /* text */ + rect->type = SUBTITLE_TEXT; + + return 1; + +fail: + if (sub->rects) { + rect = sub->rects[0]; + if (rect) { + av_freep(&rect->text); + av_freep(&rect); + } + av_freep(&sub->rects); + } + sub->num_rects = 0; + + return ret; +} + +static int aribcaption_decode(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, const AVPacket *avpkt) +{ + ARIBCaptionContext *ctx = avctx->priv_data; + int status; + + ff_dlog(ctx, "ARIB caption packet pts=%"PRIx64":\n", avpkt->pts); + if (sub->num_rects) { + avpriv_request_sample(ctx, "Different Version of Segment asked Twice"); + return AVERROR_PATCHWELCOME; + } + hex_dump_debug(ctx, avpkt->data, avpkt->size); + + ctx->sub = sub; + ctx->avpkt = avpkt; + ctx->time_base = avctx->pkt_timebase; + if (ctx->time_base.num <= 0 || ctx->time_base.den <= 0) { + av_log(ctx, AV_LOG_VERBOSE, "No timebase set. assuming 90kHz.\n"); + ctx->time_base = av_make_q(1, 90000); + } + if (avpkt->pts == AV_NOPTS_VALUE) + ctx->pts = ARIBCC_PTS_NOPTS; + else + ctx->pts = av_rescale_q(avpkt->pts, ctx->time_base, (AVRational){1, 1000}); + + status = aribcc_decoder_decode(ctx->decoder, avpkt->data, avpkt->size, + ctx->pts, &ctx->caption); + if (status == ARIBCC_DECODE_STATUS_ERROR) { + av_log(ctx, AV_LOG_ERROR, + "aribcc_decoder_decode() returned with error.\n"); + return AVERROR(EAGAIN); + } + if (status == ARIBCC_DECODE_STATUS_NO_CAPTION) { + ff_dlog(ctx, "No caption.\n"); + return avpkt->size; + } else { + ff_dlog(ctx, "type=%02x, flags=%x, lang=%03x\n", + ctx->caption.type, ctx->caption.flags, ctx->caption.iso6392_language_code); + ff_dlog(ctx, "region count = %d, start=%d.%d, duration=%d.%d\n", + ctx->caption.region_count, + (int)(ctx->caption.pts / 1000), (int)(ctx->caption.pts % 1000), + (int)((ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE) ? + -1 : ctx->caption.wait_duration / 1000), + (int)((ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE) ? + 0 : ctx->caption.wait_duration % 1000)); + } + + switch (ctx->subtitle_type) { + case SUBTITLE_TEXT: + status = aribcaption_trans_text_subtitle(ctx); + break; + + case SUBTITLE_ASS: + status = aribcaption_trans_ass_subtitle(ctx); + break; + + case SUBTITLE_BITMAP: + status = aribcaption_trans_bitmap_subtitle(ctx); + break; + + case SUBTITLE_NONE: + default: + status = 0; + } + + if (status < 0) { + av_log(ctx, AV_LOG_ERROR, "Failed to set Subtitle: %s\n", + av_err2str(status)); + aribcc_caption_cleanup(&ctx->caption); + return status; + } + if (status > 0) { + *got_sub_ptr = 1; + if (ctx->avpkt->pts != AV_NOPTS_VALUE) + sub->pts = av_rescale_q(ctx->avpkt->pts, + ctx->time_base, AV_TIME_BASE_Q); + if (ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE) + sub->end_display_time = UINT32_MAX; + else + sub->end_display_time = (uint32_t)ctx->caption.wait_duration; + } + + aribcc_caption_cleanup(&ctx->caption); + return avpkt->size; +} + +static void aribcaption_flush(AVCodecContext *avctx) +{ + ARIBCaptionContext *ctx = avctx->priv_data; + + if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP)) { + if (ctx->decoder) + aribcc_decoder_flush(ctx->decoder); + if (ctx->renderer) + aribcc_renderer_flush(ctx->renderer); + ctx->readorder = 0; + } +} + +static int aribcaption_close(AVCodecContext *avctx) +{ + ARIBCaptionContext *ctx = avctx->priv_data; + + aribcaption_flush(avctx); + + if (ctx->clut) + av_freep(&ctx->clut); + if (ctx->renderer) { + aribcc_renderer_free(ctx->renderer); + ctx->decoder = NULL; + } + if (ctx->decoder) { + aribcc_decoder_free(ctx->decoder); + ctx->decoder = NULL; + } + if (ctx->context) { + aribcc_context_free(ctx->context); + ctx->context = NULL; + } + + return 0; +} + +static int aribcaption_init(AVCodecContext *avctx) +{ + ARIBCaptionContext *ctx = avctx->priv_data; + aribcc_profile_t profile; + aribcc_encoding_scheme_t encode_scheme; + int ret = 0; + + ctx->avctx = avctx; + + switch (avctx->profile) { + case FF_PROFILE_ARIB_PROFILE_A: + profile = ARIBCC_PROFILE_A; + /* assume 960x540 at initial state */ + ctx->plane_width = 960; + ctx->plane_height = 540; + ctx->font_size = 36; + break; + case FF_PROFILE_ARIB_PROFILE_C: + profile = ARIBCC_PROFILE_C; + ctx->plane_width = 320; + ctx->plane_height = 180; + ctx->font_size = 16; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Unknown or unsupported profile set.\n"); + return AVERROR(EINVAL); + } + /* determine BorderStyle of ASS header */ + if (ctx->ignore_background) + ctx->border_style = 1; + else + ctx->border_style = 4; + ctx->charstyle = ARIBCC_CHARSTYLE_DEFAULT; + if (ctx->force_stroke_text || ctx->ignore_background) + ctx->charstyle |= ARIBCC_CHARSTYLE_STROKE; + + encode_scheme = ARIBCC_ENCODING_SCHEME_AUTO; + if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_DO_NOTHING) + encode_scheme = ARIBCC_ENCODING_SCHEME_ARIB_STD_B24_UTF8; + + if (!(ctx->context = aribcc_context_alloc())) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption context.\n"); + aribcaption_close(avctx); + return AVERROR_EXTERNAL; + } + aribcc_context_set_logcat_callback(ctx->context, logcat_callback, avctx); + if (!(ctx->decoder = aribcc_decoder_alloc(ctx->context))) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption decoder.\n"); + aribcaption_close(avctx); + return AVERROR_EXTERNAL; + } + if (!aribcc_decoder_initialize(ctx->decoder, + encode_scheme, + ARIBCC_CAPTIONTYPE_CAPTION, + profile, + ARIBCC_LANGUAGEID_FIRST)) { + av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribcaption decoder.\n"); + aribcaption_close(avctx); + return AVERROR_EXTERNAL; + } + aribcc_decoder_set_replace_msz_fullwidth_ascii(ctx->decoder, + ctx->replace_fullwidth_ascii); + + switch (ctx->subtitle_type) { + case SUBTITLE_ASS: + ret = set_ass_header(ctx); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to set ASS header: %s\n", + av_err2str(ret)); + return ret; + } + break; + + case SUBTITLE_BITMAP: + if(!(ctx->renderer = aribcc_renderer_alloc(ctx->context))) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption renderer.\n"); + aribcaption_close(avctx); + return AVERROR_EXTERNAL; + } + if(!aribcc_renderer_initialize(ctx->renderer, + ARIBCC_CAPTIONTYPE_CAPTION, + ARIBCC_FONTPROVIDER_TYPE_AUTO, + (aribcc_textrenderer_type_t)ctx->rendering_backend)) { + av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribcaption renderer.\n"); + aribcaption_close(avctx); + return AVERROR_EXTERNAL; + } + estimate_video_frame_size(ctx); + if (!aribcc_renderer_set_frame_size(ctx->renderer, + ctx->frame_width, ctx->frame_height)) { + av_log(ctx, AV_LOG_ERROR, + "aribcc_renderer_set_frame_size() returned with error.\n"); + return AVERROR_EXTERNAL; + } + if (!(ctx->clut = av_mallocz(AVPALETTE_SIZE))) + return AVERROR(ENOMEM); + + aribcc_renderer_set_storage_policy(ctx->renderer, ARIBCC_CAPTION_STORAGE_POLICY_MINIMUM, 0); + aribcc_renderer_set_replace_drcs(ctx->renderer, ctx->replace_drcs); + aribcc_renderer_set_force_stroke_text(ctx->renderer, ctx->force_stroke_text); + aribcc_renderer_set_force_no_background(ctx->renderer, ctx->ignore_background); + aribcc_renderer_set_force_no_ruby(ctx->renderer, ctx->ignore_ruby); + aribcc_renderer_set_stroke_width(ctx->renderer, ctx->stroke_width); + if (ctx->font_name && strlen(ctx->font_name) > 0) { + const char* font_families[] = { ctx->font_name }; + aribcc_renderer_set_default_font_family(ctx->renderer, font_families, 1, true); + } + break; + + case SUBTITLE_TEXT: + case SUBTITLE_NONE: + default: + /* do nothing */ ; + } + + ctx->readorder = 0; + + return 0; +} + +#if !defined(DEFAULT_SUBTITLE_TYPE) +# define DEFAULT_SUBTITLE_TYPE SUBTITLE_ASS +#endif +#if !defined(ASS_WORKAROUND) +# define ASS_WORKAROUND 1 +#endif +#if !defined(DEFAULT_FONT_FAMILY) +# if defined(_WIN32) +# define DEFAULT_FONT_FAMILY ASS_DEFAULT_FONT +# else +# define DEFAULT_FONT_FAMILY "sans-serif" +# endif +#endif + +#define OFFSET(x) offsetof(ARIBCaptionContext, x) +#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "sub_type", "subtitle rendering type", + OFFSET(subtitle_type), AV_OPT_TYPE_INT, + { .i64 = DEFAULT_SUBTITLE_TYPE }, SUBTITLE_NONE, SUBTITLE_ASS, SD, "type" }, + { "none", "do nothing", 0, AV_OPT_TYPE_CONST, + { .i64 = SUBTITLE_NONE }, .flags = SD, .unit = "type" }, + { "bitmap", "bitmap rendering", 0, AV_OPT_TYPE_CONST, + { .i64 = SUBTITLE_BITMAP }, .flags = SD, .unit = "type" }, + { "text", "plain text", 0, AV_OPT_TYPE_CONST, + { .i64 = SUBTITLE_TEXT }, .flags = SD, .unit = "type" }, + { "ass", "formatted text", 0, AV_OPT_TYPE_CONST, + { .i64 = SUBTITLE_ASS }, .flags = SD, .unit = "type" }, + { "ass_workaround", "workaround of ASS subtitle for players which can't handle multi-rectangle (e.g., MPV) [ass]", + OFFSET(ass_workaround), AV_OPT_TYPE_BOOL, { .i64 = ASS_WORKAROUND }, 0, 1, SD }, + { "font", "font name [ass, bitmap]", + OFFSET(font_name), AV_OPT_TYPE_STRING, { .str = DEFAULT_FONT_FAMILY }, 0, 0, SD }, + { "replace_fullwidth_ascii", "replace MSZ fullwidth alphanumerics with halfwidth alphanumerics [ass, bitmap]", + OFFSET(replace_fullwidth_ascii), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD }, + { "force_outline_text", "always render characters with outline [(ass), bitmap]", + OFFSET(force_stroke_text), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD }, + { "ignore_background", "ignore rendering caption background [(ass), bitmap]", + OFFSET(ignore_background), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD }, + { "ignore_ruby", "ignore ruby-like characters [ass, bitmap]", + OFFSET(ignore_ruby), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD }, + { "fadeout", "enable Fadeout [ass, bitmap]", + OFFSET(fadeout), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD }, + { "outline_width", "outline width of text [(ass), bitmap]", + OFFSET(stroke_width), AV_OPT_TYPE_FLOAT, { .dbl = 1.5 }, 0.0, 3.0, SD }, + { "replace_drcs", "replace known DRCS [bitmap]", + OFFSET(replace_drcs), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD }, + { "rendering_backend", "text rendering backend [bitmap]", + OFFSET(rendering_backend), AV_OPT_TYPE_INT, + { .i64 = ARIBCC_TEXTRENDERER_TYPE_AUTO }, + ARIBCC_TEXTRENDERER_TYPE_AUTO, ARIBCC_TEXTRENDERER_TYPE_FREETYPE, SD, "renderer"}, + { "auto", "Detect and select TextRenderer automatically", 0, AV_OPT_TYPE_CONST, + { .i64 = ARIBCC_TEXTRENDERER_TYPE_AUTO }, .flags = SD, .unit = "renderer" }, +#ifdef ARIBCC_USE_CORETEXT + { "coretext", "Apple CoreText API based TextRenderer. Available on macOS and iOS.", 0, AV_OPT_TYPE_CONST, + { .i64 = ARIBCC_TEXTRENDERER_TYPE_CORETEXT }, .flags = SD, .unit = "renderer" }, +#endif +#ifdef ARIBCC_USE_DIRECTWRITE + { "directwrite", "DirectWrite API based TextRenderer. Available on Windows 7+.", 0, AV_OPT_TYPE_CONST, + { .i64 = ARIBCC_TEXTRENDERER_TYPE_DIRECTWRITE }, .flags = SD, .unit = "renderer" }, +#endif +#ifdef ARIBCC_USE_FREETYPE + { "freetype", "Freetype based TextRenderer. Available on all platforms.", 0, AV_OPT_TYPE_CONST, + { .i64 = ARIBCC_TEXTRENDERER_TYPE_FREETYPE }, .flags = SD, .unit = "renderer" }, +#endif + { NULL } +}; + +static const AVClass aribcaption_class = { + .class_name = "aribcaption decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const FFCodec ff_aribcaption_decoder = { + .p.name = "aribcaption", + .p.long_name = NULL_IF_CONFIG_SMALL("ARIB STD-B24 caption decoder"), + .p.type = AVMEDIA_TYPE_SUBTITLE, + .p.id = AV_CODEC_ID_ARIB_CAPTION, + .priv_data_size = sizeof(ARIBCaptionContext), + .init = aribcaption_init, + .close = aribcaption_close, + FF_CODEC_DECODE_SUB_CB(aribcaption_decode), + .flush = aribcaption_flush, + .p.priv_class= &aribcaption_class, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; From patchwork Sat May 21 16:09:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: TADANO Tokumei X-Patchwork-Id: 35882 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:9992:b0:82:461d:f3b with SMTP id ve18csp1126531pzb; Sat, 21 May 2022 09:11:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXMn9/msioYiEjaInqxD2H0T6018pOspKLzd3fO7CbZmoHyv4wCaniu2674ktYfzeIBsgv X-Received: by 2002:a17:906:4fc6:b0:6f4:da16:4a09 with SMTP id i6-20020a1709064fc600b006f4da164a09mr13137583ejw.599.1653149503666; Sat, 21 May 2022 09:11:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653149503; cv=none; d=google.com; s=arc-20160816; b=DKQtUlF6OhDx2jzbeomarso2o2BIwJpWiO0qsL43mjgPk7F4Xeh9wAvx+2xu9pxA1f 1QktJY/Ce29UPYkhYxAjp6x7BLOA0Tu85Pk5CdHOQTET1Cq+Dpp/86BiNKMlQzdRmmZw 2YzG4tHxJgVQasGoylZziS0lA+7rnTK60piwLSVT7bqL+WDMoM1l/2gkQmcVipTTRg0p pn20uLos4B0nRomqjT+vX+awXI1VNfDAQSHiDaIJNctIFT2fplr9PztDs52wJX04Lm/J NStzkwMADBZJ5P9MBBusksfGQAquEgRApXFCu15hRvbIrXkRwV944PAuAJKVhdqkdRrI PNhg== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=WRybDIjHbMOO9laZH4+BxCuxgHXc0sQHQOJ2/itlYSQ=; b=tX+hHH+3aUmNIvYPOcl4Weq2yYQCjn9tou9hN9M7KhGg8pYv6sd6yMQh+jzabrl42Z EC3A4+Yn9ecTBRd9ZmW9/36cTuayXCFTI3vmzSi9USw5X3lRoYmi9/uGIlgNSOWREvnG ZkSLvKA6AdDRLJ1TPqe5CDXhxqmnVR8DIun0d6QlWidR7JoJLBMOxN592tbppVMzbsb2 QadhaACP+yz84DAANWQijpQQvfO17SgUkx0ayiWiwiQxs8K8H7BhdPB7TpasAxCXtMS7 FznwduKmuuPySa1u0QSKu9ei3phkb0K55esp3vbsCYnzF883iea+CH96uWLVaJ90UALy NMuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=Iuv4p5Cy; 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 n12-20020a1709062bcc00b006f4da4cbdd8si8876492ejg.896.2022.05.21.09.11.43; Sat, 21 May 2022 09:11:43 -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=@nifty.com header.s=dec2015msa header.b=Iuv4p5Cy; 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 C49C068B4E2; Sat, 21 May 2022 19:11:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from conssluserg-06.nifty.com (conssluserg-06.nifty.com [210.131.2.91]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8C76568B47B for ; Sat, 21 May 2022 19:11:01 +0300 (EEST) Received: from osamu-pc.kimura.local (M106073025034.v4.enabler.ne.jp [106.73.25.34]) (authenticated) by conssluserg-06.nifty.com with ESMTP id 24LGAf7Y026265; Sun, 22 May 2022 01:10:42 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-06.nifty.com 24LGAf7Y026265 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1653149442; bh=KkRP5vX77mBSWEGR6vs12errOXhelymU89Zua+HOSaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iuv4p5Cyp7wlDsOi2OKjQDSODjmO/fGMDYkDv4eAxQl2yNMkBGPaIa7RRkbVjFQZW H5jAi7t5TIBV4rdPorW2OyTGH0LpROedF08t/WGKH6gWWImy84Fv0N2mvwFSDvCCNo O4CeJVTxiF9DEPkIB0IniQDpOzGoZ8XfzOgYweC0EW6wNDF0IsDey65qlXXW1CgiPY BcRmUw5+eItMbzCVGQMyw1vag7VWVB78lw1MGUD2c+8G22AIdn7phT2H7pQUKP3Jtt 24cE0qPE1Ux94E+OrbSDkA+3K5tlTzF1aPAOPA+Tf3c5x6amJ2nv+PC6gcE6/kguNz ULZdFKDCfsOSQ== X-Nifty-SrcIP: [106.73.25.34] From: TADANO Tokumei To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 May 2022 01:09:42 +0900 Message-Id: <20220521160943.582994-3-aimingoff@pc.nifty.jp> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220521160943.582994-1-aimingoff@pc.nifty.jp> References: <20220521160943.582994-1-aimingoff@pc.nifty.jp> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 2/4] configure + lavc/Makefile: define build infrastructure for libaribcaption 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: TADANO Tokumei Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Ou9Ec5MNZDoz To compile with this feature: * libaribcaption external library has to be pre-installed. https://github.com/xqq/libaribcaption * configure with `--enable-libaribcaption` option. `--enable-libaribb24` and `--enable-libaribcaption` options are not exclusive. If both enabled, libaribcaption precedes as order listed in `libavcodec/allcodecs.c`. Signed-off-by: TADANO Tokumei --- configure | 4 ++++ libavcodec/Makefile | 1 + 2 files changed, 5 insertions(+) diff --git a/configure b/configure index f115b21064..37ce16e932 100755 --- a/configure +++ b/configure @@ -218,6 +218,7 @@ External library support: --enable-lcms2 enable ICC profile support via LittleCMS 2 [no] --enable-libaom enable AV1 video encoding/decoding via libaom [no] --enable-libaribb24 enable ARIB text and caption decoding via libaribb24 [no] + --enable-libaribcaption enable ARIB text and caption decoding via libaribcaption [no] --enable-libass enable libass subtitles rendering, needed for subtitles and ass filter [no] --enable-libbluray enable BluRay reading using libbluray [no] @@ -1817,6 +1818,7 @@ EXTERNAL_LIBRARY_LIST=" ladspa lcms2 libaom + libaribcaption libass libbluray libbs2b @@ -3319,6 +3321,7 @@ libaom_av1_decoder_deps="libaom" libaom_av1_encoder_deps="libaom" libaom_av1_encoder_select="extract_extradata_bsf" libaribb24_decoder_deps="libaribb24" +aribcaption_decoder_deps="libaribcaption" libcelt_decoder_deps="libcelt" libcodec2_decoder_deps="libcodec2" libcodec2_encoder_deps="libcodec2" @@ -6520,6 +6523,7 @@ enabled libaom && require_pkg_config libaom "aom >= 1.0.0" aom/aom_co enabled libaribb24 && { check_pkg_config libaribb24 "aribb24 > 1.0.3" "aribb24/aribb24.h" arib_instance_new || { enabled gpl && require_pkg_config libaribb24 aribb24 "aribb24/aribb24.h" arib_instance_new; } || die "ERROR: libaribb24 requires version higher than 1.0.3 or --enable-gpl."; } +enabled libaribcaption && require_pkg_config libaribcaption "libaribcaption >= 0.1.0" "aribcaption/aribcaption.h" aribcc_context_alloc enabled lv2 && require_pkg_config lv2 lilv-0 "lilv/lilv.h" lilv_world_new enabled libiec61883 && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883 enabled libass && require_pkg_config libass "libass >= 0.11.0" ass/ass.h ass_library_init diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 38425d2f22..2613fd78a1 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1050,6 +1050,7 @@ OBJS-$(CONFIG_PCM_MULAW_AT_ENCODER) += audiotoolboxenc.o OBJS-$(CONFIG_LIBAOM_AV1_DECODER) += libaomdec.o OBJS-$(CONFIG_LIBAOM_AV1_ENCODER) += libaomenc.o OBJS-$(CONFIG_LIBARIBB24_DECODER) += libaribb24.o ass.o +OBJS-$(CONFIG_ARIBCAPTION_DECODER) += libaribcaption.o ass.o OBJS-$(CONFIG_LIBCELT_DECODER) += libcelt_dec.o OBJS-$(CONFIG_LIBCODEC2_DECODER) += libcodec2.o OBJS-$(CONFIG_LIBCODEC2_ENCODER) += libcodec2.o From patchwork Sat May 21 16:09:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: TADANO Tokumei X-Patchwork-Id: 35881 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:9992:b0:82:461d:f3b with SMTP id ve18csp1126396pzb; Sat, 21 May 2022 09:11:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5y85AgWEHiGSdor11adBwkleJEcO7FyCe+FnFWcJkhATBrw8MeU/9Q3mBrSPohNulZNtQ X-Received: by 2002:a17:906:c102:b0:6f8:d325:bb82 with SMTP id do2-20020a170906c10200b006f8d325bb82mr13560048ejc.197.1653149484786; Sat, 21 May 2022 09:11:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653149484; cv=none; d=google.com; s=arc-20160816; b=OiICJQzBSSiBtydoZB2nFOTUCE3TFuWCfwB7PHwnfWygi/imHl391jHPpTg6bZI0nP ANWtvL3xv7xHnuk9+YUv3QazO3AlWTMwommLl63uN3O2f8mTv992pAW39KF0xRq+A1ua 67dRCOZAr13DH0Skd/kxAolIGmw/46rSukndih3Je0JGGj/zgOkFA3jL6NB2YyDp0lxZ pQwc8quBvfai/ED+sNYF+h78zbiKojbTsE5HWC8CSDWHgIZmxfxyQSuh+1gjRW+GQNqF euQhsHU4rNCJFP0okfd+Q1wKJk028kz4klW7eVQMIR1uyjjoKO+eZ5SEg+3fDVRQudTh 7J1g== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=vflG3+7DXvd7hcr7JizJcO9nKWmFM6Mz4shLPeuG7aI=; b=r9Qiv6DMkEr6OTDlI61MN5lSAapXX4lFDCSIj5cmEHX6i+sJgJIDOpFwBtaX4Y3hnc oXwZNI4gcLft+B+0jtZh+QkxeVLYHwEdT0AKdeUJxKS8PiutfUAGBdGfWQUJu9nDlLjR d8lyyE+I0NerI6SOj2EP6YriYWhbO3MVO83DW9vS5x3gSr0wbr5tYTPD+2tVxDZ/IrZT b8/JJ/YPZWL3iBj1/uLvuXx454yv2FyT1x9eXaBhHNHo89Ztst4kwrYckKJUyLEQkpqu JpyCnLwCx+Q/3PldmnsGr7CpTeTZHa2dTfyMkkBrH2RPal74dCIqY3+gVDZSbNDxUKUb ex3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=qmZ5U64w; 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 h21-20020a170906111500b006e7fe2ddfcdsi11137189eja.428.2022.05.21.09.11.24; Sat, 21 May 2022 09:11:24 -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=@nifty.com header.s=dec2015msa header.b=qmZ5U64w; 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 96F8568B4C1; Sat, 21 May 2022 19:11:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from conssluserg-06.nifty.com (conssluserg-06.nifty.com [210.131.2.91]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 812C468B475 for ; Sat, 21 May 2022 19:11:01 +0300 (EEST) Received: from osamu-pc.kimura.local (M106073025034.v4.enabler.ne.jp [106.73.25.34]) (authenticated) by conssluserg-06.nifty.com with ESMTP id 24LGAf7a026265; Sun, 22 May 2022 01:10:42 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-06.nifty.com 24LGAf7a026265 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1653149442; bh=R0abeW6nom4kFCYMO103S2/QFFiSSneeSffrXYlOsPA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qmZ5U64wuJW+JdIzLULkhakiZ4EOfXa3u5hn0fwUSNdog+FJEl0i+KONeBgADrCyr 3HPT9XHnZeaLCOtWs+ZVOwvIwsPlp2TDOOHMhEECy+TQ6plZ6rVNhD1gPFnDaZ3K4u rK86wIyEPnHG45YebPtUvpdICuxoyayZqp6XtBai296seCtCVsvbDkDFvRyRt0PgpA JqT2795ryqG4GKsog185nRneBNpt2zasLemu2dl3HPIbDwSTAqnXeO9oaZRDJ+VOun PkwpP0etP6V6T0vM9LONeu16RIXIZfDlFMzfarL1MSzf7rPwbosiNdNDOu9AeZ9B3r h4BuUBgpgHYAQ== X-Nifty-SrcIP: [106.73.25.34] From: TADANO Tokumei To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 May 2022 01:09:43 +0900 Message-Id: <20220521160943.582994-4-aimingoff@pc.nifty.jp> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220521160943.582994-1-aimingoff@pc.nifty.jp> References: <20220521160943.582994-1-aimingoff@pc.nifty.jp> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/4] lavc/codec_desc.c: remove AV_CODEC_PROP_TEXT_SUB property 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: TADANO Tokumei Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ma0hlDECfrne To support bitmap type of subtitles, remove AV_CODEC_PROP_TEXT_SUB property from codec descriptor for AV_CODEC_ID_ARIB_CAPTION. It is similar way to `libavcodec/libzvbi-teletextdec.c` (AV_CODEC_ID_DVB_TELETEXT). Instead, each subtitle decoder has to specify subtitile format. `libavcodec/libaribb24.c` uses same AV_CODEC_ID_ARIB_CAPTION and expects AV_CODEC_PROP_TEXT_SUB is defined. Thus add a line to specify text format subtitle. Signed-off-by: TADANO Tokumei --- libavcodec/codec_desc.c | 1 - libavcodec/libaribb24.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index e7f0f6a8d4..d0a31b536b 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3441,7 +3441,6 @@ static const AVCodecDescriptor codec_descriptors[] = { .type = AVMEDIA_TYPE_SUBTITLE, .name = "arib_caption", .long_name = NULL_IF_CONFIG_SMALL("ARIB STD-B24 caption"), - .props = AV_CODEC_PROP_TEXT_SUB, .profiles = NULL_IF_CONFIG_SMALL(ff_arib_caption_profiles), }, diff --git a/libavcodec/libaribb24.c b/libavcodec/libaribb24.c index 9658e1d5ac..46c25053b2 100644 --- a/libavcodec/libaribb24.c +++ b/libavcodec/libaribb24.c @@ -274,6 +274,7 @@ next_region: av_log(avctx, AV_LOG_DEBUG, "Styled ASS line: %s\n", buf.str); + sub->format = 1; /* text */ ret = ff_ass_add_rect(sub, buf.str, b24->read_order++, 0, NULL, NULL); } From patchwork Sat May 21 16:09:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: TADANO Tokumei X-Patchwork-Id: 35880 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:9992:b0:82:461d:f3b with SMTP id ve18csp1126326pzb; Sat, 21 May 2022 09:11:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsJAYbpxa0AFO5cdryZ1VhjXBSjPIpKA8+aDf3rmFOTJDFmccQ5UtIwtLqkwAjDs+lmq+O X-Received: by 2002:a17:907:1c25:b0:6f4:2d46:52ce with SMTP id nc37-20020a1709071c2500b006f42d4652cemr13162895ejc.25.1653149474500; Sat, 21 May 2022 09:11:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653149474; cv=none; d=google.com; s=arc-20160816; b=R7i8B4Qez4mzp8HgA6Bj9TLjoQpSq9YmJG2Y+xmbNYcc+JuG8cXIws+/0urRL8bYVZ NrRChW7WZ0fvl6Gpdq0GuOMmTTLYZhD9Hh7FNc0kEmOwjx1GuEX99ufvCkY8G3mhiB0c IAocBCwKMporVrLRVBDGxFlOyETiNMKFhNymN4/yydGDPq7fLP2BFsSl0316rb2yPWIA MtJdOd0sxRxr7LMCFiXW2Mii9h157ntdVh+BQVWf3cqXtCvhMIZTzSHhMA9iI/2cB6Zl fgv6jkMy5tnJD9bHBnOTonSB6Pl5VM9qRAjK6kpuizgfSviV07zhOt7INeOxBMhIVxea 15OA== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=KqiLG1oYQIXh4UOq54wgxVEVUlMZozrRZ52GtMPhcm8=; b=G7DDe4Wpasw29LUCtHVltYim3bvTvioU8W0nRrO2mvLdXQiFX0yKe7kiL6U1vQtz82 vBoO03FpCI4IY3rHxtIckE9hxxJeZqOVkVS4lxRYX4P758W63Q+l8mjEVVZA+9v9hNm+ VN68G4q2Png+fTGZB8DXl6CxI5keZeufJBQ4Mc5pspvB+Gq9rXyCKFMlOGMJDZbmxYgG 48D3EHvyIncL6FLRwslDqZiWep6Ro790RVmRQPqkPdbcn1x8JXWG3/UzcK2Jla+8REv0 fmzXeuCx7VlrmVki+FoNDyQiwboWbJenkmHKhcjeuzNM7wFZgxUQ2mFc6Hc5qChgZN/s tuCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=l1clnLAI; 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 a25-20020a50e719000000b0041d664093e1si10127493edn.482.2022.05.21.09.11.13; Sat, 21 May 2022 09:11:14 -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=@nifty.com header.s=dec2015msa header.b=l1clnLAI; 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 9F4AC68B4B2; Sat, 21 May 2022 19:11:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from conssluserg-06.nifty.com (conssluserg-06.nifty.com [210.131.2.91]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 89A9A68B476 for ; Sat, 21 May 2022 19:11:01 +0300 (EEST) Received: from osamu-pc.kimura.local (M106073025034.v4.enabler.ne.jp [106.73.25.34]) (authenticated) by conssluserg-06.nifty.com with ESMTP id 24LGAf7c026265; Sun, 22 May 2022 01:10:43 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-06.nifty.com 24LGAf7c026265 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1653149443; bh=BdWy/cpR4MZOMkRm0eIFMgsqh2uK1XWHkhg7RfnyK9U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l1clnLAIHFiQdOwM2ft9vyfHffn/l1tmxtcJ1xXem6xFLKAV/fDI4101nTSPjgxkU 8/+Um1I3nWAvgiOaCde5jJ9qzbDzxDJuzeYgGeKRJ12HpK+mO12soJx6LYWWs2JUxL RfbOcvqGjpAzEZ0/9b2t95v9k4izWd77ZmN/AiifCMOazv0C3xFK9wLGe8DBjGATEl IP88MM5uYcD78NTAhcRGEOdEOnG0e0dE0/haxLAIn0L/WXkjfoDw17MwP7HB3moccr J9TP7qVWa6fPVGAVysc+wgeYOq4NAAN+8CkHOILgPTmCLiJQWSLic6OPnryFoe4Dxb benv5v5Y/4APg== X-Nifty-SrcIP: [106.73.25.34] From: TADANO Tokumei To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 May 2022 01:09:44 +0900 Message-Id: <20220521160943.582994-5-aimingoff@pc.nifty.jp> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220521160943.582994-1-aimingoff@pc.nifty.jp> References: <20220521160943.582994-1-aimingoff@pc.nifty.jp> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 4/4] lavf/mpegts.c: set some properties for ARIB caption 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: TADANO Tokumei Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: tsTFVpg9Qfs9 Some additional properties are set for ARIB caption. * need_parsing = 0 ARIB caption doesn't require any parser. This avoids "parser not found" warning message. * need_context_update = 1 When any profiles are changed, set this flag to notify. * codecpar->width / codecpar->height Find best video stream and set frame size for ARIB_PROFILE_A type of ARIB caption. Signed-off-by: TADANO Tokumei --- libavformat/mpegts.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 6e761c07f1..ead833c264 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -2145,6 +2145,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type // Vol. 3, Section 2, 4.2.8.1 int actual_component_tag = sti->stream_identifier - 1; int picked_profile = FF_PROFILE_UNKNOWN; + AVStream *v_st = NULL; int data_component_id = get16(pp, desc_end); if (data_component_id < 0) return AVERROR_INVALIDDATA; @@ -2155,7 +2156,12 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type // non-mobile captioning service ("profile A"). if (actual_component_tag >= 0x30 && actual_component_tag <= 0x37) { + int v_index; picked_profile = FF_PROFILE_ARIB_PROFILE_A; + v_index = av_find_best_stream(fc, AVMEDIA_TYPE_VIDEO, + -1, 1, NULL, 0); + if (v_index >= 0) + v_st = fc->streams[v_index]; } break; case 0x0012: @@ -2174,8 +2180,19 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codecpar->codec_id = AV_CODEC_ID_ARIB_CAPTION; - st->codecpar->profile = picked_profile; + if (st->codecpar->profile != picked_profile) { + st->codecpar->profile = picked_profile; + sti->need_context_update = 1; + } + if (v_st && + (st->codecpar->width != v_st->codecpar->width || + st->codecpar->height != v_st->codecpar->height)) { + st->codecpar->width = v_st->codecpar->width; + st->codecpar->height = v_st->codecpar->height; + sti->need_context_update = 1; + } sti->request_probe = 0; + sti->need_parsing = 0; } break; case 0xb0: /* DOVI video stream descriptor */