Skip to content
Snippets Groups Projects
Commit bd383806 authored by Philippe Gorley's avatar Philippe Gorley Committed by Philippe Gorley
Browse files

encoder: fix segfault if key isn't in dictionary

Change-Id: If32273538b24853b7aba236efd8db730faa5b954
parent a3ad4e9f
No related branches found
No related tags found
No related merge requests found
...@@ -216,4 +216,14 @@ getError(int err) ...@@ -216,4 +216,14 @@ getError(int err)
return ret; return ret;
} }
const char*
getDictValue(const AVDictionary* d, const std::string& key, int flags)
{
auto kv = av_dict_get(d, key.c_str(), nullptr, flags);
if (kv)
return kv->value;
else
return "";
}
}} // namespace ring::libav_utils }} // namespace ring::libav_utils
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <map> #include <map>
#include <string> #include <string>
struct AVDictionary;
struct AVPixFmtDescriptor; struct AVPixFmtDescriptor;
namespace ring { namespace libav_utils { namespace ring { namespace libav_utils {
...@@ -44,4 +45,6 @@ namespace ring { namespace libav_utils { ...@@ -44,4 +45,6 @@ namespace ring { namespace libav_utils {
std::string getError(int err); std::string getError(int err);
const char* getDictValue(const AVDictionary* d, const std::string& key, int flags=0);
}} // namespace ring::libav_utils }} // namespace ring::libav_utils
...@@ -203,9 +203,9 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para ...@@ -203,9 +203,9 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para
encoderCtx = prepareEncoderContext(outputCodec, systemCodecInfo.mediaType == MEDIA_VIDEO); encoderCtx = prepareEncoderContext(outputCodec, systemCodecInfo.mediaType == MEDIA_VIDEO);
encoders_.push_back(encoderCtx); encoders_.push_back(encoderCtx);
auto maxBitrate = 1000 * atoi(av_dict_get(options_, "max_rate", nullptr, 0)->value); auto maxBitrate = 1000 * std::atoi(libav_utils::getDictValue(options_, "max_rate"));
auto bufSize = 2 * maxBitrate; // as recommended (TODO: make it customizable) auto bufSize = 2 * maxBitrate; // as recommended (TODO: make it customizable)
auto crf = atoi(av_dict_get(options_, "crf", nullptr, 0)->value); auto crf = std::atoi(libav_utils::getDictValue(options_, "crf"));
/* let x264 preset override our encoder settings */ /* let x264 preset override our encoder settings */
if (systemCodecInfo.avcodecId == AV_CODEC_ID_H264) { if (systemCodecInfo.avcodecId == AV_CODEC_ID_H264) {
...@@ -217,7 +217,7 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para ...@@ -217,7 +217,7 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para
crf = 30; // good value for H264-720p@30 crf = 30; // good value for H264-720p@30
RING_DBG("H264 encoder setup: crf=%u, maxrate=%u, bufsize=%u", crf, maxBitrate, bufSize); RING_DBG("H264 encoder setup: crf=%u, maxrate=%u, bufsize=%u", crf, maxBitrate, bufSize);
av_opt_set(encoderCtx->priv_data, "crf", av_dict_get(options_, "crf", nullptr, 0)->value, 0); av_opt_set_int(encoderCtx->priv_data, "crf", crf, 0);
encoderCtx->rc_buffer_size = bufSize; encoderCtx->rc_buffer_size = bufSize;
encoderCtx->rc_max_rate = maxBitrate; encoderCtx->rc_max_rate = maxBitrate;
} else if (systemCodecInfo.avcodecId == AV_CODEC_ID_VP8) { } else if (systemCodecInfo.avcodecId == AV_CODEC_ID_VP8) {
...@@ -242,7 +242,7 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para ...@@ -242,7 +242,7 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para
encoderCtx->rc_buffer_size = maxBitrate; encoderCtx->rc_buffer_size = maxBitrate;
encoderCtx->bit_rate = maxBitrate; encoderCtx->bit_rate = maxBitrate;
if (crf != SystemCodecInfo::DEFAULT_NO_QUALITY) { if (crf != SystemCodecInfo::DEFAULT_NO_QUALITY) {
av_opt_set(encoderCtx->priv_data, "crf", av_dict_get(options_, "crf", nullptr, 0)->value, 0); av_opt_set_int(encoderCtx->priv_data, "crf", crf, 0);
RING_DBG("Using quality factor %d", crf); RING_DBG("Using quality factor %d", crf);
} else { } else {
RING_DBG("Using Max bitrate %d", maxBitrate); RING_DBG("Using Max bitrate %d", maxBitrate);
...@@ -554,8 +554,8 @@ AVCodecContext* MediaEncoder::prepareEncoderContext(AVCodec* outputCodec, bool i ...@@ -554,8 +554,8 @@ AVCodecContext* MediaEncoder::prepareEncoderContext(AVCodec* outputCodec, bool i
encoderCtx->width = device_.width; encoderCtx->width = device_.width;
encoderCtx->height = device_.height; encoderCtx->height = device_.height;
} else { } else {
encoderCtx->width = atoi(av_dict_get(options_, "width", nullptr, 0)->value); encoderCtx->width = std::atoi(libav_utils::getDictValue(options_, "width"));
encoderCtx->height = atoi(av_dict_get(options_, "height", nullptr, 0)->value); encoderCtx->height = std::atoi(libav_utils::getDictValue(options_, "height"));
} }
// satisfy ffmpeg: denominator must be 16bit or less value // satisfy ffmpeg: denominator must be 16bit or less value
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment