static int stream_video_release(play_para_t *p_para) { if(p_para->vcodec) { codec_close_cntl(p_para->vcodec); codec_close(p_para->vcodec); codec_free(p_para->vcodec); } p_para->vcodec=NULL; return 0; }
int main(int argc, char * const argv[]) { int ret = CODEC_ERROR_NONE; std::string input_filename; if (argc > 1) { for (int i = 1; i < argc; i++) { if (strncasecmp(argv[i], "--input", 7) == 0) { // check the next arg with the proper value. int next = i + 1; if (next < argc) { input_filename = argv[next]; i++; } } else if (strncasecmp(argv[i], "-h", 2) == 0 || strncasecmp(argv[i], "--help", 6) == 0) { printf("Usage: %s [OPTIONS]...\n", argv[0]); printf("Arguments:\n"); printf(" --input <filename> \tInput video filename\n"); exit(0); } } } if (input_filename.empty()) { printf("no input file specified\n"); exit(0); } // install signal handlers signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); // initialize App contex. AVPacket avpkt; AppContext ctx; memset(&ctx, 0, sizeof(ctx)); player_para_init(&ctx); am_packet_init(&ctx.am_pkt); ctx.am_pkt.avpkt = &avpkt; av_init_packet(ctx.am_pkt.avpkt); // create the ffmepg file reader/demuxer ctx.demuxer = new FFmpegFileReader(input_filename.c_str()); if (!ctx.demuxer->Initialize()) { fprintf(stderr, "ERROR: Can't initialize FFmpegFileReader\n"); goto fail; } ctx.codec_context = ctx.demuxer->GetCodecContext(); ctx.format_context = ctx.demuxer->GetFormatContext(); if (!ctx.codec_context) { fprintf(stderr, "ERROR: Invalid FFmpegFileReader Codec Context\n"); goto fail; } dump_extradata(&ctx); printf("video width(%d), height(%d), extradata_size(%d)\n", (int)ctx.codec_context->width, (int)ctx.codec_context->height, ctx.codec_context->extradata_size); AVStream *pStream; pStream = ctx.format_context->streams[ctx.demuxer->GetVideoIndex()]; //ctx.vstream_info.video_pid = (unsigned short)ctx.codec_context->id; if (pStream->time_base.den) { ctx.vstream_info.start_time = pStream->start_time * pStream->time_base.num * PTS_FREQ / pStream->time_base.den; ctx.vstream_info.video_duration = ((float)pStream->time_base.num / pStream->time_base.den) * UNIT_FREQ; ctx.vstream_info.video_pts = ((float)pStream->time_base.num / pStream->time_base.den) * PTS_FREQ; } ctx.vstream_info.video_width = ctx.codec_context->width; ctx.vstream_info.video_height = ctx.codec_context->height; ctx.vstream_info.video_ratio = (float)pStream->sample_aspect_ratio.num / pStream->sample_aspect_ratio.den; if (ctx.codec_context->time_base.den) { ctx.vstream_info.video_codec_rate = (int64_t)UNIT_FREQ * ctx.codec_context->time_base.num / ctx.codec_context->time_base.den; } if (pStream->r_frame_rate.num) { ctx.vstream_info.video_rate = (int64_t)UNIT_FREQ * pStream->r_frame_rate.den / pStream->r_frame_rate.num; } log_print("time_base.num(%d), pStream->time_base.den(%d)\n", pStream->time_base.num, pStream->time_base.den); ctx.vstream_info.video_duration = ((float)pStream->time_base.num / pStream->time_base.den) * UNIT_FREQ; ctx.vstream_info.video_pts = ((float)pStream->time_base.num / pStream->time_base.den) * PTS_FREQ; printf("\n*********AMLOGIC CODEC PLAYER DEMO************\n\n"); osd_blank("/sys/class/graphics/fb0/blank", 1); osd_blank("/sys/class/graphics/fb1/blank", 1); //osd_blank("/sys/class/tsync/enable", 1); osd_blank("/sys/class/tsync/enable", 0); switch(ctx.codec_context->codec_id) { case CODEC_ID_H264: printf("CODEC_ID_H264\n"); ctx.vcodec.has_video = 1; ctx.vcodec.has_audio = 0; ctx.stream_type = STREAM_ES; //ctx.vcodec.video_pid = ctx.demuxer->GetVideoIndex(); ctx.vcodec.video_type = VFORMAT_H264; ctx.vcodec.stream_type = STREAM_TYPE_ES_VIDEO; //ctx.vcodec.noblock = !!p_para->buffering_enable; ctx.vcodec.am_sysinfo.format = VIDEO_DEC_FORMAT_H264; ctx.vcodec.am_sysinfo.width = ctx.vstream_info.video_width; ctx.vcodec.am_sysinfo.height = ctx.vstream_info.video_height; ctx.vcodec.am_sysinfo.rate = ctx.vstream_info.video_rate; ctx.vcodec.am_sysinfo.ratio = ctx.vstream_info.video_ratio; ctx.vcodec.am_sysinfo.param = (void*)(EXTERNAL_PTS | SYNC_OUTSIDE); break; case CODEC_ID_MPEG4: printf("CODEC_ID_MPEG4\n"); ctx.vcodec.video_type = VFORMAT_MPEG4; break; case CODEC_ID_MPEG2VIDEO: printf("CODEC_ID_MPEG2VIDEO\n"); ctx.vcodec.video_type = VFORMAT_MPEG12; break; default: fprintf(stderr, "ERROR: Invalid FFmpegFileReader Codec Format (not h264/mpeg4) = %d\n", ctx.codec_context->codec_id); goto fail; break; } ret = codec_init(&ctx.vcodec); if(ret != CODEC_ERROR_NONE) { printf("codec init failed, ret=-0x%x", -ret); return -1; } printf("video codec ok!\n"); ret = codec_init_cntl(&ctx.vcodec); if( ret != CODEC_ERROR_NONE ) { printf("codec_init_cntl error\n"); return -1; } codec_set_cntl_avthresh(&ctx.vcodec, AV_SYNC_THRESH); codec_set_cntl_syncthresh(&ctx.vcodec, ctx.vcodec.has_audio); { int frame_count, total = 0; int64_t bgn_us, end_us; ctx.am_pkt.codec = &ctx.vcodec; pre_header_feeding(&ctx, &ctx.am_pkt); ctx.demuxer->Read(ctx.am_pkt.avpkt); printf("byte_count(%d), dts(%llu), pts(%llu)\n", ctx.am_pkt.avpkt->size, ctx.am_pkt.avpkt->dts, ctx.am_pkt.avpkt->pts); if (!ctx.am_pkt.avpkt->size) { fprintf(stderr, "ERROR: Zero bytes read from input\n"); //goto fail; } ctx.am_pkt.type = CODEC_VIDEO; ctx.am_pkt.data = ctx.am_pkt.avpkt->data; ctx.am_pkt.data_size = ctx.am_pkt.avpkt->size; ctx.am_pkt.avpkt_newflag = 1; ctx.am_pkt.avpkt_isvalid = 1; frame_count = 0; bool done = false; struct buf_status vbuf; struct vdec_status vdec; float vlevel; while (!g_signal_abort && !done && (frame_count < 5000)) { h264_update_frame_header(&ctx.am_pkt); bgn_us = CurrentHostCounter() * 1000 / CurrentHostFrequency(); log_print("avpts(%lld), avdts(%lld)\n", ctx.am_pkt.avpkt->pts, ctx.am_pkt.avpkt->dts); ret = write_av_packet(&ctx, &ctx.am_pkt); end_us = CurrentHostCounter() * 1000 / CurrentHostFrequency(); frame_count++; //fprintf(stdout, "decode time(%llu) us\n", end_us-bgn_us); check_vcodec_state(&ctx.vcodec, &vdec, &vbuf); vlevel = 100.0 * (float)vbuf.data_len / vbuf.size; //log_print("buffering_states,vlevel=%d,vsize=%d,level=%f\n", vbuf.data_len, vbuf.size, vlevel); usleep(vlevel * 5000); ctx.demuxer->Read(ctx.am_pkt.avpkt); ctx.am_pkt.type = CODEC_VIDEO; ctx.am_pkt.data = ctx.am_pkt.avpkt->data; ctx.am_pkt.data_size = ctx.am_pkt.avpkt->size; ctx.am_pkt.avpkt_newflag = 1; ctx.am_pkt.avpkt_isvalid = 1; if (!ctx.am_pkt.data_size) done = true; total += ctx.am_pkt.data_size; } } fail: codec_close_cntl(&ctx.vcodec); codec_close(&ctx.vcodec); return 0; }