qt-faststart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #ifdef __MINGW32__
  30. #define fseeko(x,y,z) fseeko64(x,y,z)
  31. #define ftello(x) ftello64(x)
  32. #endif
  33. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  34. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  35. (((uint8_t*)(x))[1] << 16) | \
  36. (((uint8_t*)(x))[2] << 8) | \
  37. ((uint8_t*)(x))[3])
  38. #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
  39. ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
  40. ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
  41. ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
  42. ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
  43. ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
  44. ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
  45. ((uint64_t)((uint8_t*)(x))[7]))
  46. #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
  47. ( (uint32_t)(unsigned char)(ch3) | \
  48. ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
  49. ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
  50. ( (uint32_t)(unsigned char)(ch0) << 24 ) )
  51. #define QT_ATOM BE_FOURCC
  52. /* top level atoms */
  53. #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
  54. #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
  55. #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
  56. #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
  57. #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
  58. #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
  59. #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
  60. #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
  61. #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
  62. #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
  63. #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
  64. #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
  65. #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
  66. #define ATOM_PREAMBLE_SIZE 8
  67. #define COPY_BUFFER_SIZE 1024
  68. int main(int argc, char *argv[])
  69. {
  70. FILE *infile;
  71. FILE *outfile;
  72. unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
  73. uint32_t atom_type = 0;
  74. uint64_t atom_size = 0;
  75. uint64_t atom_offset = 0;
  76. uint64_t last_offset;
  77. unsigned char *moov_atom;
  78. unsigned char *ftyp_atom = 0;
  79. uint64_t moov_atom_size;
  80. uint64_t ftyp_atom_size = 0;
  81. uint64_t i, j;
  82. uint32_t offset_count;
  83. uint64_t current_offset;
  84. uint64_t start_offset = 0;
  85. unsigned char copy_buffer[COPY_BUFFER_SIZE];
  86. int bytes_to_copy;
  87. if (argc != 3) {
  88. printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
  89. return 0;
  90. }
  91. infile = fopen(argv[1], "rb");
  92. if (!infile) {
  93. perror(argv[1]);
  94. return 1;
  95. }
  96. /* traverse through the atoms in the file to make sure that 'moov' is
  97. * at the end */
  98. while (!feof(infile)) {
  99. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  100. break;
  101. }
  102. atom_size = (uint32_t)BE_32(&atom_bytes[0]);
  103. atom_type = BE_32(&atom_bytes[4]);
  104. /* keep ftyp atom */
  105. if (atom_type == FTYP_ATOM) {
  106. ftyp_atom_size = atom_size;
  107. free(ftyp_atom);
  108. ftyp_atom = malloc(ftyp_atom_size);
  109. if (!ftyp_atom) {
  110. printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
  111. atom_size);
  112. fclose(infile);
  113. return 1;
  114. }
  115. fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
  116. if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
  117. perror(argv[1]);
  118. free(ftyp_atom);
  119. fclose(infile);
  120. return 1;
  121. }
  122. start_offset = ftello(infile);
  123. } else {
  124. /* 64-bit special case */
  125. if (atom_size == 1) {
  126. if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
  127. break;
  128. }
  129. atom_size = BE_64(&atom_bytes[0]);
  130. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
  131. } else {
  132. fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
  133. }
  134. }
  135. printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
  136. (atom_type >> 24) & 255,
  137. (atom_type >> 16) & 255,
  138. (atom_type >> 8) & 255,
  139. (atom_type >> 0) & 255,
  140. atom_offset,
  141. atom_size);
  142. if ((atom_type != FREE_ATOM) &&
  143. (atom_type != JUNK_ATOM) &&
  144. (atom_type != MDAT_ATOM) &&
  145. (atom_type != MOOV_ATOM) &&
  146. (atom_type != PNOT_ATOM) &&
  147. (atom_type != SKIP_ATOM) &&
  148. (atom_type != WIDE_ATOM) &&
  149. (atom_type != PICT_ATOM) &&
  150. (atom_type != UUID_ATOM) &&
  151. (atom_type != FTYP_ATOM)) {
  152. printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
  153. break;
  154. }
  155. atom_offset += atom_size;
  156. }
  157. if (atom_type != MOOV_ATOM) {
  158. printf ("last atom in file was not a moov atom\n");
  159. free(ftyp_atom);
  160. fclose(infile);
  161. return 0;
  162. }
  163. /* moov atom was, in fact, the last atom in the chunk; load the whole
  164. * moov atom */
  165. fseeko(infile, -atom_size, SEEK_END);
  166. last_offset = ftello(infile);
  167. moov_atom_size = atom_size;
  168. moov_atom = malloc(moov_atom_size);
  169. if (!moov_atom) {
  170. printf ("could not allocate %"PRIu64" byte for moov atom\n",
  171. atom_size);
  172. free(ftyp_atom);
  173. fclose(infile);
  174. return 1;
  175. }
  176. if (fread(moov_atom, atom_size, 1, infile) != 1) {
  177. perror(argv[1]);
  178. free(moov_atom);
  179. free(ftyp_atom);
  180. fclose(infile);
  181. return 1;
  182. }
  183. /* this utility does not support compressed atoms yet, so disqualify
  184. * files with compressed QT atoms */
  185. if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
  186. printf ("this utility does not support compressed moov atoms yet\n");
  187. free(moov_atom);
  188. free(ftyp_atom);
  189. fclose(infile);
  190. return 1;
  191. }
  192. /* close; will be re-opened later */
  193. fclose(infile);
  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. free(moov_atom);
  203. free(ftyp_atom);
  204. return 1;
  205. }
  206. offset_count = BE_32(&moov_atom[i + 8]);
  207. for (j = 0; j < offset_count; j++) {
  208. current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
  209. current_offset += moov_atom_size;
  210. moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
  211. moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
  212. moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
  213. moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
  214. }
  215. i += atom_size - 4;
  216. } else if (atom_type == CO64_ATOM) {
  217. printf (" patching co64 atom...\n");
  218. atom_size = BE_32(&moov_atom[i - 4]);
  219. if (i + atom_size - 4 > moov_atom_size) {
  220. printf (" bad atom size\n");
  221. free(moov_atom);
  222. free(ftyp_atom);
  223. return 1;
  224. }
  225. offset_count = BE_32(&moov_atom[i + 8]);
  226. for (j = 0; j < offset_count; j++) {
  227. current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
  228. current_offset += moov_atom_size;
  229. moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
  230. moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
  231. moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
  232. moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
  233. moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
  234. moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
  235. moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
  236. moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
  237. }
  238. i += atom_size - 4;
  239. }
  240. }
  241. /* re-open the input file and open the output file */
  242. infile = fopen(argv[1], "rb");
  243. if (!infile) {
  244. perror(argv[1]);
  245. free(moov_atom);
  246. free(ftyp_atom);
  247. return 1;
  248. }
  249. if (start_offset > 0) { /* seek after ftyp atom */
  250. fseeko(infile, start_offset, SEEK_SET);
  251. last_offset -= start_offset;
  252. }
  253. outfile = fopen(argv[2], "wb");
  254. if (!outfile) {
  255. perror(argv[2]);
  256. fclose(outfile);
  257. free(moov_atom);
  258. free(ftyp_atom);
  259. return 1;
  260. }
  261. /* dump the same ftyp atom */
  262. if (ftyp_atom_size > 0) {
  263. printf (" writing ftyp atom...\n");
  264. if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
  265. perror(argv[2]);
  266. goto error_out;
  267. }
  268. }
  269. /* dump the new moov atom */
  270. printf (" writing moov atom...\n");
  271. if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
  272. perror(argv[2]);
  273. goto error_out;
  274. }
  275. /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
  276. printf (" copying rest of file...\n");
  277. while (last_offset) {
  278. if (last_offset > COPY_BUFFER_SIZE)
  279. bytes_to_copy = COPY_BUFFER_SIZE;
  280. else
  281. bytes_to_copy = last_offset;
  282. if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
  283. perror(argv[1]);
  284. goto error_out;
  285. }
  286. if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
  287. perror(argv[2]);
  288. goto error_out;
  289. }
  290. last_offset -= bytes_to_copy;
  291. }
  292. fclose(infile);
  293. fclose(outfile);
  294. free(moov_atom);
  295. free(ftyp_atom);
  296. return 0;
  297. error_out:
  298. fclose(infile);
  299. fclose(outfile);
  300. free(moov_atom);
  301. free(ftyp_atom);
  302. return 1;
  303. }