From 0f915d7d5bd972727e38a509a64a62e917e5b066 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 10 Sep 2026 11:17:30 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=83=E7=B4=A0=E6=9D=90=E3=81=AE=20YUV=20?= =?UTF-8?q?=E3=82=92=E3=81=9D=E3=81=AE=E3=81=BE=E3=81=BE=E6=A0=BC=E7=B4=8D?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++ src/ColorConvert.cpp | 144 ++++++++++++++----------------------------- src/ColorConvert.h | 10 +-- src/JpegCodec.cpp | 140 +++++++++++++++++------------------------ src/JpegCodec.h | 4 +- src/ProxyBuilder.cpp | 14 +++-- src/ProxyFormat.h | 2 +- 7 files changed, 122 insertions(+), 198 deletions(-) diff --git a/README.md b/README.md index e0d6394..7d2b13d 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,12 @@ ## 仕組み +### 色の扱い + +元素材の Y/Cb/Cr をそのまま JPEG の成分として格納し、そのまま取り出します。RGB を経由せず、色行列も範囲も一切仮定しません。BT.709 でも BT.601 でも、リミテッドでもフルレンジでも、元素材の値がそのまま通ります。 + +フルレンジの素材で確かめた結果です。元素材の輝度が 0〜255、平均 127.25。プロキシに格納された輝度も 0〜255、平均 127.25。同じ大きさに揃えて比べた平均誤差は 0.06(JPEG の圧縮による損失のみ)。 + ### プロキシの形式 独自の容器に、フレームごとの JPEG と索引を入れます。索引は先頭に固定長で置くため、作成しながら読むことができます。ヘッダーには元素材のパス・サイズ・更新時刻を持ち、素材が入れ替わっていれば作り直します。 diff --git a/src/ColorConvert.cpp b/src/ColorConvert.cpp index 6c93bff..2512a16 100644 --- a/src/ColorConvert.cpp +++ b/src/ColorConvert.cpp @@ -6,14 +6,6 @@ namespace pe { namespace { -int g_luma_red[256], g_luma_green[256], g_luma_blue[256]; -int g_blue_red[256], g_blue_green[256], g_blue_blue[256]; -int g_red_red[256], g_red_green[256], g_red_blue[256]; -int g_jpeg_luma[256], g_jpeg_luma_blue[256], g_jpeg_luma_red[256]; -int g_jpeg_blue_blue[256], g_jpeg_blue_red[256]; -int g_jpeg_red_blue[256], g_jpeg_red_red[256]; -bool g_ready = false; - inline unsigned char Clamp(int value) { value >>= 8; if (value < 0) return 0; @@ -33,70 +25,20 @@ std::vector BuildColumnMap(int source_width, int width) { } -void InitColorTables() { - if (g_ready) return; - for (int index = 0; index < 256; index++) { - g_luma_red[index] = (int)(0.1826 * 256.0 * index); - g_luma_green[index] = (int)(0.6142 * 256.0 * index); - g_luma_blue[index] = (int)(0.0620 * 256.0 * index) + (16 << 8); - g_blue_red[index] = (int)(-0.1006 * 256.0 * index); - g_blue_green[index] = (int)(-0.3386 * 256.0 * index); - g_blue_blue[index] = (int)(0.4392 * 256.0 * index) + (128 << 8); - g_red_red[index] = (int)(0.4392 * 256.0 * index); - g_red_green[index] = (int)(-0.3989 * 256.0 * index); - g_red_blue[index] = (int)(-0.0403 * 256.0 * index) + (128 << 8); - double centered = index - 128.0; - g_jpeg_luma[index] = (int)(0.8588 * 256.0 * index) + (16 << 8); - g_jpeg_luma_blue[index] = (int)(-0.101503 * 256.0 * centered); - g_jpeg_luma_red[index] = (int)(-0.182617 * 256.0 * centered); - g_jpeg_blue_blue[index] = (int)(0.894787 * 256.0 * centered) + (128 << 8); - g_jpeg_blue_red[index] = (int)(0.100766 * 256.0 * centered); - g_jpeg_red_blue[index] = (int)(0.065864 * 256.0 * centered); - g_jpeg_red_red[index] = (int)(0.900627 * 256.0 * centered) + (128 << 8); - } - g_ready = true; -} - -void BgraToYuy2(const unsigned char* source, int source_width, int source_height, int source_stride, - unsigned char* destination, int width, int height, int stride) { - InitColorTables(); - if (source_width <= 0 || source_height <= 0 || width <= 1 || height <= 0) return; - std::vector columns = BuildColumnMap(source_width, width); - const int pairs = (int)columns.size(); - int built = -1; - unsigned char* built_row = nullptr; - for (int y = 0; y < height; y++) { - long long mapped = (long long)y * source_height / height; - if (mapped >= source_height) mapped = source_height - 1; - unsigned char* target = destination + (size_t)y * stride; - if ((int)mapped == built && built_row) { - memcpy(target, built_row, (size_t)width * 2); - continue; - } - const unsigned char* row = source + (size_t)mapped * source_stride; - for (int index = 0; index < pairs; index++) { - const unsigned char* pixel = row + (size_t)columns[(size_t)index] * 4; - int blue = pixel[0], green = pixel[1], red = pixel[2]; - unsigned char luma = Clamp(g_luma_red[red] + g_luma_green[green] + g_luma_blue[blue]); - target[index * 4 + 0] = luma; - target[index * 4 + 1] = Clamp(g_blue_red[red] + g_blue_green[green] + g_blue_blue[blue]); - target[index * 4 + 2] = luma; - target[index * 4 + 3] = Clamp(g_red_red[red] + g_red_green[green] + g_red_blue[blue]); - } - built = (int)mapped; - built_row = target; - } -} - void PlanarToYuy2(const unsigned char* luma, int luma_stride, const unsigned char* blue, const unsigned char* red, int chroma_stride, int chroma_width, int chroma_height, int source_width, int source_height, unsigned char* destination, int width, int height, int stride) { - InitColorTables(); if (source_width <= 0 || source_height <= 0 || width <= 1 || height <= 0) return; if (chroma_width <= 0 || chroma_height <= 0) return; std::vector columns = BuildColumnMap(source_width, width); const int pairs = (int)columns.size(); + std::vector luma_columns((size_t)pairs * 2); + for (int index = 0; index < pairs * 2; index++) { + long long position = (long long)index * source_width / (width > 0 ? width : 1); + if (position >= source_width) position = source_width - 1; + luma_columns[(size_t)index] = (int)position; + } std::vector chroma_columns((size_t)pairs); for (int index = 0; index < pairs; index++) { long long position = (long long)columns[(size_t)index] * chroma_width / source_width; @@ -119,61 +61,67 @@ void PlanarToYuy2(const unsigned char* luma, int luma_stride, const unsigned cha const unsigned char* blue_line = blue + (ptrdiff_t)chroma_row * chroma_stride; const unsigned char* red_line = red + (ptrdiff_t)chroma_row * chroma_stride; for (int index = 0; index < pairs; index++) { - int column = columns[(size_t)index]; - int chroma_column = chroma_columns[(size_t)index]; - int blue_value = blue_line[chroma_column]; - int red_value = red_line[chroma_column]; - unsigned char converted = Clamp(g_jpeg_luma[luma_line[column]] + - g_jpeg_luma_blue[blue_value] + g_jpeg_luma_red[red_value]); - target[index * 4 + 0] = converted; - target[index * 4 + 1] = Clamp(g_jpeg_blue_blue[blue_value] + g_jpeg_blue_red[red_value]); - target[index * 4 + 2] = converted; - target[index * 4 + 3] = Clamp(g_jpeg_red_blue[blue_value] + g_jpeg_red_red[red_value]); + const int chroma_column = chroma_columns[(size_t)index]; + target[index * 4 + 0] = luma_line[luma_columns[(size_t)index * 2]]; + target[index * 4 + 1] = blue_line[chroma_column]; + target[index * 4 + 2] = luma_line[luma_columns[(size_t)index * 2 + 1]]; + target[index * 4 + 3] = red_line[chroma_column]; } built = (int)mapped; built_row = target; } } -void Yuy2ToBgrScaled(const unsigned char* source, int source_width, int source_height, - int source_stride, unsigned char* destination, int width, int height) { - if (source_width <= 1 || source_height <= 0 || width <= 0 || height <= 0) return; +void Yuy2ToPlanarScaled(const unsigned char* source, int source_width, int source_height, + int source_stride, unsigned char* luma, unsigned char* blue, + unsigned char* red, int width, int height) { + if (source_width <= 1 || source_height <= 0 || width <= 1 || height <= 0) return; + const int chroma_width = width / 2; + const int source_pairs = source_width / 2; for (int y = 0; y < height; y++) { int top = (int)((long long)y * source_height / height); int bottom = (int)((long long)(y + 1) * source_height / height); if (bottom <= top) bottom = top + 1; if (bottom > source_height) bottom = source_height; - unsigned char* target = destination + (size_t)y * width * 3; + unsigned char* luma_row = luma + (size_t)y * width; + unsigned char* blue_row = blue + (size_t)y * chroma_width; + unsigned char* red_row = red + (size_t)y * chroma_width; + for (int x = 0; x < width; x++) { int left = (int)((long long)x * source_width / width); int right = (int)((long long)(x + 1) * source_width / width); if (right <= left) right = left + 1; if (right > source_width) right = source_width; - int luma_total = 0, blue_total = 0, red_total = 0, luma_count = 0, chroma_count = 0; + int total = 0; + int count = 0; for (int row = top; row < bottom; row++) { const unsigned char* line = source + (size_t)row * source_stride; for (int column = left; column < right; column++) { - luma_total += line[column * 2]; - luma_count++; - int pair = column & ~1; - blue_total += line[pair * 2 + 1]; - red_total += line[pair * 2 + 3]; - chroma_count++; + total += line[(size_t)column * 2]; + count++; } } - if (luma_count == 0 || chroma_count == 0) continue; - double luma = 1.1644 * ((double)luma_total / luma_count - 16.0); - double blue = (double)blue_total / chroma_count - 128.0; - double red = (double)red_total / chroma_count - 128.0; - double to_red = 1.7927 * red; - double to_green = -0.2132 * blue - 0.5329 * red; - double to_blue = 2.1124 * blue; - auto clamp = [](double value) { - return (unsigned char)(value < 0 ? 0 : (value > 255 ? 255 : value)); - }; - target[x * 3 + 0] = clamp(luma + to_blue); - target[x * 3 + 1] = clamp(luma + to_green); - target[x * 3 + 2] = clamp(luma + to_red); + luma_row[x] = count > 0 ? (unsigned char)(total / count) : 0; + } + + for (int x = 0; x < chroma_width; x++) { + int left = (int)((long long)x * source_pairs / chroma_width); + int right = (int)((long long)(x + 1) * source_pairs / chroma_width); + if (right <= left) right = left + 1; + if (right > source_pairs) right = source_pairs; + int blue_total = 0; + int red_total = 0; + int count = 0; + for (int row = top; row < bottom; row++) { + const unsigned char* line = source + (size_t)row * source_stride; + for (int pair = left; pair < right; pair++) { + blue_total += line[(size_t)pair * 4 + 1]; + red_total += line[(size_t)pair * 4 + 3]; + count++; + } + } + blue_row[x] = count > 0 ? (unsigned char)(blue_total / count) : 128; + red_row[x] = count > 0 ? (unsigned char)(red_total / count) : 128; } } } diff --git a/src/ColorConvert.h b/src/ColorConvert.h index fcb6292..af7c2e2 100644 --- a/src/ColorConvert.h +++ b/src/ColorConvert.h @@ -7,18 +7,14 @@ namespace pe { -void InitColorTables(); - -void BgraToYuy2(const unsigned char* source, int source_width, int source_height, int source_stride, - unsigned char* destination, int width, int height, int stride); - void PlanarToYuy2(const unsigned char* luma, int luma_stride, const unsigned char* blue, const unsigned char* red, int chroma_stride, int chroma_width, int chroma_height, int source_width, int source_height, unsigned char* destination, int width, int height, int stride); -void Yuy2ToBgrScaled(const unsigned char* source, int source_width, int source_height, - int source_stride, unsigned char* destination, int width, int height); +void Yuy2ToPlanarScaled(const unsigned char* source, int source_width, int source_height, + int source_stride, unsigned char* luma, unsigned char* blue, + unsigned char* red, int width, int height); void FillBlackYuy2(unsigned char* destination, int width, int height, int stride); diff --git a/src/JpegCodec.cpp b/src/JpegCodec.cpp index 20475a7..9ff51c0 100644 --- a/src/JpegCodec.cpp +++ b/src/JpegCodec.cpp @@ -73,98 +73,72 @@ bool DecodePlanar(IWICBitmapFrameDecode* frame, int proxy_width, int proxy_heigh return done; } -bool DecodeThroughBgra(IWICImagingFactory* imaging, IWICBitmapFrameDecode* frame, int proxy_width, - int proxy_height, unsigned char* destination, int width, int height, int stride, - std::vector& scratch) { - IWICBitmapSource* source = frame; - IWICBitmapScaler* scaler = nullptr; - UINT actual_width = 0, actual_height = 0; - frame->GetSize(&actual_width, &actual_height); - if ((int)actual_width != proxy_width || (int)actual_height != proxy_height) { - if (SUCCEEDED(imaging->CreateBitmapScaler(&scaler)) && - SUCCEEDED(scaler->Initialize(frame, proxy_width, proxy_height, - WICBitmapInterpolationModeLinear))) { - source = scaler; - } - } - bool done = false; - IWICBitmapSource* converted = nullptr; - if (SUCCEEDED(WICConvertBitmapSource(GUID_WICPixelFormat32bppBGRA, source, &converted))) { - const int row = proxy_width * 4; - scratch.resize((size_t)row * proxy_height); - if (SUCCEEDED(converted->CopyPixels(nullptr, row, (UINT)scratch.size(), scratch.data()))) { - BgraToYuy2(scratch.data(), proxy_width, proxy_height, row, destination, width, height, stride); - done = true; - } - converted->Release(); - } - if (scaler) scaler->Release(); - return done; } -} - -bool EncodeJpeg(const unsigned char* bgr, int width, int height, int out_width, int out_height, - int quality, std::vector& out) { +bool EncodeJpegPlanar(const unsigned char* luma, const unsigned char* blue, const unsigned char* red, + int width, int height, int quality, std::vector& out) { IWICImagingFactory* imaging = Imaging(); - if (!imaging) return false; - IWICBitmap* bitmap = nullptr; - if (FAILED(imaging->CreateBitmapFromMemory(width, height, GUID_WICPixelFormat24bppBGR, width * 3, - (UINT)((size_t)width * height * 3), (BYTE*)bgr, &bitmap))) { - return false; - } - IWICBitmapSource* source = bitmap; - IWICBitmapScaler* scaler = nullptr; - if (out_width != width || out_height != height) { - if (SUCCEEDED(imaging->CreateBitmapScaler(&scaler)) && - SUCCEEDED(scaler->Initialize(bitmap, out_width, out_height, WICBitmapInterpolationModeFant))) { - source = scaler; - } - } + if (!imaging || width <= 1 || height <= 0) return false; + const int chroma_width = width / 2; bool done = false; IStream* stream = nullptr; - if (SUCCEEDED(CreateStreamOnHGlobal(nullptr, TRUE, &stream))) { - IWICBitmapEncoder* encoder = nullptr; - if (SUCCEEDED(imaging->CreateEncoder(GUID_ContainerFormatJpeg, nullptr, &encoder)) && - SUCCEEDED(encoder->Initialize(stream, WICBitmapEncoderNoCache))) { - IWICBitmapFrameEncode* frame = nullptr; - IPropertyBag2* options = nullptr; - if (SUCCEEDED(encoder->CreateNewFrame(&frame, &options))) { - PROPBAG2 option{}; - option.pstrName = (LPOLESTR)L"ImageQuality"; - VARIANT value{}; - value.vt = VT_R4; - value.fltVal = (float)quality / 100.0f; - options->Write(1, &option, &value); - if (SUCCEEDED(frame->Initialize(options)) && - SUCCEEDED(frame->SetSize(out_width, out_height))) { - WICPixelFormatGUID format = GUID_WICPixelFormat24bppBGR; - frame->SetPixelFormat(&format); - if (SUCCEEDED(frame->WriteSource(source, nullptr)) && SUCCEEDED(frame->Commit()) && - SUCCEEDED(encoder->Commit())) { - STATSTG status{}; - HGLOBAL memory = nullptr; - if (SUCCEEDED(stream->Stat(&status, STATFLAG_NONAME)) && - SUCCEEDED(GetHGlobalFromStream(stream, &memory))) { - SIZE_T used = (SIZE_T)status.cbSize.QuadPart; - if (used > 0 && used <= GlobalSize(memory)) { - void* address = GlobalLock(memory); - out.assign((unsigned char*)address, (unsigned char*)address + used); - GlobalUnlock(memory); - done = true; - } + if (FAILED(CreateStreamOnHGlobal(nullptr, TRUE, &stream))) return false; + IWICBitmapEncoder* encoder = nullptr; + if (SUCCEEDED(imaging->CreateEncoder(GUID_ContainerFormatJpeg, nullptr, &encoder)) && + SUCCEEDED(encoder->Initialize(stream, WICBitmapEncoderNoCache))) { + IWICBitmapFrameEncode* frame = nullptr; + IPropertyBag2* options = nullptr; + if (SUCCEEDED(encoder->CreateNewFrame(&frame, &options))) { + PROPBAG2 names[2]{}; + VARIANT values[2]{}; + names[0].pstrName = (LPOLESTR)L"ImageQuality"; + values[0].vt = VT_R4; + values[0].fltVal = (float)quality / 100.0f; + names[1].pstrName = (LPOLESTR)L"JpegYCrCbSubsampling"; + values[1].vt = VT_UI1; + values[1].bVal = (BYTE)WICJpegYCrCbSubsampling422; + options->Write(2, names, values); + IWICPlanarBitmapFrameEncode* planar = nullptr; + WICPixelFormatGUID format = GUID_WICPixelFormat24bppBGR; + if (SUCCEEDED(frame->Initialize(options)) && SUCCEEDED(frame->SetSize(width, height)) && + SUCCEEDED(frame->SetPixelFormat(&format)) && + SUCCEEDED(frame->QueryInterface(IID_PPV_ARGS(&planar)))) { + WICBitmapPlane planes[3]{}; + planes[0].Format = GUID_WICPixelFormat8bppY; + planes[0].pbBuffer = (BYTE*)luma; + planes[0].cbStride = (UINT)width; + planes[0].cbBufferSize = (UINT)((size_t)width * height); + planes[1].Format = GUID_WICPixelFormat8bppCb; + planes[1].pbBuffer = (BYTE*)blue; + planes[1].cbStride = (UINT)chroma_width; + planes[1].cbBufferSize = (UINT)((size_t)chroma_width * height); + planes[2].Format = GUID_WICPixelFormat8bppCr; + planes[2].pbBuffer = (BYTE*)red; + planes[2].cbStride = (UINT)chroma_width; + planes[2].cbBufferSize = (UINT)((size_t)chroma_width * height); + if (SUCCEEDED(planar->WritePixels((UINT)height, planes, 3)) && + SUCCEEDED(frame->Commit()) && SUCCEEDED(encoder->Commit())) { + STATSTG status{}; + HGLOBAL memory = nullptr; + if (SUCCEEDED(stream->Stat(&status, STATFLAG_NONAME)) && + SUCCEEDED(GetHGlobalFromStream(stream, &memory))) { + SIZE_T used = (SIZE_T)status.cbSize.QuadPart; + if (used > 0 && used <= GlobalSize(memory)) { + void* address = GlobalLock(memory); + out.assign((unsigned char*)address, (unsigned char*)address + used); + GlobalUnlock(memory); + done = true; } } } - if (options) options->Release(); - if (frame) frame->Release(); } + if (planar) planar->Release(); + if (options) options->Release(); + if (frame) frame->Release(); } - if (encoder) encoder->Release(); - stream->Release(); } - if (scaler) scaler->Release(); - bitmap->Release(); + if (encoder) encoder->Release(); + stream->Release(); return done; } @@ -184,10 +158,6 @@ bool DecodeJpegToYuy2(const unsigned char* data, size_t size, int proxy_width, i thread_local std::vector scratch; done = DecodePlanar(frame, proxy_width, proxy_height, destination, width, height, stride, scratch); - if (!done) { - done = DecodeThroughBgra(imaging, frame, proxy_width, proxy_height, destination, width, - height, stride, scratch); - } frame->Release(); } decoder->Release(); diff --git a/src/JpegCodec.h b/src/JpegCodec.h index 1060b77..0d2db27 100644 --- a/src/JpegCodec.h +++ b/src/JpegCodec.h @@ -7,8 +7,8 @@ namespace pe { -bool EncodeJpeg(const unsigned char* bgr, int width, int height, int out_width, int out_height, - int quality, std::vector& out); +bool EncodeJpegPlanar(const unsigned char* luma, const unsigned char* blue, const unsigned char* red, + int width, int height, int quality, std::vector& out); bool DecodeJpegToYuy2(const unsigned char* data, size_t size, int proxy_width, int proxy_height, unsigned char* destination, int width, int height, int stride); diff --git a/src/ProxyBuilder.cpp b/src/ProxyBuilder.cpp index 71a1477..366a174 100644 --- a/src/ProxyBuilder.cpp +++ b/src/ProxyBuilder.cpp @@ -154,7 +154,10 @@ void ProcessChunk(const JobPointer& job, int chunk) { const int end = std::min(begin + header.chunk_frames, header.frame_count); const int stride = header.source_width * 2; std::vector picture((size_t)stride * header.source_height); - std::vector reduced((size_t)header.proxy_width * header.proxy_height * 3); + const int chroma_width = header.proxy_width / 2; + std::vector luma((size_t)header.proxy_width * header.proxy_height); + std::vector blue((size_t)chroma_width * header.proxy_height); + std::vector red((size_t)chroma_width * header.proxy_height); std::vector encoded; auto started = std::chrono::steady_clock::now(); @@ -165,10 +168,11 @@ void ProcessChunk(const JobPointer& job, int chunk) { FailJob(job, L"復号できないフレームがありました"); break; } - Yuy2ToBgrScaled(picture.data(), header.source_width, header.source_height, stride, reduced.data(), - header.proxy_width, header.proxy_height); - if (!EncodeJpeg(reduced.data(), header.proxy_width, header.proxy_height, header.proxy_width, - header.proxy_height, header.quality, encoded)) { + Yuy2ToPlanarScaled(picture.data(), header.source_width, header.source_height, stride, + luma.data(), blue.data(), red.data(), header.proxy_width, + header.proxy_height); + if (!EncodeJpegPlanar(luma.data(), blue.data(), red.data(), header.proxy_width, + header.proxy_height, header.quality, encoded)) { FailJob(job, L"プロキシの符号化に失敗しました"); break; } diff --git a/src/ProxyFormat.h b/src/ProxyFormat.h index 9aa0f63..10f7f6c 100644 --- a/src/ProxyFormat.h +++ b/src/ProxyFormat.h @@ -37,7 +37,7 @@ struct ProxyIndexEntry { #pragma pack(pop) extern const char kProxyMagic[8]; -const int kProxyVersion = 1; +const int kProxyVersion = 2; bool ProxyHeaderIsValid(const ProxyHeader& header); long long ProxyIndexOffset(int frame);