forked from lbrandy/ffmpeg-fas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek_indices.c
319 lines (241 loc) · 8.63 KB
/
seek_indices.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*****************************************************************************
* Copyright 2008. Pittsburgh Pattern Recognition, Inc.
*
* This file is part of the Frame Accurate Seeking extension library to
* ffmpeg (ffmpeg-fas).
*
* ffmpeg-fas 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 3 of the License, or (at your
* option) any later version.
*
* The ffmpeg-fas 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 the ffmpeg-fas library. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "seek_indices.h"
#include "private_errors.h"
/**** Defines *****************************************************************/
#define DEFAULT_INITIAL_SIZE 100
static seek_error_type private_show_error (const char *message, seek_error_type error);
static seek_error_type private_resize_table (seek_table_type *table, int new_size);
/*
* seek_init_table
*/
int compare_seek_tables(seek_table_type t1, seek_table_type t2)
{
int i;
// printf("n_entries=(%d %d)\n", t1.num_entries, t2.num_entries);
if (t1.num_entries != t2.num_entries)
return 0;
if (t1.completed != t2.completed)
return 0;
if (t1.num_frames != t2.num_frames)
return 0;
for (i=0;i<t1.num_entries;i++)
{
// printf("(%d %d) (%lld %lld) (%lld %lld)\n",
// t1.array[i].display_index, t2.array[i].display_index,
// t1.array[i].decode_time, t2.array[i].decode_time,
// t1.array[i].display_time, t2.array[i].display_time);
if ((t1.array[i].display_index != t2.array[i].display_index) ||
(t1.array[i].last_packet_dts != t2.array[i].last_packet_dts) ||
(t1.array[i].first_packet_dts != t2.array[i].first_packet_dts))
return 0;
}
return 1;
}
seek_table_type seek_init_table (int initial_size)
{
seek_table_type table;
if (initial_size < 0)
initial_size = DEFAULT_INITIAL_SIZE;
table.num_entries = 0;
table.num_frames = -1;
table.completed = seek_false;
table.array = (seek_entry_type *)malloc (initial_size * sizeof(seek_entry_type));
if (NULL == table.array)
table.allocated_size = 0;
else
table.allocated_size = initial_size;
return table;
}
/*
* seek_release_table
*/
void seek_release_table (seek_table_type *table)
{
table->num_entries = 0;
table->num_frames = -1;
table->completed = seek_false;
if (NULL == table || NULL == table->array)
return;
free (table->array);
return;
}
/*
* seek_copy_table
*/
seek_table_type seek_copy_table (seek_table_type source)
{
seek_table_type dest;
dest.num_entries = source.num_entries;
dest.num_frames = source.num_frames;
dest.completed = source.completed;
if (NULL == source.array)
{
dest.array = NULL;
dest.allocated_size = 0;
return dest;
}
dest.array = (seek_entry_type *)malloc (source.num_entries * sizeof(seek_entry_type));
if (NULL == dest.array)
{
dest.array = NULL;
dest.allocated_size = 0;
return dest;
}
dest.allocated_size = source.num_entries;
int i;
for (i=0;i<source.num_entries;i++)
dest.array[i] = source.array[i];
return dest;
}
seek_error_type seek_append_table_entry (seek_table_type *table, seek_entry_type entry)
{
if (NULL == table || NULL == table->array)
return private_show_error("null or invalid seek table", seek_bad_argument);
if (table->num_entries != 0)
if (table->array[table->num_entries - 1].display_index >= entry.display_index)
return seek_no_error;
if (table->num_entries == table->allocated_size)
{
seek_error_type error = private_resize_table (table, table->num_entries * 2);
if (error != seek_no_error)
return private_show_error ("unable to resize seek table", error);
}
table->array[table->num_entries] = entry;
table->num_entries++;
return seek_no_error;
}
/*
* seek_get_nearest_entry
*/
seek_error_type seek_get_nearest_entry (seek_table_type *table, seek_entry_type *entry, int display_index, int offset)
{
/* using offset>0 returns a modified seek_entry that sets the 'time-to-seek' to be $offset keyframes in the past.
*/
if (NULL == table || NULL == table->array || table->num_entries <= 0) {
return private_show_error ("NULL or invalid seek table", seek_bad_argument);
}
if (NULL == entry) {
return private_show_error ("NULL entry buffer (for return)", seek_bad_argument);
}
if (display_index < table->array[0].display_index)
return private_show_error ("tried to seek to frame index before first frame", seek_bad_argument);
int i;
for (i=0; i < table->num_entries; i++)
if (table->array[i].display_index > display_index)
break;
i = i-1;
if (i<offset) /* target was lower than first element (including offset) */
return private_show_error ("target index out of table range (too small)", seek_bad_argument);
*entry = table->array[i];
(*entry).first_packet_dts = table->array[i-offset].first_packet_dts;
return seek_no_error;
}
/* read raw file */
seek_table_type read_table_file(char *name)
{
seek_table_type ans = { NULL, (seek_boolean_type) 0, (seek_boolean_type) 0 };
FILE *table_file = fopen(name, "r");
if (table_file == NULL)
return ans;
int completed_flag;
fscanf(table_file, "%d %d %d\n", &ans.num_frames, &ans.num_entries, &completed_flag);
if (completed_flag == 1)
ans.completed = seek_true;
else
ans.completed = seek_false;
ans.allocated_size = ans.num_entries;
ans.array = (seek_entry_type*) malloc (ans.allocated_size * sizeof(seek_entry_type));
int i;
for (i=0;i<ans.num_entries;i++)
fscanf(table_file, "%d %lld %lld\n", &(ans.array[i].display_index), &(ans.array[i].first_packet_dts), &(ans.array[i].last_packet_dts));
fclose(table_file);
return ans;
}
seek_error_type seek_show_raw_table (FILE* file, seek_table_type table)
{
seek_entry_type *entry;
int index;
if (NULL == table.array || table.num_entries <= 0)
return private_show_error ("NULL or invalid seek table", seek_bad_argument);
int completed_flag = 0;
if (table.completed == seek_true)
completed_flag = 1;
fprintf(file, "%d %d %d\n", table.num_frames, table.num_entries, completed_flag);
for (index = 0; index < table.num_entries; index++)
{
entry = &(table.array[index]);
fprintf (file, "%d %lld %lld\n", entry->display_index, entry->first_packet_dts, entry->last_packet_dts);
}
return seek_no_error;
}
seek_error_type seek_show_table (seek_table_type table)
{
seek_entry_type *entry;
int index;
if (NULL == table.array || table.num_entries <= 0) {
return private_show_error ("NULL or invalid seek table", seek_bad_argument);
}
int completed_flag = 0;
if (table.completed == seek_true)
completed_flag = 1;
fprintf (stderr, "--- Seek Table Dump ---\n");
fprintf (stderr, "n_frames: %d n_entries: %d completed: %d\n",table.num_frames, table.num_entries, completed_flag);
for (index = 0; index < table.num_entries; index++)
{
entry = &(table.array[index]);
fprintf (stderr, " %04d --> %08lld (%08lld)\n", entry->display_index, entry->first_packet_dts, entry->last_packet_dts);
}
fprintf (stderr, "-----------------------\n");
return seek_no_error;
}
/*
* private_show_error
*/
static seek_error_type private_show_error (const char *message, seek_error_type error)
{
if (SHOW_ERROR_MESSAGES)
fprintf (stderr, " ===> seek_indices: %s\n", message);
return error;
}
/*
* private_resize_table
*/
static seek_error_type private_resize_table (seek_table_type *table, int new_size)
{
seek_entry_type *new_array = NULL;
if (table == NULL || new_size < 0) {
return private_show_error ("invalid argument for private_resize_table()", seek_malloc_failed);
}
new_array = (seek_entry_type *)malloc (sizeof (seek_entry_type) * new_size);
if (NULL == new_array) {
return private_show_error ("unable to allocate more space for table", seek_malloc_failed);
}
memcpy (new_array, table->array, table->allocated_size * sizeof (seek_entry_type));
free (table->array);
table->allocated_size = new_size;
table->array = new_array;
return seek_no_error;
}