summaryrefslogtreecommitdiffstats
path: root/libhb
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2011-03-11 22:40:30 +0000
committerjstebbins <[email protected]>2011-03-11 22:40:30 +0000
commitd68cb8a25201663f832307990b2a98cc60d8ab14 (patch)
tree936279196cd07e6d68b19b6a79de61ec1e6acfbe /libhb
parent4aaed20c697c1a7e8fe7e039cf1949a320758286 (diff)
Add parameter parsing and b-frame support to ffmpeg mpeg-4 encoder
The cli will now accept ':' separated parameters using the '-x' option for ffmpeg mpeg-4. The linux gui has an entry box on the advanced tab to add options. The option keys and values are the same as what the ffmpeg command line allows. Calculation of DTS timestamps was added to encavcodec.c in order to allow out of order b-frames. The algorithm is similar to what x264 uses. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3839 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'libhb')
-rw-r--r--libhb/common.h6
-rw-r--r--libhb/decavcodec.c3
-rw-r--r--libhb/encavcodec.c339
-rw-r--r--libhb/encx264.c8
-rw-r--r--libhb/hbffmpeg.h1
-rw-r--r--libhb/muxmkv.c5
-rw-r--r--libhb/muxmp4.c12
-rw-r--r--libhb/work.c4
8 files changed, 320 insertions, 58 deletions
diff --git a/libhb/common.h b/libhb/common.h
index 031b8b642..fc7b13916 100644
--- a/libhb/common.h
+++ b/libhb/common.h
@@ -220,8 +220,8 @@ struct hb_job_s
vrate, vrate_base: output framerate is vrate / vrate_base
cfr: 0 (vfr), 1 (cfr), 2 (pfr) [see render.c]
pass: 0, 1 or 2 (or -1 for scan)
- x264opts: string of extra x264 options
- areBframes: boolean to note if b-frames are included in x264opts */
+ advanced_opts: string of extra advanced encoder options
+ areBframes: boolean to note if b-frames are included in advanced_opts */
#define HB_VCODEC_MASK 0x0000FF
#define HB_VCODEC_FFMPEG 0x000001
#define HB_VCODEC_X264 0x000002
@@ -237,7 +237,7 @@ struct hb_job_s
int vfr;
int cfr;
int pass;
- char *x264opts;
+ char *advanced_opts;
int areBframes;
int color_matrix;
diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c
index df5575d98..5fe5f8e81 100644
--- a/libhb/decavcodec.c
+++ b/libhb/decavcodec.c
@@ -1684,8 +1684,7 @@ static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
// if the packet has a timestamp use it if we don't have a timestamp yet
// or if there's been a timing discontinuity of more than 100ms.
- if ( in->start >= 0 &&
- ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
+ if ( in->start >= 0 )
{
pv->pts_next = in->start;
}
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c
index 07fc748a8..fd95f64b1 100644
--- a/libhb/encavcodec.c
+++ b/libhb/encavcodec.c
@@ -7,11 +7,37 @@
#include "hb.h"
#include "hbffmpeg.h"
+/*
+ * The frame info struct remembers information about each frame across calls
+ * to avcodec_encode_video. Since frames are uniquely identified by their
+ * frame number, we use this as an index.
+ *
+ * The size of the array is chosen so that two frames can't use the same
+ * slot during the encoder's max frame delay (set by the standard as 16
+ * frames) and so that, up to some minimum frame rate, frames are guaranteed
+ * to map to * different slots.
+ */
+#define FRAME_INFO_SIZE 32
+#define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
+
struct hb_work_private_s
{
hb_job_t * job;
AVCodecContext * context;
FILE * file;
+
+ int frameno_in;
+ int frameno_out;
+ hb_buffer_t * delay_head;
+ hb_buffer_t * delay_tail;
+
+ int64_t dts_delay;
+
+ struct {
+ int64_t start;
+ int64_t stop;
+ int64_t renderOffset;
+ } frame_info[FRAME_INFO_SIZE];
};
int encavcodecInit( hb_work_object_t *, hb_job_t * );
@@ -44,39 +70,11 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
hb_log( "hb_work_encavcodec_init: avcodec_find_encoder "
"failed" );
}
- context = avcodec_alloc_context();
- if( job->vquality < 0.0 )
- {
- /* Rate control */
- context->bit_rate = 1000 * job->vbitrate;
- context->bit_rate_tolerance = 10 * context->bit_rate;
- }
- else
- {
- /* Constant quantizer */
- // These settings produce better image quality than
- // what was previously used
- context->flags |= CODEC_FLAG_QSCALE;
- if (job->vquality < 1.0)
- {
- float vquality;
- vquality = 31 - job->vquality * 31;
- // A value of 0 has undefined behavior
- // and ffmpeg qp has integral increments
- if (vquality < 1.0)
- vquality = 1.0;
- context->global_quality = FF_QP2LAMBDA * vquality + 0.5;
- }
- else
- {
- context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
- }
- context->mb_decision = 1;
- hb_log( "encavcodec: encoding at constant quantizer %d",
- context->global_quality );
- }
- context->width = job->width;
- context->height = job->height;
+ context = avcodec_alloc_context3( codec );
+
+ // Set things in context that we will allow the user to
+ // override with advanced settings.
+ context->thread_count = ( hb_get_cpu_count() * 3 / 2 );
if( job->pass == 2 )
{
@@ -89,6 +87,13 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
rate_num = job->vrate_base;
rate_den = job->vrate;
}
+
+ // If the rate_den 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 rate_num.
if (rate_den == 27000000)
{
int ii;
@@ -114,6 +119,92 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
}
context->time_base = (AVRational) { rate_num, rate_den };
context->gop_size = 10 * (int)( (double)job->vrate / (double)job->vrate_base + 0.5 );
+
+ /*
+ This section passes the string advanced_opts to avutil for parsing
+ into an AVCodecContext.
+
+ The string is set up like this:
+ option1=value1:option2=value2
+
+ So, you have to iterate through based on the colons, and then put
+ the left side of the equals sign in "name" and the right side into
+ "value." Then you hand those strings off to avutil for interpretation.
+ */
+ if( job->advanced_opts != NULL && *job->advanced_opts != '\0' )
+ {
+ char *opts, *opts_start;
+
+ opts = opts_start = strdup(job->advanced_opts);
+
+ if( opts_start )
+ {
+ while( *opts )
+ {
+ char *name = opts;
+ char *value;
+ int ret;
+
+ opts += strcspn( opts, ":" );
+ if( *opts )
+ {
+ *opts = 0;
+ opts++;
+ }
+
+ value = strchr( name, '=' );
+ if( value )
+ {
+ *value = 0;
+ value++;
+ }
+
+ /* Here's where the strings are passed to avutil for parsing. */
+ ret = av_set_string3( context, name, value, 1, NULL );
+
+ /* Let avutil sanity check the options for us*/
+ if( ret == AVERROR(ENOENT) )
+ hb_log( "avcodec options: Unknown option %s", name );
+ if( ret == AVERROR(EINVAL) )
+ hb_log( "avcodec options: Bad argument %s=%s", name, value ? value : "(null)" );
+ }
+ }
+ free(opts_start);
+ }
+
+ // Now set the things in context that we don't want to allow
+ // the user to override.
+ if( job->vquality < 0.0 )
+ {
+ /* Rate control */
+ context->bit_rate = 1000 * job->vbitrate;
+ context->bit_rate_tolerance = 10 * context->bit_rate;
+ }
+ else
+ {
+ /* Constant quantizer */
+ // These settings produce better image quality than
+ // what was previously used
+ context->flags |= CODEC_FLAG_QSCALE;
+ if (job->vquality < 1.0)
+ {
+ float vquality;
+ vquality = 31 - job->vquality * 31;
+ // A value of 0 has undefined behavior
+ // and ffmpeg qp has integral increments
+ if (vquality < 1.0)
+ vquality = 1.0;
+ context->global_quality = FF_QP2LAMBDA * vquality + 0.5;
+ }
+ else
+ {
+ context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
+ }
+ hb_log( "encavcodec: encoding at constant quantizer %d",
+ context->global_quality );
+ }
+ context->width = job->width;
+ context->height = job->height;
context->pix_fmt = PIX_FMT_YUV420P;
if( job->anamorphic.mode )
@@ -170,17 +261,15 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
}
pv->context = context;
+ if ( context->has_b_frames )
+ {
+ job->areBframes = 1;
+ }
if( ( job->mux & HB_MUX_MP4 ) && job->pass != 1 )
{
-#if 0
- /* Hem hem */
- w->config->mpeg4.length = 15;
- memcpy( w->config->mpeg4.bytes, context->extradata + 15, 15 );
-#else
w->config->mpeg4.length = context->extradata_size;
memcpy( w->config->mpeg4.bytes, context->extradata,
context->extradata_size );
-#endif
}
return 0;
@@ -209,6 +298,109 @@ void encavcodecClose( hb_work_object_t * w )
w->private_data = NULL;
}
+/*
+ * see comments in definition of 'frame_info' in pv struct for description
+ * of what these routines are doing.
+ */
+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->start;
+ pv->frame_info[i].stop = in->stop;
+}
+
+static int64_t get_frame_start( hb_work_private_t * pv, int64_t frameno )
+{
+ int i = frameno & FRAME_INFO_MASK;
+ return pv->frame_info[i].start;
+}
+
+static int64_t get_frame_stop( hb_work_private_t * pv, int64_t frameno )
+{
+ int i = frameno & FRAME_INFO_MASK;
+ return pv->frame_info[i].stop;
+}
+
+static void compute_dts_offset( hb_work_private_t * pv, hb_buffer_t * buf )
+{
+ if ( pv->job->areBframes )
+ {
+ if ( ( pv->frameno_in - 1 ) == pv->job->areBframes )
+ {
+ pv->dts_delay = buf->start;
+ pv->job->config.h264.init_delay = pv->dts_delay;
+ }
+ }
+}
+
+// Generate DTS by rearranging PTS in this sequence:
+// pts0 - delay, pts1 - delay, pts2 - delay, pts1, pts2, pts3...
+//
+// Where pts0 - ptsN are in decoded monotonically increasing presentation
+// order and delay == pts1 (1 being the number of frames the decoder must
+// delay before it has suffecient information to decode). The number of
+// frames to delay is set by job->areBframes, so it is configurable.
+// This guarantees that DTS <= PTS for any frame.
+//
+// This is similar to how x264 generates DTS
+static hb_buffer_t * process_delay_list( hb_work_private_t * pv, hb_buffer_t * buf )
+{
+ if ( pv->job->areBframes )
+ {
+ // Has dts_delay been set yet?
+ if ( pv->frameno_in <= pv->job->areBframes )
+ {
+ // dts_delay not yet set. queue up buffers till it is set.
+ if ( pv->delay_tail == NULL )
+ {
+ pv->delay_head = pv->delay_tail = buf;
+ }
+ else
+ {
+ pv->delay_tail->next = buf;
+ pv->delay_tail = buf;
+ }
+ return NULL;
+ }
+
+ // We have dts_delay. Apply it to any queued buffers renderOffset
+ // and return all queued buffers.
+ if ( pv->delay_tail == NULL && buf != NULL )
+ {
+ pv->frameno_out++;
+ // Use the cached frame info to get the start time of Nth frame
+ // Note that start Nth frame != start time this buffer since the
+ // output buffers have rearranged start times.
+ int64_t start = get_frame_start( pv, pv->frameno_out );
+ buf->renderOffset = start - pv->dts_delay;
+ return buf;
+ }
+ else
+ {
+ pv->delay_tail->next = buf;
+ buf = pv->delay_head;
+ while ( buf )
+ {
+ pv->frameno_out++;
+ // Use the cached frame info to get the start time of Nth frame
+ // Note that start Nth frame != start time this buffer since the
+ // output buffers have rearranged start times.
+ int64_t start = get_frame_start( pv, pv->frameno_out );
+ buf->renderOffset = start - pv->dts_delay;
+ }
+ buf = pv->delay_head;
+ pv->delay_head = pv->delay_tail = NULL;
+ return buf;
+ }
+ }
+ else if ( buf )
+ {
+ buf->renderOffset = buf->start - pv->dts_delay;
+ return buf;
+ }
+ return NULL;
+}
+
/***********************************************************************
* Work
***********************************************************************
@@ -241,15 +433,78 @@ int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
// doesn't do the trick. It must be set in the AVFrame.
frame->quality = pv->context->global_quality;
+ // Bizarro ffmpeg appears to require the input AVFrame.pts to be
+ // set to a frame number. Setting it to an actual pts causes
+ // jerky video.
+ // frame->pts = in->start;
+ frame->pts = ++pv->frameno_in;
+
+ // Remember info about this frame that we need to pass across
+ // the avcodec_encode_video call (since it reorders frames).
+ save_frame_info( pv, in );
+ compute_dts_offset( pv, in );
+
if ( pv->context->codec )
{
/* Should be way too large */
buf = hb_video_buffer_init( job->width, job->height );
buf->size = avcodec_encode_video( pv->context, buf->data, buf->alloc,
frame );
- buf->start = in->start;
- buf->stop = in->stop;
- buf->frametype = pv->context->coded_frame->key_frame ? HB_FRAME_KEY : HB_FRAME_REF;
+ if ( buf->size <= 0 )
+ {
+ hb_buffer_close( &buf );
+ }
+ else
+ {
+ int64_t frameno = pv->context->coded_frame->pts;
+ buf->start = get_frame_start( pv, frameno );
+ buf->stop = get_frame_stop( pv, frameno );
+ switch ( pv->context->coded_frame->pict_type )
+ {
+ case FF_P_TYPE:
+ {
+ buf->frametype = HB_FRAME_P;
+ } break;
+
+ case FF_B_TYPE:
+ {
+ buf->frametype = HB_FRAME_B;
+ } break;
+
+ case FF_S_TYPE:
+ {
+ buf->frametype = HB_FRAME_P;
+ } break;
+
+ case FF_SP_TYPE:
+ {
+ buf->frametype = HB_FRAME_P;
+ } break;
+
+ case FF_BI_TYPE:
+ case FF_SI_TYPE:
+ case FF_I_TYPE:
+ {
+ if ( pv->context->coded_frame->key_frame )
+ {
+ buf->frametype = HB_FRAME_IDR;
+ }
+ else
+ {
+ buf->frametype = HB_FRAME_I;
+ }
+ } break;
+
+ default:
+ {
+ if ( pv->context->coded_frame->key_frame )
+ buf->frametype = HB_FRAME_KEY;
+ else
+ buf->frametype = HB_FRAME_REF;
+ } break;
+ }
+ buf = process_delay_list( pv, buf );
+ }
}
else
{
diff --git a/libhb/encx264.c b/libhb/encx264.c
index 2c64321ba..67ea45b1a 100644
--- a/libhb/encx264.c
+++ b/libhb/encx264.c
@@ -133,7 +133,7 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
param.i_log_level = X264_LOG_INFO;
/*
- This section passes the string x264opts to libx264 for parsing into
+ This section passes the string advanced_opts to libx264 for parsing into
parameter names and values.
The string is set up like this:
@@ -147,11 +147,11 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
Merritt implemented in the Mplayer/Mencoder project.
*/
- if( job->x264opts != NULL && *job->x264opts != '\0' )
+ if( job->advanced_opts != NULL && *job->advanced_opts != '\0' )
{
char *x264opts, *x264opts_start;
- x264opts = x264opts_start = strdup(job->x264opts);
+ x264opts = x264opts_start = strdup(job->advanced_opts);
while( x264opts_start && *x264opts )
{
@@ -176,7 +176,7 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
/* Here's where the strings are passed to libx264 for parsing. */
ret = x264_param_parse( &param, name, value );
- /* Let x264 sanity check the options for us*/
+ /* Let x264 sanity check the options for us*/
if( ret == X264_PARAM_BAD_NAME )
hb_log( "x264 options: Unknown suboption %s", name );
if( ret == X264_PARAM_BAD_VALUE )
diff --git a/libhb/hbffmpeg.h b/libhb/hbffmpeg.h
index 097fae250..205bd0f64 100644
--- a/libhb/hbffmpeg.h
+++ b/libhb/hbffmpeg.h
@@ -2,6 +2,7 @@
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
+#include "libavcodec/opt.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
diff --git a/libhb/muxmkv.c b/libhb/muxmkv.c
index f169e3de1..63f3e94cd 100644
--- a/libhb/muxmkv.c
+++ b/libhb/muxmkv.c
@@ -107,6 +107,8 @@ static int MKVInit( hb_mux_object_t * m )
track->codecID = MK_VCODEC_MP4ASP;
track->codecPrivate = job->config.mpeg4.bytes;
track->codecPrivateSize = job->config.mpeg4.length;
+ if (job->areBframes)
+ track->minCache = 1;
break;
case HB_VCODEC_THEORA:
{
@@ -448,7 +450,8 @@ static int MKVMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
}
mk_addFrameData(m->file, mux_data->track, buf->data, buf->size);
mk_setFrameFlags(m->file, mux_data->track, timecode,
- ((job->vcodec == HB_VCODEC_X264 &&
+ (((job->vcodec == HB_VCODEC_X264 ||
+ job->vcodec == HB_VCODEC_FFMPEG) &&
mux_data == job->mux_data) ?
(buf->frametype == HB_FRAME_IDR) :
((buf->frametype & HB_FRAME_KEY) != 0)), 0 );
diff --git a/libhb/muxmp4.c b/libhb/muxmp4.c
index 7cd4e0fcf..9387d6499 100644
--- a/libhb/muxmp4.c
+++ b/libhb/muxmp4.c
@@ -886,7 +886,8 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
{
/* Video */
- if( job->vcodec == HB_VCODEC_X264 )
+ if( job->vcodec == HB_VCODEC_X264 ||
+ job->vcodec == HB_VCODEC_FFMPEG )
{
if ( buf && buf->start < buf->renderOffset )
{
@@ -905,7 +906,8 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
if ( !buf )
return 0;
- if( job->vcodec == HB_VCODEC_X264 )
+ if( job->vcodec == HB_VCODEC_X264 ||
+ job->vcodec == HB_VCODEC_FFMPEG )
{
// x264 supplies us with DTS, so offset is PTS - DTS
offset = buf->start - buf->renderOffset;
@@ -942,7 +944,8 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
}
}
- if( job->vcodec == HB_VCODEC_X264 )
+ if( job->vcodec == HB_VCODEC_X264 ||
+ job->vcodec == HB_VCODEC_FFMPEG )
{
// x264 supplies us with DTS
if ( m->delay_buf )
@@ -1014,7 +1017,8 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
}
/* Here's where the sample actually gets muxed. */
- if( job->vcodec == HB_VCODEC_X264 && mux_data == job->mux_data )
+ if( ( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_FFMPEG )
+ && mux_data == job->mux_data )
{
/* Compute dependency flags.
*
diff --git a/libhb/work.c b/libhb/work.c
index a5f0e7418..39d926d3c 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -270,8 +270,8 @@ void hb_display_job_info( hb_job_t * job )
case HB_VCODEC_X264:
hb_log( " + encoder: x264" );
- if( job->x264opts != NULL && *job->x264opts != '\0' )
- hb_log( " + options: %s", job->x264opts);
+ if( job->advanced_opts != NULL && *job->advanced_opts != '\0' )
+ hb_log( " + options: %s", job->advanced_opts);
break;
case HB_VCODEC_THEORA: