From 3876d60f625baffa9208e1c49c33c5e93fa02c48 Mon Sep 17 00:00:00 2001 From: reimar Date: Fri, 22 Jan 2010 20:59:15 +0000 Subject: Avoid duplicating code to remove subtitles (sub_remove slave command). git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@30386 b3059339-0415-0410-9bf9-f77b7e298cf2 --- command.c | 79 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'command.c') diff --git a/command.c b/command.c index decb47f1a2..abb871841f 100644 --- a/command.c +++ b/command.c @@ -2335,6 +2335,45 @@ static const char *property_error_string(int error_value) return "UNKNOWN"; } +static void remove_subtitle_range(MPContext *mpctx, int start, int count) +{ + int idx; + int end = start + count; + int after = mpctx->set_of_sub_size - end; + sub_data **subs = mpctx->set_of_subtitles; + if (count < 0 || count > mpctx->set_of_sub_size || + start < 0 || start > mpctx->set_of_sub_size - count) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, + "Cannot remove invalid subtitle range %i +%i\n", start, count); + return; + } + for (idx = start; idx < end; idx++) { + sub_data *subd = subs[idx]; + mp_msg(MSGT_CPLAYER, MSGL_STATUS, + MSGTR_RemovedSubtitleFile, idx + 1, + filename_recode(subd->filename)); + sub_free(subd); + subs[idx] = NULL; + } + + mpctx->global_sub_size -= count; + mpctx->set_of_sub_size -= count; + if (mpctx->set_of_sub_size <= 0) + mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1; + + memmove(subs + start, subs + end, after * sizeof(*subs)); + memset(subs + start + after, 0, count * sizeof(*subs)); + + if (mpctx->set_of_sub_pos >= start && mpctx->set_of_sub_pos < end) { + mpctx->global_sub_pos = -2; + subdata = NULL; + mp_input_queue_cmd(mp_input_parse_cmd("sub_select")); + } else if (mpctx->set_of_sub_pos >= end) { + mpctx->set_of_sub_pos -= count; + mpctx->global_sub_pos -= count; + } +} + int run_command(MPContext * mpctx, mp_cmd_t * cmd) { sh_audio_t * const sh_audio = mpctx->sh_audio; @@ -2908,46 +2947,10 @@ int run_command(MPContext * mpctx, mp_cmd_t * cmd) case MP_CMD_SUB_REMOVE: if (sh_video) { int v = cmd->args[0].v.i; - sub_data *subd; if (v < 0) { - for (v = 0; v < mpctx->set_of_sub_size; ++v) { - subd = mpctx->set_of_subtitles[v]; - mp_msg(MSGT_CPLAYER, MSGL_STATUS, - MSGTR_RemovedSubtitleFile, v + 1, - filename_recode(subd->filename)); - sub_free(subd); - mpctx->set_of_subtitles[v] = NULL; - } - mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1; - mpctx->global_sub_size -= mpctx->set_of_sub_size; - mpctx->set_of_sub_size = 0; - if (mpctx->set_of_sub_pos >= 0) { - mpctx->global_sub_pos = -2; - subdata = NULL; - mp_input_queue_cmd(mp_input_parse_cmd("sub_select")); - } + remove_subtitle_range(mpctx, 0, mpctx->set_of_sub_size); } else if (v < mpctx->set_of_sub_size) { - subd = mpctx->set_of_subtitles[v]; - mp_msg(MSGT_CPLAYER, MSGL_STATUS, - MSGTR_RemovedSubtitleFile, v + 1, - filename_recode(subd->filename)); - sub_free(subd); - if (mpctx->set_of_sub_pos == v) { - mpctx->global_sub_pos = -2; - subdata = NULL; - mp_input_queue_cmd(mp_input_parse_cmd("sub_select")); - } else if (mpctx->set_of_sub_pos > v) { - --mpctx->set_of_sub_pos; - --mpctx->global_sub_pos; - } - while (++v < mpctx->set_of_sub_size) - mpctx->set_of_subtitles[v - 1] = - mpctx->set_of_subtitles[v]; - --mpctx->set_of_sub_size; - --mpctx->global_sub_size; - if (mpctx->set_of_sub_size <= 0) - mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1; - mpctx->set_of_subtitles[mpctx->set_of_sub_size] = NULL; + remove_subtitle_range(mpctx, v, 1); } } break; -- cgit v1.2.3 From 66998a98daef91678a51c3477fd06c59e1d7105a Mon Sep 17 00:00:00 2001 From: reimar Date: Fri, 22 Jan 2010 21:10:40 +0000 Subject: Also handle ASS subtitles when removing subtitles via sub_remove slave command. Fixes memory leaks and other strange behaviour like mismatch of name and subtitle data. Based on patch by Yuriy Kaminskiy [yumkam mail ru]. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@30387 b3059339-0415-0410-9bf9-f77b7e298cf2 --- command.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'command.c') diff --git a/command.c b/command.c index abb871841f..c355599502 100644 --- a/command.c +++ b/command.c @@ -2341,6 +2341,9 @@ static void remove_subtitle_range(MPContext *mpctx, int start, int count) int end = start + count; int after = mpctx->set_of_sub_size - end; sub_data **subs = mpctx->set_of_subtitles; +#ifdef CONFIG_ASS + ass_track_t **ass_tracks = mpctx->set_of_ass_tracks; +#endif if (count < 0 || count > mpctx->set_of_sub_size || start < 0 || start > mpctx->set_of_sub_size - count) { mp_msg(MSGT_CPLAYER, MSGL_ERR, @@ -2354,6 +2357,11 @@ static void remove_subtitle_range(MPContext *mpctx, int start, int count) filename_recode(subd->filename)); sub_free(subd); subs[idx] = NULL; +#ifdef CONFIG_ASS + if (ass_tracks[idx]) + ass_free_track(ass_tracks[idx]); + ass_tracks[idx] = NULL; +#endif } mpctx->global_sub_size -= count; @@ -2363,10 +2371,17 @@ static void remove_subtitle_range(MPContext *mpctx, int start, int count) memmove(subs + start, subs + end, after * sizeof(*subs)); memset(subs + start + after, 0, count * sizeof(*subs)); +#ifdef CONFIG_ASS + memmove(ass_tracks + start, ass_tracks + end, after * sizeof(*ass_tracks)); + memset(ass_tracks + start + after, 0, count * sizeof(*ass_tracks)); +#endif if (mpctx->set_of_sub_pos >= start && mpctx->set_of_sub_pos < end) { mpctx->global_sub_pos = -2; subdata = NULL; +#ifdef CONFIG_ASS + ass_track = NULL; +#endif mp_input_queue_cmd(mp_input_parse_cmd("sub_select")); } else if (mpctx->set_of_sub_pos >= end) { mpctx->set_of_sub_pos -= count; -- cgit v1.2.3