Ticket #3672: test.c

File test.c, 3.8 KB (added by hxuanyu, 12 years ago)
Line 
1static const char *src_filename = NULL;
2static AVFormatContext *pFormatCtx = NULL;
3static int video_stream_idx = -1, audio_stream_idx = -1;
4static AVStream *video_stream;
5static AVCodecContext *video_dec_ctx = NULL;
6static AVCodec* pCodec = NULL;
7static AVFrame *frame = NULL;
8static AVPacket pkt;
9static int video_frame_count = 0;
10static int audio_frame_count = 0;
11
12static int open_codec_context(int *stream_idx,
13 AVFormatContext *fmt_ctx, enum AVMediaType type)
14{
15 int ret;
16 AVStream *st;
17 AVCodecContext *dec_ctx = NULL;
18 AVCodec *dec = NULL;
19
20 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
21 if (ret < 0) {
22 //fprintf(stderr, "Could not find %s stream in input file '%s'\n",
23 // av_get_media_type_string(type), src_filename);
24 return ret;
25 } else {
26 *stream_idx = ret;
27 st = fmt_ctx->streams[*stream_idx];
28
29 dec_ctx = st->codec;
30 dec = avcodec_find_decoder(dec_ctx->codec_id);
31 if (!dec) {
32 fprintf(stderr, "Failed to find %s codec\n",
33 av_get_media_type_string(type));
34 return ret;
35 }
36
37 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
38 fprintf(stderr, "Failed to open %s codec\n",
39 av_get_media_type_string(type));
40 return ret;
41 }
42 }
43
44 return 0;
45}
46
47static int decode_packet(int *got_frame, int cached)
48{
49 int ret = 0;
50
51 if (pkt.stream_index == video_stream_idx) {
52 // decode video frame
53 ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
54 if (ret < 0) {
55 fprintf(stderr, "Error decoding video frame\n");
56 return ret;
57 }
58
59 if (*got_frame) {
60 printf("video_frame%s n:%d coded_n:%d Packet.dts = %I7d, pFrame->pkt_dts = %I7d, best timestamp = %I7d\n",
61 cached ? "(cached)" : "", video_frame_count++, frame->coded_picture_number,
62 pkt.dts, frame->pkt_dts, av_frame_get_best_effort_timestamp(frame)
63 );
64 }
65 }
66 return ret;
67}
68
69int main(int argc, char **argv)
70{
71 int ret = 0, got_frame;
72
73 if (argc == 1) {
74 printf("no argument provided, exit ************** \n");
75 return 0;
76 }
77 src_filename = argv[1];
78
79 printf("to play %s\n", src_filename);
80
81 avcodec_register_all();
82 av_register_all();
83
84 if (avformat_open_input(&pFormatCtx, src_filename, NULL, NULL) < 0) {
85 fprintf(stderr, "Could not open source file %s\n", src_filename);
86 exit(1);
87 }
88
89 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
90 fprintf(stderr, "Could not find stream information\n");
91 exit(1);
92 }
93 printf("rhbc73 pFormatCtx->start_time = %I64d\n", pFormatCtx->start_time);
94
95 if (open_codec_context(&video_stream_idx, pFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) {
96 video_stream = pFormatCtx->streams[video_stream_idx];
97 video_dec_ctx = video_stream->codec;
98 printf("video stream index = %d\n", video_stream_idx);
99 }
100
101 frame = avcodec_alloc_frame();
102 av_init_packet(&pkt);
103 pkt.data = NULL;
104 pkt.size = 0;
105 int frameFinished = 0;
106 int count = 0;
107
108 // read frames from the file
109 while (av_read_frame(pFormatCtx, &pkt) >= 0) {
110 printf("packet %d\n", count++);
111 }
112
113 ret = av_seek_frame(pFormatCtx, video_stream_idx, 0, AVSEEK_FLAG_BACKWARD);
114 if (ret < 0) {
115 printf("seek to 0 failed\n");
116 return 0;
117 } else { printf("seek ok\n"); }
118
119 av_init_packet(&pkt);
120 ret = av_read_frame(pFormatCtx, &pkt);
121 if (ret < 0) {
122 printf("read after seek failed *********************\n");
123 } else {
124 printf("read ok after seek(0)\n");
125 }
126
127 return 1;
128}