+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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 | * test/openavx_video_decoder_test.c - Compressed Video Decode, Priming, Color Conversion & Native Bitstream Tests |
| 7 | */ |
| 8 | |
| 9 | #define _DEFAULT_SOURCE |
| 10 | #define _POSIX_C_SOURCE 200809L |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "openavx.h" |
| 17 | #include "avx_platform.h" |
| 18 | #include "avx_video_codec.h" |
| 19 | |
| 20 | #define TEST_CHECK(cond) do { \ |
| 21 | if (!(cond)) { \ |
| 22 | fprintf(stderr, "CHECK FAILED at %s:%d: %s\n", __FILE__, __LINE__, #cond); \ |
| 23 | abort(); \ |
| 24 | } \ |
| 25 | } while (0) |
| 26 | |
| 27 | /* Valid 128x128 H.264 Baseline Annex-B bitstream: |
| 28 | * - Frame 0: SPS (0..26) + PPS (26..35) + IDR slices (35..132) |
| 29 | * - Frame 1: P-frame slices (132..154) |
| 30 | */ |
| 31 | static const uint8_t k_h264_f0[132] = { |
| 32 | 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xC0, 0x0B, 0xD9, 0x02, 0x04, 0x6C, |
| 33 | 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0F, 0x23, 0xC5, |
| 34 | 0x0A, 0x92, 0x00, 0x00, 0x00, 0x01, 0x68, 0xCB, 0x83, 0xCB, 0x20, 0x00, |
| 35 | 0x00, 0x01, 0x65, 0x88, 0x84, 0x04, 0xAF, 0x11, 0x8A, 0x00, 0x02, 0x71, |
| 36 | 0x31, 0xC0, 0x00, 0x43, 0x5E, 0x38, 0x00, 0x09, 0x46, 0xC9, 0xC9, 0xC9, |
| 37 | 0xC9, 0xC9, 0xC9, 0xC9, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, |
| 38 | 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xE0, 0x00, |
| 39 | 0x00, 0x01, 0x65, 0x04, 0x22, 0x21, 0x01, 0x2B, 0xC4, 0x62, 0x80, 0x00, |
| 40 | 0x9C, 0x4C, 0x70, 0x00, 0x10, 0xD7, 0x8E, 0x00, 0x02, 0x51, 0xB2, 0x72, |
| 41 | 0x72, 0x72, 0x72, 0x72, 0x72, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, |
| 42 | 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x75, 0xD7, 0x5D, 0x78 |
| 43 | }; |
| 44 | static const size_t k_h264_sps_pps_len = 35; |
| 45 | |
| 46 | static const uint8_t k_h264_f1[22] = { |
| 47 | 0x00, 0x00, 0x00, 0x01, 0x41, 0x9A, 0x38, 0x09, 0xDE, 0x08, 0x60, 0x00, |
| 48 | 0x00, 0x01, 0x41, 0x04, 0x26, 0x8E, 0x02, 0x77, 0x82, 0x18 |
| 49 | }; |
| 50 | |
| 51 | /* Valid 128x128 HEVC Main Annex-B bitstream: |
| 52 | * - Frame 0: VPS + SPS + PPS + IDR (0..111) |
| 53 | * - Frame 1: TRAIL_R (111..127) |
| 54 | */ |
| 55 | static const uint8_t k_hevc_f0[111] = { |
| 56 | 0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0C, 0x01, 0xFF, 0xFF, 0x01, 0x60, |
| 57 | 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, |
| 58 | 0x1E, 0x92, 0x80, 0x90, 0x00, 0x00, 0x00, 0x01, 0x42, 0x01, 0x01, 0x01, |
| 59 | 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, |
| 60 | 0x00, 0x1E, 0xA0, 0x10, 0x20, 0x20, 0x59, 0x64, 0xA9, 0x24, 0xCA, 0xF0, |
| 61 | 0x16, 0x80, 0x80, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x0F, 0x04, |
| 62 | 0x00, 0x00, 0x00, 0x01, 0x44, 0x01, 0xC1, 0x72, 0xB4, 0x22, 0x40, 0x00, |
| 63 | 0x00, 0x01, 0x28, 0x01, 0xAF, 0x08, 0x60, 0xF9, 0xEE, 0xAD, 0x5F, 0xFD, |
| 64 | 0x24, 0x5F, 0x89, 0x27, 0xED, 0x10, 0xBF, 0x9F, 0xCA, 0xEA, 0x4D, 0x9B, |
| 65 | 0x60, 0x25, 0x20 |
| 66 | }; |
| 67 | |
| 68 | static const uint8_t k_hevc_f1[16] = { |
| 69 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0xD0, 0x09, 0x7E, 0x10, 0xC6, 0x10, |
| 70 | 0xC0, 0xFA, 0xA1, 0x28 |
| 71 | }; |
| 72 | |
| 73 | /* Valid 128x128 AV1 OBU bitstream: |
| 74 | * - Frame 0: Sequence Header OBU + Frame Header OBU + Tile Group (0..40) |
| 75 | * - Frame 1: Frame Header OBU + Tile Group (40..62) |
| 76 | */ |
| 77 | static const uint8_t k_av1_f0[40] = { |
| 78 | 0x12, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x03, 0x37, 0xFF, 0xE3, 0x57, |
| 79 | 0xCC, 0x02, 0x32, 0x18, 0x10, 0x00, 0x8C, 0x20, 0x81, 0x45, 0x08, 0x20, |
| 80 | 0x40, 0x00, 0x02, 0x00, 0x95, 0xCE, 0x9B, 0x15, 0xDB, 0x1A, 0x45, 0x90, |
| 81 | 0x10, 0x72, 0x83, 0x8F |
| 82 | }; |
| 83 | |
| 84 | static const uint8_t k_av1_f1[22] = { |
| 85 | 0x12, 0x00, 0x32, 0x12, 0x30, 0x02, 0x04, 0x09, 0x24, 0x92, 0x3A, 0x46, |
| 86 | 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x95, 0x9F, 0x80 |
| 87 | }; |
| 88 | |
| 89 | /* Valid 128x128 10-Bit AV1 OBU bitstream (yuv420p10le): |
| 90 | * - Frame 0: Sequence Header OBU (high 10-bit profile) + Frame Header + Tile Group (43 bytes) |
| 91 | * - Frame 1: Frame Header OBU + Tile Group (22 bytes) |
| 92 | */ |
| 93 | static const uint8_t k_av1_10bit_f0[43] = { |
| 94 | 0x12, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x03, 0x37, 0xFF, 0xE3, 0x57, |
| 95 | 0xCE, 0x02, 0x32, 0x1B, 0x10, 0x00, 0x8C, 0x20, 0x82, 0x08, 0x10, 0x40, |
| 96 | 0x40, 0x3C, 0x02, 0x00, 0x95, 0xCE, 0x9E, 0x61, 0xFB, 0x1B, 0x5B, 0x9B, |
| 97 | 0x63, 0x7D, 0xD1, 0x74, 0xC0, 0x31, 0xC0 |
| 98 | }; |
| 99 | |
| 100 | static const uint8_t k_av1_10bit_f1[22] = { |
| 101 | 0x12, 0x00, 0x32, 0x12, 0x30, 0x02, 0x04, 0x09, 0x24, 0x92, 0x3A, 0x46, |
| 102 | 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x9D, 0x9D, 0xC0 |
| 103 | }; |
| 104 | |
| 105 | /* ========================================================================= */ |
| 106 | /* Deterministic Test Decoder (Tests Transport -> Decode -> Color Pipeline) */ |
| 107 | /* ========================================================================= */ |
| 108 | |
| 109 | typedef struct { |
| 110 | uint32_t w; |
| 111 | uint32_t h; |
| 112 | int calls; |
| 113 | uint8_t *nv12; |
| 114 | } mock_hx_ctx_t; |
| 115 | |
| 116 | static volatile int g_mock_calls = 0; |
| 117 | |
| 118 | static bool mock_hx_supports(uint32_t fourcc) { |
| 119 | return fourcc == OAVX_FOURCC_VIDEO_TYPE_H264; |
| 120 | } |
| 121 | |
| 122 | static int mock_hx_create(const avx_video_dec_cfg_t *cfg, void **out_ctx) { |
| 123 | mock_hx_ctx_t *m = (mock_hx_ctx_t *)calloc(1, sizeof(*m)); |
| 124 | if (!m) return -1; |
| 125 | m->w = (cfg && cfg->width) ? cfg->width : 128; |
| 126 | m->h = (cfg && cfg->height) ? cfg->height : 128; |
| 127 | size_t sz = (size_t)m->w * m->h + (size_t)m->w * ((m->h + 1) / 2); |
| 128 | m->nv12 = (uint8_t *)malloc(sz); |
| 129 | if (!m->nv12) { |
| 130 | free(m); |
| 131 | return -1; |
| 132 | } |
| 133 | *out_ctx = m; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int mock_hx_decode(void *vctx, const uint8_t *pkt, size_t len, oavx_video_frame_t *out) { |
| 138 | mock_hx_ctx_t *m = (mock_hx_ctx_t *)vctx; |
| 139 | (void)pkt; |
| 140 | if (!m || len == 0 || !out) return AVX_VIDEO_CODEC_ERROR; |
| 141 | m->calls++; |
| 142 | g_mock_calls++; |
| 143 | if (m->calls == 1) { |
| 144 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 145 | } |
| 146 | |
| 147 | size_t y_sz = (size_t)m->w * m->h; |
| 148 | size_t uv_sz = (size_t)m->w * ((m->h + 1) / 2); |
| 149 | memset(m->nv12, 145, y_sz); |
| 150 | for (size_t i = 0; i + 1 < uv_sz; i += 2) { |
| 151 | m->nv12[y_sz + i + 0] = 54; |
| 152 | m->nv12[y_sz + i + 1] = 34; |
| 153 | } |
| 154 | out->fourcc = OAVX_FOURCC_VIDEO_TYPE_NV12; |
| 155 | out->width = m->w; |
| 156 | out->height = m->h; |
| 157 | out->planes[0] = m->nv12; |
| 158 | out->strides[0] = m->w; |
| 159 | out->planes[1] = m->nv12 + y_sz; |
| 160 | out->strides[1] = m->w; |
| 161 | return AVX_VIDEO_CODEC_OK; |
| 162 | } |
| 163 | |
| 164 | static void mock_hx_reset(void *vctx) { |
| 165 | mock_hx_ctx_t *m = (mock_hx_ctx_t *)vctx; |
| 166 | if (m) m->calls = 0; |
| 167 | } |
| 168 | |
| 169 | static void mock_hx_destroy(void *vctx) { |
| 170 | mock_hx_ctx_t *m = (mock_hx_ctx_t *)vctx; |
| 171 | if (m) { |
| 172 | free(m->nv12); |
| 173 | free(m); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static const avx_video_decoder_ops_t g_mock_hx_ops = { |
| 178 | .name = "mock-compressed-pipeline", |
| 179 | .priority = 250, |
| 180 | .is_hardware = false, |
| 181 | .supports_fourcc = mock_hx_supports, |
| 182 | .create = mock_hx_create, |
| 183 | .decode = mock_hx_decode, |
| 184 | .reset = mock_hx_reset, |
| 185 | .destroy = mock_hx_destroy |
| 186 | }; |
| 187 | |
| 188 | static oavx_frame_type_t send_and_recv_video( |
| 189 | oavx_stream_t *tx, |
| 190 | oavx_stream_t *rx, |
| 191 | const uint8_t *pkt, |
| 192 | size_t pkt_len, |
| 193 | int64_t pts_ns, |
| 194 | oavx_video_frame_t *out_vf) |
| 195 | { |
| 196 | oavx_video_frame_t tx_v = { |
| 197 | .width = 128, |
| 198 | .height = 128, |
| 199 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_H264, |
| 200 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 201 | .planes = { (uint8_t *)(uintptr_t)pkt, NULL, NULL, NULL }, |
| 202 | .strides = { pkt_len, 0, 0, 0 }, |
| 203 | .framerate = { .num = 30000, .den = 1000 }, |
| 204 | .aspect_ratio = { .num = 1, .den = 1 }, |
| 205 | .pts_ns = pts_ns |
| 206 | }; |
| 207 | TEST_CHECK(oavx_stream_send_video(tx, &tx_v) == OAVX_SUCCESS); |
| 208 | |
| 209 | for (int attempt = 0; attempt < 20; ++attempt) { |
| 210 | oavx_frame_type_t ft = oavx_stream_receive(rx, out_vf, NULL, NULL, 30); |
| 211 | if (ft != OAVX_FRAME_TYPE_NONE) { |
| 212 | return ft; |
| 213 | } |
| 214 | } |
| 215 | return OAVX_FRAME_TYPE_NONE; |
| 216 | } |
| 217 | |
| 218 | static void test_transport_hx_decode_and_color_convert(oavx_context_t *ctx, |
| 219 | oavx_transport_e mode, |
| 220 | oavx_color_format_e color_fmt, |
| 221 | oavx_fourcc_video_type_e expect_fourcc) |
| 222 | { |
| 223 | printf("[*] Testing compressed video decode pipeline (transport=%d, color_fmt=%d)...\n", (int)mode, (int)color_fmt); |
| 224 | |
| 225 | oavx_stream_config_t tx_cfg = { |
| 226 | .name = "COMPRESSED_TX_TEST", |
| 227 | .direction = OAVX_DIR_TRANSMIT, |
| 228 | .transport = mode, |
| 229 | .groups = "Public" |
| 230 | }; |
| 231 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 232 | TEST_CHECK(tx != NULL); |
| 233 | |
| 234 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 235 | TEST_CHECK(ep && ep->address); |
| 236 | |
| 237 | oavx_stream_config_t rx_cfg = { |
| 238 | .name = "COMPRESSED_RX_TEST", |
| 239 | .endpoint = ep->address, |
| 240 | .direction = OAVX_DIR_RECEIVE, |
| 241 | .transport = mode, |
| 242 | .color_format = color_fmt, |
| 243 | .groups = "Public" |
| 244 | }; |
| 245 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 246 | TEST_CHECK(rx != NULL); |
| 247 | |
| 248 | avx_sleep_ms(80); |
| 249 | int base_calls = g_mock_calls; |
| 250 | |
| 251 | /* Packet 1: returns NEED_MORE_DATA unless caller asked for COMPRESSED_V1 */ |
| 252 | oavx_video_frame_t vf1 = {0}; |
| 253 | oavx_frame_type_t ft1 = send_and_recv_video(tx, rx, k_h264_f0, sizeof(k_h264_f0), 1000, &vf1); |
| 254 | if (color_fmt == OAVX_COLOR_FORMAT_COMPRESSED_V1) { |
| 255 | if (ft1 != OAVX_FRAME_TYPE_VIDEO) { |
| 256 | fprintf(stderr, "FAIL: ft1=%d (expected VIDEO) calls=%d\n", (int)ft1, (int)g_mock_calls); |
| 257 | } |
| 258 | TEST_CHECK(ft1 == OAVX_FRAME_TYPE_VIDEO); |
| 259 | TEST_CHECK(vf1.fourcc == OAVX_FOURCC_VIDEO_TYPE_H264); |
| 260 | TEST_CHECK(vf1.strides[0] == sizeof(k_h264_f0)); |
| 261 | TEST_CHECK(memcmp(vf1.planes[0], k_h264_f0, sizeof(k_h264_f0)) == 0); |
| 262 | TEST_CHECK(g_mock_calls == base_calls); /* Decoder never invoked */ |
| 263 | } else { |
| 264 | /* Priming packet consumed silently — NOT leaked as compressed bytes */ |
| 265 | if (ft1 != OAVX_FRAME_TYPE_NONE || g_mock_calls != base_calls + 1) { |
| 266 | fprintf(stderr, "FAIL: ft1=%d fourcc=0x%08X calls=%d base=%d\n", |
| 267 | (int)ft1, (unsigned)vf1.fourcc, (int)g_mock_calls, base_calls); |
| 268 | } |
| 269 | TEST_CHECK(ft1 == OAVX_FRAME_TYPE_NONE); |
| 270 | TEST_CHECK(g_mock_calls == base_calls + 1); |
| 271 | |
| 272 | oavx_video_frame_t vf2 = {0}; |
| 273 | oavx_frame_type_t ft2 = send_and_recv_video(tx, rx, k_h264_f1, sizeof(k_h264_f1), 2000, &vf2); |
| 274 | TEST_CHECK(ft2 == OAVX_FRAME_TYPE_VIDEO); |
| 275 | TEST_CHECK(vf2.width == 128 && vf2.height == 128); |
| 276 | TEST_CHECK(vf2.fourcc == expect_fourcc); |
| 277 | TEST_CHECK(vf2.planes[0] != NULL); |
| 278 | if (expect_fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA || |
| 279 | expect_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA) { |
| 280 | TEST_CHECK(vf2.strides[0] == 128U * 4U); |
| 281 | /* NV12 Y=145,U=54,V=34 -> green dominant, alpha 255 */ |
| 282 | TEST_CHECK(vf2.planes[0][1] > vf2.planes[0][0]); |
| 283 | TEST_CHECK(vf2.planes[0][3] == 255); |
| 284 | } else if (expect_fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) { |
| 285 | TEST_CHECK(vf2.strides[0] == 128U * 2U); |
| 286 | TEST_CHECK(vf2.planes[0][0] == 54 && vf2.planes[0][1] == 145 && vf2.planes[0][2] == 34); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | oavx_stream_close(rx); |
| 291 | oavx_stream_close(tx); |
| 292 | } |
| 293 | |
| 294 | static void exercise_native_decoder(uint32_t fourcc, const char *label, |
| 295 | const uint8_t *priming_pkt, size_t priming_len, |
| 296 | const uint8_t *pkt0, size_t len0, |
| 297 | const uint8_t *pkt1, size_t len1) |
| 298 | { |
| 299 | if (!avx_video_decoder_has_backend(fourcc)) { |
| 300 | printf(" [SKIP] No native %s decoder backend on this host\n", label); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | avx_video_dec_cfg_t cfg = { .fourcc = fourcc, .width = 128, .height = 128 }; |
| 305 | avx_video_decoder_t *dec = avx_video_decoder_create(&cfg); |
| 306 | TEST_CHECK(dec != NULL); |
| 307 | printf("[*] Testing real %s bitstream decode on backend '%s'...\n", |
| 308 | label, avx_video_decoder_get_backend_name(dec)); |
| 309 | |
| 310 | oavx_video_frame_t out = {0}; |
| 311 | if (priming_pkt && priming_len > 0) { |
| 312 | int rcp = avx_video_decoder_decode(dec, priming_pkt, priming_len, &out); |
| 313 | TEST_CHECK(rcp == AVX_VIDEO_CODEC_NEED_MORE_DATA || rcp == AVX_VIDEO_CODEC_OK); |
| 314 | } |
| 315 | |
| 316 | int got_frame = 0; |
| 317 | const uint8_t *pkts[4] = { pkt0, pkt1, pkt0, pkt1 }; |
| 318 | size_t lens[4] = { len0, len1, len0, len1 }; |
| 319 | for (int i = 0; i < 4; ++i) { |
| 320 | int rc = avx_video_decoder_decode(dec, pkts[i], lens[i], &out); |
| 321 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK || rc == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 322 | if (rc == AVX_VIDEO_CODEC_OK) { |
| 323 | TEST_CHECK(out.width == 128 && out.height == 128); |
| 324 | TEST_CHECK(out.fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12 || out.fourcc == OAVX_FOURCC_VIDEO_TYPE_I420); |
| 325 | TEST_CHECK(out.planes[0] != NULL && out.strides[0] >= 128); |
| 326 | TEST_CHECK(out.planes[1] != NULL && out.strides[1] >= 64); |
| 327 | got_frame++; |
| 328 | } |
| 329 | } |
| 330 | TEST_CHECK(got_frame >= 1); |
| 331 | |
| 332 | avx_video_decoder_reset(dec); |
| 333 | int rcr = avx_video_decoder_decode(dec, pkt0, len0, &out); |
| 334 | TEST_CHECK(rcr == AVX_VIDEO_CODEC_OK || rcr == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 335 | if (rcr == AVX_VIDEO_CODEC_NEED_MORE_DATA) { |
| 336 | rcr = avx_video_decoder_decode(dec, pkt1, len1, &out); |
| 337 | TEST_CHECK(rcr == AVX_VIDEO_CODEC_OK || rcr == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 338 | } |
| 339 | |
| 340 | avx_video_decoder_destroy(dec); |
| 341 | printf(" [+] %s native bitstream decode passed!\n", label); |
| 342 | } |
| 343 | |
| 344 | static size_t convert_annexb_to_avcc(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_cap) { |
| 345 | size_t pos = 0; |
| 346 | size_t out_len = 0; |
| 347 | while (pos + 3 <= src_len) { |
| 348 | size_t sc_len = 0; |
| 349 | if (src[pos] == 0 && src[pos + 1] == 0) { |
| 350 | if (src[pos + 2] == 1) sc_len = 3; |
| 351 | else if (pos + 4 <= src_len && src[pos + 2] == 0 && src[pos + 3] == 1) sc_len = 4; |
| 352 | } |
| 353 | if (sc_len > 0) { |
| 354 | pos += sc_len; |
| 355 | size_t nstart = pos; |
| 356 | while (pos + 3 <= src_len) { |
| 357 | if (src[pos] == 0 && src[pos + 1] == 0 && |
| 358 | (src[pos + 2] == 1 || (pos + 4 <= src_len && src[pos + 2] == 0 && src[pos + 3] == 1))) { |
| 359 | break; |
| 360 | } |
| 361 | pos++; |
| 362 | } |
| 363 | if (pos + 3 > src_len) pos = src_len; |
| 364 | size_t nlen = pos - nstart; |
| 365 | if (nlen > 0 && out_len + 4 + nlen <= dst_cap) { |
| 366 | dst[out_len + 0] = (uint8_t)((nlen >> 24) & 0xFF); |
| 367 | dst[out_len + 1] = (uint8_t)((nlen >> 16) & 0xFF); |
| 368 | dst[out_len + 2] = (uint8_t)((nlen >> 8) & 0xFF); |
| 369 | dst[out_len + 3] = (uint8_t)(nlen & 0xFF); |
| 370 | memcpy(dst + out_len + 4, src + nstart, nlen); |
| 371 | out_len += 4 + nlen; |
| 372 | } |
| 373 | } else { |
| 374 | pos++; |
| 375 | } |
| 376 | } |
| 377 | return out_len; |
| 378 | } |
| 379 | |
| 380 | static void exercise_avcc_decoder(uint32_t fourcc, const char *label, |
| 381 | const uint8_t *annexb_f0, size_t len0, |
| 382 | const uint8_t *annexb_f1, size_t len1) |
| 383 | { |
| 384 | if (!avx_video_decoder_has_backend(fourcc)) { |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | uint8_t avcc_f0[512]; |
| 389 | uint8_t avcc_f1[512]; |
| 390 | size_t avcc_len0 = convert_annexb_to_avcc(annexb_f0, len0, avcc_f0, sizeof(avcc_f0)); |
| 391 | size_t avcc_len1 = convert_annexb_to_avcc(annexb_f1, len1, avcc_f1, sizeof(avcc_f1)); |
| 392 | TEST_CHECK(avcc_len0 > 4 && avcc_len1 > 4); |
| 393 | |
| 394 | avx_video_dec_cfg_t cfg = { .fourcc = fourcc, .width = 128, .height = 128 }; |
| 395 | avx_video_decoder_t *dec = avx_video_decoder_create(&cfg); |
| 396 | TEST_CHECK(dec != NULL); |
| 397 | printf("[*] Testing 4-byte length-prefixed AVCC/HVCC %s decode on backend '%s'...\n", |
| 398 | label, avx_video_decoder_get_backend_name(dec)); |
| 399 | |
| 400 | oavx_video_frame_t out = {0}; |
| 401 | int got_frame = 0; |
| 402 | const uint8_t *pkts[4] = { avcc_f0, avcc_f1, avcc_f0, avcc_f1 }; |
| 403 | size_t lens[4] = { avcc_len0, avcc_len1, avcc_len0, avcc_len1 }; |
| 404 | for (int i = 0; i < 4; ++i) { |
| 405 | int rc = avx_video_decoder_decode(dec, pkts[i], lens[i], &out); |
| 406 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK || rc == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 407 | if (rc == AVX_VIDEO_CODEC_OK) { |
| 408 | TEST_CHECK(out.width == 128 && out.height == 128); |
| 409 | TEST_CHECK(out.planes[0] != NULL); |
| 410 | got_frame++; |
| 411 | } |
| 412 | } |
| 413 | TEST_CHECK(got_frame >= 1); |
| 414 | avx_video_decoder_destroy(dec); |
| 415 | printf(" [+] %s AVCC/HVCC bitstream decode passed!\n", label); |
| 416 | } |
| 417 | |
| 418 | static void exercise_10bit_decoder(void) { |
| 419 | if (!avx_video_decoder_has_backend(OAVX_FOURCC_VIDEO_TYPE_AV01)) { |
| 420 | printf(" [SKIP] No native AV1 decoder backend on this host\n"); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | avx_video_dec_cfg_t cfg = { .fourcc = OAVX_FOURCC_VIDEO_TYPE_AV01, .width = 128, .height = 128 }; |
| 425 | avx_video_decoder_t *dec = avx_video_decoder_create(&cfg); |
| 426 | TEST_CHECK(dec != NULL); |
| 427 | printf("[*] Testing 10-bit AV1 bitstream decode to native P216 on backend '%s'...\n", |
| 428 | avx_video_decoder_get_backend_name(dec)); |
| 429 | |
| 430 | oavx_video_frame_t out = {0}; |
| 431 | int rc0 = avx_video_decoder_decode(dec, k_av1_10bit_f0, sizeof(k_av1_10bit_f0), &out); |
| 432 | TEST_CHECK(rc0 == AVX_VIDEO_CODEC_OK || rc0 == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 433 | |
| 434 | if (rc0 == AVX_VIDEO_CODEC_OK) { |
| 435 | TEST_CHECK(out.width == 128 && out.height == 128); |
| 436 | TEST_CHECK(out.fourcc == OAVX_FOURCC_VIDEO_TYPE_P216); |
| 437 | TEST_CHECK(out.planes[0] != NULL && out.strides[0] == 128U * 2U); |
| 438 | TEST_CHECK(out.planes[1] != NULL && out.strides[1] == 128U * 2U); |
| 439 | const uint16_t *py = (const uint16_t *)(const void *)out.planes[0]; |
| 440 | const uint16_t *puv = (const uint16_t *)(const void *)out.planes[1]; |
| 441 | TEST_CHECK(py[0] > 0 && (py[0] & 0x3F) == 0); |
| 442 | TEST_CHECK(puv[0] > 0 && (puv[0] & 0x3F) == 0); |
| 443 | TEST_CHECK(puv[1] > 0 && (puv[1] & 0x3F) == 0); |
| 444 | } |
| 445 | |
| 446 | int rc1 = avx_video_decoder_decode(dec, k_av1_10bit_f1, sizeof(k_av1_10bit_f1), &out); |
| 447 | TEST_CHECK(rc1 == AVX_VIDEO_CODEC_OK || rc1 == AVX_VIDEO_CODEC_NEED_MORE_DATA); |
| 448 | if (rc1 == AVX_VIDEO_CODEC_OK) { |
| 449 | TEST_CHECK(out.width == 128 && out.height == 128); |
| 450 | TEST_CHECK(out.fourcc == OAVX_FOURCC_VIDEO_TYPE_P216); |
| 451 | TEST_CHECK(out.planes[0] != NULL && out.strides[0] == 128U * 2U); |
| 452 | TEST_CHECK(out.planes[1] != NULL && out.strides[1] == 128U * 2U); |
| 453 | const uint16_t *py = (const uint16_t *)(const void *)out.planes[0]; |
| 454 | const uint16_t *puv = (const uint16_t *)(const void *)out.planes[1]; |
| 455 | TEST_CHECK(py[0] > 0 && (py[0] & 0x3F) == 0); |
| 456 | TEST_CHECK(puv[0] > 0 && (puv[0] & 0x3F) == 0); |
| 457 | TEST_CHECK(puv[1] > 0 && (puv[1] & 0x3F) == 0); |
| 458 | } |
| 459 | |
| 460 | avx_video_decoder_destroy(dec); |
| 461 | printf(" [+] AV1 10-bit -> native P216 decode passed!\n"); |
| 462 | } |
| 463 | |
| 464 | static void test_transport_10bit_pipeline(oavx_context_t *ctx, |
| 465 | oavx_color_format_e color_fmt, |
| 466 | oavx_fourcc_video_type_e expect_fourcc) |
| 467 | { |
| 468 | if (!avx_video_decoder_has_backend(OAVX_FOURCC_VIDEO_TYPE_AV01)) { |
| 469 | return; |
| 470 | } |
| 471 | printf("[*] Testing 10-bit transport pipeline (color_fmt=%d, expect=0x%08X)...\n", |
| 472 | (int)color_fmt, (unsigned)expect_fourcc); |
| 473 | |
| 474 | oavx_stream_config_t tx_cfg = { |
| 475 | .name = "10BIT_TX_TEST", |
| 476 | .direction = OAVX_DIR_TRANSMIT, |
| 477 | .transport = OAVX_TRANSPORT_TCP, |
| 478 | .groups = "Public" |
| 479 | }; |
| 480 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 481 | TEST_CHECK(tx != NULL); |
| 482 | |
| 483 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 484 | TEST_CHECK(ep && ep->address); |
| 485 | |
| 486 | oavx_stream_config_t rx_cfg = { |
| 487 | .name = "10BIT_RX_TEST", |
| 488 | .endpoint = ep->address, |
| 489 | .direction = OAVX_DIR_RECEIVE, |
| 490 | .transport = OAVX_TRANSPORT_TCP, |
| 491 | .color_format = color_fmt, |
| 492 | .groups = "Public" |
| 493 | }; |
| 494 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 495 | TEST_CHECK(rx != NULL); |
| 496 | |
| 497 | avx_sleep_ms(80); |
| 498 | |
| 499 | oavx_video_frame_t tx_v = { |
| 500 | .width = 128, |
| 501 | .height = 128, |
| 502 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_AV01, |
| 503 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 504 | .planes = { (uint8_t *)(uintptr_t)k_av1_10bit_f0, NULL, NULL, NULL }, |
| 505 | .strides = { sizeof(k_av1_10bit_f0), 0, 0, 0 }, |
| 506 | .framerate = { .num = 30000, .den = 1000 }, |
| 507 | .aspect_ratio = { .num = 1, .den = 1 }, |
| 508 | .pts_ns = 1000 |
| 509 | }; |
| 510 | TEST_CHECK(oavx_stream_send_video(tx, &tx_v) == OAVX_SUCCESS); |
| 511 | |
| 512 | oavx_video_frame_t vf = {0}; |
| 513 | oavx_frame_type_t ft = OAVX_FRAME_TYPE_NONE; |
| 514 | for (int attempt = 0; attempt < 25; ++attempt) { |
| 515 | ft = oavx_stream_receive(rx, &vf, NULL, NULL, 30); |
| 516 | if (ft != OAVX_FRAME_TYPE_NONE) break; |
| 517 | } |
| 518 | |
| 519 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 520 | TEST_CHECK(vf.width == 128 && vf.height == 128); |
| 521 | TEST_CHECK(vf.fourcc == expect_fourcc); |
| 522 | TEST_CHECK(vf.planes[0] != NULL); |
| 523 | if (expect_fourcc == OAVX_FOURCC_VIDEO_TYPE_P216) { |
| 524 | TEST_CHECK(vf.planes[1] != NULL); |
| 525 | TEST_CHECK(vf.strides[0] == 128U * 2U && vf.strides[1] == 128U * 2U); |
| 526 | const uint16_t *py = (const uint16_t *)(const void *)vf.planes[0]; |
| 527 | TEST_CHECK((py[0] & 0x3F) == 0); |
| 528 | } else if (expect_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA) { |
| 529 | TEST_CHECK(vf.strides[0] == 128U * 4U); |
| 530 | TEST_CHECK(vf.planes[0][3] == 255); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | oavx_stream_close(rx); |
| 535 | oavx_stream_close(tx); |
| 536 | } |
| 537 | |
| 538 | static void exercise_encoder_roundtrip(uint32_t fourcc, const char *label) { |
| 539 | if (!avx_video_encoder_has_backend(fourcc)) { |
| 540 | printf("[*] Skipping %s encoder round-trip (no registered backend)\n", label); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | printf("[*] Testing real %s video encode & decode round-trip...\n", label); |
| 545 | |
| 546 | avx_video_enc_cfg_t enc_cfg = { |
| 547 | .fourcc = fourcc, |
| 548 | .width = 128, |
| 549 | .height = 128, |
| 550 | .fps_num = 30, |
| 551 | .fps_den = 1, |
| 552 | .bitrate_kbps = 2000, |
| 553 | .keyframe_interval = 30 |
| 554 | }; |
| 555 | avx_video_encoder_t *enc = avx_video_encoder_create(&enc_cfg); |
| 556 | TEST_CHECK(enc != NULL); |
| 557 | printf(" [+] Encoder initialized using backend: '%s'\n", |
| 558 | avx_video_encoder_get_backend_name(enc)); |
| 559 | |
| 560 | avx_video_dec_cfg_t dec_cfg = { |
| 561 | .fourcc = fourcc, |
| 562 | .width = 128, |
| 563 | .height = 128, |
| 564 | .preferred_fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY |
| 565 | }; |
| 566 | avx_video_decoder_t *dec = avx_video_decoder_create(&dec_cfg); |
| 567 | TEST_CHECK(dec != NULL); |
| 568 | |
| 569 | /* 1. Test UYVY input */ |
| 570 | uint8_t uyvy[128 * 128 * 2]; |
| 571 | for (size_t i = 0; i < sizeof(uyvy); i += 4) { |
| 572 | uyvy[i + 0] = 128; |
| 573 | uyvy[i + 1] = (uint8_t)(16 + (i % 220)); |
| 574 | uyvy[i + 2] = 128; |
| 575 | uyvy[i + 3] = (uint8_t)(16 + (i % 220)); |
| 576 | } |
| 577 | oavx_video_frame_t in_frame = { |
| 578 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 579 | .width = 128, |
| 580 | .height = 128, |
| 581 | .planes = { uyvy, NULL, NULL, NULL }, |
| 582 | .strides = { 128 * 2, 0, 0, 0 } |
| 583 | }; |
| 584 | |
| 585 | uint8_t pkt_buf[512 * 1024]; |
| 586 | size_t pkt_size = 0; |
| 587 | bool is_key = false; |
| 588 | |
| 589 | /* Frame 0: Keyframe */ |
| 590 | int rc = avx_video_encoder_encode(enc, &in_frame, pkt_buf, sizeof(pkt_buf), &pkt_size, &is_key); |
| 591 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 592 | TEST_CHECK(pkt_size > 4); |
| 593 | TEST_CHECK(is_key == true); |
| 594 | |
| 595 | oavx_video_frame_t out_frame; |
| 596 | memset(&out_frame, 0, sizeof(out_frame)); |
| 597 | int drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 598 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 599 | TEST_CHECK(out_frame.width == 128 && out_frame.height == 128); |
| 600 | |
| 601 | /* Frame 1: P-frame */ |
| 602 | rc = avx_video_encoder_encode(enc, &in_frame, pkt_buf, sizeof(pkt_buf), &pkt_size, &is_key); |
| 603 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 604 | TEST_CHECK(pkt_size > 0); |
| 605 | drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 606 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 607 | TEST_CHECK(out_frame.width == 128 && out_frame.height == 128); |
| 608 | |
| 609 | /* Frame 2: Force Keyframe */ |
| 610 | avx_video_encoder_request_keyframe(enc); |
| 611 | rc = avx_video_encoder_encode(enc, &in_frame, pkt_buf, sizeof(pkt_buf), &pkt_size, &is_key); |
| 612 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 613 | TEST_CHECK(pkt_size > 4); |
| 614 | TEST_CHECK(is_key == true); |
| 615 | drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 616 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 617 | |
| 618 | /* 2. Test RGBA input format */ |
| 619 | uint8_t rgba[128 * 128 * 4]; |
| 620 | memset(rgba, 0xAA, sizeof(rgba)); |
| 621 | oavx_video_frame_t rgba_frame = { |
| 622 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_RGBA, |
| 623 | .width = 128, |
| 624 | .height = 128, |
| 625 | .planes = { rgba, NULL, NULL, NULL }, |
| 626 | .strides = { 128 * 4, 0, 0, 0 } |
| 627 | }; |
| 628 | rc = avx_video_encoder_encode(enc, &rgba_frame, pkt_buf, sizeof(pkt_buf), &pkt_size, &is_key); |
| 629 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 630 | TEST_CHECK(pkt_size > 0); |
| 631 | drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 632 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 633 | |
| 634 | /* 3. Test P216 input format */ |
| 635 | uint16_t p216_y[128 * 128]; |
| 636 | uint16_t p216_uv[128 * 128]; |
| 637 | for (size_t i = 0; i < 128 * 128; ++i) { |
| 638 | p216_y[i] = (uint16_t)((16 + (i % 220)) << 6); |
| 639 | p216_uv[i] = (uint16_t)(128 << 6); |
| 640 | } |
| 641 | oavx_video_frame_t p216_frame = { |
| 642 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_P216, |
| 643 | .width = 128, |
| 644 | .height = 128, |
| 645 | .planes = { (uint8_t *)p216_y, (uint8_t *)p216_uv, NULL, NULL }, |
| 646 | .strides = { 128 * 2, 128 * 2, 0, 0 } |
| 647 | }; |
| 648 | rc = avx_video_encoder_encode(enc, &p216_frame, pkt_buf, sizeof(pkt_buf), &pkt_size, &is_key); |
| 649 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 650 | TEST_CHECK(pkt_size > 0); |
| 651 | drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 652 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 653 | |
| 654 | /* 4. Test dynamic quality adjustment */ |
| 655 | TEST_CHECK(avx_video_encoder_set_quality(enc, 80) == AVX_VIDEO_CODEC_OK); |
| 656 | |
| 657 | avx_video_decoder_destroy(dec); |
| 658 | avx_video_encoder_destroy(enc); |
| 659 | printf(" [+] %s encode & decode round-trip passed!\n", label); |
| 660 | } |
| 661 | |
| 662 | static void exercise_encoder_hd_roundtrip(uint32_t fourcc, const char *label) { |
| 663 | if (!avx_video_encoder_has_backend(fourcc) || !avx_video_decoder_has_backend(fourcc)) { |
| 664 | printf(" [*] Skipping %s HD encode/decode roundtrip (missing backend)\n", label); |
| 665 | return; |
| 666 | } |
| 667 | printf("[*] Testing real %s 720p HD video encode & decode round-trip...\n", label); |
| 668 | |
| 669 | const uint32_t w = 1280; |
| 670 | const uint32_t h = 720; |
| 671 | avx_video_enc_cfg_t enc_cfg = { |
| 672 | .fourcc = fourcc, |
| 673 | .width = w, |
| 674 | .height = h, |
| 675 | .bitrate_kbps = 4000, |
| 676 | .fps_num = 30, |
| 677 | .fps_den = 1, |
| 678 | .quality = 80, |
| 679 | .keyframe_interval = 30 |
| 680 | }; |
| 681 | avx_video_encoder_t *enc = avx_video_encoder_create(&enc_cfg); |
| 682 | TEST_CHECK(enc != NULL); |
| 683 | const char *bname = avx_video_encoder_get_backend_name(enc); |
| 684 | printf(" [+] HD Encoder initialized using backend: '%s'\n", bname); |
| 685 | |
| 686 | avx_video_dec_cfg_t dec_cfg = { |
| 687 | .fourcc = fourcc, |
| 688 | .width = w, |
| 689 | .height = h, |
| 690 | .preferred_fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY |
| 691 | }; |
| 692 | avx_video_decoder_t *dec = avx_video_decoder_create(&dec_cfg); |
| 693 | TEST_CHECK(dec != NULL); |
| 694 | |
| 695 | size_t uyvy_size = (size_t)w * h * 2; |
| 696 | uint8_t *uyvy = (uint8_t *)malloc(uyvy_size); |
| 697 | TEST_CHECK(uyvy != NULL); |
| 698 | for (size_t i = 0; i < uyvy_size; i += 4) { |
| 699 | uyvy[i + 0] = 128; |
| 700 | uyvy[i + 1] = (uint8_t)(16 + (i % 220)); |
| 701 | uyvy[i + 2] = 128; |
| 702 | uyvy[i + 3] = (uint8_t)(16 + (i % 220)); |
| 703 | } |
| 704 | oavx_video_frame_t in_frame = { |
| 705 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 706 | .width = w, |
| 707 | .height = h, |
| 708 | .planes = { uyvy, NULL, NULL, NULL }, |
| 709 | .strides = { w * 2, 0, 0, 0 } |
| 710 | }; |
| 711 | |
| 712 | size_t pkt_buf_size = 2 * 1024 * 1024; |
| 713 | uint8_t *pkt_buf = (uint8_t *)malloc(pkt_buf_size); |
| 714 | TEST_CHECK(pkt_buf != NULL); |
| 715 | size_t pkt_size = 0; |
| 716 | bool is_key = false; |
| 717 | |
| 718 | /* Frame 0: Keyframe */ |
| 719 | int rc = avx_video_encoder_encode(enc, &in_frame, pkt_buf, pkt_buf_size, &pkt_size, &is_key); |
| 720 | TEST_CHECK(rc == AVX_VIDEO_CODEC_OK); |
| 721 | TEST_CHECK(pkt_size > 4); |
| 722 | TEST_CHECK(is_key == true); |
| 723 | |
| 724 | oavx_video_frame_t out_frame; |
| 725 | memset(&out_frame, 0, sizeof(out_frame)); |
| 726 | int drc = avx_video_decoder_decode(dec, pkt_buf, pkt_size, &out_frame); |
| 727 | TEST_CHECK(drc == AVX_VIDEO_CODEC_OK); |
| 728 | TEST_CHECK(out_frame.width == w && out_frame.height == h); |
| 729 | |
| 730 | free(pkt_buf); |
| 731 | free(uyvy); |
| 732 | avx_video_decoder_destroy(dec); |
| 733 | avx_video_encoder_destroy(enc); |
| 734 | printf(" [+] %s HD 720p encode & decode round-trip passed!\n", label); |
| 735 | } |
| 736 | |
| 737 | static void test_transport_encoding_pipeline(oavx_context_t *ctx, |
| 738 | oavx_transport_e transport, |
| 739 | uint32_t codec_fourcc, |
| 740 | const char *label) |
| 741 | { |
| 742 | if (!avx_video_encoder_has_backend(codec_fourcc) || !avx_video_decoder_has_backend(codec_fourcc)) { |
| 743 | printf(" [*] Skipping %s transport encode/decode test (missing encoder or decoder backend)\n", label); |
| 744 | return; |
| 745 | } |
| 746 | printf("[*] Testing live transport %s encoding pipeline (transport=%d)...\n", label, (int)transport); |
| 747 | |
| 748 | oavx_stream_config_t tx_cfg = { |
| 749 | .name = "ENC_TX_TEST", |
| 750 | .direction = OAVX_DIR_TRANSMIT, |
| 751 | .transport = transport, |
| 752 | .video_codec_fourcc = codec_fourcc, |
| 753 | .video_bitrate_kbps = 4000, |
| 754 | .keyframe_interval = 30, |
| 755 | .groups = "Public" |
| 756 | }; |
| 757 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 758 | TEST_CHECK(tx != NULL); |
| 759 | TEST_CHECK(oavx_stream_get_video_compression(tx) == codec_fourcc); |
| 760 | |
| 761 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 762 | TEST_CHECK(ep && ep->address); |
| 763 | |
| 764 | oavx_stream_config_t rx_cfg = { |
| 765 | .name = "ENC_RX_TEST", |
| 766 | .endpoint = ep->address, |
| 767 | .direction = OAVX_DIR_RECEIVE, |
| 768 | .transport = transport, |
| 769 | .color_format = OAVX_COLOR_FORMAT_BEST, |
| 770 | .groups = "Public" |
| 771 | }; |
| 772 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 773 | TEST_CHECK(rx != NULL); |
| 774 | |
| 775 | avx_sleep_ms(80); |
| 776 | |
| 777 | /* Generate raw UYVY video pattern */ |
| 778 | uint8_t uyvy[128 * 128 * 2]; |
| 779 | for (size_t i = 0; i < sizeof(uyvy); i += 4) { |
| 780 | uyvy[i + 0] = 128; |
| 781 | uyvy[i + 1] = (uint8_t)(16 + (i % 220)); |
| 782 | uyvy[i + 2] = 128; |
| 783 | uyvy[i + 3] = (uint8_t)(16 + (i % 220)); |
| 784 | } |
| 785 | |
| 786 | oavx_video_frame_t tx_v = { |
| 787 | .width = 128, |
| 788 | .height = 128, |
| 789 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 790 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 791 | .planes = { uyvy, NULL, NULL, NULL }, |
| 792 | .strides = { 128 * 2, 0, 0, 0 }, |
| 793 | .framerate = { .num = 30000, .den = 1000 }, |
| 794 | .aspect_ratio = { .num = 1, .den = 1 }, |
| 795 | .pts_ns = 1000000 |
| 796 | }; |
| 797 | |
| 798 | /* Send multiple frames so keyframe + inter frames are transmitted and decoded */ |
| 799 | bool received_frame = false; |
| 800 | for (int f = 0; f < 5; ++f) { |
| 801 | tx_v.pts_ns = (int64_t)(f + 1) * 33333333LL; |
| 802 | TEST_CHECK(oavx_stream_send_video(tx, &tx_v) == OAVX_SUCCESS); |
| 803 | |
| 804 | for (int attempt = 0; attempt < 10; ++attempt) { |
| 805 | oavx_video_frame_t vf = {0}; |
| 806 | oavx_frame_type_t ft = oavx_stream_receive(rx, &vf, NULL, NULL, 30); |
| 807 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 808 | TEST_CHECK(vf.width == 128 && vf.height == 128); |
| 809 | TEST_CHECK(vf.planes[0] != NULL); |
| 810 | received_frame = true; |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | TEST_CHECK(received_frame == true); |
| 816 | |
| 817 | /* Test dynamic reconfiguration API */ |
| 818 | TEST_CHECK(oavx_stream_set_video_compression(tx, OAVX_FOURCC_VIDEO_TYPE_HEVC, 2000) == OAVX_SUCCESS); |
| 819 | TEST_CHECK(oavx_stream_get_video_compression(tx) == OAVX_FOURCC_VIDEO_TYPE_HEVC); |
| 820 | |
| 821 | oavx_stream_close(rx); |
| 822 | oavx_stream_close(tx); |
| 823 | printf(" [+] %s transport live encoding pipeline passed!\n", label); |
| 824 | } |
| 825 | |
| 826 | int main(void) { |
| 827 | printf("\n=== OpenAVX Video Decoder & Transport Pipeline Verification ===\n\n"); |
| 828 | |
| 829 | oavx_context_t *ctx = oavx_context_create(); |
| 830 | TEST_CHECK(ctx != NULL); |
| 831 | TEST_CHECK(avx_video_decoder_register(&g_mock_hx_ops)); |
| 832 | |
| 833 | /* 1. RUDP path: H264 priming suppression + NV12 -> BGRA/UYVY conversion */ |
| 834 | test_transport_hx_decode_and_color_convert( |
| 835 | ctx, OAVX_TRANSPORT_RUDP, OAVX_COLOR_FORMAT_BGRX_BGRA, OAVX_FOURCC_VIDEO_TYPE_BGRA); |
| 836 | test_transport_hx_decode_and_color_convert( |
| 837 | ctx, OAVX_TRANSPORT_RUDP, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_FOURCC_VIDEO_TYPE_UYVY); |
| 838 | |
| 839 | /* 2. TCP path: NV12 -> RGBA conversion + explicit COMPRESSED_V1 passthrough */ |
| 840 | test_transport_hx_decode_and_color_convert( |
| 841 | ctx, OAVX_TRANSPORT_TCP, OAVX_COLOR_FORMAT_RGBX_RGBA, OAVX_FOURCC_VIDEO_TYPE_RGBA); |
| 842 | test_transport_hx_decode_and_color_convert( |
| 843 | ctx, OAVX_TRANSPORT_TCP, OAVX_COLOR_FORMAT_COMPRESSED_V1, OAVX_FOURCC_VIDEO_TYPE_H264); |
| 844 | |
| 845 | /* 3. Destroy engine/context cleanly (clears mock backend), then re-init & probe real OS decoders */ |
| 846 | oavx_context_destroy(ctx); |
| 847 | TEST_CHECK(avx_video_codec_init()); |
| 848 | |
| 849 | exercise_native_decoder(OAVX_FOURCC_VIDEO_TYPE_H264, "H.264", |
| 850 | k_h264_f0, k_h264_sps_pps_len, |
| 851 | k_h264_f0, sizeof(k_h264_f0), |
| 852 | k_h264_f1, sizeof(k_h264_f1)); |
| 853 | |
| 854 | exercise_native_decoder(OAVX_FOURCC_VIDEO_TYPE_HEVC, "HEVC", |
| 855 | NULL, 0, |
| 856 | k_hevc_f0, sizeof(k_hevc_f0), |
| 857 | k_hevc_f1, sizeof(k_hevc_f1)); |
| 858 | |
| 859 | exercise_native_decoder(OAVX_FOURCC_VIDEO_TYPE_AV01, "AV1", |
| 860 | NULL, 0, |
| 861 | k_av1_f0, sizeof(k_av1_f0), |
| 862 | k_av1_f1, sizeof(k_av1_f1)); |
| 863 | |
| 864 | /* 4. Verify 4-byte length-prefixed AVCC / HVCC container bitstream auto-conversion */ |
| 865 | exercise_avcc_decoder(OAVX_FOURCC_VIDEO_TYPE_H264, "H.264", |
| 866 | k_h264_f0, sizeof(k_h264_f0), |
| 867 | k_h264_f1, sizeof(k_h264_f1)); |
| 868 | |
| 869 | exercise_avcc_decoder(OAVX_FOURCC_VIDEO_TYPE_HEVC, "HEVC", |
| 870 | k_hevc_f0, sizeof(k_hevc_f0), |
| 871 | k_hevc_f1, sizeof(k_hevc_f1)); |
| 872 | |
| 873 | /* 5. Verify 10-bit AV1 bitstream decode to native P216 */ |
| 874 | exercise_10bit_decoder(); |
| 875 | |
| 876 | /* 6. Verify end-to-end 10-bit transport pipeline */ |
| 877 | ctx = oavx_context_create(); |
| 878 | TEST_CHECK(ctx != NULL); |
| 879 | test_transport_10bit_pipeline(ctx, OAVX_COLOR_FORMAT_BEST, OAVX_FOURCC_VIDEO_TYPE_P216); |
| 880 | test_transport_10bit_pipeline(ctx, OAVX_COLOR_FORMAT_P216, OAVX_FOURCC_VIDEO_TYPE_P216); |
| 881 | test_transport_10bit_pipeline(ctx, OAVX_COLOR_FORMAT_RGBX_RGBA, OAVX_FOURCC_VIDEO_TYPE_RGBA); |
| 882 | oavx_context_destroy(ctx); |
| 883 | |
| 884 | /* 7. Verify real video encoder round-trip (H.264 & HEVC) */ |
| 885 | exercise_encoder_roundtrip(OAVX_FOURCC_VIDEO_TYPE_H264, "H.264"); |
| 886 | exercise_encoder_roundtrip(OAVX_FOURCC_VIDEO_TYPE_HEVC, "HEVC"); |
| 887 | exercise_encoder_hd_roundtrip(OAVX_FOURCC_VIDEO_TYPE_H264, "H.264"); |
| 888 | exercise_encoder_hd_roundtrip(OAVX_FOURCC_VIDEO_TYPE_HEVC, "HEVC"); |
| 889 | |
| 890 | /* 8. Verify live transport encoding pipeline across TCP & RUDP */ |
| 891 | ctx = oavx_context_create(); |
| 892 | TEST_CHECK(ctx != NULL); |
| 893 | test_transport_encoding_pipeline(ctx, OAVX_TRANSPORT_TCP, OAVX_FOURCC_VIDEO_TYPE_H264, "H.264 (TCP)"); |
| 894 | test_transport_encoding_pipeline(ctx, OAVX_TRANSPORT_RUDP, OAVX_FOURCC_VIDEO_TYPE_H264, "H.264 (RUDP)"); |
| 895 | test_transport_encoding_pipeline(ctx, OAVX_TRANSPORT_TCP, OAVX_FOURCC_VIDEO_TYPE_HEVC, "HEVC (TCP)"); |
| 896 | oavx_context_destroy(ctx); |
| 897 | |
| 898 | printf("\n[PASS] openavx_video_decoder_test\n"); |
| 899 | return 0; |
| 900 | } |