如何在不牺牲其功能的情况下将该功能拆分为较小的功能? [英] How can I split this function into smaller functions without sacrificing its functionality?
问题描述
我从此处获得了用于MJPEG解码的代码,我我正在尝试将IDCT的代码拆分为较小的功能.
I got the code for MJPEG decoding from here and I am trying to split the code for IDCT into smaller functions.
原始代码中的IDCT函数如下:
The IDCT function in the original code is as follows:
void IDCT(int32_t *input, uint8_t *output) {
int32_t Y[64];
int32_t k, l;
for (k = 0; k < 8; k++) {
for (l = 0; l < 8; l++) Y(k, l) = SCALE(input[(k << 3) + l], S_BITS);
idct_1d(&Y(k, 0));
}
for (l = 0; l < 8; l++) {
int32_t Yc[8];
for (k = 0; k < 8; k++) Yc[k] = Y(k, l);
idct_1d(Yc);
for (k = 0; k < 8; k++) {
int32_t r = 128 + DESCALE(Yc[k], S_BITS + 3);
r = r > 0 ? (r < 255 ? r : 255) : 0;
X(k, l) = r;
}
}
}
有关功能的更多详细信息,请参见此链接.
More details of the functions can be found in this link.
我可以通过以下方式进一步分解此代码:
I was able to further break this code down in the following way:
在X方向上:
void IDCTforX(int32_t *input, uint8_t *output) {
int32_t Y[64];
int32_t k, l;
int32_t Yc[8];
for (k = 0; k < 8; k++) {
for (l = 0; l < 8; l++)
{
Y(k, l) = SCALE(input[(k << 3) + l], S_BITS);
}
}
}
void IDCTfor1dim(int32_t *input, uint8_t *output)
{
int32_t Y[64];
int32_t k, l;
int32_t Yc[8];
for (k= 0; k < 8; k++)
{
idct_1d(&Y(k, 0));
}
}
在Y方向上:
void IDCTforY(int32_t *input, uint8_t *output) {
int32_t Y[64];
int32_t k, l;
for (l = 0; l < 8; l++) {
int32_t Yc[8];
for (k = 0; k < 8; k++)
{
Yc[k] = Y(k, l);
}
idct_1d(Yc);
for (k = 0; k < 8; k++) {
int32_t r = 128 + DESCALE(Yc[k], S_BITS + 3);
r = r > 0 ? (r < 255 ? r : 255) : 0;
X(k, l) = r;
}
}
DESCALE
的代码如下:
static inline int32_t DESCALE (int32_t x, int32_t n)
{
return (x + (1 << (n - 1)) - (x < 0)) >> n;
}
以上述方式重新组织IDCT,可以得到与原始代码相同的输出.但是,按照以下方式重新组织IDCTforY
的代码后,我得到了模糊的图像:
Re-organizing IDCT in the manner shown above is giving me the same output as the original code. However, after re-organizing the code for IDCTforY
in the following way I got a blurred image:
void IDCTforY(int32_t *input, uint8_t *output) {
int32_t Y[64];
int32_t k, l;
int32_t Yc[8];
for (l = 0; l < 8; l++) {
for (k = 0; k < 8; k++)
{
Yc[k] = Y(k, l);
}
idct_1d(Yc);
}
//Running the loop for de-scaling separately....
for (l = 0; l < 8; l++) {
for (k = 0; k < 8; k++) {
int32_t r = 128 + DESCALE(Yc[k], S_BITS + 3);
r = r > 0 ? (r < 255 ? r : 255) : 0;
X(k, l) = r;
}
}
}
我的输出帧看起来像上面的代码:
My output frames looks like this with the above code:
在JPEG解码中模糊图像的含义是什么?
如何以不破坏代码性质的方式拆分IDCTforY
?
How can I split IDCTforY
in such a way that the nature of my code doesn't get compromised?
推荐答案
函数IDCT()声明数组Y [],该数组在所有for循环中传输数据.在重构的代码中,每个函数都声明自己的Y []数组.与Yc []数组相同的错误.使数组成为全局数组,然后查看代码是否运行.
The function IDCT() declares the array Y[] which transfers data across all for-loops. In your refactored code every function declares its own Y[] array. The same error you did with the Yc[] array. Make the arrays global and see if the code runs.
编辑2017_08-28
给Yc []一个额外的维度:
Give Yc[] an extra dimension:
void IDCTforY(int32_t *input, uint8_t *output)
{
int32_t Y[64];
int32_t k, l;
int32_t Yc[8][8];
for (l = 0; l < 8; l++)
{
for (k = 0; k < 8; k++)
Yc[l][k] = Y(k, l);
idct_1d(Yc[l]);
}
//Running the loop for de-scaling separately....
for (l = 0; l < 8; l++)
{
for (k = 0; k < 8; k++)
{
int32_t r = 128 + DESCALE(Yc[l][k], S_BITS + 3);
r = r > 0 ? (r < 255 ? r : 255) : 0;
X(k, l) = r;
}
}
}
编辑2017-08-29
我无法解释光学效果,但您破坏了数据流.原始代码是这样的:
I cannot explain the optical effect, but you broke the data flow. The original code was like this:
for (l = 0; l < 8; l++)
{
int32_t Yc[8];
Fill(Yc);
idct_1d(Yc);
Descale_and_WriteOut(Yc);
}
您是由它制成的:
int32_t Yc[8];
for (l = 0; l < 8; l++)
{
Fill(Yc);
idct_1d(Yc);
}
for (l = 0; l < 8; l++)
{
Descale_and_WriteOut(Yc);
}
您会看到,只有输入和处理循环的最后迭代的结果才传递到输出循环.我在Yc [] []中给每个l-iteration自己的记忆.
You see, that only the result of the last iteration of the input-and-process-loop is passed to the output-loop. I gave every l-iteration own memory in Yc[][].
这篇关于如何在不牺牲其功能的情况下将该功能拆分为较小的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!