concat.c 5.3 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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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. #define AV_CAT_SEPARATOR "|"
  27. struct concat_nodes {
  28. URLContext *uc; ///< node's URLContext
  29. int64_t size; ///< url filesize
  30. };
  31. struct concat_data {
  32. struct concat_nodes *nodes; ///< list of nodes to concat
  33. size_t length; ///< number of cat'ed nodes
  34. size_t current; ///< index of currently read node
  35. };
  36. static av_cold int concat_close(URLContext *h)
  37. {
  38. int err = 0;
  39. size_t i;
  40. struct concat_data *data = h->priv_data;
  41. struct concat_nodes *nodes = data->nodes;
  42. for (i = 0; i != data->length; i++)
  43. err |= url_close(nodes[i].uc);
  44. av_freep(&data->nodes);
  45. av_freep(&h->priv_data);
  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, *tmp_uri;
  51. int err = 0;
  52. int64_t size;
  53. size_t len, i;
  54. URLContext *uc;
  55. struct concat_data *data;
  56. struct concat_nodes *nodes;
  57. av_strstart(uri, "concat:", &uri);
  58. /* creating data */
  59. if (!(data = av_mallocz(sizeof(*data))))
  60. return AVERROR(ENOMEM);
  61. h->priv_data = data;
  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. if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
  70. av_freep(&h->priv_data);
  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 (!(tmp_uri = av_realloc(node_uri, len+1))) {
  81. err = AVERROR(ENOMEM);
  82. break;
  83. } else
  84. node_uri = tmp_uri;
  85. av_strlcpy(node_uri, uri, len+1);
  86. uri += len + strspn(uri+len, AV_CAT_SEPARATOR);
  87. /* creating URLContext */
  88. if ((err = url_open(&uc, node_uri, flags)) < 0)
  89. break;
  90. /* creating size */
  91. if ((size = url_filesize(uc)) < 0) {
  92. url_close(uc);
  93. err = AVERROR(ENOSYS);
  94. break;
  95. }
  96. /* assembling */
  97. nodes[i].uc = uc;
  98. nodes[i].size = size;
  99. }
  100. av_free(node_uri);
  101. data->length = i;
  102. if (err < 0)
  103. concat_close(h);
  104. else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
  105. concat_close(h);
  106. err = AVERROR(ENOMEM);
  107. } else
  108. data->nodes = nodes;
  109. return err;
  110. }
  111. static int concat_read(URLContext *h, unsigned char *buf, int size)
  112. {
  113. int result, total = 0;
  114. struct concat_data *data = h->priv_data;
  115. struct concat_nodes *nodes = data->nodes;
  116. size_t i = data->current;
  117. while (size > 0) {
  118. result = url_read(nodes[i].uc, buf, size);
  119. if (result < 0)
  120. return total ? total : result;
  121. if (!result)
  122. if (i + 1 == data->length ||
  123. url_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
  124. break;
  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;
  141. i && pos < -nodes[i].size;
  142. i--)
  143. pos += nodes[i].size;
  144. break;
  145. case SEEK_CUR:
  146. /* get the absolute position */
  147. for (i = 0; i != data->current; i++)
  148. pos += nodes[i].size;
  149. pos += url_seek(nodes[i].uc, 0, SEEK_CUR);
  150. whence = SEEK_SET;
  151. /* fall through with the absolute position */
  152. case SEEK_SET:
  153. for (i = 0; i != data->length - 1 && pos >= nodes[i].size; i++)
  154. pos -= nodes[i].size;
  155. break;
  156. default:
  157. return AVERROR(EINVAL);
  158. }
  159. result = url_seek(nodes[i].uc, pos, whence);
  160. if (result >= 0) {
  161. data->current = i;
  162. while (i)
  163. result += nodes[--i].size;
  164. }
  165. return result;
  166. }
  167. URLProtocol ff_concat_protocol = {
  168. "concat",
  169. concat_open,
  170. concat_read,
  171. NULL,
  172. concat_seek,
  173. concat_close,
  174. };