Changeset 6353 in orxonox.OLD for branches/avi_play/src
- Timestamp:
- Dec 30, 2005, 8:34:22 PM (19 years ago)
- Location:
- branches/avi_play/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/avi_play/src/defs/class_id.h
r5975 r6353 193 193 CL_MATERIAL = 0x00000804, 194 194 CL_TEXTURE_SEQUENCE = 0x00c04805, 195 CL_MOVIE_PLAYER = 0x00000805, 195 196 CL_MEDIA_CONTAINER = 0x00000806, 196 197 CL_OBJMODEL = 0x00000807, -
branches/avi_play/src/lib/graphics/importer/media_container.cc
r6350 r6353 125 125 buffer=new uint8_t[num_bytes]; 126 126 127 // Assign appropriate parts of buffer to image planes in pFrameRGB127 // Assign appropriate parts of buffer to image planes in RGB_frame 128 128 avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height); 129 129 … … 155 155 GLuint MediaContainer::getNextFrame() 156 156 { 157 //if (glIsTexture(texture))158 // glDeleteTextures(1, &texture);159 157 160 158 // get next frame … … 189 187 ((AVPicture*)RGB_frame)->linesize[0], 190 188 codec_context->width*sizeof(uint8_t)*3); 189 190 //avcodec_flush_buffers(codec_context); 191 191 192 192 surface = SDL_CreateRGBSurfaceFrom(data, codec_context->width, … … 228 228 GL_UNSIGNED_BYTE, 229 229 surface->pixels); 230 glBindTexture(GL_TEXTURE_2D, 0); 230 glBindTexture(GL_TEXTURE_2D, 0); 231 231 232 232 return texture; … … 235 235 { 236 236 av_free_packet(&packet); 237 this->getNextFrame();237 return this->getNextFrame(); 238 238 } 239 239 } … … 241 241 { 242 242 av_free_packet(&packet); 243 this->getNextFrame();243 return this->getNextFrame(); 244 244 } 245 245 } -
branches/avi_play/src/lib/graphics/importer/movie_player.cc
r6339 r6353 18 18 /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_MEDIA module 19 19 For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput 20 */ /*20 */ 21 21 #define DEBUG_MODULE_MEDIA 22 22 … … 29 29 30 30 31 //32 // Default constructor33 //34 31 MoviePlayer::MoviePlayer(const char* filename) 35 32 { 36 media_container = new MediaContainer(filename);37 38 33 this->init(); 34 35 if (filename != NULL) 36 this->loadMovie(filename); 39 37 } 40 38 41 39 MoviePlayer::MoviePlayer() 42 40 { 43 media_container = new MediaContainer();44 45 41 this->init(); 46 42 } … … 48 44 MoviePlayer::~MoviePlayer() 49 45 { 50 delete media_container; 46 delete material; 47 delete model; 48 49 if (glIsTexture(texture)) 50 glDeleteTextures(1, &texture); 51 SDL_FreeSurface(surface); 52 53 // Free the RGB image 54 delete [] buffer; 55 av_free(RGB_frame); 56 57 // Free the frame 58 av_free(frame); 59 60 // Close the codec 61 avcodec_close(codec_context); 62 63 // Close the video file 64 av_close_input_file(format_context); 51 65 } 52 66 53 67 void MoviePlayer::init() 54 68 { 69 // set the class id for the base object 70 this->setClassID(CL_MOVIE_PLAYER, "MoviePlayer"); 71 55 72 status = STOP; 56 73 timer = 0; 74 frame_number = 0; 57 75 speed = 1; 58 76 59 77 material = new Material; 60 61 78 material->setDiffuseMap("maps/radialTransparency.png"); 62 79 … … 71 88 void MoviePlayer::loadMovie(const char* filename) 72 89 { 73 media_container->loadMedia(filename); 74 } 75 76 void MoviePlayer::printInformation() 77 { 78 media_container->printMediaInformation(); 90 // register all formats and codecs 91 av_register_all(); 92 93 // Open video file 94 if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 ) 95 PRINTF(1)("Could not open %s\n", filename); 96 97 // Retrieve stream information 98 if (av_find_stream_info(format_context) < 0) 99 PRINTF(1)("Could not find stream information in %s\n", filename); 100 101 // Find the first video stream and take it 102 video_stream = -1; 103 for(int i = 0; i < format_context->nb_streams; i++) 104 { 105 // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) 106 // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO) 107 if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) 108 { 109 video_stream = i; 110 break; 111 } 112 } 113 114 if(video_stream == -1) 115 PRINTF(1)("Could not find a video stream in %s\n", filename); 116 117 // Get a pointer to the codec context for the video stream 118 // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) 119 // codec_context = &format_context->streams[video_stream]->codec; 120 codec_context = format_context->streams[video_stream]->codec; 121 122 // Find the decoder for the video stream 123 codec = avcodec_find_decoder(codec_context->codec_id); 124 if (codec == NULL) 125 PRINTF(1)("Could not find codec\n"); 126 127 // Open codec 128 if (avcodec_open(codec_context, codec) < 0) 129 PRINTF(1)("Could not open codec\n"); 130 131 // Allocate video frame 132 frame = avcodec_alloc_frame(); 133 RGB_frame = avcodec_alloc_frame(); 134 135 // Determine required buffer size and allocate buffer 136 num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height); 137 buffer=new uint8_t[num_bytes]; 138 139 // Assign appropriate parts of buffer to image planes in RGB_frame 140 avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height); 141 142 // Calculate fps 143 fps = av_q2d(format_context->streams[video_stream]->r_frame_rate); 144 145 // duration 146 duration = format_context->duration / 1000000LL; 147 148 // create texture 149 glGenTextures(1, &texture); 150 glBindTexture(GL_TEXTURE_2D, texture); 151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 153 glTexImage2D(GL_TEXTURE_2D, 154 0, 155 GL_RGB, 156 0, 0, 157 0, 158 GL_RGB, 159 GL_UNSIGNED_BYTE, 160 NULL); 161 glBindTexture(GL_TEXTURE_2D, 0); 162 } 163 164 void MoviePlayer::getNextFrame() 165 { 166 // get next frame 167 if(av_read_frame(format_context, &packet) >= 0) 168 { 169 // Is this a packet from the video stream? 170 if(packet.stream_index == video_stream) 171 { 172 int frame_finished; 173 // Decode video frame 174 avcodec_decode_video(codec_context, frame, &frame_finished, 175 packet.data, packet.size); 176 177 // Free the packet that was allocated by av_read_frame 178 av_free_packet(&packet); 179 180 // Did we get a video frame? 181 if(frame_finished) 182 { 183 frame_number++; 184 //PRINTF(0)("frame_number: %i\n", frame_number); 185 // Conversion from YUV to RGB 186 // Most codecs return images in YUV 420 format 187 // (one luminance and two chrominance channels, with the chrominance 188 // channels samples at half the spatial resolution of the luminance channel) 189 img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame, 190 codec_context->pix_fmt, codec_context->width, codec_context->height); 191 192 data = 0; 193 data = new uint8_t[codec_context->width*codec_context->height*3*sizeof(uint8_t)]; 194 for(int i = 0; i < codec_context->height; i++) 195 memcpy(&data[i*codec_context->width*3], ((AVPicture*)RGB_frame)->data[0]+i * 196 ((AVPicture*)RGB_frame)->linesize[0], 197 codec_context->width*sizeof(uint8_t)*3); 198 199 //avcodec_flush_buffers(codec_context); 200 201 surface = SDL_CreateRGBSurfaceFrom(data, codec_context->width, 202 codec_context->height,24, 203 codec_context->width*sizeof(uint8_t)*3, 204 #if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks 205 0x000000FF, 206 0x0000FF00, 207 0x00FF0000, 208 0 209 #else 210 0xFF000000, 211 0x00FF0000, 212 0x0000FF00, 213 0 214 #endif 215 ); 216 217 glBindTexture(GL_TEXTURE_2D, texture); 218 // update the texture 219 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 220 surface->w, surface->h, 221 GL_RGB, GL_UNSIGNED_BYTE, 222 surface->pixels); 223 // build the MipMaps 224 gluBuild2DMipmaps(GL_TEXTURE_2D, 225 GL_RGB, 226 surface->w, 227 surface->h, 228 GL_RGB, 229 GL_UNSIGNED_BYTE, 230 surface->pixels); 231 glBindTexture(GL_TEXTURE_2D, 0); 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(); 247 } 248 249 void MoviePlayer::skipFrame(int frames) 250 { 251 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); 262 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 } 272 273 this->getNextFrame(); 274 275 } 276 277 void MoviePlayer::gotoFrame(int frames) 278 { 279 // seek doesnt work for the first two frames 280 // you will get ugly fragments 281 if(frames < 2) 282 { 283 // go to the begin of the video 284 av_seek_frame(format_context, video_stream, 0, AVSEEK_FLAG_BACKWARD); 285 frame_number = 0; 286 } 287 else 288 { 289 // seeks to the nearest keyframe 290 // NOTE: there is only about every 5s a keyframe! 291 av_seek_frame(format_context, video_stream, frames, AVSEEK_FLAG_BACKWARD); 292 293 // go from the keyframe to the exact position 294 codec_context->hurry_up = 1; 295 do { 296 av_read_frame(format_context, &packet); 297 if(packet.pts >= frames-1) 298 break; 299 int frame_finished; 300 avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size); 301 av_free_packet(&packet); 302 } while(1); 303 codec_context->hurry_up = 0; 304 305 frame_number = frames; 306 } 79 307 } 80 308 … … 83 311 status = PLAY; 84 312 timer = 0; 85 fps = media_container->getFPS(); 86 87 media_container->gotoFrame(start_time * fps); 88 89 PRINTF(0)("start (start_time: %0.2f)\n", start_time * fps); 313 314 this->gotoFrame(start_time * fps); 315 316 PRINTF(0)("start\n"); 90 317 } 91 318 … … 111 338 { 112 339 status = STOP; 113 texture = NULL;114 340 115 341 PRINTF(0)("stop\n"); … … 120 346 if(status == PLAY) 121 347 { 122 current_frame = media_container->getFrameNumber();123 348 timer += dt; 124 349 actual_frame = timer * fps * speed; 125 350 126 if(actual_frame != current_frame)351 if(actual_frame != frame_number) 127 352 { 128 if(actual_frame - current_frame== 1)129 t exture = media_container->getNextFrame();353 if(actual_frame - frame_number == 1) 354 this->getNextFrame(); 130 355 else 131 t exture = media_container->skipFrame(actual_frame - current_frame- 1);356 this->skipFrame(actual_frame - frame_number - 1); 132 357 } 133 //PRINTF(0)("frame_number: %i\n", media_container->getFrameNumber()); 134 135 if(texture == NULL && current_frame != 0) 136 this->stop(); 358 //PRINTF(0)("frame_number: %i\n", frame_number); 137 359 } 138 360 } … … 162 384 return this->status; 163 385 } 164 */ 386 387 void MoviePlayer::printInformation() 388 { 389 PRINTF(0)("========================\n"); 390 PRINTF(0)("========================\n"); 391 PRINTF(0)("= MEDIACONTAINER =\n"); 392 PRINTF(0)("========================\n"); 393 PRINTF(0)("========================\n"); 394 PRINTF(0)("= AVFormatContext =\n"); 395 PRINTF(0)("========================\n"); 396 PRINTF(0)("filename: %s\n", format_context->filename); 397 PRINTF(0)("nb_streams: %i\n", format_context->nb_streams); 398 PRINTF(0)("duration: (%02d:%02d:%02d)\n", duration/3600, (duration%3600)/60, duration%60); 399 PRINTF(0)("file_size: %ikb\n", format_context->file_size/1024); 400 PRINTF(0)("bit_rate: %ikb/s\n", format_context->bit_rate/1000); 401 PRINTF(0)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames); 402 PRINTF(0)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num); 403 PRINTF(0)("fps: %0.2f\n", av_q2d(format_context->streams[video_stream]->r_frame_rate)); 404 PRINTF(0)("========================\n"); 405 PRINTF(0)("= AVCodecContext =\n"); 406 PRINTF(0)("========================\n"); 407 PRINTF(0)("width: %i\n", codec_context->width); 408 PRINTF(0)("height: %i\n", codec_context->height); 409 PRINTF(0)("time_base.den: %i\n", codec_context->time_base.den); 410 PRINTF(0)("time_base.num: %i\n", codec_context->time_base.num); 411 PRINTF(0)("========================\n"); 412 PRINTF(0)("= AVCodec =\n"); 413 PRINTF(0)("========================\n"); 414 PRINTF(0)("codec name: %s\n", codec->name); 415 PRINTF(0)("========================\n"); 416 PRINTF(0)("========================\n"); 417 } -
branches/avi_play/src/lib/graphics/importer/movie_player.h
r6339 r6353 4 4 5 5 */ 6 /* 6 7 7 #ifndef _MOVIE_PLAYER 8 8 #define _MOVIE_PLAYER 9 9 10 #include <SDL.h> 11 12 #ifdef HAVE_AVFORMAT_H 13 #include <avformat.h> 14 #else 15 #include <ffmpeg/avformat.h> 16 #endif 17 10 18 #include "glincl.h" 11 //#include "sdlincl.h"12 19 13 #include "media_container.h"14 20 #include "light.h" 15 21 #include "texture.h" … … 32 38 private: 33 39 34 MediaContainer* media_container;35 40 Model* model; 36 41 Material* material; 37 42 43 AVFormatContext* format_context; 44 AVCodecContext* codec_context; 45 AVCodec* codec; 46 AVFrame* frame; 47 AVPacket packet; 48 AVFrame* RGB_frame; 49 50 SDL_Surface* surface; 38 51 GLuint texture; 52 uint8_t* data; 53 uint8_t* buffer; 54 int num_bytes; 55 int video_stream; 39 56 40 57 MP_STATUS status; … … 42 59 float timer; 43 60 int actual_frame; 44 int current_frame; 45 float fps; 61 int frame_number; 62 float fps; 63 int duration; 46 64 47 65 public: … … 51 69 ~MoviePlayer(); 52 70 53 void init();54 71 void loadMovie(const char* filename); 55 72 … … 67 84 void printInformation(); 68 85 86 private: 87 88 void init(); 89 void getNextFrame(); 90 void skipFrame(int frames); 91 void gotoFrame(int frame); 92 69 93 }; 70 94 … … 72 96 73 97 #endif // _MOVIE_PLAYER 74 */ -
branches/avi_play/src/subprojects/importer/movie_player_test.cc
r6339 r6353 21 21 #include "movie_player.h" 22 22 23 //MoviePlayer* movie_player;23 MoviePlayer* movie_player; 24 24 25 25 void Framework::moduleInit(int argc, char** argv) 26 26 { 27 //movie_player = new MoviePlayer(argv[1]);27 movie_player = new MoviePlayer(argv[1]); 28 28 29 //movie_player->printInformation();29 movie_player->printInformation(); 30 30 } 31 31 32 32 void Framework::moduleEventHandler(SDL_Event* event) 33 33 { 34 /*switch (event->type)34 switch (event->type) 35 35 { 36 36 case SDL_KEYDOWN: … … 58 58 break; 59 59 } 60 } */60 } 61 61 } 62 62 63 63 void Framework::moduleTick(float dt) 64 64 { 65 //movie_player->tick(dt);65 movie_player->tick(dt); 66 66 } 67 67 68 68 void Framework::moduleDraw(void) const 69 69 { 70 //movie_player->draw();70 movie_player->draw(); 71 71 } 72 72 -
branches/avi_play/src/subprojects/importer/multitex.cc
r6350 r6353 78 78 counter = 0; 79 79 timer = 0; 80 PRINTF(0)("total_frames: %i\n", media_container->getFrameCount());81 80 SDL_Delay(1000); 82 81 break; … … 109 108 counter = 0; 110 109 PRINTF(0)("BEGIN\n"); 110 PRINTF(0)("total_frames: %i\n", media_container->getFrameCount()); 111 111 } 112 112 }
Note: See TracChangeset
for help on using the changeset viewer.