Skip to content

Commit

Permalink
FFmpeg: Create, use av_err2string() (#689)
Browse files Browse the repository at this point in the history
- Previously 'av_make_error_string' was defined in FFmpegUtilities.h
  for the sole purpose of redefining `av_err2str()` as a call to that
  function. `av_err2str()` was then used in our code, often in string
  contexts where its output was cast to `std::string`.
- Since that was excessively circular, instead the function is named
  `av_err2string()`, and it's used directly in contexts where a
  std::string is expected.
- `av_err2str()` is still #defined as `av_err2string(...).c_str()`
  • Loading branch information
ferdnyc committed Aug 11, 2021
1 parent 7e419b9 commit fbe0242
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/FFmpegUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ extern "C" {
#endif

// This wraps an unsafe C macro to be C++ compatible function
inline static const std::string av_make_error_string(int errnum)
inline static const std::string av_err2string(int errnum)
{
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
return (std::string)errbuf;
return static_cast<std::string>(errbuf);
}

// Redefine the C macro to use our new C++ function
#undef av_err2str
#define av_err2str(errnum) av_make_error_string(errnum).c_str()
#define av_err2str(errnum) av_err2string(errnum).c_str()

// Define this for compatibility
#ifndef PixelFormat
Expand Down
22 changes: 11 additions & 11 deletions src/FFmpegWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx, int6
frames_ctx->initial_pool_size = 20;
if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
std::clog << "Failed to initialize HW frame context. " <<
"Error code: " << av_err2str(err) << "\n";
"Error code: " << av_err2string(err) << "\n";
av_buffer_unref(&hw_frames_ref);
return err;
}
Expand Down Expand Up @@ -882,7 +882,7 @@ void FFmpegWriter::flush_encoders() {
#endif // IS_FFMPEG_3_2

if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
}
if (!got_packet) {
break;
Expand All @@ -895,7 +895,7 @@ void FFmpegWriter::flush_encoders() {
// Write packet
error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
}
}

Expand All @@ -918,7 +918,7 @@ void FFmpegWriter::flush_encoders() {
#endif
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod(
"FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]",
"FFmpegWriter::flush_encoders ERROR [" + av_err2string(error_code) + "]",
"error_code", error_code);
}
if (!got_packet) {
Expand All @@ -940,7 +940,7 @@ void FFmpegWriter::flush_encoders() {
error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod(
"FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]",
"FFmpegWriter::flush_encoders ERROR [" + av_err2string(error_code) + "]",
"error_code", error_code);
}

Expand Down Expand Up @@ -1492,7 +1492,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) {
int err;
if ((err = set_hwframe_ctx(video_codec_ctx, hw_device_ctx, info.width, info.height)) < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::open_video (set_hwframe_ctx) ERROR faled to set hwframe context",
"width", info.width, "height", info.height, av_err2str(err), -1);
"width", info.width, "height", info.height, av_err2string(err), -1);
}
}
#endif // USE_HW_ACCEL
Expand Down Expand Up @@ -1598,7 +1598,7 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
// Fill input frame with sample data
int error_code = avcodec_fill_audio_frame(audio_frame, channels_in_frame, AV_SAMPLE_FMT_S16, (uint8_t *) all_queued_samples, all_queued_samples_size, 0);
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
}

// Do not convert audio to planar format (yet). We need to keep everything interleaved at this point.
Expand Down Expand Up @@ -1885,7 +1885,7 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
}

if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
}

// Increment PTS (no pkt.duration, so calculate with maths)
Expand Down Expand Up @@ -2035,7 +2035,7 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
/* write the compressed frame in the media file */
int error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
return false;
}

Expand Down Expand Up @@ -2116,7 +2116,7 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
// Write video packet (older than FFmpeg 3.2)
error_code = avcodec_encode_video2(video_codec_ctx, &pkt, frame_final, &got_packet_ptr);
if (error_code != 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + av_err2string(error_code) + "]", "error_code", error_code);
}
if (got_packet_ptr == 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet (Frame gotpacket error)");
Expand All @@ -2132,7 +2132,7 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
/* write the compressed frame in the media file */
int result = av_interleaved_write_frame(oc, &pkt);
if (result < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + (std::string) av_err2str(result) + "]", "result", result);
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + av_err2string(result) + "]", "result", result);
return false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/FrameMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,9 @@ void FrameMapper::ResampleMappedAudio(std::shared_ptr<Frame> frame, int64_t orig

if (error_code < 0)
{
ZmqLogger::Instance()->AppendDebugMethod("FrameMapper::ResampleMappedAudio ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
ZmqLogger::Instance()->AppendDebugMethod(
"FrameMapper::ResampleMappedAudio ERROR [" + av_err2string(error_code) + "]",
"error_code", error_code);
throw ErrorEncodingVideo("Error while resampling audio in frame mapper", frame->number);
}

Expand Down

0 comments on commit fbe0242

Please sign in to comment.