FS-11183 FS-11425 refactor out parse_profile

This commit is contained in:
Seven Du 2018-10-01 13:13:12 +08:00 committed by Andrey Volk
parent cd2a53b52d
commit 807efbd0fa
1 changed files with 224 additions and 219 deletions

View File

@ -1871,105 +1871,8 @@ void show_codecs(switch_stream_handle_t *stream)
#define UINTVAL(v) (v > 0 ? v : 0);
static void load_config()
static void parse_profile(switch_xml_t profile)
{
switch_xml_t cfg = NULL, xml = NULL;
int i;
switch_set_string(avcodec_globals.profiles[get_codec_index("H263")].name, "H263");
switch_set_string(avcodec_globals.profiles[get_codec_index("H263+")].name, "H263+");
switch_set_string(avcodec_globals.profiles[get_codec_index("H264")].name, "H264");
switch_set_string(avcodec_globals.profiles[get_codec_index("H265")].name, "H265");
for (i = 0; i < MAX_CODECS; i++) {
avcodec_profile_t *profile = &avcodec_globals.profiles[i];
profile->ctx.colorspace = AVCOL_SPC_RGB;
profile->ctx.color_range = AVCOL_RANGE_JPEG;
profile->ctx.flags = 0;
profile->ctx.me_cmp = -1;
profile->ctx.me_range = -1;
profile->ctx.max_b_frames = -1;
profile->ctx.refs = -1;
profile->ctx.gop_size = -1;
profile->ctx.keyint_min = -1;
profile->ctx.i_quant_factor = -1;
profile->ctx.b_quant_factor = -1;
profile->ctx.qcompress = -1;
profile->ctx.qmin = -1;
profile->ctx.qmax = -1;
profile->ctx.max_qdiff = -1;
profile->ctx.thread_count = switch_parse_cpu_string("cpu/2/4");
profile->decoder_thread_count = switch_parse_cpu_string("cpu/2/4");
if (!strcasecmp(CODEC_MAPS[i], "H264")) {
profile->ctx.profile = FF_PROFILE_H264_BASELINE;
profile->ctx.level = 41;
#ifdef AV_CODEC_FLAG_PSNR
profile->ctx.flags |= AV_CODEC_FLAG_PSNR;
#endif
#ifdef CODEC_FLAG_LOOP_FILTER
profile->ctx.flags |= CODEC_FLAG_LOOP_FILTER;
#endif
}
}
avcodec_globals.max_bitrate = 0;
xml = switch_xml_open_cfg("avcodec.conf", &cfg, NULL);
if (xml) {
switch_xml_t settings = switch_xml_child(cfg, "settings");
switch_xml_t profiles = switch_xml_child(cfg, "profiles");
if (settings) {
switch_xml_t param;
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
const char *name = switch_xml_attr(param, "name");
const char *value = switch_xml_attr(param, "value");
if (zstr(name) || zstr(value)) continue;
if (!strcmp(name, "max-bitrate")) {
avcodec_globals.max_bitrate = switch_parse_bandwidth_string(value);
} else if (!strcmp(name, "rtp-slice-size")) {
int val = atoi(value);
avcodec_globals.rtp_slice_size = UINTVAL(val);
} else if (!strcmp(name, "key-frame-min-freq")) {
int val = atoi(value);
avcodec_globals.key_frame_min_freq = UINTVAL(val);
avcodec_globals.key_frame_min_freq *= 1000;
} else if (!strcmp(name, "dec-threads")) {
int i;
unsigned int threads = switch_parse_cpu_string(value);
for (i = 0; i < MAX_CODECS; i++) {
avcodec_globals.profiles[i].decoder_thread_count = threads;
}
} else if (!strcmp(name, "enc-threads")) {
int i;
unsigned int threads = switch_parse_cpu_string(value);
for (i = 0; i < MAX_CODECS; i++) {
avcodec_globals.profiles[i].ctx.thread_count = threads;
}
} else if (!strcasecmp(name, "h263-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H263")].name, value);
} else if (!strcasecmp(name, "h263+-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H263+")].name, value);
} else if (!strcasecmp(name, "h264-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H264")].name, value);
} else if (!strcasecmp(name, "h265-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H265")].name, value);
}
}
}
if (profiles) {
switch_xml_t profile = switch_xml_child(profiles, "profile");
for (; profile; profile = profile->next) {
switch_xml_t options = switch_xml_child(profile, "options");
switch_xml_t param = NULL;
const char *profile_name = switch_xml_attr(profile, "name");
@ -1977,7 +1880,7 @@ static void load_config()
AVCodecContext *ctx = NULL;
int i;
if (zstr(profile_name)) continue;
if (zstr(profile_name)) return;
for (i = 0; i < MAX_CODECS; i++) {
if (!strcmp(profile_name, avcodec_globals.profiles[i].name)) {
@ -1987,7 +1890,7 @@ static void load_config()
}
}
if (!ctx) continue;
if (!ctx) return;
for (param = switch_xml_child(profile, "param"); param; param = param->next) {
const char *name = switch_xml_attr(param, "name");
@ -2189,6 +2092,108 @@ static void load_config()
switch_event_add_header_string(aprofile->options, SWITCH_STACK_BOTTOM, name, value);
}
} // for options
}
static void load_config()
{
switch_xml_t cfg = NULL, xml = NULL;
int i;
switch_set_string(avcodec_globals.profiles[get_codec_index("H263")].name, "H263");
switch_set_string(avcodec_globals.profiles[get_codec_index("H263+")].name, "H263+");
switch_set_string(avcodec_globals.profiles[get_codec_index("H264")].name, "H264");
switch_set_string(avcodec_globals.profiles[get_codec_index("H265")].name, "H265");
for (i = 0; i < MAX_CODECS; i++) {
avcodec_profile_t *profile = &avcodec_globals.profiles[i];
profile->ctx.colorspace = AVCOL_SPC_RGB;
profile->ctx.color_range = AVCOL_RANGE_JPEG;
profile->ctx.flags = 0;
profile->ctx.me_cmp = -1;
profile->ctx.me_range = -1;
profile->ctx.max_b_frames = -1;
profile->ctx.refs = -1;
profile->ctx.gop_size = -1;
profile->ctx.keyint_min = -1;
profile->ctx.i_quant_factor = -1;
profile->ctx.b_quant_factor = -1;
profile->ctx.qcompress = -1;
profile->ctx.qmin = -1;
profile->ctx.qmax = -1;
profile->ctx.max_qdiff = -1;
profile->ctx.thread_count = switch_parse_cpu_string("cpu/2/4");
profile->decoder_thread_count = switch_parse_cpu_string("cpu/2/4");
if (!strcasecmp(CODEC_MAPS[i], "H264")) {
profile->ctx.profile = FF_PROFILE_H264_BASELINE;
profile->ctx.level = 41;
#ifdef AV_CODEC_FLAG_PSNR
profile->ctx.flags |= AV_CODEC_FLAG_PSNR;
#endif
#ifdef CODEC_FLAG_LOOP_FILTER
profile->ctx.flags |= CODEC_FLAG_LOOP_FILTER;
#endif
}
}
avcodec_globals.max_bitrate = 0;
xml = switch_xml_open_cfg("avcodec.conf", &cfg, NULL);
if (xml) {
switch_xml_t settings = switch_xml_child(cfg, "settings");
switch_xml_t profiles = switch_xml_child(cfg, "profiles");
if (settings) {
switch_xml_t param;
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
const char *name = switch_xml_attr(param, "name");
const char *value = switch_xml_attr(param, "value");
if (zstr(name) || zstr(value)) continue;
if (!strcmp(name, "max-bitrate")) {
avcodec_globals.max_bitrate = switch_parse_bandwidth_string(value);
} else if (!strcmp(name, "rtp-slice-size")) {
int val = atoi(value);
avcodec_globals.rtp_slice_size = UINTVAL(val);
} else if (!strcmp(name, "key-frame-min-freq")) {
int val = atoi(value);
avcodec_globals.key_frame_min_freq = UINTVAL(val);
avcodec_globals.key_frame_min_freq *= 1000;
} else if (!strcmp(name, "dec-threads")) {
int i;
unsigned int threads = switch_parse_cpu_string(value);
for (i = 0; i < MAX_CODECS; i++) {
avcodec_globals.profiles[i].decoder_thread_count = threads;
}
} else if (!strcmp(name, "enc-threads")) {
int i;
unsigned int threads = switch_parse_cpu_string(value);
for (i = 0; i < MAX_CODECS; i++) {
avcodec_globals.profiles[i].ctx.thread_count = threads;
}
} else if (!strcasecmp(name, "h263-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H263")].name, value);
} else if (!strcasecmp(name, "h263+-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H263+")].name, value);
} else if (!strcasecmp(name, "h264-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H264")].name, value);
} else if (!strcasecmp(name, "h265-profile")) {
switch_set_string(avcodec_globals.profiles[get_codec_index("H265")].name, value);
}
}
}
if (profiles) {
switch_xml_t profile = switch_xml_child(profiles, "profile");
for (; profile; profile = profile->next) {
parse_profile(profile);
} // for profile
} // profiles