From f964420c86e2ac019b93bcdea02a86e7d5af4410 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 17:02:25 +0100 Subject: [PATCH] fix: preserve color metadata during FFmpeg playback --- docs/guide/media/media-files.md | 2 + .../src/main/cpp/include/webrtc_java_api.h | 22 +++ .../src/main/cpp/src/JNI_ColorProbe.cpp | 77 +++++++++++ .../src/main/cpp/src/api/ExtensionApi.cpp | 20 ++- .../src/main/cpp/include/media/VideoDecoder.h | 6 + .../src/main/cpp/src/JNI_ColorProbe.cpp | 82 +++++++++++ .../src/main/cpp/src/media/MediaPacer.cpp | 9 +- .../src/main/cpp/src/media/VideoDecoder.cpp | 129 +++++++++++++----- .../webrtc/media/player/ColorProbe.java | 31 +++++ .../webrtc/media/player/ColorSpaceTest.java | 85 ++++++++++++ 10 files changed, 426 insertions(+), 37 deletions(-) create mode 100644 webrtc-jni/src/main/cpp/src/JNI_ColorProbe.cpp create mode 100644 webrtc-media/src/main/cpp/src/JNI_ColorProbe.cpp create mode 100644 webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorProbe.java create mode 100644 webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorSpaceTest.java diff --git a/docs/guide/media/media-files.md b/docs/guide/media/media-files.md index 0fc534d0..7ee202ef 100644 --- a/docs/guide/media/media-files.md +++ b/docs/guide/media/media-files.md @@ -296,6 +296,8 @@ The FFmpeg build is deliberately small, and carries only what this module plays: Audio of any rate or layout is resampled to what WebRTC takes, which is 48 kHz 16-bit PCM in mono or stereo. Video that decodes to I420 — almost all 8-bit H.264, VP8, VP9 and MPEG-4 — reaches the encoder without being copied; anything else is converted first. +Video retains its color matrix, full or limited range, color primaries and transfer function through conversion and into the native WebRTC frame, including hardware readback. Missing matrix/range metadata defaults to BT.601 limited range; RGB and JPEG pixel formats imply full range. RGB is converted to BT.601 YUV. The negotiated encoder and receiver determine how this information is carried and displayed. + Sources can be local files, or live streams over RTSP (`rtsp://`), with RTP over UDP or TCP. Protocols that need TLS, such as `rtsps://` and `https://`, are not part of the build, and neither are HLS, DASH and RTMP. A source cannot reach any other protocol either, even one FFmpeg uses internally, so a URL passed on from a user cannot make the module fetch something else. ## Licensing diff --git a/webrtc-jni/src/main/cpp/include/webrtc_java_api.h b/webrtc-jni/src/main/cpp/include/webrtc_java_api.h index 7d303fcb..9becd952 100644 --- a/webrtc-jni/src/main/cpp/include/webrtc_java_api.h +++ b/webrtc-jni/src/main/cpp/include/webrtc_java_api.h @@ -129,6 +129,18 @@ struct wj_i420_frame { void * opaque; }; +/** + * Color description using ITU-T H.273 identifiers for primaries, transfer and + * matrix (2 means unspecified). Range: 0 unspecified, 1 limited, 2 full. + * Unknown identifiers are treated as unspecified by the receiving library. + */ +struct wj_color_space { + uint8_t primaries; + uint8_t transfer; + uint8_t matrix; + uint8_t range; +}; + /** * One chunk of decoded audio: interleaved signed 16-bit PCM in host byte * order, exactly 10 ms long, which is the only chunk length WebRTC accepts. @@ -303,6 +315,16 @@ struct webrtc_java_api { * handle must not be used afterwards. */ void (*encoded_frames_release)(void * frames); + + /** + * Like video_source_push, with a color description for the I420 pixels. + * A null color description has the same behavior as video_source_push. + * Ownership and error handling are identical. Appended in interface v1; + * callers must check size before accessing this member. + */ + int (*video_source_push_color)(void * source, const struct wj_i420_frame * frame, + const struct wj_color_space * color); + }; #ifdef __cplusplus diff --git a/webrtc-jni/src/main/cpp/src/JNI_ColorProbe.cpp b/webrtc-jni/src/main/cpp/src/JNI_ColorProbe.cpp new file mode 100644 index 00000000..e68456e5 --- /dev/null +++ b/webrtc-jni/src/main/cpp/src/JNI_ColorProbe.cpp @@ -0,0 +1,77 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// The Java class exists only in media tests. Observe the actual WebRTC frame +// after the extension table and CustomVideoSource have handled it. +#include +#include "JavaBoundary.h" +#include "api/ExtensionApi.h" +#include "media/video/CustomVideoSource.h" +#include "api/make_ref_counted.h" +#include + +namespace { +class ColorSink : public webrtc::VideoSinkInterface { +public: + jint values[7] = { -1, -1, -1, -1, -1, -1, -1 }; + void OnFrame(const webrtc::VideoFrame & frame) override { + auto pixels = frame.video_frame_buffer()->ToI420(); + values[0] = pixels->DataY()[0]; + values[1] = pixels->DataU()[0]; + values[2] = pixels->DataV()[0]; + if (frame.color_space()) { + const auto & color = *frame.color_space(); + values[3] = static_cast(color.primaries()); + values[4] = static_cast(color.transfer()); + values[5] = static_cast(color.matrix()); + values[6] = static_cast(color.range()); + } + } +}; +} + +extern "C" JNIEXPORT jintArray JNICALL Java_dev_onvoid_webrtc_media_player_ColorProbe_push +(JNIEnv * env, jclass, jintArray description, jboolean legacy) +{ + return jni::CallFromJava(env, [&]() -> jintArray { + if (description == nullptr || env->GetArrayLength(description) != 7) return nullptr; + jint data[7]; + env->GetIntArrayRegion(description, 0, 7, data); + if (env->ExceptionCheck()) return nullptr; + auto source = webrtc::make_ref_counted(std::make_shared()); + ColorSink sink; + auto * videoSource = static_cast *>(source.get()); + videoSource->AddOrUpdateSink(&sink, webrtc::VideoSinkWants()); + uint8_t y[32 * 32], u[16 * 16], v[16 * 16]; + std::memset(y, data[0], sizeof(y)); + std::memset(u, data[1], sizeof(u)); + std::memset(v, data[2], sizeof(v)); + wj_i420_frame frame = {}; + frame.width = frame.height = 32; + frame.y = y; frame.u = u; frame.v = v; + frame.stride_y = 32; frame.stride_u = frame.stride_v = 16; + wj_color_space color = { static_cast(data[3]), static_cast(data[4]), + static_cast(data[5]), static_cast(data[6]) }; + const auto * api = jni::GetExtensionApi(); + int status = legacy ? api->video_source_push(source.get(), &frame) + : api->video_source_push_color(source.get(), &frame, &color); + videoSource->RemoveSink(&sink); + if (status != WEBRTC_JAVA_OK) return nullptr; + jintArray result = env->NewIntArray(7); + if (result != nullptr) env->SetIntArrayRegion(result, 0, 7, sink.values); + return result; + }); +} diff --git a/webrtc-jni/src/main/cpp/src/api/ExtensionApi.cpp b/webrtc-jni/src/main/cpp/src/api/ExtensionApi.cpp index 04577ae5..793b11b1 100644 --- a/webrtc-jni/src/main/cpp/src/api/ExtensionApi.cpp +++ b/webrtc-jni/src/main/cpp/src/api/ExtensionApi.cpp @@ -71,7 +71,7 @@ namespace jni return error; } - int ApiVideoSourcePush(void * source, const wj_i420_frame * frame) + int ApiVideoSourcePushColor(void * source, const wj_i420_frame * frame, const wj_color_space * color) { if (frame == nullptr) { return WEBRTC_JAVA_ERR_HANDLE; @@ -112,9 +112,19 @@ namespace jni } }); + webrtc::ColorSpace colorSpace; + + if (color != nullptr) { + colorSpace.set_primaries_from_uint8(color->primaries); + colorSpace.set_transfer_from_uint8(color->transfer); + colorSpace.set_matrix_from_uint8(color->matrix); + colorSpace.set_range_from_uint8(color->range); + } + webrtc::VideoFrame videoFrame = webrtc::VideoFrame::Builder() .set_video_frame_buffer(buffer) .set_rotation(rotation) + .set_color_space(color != nullptr ? &colorSpace : nullptr) .build(); CustomVideoSource * videoSource = static_cast(source); @@ -129,6 +139,11 @@ namespace jni return WEBRTC_JAVA_OK; } + int ApiVideoSourcePush(void * source, const wj_i420_frame * frame) + { + return ApiVideoSourcePushColor(source, frame, nullptr); + } + int ApiAudioSourcePush(void * source, const wj_audio_chunk * chunk) { if (source == nullptr || chunk == nullptr || chunk->samples == nullptr) { @@ -256,7 +271,8 @@ namespace jni &ApiAudioSourceRelease, &ApiEncodedObserverAdd, &ApiEncodedObserverRemove, - &ApiEncodedFramesRelease + &ApiEncodedFramesRelease, + &ApiVideoSourcePushColor }; } diff --git a/webrtc-media/src/main/cpp/include/media/VideoDecoder.h b/webrtc-media/src/main/cpp/include/media/VideoDecoder.h index 667a83da..29fb29fb 100644 --- a/webrtc-media/src/main/cpp/include/media/VideoDecoder.h +++ b/webrtc-media/src/main/cpp/include/media/VideoDecoder.h @@ -115,6 +115,12 @@ namespace ffmpeg bool IsHardware() const; private: + friend struct ColorProbe; + + void ResolveColor(AVFrame * frame) const; + + // Resolves color properties and retains I420 or converts other formats. + int MakeI420(AVFrame * source, AVFrame ** frame); // How many packets are kept, until the hardware has produced a // frame, to be decoded again in software if it fails by then. static constexpr size_t kMaxKeptPackets = 32; diff --git a/webrtc-media/src/main/cpp/src/JNI_ColorProbe.cpp b/webrtc-media/src/main/cpp/src/JNI_ColorProbe.cpp new file mode 100644 index 00000000..212d49e8 --- /dev/null +++ b/webrtc-media/src/main/cpp/src/JNI_ColorProbe.cpp @@ -0,0 +1,82 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Test-only entry point: exercises the production color resolver and scaler +// with constant planes, avoiding lossy codecs and hardware-dependent results. +#include +#include "media/VideoDecoder.h" +#include "media/JniBoundary.h" +#include + +namespace ffmpeg { +struct ColorProbe { + static jintArray Convert(JNIEnv * env, jint format, jint matrix, jint range, + jint y, jint u, jint v) + { + VideoDecoder decoder; + AVFrame * source = av_frame_alloc(); + AVFrame * converted = nullptr; + if (source == nullptr) return nullptr; + source->width = source->height = 32; + source->format = format == 0 ? AV_PIX_FMT_YUV444P + : format == 1 ? AV_PIX_FMT_RGB24 + : format == 2 ? AV_PIX_FMT_YUVJ420P : AV_PIX_FMT_YUV420P; + source->colorspace = static_cast(matrix); + source->color_range = static_cast(range); + source->color_primaries = AVCOL_PRI_BT709; + source->color_trc = AVCOL_TRC_BT709; + int error = av_frame_get_buffer(source, 32); + if (error >= 0) { + if (format == 1) { + for (int row = 0; row < 32; ++row) { + for (int col = 0; col < 32; ++col) { + uint8_t * pixel = source->data[0] + row * source->linesize[0] + col * 3; + pixel[0] = y; pixel[1] = u; pixel[2] = v; + } + } + } + else { + const int values[] = { y, u, v }; + for (int plane = 0; plane < 3; ++plane) { + int size = format >= 2 && plane > 0 ? 16 : 32; + for (int row = 0; row < size; ++row) { + std::memset(source->data[plane] + row * source->linesize[plane], values[plane], size); + } + } + } + error = decoder.MakeI420(source, &converted); + } + jintArray result = nullptr; + if (error >= 0) { + jint values[] = { converted->data[0][0], converted->data[1][0], converted->data[2][0], + converted->color_primaries, converted->color_trc, converted->colorspace, converted->color_range }; + result = env->NewIntArray(7); + if (result != nullptr) env->SetIntArrayRegion(result, 0, 7, values); + } + av_frame_free(&converted); + av_frame_free(&source); + return result; + } +}; +} + +extern "C" JNIEXPORT jintArray JNICALL Java_dev_onvoid_webrtc_media_player_ColorProbe_convert +(JNIEnv * env, jclass, jint format, jint matrix, jint range, jint y, jint u, jint v) +{ + return ffmpeg::CallFromJava(env, static_cast(nullptr), [&]() { + return ffmpeg::ColorProbe::Convert(env, format, matrix, range, y, u, v); + }); +} diff --git a/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp b/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp index ad879afe..f7447984 100644 --- a/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp +++ b/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp @@ -504,7 +504,14 @@ namespace ffmpeg // the push succeeds or fails, so this item must not free it as well. item.frame = nullptr; - api_->video_source_push(video_source_, &pushed); + const wj_color_space color = { + static_cast(frame->color_primaries), + static_cast(frame->color_trc), + static_cast(frame->colorspace), + static_cast(frame->color_range) + }; + + api_->video_source_push_color(video_source_, &pushed, &color); } void MediaPacer::DeliverAudio(const AudioItem & item, int64_t mapped_us) diff --git a/webrtc-media/src/main/cpp/src/media/VideoDecoder.cpp b/webrtc-media/src/main/cpp/src/media/VideoDecoder.cpp index 6d32c789..9ded8cbb 100644 --- a/webrtc-media/src/main/cpp/src/media/VideoDecoder.cpp +++ b/webrtc-media/src/main/cpp/src/media/VideoDecoder.cpp @@ -588,37 +588,20 @@ namespace ffmpeg transfer_height_ = frames->height; } - result = ConvertToI420(transferred_, frame); - } - - av_frame_unref(decoded_); - } - else if (IsI420(decoded_->format)) { - // Hand over a reference to the decoded picture rather than a copy - // of it. The caller drops that reference once WebRTC is done. - AVFrame * reference = av_frame_alloc(); - - if (reference == nullptr) { - av_frame_unref(decoded_); - - return AVERROR(ENOMEM); + // Readback transfers pixels, not AVFrame properties. Copy the + // color description explicitly on every picture, including reuse. + av_frame_side_data_free(&transferred_->side_data, &transferred_->nb_side_data); + av_dict_free(&transferred_->metadata); + result = av_frame_copy_props(transferred_, decoded_); + if (result >= 0) { + result = MakeI420(transferred_, frame); + } } - result = av_frame_ref(reference, decoded_); - av_frame_unref(decoded_); - - if (result < 0) { - av_frame_free(&reference); - - return result; - } - - *frame = reference; - result = 0; } else { - result = ConvertToI420(decoded_, frame); + result = MakeI420(decoded_, frame); av_frame_unref(decoded_); } @@ -777,16 +760,85 @@ namespace ffmpeg return hardware_.load(); } + int VideoDecoder::MakeI420(AVFrame * source, AVFrame ** frame) + { + ResolveColor(source); + + if (IsI420(source->format)) { + // Hand over a reference to the decoded picture rather than a copy + // of it. The caller drops that reference once WebRTC is done. + AVFrame * reference = av_frame_alloc(); + + if (reference == nullptr) { + return AVERROR(ENOMEM); + } + + int result = av_frame_ref(reference, source); + + if (result < 0) { + av_frame_free(&reference); + + return result; + } + + *frame = reference; + return 0; + } + + return ConvertToI420(source, frame); + } + + void VideoDecoder::ResolveColor(AVFrame * frame) const + { + const AVCodecParameters * parameters = stream_ != nullptr ? stream_->codecpar : nullptr; + const AVPixFmtDescriptor * descriptor = av_pix_fmt_desc_get(static_cast(frame->format)); + const bool rgb = descriptor != nullptr && (descriptor->flags & AV_PIX_FMT_FLAG_RGB); + const bool jpeg = frame->format == AV_PIX_FMT_YUVJ420P + || frame->format == AV_PIX_FMT_YUVJ422P || frame->format == AV_PIX_FMT_YUVJ444P + || frame->format == AV_PIX_FMT_YUVJ440P || frame->format == AV_PIX_FMT_YUVJ411P; + + if (frame->colorspace == AVCOL_SPC_UNSPECIFIED) { + frame->colorspace = parameters != nullptr ? parameters->color_space : AVCOL_SPC_UNSPECIFIED; + } + if (rgb) { + frame->colorspace = AVCOL_SPC_RGB; + } + else if (frame->colorspace == AVCOL_SPC_UNSPECIFIED) { + // Match the historical swscale default; do not guess from resolution. + frame->colorspace = AVCOL_SPC_SMPTE170M; + } + if (frame->color_range == AVCOL_RANGE_UNSPECIFIED) { + frame->color_range = parameters != nullptr ? parameters->color_range : AVCOL_RANGE_UNSPECIFIED; + } + if (rgb || jpeg) { + frame->color_range = AVCOL_RANGE_JPEG; + } + else if (frame->color_range == AVCOL_RANGE_UNSPECIFIED) { + frame->color_range = AVCOL_RANGE_MPEG; + } + if (parameters != nullptr) { + if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED) { + frame->color_primaries = parameters->color_primaries; + } + if (frame->color_trc == AVCOL_TRC_UNSPECIFIED) { + frame->color_trc = parameters->color_trc; + } + } + } + int VideoDecoder::ConvertToI420(const AVFrame * source, AVFrame ** result) { - sws_context_ = sws_getCachedContext(sws_context_, - source->width, source->height, - static_cast(source->format), - source->width, source->height, AV_PIX_FMT_YUV420P, - SWS_BILINEAR, nullptr, nullptr, nullptr); + // Dynamic swscale reads matrix/range from the frames and rebuilds its + // graph when they change, even if dimensions and pixel format do not. + if (sws_context_ == nullptr) { + sws_context_ = sws_alloc_context(); + if (sws_context_ != nullptr) { + sws_context_->flags = SWS_BILINEAR; + } + } if (sws_context_ == nullptr) { - return AVERROR(EINVAL); + return AVERROR(ENOMEM); } AVFrame * converted = nullptr; @@ -797,8 +849,17 @@ namespace ffmpeg return error; } - error = sws_scale(sws_context_, source->data, source->linesize, 0, - source->height, converted->data, converted->linesize); + // Keep side data too: swscale uses HDR luminance metadata, so losing + // it on the destination could request an unintended brightness change. + error = av_frame_copy_props(converted, source); + if (error < 0) { + av_frame_free(&converted); + return error; + } + converted->colorspace = source->colorspace == AVCOL_SPC_RGB + ? AVCOL_SPC_SMPTE170M : source->colorspace; + + error = sws_scale_frame(sws_context_, converted, source); if (error < 0) { av_frame_free(&converted); diff --git a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorProbe.java b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorProbe.java new file mode 100644 index 00000000..010b5c9d --- /dev/null +++ b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorProbe.java @@ -0,0 +1,31 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc.media.player; + +import dev.onvoid.webrtc.internal.NativeApi; + +final class ColorProbe { + static { + NativeApi.tableAddress(); + FFmpeg.load(); + } + + private ColorProbe() {} + + static native int[] convert(int format, int matrix, int range, int y, int u, int v); + static native int[] push(int[] frame, boolean legacy); +} diff --git a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorSpaceTest.java b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorSpaceTest.java new file mode 100644 index 00000000..b9a8249f --- /dev/null +++ b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/ColorSpaceTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc.media.player; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +@Execution(ExecutionMode.SAME_THREAD) +class ColorSpaceTest { + @Test + void preservesColorAndRange() { + // H.273: 6 = SMPTE 170M (BT.601), 1 = BT.709; range 1 = limited, 2 = full. + // Red, green, blue, black, middle gray and white. Reference values + // are rounded from the BT.601/709 Y'CbCr equations for RGB primaries. + int[][][] references = { + { {81,90,240}, {145,54,34}, {41,240,110}, {16,128,128}, {126,128,128}, {235,128,128} }, + { {76,85,255}, {150,44,21}, {29,255,107}, {0,128,128}, {128,128,128}, {255,128,128} }, + { {63,102,240}, {173,42,26}, {32,240,118}, {16,128,128}, {126,128,128}, {235,128,128} }, + { {54,99,255}, {182,30,12}, {18,255,116}, {0,128,128}, {128,128,128}, {255,128,128} } + }; + int index = 0; + for (int matrix : new int[] {6, 1}) { + for (int range : new int[] {1, 2}) { + for (int[] sample : references[index]) { + for (int format : new int[] {0, 3}) { // YUV444 conversion and I420 direct path. + int[] converted = ColorProbe.convert(format, matrix, range, sample[0], sample[1], sample[2]); + assertNotNull(converted, "conversion failed"); + assertArrayEquals(new int[] {sample[0], sample[1], sample[2], 1, 1, matrix, range}, converted); + assertArrayEquals(converted, ColorProbe.push(converted, false)); + } + } + index++; + } + } + } + + @Test + void convertsRgbAndInfersJpegRange() { + int[][] rgb = { {255,0,0}, {0,255,0}, {0,0,255}, {0,0,0}, {128,128,128}, {255,255,255} }; + int[][] yuv = { {76,85,255}, {150,44,21}, {29,255,107}, {0,128,128}, {128,128,128}, {255,128,128} }; + for (int i = 0; i < rgb.length; i++) { + int[] converted = ColorProbe.convert(1, 0, 0, rgb[i][0], rgb[i][1], rgb[i][2]); + assertNotNull(converted); + for (int plane = 0; plane < 3; plane++) { + assertEquals(yuv[i][plane], converted[plane], 1, "RGB plane " + plane); + } + assertArrayEquals(new int[] {1,1,6,2}, java.util.Arrays.copyOfRange(converted, 3, 7)); + assertArrayEquals(converted, ColorProbe.push(converted, false)); + } + assertArrayEquals(new int[] {0,128,128,1,1,6,2}, ColorProbe.convert(2, 2, 0, 0,128,128)); + } + + @Test + void defaultsMissingMetadata() { + assertArrayEquals(new int[] {16,128,128,1,1,6,1}, ColorProbe.convert(0, 2, 0, 16,128,128)); + // Unknown H.273 identifiers must not become invalid WebRTC enum values. + assertArrayEquals(new int[] {16,128,128,2,2,2,0}, + ColorProbe.push(new int[] {16,128,128,255,255,255,255}, false)); + } + + @Test + void keepsLegacyPushCompatible() { + assertArrayEquals(new int[] {16,128,128,-1,-1,-1,-1}, + ColorProbe.push(new int[] {16,128,128,1,1,1,1}, true)); + } +}