History

Merge changes Ie7c70a1d,I2c5abbe2,If41a1ea6,Id6ba4664,I156308bc

* changes: Add unit test for ref_frame_info Add key frame group info to SimpleEncode Add ref_frame_info to encode_frame_result Add init/update_frame_indexes() Add GetVectorData()

Angie Chiang committed 6 years ago

Commit 323b11a2 parents 5532775 e7aa1e39 files · +263 −58Browse files

Unified · Split

9 files
  1. test/simple_encode_test.cc +2 −0
  2. vp9/common/vp9_blockd.h +6 −0
  3. vp9/common/vp9_onyxc_int.h +39 −1
  4. vp9/decoder/vp9_decoder.c +1 −1
  5. vp9/encoder/vp9_encoder.c +101 −43
  6. vp9/encoder/vp9_encoder.h +16 −1
  7. vp9/encoder/vp9_firstpass.c +2 −1
  8. vp9/simple_encode.cc +72 −9
  9. vp9/simple_encode.h +24 −2
test/simple_encode_test.cc+2−0
@@ -92,6 +92,8 @@ TEST(SimpleEncode, EncodeFrame) { · 91 unmodified lines
9292 EXPECT_GE(encode_frame_result.psnr, 34)
9393 << "The psnr is supposed to be greater than 34 given the "
9494 "target_bitrate 1000 kbps";
95+ EXPECT_EQ(encode_frame_result.ref_frame_info,
96+ encode_frame_list[group_index].ref_frame_info);
9597 total_data_bit_size += encode_frame_result.coding_data_bit_size;
9698 ++frame_coding_index;
9799 }
vp9/common/vp9_blockd.h+6−0
@@ -64,6 +64,12 @@ typedef struct { · 63 unmodified lines
6464
6565 typedef int8_t MV_REFERENCE_FRAME;
6666
67+static INLINE int mv_ref_frame_to_inter_ref_idx(
68+ MV_REFERENCE_FRAME mv_ref_frame) {
69+ assert(mv_ref_frame >= LAST_FRAME && mv_ref_frame < MAX_REF_FRAMES);
70+ return mv_ref_frame - 1;
71+}
72+
6773 // This structure now relates to 8x8 block regions.
6874 typedef struct MODE_INFO {
6975 // Common for both INTER and INTRA blocks
vp9/common/vp9_onyxc_int.h+39−1
@@ -69,7 +69,18 @@ typedef struct { · 68 unmodified lines
6969 int mi_rows;
7070 int mi_cols;
7171 uint8_t released;
72− int frame_index;
72+
73+ // Note that frame_index/frame_coding_index are only set by set_frame_index()
74+ // on the encoder side.
75+
76+ // TODO(angiebird): Set frame_index/frame_coding_index on the decoder side
77+ // properly.
78+ int frame_index; // Display order in the video, it's equivalent to the
79+ // show_idx defined in EncodeFrameInfo.
80+#if CONFIG_RATE_CTRL
81+ int frame_coding_index; // The coding order (starting from zero) of this
82+ // frame.
83+#endif // CONFIG_RATE_CTRL
7384 vpx_codec_frame_buffer_t raw_frame_buffer;
7485 YV12_BUFFER_CONFIG buf;
7586 } RefCntBuffer;
@@ -226,7 +237,16 @@ typedef struct VP9Common { · 150 unmodified lines
226237 unsigned int frame_context_idx; /* Context to use/update */
227238 FRAME_COUNTS counts;
228239
240+ // TODO(angiebird): current_video_frame/current_frame_coding_index into a
241+ // structure
229242 unsigned int current_video_frame;
243+#if CONFIG_RATE_CTRL
244+ // Each show or no show frame is assigned with a coding index based on its
245+ // coding order (starting from zero).
246+
247+ // Current frame's coding index.
248+ int current_frame_coding_index;
249+#endif
230250 BITSTREAM_PROFILE profile;
231251
232252 // VPX_BITS_8 in profile 0 or 1, VPX_BITS_10 or VPX_BITS_12 in profile 2 or 3.
@@ -254,6 +274,24 @@ typedef struct VP9Common { · 21 unmodified lines
254274 int lf_row;
255275 } VP9_COMMON;
256276
277+static INLINE void init_frame_indexes(VP9_COMMON *cm) {
278+ cm->current_video_frame = 0;
279+#if CONFIG_RATE_CTRL
280+ cm->current_frame_coding_index = 0;
281+#endif // CONFIG_RATE_CTRL
282+}
283+
284+static INLINE void update_frame_indexes(VP9_COMMON *cm, int show_frame) {
285+ if (show_frame) {
286+ // Don't increment frame counters if this was an altref buffer
287+ // update not a real frame
288+ ++cm->current_video_frame;
289+ }
290+#if CONFIG_RATE_CTRL
291+ ++cm->current_frame_coding_index;
292+#endif // CONFIG_RATE_CTRL
293+}
294+
257295 typedef struct {
258296 int frame_width;
259297 int frame_height;
vp9/decoder/vp9_decoder.c+1−1
@@ -188,7 +188,7 @@ VP9Decoder *vp9_decoder_create(BufferPool *const pool) { · 187 unmodified lines
188188 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
189189 memset(&cm->next_ref_frame_map, -1, sizeof(cm->next_ref_frame_map));
190190
191− cm->current_video_frame = 0;
191+ init_frame_indexes(cm);
192192 pbi->ready_for_new_data = 1;
193193 pbi->common.buffer_pool = pool;
194194
vp9/encoder/vp9_encoder.c+101−43
@@ -2311,7 +2311,7 @@ VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf, · 2310 unmodified lines
23112311
23122312 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
23132313
2314− cm->current_video_frame = 0;
2314+ init_frame_indexes(cm);
23152315 cpi->partition_search_skippable_frame = 0;
23162316 cpi->tile_data = NULL;
23172317
@@ -4887,6 +4887,9 @@ static void set_frame_index(VP9_COMP *cpi, VP9_COMMON *cm) { · 2569 unmodified lines
48874887 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
48884888 ref_buffer->frame_index =
48894889 cm->current_video_frame + gf_group->arf_src_offset[gf_group->index];
4890+#if CONFIG_RATE_CTRL
4891+ ref_buffer->frame_coding_index = cm->current_frame_coding_index;
4892+#endif // CONFIG_RATE_CTRL
48904893 }
48914894 }
48924895
@@ -5080,9 +5083,22 @@ static void set_mb_wiener_variance(VP9_COMP *cpi) { · 187 unmodified lines
50805083 cpi->norm_wiener_variance = VPXMAX(1, cpi->norm_wiener_variance);
50815084 }
50825085
5083−static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
5084− uint8_t *dest,
5085− unsigned int *frame_flags) {
5086+#if !CONFIG_REALTIME_ONLY
5087+static void update_encode_frame_result(
5088+ int ref_frame_flags, FRAME_UPDATE_TYPE update_type,
5089+ const YV12_BUFFER_CONFIG *source_frame, const RefCntBuffer *coded_frame_buf,
5090+ RefCntBuffer *ref_frame_buf[MAX_INTER_REF_FRAMES], int quantize_index,
5091+ uint32_t bit_depth, uint32_t input_bit_depth, const FRAME_COUNTS *counts,
5092+#if CONFIG_RATE_CTRL
5093+ const PARTITION_INFO *partition_info,
5094+ const MOTION_VECTOR_INFO *motion_vector_info,
5095+#endif // CONFIG_RATE_CTRL
5096+ ENCODE_FRAME_RESULT *encode_frame_result);
5097+#endif // !CONFIG_REALTIME_ONLY
5098+
5099+static void encode_frame_to_data_rate(
5100+ VP9_COMP *cpi, size_t *size, uint8_t *dest, unsigned int *frame_flags,
5101+ ENCODE_FRAME_RESULT *encode_frame_result) {
50865102 VP9_COMMON *const cm = &cpi->common;
50875103 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
50885104 struct segmentation *const seg = &cm->seg;
@@ -5247,6 +5263,44 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size, · 158 unmodified lines
52475263 // build the bitstream
52485264 vp9_pack_bitstream(cpi, dest, size);
52495265
5266+#if CONFIG_REALTIME_ONLY
5267+ (void)encode_frame_result;
5268+ assert(encode_frame_result == NULL);
5269+#else // CONFIG_REALTIME_ONLY
5270+ if (encode_frame_result != NULL) {
5271+ const int ref_frame_flags = get_ref_frame_flags(cpi);
5272+ const RefCntBuffer *coded_frame_buf =
5273+ get_ref_cnt_buffer(cm, cm->new_fb_idx);
5274+ RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES];
5275+ get_ref_frame_bufs(cpi, ref_frame_bufs);
5276+ // update_encode_frame_result() depends on twopass.gf_group.index and
5277+ // cm->new_fb_idx, cpi->Source, cpi->lst_fb_idx, cpi->gld_fb_idx and
5278+ // cpi->alt_fb_idx are updated for current frame and have
5279+ // not been updated for the next frame yet.
5280+ // The update locations are as follows.
5281+ // 1) twopass.gf_group.index is initialized at define_gf_group by vp9_zero()
5282+ // for the first frame in the gf_group and is updated for the next frame at
5283+ // vp9_twopass_postencode_update().
5284+ // 2) cpi->Source is updated at the beginning of vp9_get_compressed_data()
5285+ // 3) cm->new_fb_idx is updated at the beginning of
5286+ // vp9_get_compressed_data() by get_free_fb(cm).
5287+ // 4) cpi->lst_fb_idx/gld_fb_idx/alt_fb_idx will be updated for the next
5288+ // frame at vp9_update_reference_frames().
5289+ // This function needs to be called before vp9_update_reference_frames().
5290+ // TODO(angiebird): Improve the codebase to make the update of frame
5291+ // dependent variables more robust.
5292+ update_encode_frame_result(
5293+ ref_frame_flags,
5294+ cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
5295+ cpi->Source, coded_frame_buf, ref_frame_bufs, vp9_get_quantizer(cpi),
5296+ cpi->oxcf.input_bit_depth, cm->bit_depth, cpi->td.counts,
5297+#if CONFIG_RATE_CTRL
5298+ cpi->partition_info, cpi->motion_vector_info,
5299+#endif // CONFIG_RATE_CTRL
5300+ encode_frame_result);
5301+ }
5302+#endif // CONFIG_REALTIME_ONLY
5303+
52505304 if (cpi->rc.use_post_encode_drop && cm->base_qindex < cpi->rc.worst_quality &&
52515305 cpi->svc.spatial_layer_id == 0 && post_encode_drop_cbr(cpi, size)) {
52525306 restore_coding_context(cpi);
@@ -5340,11 +5394,9 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size, · 87 unmodified lines
53405394
53415395 if (cm->show_frame) {
53425396 vp9_swap_mi_and_prev_mi(cm);
5343− // Don't increment frame counters if this was an altref buffer
5344− // update not a real frame
5345− ++cm->current_video_frame;
53465397 if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
53475398 }
5399+ update_frame_indexes(cm, cm->show_frame);
53485400
53495401 if (cpi->use_svc) {
53505402 cpi->svc
@@ -5370,7 +5422,8 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size, · 19 unmodified lines
53705422 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
53715423 unsigned int *frame_flags) {
53725424 vp9_rc_get_svc_params(cpi);
5373− encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5425+ encode_frame_to_data_rate(cpi, size, dest, frame_flags,
5426+ /*encode_frame_result = */ NULL);
53745427 }
53755428
53765429 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
@@ -5380,17 +5433,19 @@ static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest, · 3 unmodified lines
53805433 } else {
53815434 vp9_rc_get_one_pass_vbr_params(cpi);
53825435 }
5383− encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5436+ encode_frame_to_data_rate(cpi, size, dest, frame_flags,
5437+ /*encode_frame_result = */ NULL);
53845438 }
53855439
53865440 #if !CONFIG_REALTIME_ONLY
53875441 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5388− unsigned int *frame_flags) {
5442+ unsigned int *frame_flags,
5443+ ENCODE_FRAME_RESULT *encode_frame_result) {
53895444 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
53905445 #if CONFIG_MISMATCH_DEBUG
53915446 mismatch_move_frame_idx_w();
53925447 #endif
5393− encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5448+ encode_frame_to_data_rate(cpi, size, dest, frame_flags, encode_frame_result);
53945449 }
53955450 #endif // !CONFIG_REALTIME_ONLY
53965451
@@ -7249,11 +7304,10 @@ static void yv12_buffer_to_image_buffer(const YV12_BUFFER_CONFIG *yv12_buffer, · 1852 unmodified lines
72497304 }
72507305 }
72517306 #endif // CONFIG_RATE_CTRL
7252−
72537307 static void update_encode_frame_result(
7254− int show_idx, FRAME_UPDATE_TYPE update_type,
7255− const YV12_BUFFER_CONFIG *source_frame,
7256− const YV12_BUFFER_CONFIG *coded_frame, int quantize_index,
7308+ int ref_frame_flags, FRAME_UPDATE_TYPE update_type,
7309+ const YV12_BUFFER_CONFIG *source_frame, const RefCntBuffer *coded_frame_buf,
7310+ RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES], int quantize_index,
72577311 uint32_t bit_depth, uint32_t input_bit_depth, const FRAME_COUNTS *counts,
72587312 #if CONFIG_RATE_CTRL
72597313 const PARTITION_INFO *partition_info,
@@ -7263,29 +7317,54 @@ static void update_encode_frame_result( · 3 unmodified lines
72637317 #if CONFIG_RATE_CTRL
72647318 PSNR_STATS psnr;
72657319 #if CONFIG_VP9_HIGHBITDEPTH
7266− vpx_calc_highbd_psnr(source_frame, coded_frame, &psnr, bit_depth,
7320+ vpx_calc_highbd_psnr(source_frame, coded_frame_buf->buf, &psnr, bit_depth,
72677321 input_bit_depth);
72687322 #else // CONFIG_VP9_HIGHBITDEPTH
72697323 (void)bit_depth;
72707324 (void)input_bit_depth;
7271− vpx_calc_psnr(source_frame, coded_frame, &psnr);
7325+ vpx_calc_psnr(source_frame, &coded_frame_buf->buf, &psnr);
72727326 #endif // CONFIG_VP9_HIGHBITDEPTH
7327+ encode_frame_result->frame_coding_index = coded_frame_buf->frame_coding_index;
7328+
7329+ if (update_type != KF_UPDATE) {
7330+ const VP9_REFFRAME inter_ref_flags[MAX_INTER_REF_FRAMES] = { VP9_LAST_FLAG,
7331+ VP9_GOLD_FLAG,
7332+ VP9_ALT_FLAG };
7333+ int i;
7334+ for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
7335+ assert(ref_frame_bufs[i] != NULL);
7336+ encode_frame_result->ref_frame_coding_indexes[i] =
7337+ ref_frame_bufs[i]->frame_coding_index;
7338+ encode_frame_result->ref_frame_valid_list[i] =
7339+ (ref_frame_flags & inter_ref_flags[i]) != 0;
7340+ }
7341+ } else {
7342+ // No reference frame is available when this is a key frame.
7343+ int i;
7344+ for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
7345+ encode_frame_result->ref_frame_coding_indexes[i] = -1;
7346+ encode_frame_result->ref_frame_valid_list[i] = 0;
7347+ }
7348+ }
72737349 encode_frame_result->psnr = psnr.psnr[0];
72747350 encode_frame_result->sse = psnr.sse[0];
72757351 copy_frame_counts(counts, &encode_frame_result->frame_counts);
72767352 encode_frame_result->partition_info = partition_info;
72777353 encode_frame_result->motion_vector_info = motion_vector_info;
72787354 if (encode_frame_result->coded_frame.allocated) {
7279− yv12_buffer_to_image_buffer(coded_frame, &encode_frame_result->coded_frame);
7355+ yv12_buffer_to_image_buffer(&coded_frame_buf->buf,
7356+ &encode_frame_result->coded_frame);
72807357 }
72817358 #else // CONFIG_RATE_CTRL
7359+ (void)ref_frame_flags;
72827360 (void)bit_depth;
72837361 (void)input_bit_depth;
72847362 (void)source_frame;
7285− (void)coded_frame;
7363+ (void)coded_frame_buf;
7364+ (void)ref_frame_bufs;
72867365 (void)counts;
72877366 #endif // CONFIG_RATE_CTRL
7288− encode_frame_result->show_idx = show_idx;
7367+ encode_frame_result->show_idx = coded_frame_buf->frame_index;
72897368 encode_frame_result->update_type = update_type;
72907369 encode_frame_result->quantize_index = quantize_index;
72917370 }
@@ -7294,6 +7373,7 @@ static void update_encode_frame_result( · 2 unmodified lines
72947373 void vp9_init_encode_frame_result(ENCODE_FRAME_RESULT *encode_frame_result) {
72957374 encode_frame_result->show_idx = -1; // Actual encoding doesn't happen.
72967375 #if CONFIG_RATE_CTRL
7376+ encode_frame_result->frame_coding_index = -1;
72977377 vp9_zero(encode_frame_result->coded_frame);
72987378 encode_frame_result->coded_frame.allocated = 0;
72997379 #endif // CONFIG_RATE_CTRL
@@ -7575,29 +7655,7 @@ int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags, · 275 unmodified lines
75757655 cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
75767656 vp9_first_pass(cpi, source);
75777657 } else if (oxcf->pass == 2 && !cpi->use_svc) {
7578− Pass2Encode(cpi, size, dest, frame_flags);
7579− // update_encode_frame_result() depends on twopass.gf_group.index and
7580− // cm->new_fb_idx and cpi->Source are updated for current properly and have
7581− // not been updated for the next frame yet.
7582− // The update locations are as follows.
7583− // 1) twopass.gf_group.index is initialized at define_gf_group by vp9_zero()
7584− // for the first frame in the gf_group and is updated for the next frame at
7585− // vp9_twopass_postencode_update().
7586− // 2) cpi->Source is updated at the beginning of this function, i.e.
7587− // vp9_get_compressed_data()
7588− // 3) cm->new_fb_idx is updated at the beginning of this function by
7589− // get_free_fb(cm)
7590− // TODO(angiebird): Improve the codebase to make the update of frame
7591− // dependent variables more robust.
7592− update_encode_frame_result(
7593− source->show_idx,
7594− cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
7595− cpi->Source, get_frame_new_buffer(cm), vp9_get_quantizer(cpi),
7596− cpi->oxcf.input_bit_depth, cm->bit_depth, cpi->td.counts,
7597−#if CONFIG_RATE_CTRL
7598− cpi->partition_info, cpi->motion_vector_info,
7599−#endif // CONFIG_RATE_CTRL
7600− encode_frame_result);
7658+ Pass2Encode(cpi, size, dest, frame_flags, encode_frame_result);
76017659 vp9_twopass_postencode_update(cpi);
76027660 } else if (cpi->use_svc) {
76037661 SvcEncode(cpi, size, dest, frame_flags);
vp9/encoder/vp9_encoder.h+16−1
@@ -936,6 +936,9 @@ typedef struct ENCODE_FRAME_RESULT { · 935 unmodified lines
936936 int show_idx;
937937 FRAME_UPDATE_TYPE update_type;
938938 #if CONFIG_RATE_CTRL
939+ int frame_coding_index;
940+ int ref_frame_coding_indexes[MAX_INTER_REF_FRAMES];
941+ int ref_frame_valid_list[MAX_INTER_REF_FRAMES];
939942 double psnr;
940943 uint64_t sse;
941944 FRAME_COUNTS frame_counts;
@@ -1043,10 +1046,22 @@ static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi, · 101 unmodified lines
10431046 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
10441047 }
10451048
1046−static INLINE RefCntBuffer *get_ref_cnt_buffer(VP9_COMMON *cm, int fb_idx) {
1049+static INLINE RefCntBuffer *get_ref_cnt_buffer(const VP9_COMMON *cm,
1050+ int fb_idx) {
10471051 return fb_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[fb_idx] : NULL;
10481052 }
10491053
1054+static INLINE void get_ref_frame_bufs(
1055+ const VP9_COMP *cpi, RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES]) {
1056+ const VP9_COMMON *const cm = &cpi->common;
1057+ MV_REFERENCE_FRAME ref_frame;
1058+ for (ref_frame = LAST_FRAME; ref_frame < MAX_REF_FRAMES; ++ref_frame) {
1059+ int ref_frame_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
1060+ int inter_ref_idx = mv_ref_frame_to_inter_ref_idx(ref_frame);
1061+ ref_frame_bufs[inter_ref_idx] = get_ref_cnt_buffer(cm, ref_frame_buf_idx);
1062+ }
1063+}
1064+
10501065 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
10511066 const VP9_COMP *const cpi, MV_REFERENCE_FRAME ref_frame) {
10521067 const VP9_COMMON *const cm = &cpi->common;
vp9/encoder/vp9_firstpass.c+2−1
@@ -1478,7 +1478,8 @@ void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) { · 1477 unmodified lines
14781478 fclose(recon_file);
14791479 }
14801480
1481− ++cm->current_video_frame;
1481+ // In the first pass, every frame is considered as a show frame.
1482+ update_frame_indexes(cm, /*show_frame=*/1);
14821483 if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
14831484 }
14841485
vp9/simple_encode.cc+72−9
@@ -498,6 +498,14 @@ static void update_encode_frame_result( · 497 unmodified lines
498498 encode_frame_result->coding_data_bit_size =
499499 encode_frame_result->coding_data_byte_size * 8;
500500 encode_frame_result->show_idx = encode_frame_info->show_idx;
501+ encode_frame_result->coding_idx = encode_frame_info->frame_coding_index;
502+ assert(kRefFrameTypeMax == MAX_INTER_REF_FRAMES);
503+ for (int i = 0; i < kRefFrameTypeMax; ++i) {
504+ encode_frame_result->ref_frame_info.coding_indexes[i] =
505+ encode_frame_info->ref_frame_coding_indexes[i];
506+ encode_frame_result->ref_frame_info.valid_list[i] =
507+ encode_frame_info->ref_frame_valid_list[i];
508+ }
501509 encode_frame_result->frame_type =
502510 get_frame_type_from_update_type(encode_frame_info->update_type);
503511 encode_frame_result->psnr = encode_frame_info->psnr;
@@ -524,9 +532,18 @@ static int IsGroupOfPictureFinished(const GroupOfPicture &group_of_picture) { · 20 unmodified lines
524532 group_of_picture.encode_frame_list.size();
525533 }
526534
535+bool operator==(const RefFrameInfo &a, const RefFrameInfo &b) {
536+ bool match = true;
537+ for (int i = 0; i < kRefFrameTypeMax; ++i) {
538+ match &= a.coding_indexes[i] == b.coding_indexes[i];
539+ match &= a.valid_list[i] == b.valid_list[i];
540+ }
541+ return match;
542+}
543+
527544 static void InitRefFrameInfo(RefFrameInfo *ref_frame_info) {
528545 for (int i = 0; i < kRefFrameTypeMax; ++i) {
529− ref_frame_info->coding_indexes[i] = 0;
546+ ref_frame_info->coding_indexes[i] = -1;
530547 ref_frame_info->valid_list[i] = 0;
531548 }
532549 }
@@ -579,7 +596,7 @@ static void PostUpdateRefFrameInfo(FrameType frame_type, int frame_coding_index, · 46 unmodified lines
579596 }
580597
581598 if (past_index == last_index) {
582− ref_frame_valid_list[kRefFrameTypeLast] = 0;
599+ ref_frame_valid_list[kRefFrameTypePast] = 0;
583600 }
584601
585602 if (future_index == last_index) {
@@ -693,7 +710,13 @@ SimpleEncode::SimpleEncode(int frame_width, int frame_height, · 107 unmodified lines
693710 frame_rate_den_ = frame_rate_den;
694711 target_bitrate_ = target_bitrate;
695712 num_frames_ = num_frames;
713+
696714 frame_coding_index_ = 0;
715+ show_frame_count_ = 0;
716+
717+ key_frame_group_index_ = 0;
718+ key_frame_group_size_ = 0;
719+
697720 // TODO(angirbid): Should we keep a file pointer here or keep the file_path?
698721 assert(infile_path != nullptr);
699722 in_file_ = fopen(infile_path, "r");
@@ -789,6 +812,14 @@ void SimpleEncode::SetExternalGroupOfPicture( · 89 unmodified lines
789812 external_arf_indexes_ = external_arf_indexes;
790813 }
791814
815+template <typename T>
816+T *GetVectorData(const std::vector<T> &v) {
817+ if (v.empty()) {
818+ return nullptr;
819+ }
820+ return const_cast<T *>(v.data());
821+}
822+
792823 void SimpleEncode::StartEncode() {
793824 assert(impl_ptr_->first_pass_stats.size() > 0);
794825 vpx_rational_t frame_rate =
@@ -797,7 +828,7 @@ void SimpleEncode::StartEncode() { · 2 unmodified lines
797828 vp9_get_encoder_config(frame_width_, frame_height_, frame_rate,
798829 target_bitrate_, VPX_RC_LAST_PASS);
799830 vpx_fixed_buf_t stats;
800− stats.buf = impl_ptr_->first_pass_stats.data();
831+ stats.buf = GetVectorData(impl_ptr_->first_pass_stats);
801832 stats.sz = sizeof(impl_ptr_->first_pass_stats[0]) *
802833 impl_ptr_->first_pass_stats.size();
803834
@@ -806,9 +837,15 @@ void SimpleEncode::StartEncode() { · 2 unmodified lines
806837 impl_ptr_->cpi = init_encoder(&oxcf, impl_ptr_->img_fmt);
807838 vpx_img_alloc(&impl_ptr_->tmp_img, impl_ptr_->img_fmt, frame_width_,
808839 frame_height_, 1);
840+
809841 frame_coding_index_ = 0;
842+ show_frame_count_ = 0;
843+
810844 encode_command_set_external_arf_indexes(&impl_ptr_->cpi->encode_command,
811− external_arf_indexes_.data());
845+ GetVectorData(external_arf_indexes_));
846+
847+ UpdateKeyFrameGroup(show_frame_count_);
848+
812849 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
813850 &group_of_picture_);
814851 rewind(in_file_);
@@ -829,13 +866,26 @@ void SimpleEncode::EndEncode() { · 14 unmodified lines
829866 rewind(in_file_);
830867 }
831868
832−int SimpleEncode::GetKeyFrameGroupSize(int key_frame_index) const {
869+void SimpleEncode::UpdateKeyFrameGroup(int key_frame_show_index) {
833870 const VP9_COMP *cpi = impl_ptr_->cpi;
834− return vp9_get_frames_to_next_key(&cpi->oxcf, &cpi->frame_info,
835− &cpi->twopass.first_pass_info,
836− key_frame_index, cpi->rc.min_gf_interval);
871+ key_frame_group_index_ = 0;
872+ key_frame_group_size_ = vp9_get_frames_to_next_key(
873+ &cpi->oxcf, &cpi->frame_info, &cpi->twopass.first_pass_info,
874+ key_frame_show_index, cpi->rc.min_gf_interval);
875+ assert(key_frame_group_size_ > 0);
876+ // Init the reference frame info when a new key frame group appears.
877+ InitRefFrameInfo(&ref_frame_info_);
878+}
879+
880+void SimpleEncode::PostUpdateKeyFrameGroupIndex(FrameType frame_type) {
881+ if (frame_type != kFrameTypeAltRef) {
882+ // key_frame_group_index_ only counts show frames
883+ ++key_frame_group_index_;
884+ }
837885 }
838886
887+int SimpleEncode::GetKeyFrameGroupSize() const { return key_frame_group_size_; }
888+
839889 GroupOfPicture SimpleEncode::ObserveGroupOfPicture() const {
840890 return group_of_picture_;
841891 }
@@ -852,8 +902,20 @@ void SimpleEncode::PostUpdateState( · 10 unmodified lines
852902 PostUpdateRefFrameInfo(encode_frame_result.frame_type, frame_coding_index_,
853903 &ref_frame_info_);
854904 ++frame_coding_index_;
905+ if (encode_frame_result.frame_type != kFrameTypeAltRef) {
906+ // Only kFrameTypeAltRef is not a show frame
907+ ++show_frame_count_;
908+ }
909+
910+ PostUpdateKeyFrameGroupIndex(encode_frame_result.frame_type);
911+ if (key_frame_group_index_ == key_frame_group_size_) {
912+ UpdateKeyFrameGroup(show_frame_count_);
913+ }
914+
855915 IncreaseGroupOfPictureIndex(&group_of_picture_);
856916 if (IsGroupOfPictureFinished(group_of_picture_)) {
917+ // This function needs to be called after ref_frame_info_ is updated
918+ // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup().
857919 UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
858920 &group_of_picture_);
859921 }
@@ -952,7 +1014,8 @@ int SimpleEncode::GetCodingFrameNum() const { · 92 unmodified lines
9521014 target_bitrate_, VPX_RC_LAST_PASS);
9531015 FRAME_INFO frame_info = vp9_get_frame_info(&oxcf);
9541016 FIRST_PASS_INFO first_pass_info;
955− fps_init_first_pass_info(&first_pass_info, impl_ptr_->first_pass_stats.data(),
1017+ fps_init_first_pass_info(&first_pass_info,
1018+ GetVectorData(impl_ptr_->first_pass_stats),
9561019 num_frames_);
9571020 return vp9_get_coding_frame_num(external_arf_indexes_.data(), &oxcf,
9581021 &frame_info, &first_pass_info,
vp9/simple_encode.h+24−2
@@ -86,6 +86,8 @@ struct RefFrameInfo { · 85 unmodified lines
8686 int valid_list[kRefFrameTypeMax];
8787 };
8888
89+bool operator==(const RefFrameInfo &a, const RefFrameInfo &b);
90+
8991 struct EncodeFrameInfo {
9092 int show_idx;
9193
@@ -199,6 +201,8 @@ void output_image_buffer(const ImageBuffer &image_buffer, std::FILE *out_file); · 107 unmodified lines
199201 struct EncodeFrameResult {
200202 int show_idx;
201203 FrameType frame_type;
204+ int coding_idx;
205+ RefFrameInfo ref_frame_info;
202206 size_t coding_data_bit_size;
203207 size_t coding_data_byte_size;
204208 // The EncodeFrame will allocate a buffer, write the coding data into the
@@ -304,12 +308,11 @@ class SimpleEncode { · 99 unmodified lines
304308 // This function should be called after StartEncode() or EncodeFrame().
305309 void EndEncode();
306310
307− // Given a key_frame_index, computes this key frame group's size.
308311 // The key frame group size includes one key frame plus the number of
309312 // following inter frames. Note that the key frame group size only counts the
310313 // show frames. The number of no show frames like alternate refereces are not
311314 // counted.
312− int GetKeyFrameGroupSize(int key_frame_index) const;
315+ int GetKeyFrameGroupSize() const;
313316
314317 // Provides the group of pictures that the next coding frame is in.
315318 // Only call this function between StartEncode() and EndEncode()
@@ -353,11 +356,30 @@ class SimpleEncode { · 37 unmodified lines
353356 std::vector<int> external_arf_indexes_;
354357 GroupOfPicture group_of_picture_;
355358
359+ // The key frame group size includes one key frame plus the number of
360+ // following inter frames. Note that the key frame group size only counts the
361+ // show frames. The number of no show frames like alternate refereces are not
362+ // counted.
363+ int key_frame_group_size_;
364+
365+ // The index for the to-be-coded show frame in the key frame group.
366+ int key_frame_group_index_;
367+
368+ // Update key_frame_group_size_, reset key_frame_group_index_ and init
369+ // ref_frame_info_.
370+ void UpdateKeyFrameGroup(int key_frame_show_index);
371+
372+ // Update key_frame_group_index_.
373+ void PostUpdateKeyFrameGroupIndex(FrameType frame_type);
374+
356375 // Each show or no show frame is assigned with a coding index based on its
357376 // coding order (starting from zero) in the coding process of the entire
358377 // video. The coding index of the to-be-coded frame.
359378 int frame_coding_index_;
360379
380+ // Number of show frames we have coded so far.
381+ int show_frame_count_;
382+
361383 // TODO(angiebird): Do we need to reset ref_frames_info_ when the next key
362384 // frame appears?
363385 // Reference frames info of the to-be-coded frame.

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