Skip to content

Commit

Permalink
Create C++ wrappers for the FFmpeg error functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdude42 committed Jun 17, 2020
1 parent 7a2fe46 commit 3adb5da
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 35 deletions.
5 changes: 3 additions & 2 deletions mythtv/libs/libmyth/audio/audioconvert.cpp
Expand Up @@ -27,6 +27,7 @@

#include "mythconfig.h"
#include "mythlogging.h"
#include "mythaverror.h"
#include "audioconvert.h"

extern "C" {
Expand Down Expand Up @@ -596,10 +597,10 @@ class AudioConvertInternal
int ret = swr_init(m_swr);
if (ret < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_AUDIO, LOG_ERR, LOC +
QString("error initializing resampler context (%1)")
.arg(av_make_error_string(error, sizeof(error), ret)));
.arg(av_make_error_stdstring(error, ret)));
swr_free(&m_swr);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmyth/audio/audiooutput.cpp
Expand Up @@ -599,7 +599,6 @@ int AudioOutput::DecodeAudio(AVCodecContext *ctx,
const AVPacket *pkt)
{
bool got_frame = false;
char error[AV_ERROR_MAX_STRING_SIZE];

data_size = 0;
if (!m_frame)
Expand Down Expand Up @@ -631,9 +630,10 @@ int AudioOutput::DecodeAudio(AVCodecContext *ctx,
ret = 0;
else if (ret < 0)
{
std::string error;
LOG(VB_AUDIO, LOG_ERR, LOC +
QString("audio decode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), ret))
.arg(av_make_error_stdstring(error, ret))
.arg(got_frame));
return ret;
}
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmyth/audio/audiooutput.h
Expand Up @@ -13,6 +13,7 @@
#include "audiosettings.h"
#include "audiooutputsettings.h"
#include "mythcorecontext.h"
#include "mythaverror.h"
#include "volumebase.h"
#include "output.h"

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmyth/audio/audiooutputdigitalencoder.cpp
Expand Up @@ -248,10 +248,10 @@ size_t AudioOutputDigitalEncoder::Encode(void *buf, int len, AudioFormat format)

if (ret < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("audio encode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), ret))
.arg(av_make_error_stdstring(error, ret))
.arg(got_packet));
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmyth/audio/audiooutpututil.cpp
Expand Up @@ -246,7 +246,6 @@ int AudioOutputUtil::DecodeAudio(AVCodecContext *ctx,
{
MythAVFrame frame;
bool got_frame = false;
char error[AV_ERROR_MAX_STRING_SIZE];

data_size = 0;
if (!frame)
Expand All @@ -271,9 +270,10 @@ int AudioOutputUtil::DecodeAudio(AVCodecContext *ctx,
ret = 0;
else if (ret < 0)
{
std::string error;
LOG(VB_AUDIO, LOG_ERR, LOC +
QString("audio decode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), ret))
.arg(av_make_error_stdstring(error, ret))
.arg(got_frame));
return ret;
}
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmyth/libmyth.pro
Expand Up @@ -38,7 +38,7 @@ HEADERS += audio/audiooutputgraph.h
HEADERS += backendselect.h dbsettings.h
HEADERS += langsettings.h
HEADERS +=
HEADERS += mythcontext.h
HEADERS += mythaverror.h mythcontext.h
HEADERS += mythexp.h mythmediamonitor.h
HEADERS += schemawizard.h
HEADERS += output.h
Expand All @@ -65,7 +65,7 @@ SOURCES += audio/audiooutputgraph.cpp
SOURCES += backendselect.cpp dbsettings.cpp
SOURCES += langsettings.cpp
SOURCES +=
SOURCES += mythcontext.cpp
SOURCES += mythaverror.cpp mythcontext.cpp
SOURCES += mythmediamonitor.cpp
SOURCES += schemawizard.cpp
SOURCES += output.cpp
Expand Down Expand Up @@ -134,7 +134,7 @@ inc.files += audio/audiooutputsettings.h audio/audiooutpututil.h
inc.files += audio/audioconvert.h
inc.files += audio/volumebase.h audio/eldutils.h
inc.files += inetcomms.h schemawizard.h
inc.files += mythmediamonitor.h
inc.files += mythaverror.h mythmediamonitor.h
inc.files += visual.h output.h langsettings.h
inc.files += mythexp.h storagegroupeditor.h
inc.files += mythterminal.h remoteutil.h
Expand Down
45 changes: 45 additions & 0 deletions mythtv/libs/libmyth/mythaverror.cpp
@@ -0,0 +1,45 @@
/*
* Copyright (c) David Hampton 2020
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string.h>
#include "mythaverror.h"

// \brief A quick and dirty C++ equivalent to av_strerror
//
// The caller must supply the stdstring.
//
// \see av_strerror
int av_strerror_stdstring (int errnum, std::string &errbuf)
{
errbuf.resize(AV_ERROR_MAX_STRING_SIZE);
int rc = av_strerror(errnum, errbuf.data(), errbuf.size());
errbuf.resize(strlen(errbuf.data()));
return rc;
}

// \brief A C++ equivalent to av_make_error_string
//
// The caller must supply the stdstring so that the data pointer
// remains valid after this function returns.
//
// \see av_make_error_string
char *av_make_error_stdstring(std::string &errbuf, int errnum)
{
av_strerror_stdstring(errnum, errbuf);
return errbuf.data();
}
32 changes: 32 additions & 0 deletions mythtv/libs/libmyth/mythaverror.h
@@ -0,0 +1,32 @@
/*
* Copyright (c) David Hampton 2020
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef MYTHAVERROR_H
#define MYTHAVERROR_H

#include <string>
#include "mythexp.h"

extern "C" {
#include "libavutil/error.h"
}

MPUBLIC int av_strerror_stdstring (int errnum, std::string &errbuf);
MPUBLIC char *av_make_error_stdstring(std::string &errbuf, int errnum);

#endif // MYTHAVERROR_H
24 changes: 11 additions & 13 deletions mythtv/libs/libmythtv/decoders/avformatdecoder.cpp
Expand Up @@ -1025,9 +1025,9 @@ int AvFormatDecoder::OpenFile(MythMediaBuffer *Buffer, bool novideo,
err = avformat_open_input(&m_ic, filename, fmt, nullptr);
if (err < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
av_make_error_string(error, sizeof(error), err);
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Failed to open input ('%1')").arg(error));
std::string error;
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Failed to open input ('%1')")
.arg(av_make_error_stdstring(error, err)));

// note - m_ic (AVFormatContext) is freed on failure
if (retries > 1)
Expand Down Expand Up @@ -2519,15 +2519,13 @@ bool AvFormatDecoder::OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
int ret = avcodec_open2(avctx, codec, nullptr);
if (ret < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];

av_make_error_string(error, sizeof(error), ret);
std::string error;
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Could not open codec 0x%1, id(%2) type(%3) "
"ignoring. reason %4").arg((uint64_t)avctx,0,16)
.arg(ff_codec_id_string(avctx->codec_id))
.arg(ff_codec_type_string(avctx->codec_type))
.arg(error));
.arg(av_make_error_stdstring(error, ret)));
return false;
}

Expand Down Expand Up @@ -3455,20 +3453,20 @@ bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt, boo

if (ret < 0 || ret2 < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
if (ret < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
.arg(av_make_error_string(error, sizeof(error), ret))
.arg(av_make_error_stdstring(error, ret))
.arg(ret).arg(gotpicture));
}

if (ret2 < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("video avcodec_send_packet error: %1 (%2) gotpicture:%3")
.arg(av_make_error_string(error, sizeof(error), ret2))
.arg(av_make_error_stdstring(error, ret2))
.arg(ret2).arg(gotpicture));
}

Expand Down Expand Up @@ -4937,10 +4935,10 @@ bool AvFormatDecoder::GetFrame(DecodeType decodetype, bool &Retry)

SetEof(true);
delete pkt;
char errbuf[256];
std::string errbuf(256,'\0');
QString errmsg;
if (av_strerror(retval, errbuf, sizeof errbuf) == 0)
errmsg = QString(errbuf);
if (av_strerror_stdstring(retval, errbuf) == 0)
errmsg = QString::fromStdString(errbuf);
else
errmsg = "UNKNOWN";

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/decoders/mythcodeccontext.cpp
Expand Up @@ -559,11 +559,11 @@ AVBufferRef* MythCodecContext::CreateDevice(AVHWDeviceType Type, MythOpenGLInter
return result;
}

char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_PLAYBACK, LOG_ERR, LOC + QString("Failed to create hardware device '%1'%2 Error '%3'")
.arg(av_hwdevice_get_type_name(Type))
.arg(Device == nullptr ? "" : QString(" (%1)").arg(Device))
.arg(av_make_error_string(error, sizeof(error), res)));
.arg(av_make_error_stdstring(error, res)));
return nullptr;
}

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/decoders/nuppeldecoder.cpp
Expand Up @@ -898,10 +898,10 @@ bool NuppelDecoder::DecodeFrame(struct rtframeheader *frameheader,
// received here.
if (ret < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("video decode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), ret))
.arg(av_make_error_stdstring(error, ret))
.arg(gotpicture));
}
if (!gotpicture)
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/io/mythavformatwriter.cpp
Expand Up @@ -329,9 +329,9 @@ int MythAVFormatWriter::WriteAudioFrame(unsigned char *Buffer, int /*FrameNumber

if (ret < 0)
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_GENERAL, LOG_ERR, LOC + QString("audio encode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), ret)).arg(got_packet));
.arg(av_make_error_stdstring(error, ret)).arg(got_packet));
return ret;
}

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/mheg/mhi.cpp
Expand Up @@ -1900,10 +1900,10 @@ void MHIBitmap::CreateFromMPEG(const unsigned char *data, int length)
len = 0;
if (len < 0) // Error
{
char error[AV_ERROR_MAX_STRING_SIZE];
std::string error;
LOG(VB_GENERAL, LOG_ERR,
QString("[mhi] video decode error: %1 (%2)")
.arg(av_make_error_string(error, sizeof(error), len))
.arg(av_make_error_stdstring(error, len))
.arg(gotPicture));
goto Close;
}
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/mythavutil.cpp
Expand Up @@ -693,9 +693,9 @@ MythStreamInfoList::MythStreamInfoList(QString filename)
m_errorMsg = "File could not be opened";
break;
default:
char errbuf[256];
if (av_strerror(m_errorCode, errbuf, sizeof errbuf) == 0)
m_errorMsg = QString(errbuf);
std::string errbuf;
if (av_strerror_stdstring(m_errorCode, errbuf) == 0)
m_errorMsg = QString::fromStdString(errbuf);
else
m_errorMsg = "UNKNOWN";
}
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/mythframe.h
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#endif
#include "mythtvexp.h" // for MTV_PUBLIC
#include "mythaverror.h"

extern "C" {
#include "libavcodec/avcodec.h"
Expand Down

0 comments on commit 3adb5da

Please sign in to comment.