evrcpacket.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "evrcpacket.h"
  2. #include <string.h>
  3. #ifndef MEMCPY
  4. #define MEMCPY memcpy
  5. #endif
  6. #ifndef MEMSET
  7. #define MEMSET memset
  8. #endif
  9. #define EVRC8K_FRAMES_HEADER_SIZE(frame_count) (((frame_count)+3)/4)
  10. #define EVRC8K_PACKET_HEADER_SIZE(frame_count) (EVRC8K_FRAMES_HEADER_SIZE(frame_count) + 1)
  11. #define EVRC8K_HEADER_BYTE_OFFSET(frame_index) ( (frame_index) / 4 )
  12. #define EVRC8K_HEADER_BITS_OFFSET(frame_index) ( 6 - (2 * ((frame_index) & 0x3)) )
  13. #define EVRC8K_HEADER_BITS_MARK(frame_index,rate) ( ((rate) & 0x3) << EVRC8K_HEADER_BITS_OFFSET(frame_index))
  14. size_t evrc8k_frame_rate_bytes(uint8_t rate) {
  15. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  16. rate &= 0x3;
  17. if( rate < 4 ) {
  18. return frame_size[ rate ];
  19. } else {
  20. return 0;
  21. }
  22. }
  23. size_t evrc8k_packet_header_size(size_t frame_count) {
  24. if( frame_count > EVRC8K_MAX_PACKET_FRAMES )
  25. return 0;
  26. return 1 + ((frame_count + 3) / 4);
  27. }
  28. size_t evrc8k_packet_packet(uint8_t* packet,const Evrc8KFrame* frames,size_t frame_count) {
  29. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  30. size_t i = 0;
  31. uint8_t* ptr_frames_header = NULL;
  32. uint8_t* ptr_data = NULL;
  33. size_t data_size = 0;
  34. if( NULL == packet || NULL == frames || frame_count == 0 || frame_count > EVRC8K_MAX_PACKET_FRAMES )
  35. return 0;
  36. *packet = (uint8_t)( frame_count & 0x000000FF );
  37. ptr_frames_header = packet + 1;
  38. ptr_data = ptr_frames_header + EVRC8K_FRAMES_HEADER_SIZE(frame_count);
  39. MEMSET(ptr_frames_header,0,EVRC8K_PACKET_HEADER_SIZE(frame_count));
  40. for(i = 0; i < frame_count; ++i) {
  41. ptr_frames_header[ EVRC8K_HEADER_BYTE_OFFSET(i) ] |= EVRC8K_HEADER_BITS_MARK(i,frames[i].rate);
  42. if( frames[i].rate > 3 )
  43. return 0;
  44. MEMCPY(ptr_data + data_size,frames[i].bits,frame_size[ frames[i].rate ] );
  45. data_size += frame_size[ frames[i].rate ];
  46. }
  47. return EVRC8K_PACKET_HEADER_SIZE(frame_count) + data_size;
  48. }
  49. size_t evrc8k_packet_unpacket(Evrc8KFrame* frames,size_t max_frame_count,const uint8_t* packet,size_t packet_size) {
  50. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  51. size_t frame_count = 0;
  52. size_t i = 0;
  53. const uint8_t* ptr_frames_header = NULL;
  54. const uint8_t* ptr_data = NULL;
  55. size_t data_size = 0;
  56. if( NULL == frames || max_frame_count == 0 || NULL == packet || packet_size < 4 )
  57. return 0;
  58. frame_count = *packet;
  59. ptr_frames_header = packet + 1;
  60. ptr_data = ptr_frames_header + EVRC8K_FRAMES_HEADER_SIZE(frame_count);
  61. if( frame_count > max_frame_count || ptr_data > packet + packet_size )
  62. return 0;
  63. for(i = 0; i < frame_count; ++i) {
  64. frames[i].rate = (ptr_frames_header[ EVRC8K_HEADER_BYTE_OFFSET(i) ] >> EVRC8K_HEADER_BITS_OFFSET(i) ) & 0x3;
  65. if( EVRC8K_PACKET_HEADER_SIZE(frame_count) + data_size + frame_size[ frames[i].rate ] > packet_size )
  66. return 0;
  67. MEMCPY(frames[i].bits,ptr_data + data_size,frame_size[ frames[i].rate ] );
  68. data_size += frame_size[ frames[i].rate ];
  69. }
  70. return EVRC8K_PACKET_HEADER_SIZE(frame_count) + data_size;
  71. }
  72. size_t evrc8k_frames_of_packet(const uint8_t* packet,size_t size) {
  73. size_t frame_count = *packet;
  74. if( EVRC8K_PACKET_HEADER_SIZE(frame_count) + frame_count * EVRC8K_EIGHT_SIZE <= size )
  75. return frame_count;
  76. else
  77. return 0;
  78. }
  79. int evrc8k_packet_init_parser(const uint8_t* packet,size_t packet_size,Evrc8KPacketParser* parser) {
  80. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  81. if( NULL == parser || NULL == packet || packet_size < 4 )
  82. return 0;
  83. parser->packet_size = packet_size;
  84. parser->packet = packet;
  85. parser->frame_count = *packet;
  86. parser->hptr = packet + 1;
  87. parser->dptr = parser->hptr + EVRC8K_FRAMES_HEADER_SIZE(parser->frame_count);
  88. parser->pos = 0;
  89. parser->rate = (parser->hptr[ EVRC8K_HEADER_BYTE_OFFSET(0) ] >> EVRC8K_HEADER_BITS_OFFSET(0) ) & 0x3;
  90. parser->frame = parser->dptr;
  91. parser->frame_size = frame_size[ parser->rate ];
  92. return 1;
  93. }
  94. int evrc8k_packet_next_frame(Evrc8KPacketParser* parser) {
  95. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  96. if( NULL == parser )
  97. return 0;
  98. if( parser->pos + 1 >= parser->frame_count )
  99. return 0;
  100. if( (parser->frame + parser->frame_size) >= parser->packet + parser->packet_size )
  101. return 0;
  102. parser->pos++;
  103. parser->rate = (parser->hptr[ EVRC8K_HEADER_BYTE_OFFSET(parser->pos) ] >> EVRC8K_HEADER_BITS_OFFSET(parser->pos) ) & 0x3;
  104. parser->frame += parser->frame_size;
  105. parser->frame_size = frame_size[ parser->rate ];
  106. return 1;
  107. }
  108. int evrc8k_packet_init_appender(uint8_t* buffer,size_t buffer_size,size_t frame_count,Evrc8KPacketAppender* appender) {
  109. if( NULL == buffer || buffer_size < 4 || frame_count == 0 || frame_count > 255 || NULL == appender )
  110. return 0;
  111. appender->buffer = buffer;
  112. *buffer = (uint8_t) (frame_count & 0xFF);
  113. appender->buffer_size = buffer_size;
  114. appender->frame_count = frame_count;
  115. appender->hptr = buffer + 1;
  116. appender->dptr = appender->hptr + EVRC8K_FRAMES_HEADER_SIZE(appender->frame_count);
  117. appender->pos = 0;
  118. appender->packet_size = EVRC8K_PACKET_HEADER_SIZE(appender->frame_count);
  119. MEMSET(appender->hptr,0,EVRC8K_FRAMES_HEADER_SIZE(appender->frame_count));
  120. return 1;
  121. }
  122. int evrc8k_packet_append_frame_raw(Evrc8KPacketAppender* appender,uint8_t rate,const uint8_t* frame) {
  123. const size_t frame_size[] = { EVRC8K_EIGHT_SIZE,EVRC8K_CUSTOM_SIZE,EVRC8K_HALF_SIZE,EVRC8K_FULL_SIZE };
  124. if( NULL == appender || NULL == frame || rate > 3 )
  125. return 0;
  126. if( appender->pos + 1 > appender->frame_count || appender->dptr + frame_size[ rate ] >= appender->buffer + appender->buffer_size )
  127. return 0;
  128. appender->hptr[ EVRC8K_HEADER_BYTE_OFFSET(appender->pos) ] |= EVRC8K_HEADER_BITS_MARK(appender->pos,rate);
  129. MEMCPY(appender->dptr,frame,frame_size[ rate ] );
  130. appender->dptr += frame_size[ rate ];
  131. appender->packet_size += frame_size[ rate ];
  132. appender->pos++;
  133. return 1;
  134. }
  135. int evrc8k_packet_append_frame(Evrc8KPacketAppender* appender,const Evrc8KFrame* frame) {
  136. if( NULL == appender || NULL == frame || frame->rate > 3 )
  137. return 0;
  138. return evrc8k_packet_append_frame_raw(appender,frame->rate,frame->bits);
  139. }