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.

150 lines
4.0KB

  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 <= c->start) {
  66. av_log(h, AV_LOG_ERROR, "end before start\n");
  67. return AVERROR(EINVAL);
  68. }
  69. av_strstart(filename, "subfile:", &filename);
  70. ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
  71. options, h->protocol_whitelist);
  72. if (ret < 0)
  73. return ret;
  74. c->pos = c->start;
  75. if ((ret = slave_seek(h)) < 0) {
  76. ffurl_close(c->h);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int subfile_close(URLContext *h)
  82. {
  83. SubfileContext *c = h->priv_data;
  84. return ffurl_close(c->h);
  85. }
  86. static int subfile_read(URLContext *h, unsigned char *buf, int size)
  87. {
  88. SubfileContext *c = h->priv_data;
  89. int64_t rest = c->end - c->pos;
  90. int ret;
  91. if (rest <= 0)
  92. return 0;
  93. size = FFMIN(size, rest);
  94. ret = ffurl_read(c->h, buf, size);
  95. if (ret >= 0)
  96. c->pos += ret;
  97. return ret;
  98. }
  99. static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
  100. {
  101. SubfileContext *c = h->priv_data;
  102. int64_t new_pos = -1;
  103. int ret;
  104. if (whence == AVSEEK_SIZE)
  105. return c->end - c->start;
  106. switch (whence) {
  107. case SEEK_SET:
  108. new_pos = c->start + pos;
  109. break;
  110. case SEEK_CUR:
  111. new_pos += pos;
  112. break;
  113. case SEEK_END:
  114. new_pos = c->end + c->pos;
  115. break;
  116. }
  117. if (new_pos < c->start)
  118. return AVERROR(EINVAL);
  119. c->pos = new_pos;
  120. if ((ret = slave_seek(h)) < 0)
  121. return ret;
  122. return c->pos - c->start;
  123. }
  124. const URLProtocol ff_subfile_protocol = {
  125. .name = "subfile",
  126. .url_open2 = subfile_open,
  127. .url_read = subfile_read,
  128. .url_seek = subfile_seek,
  129. .url_close = subfile_close,
  130. .priv_data_size = sizeof(SubfileContext),
  131. .priv_data_class = &subfile_class,
  132. .default_whitelist = "file",
  133. };