dholroyd / h264-reader

Rust reader for H264 bitsream syntax
Apache License 2.0
72 stars 25 forks source link

`impl Debug for Context`; `decode_avcc` example #62

Closed scottlamb closed 8 months ago

scottlamb commented 8 months ago

The new example is useful e.g. here: https://github.com/scottlamb/moonfire-nvr/issues/302#issuecomment-1892796704

Use a private helper to represent param set maps that impls Debug.

Along the way, switch the representation to let the alloc grow as necessary, rather than pre-allocating 32 slots. In the common case when only SPS 0 and PPS 0 are filled, the Vec capacity seems to be size 4, which means the allocations are 4*232=928 bytes and 4*72=288 bytes rather than 32*232=7,424 bytes and 32*72=2,304 bytes, which seems like a worthwhile savings for no real effort.

I played with a Box<[Option[T]]> to have Context be 32 bytes rather than 48, but resize_with overallocates capacity, and then into_boxed_slice does an extra copy to undo that, and writing more code to avoid that felt like premature optimization.

I also considered an [Option<Box<T>>; 32] but having Context take 2*32*8=512 bytes on the stack struck me as potentially problematic.

dholroyd commented 8 months ago

Thanks!

I am not sure of the meaning of number on the left in the "4232=928 bytes" notation you used :sweat_smile:

The size optimisation looks good nonetheless.

scottlamb commented 8 months ago

I am not sure of the meaning of number on the left in the "4232=928 bytes" notation you used 😅

Oops, markdown * fail. Edited my comment above. Thanks for the review!