[5939] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[6486] | 12 | main-programmer: David Hasenfratz |
---|
[5939] | 13 | co-programmer: |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_MEDIA module |
---|
| 19 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 20 | */ |
---|
| 21 | #define DEBUG_MODULE_MEDIA |
---|
| 22 | |
---|
| 23 | |
---|
[6486] | 24 | // include your own header |
---|
[5939] | 25 | #include "movie_player.h" |
---|
| 26 | |
---|
[6510] | 27 | #include "resource_manager.h" |
---|
| 28 | |
---|
[6486] | 29 | // header for debug output |
---|
[5939] | 30 | #include "debug.h" |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | MoviePlayer::MoviePlayer(const char* filename) |
---|
| 34 | { |
---|
[6486] | 35 | this->init(); |
---|
[5939] | 36 | |
---|
[6486] | 37 | if (filename != NULL) |
---|
| 38 | { |
---|
| 39 | if(!this->loadMovie(filename)) |
---|
| 40 | PRINTF(1)("MoviePlayer::loadMovie() failes for %s\n", filename); |
---|
| 41 | } |
---|
[5939] | 42 | } |
---|
| 43 | |
---|
[6486] | 44 | MoviePlayer::MoviePlayer() |
---|
| 45 | { |
---|
| 46 | this->init(); |
---|
| 47 | } |
---|
| 48 | |
---|
[5939] | 49 | MoviePlayer::~MoviePlayer() |
---|
| 50 | { |
---|
[6600] | 51 | //delete material; |
---|
| 52 | //delete model; |
---|
[5939] | 53 | |
---|
[6486] | 54 | if (glIsTexture(texture)) |
---|
| 55 | glDeleteTextures(1, &texture); |
---|
| 56 | |
---|
| 57 | // Free the RGB image |
---|
| 58 | delete [] buffer; |
---|
| 59 | av_free(RGB_frame); |
---|
| 60 | |
---|
| 61 | // Free the frame |
---|
| 62 | av_free(frame); |
---|
| 63 | |
---|
| 64 | avcodec_default_free_buffers(codec_context); |
---|
| 65 | |
---|
| 66 | // Close the codec |
---|
| 67 | avcodec_close(codec_context); |
---|
| 68 | |
---|
| 69 | // Close the video file |
---|
| 70 | av_close_input_file(format_context); |
---|
[5939] | 71 | } |
---|
| 72 | |
---|
[6486] | 73 | void MoviePlayer::init() |
---|
[5939] | 74 | { |
---|
[6486] | 75 | // set the class id for the base object |
---|
| 76 | this->setClassID(CL_MOVIE_PLAYER, "MoviePlayer"); |
---|
[5939] | 77 | |
---|
[6486] | 78 | status = STOP; |
---|
| 79 | timer = 0; |
---|
| 80 | frame_number = 0; |
---|
| 81 | loading = false; |
---|
| 82 | |
---|
[6600] | 83 | /*material = new Material; |
---|
[6486] | 84 | material->setDiffuseMap("maps/radialTransparency.png"); |
---|
| 85 | |
---|
| 86 | model = new PrimitiveModel(PRIM_PLANE, 10.0); |
---|
| 87 | |
---|
| 88 | LightManager* lightMan = LightManager::getInstance(); |
---|
| 89 | lightMan->setAmbientColor(.1,.1,.1); |
---|
| 90 | (new Light())->setAbsCoor(5.0, 10.0, 40.0); |
---|
| 91 | (new Light())->setAbsCoor(-10, -20, -100); |
---|
[6600] | 92 | */ |
---|
[5939] | 93 | } |
---|
| 94 | |
---|
[6486] | 95 | bool MoviePlayer::loadMovie(const char* filename) |
---|
[5939] | 96 | { |
---|
[6486] | 97 | // register all formats and codecs |
---|
| 98 | av_register_all(); |
---|
[5939] | 99 | |
---|
[6486] | 100 | // Open video file |
---|
[6510] | 101 | if (av_open_input_file(&format_context, ResourceManager::getFullName(filename), NULL, 0, NULL) !=0 ) |
---|
[6486] | 102 | { |
---|
[6510] | 103 | PRINTF(1)("Could not open %s\n", ResourceManager::getFullName(filename)); |
---|
[6486] | 104 | return false; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Retrieve stream information |
---|
| 108 | if (av_find_stream_info(format_context) < 0) |
---|
| 109 | { |
---|
[6510] | 110 | PRINTF(1)("Could not find stream information in %s\n", ResourceManager::getFullName(filename)); |
---|
[6486] | 111 | return false; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // Find the first video stream and take it |
---|
| 115 | video_stream = av_find_default_stream_index(format_context); |
---|
| 116 | |
---|
| 117 | if(video_stream == -1) |
---|
| 118 | { |
---|
[6510] | 119 | PRINTF(1)("Could not find a video stream in %s\n", ResourceManager::getFullName(filename)); |
---|
[6486] | 120 | return false; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | // Get a pointer to the codec context for the video stream |
---|
| 124 | codec_context = format_context->streams[video_stream]->codec; |
---|
| 125 | |
---|
| 126 | // Find the decoder for the video stream |
---|
| 127 | codec = avcodec_find_decoder(codec_context->codec_id); |
---|
| 128 | if (codec == NULL) |
---|
| 129 | { |
---|
| 130 | PRINTF(1)("Could not find codec\n"); |
---|
| 131 | return false; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | // Open codec |
---|
| 135 | if (avcodec_open(codec_context, codec) < 0) |
---|
| 136 | { |
---|
| 137 | PRINTF(1)("Could not open codec\n"); |
---|
| 138 | return false; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | // Allocate video frame |
---|
| 142 | frame = avcodec_alloc_frame(); |
---|
| 143 | RGB_frame = avcodec_alloc_frame(); |
---|
| 144 | |
---|
| 145 | // Determine required buffer size and allocate buffer |
---|
| 146 | num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height); |
---|
| 147 | buffer=new uint8_t[num_bytes]; |
---|
| 148 | |
---|
| 149 | // Assign appropriate parts of buffer to image planes in RGB_frame |
---|
| 150 | avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height); |
---|
| 151 | |
---|
| 152 | // data buffer for the texture |
---|
| 153 | data = new uint8_t[codec_context->width*codec_context->height*3*sizeof(uint8_t)]; |
---|
| 154 | |
---|
| 155 | // Calculate fps |
---|
| 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; |
---|
| 160 | |
---|
| 161 | // duration |
---|
| 162 | duration = format_context->duration / 1000000LL; |
---|
| 163 | |
---|
| 164 | // create texture |
---|
| 165 | glGenTextures(1, &texture); |
---|
| 166 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 167 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 168 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 169 | glTexImage2D(GL_TEXTURE_2D, |
---|
| 170 | 0, |
---|
| 171 | GL_RGB, |
---|
| 172 | 0, 0, |
---|
| 173 | 0, |
---|
| 174 | GL_RGB, |
---|
| 175 | GL_UNSIGNED_BYTE, |
---|
| 176 | NULL); |
---|
| 177 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 178 | |
---|
| 179 | loading = true; |
---|
| 180 | return true; |
---|
[5939] | 181 | } |
---|
| 182 | |
---|
[6486] | 183 | void MoviePlayer::getNextFrame() |
---|
[5939] | 184 | { |
---|
[6486] | 185 | // get next frame |
---|
| 186 | if(av_read_frame(format_context, &packet) >= 0) |
---|
| 187 | { |
---|
| 188 | // Is this a packet from the video stream? |
---|
| 189 | if(packet.stream_index == video_stream) |
---|
| 190 | { |
---|
| 191 | int frame_finished; |
---|
| 192 | // Decode video frame |
---|
| 193 | avcodec_decode_video(codec_context, frame, &frame_finished, |
---|
| 194 | packet.data, packet.size); |
---|
[5939] | 195 | |
---|
[6486] | 196 | // Free the packet that was allocated by av_read_frame |
---|
| 197 | av_free_packet(&packet); |
---|
[6600] | 198 | |
---|
[6486] | 199 | // Did we get a video frame? |
---|
| 200 | if(frame_finished) |
---|
| 201 | { |
---|
| 202 | frame_number++; |
---|
| 203 | //PRINTF(0)("frame_number: %i\n", frame_number); |
---|
| 204 | // Conversion from YUV to RGB |
---|
| 205 | // Most codecs return images in YUV 420 format |
---|
| 206 | // (one luminance and two chrominance channels, with the chrominance |
---|
| 207 | // channels samples at half the spatial resolution of the luminance channel) |
---|
| 208 | img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame, |
---|
| 209 | codec_context->pix_fmt, codec_context->width, codec_context->height); |
---|
| 210 | |
---|
| 211 | for(int i = 0; i < codec_context->height; i++) |
---|
| 212 | memcpy(&data[i*codec_context->width*3], ((AVPicture*)RGB_frame)->data[0]+i * |
---|
| 213 | ((AVPicture*)RGB_frame)->linesize[0], |
---|
| 214 | codec_context->width*sizeof(uint8_t)*3); |
---|
| 215 | |
---|
| 216 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 217 | // update the texture |
---|
| 218 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, |
---|
| 219 | codec_context->width, codec_context->height, |
---|
| 220 | GL_RGB, GL_UNSIGNED_BYTE, |
---|
| 221 | data); |
---|
| 222 | // build the MipMaps |
---|
| 223 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
| 224 | GL_RGB, |
---|
| 225 | codec_context->width, |
---|
| 226 | codec_context->height, |
---|
| 227 | GL_RGB, |
---|
| 228 | GL_UNSIGNED_BYTE, |
---|
| 229 | data); |
---|
| 230 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 231 | |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | av_free_packet(&packet); |
---|
| 236 | this->getNextFrame(); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | else |
---|
| 240 | { |
---|
| 241 | av_free_packet(&packet); |
---|
| 242 | this->getNextFrame(); |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | else |
---|
| 246 | this->stop(); |
---|
[5939] | 247 | } |
---|
| 248 | |
---|
[6486] | 249 | void MoviePlayer::skipFrame(int frames) |
---|
[5939] | 250 | { |
---|
[6600] | 251 | |
---|
[6486] | 252 | while(frames != 0) |
---|
| 253 | { |
---|
| 254 | if(av_read_frame(format_context, &packet) < 0) |
---|
| 255 | break; |
---|
| 256 | if(packet.stream_index == video_stream) |
---|
| 257 | { |
---|
| 258 | int frame_finished; |
---|
| 259 | // We have to decode the frame to not get ugly fragments |
---|
| 260 | avcodec_decode_video(codec_context, frame, &frame_finished, |
---|
| 261 | packet.data, packet.size); |
---|
[6600] | 262 | |
---|
[6486] | 263 | // Did we get a video frame? |
---|
| 264 | if(frame_finished) |
---|
| 265 | { |
---|
| 266 | frames--; |
---|
| 267 | frame_number++; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | av_free_packet(&packet); |
---|
| 271 | } |
---|
[6600] | 272 | |
---|
[6486] | 273 | this->getNextFrame(); |
---|
[5939] | 274 | |
---|
| 275 | } |
---|
| 276 | |
---|
[6486] | 277 | bool MoviePlayer::gotoFrame(int frames) |
---|
[5939] | 278 | { |
---|
[6486] | 279 | if(!loading) |
---|
| 280 | { |
---|
| 281 | PRINTF(0)("Load first the media file with loadMovie\n"); |
---|
| 282 | return false; |
---|
| 283 | } |
---|
[5939] | 284 | |
---|
[6486] | 285 | int err; |
---|
| 286 | // seek doesnt work for the first two frames |
---|
| 287 | // you will get ugly fragments |
---|
| 288 | if(frames < 2) |
---|
| 289 | { |
---|
| 290 | // go to the begin of the video |
---|
| 291 | err = av_seek_frame(format_context, video_stream, 0, AVSEEK_FLAG_BACKWARD); |
---|
| 292 | if(err < 0) |
---|
| 293 | { |
---|
| 294 | PRINTF(1)("Could not seek to frame 0\n"); |
---|
| 295 | return false; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | this->frame_number = 0; |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | { |
---|
| 302 | // seeks to the nearest keyframe |
---|
| 303 | // NOTE: there is only about every 5s a keyframe! |
---|
| 304 | err = av_seek_frame(format_context, video_stream, frames, AVSEEK_FLAG_BACKWARD); |
---|
| 305 | if(err < 0) |
---|
| 306 | { |
---|
| 307 | PRINTF(1)("Could not seek to frame %i\n", frames); |
---|
| 308 | return false; |
---|
| 309 | } |
---|
[6600] | 310 | |
---|
[6486] | 311 | // go from the keyframe to the exact position |
---|
| 312 | codec_context->hurry_up = 1; |
---|
| 313 | do { |
---|
| 314 | if(av_read_frame(format_context, &packet) < 0) |
---|
| 315 | { |
---|
| 316 | PRINTF(1)("Could not seek to frame %i\n", frames); |
---|
| 317 | return false; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | if(packet.stream_index == video_stream) |
---|
| 321 | { |
---|
| 322 | if(packet.pts >= frames-1) |
---|
| 323 | break; |
---|
| 324 | int frame_finished; |
---|
| 325 | avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size); |
---|
| 326 | av_free_packet(&packet); |
---|
| 327 | } |
---|
| 328 | } while(1); |
---|
| 329 | codec_context->hurry_up = 0; |
---|
[6600] | 330 | |
---|
[6486] | 331 | this->frame_number = frames; |
---|
| 332 | } |
---|
[6600] | 333 | |
---|
[6486] | 334 | return true; |
---|
[5939] | 335 | } |
---|
| 336 | |
---|
[6486] | 337 | void MoviePlayer::start(float start_time) |
---|
[5939] | 338 | { |
---|
[6486] | 339 | //start_frame = start_time * fps; |
---|
| 340 | start_frame = 0; |
---|
[5939] | 341 | |
---|
[6486] | 342 | if(this->gotoFrame(start_frame)) |
---|
| 343 | { |
---|
| 344 | status = PLAY; |
---|
| 345 | timer = 0; |
---|
[6600] | 346 | |
---|
[6486] | 347 | this->gotoFrame(start_frame); |
---|
[6600] | 348 | |
---|
[6486] | 349 | PRINTF(0)("start\n"); |
---|
| 350 | } |
---|
[5939] | 351 | } |
---|
| 352 | |
---|
[6486] | 353 | void MoviePlayer::resume() |
---|
[5939] | 354 | { |
---|
[6486] | 355 | if(status == PAUSE) |
---|
| 356 | { |
---|
| 357 | status = PLAY; |
---|
| 358 | PRINTF(0)("resume\n"); |
---|
| 359 | } |
---|
| 360 | } |
---|
[5939] | 361 | |
---|
[6486] | 362 | void MoviePlayer::pause() |
---|
| 363 | { |
---|
| 364 | if(status == PLAY) |
---|
| 365 | { |
---|
| 366 | status = PAUSE; |
---|
| 367 | PRINTF(0)("pause\n"); |
---|
| 368 | } |
---|
[5939] | 369 | } |
---|
| 370 | |
---|
[6486] | 371 | void MoviePlayer::stop() |
---|
[5939] | 372 | { |
---|
[6486] | 373 | status = STOP; |
---|
[5939] | 374 | |
---|
[6486] | 375 | PRINTF(0)("stop\n"); |
---|
[5939] | 376 | } |
---|
[5950] | 377 | |
---|
[6486] | 378 | void MoviePlayer::tick(float dt) |
---|
| 379 | { |
---|
| 380 | if(status == PLAY) |
---|
| 381 | { |
---|
| 382 | timer += dt; |
---|
[6600] | 383 | actual_frame = (int)(timer * fps + start_frame); |
---|
[6486] | 384 | if(actual_frame != frame_number) |
---|
| 385 | { |
---|
| 386 | if(actual_frame - frame_number == 1) |
---|
| 387 | this->getNextFrame(); |
---|
| 388 | else |
---|
| 389 | this->skipFrame(actual_frame - frame_number - 1); |
---|
[6600] | 390 | } |
---|
[6486] | 391 | //PRINTF(0)("frame_number: %i\n", frame_number); |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
[6600] | 395 | /*const void MoviePlayer::draw() |
---|
[6486] | 396 | { |
---|
| 397 | material->select(); |
---|
| 398 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 399 | model->draw(); |
---|
| 400 | |
---|
| 401 | LightManager::getInstance()->draw(); |
---|
[6600] | 402 | }*/ |
---|
| 403 | |
---|
| 404 | GLuint MoviePlayer::getTexture() |
---|
| 405 | { |
---|
| 406 | return this->texture; |
---|
[6486] | 407 | } |
---|
| 408 | |
---|
| 409 | void MoviePlayer::setFPS(float fps) |
---|
| 410 | { |
---|
| 411 | if(fps > 0) |
---|
| 412 | this->fps = fps; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | float MoviePlayer::getFPS() |
---|
| 416 | { |
---|
| 417 | return this->fps; |
---|
| 418 | } |
---|
| 419 | |
---|
[5950] | 420 | const MP_STATUS MoviePlayer::getStatus() |
---|
| 421 | { |
---|
[6486] | 422 | return this->status; |
---|
| 423 | } |
---|
[5950] | 424 | |
---|
[6486] | 425 | void MoviePlayer::printInformation() |
---|
| 426 | { |
---|
| 427 | if(!loading) |
---|
| 428 | { |
---|
| 429 | PRINTF(0)("Load first the media file with loadMovie\n"); |
---|
| 430 | return; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | PRINTF(0)("========================\n"); |
---|
| 434 | PRINTF(0)("========================\n"); |
---|
| 435 | PRINTF(0)("= MEDIACONTAINER =\n"); |
---|
| 436 | PRINTF(0)("========================\n"); |
---|
| 437 | PRINTF(0)("========================\n"); |
---|
| 438 | PRINTF(0)("= AVFormatContext =\n"); |
---|
| 439 | PRINTF(0)("========================\n"); |
---|
| 440 | PRINTF(0)("filename: %s\n", format_context->filename); |
---|
| 441 | PRINTF(0)("nb_streams: %i\n", format_context->nb_streams); |
---|
| 442 | PRINTF(0)("duration: (%02d:%02d:%02d)\n", duration/3600, (duration%3600)/60, duration%60); |
---|
| 443 | PRINTF(0)("file_size: %ikb\n", format_context->file_size/1024); |
---|
| 444 | PRINTF(0)("bit_rate: %ikb/s\n", format_context->bit_rate/1000); |
---|
| 445 | PRINTF(0)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames); |
---|
| 446 | PRINTF(0)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num); |
---|
| 447 | PRINTF(0)("fps: %0.2f\n", fps); |
---|
| 448 | PRINTF(0)("========================\n"); |
---|
| 449 | PRINTF(0)("= AVCodecContext =\n"); |
---|
| 450 | PRINTF(0)("========================\n"); |
---|
| 451 | PRINTF(0)("width: %i\n", codec_context->width); |
---|
| 452 | PRINTF(0)("height: %i\n", codec_context->height); |
---|
| 453 | PRINTF(0)("time_base.den: %i\n", codec_context->time_base.den); |
---|
| 454 | PRINTF(0)("time_base.num: %i\n", codec_context->time_base.num); |
---|
| 455 | PRINTF(0)("========================\n"); |
---|
| 456 | PRINTF(0)("= AVCodec =\n"); |
---|
| 457 | PRINTF(0)("========================\n"); |
---|
| 458 | PRINTF(0)("codec name: %s\n", codec->name); |
---|
| 459 | PRINTF(0)("========================\n"); |
---|
| 460 | PRINTF(0)("========================\n"); |
---|
[5950] | 461 | } |
---|