From patchwork Mon Jul 29 19:56:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 14134 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 C1556449190 for ; Mon, 29 Jul 2019 23:11:39 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AA9C568A7FF; Mon, 29 Jul 2019 23:11:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D105068A7EB for ; Mon, 29 Jul 2019 23:11:32 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id f9so63185393wre.12 for ; Mon, 29 Jul 2019 13:11:32 -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=C1TBonZDPrTBwg2OYtSTC48BHV1Lz6orEAKVL5W3+ug=; b=TIR5SJzr68b1EruYeXFXLVYZhhIJx1z/IBqgDW1hqhYg35fl/HllqWC8cLgLo2jz2U bk9f71ehDPA4oSyL9Re2OVP7Pcq26ZETot0dsbgOkJFgEAT8aYCAYdqPolyQWdnOI45J CU6j+LAYgmwucIoyAmsVx/G0RcKnRRFhPvjaNVAvDmgpNvI/0gQ1R/fIpwl1WUtrcsgM Dz5i/6nc2TxW57dLMmhZLvPmJxm6Bc6TAg6AkICOvmj0bCimWQqPg1V3HhV3xtwhAyC2 q/+4WxlEs8i/PjGNQ0peUsBJYRZKVsFO/sXHawH0Lb9RoTbe8HJ0pQth6DgB4FBeEwen 9h8Q== 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=C1TBonZDPrTBwg2OYtSTC48BHV1Lz6orEAKVL5W3+ug=; b=fkjMJvKEwS8+J9rt0EhwJqDOyP7UGHrV0keDdxeb0WvYsAzI5uuZyUxsmOFBKaajBf 6lRZwdBAH2yGPly+aLRBXHQVHaoFwBEHzTpLt9Cqt1BMHHIYzy2zSN+ZE5bcO0a+ACGS tOrBU1TFKKHeEa/vVKnlX9hqV2Nik+TVmesziY89r0YzDfSCmhVD1XeekDAkKAhqVIIf 7Da9q5KcYNDU8ofYJC2t3k2MV7rtj9yDQ8X9n3EPjdZDshMINbZ5PcnUr1jg+NY3hiR9 3RUHs6F78kSkiuCBUr84X769wfuDa8yw6LZvLgvvvb5vcDgpOmi4ZGjePDuz2sXG/a0p chtA== X-Gm-Message-State: APjAAAVLjHuoQhV5QUxA9ea4P+68+gUxYYjNKt0XGjFNnSl+/EMDwvtY 8nbmbC+Ea6xKzMeiW+I5tJihQDKK X-Google-Smtp-Source: APXvYqyxlakoMIZPyWaPBsGu9/AbYoLgyKXb9olRtr0V/97M0E/smm8l5W+u6rAqfxjKZr00rKGc+w== X-Received: by 2002:a5d:694a:: with SMTP id r10mr124186090wrw.345.1564430619297; Mon, 29 Jul 2019 13:03:39 -0700 (PDT) Received: from localhost.localdomain (ipbcc08b8f.dynamic.kabel-deutschland.de. [188.192.139.143]) by smtp.gmail.com with ESMTPSA id t24sm56602211wmj.14.2019.07.29.13.03.38 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 29 Jul 2019 13:03:38 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 29 Jul 2019 21:56:56 +0200 Message-Id: <20190729195658.56078-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190729195658.56078-1-andreas.rheinhardt@gmail.com> References: <20190729195658.56078-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/7] cbs_mpeg2: Fix parsing the last unit 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" There is one way to find out if avpriv_find_start_code has found a start code or not: One has to check whether the state variable contains a start code, i.e. whether the three most serious bytes are 0x00 00 01. Checking for whether the return value is the end of the designated buffer is not enough: If the last four bytes constitute a start code, the return value is also the end of the buffer. This happens with sequence_end_codes which have been ignored for exactly this reason, although e.g. all three files used for fate tests of cbs_mpeg2 contain sequence_end_codes. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_mpeg2.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c index c529038fd9..596e9677b7 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -170,27 +170,41 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamUnitType unit_type; uint32_t start_code = -1; size_t unit_size; - int err, i; + int err, i, final = 0; start = avpriv_find_start_code(frag->data, frag->data + frag->data_size, &start_code); + if (start_code >> 8 != 0x01) { + // No start code found. + return AVERROR_INVALIDDATA; + } + for (i = 0;; i++) { unit_type = start_code & 0xff; + if (start == frag->data + frag->data_size) { + // The last four bytes form a start code which constitutes + // a unit of its own. In this situation avpriv_find_start_code + // won't modify start_code at all so modify start_code so that + // the next unit will be treated as the last unit. + start_code = 0; + } + end = avpriv_find_start_code(start--, frag->data + frag->data_size, &start_code); // start points to the byte containing the start_code_identifier - // (or to the last byte of fragment->data); end points to the byte + // (may be the last byte of fragment->data); end points to the byte // following the byte containing the start code identifier (or to // the end of fragment->data). - if (end == frag->data + frag->data_size) { - // We didn't find a start code, so this is the final unit. - unit_size = end - start; - } else { + if (start_code >> 8 == 0x01) { // Unit runs from start to the beginning of the start code // pointed to by end (including any padding zeroes). unit_size = (end - 4) - start; + } else { + // We didn't find a start code, so this is the final unit. + unit_size = end - start; + final = 1; } err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type, (uint8_t*)start, @@ -198,7 +212,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, if (err < 0) return err; - if (end == frag->data + frag->data_size) + if (final) break; start = end;