]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/lfg.c
Merge commit 'a92fd8a06256e71a0be87b03751ec3c2a4a8aa21'
[ffmpeg] / libavutil / lfg.c
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;
+}