Skip to content

Commit

Permalink
Use std::unique_ptr for THEORAPLAY_AudioPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Nov 11, 2024
1 parent 95b3234 commit 19f78a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/jngl/Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ class Video::Impl : public Stream {
audio->samples + static_cast<ptrdiff_t>(audio->frames * 2));
}
}
THEORAPLAY_freeAudio(audio);
audio = THEORAPLAY_getAudio(decoder);
}

Expand Down Expand Up @@ -324,7 +323,7 @@ class Video::Impl : public Stream {

THEORAPLAY_Decoder* decoder;
const THEORAPLAY_VideoFrame* video = nullptr;
const THEORAPLAY_AudioPacket* audio = nullptr;
std::unique_ptr<const THEORAPLAY_AudioPacket> audio;
double startTime;
double timePerFrame;

Expand Down
61 changes: 22 additions & 39 deletions src/theoraplay/theoraplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct THEORAPLAY_Decoder {
VideoFrame* videolist = nullptr;
VideoFrame* videolisttail = nullptr;

AudioPacket* audiolist = nullptr;
std::unique_ptr<AudioPacket> audiolist;
AudioPacket* audiolisttail = nullptr;

std::unique_ptr<uint8_t[]> ringBuffer;
Expand Down Expand Up @@ -295,10 +295,7 @@ void WorkerThread(THEORAPLAY_Decoder* const ctx) {
const int channels = vinfo.channels;
int chanidx, frameidx;
float* samples;
auto* item = static_cast<AudioPacket*>(malloc(sizeof(AudioPacket)));
if (item == nullptr) {
goto cleanup;
}
auto item = std::make_unique<AudioPacket>();
item->playms = static_cast<unsigned int>(
((static_cast<double>(audioframes)) / (static_cast<double>(vinfo.rate))) *
1000.0);
Expand All @@ -308,13 +305,11 @@ void WorkerThread(THEORAPLAY_Decoder* const ctx) {
item->samples = static_cast<float*>(malloc(sizeof(float) * frames * channels));
item->next = nullptr;

if (item->samples == nullptr)
{
free(item);
goto cleanup;
if (item->samples == nullptr) {
goto cleanup;
}

// I bet this beats the crap out of the CPU cache...
// I bet this beats the crap out of the CPU cache...
samples = item->samples;
for (frameidx = 0; frameidx < frames; frameidx++)
{
Expand All @@ -329,18 +324,19 @@ void WorkerThread(THEORAPLAY_Decoder* const ctx) {
//printf("Decoded %d frames of audio.\n", (int) frames);
ctx->lock.lock();
ctx->audioms += item->playms;
if (ctx->audiolisttail)
AudioPacket* const tail = item.get();
if (ctx->audiolisttail)
{
assert(ctx->audiolist);
ctx->audiolisttail->next = item;
ctx->audiolisttail->next = std::move(item);
}
else
{
assert(!ctx->audiolist);
ctx->audiolist = item;
ctx->audiolist = std::move(item);
}
ctx->audiolisttail = item;
ctx->lock.unlock();
ctx->audiolisttail = tail;
ctx->lock.unlock();
} else { // no audio available left in current packet?
// try to feed another packet to the Vorbis stream...
if (ogg_stream_packetout(&vstream, &packet) <= 0)
Expand Down Expand Up @@ -577,16 +573,7 @@ void THEORAPLAY_stopDecode(THEORAPLAY_Decoder* const ctx) {
videolist = next;
}

AudioPacket *audiolist = ctx->audiolist;
while (audiolist)
{
AudioPacket *next = audiolist->next;
free(audiolist->samples);
free(audiolist);
audiolist = next;
}

delete ctx;
delete ctx;
}

bool THEORAPLAY_isDecoding(THEORAPLAY_Decoder* const ctx) {
Expand Down Expand Up @@ -637,16 +624,16 @@ bool THEORAPLAY_decodingError(THEORAPLAY_Decoder* const decoder) {
GET_SYNCED_VALUE(bool, 0, decoder, decode_error);
}

const THEORAPLAY_AudioPacket* THEORAPLAY_getAudio(THEORAPLAY_Decoder* const ctx) {
AudioPacket *retval;
std::unique_ptr<const THEORAPLAY_AudioPacket> THEORAPLAY_getAudio(THEORAPLAY_Decoder* const ctx) {
std::unique_ptr<AudioPacket> retval;

ctx->lock.lock();
retval = ctx->audiolist;
if (retval)
ctx->lock.lock();
retval = std::move(ctx->audiolist);
if (retval)
{
ctx->audioms -= retval->playms;
ctx->audiolist = retval->next;
retval->next = nullptr;
ctx->audiolist = std::move(retval->next);
retval->next = nullptr;
if (ctx->audiolist == nullptr) {
ctx->audiolisttail = nullptr;
}
Expand All @@ -656,13 +643,9 @@ const THEORAPLAY_AudioPacket* THEORAPLAY_getAudio(THEORAPLAY_Decoder* const ctx)
return retval;
}

void THEORAPLAY_freeAudio(const THEORAPLAY_AudioPacket* item) {
if (item != nullptr)
{
assert(item->next == nullptr);
free(item->samples);
free(const_cast<THEORAPLAY_AudioPacket*>(item));
}
THEORAPLAY_AudioPacket::~THEORAPLAY_AudioPacket() noexcept {
assert(next == nullptr);
free(samples);
}

const THEORAPLAY_VideoFrame* THEORAPLAY_getVideo(THEORAPLAY_Decoder* const ctx) {
Expand Down
8 changes: 5 additions & 3 deletions src/theoraplay/theoraplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <cstdint>
#include <memory>

struct THEORAPLAY_Io
{
Expand Down Expand Up @@ -38,12 +39,14 @@ struct THEORAPLAY_VideoFrame {
};

struct THEORAPLAY_AudioPacket {
~THEORAPLAY_AudioPacket() noexcept;

unsigned int playms; /* playback start time in milliseconds. */
int channels;
int freq;
int frames;
float* samples; /* frames * channels float32 samples. */
struct THEORAPLAY_AudioPacket* next;
std::unique_ptr<THEORAPLAY_AudioPacket> next;
};

THEORAPLAY_Decoder* THEORAPLAY_startDecodeFile(const char* fname, unsigned int maxframes,
Expand All @@ -60,8 +63,7 @@ bool THEORAPLAY_hasAudioStream(THEORAPLAY_Decoder*);
unsigned int THEORAPLAY_availableVideo(THEORAPLAY_Decoder*);
unsigned int THEORAPLAY_availableAudio(THEORAPLAY_Decoder*);

const THEORAPLAY_AudioPacket *THEORAPLAY_getAudio(THEORAPLAY_Decoder*);
void THEORAPLAY_freeAudio(const THEORAPLAY_AudioPacket *item);
std::unique_ptr<const THEORAPLAY_AudioPacket> THEORAPLAY_getAudio(THEORAPLAY_Decoder*);

const THEORAPLAY_VideoFrame *THEORAPLAY_getVideo(THEORAPLAY_Decoder*);
void THEORAPLAY_freeVideo(const THEORAPLAY_VideoFrame *item);
Expand Down

0 comments on commit 19f78a3

Please sign in to comment.