Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/lib/graphics/importer/movie_player.cc @ 6510

Last change on this file since 6510 was 6510, checked in by hdavid, 18 years ago

branches/avi_play: fixed loading

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