Browse Source

obs-ffmpeg: Fix AVFrame handling in FFmpeg output

Fix an issue in the way AVFrames were handled in the FFmpeg
encoder plugin, which could lead to tearing in the encoded
video due to data races in the AVFrame and AVBuffer.

This is fixed by calling av_frame_make_writable which ensures
the frame and its associated buffer are writable. If its not,
it will copy the AVFrame, create a new AVBuffer for it and
decrease the refcount of the old AVFrame and AVBuffer.
This way OBS always ends up with a usable buffer to write into
which is not still used by the encoder while avoiding a copy
when unnecessary.
Marvin Scholz 5 years ago
parent
commit
9ef263f81a
1 changed files with 9 additions and 0 deletions
  1. 9 0
      plugins/obs-ffmpeg/obs-ffmpeg-output.c

+ 9 - 0
plugins/obs-ffmpeg/obs-ffmpeg-output.c

@@ -699,6 +699,15 @@ static void receive_video(void *param, struct video_data *frame)
 	if (!data->start_timestamp)
 		data->start_timestamp = frame->timestamp;
 
+	ret = av_frame_make_writable(data->vframe);
+	if (ret < 0) {
+		blog(LOG_WARNING,
+		     "receive_video: Error obtaining writable "
+		     "AVFrame: %s",
+		     av_err2str(ret));
+		//FIXME: stop the encode with an error
+		return;
+	}
 	if (!!data->swscale)
 		sws_scale(data->swscale, (const uint8_t *const *)frame->data,
 			  (const int *)frame->linesize, 0, data->config.height,