1
0

annexb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "annexb.h"
  2. #include <bfc/platform/types.h>
  3. enum
  4. {
  5. InitialUnit = 0,
  6. NewUnit = 1, // start finding start code during AddData
  7. MidUnit = 2, // need to find the next start code from next AddData call to form a complete unit
  8. UnitReady = 3, // a new unit is ready and we are waiting for a GetUnit call
  9. };
  10. typedef struct annex_b_demuxer
  11. {
  12. size_t buffer_position;
  13. size_t number_of_zero_words; // number of zero words as identified from the first unit
  14. size_t current_zero_words; // current zero word count, saved in case NALU crosses two AddData calls
  15. int end_of_stream; // set to 1 when there's no more data (so we know not to look for the next start code)
  16. int state;
  17. size_t buffer_size;
  18. uint8_t buffer[1]; // make sure this is last
  19. } AnnexBDemuxer;
  20. int AddData(const uint8_t **data, size_t *data_len); // data and length remaining are updated on exit. if data_len>0 on exit, call again after calling GetUnit
  21. void EndOfStream();
  22. h264_annexb_demuxer_t AnnexB_Create(int size)
  23. {
  24. AnnexBDemuxer *demuxer = (AnnexBDemuxer *)malloc(sizeof(AnnexBDemuxer) + size);
  25. demuxer->buffer_size = size; // MAX_CODED_FRAME_SIZE;
  26. demuxer->state = InitialUnit;
  27. demuxer->buffer_position = 0;
  28. demuxer->number_of_zero_words = 0;
  29. demuxer->current_zero_words = 0;
  30. demuxer->end_of_stream = 0;
  31. return (h264_annexb_demuxer_t)demuxer;
  32. }
  33. static int AnnexB_GetByte(const uint8_t **data, size_t *data_len, uint8_t *data_byte)
  34. {
  35. if (*data_len)
  36. {
  37. *data_byte = **data;
  38. *data = *data + 1;
  39. *data_len = *data_len - 1;;
  40. return 1;
  41. }
  42. else
  43. return 0;
  44. }
  45. int AnnexB_AddData(h264_annexb_demuxer_t d, const void **_data, size_t *data_len)
  46. {
  47. AnnexBDemuxer *demuxer = (AnnexBDemuxer *)d;
  48. if (demuxer)
  49. {
  50. const uint8_t **data = (const uint8_t **)_data; // cast to something easier to do pointer math with
  51. if (demuxer->state == InitialUnit)
  52. {
  53. // find start code with unknown number of initial zero bytes
  54. while(demuxer->number_of_zero_words == 0)
  55. {
  56. uint8_t data_byte;
  57. if (AnnexB_GetByte(data, data_len, &data_byte))
  58. {
  59. if (data_byte == 0)
  60. {
  61. demuxer->current_zero_words++;
  62. }
  63. else if (data_byte == 1 && demuxer->current_zero_words >= 2)
  64. {
  65. demuxer->number_of_zero_words = demuxer->current_zero_words;
  66. demuxer->current_zero_words = 0;
  67. demuxer->state = MidUnit;
  68. }
  69. else
  70. {
  71. // re-sync
  72. demuxer->current_zero_words = 0;
  73. }
  74. }
  75. else
  76. {
  77. return AnnexB_NeedMoreData;
  78. }
  79. }
  80. }
  81. else if (demuxer->state == NewUnit)
  82. {
  83. // find start code with known number of initial zero b ytes
  84. while (demuxer->state == NewUnit)
  85. {
  86. uint8_t data_byte;
  87. if (AnnexB_GetByte(data, data_len, &data_byte))
  88. {
  89. if (data_byte == 0)
  90. {
  91. demuxer->current_zero_words++;
  92. }
  93. else if (data_byte == 1 && demuxer->current_zero_words >= 2) // we might get more start words than required
  94. {
  95. demuxer->current_zero_words = 0;
  96. demuxer->state = MidUnit;
  97. }
  98. else
  99. {
  100. // re-sync
  101. demuxer->current_zero_words = 0;
  102. }
  103. }
  104. else
  105. {
  106. return AnnexB_NeedMoreData;
  107. }
  108. }
  109. }
  110. if (demuxer->state == MidUnit) // no else because we fall through during the start code scanning)
  111. {
  112. uint8_t data_byte;
  113. while (AnnexB_GetByte(data, data_len, &data_byte))
  114. {
  115. if (data_byte == 0)
  116. {
  117. demuxer->current_zero_words++; // might be the next start word
  118. /* if (demuxer->current_zero_words == 3) // 00 00 00 is also a valid sequence for end-of-nal detection.
  119. {
  120. demuxer->state = UnitReady;
  121. return AnnexB_UnitAvailable;
  122. }*/
  123. }
  124. else if (data_byte == 1 && demuxer->current_zero_words >= 2)
  125. {
  126. while (demuxer->current_zero_words > demuxer->number_of_zero_words)
  127. {
  128. // write trailing zero bytes to stream
  129. if (demuxer->buffer_position >= demuxer->buffer_size)
  130. return AnnexB_BufferFull;
  131. demuxer->buffer[demuxer->buffer_position++] = 0;
  132. demuxer->current_zero_words--;
  133. }
  134. demuxer->current_zero_words = 0;
  135. demuxer->state = UnitReady;
  136. return AnnexB_UnitAvailable;
  137. }
  138. else
  139. {
  140. while (demuxer->current_zero_words)
  141. {
  142. // write any zero bytes that we read to the stream
  143. if (demuxer->buffer_position >= demuxer->buffer_size)
  144. return AnnexB_BufferFull;
  145. demuxer->buffer[demuxer->buffer_position++] = 0;
  146. demuxer->current_zero_words--;
  147. }
  148. if (demuxer->buffer_position >= demuxer->buffer_size)
  149. return AnnexB_BufferFull;
  150. demuxer->buffer[demuxer->buffer_position++] = data_byte;
  151. }
  152. }
  153. if (demuxer->end_of_stream)
  154. {
  155. demuxer->state = UnitReady;
  156. }
  157. else
  158. {
  159. return AnnexB_NeedMoreData;
  160. }
  161. }
  162. if (demuxer->state == UnitReady)
  163. return AnnexB_UnitAvailable;
  164. return AnnexB_NeedMoreData; // dunno how we'd get here
  165. }
  166. else
  167. return AnnexB_Error;
  168. }
  169. void AnnexB_EndOfStream(h264_annexb_demuxer_t d)
  170. {
  171. AnnexBDemuxer *demuxer = (AnnexBDemuxer *)d;
  172. if (demuxer)
  173. demuxer->end_of_stream = 1;
  174. }
  175. int AnnexB_GetUnit(h264_annexb_demuxer_t d, const void **data, size_t *data_len)
  176. {
  177. AnnexBDemuxer *demuxer = (AnnexBDemuxer *)d;
  178. if (demuxer)
  179. {
  180. if (demuxer->state == UnitReady)
  181. {
  182. *data = demuxer->buffer;
  183. *data_len = demuxer->buffer_position;
  184. demuxer->buffer_position = 0;
  185. // if we've found the next start code, go to MidUnit state
  186. if (demuxer->current_zero_words == 0)
  187. {
  188. demuxer->state = MidUnit;
  189. }
  190. else // no start code, need to find it
  191. {
  192. demuxer->state = NewUnit;
  193. }
  194. return AnnexB_UnitAvailable;
  195. }
  196. else
  197. {
  198. return AnnexB_NeedMoreData;
  199. }
  200. }
  201. return AnnexB_Error;
  202. }
  203. void AnnexB_Destroy(h264_annexb_demuxer_t d)
  204. {
  205. AnnexBDemuxer *demuxer = (AnnexBDemuxer *)d;
  206. if (demuxer)
  207. free(demuxer);
  208. }