In lab four, we implemented different real-time video processing tasks written
both entirely in C code and in in-line assembly. We also generated timing results
for both types of code and to see the differences in speed resulting from using the MMX
instructions and in-line assembly. We implemented the algorithms by adding C functions
to the code provided to us by Dr Maxxwell and calling them in xmain.c or adding
assembly code using the MMX instructions and registers in image processing loops.
Our group chose to implement the Background Subtraction and Contrast Enhancement algorithms in straight C code and in-line assembly and the Overlay algorithm in in-line assembly.
For background subtraction, we used keyboard input so that when the 'b' key was hit, the current image was saved as the background. Following that, any image that is processed is subtracted from the background image. The assembly version of this algorithm actually uses MMX Difference which utilizes saturated arithmetic to obtain the absolute value of the difference between the current image and the background. In the C version, we simply used the abs() function. After the difference is calculated, the assembly version also subtracts a threshold from the result.
For our C implementation, we created two separate functions for brightness and
contrast adjustment. When the '+' key is hit, the internal brightness value is
is increased by 3. Similarly, the value is decreased by 3 when '-' is entered. Next,
the brightness value is simply added to every pixel of the current image. Since
the brightness value ranges from -255 to 255, the brightness can be decreased
For our C contrast adjustment, the keys 'c' and 'v' indicated when
the contrast was supposed to be increased or decreased. The contrast value ranges
from -128 to 128. When the contrast variable is negative, the value of each pixel
of the current frame is adjusted so that it is brought closer to the center, or is
made grayer. When the contrast is positive, each pixel is adjusted so that the lighter
colors become whiter and the darker colors become more black.
In assembly, the value of our brightness variable also ranged from -255 to 255. In the image processing loop, we loaded the absolute value of the brightness into an MMX register and then added or subtracted that value using unsigned saturated arithmetic depending on whether the original brightness was positive or negative.
Our overlay algorithm attempted to mix each new image with a previous image.
To do this we created a function called MMX_AVERAGE. This function takes the
average of two images by adding them with unsigned saturated arithmetic and then
dividing by two (shifting right). It then ANDs that value with the mask 7F and
adds the threshold to obtain the final image.
We chose to call MMX_AVERAGE every fourth image by using a counter in xmain.c
and incrementing it during each loop.
We expected that the algorithms implemented using MMX assembly code would complete
much faster than those implemented entirely in C. Theoretically, since MMX instructions
can operate simultaneously on 8 bytes, if everything was done perfectly, the program
would have run eight times faster. Even with dependencies in our code which would have stalled
the pipeline, we still expected some difference in time between the programs which
used assembly code and the programs which didn't. Tragically, this was not reflected in
our tests of each algorithm. To time our code we used the gettimeofday function and
code that was provided in Lab 2 and ran each program through 500 loops. The results
are given in the table below:
Algorithm | MMX TIME | non-MMX TIME |
---|---|---|
Background Subtraction | 33.597s < 67193543.9.2 ns/loop | 33.597s < 67193424.2 ns/loop |
Contrast Adjustment | 66.519s < 133038251.9 ns/loop | 33.586s < 67171289.9 ns/loop |
Brightness Adjustment | 33.599s < 67197915.6 ns/loop | 33.598s < 67195976.3 ns/loop |
Overlay | 33.596s < 67191006.2 ns/loop | --- |
Algorithm | Increase | Decrease |
---|---|---|
Background Subtraction | b | |
Contrast Adjustment | c | v |
Brightness Adjustment | + | - |
Overlay | o |
Our Images are here.
Submitted as unlinked on the lab submission page.
We had tons of fun!! WE LOVE MMX!!!!