| 1 |
#include <libavcodec/avcodec.h> |
| 2 |
#include <libavformat/avformat.h> |
| 3 |
#include <libavfilter/avfilter.h> |
| 4 |
#include <libavfilter/buffersink.h> |
| 5 |
#include <libavfilter/buffersrc.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#ifdef _WIN32 |
| 9 |
#include <windows.h> |
| 10 |
#else |
| 11 |
#include <pthread.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#ifdef _WIN32 |
| 15 |
static CRITICAL_SECTION json_mutex; |
| 16 |
static CONDITION_VARIABLE json_cond; |
| 17 |
static int mutex_initialized = 0; |
| 18 |
#else |
| 19 |
static pthread_mutex_t json_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 20 |
static pthread_cond_t json_cond = PTHREAD_COND_INITIALIZER; |
| 21 |
#endif |
| 22 |
|
| 23 |
static char json_buffer[2048] = {0}; |
| 24 |
static int json_captured = 0; |
| 25 |
|
| 26 |
// Custom logging callback |
| 27 |
static void logging_callback(void *ptr, int level, const char *fmt, va_list vl) { |
| 28 |
char line[2048]; |
| 29 |
vsnprintf(line, sizeof(line), fmt, vl); |
| 30 |
|
| 31 |
const char *json_start = strstr(line, "{"); |
| 32 |
if (json_start) { |
| 33 |
#ifdef _WIN32 |
| 34 |
EnterCriticalSection(&json_mutex); |
| 35 |
#else |
| 36 |
pthread_mutex_lock(&json_mutex); |
| 37 |
#endif |
| 38 |
|
| 39 |
size_t len = strnlen(json_start, sizeof(json_buffer) - 1); |
| 40 |
memcpy(json_buffer, json_start, len); |
| 41 |
json_buffer[len] = '\0'; |
| 42 |
json_captured = 1; |
| 43 |
|
| 44 |
#ifdef _WIN32 |
| 45 |
WakeConditionVariable(&json_cond); |
| 46 |
LeaveCriticalSection(&json_mutex); |
| 47 |
#else |
| 48 |
pthread_cond_signal(&json_cond); |
| 49 |
pthread_mutex_unlock(&json_mutex); |
| 50 |
#endif |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
char* loudnorm_get_stats( |
| 55 |
AVFormatContext* fmt_ctx, |
| 56 |
int audio_stream_index, |
| 57 |
const char* loudnorm_args |
| 58 |
) { |
| 59 |
char* result = NULL; |
| 60 |
json_captured = 0; // Reset the captured flag |
| 61 |
memset(json_buffer, 0, sizeof(json_buffer)); // Clear the buffer |
| 62 |
|
| 63 |
#ifdef _WIN32 |
| 64 |
// Initialize synchronization objects if needed |
| 65 |
if (!mutex_initialized) { |
| 66 |
InitializeCriticalSection(&json_mutex); |
| 67 |
InitializeConditionVariable(&json_cond); |
| 68 |
mutex_initialized = 1; |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
av_log_set_callback(logging_callback); |
| 73 |
|
| 74 |
AVFilterGraph *filter_graph = NULL; |
| 75 |
AVFilterContext *src_ctx = NULL, *sink_ctx = NULL, *loudnorm_ctx = NULL; |
| 76 |
|
| 77 |
AVCodec *codec = NULL; |
| 78 |
AVCodecContext *codec_ctx = NULL; |
| 79 |
int ret; |
| 80 |
|
| 81 |
AVCodecParameters *codecpar = fmt_ctx->streams[audio_stream_index]->codecpar; |
| 82 |
codec = (AVCodec *)avcodec_find_decoder(codecpar->codec_id); |
| 83 |
codec_ctx = avcodec_alloc_context3(codec); |
| 84 |
avcodec_parameters_to_context(codec_ctx, codecpar); |
| 85 |
avcodec_open2(codec_ctx, codec, NULL); |
| 86 |
|
| 87 |
char ch_layout_str[64]; |
| 88 |
av_channel_layout_describe(&codecpar->ch_layout, ch_layout_str, sizeof(ch_layout_str)); |
| 89 |
|
| 90 |
filter_graph = avfilter_graph_alloc(); |
| 91 |
|
| 92 |
char args[512]; |
| 93 |
snprintf(args, sizeof(args), |
| 94 |
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s", |
| 95 |
fmt_ctx->streams[audio_stream_index]->time_base.num, |
| 96 |
fmt_ctx->streams[audio_stream_index]->time_base.den, |
| 97 |
codecpar->sample_rate, |
| 98 |
av_get_sample_fmt_name(codec_ctx->sample_fmt), |
| 99 |
ch_layout_str); |
| 100 |
|
| 101 |
avfilter_graph_create_filter(&src_ctx, avfilter_get_by_name("abuffer"), |
| 102 |
"src", args, NULL, filter_graph); |
| 103 |
avfilter_graph_create_filter(&sink_ctx, avfilter_get_by_name("abuffersink"), |
| 104 |
"sink", NULL, NULL, filter_graph); |
| 105 |
avfilter_graph_create_filter(&loudnorm_ctx, avfilter_get_by_name("loudnorm"), |
| 106 |
"loudnorm", loudnorm_args, NULL, filter_graph); |
| 107 |
|
| 108 |
avfilter_link(src_ctx, 0, loudnorm_ctx, 0); |
| 109 |
avfilter_link(loudnorm_ctx, 0, sink_ctx, 0); |
| 110 |
avfilter_graph_config(filter_graph, NULL); |
| 111 |
|
| 112 |
AVPacket *packet = av_packet_alloc(); |
| 113 |
AVFrame *frame = av_frame_alloc(); |
| 114 |
AVFrame *filt_frame = av_frame_alloc(); |
| 115 |
|
| 116 |
while ((ret = av_read_frame(fmt_ctx, packet)) >= 0) { |
| 117 |
if (packet->stream_index != audio_stream_index) { |
| 118 |
av_packet_unref(packet); |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
ret = avcodec_send_packet(codec_ctx, packet); |
| 123 |
if (ret < 0) { |
| 124 |
av_packet_unref(packet); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
while (ret >= 0) { |
| 129 |
ret = avcodec_receive_frame(codec_ctx, frame); |
| 130 |
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; |
| 131 |
if (ret < 0) goto end; |
| 132 |
|
| 133 |
ret = av_buffersrc_add_frame_flags(src_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF); |
| 134 |
if (ret < 0) goto end; |
| 135 |
|
| 136 |
while (1) { |
| 137 |
ret = av_buffersink_get_frame(sink_ctx, filt_frame); |
| 138 |
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; |
| 139 |
if (ret < 0) goto end; |
| 140 |
av_frame_unref(filt_frame); |
| 141 |
} |
| 142 |
} |
| 143 |
av_packet_unref(packet); |
| 144 |
} |
| 145 |
|
| 146 |
// Flush decoder |
| 147 |
avcodec_send_packet(codec_ctx, NULL); |
| 148 |
while (avcodec_receive_frame(codec_ctx, frame) >= 0) { |
| 149 |
ret = av_buffersrc_add_frame(src_ctx, frame); |
| 150 |
if (ret < 0) goto end; |
| 151 |
} |
| 152 |
|
| 153 |
// Flush filter |
| 154 |
ret = av_buffersrc_add_frame(src_ctx, NULL); |
| 155 |
if (ret < 0) goto end; |
| 156 |
while (av_buffersink_get_frame(sink_ctx, filt_frame) >= 0) { |
| 157 |
av_frame_unref(filt_frame); |
| 158 |
} |
| 159 |
|
| 160 |
// Pushes graph |
| 161 |
avfilter_graph_free(&filter_graph); |
| 162 |
|
| 163 |
end: |
| 164 |
avcodec_free_context(&codec_ctx); |
| 165 |
avformat_close_input(&fmt_ctx); |
| 166 |
av_frame_free(&filt_frame); |
| 167 |
av_frame_free(&frame); |
| 168 |
av_packet_free(&packet); |
| 169 |
|
| 170 |
#ifdef _WIN32 |
| 171 |
EnterCriticalSection(&json_mutex); |
| 172 |
while (!json_captured) { |
| 173 |
if (!SleepConditionVariableCS(&json_cond, &json_mutex, 5000)) { // 5 second timeout |
| 174 |
fprintf(stderr, "Timeout waiting for JSON data\n"); |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
if (json_captured) { |
| 179 |
result = _strdup(json_buffer); // Use _strdup on Windows |
| 180 |
} |
| 181 |
LeaveCriticalSection(&json_mutex); |
| 182 |
#else |
| 183 |
struct timespec timeout; |
| 184 |
clock_gettime(CLOCK_REALTIME, &timeout); |
| 185 |
timeout.tv_sec += 5; // 5 second timeout |
| 186 |
|
| 187 |
pthread_mutex_lock(&json_mutex); |
| 188 |
while (json_captured == 0) { |
| 189 |
int ret = pthread_cond_timedwait(&json_cond, &json_mutex, &timeout); |
| 190 |
if (ret == ETIMEDOUT) { |
| 191 |
fprintf(stderr, "Timeout waiting for JSON data\n"); |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
if (json_captured) { |
| 196 |
result = strdup(json_buffer); |
| 197 |
} |
| 198 |
pthread_mutex_unlock(&json_mutex); |
| 199 |
#endif |
| 200 |
|
| 201 |
av_log_set_callback(av_log_default_callback); |
| 202 |
return result; |
| 203 |
} |