1 /*
2 * MPEG-DASH ISO BMFF segmenter
3 * Copyright (c) 2014 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
33
42
44 size_t format_tag_size, const char **ptr) {
45 const char *next_ptr;
47
50 *ptr = next_ptr;
51 }
else if (
av_strstart(identifier,
"$RepresentationID$", &next_ptr)) {
53 // default to basic format, as $RepresentationID$ identifiers
54 // are not allowed to have custom format-tags.
56 *ptr = next_ptr;
57 } else { // the following identifiers may have an explicit format_tag
60 else if (
av_strstart(identifier,
"$Bandwidth", &next_ptr))
62 else if (
av_strstart(identifier,
"$Time", &next_ptr))
64 else
66
67 // next parse the dash format-tag and generate a c-string format tag
68 // (next_ptr now points at the first '%' at the beginning of the format-tag)
71 if (next_ptr[0] == '$') { // no dash format-tag
72 snprintf(format_tag, format_tag_size,
"%%%s", number_format);
73 *ptr = &next_ptr[1];
74 } else {
75 const char *width_ptr;
76 // only tolerate single-digit width-field (i.e. up to 9-digit width)
80 // yes, we're using a format tag to build format_tag.
81 snprintf(format_tag, format_tag_size,
"%s%c%s",
"%0", width_ptr[0], number_format);
82 *ptr = next_ptr;
83 } else {
85 "closing '$' character or a format-string like '%%0[width]d', "
86 "where width must be a single digit\n", next_ptr);
88 }
89 }
90 }
91 }
92 return id_type;
93 }
94
96 const char *template, int rep_id,
97 int number, int bit_rate,
98 int64_t time) {
99 int dst_pos = 0;
100 const char *t_cur = template;
101 while (dst_pos < buffer_size - 1 && *t_cur) {
102 char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
103 int n = 0;
105 const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
106 if (t_next) {
107 int num_copy_bytes =
FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
108 av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
109 // advance
110 dst_pos += num_copy_bytes;
111 t_cur = t_next;
112 } else { // no more DASH identifiers to substitute - just copy the rest over and break
113 av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
114 break;
115 }
116
117 if (dst_pos >= buffer_size - 1 || !*t_cur)
118 break;
119
120 // t_cur is now pointing to a '$' character
122 switch (id_type) {
125 n = 1;
126 break;
128 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
129 break;
131 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
132 break;
134 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
135 break;
137 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
138 break;
140 // copy over one byte and advance
142 n = 1;
143 t_next = &t_cur[1];
144 break;
145 }
146 // t_next points just past the processed identifier
147 // n is the number of bytes that were attempted to be written to dst
148 // (may have failed to write all because buffer_size).
149
150 // advance
151 dst_pos +=
FFMIN(n, buffer_size - dst_pos - 1);
152 t_cur = t_next;
153 }
154 }
155
156