ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/media/ffmpeg_decoder.c
Go to the documentation of this file.
1
5
6/* Storage capacities in elements; text buffers include the terminating NUL. */
7#define MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE 256
8
9#include <ascii-chat/otel.h>
11#include <ascii-chat/common.h>
12#include <libplatform/atomic.h>
13#include <ascii-chat/log/log.h>
14#include <ascii-chat/log/io.h>
18#include <libplatform/system.h>
19#include <util/string.h>
20#include <libplatform/mutex.h>
21#include <libplatform/cond.h>
22#include <libplatform/thread.h>
24#include <ascii-chat/util/url.h>
25#include <options/options.h>
26
27#include <libavformat/avformat.h>
28#include <libavcodec/avcodec.h>
29#include <libavutil/imgutils.h>
30#include <libavutil/opt.h>
31#include <libavutil/log.h>
32#include <libswscale/swscale.h>
33#include <libswresample/swresample.h>
34#include <string.h>
35#include <inttypes.h>
36
37/* ============================================================================
38 * FFmpeg Logging Suppression
39 * ============================================================================ */
40
50static void ffmpeg_silent_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
51 (void)avcl; // unused
52 (void)level; // unused
53 (void)fmt; // unused
54 (void)vl; // unused
55 // Do nothing - silently discard all FFmpeg logs
56}
57
58/* ============================================================================
59 * Constants
60 * ============================================================================ */
61
63#define TARGET_SAMPLE_RATE 48000
64
66#define TARGET_CHANNELS 2
67
69#define AVIO_BUFFER_SIZE (64 * 1024)
70
71/* ============================================================================
72 * FFmpeg Decoder Structure
73 * ============================================================================ */
74
82 // Format context
83 AVFormatContext *format_ctx;
84
85 // Video stream
86 AVCodecContext *video_codec_ctx;
88 struct SwsContext *sws_ctx;
89
90 // Audio stream
91 AVCodecContext *audio_codec_ctx;
93 struct SwrContext *swr_ctx;
94
95 // Frame and packet buffers
96 AVFrame *frame;
97 AVPacket *packet;
98
99 // Decoded image cache - double-buffered for prefetching
101
102 // Audio sample buffer (for partial frame handling)
106
107 // Background thread for frame prefetching (reduces YouTube HTTP blocking)
108 platform_thread_t prefetch_thread;
112 _Atomic bool prefetch_frame_ready;
114 _Atomic bool prefetch_failed;
116 _Atomic bool prefetch_should_stop;
117 _Atomic bool seeking_in_progress;
122
123 // Track which buffer is being read by main thread
127
128 // State flags
130 bool is_stdin;
132
133 // Stdin I/O context
134 AVIOContext *avio_ctx;
135 unsigned char *avio_buffer;
136
137 // Position tracking
140
141 // Sample-based position tracking
144
145 // Exit signal callback (for graceful shutdown during blocking I/O)
148};
149
150/* ============================================================================
151 * Stdin I/O Callbacks
152 * ============================================================================ */
153
154/* ============================================================================
155 * Memory-based AVIO for stdin buffering
156 * ============================================================================ */
157
161typedef struct {
163 size_t size;
165
173
177static int memory_read_packet(void *opaque, uint8_t *buf, int buf_size) {
179
180 if (!reader || !reader->buffer || reader->pos >= reader->buffer->size)
181 return AVERROR_EOF;
182
183 int to_read = buf_size;
184 if (reader->pos + to_read > reader->buffer->size)
185 to_read = (int)(reader->buffer->size - reader->pos);
186
187 memcpy(buf, reader->buffer->data + reader->pos, to_read);
188 reader->pos += to_read;
189
190 return to_read;
191}
192
196static int64_t memory_seek_packet(void *opaque, int64_t offset, int whence) {
198
199 if (!reader || !reader->buffer)
200 return -1;
201
202 int64_t new_pos = 0;
203 if (whence == SEEK_SET) {
204 new_pos = offset;
205 } else if (whence == SEEK_CUR) {
206 new_pos = (int64_t)reader->pos + offset;
207 } else if (whence == SEEK_END) {
208 new_pos = (int64_t)reader->buffer->size + offset;
209 } else {
210 return -1;
211 }
212
213 if (new_pos < 0 || new_pos > (int64_t)reader->buffer->size)
214 return -1;
215
216 reader->pos = (size_t)new_pos;
217 return new_pos;
218}
219
224static stdin_buffer_t *stdin_buffer_read_all(void) {
226 if (!sb)
227 return NULL;
228
229 sb->data = NULL;
230 sb->size = 0;
231
232 // Read stdin in 64KB chunks
233 const size_t chunk_size = 65536;
234 uint8_t chunk_buffer[chunk_size];
235 size_t total_capacity = 0;
236 int chunk_count = 0;
237
238 while (1) {
239 size_t bytes_read = fread(chunk_buffer, 1, chunk_size, stdin);
240 if (bytes_read == 0)
241 break;
242 chunk_count++;
243
244 // Grow buffer if needed
245 if (sb->size + bytes_read > total_capacity) {
246 total_capacity = (sb->size + bytes_read) * 2; // Always double capacity
247 uint8_t *new_data = SAFE_MALLOC(total_capacity, uint8_t *);
248 if (!new_data) {
249 SAFE_FREE(sb->data);
250 SAFE_FREE(sb);
251 return NULL;
252 }
253 if (sb->data) {
254 memcpy(new_data, sb->data, sb->size);
255 SAFE_FREE(sb->data);
256 }
257 sb->data = new_data;
258 }
259
260 // Copy chunk to buffer
261 memcpy(sb->data + sb->size, chunk_buffer, bytes_read);
262 sb->size += bytes_read;
263 }
264
265 log_debug("Buffered stdin: %zu bytes in %d chunks (capacity: %zu)", sb->size, chunk_count, total_capacity);
266 if (sb->size > 0) {
267 log_info(" First 16 bytes of stdin: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", sb->data[0], sb->data[1], sb->data[2], sb->data[3], sb->data[4], sb->data[5],
268 sb->data[6], sb->data[7], sb->data[8], sb->data[9], sb->data[10], sb->data[11], sb->data[12], sb->data[13], sb->data[14], sb->data[15]);
269 }
270 return sb;
271}
272
276static stdin_buffer_t *g_stdin_buffer = NULL;
277
278/* ============================================================================
279 * Helper Functions
280 * ============================================================================ */
281
285static inline double av_q2d_safe(AVRational r) {
286 return (r.den != 0) ? ((double)r.num / (double)r.den) : 0.0;
287}
288
292static double get_frame_pts_seconds(AVFrame *frame, AVRational time_base) {
293 if (frame->pts == AV_NOPTS_VALUE) {
294 return -1.0;
295 }
296 return (double)frame->pts * av_q2d_safe(time_base);
297}
298
305static int ffmpeg_interrupt_callback(void *opaque) {
306 ffmpeg_decoder_t *decoder = (ffmpeg_decoder_t *)opaque;
307 if (!decoder) {
308 return 0;
309 }
310
311 // Interrupt traced_packet_read() if a seek is in progress
312 if (decoder->seeking_in_progress) {
313 return 1;
314 }
315
316 // Interrupt traced_packet_read() if application is shutting down
317 // This allows Ctrl+C to abort blocking I/O operations like YouTube HTTP requests
318 bool (*cancel)(void *) = (bool (*)(void *))atomic_ptr_load(&decoder->should_exit_callback);
319 void *data = atomic_ptr_load(&decoder->exit_callback_user_data);
320 if (cancel && cancel(data)) {
321 return 1;
322 }
323
324 return 0;
325}
326
334
335static void ffmpeg_otel_result(int result, bool streaming) {
336 // EOF, buffering and application cancellation are expected control flow.
337 if (result < 0 && result != AVERROR_EXIT && !(streaming && (result == AVERROR_EOF || result == AVERROR(EAGAIN))))
338 gotel_current_error("ffmpeg.error", "FFmpeg operation failed");
339}
340
341static int traced_format_open(AVFormatContext **format, const char *path, const AVInputFormat *input, AVDictionary **options) {
342 ASCII_OTEL_SCOPE(FFMPEG_FORMAT_OPEN);
343 ASCII_OTEL_ATTR_STRING("media.source.kind", !path ? "stdin" : strstr(path, "://") ? "network" : "file");
344 int result = avformat_open_input(format, path, input, options);
345 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
346 ffmpeg_otel_result(result, false);
347 return result;
348}
349static int traced_format_probe(AVFormatContext *format, AVDictionary **options) {
350 ASCII_OTEL_SCOPE(FFMPEG_PROBE);
351 int result = avformat_find_stream_info(format, options);
352 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
353 if (result >= 0 && format->iformat)
354 ASCII_OTEL_ATTR_STRING("media.container", format->iformat->name);
355 ffmpeg_otel_result(result, false);
356 return result;
357}
358static int traced_packet_read(AVFormatContext *format, AVPacket *packet) {
359 ASCII_OTEL_SCOPE(FFMPEG_PACKET_READ);
360 int result = av_read_frame(format, packet);
361 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
362 if (result >= 0)
363 ASCII_OTEL_ATTR_INT("media.packet.size", packet->size);
364 ffmpeg_otel_result(result, true);
365 return result;
366}
367static int traced_packet_send(AVCodecContext *codec, const AVPacket *packet) {
368 ASCII_OTEL_SCOPE(FFMPEG_PACKET_SEND);
369 ASCII_OTEL_ATTR_STRING("media.type", codec->codec_type == AVMEDIA_TYPE_AUDIO ? "audio" : "video");
370 ASCII_OTEL_ATTR_STRING("media.codec", avcodec_get_name(codec->codec_id));
371 ASCII_OTEL_ATTR_INT("media.decoder.draining", packet == NULL);
372 int result = avcodec_send_packet(codec, packet);
373 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
374 ffmpeg_otel_result(result, true);
375 return result;
376}
377static int traced_frame_receive(AVCodecContext *codec, AVFrame *frame) {
378 ASCII_OTEL_SCOPE(FFMPEG_FRAME_RECEIVE);
379 ASCII_OTEL_ATTR_STRING("media.type", codec->codec_type == AVMEDIA_TYPE_AUDIO ? "audio" : "video");
380 int result = avcodec_receive_frame(codec, frame);
381 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
382 ffmpeg_otel_result(result, true);
383 return result;
384}
385static int traced_video_scale(struct SwsContext *context, const uint8_t *const data[], const int stride[], int start, int height, uint8_t *const output[], const int output_stride[]) {
386 ASCII_OTEL_SCOPE(FFMPEG_SCALE);
387 ASCII_OTEL_ATTR_INT("video.frame.height", height);
388 int result = sws_scale(context, data, stride, start, height, output, output_stride);
389 ASCII_OTEL_ATTR_INT("ffmpeg.result", result);
390 ffmpeg_otel_result(result, false);
391 return result;
392}
393static int traced_audio_resample(struct SwrContext *context, uint8_t **output, int capacity, const uint8_t **input, int samples) {
394 ASCII_OTEL_SCOPE(FFMPEG_RESAMPLE);
395 ASCII_OTEL_ATTR_INT("audio.input.samples", samples);
396 int result = swr_convert(context, output, capacity, input, samples);
397 ASCII_OTEL_ATTR_INT("audio.output.samples", result > 0 ? result : 0);
398 ffmpeg_otel_result(result, false);
399 return result;
400}
401
402static void *ffmpeg_decoder_prefetch_thread_func(void *arg) {
403 ffmpeg_decoder_t *decoder = (ffmpeg_decoder_t *)arg;
404 if (!decoder || !decoder->prefetch_image_a || !decoder->prefetch_image_b) {
405 return NULL;
406 }
407
408 log_debug("Video prefetch thread started");
409 bool use_image_a = true; // Track which buffer we're using (critical for correct buffer swapping)
410
411 mutex_lock(&decoder->prefetch_mutex);
412 while (true) {
413
414 // The consumer releases the previous image and wakes us when taking the
415 // next frame. Seek completion and shutdown also signal this condition.
416 while (!decoder->prefetch_should_stop &&
417 (atomic_load_bool(&decoder->eof_reached) || decoder->seeking_in_progress || decoder->prefetch_frame_ready || (decoder->buffer_a_in_use && decoder->buffer_b_in_use)))
418 cond_wait(&decoder->prefetch_cond, &decoder->prefetch_mutex);
419 if (decoder->prefetch_should_stop) {
420 break;
421 }
422
423 use_image_a = !decoder->buffer_a_in_use;
424 uint64_t generation = decoder->seek_generation;
425 uint64_t read_start_ns = time_get_ns();
426 bool frame_decoded = false;
427 bool reached_eof = false;
428 bool packet_rejected = false;
430
431 // Release prefetch mutex but hold read_frame_mutex to prevent concurrent traced_packet_read() calls from audio
432 // thread
433 mutex_unlock(&decoder->prefetch_mutex);
434 mutex_lock(&decoder->read_frame_mutex);
435 if (generation != decoder->seek_generation || decoder->seeking_in_progress) {
436 mutex_unlock(&decoder->read_frame_mutex);
437 mutex_lock(&decoder->prefetch_mutex);
438 continue;
439 }
440 // Interrupted reads are expected. Publish a failure only after deciding
441 // whether this attempt was cancelled or superseded by a seek.
442 bool notifications_suppressed = errno_notifications_suppress(true);
443
444 ASCII_OTEL_SCOPE(FFMPEG_VIDEO_DECODE);
445 ASCII_OTEL_ATTR_STRING("media.codec", avcodec_get_name(decoder->video_codec_ctx->codec_id));
446 int decode_result = 0;
447 // Read packets until we get a video frame - read_frame_mutex HELD
448 while (true) {
449 int ret;
450 if (decoder->video_draining) {
451 ret = decode_result = traced_frame_receive(decoder->video_codec_ctx, decoder->frame);
452 if (ret == AVERROR_EOF) {
453 atomic_store_bool(&decoder->eof_reached, true);
454 reached_eof = true;
455 break;
456 }
457 if (ret < 0) {
458 break;
459 }
460 } else {
461 ret = decode_result = traced_packet_read(decoder->format_ctx, decoder->packet);
462 if (ret < 0) {
463 if (ret != AVERROR_EOF) {
464 break;
465 }
466
467 ret = decode_result = traced_packet_send(decoder->video_codec_ctx, NULL);
468 decoder->video_draining = true;
469 if (ret < 0 && ret != AVERROR_EOF) {
470 break;
471 }
472
473 ret = decode_result = traced_frame_receive(decoder->video_codec_ctx, decoder->frame);
474 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
475 atomic_store_bool(&decoder->eof_reached, true);
476 reached_eof = true;
477 break;
478 }
479 if (ret < 0) {
480 break;
481 }
482 } else {
483 // Check if this is a video packet
484 if (decoder->packet->stream_index != decoder->video_stream_idx) {
485 av_packet_unref(decoder->packet);
486 continue;
487 }
488
489 // Send packet to decoder
490 ret = decode_result = traced_packet_send(decoder->video_codec_ctx, decoder->packet);
491 av_packet_unref(decoder->packet);
492
493 if (ret == AVERROR(EAGAIN))
494 continue;
495 if (ret < 0) {
496 packet_rejected = true;
497 break;
498 }
499
500 // Receive decoded frame
501 ret = decode_result = traced_frame_receive(decoder->video_codec_ctx, decoder->frame);
502 if (ret == AVERROR(EAGAIN)) {
503 continue; // Need more packets
504 } else if (ret < 0) {
505 break;
506 }
507 }
508 }
509
510 // Frame decoded - mutex still held
511 // Update position tracking
512 decoder->last_video_pts = get_frame_pts_seconds(decoder->frame, decoder->format_ctx->streams[decoder->video_stream_idx]->time_base);
513
514 // Convert frame to RGB24
515 int width = decoder->video_codec_ctx->width;
516 ASCII_OTEL_ATTR_INT("video.frame.width", width);
517 ASCII_OTEL_ATTR_INT("video.frame.height", decoder->video_codec_ctx->height);
518 int height = decoder->video_codec_ctx->height;
519
520 // Get current decode buffer based on which one we're using
521 // (Buffer availability was already checked before entering the decode loop)
522 image_t *decode_buffer = use_image_a ? decoder->prefetch_image_a : decoder->prefetch_image_b;
523
524 // Reallocate if needed (width/height changed)
525 if (decode_buffer->w != width || decode_buffer->h != height) {
526 image_t *replacement = image_new((size_t)width, (size_t)height);
527 if (!replacement) {
528 SET_ERRNO(ERROR_MEMORY, "Failed to allocate prefetch image buffer");
529 break;
530 }
531 image_destroy(decode_buffer);
532 decode_buffer = replacement;
533 // Update decoder's pointer to the reallocated buffer
534 if (use_image_a) {
535 decoder->prefetch_image_a = decode_buffer;
536 } else {
537 decoder->prefetch_image_b = decode_buffer;
538 }
539 }
540
541 // Convert pixel format (rgb_pixel_t is 3 bytes: r, g, b)
542 uint8_t *dst_data[1] = {(uint8_t *)decode_buffer->pixels};
543 int dst_linesize[1] = {width * 3};
544
545 static int decode_count = 0;
546 if (++decode_count <= 3) {
547 log_debug("[FRAME_DECODE] Frame %d: width=%d, height=%d, codec_pix_fmt=%d, frame_pix_fmt=%d (is_stdin=%d)", decode_count, width, height, decoder->video_codec_ctx->pix_fmt,
548 decoder->frame->format, decoder->is_stdin);
549 }
550
551 // For fragmented MP4 from stdin, pixel format might not be set - default to YUV420p
552 // which is standard for H.264/H.265 video codecs
553 int pix_fmt_to_use = decoder->video_codec_ctx->pix_fmt;
554 if (pix_fmt_to_use == AV_PIX_FMT_NONE) {
555 if (decoder->frame->format != AV_PIX_FMT_NONE) {
556 pix_fmt_to_use = decoder->frame->format;
557 log_debug("[SWSCALE_FIX] Using frame->format=%d instead of codec pix_fmt (was NONE)", decoder->frame->format);
558 } else {
559 // Fragmented MP4: Default to YUV420p (standard for H.264/H.265)
560 pix_fmt_to_use = AV_PIX_FMT_YUV420P;
561 log_debug("[SWSCALE_FIX] Fragmented format: Using hardcoded YUV420p (frame->format was also NONE)");
562 }
563 }
564
565 // Lazy initialize swscale context if not done at startup (happens with HTTP/stdin streams)
566 if (!decoder->sws_ctx) {
567 log_debug("Lazy init swscale: width=%d, height=%d, pix_fmt=%d", width, height, pix_fmt_to_use);
568 if (width > 0 && height > 0 && pix_fmt_to_use != AV_PIX_FMT_NONE) {
569 LOG_IO("swscaler", { decoder->sws_ctx = sws_getContext(width, height, pix_fmt_to_use, width, height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); });
570 if (!decoder->sws_ctx) {
571 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to create swscale context: pix_fmt=%d (might be unsupported for conversion)", pix_fmt_to_use);
572 break;
573 }
574
575 // Set proper colorspace for H.264 video (BT.709)
576 // sws_setColorspaceDetails(ctx, inv_table, srcRange, table, dstRange, brightness, contrast, saturation)
577 const int *inv_table = sws_getCoefficients(SWS_CS_ITU709);
578 const int *table = sws_getCoefficients(SWS_CS_ITU709);
579 int ret_cs = sws_setColorspaceDetails(decoder->sws_ctx, inv_table, 0, table, 0, 0, 1 << 16, 1 << 16);
580 if (ret_cs < 0) {
581 log_warn("Failed to set BT.709 colorspace details (code=%d), continuing with defaults", ret_cs);
582 } else {
583 log_debug("Set BT.709 colorspace for H.264 YUV->RGB conversion");
584 }
585
586 log_debug("Lazy initialized swscale context with %dx%d from pix_fmt=%d", width, height, pix_fmt_to_use);
587 } else {
588 SET_ERRNO(ERROR_MEDIA_DECODE, "Cannot initialize swscale: width=%d, height=%d, pix_fmt=%d (AV_PIX_FMT_NONE=%d)", width, height, pix_fmt_to_use, AV_PIX_FMT_NONE);
589 break;
590 }
591 }
592
593 if (decode_count <= 3) {
594 // Log input YUV data
595 uint8_t *y_data = decoder->frame->data[0];
596 uint8_t *u_data = decoder->frame->data[1];
597 uint8_t *v_data = decoder->frame->data[2];
598 log_debug("[YUV_INPUT] Frame %d: Y[0]=%u, U[0]=%u, V[0]=%u (linesize=%d,%d,%d)", decode_count, y_data ? y_data[0] : 255, u_data ? u_data[0] : 255, v_data ? v_data[0] : 255,
599 decoder->frame->linesize[0], decoder->frame->linesize[1], decoder->frame->linesize[2]);
600 }
601
602 int ret_scale = traced_video_scale(decoder->sws_ctx, (const uint8_t *const *)decoder->frame->data, decoder->frame->linesize, 0, height, dst_data, dst_linesize);
603 if (ret_scale <= 0) {
604 SET_ERRNO(ERROR_MEDIA_DECODE, "Video conversion failed: %d", ret_scale);
605 break;
606 }
607 if (decode_count <= 3) {
608 log_debug("[SWS_SCALE] Frame %d: returned %d", decode_count, ret_scale);
609 rgb_pixel_t *test_px = (rgb_pixel_t *)decode_buffer->pixels;
610 log_debug("[SWS_RESULT] Frame %d: RGB[0]=(%u,%u,%u)", decode_count, test_px[0].r, test_px[0].g, test_px[0].b);
611 }
612
613 frame_decoded = true;
614 break; // Exit while loop
615 }
616
617 // Release read_frame_mutex now that traced_packet_read() is done
618 mutex_unlock(&decoder->read_frame_mutex);
619
620 // Shutdown can race a buffered read without FFmpeg returning AVERROR_EXIT.
621 // The caller's cancellation request is authoritative for this operation.
622 bool (*cancel)(void *) = (bool (*)(void *))atomic_ptr_load(&decoder->should_exit_callback);
623 bool cancelled = cancel && cancel(atomic_ptr_load(&decoder->exit_callback_user_data));
624
625 if (frame_decoded) {
626 char read_time_str[TIME_DURATION_BUFFER_SIZE];
627 time_pretty(time_elapsed_ns(read_start_ns, time_get_ns()), -1, read_time_str, sizeof(read_time_str));
628 log_dev_every(5 * US_PER_SEC_INT, "PREFETCH: decoded frame in %s", read_time_str);
629 }
630
631 // Re-acquire mutex to update prefetch state
632 mutex_lock(&decoder->prefetch_mutex);
633
634 // Mutex now held - update prefetch state
635 bool obsolete = generation != decoder->seek_generation;
636 bool interrupted = decoder->prefetch_should_stop || decoder->seeking_in_progress || obsolete || cancelled;
637 errno_notifications_suppress(notifications_suppressed);
638 if (interrupted)
640 if (obsolete) {
641 frame_decoded = false;
642 reached_eof = false;
643 }
644 if (!frame_decoded && !reached_eof && !interrupted) {
645 if (!errno_peek_since(decode_scope, &decoder->prefetch_failure)) {
646 if (packet_rejected)
647 SET_ERRNO(ERROR_MEDIA_DECODE, "Video decoder rejected packet: %d", decode_result);
648 else
649 SET_ERRNO(ERROR_MEDIA_DECODE, "Background video decoding failed (FFmpeg result %d)", decode_result);
650 if (!errno_peek_since(decode_scope, &decoder->prefetch_failure)) {
652 SAFE_STRNCPY(decoder->prefetch_failure.context_message, "Background video decoding failed", sizeof(decoder->prefetch_failure.context_message));
653 }
654 }
655 }
656
657 if (!frame_decoded && !reached_eof && !interrupted)
658 atomic_store_explicit(&decoder->prefetch_failed, true, memory_order_release);
659
660 if (frame_decoded) {
661 // Get current decode buffer
662 image_t *decode_buffer = use_image_a ? decoder->prefetch_image_a : decoder->prefetch_image_b;
663
664 // Update the current prefetch image (main thread will pull from this)
665 decoder->current_prefetch_image = decode_buffer;
666 atomic_store_explicit(&decoder->prefetch_frame_ready, true, memory_order_release);
667
668 } else if (cancelled || (!reached_eof && !interrupted)) {
669 // A decoder error terminates prefetch; EOF waits for rewind above.
670 break;
671 }
672 }
673
674 mutex_unlock(&decoder->prefetch_mutex);
675 log_debug("Video prefetch thread stopped");
676 return NULL;
677}
678
682static asciichat_error_t open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type, int *stream_idx, AVCodecContext **codec_ctx) {
683 ASCII_OTEL_SCOPE(FFMPEG_CODEC_OPEN);
684 ASCII_OTEL_ATTR_STRING("media.type", type == AVMEDIA_TYPE_AUDIO ? "audio" : "video");
685 int ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
686 if (ret < 0) {
687 // Stream not found - not an error, just means no stream of this type
688 *stream_idx = -1;
689 *codec_ctx = NULL;
690 return ASCIICHAT_OK;
691 }
692
693 *stream_idx = ret;
694 AVStream *stream = fmt_ctx->streams[ret];
695
696 // Find decoder
697 const AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id);
698 if (!codec) {
699 return SET_ERRNO(ERROR_MEDIA_DECODE, "Codec not found for stream %d", ret);
700 }
701
702 ASCII_OTEL_ATTR_STRING("media.codec", codec->name);
703 // Allocate codec context
704 *codec_ctx = avcodec_alloc_context3(codec);
705 if (!*codec_ctx) {
706 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate codec context");
707 }
708
709 // Copy codec parameters
710 ret = avcodec_parameters_to_context(*codec_ctx, stream->codecpar);
711 if (ret < 0) {
712 avcodec_free_context(codec_ctx);
713 return SET_ERRNO(ret == AVERROR(ENOMEM) ? ERROR_MEMORY : ERROR_MEDIA_DECODE, "Failed to copy codec parameters");
714 }
715
716 // Open codec
717 ret = avcodec_open2(*codec_ctx, codec, NULL);
718 if (ret < 0) {
719 avcodec_free_context(codec_ctx);
720 return SET_ERRNO(ret == AVERROR(ENOMEM) ? ERROR_MEMORY : ERROR_MEDIA_DECODE, "Failed to open codec");
721 }
722
723 return ASCIICHAT_OK;
724}
725
726// A missing/unsupported stream can fall back to the other media type. Allocation
727// and other initialization failures still fail decoder creation with their cause.
728static asciichat_error_t open_optional_codec_context(AVFormatContext *format, enum AVMediaType type, int *stream_index, AVCodecContext **context) {
730 asciichat_error_t result = open_codec_context(format, type, stream_index, context);
732 LOG_ERRNO_IF_SET("Optional media stream unavailable");
733 avcodec_free_context(context);
734 *stream_index = -1;
736 return ASCIICHAT_OK;
737 }
738 if (result != ASCIICHAT_OK)
739 return result;
742 return ASCIICHAT_OK;
743}
744
745/* ============================================================================
746 * Decoder Lifecycle
747 * ============================================================================ */
748
750 return ffmpeg_decoder_create_interruptible(path, NULL, NULL);
751}
752
753ffmpeg_decoder_t *ffmpeg_decoder_create_interruptible(const char *path, bool (*cancel)(void *), void *data) {
754 return ffmpeg_decoder_create_with_request_options(path, cancel, data, NULL, NULL, NULL);
755}
756
757ffmpeg_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) {
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}
1013
1015 ASCII_OTEL_SCOPE(FFMPEG_DECODER_CREATE_STDIN);
1016 // Buffer entire stdin on first call (shared between video and audio decoders)
1017 if (!g_stdin_buffer) {
1018 log_debug("Reading stdin into buffer...");
1019 g_stdin_buffer = stdin_buffer_read_all();
1020 if (!g_stdin_buffer) {
1021 SET_ERRNO(ERROR_MEDIA_OPEN, "Failed to read stdin into buffer");
1022 return NULL;
1023 }
1024 }
1025
1027 if (!decoder) {
1028 SET_ERRNO(ERROR_MEMORY, "Failed to allocate decoder");
1029 return NULL;
1030 }
1031
1032 memset(decoder, 0, sizeof(*decoder));
1033 decoder->video_stream_idx = -1;
1034 decoder->audio_stream_idx = -1;
1035 decoder->is_stdin = true;
1036 decoder->last_video_pts = -1.0;
1037 decoder->last_audio_pts = -1.0;
1038
1039 // Create per-decoder reader (each decoder gets independent position tracking)
1041 if (!reader) {
1042 SET_ERRNO(ERROR_MEMORY, "Failed to allocate buffer reader");
1043 SAFE_FREE(decoder);
1044 return NULL;
1045 }
1046 reader->buffer = g_stdin_buffer;
1047 reader->pos = 0; // Each decoder starts from position 0
1048
1049 // AVIO takes ownership of this buffer and may resize or free it with av_free().
1050 decoder->avio_buffer = av_malloc(AVIO_BUFFER_SIZE);
1051 if (!decoder->avio_buffer) {
1052 SET_ERRNO(ERROR_MEMORY, "Failed to allocate AVIO buffer");
1053 SAFE_FREE(reader);
1054 SAFE_FREE(decoder);
1055 return NULL;
1056 }
1057
1058 decoder->avio_ctx = avio_alloc_context(decoder->avio_buffer, // internal buffer for AVIO
1059 AVIO_BUFFER_SIZE, // buffer size
1060 0, // write_flag
1061 reader, // opaque (per-decoder reader)
1062 memory_read_packet,
1063 NULL, // write_packet
1064 memory_seek_packet // seek (memory is seekable)
1065 );
1066
1067 if (!decoder->avio_ctx) {
1068 SET_ERRNO(ERROR_MEMORY, "Failed to create AVIO context");
1069 av_free(decoder->avio_buffer);
1070 decoder->avio_buffer = NULL;
1071 SAFE_FREE(decoder);
1072 return NULL;
1073 }
1074
1075 // Mark AVIO context as seekable (memory buffer supports seeking)
1076 decoder->avio_ctx->seekable = AVIO_SEEKABLE_NORMAL;
1077
1078 // Allocate format context
1079 decoder->format_ctx = avformat_alloc_context();
1080 if (!decoder->format_ctx) {
1081 SET_ERRNO(ERROR_MEMORY, "Failed to allocate format context");
1082 av_freep(&decoder->avio_ctx->buffer);
1083 avio_context_free(&decoder->avio_ctx);
1084 SAFE_FREE(decoder);
1085 return NULL;
1086 }
1087
1088 decoder->format_ctx->pb = decoder->avio_ctx;
1089 decoder->format_ctx->flags |= AVFMT_FLAG_CUSTOM_IO; // Tell FFmpeg to use our custom AVIO context
1090
1091 // Capture FFmpeg's probing output
1092 int ret = 0;
1093 LOG_IO("ffmpeg", {
1094 // Open input from buffered stdin
1095 ret = traced_format_open(&decoder->format_ctx, NULL, NULL, NULL);
1096 if (ret >= 0) {
1097 // Find stream info
1098 if (traced_format_probe(decoder->format_ctx, NULL) < 0) {
1099 ret = -1;
1100 }
1101 }
1102 });
1103
1104 if (ret < 0) {
1105 if (ret == -1) {
1106 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to find stream info from stdin buffer");
1107 } else {
1108 SET_ERRNO(ERROR_MEDIA_OPEN, "Failed to open stdin buffer");
1109 }
1110 av_freep(&decoder->avio_ctx->buffer);
1111 avio_context_free(&decoder->avio_ctx);
1112 if (decoder->format_ctx) {
1113 avformat_free_context(decoder->format_ctx);
1114 }
1115 SAFE_FREE(decoder);
1116 return NULL;
1117 }
1118
1119 // Open codecs (same as file-based decoder)
1120 asciichat_error_t err = open_optional_codec_context(decoder->format_ctx, AVMEDIA_TYPE_VIDEO, &decoder->video_stream_idx, &decoder->video_codec_ctx);
1121 if (err != ASCIICHAT_OK) {
1122 ffmpeg_decoder_destroy(decoder);
1123 return NULL;
1124 }
1125
1126 if (GET_OPTION(audio_enabled)) {
1127 err = open_optional_codec_context(decoder->format_ctx, AVMEDIA_TYPE_AUDIO, &decoder->audio_stream_idx, &decoder->audio_codec_ctx);
1128 if (err != ASCIICHAT_OK) {
1129 ffmpeg_decoder_destroy(decoder);
1130 return NULL;
1131 }
1132 } else {
1133 decoder->audio_stream_idx = -1;
1134 decoder->audio_codec_ctx = NULL;
1135 log_debug("Audio decoding disabled by user option");
1136 }
1137
1138 if (decoder->video_stream_idx < 0 && decoder->audio_stream_idx < 0) {
1139 SET_ERRNO(ERROR_MEDIA_DECODE, "No video or audio streams found in stdin");
1140 ffmpeg_decoder_destroy(decoder);
1141 return NULL;
1142 }
1143
1144 // Allocate frame and packet
1145 decoder->frame = av_frame_alloc();
1146 decoder->packet = av_packet_alloc();
1147 if (!decoder->frame || !decoder->packet) {
1148 SET_ERRNO(ERROR_MEMORY, "Failed to allocate frame/packet");
1149 ffmpeg_decoder_destroy(decoder);
1150 return NULL;
1151 }
1152
1153 // Initialize swscale/swresample (same as file-based)
1154 if (decoder->video_codec_ctx) {
1155 // Validate codec context has valid dimensions and pixel format
1156 // For stdin/HTTP streams, these might not be valid until first frame is read
1157 if (decoder->video_codec_ctx->width <= 0 || decoder->video_codec_ctx->height <= 0) {
1158 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);
1159 // Don't create swscale context yet - will create it lazily on first frame read
1160 } else if (decoder->video_codec_ctx->pix_fmt == AV_PIX_FMT_NONE) {
1161 log_warn("Video codec has invalid pixel format, will initialize swscale on first frame");
1162 // Don't create swscale context yet - will create it lazily on first frame read
1163 } else {
1164 // Create swscale context with valid parameters
1165 LOG_IO("swscaler", {
1166 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,
1167 decoder->video_codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
1168 });
1169 if (!decoder->sws_ctx) {
1170 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to create swscale context");
1171 ffmpeg_decoder_destroy(decoder);
1172 return NULL;
1173 }
1174 }
1175 }
1176
1177 if (decoder->audio_codec_ctx) {
1178 decoder->swr_ctx = swr_alloc();
1179 if (!decoder->swr_ctx) {
1180 SET_ERRNO(ERROR_MEMORY, "Failed to allocate swresample context");
1181 ffmpeg_decoder_destroy(decoder);
1182 return NULL;
1183 }
1184
1185 av_opt_set_chlayout(decoder->swr_ctx, "in_chlayout", &decoder->audio_codec_ctx->ch_layout, 0);
1186 av_opt_set_int(decoder->swr_ctx, "in_sample_rate", decoder->audio_codec_ctx->sample_rate, 0);
1187 av_opt_set_sample_fmt(decoder->swr_ctx, "in_sample_fmt", decoder->audio_codec_ctx->sample_fmt, 0);
1188
1189 AVChannelLayout out_ch_layout = AV_CHANNEL_LAYOUT_STEREO;
1190 av_opt_set_chlayout(decoder->swr_ctx, "out_chlayout", &out_ch_layout, 0);
1191 av_opt_set_int(decoder->swr_ctx, "out_sample_rate", TARGET_SAMPLE_RATE, 0);
1192 av_opt_set_sample_fmt(decoder->swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
1193 if (decoder->audio_codec_ctx->ch_layout.nb_channels == 1) {
1194 const double matrix[] = {1.0, 1.0};
1195 swr_set_matrix(decoder->swr_ctx, matrix, 1);
1196 }
1197
1198 if (swr_init(decoder->swr_ctx) < 0) {
1199 SET_ERRNO(ERROR_MEDIA_DECODE, "Failed to initialize swresample context");
1200 ffmpeg_decoder_destroy(decoder);
1201 return NULL;
1202 }
1203
1205 decoder->audio_buffer = SAFE_MALLOC(decoder->audio_buffer_size * sizeof(float), float *);
1206 if (!decoder->audio_buffer) {
1207 SET_ERRNO(ERROR_MEMORY, "Failed to allocate audio buffer");
1208 ffmpeg_decoder_destroy(decoder);
1209 return NULL;
1210 }
1211 }
1212
1213 // Initialize video frame prefetching system for stdin (same as file-based)
1214 if (decoder->video_stream_idx >= 0) {
1215 int width = decoder->video_codec_ctx->width;
1216 int height = decoder->video_codec_ctx->height;
1217
1218 // For stdin sources, dimensions might be invalid initially (0x0)
1219 // Use default dimensions for prefetch buffers; they'll be resized on first frame
1220 if (width <= 0 || height <= 0) {
1221 width = 1920; // Default width for stdin
1222 height = 1080; // Default height for stdin
1223 log_debug("stdin: Using default prefetch dimensions %dx%d (will be resized on first frame)", width, height);
1224 }
1225
1226 // Create two prefetch image buffers for double-buffering
1227 decoder->prefetch_image_a = image_new((size_t)width, (size_t)height);
1228 decoder->prefetch_image_b = image_new((size_t)width, (size_t)height);
1229 if (!decoder->prefetch_image_a || !decoder->prefetch_image_b) {
1230 SET_ERRNO(ERROR_MEMORY, "Failed to allocate prefetch image buffers");
1231 ffmpeg_decoder_destroy(decoder);
1232 return NULL;
1233 }
1234
1235 decoder->current_prefetch_image = decoder->prefetch_image_a;
1236 decoder->prefetch_frame_ready = false;
1237
1238 // Initialize prefetch mutex
1239 if (mutex_init(&decoder->prefetch_mutex, "ffmpeg_prefetch") != 0) {
1240 SET_ERRNO(ERROR_MEMORY, "Failed to initialize prefetch mutex");
1241 ffmpeg_decoder_destroy(decoder);
1242 return NULL;
1243 }
1244
1245 if (cond_init(&decoder->prefetch_cond, "ffmpeg_prefetch") != 0) {
1246 SET_ERRNO(ERROR_MEMORY, "Failed to initialize prefetch condition variable");
1247 mutex_destroy(&decoder->prefetch_mutex);
1248 ffmpeg_decoder_destroy(decoder);
1249 return NULL;
1250 }
1251
1252 if (mutex_init(&decoder->read_frame_mutex, "ffmpeg_read_frame") != 0) {
1253 SET_ERRNO(ERROR_MEMORY, "Failed to initialize read_frame mutex");
1254 mutex_destroy(&decoder->prefetch_mutex);
1255 cond_destroy(&decoder->prefetch_cond);
1256 ffmpeg_decoder_destroy(decoder);
1257 return NULL;
1258 }
1259 }
1260
1261 log_debug("FFmpeg decoder opened from stdin (video=%s, audio=%s)", decoder->video_stream_idx >= 0 ? "yes" : "no", decoder->audio_stream_idx >= 0 ? "yes" : "no");
1262
1263 NAMED_REGISTER_FFMPEG_DECODER(decoder, "stdin", NULL);
1264 NAMED_REGISTER_ATOMIC(&decoder->eof_reached, "ffmpeg_eof", decoder);
1265
1266 return decoder;
1267}
1268
1270 ASCII_OTEL_SCOPE(FFMPEG_DECODER_DESTROY);
1271 if (!decoder) {
1272 return;
1273 }
1274
1275 NAMED_UNREGISTER(&decoder->eof_reached);
1276 NAMED_UNREGISTER(decoder);
1277
1278 // Stop prefetch thread (signal it to stop and wait for it to finish)
1279 if (decoder->prefetch_thread_running) {
1280 mutex_lock(&decoder->prefetch_mutex);
1281 decoder->prefetch_should_stop = true;
1282 cond_signal(&decoder->prefetch_cond);
1283 mutex_unlock(&decoder->prefetch_mutex);
1284
1285 // Wait for thread to finish
1286 thread_join(&decoder->prefetch_thread, NULL);
1287 decoder->prefetch_thread_running = false;
1288 }
1289
1290 // Clean up prefetch state
1291 cond_destroy(&decoder->prefetch_cond);
1292 mutex_destroy(&decoder->prefetch_mutex);
1293 mutex_destroy(&decoder->read_frame_mutex);
1294
1295 // Free prefetch image buffers
1296 if (decoder->prefetch_image_a) {
1298 decoder->prefetch_image_a = NULL;
1299 }
1300 if (decoder->prefetch_image_b) {
1302 decoder->prefetch_image_b = NULL;
1303 }
1304
1305 // Don't destroy current_image - it points to one of the prefetch buffers
1306 // which have already been destroyed above
1307 decoder->current_image = NULL;
1308
1309 // Free audio buffer
1310 SAFE_FREE(decoder->audio_buffer);
1311
1312 // Free swscale context
1313 if (decoder->sws_ctx) {
1314 sws_freeContext(decoder->sws_ctx);
1315 decoder->sws_ctx = NULL;
1316 }
1317
1318 // Free swresample context
1319 if (decoder->swr_ctx) {
1320 swr_free(&decoder->swr_ctx);
1321 }
1322
1323 // Free frame and packet
1324 if (decoder->frame) {
1325 av_frame_free(&decoder->frame);
1326 }
1327 if (decoder->packet) {
1328 av_packet_free(&decoder->packet);
1329 }
1330
1331 // Free codec contexts
1332 if (decoder->video_codec_ctx) {
1333 avcodec_free_context(&decoder->video_codec_ctx);
1334 }
1335 if (decoder->audio_codec_ctx) {
1336 avcodec_free_context(&decoder->audio_codec_ctx);
1337 }
1338
1339 // Free format context
1340 if (decoder->format_ctx) {
1341 avformat_close_input(&decoder->format_ctx);
1342 }
1343
1344 // Free AVIO context (stdin only)
1345 if (decoder->avio_ctx) {
1346 // Free the opaque reader for stdin
1347 if (decoder->is_stdin) {
1348 SAFE_FREE(decoder->avio_ctx->opaque);
1349 }
1350 av_freep(&decoder->avio_ctx->buffer);
1351 avio_context_free(&decoder->avio_ctx);
1352 }
1353
1354 SAFE_FREE(decoder);
1355}
1356
1357/* ============================================================================
1358 * Video Operations
1359 * ============================================================================ */
1360
1362 ASCII_OTEL_SCOPE(FFMPEG_DECODER_POLL_VIDEO);
1363 if (frame)
1364 *frame = NULL;
1365 if (failure)
1366 memset(failure, 0, sizeof(*failure));
1367 if (!decoder || !frame || !failure) {
1369 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid video poll outputs");
1370 if (failure && !errno_peek_since(scope, failure))
1371 failure->code = ERROR_INVALID_PARAM;
1372 return FFMPEG_FRAME_FAILED;
1373 }
1374 // Empty polls need no protected state. EOF and errors still confirm under
1375 // the mutex so a final pending frame takes precedence over termination.
1376 if (!atomic_load_explicit(&decoder->prefetch_frame_ready, memory_order_acquire) && !atomic_load_explicit(&decoder->prefetch_failed, memory_order_acquire) && !ffmpeg_decoder_at_end(decoder))
1377 return FFMPEG_FRAME_WAITING;
1378
1379 mutex_lock(&decoder->prefetch_mutex);
1380 if (decoder->prefetch_frame_ready && decoder->current_prefetch_image) {
1381 // Taking the next image releases the previous reader-owned buffer.
1382 if (decoder->current_read_buffer == decoder->prefetch_image_a)
1383 decoder->buffer_a_in_use = false;
1384 else if (decoder->current_read_buffer == decoder->prefetch_image_b)
1385 decoder->buffer_b_in_use = false;
1386 *frame = decoder->current_prefetch_image;
1387 decoder->prefetch_frame_ready = false;
1388 if (*frame == decoder->prefetch_image_a)
1389 decoder->buffer_a_in_use = true;
1390 else if (*frame == decoder->prefetch_image_b)
1391 decoder->buffer_b_in_use = true;
1392 decoder->current_read_buffer = *frame;
1393 decoder->current_image = *frame;
1394 cond_signal(&decoder->prefetch_cond);
1395 mutex_unlock(&decoder->prefetch_mutex);
1396 return FFMPEG_FRAME_READY;
1397 }
1398 *failure = decoder->prefetch_failure;
1399 bool eof = ffmpeg_decoder_at_end(decoder);
1400 mutex_unlock(&decoder->prefetch_mutex);
1401 if (failure->code != ASCIICHAT_OK)
1402 return FFMPEG_FRAME_FAILED;
1404}
1405
1407 ASCII_OTEL_SCOPE(FFMPEG_DECODER_READ_VIDEO_FRAME);
1408 image_t *frame = NULL;
1409 asciichat_error_context_t failure = {0};
1410 if (ffmpeg_decoder_poll_video(decoder, &frame, &failure) == FFMPEG_FRAME_FAILED)
1411 errno_import(&failure);
1412 return frame;
1413}
1414
1423 if (!decoder || decoder->video_stream_idx < 0) {
1424 return SET_ERRNO(ERROR_INVALID_PARAM, "Decoder has no initialized video prefetch buffers");
1425 }
1426
1427 if (!decoder->prefetch_image_a || !decoder->prefetch_image_b) {
1428 return SET_ERRNO(ERROR_INVALID_PARAM, "Decoder has no initialized video prefetch buffers");
1429 }
1430
1431 // Already running
1432 if (decoder->prefetch_thread_running) {
1433 return ASCIICHAT_OK;
1434 }
1435
1436 // Reset stop flag and create thread
1437 decoder->prefetch_should_stop = false;
1438 memset(&decoder->prefetch_failure, 0, sizeof(decoder->prefetch_failure));
1439 atomic_store(&decoder->prefetch_failed, false);
1440
1441 int thread_err = platform_thread_create(&decoder->prefetch_thread, "ffmpeg_prefetch", ffmpeg_decoder_prefetch_thread_func, decoder);
1442 if (thread_err != 0) {
1443 return SET_ERRNO(ERROR_THREAD, "Failed to create video prefetch thread");
1444 }
1445
1446 decoder->prefetch_thread_running = true;
1447 return ASCIICHAT_OK;
1448}
1449
1454 if (!decoder || !decoder->prefetch_thread_running) {
1455 return;
1456 }
1457
1458 mutex_lock(&decoder->prefetch_mutex);
1459 decoder->prefetch_should_stop = true;
1460 cond_signal(&decoder->prefetch_cond);
1461 mutex_unlock(&decoder->prefetch_mutex);
1462 // Wait up to 2 seconds for thread to stop
1463 // The interrupt callback should cause av_read_frame to abort quickly
1464 if (thread_join_timeout(&decoder->prefetch_thread, NULL, 2000 * NS_PER_MS_INT) == ASCIICHAT_OK)
1465 decoder->prefetch_thread_running = false;
1466 // A timed-out worker remains owned and must be joined by decoder destruction.
1467 else
1468 log_warn("Video prefetch thread is still stopping");
1469}
1470
1471void ffmpeg_decoder_set_exit_callback(ffmpeg_decoder_t *decoder, bool (*should_exit_callback)(void *), void *user_data) {
1472 if (!decoder) {
1473 return;
1474 }
1475 atomic_ptr_store(&decoder->exit_callback_user_data, user_data);
1476 atomic_ptr_store(&decoder->should_exit_callback, (void *)should_exit_callback);
1477}
1478
1480 if (!decoder) {
1481 return false;
1482 }
1483 return decoder->prefetch_thread_running;
1484}
1485
1487 return decoder && decoder->video_stream_idx >= 0;
1488}
1489
1491 if (!decoder || decoder->video_stream_idx < 0) {
1492 return SET_ERRNO(ERROR_INVALID_PARAM, "Decoder has no video stream");
1493 }
1494
1495 if (width) {
1496 *width = decoder->video_codec_ctx->width;
1497 }
1498 if (height) {
1499 *height = decoder->video_codec_ctx->height;
1500 }
1501
1502 return ASCIICHAT_OK;
1503}
1504
1506 if (!decoder || decoder->video_stream_idx < 0) {
1507 return -1.0;
1508 }
1509
1510 AVStream *stream = decoder->format_ctx->streams[decoder->video_stream_idx];
1511
1512 // Try avg_frame_rate first (average frame rate from entire stream)
1513 double fps = av_q2d_safe(stream->avg_frame_rate);
1514
1515 // Fallback to r_frame_rate if avg_frame_rate is invalid or zero
1516 // r_frame_rate is the "real" frame rate based on codec parameters
1517 // This is more reliable for YouTube videos and some video codecs
1518 if (fps <= 0.0) {
1519 fps = av_q2d_safe(stream->r_frame_rate);
1520 }
1521
1522 return fps;
1523}
1524
1525/* ============================================================================
1526 * Audio Operations
1527 * ============================================================================ */
1528
1529size_t ffmpeg_decoder_read_audio_samples(ffmpeg_decoder_t *decoder, float *buffer, size_t num_samples) {
1530 ASCII_OTEL_SCOPE(FFMPEG_DECODER_READ_AUDIO_SAMPLES);
1531 // Keep channels and the playback clock aligned even with odd caller capacity.
1532 num_samples -= num_samples % TARGET_CHANNELS;
1533 if (!decoder || decoder->audio_stream_idx < 0 || !buffer || num_samples == 0) {
1534 return 0;
1535 }
1536
1537 mutex_lock(&decoder->read_frame_mutex);
1538 size_t samples_written = 0;
1539
1540 // First, drain any buffered samples
1541 if (decoder->audio_buffer_offset > 0) {
1542 size_t available = decoder->audio_buffer_offset;
1543 size_t to_copy = (available < num_samples) ? available : num_samples;
1544
1545 memcpy(buffer, decoder->audio_buffer, to_copy * sizeof(float));
1546 samples_written += to_copy;
1547
1548 // Shift buffer
1549 if (to_copy < available) {
1550 memmove(decoder->audio_buffer, decoder->audio_buffer + to_copy, (available - to_copy) * sizeof(float));
1551 }
1552 decoder->audio_buffer_offset -= to_copy;
1553
1554 if (samples_written >= num_samples) {
1555 decoder->audio_samples_read += samples_written / TARGET_CHANNELS;
1556 mutex_unlock(&decoder->read_frame_mutex);
1557 return samples_written;
1558 }
1559 }
1560
1561 // Read more packets to fill the request
1562 static uint64_t packet_count = 0;
1563
1564 while (samples_written < num_samples) {
1565 int ret = traced_packet_read(decoder->format_ctx, decoder->packet);
1566 if (ret < 0) {
1567 if (ret == AVERROR_EOF) {
1568 atomic_store_bool(&decoder->eof_reached, true);
1569 }
1570 break;
1571 }
1572
1573 // Check if this is an audio packet
1574 if (decoder->packet->stream_index != decoder->audio_stream_idx) {
1575 av_packet_unref(decoder->packet);
1576 continue;
1577 }
1578
1579 packet_count++;
1580 log_debug_every(US_PER_SEC_INT, "Audio packet #%lu: pts=%ld dts=%ld duration=%d size=%d", packet_count, decoder->packet->pts, decoder->packet->dts, decoder->packet->duration,
1581 decoder->packet->size);
1582
1583 // Send packet to decoder
1584 ret = traced_packet_send(decoder->audio_codec_ctx, decoder->packet);
1585 av_packet_unref(decoder->packet);
1586
1587 if (ret < 0) {
1588 char error_message[AV_ERROR_MAX_STRING_SIZE];
1589 av_strerror(ret, error_message, sizeof(error_message));
1590 log_warn_every(US_PER_SEC_INT, "Error sending audio packet to decoder: %s", error_message);
1591 continue;
1592 }
1593
1594 // Receive all decoded frames from this packet
1595 // Important: a single packet can produce multiple frames. Must drain all before next packet.
1596 while (1) {
1597 ret = traced_frame_receive(decoder->audio_codec_ctx, decoder->frame);
1598 if (ret == AVERROR(EAGAIN)) {
1599 break; // No more frames from this packet, get next packet
1600 } else if (ret < 0) {
1601 log_warn_every(US_PER_SEC_INT, "Error receiving audio frame from decoder");
1602 goto audio_read_done;
1603 }
1604
1605 // Update position tracking
1606 decoder->last_audio_pts = get_frame_pts_seconds(decoder->frame, decoder->format_ctx->streams[decoder->audio_stream_idx]->time_base);
1607
1608 // Resample the complete frame and retain output beyond this callback's
1609 // requested block. Flushing the resampler between packets would insert
1610 // padding into inputs whose sample rate differs from the output rate.
1611 uint8_t *out_ptr = (uint8_t *)decoder->audio_buffer;
1612 int converted = traced_audio_resample(decoder->swr_ctx, &out_ptr, (int)(decoder->audio_buffer_size / TARGET_CHANNELS), (const uint8_t **)decoder->frame->data, decoder->frame->nb_samples);
1613
1614 if (converted > 0) {
1615 size_t available = (size_t)converted * TARGET_CHANNELS;
1616 size_t remaining = num_samples - samples_written;
1617 size_t to_copy = available < remaining ? available : remaining;
1618 memcpy(buffer + samples_written, decoder->audio_buffer, to_copy * sizeof(float));
1619 samples_written += to_copy;
1620 decoder->audio_buffer_offset = available - to_copy;
1621 if (decoder->audio_buffer_offset > 0)
1622 memmove(decoder->audio_buffer, decoder->audio_buffer + to_copy, decoder->audio_buffer_offset * sizeof(float));
1623 }
1624
1625 if (samples_written >= num_samples) {
1626 goto audio_read_done;
1627 }
1628 }
1629 }
1630
1631audio_read_done:
1632 // Flush resampler buffer if we haven't filled the full request
1633 // The resampler may have buffered samples that need to be output
1634 if (samples_written < num_samples && atomic_load_bool(&decoder->eof_reached)) {
1635 int remaining_space = (int)((num_samples - samples_written) / TARGET_CHANNELS);
1636 uint8_t *out_ptr = (uint8_t *)(buffer + samples_written);
1637 int flushed = traced_audio_resample(decoder->swr_ctx, &out_ptr, remaining_space, NULL, 0);
1638 if (flushed > 0) {
1639 samples_written += (size_t)flushed * TARGET_CHANNELS;
1640 }
1641 }
1642
1643 // Update sample-based position tracking
1644 decoder->audio_samples_read += samples_written / TARGET_CHANNELS;
1645 mutex_unlock(&decoder->read_frame_mutex);
1646
1647 return samples_written;
1648}
1649
1651 return decoder && decoder->audio_stream_idx >= 0;
1652}
1653
1654/* ============================================================================
1655 * Playback Control
1656 * ============================================================================ */
1657
1661
1663 ASCII_OTEL_SCOPE(FFMPEG_DECODER_SEEK_TO_TIMESTAMP);
1664 if (!decoder) {
1665 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing decoder for seek");
1666 }
1667
1668 if (decoder->is_stdin) {
1669 return ERROR_NOT_SUPPORTED; // Cannot seek stdin
1670 }
1671
1672 // Hold mutex during entire seek operation to prevent race with prefetch thread
1673 mutex_lock(&decoder->prefetch_mutex);
1674
1675 // Set flag to pause prefetch thread via condition variable
1676 decoder->seeking_in_progress = true;
1677 ++decoder->seek_generation;
1678 // Serialize codec and partial-audio state with the existing packet reader.
1679 mutex_lock(&decoder->read_frame_mutex);
1680 // The old reader has stopped. Holding both guards prevents another read;
1681 // the seek must not cancel its own I/O through the interrupt callback.
1682 decoder->seeking_in_progress = false;
1683 if (decoder->format_ctx->pb && decoder->format_ctx->pb->error == AVERROR_EXIT)
1684 decoder->format_ctx->pb->error = 0;
1685
1686 // Convert seconds to FFmpeg time base units (AV_TIME_BASE = 1,000,000)
1687 int64_t target_ts = (int64_t)(timestamp_sec * AV_TIME_BASE);
1688
1689 // For HTTP streams, use simple keyframe seeking (faster than frame-accurate seeking)
1690 // HTTP seeking is expensive and can break stream state, so prefer speed over precision
1691 int seek_ret = av_seek_frame(decoder->format_ctx, -1, target_ts, AVSEEK_FLAG_BACKWARD);
1692 if (seek_ret < 0) {
1693 // Fallback: try without backward flag
1694 seek_ret = av_seek_frame(decoder->format_ctx, -1, target_ts, 0);
1695 }
1696
1697 if (seek_ret < 0) {
1698 decoder->seeking_in_progress = false;
1699 mutex_unlock(&decoder->read_frame_mutex);
1700 cond_signal(&decoder->prefetch_cond);
1701 mutex_unlock(&decoder->prefetch_mutex);
1702 return SET_ERRNO(ERROR_MEDIA_SEEK, "Failed to seek to timestamp %.2f seconds", timestamp_sec);
1703 }
1704
1705 // Flush codec buffers AFTER seeking
1706 if (decoder->video_codec_ctx) {
1707 avcodec_flush_buffers(decoder->video_codec_ctx);
1708 }
1709 if (decoder->audio_codec_ctx) {
1710 avcodec_flush_buffers(decoder->audio_codec_ctx);
1711 }
1712
1713 // Reset state
1714 atomic_store_bool(&decoder->eof_reached, false);
1715 decoder->video_draining = false;
1716 decoder->audio_buffer_offset = 0;
1717 // Clear any stale audio data in buffer
1718 if (decoder->audio_buffer) {
1719 memset(decoder->audio_buffer, 0, decoder->audio_buffer_size * sizeof(float));
1720 }
1721 decoder->last_video_pts = -1.0;
1722 decoder->last_audio_pts = -1.0;
1723 decoder->prefetch_frame_ready = false;
1724 // Set audio_samples_read to match the seek target so position tracking works correctly
1725 decoder->audio_samples_read = (uint64_t)(timestamp_sec * decoder->audio_sample_rate);
1726
1727 // The consumer retains its image across seeks until it takes a replacement.
1728
1729 // Resume prefetch thread
1730 decoder->seeking_in_progress = false;
1731 mutex_unlock(&decoder->read_frame_mutex);
1732 cond_signal(&decoder->prefetch_cond);
1733
1734 // Release mutex - prefetch thread can resume
1735 mutex_unlock(&decoder->prefetch_mutex);
1736
1737 return ASCIICHAT_OK;
1738}
1739
1741 return decoder && atomic_load_bool(&decoder->eof_reached);
1742}
1743
1745 if (!decoder || !decoder->format_ctx) {
1746 return -1.0;
1747 }
1748
1749 if (decoder->format_ctx->duration == AV_NOPTS_VALUE) {
1750 return -1.0;
1751 }
1752
1753 return (double)decoder->format_ctx->duration / AV_TIME_BASE;
1754}
1755
1757 if (!decoder) {
1758 return -1.0;
1759 }
1760
1761 // Prefer sample-based position tracking (continuous, works before frames are decoded)
1762 if (decoder->audio_sample_rate > 0 && decoder->audio_samples_read >= 0) {
1763 return (double)decoder->audio_samples_read / (double)decoder->audio_sample_rate;
1764 }
1765
1766 // Fallback to frame-based position if available
1767 if (decoder->last_video_pts >= 0.0) {
1768 return decoder->last_video_pts;
1769 } else if (decoder->last_audio_pts >= 0.0) {
1770 return decoder->last_audio_pts;
1771 }
1772
1773 return -1.0;
1774}
bool errno_peek_since(asciichat_errno_scope_t scope, asciichat_error_context_t *out)
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
bool errno_notifications_suppress(bool suppress)
asciichat_errno_scope_t errno_checkpoint(void)
asciichat_errno_scope_t errno_scope_begin(void)
asciichat_error_t errno_import(const asciichat_error_context_t *cause)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
#define ASSERT_NO_ERRNO_SINCE(scope)
@ ASCIICHAT_ERRNO_HANDLED
@ ASCIICHAT_ERRNO_DISMISSED
#define LOG_ERRNO_IF_SET(message)
#define HAS_ERRNO_CODE_SINCE(scope, code)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
Named object registry for debugging — log identifiable resource names.
🎞️ FFmpeg-based media decoder for video and audio streams
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
#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_MEDIA_SEEK
Definition error_codes.h:72
@ 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
@ ERROR_THREAD
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
bool ffmpeg_decoder_at_end(ffmpeg_decoder_t *decoder)
Check if decoder reached end of stream.
double ffmpeg_decoder_get_position(ffmpeg_decoder_t *decoder)
Get current playback position in seconds.
void ffmpeg_decoder_stop_prefetch(ffmpeg_decoder_t *decoder)
Stop the background frame prefetching thread.
image_t * ffmpeg_decoder_read_video_frame(ffmpeg_decoder_t *decoder)
bool ffmpeg_decoder_has_video(ffmpeg_decoder_t *decoder)
Check if decoder has video stream.
double ffmpeg_decoder_get_video_fps(ffmpeg_decoder_t *decoder)
Get video frame rate.
asciichat_error_t ffmpeg_decoder_rewind(ffmpeg_decoder_t *decoder)
Seek to beginning of media.
asciichat_error_t ffmpeg_decoder_get_video_dimensions(ffmpeg_decoder_t *decoder, int *width, int *height)
Get video dimensions.
ffmpeg_frame_status_t
Decode next video frame.
bool ffmpeg_decoder_has_audio(ffmpeg_decoder_t *decoder)
Check if decoder has audio stream.
bool ffmpeg_decoder_is_prefetch_running(ffmpeg_decoder_t *decoder)
Check if background frame prefetching thread is running.
asciichat_error_t ffmpeg_decoder_start_prefetch(ffmpeg_decoder_t *decoder)
Start the background frame prefetching thread.
asciichat_error_t ffmpeg_decoder_seek_to_timestamp(ffmpeg_decoder_t *decoder, double timestamp_sec)
Seek to specific timestamp in media.
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.
void ffmpeg_decoder_destroy(ffmpeg_decoder_t *decoder)
Destroy FFmpeg decoder and free resources.
ffmpeg_decoder_t * ffmpeg_decoder_create(const char *path)
Create FFmpeg decoder from file path.
size_t ffmpeg_decoder_read_audio_samples(ffmpeg_decoder_t *decoder, float *buffer, size_t num_samples)
Decode audio samples.
ffmpeg_frame_status_t ffmpeg_decoder_poll_video(ffmpeg_decoder_t *decoder, image_t **frame, asciichat_error_context_t *failure)
ffmpeg_decoder_t * ffmpeg_decoder_create_stdin(void)
Create FFmpeg decoder from stdin.
double ffmpeg_decoder_get_duration(ffmpeg_decoder_t *decoder)
Get media duration in seconds.
@ FFMPEG_FRAME_READY
@ FFMPEG_FRAME_WAITING
@ FFMPEG_FRAME_FAILED
@ FFMPEG_FRAME_EOF
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as a compact duration with configurable sub-minute precision.
Definition time.c:368
#define TIME_DURATION_BUFFER_SIZE
Definition time.h:40
#define NS_PER_MS_INT
Definition time.h:172
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)
Calculate elapsed time with wraparound safety.
Definition time.c:117
#define US_PER_SEC_INT
Definition time.h:177
#define GET_OPTION(field)
Definition options.h:1333
bool url_is_valid(const char *url)
URL validation using shared parsing and ICU hostname checks.
Definition url.c:156
void image_destroy(image_t *image)
Destroy an image allocated with image_new().
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Capture stdout/stderr and redirect to logging system.
#define LOG_IO(prefix, block)
Definition io.h:104
ffmpeg_decoder_t * ffmpeg_decoder_create_interruptible(const char *path, bool(*cancel)(void *), void *data)
#define MEDIA_FFMPEG_DECODER_DECODER_NAME_BUFFER_SIZE
#define TARGET_CHANNELS
#define AVIO_BUFFER_SIZE
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)
#define TARGET_SAMPLE_RATE
📝 Logging API with multiple log levels and terminal output control
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:743
#define log_warn_every(interval_us, fmt,...)
Rate-limited WARN logging.
Definition log/log.h:749
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define ASCII_OTEL_ATTR_INT(key, value)
Definition otel.h:72
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:71
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
Atomic value wrapper for integral/boolean types.
Definition atomic.h:110
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
FFmpeg decoder state for video and audio decoding.
mutex_t prefetch_mutex
Protect prefetch state and FFmpeg decoder access.
uint64_t audio_samples_read
Total audio samples decoded and output.
AVCodecContext * video_codec_ctx
Video codec context.
image_t * prefetch_image_a
First prefetch buffer.
AVFormatContext * format_ctx
FFmpeg format/container context.
image_t * current_read_buffer
Buffer main thread is currently reading/rendering.
_Atomic bool seeking_in_progress
Signal to pause prefetch thread during seek.
platform_thread_t prefetch_thread
Prefetch thread handle.
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.
size_t audio_buffer_offset
Current offset in audio buffer.
bool buffer_b_in_use
Whether prefetch_image_b is being read by main thread.
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.
AVIOContext * avio_ctx
Custom I/O context for stdin.
bool video_draining
Whether EOF was sent to the video decoder.
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.
unsigned char * avio_buffer
Buffer for custom I/O.
double last_audio_pts
Last audio presentation timestamp.
AVCodecContext * audio_codec_ctx
Audio codec context.
bool is_stdin
Whether reading from stdin.
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.
bool buffer_a_in_use
Whether prefetch_image_a is being read by main thread.
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.
asciichat_error_context_t prefetch_failure
_Atomic uint64_t seek_generation
Detect seeks before and after packet reads.
int audio_stream_idx
Audio stream index (-1 if none).
AVFrame * frame
Reusable frame for decoding.
image_t * current_image
Working buffer for decoding.
struct SwsContext * sws_ctx
Software scaler for format conversion.
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
size_t pos
Current read position (per-decoder).
stdin_buffer_t * buffer
Pointer to shared buffer.
uint8_t * data
Buffered data (shared between decoders).
size_t size
Total size of buffered data.
⏱️ High-precision timing utilities using sokol_time.h and uthash
🌐 Production-Grade URL Validation and Parsing