Add luma only
[yuv-player.git] / yuvPlayer.cpp
index 87fb7dc..fb8c1a9 100644 (file)
@@ -26,7 +26,8 @@ class YuvPlayer
 public:
     YuvPlayer(int width, int height, const string& title = "YUV Player");
     void play(string fname, int fps = -1);
-    void drawGrid();
+    void toggleGrid();
+    void toggleYumaOnly();
     ~YuvPlayer();
 
 private:
@@ -41,6 +42,9 @@ private:
     unsigned int frameCnt = 0;
     string title;
     ifstream::pos_type last_pos;
+    bool isShowingGrid = false;
+    bool isLumOnly = false;
+    uint8_t *raw;
 };
 
 
@@ -77,10 +81,24 @@ YuvPlayer::~YuvPlayer()
     SDL_Quit();
 }
 
+// https://wiki.videolan.org/YUV/, YCrCb
+// I420, YUV420p,  YYYY YYYY UU VV
+static void lumaOnly(uint8_t *raw, int w, int h)
+{
+    memset(raw + w * h, 128, w * h / 2); // UV 128
+    //memset(raw + w * h, 0, w * h / 2); // UV 128
+    //memset(raw + w * h, 0, w * h / 2); // UV 0, G only
+    //memset(raw + w * h, 255, w * h / 2); // UV 0, R only
+    //memset(raw + w * h, 128, w * h / 4); // U 128
+    //for (int i = 0; i < w * h; i++){
+    //    raw[i] = raw[i] / 2; // Y to 1/2
+    //}
+}
+
 void YuvPlayer::play(string file, int fps)
 {
     size_t frame_size = width * height * 3 / 2;
-    uint8_t *raw = (uint8_t *) malloc(sizeof(uint8_t) * frame_size);
+    raw = (uint8_t *) malloc(sizeof(uint8_t) * frame_size);
     ifstream in (file, ios::binary);
     if (!in.is_open()) {
         cout << "failed to open " << file << endl;
@@ -126,8 +144,10 @@ void YuvPlayer::play(string file, int fps)
                         isPlaying = !isPlaying;
                         break;
                     case SDLK_g:
-                        drawGrid();
+                        toggleGrid();
                         break;
+                    case SDLK_y:
+                        toggleYumaOnly();
                     default:
                         break;
                 }
@@ -137,23 +157,16 @@ void YuvPlayer::play(string file, int fps)
     }
     free(raw);
 }
-
-void YuvPlayer::drawGrid()
+void YuvPlayer::toggleYumaOnly()
 {
-//    cout << "drawGrid" << endl;
-//    SDL_RenderClear(renderer);
-//                    SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
-//
-//    if(SDL_RenderDrawLine(renderer, 10, 10, 200, 200) == -1){
-//        cout << "draw line error" << endl;
-//    }
-//    SDL_RenderCopy(renderer, texture, nullptr, nullptr);
-//    SDL_RenderPresent(renderer);
+    isLumOnly = !isLumOnly;
+    updateRenderer(this->raw);
+}
 
-//    SDL_UpdateTexture(texture, nullptr, raw, width * SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_IYUV));
-    SDL_RenderClear(renderer);
-    SDL_RenderCopy(renderer, texture, nullptr, nullptr);
-    SDL_RenderPresent(renderer);
+void YuvPlayer::toggleGrid()
+{
+    isShowingGrid = !isShowingGrid;
+    updateRenderer(raw);
 }
 
 
@@ -174,19 +187,37 @@ bool YuvPlayer::readFrame(ifstream& in, uint8_t *raw, size_t size)
 
 void YuvPlayer::updateRenderer(uint8_t *raw)
 {    
-    if (frameCnt == 10){
-        memset(raw, 0x00, 1280 * 720);
+    if (isLumOnly){
+        lumaOnly(raw, width, height);
     }
     SDL_UpdateTexture(texture, nullptr, raw, width * SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_IYUV));
     SDL_RenderClear(renderer);
     SDL_RenderCopy(renderer, texture, nullptr, nullptr);
+
+    if(isShowingGrid){
+        SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0xFF, 0xFF );            
+        //SDL_RenderDrawLine( renderer, 0, height / 2, width, height / 2 );
+        SDL_RenderDrawLine( renderer, 0, height / 3, width, height / 3 );
+        SDL_RenderDrawLine( renderer, 0, height * 2 / 3, width, height * 2 / 3 );
+
+        SDL_RenderDrawLine( renderer, width / 3, 0, width / 3, height );
+        SDL_RenderDrawLine( renderer, width * 2 / 3, 0, width * 2 / 3, height );
+
+    }
+
     SDL_RenderPresent(renderer);
 }
 
 int main(int argc, char* argv[])
 {
-    string fname = "out.yuv";
+//    string fname(argv[1] ? argv[1] : "out.yuv");
+//    size_t width  = atoi(argv[2] ? argv[2] : "1280");
+//    size_t height = atoi(argv[3] ? argv[3] : "720");
+//
+//    int fps = atoi(argv[4] ? argv[4] : "20");
+//    cout << fname << "\n" << width << "\n" << height << "\n" << fps << "\n" << endl;
+//
     YuvPlayer player(1280, 720);
-    //player.play(fname, 25);
-    player.play(fname);
+    player.play("out.yuv", 25);
+    //player.play(fname);
 }