Skip to content

Commit

Permalink
[libmp4parser] refine stream
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 8, 2018
1 parent 49634a4 commit 6b352b4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 95 deletions.
3 changes: 1 addition & 2 deletions libmp4parser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ OBJS_UNIT_TEST = test_$(LIBNAME).o
CFLAGS := -g -Wall -Werror -fPIC
CFLAGS += $($(ARCH)_CFLAGS)
CFLAGS += -I$(OUTPUT)/include
CFLAGS += -I../libfile/

SHARED := -shared

Expand All @@ -85,7 +84,7 @@ $(TGT_LIB_A): $(OBJS_LIB)
$(AR_V) rcs $@ $^

$(TGT_LIB_SO): $(OBJS_LIB)
$(LD_V) -o $@ $^ $(SHARED)
$(CC_V) -o $@ $^ $(SHARED) $(LDFLAGS)
@mv $(TGT_LIB_SO) $(TGT_LIB_SO_VER)
@ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO)

Expand Down
7 changes: 3 additions & 4 deletions libmp4parser/libmp4parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

struct mp4_parser *mp4_parser_create(const char *file)
{
stream_t *stream = create_file_stream();
if (stream_open(stream, file, MODE_READ) == 0) {
printf("stream_open %s failed\n", file);
stream_t *stream = create_file_stream(file);
if (!stream) {
printf("create_file_stream failed!\n");
return NULL;
}
struct mp4_parser *mp = (struct mp4_parser *)calloc(1, sizeof(struct mp4_parser));
Expand All @@ -43,7 +43,6 @@ void mp4_parser_destroy(struct mp4_parser *mp)
stream_t *stream = (stream_t *)mp->opaque_stream;
MP4_Box_t *root = (MP4_Box_t *)mp->opaque_root;
MP4_BoxFree(stream, root);
stream_close(stream);
destory_file_stream(stream);
free(mp);
}
Expand Down
108 changes: 36 additions & 72 deletions libmp4parser/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ void msg_log(int log_lvl, const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
switch (log_lvl) {
#ifdef MP4_VERBOSE
case MSG_DGB:
//printf("debug: %s\n", buf);
printf("debug: %s\n", buf);
break;
case MSG_WARN:
//printf("warn: %s\n", buf);
printf("warn: %s\n", buf);
break;
#endif
case MSG_ERR:
printf("err: %s\n", buf);
break;
Expand All @@ -113,102 +115,64 @@ void msg_log(int log_lvl, const char *fmt, ...)
}
}

static void* file_open(stream_t *stream_s, const char* filename, int mode)
int stream_Read(stream_t *s, void* buf, int size)
{
FILE* file = NULL;
const char* mode_fopen = NULL;
if ((mode & MODE_READWRITEFILTER) == MODE_READ) {
mode_fopen = "rb";
} else if (mode & MODE_EXISTING) {
mode_fopen = "r+b";
} else if (mode & MODE_CREATE) {
mode_fopen = "wb";
}
if ((filename != NULL) && (mode_fopen != NULL)) {
file = fopen(filename, mode_fopen);
}
stream_s->opaque = (void*)file;
return file;
}

static int file_read(stream_t *stream_s, void* buf, int size)
{
FILE* file = (FILE*)stream_s->opaque;
return fread(buf, 1, size, file);
}

static int file_write(stream_t *stream_s, void *buf, int size)
{
FILE* file = (FILE*)stream_s->opaque;
return fwrite(buf, 1, size, file);
return fread(buf, 1, size, s->fp);
}

static uint64_t file_seek(stream_t *stream_s, int64_t offset, int whence)
uint64_t stream_Seek(stream_t *s, int64_t offset)
{
FILE* file = (FILE*)stream_s->opaque;
return fseek(file, offset, whence);
return fseek(s->fp, offset, SEEK_SET);
}

static int64_t file_tell(stream_t *stream_s)
int64_t stream_Tell(stream_t *s)
{
FILE* file = (FILE*)stream_s->opaque;
return ftell(file);
return ftell(s->fp);
}

static int file_peek(stream_t *stream_s, const uint8_t **buf, int size)
int stream_Peek(stream_t *s, const uint8_t **buf, int size)
{
uint32_t offset = file_tell(stream_s);
uint32_t offset = stream_Tell(s);
*buf = (uint8_t *)calloc(1, size);
stream_s->priv_buf_num++;
stream_s->priv_buf = (void **)realloc(stream_s->priv_buf, stream_s->priv_buf_num * sizeof(uint8_t*));
stream_s->priv_buf[stream_s->priv_buf_num-1] = (void *)*buf;
int ret = file_read(stream_s, (void *)*buf, size);
file_seek(stream_s, offset, SEEK_SET);
s->priv_buf_num++;
s->priv_buf = (void **)realloc(s->priv_buf, s->priv_buf_num * sizeof(uint8_t*));
s->priv_buf[s->priv_buf_num-1] = (void *)*buf;
int ret = stream_Read(s, (void *)*buf, size);
stream_Seek(s, offset);
return ret;
}

static int64_t file_size(stream_t *stream_s)
int64_t stream_Size(stream_t *s)
{
FILE* file = (FILE*)stream_s->opaque;
long size;
long tmp = ftell(file);
fseek(file, 0L, SEEK_END);
size = ftell(file);
fseek(file, tmp, SEEK_SET);
return (size_t)size;
long tmp = ftell(s->fp);
fseek(s->fp, 0L, SEEK_END);
size = ftell(s->fp);
fseek(s->fp, tmp, SEEK_SET);
return (int64_t)size;
}

static int file_close(stream_t *stream_s)
{
FILE* file = (FILE*)stream_s->opaque;
return fclose(file);
}

stream_t* create_file_stream()
stream_t* create_file_stream(const char *filename)
{
stream_t* s = (stream_t*)calloc(1, sizeof(stream_t));
s->open = file_open;
s->read = file_read;
s->write = file_write;
s->peek = file_peek;
s->seek = file_seek;
s->tell = file_tell;
s->size = file_size;
s->close = file_close;
s->priv_buf_num = 0;
s->priv_buf = (void **)calloc(1, sizeof(uint32_t));
s->fp = fopen(filename, "rb");
if (!s->fp) {
printf("fopen %s failed!\n", filename);
free(s);
return NULL;
}
return s;
}

void destory_file_stream(stream_t* stream_s)
void destory_file_stream(stream_t* s)
{
int i;
for (i = 0; i < stream_s->priv_buf_num; i++) {
free(stream_s->priv_buf[i]);
fclose(s->fp);
for (i = 0; i < s->priv_buf_num; i++) {
free(s->priv_buf[i]);
}
free(stream_s->priv_buf);
free(stream_s);
free(s->priv_buf);
free(s);
}



23 changes: 6 additions & 17 deletions libmp4parser/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,19 @@ static inline uint64_t U64_AT (const void *p)


typedef struct stream {
void *(*open)(struct stream *stream_s, const char *filename, int mode);
int (*read)(struct stream *stream_s, void *buf, int size);
int (*write)(struct stream *stream_s, void *buf, int size);
int (*peek)(struct stream *stream_s, const uint8_t **buf, int size);
uint64_t (*seek)(struct stream *stream_s, int64_t offset, int whence);
int64_t (*tell)(struct stream *stream_s);
int64_t (*size)(struct stream *stream_s);
int (*close)(struct stream *stream_s);
void *opaque;
FILE *fp;
void **priv_buf;//store peek malloc buffer
int priv_buf_num;
} stream_t;

stream_t* create_file_stream();
void destory_file_stream(stream_t* stream_s);

#define stream_open(s, filename, mode) ((stream_t*)s)->open(((stream_t*)s), filename, mode)
#define stream_close(s) ((stream_t*)s)->close(((stream_t*)s))
#define stream_Read(s, buf, size) ((stream_t*)s)->read(((stream_t*)s), buf, size)
#define stream_write(s, buf, size) ((stream_t*)s)->write(((stream_t*)s), buf, size)
#define stream_Peek(s, buf, size) ((stream_t*)s)->peek(((stream_t*)s), buf, size)
#define stream_Seek(s, offset) ((stream_t*)s)->seek(((stream_t*)s), offset, SEEK_SET)
#define stream_Tell(s) ((stream_t*)s)->tell(((stream_t*)s))
#define stream_Size(s) ((stream_t*)s)->size(((stream_t*)s))
int stream_Read(stream_t *stream_s, void* buf, int size);
int stream_Peek(stream_t *stream_s, const uint8_t **buf, int size);
uint64_t stream_Seek(stream_t *stream_s, int64_t offset);
int64_t stream_Tell(stream_t *stream_s);
int64_t stream_Size(stream_t *stream_s);

bool decodeQtLanguageCode( uint16_t i_language_code, char *psz_iso,
bool *b_mactables );
Expand Down
4 changes: 4 additions & 0 deletions libmp4parser/test_libmp4parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ int main(int argc, char* argv[])
}
struct mp4_parser *mp = mp4_parser_create(argv[1]);
uint64_t duration = 0;
uint32_t w, h;
mp4_get_duration(mp, &duration);
printf("duration = %lu\n", duration);
mp4_get_resolution(mp, &w, &h);
printf("resolution = %dx%d\n", (int)w, (int)h);
mp4_parser_destroy(mp);
return 0;
}

Expand Down

0 comments on commit 6b352b4

Please sign in to comment.