|
|
|
@ -25,12 +25,14 @@ |
|
|
|
|
* @author Adam Thayer (krevnik@comcast.net) |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include <xvid.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include "avcodec.h" |
|
|
|
|
#include <xvid.h> |
|
|
|
|
|
|
|
|
|
#include "libavutil/cpu.h" |
|
|
|
|
#include "libavutil/intreadwrite.h" |
|
|
|
|
#include "libavutil/mathematics.h" |
|
|
|
|
|
|
|
|
|
#include "avcodec.h" |
|
|
|
|
#include "libxvid.h" |
|
|
|
|
#include "mpegvideo.h" |
|
|
|
|
|
|
|
|
@ -92,8 +94,8 @@ struct xvid_ff_pass1 { |
|
|
|
|
* @param handle Private context handle |
|
|
|
|
* @return Returns XVID_ERR_xxxx on failure, or 0 on success. |
|
|
|
|
*/ |
|
|
|
|
static int xvid_ff_2pass_create(xvid_plg_create_t * param, |
|
|
|
|
void ** handle) { |
|
|
|
|
static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle) |
|
|
|
|
{ |
|
|
|
|
struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *) param->param; |
|
|
|
|
char *log = x->context->twopassbuffer; |
|
|
|
|
|
|
|
|
@ -124,7 +126,8 @@ static int xvid_ff_2pass_create(xvid_plg_create_t * param, |
|
|
|
|
* @return Returns 0, success guaranteed |
|
|
|
|
*/ |
|
|
|
|
static int xvid_ff_2pass_destroy(struct xvid_context *ref, |
|
|
|
|
xvid_plg_destroy_t *param) { |
|
|
|
|
xvid_plg_destroy_t *param) |
|
|
|
|
{ |
|
|
|
|
/* Currently cannot think of anything to do on destruction */ |
|
|
|
|
/* Still, the framework should be here for reference/use */ |
|
|
|
|
if (ref->twopassbuffer) |
|
|
|
@ -140,7 +143,8 @@ static int xvid_ff_2pass_destroy(struct xvid_context *ref, |
|
|
|
|
* @return Returns 0, success guaranteed |
|
|
|
|
*/ |
|
|
|
|
static int xvid_ff_2pass_before(struct xvid_context *ref, |
|
|
|
|
xvid_plg_data_t *param) { |
|
|
|
|
xvid_plg_data_t *param) |
|
|
|
|
{ |
|
|
|
|
int motion_remove; |
|
|
|
|
int motion_replacements; |
|
|
|
|
int vop_remove; |
|
|
|
@ -183,7 +187,8 @@ static int xvid_ff_2pass_before(struct xvid_context *ref, |
|
|
|
|
* @return Returns XVID_ERR_xxxx on failure, or 0 on success |
|
|
|
|
*/ |
|
|
|
|
static int xvid_ff_2pass_after(struct xvid_context *ref, |
|
|
|
|
xvid_plg_data_t *param) { |
|
|
|
|
xvid_plg_data_t *param) |
|
|
|
|
{ |
|
|
|
|
char *log = ref->twopassbuffer; |
|
|
|
|
const char *frame_types = " ipbs"; |
|
|
|
|
char frame_type; |
|
|
|
@ -193,16 +198,16 @@ static int xvid_ff_2pass_after(struct xvid_context *ref, |
|
|
|
|
return XVID_ERR_FAIL; |
|
|
|
|
|
|
|
|
|
/* Convert the type given to us into a character */ |
|
|
|
|
if( param->type < 5 && param->type > 0 ) { |
|
|
|
|
if (param->type < 5 && param->type > 0) |
|
|
|
|
frame_type = frame_types[param->type]; |
|
|
|
|
} else { |
|
|
|
|
else |
|
|
|
|
return XVID_ERR_FAIL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), |
|
|
|
|
"%c %d %d %d %d %d %d\n", |
|
|
|
|
frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks, |
|
|
|
|
param->stats.ublks, param->stats.length, param->stats.hlength); |
|
|
|
|
frame_type, param->stats.quant, param->stats.kblks, |
|
|
|
|
param->stats.mblks, param->stats.ublks, |
|
|
|
|
param->stats.length, param->stats.hlength); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -224,19 +229,14 @@ static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) |
|
|
|
|
case XVID_PLG_INFO: |
|
|
|
|
case XVID_PLG_FRAME: |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
case XVID_PLG_BEFORE: |
|
|
|
|
return xvid_ff_2pass_before(ref, p1); |
|
|
|
|
|
|
|
|
|
case XVID_PLG_CREATE: |
|
|
|
|
return xvid_ff_2pass_create(p1, p2); |
|
|
|
|
|
|
|
|
|
case XVID_PLG_AFTER: |
|
|
|
|
return xvid_ff_2pass_after(ref, p1); |
|
|
|
|
|
|
|
|
|
case XVID_PLG_DESTROY: |
|
|
|
|
return xvid_ff_2pass_destroy(ref, p1); |
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
return XVID_ERR_FAIL; |
|
|
|
|
} |
|
|
|
@ -255,10 +255,10 @@ static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) |
|
|
|
|
* @param frame_len Length of encoded frame data |
|
|
|
|
* @return Returns new length of frame data |
|
|
|
|
*/ |
|
|
|
|
static int xvid_strip_vol_header(AVCodecContext *avctx, |
|
|
|
|
AVPacket *pkt, |
|
|
|
|
static int xvid_strip_vol_header(AVCodecContext *avctx, AVPacket *pkt, |
|
|
|
|
unsigned int header_len, |
|
|
|
|
unsigned int frame_len) { |
|
|
|
|
unsigned int frame_len) |
|
|
|
|
{ |
|
|
|
|
int vo_len = 0, i; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < header_len - 3; i++) { |
|
|
|
@ -279,7 +279,7 @@ static int xvid_strip_vol_header(AVCodecContext *avctx, |
|
|
|
|
avctx->extradata_size = vo_len; |
|
|
|
|
} |
|
|
|
|
/* Less dangerous now, memmove properly copies the two
|
|
|
|
|
chunks of overlapping data */ |
|
|
|
|
* chunks of overlapping data */ |
|
|
|
|
memmove(pkt->data, &pkt->data[vo_len], frame_len - vo_len); |
|
|
|
|
pkt->size = frame_len - vo_len; |
|
|
|
|
} |
|
|
|
@ -345,7 +345,8 @@ static void xvid_correct_framerate(AVCodecContext *avctx) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
static av_cold int xvid_encode_init(AVCodecContext *avctx) |
|
|
|
|
{ |
|
|
|
|
int xerr, i; |
|
|
|
|
int xvid_flags = avctx->flags; |
|
|
|
|
struct xvid_context *x = avctx->priv_data; |
|
|
|
@ -366,8 +367,7 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */ |
|
|
|
|
if (xvid_flags & CODEC_FLAG_4MV) |
|
|
|
|
x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */ |
|
|
|
|
if( avctx->trellis |
|
|
|
|
) |
|
|
|
|
if (avctx->trellis) |
|
|
|
|
x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */ |
|
|
|
|
if (xvid_flags & CODEC_FLAG_AC_PRED) |
|
|
|
|
x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */ |
|
|
|
@ -378,20 +378,20 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
x->me_flags = 0; |
|
|
|
|
switch (avctx->me_method) { |
|
|
|
|
case ME_FULL: /* Quality 6 */ |
|
|
|
|
x->me_flags |= XVID_ME_EXTSEARCH16 |
|
|
|
|
| XVID_ME_EXTSEARCH8; |
|
|
|
|
x->me_flags |= XVID_ME_EXTSEARCH16 | |
|
|
|
|
XVID_ME_EXTSEARCH8; |
|
|
|
|
|
|
|
|
|
case ME_EPZS: /* Quality 4 */ |
|
|
|
|
x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 |
|
|
|
|
| XVID_ME_HALFPELREFINE8 |
|
|
|
|
| XVID_ME_CHROMA_PVOP |
|
|
|
|
| XVID_ME_CHROMA_BVOP; |
|
|
|
|
x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 | |
|
|
|
|
XVID_ME_HALFPELREFINE8 | |
|
|
|
|
XVID_ME_CHROMA_PVOP | |
|
|
|
|
XVID_ME_CHROMA_BVOP; |
|
|
|
|
|
|
|
|
|
case ME_LOG: /* Quality 2 */ |
|
|
|
|
case ME_PHODS: |
|
|
|
|
case ME_X1: |
|
|
|
|
x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 |
|
|
|
|
| XVID_ME_HALFPELREFINE16; |
|
|
|
|
x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 | |
|
|
|
|
XVID_ME_HALFPELREFINE16; |
|
|
|
|
|
|
|
|
|
case ME_ZERO: /* Quality 0 */ |
|
|
|
|
default: |
|
|
|
@ -402,16 +402,15 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
switch (avctx->mb_decision) { |
|
|
|
|
case 2: |
|
|
|
|
x->vop_flags |= XVID_VOP_MODEDECISION_RD; |
|
|
|
|
x->me_flags |= XVID_ME_HALFPELREFINE8_RD |
|
|
|
|
| XVID_ME_QUARTERPELREFINE8_RD |
|
|
|
|
| XVID_ME_EXTSEARCH_RD |
|
|
|
|
| XVID_ME_CHECKPREDICTION_RD; |
|
|
|
|
x->me_flags |= XVID_ME_HALFPELREFINE8_RD | |
|
|
|
|
XVID_ME_QUARTERPELREFINE8_RD | |
|
|
|
|
XVID_ME_EXTSEARCH_RD | |
|
|
|
|
XVID_ME_CHECKPREDICTION_RD; |
|
|
|
|
case 1: |
|
|
|
|
if (!(x->vop_flags & XVID_VOP_MODEDECISION_RD)) |
|
|
|
|
x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD; |
|
|
|
|
x->me_flags |= XVID_ME_HALFPELREFINE16_RD |
|
|
|
|
| XVID_ME_QUARTERPELREFINE16_RD; |
|
|
|
|
|
|
|
|
|
x->me_flags |= XVID_ME_HALFPELREFINE16_RD | |
|
|
|
|
XVID_ME_QUARTERPELREFINE16_RD; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -445,8 +444,10 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
xvid_enc_create.version = XVID_VERSION; |
|
|
|
|
|
|
|
|
|
/* Store the desired frame size */ |
|
|
|
|
xvid_enc_create.width = x->xsize = avctx->width; |
|
|
|
|
xvid_enc_create.height = x->ysize = avctx->height; |
|
|
|
|
xvid_enc_create.width = |
|
|
|
|
x->xsize = avctx->width; |
|
|
|
|
xvid_enc_create.height = |
|
|
|
|
x->ysize = avctx->height; |
|
|
|
|
|
|
|
|
|
/* Xvid can determine the proper profile to use */ |
|
|
|
|
/* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ |
|
|
|
@ -475,7 +476,8 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
"Xvid: Cannot allocate 2-pass log buffers\n"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; |
|
|
|
|
x->twopassbuffer[0] = |
|
|
|
|
x->old_twopassbuffer[0] = 0; |
|
|
|
|
|
|
|
|
|
plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; |
|
|
|
|
plugins[xvid_enc_create.num_plugins].param = &rc2pass1; |
|
|
|
@ -486,8 +488,7 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
|
|
|
|
|
fd = ff_tempfile("xvidff.", &x->twopassfile); |
|
|
|
|
if (fd == -1) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, |
|
|
|
|
"Xvid: Cannot write 2-pass pipe\n"); |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write 2-pass pipe\n"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -500,8 +501,7 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
if (strlen(avctx->stats_in) > |
|
|
|
|
write(fd, avctx->stats_in, strlen(avctx->stats_in))) { |
|
|
|
|
close(fd); |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, |
|
|
|
|
"Xvid: Cannot write to 2-pass pipe\n"); |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write to 2-pass pipe\n"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -536,8 +536,8 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
|
|
|
|
|
/* The old behavior is that when avctx->lumi_masking is specified,
|
|
|
|
|
* plugins[...].param = NULL. Trying to keep the old behavior here. */ |
|
|
|
|
plugins[xvid_enc_create.num_plugins].param = avctx->lumi_masking ? NULL |
|
|
|
|
: &masking_l; |
|
|
|
|
plugins[xvid_enc_create.num_plugins].param = |
|
|
|
|
avctx->lumi_masking ? NULL : &masking_l; |
|
|
|
|
xvid_enc_create.num_plugins++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -570,8 +570,10 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
xvid_enc_create.max_key_interval = 240; /* Xvid's best default */ |
|
|
|
|
|
|
|
|
|
/* Quants */ |
|
|
|
|
if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; |
|
|
|
|
else x->qscale = 0; |
|
|
|
|
if (xvid_flags & CODEC_FLAG_QSCALE) |
|
|
|
|
x->qscale = 1; |
|
|
|
|
else |
|
|
|
|
x->qscale = 0; |
|
|
|
|
|
|
|
|
|
xvid_enc_create.min_quant[0] = avctx->qmin; |
|
|
|
|
xvid_enc_create.min_quant[1] = avctx->qmin; |
|
|
|
@ -581,7 +583,8 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
xvid_enc_create.max_quant[2] = avctx->qmax; |
|
|
|
|
|
|
|
|
|
/* Quant Matrices */ |
|
|
|
|
x->intra_matrix = x->inter_matrix = NULL; |
|
|
|
|
x->intra_matrix = |
|
|
|
|
x->inter_matrix = NULL; |
|
|
|
|
if (avctx->mpeg_quant) |
|
|
|
|
x->vol_flags |= XVID_VOL_MPEGQUANT; |
|
|
|
|
if ((avctx->intra_matrix || avctx->inter_matrix)) { |
|
|
|
@ -630,7 +633,8 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx) { |
|
|
|
|
xvid_enc_create.max_bframes = avctx->max_b_frames; |
|
|
|
|
xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset; |
|
|
|
|
xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor; |
|
|
|
|
if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; |
|
|
|
|
if (avctx->max_b_frames > 0 && !x->quicktime_format) |
|
|
|
|
xvid_enc_create.global |= XVID_GLOBAL_PACKED; |
|
|
|
|
|
|
|
|
|
/* Create encoder context */ |
|
|
|
|
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
|
|
|
@ -651,11 +655,11 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
|
|
|
|
const AVFrame *picture, int *got_packet) |
|
|
|
|
{ |
|
|
|
|
int xerr, i, ret, user_packet = !!pkt->data; |
|
|
|
|
char *tmp; |
|
|
|
|
struct xvid_context *x = avctx->priv_data; |
|
|
|
|
AVFrame *p = avctx->coded_frame; |
|
|
|
|
int mb_width = (avctx->width + 15) / 16; |
|
|
|
|
int mb_height = (avctx->height + 15) / 16; |
|
|
|
|
char *tmp; |
|
|
|
|
|
|
|
|
|
xvid_enc_frame_t xvid_enc_frame = { 0 }; |
|
|
|
|
xvid_enc_stats_t xvid_enc_stats = { 0 }; |
|
|
|
@ -676,7 +680,8 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
|
|
|
|
|
|
|
|
|
/* Initialize input image fields */ |
|
|
|
|
if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Xvid: Color spaces other than 420p not supported\n"); |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, |
|
|
|
|
"Xvid: Color spaces other than 420P not supported\n"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -709,8 +714,10 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
|
|
|
|
xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den; |
|
|
|
|
|
|
|
|
|
/* Quant Setting */ |
|
|
|
|
if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; |
|
|
|
|
else xvid_enc_frame.quant = 0; |
|
|
|
|
if (x->qscale) |
|
|
|
|
xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; |
|
|
|
|
else |
|
|
|
|
xvid_enc_frame.quant = 0; |
|
|
|
|
|
|
|
|
|
/* Matrices */ |
|
|
|
|
xvid_enc_frame.quant_intra_matrix = x->intra_matrix; |
|
|
|
@ -761,12 +768,14 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
|
|
|
|
av_free_packet(pkt); |
|
|
|
|
if (!xerr) |
|
|
|
|
return 0; |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr); |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, |
|
|
|
|
"Xvid: Encoding Error Occurred: %i\n", xerr); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static av_cold int xvid_encode_close(AVCodecContext *avctx) { |
|
|
|
|
static av_cold int xvid_encode_close(AVCodecContext *avctx) |
|
|
|
|
{ |
|
|
|
|
struct xvid_context *x = avctx->priv_data; |
|
|
|
|
|
|
|
|
|
xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
|
|
|