qt-faststart.c 11 KB

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