History

libavutil: add av_lfg_init_from_data() function

seeds an AVLFG from binary data.

Jonathan Campbell committed 10 years ago

Commit 76c5a691 parent 0a5add44 files · +40 −1Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/lfg.c +27 −0
  3. libavutil/lfg.h +9 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2015-08-28 · 14 unmodified lines
1515
1616 API changes, most recent first:
1717
18+2017-01-22 - xxxxxxx - lavu 55.44.100 - lfg.h
19+ Add av_lfg_init_from_data().
20+
1821 2017-01-xx - xxxxxxx - lavc 57.74.100 - vaapi.h
1922 Deprecate struct vaapi_context and the vaapi.h installed header.
2023 Callers should set AVCodecContext.hw_frames_ctx instead.
libavutil/lfg.c+27−0
@@ -23,7 +23,9 @@ · 22 unmodified lines
2323 #include <limits.h>
2424 #include <math.h>
2525 #include "lfg.h"
26+#include "crc.h"
2627 #include "md5.h"
28+#include "error.h"
2729 #include "intreadwrite.h"
2830 #include "attributes.h"
2931
@@ -58,3 +60,28 @@ void av_bmg_get(AVLFG *lfg, double out[2]) · 28 unmodified lines
5860 out[0] = x1 * w;
5961 out[1] = x2 * w;
6062 }
63+
64+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length) {
65+ unsigned int beg, end, segm;
66+ const AVCRC *avcrc;
67+ uint32_t crc = 1;
68+
69+ /* avoid integer overflow in the loop below. */
70+ if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL);
71+
72+ c->index = 0;
73+ avcrc = av_crc_get_table(AV_CRC_32_IEEE); /* This can't fail. It's a well-defined table in crc.c */
74+
75+ /* across 64 segments of the incoming data,
76+ * do a running crc of each segment and store the crc as the state for that slot.
77+ * this works even if the length of the segment is 0 bytes. */
78+ beg = 0;
79+ for (segm = 0;segm < 64;segm++) {
80+ end = (((segm + 1) * length) / 64);
81+ crc = av_crc(avcrc, crc, data + beg, end - beg);
82+ c->state[segm] = (unsigned int)crc;
83+ beg = end;
84+ }
85+
86+ return 0;
87+}
libavutil/lfg.h+9−0
@@ -22,6 +22,8 @@ · 21 unmodified lines
2222 #ifndef AVUTIL_LFG_H
2323 #define AVUTIL_LFG_H
2424
25+#include <stdint.h>
26+
2527 typedef struct AVLFG {
2628 unsigned int state[64];
2729 int index;
@@ -29,6 +31,13 @@ typedef struct AVLFG { · 1 unmodified line
2931
3032 void av_lfg_init(AVLFG *c, unsigned int seed);
3133
34+/**
35+ * Seed the state of the ALFG using binary data.
36+ *
37+ * Return value: 0 on success, negative value (AVERROR) on failure.
38+ */
39+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length);
40+
3241 /**
3342 * Get the next random unsigned 32-bit number using an ALFG.
3443 *
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
7979 */
8080
8181 #define LIBAVUTIL_VERSION_MAJOR 55
82−#define LIBAVUTIL_VERSION_MINOR 43
82+#define LIBAVUTIL_VERSION_MINOR 44
8383 #define LIBAVUTIL_VERSION_MICRO 100
8484
8585 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t