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.

318 lines
11KB

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