| 1 | static int open_codec_context(int *stream_idx,
|
|---|
| 2 | AVFormatContext *fmt_ctx, enum AVMediaType type)
|
|---|
| 3 | {
|
|---|
| 4 | int ret;
|
|---|
| 5 | AVStream *st;
|
|---|
| 6 | AVCodecContext *dec_ctx = NULL;
|
|---|
| 7 | AVCodec *dec = NULL;
|
|---|
| 8 |
|
|---|
| 9 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
|
|---|
| 10 | if (ret < 0) {
|
|---|
| 11 | //fprintf(stderr, "Could not find %s stream in input file '%s'\n",
|
|---|
| 12 | // av_get_media_type_string(type), src_filename);
|
|---|
| 13 | return ret;
|
|---|
| 14 | } else {
|
|---|
| 15 | *stream_idx = ret;
|
|---|
| 16 | st = fmt_ctx->streams[*stream_idx];
|
|---|
| 17 |
|
|---|
| 18 | /* find decoder for the stream */
|
|---|
| 19 | dec_ctx = st->codec;
|
|---|
| 20 | dec = avcodec_find_decoder(dec_ctx->codec_id);
|
|---|
| 21 | if (!dec) {
|
|---|
| 22 | fprintf(stderr, "Failed to find %s codec\n",
|
|---|
| 23 | av_get_media_type_string(type));
|
|---|
| 24 | return ret;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
|
|---|
| 28 | fprintf(stderr, "Failed to open %s codec\n",
|
|---|
| 29 | av_get_media_type_string(type));
|
|---|
| 30 | return ret;
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | return 0;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | int main(int argc, char **argv)
|
|---|
| 38 | {
|
|---|
| 39 | int ret;
|
|---|
| 40 |
|
|---|
| 41 | if (argc == 1) {
|
|---|
| 42 | printf("no argument provided, exit ************** \n");
|
|---|
| 43 | return 0;
|
|---|
| 44 | }
|
|---|
| 45 | const char *src_filename = argv[1];
|
|---|
| 46 |
|
|---|
| 47 | printf("to play %s\n", src_filename);
|
|---|
| 48 |
|
|---|
| 49 | avcodec_register_all();
|
|---|
| 50 | av_register_all();
|
|---|
| 51 |
|
|---|
| 52 | AVFormatContext *pFormatCtx = NULL;
|
|---|
| 53 | if (avformat_open_input(&pFormatCtx, src_filename, NULL, NULL) < 0) {
|
|---|
| 54 | fprintf(stderr, "Could not open source file %s\n", src_filename);
|
|---|
| 55 | exit(1);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
|
|---|
| 59 | fprintf(stderr, "Could not find stream information\n");
|
|---|
| 60 | exit(1);
|
|---|
| 61 | }
|
|---|
| 62 | printf("rhbc73 pFormatCtx->start_time = %I64d\n", pFormatCtx->start_time);
|
|---|
| 63 |
|
|---|
| 64 | int video_stream_idx = -1, audio_stream_idx = -1;
|
|---|
| 65 | AVStream *video_stream;
|
|---|
| 66 | AVCodecContext *pCodecCtx = NULL;
|
|---|
| 67 | if (open_codec_context(&video_stream_idx, pFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) {
|
|---|
| 68 | video_stream = pFormatCtx->streams[video_stream_idx];
|
|---|
| 69 | pCodecCtx = video_stream->codec;
|
|---|
| 70 | printf("video stream index = %d\n", video_stream_idx);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
|
|---|
| 74 | avcodec_open2(pCodecCtx, pCodec, NULL);
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | AVFrame *frame = avcodec_alloc_frame();
|
|---|
| 78 | AVPacket pkt;
|
|---|
| 79 |
|
|---|
| 80 | av_init_packet(&pkt);
|
|---|
| 81 | pkt.data = NULL;
|
|---|
| 82 | pkt.size = 0;
|
|---|
| 83 | int frameFinished = 0;
|
|---|
| 84 | int count = 0;
|
|---|
| 85 | while (av_read_frame(pFormatCtx, &pkt) >= 0) {
|
|---|
| 86 | if (pkt.stream_index == video_stream_idx) {
|
|---|
| 87 | printf("rhbc73 Packet [%d] dts = %I64d, pts = %I64d, KEY =%d\n", count++, pkt.dts, pkt.pts, (pkt.flags & AV_PKT_FLAG_KEY) != 0);
|
|---|
| 88 | //if ((pkt.flags & AV_PKT_FLAG_KEY) != 0) printf(" ==== got a key packet dts = %I64d\n", pkt.dts);
|
|---|
| 89 | int ret = avcodec_decode_video2(pCodecCtx, frame, &frameFinished, &pkt);
|
|---|
| 90 | if (ret >=0) {
|
|---|
| 91 | 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));
|
|---|
| 92 | else printf("rhbc73 **************** NO complete frame\n");
|
|---|
| 93 | } else printf ("rhbc73 **************** decode failed\n");
|
|---|
| 94 |
|
|---|
| 95 | if (pkt.dts == 10) {
|
|---|
| 96 | printf("rhbc73 now try to see to packet where dts = 3\n");
|
|---|
| 97 | int ret = av_seek_frame(pFormatCtx, video_stream_idx, 3, AVSEEK_FLAG_BACKWARD);
|
|---|
| 98 | if (ret >= 0) {
|
|---|
| 99 | printf("rhbc73 seek succeed, then continue to read\n");
|
|---|
| 100 | av_init_packet(&pkt);
|
|---|
| 101 | while (av_read_frame(pFormatCtx, &pkt) >= 0) {
|
|---|
| 102 | if (pkt.stream_index == video_stream_idx) {
|
|---|
| 103 | printf("rhbc73 Packet [%d] dts = %I64d, pts = %I64d, KEY =%d\n", count++, pkt.dts, pkt.pts, (pkt.flags & AV_PKT_FLAG_KEY) != 0);
|
|---|
| 104 | int ret = avcodec_decode_video2(pCodecCtx, frame, &frameFinished, &pkt);
|
|---|
| 105 | if (ret >=0) {
|
|---|
| 106 | 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));
|
|---|
| 107 | else printf("rhbc73 **************** NO complete frame\n");
|
|---|
| 108 | } else printf ("rhbc73 **************** decode failed\n");
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | return 1;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | return 1;
|
|---|
| 118 | }
|
|---|