Просмотр исходного кода

obs-ffmpeg: Replace circlebuf with deque

derrod 2 лет назад
Родитель
Сommit
f083d14143

+ 17 - 17
plugins/obs-ffmpeg/ffmpeg-mux/ffmpeg-mux.c

@@ -28,7 +28,7 @@
 
 #include <util/threading.h>
 #include <util/platform.h>
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/dstr.h>
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -132,7 +132,7 @@ struct io_buffer {
 	pthread_t io_thread;
 	pthread_mutex_t data_mutex;
 	FILE *output_file;
-	struct circlebuf data;
+	struct deque data;
 	uint64_t next_pos;
 };
 
@@ -207,7 +207,7 @@ static void ffmpeg_mux_free(struct ffmpeg_mux *ffm)
 		av_write_trailer(ffm->output);
 	}
 
-	// If we're writing to a file with the circlebuf, shut it
+	// If we're writing to a file with the deque, shut it
 	// down gracefully
 	if (ffm->io.active) {
 		os_atomic_set_bool(&ffm->io.shutdown_requested, true);
@@ -224,7 +224,7 @@ static void ffmpeg_mux_free(struct ffmpeg_mux *ffm)
 
 		pthread_mutex_destroy(&ffm->io.data_mutex);
 
-		circlebuf_free(&ffm->io.data);
+		deque_free(&ffm->io.data);
 	}
 
 	free_avformat(ffm);
@@ -750,7 +750,7 @@ static void *ffmpeg_mux_io_thread(void *data)
 
 			pthread_mutex_lock(&ffm->io.data_mutex);
 
