[FFmpeg-cvslog] avutil: Add functions for allocating opaque contexts for algorithms

Martin Storsjö git at videolan.org
Fri Oct 12 14:59:04 CEST 2012


ffmpeg | branch: master | Martin Storsjö <martin at martin.st> | Thu Oct 11 15:08:04 2012 +0300| [9a92aea27bad2f5603ca85e0d0716c679a6b686c] | committer: Martin Storsjö

avutil: Add functions for allocating opaque contexts for algorithms

The current API where the plain size is exposed is not of much
use - in most cases it is allocated dynamically anyway.

If allocated e.g. on the stack via an uint8_t array, there's no
guarantee that the struct's members are aligned properly (unless
the array is overallocated and the opaque pointer within it
manually aligned to some unspecified alignment).

Signed-off-by: Martin Storsjö <martin at martin.st>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9a92aea27bad2f5603ca85e0d0716c679a6b686c
---

 doc/APIchanges      |    4 ++++
 libavutil/aes.c     |    7 +++++++
 libavutil/aes.h     |   12 +++++++++++-
 libavutil/md5.c     |    8 ++++++++
 libavutil/md5.h     |    8 +++++++-
 libavutil/sha.c     |    8 ++++++++
 libavutil/sha.h     |   12 +++++++++++-
 libavutil/tree.c    |    7 +++++++
 libavutil/tree.h    |   12 +++++++++++-
 libavutil/version.h |    5 ++++-
 10 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 3e93354..81a2266 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,10 @@ libavutil:     2011-04-18
 
 API changes, most recent first:
 
+2012-10-xx - xxxxxxx - lavu 51.43.0 - aes.h, md5.h, sha.h, tree.h
+  Add functions for allocating the opaque contexts for the algorithms,
+  deprecate the context size variables.
+
 2012-10-xx - xxxxxxx - lavf 54.18.0 - avio.h
   Add avio_closep to complement avio_close.
 
diff --git a/libavutil/aes.c b/libavutil/aes.c
index 6803c71..4656a48 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -39,7 +39,14 @@ typedef struct AVAES {
     int rounds;
 } AVAES;
 
+#if FF_API_CONTEXT_SIZE
 const int av_aes_size= sizeof(AVAES);
+#endif
+
+struct AVAES *av_aes_alloc(void)
+{
+    return av_mallocz(sizeof(struct AVAES));
+}
 
 static const uint8_t rcon[10] = {
   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
diff --git a/libavutil/aes.h b/libavutil/aes.h
index cf7b462..edff275 100644
--- a/libavutil/aes.h
+++ b/libavutil/aes.h
@@ -23,17 +23,27 @@
 
 #include <stdint.h>
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_aes AES
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_aes_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_aes_size;
+#endif
 
 struct AVAES;
 
 /**
+ * Allocate an AVAES context.
+ */
+struct AVAES *av_aes_alloc(void);
+
+/**
  * Initialize an AVAES context.
  * @param key_bits 128, 192 or 256
  * @param decrypt 0 for encryption, 1 for decryption
diff --git a/libavutil/md5.c b/libavutil/md5.c
index ca0e598..93a91d7 100644
--- a/libavutil/md5.c
+++ b/libavutil/md5.c
@@ -34,6 +34,7 @@
 #include "bswap.h"
 #include "intreadwrite.h"
 #include "md5.h"
+#include "mem.h"
 
 typedef struct AVMD5{
     uint64_t len;
@@ -41,7 +42,14 @@ typedef struct AVMD5{
     uint32_t ABCD[4];
 } AVMD5;
 
+#if FF_API_CONTEXT_SIZE
 const int av_md5_size = sizeof(AVMD5);
+#endif
+
+struct AVMD5 *av_md5_alloc(void)
+{
+    return av_mallocz(sizeof(struct AVMD5));
+}
 
 static const uint8_t S[4][4] = {
     { 7, 12, 17, 22 },  /* round 1 */
diff --git a/libavutil/md5.h b/libavutil/md5.h
index c5b858a..29e4e7c 100644
--- a/libavutil/md5.h
+++ b/libavutil/md5.h
@@ -23,16 +23,22 @@
 
 #include <stdint.h>
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_md5 MD5
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_md5_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_md5_size;
+#endif
 
 struct AVMD5;
 
+struct AVMD5 *av_md5_alloc(void);
 void av_md5_init(struct AVMD5 *ctx);
 void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, const int len);
 void av_md5_final(struct AVMD5 *ctx, uint8_t *dst);
