-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm4mudex.cc
359 lines (317 loc) · 11.3 KB
/
m4mudex.cc
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/***
* Removes the meta boxes from the provided mpeg4 source file, writing the result
* out to a new file given the provided filename.
*
* This sample program only works for media files that stay within 32-bit box/file sizes.
* If the file is larger than the 32-bit maximum, the following additions could be made:
*
* TODO
* 1. Check for 64-bit boxes size encoding (atom size=1) and adjust accordingly.
* 2. Find the co64 table instead of stco table, and modify those table entries.
*/
#include "stdio.h"
#include "stdint.h"
#include "stdlib.h"
#include "stddef.h"
#include "string.h"
#include <vector>
/* M4A atoms can be either data holders, or containers of other
* atoms. Actually, it's slightly more complicated than that, since there
* are a few choice atoms that are containers that also have data inside of them.
* However, we don't need to worry about this in this utility, since the only
* container that has that property is the "meta" container, but we're just
* removing it, anyway; we don't need to recurse into it.
*
* According to the docs for the m4a file type, we can find meta in the following
* places in the hierarchy:
* meta
* moov.udta.meta
* moov.udta.trak.meta
*
* So, based on that information, we track make sure to check the
* substructure of the necessary containers.
*
* If we were not stripping the meta box, it may have also been necessary to adjust
* values in the 'iloc' and 'dref' sub-boxes of the meta box, not sure.
*/
const char *const containers_of_interest = "moov|udta|trak|mdia|minf|stbl";
typedef struct atom_t {
atom_t* parent;
uint32_t len;
char name[5];
uint32_t data_size;
int32_t data_remaining;
unsigned char* data;
std::vector<atom_t*> children;
bool active;
} atom_t;
/***
* Find the next box (atom) starting from the current
* position of the provided m4a file.
*
* Allocates memory for the atom if necessary, and returns
* a new atom_t with information about the new atom.
*/
atom_t* get_next_box(FILE* m4a_file) {
atom_t *atom = (atom_t*)malloc(sizeof(atom_t));
/* Read size in big-endian order */
fread(&atom->len, 4, 1, m4a_file);
atom->active = true;
atom->len = htonl(atom->len);
fread(atom->name, 1, 4, m4a_file);
/* If the standard length word is 1, then we
* expect an 8-byte length immediately follow
* the name.
*
* Also the header is effectively 16 bytes now. */
//if(atom->len == 1) {
/* Read in big-endian order */
// for(i = 7; i >= 0; i--) {
// fread(&(atom->len.bytes[i]), 1, 1, m4a_file);
// }
// atom->data_size = atom->len.word - 16;
//} else {
atom->data_size = atom->len - 8;
//}
/* Initialize the struct depending on whether
* it's a container of interest or just a
* data blob to pass through.
*/
if(strstr(containers_of_interest, atom->name) != 0) {
//If it's a container, mark the size in data_remaining so the main loop
//knows how much to process
atom->data = NULL;
atom->data_remaining = atom->data_size;
} else {
//Otherwise, just throw the data in a char blob
//to dump back out later
atom->data = (unsigned char*)malloc(atom->data_size);;
fread(atom->data, atom->data_size, 1, m4a_file);
atom->data_remaining = 0;
}
return atom;
}
// A little function to look for meta tags in a less structured way
// Just used for testing
// Could possibly result in a false positive if the "meta" tag appears
// in binary data by chance, although my hunch is that this is unlikely.
int find_meta(FILE *m4a_file) {
const char* target = "meta";
unsigned int tmp;
uint32_t i;
for(i = 0; i < strlen(target); i++) {
tmp <<= 8;
tmp |= (unsigned char)target[i];
}
const unsigned int target_checksum = tmp; //Sum of characters in "meta"
uint32_t checksum = 0;
unsigned char c = 0;
unsigned char buffer[8] = {0,0,0,0,0,0,0,0};
int index = 0;
while(fread(&c, 1, 1, m4a_file) > 0) {
index++;
buffer[index & 7] = c;
checksum <<= 8;
checksum |= c;
if(checksum == target_checksum) {
return index;
}
}
return -1;
}
/* Print out the atom tree representation
* on stdout
*/
void print_tree_rec(atom_t* node, uint8_t level) {
uint8_t i;
for(i=1; i<level; i++) {
printf(".");
}
//skip root content, it's not *really* an atom
if(node->parent != NULL) {
printf("%u %s", node->len, node->name);
if(strncmp(node->name, "stco", 4) == 0) {
uint32_t stco_entries = htonl(*((uint32_t*)(node->data + 4)));
printf(" (%d entries)", stco_entries);
for(i=0; i <= (10 > stco_entries ? stco_entries : 10); i++) {
printf(" %d ", htonl(*((uint32_t*)(node->data + 8 + 4*i))));
}
if(stco_entries > 10) {
printf("...");
}
}
printf("\n");
}
for(i=0; i<node->children.size(); i++) {
if(node->children[i]->active == true) {
print_tree_rec(node->children[i], level+1);
}
}
}
void print_tree(atom_t* node) {
print_tree_rec(node, 0);
}
//Write the atoms back out to file.
void output_tree(atom_t* node, FILE *out_file) {
uint32_t i;
//skip root content, it's not *really* an atom
if(node->parent != NULL) {
uint32_t out_len = htonl(node->len);
fwrite(&out_len, 4, 1, out_file);
fwrite(node->name, 4, 1, out_file);
if(node->data_size > 0 && node->data != NULL) {
fwrite(node->data, node->data_size, 1, out_file);
}
}
for(i=0; i < node->children.size(); i++) {
if(node->children[i]->active == true) {
output_tree(node->children[i], out_file);
}
}
}
//Given an atom that we expect to be a stco block,
//and an offset_adjustment, fix the data portion of the
//atom so that the offsets are reduced by the adjustment
//amount
void adjust_stco_offset(atom_t *stco, int offset_adjust) {
int i;
//Offset past the version byte
unsigned char* stco_data_ptr = (stco->data + 4);
int stco_entries = htonl(*((uint32_t*)(stco_data_ptr)));
//Ofset version bytes and length bytes
stco_data_ptr = stco->data + 8;
//Read the bytes in big-endian order,
//subtract offset,
//write back out.
for(i = 0; i < stco_entries; i++) {
uint32_t stco_offset = htonl(*((uint32_t*)(stco_data_ptr)));
stco_offset -= offset_adjust;
*((uint32_t*)stco_data_ptr) = htonl(stco_offset);
stco_data_ptr += 4;
}
}
//Strip meta boxes, returning the size of
//meta tags before mdat boxes for later
//adjustment.
//It adds up all meta box sizes occurring before mdat, and returns them in accumulator
//That value can subsequently be used to adjust other chunk offsets.
void strip_meta_box_rec(atom_t *node, bool do_accumulate, uint32_t &accumulator, atom_t **stco) {
uint32_t i;
if(strncmp(node->name, "mdat", 4) == 0) {
do_accumulate = false;
} else if(strncmp(node->name, "stco", 4) == 0) {
*stco = node;
} else if(do_accumulate && strncmp(node->name, "meta", 4) == 0) {
accumulator += node->len;
node->active = false;
//Fix up this meta box's parent box sizes
atom_t *cur = node->parent;
while(cur != NULL) {
cur->len -= node->len;
cur = cur->parent;
}
}
for(i = 0; i < node->children.size(); i++) {
strip_meta_box_rec(node->children[i], do_accumulate, accumulator, stco);
}
}
void strip_meta_box(atom_t *node) {
uint32_t offset_adjust = 0;
atom_t* stco;
strip_meta_box_rec(node, true, offset_adjust, &stco);
adjust_stco_offset(stco, offset_adjust);
}
//Create a representation of the tree structure of the atoms
//This function is called recursively. If an atom is marked as a
//container, move through the data section of the atom sub-atom
//at a time, otherwise just dump the whole data thing into a blob.
atom_t* build_tree(FILE* m4a_file) {
//Place to hold the current working atom.
atom_t *atom;
//Create an abstract root node to hold the top-level
//atom list.
atom_t *root = (atom_t*)calloc(sizeof(atom_t), 1);
atom_t *current_parent = root;
/* Loop through the rest of the atoms */
while((atom = get_next_box(m4a_file))->len > 0) {
//Set the parent of the newly created atom
atom->parent = current_parent;
//Add new atom to the current parent list.
current_parent->children.push_back(atom);
//Subtract size of current atom from
//the data_remaining of the parent
//Note: this must occur before the next step
//which might change the level. Don't try to
//include it in the following step.
if(current_parent != root) {
current_parent->data_remaining -= atom->len;
}
//If the atom has data_remaining set, then must have some children
if(atom->data_remaining > 0) {
current_parent = atom;
}
//Check if we have data remaining in the parent.
//If not, if atom was the last one in the parent.
//So, move back up one level
if(current_parent != root) {
//Make sure we haven't overrun any expected sizes in the parent
if(current_parent->data_remaining < 0) {
printf("Something wrong: child atom overruns the parent size.");
printf("Parent name is: %s\n",current_parent->name);
exit(0);
}
//We're done getting the children of this parent, move back up.
while (current_parent && current_parent->parent && current_parent->data_remaining == 0) {
current_parent = current_parent->parent;
}
}
}
return root;
}
int main(int argc, char** argv) {
FILE *m4a_file;
FILE *out_file;
int meta_idx = 0;
//Check inputs, open file, check for success
if(argc < 3) {
printf("Usage: m4mudex <infilename> <outfilename>");
exit(1);
}
m4a_file = fopen(argv[1], "rb");
if (m4a_file == NULL) {
printf("Provide the name of an existing m4a file to parse\n");
exit(1);
}
//Quick sanity check on input file
printf("\nChecking to see if source file has a meta box: \n");
if((meta_idx = find_meta(m4a_file)) >= 0) {
printf("Found a meta box at %d\n",meta_idx);
} else {
printf("No meta box found.\n");
}
rewind(m4a_file);
//Build the tree
atom_t* m4a_tree = build_tree(m4a_file);
//Show the tree
printf("Original tree:\n");
print_tree(m4a_tree);
printf("\n");
//Get rid of metas and adjust offsets
strip_meta_box(m4a_tree);
//Show the modified tree
printf("Modified tree:\n");
print_tree(m4a_tree);
printf("\n");
//Write out the modified tree.
out_file = fopen(argv[2], "wb");
output_tree(m4a_tree, out_file);
fclose(out_file);
//Verify the output file
printf("\nVerifying that output file has no meta box: \n");
out_file = fopen(argv[2], "rb");
if((meta_idx = find_meta(out_file)) >= 0) {
printf("Found a meta box at %d\n",meta_idx);
} else {
printf("No meta box found.\n");
}
}