File: framepool.cpp

package info (click to toggle)
dvr 3.2-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 896 kB
  • ctags: 394
  • sloc: cpp: 3,192; makefile: 134; sh: 100; yacc: 39
file content (196 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*********************************************************************************************************
DVR, Digital Video Recorder - a tool to record movies (audio/video), using realtime compression

It uses libavifile (see http://divx.euro.ru) and some code from kwintv (see wenk@mathematik.uni-kl.de)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, etc.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, etc.

copyright(C) february 2001, Pierre Hbert (pierre.hebert@netcourrier.com)
*********************************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "framepool.h"


FramePool::FramePool(int _width, int _height, int _bpp, int _max_frames):
	width(_width), height(_height), bpp(_bpp), opp((_bpp+7)/8), top_margin(0), bottom_margin(0),
	max_frames(_max_frames), previous_frame(NULL), newest(0), oldest(0), nb_frames(0)
{	
	frame_size=width*height*opp;
	frames=new unsigned char[max_frames*frame_size];
  frames_null=new bool[max_frames];

	sem_id=semget(IPC_PRIVATE, 2, IPC_CREAT | 0666 );

	semctl(sem_id, SEM_ID_FRAME_AVAILABLE, SETVAL, 0);
	semctl(sem_id, SEM_ID_MUTEX, SETVAL, 1);
}

FramePool::~FramePool() {
	delete []frames_null;
	delete []frames;
	semctl(sem_id, 0, IPC_RMID, 0);
}

void FramePool::addFrame(unsigned char *src) {
  unsigned char *d=frames+frame_size*newest;
  if(src) {
//#define FLIP_IMAGE
#ifdef FLIP_IMAGE
/*    int line_size=width*opp;
    unsigned char *s=src+(height-bottom_margin-1)*line_size;
    for(int i=0; i<(height-top_margin-bottom_margin);
      s-=line_size,d+=line_size, i++) {
      memcpy(d, s, line_size);
    }*/
    unsigned char *s=src;
    int w=width;
    int h=height-top_margin-bottom_margin;
    for(int i=0; i<h; i++) {
      for(int j=0; j<w; j++) {
        d[j]=s[w-j-1];
      }
      d+=w;
      s+=w;
    }
    w=width/2;
    for(int i=0; i<h/2; i++) {
      for(int j=0; j<w; j++) {
        d[j]=s[w-j-1];
      }
      d+=w;
      s+=w;
    }
    for(int i=0; i<h/2; i++) {
      for(int j=0; j<w; j++) {
        d[j]=s[w-j-1];
      }
      d+=w;
      s+=w;
    }

#else
    memcpy(d, src, width*height*opp);
#endif
    frames_null[newest]=false;
  } else {
    frames_null[newest]=true;
  }

	previous_frame=d;

	struct sembuf op;
	op.sem_flg=0;
	op.sem_num=SEM_ID_MUTEX;op.sem_op=-1;semop(sem_id, &op, 1);
	nb_frames++;
	op.sem_num=SEM_ID_MUTEX;op.sem_op=1;semop(sem_id, &op, 1);
	
	// signal new frame available
	op.sem_num=SEM_ID_FRAME_AVAILABLE;
	op.sem_op=1;
	semop(sem_id, &op, 1);
	
	newest++;
	if(newest==max_frames) newest=0;
}

/*void FramePool::addSameFrame() {
	if(previous_frame) {
		unsigned char *c=frames+frame_size*newest;
		memcpy(c, previous_frame, width*height*opp);
		previous_frame=c;

		struct sembuf op;
		op.sem_flg=0;
		op.sem_num=SEM_ID_MUTEX;op.sem_op=-1;semop(sem_id, &op, 1);
		nb_frames++;
		op.sem_num=SEM_ID_MUTEX;op.sem_op=1;semop(sem_id, &op, 1);
	
		// signal new frame available
		op.sem_num=SEM_ID_FRAME_AVAILABLE;
		op.sem_op=1;
		semop(sem_id, &op, 1);
	
		newest++;
		if(newest==max_frames) newest=0;
	}
}*/

unsigned char *FramePool::getFrame() {
	// wait for a frame to be available     
	struct sembuf op;
	op.sem_num=SEM_ID_FRAME_AVAILABLE;
	op.sem_op=-1;op.sem_flg=0;
	semop(sem_id, &op, 1);
		
	unsigned char *c;

  if(frames_null[oldest]) {
    c=NULL;
  } else {
    c=frames+frame_size*oldest;
  }
	
	oldest++;
	if(oldest==max_frames) oldest=0;

	return c;
}

void FramePool::removeFrame() {
	struct sembuf op;
	op.sem_flg=0;
	op.sem_num=SEM_ID_MUTEX;op.sem_op=-1;semop(sem_id, &op, 1);
	nb_frames--;
	op.sem_num=SEM_ID_MUTEX;op.sem_op=1;semop(sem_id, &op, 1);
}

int FramePool::size() const {
	return nb_frames;
}

int FramePool::max_size() const {
	return max_frames;
}

bool FramePool::full() const {
	return nb_frames==max_frames;
}

void FramePool::setTopMargin(int m) {
	if(m<0) {
		top_margin=0;
	} 
	else if(m>(height-bottom_margin)) {
		top_margin=height-bottom_margin;
	} 
	else {
		top_margin=m;
	}
}

int FramePool::getTopMargin() const {
	return top_margin;
}

void FramePool::setBottomMargin(int m) {
	if(m<0) {
		bottom_margin=0;
	} 
	else if(m>(height-top_margin)) {
		bottom_margin=height-top_margin;
	} 
	else {
		bottom_margin=m;
	}
}

int FramePool::getBottomMargin() const {
	return bottom_margin;
}