Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6401 in orxonox.OLD for branches/avi_play


Ignore:
Timestamp:
Jan 3, 2006, 7:47:05 PM (19 years ago)
Author:
hdavid
Message:

branches\avi_play

Location:
branches/avi_play/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/avi_play/src/lib/graphics/importer/media_container.cc

    r6382 r6401  
    4040
    4141  if (filename != NULL)
    42     this->loadMedia(filename);
     42  {
     43    if(!this->loadMedia(filename))
     44      PRINTF(1)("MediaContainer::loadMedia() failed for %s\n", filename);
     45  }
    4346}
    4447
     
    7477}
    7578
    76 void MediaContainer::loadMedia(const char* filename)
     79bool MediaContainer::loadMedia(const char* filename)
    7780{
    7881  // register all formats and codecs
     
    8184  // Open video file
    8285  if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 )
     86  {
    8387    PRINTF(1)("Could not open %s\n", filename);
     88    return false;
     89  }
    8490
    8591  // Retrieve stream information
    8692  if (av_find_stream_info(format_context) < 0)
     93  {
    8794    PRINTF(1)("Could not find stream information in %s\n", filename);
     95    return false;
     96  }
    8897
    8998  // Find the first video stream and take it
     
    91100 
    92101  if(video_stream == -1)
     102  {
    93103    PRINTF(1)("Could not find a video stream in %s\n", filename);
     104    return false;
     105  }
    94106
    95107  // Get a pointer to the codec context for the video stream
     
    101113  codec = avcodec_find_decoder(codec_context->codec_id);
    102114  if (codec == NULL)
     115  {
    103116    PRINTF(1)("Could not find codec\n");
     117    return false;
     118  }
    104119
    105120  // Open codec
    106121  if (avcodec_open(codec_context, codec) < 0)
     122  {
    107123    PRINTF(1)("Could not open codec\n");
     124    return false;
     125  }
    108126
    109127  // Allocate video frame
     
    123141  // Calculate fps
    124142  fps = av_q2d(format_context->streams[video_stream]->r_frame_rate);
     143  // NOTE: fix this fps problem!!
     144  if(fps < 0 || fps > 1000)
     145    fps = 30;
    125146
    126147  // read the frames and save them in a sequence as textures
    127148  this->loadFrames();
     149
     150  return true;
    128151}
    129152
  • branches/avi_play/src/lib/graphics/importer/media_container.h

    r6373 r6401  
    4848  ~MediaContainer();
    4949
    50   void loadMedia(const char* filename);
     50  bool loadMedia(const char* filename);
    5151  void loadFrames();
    5252
  • branches/avi_play/src/lib/graphics/importer/movie_player.cc

    r6382 r6401  
    3434
    3535  if (filename != NULL)
    36     this->loadMovie(filename);
     36  {
     37    if(!this->loadMovie(filename))
     38      PRINTF(1)("MoviePlayer::loadMovie() failes for %s\n", filename);
     39  }
    3740}
    3841
     
    5861  av_free(frame);
    5962
    60 avcodec_default_free_buffers(codec_context);
     63  avcodec_default_free_buffers(codec_context);
    6164
    6265  // Close the codec
     
    7578  timer = 0;
    7679  frame_number = 0;
    77   speed = 1;
     80  loading = false;
    7881
    7982  material = new Material;
     
    8891}
    8992
    90 void MoviePlayer::loadMovie(const char* filename)
     93bool MoviePlayer::loadMovie(const char* filename)
    9194{
    9295  // register all formats and codecs
     
    9598  // Open video file
    9699  if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 )
     100  {
    97101    PRINTF(1)("Could not open %s\n", filename);
     102    return false;
     103  }
    98104
    99105  // Retrieve stream information
    100106  if (av_find_stream_info(format_context) < 0)
     107  {
    101108    PRINTF(1)("Could not find stream information in %s\n", filename);
     109    return false;
     110  }
    102111
    103112  // Find the first video stream and take it
     
    105114
    106115  if(video_stream == -1)
     116  {
    107117    PRINTF(1)("Could not find a video stream in %s\n", filename);
     118    return false;
     119  }
    108120
    109121  // Get a pointer to the codec context for the video stream
     
    115127  codec = avcodec_find_decoder(codec_context->codec_id);
    116128  if (codec == NULL)
     129  {
    117130    PRINTF(1)("Could not find codec\n");
     131    return false;
     132  }
    118133
    119134  // Open codec
    120135  if (avcodec_open(codec_context, codec) < 0)
     136  {
    121137    PRINTF(1)("Could not open codec\n");
     138    return false;
     139  }
    122140
    123141  // Allocate video frame
     
    137155  // Calculate fps
    138156  fps = av_q2d(format_context->streams[video_stream]->r_frame_rate);
     157  // NOTE: fix this fps problem!!
     158  if(fps < 0 || fps > 1000)
     159    fps = 30;
    139160
    140161  // duration
     
    155176              NULL);
    156177  glBindTexture(GL_TEXTURE_2D, 0);
     178
     179  loading = true;
     180  return true;
    157181}
    158182
     
    284308}
    285309
    286 void MoviePlayer::gotoFrame(int frames)
    287 {
     310bool MoviePlayer::gotoFrame(int frames)
     311{
     312  if(!loading)
     313  {
     314    PRINTF(0)("Load first the media file with loadMovie\n");
     315    return false;
     316  }
     317
     318  int err;
    288319  // seek doesnt work for the first two frames
    289320  // you will get ugly fragments
     
    291322  {
    292323    // go to the begin of the video
    293     av_seek_frame(format_context, video_stream, 0, AVSEEK_FLAG_BACKWARD);
    294     frame_number = 0;
     324    err = av_seek_frame(format_context, video_stream, 0, AVSEEK_FLAG_BACKWARD);
     325    if(err < 0)
     326    {
     327      PRINTF(1)("Could not seek to frame 0\n");
     328      return false;
     329    }
     330
     331    this->frame_number = 0;
    295332  }
    296333  else
     
    298335    // seeks to the nearest keyframe
    299336    // NOTE: there is only about every 5s a keyframe!
    300     av_seek_frame(format_context, video_stream, frames, AVSEEK_FLAG_BACKWARD);
     337    err = av_seek_frame(format_context, video_stream, frames, AVSEEK_FLAG_BACKWARD);
     338    if(err < 0)
     339    {
     340      PRINTF(1)("Could not seek to frame %i\n", frames);
     341      return false;
     342    }
    301343   
    302344    // go from the keyframe to the exact position
    303345    codec_context->hurry_up = 1;
    304346    do {
    305       av_read_frame(format_context, &packet);
    306       if(packet.pts >= frames-1)
    307         break;
    308       int frame_finished;
    309       avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size);
    310       av_free_packet(&packet);
     347      if(av_read_frame(format_context, &packet) < 0)
     348      {
     349        PRINTF(1)("Could not seek to frame %i\n", frames);
     350        return false;
     351      }
     352
     353      if(packet.stream_index == video_stream)
     354      {
     355        if(packet.pts >= frames-1)
     356          break;
     357        int frame_finished;
     358        avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size);
     359        av_free_packet(&packet);
     360      }
    311361    } while(1);
    312362    codec_context->hurry_up = 0;
    313363 
    314     frame_number = frames;
    315   }
     364    this->frame_number = frames;
     365  }
     366 
     367  return true;
    316368}
    317369
    318370void MoviePlayer::start(float start_time)
    319371{
    320   status = PLAY;
    321   timer = 0;
    322 
    323   this->gotoFrame(start_time * fps);
    324 
    325    PRINTF(0)("start\n");
     372  //start_frame = start_time * fps;
     373  start_frame = 0;
     374
     375  if(this->gotoFrame(start_frame))
     376  {
     377    status = PLAY;
     378    timer = 0;
     379 
     380    this->gotoFrame(start_frame);
     381 
     382    PRINTF(0)("start\n");
     383  }
    326384}
    327385
     
    356414  {
    357415    timer += dt;
    358     actual_frame = timer * fps * speed;
    359 
     416    actual_frame = timer * fps + start_frame;
    360417    if(actual_frame != frame_number)
    361418    {
     
    378435}
    379436
    380 void MoviePlayer::setSpeed(float speed)
    381 {
    382   if(speed > 0)
    383     this->speed = speed;
    384 }
    385 
    386 float MoviePlayer::getSpeed()
    387 {
    388   return this->speed;
     437void MoviePlayer::setFPS(float fps)
     438{
     439  if(fps > 0)
     440    this->fps = fps;
     441}
     442
     443float MoviePlayer::getFPS()
     444{
     445  return this->fps;
    389446}
    390447
     
    396453void MoviePlayer::printInformation()
    397454{
     455  if(!loading)
     456  {
     457    PRINTF(0)("Load first the media file with loadMovie\n");
     458    return;
     459  }
     460
    398461  PRINTF(0)("========================\n");
    399462  PRINTF(0)("========================\n");
     
    410473  PRINTF(0)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames);
    411474  PRINTF(0)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num);
    412   PRINTF(0)("fps: %0.2f\n", av_q2d(format_context->streams[video_stream]->r_frame_rate));
     475  PRINTF(0)("fps: %0.2f\n", fps);
    413476  PRINTF(0)("========================\n");
    414477  PRINTF(0)("=    AVCodecContext    =\n");
  • branches/avi_play/src/lib/graphics/importer/movie_player.h

    r6382 r6401  
    5656
    5757  MP_STATUS status;     
    58   float speed;
    5958  float timer;
     59  int start_frame;
    6060  int actual_frame;
    6161  int frame_number;
    6262  float fps;
    63   int duration; 
     63  int duration;
     64  bool loading; 
    6465
    6566public:
     
    6970  ~MoviePlayer();
    7071
    71   void loadMovie(const char* filename);
     72  bool loadMovie(const char* filename);
    7273
    7374        void start(float start_time);
     
    7980        const void draw();
    8081
    81         void setSpeed(float speed);
    82         float getSpeed();
     82        void setFPS(float fps);
     83        float getFPS();
    8384        const MP_STATUS getStatus();
    8485  void printInformation();
     
    8990  void getNextFrame();
    9091  void skipFrame(int frames);
    91   void gotoFrame(int frames);
     92  bool gotoFrame(int frames);
    9293
    9394};
  • branches/avi_play/src/subprojects/importer/movie_player_test.cc

    r6382 r6401  
    2222
    2323MoviePlayer* movie_player;
    24 float start_time;
    2524
    2625void Framework::moduleInit(int argc, char** argv)
     
    3433
    3534  movie_player = new MoviePlayer(argv[1]);
     35
     36 
    3637  movie_player->printInformation();
    37  
    38   //start_time = atoi(argv[2]);
    39   start_time = 0;
    4038}
    4139
     
    4846      {
    4947        case SDLK_1:
    50           movie_player->start(start_time);
     48          movie_player->start(0);
    5149          break;
    5250        case SDLK_2:
     
    6058          break;
    6159        case SDLK_9:
    62           movie_player->setSpeed(movie_player->getSpeed() - 0.5);
    63           PRINTF(0)("speed: %f\n", movie_player->getSpeed());
     60          movie_player->setFPS(movie_player->getFPS() - 1);
     61          PRINTF(0)("fps: %0.2f\n", movie_player->getFPS());
    6462          break;
    6563        case SDLK_0:
    66           movie_player->setSpeed(movie_player->getSpeed() + 0.5);
    67           PRINTF(0)("speed: %f\n", movie_player->getSpeed());
     64          movie_player->setFPS(movie_player->getFPS() + 1);
     65          PRINTF(0)("fps: %0.2f\n", movie_player->getFPS());
    6866          break;
    6967      }
Note: See TracChangeset for help on using the changeset viewer.