From 65b370034c6d09637c80e951d343a95e1a3b74bf Mon Sep 17 00:00:00 2001 From: Haejeong Kim Date: Fri, 28 Jul 2017 15:42:40 +0900 Subject: [PATCH] Remove deprecated ffurl related code. Use own io_context instead of it Change-Id: I26a8c473fb9d4e9aa29aec928cf248299c4c5bfd --- formats/ffmpeg/Makefile.am | 2 - formats/ffmpeg/include/mm_file_format_ffmpeg_mem.h | 38 ---- formats/ffmpeg/mm_file_format_ffmpeg.c | 200 ++++++++++++++++-- formats/ffmpeg/mm_file_format_ffmpeg_mem.c | 232 --------------------- formats/ffmpeg/mm_file_format_frame.c | 202 ++++++++++++++++-- packaging/libmm-fileinfo.spec | 2 +- utils/include/mm_file_utils.h | 1 + 7 files changed, 378 insertions(+), 299 deletions(-) delete mode 100755 formats/ffmpeg/include/mm_file_format_ffmpeg_mem.h delete mode 100755 formats/ffmpeg/mm_file_format_ffmpeg_mem.c diff --git a/formats/ffmpeg/Makefile.am b/formats/ffmpeg/Makefile.am index 1a1e3c3..763390d 100755 --- a/formats/ffmpeg/Makefile.am +++ b/formats/ffmpeg/Makefile.am @@ -8,7 +8,6 @@ noinst_HEADERS = include/mm_file_format_dummy.h \ include/mm_file_format_amr.h \ include/mm_file_format_imelody.h \ include/mm_file_format_midi.h \ - include/mm_file_format_ffmpeg_mem.h \ include/mm_file_format_ffmpeg.h \ include/mm_file_format_mmf.h \ #include/mm_file_format_mp3.h \ @@ -18,7 +17,6 @@ noinst_HEADERS = include/mm_file_format_dummy.h \ libmmfile_formats_la_SOURCES = mm_file_formats.c \ mm_file_format_dummy.c \ mm_file_format_ffmpeg.c \ - mm_file_format_ffmpeg_mem.c \ mm_file_format_mp3.c \ mm_file_format_aac.c \ mm_file_format_mmf.c \ diff --git a/formats/ffmpeg/include/mm_file_format_ffmpeg_mem.h b/formats/ffmpeg/include/mm_file_format_ffmpeg_mem.h deleted file mode 100755 index 48a56be..0000000 --- a/formats/ffmpeg/include/mm_file_format_ffmpeg_mem.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * libmm-fileinfo - * - * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. - * - * Contact: Haejeong Kim - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef _MM_FILE_PLUGIN_FFMPEG_MEM_H_ -#define _MM_FILE_PLUGIN_FFMPEG_MEM_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -extern URLProtocol MMFileMEMProtocol; - -#ifdef __cplusplus -} -#endif - -#endif /* _MM_FILE_PLUGIN_FFMPEG_H_ */ diff --git a/formats/ffmpeg/mm_file_format_ffmpeg.c b/formats/ffmpeg/mm_file_format_ffmpeg.c index db24153..ca287c9 100755 --- a/formats/ffmpeg/mm_file_format_ffmpeg.c +++ b/formats/ffmpeg/mm_file_format_ffmpeg.c @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -32,7 +31,6 @@ #include "mm_file_formats.h" #include "mm_file_utils.h" #include "mm_file_format_ffmpeg.h" -#include "mm_file_format_ffmpeg_mem.h" #include #define _SHORT_MEDIA_LIMIT 2000 /* under X seconds duration*/ @@ -59,7 +57,147 @@ int mmfile_format_read_tag_ffmpg(MMFileFormatContext *formatContext); int mmfile_format_close_ffmpg(MMFileFormatContext *formatContext); static int getMimeType(int formatId, char *mimeType, int buf_size); +static int _mmf_mem_read(void *opaque, uint8_t *buf, int size); +static int64_t _mmf_mem_seek(void *opaque, int64_t pos, int whence); +typedef struct { + unsigned char *ptr; + long long size; + long long offset; + int state; +} MMFmemIOHandle; + +static int _mmf_mem_open(MMFmemIOHandle **handle, const char *filename) +{ + MMFmemIOHandle *memHandle = NULL; + char **splitedString = NULL; + + filename += strlen(MMFILE_MEM_URI); + + splitedString = mmfile_strsplit(filename, ":"); + if (splitedString == NULL) { + debug_error(DEBUG, "invalid param\n"); + return MMFILE_IO_FAILED; + } + + if (!splitedString[0] || !splitedString[1]) { + debug_error(DEBUG, "invalid param\n"); + goto exception; + } + + memHandle = mmfile_malloc(sizeof(MMFmemIOHandle)); + if (!memHandle) { + debug_error(DEBUG, "error: mmfile_malloc memHandle\n"); + goto exception; + } + + memHandle->ptr = (unsigned char *)atoll(splitedString[0]); /*memory allocation address changed. memHandle->ptr = (unsigned char*)atoi(splitedString[0]); */ + memHandle->size = atoi(splitedString[1]); + memHandle->offset = 0; + memHandle->state = 0; + + *handle = memHandle; + + if (splitedString) { + mmfile_strfreev(splitedString); + } + + return MMFILE_IO_SUCCESS; + +exception: + + if (splitedString) { + mmfile_strfreev(splitedString); + } + + return MMFILE_IO_FAILED; +} + +static int _mmf_mem_read(void *opaque, uint8_t *buf, int size) +{ + MMFmemIOHandle *memHandle = NULL; + const unsigned char *c = NULL; + int len = 0; + + if (!opaque || !buf) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + memHandle = opaque; + + if (!memHandle->ptr) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + if (memHandle->offset >= memHandle->size) { + /* for some file formats last file read */ + debug_error(DEBUG, "File Read is beyond the file Size\n"); + return MMFILE_UTIL_FAIL; + + } + + c = memHandle->ptr + memHandle->offset; + + if (memHandle->state != EOF) { + len = size; + if (len + memHandle->offset > memHandle->size) { + len = memHandle->size - memHandle->offset; + } + } + + memcpy(buf, c, len); + + memHandle->offset += len; + + if (memHandle->offset == memHandle->size) { + memHandle->state = EOF; + } + + return len; +} + +static int64_t _mmf_mem_seek(void *opaque, int64_t pos, int whence) +{ + MMFmemIOHandle *memHandle = NULL; + long long tmp_offset = 0; + + if (!opaque) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + memHandle = opaque; + + switch (whence) { + case SEEK_SET: + tmp_offset = 0 + pos; + break; + case SEEK_CUR: + tmp_offset = memHandle->offset + pos; + break; + case SEEK_END: + tmp_offset = memHandle->size + pos; + break; + case AVSEEK_SIZE: /*FFMPEG specific*/ + return memHandle->size; + default: + return MMFILE_UTIL_FAIL; + } + + /*check validation*/ + if (tmp_offset < 0 && tmp_offset > memHandle->size) { + debug_error(DEBUG, "invalid file offset\n"); + return MMFILE_UTIL_FAIL; + } + + /*set */ + memHandle->state = (tmp_offset >= memHandle->size) ? EOF : !EOF; + memHandle->offset = (unsigned int) tmp_offset; + + return tmp_offset; +} EXPORT_API int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) @@ -70,6 +208,9 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) unsigned int i; char ffmpegFormatName[MMFILE_FILE_FMT_MAX_LEN] = {0, }; char mimeType[MMFILE_MIMETYPE_MAX_LEN] = {0, }; + AVIOContext *pIOCtx = NULL; + MMFmemIOHandle *handle = NULL; + uint8_t *avio_ctx_buffer = NULL; formatContext->ReadStream = mmfile_format_read_stream_ffmpg; formatContext->ReadFrame = mmfile_format_read_frame_ffmpg; @@ -96,9 +237,6 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) av_register_all(); if (formatContext->filesrc->type == MM_FILE_SRC_TYPE_MEMORY) { - - ffurl_register_protocol(&MMFileMEMProtocol); - if (getMimeType(formatContext->filesrc->memory.format, mimeType, MMFILE_MIMETYPE_MAX_LEN) < 0) { debug_error(DEBUG, "error: Error in MIME Type finding\n"); return MMFILE_FORMAT_FAIL; @@ -120,11 +258,38 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) goto exception; } - ret = avformat_open_input(&pFormatCtx, formatContext->uriFileName, grab_iformat, NULL); + avio_ctx_buffer = av_malloc(MMFILE_AVIO_BUFFER_LEN); + if (avio_ctx_buffer == NULL) { + debug_error(DEBUG, "error: cannot alloc avio_ctx_buffert\n"); + goto exception; + } + + ret = _mmf_mem_open(&handle, formatContext->uriFileName); + if (ret != MMFILE_IO_SUCCESS) { + debug_warning(DEBUG, "failed to _mmf_mem_open.\n"); + goto exception; + } + + pIOCtx = avio_alloc_context(avio_ctx_buffer, MMFILE_AVIO_BUFFER_LEN, 0, handle, _mmf_mem_read, NULL, _mmf_mem_seek); + if (pIOCtx == NULL) { + debug_error(DEBUG, "error: cannot alloc io context\n"); + goto exception; + } + + pFormatCtx = avformat_alloc_context(); + if (pFormatCtx == NULL) { + debug_warning(DEBUG, "failed to avformat_alloc_context\n"); + goto exception; + } + + pFormatCtx->pb = pIOCtx; + + ret = avformat_open_input(&pFormatCtx, "", grab_iformat, NULL); if (ret < 0) { debug_error(DEBUG, "error: cannot open %s %d\n", formatContext->uriFileName, ret); goto exception; } + formatContext->privateFormatData = pFormatCtx; } @@ -174,8 +339,19 @@ int mmfile_format_open_ffmpg(MMFileFormatContext *formatContext) return MMFILE_FORMAT_SUCCESS; exception: /* fail to get content information */ + if (formatContext->filesrc->type == MM_FILE_SRC_TYPE_MEMORY) { + if (avio_ctx_buffer) + av_free(avio_ctx_buffer); + if (handle) + mmfile_free(handle); + if (pIOCtx) + av_free(pIOCtx); + if (pFormatCtx) + avformat_close_input(&pFormatCtx); + } else { + mmfile_format_close_ffmpg(formatContext); + } - mmfile_format_close_ffmpg(formatContext); formatContext->privateFormatData = NULL; return MMFILE_FORMAT_FAIL; @@ -720,10 +896,6 @@ int mmfile_format_close_ffmpg(MMFileFormatContext *formatContext) avformat_close_input(&pFormatCtx); formatContext->privateFormatData = NULL; } - - if (formatContext->filesrc->type == MM_FILE_SRC_TYPE_MEMORY) { - ffurl_deregister_protocol(&MMFileMEMProtocol); - } } return MMFILE_FORMAT_SUCCESS; @@ -953,7 +1125,8 @@ static int _get_first_good_video_frame(AVFormatContext *pFormatCtx, AVCodecConte if (frame->key_frame) { debug_msg(RELEASE, "key frame!\n"); #ifdef MMFILE_FORMAT_DEBUG_DUMP - sprintf(pgm_name, "./key_%d_%d_%d.pgm", retry, i, v); + 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 @@ -988,7 +1161,8 @@ static int _get_first_good_video_frame(AVFormatContext *pFormatCtx, AVCodecConte } else { debug_msg(RELEASE, "skip (not key frame).\n"); #ifdef MMFILE_FORMAT_DEBUG_DUMP - sprintf(pgm_name, "./not_key_%d_%d_%d.pgm", retry, i, v); + 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 } diff --git a/formats/ffmpeg/mm_file_format_ffmpeg_mem.c b/formats/ffmpeg/mm_file_format_ffmpeg_mem.c deleted file mode 100755 index c19b095..0000000 --- a/formats/ffmpeg/mm_file_format_ffmpeg_mem.c +++ /dev/null @@ -1,232 +0,0 @@ -/* - * libmm-fileinfo - * - * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. - * - * Contact: Haejeong Kim - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include - -#include -#include -#include "mm_file_debug.h" -#include "mm_file_utils.h" -#include "mm_file_format_ffmpeg_mem.h" - - -typedef struct { - unsigned char *ptr; - long long size; - long long offset; - int state; -} MMFmemIOHandle; - -static int mmf_mem_open(URLContext *handle, const char *filename, int flags) -{ - MMFmemIOHandle *memHandle = NULL; - char **splitedString = NULL; - - - if (!handle || !filename || !handle->prot) { - debug_error(DEBUG, "invalid param\n"); - return MMFILE_UTIL_FAIL; - } - - filename += strlen(handle->prot->name) + 3; /* ://%d:%d means (memory addr:mem size)*/ - - splitedString = mmfile_strsplit(filename, ":"); - if (splitedString == NULL) { - debug_error(DEBUG, "invalid param\n"); - return MMFILE_UTIL_FAIL; - } - - if (!splitedString[0] || !splitedString[1]) - - { - debug_error(DEBUG, "invalid param\n"); - goto exception; - } - - memHandle = mmfile_malloc(sizeof(MMFmemIOHandle)); - if (!memHandle) { - debug_error(DEBUG, "error: mmfile_malloc memHandle\n"); - goto exception; - } - - memHandle->ptr = (unsigned char *)atoll(splitedString[0]); /*memory allocation address changed. memHandle->ptr = (unsigned char *) atoi(splitedString[0]); */ - memHandle->size = atoi(splitedString[1]); - memHandle->offset = 0; - memHandle->state = 0; - - handle->priv_data = (void *) memHandle; - - /* Imp to reset them otherwise file seek will fail */ - handle->is_streamed = 0; /*FALSE*/ - handle->max_packet_size = 0; - - if (splitedString) { - mmfile_strfreev(splitedString); - } - - return MMFILE_UTIL_SUCCESS; - -exception: - - if (splitedString) { - mmfile_strfreev(splitedString); - } - -#if 0 /*dead code */ - if (memHandle) { - mmfile_free(memHandle); - handle->priv_data = NULL; - } -#endif - - return MMFILE_UTIL_FAIL; -} - -static int mmf_mem_read(URLContext *h, unsigned char *buf, int size) -{ - MMFmemIOHandle *memHandle = NULL; - const unsigned char *c = NULL; - int len = 0; - - - if (!h || !h->priv_data || !buf || size <= 0) { - debug_error(DEBUG, "invalid para\n"); - return MMFILE_UTIL_FAIL; - } - - memHandle = h->priv_data; - - if (!memHandle->ptr) { - debug_error(DEBUG, "invalid para\n"); - return MMFILE_UTIL_FAIL; - } - - if (memHandle->offset >= memHandle->size) { - /* for some file formats last file read */ - debug_error(DEBUG, "File Read is beyond the file Size\n"); - return MMFILE_UTIL_FAIL; - - } - - c = memHandle->ptr + memHandle->offset; - - if (memHandle->state != EOF) { - len = size; - if (len + memHandle->offset > memHandle->size) { - len = memHandle->size - memHandle->offset; - } - } - - memcpy(buf, c, len); - - memHandle->offset += len; - - if (memHandle->offset == memHandle->size) { - memHandle->state = EOF; - } - - return len; -} - -static int mmf_mem_write(URLContext *h, const unsigned char *buf, int size) -{ - if (!h || !h->priv_data || !buf) { - debug_error(DEBUG, "invalid para\n"); - return MMFILE_UTIL_FAIL; - } - - debug_error(DEBUG, "NOTE PERMITTED\n"); - return MMFILE_UTIL_FAIL; -} - - -static int64_t mmf_mem_seek(URLContext *h, int64_t pos, int whence) -{ - MMFmemIOHandle *memHandle = NULL; - long long tmp_offset = 0; - - - if (!h || !h->priv_data) { - debug_error(DEBUG, "invalid para\n"); - return MMFILE_UTIL_FAIL; - } - - memHandle = h->priv_data; - - switch (whence) { - case SEEK_SET: - tmp_offset = 0 + pos; - break; - case SEEK_CUR: - tmp_offset = memHandle->offset + pos; - break; - case SEEK_END: - tmp_offset = memHandle->size + pos; - break; - case AVSEEK_SIZE: /*FFMPEG specific*/ - return memHandle->size; - default: - return MMFILE_UTIL_FAIL; - } - - /*check validation*/ - if (tmp_offset < 0 && tmp_offset > memHandle->size) { - debug_error(DEBUG, "invalid file offset\n"); - return MMFILE_UTIL_FAIL; - } - - /*set */ - memHandle->state = (tmp_offset >= memHandle->size) ? EOF : !EOF; - memHandle->offset = (unsigned int) tmp_offset; - - return tmp_offset; -} - -static int mmf_mem_close(URLContext *h) -{ - MMFmemIOHandle *memHandle = NULL; - - if (!h || !h->priv_data) { - debug_error(DEBUG, "invalid para\n"); - return MMFILE_UTIL_FAIL; - } - - memHandle = h->priv_data; - - if (memHandle) { - mmfile_free(memHandle); - h->priv_data = NULL; - } - - return MMFILE_UTIL_SUCCESS; -} - - -URLProtocol MMFileMEMProtocol = { - .name = "mem", - .url_open = mmf_mem_open, - .url_read = mmf_mem_read, - .url_write = mmf_mem_write, - .url_seek = mmf_mem_seek, - .url_close = mmf_mem_close, -}; diff --git a/formats/ffmpeg/mm_file_format_frame.c b/formats/ffmpeg/mm_file_format_frame.c index 071e8c0..90ad04a 100755 --- a/formats/ffmpeg/mm_file_format_frame.c +++ b/formats/ffmpeg/mm_file_format_frame.c @@ -27,7 +27,6 @@ #include "mm_file_debug.h" #include "mm_file_formats.h" #include "mm_file_utils.h" -#include "mm_file_format_ffmpeg_mem.h" #include "mm_file_format_frame.h" #define MILLION 1000000 @@ -37,10 +36,12 @@ static void __save_frame(AVFrame *pFrame, int width, int height, int iFrame); void __save_frame(AVFrame *pFrame, int width, int height, int iFrame) { FILE *pFile; - char szFilename[32]; - int y; + char szFilename[32] = {0,}; + int y = 0; /* Open file */ - sprintf(szFilename, "frame%d.ppm", iFrame); + + memset(szFilename, 0x00, sizeof(szFilename)); + snprintf(szFilename, sizeof(szFilename), "frame%d.ppm", iFrame); pFile = fopen(szFilename, "wb"); if (pFile == NULL) return; @@ -56,6 +57,148 @@ void __save_frame(AVFrame *pFrame, int width, int height, int iFrame) } #endif +static int _mmf_mem_read(void *opaque, uint8_t *buf, int size); +static int64_t _mmf_mem_seek(void *opaque, int64_t pos, int whence); + +typedef struct { + unsigned char *ptr; + long long size; + long long offset; + int state; +} MMFmemIOHandle; + +static int _mmf_mem_open(MMFmemIOHandle **handle, const char *filename) +{ + MMFmemIOHandle *memHandle = NULL; + char **splitedString = NULL; + + filename += strlen(MMFILE_MEM_URI); + + splitedString = mmfile_strsplit(filename, ":"); + if (splitedString == NULL) { + debug_error(DEBUG, "invalid param\n"); + return MMFILE_IO_FAILED; + } + + if (!splitedString[0] || !splitedString[1]) { + debug_error(DEBUG, "invalid param\n"); + goto exception; + } + + memHandle = mmfile_malloc(sizeof(MMFmemIOHandle)); + if (!memHandle) { + debug_error(DEBUG, "error: mmfile_malloc memHandle\n"); + goto exception; + } + + memHandle->ptr = (unsigned char *)atoll(splitedString[0]); /*memory allocation address changed. memHandle->ptr = (unsigned char*)atoi(splitedString[0]); */ + memHandle->size = atoi(splitedString[1]); + memHandle->offset = 0; + memHandle->state = 0; + + *handle = memHandle; + + if (splitedString) { + mmfile_strfreev(splitedString); + } + + return MMFILE_IO_SUCCESS; + +exception: + + if (splitedString) { + mmfile_strfreev(splitedString); + } + + return MMFILE_IO_FAILED; +} + +static int _mmf_mem_read(void *opaque, uint8_t *buf, int size) +{ + MMFmemIOHandle *memHandle = NULL; + const unsigned char *c = NULL; + int len = 0; + + if (!opaque || !buf) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + memHandle = opaque; + + if (!memHandle->ptr) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + if (memHandle->offset >= memHandle->size) { + /* for some file formats last file read */ + debug_error(DEBUG, "File Read is beyond the file Size\n"); + return MMFILE_UTIL_FAIL; + + } + + c = memHandle->ptr + memHandle->offset; + + if (memHandle->state != EOF) { + len = size; + if (len + memHandle->offset > memHandle->size) { + len = memHandle->size - memHandle->offset; + } + } + + memcpy(buf, c, len); + + memHandle->offset += len; + + if (memHandle->offset == memHandle->size) { + memHandle->state = EOF; + } + + return len; +} + +static int64_t _mmf_mem_seek(void *opaque, int64_t pos, int whence) +{ + MMFmemIOHandle *memHandle = NULL; + long long tmp_offset = 0; + + if (!opaque) { + debug_error(DEBUG, "invalid para\n"); + return MMFILE_UTIL_FAIL; + } + + memHandle = opaque; + + switch (whence) { + case SEEK_SET: + tmp_offset = 0 + pos; + break; + case SEEK_CUR: + tmp_offset = memHandle->offset + pos; + break; + case SEEK_END: + tmp_offset = memHandle->size + pos; + break; + case AVSEEK_SIZE: /*FFMPEG specific*/ + return memHandle->size; + default: + return MMFILE_UTIL_FAIL; + } + + /*check validation*/ + if (tmp_offset < 0 && tmp_offset > memHandle->size) { + debug_error(DEBUG, "invalid file offset\n"); + return MMFILE_UTIL_FAIL; + } + + /*set */ + memHandle->state = (tmp_offset >= memHandle->size) ? EOF : !EOF; + memHandle->offset = (unsigned int) tmp_offset; + + return tmp_offset; +} + static int __getMimeType(int formatId, char *mimeType, int buf_size) { int ret = 0; /*default: success*/ @@ -231,7 +374,6 @@ FILE_FORMAT_SUCCESS: static int __mmfile_get_frame(AVFormatContext *pFormatCtx, int64_t timestamp, bool is_accurate, unsigned char **frame, int *size, int *width, int *height) { - unsigned int i = 0; int len = 0; int ret = MMFILE_FORMAT_SUCCESS; @@ -542,6 +684,9 @@ int mmfile_format_get_frame_from_memory(const void *data, unsigned int datasize, char *urifilename = NULL; AVFormatContext *pFormatCtx = NULL; AVInputFormat *grab_iformat = NULL; + AVIOContext *pIOCtx = NULL; + MMFmemIOHandle *handle = NULL; + uint8_t *avio_ctx_buffer = NULL; if (!size || !width || !height) { return MMFILE_FORMAT_FAIL; @@ -564,8 +709,6 @@ int mmfile_format_get_frame_from_memory(const void *data, unsigned int datasize, return MMFILE_FORMAT_FAIL; } - ffurl_register_protocol(&MMFileMEMProtocol); - if (__getMimeType(format, mimeType, MMFILE_MIMETYPE_MAX_LEN) < 0) { debug_error(DEBUG, "error: Error in MIME Type finding\n"); return MMFILE_FORMAT_FAIL; @@ -584,28 +727,61 @@ int mmfile_format_get_frame_from_memory(const void *data, unsigned int datasize, if (NULL == grab_iformat) { debug_error(DEBUG, "error: cannot find format\n"); + ret = MMFILE_FORMAT_FAIL; goto exception; } - ret = avformat_open_input(&pFormatCtx, urifilename, grab_iformat, NULL); - if (ret < 0) { - debug_error(DEBUG, "error: cannot open %s %d\n", urifilename, ret); + avio_ctx_buffer = av_malloc(MMFILE_AVIO_BUFFER_LEN); + if (avio_ctx_buffer == NULL) { + debug_error(DEBUG, "error: cannot alloc avio_ctx_buffert\n"); + ret = MMFILE_FORMAT_FAIL; goto exception; } + ret = _mmf_mem_open(&handle, urifilename); + if (ret !=MMFILE_IO_SUCCESS) { + debug_warning(DEBUG, "failed to _mmf_mem_open.\n"); + av_free(avio_ctx_buffer); + ret = MMFILE_FORMAT_FAIL; + goto exception; + } + + pIOCtx = avio_alloc_context(avio_ctx_buffer, MMFILE_AVIO_BUFFER_LEN, 0, handle, _mmf_mem_read, NULL, _mmf_mem_seek); + if (pIOCtx == NULL) { + debug_error(DEBUG, "error: cannot alloc io context\n"); + av_free(avio_ctx_buffer); + mmfile_free(handle); + ret = MMFILE_FORMAT_FAIL; + goto exception; + } + + pFormatCtx = avformat_alloc_context(); if (!pFormatCtx) { - debug_warning(DEBUG, "failed to find av stream. maybe corrupted data.\n"); + debug_warning(DEBUG, "failed to avformat_alloc_context\n"); ret = MMFILE_FORMAT_FAIL; goto exception; } + pFormatCtx->pb = pIOCtx; + + ret = avformat_open_input(&pFormatCtx, "", grab_iformat, NULL); + if (ret < 0) { + debug_error(DEBUG, "error: cannot open %s %d\n", urifilename, ret); + goto exception; + } + ret = __mmfile_get_frame(pFormatCtx, timestamp, is_accurate, frame, size, width, height); exception: /* Close video file */ - if (pFormatCtx) avformat_close_input(&pFormatCtx); + if (pIOCtx) { + av_free(pIOCtx->buffer); + av_free(pIOCtx->opaque); + av_free(pIOCtx); + } - ffurl_deregister_protocol(&MMFileMEMProtocol); + if (pFormatCtx) + avformat_close_input(&pFormatCtx); return ret; } diff --git a/packaging/libmm-fileinfo.spec b/packaging/libmm-fileinfo.spec index 5fcb9ac..4c28c1c 100755 --- a/packaging/libmm-fileinfo.spec +++ b/packaging/libmm-fileinfo.spec @@ -1,6 +1,6 @@ Name: libmm-fileinfo Summary: Media Fileinfo -Version: 0.6.64 +Version: 0.6.65 Release: 0 Group: System/Libraries License: Apache-2.0 diff --git a/utils/include/mm_file_utils.h b/utils/include/mm_file_utils.h index 41da0e2..ef47dee 100755 --- a/utils/include/mm_file_utils.h +++ b/utils/include/mm_file_utils.h @@ -112,6 +112,7 @@ int MMFileFormatIsValidMPEGAUDIO(MMFileIOHandle *pFileIO, const char *mmfileuri, #define MMFILE_MEM_URI_LEN 6 #define MMFILE_MMAP_URI "mmap://" #define MMFILE_MMAP_URI_LEN 7 +#define MMFILE_AVIO_BUFFER_LEN 32768 #define MMFILE_RDONLY O_RDONLY #define MMFILE_WRONLY O_WRONLY -- 2.7.4