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.

191 lines
5.3KB

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