#include "libavcodec/put_bits.h"
#include "internal.h"
#include "libavutil/avstring.h"
-#include "libavutil/intfloat_readwrite.h"
+#include "libavutil/intfloat.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/dict.h"
#include "rtpenc.h"
+#include "mov_chan.h"
#undef NDEBUG
#include <assert.h>
{ "frag_size", "maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
{ "frag_duration", "maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
+ { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
+ { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
+ { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
{ NULL },
};
/**
* This function writes extradata "as is".
- * Extradata must be formated like a valid atom (with size and tag)
+ * Extradata must be formatted like a valid atom (with size and tag).
*/
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
{
return updateSize(pb, pos);
}
+static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
+{
+ uint32_t layout_tag, bitmap;
+ int64_t pos = avio_tell(pb);
+
+ layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
+ track->enc->channel_layout,
+ &bitmap);
+ if (!layout_tag) {
+ av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
+ "lack of channel information\n");
+ return 0;
+ }
+
+ avio_wb32(pb, 0); // Size
+ ffio_wfourcc(pb, "chan"); // Type
+ avio_w8(pb, 0); // Version
+ avio_wb24(pb, 0); // Flags
+ avio_wb32(pb, layout_tag); // mChannelLayoutTag
+ avio_wb32(pb, bitmap); // mChannelBitmap
+ avio_wb32(pb, 0); // mNumberChannelDescriptions
+
+ return updateSize(pb, pos);
+}
+
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
{
int64_t pos = avio_tell(pb);
} else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
mov_write_amr_tag(pb, track);
} else if (track->enc->codec_id == CODEC_ID_AC3) {
+ mov_write_chan_tag(pb, track);
mov_write_ac3_tag(pb, track);
} else if (track->enc->codec_id == CODEC_ID_ALAC) {
mov_write_extradata_tag(pb, track);
uint32_t tag = track->tag;
if (track->mode == MODE_MOV) {
- if (track->timescale > UINT16_MAX) {
- if (mov_get_lpcm_flags(track->enc->codec_id))
- tag = AV_RL32("lpcm");
- version = 2;
- } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
- track->enc->codec_id == CODEC_ID_ADPCM_MS ||
- track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
- version = 1;
- }
+ if (mov_get_lpcm_flags(track->enc->codec_id))
+ tag = AV_RL32("lpcm");
+ version = 2;
}
avio_wb32(pb, 0); /* size */
avio_wb16(pb, 0);
avio_wb32(pb, 0x00010000);
avio_wb32(pb, 72);
- avio_wb64(pb, av_dbl2int(track->timescale));
+ avio_wb64(pb, av_double2int(track->timescale));
avio_wb32(pb, track->enc->channels);
avio_wb32(pb, 0x7F000000);
avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
avio_wb32(pb, track->sampleSize);
- avio_wb32(pb, track->enc->frame_size);
+ avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
} else {
- if (track->mode == MODE_MOV) {
- avio_wb16(pb, track->enc->channels);
- if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
- track->enc->codec_id == CODEC_ID_PCM_S8)
- avio_wb16(pb, 8); /* bits per sample */
- else
- avio_wb16(pb, 16);
- avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
- } else { /* reserved for mp4/3gp */
- avio_wb16(pb, 2);
- avio_wb16(pb, 16);
- avio_wb16(pb, 0);
- }
+ /* reserved for mp4/3gp */
+ avio_wb16(pb, 2);
+ avio_wb16(pb, 16);
+ avio_wb16(pb, 0);
avio_wb16(pb, 0); /* packet size (= 0) */
avio_wb16(pb, track->timescale); /* Time scale */
avio_wb16(pb, 0); /* Reserved */
}
- if(version == 1) { /* SoundDescription V1 extended info */
- avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
- avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
- avio_wb32(pb, track->sampleSize); /* Bytes per frame */
- avio_wb32(pb, 2); /* Bytes per sample */
- }
-
if(track->mode == MODE_MOV &&
(track->enc->codec_id == CODEC_ID_AAC ||
track->enc->codec_id == CODEC_ID_AC3 ||
avio_wb32(pb, track->enc->height << 16);
return updateSize(pb, pos);
-};
+}
// This box seems important for the psp playback ... without it the movie seems to hang
static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
avio_wb32(pb, 0); /* size */
ffio_wfourcc(pb, "trak");
mov_write_tkhd_tag(pb, track, st);
- if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts)
- if(!mov->fragments) // EDTS with fragments is tricky as we dont know the duration when its written
- mov_write_edts_tag(pb, track); // PSP Movies require edts box
+ if(!mov->fragments) // EDTS with fragments is tricky as we dont know the duration when its written
+ mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
if (track->tref_tag)
mov_write_tref_tag(pb, track);
mov_write_mdia_tag(pb, track);
int tr_flags=0;
int i;
- tr_flags |= 0xF00; //FIXME
+ for(i=track->cluster_write_index; i<track->entry; i++){
+ int64_t duration = i + 1 == track->entry ?
+ track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
+ track->cluster[i+1].dts - track->cluster[i].dts;
+ if(duration != 1) tr_flags |= 0x100;
+ if(track->trex_size != track->cluster[i].size) tr_flags |= 0x200;
+ if(track->trex_flags != ((track->cluster[i].flags&MOV_SYNC_SAMPLE) ? 0x02000000 : 0x01010000))
+ tr_flags |= 0x400;
+ if(track->cluster[i].cts) tr_flags |= 0x800;
+ }
avio_wb32(pb, 0);
ffio_wfourcc(pb, "trun");
return updateSize(pb, pos);
}
-#if 0
-/* TODO: Not sorted out, but not necessary either */
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
{
- avio_wb32(pb, 0x15); /* size */
+ int i, has_audio = 0, has_video = 0;
+ int64_t pos = avio_tell(pb);
+ int audio_profile = mov->iods_audio_profile;
+ int video_profile = mov->iods_video_profile;
+ for (i = 0; i < mov->nb_streams; i++) {
+ if(mov->tracks[i].entry > 0) {
+ has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
+ has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
+ }
+ }
+ if (audio_profile < 0)
+ audio_profile = 0xFF - has_audio;
+ if (video_profile < 0)
+ video_profile = 0xFF - has_video;
+ avio_wb32(pb, 0x0); /* size */
ffio_wfourcc(pb, "iods");
avio_wb32(pb, 0); /* version & flags */
- avio_wb16(pb, 0x1007);
- avio_w8(pb, 0);
- avio_wb16(pb, 0x4fff);
- avio_wb16(pb, 0xfffe);
- avio_wb16(pb, 0x01ff);
- return 0x15;
+ putDescr(pb, 0x10, 7);
+ avio_wb16(pb, 0x004f);
+ avio_w8(pb, 0xff);
+ avio_w8(pb, 0xff);
+ avio_w8(pb, audio_profile);
+ avio_w8(pb, video_profile);
+ avio_w8(pb, 0xff);
+ return updateSize(pb, pos);
}
-#endif
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
{
avio_wb32(pb, track->trackID);
avio_wb32(pb, 1); // stsd_id
avio_wb32(pb, 1); // duration
- avio_wb32(pb, 1/*Size*/);
- avio_wb32(pb, 1<<16);
+ track->trex_size= track->entry ? track->cluster[FFMIN(1, track->entry-1)].size : 1;
+ avio_wb32(pb, track->trex_size);
+ track->trex_flags= st->codec->codec_type != AVMEDIA_TYPE_VIDEO ? 0x02000000 : 0x01010000;
+ avio_wb32(pb, track->trex_flags);
return updateSize(pb, pos);
}
}
mov_write_mvhd_tag(pb, mov);
- //mov_write_iods_tag(pb, mov);
+ if (mov->mode != MODE_MOV && !mov->iods_skip)
+ mov_write_iods_tag(pb, mov);
for (i=0; i<mov->nb_streams; i++) {
if(mov->tracks[i].entry > 0) {
mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
AVCodecContext *enc = trk->enc;
unsigned int samplesInChunk = 0;
int size= pkt->size;
+ uint8_t *reformatted_data = NULL;
if (!s->pb->seekable) return 0; /* Can't handle that */
if (!size) return 0; /* Discard 0 sized packets */
av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
return -1;
}
- } else if (enc->codec_id == CODEC_ID_ADPCM_MS ||
- enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
- samplesInChunk = enc->frame_size;
} else if (trk->sampleSize)
samplesInChunk = size/trk->sampleSize;
else
if(mov->frag_seq_num>0){
uint8_t *buf=NULL;
size= pkt->size;
+
if(ff_avc_parse_nal_units_buf(pkt->data, &buf, &size) < 0){
av_log(s, AV_LOG_ERROR, "malformated H264 bitstream\n");
return -1;
}
trk->cluster[trk->entry].data= buf;
- }else
+ if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
+ reformatted_data= av_malloc(size);
+ memcpy(reformatted_data, buf, size);
+ }
+ }else if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
+ ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
+ &size);
+ avio_write(pb, reformatted_data, size);
+ } else {
size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+ }
} else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
(AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
avio_flush(pb);
if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
- ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
+ ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
+ reformatted_data, size);
+ av_free(reformatted_data);
return 0;
}
"or choose different container.\n");
}else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
track->timescale = 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);
- goto error;
- }else if(st->codec->codec_id == CODEC_ID_ADPCM_MS ||
- st->codec->codec_id == CODEC_ID_ADPCM_IMA_WAV){
+ /* set sampleSize for PCM and ADPCM */
+ if (av_get_bits_per_sample(st->codec->codec_id)) {
if (!st->codec->block_align) {
- av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
+ av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
goto error;
}
track->sampleSize = st->codec->block_align;
- }else if(st->codec->frame_size > 1){ /* assume compressed audio */
+ }
+ /* set audio_vbr for compressed audio */
+ if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
+ if (!st->codec->frame_size) {
+ av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
+ goto error;
+ }
track->audio_vbr = 1;
- }else{
- st->codec->frame_size = 1;
- track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
}
if (track->mode != MODE_MOV) {
if (track->timescale > UINT16_MAX) {
if(mov->reserved_moov_size){
int64_t size= mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
if(size < 8){
- av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %Ld additional\n", 8-size);
+ av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
return -1;
}
avio_wb32(pb, size);