Further fixing av_init_packet deprecation. In the future AVPacket will not be able to be allocated on the stack. Instead using av_packet_alloc/av_packet_free.

This commit is contained in:
mjbudd77 2021-10-27 07:02:44 -04:00
parent 672e6cb0a6
commit 03341dd834
1 changed files with 40 additions and 17 deletions

View File

@ -756,6 +756,7 @@ struct OutputStream
AVCodecContext *enc; AVCodecContext *enc;
AVFrame *frame; AVFrame *frame;
AVFrame *tmp_frame; AVFrame *tmp_frame;
AVPacket *pkt;
struct SwsContext *sws_ctx; struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx; struct SwrContext *swr_ctx;
int64_t next_pts; int64_t next_pts;
@ -774,6 +775,7 @@ struct OutputStream
st = NULL; st = NULL;
enc = NULL; enc = NULL;
frame = tmp_frame = NULL; frame = tmp_frame = NULL;
pkt = NULL;
sws_ctx = NULL; sws_ctx = NULL;
swr_ctx = NULL; swr_ctx = NULL;
bytesPerSample = 0; bytesPerSample = 0;
@ -800,6 +802,10 @@ struct OutputStream
{ {
avcodec_free_context(&enc); enc = NULL; avcodec_free_context(&enc); enc = NULL;
} }
if ( pkt != NULL )
{
av_packet_free(&pkt); pkt = NULL;
}
if ( frame != NULL ) if ( frame != NULL )
{ {
av_frame_free(&frame); frame = NULL; av_frame_free(&frame); frame = NULL;
@ -1223,6 +1229,14 @@ static int initVideoStream( const char *codec_name, OutputStream *ost )
return -1; return -1;
} }
/* packet for holding encoded output */
ost->pkt = av_packet_alloc();
if (ost->pkt == NULL)
{
fprintf( avLogFp, "Could not allocate the video packet\n");
return -1;
}
/* Allocate the encoded raw picture. */ /* Allocate the encoded raw picture. */
ost->frame = alloc_picture(c->pix_fmt, c->width, c->height); ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
@ -1456,6 +1470,14 @@ static int initAudioStream( const char *codec_name, OutputStream *ost )
return -1; return -1;
} }
/* packet for holding encoded output */
ost->pkt = av_packet_alloc();
if (ost->pkt == NULL)
{
fprintf( avLogFp, "Could not allocate the audio packet\n");
return -1;
}
if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
{ {
nb_samples = audioSampleRate / 4; nb_samples = audioSampleRate / 4;
@ -1722,11 +1744,11 @@ static int write_audio_frame( AVFrame *frame )
} }
while (ret >= 0) while (ret >= 0)
{ {
AVPacket pkt = { 0 }; // data and size must be 0; //AVPacket pkt = { 0 }; // data and size must be 0;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 59, 0, 0 ) //#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 59, 0, 0 )
av_init_packet(&pkt); // av_init_packet(&pkt);
#endif //#endif
ret = avcodec_receive_packet(ost->enc, &pkt); ret = avcodec_receive_packet(ost->enc, ost->pkt);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{ {
fprintf( avLogFp, "Error encoding audio frame\n"); fprintf( avLogFp, "Error encoding audio frame\n");
@ -1735,16 +1757,17 @@ static int write_audio_frame( AVFrame *frame )
} }
else if (ret >= 0) else if (ret >= 0)
{ {
av_packet_rescale_ts(&pkt, ost->enc->time_base, ost->st->time_base); av_packet_rescale_ts(ost->pkt, ost->enc->time_base, ost->st->time_base);
pkt.stream_index = ost->st->index; ost->pkt->stream_index = ost->st->index;
/* Write the compressed frame to the media file. */ /* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt); ret = av_interleaved_write_frame(oc, ost->pkt);
if (ret < 0) if (ret < 0)
{ {
fprintf( avLogFp, "Error while writing audio frame\n"); fprintf( avLogFp, "Error while writing audio frame\n");
ost->writeError = true; ost->writeError = true;
return -1; return -1;
} }
av_packet_unref(ost->pkt);
} }
} }
return 0; return 0;
@ -1918,13 +1941,12 @@ static int encode_video_frame( unsigned char *inBuf )
while (ret >= 0) while (ret >= 0)
{ {
AVPacket pkt = { 0 }; //AVPacket pkt = { 0 };
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 59, 0, 0 ) //#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 59, 0, 0 )
av_init_packet(&pkt); // av_init_packet(&pkt);
#endif //#endif
ret = avcodec_receive_packet(c, ost->pkt);
ret = avcodec_receive_packet(c, &pkt);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{ {
@ -1934,16 +1956,17 @@ static int encode_video_frame( unsigned char *inBuf )
} }
else if (ret >= 0) else if (ret >= 0)
{ {
av_packet_rescale_ts(&pkt, c->time_base, video_st.st->time_base); av_packet_rescale_ts(ost->pkt, c->time_base, video_st.st->time_base);
pkt.stream_index = video_st.st->index; ost->pkt->stream_index = video_st.st->index;
/* Write the compressed frame to the media file. */ /* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt); ret = av_interleaved_write_frame(oc, ost->pkt);
if (ret < 0) if (ret < 0)
{ {
fprintf( avLogFp, "Error while writing video frame\n"); fprintf( avLogFp, "Error while writing video frame\n");
ost->writeError = true; ost->writeError = true;
return -1; return -1;
} }
av_packet_unref(ost->pkt);
} }
} }