Ring.cpp
上传用户:lusi_8715
上传日期:2007-01-08
资源大小:199k
文件大小:3k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  * This application contains code from OpenDivX and is released as a "Larger Work"    *
  4.  * under that license. Consistant with that license, this application is released     *
  5.  * under the GNU General Public License.                                              *
  6.  *                                                                                    *
  7.  * The OpenDivX license can be found at: http://www.projectmayo.com/opendivx/docs.php *
  8.  * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html                      *
  9.  *                                                                                    *
  10.  * Authors: Damien Chavarria <roy204 at projectmayo.com>                              *
  11.  *                                                                                    *
  12.  **************************************************************************************/
  13. /**
  14.  *
  15.  * ring.c
  16.  *
  17.  * Audio Queue.
  18.  * @Author Chavarria Damien.
  19.  *
  20.  */
  21. /*********************************************************************
  22.  *                             INCLUDES                              *
  23.  *********************************************************************/
  24. #include "ring.h"
  25. /*
  26.  * some ring data for
  27.  * the bufferisation
  28.  *
  29.  */
  30. #define RING_SIZE   32000
  31. char                ring[RING_SIZE];
  32. unsigned int        read_pos;
  33. unsigned int        write_pos;
  34. /*********************************************************************
  35.  *                            FUNCTIONS                              *
  36.  *********************************************************************/
  37. void ring_init()
  38. {
  39.   read_pos  = 0;
  40.   write_pos = 0;
  41. }
  42. void ring_read(char *data, int size)
  43. {
  44.   if(write_pos <= read_pos) {
  45.     
  46.     if(read_pos + size < RING_SIZE) {
  47.       memcpy(data, ring + read_pos, size);
  48.       read_pos += size;
  49.     }
  50.     else {
  51.       if(read_pos + size < RING_SIZE + write_pos) {
  52. unsigned int before, after;
  53. before = (RING_SIZE - 1) - read_pos;
  54. after = size - before;
  55. memcpy(data, ring + read_pos, before);
  56. memcpy(data + before, ring, after);
  57. read_pos = after;
  58.       }
  59.       else {
  60.   }
  61.     }
  62.   }
  63.   else {
  64.     if(read_pos + size <= write_pos) {
  65.       memcpy(data, ring + read_pos, size);
  66.       read_pos += size;
  67.     }
  68.     else {
  69.     }
  70.   } 
  71. }
  72. void ring_write(char *data, int size)
  73. {
  74.   if(write_pos >= read_pos) {
  75.     
  76.     if(write_pos + size < RING_SIZE) {
  77.       memcpy(ring + write_pos, data, size);
  78.       write_pos += size;
  79.     }
  80.     else {
  81.       if(write_pos + size < RING_SIZE + read_pos) {
  82. unsigned int before, after;
  83. before = (RING_SIZE - 1) - write_pos;
  84. after = size - before;
  85. memcpy(ring + write_pos, data, before);
  86. memcpy(ring, data + before, after);
  87. write_pos = after;
  88.       }
  89.     }
  90.   }
  91.   else {
  92.     if(write_pos + size <= read_pos) {
  93.       memcpy(ring + write_pos, data, size);
  94.       write_pos += size;
  95.     }
  96.     return;
  97.   }
  98. }
  99. int ring_full(int size)
  100. {
  101.   if(write_pos == read_pos)
  102.     return 0;
  103.   if(write_pos > read_pos) {
  104.     if(write_pos + size < read_pos + RING_SIZE)
  105.       return 0;
  106.     return 1;
  107.   }
  108.   else {
  109.     if(write_pos + size < read_pos)
  110.       return 0;
  111.     return 1;
  112.   }
  113. }