static const char *src_filename = NULL;
static AVFormatContext *pFormatCtx = NULL;
static int video_stream_idx = -1, audio_stream_idx = -1;
static AVStream *video_stream;
static AVCodecContext *video_dec_ctx = NULL;
static AVCodec* pCodec = NULL;
static AVFrame *frame = NULL;
static AVPacket pkt;
static int video_frame_count = 0;
static int audio_frame_count = 0;

static int open_codec_context(int *stream_idx,
                              AVFormatContext *fmt_ctx, enum AVMediaType type)
{
    int ret;
    AVStream *st;
    AVCodecContext *dec_ctx = NULL;
    AVCodec *dec = NULL;

    ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
    if (ret < 0) {
        //fprintf(stderr, "Could not find %s stream in input file '%s'\n",
        //        av_get_media_type_string(type), src_filename);
        return ret;
    } else {
        *stream_idx = ret;
        st = fmt_ctx->streams[*stream_idx];

        dec_ctx = st->codec;
        dec = avcodec_find_decoder(dec_ctx->codec_id);
        if (!dec) {
            fprintf(stderr, "Failed to find %s codec\n",
                    av_get_media_type_string(type));
            return ret;
        }

        if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
            fprintf(stderr, "Failed to open %s codec\n",
                    av_get_media_type_string(type));
            return ret;
        }
    }

    return 0;
}

static int decode_packet(int *got_frame, int cached)
{
    int ret = 0;

    if (pkt.stream_index == video_stream_idx) {
        // decode video frame 
        ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error decoding video frame\n");
            return ret;
        }

        if (*got_frame) {
            printf("video_frame%s n:%d coded_n:%d Packet.dts = %I7d, pFrame->pkt_dts = %I7d, best timestamp = %I7d\n", 
                      cached ? "(cached)" : "", video_frame_count++, frame->coded_picture_number,
                      pkt.dts, frame->pkt_dts, av_frame_get_best_effort_timestamp(frame)
                   );
        }
    } 
    return ret;
}

int main(int argc, char **argv)
{
    int ret = 0, got_frame;

    if (argc == 1) {
        printf("no argument provided, exit ************** \n");
        return 0;
    }
    src_filename = argv[1];

    printf("to play %s\n", src_filename);
    
    avcodec_register_all();
    av_register_all();

    if (avformat_open_input(&pFormatCtx, src_filename, NULL, NULL) < 0) {
        fprintf(stderr, "Could not open source file %s\n", src_filename);
        exit(1);
    }

    if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
        fprintf(stderr, "Could not find stream information\n");
        exit(1);
    }
    printf("rhbc73 pFormatCtx->start_time = %I64d\n", pFormatCtx->start_time);

    if (open_codec_context(&video_stream_idx, pFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) {
        video_stream = pFormatCtx->streams[video_stream_idx];
        video_dec_ctx = video_stream->codec;
        printf("video stream index = %d\n", video_stream_idx);
    }

    frame = avcodec_alloc_frame();
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
    int frameFinished = 0;
    int count = 0;

    // read frames from the file
    while (av_read_frame(pFormatCtx, &pkt) >= 0) {
        printf("packet %d\n", count++);
    }

    ret = av_seek_frame(pFormatCtx, video_stream_idx, 0, AVSEEK_FLAG_BACKWARD);
    if (ret < 0) { 
        printf("seek to 0 failed\n"); 
        return 0; 
    } else { printf("seek ok\n"); }

    av_init_packet(&pkt);
    ret = av_read_frame(pFormatCtx, &pkt);
    if (ret < 0) { 
        printf("read after seek failed *********************\n"); 
    } else {
	    printf("read ok after seek(0)\n");
	}

    return 1;
}