qt-faststart.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  26. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  27. (((uint8_t*)(x))[1] << 16) | \
  28. (((uint8_t*)(x))[2] << 8) | \
  29. ((uint8_t*)(x))[3])
  30. #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
  31. ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
  32. ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
  33. ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
  34. ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
  35. ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
  36. ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
  37. ((uint64_t)((uint8_t*)(x))[7]))
  38. #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
  39. ( (uint32_t)(unsigned char)(ch3) | \
  40. ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
  41. ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
  42. ( (uint32_t)(unsigned char)(ch0) << 24 ) )
  43. #define QT_ATOM BE_FOURCC
  44. /* top level atoms */
  45. #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
  46. #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
  47. #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
  48. #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
  49. #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
  50. #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
  51. #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
  52. #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
  53. #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
  54. #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
  55. #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
  56. #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
  57. #define ATOM_PREAMBLE_SIZE 8
  58. #define COPY_BUFFER_SIZE 1024
  59. int main(int argc, char *argv[])
  60. {
  61. FILE *infile;
  62. FILE *outfile;
  63. unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
  64. uint32_t atom_type = 0;
  65. uint64_t atom_size;
  66. uint64_t last_offset;
  67. unsigned char *moov_atom;
  68. uint64_t moov_atom_size;
  69. uint64_t i, j;
  70. uint32_t offset_count;
  71. uint64_t current_offset;
  72. unsigned char copy_buffer[COPY_BUFFER_SIZE];
  73. int bytes_to_copy;
  74. if (argc != 3) {
  75. printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
  76. return 0;
  77. }
  78. infile = fopen(argv[1], "rb");
  79. if (!infile) {
  80. perror(argv[1]);
  81. return 1;
  82. }
  83. /* traverse through the atoms in the file to make sure that 'moov' is
  84. * at the end */
  85. while (!feof(infile)) {
  86. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  87. break;
  88. }
  89. atom_size = BE_32(&atom_bytes[0]);
  90. atom_type = BE_32(&atom_bytes[4]);
  91. if ((atom_type != FREE_ATOM) &&
  92. (atom_type != JUNK_ATOM) &&
  93. (atom_type != MDAT_ATOM) &&
  94. (atom_type != MOOV_ATOM) &&
  95. (atom_type != PNOT_ATOM) &&
  96. (atom_type != SKIP_ATOM) &&
  97. (atom_type != WIDE_ATOM) &&
  98. (atom_type != PICT_ATOM) &&
  99. (atom_type != FTYP_ATOM)) {
  100. printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
  101. break;
  102. }
  103. /* 64-bit special case */
  104. if (atom_size == 1) {
  105. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  106. break;
  107. }
  108. atom_size = BE_64(&atom_bytes[0]);
  109. fseek(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
  110. } else {
  111. fseek(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
  112. }
  113. }
  114. if (atom_type != MOOV_ATOM) {
  115. printf ("last atom in file was not a moov atom\n");
  116. fclose(infile);
  117. return 0;
  118. }
  119. /* moov atom was, in fact, the last atom in the chunk; load the whole
  120. * moov atom */
  121. fseek(infile, -atom_size, SEEK_END);
  122. last_offset = (uint64_t)ftell(infile);
  123. moov_atom_size = atom_size;
  124. moov_atom = malloc(moov_atom_size);
  125. if (!moov_atom) {
  126. printf ("could not allocate 0x%llX byte for moov atom\n",
  127. atom_size);
  128. fclose(infile);
  129. return 1;
  130. }
  131. if (fread(moov_atom, atom_size, 1, infile) != 1) {
  132. perror(argv[1]);
  133. free(moov_atom);
  134. fclose(infile);
  135. return 1;
  136. }
  137. /* this utility does not support compressed atoms yet, so disqualify
  138. * files with compressed QT atoms */
  139. if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
  140. printf ("this utility does not support compressed moov atoms yet\n");
  141. free(moov_atom);
  142. fclose(infile);
  143. return 1;
  144. }
  145. /* close; will be re-opened later */
  146. fclose(infile);
  147. /* crawl through the moov chunk in search of stco or co64 atoms */
  148. for (i = 4; i < moov_atom_size - 4; i++) {
  149. atom_type = BE_32(&moov_atom[i]);
  150. if (atom_type == STCO_ATOM) {
  151. printf (" patching stco atom...\n");
  152. atom_size = BE_32(&moov_atom[i - 4]);
  153. if (i + atom_size - 4 > moov_atom_size) {
  154. printf (" bad atom size\n");
  155. free(moov_atom);
  156. return 1;
  157. }
  158. offset_count = BE_32(&moov_atom[i + 8]);
  159. for (j = 0; j < offset_count; j++) {
  160. current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
  161. current_offset += moov_atom_size;
  162. moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
  163. moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
  164. moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
  165. moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
  166. }
  167. i += atom_size - 4;
  168. } else if (atom_type == CO64_ATOM) {
  169. printf (" patching co64 atom...\n");
  170. atom_size = BE_32(&moov_atom[i - 4]);
  171. if (i + atom_size - 4 > moov_atom_size) {
  172. printf (" bad atom size\n");
  173. free(moov_atom);
  174. return 1;
  175. }
  176. offset_count = BE_32(&moov_atom[i + 8]);
  177. for (j = 0; j < offset_count; j++) {
  178. current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
  179. current_offset += moov_atom_size;
  180. moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
  181. moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
  182. moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
  183. moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
  184. moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
  185. moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
  186. moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
  187. moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
  188. }
  189. i += atom_size - 4;
  190. }
  191. }
  192. /* re-open the input file and open the output file */
  193. infile = fopen(argv[1], "rb");
  194. if (!infile) {
  195. perror(argv[1]);
  196. free(moov_atom);
  197. return 1;
  198. }
  199. outfile = fopen(argv[2], "wb");
  200. if (!outfile) {
  201. perror(argv[2]);
  202. fclose(outfile);
  203. free(moov_atom);
  204. return 1;
  205. }
  206. /* dump the new moov atom */
  207. printf (" writing moov atom...\n");
  208. if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
  209. perror(argv[2]);
  210. goto error_out;
  211. }
  212. /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
  213. printf (" copying rest of file...\n");
  214. while (last_offset) {
  215. if (last_offset > COPY_BUFFER_SIZE)
  216. bytes_to_copy = COPY_BUFFER_SIZE;
  217. else
  218. bytes_to_copy = last_offset;
  219. if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
  220. perror(argv[1]);
  221. goto error_out;
  222. }
  223. if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
  224. perror(argv[2]);
  225. goto error_out;
  226. }
  227. last_offset -= bytes_to_copy;
  228. }
  229. fclose(infile);
  230. fclose(outfile);
  231. free(moov_atom);
  232. return 0;
  233. error_out:
  234. fclose(infile);
  235. fclose(outfile);
  236. free(moov_atom);
  237. return 1;
  238. }