From patchwork Sat Jul 18 00:19:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21152 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 60A58449F78 for ; Sat, 18 Jul 2020 03:19:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 43B3D68B6C6; Sat, 18 Jul 2020 03:19:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f68.google.com (mail-ej1-f68.google.com [209.85.218.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CF98D68B69D for ; Sat, 18 Jul 2020 03:19:50 +0300 (EEST) Received: by mail-ej1-f68.google.com with SMTP id o18so12675797eje.7 for ; Fri, 17 Jul 2020 17:19:50 -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=DA2qZGrh4AY3BXVlCKaJCp/OaEOE3ix9Uah5GV9CFEc=; b=seXWn0o2NMNFOth9qH5pZi8chvo1//Wg+r/7kKWc+/EUMtj5DfiyDtYckODo7doVNU fbwGHaELHL5qiVD6BYOyCyVWtzMQUwSV46QwsUCL/KsAgQyqy76k1PkyJ+LQhDyxTAAV KzplLs0CRbo6VQt+KfbLnHDF0yhzs9aYI1G6Cqw/fF6wk2TjuGU3sb35/X58ddChgIJn U0SGJmEGNjVwJYLGZe8TmNpqLPqhjbHffOkudcn4JHnIN8gjwhaZwkg6/PSx3njexidZ ZNVt4QN2EN2OY72/l+vXc40wHO70gZLKxdcEkIiteNPjivZVs8iAfGsojR7S7jlz2F3r NJSw== 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=DA2qZGrh4AY3BXVlCKaJCp/OaEOE3ix9Uah5GV9CFEc=; b=ubsCC7psGHZZBNq7Ts5TylTDJ5LGZqoXdc0thcd0ZWDrCm6nQTdx4+lidkH6vhmpn8 6KeEQXVx9w1PuYZP/smVIpxM5OYnacngntrTtv00jYA1+MwtTXubQfa9QN5IDGWFXkya g+2Mn5AjXC/vdVnY5/6JEYFpS2HlhEkcynWzli1sxR5I6pFOFKXrxn38/jQyADHGvbtq b3Xltvr+IRhh4sBFLDD0xcEynanYHlqCZMwUFPd6fW9nfJ/VnnUGHtOFEyn8Blura+ZV 40WItfEnZtriwXLRrjjwPJtazkTOxuHGtSPtLJ5qfV5RKwg7GpwiG3fdUEkpaPWgDaMg CVkw== X-Gm-Message-State: AOAM532XZgltgGF0fBIBE9shAg4FHVfJresJYg4bZ2g9hRpIKSdusxyr aBMpcvhwo4sNyMNYEIaMbC/igSZl X-Google-Smtp-Source: ABdhPJwIzzWwiFFFYnGVesEufOOVRqITBw1xNtQ+su9ZKgExCWDhtF6m68CA/tFTBiOPPu5u0tevvQ== X-Received: by 2002:a17:906:dce:: with SMTP id p14mr10676016eji.442.1595031589658; Fri, 17 Jul 2020 17:19:49 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id y7sm9299001ejd.73.2020.07.17.17.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 17:19:49 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Jul 2020 02:19:21 +0200 Message-Id: <20200718001928.10603-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200718000636.2167-1-andreas.rheinhardt@gmail.com> References: <20200718000636.2167-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/11] avformat/webmdashenc: Cosmetics 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" Mostly using intermediate pointers for accesses (i.e. storing s->pb in a variable pb and then using pb for writing instead of s->pb) to improve readability. Furthermore, the opening brace '{' of a function has been moved into a line of its own in instances where it wasn't before. Signed-off-by: Andreas Rheinhardt --- libavformat/webmdashenc.c | 177 +++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 81 deletions(-) diff --git a/libavformat/webmdashenc.c b/libavformat/webmdashenc.c index 4d25d79846..04f8cbe39d 100644 --- a/libavformat/webmdashenc.c +++ b/libavformat/webmdashenc.c @@ -79,19 +79,20 @@ static double get_duration(AVFormatContext *s) static int write_header(AVFormatContext *s) { WebMDashMuxContext *w = s->priv_data; + AVIOContext *pb = s->pb; double min_buffer_time = 1.0; - avio_printf(s->pb, "\n"); - avio_printf(s->pb, "pb, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"); - avio_printf(s->pb, " xmlns=\"urn:mpeg:DASH:schema:MPD:2011\"\n"); - avio_printf(s->pb, " xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011\"\n"); - avio_printf(s->pb, " type=\"%s\"\n", w->is_live ? "dynamic" : "static"); + avio_printf(pb, "\n"); + avio_printf(pb, "is_live ? "dynamic" : "static"); if (!w->is_live) { - avio_printf(s->pb, " mediaPresentationDuration=\"PT%gS\"\n", + avio_printf(pb, " mediaPresentationDuration=\"PT%gS\"\n", get_duration(s)); } - avio_printf(s->pb, " minBufferTime=\"PT%gS\"\n", min_buffer_time); - avio_printf(s->pb, " profiles=\"%s\"%s", + avio_printf(pb, " minBufferTime=\"PT%gS\"\n", min_buffer_time); + avio_printf(pb, " profiles=\"%s\"%s", w->is_live ? "urn:mpeg:dash:profile:isoff-live:2011" : "urn:webm:dash:profile:webm-on-demand:2012", w->is_live ? "\n" : ">\n"); if (w->is_live) { @@ -105,14 +106,14 @@ static int write_header(AVFormatContext *s) if (s->flags & AVFMT_FLAG_BITEXACT) { av_strlcpy(gmt_iso, "", 1); } - avio_printf(s->pb, " availabilityStartTime=\"%s\"\n", gmt_iso); - avio_printf(s->pb, " timeShiftBufferDepth=\"PT%gS\"\n", w->time_shift_buffer_depth); - avio_printf(s->pb, " minimumUpdatePeriod=\"PT%dS\"", w->minimum_update_period); - avio_printf(s->pb, ">\n"); + avio_printf(pb, " availabilityStartTime=\"%s\"\n", gmt_iso); + avio_printf(pb, " timeShiftBufferDepth=\"PT%gS\"\n", w->time_shift_buffer_depth); + avio_printf(pb, " minimumUpdatePeriod=\"PT%dS\"", w->minimum_update_period); + avio_printf(pb, ">\n"); if (w->utc_timing_url) { - avio_printf(s->pb, "pb, " schemeIdUri=\"urn:mpeg:dash:utc:http-iso:2014\"\n"); - avio_printf(s->pb, " value=\"%s\"/>\n", w->utc_timing_url); + avio_printf(pb, "\n", w->utc_timing_url); } } return 0; @@ -123,7 +124,8 @@ static void write_footer(AVFormatContext *s) avio_printf(s->pb, "\n"); } -static int subsegment_alignment(AVFormatContext *s, AdaptationSet *as) { +static int subsegment_alignment(AVFormatContext *s, const AdaptationSet *as) +{ int i; AVDictionaryEntry *gold = av_dict_get(s->streams[as->streams[0]]->metadata, CUE_TIMESTAMPS, NULL, 0); @@ -136,16 +138,19 @@ static int subsegment_alignment(AVFormatContext *s, AdaptationSet *as) { return 1; } -static int bitstream_switching(AVFormatContext *s, AdaptationSet *as) { +static int bitstream_switching(AVFormatContext *s, const AdaptationSet *as) +{ int i; - AVDictionaryEntry *gold_track_num = av_dict_get(s->streams[as->streams[0]]->metadata, + const AVStream *gold_st = s->streams[as->streams[0]]; + AVDictionaryEntry *gold_track_num = av_dict_get(gold_st->metadata, TRACK_NUMBER, NULL, 0); - AVCodecParameters *gold_par = s->streams[as->streams[0]]->codecpar; + AVCodecParameters *gold_par = gold_st->codecpar; if (!gold_track_num) return 0; for (i = 1; i < as->nb_streams; i++) { - AVDictionaryEntry *track_num = av_dict_get(s->streams[as->streams[i]]->metadata, + const AVStream *st = s->streams[as->streams[i]]; + AVDictionaryEntry *track_num = av_dict_get(st->metadata, TRACK_NUMBER, NULL, 0); - AVCodecParameters *par = s->streams[as->streams[i]]->codecpar; + AVCodecParameters *par = st->codecpar; if (!track_num || strncmp(gold_track_num->value, track_num->value, strlen(gold_track_num->value)) || gold_par->codec_id != par->codec_id || @@ -161,61 +166,65 @@ static int bitstream_switching(AVFormatContext *s, AdaptationSet *as) { * Writes a Representation within an Adaptation Set. Returns 0 on success and * < 0 on failure. */ -static int write_representation(AVFormatContext *s, AVStream *stream, char *id, +static int write_representation(AVFormatContext *s, AVStream *st, char *id, int output_width, int output_height, - int output_sample_rate) { + int output_sample_rate) +{ WebMDashMuxContext *w = s->priv_data; - AVDictionaryEntry *bandwidth = av_dict_get(stream->metadata, BANDWIDTH, NULL, 0); + AVIOContext *pb = s->pb; + const AVCodecParameters *par = st->codecpar; + AVDictionaryEntry *bandwidth = av_dict_get(st->metadata, BANDWIDTH, NULL, 0); const char *bandwidth_str; - avio_printf(s->pb, "value; } else if (w->is_live) { // if bandwidth for live was not provided, use a default - bandwidth_str = (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? "128000" : "1000000"; + bandwidth_str = (par->codec_type == AVMEDIA_TYPE_AUDIO) ? "128000" : "1000000"; } else { return AVERROR(EINVAL); } - avio_printf(s->pb, " bandwidth=\"%s\"", bandwidth_str); - if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && output_width) - avio_printf(s->pb, " width=\"%d\"", stream->codecpar->width); - if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && output_height) - avio_printf(s->pb, " height=\"%d\"", stream->codecpar->height); - if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && output_sample_rate) - avio_printf(s->pb, " audioSamplingRate=\"%d\"", stream->codecpar->sample_rate); + avio_printf(pb, " bandwidth=\"%s\"", bandwidth_str); + if (par->codec_type == AVMEDIA_TYPE_VIDEO && output_width) + avio_printf(pb, " width=\"%d\"", par->width); + if (par->codec_type == AVMEDIA_TYPE_VIDEO && output_height) + avio_printf(pb, " height=\"%d\"", par->height); + if (par->codec_type == AVMEDIA_TYPE_AUDIO && output_sample_rate) + avio_printf(pb, " audioSamplingRate=\"%d\"", par->sample_rate); if (w->is_live) { // For live streams, Codec and Mime Type always go in the Representation tag. - avio_printf(s->pb, " codecs=\"%s\"", get_codec_name(stream->codecpar->codec_id)); - avio_printf(s->pb, " mimeType=\"%s/webm\"", - stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio"); + avio_printf(pb, " codecs=\"%s\"", get_codec_name(par->codec_id)); + avio_printf(pb, " mimeType=\"%s/webm\"", + par->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio"); // For live streams, subsegments always start with key frames. So this // is always 1. - avio_printf(s->pb, " startsWithSAP=\"1\""); - avio_printf(s->pb, ">"); + avio_printf(pb, " startsWithSAP=\"1\""); + avio_printf(pb, ">"); } else { - AVDictionaryEntry *irange = av_dict_get(stream->metadata, INITIALIZATION_RANGE, NULL, 0); - AVDictionaryEntry *cues_start = av_dict_get(stream->metadata, CUES_START, NULL, 0); - AVDictionaryEntry *cues_end = av_dict_get(stream->metadata, CUES_END, NULL, 0); - AVDictionaryEntry *filename = av_dict_get(stream->metadata, FILENAME, NULL, 0); + AVDictionaryEntry *irange = av_dict_get(st->metadata, INITIALIZATION_RANGE, NULL, 0); + AVDictionaryEntry *cues_start = av_dict_get(st->metadata, CUES_START, NULL, 0); + AVDictionaryEntry *cues_end = av_dict_get(st->metadata, CUES_END, NULL, 0); + AVDictionaryEntry *filename = av_dict_get(st->metadata, FILENAME, NULL, 0); if (!irange || !cues_start || !cues_end || !filename) return AVERROR(EINVAL); - avio_printf(s->pb, ">\n"); - avio_printf(s->pb, "%s\n", filename->value); - avio_printf(s->pb, "pb, " indexRange=\"%s-%s\">\n", cues_start->value, cues_end->value); - avio_printf(s->pb, "pb, " range=\"0-%s\" />\n", irange->value); - avio_printf(s->pb, "\n"); + avio_printf(pb, ">\n"); + avio_printf(pb, "%s\n", filename->value); + avio_printf(pb, "\n", cues_start->value, cues_end->value); + avio_printf(pb, "\n", irange->value); + avio_printf(pb, "\n"); } - avio_printf(s->pb, "\n"); + avio_printf(pb, "\n"); return 0; } /* * Checks if width of all streams are the same. Returns 1 if true, 0 otherwise. */ -static int check_matching_width(AVFormatContext *s, AdaptationSet *as) { +static int check_matching_width(AVFormatContext *s, const AdaptationSet *as) +{ int first_width, i; if (as->nb_streams < 2) return 1; first_width = s->streams[as->streams[0]]->codecpar->width; @@ -228,7 +237,8 @@ static int check_matching_width(AVFormatContext *s, AdaptationSet *as) { /* * Checks if height of all streams are the same. Returns 1 if true, 0 otherwise. */ -static int check_matching_height(AVFormatContext *s, AdaptationSet *as) { +static int check_matching_height(AVFormatContext *s, const AdaptationSet *as) +{ int first_height, i; if (as->nb_streams < 2) return 1; first_height = s->streams[as->streams[0]]->codecpar->height; @@ -241,7 +251,8 @@ static int check_matching_height(AVFormatContext *s, AdaptationSet *as) { /* * Checks if sample rate of all streams are the same. Returns 1 if true, 0 otherwise. */ -static int check_matching_sample_rate(AVFormatContext *s, AdaptationSet *as) { +static int check_matching_sample_rate(AVFormatContext *s, const AdaptationSet *as) +{ int first_sample_rate, i; if (as->nb_streams < 2) return 1; first_sample_rate = s->streams[as->streams[0]]->codecpar->sample_rate; @@ -251,7 +262,8 @@ static int check_matching_sample_rate(AVFormatContext *s, AdaptationSet *as) { return 1; } -static void free_adaptation_sets(AVFormatContext *s) { +static void free_adaptation_sets(AVFormatContext *s) +{ WebMDashMuxContext *w = s->priv_data; int i; for (i = 0; i < w->nb_as; i++) { @@ -289,8 +301,10 @@ static int write_adaptation_set(AVFormatContext *s, int as_index) { WebMDashMuxContext *w = s->priv_data; AdaptationSet *as = &w->as[as_index]; - AVCodecParameters *par = s->streams[as->streams[0]]->codecpar; + const AVStream *st = s->streams[as->streams[0]]; + AVCodecParameters *par = st->codecpar; AVDictionaryEntry *lang; + AVIOContext *pb = s->pb; int i; static const char boolean[2][6] = { "false", "true" }; int subsegmentStartsWithSAP = 1; @@ -301,30 +315,31 @@ static int write_adaptation_set(AVFormatContext *s, int as_index) // in the Representation tag. int width_in_as = 1, height_in_as = 1, sample_rate_in_as = 1; if (par->codec_type == AVMEDIA_TYPE_VIDEO) { - width_in_as = !w->is_live && check_matching_width(s, as); + width_in_as = !w->is_live && check_matching_width (s, as); height_in_as = !w->is_live && check_matching_height(s, as); } else { sample_rate_in_as = !w->is_live && check_matching_sample_rate(s, as); } - avio_printf(s->pb, "id); - avio_printf(s->pb, " mimeType=\"%s/webm\"", + avio_printf(pb, "id); + avio_printf(pb, " mimeType=\"%s/webm\"", par->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio"); - avio_printf(s->pb, " codecs=\"%s\"", get_codec_name(par->codec_id)); + avio_printf(pb, " codecs=\"%s\"", get_codec_name(par->codec_id)); - lang = av_dict_get(s->streams[as->streams[0]]->metadata, "language", NULL, 0); - if (lang) avio_printf(s->pb, " lang=\"%s\"", lang->value); + lang = av_dict_get(st->metadata, "language", NULL, 0); + if (lang) + avio_printf(pb, " lang=\"%s\"", lang->value); if (par->codec_type == AVMEDIA_TYPE_VIDEO && width_in_as) - avio_printf(s->pb, " width=\"%d\"", par->width); + avio_printf(pb, " width=\"%d\"", par->width); if (par->codec_type == AVMEDIA_TYPE_VIDEO && height_in_as) - avio_printf(s->pb, " height=\"%d\"", par->height); + avio_printf(pb, " height=\"%d\"", par->height); if (par->codec_type == AVMEDIA_TYPE_AUDIO && sample_rate_in_as) - avio_printf(s->pb, " audioSamplingRate=\"%d\"", par->sample_rate); + avio_printf(pb, " audioSamplingRate=\"%d\"", par->sample_rate); - avio_printf(s->pb, " bitstreamSwitching=\"%s\"", + avio_printf(pb, " bitstreamSwitching=\"%s\"", boolean[bitstream_switching(s, as)]); - avio_printf(s->pb, " subsegmentAlignment=\"%s\"", + avio_printf(pb, " subsegmentAlignment=\"%s\"", boolean[w->is_live || subsegment_alignment(s, as)]); for (i = 0; i < as->nb_streams; i++) { @@ -332,12 +347,12 @@ static int write_adaptation_set(AVFormatContext *s, int as_index) CLUSTER_KEYFRAME, NULL, 0); if (!w->is_live && (!kf || !strncmp(kf->value, "0", 1))) subsegmentStartsWithSAP = 0; } - avio_printf(s->pb, " subsegmentStartsWithSAP=\"%d\"", subsegmentStartsWithSAP); - avio_printf(s->pb, ">\n"); + avio_printf(pb, " subsegmentStartsWithSAP=\"%d\"", subsegmentStartsWithSAP); + avio_printf(pb, ">\n"); if (w->is_live) { AVDictionaryEntry *filename = - av_dict_get(s->streams[as->streams[0]]->metadata, FILENAME, NULL, 0); + av_dict_get(st->metadata, FILENAME, NULL, 0); char *underscore_pos, *period_pos; int ret; if (!filename) @@ -345,26 +360,27 @@ static int write_adaptation_set(AVFormatContext *s, int as_index) ret = split_filename(filename->value, &underscore_pos, &period_pos); if (ret) return ret; *underscore_pos = '\0'; - avio_printf(s->pb, "\n", + avio_printf(pb, "\n", par->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio"); - avio_printf(s->pb, "pb, " timescale=\"1000\""); - avio_printf(s->pb, " duration=\"%d\"", w->chunk_duration); - avio_printf(s->pb, " media=\"%s_$RepresentationID$_$Number$.chk\"", + avio_printf(pb, "chunk_duration); + avio_printf(pb, " media=\"%s_$RepresentationID$_$Number$.chk\"", filename->value); - avio_printf(s->pb, " startNumber=\"%d\"", w->chunk_start_index); - avio_printf(s->pb, " initialization=\"%s_$RepresentationID$.hdr\"", + avio_printf(pb, " startNumber=\"%d\"", w->chunk_start_index); + avio_printf(pb, " initialization=\"%s_$RepresentationID$.hdr\"", filename->value); - avio_printf(s->pb, "/>\n"); + avio_printf(pb, "/>\n"); *underscore_pos = '_'; } for (i = 0; i < as->nb_streams; i++) { char buf[25], *representation_id = buf, *underscore_pos, *period_pos; + AVStream *st = s->streams[as->streams[i]]; int ret; if (w->is_live) { AVDictionaryEntry *filename = - av_dict_get(s->streams[as->streams[i]]->metadata, FILENAME, NULL, 0); + av_dict_get(st->metadata, FILENAME, NULL, 0); if (!filename) return AVERROR(EINVAL); ret = split_filename(filename->value, &underscore_pos, &period_pos); @@ -375,8 +391,7 @@ static int write_adaptation_set(AVFormatContext *s, int as_index) } else { snprintf(buf, sizeof(buf), "%d", w->representation_id++); } - ret = write_representation(s, s->streams[as->streams[i]], - representation_id, !width_in_as, + ret = write_representation(s, st, representation_id, !width_in_as, !height_in_as, !sample_rate_in_as); if (ret) return ret; if (w->is_live)