From patchwork Thu May 16 22:30:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13150 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 8AEC244964F for ; Fri, 17 May 2019 01:43:26 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 77713689FCE; Fri, 17 May 2019 01:43:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9DC80689EED for ; Fri, 17 May 2019 01:43:22 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id m3so4900428wrv.2 for ; Thu, 16 May 2019 15:43:22 -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=18zuU1sUb6wGITQFxxDV1ysLlWjEdjnkxxYLw6BCabY=; b=hHMCjYKPIdWV3Ym9Enup7x1NpjpeBiH1Xzlv6/TqFVj56gNta9R/dBcGYPFsvGL9HR QpYhkK/vP/anQbT7Jw6UoYcgP0eBq70TTO/V7jGnJJtWky8rZdxJUM5CJjmBsYMNDDkR pOtq82IpIgCs/5q2EbH7VWzk7YcJl79+Crgfp7pOEoHyTNMjrmC3La1SRWYiSJwYUb3T HeaJUeujgt/nxSxOX7XC5DK8fXL4mw5nkO5HvTtFSvaKYC8kktvZ+XdfrT6IoaDWmEvk 6pDD5U8oTxLRAZh7WeRTk7GJYpzaopuEowxATvmDoheYI7ME7ZtqQattLJNSfFeKdvcx 2UgQ== 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=18zuU1sUb6wGITQFxxDV1ysLlWjEdjnkxxYLw6BCabY=; b=EXU8gQ/TeSoqAc4rzu96FR8nlojK7v7J5sEzIgcMXgBQFX/a8tJFhPsJjjlWhgJgfA ocKbH66TvMDLsjWovUOp3bufiypeDNjC/eR29evgAtlBM1h46uXQ62l/9mx/gwdtGJe/ f0DvSJp02wbp1N45tf80zKhvsnJVrDWQ/7LHoHMTOw/M/KDHP8QbNeYRHEjuslo/sGWu gS+M7iYwHYx+YI6qaV08I6LEosIsOR/KA2b9fZaJT3IwJeR2AFwNKML7paAIuxQuP6pc CphguIPtz2y1ib6b160Fj0y44GT/325uml4bvCpX7WBFGZ5jF6fwxvwxqfTCNWKOr4AX w85g== X-Gm-Message-State: APjAAAUSBnOZrx/Mq8WAx6Kj2oW/pkBdnS8Sda+GhtPBR76GKWbWTqFm ap73h68vi3UWHH6ftvwH2+Wy562a X-Google-Smtp-Source: APXvYqzI2DEZUYajrLQhv01MFB59A6mgKRqAA8A+Nedb0n6P4KG6H50aL1okqu1zH+6HHqCw3sbhow== X-Received: by 2002:a5d:4cd0:: with SMTP id c16mr6169536wrt.20.1558046601932; Thu, 16 May 2019 15:43:21 -0700 (PDT) Received: from localhost.localdomain (ipbcc18715.dynamic.kabel-deutschland.de. [188.193.135.21]) by smtp.gmail.com with ESMTPSA id i185sm11168725wmg.32.2019.05.16.15.43.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 15:43:21 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 May 2019 00:30:08 +0200 Message-Id: <20190516223018.30827-25-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> References: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 24/37] 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: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt 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. And the level is now explicitly used to determine how to parse. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 106 +++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 59 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 97e25c4c99..4fe9978321 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -722,15 +722,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 } /* We don't want to go back to level 0, so don't add the parent. */ + CHILD_OF(matroska_segment) }; -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 }, @@ -739,12 +734,20 @@ static const EbmlSyntax matroska_cluster[] = { CHILD_OF(matroska_segment) }; +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 } /* We don't want to go back to level 0, so don't add the parent. */ }; #undef CHILD_OF @@ -814,24 +817,6 @@ static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos) return pb->error ? pb->error : 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 @@ -3596,43 +3581,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); - else - cluster->pos = 0; + + 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 >= 0 && 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 >= 0 && 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; @@ -3640,6 +3621,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; }