-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathGPMF_demo.c
294 lines (247 loc) · 8.17 KB
/
GPMF_demo.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
/*! @file GPMF_demo.c
*
* @brief Demo to extract GPMF from an MP4
*
* @version 1.5.0
*
* (C) Copyright 2017 GoPro Inc (http://gopro.com/).
*
* Licensed under either:
* - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
* - MIT license, http://opensource.org/licenses/MIT
* at your option.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "../GPMF_parser.h"
#include "GPMF_mp4reader.h"
extern void PrintGPMF(GPMF_stream *ms);
int main(int argc, char *argv[])
{
GPMF_ERR ret = GPMF_OK;
GPMF_stream metadata_stream, *ms = &metadata_stream;
double metadatalength;
uint32_t *payload = NULL; //buffer to store GPMF samples from the MP4.
// get file return data
if (argc != 2)
{
printf("usage: %s <file_with_GPMF>\n", argv[0]);
return -1;
}
#if 1 // Search for GPMF Track
size_t mp4 = OpenMP4Source(argv[1], MOV_GPMF_TRAK_TYPE, MOV_GPMF_TRAK_SUBTYPE);
#else // look for a global GPMF payload in the moov header, within 'udta'
size_t mp4 = OpenMP4SourceUDTA(argv[1]); //Search for GPMF payload with MP4's udta
#endif
if (mp4 == 0)
{
printf("error: %s is an invalid MP4/MOV or it has no GPMF data\n", argv[1]);
return -1;
}
metadatalength = GetDuration(mp4);
//If the GPMF streams are using (non-zero) timestamps, which stream should time zero be relative to.
SetTimeBaseStream(mp4, STR2FOURCC("SHUT"));
if (metadatalength > 0.0)
{
uint32_t index, payloads = GetNumberPayloads(mp4);
// printf("found %.2fs of metadata, from %d payloads, within %s\n", metadatalength, payloads, argv[1]);
uint32_t fr_num, fr_dem;
uint32_t frames = GetVideoFrameRateAndCount(mp4, &fr_num, &fr_dem);
if (frames)
{
printf("video framerate is %.2f with %d frames\n", (float)fr_num/(float)fr_dem, frames);
}
#if 1
if (payloads == 1) // Printf the contents of the single payload
{
uint32_t payloadsize = GetPayloadSize(mp4,0);
payload = GetPayload(mp4, payload, 0);
if(payload == NULL)
goto cleanup;
ret = GPMF_Init(ms, payload, payloadsize);
if (ret != GPMF_OK)
goto cleanup;
// Output (printf) all the contained GPMF data within this payload
ret = GPMF_Validate(ms, GPMF_RECURSE_LEVELS); // optional
if (GPMF_OK != ret)
{
printf("Invalid Structure\n");
goto cleanup;
}
GPMF_ResetState(ms);
do
{
PrintGPMF(ms); // printf current GPMF KLV
} while (GPMF_OK == GPMF_Next(ms, GPMF_RECURSE_LEVELS));
GPMF_ResetState(ms);
printf("\n");
}
#endif
for (index = 0; index < payloads; index++)
{
uint32_t payloadsize = GetPayloadSize(mp4, index);
double in = 0.0, out = 0.0; //times
payload = GetPayload(mp4, payload, index);
if (payload == NULL)
goto cleanup;
ret = GetPayloadTime(mp4, index, &in, &out);
if (ret != GPMF_OK)
goto cleanup;
ret = GPMF_Init(ms, payload, payloadsize);
if (ret != GPMF_OK)
goto cleanup;
#if 1 // Find all the available Streams and the data carrying FourCC
if (index == 0) // show first payload
{
ret = GPMF_FindNext(ms, GPMF_KEY_STREAM, GPMF_RECURSE_LEVELS);
while (GPMF_OK == ret)
{
ret = GPMF_SeekToSamples(ms);
if (GPMF_OK == ret) //find the last FOURCC within the stream
{
uint32_t key = GPMF_Key(ms);
GPMF_SampleType type = GPMF_Type(ms);
uint32_t elements = GPMF_ElementsInStruct(ms);
//uint32_t samples = GPMF_Repeat(ms);
uint32_t samples = GPMF_PayloadSampleCount(ms);
if (samples)
{
printf(" STRM of %c%c%c%c ", PRINTF_4CC(key));
if (type == GPMF_TYPE_COMPLEX)
{
GPMF_stream find_stream;
GPMF_CopyState(ms, &find_stream);
if (GPMF_OK == GPMF_FindPrev(&find_stream, GPMF_KEY_TYPE, GPMF_CURRENT_LEVEL))
{
char tmp[64];
char *data = (char *)GPMF_RawData(&find_stream);
uint32_t size = GPMF_RawDataSize(&find_stream);
if (size < sizeof(tmp))
{
memcpy(tmp, data, size);
tmp[size] = 0;
printf("of type %s ", tmp);
}
}
}
else
{
printf("of type %c ", type);
}
printf("with %d sample%s ", samples, samples > 1 ? "s" : "");
if (elements > 1)
printf("-- %d elements per sample", elements);
printf("\n");
}
ret = GPMF_FindNext(ms, GPMF_KEY_STREAM, GPMF_RECURSE_LEVELS);
}
else
{
if (ret == GPMF_ERROR_BAD_STRUCTURE) // some payload element was corrupt, skip to the next valid GPMF KLV at the previous level.
{
ret = GPMF_Next(ms, GPMF_CURRENT_LEVEL); // this will be the next stream if any more are present.
}
}
}
GPMF_ResetState(ms);
printf("\n");
}
#endif
#if 1 // Find GPS values and return scaled doubles.
if (index == 0) // show first payload
{
// if (GPMF_OK == GPMF_FindNext(ms, STR2FOURCC("GPS5"), GPMF_RECURSE_LEVELS) || //GoPro Hero5/6/7 GPS
// GPMF_OK == GPMF_FindNext(ms, STR2FOURCC("GPRI"), GPMF_RECURSE_LEVELS)) //GoPro Karma GPS
if (GPMF_OK == GPMF_FindNext(ms, STR2FOURCC("ACCL"), GPMF_RECURSE_LEVELS)) //GoPro Hero5/6/7 Accelerometer
{
uint32_t key = GPMF_Key(ms);
uint32_t samples = GPMF_Repeat(ms);
uint32_t elements = GPMF_ElementsInStruct(ms);
uint32_t buffersize = samples * elements * sizeof(double);
GPMF_stream find_stream;
double *ptr, *tmpbuffer = (double *)malloc(buffersize);
char units[10][6] = { "" };
uint32_t unit_samples = 1;
printf("MP4 Payload time %.3f to %.3f seconds\n", in, out);
if (tmpbuffer && samples)
{
uint32_t i, j;
#if 0 // Sample code for changing GPMF KLVs
GPMF_CopyState(ms, &find_stream);
if (GPMF_OK == GPMF_FindPrev(&find_stream, GPMF_KEY_SCALE, GPMF_CURRENT_LEVEL))
{
float scale = 4096.0;
if (GPMF_OK == GPMF_Modify(&find_stream,
GPMF_KEY_SCALE, GPMF_KEY_SCALE, 'f', 4, 1, (void*)&scale))
{
printf("change scale\n");
uint32_t ret = 0;
ret = WritePayload(mp4, payload, payloadsize, index);
if (ret != 0)
{
printf("updated file data\n");
}
}
}
#endif
//Search for any units to display
GPMF_CopyState(ms, &find_stream);
if (GPMF_OK == GPMF_FindPrev(&find_stream, GPMF_KEY_SI_UNITS, GPMF_CURRENT_LEVEL) ||
GPMF_OK == GPMF_FindPrev(&find_stream, GPMF_KEY_UNITS, GPMF_CURRENT_LEVEL))
{
char *data = (char *)GPMF_RawData(&find_stream);
uint32_t ssize = GPMF_StructSize(&find_stream);
unit_samples = GPMF_Repeat(&find_stream);
for (i = 0; i < unit_samples; i++)
{
memcpy(units[i], data, ssize);
units[i][ssize] = 0;
data += ssize;
}
}
//GPMF_FormattedData(ms, tmpbuffer, buffersize, 0, samples); // Output data in LittleEnd, but no scale
GPMF_ScaledData(ms, tmpbuffer, buffersize, 0, samples, GPMF_TYPE_DOUBLE); //Output scaled data as floats
ptr = tmpbuffer;
for (i = 0; i < samples; i++)
{
printf("%c%c%c%c ", PRINTF_4CC(key));
for (j = 0; j < elements; j++)
printf("%.3f%s, ", *ptr++, units[j%unit_samples]);
printf("\n");
}
free(tmpbuffer);
}
}
GPMF_ResetState(ms);
printf("\n");
}
#endif
}
#if 1
// Find all the available Streams and compute they sample rates
while (GPMF_OK == GPMF_FindNext(ms, GPMF_KEY_STREAM, GPMF_RECURSE_LEVELS))
{
if (GPMF_OK == GPMF_SeekToSamples(ms)) //find the last FOURCC within the stream
{
double start, end;
uint32_t fourcc = GPMF_Key(ms);
double rate = GetGPMFSampleRate(mp4, fourcc, GPMF_SAMPLE_RATE_PRECISE, &start, &end);// GPMF_SAMPLE_RATE_FAST);
printf("%c%c%c%c sampling rate = %fHz (time %f to %f)\",\n", PRINTF_4CC(fourcc), rate, start, end);
}
}
#endif
cleanup:
if (payload) FreePayload(payload); payload = NULL;
CloseSource(mp4);
}
return (int) ret;
}