From patchwork Mon Feb 19 04:01:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 46361 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:dda5:b0:19e:cdac:8cce with SMTP id kw37csp1049972pzb; Sun, 18 Feb 2024 20:02:56 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXhe080FXtkjjKfFQNUEDnDd59hVIE/+3I/3wZxfmYgVlUbYo7MFtT3JSag8NLrBSjHjWKkcbN+UKEZWL3iPKfM7lvel3J9+DY95g== X-Google-Smtp-Source: AGHT+IFdiqZSZVEyUa2ppEQ1TfuhuVTW25ib85Ny1n8u2ZVROIWRppK2dKbRNlV1gpLrCFtoCdhi X-Received: by 2002:a50:8add:0:b0:560:1f51:80f6 with SMTP id k29-20020a508add000000b005601f5180f6mr5324520edk.4.1708315376000; Sun, 18 Feb 2024 20:02:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708315375; cv=none; d=google.com; s=arc-20160816; b=CI9VKFywMP2TNNxf+6CBC4Jy485IDdU7ylrZVgYeXGNdD+j33p3uKcN4Mt40/6U6Z/ nwljmQZqghFcYyyQbl0sQ+QXCTpdJJKZvK9GFRjRy2mz8qHJQHPehBwh3dOPQNLt4WYw +rwgTWyMxcEziS81StF4MLVX8IxqKrLg4BaZMTL6yzbR5YTBP6mr8ZuFqypzCMR1Ybd4 jqZHjrAOsoXYuNL7pGPGYFbD+1hrT3ZvdR0F0beujPYh2PAJQahdzUl4Xwk/wNtL62RX GdmMBwSumjRL1C1TfJ/ZPcbzlTAEPUkjG4k1CQ11Ola4P4yidxVYxjkkWPHjVBcwc9nv LTAw== 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:delivered-to; bh=X08ayZC9ccQE/o9Kg/XgSGK3Xp74ArSWoUG4VK/hT7o=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=mVc60+ajOimSPAgsYicmL1H4CTs2fHsLTL2bRYWOjLADziBA5l8+lQ8E7Kc3AO2L6G 1qhYqfPsWzys2xTcn1NjCb50arctKM7NsXQjEdBAT5X67qBUR1lT+mLx8GAv7ff/3vo1 TqOuhKacp3iJO6kX+Tj3y98GDrL4+0zLwlXuq8NkS6yvWL1yvFgIpOE9qiPVXyn9YuiD AuUKHUZwijdM8wRVo1Z8DCFADQiO8h3y66a0z9aIBUXNPBNRv5d1R81lVHH8DmzkVDM5 wguzxNec9uEF9937NQBV9+L7j5AA7PNkGwJcNXVjc4chYgkAkHTR661F2uYd2i2eYM9r nl/w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@proxyid.net header.s=google header.b=anmZRucg; 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 r7-20020a056402234700b00563d1136229si2188878eda.436.2024.02.18.20.02.55; Sun, 18 Feb 2024 20:02:55 -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; dkim=neutral (body hash did not verify) header.i=@proxyid.net header.s=google header.b=anmZRucg; 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 7250C68D3CF; Mon, 19 Feb 2024 06:02:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f227.google.com (mail-pg1-f227.google.com [209.85.215.227]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2787A68D3C0 for ; Mon, 19 Feb 2024 06:02:34 +0200 (EET) Received: by mail-pg1-f227.google.com with SMTP id 41be03b00d2f7-5dbcfa0eb5dso3715843a12.3 for ; Sun, 18 Feb 2024 20:02:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1708315352; x=1708920152; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iFnR4WkC5DjWbAwzAoaOnH3zsUH1Muko5gIeh/AD3cc=; b=anmZRucgT4PTJHsA3I5+TVzDpIGJJ4msxf48Qf9UeGQzQL0vEdP5l4UnEL5BJGByDs L5QnSRBRA9GJkcp7z5gqQ2MqFYPrZF32TvxRRGv1HLKLEaMvczEebuHPg3LXDAATq3EO PdRU2JQKGxG4CG8OpZ3tfaaMNGuvTgufjRSxchturLxaUS4SI3mkeQlDgtBFUhK0H+7P 4nme8gLAL9EaG7yykRQSnRFmxOLQl7qcnVcRmI1Xcdp3tI3WeGC76u3efnJr3/WkSXiZ abuSVNnwW1yXpCA6OX1vu4+kACabkYBGnSLSE6/d8+iVtat03THLu7obs+tb19ceBIG5 CDng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708315352; x=1708920152; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iFnR4WkC5DjWbAwzAoaOnH3zsUH1Muko5gIeh/AD3cc=; b=DXvhXyMwHK9WpR0cKqDD/H6+T84DLqv3pkSgY2AqTcjRkZqS1J2sabNR0TllQ5ptu/ PHtQPC1yHpXpD4tLkXiMmGWRXvrEgAUDtQSa1dxnGnjSGngyyTIkPVrLWMYkgeGMgZEE Uw8ROinHBuT/7CTFuGSgB0GcibzcjrKgt97X6w2o33EhRkhSf12miIezmX2TmNPTO7HR OeeWdbFGas6WYZxgcUuTNgKN/WXgKKEtp1yJTphg+Vx2QAdn6gCGXh4TaAgwH7pulHdu RR/9wKNCC+c1xMC6Rw+lstet4RwsZ04wpn+LJGp/ev0u96vsW6i0iwigliPCWMDPRNkL SBvQ== X-Gm-Message-State: AOJu0Yw6fVV0ziNOdTZsmJMEAciS/B6fB6eU1IWcyvXDokvwPuh7ZYxd RwKXw8wequ9DMceeDHqpcdKuuAb2bMaBAFWeSDO/dEIJmO3bx5G4nVwxn1vbhMPX1o4z8BULUMs jgfgQq8ZwVl8zJr5CKrg6tihNYFp+E0uPPhf+76qO X-Received: by 2002:a05:6a20:c78d:b0:19e:9da6:c73b with SMTP id hk13-20020a056a20c78d00b0019e9da6c73bmr11383189pzb.8.1708315352209; Sun, 18 Feb 2024 20:02:32 -0800 (PST) Received: from wsx-cc1-001.. (c-76-141-249-38.hsd1.il.comcast.net. [76.141.249.38]) by smtp-relay.gmail.com with ESMTPS id k78-20020a628451000000b006e455b86b38sm220723pfd.11.2024.02.18.20.02.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 20:02:32 -0800 (PST) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Sun, 18 Feb 2024 22:01:45 -0600 Message-Id: <20240219040145.1916326-2-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240219040145.1916326-1-marth64@proxyid.net> References: <20240219040145.1916326-1-marth64@proxyid.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v14 2/2] libavformat/dvdvideo: add CLUT utilities and enable subtitle palette support 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: Marth64 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ju/hqoyG5fuP Signed-off-by: Marth64 --- doc/demuxers.texi | 5 +++ libavformat/Makefile | 2 +- libavformat/dvdclut.c | 76 +++++++++++++++++++++++++++++++++++++++ libavformat/dvdclut.h | 37 +++++++++++++++++++ libavformat/dvdvideodec.c | 14 ++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 libavformat/dvdclut.c create mode 100644 libavformat/dvdclut.h diff --git a/doc/demuxers.texi b/doc/demuxers.texi index 062ea2ea42..d3c7675631 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -391,6 +391,11 @@ often with junk data intended for controlling a real DVD player's buffering speed and with no other material data value. Default is 1, true. +@item clut_rgb @var{bool} +Output subtitle palettes (CLUTs) as RGB, required for Matroska. +Disable to output the palette in its original YUV colorspace. +Default is 1, true. + @end table @subsection Examples diff --git a/libavformat/Makefile b/libavformat/Makefile index eda1f6e177..501159d9ca 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -192,7 +192,7 @@ OBJS-$(CONFIG_DTS_MUXER) += rawenc.o OBJS-$(CONFIG_DV_MUXER) += dvenc.o OBJS-$(CONFIG_DVBSUB_DEMUXER) += dvbsub.o rawdec.o OBJS-$(CONFIG_DVBTXT_DEMUXER) += dvbtxt.o rawdec.o -OBJS-$(CONFIG_DVDVIDEO_DEMUXER) += dvdvideodec.o +OBJS-$(CONFIG_DVDVIDEO_DEMUXER) += dvdvideodec.o dvdclut.o OBJS-$(CONFIG_DXA_DEMUXER) += dxa.o OBJS-$(CONFIG_EA_CDATA_DEMUXER) += eacdata.o OBJS-$(CONFIG_EA_DEMUXER) += electronicarts.o diff --git a/libavformat/dvdclut.c b/libavformat/dvdclut.c new file mode 100644 index 0000000000..2125e91541 --- /dev/null +++ b/libavformat/dvdclut.c @@ -0,0 +1,76 @@ +/* + * DVD-Video subpicture CLUT (Color Lookup Table) utilities + * + * 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 "libavutil/bprint.h" +#include "libavutil/colorspace.h" +#include "libavutil/common.h" + +#include "dvdclut.h" +#include "internal.h" + +int ff_dvdclut_palette_extradata_cat(const uint32_t *clut, + const size_t clut_size, + AVCodecParameters *par) +{ + int ret = 0; + AVBPrint bp; + + if (clut_size != FF_DVDCLUT_CLUT_SIZE) + return AVERROR(EINVAL); + + av_bprint_init(&bp, 0, FF_DVDCLUT_EXTRADATA_SIZE); + + av_bprintf(&bp, "palette: "); + + for (int i = 0; i < FF_DVDCLUT_CLUT_LEN; i++) + av_bprintf(&bp, "%06"PRIx32"%s", clut[i], + i != (FF_DVDCLUT_CLUT_LEN - 1) ? ", " : ""); + + av_bprintf(&bp, "\n"); + + return ff_bprint_to_codecpar_extradata(par, &bp); +} + +int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size) +{ + int y, cb, cr; + uint8_t r, g, b; + int r_add, g_add, b_add; + + if (clut_size != FF_DVDCLUT_CLUT_SIZE) + return AVERROR(EINVAL); + + for (int i = 0; i < FF_DVDCLUT_CLUT_LEN; i++) { + y = (clut[i] >> 16) & 0xFF; + cr = (clut[i] >> 8) & 0xFF; + cb = clut[i] & 0xFF; + + YUV_TO_RGB1_CCIR(cb, cr); + + y = (y - 16) * FIX(255.0 / 219.0); + r = av_clip_uint8((y + r_add - 1024) >> SCALEBITS); + g = av_clip_uint8((y + g_add - 1024) >> SCALEBITS); + b = av_clip_uint8((y + b_add - 1024) >> SCALEBITS); + + clut[i] = (r << 16) | (g << 8) | b; + } + + return 0; +} diff --git a/libavformat/dvdclut.h b/libavformat/dvdclut.h new file mode 100644 index 0000000000..41cea7e2c9 --- /dev/null +++ b/libavformat/dvdclut.h @@ -0,0 +1,37 @@ +/* + * DVD-Video subpicture CLUT (Color Lookup Table) utilities + * + * 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 + */ + +#ifndef AVFORMAT_DVDCLUT_H +#define AVFORMAT_DVDCLUT_H + +#include "libavcodec/codec_par.h" + +/* ("palette: ") + ("rrggbb, "*15) + ("rrggbb") + \n + \0 */ +#define FF_DVDCLUT_EXTRADATA_SIZE (9 + (8 * 15) + 6 + 1 + 1) +#define FF_DVDCLUT_CLUT_LEN 16 +#define FF_DVDCLUT_CLUT_SIZE FF_DVDCLUT_CLUT_LEN * sizeof(uint32_t) + +int ff_dvdclut_palette_extradata_cat(const uint32_t *clut, + const size_t clut_size, + AVCodecParameters *par); + +int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size); + +#endif /* AVFORMAT_DVDCLUT_H */ diff --git a/libavformat/dvdvideodec.c b/libavformat/dvdvideodec.c index 3355010356..0951189124 100644 --- a/libavformat/dvdvideodec.c +++ b/libavformat/dvdvideodec.c @@ -49,6 +49,7 @@ #include "avio_internal.h" #include "avlanguage.h" #include "demux.h" +#include "dvdclut.h" #include "internal.h" #include "url.h" @@ -95,6 +96,7 @@ typedef struct DVDVideoPGCSubtitleStreamEntry { int startcode; enum DVDVideoSubpictureViewport viewport; int disposition; + uint32_t clut[FF_DVDCLUT_CLUT_LEN]; const char *lang_iso; } DVDVideoPGCSubtitleStreamEntry; @@ -128,6 +130,7 @@ typedef struct DVDVideoDemuxContext { int opt_angle; /* the user-provided angle number (1-indexed) */ int opt_chapter_end; /* the user-provided exit PTT (0 for last) */ int opt_chapter_start; /* the user-provided entry PTT (1-indexed) */ + int opt_clut_rgb; /* output subtitle palette (CLUT) as RGB */ int opt_pg; /* the user-provided PG number (1-indexed) */ int opt_pgc; /* the user-provided PGC number (1-indexed) */ int opt_preindex; /* pre-indexing mode (2-pass read) */ @@ -1038,6 +1041,8 @@ break_error: static int dvdvideo_subp_stream_analyze(AVFormatContext *s, uint32_t offset, subp_attr_t subp_attr, DVDVideoPGCSubtitleStreamEntry *entry) { + DVDVideoDemuxContext *c = s->priv_data; + char lang_dvd[3] = {0}; entry->startcode = 0x20 + (offset & 0x1F); @@ -1045,6 +1050,11 @@ static int dvdvideo_subp_stream_analyze(AVFormatContext *s, uint32_t offset, sub if (subp_attr.lang_extension == 9) entry->disposition |= AV_DISPOSITION_FORCED; + memcpy(&entry->clut, c->play_state.pgc->palette, FF_DVDCLUT_CLUT_SIZE); + + if (c->opt_clut_rgb) + ff_dvdclut_yuv_to_rgb(entry->clut, FF_DVDCLUT_CLUT_SIZE); + AV_WB16(lang_dvd, subp_attr.lang_code); entry->lang_iso = ff_convert_lang_to(lang_dvd, AV_LANG_ISO639_2_BIBL); @@ -1066,6 +1076,9 @@ static int dvdvideo_subp_stream_add(AVFormatContext *s, DVDVideoPGCSubtitleStrea st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codecpar->codec_id = AV_CODEC_ID_DVD_SUBTITLE; + if ((ret = ff_dvdclut_palette_extradata_cat(entry->clut, FF_DVDCLUT_CLUT_SIZE, st->codecpar)) < 0) + return ret; + if (entry->lang_iso) av_dict_set(&st->metadata, "language", entry->lang_iso, 0); @@ -1382,6 +1395,7 @@ static const AVOption dvdvideo_options[] = { {"angle", "playback angle number", OFFSET(opt_angle), AV_OPT_TYPE_INT, { .i64=1 }, 1, 9, AV_OPT_FLAG_DECODING_PARAM }, {"chapter_end", "exit chapter (PTT) number (0=end)", OFFSET(opt_chapter_end), AV_OPT_TYPE_INT, { .i64=0 }, 0, 99, AV_OPT_FLAG_DECODING_PARAM }, {"chapter_start", "entry chapter (PTT) number", OFFSET(opt_chapter_start), AV_OPT_TYPE_INT, { .i64=1 }, 1, 99, AV_OPT_FLAG_DECODING_PARAM }, + {"clut_rgb", "output subtitle palette (CLUT) as RGB", OFFSET(opt_clut_rgb), AV_OPT_TYPE_BOOL, { .i64=1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, {"pg", "entry PG number (0=auto)", OFFSET(opt_pg), AV_OPT_TYPE_INT, { .i64=0 }, 0, 255, AV_OPT_FLAG_DECODING_PARAM }, {"pgc", "entry PGC number (0=auto)", OFFSET(opt_pgc), AV_OPT_TYPE_INT, { .i64=0 }, 0, 999, AV_OPT_FLAG_DECODING_PARAM }, {"preindex", "enable for accurate chapter markers, slow (2-pass read)", OFFSET(opt_preindex), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },