[FFmpeg-devel] [PATCH] libavformat/dashdec: Avoid multiple HTTP requests for initialization segment that is common among all representations

sanilraut raut.sanil at gmail.com
Mon Apr 9 10:04:26 EEST 2018


For example:

In the following mpd, initialization segment ( <SegmentTemplate
initialization="test_init.mp4"/> )  is common for all the representations.
As seen in the screen shot (BeforePatch_Sintel_1), there are multiple HTTP
requests for the same initialization segment. After applying the patch,
there is only one request for the initialization segment
(AfterPatch_Sintel_1).

<?xml version="1.0"?>
<!-- MPD file Generated with GPAC version 0.5.2-DEV-revVersion:
0.5.2-426-gc5ad4e4+dfsg5-1build1  at 2017-09-09T18:21:44.557Z-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500S"
type="static" mediaPresentationDuration="PT0H0M52.208S"
maxSegmentDuration="PT0H0M4.000S" profiles="urn:mpeg:dash:profil
e:isoff-live:2011">
 <ProgramInformation moreInformationURL="http://gpac.sourceforge.net">
  <Title>test.mpd generated by GPAC</Title>
 </ProgramInformation>

 <Period duration="PT0H0M52.208S">
  <AdaptationSet segmentAlignment="true" bitstreamSwitching="true"
maxWidth="1280" maxHeight="720" maxFrameRate="24" par="16:9" lang="und">
   <SegmentTemplate initialization="test_init.mp4"/>
   <Representation id="1" mimeType="video/mp4" codecs="avc3.4d401f"
width="1280" height="720" frameRate="24" sar="1:1" startWithSAP="1"
bandwidth="2291892">
    <SegmentTemplate timescale="96" media="Sintel-1280x720_dash$Number$.m4s"
startNumber="1" duration="384"/>
   </Representation>
   <Representation id="2" mimeType="video/mp4" codecs="avc3.4d401f"
width="1024" height="576" frameRate="24" sar="1:1" startWithSAP="1"
bandwidth="1635644">
    <SegmentTemplate timescale="96" media="Sintel-1024x576_dash$Number$.m4s"
startNumber="1" duration="384"/>
   </Representation>
   <Representation id="3" mimeType="video/mp4" codecs="avc3.4d401f"
width="768" height="432" frameRate="24" sar="1:1" startWithSAP="1"
bandwidth="1054520">
    <SegmentTemplate timescale="96" media="Sintel-768x432_dash$Number$.m4s"
startNumber="1" duration="384"/>
   </Representation>
   <Representation id="4" mimeType="video/mp4" codecs="avc3.4d401f"
width="640" height="360" frameRate="24" sar="1:1" startWithSAP="1"
bandwidth="662598">
    <SegmentTemplate timescale="96" media="Sintel-640x360_dash$Number$.m4s"
startNumber="1" duration="384"/>
   </Representation>
   <Representation id="5" mimeType="video/mp4" codecs="avc3.4d401f"
width="512" height="288" frameRate="24" sar="1:1" startWithSAP="1"
bandwidth="441780">
    <SegmentTemplate timescale="96" media="Sintel-512x288_dash$Number$.m4s"
startNumber="1" duration="384"/>
   </Representation>
  </AdaptationSet>
 </Period>
</MPD>


On Mon, Apr 9, 2018 at 12:02 AM, sanilraut <raut.sanil at gmail.com> wrote:

