]> git.sesse.net Git - ffmpeg/commitdiff
libavutil: add av_lfg_init_from_data() function
authorJonathan Campbell <jonathan@castus.tv>
Sat, 3 Sep 2016 10:29:29 +0000 (03:29 -0700)
committerMichael Niedermayer <michael@niedermayer.cc>
Sun, 22 Jan 2017 01:28:53 +0000 (02:28 +0100)
seeds an AVLFG from binary data.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
doc/APIchanges
libavutil/lfg.c
libavutil/lfg.h
libavutil/version.h

index 98f8076f4731ae0600bcb289d12184a9a6748009..f5f7e0c07f3f80955315e8a28d102bcb4d538745 100644 (file)
@@ -15,6 +15,9 @@ libavutil:     2015-08-28
 
 API changes, most recent first:
 
+2017-01-22 - xxxxxxx - lavu 55.44.100 - lfg.h
+  Add av_lfg_init_from_data().
+
 2017-01-xx - xxxxxxx - lavc 57.74.100 - vaapi.h
   Deprecate struct vaapi_context and the vaapi.h installed header.
   Callers should set AVCodecContext.hw_frames_ctx instead.
index 08a4f678e2e89727c39ad8940f10b37a65dc3919..46b04d24039f416dd84a6668c65cc323eaa6b6c2 100644 (file)
@@ -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;
+}
index ec90562cf24d38d815b9c166bcc4d2e1b6eed170..03f779ad8a4d2957bc72447c7ca17b2863120338 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef AVUTIL_LFG_H
 #define AVUTIL_LFG_H
 
+#include <stdint.h>
+
 typedef struct AVLFG {
     unsigned int state[64];
     int index;
@@ -29,6 +31,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.
  *
index 9f8c4c2364736451bf8192b47b6930fca63cfd3e..2e83ef279e4078593814bd7708aecab0d88f82e8 100644 (file)
@@ -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, \