From bd8cab84e243ab0e2a50bd139e5d9ee456376a63 Mon Sep 17 00:00:00 2001 From: "jiyong.min" Date: Fri, 25 Oct 2019 10:03:13 +0900 Subject: [PATCH] Code Cleanup for 'break' and add function to count the number of streams Change-Id: I48131561ecded342993344d7198915ed9b935a75 --- formats/ffmpeg/mm_file_format_ffmpeg.c | 156 +++++++++++++++++---------------- 1 file changed, 81 insertions(+), 75 deletions(-) mode change 100755 => 100644 formats/ffmpeg/mm_file_format_ffmpeg.c diff --git a/formats/ffmpeg/mm_file_format_ffmpeg.c b/formats/ffmpeg/mm_file_format_ffmpeg.c old mode 100755 new mode 100644 index abdc455..cd6d40b --- a/formats/ffmpeg/mm_file_format_ffmpeg.c +++ b/formats/ffmpeg/mm_file_format_ffmpeg.c @@ -67,6 +67,31 @@ typedef struct { int state; } MMFmemIOHandle; +static void __count_stream_num(AVFormatContext *pFormatCtx, MMFileFormatContext *formatContext) +{ + unsigned int i = 0; + + formatContext->videoTotalTrackNum = 0; + formatContext->audioTotalTrackNum = 0; + + for (i = 0; i < pFormatCtx->nb_streams; i++) { + if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + debug_msg(RELEASE, "FFMPEG video codec id: 0x%08X\n", pFormatCtx->streams[i]->codecpar->codec_id); + + AVPacket pkt = pFormatCtx->streams[i]->attached_pic; + if ((pkt.data != NULL) && (pkt.size > 0)) + continue; + + /*eventhough codec_id is 0, avformat_find_stream_info() can find proper codec_id. */ + formatContext->videoTotalTrackNum += 1; + } + if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + debug_msg(RELEASE, "FFMPEG audio codec id: 0x%08X\n", pFormatCtx->streams[i]->codecpar->codec_id); + formatContext->audioTotalTrackNum += 1; + } + } +} + static int _mmf_mem_open(MMFmemIOHandle **handle, const char *filename) { MMFmemIOHandle *memHandle = NULL; @@ -205,7 +230,6 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) AVFormatContext *pFormatCtx = NULL; AVInputFormat *grab_iformat = NULL; int ret = 0; - unsigned int i; char ffmpegFormatName[MMFILE_FILE_FMT_MAX_LEN] = {0, }; char mimeType[MMFILE_MIMETYPE_MAX_LEN] = {0, }; AVIOContext *pIOCtx = NULL; @@ -245,7 +269,6 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) memset(ffmpegFormatName, 0x00, MMFILE_FILE_FMT_MAX_LEN); ret = mmfile_util_get_ffmpeg_format(mimeType, ffmpegFormatName); - if (MMFILE_UTIL_SUCCESS != ret) { debug_error(DEBUG, "error: mmfile_util_get_ffmpeg_format\n"); return MMFILE_FORMAT_FAIL; @@ -311,25 +334,7 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) debug_msg(RELEASE, "number of stream: %d\n", pFormatCtx->nb_streams); - formatContext->videoTotalTrackNum = 0; - formatContext->audioTotalTrackNum = 0; - - for (i = 0; i < pFormatCtx->nb_streams; i++) { - if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - debug_msg(RELEASE, "FFMPEG video codec id: 0x%08X\n", pFormatCtx->streams[i]->codecpar->codec_id); - - AVPacket pkt = pFormatCtx->streams[i]->attached_pic; - if ((pkt.data != NULL) && (pkt.size > 0)) - continue; - - /*eventhough codec_id is 0, avformat_find_stream_info() can find proper codec_id. */ - formatContext->videoTotalTrackNum += 1; - } - if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { - debug_msg(RELEASE, "FFMPEG audio codec id: 0x%08X\n", pFormatCtx->streams[i]->codecpar->codec_id); - formatContext->audioTotalTrackNum += 1; - } - } + __count_stream_num(pFormatCtx, formatContext); debug_msg(RELEASE, "format: %s (%s)\n", pFormatCtx->iformat->name, pFormatCtx->iformat->long_name); #ifdef __MMFILE_TEST_MODE__ @@ -1143,79 +1148,80 @@ static int _get_first_good_video_frame(AVFormatContext *pFormatCtx, AVCodecConte if (ret < 0) { debug_error(DEBUG, "read failed. (maybe EOF or broken)\n"); break; - } else { - if (avcodec_get_type(pFormatCtx->streams[pkt.stream_index]->codecpar->codec_id) == AVMEDIA_TYPE_VIDEO) { - v++; - if ((pkt.flags & AV_PKT_FLAG_KEY) || (key_detected == 1)) { - debug_msg(RELEASE, "video frame: %d, %d, %d\n", retry, i, v); + } + + if (avcodec_get_type(pFormatCtx->streams[pkt.stream_index]->codecpar->codec_id) == AVMEDIA_TYPE_VIDEO) { + v++; + if ((pkt.flags & AV_PKT_FLAG_KEY) || (key_detected == 1)) { + debug_msg(RELEASE, "video frame: %d, %d, %d\n", retry, i, v); #ifdef __MMFILE_TEST_MODE__ - _dump_av_packet(&pkt); + _dump_av_packet(&pkt); #endif - i++; - key_detected = 0; + i++; + key_detected = 0; - len = avcodec_send_packet(pCodecCtx, &pkt); - if (len < 0 && len != AVERROR(EAGAIN) && len != AVERROR_EOF) { - debug_error(RELEASE, "Error while avcodec_send_packet[%2d]", len); - break; - } + len = avcodec_send_packet(pCodecCtx, &pkt); + if (len < 0 && len != AVERROR(EAGAIN) && len != AVERROR_EOF) { + debug_error(RELEASE, "Error while avcodec_send_packet[%2d]", len); + break; + } - len = avcodec_receive_frame(pCodecCtx, frame); - if (len < 0 && len != AVERROR(EAGAIN) && len != AVERROR_EOF) { - debug_warning(DEBUG, "Error while decoding frame %dth\n", i); - } else if (len >= 0) { - if (frame->key_frame) { - debug_msg(RELEASE, "key frame!\n"); + len = avcodec_receive_frame(pCodecCtx, frame); + if (len < 0 && len != AVERROR(EAGAIN) && len != AVERROR_EOF) { + debug_warning(DEBUG, "Error while decoding frame %dth\n", i); + } else if (len >= 0) { + if (frame->key_frame) { + debug_msg(RELEASE, "key frame!\n"); #ifdef MMFILE_FORMAT_DEBUG_DUMP - memset(pgm_name, 0x00, sizeof(pgm_name)); - snprintf(pgm_name, sizeof(pgm_name), "./key_%d_%d_%d.pgm", retry, i, v); - _save_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name); + memset(pgm_name, 0x00, sizeof(pgm_name)); + snprintf(pgm_name, sizeof(pgm_name), "./key_%d_%d_%d.pgm", retry, i, v); + _save_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name); #endif - found++; + found++; #ifdef __MMFILE_TEST_MODE__ - int64_t ti; - ti = gettime(); + int64_t ti; + ti = gettime(); #endif - ret = _is_good_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height); + ret = _is_good_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height); #ifdef __MMFILE_TEST_MODE__ - ti = gettime() - ti; - debug_msg(RELEASE, "Elapsed time = %lld\n", ti); -#endif - if (ret != 0) { - debug_msg(RELEASE, "is good frame.\n"); - break; - } else { - /*reset video frame count & retry searching*/ - debug_warning(RELEASE, "not good fame. retry scanning.\n"); - i = 0; - v = 0; - retry++; - } - - /*set buffer frame*/ - frame = tmp_frame; - - /*limit of retry.*/ - if (retry > _RETRY_SEARCH_LIMIT) break; - - } else { - debug_msg(RELEASE, "skip (not key frame).\n"); -#ifdef MMFILE_FORMAT_DEBUG_DUMP - memset(pgm_name, 0x00, sizeof(pgm_name)); - snprintf(pgm_name, sizeof(pgm_name), "./not_key_%d_%d_%d.pgm", retry, i, v); - _save_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name); + ti = gettime() - ti; + debug_msg(RELEASE, "Elapsed time = %lld\n", ti); #endif + if (ret != 0) { + debug_msg(RELEASE, "is good frame.\n"); + break; } + + /*reset video frame count & retry searching*/ + debug_warning(RELEASE, "not good fame. retry scanning.\n"); + i = 0; + v = 0; + retry++; + + /*set buffer frame*/ + frame = tmp_frame; + + /*limit of retry.*/ + if (retry > _RETRY_SEARCH_LIMIT) break; + } else { - debug_msg(RELEASE, "decode not completed.\n"); - key_detected = 1; + debug_msg(RELEASE, "skip (not key frame).\n"); +#ifdef MMFILE_FORMAT_DEBUG_DUMP + memset(pgm_name, 0x00, sizeof(pgm_name)); + snprintf(pgm_name, sizeof(pgm_name), "./not_key_%d_%d_%d.pgm", retry, i, v); + _save_pgm(frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name); +#endif } + } else { + debug_msg(RELEASE, "decode not completed.\n"); + key_detected = 1; } } } + av_packet_unref(&pkt); } -- 2.7.4