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
532532 int_mv mv[2];
533533 } MOTION_VECTOR_INFO;
534534
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+
535563 typedef struct ENCODE_COMMAND {
536564 int use_external_quantize_index;
537565 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;
541567 } ENCODE_COMMAND;
542568
543569 static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) {
544570 vp9_zero(*encode_command);
545571 encode_command->use_external_quantize_index = 0;
546572 encode_command->external_quantize_index = -1;
547− encode_command->external_arf_indexes = NULL;
573+ gop_command_off(&encode_command->gop_command);
548574 }
549575
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;
553579 }
554580
555581 static 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
25082508 * structs.
25092509 */
25102510 static int get_gop_coding_frame_num(
2511−#if CONFIG_RATE_CTRL
2512− const int *external_arf_indexes,
2513−#endif
25142511 int *use_alt_ref, const FRAME_INFO *frame_info,
25152512 const FIRST_PASS_INFO *first_pass_info, const RATE_CONTROL *rc,
25162513 int gf_start_show_idx, const RANGE *active_gf_interval,
@@ -2526,24 +2523,6 @@ static int get_gop_coding_frame_num( · 9 unmodified lines
25262523 (frame_info->frame_height + frame_info->frame_width) / 4.0;
25272524 double zero_motion_accumulator = 1.0;
25282525 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
25472526
25482527 *use_alt_ref = 1;
25492528 gop_coding_frames = 0;
@@ -2770,15 +2749,26 @@ static void define_gf_group(VP9_COMP *cpi, int gf_start_show_idx) { · 220 unmodified lines
27702749 gop_intra_factor = 1.0;
27712750 }
27722751
2773− {
2774− gop_coding_frames = get_gop_coding_frame_num(
27752752 #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+ }
27812765 }
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
27822772
27832773 // Was the group length constrained by the requirement for a new KF?
27842774 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
37043694 int *use_alt_ref, int *coding_frame_count,
37053695 int *first_show_idx,
37063696 int *last_gop_use_alt_ref) {
3697+ const GOP_COMMAND *gop_command = &cpi->encode_command.gop_command;
37073698 // We make a copy of rc here because we want to get information from the
37083699 // encoder without changing its state.
37093700 // 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
37263717 *first_is_key_frame = 1;
37273718 }
37283719
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+ }
37333730 }
37343731
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,
37373733 const FRAME_INFO *frame_info,
37383734 const FIRST_PASS_INFO *first_pass_info,
37393735 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
37563752 }
37573753
37583754 frame_count = get_gop_coding_frame_num(
3759−#if CONFIG_RATE_CTRL
3760− external_arf_indexes,
3761−#endif
37623755 use_alt_ref, frame_info, first_pass_info, rc, show_idx,
37633756 &active_gf_interval, gop_intra_factor, oxcf->lag_in_frames);
37643757 *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
37673760
37683761 // Under CONFIG_RATE_CTRL, once the first_pass_info is ready, the number of
37693762 // 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,
37723764 const FRAME_INFO *frame_info,
37733765 const FIRST_PASS_INFO *first_pass_info,
37743766 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
37923784 }
37933785
37943786 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);
37983789
37993790 rc.source_alt_ref_active = use_alt_ref;
38003791 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
264264
265265 /*!\brief Call this function before coding a new group of pictures to get
266266 * information about it.
267− * \param[in] external_arf_indexes External arf indexs passed in
268267 * \param[in] oxcf Encoder config
269268 * \param[in] frame_info Frame info
270269 * \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
279278 *
280279 * \return Returns coding frame count
281280 */
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,
284282 const FRAME_INFO *frame_info,
285283 const FIRST_PASS_INFO *first_pass_info,
286284 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
288286 int first_is_key_frame,
289287 int last_gop_use_alt_ref, int *use_alt_ref);
290288
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,
293290 const FRAME_INFO *frame_info,
294291 const FIRST_PASS_INFO *first_pass_info,
295292 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
813813 return const_cast<T *>(v.data());
814814 }
815815
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+
816839 void SimpleEncode::StartEncode() {
817840 assert(impl_ptr_->first_pass_stats.size() > 0);
818841 vpx_rational_t frame_rate =
@@ -834,9 +857,6 @@ void SimpleEncode::StartEncode() { · 15 unmodified lines
834857 frame_coding_index_ = 0;
835858 show_frame_count_ = 0;
836859
837− encode_command_set_external_arf_indexes(&impl_ptr_->cpi->encode_command,
838− GetVectorData(external_arf_indexes_));
839−
840860 UpdateKeyFrameGroup(show_frame_count_);
841861
842862 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
@@ -914,6 +934,10 @@ void SimpleEncode::PostUpdateState( · 71 unmodified lines
914934
915935 IncreaseGroupOfPictureIndex(&group_of_picture_);
916936 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);
917941 // This function needs to be called after ref_frame_info_ is updated
918942 // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup().
919943 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
@@ -1002,8 +1026,24 @@ void SimpleEncode::EncodeFrameWithQuantizeIndex( · 82 unmodified lines
10021026 encode_command_reset_external_quantize_index(&impl_ptr_->cpi->encode_command);
10031027 }
10041028
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+
10051041 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+
10071047 // These are the default settings for now.
10081048 const int multi_layer_arf = 0;
10091049 const int allow_alt_ref = 1;
@@ -1017,8 +1057,7 @@ int SimpleEncode::GetCodingFrameNum() const { · 7 unmodified lines
10171057 fps_init_first_pass_info(&first_pass_info,
10181058 GetVectorData(impl_ptr_->first_pass_stats),
10191059 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,
10221061 multi_layer_arf, allow_alt_ref);
10231062 }
10241063

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