| 1 | #include <stdio.h>
|
|---|
| 2 | #include <inttypes.h>
|
|---|
| 3 | #include <libavformat/avformat.h>
|
|---|
| 4 | #include <libavcodec/avcodec.h>
|
|---|
| 5 |
|
|---|
| 6 | int main ( int argc, char *argv[] )
|
|---|
| 7 | {
|
|---|
| 8 | AVCodec *video_codec;
|
|---|
| 9 | AVFormatContext *format_context;
|
|---|
| 10 | AVFrame *picture;
|
|---|
| 11 | int width = 0, height = 0;
|
|---|
| 12 | unsigned int i;
|
|---|
| 13 | int videostream;
|
|---|
| 14 | AVCodecContext *video_codec_context = NULL;
|
|---|
| 15 |
|
|---|
| 16 | av_register_all();
|
|---|
| 17 | av_log_set_level( AV_LOG_QUIET );
|
|---|
| 18 |
|
|---|
| 19 | if ( av_open_input_file( &format_context, argv[1], NULL, 0, NULL ) != 0)
|
|---|
| 20 | return -1;
|
|---|
| 21 |
|
|---|
| 22 | if ( av_find_stream_info( format_context ) < 0 )
|
|---|
| 23 | return -2;
|
|---|
| 24 |
|
|---|
| 25 | videostream = -1;
|
|---|
| 26 |
|
|---|
| 27 | for ( i = 0; i < format_context->nb_streams; i++ ) {
|
|---|
| 28 | if ( format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
|
|---|
| 29 | videostream = i;
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | if ( videostream == -1 )
|
|---|
| 34 | return -3;
|
|---|
| 35 |
|
|---|
| 36 | video_codec_context = format_context->streams[videostream]->codec;
|
|---|
| 37 | if (!video_codec_context)
|
|---|
| 38 | return -4;
|
|---|
| 39 |
|
|---|
| 40 | width = video_codec_context->width;
|
|---|
| 41 | height = video_codec_context->height;
|
|---|
| 42 |
|
|---|
| 43 | video_codec = avcodec_find_decoder ( video_codec_context->codec_id );
|
|---|
| 44 | if (!video_codec)
|
|---|
| 45 | return -5;
|
|---|
| 46 |
|
|---|
| 47 | picture = avcodec_alloc_frame();
|
|---|
| 48 | if (!picture)
|
|---|
| 49 | return -6;
|
|---|
| 50 |
|
|---|
| 51 | if (avcodec_open ( video_codec_context, video_codec ) < 0)
|
|---|
| 52 | return -7;
|
|---|
| 53 |
|
|---|
| 54 | AVFrame *pictured;
|
|---|
| 55 | uint8_t *picture_buf;
|
|---|
| 56 | int size;
|
|---|
| 57 |
|
|---|
| 58 | pictured = avcodec_alloc_frame();
|
|---|
| 59 | if (!pictured)
|
|---|
| 60 | return -8;
|
|---|
| 61 |
|
|---|
| 62 | size = avpicture_get_size( PIX_FMT_YUV420P, width, height );
|
|---|
| 63 |
|
|---|
| 64 | picture_buf = ( uint8_t* )av_malloc( size );
|
|---|
| 65 | if (!picture_buf)
|
|---|
| 66 | return -9;
|
|---|
| 67 |
|
|---|
| 68 | size = avpicture_fill( ( AVPicture * )pictured, picture_buf, PIX_FMT_YUV420P, width, height );
|
|---|
| 69 | if (size < 0)
|
|---|
| 70 | return -10;
|
|---|
| 71 |
|
|---|
| 72 | AVPacket packet;
|
|---|
| 73 | int got_picture, len;
|
|---|
| 74 |
|
|---|
| 75 | int64_t pts = -1;
|
|---|
| 76 |
|
|---|
| 77 | pts = atoi( argv[2] );
|
|---|
| 78 | int seek_result = 0;
|
|---|
| 79 | #ifdef NEW_SEEK
|
|---|
| 80 | seek_result = avformat_seek_file( format_context, videostream, pts, pts, pts, AVSEEK_FLAG_ANY );
|
|---|
| 81 | #else
|
|---|
| 82 | seek_result = av_seek_frame( format_context, videostream, pts, AVSEEK_FLAG_ANY );
|
|---|
| 83 | #endif
|
|---|
| 84 | printf("seek result: %d\n", seek_result);
|
|---|
| 85 |
|
|---|
| 86 | if (av_read_frame( format_context, &packet ) < 0)
|
|---|
| 87 | return -11;
|
|---|
| 88 |
|
|---|
| 89 | printf("timestamps %8" PRId64 " ", packet.dts);
|
|---|
| 90 | printf(" %8" PRId64 "", AV_NOPTS_VALUE == packet.pts ? 0 : packet.pts);
|
|---|
| 91 |
|
|---|
| 92 | if ( packet.stream_index == videostream ) {
|
|---|
| 93 | len = avcodec_decode_video2 ( video_codec_context, picture, &got_picture, &packet );
|
|---|
| 94 |
|
|---|
| 95 | if ( len < 0 )
|
|---|
| 96 | return -12;
|
|---|
| 97 | else
|
|---|
| 98 | if ( got_picture )
|
|---|
| 99 | printf(" %8" PRId64 "", AV_NOPTS_VALUE == picture->pts ? 0 : picture->pts);
|
|---|
| 100 | }
|
|---|
| 101 | printf("\n");
|
|---|
| 102 | return 0;
|
|---|
| 103 | }
|
|---|