You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.2KB

  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. #include "config.h"
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/rational.h"
  31. #include "libavutil/time_internal.h"
  32. #include "avc.h"
  33. #include "avformat.h"
  34. #include "avio_internal.h"
  35. #include "internal.h"
  36. #include "isom.h"
  37. #include "os_support.h"
  38. #include "url.h"
  39. #include "dash.h"
  40. static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
  41. size_t format_tag_size, const char **ptr) {
  42. const char *next_ptr;
  43. DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
  44. if (av_strstart(identifier, "$$", &next_ptr)) {
  45. id_type = DASH_TMPL_ID_ESCAPE;
  46. *ptr = next_ptr;
  47. } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
  48. id_type = DASH_TMPL_ID_REP_ID;
  49. // default to basic format, as $RepresentationID$ identifiers
  50. // are not allowed to have custom format-tags.
  51. av_strlcpy(format_tag, "%d", format_tag_size);
  52. *ptr = next_ptr;
  53. } else { // the following identifiers may have an explicit format_tag
  54. if (av_strstart(identifier, "$Number", &next_ptr))
  55. id_type = DASH_TMPL_ID_NUMBER;
  56. else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
  57. id_type = DASH_TMPL_ID_BANDWIDTH;
  58. else if (av_strstart(identifier, "$Time", &next_ptr))
  59. id_type = DASH_TMPL_ID_TIME;
  60. else
  61. id_type = DASH_TMPL_ID_UNDEFINED;
  62. // next parse the dash format-tag and generate a c-string format tag
  63. // (next_ptr now points at the first '%' at the beginning of the format-tag)
  64. if (id_type != DASH_TMPL_ID_UNDEFINED) {
  65. const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
  66. if (next_ptr[0] == '$') { // no dash format-tag
  67. snprintf(format_tag, format_tag_size, "%%%s", number_format);
  68. *ptr = &next_ptr[1];
  69. } else {
  70. const char *width_ptr;
  71. // only tolerate single-digit width-field (i.e. up to 9-digit width)
  72. if (av_strstart(next_ptr, "%0", &width_ptr) &&
  73. av_isdigit(width_ptr[0]) &&
  74. av_strstart(&width_ptr[1], "d$", &next_ptr)) {
  75. // yes, we're using a format tag to build format_tag.
  76. snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
  77. *ptr = next_ptr;
  78. } else {
  79. av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
  80. "closing '$' character or a format-string like '%%0[width]d', "
  81. "where width must be a single digit\n", next_ptr);
  82. id_type = DASH_TMPL_ID_UNDEFINED;
  83. }
  84. }
  85. }
  86. }
  87. return id_type;
  88. }
  89. void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size,
  90. const char *template, int rep_id,
  91. int number, int bit_rate,
  92. int64_t time) {
  93. int dst_pos = 0;
  94. const char *t_cur = template;
  95. while (dst_pos < buffer_size - 1 && *t_cur) {
  96. char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
  97. int n = 0;
  98. DASHTmplId id_type;
  99. const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
  100. if (t_next) {
  101. int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
  102. av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
  103. // advance
  104. dst_pos += num_copy_bytes;
  105. t_cur = t_next;
  106. } else { // no more DASH identifiers to substitute - just copy the rest over and break
  107. av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
  108. break;
  109. }
  110. if (dst_pos >= buffer_size - 1 || !*t_cur)
  111. break;
  112. // t_cur is now pointing to a '$' character
  113. id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
  114. switch (id_type) {
  115. case DASH_TMPL_ID_ESCAPE:
  116. av_strlcpy(&dst[dst_pos], "$", 2);
  117. n = 1;
  118. break;
  119. case DASH_TMPL_ID_REP_ID:
  120. n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
  121. break;
  122. case DASH_TMPL_ID_NUMBER:
  123. n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
  124. break;
  125. case DASH_TMPL_ID_BANDWIDTH:
  126. n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
  127. break;
  128. case DASH_TMPL_ID_TIME:
  129. n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
  130. break;
  131. case DASH_TMPL_ID_UNDEFINED:
  132. // copy over one byte and advance
  133. av_strlcpy(&dst[dst_pos], t_cur, 2);
  134. n = 1;
  135. t_next = &t_cur[1];
  136. break;
  137. }
  138. // t_next points just past the processed identifier
  139. // n is the number of bytes that were attempted to be written to dst
  140. // (may have failed to write all because buffer_size).
  141. // advance
  142. dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
  143. t_cur = t_next;
  144. }
  145. }