fixed various audio issues
This commit is contained in:
+6
-27
@@ -13,24 +13,11 @@ QAudioOutput* audio_output;
|
||||
QIODevice* audio_io_device;
|
||||
bool audio_device_set = false;
|
||||
|
||||
uint8_t* audio_cache_A = NULL;
|
||||
uint8_t* audio_cache_B = NULL;
|
||||
int audio_cache_size = 0;
|
||||
bool switch_audio_cache = true;
|
||||
bool reading_audio_cache_A = false;
|
||||
int audio_bytes_written = 0;
|
||||
qint8 audio_ibuffer[audio_ibuffer_size];
|
||||
int audio_ibuffer_read = 0;
|
||||
QVector<int> audio_ibuffer_write;
|
||||
|
||||
void init_audio(Sequence* s) {
|
||||
if (audio_cache_A != NULL) {
|
||||
delete [] audio_cache_A;
|
||||
audio_cache_A = NULL;
|
||||
}
|
||||
|
||||
if (audio_cache_B != NULL) {
|
||||
delete [] audio_cache_B;
|
||||
audio_cache_B = NULL;
|
||||
}
|
||||
|
||||
if (audio_device_set) {
|
||||
audio_output->stop();
|
||||
delete audio_output;
|
||||
@@ -55,19 +42,11 @@ void init_audio(Sequence* s) {
|
||||
audio_io_device = audio_output->start();
|
||||
audio_device_set = true;
|
||||
|
||||
audio_cache_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(s->audio_layout), s->audio_frequency/8, AV_SAMPLE_FMT_S16, 1);
|
||||
audio_cache_A = new uint8_t[audio_cache_size];
|
||||
audio_cache_B = new uint8_t[audio_cache_size];
|
||||
clear_cache(true, true);
|
||||
clear_audio_ibuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clear_cache(bool clear_A, bool clear_B) {
|
||||
if (clear_A) {
|
||||
memset(audio_cache_A, 0, audio_cache_size);
|
||||
}
|
||||
if (clear_B) {
|
||||
memset(audio_cache_B, 0, audio_cache_size);
|
||||
}
|
||||
void clear_audio_ibuffer() {
|
||||
memset(audio_ibuffer, 0, audio_ibuffer_size);
|
||||
}
|
||||
|
||||
+7
-8
@@ -1,7 +1,7 @@
|
||||
#ifndef AUDIO_H
|
||||
#define AUDIO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <QVector>
|
||||
|
||||
class QAudioOutput;
|
||||
class QIODevice;
|
||||
@@ -11,13 +11,12 @@ struct Sequence;
|
||||
extern QAudioOutput* audio_output;
|
||||
extern QIODevice* audio_io_device;
|
||||
|
||||
extern uint8_t* audio_cache_A;
|
||||
extern uint8_t* audio_cache_B;
|
||||
extern int audio_cache_size;
|
||||
extern int audio_bytes_written;
|
||||
extern bool switch_audio_cache;
|
||||
extern bool reading_audio_cache_A;
|
||||
#define audio_ibuffer_size 192000
|
||||
extern qint8 audio_ibuffer[audio_ibuffer_size];
|
||||
extern int audio_ibuffer_read;
|
||||
//extern QVector<int> audio_ibuffer_write;
|
||||
void clear_audio_ibuffer();
|
||||
|
||||
void init_audio(Sequence* s);
|
||||
void clear_cache(bool clear_A, bool clear_B);
|
||||
|
||||
#endif // AUDIO_H
|
||||
|
||||
+61
-45
@@ -16,46 +16,67 @@ extern "C" {
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
void cache_audio_worker(Clip* c, bool write_A) {
|
||||
// gets one frame worth of audio and sends it to the audio buffer
|
||||
AVFrame* frame = c->cache_A.frames[0];
|
||||
uint8_t* cache = (write_A) ? audio_cache_A : audio_cache_B;
|
||||
void cache_audio_worker(Clip* c) {
|
||||
int written = 0;
|
||||
int max_write = 16384;
|
||||
while (written < max_write) {
|
||||
// gets one frame worth of audio and sends it to the audio buffer
|
||||
AVFrame* frame = c->cache_A.frames[0];
|
||||
|
||||
int bytes_written = 0;
|
||||
int j = 0;
|
||||
while (bytes_written < audio_cache_size && !c->reached_end) {
|
||||
// is there audio left in the frame
|
||||
if (c->frame_sample_index == 0) {
|
||||
// no more audio left in frame, get a new one
|
||||
retrieve_next_frame_raw_data(c, frame);
|
||||
}
|
||||
if (!c->reached_end) {
|
||||
int nb_bytes = av_samples_get_buffer_size(NULL, frame->channels, frame->nb_samples, static_cast<AVSampleFormat>(frame->format), 1);
|
||||
int limit = std::min(c->frame_sample_index + audio_cache_size - bytes_written, nb_bytes);
|
||||
// perform all audio effects
|
||||
if (c->frame_sample_index == 0) {
|
||||
// no more audio left in frame, get a new one
|
||||
if (!c->reached_end) {
|
||||
retrieve_next_frame_raw_data(c, frame);
|
||||
} else {
|
||||
// set by retrieve_next_frame_raw_data indicating no more frames in file,
|
||||
// but there still may be samples in swresample
|
||||
swr_convert_frame(c->swr_ctx, frame, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (frame->nb_samples == 0) {
|
||||
written = max_write;
|
||||
} else {
|
||||
int nb_bytes = av_samples_get_buffer_size(NULL, frame->channels, frame->nb_samples, static_cast<AVSampleFormat>(frame->format), 1);
|
||||
|
||||
// perform all audio effects
|
||||
for (int j=0;j<c->effects.size();j++) {
|
||||
c->effects.at(j)->process_audio(frame->data[0], limit);
|
||||
}
|
||||
// mix audio into cache
|
||||
for (int i=c->frame_sample_index;i<limit;i++) {
|
||||
cache[j] += frame->data[0][i];
|
||||
j++;
|
||||
}
|
||||
bytes_written += limit - c->frame_sample_index;
|
||||
c->frame_sample_index = (limit == nb_bytes) ? 0 : limit;
|
||||
}
|
||||
}
|
||||
c->effects.at(j)->process_audio(frame->data[0], nb_bytes);
|
||||
}
|
||||
|
||||
if (c->audio_buffer_write == 0) c->audio_buffer_write = audio_ibuffer_read + 1024;
|
||||
while (c->frame_sample_index < nb_bytes) {
|
||||
if (c->audio_buffer_write >= audio_ibuffer_read+(audio_ibuffer_size/2)) {
|
||||
written = max_write;
|
||||
break;
|
||||
} else {
|
||||
audio_ibuffer[c->audio_buffer_write%audio_ibuffer_size] += frame->data[0][c->frame_sample_index];
|
||||
c->audio_buffer_write++;
|
||||
c->frame_sample_index++;
|
||||
}
|
||||
}
|
||||
written += c->frame_sample_index;
|
||||
if (c->frame_sample_index == nb_bytes) {
|
||||
c->frame_sample_index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cache_video_worker(Clip* c, long playhead, ClipCache* cache) {
|
||||
cache->mutex.lock();
|
||||
cache->mutex.lock();
|
||||
|
||||
cache->offset = playhead;
|
||||
for (size_t i=0;i<c->cache_size;i++) {
|
||||
retrieve_next_frame_raw_data(c, cache->frames[i]);
|
||||
}
|
||||
cache->written = true;
|
||||
cache->unread = true;
|
||||
cache->offset = playhead;
|
||||
if (!c->reached_end) {
|
||||
for (size_t i=0;i<c->cache_size;i++) {
|
||||
retrieve_next_frame_raw_data(c, cache->frames[i]);
|
||||
if (c->reached_end) break;
|
||||
}
|
||||
}
|
||||
cache->written = true;
|
||||
cache->unread = true;
|
||||
// setting the cache to written even if it hasn't reached_end prevents playback from
|
||||
// signaling a seek/reset because it wasn't able to find the frame
|
||||
|
||||
cache->mutex.unlock();
|
||||
}
|
||||
@@ -81,7 +102,7 @@ void reset_cache(Clip* c, long target_frame) {
|
||||
// play up to the frame we actually want
|
||||
long retrieved_frame = 0;
|
||||
AVFrame* temp = av_frame_alloc();
|
||||
do {
|
||||
do {
|
||||
retrieve_next_frame(c, temp);
|
||||
if (retrieved_frame == 0) {
|
||||
if (target_frame != 0) {
|
||||
@@ -223,7 +244,7 @@ void open_clip_worker(Clip* clip) {
|
||||
clip->sequence->audio_layout,
|
||||
static_cast<AVSampleFormat>(sample_format),
|
||||
clip->sequence->audio_frequency,
|
||||
clip->stream->codecpar->channel_layout,
|
||||
clip->codecCtx->channel_layout,
|
||||
static_cast<AVSampleFormat>(clip->stream->codecpar->format),
|
||||
clip->stream->codecpar->sample_rate,
|
||||
0,
|
||||
@@ -266,13 +287,8 @@ void cache_clip_worker(Clip* clip, long playhead, bool write_A, bool write_B, bo
|
||||
if (write_B) {
|
||||
cache_video_worker(clip, playhead, &clip->cache_B);
|
||||
}
|
||||
} else if (clip->stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||
if (write_A) {
|
||||
cache_audio_worker(clip, true);
|
||||
}
|
||||
if (write_B) {
|
||||
cache_audio_worker(clip, false);
|
||||
}
|
||||
} else if (clip->stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||
cache_audio_worker(clip);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,8 +339,8 @@ void Cacher::run() {
|
||||
|
||||
caching = true;
|
||||
while (true) {
|
||||
clip->can_cache.wait(&clip->lock);
|
||||
if (!caching) {
|
||||
clip->can_cache.wait(&clip->lock);
|
||||
if (!caching) {
|
||||
break;
|
||||
} else {
|
||||
cache_clip_worker(clip, playhead, write_A, write_B, reset);
|
||||
|
||||
+25
-24
@@ -34,7 +34,7 @@ void handle_media(Sequence* sequence, long playhead, bool multithreaded) {
|
||||
// if thread is already working, we don't want to touch this,
|
||||
// but we also don't want to hang the UI thread
|
||||
if (!c.open) {
|
||||
if (c.lock.tryLock()) {
|
||||
if (c.lock.tryLock()) {
|
||||
open_clip(&c, multithreaded);
|
||||
|
||||
// add to current_clips, (insertion) sorted by track so composite them in order
|
||||
@@ -229,13 +229,10 @@ float clip_frame_to_seconds(Clip* c, long clip_frame) {
|
||||
|
||||
int retrieve_next_frame(Clip* c, AVFrame* f) {
|
||||
int result = 0;
|
||||
int receive_ret;
|
||||
|
||||
// could be optimized if "linked stream" shares timeline_in and clip_in, only use one read_frame?
|
||||
// depends how much time av_read_frame needs, the decoding is probably far more consuming anyway
|
||||
int receive_ret;
|
||||
|
||||
// do we need to retrieve a new packet for a new frame?
|
||||
while ((receive_ret = avcodec_receive_frame(c->codecCtx, f)) == AVERROR(EAGAIN)) {
|
||||
while ((receive_ret = avcodec_receive_frame(c->codecCtx, f)) == AVERROR(EAGAIN)) {
|
||||
int read_ret = 0;
|
||||
do {
|
||||
if (c->pkt_written) {
|
||||
@@ -249,10 +246,9 @@ int retrieve_next_frame(Clip* c, AVFrame* f) {
|
||||
int send_ret = avcodec_send_packet(c->codecCtx, c->pkt);
|
||||
if (send_ret < 0) {
|
||||
qDebug() << "[ERROR] Failed to send packet to decoder." << send_ret;
|
||||
result = send_ret;
|
||||
break;
|
||||
return send_ret;
|
||||
}
|
||||
} else {
|
||||
} else {
|
||||
if (read_ret != AVERROR_EOF) qDebug() << "[ERROR] Could not read frame." << read_ret;
|
||||
return read_ret; // skips trying to find a frame at all
|
||||
}
|
||||
@@ -266,21 +262,26 @@ int retrieve_next_frame(Clip* c, AVFrame* f) {
|
||||
}
|
||||
|
||||
void retrieve_next_frame_raw_data(Clip* c, AVFrame* output) {
|
||||
int ret = retrieve_next_frame(c, c->frame);
|
||||
if (ret >= 0) {
|
||||
if (c->stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
||||
sws_scale(c->sws_ctx, c->frame->data, c->frame->linesize, 0, c->stream->codecpar->height, output->data, output->linesize);
|
||||
} else if (c->stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||
ret = swr_convert_frame(c->swr_ctx, output, c->frame);
|
||||
if (ret < 0) {
|
||||
qDebug() << "[ERROR] Failed to resample audio." << ret;
|
||||
}
|
||||
}
|
||||
} else if (ret == AVERROR_EOF) {
|
||||
c->reached_end = true;
|
||||
} else {
|
||||
qDebug() << "[WARNING] Raw frame data could not be retrieved." << ret;
|
||||
}
|
||||
if (c->reached_end) {
|
||||
qDebug() << "[WARNING] Attempted to retrieve frame of stream with no frames left";
|
||||
} else {
|
||||
int ret = retrieve_next_frame(c, c->frame);
|
||||
if (ret >= 0) {
|
||||
if (c->stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
||||
sws_scale(c->sws_ctx, c->frame->data, c->frame->linesize, 0, c->stream->codecpar->height, output->data, output->linesize);
|
||||
} else if (c->stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||
ret = swr_convert_frame(c->swr_ctx, output, c->frame);
|
||||
if (ret < 0) {
|
||||
qDebug() << "[ERROR] Failed to resample audio." << ret;
|
||||
}
|
||||
}
|
||||
} else if (ret == AVERROR_EOF) {
|
||||
// qDebug() << "reached end triggered" << c->track;
|
||||
c->reached_end = true;
|
||||
} else {
|
||||
qDebug() << "[WARNING] Raw frame data could not be retrieved." << ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_clip_active(Clip* c, long playhead) {
|
||||
|
||||
Reference in New Issue
Block a user