Skip to content

Commit

Permalink
Merge pull request #1504 from kaltura/mkv-fixes-and-lace-support
Browse files Browse the repository at this point in the history
mkv fixes and laces support
  • Loading branch information
erankor authored Jan 28, 2024
2 parents 1750eb0 + 484c498 commit 9b4d83d
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 148 deletions.
13 changes: 13 additions & 0 deletions vod/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
#define vod_log_debug2(level, log, err, fmt, arg1, arg2)
#define vod_log_debug3(level, log, err, fmt, arg1, arg2, arg3)
#define vod_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)
#define vod_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)
#define vod_log_debug6(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6)
#define vod_log_debug7(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7)

typedef int bool_t;
typedef int vod_status_t;
Expand All @@ -98,6 +101,7 @@ void vod_log_error(vod_uint_t level, vod_log_t *log, int err,

#define VOD_INT64_LEN NGX_INT64_LEN
#define VOD_INT32_LEN NGX_INT32_LEN
#define VOD_MAX_UINT32_VALUE NGX_MAX_UINT32_VALUE
#define VOD_MAX_SIZE_T_VALUE NGX_MAX_SIZE_T_VALUE
#define VOD_MAX_OFF_T_VALUE NGX_MAX_OFF_T_VALUE

Expand Down Expand Up @@ -263,6 +267,15 @@ void vod_log_error(vod_uint_t level, vod_log_t *log, int err,
#define vod_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4) \
ngx_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)

#define vod_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5) \
ngx_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)

#define vod_log_debug6(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
ngx_log_debug6(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6)

#define vod_log_debug7(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
ngx_log_debug7(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7)

#define vod_errno ngx_errno

typedef intptr_t bool_t;
Expand Down
46 changes: 29 additions & 17 deletions vod/mkv/ebml.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ ebml_read_num(ebml_context_t* context, uint64_t* result, size_t max_size, int re
}

static vod_status_t
ebml_read_size(ebml_context_t* context, uint64_t* result)
ebml_read_size(ebml_context_t* context, uint64_t* result, bool_t truncate)
{
vod_status_t rc;
uint64_t left;

rc = ebml_read_num(context, result, 8, 1);
if (rc < 0)
Expand All @@ -109,19 +110,28 @@ ebml_read_size(ebml_context_t* context, uint64_t* result)
return rc;
}

left = context->end_pos - context->cur_pos;
if (is_unknown_size(*result, rc))
{
*result = context->end_pos - context->cur_pos;
*result = left;
return VOD_OK;
}
else if (*result > (uint64_t)(context->end_pos - context->cur_pos))

if (*result <= left)
{
vod_log_error(VOD_LOG_ERR, context->request_context->log, 0,
"ebml_read_size: size %uL greater than the remaining stream bytes %uL",
*result, (uint64_t)(context->end_pos - context->cur_pos));
return VOD_BAD_DATA;
return VOD_OK;
}

return VOD_OK;
if (truncate)
{
*result = left;
return VOD_OK;
}

vod_log_error(VOD_LOG_ERR, context->request_context->log, 0,
"ebml_read_size: size %uL greater than the remaining stream bytes %uL",
*result, left);
return VOD_BAD_DATA;
}

static vod_status_t
Expand Down Expand Up @@ -194,9 +204,10 @@ ebml_parse_element(ebml_context_t* context, ebml_spec_t* spec, void* dest)
uint64_t size;
void* cur_dest;
vod_status_t rc;
ebml_type_t type;

