mpeg.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2017 Twitch Interactive, Inc. or its affiliates. All Rights Reserved. */
  5. /* */
  6. /* Permission is hereby granted, free of charge, to any person obtaining a copy */
  7. /* of this software and associated documentation files (the "Software"), to deal */
  8. /* in the Software without restriction, including without limitation the rights */
  9. /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
  10. /* copies of the Software, and to permit persons to whom the Software is */
  11. /* furnished to do so, subject to the following conditions: */
  12. /* */
  13. /* The above copyright notice and this permission notice shall be included in */
  14. /* all copies or substantial portions of the Software. */
  15. /* */
  16. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
  17. /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
  18. /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
  19. /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
  20. /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
  21. /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
  22. /* THE SOFTWARE. */
  23. /**********************************************************************************************/
  24. #ifndef LIBCAPTION_MPEG_H
  25. #define LIBCAPTION_MPEG_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "caption.h"
  30. #include "cea708.h"
  31. #include "scc.h"
  32. #include <float.h>
  33. #include <stddef.h>
  34. ////////////////////////////////////////////////////////////////////////////////
  35. #define STREAM_TYPE_H262 0x02
  36. #define STREAM_TYPE_H264 0x1B
  37. #define STREAM_TYPE_H265 0x24
  38. #define H262_SEI_PACKET 0xB2
  39. #define H264_SEI_PACKET 0x06
  40. #define H265_SEI_PACKET 0x27 // There is also 0x28
  41. #define MAX_NALU_SIZE (6 * 1024 * 1024)
  42. #define MAX_REFRENCE_FRAMES 64
  43. typedef struct {
  44. size_t size;
  45. uint8_t data[MAX_NALU_SIZE + 1];
  46. double dts, cts;
  47. libcaption_stauts_t status;
  48. // Priority queue for out of order frame processing
  49. // Should probablly be a linked list
  50. size_t front;
  51. size_t latent;
  52. cea708_t cea708[MAX_REFRENCE_FRAMES];
  53. } mpeg_bitstream_t;
  54. void mpeg_bitstream_init(mpeg_bitstream_t* packet);
  55. ////////////////////////////////////////////////////////////////////////////////
  56. // TODO make convenience functions for flv/mp4
  57. /*! \brief
  58. \param
  59. */
  60. size_t mpeg_bitstream_parse(mpeg_bitstream_t* packet, caption_frame_t* frame, const uint8_t* data, size_t size, unsigned stream_type, double dts, double cts);
  61. /*! \brief
  62. \param
  63. */
  64. static inline libcaption_stauts_t mpeg_bitstream_status(mpeg_bitstream_t* packet) { return packet->status; }
  65. /*! \brief
  66. Flushes latent packets caused by out or order frames.
  67. Returns number of latent frames remaining, 0 when complete;
  68. \param
  69. */
  70. size_t mpeg_bitstream_flush(mpeg_bitstream_t* packet, caption_frame_t* frame);
  71. ////////////////////////////////////////////////////////////////////////////////
  72. typedef enum {
  73. sei_type_buffering_period = 0,
  74. sei_type_pic_timing = 1,
  75. sei_type_pan_scan_rect = 2,
  76. sei_type_filler_payload = 3,
  77. sei_type_user_data_registered_itu_t_t35 = 4,
  78. sei_type_user_data_unregistered = 5,
  79. sei_type_recovery_point = 6,
  80. sei_type_dec_ref_pic_marking_repetition = 7,
  81. sei_type_spare_pic = 8,
  82. sei_type_scene_info = 9,
  83. sei_type_sub_seq_info = 10,
  84. sei_type_sub_seq_layer_characteristics = 11,
  85. sei_type_sub_seq_characteristics = 12,
  86. sei_type_full_frame_freeze = 13,
  87. sei_type_full_frame_freeze_release = 14,
  88. sei_type_full_frame_snapshot = 15,
  89. sei_type_progressive_refinement_segment_start = 16,
  90. sei_type_progressive_refinement_segment_end = 17,
  91. sei_type_motion_constrained_slice_group_set = 18,
  92. sei_type_film_grain_characteristics = 19,
  93. sei_type_deblocking_filter_display_preference = 20,
  94. sei_type_stereo_video_info = 21,
  95. } sei_msgtype_t;
  96. ////////////////////////////////////////////////////////////////////////////////
  97. typedef struct _sei_message_t {
  98. size_t size;
  99. sei_msgtype_t type;
  100. struct _sei_message_t* next;
  101. } sei_message_t;
  102. typedef struct {
  103. double timestamp;
  104. sei_message_t* head;
  105. sei_message_t* tail;
  106. } sei_t;
  107. /*! \brief
  108. \param
  109. */
  110. void sei_init(sei_t* sei, double timestamp);
  111. /*! \brief
  112. \param
  113. */
  114. void sei_free(sei_t* sei);
  115. /*! \brief
  116. \param
  117. */
  118. void sei_cat(sei_t* to, sei_t* from, int itu_t_t35);
  119. /*! \brief
  120. \param
  121. */
  122. void sei_message_append(sei_t* sei, sei_message_t* msg);
  123. /*! \brief
  124. \param
  125. */
  126. libcaption_stauts_t sei_parse(sei_t* sei, const uint8_t* data, size_t size, double timestamp);
  127. /*! \brief
  128. \param
  129. */
  130. static inline sei_message_t* sei_message_head(sei_t* sei) { return sei->head; }
  131. /*! \brief
  132. \param
  133. */
  134. static inline sei_message_t* sei_message_tail(sei_t* sei) { return sei->tail; }
  135. /*! \brief
  136. \param
  137. */
  138. sei_message_t* sei_message_next(sei_message_t* msg);
  139. /*! \brief
  140. \param
  141. */
  142. sei_msgtype_t sei_message_type(sei_message_t* msg);
  143. /*! \brief
  144. \param
  145. */
  146. size_t sei_message_size(sei_message_t* msg);
  147. /*! \brief
  148. \param
  149. */
  150. uint8_t* sei_message_data(sei_message_t* msg);
  151. /*! \brief
  152. \param
  153. */
  154. sei_message_t* sei_message_new(sei_msgtype_t type, uint8_t* data, size_t size);
  155. /*! \brief
  156. \param
  157. */
  158. static inline sei_message_t* sei_message_copy(sei_message_t* msg)
  159. {
  160. return sei_message_new(sei_message_type(msg), sei_message_data(msg), sei_message_size(msg));
  161. }
  162. /**
  163. Free message and all accoiated data. Messaged added to sei_t by using sei_append_message MUST NOT be freed
  164. These messages will be freed by calling sei_free()
  165. */
  166. /*! \brief
  167. \param
  168. */
  169. void sei_message_free(sei_message_t* msg);
  170. ////////////////////////////////////////////////////////////////////////////////
  171. /*! \brief
  172. \param
  173. */
  174. size_t sei_render_size(sei_t* sei);
  175. /*! \brief
  176. \param
  177. */
  178. size_t sei_render(sei_t* sei, uint8_t* data);
  179. /*! \brief
  180. \param
  181. */
  182. void sei_dump(sei_t* sei);
  183. /*! \brief
  184. \param
  185. */
  186. void sei_dump_messages(sei_message_t* head, double timestamp);
  187. ////////////////////////////////////////////////////////////////////////////////
  188. /*! \brief
  189. \param
  190. */
  191. libcaption_stauts_t sei_from_scc(sei_t* sei, const scc_t* scc);
  192. /*! \brief
  193. \param
  194. */
  195. libcaption_stauts_t sei_from_caption_frame(sei_t* sei, caption_frame_t* frame);
  196. /*! \brief
  197. \param
  198. */
  199. libcaption_stauts_t sei_from_caption_clear(sei_t* sei);
  200. /*! \brief
  201. \param
  202. */
  203. libcaption_stauts_t sei_to_caption_frame(sei_t* sei, caption_frame_t* frame);
  204. ////////////////////////////////////////////////////////////////////////////////
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif