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.

198 lines
5.5KB

  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. };
  37. static av_cold int concat_close(URLContext *h)
  38. {
  39. int err = 0;
  40. size_t i;
  41. struct concat_data *data = h->priv_data;
  42. struct concat_nodes *nodes = data->nodes;
  43. for (i = 0; i != data->length; i++)
  44. err |= ffurl_close(nodes[i].uc);
  45. av_freep(&data->nodes);
  46. return err < 0 ? -1 : 0;
  47. }
  48. static av_cold int concat_open(URLContext *h, const char *uri, int flags)
  49. {
  50. char *node_uri = NULL;
  51. int err = 0;
  52. int64_t size;
  53. size_t len, i;
  54. URLContext *uc;
  55. struct concat_data *data = h->priv_data;
  56. struct concat_nodes *nodes;
  57. if (!av_strstart(uri, "concat:", &uri)) {
  58. av_log(h, AV_LOG_ERROR, "URL %s lacks prefix\n", uri);
  59. return AVERROR(EINVAL);
  60. }
  61. for (i = 0, len = 1; uri[i]; i++) {
  62. if (uri[i] == *AV_CAT_SEPARATOR) {
  63. /* integer overflow */
  64. if (++len == UINT_MAX / sizeof(*nodes)) {
  65. av_freep(&h->priv_data);
  66. return AVERROR(ENAMETOOLONG);
  67. }
  68. }
  69. }
  70. if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
  71. return AVERROR(ENOMEM);
  72. else
  73. data->nodes = nodes;
  74. /* handle input */
  75. if (!*uri)
  76. err = AVERROR(ENOENT);
  77. for (i = 0; *uri; i++) {
  78. /* parsing uri */
  79. len = strcspn(uri, AV_CAT_SEPARATOR);
  80. if ((err = av_reallocp(&node_uri, len + 1)) < 0)
  81. break;
  82. av_strlcpy(node_uri, uri, len + 1);
  83. uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
  84. /* creating URLContext */
  85. err = ffurl_open_whitelist(&uc, node_uri, flags,
  86. &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist);
  87. if (err < 0)
  88. break;
  89. /* creating size */
  90. if ((size = ffurl_size(uc)) < 0) {
  91. ffurl_close(uc);
  92. err = AVERROR(ENOSYS);
  93. break;
  94. }
  95. /* assembling */
  96. nodes[i].uc = uc;
  97. nodes[i].size = size;
  98. }
  99. av_free(node_uri);
  100. data->length = i;
  101. if (err < 0)
  102. concat_close(h);
  103. else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
  104. concat_close(h);
  105. err = AVERROR(ENOMEM);
  106. } else
  107. data->nodes = nodes;
  108. return err;
  109. }
  110. static int concat_read(URLContext *h, unsigned char *buf, int size)
  111. {
  112. int result, total = 0;
  113. struct concat_data *data = h->priv_data;
  114. struct concat_nodes *nodes = data->nodes;
  115. size_t i = data->current;
  116. while (size > 0) {
  117. result = ffurl_read(nodes[i].uc, buf, size);
  118. if (result < 0)
  119. return total ? total : result;
  120. if (!result) {
  121. if (i + 1 == data->length ||
  122. ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
  123. break;
  124. }
  125. total += result;
  126. buf += result;
  127. size -= result;
  128. }
  129. data->current = i;
  130. return total;
  131. }
  132. static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
  133. {
  134. int64_t result;
  135. struct concat_data *data = h->priv_data;
  136. struct concat_nodes *nodes = data->nodes;
  137. size_t i;
  138. switch (whence) {
  139. case SEEK_END:
  140. for (i = data->length - 1; i && pos < -nodes[i].size; i--)
  141. pos += nodes[i].size;
  142. break;
  143. case SEEK_CUR:
  144. /* get the absolute position */
  145. for (i = 0; i != data->current; i++)
  146. pos += nodes[i].size;
  147. pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
  148. whence = SEEK_SET;
  149. /* fall through with the absolute position */
  150. case SEEK_SET:
  151. for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
  152. pos -= nodes[i].size;
  153. break;
  154. default:
  155. return AVERROR(EINVAL);
  156. }
  157. result = ffurl_seek(nodes[i].uc, pos, whence);
  158. if (result >= 0) {
  159. data->current = i;
  160. while (i)
  161. result += nodes[--i].size;
  162. }
  163. return result;
  164. }
  165. const URLProtocol ff_concat_protocol = {
  166. .name = "concat",
  167. .url_open = concat_open,
  168. .url_read = concat_read,
  169. .url_seek = concat_seek,
  170. .url_close = concat_close,
  171. .priv_data_size = sizeof(struct concat_data),
  172. .default_whitelist = "concat,file,subfile",
  173. };