summaryrefslogtreecommitdiff
path: root/libavutil
diff options
context:
space:
mode:
authorJonathan Campbell <jonathan@castus.tv>2016-09-03 03:29:29 -0700
committerMichael Niedermayer <michael@niedermayer.cc>2017-01-22 02:28:53 +0100
commit76c5a69e26afbfdfc5b6f538594ea12efa31fc9a (patch)
tree0cef85bf008e1670513082029c727a5b38cc3979 /libavutil
parent0a5add45c7527bbe627899be744f962588e5b2fa (diff)
libavutil: add av_lfg_init_from_data() function
seeds an AVLFG from binary data. Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
Diffstat (limited to 'libavutil')
-rw-r--r--libavutil/lfg.c27
-rw-r--r--libavutil/lfg.h9
-rw-r--r--libavutil/version.h2
3 files changed, 37 insertions, 1 deletions
diff --git a/libavutil/lfg.c b/libavutil/lfg.c
index 08a4f678e2..46b04d2403 100644
--- a/libavutil/lfg.c
+++ b/libavutil/lfg.c
@@ -23,7 +23,9 @@
#include <limits.h>
#include <math.h>
#include "lfg.h"
+#include "crc.h"
#include "md5.h"
+#include "error.h"
#include "intreadwrite.h"
#include "attributes.h"
@@ -58,3 +60,28 @@ void av_bmg_get(AVLFG *lfg, double out[2])
out[0] = x1 * w;
out[1] = x2 * w;
}
+
+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length) {
+ unsigned int beg, end, segm;
+ const AVCRC *avcrc;
+ uint32_t crc = 1;
+
+ /* avoid integer overflow in the loop below. */
+ if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL);
+
+ c->index = 0;
+ avcrc = av_crc_get_table(AV_CRC_32_IEEE); /* This can't fail. It's a well-defined table in crc.c */
+
+ /* across 64 segments of the incoming data,
+ * do a running crc of each segment and store the crc as the state for that slot.
+ * this works even if the length of the segment is 0 bytes. */
+ beg = 0;
+ for (segm = 0;segm < 64;segm++) {
+ end = (((segm + 1) * length) / 64);
+ crc = av_crc(avcrc, crc, data + beg, end - beg);
+ c->state[segm] = (unsigned int)crc;
+ beg = end;
+ }
+
+ return 0;
+}
diff --git a/libavutil/lfg.h b/libavutil/lfg.h
index ec90562cf2..03f779ad8a 100644
--- a/libavutil/lfg.h
+++ b/libavutil/lfg.h
@@ -22,6 +22,8 @@
#ifndef AVUTIL_LFG_H
#define AVUTIL_LFG_H
+#include <stdint.h>
+
typedef struct AVLFG {
unsigned int state[64];
int index;
@@ -30,6 +32,13 @@ typedef struct AVLFG {
void av_lfg_init(AVLFG *c, unsigned int seed);
/**
+ * Seed the state of the ALFG using binary data.
+ *
+ * Return value: 0 on success, negative value (AVERROR) on failure.
+ */
+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length);
+
+/**
* Get the next random unsigned 32-bit number using an ALFG.
*
* Please also consider a simple LCG like state= state*1664525+1013904223,
diff --git a/libavutil/version.h b/libavutil/version.h
index 9f8c4c2364..2e83ef279e 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 55
-#define LIBAVUTIL_VERSION_MINOR 43
+#define LIBAVUTIL_VERSION_MINOR 44
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \