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().
Unified · Split
| @@ -532,24 +532,50 @@ typedef struct MOTION_VECTOR_INFO { · 531 unmodified lines | ||
| 532 | 532 | int_mv mv[2]; |
| 533 | 533 | } MOTION_VECTOR_INFO; |
| 534 | 534 | |
| 535 | +typedef 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 | + | |
| 541 | +static 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 | + | |
| 548 | +static 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 | + | |
| 554 | +static 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 | + | |
| 535 | 563 | typedef struct ENCODE_COMMAND { |
| 536 | 564 | int use_external_quantize_index; |
| 537 | 565 | int external_quantize_index; |
| 538 | − // A list of binary flags set from the external controller. | |
| 539 | − // Each binary flag indicates whether the frame is an arf or not. | |
| 540 | − const int *external_arf_indexes; | |
| 566 | + GOP_COMMAND gop_command; | |
| 541 | 567 | } ENCODE_COMMAND; |
| 542 | 568 | |
| 543 | 569 | static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) { |
| 544 | 570 | vp9_zero(*encode_command); |
| 545 | 571 | encode_command->use_external_quantize_index = 0; |
| 546 | 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 | |
| 550 | −static INLINE void encode_command_set_external_arf_indexes( | |
| 551 | − ENCODE_COMMAND *encode_command, const int *external_arf_indexes) { | |
| 552 | − encode_command->external_arf_indexes = external_arf_indexes; | |
| 576 | +static INLINE void encode_command_set_gop_command( | |
| 577 | + ENCODE_COMMAND *encode_command, GOP_COMMAND gop_command) { | |
| 578 | + encode_command->gop_command = gop_command; | |
| 553 | 579 | } |
| 554 | 580 | |
| 555 | 581 | static INLINE void encode_command_set_external_quantize_index( |
| @@ -2508,9 +2508,6 @@ typedef struct RANGE { · 2507 unmodified lines | ||
| 2508 | 2508 | * structs. |
| 2509 | 2509 | */ |
| 2510 | 2510 | static int get_gop_coding_frame_num( |
| 2511 | −#if CONFIG_RATE_CTRL | |
| 2512 | − const int *external_arf_indexes, | |
| 2513 | −#endif | |
| 2514 | 2511 | int *use_alt_ref, const FRAME_INFO *frame_info, |
| 2515 | 2512 | const FIRST_PASS_INFO *first_pass_info, const RATE_CONTROL *rc, |
| 2516 | 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 | 2523 | (frame_info->frame_height + frame_info->frame_width) / 4.0; |
| 2527 | 2524 | double zero_motion_accumulator = 1.0; |
| 2528 | 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 | 2527 | *use_alt_ref = 1; |
| 2549 | 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 | 2749 | gop_intra_factor = 1.0; |
| 2771 | 2750 | } |
| 2772 | 2751 | |
| 2773 | − { | |
| 2774 | − gop_coding_frames = get_gop_coding_frame_num( | |
| 2775 | 2752 | #if CONFIG_RATE_CTRL |
| 2776 | − cpi->encode_command.external_arf_indexes, | |
| 2777 | −#endif | |
| 2778 | − &use_alt_ref, frame_info, first_pass_info, rc, gf_start_show_idx, | |
| 2779 | − &active_gf_interval, gop_intra_factor, cpi->oxcf.lag_in_frames); | |
| 2780 | − use_alt_ref &= allow_alt_ref; | |
| 2753 | + { | |
| 2754 | + const GOP_COMMAND *gop_command = &cpi->encode_command.gop_command; | |
| 2755 | + assert(allow_alt_ref == 1); | |
| 2756 | + if (gop_command->use) { | |
| 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 | 2773 | // Was the group length constrained by the requirement for a new KF? |
| 2784 | 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 | 3694 | int *use_alt_ref, int *coding_frame_count, |
| 3705 | 3695 | int *first_show_idx, |
| 3706 | 3696 | int *last_gop_use_alt_ref) { |
| 3697 | + const GOP_COMMAND *gop_command = &cpi->encode_command.gop_command; | |
| 3707 | 3698 | // We make a copy of rc here because we want to get information from the |
| 3708 | 3699 | // encoder without changing its state. |
| 3709 | 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 | 3717 | *first_is_key_frame = 1; |
| 3727 | 3718 | } |
| 3728 | 3719 | |
| 3729 | − *coding_frame_count = vp9_get_gop_coding_frame_count( | |
| 3730 | − cpi->encode_command.external_arf_indexes, &cpi->oxcf, &cpi->frame_info, | |
| 3731 | − &cpi->twopass.first_pass_info, &rc, *first_show_idx, multi_layer_arf, | |
| 3732 | − allow_alt_ref, *first_is_key_frame, *last_gop_use_alt_ref, use_alt_ref); | |
| 3720 | + if (gop_command->use) { | |
| 3721 | + *coding_frame_count = gop_command_coding_frame_count(gop_command); | |
| 3722 | + *use_alt_ref = gop_command->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 | |
| 3735 | −int vp9_get_gop_coding_frame_count(const int *external_arf_indexes, | |
| 3736 | − const VP9EncoderConfig *oxcf, | |
| 3732 | +int vp9_get_gop_coding_frame_count(const VP9EncoderConfig *oxcf, | |
| 3737 | 3733 | const FRAME_INFO *frame_info, |
| 3738 | 3734 | const FIRST_PASS_INFO *first_pass_info, |
| 3739 | 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 | 3754 | frame_count = get_gop_coding_frame_num( |
| 3759 | −#if CONFIG_RATE_CTRL | |
| 3760 | − external_arf_indexes, | |
| 3761 | −#endif | |
| 3762 | 3755 | use_alt_ref, frame_info, first_pass_info, rc, show_idx, |
| 3763 | 3756 | &active_gf_interval, gop_intra_factor, oxcf->lag_in_frames); |
| 3764 | 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 | 3761 | // Under CONFIG_RATE_CTRL, once the first_pass_info is ready, the number of |
| 3769 | 3762 | // coding frames (including show frame and alt ref) can be determined. |
| 3770 | −int vp9_get_coding_frame_num(const int *external_arf_indexes, | |
| 3771 | − const VP9EncoderConfig *oxcf, | |
| 3763 | +int vp9_get_coding_frame_num(const VP9EncoderConfig *oxcf, | |
| 3772 | 3764 | const FRAME_INFO *frame_info, |
| 3773 | 3765 | const FIRST_PASS_INFO *first_pass_info, |
| 3774 | 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 | 3786 | gop_coding_frame_count = vp9_get_gop_coding_frame_count( |
| 3795 | − external_arf_indexes, oxcf, frame_info, first_pass_info, &rc, show_idx, | |
| 3796 | − multi_layer_arf, allow_alt_ref, first_is_key_frame, | |
| 3797 | − last_gop_use_alt_ref, &use_alt_ref); | |
| 3787 | + oxcf, frame_info, first_pass_info, &rc, show_idx, multi_layer_arf, | |
| 3788 | + allow_alt_ref, first_is_key_frame, last_gop_use_alt_ref, &use_alt_ref); | |
| 3798 | 3789 | |
| 3799 | 3790 | rc.source_alt_ref_active = use_alt_ref; |
| 3800 | 3791 | last_gop_use_alt_ref = use_alt_ref; |
| @@ -264,7 +264,6 @@ void vp9_get_next_group_of_picture(const struct VP9_COMP *cpi, · 263 unmodified lines | ||
| 264 | 264 | |
| 265 | 265 | /*!\brief Call this function before coding a new group of pictures to get |
| 266 | 266 | * information about it. |
| 267 | − * \param[in] external_arf_indexes External arf indexs passed in | |
| 268 | 267 | * \param[in] oxcf Encoder config |
| 269 | 268 | * \param[in] frame_info Frame info |
| 270 | 269 | * \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 | 279 | * \return Returns coding frame count |
| 281 | 280 | */ |
| 282 | −int vp9_get_gop_coding_frame_count(const int *external_arf_indexes, | |
| 283 | − const struct VP9EncoderConfig *oxcf, | |
| 281 | +int vp9_get_gop_coding_frame_count(const struct VP9EncoderConfig *oxcf, | |
| 284 | 282 | const FRAME_INFO *frame_info, |
| 285 | 283 | const FIRST_PASS_INFO *first_pass_info, |
| 286 | 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 | 286 | int first_is_key_frame, |
| 289 | 287 | int last_gop_use_alt_ref, int *use_alt_ref); |
| 290 | 288 | |
| 291 | −int vp9_get_coding_frame_num(const int *external_arf_indexes, | |
| 292 | − const struct VP9EncoderConfig *oxcf, | |
| 289 | +int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf, | |
| 293 | 290 | const FRAME_INFO *frame_info, |
| 294 | 291 | const FIRST_PASS_INFO *first_pass_info, |
| 295 | 292 | int multi_layer_arf, int allow_alt_ref); |
| @@ -813,6 +813,29 @@ T *GetVectorData(const std::vector<T> &v) { · 812 unmodified lines | ||
| 813 | 813 | return const_cast<T *>(v.data()); |
| 814 | 814 | } |
| 815 | 815 | |
| 816 | +static 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 | + | |
| 816 | 839 | void SimpleEncode::StartEncode() { |
| 817 | 840 | assert(impl_ptr_->first_pass_stats.size() > 0); |
| 818 | 841 | vpx_rational_t frame_rate = |
| @@ -834,9 +857,6 @@ void SimpleEncode::StartEncode() { · 15 unmodified lines | ||
| 834 | 857 | frame_coding_index_ = 0; |
| 835 | 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 | 860 | UpdateKeyFrameGroup(show_frame_count_); |
| 841 | 861 | |
| 842 | 862 | UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_, |
| @@ -914,6 +934,10 @@ void SimpleEncode::PostUpdateState( · 71 unmodified lines | ||
| 914 | 934 | |
| 915 | 935 | IncreaseGroupOfPictureIndex(&group_of_picture_); |
| 916 | 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 | 941 | // This function needs to be called after ref_frame_info_ is updated |
| 918 | 942 | // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup(). |
| 919 | 943 | UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_, |
| @@ -1002,8 +1026,24 @@ void SimpleEncode::EncodeFrameWithQuantizeIndex( · 82 unmodified lines | ||
| 1002 | 1026 | encode_command_reset_external_quantize_index(&impl_ptr_->cpi->encode_command); |
| 1003 | 1027 | } |
| 1004 | 1028 | |
| 1029 | +static 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 | + | |
| 1005 | 1041 | int 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 | 1047 | // These are the default settings for now. |
| 1008 | 1048 | const int multi_layer_arf = 0; |
| 1009 | 1049 | const int allow_alt_ref = 1; |
| @@ -1017,8 +1057,7 @@ int SimpleEncode::GetCodingFrameNum() const { · 7 unmodified lines | ||
| 1017 | 1057 | fps_init_first_pass_info(&first_pass_info, |
| 1018 | 1058 | GetVectorData(impl_ptr_->first_pass_stats), |
| 1019 | 1059 | num_frames_); |
| 1020 | − return vp9_get_coding_frame_num(external_arf_indexes_.data(), &oxcf, | |
| 1021 | − &frame_info, &first_pass_info, | |
| 1060 | + return vp9_get_coding_frame_num(&oxcf, &frame_info, &first_pass_info, | |
| 1022 | 1061 | multi_layer_arf, allow_alt_ref); |
| 1023 | 1062 | } |
| 1024 | 1063 | |