From patchwork Wed Nov 6 02:49:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16136 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7ACFE4494FC for ; Wed, 6 Nov 2019 04:58:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5D13968B0C1; Wed, 6 Nov 2019 04:58:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 44BFD689970 for ; Wed, 6 Nov 2019 04:58:56 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id i10so3235916wrs.7 for ; Tue, 05 Nov 2019 18:58:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Jyokjedx3PFKXxKb6y1AwgJqb4raMqgFFAH1mlpymsI=; b=fC6PNAd5cCyAz0kpwO7Y9i/dKV1BCtfkJO1BTRt+jvM0gXaO79vtCwsApLC4wWw9q2 4+unu3vz/NxtTvRr57J505k4BBOtDfrxW7tRKOPYtR7rPGA6CbnqduDRShYlaGiFhYlK vkknpIrnMz8J4e7e2rk6qh3zvL+HgRiUxuVDx93g88+BPJWssm1AzyrjBcrnzlf5brti TLORksjBX83l6Du8WBxwb4cBLC74OuPwsRyKvKEKg9HLSjAtjoKpYgLy4cZkN9zHE4td sdIEeNn0FDjEKZSBjeW9/VNmj4T0LBH7D9dw92Fkjv0y545QS+w21perdb+yIALhxmiv OZgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jyokjedx3PFKXxKb6y1AwgJqb4raMqgFFAH1mlpymsI=; b=mb48PExkHc+EDePaMdspZz2909H4FV3FtlfYuajH629FpzKtapqqpnNQH2bC2XHCrI mx3ig1csr3DNZE9O1JDZzgywEMNilQd7Wk9kyzkBtyLOgLHCvqD8LLuI+1lGixIfIazN HqDhuu766K/xIn9TsclG/VOBlfBeWKmSWg6en/pJbwcfrYCZFmLdvBLww2HnXEU2Hyff yNecaepsXNoBlaYUicEMOpySzyi6vgL03XU6ex9iiLNnIdphbBLqr4TkfyGJ41mFzzhF cKPLgruquZuE4wx/6UYIbP2THP6OG+zc3gmKHat/81RdrZW53PLyHfaJ05k0A0RpKa0h FEfA== X-Gm-Message-State: APjAAAV3QlAcor7hDr84Dhqg+mEiT08+KWUxXKX8iCNq3nVOlpo6P2fH 3v/bPKShMeV5CXeFkTbJGDmAtrav X-Google-Smtp-Source: APXvYqyAIEoDkylpZCGmqD/UPiLuR7NlbwfGkaNiGD73McEz+7F/ARgsGT0Gv3e+LM6DuathYB8Dcg== X-Received: by 2002:a5d:6789:: with SMTP id v9mr394274wru.344.1573008641957; Tue, 05 Nov 2019 18:50:41 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id l13sm1281710wmh.12.2019.11.05.18.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2019 18:50:41 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Nov 2019 03:49:02 +0100 Message-Id: <20191106024922.19228-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191106024922.19228-1-andreas.rheinhardt@gmail.com> References: <20191106024922.19228-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/23] avformat/matroskaenc: Use random TrackUID X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, the TrackUID of a Matroska track which is supposed to be random was not random at all: It always coincided with the TrackNumber which is usually the 1-based index of the corresponding stream in the array of AVStreams. This has been changed: It is now set via an AVLFG if AVFMT_FLAG_BITEXACT is not set. Otherwise it is set like it is set now (the only change happens if an explicit track number has been choosen via dash_track_number, because the system used in the normal situation is now used, too). In particular, no FATE tests need to be updated. This also fixes a bug in case the dash_track_number option was used: In this case the TrackUID was set to the track number, but the tags were written with a TagTrackUID simply based upon the index, so that the tags didn't apply to the track they ought to apply to. Signed-off-by: Andreas Rheinhardt --- mkv_get_uid() might be overkill, but I simply wanted to be sure that there are no collisions. libavformat/matroskaenc.c | 65 ++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index de57e474be..b87d15b7ff 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -94,6 +94,7 @@ typedef struct mkv_cues { typedef struct mkv_track { int write_dts; int has_cue; + uint32_t uid; int sample_rate; int64_t sample_rate_offset; int64_t last_timestamp; @@ -1199,8 +1200,7 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0); put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER, mkv->is_dash ? mkv->dash_track_number : i + 1); - put_ebml_uint (pb, MATROSKA_ID_TRACKUID, - mkv->is_dash ? mkv->dash_track_number : i + 1); + put_ebml_uint (pb, MATROSKA_ID_TRACKUID, mkv->tracks[i].uid); put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet) if ((tag = av_dict_get(st->metadata, "title", NULL, 0))) @@ -1650,7 +1650,8 @@ static int mkv_write_tags(AVFormatContext *s) if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID)) continue; - ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1); + ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, + mkv->tracks[i].uid); if (ret < 0) return ret; } @@ -1658,13 +1659,15 @@ static int mkv_write_tags(AVFormatContext *s) for (i = 0; i < s->nb_streams; i++) { AVIOContext *pb; AVStream *st = s->streams[i]; + mkv_track *track = &mkv->tracks[i]; ebml_master tag_target; ebml_master tag; if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) continue; - mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tag_target); + mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, + track->uid, &tag_target); pb = mkv->tags_bc; tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0); @@ -1863,10 +1866,6 @@ static int mkv_write_header(AVFormatContext *s) version = 4; } - mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks)); - if (!mkv->tracks) { - return AVERROR(ENOMEM); - } ebml_header = start_ebml_master(pb, EBML_ID_HEADER, MAX_EBML_HEADER_SIZE); put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1); put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1); @@ -2667,8 +2666,42 @@ static int webm_query_codec(enum AVCodecID codec_id, int std_compliance) return 0; } +static uint32_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c) +{ + uint32_t uid; + + for (int j = 0, k; j < 5; j++) { + uid = av_lfg_get(c); + if (!uid) + continue; + for (k = 0; k < i; k++) { + if (tracks[k].uid == uid) { + /* Was something wrong with our random seed? */ + av_lfg_init(c, av_get_random_seed()); + break; + } + } + if (k == i) + return uid; + } + + /* Test the numbers from 1 to i. */ + for (int j = 1, k; j < i + 1; j++) { + for (k = 0; k < i; k++) { + if (tracks[k].uid == j) + break; + } + if (k == i) + return j; + } + /* Return i + 1. It can't coincide with another uid. */ + return i + 1; +} + static int mkv_init(struct AVFormatContext *s) { + MatroskaMuxContext *mkv = s->priv_data; + AVLFG c; int i; if (s->nb_streams > MAX_TRACKS) { @@ -2697,7 +2730,23 @@ static int mkv_init(struct AVFormatContext *s) s->internal->avoid_negative_ts_use_pts = 1; } + mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks)); + if (!mkv->tracks) { + return AVERROR(ENOMEM); + } + + if (!(s->flags & AVFMT_FLAG_BITEXACT)) + av_lfg_init(&c, av_get_random_seed()); + for (i = 0; i < s->nb_streams; i++) { + mkv_track *track = &mkv->tracks[i]; + + if (s->flags & AVFMT_FLAG_BITEXACT) { + track->uid = i + 1; + } else { + track->uid = mkv_get_uid(mkv->tracks, i, &c); + } + // ms precision is the de-facto standard timescale for mkv files avpriv_set_pts_info(s->streams[i], 64, 1, 1000); }