- Timestamp:
- Jan 3, 2006, 7:47:05 PM (19 years ago)
- Location:
- branches/avi_play/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/avi_play/src/lib/graphics/importer/media_container.cc
r6382 r6401 40 40 41 41 if (filename != NULL) 42 this->loadMedia(filename); 42 { 43 if(!this->loadMedia(filename)) 44 PRINTF(1)("MediaContainer::loadMedia() failed for %s\n", filename); 45 } 43 46 } 44 47 … … 74 77 } 75 78 76 voidMediaContainer::loadMedia(const char* filename)79 bool MediaContainer::loadMedia(const char* filename) 77 80 { 78 81 // register all formats and codecs … … 81 84 // Open video file 82 85 if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 ) 86 { 83 87 PRINTF(1)("Could not open %s\n", filename); 88 return false; 89 } 84 90 85 91 // Retrieve stream information 86 92 if (av_find_stream_info(format_context) < 0) 93 { 87 94 PRINTF(1)("Could not find stream information in %s\n", filename); 95 return false; 96 } 88 97 89 98 // Find the first video stream and take it … … 91 100 92 101 if(video_stream == -1) 102 { 93 103 PRINTF(1)("Could not find a video stream in %s\n", filename); 104 return false; 105 } 94 106 95 107 // Get a pointer to the codec context for the video stream … … 101 113 codec = avcodec_find_decoder(codec_context->codec_id); 102 114 if (codec == NULL) 115 { 103 116 PRINTF(1)("Could not find codec\n"); 117 return false; 118 } 104 119 105 120 // Open codec 106 121 if (avcodec_open(codec_context, codec) < 0) 122 { 107 123 PRINTF(1)("Could not open codec\n"); 124 return false; 125 } 108 126 109 127 // Allocate video frame … … 123 141 // Calculate fps 124 142 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; 125 146 126 147 // read the frames and save them in a sequence as textures 127 148 this->loadFrames(); 149 150 return true; 128 151 } 129 152 -
branches/avi_play/src/lib/graphics/importer/media_container.h
r6373 r6401 48 48 ~MediaContainer(); 49 49 50 voidloadMedia(const char* filename);50 bool loadMedia(const char* filename); 51 51 void loadFrames(); 52 52 -
branches/avi_play/src/lib/graphics/importer/movie_player.cc
r6382 r6401 34 34 35 35 if (filename != NULL) 36 this->loadMovie(filename); 36 { 37 if(!this->loadMovie(filename)) 38 PRINTF(1)("MoviePlayer::loadMovie() failes for %s\n", filename); 39 } 37 40 } 38 41 … … 58 61 av_free(frame); 59 62 60 avcodec_default_free_buffers(codec_context);63 avcodec_default_free_buffers(codec_context); 61 64 62 65 // Close the codec … … 75 78 timer = 0; 76 79 frame_number = 0; 77 speed = 1;80 loading = false; 78 81 79 82 material = new Material; … … 88 91 } 89 92 90 voidMoviePlayer::loadMovie(const char* filename)93 bool MoviePlayer::loadMovie(const char* filename) 91 94 { 92 95 // register all formats and codecs … … 95 98 // Open video file 96 99 if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 ) 100 { 97 101 PRINTF(1)("Could not open %s\n", filename); 102 return false; 103 } 98 104 99 105 // Retrieve stream information 100 106 if (av_find_stream_info(format_context) < 0) 107 { 101 108 PRINTF(1)("Could not find stream information in %s\n", filename); 109 return false; 110 } 102 111 103 112 // Find the first video stream and take it … … 105 114 106 115 if(video_stream == -1) 116 { 107 117 PRINTF(1)("Could not find a video stream in %s\n", filename); 118 return false; 119 } 108 120 109 121 // Get a pointer to the codec context for the video stream … … 115 127 codec = avcodec_find_decoder(codec_context->codec_id); 116 128 if (codec == NULL) 129 { 117 130 PRINTF(1)("Could not find codec\n"); 131 return false; 132 } 118 133 119 134 // Open codec 120 135 if (avcodec_open(codec_context, codec) < 0) 136 { 121 137 PRINTF(1)("Could not open codec\n"); 138 return false; 139 } 122 140 123 141 // Allocate video frame … … 137 155 // Calculate fps 138 156 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; 139 160 140 161 // duration … … 155 176 NULL); 156 177 glBindTexture(GL_TEXTURE_2D, 0); 178 179 loading = true; 180 return true; 157 181 } 158 182 … … 284 308 } 285 309 286 void MoviePlayer::gotoFrame(int frames) 287 { 310 bool 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; 288 319 // seek doesnt work for the first two frames 289 320 // you will get ugly fragments … … 291 322 { 292 323 // 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; 295 332 } 296 333 else … … 298 335 // seeks to the nearest keyframe 299 336 // 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 } 301 343 302 344 // go from the keyframe to the exact position 303 345 codec_context->hurry_up = 1; 304 346 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 } 311 361 } while(1); 312 362 codec_context->hurry_up = 0; 313 363 314 frame_number = frames; 315 } 364 this->frame_number = frames; 365 } 366 367 return true; 316 368 } 317 369 318 370 void MoviePlayer::start(float start_time) 319 371 { 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 } 326 384 } 327 385 … … 356 414 { 357 415 timer += dt; 358 actual_frame = timer * fps * speed; 359 416 actual_frame = timer * fps + start_frame; 360 417 if(actual_frame != frame_number) 361 418 { … … 378 435 } 379 436 380 void MoviePlayer::set Speed(float speed)381 { 382 if( speed> 0)383 this-> speed = speed;384 } 385 386 float MoviePlayer::get Speed()387 { 388 return this-> speed;437 void MoviePlayer::setFPS(float fps) 438 { 439 if(fps > 0) 440 this->fps = fps; 441 } 442 443 float MoviePlayer::getFPS() 444 { 445 return this->fps; 389 446 } 390 447 … … 396 453 void MoviePlayer::printInformation() 397 454 { 455 if(!loading) 456 { 457 PRINTF(0)("Load first the media file with loadMovie\n"); 458 return; 459 } 460 398 461 PRINTF(0)("========================\n"); 399 462 PRINTF(0)("========================\n"); … … 410 473 PRINTF(0)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames); 411 474 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); 413 476 PRINTF(0)("========================\n"); 414 477 PRINTF(0)("= AVCodecContext =\n"); -
branches/avi_play/src/lib/graphics/importer/movie_player.h
r6382 r6401 56 56 57 57 MP_STATUS status; 58 float speed;59 58 float timer; 59 int start_frame; 60 60 int actual_frame; 61 61 int frame_number; 62 62 float fps; 63 int duration; 63 int duration; 64 bool loading; 64 65 65 66 public: … … 69 70 ~MoviePlayer(); 70 71 71 voidloadMovie(const char* filename);72 bool loadMovie(const char* filename); 72 73 73 74 void start(float start_time); … … 79 80 const void draw(); 80 81 81 void set Speed(float speed);82 float get Speed();82 void setFPS(float fps); 83 float getFPS(); 83 84 const MP_STATUS getStatus(); 84 85 void printInformation(); … … 89 90 void getNextFrame(); 90 91 void skipFrame(int frames); 91 voidgotoFrame(int frames);92 bool gotoFrame(int frames); 92 93 93 94 }; -
branches/avi_play/src/subprojects/importer/movie_player_test.cc
r6382 r6401 22 22 23 23 MoviePlayer* movie_player; 24 float start_time;25 24 26 25 void Framework::moduleInit(int argc, char** argv) … … 34 33 35 34 movie_player = new MoviePlayer(argv[1]); 35 36 36 37 movie_player->printInformation(); 37 38 //start_time = atoi(argv[2]);39 start_time = 0;40 38 } 41 39 … … 48 46 { 49 47 case SDLK_1: 50 movie_player->start( start_time);48 movie_player->start(0); 51 49 break; 52 50 case SDLK_2: … … 60 58 break; 61 59 case SDLK_9: 62 movie_player->set Speed(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()); 64 62 break; 65 63 case SDLK_0: 66 movie_player->set Speed(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()); 68 66 break; 69 67 }
Note: See TracChangeset
for help on using the changeset viewer.