O. I forgot to include on other patches the ticket 6856 fix depended on.
Colin PS: the depended patches are 0001-libavformat-dashdec-Fix-for-ticket-6658-Dash-demuxer.patch 0001-Download-dash-content-with-byte-range-info.patch (not necessary for compilation but needed for the complete solution) ________________________________ From: ffmpeg-devel <ffmpeg-devel-boun...@ffmpeg.org> on behalf of Michael Niedermayer <mich...@niedermayer.cc> Sent: January 13, 2018 6:56 PM To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] [PATCH] libavformat/dashdec: Fix for ticket 6856 (filename limited to 1024) On Fri, Jan 12, 2018 at 10:50:07PM +0000, Colin NG wrote: > --- > libavformat/dashdec.c | 87 > +++++++++++++++++++++++++++++++++------------------ > 1 file changed, 56 insertions(+), 31 deletions(-) applying: libavformat/dashdec: Fix for ticket 6856 (filename limited to 1024) error: sha1 information is lacking or useless (libavformat/dashdec.c). error: could not build fake ancestor Patch failed at 0001 libavformat/dashdec: Fix for ticket 6856 (filename limited to 1024) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Asymptotically faster algorithms should always be preferred if you have asymptotical amounts of data
From ab3436c0db92698b658aa4085a260f151e53fd81 Mon Sep 17 00:00:00 2001 From: Colin Ng <colin...@hotmail.com> Date: Wed, 3 Jan 2018 16:40:01 -0800 Subject: [PATCH] libavformat/dashdec: Fix for ticket 6658 (Dash demuxer segfault) - Add function 'resolve_content_path' to propagate the baseURL from upper level nodes. * if no baseURL is available, the path of mpd file will be set as the baseURL. - Remove checking for newly established connection. - Establish the communication protocol in each connection rather than applying one protocol to all connection. --- libavformat/dashdec.c | 126 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 13 deletions(-) diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c index 3798649..5345f91 100644 --- a/libavformat/dashdec.c +++ b/libavformat/dashdec.c @@ -148,6 +148,11 @@ static uint64_t get_current_time_in_sec(void) return av_gettime() / 1000000; } +static int ishttp(char *url) { + const char *proto_name = avio_find_protocol_name(url); + return av_strstart(proto_name, "http", NULL); +} + static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime) { struct tm timeinfo; @@ -392,7 +397,8 @@ static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5)) return AVERROR_INVALIDDATA; - ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); + av_freep(pb); + ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp); if (ret >= 0) { // update cookies on http response with setcookies. char *new_cookies = NULL; @@ -639,6 +645,103 @@ static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representat return 0; } +static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) { + + char *tmp_str = NULL; + char *path = NULL; + char *mpdName = NULL; + xmlNodePtr node = NULL; + char *baseurl = NULL; + char *root_url = NULL; + char *text = NULL; + + int isRootHttp = 0; + char token ='/'; + int start = 0; + int rootId = 0; + int updated = 0; + int size = 0; + int i; + int max_url_size = strlen(url); + + for (i = n_baseurl_nodes-1; i >= 0 ; i--) { + text = xmlNodeGetContent(baseurl_nodes[i]); + if (!text) + continue; + max_url_size += strlen(text); + if (ishttp(text)) { + xmlFree(text); + break; + } + xmlFree(text); + } + + text = av_mallocz(max_url_size); + if (!text) { + updated = AVERROR(ENOMEM); + goto end; + } + av_strlcpy(text, url, strlen(url)+1); + while (mpdName = av_strtok(text, "/", &text)) { + size = strlen(mpdName); + } + + path = av_mallocz(max_url_size); + tmp_str = av_mallocz(max_url_size); + if (!tmp_str || !path) { + updated = AVERROR(ENOMEM); + goto end; + } + + av_strlcpy (path, url, strlen(url) - size + 1); + for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) { + if (!(node = baseurl_nodes[rootId])) { + continue; + } + if (ishttp(xmlNodeGetContent(node))) { + break; + } + } + + node = baseurl_nodes[rootId]; + baseurl = xmlNodeGetContent(node); + root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path; + if (node) { + xmlNodeSetContent(node, root_url); + } + + size = strlen(root_url); + isRootHttp = ishttp(root_url); + + if (root_url[size - 1] != token) { + av_strlcat(root_url, "/", size + 2); + size += 2; + } + + for (i = 0; i < n_baseurl_nodes; ++i) { + if (i == rootId) { + continue; + } + text = xmlNodeGetContent(baseurl_nodes[i]); + if (text) { + memset(tmp_str, 0, strlen(tmp_str)); + if (!ishttp(text) && isRootHttp) { + av_strlcpy(tmp_str, root_url, size + 1); + } + start = (text[0] == token); + av_strlcat(tmp_str, text + start, max_url_size); + xmlNodeSetContent(baseurl_nodes[i], tmp_str); + updated = 1; + xmlFree(text); + } + } + +end: + av_free(path); + av_free(tmp_str); + return updated; + +} static int parse_manifest_representation(AVFormatContext *s, const char *url, xmlNodePtr node, xmlNodePtr adaptionset_node, @@ -698,6 +801,11 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url, baseurl_nodes[2] = adaptionset_baseurl_node; baseurl_nodes[3] = representation_baseurl_node; + ret = resolve_content_path(s, url, baseurl_nodes, 4); + if (ret == AVERROR(ENOMEM) || ret == 0) { + goto end; + } + if (representation_segmenttemplate_node || fragment_template_node) { fragment_timeline_node = NULL; fragment_templates_tab[0] = representation_segmenttemplate_node; @@ -993,6 +1101,9 @@ static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in) } mpd_baseurl_node = find_child_node_by_name(node, "BaseURL"); + if (!mpd_baseurl_node) { + mpd_baseurl_node = xmlNewNode(NULL, "BaseURL"); + } // at now we can handle only one period, with the longest duration node = xmlFirstElementChild(node); @@ -1315,6 +1426,7 @@ static int read_from_url(struct representation *pls, struct fragment *seg, } else { ret = avio_read(pls->input, buf, buf_size); } + if (ret > 0) pls->cur_seg_offset += ret; @@ -1343,18 +1455,6 @@ static int open_input(DASHContext *c, struct representation *pls, struct fragmen goto cleanup; } - /* Seek to the requested position. If this was a HTTP request, the offset - * should already be where want it to, but this allows e.g. local testing - * without a HTTP server. */ - if (!ret && seg->url_offset) { - int64_t seekret = avio_seek(pls->input, seg->url_offset, SEEK_SET); - if (seekret < 0) { - av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of DASH fragment '%s'\n", seg->url_offset, seg->url); - ret = (int) seekret; - ff_format_io_close(pls->parent, &pls->input); - } - } - cleanup: av_dict_free(&opts); pls->cur_seg_offset = 0; -- 2.7.4
From 3e6298f97838ee371e5690b9b38830e090c431a0 Mon Sep 17 00:00:00 2001 From: Colin Ng <colin...@hotmail.com> Date: Wed, 22 Nov 2017 14:51:32 -0800 Subject: [PATCH] Download dash content with byte range info --- libavformat/dashdec.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c index 0e3afd2..3798649 100644 --- a/libavformat/dashdec.c +++ b/libavformat/dashdec.c @@ -522,6 +522,24 @@ static enum AVMediaType get_content_type(xmlNodePtr node) return type; } +static struct fragment * get_Fragment(char *range) +{ + struct fragment * seg = av_mallocz(sizeof(struct fragment)); + + if (!seg) + return NULL; + + seg->size = -1; + if (range) { + char *str_end_offset; + char *str_offset = av_strtok(range, "-", &str_end_offset); + seg->url_offset = strtoll(str_offset, NULL, 10); + seg->size = strtoll(str_end_offset, NULL, 10) -seg->url_offset; + } + + return seg; +} + static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep, xmlNodePtr fragmenturl_node, xmlNodePtr *baseurl_nodes, @@ -530,33 +548,40 @@ static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati { char *initialization_val = NULL; char *media_val = NULL; + char *range_val = NULL; if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) { initialization_val = xmlGetProp(fragmenturl_node, "sourceURL"); - if (initialization_val) { - rep->init_section = av_mallocz(sizeof(struct fragment)); + range_val = xmlGetProp(fragmenturl_node, "range"); + if (initialization_val || range_val) { + rep->init_section = get_Fragment(range_val); if (!rep->init_section) { xmlFree(initialization_val); + xmlFree(range_val); return AVERROR(ENOMEM); } rep->init_section->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, initialization_val); + if (!rep->init_section->url) { av_free(rep->init_section); xmlFree(initialization_val); + xmlFree(range_val); return AVERROR(ENOMEM); } - rep->init_section->size = -1; xmlFree(initialization_val); + xmlFree(range_val); } } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) { media_val = xmlGetProp(fragmenturl_node, "media"); - if (media_val) { - struct fragment *seg = av_mallocz(sizeof(struct fragment)); + range_val = xmlGetProp(fragmenturl_node, "mediaRange"); + if (media_val || range_val) { + struct fragment *seg = get_Fragment(range_val); if (!seg) { xmlFree(media_val); + xmlFree(range_val); return AVERROR(ENOMEM); } seg->url = get_content_url(baseurl_nodes, 4, @@ -566,11 +591,12 @@ static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati if (!seg->url) { av_free(seg); xmlFree(media_val); + xmlFree(range_val); return AVERROR(ENOMEM); } - seg->size = -1; dynarray_add(&rep->fragments, &rep->n_fragments, seg); xmlFree(media_val); + xmlFree(range_val); } } -- 2.7.4
From 2cae06348d0a1bb58eb8c05258d1430808b19e1a Mon Sep 17 00:00:00 2001 From: Colin Ng <colin...@hotmail.com> Date: Fri, 12 Jan 2018 14:43:04 -0800 Subject: [PATCH] libavformat/dashdec: Fix for ticket 6856 (filename limited to 1024) --- libavformat/dashdec.c | 87 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c index 5345f91..7316213 100644 --- a/libavformat/dashdec.c +++ b/libavformat/dashdec.c @@ -141,6 +141,7 @@ typedef struct DASHContext { char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context char *allowed_extensions; AVDictionary *avio_opts; + int max_url_size; } DASHContext; static uint64_t get_current_time_in_sec(void) @@ -153,6 +154,10 @@ static int ishttp(char *url) { return av_strstart(proto_name, "http", NULL); } +static int aligned(int val) { + return ((val + 0x3F) >> 6) << 6; +} + static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime) { struct tm timeinfo; @@ -424,6 +429,7 @@ static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, static char *get_content_url(xmlNodePtr *baseurl_nodes, int n_baseurl_nodes, + int max_url_size, char *rep_id_val, char *rep_bandwidth_val, char *val) @@ -431,10 +437,13 @@ static char *get_content_url(xmlNodePtr *baseurl_nodes, int i; char *text; char *url = NULL; - char tmp_str[MAX_URL_SIZE]; - char tmp_str_2[MAX_URL_SIZE]; - memset(tmp_str, 0, sizeof(tmp_str)); + char *tmp_str = av_mallocz(max_url_size); + char *tmp_str_2 = av_mallocz(max_url_size); + + if (!tmp_str || !tmp_str_2) { + return NULL; + } for (i = 0; i < n_baseurl_nodes; ++i) { if (baseurl_nodes[i] && @@ -442,32 +451,35 @@ static char *get_content_url(xmlNodePtr *baseurl_nodes, baseurl_nodes[i]->children->type == XML_TEXT_NODE) { text = xmlNodeGetContent(baseurl_nodes[i]->children); if (text) { - memset(tmp_str, 0, sizeof(tmp_str)); - memset(tmp_str_2, 0, sizeof(tmp_str_2)); - ff_make_absolute_url(tmp_str_2, MAX_URL_SIZE, tmp_str, text); - av_strlcpy(tmp_str, tmp_str_2, sizeof(tmp_str)); + memset(tmp_str, 0, max_url_size); + memset(tmp_str_2, 0, max_url_size); + ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text); + av_strlcpy(tmp_str, tmp_str_2, max_url_size); xmlFree(text); } } } if (val) - av_strlcat(tmp_str, (const char*)val, sizeof(tmp_str)); - + av_strlcat(tmp_str, (const char*)val, max_url_size); if (rep_id_val) { url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val); if (!url) { - return NULL; + goto end; } - av_strlcpy(tmp_str, url, sizeof(tmp_str)); + av_strlcpy(tmp_str, url, max_url_size); av_free(url); } if (rep_bandwidth_val && tmp_str[0] != '\0') { url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val); if (!url) { - return NULL; + goto end; } } + +end: + av_free(tmp_str); + av_free(tmp_str_2); return url; } @@ -540,7 +552,7 @@ static struct fragment * get_Fragment(char *range) char *str_end_offset; char *str_offset = av_strtok(range, "-", &str_end_offset); seg->url_offset = strtoll(str_offset, NULL, 10); - seg->size = strtoll(str_end_offset, NULL, 10) -seg->url_offset; + seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset; } return seg; @@ -552,9 +564,11 @@ static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati char *rep_id_val, char *rep_bandwidth_val) { + DASHContext *c = s->priv_data; char *initialization_val = NULL; char *media_val = NULL; char *range_val = NULL; + int max_url_size = c ? c->max_url_size: MAX_URL_SIZE; if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) { initialization_val = xmlGetProp(fragmenturl_node, "sourceURL"); @@ -567,6 +581,7 @@ static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati return AVERROR(ENOMEM); } rep->init_section->url = get_content_url(baseurl_nodes, 4, + max_url_size, rep_id_val, rep_bandwidth_val, initialization_val); @@ -591,6 +606,7 @@ static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representati return AVERROR(ENOMEM); } seg->url = get_content_url(baseurl_nodes, 4, + max_url_size, rep_id_val, rep_bandwidth_val, media_val); @@ -645,7 +661,7 @@ static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representat return 0; } -static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) { +static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) { char *tmp_str = NULL; char *path = NULL; @@ -662,13 +678,13 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr int updated = 0; int size = 0; int i; - int max_url_size = strlen(url); + int tmp_max_url_size = strlen(url); for (i = n_baseurl_nodes-1; i >= 0 ; i--) { text = xmlNodeGetContent(baseurl_nodes[i]); if (!text) continue; - max_url_size += strlen(text); + tmp_max_url_size += strlen(text); if (ishttp(text)) { xmlFree(text); break; @@ -676,7 +692,8 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr xmlFree(text); } - text = av_mallocz(max_url_size); + tmp_max_url_size = aligned(tmp_max_url_size); + text = av_mallocz(tmp_max_url_size); if (!text) { updated = AVERROR(ENOMEM); goto end; @@ -686,8 +703,8 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr size = strlen(mpdName); } - path = av_mallocz(max_url_size); - tmp_str = av_mallocz(max_url_size); + path = av_mallocz(tmp_max_url_size); + tmp_str = av_mallocz(tmp_max_url_size); if (!tmp_str || !path) { updated = AVERROR(ENOMEM); goto end; @@ -708,6 +725,7 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path; if (node) { xmlNodeSetContent(node, root_url); + updated = 1; } size = strlen(root_url); @@ -729,7 +747,7 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr av_strlcpy(tmp_str, root_url, size + 1); } start = (text[0] == token); - av_strlcat(tmp_str, text + start, max_url_size); + av_strlcat(tmp_str, text + start, tmp_max_url_size); xmlNodeSetContent(baseurl_nodes[i], tmp_str); updated = 1; xmlFree(text); @@ -737,6 +755,9 @@ static int resolve_content_path(AVFormatContext *s, const char *url, xmlNodePtr } end: + if (tmp_max_url_size > *max_url_size) { + *max_url_size = tmp_max_url_size; + } av_free(path); av_free(tmp_str); return updated; @@ -801,7 +822,8 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url, baseurl_nodes[2] = adaptionset_baseurl_node; baseurl_nodes[3] = representation_baseurl_node; - ret = resolve_content_path(s, url, baseurl_nodes, 4); + ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4); + c->max_url_size = aligned(c->max_url_size + strlen(rep_id_val) + strlen(rep_bandwidth_val)); if (ret == AVERROR(ENOMEM) || ret == 0) { goto end; } @@ -825,7 +847,8 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url, ret = AVERROR(ENOMEM); goto end; } - rep->init_section->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, initialization_val); + c->max_url_size = aligned(c->max_url_size + strlen(initialization_val)); + rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val); if (!rep->init_section->url) { av_free(rep->init_section); av_free(rep); @@ -837,7 +860,8 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url, } if (media_val) { - rep->url_template = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, media_val); + c->max_url_size = aligned(c->max_url_size + strlen(media_val)); + rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val); xmlFree(media_val); } @@ -878,7 +902,7 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url, ret = AVERROR(ENOMEM); goto end; } - seg->url = get_content_url(baseurl_nodes, 4, rep_id_val, rep_bandwidth_val, NULL); + seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL); if (!seg->url) { av_free(seg); ret = AVERROR(ENOMEM); @@ -1384,19 +1408,19 @@ static struct fragment *get_current_fragment(struct representation *pls) } } if (seg) { - char tmpfilename[MAX_URL_SIZE]; - - ff_dash_fill_tmpl_params(tmpfilename, sizeof(tmpfilename), pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no)); + char *tmpfilename= av_mallocz(c->max_url_size); + ff_dash_fill_tmpl_params(tmpfilename, c->max_url_size, pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no)); seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename); if (!seg->url) { av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template); seg->url = av_strdup(pls->url_template); if (!seg->url) { av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template); + av_free(tmpfilename); return NULL; } } - + av_free(tmpfilename); seg->size = -1; } @@ -1436,7 +1460,7 @@ static int read_from_url(struct representation *pls, struct fragment *seg, static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg) { AVDictionary *opts = NULL; - char url[MAX_URL_SIZE]; + char *url = av_mallocz(c->max_url_size); int ret; set_httpheader_options(c, &opts); @@ -1447,7 +1471,7 @@ static int open_input(DASHContext *c, struct representation *pls, struct fragmen av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0); } - ff_make_absolute_url(url, MAX_URL_SIZE, c->base_url, seg->url); + ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url); av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n", url, seg->url_offset, pls->rep_idx); ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL); @@ -1456,6 +1480,7 @@ static int open_input(DASHContext *c, struct representation *pls, struct fragmen } cleanup: + av_free(url); av_dict_free(&opts); pls->cur_seg_offset = 0; pls->cur_seg_size = seg->size; @@ -1839,7 +1864,7 @@ static int dash_close(AVFormatContext *s) if (c->cur_video) { free_representation(c->cur_video); } - + c->max_url_size = 0; av_freep(&c->cookies); av_freep(&c->user_agent); av_dict_free(&c->avio_opts); -- 2.7.4
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel