Changeset 6401 in orxonox.OLD for branches/avi_play/src/lib/graphics/importer/movie_player.cc
- Timestamp:
- Jan 3, 2006, 7:47:05 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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");
Note: See TracChangeset
for help on using the changeset viewer.