Skip to content

Commit 0f4a066

Browse files
committed
Fix encoding/decoding and codec context
1 parent 3194748 commit 0f4a066

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer)
9696
// @see CODEC_CAP_DELAY
9797
int ret = avcodec_send_packet(&_inputStream->getVideoCodec().getAVCodecContext(), &data.getAVPacket());
9898

99-
if(ret < 0)
100-
{
99+
if (ret < 0 && (nextPacketRead || ret != AVERROR_EOF))
101100
throw std::runtime_error("An error occurred sending video packet to decoder: " + getDescriptionFromErrorCode(ret));
102-
}
103101

104-
ret = avcodec_receive_frame(&_inputStream->getAudioCodec().getAVCodecContext(), &frameBuffer.getAVFrame());
102+
ret = avcodec_receive_frame(&_inputStream->getVideoCodec().getAVCodecContext(), &frameBuffer.getAVFrame());
105103

106104
if (ret == 0)
107105
got_frame = true;
@@ -111,7 +109,7 @@ bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer)
111109
throw std::runtime_error("An error occurred receiving video packet from decoder: " + getDescriptionFromErrorCode(ret));
112110

113111
// if no frame could be decompressed
114-
if(!nextPacketRead && ret == 0 && got_frame == 0)
112+
if ((!nextPacketRead && ret == 0) || !got_frame)
115113
decodeNextFrame = false;
116114
else
117115
decodeNextFrame = true;

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ bool AudioEncoder::encode(const AVFrame* decodedData, AVPacket& encodedData)
122122
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
123123
#if LIBAVCODEC_VERSION_MAJOR > 58
124124
int ret = avcodec_send_frame(&avCodecContext, decodedData);
125+
125126
if(ret != 0)
126-
{
127127
throw std::runtime_error("Error sending audio frame to encoder: " + getDescriptionFromErrorCode(ret));
128-
}
129128

130129
ret = avcodec_receive_packet(&avCodecContext, &encodedData);
131130

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,19 @@ bool VideoEncoder::encode(const AVFrame* decodedData, AVPacket& encodedData)
138138
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
139139
#if LIBAVCODEC_VERSION_MAJOR > 58
140140
int ret = avcodec_send_frame(&avCodecContext, decodedData);
141-
if(ret != 0)
142-
{
141+
142+
if (ret != 0 && ret != AVERROR_EOF)
143143
throw std::runtime_error("Error sending video frame to encoder: " + getDescriptionFromErrorCode(ret));
144-
}
145144

146145
ret = avcodec_receive_packet(&avCodecContext, &encodedData);
147146

148147
if (ret == 0)
149-
{
150148
return true;
151-
}
152-
else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
153-
{
149+
150+
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
154151
return false;
155-
}
156-
else
157-
{
158-
throw std::runtime_error("Error receiving video frame from encoder: " + getDescriptionFromErrorCode(ret));
159-
}
152+
153+
throw std::runtime_error("Error receiving video frame from encoder: " + getDescriptionFromErrorCode(ret));
160154

161155
#elif LIBAVCODEC_VERSION_MAJOR > 53
162156
int gotPacket = 0;

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ IOutputStream& OutputFile::addVideoStream(const VideoCodec& videoDesc)
3636
{
3737
AVStream& stream = _formatContext.addAVStream(videoDesc.getAVCodec());
3838

39+
stream.codecpar->codec_type = videoDesc.getAVCodecContext().codec_type;
40+
stream.codecpar->codec_id = videoDesc.getAVCodecContext().codec_id;
41+
stream.codecpar->codec_tag = videoDesc.getAVCodecContext().codec_tag;
42+
3943
stream.codecpar->width = videoDesc.getAVCodecContext().width;
4044
stream.codecpar->height = videoDesc.getAVCodecContext().height;
4145
stream.codecpar->bit_rate = videoDesc.getAVCodecContext().bit_rate;
@@ -69,6 +73,10 @@ IOutputStream& OutputFile::addAudioStream(const AudioCodec& audioDesc)
6973
{
7074
AVStream& stream = _formatContext.addAVStream(audioDesc.getAVCodec());
7175

76+
stream.codecpar->codec_type = audioDesc.getAVCodecContext().codec_type;
77+
stream.codecpar->codec_id = audioDesc.getAVCodecContext().codec_id;
78+
stream.codecpar->codec_tag = audioDesc.getAVCodecContext().codec_tag;
79+
7280
stream.codecpar->sample_rate = audioDesc.getAVCodecContext().sample_rate;
7381
stream.codecpar->channels = audioDesc.getAVCodecContext().channels;
7482
stream.codecpar->channel_layout = audioDesc.getAVCodecContext().channel_layout;
@@ -91,6 +99,10 @@ IOutputStream& OutputFile::addCustomStream(const ICodec& iCodecDesc)
9199
{
92100
AVStream& stream = _formatContext.addAVStream(iCodecDesc.getAVCodec());
93101

102+
stream.codecpar->codec_type = iCodecDesc.getAVCodecContext().codec_type;
103+
stream.codecpar->codec_id = iCodecDesc.getAVCodecContext().codec_id;
104+
stream.codecpar->codec_tag = iCodecDesc.getAVCodecContext().codec_tag;
105+
94106
stream.codecpar->sample_rate = 48000;
95107
stream.codecpar->channels = 1;
96108
stream.codecpar->channel_layout = AV_CH_LAYOUT_MONO;

src/AvTranscoder/stream/InputStream.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ InputStream::InputStream(InputFile& inputFile, const size_t streamIndex)
2121
, _streamIndex(streamIndex)
2222
, _isActivated(false)
2323
{
24-
const AVCodec* codec = avcodec_find_encoder(_inputFile->getFormatContext().getAVStream(_streamIndex).codecpar->codec_id);
24+
AVCodecParameters* codecParameters = _inputFile->getFormatContext().getAVStream(_streamIndex).codecpar;
25+
const AVCodec* codec = avcodec_find_decoder(codecParameters->codec_id);
2526
AVCodecContext* context = avcodec_alloc_context3(codec);
27+
int ret = avcodec_parameters_to_context(context, codecParameters);
28+
29+
if (ret < 0)
30+
throw std::runtime_error("Failed to copy decoder parameters to input stream context");
2631

2732
switch(context->codec_type)
2833
{

src/AvTranscoder/stream/OutputStream.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ OutputStream::OutputStream(OutputFile& outputFile, const size_t streamIndex)
1717
, _lastWrappedPacketDuration(0)
1818
, _isPTSGenerated(false)
1919
{
20-
const AVCodec* codec = avcodec_find_decoder(_outputAVStream.codecpar->codec_id);
20+
const AVCodec* codec = avcodec_find_encoder(_outputAVStream.codecpar->codec_id);
2121
_codecContext = avcodec_alloc_context3(codec);
2222

23-
avcodec_parameters_to_context(_codecContext, _outputAVStream.codecpar);
23+
int ret = avcodec_parameters_to_context(_codecContext, _outputAVStream.codecpar);
24+
if (ret < 0)
25+
throw std::runtime_error("Failed to copy encoder parameters to output stream context");
2426

2527
#if LIBAVCODEC_VERSION_MAJOR > 58
2628
// depending on the format, place global headers in extradata instead of every keyframe

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy