qt-faststart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * qt-faststart.c, v0.1
  3. * by Mike Melanson (melanson@pcisys.net)
  4. * This file is placed in the public domain. Use the program however you
  5. * see fit.
  6. *
  7. * This utility rearranges a Quicktime file such that the moov atom
  8. * is in front of the data, thus facilitating network streaming.
  9. *
  10. * Compile this program using:
  11. * cc qt-faststart.c -o qt-faststart
  12. * Invoke the program with:
  13. * qt-faststart <infile.mov> <outfile.mov>
  14. *
  15. * Notes: Quicktime files can come in many configurations of top-level
  16. * atoms. This utility stipulates that the very last atom in the file needs
  17. * to be a moov atom. When given such a file, this utility will rearrange
  18. * the top-level atoms by shifting the moov atom from the back of the file
  19. * to the front, and patch the chunk offsets along the way. This utility
  20. * presently only operates on uncompressed moov atoms.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <inttypes.h>
  25. #ifdef __MINGW32__
  26. #define fseeko(x,y,z) fseeko64(x,y,z)
  27. #define ftello(x) ftello64(x)
  28. #endif
  29. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  30. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  31. (((uint8_t*)(x))[1] << 16) | \
  32. (((uint8_t*)(x))[2] << 8) | \
  33. ((uint8_t*)(x))[3])
  34. #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
  35. ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
  36. ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
  37. ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
  38. ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
  39. ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
  40. ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
  41. ((uint64_t)((uint8_t*)(x))[7]))
  42. #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
  43. ( (uint32_t)(unsigned char)(ch3) | \
  44. ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
  45. ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
  46. ( (uint32_t)(unsigned char)(ch0) << 24 ) )
  47. #define QT_ATOM BE_FOURCC
  48. /* top level atoms */
  49. #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
  50. #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
  51. #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
  52. #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
  53. #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
  54. #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
  55. #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
  56. #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
  57. #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
  58. #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
  59. #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
  60. #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
  61. #define ATOM_PREAMBLE_SIZE 8
  62. #define COPY_BUFFER_SIZE 1024
  63. int main(int argc, char *argv[])
  64. {
  65. FILE *infile;
  66. FILE *outfile;
  67. unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
  68. uint32_t atom_type = 0;
  69. uint64_t atom_size = 0;
  70. uint64_t last_offset;
  71. unsigned char *moov_atom;
  72. unsigned char *ftyp_atom = 0;
  73. uint64_t moov_atom_size;
  74. uint64_t ftyp_atom_size = 0;
  75. uint64_t i, j;
  76. uint32_t offset_count;
  77. uint64_t current_offset;
  78. uint64_t start_offset = 0;
  79. unsigned char copy_buffer[COPY_BUFFER_SIZE];
  80. int bytes_to_copy;
  81. if (argc != 3) {
  82. printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
  83. return 0;
  84. }
  85. infile = fopen(argv[1], "rb");
  86. if (!infile) {
  87. perror(argv[1]);
  88. return 1;
  89. }
  90. /* traverse through the atoms in the file to make sure that 'moov' is
  91. * at the end */
  92. while (!feof(infile)) {
  93. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  94. break;
  95. }
  96. atom_size = (uint32_t)BE_32(&atom_bytes[0]);
  97. atom_type = BE_32(&atom_bytes[4]);
  98. if ((atom_type != FREE_ATOM) &&
  99. (atom_type != JUNK_ATOM) &&
  100. (atom_type != MDAT_ATOM) &&
  101. (atom_type != MOOV_ATOM) &&
  102. (atom_type != PNOT_ATOM) &&
  103. (atom_type != SKIP_ATOM) &&
  104. (atom_type != WIDE_ATOM) &&
  105. (atom_type != PICT_ATOM) &&
  106. (atom_type != FTYP_ATOM)) {
  107. printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
  108. break;
  109. }
  110. /* keep ftyp atom */
  111. if (atom_type == FTYP_ATOM) {
  112. ftyp_atom_size = atom_size;
  113. ftyp_atom = malloc(ftyp_atom_size);
  114. if (!ftyp_atom) {
  115. printf ("could not allocate 0x%llX byte for ftyp atom\n",
  116. atom_size);
  117. fclose(infile);
  118. return 1;
  119. }
  120. fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
  121. if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
  122. perror(argv[1]);
  123. free(ftyp_atom);
  124. fclose(infile);
  125. return 1;
  126. }
  127. start_offset = ftello(infile);
  128. continue;
  129. }
  130. /* 64-bit special case */
  131. if (atom_size == 1) {
  132. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  133. break;
  134. }
  135. atom_size = BE_64(&atom_bytes[0]);
  136. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
  137. } else {
  138. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
  139. }
  140. }
  141. if (atom_type != MOOV_ATOM) {
  142. printf ("last atom in file was not a moov atom\n");
  143. fclose(infile);
  144. return 0;
  145. }
  146. /* moov atom was, in fact, the last atom in the chunk; load the whole
  147. * moov atom */
  148. fseeko(infile, -atom_size, SEEK_END);
  149. last_offset = ftello(infile);
  150. moov_atom_size = atom_size;
  151. moov_atom = malloc(moov_atom_size);
  152. if (!moov_atom) {
  153. printf ("could not allocate 0x%llX byte for moov atom\n",
  154. atom_size);
  155. fclose(infile);
  156. return 1;
  157. }
  158. if (fread(moov_atom, atom_size, 1, infile) != 1) {
  159. perror(argv[1]);
  160. free(moov_atom);
  161. fclose(infile);
  162. return 1;
  163. }
  164. /* this utility does not support compressed atoms yet, so disqualify
  165. * files with compressed QT atoms */
  166. if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
  167. printf ("this utility does not support compressed moov atoms yet\n");
  168. free(moov_atom);
  169. fclose(infile);
  170. return 1;
  171. }
  172. /* close; will be re-opened later */
  173. fclose(infile);
  174. /* crawl through the moov chunk in search of stco or co64 atoms */
  175. for (i = 4; i < moov_atom_size - 4; i++) {
  176. atom_type = BE_32(&moov_atom[i]);
  177. if (atom_type == STCO_ATOM) {
  178. printf (" patching stco atom...\n");
  179. atom_size = BE_32(&moov_atom[i - 4]);
  180. if (i + atom_size - 4 > moov_atom_size) {
  181. printf (" bad atom size\n");
  182. free(moov_atom);
  183. return 1;
  184. }
  185. offset_count = BE_32(&moov_atom[i + 8]);
  186. for (j = 0; j < offset_count; j++) {
  187. current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
  188. current_offset += moov_atom_size;
  189. moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
  190. moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
  191. moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
  192. moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
  193. }
  194. i += atom_size - 4;
  195. } else if (atom_type == CO64_ATOM) {
  196. printf (" patching co64 atom...\n");
  197. atom_size = BE_32(&moov_atom[i - 4]);
  198. if (i + atom_size - 4 > moov_atom_size) {
  199. printf (" bad atom size\n");
  200. free(moov_atom);
  201. return 1;
  202. }
  203. offset_count = BE_32(&moov_atom[i + 8]);
  204. for (j = 0; j < offset_count; j++) {
  205. current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
  206. current_offset += moov_atom_size;
  207. moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
  208. moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
  209. moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
  210. moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
  211. moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
  212. moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
  213. moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
  214. moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
  215. }
  216. i += atom_size - 4;
  217. }
  218. }
  219. /* re-open the input file and open the output file */
  220. infile = fopen(argv[1], "rb");
  221. if (!infile) {
  222. perror(argv[1]);
  223. free(moov_atom);
  224. return 1;
  225. }
  226. if (start_offset > 0) { /* seek after ftyp atom */
  227. fseeko(infile, start_offset, SEEK_SET);
  228. last_offset -= start_offset;
  229. }
  230. outfile = fopen(argv[2], "wb");
  231. if (!outfile) {
  232. perror(argv[2]);
  233. fclose(outfile);
  234. free(moov_atom);
  235. return 1;
  236. }
  237. /* dump the same ftyp atom */
  238. if (ftyp_atom_size > 0) {
  239. printf (" writing ftyp atom...\n");
  240. if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
  241. perror(argv[2]);
  242. goto error_out;
  243. }
  244. }
  245. /* dump the new moov atom */
  246. printf (" writing moov atom...\n");
  247. if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
  248. perror(argv[2]);
  249. goto error_out;
  250. }
  251. /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
  252. printf (" copying rest of file...\n");
  253. while (last_offset) {
  254. if (last_offset > COPY_BUFFER_SIZE)
  255. bytes_to_copy = COPY_BUFFER_SIZE;
  256. else
  257. bytes_to_copy = last_offset;
  258. if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
  259. perror(argv[1]);
  260. goto error_out;
  261. }
  262. if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
  263. perror(argv[2]);
  264. goto error_out;
  265. }
  266. last_offset -= bytes_to_copy;
  267. }
  268. fclose(infile);
  269. fclose(outfile);
  270. free(moov_atom);
  271. if (ftyp_atom_size > 0)
  272. free(ftyp_atom);
  273. return 0;
  274. error_out:
  275. fclose(infile);
  276. fclose(outfile);
  277. free(moov_atom);
  278. if (ftyp_atom_size > 0)
  279. free(ftyp_atom);
  280. return 1;
  281. }