SDL  2.0
testoverlay2.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for testoverlay2.c:

Go to the source code of this file.

Macros

#define MOOSEPIC_W   64
 
#define MOOSEPIC_H   88
 
#define MOOSEFRAME_SIZE   (MOOSEPIC_W * MOOSEPIC_H)
 
#define MOOSEFRAMES_COUNT   10
 

Functions

static void quit (int rc)
 
void RGBtoYUV (Uint8 *rgb, int *yuv, int monochrome, int luminance)
 
void ConvertRGBtoYV12 (Uint8 *rgb, Uint8 *out, int w, int h, int monochrome, int luminance)
 
void ConvertRGBtoNV12 (Uint8 *rgb, Uint8 *out, int w, int h, int monochrome, int luminance)
 
static void PrintUsage (char *argv0)
 
void loop ()
 
int main (int argc, char **argv)
 

Variables

SDL_Color MooseColors [84]
 
Uint8 MooseFrame [MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
 
SDL_TextureMooseTexture
 
SDL_Rect displayrect
 
int window_w
 
int window_h
 
SDL_Windowwindow
 
SDL_Rendererrenderer
 
int paused = 0
 
int i
 
SDL_bool done = SDL_FALSE
 
Uint32 pixel_format = SDL_PIXELFORMAT_YV12
 
int fpsdelay
 

Macro Definition Documentation

#define MOOSEFRAME_SIZE   (MOOSEPIC_W * MOOSEPIC_H)

Definition at line 32 of file testoverlay2.c.

Referenced by main().

#define MOOSEFRAMES_COUNT   10

Definition at line 33 of file testoverlay2.c.

Referenced by loop(), and main().

#define MOOSEPIC_H   88

Definition at line 30 of file testoverlay2.c.

Referenced by main().

#define MOOSEPIC_W   64

Definition at line 29 of file testoverlay2.c.

Referenced by loop(), and main().

Function Documentation

void ConvertRGBtoNV12 ( Uint8 rgb,
Uint8 out,
int  w,
int  h,
int  monochrome,
int  luminance 
)

Definition at line 226 of file testoverlay2.c.

References RGBtoYUV().

Referenced by main().

228 {
229  int x, y;
230  int yuv[3];
231  Uint8 *op[2];
232 
233  op[0] = out;
234  op[1] = op[0] + w*h;
235  for (y = 0; y < h; ++y) {
236  for (x = 0; x < w; ++x) {
237  RGBtoYUV(rgb, yuv, monochrome, luminance);
238  *(op[0]++) = yuv[0];
239  if (x % 2 == 0 && y % 2 == 0) {
240  *(op[1]++) = yuv[1];
241  *(op[1]++) = yuv[2];
242  }
243  rgb += 3;
244  }
245  }
246 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
GLubyte GLubyte GLubyte GLubyte w
void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
Definition: testoverlay2.c:170
GLfloat GLfloat GLfloat GLfloat h
void ConvertRGBtoYV12 ( Uint8 rgb,
Uint8 out,
int  w,
int  h,
int  monochrome,
int  luminance 
)

Definition at line 202 of file testoverlay2.c.

References RGBtoYUV().

Referenced by main().

204 {
205  int x, y;
206  int yuv[3];
207  Uint8 *op[3];
208 
209  op[0] = out;
210  op[1] = op[0] + w*h;
211  op[2] = op[1] + w*h/4;
212  for (y = 0; y < h; ++y) {
213  for (x = 0; x < w; ++x) {
214  RGBtoYUV(rgb, yuv, monochrome, luminance);
215  *(op[0]++) = yuv[0];
216  if (x % 2 == 0 && y % 2 == 0) {
217  *(op[1]++) = yuv[2];
218  *(op[2]++) = yuv[1];
219  }
220  rgb += 3;
221  }
222  }
223 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
GLubyte GLubyte GLubyte GLubyte w
void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
Definition: testoverlay2.c:170
GLfloat GLfloat GLfloat GLfloat h
void loop ( )

Definition at line 266 of file testoverlay2.c.

References done, SDL_WindowEvent::event, fpsdelay, SDL_Rect::h, i, SDL_Event::key, SDL_KeyboardEvent::keysym, MooseFrame, MOOSEFRAMES_COUNT, MOOSEPIC_W, SDL_Event::motion, NULL, paused, pixel_format, SDL_BYTESPERPIXEL, SDL_Delay, SDL_KEYDOWN, SDL_MOUSEBUTTONDOWN, SDL_MOUSEMOTION, SDL_PollEvent, SDL_QUIT, SDL_RenderClear, SDL_RenderCopy, SDL_RenderPresent, SDL_RenderSetViewport, SDL_TRUE, SDL_UpdateTexture, SDL_WINDOWEVENT, SDL_WINDOWEVENT_RESIZED, SDLK_ESCAPE, SDLK_SPACE, SDL_MouseMotionEvent::state, SDL_Keysym::sym, SDL_Event::type, SDL_Rect::w, SDL_Event::window, window_h, window_w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

267 {
269 
270  while (SDL_PollEvent(&event)) {
271  switch (event.type) {
272  case SDL_WINDOWEVENT:
273  if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
275  displayrect.w = window_w = event.window.data1;
276  displayrect.h = window_h = event.window.data2;
277  }
278  break;
279  case SDL_MOUSEBUTTONDOWN:
280  displayrect.x = event.button.x - window_w / 2;
281  displayrect.y = event.button.y - window_h / 2;
282  break;
283  case SDL_MOUSEMOTION:
284  if (event.motion.state) {
285  displayrect.x = event.motion.x - window_w / 2;
286  displayrect.y = event.motion.y - window_h / 2;
287  }
288  break;
289  case SDL_KEYDOWN:
290  if (event.key.keysym.sym == SDLK_SPACE) {
291  paused = !paused;
292  break;
293  }
294  if (event.key.keysym.sym != SDLK_ESCAPE) {
295  break;
296  }
297  case SDL_QUIT:
298  done = SDL_TRUE;
299  break;
300  }
301  }
302 
303 #ifndef __EMSCRIPTEN__
305 #endif
306 
307  if (!paused) {
308  i = (i + 1) % MOOSEFRAMES_COUNT;
309 
311  }
315 
316 #ifdef __EMSCRIPTEN__
317  if (done) {
318  emscripten_cancel_main_loop();
319  }
320 #endif
321 }
#define SDL_RenderSetViewport
SDL_MouseMotionEvent motion
Definition: SDL_events.h:529
#define MOOSEPIC_W
Definition: testoverlay2.c:29
#define SDL_PollEvent
SDL_Texture * MooseTexture
Definition: testoverlay2.c:143
SDL_bool done
Definition: testoverlay2.c:151
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:127
#define SDL_UpdateTexture
SDL_WindowEvent window
Definition: SDL_events.h:525
#define SDL_RenderCopy
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:142
SDL_Renderer * renderer
Definition: testoverlay2.c:148
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:33
struct _cl_event * event
int fpsdelay
Definition: testoverlay2.c:153
int paused
Definition: testoverlay2.c:149
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:196
int w
Definition: SDL_rect.h:67
#define SDL_Delay
#define NULL
Definition: begin_code.h:143
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:526
int h
Definition: SDL_rect.h:67
SDL_Keycode sym
Definition: SDL_keyboard.h:50
int window_h
Definition: testoverlay2.c:146
int i
Definition: testoverlay2.c:150
Uint32 pixel_format
Definition: testoverlay2.c:152
General event structure.
Definition: SDL_events.h:521
SDL_Rect displayrect
Definition: testoverlay2.c:144
int y
Definition: SDL_rect.h:66
int window_w
Definition: testoverlay2.c:145
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:523
int main ( int  argc,
char **  argv 
)

Definition at line 324 of file testoverlay2.c.

References SDL_Color::b, ConvertRGBtoNV12(), ConvertRGBtoYV12(), done, fpsdelay, frame, free, SDL_Color::g, SDL_Rect::h, i, j, loop(), malloc, MooseFrame, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT, MOOSEPIC_H, MOOSEPIC_W, NULL, pixel_format, PrintUsage(), quit(), SDL_Color::r, SDL_CreateRenderer, SDL_CreateTexture, SDL_CreateWindow, SDL_DestroyRenderer, SDL_EventState, SDL_GetError, SDL_IGNORE, SDL_Init, SDL_INIT_VIDEO, SDL_KEYUP, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_YV12, SDL_RWclose, SDL_RWFromFile, SDL_RWread, SDL_TEXTUREACCESS_STREAMING, SDL_WINDOW_RESIZABLE, SDL_WINDOWPOS_UNDEFINED, SDL_Rect::w, window, window_h, window_w, SDL_Rect::x, and SDL_Rect::y.

325 {
326  Uint8 *RawMooseData;
327  SDL_RWops *handle;
329  int j;
330  int fps = 12;
331  int fpsdelay;
332  int nodelay = 0;
333 #ifdef TEST_NV12
335 #else
336  Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
337 #endif
338  int scale = 5;
339 
340  /* Enable standard application logging */
342 
343  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
344  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
345  return 3;
346  }
347 
348  while (argc > 1) {
349  if (strcmp(argv[1], "-fps") == 0) {
350  if (argv[2]) {
351  fps = atoi(argv[2]);
352  if (fps == 0) {
354  "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
355  quit(10);
356  }
357  if ((fps < 0) || (fps > 1000)) {
359  "The -fps option must be in range from 1 to 1000, default is 12.\n");
360  quit(10);
361  }
362  argv += 2;
363  argc -= 2;
364  } else {
366  "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
367  quit(10);
368  }
369  } else if (strcmp(argv[1], "-nodelay") == 0) {
370  nodelay = 1;
371  argv += 1;
372  argc -= 1;
373  } else if (strcmp(argv[1], "-scale") == 0) {
374  if (argv[2]) {
375  scale = atoi(argv[2]);
376  if (scale == 0) {
378  "The -scale option requires an argument [from 1 to 50], default is 5.\n");
379  quit(10);
380  }
381  if ((scale < 0) || (scale > 50)) {
383  "The -scale option must be in range from 1 to 50, default is 5.\n");
384  quit(10);
385  }
386  argv += 2;
387  argc -= 2;
388  } else {
390  "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
391  quit(10);
392  }
393  } else if ((strcmp(argv[1], "-help") == 0)
394  || (strcmp(argv[1], "-h") == 0)) {
395  PrintUsage(argv[0]);
396  quit(0);
397  } else {
398  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
399  quit(10);
400  }
401  break;
402  }
403 
404  RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
405  if (RawMooseData == NULL) {
406  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
407  free(RawMooseData);
408  quit(1);
409  }
410 
411  /* load the trojan moose images */
412  handle = SDL_RWFromFile("moose.dat", "rb");
413  if (handle == NULL) {
414  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
415  free(RawMooseData);
416  quit(2);
417  }
418 
419  SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
420 
421  SDL_RWclose(handle);
422 
423  /* Create the window and renderer */
426  window = SDL_CreateWindow("Happy Moose",
431  if (!window) {
432  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
433  free(RawMooseData);
434  quit(4);
435  }
436 
437  renderer = SDL_CreateRenderer(window, -1, 0);
438  if (!renderer) {
439  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
440  free(RawMooseData);
441  quit(4);
442  }
443 
445  if (!MooseTexture) {
446  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
447  free(RawMooseData);
448  quit(5);
449  }
450  /* Uncomment this to check vertex color with a YUV texture */
451  /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
452 
453  for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
454  Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
455  Uint8 *rgb;
456  Uint8 *frame;
457 
458  rgb = MooseFrameRGB;
459  frame = RawMooseData + i * MOOSEFRAME_SIZE;
460  for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
461  rgb[0] = MooseColors[frame[j]].r;
462  rgb[1] = MooseColors[frame[j]].g;
463  rgb[2] = MooseColors[frame[j]].b;
464  rgb += 3;
465  }
466  switch (pixel_format) {
468  ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
469  break;
471  ConvertRGBtoNV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
472  break;
473  default:
474  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported pixel format\n");
475  break;
476  }
477  }
478 
479  free(RawMooseData);
480 
481  /* set the start frame */
482  i = 0;
483  if (nodelay) {
484  fpsdelay = 0;
485  } else {
486  fpsdelay = 1000 / fps;
487  }
488 
489  displayrect.x = 0;
490  displayrect.y = 0;
493 
494  /* Ignore key up events, they don't even get filtered */
496 
497  /* Loop, waiting for QUIT or RESIZE */
498 #ifdef __EMSCRIPTEN__
499  emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
500 #else
501  while (!done) {
502  loop();
503  }
504 #endif
505 
507  quit(0);
508  return 0;
509 }
#define SDL_CreateTexture
GLenum GLenum GLenum GLenum GLenum scale
#define MOOSEPIC_W
Definition: testoverlay2.c:29
void loop()
Definition: testoverlay2.c:266
#define SDL_GetError
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
Uint8 g
Definition: SDL_pixels.h:282
void ConvertRGBtoNV12(Uint8 *rgb, Uint8 *out, int w, int h, int monochrome, int luminance)
Definition: testoverlay2.c:226
static void quit(int rc)
Definition: testoverlay2.c:157
#define MOOSEFRAME_SIZE
Definition: testoverlay2.c:32
SDL_EventEntry * free
Definition: SDL_events.c:81
SDL_Texture * MooseTexture
Definition: testoverlay2.c:143
SDL_bool done
Definition: testoverlay2.c:151
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
Uint8 b
Definition: SDL_pixels.h:283
#define SDL_CreateWindow
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:120
#define SDL_LogError
#define MOOSEPIC_H
Definition: testoverlay2.c:30
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:142
#define SDL_RWFromFile
SDL_Renderer * renderer
Definition: testoverlay2.c:148
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:33
Uint8 r
Definition: SDL_pixels.h:281
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
int frame
Definition: teststreaming.c:60
int fpsdelay
Definition: testoverlay2.c:153
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define malloc
Definition: SDL_malloc.c:641
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:71
#define SDL_EventState
SDL_Window * window
Definition: testoverlay2.c:147
int window_h
Definition: testoverlay2.c:146
int i
Definition: testoverlay2.c:150
SDL_Color MooseColors[84]
Definition: testoverlay2.c:35
Uint32 pixel_format
Definition: testoverlay2.c:152
#define SDL_Init
SDL_Rect displayrect
Definition: testoverlay2.c:144
#define SDL_DestroyRenderer
static void PrintUsage(char *argv0)
Definition: testoverlay2.c:249
int y
Definition: SDL_rect.h:66
int window_w
Definition: testoverlay2.c:145
#define SDL_INIT_VIDEO
Definition: SDL.h:77
#define SDL_CreateRenderer
#define SDL_IGNORE
Definition: SDL_events.h:716
void ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h, int monochrome, int luminance)
Definition: testoverlay2.c:202
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:42
static void PrintUsage ( char *  argv0)
static

