[HN Gopher] Video codec in 100 lines of Rust
       ___________________________________________________________________
        
       Video codec in 100 lines of Rust
        
       Author : kevmo314
       Score  : 127 points
       Date   : 2022-12-19 17:50 UTC (5 hours ago)
        
 (HTM) web link (blog.tempus-ex.com)
 (TXT) w3m dump (blog.tempus-ex.com)
        
       | dr-ando wrote:
       | Funnily enough I recently released 0.1.0 of "less-avc" a pure
       | Rust H.264 (AVC) video encoder: https://github.com/strawlab/less-
       | avc/ . For now it only implements a lossless I PCM encoder but
       | supports a few features I need such as high bit depth. If anyone
       | has a codec-writing itch they want to scratch, I would welcome
       | work towards the compression algorithms H.264 supports: context-
       | adaptive variable-length coding (CAVLC) and context-adaptive
       | binary arithmetic coding (CABAC). Also I'm happy for constructive
       | criticism or questions on this library. I think it is fairly
       | idiomatic, and no `unsafe`, rust. While H.264 is an older codec
       | now, as far as I can tell, this also means any patents on it are
       | about to run out and it is very widely supported.
        
       | pcwalton wrote:
       | This is really an image codec, isn't it? Since it doesn't have
       | any temporal compression capabilities.
       | 
       | It's interesting to see how well such a simple technique
       | performs. I wonder what would happen if you added trivial
       | temporal compression by simply subtracting the color values of
       | the previous frame from the next and encoding the residual. How
       | would that perform?
        
         | sjsdaiuasgdia wrote:
         | Why would temporal compression be a necessary requirement to be
         | called a video codec?
         | 
         | Quite a few codecs in the "intra-frame only" section of this
         | Wikipedia list, and that section is within the "Video
         | compression formats" section:
         | 
         | https://en.wikipedia.org/wiki/List_of_codecs#Intra-frame-onl...
        
           | IshKebab wrote:
           | It's a bit debatable but he definitely only did the image
           | coding part of the video codec. All of those listed formats
           | also support the metadata required for video.
           | 
           | I was certainly expecting some motion coding.
        
           | a-dub wrote:
           | some early implementations of mpeg-1 compressors only
           | supported I frames. amusingly, this is still a valid mpeg-1
           | bitstream.
        
         | pornel wrote:
         | A simple delta between frames wouldn't perform well if there
         | was any camera movement: you'd pay for every edge twice.
         | 
         | Instead of working with a delta, conditionally using previous
         | frame as prediction source could work (e.g. if pixel A was
         | closer to previous frame's A than to current frame's B, predict
         | from previous frame's X). Or you could signal prediction source
         | explicitly per block or with RLE. Ideally you'd do motion
         | compensation, but doing that precisely enough for a lossless
         | compressor is more than 100 lines.
        
         | vkaku wrote:
         | I-P-B.
         | 
         | Some video formats only go I. Then there's not a lot different
         | between images and video, as far as editing goes. Decoding for
         | end user transportation has a lot more going on, but one has to
         | start somewhere.
         | 
         | Anyway - I think that this kind of work is a great starter and
         | gets more people interested in this.
        
       | Lerc wrote:
       | Naive Image and video codecs are quite fun to make, I have done a
       | bunch of them over the years and it's quite easy to get within
       | cooee of established formats. And even surpass them under certain
       | conditions. I made a lossy image format that achieves 20-25 PSNR
       | at around 200:1 compression, which is better than most lossy
       | formats because that's in a quality/data-size that most image
       | formats consider out of scope.
       | 
       | QOI https://qoiformat.org/ is a good example of a practically
       | useful simple format.
       | 
       | It's still quite a leap to get to the best new codecs, suddenly
       | you are in a world of head hurty math.
       | 
       | It's also worth noting that it is easy to beat the old formats
       | all-round with off-the-shelf parts, both JPEG and PNG can be
       | bested by changing the outer level of compression for something
       | that was invented after the formats were made. For instance using
       | LZMA or zstd as the final stage. Quite often that's enough to put
       | them on a par with more radically different newer formats.
        
         | repple wrote:
         | within cooee : within hailing distance : not unapproachable
        
       | adamnemecek wrote:
       | (2021)
        
       | kevmo314 wrote:
       | I discovered this cool guide while looking for more resources for
       | my own codec from scratch project:
       | https://github.com/kevmo314/codec-from-scratch
        
       ___________________________________________________________________
       (page generated 2022-12-19 23:00 UTC)