Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

branches\avi_play

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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");
Note: See TracChangeset for help on using the changeset viewer.