From 62704e231578d9593739d62b82a676ddbda84ac0 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Thu, 22 Jul 2021 20:45:10 +0300 Subject: sub: SDH filter: small refinements 1. On a pathological case where event_format is NULL, previously the filter was trying to use it with each new sub - and re-failed. Now the filter gets disabled on init (event_format doesn't change). 2. Previously, if the filter didn't modify the text or if the text could not be extracted - it still allocated a new packet with same content. Now it returns the original, saving a whole lot of no-ops (there are still few allocations in this case though). 1 above is preparation for the next commit, and 2 was trivial, but there's more to do if anyone cares (NIH string functions instead of bstr, unused arguments, messages could be improved, and more). --- sub/filter_sdh.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sub/filter_sdh.c b/sub/filter_sdh.c index 2b544ea222..9271cc261a 100644 --- a/sub/filter_sdh.c +++ b/sub/filter_sdh.c @@ -346,11 +346,6 @@ static void remove_leading_hyphen_space(struct sd_filter *sd, int start_pos, static char *filter_SDH(struct sd_filter *sd, char *format, int n_ignored, char *data, int length) { - if (!format) { - MP_VERBOSE(sd, "SDH filtering not possible - format missing\n"); - return length ? talloc_strndup(NULL, data, length) : talloc_strdup(NULL, data); - } - // need null terminated string char *ass = length ? talloc_strndup(NULL, data, length) : data; @@ -475,6 +470,11 @@ static bool sdh_init(struct sd_filter *ft) if (!ft->opts->sub_filter_SDH) return false; + if (!ft->event_format) { + MP_VERBOSE(ft, "SDH filtering not possible - format missing\n"); + return false; + } + return true; } @@ -489,10 +489,14 @@ static struct demux_packet *sdh_filter(struct sd_filter *ft, line = filter_SDH(ft, ft->event_format, 1, line, len); if (!line) return NULL; + if (0 == bstrcmp0((bstr){(char *)pkt->buffer, pkt->len}, line)) { + talloc_free(line); + return pkt; // unmodified, no need to allocate new packet + } // Stupidly, this copies it again. One could possibly allocate the packet // for writing in the first place (new_demux_packet()) and use - // demux_packet_shorten(). Or not allocate anything on no change. + // demux_packet_shorten(). struct demux_packet *npkt = new_demux_packet_from(line, strlen(line)); if (npkt) demux_packet_copy_attribs(npkt, pkt); -- cgit v1.2.3