Skip to content

Commit

Permalink
Implement forced keyframes for x264 (pion#388)
Browse files Browse the repository at this point in the history
* Implement forced keyframes for x264

* format code
  • Loading branch information
zjzhang-cn authored Apr 5, 2022
1 parent ff18b21 commit e780bdc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/codec/x264/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct Encoder {
x264_t *h;
x264_picture_t pic_in;
x264_param_t param;
int force_key_frame;
} Encoder;

Encoder *enc_new(x264_param_t param, char *preset, int *rc) {
Expand Down Expand Up @@ -85,8 +86,14 @@ Slice enc_encode(Encoder *e, uint8_t *y, uint8_t *cb, uint8_t *cr, int *rc) {
e->pic_in.img.plane[0] = y;
e->pic_in.img.plane[1] = cb;
e->pic_in.img.plane[2] = cr;
if (e->force_key_frame) {
e->pic_in.i_type = X264_TYPE_IDR;
} else {
e->pic_in.i_type = X264_TYPE_AUTO;
}

int frame_size = x264_encoder_encode(e->h, &nal, &i_nal, &e->pic_in, &pic_out);
e->force_key_frame = 0;
Slice s = {.data_len = frame_size};
if (frame_size <= 0) {
*rc = ERR_ENCODE;
Expand Down
3 changes: 2 additions & 1 deletion pkg/codec/x264/x264.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func (e *encoder) SetBitRate(b int) error {
}

func (e *encoder) ForceKeyFrame() error {
panic("ForceKeyFrame is not implemented")
e.engine.force_key_frame = C.int(1)
return nil
}

func (e *encoder) Close() error {
Expand Down

0 comments on commit e780bdc

Please sign in to comment.