Skip to content

Commit

Permalink
Cherry-pick 265870.611@safari-7616.2.9.10-branch (6de8390). <bug>
Browse files Browse the repository at this point in the history
    Cherry-pick 505f26e. rdar://116293231

        VP9 additional changes related to CVE-2023-5217
        rdar://116293231

        Reviewed by Jean-Yves Avenard.

        Cherry-picking patches that do hardening of VP9 encoder reconfiguration:
        - 02ab555e992c191e5c509ed87b3cc48ed915b447
        - 263682c9a29395055f3b3afe2d97be1828a6223f

        I had to update CHECK_MEM_ERROR call site since we need to pass cm currently, while they do pass cm->error upstream.

        While we do not think we are exercising this code path of reconfiguring while encoding,
        it is future proof and low risk to cherry-pick these changes.

        * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/test/resize_test.cc:
        * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/common/vp9_alloccommon.c:
        (free_seg_map):
        (vp9_free_context_buffers):
        (vp9_alloc_context_buffers):
        * Source/ThirdParty/libwebrtc/Source/third_party/libvpx/source/libvpx/vp9/encoder/vp9_encoder.c:
        (free_copy_partition_data):
        (vp9_change_config):

        Canonical link: https://commits.webkit.org/267815.170@safari-7617-branch

    Canonical link: https://commits.webkit.org/265870.611@safari-7616.2.9.10-branch
  • Loading branch information
Dan Robson authored and aperezdc committed Oct 24, 2023
1 parent 325c110 commit 027d2d6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ void ScaleForFrameNumber(unsigned int frame, unsigned int initial_w,
*h = initial_h;
return;
}
if (frame < 100) {
*w = initial_w * 7 / 10;
*h = initial_h * 16 / 10;
return;
}
*w = initial_w * 7 / 10;
*h = initial_h * 16 / 10;
return;
}
if (frame < 10) {
Expand Down Expand Up @@ -578,7 +575,7 @@ TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
}
}

TEST_P(ResizeRealtimeTest, DISABLED_TestExternalResizeSmallerWidthBiggerSize) {
TEST_P(ResizeRealtimeTest, TestExternalResizeSmallerWidthBiggerSize) {
ResizingVideoSource video;
video.flag_codec_ = true;
video.smaller_width_larger_size_ = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static void free_seg_map(VP9_COMMON *cm) {
vpx_free(cm->seg_map_array[i]);
cm->seg_map_array[i] = NULL;
}
cm->seg_map_alloc_size = 0;

cm->current_frame_seg_map = NULL;
cm->last_frame_seg_map = NULL;
Expand Down Expand Up @@ -106,6 +107,7 @@ void vp9_free_context_buffers(VP9_COMMON *cm) {
cm->above_context = NULL;
vpx_free(cm->above_seg_context);
cm->above_seg_context = NULL;
cm->above_context_alloc_cols = 0;
vpx_free(cm->lf.lfm);
cm->lf.lfm = NULL;
}
Expand All @@ -131,13 +133,6 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
cm->free_mi(cm);
if (cm->alloc_mi(cm, new_mi_size)) goto fail;
}

if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
// Create the segmentation map structure and set to 0.
free_seg_map(cm);
if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
}

if (cm->above_context_alloc_cols < cm->mi_cols) {
vpx_free(cm->above_context);
cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
Expand All @@ -152,6 +147,12 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
cm->above_context_alloc_cols = cm->mi_cols;
}

if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
// Create the segmentation map structure and set to 0.
free_seg_map(cm);
if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
}

if (vp9_alloc_loop_filter(cm)) goto fail;

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,17 @@ static void alloc_copy_partition_data(VP9_COMP *cpi) {
}
}

static void free_copy_partition_data(VP9_COMP *cpi) {
vpx_free(cpi->prev_partition);
cpi->prev_partition = NULL;
vpx_free(cpi->prev_segment_id);
cpi->prev_segment_id = NULL;
vpx_free(cpi->prev_variance_low);
cpi->prev_variance_low = NULL;
vpx_free(cpi->copied_frame_cnt);
cpi->copied_frame_cnt = NULL;
}

void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
VP9_COMMON *const cm = &cpi->common;
RATE_CONTROL *const rc = &cpi->rc;
Expand Down Expand Up @@ -2049,6 +2060,8 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
if (cm->mi_alloc_size < new_mi_size) {
vp9_free_context_buffers(cm);
vp9_free_pc_tree(&cpi->td);
vpx_free(cpi->mbmi_ext_base);
alloc_compressor_data(cpi);
realloc_segmentation_maps(cpi);
cpi->initial_width = cpi->initial_height = 0;
Expand All @@ -2064,8 +2077,18 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
update_frame_size(cpi);

if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
memset(cpi->consec_zero_mv, 0,
cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
vpx_free(cpi->consec_zero_mv);
CHECK_MEM_ERROR(
cm, cpi->consec_zero_mv,
vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));

vpx_free(cpi->skin_map);
CHECK_MEM_ERROR(
cm, cpi->skin_map,
vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));

free_copy_partition_data(cpi);
alloc_copy_partition_data(cpi);
if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
vp9_cyclic_refresh_reset_resize(cpi);
rc->rc_1_frame = 0;
Expand Down

0 comments on commit 027d2d6

Please sign in to comment.