From fac11ec5ed1f1626248ffada824fa853fb9350ec Mon Sep 17 00:00:00 2001 From: kapil kumar Date: Sun, 5 Mar 2017 23:45:43 +0530 Subject: [PATCH 1/2] modified subtitle file name genrated by matroska(mkv) --- src/lib_ccx/matroska.c | 75 +++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/src/lib_ccx/matroska.c b/src/lib_ccx/matroska.c index e9881f0f..66066105 100644 --- a/src/lib_ccx/matroska.c +++ b/src/lib_ccx/matroska.c @@ -1,7 +1,8 @@ #include "lib_ccx.h" #include "matroska.h" #include - +#include +#define MAX_FILE_NAME_SIZE 200 void skip_bytes(FILE* file, ULLONG n) { FSEEK(file, n, SEEK_CUR); } @@ -33,13 +34,11 @@ UBYTE mkv_read_byte(FILE* file) { ULLONG read_vint_length(FILE* file) { UBYTE ch = mkv_read_byte(file); - int cnt = 0; - for (int i = 7; i >= 0; i--) { - if ((ch & (1 << i)) != 0) { - cnt = 8 - i; - break; - } - } +#ifdef _WIN32 + int cnt = 8 - __lzcnt16(ch); +#else + int cnt = __builtin_clz(ch) - 24 + 1; +#endif ch ^= (1 << (8 - cnt)); ULLONG ret = ch; for (int i = 1; i < cnt; i++) { @@ -127,7 +126,7 @@ void parse_ebml(FILE* file) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping EBML block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping EBML block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -174,7 +173,7 @@ void parse_segment_info(FILE* file) { read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_INFO_TIMECODE_SCALE: - mprint("Timecode scale: %I64d\n", read_vint_block_int(file)); + mprint("Timecode scale: %lld\n", read_vint_block_int(file)); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_INFO_DURATION: read_vint_block_skip(file); @@ -201,7 +200,7 @@ void parse_segment_info(FILE* file) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment info block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment info block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -221,7 +220,7 @@ char* generate_timestamp_utf8(ULLONG milliseconds) { ULLONG hours = milliseconds; char* buf = malloc(sizeof(char) * 15); - sprintf(buf, "%02I64d:%02I64d:%02I64d,%03I64d", hours, minutes, seconds, millis); + sprintf(buf, "%02ld:%02ld:%02ld,%03ld", hours, minutes, seconds, millis); return buf; } @@ -235,7 +234,7 @@ char* generate_timestamp_ass_ssa(ULLONG milliseconds) { ULLONG hours = milliseconds; char* buf = malloc(sizeof(char) * 15); - sprintf(buf, "%I64d:%02I64d:%02I64d.%02I64d", hours, minutes, seconds, millis); + sprintf(buf, "%ld:%02ld:%02ld.%02ld", hours, minutes, seconds, millis); return buf; } @@ -348,7 +347,7 @@ void parse_segment_cluster_block_group(struct matroska_ctx* mkv_ctx, ULLONG clus default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment cluster block group\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment cluster block group\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -371,6 +370,7 @@ void parse_segment_cluster_block_group(struct matroska_ctx* mkv_ctx, ULLONG clus } void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { + FILE* file = mkv_ctx->file; ULLONG len = read_vint_length(file); ULLONG pos = get_current_byte(file); @@ -417,7 +417,7 @@ void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment cluster block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment cluster block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -434,6 +434,7 @@ void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { } char* get_track_entry_type_description(enum matroska_track_entry_type type) { + switch (type) { case MATROSKA_TRACK_TYPE_VIDEO: return "video"; @@ -485,7 +486,7 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { /* Track entry ids*/ case MATROSKA_SEGMENT_TRACK_TRACK_NUMBER: track_number = read_vint_block_int(file); - mprint(" Track number: %I64d\n", track_number); + mprint(" Track number: %lld\n", track_number); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_TRACK_TRACK_UID: mprint(" UID: %llu\n", read_vint_block_int(file)); @@ -576,12 +577,12 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { /* Deprecated IDs */ case MATROSKA_SEGMENT_TRACK_TRACK_TIMECODE_SCALE: - mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %I64d\n", code, + mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %lld\n", code, get_current_byte(file) - 3); // minus length of the ID read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_TRACK_TRACK_OFFSET: - mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %I64d\n", code, + mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %lld\n", code, get_current_byte(file) - 2); // minus length of the ID read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); @@ -612,7 +613,7 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment track entry block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment track entry block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -669,7 +670,7 @@ void parse_segment_tracks(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment tracks block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment tracks block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -728,7 +729,7 @@ void parse_segment(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -737,15 +738,35 @@ void parse_segment(struct matroska_ctx* mkv_ctx) } } } +char *filename_without_ext(char* filename) { + char *returnname; + char *lastdot; + if (filename == NULL) + return filename; + if ((returnname = malloc (strlen (filename) + 1)) == NULL) + return filename; + strcpy (returnname, filename); + lastdot = strrchr (returnname, '.'); + if (lastdot != NULL) + *lastdot = '\0'; + return returnname; +} char* generate_filename_from_track(struct matroska_ctx* mkv_ctx, struct matroska_sub_track* track) -{ - char* buf = malloc(sizeof(char) * 200); +{ + char* filename = malloc(sizeof(char)*(MAX_FILE_NAME_SIZE-30)); + char * filename_non_ext; + filename[MAX_FILE_NAME_SIZE-31] = '\0'; + strncpy(filename, mkv_ctx->filename,(MAX_FILE_NAME_SIZE-31)); + filename_non_ext=filename_without_ext(filename); + char* buf = malloc(sizeof(char) * MAX_FILE_NAME_SIZE); if (track->lang_index == 0) - sprintf(buf, "%s_%s.%s", mkv_ctx->filename, track->lang, matroska_track_text_subtitle_id_extensions[track->codec_id]); + sprintf(buf, "%s_%s.%s", filename_non_ext, track->lang, matroska_track_text_subtitle_id_extensions[track->codec_id]); else - sprintf(buf, "%s_%s_%llu.%s", mkv_ctx->filename, track->lang, track->lang_index, + sprintf(buf, "%s_%s_%llu.%s", filename_without_ext, track->lang, track->lang_index, matroska_track_text_subtitle_id_extensions[track->codec_id]); +free(filename); +free(filename_non_ext); return buf; } @@ -892,7 +913,7 @@ void matroska_parse(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping file parsing\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping file parsing\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); return; } @@ -946,4 +967,4 @@ int matroska_loop(struct lib_ccx_ctx *ctx) mprint("\n"); return sentence_count; -} \ No newline at end of file +} From d80a4f4b3c3e60ced4a9d25511463ba0c527c8cc Mon Sep 17 00:00:00 2001 From: kapil kumar Date: Sun, 5 Mar 2017 23:57:57 +0530 Subject: [PATCH 2/2] modified subtitle file name genrated by matroska(mkv) -some changes --- src/lib_ccx/matroska.c | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/lib_ccx/matroska.c b/src/lib_ccx/matroska.c index 66066105..9a9646ab 100644 --- a/src/lib_ccx/matroska.c +++ b/src/lib_ccx/matroska.c @@ -1,8 +1,7 @@ #include "lib_ccx.h" #include "matroska.h" #include -#include -#define MAX_FILE_NAME_SIZE 200 + void skip_bytes(FILE* file, ULLONG n) { FSEEK(file, n, SEEK_CUR); } @@ -34,11 +33,13 @@ UBYTE mkv_read_byte(FILE* file) { ULLONG read_vint_length(FILE* file) { UBYTE ch = mkv_read_byte(file); -#ifdef _WIN32 - int cnt = 8 - __lzcnt16(ch); -#else - int cnt = __builtin_clz(ch) - 24 + 1; -#endif + int cnt = 0; + for (int i = 7; i >= 0; i--) { + if ((ch & (1 << i)) != 0) { + cnt = 8 - i; + break; + } + } ch ^= (1 << (8 - cnt)); ULLONG ret = ch; for (int i = 1; i < cnt; i++) { @@ -126,7 +127,7 @@ void parse_ebml(FILE* file) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping EBML block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping EBML block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -173,7 +174,7 @@ void parse_segment_info(FILE* file) { read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_INFO_TIMECODE_SCALE: - mprint("Timecode scale: %lld\n", read_vint_block_int(file)); + mprint("Timecode scale: %I64d\n", read_vint_block_int(file)); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_INFO_DURATION: read_vint_block_skip(file); @@ -200,7 +201,7 @@ void parse_segment_info(FILE* file) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment info block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment info block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -220,7 +221,7 @@ char* generate_timestamp_utf8(ULLONG milliseconds) { ULLONG hours = milliseconds; char* buf = malloc(sizeof(char) * 15); - sprintf(buf, "%02ld:%02ld:%02ld,%03ld", hours, minutes, seconds, millis); + sprintf(buf, "%02I64d:%02I64d:%02I64d,%03I64d", hours, minutes, seconds, millis); return buf; } @@ -234,7 +235,7 @@ char* generate_timestamp_ass_ssa(ULLONG milliseconds) { ULLONG hours = milliseconds; char* buf = malloc(sizeof(char) * 15); - sprintf(buf, "%ld:%02ld:%02ld.%02ld", hours, minutes, seconds, millis); + sprintf(buf, "%I64d:%02I64d:%02I64d.%02I64d", hours, minutes, seconds, millis); return buf; } @@ -347,7 +348,7 @@ void parse_segment_cluster_block_group(struct matroska_ctx* mkv_ctx, ULLONG clus default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment cluster block group\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment cluster block group\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -370,7 +371,6 @@ void parse_segment_cluster_block_group(struct matroska_ctx* mkv_ctx, ULLONG clus } void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { - FILE* file = mkv_ctx->file; ULLONG len = read_vint_length(file); ULLONG pos = get_current_byte(file); @@ -417,7 +417,7 @@ void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment cluster block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment cluster block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -434,7 +434,6 @@ void parse_segment_cluster(struct matroska_ctx* mkv_ctx) { } char* get_track_entry_type_description(enum matroska_track_entry_type type) { - switch (type) { case MATROSKA_TRACK_TYPE_VIDEO: return "video"; @@ -486,7 +485,7 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { /* Track entry ids*/ case MATROSKA_SEGMENT_TRACK_TRACK_NUMBER: track_number = read_vint_block_int(file); - mprint(" Track number: %lld\n", track_number); + mprint(" Track number: %I64d\n", track_number); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_TRACK_TRACK_UID: mprint(" UID: %llu\n", read_vint_block_int(file)); @@ -577,12 +576,12 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { /* Deprecated IDs */ case MATROSKA_SEGMENT_TRACK_TRACK_TIMECODE_SCALE: - mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %lld\n", code, + mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %I64d\n", code, get_current_byte(file) - 3); // minus length of the ID read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); case MATROSKA_SEGMENT_TRACK_TRACK_OFFSET: - mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %lld\n", code, + mprint(MATROSKA_WARNING "Deprecated element 0x%x at position %I64d\n", code, get_current_byte(file) - 2); // minus length of the ID read_vint_block_skip(file); MATROSKA_SWITCH_BREAK(code, code_len); @@ -613,7 +612,7 @@ void parse_segment_track_entry(struct matroska_ctx* mkv_ctx) { MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment track entry block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment track entry block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -670,7 +669,7 @@ void parse_segment_tracks(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment tracks block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment tracks block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -729,7 +728,7 @@ void parse_segment(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping segment block\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping segment block\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); set_bytes(file, pos + len); return; @@ -738,6 +737,7 @@ void parse_segment(struct matroska_ctx* mkv_ctx) } } } + char *filename_without_ext(char* filename) { char *returnname; char *lastdot; @@ -913,7 +913,7 @@ void matroska_parse(struct matroska_ctx* mkv_ctx) MATROSKA_SWITCH_BREAK(code, code_len); default: if (code_len == MATROSKA_MAX_ID_LENGTH) { - mprint(MATROSKA_ERROR "Unknown element 0x%x at position %lld, skipping file parsing\n", code, + mprint(MATROSKA_ERROR "Unknown element 0x%x at position %I64d, skipping file parsing\n", code, get_current_byte(file) - MATROSKA_MAX_ID_LENGTH); return; }