dash.c 6.2 KB

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