JM8.6解码端是如何把像素值写进test_dec.yuv文件的?(write_out_picture函数)

      写文件的过程必然涉及到打开文件,所以在代码中找fopen函数,而解码器中的fopen不是很多(如果fopen太多,也可以从fwrite, fputc, putc的角度来找),所以可以很快找到我们感兴趣的代码:

if ((p_out=fopen(inp->outfile,"wb"))==0)
{
  snprintf(errortext, ET_SIZE, "Error open file %s ",inp->outfile);
  error(errortext,500);
}

      由此可知:p_out指向了最后保存yuv的文件(test_dec.yuv),而p_out是一个全局的指针变量,所以要对test_dec.yuv进行写文件操作,必然涉及到对p_out进行操作. 在代码中搜索一下p_out, 很快就会找到我们感兴趣的p_out, 事实上,写二进制文件的函数通常就是fwrite, fputc, putc等函数,所以可以定位到write_out_picture函数中的三个语句:

fputc(p->imgY[i][j],p_out);
fputc(p->imgUV[0][i][j],p_out);
fputc(p->imgUV[1][i][j],p_out);


     像素值就是这样写进test_dec.yuv文件的. 像素值仅仅在这三处写入么?是的,下面来加以验证:

     在output.c中加入如下代码:

extern FILE *myFp;
// 在main函数中提前把myYuvData.txt文件打开
// myFp指向了存储yuv的文本文件:myYuvData.txt
void printDataIntoFile(FILE *fp, int n)
{
	assert(n >= 0 && n <= 255);
	fprintf(fp, "%5d", n);	
}


    另外,在write_out_picture函数中添加对printDataIntoFile函数的调用,使之成为:(本人编码了3帧,发现3次调用write_out_picture函数)

void write_out_picture(StorablePicture *p, FILE *p_out)
{
  int i,j;

  int crop_left, crop_right, crop_top, crop_bottom;
  int crop_vert_mult;
  
  if (p->non_existing)
    return;

  if (p->frame_mbs_only_flag)
  {
    crop_vert_mult = 2;
  }
  else
  {
    if (p->structure != FRAME)
    {
      crop_vert_mult = 2;
    }
    else
    {
      crop_vert_mult = 4;
    }
  }

  if (p->frame_cropping_flag)
  {
    crop_left   = 2 * p->frame_cropping_rect_left_offset;
    crop_right  = 2 * p->frame_cropping_rect_right_offset;
    crop_top    = crop_vert_mult * p->frame_cropping_rect_top_offset;
    crop_bottom = crop_vert_mult * p->frame_cropping_rect_bottom_offset;
  }
  else
  {
    crop_left = crop_right = crop_top = crop_bottom = 0;
  }

  //printf ("write frame size: %dx%d\n", p->size_x-crop_left-crop_right,p->size_y-crop_top-crop_bottom );
  
  for(i=crop_top;i<p->size_y-crop_bottom;i++)
    for(j=crop_left;j<p->size_x-crop_right;j++)
    {
      fputc(p->imgY[i][j],p_out);
	  // 为了简便起见,只把Y分量写进myYuvData.txt文件
	  printDataIntoFile(myFp, p->imgY[i][j]);
    }

  crop_left   /= 2;
  crop_right  /= 2;
  crop_top    /= 2;
  crop_bottom /= 2;

  for(i=crop_top;i<p->size_y_cr-crop_bottom;i++)
    for(j=crop_left;j<p->size_x_cr-crop_right;j++)
    {
      fputc(p->imgUV[0][i][j],p_out);
	  //printDataIntoFile(myFp, p->imgUV[0][i][j]);
    }
  for(i=crop_top;i<p->size_y_cr-crop_bottom;i++)
    for(j=crop_left;j<p->size_x_cr-crop_right;j++)
    {
      fputc(p->imgUV[1][i][j],p_out);
	  //printDataIntoFile(myFp, p->imgUV[1][i][j]);
    }
    
  fflush(p_out);
}


      运行程序,观察生成的myYuvData.txt文件,发现其中有不少数据,myYuvData.txt中的部分数据是这样的:

50  216  255  238  255  252  255  255  247  255  254  253  253  254......

 

     下面用matlab读取myYuvData.txt中的数据,并进行重新组织(本人编码了三帧),matlab代码如下:

clear
clc

width = 176;
height = 144;
size = width * height;

cd('C:\Documents and Settings\Administrator\桌面\jm模拟\bin');
fid = fopen('myYuvData.txt', 'r');
[a count] = fscanf(fid,'%d', inf);  % s是所有亮度数据,下面会分割成x, y, z
fclose(fid);

x = a(1:size);
y = a(size + 1 : 2 * size);
z = a(2 * size + 1 : 3 * size);

