home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / xmovie / vrender.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  4KB  |  167 lines

  1. #include "file.h"
  2. #include "settings.h"
  3. #include "mainmenu.h"
  4. #include "mainwindow.h"
  5. #include "mwindowgui.h"
  6. #include "playbackengine.h"
  7. #include "settings.h"
  8. #include "units.h"
  9. #include "vrender.h"
  10.  
  11.  
  12. VRender::VRender(MainWindow *mwindow)
  13. {
  14.     this->mwindow = mwindow;
  15. }
  16.  
  17. VRender::~VRender()
  18. {
  19. }
  20.  
  21. int VRender::arm_playback()
  22. {
  23.     startup_lock.lock();
  24.     synchronous = 1;
  25.  
  26.     mwindow->video_file->set_position(mwindow->current_percentage);
  27.     interrupt = 0;
  28.     mwindow->gui->start_playback();
  29.     return 0;
  30. }
  31.  
  32. int VRender::stop_playback()
  33. {
  34.     interrupt = 1;
  35.     return 0;
  36. }
  37.  
  38. int VRender::close_playback()
  39. {
  40.     double percentage, time;
  41.     mwindow->gui->stop_playback();
  42.     mwindow->video_file->get_position(percentage, time);
  43.     mwindow->flash_frame(percentage);
  44.     mwindow->gui->canvas->flash();
  45.     return 0;
  46. }
  47.  
  48. int VRender::wait_for_startup()
  49. {
  50.     startup_lock.lock();
  51.     startup_lock.unlock();
  52.     return 0;
  53. }
  54.  
  55. int VRender::wait_for_completion()
  56. {
  57.     join();
  58.     return 0;
  59. }
  60.  
  61. void VRender::run()
  62. {
  63.     long current_sample = 0;     // Time the frame was finished being decompressed.
  64.     long current_frame = 0;      // Current frame being flashed
  65.     int last_frame = 0;
  66.     long start_sample, end_sample; // Absolute counts.
  67.     int done = 0, skip;
  68.     long delay;
  69.     long skip_countdown = VRENDER_THRESHOLD;
  70.     long delay_countdown = VRENDER_THRESHOLD;
  71.  
  72.     framerate_counter = 0;
  73.     startup_lock.unlock();
  74.     while(!interrupt && !done)
  75.     {
  76. // Update the framerate counter
  77.         if(!framerate_counter)
  78.         {
  79.             mwindow->actual_framerate = (float)((int)mwindow->frame_rate)  / ((float)framerate_timer.get_difference() / 1000);
  80.             if(mwindow->settingsmenu->thread->thread_running)
  81.             {
  82.                 mwindow->settingsmenu->thread->update_framerate();
  83.             }
  84.             framerate_counter = (int)mwindow->frame_rate;
  85.             framerate_timer.update();
  86.         }
  87.         framerate_counter--;
  88.  
  89. // Skip frames now
  90.         mwindow->video_file->drop_frames(current_frame - last_frame - 1);
  91.         last_frame = current_frame;
  92.  
  93. // Perform the most time consuming part of frame decompression now.
  94.         mwindow->arm_frame();
  95.  
  96. // Determine the delay until the frame needs to be shown.
  97.         current_sample = (long)(mwindow->engine->current_sample());
  98. // latest sample at which the frame can be shown.
  99.         end_sample = Units::tosamples(current_frame, mwindow->samplerate, mwindow->frame_rate);
  100. // earliest sample by which the frame needs to be shown.
  101.         start_sample = Units::tosamples(current_frame - 1, mwindow->samplerate, mwindow->frame_rate);
  102.  
  103.         if(end_sample < current_sample)
  104.         {
  105. // Frame rendered late.  Flash it now.
  106.             mwindow->gui->lock_window();
  107.             mwindow->flash_armed_frame();
  108.             mwindow->gui->unlock_window();
  109.  
  110.             if(mwindow->every_frame)
  111.             {
  112. // User wants every frame.
  113.                 current_frame++;
  114.             }
  115.             else
  116.             if(skip_countdown > 0)
  117.             {
  118. // Maybe just a freak.
  119.                 current_frame++;
  120.                 skip_countdown--;
  121.             }
  122.             else
  123.             {
  124. // Get the frames to skip.
  125.                 delay_countdown = VRENDER_THRESHOLD;
  126.                 skip = 1 + (long)Units::toframes(current_sample, mwindow->samplerate, mwindow->frame_rate) - 
  127.                     (long)Units::toframes(end_sample, mwindow->samplerate, mwindow->frame_rate);
  128.                 current_frame += skip;
  129.             }
  130.         }
  131.         else
  132.         {
  133. // Frame rendered early or just in time.
  134.             current_frame++;
  135.  
  136.             if(delay_countdown > 0)
  137.             {
  138. // Maybe just a freak
  139.                 delay_countdown--;
  140.             }
  141.             else
  142.             {
  143.                 skip_countdown = VRENDER_THRESHOLD;
  144.                 if(start_sample > current_sample)
  145.                 {
  146. // Came before the earliest sample so delay
  147.                     delay = (long)((float)(start_sample - current_sample) * 1000 / mwindow->samplerate);
  148.                     timer.delay(delay);
  149.                 }
  150.                 else
  151.                 {
  152. // Came after the earliest sample so keep going
  153.                 }
  154.             }
  155.  
  156. // Flash frame now.
  157.             mwindow->gui->lock_window();
  158.             mwindow->flash_armed_frame();
  159.             mwindow->gui->unlock_window();
  160.         }
  161.  
  162.         if(mwindow->video_file->end_of_video()) done = 1;
  163.     }
  164.  
  165.     close_playback();
  166. }
  167.