* MOV, 3GP, MP4 muxer
* Copyright (c) 2003 Thomas Raivio
* Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
+ * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
*
* This file is part of FFmpeg.
*
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+
#include "avformat.h"
#include "riff.h"
#include "avio.h"
#include "isom.h"
#include "avc.h"
-#include "libavcodec/bitstream.h"
+#include "libavcodec/get_bits.h"
+#include "libavcodec/put_bits.h"
#undef NDEBUG
#include <assert.h>
#define MODE_IPOD 0x20
typedef struct MOVIentry {
- unsigned int flags, size;
+ unsigned int size;
uint64_t pos;
unsigned int samplesInChunk;
- char key_frame;
unsigned int entries;
- int64_t cts;
+ int cts;
int64_t dts;
+#define MOV_SYNC_SAMPLE 0x0001
+#define MOV_PARTIAL_SYNC_SAMPLE 0x0002
+ uint32_t flags;
} MOVIentry;
typedef struct MOVIndex {
long sampleCount;
long sampleSize;
int hasKeyframes;
- int hasBframes;
+#define MOV_TRACK_CTTS 0x0001
+#define MOV_TRACK_STPS 0x0002
+ uint32_t flags;
int language;
int trackID;
int tag; ///< stsd fourcc
int height; ///< active picture (w/o VBI) height for D-10/IMX
} MOVTrack;
-typedef struct MOVContext {
+typedef struct MOVMuxContext {
int mode;
int64_t time;
int nb_streams;
int64_t mdat_pos;
uint64_t mdat_size;
long timescale;
- MOVTrack tracks[MAX_STREAMS];
-} MOVContext;
+ MOVTrack *tracks;
+} MOVMuxContext;
//FIXME support 64 bit variant with wide placeholders
static int64_t updateSize(ByteIOContext *pb, int64_t pos)
}
/* Sync sample atom */
-static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track)
+static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track, uint32_t flag)
{
int64_t curpos, entryPos;
int i, index = 0;
int64_t pos = url_ftell(pb);
put_be32(pb, 0); // size
- put_tag(pb, "stss");
+ put_tag(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
put_be32(pb, 0); // version & flags
entryPos = url_ftell(pb);
put_be32(pb, track->entry); // entry count
for (i=0; i<track->entry; i++) {
- if(track->cluster[i].key_frame == 1) {
+ if (track->cluster[i].flags & flag) {
put_be32(pb, i+1);
index++;
}
track->enc->sample_rate > 24000)
put_byte(pb, 0x6B); // 11172-3
else
- put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
+ put_byte(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
// the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
// plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
return 0;
}
-static const AVCodecTag codec_3gp_tags[] = {
- { CODEC_ID_H263, MKTAG('s','2','6','3') },
+static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag = track->enc->codec_tag;
+
+ if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
+ return 0;
+
+ if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
+ else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
+ else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
+ else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
+ else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
+ else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
+
+ return tag;
+}
+
+static const AVCodecTag codec_ipod_tags[] = {
{ CODEC_ID_H264, MKTAG('a','v','c','1') },
{ CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
{ CODEC_ID_AAC, MKTAG('m','p','4','a') },
- { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
- { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
+ { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
+ { CODEC_ID_AC3, MKTAG('a','c','-','3') },
{ CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
+ { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
{ CODEC_ID_NONE, 0 },
};
-static const AVCodecTag mov_pix_fmt_tags[] = {
- { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
- { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
- { PIX_FMT_BGR555, MKTAG('r','a','w',' ') },
- { PIX_FMT_RGB24, MKTAG('r','a','w',' ') },
- { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
+static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag = track->enc->codec_tag;
+
+ // keep original tag for subs, ipod supports both formats
+ if (!(track->enc->codec_type == CODEC_TYPE_SUBTITLE &&
+ (tag == MKTAG('t','x','3','g') ||
+ tag == MKTAG('t','e','x','t'))))
+ tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
+
+ if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
+ av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
+ "Quicktime/Ipod might not play the file\n");
+
+ return tag;
+}
+
+static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag;
+
+ if (track->enc->height == 480) /* NTSC */
+ if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
+ else tag = MKTAG('d','v','c',' ');
+ else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
+ else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
+ else tag = MKTAG('d','v','p','p');
+
+ return tag;
+}
+
+static const struct {
+ enum PixelFormat pix_fmt;
+ uint32_t tag;
+ unsigned bps;
+} mov_pix_fmt_tags[] = {
+ { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
+ { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
+ { PIX_FMT_BGR555, MKTAG('r','a','w',' '), 16 },
+ { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
+ { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
+ { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
+ { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
+ { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
+ { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
+ { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
+ { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
};
-static const AVCodecTag codec_ipod_tags[] = {
+static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag = track->enc->codec_tag;
+ int i;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
+ if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
+ tag = mov_pix_fmt_tags[i].tag;
+ track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
+ break;
+ }
+ }
+
+ return tag;
+}
+
+static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag = track->enc->codec_tag;
+
+ if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
+ (tag == MKTAG('d','v','c','p') ||
+ track->enc->codec_id == CODEC_ID_RAWVIDEO ||
+ av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
+ if (track->enc->codec_id == CODEC_ID_DVVIDEO)
+ tag = mov_get_dv_codec_tag(s, track);
+ else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
+ tag = mov_get_rawvideo_codec_tag(s, track);
+ else if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
+ tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
+ if (!tag) { // if no mac fcc found, try with Microsoft tags
+ tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
+ if (tag)
+ av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
+ "the file may be unplayable!\n");
+ }
+ } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
+ tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
+ if (!tag) { // if no mac fcc found, try with Microsoft tags
+ int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
+ if (ms_tag) {
+ tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
+ av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
+ "the file may be unplayable!\n");
+ }
+ }
+ } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
+ tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
+ }
+
+ return tag;
+}
+
+static const AVCodecTag codec_3gp_tags[] = {
+ { CODEC_ID_H263, MKTAG('s','2','6','3') },
{ CODEC_ID_H264, MKTAG('a','v','c','1') },
{ CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
{ CODEC_ID_AAC, MKTAG('m','p','4','a') },
- { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
- { CODEC_ID_AC3, MKTAG('a','c','-','3') },
+ { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
+ { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
{ CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
{ CODEC_ID_NONE, 0 },
};
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
{
int tag = track->enc->codec_tag;
- if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
- if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
- return 0;
- if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
- else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
- else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
- else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
- else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
- else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
- } else if (track->mode == MODE_IPOD) {
- tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
- if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
- av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
- "Quicktime/Ipod might not play the file\n");
- } else if (track->mode & MODE_3GP) {
- tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
- } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
- (tag == MKTAG('d','v','c','p') ||
- track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
- if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
- if (track->enc->height == 480) /* NTSC */
- if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
- else tag = MKTAG('d','v','c',' ');
- else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
- else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
- else tag = MKTAG('d','v','p','p');
- } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
- tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
- if (!tag) // restore tag
- tag = track->enc->codec_tag;
- } else {
- if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
- tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
- if (!tag) { // if no mac fcc found, try with Microsoft tags
- tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
- if (tag)
- av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
- "the file may be unplayable!\n");
- }
- } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
- tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
- if (!tag) { // if no mac fcc found, try with Microsoft tags
- int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
- if (ms_tag) {
- tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
- av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
- "the file may be unplayable!\n");
- }
- }
- } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
- tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
- }
- }
- }
+
+ if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
+ tag = mp4_get_codec_tag(s, track);
+ else if (track->mode == MODE_IPOD)
+ tag = ipod_get_codec_tag(s, track);
+ else if (track->mode & MODE_3GP)
+ tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
+ else
+ tag = mov_get_codec_tag(s, track);
+
return tag;
}
mov_write_stts_tag(pb, track);
if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
track->hasKeyframes && track->hasKeyframes < track->entry)
- mov_write_stss_tag(pb, track);
+ mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
+ if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
+ mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
- track->hasBframes)
+ track->flags & MOV_TRACK_CTTS)
mov_write_ctts_tag(pb, track);
mov_write_stsc_tag(pb, track);
mov_write_stsz_tag(pb, track);
hdlr_type = "soun";
descr = "SoundHandler";
} else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
- if (track->mode == MODE_IPOD) hdlr_type = "sbtl";
- else hdlr_type = "text";
+ if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
+ else hdlr_type = "text";
descr = "SubtitleHandler";
}
}
put_be32(pb ,0); /* reserved */
put_be32(pb ,0); /* reserved */
put_be32(pb ,0); /* reserved */
- put_byte(pb, strlen(descr)); /* string counter */
+ if (!track || track->mode == MODE_MOV)
+ put_byte(pb, strlen(descr)); /* pascal string */
put_buffer(pb, descr, strlen(descr)); /* handler description */
+ if (track && track->mode != MODE_MOV)
+ put_byte(pb, 0); /* c string */
return updateSize(pb, pos);
}
else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
mov_write_smhd_tag(pb);
else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
- if (track->mode == MODE_MOV) mov_write_gmhd_tag(pb);
- else mov_write_nmhd_tag(pb);
+ if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
+ else mov_write_nmhd_tag(pb);
}
if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
mov_write_hdlr_tag(pb, NULL);
put_be32(pb, 0); /* size */
put_tag(pb, "trak");
mov_write_tkhd_tag(pb, track, st);
- if (track->mode == MODE_PSP || track->hasBframes)
+ if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS)
mov_write_edts_tag(pb, track); // PSP Movies require edts box
mov_write_mdia_tag(pb, track);
if (track->mode == MODE_PSP)
#if 0
/* TODO: Not sorted out, but not necessary either */
-static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
+static int mov_write_iods_tag(ByteIOContext *pb, MOVMuxContext *mov)
{
put_be32(pb, 0x15); /* size */
put_tag(pb, "iods");
}
#endif
-static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
+static int mov_write_mvhd_tag(ByteIOContext *pb, MOVMuxContext *mov)
{
int maxTrackID = 1, i;
int64_t maxTrackLenTemp, maxTrackLen = 0;
return 0x6c;
}
-static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
int64_t pos = url_ftell(pb);
}
/* helper function to write a data tag with the specified string as data */
-static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
+static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int lang, int long_style)
{
if(long_style){
int64_t pos = url_ftell(pb);
return updateSize(pb, pos);
}else{
put_be16(pb, strlen(data)); /* string length */
- put_be16(pb, 0);
+ put_be16(pb, lang);
put_buffer(pb, data, strlen(data));
return strlen(data) + 4;
}
}
-static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
+static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int lang, int long_style){
int size = 0;
if (value && value[0]) {
int64_t pos = url_ftell(pb);
put_be32(pb, 0); /* size */
put_tag(pb, name);
- mov_write_string_data_tag(pb, value, long_style);
+ mov_write_string_data_tag(pb, value, lang, long_style);
size= updateSize(pb, pos);
}
return size;
}
-/* iTunes year */
-static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
+static int mov_write_string_metadata(AVFormatContext *s, ByteIOContext *pb,
+ const char *name, const char *tag,
+ int long_style)
{
- if(year){
- char year_str[5];
- snprintf(year_str, sizeof(year_str), "%04d", year);
- return mov_write_string_tag(pb, "\251day", year_str, long_style);
- }else
+ int l, lang = 0, len, len2;
+ AVMetadataTag *t, *t2 = NULL;
+ char tag2[16];
+
+ if (!(t = av_metadata_get(s->metadata, tag, NULL, 0)))
return 0;
+
+ len = strlen(t->key);
+ snprintf(tag2, sizeof(tag2), "%s-", tag);
+ while ((t2 = av_metadata_get(s->metadata, tag2, t2, AV_METADATA_IGNORE_SUFFIX))) {
+ len2 = strlen(t2->key);
+ if (len2 == len+4 && !strcmp(t->value, t2->value)
+ && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 0)) >= 0) {
+ lang = l;
+ break;
+ }
+ }
+ return mov_write_string_tag(pb, name, t->value, lang, long_style);
}
/* iTunes track number */
-static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_trkn_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
- int size = 0;
- if (s->track) {
+ AVMetadataTag *t = av_metadata_get(s->metadata, "track", NULL, 0);
+ int size = 0, track = t ? atoi(t->value) : 0;
+ if (track) {
int64_t pos = url_ftell(pb);
put_be32(pb, 0); /* size */
put_tag(pb, "trkn");
put_be32(pb, 0); // 8 bytes empty
put_be32(pb, 0);
put_be16(pb, 0); // empty
- put_be16(pb, s->track); // track number
+ put_be16(pb, track); // track number
put_be16(pb, 0); // total track number
put_be16(pb, 0); // empty
updateSize(pb, pos);
}
/* iTunes meta data list */
-static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_ilst_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
int64_t pos = url_ftell(pb);
put_be32(pb, 0); /* size */
put_tag(pb, "ilst");
- mov_write_string_tag(pb, "\251nam", s->title , 1);
- mov_write_string_tag(pb, "\251ART", s->author , 1);
- mov_write_string_tag(pb, "\251wrt", s->author , 1);
- mov_write_string_tag(pb, "\251alb", s->album , 1);
- mov_write_day_tag(pb, s->year ,1);
- mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
- mov_write_string_tag(pb, "\251cmt", s->comment , 1);
- mov_write_string_tag(pb, "\251gen", s->genre , 1);
- mov_write_string_tag(pb, "\251cpy", s->copyright , 1);
+ mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
+ mov_write_string_metadata(s, pb, "\251ART", "author" , 1);
+ mov_write_string_metadata(s, pb, "\251wrt", "author" , 1);
+ mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
+ mov_write_string_metadata(s, pb, "\251day", "year" , 1);
+ mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
+ mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
+ mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
+ mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
mov_write_trkn_tag(pb, mov, s);
return updateSize(pb, pos);
}
/* iTunes meta data tag */
-static int mov_write_meta_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_meta_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
int size = 0;
-
- // only save meta tag if required
- if (s->title[0] || s->author[0] || s->album[0] || s->year ||
- s->comment[0] || s->genre[0] || s->track) {
- int64_t pos = url_ftell(pb);
- put_be32(pb, 0); /* size */
- put_tag(pb, "meta");
- put_be32(pb, 0);
- mov_write_itunes_hdlr_tag(pb, mov, s);
- mov_write_ilst_tag(pb, mov, s);
- size = updateSize(pb, pos);
- }
+ int64_t pos = url_ftell(pb);
+ put_be32(pb, 0); /* size */
+ put_tag(pb, "meta");
+ put_be32(pb, 0);
+ mov_write_itunes_hdlr_tag(pb, mov, s);
+ mov_write_ilst_tag(pb, mov, s);
+ size = updateSize(pb, pos);
return size;
}
const char *tag, const char *str)
{
int64_t pos = url_ftell(pb);
- if (!utf8len(str))
+ AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
+ if (!t || !utf8len(t->value))
return 0;
put_be32(pb, 0); /* size */
put_tag (pb, tag); /* type */
put_be32(pb, 0); /* version + flags */
if (!strcmp(tag, "yrrc"))
- put_be16(pb, s->year);
+ put_be16(pb, atoi(t->value));
else {
put_be16(pb, language_code("eng")); /* language */
- ascii_to_wc(pb, str);
- if (!strcmp(tag, "albm") && s->year)
- put_byte(pb, s->year);
+ put_buffer(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
+ if (!strcmp(tag, "albm") &&
+ (t = av_metadata_get(s->metadata, "year", NULL, 0)))
+ put_byte(pb, atoi(t->value));
}
return updateSize(pb, pos);
}
-static int mov_write_udta_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
- int i;
- int bitexact = 0;
+ ByteIOContext *pb_buf;
+ int i, ret, size;
+ uint8_t *buf;
for (i = 0; i < s->nb_streams; i++)
if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
- bitexact = 1;
- break;
+ return 0;
}
- if (!bitexact && (s->title[0] || s->author[0] || s->album[0] || s->year ||
- s->comment[0] || s->genre[0] || s->track)) {
- int64_t pos = url_ftell(pb);
-
- put_be32(pb, 0); /* size */
- put_tag(pb, "udta");
+ ret = url_open_dyn_buf(&pb_buf);
+ if(ret < 0)
+ return ret;
if (mov->mode & MODE_3GP) {
- mov_write_3gp_udta_tag(pb, s, "titl", s->title);
- mov_write_3gp_udta_tag(pb, s, "auth", s->author);
- mov_write_3gp_udta_tag(pb, s, "gnre", s->genre);
- mov_write_3gp_udta_tag(pb, s, "dscp", s->comment);
- mov_write_3gp_udta_tag(pb, s, "albm", s->album);
- mov_write_3gp_udta_tag(pb, s, "cprt", s->copyright);
- mov_write_3gp_udta_tag(pb, s, "yrrc", "nil");
+ mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
+ mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
+ mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
+ mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
+ mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
+ mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
+ mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "year");
} else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
- mov_write_string_tag(pb, "\251nam", s->title , 0);
- mov_write_string_tag(pb, "\251aut", s->author , 0);
- mov_write_string_tag(pb, "\251alb", s->album , 0);
- mov_write_day_tag(pb, s->year, 0);
- mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
- mov_write_string_tag(pb, "\251des", s->comment , 0);
- mov_write_string_tag(pb, "\251gen", s->genre , 0);
- mov_write_string_tag(pb, "\251cpy", s->copyright , 0);
+ mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
+ mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
+ mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
+ mov_write_string_metadata(s, pb_buf, "\251day", "year" , 0);
+ mov_write_string_tag(pb_buf, "\251enc", LIBAVFORMAT_IDENT, 0, 0);
+ mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
+ mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
+ mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
} else {
/* iTunes meta data */
- mov_write_meta_tag(pb, mov, s);
+ mov_write_meta_tag(pb_buf, mov, s);
}
- return updateSize(pb, pos);
+
+ if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
+ put_be32(pb, size+8);
+ put_tag(pb, "udta");
+ put_buffer(pb, buf, size);
+ av_free(buf);
}
return 0;
static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
{
+ AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
int64_t pos, pos2;
- if (s->title[0]) {
+ if (title) {
pos = url_ftell(pb);
put_be32(pb, 0); /* size placeholder*/
put_tag(pb, "uuid");
put_be16(pb, 0x021C); /* data */
mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
- mov_write_psp_udta_tag(pb, s->title, "eng", 0x01);
+ mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
// snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
return 0;
}
-static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
+static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov,
AVFormatContext *s)
{
int i;
return updateSize(pb, pos);
}
-static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext *mov)
+static int mov_write_mdat_tag(ByteIOContext *pb, MOVMuxContext *mov)
{
put_be32(pb, 8); // placeholder for extended size field (64 bit)
put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
/* TODO: This needs to be more general */
static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
{
- MOVContext *mov = s->priv_data;
+ MOVMuxContext *mov = s->priv_data;
int64_t pos = url_ftell(pb);
int has_h264 = 0, has_video = 0;
int minor = 0x200;
static int mov_write_header(AVFormatContext *s)
{
ByteIOContext *pb = s->pb;
- MOVContext *mov = s->priv_data;
+ MOVMuxContext *mov = s->priv_data;
int i;
if (url_is_streamed(s->pb)) {
}
}
+ mov->tracks = av_mallocz(s->nb_streams*sizeof(*mov->tracks));
+ if (!mov->tracks)
+ return AVERROR(ENOMEM);
+
for(i=0; i<s->nb_streams; i++){
AVStream *st= s->streams[i];
MOVTrack *track= &mov->tracks[i];
+ AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
track->enc = st->codec;
- track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
+ track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
+ if (track->language < 0)
+ track->language = 0;
track->mode = mov->mode;
track->tag = mov_find_codec_tag(s, track);
if (!track->tag) {
av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
"codec not currently supported in container\n", i);
- return -1;
+ goto error;
}
if(st->codec->codec_type == CODEC_TYPE_VIDEO){
if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
- return -1;
+ goto error;
}
track->height = track->tag>>24 == 'n' ? 486 : 576;
- } else
- track->height = st->codec->height;
+ }
track->timescale = st->codec->time_base.den;
- av_set_pts_info(st, 64, 1, st->codec->time_base.den);
if (track->mode == MODE_MOV && track->timescale > 100000)
av_log(s, AV_LOG_WARNING,
"WARNING codec timebase is very high. If duration is too long,\n"
"or choose different container.\n");
}else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
track->timescale = st->codec->sample_rate;
- av_set_pts_info(st, 64, 1, st->codec->sample_rate);
if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
- return -1;
+ goto error;
}else if(st->codec->frame_size > 1){ /* assume compressed audio */
track->audio_vbr = 1;
}else{
track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
i, track->enc->sample_rate);
- return -1;
+ goto error;
}
}else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
track->timescale = st->codec->time_base.den;
- av_set_pts_info(st, 64, 1, st->codec->time_base.den);
}
+ if (!track->height)
+ track->height = st->codec->height;
+
+ av_set_pts_info(st, 64, 1, track->timescale);
}
mov_write_mdat_tag(pb, mov);
put_flush_packet(pb);
+ return 0;
+ error:
+ av_freep(&mov->tracks);
+ return -1;
+}
+
+static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
+{
+ uint32_t c = -1;
+ int i, closed_gop = 0;
+
+ for (i = 0; i < pkt->size - 4; i++) {
+ c = (c<<8) + pkt->data[i];
+ if (c == 0x1b8) { // gop
+ closed_gop = pkt->data[i+4]>>6 & 0x01;
+ } else if (c == 0x100) { // pic
+ int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
+ if (!temp_ref || closed_gop) // I picture is not reordered
+ *flags = MOV_SYNC_SAMPLE;
+ else
+ *flags = MOV_PARTIAL_SYNC_SAMPLE;
+ break;
+ }
+ }
return 0;
}
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
{
- MOVContext *mov = s->priv_data;
+ MOVMuxContext *mov = s->priv_data;
ByteIOContext *pb = s->pb;
MOVTrack *trk = &mov->tracks[pkt->stream_index];
AVCodecContext *enc = trk->enc;
}
if ((enc->codec_id == CODEC_ID_DNXHD ||
- enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
+ enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
/* copy frame to create needed atoms */
trk->vosLen = size;
trk->vosData = av_malloc(size);
pkt->pts = pkt->dts;
}
if (pkt->dts != pkt->pts)
- trk->hasBframes = 1;
+ trk->flags |= MOV_TRACK_CTTS;
trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
- trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
- if(trk->cluster[trk->entry].key_frame)
- trk->hasKeyframes++;
+ trk->cluster[trk->entry].flags = 0;
+ if (pkt->flags & PKT_FLAG_KEY) {
+ if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO) {
+ mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
+ if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
+ trk->flags |= MOV_TRACK_STPS;
+ } else {
+ trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
+ }
+ if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
+ trk->hasKeyframes++;
+ }
trk->entry++;
trk->sampleCount += samplesInChunk;
mov->mdat_size += size;
static int mov_write_trailer(AVFormatContext *s)
{
- MOVContext *mov = s->priv_data;
+ MOVMuxContext *mov = s->priv_data;
ByteIOContext *pb = s->pb;
int res = 0;
int i;
put_flush_packet(pb);
+ av_freep(&mov->tracks);
+
return res;
}
NULL_IF_CONFIG_SMALL("MOV format"),
NULL,
"mov",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AAC,
CODEC_ID_MPEG4,
mov_write_header,
mov_write_packet,
mov_write_trailer,
- .flags = AVFMT_GLOBALHEADER,
+ .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
.codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
};
#endif
NULL_IF_CONFIG_SMALL("3GP format"),
NULL,
"3gp",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AMR_NB,
CODEC_ID_H263,
mov_write_header,
NULL_IF_CONFIG_SMALL("MP4 format"),
"application/mp4",
"mp4",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AAC,
CODEC_ID_MPEG4,
mov_write_header,
mov_write_packet,
mov_write_trailer,
- .flags = AVFMT_GLOBALHEADER,
+ .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
.codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
};
#endif
NULL_IF_CONFIG_SMALL("PSP MP4 format"),
NULL,
"mp4,psp",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AAC,
CODEC_ID_MPEG4,
mov_write_header,
NULL_IF_CONFIG_SMALL("3GP2 format"),
NULL,
"3g2",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AMR_NB,
CODEC_ID_H263,
mov_write_header,
NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
"application/mp4",
"m4v,m4a",
- sizeof(MOVContext),
+ sizeof(MOVMuxContext),
CODEC_ID_AAC,
CODEC_ID_H264,
mov_write_header,
mov_write_packet,
mov_write_trailer,
.flags = AVFMT_GLOBALHEADER,
- .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
+ .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
};
#endif