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.

77 lines
2.1KB

  1. #include "flv-writer.h"
  2. #include "flv-muxer.h"
  3. #include "mpeg4-aac.h"
  4. #include "mpeg-ts.h"
  5. #include "mpeg-types.h"
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. static int on_flv_packet(void* flv, int type, const void* data, size_t bytes, uint32_t timestamp)
  11. {
  12. return flv_writer_input(flv, type, data, bytes, timestamp);
  13. }
  14. static int on_ts_packet(void* param, int program, int /*stream*/, int avtype, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  15. {
  16. static int64_t s_pts = PTS_NO_VALUE;
  17. if (PTS_NO_VALUE == s_pts)
  18. s_pts = pts;
  19. pts -= s_pts;
  20. dts -= s_pts;
  21. flv_muxer_t* muxer = (flv_muxer_t*)param;
  22. if (PSI_STREAM_AAC == avtype)
  23. {
  24. int len = mpeg4_aac_adts_frame_length((const uint8_t*)data, bytes);
  25. while (len > 0 && len <= bytes)
  26. {
  27. flv_muxer_aac(muxer, data, len, (uint32_t)(pts / 90), (uint32_t)(pts / 90));
  28. mpeg4_aac_t aac;
  29. memset(&aac, 0, sizeof(aac));
  30. if(mpeg4_aac_adts_load((const uint8_t*)data, bytes, &aac) > 0 && aac.sampling_frequency > 0)
  31. pts += 1024 /*frame*/ * 1000 / aac.sampling_frequency;
  32. data = (const uint8_t*)data + len;
  33. bytes -= len;
  34. len = mpeg4_aac_adts_frame_length((const uint8_t*)data, bytes);
  35. }
  36. assert(0 == bytes);
  37. }
  38. else if (PSI_STREAM_MP3 == avtype)
  39. {
  40. flv_muxer_mp3(muxer, data, bytes, (uint32_t)(pts / 90), (uint32_t)(dts / 90));
  41. }
  42. else if (PSI_STREAM_H264 == avtype)
  43. {
  44. flv_muxer_avc(muxer, data, bytes, (uint32_t)(pts / 90), (uint32_t)(dts / 90));
  45. }
  46. else if (PSI_STREAM_H265 == avtype)
  47. {
  48. flv_muxer_hevc(muxer, data, bytes, (uint32_t)(pts / 90), (uint32_t)(dts / 90));
  49. }
  50. return 0;
  51. }
  52. void ts2flv_test(const char* inputTS, const char* outputFLV)
  53. {
  54. void* f = flv_writer_create(outputFLV);
  55. flv_muxer_t* m = flv_muxer_create(on_flv_packet, f);
  56. unsigned char ptr[188];
  57. FILE* fp = fopen(inputTS, "rb");
  58. ts_demuxer_t *ts = ts_demuxer_create(on_ts_packet, m);
  59. while (1 == fread(ptr, sizeof(ptr), 1, fp))
  60. {
  61. ts_demuxer_input(ts, ptr, sizeof(ptr));
  62. }
  63. ts_demuxer_flush(ts);
  64. ts_demuxer_destroy(ts);
  65. fclose(fp);
  66. flv_muxer_destroy(m);
  67. flv_writer_destroy(f);
  68. }