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.

137 lines
5.4KB

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