slice.h
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:6k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. #ifndef SLICE_H
  2. #define SLICE_H
  3. #include <sys/types.h>
  4. #ifndef SDL_THREADS
  5. #include <pthread.h>
  6. #else
  7. #include "SDL.h"
  8. #include "SDL_thread.h"
  9. #endif
  10. /* Array of these feeds the slice decoders */
  11. typedef struct
  12. {
  13. unsigned char *data;   /* Buffer for holding the slice data */
  14. int buffer_size;         /* Size of buffer */
  15. int buffer_allocation;   /* Space allocated for buffer  */
  16. int current_position;    /* Position in buffer */
  17. u_int32_t bits;
  18. int bits_size;
  19. #ifndef SDL_THREADS
  20. pthread_mutex_t completion_lock; /* Lock slice until completion */
  21. #else
  22. SDL_mutex *completion_lock;
  23. #endif
  24. int done;           /* Signal for slice decoder to skip */
  25. } mpeg3_slice_buffer_t;
  26. /* Each slice decoder */
  27. typedef struct
  28. {
  29. void *video;     /* mpeg3video_t */
  30. mpeg3_slice_buffer_t *slice_buffer;
  31. int thread_number;      /* Number of this thread */
  32. int current_buffer;     /* Buffer this slice decoder is on */
  33. int buffer_step;        /* Number of buffers to skip */
  34. int last_buffer;        /* Last buffer this decoder should process */
  35. int fault;
  36. int done;
  37. int quant_scale;
  38. int pri_brk;                  /* slice/macroblock */
  39. short block[12][64];
  40. int sparse[12];
  41. #ifndef SDL_THREADS
  42. pthread_t tid;   /* ID of thread */
  43. pthread_mutex_t input_lock, output_lock, completion_lock;
  44. #else
  45. SDL_Thread *tid;   /* ID of thread */
  46. SDL_mutex *input_lock, *output_lock, *completion_lock;
  47. #endif
  48. } mpeg3_slice_t;
  49. #define mpeg3slice_fillbits(buffer, nbits) 
  50. while(((mpeg3_slice_buffer_t*)(buffer))->bits_size < (nbits)) 
  51. if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) 
  52. ((mpeg3_slice_buffer_t*)(buffer))->bits <<= 8; 
  53. ((mpeg3_slice_buffer_t*)(buffer))->bits |= ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; 
  54. ((mpeg3_slice_buffer_t*)(buffer))->bits_size += 8; 
  55. }
  56. #define mpeg3slice_flushbits(buffer, nbits) 
  57. mpeg3slice_fillbits((buffer), (nbits)); 
  58. ((mpeg3_slice_buffer_t*)(buffer))->bits_size -= (nbits); 
  59. }
  60. #define mpeg3slice_flushbit(buffer) 
  61. if(((mpeg3_slice_buffer_t*)(buffer))->bits_size) 
  62. ((mpeg3_slice_buffer_t*)(buffer))->bits_size--; 
  63. else 
  64. if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) 
  65. ((mpeg3_slice_buffer_t*)(buffer))->bits = 
  66. ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; 
  67. ((mpeg3_slice_buffer_t*)(buffer))->bits_size = 7; 
  68. }
  69. static __inline unsigned int mpeg3slice_getbit(mpeg3_slice_buffer_t *buffer)
  70. {
  71. if(buffer->bits_size)
  72. return (buffer->bits >> (--buffer->bits_size)) & 0x1;
  73. else
  74. if(buffer->current_position < buffer->buffer_size)
  75. {
  76. buffer->bits = buffer->data[buffer->current_position++];
  77. buffer->bits_size = 7;
  78. return (buffer->bits >> 7) & 0x1;
  79. }
  80. return 0;
  81. }
  82. static __inline unsigned int mpeg3slice_getbits2(mpeg3_slice_buffer_t *buffer)
  83. {
  84. if(buffer->bits_size >= 2)
  85. return (buffer->bits >> (buffer->bits_size -= 2)) & 0x3;
  86. else
  87. if(buffer->current_position < buffer->buffer_size)
  88. {
  89. buffer->bits <<= 8;
  90. buffer->bits |= buffer->data[buffer->current_position++];
  91. buffer->bits_size += 6;
  92. return (buffer->bits >> buffer->bits_size)  & 0x3;
  93. }
  94. return 0;
  95. }
  96. static __inline unsigned int mpeg3slice_getbyte(mpeg3_slice_buffer_t *buffer)
  97. {
  98. if(buffer->bits_size >= 8)
  99. return (buffer->bits >> (buffer->bits_size -= 8)) & 0xff;
  100. else
  101. if(buffer->current_position < buffer->buffer_size)
  102. {
  103. buffer->bits <<= 8;
  104. buffer->bits |= buffer->data[buffer->current_position++];
  105. return (buffer->bits >> buffer->bits_size) & 0xff;
  106. }
  107. return 0;
  108. }
  109. static __inline unsigned int mpeg3slice_getbits(mpeg3_slice_buffer_t *slice_buffer, int bits)
  110. {
  111. if(bits == 1) return mpeg3slice_getbit(slice_buffer);
  112. mpeg3slice_fillbits(slice_buffer, bits);
  113. return (slice_buffer->bits >> (slice_buffer->bits_size -= bits)) & (0xffffffff >> (32 - bits));
  114. }
  115. static __inline unsigned int mpeg3slice_showbits16(mpeg3_slice_buffer_t *buffer)
  116. {
  117. if(buffer->bits_size >= 16)
  118. return (buffer->bits >> (buffer->bits_size - 16)) & 0xffff;
  119. else
  120. if(buffer->current_position < buffer->buffer_size)
  121. {
  122. buffer->bits <<= 16;
  123. buffer->bits_size += 16;
  124. buffer->bits |= (unsigned int)buffer->data[buffer->current_position++] << 8;
  125. buffer->bits |= buffer->data[buffer->current_position++];
  126. return (buffer->bits >> (buffer->bits_size - 16)) & 0xffff;
  127. }
  128. return 0;
  129. }
  130. static __inline unsigned int mpeg3slice_showbits9(mpeg3_slice_buffer_t *buffer)
  131. {
  132. if(buffer->bits_size >= 9)
  133. return (buffer->bits >> (buffer->bits_size - 9)) & 0x1ff;
  134. else
  135. if(buffer->current_position < buffer->buffer_size)
  136. {
  137. buffer->bits <<= 16;
  138. buffer->bits_size += 16;
  139. buffer->bits |= (unsigned int)buffer->data[buffer->current_position++] << 8;
  140. buffer->bits |= buffer->data[buffer->current_position++];
  141. return (buffer->bits >> (buffer->bits_size - 9)) & 0x1ff;
  142. }
  143. return 0;
  144. }
  145. static __inline unsigned int mpeg3slice_showbits5(mpeg3_slice_buffer_t *buffer)
  146. {
  147. if(buffer->bits_size >= 5)
  148. return (buffer->bits >> (buffer->bits_size - 5)) & 0x1f;
  149. else
  150. if(buffer->current_position < buffer->buffer_size)
  151. {
  152. buffer->bits <<= 8;
  153. buffer->bits_size += 8;
  154. buffer->bits |= buffer->data[buffer->current_position++];
  155. return (buffer->bits >> (buffer->bits_size - 5)) & 0x1f;
  156. }
  157. return 0;
  158. }
  159. static __inline unsigned int mpeg3slice_showbits(mpeg3_slice_buffer_t *slice_buffer, int bits)
  160. {
  161. mpeg3slice_fillbits(slice_buffer, bits);
  162. return (slice_buffer->bits >> (slice_buffer->bits_size - bits)) & (0xffffffff >> (32 - bits));
  163. }
  164. #endif