concat.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. av_freep(&h->priv_data);
  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, *tmp_uri;
  52. int err = 0;
  53. int64_t size;
  54. size_t len, i;
  55. URLContext *uc;
  56. struct concat_data *data;
  57. struct concat_nodes *nodes;
  58. av_strstart(uri, "concat:", &uri);
  59. /* creating data */
  60. if (!(data = av_mallocz(sizeof(*data))))
  61. return AVERROR(ENOMEM);
  62. h->priv_data = data;
  63. for (i = 0, len = 1; uri[i]; i++)
  64. if (uri[i] == *AV_CAT_SEPARATOR)
  65. /* integer overflow */
  66. if (++len == UINT_MAX / sizeof(*nodes)) {
  67. av_freep(&h->priv_data);
  68. return AVERROR(ENAMETOOLONG);
  69. }
  70. if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
  71. av_freep(&h->priv_data);
  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 (!(tmp_uri = av_realloc(node_uri, len+1))) {
  82. err = AVERROR(ENOMEM);
  83. break;
  84. } else
  85. node_uri = tmp_uri;
  86. av_strlcpy(node_uri, uri, len+1);
  87. uri += len + strspn(uri+len, AV_CAT_SEPARATOR);
  88. /* creating URLContext */
  89. if ((err = ffurl_open(&uc, node_uri, flags)) < 0)
  90. break;
  91. /* creating size */
  92. if ((size = ffurl_size(uc)) < 0) {
  93. ffurl_close(uc);
  94. err = AVERROR(ENOSYS);
  95. break;
  96. }
  97. /* assembling */
  98. nodes[i].uc = uc;
  99. nodes[i].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. return err;
  111. }
  112. static int concat_read(URLContext *h, unsigned char *buf, int size)
  113. {
  114. int result, total = 0;
  115. struct concat_data *data = h->priv_data;
  116. struct concat_nodes *nodes = data->nodes;
  117. size_t i = data->current;
  118. while (size > 0) {
  119. result = ffurl_read(nodes[i].uc, buf, size);
  120. if (result < 0)
  121. return total ? total : result;
  122. if (!result)
  123. if (i + 1 == data->length ||
  124. ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
  125. break;
  126. total += result;
  127. buf += result;
  128. size -= result;
  129. }
  130. data->current = i;
  131. return total;
  132. }
  133. static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
  134. {
  135. int64_t result;
  136. struct concat_data *data = h->priv_data;
  137. struct concat_nodes *nodes = data->nodes;
  138. size_t i;
  139. switch (whence) {
  140. case SEEK_END:
  141. for (i = data->length - 1;
  142. i && pos < -nodes[i].size;
  143. i--)
  144. pos += nodes[i].size;
  145. break;
  146. case SEEK_CUR:
  147. /* get the absolute position */
  148. for (i = 0; i != data->current; i++)
  149. pos += nodes[i].size;
  150. pos += ffurl_seek(nodes[i].uc, 0, SEEK_CUR);
  151. whence = SEEK_SET;
  152. /* fall through with the absolute position */
  153. case SEEK_SET:
  154. for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
  155. pos -= nodes[i].size;
  156. break;
  157. default:
  158. return AVERROR(EINVAL);
  159. }
  160. result = ffurl_seek(nodes[i].uc, pos, whence);
  161. if (result >= 0) {
  162. data->current = i;
  163. while (i)
  164. result += nodes[--i].size;
  165. }
  166. return result;
  167. }
  168. URLProtocol ff_concat_protocol = {
  169. .name = "concat",
  170. .url_open = concat_open,
  171. .url_read = concat_read,
  172. .url_seek = concat_seek,
  173. .url_close = concat_close,
  174. };