Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/movie_player.cc @ 6713

Last change on this file since 6713 was 6600, checked in by bensch, 19 years ago

TRUNK: merged the avi_play branche again

File size: 11.3 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}
94
95bool MoviePlayer::loadMovie(const char* filename)
96{
97  // register all formats and codecs
98  av_register_all();
99
100  // Open video file
101  if (av_open_input_file(&format_context, ResourceManager::getFullName(filename), NULL, 0, NULL) !=0 )
102  {
103    PRINTF(1)("Could not open %s\n", ResourceManager::getFullName(filename));
104    return false;
105  }
106
107  // Retrieve stream information
108  if (av_find_stream_info(format_context) < 0)
109  {
110    PRINTF(1)("Could not find stream information in %s\n", ResourceManager::getFullName(filename));
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  {
119    PRINTF(1)("Could not find a video stream in %s\n", ResourceManager::getFullName(filename));
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;
181}
182
183void MoviePlayer::getNextFrame()
184{
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);
195
196      // Free the packet that was allocated by av_read_frame
197      av_free_packet(&packet);
198
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();
247}
248
249void 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
277bool MoviePlayer::gotoFrame(int frames)
278{
279  if(!loading)
280  {
281    PRINTF(0)("Load first the media file with loadMovie\n");
282    return false;
283  }
284
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    }
310
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;
330
331    this->frame_number = frames;
332  }
333
334  return true;
335}
336
337void MoviePlayer::start(float start_time)
338{
339  //start_frame = start_time * fps;
340  start_frame = 0;
341
342  if(this->gotoFrame(start_frame))
343  {
344    status = PLAY;
345    timer = 0;
346
347    this->gotoFrame(start_frame);
348
349    PRINTF(0)("start\n");
350  }
351}
352
353void MoviePlayer::resume()
354{
355  if(status == PAUSE)
356  {
357    status = PLAY;
358    PRINTF(0)("resume\n");
359  }
360}
361
362void MoviePlayer::pause()
363{
364  if(status == PLAY)
365  {
366    status = PAUSE;
367    PRINTF(0)("pause\n");
368  }
369}
370
371void MoviePlayer::stop()
372{
373  status = STOP;
374
375  PRINTF(0)("stop\n");
376}
377
378void MoviePlayer::tick(float dt)
379{
380  if(status == PLAY)
381  {
382    timer += dt;
383    actual_frame = (int)(timer * fps + start_frame);
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);
390    }
391    //PRINTF(0)("frame_number: %i\n", frame_number);
392  }
393}
394
395/*const void MoviePlayer::draw()
396{
397  material->select();
398  glBindTexture(GL_TEXTURE_2D, texture);
399  model->draw();
400
401  LightManager::getInstance()->draw();
402}*/
403
404GLuint MoviePlayer::getTexture()
405{
406  return this->texture;
407}
408
409void MoviePlayer::setFPS(float fps)
410{
411  if(fps > 0)
412    this->fps = fps;
413}
414
415float MoviePlayer::getFPS()
416{
417  return this->fps;
418}
419
420const MP_STATUS MoviePlayer::getStatus()
421{
422  return this->status;
423}
424
425void 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");
461}
Note: See TracBrowser for help on using the repository browser.