Convert README to README.rst for Github co-hosting
[sparrow.git] / load_images.c
blob3e2e68ef9abcedb3a9eb7ca56ed282decb8d5915
1 /* Copyright (C) <2010> Douglas Bagnall <douglas@halo.gen.nz>
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
19 #include "sparrow.h"
20 #include "gstsparrow.h"
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
26 #include <sys/mman.h>
28 static const char *JPEG_BLOB_NAME = "/home/douglas/sparrow/content/jpeg.blob";
29 static const char *JPEG_INDEX_NAME = "/home/douglas/sparrow/content/jpeg.index";
31 INVISIBLE sparrow_shared_t *
32 sparrow_get_shared(void){
33 static sparrow_shared_t shared;
34 return &shared;
37 static gpointer
38 load_images(gpointer p){
39 GST_DEBUG("load_images with pointer %p", p);
40 GstSparrow *sparrow = (GstSparrow *)p;
41 int fd = open(JPEG_BLOB_NAME, O_RDONLY);
42 off_t length = lseek(fd, 0, SEEK_END);
43 lseek(fd, 0, SEEK_SET);
44 GST_DEBUG("about to mmap");
45 void *mem = mmap(NULL, length, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
46 GST_DEBUG("mmap returned %p", mem);
47 madvise(mem, length, POSIX_MADV_WILLNEED);
48 GST_DEBUG("done madvise. sparrow->shared is %p", sparrow->shared);
49 sparrow->shared->jpeg_blob = mem;
50 sparrow->shared->blob_size = length;
52 return mem;
55 static gpointer
56 unload_images(gpointer p){
57 GstSparrow *sparrow = (GstSparrow *)p;
58 sparrow_shared_t *shared = sparrow->shared;
59 munmap(shared->jpeg_blob, shared->blob_size);
60 return NULL;
63 static gpointer
64 load_index(gpointer p){
65 GstSparrow *sparrow = (GstSparrow *)p;
66 int fd = open(JPEG_INDEX_NAME, O_RDONLY);
67 off_t length = lseek(fd, 0, SEEK_END);
68 lseek(fd, 0, SEEK_SET);
69 void *mem = mmap(NULL, length, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
70 GST_DEBUG("mmap returned %p", mem);
71 madvise(mem, length, POSIX_MADV_WILLNEED);
72 sparrow->shared->index = mem;
73 sparrow->shared->image_count = length / sizeof(sparrow_frame_t);
74 GST_DEBUG("found %d frame info structures of size %d\n", sparrow->shared->image_count,
75 sizeof(sparrow_frame_t));
76 return mem;
79 static gpointer
80 unload_index(gpointer p){
81 GstSparrow *sparrow = (GstSparrow *)p;
82 munmap(sparrow->shared->index,
83 sparrow->shared->image_count * sizeof(sparrow_frame_t));
84 return NULL;
88 INVISIBLE void
89 maybe_load_images(GstSparrow *sparrow)
91 GST_DEBUG("maybe load images");
92 static GOnce image_once = G_ONCE_INIT;
93 g_once(&image_once, load_images, sparrow);
96 INVISIBLE void
97 maybe_unload_images(GstSparrow *sparrow){
98 GST_DEBUG("maybe unload images");
99 static GOnce once = G_ONCE_INIT;
100 g_once(&once, unload_images, sparrow);
103 INVISIBLE void
104 maybe_load_index(GstSparrow *sparrow)
106 GST_DEBUG("maybe load index");
107 static GOnce index_once = G_ONCE_INIT;
108 g_once(&index_once, load_index, sparrow);
111 INVISIBLE void
112 maybe_unload_index(GstSparrow *sparrow){
113 static GOnce once = G_ONCE_INIT;
114 g_once(&once, unload_index, sparrow);