Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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 ;)

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.