From patchwork Wed Jan 4 19:25:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 2054 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp4975206vsb; Wed, 4 Jan 2017 11:25:54 -0800 (PST) X-Received: by 10.28.147.204 with SMTP id v195mr49973279wmd.35.1483557954604; Wed, 04 Jan 2017 11:25:54 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p123si78815030wmg.154.2017.01.04.11.25.54; Wed, 04 Jan 2017 11:25:54 -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; dkim=neutral (body hash did not verify) header.i=@hotmail.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; dmarc=fail (p=NONE dis=NONE) header.from=hotmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ECDAA68A0B2; Wed, 4 Jan 2017 21:25:46 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from SNT004-OMC3S21.hotmail.com (snt004-omc3s21.hotmail.com [65.55.90.160]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 70D4D68A065 for ; Wed, 4 Jan 2017 21:25:39 +0200 (EET) Received: from NAM03-DM3-obe.outbound.protection.outlook.com ([65.55.90.137]) by SNT004-OMC3S21.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Wed, 4 Jan 2017 11:25:43 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=GtdmaAOFUa6WxsfUoHxPSt5FF33Alz4+kwlES1IUmSI=; b=movT0N1ewqMxCgswA2U318UCh+L2zcocKMeXJUorYMyxkYrHiDvXiJGlwLdhmwko58Usnr3dZdQsT9+H8Ix+Ta9SCUG5nlMm6LZMz/qsPf2c8aylgTtPi1FzZvJMmChSfMqyGnVVv4l7RwsQonkOlz+aYsb6F4m7/Id7FutR4lLqBbB1pEiTNszCbVMpysSlYXCmH/gxjGlL9s7BO23fXTFvRGFgkvA+7TE6MLCqQMPayE8t2KFYX4c+tNHTCi5qJf2995kosZJH8Hn3uTGjFQ7jm1IgWYUNtNwdHt/AtMq4UhxI44fo9jUQEERhnW27cKt6opZuj8emhmh1V8m93A== Received: from CO1NAM03FT039.eop-NAM03.prod.protection.outlook.com (10.152.80.56) by CO1NAM03HT210.eop-NAM03.prod.protection.outlook.com (10.152.80.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.803.8; Wed, 4 Jan 2017 19:25:42 +0000 Received: from MWHPR13MB1678.namprd13.prod.outlook.com (10.152.80.60) by CO1NAM03FT039.mail.protection.outlook.com (10.152.81.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.803.8 via Frontend Transport; Wed, 4 Jan 2017 19:25:42 +0000 Received: from MWHPR13MB1678.namprd13.prod.outlook.com ([10.171.144.16]) by MWHPR13MB1678.namprd13.prod.outlook.com ([10.171.144.16]) with mapi id 15.01.0829.003; Wed, 4 Jan 2017 19:25:42 +0000 From: Soft Works To: FFmpeg development discussions and patches Thread-Topic: [FFmpeg-devel] [PATCH] avformat/matroskaenc: Regression fix for invalid MKV headers Thread-Index: AQHSZsBWW9gLApb5FUK+g+brqjS2rA== Date: Wed, 4 Jan 2017 19:25:41 +0000 Message-ID: References: , <20170104124927.GT4749@nb4>, In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: authentication-results: ffmpeg.org; dkim=none (message not signed) header.d=none; ffmpeg.org; dmarc=none action=none header.from=hotmail.com; x-incomingtopheadermarker: OriginalChecksum:7CA324E77B39BF13EF90A3E850CE9355F861E0FE6400AAAD14364B445A9428FB; UpperCasedChecksum:7E5C1C7F4FE48071FC59171416A08FEDF2B4F4CB9FF92795B6ED8649FCA6F2A9; SizeAsReceived:7649; Count:39 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [vbbJ0NxylHHLWZm6ZzBVZtVHBEUnlIdJ] x-incomingheadercount: 39 x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; CO1NAM03HT210; 7:/gI6DD0iBAoNnxkljG8aMTjPDe8bYi6a7Qx7Ll9uwR9KuL73JcUqqJW2EWO0meTDhK2aJtNIk+xvWGG3nXiBJqQA2G8fuBmeBVkZ4eoX+n7s1H8eHUnX9AyomnkLItOL9uMoP/TAidjJFN2u1gdidVrP2OD5lgdINZGjaxu7HLOuMoso5qCfBQP84z0o0iawOBZVibJlOIBWhrVQmXB9W1okKj9XhvkdctEcvxK8ayFfceWSzzxxhKtEAtySaeTT5xzPaSWPLidIqzvTMwcafUq6LEKTQMQGXxs6DimK73Bt52ovOZXZn5YVSzDRSgoe7C7/ALeYYkMRT4tqUpjaczYbrakqsW1Sp4cNfa1VlwzyfxSiDU5KQA3r6/n3/dHtu8nR8zlvLJ8Ct/JRrcfmKdT552HYe0gKokCUKPxIQjyt0/JzaKOPDgrsmyOo51hf+4eXaR04JD89XvtXxrb3aw== x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:CO1NAM03HT210; H:MWHPR13MB1678.namprd13.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: d079142c-5f11-40ab-3f2d-08d434d7791b x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(1601124038)(5061506270)(5061507270)(1603103113)(1603101340)(1601125047); SRVR:CO1NAM03HT210; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(444111334)(444112120)(432015012)(102415395)(82015046); SRVR:CO1NAM03HT210; BCL:0; PCL:0; RULEID:; SRVR:CO1NAM03HT210; x-forefront-prvs: 0177904E6B spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Jan 2017 19:25:41.9913 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1NAM03HT210 X-OriginalArrivalTime: 04 Jan 2017 19:25:43.0291 (UTC) FILETIME=[57613CB0:01D266C0] Subject: Re: [FFmpeg-devel] [PATCH] avformat/matroskaenc: Regression fix for invalid MKV headers 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" ==> Revised patch in plain text (the attachment from my previous reply wasn't recognized by patchwork) The following three commits created a regression by writing initially invalid mkv headers: 650e17d88b63b5aca6e0a43483e89e64b0f7d2dd avformat/matroskaenc: write a CRC32 element on Tags 3bcadf822711720ff0f8d14db71ae47cdf97e652 avformat/matroskaenc: write a CRC32 element on Info ee888cfbe777cd2916a3548c750e433ab8f8e6a5 avformat/matroskaenc: postpone writing the Tracks master Symptoms: - You can no longer playback a file that is still processed by ffmpeg, e.g. VLC fails playback - You can no longer stream a file to a client while if is still being processed - Various diagnosing tools show header errors or incomplete headers (e.g. ffprobe, mediainfo, mkvalidator) Note: The symptoms do not apply to completed files or ffmpeg runs that were interrupted with 'q' Cause: The mentioned commits made changes in a way that some header elements are only partially written in mkv_write_header, leaving the header in an invalid state. Only in mkv_write_trailer, these elements are finished correctly, but that does only occur at the end of the process. Regression: Before these commits were applied, mkv headers have always been valid, even before completion of ffmpeg. This has worked reliably over many versions of ffmpeg, to it was an obvious regression. Bugtracker: This issue has been recorded as #5977 which is resolved by this patch Patch: The patch adds a new function 'end_ebml_master_crc32_preliminary' that preliminarily finishes the ebl element without destroying the buffer. The buffer can be used to update the ebml element later during mkv_write_trailer. But most important: mkv_write_header finishes with a valid mkv header again. --- libavformat/avio.h | 12 ++++++++++++ libavformat/aviobuf.c | 17 +++++++++++++++++ libavformat/matroskaenc.c | 28 +++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/libavformat/avio.h b/libavformat/avio.h index b1ce1d1..f2b9a6f 100644 --- a/libavformat/avio.h +++ b/libavformat/avio.h @@ -704,6 +704,18 @@ int avio_closep(AVIOContext **s); int avio_open_dyn_buf(AVIOContext **s); /** + * Return the written size and a pointer to the buffer. + * The AVIOContext stream is left intact. + * The buffer must NOT be freed. + * No padding is added to the buffer. + * + * @param s IO context + * @param pbuffer pointer to a byte buffer + * @return the length of the byte buffer + */ +int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer); + +/** * Return the written size and a pointer to the buffer. The buffer * must be freed with av_free(). * Padding of AV_INPUT_BUFFER_PADDING_SIZE is added to the buffer. diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 134d627..bf7e5f8 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1277,6 +1277,23 @@ int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size) return url_open_dyn_buf_internal(s, max_packet_size); } +int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer) +{ + DynBuffer *d; + + if (!s) { + *pbuffer = NULL; + return 0; + } + + avio_flush(s); + + d = s->opaque; + *pbuffer = d->buffer; + + return d->size; +} + int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer) { DynBuffer *d; diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 827d755..27d83a6 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -367,6 +367,28 @@ static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, Matrosk *dyn_cp = NULL; } +/** +* Complete ebml master whithout destroying the buffer, allowing for later updates +*/ +static void end_ebml_master_crc32_preliminary(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, + ebml_master master) +{ + uint8_t *buf, crc[4]; + int size, skip = 0; + + if (pb->seekable) { + + size = avio_get_dyn_buf(*dyn_cp, &buf); + if (mkv->write_crc && mkv->mode != MODE_WEBM) { + skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */ + AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX); + put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc)); + } + avio_write(pb, buf + skip, size - skip); + end_ebml_master(pb, master); + } +} + static void put_xiph_size(AVIOContext *pb, int size) { ffio_fill(pb, 255, size / 255); @@ -1309,7 +1331,7 @@ static int mkv_write_tracks(AVFormatContext *s) } if (pb->seekable && !mkv->is_live) - put_ebml_void(pb, avio_tell(mkv->tracks_bc)); + end_ebml_master_crc32_preliminary(pb, &mkv->tracks_bc, mkv, mkv->tracks_master); else end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, mkv->tracks_master); @@ -1554,7 +1576,7 @@ static int mkv_write_tags(AVFormatContext *s) if (mkv->tags.pos) { if (s->pb->seekable && !mkv->is_live) - put_ebml_void(s->pb, avio_tell(mkv->tags_bc)); + end_ebml_master_crc32_preliminary(s->pb, &mkv->tags_bc, mkv, mkv->tags); else end_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, mkv->tags); } @@ -1811,7 +1833,7 @@ static int mkv_write_header(AVFormatContext *s) } } if (s->pb->seekable && !mkv->is_live) - put_ebml_void(s->pb, avio_tell(pb)); + end_ebml_master_crc32_preliminary(s->pb, &mkv->info_bc, mkv, mkv->info); else end_ebml_master_crc32(s->pb, &mkv->info_bc, mkv, mkv->info); pb = s->pb;