forked from bangnoise/qt-flatten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt_flatten.c
1085 lines (991 loc) · 42.5 KB
/
qt_flatten.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// qt_flatten.c
//
// Created by Tom Butterworth on 08/05/2012.
// Copyright (c) 2012 Tom Butterworth. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of qt-flatten nor the name of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "qt_flatten.h"
#include <stdlib.h> // malloc, free
#include <stdint.h> // sized & signed types
#include <fcntl.h> // open
#include <sys/param.h> // MIN
#include <string.h> // memcpy
#include <sys/stat.h> // fstat
#include <zlib.h> // inflate, deflate
#define QTF_FCC_ftyp (0x66747970)
#define QTF_FCC_moov (0x6d6f6f76)
#define QTF_FCC_free (0x66726565)
#define QTF_FCC_skip (0x736B6970)
#define QTF_FCC_wide (0x77696465)
#define QTF_FCC_mdat (0x6d646174)
#define QTF_FCC_qt__ (0x71742020)
#define QTF_FCC_mp41 (0x6d703431)
#define QTF_FCC_mp42 (0x6d703432)
#define QTF_FCC_cmov (0x636d6f76)
#define QTF_FCC_dcom (0x64636f6d)
#define QTF_FCC_cmvd (0x636d7664)
#define QTF_FCC_zlib (0x7a6c6962)
#define QTF_FCC_trak (0x7472616b)
#define QTF_FCC_mdia (0x6d646961)
#define QTF_FCC_minf (0x6d696e66)
#define QTF_FCC_stbl (0x7374626c)
#define QTF_FCC_stco (0x7374636f)
#define QTF_FCC_co64 (0x636F3634)
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define qtf_swap_big_to_host_int_32(x) OSSwapBigToHostInt32((x))
#define qtf_swap_big_to_host_int_64(x) OSSwapBigToHostInt64((x))
#define qtf_swap_host_to_big_int_32(x) OSSwapHostToBigInt32((x))
#define qtf_swap_host_to_big_int_64(x) OSSwapHostToBigInt64((x))
#elif defined(__linux__)
# include <endian.h>
# if !defined(__BYTE_ORDER) || !defined(__LITTLE_ENDIAN)
# error No byte-order macros available in endian.h
# endif
# if __BYTE_ORDER == __LITTLE_ENDIAN
# include <byteswap.h>
# define qtf_swap_big_to_host_int_32(x) bswap_32((x))
# define qtf_swap_big_to_host_int_64(x) bswap_64((x))
# define qtf_swap_host_to_big_int_32(x) bswap_32((x))
# define qtf_swap_host_to_big_int_64(x) bswap_64((x))
# else
# define qtf_swap_big_to_host_int_32(x) (x)
# define qtf_swap_big_to_host_int_64(x) (x)
# define qtf_swap_host_to_big_int_32(x) (x)
# define qtf_swap_host_to_big_int_64(x) (x)
# endif
#elif defined(__WIN32)
static __inline unsigned short qtf_swap_16(unsigned short x)
{
return (x >> 8) | (x << 8);
}
static __inline unsigned int qtf_swap_32(unsigned int x)
{
return (qtf_swap_16(x & 0xffff) << 16) | (qtf_swap_16(x >> 16));
}
static __inline unsigned long long qtf_swap_64 (unsigned long long x)
{
return (((unsigned long long) qtf_swap_32(x & 0xffffffffull)) << 32) | (qtf_swap_32 (x >> 32));
}
#define qtf_swap_big_to_host_int_32(x) qtf_swap_32((x))
#define qtf_swap_big_to_host_int_64(x) qtf_swap_64((x))
#define qtf_swap_host_to_big_int_32(x) qtf_swap_32((x))
#define qtf_swap_host_to_big_int_64(x) qtf_swap_64((x))
#endif
#ifndef MIN
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#endif
#define QTF_COPY_BUFFER_SIZE (10240)
/*
atoms may be larger than size_t on some systems
*/
typedef uint64_t qtf_atom_size;
/*
* Compression/Decompression
*/
static size_t qtf_decompress_data(void *source_buffer, size_t source_buffer_length,
void *decompressed_buffer, size_t decompressed_buffer_length)
{
size_t size_out = 0;
uLongf decompressed_length = decompressed_buffer_length;
int result = uncompress(decompressed_buffer, &decompressed_length, source_buffer, source_buffer_length);
if (result == Z_OK) size_out = decompressed_length;
return size_out;
}
static size_t qtf_compress_data(void *source_buffer, size_t source_buffer_length,
void *compressed_buffer, size_t compressed_buffer_length,
int compression_level)
{
size_t size_out = 0;
uLongf compressed_length = compressed_buffer_length;
int result = compress2(compressed_buffer, &compressed_length, source_buffer, source_buffer_length, compression_level);
if (result == Z_OK) size_out = compressed_length;
return size_out;
}
/*
* qtf_edit_list
*
* qtf_edit_list maintains a list of data insertions and removals and calculates the overall change for data at given offsets
*/
typedef struct qtf_edit_s **qtf_edit_list;
typedef struct qtf_edit_s
{
off_t offset;
off_t edit;
struct qtf_edit_s *next;
} qtf_edit_s;
static qtf_edit_list qtf_edit_list_create()
{
qtf_edit_list list = malloc(sizeof(qtf_edit_s *));
if (list)
{
*list = NULL;
}
return list;
}
static void qtf_edit_list_destroy(qtf_edit_list list)
{
if (list)
{
qtf_edit_s *edit = *list;
while (edit) {
qtf_edit_s *next = edit->next;
free(edit);
edit = next;
}
free(list);
}
}
static void qtf_edit_list_add_edit(qtf_edit_list list, off_t offset, off_t edit)
{
if (list)
{
qtf_edit_s *entry = malloc(sizeof(qtf_edit_s));
if (entry)
{
entry->offset = offset;
entry->edit = edit;
entry->next = *list;
*list = entry;
}
}
}
static off_t qtf_edit_list_get_offset_change(qtf_edit_list list, off_t offset)
{
off_t change = 0;
if (list)
{
qtf_edit_s *edit = *list;
while (edit) {
if (edit->offset <= offset)
{
change += edit->edit;
}
edit = edit->next;
}
}
return change;
}
/*
* Utility
*/
/*
qtf_read is like read except it returns an error if the expected number of bytes couldn't be read
*/
static qtf_result qtf_read(int fd, void *buffer, size_t length)
{
off_t got = read(fd, buffer, length);
if (got == -1)
{
return qtf_result_file_read_error;
}
else if (got != length)
{
return qtf_result_file_not_movie;
}
return qtf_result_ok;
}
/*
qtf_write is like write except it returns an error if the expected number of bytes couldn't be written
*/
static qtf_result qtf_write(int fd, const void *buffer, size_t length)
{
ssize_t written = write(fd, buffer, length);
if (written == -1 || written != length)
{
return qtf_result_file_write_error;
}
return qtf_result_ok;
}
/*
returns 0 on success or a qtf_result
*/
static qtf_result qtf_get_file_size(int fd, off_t *out_file_size)
{
struct stat stat_info;
int got = fstat(fd, &stat_info);
if (got == -1)
{
return qtf_result_file_read_error;
}
*out_file_size = stat_info.st_size;
return qtf_result_ok;
}
/*
returns 0 on success or a qtf_result
*/
static qtf_result qtf_read_atom_header(int fd, void *dest_buffer, size_t dest_buffer_length, uint32_t *out_type, qtf_atom_size *out_size, size_t *out_bytes_read)
{
*out_bytes_read = 0;
*out_size = 0;
*out_type = 0;
if (dest_buffer_length < 16)
{
return qtf_result_memory_error;
}
qtf_atom_size size;
uint32_t type;
off_t got = read(fd, dest_buffer, 8);
if (got == -1)
{
return qtf_result_file_read_error;
}
*out_bytes_read += got;
if (got == 0)
{
return 0;
}
else if (got != 8)
{
return qtf_result_file_not_movie;
}
size = qtf_swap_big_to_host_int_32(*(uint32_t *)dest_buffer);
type = qtf_swap_big_to_host_int_32(*(uint32_t *)(dest_buffer + 4));
if (size == 0)
{
off_t offset = lseek(fd, 0, SEEK_CUR);
if (offset == -1)
{
return qtf_result_file_read_error;
}
off_t file_size = 0;
qtf_result result = qtf_get_file_size(fd, &file_size);
if (result != qtf_result_ok) return result;
size = file_size - offset + 8;
}
else if (size == 1)
{
qtf_result result = qtf_read(fd, dest_buffer + 8, 8);
if (result != qtf_result_ok) return result;
*out_bytes_read += 8;
size = qtf_swap_big_to_host_int_64(*(uint64_t *)(dest_buffer + 8));
}
*out_size = size;
*out_type = type;
return qtf_result_ok;
}
// set as many try_ flags as you want, they will be tried sequentially until one works in the given buffer size
// returns the size of the compressed atom on success, or 0 on failure
static size_t qtf_compress_movie_atom(void *atom_buffer, size_t atom_buffer_length,
void *compressed_atom_buffer, size_t compressed_atom_buffer_length,
bool try_fast, bool try_default, bool try_best)
{
// leave space for the compressed movie atoms (40 bytes)
size_t compressed_data_max_length = compressed_atom_buffer_length - 40;
void *compressed_data = compressed_atom_buffer + 40;
size_t compressed_data_length = 0;
if (try_fast)
{
compressed_data_length = qtf_compress_data(atom_buffer, atom_buffer_length, compressed_data, compressed_data_max_length, Z_BEST_SPEED);
}
if (try_default && compressed_data_length == 0)
{
compressed_data_length = qtf_compress_data(atom_buffer, atom_buffer_length, compressed_data, compressed_data_max_length, Z_DEFAULT_COMPRESSION);
}
if (try_best || compressed_data_length == 0)
{
compressed_data_length = qtf_compress_data(atom_buffer, atom_buffer_length, compressed_data, compressed_data_max_length, Z_BEST_COMPRESSION);
}
if (compressed_data_length != 0)
{
// write the compressed movie atom header before the compressed data
*(uint32_t *)compressed_atom_buffer = qtf_swap_host_to_big_int_32(compressed_data_length + 40);
*(uint32_t *)(compressed_atom_buffer + 4) = qtf_swap_host_to_big_int_32(QTF_FCC_moov);
*(uint32_t *)(compressed_atom_buffer + 8) = qtf_swap_host_to_big_int_32(compressed_data_length + 32);
*(uint32_t *)(compressed_atom_buffer + 12) = qtf_swap_host_to_big_int_32(QTF_FCC_cmov);
*(uint32_t *)(compressed_atom_buffer + 16) = qtf_swap_host_to_big_int_32(12);
*(uint32_t *)(compressed_atom_buffer + 20) = qtf_swap_host_to_big_int_32(QTF_FCC_dcom);
*(uint32_t *)(compressed_atom_buffer + 24) = qtf_swap_host_to_big_int_32(QTF_FCC_zlib);
*(uint32_t *)(compressed_atom_buffer + 28) = qtf_swap_host_to_big_int_32(compressed_data_length + 12);
*(uint32_t *)(compressed_atom_buffer + 32) = qtf_swap_host_to_big_int_32(QTF_FCC_cmvd);
*(uint32_t *)(compressed_atom_buffer + 36) = qtf_swap_host_to_big_int_32(atom_buffer_length);
compressed_data_length += 40;
}
return compressed_data_length;
}
static qtf_result qtf_offsets_apply_list(void *moov_atom, qtf_atom_size moov_atom_size, qtf_edit_list edit_list)
{
qtf_result result = qtf_result_ok;
for (int i = 8; i < moov_atom_size; ) {
uint32_t size = qtf_swap_big_to_host_int_32(*(uint32_t *)(moov_atom + i));
uint32_t type = qtf_swap_big_to_host_int_32(*(uint32_t *)(moov_atom + i + 4));
if (size > (moov_atom_size - i))
{
result = qtf_result_file_not_movie;
break;
}
if (type == QTF_FCC_stco)
{
uint32_t entry_count = qtf_swap_big_to_host_int_32(*(uint32_t *)(moov_atom + i + 12));
if (entry_count * 4 > size - 16)
{
result = qtf_result_file_not_movie;
break;
}
for (int j = 0; j < entry_count; j++) {
uint32_t *entry = moov_atom + i + 16 + (j * 4);
uint32_t current_offset = qtf_swap_big_to_host_int_32(*entry);
current_offset += qtf_edit_list_get_offset_change(edit_list, current_offset);
*entry = qtf_swap_host_to_big_int_32(current_offset);
}
}
else if (type == QTF_FCC_co64)
{
uint32_t entry_count = qtf_swap_big_to_host_int_32(*(uint32_t *)(moov_atom + i + 12));
if (entry_count * 8 > size - 16)
{
result = qtf_result_file_not_movie;
break;
}
for (int j = 0; j < entry_count; j++) {
uint64_t *entry = moov_atom + i + 16 + (j * 8);
uint64_t current_offset = qtf_swap_big_to_host_int_64(*entry);
current_offset += qtf_edit_list_get_offset_change(edit_list, current_offset);
*entry = qtf_swap_host_to_big_int_64(current_offset);
}
}
switch (type) {
case QTF_FCC_trak:
case QTF_FCC_mdia:
case QTF_FCC_minf:
case QTF_FCC_stbl:
// enter it
i += 8;
break;
default:
// skip it
i += size;
break;
}
}
return result;
}
static qtf_result qtf_offsets_modify(void *moov_atom, qtf_atom_size moov_atom_size, ssize_t change)
{
// fake a qtf_edit_list with one edit at offset 0
qtf_edit_s edit = {0, change, NULL};
qtf_edit_s *edit_ptr = &edit;
return qtf_offsets_apply_list(moov_atom, moov_atom_size, &edit_ptr);
}
/*
* Public Functions
*/
qtf_result qtf_flatten_movie(const char *src_path, const char *dst_path, bool allow_compressed_moov_atom)
{
// open source
#if defined(_WIN32)
int fd_source = _open(src_path, _O_RDONLY | _O_BINARY);
#else
int fd_source = open(src_path, O_RDONLY);
#endif
if (fd_source == -1)
{
return qtf_result_file_read_error;
}
// an error, to return when we finish
int result = 0;
// the atoms we will directly deal with
void *atom_ftyp = NULL;
qtf_atom_size atom_ftyp_size = 0;
void *atom_moov = NULL;
qtf_atom_size atom_moov_size = 0;
bool atom_mdat_present = false;
qtf_edit_list edit_list = qtf_edit_list_create();
if (edit_list == NULL) result = qtf_result_memory_error;
off_t offset = 0;
// copy the ftyp atom if present and the moov atom, get other information we need to ignore free space in the file
while (result == qtf_result_ok) {
uint32_t atom_header[4];
qtf_atom_size size = 0;
uint32_t type = 0;
size_t bytes_read;
result = qtf_read_atom_header(fd_source, atom_header, sizeof(atom_header), &type, &size, &bytes_read);
if (result != 0 || bytes_read == 0) break;
switch (type) {
case QTF_FCC_ftyp:
// QTFF Chapter 1, The File Type Compatibility Atom
// ISO IEC 14496-14 Section 4, File Identification
// TODO: could drop 0x0 compatibility atoms (save 3 bytes from QT-built files, woo)
if (atom_ftyp_size != 0)
{
result = qtf_result_file_not_movie; // there must be only one ftyp atom
}
else if (offset != 0)
{
// This is lazy but most files have their ftyp atom first
result = qtf_result_file_too_complex;
}
else
{
if (bytes_read != 8 || size < 20) result = qtf_result_file_not_movie; // minimally useful size 20 bytes: atom header(8) + major brand(4) + version(4) + 1 compatible brand(4)
if (result == qtf_result_ok)
{
// We can only work with the atom if we can load it all in memory, fail otherwise
if (size <= SIZE_MAX)
{
atom_ftyp_size = size;
atom_ftyp = malloc((size_t)atom_ftyp_size);
}
if (atom_ftyp == NULL)
{
result = qtf_result_memory_error;
}
}
if (result == qtf_result_ok)
{
// copy what we already read
memcpy(atom_ftyp, atom_header, bytes_read);
// read the rest
result = qtf_read(fd_source, atom_ftyp + bytes_read, (size_t)atom_ftyp_size - bytes_read);
if (result == qtf_result_ok)
{
bytes_read += atom_ftyp_size - bytes_read;
}
}
if (result == qtf_result_ok)
{
// Check for compatibility
unsigned long brand_count = ((size_t)atom_ftyp_size - 16) / 4;
bool has_qt_or_mp4 = false;
for (unsigned long i = 0; i < brand_count; i++) {
uint32_t brand = qtf_swap_big_to_host_int_32(*(uint32_t *)(atom_ftyp + 16 + (i * 4)));
if (brand == QTF_FCC_qt__ || brand == QTF_FCC_mp41 || brand == QTF_FCC_mp42)
{
has_qt_or_mp4 = true;
break;
}
}
if (!has_qt_or_mp4) result = qtf_result_file_not_movie;
// The ftyp atom must precede the moov atom and movie data
if (atom_mdat_present == true || atom_moov_size != 0) result = qtf_result_file_not_movie;
}
}
break;
case QTF_FCC_moov:
// remove the atom from the file in its current location, we will add it again later
qtf_edit_list_add_edit(edit_list, offset, -(ssize_t)size);
// there should only be one of these, we discard any others
if (atom_moov_size == 0)
{
size_t contents_bytes_read = 0;
qtf_atom_size contents_size = 0;
uint32_t contents_type = 0;
uint32_t decompressed_size = 0;
off_t compressed_data_start = 0;
// We can only work with the atom if we can load it all in memory, fail otherwise
if (size <= SIZE_MAX)
{
atom_moov_size = size;
atom_moov = malloc((size_t)atom_moov_size);
}
if (atom_moov == NULL)
{
result = qtf_result_memory_error;
}
if (result == qtf_result_ok)
{
// copy what we already read
memcpy(atom_moov, atom_header, bytes_read);
// read the first atom header inside the moov atom straight into the moov atom in memory
result = qtf_read_atom_header(fd_source, atom_moov + bytes_read, (size_t)atom_moov_size - bytes_read, &contents_type, &contents_size, &contents_bytes_read);
bytes_read += contents_bytes_read;
}
if (result == qtf_result_ok)
{
// check if the atom is compressed
// QTFF Chapter 2, Compressed Movie Resources
if (contents_type == QTF_FCC_cmov)
{
// read the next atom header inside the cmov atom
result = qtf_read_atom_header(fd_source, atom_moov + bytes_read, (size_t)atom_moov_size - bytes_read, &contents_type, &contents_size, &contents_bytes_read);
bytes_read += contents_bytes_read;
// check it's a valid dcom atom
if (result == qtf_result_ok && (contents_type != QTF_FCC_dcom || (contents_size - contents_bytes_read) != 4)) result = qtf_result_file_not_movie;
if (result == qtf_result_ok)
{
// read the 4 byte compression type from the dcom atom
result = qtf_read(fd_source, atom_moov + bytes_read, 4);
if (result == qtf_result_ok)
{
// check for zlib compression
uint32_t compression = qtf_swap_big_to_host_int_32(*(uint32_t *)(atom_moov + bytes_read));
if (compression != QTF_FCC_zlib)
{
result = qtf_result_file_too_complex;
}
bytes_read += 4;
}
}
if (result == qtf_result_ok)
{
// read the cmvd atom header
result = qtf_read_atom_header(fd_source, atom_moov + bytes_read, (size_t)atom_moov_size - bytes_read, &contents_type, &contents_size, &contents_bytes_read);
bytes_read += contents_bytes_read;
// check it's a valid cmvd atom
if (result == qtf_result_ok && (contents_type != QTF_FCC_cmvd || (contents_size - contents_bytes_read) < 4)) result = qtf_result_file_not_movie;
if (result == qtf_result_ok)
{
// read the 4 byte decompressed size
result = qtf_read(fd_source, atom_moov + bytes_read, 4);
if (result == qtf_result_ok)
{
decompressed_size = qtf_swap_big_to_host_int_32(*(uint32_t *)(atom_moov + bytes_read));
if (decompressed_size == 0) result = qtf_result_file_not_movie;
bytes_read += 4;
compressed_data_start = bytes_read;
}
}
}
}
}
if (result == qtf_result_ok)
{
// read the rest of the atom
result = qtf_read(fd_source, atom_moov + bytes_read, (size_t)atom_moov_size - bytes_read);
if (result == qtf_result_ok)
{
bytes_read += atom_moov_size - bytes_read;
}
}
if (result == qtf_result_ok && decompressed_size != 0) // ie the moov atom is compressed
{
void *atom_moov_decompressed = malloc(decompressed_size);
if (atom_moov_decompressed == NULL) result = qtf_result_memory_error;
if (result == qtf_result_ok)
{
size_t actuallly_decompressed = qtf_decompress_data(atom_moov + compressed_data_start, (size_t)(atom_moov_size - compressed_data_start),
atom_moov_decompressed, decompressed_size);
if (actuallly_decompressed != decompressed_size) result = qtf_result_file_not_movie;
else
{
free(atom_moov);
atom_moov = atom_moov_decompressed;
atom_moov_decompressed = NULL;
atom_moov_size = decompressed_size;
}
}
free(atom_moov_decompressed); // if all went well this will be NULL by now
}
}
break;
case QTF_FCC_free:
case QTF_FCC_skip:
case QTF_FCC_wide:
qtf_edit_list_add_edit(edit_list, offset, -size);
break;
case QTF_FCC_mdat:
atom_mdat_present = true;
break;
default:
break;
}
if (result == qtf_result_ok)
{
offset = lseek(fd_source, size - bytes_read, SEEK_CUR);
if (offset == -1)
{
result = qtf_result_file_read_error;
}
}
}
// check we can do something with this file
if (result == qtf_result_ok && (atom_mdat_present == false || atom_moov_size == 0))
{
result = qtf_result_file_too_complex;
}
if (allow_compressed_moov_atom)
{
void *atom_moov_compressed = NULL;
qtf_atom_size atom_moov_compressed_size = 0;
qtf_atom_size atom_moov_compressed_expected_size = 0;
qtf_atom_size atom_moov_compressed_actual_size = 0;
if (atom_moov_size < 20) result = qtf_result_file_not_movie;
if (result == qtf_result_ok)
{
if (result == qtf_result_ok)
{
atom_moov_compressed_size = atom_moov_size;
atom_moov_compressed = malloc((size_t)atom_moov_compressed_size);
if (atom_moov_compressed == NULL)
{
result = qtf_result_memory_error;
}
}
if (result == qtf_result_ok)
{
// We have to estimate a compressed size, modify the sample data offsets for that estimated size, then compress
// the modified atom and see if we met our target. If not, repeat the process, allowing a little more space until
// we succeed or arrive at the original atom size
size_t increments = (((size_t)atom_moov_size / 16) + (16 - 1)) & ~(16 - 1);
atom_moov_compressed_expected_size = increments * 3;
off_t total_offset_change = atom_moov_compressed_expected_size;
bool can_store_atoms = false;
// add an edit for our estimated size
qtf_edit_list_add_edit(edit_list, atom_ftyp_size, atom_moov_compressed_expected_size);
// apply all the edits to date
result = qtf_offsets_apply_list(atom_moov, atom_moov_size, edit_list);
do {
// This is skipped the first pass, then expands the space we reserve on subsequent passes
if (result == qtf_result_ok && atom_moov_compressed_actual_size > (atom_moov_compressed_expected_size - 8))
{
atom_moov_compressed_expected_size += increments;
total_offset_change += increments;
result = qtf_offsets_modify(atom_moov, atom_moov_size, increments);
}
if (result == qtf_result_ok)
{
atom_moov_compressed_actual_size = qtf_compress_movie_atom(atom_moov, (size_t)atom_moov_size,
atom_moov_compressed, (size_t)atom_moov_compressed_size,
false, true, false);
}
if (result == qtf_result_ok
&&
(atom_moov_compressed_actual_size == atom_moov_compressed_expected_size
|| atom_moov_compressed_actual_size < (atom_moov_compressed_expected_size - 8))
)
{
// atom_moov_compressed_actual_size will be zero if we couldn't compress it in the buffer space
// or if an error occurred in compression, so we use the uncompressed atom in those cases
if (atom_moov_compressed_actual_size != 0)
{
// we substitute the existing atom_moov with the an "atom" which is usually two atoms:
// the compressed moov atom plus a free atom for the extra space we estimated when
// calculating the offset
free(atom_moov);
atom_moov = atom_moov_compressed;
atom_moov_size = atom_moov_compressed_expected_size; // The total size we'll write to the file
atom_moov_compressed = NULL;
size_t free_size = (size_t)(atom_moov_compressed_expected_size - atom_moov_compressed_actual_size);
if (free_size > 0 && free_size < 8)
{
result = qtf_result_memory_error; // this shouldn't happen but it's fatal so check
}
else
{
uint32_t *size = atom_moov + atom_moov_compressed_actual_size;
uint32_t *type = atom_moov + atom_moov_compressed_actual_size + 4;
*size = qtf_swap_host_to_big_int_32(free_size);
*type = qtf_swap_host_to_big_int_32(QTF_FCC_free);
}
}
else
{
// we failed to compress the atom, set the offsets for the uncompressed atom size
result = qtf_offsets_modify(atom_moov, atom_moov_size, (ssize_t)atom_moov_size - (ssize_t)total_offset_change);
}
can_store_atoms = true;
}
} while (result == qtf_result_ok && can_store_atoms == false);
}
}
// we've swapped it into atom_moov by now if things went well, and atom_moov_compressed will be NULL
free(atom_moov_compressed);
}
else
{
if (result == qtf_result_ok)
{
// add the movie back in its new position
qtf_edit_list_add_edit(edit_list, atom_ftyp_size, atom_moov_size);
// update the moov atom with the new offsets
result = qtf_offsets_apply_list(atom_moov, atom_moov_size, edit_list);
}
}
// We're finished with the edit_list now
qtf_edit_list_destroy(edit_list);
edit_list = NULL;
int fd_dest = 0;
if (result == qtf_result_ok)
{
#if defined(_WIN32)
fd_dest = _open(dst_path, _O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY, _S_IREAD | _S_IWRITE);
#else
fd_dest = open(dst_path, O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // RW owner, R group, R others
#endif
if (fd_dest == -1)
{
result = qtf_result_file_write_error;
}
}
if (result == qtf_result_ok)
{
// Write the ftyp atom if there was one
if (atom_ftyp != NULL)
{
result = qtf_write(fd_dest, atom_ftyp, (size_t)atom_ftyp_size);
}
// Write the moov atom
if (result == qtf_result_ok)
{
result = qtf_write(fd_dest, atom_moov, (size_t)atom_moov_size);
}
if (result == qtf_result_ok)
{
// skip over the ftyp atom if present
off_t source_offset = lseek(fd_source, atom_ftyp_size, SEEK_SET);
if (source_offset == -1)
{
result = qtf_result_file_read_error;
}
// Copy everything except the moov atom(s) and any free skip or wide atoms
char buffer[QTF_COPY_BUFFER_SIZE];
while (result == qtf_result_ok) {
uint32_t type;
qtf_atom_size size;
size_t bytes_read;
result = qtf_read_atom_header(fd_source, buffer, QTF_COPY_BUFFER_SIZE, &type, &size, &bytes_read);
if (result != 0 || bytes_read == 0) break;
bool skip;
switch (type) {
case QTF_FCC_moov:
case QTF_FCC_free:
case QTF_FCC_skip:
case QTF_FCC_wide:
skip = true;
break;
default:
skip = false;
break;
}
if (skip)
{
// Skip these atoms
offset = lseek(fd_source, size - bytes_read, SEEK_CUR);
if (offset == -1)
{
result = qtf_result_file_read_error;
}
else
{
bytes_read += (size - bytes_read);
}
}
else
{
// Write all other atoms to the new file
result = qtf_write(fd_dest, buffer, bytes_read);
if (result == qtf_result_ok)
{
size -= bytes_read;
while (result == qtf_result_ok && size > 0)
{
size_t to_copy = (size_t)MIN(size, QTF_COPY_BUFFER_SIZE);
result = qtf_read(fd_source, buffer, to_copy);
bytes_read += to_copy;
if (result == qtf_result_ok)
{
result = qtf_write(fd_dest, buffer, to_copy);
if (result == qtf_result_ok)
{
size -= to_copy;
}
}
}
}
} // skip
source_offset += bytes_read;
} // while
}
}
free(atom_moov);
free(atom_ftyp);
if (fd_source) close(fd_source);
if (fd_dest) close(fd_dest);
return result;
}
qtf_result qtf_flatten_movie_in_place(const char *src_path, bool allow_compressed_moov_atom)
{
qtf_result result = qtf_result_ok;
#if defined(_WIN32)
int fd = _open(src_path, _O_RDWR | _O_BINARY);
#else
int fd = open(src_path, O_RDWR);
#endif
if (fd == -1)
{
return qtf_result_file_read_error;
}
off_t file_length = 0;
result = qtf_get_file_size(fd, &file_length);
if (result == qtf_result_ok)
{
off_t free_start = 0;
off_t moov_start = 0;
off_t mdat_start = 0;
qtf_atom_size free_size = 0;
qtf_atom_size moov_size = 0;
qtf_atom_size mdat_size = 0;
off_t offset = 0;
while (result == qtf_result_ok && (moov_size == 0 || free_size == 0 || mdat_size == 0))
{
uint32_t atom_header[4];
uint64_t size = 0;
uint32_t type = 0;
size_t bytes_read;
off_t got = 0;
result = qtf_read_atom_header(fd, atom_header, sizeof(atom_header), &type, &size, &bytes_read);
if (result != qtf_result_ok || bytes_read == 0) break;
if (type == QTF_FCC_free && free_size == 0) // use only the first free atom
{
free_start = offset;
free_size = size;
}
else if (type == QTF_FCC_wide && offset == (free_start + free_size))
{
// we can swallow up a wide atom if it immediately follows the free atom
free_size += size;
}
else if (type == QTF_FCC_moov)
{
moov_start = offset;
moov_size = size;
}
else if (type == QTF_FCC_mdat)
{
mdat_start = offset;
mdat_size = size;
}
offset += size;
got = lseek(fd, size - bytes_read, SEEK_CUR);
if (got == -1) result = qtf_result_file_read_error;
}
// Check there is an mdat atom. This doesn't guarantee this isn't a reference movie
if (result == qtf_result_ok && mdat_size == 0)
{
result = qtf_result_file_too_complex;
}
// Check we can copy the moov atom into memory
if (moov_size > SIZE_MAX)
{
result = qtf_result_memory_error;
}
// Check there is a free atom before the mdat atom and the movie isn't already flattened
if (result == qtf_result_ok
&& (free_start < mdat_start)
&& (moov_start > mdat_start)
&& (free_size > 8)
&& (moov_size > 8))
{
bool moov_was_at_end = ((moov_start + moov_size) == file_length) ? true : false;
void *moov = malloc((size_t)moov_size);
if (!moov)
{
result = qtf_result_memory_error;
}
if (result == qtf_result_ok)
{
off_t got = 0;
got = lseek(fd, moov_start, SEEK_SET);
if (got == -1) result = qtf_result_file_read_error;
if (result == qtf_result_ok)
{