From patchwork Wed Mar 6 07:19:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 46831 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d90e:b0:19e:cdac:8cce with SMTP id jd14csp224861pzb; Tue, 5 Mar 2024 23:21:06 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUye/md5oxQuuPk7qz6na3rWuaGHnoMEceZm/fJ6A1gJ5ANol6AcmSgtcFRYSHgceLUfKYXFRE0v4yD5TG1pahEIe9pUK4btJot/Q== X-Google-Smtp-Source: AGHT+IEhoLWu7YWP4Qdz9Nfe7R0extvv3BknrQ9D53RwtI0SzUY49RBO1oLUISBvPvyC89fJkgI5 X-Received: by 2002:a05:6402:202e:b0:565:9ac6:a9e5 with SMTP id ay14-20020a056402202e00b005659ac6a9e5mr11171784edb.21.1709709666313; Tue, 05 Mar 2024 23:21:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709709666; cv=none; d=google.com; s=arc-20160816; b=C2BYdfD50gB8TmVinOEz44iBgKtXcp+kXVSFzcM0VkHVVDtqIWpKNbcHOWqb2EJ9Qr sN1OUO9yh+Uci6/Z/tiGlpAkAJRmyRaC40mVRHhCTLxtg+Rz0xqsQX23RDOuymi3LJAB T03WzLvdVJ9NzwOCwfZ3DBH1c0qHPKRd9E/W2byWuxWULI5x6x39Xm5eBtnjUgAtb2JH ENZMVRFVJXZf+4ci+M5uleoDMYtDHsBeur59+qLwGYhV4OIdLS9tFSb4yrIKNoy7b2GP eGvwIqHSblyJXhBHocWL61rWmyfnrN1hnG9m/DnEepyWAISeWkz16rIfEatJaHrRdCY5 jTAg== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=EsCV0A5ldytxzOqgts7LNUmdLaFv3FWO06hhTq0tcGw=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=cdhBT6HcTkbkPc5hVsPArshqFS7+IhB1HzU78DZl9tD4g6EM2JKEyNtAGQ+WjQA//7 lqK4j24WnOD3p0RzsLid5XHIh9c2e4jeeG7y/P7q+YQnI1iRDoN8Qh963VbJd+Y1vykn 10FNXeuvoaD2ymqqTHBdmqat2aeBiSD7IVqzfiFN69qDUBafM2xlcKkFU8+buWczmv71 /xUsyeoSt2k2TPv9RRSxDtcM4+1BbSDgSW/qUq8L75QLQ/lt6l/uni+cMwi/yIyRrF4w bkx1pV5BrBlGXS/f42tQMUxJdOiRdlFTD5Jc0id/9fJrTEyk0Mg6v1KmaxhYSLfYPPhP c1VA==; 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=Ccgx89nr; 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 q17-20020a056402519100b00565ff637724si5820756edd.91.2024.03.05.23.21.05; Tue, 05 Mar 2024 23:21:06 -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=Ccgx89nr; 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 4A01F68CB6E; Wed, 6 Mar 2024 09:20:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f228.google.com (mail-oi1-f228.google.com [209.85.167.228]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3879468C3D2 for ; Wed, 6 Mar 2024 09:20:44 +0200 (EET) Received: by mail-oi1-f228.google.com with SMTP id 5614622812f47-3c1a2f7e1d2so3844371b6e.1 for ; Tue, 05 Mar 2024 23:20:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1709709641; x=1710314441; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Po6QVK/XJr7QJip7nB+sDc8m8J2YAegAFAv4kjgJhrM=; b=Ccgx89nrad6kcJgp+pGFS36EL4qwefn2EE7zSMUZcJUFY1fXKcUWUi2GCdQdC7SRlg xkr1AAPnD6wpl+XtcykleX4XbhbZhBw1RTrS7cTy+8bVFeszBHRw1i1sHw5fEchg6PRs 9V8VKeC1O91skkBxmxk5SyJhh7P/I8mjBu8hxkyM031XN/iTAfBZHL1iIQD6eLJTfLY7 R2vCTmr5oJKloLPfVkLh5K2UTYgsviG04gNiEy0s6pq+fLEmI9p/odzFdKmjtVs676gL 4Y3e+WBJAWfCrtF+K7Bzm1CWquxvbb2Ipb97bwJZ+3IJDpioSAOCc+RYwTQ0uTQP0xT9 hdWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709709641; x=1710314441; 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=Po6QVK/XJr7QJip7nB+sDc8m8J2YAegAFAv4kjgJhrM=; b=Ju9t7nwijrueN4ptlCFnOnJgGedUWvQJtHskYQkX275mrdbJDkVxnl+xren7AITDU6 4K2YbYRmTlCGwspYYbzJM/SG05zKoFVxnIgrd6FKY0mZHya2iCj9piktdDKkQ1CEBmiX zxL5Xk8MbsK3t/GDOmtkpicCFz6wNrt1AmGDcGd1Ynbhnqm9CwqwqwhzmyL+l/oLLTqi QtgDoj1t7ivy9uqchaOMFbuu9HvfY64yP8tBfd9i1DQokQvXN6+jIk+319otwx+bY+Qr uq52WEr0q4cSBDWlKQWrTG45fRqo1Gl82Wfz7+wU40q9oWj1SyVQHd328rLazUQqX7fA vW+Q== X-Gm-Message-State: AOJu0YyaRiZrzz9IInW0h/7jiiXqfIeHuahig9+zPDs9zn69fOGhVFrn iOPcEe3UNinq5e9rx9eBfiiRE2uAfruRAbqkZrfHmvdQqCprDwRWI0HbRFkXg8TzMh7IDQMjPT4 tU+PNSZ9o/4Q/lGhCRLeJWg7uCyySNzNiuFiCVOFW X-Received: by 2002:aca:f01:0:b0:3c1:dbd5:fc39 with SMTP id 1-20020aca0f01000000b003c1dbd5fc39mr4005601oip.31.1709709641418; Tue, 05 Mar 2024 23:20:41 -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 s5-20020a63ff45000000b005dc846d791csm553686pgk.14.2024.03.05.23.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 23:20:41 -0800 (PST) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Mar 2024 01:19:11 -0600 Message-Id: <20240306071913.2735832-1-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avformat/dvdvideodec: add CLUT utilities and 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: AT7SxZTUJtU4 Since last iteration: removes unused variable in dvdclut.c Signed-off-by: Marth64 --- doc/demuxers.texi | 5 +++ libavformat/Makefile | 2 +- libavformat/dvdclut.c | 75 +++++++++++++++++++++++++++++++++++++++ libavformat/dvdclut.h | 37 +++++++++++++++++++ libavformat/dvdvideodec.c | 15 ++++++++ 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 f4bac8f3b3..1a17c6db16 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -394,6 +394,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 8811a0ffc9..a3bfc209c3 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -194,7 +194,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..cd4b103e4b --- /dev/null +++ b/libavformat/dvdclut.c @@ -0,0 +1,75 @@ +/* + * 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) +{ + 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 b83ae0c2a5..3bc76f5c65 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); @@ -1056,6 +1066,7 @@ static int dvdvideo_subp_stream_add(AVFormatContext *s, DVDVideoPGCSubtitleStrea { AVStream *st; FFStream *sti; + int ret; st = avformat_new_stream(s, NULL); if (!st) @@ -1065,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); @@ -1373,6 +1387,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 },