/* SPDX-License-Identifier: BSD-2-Clause */ /* * bitstream_writer.h — MSB-first bit packer with Exp-Golomb coding. * * Used by h264_nal_synth to emit AnnexB SPS / PPS NAL units the * daemon prepends to libva-provided slice data before feeding * libavcodec. The writer is the minimum primitives the H.264 * SPS/PPS RBSPs need: * * put_u(v, n) — n-bit unsigned, MSB first * put_ue(v) — Exp-Golomb unsigned (CAVLC ue(v)) * put_se(v) — Exp-Golomb signed (CAVLC se(v)) * align() — rbsp_trailing_bits: stop_one + zero-pad to byte * * No allocations — the caller hands in a fixed buffer and the writer * tracks (byte, bit) cursor in it. Overruns are detected and made * sticky via an error flag; callers check bsw_overflowed at the end. */ #ifndef DAEDALUS_BITSTREAM_WRITER_H #define DAEDALUS_BITSTREAM_WRITER_H #include #include #include struct bs_writer { uint8_t *buf; size_t cap; size_t pos_bytes; int pos_bit; /* 0..7, MSB-first within the current byte */ bool overflow; }; void bsw_init(struct bs_writer *bs, uint8_t *buf, size_t cap); void bsw_put_u(struct bs_writer *bs, uint32_t v, int n); void bsw_put_ue(struct bs_writer *bs, uint32_t v); void bsw_put_se(struct bs_writer *bs, int32_t v); /* Align to next byte boundary by appending rbsp_trailing_bits: * a single '1' followed by '0's up to the byte boundary. After * this call bsw_bytes is the RBSP length. */ void bsw_align_rbsp(struct bs_writer *bs); /* Byte count of payload written so far. If pos_bit != 0, returns * pos_bytes (incomplete bits are not counted; finalise with * bsw_align_rbsp first). */ size_t bsw_bytes(const struct bs_writer *bs); static inline bool bsw_overflowed(const struct bs_writer *bs) { return bs->overflow; } #endif /* DAEDALUS_BITSTREAM_WRITER_H */