Definition at line 249 of file testoverlay2.c.

References SDL_Log.

Referenced by main().

250 {
251  SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
252  SDL_Log("\n");
253  SDL_Log("Where 'arg' is any of the following options:\n");
254  SDL_Log("\n");
255  SDL_Log(" -fps <frames per second>\n");
256  SDL_Log(" -nodelay\n");
257  SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
258  SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
259  SDL_Log(" -help (shows this help)\n");
260  SDL_Log("\n");
261  SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
262  SDL_Log("\n");
263 }
#define SDL_Log
static void quit ( int  rc)
static

Definition at line 157 of file testoverlay2.c.

References SDL_Quit.

Referenced by main().

158 {
159  SDL_Quit();
160  exit(rc);
161 }
#define SDL_Quit
void RGBtoYUV ( Uint8 rgb,
int *  yuv,
int  monochrome,
int  luminance 
)

Definition at line 170 of file testoverlay2.c.

Referenced by ConvertRGBtoNV12(), and ConvertRGBtoYV12().

171 {
172  if (monochrome) {
173 #if 1 /* these are the two formulas that I found on the FourCC site... */
174  yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
175  yuv[1] = 128;
176  yuv[2] = 128;
177 #else
178  yuv[0] = (int)(0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
179  yuv[1] = 128;
180  yuv[2] = 128;
181 #endif
182  } else {
183 #if 1 /* these are the two formulas that I found on the FourCC site... */
184  yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
185  yuv[1] = (int)((rgb[2] - yuv[0]) * 0.565 + 128);
186  yuv[2] = (int)((rgb[0] - yuv[0]) * 0.713 + 128);
187 #else
188  yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
189  yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
190  yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
191 #endif
192  }
193 
194  if (luminance != 100) {
195  yuv[0] = yuv[0] * luminance / 100;
196  if (yuv[0] > 255)
197  yuv[0] = 255;
198  }
199 }

Variable Documentation

SDL_Rect displayrect

Definition at line 144 of file testoverlay2.c.

Definition at line 151 of file testoverlay2.c.

Referenced by loop(), and main().

int fpsdelay

Definition at line 153 of file testoverlay2.c.

Referenced by loop(), and main().

int i

Definition at line 150 of file testoverlay2.c.

Referenced by loop(), and main().

SDL_Color MooseColors[84]

Definition at line 35 of file testoverlay2.c.

Definition at line 142 of file testoverlay2.c.

Referenced by loop(), and main().

SDL_Texture* MooseTexture

Definition at line 143 of file testoverlay2.c.

int paused = 0

Definition at line 149 of file testoverlay2.c.

Referenced by loop().

Uint32 pixel_format = SDL_PIXELFORMAT_YV12

Definition at line 152 of file testoverlay2.c.

Referenced by loop(), main(), and SDL_InitFormat().

SDL_Renderer* renderer

Definition at line 148 of file testoverlay2.c.

SDL_Window* window

Definition at line 147 of file testoverlay2.c.

Referenced by main().

int window_h

Definition at line 146 of file testoverlay2.c.

Referenced by loop(), main(), and MoveSprites().

int window_w

Definition at line 145 of file testoverlay2.c.

Referenced by loop(), main(), and MoveSprites().