Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/lib/graphics/importer/media_container.cc @ 6320

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

branches\avi_play: new function

File size: 11.0 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, Stephan Lienhard
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 "media_container.h"
26
27/* header for debug output */
28#include "debug.h"
29
30
31/**
32 * Default constructor
33 */
34MediaContainer::MediaContainer(const char* filename)
35{
36  this->init();
37
38  if (filename != NULL)
39    this->loadMedia(filename);
40}
41
42MediaContainer::MediaContainer()
43{
44  this->init();
45}
46
47/**
48 * Default destructor
49 */
50MediaContainer::~MediaContainer()
51{
52  // delete all textures.
53  while(!this->texture_list.empty())
54  {
55    if (glIsTexture(this->texture_list.back()))
56      glDeleteTextures(1, &this->texture_list.back());
57    this->texture_list.pop_back();
58  }
59  glDeleteTextures(1, &texture);
60  SDL_FreeSurface(surface);
61
62  // Free the RGB image
63  delete [] buffer;
64  av_free(RGB_frame);
65
66  /* Free the frame */
67  av_free(frame);
68
69  /* Close the codec */
70  avcodec_close(codec_context);
71
72  /* Close the video file */
73  av_close_input_file(format_context);
74
75}
76
77void MediaContainer::init()
78{
79  /* set the class id for the base object */
80  this->setClassID(CL_MEDIA_CONTAINER, "MediaContainer");
81
82  /* register all formats and codecs */
83  av_register_all();
84
85  fps = 0;
86  frame_num = 0;
87}
88
89GLuint MediaContainer::getFrame(int frame_number)
90{
91  // seek doesnt work for the first two frames
92  // you will get ugly fragments
93  if(frame_number < 2)
94    return this->getNextFrame();
95  else
96  {
97    // seeks to the nearest keyframe
98    // NOTE: there is only about every 5s a keyframe!
99    av_seek_frame(format_context, video_stream, frame_number, AVSEEK_FLAG_BACKWARD);
100   
101    // go from the keyframe to the exact position
102    codec_context->hurry_up = 1;
103    do {
104      av_read_frame(format_context, &packet);
105      if(packet.pts >= frame_number-1)
106        break;
107      int frame_finished;
108      avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size);
109      av_free_packet(&packet);
110    } while(1);
111    codec_context->hurry_up = 0;
112 
113    return this->getNextFrame();
114  }
115}
116
117GLuint MediaContainer::getNextFrame()
118{
119  /* get next frame */
120  if(av_read_frame(format_context, &packet) >= 0)
121  {
122    //this->printPacketInformation();
123
124    /* Is this a packet from the video stream? */
125    if(packet.stream_index == video_stream)
126    {
127      int frame_finished;
128      // Decode video frame
129      avcodec_decode_video(codec_context, frame, &frame_finished,
130                           packet.data, packet.size);
131
132      // Free the packet that was allocated by av_read_frame
133      av_free_packet(&packet);
134     
135      // Did we get a video frame?
136      if(frame_finished)
137      {
138        frame_num++;
139        //PRINTF(1)("frame_number: %i\n", this->getFrameNumber());
140        // Conversion from YUV to RGB
141        // Most codecs return images in YUV 420 format
142        // (one luminance and two chrominance channels, with the chrominance
143        // channels samples at half the spatial resolution of the luminance channel)
144        img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame,
145                    codec_context->pix_fmt, codec_context->width, codec_context->height);
146
147        picture = (AVPicture*)RGB_frame;
148
149
150        data = 0;
151        data = new uint8_t[codec_context->width*codec_context->height*3*sizeof(uint8_t)];
152        for(int i = 0; i < codec_context->height; i++)
153          memcpy(&data[i*codec_context->width*3], picture->data[0]+i *
154                 picture->linesize[0],codec_context->width*sizeof(uint8_t)*3);
155
156        surface = SDL_CreateRGBSurfaceFrom(data, codec_context->width,
157                                           codec_context->height,24,
158                                           codec_context->width*sizeof(uint8_t)*3,
159#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
160                                           0x000000FF,
161                                           0x0000FF00,
162                                           0x00FF0000,
163                                           0
164#else
165                                           0xFF000000,
166                                           0x00FF0000,
167                                           0x0000FF00,
168                                           0
169#endif
170                                            );
171
172        // NOTE: use glTexSubImage2D, it's faster!! //
173        /* Create an OpenGL texture from the surface */
174        glGenTextures(1, &texture);
175        glBindTexture(GL_TEXTURE_2D, texture);
176        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178        // build the Texture
179        glTexImage2D(GL_TEXTURE_2D,
180                    0,
181                    GL_RGB,
182                    surface->w, surface->h,
183                    0,
184                    GL_RGB,
185                    GL_UNSIGNED_BYTE,
186                    surface->pixels);
187        // build the MipMaps
188        gluBuild2DMipmaps(GL_TEXTURE_2D,
189                          GL_RGB,
190                          surface->w,
191                          surface->h,
192                          GL_RGB,
193                          GL_UNSIGNED_BYTE,
194                          surface->pixels);
195        glBindTexture(GL_TEXTURE_2D, 0);
196
197        return texture;
198      }
199    }
200    else
201    {
202      av_free_packet(&packet);
203      this->getNextFrame();
204    }
205  }
206  else
207    return NULL;
208}
209
210void MediaContainer::skipFrame(int num_frames)
211{
212
213}
214
215vector<GLuint> MediaContainer::getFrameList()
216{
217
218  while((texture = this->getNextFrame()) != NULL)
219    texture_list.push_back(texture);
220
221  return texture_list;
222}
223
224void MediaContainer::saveCurrentFrame()
225{
226  FILE *file;
227  char filename[32];
228  int  y;
229
230  // Open file
231  sprintf(filename, "frame%i.ppm", frame_num);
232  file = fopen(filename, "wb");
233  if(file == NULL)
234        return;
235
236  // Write header
237  fprintf(file, "P6\n%d %d\n255\n", codec_context->width, codec_context->height);
238  // Write pixel data
239  for(y = 0; y < codec_context->height; y++)
240    fwrite(picture->data[0]+y * picture->linesize[0], 1, codec_context->width*3, file);
241  // Close file
242  fclose(file);
243
244  PRINTF(1)("created file: %s\n", filename);
245}
246
247void MediaContainer::loadMedia(const char* filename)
248{
249  /* Open video file */
250  if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 )
251    PRINTF(1)("Could not open %s\n", filename);
252
253  /* Retrieve stream information */
254  if (av_find_stream_info(format_context) < 0)
255    PRINTF(1)("Could not find stream information in %s\n", filename);
256
257  // Dump information about file onto standard error
258  //dump_format(pFormatCtx, 0, argv[1], false);
259
260  /* Find the first video stream and take it */
261  video_stream = -1;
262  for(int i = 0; i < format_context->nb_streams; i++)
263  {
264    // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
265    // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO)
266    if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
267    {
268      video_stream = i;
269      break;
270    }
271  }
272
273  if(video_stream == -1)
274    PRINTF(1)("Could not find a video stream in %s\n", filename);
275
276  /* Get a pointer to the codec context for the video stream */
277  // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
278  // codec_context = &format_context->streams[video_stream]->codec;
279  codec_context = format_context->streams[video_stream]->codec;
280
281  /* Find the decoder for the video stream */
282  codec = avcodec_find_decoder(codec_context->codec_id);
283  if (codec == NULL)
284    PRINTF(1)("Could not find codec\n");
285
286  /* Open codec */
287  if (avcodec_open(codec_context, codec) < 0)
288    PRINTF(1)("Could not open codec\n");
289
290  // Allocate video frame
291  frame = avcodec_alloc_frame();
292  RGB_frame = avcodec_alloc_frame();
293
294  // Determine required buffer size and allocate buffer
295  num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height);
296  buffer=new uint8_t[num_bytes];
297
298  // Assign appropriate parts of buffer to image planes in pFrameRGB
299  avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);
300
301  // Calculate fps
302  fps = av_q2d(format_context->streams[video_stream]->r_frame_rate);
303
304  // duration
305  duration = format_context->duration / 1000000LL;
306
307  frame_num = 0;
308}
309
310int MediaContainer::getHeight()
311{
312  return codec_context->height;
313}
314
315int MediaContainer::getWidth()
316{
317  return codec_context->width;
318}
319
320int MediaContainer::getFrameNumber()
321{
322  return frame_num;
323}
324
325double MediaContainer::getFPS()
326{
327  return this->fps;
328}
329
330void MediaContainer::printMediaInformation()
331{
332  PRINTF(1)("========================\n");
333  PRINTF(1)("========================\n");
334  PRINTF(1)("=    MEDIACONTAINER    =\n");
335  PRINTF(1)("========================\n");
336  PRINTF(1)("========================\n");
337  PRINTF(1)("=    AVFormatContext   =\n");
338  PRINTF(1)("========================\n");
339  PRINTF(1)("filename: %s\n", format_context->filename);
340  PRINTF(1)("nb_streams: %i\n", format_context->nb_streams);
341  PRINTF(1)("duration: (%02d:%02d:%02d)\n", duration/3600, (duration%3600)/60, duration%60);
342  PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024);
343  PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000);
344  PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames);
345  PRINTF(1)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num);
346  PRINTF(1)("fps: %0.2f\n", av_q2d(format_context->streams[video_stream]->r_frame_rate));
347  PRINTF(1)("========================\n");
348  PRINTF(1)("=    AVCodecContext    =\n");
349  PRINTF(1)("========================\n");
350  PRINTF(1)("width: %i\n", codec_context->width);
351  PRINTF(1)("height: %i\n", codec_context->height);
352  PRINTF(1)("time_base.den: %i\n", codec_context->time_base.den);
353  PRINTF(1)("time_base.num: %i\n", codec_context->time_base.num);
354  PRINTF(1)("========================\n");
355  PRINTF(1)("=       AVCodec        =\n");
356  PRINTF(1)("========================\n");
357  PRINTF(1)("codec name: %s\n", codec->name);
358  PRINTF(1)("========================\n");
359  PRINTF(1)("========================\n");
360}
361
362void MediaContainer::printPacketInformation()
363{
364  PRINTF(1)("========================\n");
365  PRINTF(1)("========================\n");
366  PRINTF(1)("=       AVPacket       =\n");
367  PRINTF(1)("========================\n");
368  PRINTF(1)("pts: %i\n", packet.pts);
369  PRINTF(1)("dts: %i\n", packet.dts);
370  PRINTF(1)("size: %i\n", packet.size);
371  PRINTF(1)("stream_index: %i\n", packet.stream_index);
372  PRINTF(1)("duration: %i\n", packet.duration);
373  PRINTF(1)("pos: %i\n", packet.pos);
374  PRINTF(1)("========================\n");
375  PRINTF(1)("========================\n");
376}
Note: See TracBrowser for help on using the repository browser.