|
-
- double hist_diff(short * cur_buffer, short * org_buffer, int size)
- {
- double sum = 0.0;
- int cur_result[256] = { 0 };
- int org_result[256] = { 0 };
- array_to_hist(cur_buffer, size, cur_result);
- array_to_hist(org_buffer, size, org_result);
- for (int i = 0; i < 256; i++)
- {
- int Abs = abs(cur_result[i] - org_result[i]);
- int max = (cur_result[i] > org_result[i]) ? cur_result[i] : org_result[i];
- if (cur_result[i] == org_result[i])
- {
- sum += 1 - 0;
- }
- else
- {
- sum += 1 - (double)Abs / (double)max;
- }
-
- //sum = sum + r;
- }
- double result = sum / 256;
- return 1 - result;
-
- /*printf("=======================");
- for (int i = 0; i < 256; i++)
- {
- printf("%d\t", org_result[i]);
- }*/
-
- }
-
- void cal_ssim_yh(int factor, double rad_struct[], int width_s1, int height_s1, short* org_curr_scale, short* rec_curr_scale, int bit_depth)
- {
- int width, height, stride;
- const double K1 = 0.01;
- const double K2 = 0.03;
- //const int WIN_SIZE = 7;
- const int peak = (1 << bit_depth) - 1; //255 for 8-bit, 1023 for 10-bit
- const double C1 = K1 * K1 * peak * peak;
- const double C2 = K2 * K2 * peak * peak;
- double tmp_luma, tmp_simi, loc_mean_ref, loc_mean_rec, loc_var_ref,
- loc_var_rec, loc_covar, num1, num2, den1, den2;
- int num_win;
- int win_width, win_height;
- short *org, *rec, *org_pel, *rec_pel;
- int i, j, x, y;
- width = stride = width_s1 / factor;
- height = height_s1 / factor;
- win_width = WIN_SIZE < width ? WIN_SIZE : width;
- win_height = WIN_SIZE < height ? WIN_SIZE : height;
- org = org_curr_scale;
- rec = rec_curr_scale;
- org_pel = org;
- rec_pel = rec;
- num_win = (height - win_height + 1)*(width - win_width + 1);
- for (j = 0; j <= height - win_height; j++)
- {
- for (i = 0; i <= width - win_width; i++)
- {
- loc_mean_ref = 0;
- loc_mean_rec = 0;
- loc_var_ref = 0;
- loc_var_rec = 0;
- loc_covar = 0;
- org_pel = org + i + width*j;
- rec_pel = rec + i + width*j;
- for (y = 0; y < win_height; y++)
- {
- for (x = 0; x < win_width; x++)
- {
- loc_mean_ref += org_pel[x] * gaussian_filter[y][x];
- loc_mean_rec += rec_pel[x] * gaussian_filter[y][x];
- loc_var_ref += org_pel[x] * gaussian_filter[y][x] * org_pel[x];
- loc_var_rec += rec_pel[x] * gaussian_filter[y][x] * rec_pel[x];
- loc_covar += org_pel[x] * gaussian_filter[y][x] * rec_pel[x];
- }
- org_pel += width;
- rec_pel += width;
- }
- loc_var_ref = (loc_var_ref - loc_mean_ref * loc_mean_ref);
- loc_var_rec = (loc_var_rec - loc_mean_rec * loc_mean_rec);
- loc_covar = (loc_covar - loc_mean_ref * loc_mean_rec);
- num1 = 2.0 * loc_mean_ref * loc_mean_rec + C1;
- num2 = 2.0 * loc_covar + C2;
- den1 = loc_mean_ref * loc_mean_ref + loc_mean_rec * loc_mean_rec + C1;
- den2 = loc_var_ref + loc_var_rec + C2;
- tmp_luma = num1 / den1;
- tmp_simi = num2 / den2;
- rad_struct[0] += tmp_luma;
- rad_struct[1] += tmp_simi;
- rad_struct[2] += tmp_luma * tmp_simi;
- }
- }
- rad_struct[0] /= (double)num_win;
- rad_struct[1] /= (double)num_win;
- rad_struct[2] /= (double)num_win;
- }
-
- double ssim_diff(short * cur_buffer, short * org_buffer, int w, int h)
- {
- int factor = 1;
- double struct_y[3] = { 0.0, 0.0, 0.0 };
- cal_ssim(factor, struct_y, w, h, cur_buffer, org_buffer, 8);
- double ms_ssim = struct_y[1];
- return 1-ms_ssim;
- }
-
- int compute_difference_in_frame_level(short * org_buffer, short * cur_buffer, int threshold, int * flag, int width, int height)
- {
- int w_count = width / 128 + 1;
- int h_count = height / 128 + 1;
- int delta_w = 0;
- int delta_h = 0;
- int size = width * height;
- double th = (double)threshold / 100;
-
- if (width % 128 == 0)
- {
- w_count -= 1;
- delta_w = 0;
- }
- else
- {
- delta_w = width - (w_count - 1) * 128;
- }
- if (height % 128 == 0)
- {
- h_count -= 1;
- delta_h = 0;
- }
- else
- {
- delta_h = height - (h_count - 1) * 128;
- }
-
- double diff = hist_diff(org_buffer, cur_buffer, size);
- double ssim = ssim_diff(cur_buffer, org_buffer, width, height);
- printf("hist diff:%f ssim diff:%f\n", diff,ssim);
- for (int row = 0; row < h_count; row++)
- {
- for (int line = 0; line < w_count; line++)
- {
- int idx = row * w_count + line;
- flag[idx] = (diff < th) ? 1 : 0;
- }
- }
-
- return 1;
- }
-
- int compute_difference_in_block_level(short * org_buffer, short * cur_buffer,int threshold,int * flag,int * BT_flag, int width, int height)
- {
-
- int w_count = width / 128 + 1;
- int h_count = height / 128 + 1;
- int delta_w = 0;
- int delta_h = 0;
- //double th = (double)threshold / 100;
-
- /*预测目标为0的阈值
- double hist_th = 0.028620604572508382;
- double ssim_th = 0.0014407192763906192;*/
-
- //best value
- /*double hist_th = 0.05535194513150425;
- double ssim_th = 0.03558558558558559;*/
-
- //QP37 cnt5
- /*double hist_th = 0.15000000000000002;
- double ssim_th = 0.14979959919839678;*/
-
- double EQT_hist_th = 0.05535194513150425;
- double EQT_ssim_th = 0.03558558558558559;
- double BT_hist_th = 0.03064908595970721;
- double BT_ssim_th = 0.007569493341036428;
-
- //random 1
- /*double hist_th = 0.055446227790917166;
- double ssim_th = 0.035785785785785784;*/
-
- //random 2
- /*double hist_th = 0.05709466982012073;
- double ssim_th = 0.03568568568568569;*/
-
- //QP32
- //double hist_th = 0.14979959919839678;
- //double ssim_th = 0.15000000000000002;
-
-
- if (width % 128 == 0)
- {
- w_count -= 1;
- delta_w = 0;
- }
- else
- {
- delta_w = width - (w_count - 1) * 128;
- }
- if (height % 128 == 0)
- {
- h_count -= 1;
- delta_h = 0;
- }
- else
- {
- delta_h = height - (h_count - 1) * 128;
- }
- for (int row = 0; row < h_count; row++)
- {
- for (int line = 0; line < w_count; line++)
- {
- int interval_w = ((delta_w != 0) && (line + 1 == w_count)) ? delta_w : 128;
- int interval_h = ((delta_h != 0) && (row + 1 == h_count)) ? delta_h : 128;
- int size = interval_w * interval_h;
- int block_byte = interval_w * interval_h;
- short *cur_block = (short *)malloc(sizeof(short) * block_byte);
- short *org_block = (short *)malloc(sizeof(short) * block_byte);
- int x_index = line * 128;
- int y_index = row * 128;
- for (int j = 0; j < interval_h; j++)
- {
- for (int i = 0; i < interval_w; i++)
- {
- int block_idx = j * interval_w + i;
- int pic_idx = (y_index + j) * width + (x_index + i);
- cur_block[block_idx] = cur_buffer[pic_idx];
- org_block[block_idx] = org_buffer[pic_idx];
- }
- }
- // f = 1,skip the EHP!!
- int EQT_f = 0;
- int BT_f = 0;
- int idx = row * w_count + line;
- double hist = hist_diff(cur_block, org_block, size);
- double ssim = ssim_diff(cur_block, org_block, interval_w, interval_h);
- //*****************EQT Skip checking****************//
- if (hist > EQT_hist_th)
- {
- if (ssim > EQT_ssim_th)
- {
- EQT_f = 0;
- }
- else
- {
- EQT_f = 1;
- }
- }
- else
- {
- EQT_f = 1;
- }
- //*****************BT Skip checking****************//
- if (hist > BT_hist_th)
- {
- if (ssim > BT_ssim_th)
- {
- BT_f = 0;
- }
- else
- {
- BT_f = 1;
- }
- }
- else
- {
- BT_f = 1;
- }
- flag[idx] = EQT_f;
- BT_flag[idx] = BT_f;
- //printf("block Hist: %f flag_state:%d\n", diff, flag[idx]);
- free(cur_block);
- free(org_block);
- }
- }
- return 1;
- }
-
- int copy_p(ENC_CTX * ctx, int * miss_array, int * BT_miss_array)
- {
- ctx->info.miss_array = miss_array;
- ctx->info.BT_miss_array = BT_miss_array;
- ctx->info.m_flag = 1;
- return 1;
- }
-
- int init_m_flag(ENC_CTX * ctx)
- {
- ctx->info.m_flag = 0;
- return 1;
- }
-
-
- int main(int argc, const char **argv)
- {
-
- //yh add
- int miss_begin = 0;
- int org_pic_size = op_w * op_h * 2;
- int test_frame_size = op_w * op_h;
- int block_size = (op_w / 128 + 1)*(op_h / 128 + 1);
- short * org_pic_data = (short *)malloc(sizeof(short)*org_pic_size);
- int * miss_flag_yh = (int *)malloc(sizeof(int) * block_size);
- int * BT_miss_flag_yh = (int *)malloc(sizeof(int) * block_size);
- //yh end
-
- // the above code add before the following annotation
- /* encode pictures *******************************************************/
-
- while(1)
- {
- /* compute image difference with last frame in block level */
-
- if (miss_begin == 1)
- {
- //compute_difference_in_frame_level(org_pic_data, (short*)ilist_t->imgb->addr_plane[0], op_miss, miss_flag_yh, op_w, op_h);
- compute_difference_in_block_level(org_pic_data, (short*)ilist_t->imgb->addr_plane[0], op_miss, miss_flag_yh,BT_miss_flag_yh, op_w, op_h);
- copy_p((ENC_CTX *)id, miss_flag_yh,BT_miss_flag_yh);
- copy_data_for_computing_diff(org_pic_data, (short*)ilist_t->imgb->addr_plane[0], op_w, op_h);
- }
- else
- {
- miss_begin = 1;
- init_m_flag((ENC_CTX *)id);
- copy_data_for_computing_diff(org_pic_data, (short*)ilist_t->imgb->addr_plane[0], op_w, op_h);
- }
- }
-
- }
|