Ticket #6627: ffmpegbug.cpp

File ffmpegbug.cpp, 2.8 KB (added by leiming, 9 years ago)

program to reproduce the bug.

Line 
1extern "C" {
2#include <libavcodec/avcodec.h>
3#include <libavformat/avformat.h>
4#include <libavutil/avutil.h>
5};
6#include <stdio.h>
7
8int main()
9{
10 av_register_all();
11 AVFormatContext* ctx = 0;
12 if (avformat_open_input(&ctx, "test.mp4", NULL, NULL) != 0)
13 {
14 perror("open file failed");
15 exit(1);
16 }
17
18 AVCodec* codec = 0;
19 int atrack = av_find_best_stream(ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
20 if (atrack < 0)
21 {
22 perror("fail to find audio stream");
23 exit(1);
24 }
25
26 AVCodecContext* codecCtx = avcodec_alloc_context3(codec);
27 if (!codecCtx)
28 {
29 perror("fail to alloc codec context");
30 exit(1);
31 }
32
33 if (avcodec_parameters_to_context(codecCtx, ctx->streams[atrack]->codecpar) < 0)
34 {
35 perror("fail to set decoder param");
36 exit(1);
37 }
38
39 if (avcodec_open2(codecCtx, codec, NULL) != 0)
40 {
41 perror("fail to open codec");
42 exit(1);
43 }
44
45 for (int rep = 0; rep < 2; ++rep)
46 {
47 if (rep > 0 && av_seek_frame(ctx, atrack, -1020, AVSEEK_FLAG_BACKWARD) < 0)
48 {
49 perror("fail to seek file");
50 exit(1);
51 }
52 avcodec_flush_buffers(codecCtx);
53
54 for (;;)
55 {
56 AVPacket pkt;
57 av_init_packet(&pkt);
58 int ret = av_read_frame(ctx, &pkt);
59
60 if (ret == AVERROR_EOF)
61 break;
62 else if (ret < 0)
63 {
64 perror("read packet error");
65 exit(1);
66 }
67
68 if (pkt.pts == -1020)
69 {
70 printf("packet of pts=-1020 is %d bytes\n", pkt.size);
71
72 avcodec_flush_buffers(codecCtx);
73 }
74
75 if (pkt.stream_index == atrack)
76 {
77 AVFrame* frame = av_frame_alloc();
78 ret = avcodec_send_packet(codecCtx, &pkt);
79 if (ret == 0)
80 {
81 ret = avcodec_receive_frame(codecCtx, frame);
82 if (ret == 0)
83 {
84 if (pkt.pts == -1020)
85 {
86 printf("decode packet of pts=-1020 and got outputed frame\n");
87 }
88 }
89 else
90 {
91 if (pkt.pts == -1020)
92 {
93 printf("decode packet of pts=-1020 but not got outputed frame\n");
94 }
95 }
96 }
97
98 av_frame_free(&frame);
99 }
100
101 av_packet_unref(&pkt);
102 }
103 }
104
105 avcodec_close(codecCtx);
106 avformat_close_input(&ctx);
107
108 return 0;
109}