/libavformat/cache.c

http://github.com/FFmpeg/FFmpeg · C · 343 lines · 262 code · 48 blank · 33 comment · 65 complexity · 3d88dc92c5eb021f95ff0fb3da513b9c MD5 · raw file

  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/internal.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. char *filename;
  54. struct AVTreeNode *root;
  55. int64_t logical_pos;
  56. int64_t cache_pos;
  57. int64_t inner_pos;
  58. int64_t end;
  59. int is_true_eof;
  60. URLContext *inner;
  61. int64_t cache_hit, cache_miss;
  62. int read_ahead_limit;
  63. } Context;
  64. static int cmp(const void *key, const void *node)
  65. {
  66. return FFDIFFSIGN(*(const int64_t *)key, ((const CacheEntry *) node)->logical_pos);
  67. }
  68. static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
  69. {
  70. int ret;
  71. char *buffername;
  72. Context *c= h->priv_data;
  73. av_strstart(arg, "cache:", &arg);
  74. c->fd = avpriv_tempfile("ffcache", &buffername, 0, h);
  75. if (c->fd < 0){
  76. av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
  77. return c->fd;
  78. }
  79. ret = unlink(buffername);
  80. if (ret >= 0)
  81. av_freep(&buffername);
  82. else
  83. c->filename = buffername;
  84. return ffurl_open_whitelist(&c->inner, arg, flags, &h->interrupt_callback,
  85. options, h->protocol_whitelist, h->protocol_blacklist, h);
  86. }
  87. static int add_entry(URLContext *h, const unsigned char *buf, int size)
  88. {
  89. Context *c= h->priv_data;
  90. int64_t pos = -1;
  91. int ret;
  92. CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
  93. CacheEntry *entry_ret;
  94. struct AVTreeNode *node = NULL;
  95. //FIXME avoid lseek
  96. pos = lseek(c->fd, 0, SEEK_END);
  97. if (pos < 0) {
  98. ret = AVERROR(errno);
  99. av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
  100. goto fail;
  101. }
  102. c->cache_pos = pos;
  103. ret = write(c->fd, buf, size);
  104. if (ret < 0) {
  105. ret = AVERROR(errno);
  106. av_log(h, AV_LOG_ERROR, "write in cache failed\n");
  107. goto fail;
  108. }
  109. c->cache_pos += ret;
  110. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  111. if (!entry)
  112. entry = next[0];
  113. if (!entry ||
  114. entry->logical_pos + entry->size != c->logical_pos ||
  115. entry->physical_pos + entry->size != pos
  116. ) {
  117. entry = av_malloc(sizeof(*entry));
  118. node = av_tree_node_alloc();
  119. if (!entry || !node) {
  120. ret = AVERROR(ENOMEM);
  121. goto fail;
  122. }
  123. entry->logical_pos = c->logical_pos;
  124. entry->physical_pos = pos;
  125. entry->size = ret;
  126. entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
  127. if (entry_ret && entry_ret != entry) {
  128. ret = -1;
  129. av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
  130. goto fail;
  131. }
  132. } else
  133. entry->size += ret;
  134. return 0;
  135. fail:
  136. //we could truncate the file to pos here if pos >=0 but ftruncate isn't available in VS so
  137. //for simplicty we just leave the file a bit larger
  138. av_free(entry);
  139. av_free(node);
  140. return ret;
  141. }
  142. static int cache_read(URLContext *h, unsigned char *buf, int size)
  143. {
  144. Context *c= h->priv_data;
  145. CacheEntry *entry, *next[2] = {NULL, NULL};
  146. int64_t r;
  147. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  148. if (!entry)
  149. entry = next[0];
  150. if (entry) {
  151. int64_t in_block_pos = c->logical_pos - entry->logical_pos;
  152. av_assert0(entry->logical_pos <= c->logical_pos);
  153. if (in_block_pos < entry->size) {
  154. int64_t physical_target = entry->physical_pos + in_block_pos;
  155. if (c->cache_pos != physical_target) {
  156. r = lseek(c->fd, physical_target, SEEK_SET);
  157. } else
  158. r = c->cache_pos;
  159. if (r >= 0) {
  160. c->cache_pos = r;
  161. r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
  162. }
  163. if (r > 0) {
  164. c->cache_pos += r;
  165. c->logical_pos += r;
  166. c->cache_hit ++;
  167. return r;
  168. }
  169. }
  170. }
  171. // Cache miss or some kind of fault with the cache
  172. if (c->logical_pos != c->inner_pos) {
  173. r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
  174. if (r<0) {
  175. av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
  176. return r;
  177. }
  178. c->inner_pos = r;
  179. }
  180. r = ffurl_read(c->inner, buf, size);
  181. if (r == AVERROR_EOF && size>0) {
  182. c->is_true_eof = 1;
  183. av_assert0(c->end >= c->logical_pos);
  184. }
  185. if (r<=0)
  186. return r;
  187. c->inner_pos += r;
  188. c->cache_miss ++;
  189. add_entry(h, buf, r);
  190. c->logical_pos += r;
  191. c->end = FFMAX(c->end, c->logical_pos);
  192. return r;
  193. }
  194. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  195. {
  196. Context *c= h->priv_data;
  197. int64_t ret;
  198. if (whence == AVSEEK_SIZE) {
  199. pos= ffurl_seek(c->inner, pos, whence);
  200. if(pos <= 0){
  201. pos= ffurl_seek(c->inner, -1, SEEK_END);
  202. if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
  203. av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
  204. }
  205. if (pos > 0)
  206. c->is_true_eof = 1;
  207. c->end = FFMAX(c->end, pos);
  208. return pos;
  209. }
  210. if (whence == SEEK_CUR) {
  211. whence = SEEK_SET;
  212. pos += c->logical_pos;
  213. } else if (whence == SEEK_END && c->is_true_eof) {
  214. resolve_eof:
  215. whence = SEEK_SET;
  216. pos += c->end;
  217. }
  218. if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
  219. //Seems within filesize, assume it will not fail.
  220. c->logical_pos = pos;
  221. return pos;
  222. }
  223. //cache miss
  224. ret= ffurl_seek(c->inner, pos, whence);
  225. if ((whence == SEEK_SET && pos >= c->logical_pos ||
  226. whence == SEEK_END && pos <= 0) && ret < 0) {
  227. if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
  228. || c->read_ahead_limit < 0) {
  229. uint8_t tmp[32768];
  230. while (c->logical_pos < pos || whence == SEEK_END) {
  231. int size = sizeof(tmp);
  232. if (whence == SEEK_SET)
  233. size = FFMIN(sizeof(tmp), pos - c->logical_pos);
  234. ret = cache_read(h, tmp, size);
  235. if (ret == AVERROR_EOF && whence == SEEK_END) {
  236. av_assert0(c->is_true_eof);
  237. goto resolve_eof;
  238. }
  239. if (ret < 0) {
  240. return ret;
  241. }
  242. }
  243. return c->logical_pos;
  244. }
  245. }
  246. if (ret >= 0) {
  247. c->logical_pos = ret;
  248. c->end = FFMAX(c->end, ret);
  249. }
  250. return ret;
  251. }
  252. static int enu_free(void *opaque, void *elem)
  253. {
  254. av_free(elem);
  255. return 0;
  256. }
  257. static int cache_close(URLContext *h)
  258. {
  259. Context *c= h->priv_data;
  260. int ret;
  261. av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
  262. c->cache_hit, c->cache_miss);
  263. close(c->fd);
  264. if (c->filename) {
  265. ret = unlink(c->filename);
  266. if (ret < 0)
  267. av_log(h, AV_LOG_ERROR, "Could not delete %s.\n", c->filename);
  268. av_freep(&c->filename);
  269. }
  270. ffurl_close(c->inner);
  271. av_tree_enumerate(c->root, NULL, NULL, enu_free);
  272. av_tree_destroy(c->root);
  273. return 0;
  274. }
  275. #define OFFSET(x) offsetof(Context, x)
  276. #define D AV_OPT_FLAG_DECODING_PARAM
  277. static const AVOption options[] = {
  278. { "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 },
  279. {NULL},
  280. };
  281. static const AVClass cache_context_class = {
  282. .class_name = "cache",
  283. .item_name = av_default_item_name,
  284. .option = options,
  285. .version = LIBAVUTIL_VERSION_INT,
  286. };
  287. const URLProtocol ff_cache_protocol = {
  288. .name = "cache",
  289. .url_open2 = cache_open,
  290. .url_read = cache_read,
  291. .url_seek = cache_seek,
  292. .url_close = cache_close,
  293. .priv_data_size = sizeof(Context),
  294. .priv_data_class = &cache_context_class,
  295. };