]> git.sesse.net Git - ffmpeg/blobdiff - tools/qt-faststart.c
libx264: add 'psy_trellis' private option.
[ffmpeg] / tools / qt-faststart.c
index a256eb18563d44824bf18d1af6a3cf05a40ebdaa..eefeafdfac39229947f3d556772e80edfcb249af 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * qt-faststart.c, v0.1
+ * qt-faststart.c, v0.2
  * by Mike Melanson (melanson@pcisys.net)
  * This file is placed in the public domain. Use the program however you
  * see fit.
@@ -8,7 +8,7 @@
  * is in front of the data, thus facilitating network streaming.
  *
  * To compile this program, start from the base directory from which you
- * are building FFmpeg and type:
+ * are building Libav and type:
  *  make tools/qt-faststart
  * The qt-faststart program will be built in the tools/ directory. If you
  * do not build the program in this manner, correct results are not
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>
+#include <string.h>
 
 #ifdef __MINGW32__
 #define fseeko(x,y,z)  fseeko64(x,y,z)
@@ -64,6 +65,7 @@
 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
+#define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
 
 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
 
 int main(int argc, char *argv[])
 {
-    FILE *infile;
-    FILE *outfile;
+    FILE *infile  = NULL;
+    FILE *outfile = NULL;
     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
     uint32_t atom_type = 0;
     uint64_t atom_size = 0;
+    uint64_t atom_offset = 0;
     uint64_t last_offset;
-    unsigned char *moov_atom;
-    unsigned char *ftyp_atom = 0;
+    unsigned char *moov_atom = NULL;
+    unsigned char *ftyp_atom = NULL;
     uint64_t moov_atom_size;
     uint64_t ftyp_atom_size = 0;
     uint64_t i, j;
@@ -96,10 +99,15 @@ int main(int argc, char *argv[])
         return 0;
     }
 
+    if (!strcmp(argv[1], argv[2])) {
+        fprintf(stderr, "input and output files need to be different\n");
+        return 1;
+    }
+
     infile = fopen(argv[1], "rb");
     if (!infile) {
         perror(argv[1]);
-        return 1;
+        goto error_out;
     }
 
     /* traverse through the atoms in the file to make sure that 'moov' is
@@ -111,39 +119,23 @@ int main(int argc, char *argv[])
         atom_size = (uint32_t)BE_32(&atom_bytes[0]);
         atom_type = BE_32(&atom_bytes[4]);
 
-        if ((atom_type != FREE_ATOM) &&
-            (atom_type != JUNK_ATOM) &&
-            (atom_type != MDAT_ATOM) &&
-            (atom_type != MOOV_ATOM) &&
-            (atom_type != PNOT_ATOM) &&
-            (atom_type != SKIP_ATOM) &&
-            (atom_type != WIDE_ATOM) &&
-            (atom_type != PICT_ATOM) &&
-            (atom_type != FTYP_ATOM)) {
-            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
-            break;
-        }
-
         /* keep ftyp atom */
         if (atom_type == FTYP_ATOM) {
             ftyp_atom_size = atom_size;
+            free(ftyp_atom);
             ftyp_atom = malloc(ftyp_atom_size);
             if (!ftyp_atom) {
                 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
                         atom_size);
-                fclose(infile);
-                return 1;
+                goto error_out;
             }
             fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
             if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
                 perror(argv[1]);
-                free(ftyp_atom);
-                fclose(infile);
-                return 1;
+                goto error_out;
             }
             start_offset = ftello(infile);
-            continue;
-        }
+        } else {
 
         /* 64-bit special case */
         if (atom_size == 1) {
@@ -156,9 +148,38 @@ int main(int argc, char *argv[])
             fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
         }
     }
