以前在6467上似乎没遇到这个问题,最近在DM368(arm_v5t_le-)上遇到了
传说是因为编译优化选项引起的(-O2),可去掉测试看下
此外的解决方法是使用临时变量及memcpy
两个代码片段
- static int replaceVideostartCode(char* data, int len, int isKey)
- {
- int i = 0;
- unsigned int startCode = 0;
- int lastStartCodepos = 0;
- int tofoundtimes = 2;
- int alreadyfound = 0;
-
- if(!data || len < 4)
- {
- return 0;
- }
-
- if(((data[i] << 24) | (data[i+1] << 16) |(data[i+2] << 8) | data[i+3]) != 0x00000001)
- {
- return 0;
- }
- if(isKey)
- {
- tofoundtimes = 4;/* sps pps sei iSlice */
- }
- for(i = 0;i < len - 3 && alreadyfound < tofoundtimes;i++)
- {
- startCode = (data[i] << 24) | (data[i+1] << 16) |(data[i+2] << 8) | data[i+3];
- if(startCode == 0x00000001)
- {
- if(i > 0)
- {
- *((unsigned int*)(data + lastStartCodepos)) = htonl(i - lastStartCodepos - 4);
- }
- lastStartCodepos = i;
- alreadyfound++;
- }
- }
-
- *((unsigned int*)(data + lastStartCodepos)) = htonl(len - lastStartCodepos - 4);
- return 1;
- }
- static int replaceVideostartCode2(char* data, int len, int isKey)
- {
- int i = 0;
- unsigned int startCode = 0;
- int lastStartCodepos = 0;
- int tofoundtimes = 2;
- int alreadyfound = 0;
- unsigned char tmp0 = 0;
- unsigned char tmp1 = 0;
- unsigned char tmp2 = 0;
- unsigned char tmp3 = 0;
- unsigned int tmp = NULL;
-
- if(!data || len < 4)
- {
- return 0;
- }
-
- tmp0 = data[0];
- tmp1 = data[1];
- tmp2 = data[2];
- tmp3 = data[3];
- if(((tmp0 << 24) | (tmp1 << 16) | (tmp2 << 8) | tmp3) != 0x00000001)
- {
- printf("replaceVideostartCode out \n");
- return 0;
- }
- if(isKey)
- {
- tofoundtimes = 4;
- }
- for(i = 0;i < len - 3 && alreadyfound < tofoundtimes;i++)
- {
- tmp0 = data[i + 0];
- tmp1 = data[i + 1];
- tmp2 = data[i + 2];
- tmp3 = data[i + 3];
- startCode = (tmp0 << 24) | (tmp1 << 16) | (tmp2 << 8) | tmp3;
- if(startCode == 0x00000001)
- {
- if(i > 0)
- {
- tmp = htonl(i - lastStartCodepos - 4);
- memcpy(data + lastStartCodepos, (unsigned char*)&tmp, sizeof(unsigned int));
- }
- lastStartCodepos = i;
- alreadyfound++;
- }
- }
- tmp = htonl(len - lastStartCodepos - 4);
- memcpy(data + lastStartCodepos, (unsigned char*)&tmp, sizeof(unsigned int));
- return 1;
- }
- #if 1
- tmp = htons(pos[1].len);
- memcpy(towrite + 1, (unsigned char*)&tmp, sizeof(unsigned short));
- towrite += sizeof(unsigned short) + 1;
- #else
- towrite += 1;
- *((unsigned short*)(towrite)) = htons(pos[1].len);
- towrite += sizeof(unsigned short);
- #endif
阅读(1272) | 评论(0) | 转发(0) |