PR #25977 opened by kierank URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25977 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25977.patch
The pixel side of error concealment assumes 8-bit 4:2:0: put_dc(), the DC estimate, the boundary score in guess_mv(), the SAD in is_intra_more_likely() and the h/v block filters read and write single bytes at byte offset mb_x * 16, clip with the 8-bit crop table, and treat chroma as one 8x8 block per MB at mb_x * 8, mb_y * 8. H.264 runs this code for every bit depth and chroma format it decodes (enable_er is on by default unless slice threading is used), with these results: - 9 to 14 bits: concealed MBs are written as bytes into 16-bit samples. Byte offset mb_x * 16 is sample mb_x * 8, so the writes for every MB column land in the left half of the picture, 8 samples wide. put_dc() stores byte-sized DC / 8 values, and the block filters do 8-bit clipped arithmetic on the two bytes of a sample. This gives sample values such as 0x8080 (both bytes of a sample set to the 8-bit grey level 128), far above the 1023 maximum of a 10-bit picture. Motion compensation then carries them into the following pictures through the references. The SIMD functions only have to match C for in-range input, and the decoded output of such files can also differ between -cpuflags 0 and the default. The MV choice in guess_mv() and the intra/inter decision compare the wrong bytes too. - 4:2:2: chroma DC concealment and filtering cover only the top half of each MB's 8x16 chroma block, at the vertical position of a 4:2:0 picture. - 4:4:4: chroma is concealed as an 8x8 block per MB, at 4:2:0 positions. Concealment by motion compensation through h264_er_decode_mb() is not affected; it goes through the normal H.264 reconstruction. For H.264, take the bit depth and the chroma subsampling from the pixel format of cur_pic: - read and write 16-bit samples above 8 bits; - clip every written sample to [0, (1 << bit_depth) - 1]; - compute and write chroma DC over the MB's (16 >> chroma_x_shift) x (16 >> chroma_y_shift) block, and run the chroma block filters over all of its 8x8 blocks; - use mid-grey at the bit depth as the default DC; - compute the is_intra_more_likely() SAD on 16-bit samples. dc_val keeps holding 8 * mean, shifted right by bit_depth - 10 above 10 bits, so that it and the output of filter181() stay within int16_t. Everything is derived from the bit depth, so 9, 10, 12 and 14 bits are all covered. 8-bit 4:2:0 H.264 is computed exactly as before (same rounding, clipping and indices), and other codecs keep the 8-bit 4:2:0 path. The new ERContext fields are placed after mb_height, in what is padding on 64-bit targets, so that no other field moves there. Signed-off-by: Kieran Kunhya <[email protected]> # Summary of changes Briefly describe what this PR does and why. <!-- If this PR requires new FATE test samples, attach them to the PR and list their target paths below (relative to the fate-suite root). Attached filenames must match the sample's filename: ```fate-samples # e.g. vorbis/new-sample.ogg ``` --> >From 8af0e0e4c772a25cc692dd8fb4104c733b9e6f2d Mon Sep 17 00:00:00 2001 From: Kieran Kunhya <[email protected]> Date: Thu, 12 Nov 2026 16:12:35 +0000 Subject: [PATCH] avcodec/error_resilience: support high bit depth and 4:2:2/4:4:4 for H.264 The pixel side of error concealment assumes 8-bit 4:2:0: put_dc(), the DC estimate, the boundary score in guess_mv(), the SAD in is_intra_more_likely() and the h/v block filters read and write single bytes at byte offset mb_x * 16, clip with the 8-bit crop table, and treat chroma as one 8x8 block per MB at mb_x * 8, mb_y * 8. H.264 runs this code for every bit depth and chroma format it decodes (enable_er is on by default unless slice threading is used), with these results: - 9 to 14 bits: concealed MBs are written as bytes into 16-bit samples. Byte offset mb_x * 16 is sample mb_x * 8, so the writes for every MB column land in the left half of the picture, 8 samples wide. put_dc() stores byte-sized DC / 8 values, and the block filters do 8-bit clipped arithmetic on the two bytes of a sample. This gives sample values such as 0x8080 (both bytes of a sample set to the 8-bit grey level 128), far above the 1023 maximum of a 10-bit picture. Motion compensation then carries them into the following pictures through the references. The SIMD functions only have to match C for in-range input, and the decoded output of such files can also differ between -cpuflags 0 and the default. The MV choice in guess_mv() and the intra/inter decision compare the wrong bytes too. - 4:2:2: chroma DC concealment and filtering cover only the top half of each MB's 8x16 chroma block, at the vertical position of a 4:2:0 picture. - 4:4:4: chroma is concealed as an 8x8 block per MB, at 4:2:0 positions. Concealment by motion compensation through h264_er_decode_mb() is not affected; it goes through the normal H.264 reconstruction. For H.264, take the bit depth and the chroma subsampling from the pixel format of cur_pic: - read and write 16-bit samples above 8 bits; - clip every written sample to [0, (1 << bit_depth) - 1]; - compute and write chroma DC over the MB's (16 >> chroma_x_shift) x (16 >> chroma_y_shift) block, and run the chroma block filters over all of its 8x8 blocks; - use mid-grey at the bit depth as the default DC; - compute the is_intra_more_likely() SAD on 16-bit samples. dc_val keeps holding 8 * mean, shifted right by bit_depth - 10 above 10 bits, so that it and the output of filter181() stay within int16_t. Everything is derived from the bit depth, so 9, 10, 12 and 14 bits are all covered. 8-bit 4:2:0 H.264 is computed exactly as before (same rounding, clipping and indices), and other codecs keep the 8-bit 4:2:0 path. The new ERContext fields are placed after mb_height, in what is padding on 64-bit targets, so that no other field moves there. Signed-off-by: Kieran Kunhya <[email protected]> --- libavcodec/error_resilience.c | 298 +++++++++++++++++++++------------- libavcodec/error_resilience.h | 5 + 2 files changed, 194 insertions(+), 109 deletions(-) diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c index 8cf5bc6a3c7..93cfb3ab530 100644 --- a/libavcodec/error_resilience.c +++ b/libavcodec/error_resilience.c @@ -29,7 +29,9 @@ #include "libavutil/avassert.h" #include "libavutil/attributes.h" +#include "libavutil/intreadwrite.h" #include "libavutil/mem.h" +#include "libavutil/pixdesc.h" #include "avcodec.h" #include "error_resilience.h" #include "mathops.h" @@ -73,6 +75,44 @@ static void set_mv_strides(ERContext *s, ptrdiff_t *mv_step, ptrdiff_t *stride) } } +static av_always_inline int get_px(const uint8_t *row, int x, int pixel_shift) +{ + return pixel_shift ? AV_RN16A(row + 2 * x) : row[x]; +} + +static av_always_inline void set_px(uint8_t *row, int x, int pixel_shift, int v) +{ + if (pixel_shift) + AV_WN16A(row + 2 * x, v); + else + row[x] = v; +} + +static int er_pixel_max(const ERContext *s) +{ + return (1 << s->bit_depth) - 1; +} + +/** + * dc_val holds 8 * mean >> dc_shift, which keeps it and the result of + * filter181() within int16_t above 10 bits. + */ +static int er_dc_shift(const ERContext *s) +{ + return FFMAX(s->bit_depth - 10, 0); +} + +/** + * Sample value of a DC as stored in dc_val, clipped. + */ +static int dc_to_px(const ERContext *s, int dc) +{ + const int max = er_pixel_max(s), shift = er_dc_shift(s); + + dc = av_clip(dc, 0, 8 * max >> shift); + return FFMIN((dc << shift) >> 3, max); +} + /** * Replace the current MB with a flat dc-only version. */ @@ -80,36 +120,27 @@ static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y) { int *linesize = s->cur_pic.f->linesize; + const int ps = s->pixel_shift; + const int cw = 16 >> s->chroma_x_shift, ch = 16 >> s->chroma_y_shift; int dc, dcu, dcv, y, i; for (i = 0; i < 4; i++) { - dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride]; - if (dc < 0) - dc = 0; - else if (dc > 2040) - dc = 2040; + dc = dc_to_px(s, s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride]); for (y = 0; y < 8; y++) { + uint8_t *row = dest_y + (y + (i >> 1) * 8) * linesize[0]; int x; for (x = 0; x < 8; x++) - dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8; + set_px(row, x + (i & 1) * 8, ps, dc); } } - dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride]; - dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride]; - if (dcu < 0) - dcu = 0; - else if (dcu > 2040) - dcu = 2040; - if (dcv < 0) - dcv = 0; - else if (dcv > 2040) - dcv = 2040; + dcu = dc_to_px(s, s->dc_val[1][mb_x + mb_y * s->mb_stride]); + dcv = dc_to_px(s, s->dc_val[2][mb_x + mb_y * s->mb_stride]); if (dest_cr) - for (y = 0; y < 8; y++) { + for (y = 0; y < ch; y++) { int x; - for (x = 0; x < 8; x++) { - dest_cb[x + y * linesize[1]] = dcu / 8; - dest_cr[x + y * linesize[2]] = dcv / 8; + for (x = 0; x < cw; x++) { + set_px(dest_cb + y * linesize[1], x, ps, dcu); + set_px(dest_cr + y * linesize[2], x, ps, dcv); } } } @@ -159,6 +190,8 @@ static void guess_dc(ERContext *s, int16_t *dc, int w, int h, ptrdiff_t stride, int is_luma) { int b_x, b_y; + /* mid-grey */ + const int mid = (8 * (er_pixel_max(s) + 1) >> 1) >> er_dc_shift(s); int16_t (*col )[4] = av_malloc_array(stride, h*sizeof( int16_t)*4); uint32_t (*dist)[4] = av_malloc_array(stride, h*sizeof(uint32_t)*4); @@ -168,7 +201,7 @@ static void guess_dc(ERContext *s, int16_t *dc, int w, } for(b_y=0; b_y<h; b_y++){ - int color= 1024; + int color= mid; int distance= -1; for(b_x=0; b_x<w; b_x++){ int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride; @@ -181,7 +214,7 @@ static void guess_dc(ERContext *s, int16_t *dc, int w, col [b_x + b_y*stride][1]= color; dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999; } - color= 1024; + color= mid; distance= -1; for(b_x=w-1; b_x>=0; b_x--){ int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride; @@ -196,7 +229,7 @@ static void guess_dc(ERContext *s, int16_t *dc, int w, } } for(b_x=0; b_x<w; b_x++){ - int color= 1024; + int color= mid; int distance= -1; for(b_y=0; b_y<h; b_y++){ int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride; @@ -209,7 +242,7 @@ static void guess_dc(ERContext *s, int16_t *dc, int w, col [b_x + b_y*stride][3]= color; dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999; } - color= 1024; + color= mid; distance= -1; for(b_y=h-1; b_y>=0; b_y--){ int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride; @@ -257,27 +290,28 @@ fail: * simple horizontal deblocking filter used for error resilience * @param w width in 8 pixel blocks * @param h height in 8 pixel blocks + * @param xs log2 of the number of blocks per MB horizontally + * @param ys log2 of the number of blocks per MB vertically */ static void h_block_filter(ERContext *s, uint8_t *dst, int w, - int h, ptrdiff_t stride, int is_luma) + int h, ptrdiff_t stride, int xs, int ys) { int b_x, b_y; - ptrdiff_t mvx_stride, mvy_stride; - const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; - set_mv_strides(s, &mvx_stride, &mvy_stride); - mvx_stride >>= is_luma; - mvy_stride *= mvx_stride; + ptrdiff_t mv_step, mv_stride, mvx_stride, mvy_stride; + const int ps = s->pixel_shift, max = er_pixel_max(s); + set_mv_strides(s, &mv_step, &mv_stride); + mvx_stride = mv_step >> xs; + mvy_stride = mv_stride * (mv_step >> ys); for (b_y = 0; b_y < h; b_y++) { for (b_x = 0; b_x < w - 1; b_x++) { int y; - int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]; - int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]; - int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]); - int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]); + int left_status = s->error_status_table[( b_x >> xs) + (b_y >> ys) * s->mb_stride]; + int right_status = s->error_status_table[((b_x + 1) >> xs) + (b_y >> ys) * s->mb_stride]; + int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> xs) + (b_y >> ys) * s->mb_stride]); + int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> xs) + (b_y >> ys) * s->mb_stride]); int left_damage = left_status & ER_MB_ERROR; int right_damage = right_status & ER_MB_ERROR; - int offset = b_x * 8 + b_y * stride * 8; int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x]; int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)]; if (!(left_damage || right_damage)) @@ -288,11 +322,13 @@ static void h_block_filter(ERContext *s, uint8_t *dst, int w, continue; for (y = 0; y < 8; y++) { + uint8_t *row = dst + (b_y * 8 + y) * stride; + const int x0 = b_x * 8; int a, b, c, d; - a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride]; - b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride]; - c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride]; + a = get_px(row, x0 + 7, ps) - get_px(row, x0 + 6, ps); + b = get_px(row, x0 + 8, ps) - get_px(row, x0 + 7, ps); + c = get_px(row, x0 + 9, ps) - get_px(row, x0 + 8, ps); d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1); d = FFMAX(d, 0); @@ -306,16 +342,16 @@ static void h_block_filter(ERContext *s, uint8_t *dst, int w, d = d * 16 / 9; if (left_damage) { - dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)]; - dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)]; - dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)]; - dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)]; + set_px(row, x0 + 7, ps, av_clip(get_px(row, x0 + 7, ps) + ((d * 7) >> 4), 0, max)); + set_px(row, x0 + 6, ps, av_clip(get_px(row, x0 + 6, ps) + ((d * 5) >> 4), 0, max)); + set_px(row, x0 + 5, ps, av_clip(get_px(row, x0 + 5, ps) + ((d * 3) >> 4), 0, max)); + set_px(row, x0 + 4, ps, av_clip(get_px(row, x0 + 4, ps) + ((d * 1) >> 4), 0, max)); } if (right_damage) { - dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)]; - dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)]; - dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)]; - dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)]; + set_px(row, x0 + 8, ps, av_clip(get_px(row, x0 + 8, ps) - ((d * 7) >> 4), 0, max)); + set_px(row, x0 + 9, ps, av_clip(get_px(row, x0 + 9, ps) - ((d * 5) >> 4), 0, max)); + set_px(row, x0 + 10, ps, av_clip(get_px(row, x0 + 10, ps) - ((d * 3) >> 4), 0, max)); + set_px(row, x0 + 11, ps, av_clip(get_px(row, x0 + 11, ps) - ((d * 1) >> 4), 0, max)); } } } @@ -326,27 +362,30 @@ static void h_block_filter(ERContext *s, uint8_t *dst, int w, * simple vertical deblocking filter used for error resilience * @param w width in 8 pixel blocks * @param h height in 8 pixel blocks + * @param xs log2 of the number of blocks per MB horizontally + * @param ys log2 of the number of blocks per MB vertically */ static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h, - ptrdiff_t stride, int is_luma) + ptrdiff_t stride, int xs, int ys) { int b_x, b_y; - ptrdiff_t mvx_stride, mvy_stride; - const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; - set_mv_strides(s, &mvx_stride, &mvy_stride); - mvx_stride >>= is_luma; - mvy_stride *= mvx_stride; + ptrdiff_t mv_step, mv_stride, mvx_stride, mvy_stride; + const int ps = s->pixel_shift, max = er_pixel_max(s); + set_mv_strides(s, &mv_step, &mv_stride); + mvx_stride = mv_step >> xs; + mvy_stride = mv_stride * (mv_step >> ys); for (b_y = 0; b_y < h - 1; b_y++) { for (b_x = 0; b_x < w; b_x++) { int x; - int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]; - int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]; - int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]); - int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]); + int top_status = s->error_status_table[(b_x >> xs) + (b_y >> ys) * s->mb_stride]; + int bottom_status = s->error_status_table[(b_x >> xs) + ((b_y + 1) >> ys) * s->mb_stride]; + int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> xs) + ( b_y >> ys) * s->mb_stride]); + int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> xs) + ((b_y + 1) >> ys) * s->mb_stride]); int top_damage = top_status & ER_MB_ERROR; int bottom_damage = bottom_status & ER_MB_ERROR; - int offset = b_x * 8 + b_y * stride * 8; + uint8_t *row = dst + b_y * 8 * stride; + const int x0 = b_x * 8; int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x]; int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x]; @@ -359,12 +398,12 @@ static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h, FFABS(top_mv[1] + bottom_mv[1]) < 2) continue; - for (x = 0; x < 8; x++) { + for (x = x0; x < x0 + 8; x++) { int a, b, c, d; - a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride]; - b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride]; - c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride]; + a = get_px(row + 7 * stride, x, ps) - get_px(row + 6 * stride, x, ps); + b = get_px(row + 8 * stride, x, ps) - get_px(row + 7 * stride, x, ps); + c = get_px(row + 9 * stride, x, ps) - get_px(row + 8 * stride, x, ps); d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1); d = FFMAX(d, 0); @@ -378,16 +417,16 @@ static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h, d = d * 16 / 9; if (top_damage) { - dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)]; - dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)]; - dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)]; - dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)]; + set_px(row + 7 * stride, x, ps, av_clip(get_px(row + 7 * stride, x, ps) + ((d * 7) >> 4), 0, max)); + set_px(row + 6 * stride, x, ps, av_clip(get_px(row + 6 * stride, x, ps) + ((d * 5) >> 4), 0, max)); + set_px(row + 5 * stride, x, ps, av_clip(get_px(row + 5 * stride, x, ps) + ((d * 3) >> 4), 0, max)); + set_px(row + 4 * stride, x, ps, av_clip(get_px(row + 4 * stride, x, ps) + ((d * 1) >> 4), 0, max)); } if (bottom_damage) { - dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)]; - dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)]; - dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)]; - dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)]; + set_px(row + 8 * stride, x, ps, av_clip(get_px(row + 8 * stride, x, ps) - ((d * 7) >> 4), 0, max)); + set_px(row + 9 * stride, x, ps, av_clip(get_px(row + 9 * stride, x, ps) - ((d * 5) >> 4), 0, max)); + set_px(row + 10 * stride, x, ps, av_clip(get_px(row + 10 * stride, x, ps) - ((d * 3) >> 4), 0, max)); + set_px(row + 11 * stride, x, ps, av_clip(get_px(row + 11 * stride, x, ps) - ((d * 1) >> 4), 0, max)); } } } @@ -647,8 +686,9 @@ skip_mean_and_median: for (j = 0; j < pred_count; j++) { int *linesize = s->cur_pic.f->linesize; int score = 0; + const int ps = s->pixel_shift; uint8_t *src = s->cur_pic.f->data[0] + - mb_x * 16 + mb_y * 16 * linesize[0]; + (mb_x * 16 << ps) + mb_y * 16 * linesize[0]; s->cur_pic.motion_val[0][mot_index][0] = s->mv[0][0][0] = mv_predictor[j][0]; @@ -665,25 +705,26 @@ skip_mean_and_median: if (mb_x > 0 && fixed[mb_xy - 1] > 1) { int k; for (k = 0; k < 16; k++) - score += FFABS(src[k * linesize[0] - 1] - - src[k * linesize[0]]); + score += FFABS(get_px(src + k * linesize[0], -1, ps) - + get_px(src + k * linesize[0], 0, ps)); } if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) { int k; for (k = 0; k < 16; k++) - score += FFABS(src[k * linesize[0] + 15] - - src[k * linesize[0] + 16]); + score += FFABS(get_px(src + k * linesize[0], 15, ps) - + get_px(src + k * linesize[0], 16, ps)); } if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) { int k; for (k = 0; k < 16; k++) - score += FFABS(src[k - linesize[0]] - src[k]); + score += FFABS(get_px(src - linesize[0], k, ps) - + get_px(src, k, ps)); } if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] > 1) { int k; for (k = 0; k < 16; k++) - score += FFABS(src[k + linesize[0] * 15] - - src[k + linesize[0] * 16]); + score += FFABS(get_px(src + linesize[0] * 15, k, ps) - + get_px(src + linesize[0] * 16, k, ps)); } if (score <= best_score) { // <= will favor the last MV @@ -740,6 +781,22 @@ skip_mean_and_median: } } +/** + * 16x16 SAD of 8-bit or 16-bit samples. + */ +static int er_sad16(const ERContext *s, const uint8_t *a, const uint8_t *b, + ptrdiff_t stride) +{ + int x, y, sum = 0; + + if (!s->pixel_shift) + return s->sad(NULL, a, b, stride, 16); + for (y = 0; y < 16; y++, a += stride, b += stride) + for (x = 0; x < 16; x++) + sum += FFABS(AV_RN16A(a + 2 * x) - AV_RN16A(b + 2 * x)); + return sum; +} + static int is_intra_more_likely(ERContext *s) { int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y; @@ -782,21 +839,21 @@ static int is_intra_more_likely(ERContext *s) if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) { int *linesize = s->cur_pic.f->linesize; uint8_t *mb_ptr = s->cur_pic.f->data[0] + - mb_x * 16 + mb_y * 16 * linesize[0]; + (mb_x * 16 << s->pixel_shift) + mb_y * 16 * linesize[0]; uint8_t *last_mb_ptr = s->last_pic.f->data[0] + - mb_x * 16 + mb_y * 16 * linesize[0]; + (mb_x * 16 << s->pixel_shift) + mb_y * 16 * linesize[0]; if (s->avctx->codec_id == AV_CODEC_ID_H264) { // FIXME } else { ff_thread_progress_await(s->last_pic.progress, mb_y); } - is_intra_likely += s->sad(NULL, last_mb_ptr, mb_ptr, - linesize[0], 16); + is_intra_likely += er_sad16(s, last_mb_ptr, mb_ptr, + linesize[0]); // FIXME need await_progress() here - is_intra_likely -= s->sad(NULL, last_mb_ptr, - last_mb_ptr + linesize[0] * 16, - linesize[0], 16); + is_intra_likely -= er_sad16(s, last_mb_ptr, + last_mb_ptr + linesize[0] * 16, + linesize[0]); } else { if (IS_INTRA(s->cur_pic.mb_type[mb_xy])) is_intra_likely++; @@ -917,6 +974,7 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) int is_intra_likely; int size = s->b8_stride * 2 * s->mb_height; int guessed_mb_type; + int ps, cw, ch, c_log2, dc_sh; /* We do not support ER of field pictures yet, * though it should not crash if enabled. */ @@ -927,6 +985,25 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) (s->avctx->skip_top + s->avctx->skip_bottom)) { return; } + + s->bit_depth = 8; + s->pixel_shift = 0; + s->chroma_x_shift = + s->chroma_y_shift = 1; + if (s->avctx->codec_id == AV_CODEC_ID_H264) { + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->cur_pic.f->format); + s->bit_depth = desc->comp[0].depth; + s->pixel_shift = desc->comp[0].depth > 8; + s->chroma_x_shift = desc->log2_chroma_w; + s->chroma_y_shift = desc->log2_chroma_h; + } + ps = s->pixel_shift; + cw = 16 >> s->chroma_x_shift; + ch = 16 >> s->chroma_y_shift; + /* log2 of the number of chroma samples per MB */ + c_log2 = 8 - s->chroma_x_shift - s->chroma_y_shift; + dc_sh = er_dc_shift(s); + linesize = s->cur_pic.f->linesize; if ( s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO @@ -1253,9 +1330,9 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) // if (error & ER_MV_ERROR) // continue; // inter data damaged FIXME is this good? - dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0]; - dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1]; - dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2]; + dest_y = s->cur_pic.f->data[0] + (mb_x * 16 << ps) + mb_y * 16 * linesize[0]; + dest_cb = s->cur_pic.f->data[1] + (mb_x * cw << ps) + mb_y * ch * linesize[1]; + dest_cr = s->cur_pic.f->data[2] + (mb_x * cw << ps) + mb_y * ch * linesize[2]; dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride]; for (n = 0; n < 4; n++) { @@ -1263,25 +1340,26 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) for (y = 0; y < 8; y++) { int x; for (x = 0; x < 8; x++) - dc += dest_y[x + (n & 1) * 8 + - (y + (n >> 1) * 8) * linesize[0]]; + dc += get_px(dest_y + (y + (n >> 1) * 8) * linesize[0], + x + (n & 1) * 8, ps); } - dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3; + dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + (4 << dc_sh)) >> (3 + dc_sh); } if (!s->cur_pic.f->data[2]) continue; dcu = dcv = 0; - for (y = 0; y < 8; y++) { + for (y = 0; y < ch; y++) { int x; - for (x = 0; x < 8; x++) { - dcu += dest_cb[x + y * linesize[1]]; - dcv += dest_cr[x + y * linesize[2]]; + for (x = 0; x < cw; x++) { + dcu += get_px(dest_cb + y * linesize[1], x, ps); + dcv += get_px(dest_cr + y * linesize[2], x, ps); } } - s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3; - s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3; + /* 8 * mean of the cw x ch block */ + s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + (1 << (c_log2 - 4 + dc_sh))) >> (c_log2 - 3 + dc_sh); + s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + (1 << (c_log2 - 4 + dc_sh))) >> (c_log2 - 3 + dc_sh); } } #if 1 @@ -1309,9 +1387,9 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) if (!(error & ER_AC_ERROR)) continue; // undamaged - dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0]; - dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1]; - dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2]; + dest_y = s->cur_pic.f->data[0] + (mb_x * 16 << ps) + mb_y * 16 * linesize[0]; + dest_cb = s->cur_pic.f->data[1] + (mb_x * cw << ps) + mb_y * ch * linesize[1]; + dest_cr = s->cur_pic.f->data[2] + (mb_x * cw << ps) + mb_y * ch * linesize[2]; if (!s->cur_pic.f->data[2]) dest_cb = dest_cr = NULL; @@ -1323,21 +1401,23 @@ void ff_er_frame_end(ERContext *s, int *decode_error_flags) if (s->avctx->error_concealment & FF_EC_DEBLOCK) { /* filter horizontal block boundaries */ h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2, - s->mb_height * 2, linesize[0], 1); + s->mb_height * 2, linesize[0], 1, 1); /* filter vertical block boundaries */ v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2, - s->mb_height * 2, linesize[0], 1); + s->mb_height * 2, linesize[0], 1, 1); if (s->cur_pic.f->data[2]) { - h_block_filter(s, s->cur_pic.f->data[1], s->mb_width, - s->mb_height, linesize[1], 0); - h_block_filter(s, s->cur_pic.f->data[2], s->mb_width, - s->mb_height, linesize[2], 0); - v_block_filter(s, s->cur_pic.f->data[1], s->mb_width, - s->mb_height, linesize[1], 0); - v_block_filter(s, s->cur_pic.f->data[2], s->mb_width, - s->mb_height, linesize[2], 0); + /* 8x8 chroma blocks per MB: 1 << cbx horizontally, 1 << cby vertically */ + const int cbx = 1 - s->chroma_x_shift, cby = 1 - s->chroma_y_shift; + h_block_filter(s, s->cur_pic.f->data[1], s->mb_width << cbx, + s->mb_height << cby, linesize[1], cbx, cby); + h_block_filter(s, s->cur_pic.f->data[2], s->mb_width << cbx, + s->mb_height << cby, linesize[2], cbx, cby); + v_block_filter(s, s->cur_pic.f->data[1], s->mb_width << cbx, + s->mb_height << cby, linesize[1], cbx, cby); + v_block_filter(s, s->cur_pic.f->data[2], s->mb_width << cbx, + s->mb_height << cby, linesize[2], cbx, cby); } } diff --git a/libavcodec/error_resilience.h b/libavcodec/error_resilience.h index 1beae5a6b05..eba3494c269 100644 --- a/libavcodec/error_resilience.h +++ b/libavcodec/error_resilience.h @@ -60,6 +60,11 @@ typedef struct ERContext { int *mb_index2xy; int mb_num; int mb_width, mb_height; + /* sample format of cur_pic, set by ff_er_frame_end(); 8-bit 4:2:0 + * except for H.264 */ + uint8_t bit_depth; + uint8_t pixel_shift; ///< 1 for 16-bit samples + uint8_t chroma_x_shift, chroma_y_shift; ptrdiff_t mb_stride; ptrdiff_t b8_stride; -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
