X-Git-Url: http://git.videolan.org/?p=ffmpeg.git;a=blobdiff_plain;f=qt-faststart.c;h=2cc68631520de679b6c20d8a4b20a43f0b34d4f9;hp=e19125317afbe5779fa8c0d5dc9e102bd0f7c927;hb=dfca23e361cfcd557a0ec65ee0469e11bfc70f3a;hpb=d4fcd7c61afe2e997655e7b660c674a3311bfd1e diff --git a/qt-faststart.c b/qt-faststart.c index e19125317a..2cc6863152 100644 --- a/qt-faststart.c +++ b/qt-faststart.c @@ -24,6 +24,11 @@ #include #include +#ifdef __MINGW32__ +#define fseeko(x,y,z) fseeko64(x,y,z) +#define ftello(x) ftello64(x) +#endif + #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1]) #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \ (((uint8_t*)(x))[1] << 16) | \ @@ -69,13 +74,16 @@ int main(int argc, char *argv[]) FILE *outfile; unsigned char atom_bytes[ATOM_PREAMBLE_SIZE]; uint32_t atom_type = 0; - uint64_t atom_size; + uint64_t atom_size = 0; uint64_t last_offset; unsigned char *moov_atom; + unsigned char *ftyp_atom = 0; uint64_t moov_atom_size; + uint64_t ftyp_atom_size = 0; uint64_t i, j; uint32_t offset_count; uint64_t current_offset; + uint64_t start_offset = 0; unsigned char copy_buffer[COPY_BUFFER_SIZE]; int bytes_to_copy; @@ -96,7 +104,7 @@ int main(int argc, char *argv[]) if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) { break; } - atom_size = BE_32(&atom_bytes[0]); + atom_size = (uint32_t)BE_32(&atom_bytes[0]); atom_type = BE_32(&atom_bytes[4]); if ((atom_type != FREE_ATOM) && @@ -112,15 +120,36 @@ int main(int argc, char *argv[]) break; } + /* keep ftyp atom */ + if (atom_type == FTYP_ATOM) { + ftyp_atom_size = atom_size; + ftyp_atom = malloc(ftyp_atom_size); + if (!ftyp_atom) { + printf ("could not allocate 0x%llX byte for ftyp atom\n", + atom_size); + fclose(infile); + return 1; + } + 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; + } + start_offset = ftello(infile); + continue; + } + /* 64-bit special case */ if (atom_size == 1) { if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) { break; } atom_size = BE_64(&atom_bytes[0]); - fseek(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR); + fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR); } else { - fseek(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR); + fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR); } } @@ -132,8 +161,8 @@ int main(int argc, char *argv[]) /* moov atom was, in fact, the last atom in the chunk; load the whole * moov atom */ - fseek(infile, -atom_size, SEEK_END); - last_offset = (uint64_t)ftell(infile); + fseeko(infile, -atom_size, SEEK_END); + last_offset = ftello(infile); moov_atom_size = atom_size; moov_atom = malloc(moov_atom_size); if (!moov_atom) { @@ -214,6 +243,9 @@ int main(int argc, char *argv[]) free(moov_atom); return 1; } + /* seek after ftyp atom if needed */ + fseeko(infile, start_offset, SEEK_SET); + outfile = fopen(argv[2], "wb"); if (!outfile) { perror(argv[2]); @@ -222,6 +254,15 @@ int main(int argc, char *argv[]) return 1; } + /* dump the same ftyp atom */ + if (ftyp_atom_size > 0) { + printf (" writing ftyp atom...\n"); + if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) { + perror(argv[2]); + goto error_out; + } + } + /* dump the new moov atom */ printf (" writing moov atom...\n"); if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) { @@ -252,6 +293,8 @@ int main(int argc, char *argv[]) fclose(infile); fclose(outfile); free(moov_atom); + if (ftyp_atom_size > 0) + free(ftyp_atom); return 0; @@ -259,5 +302,7 @@ error_out: fclose(infile); fclose(outfile); free(moov_atom); + if (ftyp_atom_size > 0) + free(ftyp_atom); return 1; }