This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFrameBuffer.c
226 lines (202 loc) · 5.86 KB
/
FrameBuffer.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* FrameBuffer.c
* Created by Graham Booker on 1/30/07.
*
* This file is part of Perian.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libavcodec/avcodec.h>
#include <sys/param.h>
#include "FrameBuffer.h"
#include "CommonUtils.h"
void FFusionDataSetup(FFusionData *data, int dataSize, int bufferSize)
{
memset(data, 0, sizeof(FFusionData));
int framesSize = sizeof(FrameData) * dataSize;
data->frames = malloc(framesSize);
memset(data->frames, 0, framesSize);
int i;
for(i=0; i<dataSize; i++)
{
FrameData *fdata = data->frames + i;
fdata->buffer = NULL;
fdata->data = data;
}
data->frameSize = dataSize;
data->ringBuffer = av_malloc(bufferSize);
data->ringSize = bufferSize;
}
void FFusionDataFree(FFusionData *data)
{
free(data->frames);
if(data->previousData != NULL)
{
FFusionDataFree(data->previousData);
free(data->previousData);
}
av_free(data->ringBuffer);
}
//Expands both the frame array and the ring buffer.
//Old data is kept in the previousData pointer to be dealloced when done
static void expansion(FFusionData *data, int dataSize)
{
//Create the prev structure to hold all existing frames
FFusionData *prev = malloc(sizeof(FFusionData));
//Move all frames to prev
memcpy(prev, data, sizeof(FFusionData));
int i;
for(i=0; i<data->frameSize; i++)
data->frames[i].data = prev;
//Create new data
int newRingSize = MAX(dataSize * 10, data->ringSize * 2);
FFusionDataSetup(data, data->frameSize * 2, newRingSize);
//Preserve pointer to old data
data->previousData = prev;
}
uint8_t *FFusionCreateEntireDataBuffer(FFusionData *data, uint8_t *buffer, int bufferSize)
{
data->ringBuffer = fast_realloc_with_padding(data->ringBuffer, &data->ringSize, bufferSize);
if (data->ringBuffer) {
memcpy(data->ringBuffer, buffer, bufferSize);
}
return data->ringBuffer;
}
//Find dataSize bytes in ringbuffer, expand if none available
static uint8_t *createBuffer(FFusionData *data, int dataSize)
{
if(data->ringWrite >= data->ringRead)
{
//Write is after read
if(data->ringWrite + dataSize + FF_INPUT_BUFFER_PADDING_SIZE < data->ringSize)
{
//Found at end
int offset = data->ringWrite;
data->ringWrite = offset + dataSize;
return data->ringBuffer + offset;
}
else
//Can't fit at end, loop
data->ringWrite = 0;
}
if(data->ringWrite + dataSize + FF_INPUT_BUFFER_PADDING_SIZE < data->ringRead)
{
//Found at write
int offset = data->ringWrite;
data->ringWrite = offset + dataSize;
return data->ringBuffer + offset;
}
else
{
expansion(data, dataSize);
data->ringWrite = dataSize;
return data->ringBuffer;
}
}
//Insert buffer into ring buffer
static uint8_t *insertIntoBuffer(FFusionData *data, uint8_t *buffer, int dataSize)
{
uint8_t *ret = createBuffer(data, dataSize);
memcpy(ret, buffer, dataSize);
memset(ret + dataSize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
return ret;
}
FrameData *FFusionDataAppend(FFusionData *data, uint8_t *buffer, int dataSize, int type)
{
//Find an available frame
if((data->frameWrite + 1) % data->frameSize == data->frameRead)
{
expansion(data, dataSize);
}
FrameData *dest = data->frames + data->frameWrite;
if(data->unparsedFrames.buffer == buffer)
{
//This was an unparsed frame, don't memcpy; it's already in the correct place.
dest->buffer = buffer;
data->unparsedFrames.buffer += dataSize;
data->unparsedFrames.dataSize -= dataSize;
}
else
{
uint8_t *saveBuffer = insertIntoBuffer(data, buffer, dataSize);
dest->buffer = saveBuffer;
}
dest->dataSize = dataSize;
dest->type = type;
dest->prereqFrame = NULL;
dest->decoded = FALSE;
dest->nextFrame = NULL;
data->frameWrite = (data->frameWrite + 1) % data->frameSize;
return dest;
}
void FFusionDataSetUnparsed(FFusionData *data, uint8_t *buffer, int bufferSize)
{
FrameData *unparsed = &(data->unparsedFrames);
if(unparsed->buffer == buffer)
{
//This part was already unparsed; don't memcpy again
unparsed->dataSize = bufferSize;
}
else
{
unparsed->buffer = insertIntoBuffer(data, buffer, bufferSize);
if (unparsed->buffer) {
unparsed->dataSize = bufferSize;
}
}
}
//Seems to be unused
void FFusionDataReadUnparsed(FFusionData *data)
{
data->ringWrite -= data->unparsedFrames.dataSize;
data->unparsedFrames.dataSize = 0;
}
FrameData *FrameDataCheckPrereq(FrameData * toData)
{
FrameData *prereq = toData->prereqFrame;
if(prereq && prereq->decoded)
return NULL;
return prereq;
}
void FFusionDataMarkRead(FrameData *toData)
{
if(toData == NULL)
return;
if(toData->prereqFrame != NULL)
return;
FFusionData *data = toData->data;
data->frameRead = toData - data->frames + 1;
data->ringRead = toData->buffer + toData->dataSize - data->ringBuffer;
if(data->previousData != NULL)
{
//If there's previous data, free it since we are now done with it
FFusionDataFree(data->previousData);
free(data->previousData);
data->previousData = NULL;
}
}
FrameData *FFusionDataFind(FFusionData *data, int frameNumber)
{
int i;
for(i=data->frameRead; i!=data->frameWrite; i = (i + 1) % data->frameSize)
{
if(data->frames[i].frameNumber == frameNumber)
return data->frames + i;
}
if(data->previousData != NULL)
//Check previous data as well
return FFusionDataFind(data->previousData, frameNumber);
return NULL;
}