// size
rc = ebml_read_size(context, &size);
rc = ebml_read_size(context, &size, spec->type & EBML_TRUNCATE_SIZE);
if (rc != VOD_OK)
{
vod_log_debug1(VOD_LOG_DEBUG_LEVEL, context->request_context->log, 0,
Expand All @@ -210,7 +221,8 @@ ebml_parse_element(ebml_context_t* context, ebml_spec_t* spec, void* dest)
return VOD_OK;
}

max_size = ebml_max_sizes[spec->type];
type = spec->type & ~EBML_TRUNCATE_SIZE;
max_size = ebml_max_sizes[type];
if (max_size && size > max_size)
{
vod_log_error(VOD_LOG_ERR, context->request_context->log, 0,
Expand All @@ -220,7 +232,7 @@ ebml_parse_element(ebml_context_t* context, ebml_spec_t* spec, void* dest)

cur_dest = (u_char*)dest + spec->offset;

switch (spec->type)
switch (type)
{
case EBML_UINT:
rc = ebml_read_uint(context, size, cur_dest);
Expand Down Expand Up @@ -249,9 +261,9 @@ ebml_parse_element(ebml_context_t* context, ebml_spec_t* spec, void* dest)
break;

case EBML_MASTER:
next_context.request_context = context->request_context;
next_context.cur_pos = context->cur_pos + size;
next_context.end_pos = context->end_pos;
next_context = *context;
next_context.cur_pos += size;

context->end_pos = next_context.cur_pos;
rc = ebml_parse_master(context, spec->child, cur_dest);
if (rc != VOD_OK)
Expand All @@ -264,9 +276,9 @@ ebml_parse_element(ebml_context_t* context, ebml_spec_t* spec, void* dest)
return VOD_OK;

case EBML_CUSTOM:
next_context.request_context = context->request_context;
next_context.cur_pos = context->cur_pos + size;
next_context.end_pos = context->end_pos;
next_context = *context;
next_context.cur_pos += size;

context->end_pos = next_context.cur_pos;
parser = spec->child;
rc = parser(context, spec, cur_dest);
Expand Down
3 changes: 3 additions & 0 deletions vod/mkv/ebml.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define ebml_read_id(context, id) ebml_read_num(context, id, 4, 0)
#define is_unknown_size(num, num_bytes) ((num) + 1 == 1ULL << (7 * (num_bytes)))

#define EBML_TRUNCATE_SIZE 0x80

// typedefs
typedef enum {
EBML_NONE,
Expand All @@ -22,6 +24,7 @@ typedef struct {
request_context_t* request_context;
const u_char* cur_pos;
const u_char* end_pos;
int64_t offset_delta;
} ebml_context_t;

typedef struct {
Expand Down
10 changes: 7 additions & 3 deletions vod/mkv/mkv_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mkv_codec_type_t mkv_codec_types[] = {
// video
{ vod_string("V_MPEG4/ISO/AVC"), VOD_CODEC_ID_AVC, FORMAT_AVC1, TRUE },
{ vod_string("V_MPEGH/ISO/HEVC"), VOD_CODEC_ID_HEVC, FORMAT_HEV1, TRUE },
{ vod_string("V_MPEGH/ISO/HEVC"), VOD_CODEC_ID_HEVC, FORMAT_HVC1, TRUE },
{ vod_string("V_VP8"), VOD_CODEC_ID_VP8, 0, FALSE },
{ vod_string("V_VP9"), VOD_CODEC_ID_VP9, 0, FALSE },
{ vod_string("V_AV1"), VOD_CODEC_ID_AV1, 0, FALSE },
Expand All @@ -15,7 +15,11 @@ mkv_codec_type_t mkv_codec_types[] = {
{ vod_string("A_AAC"), VOD_CODEC_ID_AAC, FORMAT_MP4A, TRUE },
{ vod_string("A_MPEG/L3"), VOD_CODEC_ID_MP3, FORMAT_MP4A, FALSE },
{ vod_string("A_VORBIS"), VOD_CODEC_ID_VORBIS,0, TRUE },
{ vod_string("A_OPUS"), VOD_CODEC_ID_OPUS, 0, TRUE },

{ vod_string("A_OPUS"), VOD_CODEC_ID_OPUS, FORMAT_OPUS, TRUE },
{ vod_string("A_AC3"), VOD_CODEC_ID_AC3, FORMAT_AC3, FALSE },
{ vod_string("A_EAC3"), VOD_CODEC_ID_EAC3, FORMAT_EAC3, FALSE },
{ vod_string("A_DTS"), VOD_CODEC_ID_DTS, 0, TRUE },
{ vod_string("A_FLAC"), VOD_CODEC_ID_FLAC, FORMAT_FLAC, TRUE },

{ vod_null_string, 0, 0, FALSE }
};
3 changes: 3 additions & 0 deletions vod/mkv/mkv_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
// cluster
#define MKV_ID_CLUSTERTIMECODE (0xE7)
#define MKV_ID_SIMPLEBLOCK (0xA3)
#define MKV_ID_BLOCKGROUP (0xA0)
#define MKV_ID_BLOCK (0xA1)
#define MKV_ID_REFERENCEBLOCK (0xFB)
#define MKV_ID_CLUSTER (0x1F43B675)

// sections
Expand Down
Loading

0 comments on commit 9b4d83d

Please sign in to comment.