> Hi,
>
> The following patch avoid multiple HTTP requests for initialization
> segment that is common among all representations.
>
> ---
>  libavformat/dashdec.c | 96 ++++++++++++++++++++++++++++++
> ++++++++++++++-------
>  1 file changed, 83 insertions(+), 13 deletions(-)
>
> diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
> index 8bfde4d..4d0445f 100644
> --- a/libavformat/dashdec.c
> +++ b/libavformat/dashdec.c
> @@ -149,6 +149,11 @@ typedef struct DASHContext {
>      char *allowed_extensions;
>      AVDictionary *avio_opts;
>      int max_url_size;
> +
> +    /* Flags for init section*/
> +    int is_init_section_common_video;
> +    int is_init_section_common_audio;
> +
>  } DASHContext;
>
>  static int ishttp(char *url)
> @@ -416,9 +421,9 @@ static int open_url(AVFormatContext *s, AVIOContext
> **pb, const char *url,
>      if (av_strstart(proto_name, "file", NULL)) {
>          if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url,
> c->allowed_extensions)) {
>              av_log(s, AV_LOG_ERROR,
> -                "Filename extension of \'%s\' is not a common multimedia
> extension, blocked for security reasons.\n"
> -                "If you wish to override this adjust allowed_extensions,
> you can set it to \'ALL\' to allow all\n",
> -                url);
> +                   "Filename extension of \'%s\' is not a common
> multimedia extension, blocked for security reasons.\n"
> +                   "If you wish to override this adjust
> allowed_extensions, you can set it to \'ALL\' to allow all\n",
> +                   url);
>              return AVERROR_INVALIDDATA;
>          }
>      } else if (av_strstart(proto_name, "http", NULL)) {
> @@ -931,7 +936,7 @@ static int parse_manifest_representation(AVFormatContext
> *s, const char *url,
>                          rep->last_seq_no =(int64_t) strtoll(val, NULL,
> 10) - 1;
>                          xmlFree(val);
>                      }
> -                 }
> +                }
>              }
>
>              fragment_timeline_node = find_child_node_by_name(
> representation_segmenttemplate_node, "SegmentTimeline");
> @@ -1160,7 +1165,7 @@ static int parse_manifest(AVFormatContext *s, const
> char *url, AVIOContext *in)
>      } else {
>          LIBXML_TEST_VERSION
>
> -        doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
> +            doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
>          root_element = xmlDocGetRootElement(doc);
>          node = root_element;
>
> @@ -1396,14 +1401,14 @@ static int refresh_manifest(AVFormatContext *s)
>
>      if (c->n_videos != n_videos) {
>          av_log(c, AV_LOG_ERROR,
> -            "new manifest has mismatched no. of video representations, %d
> -> %d\n",
> -            n_videos, c->n_videos);
> +               "new manifest has mismatched no. of video representations,
> %d -> %d\n",
> +               n_videos, c->n_videos);
>          return AVERROR_INVALIDDATA;
>      }
>      if (c->n_audios != n_audios) {
>          av_log(c, AV_LOG_ERROR,
> -            "new manifest has mismatched no. of audio representations, %d
> -> %d\n",
> -            n_audios, c->n_audios);
> +               "new manifest has mismatched no. of audio representations,
> %d -> %d\n",
> +               n_audios, c->n_audios);
>          return AVERROR_INVALIDDATA;
>      }
>
> @@ -1862,6 +1867,45 @@ fail:
>      return ret;
>  }
>
> +static int init_section_compare_video(DASHContext *c)
> +{
> +    int i = 0;
> +    char *url = c->videos[0]->init_section->url;
> +    int64_t url_offset = c->videos[0]->init_section->url_offset;
> +    int64_t size = c->videos[0]->init_section->size;
> +    for (i=0;i<c->n_videos;i++) {
> +        if (av_strcasecmp(c->videos[i]->init_section->url,url) ||
> c->videos[i]->init_section->url_offset != url_offset ||
> c->videos[i]->init_section->size != size) {
> +            return 0;
> +        }
> +    }
> +    return 1;
> +}
> +
> +static int init_section_compare_audio(DASHContext *c)
> +{
> +    int i = 0;
> +    char *url = c->audios[0]->init_section->url;
> +    int64_t url_offset = c->audios[0]->init_section->url_offset;
> +    int64_t size = c->audios[0]->init_section->size;
> +    for (i=0;i<c->n_audios;i++) {
> +        if (av_strcasecmp(c->audios[i]->init_section->url,url) ||
> c->audios[i]->init_section->url_offset != url_offset ||
> c->audios[i]->init_section->size != size) {
> +            return 0;
> +        }
> +    }
> +    return 1;
> +}
> +
> +static void copy_init_section(struct representation *rep_dest, struct
> representation *rep_src)
> +{
> +    memcpy(rep_dest->init_section, rep_src->init_section,
> sizeof(rep_src->init_section));
> +    rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
> +    memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf,
> rep_src->init_sec_data_len);
> +    rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
> +    rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
> +    rep_dest->cur_timestamp = rep_src->cur_timestamp;
> +}
> +
> +
>  static int dash_read_header(AVFormatContext *s)
>  {
>      void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
> @@ -1890,19 +1934,45 @@ static int dash_read_header(AVFormatContext *s)
>          s->duration = (int64_t) c->media_presentation_duration *
> AV_TIME_BASE;
>      }
>
> +    if (c->n_videos != 0) {
> +        c->is_init_section_common_video = init_section_compare_video(c);
> +    }
> +
>      /* Open the demuxer for video and audio components if available */
>      for (i = 0; i < c->n_videos; i++) {
>          struct representation *cur_video = c->videos[i];
> -        ret = open_demux_for_component(s, cur_video);
> +        if (!c->is_init_section_common_video) {
> +            ret = open_demux_for_component(s, cur_video);
> +        } else {
> +            if (i == 0) {
> +                ret = open_demux_for_component(s, cur_video);
> +            } else {
> +                copy_init_section(cur_video,c->videos[0]);
> +                ret = open_demux_for_component(s, cur_video);
> +            }
> +        }
>          if (ret)
>              goto fail;
>          cur_video->stream_index = stream_index;
>          ++stream_index;
>      }
>
> +    if (c->n_audios != 0) {
> +        c->is_init_section_common_audio = init_section_compare_audio(c);
> +    }
> +
>      for (i = 0; i < c->n_audios; i++) {
>          struct representation *cur_audio = c->audios[i];
> -        ret = open_demux_for_component(s, cur_audio);
> +        if (!c->is_init_section_common_audio) {
> +            ret = open_demux_for_component(s, cur_audio);
> +        } else {
> +            if (i == 0) {
> +                ret = open_demux_for_component(s, cur_audio);
> +            } else {
> +                copy_init_section(cur_audio,c->audios[0]);
> +                ret = open_demux_for_component(s, cur_audio);
> +            }
> +        }
>          if (ret)
>              goto fail;
>          cur_audio->stream_index = stream_index;
> @@ -1931,7 +2001,7 @@ static int dash_read_header(AVFormatContext *s)
>                  av_dict_set_int(&pls->assoc_stream->metadata,
> "variant_bitrate", pls->bandwidth, 0);
>              if (pls->id[0])
>                  av_dict_set(&pls->assoc_stream->metadata, "id", pls->id,
> 0);
> -         }
> +        }
>          for (i = 0; i < c->n_audios; i++) {
>              struct representation *pls = c->audios[i];
>
> @@ -2048,7 +2118,7 @@ static int dash_seek(AVFormatContext *s, struct
> representation *pls, int64_t see
>      int64_t duration = 0;
>
>      av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms],
> playlist %d%s\n",
> -            seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
> +           seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
>
>      // single fragment mode
>      if (pls->n_fragments == 1) {
> --
>
> Thanks
>
> 2.7.4
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: AfterPatch_Sintel_1.png
Type: image/png
Size: 191740 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20180409/2885e9b5/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BeforePatch_Sintel_1.png
Type: image/png
Size: 206917 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20180409/2885e9b5/attachment-0001.png>


More information about the ffmpeg-devel mailing list