bitrate, buffer and crf can be changed while an x264 encoder is actively encoding frames. You cannot change to/from CBR without stopping the stream.

This commit is contained in:
Zakk 2015-02-28 04:46:34 -05:00
parent ee522c5509
commit e302a3b6bd
4 changed files with 58 additions and 14 deletions

View file

@ -8,6 +8,8 @@
#import "x264Compressor.h"
#import "OutputDestination.h"
#import <libavutil/opt.h>
@implementation x264Compressor
@ -203,6 +205,9 @@
}
[self reconfigureCompressor];
if (frameData.videoFrame)
{
CVPixelBufferRetain(frameData.videoFrame);
@ -347,12 +352,37 @@
-(void)reconfigureCompressor
{
if (!_av_codec_ctx)
{
return;
}
_av_codec_ctx->rc_max_rate = self.vbv_maxrate*1000;
if (!self.use_cbr)
{
_av_codec_ctx->rc_buffer_size = self.vbv_buffer*1000;
av_opt_set(_av_codec_ctx->priv_data, "crf", [[NSString stringWithFormat:@"%d", self.crf] UTF8String], 0);
} else {
_av_codec_ctx->rc_buffer_size = self.vbv_buffer*1000;
_av_codec_ctx->bit_rate = self.vbv_maxrate*1000;
}
}
-(bool)setupCompressor:(CVPixelBufferRef)videoFrame
{
avcodec_register_all();
NSLog(@"IN COMPRESSOR SETUP");
if (!self.settingsController)
@ -361,6 +391,9 @@
}
NSString *useAdvancedSettings = self.advancedSettings.copy;
[self setupResolution:videoFrame];
_compressor_queue = dispatch_queue_create("x264 encoder queue", NULL);
@ -413,10 +446,17 @@
//what did we learn today? Don't believe shit you read in forum posts...
//_av_codec_ctx->rc_buffer_size = ((1/self.settingsController.captureFPS)*self.settingsController.captureVideoAverageBitrate)*1000;
_av_codec_ctx->rc_buffer_size = self.vbv_maxrate*1000;
_av_codec_ctx->rc_buffer_size = self.vbv_buffer*1000;
_av_codec_ctx->bit_rate = self.vbv_maxrate*1000;
av_dict_set(&opts, "nal-hrd", "cbr", 0);
if (!useAdvancedSettings)
{
useAdvancedSettings = [NSString stringWithFormat:@"filler=1"];
} else {
useAdvancedSettings = [useAdvancedSettings stringByAppendingString:@":filler=1"];
}
//av_dict_set(&opts, "nal-hrd", "cbr", 0);
}
_av_codec_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
@ -452,11 +492,10 @@
}
id advancedSettings = self.advancedSettings;
if (advancedSettings)
if (useAdvancedSettings)
{
av_dict_set(&opts, "x264opts", [advancedSettings UTF8String], 0);
av_dict_set(&opts, "x264opts", [useAdvancedSettings UTF8String], 0);
}

View file

@ -320,7 +320,6 @@ void getAudioExtradata(char *cookie, char **buffer, size_t *size)
_av_video_stream->time_base.den = self.framerate*1000000;
NSLog(@"FRAMERATE FFMP %d SAMPLE %d BITRATE %d", self.framerate, _samplerate, _audio_bitrate);
_av_audio_stream = avformat_new_stream(_av_fmt_ctx, 0);

View file

@ -161,15 +161,16 @@ Gw
</connections>
</popUpButton>
<tabView fixedFrame="YES" drawsBackground="NO" controlSize="small" type="noTabsNoBorder" initialItem="gyg-eG-YlU" translatesAutoresizingMaskIntoConstraints="NO" id="EJA-Rc-sIZ">
<rect key="frame" x="13" y="41" width="554" height="279"/>
<rect key="frame" x="6" y="31" width="568" height="292"/>
<tabViewItems>
<tabViewItem label="None" identifier="" id="gyg-eG-YlU">
<view key="view" ambiguous="YES" id="lRi-Ns-JYI">
<view key="view" id="lRi-Ns-JYI">
<rect key="frame" x="10" y="25" width="548" height="254"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</tabViewItem>
<tabViewItem label="x264" identifier="1" id="I6I-nX-nIi">
<view key="view" id="AiJ-YZ-cX2">
<view key="view" ambiguous="YES" id="AiJ-YZ-cX2">
<rect key="frame" x="0.0" y="0.0" width="554" height="279"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
@ -254,6 +255,11 @@ Gw
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="TZe-4D-nmN" name="enabled" keyPath="selection.use_cbr" id="z4t-yD-nTr">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
<binding destination="TZe-4D-nmN" name="value" keyPath="selection.crf" id="xEV-u0-L4J">
<dictionary key="options">
<integer key="NSNullPlaceholder" value="0"/>
@ -392,6 +398,11 @@ Gw
<font key="font" metaFont="smallSystem"/>
</buttonCell>
<connections>
<binding destination="-2" name="enabled" keyPath="self.captureRunning" id="1JB-Uf-a7i">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
<binding destination="TZe-4D-nmN" name="value" keyPath="selection.use_cbr" id="vy7-fH-obh">
<dictionary key="options">
<bool key="NSRaisesForNotApplicableKeys" value="NO"/>

View file

@ -1158,11 +1158,6 @@
<binding destination="gSI-N4-DsK" name="contentValues" keyPath="arrangedObjects.key" previousBinding="RTO-Y1-SNp" id="Ftc-NH-ogE"/>
<binding destination="gSI-N4-DsK" name="contentObjects" keyPath="arrangedObjects.value" previousBinding="IOC-y6-UGQ" id="RTO-Y1-SNp"/>
<binding destination="gSI-N4-DsK" name="selectedIndex" keyPath="selectionIndex" previousBinding="Ftc-NH-ogE" id="0G7-uW-cOg"/>
<binding destination="816" name="enabled" keyPath="self.captureRunning" id="TFb-NE-zCz">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
</connections>
</popUpButton>
<button translatesAutoresizingMaskIntoConstraints="NO" id="3602">