flv.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_FLV_H
  25. #define LIBCAPTION_FLV_H
  26. #include <stdio.h>
  27. #include <stddef.h>
  28. #include <inttypes.h>
  29. #define FLV_HEADER_SIZE 13
  30. #define FLV_FOOTER_SIZE 4
  31. #define FLV_TAG_HEADER_SIZE 11
  32. #define FLV_TAG_FOOTER_SIZE 4
  33. ////////////////////////////////////////////////////////////////////////////////
  34. #include "avc.h"
  35. ////////////////////////////////////////////////////////////////////////////////
  36. typedef struct {
  37. uint8_t* data;
  38. size_t aloc;
  39. } flvtag_t;
  40. void flvtag_init (flvtag_t* tag);
  41. void flvtag_free (flvtag_t* tag);
  42. void flvtag_swap (flvtag_t* tag1, flvtag_t* tag2);
  43. ////////////////////////////////////////////////////////////////////////////////
  44. typedef enum {
  45. flvtag_type_audio = 0x08,
  46. flvtag_type_video = 0x09,
  47. flvtag_type_scriptdata = 0x12,
  48. } flvtag_type_t;
  49. static inline flvtag_type_t flvtag_type (flvtag_t* tag) { return (flvtag_type_t) tag->data[0]&0x1F; }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. typedef enum {
  52. flvtag_soundformat_unknown = -1,
  53. flvtag_soundformat_linearpcmplatformendian = 0,
  54. flvtag_soundformat_adpcm = 1,
  55. flvtag_soundformat_mp3 = 2,
  56. flvtag_soundformat_linearpcmlittleendian = 3,
  57. flvtag_soundformat_nellymoser_16khzmono = 4,
  58. flvtag_soundformat_nellymoser_8khzmono = 5,
  59. flvtag_soundformat_nellymoser = 6,
  60. flvtag_soundformat_g711alawlogarithmicpcm = 7,
  61. flvtag_soundformat_g711mulawlogarithmicpcm = 8,
  62. flvtag_soundformat_reserved = 9,
  63. flvtag_soundformat_aac = 10,
  64. flvtag_soundformat_speex = 11,
  65. flvtag_soundformat_mp3_8khz = 14,
  66. flvtag_soundformat_devicespecificsound = 15
  67. } flvtag_soundformat_t;
  68. static inline flvtag_soundformat_t flvtag_soundformat (flvtag_t* tag) { return (flvtag_type_audio!=flvtag_type (tag)) ?flvtag_soundformat_unknown: (flvtag_soundformat_t) (tag->data[0]>>4) &0x0F; }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. typedef enum {
  71. flvtag_codecid_unknown = -1,
  72. flvtag_codecid_sorensonh263 = 2,
  73. flvtag_codecid_screenvideo = 3,
  74. flvtag_codecid_on2vp6 = 4,
  75. flvtag_codecid_on2vp6withalphachannel = 5,
  76. flvtag_codecid_screenvideoversion2 = 6,
  77. flvtag_codecid_avc = 7
  78. } flvtag_codecid_t;
  79. static inline flvtag_codecid_t flvtag_codecid (flvtag_t* tag) { return (flvtag_type_video!=flvtag_type (tag)) ? (flvtag_codecid_unknown) : (tag->data[11]&0x0F); }
  80. ////////////////////////////////////////////////////////////////////////////////
  81. typedef enum {
  82. flvtag_frametype_unknown = -1,
  83. flvtag_frametype_keyframe = 1,
  84. flvtag_frametype_interframe = 2,
  85. flvtag_frametype_disposableinterframe = 3,
  86. flvtag_frametype_generatedkeyframe = 4,
  87. flvtag_frametype_commandframe = 5
  88. } flvtag_frametype_t;
  89. static inline flvtag_frametype_t flvtag_frametype (flvtag_t* tag) { return (flvtag_type_video!=flvtag_type (tag)) ?flvtag_frametype_keyframe: ( (tag->data[11]>>4) &0x0F); }
  90. ////////////////////////////////////////////////////////////////////////////////
  91. typedef enum {
  92. flvtag_avcpackettype_unknown = -1,
  93. flvtag_avcpackettype_sequenceheader = 0,
  94. flvtag_avcpackettype_nalu = 1,
  95. flvtag_avcpackettype_endofsequence = 2
  96. } flvtag_avcpackettype_t;
  97. static inline flvtag_avcpackettype_t flvtag_avcpackettype (flvtag_t* tag) { return (flvtag_codecid_avc!=flvtag_codecid (tag)) ?flvtag_avcpackettype_unknown:tag->data[12]; }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. static inline size_t flvtag_size (flvtag_t* tag) { return (tag->data[1]<<16) | (tag->data[2]<<8) | tag->data[3]; }
  100. static inline uint32_t flvtag_timestamp (flvtag_t* tag) { return (tag->data[7]<<24) | (tag->data[4]<<16) | (tag->data[5]<<8) | tag->data[6]; }
  101. static inline uint32_t flvtag_dts (flvtag_t* tag) { return flvtag_timestamp (tag); }
  102. static inline uint32_t flvtag_cts (flvtag_t* tag) { return (flvtag_avcpackettype_nalu!=flvtag_avcpackettype (tag)) ?0: (tag->data[13]<<16) | (tag->data[14]<<8) |tag->data[15]; }
  103. static inline uint32_t flvtag_pts (flvtag_t* tag) { return flvtag_dts (tag)+flvtag_cts (tag); }
  104. static inline double flvtag_dts_seconds (flvtag_t* tag) { return flvtag_dts (tag) / 1000.0; }
  105. static inline double flvtag_cts_seconds (flvtag_t* tag) { return flvtag_cts (tag) / 1000.0; }
  106. static inline double flvtag_pts_seconds (flvtag_t* tag) { return flvtag_pts (tag) / 1000.0; }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. size_t flvtag_header_size (flvtag_t* tag);
  109. size_t flvtag_payload_size (flvtag_t* tag);
  110. uint8_t* flvtag_payload_data (flvtag_t* tag);
  111. ////////////////////////////////////////////////////////////////////////////////
  112. FILE* flv_open_read (const char* flv);
  113. FILE* flv_open_write (const char* flv);
  114. FILE* flv_close (FILE* flv);
  115. ////////////////////////////////////////////////////////////////////////////////
  116. static inline const uint8_t* flvtag_raw_data (flvtag_t* tag) { return tag->data; }
  117. static inline const size_t flvtag_raw_size (flvtag_t* tag) { return flvtag_size (tag)+FLV_TAG_HEADER_SIZE+FLV_TAG_FOOTER_SIZE; }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. int flv_read_tag (FILE* flv, flvtag_t* tag);
  120. int flv_write_tag (FILE* flv, flvtag_t* tag);
  121. int flv_read_header (FILE* flv, int* has_audio, int* has_video);
  122. int flv_write_header (FILE* flv, int has_audio, int has_video);
  123. ////////////////////////////////////////////////////////////////////////////////
  124. // If the tage has more that on sei message, they will be combined into one
  125. sei_t* flv_read_sei (FILE* flv, flvtag_t* tag);
  126. ////////////////////////////////////////////////////////////////////////////////
  127. int flvtag_initavc (flvtag_t* tag, uint32_t dts, int32_t cts, flvtag_frametype_t type);
  128. int flvtag_avcwritenal (flvtag_t* tag, uint8_t* data, size_t size);
  129. int flvtag_addcaption (flvtag_t* tag, const utf8_char_t* text);
  130. ////////////////////////////////////////////////////////////////////////////////
  131. int flvtag_amfcaption_708 (flvtag_t* tag, uint32_t timestamp, sei_message_t* msg);
  132. ////////////////////////////////////////////////////////////////////////////////
  133. // This method is expermental, and not currently available on Twitch
  134. int flvtag_amfcaption_utf8 (flvtag_t* tag, uint32_t timestamp, const utf8_char_t* text);
  135. #endif