From patchwork Mon May 25 19:43:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19866 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 5DE23449D0B for ; Mon, 25 May 2020 22:43:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4A7BC6898BD; Mon, 25 May 2020 22:43:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DC8996882F4 for ; Mon, 25 May 2020 22:43:36 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id i15so18055149wrx.10 for ; Mon, 25 May 2020 12:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MDwYHX/Hg0CN+nfeCYfjyex1T4velIDHkSZPKccnPBE=; b=nBzVE8iEwCtpap5vf4f91ADgpqtpGQE0IDJZswBuORRZiI5Zo+P0r2FafSbKGhQf87 P6HA83lsOv3K1pelgfdkvZal50xpxlv2G/a3vKjjokGW8Gc9Aq09Yhi3TvVYTjlZIXzD Su64URj7EO7gCcYa78UbJOG81/NVYCzCeB7QCWvPa5jNLopuyWyGp12uGOy1n/ApFSPb hG25+UT2QHbB4xsdEK2Zzzgu6zP2cobkR2y8GRcRx257GzZqh/c70vVVm/FvPE9T+cqL UrG1twKFNoBqaROdTsGWGKMj1Uct6PgLcluaDPXTIL90sfS1YauCYHTQeT1GsF7m/M2i 05QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MDwYHX/Hg0CN+nfeCYfjyex1T4velIDHkSZPKccnPBE=; b=ow7N8Uxt53urLq2GsU9uJ6p6xNcg4aAK9bzrQ5/qowFFmMHiLalhr66W5hIFwMiUl8 tLTy+wkYZrjrWOqZARktg3vJ20/hxlM7+yI81PCWv4bYDVkNTIru9BARVnwBY8vceVw5 e+CKuMN5d7MDGX2sDPmSdDl2P2wTPTh43MBLqJiBTGehs7H6DienEtV5Cb0gUQjFE54k 1+7ae68yImN7kY9ifgx3+mCqFEL4Sk06A4+3kqa89R9AGHNz6jZuhyMZ6jw++kD2XXe0 6UETGQjfplAEgMRLsJiBA3HeXnTGT1Kepo9taneqEsB+ItxNxV4J2g8BRah0aTB9Ww73 M1OQ== X-Gm-Message-State: AOAM532DkwSvWcebTXxLFSArrKJW972e9nkGNBBqmDz+Q9XmjcNCSjlG kLoCJ/16R0MuDKdH2M4Lad+iTGKL X-Google-Smtp-Source: ABdhPJzVzy9y4Olh0PPHFZX6U1x4B01KVLYweqECaczlEf7O+4MLkZwGa8bzgzhMfTtBpBjyZiKJQg== X-Received: by 2002:adf:ab1b:: with SMTP id q27mr9328152wrc.268.1590435816080; Mon, 25 May 2020 12:43:36 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id l204sm9254368wmf.19.2020.05.25.12.43.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 12:43:35 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 21:43:01 +0200 Message-Id: <20200525194302.24515-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200525194302.24515-1-andreas.rheinhardt@gmail.com> References: <20200525194302.24515-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avformat/hlsenc: Avoid duplicating strings when parsing X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, the HLS muxer uses av_strtok() to split an input string controlling parameters of the VariantStreams and then duplicates parts of this string containing parameters such as the language or the name of the VariantStream. But these parts are proper zero-terminated strings of their own that are never modified lateron, so one can simply use the substring as-is without creating a copy. This commit implements this. The same also happened for the string controlling the closed caption groups. Furthermore, add const to indicate that the pointers to these substrings are not used to modify them and also to indicate that these strings are not allocated on their own. Signed-off-by: Andreas Rheinhardt --- libavformat/hlsenc.c | 77 +++++++++++--------------------------------- 1 file changed, 19 insertions(+), 58 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 77e49b44b1..02bd5c3528 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -173,17 +173,17 @@ typedef struct VariantStream { unsigned int nb_streams; int m3u8_created; /* status of media play-list creation */ int is_default; /* default status of audio group */ - char *language; /* audio lauguage name */ - char *agroup; /* audio group name */ - char *sgroup; /* subtitle group name */ - char *ccgroup; /* closed caption group name */ - char *varname; // variant name + const char *language; /* audio lauguage name */ + const char *agroup; /* audio group name */ + const char *sgroup; /* subtitle group name */ + const char *ccgroup; /* closed caption group name */ + const char *varname; /* variant name */ } VariantStream; typedef struct ClosedCaptionsStream { - char *ccgroup; /* closed caption group name */ - char *instreamid; /* closed captions INSTREAM-ID */ - char *language; /* closed captions langauge */ + const char *ccgroup; /* closed caption group name */ + const char *instreamid; /* closed captions INSTREAM-ID */ + const char *language; /* closed captions langauge */ } ClosedCaptionsStream; typedef struct HLSContext { @@ -1290,8 +1290,8 @@ static int create_master_playlist(AVFormatContext *s, int ret, bandwidth; const char *m3u8_rel_name = NULL; const char *vtt_m3u8_rel_name = NULL; - char *ccgroup; - char *sgroup = NULL; + const char *ccgroup; + const char *sgroup = NULL; ClosedCaptionsStream *ccs; const char *proto = avio_find_protocol_name(hls->master_m3u8_url); int is_file_proto = proto && !strcmp(proto, "file"); @@ -1962,10 +1962,7 @@ static int parse_variant_stream_mapstring(AVFormatContext *s) char *end; varstr = NULL; if (av_strstart(keyval, "language:", &val)) { - av_free(vs->language); - vs->language = av_strdup(val); - if (!vs->language) - return AVERROR(ENOMEM); + vs->language = val; continue; } else if (av_strstart(keyval, "default:", &val)) { vs->is_default = (!av_strncasecmp(val, "YES", strlen("YES")) || @@ -1973,28 +1970,16 @@ static int parse_variant_stream_mapstring(AVFormatContext *s) hls->has_default_key = 1; continue; } else if (av_strstart(keyval, "name:", &val)) { - av_free(vs->varname); - vs->varname = av_strdup(val); - if (!vs->varname) - return AVERROR(ENOMEM); + vs->varname = val; continue; } else if (av_strstart(keyval, "agroup:", &val)) { - av_free(vs->agroup); - vs->agroup = av_strdup(val); - if (!vs->agroup) - return AVERROR(ENOMEM); + vs->agroup = val; continue; } else if (av_strstart(keyval, "sgroup:", &val)) { - av_free(vs->sgroup); - vs->sgroup = av_strdup(val); - if (!vs->sgroup) - return AVERROR(ENOMEM); + vs->sgroup = val; continue; } else if (av_strstart(keyval, "ccgroup:", &val)) { - av_free(vs->ccgroup); - vs->ccgroup = av_strdup(val); - if (!vs->ccgroup) - return AVERROR(ENOMEM); + vs->ccgroup = val; continue; } else if (av_strstart(keyval, "v:", &val)) { codec_type = AVMEDIA_TYPE_VIDEO; @@ -2085,20 +2070,11 @@ static int parse_cc_stream_mapstring(AVFormatContext *s) ccstr = NULL; if (av_strstart(keyval, "ccgroup:", &val)) { - av_free(ccs->ccgroup); - ccs->ccgroup = av_strdup(val); - if (!ccs->ccgroup) - return AVERROR(ENOMEM); + ccs->ccgroup = val; } else if (av_strstart(keyval, "instreamid:", &val)) { - av_free(ccs->instreamid); - ccs->instreamid = av_strdup(val); - if (!ccs->instreamid) - return AVERROR(ENOMEM); + ccs->instreamid = val; } else if (av_strstart(keyval, "language:", &val)) { - av_free(ccs->language); - ccs->language = av_strdup(val); - if (!ccs->language) - return AVERROR(ENOMEM); + ccs->language = val; } else { av_log(s, AV_LOG_ERROR, "Invalid keyval %s\n", keyval); return AVERROR(EINVAL); @@ -2162,9 +2138,7 @@ static int update_variant_stream_info(AVFormatContext *s) //by default, the first available ccgroup is mapped to the variant stream if (hls->nb_ccstreams) { - hls->var_streams[0].ccgroup = av_strdup(hls->cc_streams[0].ccgroup); - if (!hls->var_streams[0].ccgroup) - return AVERROR(ENOMEM); + hls->var_streams[0].ccgroup = hls->cc_streams[0].ccgroup; } for (i = 0; i < s->nb_streams; i++) @@ -2576,19 +2550,6 @@ static void hls_deinit(AVFormatContext *s) hls_free_segments(vs->old_segments); av_freep(&vs->m3u8_name); av_freep(&vs->streams); - av_freep(&vs->agroup); - av_freep(&vs->sgroup); - av_freep(&vs->language); - av_freep(&vs->ccgroup); - av_freep(&vs->varname); - } - - for (i = 0; i < hls->nb_ccstreams; i++) { - ClosedCaptionsStream *ccs = &hls->cc_streams[i]; - - av_freep(&ccs->ccgroup); - av_freep(&ccs->instreamid); - av_freep(&ccs->language); } ff_format_io_close(s, &hls->m3u8_out);