avc.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2016 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_AVC_H
  25. #define LIBCAPTION_AVC_H
  26. #include "cea708.h"
  27. #include "caption.h"
  28. #include <float.h>
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #define MAX_NALU_SIZE (4*1024*1024)
  31. typedef struct {
  32. size_t size;
  33. uint8_t data[MAX_NALU_SIZE];
  34. } avcnalu_t;
  35. void avcnalu_init (avcnalu_t* nalu);
  36. int avcnalu_parse_annexb (avcnalu_t* nalu, const uint8_t** data, size_t* size);
  37. static inline uint8_t avcnalu_type (avcnalu_t* nalu) { return nalu->data[0] & 0x1F; }
  38. static inline uint8_t* avcnalu_data (avcnalu_t* nalu) { return &nalu->data[0]; }
  39. static inline size_t avcnalu_size (avcnalu_t* nalu) { return nalu->size; }
  40. ////////////////////////////////////////////////////////////////////////////////
  41. typedef struct _sei_message_t sei_message_t;
  42. typedef enum {
  43. sei_type_buffering_period = 0,
  44. sei_type_pic_timing = 1,
  45. sei_type_pan_scan_rect = 2,
  46. sei_type_filler_payload = 3,
  47. sei_type_user_data_registered_itu_t_t35 = 4,
  48. sei_type_user_data_unregistered = 5,
  49. sei_type_recovery_point = 6,
  50. sei_type_dec_ref_pic_marking_repetition = 7,
  51. sei_type_spare_pic = 8,
  52. sei_type_scene_info = 9,
  53. sei_type_sub_seq_info = 10,
  54. sei_type_sub_seq_layer_characteristics = 11,
  55. sei_type_sub_seq_characteristics = 12,
  56. sei_type_full_frame_freeze = 13,
  57. sei_type_full_frame_freeze_release = 14,
  58. sei_type_full_frame_snapshot = 15,
  59. sei_type_progressive_refinement_segment_start = 16,
  60. sei_type_progressive_refinement_segment_end = 17,
  61. sei_type_motion_constrained_slice_group_set = 18,
  62. sei_type_film_grain_characteristics = 19,
  63. sei_type_deblocking_filter_display_preference = 20,
  64. sei_type_stereo_video_info = 21,
  65. } sei_msgtype_t;
  66. ////////////////////////////////////////////////////////////////////////////////
  67. // time in seconds
  68. typedef struct {
  69. double dts;
  70. double cts;
  71. sei_message_t* head;
  72. sei_message_t* tail;
  73. } sei_t;
  74. /*! \brief
  75. \param
  76. */
  77. void sei_init (sei_t* sei);
  78. /*! \brief
  79. \param
  80. */
  81. void sei_free (sei_t* sei);
  82. /*! \brief
  83. \param
  84. */
  85. static inline double sei_dts (sei_t* sei) { return sei->dts; }
  86. static inline double sei_cts (sei_t* sei) { return sei->cts; }
  87. static inline double sei_pts (sei_t* sei) { return sei->dts + sei->cts; }
  88. /*! \brief
  89. \param
  90. */
  91. int sei_parse_nalu (sei_t* sei, const uint8_t* data, size_t size, double dts, double cts);
  92. /*! \brief
  93. \param
  94. */
  95. // TODO add dts,cts to nalu
  96. static inline int sei_parse_avcnalu (sei_t* sei, avcnalu_t* nalu, double dts, double cts) { return sei_parse_nalu (sei,avcnalu_data (nalu),avcnalu_size (nalu),dts,cts); }
  97. /*! \brief
  98. \param
  99. */
  100. static inline int sei_finish (sei_t* sei) { return sei_parse_nalu (sei,0,0,0.0,DBL_MAX); }
  101. /*! \brief
  102. \param
  103. */
  104. static inline sei_message_t* sei_message_head (sei_t* sei) { return sei->head; }
  105. /*! \brief
  106. \param
  107. */
  108. static inline sei_message_t* sei_message_tail (sei_t* sei) { return sei->tail; }
  109. /*! \brief
  110. \param
  111. */
  112. sei_message_t* sei_message_next (sei_message_t* msg);
  113. /*! \brief
  114. \param
  115. */
  116. sei_msgtype_t sei_message_type (sei_message_t* msg);
  117. /*! \brief
  118. \param
  119. */
  120. size_t sei_message_size (sei_message_t* msg);
  121. /*! \brief
  122. \param
  123. */
  124. uint8_t* sei_message_data (sei_message_t* msg);
  125. /*! \brief
  126. \param
  127. */
  128. sei_message_t* sei_message_new (sei_msgtype_t type, uint8_t* data, size_t size);
  129. /*! \brief
  130. \param
  131. */
  132. static inline sei_message_t* sei_message_copy (sei_message_t* msg)
  133. {
  134. return sei_message_new (sei_message_type (msg), sei_message_data (msg), sei_message_size (msg));
  135. }
  136. /**
  137. Free message and all accoiated data. Messaged added to sei_t by using sei_append_message MUST NOT be freed
  138. These messages will be freed by calling sei_free()
  139. */
  140. /*! \brief
  141. \param
  142. */
  143. void sei_message_free (sei_message_t* msg);
  144. ////////////////////////////////////////////////////////////////////////////////
  145. /*! \brief
  146. \param
  147. */
  148. static inline int sei_decode_cea708 (sei_message_t* msg, cea708_t* cea708)
  149. {
  150. if (sei_type_user_data_registered_itu_t_t35 == sei_message_type (msg)) {
  151. return cea708_parse (sei_message_data (msg), sei_message_size (msg), cea708);
  152. } else {
  153. return 0;
  154. }
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////
  157. /*! \brief
  158. \param
  159. */
  160. size_t sei_render_size (sei_t* sei);
  161. /*! \brief
  162. \param
  163. */
  164. size_t sei_render (sei_t* sei, uint8_t* data);
  165. /*! \brief
  166. \param
  167. */
  168. void sei_dump (sei_t* sei);
  169. /*! \brief
  170. \param
  171. */
  172. void sei_dump_messages (sei_message_t* head);
  173. ////////////////////////////////////////////////////////////////////////////////
  174. /*! \brief
  175. \param
  176. */
  177. int sei_from_caption_frame (sei_t* sei, caption_frame_t* frame);
  178. /*! \brief
  179. \param
  180. */
  181. libcaption_stauts_t sei_to_caption_frame (sei_t* sei, caption_frame_t* frame);
  182. /*! \brief
  183. \param
  184. */
  185. static inline int nalu_to_caption_frame (caption_frame_t* frame, const uint8_t* data, size_t size, double pts, double dts)
  186. {
  187. sei_t sei;
  188. sei_init (&sei);
  189. sei_parse_nalu (&sei, data, size, pts, dts);
  190. sei_to_caption_frame (&sei,frame);
  191. sei_free (&sei);
  192. return 1;
  193. }
  194. ////////////////////////////////////////////////////////////////////////////////
  195. #endif