Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * tools/openavx_test_pattern.c - SMPTE test pattern and tone generator CLI
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <signal.h>
16 #include <time.h>
17 #include <math.h>
18 #include "avx_platform.h"
19  
20 #include "openavx.h"
21 #include "smpte_h264_pattern.h"
22 #include "openavx_cli.h"
23  
24 #ifndef M_PI
25 #define M_PI 3.14159265358979323846
26 #endif
27  
28 static volatile bool g_running = true;
29  
30 static void sigint_handler(int sig) {
31 (void)sig;
32 g_running = false;
33 }
34  
35 typedef enum {
36 CODEC_UYVY,
37 CODEC_H264
38 } codec_type_e;
39  
40 typedef enum {
41 PATTERN_SMPTE_BARS,
42 PATTERN_COLOR_BARS_100,
43 PATTERN_GRID,
44 PATTERN_RAMP,
45 PATTERN_SOLID_WHITE,
46 PATTERN_SOLID_BLACK,
47 PATTERN_SOLID_GRAY
48 } pattern_type_e;
49  
50 typedef enum {
51 TONE_1000HZ,
52 TONE_440HZ,
53 TONE_BEEP,
54 TONE_NONE
55 } tone_type_e;
56  
57 /* Rec. 709 / Rec. 601 8-bit YCbCr Color definitions for UYVY (U, Y, V) */
58 typedef struct {
59 uint8_t u;
60 uint8_t y;
61 uint8_t v;
62 } yuv_color_t;
63  
64 #if defined(__GNUC__) || defined(__clang__)
65 #define AVX_UNUSED __attribute__((unused))
66 #else
67 #define AVX_UNUSED
68 #endif
69  
70 static const yuv_color_t COLOR_75_WHITE = { 128, 180, 128 };
71 static const yuv_color_t COLOR_75_YELLOW AVX_UNUSED = { 44, 168, 142 };
72 static const yuv_color_t COLOR_75_CYAN AVX_UNUSED = { 148, 145, 24 };
73 static const yuv_color_t COLOR_75_GREEN AVX_UNUSED = { 64, 133, 38 };
74 static const yuv_color_t COLOR_75_MAGENTA = { 192, 63, 218 };
75 static const yuv_color_t COLOR_75_RED AVX_UNUSED = { 108, 51, 232 };
76 static const yuv_color_t COLOR_75_BLUE = { 212, 28, 114 };
77 static const yuv_color_t COLOR_BLACK = { 128, 16, 128 };
78  
79 static const yuv_color_t COLOR_100_WHITE = { 128, 235, 128 };
80 static const yuv_color_t COLOR_100_YELLOW AVX_UNUSED = { 16, 210, 146 };
81 static const yuv_color_t COLOR_100_CYAN AVX_UNUSED = { 166, 170, 16 };
82 static const yuv_color_t COLOR_100_GREEN AVX_UNUSED = { 54, 145, 34 };
83 static const yuv_color_t COLOR_100_MAGENTA AVX_UNUSED = { 202, 106, 222 };
84 static const yuv_color_t COLOR_100_RED AVX_UNUSED = { 90, 81, 240 };
85 static const yuv_color_t COLOR_100_BLUE AVX_UNUSED = { 240, 41, 110 };
86  
87 static const yuv_color_t SMPTE_75_BARS[8] = {
88 { 128, 180, 128 }, /* 75% White / Gray */
89 { 44, 168, 142 }, /* Yellow */
90 { 148, 145, 24 }, /* Cyan */
91 { 64, 133, 38 }, /* Green */
92 { 192, 63, 218 }, /* Magenta */
93 { 108, 51, 232 }, /* Red */
94 { 212, 28, 114 }, /* Blue */
95 { 128, 16, 128 } /* Black */
96 };
97  
98 static const yuv_color_t COLOR_100_BARS[8] = {
99 { 128, 235, 128 }, /* 100% White */
100 { 16, 210, 146 }, /* Yellow */
101 { 166, 170, 16 }, /* Cyan */
102 { 54, 145, 34 }, /* Green */
103 { 202, 106, 222 }, /* Magenta */
104 { 90, 81, 240 }, /* Red */
105 { 240, 41, 110 }, /* Blue */
106 { 128, 16, 128 } /* Black */
107 };
108  
109 static inline void set_macropixel_uyvy(uint8_t* p, yuv_color_t c0, yuv_color_t c1) {
110 p[0] = (uint8_t)(((int)c0.u + c1.u) >> 1);
111 p[1] = c0.y;
112 p[2] = (uint8_t)(((int)c0.v + c1.v) >> 1);
113 p[3] = c1.y;
114 }
115  
116 static void render_base_pattern(uint8_t* buf, int width, int height, pattern_type_e pat) {
117 int stride = width * 2;
118  
119 switch (pat) {
120 case PATTERN_SMPTE_BARS:
121 case PATTERN_COLOR_BARS_100: {
122 const yuv_color_t* bars = (pat == PATTERN_COLOR_BARS_100) ? COLOR_100_BARS : SMPTE_75_BARS;
123 int top_h = (height * 3) / 4;
124  
125 /* Top 75%: 8 Primary Color Bars */
126 for (int y = 0; y < top_h; ++y) {
127 uint8_t* row = buf + y * stride;
128 for (int x = 0; x < width; x += 2) {
129 int bar_idx = (x * 8) / width;
130 if (bar_idx >= 8) bar_idx = 7;
131 set_macropixel_uyvy(row + x * 2, bars[bar_idx], bars[bar_idx]);
132 }
133 }
134  
135 /* Bottom 25%: SMPTE cast bars (Blue, White, Purple, Black Pluge) */
136 for (int y = top_h; y < height; ++y) {
137 uint8_t* row = buf + y * stride;
138 for (int x = 0; x < width; x += 2) {
139 int col_idx = (x * 4) / width;
140 yuv_color_t c;
141 if (col_idx == 0) c = COLOR_75_BLUE;
142 else if (col_idx == 1) c = COLOR_100_WHITE;
143 else if (col_idx == 2) c = COLOR_75_MAGENTA;
144 else c = COLOR_BLACK;
145 set_macropixel_uyvy(row + x * 2, c, c);
146 }
147 }
148 break;
149 }
150 case PATTERN_GRID: {
151 for (int y = 0; y < height; ++y) {
152 uint8_t* row = buf + y * stride;
153 bool is_grid_y = (y % 60 == 0) || (y == height - 1);
154 for (int x = 0; x < width; x += 2) {
155 bool is_grid_x = (x % 60 < 2);
156 yuv_color_t c = (is_grid_y || is_grid_x) ? COLOR_100_WHITE : COLOR_BLACK;
157 set_macropixel_uyvy(row + x * 2, c, c);
158 }
159 }
160 break;
161 }
162 case PATTERN_RAMP: {
163 for (int y = 0; y < height; ++y) {
164 uint8_t* row = buf + y * stride;
165 for (int x = 0; x < width; x += 2) {
166 uint8_t y_val = (uint8_t)(16 + (x * (235 - 16)) / width);
167 yuv_color_t c = { 128, y_val, 128 };
168 set_macropixel_uyvy(row + x * 2, c, c);
169 }
170 }
171 break;
172 }
173 case PATTERN_SOLID_WHITE: {
174 for (int y = 0; y < height; ++y) {
175 uint8_t* row = buf + y * stride;
176 for (int x = 0; x < width; x += 2) {
177 set_macropixel_uyvy(row + x * 2, COLOR_100_WHITE, COLOR_100_WHITE);
178 }
179 }
180 break;
181 }
182 case PATTERN_SOLID_BLACK: {
183 for (int y = 0; y < height; ++y) {
184 uint8_t* row = buf + y * stride;
185 for (int x = 0; x < width; x += 2) {
186 set_macropixel_uyvy(row + x * 2, COLOR_BLACK, COLOR_BLACK);
187 }
188 }
189 break;
190 }
191 case PATTERN_SOLID_GRAY: {
192 for (int y = 0; y < height; ++y) {
193 uint8_t* row = buf + y * stride;
194 for (int x = 0; x < width; x += 2) {
195 set_macropixel_uyvy(row + x * 2, COLOR_75_WHITE, COLOR_75_WHITE);
196 }
197 }
198 break;
199 }
200 }
201 }
202  
203 static void overlay_moving_ticker(uint8_t* dest, const uint8_t* src, int width, int height, int bar_pos_x, int bar_w) {
204 int stride = width * 2;
205 memcpy(dest, src, (size_t)stride * height);
206  
207 /* Draw moving high-contrast white box with dark border across the screen */
208 int x_start = bar_pos_x;
209 int x_end = bar_pos_x + bar_w;
210 if (x_end > width) x_end = width;
211  
212 int box_y_start = height / 4;
213 int box_y_end = (height * 3) / 4;
214  
215 for (int y = box_y_start; y < box_y_end; ++y) {
216 uint8_t* row = dest + y * stride;
217 for (int x = x_start; x < x_end; x += 2) {
218 yuv_color_t c = (y == box_y_start || y == box_y_end - 1 || x == x_start || x >= x_end - 2)
219 ? COLOR_BLACK : COLOR_100_WHITE;
220 set_macropixel_uyvy(row + x * 2, c, c);
221 }
222 }
223 }
224  
225 static void generate_audio_frame(float* left, float* right, uint32_t frames, tone_type_e tone, int64_t* sample_counter) {
226 const double sample_rate = 48000.0;
227 const double amplitude = 0.10; /* -20 dBFS nominal broadcast line level */
228  
229 double freq = 1000.0;
230 if (tone == TONE_440HZ) freq = 440.0;
231  
232 for (uint32_t i = 0; i < frames; ++i) {
233 int64_t s_idx = (*sample_counter)++;
234 double val = 0.0;
235  
236 if (tone == TONE_1000HZ || tone == TONE_440HZ) {
237 double t = (double)s_idx / sample_rate;
238 val = amplitude * sin(2.0 * M_PI * freq * t);
239 } else if (tone == TONE_BEEP) {
240 /* 100ms tone burst at the top of every second (4800 samples) */
241 int second_pos = (int)(s_idx % 48000);
242 if (second_pos < 4800) {
243 double t = (double)s_idx / sample_rate;
244 val = amplitude * sin(2.0 * M_PI * 1000.0 * t);
245 } else {
246 val = 0.0;
247 }
248 } else {
249 val = 0.0;
250 }
251  
252 left[i] = (float)val;
253 right[i] = (float)val;
254 }
255 }
256  
257 static void print_usage(const char* prog) {
258 printf("OpenAVX Test Pattern Generator (Broadcast Signal Generator)\n");
259 printf("Usage: %s [options]\n\n", prog);
260 printf("Options:\n");
261 printf(" -n, --name <NAME> Stream name (default: \"OpenAVX Test Pattern\")\n");
262 printf(" -p, --pattern <TYPE> Test pattern: smpte, bars100, grid, ramp, white, black, gray (default: smpte)\n");
263 printf(" -r, --res <RESOLUTION> Video resolution: 1080p, 720p, 4k, or WxH (default: 1920x1080)\n");
264 printf(" -f, --fps <FRAMERATE> Framerate: 60, 59.94, 50, 30, 29.97, 25, 24 (default: 60)\n");
265 printf(" -t, --tone <TONE> Audio tone: 1000, 440, beep, none (default: 1000 Hz @ -20 dBFS)\n");
266 printf(" -C, --codec <CODEC> Video codec: uyvy, h264 (default: uyvy, auto-switches to h264 for web/rtsp)\n");
267 printf(" --h264 Shorthand for --codec h264 (Annex-B H.264 bitstream)\n");
268 printf(" -W, --web [PORT] Enable embedded HTTP/fMP4 Web Preview server (default: 8080)\n");
269 printf(" -R, --rtsp [PORT] Enable native RTSP 1.0 server (default: 8554)\n");
270 printf(" -m, --moving Enable moving live motion ticker (UYVY only, default: enabled)\n");
271 printf(" --no-moving Disable moving live motion ticker\n");
272 printf(" -g, --groups <GROUPS> Broadcast groups (default: public)\n");
273 printf(" -c, --count <FRAMES> Transmit N frames then exit (default: run indefinitely)\n");
274 printf(" -q, --quiet Quiet mode, suppress live frame stats\n");
275 printf(" -h, --help Display this help and exit\n\n");
276 printf("Examples:\n");
277 printf(" %s # 1080p60 SMPTE bars with 1kHz tone\n", prog);
278 printf(" %s --web # Instant Web preview on http://localhost:8080\n", prog);
279 printf(" %s --rtsp 8554 --web 8080 --h264 # Dual RTSP & Web streaming reference bars\n", prog);
280 printf(" %s --res 4k --fps 59.94 # 4K UHD 59.94 FPS\n", prog);
281 printf(" %s --pattern grid --tone beep # Alignment grid with 1-second sync beeper\n", prog);
282 }
283  
284 int openavx_cmd_pattern(int argc, char** argv) {
285 optind = 1;
286 g_running = true;
287 char stream_name[256] = "OpenAVX Test Pattern";
288 char groups[256] = "public";
289 pattern_type_e pat_type = PATTERN_SMPTE_BARS;
290 tone_type_e tone_type = TONE_1000HZ;
291 codec_type_e codec = CODEC_UYVY;
292 bool codec_explicitly_set = false;
293 bool enable_web = false;
294 uint16_t web_port = 8080;
295 bool enable_rtsp = false;
296 uint16_t rtsp_port = 8554;
297 char rtsp_path[128] = "live";
298 int width = 1920;
299 int height = 1080;
300 int fps_n = 60000;
301 int fps_d = 1000;
302 bool moving_bar = true;
303 int frame_limit = 0;
304 bool quiet = false;
305 const char* bad_pattern = NULL;
306 const char* bad_res = NULL;
307 const char* bad_codec = NULL;
308  
309 static struct option long_opts[] = {
310 { "name", required_argument, 0, 'n' },
311 { "pattern", required_argument, 0, 'p' },
312 { "res", required_argument, 0, 'r' },
313 { "fps", required_argument, 0, 'f' },
314 { "tone", required_argument, 0, 't' },
315 { "codec", required_argument, 0, 'C' },
316 { "h264", no_argument, 0, 1002 },
317 { "web", optional_argument, 0, 'W' },
318 { "rtsp", optional_argument, 0, 'R' },
319 { "moving", no_argument, 0, 'm' },
320 { "no-moving", no_argument, 0, 1001 },
321 { "groups", required_argument, 0, 'g' },
322 { "count", required_argument, 0, 'c' },
323 { "quiet", no_argument, 0, 'q' },
324 { "help", no_argument, 0, 'h' },
325 { 0, 0, 0, 0 }
326 };
327  
328 int opt;
329 while ((opt = getopt_long(argc, argv, "n:p:r:f:t:C:W::R::mg:c:qh", long_opts, NULL)) != -1) {
330 switch (opt) {
331 case 'n':
332 strncpy(stream_name, optarg, sizeof(stream_name) - 1);
333 break;
334 case 'p':
335 if (strcmp(optarg, "smpte") == 0) { pat_type = PATTERN_SMPTE_BARS; bad_pattern = NULL; }
336 else if (strcmp(optarg, "bars100") == 0) { pat_type = PATTERN_COLOR_BARS_100; bad_pattern = NULL; }
337 else if (strcmp(optarg, "grid") == 0) { pat_type = PATTERN_GRID; bad_pattern = NULL; }
338 else if (strcmp(optarg, "ramp") == 0) { pat_type = PATTERN_RAMP; bad_pattern = NULL; }
339 else if (strcmp(optarg, "white") == 0) { pat_type = PATTERN_SOLID_WHITE; bad_pattern = NULL; }
340 else if (strcmp(optarg, "black") == 0) { pat_type = PATTERN_SOLID_BLACK; bad_pattern = NULL; }
341 else if (strcmp(optarg, "gray") == 0) { pat_type = PATTERN_SOLID_GRAY; bad_pattern = NULL; }
342 else {
343 bad_pattern = optarg;
344 }
345 break;
346 case 'r':
347 if (strcmp(optarg, "1080p") == 0) { width = 1920; height = 1080; bad_res = NULL; }
348 else if (strcmp(optarg, "720p") == 0) { width = 1280; height = 720; bad_res = NULL; }
349 else if (strcmp(optarg, "4k") == 0) { width = 3840; height = 2160; bad_res = NULL; }
350 else if (sscanf(optarg, "%dx%d", &width, &height) == 2 && width > 0 && height > 0) {
351 bad_res = NULL;
352 } else {
353 bad_res = optarg;
354 width = 1920; height = 1080;
355 }
356 break;
357 case 'f':
358 if (strcmp(optarg, "60") == 0) { fps_n = 60000; fps_d = 1000; }
359 else if (strcmp(optarg, "59.94") == 0) { fps_n = 60000; fps_d = 1001; }
360 else if (strcmp(optarg, "50") == 0) { fps_n = 50000; fps_d = 1000; }
361 else if (strcmp(optarg, "30") == 0) { fps_n = 30000; fps_d = 1000; }
362 else if (strcmp(optarg, "29.97") == 0) { fps_n = 30000; fps_d = 1001; }
363 else if (strcmp(optarg, "25") == 0) { fps_n = 25000; fps_d = 1000; }
364 else if (strcmp(optarg, "24") == 0) { fps_n = 24000; fps_d = 1000; }
365 else {
366 double f = atof(optarg);
367 if (f > 1.0) { fps_n = (int)(f * 1000.0 + 0.5); fps_d = 1000; }
368 }
369 break;
370 case 't':
371 if (strcmp(optarg, "1000") == 0) tone_type = TONE_1000HZ;
372 else if (strcmp(optarg, "440") == 0) tone_type = TONE_440HZ;
373 else if (strcmp(optarg, "beep") == 0) tone_type = TONE_BEEP;
374 else if (strcmp(optarg, "none") == 0) tone_type = TONE_NONE;
375 break;
376 case 'C':
377 if (strcasecmp(optarg, "h264") == 0 || strcasecmp(optarg, "avc") == 0) {
378 codec = CODEC_H264;
379 bad_codec = NULL;
380 } else if (strcasecmp(optarg, "uyvy") == 0 || strcasecmp(optarg, "raw") == 0) {
381 codec = CODEC_UYVY;
382 bad_codec = NULL;
383 } else {
384 bad_codec = optarg;
385 codec = CODEC_UYVY;
386 }
387 codec_explicitly_set = true;
388 break;
389 case 1002:
390 codec = CODEC_H264;
391 bad_codec = NULL;
392 codec_explicitly_set = true;
393 break;
394 case 'W': {
395 enable_web = true;
396 if (!optarg && optind < argc && argv[optind][0] != '-') {
397 optarg = argv[optind++];
398 }
399 if (optarg) {
400 int p = atoi(optarg);
401 if (p > 0 && p <= 65535) web_port = (uint16_t)p;
402 }
403 break;
404 }
405 case 'R': {
406 enable_rtsp = true;
407 if (!optarg && optind < argc && argv[optind][0] != '-') {
408 optarg = argv[optind++];
409 }
410 if (optarg) {
411 int p = atoi(optarg);
412 if (p > 0 && p <= 65535) rtsp_port = (uint16_t)p;
413 }
414 break;
415 }
416 case 'm':
417 moving_bar = true;
418 break;
419 case 1001:
420 moving_bar = false;
421 break;
422 case 'g':
423 strncpy(groups, optarg, sizeof(groups) - 1);
424 break;
425 case 'c':
426 frame_limit = atoi(optarg);
427 break;
428 case 'q':
429 quiet = true;
430 break;
431 case 'h':
432 print_usage(argv[0]);
433 return 0;
434 default:
435 print_usage(argv[0]);
436 return 1;
437 }
438 }
439  
440 openavx_cli_set_quiet(quiet);
441  
442 if (bad_pattern) oavx_log_write(OAVX_LOG_WARN, "pattern", "Unknown pattern '%s', defaulting to 'smpte'", bad_pattern);
443 if (bad_res) oavx_log_write(OAVX_LOG_WARN, "pattern", "Invalid resolution '%s', defaulting to 1920x1080", bad_res);
444 if (bad_codec) oavx_log_write(OAVX_LOG_WARN, "pattern", "Unknown codec '%s', defaulting to 'uyvy'", bad_codec);
445  
446 /* Auto-resolve codec: Web preview & RTSP require H.264 bitstream */
447 if (enable_web || enable_rtsp) {
448 if (codec == CODEC_UYVY && !codec_explicitly_set) {
449 codec = CODEC_H264;
450 } else if (codec == CODEC_UYVY && codec_explicitly_set) {
451 oavx_log_write(OAVX_LOG_WARN, "pattern",
452 "Web/RTSP enabled with raw UYVY; browser preview & RTSP players require H.264 (--codec h264)");
453 }
454 }
455  
456 if (codec == CODEC_H264) {
457 if (width != 1920 || height != 1080) {
458 oavx_log_write(OAVX_LOG_INFO, "pattern",
459 "Embedded H.264 bitstream is fixed at 1920x1080; overriding resolution");
460 width = 1920;
461 height = 1080;
462 }
463 }
464  
465 /* Enforce even width for UYVY macropixels */
466 if (width % 2 != 0) width++;
467  
468 signal(SIGINT, sigint_handler);
469 signal(SIGTERM, sigint_handler);
470  
471 oavx_context_t *ctx = oavx_context_create();
472 if (!ctx) {
473 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to initialize OpenAVX context engine");
474 return 1;
475 }
476  
477 oavx_stream_config_t send_settings = {
478 .name = stream_name,
479 .groups = groups,
480 .direction = OAVX_DIR_TRANSMIT,
481 .clock_sync = true
482 };
483  
484 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_settings);
485 if (!p_send) {
486 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to create broadcast sender '%s'", stream_name);
487 oavx_context_destroy(ctx);
488 return 1;
489 }
490  
491 /* Activate Web Preview and RTSP servers if requested */
492 char web_url[256] = "";
493 if (enable_web) {
494 if (oavx_stream_enable_web_preview(p_send, web_port) == OAVX_SUCCESS) {
495 web_port = oavx_stream_get_web_preview_port(p_send);
496 oavx_stream_get_web_preview_url(p_send, web_url, sizeof(web_url));
497 } else {
498 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to start HTTP Web Preview server on port %u", web_port);
499 enable_web = false;
500 }
501 }
502  
503 if (enable_rtsp) {
504 if (oavx_stream_enable_rtsp_server(p_send, rtsp_port, rtsp_path) == OAVX_SUCCESS) {
505 rtsp_port = oavx_stream_get_rtsp_port(p_send);
506 } else {
507 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to start RTSP 1.0 server on port %u", rtsp_port);
508 enable_rtsp = false;
509 }
510 }
511  
512 openavx_cli_header("pattern");
513 openavx_cli_kv("stream", "%s", stream_name);
514 openavx_cli_kv("codec", "%s", codec == CODEC_H264 ? "H.264 / AVC (SMPTE 75% Annex-B)" : "UYVY 4:2:2 (raw)");
515 openavx_cli_kv("resolution", "%dx%d", width, height);
516 openavx_cli_kv("framerate", "%.2f fps (%d/%d)", (double)fps_n / fps_d, fps_n, fps_d);
517 openavx_cli_kv("groups", "%s", groups);
518 openavx_cli_kv("audio", "%s",
519 tone_type == TONE_1000HZ ? "1 kHz sine @ -20 dBFS" :
520 tone_type == TONE_440HZ ? "440 Hz sine @ -20 dBFS" :
521 tone_type == TONE_BEEP ? "1s A/V sync beep" : "none");
522 openavx_cli_kv("motion", "%s", codec == CODEC_H264 ? "embedded SMPTE IDR" : (moving_bar ? "moving ticker" : "static"));
523 if (enable_web) {
524 openavx_cli_kv("web", "%s", web_url[0] ? web_url : "active");
525 }
526 if (enable_rtsp) {
527 openavx_cli_kv("rtsp", "rtsp://127.0.0.1:%u/%s", rtsp_port, rtsp_path);
528 }
529 oavx_log_write(OAVX_LOG_INFO, "pattern", "Broadcasting stream (Ctrl+C to stop)");
530  
531 /* Allocate video buffers if UYVY raw mode is active */
532 uint8_t* base_frame = NULL;
533 uint8_t* render_frame = NULL;
534 if (codec == CODEC_UYVY) {
535 size_t frame_bytes = (size_t)width * height * 2;
536 base_frame = (uint8_t*)malloc(frame_bytes);
537 render_frame = (uint8_t*)malloc(frame_bytes);
538 if (!base_frame || !render_frame) {
539 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to allocate video frame memory");
540 free(base_frame);
541 free(render_frame);
542 if (enable_web) oavx_stream_disable_web_preview(p_send);
543 if (enable_rtsp) oavx_stream_disable_rtsp_server(p_send);
544 oavx_stream_close(p_send);
545 oavx_context_destroy(ctx);
546 return 1;
547 }
548  
549 render_base_pattern(base_frame, width, height, pat_type);
550 memcpy(render_frame, base_frame, frame_bytes);
551 }
552  
553 /* Allocate audio buffers (48 kHz stereo) */
554 const int max_samples_per_frame = 48000 / 24 + 128;
555 float* audio_left = (float*)malloc(max_samples_per_frame * sizeof(float));
556 float* audio_right = (float*)malloc(max_samples_per_frame * sizeof(float));
557 float* audio_planar = (float*)malloc(2 * max_samples_per_frame * sizeof(float));
558 if (!audio_left || !audio_right || !audio_planar) {
559 oavx_log_write(OAVX_LOG_ERROR, "pattern", "Failed to allocate audio buffers");
560 free(base_frame); free(render_frame);
561 free(audio_left); free(audio_right); free(audio_planar);
562 if (enable_web) oavx_stream_disable_web_preview(p_send);
563 if (enable_rtsp) oavx_stream_disable_rtsp_server(p_send);
564 oavx_stream_close(p_send);
565 oavx_context_destroy(ctx);
566 return 1;
567 }
568  
569 int64_t audio_sample_counter = 0;
570 int64_t frame_index = 0;
571 int bar_pos_x = 0;
572 int bar_width = width / 20;
573 if (bar_width < 16) bar_width = 16;
574 if (bar_width % 2 != 0) bar_width++;
575  
576 uint64_t frame_duration_ns = (uint64_t)((double)fps_d * 1000000000.0 / (double)fps_n);
577 int64_t start_time_ns = avx_time_ns();
578  
579 while (g_running) {
580 if (frame_limit > 0 && frame_index >= frame_limit) {
581 break;
582 }
583  
584 /* Update and send video frame */
585 int64_t frame_pts_ns = (int64_t)(frame_index * (1000000000LL * fps_d / fps_n));
586  
587 if (codec == CODEC_H264) {
588 oavx_video_frame_t vf = {
589 .width = width,
590 .height = height,
591 .fourcc = OAVX_FOURCC_VIDEO_TYPE_H264,
592 .scan_type = OAVX_SCAN_PROGRESSIVE,
593 .planes = { (uint8_t*)g_smpte_h264_1080p, NULL, NULL, NULL },
594 .strides = { g_smpte_h264_1080p_len, 0, 0, 0 },
595 .framerate = { (uint32_t)fps_n, (uint32_t)fps_d },
596 .aspect_ratio = { (uint32_t)width, (uint32_t)height },
597 .pts_ns = frame_pts_ns,
598 .dts_ns = 0,
599 .metadata = NULL,
600 .metadata_len = 0,
601 .user_ctx = NULL
602 };
603 oavx_stream_send_video(p_send, &vf);
604 } else {
605 if (moving_bar) {
606 overlay_moving_ticker(render_frame, base_frame, width, height, bar_pos_x, bar_width);
607 bar_pos_x = (bar_pos_x + (width / 240) + 4) % (width - bar_width);
608 }
609  
610 oavx_video_frame_t vf = {
611 .width = width,
612 .height = height,
613 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
614 .scan_type = OAVX_SCAN_PROGRESSIVE,
615 .planes = { render_frame, NULL, NULL, NULL },
616 .strides = { (size_t)width * 2, 0, 0, 0 },
617 .framerate = { (uint32_t)fps_n, (uint32_t)fps_d },
618 .aspect_ratio = { (uint32_t)width, (uint32_t)height },
619 .pts_ns = frame_pts_ns,
620 .dts_ns = 0,
621 .metadata = NULL,
622 .metadata_len = 0,
623 .user_ctx = NULL
624 };
625 oavx_stream_send_video(p_send, &vf);
626 }
627  
628 /* Calculate exact audio samples needed for this frame at 48 kHz */
629 int audio_samples = (int)((double)(frame_index + 1) * 48000.0 * fps_d / fps_n) -
630 (int)((double)frame_index * 48000.0 * fps_d / fps_n);
631  
632 if (tone_type != TONE_NONE && audio_samples > 0) {
633 generate_audio_frame(audio_left, audio_right, (uint32_t)audio_samples, tone_type, &audio_sample_counter);
634  
635 /* Pack planar audio (Ch0 followed by Ch1) */
636 memcpy(audio_planar, audio_left, (size_t)audio_samples * sizeof(float));
637 memcpy(audio_planar + audio_samples, audio_right, (size_t)audio_samples * sizeof(float));
638  
639 oavx_audio_frame_t af = {
640 .sample_rate = 48000,
641 .channels = 2,
642 .frames = (uint32_t)audio_samples,
643 .format = OAVX_AUDIO_FMT_FLT_PLANAR,
644 .planes = { audio_left, audio_right, NULL, NULL, NULL, NULL, NULL, NULL },
645 .stride_bytes = (size_t)audio_samples * sizeof(float),
646 .pts_ns = frame_pts_ns,
647 .metadata = NULL,
648 .metadata_len = 0,
649 .user_ctx = NULL
650 };
651 oavx_stream_send_audio(p_send, &af);
652 }
653  
654 frame_index++;
655  
656 if (oavx_get_log_level() >= OAVX_LOG_INFO && (frame_index % (fps_n / fps_d) == 0)) {
657 oavx_tally_t tally = {0};
658 oavx_stream_get_tally(p_send, &tally, 0);
659 int conns = oavx_stream_get_connection_count(p_send, 0);
660 int rtsp_c = enable_rtsp ? oavx_stream_get_rtsp_client_count(p_send) : 0;
661 int web_c = enable_web ? oavx_stream_get_web_client_count(p_send) : 0;
662 if (enable_web || enable_rtsp) {
663 openavx_cli_status("pattern", "frames=%ld (%.1fs) clients=[avx:%d rtsp:%d web:%d] tally=[PGM:%s PVW:%s]",
664 (long)frame_index, (double)frame_index * fps_d / fps_n,
665 conns, rtsp_c, web_c,
666 (tally.flags & OAVX_TALLY_PROGRAM) ? "YES" : "NO",
667 (tally.flags & OAVX_TALLY_PREVIEW) ? "YES" : "NO");
668 } else {
669 openavx_cli_status("pattern", "frames=%ld (%.1fs) receivers=%d tally=[PGM:%s PVW:%s]",
670 (long)frame_index, (double)frame_index * fps_d / fps_n, conns,
671 (tally.flags & OAVX_TALLY_PROGRAM) ? "YES" : "NO",
672 (tally.flags & OAVX_TALLY_PREVIEW) ? "YES" : "NO");
673 }
674 }
675  
676 /* Accurate frame rate pacing */
677 int64_t now_ns = avx_time_ns();
678 uint64_t target_ns = (uint64_t)frame_index * frame_duration_ns;
679 uint64_t elapsed_ns = (uint64_t)(now_ns - start_time_ns);
680  
681 if (target_ns > elapsed_ns) {
682 avx_sleep_ns(target_ns - elapsed_ns);
683 }
684 }
685  
686 openavx_cli_status_end();
687 oavx_log_write(OAVX_LOG_INFO, "pattern", "Shutting down signal generator...");
688  
689 if (enable_web) {
690 oavx_stream_disable_web_preview(p_send);
691 }
692 if (enable_rtsp) {
693 oavx_stream_disable_rtsp_server(p_send);
694 }
695  
696 free(base_frame);
697 free(render_frame);
698 free(audio_left);
699 free(audio_right);
700 free(audio_planar);
701  
702 oavx_stream_close(p_send);
703 oavx_context_destroy(ctx);
704  
705 oavx_log_write(OAVX_LOG_INFO, "pattern", "Transmitted %ld frames total; generator stopped", (long)frame_index);
706 return 0;
707 }