-			// Fetch as many writes as possible from the circlebuf
+			// Fetch as many writes as possible from the deque
 			// and fill up our local chunk. This may involve seeking
 			// if ffmpeg needs to, so take care of that as well.
 			for (;;) {
@@ -763,8 +763,8 @@ static void *ffmpeg_mux_io_thread(void *data)
 
 				// Get seek offset and data size
 				struct io_header header;
-				circlebuf_peek_front(&ffm->io.data, &header,
-						     sizeof(header));
+				deque_peek_front(&ffm->io.data, &header,
+						 sizeof(header));
 
 				// Do we need to seek?
 				if (header.seek_offset !=
@@ -796,13 +796,13 @@ static void *ffmpeg_mux_io_thread(void *data)
 				}
 
 				// Remove header that we already read
-				circlebuf_pop_front(&ffm->io.data, NULL,
-						    sizeof(header));
+				deque_pop_front(&ffm->io.data, NULL,
+						sizeof(header));
 
 				// Copy from the buffer to our local chunk
-				circlebuf_pop_front(&ffm->io.data,
-						    chunk + chunk_used,
-						    header.data_length);
+				deque_pop_front(&ffm->io.data,
+						chunk + chunk_used,
+						header.data_length);
 
 				// Update offsets
 				chunk_used += header.data_length;
@@ -897,7 +897,7 @@ static int ffmpeg_mux_write_av_buffer(void *opaque, uint8_t *buf, int buf_size)
 	for (;;) {
 		pthread_mutex_lock(&ffm->io.data_mutex);
 
-		// Avoid unbounded growth of the circlebuf, cap to 256 MB
+		// Avoid unbounded growth of the deque, cap to 256 MB
 		if (ffm->io.data.capacity >= 256 * 1048576 &&
 		    ffm->io.data.capacity - ffm->io.data.size <
 			    buf_size + sizeof(struct io_header)) {
@@ -916,8 +916,8 @@ static int ffmpeg_mux_write_av_buffer(void *opaque, uint8_t *buf, int buf_size)
 	header.seek_offset = ffm->io.next_pos;
 
 	// Copy the data into the buffer
-	circlebuf_push_back(&ffm->io.data, &header, sizeof(header));
-	circlebuf_push_back(&ffm->io.data, buf, buf_size);
+	deque_push_back(&ffm->io.data, &header, sizeof(header));
+	deque_push_back(&ffm->io.data, buf, buf_size);
 
 	// Advance the next write position
 	ffm->io.next_pos += buf_size;
@@ -941,7 +941,7 @@ static inline int open_output_file(struct ffmpeg_mux *ffm)
 
 	if ((format->flags & AVFMT_NOFILE) == 0) {
 		if (!ffmpeg_mux_is_network(ffm)) {
-			// If not outputting to a network, write to a circlebuf
+			// If not outputting to a network, write to a deque
 			// instead of relying on ffmpeg disk output. This hopefully
 			// works around too small buffers somewhere causing output
 			// stalls when recording.
@@ -958,7 +958,7 @@ static inline int open_output_file(struct ffmpeg_mux *ffm)
 			// Start at 1MB, this can grow up to 256 MB depending
 			// how fast data is going in and out (limited in
 			// ffmpeg_mux_write_av_buffer)
-			circlebuf_reserve(&ffm->io.data, 1048576);
+			deque_reserve(&ffm->io.data, 1048576);
 
 			pthread_mutex_init(&ffm->io.data_mutex, NULL);
 

+ 1 - 1
plugins/obs-ffmpeg/obs-ffmpeg-audio-encoders.c

@@ -16,7 +16,7 @@
 ******************************************************************************/
 
 #include <util/base.h>
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/darray.h>
 #include <util/dstr.h>
 #include <obs-module.h>

+ 10 - 10
plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c

@@ -35,7 +35,7 @@ void ffmpeg_hls_mux_destroy(void *data)
 		os_event_destroy(stream->stop_event);
 
 		da_free(stream->mux_packets);
-		circlebuf_free(&stream->packets);
+		deque_free(&stream->packets);
 
 		os_process_pipe_destroy(stream->pipe);
 		dstr_free(&stream->path);
@@ -77,7 +77,7 @@ static bool process_packet(struct ffmpeg_muxer *stream)
 	pthread_mutex_lock(&stream->write_mutex);
 
 	if (stream->packets.size) {
-		circlebuf_pop_front(&stream->packets, &packet, sizeof(packet));
+		deque_pop_front(&stream->packets, &packet, sizeof(packet));
 		has_packet = true;
 	}
 
@@ -180,33 +180,33 @@ bool ffmpeg_hls_mux_start(void *data)
 static bool write_packet_to_buf(struct ffmpeg_muxer *stream,
 				struct encoder_packet *packet)
 {
-	circlebuf_push_back(&stream->packets, packet,
-			    sizeof(struct encoder_packet));
+	deque_push_back(&stream->packets, packet,
+			sizeof(struct encoder_packet));
 	return true;
 }
 
 static void drop_frames(struct ffmpeg_muxer *stream, int highest_priority)
 {
-	struct circlebuf new_buf = {0};
+	struct deque new_buf = {0};
 	int num_frames_dropped = 0;
 
-	circlebuf_reserve(&new_buf, sizeof(struct encoder_packet) * 8);
+	deque_reserve(&new_buf, sizeof(struct encoder_packet) * 8);
 
 	while (stream->packets.size) {
 		struct encoder_packet packet;
-		circlebuf_pop_front(&stream->packets, &packet, sizeof(packet));
+		deque_pop_front(&stream->packets, &packet, sizeof(packet));
 
 		/* do not drop audio data or video keyframes */
 		if (packet.type == OBS_ENCODER_AUDIO ||
 		    packet.drop_priority >= highest_priority) {
-			circlebuf_push_back(&new_buf, &packet, sizeof(packet));
+			deque_push_back(&new_buf, &packet, sizeof(packet));
 		} else {
 			num_frames_dropped++;
 			obs_encoder_packet_release(&packet);
 		}
 	}
 
-	circlebuf_free(&stream->packets);
+	deque_free(&stream->packets);
 	stream->packets = new_buf;
 
 	if (stream->min_priority < highest_priority)
@@ -222,7 +222,7 @@ static bool find_first_video_packet(struct ffmpeg_muxer *stream,
 
 	for (size_t i = 0; i < count; i++) {
 		struct encoder_packet *cur =
-			circlebuf_data(&stream->packets, i * sizeof(*first));
+			deque_data(&stream->packets, i * sizeof(*first));
 		if (cur->type == OBS_ENCODER_VIDEO && !cur->keyframe) {
 			*first = *cur;
 			return true;

+ 2 - 2
plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c

@@ -16,7 +16,7 @@
 ******************************************************************************/
 
 #include <obs-module.h>
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/threading.h>
 #include <util/dstr.h>
 #include <util/darray.h>
@@ -568,7 +568,7 @@ static void close_audio(struct ffmpeg_data *data)
 {
 	for (int idx = 0; idx < data->num_audio_streams; idx++) {
 		for (size_t i = 0; i < MAX_AV_PLANES; i++)
-			circlebuf_free(&data->excess_frames[idx][i]);
+			deque_free(&data->excess_frames[idx][i]);
 
 		if (data->samples[idx][0])
 			av_freep(&data->samples[idx][0]);

+ 10 - 11
plugins/obs-ffmpeg/obs-ffmpeg-mux.c

@@ -49,11 +49,11 @@ static inline void replay_buffer_clear(struct ffmpeg_muxer *stream)
 {
 	while (stream->packets.size > 0) {
 		struct encoder_packet pkt;
-		circlebuf_pop_front(&stream->packets, &pkt, sizeof(pkt));
+		deque_pop_front(&stream->packets, &pkt, sizeof(pkt));
 		obs_encoder_packet_release(&pkt);
 	}
 
-	circlebuf_free(&stream->packets);
+	deque_free(&stream->packets);
 	stream->cur_size = 0;
 	stream->cur_time = 0;
 	stream->max_size = 0;
@@ -72,7 +72,7 @@ static void ffmpeg_mux_destroy(void *data)
 	for (size_t i = 0; i < stream->mux_packets.num; i++)
 		obs_encoder_packet_release(&stream->mux_packets.array[i]);
 	da_free(stream->mux_packets);
-	circlebuf_free(&stream->packets);
+	deque_free(&stream->packets);
 
 	os_process_pipe_destroy(stream->pipe);
 	dstr_free(&stream->path);
@@ -521,8 +521,8 @@ int deactivate(struct ffmpeg_muxer *stream, int code)
 
 		while (stream->packets.size) {
 			struct encoder_packet packet;
-			circlebuf_pop_front(&stream->packets, &packet,
-					    sizeof(packet));
+			deque_pop_front(&stream->packets, &packet,
+					sizeof(packet));
 			obs_encoder_packet_release(&packet);
 		}
 
@@ -1064,7 +1064,7 @@ static bool purge_front(struct ffmpeg_muxer *stream)
 	if (!stream->packets.size)
 		return false;
 
-	circlebuf_pop_front(&stream->packets, &pkt, sizeof(pkt));
+	deque_pop_front(&stream->packets, &pkt, sizeof(pkt));
 
 	keyframe = pkt.type == OBS_ENCODER_VIDEO && pkt.keyframe;
 
@@ -1076,7 +1076,7 @@ static bool purge_front(struct ffmpeg_muxer *stream)
 		stream->cur_time = 0;
 	} else {
 		struct encoder_packet first;
-		circlebuf_peek_front(&stream->packets, &first, sizeof(first));
+		deque_peek_front(&stream->packets, &first, sizeof(first));
 		stream->cur_time = first.dts_usec;
 		stream->cur_size -= (int64_t)pkt.size;
 	}
@@ -1093,8 +1093,7 @@ static inline void purge(struct ffmpeg_muxer *stream)
 		for (;;) {
 			if (!stream->packets.size)
 				return;
-			circlebuf_peek_front(&stream->packets, &pkt,
-					     sizeof(pkt));
+			deque_peek_front(&stream->packets, &pkt, sizeof(pkt));
 			if (pkt.type == OBS_ENCODER_VIDEO && pkt.keyframe)
 				return;
 
@@ -1223,7 +1222,7 @@ static void replay_buffer_save(struct ffmpeg_muxer *stream)
 
 	for (size_t i = 0; i < num_packets; i++) {
 		struct encoder_packet *pkt;
-		pkt = circlebuf_data(&stream->packets, i * size);
+		pkt = deque_data(&stream->packets, i * size);
 
 		if (pkt->type == OBS_ENCODER_VIDEO) {
 			if (!found_video) {
@@ -1299,7 +1298,7 @@ static void replay_buffer_data(void *data, struct encoder_packet *packet)
 		stream->cur_time = pkt.dts_usec;
 	stream->cur_size += pkt.size;
 
-	circlebuf_push_back(&stream->packets, packet, sizeof(*packet));
+	deque_push_back(&stream->packets, packet, sizeof(*packet));
 
 	if (packet->type == OBS_ENCODER_VIDEO && packet->keyframe)
 		stream->keyframes++;

+ 2 - 2
plugins/obs-ffmpeg/obs-ffmpeg-mux.h

@@ -2,7 +2,7 @@
 
 #include <obs-module.h>
 #include <obs-hotkey.h>
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/darray.h>
 #include <util/dstr.h>
 #include <util/pipe.h>
@@ -49,7 +49,7 @@ struct ffmpeg_muxer {
 	/* these are accessed both by replay buffer and by HLS */
 	pthread_t mux_thread;
 	bool mux_thread_joinable;
-	struct circlebuf packets;
+	struct deque packets;
 
 	/* HLS only */
 	int keyint_sec;

+ 7 - 8
plugins/obs-ffmpeg/obs-ffmpeg-output.c

@@ -16,7 +16,7 @@
 ******************************************************************************/
 
 #include <obs-module.h>
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/threading.h>
 #include <util/dstr.h>
 #include <util/darray.h>
@@ -515,7 +515,7 @@ static void close_audio(struct ffmpeg_data *data)
 {
 	for (int idx = 0; idx < data->num_audio_streams; idx++) {
 		for (size_t i = 0; i < MAX_AV_PLANES; i++)
-			circlebuf_free(&data->excess_frames[idx][i]);
+			deque_free(&data->excess_frames[idx][i]);
 
 		if (data->samples[idx][0])
 			av_freep(&data->samples[idx][0]);
@@ -961,15 +961,14 @@ static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
 	frame_size_bytes = (size_t)data->frame_size * data->audio_size;
 
 	for (size_t i = 0; i < data->audio_planes; i++)
-		circlebuf_push_back(&data->excess_frames[track_order][i],
-				    in.data[i], in.frames * data->audio_size);
+		deque_push_back(&data->excess_frames[track_order][i],
+				in.data[i], in.frames * data->audio_size);
 
 	while (data->excess_frames[track_order][0].size >= frame_size_bytes) {
 		for (size_t i = 0; i < data->audio_planes; i++)
-			circlebuf_pop_front(
-				&data->excess_frames[track_order][i],
-				data->samples[track_order][i],
-				frame_size_bytes);
+			deque_pop_front(&data->excess_frames[track_order][i],
+					data->samples[track_order][i],
+					frame_size_bytes);
 
 		encode_audio(output, track_order, context, data->audio_size);
 	}

+ 1 - 1
plugins/obs-ffmpeg/obs-ffmpeg-output.h

@@ -73,7 +73,7 @@ struct ffmpeg_data {
 
 	/* audio_tracks is a bitmask storing the indices of the mixes */
 	int audio_tracks;
-	struct circlebuf excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
+	struct deque excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
 	uint8_t *samples[MAX_AUDIO_MIXES][MAX_AV_PLANES];
 	AVFrame *aframe[MAX_AUDIO_MIXES];
 

+ 5 - 5
plugins/obs-ffmpeg/obs-nvenc.c

@@ -1,5 +1,5 @@
 #include "obs-nvenc.h"
-#include <util/circlebuf.h>
+#include <util/deque.h>
 #include <util/darray.h>
 #include <util/dstr.h>
 #include <obs-avc.h>
@@ -109,7 +109,7 @@ struct nvenc_data {
 	DARRAY(struct nv_bitstream) bitstreams;
 	DARRAY(struct nv_texture) textures;
 	DARRAY(struct handle_tex) input_textures;
-	struct circlebuf dts_list;
+	struct deque dts_list;
 
 	DARRAY(uint8_t) packet_data;
 	int64_t packet_pts;
@@ -1253,7 +1253,7 @@ static void nvenc_destroy(void *data)
 
 	bfree(enc->header);
 	bfree(enc->sei);
-	circlebuf_free(&enc->dts_list);
+	deque_free(&enc->dts_list);
 	da_free(enc->textures);
 	da_free(enc->bitstreams);
 	da_free(enc->input_textures);
@@ -1509,7 +1509,7 @@ static bool nvenc_encode_tex(void *data, uint32_t handle, int64_t pts,
 		return false;
 	}
 
-	circlebuf_push_back(&enc->dts_list, &pts, sizeof(pts));
+	deque_push_back(&enc->dts_list, &pts, sizeof(pts));
 
 	/* ------------------------------------ */
 	/* copy to output tex                   */
@@ -1579,7 +1579,7 @@ static bool nvenc_encode_tex(void *data, uint32_t handle, int64_t pts,
 
 	if (enc->packet_data.num) {
 		int64_t dts;
-		circlebuf_pop_front(&enc->dts_list, &dts, sizeof(dts));
+		deque_pop_front(&enc->dts_list, &dts, sizeof(dts));
 
 		/* subtract bframe delay from dts */
 		dts -= (int64_t)enc->bframes * packet->timebase_num;