Ticket #3221: main.cpp

File main.cpp, 3.6 KB (added by hxuanyu, 13 years ago)

test code

Line 
1int main(int argc, char **argv)
2{
3 int ret;
4
5 if (argc == 1) {
6 printf("no argument provided, exit ************** \n");
7 return 0;
8 }
9 const char *src_filename = argv[1];
10
11 printf("to play %s\n", src_filename);
12
13 avcodec_register_all();
14 av_register_all();
15
16 AVFormatContext *pFormatCtx = NULL;
17 if (avformat_open_input(&pFormatCtx, src_filename, NULL, NULL) < 0) {
18 fprintf(stderr, "Could not open source file %s\n", src_filename);
19 exit(1);
20 }
21
22 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
23 fprintf(stderr, "Could not find stream information\n");
24 exit(1);
25 }
26 printf("rhbc73 pFormatCtx->start_time = %I64d\n", pFormatCtx->start_time);
27
28 int video_stream_idx = -1, audio_stream_idx = -1;
29 AVStream *video_stream;
30 AVCodecContext *pCodecCtx = NULL;
31 if (open_codec_context(&video_stream_idx, pFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) {
32 video_stream = pFormatCtx->streams[video_stream_idx];
33 pCodecCtx = video_stream->codec;
34 printf("video stream index = %d\n", video_stream_idx);
35 }
36
37 AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
38 avcodec_open2(pCodecCtx, pCodec, NULL);
39
40
41 AVFrame *frame = avcodec_alloc_frame();
42 AVPacket pkt;
43
44 av_init_packet(&pkt);
45 pkt.data = NULL;
46 pkt.size = 0;
47 int frameFinished = 0;
48 int count = 0;
49 while (av_read_frame(pFormatCtx, &pkt) >= 0) {
50 if (pkt.stream_index == video_stream_idx) {
51 printf("rhbc73 Packet [%d] dts = %I64d, pts = %I64d, KEY =%d\n", count++, pkt.dts, pkt.pts, (pkt.flags & AV_PKT_FLAG_KEY) != 0);
52 //if ((pkt.flags & AV_PKT_FLAG_KEY) != 0) printf(" ==== got a key packet dts = %I64d\n", pkt.dts);
53 int ret = avcodec_decode_video2(pCodecCtx, frame, &frameFinished, &pkt);
54 if (ret >=0) {
55 if (frameFinished) printf("rhbc73 ======= got a complete frame, pkt_dts = %I64d, best effort time stamp = %I64d\n", frame->pkt_dts, av_frame_get_best_effort_timestamp(frame));
56 else printf("rhbc73 **************** NO complete frame\n");
57 } else printf ("rhbc73 **************** decode failed\n");
58
59 if (pkt.dts == 10) {
60 printf("rhbc73 now try to see to packet where dts = 3\n");
61 int ret = av_seek_frame(pFormatCtx, video_stream_idx, 3, AVSEEK_FLAG_BACKWARD);
62 if (ret >= 0) {
63 printf("rhbc73 seek succeed, then continue to read\n");
64 av_init_packet(&pkt);
65 while (av_read_frame(pFormatCtx, &pkt) >= 0) {
66 if (pkt.stream_index == video_stream_idx) {
67 printf("rhbc73 Packet [%d] dts = %I64d, pts = %I64d, KEY =%d\n", count++, pkt.dts, pkt.pts, (pkt.flags & AV_PKT_FLAG_KEY) != 0);
68 int ret = avcodec_decode_video2(pCodecCtx, frame, &frameFinished, &pkt);
69 if (ret >=0) {
70 if (frameFinished) printf("rhbc73 ======= got a complete frame, pkt_dts = %I64d, best effort time stamp = %I64d\n", frame->pkt_dts, av_frame_get_best_effort_timestamp(frame));
71 else printf("rhbc73 **************** NO complete frame\n");
72 } else printf ("rhbc73 **************** decode failed\n");
73 }
74 }
75 return 1;
76 }
77 }
78 }
79 }
80
81 return 1;
82}