Skip to content

Commit 730718c

Browse files
uknunknownFlole998
authored andcommitted
fix memory leak 3 - transcoding
- fix memory leak 3 - transcoding
1 parent 19026f3 commit 730718c

1 file changed

Lines changed: 11 additions & 30 deletions

File tree

  • src/transcoding/transcode/hwaccels

src/transcoding/transcode/hwaccels/vaapi.c

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ typedef struct tvh_vaapi_context_t {
3232
int width;
3333
int height;
3434
AVBufferRef *hw_device_ref;
35-
AVBufferRef *hw_frame_ref;
3635
} TVHVAContext;
3736

3837

@@ -53,10 +52,6 @@ tvhva_context_destroy(TVHVAContext *self)
5352
av_buffer_unref(&self->hw_device_ref);
5453
self->hw_device_ref = NULL;
5554
}
56-
if (self->hw_frame_ref) {
57-
av_buffer_unref(&self->hw_frame_ref);
58-
self->hw_frame_ref = NULL;
59-
}
6055
free(self);
6156
self = NULL;
6257
}
@@ -631,18 +626,17 @@ vaapi_decode_setup_context(AVCodecContext *avctx)
631626
tvherror(LS_VAAPI, "Decode: Failed to allocate VAAPI context (TVHVAContext)");
632627
return AVERROR(ENOMEM);
633628
}
634-
629+
// lifted from ffmpeg-6.1.1/doc/examples/vaapi_transcode.c line 237
635630
/* Open VAAPI device and create an AVHWDeviceContext for it*/
636631
if ((ret = av_hwdevice_ctx_create(&self->hw_device_ref, AV_HWDEVICE_TYPE_VAAPI, ctx->hw_accel_device, NULL, 0)) < 0) {
637632
tvherror(LS_VAAPI, "Decode: Failed to Open VAAPI device and create an AVHWDeviceContext for device: "
638633
"%s with error code: %s",
639634
ctx->hw_accel_device, av_err2str(ret));
640-
// unref self
641635
free(self);
642636
self = NULL;
643637
return ret;
644638
}
645-
639+
// lifted from ffmpeg-6.1.1/doc/examples/vaapi_transcode.c line 95
646640
/* set hw_frames_ctx for decoder's AVCodecContext */
647641
avctx->hw_device_ctx = av_buffer_ref(self->hw_device_ref);
648642
if (!avctx->hw_device_ctx) {
@@ -651,7 +645,6 @@ vaapi_decode_setup_context(AVCodecContext *avctx)
651645
// unref hw_device_ref
652646
av_buffer_unref(&self->hw_device_ref);
653647
self->hw_device_ref = NULL;
654-
// unref self
655648
free(self);
656649
self = NULL;
657650
return AVERROR(ENOMEM);
@@ -764,37 +757,24 @@ int
764757
vaapi_encode_setup_context(AVCodecContext *avctx)
765758
{
766759
TVHContext *ctx = avctx->opaque;
767-
TVHVAContext *self = NULL;
768760
int ret = 0;
769761

770-
if (!(self = calloc(1, sizeof(TVHVAContext)))) {
771-
tvherror(LS_VAAPI, "Encode: Failed to allocate VAAPI context (TVHVAContext)");
772-
return AVERROR(ENOMEM);
773-
}
774-
762+
// lifted from ffmpeg-6.1.1/doc/examples/vaapi_encode.c line 127
775763
/* Open VAAPI device and create an AVHWDeviceContext for it*/
776-
if ((ret = av_hwdevice_ctx_create(&self->hw_frame_ref, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0)) < 0) {
764+
if ((ret = av_hwdevice_ctx_create(&ctx->hw_device_octx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0)) < 0) {
777765
tvherror(LS_VAAPI, "Encode: Failed to open VAAPI device and create an AVHWDeviceContext for it."
778766
"Error code: %s",av_err2str(ret));
779-
free(self);
780-
self = NULL;
781767
return ret;
782768
}
783-
769+
// lifted from ffmpeg-6.1.1/doc/examples/vaapi_encode.c line 152
784770
/* set hw_frames_ctx for encoder's AVCodecContext */
785-
if ((ret = set_hwframe_ctx(avctx, self->hw_frame_ref)) < 0) {
771+
if ((ret = set_hwframe_ctx(avctx, ctx->hw_device_octx)) < 0) {
786772
tvherror(LS_VAAPI, "Encode: Failed to set hwframe context."
787773
"Error code: %s",av_err2str(ret));
788-
// unref hw_frame_ref
789-
av_buffer_unref(&self->hw_frame_ref);
790-
self->hw_frame_ref = NULL;
791-
free(self);
792-
self = NULL;
793-
return ret;
774+
av_buffer_unref(&ctx->hw_device_octx);
794775
}
795-
ctx->hw_device_octx = av_buffer_ref(self->hw_frame_ref);
796-
free(self);
797-
self = NULL;
776+
return ret;
777+
}
798778
#else
799779
vaapi_encode_setup_context(AVCodecContext *avctx, int low_power)
800780
{
@@ -818,9 +798,10 @@ vaapi_encode_setup_context(AVCodecContext *avctx, int low_power)
818798
return AVERROR(ENOMEM);
819799
}
820800
tvhva_context_destroy(hwaccel_context);
821-
#endif
801+
822802
return 0;
823803
}
804+
#endif
824805

825806

826807
void

0 commit comments

Comments
 (0)