Jon Olick
  • Home
  • Presentations
  • Publications
  • Patents
  • Videos
  • Code
  • Games
  • Art
  • Blogspot
  • Twitter
  • WikiCoder
  • Contact
  • Links
  • Home
  • Presentations
  • Publications
  • Patents
  • Videos
  • Code
  • Games
  • Art
  • Blogspot
  • Twitter
  • WikiCoder
  • Contact
  • Links

STB style MPEG video writer

9/25/2016

5 Comments

 
Announcing my STB style 256 lines of code MPEG1/2 writer!

In this initial release the features are:
1) 256 lines of C code (single file)
2) no memory allocations
3) public domain
4) patent free (as far as I know)

There is a lot left to be done, notably the encoding of P frames and audio. However, this is very useful as is for a drop-in MPEG writer in any project.

Basic Usage:
 FILE *fp = fopen("foo.mpg", "wb");
jo_write_mpeg(fp, frame0_rgbx, width, height, 60);
jo_write_mpeg(fp, frame1_rgbx, width, height, 60);
 jo_write_mpeg(fp, frame2_rgbx, width, height, 60);
 //  ...
 fclose(fp);

Some notes that this writer takes advantage of the fact that MPEG1/2 is designed so that you can literally concatenate files together to combine movies. Technically each frame is its own movie (until p-frames are implemented, then a set of frames would be its own movie).

Some video players mess up here and don't decode correctly, but MPlayer, SMPlayer, FFMpeg and others work correctly. So this is great as a quick intermediate format!

Have fun and code responsibly! (j/k)


jo_mpeg.cpp
File Size: 8 kb
File Type: cpp
Download File

5 Comments
Szwagier
5/8/2017 08:16:50 am

Is there any possibility to improve quality of the video? It is being far more stronger copressed then I need for my purpose. Even lossless format would be great.

Reply
angros47
8/14/2019 03:59:44 pm

Is there a way to add audio to it?

Reply
Janelle S link
12/14/2020 07:56:03 am

Hi thanks for posting this

Reply
niXman
3/12/2021 01:19:25 am

thank you very much for your work, but I have a question.

I changed your code so it not write to a file but to a previously allocated memory.
next I write this array to UDP socket and view the video-stream with a player (vlc).
but the problem is the player plays for a few seconds and then the video freezes.

my question is, perhaps I need to change something in your code in order for the player to play such a stream?

// pseudocode
while () {
unsigned char *src = malloc(w*h*sizeof(uint32_t));
SeekCam_GetDisplayImage(src, w, h);

unsigned char *dst = malloc(w*h*3);
jo_write_mpeg(dst, src, w, h, 24);

write(udpSock, dst, w*h*3);
}

but if I will write the `dst` into file - the file will be played successfully.
ideas?

Reply
FrostKiwi link
5/7/2022 05:42:24 am

Really happy this exists <3
Converted it to C here: https://github.com/FrostKiwi/treasurechest/blob/main/README.md#jo_mpeg-converted-to-c

Reply



Leave a Reply.

    Archives

    November 2021
    October 2021
    September 2021
    April 2021
    February 2021
    January 2021
    December 2020
    June 2020
    May 2020
    April 2020
    November 2019
    April 2019
    August 2018
    April 2017
    March 2017
    January 2017
    November 2016
    October 2016
    September 2016
    January 2016
    March 2015
    August 2013
    July 2013
    December 2012

    Categories

    All
    Compression
    Dxt

    RSS Feed