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.

353 lines
12KB

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