drivers/media/pci/tw5864/tw5864-h264.c

Source file repositories/reference/linux-study-clean/drivers/media/pci/tw5864/tw5864-h264.c

File Facts

System
Linux kernel
Corpus path
drivers/media/pci/tw5864/tw5864-h264.c
Extension
.c
Size
6424 bytes
Lines
251
Domain
Driver Families
Bucket
drivers/media
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct bs {
	u8 *buf; /* pointer to buffer beginning */
	u8 *buf_end; /* pointer to buffer end */
	u8 *ptr; /* pointer to current byte in buffer */
	unsigned int bits_left; /* number of available bits in current byte */
};

static void bs_init(struct bs *s, void *buf, int size)
{
	s->buf = buf;
	s->ptr = buf;
	s->buf_end = s->ptr + size;
	s->bits_left = 8;
}

static int bs_len(struct bs *s)
{
	return s->ptr - s->buf;
}

static void bs_write(struct bs *s, int count, u32 bits)
{
	if (s->ptr >= s->buf_end - 4)
		return;
	while (count > 0) {
		if (count < 32)
			bits &= (1 << count) - 1;
		if (count < s->bits_left) {
			*s->ptr = (*s->ptr << count) | bits;
			s->bits_left -= count;
			break;
		}
		*s->ptr = (*s->ptr << s->bits_left) |
			(bits >> (count - s->bits_left));
		count -= s->bits_left;
		s->ptr++;
		s->bits_left = 8;
	}
}

static void bs_write1(struct bs *s, u32 bit)
{
	if (s->ptr < s->buf_end) {
		*s->ptr <<= 1;
		*s->ptr |= bit;
		s->bits_left--;
		if (s->bits_left == 0) {
			s->ptr++;
			s->bits_left = 8;
		}
	}
}

static void bs_write_ue(struct bs *s, u32 val)
{
	if (val == 0) {
		bs_write1(s, 1);
	} else {
		val++;
		bs_write(s, 2 * fls(val) - 1, val);
	}
}

static void bs_write_se(struct bs *s, int val)
{
	bs_write_ue(s, val <= 0 ? -val * 2 : val * 2 - 1);
}

static void bs_rbsp_trailing(struct bs *s)
{
	bs_write1(s, 1);
	if (s->bits_left != 8)
		bs_write(s, s->bits_left, 0x00);
}

/* H.264 headers generation functions */

static int tw5864_h264_gen_sps_rbsp(u8 *buf, size_t size, int width, int height)
{
	struct bs bs, *s;

	s = &bs;
	bs_init(s, buf, size);
	bs_write(s, 8, 0x42); /* profile_idc, baseline */
	bs_write(s, 1, 1); /* constraint_set0_flag */
	bs_write(s, 1, 1); /* constraint_set1_flag */
	bs_write(s, 1, 0); /* constraint_set2_flag */
	bs_write(s, 5, 0); /* reserved_zero_5bits */
	bs_write(s, 8, 0x1e); /* level_idc */
	bs_write_ue(s, 0); /* seq_parameter_set_id */

Annotation

Implementation Notes