Skip to content

Commit 495663e

Browse files
authored
Merge pull request #12 from andreialecu/feat/android-stream-color-space
feat(android): decoded frames take the stream's color space where the config has none
2 parents 12f1715 + 68f8e5b commit 495663e

10 files changed

Lines changed: 257 additions & 15 deletions

File tree

‎android/src/main/cpp/VideoDecoder.hpp‎

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <map>
1212

1313
#include "HybridNativeVideoDecoderSpec.hpp"
14+
#include "NativeColorSpace.hpp"
1415
#include "NativeDecodedFrame.hpp"
1516
#include "Capabilities.hpp"
1617
#include "NativeVideoDecoderConfig.hpp"
@@ -19,6 +20,59 @@
1920

2021
namespace margelo::nitro::webcodecs {
2122

23+
// MediaFormat's color aspects (KEY_COLOR_STANDARD, _TRANSFER, _RANGE) as a
24+
// WebCodecs color space. 0 (unspecified) and values WebCodecs has no name for
25+
// leave the member out. SDR video's transfer is BT.601's or BT.709's curve,
26+
// the same one, named after the standard.
27+
inline NativeColorSpace colorSpaceOf(int32_t standard, int32_t transfer, int32_t range) {
28+
NativeColorSpace colorSpace;
29+
bool bt601 = false;
30+
switch (standard) {
31+
case 1: // BT.709
32+
colorSpace.primaries = "bt709";
33+
colorSpace.matrix = "bt709";
34+
break;
35+
case 2: // BT.601 625 (PAL)
36+
case 3: // BT.601 625, unadjusted
37+
colorSpace.primaries = "bt470bg";
38+
colorSpace.matrix = "bt470bg";
39+
bt601 = true;
40+
break;
41+
case 4: // BT.601 525 (NTSC)
42+
case 5: // BT.601 525, unadjusted
43+
colorSpace.primaries = "smpte170m";
44+
colorSpace.matrix = "smpte170m";
45+
bt601 = true;
46+
break;
47+
case 6: // BT.2020
48+
colorSpace.primaries = "bt2020";
49+
colorSpace.matrix = "bt2020-ncl";
50+
break;
51+
default:
52+
break;
53+
}
54+
switch (transfer) {
55+
case 1:
56+
colorSpace.transfer = "linear";
57+
break;
58+
case 3: // SDR video
59+
colorSpace.transfer = bt601 ? "smpte170m" : "bt709";
60+
break;
61+
case 6:
62+
colorSpace.transfer = "pq";
63+
break;
64+
case 7:
65+
colorSpace.transfer = "hlg";
66+
break;
67+
default:
68+
break;
69+
}
70+
if (range == 1 || range == 2) {
71+
colorSpace.fullRange = range == 1;
72+
}
73+
return colorSpace;
74+
}
75+
2276
struct ReaderOwner {
2377
AImageReader* reader;
2478
explicit ReaderOwner(AImageReader* reader) : reader(reader) {}
@@ -361,8 +415,11 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
361415
while (draining()) {
362416
AMediaCodecBufferInfo info{};
363417
ssize_t index = AMediaCodec_dequeueOutputBuffer(_codec, &info, 10000);
364-
if (index == AMEDIACODEC_INFO_TRY_AGAIN_LATER || index == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED ||
365-
index == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
418+
if (index == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
419+
readColorSpace();
420+
continue;
421+
}
422+
if (index == AMEDIACODEC_INFO_TRY_AGAIN_LATER || index == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
366423
continue;
367424
}
368425
if (isCodecError(index)) {
@@ -486,10 +543,35 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
486543
// picture is sampled through the buffer's own YCbCr conversion.
487544
auto surface = std::make_shared<HybridNativeSurface>(ref, -1, static_cast<uint32_t>(std::max(visibleWidth, 0)),
488545
static_cast<uint32_t>(std::max(visibleHeight, 0)), "NV12");
489-
_output(NativeDecodedFrame(surface, static_cast<double>(pts), duration));
546+
std::optional<NativeColorSpace> colorSpace;
547+
{
548+
std::lock_guard<std::mutex> lock(_mutex);
549+
colorSpace = _colorSpace;
550+
}
551+
_output(NativeDecodedFrame(surface, static_cast<double>(pts), duration, colorSpace));
490552
countDelivered();
491553
}
492554

555+
// On the drain thread: the color space of the pictures to come, from the
556+
// output format's color aspects (the stream's VUI). Pictures come out after
557+
// the format change that describes them.
558+
void readColorSpace() {
559+
AMediaFormat* format = AMediaCodec_getOutputFormat(_codec);
560+
if (!format) {
561+
return;
562+
}
563+
int32_t standard = 0;
564+
int32_t transfer = 0;
565+
int32_t range = 0;
566+
AMediaFormat_getInt32(format, "color-standard", &standard);
567+
AMediaFormat_getInt32(format, "color-transfer", &transfer);
568+
AMediaFormat_getInt32(format, "color-range", &range);
569+
AMediaFormat_delete(format);
570+
auto colorSpace = colorSpaceOf(standard, transfer, range);
571+
std::lock_guard<std::mutex> lock(_mutex);
572+
_colorSpace = colorSpace;
573+
}
574+
493575
double takeDuration(int64_t ts) {
494576
std::lock_guard<std::mutex> lock(_mutex);
495577
auto it = _durations.find(ts);
@@ -576,6 +658,8 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
576658
std::mutex _mutex;
577659
std::condition_variable _cv;
578660
std::map<int64_t, double> _durations;
661+
// The stream's color space, as the decoder last reported it.
662+
std::optional<NativeColorSpace> _colorSpace;
579663
int64_t _rendered = 0;
580664
int64_t _delivered = 0;
581665
int64_t _acquired = 0;

‎docs/guides.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ frame.close();
160160
| Android | The decoder's own YUV (NV12) `AHardwareBuffer`. It can be wider or taller than the frame (software decoders pad the width to a multiple of 128): the picture is its top-left `codedWidth` x `codedHeight`, so sample only that. |
161161

162162
The frame's `colorSpace` comes from the stream (Mediabunny passes the
163-
track's to `configure`). Android's buffers carry no color information of
164-
their own, so pass it to `createNativeBufferFromPointer`: without it,
163+
track's to `configure`; on Android, what the config leaves out is what the
164+
decoder reads in the stream). Android's buffers carry no color information
165+
of their own, so pass it to `createNativeBufferFromPointer`: without it,
165166
BT.709 video is read with the BT.601 matrix, and pure red comes out as
166167
(225, 0, 31). The option takes react-native-webgpu 0.26.0 or later, and
167168
names color spaces as Dawn does (`"rec709"`, `range: "narrow"`), not as

‎ios/VideoDecoder.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ final class HybridNativeVideoDecoder: HybridNativeVideoDecoderSpec {
554554
guard let imageBuffer else { return } // dropped
555555
let ts = pts.isValid ? Double(CMTimeConvertScale(pts, timescale: 1_000_000, method: .roundHalfAwayFromZero).value) : timestamp
556556
let d = dur.isValid ? Double(CMTimeConvertScale(dur, timescale: 1_000_000, method: .roundHalfAwayFromZero).value) : duration
557-
self.hold(NativeDecodedFrame(surface: HybridNativeSurface(pixelBuffer: imageBuffer), timestamp: ts, duration: d))
557+
self.hold(NativeDecodedFrame(surface: HybridNativeSurface(pixelBuffer: imageBuffer), timestamp: ts, duration: d, colorSpace: nil))
558558
}
559559
if status != noErr && status != kVTVideoDecoderReferenceMissingErr {
560560
self.fail(HybridNativeVideoEncoder.describe(status).replacingOccurrences(of: "encoder", with: "decoder"))

‎nitrogen/generated/ios/NitroWebCodecs-Swift-Cxx-Bridge.hpp‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎nitrogen/generated/ios/c++/HybridWebCodecsSpecSwift.hpp‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎nitrogen/generated/ios/swift/NativeDecodedFrame.swift‎

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎nitrogen/generated/shared/c++/NativeDecodedFrame.hpp‎

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)