/* * 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.2.0" /* The backend vtable ABI this header declares. The core ACCEPTS 1..2 * (ADR-0001 §7.1): append-only, an ABI-1 backend keeps working unchanged. */ #define SPPE_BACKEND_ABI 2 #define SPPE_BACKEND_ABI_1 1 #define SPPE_BACKEND_ABI_MIN 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); /* ---------------- ABI 2 (additive; ABI-1 backends leave these NULL) --------- * ADR-0001 §7.2. The core reads NONE of these unless `abi >= 2`, so an ABI-1 * backend compiled against the smaller struct stays valid. Every entry is * optional: NULL means "not supported", never an error. */ /* Display refresh, ns, or 0 = unknown. (declared; not yet called by the core) */ int64_t (*video_vsync_period_ns)(void *ctx, int *width, int *height); /* Present with pacing. 1 = shown/queued, 0 = too late (drop), <0 = sink error. * (declared; not yet called by the core) */ int (*video_present2)(void *ctx, const sppe_hw_frame *hw, const sppe_sw_frame *sw, int64_t pts_ticks, int64_t duration_ticks, int64_t vsync_target_ticks, int flags); /* Per-frame HDR/DV switch. (declared; not yet called by the core) */ int (*video_set_hdr2)(void *ctx, const sppe_hdr_meta *meta, int64_t pts_ticks); /* * Audio config for compressed transport (HDMI / S/PDIF bitstream). * * spdif_format "ac3" | "eac3" | "dts" | "truehd" | NULL (= PCM, behave like * audio_configure). * sample_rate the IEC 61937 TRANSPORT rate, not the codec rate: * ac3/dts = codec rate, eac3 = 4 x codec rate (192 kHz for * 48 kHz content), truehd = 4 x codec rate (192 kHz) on 8 ch. * channels transport channels: 2, or 8 for truehd (HBR). * sample_fmt always AV_SAMPLE_FMT_S16 (1): 16-bit little-endian words. * * Return 0 = the sink accepted bitstream: from now on `audio_write` receives * IEC 61937 bursts in planes[0], `linesize[0]` = the burst size in BYTES and * `nb_samples` = transport frames (bytes / (2 * channels)). * Any non-zero return = refused; the core falls back to decoding to PCM and * calls `audio_configure` as an ABI-1 core would. The sink's capability * (HDMI ELD, PipeWire iec958 codecs) is authoritative — the core never forces. */ int (*audio_configure_ex)(void *ctx, int sample_rate, int channels, int sample_fmt, const char *layout_name, const char *spdif_format, const void *extradata, int extradata_size); /* The timeline jumped (seek, flush, track switch): drop everything queued at * or after at_pts. Called by the core after every seek when non-NULL. */ int (*audio_discontinuity)(void *ctx, int64_t at_pts_ticks); /* (declared; not yet called by the core) */ void *(*hw_device_open)(void *ctx, const char *kind, const char *opts_json); void (*hw_device_close)(void *ctx, void *device); int (*hw_frame_export)(void *ctx, const sppe_hw_frame *f, int *fds, int max_fds, uint64_t *modifier, int *fourcc, int *stride, int *offset); int (*hw_submit_packet)(void *ctx, void *dec, const void *av_packet); void *reserved2[4]; /* must be zero; room for the next bump */ } 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. * * Audio passthrough (v1.1.0, needs an ABI-2 backend with audio_configure_ex): * "passthrough": "ac3,eac3,dts,truehd" codecs the shell WANTS bitstreamed * (default "" = always decode to PCM). * The backend still has the final word: a refused audio_configure_ex falls back * to PCM, and sppe_status_json reports "audio_out":"pcm"|"bitstream:". */ 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); /* =========================================== v1.1.0 — ADR-0001 §4 phase B/C == * The getters a shell that came from sppb_ (WinRT component, WinUI3, Xbox) * needs without parsing info_json, and the TPlayer product features with no * core support before (external subtitles, subtitle delay, screenshot, remux). * Strings are engine-owned and valid until the next call on the instance. */ /* chapters: hidden unless count >= config "chapters_min" (default 2, §3.4). * field: "title" | "start_ticks" | "end_ticks" | "id" */ SPPE_API int sppe_chapter_count(const sppe_t *e); SPPE_API const char *sppe_chapter_field(sppe_t *e, int i, const char *field); /* attachments (fonts, cover art). field: "filename" | "mimetype" | "bytes" | "stream" */ SPPE_API int sppe_attachment_count(const sppe_t *e); SPPE_API const char *sppe_attachment_field(sppe_t *e, int i, const char *field); /* bytes written, or the size needed when dst is NULL/too small, or <0 */ SPPE_API int sppe_attachment_data(const sppe_t *e, int i, uint8_t *dst, int cap); /* the container start the zero-based timeline is shifted by (§3.1) */ SPPE_API int64_t sppe_start_time_ticks(const sppe_t *e); SPPE_API const char *sppe_get_config(sppe_t *e); /* the effective config, JSON */ /* subtitle delay: positive = later; clamped to ±600 s; no re-scan */ SPPE_API int sppe_set_subtitle_delay(sppe_t *e, int64_t ticks); SPPE_API int64_t sppe_subtitle_delay(const sppe_t *e); /* sidecar .srt/.ass/.vtt/... -> a new subtitle stream (decoded once). * Returns the stream index (select it with sppe_select), or <0. Max 8. */ SPPE_API int sppe_add_external_subtitle(sppe_t *e, const char *utf8_path); SPPE_API int sppe_add_external_subtitle_io(sppe_t *e, const sppe_io *io, const char *name_hint); /* cue enumeration for shells that render text natively. stream_index < 0 = * the selected primary. Writes {"stream","start_ticks","end_ticks","kind", * "text","ass"} (delay applied). SPPE_OK / SPPE_EOF / <0 (ERR_ARG if cap is * too small — retry with more). A seek restarts at the landing. */ SPPE_API int sppe_next_subtitle(sppe_t *e, int stream_index, char *out_json, int cap); /* drop queues/decoder/pending state without seeking; bumps the epoch */ SPPE_API int sppe_flush(sppe_t *e); SPPE_API uint32_t sppe_source_epoch(const sppe_t *e); /* +1 per seek/flush */ /* {video_packets, video_capacity, audio_packets, audio_capacity, ahead_ticks, * bytes_read, source_size, eof, demux_guard_tripped, epoch} */ SPPE_API const char *sppe_buffer_status(sppe_t *e); /* <0 leaves a value unchanged; 0 = no timeout */ SPPE_API int sppe_set_timeouts(sppe_t *e, int open_ms, int read_ms); SPPE_API int sppe_timeouts(const sppe_t *e, int *open_ms, int *read_ms); /* FFmpeg codec name -> 2 = the installed backend claims hardware, 1 = a * software decoder in this FFmpeg, 0 = no. e may be NULL (software only). */ SPPE_API int sppe_codec_supported(sppe_t *e, const char *codec_name); /* screenshot at at_ticks (<0 = now), RGBA w x h (0 = source size). Returns * bytes written, the size needed when dst is NULL/too small, or <0. The * playback position is restored. */ SPPE_API int sppe_grab_frame(sppe_t *e, int64_t at_ticks, int w, int h, uint8_t *dst, int cap); /* 1 = wrote PNG, 2 = wrote PPM (no PNG encoder in this FFmpeg), <0 error */ SPPE_API int sppe_grab_frame_to_file(sppe_t *e, int64_t at_ticks, int w, int h, const char *utf8_path); /* remux video (from video_path) + audio (from audio_path) into out_path, no * decode. out_format NULL = from the extension. SPPE_OK or <0. */ SPPE_API int sppe_combine(const char *video_path, const char *audio_path, const char *out_path, const char *out_format); SPPE_API const char *sppe_combine_last_error(void); /* ------------------------------------------------------------- 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); /* ============================================ v1.2.0 — live cue overrides ==== * Live subtitle translation / dual-language display (SPEC §16). Additive: the * backend ABI is unchanged (still 2). Cue ordinals in this block are PER * STREAM — the 0-based index into that stream's cue table — and are stable * (cues are only ever appended). stream_index < 0 = the selected primary. * * sppe_sub_peek_window: NON-BLOCKING. Never runs the one-pass subtitle scan, * never seeks, never reads the source; it returns what the cue table already * holds (all cues for an external / a scanned local file, the cues inline * ingest has buffered ahead on a remote one). Writes a JSON array of the TEXT * cues overlapping [from_ticks, to_ticks] (delay applied), in start order: * [{"stream","ordinal","start_ms","end_ms","start_ticks","end_ticks", * "text" (plain: ASS tags stripped, \N -> newline), "ass" (the ASS Text * field), "override" (only when one is set)}] * Bitmap (PGS/DVB) cues are never listed. Returns the number of cues (>= 0), * SPPE_ERR_NOT_FOUND (not a subtitle stream), or SPPE_ERR_ARG (cap too small — * retry with more; out_json is set to ""). * * sppe_sub_set_cue_override: replaces the DISPLAYED text of one text cue * (utf8_text NULL = clear that cue's override). "\n" becomes libass's hard * break, so "Original\nTranslated" renders as two lines in the cue's own * style and position. The next sppe_render_subtitle / sppe_pump re-renders * immediately. The original text/ass (sppe_sub_json, sppe_next_subtitle) are * never changed. SPPE_OK, SPPE_ERR_NOT_FOUND (stream/ordinal), SPPE_ERR_ARG * (text > 8192 bytes), SPPE_ERR_UNSUPPORTED (bitmap cue). * * sppe_sub_clear_overrides: every override on stream_index (< 0 = ALL streams). * Returns the number cleared (>= 0) or SPPE_ERR_NOT_FOUND. */ SPPE_API int sppe_sub_peek_window(sppe_t *e, int stream_index, int64_t from_ticks, int64_t to_ticks, char *out_json, int cap); SPPE_API int sppe_sub_set_cue_override(sppe_t *e, int stream_index, int cue_ordinal, const char *utf8_text); SPPE_API int sppe_sub_clear_overrides(sppe_t *e, int stream_index); /* ---------------------------------------------------------------- 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 */