History

Add GOP_COMMAND

Send GOP_COMMAND to vp9 for setting gop decisions on the fly. GOP_COMMAND has three members. use: use this command to set gop or use vp9's gop decision. show_frame_count: number of show frames in this gop. use_alt_ref: use alt ref frame or not. Move the logic of processing external_arf_indexes_ from get_gop_coding_frame_num() to GetGopCommand() and GetCodingFrameNumFromGopMap().

angiebird committed 6 years ago

Commit fdf04091 parent 3bc58f14 files · +113 −60Browse files

Unified · Split

4 files
  1. vp9/encoder/vp9_encoder.h +33 −7
  2. vp9/encoder/vp9_firstpass.c +33 −42
  3. vp9/encoder/vp9_firstpass.h +2 −5
  4. vp9/simple_encode.cc +45 −6
vp9/encoder/vp9_encoder.h+33−7
@@ -532,24 +532,50 @@ typedef struct MOTION_VECTOR_INFO { · 531 unmodified lines
532 int_mv mv[2];532 int_mv mv[2];
533} MOTION_VECTOR_INFO;533} MOTION_VECTOR_INFO;
534 534
535typedef struct GOP_COMMAND {
536 int use; // use this command to set gop or not. If not, use vp9's decision.
537 int show_frame_count;
538 int use_alt_ref;
539} GOP_COMMAND;
540
541static INLINE void gop_command_on(GOP_COMMAND *gop_command,
542 int show_frame_count, int use_alt_ref) {
543 gop_command->use = 1;
544 gop_command->show_frame_count = show_frame_count;
545 gop_command->use_alt_ref = use_alt_ref;
546}
547
548static INLINE void gop_command_off(GOP_COMMAND *gop_command) {
549 gop_command->use = 0;
550 gop_command->show_frame_count = 0;
551 gop_command->use_alt_ref = 0;
552}
553
554static INLINE int gop_command_coding_frame_count(
555 const GOP_COMMAND *gop_command) {
556 if (gop_command->use == 0) {
557 assert(0);
558 return -1;
559 }
560 return gop_command->show_frame_count + gop_command->use_alt_ref;
561}
562
535typedef struct ENCODE_COMMAND {563typedef struct ENCODE_COMMAND {
536 int use_external_quantize_index;564 int use_external_quantize_index;
537 int external_quantize_index;565 int external_quantize_index;
538 // A list of binary flags set from the external controller.566 GOP_COMMAND gop_command;
539 // Each binary flag indicates whether the frame is an arf or not.
540 const int *external_arf_indexes;
541} ENCODE_COMMAND;567} ENCODE_COMMAND;
542 568
543static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) {569static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) {
544 vp9_zero(*encode_command);570 vp9_zero(*encode_command);
545 encode_command->use_external_quantize_index = 0;571 encode_command->use_external_quantize_index = 0;
546 encode_command->external_quantize_index = -1;572 encode_command->external_quantize_index = -1;
547 encode_command->external_arf_indexes = NULL;573 gop_command_off(&encode_command->gop_command);
548}574}
549 575
550static INLINE void encode_command_set_external_arf_indexes(576static INLINE void encode_command_set_gop_command(
551 ENCODE_COMMAND *encode_command, const int *external_arf_indexes) {577 ENCODE_COMMAND *encode_command, GOP_COMMAND gop_command) {
552 encode_command->external_arf_indexes = external_arf_indexes;578 encode_command->gop_command = gop_command;
553}579}
554 580
555static INLINE void encode_command_set_external_quantize_index(581static INLINE void encode_command_set_external_quantize_index(
vp9/encoder/vp9_firstpass.c+33−42
@@ -2508,9 +2508,6 @@ typedef struct RANGE { · 2507 unmodified lines
2508 * structs.2508 * structs.
2509 */2509 */
2510static int get_gop_coding_frame_num(2510static int get_gop_coding_frame_num(
2511#if CONFIG_RATE_CTRL
2512 const int *external_arf_indexes,
2513#endif
2514 int *use_alt_ref, const FRAME_INFO *frame_info,2511 int *use_alt_ref, const FRAME_INFO *frame_info,
2515 const FIRST_PASS_INFO *first_pass_info, const RATE_CONTROL *rc,2512 const FIRST_PASS_INFO *first_pass_info, const RATE_CONTROL *rc,
2516 int gf_start_show_idx, const RANGE *active_gf_interval,2513 int gf_start_show_idx, const RANGE *active_gf_interval,
@@ -2526,24 +2523,6 @@ static int get_gop_coding_frame_num( · 9 unmodified lines
2526 (frame_info->frame_height + frame_info->frame_width) / 4.0;2523 (frame_info->frame_height + frame_info->frame_width) / 4.0;
2527 double zero_motion_accumulator = 1.0;2524 double zero_motion_accumulator = 1.0;
2528 int gop_coding_frames;2525 int gop_coding_frames;
2529#if CONFIG_RATE_CTRL
2530 (void)mv_ratio_accumulator_thresh;
2531 (void)active_gf_interval;
2532 (void)gop_intra_factor;
2533
2534 if (external_arf_indexes != NULL && rc->frames_to_key > 1) {
2535 // gop_coding_frames = 1 is necessary to filter out the overlay frame,
2536 // since the arf is in this group of picture and its overlay is in the next.
2537 gop_coding_frames = 1;
2538 *use_alt_ref = 1;
2539 while (gop_coding_frames < rc->frames_to_key) {
2540 const int frame_index = gf_start_show_idx + gop_coding_frames;
2541 ++gop_coding_frames;
2542 if (external_arf_indexes[frame_index] == 1) break;
2543 }
2544 return gop_coding_frames;
2545 }
2546#endif // CONFIG_RATE_CTRL
2547 2526
2548 *use_alt_ref = 1;2527 *use_alt_ref = 1;
2549 gop_coding_frames = 0;2528 gop_coding_frames = 0;
@@ -2770,15 +2749,26 @@ static void define_gf_group(VP9_COMP *cpi, int gf_start_show_idx) { · 220 unmodified lines
2770 gop_intra_factor = 1.0;2749 gop_intra_factor = 1.0;
2771 }2750 }
2772 2751
2773 {
2774 gop_coding_frames = get_gop_coding_frame_num(
2775#if CONFIG_RATE_CTRL2752#if CONFIG_RATE_CTRL
2776 cpi->encode_command.external_arf_indexes,2753 {
2777#endif2754 const GOP_COMMAND *gop_command = &cpi->encode_command.gop_command;
2778 &use_alt_ref, frame_info, first_pass_info, rc, gf_start_show_idx,2755 assert(allow_alt_ref == 1);
2779 &active_gf_interval, gop_intra_factor, cpi->oxcf.lag_in_frames);2756 if (gop_command->use) {
2780 use_alt_ref &= allow_alt_ref;2757 gop_coding_frames = gop_command_coding_frame_count(gop_command);
2758 use_alt_ref = gop_command->use_alt_ref;
2759 } else {
2760 gop_coding_frames = get_gop_coding_frame_num(
2761 &use_alt_ref, frame_info, first_pass_info, rc, gf_start_show_idx,
2762 &active_gf_interval, gop_intra_factor, cpi->oxcf.lag_in_frames);
2763 use_alt_ref &= allow_alt_ref;
2764 }
2781 }2765 }
2766#else
2767 gop_coding_frames = get_gop_coding_frame_num(
2768 &use_alt_ref, frame_info, first_pass_info, rc, gf_start_show_idx,
2769 &active_gf_interval, gop_intra_factor, cpi->oxcf.lag_in_frames);
2770 use_alt_ref &= allow_alt_ref;
2771#endif
2782 2772
2783 // Was the group length constrained by the requirement for a new KF?2773 // Was the group length constrained by the requirement for a new KF?
2784 rc->constrained_gf_group = (gop_coding_frames >= rc->frames_to_key) ? 1 : 0;2774 rc->constrained_gf_group = (gop_coding_frames >= rc->frames_to_key) ? 1 : 0;
@@ -3704,6 +3694,7 @@ void vp9_get_next_group_of_picture(const VP9_COMP *cpi, int *first_is_key_frame, · 919 unmodified lines
3704 int *use_alt_ref, int *coding_frame_count,3694 int *use_alt_ref, int *coding_frame_count,
3705 int *first_show_idx,3695 int *first_show_idx,
3706 int *last_gop_use_alt_ref) {3696 int *last_gop_use_alt_ref) {
3697 const GOP_COMMAND *gop_command = &cpi->encode_command.gop_command;
3707 // We make a copy of rc here because we want to get information from the3698 // We make a copy of rc here because we want to get information from the
3708 // encoder without changing its state.3699 // encoder without changing its state.
3709 // TODO(angiebird): Avoid copying rc here.3700 // TODO(angiebird): Avoid copying rc here.
@@ -3726,14 +3717,19 @@ void vp9_get_next_group_of_picture(const VP9_COMP *cpi, int *first_is_key_frame, · 16 unmodified lines
3726 *first_is_key_frame = 1;3717 *first_is_key_frame = 1;
3727 }3718 }
3728 3719
3729 *coding_frame_count = vp9_get_gop_coding_frame_count(3720 if (gop_command->use) {
3730 cpi->encode_command.external_arf_indexes, &cpi->oxcf, &cpi->frame_info,3721 *coding_frame_count = gop_command_coding_frame_count(gop_command);
3731 &cpi->twopass.first_pass_info, &rc, *first_show_idx, multi_layer_arf,3722 *use_alt_ref = gop_command->use_alt_ref;
3732 allow_alt_ref, *first_is_key_frame, *last_gop_use_alt_ref, use_alt_ref);3723 assert(*coding_frame_count < rc.frames_to_key);
3724 } else {
3725 *coding_frame_count = vp9_get_gop_coding_frame_count(
3726 &cpi->oxcf, &cpi->frame_info, &cpi->twopass.first_pass_info, &rc,
3727 *first_show_idx, multi_layer_arf, allow_alt_ref, *first_is_key_frame,
3728 *last_gop_use_alt_ref, use_alt_ref);
3729 }
3733}3730}
3734 3731
3735int vp9_get_gop_coding_frame_count(const int *external_arf_indexes,3732int vp9_get_gop_coding_frame_count(const VP9EncoderConfig *oxcf,
3736 const VP9EncoderConfig *oxcf,
3737 const FRAME_INFO *frame_info,3733 const FRAME_INFO *frame_info,
3738 const FIRST_PASS_INFO *first_pass_info,3734 const FIRST_PASS_INFO *first_pass_info,
3739 const RATE_CONTROL *rc, int show_idx,3735 const RATE_CONTROL *rc, int show_idx,
@@ -3756,9 +3752,6 @@ int vp9_get_gop_coding_frame_count(const int *external_arf_indexes, · 16 unmodified lines
3756 }3752 }
3757 3753
3758 frame_count = get_gop_coding_frame_num(3754 frame_count = get_gop_coding_frame_num(
3759#if CONFIG_RATE_CTRL
3760 external_arf_indexes,
3761#endif
3762 use_alt_ref, frame_info, first_pass_info, rc, show_idx,3755 use_alt_ref, frame_info, first_pass_info, rc, show_idx,
3763 &active_gf_interval, gop_intra_factor, oxcf->lag_in_frames);3756 &active_gf_interval, gop_intra_factor, oxcf->lag_in_frames);
3764 *use_alt_ref &= allow_alt_ref;3757 *use_alt_ref &= allow_alt_ref;
@@ -3767,8 +3760,7 @@ int vp9_get_gop_coding_frame_count(const int *external_arf_indexes, · 2 unmodified lines
3767 3760
3768// Under CONFIG_RATE_CTRL, once the first_pass_info is ready, the number of3761// Under CONFIG_RATE_CTRL, once the first_pass_info is ready, the number of
3769// coding frames (including show frame and alt ref) can be determined.3762// coding frames (including show frame and alt ref) can be determined.
3770int vp9_get_coding_frame_num(const int *external_arf_indexes,3763int vp9_get_coding_frame_num(const VP9EncoderConfig *oxcf,
3771 const VP9EncoderConfig *oxcf,
3772 const FRAME_INFO *frame_info,3764 const FRAME_INFO *frame_info,
3773 const FIRST_PASS_INFO *first_pass_info,3765 const FIRST_PASS_INFO *first_pass_info,
3774 int multi_layer_arf, int allow_alt_ref) {3766 int multi_layer_arf, int allow_alt_ref) {
@@ -3792,9 +3784,8 @@ int vp9_get_coding_frame_num(const int *external_arf_indexes, · 17 unmodified lines
3792 }3784 }
3793 3785
3794 gop_coding_frame_count = vp9_get_gop_coding_frame_count(3786 gop_coding_frame_count = vp9_get_gop_coding_frame_count(
3795 external_arf_indexes, oxcf, frame_info, first_pass_info, &rc, show_idx,3787 oxcf, frame_info, first_pass_info, &rc, show_idx, multi_layer_arf,
3796 multi_layer_arf, allow_alt_ref, first_is_key_frame,3788 allow_alt_ref, first_is_key_frame, last_gop_use_alt_ref, &use_alt_ref);
3797 last_gop_use_alt_ref, &use_alt_ref);
3798 3789
3799 rc.source_alt_ref_active = use_alt_ref;3790 rc.source_alt_ref_active = use_alt_ref;
3800 last_gop_use_alt_ref = use_alt_ref;3791 last_gop_use_alt_ref = use_alt_ref;
vp9/encoder/vp9_firstpass.h+2−5
@@ -264,7 +264,6 @@ void vp9_get_next_group_of_picture(const struct VP9_COMP *cpi, · 263 unmodified lines
264 264
265/*!\brief Call this function before coding a new group of pictures to get265/*!\brief Call this function before coding a new group of pictures to get
266 * information about it.266 * information about it.
267 * \param[in] external_arf_indexes External arf indexs passed in
268 * \param[in] oxcf Encoder config267 * \param[in] oxcf Encoder config
269 * \param[in] frame_info Frame info268 * \param[in] frame_info Frame info
270 * \param[in] first_pass_info First pass stats269 * \param[in] first_pass_info First pass stats
@@ -279,8 +278,7 @@ void vp9_get_next_group_of_picture(const struct VP9_COMP *cpi, · 8 unmodified lines
279 *278 *
280 * \return Returns coding frame count279 * \return Returns coding frame count
281 */280 */
282int vp9_get_gop_coding_frame_count(const int *external_arf_indexes,281int vp9_get_gop_coding_frame_count(const struct VP9EncoderConfig *oxcf,
283 const struct VP9EncoderConfig *oxcf,
284 const FRAME_INFO *frame_info,282 const FRAME_INFO *frame_info,
285 const FIRST_PASS_INFO *first_pass_info,283 const FIRST_PASS_INFO *first_pass_info,
286 const RATE_CONTROL *rc, int show_idx,284 const RATE_CONTROL *rc, int show_idx,
@@ -288,8 +286,7 @@ int vp9_get_gop_coding_frame_count(const int *external_arf_indexes, · 1 unmodified line
288 int first_is_key_frame,286 int first_is_key_frame,
289 int last_gop_use_alt_ref, int *use_alt_ref);287 int last_gop_use_alt_ref, int *use_alt_ref);
290 288
291int vp9_get_coding_frame_num(const int *external_arf_indexes,289int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf,
292 const struct VP9EncoderConfig *oxcf,
293 const FRAME_INFO *frame_info,290 const FRAME_INFO *frame_info,
294 const FIRST_PASS_INFO *first_pass_info,291 const FIRST_PASS_INFO *first_pass_info,
295 int multi_layer_arf, int allow_alt_ref);292 int multi_layer_arf, int allow_alt_ref);
vp9/simple_encode.cc+45−6
@@ -813,6 +813,29 @@ T *GetVectorData(const std::vector<T> &v) { · 812 unmodified lines
813 return const_cast<T *>(v.data());813 return const_cast<T *>(v.data());
814}814}
815 815
816static GOP_COMMAND GetGopCommand(const std::vector<int> &gop_map,
817 int start_show_index) {
818 assert(static_cast<size_t>(start_show_index) < gop_map.size());
819 GOP_COMMAND gop_command;
820 if (gop_map.size() > 0) {
821 int end_show_index = start_show_index + 1;
822 while (static_cast<size_t>(end_show_index) < gop_map.size() &&
823 gop_map[end_show_index] == 0) {
824 ++end_show_index;
825 }
826 const int show_frame_count = end_show_index - start_show_index;
827 int use_alt_ref = 1;
828 if (static_cast<size_t>(end_show_index) == gop_map.size()) {
829 // This is the last gop group, there must be no altref.
830 use_alt_ref = 0;
831 }
832 gop_command_on(&gop_command, show_frame_count, use_alt_ref);
833 } else {
834 gop_command_off(&gop_command);
835 }
836 return gop_command;
837}
838
816void SimpleEncode::StartEncode() {839void SimpleEncode::StartEncode() {
817 assert(impl_ptr_->first_pass_stats.size() > 0);840 assert(impl_ptr_->first_pass_stats.size() > 0);
818 vpx_rational_t frame_rate =841 vpx_rational_t frame_rate =
@@ -834,9 +857,6 @@ void SimpleEncode::StartEncode() { · 15 unmodified lines
834 frame_coding_index_ = 0;857 frame_coding_index_ = 0;
835 show_frame_count_ = 0;858 show_frame_count_ = 0;
836 859
837 encode_command_set_external_arf_indexes(&impl_ptr_->cpi->encode_command,
838 GetVectorData(external_arf_indexes_));
839
840 UpdateKeyFrameGroup(show_frame_count_);860 UpdateKeyFrameGroup(show_frame_count_);
841 861
842 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,862 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
@@ -914,6 +934,10 @@ void SimpleEncode::PostUpdateState( · 71 unmodified lines
914 934
915 IncreaseGroupOfPictureIndex(&group_of_picture_);935 IncreaseGroupOfPictureIndex(&group_of_picture_);
916 if (IsGroupOfPictureFinished(group_of_picture_)) {936 if (IsGroupOfPictureFinished(group_of_picture_)) {
937 const GOP_COMMAND gop_command =
938 GetGopCommand(external_arf_indexes_, show_frame_count_);
939 encode_command_set_gop_command(&impl_ptr_->cpi->encode_command,
940 gop_command);
917 // This function needs to be called after ref_frame_info_ is updated941 // This function needs to be called after ref_frame_info_ is updated
918 // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup().942 // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup().
919 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,943 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
@@ -1002,8 +1026,24 @@ void SimpleEncode::EncodeFrameWithQuantizeIndex( · 82 unmodified lines
1002 encode_command_reset_external_quantize_index(&impl_ptr_->cpi->encode_command);1026 encode_command_reset_external_quantize_index(&impl_ptr_->cpi->encode_command);
1003}1027}
1004 1028
1029static int GetCodingFrameNumFromGopMap(const std::vector<int> &gop_map) {
1030 int start_show_index = 0;
1031 int coding_frame_count = 0;
1032 while (static_cast<size_t>(start_show_index) < gop_map.size()) {
1033 const GOP_COMMAND gop_command = GetGopCommand(gop_map, start_show_index);
1034 start_show_index += gop_command.show_frame_count;
1035 coding_frame_count += gop_command_coding_frame_count(&gop_command);
1036 }
1037 assert(start_show_index == gop_map.size());
1038 return coding_frame_count;
1039}
1040
1005int SimpleEncode::GetCodingFrameNum() const {1041int SimpleEncode::GetCodingFrameNum() const {
1006 assert(impl_ptr_->first_pass_stats.size() - 1 > 0);1042 assert(impl_ptr_->first_pass_stats.size() > 0);
1043 if (external_arf_indexes_.size() > 0) {
1044 return GetCodingFrameNumFromGopMap(external_arf_indexes_);
1045 }
1046
1007 // These are the default settings for now.1047 // These are the default settings for now.
1008 const int multi_layer_arf = 0;1048 const int multi_layer_arf = 0;
1009 const int allow_alt_ref = 1;1049 const int allow_alt_ref = 1;
@@ -1017,8 +1057,7 @@ int SimpleEncode::GetCodingFrameNum() const { · 7 unmodified lines
1017 fps_init_first_pass_info(&first_pass_info,1057 fps_init_first_pass_info(&first_pass_info,
1018 GetVectorData(impl_ptr_->first_pass_stats),1058 GetVectorData(impl_ptr_->first_pass_stats),
1019 num_frames_);1059 num_frames_);
1020 return vp9_get_coding_frame_num(external_arf_indexes_.data(), &oxcf,1060 return vp9_get_coding_frame_num(&oxcf, &frame_info, &first_pass_info,
1021 &frame_info, &first_pass_info,
1022 multi_layer_arf, allow_alt_ref);1061 multi_layer_arf, allow_alt_ref);
1023}1062}
1024 1063

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t