You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
13KB

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