From patchwork Fri Apr 24 15:20:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: vectronic X-Patchwork-Id: 19218 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 4BE8644BACD for ; Fri, 24 Apr 2020 18:21:03 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 32DF868C146; Fri, 24 Apr 2020 18:21:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3BF0868C130 for ; Fri, 24 Apr 2020 18:20:56 +0300 (EEST) Received: by mail-wm1-f47.google.com with SMTP id z6so11247471wml.2 for ; Fri, 24 Apr 2020 08:20:56 -0700 (PDT) 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=0neAe7q4SCRJw5uX4nDJkMgfnOJVIYYkuxr+X8I9DV8=; b=mKlN9UF5KHKhHxK47XQ/vCQcRfz3hUTppjbgegDhartpskDI0FQi+KFRVJhYLI4md0 sU6lPDzOzS9087R4xTrDgdk0pb8j8gYJxF3bt/E5VMVrzFy4mHaIVlRMmoaTY8g3mpf9 FHhkQxhYvcFFomuUhjZHiIVzxziYBdo7tNXSgT1XpBRm0YroBOGVBdcShoN9Y9+HCdjx AEIxEYOQLxQEYP1sJtAK0HiPuIno/c2tNHCzp7FZ7QFHsEm3cMvmby/fFIM3Ykbqp61Z g/JR4ODC4SZj/uYEtwbo/t/0hdLcZgn+BcmQTazPp2OY9d78siIQbvvdy3DLNvZJQekT N/Uw== 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=0neAe7q4SCRJw5uX4nDJkMgfnOJVIYYkuxr+X8I9DV8=; b=OU+fzAcFALKTC0bKEUGV29qpDOUR72MzqNquYxoJKq6lQKRTgj6zHe/9goGO5Qm4Es 25rpZdPxdSRFEiZ9ZOzkPeK1Hq+OL9jOkCA0/08hVUqfDS1x+wF84osf4SbKG9xdItNo 1IogmchXIl6650zLXJxn1Dmr8/C1vvxbBr2x/y4qhCpyhLJzS5oykszLv3K1cd1LBhL8 Q+ZC9C03Cj/Ue9RGOCvG++akPfAoz4jj093JT7U0TeqYss4uc7ChLE5Wqk8WqhuotXtd pfWLHmKnphchMOpNoYIe0bhtCTkqBqxo1euzNvBFN4LNatfFZBtPaIOvKM/ktXQ211MD g29w== X-Gm-Message-State: AGi0PuazZe/v1Y2qxKT7a3M7QxvubDtlzIIA/eTpp4SWG7uthSUMliwQ BW+pRPDrJPL2oYKNiNxmuBfIUwXkXmQ= X-Google-Smtp-Source: APiQypIUDPzBd9z5/PmeBmBjT0HEctEhtZhs3AyaN0+gJdOi6XZyWSaxxUA1t9KYvipNKO+kq2ta5Q== X-Received: by 2002:a7b:cd10:: with SMTP id f16mr11102730wmj.21.1587741655324; Fri, 24 Apr 2020 08:20:55 -0700 (PDT) Received: from mechagodzilla.chapatronic.net (cpc76904-dals22-2-0-cust471.20-2.cable.virginm.net. [81.106.45.216]) by smtp.gmail.com with ESMTPSA id l15sm3131661wmi.48.2020.04.24.08.20.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Apr 2020 08:20:54 -0700 (PDT) From: vectronic To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Apr 2020 16:20:42 +0100 Message-Id: <20200424152042.29383-3-hello.vectronic@gmail.com> X-Mailer: git-send-email 2.24.2 (Apple Git-127) In-Reply-To: <20200424152042.29383-1-hello.vectronic@gmail.com> References: <20200424152042.29383-1-hello.vectronic@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avformat mov fix to detect if stream position has been reset 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: vectronic Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" if pos has been reset, clear fragments and indexes and search for next root Signed-off-by: vectronic --- libavformat/mov.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index 3d6fef685d..8f051fb9b1 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -7796,15 +7796,15 @@ static int mov_switch_root(AVFormatContext *s, int64_t target, int index) if (index >= 0 && index < mov->frag_index.nb_items) target = mov->frag_index.item[index].moof_offset; - if (avio_seek(s->pb, target, SEEK_SET) != target) { + if (target >= 0 && avio_seek(s->pb, target, SEEK_SET) != target) { av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target); return AVERROR_INVALIDDATA; } mov->next_root_atom = 0; - if (index < 0 || index >= mov->frag_index.nb_items) + if ((index < 0 && target >= 0) || index >= mov->frag_index.nb_items) index = search_frag_moof_offset(&mov->frag_index, target); - if (index < mov->frag_index.nb_items && + if (index >= 0 && index < mov->frag_index.nb_items && mov->frag_index.item[index].moof_offset == target) { if (index + 1 < mov->frag_index.nb_items) mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset; @@ -7856,8 +7856,38 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) AVStream *st = NULL; int64_t current_index; int ret; + int i; mov->fc = s; retry: + if (s->pb->pos == 0) { + + // Discard current fragment index + if (mov->frag_index.allocated_size > 0) { + av_freep(&mov->frag_index.item); + mov->frag_index.nb_items = 0; + mov->frag_index.allocated_size = 0; + mov->frag_index.current = -1; + mov->frag_index.complete = 0; + } + + for (i = 0; i < s->nb_streams; i++) { + AVStream *avst = s->streams[i]; + MOVStreamContext *msc = avst->priv_data; + + // Clear current sample + mov_current_sample_set(msc, 0); + + // Discard current index entries + if (avst->index_entries_allocated_size > 0) { + av_freep(&avst->index_entries); + avst->index_entries_allocated_size = 0; + avst->nb_index_entries = 0; + } + } + + if ((ret = mov_switch_root(s, -1, -1)) < 0) + return ret; + } sample = mov_find_next_sample(s, &st); if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) { if (!mov->next_root_atom)