From patchwork Thu Sep 12 09:23:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Moritz Barsnick X-Patchwork-Id: 15039 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 EA6EB44A18B for ; Thu, 12 Sep 2019 12:23:26 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D61E2688124; Thu, 12 Sep 2019 12:23:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4E36C688120 for ; Thu, 12 Sep 2019 12:23:18 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1568280197; bh=LxHpAxVmBe1O3e55Z03sO8w0cRCq7y3cUmocKjM/kwY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=atEA6V90BodaSGYZ9gWD7otDHKT1B5cQbm8OKQZsK3ndOkQTfAMOxwZ2zHYhFWvwO VJh9Ppg1fRS8HMppSxqMT/HtoCeeKOA4ZVP4Fvm39PlZcerV7aLNOt/9g/Yvshm93f Zm9kCXPhpbQikuh92OfwxBWqhSX7dGbX10P0gYAI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from paradise.fritz.box ([89.182.225.50]) by mail.gmx.com (mrgmx001 [212.227.17.190]) with ESMTPSA (Nemesis) id 0MYwVv-1hdODO2JTn-00Vezk; Thu, 12 Sep 2019 11:23:17 +0200 From: Moritz Barsnick To: FFmpeg development discussions and patches Date: Thu, 12 Sep 2019 11:23:05 +0200 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:OoWD2P7iyunA6/BGrDqUoDvo36XKboFBmNh2toHZFKaCW9HjXUZ GiGiKEG4Qs45NvJVNz67nbq6UaAEiRzg1OLAucJ0Z/pWh/xvWbQAw/sYpHqQbn7J1AqCFrq KxIP/tqmjmmzCd/bBJKFgVPqcLW/gsE3qTvjngwRy4giJqI3p64QZ2/Zj3tZkh1yfNHcnBS MTufXFBZKRBpECJsH1ZZw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1; V03:K0:VEPhvHuuf9U=:vKQUJKRWS8vLLRA4XmFCme uPAFVlJtDjKr/YctBiqiNoEyjCUgPkgDJOA7ifbM+OP0TRjonDKtl9I8uGwYpbeBuGYRpP3sl fBlXQNyw2lel2VLAc38nCCFpUfI4oG/q5F1sGpO17F4jubcZjt5AZJ/FllZ4MnqSY0z8WnlZU R4pqXfaXenhOCIDQLCW9U2i5sE0ArfkWt+hbEu9GMCHgYU6wnYnfTKLms48aTzYjyTaBzSsvS DTzGu9hoaVQo13IyNjnVW0Xq9Ep+H/aUjJocICxsLOA+XfSGpJramN5csUzOWcOMnIXzSvKaT BNGHOgWFrdOkUY6zT9o5waXzAsQTJA9rDHYvcIhS/xJ/NO1x9gg+M7Ocqlj5Mk5J/YjqtOQk4 Js/sdkMPkrUZfyP0kKwVwVbG1swYzPrkF4qL5t6ZhLMh9PIDYN2FNpJtE1p9n3pqYGCOY2HxR Io8KrnhCGEaNFDSfLu/luJNJoNrdWq5kBCUUjMdN8NHCZVHPocxZuvk7n1Zsu6MPG+foJzIWX zTPjKVRIMUV+ZdeaYP8OWqDkDvXKRidgCLIw6i3Z6SXZydetY2bpLQcQVNVGNVKEHSLlH/Cy/ BI4MQaFhfvAGgd82vi2njNr5cwBuJiuylWSnRWEWBzYUIC9tAfEkal09/Se0p2wRvlfxNAfyL I9Iol20Xd+HgsqMPiKFvSbFFuurzBguffg+883jupDih8eWXj67zft5XxzCcAxhTKpc1AvRIE 80x33HyuKZBp0dF20yb1kNmmx+m1+8EzhRsbkPfNtkyjFAhJGy9Im1a2AeynhhJjEVN6O3CDW Gz7MslRtWfYbRPX9xAq3YJOQTNDAdRYOlci78SnDKOAhJr96giza7jj345ri+nbUGhmfXVysg zK4iqVVzcYkZ6OXm5tTEhgorQjauORTbfgSmd5RHeJctDH9NzLsLVSwdmWEvTbiBDEwiEr1Bi o4nTXc2qgjUVipJwOFsWEzquGfdqRE/BpxPgZ656vnDv01HRedlkPXdq0POMvpXoCDOgWJsiX UJCOlt0iOzPffoHqpl47ifFCHk2bXd8C918dJU3J1pYoXCNkNuWQ0OvwSNJsFD4VPnUBt/9Qr ihkE5fhJR7d6fcXAxlUYALTneqp3SYWWkAjP4cTpzT9i5oQTe6Nf1qpfhSX9+RXljbFeyJPi5 jACMZE/01cLUvIyAKer01Ug7oblL82zR/x8a3xmyE8srVRxQ32boGQ+ewP0o8vVJ9BGXNZkjK c49ODgXrJTZbiVfIOi4CKvtivLfLEdCGHobiaMnVfOgVkvkeHFDr5kIx6MBc= Subject: [FFmpeg-devel] [PATCH v3 2/3] avformat/hashenc: use an array of hashes 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: Moritz Barsnick Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Only the first element of the array is used currently, the other elements are in preparation for a new muxer calculating multiple hashes. Also move alloc/init code from the write_header() functions to dedicated init() functions, and the cleanup code from the write_trailer() functions to dedicated deinit() functions. hash_free() and framehash_free() turn out to be identical here, but will differ in the subsequent commit, so they are not consolidated. Signed-off-by: Moritz Barsnick --- libavformat/hashenc.c | 82 ++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 28 deletions(-) -- 2.20.1 diff --git a/libavformat/hashenc.c b/libavformat/hashenc.c index 210bfdea0e..a9be6d0c81 100644 --- a/libavformat/hashenc.c +++ b/libavformat/hashenc.c @@ -29,7 +29,7 @@ struct HashContext { const AVClass *avclass; - struct AVHashContext *hash; + struct AVHashContext **hashes; char *hash_name; int format_version; }; @@ -72,20 +72,24 @@ static const AVOption framemd5_options[] = { #endif #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER -static int hash_write_header(struct AVFormatContext *s) +static int hash_init(struct AVFormatContext *s) { + int res; struct HashContext *c = s->priv_data; - int res = av_hash_alloc(&c->hash, c->hash_name); + c->hashes = av_mallocz_array(1, sizeof(c->hashes)); + if (!c->hashes) + return AVERROR(ENOMEM); + res = av_hash_alloc(&c->hashes[0], c->hash_name); if (res < 0) return res; - av_hash_init(c->hash); + av_hash_init(c->hashes[0]); return 0; } static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt) { struct HashContext *c = s->priv_data; - av_hash_update(c->hash, pkt->data, pkt->size); + av_hash_update(c->hashes[0], pkt->data, pkt->size); return 0; } @@ -93,16 +97,23 @@ static int hash_write_trailer(struct AVFormatContext *s) { struct HashContext *c = s->priv_data; char buf[AV_HASH_MAX_SIZE*2+128]; - snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hash)); + snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[0])); - av_hash_final_hex(c->hash, buf + strlen(buf), sizeof(buf) - strlen(buf)); + av_hash_final_hex(c->hashes[0], buf + strlen(buf), sizeof(buf) - strlen(buf)); av_strlcatf(buf, sizeof(buf), "\n"); avio_write(s->pb, buf, strlen(buf)); avio_flush(s->pb); - av_hash_freep(&c->hash); return 0; } + +static void hash_free(struct AVFormatContext *s) +{ + struct HashContext *c = s->priv_data; + if (c->hashes) + av_hash_freep(&c->hashes[0]); + av_freep(&c->hashes); +} #endif #if CONFIG_HASH_MUXER @@ -119,9 +130,10 @@ AVOutputFormat ff_hash_muxer = { .priv_data_size = sizeof(struct HashContext), .audio_codec = AV_CODEC_ID_PCM_S16LE, .video_codec = AV_CODEC_ID_RAWVIDEO, - .write_header = hash_write_header, + .init = hash_init, .write_packet = hash_write_packet, .write_trailer = hash_write_trailer, + .deinit = hash_free, .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | AVFMT_TS_NEGATIVE, .priv_class = &hashenc_class, @@ -142,9 +154,10 @@ AVOutputFormat ff_md5_muxer = { .priv_data_size = sizeof(struct HashContext), .audio_codec = AV_CODEC_ID_PCM_S16LE, .video_codec = AV_CODEC_ID_RAWVIDEO, - .write_header = hash_write_header, + .init = hash_init, .write_packet = hash_write_packet, .write_trailer = hash_write_trailer, + .deinit = hash_free, .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | AVFMT_TS_NEGATIVE, .priv_class = &md5enc_class, @@ -164,24 +177,34 @@ static void framehash_print_extradata(struct AVFormatContext *s) char buf[AV_HASH_MAX_SIZE*2+1]; avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size); - av_hash_init(c->hash); - av_hash_update(c->hash, par->extradata, par->extradata_size); - av_hash_final_hex(c->hash, buf, sizeof(buf)); + av_hash_init(c->hashes[0]); + av_hash_update(c->hashes[0], par->extradata, par->extradata_size); + av_hash_final_hex(c->hashes[0], buf, sizeof(buf)); avio_write(s->pb, buf, strlen(buf)); avio_printf(s->pb, "\n"); } } } -static int framehash_write_header(struct AVFormatContext *s) +static int framehash_init(struct AVFormatContext *s) { + int res; struct HashContext *c = s->priv_data; - int res = av_hash_alloc(&c->hash, c->hash_name); + c->hashes = av_mallocz_array(1, sizeof(c->hashes)); + if (!c->hashes) + return AVERROR(ENOMEM); + res = av_hash_alloc(&c->hashes[0], c->hash_name); if (res < 0) return res; + return 0; +} + +static int framehash_write_header(struct AVFormatContext *s) +{ + struct HashContext *c = s->priv_data; avio_printf(s->pb, "#format: frame checksums\n"); avio_printf(s->pb, "#version: %d\n", c->format_version); - avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash)); + avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0])); framehash_print_extradata(s); ff_framehash_write_header(s); avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n"); @@ -193,30 +216,30 @@ static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt) struct HashContext *c = s->priv_data; char buf[AV_HASH_MAX_SIZE*2+128]; int len; - av_hash_init(c->hash); - av_hash_update(c->hash, pkt->data, pkt->size); + av_hash_init(c->hashes[0]); + av_hash_update(c->hashes[0], pkt->data, pkt->size); snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ", pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size); len = strlen(buf); - av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len); + av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len); avio_write(s->pb, buf, strlen(buf)); if (c->format_version > 1 && pkt->side_data_elems) { int i, j; avio_printf(s->pb, ", S=%d", pkt->side_data_elems); for (i = 0; i < pkt->side_data_elems; i++) { - av_hash_init(c->hash); + av_hash_init(c->hashes[0]); if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) { for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) { uint32_t data = AV_RL32(pkt->side_data[i].data + j); - av_hash_update(c->hash, (uint8_t *)&data, sizeof(uint32_t)); + av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t)); } } else - av_hash_update(c->hash, pkt->side_data[i].data, pkt->side_data[i].size); + av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size); snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size); len = strlen(buf); - av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len); + av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len); avio_write(s->pb, buf, strlen(buf)); } } @@ -226,11 +249,12 @@ static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt) return 0; } -static int framehash_write_trailer(struct AVFormatContext *s) +static void framehash_free(struct AVFormatContext *s) { struct HashContext *c = s->priv_data; - av_hash_freep(&c->hash); - return 0; + if (c->hashes) + av_hash_freep(&c->hashes[0]); + av_freep(&c->hashes); } #endif @@ -248,9 +272,10 @@ AVOutputFormat ff_framehash_muxer = { .priv_data_size = sizeof(struct HashContext), .audio_codec = AV_CODEC_ID_PCM_S16LE, .video_codec = AV_CODEC_ID_RAWVIDEO, + .init = framehash_init, .write_header = framehash_write_header, .write_packet = framehash_write_packet, - .write_trailer = framehash_write_trailer, + .deinit = framehash_free, .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | AVFMT_TS_NEGATIVE, .priv_class = &framehash_class, @@ -271,9 +296,10 @@ AVOutputFormat ff_framemd5_muxer = { .priv_data_size = sizeof(struct HashContext), .audio_codec = AV_CODEC_ID_PCM_S16LE, .video_codec = AV_CODEC_ID_RAWVIDEO, + .init = framehash_init, .write_header = framehash_write_header, .write_packet = framehash_write_packet, - .write_trailer = framehash_write_trailer, + .deinit = framehash_free, .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | AVFMT_TS_NEGATIVE, .priv_class = &framemd5_class,