Advertisement
Guest User

Untitled

a guest
Feb 5th, 2013
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <libavformat/avformat.h>
  2. #include <libavcodec/avcodec.h>
  3. #include <libswscale/swscale.h>
  4.  
  5. void print_packet_pts_decoded_pts(AVFormatContext *pFormatCtx, int StreamIndex)
  6. {
  7.     printf("\n# PRINTING TIMESTAMPS OF ALL PACKETS & DECODED FRAMES \n\n");
  8.  
  9.     AVCodecContext* pCodecCtx = pFormatCtx->streams[StreamIndex]->codec;
  10.     avcodec_open2(pCodecCtx, avcodec_find_decoder(pCodecCtx->codec_id), NULL);
  11.     AVFrame *pFrameDecoded = avcodec_alloc_frame();
  12.     AVPacket packet;
  13.  
  14.     int got_picture;
  15.  
  16.     while(!av_read_frame(pFormatCtx, &packet))
  17.     {
  18.         if(packet.stream_index == StreamIndex)
  19.         {
  20.             if(avcodec_decode_video2(pCodecCtx, pFrameDecoded, &got_picture, &packet) > 0)
  21.             {
  22.                 if(got_picture)
  23.                 {
  24.                     printf("  packet.pts=%lu => frame.pts=%lu\n", packet.pts, pFrameDecoded->pkt_pts);
  25.                 }
  26.                 else
  27.                 {
  28.                     printf("  packet.pts=%lu => GOT NO PICTURE\n", packet.pts);
  29.                 }
  30.             }
  31.         }
  32.         av_free_packet(&packet);
  33.     }
  34.  
  35. //printf("\n# DECODING FRAMES FROM PACKET BUFFER...\n\n");
  36.  
  37.     AVPacket empty_packet;
  38.     av_init_packet(&empty_packet);
  39.  
  40.     while(got_picture)
  41.     {
  42.         avcodec_decode_video2(pCodecCtx, pFrameDecoded, &got_picture, &empty_packet);
  43.  
  44.         if(got_picture)
  45.         {
  46.             printf("  USE EMPTY PACKET => frame.pts=%lu\n", pFrameDecoded->pkt_pts);
  47.         }
  48.         else
  49.         {
  50.             //printf("  DECODING DONE\n");
  51.         }
  52.     }
  53.  
  54.     av_free(pFrameDecoded);
  55.     pCodecCtx = NULL;
  56.  
  57.     // go back to first packet/frame
  58.     av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_BACKWARD);
  59.     avcodec_flush_buffers(pFormatCtx->streams[0]->codec);
  60. }
  61.  
  62. int main(int argc, char *argv[])
  63. {
  64.     av_register_all();
  65.     avcodec_register_all();
  66.  
  67.     AVFormatContext *pFormatCtx;
  68.  
  69.     // open file "10_frames_with_numberes.avi"
  70.     if(avformat_open_input(&pFormatCtx, "10_frames_with_numberes.avi", NULL, NULL) == 0)
  71.     {
  72.         print_packet_pts_decoded_pts(pFormatCtx, 0);
  73.  
  74.         printf("\n# avformat_find_stream_info()\n");
  75.         avformat_find_stream_info(pFormatCtx, NULL); // try to get info by decoding some frames
  76.  
  77.         print_packet_pts_decoded_pts(pFormatCtx, 0);
  78.     }
  79.  
  80.     // close file
  81.     if(pFormatCtx != NULL)
  82.     {
  83.         avformat_close_input(&pFormatCtx);
  84.         pFormatCtx = NULL;
  85.         printf("\n");
  86.     }
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement