JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
VideoBuf.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#include <sys/mman.h>
19#include <fstream>
20
22#include <jevois/Debug/Log.H>
23
24#include <unistd.h> // for close()
25
26// ####################################################################################################
27jevois::VideoBuf::VideoBuf(int const fd, size_t const length, unsigned int offset, int const dmafd) :
28 itsFd(fd), itsLength(length), itsBytesUsed(0), itsDmaBufFd(dmafd)
29{
30 if (itsFd > 0)
31 {
32 // mmap the buffer to any address:
33#ifdef JEVOIS_PLATFORM_A33
34 // PROT_EXEC needed for clearcache() to work:
35 itsAddr = mmap(NULL, length, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd, offset);
36#else
37 itsAddr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
38#endif
39 if (itsAddr == MAP_FAILED) PLFATAL("Unable to map buffer");
40 }
41 else
42 {
43 // Simple memory allocation:
44 itsAddr = reinterpret_cast<void *>(new char[length]);
45 }
46}
47
48// ####################################################################################################
50{
51 if (itsFd > 0)
52 {
53 if (munmap(itsAddr, itsLength) < 0) PLERROR("munmap failed");
54 }
55 else
56 {
57 delete [] reinterpret_cast<char *>(itsAddr);
58 }
59
60 if (itsDmaBufFd > 0) close(itsDmaBufFd);
61}
62
63// ARMv7 code that only runs on JeVois-A33 platform
64void clearcache(char* begin, char *end)
65{
66#ifdef JEVOIS_PLATFORM_A33
67 int const syscall = 0xf0002;
68 __asm __volatile (
69 "mov r0, %0\n"
70 "mov r1, %1\n"
71 "mov r7, %2\n"
72 "mov r2, #0x0\n"
73 "svc 0x00000000\n"
74 :
75 : "r" (begin), "r" (end), "r" (syscall)
76 : "r0", "r1", "r7"
77 );
78#else
79 // Just keep compiler happy:
80 (void)begin; (void)end;
81#endif
82}
83
84// ####################################################################################################
86{
87#ifdef JEVOIS_PLATFORM_A33
88 if (itsFd > 0)
89 {
90 // This does nothing useful for us:
91 // msync(itsAddr, itsLength, MS_SYNC | MS_INVALIDATE);
92
93 // This works ok but a bit brutal:
94 std::ofstream ofs("/proc/sys/vm/drop_caches");
95 if (ofs.is_open() == false) { LERROR("Cannot flush cache -- IGNORED"); return; }
96 ofs << "1" << std::endl;
97
98 // Here is some arm-specific code (only works on JeVois-A33 platform):
99 clearcache(reinterpret_cast<char *>(itsAddr), reinterpret_cast<char *>(itsAddr) + itsLength);
100 }
101#endif
102}
103
104// ####################################################################################################
106{
107 return itsAddr;
108}
109
110// ####################################################################################################
112{
113 return itsLength;
114}
115
116// ####################################################################################################
118{
119 itsBytesUsed = n;
120}
121
122// ####################################################################################################
124{
125 return itsBytesUsed;
126}
127
128// ####################################################################################################
130{
131 return itsDmaBufFd;
132}
void clearcache(char *begin, char *end)
Definition VideoBuf.C:64
size_t bytesUsed() const
Get the number of bytes used, valid only for MJPEG images.
Definition VideoBuf.C:123
~VideoBuf()
Destructor unmaps the memory.
Definition VideoBuf.C:49
VideoBuf(int const fd, size_t const length, unsigned int offset, int const dmafd)
Construct and allocate MMAP'd memory.
Definition VideoBuf.C:27
void setBytesUsed(size_t n)
Set the number of bytes used, eg, for MJPEG images that application code compressed into the buffer.
Definition VideoBuf.C:117
size_t length() const
Get the allocated memory length.
Definition VideoBuf.C:111
void sync()
Sync the data.
Definition VideoBuf.C:85
void * data() const
Get a pointer to the buffer data.
Definition VideoBuf.C:105
int dmaFd() const
Get the dma_buf fd associated with this buffer, which was given at construction.
Definition VideoBuf.C:129
#define PLERROR(msg)
Like LERROR but appends errno and strerror(errno), to be used when some system call fails.
Definition Log.H:219
#define PLFATAL(msg)
Like LDEBUG but appends errno and strerror(errno), to be used when some system call fails.
Definition Log.H:239
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211