You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
8.1KB

  1. #include "flv-demuxer.h"
  2. #include "flv-header.h"
  3. #include "flv-proto.h"
  4. #include "mpeg4-aac.h"
  5. #include "mpeg4-avc.h"
  6. #include "mpeg4-hevc.h"
  7. #include "opus-head.h"
  8. #include "aom-av1.h"
  9. #include "amf0.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <errno.h>
  14. struct flv_demuxer_t
  15. {
  16. union
  17. {
  18. struct mpeg4_aac_t aac;
  19. struct opus_head_t opus;
  20. } a;
  21. union
  22. {
  23. struct aom_av1_t av1;
  24. struct mpeg4_avc_t avc;
  25. struct mpeg4_hevc_t hevc;
  26. } v;
  27. flv_demuxer_handler handler;
  28. void* param;
  29. uint8_t* ptr;
  30. int capacity;
  31. };
  32. struct flv_demuxer_t* flv_demuxer_create(flv_demuxer_handler handler, void* param)
  33. {
  34. struct flv_demuxer_t* flv;
  35. flv = (struct flv_demuxer_t*)malloc(sizeof(struct flv_demuxer_t));
  36. if (NULL == flv)
  37. return NULL;
  38. memset(flv, 0, sizeof(struct flv_demuxer_t));
  39. flv->handler = handler;
  40. flv->param = param;
  41. return flv;
  42. }
  43. void flv_demuxer_destroy(struct flv_demuxer_t* flv)
  44. {
  45. if (flv->ptr)
  46. {
  47. assert(flv->capacity > 0);
  48. free(flv->ptr);
  49. }
  50. free(flv);
  51. }
  52. static int flv_demuxer_check_and_alloc(struct flv_demuxer_t* flv, int bytes)
  53. {
  54. if (bytes > flv->capacity)
  55. {
  56. void* p = realloc(flv->ptr, bytes);
  57. if (NULL == p)
  58. return -1;
  59. flv->ptr = (uint8_t*)p;
  60. flv->capacity = bytes;
  61. }
  62. return 0;
  63. }
  64. static int flv_demuxer_audio(struct flv_demuxer_t* flv, const uint8_t* data, int bytes, uint32_t timestamp)
  65. {
  66. int r, n;
  67. struct flv_audio_tag_header_t audio;
  68. n = flv_audio_tag_header_read(&audio, data, bytes);
  69. if (n < 0)
  70. return n;
  71. if (FLV_AUDIO_AAC == audio.codecid)
  72. {
  73. // Adobe Flash Video File Format Specification Version 10.1 >> E.4.2.1 AUDIODATA (p77)
  74. // If the SoundFormat indicates AAC, the SoundType should be 1 (stereo) and the SoundRate should be 3 (44 kHz).
  75. // However, this does not mean that AAC audio in FLV is always stereo, 44 kHz data.Instead, the Flash Player ignores
  76. // these values and extracts the channel and sample rate data is encoded in the AAC bit stream.
  77. //assert(3 == audio.bitrate && 1 == audio.channel);
  78. if (FLV_SEQUENCE_HEADER == audio.avpacket)
  79. {
  80. flv->a.aac.profile = MPEG4_AAC_LC;
  81. flv->a.aac.sampling_frequency_index = MPEG4_AAC_44100;
  82. flv->a.aac.channel_configuration = 2;
  83. flv->a.aac.channels = 2;
  84. flv->a.aac.sampling_frequency = 44100;
  85. flv->a.aac.extension_frequency = 44100;
  86. mpeg4_aac_audio_specific_config_load(data + n, bytes - n, &flv->a.aac);
  87. return flv->handler(flv->param, FLV_AUDIO_ASC, data + n, bytes - n, timestamp, timestamp, 0);
  88. }
  89. else
  90. {
  91. if (0 != flv_demuxer_check_and_alloc(flv, bytes + 7 + 1 + flv->a.aac.npce))
  92. return -ENOMEM;
  93. // AAC ES stream with ADTS header
  94. assert(bytes <= 0x1FFF);
  95. assert(bytes > 2 && 0xFFF0 != (((data[2] << 8) | data[3]) & 0xFFF0)); // don't have ADTS
  96. r = mpeg4_aac_adts_save(&flv->a.aac, (uint16_t)bytes - n, flv->ptr, 7 + 1 + flv->a.aac.npce); // 13-bits
  97. if (r < 7) return -EINVAL; // invalid pce
  98. flv->a.aac.npce = 0; // pce write only once
  99. memmove(flv->ptr + r, data + n, bytes - n);
  100. return flv->handler(flv->param, FLV_AUDIO_AAC, flv->ptr, bytes - n + r, timestamp, timestamp, 0);
  101. }
  102. }
  103. else if (FLV_AUDIO_OPUS == audio.codecid)
  104. {
  105. if (FLV_SEQUENCE_HEADER == audio.avpacket)
  106. {
  107. opus_head_load(data + n, bytes - n, &flv->a.opus);
  108. return flv->handler(flv->param, FLV_AUDIO_OPUS_HEAD, data + n, bytes - n, timestamp, timestamp, 0);
  109. }
  110. else
  111. {
  112. return flv->handler(flv->param, audio.codecid, data + n, bytes - n, timestamp, timestamp, 0);
  113. }
  114. }
  115. else if (FLV_AUDIO_MP3 == audio.codecid || FLV_AUDIO_MP3_8K == audio.codecid)
  116. {
  117. return flv->handler(flv->param, audio.codecid, data + n, bytes - n, timestamp, timestamp, 0);
  118. }
  119. else
  120. {
  121. // Audio frame data
  122. return flv->handler(flv->param, audio.codecid, data + n, bytes - n, timestamp, timestamp, 0);
  123. }
  124. }
  125. static int flv_demuxer_video(struct flv_demuxer_t* flv, const uint8_t* data, int bytes, uint32_t timestamp)
  126. {
  127. int n;
  128. struct flv_video_tag_header_t video;
  129. n = flv_video_tag_header_read(&video, data, bytes);
  130. if (n < 0)
  131. return n;
  132. if (FLV_VIDEO_H264 == video.codecid)
  133. {
  134. if (FLV_SEQUENCE_HEADER == video.avpacket)
  135. {
  136. // AVCDecoderConfigurationRecord
  137. assert(bytes > n + 7);
  138. mpeg4_avc_decoder_configuration_record_load(data + n, bytes - n, &flv->v.avc);
  139. return flv->handler(flv->param, FLV_VIDEO_AVCC, data + n, bytes - n, timestamp + video.cts, timestamp, 0);
  140. }
  141. else if(FLV_AVPACKET == video.avpacket)
  142. {
  143. // feat: h264_mp4toannexb support flv->v.avc.nalu == 0
  144. //assert(flv->v.avc.nalu > 0); // parse AVCDecoderConfigurationRecord failed
  145. //if (flv->v.avc.nalu > 0 && bytes > n) // 5 == bytes flv eof
  146. {
  147. // H.264
  148. if (0 != flv_demuxer_check_and_alloc(flv, bytes + 4 * 1024))
  149. return -ENOMEM;
  150. assert(flv->v.avc.nalu <= 4);
  151. n = h264_mp4toannexb(&flv->v.avc, data + n, bytes - n, flv->ptr, flv->capacity);
  152. if (n <= 0 || n > flv->capacity)
  153. {
  154. assert(0);
  155. return -ENOMEM;
  156. }
  157. return flv->handler(flv->param, FLV_VIDEO_H264, flv->ptr, n, timestamp + video.cts, timestamp, (FLV_VIDEO_KEY_FRAME == video.keyframe) ? 1 : 0);
  158. }
  159. return -EINVAL;
  160. }
  161. else if (FLV_END_OF_SEQUENCE == video.avpacket)
  162. {
  163. return 0; // AVC end of sequence (lower level NALU sequence ender is not required or supported)
  164. }
  165. else
  166. {
  167. assert(0);
  168. return -EINVAL;
  169. }
  170. }
  171. else if (FLV_VIDEO_H265 == video.codecid)
  172. {
  173. if (FLV_SEQUENCE_HEADER == video.avpacket)
  174. {
  175. // HEVCDecoderConfigurationRecord
  176. assert(bytes > n + 7);
  177. mpeg4_hevc_decoder_configuration_record_load(data + n, bytes - n, &flv->v.hevc);
  178. return flv->handler(flv->param, FLV_VIDEO_HVCC, data + n, bytes - n, timestamp + video.cts, timestamp, 0);
  179. }
  180. else if (FLV_AVPACKET == video.avpacket)
  181. {
  182. // feat: h265_mp4toannexb support flv->v.hevc.numOfArrays == 0
  183. //assert(flv->v.hevc.numOfArrays > 0); // parse HEVCDecoderConfigurationRecord failed
  184. //if (flv->v.hevc.numOfArrays > 0 && bytes > n) // 5 == bytes flv eof
  185. {
  186. // H.265
  187. if (0 != flv_demuxer_check_and_alloc(flv, bytes + 4 * 1024))
  188. return -ENOMEM;
  189. n = h265_mp4toannexb(&flv->v.hevc, data + n, bytes - n, flv->ptr, flv->capacity);
  190. if (n <= 0 || n > flv->capacity)
  191. {
  192. assert(0);
  193. return -ENOMEM;
  194. }
  195. return flv->handler(flv->param, FLV_VIDEO_H265, flv->ptr, n, timestamp + video.cts, timestamp, (FLV_VIDEO_KEY_FRAME == video.keyframe) ? 1 : 0);
  196. }
  197. return -EINVAL;
  198. }
  199. else if (FLV_END_OF_SEQUENCE == video.avpacket)
  200. {
  201. return 0; // AVC end of sequence (lower level NALU sequence ender is not required or supported)
  202. }
  203. else
  204. {
  205. assert(0);
  206. return -EINVAL;
  207. }
  208. }
  209. else if (FLV_VIDEO_AV1 == video.codecid)
  210. {
  211. if (FLV_SEQUENCE_HEADER == video.avpacket)
  212. {
  213. // AV1CodecConfigurationRecord
  214. assert(bytes > n + 5);
  215. aom_av1_codec_configuration_record_load(data + n, bytes - n, &flv->v.av1);
  216. return flv->handler(flv->param, FLV_VIDEO_AV1C, data + n, bytes - n, timestamp + video.cts, timestamp, 0);
  217. }
  218. else if (FLV_AVPACKET == video.avpacket)
  219. {
  220. return flv->handler(flv->param, FLV_VIDEO_AV1, data + n, bytes - n, timestamp + video.cts, timestamp, (FLV_VIDEO_KEY_FRAME == video.keyframe) ? 1 : 0);
  221. }
  222. else if (FLV_END_OF_SEQUENCE == video.avpacket)
  223. {
  224. return 0; // AV1 end of sequence (lower level NALU sequence ender is not required or supported)
  225. }
  226. else
  227. {
  228. assert(0);
  229. return -EINVAL;
  230. }
  231. }
  232. else
  233. {
  234. // Video frame data
  235. return flv->handler(flv->param, video.codecid, data + n, bytes - n, timestamp + video.cts, timestamp, (FLV_VIDEO_KEY_FRAME==video.keyframe) ? 1 : 0);
  236. }
  237. }
  238. int flv_demuxer_script(struct flv_demuxer_t* flv, const uint8_t* data, size_t bytes);
  239. int flv_demuxer_input(struct flv_demuxer_t* flv, int type, const void* data, size_t bytes, uint32_t timestamp)
  240. {
  241. int n;
  242. if (bytes < 1)
  243. return 0;
  244. switch (type)
  245. {
  246. case FLV_TYPE_AUDIO:
  247. return flv_demuxer_audio(flv, data, (int)bytes, timestamp);
  248. case FLV_TYPE_VIDEO:
  249. return flv_demuxer_video(flv, data, (int)bytes, timestamp);
  250. case FLV_TYPE_SCRIPT:
  251. n = flv_demuxer_script(flv, data, bytes);
  252. if (n < 12)
  253. return 0; // ignore
  254. n -= 12; // 2-LEN + 10-onMetaData
  255. return flv->handler(flv->param, FLV_SCRIPT_METADATA, (const uint8_t*)data + n, bytes - n, timestamp, timestamp, 0);
  256. default:
  257. assert(0);
  258. return -1;
  259. }
  260. }