ports/143279: new port: multimedia/qt-faststart-0.1

J.R. Oldroyd fbsd at opal.com
Tue Jan 26 23:40:06 UTC 2010


>Number:         143279
>Category:       ports
>Synopsis:       new port: multimedia/qt-faststart-0.1
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jan 26 23:40:05 UTC 2010
>Closed-Date:
>Last-Modified:
>Originator:     J.R. Oldroyd
>Release:        FreeBSD 8.0-RELEASE-p1 amd64
>Organization:
>Environment:
System: FreeBSD xx.opal.com 8.0-RELEASE-p1 FreeBSD 8.0-RELEASE-p1 #4: Sun Jan 24 15:29:35 EST 2010 xx at xx.opal.com:/usr/src/sys/amd64/compile/XX amd64
>Description:
This utility rearranges a Quicktime file such that the moov atom
is in front of the data, thus facilitating network streaming.

This is needed to prepare .mp4 and .3gp files for playing on an
Android device.
>How-To-Repeat:
>Fix:
# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	multimedia/qt-faststart
#	multimedia/qt-faststart/files
#	multimedia/qt-faststart/files/qt-faststart.c
#	multimedia/qt-faststart/Makefile
#	multimedia/qt-faststart/pkg-descr
#	multimedia/qt-faststart/pkg-plist
#
echo c - multimedia/qt-faststart
mkdir -p multimedia/qt-faststart > /dev/null 2>&1
echo c - multimedia/qt-faststart/files
mkdir -p multimedia/qt-faststart/files > /dev/null 2>&1
echo x - multimedia/qt-faststart/files/qt-faststart.c
sed 's/^X//' >multimedia/qt-faststart/files/qt-faststart.c << 'cd8a0833e082e498c35855f6c0fb99c6'
X/*
X * qt-faststart.c, v0.1
X * by Mike Melanson (melanson at pcisys.net)
X * This file is placed in the public domain. Use the program however you
X * see fit.
X *
X * This utility rearranges a Quicktime file such that the moov atom
X * is in front of the data, thus facilitating network streaming.
X *
X * Compile this program using:
X *  cc qt-faststart.c -o qt-faststart
X * Invoke the program with:
X *  qt-faststart <infile.mov> <outfile.mov>
X *
X * Notes: Quicktime files can come in many configurations of top-level
X * atoms. This utility stipulates that the very last atom in the file needs
X * to be a moov atom. When given such a file, this utility will rearrange
X * the top-level atoms by shifting the moov atom from the back of the file
X * to the front, and patch the chunk offsets along the way. This utility
X * presently only operates on uncompressed moov atoms.
X */
X
X#include <stdio.h>
X#include <stdlib.h>
X#include <inttypes.h>
X
X#ifdef __MINGW32__
X#define fseeko(x,y,z)  fseeko64(x,y,z)
X#define ftello(x)      ftello64(x)
X#endif
X
X#define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
X#define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
X                  (((uint8_t*)(x))[1] << 16) | \
X                  (((uint8_t*)(x))[2] << 8) | \
X                   ((uint8_t*)(x))[3])
X#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
X                  ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
X                  ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
X                  ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
X                  ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
X                  ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
X                  ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
X                  ((uint64_t)((uint8_t*)(x))[7]))
X
X#define BE_FOURCC( ch0, ch1, ch2, ch3 )             \
X        ( (uint32_t)(unsigned char)(ch3) |          \
X        ( (uint32_t)(unsigned char)(ch2) << 8 ) |   \
X        ( (uint32_t)(unsigned char)(ch1) << 16 ) |  \
X        ( (uint32_t)(unsigned char)(ch0) << 24 ) )
X
X#define QT_ATOM BE_FOURCC
X/* top level atoms */
X#define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
X#define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
X#define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
X#define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
X#define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
X#define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
X#define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
X#define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
X#define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
X
X#define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
X#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
X#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
X
X#define ATOM_PREAMBLE_SIZE 8
X#define COPY_BUFFER_SIZE 1024
X
Xint main(int argc, char *argv[])
X{
X    FILE *infile;
X    FILE *outfile;
X    unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
X    uint32_t atom_type = 0;
X    uint64_t atom_size = 0;
X    uint64_t last_offset;
X    unsigned char *moov_atom;
X    unsigned char *ftyp_atom = 0;
X    uint64_t moov_atom_size;
X    uint64_t ftyp_atom_size = 0;
X    uint64_t i, j;
X    uint32_t offset_count;
X    uint64_t current_offset;
X    uint64_t start_offset = 0;
X    unsigned char copy_buffer[COPY_BUFFER_SIZE];
X    int bytes_to_copy;
X
X    if (argc != 3) {
X        printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
X        return 0;
X    }
X
X    infile = fopen(argv[1], "rb");
X    if (!infile) {
X        perror(argv[1]);
X        return 1;
X    }
X
X    /* traverse through the atoms in the file to make sure that 'moov' is
X     * at the end */
X    while (!feof(infile)) {
X        if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
X            break;
X        }
X        atom_size = (uint32_t)BE_32(&atom_bytes[0]);
X        atom_type = BE_32(&atom_bytes[4]);
X
X        if ((atom_type != FREE_ATOM) &&
X            (atom_type != JUNK_ATOM) &&
X            (atom_type != MDAT_ATOM) &&
X            (atom_type != MOOV_ATOM) &&
X            (atom_type != PNOT_ATOM) &&
X            (atom_type != SKIP_ATOM) &&
X            (atom_type != WIDE_ATOM) &&
X            (atom_type != PICT_ATOM) &&
X            (atom_type != FTYP_ATOM)) {
X            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
X            break;
X        }
X
X        /* keep ftyp atom */
X        if (atom_type == FTYP_ATOM) {
X            ftyp_atom_size = atom_size;
X            ftyp_atom = malloc(ftyp_atom_size);
X            if (!ftyp_atom) {
X                printf ("could not allocate 0x%llX byte for ftyp atom\n",
X                        atom_size);
X                fclose(infile);
X                return 1;
X            }
X            fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
X            if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
X                perror(argv[1]);
X                free(ftyp_atom);
X                fclose(infile);
X                return 1;
X            }
X            start_offset = ftello(infile);
X            continue;
X        }
X
X        /* 64-bit special case */
X        if (atom_size == 1) {
X            if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
X                break;
X            }
X            atom_size = BE_64(&atom_bytes[0]);
X            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
X        } else {
X            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
X        }
X    }
X
X    if (atom_type != MOOV_ATOM) {
X        printf ("last atom in file was not a moov atom\n");
X        fclose(infile);
X        return 0;
X    }
X
X    /* moov atom was, in fact, the last atom in the chunk; load the whole
X     * moov atom */
X    fseeko(infile, -atom_size, SEEK_END);
X    last_offset = ftello(infile);
X    moov_atom_size = atom_size;
X    moov_atom = malloc(moov_atom_size);
X    if (!moov_atom) {
X        printf ("could not allocate 0x%llX byte for moov atom\n",
X            atom_size);
X        fclose(infile);
X        return 1;
X    }
X    if (fread(moov_atom, atom_size, 1, infile) != 1) {
X        perror(argv[1]);
X        free(moov_atom);
X        fclose(infile);
X        return 1;
X    }
X
X    /* this utility does not support compressed atoms yet, so disqualify
X     * files with compressed QT atoms */
X    if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
X        printf ("this utility does not support compressed moov atoms yet\n");
X        free(moov_atom);
X        fclose(infile);
X        return 1;
X    }
X
X    /* close; will be re-opened later */
X    fclose(infile);
X
X    /* crawl through the moov chunk in search of stco or co64 atoms */
X    for (i = 4; i < moov_atom_size - 4; i++) {
X        atom_type = BE_32(&moov_atom[i]);
X        if (atom_type == STCO_ATOM) {
X            printf (" patching stco atom...\n");
X            atom_size = BE_32(&moov_atom[i - 4]);
X            if (i + atom_size - 4 > moov_atom_size) {
X                printf (" bad atom size\n");
X                free(moov_atom);
X                return 1;
X            }
X            offset_count = BE_32(&moov_atom[i + 8]);
X            for (j = 0; j < offset_count; j++) {
X                current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
X                current_offset += moov_atom_size;
X                moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
X                moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
X                moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
X                moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
X            }
X            i += atom_size - 4;
X        } else if (atom_type == CO64_ATOM) {
X            printf (" patching co64 atom...\n");
X            atom_size = BE_32(&moov_atom[i - 4]);
X            if (i + atom_size - 4 > moov_atom_size) {
X                printf (" bad atom size\n");
X                free(moov_atom);
X                return 1;
X            }
X            offset_count = BE_32(&moov_atom[i + 8]);
X            for (j = 0; j < offset_count; j++) {
X                current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
X                current_offset += moov_atom_size;
X                moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
X                moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
X            }
X            i += atom_size - 4;
X        }
X    }
X
X    /* re-open the input file and open the output file */
X    infile = fopen(argv[1], "rb");
X    if (!infile) {
X        perror(argv[1]);
X        free(moov_atom);
X        return 1;
X    }
X
X    if (start_offset > 0) { /* seek after ftyp atom */
X        fseeko(infile, start_offset, SEEK_SET);
X        last_offset -= start_offset;
X    }
X
X    outfile = fopen(argv[2], "wb");
X    if (!outfile) {
X        perror(argv[2]);
X        fclose(outfile);
X        free(moov_atom);
X        return 1;
X    }
X
X    /* dump the same ftyp atom */
X    if (ftyp_atom_size > 0) {
X        printf (" writing ftyp atom...\n");
X        if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
X            perror(argv[2]);
X            goto error_out;
X        }
X    }
X
X    /* dump the new moov atom */
X    printf (" writing moov atom...\n");
X    if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
X        perror(argv[2]);
X        goto error_out;
X    }
X
X    /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
X    printf (" copying rest of file...\n");
X    while (last_offset) {
X        if (last_offset > COPY_BUFFER_SIZE)
X            bytes_to_copy = COPY_BUFFER_SIZE;
X        else
X            bytes_to_copy = last_offset;
X
X        if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
X            perror(argv[1]);
X            goto error_out;
X        }
X        if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
X            perror(argv[2]);
X            goto error_out;
X        }
X
X        last_offset -= bytes_to_copy;
X    }
X
X    fclose(infile);
X    fclose(outfile);
X    free(moov_atom);
X    if (ftyp_atom_size > 0)
X        free(ftyp_atom);
X
X    return 0;
X
Xerror_out:
X    fclose(infile);
X    fclose(outfile);
X    free(moov_atom);
X    if (ftyp_atom_size > 0)
X        free(ftyp_atom);
X    return 1;
X}
cd8a0833e082e498c35855f6c0fb99c6
echo x - multimedia/qt-faststart/Makefile
sed 's/^X//' >multimedia/qt-faststart/Makefile << '25dd9a8183be71b8ec9808b81d75b564'
X# New ports collection makefile for:	qt-faststart
X# Date created:		2010 January 26
X# Whom:			J.R. Oldroyd <fbsd at opal.com>
X#
X# $FreeBSD$
X#
X
XPORTNAME=	qt-faststart
XPORTVERSION=	0.1
XCATEGORIES=	multimedia
XDISTFILES=
X
XMAINTAINER=	fbsd at opal.com
XCOMMENT=	Convert QuickTime movie files to streamable format
X
X.include <bsd.port.pre.mk>
X
Xdo-fetch:
X
Xdo-extract:
X		${MKDIR} ${WRKSRC}
X		${CP} ${FILESDIR}/${PORTNAME}.c ${WRKSRC}
X
Xdo-build:
X		cd ${WRKSRC}; ${CC} ${PORTNAME}.c -o ${PORTNAME}
X
Xdo-install:
X		${INSTALL_PROGRAM} ${WRKSRC}/${PORTNAME} ${PREFIX}/bin
X
X.include <bsd.port.post.mk>
25dd9a8183be71b8ec9808b81d75b564
echo x - multimedia/qt-faststart/pkg-descr
sed 's/^X//' >multimedia/qt-faststart/pkg-descr << 'a12e36aa1c07f19b2214ac6d64759242'
XThis utility rearranges a Quicktime file such that the moov atom
Xis in front of the data, thus facilitating network streaming.
X
XThis is needed to prepare .mp4 and .3gp files for playing on an
XAndroid device.
a12e36aa1c07f19b2214ac6d64759242
echo x - multimedia/qt-faststart/pkg-plist
sed 's/^X//' >multimedia/qt-faststart/pkg-plist << '6f9d82674d836a55042e8f6afde2df4d'
Xbin/qt-faststart
6f9d82674d836a55042e8f6afde2df4d
exit
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the freebsd-ports-bugs mailing list