You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
10.0KB

  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 = 0;
  66. uint64_t last_offset;
  67. unsigned char *moov_atom;
  68. unsigned char *ftyp_atom = 0;
  69. uint64_t moov_atom_size;
  70. uint64_t ftyp_atom_size = 0;
  71. uint64_t i, j;
  72. uint32_t offset_count;
  73. uint64_t current_offset;
  74. uint64_t start_offset = 0;
  75. unsigned char copy_buffer[COPY_BUFFER_SIZE];
  76. int bytes_to_copy;
  77. if (argc != 3) {
  78. printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
  79. return 0;
  80. }
  81. infile = fopen(argv[1], "rb");
  82. if (!infile) {
  83. perror(argv[1]);
  84. return 1;
  85. }
  86. /* traverse through the atoms in the file to make sure that 'moov' is
  87. * at the end */
  88. while (!feof(infile)) {
  89. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  90. break;
  91. }
  92. atom_size = (uint32_t)BE_32(&atom_bytes[0]);
  93. atom_type = BE_32(&atom_bytes[4]);
  94. if ((atom_type != FREE_ATOM) &&
  95. (atom_type != JUNK_ATOM) &&
  96. (atom_type != MDAT_ATOM) &&
  97. (atom_type != MOOV_ATOM) &&
  98. (atom_type != PNOT_ATOM) &&
  99. (atom_type != SKIP_ATOM) &&
  100. (atom_type != WIDE_ATOM) &&
  101. (atom_type != PICT_ATOM) &&
  102. (atom_type != FTYP_ATOM)) {
  103. printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
  104. break;
  105. }
  106. /* keep ftyp atom */
  107. if (atom_type == FTYP_ATOM) {
  108. ftyp_atom_size = atom_size;
  109. ftyp_atom = malloc(ftyp_atom_size);
  110. if (!ftyp_atom) {
  111. printf ("could not allocate 0x%llX byte for ftyp atom\n",
  112. atom_size);
  113. fclose(infile);
  114. return 1;
  115. }
  116. fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
  117. if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
  118. perror(argv[1]);
  119. free(ftyp_atom);
  120. fclose(infile);
  121. return 1;
  122. }
  123. start_offset = ftello(infile);
  124. continue;
  125. }
  126. /* 64-bit special case */
  127. if (atom_size == 1) {
  128. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  129. break;
  130. }
  131. atom_size = BE_64(&atom_bytes[0]);
  132. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
  133. } else {
  134. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
  135. }
  136. }
  137. if (atom_type != MOOV_ATOM) {
  138. printf ("last atom in file was not a moov atom\n");
  139. fclose(infile);
  140. return 0;
  141. }
  142. /* moov atom was, in fact, the last atom in the chunk; load the whole
  143. * moov atom */
  144. fseeko(infile, -atom_size, SEEK_END);
  145. last_offset = ftello(infile);
  146. moov_atom_size = atom_size;
  147. moov_atom = malloc(moov_atom_size);
  148. if (!moov_atom) {
  149. printf ("could not allocate 0x%llX byte for moov atom\n",
  150. atom_size);
  151. fclose(infile);
  152. return 1;
  153. }
  154. if (fread(moov_atom, atom_size, 1, infile) != 1) {
  155. perror(argv[1]);
  156. free(moov_atom);
  157. fclose(infile);
  158. return 1;
  159. }
  160. /* this utility does not support compressed atoms yet, so disqualify
  161. * files with compressed QT atoms */
  162. if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
  163. printf ("this utility does not support compressed moov atoms yet\n");
  164. free(moov_atom);
  165. fclose(infile);
  166. return 1;
  167. }
  168. /* close; will be re-opened later */
  169. fclose(infile);
  170. /* crawl through the moov chunk in search of stco or co64 atoms */
  171. for (i = 4; i < moov_atom_size - 4; i++) {
  172. atom_type = BE_32(&moov_atom[i]);
  173. if (atom_type == STCO_ATOM) {
  174. printf (" patching stco atom...\n");
  175. atom_size = BE_32(&moov_atom[i - 4]);
  176. if (i + atom_size - 4 > moov_atom_size) {
  177. printf (" bad atom size\n");
  178. free(moov_atom);
  179. return 1;
  180. }
  181. offset_count = BE_32(&moov_atom[i + 8]);
  182. for (j = 0; j < offset_count; j++) {
  183. current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
  184. current_offset += moov_atom_size;
  185. moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
  186. moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
  187. moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
  188. moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
  189. }
  190. i += atom_size - 4;
  191. } else if (atom_type == CO64_ATOM) {
  192. printf (" patching co64 atom...\n");
  193. atom_size = BE_32(&moov_atom[i - 4]);
  194. if (i + atom_size - 4 > moov_atom_size) {
  195. printf (" bad atom size\n");
  196. free(moov_atom);
  197. return 1;
  198. }
  199. offset_count = BE_32(&moov_atom[i + 8]);
  200. for (j = 0; j < offset_count; j++) {
  201. current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
  202. current_offset += moov_atom_size;
  203. moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
  204. moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
  205. moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
  206. moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
  207. moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
  208. moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
  209. moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
  210. moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
  211. }
  212. i += atom_size - 4;
  213. }
  214. }
  215. /* re-open the input file and open the output file */
  216. infile = fopen(argv[1], "rb");
  217. if (!infile) {
  218. perror(argv[1]);
  219. free(moov_atom);
  220. return 1;
  221. }
  222. /* seek after ftyp atom if needed */
  223. fseeko(infile, start_offset, SEEK_SET);
  224. outfile = fopen(argv[2], "wb");
  225. if (!outfile) {
  226. perror(argv[2]);
  227. fclose(outfile);
  228. free(moov_atom);
  229. return 1;
  230. }
  231. /* dump the same ftyp atom */
  232. if (ftyp_atom_size > 0) {
  233. printf (" writing ftyp atom...\n");
  234. if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
  235. perror(argv[2]);
  236. goto error_out;
  237. }
  238. }
  239. /* dump the new moov atom */
  240. printf (" writing moov atom...\n");
  241. if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
  242. perror(argv[2]);
  243. goto error_out;
  244. }
  245. /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
  246. printf (" copying rest of file...\n");
  247. while (last_offset) {
  248. if (last_offset > COPY_BUFFER_SIZE)
  249. bytes_to_copy = COPY_BUFFER_SIZE;
  250. else
  251. bytes_to_copy = last_offset;
  252. if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
  253. perror(argv[1]);
  254. goto error_out;
  255. }
  256. if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
  257. perror(argv[2]);
  258. goto error_out;
  259. }
  260. last_offset -= bytes_to_copy;
  261. }
  262. fclose(infile);
  263. fclose(outfile);
  264. free(moov_atom);
  265. return 0;
  266. error_out:
  267. fclose(infile);
  268. fclose(outfile);
  269. free(moov_atom);
  270. return 1;
  271. }