From patchwork Fri Apr 12 09:40:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 48014 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:670b:b0:1a9:af23:56c1 with SMTP id wh11csp152197pzb; Fri, 12 Apr 2024 02:41:01 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW9De/WFsFQWdEjCaDr9RQMxVDcb0WvlpUiVZEys28EN+L+NmtHlKFVLiId3uPRw4r6AVN0+UKBPWYm1Q11NzRPhoer98+312lSww== X-Google-Smtp-Source: AGHT+IHkf1o5Vvl2n2cjuRCr8e354ATX4nDC0kZKCa5jUIU6ihWuJF8zBEPNGZsQ4skMM+Y3f6RU X-Received: by 2002:a50:9fc6:0:b0:56f:e70c:16d with SMTP id c64-20020a509fc6000000b0056fe70c016dmr1688697edf.0.1712914861171; Fri, 12 Apr 2024 02:41:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712914861; cv=none; d=google.com; s=arc-20160816; b=XshLadRtomafqH7O53xX7VQW5Ip/mSNwWqqzPWb/uURGkcDahTRRFAmsOuXJ3h130D XDtSTOxPbcOaCF/83tBfKdTs+FuKGZfCxZqoWno1kRQnAmhAjFG/cz3HmXapQD0wUDhB ZpnMBU2aXXr/xwiwN3rHtfNe+h2TFhr4ziG9/LBfCzASvzAZ02N3sbuiI2CZtWGmM5J1 d4Z/W26FlH84DvO5A2azPydVLAoDfPoD9OwVnJyF/seZlqyGH4o/2kaC+jRDghoBW00h bjnAaL4thF8nbbfwRkU/QhpzWfgHDcz3tq/PYPZNmFUg5arpihKVd2ZdyObnNFqYjB88 8l5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:date:to:from:message-id:delivered-to; bh=C3Snw/T8wGEZM2rja08QA/zUrGhip8WC+Fde1PzWwlI=; fh=vgcpdHWu4da4Dk/quIxpjcmmxLeEWfPnCXzoiCPpQLU=; b=d8yWWYJ2YmaBgzMt6sxghvIQDNK3rti71VFmE6RioOkDKZDakq7ZN58nPQJyA0SRxW 9X9XXbtVwnk2XZXXDQQSnuH9+KVWP+nxFKyO/trcp5aUDJ+Ebw3bB2BkPaUO+VlVYQhM TJ0lDCt+xp1/wuqwG6Bf0sV2ABfjlO2dVyxlWHXwYXyohuFuPtUpP6+hOlLgJJV3wszK P1WxoVptmBdzP4SZQyZ57VOQZOMccKWFtv1D/vb1Glz1FIwiygg43wcXQEYwTxPK3VeJ BgE/2qxL97nEK5F2OsBaNQURj/INrC91oSRjVOwza1xn3NquiIUY1avLaRL3c08rRQsl ANDg==; dara=google.com 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 en27-20020a056402529b00b0056c169a23a1si1514783edb.427.2024.04.12.02.40.59; Fri, 12 Apr 2024 02:41:01 -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; 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 3D99568D223; Fri, 12 Apr 2024 12:40:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from glom.nmugroup.com (glom.nmugroup.com [193.183.80.6]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 380E668D0A6 for ; Fri, 12 Apr 2024 12:40:49 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id B6B2E542A188 for ; Fri, 12 Apr 2024 11:40:48 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 6BA21542A182 for ; Fri, 12 Apr 2024 11:40:48 +0200 (CEST) Message-ID: <71fccb5a1e28dfd23e3dd710c86c8a3a662bbd61.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: ffmpeg-devel Date: Fri, 12 Apr 2024 11:40:47 +0200 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] ffmpeg: Carry streamid as metadata key 'id' 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: nyFqAyAFDJ5A This idea could be extended to other fields not presently considered to be metadata, that would be handy to treat as such. I use the key "id" because ffprobe outputs id= for streamid. Another option could be to collect these types of metadata that go into AVStream fields under a namespace like FFMPEG: or AVSTREAM: or something, then delete all of them using AV_DICT_IGNORE_SUFFIX near the end of of_open() since they're for internal ffmpeg use. The FATE change is just because av_dict() changes the order of things when elements are deleted. /Tomas From 7799f1b2eb8ab02e58118565f3e889fbe0d568a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Fri, 12 Apr 2024 10:34:12 +0200 Subject: [PATCH] ffmpeg: Carry streamid as metadata key 'id' This allows using -map_metadata and -metadata to copy/set streamids (PIDs). --- fftools/ffmpeg_demux.c | 5 +++ fftools/ffmpeg_mux_init.c | 56 +++++++++++++++++++++-------- tests/ref/fate/matroska-stereo_mode | 6 ++-- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index cba63dab5f..1b0ef91abb 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -1454,6 +1454,11 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st) ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id); + // carry streamid as metadata so that streamids can be be handled like any other metadata + // be careful not to overwrite any existing value, just in case + if ((ret = av_dict_set_int(&st->metadata, "id", st->id, AV_DICT_DONT_OVERWRITE)) < 0) + av_log(ist, AV_LOG_ERROR, "error %i setting \"id\" to %i. id already set?\n", ret, st->id); + return 0; } diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 6d8bd5bcdf..dbe16e8d0f 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -1073,21 +1073,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, ost = &ms->ost; - if (o->streamid) { - AVDictionaryEntry *e; - char idx[16], *p; - snprintf(idx, sizeof(idx), "%d", ost->index); - - e = av_dict_get(o->streamid, idx, NULL, 0); - if (e) { - st->id = strtol(e->value, &p, 0); - if (!e->value[0] || *p) { - av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value); - return AVERROR(EINVAL); - } - } - } - ost->par_in = avcodec_parameters_alloc(); if (!ost->par_in) return AVERROR(ENOMEM); @@ -3016,6 +3001,43 @@ static Muxer *mux_alloc(void) return mux; } +static int of_set_streamid(Muxer *mux, const OptionsContext *o) +{ + for (int i = 0; i < mux->fc->nb_streams; i++) { + OutputStream *ost = mux->of.streams[i]; + AVDictionaryEntry *e = NULL; + + // take -streamid if explicitly set + if (o->streamid) { + char idx[16]; + snprintf(idx, sizeof(idx), "%d", ost->index); + + e = av_dict_get(o->streamid, idx, NULL, 0); + } + + // if -streamid not set then try to grab it from metadata + // this maintains backward compatibility + if (!e) + e = av_dict_get(ost->st->metadata, "id", NULL, 0); + + if (e) { + char *p; + ost->st->id = strtol(e->value, &p, 0); + if (!e->value[0] || *p) { + av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value); + return AVERROR(EINVAL); + } else { + // delete id now that we've picked it up, + // so that it doesn't make it into the output + int ret = av_dict_set(&ost->st->metadata, "id", NULL, 0); + if (ret < 0) + return ret; + } + } + } + return 0; +} + int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) { Muxer *mux; @@ -3149,6 +3171,10 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) if (err < 0) return err; + err = of_set_streamid(mux, o); + if (err < 0) + return err; + err = set_dispositions(mux, o); if (err < 0) { av_log(mux, AV_LOG_FATAL, "Error setting output stream dispositions\n"); diff --git a/tests/ref/fate/matroska-stereo_mode b/tests/ref/fate/matroska-stereo_mode index 739b789fea..e46f75cbb4 100644 --- a/tests/ref/fate/matroska-stereo_mode +++ b/tests/ref/fate/matroska-stereo_mode @@ -1,4 +1,4 @@ -a7a220a77001e81685ec807ce5ac3bc6 *tests/data/fate/matroska-stereo_mode.matroska +40d2771cf74d378476cc4764b87af156 *tests/data/fate/matroska-stereo_mode.matroska 1470764 tests/data/fate/matroska-stereo_mode.matroska #extradata 0: 3510, 0x560c3919 #extradata 1: 3510, 0x560c3919 @@ -125,8 +125,8 @@ DISPOSITION:dub=0 DISPOSITION:original=0 TAG:language=ger-at TAG:stereo_mode=left_right -TAG:DESCRIPTION-ger=Deutsch TAG:DESCRIPTION-fre=Francais +TAG:DESCRIPTION-ger=Deutsch TAG:DURATION=00:00:10.000000000 [SIDE_DATA] side_data_type=Stereo 3D @@ -140,8 +140,8 @@ DISPOSITION:dub=0 DISPOSITION:original=0 TAG:language=eng TAG:stereo_mode=bottom_top -TAG:DESCRIPTION-ger=Deutsch TAG:DESCRIPTION-fre=Francais +TAG:DESCRIPTION-ger=Deutsch TAG:DURATION=00:00:10.000000000 [SIDE_DATA] side_data_type=Stereo 3D -- 2.39.2