選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

460 行
13KB

  1. #include "flv-muxer.h"
  2. #include "flv-proto.h"
  3. #include "flv-header.h"
  4. #include "amf0.h"
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include "aom-av1.h"
  12. #include "mpeg4-aac.h"
  13. #include "mpeg4-avc.h"
  14. #include "mpeg4-hevc.h"
  15. #include "mp3-header.h"
  16. #include "opus-head.h"
  17. #define FLV_MUXER "ireader/media-server"
  18. struct flv_muxer_t
  19. {
  20. flv_muxer_handler handler;
  21. void* param;
  22. uint8_t audio_sequence_header;
  23. uint8_t video_sequence_header;
  24. union
  25. {
  26. struct mpeg4_aac_t aac;
  27. struct opus_head_t opus;
  28. } a;
  29. union
  30. {
  31. struct aom_av1_t av1;
  32. struct mpeg4_avc_t avc;
  33. struct mpeg4_hevc_t hevc;
  34. } v;
  35. int vcl; // 0-non vcl, 1-idr, 2-p/b
  36. int update; // avc/hevc sequence header update
  37. uint8_t* ptr;
  38. size_t bytes;
  39. size_t capacity;
  40. };
  41. struct flv_muxer_t* flv_muxer_create(flv_muxer_handler handler, void* param)
  42. {
  43. struct flv_muxer_t* flv;
  44. flv = (struct flv_muxer_t*)calloc(1, sizeof(struct flv_muxer_t));
  45. if (NULL == flv)
  46. return NULL;
  47. flv_muxer_reset(flv);
  48. flv->handler = handler;
  49. flv->param = param;
  50. return flv;
  51. }
  52. void flv_muxer_destroy(struct flv_muxer_t* flv)
  53. {
  54. if (flv->ptr)
  55. {
  56. assert(flv->capacity > 0);
  57. free(flv->ptr);
  58. flv->ptr = NULL;
  59. }
  60. free(flv);
  61. }
  62. int flv_muxer_reset(struct flv_muxer_t* flv)
  63. {
  64. memset(&flv->v, 0, sizeof(flv->v));
  65. flv->audio_sequence_header = 0;
  66. flv->video_sequence_header = 0;
  67. return 0;
  68. }
  69. static int flv_muxer_alloc(struct flv_muxer_t* flv, size_t bytes)
  70. {
  71. void* p;
  72. p = realloc(flv->ptr, bytes);
  73. if (!p)
  74. return -ENOMEM;
  75. flv->ptr = (uint8_t*)p;
  76. flv->capacity = bytes;
  77. return 0;
  78. }
  79. int flv_muxer_g711a(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  80. {
  81. struct flv_audio_tag_header_t audio;
  82. (void)pts;
  83. if (flv->capacity < bytes + 1)
  84. {
  85. if (0 != flv_muxer_alloc(flv, bytes + 4))
  86. return -ENOMEM;
  87. }
  88. audio.bits = FLV_SOUND_BIT_16; // 16-bit samples
  89. audio.channels = FLV_SOUND_CHANNEL_MONO;
  90. audio.rate = 0;
  91. audio.codecid = FLV_AUDIO_G711A;
  92. audio.avpacket = FLV_AVPACKET;
  93. flv_audio_tag_header_write(&audio, flv->ptr, 1);
  94. memcpy(flv->ptr + 1, data, bytes);
  95. return flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, bytes + 1, dts);
  96. }
  97. int flv_muxer_g711u(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  98. {
  99. struct flv_audio_tag_header_t audio;
  100. (void)pts;
  101. if (flv->capacity < bytes + 1)
  102. {
  103. if (0 != flv_muxer_alloc(flv, bytes + 4))
  104. return -ENOMEM;
  105. }
  106. audio.bits = FLV_SOUND_BIT_16; // 16-bit samples
  107. audio.channels = FLV_SOUND_CHANNEL_MONO;
  108. audio.rate = 0;
  109. audio.codecid = FLV_AUDIO_G711U;
  110. audio.avpacket = FLV_AVPACKET;
  111. flv_audio_tag_header_write(&audio, flv->ptr, 1);
  112. memcpy(flv->ptr + 1, data, bytes);
  113. return flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, bytes + 1, dts);
  114. }
  115. int flv_muxer_mp3(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  116. {
  117. struct mp3_header_t mp3;
  118. struct flv_audio_tag_header_t audio;
  119. (void)pts;
  120. if (0 == mp3_header_load(&mp3, data, (int)bytes))
  121. {
  122. return -EINVAL;
  123. }
  124. else
  125. {
  126. audio.channels = 3 == mp3.mode ? FLV_SOUND_CHANNEL_MONO : FLV_SOUND_CHANNEL_STEREO;
  127. switch (mp3_get_frequency(&mp3))
  128. {
  129. case 5500: audio.rate = FLV_SOUND_RATE_5500; break;
  130. case 11025: audio.rate = FLV_SOUND_RATE_11025; break;
  131. case 22050: audio.rate = FLV_SOUND_RATE_22050; break;
  132. case 44100: audio.rate = FLV_SOUND_RATE_44100; break;
  133. default: audio.rate = FLV_SOUND_RATE_44100;
  134. }
  135. }
  136. if (flv->capacity < bytes + 1)
  137. {
  138. if (0 != flv_muxer_alloc(flv, bytes + 4))
  139. return -ENOMEM;
  140. }
  141. audio.bits = FLV_SOUND_BIT_16; // 16-bit samples
  142. audio.codecid = FLV_AUDIO_MP3;
  143. audio.avpacket = FLV_AVPACKET;
  144. flv_audio_tag_header_write(&audio, flv->ptr, 1);
  145. memcpy(flv->ptr + 1, data, bytes); // MP3
  146. return flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, bytes + 1, dts);
  147. }
  148. int flv_muxer_aac(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  149. {
  150. int r, n, m;
  151. struct flv_audio_tag_header_t audio;
  152. (void)pts;
  153. if (flv->capacity < bytes + 2/*AudioTagHeader*/ + 2/*AudioSpecificConfig*/)
  154. {
  155. if (0 != flv_muxer_alloc(flv, bytes + 4))
  156. return -ENOMEM;
  157. }
  158. /* ADTS */
  159. n = mpeg4_aac_adts_load(data, bytes, &flv->a.aac);
  160. if (n <= 0)
  161. return -1; // invalid data
  162. audio.codecid = FLV_AUDIO_AAC;
  163. audio.rate = FLV_SOUND_RATE_44100; // 44k-SoundRate
  164. audio.bits = FLV_SOUND_BIT_16; // 16-bit samples
  165. audio.channels = FLV_SOUND_CHANNEL_STEREO; // Stereo sound
  166. if (0 == flv->audio_sequence_header)
  167. {
  168. flv->audio_sequence_header = 1; // once only
  169. audio.avpacket = FLV_SEQUENCE_HEADER;
  170. // AudioSpecificConfig(AAC sequence header)
  171. flv_audio_tag_header_write(&audio, flv->ptr, flv->capacity);
  172. m = mpeg4_aac_audio_specific_config_save(&flv->a.aac, flv->ptr + 2, flv->capacity - 2);
  173. assert(m + 2 <= (int)flv->capacity);
  174. r = flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, m + 2, dts);
  175. if (0 != r) return r;
  176. }
  177. audio.avpacket = FLV_AVPACKET;
  178. flv_audio_tag_header_write(&audio, flv->ptr, flv->capacity);
  179. memcpy(flv->ptr + 2, (uint8_t*)data + n, bytes - n); // AAC exclude ADTS
  180. assert(bytes - n + 2 <= flv->capacity);
  181. return flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, bytes - n + 2, dts);
  182. }
  183. int flv_muxer_opus(flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  184. {
  185. int r, m;
  186. struct flv_audio_tag_header_t audio;
  187. (void)pts;
  188. if (flv->capacity < bytes + 2/*AudioTagHeader*/ + 29/*OpusHead*/)
  189. {
  190. if (0 != flv_muxer_alloc(flv, bytes + 4))
  191. return -ENOMEM;
  192. }
  193. audio.codecid = FLV_AUDIO_OPUS;
  194. audio.rate = FLV_SOUND_RATE_44100; // 44k-SoundRate
  195. audio.bits = FLV_SOUND_BIT_16; // 16-bit samples
  196. audio.channels = FLV_SOUND_CHANNEL_STEREO; // Stereo sound
  197. if (0 == flv->audio_sequence_header)
  198. {
  199. if (opus_head_load(data, bytes, &flv->a.opus) < 0)
  200. return -1;
  201. flv->audio_sequence_header = 1; // once only
  202. audio.avpacket = FLV_SEQUENCE_HEADER;
  203. // Opus Head
  204. m = flv_audio_tag_header_write(&audio, flv->ptr, flv->capacity);
  205. m += opus_head_save(&flv->a.opus, flv->ptr+m, flv->capacity-m);
  206. assert(m <= (int)flv->capacity);
  207. r = flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, m, dts);
  208. if (0 != r) return r;
  209. }
  210. audio.avpacket = FLV_AVPACKET;
  211. m = flv_audio_tag_header_write(&audio, flv->ptr, flv->capacity);
  212. memcpy(flv->ptr + m, (uint8_t*)data, bytes);
  213. assert(bytes - m <= flv->capacity);
  214. return flv->handler(flv->param, FLV_TYPE_AUDIO, flv->ptr, bytes + m, dts);
  215. }
  216. static int flv_muxer_h264(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
  217. {
  218. int r;
  219. int m;
  220. struct flv_video_tag_header_t video;
  221. video.codecid = FLV_VIDEO_H264;
  222. if ( /*0 == flv->video_sequence_header &&*/ flv->update && flv->v.avc.nb_sps > 0 && flv->v.avc.nb_pps > 0)
  223. {
  224. video.cts = 0;
  225. video.keyframe = 1; // keyframe
  226. video.avpacket = FLV_SEQUENCE_HEADER;
  227. flv_video_tag_header_write(&video, flv->ptr + flv->bytes, flv->capacity - flv->bytes);
  228. m = mpeg4_avc_decoder_configuration_record_save(&flv->v.avc, flv->ptr + flv->bytes + 5, flv->capacity - flv->bytes - 5);
  229. if (m <= 0)
  230. return -1; // invalid data
  231. flv->video_sequence_header = 1; // once only
  232. assert(flv->bytes + m + 5 <= flv->capacity);
  233. r = flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr + flv->bytes, m + 5, dts);
  234. if (0 != r) return r;
  235. }
  236. // has video frame
  237. if (flv->vcl && flv->video_sequence_header)
  238. {
  239. video.cts = pts - dts;
  240. video.keyframe = 1 == flv->vcl ? FLV_VIDEO_KEY_FRAME : FLV_VIDEO_INTER_FRAME;
  241. video.avpacket = FLV_AVPACKET;
  242. flv_video_tag_header_write(&video, flv->ptr, flv->capacity);
  243. assert(flv->bytes <= flv->capacity);
  244. return flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr, flv->bytes, dts);
  245. }
  246. return 0;
  247. }
  248. int flv_muxer_avc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  249. {
  250. if (flv->capacity < bytes + sizeof(flv->v.avc) /*AVCDecoderConfigurationRecord*/)
  251. {
  252. if (0 != flv_muxer_alloc(flv, bytes + sizeof(flv->v.avc)))
  253. return -ENOMEM;
  254. }
  255. flv->bytes = 5;
  256. flv->bytes += h264_annexbtomp4(&flv->v.avc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
  257. if (flv->bytes <= 5)
  258. return -ENOMEM;
  259. return flv_muxer_h264(flv, pts, dts);
  260. }
  261. static int flv_muxer_h265(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
  262. {
  263. int r;
  264. int m;
  265. struct flv_video_tag_header_t video;
  266. video.codecid = FLV_VIDEO_H265;
  267. if ( /*0 == flv->avc_sequence_header &&*/ flv->update && flv->v.hevc.numOfArrays >= 3) // vps + sps + pps
  268. {
  269. video.cts = 0;
  270. video.keyframe = 1; // keyframe
  271. video.avpacket = FLV_SEQUENCE_HEADER;
  272. flv_video_tag_header_write(&video, flv->ptr + flv->bytes, flv->capacity - flv->bytes);
  273. m = mpeg4_hevc_decoder_configuration_record_save(&flv->v.hevc, flv->ptr + flv->bytes + 5, flv->capacity - flv->bytes - 5);
  274. if (m <= 0)
  275. return -1; // invalid data
  276. flv->video_sequence_header = 1; // once only
  277. assert(flv->bytes + m + 5 <= flv->capacity);
  278. r = flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr + flv->bytes, m + 5, dts);
  279. if (0 != r) return r;
  280. }
  281. // has video frame
  282. if (flv->vcl && flv->video_sequence_header)
  283. {
  284. video.cts = pts - dts;
  285. video.keyframe = 1 == flv->vcl ? FLV_VIDEO_KEY_FRAME : FLV_VIDEO_INTER_FRAME;
  286. video.avpacket = FLV_AVPACKET;
  287. flv_video_tag_header_write(&video, flv->ptr, flv->capacity);
  288. assert(flv->bytes <= flv->capacity);
  289. return flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr, flv->bytes, dts);
  290. }
  291. return 0;
  292. }
  293. int flv_muxer_hevc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  294. {
  295. if ((size_t)flv->capacity < bytes + sizeof(flv->v.hevc) /*HEVCDecoderConfigurationRecord*/)
  296. {
  297. if (0 != flv_muxer_alloc(flv, bytes + sizeof(flv->v.hevc)))
  298. return -ENOMEM;
  299. }
  300. flv->bytes = 5;
  301. flv->bytes += h265_annexbtomp4(&flv->v.hevc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
  302. if (flv->bytes <= 5)
  303. return -ENOMEM;
  304. return flv_muxer_h265(flv, pts, dts);
  305. }
  306. int flv_muxer_av1(flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts, uint32_t dts)
  307. {
  308. int r;
  309. int m;
  310. struct flv_video_tag_header_t video;
  311. if ((size_t)flv->capacity < bytes + 5 + sizeof(flv->v.av1) /*HEVCDecoderConfigurationRecord*/)
  312. {
  313. if (0 != flv_muxer_alloc(flv, bytes + sizeof(flv->v.av1)))
  314. return -ENOMEM;
  315. }
  316. video.codecid = FLV_VIDEO_AV1;
  317. if (0 == flv->video_sequence_header)
  318. {
  319. // load av1 information
  320. r = aom_av1_codec_configuration_record_init(&flv->v.av1, data, bytes);
  321. if (0 != r || flv->v.av1.width < 1 || flv->v.av1.height < 1)
  322. return 0 == r ? -1 : r;
  323. video.cts = 0;
  324. video.keyframe = 1; // keyframe
  325. video.avpacket = FLV_SEQUENCE_HEADER;
  326. flv_video_tag_header_write(&video, flv->ptr + flv->bytes, flv->capacity - flv->bytes);
  327. m = aom_av1_codec_configuration_record_save(&flv->v.av1, flv->ptr + flv->bytes + 5, flv->capacity - flv->bytes - 5);
  328. if (m <= 0)
  329. return -1; // invalid data
  330. flv->video_sequence_header = 1; // once only
  331. assert(flv->bytes + m + 5 <= flv->capacity);
  332. r = flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr + flv->bytes, m + 5, dts);
  333. if (0 != r) return r;
  334. }
  335. // has video frame
  336. if (flv->video_sequence_header)
  337. {
  338. video.cts = pts - dts;
  339. video.keyframe = 1 == flv->vcl ? FLV_VIDEO_KEY_FRAME : FLV_VIDEO_INTER_FRAME;
  340. video.avpacket = FLV_AVPACKET;
  341. flv_video_tag_header_write(&video, flv->ptr, flv->capacity);
  342. memcpy(flv->ptr + 5, data, bytes);
  343. return flv->handler(flv->param, FLV_TYPE_VIDEO, flv->ptr, bytes + 5, dts);
  344. }
  345. return 0;
  346. }
  347. int flv_muxer_metadata(flv_muxer_t* flv, const struct flv_metadata_t* metadata)
  348. {
  349. uint8_t* ptr, *end;
  350. uint32_t count;
  351. if (!metadata) return -1;
  352. count = (metadata->audiocodecid ? 5 : 0) + (metadata->videocodecid ? 5 : 0) + 1;
  353. if (flv->capacity < 1024)
  354. {
  355. if (0 != flv_muxer_alloc(flv, 1024))
  356. return -ENOMEM;
  357. }
  358. ptr = flv->ptr;
  359. end = flv->ptr + flv->capacity;
  360. count = (metadata->audiocodecid ? 5 : 0) + (metadata->videocodecid ? 5 : 0) + 1;
  361. // ScriptTagBody
  362. // name
  363. ptr = AMFWriteString(ptr, end, "onMetaData", 10);
  364. // value: SCRIPTDATAECMAARRAY
  365. ptr[0] = AMF_ECMA_ARRAY;
  366. ptr[1] = (uint8_t)((count >> 24) & 0xFF);;
  367. ptr[2] = (uint8_t)((count >> 16) & 0xFF);;
  368. ptr[3] = (uint8_t)((count >> 8) & 0xFF);
  369. ptr[4] = (uint8_t)(count & 0xFF);
  370. ptr += 5;
  371. if (metadata->audiocodecid)
  372. {
  373. ptr = AMFWriteNamedDouble(ptr, end, "audiocodecid", 12, metadata->audiocodecid);
  374. ptr = AMFWriteNamedDouble(ptr, end, "audiodatarate", 13, metadata->audiodatarate /* / 1024.0*/);
  375. ptr = AMFWriteNamedDouble(ptr, end, "audiosamplerate", 15, metadata->audiosamplerate);
  376. ptr = AMFWriteNamedDouble(ptr, end, "audiosamplesize", 15, metadata->audiosamplesize);
  377. ptr = AMFWriteNamedBoolean(ptr, end, "stereo", 6, (uint8_t)metadata->stereo);
  378. }
  379. if (metadata->videocodecid)
  380. {
  381. ptr = AMFWriteNamedDouble(ptr, end, "duration", 8, metadata->duration);
  382. ptr = AMFWriteNamedDouble(ptr, end, "interval", 8, metadata->interval);
  383. ptr = AMFWriteNamedDouble(ptr, end, "videocodecid", 12, metadata->videocodecid);
  384. ptr = AMFWriteNamedDouble(ptr, end, "videodatarate", 13, metadata->videodatarate /* / 1024.0*/);
  385. ptr = AMFWriteNamedDouble(ptr, end, "framerate", 9, metadata->framerate);
  386. ptr = AMFWriteNamedDouble(ptr, end, "height", 6, metadata->height);
  387. ptr = AMFWriteNamedDouble(ptr, end, "width", 5, metadata->width);
  388. }
  389. ptr = AMFWriteNamedString(ptr, end, "encoder", 7, FLV_MUXER, strlen(FLV_MUXER));
  390. ptr = AMFWriteObjectEnd(ptr, end);
  391. return flv->handler(flv->param, FLV_TYPE_SCRIPT, flv->ptr, ptr - flv->ptr, 0);
  392. }