flv_bytestream.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*****************************************************************************
  2. * flv_bytestream.h: flv muxer utilities
  3. *****************************************************************************
  4. * Copyright (C) 2009-2018 x264 project
  5. *
  6. * Authors: Kieran Kunhya <kieran@kunhya.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  21. *
  22. * This program is also available under a commercial proprietary license.
  23. * For more information, contact us at licensing@x264.com.
  24. *****************************************************************************/
  25. #ifndef X264_FLV_BYTESTREAM_H
  26. #define X264_FLV_BYTESTREAM_H
  27. /* offsets for packed values */
  28. #define FLV_AUDIO_SAMPLESSIZE_OFFSET 1
  29. #define FLV_AUDIO_SAMPLERATE_OFFSET 2
  30. #define FLV_AUDIO_CODECID_OFFSET 4
  31. #define FLV_VIDEO_FRAMETYPE_OFFSET 4
  32. /* bitmasks to isolate specific values */
  33. #define FLV_AUDIO_CHANNEL_MASK 0x01
  34. #define FLV_AUDIO_SAMPLESIZE_MASK 0x02
  35. #define FLV_AUDIO_SAMPLERATE_MASK 0x0c
  36. #define FLV_AUDIO_CODECID_MASK 0xf0
  37. #define FLV_VIDEO_CODECID_MASK 0x0f
  38. #define FLV_VIDEO_FRAMETYPE_MASK 0xf0
  39. #define AMF_END_OF_OBJECT 0x09
  40. enum
  41. {
  42. FLV_HEADER_FLAG_HASVIDEO = 1,
  43. FLV_HEADER_FLAG_HASAUDIO = 4,
  44. };
  45. enum
  46. {
  47. FLV_TAG_TYPE_AUDIO = 0x08,
  48. FLV_TAG_TYPE_VIDEO = 0x09,
  49. FLV_TAG_TYPE_META = 0x12,
  50. };
  51. enum
  52. {
  53. FLV_MONO = 0,
  54. FLV_STEREO = 1,
  55. };
  56. enum
  57. {
  58. FLV_SAMPLESSIZE_8BIT = 0,
  59. FLV_SAMPLESSIZE_16BIT = 1 << FLV_AUDIO_SAMPLESSIZE_OFFSET,
  60. };
  61. enum
  62. {
  63. FLV_SAMPLERATE_SPECIAL = 0, /**< signifies 5512Hz and 8000Hz in the case of NELLYMOSER */
  64. FLV_SAMPLERATE_11025HZ = 1 << FLV_AUDIO_SAMPLERATE_OFFSET,
  65. FLV_SAMPLERATE_22050HZ = 2 << FLV_AUDIO_SAMPLERATE_OFFSET,
  66. FLV_SAMPLERATE_44100HZ = 3 << FLV_AUDIO_SAMPLERATE_OFFSET,
  67. };
  68. enum
  69. {
  70. FLV_CODECID_MP3 = 2 << FLV_AUDIO_CODECID_OFFSET,
  71. FLV_CODECID_AAC = 10<< FLV_AUDIO_CODECID_OFFSET,
  72. };
  73. enum
  74. {
  75. FLV_CODECID_H264 = 7,
  76. };
  77. enum
  78. {
  79. FLV_FRAME_KEY = 1 << FLV_VIDEO_FRAMETYPE_OFFSET,
  80. FLV_FRAME_INTER = 2 << FLV_VIDEO_FRAMETYPE_OFFSET,
  81. };
  82. typedef enum
  83. {
  84. AMF_DATA_TYPE_NUMBER = 0x00,
  85. AMF_DATA_TYPE_BOOL = 0x01,
  86. AMF_DATA_TYPE_STRING = 0x02,
  87. AMF_DATA_TYPE_OBJECT = 0x03,
  88. AMF_DATA_TYPE_NULL = 0x05,
  89. AMF_DATA_TYPE_UNDEFINED = 0x06,
  90. AMF_DATA_TYPE_REFERENCE = 0x07,
  91. AMF_DATA_TYPE_MIXEDARRAY = 0x08,
  92. AMF_DATA_TYPE_OBJECT_END = 0x09,
  93. AMF_DATA_TYPE_ARRAY = 0x0a,
  94. AMF_DATA_TYPE_DATE = 0x0b,
  95. AMF_DATA_TYPE_LONG_STRING = 0x0c,
  96. AMF_DATA_TYPE_UNSUPPORTED = 0x0d,
  97. } AMFDataType;
  98. typedef struct flv_buffer
  99. {
  100. uint8_t *data;
  101. unsigned d_cur;
  102. unsigned d_max;
  103. FILE *fp;
  104. uint64_t d_total;
  105. } flv_buffer;
  106. flv_buffer *flv_create_writer( const char *filename );
  107. int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size );
  108. int flv_write_byte( flv_buffer *c, uint8_t *byte );
  109. int flv_flush_data( flv_buffer *c );
  110. void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start );
  111. uint64_t flv_dbl2int( double value );
  112. void flv_put_byte( flv_buffer *c, uint8_t b );
  113. void flv_put_be32( flv_buffer *c, uint32_t val );
  114. void flv_put_be64( flv_buffer *c, uint64_t val );
  115. void flv_put_be16( flv_buffer *c, uint16_t val );
  116. void flv_put_be24( flv_buffer *c, uint32_t val );
  117. void flv_put_tag( flv_buffer *c, const char *tag );
  118. void flv_put_amf_string( flv_buffer *c, const char *str );
  119. void flv_put_amf_double( flv_buffer *c, double d );
  120. #endif