博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JM8.6解码端是如何把像素值写进test_dec.yuv文件的?(write_out_picture函数)
阅读量:4141 次
发布时间:2019-05-25

本文共 6584 字,大约阅读时间需要 21 分钟。

      写文件的过程必然涉及到打开文件,所以在代码中找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.txtvoid 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
size_y-crop_bottom;i++) for(j=crop_left;j
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
size_y_cr-crop_bottom;i++) for(j=crop_left;j
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
size_y_cr-crop_bottom;i++) for(j=crop_left;j
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代码如下:

clearclcwidth = 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, zfclose(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代码实现对比的方式如下:

clearclcwidth = 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 - bc = 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);

   

   

转载地址:http://mjzti.baihongyu.com/

你可能感兴趣的文章
吴恩达机器学习视频及答案2018
查看>>
corr相关系数
查看>>
pairplot 中参数hue的作用就是在图像中将输出的散点图按照hue指定的特征或标签的类别的颜色种类进行区分
查看>>
理解特征统计偏差、方差、平均值、中位数、百分数等等
查看>>
转 机器学习系列 08:深入理解拉格朗日乘子法、KKT 条件和拉格朗日对偶性
查看>>
scikit对超参数模型优化对比(网格搜索与随机搜索对比)
查看>>
range() 和 np.arange()区别
查看>>
matplotlib横plt.bar()竖plt.barh柱状图对比
查看>>
numpy.where用法详解,对2维数组判断解释的比较清楚的
查看>>
ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed.
查看>>
LSTM预测MNIST手写数字张量流图分析
查看>>
在tensorflow2.0下遇到1.x版本中占位符不兼容问题 tf.placeholder() is not compatible with eager execution的解决方法
查看>>
keras,在 fit 和 evaluate 中 都有 verbose 这个参数标记是否打印进度条
查看>>
tensorflow2.0中valid_data的作用是在训练的过程对对比训练数据与测试数据的准确率 损失率,便于判断模型的训练效果:是过拟合还是欠拟合(过拟合)
查看>>
opencv2安装报错no module named cv2
查看>>
配分函数讲解到位的
查看>>
plt.scatter参数详解 s=25代表点的面积
查看>>
pandas.series的数据定位为什么用两个左中括号[[
查看>>
numpy常用函数之random.normal函数
查看>>
unsupported operand type(s) for + NoneType and int
查看>>