summaryrefslogtreecommitdiffstats
path: root/libhb
diff options
context:
space:
mode:
Diffstat (limited to 'libhb')
-rw-r--r--libhb/common.c99
-rw-r--r--libhb/common.h8
-rw-r--r--libhb/deccc608sub.c2
-rw-r--r--libhb/decmetadata.c7
-rw-r--r--libhb/decpgssub.c3
-rw-r--r--libhb/decssasub.c22
-rw-r--r--libhb/dectx3gsub.c1
-rw-r--r--libhb/decutf8sub.c2
-rw-r--r--libhb/decvobsub.c2
-rw-r--r--libhb/encavcodec.c22
-rw-r--r--libhb/encavcodecaudio.c22
-rw-r--r--libhb/encfaac.c11
-rw-r--r--libhb/enclame.c7
-rw-r--r--libhb/enctheora.c30
-rw-r--r--libhb/encvorbis.c15
-rw-r--r--libhb/encx264.c6
-rw-r--r--libhb/fifo.c6
-rw-r--r--libhb/internal.h23
-rw-r--r--libhb/module.defs19
-rw-r--r--libhb/muxavformat.c1239
-rw-r--r--libhb/muxcommon.c256
-rw-r--r--libhb/muxmkv.c58
-rw-r--r--libhb/muxmp4.c348
-rw-r--r--libhb/sync.c52
-rw-r--r--libhb/work.c1
25 files changed, 1798 insertions, 463 deletions
diff --git a/libhb/common.c b/libhb/common.c
index e6c996f5e..18302e3b8 100644
--- a/libhb/common.c
+++ b/libhb/common.c
@@ -300,25 +300,30 @@ hb_container_t *hb_containers_last_item = NULL;
hb_container_internal_t hb_containers[] =
{
// legacy muxers, back to HB 0.9.4 whenever possible (disabled)
- { { "M4V file", "m4v", "m4v", 0, }, NULL, 0, HB_GID_MUX_MP4, },
- { { "MP4 file", "mp4", "mp4", 0, }, NULL, 0, HB_GID_MUX_MP4, },
- { { "MKV file", "mkv", "mkv", 0, }, NULL, 0, HB_GID_MUX_MKV, },
+ { { "M4V file", "m4v", "m4v", 0, }, NULL, 0, HB_GID_MUX_MP4, },
+ { { "MP4 file", "mp4", "mp4", 0, }, NULL, 0, HB_GID_MUX_MP4, },
+ { { "MKV file", "mkv", "mkv", 0, }, NULL, 0, HB_GID_MUX_MKV, },
// actual muxers
- { { "MPEG-4 (mp4v2)", "mp4v2", "mp4", HB_MUX_MP4V2, }, NULL, 1, HB_GID_MUX_MP4, },
- { { "Matroska (libmkv)", "libmkv", "mkv", HB_MUX_LIBMKV, }, NULL, 1, HB_GID_MUX_MKV, },
+ { { "MPEG-4 (mp4v2)", "mp4v2", "mp4", HB_MUX_MP4V2, }, NULL, 1, HB_GID_MUX_MP4, },
+ { { "Matroska (libmkv)", "libmkv", "mkv", HB_MUX_LIBMKV, }, NULL, 1, HB_GID_MUX_MKV, },
+ { { "MPEG-4 (avformat)", "av_mp4", "mp4", HB_MUX_AV_MP4, }, NULL, 1, HB_GID_MUX_MP4, },
+ { { "Matroska (avformat)", "av_mkv", "mkv", HB_MUX_AV_MKV, }, NULL, 1, HB_GID_MUX_MKV, },
};
int hb_containers_count = sizeof(hb_containers) / sizeof(hb_containers[0]);
static int hb_container_is_enabled(int format)
{
switch (format)
{
-#if 1 //#ifdef USE_MP4V2
+#ifdef USE_MP4V2
case HB_MUX_MP4V2:
- return 1;
#endif
-
- // the following muxers are always enabled
+#ifdef USE_LIBMKV
case HB_MUX_LIBMKV:
+#endif
+#ifdef USE_AVFORMAT
+ case HB_MUX_AV_MP4:
+ case HB_MUX_AV_MKV:
+#endif
return 1;
default:
@@ -3499,46 +3504,50 @@ int hb_subtitle_can_burn( int source )
int hb_subtitle_can_pass( int source, int mux )
{
- if ( mux == HB_MUX_MKV )
+ switch (mux)
{
- switch( source )
- {
- case PGSSUB:
- case VOBSUB:
- case SSASUB:
- case SRTSUB:
- case UTF8SUB:
- case TX3GSUB:
- case CC608SUB:
- case CC708SUB:
- return 1;
+ case HB_MUX_AV_MKV:
+ case HB_MUX_LIBMKV:
+ switch( source )
+ {
+ case PGSSUB:
+ case VOBSUB:
+ case SSASUB:
+ case SRTSUB:
+ case UTF8SUB:
+ case TX3GSUB:
+ case CC608SUB:
+ case CC708SUB:
+ return 1;
- default:
- return 0;
- }
- }
- else if ( mux == HB_MUX_MP4 )
- {
- switch( source )
- {
- case VOBSUB:
- case SSASUB:
- case SRTSUB:
- case UTF8SUB:
- case TX3GSUB:
- case CC608SUB:
- case CC708SUB:
+ default:
+ return 0;
+ } break;
+
+ case HB_MUX_MP4V2:
+ if (source == VOBSUB)
+ {
return 1;
+ } // fall through to next case...
+ case HB_MUX_AV_MP4:
+ switch( source )
+ {
+ case SSASUB:
+ case SRTSUB:
+ case UTF8SUB:
+ case TX3GSUB:
+ case CC608SUB:
+ case CC708SUB:
+ return 1;
- default:
- return 0;
- }
- }
- else
- {
- // Internal error. Should never get here.
- hb_error("internel error. Bad mux %d\n", mux);
- return 0;
+ default:
+ return 0;
+ } break;
+
+ default:
+ // Internal error. Should never get here.
+ hb_error("internel error. Bad mux %d\n", mux);
+ return 0;
}
}
diff --git a/libhb/common.h b/libhb/common.h
index 472d8d018..2ce180034 100644
--- a/libhb/common.h
+++ b/libhb/common.h
@@ -463,9 +463,12 @@ struct hb_job_s
*/
#define HB_MUX_MASK 0xFF0000
#define HB_MUX_MP4V2 0x010000
-#define HB_MUX_MASK_MP4 0x0F0000
+#define HB_MUX_AV_MP4 0x020000
+#define HB_MUX_MASK_MP4 0x030000
#define HB_MUX_LIBMKV 0x100000
-#define HB_MUX_MASK_MKV 0xF00000
+#define HB_MUX_AV_MKV 0x200000
+#define HB_MUX_MASK_MKV 0x300000
+#define HB_MUX_MASK_AV 0x220000
/* default muxer for each container */
#define HB_MUX_MP4 HB_MUX_MP4V2
#define HB_MUX_MKV HB_MUX_LIBMKV
@@ -590,6 +593,7 @@ struct hb_audio_config_s
int normalize_mix_level; /* mix level normalization (boolean) */
int dither_method; /* dither algorithm */
char * name; /* Output track name */
+ int delay;
} out;
/* Input */
diff --git a/libhb/deccc608sub.c b/libhb/deccc608sub.c
index 1d16410e4..c18fa76b8 100644
--- a/libhb/deccc608sub.c
+++ b/libhb/deccc608sub.c
@@ -1497,6 +1497,7 @@ void write_cc_line_as_transcript (struct eia608_screen *data, struct s_write *wb
* Put this subtitle in a hb_buffer_t and shove it into the subtitle fifo
*/
buffer = hb_buffer_init( length + 1 );
+ buffer->s.frametype = HB_FRAME_SUBTITLE;
buffer->s.start = wb->data608->current_visible_start_ms;
buffer->s.stop = get_fts(wb);
memcpy( buffer->data, wb->subline, length + 1 );
@@ -1650,6 +1651,7 @@ int write_cc_buffer_as_srt (struct eia608_screen *data, struct s_write *wb)
if (wb->enc_buffer_used)
{
hb_buffer_t *buffer = hb_buffer_init( wb->enc_buffer_used + 1 );
+ buffer->s.frametype = HB_FRAME_SUBTITLE;
buffer->s.start = ms_start;
buffer->s.stop = ms_end;
memcpy( buffer->data, wb->enc_buffer, wb->enc_buffer_used + 1 );
diff --git a/libhb/decmetadata.c b/libhb/decmetadata.c
index 5f6b6af7b..c7c0d9858 100644
--- a/libhb/decmetadata.c
+++ b/libhb/decmetadata.c
@@ -7,10 +7,12 @@
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
-#include <mp4v2/mp4v2.h>
#include "common.h"
+#if defined(USE_MP4V2)
+#include <mp4v2/mp4v2.h>
+
static int decmp4metadata( hb_title_t *title )
{
MP4FileHandle input_file;
@@ -172,6 +174,7 @@ static int decmp4metadata( hb_title_t *title )
}
return result;
}
+#endif // USE_MP4V2
/*
* decmetadata()
@@ -190,6 +193,7 @@ int decmetadata( hb_title_t *title )
return 0;
}
+#if defined(USE_MP4V2)
/*
* Hacky way of figuring out if this is an MP4, in which case read the data using libmp4v2
*/
@@ -197,5 +201,6 @@ int decmetadata( hb_title_t *title )
{
return decmp4metadata( title );
}
+#endif
return 0;
}
diff --git a/libhb/decpgssub.c b/libhb/decpgssub.c
index 8229c6eda..3c6c8fe90 100644
--- a/libhb/decpgssub.c
+++ b/libhb/decpgssub.c
@@ -332,6 +332,7 @@ static int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_close( &pv->list_pass_buffer );
out->s = in->s;
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->sequence = in->sequence;
}
out->s.start = out->s.stop = pts;
@@ -348,6 +349,7 @@ static int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
out = hb_frame_buffer_init(AV_PIX_FMT_YUVA420P,
rect->w, rect->h);
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->s.id = in->s.id;
out->sequence = in->sequence;
out->s.start = pts;
@@ -407,6 +409,7 @@ static int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
{
out = hb_buffer_init( 1 );
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->s.id = in->s.id;
out->s.start = pts;
out->s.stop = pts;
diff --git a/libhb/decssasub.c b/libhb/decssasub.c
index d648f1d9d..277e3672b 100644
--- a/libhb/decssasub.c
+++ b/libhb/decssasub.c
@@ -33,6 +33,7 @@ struct hb_work_private_s
{
// If decoding to PICTURESUB format:
int readOrder;
+ int raw;
hb_job_t *job;
};
@@ -336,6 +337,7 @@ static hb_buffer_t *ssa_decode_line_to_utf8( uint8_t *in_data, int in_size, int
out->size = dst - out->data;
// Copy metadata from the input packet to the output packet
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->s.start = in_start;
out->s.stop = in_stop;
out->sequence = in_sequence;
@@ -401,6 +403,17 @@ static hb_buffer_t *ssa_decode_line_to_mkv_ssa( hb_work_object_t * w, uint8_t *i
if ( parse_timing_from_ssa_packet( (char *) in_data, &in_start, &in_stop ) )
goto fail;
+ if (pv->raw)
+ {
+ out = hb_buffer_init(in_size + 3);
+ snprintf((char*)out->data, in_size + 3, "%s\r\n", in_data);
+ out->s.frametype = HB_FRAME_SUBTITLE;
+ out->s.start = in_start;
+ out->s.stop = in_stop;
+ out->sequence = in_sequence;
+ return out;
+ }
+
// Convert the SSA packet to MKV-SSA format, which is what libass expects
char *mkvIn;
int numPartsRead;
@@ -439,6 +452,7 @@ static hb_buffer_t *ssa_decode_line_to_mkv_ssa( hb_work_object_t * w, uint8_t *i
strcat( mkvIn, (char *) styleToTextFields );
out->size = strlen(mkvIn);
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->s.start = in_start;
out->s.stop = in_stop;
out->sequence = in_sequence;
@@ -464,6 +478,11 @@ static int decssaInit( hb_work_object_t * w, hb_job_t * job )
pv = calloc( 1, sizeof( hb_work_private_t ) );
w->private_data = pv;
pv->job = job;
+
+ if (job->mux & HB_MUX_MASK_AV)
+ {
+ pv->raw = 1;
+ }
return 0;
}
@@ -485,7 +504,8 @@ static int decssaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
return HB_WORK_DONE;
}
- if ( w->subtitle->config.dest == PASSTHRUSUB && pv->job->mux == HB_MUX_MKV )
+ if (w->subtitle->config.dest == PASSTHRUSUB &&
+ (pv->job->mux & HB_MUX_MASK_MKV))
{
*buf_out = ssa_to_mkv_ssa(w, in);
}
diff --git a/libhb/dectx3gsub.c b/libhb/dectx3gsub.c
index a2231a4fb..f492040c3 100644
--- a/libhb/dectx3gsub.c
+++ b/libhb/dectx3gsub.c
@@ -168,6 +168,7 @@ static hb_buffer_t *tx3g_decode_to_utf8( hb_buffer_t *in )
out->size = dst - out->data;
// Copy metadata from the input packet to the output packet
+ out->s.frametype = HB_FRAME_SUBTITLE;
out->s.start = in->s.start;
out->s.stop = in->s.stop;
diff --git a/libhb/decutf8sub.c b/libhb/decutf8sub.c
index ea6440954..0cd0513d9 100644
--- a/libhb/decutf8sub.c
+++ b/libhb/decutf8sub.c
@@ -31,6 +31,8 @@ static int decutf8Work(hb_work_object_t * w,
// Pass the packets through without modification
hb_buffer_t *out = *buf_in;
+ out->s.frametype = HB_FRAME_SUBTITLE;
+
// Warn if the subtitle's duration has not been passed through by the
// demuxer, which will prevent the subtitle from displaying at all
if (out->s.stop == 0)
diff --git a/libhb/decvobsub.c b/libhb/decvobsub.c
index 12bf94417..3b6c3cb79 100644
--- a/libhb/decvobsub.c
+++ b/libhb/decvobsub.c
@@ -123,6 +123,7 @@ int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
pv->buf = hb_buffer_init( 0xFFFF );
memcpy( pv->buf->data, in->data, in->size );
pv->buf->s.id = in->s.id;
+ pv->buf->s.frametype = HB_FRAME_SUBTITLE;
pv->buf->sequence = in->sequence;
pv->size_got = in->size;
if( in->s.start >= 0 )
@@ -537,6 +538,7 @@ static hb_buffer_t * CropSubtitle( hb_work_object_t * w, uint8_t * raw )
realheight = crop[1] - crop[0] + 1;
buf = hb_frame_buffer_init( AV_PIX_FMT_YUVA420P, realwidth, realheight );
+ buf->s.frametype = HB_FRAME_SUBTITLE;
buf->s.start = pv->pts_start;
buf->s.stop = pv->pts_stop;
buf->s.type = SUBTITLE_BUF;
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c
index dcb19da34..f8f9d4796 100644
--- a/libhb/encavcodec.c
+++ b/libhb/encavcodec.c
@@ -39,8 +39,7 @@ struct hb_work_private_s
struct {
int64_t start;
- int64_t stop;
- int64_t renderOffset;
+ int64_t duration;
} frame_info[FRAME_INFO_SIZE];
};
@@ -209,7 +208,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
job->anamorphic.par_width, job->anamorphic.par_height );
}
- if( job->mux & HB_MUX_MP4 )
+ if( job->mux & HB_MUX_MASK_MP4 )
{
context->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
@@ -268,7 +267,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
{
job->areBframes = 1;
}
- if( ( job->mux & HB_MUX_MP4 ) && job->pass != 1 )
+ if( ( job->mux & HB_MUX_MASK_MP4 ) && job->pass != 1 )
{
w->config->mpeg4.length = context->extradata_size;
memcpy( w->config->mpeg4.bytes, context->extradata,
@@ -309,7 +308,7 @@ static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
{
int i = pv->frameno_in & FRAME_INFO_MASK;
pv->frame_info[i].start = in->s.start;
- pv->frame_info[i].stop = in->s.stop;
+ pv->frame_info[i].duration = in->s.stop - in->s.start;
}
static int64_t get_frame_start( hb_work_private_t * pv, int64_t frameno )
@@ -318,10 +317,10 @@ static int64_t get_frame_start( hb_work_private_t * pv, int64_t frameno )
return pv->frame_info[i].start;
}
-static int64_t get_frame_stop( hb_work_private_t * pv, int64_t frameno )
+static int64_t get_frame_duration( hb_work_private_t * pv, int64_t frameno )
{
int i = frameno & FRAME_INFO_MASK;
- return pv->frame_info[i].stop;
+ return pv->frame_info[i].duration;
}
static void compute_dts_offset( hb_work_private_t * pv, hb_buffer_t * buf )
@@ -469,10 +468,11 @@ int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
else
{
int64_t frameno = pkt.pts;
- buf->size = pkt.size;
- buf->s.start = get_frame_start( pv, frameno );
- buf->s.stop = get_frame_stop( pv, frameno );
- buf->s.flags &= ~HB_FRAME_REF;
+ buf->size = pkt.size;
+ buf->s.start = get_frame_start( pv, frameno );
+ buf->s.duration = get_frame_duration( pv, frameno );
+ buf->s.stop = buf->s.stop + buf->s.duration;
+ buf->s.flags &= ~HB_FRAME_REF;
switch ( pv->context->coded_frame->pict_type )
{
case AV_PICTURE_TYPE_P:
diff --git a/libhb/encavcodecaudio.c b/libhb/encavcodecaudio.c
index e24eaca32..44c3be75d 100644
--- a/libhb/encavcodecaudio.c
+++ b/libhb/encavcodecaudio.c
@@ -243,6 +243,9 @@ static int encavcodecaInit(hb_work_object_t *w, hb_job_t *job)
w->config->extradata.length = context->extradata_size;
}
+ audio->config.out.delay = av_rescale_q(context->delay, context->time_base,
+ (AVRational){1, 90000});
+
return 0;
}
@@ -388,21 +391,14 @@ static hb_buffer_t* Encode(hb_work_object_t *w)
if (got_packet && pkt.size)
{
out->size = pkt.size;
-
// The output pts from libav is in context->time_base. Convert it back
// to our timebase.
- //
- // Also account for the "delay" factor that libav seems to arbitrarily
- // subtract from the packet. Not sure WTH they think they are doing by
- // offsetting the value in a negative direction.
- out->s.start = av_rescale_q(pv->context->delay + pkt.pts,
- pv->context->time_base,
- (AVRational){1, 90000});
-
- out->s.stop = out->s.start + (90000 * pv->samples_per_frame /
- audio->config.out.samplerate);
-
- out->s.type = AUDIO_BUF;
+ out->s.start = av_rescale_q(pkt.pts, pv->context->time_base,
+ (AVRational){1, 90000});
+ out->s.duration = (double)90000 * pv->samples_per_frame /
+ audio->config.out.samplerate;
+ out->s.stop = out->s.start + out->s.duration;
+ out->s.type = AUDIO_BUF;
out->s.frametype = HB_FRAME_AUDIO;
}
else
diff --git a/libhb/encfaac.c b/libhb/encfaac.c
index 3bdd7cee0..551a7ea01 100644
--- a/libhb/encfaac.c
+++ b/libhb/encfaac.c
@@ -201,12 +201,13 @@ static hb_buffer_t * Encode( hb_work_object_t * w )
{
hb_buffer_t * buf = hb_buffer_init( size );
memcpy( buf->data, pv->obuf, size );
- buf->size = size;
- buf->s.start = pv->pts;
- pv->pts += pv->framedur;
- buf->s.stop = pv->pts;
- buf->s.type = AUDIO_BUF;
+ buf->size = size;
+ buf->s.start = pv->pts;
+ buf->s.duration = pv->framedur;
+ buf->s.stop = buf->s.start + buf->s.duration;
+ buf->s.type = AUDIO_BUF;
buf->s.frametype = HB_FRAME_AUDIO;
+ pv->pts += pv->framedur;
return buf;
}
return NULL;
diff --git a/libhb/enclame.c b/libhb/enclame.c
index 2858f6078..6cb4ab3a3 100644
--- a/libhb/enclame.c
+++ b/libhb/enclame.c
@@ -145,9 +145,10 @@ static hb_buffer_t * Encode( hb_work_object_t * w )
}
}
- buf = hb_buffer_init( pv->output_bytes );
- buf->s.start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
- buf->s.stop = buf->s.start + 90000 * 1152 / audio->config.out.samplerate;
+ buf = hb_buffer_init( pv->output_bytes );
+ buf->s.start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
+ buf->s.duration = (double)90000 * 1152 / audio->config.out.samplerate;
+ buf->s.stop = buf->s.start + buf->s.duration;
pv->pts = buf->s.stop;
buf->size = lame_encode_buffer_float(
pv->lame, samples[0], samples[1],
diff --git a/libhb/enctheora.c b/libhb/enctheora.c
index 07fdb7f95..36fd9e98a 100644
--- a/libhb/enctheora.c
+++ b/libhb/enctheora.c
@@ -178,17 +178,17 @@ int enctheoraInit( hb_work_object_t * w, hb_job_t * job )
th_comment_init( &tc );
- th_encode_flushheader( pv->ctx, &tc, &op );
- memcpy(w->config->theora.headers[0], &op, sizeof(op));
- memcpy(w->config->theora.headers[0] + sizeof(op), op.packet, op.bytes );
+ ogg_packet *header;
- th_encode_flushheader( pv->ctx, &tc, &op );
- memcpy(w->config->theora.headers[1], &op, sizeof(op));
- memcpy(w->config->theora.headers[1] + sizeof(op), op.packet, op.bytes );
-
- th_encode_flushheader( pv->ctx, &tc, &op );
- memcpy(w->config->theora.headers[2], &op, sizeof(op));
- memcpy(w->config->theora.headers[2] + sizeof(op), op.packet, op.bytes );
+ int ii;
+ for (ii = 0; ii < 3; ii++)
+ {
+ th_encode_flushheader( pv->ctx, &tc, &op );
+ header = (ogg_packet*)w->config->theora.headers[ii];
+ memcpy(header, &op, sizeof(op));
+ header->packet = w->config->theora.headers[ii] + sizeof(ogg_packet);
+ memcpy(header->packet, op.packet, op.bytes );
+ }
th_comment_clear( &tc );
@@ -362,15 +362,15 @@ int enctheoraWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
th_encode_packetout( pv->ctx, 0, &op );
- buf = hb_buffer_init( op.bytes + sizeof(op) );
- memcpy(buf->data, &op, sizeof(op));
- memcpy(buf->data + sizeof(op), op.packet, op.bytes);
+ buf = hb_buffer_init(op.bytes);
+ memcpy(buf->data, op.packet, op.bytes);
buf->f.fmt = AV_PIX_FMT_YUV420P;
buf->f.width = frame_width;
buf->f.height = frame_height;
buf->s.frametype = ( th_packet_iskeyframe(&op) ) ? HB_FRAME_KEY : HB_FRAME_REF;
- buf->s.start = in->s.start;
- buf->s.stop = in->s.stop;
+ buf->s.start = in->s.start;
+ buf->s.stop = in->s.stop;
+ buf->s.duration = in->s.stop - in->s.start;
*buf_out = buf;
diff --git a/libhb/encvorbis.c b/libhb/encvorbis.c
index e75bce622..c81b44d76 100644
--- a/libhb/encvorbis.c
+++ b/libhb/encvorbis.c
@@ -114,11 +114,13 @@ int encvorbisInit(hb_work_object_t *w, hb_job_t *job)
/* get the 3 headers */
vorbis_analysis_headerout(&pv->vd, &pv->vc,
&header[0], &header[1], &header[2]);
+ ogg_packet *pheader;
for (i = 0; i < 3; i++)
{
- memcpy(w->config->vorbis.headers[i], &header[i], sizeof(ogg_packet));
- memcpy(w->config->vorbis.headers[i] + sizeof(ogg_packet),
- header[i].packet, header[i].bytes);
+ pheader = (ogg_packet*)w->config->theora.headers[i];
+ memcpy(pheader, &header[i], sizeof(ogg_packet));
+ pheader->packet = w->config->theora.headers[i] + sizeof(ogg_packet);
+ memcpy(pheader->packet, header[i].packet, header[i].bytes );
}
pv->input_samples = pv->out_discrete_channels * OGGVORBIS_FRAME_SIZE;
@@ -180,10 +182,8 @@ static hb_buffer_t * Flush( hb_work_object_t * w )
if( vorbis_bitrate_flushpacket( &pv->vd, &op ) )
{
- buf = hb_buffer_init( sizeof( ogg_packet ) + op.bytes );
- memcpy( buf->data, &op, sizeof( ogg_packet ) );
- memcpy( buf->data + sizeof( ogg_packet ), op.packet,
- op.bytes );
+ buf = hb_buffer_init( op.bytes );
+ memcpy( buf->data, op.packet, op.bytes );
blocksize = vorbis_packet_blocksize(&pv->vi, &op);
buf->s.type = AUDIO_BUF;
@@ -191,6 +191,7 @@ static hb_buffer_t * Flush( hb_work_object_t * w )
buf->s.start = (int64_t)(vorbis_granule_time(&pv->vd, op.granulepos) * 90000);
buf->s.stop = (int64_t)(vorbis_granule_time(&pv->vd, (pv->prev_blocksize + blocksize)/4 + op.granulepos) * 90000);
+ buf->s.duration = buf->s.stop - buf->s.start;
/* The stop time isn't accurate for the first ~3 packets, as the actual blocksize depends on the previous _and_ current packets. */
pv->prev_blocksize = blocksize;
return buf;
diff --git a/libhb/encx264.c b/libhb/encx264.c
index df978206a..790fbcece 100644
--- a/libhb/encx264.c
+++ b/libhb/encx264.c
@@ -389,9 +389,9 @@ static hb_buffer_t *nal_encode( hb_work_object_t *w, x264_picture_t *pic_out,
buf->s.frametype = 0;
// use the pts to get the original frame's duration.
- int64_t duration = get_frame_duration( pv, pic_out->i_pts );
- buf->s.start = pic_out->i_pts;
- buf->s.stop = pic_out->i_pts + duration;
+ buf->s.duration = get_frame_duration( pv, pic_out->i_pts );
+ buf->s.start = pic_out->i_pts;
+ buf->s.stop = buf->s.start + buf->s.duration;
buf->s.renderOffset = pic_out->i_dts;
if ( !w->config->h264.init_delay && pic_out->i_dts < 0 )
{
diff --git a/libhb/fifo.c b/libhb/fifo.c
index df83340fd..a32242c03 100644
--- a/libhb/fifo.c
+++ b/libhb/fifo.c
@@ -299,6 +299,9 @@ hb_buffer_t * hb_buffer_init( int size )
b->alloc = buffer_pool->buffer_size;
b->size = size;
b->data = data;
+ b->s.start = -1;
+ b->s.stop = -1;
+ b->s.renderOffset = -1;
return( b );
}
}
@@ -335,6 +338,9 @@ hb_buffer_t * hb_buffer_init( int size )
buffers.allocated += b->alloc;
hb_unlock(buffers.lock);
}
+ b->s.start = -1;
+ b->s.stop = -1;
+ b->s.renderOffset = -1;
return b;
}
diff --git a/libhb/internal.h b/libhb/internal.h
index 4a2f775a9..d93119fb0 100644
--- a/libhb/internal.h
+++ b/libhb/internal.h
@@ -83,14 +83,15 @@ struct hb_buffer_s
uint8_t discontinuity;
int new_chap; // Video packets: if non-zero, is the index of the chapter whose boundary was crossed
- #define HB_FRAME_IDR 0x01
- #define HB_FRAME_I 0x02
- #define HB_FRAME_AUDIO 0x04
- #define HB_FRAME_P 0x10
- #define HB_FRAME_B 0x20
- #define HB_FRAME_BREF 0x40
- #define HB_FRAME_KEY 0x0F
- #define HB_FRAME_REF 0xF0
+ #define HB_FRAME_IDR 0x01
+ #define HB_FRAME_I 0x02
+ #define HB_FRAME_AUDIO 0x04
+ #define HB_FRAME_SUBTITLE 0x08
+ #define HB_FRAME_P 0x10
+ #define HB_FRAME_B 0x20
+ #define HB_FRAME_BREF 0x40
+ #define HB_FRAME_KEY 0x0F
+ #define HB_FRAME_REF 0xF0
uint8_t frametype;
uint16_t flags;
} s;
@@ -466,7 +467,9 @@ typedef struct hb_mux_data_s hb_mux_data_t;
hb_mux_object_t * hb_mux_##a##_init( hb_job_t * );
DECLARE_MUX( mp4 );
-DECLARE_MUX( avi );
-DECLARE_MUX( ogm );
DECLARE_MUX( mkv );
+DECLARE_MUX( avformat );
+void hb_muxmp4_process_subtitle_style( uint8_t *input,
+ uint8_t *output,
+ uint8_t *style, uint16_t *stylesize );
diff --git a/libhb/module.defs b/libhb/module.defs
index 9f5e46795..49df1b28f 100644
--- a/libhb/module.defs
+++ b/libhb/module.defs
@@ -46,6 +46,15 @@ endif
ifeq (1,$(FEATURE.faac))
LIBHB.GCC.D += USE_FAAC
endif
+ifeq (1,$(FEATURE.mp4v2))
+LIBHB.GCC.D += USE_MP4V2
+endif
+ifeq (1,$(FEATURE.libmkv))
+LIBHB.GCC.D += USE_LIBMKV
+endif
+ifeq (1,$(FEATURE.avformat))
+LIBHB.GCC.D += USE_AVFORMAT
+endif
LIBHB.GCC.D += __LIBHB__ USE_PTHREAD
LIBHB.GCC.I += $(LIBHB.build/) $(CONTRIB.build/)include
@@ -102,7 +111,7 @@ LIBHB.lib = $(LIBHB.build/)hb.lib
LIBHB.dll.libs = $(foreach n, \
a52 ass avcodec avformat avutil avresample dvdnav dvdread \
- fontconfig freetype mkv mpeg2 mp3lame mp4v2 \
+ fontconfig freetype mpeg2 mp3lame \
ogg samplerate swscale theora vorbis vorbisenc x264 xml2 bluray, \
$(CONTRIB.build/)lib/lib$(n).a )
@@ -114,6 +123,14 @@ ifeq (1,$(FEATURE.faac))
LIBHB.dll.libs += $(CONTRIB.build/)lib/libfaac.a
endif
+ifeq (1,$(FEATURE.mp4v2))
+LIBHB.dll.libs += $(CONTRIB.build/)lib/libmp4v2.a
+endif
+
+ifeq (1,$(FEATURE.libmkv))
+LIBHB.dll.libs += $(CONTRIB.build/)lib/libmkv.a
+endif
+
ifneq ($(HAS.iconv),1)
LIBHB.dll.libs += $(CONTRIB.build/)lib/libiconv.a
else
diff --git a/libhb/muxavformat.c b/libhb/muxavformat.c
new file mode 100644
index 000000000..7053c6dc6
--- /dev/null
+++ b/libhb/muxavformat.c
@@ -0,0 +1,1239 @@
+/* muxavformat.c
+
+ Copyright (c) 2003-2013 HandBrake Team
+ This file is part of the HandBrake source code
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License v2.
+ For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
+ */
+
+#if defined(USE_AVFORMAT)
+
+#include <ogg/ogg.h>
+#include "libavformat/avformat.h"
+#include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
+
+#include "hb.h"
+#include "lang.h"
+
+struct hb_mux_data_s
+{
+ enum
+ {
+ MUX_TYPE_VIDEO,
+ MUX_TYPE_AUDIO,
+ MUX_TYPE_SUBTITLE
+ } type;
+
+ AVStream *st;
+
+ int64_t duration;
+
+ hb_buffer_t * delay_buf;
+
+ int64_t prev_chapter_tc;
+ int16_t current_chapter;
+};
+
+struct hb_mux_object_s
+{
+ HB_MUX_COMMON;
+
+ hb_job_t * job;
+
+ AVFormatContext * oc;
+ AVRational time_base;
+
+ int ntracks;
+ hb_mux_data_t ** tracks;
+
+ int delay;
+};
+
+enum
+{
+ META_TITLE,
+ META_ARTIST,
+ META_DIRECTOR,
+ META_COMPOSER,
+ META_RELEASE_DATE,
+ META_COMMENT,
+ META_ALBUM,
+ META_GENRE,
+ META_DESCRIPTION,
+ META_SYNOPSIS,
+ META_LAST
+};
+
+enum
+{
+ META_MUX_MP4,
+ META_MUX_MKV,
+ META_MUX_LAST
+};
+
+const char *metadata_keys[META_LAST][META_MUX_LAST] =
+{
+ {"title", "TITLE"},
+ {"artist", "ARTIST"},
+ {"album_artist", "DIRECTOR"},
+ {"composer", "COMPOSER"},
+ {"date", "DATE_RELEASED"},
+ {"comment", "SUMMARY"},
+ {"album", NULL},
+ {"genre", "GENRE"},
+ {"description", "DESCRIPTION"},
+ {"synopsis", "SYNOPSIS"}
+};
+
+static char* lookup_lang_code(int mux, char *iso639_2)
+{
+ iso639_lang_t *lang;
+ char *out = NULL;
+
+ switch (mux)
+ {
+ case HB_MUX_AV_MP4:
+ out = iso639_2;
+ break;
+ case HB_MUX_AV_MKV:
+ // MKV lang codes should be ISO-639-2B if it exists,
+ // else ISO-639-2
+ lang = lang_for_code2( iso639_2 );
+ out = lang->iso639_2b ? lang->iso639_2b : lang->iso639_2;
+ break;
+ default:
+ break;
+ }
+ return out;
+}
+
+/**********************************************************************
+ * avformatInit
+ **********************************************************************
+ * Allocates hb_mux_data_t structures, create file and write headers
+ *********************************************************************/
+static int avformatInit( hb_mux_object_t * m )
+{
+ hb_job_t * job = m->job;
+ hb_audio_t * audio;
+ hb_mux_data_t * track;
+ int meta_mux;
+ int max_tracks;
+ int ii, ret;
+
+ const char *muxer_name = NULL;
+
+ uint8_t default_track_flag = 1;
+ uint8_t need_fonts = 0;
+ char *lang;
+
+
+ m->delay = -1;
+ max_tracks = 1 + hb_list_count( job->list_audio ) +
+ hb_list_count( job->list_subtitle );
+
+ m->tracks = calloc(max_tracks, sizeof(hb_mux_data_t*));
+
+ m->oc = avformat_alloc_context();
+ if (m->oc == NULL)
+ {
+ hb_error( "Could not initialize avformat context." );
+ goto error;
+ }
+
+ switch (job->mux)
+ {
+ case HB_MUX_AV_MP4:
+ m->time_base.num = 1;
+ m->time_base.den = 90000;
+ if( job->ipod_atom )
+ muxer_name = "ipod";
+ else
+ muxer_name = "mp4";
+ meta_mux = META_MUX_MP4;
+ break;
+
+ case HB_MUX_AV_MKV:
+ // libavformat is essentially hard coded such that it only
+ // works with a timebase of 1/1000
+ m->time_base.num = 1;
+ m->time_base.den = 1000;
+ muxer_name = "matroska";
+ meta_mux = META_MUX_MKV;
+ break;
+
+ default:
+ {
+ hb_error("Invalid Mux %x", job->mux);
+ goto error;
+ }
+ }
+ m->oc->oformat = av_guess_format(muxer_name, NULL, NULL);
+ if(m->oc->oformat == NULL)
+ {
+ hb_error("Could not guess output format %s", muxer_name);
+ goto error;
+ }
+ av_strlcpy(m->oc->filename, job->file, sizeof(m->oc->filename));
+ ret = avio_open2(&m->oc->pb, job->file, AVIO_FLAG_WRITE,
+ &m->oc->interrupt_callback, NULL);
+ if( ret < 0 )
+ {
+ hb_error( "avio_open2 failed!");
+ goto error;
+ }
+
+ /* Video track */
+ track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
+ job->mux_data = track;
+
+ track->type = MUX_TYPE_VIDEO;
+ track->st = avformat_new_stream(m->oc, NULL);
+ if (track->st == NULL)
+ {
+ hb_error("Could not initialize video stream");
+ goto error;
+ }
+ track->st->time_base = m->time_base;
+ avcodec_get_context_defaults3(track->st->codec, NULL);
+
+ track->st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+ track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
+ uint8_t *priv_data = NULL;
+ int priv_size = 0;
+ switch (job->vcodec)
+ {
+ case HB_VCODEC_X264:
+ track->st->codec->codec_id = AV_CODEC_ID_H264;
+
+ /* Taken from x264 muxers.c */
+ priv_size = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 +
+ job->config.h264.pps_length;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+
+ priv_data[0] = 1;
+ priv_data[1] = job->config.h264.sps[1]; /* AVCProfileIndication */
+ priv_data[2] = job->config.h264.sps[2]; /* profile_compat */
+ priv_data[3] = job->config.h264.sps[3]; /* AVCLevelIndication */
+ priv_data[4] = 0xff; // nalu size length is four bytes
+ priv_data[5] = 0xe1; // one sps
+
+ priv_data[6] = job->config.h264.sps_length >> 8;
+ priv_data[7] = job->config.h264.sps_length;
+
+ memcpy(priv_data+8, job->config.h264.sps,
+ job->config.h264.sps_length);
+
+ priv_data[8+job->config.h264.sps_length] = 1; // one pps
+ priv_data[9+job->config.h264.sps_length] =
+ job->config.h264.pps_length >> 8;
+ priv_data[10+job->config.h264.sps_length] =
+ job->config.h264.pps_length;
+
+ memcpy(priv_data+11+job->config.h264.sps_length,
+ job->config.h264.pps, job->config.h264.pps_length );
+ break;
+
+ case HB_VCODEC_FFMPEG_MPEG4:
+ track->st->codec->codec_id = AV_CODEC_ID_MPEG4;
+
+ if (job->config.mpeg4.length != 0)
+ {
+ priv_size = job->config.mpeg4.length;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, job->config.mpeg4.bytes, priv_size);
+ }
+ break;
+
+ case HB_VCODEC_FFMPEG_MPEG2:
+ track->st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO;
+
+ if (job->config.mpeg4.length != 0)
+ {
+ priv_size = job->config.mpeg4.length;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, job->config.mpeg4.bytes, priv_size);
+ }
+ break;
+
+ case HB_VCODEC_THEORA:
+ {
+ track->st->codec->codec_id = AV_CODEC_ID_THEORA;
+
+ int size = 0;
+ ogg_packet *ogg_headers[3];
+
+ for (ii = 0; ii < 3; ii++)
+ {
+ ogg_headers[ii] = (ogg_packet *)job->config.theora.headers[ii];
+ size += ogg_headers[ii]->bytes + 2;
+ }
+
+ priv_size = size;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+
+ size = 0;
+ for(ii = 0; ii < 3; ii++)
+ {
+ AV_WB16(priv_data + size, ogg_headers[ii]->bytes);
+ size += 2;
+ memcpy(priv_data+size, ogg_headers[ii]->packet,
+ ogg_headers[ii]->bytes);
+ size += ogg_headers[ii]->bytes;
+ }
+ } break;
+
+ default:
+ hb_error("muxavformat: Unknown video codec: %x", job->vcodec);
+ goto error;
+ }
+ track->st->codec->extradata = priv_data;
+ track->st->codec->extradata_size = priv_size;
+
+ track->st->codec->width = job->width;
+ track->st->codec->height = job->height;
+ track->st->sample_aspect_ratio.num = job->anamorphic.par_width;
+ track->st->sample_aspect_ratio.den = job->anamorphic.par_height;
+ track->st->codec->sample_aspect_ratio.num = job->anamorphic.par_width;
+ track->st->codec->sample_aspect_ratio.den = job->anamorphic.par_height;
+ track->st->disposition |= AV_DISPOSITION_DEFAULT;
+
+ int vrate_base, vrate;
+ if( job->pass == 2 )
+ {
+ hb_interjob_t * interjob = hb_interjob_get( job->h );
+ vrate_base = interjob->vrate_base;
+ vrate = interjob->vrate;
+ }
+ else
+ {
+ vrate_base = job->vrate_base;
+ vrate = job->vrate;
+ }
+
+ // If the vrate is 27000000, there's a good chance this is
+ // a standard rate that we have in our hb_video_rates table.
+ // Because of rounding errors and approximations made while
+ // measuring framerate, the actual value may not be exact. So
+ // we look for rates that are "close" and make an adjustment
+ // to fps.den.
+ if (vrate == 27000000)
+ {
+ const hb_rate_t *video_framerate = NULL;
+ while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
+ {
+ if (abs(vrate_base - video_framerate->rate) < 10)
+ {
+ vrate_base = video_framerate->rate;
+ break;
+ }
+ }
+ }
+ hb_reduce(&vrate_base, &vrate, vrate_base, vrate);
+ if (job->mux == HB_MUX_AV_MP4)
+ {
+ // libavformat mp4 muxer requires that the codec time_base have the
+ // same denominator as the stream time_base, it uses it for the
+ // mdhd timescale.
+ double scale = (double)track->st->time_base.den / vrate;
+ track->st->codec->time_base.den = track->st->time_base.den;
+ track->st->codec->time_base.num = vrate_base * scale;
+ }
+ else
+ {
+ track->st->codec->time_base.num = vrate_base;
+ track->st->codec->time_base.den = vrate;
+ }
+
+ /* add the audio tracks */
+ for(ii = 0; ii < hb_list_count( job->list_audio ); ii++ )
+ {
+ audio = hb_list_item( job->list_audio, ii );
+ track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
+ audio->priv.mux_data = track;
+
+ track->type = MUX_TYPE_AUDIO;
+
+ track->st = avformat_new_stream(m->oc, NULL);
+ if (track->st == NULL)
+ {
+ hb_error("Could not initialize audio stream");
+ goto error;
+ }
+ avcodec_get_context_defaults3(track->st->codec, NULL);
+
+ track->st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+ track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ if (job->mux == HB_MUX_AV_MP4)
+ {
+ track->st->codec->time_base.num = audio->config.out.samples_per_frame;
+ track->st->codec->time_base.den = audio->config.out.samplerate;
+ track->st->time_base.num = 1;
+ track->st->time_base.den = audio->config.out.samplerate;
+ }
+ else
+ {
+ track->st->codec->time_base = m->time_base;
+ }
+
+ priv_data = NULL;
+ priv_size = 0;
+ switch (audio->config.out.codec & HB_ACODEC_MASK)
+ {
+ case HB_ACODEC_DCA:
+ case HB_ACODEC_DCA_HD:
+ track->st->codec->codec_id = AV_CODEC_ID_DTS;
+ break;
+ case HB_ACODEC_AC3:
+ track->st->codec->codec_id = AV_CODEC_ID_AC3;
+ break;
+ case HB_ACODEC_LAME:
+ case HB_ACODEC_MP3:
+ track->st->codec->codec_id = AV_CODEC_ID_MP3;
+ break;
+ case HB_ACODEC_VORBIS:
+ {
+ track->st->codec->codec_id = AV_CODEC_ID_VORBIS;
+
+ int jj, size = 0;
+ ogg_packet *ogg_headers[3];
+
+ for (jj = 0; jj < 3; jj++)
+ {
+ ogg_headers[jj] = (ogg_packet *)audio->priv.config.vorbis.headers[jj];
+ size += ogg_headers[jj]->bytes + 2;
+ }
+
+ priv_size = size;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+
+ size = 0;
+ for(jj = 0; jj < 3; jj++)
+ {
+ AV_WB16(priv_data + size, ogg_headers[jj]->bytes);
+ size += 2;
+ memcpy(priv_data+size, ogg_headers[jj]->packet,
+ ogg_headers[jj]->bytes);
+ size += ogg_headers[jj]->bytes;
+ }
+ } break;
+ case HB_ACODEC_FFFLAC:
+ case HB_ACODEC_FFFLAC24:
+ track->st->codec->codec_id = AV_CODEC_ID_FLAC;
+
+ if (audio->priv.config.extradata.bytes)
+ {
+ priv_size = audio->priv.config.extradata.length;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data,
+ audio->priv.config.extradata.bytes,
+ audio->priv.config.extradata.length);
+ }
+ break;
+ case HB_ACODEC_FAAC:
+ case HB_ACODEC_FFAAC:
+ case HB_ACODEC_CA_AAC:
+ case HB_ACODEC_CA_HAAC:
+ case HB_ACODEC_FDK_AAC:
+ case HB_ACODEC_FDK_HAAC:
+ track->st->codec->codec_id = AV_CODEC_ID_AAC;
+
+ if (audio->priv.config.extradata.bytes)
+ {
+ priv_size = audio->priv.config.extradata.length;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data,
+ audio->priv.config.extradata.bytes,
+ audio->priv.config.extradata.length);
+ }
+ break;
+ default:
+ hb_error("muxavformat: Unknown audio codec: %x",
+ audio->config.out.codec);
+ goto error;
+ }
+ track->st->codec->extradata = priv_data;
+ track->st->codec->extradata_size = priv_size;
+
+ if( default_track_flag )
+ {
+ track->st->disposition |= AV_DISPOSITION_DEFAULT;
+ default_track_flag = 0;
+ }
+
+ lang = lookup_lang_code(job->mux, audio->config.lang.iso639_2 );
+ if (lang != NULL)
+ {
+ av_dict_set(&track->st->metadata, "language", lang, 0);
+ }
+ track->st->codec->sample_rate = audio->config.out.samplerate;
+ if (audio->config.out.codec & HB_ACODEC_PASS_FLAG)
+ {
+ track->st->codec->channels = av_get_channel_layout_nb_channels(audio->config.in.channel_layout);
+ track->st->codec->channel_layout = audio->config.in.channel_layout;
+ }
+ else
+ {
+ track->st->codec->channels = hb_mixdown_get_discrete_channel_count(audio->config.out.mixdown);
+ track->st->codec->channel_layout = hb_ff_mixdown_xlat(audio->config.out.mixdown, NULL);
+ }
+
+ char *name;
+ if (audio->config.out.name == NULL)
+ {
+ switch (track->st->codec->channels)
+ {
+ case 1:
+ name = "Mono";
+ break;
+
+ case 2:
+ name = "Stereo";
+ break;
+
+ default:
+ name = "Surround";
+ break;
+ }
+ }
+ else
+ {
+ name = audio->config.out.name;
+ }
+ av_dict_set(&track->st->metadata, "title", name, 0);
+ }
+
+ char * subidx_fmt =
+ "size: %dx%d\n"
+ "org: %d, %d\n"
+ "scale: 100%%, 100%%\n"
+ "alpha: 100%%\n"
+ "smooth: OFF\n"
+ "fadein/out: 50, 50\n"
+ "align: OFF at LEFT TOP\n"
+ "time offset: 0\n"
+ "forced subs: %s\n"
+ "palette: %06x, %06x, %06x, %06x, %06x, %06x, "
+ "%06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x, %06x\n"
+ "custom colors: OFF, tridx: 0000, "
+ "colors: 000000, 000000, 000000, 000000\n";
+
+ int subtitle_default = -1;
+ for( ii = 0; ii < hb_list_count( job->list_subtitle ); ii++ )
+ {
+ hb_subtitle_t *subtitle = hb_list_item( job->list_subtitle, ii );
+
+ if( subtitle->config.dest == PASSTHRUSUB )
+ {
+ if ( subtitle->config.default_track )
+ subtitle_default = ii;
+ }
+ }
+ // Quicktime requires that at least one subtitle is enabled,
+ // else it doesn't show any of the subtitles.
+ // So check to see if any of the subtitles are flagged to be
+ // the defualt. The default will the the enabled track, else
+ // enable the first track.
+ if (job->mux == HB_MUX_AV_MP4 && subtitle_default == -1)
+ {
+ subtitle_default = 0;
+ }
+
+ for( ii = 0; ii < hb_list_count( job->list_subtitle ); ii++ )
+ {
+ hb_subtitle_t * subtitle;
+ uint32_t rgb[16];
+ char subidx[2048];
+ int len;
+
+ subtitle = hb_list_item( job->list_subtitle, ii );
+ if (subtitle->config.dest != PASSTHRUSUB)
+ continue;
+
+ track = m->tracks[m->ntracks++] = calloc(1, sizeof( hb_mux_data_t ) );
+ subtitle->mux_data = track;
+
+ track->type = MUX_TYPE_SUBTITLE;
+ track->st = avformat_new_stream(m->oc, NULL);
+ if (track->st == NULL)
+ {
+ hb_error("Could not initialize subtitle stream");
+ goto error;
+ }
+ avcodec_get_context_defaults3(track->st->codec, NULL);
+
+ track->st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+ track->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ track->st->time_base = m->time_base;
+ track->st->codec->time_base = m->time_base;
+ track->st->codec->width = subtitle->width;
+ track->st->codec->height = subtitle->height;
+
+ priv_data = NULL;
+ priv_size = 0;
+ switch (subtitle->source)
+ {
+ case VOBSUB:
+ {
+ int jj;
+ track->st->codec->codec_id = AV_CODEC_ID_DVD_SUBTITLE;
+
+ for (jj = 0; jj < 16; jj++)
+ rgb[jj] = hb_yuv2rgb(subtitle->palette[jj]);
+ len = snprintf(subidx, 2048, subidx_fmt,
+ subtitle->width, subtitle->height,
+ 0, 0, "OFF",
+ rgb[0], rgb[1], rgb[2], rgb[3],
+ rgb[4], rgb[5], rgb[6], rgb[7],
+ rgb[8], rgb[9], rgb[10], rgb[11],
+ rgb[12], rgb[13], rgb[14], rgb[15]);
+
+ priv_size = len + 1;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, subidx, priv_size);
+ } break;
+
+ case PGSSUB:
+ {
+ track->st->codec->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE;
+ } break;
+
+ case SSASUB:
+ {
+ if (job->mux == HB_MUX_AV_MP4)
+ {
+ track->st->codec->codec_id = AV_CODEC_ID_MOV_TEXT;
+ }
+ else
+ {
+ track->st->codec->codec_id = AV_CODEC_ID_SSA;
+ need_fonts = 1;
+
+ if (subtitle->extradata_size)
+ {
+ priv_size = subtitle->extradata_size;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, subtitle->extradata, priv_size);
+ }
+ }
+ } break;
+
+ case CC608SUB:
+ case CC708SUB:
+ case UTF8SUB:
+ case TX3GSUB:
+ case SRTSUB:
+ {
+ if (job->mux == HB_MUX_AV_MP4)
+ track->st->codec->codec_id = AV_CODEC_ID_MOV_TEXT;
+ else
+ track->st->codec->codec_id = AV_CODEC_ID_TEXT;
+ } break;
+
+ default:
+ continue;
+ }
+ if (track->st->codec->codec_id == AV_CODEC_ID_MOV_TEXT)
+ {
+ // Build codec extradata for tx3g.
+ // If we were using a libav codec to generate this data
+ // this would (or should) be done for us.
+ uint8_t properties[] = {
+ 0x00, 0x00, 0x00, 0x00, // Display Flags
+ 0x01, // Horiz. Justification
+ 0xff, // Vert. Justification
+ 0x00, 0x00, 0x00, 0xff, // Bg color
+ 0x00, 0x00, 0x00, 0x00, // Default text box
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, // Reserved
+ 0x00, 0x01, // Font ID
+ 0x00, // Font face
+ 0x18, // Font size
+ 0xff, 0xff, 0xff, 0xff, // Fg color
+ // Font table:
+ 0x00, 0x00, 0x00, 0x12, // Font table size
+ 'f','t','a','b', // Tag
+ 0x00, 0x01, // Count
+ 0x00, 0x01, // Font ID
+ 0x05, // Font name length
+ 'A','r','i','a','l' // Font name
+ };
+
+ int width, height = 60;
+ if (job->anamorphic.mode)
+ width = job->width * ((float)job->anamorphic.par_width / job->anamorphic.par_height);
+ else
+ width = job->width;
+ track->st->codec->width = width;
+ track->st->codec->height = height;
+ properties[14] = height >> 8;
+ properties[15] = height & 0xff;
+ properties[16] = width >> 8;
+ properties[17] = width & 0xff;
+
+ priv_size = sizeof(properties);
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, properties, priv_size);
+ }
+ track->st->codec->extradata = priv_data;
+ track->st->codec->extradata_size = priv_size;
+
+ if ( ii == subtitle_default )
+ {
+ track->st->disposition |= AV_DISPOSITION_DEFAULT;
+ }
+
+ lang = lookup_lang_code(job->mux, subtitle->iso639_2 );
+ if (lang != NULL)
+ {
+ av_dict_set(&track->st->metadata, "language", lang, 0);
+ }
+ }
+
+ if (need_fonts)
+ {
+ hb_list_t * list_attachment = job->list_attachment;
+ int i;
+ for ( i = 0; i < hb_list_count(list_attachment); i++ )
+ {
+ hb_attachment_t * attachment = hb_list_item( list_attachment, i );
+
+ if (attachment->type == FONT_TTF_ATTACH &&
+ attachment->size > 0)
+ {
+ AVStream *st = avformat_new_stream(m->oc, NULL);
+ if (st == NULL)
+ {
+ hb_error("Could not initialize attachment stream");
+ goto error;
+ }
+ avcodec_get_context_defaults3(st->codec, NULL);
+
+ st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
+ track->st->codec->codec_id = AV_CODEC_ID_TTF;
+
+ priv_size = attachment->size;
+ priv_data = av_malloc(priv_size);
+ if (priv_data == NULL)
+ {
+ hb_error("malloc failure");
+ goto error;
+ }
+ memcpy(priv_data, attachment->data, priv_size);
+
+ track->st->codec->extradata = priv_data;
+ track->st->codec->extradata_size = priv_size;
+
+ av_dict_set(&st->metadata, "filename", attachment->name, 0);
+ }
+ }
+ }
+
+ if( job->metadata )
+ {
+ hb_metadata_t *md = job->metadata;
+
+ hb_deep_log(2, "Writing Metadata to output file...");
+ if (md->name &&
+ metadata_keys[META_TITLE][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_TITLE][meta_mux], md->name, 0);
+ }
+ if (md->artist &&
+ metadata_keys[META_ARTIST][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_ARTIST][meta_mux], md->artist, 0);
+ }
+ if (md->album_artist &&
+ metadata_keys[META_DIRECTOR][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_DIRECTOR][meta_mux],
+ md->album_artist, 0);
+ }
+ if (md->composer &&
+ metadata_keys[META_COMPOSER][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_COMPOSER][meta_mux],
+ md->composer, 0);
+ }
+ if (md->release_date &&
+ metadata_keys[META_RELEASE_DATE][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_RELEASE_DATE][meta_mux],
+ md->release_date, 0);
+ }
+ if (md->comment &&
+ metadata_keys[META_COMMENT][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_COMMENT][meta_mux], md->comment, 0);
+ }
+ if (!md->name && md->album &&
+ metadata_keys[META_ALBUM][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_ALBUM][meta_mux], md->album, 0);
+ }
+ if (md->genre &&
+ metadata_keys[META_GENRE][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_GENRE][meta_mux], md->genre, 0);
+ }
+ if (md->description &&
+ metadata_keys[META_DESCRIPTION][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_DESCRIPTION][meta_mux],
+ md->description, 0);
+ }
+ if (md->long_description &&
+ metadata_keys[META_SYNOPSIS][meta_mux] != NULL)
+ {
+ av_dict_set(&m->oc->metadata,
+ metadata_keys[META_SYNOPSIS][meta_mux],
+ md->long_description, 0);
+ }
+ }
+
+ AVDictionary * av_opts = NULL;
+ if (job->mp4_optimize && (job->mux & HB_MUX_MASK_MP4))
+ av_dict_set( &av_opts, "movflags", "faststart", 0 );
+
+ ret = avformat_write_header(m->oc, &av_opts);
+ if( ret < 0 )
+ {
+ av_dict_free( &av_opts );
+ hb_error( "muxavformat: avformat_write_header failed!");
+ goto error;
+ }
+
+ AVDictionaryEntry *t = NULL;
+ while( ( t = av_dict_get( av_opts, "", t, AV_DICT_IGNORE_SUFFIX ) ) )
+ {
+ hb_log( "muxavformat: Unknown option %s", t->key );
+ }
+ av_dict_free( &av_opts );
+
+ return 0;
+
+error:
+ free(job->mux_data);
+ job->mux_data = NULL;
+ avformat_free_context(m->oc);
+ *job->die = 1;
+ return -1;
+}
+
+static int add_chapter(hb_mux_object_t *m, int64_t start, int64_t end, char * title)
+{
+ AVChapter *chap;
+ AVChapter **chapters;
+ int nchap = m->oc->nb_chapters;
+
+ nchap++;
+ chapters = av_realloc(m->oc->chapters, nchap * sizeof(AVChapter*));
+ if (chapters == NULL)
+ {
+ hb_error("malloc failure");
+ return -1;
+ }
+
+ chap = av_mallocz(sizeof(AVChapter));
+ if (chap == NULL)
+ {
+ hb_error("malloc failure");
+ return -1;
+ }
+
+ m->oc->chapters = chapters;
+ m->oc->chapters[nchap-1] = chap;
+ m->oc->nb_chapters = nchap;
+
+ chap->id = nchap;
+ chap->time_base = m->time_base;
+ chap->start = start;
+ chap->end = end;
+ av_dict_set(&chap->metadata, "title", title, 0);
+
+ return 0;
+}
+
+// Video with b-frames and certain audio types require a lead-in delay.
+// Compute the max delay and offset all timestamps by this amount.
+//
+// For mp4, avformat will automatically put entries in the edts atom
+// to account for the offset of the first dts in each track.
+static void computeDelay(hb_mux_object_t *m)
+{
+ int ii;
+ hb_audio_t * audio;
+
+ m->delay = m->job->config.h264.init_delay;
+ for(ii = 0; ii < hb_list_count( m->job->list_audio ); ii++ )
+ {
+ audio = hb_list_item( m->job->list_audio, ii );
+ if (audio->config.out.delay > m->delay)
+ m->delay = audio->config.out.delay;
+ }
+}
+
+static int avformatMux(hb_mux_object_t *m, hb_mux_data_t *track, hb_buffer_t *buf)
+{
+ AVPacket pkt;
+ int64_t dts, pts, duration = -1;
+ hb_job_t *job = m->job;
+ uint8_t tx3g_out[2048];
+
+ if (m->delay == -1)
+ {
+ computeDelay(m);
+ }
+
+ if (buf != NULL)
+ {
+ if (buf->s.start != -1)
+ buf->s.start += m->delay;
+ if (buf->s.renderOffset != -1)
+ buf->s.renderOffset += m->delay;
+ }
+
+ // We only compute dts duration for MP4 files
+ if (track->type == MUX_TYPE_VIDEO && (job->mux & HB_MUX_MASK_MP4))
+ {
+ hb_buffer_t * tmp;
+
+ // delay by one frame so that we can compute duration properly.
+ tmp = track->delay_buf;
+ track->delay_buf = buf;
+ buf = tmp;
+ }
+ if (buf == NULL)
+ return 0;
+
+ if (buf->s.renderOffset == -1)
+ {
+ dts = av_rescale_q(buf->s.start, (AVRational){1,90000},
+ track->st->time_base);
+ }
+ else
+ {
+ dts = av_rescale_q(buf->s.renderOffset, (AVRational){1,90000},
+ track->st->time_base);
+ }
+
+ pts = av_rescale_q(buf->s.start, (AVRational){1,90000},
+ track->st->time_base);
+
+ if (track->type == MUX_TYPE_VIDEO && track->delay_buf != NULL)
+ {
+ int64_t delayed_dts;
+ delayed_dts = av_rescale_q(track->delay_buf->s.renderOffset,
+ (AVRational){1,90000},
+ track->st->time_base);
+ duration = delayed_dts - dts;
+ }
+ if (duration < 0 && buf->s.duration > 0)
+ {
+ duration = av_rescale_q(buf->s.duration, (AVRational){1,90000},
+ track->st->time_base);
+ }
+ if (duration < 0)
+ {
+ // There is a possiblility that some subtitles get through the pipeline
+ // without ever discovering their true duration. Make the duration
+ // 10 seconds in this case.
+ if (track->type == MUX_TYPE_SUBTITLE)
+ duration = av_rescale_q(10, (AVRational){1,1},
+ track->st->time_base);
+ else
+ duration = 0;
+ }
+
+ av_init_packet(&pkt);
+ pkt.data = buf->data;
+ pkt.size = buf->size;
+ pkt.dts = dts;
+ pkt.pts = pts;
+ pkt.duration = duration;
+
+ if (track->type == MUX_TYPE_VIDEO &&
+ (job->vcodec == HB_VCODEC_X264 || job->vcodec & HB_VCODEC_FFMPEG_MASK))
+ {
+ if (buf->s.frametype == HB_FRAME_IDR)
+ pkt.flags |= AV_PKT_FLAG_KEY;
+ }
+ else if (buf->s.frametype & HB_FRAME_KEY)
+ {
+ pkt.flags |= AV_PKT_FLAG_KEY;
+ }
+
+ track->duration += pkt.duration;
+
+ switch (track->type)
+ {
+ case MUX_TYPE_VIDEO:
+ {
+ if (job->chapter_markers && buf->s.new_chap)
+ {
+ hb_chapter_t *chapter;
+
+ // reached chapter N, write marker for chapter N-1
+ // we don't know the end time of chapter N-1 till we receive
+ // chapter N. So we are always writing the previous chapter
+ // mark.
+ track->current_chapter = buf->s.new_chap - 1;
+
+ // chapter numbers start at 1, but the list starts at 0
+ chapter = hb_list_item(job->list_chapter,
+ track->current_chapter - 1);
+
+ // make sure we're not writing a chapter that has 0 length
+ if (chapter != NULL && track->prev_chapter_tc < pkt.pts)
+ {
+ char title[1024];
+ if (chapter->title != NULL)
+ {
+ snprintf(title, 1023, "%s", chapter->title);
+ }
+ else
+ {
+ snprintf(title, 1023, "Chapter %d",
+ track->current_chapter);
+ }
+ add_chapter(m, track->prev_chapter_tc, pkt.pts, title);
+ }
+ track->prev_chapter_tc = pkt.pts;
+ }
+ } break;
+
+ case MUX_TYPE_SUBTITLE:
+ {
+ if (job->mux == HB_MUX_AV_MP4)
+ {
+ /* Write an empty sample */
+ if ( track->duration < pts )
+ {
+ AVPacket empty_pkt;
+ uint8_t empty[2] = {0,0};
+
+ av_init_packet(&empty_pkt);
+ empty_pkt.data = empty;
+ empty_pkt.size = 2;
+ empty_pkt.dts = track->duration;
+ empty_pkt.pts = track->duration;
+ empty_pkt.duration = pts - duration;
+ empty_pkt.convergence_duration = empty_pkt.duration;
+ empty_pkt.stream_index = track->st->index;
+ int ret = av_interleaved_write_frame(m->oc, &empty_pkt);
+ if (ret < 0)
+ {
+ hb_error("av_interleaved_write_frame failed!");
+ *job->die = 1;
+ return -1;
+ }
+ track->duration = pts;
+ }
+ uint8_t styleatom[2048];;
+ uint16_t stylesize = 0;
+ uint8_t buffer[2048];
+ uint16_t buffersize = 0;
+
+ *buffer = '\0';
+
+ /*
+ * Copy the subtitle into buffer stripping markup and creating
+ * style atoms for them.
+ */
+ hb_muxmp4_process_subtitle_style( buf->data,
+ buffer,
+ styleatom, &stylesize );
+
+ buffersize = strlen((char*)buffer);
+
+ /* Write the subtitle sample */
+ memcpy( tx3g_out + 2, buffer, buffersize );
+ memcpy( tx3g_out + 2 + buffersize, styleatom, stylesize);
+ tx3g_out[0] = ( buffersize >> 8 ) & 0xff;
+ tx3g_out[1] = buffersize & 0xff;
+ pkt.data = tx3g_out;
+ pkt.size = buffersize + stylesize + 2;
+ }
+ pkt.convergence_duration = pkt.duration;
+
+ } break;
+ case MUX_TYPE_AUDIO:
+ default:
+ break;
+ }
+
+ pkt.stream_index = track->st->index;
+ int ret = av_interleaved_write_frame(m->oc, &pkt);
+ if (ret < 0)
+ {
+ hb_error("av_interleaved_write_frame failed!");
+ *job->die = 1;
+ return -1;
+ }
+
+ hb_buffer_close( &buf );
+ return 0;
+}
+
+static int avformatEnd(hb_mux_object_t *m)
+{
+ hb_job_t *job = m->job;
+ hb_mux_data_t *track = job->mux_data;
+
+ if( !job->mux_data )
+ {
+ /*
+ * We must have failed to create the file in the first place.
+ */
+ return 0;
+ }
+
+ // Flush any delayed frames
+ int ii;
+ for (ii = 0; ii < m->ntracks; ii++)
+ {
+ avformatMux(m, m->tracks[ii], NULL);
+ }
+
+ if (job->chapter_markers)
+ {
+ hb_chapter_t *chapter;
+
+ // get the last chapter
+ chapter = hb_list_item(job->list_chapter, track->current_chapter++);
+
+ // only write the last chapter marker if it lasts at least 1.5 second
+ if (chapter != NULL && chapter->duration > 135000LL)
+ {
+ char title[1024];
+ if (chapter->title != NULL)
+ {
+ snprintf(title, 1023, "%s", chapter->title);
+ }
+ else
+ {
+ snprintf(title, 1023, "Chapter %d", track->current_chapter);
+ }
+ add_chapter(m, track->prev_chapter_tc, track->duration, title);
+ }
+ }
+
+ // Update and track private data that can change during
+ // encode.
+ for(ii = 0; ii < hb_list_count( job->list_audio ); ii++)
+ {
+ AVStream *st;
+ hb_audio_t * audio;
+
+ audio = hb_list_item(job->list_audio, ii);
+ st = audio->priv.mux_data->st;
+
+ switch (audio->config.out.codec & HB_ACODEC_MASK)
+ {
+ case HB_ACODEC_FFFLAC:
+ case HB_ACODEC_FFFLAC24:
+ if( audio->priv.config.extradata.bytes )
+ {
+ uint8_t *priv_data;
+ int priv_size;
+
+ priv_size = audio->priv.config.extradata.length;
+ priv_data = av_realloc(st->codec->extradata, priv_size);
+ if (priv_data == NULL)
+ {
+ break;
+ }
+ memcpy(priv_data,
+ audio->priv.config.extradata.bytes,
+ audio->priv.config.extradata.length);
+ st->codec->extradata = priv_data;
+ st->codec->extradata_size = priv_size;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ av_write_trailer(m->oc);
+ avio_close(m->oc->pb);
+ avformat_free_context(m->oc);
+ m->oc = NULL;
+
+ return 0;
+}
+
+hb_mux_object_t * hb_mux_avformat_init( hb_job_t * job )
+{
+ hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );
+ m->init = avformatInit;
+ m->mux = avformatMux;
+ m->end = avformatEnd;
+ m->job = job;
+ return m;
+}
+
+#endif // USE_AVFORMAT
diff --git a/libhb/muxcommon.c b/libhb/muxcommon.c
index a97ada699..9417c707b 100644
--- a/libhb/muxcommon.c
+++ b/libhb/muxcommon.c
@@ -459,12 +459,22 @@ hb_work_object_t * hb_muxer_init( hb_job_t * job )
{
switch( job->mux )
{
- case HB_MUX_MP4:
+#ifdef USE_MP4V2
+ case HB_MUX_MP4V2:
mux->m = hb_mux_mp4_init( job );
break;
- case HB_MUX_MKV:
+#endif
+#ifdef USE_LIBMKV
+ case HB_MUX_LIBMKV:
mux->m = hb_mux_mkv_init( job );
break;
+#endif
+#ifdef USE_AVFORMAT
+ case HB_MUX_AV_MP4:
+ case HB_MUX_AV_MKV:
+ mux->m = hb_mux_avformat_init( job );
+ break;
+#endif
default:
hb_error( "No muxer selected, exiting" );
*job->die = 1;
@@ -544,3 +554,245 @@ hb_work_object_t hb_muxer =
muxClose
};
+typedef struct stylerecord_s {
+ enum style_s {ITALIC, BOLD, UNDERLINE} style;
+ uint16_t start;
+ uint16_t stop;
+ struct stylerecord_s *next;
+} stylerecord;
+
+static void hb_makestylerecord( stylerecord **stack,
+ enum style_s style, int start )
+{
+ stylerecord *record = calloc( sizeof( stylerecord ), 1 );
+
+ if( record )
+ {
+ record->style = style;
+ record->start = start;
+ record->next = *stack;
+ *stack = record;
+ }
+}
+
+static void hb_makestyleatom( stylerecord *record, uint8_t *style)
+{
+ uint8_t face = 1;
+ hb_deep_log(3, "Made style '%s' from %d to %d",
+ record->style == ITALIC ? "Italic" : record->style == BOLD ? "Bold" : "Underline", record->start, record->stop);
+
+ switch( record->style )
+ {
+ case ITALIC:
+ face = 2;
+ break;
+ case BOLD:
+ face = 1;
+ break;
+ case UNDERLINE:
+ face = 4;
+ break;
+ default:
+ face = 2;
+ break;
+ }
+
+ style[0] = (record->start >> 8) & 0xff; // startChar
+ style[1] = record->start & 0xff;
+ style[2] = (record->stop >> 8) & 0xff; // endChar
+ style[3] = record->stop & 0xff;
+ style[4] = (1 >> 8) & 0xff; // font-ID
+ style[5] = 1 & 0xff;
+ style[6] = face; // face-style-flags: 1 bold; 2 italic; 4 underline
+ style[7] = 24; // font-size
+ style[8] = 255; // r
+ style[9] = 255; // g
+ style[10] = 255; // b
+ style[11] = 255; // a
+}
+
+/*
+ * Copy the input to output removing markup and adding markup to the style
+ * atom where appropriate.
+ */
+void hb_muxmp4_process_subtitle_style( uint8_t *input,
+ uint8_t *output,
+ uint8_t *style, uint16_t *stylesize )
+{
+ uint8_t *reader = input;
+ uint8_t *writer = output;
+ uint8_t stylecount = 0;
+ uint16_t utf8_count = 0; // utf8 count from start of subtitle
+ stylerecord *stylestack = NULL;
+ stylerecord *oldrecord = NULL;
+
+ while(*reader != '\0') {
+ if( ( *reader & 0xc0 ) == 0x80 )
+ {
+ /*
+ * Track the utf8_count when doing markup so that we get the tx3g stops
+ * based on UTF8 chr counts rather than bytes.
+ */
+ utf8_count++;
+ hb_deep_log( 3, "MuxMP4: Counted %d UTF-8 chrs within subtitle so far",
+ utf8_count);
+ }
+ if (*reader == '<') {
+ /*
+ * possible markup, peek at the next chr
+ */
+ switch(*(reader+1)) {
+ case 'i':
+ if (*(reader+2) == '>') {
+ reader += 3;
+ hb_makestylerecord(&stylestack, ITALIC, (writer - output - utf8_count));
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ case 'b':
+ if (*(reader+2) == '>') {
+ reader += 3;
+ hb_makestylerecord(&stylestack, BOLD, (writer - output - utf8_count));
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ case 'u':
+ if (*(reader+2) == '>') {
+ reader += 3;
+ hb_makestylerecord(&stylestack, UNDERLINE, (writer - output - utf8_count));
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ case '/':
+ switch(*(reader+2)) {
+ case 'i':
+ if (*(reader+3) == '>') {
+ /*
+ * Check whether we then immediately start more markup of the same type, if so then
+ * lets not close it now and instead continue this markup.
+ */
+ if ((*(reader+4) && *(reader+4) == '<') &&
+ (*(reader+5) && *(reader+5) == 'i') &&
+ (*(reader+6) && *(reader+6) == '>')) {
+ /*
+ * Opening italics right after, so don't close off these italics.
+ */
+ hb_deep_log(3, "Joining two sets of italics");
+ reader += (4 + 3);
+ continue;
+ }
+
+
+ if ((*(reader+4) && *(reader+4) == ' ') &&
+ (*(reader+5) && *(reader+5) == '<') &&
+ (*(reader+6) && *(reader+6) == 'i') &&
+ (*(reader+7) && *(reader+7) == '>')) {
+ /*
+ * Opening italics right after, so don't close off these italics.
+ */
+ hb_deep_log(3, "Joining two sets of italics (plus space)");
+ reader += (4 + 4);
+ *writer++ = ' ';
+ continue;
+ }
+ if (stylestack && stylestack->style == ITALIC) {
+ uint8_t style_record[12];
+ stylestack->stop = writer - output - utf8_count;
+ hb_makestyleatom(stylestack, style_record);
+
+ memcpy(style + 10 + (12 * stylecount), style_record, 12);
+ stylecount++;
+
+ oldrecord = stylestack;
+ stylestack = stylestack->next;
+ free(oldrecord);
+ } else {
+ hb_error("Mismatched Subtitle markup '%s'", input);
+ }
+ reader += 4;
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ case 'b':
+ if (*(reader+3) == '>') {
+ if (stylestack && stylestack->style == BOLD) {
+ uint8_t style_record[12];
+ stylestack->stop = writer - output - utf8_count;
+ hb_makestyleatom(stylestack, style_record);
+
+ memcpy(style + 10 + (12 * stylecount), style_record, 12);
+ stylecount++;
+ oldrecord = stylestack;
+ stylestack = stylestack->next;
+ free(oldrecord);
+ } else {
+ hb_error("Mismatched Subtitle markup '%s'", input);
+ }
+
+ reader += 4;
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ case 'u':
+ if (*(reader+3) == '>') {
+ if (stylestack && stylestack->style == UNDERLINE) {
+ uint8_t style_record[12];
+ stylestack->stop = writer - output - utf8_count;
+ hb_makestyleatom(stylestack, style_record);
+
+ memcpy(style + 10 + (12 * stylecount), style_record, 12);
+ stylecount++;
+
+ oldrecord = stylestack;
+ stylestack = stylestack->next;
+ free(oldrecord);
+ } else {
+ hb_error("Mismatched Subtitle markup '%s'", input);
+ }
+ reader += 4;
+ } else {
+ *writer++ = *reader++;
+ }
+ break;
+ default:
+ *writer++ = *reader++;
+ break;
+ }
+ break;
+ default:
+ *writer++ = *reader++;
+ break;
+ }
+ } else if (*reader == '\r') {
+ // skip '\r' and replace with '\n' if necessary
+ if (*(++reader) != '\n') {
+ *writer++ = '\n';
+ }
+ } else {
+ *writer++ = *reader++;
+ }
+ }
+ *writer = '\0';
+
+ if( stylecount )
+ {
+ *stylesize = 10 + ( stylecount * 12 );
+
+ memcpy( style + 4, "styl", 4);
+
+ style[0] = 0;
+ style[1] = 0;
+ style[2] = (*stylesize >> 8) & 0xff;
+ style[3] = *stylesize & 0xff;
+ style[8] = (stylecount >> 8) & 0xff;
+ style[9] = stylecount & 0xff;
+
+ }
+
+}
+
diff --git a/libhb/muxmkv.c b/libhb/muxmkv.c
index a170eb557..ca72a7be8 100644
--- a/libhb/muxmkv.c
+++ b/libhb/muxmkv.c
@@ -8,6 +8,9 @@
*/
/* libmkv header */
+
+#if defined(USE_LIBMKV)
+
#include "libmkv.h"
#include <ogg/ogg.h>
@@ -17,15 +20,16 @@
/* Scale factor to apply to timecodes to convert from HandBrake's
* 1/90000s to nanoseconds as expected by libmkv */
-#define TIMECODE_SCALE 1000000000 / 90000
+#define NANOSECOND_SCALE 1000000000L
+#define TIMECODE_SCALE 1000000000L / 90000
struct hb_mux_object_s
{
HB_MUX_COMMON;
- hb_job_t * job;
-
+ hb_job_t * job;
mk_Writer * file;
+ int delay;
};
struct hb_mux_data_s
@@ -202,6 +206,9 @@ static int MKVInit( hb_mux_object_t * m )
mux_data = calloc(1, sizeof( hb_mux_data_t ) );
audio->priv.mux_data = mux_data;
+ if (audio->config.out.delay > m->delay)
+ m->delay = audio->config.out.delay;
+
mux_data->codec = audio->config.out.codec;
memset(track, 0, sizeof(mk_TrackConfig));
@@ -440,14 +447,13 @@ static int MKVMux(hb_mux_object_t *m, hb_mux_data_t *mux_data, hb_buffer_t *buf)
char chapter_name[1024];
hb_chapter_t *chapter_data;
uint64_t timecode = 0;
- ogg_packet *op = NULL;
hb_job_t *job = m->job;
+ // Adjust for audio preroll and scale units
+ timecode = (buf->s.start + m->delay) * TIMECODE_SCALE;
if (mux_data == job->mux_data)
{
/* Video */
- timecode = buf->s.start * TIMECODE_SCALE;
-
if (job->chapter_markers && buf->s.new_chap)
{
// reached chapter N, write marker for chapter N-1
@@ -475,22 +481,6 @@ static int MKVMux(hb_mux_object_t *m, hb_mux_data_t *mux_data, hb_buffer_t *buf)
}
mux_data->prev_chapter_tc = timecode;
}
-
- if (job->vcodec == HB_VCODEC_THEORA)
- {
- /* ughhh, theora is a pain :( */
- op = (ogg_packet *)buf->data;
- op->packet = buf->data + sizeof( ogg_packet );
- if (mk_startFrame(m->file, mux_data->track) < 0)
- {
- hb_error( "Failed to write frame to output file, Disk Full?" );
- *job->die = 1;
- }
- mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
- mk_setFrameFlags(m->file, mux_data->track, timecode, 1, 0);
- hb_buffer_close( &buf );
- return 0;
- }
}
else if (mux_data->subtitle)
{
@@ -500,14 +490,13 @@ static int MKVMux(hb_mux_object_t *m, hb_mux_data_t *mux_data, hb_buffer_t *buf)
*job->die = 1;
}
uint64_t duration;
- timecode = buf->s.start * TIMECODE_SCALE;
- if (buf->s.stop <= buf->s.start)
+ if (buf->s.duration < 0)
{
- duration = 0;
+ duration = 10 * NANOSECOND_SCALE;
}
else
{
- duration = buf->s.stop * TIMECODE_SCALE - timecode;
+ duration = buf->s.duration * TIMECODE_SCALE;
}
mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
mk_setFrameFlags(m->file, mux_data->track, timecode, 1, duration);
@@ -518,22 +507,6 @@ static int MKVMux(hb_mux_object_t *m, hb_mux_data_t *mux_data, hb_buffer_t *buf)
else
{
/* Audio */
- timecode = buf->s.start * TIMECODE_SCALE;
- if (mux_data->codec == HB_ACODEC_VORBIS)
- {
- /* ughhh, vorbis is a pain :( */
- op = (ogg_packet *)buf->data;
- op->packet = buf->data + sizeof( ogg_packet );
- if (mk_startFrame(m->file, mux_data->track))
- {
- hb_error( "Failed to write frame to output file, Disk Full?" );
- *job->die = 1;
- }
- mk_addFrameData(m->file, mux_data->track, op->packet, op->bytes);
- mk_setFrameFlags(m->file, mux_data->track, timecode, 1, 0);
- hb_buffer_close( &buf );
- return 0;
- }
}
if( mk_startFrame(m->file, mux_data->track) < 0)
@@ -690,3 +663,4 @@ hb_mux_object_t * hb_mux_mkv_init( hb_job_t * job )
m->job = job;
return m;
}
+#endif // USE_LIBMKV
diff --git a/libhb/muxmp4.c b/libhb/muxmp4.c
index 66f02d5c7..e544c26be 100644
--- a/libhb/muxmp4.c
+++ b/libhb/muxmp4.c
@@ -7,11 +7,13 @@
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
+#include "hb.h"
+
+#if defined(USE_MP4V2)
+
#include "mp4v2/mp4v2.h"
#include "a52dec/a52.h"
-#include "hb.h"
-
struct hb_mux_object_s
{
HB_MUX_COMMON;
@@ -658,257 +660,24 @@ static int MP4Init( hb_mux_object_t * m )
return 0;
}
-typedef struct stylerecord_s {
- enum style_s {ITALIC, BOLD, UNDERLINE} style;
- uint16_t start;
- uint16_t stop;
- struct stylerecord_s *next;
-} stylerecord;
-
-static void hb_makestylerecord( stylerecord **stack,
- enum style_s style, int start )
-{
- stylerecord *record = calloc( sizeof( stylerecord ), 1 );
-
- if( record )
- {
- record->style = style;
- record->start = start;
- record->next = *stack;
- *stack = record;
- }
-}
-
-static void hb_makestyleatom( stylerecord *record, uint8_t *style)
-{
- uint8_t face = 1;
- hb_deep_log(3, "Made style '%s' from %d to %d",
- record->style == ITALIC ? "Italic" : record->style == BOLD ? "Bold" : "Underline", record->start, record->stop);
-
- switch( record->style )
- {
- case ITALIC:
- face = 2;
- break;
- case BOLD:
- face = 1;
- break;
- case UNDERLINE:
- face = 4;
- break;
- default:
- face = 2;
- break;
- }
-
- style[0] = (record->start >> 8) & 0xff; // startChar
- style[1] = record->start & 0xff;
- style[2] = (record->stop >> 8) & 0xff; // endChar
- style[3] = record->stop & 0xff;
- style[4] = (1 >> 8) & 0xff; // font-ID
- style[5] = 1 & 0xff;
- style[6] = face; // face-style-flags: 1 bold; 2 italic; 4 underline
- style[7] = 24; // font-size
- style[8] = 255; // r
- style[9] = 255; // g
- style[10] = 255; // b
- style[11] = 255; // a
-
-}
-
-/*
- * Copy the input to output removing markup and adding markup to the style
- * atom where appropriate.
- */
-static void hb_muxmp4_process_subtitle_style( uint8_t *input,
- uint8_t *output,
- uint8_t *style, uint16_t *stylesize )
-{
- uint8_t *reader = input;
- uint8_t *writer = output;
- uint8_t stylecount = 0;
- uint16_t utf8_count = 0; // utf8 count from start of subtitle
- stylerecord *stylestack = NULL;
- stylerecord *oldrecord = NULL;
-
- while(*reader != '\0') {
- if( ( *reader & 0xc0 ) == 0x80 )
- {
- /*
- * Track the utf8_count when doing markup so that we get the tx3g stops
- * based on UTF8 chr counts rather than bytes.
- */
- utf8_count++;
- hb_deep_log( 3, "MuxMP4: Counted %d UTF-8 chrs within subtitle so far",
- utf8_count);
- }
- if (*reader == '<') {
- /*
- * possible markup, peek at the next chr
- */
- switch(*(reader+1)) {
- case 'i':
- if (*(reader+2) == '>') {
- reader += 3;
- hb_makestylerecord(&stylestack, ITALIC, (writer - output - utf8_count));
- } else {
- *writer++ = *reader++;
- }
- break;
- case 'b':
- if (*(reader+2) == '>') {
- reader += 3;
- hb_makestylerecord(&stylestack, BOLD, (writer - output - utf8_count));
- } else {
- *writer++ = *reader++;
- }
- break;
- case 'u':
- if (*(reader+2) == '>') {
- reader += 3;
- hb_makestylerecord(&stylestack, UNDERLINE, (writer - output - utf8_count));
- } else {
- *writer++ = *reader++;
- }
- break;
- case '/':
- switch(*(reader+2)) {
- case 'i':
- if (*(reader+3) == '>') {
- /*
- * Check whether we then immediately start more markup of the same type, if so then
- * lets not close it now and instead continue this markup.
- */
- if ((*(reader+4) && *(reader+4) == '<') &&
- (*(reader+5) && *(reader+5) == 'i') &&
- (*(reader+6) && *(reader+6) == '>')) {
- /*
- * Opening italics right after, so don't close off these italics.
- */
- hb_deep_log(3, "Joining two sets of italics");
- reader += (4 + 3);
- continue;
- }
-
-
- if ((*(reader+4) && *(reader+4) == ' ') &&
- (*(reader+5) && *(reader+5) == '<') &&
- (*(reader+6) && *(reader+6) == 'i') &&
- (*(reader+7) && *(reader+7) == '>')) {
- /*
- * Opening italics right after, so don't close off these italics.
- */
- hb_deep_log(3, "Joining two sets of italics (plus space)");
- reader += (4 + 4);
- *writer++ = ' ';
- continue;
- }
- if (stylestack && stylestack->style == ITALIC) {
- uint8_t style_record[12];
- stylestack->stop = writer - output - utf8_count;
- hb_makestyleatom(stylestack, style_record);
-
- memcpy(style + 10 + (12 * stylecount), style_record, 12);
- stylecount++;
-
- oldrecord = stylestack;
- stylestack = stylestack->next;
- free(oldrecord);
- } else {
- hb_error("Mismatched Subtitle markup '%s'", input);
- }
- reader += 4;
- } else {
- *writer++ = *reader++;
- }
- break;
- case 'b':
- if (*(reader+3) == '>') {
- if (stylestack && stylestack->style == BOLD) {
- uint8_t style_record[12];
- stylestack->stop = writer - output - utf8_count;
- hb_makestyleatom(stylestack, style_record);
-
- memcpy(style + 10 + (12 * stylecount), style_record, 12);
- stylecount++;
- oldrecord = stylestack;
- stylestack = stylestack->next;
- free(oldrecord);
- } else {
- hb_error("Mismatched Subtitle markup '%s'", input);
- }
-
- reader += 4;
- } else {
- *writer++ = *reader++;
- }
- break;
- case 'u':
- if (*(reader+3) == '>') {
- if (stylestack && stylestack->style == UNDERLINE) {
- uint8_t style_record[12];
- stylestack->stop = writer - output - utf8_count;
- hb_makestyleatom(stylestack, style_record);
-
- memcpy(style + 10 + (12 * stylecount), style_record, 12);
- stylecount++;
-
- oldrecord = stylestack;
- stylestack = stylestack->next;
- free(oldrecord);
- } else {
- hb_error("Mismatched Subtitle markup '%s'", input);
- }
- reader += 4;
- } else {
- *writer++ = *reader++;
- }
- break;
- default:
- *writer++ = *reader++;
- break;
- }
- break;
- default:
- *writer++ = *reader++;
- break;
- }
- } else if (*reader == '\r') {
- // skip '\r' and replace with '\n' if necessary
- if (*(++reader) != '\n') {
- *writer++ = '\n';
- }
- } else {
- *writer++ = *reader++;
- }
- }
- *writer = '\0';
-
- if( stylecount )
- {
- *stylesize = 10 + ( stylecount * 12 );
-
- memcpy( style + 4, "styl", 4);
-
- style[0] = 0;
- style[1] = 0;
- style[2] = (*stylesize >> 8) & 0xff;
- style[3] = *stylesize & 0xff;
- style[8] = (stylecount >> 8) & 0xff;
- style[9] = stylecount & 0xff;
-
- }
-
-}
-
static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
hb_buffer_t * buf )
{
hb_job_t * job = m->job;
- int64_t duration;
+ int64_t duration, stop = -1;
int64_t offset = 0;
hb_buffer_t *tmp;
+ if (buf->s.duration >= 0)
+ {
+ stop = buf->s.start + buf->s.duration;
+ }
+ else if (mux_data->subtitle)
+ {
+ buf->s.duration = 10 * 90000;
+ stop = buf->s.start + buf->s.duration;
+ }
+
if( mux_data == job->mux_data )
{
/* Video */
@@ -980,7 +749,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
}
else
{
- duration = buf->s.stop - m->sum_dur;
+ duration = stop - m->sum_dur;
// Due to how libx264 generates DTS, it's possible for the
// above calculation to be negative.
//
@@ -1016,7 +785,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
// We're getting the frames in decode order but the timestamps are
// for presentation so we have to use durations and effectively
// compute a DTS.
- duration = buf->s.stop - buf->s.start;
+ duration = buf->s.duration;
}
if ( duration <= 0 )
@@ -1027,7 +796,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
try to fix the error so that the file will still be playable. */
hb_log("MP4Mux: illegal duration %"PRId64", start %"PRId64","
"stop %"PRId64", sum_dur %"PRId64,
- duration, buf->s.start, buf->s.stop, m->sum_dur );
+ duration, buf->s.start, stop, m->sum_dur );
/* we don't know when the next frame starts so we can't pick a
valid duration for this one. we pick something "short"
(roughly 1/3 of an NTSC frame time) to take time from
@@ -1111,11 +880,11 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
code should coalesce overlapping subtitle lines. */
if( buf->s.start < mux_data->sum_dur )
{
- if ( buf->s.stop - mux_data->sum_dur > 90*500 )
+ if ( stop - mux_data->sum_dur > 90*500 )
{
hb_log("MP4Mux: shortening overlapping subtitle, "
"start %"PRId64", stop %"PRId64", sum_dur %"PRId64,
- buf->s.start, buf->s.stop, m->sum_dur);
+ buf->s.start, stop, m->sum_dur);
buf->s.start = mux_data->sum_dur;
}
}
@@ -1123,20 +892,10 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
{
hb_log("MP4Mux: skipping overlapping subtitle, "
"start %"PRId64", stop %"PRId64", sum_dur %"PRId64,
- buf->s.start, buf->s.stop, m->sum_dur);
+ buf->s.start, stop, m->sum_dur);
}
else
{
- int64_t duration;
-
- if( buf->s.start < 0 )
- buf->s.start = mux_data->sum_dur;
-
- if( buf->s.stop < 0 )
- duration = 90000L * 10;
- else
- duration = buf->s.stop - buf->s.start;
-
/* Write an empty sample */
if ( mux_data->sum_dur < buf->s.start )
{
@@ -1172,9 +931,9 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
buffersize = strlen((char*)buffer);
- hb_deep_log(3, "MuxMP4:Sub:%fs:%"PRId64":%"PRId64":%"PRId64": %s",
- (float)buf->s.start / 90000, buf->s.start, buf->s.stop,
- duration, buffer);
+ hb_deep_log(3, "MuxMP4:Sub:%fs:%"PRId64":%"PRId64":%f: %s",
+ (float)buf->s.start / 90000, buf->s.start, stop,
+ buf->s.duration, buffer);
/* Write the subtitle sample */
memcpy( output + 2, buffer, buffersize );
@@ -1186,7 +945,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
mux_data->track,
output,
buffersize + stylesize + 2,
- duration,
+ buf->s.duration,
0,
1 ))
{
@@ -1194,21 +953,11 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
*job->die = 1;
}
- mux_data->sum_dur += duration;
+ mux_data->sum_dur += buf->s.duration;
}
}
else if( mux_data->sub_format == PICTURESUB )
{
- int64_t duration;
-
- if( buf->s.start < 0 )
- buf->s.start = mux_data->sum_dur;
-
- if( buf->s.stop < 0 )
- duration = 90000L * 10;
- else
- duration = buf->s.stop - buf->s.start;
-
/* Write an empty sample */
if ( mux_data->sum_dur < buf->s.start )
{
@@ -1230,7 +979,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
mux_data->track,
buf->data,
buf->size,
- duration,
+ buf->s.duration,
0,
1 ))
{
@@ -1238,7 +987,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
*job->die = 1;
}
- mux_data->sum_dur += duration;
+ mux_data->sum_dur += buf->s.duration;
}
}
else
@@ -1266,6 +1015,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
static int MP4End( hb_mux_object_t * m )
{
hb_job_t * job = m->job;
+ int i;
if (m->file != MP4_INVALID_FILE_HANDLE)
{
@@ -1295,19 +1045,32 @@ static int MP4End( hb_mux_object_t * m )
if ( job->config.h264.init_delay )
{
- // Insert track edit to get A/V back in sync. The edit amount is
- // the init_delay.
- int64_t edit_amt = job->config.h264.init_delay;
- MP4AddTrackEdit(m->file, 1, MP4_INVALID_EDIT_ID, edit_amt,
- MP4GetTrackDuration(m->file, 1), 0);
- if ( m->job->chapter_markers )
- {
- // apply same edit to chapter track to keep it in sync with video
- MP4AddTrackEdit(m->file, m->chapter_track, MP4_INVALID_EDIT_ID,
- edit_amt,
- MP4GetTrackDuration(m->file, m->chapter_track), 0);
- }
- }
+ // Insert track edit to get A/V back in sync. The edit amount is
+ // the init_delay.
+ int64_t edit_amt = job->config.h264.init_delay;
+ MP4AddTrackEdit(m->file, 1, MP4_INVALID_EDIT_ID, edit_amt,
+ MP4GetTrackDuration(m->file, 1), 0);
+ if ( m->job->chapter_markers )
+ {
+ // apply same edit to chapter track to keep it in sync with video
+ MP4AddTrackEdit(m->file, m->chapter_track, MP4_INVALID_EDIT_ID,
+ edit_amt,
+ MP4GetTrackDuration(m->file, m->chapter_track), 0);
+ }
+ }
+
+ // Check for audio preroll and add edit entries for audio
+ for( i = 0; i < hb_list_count( job->list_audio ); i++ )
+ {
+ hb_audio_t *audio = hb_list_item( job->list_audio, i );
+ hb_mux_data_t *mux_data = audio->priv.mux_data;
+ if (audio->config.out.delay > 0)
+ {
+ int64_t edit_amt = audio->config.out.delay;
+ MP4AddTrackEdit(m->file, mux_data->track, MP4_INVALID_EDIT_ID,
+ edit_amt, MP4GetTrackDuration(m->file, 1), 0);
+ }
+ }
/*
* Write the MP4 iTunes metadata if we have any metadata
@@ -1413,3 +1176,4 @@ hb_mux_object_t * hb_mux_mp4_init( hb_job_t * job )
return m;
}
+#endif // USE_MP4V2
diff --git a/libhb/sync.c b/libhb/sync.c
index a5158f738..e3d44694b 100644
--- a/libhb/sync.c
+++ b/libhb/sync.c
@@ -337,8 +337,14 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
}
hb_lock( pv->common->mutex );
- // Tell the audio threads what must be dropped
- pv->common->audio_pts_thresh = next_start + pv->common->video_pts_slip;
+ if (job->frame_to_start > 0)
+ {
+ // When doing frame based p-to-p we must update the audio
+ // start point with each frame skipped.
+ //
+ // Tell the audio threads what must be dropped
+ pv->common->audio_pts_thresh = next->s.start;
+ }
hb_cond_broadcast( pv->common->next_frame );
hb_unlock( pv->common->mutex );
@@ -553,7 +559,7 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
{
- int64_t sub_start, sub_stop, duration;
+ int64_t sub_start, sub_stop, sub_dts, duration;
subtitle = hb_list_item( job->list_subtitle, i );
@@ -563,6 +569,10 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
{
hb_lock( pv->common->mutex );
sub_start = sub->s.start - pv->common->video_pts_slip;
+ if (sub->s.renderOffset != -1)
+ sub_dts = sub->s.renderOffset - pv->common->video_pts_slip;
+ else
+ sub_dts = -1;
hb_unlock( pv->common->mutex );
if (sub->s.stop == -1)
@@ -595,6 +605,7 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
// Need to re-write subtitle timestamps to account
// for any slippage.
sub_stop = -1;
+ duration = -1;
if ( sub->s.stop != -1 )
{
duration = sub->s.stop - sub->s.start;
@@ -602,7 +613,9 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
sub->s.start = sub_start;
+ sub->s.renderOffset = sub_dts;
sub->s.stop = sub_stop;
+ sub->s.duration = duration;
hb_fifo_push( subtitle->fifo_out, sub );
}
@@ -626,6 +639,8 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
sync->cur = cur = next;
cur->sub = NULL;
cur->s.start -= pv->common->video_pts_slip;
+ if (cur->s.renderOffset != -1)
+ cur->s.renderOffset -= pv->common->video_pts_slip;
cur->s.stop -= pv->common->video_pts_slip;
sync->pts_skip = 0;
if ( duration <= 0 )
@@ -758,9 +773,15 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_unlock( pv->common->mutex );
}
- /* Wait for start frame if doing point-to-point */
+ // Wait for start frame if doing point-to-point
+ //
+ // When doing p-to-p, video leads the way. The video thead will set
+ // start_found when we have reached the start point.
+ //
+ // When doing frame based p-to-p, as each video frame is processed
+ // it advances audio_pts_thresh which informs us how much audio must
+ // be dropped.
hb_lock( pv->common->mutex );
- start = buf->s.start - pv->common->audio_pts_slip;
while ( !pv->common->start_found && !*w->done )
{
if ( pv->common->audio_pts_thresh < 0 )
@@ -776,6 +797,12 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_unlock( pv->common->mutex );
return HB_WORK_OK;
}
+
+ // We should only get here when doing frame based p-to-p.
+ // In frame based p-to-p, the video sync thread updates
+ // audio_pts_thresh as it discards frames. So wait here
+ // until the current audio frame needs to be discarded
+ // or start point is found.
while ( !pv->common->start_found &&
buf->s.start >= pv->common->audio_pts_thresh && !*w->done )
{
@@ -804,15 +831,20 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
}
}
- start = buf->s.start - pv->common->audio_pts_slip;
}
- if ( start < 0 )
+ start = buf->s.start - pv->common->audio_pts_slip;
+ hb_unlock( pv->common->mutex );
+
+ // When doing p-to-p, video determines when the start point has been
+ // found. Since audio and video are processed asynchronously, there
+ // may yet be audio packets in the pipe that are before the start point.
+ // These packets will have negative start times after applying
+ // audio_pts_slip.
+ if (start < 0)
{
- hb_buffer_close( &buf );
- hb_unlock( pv->common->mutex );
+ hb_buffer_close(&buf);
return HB_WORK_OK;
}
- hb_unlock( pv->common->mutex );
if( job->frame_to_stop && pv->common->count_frames >= job->frame_to_stop )
{
diff --git a/libhb/work.c b/libhb/work.c
index ceab14b1f..df22d791e 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -210,6 +210,7 @@ void hb_display_job_info(hb_job_t *job)
hb_log(" + container: %s", hb_container_get_name(job->mux));
switch (job->mux)
{
+ case HB_MUX_AV_MP4:
case HB_MUX_MP4V2:
if (job->largeFileSize)
hb_log(" + 64-bit chunk offsets");