cache.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Input cache protocol.
  3. * Copyright (c) 2011,2014 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * Based on file.c by Fabrice Bellard
  22. */
  23. /**
  24. * @TODO
  25. * support keeping files
  26. * support filling with a background thread
  27. */
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/file.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/tree.h"
  33. #include "avformat.h"
  34. #include <fcntl.h>
  35. #if HAVE_IO_H
  36. #include <io.h>
  37. #endif
  38. #if HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #include <sys/stat.h>
  42. #include <stdlib.h>
  43. #include "os_support.h"
  44. #include "url.h"
  45. typedef struct CacheEntry {
  46. int64_t logical_pos;
  47. int64_t physical_pos;
  48. int size;
  49. } CacheEntry;
  50. typedef struct Context {
  51. AVClass *class;
  52. int fd;
  53. struct AVTreeNode *root;
  54. int64_t logical_pos;
  55. int64_t cache_pos;
  56. int64_t inner_pos;
  57. int64_t end;
  58. int is_true_eof;
  59. URLContext *inner;
  60. int64_t cache_hit, cache_miss;
  61. int read_ahead_limit;
  62. } Context;
  63. static int cmp(void *key, const void *node)
  64. {
  65. return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
  66. }
  67. static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
  68. {
  69. char *buffername;
  70. Context *c= h->priv_data;
  71. av_strstart(arg, "cache:", &arg);
  72. c->fd = av_tempfile("ffcache", &buffername, 0, h);
  73. if (c->fd < 0){
  74. av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
  75. return c->fd;
  76. }
  77. unlink(buffername);
  78. av_freep(&buffername);
  79. return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, options);
  80. }
  81. static int add_entry(URLContext *h, const unsigned char *buf, int size)
  82. {
  83. Context *c= h->priv_data;
  84. int64_t pos = -1;
  85. int ret;
  86. CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
  87. CacheEntry *entry_ret;
  88. struct AVTreeNode *node = NULL;
  89. //FIXME avoid lseek
  90. pos = lseek(c->fd, 0, SEEK_END);
  91. if (pos < 0) {
  92. ret = AVERROR(errno);
  93. av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
  94. goto fail;
  95. }
  96. c->cache_pos = pos;
  97. ret = write(c->fd, buf, size);
  98. if (ret < 0) {
  99. ret = AVERROR(errno);
  100. av_log(h, AV_LOG_ERROR, "write in cache failed\n");
  101. goto fail;
  102. }
  103. c->cache_pos += ret;
  104. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  105. if (!entry)
  106. entry = next[0];
  107. if (!entry ||
  108. entry->logical_pos + entry->size != c->logical_pos ||
  109. entry->physical_pos + entry->size != pos
  110. ) {
  111. entry = av_malloc(sizeof(*entry));
  112. node = av_tree_node_alloc();
  113. if (!entry || !node) {
  114. ret = AVERROR(ENOMEM);
  115. goto fail;
  116. }
  117. entry->logical_pos = c->logical_pos;
  118. entry->physical_pos = pos;
  119. entry->size = ret;
  120. entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
  121. if (entry_ret && entry_ret != entry) {
  122. ret = -1;
  123. av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
  124. goto fail;
  125. }
  126. } else
  127. entry->size += ret;
  128. return 0;
  129. fail:
  130. //we could truncate the file to pos here if pos >=0 but ftruncate isn't available in VS so
  131. //for simplicty we just leave the file a bit larger
  132. av_free(entry);
  133. av_free(node);
  134. return ret;
  135. }
  136. static int cache_read(URLContext *h, unsigned char *buf, int size)
  137. {
  138. Context *c= h->priv_data;
  139. CacheEntry *entry, *next[2] = {NULL, NULL};
  140. int r;
  141. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  142. if (!entry)
  143. entry = next[0];
  144. if (entry) {
  145. int64_t in_block_pos = c->logical_pos - entry->logical_pos;
  146. av_assert0(entry->logical_pos <= c->logical_pos);
  147. if (in_block_pos < entry->size) {
  148. int64_t physical_target = entry->physical_pos + in_block_pos;
  149. if (c->cache_pos != physical_target) {
  150. r = lseek(c->fd, physical_target, SEEK_SET);
  151. } else
  152. r = c->cache_pos;
  153. if (r >= 0) {
  154. c->cache_pos = r;
  155. r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
  156. }
  157. if (r > 0) {
  158. c->cache_pos += r;
  159. c->logical_pos += r;
  160. c->cache_hit ++;
  161. return r;
  162. }
  163. }
  164. }
  165. // Cache miss or some kind of fault with the cache
  166. if (c->logical_pos != c->inner_pos) {
  167. r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
  168. if (r<0) {
  169. av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
  170. return r;
  171. }
  172. c->inner_pos = r;
  173. }
  174. r = ffurl_read(c->inner, buf, size);
  175. if (r == 0 && size>0) {
  176. c->is_true_eof = 1;
  177. av_assert0(c->end >= c->logical_pos);
  178. }
  179. if (r<=0)
  180. return r;
  181. c->inner_pos += r;
  182. c->cache_miss ++;
  183. add_entry(h, buf, r);
  184. c->logical_pos += r;
  185. c->end = FFMAX(c->end, c->logical_pos);
  186. return r;
  187. }
  188. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  189. {
  190. Context *c= h->priv_data;
  191. int64_t ret;
  192. if (whence == AVSEEK_SIZE) {
  193. pos= ffurl_seek(c->inner, pos, whence);
  194. if(pos <= 0){
  195. pos= ffurl_seek(c->inner, -1, SEEK_END);
  196. if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
  197. av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
  198. }
  199. if (pos > 0)
  200. c->is_true_eof = 1;
  201. c->end = FFMAX(c->end, pos);
  202. return pos;
  203. }
  204. if (whence == SEEK_CUR) {
  205. whence = SEEK_SET;
  206. pos += c->logical_pos;
  207. } else if (whence == SEEK_END && c->is_true_eof) {
  208. resolve_eof:
  209. whence = SEEK_SET;
  210. pos += c->end;
  211. }
  212. if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
  213. //Seems within filesize, assume it will not fail.
  214. c->logical_pos = pos;
  215. return pos;
  216. }
  217. //cache miss
  218. ret= ffurl_seek(c->inner, pos, whence);
  219. if ((whence == SEEK_SET && pos >= c->logical_pos ||
  220. whence == SEEK_END && pos <= 0) && ret < 0) {
  221. if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
  222. || c->read_ahead_limit < 0) {
  223. uint8_t tmp[32768];
  224. while (c->logical_pos < pos || whence == SEEK_END) {
  225. int size = sizeof(tmp);
  226. if (whence == SEEK_SET)
  227. size = FFMIN(sizeof(tmp), pos - c->logical_pos);
  228. ret = cache_read(h, tmp, size);
  229. if (ret == 0 && whence == SEEK_END) {
  230. av_assert0(c->is_true_eof);
  231. goto resolve_eof;
  232. }
  233. if (ret < 0) {
  234. return ret;
  235. }
  236. }
  237. return c->logical_pos;
  238. }
  239. }
  240. if (ret >= 0) {
  241. c->logical_pos = ret;
  242. c->end = FFMAX(c->end, ret);
  243. }
  244. return ret;
  245. }
  246. static int cache_close(URLContext *h)
  247. {
  248. Context *c= h->priv_data;
  249. av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
  250. c->cache_hit, c->cache_miss);
  251. close(c->fd);
  252. ffurl_close(c->inner);
  253. av_tree_destroy(c->root);
  254. return 0;
  255. }
  256. #define OFFSET(x) offsetof(Context, x)
  257. #define D AV_OPT_FLAG_DECODING_PARAM
  258. static const AVOption options[] = {
  259. { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isn't supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
  260. {NULL},
  261. };
  262. static const AVClass cache_context_class = {
  263. .class_name = "Cache",
  264. .item_name = av_default_item_name,
  265. .option = options,
  266. .version = LIBAVUTIL_VERSION_INT,
  267. };
  268. URLProtocol ff_cache_protocol = {
  269. .name = "cache",
  270. .url_open2 = cache_open,
  271. .url_read = cache_read,
  272. .url_seek = cache_seek,
  273. .url_close = cache_close,
  274. .priv_data_size = sizeof(Context),
  275. .priv_data_class = &cache_context_class,
  276. };