A = reshape(x, width, height); % 向量变矩阵
frameA = A';
B = reshape(y, width, height);
frameB = B';
C = reshape(x, width, height);
frameC = C';


       frameA, frameB, frameC分别对应第1,2,3帧的数据. 用H.264visa打开生成的码流test.264,也可以得到3帧,对比分析数据,发现完全一致,从而证明下面这一句的确实现了写Y的操作.

fputc(p->imgY[i][j],p_out);

    

      下面,我们来看看对比结果,frameB的第二个宏块的Y数据为:(从matlab的结果中复制过来)

248 255 251 254 255 252 246 236 227 227 226 226 226 224 223 223
218 197 193 236 210 185 219 238 227 227 228 230 230 229 228 229
195 167 151 187 203 164 180 225 227 227 225 225 223 225 226 228
207 203 171 167 241 211 167 209 227 227 227 227 233 224 204 195
224 224 224 224 229 217 213 221 228 227 232 228 217 186 168 155
230 230 229 229 226 223 225 231 215 226 211 180 160 154 150 181
217 217 218 218 220 223 221 215 206 185 162 160 161 166 178 210
222 222 222 221 222 223 207 189 159 153 166 186 194 193 215 215
224 225 222 212 191 171 148 148 175 189 200 205 207 212 216 216
235 212 183 164 152 158 169 181 205 209 210 209 209 213 216 216
181 166 146 143 167 183 205 212 215 210 208 207 208 213 216 216
146 154 171 179 200 203 209 212 212 210 206 205 207 211 217 218
160 177 197 206 211 211 211 212 212 208 205 206 209 214 217 217
199 200 205 207 210 211 211 211 210 207 207 210 213 216 218 217
214 210 209 207 211 212 211 209 208 207 208 213 216 217 218 218
213 211 212 214 212 212 210 208 207 207 209 214 217 218 218 218

       而从H.264visa中复制过来的结果为:

====================== Y Data ======================
+----------------+----------------+----------------+----------------+
|248,255,251,254,|255,252,246,236,|227,227,226,226,|226,224,223,223,|
|218,197,193,236,|210,185,219,238,|227,227,228,230,|230,229,228,229,|
|195,167,151,187,|203,164,180,225,|227,227,225,225,|223,225,226,228,|
|207,203,171,167,|241,211,167,209,|227,227,227,227,|233,224,204,195,|
+----------------+----------------+----------------+----------------+
|224,224,224,224,|229,217,213,221,|228,227,232,228,|217,186,168,155,|
|230,230,229,229,|226,223,225,231,|215,226,211,180,|160,154,150,181,|
|217,217,218,218,|220,223,221,215,|206,185,162,160,|161,166,178,210,|
|222,222,222,221,|222,223,207,189,|159,153,166,186,|194,193,215,215,|
+----------------+----------------+----------------+----------------+
|224,225,222,212,|191,171,148,148,|175,189,200,205,|207,212,216,216,|
|235,212,183,164,|152,158,169,181,|205,209,210,209,|209,213,216,216,|
|181,166,146,143,|167,183,205,212,|215,210,208,207,|208,213,216,216,|
|146,154,171,179,|200,203,209,212,|212,210,206,205,|207,211,217,218,|
+----------------+----------------+----------------+----------------+
|160,177,197,206,|211,211,211,212,|212,208,205,206,|209,214,217,217,|
|199,200,205,207,|210,211,211,211,|210,207,207,210,|213,216,218,217,|
|214,210,209,207,|211,212,211,209,|208,207,208,213,|216,217,218,218,|
|213,211,212,214,|212,212,210,208,|207,207,209,214,|217,218,218,218,|
+----------------+----------------+----------------+----------------+

        再次可见,数据高度一致.

 

        其实还有一种更好而且更简单的方法来证实上述结论,方法是,打开上述C代码中的printDataIntoFile(myFp, p->imgUV[0][i][j]);语句和printDataIntoFile(myFp, p->imgUV[1][i][j]);语句,这样myYuvData.txt中装的数据就包含了YUV三种分量,而test_dec.yuv中也包含了这三种分量,只需要将这两者对比就可以了, 用matlab代码实现对比的方式如下:

clear
clc

width = 176;
height = 144;

cd('C:\Documents and Settings\Administrator\桌面\jm模拟\bin');
fid=fopen('myYuvData.txt', 'r');
[a count1] = fscanf(fid,'%d', inf); % count1为个数
fclose(fid);

fid = fopen('test_dec.yuv', 'r');
[b count2] = fread(fid, inf, 'uchar'); % count2为个数
fclose(fid);

% count1与count2相等,故可以有a - b
c = abs(a - b);

% result 的结果为0,证明a向量与b向量相等
result = sum(c);


      上面的result结果为0,从而证明向量a和b相等,从而证明myYuvData.txt和test_dec.yuv中的数据是完全对应的,进而说明了只有下面三个地方真正实现写像素到test_dec.yuv中.

fputc(p->imgY[i][j],p_out);
fputc(p->imgUV[0][i][j],p_out);
fputc(p->imgUV[1][i][j],p_out);

   

   

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值