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.

159 lines
4.2KB

  1. /*
  2. * Copyright (c) 2014 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "url.h"
  25. typedef struct SubfileContext {
  26. const AVClass *class;
  27. URLContext *h;
  28. int64_t start;
  29. int64_t end;
  30. int64_t pos;
  31. } SubfileContext;
  32. #define OFFSET(field) offsetof(SubfileContext, field)
  33. #define D AV_OPT_FLAG_DECODING_PARAM
  34. static const AVOption subfile_options[] = {
  35. { "start", "start offset", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
  36. { "end", "end offset", OFFSET(end), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
  37. { NULL }
  38. };
  39. #undef OFFSET
  40. #undef D
  41. static const AVClass subfile_class = {
  42. .class_name = "subfile",
  43. .item_name = av_default_item_name,
  44. .option = subfile_options,
  45. .version = LIBAVUTIL_VERSION_INT,
  46. };
  47. static int slave_seek(URLContext *h)
  48. {
  49. SubfileContext *c = h->priv_data;
  50. int64_t ret;
  51. if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) {
  52. if (ret >= 0)
  53. ret = AVERROR_BUG;
  54. av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n",
  55. av_err2str(ret));
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. static int subfile_open(URLContext *h, const char *filename, int flags,
  61. AVDictionary **options)
  62. {
  63. SubfileContext *c = h->priv_data;
  64. int ret;
  65. if (!c->end)
  66. c->end = INT64_MAX;
  67. if (c->end <= c->start) {
  68. av_log(h, AV_LOG_ERROR, "end before start\n");
  69. return AVERROR(EINVAL);
  70. }
  71. av_strstart(filename, "subfile:", &filename);
  72. ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
  73. options, h->protocol_whitelist, h->protocol_blacklist, h);
  74. if (ret < 0)
  75. return ret;
  76. c->pos = c->start;
  77. if ((ret = slave_seek(h)) < 0) {
  78. ffurl_closep(&c->h);
  79. return ret;
  80. }
  81. return 0;
  82. }
  83. static int subfile_close(URLContext *h)
  84. {
  85. SubfileContext *c = h->priv_data;
  86. return ffurl_closep(&c->h);
  87. }
  88. static int subfile_read(URLContext *h, unsigned char *buf, int size)
  89. {
  90. SubfileContext *c = h->priv_data;
  91. int64_t rest = c->end - c->pos;
  92. int ret;
  93. if (rest <= 0)
  94. return AVERROR_EOF;
  95. size = FFMIN(size, rest);
  96. ret = ffurl_read(c->h, buf, size);
  97. if (ret >= 0)
  98. c->pos += ret;
  99. return ret;
  100. }
  101. static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
  102. {
  103. SubfileContext *c = h->priv_data;
  104. int64_t new_pos, end;
  105. int ret;
  106. if (whence == AVSEEK_SIZE || whence == SEEK_END) {
  107. end = c->end;
  108. if (end == INT64_MAX && (end = ffurl_seek(c->h, 0, AVSEEK_SIZE)) < 0)
  109. return end;
  110. }
  111. if (whence == AVSEEK_SIZE)
  112. return end - c->start;
  113. switch (whence) {
  114. case SEEK_SET:
  115. new_pos = c->start + pos;
  116. break;
  117. case SEEK_CUR:
  118. new_pos = c->pos + pos;
  119. break;
  120. case SEEK_END:
  121. new_pos = end + pos;
  122. break;
  123. }
  124. if (new_pos < c->start)
  125. return AVERROR(EINVAL);
  126. c->pos = new_pos;
  127. if ((ret = slave_seek(h)) < 0)
  128. return ret;
  129. return c->pos - c->start;
  130. }
  131. const URLProtocol ff_subfile_protocol = {
  132. .name = "subfile",
  133. .url_open2 = subfile_open,
  134. .url_read = subfile_read,
  135. .url_seek = subfile_seek,
  136. .url_close = subfile_close,
  137. .priv_data_size = sizeof(SubfileContext),
  138. .priv_data_class = &subfile_class,
  139. .default_whitelist = "file",
  140. };