2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
11 /****************************************************************************
13 * Module Title : preproc.c
15 * Description : Simple pre-processor.
17 ****************************************************************************/
19 /****************************************************************************
21 ****************************************************************************/
25 #include "vpx_mem/vpx_mem.h"
27 /****************************************************************************
29 ****************************************************************************/
31 #define ROUNDUP32(X) ( ( ( (unsigned long) X ) + 31 )&( 0xFFFFFFE0 ) )
33 /****************************************************************************
35 ****************************************************************************/
36 extern void vp8_get_processor_flags(int *mmx_enabled, int *xmm_enabled, int *wmt_enabled);
38 /****************************************************************************
39 * Exported Global Variables
40 ****************************************************************************/
41 void (*temp_filter)(pre_proc_instance *ppi, unsigned char *s, unsigned char *d, int bytes, int strength);
44 pre_proc_instance *ppi,
52 pre_proc_instance *ppi,
59 /****************************************************************************
61 * ROUTINE : temp_filter_c
63 * INPUTS : pre_proc_instance *ppi : Pointer to pre-processor instance.
64 * unsigned char *s : Pointer to source frame.
65 * unsigned char *d : Pointer to destination frame.
66 * int bytes : Number of bytes to filter.
67 * int strength : Strength of filter to apply.
73 * FUNCTION : Performs a closesness adjusted temporarl blur
75 * SPECIAL NOTES : Destination frame can be same as source frame.
77 ****************************************************************************/
80 pre_proc_instance *ppi,
88 unsigned char *frameptr = ppi->frame_buffer;
102 while (frame < FRAMECOUNT);
108 while (byte < bytes);
113 int offset = (ppi->frame % FRAMECOUNT);
121 frameptr[offset] = s[byte];
125 int pixel_value = *frameptr;
128 modifier -= pixel_value;
129 modifier *= modifier;
130 modifier >>= strength;
136 modifier = 16 - modifier;
138 accumulator += modifier * pixel_value;
146 while (frame < FRAMECOUNT);
148 accumulator += (count >> 1);
149 accumulator *= ppi->fixed_divide[count];
152 d[byte] = accumulator;
156 while (byte < bytes);
161 /****************************************************************************
163 * ROUTINE : delete_pre_proc
165 * INPUTS : pre_proc_instance *ppi : Pointer to pre-processor instance.
171 * FUNCTION : Deletes a pre-processing instance.
173 * SPECIAL NOTES : None.
175 ****************************************************************************/
176 void delete_pre_proc(pre_proc_instance *ppi)
178 if (ppi->frame_buffer_alloc)
179 vpx_free(ppi->frame_buffer_alloc);
181 ppi->frame_buffer_alloc = 0;
182 ppi->frame_buffer = 0;
184 if (ppi->fixed_divide_alloc)
185 vpx_free(ppi->fixed_divide_alloc);
187 ppi->fixed_divide_alloc = 0;
188 ppi->fixed_divide = 0;
191 /****************************************************************************
193 * ROUTINE : init_pre_proc
195 * INPUTS : pre_proc_instance *ppi : Pointer to pre-processor instance.
196 * int frame_size : Number of bytes in one frame.
200 * RETURNS : int: 1 if successful, 0 if failed.
202 * FUNCTION : Initializes prepprocessor instance.
204 * SPECIAL NOTES : None.
206 ****************************************************************************/
207 int init_pre_proc7(pre_proc_instance *ppi, int frame_size)
214 vp8_get_processor_flags(&mmx_enabled, &xmm_enabled, &wmt_enabled);
217 temp_filter = temp_filter_wmt;
218 else if (mmx_enabled)
219 temp_filter = temp_filter_mmx;
221 temp_filter = temp_filter_c;
224 delete_pre_proc(ppi);
226 ppi->frame_buffer_alloc = vpx_malloc(32 + frame_size * FRAMECOUNT * sizeof(unsigned char));
228 if (!ppi->frame_buffer_alloc)
230 delete_pre_proc(ppi);
234 ppi->frame_buffer = (unsigned char *) ROUNDUP32(ppi->frame_buffer_alloc);
236 ppi->fixed_divide_alloc = vpx_malloc(32 + 255 * sizeof(unsigned int));
238 if (!ppi->fixed_divide_alloc)
240 delete_pre_proc(ppi);
244 ppi->fixed_divide = (unsigned int *) ROUNDUP32(ppi->fixed_divide_alloc);
246 for (i = 1; i < 255; i++)
247 ppi->fixed_divide[i] = 0x10000 / i;