initial code

This commit is contained in:
lieff 2018-09-25 00:29:16 +03:00
parent 4b424d818b
commit 9e0aeb5952
9 changed files with 3510 additions and 0 deletions

32
.travis.yml Normal file
View File

@ -0,0 +1,32 @@
language: c
addons:
apt:
packages:
- libc6-dev-i386
- linux-libc-dev:i386
- gcc-arm-none-eabi
- gcc-arm-linux-gnueabihf
- libnewlib-arm-none-eabi
- gcc-4.8-multilib
- gcc-4.8-aarch64-linux-gnu
- gcc-4.8-powerpc-linux-gnu
- gcc-aarch64-linux-gnu
- gcc-powerpc-linux-gnu
- libc6-armhf-cross
- libc6-arm64-cross
- libc6-powerpc-cross
- libc6-dev-armhf-cross
- libc6-dev-arm64-cross
- libc6-dev-powerpc-cross
- qemu
os:
- linux
compiler:
- gcc
script:
- scripts/build_x86.sh
- scripts/build_arm.sh
- scripts/test.sh

4
README.md Normal file
View File

@ -0,0 +1,4 @@
Mini MP4
==========
Easy embeddable MP4 mux/demux library.

3343
minimp4.h Normal file

File diff suppressed because it is too large Load Diff

92
minimp4_test.c Normal file
View File

@ -0,0 +1,92 @@
#define MINIMP4_IMPLEMENTATION
#include "minimp4.h"
#define VIDEO_FPS 30
static uint8_t *preload(const char *path, int *data_size)
{
FILE *file = fopen(path, "rb");
uint8_t *data;
*data_size = 0;
if (!file)
return 0;
if (fseek(file, 0, SEEK_END))
exit(1);
*data_size = (int)ftell(file);
if (*data_size < 0)
exit(1);
if (fseek(file, 0, SEEK_SET))
exit(1);
data = (unsigned char*)malloc(*data_size);
if (!data)
exit(1);
if ((int)fread(data, 1, *data_size, file) != *data_size)
exit(1);
fclose(file);
return data;
}
static int get_nal_size(uint8_t *buf, int size)
{
int pos = 3;
while ((size - pos) > 3)
{
if (buf[pos] == 0 && buf[pos + 1] == 0 && buf[pos + 2] == 1)
return pos;
if (buf[pos] == 0 && buf[pos + 1] == 0 && buf[pos + 2] == 0 && buf[pos + 3] == 1)
return pos;
pos++;
}
return size;
}
static void write_callback(int64_t offset, const void *buffer, size_t size, void *token)
{
FILE *f = (FILE*)token;
fseek(f, (long)offset, SEEK_SET);
fwrite(buffer, size, 1, f);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("usage: minimp4 in.h264 out.mp4\n");
return 0;
}
int h264_size;
uint8_t *alloc_buf;
uint8_t *buf_h264 = alloc_buf = preload(argv[1], &h264_size);
if (!buf_h264)
{
printf("error: can't open h264 file\n");
return 0;
}
FILE *fout = fopen(argv[2], "wb");
if (!fout)
{
printf("error: can't open output file\n");
return 0;
}
int sequential_mode = 0;
MP4E_mux_t *mux;
mp4_h264_writer_t mp4wr;
mux = MP4E__open(sequential_mode, fout, write_callback);
mp4_h264_write_init(&mp4wr, mux, 352, 288);
while (h264_size > 0)
{
int nal_size = get_nal_size(buf_h264, h264_size);
mp4_h264_write_nal(&mp4wr, buf_h264, nal_size, 90000/VIDEO_FPS);
buf_h264 += nal_size;
h264_size -= nal_size;
}
if (alloc_buf)
free(alloc_buf);
MP4E__close(mux);
if (fout)
fclose(fout);
}

9
scripts/build_arm.sh Executable file
View File

@ -0,0 +1,9 @@
_FILENAME=${0##*/}
CUR_DIR=${0/${_FILENAME}}
CUR_DIR=$(cd $(dirname ${CUR_DIR}); pwd)/$(basename ${CUR_DIR})/
pushd $CUR_DIR/..
arm-linux-gnueabihf-gcc -static -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=hard -DNDEBUG \
-flto -O3 -std=gnu11 -ffast-math -fomit-frame-pointer -ftree-vectorize \
-o minimp4_arm_gcc minimp4_test.c -lm -lpthread

9
scripts/build_x86.sh Executable file
View File

@ -0,0 +1,9 @@
_FILENAME=${0##*/}
CUR_DIR=${0/${_FILENAME}}
CUR_DIR=$(cd $(dirname ${CUR_DIR}); pwd)/$(basename ${CUR_DIR})/
pushd $CUR_DIR/..
gcc -flto -O3 -std=gnu11 -DNDEBUG \
-fno-stack-protector -ffunction-sections -fdata-sections -Wl,--gc-sections \
-o minimp4_x86 minimp4_test.c -lm -lpthread

21
scripts/test.sh Executable file
View File

@ -0,0 +1,21 @@
_FILENAME=${0##*/}
CUR_DIR=${0/${_FILENAME}}
CUR_DIR=$(cd $(dirname ${CUR_DIR}); pwd)/$(basename ${CUR_DIR})/
pushd $CUR_DIR/..
./minimp4_x86 vectors/foreman.264 out.mp4
if ! cmp ./out.mp4 vectors/out_ref.mp4 >/dev/null 2>&1
then
echo test failed
exit 1
fi
rm out.mp4
qemu-arm ./minimp4_arm_gcc vectors/foreman.264 out.mp4
if ! cmp ./out.mp4 vectors/out_ref.mp4 >/dev/null 2>&1
then
echo test failed
exit 1
fi
rm out.mp4
echo test passed

BIN
vectors/foreman.264 Normal file

Binary file not shown.

BIN
vectors/out_ref.mp4 Normal file

Binary file not shown.