summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Burakov <mburakov@mailbox.org>2023-05-01 17:43:43 +0200
committerMikhail Burakov <mburakov@mailbox.org>2023-05-01 17:43:43 +0200
commit9546ab4acf17ea6a81f058eb28b8a72e1174a671 (patch)
treebe80fc2aa4d8c857a64c6cb3bbb9ee35470a38aa
parent68fbbd6e8a10bc034dbc003392e536cd898806bc (diff)
Add simplistic bitstream writer implementation
-rw-r--r--bitstream.c59
-rw-r--r--bitstream.h34
2 files changed, 93 insertions, 0 deletions
diff --git a/bitstream.c b/bitstream.c
new file mode 100644
index 0000000..1f3242e
--- /dev/null
+++ b/bitstream.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2023 Mikhail Burakov. This file is part of streamer.
+ *
+ * streamer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * streamer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with streamer. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "bitstream.h"
+
+void BitstreamAppend(struct Bitstream* bitstream, size_t size, uint32_t bits) {
+ uint8_t* ptr = (uint8_t*)bitstream->data + bitstream->size / 8;
+ size_t vacant_bits = 8 - bitstream->size % 8;
+ *ptr &= ~0 << vacant_bits;
+
+ if (vacant_bits >= size) {
+ *ptr |= bits << (vacant_bits - size);
+ bitstream->size += size;
+ return;
+ }
+
+ *ptr |= bits >> (size - vacant_bits);
+ bitstream->size += vacant_bits;
+ BitstreamAppend(bitstream, size - vacant_bits, bits);
+}
+
+void BitstreamAppendUE(struct Bitstream* bitstream, uint32_t bits) {
+ size_t size = 0;
+ uint32_t dummy = ++bits;
+ while (dummy) {
+ dummy >>= 1;
+ size++;
+ }
+ BitstreamAppend(bitstream, size - 1, 0);
+ BitstreamAppend(bitstream, size, bits);
+}
+
+void BitstreamAppendSE(struct Bitstream* bitstream, int32_t bits) {
+ BitstreamAppendUE(
+ bitstream, bits <= 0 ? (uint32_t)(-2 * bits) : (uint32_t)(2 * bits - 1));
+}
+
+void BitstreamByteAlign(struct Bitstream* bitstream) {
+ uint8_t* ptr = (uint8_t*)bitstream->data + bitstream->size / 8;
+ size_t vacant_bits = 8 - bitstream->size % 8;
+ if (vacant_bits == 8) return;
+
+ *ptr &= ~0 << vacant_bits;
+ bitstream->size += vacant_bits;
+}
diff --git a/bitstream.h b/bitstream.h
new file mode 100644
index 0000000..87edec3
--- /dev/null
+++ b/bitstream.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 Mikhail Burakov. This file is part of streamer.
+ *
+ * streamer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * streamer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with streamer. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef STREAMER_BITSTREAM_H_
+#define STREAMER_BITSTREAM_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+struct Bitstream {
+ void* data;
+ size_t size;
+};
+
+void BitstreamAppend(struct Bitstream* bitstream, size_t size, uint32_t bits);
+void BitstreamAppendUE(struct Bitstream* bitstream, uint32_t bits);
+void BitstreamAppendSE(struct Bitstream* bitstream, int32_t bits);
+void BitstreamByteAlign(struct Bitstream* bitstream);
+
+#endif // STREAMER_BITSTREAM_H_