Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenBaert committed Dec 18, 2012
1 parent 064aa04 commit bf4174d
Show file tree
Hide file tree
Showing 61 changed files with 8,261 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.so
*.o
*.user
74 changes: 74 additions & 0 deletions AV/AVWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright (c) 2012 Maarten Baert <maarten-baert@hotmail.com>
This file is part of SimpleScreenRecorder.
SimpleScreenRecorder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SimpleScreenRecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "AVWrapper.h"

int lock_manager(void** m, AVLockOp op) {
QMutex *&mutex = *(QMutex**) m;
switch(op) {
case AV_LOCK_CREATE: mutex = new QMutex(); break;
case AV_LOCK_DESTROY: delete mutex; break;
case AV_LOCK_OBTAIN: mutex->lock(); break;
case AV_LOCK_RELEASE: mutex->unlock(); break;
}
return 0;
}

class AVGlobal {
public:
AVGlobal() {
av_register_all();
avdevice_register_all();
av_lockmgr_register(&lock_manager);
}
} g_av_global;

AVFrameWrapper::AVFrameWrapper() {
avcodec_get_frame_defaults(this);
}

AVFrameWrapper::AVFrameWrapper(size_t size) {
avcodec_get_frame_defaults(this);
data[0] = (uint8_t*) av_malloc(size);
if(data[0] == NULL) {
throw std::bad_alloc();
}
}

AVFrameWrapper::~AVFrameWrapper() {
av_free(data[0]);
}

AVPacketWrapper::AVPacketWrapper() {
av_init_packet(this);
data = NULL;
size = 0;
}

AVPacketWrapper::AVPacketWrapper(size_t size) {
if(av_new_packet(this, size) != 0) {
throw std::bad_alloc();
}
}

AVPacketWrapper::~AVPacketWrapper() {
av_free_packet(this);
}

39 changes: 39 additions & 0 deletions AV/AVWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright (c) 2012 Maarten Baert <maarten-baert@hotmail.com>
This file is part of SimpleScreenRecorder.
SimpleScreenRecorder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SimpleScreenRecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#include "StdAfx.h"

class AVFrameWrapper : public AVFrame {
public:
AVFrameWrapper();
AVFrameWrapper(size_t size);
AVFrameWrapper(const AVFrameWrapper& other) = delete;
AVFrameWrapper* operator=(const AVFrameWrapper& other) = delete;
~AVFrameWrapper();
};

class AVPacketWrapper : public AVPacket {
public:
AVPacketWrapper();
AVPacketWrapper(size_t size);
AVPacketWrapper(const AVPacketWrapper& other) = delete;
AVPacketWrapper* operator=(const AVPacketWrapper& other) = delete;
~AVPacketWrapper();
};
86 changes: 86 additions & 0 deletions AV/Encoder/AudioEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright (c) 2012 Maarten Baert <maarten-baert@hotmail.com>
This file is part of SimpleScreenRecorder.
SimpleScreenRecorder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SimpleScreenRecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "AudioEncoder.h"

#include "Logger.h"
#include "AVWrapper.h"
#include "Muxer.h"

AudioEncoder::AudioEncoder(Logger* logger, Muxer* muxer, const QString& codec_name, const std::vector<std::pair<QString, QString> >& codec_options,
unsigned int bit_rate, unsigned int sample_rate)
: BaseEncoder(logger, muxer) {

m_bit_rate = bit_rate;
m_sample_rate = sample_rate;

// start the encoder
AVDictionary *options = NULL;
try {
for(unsigned int i = 0; i < codec_options.size(); ++i) {
av_dict_set(&options, qPrintable(codec_options[i].first), qPrintable(codec_options[i].second), 0);
}
CreateCodec(qPrintable(codec_name), &options);
av_dict_free(&options);
} catch(...) {
av_dict_free(&options);
throw;
}

}

unsigned int AudioEncoder::GetRequiredFrameSize() {
return (GetCodecContext()->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)? 1024 : GetCodecContext()->frame_size;
}

void AudioEncoder::FillCodecContext() {

GetCodecContext()->bit_rate = m_bit_rate;
GetCodecContext()->sample_rate = m_sample_rate;
GetCodecContext()->channels = 2;
GetCodecContext()->sample_fmt = AV_SAMPLE_FMT_S16;

}

bool AudioEncoder::EncodeFrame(AVFrame* frame) {

// allocate a packet
std::unique_ptr<AVPacketWrapper> packet(new AVPacketWrapper());

// encode the frame
int got_packet;
if(avcodec_encode_audio2(GetCodecContext(), packet.get(), frame, &got_packet) < 0) {
GetLogger()->LogError("[AudioEncoder::EncodeFrame] Error: Encoding of audio frame failed!");
throw LibavException();
}

// do we have a packet?
if(got_packet) {

// send the packet to the muxer
GetMuxer()->AddPacket(GetStreamIndex(), std::move(packet));
return true;

} else {
return false;
}

}

45 changes: 45 additions & 0 deletions AV/Encoder/AudioEncoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright (c) 2012 Maarten Baert <maarten-baert@hotmail.com>
This file is part of SimpleScreenRecorder.
SimpleScreenRecorder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SimpleScreenRecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#include "StdAfx.h"

#include "BaseEncoder.h"

class AudioEncoder : public BaseEncoder {

private:
unsigned int m_bit_rate;
unsigned int m_sample_rate;

public:
AudioEncoder(Logger* logger, Muxer* muxer, const QString& codec_name, const std::vector<std::pair<QString, QString> >& codec_options,
unsigned int bit_rate, unsigned int sample_rate);

// Returns the required frame size, i.e. the number of samples. A sample means 2 bytes for mono
// or 4 bytes for stereo. Currently only stereo is supported.
unsigned int GetRequiredFrameSize();

inline unsigned int GetSampleRate() { return m_sample_rate; }

private:
virtual void FillCodecContext();
virtual bool EncodeFrame(AVFrame* frame);

};
Loading

0 comments on commit bf4174d

Please sign in to comment.