ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
ffmpeg_decoder.c File Reference

FFmpeg-based media decoder implementation. More...

Go to the source code of this file.

Data Structures

struct  ffmpeg_decoder_t
 FFmpeg decoder state for video and audio decoding. More...
struct  stdin_buffer_t
struct  stdin_buffer_reader_t

Macros

#define MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE   256
#define TARGET_SAMPLE_RATE   48000
#define TARGET_CHANNELS   2
#define AVIO_BUFFER_SIZE   (64 * 1024)

Functions

ffmpeg_decoder_t * ffmpeg_decoder_create (const char *path)
 Create FFmpeg decoder from file path.
ffmpeg_decoder_t * ffmpeg_decoder_create_interruptible (const char *path, bool(*cancel)(void *), void *data)
ffmpeg_decoder_t * ffmpeg_decoder_create_with_request_options (const char *path, bool(*cancel)(void *), void *data, const char *headers, const char *cookies, const char *proxy)
ffmpeg_decoder_t * ffmpeg_decoder_create_stdin (void)
 Create FFmpeg decoder from stdin.
void ffmpeg_decoder_destroy (ffmpeg_decoder_t *decoder)
 Destroy FFmpeg decoder and free resources.
ffmpeg_frame_status_t ffmpeg_decoder_poll_video (ffmpeg_decoder_t *decoder, image_t **frame, asciichat_error_context_t *failure)
image_t * ffmpeg_decoder_read_video_frame (ffmpeg_decoder_t *decoder)
asciichat_error_t ffmpeg_decoder_start_prefetch (ffmpeg_decoder_t *decoder)
 Start the background frame prefetching thread.
void ffmpeg_decoder_stop_prefetch (ffmpeg_decoder_t *decoder)
 Stop the background frame prefetching thread.
void ffmpeg_decoder_set_exit_callback (ffmpeg_decoder_t *decoder, bool(*should_exit_callback)(void *), void *user_data)
 Set exit signal callback for graceful shutdown during I/O.
bool ffmpeg_decoder_is_prefetch_running (ffmpeg_decoder_t *decoder)
 Check if background frame prefetching thread is running.
bool ffmpeg_decoder_has_video (ffmpeg_decoder_t *decoder)
 Check if decoder has video stream.
asciichat_error_t ffmpeg_decoder_get_video_dimensions (ffmpeg_decoder_t *decoder, int *width, int *height)
 Get video dimensions.
double ffmpeg_decoder_get_video_fps (ffmpeg_decoder_t *decoder)
 Get video frame rate.
size_t ffmpeg_decoder_read_audio_samples (ffmpeg_decoder_t *decoder, float *buffer, size_t num_samples)
 Decode audio samples.
bool ffmpeg_decoder_has_audio (ffmpeg_decoder_t *decoder)
 Check if decoder has audio stream.
asciichat_error_t ffmpeg_decoder_rewind (ffmpeg_decoder_t *decoder)
 Seek to beginning of media.
asciichat_error_t ffmpeg_decoder_seek_to_timestamp (ffmpeg_decoder_t *decoder, double timestamp_sec)
 Seek to specific timestamp in media.
bool ffmpeg_decoder_at_end (ffmpeg_decoder_t *decoder)
 Check if decoder reached end of stream.
double ffmpeg_decoder_get_duration (ffmpeg_decoder_t *decoder)
 Get media duration in seconds.
double ffmpeg_decoder_get_position (ffmpeg_decoder_t *decoder)
 Get current playback position in seconds.

Detailed Description

FFmpeg-based media decoder implementation.

Definition in file lib/media/ffmpeg_decoder.c.

Macro Definition Documentation

◆ AVIO_BUFFER_SIZE

#define AVIO_BUFFER_SIZE   (64 * 1024)

AVIO buffer size for stdin reading (64KB)

Definition at line 69 of file lib/media/ffmpeg_decoder.c.

Referenced by ffmpeg_decoder_create_stdin().

◆ MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE

#define MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE   256

◆ TARGET_CHANNELS

#define TARGET_CHANNELS   2

Target audio channels (interleaved stereo)

Definition at line 66 of file lib/media/ffmpeg_decoder.c.

Referenced by ffmpeg_decoder_create_stdin(), ffmpeg_decoder_create_with_request_options(), and ffmpeg_decoder_read_audio_samples().

◆ TARGET_SAMPLE_RATE

#define TARGET_SAMPLE_RATE   48000

Target audio sample rate (48kHz for Opus compatibility)

Definition at line 63 of file lib/media/ffmpeg_decoder.c.

