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.

106 line
2.8KB

  1. #include "mpeg-ps.h"
  2. #include "mpeg-ts.h"
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <map>
  6. #include <string.h>
  7. static void* ps_alloc(void* /*param*/, size_t bytes)
  8. {
  9. static char s_buffer[2 * 1024 * 1024];
  10. assert(bytes <= sizeof(s_buffer));
  11. return s_buffer;
  12. }
  13. static void ps_free(void* /*param*/, void* /*packet*/)
  14. {
  15. return;
  16. }
  17. static int ps_write(void* param, int stream, void* packet, size_t bytes)
  18. {
  19. return 1 == fwrite(packet, bytes, 1, (FILE*)param) ? 0 : ferror((FILE*)param);
  20. }
  21. static inline const char* ps_type(int type)
  22. {
  23. switch (type)
  24. {
  25. case PSI_STREAM_MP3: return "MP3";
  26. case PSI_STREAM_AAC: return "AAC";
  27. case PSI_STREAM_H264: return "H264";
  28. case PSI_STREAM_H265: return "H265";
  29. case PSI_STREAM_AUDIO_OPUS: return "OPUS";
  30. case PSI_STREAM_AUDIO_G711A: return "G711";
  31. default: return "*";
  32. }
  33. }
  34. static int on_ts_packet(void* ps, int stream, int codecid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  35. {
  36. printf("[%s] pts: %08lu, dts: %08lu%s\n", ps_type(codecid), (unsigned long)pts, (unsigned long)dts, flags ? " [I]" : "");
  37. int i;
  38. static std::map<int, int> streams;
  39. if (0 == streams.size())
  40. {
  41. //i = ps_muxer_add_stream((ps_muxer_t*)ps, PSI_STREAM_AAC, NULL, 0);
  42. //streams[PSI_STREAM_AAC] = i;
  43. //i = ps_muxer_add_stream((ps_muxer_t*)ps, PSI_STREAM_H264, NULL, 0);
  44. //streams[PSI_STREAM_H264] = i;
  45. //i = ps_muxer_add_stream((ps_muxer_t*)ps, PSI_STREAM_H265, NULL, 0);
  46. //streams[PSI_STREAM_H265] = i;
  47. }
  48. std::map<int, int>::const_iterator it = streams.find(codecid);
  49. if (streams.end() == it)
  50. {
  51. i = ps_muxer_add_stream((ps_muxer_t*)ps, codecid, NULL, 0);
  52. streams[codecid] = i;
  53. }
  54. else
  55. {
  56. i = it->second;
  57. }
  58. return ps_muxer_input((ps_muxer_t*)ps, i, flags, pts, dts, data, bytes);
  59. }
  60. static void ps_demuxer(const char* file, ps_muxer_t* muxer)
  61. {
  62. static uint8_t packet[2 * 1024 * 1024];
  63. FILE* fp = fopen(file, "rb");
  64. size_t n, i = 0;
  65. ps_demuxer_t* ps = ps_demuxer_create(on_ts_packet, muxer);
  66. while ((n = fread(packet + i, 1, sizeof(packet) - i, fp)) > 0)
  67. {
  68. size_t r = ps_demuxer_input(ps, packet, n + i);
  69. memmove(packet, packet + r, n + i - r);
  70. i = n + i - r;
  71. }
  72. ps_demuxer_destroy(ps);
  73. fclose(fp);
  74. }
  75. //mpeg_ps_test("test/fileSequence0.ps", "test/fileSequence0.ps")
  76. void mpeg_ps_test(const char* input)
  77. {
  78. char output[256] = { 0 };
  79. snprintf(output, sizeof(output) - 1, "%s.ps", input);
  80. struct ps_muxer_func_t handler;
  81. handler.alloc = ps_alloc;
  82. handler.write = ps_write;
  83. handler.free = ps_free;
  84. FILE* fp = fopen(output, "wb");
  85. ps_muxer_t* ps = ps_muxer_create(&handler, fp);
  86. ps_demuxer(input, ps);
  87. ps_muxer_destroy(ps);
  88. fclose(fp);
  89. }