From patchwork Tue Nov 16 14:15:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 31447 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp7450947iob; Tue, 16 Nov 2021 06:16:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJxGkwV3Kk9c/dwKeBcwL6EZ6+LrwpzyEyIn8IP8krV4GsNMgft7bTgdzNc6kOfhhq69bHPp X-Received: by 2002:a17:907:720b:: with SMTP id dr11mr10619717ejc.93.1637072184083; Tue, 16 Nov 2021 06:16:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637072184; cv=none; d=google.com; s=arc-20160816; b=Vkc/pGL1Jq5/Dl7jRrI1sXTDhXcUYdEP3dEs7k3yfZefJVEbBB+EtIgs2qjrooVERA piGjI6NqH1W3XomSX50deQV9oA63FbT1VSwUJAkHtHElcbZtPRyJDvfD9v+sL3i71ZnY r0vBY9xaxLYAn6iB8prAkATJ3mAH2Esos31F7FOjIqcUs2S0ftdLBSS3ZruLMZce7zWV gSirJd5q67YutwzmG38jeWxjv3cSLiWBelsuvU7Pvps0HeIdaOrzb5Jidzng7E7NKfNZ VdQaCZjWAdj0nYVfQZzM22Y0hLbZeDN0KyQR0ymec5mjkNFXLmU3WqxAlxUQa6j9Gxk9 2RIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=TIfV0QSskRb4qfoGS7GRKA55QjvsxAUw3/bu31p/oPw=; b=ehzISnysH+cHZmW1eFiaEhHLKKZ2Ta3dHBFjh16KeXp8fYLkKJN29PsYdNmx1RRU3Z bJncjRuOMXMNb1RZ9Q5YRlvvg3boFV2fJEgLWrbtUQABsIXB/ty3/OpgBpXDaRmjyTIw niSmVulQJ2O408kRTcfXe6Bbkq9IkhbXTCOvwl7cv2ETorUH8vYFTj6+sM6VrH4WCIbq FVMOPJvf03HRHj0u+13DasOiRqjWgs5eZVedko8XBR0hvi3i+5lNLrDY6SH4Ux9ga7kq 12WCsMKRT4/uRGvj8ZUl1ygPJ9pCImFBVXXNRmaY7Atgj17GvuOzWYW4eiNrrNHySWHt hpMA== 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 nb16si48740195ejc.354.2021.11.16.06.16.22; Tue, 16 Nov 2021 06:16:24 -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; 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 D9DEF680B71; Tue, 16 Nov 2021 16:16:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0739680B71 for ; Tue, 16 Nov 2021 16:16:08 +0200 (EET) Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:105:465:1:4:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4Htp536DJMzQjgG for ; Tue, 16 Nov 2021 15:16:07 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Nov 2021 19:45:33 +0530 Message-Id: <20211116141533.1708-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/mov: make STTS duration unsigned int 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: 2TBWueidr/1B As per 8.6.1.2.2 of ISO/IEC 14496-12:2015(E), STTS sample offsets are to be always stored as uint32_t. So far, they have been signed ints which led to desync in files with very large offsets. The MOVStts struct was used to store CTTS offsets as well. These can be negative in version 1. So a new struct MOVCtts was created and all declarations for CTTS usage changed to MOVCtts. --- The muxer should be adjusted to be able to write larger durations in the stts but that's a larger undertaking which I'll do later. libavformat/isom.h | 9 +++++++-- libavformat/mov.c | 20 ++++++++++---------- libavformat/movenc.c | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/libavformat/isom.h b/libavformat/isom.h index f3c18c95be..ef8f19b18c 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -55,9 +55,14 @@ struct AVAESCTR; typedef struct MOVStts { unsigned int count; - int duration; + unsigned int duration; } MOVStts; +typedef struct MOVCtts { + unsigned int count; + int duration; +} MOVCtts; + typedef struct MOVStsc { int first; int count; @@ -168,7 +173,7 @@ typedef struct MOVStreamContext { uint8_t *sdtp_data; unsigned int ctts_count; unsigned int ctts_allocated_size; - MOVStts *ctts_data; + MOVCtts *ctts_data; unsigned int stsc_count; MOVStsc *stsc_data; unsigned int stsc_index; diff --git a/libavformat/mov.c b/libavformat/mov.c index 8a910a3165..451cb78bbf 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -77,7 +77,7 @@ typedef struct MOVParseTableEntry { static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom); static int mov_read_mfra(MOVContext *c, AVIOContext *f); -static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size, +static int64_t add_ctts_entry(MOVCtts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size, int count, int duration); static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, @@ -2938,7 +2938,7 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) return AVERROR(ENOMEM); for (i = 0; i < entries && !pb->eof_reached; i++) { - int sample_duration; + unsigned int sample_duration; unsigned int sample_count; unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries); MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size, @@ -3191,7 +3191,7 @@ static int get_edit_list_entry(MOVContext *mov, static int find_prev_closest_index(AVStream *st, AVIndexEntry *e_old, int nb_old, - MOVStts* ctts_data, + MOVCtts* ctts_data, int64_t ctts_count, int64_t timestamp_pts, int flag, @@ -3342,17 +3342,17 @@ static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ * Append a new ctts entry to ctts_data. * Returns the new ctts_count if successful, else returns -1. */ -static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size, +static int64_t add_ctts_entry(MOVCtts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size, int count, int duration) { - MOVStts *ctts_buf_new; - const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts); + MOVCtts *ctts_buf_new; + const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVCtts); const size_t requested_size = min_size_needed > *allocated_size ? FFMAX(min_size_needed, 2 * (*allocated_size)) : min_size_needed; - if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1) + if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVCtts) - 1) return -1; ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size); @@ -3486,7 +3486,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) int nb_old = sti->nb_index_entries; const AVIndexEntry *e_old_end = e_old + nb_old; const AVIndexEntry *current = NULL; - MOVStts *ctts_data_old = msc->ctts_data; + MOVCtts *ctts_data_old = msc->ctts_data; int64_t ctts_index_old = 0; int64_t ctts_sample_old = 0; int64_t ctts_count_old = msc->ctts_count; @@ -3793,7 +3793,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st) unsigned int stps_index = 0; unsigned int i, j; uint64_t stream_size = 0; - MOVStts *ctts_data_old = sc->ctts_data; + MOVCtts *ctts_data_old = sc->ctts_data; unsigned int ctts_count_old = sc->ctts_count; if (sc->elst_count) { @@ -4754,7 +4754,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) AVStream *st = NULL; FFStream *sti = NULL; MOVStreamContext *sc; - MOVStts *ctts_data; + MOVCtts *ctts_data; uint64_t offset; int64_t dts, pts = AV_NOPTS_VALUE; int data_offset = 0; diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 37d4403f7a..233aee62d4 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -2434,7 +2434,7 @@ static int mov_write_stsd_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext static int mov_write_ctts_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track) { MOVMuxContext *mov = s->priv_data; - MOVStts *ctts_entries; + MOVCtts *ctts_entries; uint32_t entries = 0; uint32_t atom_size; int i;