Skip to content

Commit

Permalink
add color space in media info
Browse files Browse the repository at this point in the history
not complete.
wang-bin/mdk-sdk#265
  • Loading branch information
wang-bin committed Jan 3, 2025
1 parent 411c9dc commit bc83ae0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
32 changes: 31 additions & 1 deletion MediaInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
/*
* Copyright (c) 2019-2024 WangBin <wbsecg1 at gmail.com>
* Copyright (c) 2019-2025 WangBin <wbsecg1 at gmail.com>
*/
#include "mdk/c/MediaInfo.h"
#include "mdk/MediaInfo.h"
#include "mdk/VideoFormat.h"
#include "MediaInfoInternal.h"
#include <cassert>
#include <algorithm>

ColorSpace kColorSpaceMap[] = {
ColorSpaceUnknown,
ColorSpaceBT709,
ColorSpaceBT2100_PQ,
ColorSpaceSCRGB,
ColorSpaceExtendedLinearDisplayP3,
ColorSpaceExtendedSRGB,
ColorSpaceExtendedLinearSRGB,
ColorSpaceBT2100_HLG,
};

MDK_ColorSpace toC(const ColorSpace& cs)
{
const auto dist = [&](const ColorSpace& cs1, const ColorSpace& cs2) {
if (cs1 == cs2)
return 0;
if ((cs1.range == ColorSpace::Range::Extended || cs2.range == ColorSpace::Range::Extended) && cs1.range != cs2.range)
return 1000000;
if (cs1 == ColorSpaceUnknown || cs2 == ColorSpaceUnknown)
return 99;
return (cs2.primaries != cs1.primaries) * 100 + (cs2.transfer != cs1.transfer);
};
const auto it = std::min_element(std::begin(kColorSpaceMap), std::end(kColorSpaceMap), [&](const ColorSpace& cs1, const ColorSpace& cs2) {
return dist(cs1, cs) < dist(cs2, cs);
});
return MDK_ColorSpace(std::distance(std::begin(kColorSpaceMap), it));
}

static void from_abi(const AudioCodecParameters& in, mdkAudioCodecParameters& out)
{
Expand Down Expand Up @@ -58,6 +87,7 @@ static void from_abi(const VideoCodecParameters& in, mdkVideoCodecParameters& ou
out.height = in.height;
out.b_frames = in.b_frames;
out.par = in.par;
out.color_space = toC(in.color_space);
}

static void from_abi(const VideoStreamInfo& in, mdkVideoStreamInfo& out)
Expand Down
7 changes: 4 additions & 3 deletions include/mdk/c/MediaInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 WangBin <wbsecg1 at gmail.com>
* Copyright (c) 2019-2025 WangBin <wbsecg1 at gmail.com>
* This file is part of MDK
* MDK SDK: https://github.com/wang-bin/mdk-sdk
* Free for opensource softwares or non-commercial use.
Expand Down Expand Up @@ -34,7 +34,7 @@ typedef struct mdkAudioCodecParameters {
int block_align;
int frame_size; /* const samples per channel in a frame */

char reserved[128]; /* color info etc. */
char reserved[128];
} mdkAudioCodecParameters;

typedef struct mdkAudioStreamInfo {
Expand Down Expand Up @@ -67,7 +67,8 @@ typedef struct mdkVideoCodecParameters {
int b_frames;

float par;
char reserved[128];
enum MDK_ColorSpace color_space;
char reserved[124];
} mdkVideoCodecParameters;

typedef struct mdkVideoStreamInfo {
Expand Down
3 changes: 2 additions & 1 deletion include/mdk/c/global.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024 WangBin <wbsecg1 at gmail.com>
* Copyright (c) 2019-2025 WangBin <wbsecg1 at gmail.com>
* This file is part of MDK
* MDK SDK: https://github.com/wang-bin/mdk-sdk
* Free for opensource softwares or non-commercial use.
Expand Down Expand Up @@ -145,6 +145,7 @@ enum MDK_ColorSpace {
MDK_ColorSpace_ExtendedLinearDisplayP3,
MDK_ColorSpace_ExtendedSRGB,
MDK_ColorSpace_ExtendedLinearSRGB,
MDK_ColorSpace_BT2100_HLG,
};

MDK_API int MDK_version();
Expand Down
3 changes: 2 additions & 1 deletion include/mdk/cpp/MediaInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 WangBin <wbsecg1 at gmail.com>
* Copyright (c) 2019-2025 WangBin <wbsecg1 at gmail.com>
* This file is part of MDK
* MDK SDK: https://github.com/wang-bin/mdk-sdk
* Free for opensource softwares or non-commercial use.
Expand Down Expand Up @@ -64,6 +64,7 @@ struct VideoCodecParameters {
int height;
int b_frames;
float par;
ColorSpace color_space;
};

struct VideoStreamInfo {
Expand Down
3 changes: 2 additions & 1 deletion include/mdk/cpp/global.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024 WangBin <wbsecg1 at gmail.com>
* Copyright (c) 2019-2025 WangBin <wbsecg1 at gmail.com>
* This file is part of MDK
* MDK SDK: https://github.com/wang-bin/mdk-sdk
* Free for opensource softwares or non-commercial use.
Expand Down Expand Up @@ -353,6 +353,7 @@ enum ColorSpace {
ColorSpaceExtendedSRGB,
// linear sRGB in extended component range. Display-referred white level
ColorSpaceExtendedLinearSRGB,
ColorSpaceBT2100_HLG,
};

MDK_NS_END

0 comments on commit bc83ae0

Please sign in to comment.