cosmopolitan/dsp/mpeg
Jōshin 6e6fc38935
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)

I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.

My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.

It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.

fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 10:38:00 -07:00
..
blockset.h Reduce header complexity 2023-11-28 14:39:42 -08:00
buffer.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
buffer.h Use DNS implementation from Musl Libc 2023-12-28 23:04:35 -08:00
BUILD.mk more modeline errata (#1019) 2023-12-16 23:07:10 -05:00
clamp4int256-core.S Fix more vi modelines (#1006) 2023-12-13 02:28:11 -05:00
demux.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
demux.h Reduce header complexity 2023-11-28 14:39:42 -08:00
idct.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
idct.h Reduce header complexity 2023-11-28 14:39:42 -08:00
macroblock.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
mp2.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
mpeg.h Use DNS implementation from Musl Libc 2023-12-28 23:04:35 -08:00
mpeg1.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
notice.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
plm.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
README.txt Remove trailing whitespace from all files (#497) 2022-07-20 20:31:16 -07:00
slowrgb.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00
video.h Reduce header complexity 2023-11-28 14:39:42 -08:00

PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer
Dominic Szablewski - https://phoboslab.org

-- Synopsis

// This function gets called for each decoded video frame
void my_video_callback(plm_t *plm, plm_frame_t *frame, void *user) {
	// Do something with frame->y.data, frame->cr.data, frame->cb.data
}

// This function gets called for each decoded audio frame
void my_audio_callback(plm_t *plm, plm_samples_t *frame, void *user) {
	// Do something with samples->interleaved
}

// Load a .mpg (MPEG Program Stream) file
plm_t *plm = plm_create_with_filename("some-file.mpg");

// Install the video & audio decode callbacks
plm_set_video_decode_callback(plm, my_video_callback, my_data);
plm_set_audio_decode_callback(plm, my_audio_callback, my_data);


// Decode
do {
	plm_decode(plm, time_since_last_call);
} while (!plm_has_ended(plm));

// All done
plm_destroy(plm);



-- Documentation

This library provides several interfaces to load, demux and decode MPEG video
and audio data. A high-level API combines the demuxer, video & audio decoders
in an easy to use wrapper.

Lower-level APIs for accessing the demuxer, video decoder and audio decoder,
as well as providing different data sources are also available.

Interfaces are written in an object orientet style, meaning you create object
instances via various different constructor functions (plm_*create()),
do some work on them and later dispose them via plm_*destroy().

plm_*		-- the high-level interface, combining demuxer and decoders
plm_buffer_* -- the data source used by all interfaces
plm_demux_*  -- the MPEG-PS demuxer
plm_video_*  -- the MPEG1 Video ("mpeg1") decoder
plm_audio_*  -- the MPEG1 Audio Layer II ("mp2") decoder


This library uses malloc(), realloc() and free() to manage memory. Typically
all allocation happens up-front when creating the interface. However, the
default buffer size may be too small for certain inputs. In these cases plmpeg
will realloc() the buffer with a larger size whenever needed. You can configure
the default buffer size by defining PLM_BUFFER_DEFAULT_SIZE *before*
including this library.

With the high-level interface you have two options to decode video & audio:

1) Use plm_decode() and just hand over the delta time since the last call.
It will decode everything needed and call your callbacks (specified through
plm_set_{video|audio}_decode_callback()) any number of times.

2) Use plm_decode_video() and plm_decode_audio() to decode exactly one
frame of video or audio data at a time. How you handle the synchronization of
both streams is up to you.

If you only want to decode video *or* audio through these functions, you should
disable the other stream (plm_set_{video|audio}_enabled(false))


Video data is decoded into a struct with all 3 planes (Y, Cr, Cb) stored in
separate buffers. You can either convert this to RGB on the CPU (slow) via the
plm_frame_to_rgb() function or do it on the GPU with the following matrix:

mat4 rec601 = mat4(
	1.16438,  0.00000,  1.59603, -0.87079,
	1.16438, -0.39176, -0.81297,  0.52959,
	1.16438,  2.01723,  0.00000, -1.08139,
	0, 0, 0, 1
);
gl_FragColor = vec4(y, cb, cr, 1.0) * rec601;

Audio data is decoded into a struct with either one single float array with the
samples for the left and right channel interleaved, or if the
PLM_AUDIO_SEPARATE_CHANNELS is defined *before* including this library, into
two separate float arrays - one for each channel.

See below for detailed the API documentation.