Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 4267 lines (3443 sloc) 133 KB
/*
PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer
Dominic Szablewski - https://phoboslab.org
-- LICENSE: The MIT License(MIT)
Copyright(c) 2019 Dominic Szablewski
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files(the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-- Synopsis
// Define `PL_MPEG_IMPLEMENTATION` in *one* C/C++ file before including this
// library to create the implementation.
#define PL_MPEG_IMPLEMENTATION
#include "plmpeg.h"
// 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
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 bt601 = 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) * bt601;
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.
Data can be supplied to the high level interface, the demuxer and the decoders
in three different ways:
1. Using plm_create_from_filename() or with a file handle with
plm_create_from_file().
2. Using plm_create_with_memory() and supplying a pointer to memory that
contains the whole file.
3. Using plm_create_with_buffer(), supplying your own plm_buffer_t instance and
periodically writing to this buffer.
When using your own plm_buffer_t instance, you can fill this buffer using
plm_buffer_write(). You can either monitor plm_buffer_get_remaining() and push
data when appropriate, or install a callback on the buffer with
plm_buffer_set_load_callback() that gets called whenever the buffer needs more
data.
A buffer created with plm_buffer_create_with_capacity() is treated as a ring
buffer, meaning that data that has already been read, will be discarded. In
contrast, a buffer created with plm_buffer_create_for_appending() will keep all
data written to it in memory. This enables seeking in the already loaded data.
There should be no need to use the lower level plm_demux_*, plm_video_* and
plm_audio_* functions, if all you want to do is read/decode an MPEG-PS file.
However, if you get raw mpeg1video data or raw mp2 audio data from a different
source, these functions can be used to decode the raw data directly. Similarly,
if you only want to analyze an MPEG-PS file or extract raw video or audio
packets from it, you can use the plm_demux_* functions.
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.
See below for detailed the API documentation.
*/
#ifndef PL_MPEG_H
#define PL_MPEG_H
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
// -----------------------------------------------------------------------------
// Public Data Types
// Object types for the various interfaces
typedef struct plm_t plm_t;
typedef struct plm_buffer_t plm_buffer_t;
typedef struct plm_demux_t plm_demux_t;
typedef struct plm_video_t plm_video_t;
typedef struct plm_audio_t plm_audio_t;
// Demuxed MPEG PS packet
// The type maps directly to the various MPEG-PES start codes. PTS is the
// presentation time stamp of the packet in seconds. Note that not all packets
// have a PTS value, indicated by PLM_PACKET_INVALID_TS.
#define PLM_PACKET_INVALID_TS -1
typedef struct {
int type;
double pts;
size_t length;
uint8_t *data;
} plm_packet_t;
// Decoded Video Plane
// The byte length of the data is width * height. Note that different planes
// have different sizes: the Luma plane (Y) is double the size of each of
// the two Chroma planes (Cr, Cb) - i.e. 4 times the byte length.
// Also note that the size of the plane does *not* denote the size of the
// displayed frame. The sizes of planes are always rounded up to the nearest
// macroblock (16px).
typedef struct {
unsigned int width;
unsigned int height;
uint8_t *data;
} plm_plane_t;
// Decoded Video Frame
// width and height denote the desired display size of the frame. This may be
// different from the internal size of the 3 planes.
typedef struct {
double time;
unsigned int width;
unsigned int height;
plm_plane_t y;
plm_plane_t cr;
plm_plane_t cb;
} plm_frame_t;
// Callback function type for decoded video frames used by the high-level
// plm_* interface
typedef void(*plm_video_decode_callback)
(plm_t *self, plm_frame_t *frame, void *user);
// Decoded Audio Samples
// Samples are stored as normalized (-1, 1) float either interleaved, or if
// PLM_AUDIO_SEPARATE_CHANNELS is defined, in two separate arrays.
// The `count` is always PLM_AUDIO_SAMPLES_PER_FRAME and just there for
// convenience.
#define PLM_AUDIO_SAMPLES_PER_FRAME 1152
typedef struct {
double time;
unsigned int count;
#ifdef PLM_AUDIO_SEPARATE_CHANNELS
float left[PLM_AUDIO_SAMPLES_PER_FRAME];
float right[PLM_AUDIO_SAMPLES_PER_FRAME];
#else
float interleaved[PLM_AUDIO_SAMPLES_PER_FRAME * 2];
#endif
} plm_samples_t;
// Callback function type for decoded audio samples used by the high-level
// plm_* interface
typedef void(*plm_audio_decode_callback)
(plm_t *self, plm_samples_t *samples, void *user);
// Callback function for plm_buffer when it needs more data
typedef void(*plm_buffer_load_callback)(plm_buffer_t *self, void *user);
// -----------------------------------------------------------------------------
// plm_* public API
// High-Level API for loading/demuxing/decoding MPEG-PS data
// Create a plmpeg instance with a filename. Returns NULL if the file could not
// be opened.
plm_t *plm_create_with_filename(const char *filename);
// Create a plmpeg instance with a file handle. Pass TRUE to close_when_done to
// let plmpeg call fclose() on the handle when plm_destroy() is called.
plm_t *plm_create_with_file(FILE *fh, int close_when_done);
// Create a plmpeg instance with a pointer to memory as source. This assumes the
// whole file is in memory. The memory is not copied. Pass TRUE to
// free_when_done to let plmpeg call free() on the pointer when plm_destroy()
// is called.
plm_t *plm_create_with_memory(uint8_t *bytes, size_t length, int free_when_done);
// Create a plmpeg instance with a plm_buffer as source. Pass TRUE to
// destroy_when_done to let plmpeg call plm_buffer_destroy() on the buffer when
// plm_destroy() is called.
plm_t *plm_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done);
// Destroy a plmpeg instance and free all data.
void plm_destroy(plm_t *self);
// Get whether we have headers on all available streams and we can accurately
// report the number of video/audio streams, video dimensions, framerate and
// audio samplerate.
// This returns FALSE if the file is not an MPEG-PS file or - when not using a
// file as source - when not enough data is available yet.
int plm_has_headers(plm_t *self);
// Get or set whether video decoding is enabled. Default TRUE.
int plm_get_video_enabled(plm_t *self);
void plm_set_video_enabled(plm_t *self, int enabled);
// Get the number of video streams (0--1) reported in the system header.
int plm_get_num_video_streams(plm_t *self);
// Get the display width/height of the video stream.
int plm_get_width(plm_t *self);
int plm_get_height(plm_t *self);
// Get the framerate of the video stream in frames per second.
double plm_get_framerate(plm_t *self);
// Get or set whether audio decoding is enabled. Default TRUE.
int plm_get_audio_enabled(plm_t *self);
void plm_set_audio_enabled(plm_t *self, int enabled);
// Get the number of audio streams (0--4) reported in the system header.
int plm_get_num_audio_streams(plm_t *self);
// Set the desired audio stream (0--3). Default 0.
void plm_set_audio_stream(plm_t *self, int stream_index);
// Get the samplerate of the audio stream in samples per second.
int plm_get_samplerate(plm_t *self);
// Get or set the audio lead time in seconds - the time in which audio samples
// are decoded in advance (or behind) the video decode time. Typically this
// should be set to the duration of the buffer of the audio API that you use
// for output. E.g. for SDL2: (SDL_AudioSpec.samples / samplerate)
double plm_get_audio_lead_time(plm_t *self);
void plm_set_audio_lead_time(plm_t *self, double lead_time);
// Get the current internal time in seconds.
double plm_get_time(plm_t *self);
// Get the video duration of the underlying source in seconds.
double plm_get_duration(plm_t *self);
// Rewind all buffers back to the beginning.
void plm_rewind(plm_t *self);
// Get or set looping. Default FALSE.
int plm_get_loop(plm_t *self);
void plm_set_loop(plm_t *self, int loop);
// Get whether the file has ended. If looping is enabled, this will always
// return FALSE.
int plm_has_ended(plm_t *self);
// Set the callback for decoded video frames used with plm_decode(). If no
// callback is set, video data will be ignored and not be decoded. The *user
// Parameter will be passed to your callback.
void plm_set_video_decode_callback(plm_t *self, plm_video_decode_callback fp, void *user);
// Set the callback for decoded audio samples used with plm_decode(). If no
// callback is set, audio data will be ignored and not be decoded. The *user
// Parameter will be passed to your callback.
void plm_set_audio_decode_callback(plm_t *self, plm_audio_decode_callback fp, void *user);
// Advance the internal timer by seconds and decode video/audio up to this time.
// This will call the video_decode_callback and audio_decode_callback any number
// of times. A frame-skip is not implemented, i.e. everything up to current time
// will be decoded.
void plm_decode(plm_t *self, double seconds);
// Decode and return one video frame. Returns NULL if no frame could be decoded
// (either because the source ended or data is corrupt). If you only want to
// decode video, you should disable audio via plm_set_audio_enabled().
// The returned plm_frame_t is valid until the next call to plm_decode_video()
// or until plm_destroy() is called.
plm_frame_t *plm_decode_video(plm_t *self);
// Decode and return one audio frame. Returns NULL if no frame could be decoded
// (either because the source ended or data is corrupt). If you only want to
// decode audio, you should disable video via plm_set_video_enabled().
// The returned plm_samples_t is valid until the next call to plm_decode_audio()
// or until plm_destroy() is called.
plm_samples_t *plm_decode_audio(plm_t *self);
// Seek to the specified time, clamped between 0 -- duration. This can only be
// used when the underlying plm_buffer is seekable, i.e. for files, fixed
// memory buffers or _for_appending buffers.
// If seek_exact is TRUE this will seek to the exact time, otherwise it will
// seek to the last intra frame just before the desired time. Exact seeking can
// be slow, because all frames up to the seeked one have to be decoded on top of
// the previous intra frame.
// If seeking succeeds, this function will call the video_decode_callback
// exactly once with the target frame. If audio is enabled, it will also call
// the audio_decode_callback any number of times, until the audio_lead_time is
// satisfied.
// Returns TRUE if seeking succeeded or FALSE if no frame could be found.
int plm_seek(plm_t *self, double time, int seek_exact);
// Similar to plm_seek(), but will not call the video_decode_callback,
// audio_decode_callback or make any attempts to sync audio.
// Returns the found frame or NULL if no frame could be found.
plm_frame_t *plm_seek_frame(plm_t *self, double time, int seek_exact);
// -----------------------------------------------------------------------------
// plm_buffer public API
// Provides the data source for all other plm_* interfaces
// The default size for buffers created from files or by the high-level API
#ifndef PLM_BUFFER_DEFAULT_SIZE
#define PLM_BUFFER_DEFAULT_SIZE (128 * 1024)
#endif
// Create a buffer instance with a filename. Returns NULL if the file could not
// be opened.
plm_buffer_t *plm_buffer_create_with_filename(const char *filename);
// Create a buffer instance with a file handle. Pass TRUE to close_when_done
// to let plmpeg call fclose() on the handle when plm_destroy() is called.
plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done);
// Create a buffer instance with a pointer to memory as source. This assumes
// the whole file is in memory. The bytes are not copied. Pass 1 to
// free_when_done to let plmpeg call free() on the pointer when plm_destroy()
// is called.
plm_buffer_t *plm_buffer_create_with_memory(uint8_t *bytes, size_t length, int free_when_done);
// Create an empty buffer with an initial capacity. The buffer will grow
// as needed. Data that has already been read, will be discarded.
plm_buffer_t *plm_buffer_create_with_capacity(size_t capacity);
// Create an empty buffer with an initial capacity. The buffer will grow
// as needed. Decoded data will *not* be discarded. This can be used when
// loading a file over the network, without needing to throttle the download.
// It also allows for seeking in the already loaded data.
plm_buffer_t *plm_buffer_create_for_appending(size_t initial_capacity);
// Destroy a buffer instance and free all data
void plm_buffer_destroy(plm_buffer_t *self);
// Copy data into the buffer. If the data to be written is larger than the
// available space, the buffer will realloc() with a larger capacity.
// Returns the number of bytes written. This will always be the same as the
// passed in length, except when the buffer was created _with_memory() for
// which _write() is forbidden.
size_t plm_buffer_write(plm_buffer_t *self, uint8_t *bytes, size_t length);
// Mark the current byte length as the end of this buffer and signal that no
// more data is expected to be written to it. This function should be called
// just after the last plm_buffer_write().
// For _with_capacity buffers, this is cleared on a plm_buffer_rewind().
void plm_buffer_signal_end(plm_buffer_t *self);
// Set a callback that is called whenever the buffer needs more data
void plm_buffer_set_load_callback(plm_buffer_t *self, plm_buffer_load_callback fp, void *user);
// Rewind the buffer back to the beginning. When loading from a file handle,
// this also seeks to the beginning of the file.
void plm_buffer_rewind(plm_buffer_t *self);
// Get the total size. For files, this returns the file size. For all other
// types it returns the number of bytes currently in the buffer.
size_t plm_buffer_get_size(plm_buffer_t *self);
// Get the number of remaining (yet unread) bytes in the buffer. This can be
// useful to throttle writing.
size_t plm_buffer_get_remaining(plm_buffer_t *self);
// Get whether the read position of the buffer is at the end and no more data
// is expected.
int plm_buffer_has_ended(plm_buffer_t *self);
// -----------------------------------------------------------------------------
// plm_demux public API
// Demux an MPEG Program Stream (PS) data into separate packages
// Various Packet Types
static const int PLM_DEMUX_PACKET_PRIVATE = 0xBD;
static const int PLM_DEMUX_PACKET_AUDIO_1 = 0xC0;
static const int PLM_DEMUX_PACKET_AUDIO_2 = 0xC1;
static const int PLM_DEMUX_PACKET_AUDIO_3 = 0xC2;
static const int PLM_DEMUX_PACKET_AUDIO_4 = 0xC2;
static const int PLM_DEMUX_PACKET_VIDEO_1 = 0xE0;
// Create a demuxer with a plm_buffer as source. This will also attempt to read
// the pack and system headers from the buffer.
plm_demux_t *plm_demux_create(plm_buffer_t *buffer, int destroy_when_done);
// Destroy a demuxer and free all data.
void plm_demux_destroy(plm_demux_t *self);
// Returns TRUE/FALSE whether pack and system headers have been found. This will
// attempt to read the headers if non are present yet.
int plm_demux_has_headers(plm_demux_t *self);
// Returns the number of video streams found in the system header. This will
// attempt to read the system header if non is present yet.
int plm_demux_get_num_video_streams(plm_demux_t *self);
// Returns the number of audio streams found in the system header. This will
// attempt to read the system header if non is present yet.
int plm_demux_get_num_audio_streams(plm_demux_t *self);
// Rewind the internal buffer. See plm_buffer_rewind().
void plm_demux_rewind(plm_demux_t *self);
// Get whether the file has ended. This will be cleared on seeking or rewind.
int plm_demux_has_ended(plm_demux_t *self);
// Seek to a packet of the specified type with a PTS just before specified time.
// If force_intra is TRUE, only packets containing an intra frame will be
// considered - this only makes sense when the type is PLM_DEMUX_PACKET_VIDEO_1.
// Note that the specified time is considered 0-based, regardless of the first
// PTS in the data source.
plm_packet_t *plm_demux_seek(plm_demux_t *self, double time, int type, int force_intra);
// Get the PTS of the first packet of this type. Returns PLM_PACKET_INVALID_TS
// if not packet of this packet type can be found.
double plm_demux_get_start_time(plm_demux_t *self, int type);
// Get the duration for the specified packet type - i.e. the span between the
// the first PTS and the last PTS in the data source. This only makes sense when
// the underlying data source is a file or fixed memory.
double plm_demux_get_duration(plm_demux_t *self, int type);
// Decode and return the next packet. The returned packet_t is valid until
// the next call to plm_demux_decode() or until the demuxer is destroyed.
plm_packet_t *plm_demux_decode(plm_demux_t *self);
// -----------------------------------------------------------------------------
// plm_video public API
// Decode MPEG1 Video ("mpeg1") data into raw YCrCb frames
// Create a video decoder with a plm_buffer as source.
plm_video_t *plm_video_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done);
// Destroy a video decoder and free all data.
void plm_video_destroy(plm_video_t *self);
// Get whether a sequence header was found and we can accurately report on
// dimensions and framerate.
int plm_video_has_header(plm_video_t *self);
// Get the framerate in frames per second.
double plm_video_get_framerate(plm_video_t *self);
// Get the display width/height.
int plm_video_get_width(plm_video_t *self);
int plm_video_get_height(plm_video_t *self);
// Set "no delay" mode. When enabled, the decoder assumes that the video does
// *not* contain any B-Frames. This is useful for reducing lag when streaming.
// The default is FALSE.
void plm_video_set_no_delay(plm_video_t *self, int no_delay);
// Get the current internal time in seconds.
double plm_video_get_time(plm_video_t *self);
// Set the current internal time in seconds. This is only useful when you
// manipulate the underlying video buffer and want to enforce a correct
// timestamps.
void plm_video_set_time(plm_video_t *self, double time);
// Rewind the internal buffer. See plm_buffer_rewind().
void plm_video_rewind(plm_video_t *self);
// Get whether the file has ended. This will be cleared on rewind.
int plm_video_has_ended(plm_video_t *self);
// Decode and return one frame of video and advance the internal time by
// 1/framerate seconds. The returned frame_t is valid until the next call of
// plm_video_decode() or until the video decoder is destroyed.
plm_frame_t *plm_video_decode(plm_video_t *self);
// Convert the YCrCb data of a frame into interleaved R G B data. The stride
// specifies the width in bytes of the destination buffer. I.e. the number of
// bytes from one line to the next. The stride must be at least
// (frame->width * bytes_per_pixel). The buffer pointed to by *dest must have a
// size of at least (stride * frame->height).
// Note that the alpha component of the dest buffer is always left untouched.
void plm_frame_to_rgb(plm_frame_t *frame, uint8_t *dest, int stride);
void plm_frame_to_bgr(plm_frame_t *frame, uint8_t *dest, int stride);
void plm_frame_to_rgba(plm_frame_t *frame, uint8_t *dest, int stride);
void plm_frame_to_bgra(plm_frame_t *frame, uint8_t *dest, int stride);
void plm_frame_to_argb(plm_frame_t *frame, uint8_t *dest, int stride);
void plm_frame_to_abgr(plm_frame_t *frame, uint8_t *dest, int stride);
// -----------------------------------------------------------------------------
// plm_audio public API
// Decode MPEG-1 Audio Layer II ("mp2") data into raw samples
// Create an audio decoder with a plm_buffer as source.
plm_audio_t *plm_audio_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done);
// Destroy an audio decoder and free all data.
void plm_audio_destroy(plm_audio_t *self);
// Get whether a frame header was found and we can accurately report on
// samplerate.
int plm_audio_has_header(plm_audio_t *self);
// Get the samplerate in samples per second.
int plm_audio_get_samplerate(plm_audio_t *self);
// Get the current internal time in seconds.
double plm_audio_get_time(plm_audio_t *self);
// Set the current internal time in seconds. This is only useful when you
// manipulate the underlying video buffer and want to enforce a correct
// timestamps.
void plm_audio_set_time(plm_audio_t *self, double time);
// Rewind the internal buffer. See plm_buffer_rewind().
void plm_audio_rewind(plm_audio_t *self);
// Get whether the file has ended. This will be cleared on rewind.
int plm_audio_has_ended(plm_audio_t *self);
// Decode and return one "frame" of audio and advance the internal time by
// (PLM_AUDIO_SAMPLES_PER_FRAME/samplerate) seconds. The returned samples_t
// is valid until the next call of plm_audio_decode() or until the audio
// decoder is destroyed.
plm_samples_t *plm_audio_decode(plm_audio_t *self);
#ifdef __cplusplus
}
#endif
#endif // PL_MPEG_H
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// IMPLEMENTATION
#ifdef PL_MPEG_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define PLM_UNUSED(expr) (void)(expr)
// -----------------------------------------------------------------------------
// plm (high-level interface) implementation
typedef struct plm_t {
plm_demux_t *demux;
double time;
int has_ended;
int loop;
int has_decoders;
int video_enabled;
int video_packet_type;
plm_buffer_t *video_buffer;
plm_video_t *video_decoder;
int audio_enabled;
int audio_stream_index;
int audio_packet_type;
double audio_lead_time;
plm_buffer_t *audio_buffer;
plm_audio_t *audio_decoder;
plm_video_decode_callback video_decode_callback;
void *video_decode_callback_user_data;
plm_audio_decode_callback audio_decode_callback;
void *audio_decode_callback_user_data;
} plm_t;
int plm_init_decoders(plm_t *self);
void plm_handle_end(plm_t *self);
void plm_read_video_packet(plm_buffer_t *buffer, void *user);
void plm_read_audio_packet(plm_buffer_t *buffer, void *user);
void plm_read_packets(plm_t *self, int requested_type);
plm_t *plm_create_with_filename(const char *filename) {
plm_buffer_t *buffer = plm_buffer_create_with_filename(filename);
if (!buffer) {
return NULL;
}
return plm_create_with_buffer(buffer, TRUE);
}
plm_t *plm_create_with_file(FILE *fh, int close_when_done) {
plm_buffer_t *buffer = plm_buffer_create_with_file(fh, close_when_done);
return plm_create_with_buffer(buffer, TRUE);
}
plm_t *plm_create_with_memory(uint8_t *bytes, size_t length, int free_when_done) {
plm_buffer_t *buffer = plm_buffer_create_with_memory(bytes, length, free_when_done);
return plm_create_with_buffer(buffer, TRUE);
}
plm_t *plm_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done) {
plm_t *self = (plm_t *)malloc(sizeof(plm_t));
memset(self, 0, sizeof(plm_t));
self->demux = plm_demux_create(buffer, destroy_when_done);
self->video_enabled = TRUE;
self->audio_enabled = TRUE;
plm_init_decoders(self);
return self;
}
int plm_init_decoders(plm_t *self) {
if (self->has_decoders) {
return TRUE;
}
if (!plm_demux_has_headers(self->demux)) {
return FALSE;
}
if (plm_demux_get_num_video_streams(self->demux) > 0) {
if (self->video_enabled) {
self->video_packet_type = PLM_DEMUX_PACKET_VIDEO_1;
}
self->video_buffer = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE);
plm_buffer_set_load_callback(self->video_buffer, plm_read_video_packet, self);
}
if (plm_demux_get_num_audio_streams(self->demux) > 0) {
if (self->audio_enabled) {
self->audio_packet_type = PLM_DEMUX_PACKET_AUDIO_1 + self->audio_stream_index;
}
self->audio_buffer = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE);
plm_buffer_set_load_callback(self->audio_buffer, plm_read_audio_packet, self);
}
if (self->video_buffer) {
self->video_decoder = plm_video_create_with_buffer(self->video_buffer, TRUE);
}
if (self->audio_buffer) {
self->audio_decoder = plm_audio_create_with_buffer(self->audio_buffer, TRUE);
}
self->has_decoders = TRUE;
return TRUE;
}
void plm_destroy(plm_t *self) {
if (self->video_decoder) {
plm_video_destroy(self->video_decoder);
}
if (self->audio_decoder) {
plm_audio_destroy(self->audio_decoder);
}
plm_demux_destroy(self->demux);
free(self);
}
int plm_get_audio_enabled(plm_t *self) {
return self->audio_enabled;
}
int plm_has_headers(plm_t *self) {
if (!plm_demux_has_headers(self->demux)) {
return FALSE;
}
if (!plm_init_decoders(self)) {
return FALSE;
}
if (
(self->video_decoder && !plm_video_has_header(self->video_decoder)) ||
(self->audio_decoder && !plm_audio_has_header(self->audio_decoder))
) {
return FALSE;
}
return TRUE;
}
void plm_set_audio_enabled(plm_t *self, int enabled) {
self->audio_enabled = enabled;
if (!enabled) {
self->audio_packet_type = 0;
return;
}
self->audio_packet_type = (plm_init_decoders(self) && self->audio_decoder)
? PLM_DEMUX_PACKET_AUDIO_1 + self->audio_stream_index
: 0;
}
void plm_set_audio_stream(plm_t *self, int stream_index) {
if (stream_index < 0 || stream_index > 3) {
return;
}
self->audio_stream_index = stream_index;
// Set the correct audio_packet_type
plm_set_audio_enabled(self, self->audio_enabled);
}
int plm_get_video_enabled(plm_t *self) {
return self->video_enabled;
}
void plm_set_video_enabled(plm_t *self, int enabled) {
self->video_enabled = enabled;
if (!enabled) {
self->video_packet_type = 0;
return;
}
self->video_packet_type = (plm_init_decoders(self) && self->video_decoder)
? PLM_DEMUX_PACKET_VIDEO_1
: 0;
}
int plm_get_num_video_streams(plm_t *self) {
return plm_demux_get_num_video_streams(self->demux);
}
int plm_get_width(plm_t *self) {
return (plm_init_decoders(self) && self->video_decoder)
? plm_video_get_width(self->video_decoder)
: 0;
}
int plm_get_height(plm_t *self) {
return (plm_init_decoders(self) && self->video_decoder)
? plm_video_get_height(self->video_decoder)
: 0;
}
double plm_get_framerate(plm_t *self) {
return (plm_init_decoders(self) && self->video_decoder)
? plm_video_get_framerate(self->video_decoder)
: 0;
}
int plm_get_num_audio_streams(plm_t *self) {
return plm_demux_get_num_audio_streams(self->demux);
}
int plm_get_samplerate(plm_t *self) {
return (plm_init_decoders(self) && self->audio_decoder)
? plm_audio_get_samplerate(self->audio_decoder)
: 0;
}
double plm_get_audio_lead_time(plm_t *self) {
return self->audio_lead_time;
}
void plm_set_audio_lead_time(plm_t *self, double lead_time) {
self->audio_lead_time = lead_time;
}
double plm_get_time(plm_t *self) {
return self->time;
}
double plm_get_duration(plm_t *self) {
return plm_demux_get_duration(self->demux, PLM_DEMUX_PACKET_VIDEO_1);
}
void plm_rewind(plm_t *self) {
if (self->video_decoder) {
plm_video_rewind(self->video_decoder);
}
if (self->audio_decoder) {
plm_audio_rewind(self->audio_decoder);
}
plm_demux_rewind(self->demux);
self->time = 0;
}
int plm_get_loop(plm_t *self) {
return self->loop;
}
void plm_set_loop(plm_t *self, int loop) {
self->loop = loop;
}
int plm_has_ended(plm_t *self) {
return self->has_ended;
}
void plm_set_video_decode_callback(plm_t *self, plm_video_decode_callback fp, void *user) {
self->video_decode_callback = fp;
self->video_decode_callback_user_data = user;
}
void plm_set_audio_decode_callback(plm_t *self, plm_audio_decode_callback fp, void *user) {
self->audio_decode_callback = fp;
self->audio_decode_callback_user_data = user;
}
void plm_decode(plm_t *self, double tick) {
if (!plm_init_decoders(self)) {
return;
}
int decode_video = (self->video_decode_callback && self->video_packet_type);
int decode_audio = (self->audio_decode_callback && self->audio_packet_type);
if (!decode_video && !decode_audio) {
// Nothing to do here
return;
}
int did_decode = FALSE;
int decode_video_failed = FALSE;
int decode_audio_failed = FALSE;
double video_target_time = self->time + tick;
double audio_target_time = self->time + tick + self->audio_lead_time;
do {
did_decode = FALSE;
if (decode_video && plm_video_get_time(self->video_decoder) < video_target_time) {
plm_frame_t *frame = plm_video_decode(self->video_decoder);
if (frame) {
self->video_decode_callback(self, frame, self->video_decode_callback_user_data);
did_decode = TRUE;
}
else {
decode_video_failed = TRUE;
}
}
if (decode_audio && plm_audio_get_time(self->audio_decoder) < audio_target_time) {
plm_samples_t *samples = plm_audio_decode(self->audio_decoder);
if (samples) {
self->audio_decode_callback(self, samples, self->audio_decode_callback_user_data);
did_decode = TRUE;
}
else {
decode_audio_failed = TRUE;
}
}
} while (did_decode);
// Did all sources we wanted to decode fail and the demuxer is at the end?
if (
(!decode_video || decode_video_failed) &&
(!decode_audio || decode_audio_failed) &&
plm_demux_has_ended(self->demux)
) {
plm_handle_end(self);
return;
}
self->time += tick;
}
plm_frame_t *plm_decode_video(plm_t *self) {
if (!plm_init_decoders(self)) {
return NULL;
}
if (!self->video_packet_type) {
return NULL;
}
plm_frame_t *frame = plm_video_decode(self->video_decoder);
if (frame) {
self->time = frame->time;
}
else if (plm_demux_has_ended(self->demux)) {
plm_handle_end(self);
}
return frame;
}
plm_samples_t *plm_decode_audio(plm_t *self) {
if (!plm_init_decoders(self)) {
return NULL;
}
if (!self->audio_packet_type) {
return NULL;
}
plm_samples_t *samples = plm_audio_decode(self->audio_decoder);
if (samples) {
self->time = samples->time;
}
else if (plm_demux_has_ended(self->demux)) {
plm_handle_end(self);
}
return samples;
}
void plm_handle_end(plm_t *self) {
if (self->loop) {
plm_rewind(self);
}
else {
self->has_ended = TRUE;
}
}
void plm_read_video_packet(plm_buffer_t *buffer, void *user) {
PLM_UNUSED(buffer);
plm_t *self = (plm_t *)user;
plm_read_packets(self, self->video_packet_type);
}
void plm_read_audio_packet(plm_buffer_t *buffer, void *user) {
PLM_UNUSED(buffer);
plm_t *self = (plm_t *)user;
plm_read_packets(self, self->audio_packet_type);
}
void plm_read_packets(plm_t *self, int requested_type) {
plm_packet_t *packet;
while ((packet = plm_demux_decode(self->demux))) {
if (packet->type == self->video_packet_type) {
plm_buffer_write(self->video_buffer, packet->data, packet->length);
}
else if (packet->type == self->audio_packet_type) {
plm_buffer_write(self->audio_buffer, packet->data, packet->length);
}
if (packet->type == requested_type) {
return;
}
}
if (plm_demux_has_ended(self->demux)) {
if (self->video_buffer) {
plm_buffer_signal_end(self->video_buffer);
}
if (self->audio_buffer) {
plm_buffer_signal_end(self->audio_buffer);
}
}
}
plm_frame_t *plm_seek_frame(plm_t *self, double time, int seek_exact) {
if (!plm_init_decoders(self)) {
return NULL;
}
if (!self->video_packet_type) {
return NULL;
}
int type = self->video_packet_type;
double start_time = plm_demux_get_start_time(self->demux, type);
double duration = plm_demux_get_duration(self->demux, type);
if (time < 0) {
time = 0;
}
else if (time > duration) {
time = duration;
}
plm_packet_t *packet = plm_demux_seek(self->demux, time, type, TRUE);
if (!packet) {
return NULL;
}
// Disable writing to the audio buffer while decoding video
int previous_audio_packet_type = self->audio_packet_type;
self->audio_packet_type = 0;
// Clear video buffer and decode the found packet
plm_video_rewind(self->video_decoder);
plm_video_set_time(self->video_decoder, packet->pts - start_time);
plm_buffer_write(self->video_buffer, packet->data, packet->length);
plm_frame_t *frame = plm_video_decode(self->video_decoder);
// If we want to seek to an exact frame, we have to decode all frames
// on top of the intra frame we just jumped to.
if (seek_exact) {
while (frame && frame->time < time) {
frame = plm_video_decode(self->video_decoder);
}
}
// Enable writing to the audio buffer again?
self->audio_packet_type = previous_audio_packet_type;
if (frame) {
self->time = frame->time;
}
self->has_ended = FALSE;
return frame;
}
int plm_seek(plm_t *self, double time, int seek_exact) {
plm_frame_t *frame = plm_seek_frame(self, time, seek_exact);
if (!frame) {
return FALSE;
}
if (self->video_decode_callback) {
self->video_decode_callback(self, frame, self->video_decode_callback_user_data);
}
// If audio is not enabled we are done here.
if (!self->audio_packet_type) {
return TRUE;
}
// Sync up Audio. This demuxes more packets until the first audio packet
// with a PTS greater than the current time is found. plm_decode() is then
// called to decode enough audio data to satisfy the audio_lead_time.
double start_time = plm_demux_get_start_time(self->demux, self->video_packet_type);
plm_audio_rewind(self->audio_decoder);
plm_packet_t *packet = NULL;
while ((packet = plm_demux_decode(self->demux))) {
if (packet->type == self->video_packet_type) {
plm_buffer_write(self->video_buffer, packet->data, packet->length);
}
else if (
packet->type == self->audio_packet_type &&
packet->pts - start_time > self->time
) {
plm_audio_set_time(self->audio_decoder, packet->pts - start_time);
plm_buffer_write(self->audio_buffer, packet->data, packet->length);
plm_decode(self, 0);
break;
}
}
return TRUE;
}
// -----------------------------------------------------------------------------
// plm_buffer implementation
enum plm_buffer_mode {
PLM_BUFFER_MODE_FILE,
PLM_BUFFER_MODE_FIXED_MEM,
PLM_BUFFER_MODE_RING,
PLM_BUFFER_MODE_APPEND
};
typedef struct plm_buffer_t {
size_t bit_index;
size_t capacity;
size_t length;
size_t total_size;
int discard_read_bytes;
int has_ended;
int free_when_done;
int close_when_done;
FILE *fh;
plm_buffer_load_callback load_callback;
void *load_callback_user_data;
uint8_t *bytes;
enum plm_buffer_mode mode;
} plm_buffer_t;
typedef struct {
int16_t index;
int16_t value;
} plm_vlc_t;
typedef struct {
int16_t index;
uint16_t value;
} plm_vlc_uint_t;
void plm_buffer_seek(plm_buffer_t *self, size_t pos);
size_t plm_buffer_tell(plm_buffer_t *self);
void plm_buffer_discard_read_bytes(plm_buffer_t *self);
void plm_buffer_load_file_callback(plm_buffer_t *self, void *user);
int plm_buffer_has(plm_buffer_t *self, size_t count);
int plm_buffer_read(plm_buffer_t *self, int count);
void plm_buffer_align(plm_buffer_t *self);
void plm_buffer_skip(plm_buffer_t *self, size_t count);
int plm_buffer_skip_bytes(plm_buffer_t *self, uint8_t v);
int plm_buffer_next_start_code(plm_buffer_t *self);
int plm_buffer_find_start_code(plm_buffer_t *self, int code);
int plm_buffer_no_start_code(plm_buffer_t *self);
int16_t plm_buffer_read_vlc(plm_buffer_t *self, const plm_vlc_t *table);
uint16_t plm_buffer_read_vlc_uint(plm_buffer_t *self, const plm_vlc_uint_t *table);
plm_buffer_t *plm_buffer_create_with_filename(const char *filename) {
FILE *fh = fopen(filename, "rb");
if (!fh) {
return NULL;
}
return plm_buffer_create_with_file(fh, TRUE);
}
plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done) {
plm_buffer_t *self = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE);
self->fh = fh;
self->close_when_done = close_when_done;
self->mode = PLM_BUFFER_MODE_FILE;
self->discard_read_bytes = TRUE;
fseek(self->fh, 0, SEEK_END);
self->total_size = ftell(self->fh);
fseek(self->fh, 0, SEEK_SET);
plm_buffer_set_load_callback(self, plm_buffer_load_file_callback, NULL);
return self;
}
plm_buffer_t *plm_buffer_create_with_memory(uint8_t *bytes, size_t length, int free_when_done) {
plm_buffer_t *self = (plm_buffer_t *)malloc(sizeof(plm_buffer_t));
memset(self, 0, sizeof(plm_buffer_t));
self->capacity = length;
self->length = length;
self->total_size = length;
self->free_when_done = free_when_done;
self->bytes = bytes;
self->mode = PLM_BUFFER_MODE_FIXED_MEM;
self->discard_read_bytes = FALSE;
return self;
}
plm_buffer_t *plm_buffer_create_with_capacity(size_t capacity) {
plm_buffer_t *self = (plm_buffer_t *)malloc(sizeof(plm_buffer_t));
memset(self, 0, sizeof(plm_buffer_t));
self->capacity = capacity;
self->free_when_done = TRUE;
self->bytes = (uint8_t *)malloc(capacity);
self->mode = PLM_BUFFER_MODE_RING;
self->discard_read_bytes = TRUE;
return self;
}
plm_buffer_t *plm_buffer_create_for_appending(size_t initial_capacity) {
plm_buffer_t *self = plm_buffer_create_with_capacity(initial_capacity);
self->mode = PLM_BUFFER_MODE_APPEND;
self->discard_read_bytes = FALSE;
return self;
}
void plm_buffer_destroy(plm_buffer_t *self) {
if (self->fh && self->close_when_done) {
fclose(self->fh);
}
if (self->free_when_done) {
free(self->bytes);
}
free(self);
}
size_t plm_buffer_get_size(plm_buffer_t *self) {
return (self->mode == PLM_BUFFER_MODE_FILE)
? self->total_size
: self->length;
}
size_t plm_buffer_get_remaining(plm_buffer_t *self) {
return self->length - (self->bit_index >> 3);
}
size_t plm_buffer_write(plm_buffer_t *self, uint8_t *bytes, size_t length) {
if (self->mode == PLM_BUFFER_MODE_FIXED_MEM) {
return 0;
}
if (self->discard_read_bytes) {
// This should be a ring buffer, but instead it just shifts all unread
// data to the beginning of the buffer and appends new data at the end.
// Seems to be good enough.
plm_buffer_discard_read_bytes(self);
if (self->mode == PLM_BUFFER_MODE_RING) {
self->total_size = 0;
}
}
// Do we have to resize to fit the new data?
size_t bytes_available = self->capacity - self->length;
if (bytes_available < length) {
size_t new_size = self->capacity;
do {
new_size *= 2;
} while (new_size - self->length < length);
self->bytes = (uint8_t *)realloc(self->bytes, new_size);
self->capacity = new_size;
}
memcpy(self->bytes + self->length, bytes, length);
self->length += length;
self->has_ended = FALSE;
return length;
}
void plm_buffer_signal_end(plm_buffer_t *self) {
self->total_size = self->length;
}
void plm_buffer_set_load_callback(plm_buffer_t *self, plm_buffer_load_callback fp, void *user) {
self->load_callback = fp;
self->load_callback_user_data = user;
}
void plm_buffer_rewind(plm_buffer_t *self) {
plm_buffer_seek(self, 0);
}
void plm_buffer_seek(plm_buffer_t *self, size_t pos) {
self->has_ended = FALSE;
if (self->mode == PLM_BUFFER_MODE_FILE) {
fseek(self->fh, pos, SEEK_SET);
self->bit_index = 0;
self->length = 0;
}
else if (self->mode == PLM_BUFFER_MODE_RING) {
if (pos != 0) {
// Seeking to non-0 is forbidden for dynamic-mem buffers
return;
}
self->bit_index = 0;
self->length = 0;
self->total_size = 0;
}
else if (pos < self->length) {
self->bit_index = pos << 3;
}
}
size_t plm_buffer_tell(plm_buffer_t *self) {
return self->mode == PLM_BUFFER_MODE_FILE
? ftell(self->fh) + (self->bit_index >> 3) - self->length
: self->bit_index >> 3;
}
void plm_buffer_discard_read_bytes(plm_buffer_t *self) {
size_t byte_pos = self->bit_index >> 3;
if (byte_pos == self->length) {
self->bit_index = 0;
self->length = 0;
}
else if (byte_pos > 0) {
memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos);
self->bit_index -= byte_pos << 3;
self->length -= byte_pos;
}
}
void plm_buffer_load_file_callback(plm_buffer_t *self, void *user) {
PLM_UNUSED(user);
if (self->discard_read_bytes) {
plm_buffer_discard_read_bytes(self);
}
size_t bytes_available = self->capacity - self->length;
size_t bytes_read = fread(self->bytes + self->length, 1, bytes_available, self->fh);
self->length += bytes_read;
if (bytes_read == 0) {
self->has_ended = TRUE;
}
}
int plm_buffer_has_ended(plm_buffer_t *self) {
return self->has_ended;
}
int plm_buffer_has(plm_buffer_t *self, size_t count) {
if (((self->length << 3) - self->bit_index) >= count) {
return TRUE;
}
if (self->load_callback) {
self->load_callback(self, self->load_callback_user_data);
if (((self->length << 3) - self->bit_index) >= count) {
return TRUE;
}
}
if (self->total_size != 0 && self->length == self->total_size) {
self->has_ended = TRUE;
}
return FALSE;
}
int plm_buffer_read(plm_buffer_t *self, int count) {
if (!plm_buffer_has(self, count)) {
return 0;
}
int value = 0;
while (count) {
int current_byte = self->bytes[self->bit_index >> 3];
int remaining = 8 - (self->bit_index & 7); // Remaining bits in byte
int read = remaining < count ? remaining : count; // Bits in self run
int shift = remaining - read;
int mask = (0xff >> (8 - read));
value = (value << read) | ((current_byte & (mask << shift)) >> shift);
self->bit_index += read;
count -= read;
}
return value;
}
void plm_buffer_align(plm_buffer_t *self) {
self->bit_index = ((self->bit_index + 7) >> 3) << 3; // Align to next byte
}
void plm_buffer_skip(plm_buffer_t *self, size_t count) {
if (plm_buffer_has(self, count)) {
self->bit_index += count;
}
}
int plm_buffer_skip_bytes(plm_buffer_t *self, uint8_t v) {
plm_buffer_align(self);
int skipped = 0;
while (plm_buffer_has(self, 8) && self->bytes[self->bit_index >> 3] == v) {
self->bit_index += 8;
skipped++;
}
return skipped;
}
int plm_buffer_next_start_code(plm_buffer_t *self) {
plm_buffer_align(self);
while (plm_buffer_has(self, (5 << 3))) {
size_t byte_index = (self->bit_index) >> 3;
if (
self->bytes[byte_index] == 0x00 &&
self->bytes[byte_index + 1] == 0x00 &&
self->bytes[byte_index + 2] == 0x01
) {
self->bit_index = (byte_index + 4) << 3;
return self->bytes[byte_index + 3];
}
self->bit_index += 8;
}
return -1;
}
int plm_buffer_find_start_code(plm_buffer_t *self, int code) {
int current = 0;
while (TRUE) {
current = plm_buffer_next_start_code(self);
if (current == code || current == -1) {
return current;
}
}
return -1;
}
int plm_buffer_has_start_code(plm_buffer_t *self, int code) {
size_t previous_bit_index = self->bit_index;
int previous_discard_read_bytes = self->discard_read_bytes;
self->discard_read_bytes = FALSE;
int current = plm_buffer_find_start_code(self, code);
self->bit_index = previous_bit_index;
self->discard_read_bytes = previous_discard_read_bytes;
return current;
}
int plm_buffer_no_start_code(plm_buffer_t *self) {
if (!plm_buffer_has(self, (5 << 3))) {
return FALSE;
}
size_t byte_index = ((self->bit_index + 7) >> 3);
return !(
self->bytes[byte_index] == 0x00 &&
self->bytes[byte_index + 1] == 0x00 &&
self->bytes[byte_index + 2] == 0x01
);
}
int16_t plm_buffer_read_vlc(plm_buffer_t *self, const plm_vlc_t *table) {
plm_vlc_t state = {0, 0};
do {
state = table[state.index + plm_buffer_read(self, 1)];
} while (state.index > 0);
return state.value;
}
uint16_t plm_buffer_read_vlc_uint(plm_buffer_t *self, const plm_vlc_uint_t *table) {
return (uint16_t)plm_buffer_read_vlc(self, (const plm_vlc_t *)table);
}
// ----------------------------------------------------------------------------
// plm_demux implementation
static const int PLM_START_PACK = 0xBA;
static const int PLM_START_END = 0xB9;
static const int PLM_START_SYSTEM = 0xBB;
typedef struct plm_demux_t {
plm_buffer_t *buffer;
int destroy_buffer_when_done;
double system_clock_ref;
size_t last_file_size;
double last_decoded_pts;
double start_time;
double duration;
int start_code;
int has_pack_header;
int has_system_header;
int has_headers;
int num_audio_streams;
int num_video_streams;
plm_packet_t current_packet;
plm_packet_t next_packet;
} plm_demux_t;
void plm_demux_buffer_seek(plm_demux_t *self, size_t pos);
double plm_demux_decode_time(plm_demux_t *self);
plm_packet_t *plm_demux_decode_packet(plm_demux_t *self, int type);
plm_packet_t *plm_demux_get_packet(plm_demux_t *self);
plm_demux_t *plm_demux_create(plm_buffer_t *buffer, int destroy_when_done) {
plm_demux_t *self = (plm_demux_t *)malloc(sizeof(plm_demux_t));
memset(self, 0, sizeof(plm_demux_t));
self->buffer = buffer;
self->destroy_buffer_when_done = destroy_when_done;
self->start_time = PLM_PACKET_INVALID_TS;
self->duration = PLM_PACKET_INVALID_TS;
self->start_code = -1;
plm_demux_has_headers(self);
return self;
}
void plm_demux_destroy(plm_demux_t *self) {
if (self->destroy_buffer_when_done) {
plm_buffer_destroy(self->buffer);
}
free(self);
}
int plm_demux_has_headers(plm_demux_t *self) {
if (self->has_headers) {
return TRUE;
}
// Decode pack header
if (!self->has_pack_header) {
if (
self->start_code != PLM_START_PACK &&
plm_buffer_find_start_code(self->buffer, PLM_START_PACK) == -1
) {
return FALSE;
}
self->start_code = PLM_START_PACK;
if (!plm_buffer_has(self->buffer, 64)) {
return FALSE;
}
self->start_code = -1;
if (plm_buffer_read(self->buffer, 4) != 0x02) {
return FALSE;
}
self->system_clock_ref = plm_demux_decode_time(self);
plm_buffer_skip(self->buffer, 1);
plm_buffer_skip(self->buffer, 22); // mux_rate * 50
plm_buffer_skip(self->buffer, 1);
self->has_pack_header = TRUE;
}
// Decode system header
if (!self->has_system_header) {
if (
self->start_code != PLM_START_SYSTEM &&
plm_buffer_find_start_code(self->buffer, PLM_START_SYSTEM) == -1
) {
return FALSE;
}
self->start_code = PLM_START_SYSTEM;
if (!plm_buffer_has(self->buffer, 56)) {
return FALSE;
}
self->start_code = -1;
plm_buffer_skip(self->buffer, 16); // header_length
plm_buffer_skip(self->buffer, 24); // rate bound
self->num_audio_streams = plm_buffer_read(self->buffer, 6);
plm_buffer_skip(self->buffer, 5); // misc flags
self->num_video_streams = plm_buffer_read(self->buffer, 5);
self->has_system_header = TRUE;
}
self->has_headers = TRUE;
return TRUE;
}
int plm_demux_get_num_video_streams(plm_demux_t *self) {
return plm_demux_has_headers(self)
? self->num_video_streams
: 0;
}
int plm_demux_get_num_audio_streams(plm_demux_t *self) {
return plm_demux_has_headers(self)
? self->num_audio_streams
: 0;
}
void plm_demux_rewind(plm_demux_t *self) {
plm_buffer_rewind(self->buffer);
self->current_packet.length = 0;
self->next_packet.length = 0;
self->start_code = -1;
}
int plm_demux_has_ended(plm_demux_t *self) {
return plm_buffer_has_ended(self->buffer);
}
void plm_demux_buffer_seek(plm_demux_t *self, size_t pos) {
plm_buffer_seek(self->buffer, pos);
self->current_packet.length = 0;
self->next_packet.length = 0;
self->start_code = -1;
}
double plm_demux_get_start_time(plm_demux_t *self, int type) {
if (self->start_time != PLM_PACKET_INVALID_TS) {
return self->start_time;
}
int previous_pos = plm_buffer_tell(self->buffer);
int previous_start_code = self->start_code;
// Find first video PTS
plm_demux_rewind(self);
do {
plm_packet_t *packet = plm_demux_decode(self);
if (!packet) {
break;
}
if (packet->type == type) {
self->start_time = packet->pts;
}
} while (self->start_time == PLM_PACKET_INVALID_TS);
plm_demux_buffer_seek(self, previous_pos);
self->start_code = previous_start_code;
return self->start_time;
}
double plm_demux_get_duration(plm_demux_t *self, int type) {
size_t file_size = plm_buffer_get_size(self->buffer);
if (
self->duration != PLM_PACKET_INVALID_TS &&
self->last_file_size == file_size
) {
return self->duration;
}
size_t previous_pos = plm_buffer_tell(self->buffer);
int previous_start_code = self->start_code;
// Find last video PTS. Start searching 64kb from the end and go further
// back if needed.
long start_range = 64 * 1024;
long max_range = 4096 * 1024;
for (long range = start_range; range <= max_range; range *= 2) {
long seek_pos = file_size - range;
if (seek_pos < 0) {
seek_pos = 0;
range = max_range; // Make sure to bail after this round
}
plm_demux_buffer_seek(self, seek_pos);
self->current_packet.length = 0;
double last_pts = PLM_PACKET_INVALID_TS;
plm_packet_t *packet = NULL;
while ((packet = plm_demux_decode(self))) {
if (packet->pts != PLM_PACKET_INVALID_TS && packet->type == type) {
last_pts = packet->pts;
}
}
if (last_pts != PLM_PACKET_INVALID_TS) {
self->duration = last_pts - plm_demux_get_start_time(self, type);
break;
}
}
plm_demux_buffer_seek(self, previous_pos);
self->start_code = previous_start_code;
self->last_file_size = file_size;
return self->duration;
}
plm_packet_t *plm_demux_seek(plm_demux_t *self, double seek_time, int type, int force_intra) {
if (!plm_demux_has_headers(self)) {
return NULL;
}
// Using the current time, current byte position and the average bytes per
// second for this file, try to jump to a byte position that hopefully has
// packets containing timestamps within one second before to the desired
// seek_time.
// If we hit close to the seek_time scan through all packets to find the
// last one (just before the seek_time) containing an intra frame.
// Otherwise we should at least be closer than before. Calculate the bytes
// per second for the jumped range and jump again.
// The number of retries here is hard-limited to a generous amount. Usually
// the correct range is found after 1--5 jumps, even for files with very
// variable bitrates. If significantly more jumps are needed, there's
// probably something wrong with the file and we just avoid getting into an
// infinite loop. 32 retries should be enough for anybody.
double duration = plm_demux_get_duration(self, type);
long file_size = plm_buffer_get_size(self->buffer);
long byterate = file_size / duration;
double cur_time = self->last_decoded_pts;
double scan_span = 1;
if (seek_time > duration) {
seek_time = duration;
}
else if (seek_time < 0) {
seek_time = 0;
}
seek_time += self->start_time;
for (int retry = 0; retry < 32; retry++) {
int found_packet_with_pts = FALSE;
int found_packet_in_range = FALSE;
long last_valid_packet_start = -1;
double first_packet_time = PLM_PACKET_INVALID_TS;
long cur_pos = plm_buffer_tell(self->buffer);
// Estimate byte offset and jump to it.
long offset = (seek_time - cur_time - scan_span) * byterate;
long seek_pos = cur_pos + offset;
if (seek_pos < 0) {
seek_pos = 0;
}
else if (seek_pos > file_size - 256) {
seek_pos = file_size - 256;
}
plm_demux_buffer_seek(self, seek_pos);
// Scan through all packets up to the seek_time to find the last packet
// containing an intra frame.
while (plm_buffer_find_start_code(self->buffer, type) != -1) {
long packet_start = plm_buffer_tell(self->buffer);
plm_packet_t *packet = plm_demux_decode_packet(self, type);
// Skip packet if it has no PTS
if (!packet || packet->pts == PLM_PACKET_INVALID_TS) {
continue;
}
// Bail scanning through packets if we hit one that is outside
// seek_time - scan_span.
// We also adjust the cur_time and byterate values here so the next
// iteration can be a bit more precise.
if (packet->pts > seek_time || packet->pts < seek_time - scan_span) {
found_packet_with_pts = TRUE;
byterate = (seek_pos - cur_pos) / (packet->pts - cur_time);
cur_time = packet->pts;
break;
}
// If we are still here, it means this packet is in close range to
// the seek_time. If this is the first packet for this jump position
// record the PTS. If we later have to back off, when there was no
// intra frame in this range, we can lower the seek_time to not scan
// this range again.
if (!found_packet_in_range) {
found_packet_in_range = TRUE;
first_packet_time = packet->pts;
}
// Check if this is an intra frame packet. If so, record the buffer
// position of the start of this packet. We want to jump back to it
// later, when we know it's the last intra frame before desired
// seek time.
if (force_intra) {
for (size_t i = 0; i < packet->length - 6; i++) {
// Find the START_PICTURE code
if (
packet->data[i] == 0x00 &&
packet->data[i + 1] == 0x00 &&
packet->data[i + 2] == 0x01 &&
packet->data[i + 3] == 0x00
) {
// Bits 11--13 in the picture header contain the frame
// type, where 1=Intra
if ((packet->data[i + 5] & 0x38) == 8) {
last_valid_packet_start = packet_start;
}
break;
}
}
}
// If we don't want intra frames, just use the last PTS found.
else {
last_valid_packet_start = packet_start;
}
}
// If there was at least one intra frame in the range scanned above,
// our search is over. Jump back to the packet and decode it again.
if (last_valid_packet_start != -1) {
plm_demux_buffer_seek(self, last_valid_packet_start);
return plm_demux_decode_packet(self, type);
}
// If we hit the right range, but still found no intra frame, we have
// to increases the scan_span. This is done exponentially to also handle
// video files with very few intra frames.
else if (found_packet_in_range) {
scan_span *= 2;
seek_time = first_packet_time;
}
// If we didn't find any packet with a PTS, it probably means we reached
// the end of the file. Estimate byterate and cur_time accordingly.
else if (!found_packet_with_pts) {
byterate = (seek_pos - cur_pos) / (duration - cur_time);
cur_time = duration;
}
}
return NULL;
}
plm_packet_t *plm_demux_decode(plm_demux_t *self) {
if (!plm_demux_has_headers(self)) {
return NULL;
}
if (self->current_packet.length) {
size_t bits_till_next_packet = self->current_packet.length << 3;
if (!plm_buffer_has(self->buffer, bits_till_next_packet)) {
return NULL;
}
plm_buffer_skip(self->buffer, bits_till_next_packet);
self->current_packet.length = 0;
}
// Pending packet waiting for data?
if (self->next_packet.length) {
return plm_demux_get_packet(self);
}
// Pending packet waiting for header?
if (self->start_code != -1) {
return plm_demux_decode_packet(self, self->start_code);
}
do {
self->start_code = plm_buffer_next_start_code(self->buffer);
if (
self->start_code == PLM_DEMUX_PACKET_VIDEO_1 ||
self->start_code == PLM_DEMUX_PACKET_PRIVATE || (
self->start_code >= PLM_DEMUX_PACKET_AUDIO_1 &&
self->start_code <= PLM_DEMUX_PACKET_AUDIO_4
)
) {
return plm_demux_decode_packet(self, self->start_code);
}
} while (self->start_code != -1);
return NULL;
}
double plm_demux_decode_time(plm_demux_t *self) {
int64_t clock = plm_buffer_read(self->buffer, 3) << 30;
plm_buffer_skip(self->buffer, 1);
clock |= plm_buffer_read(self->buffer, 15) << 15;
plm_buffer_skip(self->buffer, 1);
clock |= plm_buffer_read(self->buffer, 15);
plm_buffer_skip(self->buffer, 1);
return (double)clock / 90000.0;
}
plm_packet_t *plm_demux_decode_packet(plm_demux_t *self, int type) {
if (!plm_buffer_has(self->buffer, 16 << 3)) {
return NULL;
}
self->start_code = -1;
self->next_packet.type = type;
self->next_packet.length = plm_buffer_read(self->buffer, 16);
self->next_packet.length -= plm_buffer_skip_bytes(self->buffer, 0xff); // stuffing
// skip P-STD
if (plm_buffer_read(self->buffer, 2) == 0x01) {
plm_buffer_skip(self->buffer, 16);
self->next_packet.length -= 2;
}
int pts_dts_marker = plm_buffer_read(self->buffer, 2);
if (pts_dts_marker == 0x03) {
self->next_packet.pts = plm_demux_decode_time(self);
self->last_decoded_pts = self->next_packet.pts;
plm_buffer_skip(self->buffer, 40); // skip dts
self->next_packet.length -= 10;
}
else if (pts_dts_marker == 0x02) {
self->next_packet.pts = plm_demux_decode_time(self);
self->last_decoded_pts = self->next_packet.pts;
self->next_packet.length -= 5;
}
else if (pts_dts_marker == 0x00) {
self->next_packet.pts = PLM_PACKET_INVALID_TS;
plm_buffer_skip(self->buffer, 4);
self->next_packet.length -= 1;
}
else {
return NULL; // invalid
}
return plm_demux_get_packet(self);
}
plm_packet_t *plm_demux_get_packet(plm_demux_t *self) {
if (!plm_buffer_has(self->buffer, self->next_packet.length << 3)) {
return NULL;
}
self->current_packet.data = self->buffer->bytes + (self->buffer->bit_index >> 3);
self->current_packet.length = self->next_packet.length;
self->current_packet.type = self->next_packet.type;
self->current_packet.pts = self->next_packet.pts;
self->next_packet.length = 0;
return &self->current_packet;
}
// -----------------------------------------------------------------------------
// plm_video implementation
// Inspired by Java MPEG-1 Video Decoder and Player by Zoltan Korandi
// https://sourceforge.net/projects/javampeg1video/
static const int PLM_VIDEO_PICTURE_TYPE_INTRA = 1;
static const int PLM_VIDEO_PICTURE_TYPE_PREDICTIVE = 2;
static const int PLM_VIDEO_PICTURE_TYPE_B = 3;
static const int PLM_START_SEQUENCE = 0xB3;
static const int PLM_START_SLICE_FIRST = 0x01;
static const int PLM_START_SLICE_LAST = 0xAF;
static const int PLM_START_PICTURE = 0x00;
static const int PLM_START_EXTENSION = 0xB5;
static const int PLM_START_USER_DATA = 0xB2;
#define PLM_START_IS_SLICE(c) \
(c >= PLM_START_SLICE_FIRST && c <= PLM_START_SLICE_LAST)
static const double PLM_VIDEO_PICTURE_RATE[] = {
0.000, 23.976, 24.000, 25.000, 29.970, 30.000, 50.000, 59.940,
60.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
};
static const uint8_t PLM_VIDEO_ZIG_ZAG[] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
};
static const uint8_t PLM_VIDEO_INTRA_QUANT_MATRIX[] = {
8, 16, 19, 22, 26, 27, 29, 34,
16, 16, 22, 24, 27, 29, 34, 37,
19, 22, 26, 27, 29, 34, 34, 38,
22, 22, 26, 27, 29, 34, 37, 40,
22, 26, 27, 29, 32, 35, 40, 48,
26, 27, 29, 32, 35, 40, 48, 58,
26, 27, 29, 34, 38, 46, 56, 69,
27, 29, 35, 38, 46, 56, 69, 83
};
static const uint8_t PLM_VIDEO_NON_INTRA_QUANT_MATRIX[] = {
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16
};
static const uint8_t PLM_VIDEO_PREMULTIPLIER_MATRIX[] = {
32, 44, 42, 38, 32, 25, 17, 9,
44, 62, 58, 52, 44, 35, 24, 12,
42, 58, 55, 49, 42, 33, 23, 12,
38, 52, 49, 44, 38, 30, 20, 10,
32, 44, 42, 38, 32, 25, 17, 9,
25, 35, 33, 30, 25, 20, 14, 7,
17, 24, 23, 20, 17, 14, 9, 5,
9, 12, 12, 10, 9, 7, 5, 2
};
static const plm_vlc_t PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT[] = {
{ 1 << 1, 0}, { 0, 1}, // 0: x
{ 2 << 1, 0}, { 3 << 1, 0}, // 1: 0x
{ 4 << 1, 0}, { 5 << 1, 0}, // 2: 00x
{ 0, 3}, { 0, 2}, // 3: 01x
{ 6 << 1, 0}, { 7 << 1, 0}, // 4: 000x
{ 0, 5}, { 0, 4}, // 5: 001x
{ 8 << 1, 0}, { 9 << 1, 0}, // 6: 0000x
{ 0, 7}, { 0, 6}, // 7: 0001x
{ 10 << 1, 0}, { 11 << 1, 0}, // 8: 0000 0x
{ 12 << 1, 0}, { 13 << 1, 0}, // 9: 0000 1x
{ 14 << 1, 0}, { 15 << 1, 0}, // 10: 0000 00x
{ 16 << 1, 0}, { 17 << 1, 0}, // 11: 0000 01x
{ 18 << 1, 0}, { 19 << 1, 0}, // 12: 0000 10x
{ 0, 9}, { 0, 8}, // 13: 0000 11x
{ -1, 0}, { 20 << 1, 0}, // 14: 0000 000x
{ -1, 0}, { 21 << 1, 0}, // 15: 0000 001x
{ 22 << 1, 0}, { 23 << 1, 0}, // 16: 0000 010x
{ 0, 15}, { 0, 14}, // 17: 0000 011x
{ 0, 13}, { 0, 12}, // 18: 0000 100x
{ 0, 11}, { 0, 10}, // 19: 0000 101x
{ 24 << 1, 0}, { 25 << 1, 0}, // 20: 0000 0001x
{ 26 << 1, 0}, { 27 << 1, 0}, // 21: 0000 0011x
{ 28 << 1, 0}, { 29 << 1, 0}, // 22: 0000 0100x
{ 30 << 1, 0}, { 31 << 1, 0}, // 23: 0000 0101x
{ 32 << 1, 0}, { -1, 0}, // 24: 0000 0001 0x
{ -1, 0}, { 33 << 1, 0}, // 25: 0000 0001 1x
{ 34 << 1, 0}, { 35 << 1, 0}, // 26: 0000 0011 0x
{ 36 << 1, 0}, { 37 << 1, 0}, // 27: 0000 0011 1x
{ 38 << 1, 0}, { 39 << 1, 0}, // 28: 0000 0100 0x
{ 0, 21}, { 0, 20}, // 29: 0000 0100 1x
{ 0, 19}, { 0, 18}, // 30: 0000 0101 0x
{ 0, 17}, { 0, 16}, // 31: 0000 0101 1x
{ 0, 35}, { -1, 0}, // 32: 0000 0001 00x
{ -1, 0}, { 0, 34}, // 33: 0000 0001 11x
{ 0, 33}, { 0, 32}, // 34: 0000 0011 00x
{ 0, 31}, { 0, 30}, // 35: 0000 0011 01x
{ 0, 29}, { 0, 28}, // 36: 0000 0011 10x
{ 0, 27}, { 0, 26}, // 37: 0000 0011 11x
{ 0, 25}, { 0, 24}, // 38: 0000 0100 00x
{ 0, 23}, { 0, 22}, // 39: 0000 0100 01x
};
static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_INTRA[] = {
{ 1 << 1, 0}, { 0, 0x01}, // 0: x
{ -1, 0}, { 0, 0x11}, // 1: 0x
};
static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_PREDICTIVE[] = {
{ 1 << 1, 0}, { 0, 0x0a}, // 0: x
{ 2 << 1, 0}, { 0, 0x02}, // 1: 0x
{ 3 << 1, 0}, { 0, 0x08}, // 2: 00x
{ 4 << 1, 0}, { 5 << 1, 0}, // 3: 000x
{ 6 << 1, 0}, { 0, 0x12}, // 4: 0000x
{ 0, 0x1a}, { 0, 0x01}, // 5: 0001x
{ -1, 0}, { 0, 0x11}, // 6: 0000 0x
};
static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_B[] = {
{ 1 << 1, 0}, { 2 << 1, 0}, // 0: x
{ 3 << 1, 0}, { 4 << 1, 0}, // 1: 0x
{ 0, 0x0c}, { 0, 0x0e}, // 2: 1x
{ 5 << 1, 0}, { 6 << 1, 0}, // 3: 00x
{ 0, 0x04}, { 0, 0x06}, // 4: 01x
{ 7 << 1, 0}, { 8 << 1, 0}, // 5: 000x
{ 0, 0x08}, { 0, 0x0a}, // 6: 001x
{ 9 << 1, 0}, { 10 << 1, 0}, // 7: 0000x
{ 0, 0x1e}, { 0, 0x01}, // 8: 0001x
{ -1, 0}, { 0, 0x11}, // 9: 0000 0x
{ 0, 0x16}, { 0, 0x1a}, // 10: 0000 1x
};
static const plm_vlc_t *PLM_VIDEO_MACROBLOCK_TYPE[] = {
NULL,
PLM_VIDEO_MACROBLOCK_TYPE_INTRA,
PLM_VIDEO_MACROBLOCK_TYPE_PREDICTIVE,
PLM_VIDEO_MACROBLOCK_TYPE_B
};
static const plm_vlc_t PLM_VIDEO_CODE_BLOCK_PATTERN[] = {
{ 1 << 1, 0}, { 2 << 1, 0}, // 0: x
{ 3 << 1, 0}, { 4 << 1, 0}, // 1: 0x
{ 5 << 1, 0}, { 6 << 1, 0}, // 2: 1x
{ 7 << 1, 0}, { 8 << 1, 0}, // 3: 00x
{ 9 << 1, 0}, { 10 << 1, 0}, // 4: 01x
{ 11 << 1, 0}, { 12 << 1, 0}, // 5: 10x
{ 13 << 1, 0}, { 0, 60}, // 6: 11x
{ 14 << 1, 0}, { 15 << 1, 0}, // 7: 000x
{ 16 << 1, 0}, { 17 << 1, 0}, // 8: 001x
{ 18 << 1, 0}, { 19 << 1, 0}, // 9: 010x
{ 20 << 1, 0}, { 21 << 1, 0}, // 10: 011x
{ 22 << 1, 0}, { 23 << 1, 0}, // 11: 100x
{ 0, 32}, { 0, 16}, // 12: 101x
{ 0, 8}, { 0, 4}, // 13: 110x
{ 24 << 1, 0}, { 25 << 1, 0}, // 14: 0000x
{ 26 << 1, 0}, { 27 << 1, 0}, // 15: 0001x
{ 28 << 1, 0}, { 29 << 1, 0}, // 16: 0010x
{ 30 << 1, 0}, { 31 << 1, 0}, // 17: 0011x
{ 0, 62}, { 0, 2}, // 18: 0100x
{ 0, 61}, { 0, 1}, // 19: 0101x
{ 0, 56}, { 0, 52}, // 20: 0110x
{ 0, 44}, { 0, 28}, // 21: 0111x
{ 0, 40}, { 0, 20}, // 22: 1000x
{ 0, 48}, { 0, 12}, // 23: 1001x
{ 32 << 1, 0}, { 33 << 1, 0}, // 24: 0000 0x
{ 34 << 1, 0}, { 35 << 1, 0}, // 25: 0000 1x
{ 36 << 1, 0}, { 37 << 1, 0}, // 26: 0001 0x
{ 38 << 1, 0}, { 39 << 1, 0}, // 27: 0001 1x
{ 40 << 1, 0}, { 41 << 1, 0}, // 28: 0010 0x
{ 42 << 1, 0}, { 43 << 1, 0}, // 29: 0010 1x
{ 0, 63}, { 0, 3}, // 30: 0011 0x
{ 0, 36}, { 0, 24}, // 31: 0011 1x
{ 44 << 1, 0}, { 45 << 1, 0}, // 32: 0000 00x
{ 46 << 1, 0}, { 47 << 1, 0}, // 33: 0000 01x
{ 48 << 1, 0}, { 49 << 1, 0}, // 34: 0000 10x
{ 50 << 1, 0}, { 51 << 1, 0}, // 35: 0000 11x
{ 52 << 1, 0}, { 53 << 1, 0}, // 36: 0001 00x
{ 54 << 1, 0}, { 55 << 1, 0}, // 37: 0001 01x
{ 56 << 1, 0}, { 57 << 1, 0}, // 38: 0001 10x
{ 58 << 1, 0}, { 59 << 1, 0}, // 39: 0001 11x
{ 0, 34}, { 0, 18}, // 40: 0010 00x
{ 0, 10}, { 0, 6}, // 41: 0010 01x
{ 0, 33}, { 0, 17}, // 42: 0010 10x
{ 0, 9}, { 0, 5}, // 43: 0010 11x
{ -1, 0}, { 60 << 1, 0}, // 44: 0000 000x
{ 61 << 1, 0}, { 62 << 1, 0}, // 45: 0000 001x
{ 0, 58}, { 0, 54}, // 46: 0000 010x
{ 0, 46}, { 0, 30}, // 47: 0000 011x
{ 0, 57}, { 0, 53}, // 48: 0000 100x
{ 0, 45}, { 0, 29}, // 49: 0000 101x
{ 0, 38}, { 0, 26}, // 50: 0000 110x
{ 0, 37}, { 0, 25}, // 51: 0000 111x
{ 0, 43}, { 0, 23}, // 52: 0001 000x
{ 0, 51}, { 0, 15}, // 53: 0001 001x
{ 0, 42}, { 0, 22}, // 54: 0001 010x
{ 0, 50}, { 0, 14}, // 55: 0001 011x
{ 0, 41}, { 0, 21}, // 56: 0001 100x
{ 0, 49}, { 0, 13}, // 57: 0001 101x
{ 0, 35}, { 0, 19}, // 58: 0001 110x
{ 0, 11}, { 0, 7}, // 59: 0001 111x
{ 0, 39}, { 0, 27}, // 60: 0000 0001x
{ 0, 59}, { 0, 55}, // 61: 0000 0010x
{ 0, 47}, { 0, 31}, // 62: 0000 0011x
};
static const plm_vlc_t PLM_VIDEO_MOTION[] = {
{ 1 << 1, 0}, { 0, 0}, // 0: x
{ 2 << 1, 0}, { 3 << 1, 0}, // 1: 0x
{ 4 << 1, 0}, { 5 << 1, 0}, // 2: 00x
{ 0, 1}, { 0, -1}, // 3: 01x
{ 6 << 1, 0}, { 7 << 1, 0}, // 4: 000x
{ 0, 2}, { 0, -2}, // 5: 001x
{ 8 << 1, 0}, { 9 << 1, 0}, // 6: 0000x
{ 0, 3}, { 0, -3}, // 7: 0001x
{ 10 << 1, 0}, { 11 << 1, 0}, // 8: 0000 0x
{ 12 << 1, 0}, { 13 << 1, 0}, // 9: 0000 1x
{ -1, 0}, { 14 << 1, 0}, // 10: 0000 00x
{ 15 << 1, 0}, { 16 << 1, 0}, // 11: 0000 01x
{ 17 << 1, 0}, { 18 << 1, 0}, // 12: 0000 10x
{ 0, 4}, { 0, -4}, // 13: 0000 11x
{ -1, 0}, { 19 << 1, 0}, // 14: 0000 001x
{ 20 << 1, 0}, { 21 << 1, 0}, // 15: 0000 010x
{ 0, 7}, { 0, -7}, // 16: 0000 011x
{ 0, 6}, { 0, -6}, // 17: 0000 100x
{ 0, 5}, { 0, -5}, // 18: 0000 101x
{ 22 << 1, 0}, { 23 << 1, 0}, // 19: 0000 0011x
{ 24 << 1, 0}, { 25 << 1, 0}, // 20: 0000 0100x
{ 26 << 1, 0}, { 27 << 1, 0}, // 21: 0000 0101x
{ 28 << 1, 0}, { 29 << 1, 0}, // 22: 0000 0011 0x
{ 30 << 1, 0}, { 31 << 1, 0}, // 23: 0000 0011 1x
{ 32 << 1, 0}, { 33 << 1, 0}, // 24: 0000 0100 0x
{ 0, 10}, { 0, -10}, // 25: 0000 0100 1x
{ 0, 9}, { 0, -9}, // 26: 0000 0101 0x
{ 0, 8}, { 0, -8}, // 27: 0000 0101 1x
{ 0, 16}, { 0, -16}, // 28: 0000 0011 00x
{ 0, 15}, { 0, -15}, // 29: 0000 0011 01x
{ 0, 14}, { 0, -14}, // 30: 0000 0011 10x
{ 0, 13}, { 0, -13}, // 31: 0000 0011 11x
{ 0, 12}, { 0, -12}, // 32: 0000 0100 00x
{ 0, 11}, { 0, -11}, // 33: 0000 0100 01x
};
static const plm_vlc_t PLM_VIDEO_DCT_SIZE_LUMINANCE[] = {
{ 1 << 1, 0}, { 2 << 1, 0}, // 0: x
{ 0, 1}, { 0, 2}, // 1: 0x
{ 3 << 1, 0}, { 4 << 1, 0}, // 2: 1x
{ 0, 0}, { 0, 3}, // 3: 10x
{ 0, 4}, { 5 << 1, 0}, // 4: 11x
{ 0, 5}, { 6 << 1, 0}, // 5: 111x
{ 0, 6}, { 7 << 1, 0}, // 6: 1111x
{ 0, 7}, { 8 << 1, 0}, // 7: 1111 1x
{ 0, 8}, { -1, 0}, // 8: 1111 11x
};
static const plm_vlc_t PLM_VIDEO_DCT_SIZE_CHROMINANCE[] = {
{ 1 << 1, 0}, { 2 << 1, 0}, // 0: x
{ 0, 0}, { 0, 1}, // 1: 0x
{ 0, 2}, { 3 << 1, 0}, // 2: 1x
{ 0, 3}, { 4 << 1, 0}, // 3: 11x
{ 0, 4}, { 5 << 1, 0}, // 4: 111x
{ 0, 5}, { 6 << 1, 0}, // 5: 1111x
{ 0, 6}, { 7 << 1, 0}, // 6: 1111 1x
{ 0, 7}, { 8 << 1, 0}, // 7: 1111 11x
{ 0, 8}, { -1, 0}, // 8: 1111 111x
};
static const plm_vlc_t *PLM_VIDEO_DCT_SIZE[] = {
PLM_VIDEO_DCT_SIZE_LUMINANCE,
PLM_VIDEO_DCT_SIZE_CHROMINANCE,
PLM_VIDEO_DCT_SIZE_CHROMINANCE
};
// dct_coeff bitmap:
// 0xff00 run
// 0x00ff level
// Decoded values are unsigned. Sign bit follows in the stream.
static const plm_vlc_uint_t PLM_VIDEO_DCT_COEFF[] = {
{ 1 << 1, 0}, { 0, 0x0001}, // 0: x
{ 2 << 1, 0}, { 3 << 1, 0}, // 1: 0x
{ 4 << 1, 0}, { 5 << 1, 0}, // 2: 00x
{ 6 << 1, 0}, { 0, 0x0101}, // 3: 01x
{ 7 << 1, 0}, { 8 << 1, 0}, // 4: 000x
{ 9 << 1, 0}, { 10 << 1, 0}, // 5: 001x
{ 0, 0x0002}, { 0, 0x0201}, // 6: 010x
{ 11 << 1, 0}, { 12 << 1, 0}, // 7: 0000x
{ 13 << 1, 0}, { 14 << 1, 0}, // 8: 0001x
{ 15 << 1, 0}, { 0, 0x0003}, // 9: 0010x
{ 0, 0x0401}, { 0, 0x0301}, // 10: 0011x
{ 16 << 1, 0}, { 0, 0xffff}, // 11: 0000 0x
{ 17 << 1, 0}, { 18 << 1, 0}, // 12: 0000 1x
{ 0, 0x0701}, { 0, 0x0601}, // 13: 0001 0x
{ 0, 0x0102}, { 0, 0x0501}, // 14: 0001 1x
{ 19 << 1, 0}, { 20 << 1, 0}, // 15: 0010 0x
{ 21 << 1, 0}, { 22 << 1, 0}, // 16: 0000 00x
{ 0, 0x0202}, { 0, 0x0901}, // 17: 0000 10x
{ 0, 0x0004}, { 0, 0x0801}, // 18: 0000 11x
{ 23 << 1, 0}, { 24 << 1, 0}, // 19: 0010 00x
{ 25 << 1, 0}, { 26 << 1, 0}, // 20: 0010 01x
{ 27 << 1, 0}, { 28 << 1, 0}, // 21: 0000 000x
{ 29 << 1, 0}, { 30 << 1, 0}, // 22: 0000 001x
{ 0, 0x0d01}, { 0, 0x0006}, // 23: 0010 000x
{ 0, 0x0c01}, { 0, 0x0b01}, // 24: 0010 001x
{ 0, 0x0302}, { 0, 0x0103}, // 25: 0010 010x
{ 0, 0x0005}, { 0, 0x0a01}, // 26: 0010 011x
{ 31 << 1, 0}, { 32 << 1, 0}, // 27: 0000 0000x
{ 33 << 1, 0}, { 34 << 1, 0}, // 28: 0000 0001x
{ 35 << 1, 0}, { 36 << 1, 0}, // 29: 0000 0010x
{ 37 << 1, 0}, { 38 << 1, 0}, // 30: 0000 0011x
{ 39 << 1, 0}, { 40 << 1, 0}, // 31: 0000 0000 0x
{ 41 << 1, 0}, { 42 << 1, 0}, // 32: 0000 0000 1x
{ 43 << 1, 0}, { 44 << 1, 0}, // 33: 0000 0001 0x
{ 45 << 1, 0}, { 46 << 1, 0}, // 34: 0000 0001 1x
{ 0, 0x1001}, { 0, 0x0502}, // 35: 0000 0010 0x
{ 0, 0x0007}, { 0, 0x0203}, // 36: 0000 0010 1x
{ 0, 0x0104}, { 0, 0x0f01}, // 37: 0000 0011 0x
{ 0, 0x0e01}, { 0, 0x0402}, // 38: 0000 0011 1x
{ 47 << 1, 0}, { 48 << 1, 0}, // 39: 0000 0000 00x
{ 49 << 1, 0}, { 50 << 1, 0}, // 40: 0000 0000 01x
{ 51 << 1, 0}, { 52 << 1, 0}, // 41: 0000 0000 10x
{ 53 << 1, 0}, { 54 << 1, 0}, // 42: 0000 0000 11x
{ 55 << 1, 0}, { 56 << 1, 0}, // 43: 0000 0001 00x
{ 57 << 1, 0}, { 58 << 1, 0}, // 44: 0000 0001 01x
{ 59 << 1, 0}, { 60 << 1, 0}, // 45: 0000 0001 10x
{ 61 << 1, 0}, { 62 << 1, 0}, // 46: 0000 0001 11x
{ -1, 0}, { 63 << 1, 0}, // 47: 0000 0000 000x
{ 64 << 1, 0}, { 65 << 1, 0}, // 48: 0000 0000 001x
{ 66 << 1, 0}, { 67 << 1, 0}, // 49: 0000 0000 010x
{ 68 << 1, 0}, { 69 << 1, 0}, // 50: 0000 0000 011x
{ 70 << 1, 0}, { 71 << 1, 0}, // 51: 0000 0000 100x
{ 72 << 1, 0}, { 73 << 1, 0}, // 52: 0000 0000 101x
{ 74 << 1, 0}, { 75 << 1, 0}, // 53: 0000 0000 110x
{ 76 << 1, 0}, { 77 << 1, 0}, // 54: 0000 0000 111x
{ 0, 0x000b}, { 0, 0x0802}, // 55: 0000 0001 000x
{ 0, 0x0403}, { 0, 0x000a}, // 56: 0000 0001 001x
{ 0, 0x0204}, { 0, 0x0702}, // 57: 0000 0001 010x
{ 0, 0x1501}, { 0, 0x1401}, // 58: 0000 0001 011x
{ 0, 0x0009}, { 0, 0x1301}, // 59: 0000 0001 100x
{ 0, 0x1201}, { 0, 0x0105}, // 60: 0000 0001 101x
{ 0, 0x0303}, { 0, 0x0008}, // 61: 0000 0001 110x
{ 0, 0x0602}, { 0, 0x1101}, // 62: 0000 0001 111x
{ 78 << 1, 0}, { 79 << 1, 0}, // 63: 0000 0000 0001x
{ 80 << 1, 0}, { 81 << 1, 0}, // 64: 0000 0000 0010x
{ 82 << 1, 0}, { 83 << 1, 0}, // 65: 0000 0000 0011x
{ 84 << 1, 0}, { 85 << 1, 0}, // 66: 0000 0000 0100x
{ 86 << 1, 0}, { 87 << 1, 0}, // 67: 0000 0000 0101x
{ 88 << 1, 0}, { 89 << 1, 0}, // 68: 0000 0000 0110x
{ 90 << 1, 0}, { 91 << 1, 0}, // 69: 0000 0000 0111x
{ 0, 0x0a02}, { 0, 0x0902}, // 70: 0000 0000 1000x
{ 0, 0x0503}, { 0, 0x0304}, // 71: 0000 0000 1001x
{ 0, 0x0205}, { 0, 0x0107}, // 72: 0000 0000 1010x
{ 0, 0x0106}, { 0, 0x000f}, // 73: 0000 0000 1011x
{ 0, 0x000e}, { 0, 0x000d}, // 74: 0000 0000 1100x
{ 0, 0x000c}, { 0, 0x1a01}, // 75: 0000 0000 1101x
{ 0, 0x1901}, { 0, 0x1801}, // 76: 0000 0000 1110x
{ 0, 0x1701}, { 0, 0x1601}, // 77: 0000 0000 1111x
{ 92 << 1, 0}, { 93 << 1, 0}, // 78: 0000 0000 0001 0x
{ 94 << 1, 0}, { 95 << 1, 0}, // 79: 0000 0000 0001 1x
{ 96 << 1, 0}, { 97 << 1, 0}, // 80: 0000 0000 0010 0x
{ 98 << 1, 0}, { 99 << 1, 0}, // 81: 0000 0000 0010 1x
{100 << 1, 0}, {101 << 1, 0}, // 82: 0000 0000 0011 0x
{102 << 1, 0}, {103 << 1, 0}, // 83: 0000 0000 0011 1x
{ 0, 0x001f}, { 0, 0x001e}, // 84: 0000 0000 0100 0x
{ 0, 0x001d}, { 0, 0x001c}, // 85: 0000 0000 0100 1x
{ 0, 0x001b}, { 0, 0x001a}, // 86: 0000 0000 0101 0x
{ 0, 0x0019}, { 0, 0x0018}, // 87: 0000 0000 0101 1x
{ 0, 0x0017}, { 0, 0x0016}, // 88: 0000 0000 0110 0x
{ 0, 0x0015}, { 0, 0x0014}, // 89: 0000 0000 0110 1x
{ 0, 0x0013}, { 0, 0x0012}, // 90: 0000 0000 0111 0x
{ 0, 0x0011}, { 0, 0x0010}, // 91: 0000 0000 0111 1x
{104 << 1, 0}, {105 << 1, 0}, // 92: 0000 0000 0001 00x
{106 << 1, 0}, {107 << 1, 0}, // 93: 0000 0000 0001 01x
{108 << 1, 0}, {109 << 1, 0}, // 94: 0000 0000 0001 10x
{110 << 1, 0}, {111 << 1, 0}, // 95: 0000 0000 0001 11x
{ 0, 0x0028}, { 0, 0x0027}, // 96: 0000 0000 0010 00x
{ 0, 0x0026}, { 0, 0x0025}, // 97: 0000 0000 0010 01x
{ 0, 0x0024}, { 0, 0x0023}, // 98: 0000 0000 0010 10x
{ 0, 0x0022}, { 0, 0x0021}, // 99: 0000 0000 0010 11x
{ 0, 0x0020}, { 0, 0x010e}, // 100: 0000 0000 0011 00x
{ 0, 0x010d}, { 0, 0x010c}, // 101: 0000 0000 0011 01x
{ 0, 0x010b}, { 0, 0x010a}, // 102: 0000 0000 0011 10x
{ 0, 0x0109}, { 0, 0x0108}, // 103: 0000 0000 0011 11x
{ 0, 0x0112}, { 0, 0x0111}, // 104: 0000 0000 0001 000x
{ 0, 0x0110}, { 0, 0x010f}, // 105: 0000 0000 0001 001x
{ 0, 0x0603}, { 0, 0x1002}, // 106: 0000 0000 0001 010x
{ 0, 0x0f02}, { 0, 0x0e02}, // 107: 0000 0000 0001 011x
{ 0, 0x0d02}, { 0, 0x0c02}, // 108: 0000 0000 0001 100x
{ 0, 0x0b02}, { 0, 0x1f01}, // 109: 0000 0000 0001 101x
{ 0, 0x1e01}, { 0, 0x1d01}, // 110: 0000 0000 0001 110x
{ 0, 0x1c01}, { 0, 0x1b01}, // 111: 0000 0000 0001 111x
};
typedef struct {
int full_px;
int is_set;
int r_size;
int h;
int v;
} plm_video_motion_t;
typedef struct plm_video_t {
double framerate;
double time;
int frames_decoded;
int width;
int height;
int mb_width;
int mb_height;
int mb_size;
int luma_width;
int luma_height;
int chroma_width;
int chroma_height;
int start_code;
int picture_type;
plm_video_motion_t motion_forward;
plm_video_motion_t motion_backward;
int has_sequence_header;
int quantizer_scale;
int slice_begin;
int macroblock_address;
int mb_row;
int mb_col;
int macroblock_type;
int macroblock_intra;
int dc_predictor[3];
plm_buffer_t *buffer;
int destroy_buffer_when_done;
plm_frame_t frame_current;
plm_frame_t frame_forward;
plm_frame_t frame_backward;
uint8_t *frames_data;
int block_data[64];
uint8_t intra_quant_matrix[64];
uint8_t non_intra_quant_matrix[64];
int has_reference_frame;
int assume_no_b_frames;
} plm_video_t;
static inline uint8_t plm_clamp(int n) {
if (n > 255) {
n = 255;
}
else if (n < 0) {
n = 0;
}
return n;
}
int plm_video_decode_sequence_header(plm_video_t *self);
void plm_video_init_frame(plm_video_t *self, plm_frame_t *frame, uint8_t *base);
void plm_video_decode_picture(plm_video_t *self);
void plm_video_decode_slice(plm_video_t *self, int slice);
void plm_video_decode_macroblock(plm_video_t *self);
void plm_video_decode_motion_vectors(plm_video_t *self);
int plm_video_decode_motion_vector(plm_video_t *self, int r_size, int motion);
void plm_video_predict_macroblock(plm_video_t *self);
void plm_video_copy_macroblock(plm_video_t *self, plm_frame_t *s, int motion_h, int motion_v);
void plm_video_interpolate_macroblock(plm_video_t *self, plm_frame_t *s, int motion_h, int motion_v);
void plm_video_process_macroblock(plm_video_t *self, uint8_t *s, uint8_t *d, int mh, int mb, int bs, int interp);
void plm_video_decode_block(plm_video_t *self, int block);
void plm_video_idct(int *block);
plm_video_t * plm_video_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done) {
plm_video_t *self = (plm_video_t *)malloc(sizeof(plm_video_t));
memset(self, 0, sizeof(plm_video_t));
self->buffer = buffer;
self->destroy_buffer_when_done = destroy_when_done;
// Attempt to decode the sequence header
self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_SEQUENCE);
if (self->start_code != -1) {
plm_video_decode_sequence_header(self);
}
return self;
}
void plm_video_destroy(plm_video_t *self) {
if (self->destroy_buffer_when_done) {
plm_buffer_destroy(self->buffer);
}
if (self->has_sequence_header) {
free(self->frames_data);
}
free(self);
}
double plm_video_get_framerate(plm_video_t *self) {
return plm_video_has_header(self)
? self->framerate
: 0;
}
int plm_video_get_width(plm_video_t *self) {
return plm_video_has_header(self)
? self->width
: 0;
}
int plm_video_get_height(plm_video_t *self) {
return plm_video_has_header(self)
? self->height
: 0;
}
void plm_video_set_no_delay(plm_video_t *self, int no_delay) {
self->assume_no_b_frames = no_delay;
}
double plm_video_get_time(plm_video_t *self) {
return self->time;
}
void plm_video_set_time(plm_video_t *self, double time) {
self->frames_decoded = self->framerate * time;
self->time = time;
}
void plm_video_rewind(plm_video_t *self) {
plm_buffer_rewind(self->buffer);
self->time = 0;
self->frames_decoded = 0;
self->has_reference_frame = FALSE;
self->start_code = -1;
}
int plm_video_has_ended(plm_video_t *self) {
return plm_buffer_has_ended(self->buffer);
}
plm_frame_t *plm_video_decode(plm_video_t *self) {
if (!plm_video_has_header(self)) {
return NULL;
}
plm_frame_t *frame = NULL;
do {
if (self->start_code != PLM_START_PICTURE) {
self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_PICTURE);
if (self->start_code == -1) {
// If we reached the end of the file and the previously decoded
// frame was a reference frame, we still have to return it.
if (
self->has_reference_frame &&
!self->assume_no_b_frames &&
plm_buffer_has_ended(self->buffer) && (
self->picture_type == PLM_VIDEO_PICTURE_TYPE_INTRA ||
self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE
)
) {
self->has_reference_frame = FALSE;
frame = &self->frame_backward;
break;
}
return NULL;
}
}
// Make sure we have a full picture in the buffer before attempting to
// decode it. Sadly, this can only be done by seeking for the start code
// of the next picture. Also, if we didn't find the start code for the
// next picture, but the source has ended, we assume that this last
// picture is in the buffer.
if (
plm_buffer_has_start_code(self->buffer, PLM_START_PICTURE) == -1 &&
!plm_buffer_has_ended(self->buffer)
) {
return NULL;
}
plm_video_decode_picture(self);
if (self->assume_no_b_frames) {
frame = &self->frame_backward;
}
else if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) {
frame = &self->frame_current;
}
else if (self->has_reference_frame) {
frame = &self->frame_forward;
}
else {
self->has_reference_frame = TRUE;
}
} while (!frame);
frame->time = self->time;
self->frames_decoded++;
self->time = (double)self->frames_decoded / self->framerate;
return frame;
}
int plm_video_has_header(plm_video_t *self) {
if (self->has_sequence_header) {
return TRUE;
}
if (self->start_code != PLM_START_SEQUENCE) {
self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_SEQUENCE);
}
if (self->start_code == -1) {
return FALSE;
}
if (!plm_video_decode_sequence_header(self)) {
return FALSE;
}
return TRUE;
}
int plm_video_decode_sequence_header(plm_video_t *self) {
int max_header_size = 64 + 2 * 64 * 8; // 64 bit header + 2x 64 byte matrix
if (!plm_buffer_has(self->buffer, max_header_size)) {
return FALSE;
}
self->width = plm_buffer_read(self->buffer, 12);
self->height = plm_buffer_read(self->buffer, 12);
if (self->width <= 0 || self->height <= 0) {
return FALSE;
}
// Skip pixel aspect ratio
plm_buffer_skip(self->buffer, 4);
self->framerate = PLM_VIDEO_PICTURE_RATE[plm_buffer_read(self->buffer, 4)];
// Skip bit_rate, marker, buffer_size and constrained bit
plm_buffer_skip(self->buffer, 18 + 1 + 10 + 1);
// Load custom intra quant matrix?
if (plm_buffer_read(self->buffer, 1)) {
for (int i = 0; i < 64; i++) {
int idx = PLM_VIDEO_ZIG_ZAG[i];
self->intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8);
}
}
else {
memcpy(self->intra_quant_matrix, PLM_VIDEO_INTRA_QUANT_MATRIX, 64);
}
// Load custom non intra quant matrix?
if (plm_buffer_read(self->buffer, 1)) {
for (int i = 0; i < 64; i++) {
int idx = PLM_VIDEO_ZIG_ZAG[i];
self->non_intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8);
}
}
else {
memcpy(self->non_intra_quant_matrix, PLM_VIDEO_NON_INTRA_QUANT_MATRIX, 64);
}
self->mb_width = (self->width + 15) >> 4;
self->mb_height = (self->height + 15) >> 4;
self->mb_size = self->mb_width * self->mb_height;
self->luma_width = self->mb_width << 4;
self->luma_height = self->mb_height << 4;
self->chroma_width = self->mb_width << 3;
self->chroma_height = self->mb_height << 3;
// Allocate one big chunk of data for all 3 frames = 9 planes
size_t luma_plane_size = self->luma_width * self->luma_height;
size_t chroma_plane_size = self->chroma_width * self->chroma_height;
size_t frame_data_size = (luma_plane_size + 2 * chroma_plane_size);
self->frames_data = (uint8_t*)malloc(frame_data_size * 3);
plm_video_init_frame(self, &self->frame_current, self->frames_data + frame_data_size * 0);
plm_video_init_frame(self, &self->frame_forward, self->frames_data + frame_data_size * 1);
plm_video_init_frame(self, &self->frame_backward, self->frames_data + frame_data_size * 2);
self->has_sequence_header = TRUE;
return TRUE;
}
void plm_video_init_frame(plm_video_t *self, plm_frame_t *frame, uint8_t *base) {
size_t luma_plane_size = self->luma_width * self->luma_height;
size_t chroma_plane_size = self->chroma_width * self->chroma_height;
frame->width = self->width;
frame->height = self->height;
frame->y.width = self->luma_width;
frame->y.height = self->luma_height;
frame->y.data = base;
frame->cr.width = self->chroma_width;
frame->cr.height = self->chroma_height;
frame->cr.data = base + luma_plane_size;
frame->cb.width = self->chroma_width;
frame->cb.height = self->chroma_height;
frame->cb.data = base + luma_plane_size + chroma_plane_size;
}
void plm_video_decode_picture(plm_video_t *self) {
plm_buffer_skip(self->buffer, 10); // skip temporalReference
self->picture_type = plm_buffer_read(self->buffer, 3);
plm_buffer_skip(self->buffer, 16); // skip vbv_delay
// D frames or unknown coding type
if (self->picture_type <= 0 || self->picture_type > PLM_VIDEO_PICTURE_TYPE_B) {
return;
}
// Forward full_px, f_code
if (
self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE ||
self->picture_type == PLM_VIDEO_PICTURE_TYPE_B
) {
self->motion_forward.full_px = plm_buffer_read(self->buffer, 1);
int f_code = plm_buffer_read(self->buffer, 3);
if (f_code == 0) {
// Ignore picture with zero f_code
return;
}
self->motion_forward.r_size = f_code - 1;
}
// Backward full_px, f_code
if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) {
self->motion_backward.full_px = plm_buffer_read(self->buffer, 1);
int f_code = plm_buffer_read(self->buffer, 3);
if (f_code == 0) {
// Ignore picture with zero f_code
return;
}
self->motion_backward.r_size = f_code - 1;
}
plm_frame_t frame_temp = self->frame_forward;
if (
self->picture_type == PLM_VIDEO_PICTURE_TYPE_INTRA ||
self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE
) {
self->frame_forward = self->frame_backward;
}
// Find first slice start code; skip extension and user data
do {
self->start_code = plm_buffer_next_start_code(self->buffer);
} while (
self->start_code == PLM_START_EXTENSION ||
self->start_code == PLM_START_USER_DATA
);
// Decode all slices
while (PLM_START_IS_SLICE(self->start_code)) {
plm_video_decode_slice(self, self->start_code & 0x000000FF);
if (self->macroblock_address >= self->mb_size - 2) {
break;
}
self->start_code = plm_buffer_next_start_code(self->buffer);
}
// If this is a reference picture rotate the prediction pointers
if (
self->picture_type == PLM_VIDEO_PICTURE_TYPE_INTRA ||
self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE
) {
self->frame_backward = self->frame_current;
self->frame_current = frame_temp;
}
}
void plm_video_decode_slice(plm_video_t *self, int slice) {
self->slice_begin = TRUE;
self->macroblock_address = (slice - 1) * self->mb_width - 1;
// Reset motion vectors and DC predictors
self->motion_backward.h = self->motion_forward.h = 0;
self->motion_backward.v = self->motion_forward.v = 0;
self->dc_predictor[0] = 128;
self->dc_predictor[1] = 128;
self->dc_predictor[2] = 128;
self->quantizer_scale = plm_buffer_read(self->buffer, 5);
// Skip extra
while (plm_buffer_read(self->buffer, 1)) {
plm_buffer_skip(self->buffer, 8);
}
do {
plm_video_decode_macroblock(self);
} while (
self->macroblock_address < self->mb_size - 1 &&
plm_buffer_no_start_code(self->buffer)
);
}
void plm_video_decode_macroblock(plm_video_t *self) {
// Decode increment
int increment = 0;
int t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT);
while (t == 34) {
// macroblock_stuffing
t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT);
}
while (t == 35) {
// macroblock_escape
increment += 33;
t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT);
}
increment += t;
// Process any skipped macroblocks
if (self->slice_begin) {
// The first increment of each slice is relative to beginning of the
// preverious row, not the preverious macroblock
self->slice_begin = FALSE;
self->macroblock_address += increment;
}
else {