qt-faststart.c 11 KB

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