Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1852982
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: LINUX

2012-10-23 09:57:15

以前在6467上似乎没遇到这个问题,最近在DM368(arm_v5t_le-)上遇到了
传说是因为编译优化选项引起的(-O2),可去掉测试看下
此外的解决方法是使用临时变量及memcpy
两个代码片段

点击(此处)折叠或打开

  1. static int replaceVideostartCode(char* data, int len, int isKey)
  2. {
  3.     int i = 0;
  4.     unsigned int startCode = 0;
  5.     int lastStartCodepos = 0;
  6.     int tofoundtimes = 2;
  7.     int alreadyfound = 0;
  8.     
  9.     if(!data || len < 4)
  10.     {
  11.         return 0;
  12.     }
  13.     
  14.     if(((data[i] << 24) | (data[i+1] << 16) |(data[i+2] << 8) | data[i+3]) != 0x00000001)
  15.     {
  16.         return 0;
  17.     }

  18.     if(isKey)
  19.     {
  20.         tofoundtimes = 4;/* sps pps sei iSlice */
  21.     }

  22.     for(i = 0;i < len - 3 && alreadyfound < tofoundtimes;i++)
  23.     {
  24.         startCode = (data[i] << 24) | (data[i+1] << 16) |(data[i+2] << 8) | data[i+3];
  25.         if(startCode == 0x00000001)
  26.         {
  27.             if(i > 0)
  28.             {
  29.                 *((unsigned int*)(data + lastStartCodepos)) = htonl(i - lastStartCodepos - 4);
  30.             }
  31.             lastStartCodepos = i;
  32.             alreadyfound++;
  33.         }
  34.     }
  35.     
  36.     *((unsigned int*)(data + lastStartCodepos)) = htonl(len - lastStartCodepos - 4);
  37.     return 1;
  38. }


  39. static int replaceVideostartCode2(char* data, int len, int isKey)
  40. {
  41.     int i = 0;
  42.     unsigned int startCode = 0;
  43.     int lastStartCodepos = 0;
  44.     int tofoundtimes = 2;
  45.     int alreadyfound = 0;
  46.     unsigned char tmp0 = 0;
  47.     unsigned char tmp1 = 0;
  48.     unsigned char tmp2 = 0;
  49.     unsigned char tmp3 = 0;
  50.     unsigned int tmp = NULL;
  51.     
  52.     if(!data || len < 4)
  53.     {
  54.         return 0;
  55.     }
  56.     
  57.     tmp0 = data[0];
  58.     tmp1 = data[1];
  59.     tmp2 = data[2];
  60.     tmp3 = data[3];
  61.     if(((tmp0 << 24) | (tmp1 << 16) | (tmp2 << 8) | tmp3) != 0x00000001)
  62.     {
  63.      printf("replaceVideostartCode out \n");
  64.         return 0;
  65.     }

  66.     if(isKey)
  67.     {
  68.         tofoundtimes = 4;
  69.     }

  70.     for(i = 0;i < len - 3 && alreadyfound < tofoundtimes;i++)
  71.     {
  72.         tmp0 = data[i + 0];
  73.         tmp1 = data[i + 1];
  74.         tmp2 = data[i + 2];
  75.         tmp3 = data[i + 3];
  76.         startCode = (tmp0 << 24) | (tmp1 << 16) | (tmp2 << 8) | tmp3;
  77.         if(startCode == 0x00000001)
  78.         {
  79.             if(i > 0)
  80.             {
  81.                 tmp = htonl(i - lastStartCodepos - 4);
  82.                 memcpy(data + lastStartCodepos, (unsigned char*)&tmp, sizeof(unsigned int));
  83.             }
  84.             lastStartCodepos = i;
  85.             alreadyfound++;
  86.         }
  87.     }

  88.     tmp = htonl(len - lastStartCodepos - 4);
  89.     memcpy(data + lastStartCodepos, (unsigned char*)&tmp, sizeof(unsigned int));
  90.     return 1;
  91. }

  92. #if 1
  93.     tmp = htons(pos[1].len);
  94.     memcpy(towrite + 1, (unsigned char*)&tmp, sizeof(unsigned short));
  95.     towrite += sizeof(unsigned short) + 1;
  96. #else
  97.     towrite += 1;
  98.     *((unsigned short*)(towrite)) = htons(pos[1].len);
  99.     towrite += sizeof(unsigned short);
  100. #endif

阅读(1272) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~