diff --git a/libavutil/sha.c b/libavutil/sha.c
index cbe1608..d583191 100644
--- a/libavutil/sha.c
+++ b/libavutil/sha.c
@@ -26,6 +26,7 @@
 #include "bswap.h"
 #include "sha.h"
 #include "intreadwrite.h"
+#include "mem.h"
 
 /** hash context */
 typedef struct AVSHA {
@@ -37,7 +38,14 @@ typedef struct AVSHA {
     void     (*transform)(uint32_t *state, const uint8_t buffer[64]);
 } AVSHA;
 
+#if FF_API_CONTEXT_SIZE
 const int av_sha_size = sizeof(AVSHA);
+#endif
+
+struct AVSHA *av_sha_alloc(void)
+{
+    return av_mallocz(sizeof(struct AVSHA));
+}
 
 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
 
diff --git a/libavutil/sha.h b/libavutil/sha.h
index 8350954..4c9a0c9 100644
--- a/libavutil/sha.h
+++ b/libavutil/sha.h
@@ -23,17 +23,27 @@
 
 #include <stdint.h>
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_sha SHA
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_sha_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_sha_size;
+#endif
 
 struct AVSHA;
 
 /**
+ * Allocate an AVSHA context.
+ */
+struct AVSHA *av_sha_alloc(void);
+
+/**
  * Initialize SHA-1 or SHA-2 hashing.
  *
  * @param context pointer to the function context (of size av_sha_size)
diff --git a/libavutil/tree.c b/libavutil/tree.c
index 0e68bb7..55dcbc5 100644
--- a/libavutil/tree.c
+++ b/libavutil/tree.c
@@ -28,7 +28,14 @@ typedef struct AVTreeNode {
     int state;
 } AVTreeNode;
 
+#if FF_API_CONTEXT_SIZE
 const int av_tree_node_size = sizeof(AVTreeNode);
+#endif
+
+struct AVTreeNode *av_tree_node_alloc(void)
+{
+    return av_mallocz(sizeof(struct AVTreeNode));
+}
 
 void *av_tree_find(const AVTreeNode *t, void *key,
                    int (*cmp)(void *key, const void *b), void *next[2])
diff --git a/libavutil/tree.h b/libavutil/tree.h
index 59ea01d..623280f 100644
--- a/libavutil/tree.h
+++ b/libavutil/tree.h
@@ -27,6 +27,9 @@
 #ifndef AVUTIL_TREE_H
 #define AVUTIL_TREE_H
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @addtogroup lavu_tree AVTree
  * @ingroup lavu_data
@@ -40,7 +43,14 @@
 
 
 struct AVTreeNode;
-extern const int av_tree_node_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_tree_node_size;
+#endif
+
+/**
+ * Allocate an AVTreeNode.
+ */
+struct AVTreeNode *av_tree_node_alloc(void);
 
 /**
  * Find an element.
diff --git a/libavutil/version.h b/libavutil/version.h
index c802bfe..8562835 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -37,7 +37,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 51
-#define LIBAVUTIL_VERSION_MINOR 42
+#define LIBAVUTIL_VERSION_MINOR 43
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
@@ -76,6 +76,9 @@
 #ifndef FF_API_PIX_FMT
 #define FF_API_PIX_FMT                  (LIBAVUTIL_VERSION_MAJOR < 52)
 #endif
+#ifndef FF_API_CONTEXT_SIZE
+#define FF_API_CONTEXT_SIZE             (LIBAVUTIL_VERSION_MAJOR < 52)
+#endif
 
 /**
  * @}



More information about the ffmpeg-cvslog mailing list