From 57975b42877c1d9b983cd9924ea2ca4332c8156c Mon Sep 17 00:00:00 2001 From: rstarkov Date: Sun, 16 Oct 2016 17:38:04 +0100 Subject: [PATCH] Use #using "class.obj" instead of headers for managed classes. --- BasicFFEncode/BasicEncoder.cpp | 33 +++- BasicFFEncode/BasicEncoder.h | 30 ---- BasicFFEncode/BasicEncoderSettings.cpp | 72 +++++++- BasicFFEncode/BasicEncoderSettings.h | 70 -------- BasicFFEncode/BasicFFEncode.vcxproj | 9 +- BasicFFEncode/BasicFFEncode.vcxproj.filters | 5 +- BasicFFEncode/{Enums.h => Enums.cpp} | 10 +- BasicFFEncode/Frames.cpp | 178 ++++++++++++-------- BasicFFEncode/Frames.h | 37 ---- BasicFFEncode/Helpers.cpp | 2 +- 10 files changed, 225 insertions(+), 221 deletions(-) delete mode 100644 BasicFFEncode/BasicEncoder.h rename BasicFFEncode/{Enums.h => Enums.cpp} (99%) delete mode 100644 BasicFFEncode/Frames.h diff --git a/BasicFFEncode/BasicEncoder.cpp b/BasicFFEncode/BasicEncoder.cpp index c2c21fb..4adc39a 100644 --- a/BasicFFEncode/BasicEncoder.cpp +++ b/BasicFFEncode/BasicEncoder.cpp @@ -1,5 +1,5 @@ // Parts of this code are derived from the muxing.c example for FFmpeg: https://raw.githubusercontent.com/FFmpeg/FFmpeg/master/doc/examples/muxing.c -#include "stdafx.h" +#include "Stdafx.h" extern "C" { @@ -19,7 +19,14 @@ extern "C" #pragma comment(lib, "swscale.lib") #pragma comment(lib, "swresample.lib") -#include "BasicEncoder.h" +#include "Helpers.h" +#using "Frames.obj" +#using "Enums.obj" +#using "BasicEncoderSettings.obj" + +using namespace System; +using namespace System::Collections::Generic; +using namespace System::Runtime::InteropServices; @@ -57,6 +64,28 @@ class UnmanagedPrivates +namespace BasicFFEncode +{ + public ref class BasicEncoder + { + private: + String^ _filename; + UnmanagedPrivates* _priv; + + private: + static bool _initialised = false; + static void Initialise(); + + public: + BasicEncoder(String^ filename, BasicEncoderSettings^ settings); + ~BasicEncoder(); + void EncodeFrame(BasicVideoFrame^ frame, Int64 presentationTimestamp); + void EncodeFrame(BasicAudioFrame^ frame, Int64 presentationTimestamp); + }; +} + + + void BasicFFEncode::BasicEncoder::Initialise() { if (_initialised) diff --git a/BasicFFEncode/BasicEncoder.h b/BasicFFEncode/BasicEncoder.h deleted file mode 100644 index 50ed615..0000000 --- a/BasicFFEncode/BasicEncoder.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "Helpers.h" -#include "Frames.h" -#include "BasicEncoderSettings.h" - -using namespace System; -using namespace System::Collections::Generic; -using namespace System::Runtime::InteropServices; - -class UnmanagedPrivates; - -namespace BasicFFEncode -{ - public ref class BasicEncoder - { - private: - String^ _filename; - UnmanagedPrivates* _priv; - - private: - static bool _initialised = false; - static void Initialise(); - - public: - BasicEncoder(String^ filename, BasicEncoderSettings^ settings); - ~BasicEncoder(); - void EncodeFrame(BasicVideoFrame^ frame, Int64 presentationTimestamp); - void EncodeFrame(BasicAudioFrame^ frame, Int64 presentationTimestamp); - }; -} diff --git a/BasicFFEncode/BasicEncoderSettings.cpp b/BasicFFEncode/BasicEncoderSettings.cpp index c1979a2..1f7499b 100644 --- a/BasicFFEncode/BasicEncoderSettings.cpp +++ b/BasicFFEncode/BasicEncoderSettings.cpp @@ -1,2 +1,72 @@ #include "Stdafx.h" -#include "BasicEncoderSettings.h" + +#using "Frames.obj" +#using "Enums.obj" + +namespace BasicFFEncode +{ + using namespace System; + using namespace System::Collections::Generic; + + public value class Rational + { + private: + int _num, _den; + public: + property int Num { int get() { return _num; }} + property int Den { int get() { return _den; }} + Rational(int num, int den) : _num(num), _den(den) {} + virtual String^ ToString() override { return String::Format("{0}/{1} ({2})", Num, Den, (Decimal)Den / (Decimal)Num); } + }; + + public ref class BasicVideoEncoderSettings + { + public: + property BasicVideoCodecId CodecId; + property Rational Timebase; + property int Width; + property int Height; + property BasicPixelFormat PixelFormat; + property Rational PixelAspectRatio; + property UInt64 Bitrate; + property int GopSize; + }; + + public ref class BasicAudioEncoderSettings + { + public: + property BasicAudioCodecId CodecId; + property UInt64 Bitrate; + property int SampleRate; // may be updated by the BasicEncoder constructor to one of the supported sample rates + property int Channels; + property BasicSampleFormat SampleFormat; + property int FrameSize; + property BasicChannelLayout ChannelLayout; + }; + + public ref class BasicEncoderSettings + { + public: + property BasicVideoEncoderSettings^ Video; + property BasicAudioEncoderSettings^ Audio; + property Dictionary^ Options; + + BasicEncoderSettings() + { + Video = gcnew BasicVideoEncoderSettings(); + Video->Width = 640; + Video->Height = 480; + Video->PixelFormat = BasicPixelFormat::YUV420P; + Video->Bitrate = 400000; + Video->GopSize = 12; + Video->Timebase = Rational(1, 25); + Video->PixelAspectRatio = Rational(1, 1); + + Audio = gcnew BasicAudioEncoderSettings(); + Audio->Bitrate = 64000; + Audio->SampleRate = 44100; + + Options = gcnew Dictionary(); + } + }; +} diff --git a/BasicFFEncode/BasicEncoderSettings.h b/BasicFFEncode/BasicEncoderSettings.h index dc48b28..6f70f09 100644 --- a/BasicFFEncode/BasicEncoderSettings.h +++ b/BasicFFEncode/BasicEncoderSettings.h @@ -1,71 +1 @@ #pragma once -#include "Frames.h" -#include "Enums.h" - -namespace BasicFFEncode -{ - using namespace System; - using namespace System::Collections::Generic; - - public value class Rational - { - private: - int _num, _den; - public: - property int Num { int get() { return _num; }} - property int Den { int get() { return _den; }} - Rational(int num, int den) : _num(num), _den(den) {} - virtual String^ ToString() override { return String::Format("{0}/{1} ({2})", Num, Den, (Decimal) Den / (Decimal) Num); } - }; - - public ref class BasicVideoEncoderSettings - { - public: - property BasicVideoCodecId CodecId; - property Rational Timebase; - property int Width; - property int Height; - property BasicPixelFormat PixelFormat; - property Rational PixelAspectRatio; - property UInt64 Bitrate; - property int GopSize; - }; - - public ref class BasicAudioEncoderSettings - { - public: - property BasicAudioCodecId CodecId; - property UInt64 Bitrate; - property int SampleRate; // may be updated by the BasicEncoder constructor to one of the supported sample rates - property int Channels; - property BasicSampleFormat SampleFormat; - property int FrameSize; - property BasicChannelLayout ChannelLayout; - }; - - public ref class BasicEncoderSettings - { - public: - property BasicVideoEncoderSettings^ Video; - property BasicAudioEncoderSettings^ Audio; - property Dictionary^ Options; - - BasicEncoderSettings() - { - Video = gcnew BasicVideoEncoderSettings(); - Video->Width = 640; - Video->Height = 480; - Video->PixelFormat = BasicPixelFormat::YUV420P; - Video->Bitrate = 400000; - Video->GopSize = 12; - Video->Timebase = Rational(1, 25); - Video->PixelAspectRatio = Rational(1, 1); - - Audio = gcnew BasicAudioEncoderSettings(); - Audio->Bitrate = 64000; - Audio->SampleRate = 44100; - - Options = gcnew Dictionary(); - } - }; -} diff --git a/BasicFFEncode/BasicFFEncode.vcxproj b/BasicFFEncode/BasicFFEncode.vcxproj index fabb426..b41c264 100644 --- a/BasicFFEncode/BasicFFEncode.vcxproj +++ b/BasicFFEncode/BasicFFEncode.vcxproj @@ -99,6 +99,7 @@ WIN32;_DEBUG;%(PreprocessorDefinitions) Use ..\FFmpeg\include + $(IntDir) @@ -113,6 +114,7 @@ _DEBUG;%(PreprocessorDefinitions) ..\FFmpeg\include Use + $(IntDir) @@ -126,6 +128,7 @@ WIN32;NDEBUG;%(PreprocessorDefinitions) Use ..\FFmpeg\include + $(IntDir) @@ -139,6 +142,7 @@ NDEBUG;%(PreprocessorDefinitions) Use ..\FFmpeg\include + $(IntDir) @@ -152,10 +156,6 @@ - - - - @@ -163,6 +163,7 @@ + diff --git a/BasicFFEncode/BasicFFEncode.vcxproj.filters b/BasicFFEncode/BasicFFEncode.vcxproj.filters index f60087d..c13262d 100644 --- a/BasicFFEncode/BasicFFEncode.vcxproj.filters +++ b/BasicFFEncode/BasicFFEncode.vcxproj.filters @@ -2,17 +2,14 @@ - - - - + diff --git a/BasicFFEncode/Enums.h b/BasicFFEncode/Enums.cpp similarity index 99% rename from BasicFFEncode/Enums.h rename to BasicFFEncode/Enums.cpp index ed7b84e..df5bf73 100644 --- a/BasicFFEncode/Enums.h +++ b/BasicFFEncode/Enums.cpp @@ -1,12 +1,16 @@ -#pragma once +#include "Stdafx.h" extern "C" { #include #include } + + namespace BasicFFEncode { + using namespace System; + public enum class BasicSampleFormat { U8 = AV_SAMPLE_FMT_U8, ///< unsigned 8 bits @@ -629,8 +633,8 @@ namespace BasicFFEncode YLC = AV_CODEC_ID_YLC, }; - [System::FlagsAttribute] - public enum class BasicChannelLayout : System::UInt64 + [FlagsAttribute] + public enum class BasicChannelLayout : UInt64 { FrontLeft = AV_CH_FRONT_LEFT, FrontRight = AV_CH_FRONT_RIGHT, diff --git a/BasicFFEncode/Frames.cpp b/BasicFFEncode/Frames.cpp index 416cdba..df73c68 100644 --- a/BasicFFEncode/Frames.cpp +++ b/BasicFFEncode/Frames.cpp @@ -1,85 +1,125 @@ #include "Stdafx.h" -#include "Frames.h" - -#pragma comment(lib, "avcodec.lib") - -using namespace System; - -BasicFFEncode::BasicVideoFrame::BasicVideoFrame(int width, int height, BasicPixelFormat pixelFormat) +extern "C" { - pFrame = av_frame_alloc(); - if (!pFrame) - throw gcnew Exception("Could not allocate video frame"); - - pFrame->format = static_cast(pixelFormat); - pFrame->width = width; - pFrame->height = height; - - if (av_frame_get_buffer(pFrame, 32) < 0) - { - pin_ptr pFrame_pinned = &pFrame; - av_frame_free(pFrame_pinned); - throw gcnew Exception("Could not allocate video frame buffer"); - } +#include } +#pragma comment(lib, "avcodec.lib") +#using "Enums.obj" -BasicFFEncode::BasicVideoFrame::~BasicVideoFrame() -{ - pin_ptr pFrame_pinned = &pFrame; - av_frame_free(pFrame_pinned); -} -Byte* BasicFFEncode::BasicVideoFrame::GetBuffer(int plane) -{ - if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) - throw gcnew ArgumentException("Plane index is out of range"); - return pFrame->data[plane]; -} -Int32 BasicFFEncode::BasicVideoFrame::GetStride(int plane) +namespace BasicFFEncode { - if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) - throw gcnew ArgumentException("Plane index is out of range"); - return pFrame->linesize[plane]; -} + using namespace System; + public ref class BasicVideoFrame + { + private: + AVFrame* _pFrame; + + internal: + property AVFrame* pFrame { AVFrame* get() { + return _pFrame; + } + void set(AVFrame* f) { + _pFrame = f; + } + } + + public: + BasicVideoFrame(int width, int height, BasicPixelFormat pixelFormat) + { + pFrame = av_frame_alloc(); + if (!pFrame) + throw gcnew Exception("Could not allocate video frame"); + + pFrame->format = static_cast(pixelFormat); + pFrame->width = width; + pFrame->height = height; + + if (av_frame_get_buffer(pFrame, 32) < 0) + { + pin_ptr pFrame_pinned = &_pFrame; + av_frame_free(pFrame_pinned); + throw gcnew Exception("Could not allocate video frame buffer"); + } + } + + ~BasicVideoFrame() + { + pin_ptr pFrame_pinned = &_pFrame; + av_frame_free(pFrame_pinned); + } + + Byte* GetBuffer(int plane) + { + if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) + throw gcnew ArgumentException("Plane index is out of range"); + return pFrame->data[plane]; + } + + Int32 GetStride(int plane) + { + if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) + throw gcnew ArgumentException("Plane index is out of range"); + return pFrame->linesize[plane]; + } + }; + + + + public ref class BasicAudioFrame + { + internal: + AVFrame* pFrame; + + public: + BasicAudioFrame(int sampleCount, BasicSampleFormat sampleFormat, BasicChannelLayout channelLayout) + { + pFrame = av_frame_alloc(); + if (!pFrame) + throw gcnew Exception("Could not allocate audio frame"); + + pFrame->format = static_cast(sampleFormat); + pFrame->channel_layout = static_cast(channelLayout); + pFrame->nb_samples = sampleCount; + + if (av_frame_get_buffer(pFrame, 0) < 0) + { + pin_ptr pFrame_pinned = &pFrame; + av_frame_free(pFrame_pinned); + throw gcnew Exception("Could not allocate audio frame buffer"); + } + } + + ~BasicAudioFrame() + { + pin_ptr pFrame_pinned = &pFrame; + av_frame_free(pFrame_pinned); + } + + Byte* GetBuffer(int plane) + { + if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) + throw gcnew ArgumentException("Plane index is out of range"); + return pFrame->data[plane]; + } + + Int32 GetStride(int plane) + { + if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) + throw gcnew ArgumentException("Plane index is out of range"); + return pFrame->linesize[plane]; + } + + property Int32 SampleCount { Int32 get() { return pFrame->nb_samples; }} + }; +} -BasicFFEncode::BasicAudioFrame::BasicAudioFrame(int sampleCount, BasicSampleFormat sampleFormat, BasicChannelLayout channelLayout) -{ - pFrame = av_frame_alloc(); - if (!pFrame) - throw gcnew Exception("Could not allocate audio frame"); - pFrame->format = static_cast(sampleFormat); - pFrame->channel_layout = static_cast(channelLayout); - pFrame->nb_samples = sampleCount; - if (av_frame_get_buffer(pFrame, 0) < 0) - { - pin_ptr pFrame_pinned = &pFrame; - av_frame_free(pFrame_pinned); - throw gcnew Exception("Could not allocate audio frame buffer"); - } -} -BasicFFEncode::BasicAudioFrame::~BasicAudioFrame() -{ - pin_ptr pFrame_pinned = &pFrame; - av_frame_free(pFrame_pinned); -} -Byte* BasicFFEncode::BasicAudioFrame::GetBuffer(int plane) -{ - if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) - throw gcnew ArgumentException("Plane index is out of range"); - return pFrame->data[plane]; -} -Int32 BasicFFEncode::BasicAudioFrame::GetStride(int plane) -{ - if (plane < 0 || plane >= AV_NUM_DATA_POINTERS) - throw gcnew ArgumentException("Plane index is out of range"); - return pFrame->linesize[plane]; -} diff --git a/BasicFFEncode/Frames.h b/BasicFFEncode/Frames.h deleted file mode 100644 index 2df1f7a..0000000 --- a/BasicFFEncode/Frames.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include "Enums.h" -extern "C" -{ -#include -} - -namespace BasicFFEncode -{ - using namespace System; - - public ref class BasicVideoFrame - { - internal: - AVFrame* pFrame; - - public: - BasicVideoFrame(int width, int height, BasicPixelFormat pixelFormat); - ~BasicVideoFrame(); - Byte* GetBuffer(int plane); - Int32 GetStride(int plane); - }; - - public ref class BasicAudioFrame - { - internal: - AVFrame* pFrame; - - public: - BasicAudioFrame(Int32 sampleCount, BasicSampleFormat sampleFormat, BasicChannelLayout channelLayout); - ~BasicAudioFrame(); - Byte* GetBuffer(Int32 plane); - Int32 GetStride(Int32 plane); - property Int32 SampleCount { Int32 get() { return pFrame->nb_samples; }} - }; - -} diff --git a/BasicFFEncode/Helpers.cpp b/BasicFFEncode/Helpers.cpp index aefa1f6..f73ab10 100644 --- a/BasicFFEncode/Helpers.cpp +++ b/BasicFFEncode/Helpers.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h" +#include "Stdafx.h" #include "Helpers.h" using namespace System;