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.

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