Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 87 additions & 3 deletions android/src/main/cpp/VideoDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <map>

#include "HybridNativeVideoDecoderSpec.hpp"
#include "NativeColorSpace.hpp"
#include "NativeDecodedFrame.hpp"
#include "Capabilities.hpp"
#include "NativeVideoDecoderConfig.hpp"
Expand All @@ -19,6 +20,59 @@

namespace margelo::nitro::webcodecs {

// MediaFormat's color aspects (KEY_COLOR_STANDARD, _TRANSFER, _RANGE) as a
// WebCodecs color space. 0 (unspecified) and values WebCodecs has no name for
// leave the member out. SDR video's transfer is BT.601's or BT.709's curve,
// the same one, named after the standard.
inline NativeColorSpace colorSpaceOf(int32_t standard, int32_t transfer, int32_t range) {
NativeColorSpace colorSpace;
bool bt601 = false;
switch (standard) {
case 1: // BT.709
colorSpace.primaries = "bt709";
colorSpace.matrix = "bt709";
break;
case 2: // BT.601 625 (PAL)
case 3: // BT.601 625, unadjusted
colorSpace.primaries = "bt470bg";
colorSpace.matrix = "bt470bg";
bt601 = true;
break;
case 4: // BT.601 525 (NTSC)
case 5: // BT.601 525, unadjusted
colorSpace.primaries = "smpte170m";
colorSpace.matrix = "smpte170m";
bt601 = true;
break;
case 6: // BT.2020
colorSpace.primaries = "bt2020";
colorSpace.matrix = "bt2020-ncl";
break;
default:
break;
}
switch (transfer) {
case 1:
colorSpace.transfer = "linear";
break;
case 3: // SDR video
colorSpace.transfer = bt601 ? "smpte170m" : "bt709";
break;
case 6:
colorSpace.transfer = "pq";
break;
case 7:
colorSpace.transfer = "hlg";
break;
default:
break;
}
if (range == 1 || range == 2) {
colorSpace.fullRange = range == 1;
}
return colorSpace;
}

struct ReaderOwner {
AImageReader* reader;
explicit ReaderOwner(AImageReader* reader) : reader(reader) {}
Expand Down Expand Up @@ -361,8 +415,11 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
while (draining()) {
AMediaCodecBufferInfo info{};
ssize_t index = AMediaCodec_dequeueOutputBuffer(_codec, &info, 10000);
if (index == AMEDIACODEC_INFO_TRY_AGAIN_LATER || index == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED ||
index == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
if (index == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
readColorSpace();
continue;
}
if (index == AMEDIACODEC_INFO_TRY_AGAIN_LATER || index == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
continue;
}
if (isCodecError(index)) {
Expand Down Expand Up @@ -486,10 +543,35 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
// picture is sampled through the buffer's own YCbCr conversion.
auto surface = std::make_shared<HybridNativeSurface>(ref, -1, static_cast<uint32_t>(std::max(visibleWidth, 0)),
static_cast<uint32_t>(std::max(visibleHeight, 0)), "NV12");
_output(NativeDecodedFrame(surface, static_cast<double>(pts), duration));
std::optional<NativeColorSpace> colorSpace;
{
std::lock_guard<std::mutex> lock(_mutex);
colorSpace = _colorSpace;
}
_output(NativeDecodedFrame(surface, static_cast<double>(pts), duration, colorSpace));
countDelivered();
}

// On the drain thread: the color space of the pictures to come, from the
// output format's color aspects (the stream's VUI). Pictures come out after
// the format change that describes them.
void readColorSpace() {
AMediaFormat* format = AMediaCodec_getOutputFormat(_codec);
if (!format) {
return;
}
int32_t standard = 0;
int32_t transfer = 0;
int32_t range = 0;
AMediaFormat_getInt32(format, "color-standard", &standard);
AMediaFormat_getInt32(format, "color-transfer", &transfer);
AMediaFormat_getInt32(format, "color-range", &range);
AMediaFormat_delete(format);
auto colorSpace = colorSpaceOf(standard, transfer, range);
std::lock_guard<std::mutex> lock(_mutex);
_colorSpace = colorSpace;
}

double takeDuration(int64_t ts) {
std::lock_guard<std::mutex> lock(_mutex);
auto it = _durations.find(ts);
Expand Down Expand Up @@ -576,6 +658,8 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec {
std::mutex _mutex;
std::condition_variable _cv;
std::map<int64_t, double> _durations;
// The stream's color space, as the decoder last reported it.
std::optional<NativeColorSpace> _colorSpace;
int64_t _rendered = 0;
int64_t _delivered = 0;
int64_t _acquired = 0;
Expand Down
5 changes: 3 additions & 2 deletions docs/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ frame.close();
| 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. |

The frame's `colorSpace` comes from the stream (Mediabunny passes the
track's to `configure`). Android's buffers carry no color information of
their own, so pass it to `createNativeBufferFromPointer`: without it,
track's to `configure`; on Android, what the config leaves out is what the
decoder reads in the stream). Android's buffers carry no color information
of their own, so pass it to `createNativeBufferFromPointer`: without it,
BT.709 video is read with the BT.601 matrix, and pure red comes out as
(225, 0, 31). The option takes react-native-webgpu 0.26.0 or later, and
names color spaces as Dawn does (`"rec709"`, `range: "narrow"`), not as
Expand Down
2 changes: 1 addition & 1 deletion ios/VideoDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ final class HybridNativeVideoDecoder: HybridNativeVideoDecoderSpec {
guard let imageBuffer else { return } // dropped
let ts = pts.isValid ? Double(CMTimeConvertScale(pts, timescale: 1_000_000, method: .roundHalfAwayFromZero).value) : timestamp
let d = dur.isValid ? Double(CMTimeConvertScale(dur, timescale: 1_000_000, method: .roundHalfAwayFromZero).value) : duration
self.hold(NativeDecodedFrame(surface: HybridNativeSurface(pixelBuffer: imageBuffer), timestamp: ts, duration: d))
self.hold(NativeDecodedFrame(surface: HybridNativeSurface(pixelBuffer: imageBuffer), timestamp: ts, duration: d, colorSpace: nil))
}
if status != noErr && status != kVTVideoDecoderReferenceMissingErr {
self.fail(HybridNativeVideoEncoder.describe(status).replacingOccurrences(of: "encoder", with: "decoder"))
Expand Down
18 changes: 18 additions & 0 deletions nitrogen/generated/ios/NitroWebCodecs-Swift-Cxx-Bridge.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions nitrogen/generated/ios/c++/HybridWebCodecsSpecSwift.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions nitrogen/generated/ios/swift/NativeDecodedFrame.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions nitrogen/generated/shared/c++/NativeDecodedFrame.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading