Skip to content

Commit c8aeff2

Browse files
authored
Pickrst: Size restoration line buffers to frame width (#5337)
- Replace the RESTORATION_LINEBUFFER_WIDTH based static buffers, sized for a max supported picture with buffers allocated per plane from the actual frame width. - Replace per RU alloc and free in favor of per frame allocation no stats changed
1 parent f90877c commit c8aeff2

5 files changed

Lines changed: 69 additions & 30 deletions

File tree

av2/common/alloccommon.c

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,56 @@ void av2_alloc_cdef_buffers(AV2_COMMON *const cm,
296296
cdef_info->allocated_mi_rows);
297297
}
298298

299+
void av2_alloc_restoration_line_buffers(AV2_COMMON *const cm,
300+
RestorationLineBuffers **rlbs_ptr) {
301+
if (!*rlbs_ptr) {
302+
CHECK_MEM_ERROR(cm, *rlbs_ptr, avm_calloc(1, sizeof(**rlbs_ptr)));
303+
}
304+
305+
RestorationLineBuffers *rlbs = *rlbs_ptr;
306+
const int frame_w = cm->mi_params.mi_cols << MI_SIZE_LOG2;
307+
for (PLANE_TYPE ptype = PLANE_TYPE_Y;
308+
ptype <= (cm->seq_params.monochrome ? PLANE_TYPE_Y : PLANE_TYPE_UV);
309+
++ptype) {
310+
// allocate for line buffer width.
311+
const int ss_x = ptype && cm->seq_params.subsampling_x;
312+
const int plane_w =
313+
((frame_w + ss_x) >> ss_x) + 2 * RESTORATION_BORDER_HORZ;
314+
const int stride = ALIGN_POWER_OF_TWO(plane_w, 5);
315+
316+
if (stride > rlbs->line_stride[ptype]) {
317+
for (int i = 0; i < RESTORATION_BORDER_VERT; ++i) {
318+
avm_free(rlbs->tmp_save_above[ptype][i]);
319+
rlbs->tmp_save_above[ptype][i] = NULL;
320+
CHECK_MEM_ERROR(
321+
cm, rlbs->tmp_save_above[ptype][i],
322+
avm_memalign(32, stride * sizeof(*rlbs->tmp_save_above[ptype][i])));
323+
324+
avm_free(rlbs->tmp_save_below[ptype][i]);
325+
rlbs->tmp_save_below[ptype][i] = NULL;
326+
CHECK_MEM_ERROR(
327+
cm, rlbs->tmp_save_below[ptype][i],
328+
avm_memalign(32, stride * sizeof(*rlbs->tmp_save_below[ptype][i])));
329+
}
330+
rlbs->line_stride[ptype] = stride;
331+
}
332+
}
333+
}
334+
335+
void av2_free_restoration_line_buffers(RestorationLineBuffers *rlbs) {
336+
if (rlbs) {
337+
for (PLANE_TYPE ptype = PLANE_TYPE_Y; ptype < PLANE_TYPES; ++ptype) {
338+
for (int i = 0; i < RESTORATION_BORDER_VERT; ++i) {
339+
avm_free(rlbs->tmp_save_above[ptype][i]);
340+
rlbs->tmp_save_above[ptype][i] = NULL;
341+
avm_free(rlbs->tmp_save_below[ptype][i]);
342+
rlbs->tmp_save_below[ptype][i] = NULL;
343+
}
344+
}
345+
avm_free(rlbs);
346+
}
347+
}
348+
299349
// Assumes cm->rst_info[p].restoration_unit_size is already initialized
300350
void av2_alloc_restoration_buffers(AV2_COMMON *cm) {
301351
const int num_planes = av2_num_planes(cm);
@@ -307,9 +357,8 @@ void av2_alloc_restoration_buffers(AV2_COMMON *cm) {
307357
translate_pcwiener_filters_to_wienerns(cm);
308358
}
309359

310-
if (cm->rlbs == NULL) {
311-
CHECK_MEM_ERROR(cm, cm->rlbs, avm_malloc(sizeof(RestorationLineBuffers)));
312-
}
360+
av2_alloc_restoration_line_buffers(cm, &cm->rlbs);
361+
313362
if (cm->lru_stripe_buf == NULL) {
314363
CHECK_MEM_ERROR(
315364
cm, cm->lru_stripe_buf,
@@ -373,7 +422,7 @@ void av2_free_restoration_buffers(AV2_COMMON *cm) {
373422
int p;
374423
for (p = 0; p < MAX_MB_PLANE; ++p)
375424
av2_free_restoration_struct(&cm->rst_info[p]);
376-
avm_free(cm->rlbs);
425+
av2_free_restoration_line_buffers(cm->rlbs);
377426
cm->rlbs = NULL;
378427
avm_free(cm->lru_stripe_buf);
379428
cm->lru_stripe_buf = NULL;

av2/common/alloccommon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct CommonContexts;
2727
struct CommonModeInfoParams;
2828
struct AV2CdefWorker;
2929
struct AV2CdefSyncData;
30+
struct RestorationLineBuffers;
3031

3132
void av2_remove_common(struct AV2Common *cm);
3233

@@ -56,6 +57,11 @@ void av2_alloc_restoration_boundary_buffers(struct AV2Common *cm,
5657
int num_planes);
5758
void av2_free_restoration_buffers(struct AV2Common *cm);
5859

60+
void av2_alloc_restoration_line_buffers(
61+
struct AV2Common *const cm, struct RestorationLineBuffers **rlbs_ptr);
62+
63+
void av2_free_restoration_line_buffers(struct RestorationLineBuffers *rlbs);
64+
5965
int av2_get_MBs(int width, int height);
6066

6167
#ifdef __cplusplus

av2/common/restoration.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,13 +1665,6 @@ uint16_t *wienerns_copy_luma_with_virtual_lines(struct AV2Common *cm,
16651665
int height_y = frame_buf->heights[AVM_PLANE_Y];
16661666
int width_uv = frame_buf->widths[1];
16671667
int height_uv = frame_buf->heights[1];
1668-
1669-
if (width_y > RESTORATION_LINEBUFFER_WIDTH)
1670-
avm_internal_error(
1671-
&cm->error, AVM_CODEC_ERROR,
1672-
"picture width is larger than 8192 * 8, need to disable "
1673-
"cross-component wienerns in this software implementation");
1674-
16751668
int in_stride = frame_buf->strides[AVM_PLANE_Y];
16761669
int border = WIENERNS_UV_BRD;
16771670
int resized_luma_stride = width_uv + 2 * WIENERNS_UV_BRD;

av2/common/restoration.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,12 @@ typedef struct {
260260
#define RESTORATION_LINEBUFFER_WIDTH \
261261
(MAX_SUPPORTED_PIC_WIDTH_IN_CCALF_IMP * 3 / 2 + 2 * RESTORATION_BORDER_HORZ)
262262

263-
// Similarly, the column buffers (used when we're at a vertical tile edge
264-
// that we can't filter across) need space for one processing unit's worth
265-
// of pixels, plus the top/bottom border width
266-
#define RESTORATION_COLBUFFER_HEIGHT \
267-
(RESTORATION_PROC_UNIT_SIZE + 2 * RESTORATION_BORDER_VERT)
268-
269-
typedef struct {
263+
typedef struct RestorationLineBuffers {
270264
// Temporary buffers to save/restore 3 lines above/below the restoration
271265
// stripe.
272-
uint16_t tmp_save_above[2][RESTORATION_BORDER_VERT]
273-
[RESTORATION_LINEBUFFER_WIDTH];
274-
uint16_t tmp_save_below[2][RESTORATION_BORDER_VERT]
275-
[RESTORATION_LINEBUFFER_WIDTH];
276-
uint16_t tmp_save_left[2][RESTORATION_COLBUFFER_HEIGHT]
277-
[RESTORATION_BORDER_HORZ];
278-
uint16_t tmp_save_right[2][RESTORATION_COLBUFFER_HEIGHT]
279-
[RESTORATION_BORDER_HORZ];
266+
uint16_t *tmp_save_above[PLANE_TYPES][RESTORATION_BORDER_VERT];
267+
uint16_t *tmp_save_below[PLANE_TYPES][RESTORATION_BORDER_VERT];
268+
int line_stride[PLANE_TYPES];
280269
} RestorationLineBuffers;
281270
/*!\endcond */
282271

av2/encoder/pickrst.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ typedef struct {
142142

143143
int luma_stride;
144144

145+
RestorationLineBuffers *rlbs;
145146
// Temporary storage used by *wienerns_filter* functions.
146147
double *wienerns_tmpbuf;
147148

@@ -325,9 +326,7 @@ static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
325326
const int plane = rsc->plane;
326327
const int is_uv = plane > 0;
327328
const RestorationInfo *rsi = &cm->rst_info[plane];
328-
RestorationLineBuffers *rlbs = avm_malloc(sizeof(RestorationLineBuffers));
329-
if (rlbs == NULL)
330-
fprintf(stderr, "rlbs buffer does not allocate successfully\n");
329+
RestorationLineBuffers *rlbs = rsc->rlbs;
331330
const int bit_depth = cm->seq_params.bit_depth;
332331

333332
const YV12_BUFFER_CONFIG *fts = &cm->cur_frame->buf;
@@ -342,7 +341,6 @@ static int64_t try_restoration_unit(const RestSearchCtxt *rsc,
342341
rsc->plane_width, cm->seq_params.disable_loopfilters_across_tiles,
343342
optimized_lr);
344343

345-
if (rlbs != NULL) avm_free(rlbs);
346344
return sse_restoration_unit(limits, rsc->src, rsc->dst, plane);
347345
}
348346

@@ -3845,6 +3843,9 @@ void av2_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV2_COMP *cpi) {
38453843
uint16_t *luma_virtual = NULL;
38463844
uint16_t *luma_virtual_buf;
38473845

3846+
rsc.rlbs = NULL;
3847+
av2_alloc_restoration_line_buffers(cm, &rsc.rlbs);
3848+
38483849
luma_virtual_buf = wienerns_copy_luma_with_virtual_lines(cm, &luma_virtual);
38493850
rsc.luma = luma_virtual;
38503851

@@ -4027,6 +4028,7 @@ void av2_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV2_COMP *cpi) {
40274028
avm_free(rusi);
40284029
free(luma_buf);
40294030
free(luma_virtual_buf);
4031+
av2_free_restoration_line_buffers(rsc.rlbs);
40304032
avm_free(rsc.wienerns_tmpbuf);
40314033
avm_vector_destroy(&wienerns_stats);
40324034
avm_vector_destroy(&unit_stack);

0 commit comments

Comments
 (0)