qt-faststart.c 10 KB

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