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.

148 lines
3.9KB

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