+        printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
+               (atom_type >> 24) & 255,
+               (atom_type >> 16) & 255,
+               (atom_type >>  8) & 255,
+               (atom_type >>  0) & 255,
+               atom_offset,
+               atom_size);
+        if ((atom_type != FREE_ATOM) &&
+            (atom_type != JUNK_ATOM) &&
+            (atom_type != MDAT_ATOM) &&
+            (atom_type != MOOV_ATOM) &&
+            (atom_type != PNOT_ATOM) &&
+            (atom_type != SKIP_ATOM) &&
+            (atom_type != WIDE_ATOM) &&
+            (atom_type != PICT_ATOM) &&
+            (atom_type != UUID_ATOM) &&
+            (atom_type != FTYP_ATOM)) {
+            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
+            break;
+        }
+        atom_offset += atom_size;
+
+        /* The atom header is 8 (or 16 bytes), if the atom size (which
+         * includes these 8 or 16 bytes) is less than that, we won't be
+         * able to continue scanning sensibly after this atom, so break. */
+        if (atom_size < 8)
+            break;
+    }
 
     if (atom_type != MOOV_ATOM) {
         printf ("last atom in file was not a moov atom\n");
+        free(ftyp_atom);
         fclose(infile);
         return 0;
     }
@@ -172,27 +193,23 @@ int main(int argc, char *argv[])
     if (!moov_atom) {
         printf ("could not allocate %"PRIu64" byte for moov atom\n",
             atom_size);
-        fclose(infile);
-        return 1;
+        goto error_out;
     }
     if (fread(moov_atom, atom_size, 1, infile) != 1) {
         perror(argv[1]);
-        free(moov_atom);
-        fclose(infile);
-        return 1;
+        goto error_out;
     }
 
     /* this utility does not support compressed atoms yet, so disqualify
      * files with compressed QT atoms */
     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
         printf ("this utility does not support compressed moov atoms yet\n");
-        free(moov_atom);
-        fclose(infile);
-        return 1;
+        goto error_out;
     }
 
     /* close; will be re-opened later */
     fclose(infile);
+    infile = NULL;
 
     /* crawl through the moov chunk in search of stco or co64 atoms */
     for (i = 4; i < moov_atom_size - 4; i++) {
@@ -202,8 +219,7 @@ int main(int argc, char *argv[])
             atom_size = BE_32(&moov_atom[i - 4]);
             if (i + atom_size - 4 > moov_atom_size) {
                 printf (" bad atom size\n");
-                free(moov_atom);
-                return 1;
+                goto error_out;
             }
             offset_count = BE_32(&moov_atom[i + 8]);
             for (j = 0; j < offset_count; j++) {
@@ -220,8 +236,7 @@ int main(int argc, char *argv[])
             atom_size = BE_32(&moov_atom[i - 4]);
             if (i + atom_size - 4 > moov_atom_size) {
                 printf (" bad atom size\n");
-                free(moov_atom);
-                return 1;
+                goto error_out;
             }
             offset_count = BE_32(&moov_atom[i + 8]);
             for (j = 0; j < offset_count; j++) {
@@ -244,8 +259,7 @@ int main(int argc, char *argv[])
     infile = fopen(argv[1], "rb");
     if (!infile) {
         perror(argv[1]);
-        free(moov_atom);
-        return 1;
+        goto error_out;
     }
 
     if (start_offset > 0) { /* seek after ftyp atom */
@@ -256,9 +270,7 @@ int main(int argc, char *argv[])
     outfile = fopen(argv[2], "wb");
     if (!outfile) {
         perror(argv[2]);
-        fclose(outfile);
-        free(moov_atom);
-        return 1;
+        goto error_out;
     }
 
     /* dump the same ftyp atom */
@@ -300,16 +312,16 @@ int main(int argc, char *argv[])
     fclose(infile);
     fclose(outfile);
     free(moov_atom);
-    if (ftyp_atom_size > 0)
-        free(ftyp_atom);
+    free(ftyp_atom);
 
     return 0;
 
 error_out:
-    fclose(infile);
-    fclose(outfile);
+    if (infile)
+        fclose(infile);
+    if (outfile)
+        fclose(outfile);
     free(moov_atom);
-    if (ftyp_atom_size > 0)
-        free(ftyp_atom);
+    free(ftyp_atom);
     return 1;
 }