Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
transcoding: Add video bitrate limiter for h.264/VP8/MPEG2
  • Loading branch information
tajson authored and perexg committed Jan 13, 2015
1 parent ef9556e commit 619654f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/plumbing/transcoding.c
Expand Up @@ -1037,23 +1037,33 @@ transcoder_stream_video(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
octx->qmin = 1;
octx->qmax = FF_LAMBDA_MAX;

octx->bit_rate = 2 * octx->width * octx->height;
octx->rc_max_rate = 4 * octx->bit_rate;
if (t->t_props.tp_vbitrate == 0) {
octx->bit_rate = 2 * octx->width * octx->height;
octx->rc_max_rate = 4 * octx->bit_rate;

} else {
octx->bit_rate = t->t_props.tp_vbitrate * 1000;
octx->rc_max_rate = octx->bit_rate * 1.05;
}

octx->rc_buffer_size = 2 * octx->rc_max_rate;
break;

case SCT_VP8:
octx->codec_id = AV_CODEC_ID_VP8;
octx->pix_fmt = PIX_FMT_YUV420P;

octx->qmin = 10;
octx->qmax = 20;

av_dict_set(&opts, "quality", "realtime", 0);

octx->bit_rate = 3 * octx->width * octx->height;
if (t->t_props.tp_vbitrate == 0) {
octx->qmin = 10;
octx->qmax = 20;
} else {
octx->bit_rate = t->t_props.tp_vbitrate * 1000;
octx->rc_max_rate = octx->bit_rate * 1.05;
}

octx->rc_buffer_size = 8 * 1024 * 224;
octx->rc_max_rate = 2 * octx->bit_rate;
break;

case SCT_H264:
Expand All @@ -1072,18 +1082,25 @@ transcoder_stream_video(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)

// Minimum quantizer. Doesn't need to be changed.
// Recommended default: -qmin 10
octx->qmin = 10;
// octx->qmin = 10;

// Maximum quantizer. Doesn't need to be changed.
// Recommended default: -qmax 51
octx->qmax = 30;
// octx->qmax = 30;

av_dict_set(&opts, "preset", "medium", 0);
av_dict_set(&opts, "profile", "baseline", 0);
av_dict_set(&opts, "tune", "zerolatency", 0);

if (t->t_props.tp_vbitrate == 0) {
octx->qmin = 10;
octx->qmax = 30;
} else {
octx->bit_rate = t->t_props.tp_vbitrate * 1000;
octx->rc_max_rate = octx->bit_rate * 1.05;
}

octx->bit_rate = 2 * octx->width * octx->height;
octx->rc_buffer_size = 8 * 1024 * 224;
octx->rc_max_rate = 2 * octx->rc_buffer_size;
break;

default:
Expand Down Expand Up @@ -1749,6 +1766,7 @@ transcoder_set_properties(streaming_target_t *st,
strncpy(tp->tp_scodec, props->tp_scodec, sizeof(tp->tp_scodec)-1);
tp->tp_channels = props->tp_channels;
tp->tp_bandwidth = props->tp_bandwidth;
tp->tp_vbitrate = props->tp_vbitrate;
tp->tp_resolution = props->tp_resolution;

memcpy(tp->tp_language, props->tp_language, 4);
Expand Down
1 change: 1 addition & 0 deletions src/plumbing/transcoding.h
Expand Up @@ -27,6 +27,7 @@ typedef struct transcoder_prop {

int8_t tp_channels;
int32_t tp_bandwidth;
int32_t tp_vbitrate;
char tp_language[4];
int32_t tp_resolution;

Expand Down
20 changes: 20 additions & 0 deletions src/profile.c
Expand Up @@ -1019,6 +1019,7 @@ typedef struct profile_transcode {
uint32_t pro_resolution;
uint32_t pro_channels;
uint32_t pro_bandwidth;
uint32_t pro_vbitrate;
char *pro_language;
char *pro_vcodec;
char *pro_acodec;
Expand Down Expand Up @@ -1214,6 +1215,13 @@ const idclass_t profile_transcode_class =
.def.s = "libx264",
.list = profile_class_vcodec_list,
},
{
.type = PT_U32,
.id = "vbitrate",
.name = "Video Bitrate (kb/s) (0=Auto)",
.off = offsetof(profile_transcode_t, pro_vbitrate),
.def.u32 = 0,
},
{
.type = PT_STR,
.id = "acodec",
Expand Down Expand Up @@ -1246,6 +1254,12 @@ profile_transcode_bandwidth(profile_transcode_t *pro)
return pro->pro_bandwidth >= 64 ? pro->pro_bandwidth : 64;
}

static int
profile_transcode_vbitrate(profile_transcode_t *pro)
{
return pro->pro_vbitrate;
}

static int
profile_transcode_can_share(profile_chain_t *prch,
profile_chain_t *joiner)
Expand All @@ -1270,6 +1284,8 @@ profile_transcode_can_share(profile_chain_t *prch,
return 0;
if (profile_transcode_bandwidth(pro1) != profile_transcode_bandwidth(pro2))
return 0;
if (profile_transcode_vbitrate(pro1) != profile_transcode_vbitrate(pro2))
return 0;
if (strcmp(pro1->pro_language ?: "", pro2->pro_language ?: ""))
return 0;
return 1;
Expand Down Expand Up @@ -1297,6 +1313,7 @@ profile_transcode_work(profile_chain_t *prch,
props.tp_resolution = profile_transcode_resolution(pro);
props.tp_channels = pro->pro_channels;
props.tp_bandwidth = profile_transcode_bandwidth(pro);
props.tp_vbitrate = profile_transcode_vbitrate(pro);
strncpy(props.tp_language, pro->pro_language ?: "", 3);

dst = prch->prch_gh = globalheaders_create(dst);
Expand Down Expand Up @@ -1506,6 +1523,7 @@ profile_init(void)
htsmsg_add_u32 (conf, "resolution", 384);
htsmsg_add_u32 (conf, "channels", 2);
htsmsg_add_str (conf, "vcodec", "libvpx");
htsmsg_add_str (conf, "vbitrate", 0);
htsmsg_add_str (conf, "acodec", "libvorbis");
htsmsg_add_bool(conf, "shield", 1);
(void)profile_create(NULL, conf, 1);
Expand All @@ -1525,6 +1543,7 @@ profile_init(void)
htsmsg_add_u32 (conf, "resolution", 384);
htsmsg_add_u32 (conf, "channels", 2);
htsmsg_add_str (conf, "vcodec", "libx264");
htsmsg_add_str (conf, "vbitrate", 0);
htsmsg_add_str (conf, "acodec", "aac");
htsmsg_add_bool(conf, "shield", 1);
(void)profile_create(NULL, conf, 1);
Expand All @@ -1544,6 +1563,7 @@ profile_init(void)
htsmsg_add_u32 (conf, "resolution", 384);
htsmsg_add_u32 (conf, "channels", 2);
htsmsg_add_str (conf, "vcodec", "libx264");
htsmsg_add_str (conf, "vbitrate", 0);
htsmsg_add_str (conf, "acodec", "aac");
htsmsg_add_bool(conf, "shield", 1);
(void)profile_create(NULL, conf, 1);
Expand Down

0 comments on commit 619654f

Please sign in to comment.