diff options
-rw-r--r-- | libmkv/.gitignore | 3 | ||||
-rw-r--r-- | libmkv/Makefile | 6 | ||||
-rw-r--r-- | libmkv/nut-3.c | 25 | ||||
-rw-r--r-- | libmkv/nut-crc32.c | 47 |
4 files changed, 67 insertions, 14 deletions
diff --git a/libmkv/.gitignore b/libmkv/.gitignore new file mode 100644 index 0000000..bc7a3df --- /dev/null +++ b/libmkv/.gitignore @@ -0,0 +1,3 @@ +/nut-3 +/nut-crc32 +*.nut diff --git a/libmkv/Makefile b/libmkv/Makefile new file mode 100644 index 0000000..3c82486 --- /dev/null +++ b/libmkv/Makefile @@ -0,0 +1,6 @@ +CFLAGS = -Wall -Werror -Wextra + +all: $(patsubst %.c,%,$(filter-out mkv.c,$(wildcard *.c))) +clean: + rm -f -- $(patsubst %.c,%,$(filter-out mkv.c,$(wildcard *.c))) + rm -f -- *.avi *.mkv diff --git a/libmkv/nut-3.c b/libmkv/nut-3.c index 0441400..2500af5 100644 --- a/libmkv/nut-3.c +++ b/libmkv/nut-3.c @@ -254,7 +254,6 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { struct buf out = {0}; struct buf pkt = {0}; - struct buf hdr = {0}; append(&out, nut_file_id_string, sizeof(nut_file_id_string)); @@ -265,8 +264,8 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { * there goes a good debugging tool. */ +#define BOGUS(n) n /* main_header ********************************************************/ -#define BOGUS 0 pkt.len = 0; /* head *******************************************/ @@ -289,13 +288,13 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { /* frame_code=0 (invalid) */ nut_append_vu(&pkt, NUT_FRAMEFLAG_INVALID); /*! flags */ nut_append_vu(&pkt, 2); /*! field_count */ - nut_append_vu(&pkt, BOGUS); /*! 0: fields.pts */ + nut_append_vu(&pkt, BOGUS(0)); /*! 0: fields.pts */ nut_append_vu(&pkt, 1); /*! 1: fields.size_msb_nul */ /* frame_code=1 (640x480) */ nut_append_vu(&pkt, APP_COMMON_FLAGS); /*! flags */ nut_append_vu(&pkt, 6); /*! field_count */ - nut_append_vu(&pkt, BOGUS); /*! 0: fields.pts */ + nut_append_vu(&pkt, BOGUS(0)); /*! 0: fields.pts */ nut_append_vu(&pkt, APP_FRAME_SIZE_MSB_MUL); /*! 1: fields.size_msb_nul */ nut_append_vu(&pkt, 0); /*! 2: fields.stream */ nut_append_vu(&pkt, APP_FRAME_SIZE(640_480) % /*! 3: fields.size_lsb */ @@ -306,7 +305,7 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { /* frame_code=2 (720x480) */ nut_append_vu(&pkt, APP_COMMON_FLAGS); /*! flags */ nut_append_vu(&pkt, 6); /*! field_count */ - nut_append_vu(&pkt, BOGUS); /*! 0: fields.pts */ + nut_append_vu(&pkt, BOGUS(0)); /*! 0: fields.pts */ nut_append_vu(&pkt, APP_FRAME_SIZE_MSB_MUL); /*! 1: fields.size_msb_mul */ nut_append_vu(&pkt, 0); /*! 2: fields.stream */ nut_append_vu(&pkt, APP_FRAME_SIZE(720_480) % /*! 3: fields.size_lsb */ @@ -317,7 +316,7 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { /* frame_code=3 (720x576) */ nut_append_vu(&pkt, APP_COMMON_FLAGS); /*! flags */ nut_append_vu(&pkt, 6); /*! field_count */ - nut_append_vu(&pkt, BOGUS); /*! 0: fields.pts */ + nut_append_vu(&pkt, BOGUS(0)); /*! 0: fields.pts */ nut_append_vu(&pkt, APP_FRAME_SIZE_MSB_MUL); /*! 1: fields.size_msb_nul */ nut_append_vu(&pkt, 0); /*! 2: fields.stream */ nut_append_vu(&pkt, APP_FRAME_SIZE(720_576) % /*! 3: fields.size_lsb */ @@ -328,7 +327,7 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { /* frame_code=4-255 (invalid) */ nut_append_vu(&pkt, NUT_FRAMEFLAG_INVALID); /*! flags */ nut_append_vu(&pkt, 2); /*! field_count */ - nut_append_vu(&pkt, BOGUS); /*! 0: fields.pts */ + nut_append_vu(&pkt, BOGUS(0)); /*! 0: fields.pts */ nut_append_vu(&pkt, 256-4-1); /*! 1: fields.size_msb_nul */ /* tail *******************************************/ @@ -336,31 +335,29 @@ bool app_write_intro(int fd, struct app_state *state, uint64_t time_ns) { nut_append_vu(&pkt, 0); /*! main_flags */ nut_append_packet(&out, nut_startcode_main, pkt.dat, pkt.len); -#undef BOGUS /* stream_header ******************************************************/ -#define BOGUS 1 pkt.len = 0; nut_append_vu(&pkt, 0); /*! stream_id */ nut_append_vu(&pkt, NUT_STREAMCLASS_VIDEO); /*! stream_class */ nut_append_vb(&pkt, "BGR\x08", 4); /*! fourcc */ nut_append_vu(&pkt, 0); /*! time_base_id */ - nut_append_vu(&pkt, BOGUS); /*! msb_pts_shift (only relevant if FRAMEFLAG_CODED_PTS) */ - nut_append_vu(&pkt, BOGUS); /*! max_pts_distance (all frames have a checksum) */ + nut_append_vu(&pkt, BOGUS(0)); /*! msb_pts_shift (only relevant if FRAMEFLAG_CODED_PTS) */ + nut_append_vu(&pkt, BOGUS(0)); /*! max_pts_distance (all frames have a checksum) */ nut_append_vu(&pkt, 0); /*! decode_delay */ nut_append_vu(&pkt, 0); /*! stream_flags */ nut_append_vb(&pkt, NULL, 0); /*! codec_specific_data */ - nut_append_vu(&pkt, BOGUS); /*! width (all frames set width) */ - nut_append_vu(&pkt, BOGUS); /*! height (all frames set height) */ + nut_append_vu(&pkt, BOGUS(640)); /*! width (all frames set width) */ + nut_append_vu(&pkt, BOGUS(480)); /*! height (all frames set height) */ nut_append_vu(&pkt, 1); /*! sample_width */ nut_append_vu(&pkt, 2); /*! sample_height */ nut_append_vu(&pkt, NUT_COLORSPACE_UNKNOWN); /*! colorspace_type */ nut_append_packet(&out, nut_startcode_stream, pkt.dat, pkt.len); -#undef BOGUS /* flush **************************************************************/ +#undef BOGUS bool ret = xwrite(fd, out.dat, out.len); free(out.dat); diff --git a/libmkv/nut-crc32.c b/libmkv/nut-crc32.c new file mode 100644 index 0000000..e81c5d7 --- /dev/null +++ b/libmkv/nut-crc32.c @@ -0,0 +1,47 @@ +#define _GNU_SOURCE /* for error() */ +#include <errno.h> /* for errno */ +#include <error.h> /* for error() */ +#include <stdint.h> /* for uint{n}_t */ +#include <stdio.h> /* for printf() */ +#include <unistd.h> /* for read() */ + +void nut_crc32_init(uint32_t *crc) { + /* "Starting value is zero." */ + *crc = 0; +} + +void nut_crc32_write(uint32_t *crc, uint8_t *dat, size_t len) { + /* "Generator polynomial is 0x104C11DB7." */ + static const uint32_t table[16] = { + 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, + 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005, + 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61, + 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, + }; + + while (len--) { + *crc ^= *dat++ << 24; + *crc = (*crc<<4) ^ table[*crc>>28]; + *crc = (*crc<<4) ^ table[*crc>>28]; + } +} + +int main() { + uint32_t crc; + uint8_t buf[128]; + + nut_crc32_init(&crc); + + size_t tot = 0; + for (;;) { + ssize_t n = read(0, buf, sizeof(buf)); + if (n <= 0) + break; + nut_crc32_write(&crc, buf, n); + tot += n; + } + printf("crc32 = 0x%08x ; size = %zu\n", crc, tot); + if (errno) + error(1, errno, "read"); + return 0; +} |