Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches\avi_play: added ability to get the frames in a list

File size: 8.8 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  /* register all formats and codecs */
40  av_register_all();
41
42  fps = 0;
43
44  if (filename != NULL)
45    this->loadMedia(filename);
46
47}
48
49/**
50 * Default destructor
51 */
52MediaContainer::~MediaContainer()
53{
54  // delete all surfaces in the list
55  while(!this->surface_list.empty())
56  {
57    SDL_FreeSurface(this->surface_list.back());
58    this->surface_list.pop_back();
59  }
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
77SDL_Surface* MediaContainer::getFrame(int frame_number)
78{
79
80}
81
82SDL_Surface* MediaContainer::getNextFrame()
83{
84  /* get next frame */
85  if(av_read_frame(format_context, &packet) >= 0)
86  {
87    //this->printPacketInformation();
88
89    /* Is this a packet from the video stream? */
90    if(packet.stream_index == video_stream)
91    {
92      int frame_finished;
93      // Decode video frame
94      avcodec_decode_video(codec_context, frame, &frame_finished,
95                           packet.data, packet.size);
96
97      // Did we get a video frame?
98      if(frame_finished)
99      {
100        // Conversion from YUV to RGB
101        // Most codecs return images in YUV 420 format
102        // (one luminance and two chrominance channels, with the chrominance
103        // channels samples at half the spatial resolution of the luminance channel)
104        img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame,
105                    codec_context->pix_fmt, codec_context->width, codec_context->height);
106
107        picture = (AVPicture*)RGB_frame;
108
109
110        data = 0;
111        data = new uint8_t[codec_context->width*codec_context->height*3*sizeof(uint8_t)];
112        for(int i = 0; i < codec_context->height; i++)
113          memcpy(&data[i*codec_context->width*3], picture->data[0]+i * picture->linesize[0],codec_context->width*sizeof(uint8_t)*3);
114
115        surface = SDL_CreateRGBSurfaceFrom(data, codec_context->width,
116                                           codec_context->height,24,
117                                           codec_context->width*sizeof(uint8_t)*3,
118#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
119                                           0x000000FF,
120                                           0x0000FF00,
121                                           0x00FF0000,
122                                           0
123#else
124                                           0xFF000000,
125                                           0x00FF0000,
126                                           0x0000FF00,
127                                           0
128#endif
129                                            );
130
131        return surface;
132      }
133    }
134    // Free the packet that was allocated by av_read_frame
135    av_free_packet(&packet);
136  }
137  else
138    return NULL;
139}
140
141vector<SDL_Surface*> MediaContainer::getFrameList()
142{
143
144  while((surface = this->getNextFrame()) != NULL)
145    surface_list.push_back(surface);
146
147  return surface_list;
148}
149
150void MediaContainer::saveCurrentFrame()
151{
152  FILE *file;
153  char filename[32];
154  int  y;
155
156  // Open file
157  sprintf(filename, "frame%i.ppm", codec_context->frame_number);
158  file = fopen(filename, "wb");
159  if(file == NULL)
160        return;
161
162  // Write header
163  fprintf(file, "P6\n%d %d\n255\n", codec_context->width, codec_context->height);
164  // Write pixel data
165  for(y = 0; y < codec_context->height; y++)
166    fwrite(picture->data[0]+y * picture->linesize[0], 1, codec_context->width*3, file);
167  // Close file
168  fclose(file);
169
170  PRINTF(1)("created file: %s\n", filename);
171}
172
173void MediaContainer::loadMedia(const char* filename)
174{
175  /* Open video file */
176  if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 )
177    PRINTF(1)("Could not open %s\n", filename);
178
179  /* Retrieve stream information */
180  if (av_find_stream_info(format_context) < 0)
181    PRINTF(1)("Could not find stream information in %s\n", filename);
182
183  // Dump information about file onto standard error
184  //dump_format(pFormatCtx, 0, argv[1], false);
185
186  /* Find the first video stream and take it */
187  video_stream = -1;
188  for(int i = 0; i < format_context->nb_streams; i++)
189  {
190    // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
191    // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO)
192    if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
193    {
194      video_stream = i;
195      break;
196    }
197  }
198
199  if(video_stream == -1)
200    PRINTF(1)("Could not find a video stream in %s\n", filename);
201
202  /* Get a pointer to the codec context for the video stream */
203  // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis)
204  // codec_context = &format_context->streams[video_stream]->codec;
205  codec_context = format_context->streams[video_stream]->codec;
206
207  /* Find the decoder for the video stream */
208  codec = avcodec_find_decoder(codec_context->codec_id);
209  if (codec == NULL)
210    PRINTF(1)("Could not find codec\n");
211
212  /* Open codec */
213  if (avcodec_open(codec_context, codec) < 0)
214    PRINTF(1)("Could not open codec\n");
215
216  // Allocate video frame
217  frame = avcodec_alloc_frame();
218  RGB_frame = avcodec_alloc_frame();
219
220  // Determine required buffer size and allocate buffer
221  num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height);
222  buffer=new uint8_t[num_bytes];
223
224  // Assign appropriate parts of buffer to image planes in pFrameRGB
225  avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);
226
227  // Calculate fps
228  fps = av_q2d(format_context->streams[video_stream]->r_frame_rate);
229}
230
231int MediaContainer::getHeight()
232{
233  return codec_context->height;
234}
235
236int MediaContainer::getWidth()
237{
238  return codec_context->width;
239}
240
241int MediaContainer::getFrameNumber()
242{
243  return codec_context->frame_number;
244}
245
246double MediaContainer::getFPS()
247{
248  return this->fps;
249}
250
251void MediaContainer::printMediaInformation()
252{
253  PRINTF(1)("========================\n");
254  PRINTF(1)("========================\n");
255  PRINTF(1)("=    MEDIACONTAINER    =\n");
256  PRINTF(1)("========================\n");
257  PRINTF(1)("========================\n");
258  PRINTF(1)("=    AVFormatContext   =\n");
259  PRINTF(1)("========================\n");
260  PRINTF(1)("filename: %s\n", format_context->filename);
261  PRINTF(1)("nb_streams: %i\n", format_context->nb_streams);
262  PRINTF(1)("duration: %fs\n", format_context->duration/1000000.);
263  PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024);
264  PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000);
265  PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames);
266  PRINTF(1)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num);
267  PRINTF(1)("FPS: %f\n", av_q2d(format_context->streams[video_stream]->r_frame_rate));
268  PRINTF(1)("========================\n");
269  PRINTF(1)("=    AVCodecContext    =\n");
270  PRINTF(1)("========================\n");
271  PRINTF(1)("width: %i\n", codec_context->width);
272  PRINTF(1)("height: %i\n", codec_context->height);
273  PRINTF(1)("time_base.den: %i\n", codec_context->time_base.den);
274  PRINTF(1)("time_base.num: %i\n", codec_context->time_base.num);
275  PRINTF(1)("========================\n");
276  PRINTF(1)("=       AVCodec        =\n");
277  PRINTF(1)("========================\n");
278  PRINTF(1)("codec name: %s\n", codec->name);
279  PRINTF(1)("========================\n");
280  PRINTF(1)("========================\n");
281}
282
283void MediaContainer::printPacketInformation()
284{
285  PRINTF(1)("========================\n");
286  PRINTF(1)("========================\n");
287  PRINTF(1)("=       AVPacket       =\n");
288  PRINTF(1)("========================\n");
289  PRINTF(1)("pts: %i\n", packet.pts);
290  PRINTF(1)("dts: %i\n", packet.dts);
291  PRINTF(1)("size: %i\n", packet.size);
292  PRINTF(1)("stream_index: %i\n", packet.stream_index);
293  PRINTF(1)("duration: %i\n", packet.duration);
294  PRINTF(1)("pos: %i\n", packet.pos);
295  PRINTF(1)("========================\n");
296  PRINTF(1)("========================\n");
297}
Note: See TracBrowser for help on using the repository browser.