Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6068 in orxonox.OLD


Ignore:
Timestamp:
Dec 12, 2005, 4:32:11 PM (18 years ago)
Author:
hdavid
Message:

branches\avi_play: MediaContainer::getNextFrame() grabs a frame and saves it as a ppm image, would not try it with large video files ;)

Location:
branches/avi_play/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/avi_play/src/lib/graphics/importer/media_container.cc

    r6057 r6068  
    4141
    4242  current_frame = 0;
    43   num_frames = 0;
    44         frames_left = true;
    4543
    4644  if (filename != NULL)
     
    5452MediaContainer::~MediaContainer()
    5553{
     54  // Free the RGB image
     55  delete [] buffer;
     56  av_free(RGB_frame);
    5657
    5758  /* Free the frame */
     
    6667}
    6768
    68 /*GLuint MediaContainer::getFrame(int frame_number)
     69GLuint MediaContainer::getFrame(int frame_number)
    6970{
    7071
     
    7374GLuint MediaContainer::getNextFrame()
    7475{
    75 
    76 }*/
    77 
    78 SDL_Surface* MediaContainer::getFrame(int frame_number)
    79 {
    80 
    81 }
    82 
    83 SDL_Surface* MediaContainer::getNextFrame()
    84 {
    85         /* get next frame */
    86         if(av_read_frame(format_context, &packet) >= 0)
    87         {
    88                 frames_left = true;
    89                 current_frame++;
    90                 PRINTF(1)("current_frame: %i\n", current_frame);
    91 
    92 
    93                 /* work with the frame */
    94                 /* packet -> SDL_Surface */
    95 
    96 
    97 
    98 
    99         }
    100         else
    101                 frames_left = false;
    102 
     76  /* get next frame */
     77  if(av_read_frame(format_context, &packet) >= 0)
     78  {
     79    //this->printPacketInformation();
     80
     81    /* Is this a packet from the video stream? */
     82    if(packet.stream_index == video_stream)
     83    {
     84      int frame_finished;
     85      // Decode video frame
     86      avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size);
     87
     88      // Did we get a video frame?
     89      if(frame_finished)
     90      {
     91        current_frame++;
     92        PRINTF(1)("current_frame: %i\n", current_frame);
     93
     94        // Convert the image from its native format to RGB
     95        img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame, codec_context->pix_fmt,
     96                                        codec_context->width, codec_context->height);
     97                       
     98        // save frame
     99        this->saveCurrentFrame();
     100
     101
     102        /* RGB_picture -> texture */
     103
     104
     105        return texture;
     106      }
     107    }
     108    // Free the packet that was allocated by av_read_frame
     109    av_free_packet(&packet);
     110  }
     111  else
     112    return NULL;
     113}
     114
     115void MediaContainer::saveCurrentFrame()
     116{
     117  FILE *file;
     118  char filename[32];
     119  int  y;
     120
     121  picture = (AVPicture*)RGB_frame;
     122
     123  // Open file
     124  sprintf(filename, "frame%i.ppm", current_frame);
     125  file = fopen(filename, "wb");
     126  if(file == NULL)
     127        return;
     128
     129  // Write header
     130  fprintf(file, "P6\n%d %d\n255\n", codec_context->width, codec_context->height);
     131  // Write pixel data
     132  for(y = 0; y < codec_context->height; y++)
     133    fwrite(picture->data[0]+y * picture->linesize[0], 1, codec_context->width*3, file);
     134  // Close file
     135  fclose(file);
     136
     137  PRINTF(1)("created file: %s\n", filename);
    103138}
    104139
     
    144179  /* Open codec */
    145180  if (avcodec_open(codec_context, codec) < 0)
    146     PRINTF(1)("Could not open codec\n");       
     181    PRINTF(1)("Could not open codec\n");
     182
     183  // Allocate video frame
     184  frame = avcodec_alloc_frame();
     185  RGB_frame = avcodec_alloc_frame();
     186
     187  // Determine required buffer size and allocate buffer
     188  num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height);
     189  buffer=new uint8_t[num_bytes];
     190
     191  // Assign appropriate parts of buffer to image planes in pFrameRGB
     192  avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);   
    147193
    148194}
     
    150196int MediaContainer::getHeight()
    151197{
    152         return codec_context->height;
     198  return codec_context->height;
    153199}
    154200
    155201int MediaContainer::getWidth()
    156202{
    157         return codec_context->width;
     203  return codec_context->width;
    158204}
    159205
     
    168214}
    169215
    170 bool MediaContainer::framesLeft()
    171 {
    172         return frames_left;
    173 }
    174 
    175216void MediaContainer::printMediaInformation()
    176217{
    177         PRINTF(1)("========================\n");
    178         PRINTF(1)("========================\n");
    179         PRINTF(1)("=    MEDIACONTAINER    =\n");
    180         PRINTF(1)("========================\n");
    181         PRINTF(1)("========================\n");
    182         PRINTF(1)("=    AVFormatContext   =\n");
    183         PRINTF(1)("========================\n");
    184         PRINTF(1)("filename: %s\n", format_context->filename);
    185         PRINTF(1)("nb_streams: %i\n", format_context->nb_streams);
    186         PRINTF(1)("duration: %fs\n", format_context->duration/1000000.);
    187         PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024);
    188         PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000);
    189         PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames);
    190         //PRINTF(1)("r_frame_rate: %ifps\n", format_context->streams[video_stream]->r_frame_rate.num);
    191         PRINTF(1)("========================\n");
    192         PRINTF(1)("=    AVCodecContext    =\n");
    193         PRINTF(1)("========================\n");
    194         PRINTF(1)("width: %i\n", codec_context->width);
    195         PRINTF(1)("height: %i\n", codec_context->height);
    196         PRINTF(1)("========================\n");
    197         PRINTF(1)("=       AVCodec        =\n");
    198         PRINTF(1)("========================\n");
    199         PRINTF(1)("codec name: %s\n", codec->name);
    200         PRINTF(1)("========================\n");
    201         PRINTF(1)("========================\n");
    202 }
     218  PRINTF(1)("========================\n");
     219  PRINTF(1)("========================\n");
     220  PRINTF(1)("=    MEDIACONTAINER    =\n");
     221  PRINTF(1)("========================\n");
     222  PRINTF(1)("========================\n");
     223  PRINTF(1)("=    AVFormatContext   =\n");
     224  PRINTF(1)("========================\n");
     225  PRINTF(1)("filename: %s\n", format_context->filename);
     226  PRINTF(1)("nb_streams: %i\n", format_context->nb_streams);
     227  PRINTF(1)("duration: %fs\n", format_context->duration/1000000.);
     228  PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024);
     229  PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000);
     230  PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames);
     231  PRINTF(1)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num);
     232  PRINTF(1)("========================\n");
     233  PRINTF(1)("=    AVCodecContext    =\n");
     234  PRINTF(1)("========================\n");
     235  PRINTF(1)("width: %i\n", codec_context->width);
     236  PRINTF(1)("height: %i\n", codec_context->height);
     237  PRINTF(1)("========================\n");
     238  PRINTF(1)("=       AVCodec        =\n");
     239  PRINTF(1)("========================\n");
     240  PRINTF(1)("codec name: %s\n", codec->name);
     241  PRINTF(1)("========================\n");
     242  PRINTF(1)("========================\n");
     243}
     244
     245void MediaContainer::printPacketInformation()
     246{
     247  PRINTF(1)("========================\n");
     248  PRINTF(1)("========================\n");
     249  PRINTF(1)("=       AVPacket       =\n");
     250  PRINTF(1)("========================\n");
     251  PRINTF(1)("pts: %i\n", packet.pts);
     252  PRINTF(1)("dts: %i\n", packet.dts);
     253  PRINTF(1)("size: %i\n", packet.size);
     254  PRINTF(1)("stream_index: %i\n", packet.stream_index);
     255  PRINTF(1)("duration: %i\n", packet.duration);
     256  PRINTF(1)("pos: %i\n", packet.pos);
     257  PRINTF(1)("========================\n");
     258  PRINTF(1)("========================\n");
     259}
  • branches/avi_play/src/lib/graphics/importer/media_container.h

    r6057 r6068  
    2424using namespace std;
    2525
    26 /* Forward Declaration */
    27 struct SDL_Surface;
    28 
    2926class MediaContainer : public BaseObject
    3027{
     
    3229private:
    3330
    34         int current_frame;
    35         int num_frames;
     31  int current_frame;
     32  int num_frames;
     33  GLuint texture;
    3634
    37         AVFormatContext* format_context;
    38         AVCodecContext* codec_context;
    39         AVCodec* codec;
    40         AVFrame* frame;
    41         AVPacket packet;
     35  AVFormatContext* format_context;
     36  AVCodecContext* codec_context;
     37  AVCodec* codec;
     38  AVFrame* frame;
     39  AVPacket packet;
     40  AVPicture* picture;
     41  AVFrame* RGB_frame;
    4242       
    43         int num_bytes;
    44         int video_stream;
    45         bool frames_left;
     43  int num_bytes;
     44  uint8_t *buffer;
     45  int video_stream;
    4646
    4747public:
     
    5050  ~MediaContainer();
    5151
    52         //GLuint getFrame(int frame_number);
    53         SDL_Surface* getFrame(int frame_number);
    54         //GLuint getNextFrame();
    55         SDL_Surface* getNextFrame();
    56         void loadMedia(const char* filename);
     52  GLuint getFrame(int frame_number);
     53  GLuint getNextFrame();
     54  void loadMedia(const char* filename);
    5755       
    58         int getHeight();
    59         int getWidth();
    60         int getFrameRate();
    61         void getStream(/* stream */);
     56  int getHeight();
     57  int getWidth();
     58  int getFrameRate();
     59  void getStream(/* stream */);
    6260
    63         bool framesLeft();
    64 
    65         /* prints some information about the
    66            media file for debug reasons */
    67         void printMediaInformation();
     61  /* prints some information about the
     62     media file for debug reasons */
     63  void printMediaInformation();
     64  void printPacketInformation();
     65  void saveCurrentFrame();
    6866
    6967};
  • branches/avi_play/src/subprojects/importer/importer.cc

    r6057 r6068  
    3535  movie = new MediaContainer(argv[1]);
    3636
    37         // print information about the media file
    38         movie->printMediaInformation();
     37  // print information about the media file
     38  movie->printMediaInformation();
    3939
    40 /*
    41   ResourceManager::getInstance()->addImageDir("./");
     40  SDL_Delay(1000);
    4241
    43   for (int i = 0; i < argc; i++)
    44   {
    45     printf("%s\n", argv[i]);
    46   }
    47   ResourceManager::getInstance()->addImageDir("");
     42  // get the frame and save it as an image
     43  //while(movie->getNextFrame() != NULL);       
    4844
    49 
    50   if (argc>=3)
    51     obj = new OBJModel (argv[1], atof(argv[2]));
    52   else if (argc>=2)
    53     obj = new OBJModel(argv[1]);
    54   else
    55     obj = new PrimitiveModel(PRIM_CYLINDER);
    56 
    57   ResourceManager::getInstance()->debug();
    58 
    59   LightManager* lightMan = LightManager::getInstance();
    60   lightMan->setAmbientColor(.1,.1,.1);
    61   (new Light())->setAbsCoor(5.0, 10.0, 40.0);
    62   (new Light())->setAbsCoor(-10, -20, -100);*/
    6345}
    6446
     
    7860void Framework::moduleTick(float dt)
    7961{
    80         if(movie->framesLeft())
    81                 movie->getNextFrame();
     62  while(movie->getNextFrame() != NULL);
    8263}
    8364
    8465void Framework::moduleDraw(void) const
    8566{
    86   //obj->draw();
    8767
    88   //LightManager::getInstance()->draw();
    8968}
    9069
  • branches/avi_play/src/subprojects/importer/multitex.cc

    r6013 r6068  
    7676          obj = new PrimitiveModel(PRIM_CUBE, 10.0);
    7777          break;
    78         case SDLK_2:
     78        case SDLK_2:
    7979          obj = new PrimitiveModel(PRIM_SPHERE, 10.0);
    8080          break;
Note: See TracChangeset for help on using the changeset viewer.