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.

204 lines
5.7KB

  1. /*
  2. * Concat URL protocol
  3. * Copyright (c) 2006 Steve Lhomme
  4. * Copyright (c) 2007 Wolfram Gloger
  5. * Copyright (c) 2010 Michele OrrĂ¹
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/mem.h"
  25. #include "avformat.h"
  26. #include "url.h"
  27. #define AV_CAT_SEPARATOR "|"
  28. struct concat_nodes {
  29. URLContext *uc; ///< node's URLContext
  30. int64_t size; ///< url filesize
  31. };
  32. struct concat_data {
  33. struct concat_nodes *nodes; ///< list of nodes to concat
  34. size_t length; ///< number of cat'ed nodes
  35. size_t current; ///< index of currently read node
  36. uint64_t total_size;
  37. };
  38. static av_cold int concat_close(URLContext *h)
  39. {
  40. int err = 0;
  41. size_t i;
  42. struct concat_data *data = h->priv_data;
  43. struct concat_nodes *nodes = data->nodes;
  44. for (i = 0; i != data->length; i++)
  45. err |= ffurl_close(nodes[i].uc);
  46. av_freep(&data->nodes);
  47. return err < 0 ? -1 : 0;
  48. }
  49. static av_cold int concat_open(URLContext *h, const char *uri, int flags)
  50. {
  51. char *node_uri = NULL;
  52. int err = 0;
  53. int64_t size, total_size = 0;
  54. size_t len, i;
  55. URLContext *uc;
  56. struct concat_data *data = h->priv_data;
  57. struct concat_nodes *nodes;
  58. if (!av_strstart(uri, "concat:", &uri)) {
  59. av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
  60. return AVERROR(EINVAL);
  61. }
  62. for (i = 0, len = 1; uri[i]; i++) {
  63. if (uri[i] == *AV_CAT_SEPARATOR) {
  64. /* integer overflow */
  65. if (++len == UINT_MAX / sizeof(*nodes)) {
  66. av_freep(&h->priv_data);
  67. return AVERROR(ENAMETOOLONG);
  68. }
  69. }
  70. }
  71. if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
  72. return AVERROR(ENOMEM);
  73. else
  74. data->nodes = nodes;
  75. /* handle input */
  76. if (!*uri)
  77. err = AVERROR(ENOENT);
  78. for (i = 0; *uri; i++) {
  79. /* parsing uri */
  80. len = strcspn(uri, AV_CAT_SEPARATOR);
  81. if ((err = av_reallocp(&node_uri, len + 1)) < 0)
  82. break;
  83. av_strlcpy(node_uri, uri, len + 1);
  84. uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
  85. /* creating URLContext */
  86. err = ffurl_open_whitelist(&uc, node_uri, flags,
  87. &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
  88. if (err < 0)
  89. break;
  90. /* creating size */
  91. if ((size = ffurl_size(uc)) < 0) {
  92. ffurl_close(uc);
  93. err = AVERROR(ENOSYS);
  94. break;
  95. }
  96. /* assembling */
  97. nodes[i].uc = uc;
  98. nodes[i].size = size;
  99. total_size += size;
  100. }
  101. av_free(node_uri);
  102. data->length = i;
  103. if (err < 0)
  104. concat_close(h);
  105. else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
  106. concat_close(h);
  107. err = AVERROR(ENOMEM);
  108. } else
  109. data->nodes = nodes;
  110. data->total_size = total_size;
  111. return err;
  112. }
  113. static int concat_read(URLContext *h, unsigned char *buf, int size)
  114. {
  115. int result, total = 0;
  116. struct concat_data *data = h->priv_data;
  117. struct concat_nodes *nodes = data->nodes;
  118. size_t i = data->current;
  119. while (size > 0) {
  120. result = ffurl_read(nodes[i].uc, buf, size);
  121. if (result == AVERROR_EOF) {
  122. if (i + 1 == data->length ||
  123. ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
  124. break;
  125. result = 0;
  126. }
  127. if (result < 0)
  128. return total ? total : result;
  129. total += result;
  130. buf += result;
  131. size -= result;
  132. }
  133. data->current = i;
  134. return total ? total : result;
  135. }
  136. static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
  137. {
  138. int64_t result;
  139. struct concat_data *data = h->priv_data;
  140. struct concat_nodes *nodes = data->nodes;
  141. size_t i;
  142. if ((whence & AVSEEK_SIZE))
  143. return data->total_size;
  144. switch (whence) {
  145. case SEEK_END:
  146. for (i = data->length - 1; i && pos < -nodes[i].size; i--)
  147. pos += nodes[i].size;
  148. break;
  149. case SEEK_CUR:
  150. /* get the absolute position */
  151. for (i = 0; i != data->current; i++)
  152. pos += nodes[i].size;
  153. pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
  154. whence = SEEK_SET;
  155. /* fall through with the absolute position */
  156. case SEEK_SET:
  157. for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
  158. pos -= nodes[i].size;
  159. break;
  160. default:
  161. return AVERROR(EINVAL);
  162. }
  163. result = ffurl_seek(nodes[i].uc, pos, whence);
  164. if (result >= 0) {
  165. data->current = i;
  166. while (i)
  167. result += nodes[--i].size;
  168. }
  169. return result;
  170. }
  171. const URLProtocol ff_concat_protocol = {
  172. .name = "concat",
  173. .url_open = concat_open,
  174. .url_read = concat_read,
  175. .url_seek = concat_seek,
  176. .url_close = concat_close,
  177. .priv_data_size = sizeof(struct concat_data),
  178. .default_whitelist = "concat,file,subfile",
  179. };