Referenced by ffmpeg_decoder_create_stdin(), and ffmpeg_decoder_create_with_request_options().

Function Documentation

◆ ffmpeg_decoder_create_interruptible()

ffmpeg_decoder_t * ffmpeg_decoder_create_interruptible ( const char * path,
bool(* cancel )(void *),
void * data )

Definition at line 753 of file lib/media/ffmpeg_decoder.c.

753 {
754 return ffmpeg_decoder_create_with_request_options(path, cancel, data, NULL, NULL, NULL);
755}
ffmpeg_decoder_t * ffmpeg_decoder_create_with_request_options(const char *path, bool(*cancel)(void *), void *data, const char *headers, const char *cookies, const char *proxy)

References ffmpeg_decoder_create_with_request_options().

Referenced by ffmpeg_decoder_create(), and http_renderer_render_file().

◆ ffmpeg_decoder_create_with_request_options()

ffmpeg_decoder_t * ffmpeg_decoder_create_with_request_options ( const char * path,
bool(* cancel )(void *),
void * data,
const char * headers,
const char * cookies,
const char * proxy )

Definition at line 757 of file lib/media/ffmpeg_decoder.c.

757 {
758 ASCII_OTEL_SCOPE(FFMPEG_DECODER_CREATE_INTERRUPTIBLE);
759 if (!path) {
760 SET_ERRNO(ERROR_INVALID_PARAM, "Path is NULL");
761 return NULL;
762 }
763
764 // Suppress FFmpeg's verbose debug logging (H.264 codec warnings, etc.)
765 // Only set this once, it's a global setting
766 static bool ffmpeg_log_level_set = false;
767 if (!ffmpeg_log_level_set) {
768 av_log_set_level(AV_LOG_QUIET); // Suppress all FFmpeg logging
769 av_log_set_callback(ffmpeg_silent_log_callback); // Install silent callback to discard all output
770 ffmpeg_log_level_set = true;
771 }
772
774 if (!decoder) {
775 SET_ERRNO(ERROR_MEMORY, "Failed to allocate decoder");
776 return NULL;
777 }
778
779 memset(decoder, 0, sizeof(*decoder));
780 atomic_ptr_store(&decoder->should_exit_callback, (void *)cancel);
782 decoder->format_ctx = avformat_alloc_context();
783 if (!decoder->format_ctx) {
784 SAFE_FREE(decoder);
785 return NULL;
786 }
787 decoder->format_ctx->interrupt_callback.callback = ffmpeg_interrupt_callback;
788 decoder->format_ctx->interrupt_callback.opaque = decoder;
789 decoder->video_stream_idx = -1;
790 decoder->audio_stream_idx = -1;
791 decoder->last_video_pts = -1.0;
792 decoder->last_audio_pts = -1.0;
793
794 // Capture FFmpeg's probing output (FFmpeg may write directly to either stream)
795 int ret = 0;
796 LOG_IO("ffmpeg", {
797 // Configure FFmpeg options for HTTP streaming performance
798 AVDictionary *options = NULL;
799
800 // FFmpeg accepts HTTP authorities that the application URL validator rejects.
801 // Classify the protocol rather than validating the host for timeout settings.
802 bool is_http = path && (platform_strncasecmp(path, "http://", sizeof("http://") - 1) == 0 || platform_strncasecmp(path, "https://", sizeof("https://") - 1) == 0);
804 bool is_url = is_http || (path && strstr(path, "://") && url_is_valid(path));
806 if (is_url) {
807 if (headers && *headers)
808 av_dict_set(&options, "headers", headers, 0);
809 if (cookies && *cookies)
810 av_dict_set(&options, "cookies", cookies, 0);
811 if (proxy)
812 av_dict_set(&options, "http_proxy", proxy, 0);
813 // Limit probing to 32KB for faster format detection
814 av_dict_set(&options, "probesize", "32768", 0);
815 // Analyze for 100ms max to determine streams quickly
816 av_dict_set(&options, "analyzeduration", "100000", 0);
817 // Enable auto-reconnection for interrupted connections
818 av_dict_set(&options, "reconnect", "1", 0);
819 // Allow reconnection for streamed protocols
820 av_dict_set(&options, "reconnect_streamed", "1", 0);
821 // Set reasonable I/O timeout (10 seconds)
822 av_dict_set(&options, "rw_timeout", "10000000", 0);
823 // Enable HTTP persistent connection (keep-alive) for better performance
824 av_dict_set(&options, "http_persistent", "1", 0);
825 // Reduce connect timeout to fail faster if server is unreachable
826 av_dict_set(&options, "connect_timeout", "5000000", 0);
827 }
828
829 // Open input file
830 ret = traced_format_open(&decoder->format_ctx, path, NULL, &options);
831 av_dict_free(&options); // Free options dictionary
832
833 if (ret >= 0) {
834 // Find stream info
835 if (traced_format_probe(decoder->format_ctx, NULL) < 0) {
836 ret = -1;
837 }
838 }
839 });
840
841 if (ret < 0) {
842 if (ret == -1) {
843 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to find stream info");
844 } else {
845 SET_ERRNO(ERROR_MEDIA_OPEN, "Failed to open media file: %s", path);
846 }
847 if (decoder->format_ctx) {
848 avformat_close_input(&decoder->format_ctx);
849 }
850 SAFE_FREE(decoder);
851 return NULL;
852 }
853
854 // Install interrupt callback to allow seeking to interrupt long traced_packet_read() calls
855 // Do this AFTER finding stream info to ensure format context is fully initialized
856 if (decoder->format_ctx) {
857 decoder->format_ctx->interrupt_callback.callback = ffmpeg_interrupt_callback;
858 decoder->format_ctx->interrupt_callback.opaque = decoder;
859 }
860
861 // Open video codec
862 asciichat_error_t err = open_optional_codec_context(decoder->format_ctx, AVMEDIA_TYPE_VIDEO, &decoder->video_stream_idx, &decoder->video_codec_ctx);
863 if (err != ASCIICHAT_OK) {
864 ffmpeg_decoder_destroy(decoder);
865 return NULL;
866 }
867
868 // Open audio codec - audio is enabled by default (no option needed)
869 // Always try to open audio codec, don't rely on GET_OPTION(audio_enabled) which has a default issue
870 err = open_optional_codec_context(decoder->format_ctx, AVMEDIA_TYPE_AUDIO, &decoder->audio_stream_idx, &decoder->audio_codec_ctx);
871 if (err != ASCIICHAT_OK) {
872 ffmpeg_decoder_destroy(decoder);
873 return NULL;
874 }
875
876 // Require at least one stream
877 if (decoder->video_stream_idx < 0 && decoder->audio_stream_idx < 0) {
878 SET_ERRNO(ERROR_MEDIA_DECODE, "No video or audio streams found");
879 ffmpeg_decoder_destroy(decoder);
880 return NULL;
881 }
882
883 // Allocate frame and packet
884 decoder->frame = av_frame_alloc();
885 decoder->packet = av_packet_alloc();
886 if (!decoder->frame || !decoder->packet) {
887 SET_ERRNO(ERROR_MEMORY, "Failed to allocate frame/packet");
888 ffmpeg_decoder_destroy(decoder);
889 return NULL;
890 }
891
892 // Initialize swscale context for video if present
893 if (decoder->video_codec_ctx) {
894 // Validate codec context has valid dimensions and pixel format
895 // For HTTP streams, these might not be valid until first frame is read
896 if (decoder->video_codec_ctx->width <= 0 || decoder->video_codec_ctx->height <= 0) {
897 log_warn("Video codec has invalid dimensions (%dx%d), will initialize swscale on first frame", decoder->video_codec_ctx->width, decoder->video_codec_ctx->height);
898 // Don't create swscale context yet - will create it lazily on first frame read
899 } else if (decoder->video_codec_ctx->pix_fmt == AV_PIX_FMT_NONE) {
900 log_warn("Video codec has invalid pixel format, will initialize swscale on first frame");
901 // Don't create swscale context yet - will create it lazily on first frame read
902 } else {
903 // Create swscale context with valid parameters
904 LOG_IO("swscaler", {
905 decoder->sws_ctx = sws_getContext(decoder->video_codec_ctx->width, decoder->video_codec_ctx->height, decoder->video_codec_ctx->pix_fmt, decoder->video_codec_ctx->width,
906 decoder->video_codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
907 });
908 if (!decoder->sws_ctx) {
909 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to create swscale context");
910 ffmpeg_decoder_destroy(decoder);
911 return NULL;
912 }
913 }
914 }
915
916 // Initialize swresample context for audio if present
917 if (decoder->audio_codec_ctx) {
918 // Store output sample rate for position tracking
920
921 // Allocate resampler context
922 decoder->swr_ctx = swr_alloc();
923 if (!decoder->swr_ctx) {
924 SET_ERRNO(ERROR_MEMORY, "Failed to allocate swresample context");
925 ffmpeg_decoder_destroy(decoder);
926 return NULL;
927 }
928
929 // Set options
930 av_opt_set_chlayout(decoder->swr_ctx, "in_chlayout", &decoder->audio_codec_ctx->ch_layout, 0);
931 av_opt_set_int(decoder->swr_ctx, "in_sample_rate", decoder->audio_codec_ctx->sample_rate, 0);
932 av_opt_set_sample_fmt(decoder->swr_ctx, "in_sample_fmt", decoder->audio_codec_ctx->sample_fmt, 0);
933
934 AVChannelLayout out_ch_layout = AV_CHANNEL_LAYOUT_STEREO;
935 av_opt_set_chlayout(decoder->swr_ctx, "out_chlayout", &out_ch_layout, 0);
936 av_opt_set_int(decoder->swr_ctx, "out_sample_rate", TARGET_SAMPLE_RATE, 0);
937 av_opt_set_sample_fmt(decoder->swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
938
939 // A mono source stays centered at unity gain in the stereo pipeline.
940 if (decoder->audio_codec_ctx->ch_layout.nb_channels == 1) {
941 const double matrix[] = {1.0, 1.0};
942 swr_set_matrix(decoder->swr_ctx, matrix, 1);
943 }
944 // Initialize
945 if (swr_init(decoder->swr_ctx) < 0) {
946 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to initialize swresample context");
947 ffmpeg_decoder_destroy(decoder);
948 return NULL;
949 }
950
951 // Allocate audio buffer (10 seconds worth)
953 decoder->audio_buffer = SAFE_MALLOC(decoder->audio_buffer_size * sizeof(float), float *);
954 if (!decoder->audio_buffer) {
955 SET_ERRNO(ERROR_MEMORY, "Failed to allocate audio buffer");
956 ffmpeg_decoder_destroy(decoder);
957 return NULL;
958 }
959 }
960
961 // Initialize video frame prefetching system (for YouTube streaming)
962 if (decoder->video_stream_idx >= 0) {
963 int width = decoder->video_codec_ctx->width;
964 int height = decoder->video_codec_ctx->height;
965
966 // Create two prefetch image buffers for double-buffering
967 decoder->prefetch_image_a = image_new((size_t)width, (size_t)height);
968 decoder->prefetch_image_b = image_new((size_t)width, (size_t)height);
969 if (!decoder->prefetch_image_a || !decoder->prefetch_image_b) {
970 SET_ERRNO(ERROR_MEMORY, "Failed to allocate prefetch image buffers");
971 ffmpeg_decoder_destroy(decoder);
972 return NULL;
973 }
974
975 decoder->current_prefetch_image = decoder->prefetch_image_a;
976 decoder->prefetch_frame_ready = false;
977
978 // Initialize prefetch mutex
979 if (mutex_init(&decoder->prefetch_mutex, "ffmpeg_prefetch") != 0) {
980 SET_ERRNO(ERROR_MEMORY, "Failed to initialize prefetch mutex");
981 ffmpeg_decoder_destroy(decoder);
982 return NULL;
983 }
984
985 if (cond_init(&decoder->prefetch_cond, "ffmpeg_prefetch") != 0) {
986 SET_ERRNO(ERROR_MEMORY, "Failed to initialize prefetch condition variable");
987 mutex_destroy(&decoder->prefetch_mutex);
988 ffmpeg_decoder_destroy(decoder);
989 return NULL;
990 }
991
992 if (mutex_init(&decoder->read_frame_mutex, "ffmpeg_read_frame") != 0) {
993 SET_ERRNO(ERROR_MEMORY, "Failed to initialize read_frame mutex");
994 mutex_destroy(&decoder->prefetch_mutex);
995 cond_destroy(&decoder->prefetch_cond);
996 ffmpeg_decoder_destroy(decoder);
997 return NULL;
998 }
999
1000 decoder->prefetch_thread_running = false;
1001 decoder->prefetch_should_stop = false;
1002 }
1003
1004 log_debug("FFmpeg decoder opened: %s (video=%s, audio=%s)", path, decoder->video_stream_idx >= 0 ? "yes" : "no", decoder->audio_stream_idx >= 0 ? "yes" : "no");
1005
1007 snprintf(decoder_name, sizeof(decoder_name), "%s", path);
1008 NAMED_REGISTER_FFMPEG_DECODER(decoder, decoder_name, NULL);
1009 NAMED_REGISTER_ATOMIC(&decoder->eof_reached, "ffmpeg_eof", decoder);
1010
1011 return decoder;
1012}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_DISMISSED
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_REGISTER_FFMPEG_DECODER(decoder, name, parent_ptr)
Register an FFmpeg decoder with automatic format specifier.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_MEDIA_DECODE
Definition error_codes.h:71
@ ERROR_INVALID_PARAM
@ ERROR_MEDIA_OPEN
Definition error_codes.h:70
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
void ffmpeg_decoder_destroy(ffmpeg_decoder_t *decoder)
Destroy FFmpeg decoder and free resources.
bool url_is_valid(const char *url)
URL validation using shared parsing and ICU hostname checks.
Definition url.c:156
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.
#define LOG_IO(prefix, block)
Definition io.h:104
#define MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE
#define TARGET_CHANNELS
#define TARGET_SAMPLE_RATE
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
FFmpeg decoder state for video and audio decoding.
mutex_t prefetch_mutex
Protect prefetch state and FFmpeg decoder access.
AVCodecContext * video_codec_ctx
Video codec context.
image_t * prefetch_image_a
First prefetch buffer.
AVFormatContext * format_ctx
FFmpeg format/container context.
atomic_t eof_reached
Whether end of file was reached.
atomic_ptr_t exit_callback_user_data
User data for exit callback.
bool prefetch_thread_running
Whether prefetch thread is active.
image_t * prefetch_image_b
Second prefetch buffer.
atomic_ptr_t should_exit_callback
Callback to check if app should exit.
cond_t prefetch_cond
Condition variable for pausing during seek.
mutex_t read_frame_mutex
Protect traced_packet_read() calls from video and audio threads.
struct SwrContext * swr_ctx
Software resampler for format conversion.
AVPacket * packet
Reusable packet for reading.
int video_stream_idx
Video stream index (-1 if none).
_Atomic bool prefetch_should_stop
Signal to stop prefetch thread.
double last_audio_pts
Last audio presentation timestamp.
AVCodecContext * audio_codec_ctx
Audio codec context.
int audio_sample_rate
Audio sample rate (Hz).
size_t audio_buffer_size
Total size of audio buffer.
image_t * current_prefetch_image
Currently available prefetched frame.
float * audio_buffer
Buffer for partial audio frames.
double last_video_pts
Last video presentation timestamp.
_Atomic bool prefetch_frame_ready
Whether current_prefetch_image has valid data.
int audio_stream_idx
Audio stream index (-1 if none).
AVFrame * frame
Reusable frame for decoding.
struct SwsContext * sws_ctx
Software scaler for format conversion.

References ASCII_OTEL_SCOPE, ASCIICHAT_ERRNO_DISMISSED, ASCIICHAT_OK, atomic_ptr_store(), ffmpeg_decoder_t::audio_buffer, ffmpeg_decoder_t::audio_buffer_size, ffmpeg_decoder_t::audio_codec_ctx, ffmpeg_decoder_t::audio_sample_rate, ffmpeg_decoder_t::audio_stream_idx, ffmpeg_decoder_t::current_prefetch_image, ffmpeg_decoder_t::eof_reached, errno_scope_begin(), errno_scope_end(), ERROR_INVALID_PARAM, ERROR_MEDIA_DECODE, ERROR_MEDIA_OPEN, ERROR_MEMORY, ffmpeg_decoder_t::exit_callback_user_data, ffmpeg_decoder_destroy(), ffmpeg_decoder_t::format_ctx, ffmpeg_decoder_t::frame, height, image_new(), ffmpeg_decoder_t::last_audio_pts, ffmpeg_decoder_t::last_video_pts, log_debug, LOG_IO, log_warn, MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE, NAMED_REGISTER_ATOMIC, NAMED_REGISTER_FFMPEG_DECODER, ffmpeg_decoder_t::packet, platform_strncasecmp(), ffmpeg_decoder_t::prefetch_cond, ffmpeg_decoder_t::prefetch_frame_ready, ffmpeg_decoder_t::prefetch_image_a, ffmpeg_decoder_t::prefetch_image_b, ffmpeg_decoder_t::prefetch_mutex, ffmpeg_decoder_t::prefetch_should_stop, ffmpeg_decoder_t::prefetch_thread_running, ffmpeg_decoder_t::read_frame_mutex, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, ffmpeg_decoder_t::should_exit_callback, ffmpeg_decoder_t::swr_ctx, ffmpeg_decoder_t::sws_ctx, TARGET_CHANNELS, TARGET_SAMPLE_RATE, url_is_valid(), ffmpeg_decoder_t::video_codec_ctx, ffmpeg_decoder_t::video_stream_idx, and width.

Referenced by ffmpeg_decoder_create_interruptible(), and media_source_create_interruptible().