Skip to content

Commit 815b4aa

Browse files
committed
Added runtime bitrate and fps control
1 parent 361d593 commit 815b4aa

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

EncoderTest/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ static void Main(string[] args)
5252
foreach (var frame in frames)
5353
{
5454

55-
byte[] b = frame.ToByteArray();
56-
frame.CopyTo(buffer, 0);
55+
//byte[] b = frame.ToByteArray();
56+
//frame.CopyTo(buffer, 0);
5757
Decode(frame.Data, frame.Length, frame.Type);
5858
}
5959

6060
}
61+
encoder.SetMaxBitrate((1+i)*100000);
62+
encoder.SetTargetFps(22);
6163
}
6264
Console.WriteLine("\n Time: "+sw.ElapsedMilliseconds);
6365

H264Sharp/Decoder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using namespace System::Drawing;
99
using namespace System::Drawing::Imaging;
1010
using namespace System::Runtime::InteropServices;
11-
using namespace std::chrono;
1211

1312
namespace H264Sharp {
1413

H264Sharp/Encoder.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <iostream>
66
#include "Encoder.h"
77

8-
using namespace std::chrono;
98
using namespace System::Drawing;
109
using namespace System::Drawing::Imaging;
1110
using namespace System::Runtime::InteropServices;
@@ -323,9 +322,9 @@ namespace H264Sharp {
323322

324323
}
325324

326-
bool Encoder::Encode(array<Byte>^ i420, [Out]array<EncodedFrame^>^% frame)
325+
bool Encoder::Encode(array<Byte>^ i420, int startIndex, [Out]array<EncodedFrame^>^% frame)
327326
{
328-
pin_ptr<Byte> ptr = &i420[0];
327+
pin_ptr<Byte> ptr = &i420[startIndex];
329328
bool res = Encode(innerBuffer, frame);
330329

331330
ptr = nullptr; // unpin
@@ -381,6 +380,23 @@ namespace H264Sharp {
381380
return encoder->ForceIntraFrame(true);
382381
}
383382

383+
void H264Sharp::Encoder::SetMaxBitrate(int target)
384+
{
385+
SBitrateInfo param;
386+
387+
memset(&param, 0, sizeof(SBitrateInfo));
388+
param.iBitrate = target;
389+
param.iLayer = SPATIAL_LAYER_ALL;
390+
encoder->SetOption(ENCODER_OPTION_MAX_BITRATE, &param);
391+
encoder->SetOption(ENCODER_OPTION_BITRATE, &param);
392+
}
393+
void H264Sharp::Encoder::SetTargetFps(float target)
394+
{
395+
396+
encoder->SetOption(ENCODER_OPTION_FRAME_RATE, &target);
397+
}
398+
399+
384400
Encoder::~Encoder()
385401
{
386402
this->!Encoder();

H264Sharp/Encoder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ namespace H264Sharp {
2525
bool Encode(BgraImage ^bgra, array<EncodedFrame^>^% frame);
2626
bool Encode(RgbImage ^rgb, array<EncodedFrame^>^% frame);
2727
bool Encode(RgbaImage ^rgba, array<EncodedFrame^>^% frame);
28-
bool Encode(array<Byte> ^i420, array<EncodedFrame^>^% frame);
28+
bool Encode(array<Byte> ^i420, int startIndex, array<EncodedFrame^>^% frame);
2929
bool Encode(unsigned char* i420, array<EncodedFrame^>^% frame);
3030
bool Encode(IntPtr^ i420, array<EncodedFrame^>^% frame);
3131

3232
int ForceIntraFrame();
33-
33+
void SetMaxBitrate(int target);
34+
void SetTargetFps(float target);
3435

3536
private:
3637
int buffer_size;

0 commit comments

Comments
 (0)