/* * sppe.h — scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine * * Pure C11 playback core for iOS / tvOS / macOS / Android. * * The core owns: demux (our FFmpeg), the master clock, A/V sync, track * selection, seek, buffering, subtitle decode + render (libass / PGS -> RGBA), * audio decode to PCM, and software fallback. * * The OS does ONLY hardware decode + presentation, through the `sppe_backend` * vtable implemented by the thin platform repos: * Apple — VideoToolbox + AVSampleBufferDisplayLayer/AudioRenderer * Android — AMediaCodec + ANativeWindow + AAudio * * Contains zero Objective-C / Swift / Java / WinRT. * * Units: 100 ns ticks ("ticks"), int64_t. Same unit as sppb_ and TimeSpan. * Threading: one instance is used from one thread at a time. No internal threads. * Returned strings are engine-owned and valid until the next call that changes them. * * SPDX-License-Identifier: MIT */ #ifndef SPPE_H #define SPPE_H #include #include #ifdef __cplusplus extern "C" { #endif #if defined(_WIN32) # if defined(SPPE_BUILD) # define SPPE_API __declspec(dllexport) # else # define SPPE_API __declspec(dllimport) # endif #else # define SPPE_API __attribute__((visibility("default"))) #endif #define SPPE_VERSION "1.0.0" #define SPPE_BACKEND_ABI 1 /* ---------------------------------------------------------------- errors -- */ /* 0 and positive values are non-error outcomes; everything else is negative. */ #define SPPE_OK 0 #define SPPE_AGAIN 1 /* need more input / nothing due yet */ #define SPPE_EOF 2 /* end of stream */ #define SPPE_ERR_TRANSPORT -1 #define SPPE_ERR_FORMAT -2 #define SPPE_ERR_CODEC -3 #define SPPE_ERR_ARG -4 #define SPPE_ERR_OOM -5 #define SPPE_ERR_NOT_FOUND -6 #define SPPE_ERR_SEEK -7 #define SPPE_ERR_STATE -8 #define SPPE_ERR_UNSUPPORTED -9 #define SPPE_ERR_CLOSED -10 #define SPPE_ERR_IO -11 #define SPPE_ERR_HW -12 /* ---------------------------------------------------------------- io seam - */ /* * The only transport seam. iOS has no libcurl, so the caller always supplies * the bytes. `read` is the single place bytes_read is counted. * * read : >0 bytes read, 0 at EOF, <0 on error. Short reads are legal. * seek : SEEK_SET/CUR/END. whence 0x10000 (AVSEEK_SIZE) asks for the total * size: return it, or -1 if unknown (do not fail). * size : total bytes, or -1 if unknown. * interrupt: polled during long FFmpeg operations; non-zero aborts. NULL ok. */ typedef struct sppe_io { void *ctx; int (*read)(void *ctx, uint8_t *buf, int n); int64_t (*seek)(void *ctx, int64_t offset, int whence); int64_t (*size)(void *ctx); int (*interrupt)(void *ctx); void *reserved[4]; } sppe_io; /* ------------------------------------------------------------- backend --- */ typedef struct sppe_codec_par { const char *codec_name; const uint8_t *extradata; int extradata_size; int width, height; int profile, level; int pix_fmt; int bit_depth; int sample_rate, channels; int color_primaries, color_trc, color_space, color_range; int tb_num, tb_den; /* the stream time base — pts ticks are scaled by it */ const void *av_codecpar; /* escape hatch: const AVCodecParameters* */ } sppe_codec_par; typedef struct sppe_hw_frame { void *handle; /* CVPixelBufferRef / AHardwareBuffer* — opaque here */ int width, height; int pix_fmt; int64_t pts_ticks; void *opaque; /* backend-private (e.g. the CMSampleBuffer) */ } sppe_hw_frame; typedef struct sppe_hdr_meta { int present; int transfer; /* 0 SDR, 1 PQ, 2 HLG */ int primaries; /* 1 BT.709, 9 BT.2020 */ int matrix; int full_range; int has_mastering; int prim_x[3], prim_y[3], wp_x, wp_y; int64_t min_lum, max_lum; int has_cll; int max_cll, max_fall; int has_dovi; int dv_profile, dv_level, dv_bl_signal_compatibility_id, dv_rpu_present; } sppe_hdr_meta; typedef struct sppe_sw_frame { int width, height, pix_fmt; const uint8_t *data[4]; int linesize[4]; } sppe_sw_frame; #define SPPE_SUB_PRESENTED 0 #define SPPE_SUB_DROPPED 1 #define SPPE_SUB_HELD 2 typedef struct sppe_backend { int abi; /* must be SPPE_BACKEND_ABI */ const char *name; void *ctx; /* ---- hardware decoder ------------------------------------------------ */ int (*hw_probe)(void *ctx, const sppe_codec_par *par); void *(*hw_open)(void *ctx, const sppe_codec_par *par); int (*hw_submit)(void *ctx, void *dec, const void *pkt_data, int pkt_size, int64_t pts, int64_t dts, int key); /* 0 = frame in *out, 1 = need more input, 2 = EOF, <0 = runtime error * (a runtime error triggers the documented software fallback). */ int (*hw_receive)(void *ctx, void *dec, sppe_hw_frame *out); int (*hw_flush)(void *ctx, void *dec); void (*hw_close)(void *ctx, void *dec); void (*hw_frame_free)(void *ctx, sppe_hw_frame *f); /* ---- sinks ----------------------------------------------------------- */ int (*video_present)(void *ctx, const sppe_hw_frame *hw, const sppe_sw_frame *sw, int64_t pts_ticks, int64_t duration_ticks); int (*video_set_hdr)(void *ctx, const sppe_hdr_meta *meta); int (*audio_configure)(void *ctx, int sample_rate, int channels, int sample_fmt, const char *layout_name); int (*audio_write)(void *ctx, const uint8_t *const *planes, const int *linesize, int nb_samples, int64_t pts_ticks); int64_t (*audio_clock_now)(void *ctx); int (*audio_flush)(void *ctx); int (*subtitle_overlay)(void *ctx, const uint8_t *rgba, int width, int height, int stride, int x, int y, int64_t pts_ticks, int64_t duration_ticks); int (*subtitle_clear)(void *ctx); } sppe_backend; /* ------------------------------------------------------------- lifecycle -- */ typedef struct sppe sppe_t; SPPE_API sppe_t *sppe_open_path(const char *utf8_path_or_url, const char *config_json); SPPE_API sppe_t *sppe_open_io(const sppe_io *io, const char *name_hint, const char *config_json); /* Frees the instance; the pointer must not be used again (no double close). */ SPPE_API void sppe_close(sppe_t *e); SPPE_API int sppe_is_closed(const sppe_t *e); SPPE_API const char *sppe_version(void); /* ---------------------------------------------------------------- config -- */ /* Replaces the whole config. A MALFORMED json is a typed error * (SPPE_ERR_ARG, message in sppe_last_error) — silently falling back to * defaults would hide a typo in an app's config. Unknown KEYS are ignored; * out-of-range values are clamped. */ SPPE_API int sppe_set_config(sppe_t *e, const char *config_json); /* ------------------------------------------------------------------ info -- */ /* streams, HDR/DV metadata, chapters, attachments. */ SPPE_API const char *sppe_info_json(sppe_t *e); SPPE_API const char *sppe_status_json(sppe_t *e); SPPE_API int sppe_nb_streams(const sppe_t *e); SPPE_API int sppe_stream_index_at(const sppe_t *e, int ordinal); SPPE_API const char *sppe_stream_json(const sppe_t *e, int index); SPPE_API int64_t sppe_duration_ticks(sppe_t *e); SPPE_API int64_t sppe_bytes_read(const sppe_t *e); /* --------------------------------------------------------------- control -- */ /* index < 0 = keep the current one, -2 = disable. */ SPPE_API int sppe_select(sppe_t *e, int video, int audio, int sub_primary, int sub_secondary); SPPE_API int sppe_selected(const sppe_t *e, int kind); /* 0 video 1 audio 2 sub 3 sub2 */ SPPE_API int sppe_play(sppe_t *e); SPPE_API int sppe_pause(sppe_t *e); SPPE_API int sppe_is_playing(const sppe_t *e); SPPE_API int sppe_set_rate(sppe_t *e, double rate); SPPE_API int sppe_seek(sppe_t *e, int64_t ticks, int mode); /* 0 accurate 1 fast */ SPPE_API int sppe_set_audio_delay(sppe_t *e, int64_t ticks); SPPE_API int64_t sppe_clock_now(sppe_t *e); SPPE_API int sppe_eof(const sppe_t *e); /* One step of the engine loop. max_wait_ms 0 = non-blocking. * Returns SPPE_OK (did work), SPPE_AGAIN (nothing due), SPPE_EOF (finished), * or a negative error. */ SPPE_API int sppe_pump(sppe_t *e, int max_wait_ms); /* --------------------------------------------------------------- backend -- */ SPPE_API int sppe_install_backend(sppe_t *e, const sppe_backend *b); SPPE_API const char *sppe_backend_name(const sppe_t *e); /* ------------------------------------------------------- test backend ----- */ /* * The fake backend (hw=none or simulated hw, sinks = recorders) is installed by * the HOST, not by a platform repo, so the whole pipeline can run headless from * any language binding. `opts_json` is {"hw":"h264,hevc"}, {"hw":"all"}, * {"hw":"","hw_fail_frame":120}, {"clock":""}. * * These two exports exist so a C#/Swift/JNI harness can drive the pipeline * without building a platform backend. A shipping app never calls them. */ SPPE_API int sppe_install_fake_backend(sppe_t *e, const char *opts_json); SPPE_API const char *sppe_fake_log_json(sppe_t *e); /* `presented` counts VIDEO frames + AUDIO writes, so an audio-only file is not * indistinguishable from a stalled one. */ SPPE_API int sppe_fake_counters(sppe_t *e, int *presented, int *dropped, int *repeated, int *sub_overlays); /* VIDEO frames only: the number status_json reports as `presented`. */ SPPE_API int sppe_fake_video_frames(sppe_t *e); /* Geometry + FNV-1a of the last subtitle overlay, so a test can assert the * pixels rather than only that a callback fired. */ SPPE_API int sppe_fake_last_overlay(sppe_t *e, int *w, int *h, int *x, int *y, unsigned *crc); /* ------------------------------------------------------------- subtitles -- */ /* Cue list for the selected subtitle stream (text cues carry their bytes). */ SPPE_API int sppe_sub_count(const sppe_t *e); SPPE_API int sppe_sub_index_at(const sppe_t *e, int ordinal); SPPE_API const char *sppe_sub_json(const sppe_t *e, int ordinal); SPPE_API const char *sppe_sub_text(const sppe_t *e, int ordinal); /* Render the overlay due at `at_ticks` for the SELECTED subtitle stream(s) * through the backend's subtitle sink. Nothing renders while no subtitle stream * is selected — that is the "subtitles off" state, not an error. * Returns SPPE_OK if an overlay was pushed, SPPE_AGAIN if nothing changed. */ SPPE_API int sppe_render_subtitle(sppe_t *e, int64_t at_ticks); /* ---------------------------------------------------------------- oracles -- */ /* ffmpeg `-f framemd5` compatible dump of the software decode path. */ SPPE_API int sppe_framemd5(sppe_t *e, int stream_index, const char *pix_fmt); SPPE_API const char *sppe_framemd5_text(sppe_t *e); /* The pts the last seek landed on (compared with ffprobe keyframes). */ SPPE_API int64_t sppe_last_seek_landed_ticks(const sppe_t *e); /* ----------------------------------------------------------------- errors -- */ /* When sppe_open_* returns NULL there is no instance to ask, so the reason is * kept process-wide. Same lifetime rules as the per-instance string. */ SPPE_API const char *sppe_last_open_error(void); SPPE_API int sppe_last_open_error_code(void); SPPE_API const char *sppe_last_error(const sppe_t *e); SPPE_API int sppe_last_error_code(const sppe_t *e); #ifdef __cplusplus } #endif #endif /* SPPE_H */