diff mbox

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

Message ID 26f78761602ecfc977b70a736a2426ab95082e13.1565527501.git.barsnick@gmx.net
State New
Headers show

Commit Message

Moritz Barsnick Aug. 11, 2019, 12:47 p.m. UTC
Only the first one used currently.

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

--
2.20.1

Comments

Nicolas George Aug. 11, 2019, 1:57 p.m. UTC | #1
Moritz Barsnick (12019-08-11):
> Only the first one used currently.
> 
> Signed-off-by: Moritz Barsnick <barsnick@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);

Maybe move all the freing code in a deinit() function?

>          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

Regards,
Moritz Barsnick Aug. 12, 2019, 12:55 p.m. UTC | #2
On Sun, Aug 11, 2019 at 15:57:46 +0200, Nicolas George wrote:
> > +    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);
>
> Maybe move all the freing code in a deinit() function?

Sure, I'll have a look how the other muxers do it.

The code is later allocating hashes av_hash_alloc() in a loop over
nb_streams. If this fails (i.e. out of memory) along the way, do I need
to remember how many succeeded before? (See patch 4/4.) Or can I just
av_hash_freep() each one in the array, regardsless?

Grateful for any other reviews,
thanks,
Moritz
Paul B Mahol Aug. 12, 2019, 1:11 p.m. UTC | #3
On Mon, Aug 12, 2019 at 2:56 PM Moritz Barsnick <barsnick@gmx.net> wrote:

> On Sun, Aug 11, 2019 at 15:57:46 +0200, Nicolas George wrote:
> > > +    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);
> >
> > Maybe move all the freing code in a deinit() function?
>
> Sure, I'll have a look how the other muxers do it.
>
> The code is later allocating hashes av_hash_alloc() in a loop over
> nb_streams. If this fails (i.e. out of memory) along the way, do I need
> to remember how many succeeded before? (See patch 4/4.) Or can I just
> av_hash_freep() each one in the array, regardsless?
>
>
Use freep and make sure that arrray holding other arrays is initialized to
0 upon allocation.


> Grateful for any other reviews,
> thanks,
> Moritz
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox

Patch

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