Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

force key frame implementation for vaapi #437

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/codec/vaapi/vp8.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type encoderVP8 struct {

rate *framerateDetector

forceKeyFrame bool

mu sync.Mutex
closed bool
}
Expand Down Expand Up @@ -315,7 +317,7 @@ func (e *encoderVP8) Read() ([]byte, func(), error) {

e.frParam.data.framerate = C.uint(e.rate.Calc())

if kf {
if kf || e.forceKeyFrame {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forceKeyFrame isn't reset, so every frames after ForceKeyFrame() will be keyframe.

// Key frame
C.setForceKFFlagVP8(&e.picParam, 1)
C.setFrameTypeFlagVP8(&e.picParam, 0)
Expand Down Expand Up @@ -545,6 +547,10 @@ func (e *encoderVP8) Controller() codec.EncoderController {
return e
}

func (e *encoderVP8) ForceKeyFrame() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, this function method already exists and returns the encoder itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't remember what I wanted to point and old CI log is gone.
The interface seems be correct.

e.forceKeyFrame = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may cause data race if ForceKeyFrame() is called from other goroutine during Read()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the type to use an atomic.Bool instead. This way, there is no data race possible.

}

func (e *encoderVP8) Close() error {
e.mu.Lock()
defer e.mu.Unlock()
Expand Down
2 changes: 0 additions & 2 deletions pkg/codec/vaapi/vp8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func TestVP8ShouldImplementBitRateControl(t *testing.T) {
}

func TestVP8ShouldImplementKeyFrameControl(t *testing.T) {
t.SkipNow() // TODO: Implement key frame control

e := &encoderVP8{}
if _, ok := e.Controller().(codec.KeyFrameController); !ok {
t.Error()
Expand Down
8 changes: 7 additions & 1 deletion pkg/codec/vaapi/vp9.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type encoderVP9 struct {

rate *framerateDetector

forceKeyFrame bool

mu sync.Mutex
closed bool
}
Expand Down Expand Up @@ -304,7 +306,7 @@ func (e *encoderVP9) Read() ([]byte, func(), error) {

e.frParam.data.framerate = C.uint(e.rate.Calc())

if kf {
if kf || e.forceKeyFrame {
C.setForceKFFlag9(&e.picParam, 1)
C.setFrameTypeFlagVP9(&e.picParam, 0)
e.picParam.refresh_frame_flags = 0
Expand Down Expand Up @@ -480,6 +482,10 @@ func (e *encoderVP9) Controller() codec.EncoderController {
return e
}

func (e *encoderVP9) ForceKeyFrame() {
e.forceKeyFrame = true
}

func (e *encoderVP9) Close() error {
e.mu.Lock()
defer e.mu.Unlock()
Expand Down
2 changes: 0 additions & 2 deletions pkg/codec/vaapi/vp9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func TestVP9ShouldImplementBitRateControl(t *testing.T) {
}

func TestVP9ShouldImplementKeyFrameControl(t *testing.T) {
t.SkipNow() // TODO: Implement key frame control

e := &encoderVP9{}
if _, ok := e.Controller().(codec.KeyFrameController); !ok {
t.Error()
Expand Down