| 1 |
/* |
| 2 |
This file is part of dec265, an example application using libde265. |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2013-2014 struktur AG, Dirk Farin <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "sdl.hh" |
| 28 |
#include <sstream> |
| 29 |
|
| 30 |
|
| 31 |
std::optional<std::string> SDL_YUV_Display::init(int frame_width, int frame_height, enum SDL_Chroma chroma, |
| 32 |
const char* window_title) |
| 33 |
{ |
| 34 |
// reduce image size to a multiple of 8 (apparently required by YUV overlay) |
| 35 |
|
| 36 |
frame_width &= ~7; |
| 37 |
frame_height &= ~7; |
| 38 |
|
| 39 |
mChroma = chroma; |
| 40 |
|
| 41 |
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
| 42 |
std::stringstream sstr; |
| 43 |
sstr << "SDL_Init() failed: " << SDL_GetError(); |
| 44 |
SDL_Quit(); |
| 45 |
return sstr.str(); |
| 46 |
} |
| 47 |
|
| 48 |
// set window title |
| 49 |
mWindow = SDL_CreateWindow(window_title, |
| 50 |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, |
| 51 |
frame_width, frame_height, 0); |
| 52 |
if (!mWindow) { |
| 53 |
std::stringstream sstr; |
| 54 |
sstr << "SDL: Couldn't set video mode to " << frame_width << "x" << frame_height << ": " << SDL_GetError(); |
| 55 |
SDL_Quit(); |
| 56 |
return sstr.str(); |
| 57 |
} |
| 58 |
|
| 59 |
Uint32 flags = 0; // Empty flags prioritize SDL_RENDERER_ACCELERATED. |
| 60 |
mRenderer = SDL_CreateRenderer(mWindow, -1, flags); |
| 61 |
if (!mRenderer) { |
| 62 |
std::stringstream sstr; |
| 63 |
sstr << "SDL: Couldn't create renderer: " << SDL_GetError(); |
| 64 |
SDL_Quit(); |
| 65 |
return sstr.str(); |
| 66 |
} |
| 67 |
|
| 68 |
Uint32 pixelFormat = 0; |
| 69 |
switch (mChroma) { |
| 70 |
case SDL_CHROMA_MONO: pixelFormat = SDL_PIXELFORMAT_YV12; break; |
| 71 |
case SDL_CHROMA_420: pixelFormat = SDL_PIXELFORMAT_YV12; break; |
| 72 |
case SDL_CHROMA_422: pixelFormat = SDL_PIXELFORMAT_YV12; break; |
| 73 |
case SDL_CHROMA_444: pixelFormat = SDL_PIXELFORMAT_YV12; break; |
| 74 |
default: { |
| 75 |
std::stringstream sstr; |
| 76 |
sstr << "Unsupported chroma: " << (int)mChroma; |
| 77 |
SDL_Quit(); |
| 78 |
return sstr.str(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
mTexture = SDL_CreateTexture(mRenderer, pixelFormat, |
| 83 |
SDL_TEXTUREACCESS_STREAMING, frame_width, frame_height); |
| 84 |
if (!mTexture ) { |
| 85 |
std::stringstream sstr; |
| 86 |
sstr << "SDL: Couldn't create SDL texture: " << SDL_GetError(); |
| 87 |
SDL_Quit(); |
| 88 |
return sstr.str(); |
| 89 |
} |
| 90 |
|
| 91 |
rect.x = 0; |
| 92 |
rect.y = 0; |
| 93 |
rect.w = frame_width; |
| 94 |
rect.h = frame_height; |
| 95 |
|
| 96 |
mWindowOpen=true; |
| 97 |
|
| 98 |
return {}; |
| 99 |
} |
| 100 |
|
| 101 |
void SDL_YUV_Display::display(const unsigned char *Y, |
| 102 |
const unsigned char *U, |
| 103 |
const unsigned char *V, |
| 104 |
int stride, int chroma_stride) |
| 105 |
{ |
| 106 |
if (!mWindowOpen) return; |
| 107 |
if (SDL_LockTexture(mTexture, nullptr, |
| 108 |
reinterpret_cast<void**>(&mPixels), &mStride) < 0) return; |
| 109 |
|
| 110 |
if (mChroma == SDL_CHROMA_420) { |
| 111 |
display420(Y,U,V,stride,chroma_stride); |
| 112 |
} |
| 113 |
else if (mChroma == SDL_CHROMA_422) { |
| 114 |
display422(Y,U,V,stride,chroma_stride); |
| 115 |
} |
| 116 |
else if (mChroma == SDL_CHROMA_444) { |
| 117 |
display444as420(Y,U,V,stride,chroma_stride); |
| 118 |
//display444as422(Y,U,V,stride,chroma_stride); |
| 119 |
} |
| 120 |
else if (mChroma == SDL_CHROMA_MONO) { |
| 121 |
display400(Y,stride); |
| 122 |
} |
| 123 |
|
| 124 |
SDL_UnlockTexture(mTexture); |
| 125 |
|
| 126 |
SDL_RenderCopy(mRenderer, mTexture, nullptr, nullptr); |
| 127 |
SDL_RenderPresent(mRenderer); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
void SDL_YUV_Display::display420(const unsigned char *Y, |
| 132 |
const unsigned char *U, |
| 133 |
const unsigned char *V, |
| 134 |
int stride, int chroma_stride) |
| 135 |
{ |
| 136 |
if (stride == mStride && chroma_stride == mStride/2) { |
| 137 |
|
| 138 |
// fast copy |
| 139 |
|
| 140 |
memcpy(mPixels, Y, rect.w * rect.h); |
| 141 |
memcpy(&mPixels[rect.w * rect.h], V, rect.w * rect.h / 4); |
| 142 |
memcpy(&mPixels[(rect.w * rect.h) + (rect.w * rect.h / 4)], U, rect.w * rect.h / 4); |
| 143 |
} |
| 144 |
else { |
| 145 |
// copy line by line, because sizes are different |
| 146 |
uint8_t *dest = mPixels; |
| 147 |
|
| 148 |
for (int y=0;y<rect.h;y++,dest+=mStride) |
| 149 |
{ |
| 150 |
memcpy(dest, Y+stride*y, rect.w); |
| 151 |
} |
| 152 |
|
| 153 |
for (int y=0;y<rect.h/2;y++,dest+=mStride/2) |
| 154 |
{ |
| 155 |
memcpy(dest, V+chroma_stride*y, rect.w/2); |
| 156 |
} |
| 157 |
|
| 158 |
for (int y=0;y<rect.h/2;y++,dest+=mStride/2) |
| 159 |
{ |
| 160 |
memcpy(dest, U+chroma_stride*y, rect.w/2); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
void SDL_YUV_Display::display400(const unsigned char *Y, int stride) |
| 167 |
{ |
| 168 |
uint8_t *dest = mPixels; |
| 169 |
if (stride == mStride) { |
| 170 |
|
| 171 |
// fast copy |
| 172 |
|
| 173 |
memcpy(mPixels, Y, rect.w * rect.h); |
| 174 |
dest += mStride * rect.h; |
| 175 |
} |
| 176 |
else { |
| 177 |
// copy line by line, because sizes are different |
| 178 |
|
| 179 |
for (int y=0;y<rect.h;y++,dest+=mStride) |
| 180 |
{ |
| 181 |
memcpy(dest, Y+stride*y, rect.w); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// clear chroma planes |
| 186 |
|
| 187 |
memset(dest, 0x80, mStride * rect.h / 2); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
void SDL_YUV_Display::display422(const unsigned char* Y, |
| 192 |
const unsigned char* U, |
| 193 |
const unsigned char* V, |
| 194 |
int stride, int chroma_stride) |
| 195 |
{ |
| 196 |
for (int y = 0; y < rect.h; y++) { |
| 197 |
unsigned char* dstY = mPixels + y * mStride; |
| 198 |
const unsigned char* Yp = Y + y * stride; |
| 199 |
|
| 200 |
memcpy(dstY, Yp, rect.w); |
| 201 |
} |
| 202 |
|
| 203 |
for (int y = 0; y < rect.h; y += 2) { |
| 204 |
unsigned char* dstV = mPixels + (y / 2) * mStride / 2 + rect.w * rect.h; |
| 205 |
unsigned char* dstU = mPixels + (y / 2) * mStride / 2 + rect.w * rect.h + rect.w * rect.h / 4; |
| 206 |
|
| 207 |
const unsigned char* Up = U + y * chroma_stride; |
| 208 |
const unsigned char* Vp = V + y * chroma_stride; |
| 209 |
|
| 210 |
memcpy(dstU, Up, rect.w / 2); |
| 211 |
memcpy(dstV, Vp, rect.w / 2); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/* This converts down 4:4:4 input to 4:2:2 for display, as SDL does not support |
| 217 |
any 4:4:4 pixel format. |
| 218 |
*/ |
| 219 |
void SDL_YUV_Display::display444as422(const unsigned char *Y, |
| 220 |
const unsigned char *U, |
| 221 |
const unsigned char *V, |
| 222 |
int stride, int chroma_stride) |
| 223 |
{ |
| 224 |
for (int y=0;y<rect.h;y++) |
| 225 |
{ |
| 226 |
unsigned char* p = mPixels + y*mStride *2; |
| 227 |
|
| 228 |
const unsigned char* Yp = Y + y*stride; |
| 229 |
const unsigned char* Up = U + y*chroma_stride; |
| 230 |
const unsigned char* Vp = V + y*chroma_stride; |
| 231 |
|
| 232 |
for (int x=0;x<rect.w;x+=2) { |
| 233 |
*p++ = Yp[x]; |
| 234 |
*p++ = Up[x]; |
| 235 |
*p++ = Yp[x+1]; |
| 236 |
*p++ = Vp[x]; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
void SDL_YUV_Display::display444as420(const unsigned char *Y, |
| 243 |
const unsigned char *U, |
| 244 |
const unsigned char *V, |
| 245 |
int stride, int chroma_stride) |
| 246 |
{ |
| 247 |
for (int y=0;y<rect.h;y++) |
| 248 |
{ |
| 249 |
unsigned char* p = mPixels + y*mStride; |
| 250 |
memcpy(p, Y+y*stride, rect.w); |
| 251 |
} |
| 252 |
|
| 253 |
uint8_t *startV = mPixels + (rect.h*mStride); |
| 254 |
uint8_t *startU = startV + (rect.h*mStride/2); |
| 255 |
for (int y=0;y<rect.h;y+=2) |
| 256 |
{ |
| 257 |
uint8_t* u = startU + y/2*mStride/2; |
| 258 |
uint8_t* v = startV + y/2*mStride/2; |
| 259 |
|
| 260 |
for (int x=0;x<rect.w;x+=2) { |
| 261 |
u[x / 2] = static_cast<uint8_t>((U[(y + 0) * chroma_stride + x] + U[(y + 0) * chroma_stride + x + 1] + |
| 262 |
U[(y + 1) * chroma_stride + x] + U[(y + 1) * chroma_stride + x + 1]) / 4); |
| 263 |
v[x / 2] = static_cast<uint8_t>((V[(y + 0) * chroma_stride + x] + V[(y + 0) * chroma_stride + x + 1] + |
| 264 |
V[(y + 1) * chroma_stride + x] + V[(y + 1) * chroma_stride + x + 1]) / 4); |
| 265 |
|
| 266 |
//u[x/2] = U[y*chroma_stride + x]; |
| 267 |
//v[x/2] = V[y*chroma_stride + x]; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
bool SDL_YUV_Display::doQuit() const |
| 274 |
{ |
| 275 |
SDL_Event event; |
| 276 |
while (SDL_PollEvent(&event)) { |
| 277 |
if (event.type == SDL_QUIT) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
void SDL_YUV_Display::close() |
| 286 |
{ |
| 287 |
if (mTexture) { |
| 288 |
SDL_DestroyTexture(mTexture); |
| 289 |
mTexture = nullptr; |
| 290 |
} |
| 291 |
if (mRenderer) { |
| 292 |
SDL_DestroyRenderer(mRenderer); |
| 293 |
mRenderer = nullptr; |
| 294 |
} |
| 295 |
if (mWindow) { |
| 296 |
SDL_DestroyWindow(mWindow); |
| 297 |
mWindow = nullptr; |
| 298 |
} |
| 299 |
SDL_Quit(); |
| 300 |
|
| 301 |
mWindowOpen=false; |
| 302 |
} |