Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libswscale/swscale.h>
- void print_packet_pts_decoded_pts(AVFormatContext *pFormatCtx, int StreamIndex)
- {
- printf("\n# PRINTING TIMESTAMPS OF ALL PACKETS & DECODED FRAMES \n\n");
- AVCodecContext* pCodecCtx = pFormatCtx->streams[StreamIndex]->codec;
- avcodec_open2(pCodecCtx, avcodec_find_decoder(pCodecCtx->codec_id), NULL);
- AVFrame *pFrameDecoded = avcodec_alloc_frame();
- AVPacket packet;
- int got_picture;
- while(!av_read_frame(pFormatCtx, &packet))
- {
- if(packet.stream_index == StreamIndex)
- {
- if(avcodec_decode_video2(pCodecCtx, pFrameDecoded, &got_picture, &packet) > 0)
- {
- if(got_picture)
- {
- printf(" packet.pts=%lu => frame.pts=%lu\n", packet.pts, pFrameDecoded->pkt_pts);
- }
- else
- {
- printf(" packet.pts=%lu => GOT NO PICTURE\n", packet.pts);
- }
- }
- }
- av_free_packet(&packet);
- }
- //printf("\n# DECODING FRAMES FROM PACKET BUFFER...\n\n");
- AVPacket empty_packet;
- av_init_packet(&empty_packet);
- while(got_picture)
- {
- avcodec_decode_video2(pCodecCtx, pFrameDecoded, &got_picture, &empty_packet);
- if(got_picture)
- {
- printf(" USE EMPTY PACKET => frame.pts=%lu\n", pFrameDecoded->pkt_pts);
- }
- else
- {
- //printf(" DECODING DONE\n");
- }
- }
- av_free(pFrameDecoded);
- pCodecCtx = NULL;
- // go back to first packet/frame
- av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_BACKWARD);
- avcodec_flush_buffers(pFormatCtx->streams[0]->codec);
- }
- int main(int argc, char *argv[])
- {
- av_register_all();
- avcodec_register_all();
- AVFormatContext *pFormatCtx;
- // open file "10_frames_with_numberes.avi"
- if(avformat_open_input(&pFormatCtx, "10_frames_with_numberes.avi", NULL, NULL) == 0)
- {
- print_packet_pts_decoded_pts(pFormatCtx, 0);
- printf("\n# avformat_find_stream_info()\n");
- avformat_find_stream_info(pFormatCtx, NULL); // try to get info by decoding some frames
- print_packet_pts_decoded_pts(pFormatCtx, 0);
- }
- // close file
- if(pFormatCtx != NULL)
- {
- avformat_close_input(&pFormatCtx);
- pFormatCtx = NULL;
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement