From patchwork Wed Mar 27 11:18:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Felix de Souza via ffmpeg-devel X-Patchwork-Id: 12478 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 5977E448DC9 for ; Wed, 27 Mar 2019 13:21:35 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3BA9768A9B3; Wed, 27 Mar 2019 13:21:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D3B9268A8F5 for ; Wed, 27 Mar 2019 13:21:32 +0200 (EET) Received: by mail-wm1-f65.google.com with SMTP id z6so4584009wmi.0 for ; Wed, 27 Mar 2019 04:21:32 -0700 (PDT) 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=NfE01wbEURQhpuokmRJR2w/60I0Srl/tPmevbP1uVk0=; b=j8ESqMdCtb96Clx66loUtaRMZUe22lTMJgsJu0PGyClxFVrWuJXkozSYzDKaLD3iqb 2/n5vIce6AcjRTrSldlrXF3G82cAHLdYW8NflOLVI7IqUlMKf80BUQIIYvZNu1ptES1D antlxPAAWizS4qvwskl0nBQnxHTRhCb3Dc/A59cDTf5tsdm6MIZb3xXSn68IIIyTbUdc LsereUDtUq6EQj/TgeqBMxVihnVsD1sSWcAja2oGtvdPpr5spXa0eVi+saLBrBqj1vrl hvTCVsnpcbRa0xgxhGiadn/RC0+zXTvuSHvIw04bI/JpwDPD+/2OFmEgKeigwHT8PZDl ZxtA== X-Gm-Message-State: APjAAAWCWGU8HhEpr2vPreqNiCzMF2B+OETYfA929aTnmdPG0Np3tMP+ nWjULVrblTuWP6NDFsCVpjdNXGbnz+I= X-Google-Smtp-Source: APXvYqyNlpdPNBAn6ltzNnVNqSPRhWWCadsh3JlE45Qm9qksATPvgn/ScquHjoqzRTw1+4mV84kyew== X-Received: by 2002:a05:600c:2309:: with SMTP id 9mr18746130wmo.52.1553685692180; Wed, 27 Mar 2019 04:21:32 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id h10sm31745448wrs.27.2019.03.27.04.21.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Mar 2019 04:21:31 -0700 (PDT) To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Mar 2019 12:18:47 +0100 Message-Id: <20190327111852.3784-17-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190327111852.3784-1-andreas.rheinhardt@googlemail.com> References: <20190308092604.3752-1-andreas.rheinhardt@googlemail.com> <20190327111852.3784-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 16/21] avformat/matroskadec: Make cluster parsing level compatible 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: , X-Patchwork-Original-From: Andreas Rheinhardt via ffmpeg-devel From: Diego Felix de Souza via ffmpeg-devel Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt , robux4@ycbcr.xyz Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Before this commit, the parsing of clusters mixed EBML levels by allowing elements from different levels in a EbmlSyntax (namely matroska_cluster_parsing). This has been changed. The level is now explicitly used to determine how to parse. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 102 +++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 32cf57685f..fc8c3f936c 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -709,15 +709,10 @@ static const EbmlSyntax matroska_cluster_parsing[] = { { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock, bin) }, { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE }, { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE }, - { MATROSKA_ID_INFO, EBML_NONE }, - { MATROSKA_ID_CUES, EBML_NONE }, - { MATROSKA_ID_TAGS, EBML_NONE }, - { MATROSKA_ID_SEEKHEAD, EBML_NONE }, - { MATROSKA_ID_CLUSTER, EBML_STOP }, { 0 } }; -static const EbmlSyntax matroska_cluster[] = { +static const EbmlSyntax matroska_cluster_initial[] = { { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, offsetof(MatroskaCluster, timecode) }, { MATROSKA_ID_BLOCKGROUP, EBML_STOP }, { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP }, @@ -726,12 +721,20 @@ static const EbmlSyntax matroska_cluster[] = { { 0 } }; +static const EbmlSyntax matroska_cluster_enter[] = { + { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, { .n = matroska_cluster_initial } }, + { 0 } +}; + static const EbmlSyntax matroska_clusters[] = { - { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, { .n = matroska_cluster } }, - { MATROSKA_ID_INFO, EBML_NONE }, - { MATROSKA_ID_CUES, EBML_NONE }, - { MATROSKA_ID_TAGS, EBML_NONE }, - { MATROSKA_ID_SEEKHEAD, EBML_NONE }, + { MATROSKA_ID_CLUSTER, EBML_STOP }, + { MATROSKA_ID_CUES, EBML_NONE }, + { MATROSKA_ID_TAGS, EBML_NONE }, + { MATROSKA_ID_INFO, EBML_NONE }, + { MATROSKA_ID_TRACKS, EBML_NONE }, + { MATROSKA_ID_ATTACHMENTS, EBML_NONE }, + { MATROSKA_ID_CHAPTERS, EBML_NONE }, + { MATROSKA_ID_SEEKHEAD, EBML_NONE }, { 0 } }; @@ -789,24 +792,6 @@ static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos) return AVERROR_EOF; } -/* - * Return: Whether we reached the end of a level in the hierarchy or not. - */ -static int ebml_level_end(MatroskaDemuxContext *matroska) -{ - AVIOContext *pb = matroska->ctx->pb; - int64_t pos = avio_tell(pb); - - if (matroska->num_levels > 0) { - MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1]; - if (pos - level->start >= level->length || matroska->current_id) { - matroska->num_levels--; - return 1; - } - } - return (matroska->is_live && matroska->ctx->pb->eof_reached) ? 1 : 0; -} - /* * Read: an "EBML number", which is defined as a variable-length * array of bytes. The first byte indicates the length by giving a @@ -3533,41 +3518,39 @@ static int matroska_parse_cluster(MatroskaDemuxContext *matroska) MatroskaCluster *cluster = &matroska->current_cluster; MatroskaBlock *block = &cluster->block; int res; - res = ebml_parse(matroska, - matroska_cluster_parsing, - cluster); - if (res == 1) { - /* New Cluster */ - if (cluster->pos) - ebml_level_end(matroska); - cluster->pos = avio_tell(matroska->ctx->pb); - /* sizeof the ID which was already read */ - if (matroska->current_id) - cluster->pos -= 4; - res = ebml_parse(matroska, - matroska_clusters, - cluster); - /* Try parsing the block again. */ - if (res == 1) - res = ebml_parse(matroska, - matroska_cluster_parsing, - cluster); + + av_assert0(matroska->num_levels <= 2); + + if (matroska->num_levels == 1) { + res = ebml_parse(matroska, matroska_clusters, NULL); + + if (res == 1) { + /* Found a cluster: subtract the size of the ID already read. */ + cluster->pos = avio_tell(matroska->ctx->pb) - 4; + + res = ebml_parse(matroska, matroska_cluster_enter, cluster); + if (res < 0) + return res; + } } - if ((!res || res == LEVEL_ENDED) && block->bin.size > 0) { + if (matroska->num_levels == 2) { + int err = 0; + /* We are inside a cluster. */ + res = ebml_parse(matroska, matroska_cluster_parsing, cluster); + + if ((!res || res == LEVEL_ENDED) && block->bin.size > 0) { int is_keyframe = block->non_simple ? block->reference == INT64_MIN : -1; uint8_t* additional = block->additional.size > 0 ? block->additional.data : NULL; - res = matroska_parse_block(matroska, block->bin.buf, block->bin.data, + err = matroska_parse_block(matroska, block->bin.buf, block->bin.data, block->bin.size, block->bin.pos, - matroska->current_cluster.timecode, - block->duration, is_keyframe, - additional, block->additional_id, - block->additional.size, - cluster->pos, + cluster->timecode, block->duration, + is_keyframe, additional, block->additional_id, + block->additional.size, cluster->pos, block->discard_padding); - } + } if (res == LEVEL_ENDED) cluster->pos = 0; @@ -3575,6 +3558,13 @@ static int matroska_parse_cluster(MatroskaDemuxContext *matroska) ebml_free(matroska_blockgroup, block); memset(block, 0, sizeof(*block)); + if (err < 0) + return err; + } else if (!matroska->num_levels) { + matroska->done = 1; + return AVERROR_EOF; + } + return res; }