|
11 | 11 | #include <map> |
12 | 12 |
|
13 | 13 | #include "HybridNativeVideoDecoderSpec.hpp" |
| 14 | +#include "NativeColorSpace.hpp" |
14 | 15 | #include "NativeDecodedFrame.hpp" |
15 | 16 | #include "Capabilities.hpp" |
16 | 17 | #include "NativeVideoDecoderConfig.hpp" |
|
19 | 20 |
|
20 | 21 | namespace margelo::nitro::webcodecs { |
21 | 22 |
|
| 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 | + |
22 | 76 | struct ReaderOwner { |
23 | 77 | AImageReader* reader; |
24 | 78 | explicit ReaderOwner(AImageReader* reader) : reader(reader) {} |
@@ -361,8 +415,11 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { |
361 | 415 | while (draining()) { |
362 | 416 | AMediaCodecBufferInfo info{}; |
363 | 417 | 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) { |
366 | 423 | continue; |
367 | 424 | } |
368 | 425 | if (isCodecError(index)) { |
@@ -486,10 +543,35 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { |
486 | 543 | // picture is sampled through the buffer's own YCbCr conversion. |
487 | 544 | auto surface = std::make_shared<HybridNativeSurface>(ref, -1, static_cast<uint32_t>(std::max(visibleWidth, 0)), |
488 | 545 | 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)); |
490 | 552 | countDelivered(); |
491 | 553 | } |
492 | 554 |
|
| 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 | + |
493 | 575 | double takeDuration(int64_t ts) { |
494 | 576 | std::lock_guard<std::mutex> lock(_mutex); |
495 | 577 | auto it = _durations.find(ts); |
@@ -576,6 +658,8 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { |
576 | 658 | std::mutex _mutex; |
577 | 659 | std::condition_variable _cv; |
578 | 660 | std::map<int64_t, double> _durations; |
| 661 | + // The stream's color space, as the decoder last reported it. |
| 662 | + std::optional<NativeColorSpace> _colorSpace; |
579 | 663 | int64_t _rendered = 0; |
580 | 664 | int64_t _delivered = 0; |
581 | 665 | int64_t _acquired = 0; |
|
0 commit comments