qt-faststart.c 12 KB

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