Advertisement
Guest User

Untitled

a guest
Jan 9th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <libavformat/avformat.h>
  2. #include <libavcodec/avcodec.h>
  3. #include <libswscale/swscale.h>
  4.  
  5. void print_packet_frame_pts(AVFormatContext *pFormatCtx, int StreamIndex)
  6. {
  7.     AVCodecContext* pCodecCtx = pFormatCtx->streams[StreamIndex]->codec;
  8.     avcodec_open2(pCodecCtx, avcodec_find_decoder(pCodecCtx->codec_id), NULL);
  9.     AVFrame *pFrameSource = avcodec_alloc_frame();
  10.     AVPacket packet;
  11.  
  12.     int finished = 0;
  13.  
  14.     // read all packets
  15.     while(!av_read_frame(pFormatCtx, &packet))
  16.     {
  17.         // only process packets from video stream
  18.         if(packet.stream_index == StreamIndex)
  19.         {
  20.             // decode packet into frame
  21.             if(avcodec_decode_video2(pCodecCtx, pFrameSource, &finished, &packet) > 0)
  22.             {
  23.                 // check if frame is completely decoded
  24.                 if(finished)
  25.                 {
  26.                     printf("  packet.pts=%lu => frame.pts=%lu\n", packet.pts, pFrameSource->pkt_pts);
  27.                 }
  28.                 else
  29.                 {
  30.                     printf("  packet.pts=%lu => FRAME NOT FINISHED\n", packet.pts);
  31.                 }
  32.             }
  33.         }
  34.         av_free_packet(&packet);
  35.     }
  36.     av_free(pFrameSource);
  37.     pCodecCtx = NULL;
  38.  
  39.     // go back to frame 0
  40.     av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_BACKWARD);
  41.     avcodec_flush_buffers(pFormatCtx->streams[0]->codec);
  42. }
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     av_register_all();
  47.     avcodec_register_all();
  48.  
  49.     AVFormatContext *pFormatCtx = NULL;
  50.  
  51.     // open file "test.avi"
  52.     if(avformat_open_input(&pFormatCtx, "test.avi", NULL, NULL) == 0)
  53.     {
  54.         // read all packets and print pts, also decode corresponding frames and print pts
  55.         printf("\n# BEFORE avformat_find_stream_info()\n\n");
  56.         print_packet_frame_pts(pFormatCtx, 0);
  57.  
  58.         // try to get info by decoding some frames
  59.         avformat_find_stream_info(pFormatCtx, NULL);
  60.  
  61.         // FIXME: insert fix here so packet/frame pts will not be corrupted after avformat_find_stream_info()
  62.         // flush internal packet buffer ?
  63.  
  64.         // read all packets and print pts, also decode corresponding frames and print pts
  65.         printf("\n# AFTER avformat_find_stream_info()\n\n");
  66.         print_packet_frame_pts(pFormatCtx, 0);
  67.     }
  68.  
  69.     // close file
  70.     if(pFormatCtx != NULL)
  71.     {
  72.         avformat_close_input(&pFormatCtx);
  73.         pFormatCtx = NULL;
  74.         printf("\n");
  75.     }
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement