Hacker News new | comments | show | ask | jobs | submit login
Render Multimedia in Pure C (nullprogram.com)
126 points by signa11 10 months ago | hide | past | web | favorite | 15 comments



Ah, raw PPM output. Reminds me of this which I wrote some years ago:

    #!/usr/bin/perl
    # Output is a PPM file : try snowflake | xloadimage stdin
    # see http://cam.pm.org/christmasppc-summary.shtml#Pete_Clay for explanation

            $w=                          100
          ;$w*=5;                      ;$W=$w*
         $w;   q--;  $p=atan2(1,0)*  2;3;   @P=
          ("\000"x3)x(    $W);    BEGIN{$";sub
          S{$_.='fhoq'    }sub    K{$_.='s'.((
         $n  ++))   .((    ''    .''   .'{'  ))
        ;};    sub   Y{$_  .=  $_[0   ]};    S;K
       ;Y(''.   '$k/=2;$'. '' .'l/=2}');   S;K;0;
      Y(     '$k/=3;$l/=3;$k+=$j/4}fhoqh{'.     ''
  .'cevq"P6\a$jq$j\a255\a",q@P}');S;K;Y('$g=$c/3;e}');
 S;K;  Y(q.$g=$.  .'c/-3;'    .'e}');S  ;K;Y('$'.  'g'.
 '='    .'$c*2'    .'/3}'      );;s/v    /vag/x;    sub
 p{$P  [$x+$w/2+  int($y)*    $w+$W/2]  ="\377"x3  }sub
  r{$z=$x*cos($t)+$y*sin($t);$y=$x*sin($t)-$y*cos($t);
      $x     =$z};tr/a-z/n-za-m/;s/d/$"/gx;     $x
       ++;$y=   4;eval$_}; s/ /japh/;sub   T{;$a=
        $_[    0];   ;$b=  ((  $a&(   ($a    |(+
         $a  <<1)   )<<    1)    )>>   2)|(  $a
          >>3);if($b){    $_[0    ]=""}else{$_
          [0]="f$a;p;"    }};;    for(1..20000
         *10   ){$_  =int(rand(20))  ;T$_   ;0;
          eval$_}                      u#byPJC
            #JA                          PH#
Ironically on the DOS systems (and early home computers) I learnt on it was really easy to display images from programs as the framebuffer was at a known memory address and you could just write to it. Windowing systems make that a bit harder, and very little of this is really portable.


On MS-DOS the FB was at $A000 and you could address it easily using stuff like mem[] in Pascal.

I remember doing lots of animation and random graphics stuff in DOS using mode 13, the usual stuff at the time, plasma etc. 320 x 200 was more than enough for this kind of fun. Good times, simple times!


It is as easy to setup a framebuffer into a SDL window, working pretty much like in the DOS days.


yeah, SDL rekindled my passion with this kind of stuff, man. It is so easy to set up and deference a simple software-only framebuffer and after you whet your appetite, setting up a hardware accelerated one is also dead simple in SDL2.


I used to love the simplicity of this, though the default of being limited to 64k segments was a bit of a bummer.


Might be a bit offtopic, but you can easily output audio and video at once and then mux them later on:

    ./yourprogram -a >(ffmpeg -f rawaudio ..other audio opts.. -i - ..encoding options.. audio.m4a) -v >(ffmpeg -f rawvideo ..video opts.. -i - ..encoding options.. video.m4v)
    ffmpeg -i audio.m4a -i video.m4v -c:a copy -c:v copy out.mp4
The >() syntax is a bash extension, supported also by zsh, and replaces the argument with a file path to a pipe that's connected to the process within the ()'s stdin. Similarly you can use <() to replace the arg with a pipe connected to the process's stdout. One thing I use it for sometimes is to compare directory contents:

    vimdiff <(ls -1 dir1 | cut -d/ -f2-)<(ls -1 dir2 | cut -d/ -f2-)
There are better tools for this specific task, but for a quick and dirty compare on a machine where you only have vim and bash, it's hard to beat.


I'm returned to the heyday of Sorenson Video 3, Windows Media Video, and RealVideo. =:D Our application was used by most web video professionals at the time - go to a trailer on Apple's site, and it'd almost certainly have been produced by our work.

All C, up to Media Cleaner Pro 4, migrating to C++ with Cleaner 5, with some Altivec (oh, what a joyous SIMD ISA) on the Mac side.


Those who find this interesting are recommended to look at the demoscene --- where realtime procedural graphics and music are the norm, and some groups have gotten very very good at it.


This was one of the only ways to get video working easily 20 years ago. I remember piping ppm files into the Berkeley MPEG_ENCODE [1] and getting out a poorly encoded but animated raytracer output.

[1] http://vis.lbl.gov/NERSC/HowTos/mpeg/help/tools/mpeg_encode....


I did the same for my diploma thesis, where I simulated quantum waveforms on a small compute cluster. Each run would consist of about 30,000 separate simulations with initial conditions, and while they usually only output a few statistics each, there was a debugging switch which dumped the waveform as a PPM after every timestep, so that I could merge them all into a video and assess the quality of the simulation visually.


Really great visualizations of search algorithms - would love to have brief description of technique used for actual graphics/audio output (only 600LOC but my C is craptastic so..)


> brief description of technique used for actual graphics/audio output

That's most of the article. It can be found in the "frame" and "ppm_*" functions in the program.

Nice clean classical C style there in my opinion.


I'm amazed at how slow Quicksort appears in the sorting video.


His 2D structure with pipes makes me wanna write transforms in APL


I was thinking of a similar thing, but in J [1]. J's library or routines (j.dll on windows), can be called easily from C. I am trying to establish a synthesis of my love for J programming with my C coding experience.

  [1] jsoftware.com




Applications are open for YC Winter 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: