static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration,

                              int64_t pos, int64_t size)

{

    HLSSegment *en = av_malloc(sizeof(*en));

    const char  *filename;

    int ret;



    if (!en)

        return AVERROR(ENOMEM);



    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&

        strlen(hls->current_segment_final_filename_fmt)) {

        char * old_filename = av_strdup(hls->avf->filename);  // %%s will be %s after strftime

        av_strlcpy(hls->avf->filename, hls->current_segment_final_filename_fmt, sizeof(hls->avf->filename));

        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {

            char * filename = av_strdup(hls->avf->filename);  // %%s will be %s after strftime

            if (!filename)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),

                filename, 's', pos + size) < 1) {

                av_log(hls, AV_LOG_ERROR,

                       "Invalid second level segment filename template '%s', "

                        "you can try to remove second_level_segment_size flag\n",

                       filename);

                av_free(filename);

                av_free(old_filename);

                return AVERROR(EINVAL);

            }

            av_free(filename);

        }

        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {

            char * filename = av_strdup(hls->avf->filename);  // %%t will be %t after strftime

            if (!filename)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),

                filename, 't',  (int64_t)round(1000000 * duration)) < 1) {

                av_log(hls, AV_LOG_ERROR,

                       "Invalid second level segment filename template '%s', "

                        "you can try to remove second_level_segment_time flag\n",

                       filename);

                av_free(filename);

                av_free(old_filename);

                return AVERROR(EINVAL);

            }

            av_free(filename);

        }

        ff_rename(old_filename, hls->avf->filename, hls);

        av_free(old_filename);

    }





    filename = av_basename(hls->avf->filename);



    if (hls->use_localtime_mkdir) {

        filename = hls->avf->filename;

    }

    if (find_segment_by_filename(hls->segments, filename)

        || find_segment_by_filename(hls->old_segments, filename)) {

        av_log(hls, AV_LOG_WARNING, "Duplicated segment filename detected: %s\n", filename);

    }

    av_strlcpy(en->filename, filename, sizeof(en->filename));



    if(hls->has_subtitle)

        av_strlcpy(en->sub_filename, av_basename(hls->vtt_avf->filename), sizeof(en->sub_filename));

    else

        en->sub_filename[0] = '\0';



    en->duration = duration;

    en->pos      = pos;

    en->size     = size;

    en->next     = NULL;

    en->discont  = 0;



    if (hls->discontinuity) {

        en->discont = 1;

        hls->discontinuity = 0;

    }



    if (hls->key_info_file) {

        av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));

        av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));

    }



    if (!hls->segments)

        hls->segments = en;

    else

        hls->last_segment->next = en;



    hls->last_segment = en;



    // EVENT or VOD playlists imply sliding window cannot be used

    if (hls->pl_type != PLAYLIST_TYPE_NONE)

        hls->max_nb_segments = 0;



    if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {

        en = hls->segments;

        hls->initial_prog_date_time += en->duration;

        hls->segments = en->next;

        if (en && hls->flags & HLS_DELETE_SEGMENTS &&

                !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {

            en->next = hls->old_segments;

            hls->old_segments = en;

            if ((ret = hls_delete_old_segments(hls)) < 0)

                return ret;

        } else

            av_free(en);

    } else

        hls->nb_entries++;



    if (hls->max_seg_size > 0) {

        return 0;

    }

    hls->sequence++;



    return 0;

}

static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration,

                              int64_t pos, int64_t size)

{

    HLSSegment *en = av_malloc(sizeof(*en));

    const char  *filename;

    int ret;



    if (!en)

        return AVERROR(ENOMEM);



    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&

        strlen(hls->current_segment_final_filename_fmt)) {

        char * old_filename = av_strdup(hls->avf->filename);  

        av_strlcpy(hls->avf->filename, hls->current_segment_final_filename_fmt, sizeof(hls->avf->filename));

        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {

            char * filename = av_strdup(hls->avf->filename);  

            if (!filename)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),

                filename, 's', pos + size) < 1) {

                av_log(hls, AV_LOG_ERROR,

                       "Invalid second level segment filename template '%s', "

                        "you can try to remove second_level_segment_size flag\n",

                       filename);

                av_free(filename);

                av_free(old_filename);

                return AVERROR(EINVAL);

            }

            av_free(filename);

        }

        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {

            char * filename = av_strdup(hls->avf->filename);  

            if (!filename)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),

                filename, 't',  (int64_t)round(1000000 * duration)) < 1) {

                av_log(hls, AV_LOG_ERROR,

                       "Invalid second level segment filename template '%s', "

                        "you can try to remove second_level_segment_time flag\n",

                       filename);

                av_free(filename);

                av_free(old_filename);

                return AVERROR(EINVAL);

            }

            av_free(filename);

        }

        ff_rename(old_filename, hls->avf->filename, hls);

        av_free(old_filename);

    }





    filename = av_basename(hls->avf->filename);



    if (hls->use_localtime_mkdir) {

        filename = hls->avf->filename;

    }

    if (find_segment_by_filename(hls->segments, filename)

        || find_segment_by_filename(hls->old_segments, filename)) {

        av_log(hls, AV_LOG_WARNING, "Duplicated segment filename detected: %s\n", filename);

    }

    av_strlcpy(en->filename, filename, sizeof(en->filename));



    if(hls->has_subtitle)

        av_strlcpy(en->sub_filename, av_basename(hls->vtt_avf->filename), sizeof(en->sub_filename));

    else

        en->sub_filename[0] = '\0';



    en->duration = duration;

    en->pos      = pos;

    en->size     = size;

    en->next     = NULL;

    en->discont  = 0;



    if (hls->discontinuity) {

        en->discont = 1;

        hls->discontinuity = 0;

    }



    if (hls->key_info_file) {

        av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));

        av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));

    }



    if (!hls->segments)

        hls->segments = en;

    else

        hls->last_segment->next = en;



    hls->last_segment = en;



    

    if (hls->pl_type != PLAYLIST_TYPE_NONE)

        hls->max_nb_segments = 0;



    if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {

        en = hls->segments;

        hls->initial_prog_date_time += en->duration;

        hls->segments = en->next;

        if (en && hls->flags & HLS_DELETE_SEGMENTS &&

                !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {

            en->next = hls->old_segments;

            hls->old_segments = en;

            if ((ret = hls_delete_old_segments(hls)) < 0)

                return ret;

        } else

            av_free(en);

    } else

        hls->nb_entries++;



    if (hls->max_seg_size > 0) {

        return 0;

    }

    hls->sequence++;



    return 0;

}

            if (!filename)             if (!filename)
static int FUNC_0(struct AVFormatContext *VAR_0, HLSContext *VAR_1, double VAR_2,

                              int64_t VAR_3, int64_t VAR_4)

{

    HLSSegment *en = av_malloc(sizeof(*en));

    const char  *VAR_8;

    int VAR_6;



    if (!en)

        return AVERROR(ENOMEM);



    if ((VAR_1->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&

        strlen(VAR_1->current_segment_final_filename_fmt)) {

        char * VAR_7 = av_strdup(VAR_1->avf->VAR_8);  

        av_strlcpy(VAR_1->avf->VAR_8, VAR_1->current_segment_final_filename_fmt, sizeof(VAR_1->avf->VAR_8));

        if (VAR_1->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {

            char * VAR_8 = av_strdup(VAR_1->avf->VAR_8);  

            if (!VAR_8)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(VAR_1->avf->VAR_8, sizeof(VAR_1->avf->VAR_8),

                VAR_8, 'VAR_0', VAR_3 + VAR_4) < 1) {

                av_log(VAR_1, AV_LOG_ERROR,

                       "Invalid second level segment VAR_8 template '%VAR_0', "

                        "you can try to remove second_level_segment_size flag\n",

                       VAR_8);

                av_free(VAR_8);

                av_free(VAR_7);

                return AVERROR(EINVAL);

            }

            av_free(VAR_8);

        }

        if (VAR_1->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {

            char * VAR_8 = av_strdup(VAR_1->avf->VAR_8);  

            if (!VAR_8)

                return AVERROR(ENOMEM);

            if (replace_int_data_in_filename(VAR_1->avf->VAR_8, sizeof(VAR_1->avf->VAR_8),

                VAR_8, 't',  (int64_t)round(1000000 * VAR_2)) < 1) {

                av_log(VAR_1, AV_LOG_ERROR,

                       "Invalid second level segment VAR_8 template '%VAR_0', "

                        "you can try to remove second_level_segment_time flag\n",

                       VAR_8);

                av_free(VAR_8);

                av_free(VAR_7);

                return AVERROR(EINVAL);

            }

            av_free(VAR_8);

        }

        ff_rename(VAR_7, VAR_1->avf->VAR_8, VAR_1);

        av_free(VAR_7);

    }





    VAR_8 = av_basename(VAR_1->avf->VAR_8);



    if (VAR_1->use_localtime_mkdir) {

        VAR_8 = VAR_1->avf->VAR_8;

    }

    if (find_segment_by_filename(VAR_1->segments, VAR_8)

        || find_segment_by_filename(VAR_1->old_segments, VAR_8)) {

        av_log(VAR_1, AV_LOG_WARNING, "Duplicated segment VAR_8 detected: %VAR_0\n", VAR_8);

    }

    av_strlcpy(en->VAR_8, VAR_8, sizeof(en->VAR_8));



    if(VAR_1->has_subtitle)

        av_strlcpy(en->sub_filename, av_basename(VAR_1->vtt_avf->VAR_8), sizeof(en->sub_filename));

    else

        en->sub_filename[0] = '\0';



    en->VAR_2 = VAR_2;

    en->VAR_3      = VAR_3;

    en->VAR_4     = VAR_4;

    en->next     = NULL;

    en->discont  = 0;



    if (VAR_1->discontinuity) {

        en->discont = 1;

        VAR_1->discontinuity = 0;

    }



    if (VAR_1->key_info_file) {

        av_strlcpy(en->key_uri, VAR_1->key_uri, sizeof(en->key_uri));

        av_strlcpy(en->iv_string, VAR_1->iv_string, sizeof(en->iv_string));

    }



    if (!VAR_1->segments)

        VAR_1->segments = en;

    else

        VAR_1->last_segment->next = en;



    VAR_1->last_segment = en;



    

    if (VAR_1->pl_type != PLAYLIST_TYPE_NONE)

        VAR_1->max_nb_segments = 0;



    if (VAR_1->max_nb_segments && VAR_1->nb_entries >= VAR_1->max_nb_segments) {

        en = VAR_1->segments;

        VAR_1->initial_prog_date_time += en->VAR_2;

        VAR_1->segments = en->next;

        if (en && VAR_1->flags & HLS_DELETE_SEGMENTS &&

                !(VAR_1->flags & HLS_SINGLE_FILE || VAR_1->wrap)) {

            en->next = VAR_1->old_segments;

            VAR_1->old_segments = en;

            if ((VAR_6 = hls_delete_old_segments(VAR_1)) < 0)

                return VAR_6;

        } else

            av_free(en);

    } else

        VAR_1->nb_entries++;



    if (VAR_1->max_seg_size > 0) {

        return 0;

    }

    VAR_1->sequence++;



    return 0;

}

static int FUNC_0(struct AVFormatContext *VAR_0, HLSContext *VAR_1, double VAR_2,
int64_t VAR_3, int64_t VAR_4)
{ HLSSegment *en = av_malloc(sizeof(*en)); const char  *VAR_8; int VAR_6; if (!en)
return AVERROR(ENOMEM); if ((VAR_1->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
strlen(VAR_1->current_segment_final_filename_fmt)) { char * VAR_7 = av_strdup(VAR_1->avf->VAR_8); av_strlcpy(VAR_1->avf->VAR_8, VAR_1->current_segment_final_filename_fmt, sizeof(VAR_1->avf->VAR_8)); if (VAR_1->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) { char * VAR_8 = av_strdup(VAR_1->avf->VAR_8); if (!VAR_8)
return AVERROR(ENOMEM); if (replace_int_data_in_filename(VAR_1->avf->VAR_8, sizeof(VAR_1->avf->VAR_8),
VAR_8, 'VAR_0', VAR_3 + VAR_4) < 1) { av_log(VAR_1, AV_LOG_ERROR,
"Invalid second level segment VAR_8 template '%VAR_0', "
"you can try to remove second_level_segment_size flag\n",
VAR_8); av_free(VAR_8); av_free(VAR_7); return AVERROR(EINVAL); } av_free(VAR_8); } if (VAR_1->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) { char * VAR_8 = av_strdup(VAR_1->avf->VAR_8); if (!VAR_8)
return AVERROR(ENOMEM); if (replace_int_data_in_filename(VAR_1->avf->VAR_8, sizeof(VAR_1->avf->VAR_8),
VAR_8, 't',  (int64_t)round(1000000 * VAR_2)) < 1) { av_log(VAR_1, AV_LOG_ERROR,
"Invalid second level segment VAR_8 template '%VAR_0', "
"you can try to remove second_level_segment_time flag\n",
VAR_8); av_free(VAR_8); av_free(VAR_7); return AVERROR(EINVAL); } av_free(VAR_8); } ff_rename(VAR_7, VAR_1->avf->VAR_8, VAR_1); av_free(VAR_7); } VAR_8 = av_basename(VAR_1->avf->VAR_8); if (VAR_1->use_localtime_mkdir) { VAR_8 = VAR_1->avf->VAR_8; } if (find_segment_by_filename(VAR_1->segments, VAR_8)
|| find_segment_by_filename(VAR_1->old_segments, VAR_8)) { av_log(VAR_1, AV_LOG_WARNING, "Duplicated segment VAR_8 detected: %VAR_0\n", VAR_8); } av_strlcpy(en->VAR_8, VAR_8, sizeof(en->VAR_8)); if(VAR_1->has_subtitle)
av_strlcpy(en->sub_filename, av_basename(VAR_1->vtt_avf->VAR_8), sizeof(en->sub_filename)); else
en->sub_filename[0] = '\0'; en->VAR_2 = VAR_2; en->VAR_3      = VAR_3; en->VAR_4     = VAR_4; en->next     = NULL; en->discont  = 0; if (VAR_1->discontinuity) { en->discont = 1; VAR_1->discontinuity = 0; } if (VAR_1->key_info_file) { av_strlcpy(en->key_uri, VAR_1->key_uri, sizeof(en->key_uri)); av_strlcpy(en->iv_string, VAR_1->iv_string, sizeof(en->iv_string)); } if (!VAR_1->segments)
VAR_1->segments = en; else
VAR_1->last_segment->next = en; VAR_1->last_segment = en; if (VAR_1->pl_type != PLAYLIST_TYPE_NONE)
VAR_1->max_nb_segments = 0; if (VAR_1->max_nb_segments && VAR_1->nb_entries >= VAR_1->max_nb_segments) { en = VAR_1->segments; VAR_1->initial_prog_date_time += en->VAR_2; VAR_1->segments = en->next; if (en && VAR_1->flags & HLS_DELETE_SEGMENTS &&
!(VAR_1->flags & HLS_SINGLE_FILE || VAR_1->wrap)) { en->next = VAR_1->old_segments; VAR_1->old_segments = en; if ((VAR_6 = hls_delete_old_segments(VAR_1)) < 0)
return VAR_6; } else av_free(en); } else VAR_1->nb_entries++; if (VAR_1->max_seg_size > 0) { return 0; } VAR_1->sequence++; return 0; }