Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches\avi_play: some copy, pasta & delete

File size: 7.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  // set the class id for the base object
37  this->setClassID(CL_MEDIA_CONTAINER, "MediaContainer");
38
39  fps = 0;
40
41  if (filename != NULL)
42    this->loadMedia(filename);
43}
44
45MediaContainer::MediaContainer()
46{
47  // set the class id for the base object
48  this->setClassID(CL_MEDIA_CONTAINER, "MediaContainer");
49
50  fps = 0;
51}
52
53/**
54 * Default destructor
55 */
56MediaContainer::~MediaContainer()
57{
58  if (glIsTexture(texture))
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
76void MediaContainer::loadMedia(const char* filename)
77{
78  // register all formats and codecs
79  av_register_all();
80
81  // Open video file
82  if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 )
83    PRINTF(1)("Could not open %s\n", filename);
84
85  // Retrieve stream information
86  if (av_find_stream_info(format_context) < 0)
87    PRINTF(1)("Could not find stream information in %s\n", filename);
88
89  // Find the first video stream and take it
90  video_stream = -1;
91  for(int i = 0; i < format_context->nb_streams; i++)
92  {
93    // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
94    // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO)
95    if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
96    {
97      video_stream = i;
98      break;
99    }
100  }
101
102  if(video_stream == -1)
103    PRINTF(1)("Could not find a video stream in %s\n", filename);
104
105  // Get a pointer to the codec context for the video stream
106  // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
107  // codec_context = &format_context->streams[video_stream]->codec;
108  codec_context = format_context->streams[video_stream]->codec;
109
110  // Find the decoder for the video stream
111  codec = avcodec_find_decoder(codec_context->codec_id);
112  if (codec == NULL)
113    PRINTF(1)("Could not find codec\n");
114
115  // Open codec
116  if (avcodec_open(codec_context, codec) < 0)
117    PRINTF(1)("Could not open codec\n");
118
119  // Allocate video frame
120  frame = avcodec_alloc_frame();
121  RGB_frame = avcodec_alloc_frame();
122
123  // Determine required buffer size and allocate buffer
124  num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height);
125  buffer=new uint8_t[num_bytes];
126
127  // Assign appropriate parts of buffer to image planes in pFrameRGB
128  avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);
129
130  // Calculate fps
131  fps = av_q2d(format_context->streams[video_stream]->r_frame_rate);
132
133  // read the frames and save them in a sequence as textures
134  this->loadFrames();
135}
136
137double MediaContainer::getFPS()
138{
139  return this->fps;
140}
141
142void MediaContainer::loadFrames()
143{
144  // go to the begin of the video
145  av_seek_frame(format_context, video_stream, 0, AVSEEK_FLAG_BACKWARD);
146
147  // get all the frames and save them in the sequence
148  while(this->addFrame(this->getNextFrame()) != NULL);
149}
150
151
152GLuint MediaContainer::getNextFrame()
153{
154  // get next frame
155  if(av_read_frame(format_context, &packet) >= 0)
156  {
157    // Is this a packet from the video stream?
158    if(packet.stream_index == video_stream)
159    {
160      int frame_finished;
161      // Decode video frame
162      avcodec_decode_video(codec_context, frame, &frame_finished,
163                           packet.data, packet.size);
164
165      // Free the packet that was allocated by av_read_frame
166      av_free_packet(&packet);
167     
168      // Did we get a video frame?
169      if(frame_finished)
170      {
171        // Conversion from YUV to RGB
172        // Most codecs return images in YUV 420 format
173        // (one luminance and two chrominance channels, with the chrominance
174        // channels samples at half the spatial resolution of the luminance channel)
175        img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame,
176                    codec_context->pix_fmt, codec_context->width, codec_context->height);
177
178        data = 0;
179        data = new uint8_t[codec_context->width*codec_context->height*3*sizeof(uint8_t)];
180        for(int i = 0; i < codec_context->height; i++)
181          memcpy(&data[i*codec_context->width*3],
182                 ((AVPicture*)RGB_frame)->data[0]+i *
183                 ((AVPicture*)RGB_frame)->linesize[0], 
184                  codec_context->width*sizeof(uint8_t)*3);
185
186        surface = SDL_CreateRGBSurfaceFrom(data, codec_context->width,
187                                           codec_context->height,24,
188                                           codec_context->width*sizeof(uint8_t)*3,
189#if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks
190                                           0x000000FF,
191                                           0x0000FF00,
192                                           0x00FF0000,
193                                           0
194#else
195                                           0xFF000000,
196                                           0x00FF0000,
197                                           0x0000FF00,
198                                           0
199#endif
200                                            );
201
202        // Create an OpenGL texture from the surface
203        glGenTextures(1, &texture);
204        glBindTexture(GL_TEXTURE_2D, texture);
205        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
206        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
207        // create the texture
208        glTexImage2D(GL_TEXTURE_2D,
209                    0,
210                    GL_RGB,
211                    surface->w, surface->h,
212                    0,
213                    GL_RGB,
214                    GL_UNSIGNED_BYTE,
215                    surface->pixels);
216        // build the MipMaps
217        gluBuild2DMipmaps(GL_TEXTURE_2D,
218                        GL_RGB,
219                        surface->w,
220                        surface->h,
221                        GL_RGB,
222                        GL_UNSIGNED_BYTE,
223                        surface->pixels);
224        glBindTexture(GL_TEXTURE_2D, 0);
225       
226        return texture;
227      }
228      else
229      {
230        av_free_packet(&packet);
231        this->getNextFrame();
232      }
233    }
234    else
235    {
236      av_free_packet(&packet);
237      this->getNextFrame();
238    }
239  }
240  else
241    return NULL;
242}
Note: See TracBrowser for help on using the repository browser.