[FFmpeg-devel] [PATCH 1/4] avformat/hashenc: use an array of hashes

Moritz Barsnick barsnick at gmx.net
Sun Aug 11 15:47:53 EEST 2019


Only the first one used currently.

Signed-off-by: Moritz Barsnick <barsnick at gmx.net>
---
 libavformat/hashenc.c | 58 +++++++++++++++++++++++++++----------------
 1 file changed, 36 insertions(+), 22 deletions(-)

diff --git a/libavformat/hashenc.c b/libavformat/hashenc.c
index 06fc085d18..7f83df5cca 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;
 };
@@ -55,18 +55,24 @@ static const AVOption md5_options[] = {
 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
 static int hash_write_header(struct AVFormatContext *s)
 {
+    int res;
     struct HashContext *c = s->priv_data;
-    int res = av_hash_alloc(&c->hash, c->hash_name);
-    if (res < 0)
+    c->hashes = av_malloc_array(1, sizeof(c->hashes));
+    if (!c->hashes)
+        return AVERROR(ENOMEM);
+    res = av_hash_alloc(&c->hashes[0], c->hash_name);
+    if (res < 0) {
+        av_freep(&c->hashes);
         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;
 }

@@ -74,14 +80,15 @@ 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);
+    av_hash_freep(&c->hashes[0]);
+    av_freep(&c->hashes);
     return 0;
 }
 #endif
@@ -145,9 +152,9 @@ 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");
         }
@@ -156,13 +163,19 @@ static void framehash_print_extradata(struct AVFormatContext *s)

 static int framehash_write_header(struct AVFormatContext *s)
 {
+    int res;
     struct HashContext *c = s->priv_data;
-    int res = av_hash_alloc(&c->hash, c->hash_name);
-    if (res < 0)
+    c->hashes = av_malloc_array(1, sizeof(c->hashes));
+    if (!c->hashes)
+        return AVERROR(ENOMEM);
+    res = av_hash_alloc(&c->hashes[0], c->hash_name);
+    if (res < 0) {
+        av_freep(&c->hashes);
         return res;
+    }
     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");
@@ -174,30 +187,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));
         }
     }
@@ -210,7 +223,8 @@ static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
 static int framehash_write_trailer(struct AVFormatContext *s)
 {
     struct HashContext *c = s->priv_data;
-    av_hash_freep(&c->hash);
+    av_hash_freep(&c->hashes[0]);
+    av_freep(&c->hashes);
     return 0;
 }
 #endif
--
2.20.1



More information about the ffmpeg-devel mailing list