2004-03-26 20:42:28 +00:00
|
|
|
/*
|
2009-07-06 05:36:14 +00:00
|
|
|
* qt-faststart.c, v0.2
|
2004-03-26 20:42:28 +00:00
|
|
|
* by Mike Melanson (melanson@pcisys.net)
|
|
|
|
* This file is placed in the public domain. Use the program however you
|
|
|
|
* see fit.
|
|
|
|
*
|
|
|
|
* This utility rearranges a Quicktime file such that the moov atom
|
|
|
|
* is in front of the data, thus facilitating network streaming.
|
|
|
|
*
|
2008-05-19 04:57:57 +00:00
|
|
|
* To compile this program, start from the base directory from which you
|
|
|
|
* are building FFmpeg 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
|
|
|
|
* guaranteed, particularly on 64-bit platforms.
|
2004-03-26 20:42:28 +00:00
|
|
|
* Invoke the program with:
|
|
|
|
* qt-faststart <infile.mov> <outfile.mov>
|
|
|
|
*
|
|
|
|
* Notes: Quicktime files can come in many configurations of top-level
|
|
|
|
* atoms. This utility stipulates that the very last atom in the file needs
|
|
|
|
* to be a moov atom. When given such a file, this utility will rearrange
|
|
|
|
* the top-level atoms by shifting the moov atom from the back of the file
|
|
|
|
* to the front, and patch the chunk offsets along the way. This utility
|
|
|
|
* presently only operates on uncompressed moov atoms.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
2010-06-21 01:05:30 +00:00
|
|
|
#include <string.h>
|
2018-06-01 06:55:45 +00:00
|
|
|
#include <limits.h>
|
2004-03-26 20:42:28 +00:00
|
|
|
|
2017-08-30 19:04:30 +00:00
|
|
|
#ifdef __MINGW32__
|
2014-08-15 19:43:48 +00:00
|
|
|
#undef fseeko
|
2011-12-30 15:54:52 +00:00
|
|
|
#define fseeko(x, y, z) fseeko64(x, y, z)
|
2014-08-15 19:43:48 +00:00
|
|
|
#undef ftello
|
2011-12-30 15:54:52 +00:00
|
|
|
#define ftello(x) ftello64(x)
|
2012-08-28 19:40:27 +00:00
|
|
|
#elif defined(_WIN32)
|
2014-08-15 19:43:48 +00:00
|
|
|
#undef fseeko
|
2012-08-28 19:40:27 +00:00
|
|
|
#define fseeko(x, y, z) _fseeki64(x, y, z)
|
2014-08-15 19:43:48 +00:00
|
|
|
#undef ftello
|
2012-08-28 19:40:27 +00:00
|
|
|
#define ftello(x) _ftelli64(x)
|
2006-03-27 16:34:21 +00:00
|
|
|
#endif
|
|
|
|
|
2012-09-30 19:53:26 +00:00
|
|
|
#define MIN(a,b) ((a) > (b) ? (b) : (a))
|
2012-09-30 19:52:59 +00:00
|
|
|
|
2014-02-28 10:19:49 +00:00
|
|
|
#define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) | \
|
|
|
|
(((uint8_t*)(x))[1] << 16) | \
|
|
|
|
(((uint8_t*)(x))[2] << 8) | \
|
|
|
|
((uint8_t*)(x))[3])
|
2011-12-30 15:54:52 +00:00
|
|
|
|
|
|
|
#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
|
|
|
|
((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
|
|
|
|
((uint64_t)( (uint8_t*)(x))[7]))
|
|
|
|
|
2018-06-01 06:55:45 +00:00
|
|
|
#define AV_WB32(p, val) { \
|
|
|
|
((uint8_t*)(p))[0] = ((val) >> 24) & 0xff; \
|
|
|
|
((uint8_t*)(p))[1] = ((val) >> 16) & 0xff; \
|
|
|
|
((uint8_t*)(p))[2] = ((val) >> 8) & 0xff; \
|
|
|
|
((uint8_t*)(p))[3] = (val) & 0xff; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AV_WB64(p, val) { \
|
|
|
|
AV_WB32(p, (val) >> 32) \
|
|
|
|
AV_WB32(p + 4, val) \
|
|
|
|
}
|
|
|
|
|
2011-12-30 15:54:52 +00:00
|
|
|
#define BE_FOURCC(ch0, ch1, ch2, ch3) \
|
|
|
|
( (uint32_t)(unsigned char)(ch3) | \
|
|
|
|
((uint32_t)(unsigned char)(ch2) << 8) | \
|
|
|
|
((uint32_t)(unsigned char)(ch1) << 16) | \
|
|
|
|
((uint32_t)(unsigned char)(ch0) << 24) )
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
#define QT_ATOM BE_FOURCC
|
|
|
|
/* top level atoms */
|
|
|
|
#define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
|
|
|
|
#define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
|
|
|
|
#define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
|
|
|
|
#define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
|
|
|
|
#define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
|
|
|
|
#define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
|
|
|
|
#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')
|
2009-07-06 05:36:14 +00:00
|
|
|
#define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
#define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
|
2018-06-01 06:55:45 +00:00
|
|
|
#define TRAK_ATOM QT_ATOM('t', 'r', 'a', 'k')
|
|
|
|
#define MDIA_ATOM QT_ATOM('m', 'd', 'i', 'a')
|
|
|
|
#define MINF_ATOM QT_ATOM('m', 'i', 'n', 'f')
|
|
|
|
#define STBL_ATOM QT_ATOM('s', 't', 'b', 'l')
|
2004-03-26 20:42:28 +00:00
|
|
|
#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
|
|
|
|
#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
|
|
|
|
|
2011-12-30 15:54:52 +00:00
|
|
|
#define ATOM_PREAMBLE_SIZE 8
|
2012-09-30 19:32:26 +00:00
|
|
|
#define COPY_BUFFER_SIZE 33554432
|
2018-06-13 08:48:20 +00:00
|
|
|
#define MAX_FTYP_ATOM_SIZE 1048576
|
2004-03-26 20:42:28 +00:00
|
|
|
|
2018-06-01 06:55:45 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t header_size;
|
|
|
|
uint64_t size;
|
|
|
|
unsigned char *data;
|
|
|
|
} atom_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint64_t moov_atom_size;
|
|
|
|
uint64_t stco_offset_count;
|
|
|
|
uint64_t stco_data_size;
|
|
|
|
int stco_overflow;
|
|
|
|
uint32_t depth;
|
|
|
|
} update_chunk_offsets_context_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char *dest;
|
|
|
|
uint64_t original_moov_size;
|
|
|
|
uint64_t new_moov_size;
|
|
|
|
} upgrade_stco_context_t;
|
|
|
|
|
|
|
|
typedef int (*parse_atoms_callback_t)(void *context, atom_t *atom);
|
|
|
|
|
|
|
|
static int parse_atoms(
|
|
|
|
unsigned char *buf,
|
|
|
|
uint64_t size,
|
|
|
|
parse_atoms_callback_t callback,
|
|
|
|
void *context)
|
|
|
|
{
|
|
|
|
unsigned char *pos = buf;
|
|
|
|
unsigned char *end = pos + size;
|
|
|
|
atom_t atom;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (end - pos >= ATOM_PREAMBLE_SIZE) {
|
|
|
|
atom.size = BE_32(pos);
|
|
|
|
atom.type = BE_32(pos + 4);
|
|
|
|
pos += ATOM_PREAMBLE_SIZE;
|
|
|
|
atom.header_size = ATOM_PREAMBLE_SIZE;
|
|
|
|
|
|
|
|
switch (atom.size) {
|
|
|
|
case 1:
|
|
|
|
if (end - pos < 8) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "not enough room for 64 bit atom size\n");
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom.size = BE_64(pos);
|
|
|
|
pos += 8;
|
|
|
|
atom.header_size = ATOM_PREAMBLE_SIZE + 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
atom.size = ATOM_PREAMBLE_SIZE + end - pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atom.size < atom.header_size) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "atom size %"PRIu64" too small\n", atom.size);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom.size -= atom.header_size;
|
|
|
|
|
|
|
|
if (atom.size > end - pos) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "atom size %"PRIu64" too big\n", atom.size);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom.data = pos;
|
|
|
|
ret = callback(context, &atom);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += atom.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_stco_offsets(update_chunk_offsets_context_t *context, atom_t *atom)
|
|
|
|
{
|
|
|
|
uint32_t current_offset;
|
|
|
|
uint32_t offset_count;
|
|
|
|
unsigned char *pos;
|
|
|
|
unsigned char *end;
|
|
|
|
|
|
|
|
printf(" patching stco atom...\n");
|
|
|
|
if (atom->size < 8) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "stco atom size %"PRIu64" too small\n", atom->size);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset_count = BE_32(atom->data + 4);
|
|
|
|
if (offset_count > (atom->size - 8) / 4) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "stco offset count %"PRIu32" too big\n", offset_count);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->stco_offset_count += offset_count;
|
|
|
|
context->stco_data_size += atom->size - 8;
|
|
|
|
|
|
|
|
for (pos = atom->data + 8, end = pos + offset_count * 4;
|
|
|
|
pos < end;
|
|
|
|
pos += 4) {
|
|
|
|
current_offset = BE_32(pos);
|
|
|
|
if (current_offset > UINT_MAX - context->moov_atom_size) {
|
|
|
|
context->stco_overflow = 1;
|
|
|
|
}
|
|
|
|
current_offset += context->moov_atom_size;
|
|
|
|
AV_WB32(pos, current_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_co64_offsets(update_chunk_offsets_context_t *context, atom_t *atom)
|
|
|
|
{
|
|
|
|
uint64_t current_offset;
|
|
|
|
uint32_t offset_count;
|
|
|
|
unsigned char *pos;
|
|
|
|
unsigned char *end;
|
|
|
|
|
|
|
|
printf(" patching co64 atom...\n");
|
|
|
|
if (atom->size < 8) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "co64 atom size %"PRIu64" too small\n", atom->size);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset_count = BE_32(atom->data + 4);
|
|
|
|
if (offset_count > (atom->size - 8) / 8) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "co64 offset count %"PRIu32" too big\n", offset_count);
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos = atom->data + 8, end = pos + offset_count * 8;
|
|
|
|
pos < end;
|
|
|
|
pos += 8) {
|
|
|
|
current_offset = BE_64(pos);
|
|
|
|
current_offset += context->moov_atom_size;
|
|
|
|
AV_WB64(pos, current_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_chunk_offsets_callback(void *ctx, atom_t *atom)
|
|
|
|
{
|
|
|
|
update_chunk_offsets_context_t *context = ctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (atom->type) {
|
|
|
|
case STCO_ATOM:
|
|
|
|
return update_stco_offsets(context, atom);
|
|
|
|
|
|
|
|
case CO64_ATOM:
|
|
|
|
return update_co64_offsets(context, atom);
|
|
|
|
|
|
|
|
case MOOV_ATOM:
|
|
|
|
case TRAK_ATOM:
|
|
|
|
case MDIA_ATOM:
|
|
|
|
case MINF_ATOM:
|
|
|
|
case STBL_ATOM:
|
|
|
|
context->depth++;
|
|
|
|
if (context->depth > 10) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "atoms too deeply nested\n");
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = parse_atoms(
|
|
|
|
atom->data,
|
|
|
|
atom->size,
|
|
|
|
update_chunk_offsets_callback,
|
|
|
|
context);
|
|
|
|
context->depth--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_atom_size(unsigned char *header, uint32_t header_size, uint64_t size)
|
|
|
|
{
|
|
|
|
switch (header_size) {
|
|
|
|
case 8:
|
|
|
|
AV_WB32(header, size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
AV_WB64(header + 8, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void upgrade_stco_atom(upgrade_stco_context_t *context, atom_t *atom)
|
|
|
|
{
|
|
|
|
unsigned char *pos;
|
|
|
|
unsigned char *end;
|
|
|
|
uint64_t new_offset;
|
|
|
|
uint32_t offset_count;
|
|
|
|
uint32_t original_offset;
|
|
|
|
|
|
|
|
/* Note: not performing validations since they were performed on the first pass */
|
|
|
|
|
|
|
|
offset_count = BE_32(atom->data + 4);
|
|
|
|
|
|
|
|
/* write the header */
|
|
|
|
memcpy(context->dest, atom->data - atom->header_size, atom->header_size + 8);
|
|
|
|
AV_WB32(context->dest + 4, CO64_ATOM);
|
|
|
|
set_atom_size(context->dest, atom->header_size, atom->header_size + 8 + offset_count * 8);
|
|
|
|
context->dest += atom->header_size + 8;
|
|
|
|
|
|
|
|
/* write the data */
|
|
|
|
for (pos = atom->data + 8, end = pos + offset_count * 4;
|
|
|
|
pos < end;
|
|
|
|
pos += 4) {
|
|
|
|
original_offset = BE_32(pos) - context->original_moov_size;
|
|
|
|
new_offset = (uint64_t)original_offset + context->new_moov_size;
|
|
|
|
AV_WB64(context->dest, new_offset);
|
|
|
|
context->dest += 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int upgrade_stco_callback(void *ctx, atom_t *atom)
|
|
|
|
{
|
|
|
|
upgrade_stco_context_t *context = ctx;
|
|
|
|
unsigned char *start_pos;
|
|
|
|
uint64_t copy_size;
|
|
|
|
|
|
|
|
switch (atom->type) {
|
|
|
|
case STCO_ATOM:
|
|
|
|
upgrade_stco_atom(context, atom);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOOV_ATOM:
|
|
|
|
case TRAK_ATOM:
|
|
|
|
case MDIA_ATOM:
|
|
|
|
case MINF_ATOM:
|
|
|
|
case STBL_ATOM:
|
|
|
|
/* write the atom header */
|
|
|
|
memcpy(context->dest, atom->data - atom->header_size, atom->header_size);
|
|
|
|
start_pos = context->dest;
|
|
|
|
context->dest += atom->header_size;
|
|
|
|
|
|
|
|
/* parse internal atoms*/
|
|
|
|
if (parse_atoms(
|
|
|
|
atom->data,
|
|
|
|
atom->size,
|
|
|
|
upgrade_stco_callback,
|
|
|
|
context) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update the atom size */
|
|
|
|
set_atom_size(start_pos, atom->header_size, context->dest - start_pos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
copy_size = atom->header_size + atom->size;
|
|
|
|
memcpy(context->dest, atom->data - atom->header_size, copy_size);
|
|
|
|
context->dest += copy_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_moov_atom(
|
|
|
|
unsigned char **moov_atom,
|
|
|
|
uint64_t *moov_atom_size)
|
|
|
|
{
|
|
|
|
update_chunk_offsets_context_t update_context = { 0 };
|
|
|
|
upgrade_stco_context_t upgrade_context;
|
|
|
|
unsigned char *new_moov_atom;
|
|
|
|
|
|
|
|
update_context.moov_atom_size = *moov_atom_size;
|
|
|
|
|
|
|
|
if (parse_atoms(
|
|
|
|
*moov_atom,
|
|
|
|
*moov_atom_size,
|
|
|
|
update_chunk_offsets_callback,
|
|
|
|
&update_context) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!update_context.stco_overflow) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" upgrading stco atoms to co64...\n");
|
|
|
|
upgrade_context.new_moov_size = *moov_atom_size +
|
|
|
|
update_context.stco_offset_count * 8 -
|
|
|
|
update_context.stco_data_size;
|
|
|
|
|
|
|
|
new_moov_atom = malloc(upgrade_context.new_moov_size);
|
|
|
|
if (new_moov_atom == NULL) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "could not allocate %"PRIu64" bytes for updated moov atom\n",
|
2018-06-01 06:55:45 +00:00
|
|
|
upgrade_context.new_moov_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_context.original_moov_size = *moov_atom_size;
|
|
|
|
upgrade_context.dest = new_moov_atom;
|
|
|
|
|
|
|
|
if (parse_atoms(
|
|
|
|
*moov_atom,
|
|
|
|
*moov_atom_size,
|
|
|
|
upgrade_stco_callback,
|
|
|
|
&upgrade_context) < 0) {
|
|
|
|
free(new_moov_atom);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(*moov_atom);
|
|
|
|
*moov_atom = new_moov_atom;
|
|
|
|
*moov_atom_size = upgrade_context.new_moov_size;
|
|
|
|
|
|
|
|
if (upgrade_context.dest != *moov_atom + *moov_atom_size) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "unexpected - wrong number of moov bytes written\n");
|
2018-06-01 06:55:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:42:28 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-05-13 19:17:15 +00:00
|
|
|
FILE *infile = NULL;
|
|
|
|
FILE *outfile = NULL;
|
2004-03-26 20:42:28 +00:00
|
|
|
unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
|
2011-12-30 15:54:52 +00:00
|
|
|
uint32_t atom_type = 0;
|
|
|
|
uint64_t atom_size = 0;
|
2009-07-15 08:57:27 +00:00
|
|
|
uint64_t atom_offset = 0;
|
2012-10-29 21:05:33 +00:00
|
|
|
int64_t last_offset;
|
2010-05-13 19:17:15 +00:00
|
|
|
unsigned char *moov_atom = NULL;
|
2010-05-13 19:18:27 +00:00
|
|
|
unsigned char *ftyp_atom = NULL;
|
2004-03-26 20:42:28 +00:00
|
|
|
uint64_t moov_atom_size;
|
2006-03-16 16:37:05 +00:00
|
|
|
uint64_t ftyp_atom_size = 0;
|
2012-10-29 21:05:33 +00:00
|
|
|
int64_t start_offset = 0;
|
2012-09-30 19:32:26 +00:00
|
|
|
unsigned char *copy_buffer = NULL;
|
2004-03-26 20:42:28 +00:00
|
|
|
int bytes_to_copy;
|
2018-07-11 23:24:39 +00:00
|
|
|
uint64_t free_size = 0;
|
|
|
|
uint64_t moov_size = 0;
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
if (argc != 3) {
|
2014-01-07 19:59:04 +00:00
|
|
|
printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
|
|
|
|
"Note: alternatively you can use -movflags +faststart in ffmpeg\n");
|
2004-03-26 20:42:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-21 01:05:30 +00:00
|
|
|
if (!strcmp(argv[1], argv[2])) {
|
|
|
|
fprintf(stderr, "input and output files need to be different\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:42:28 +00:00
|
|
|
infile = fopen(argv[1], "rb");
|
|
|
|
if (!infile) {
|
|
|
|
perror(argv[1]);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* traverse through the atoms in the file to make sure that 'moov' is
|
|
|
|
* at the end */
|
|
|
|
while (!feof(infile)) {
|
|
|
|
if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
|
|
|
|
break;
|
|
|
|
}
|
2014-02-28 10:19:49 +00:00
|
|
|
atom_size = BE_32(&atom_bytes[0]);
|
2004-03-26 20:42:28 +00:00
|
|
|
atom_type = BE_32(&atom_bytes[4]);
|
|
|
|
|
2006-03-16 16:37:05 +00:00
|
|
|
/* keep ftyp atom */
|
|
|
|
if (atom_type == FTYP_ATOM) {
|
2018-06-13 08:48:20 +00:00
|
|
|
if (atom_size > MAX_FTYP_ATOM_SIZE) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "ftyp atom size %"PRIu64" too big\n",
|
2018-06-13 08:48:20 +00:00
|
|
|
atom_size);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2006-03-16 16:37:05 +00:00
|
|
|
ftyp_atom_size = atom_size;
|
2010-05-09 17:46:42 +00:00
|
|
|
free(ftyp_atom);
|
2006-03-16 16:37:05 +00:00
|
|
|
ftyp_atom = malloc(ftyp_atom_size);
|
|
|
|
if (!ftyp_atom) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "could not allocate %"PRIu64" bytes for ftyp atom\n",
|
2011-12-30 15:54:52 +00:00
|
|
|
atom_size);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2006-03-16 16:37:05 +00:00
|
|
|
}
|
2012-10-22 20:42:51 +00:00
|
|
|
if (fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR) ||
|
2014-03-01 12:54:35 +00:00
|
|
|
fread(ftyp_atom, atom_size, 1, infile) != 1 ||
|
2012-10-24 22:39:33 +00:00
|
|
|
(start_offset = ftello(infile)) < 0) {
|
2006-03-16 16:37:05 +00:00
|
|
|
perror(argv[1]);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2006-03-16 16:37:05 +00:00
|
|
|
}
|
2009-07-15 08:57:27 +00:00
|
|
|
} else {
|
2012-10-22 20:42:51 +00:00
|
|
|
int ret;
|
2011-12-29 16:43:30 +00:00
|
|
|
/* 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]);
|
2012-10-22 20:42:51 +00:00
|
|
|
ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
|
2011-12-29 16:43:30 +00:00
|
|
|
} else {
|
2012-10-22 20:42:51 +00:00
|
|
|
ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
|
|
|
|
}
|
2012-10-22 20:42:51 +00:00
|
|
|
if (ret) {
|
2012-10-22 20:42:51 +00:00
|
|
|
perror(argv[1]);
|
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-15 08:57:27 +00:00
|
|
|
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)) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "encountered non-QT top-level atom (is this a QuickTime file?)\n");
|
2009-07-15 08:57:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
atom_offset += atom_size;
|
2010-05-13 19:22:56 +00:00
|
|
|
|
|
|
|
/* 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;
|
2018-07-11 23:24:39 +00:00
|
|
|
|
|
|
|
if (atom_type == MOOV_ATOM)
|
|
|
|
moov_size = atom_size;
|
|
|
|
|
|
|
|
if (moov_size && atom_type == FREE_ATOM) {
|
|
|
|
free_size += atom_size;
|
|
|
|
atom_type = MOOV_ATOM;
|
|
|
|
atom_size = moov_size;
|
|
|
|
}
|
2009-07-15 08:57:27 +00:00
|
|
|
}
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
if (atom_type != MOOV_ATOM) {
|
2011-12-30 15:54:52 +00:00
|
|
|
printf("last atom in file was not a moov atom\n");
|
2010-05-01 21:05:28 +00:00
|
|
|
free(ftyp_atom);
|
2004-03-26 20:42:28 +00:00
|
|
|
fclose(infile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:18:05 +00:00
|
|
|
if (atom_size < 16) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "bad moov atom size\n");
|
2018-05-29 13:18:05 +00:00
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:42:28 +00:00
|
|
|
/* moov atom was, in fact, the last atom in the chunk; load the whole
|
|
|
|
* moov atom */
|
2018-07-11 23:24:39 +00:00
|
|
|
if (fseeko(infile, -(atom_size + free_size), SEEK_END)) {
|
2012-10-22 20:42:51 +00:00
|
|
|
perror(argv[1]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-12-30 15:54:52 +00:00
|
|
|
last_offset = ftello(infile);
|
2012-10-24 22:39:33 +00:00
|
|
|
if (last_offset < 0) {
|
|
|
|
perror(argv[1]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2004-03-26 20:42:28 +00:00
|
|
|
moov_atom_size = atom_size;
|
2011-12-30 15:54:52 +00:00
|
|
|
moov_atom = malloc(moov_atom_size);
|
2004-03-26 20:42:28 +00:00
|
|
|
if (!moov_atom) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
if (fread(moov_atom, atom_size, 1, infile) != 1) {
|
|
|
|
perror(argv[1]);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this utility does not support compressed atoms yet, so disqualify
|
|
|
|
* files with compressed QT atoms */
|
|
|
|
if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "this utility does not support compressed moov atoms yet\n");
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close; will be re-opened later */
|
|
|
|
fclose(infile);
|
2010-05-13 19:17:15 +00:00
|
|
|
infile = NULL;
|
2004-03-26 20:42:28 +00:00
|
|
|
|
2018-06-01 06:55:45 +00:00
|
|
|
if (update_moov_atom(&moov_atom, &moov_atom_size) < 0) {
|
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* re-open the input file and open the output file */
|
|
|
|
infile = fopen(argv[1], "rb");
|
|
|
|
if (!infile) {
|
|
|
|
perror(argv[1]);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
2006-04-10 17:18:35 +00:00
|
|
|
|
|
|
|
if (start_offset > 0) { /* seek after ftyp atom */
|
2012-10-22 20:42:51 +00:00
|
|
|
if (fseeko(infile, start_offset, SEEK_SET)) {
|
|
|
|
perror(argv[1]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2006-04-10 17:18:35 +00:00
|
|
|
last_offset -= start_offset;
|
|
|
|
}
|
2006-03-16 16:37:05 +00:00
|
|
|
|
2004-03-26 20:42:28 +00:00
|
|
|
outfile = fopen(argv[2], "wb");
|
|
|
|
if (!outfile) {
|
|
|
|
perror(argv[2]);
|
2010-05-13 19:17:15 +00:00
|
|
|
goto error_out;
|
2004-03-26 20:42:28 +00:00
|
|
|
}
|
|
|
|
|
2006-03-16 16:37:05 +00:00
|
|
|
/* dump the same ftyp atom */
|
|
|
|
if (ftyp_atom_size > 0) {
|
2011-12-30 15:54:52 +00:00
|
|
|
printf(" writing ftyp atom...\n");
|
2006-03-16 16:37:05 +00:00
|
|
|
if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
|
|
|
|
perror(argv[2]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:42:28 +00:00
|
|
|
/* dump the new moov atom */
|
2011-12-30 15:54:52 +00:00
|
|
|
printf(" writing moov atom...\n");
|
2004-03-26 20:42:28 +00:00
|
|
|
if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
|
|
|
|
perror(argv[2]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
|
2014-03-01 12:47:08 +00:00
|
|
|
bytes_to_copy = MIN(COPY_BUFFER_SIZE, last_offset);
|
2012-09-30 19:52:59 +00:00
|
|
|
copy_buffer = malloc(bytes_to_copy);
|
2012-09-30 19:32:26 +00:00
|
|
|
if (!copy_buffer) {
|
2018-06-14 11:06:14 +00:00
|
|
|
fprintf(stderr, "could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
|
2012-09-30 19:32:26 +00:00
|
|
|
goto error_out;
|
|
|
|
}
|
2011-12-30 15:54:52 +00:00
|
|
|
printf(" copying rest of file...\n");
|
2004-03-26 20:42:28 +00:00
|
|
|
while (last_offset) {
|
2014-03-01 12:47:08 +00:00
|
|
|
bytes_to_copy = MIN(bytes_to_copy, last_offset);
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
|
|
|
|
perror(argv[1]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
|
|
|
|
perror(argv[2]);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
last_offset -= bytes_to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
free(moov_atom);
|
2010-05-01 21:03:52 +00:00
|
|
|
free(ftyp_atom);
|
2012-09-30 19:32:26 +00:00
|
|
|
free(copy_buffer);
|
2004-03-26 20:42:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_out:
|
2010-05-13 19:17:15 +00:00
|
|
|
if (infile)
|
2010-05-13 19:17:49 +00:00
|
|
|
fclose(infile);
|
2010-05-13 19:17:15 +00:00
|
|
|
if (outfile)
|
2010-05-13 19:17:49 +00:00
|
|
|
fclose(outfile);
|
2004-03-26 20:42:28 +00:00
|
|
|
free(moov_atom);
|
2010-05-01 21:03:52 +00:00
|
|
|
free(ftyp_atom);
|
2012-09-30 19:32:26 +00:00
|
|
|
free(copy_buffer);
|
2004-03-26 20:42:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|