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.

102 line
3.2KB

  1. #include "mpeg-ps.h"
  2. #include "mpeg-ts.h"
  3. #include "mpeg-types.h"
  4. #include "mpeg4-aac.h"
  5. #include <stdio.h>
  6. #include <assert.h>
  7. static FILE* vfp;
  8. static FILE* afp;
  9. inline const char* ftimestamp(int64_t t, char* buf)
  10. {
  11. if (PTS_NO_VALUE == t)
  12. {
  13. sprintf(buf, "(null)");
  14. }
  15. else
  16. {
  17. t /= 90;
  18. sprintf(buf, "%d:%02d:%02d.%03d", (int)(t / 3600000), (int)((t / 60000) % 60), (int)((t / 1000) % 60), (int)(t % 1000));
  19. }
  20. return buf;
  21. }
  22. 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)
  23. {
  24. static char s_pts[64], s_dts[64];
  25. if (PSI_STREAM_AAC == avtype || PSI_STREAM_AUDIO_OPUS == avtype)
  26. {
  27. static int64_t a_pts = 0, a_dts = 0;
  28. if (PTS_NO_VALUE == dts)
  29. dts = pts;
  30. //assert(0 == a_dts || dts >= a_dts);
  31. printf("[A][%d:%d] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, bytes: %u%s%s\n", program, stream, ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - a_pts) / 90, (int)(dts - a_dts) / 90, (unsigned int)bytes, flags & MPEG_FLAG_PACKET_CORRUPT ? " [X]" : "", flags & MPEG_FLAG_PACKET_LOST ? " [-]" : "");
  32. a_pts = pts;
  33. a_dts = dts;
  34. fwrite(data, 1, bytes, afp);
  35. int count = 0;
  36. int len = mpeg4_aac_adts_frame_length((const uint8_t*)data, bytes);
  37. while (len > 7 && (size_t)len <= bytes)
  38. {
  39. count++;
  40. bytes -= len;
  41. data = (const uint8_t*)data + len;
  42. len = mpeg4_aac_adts_frame_length((const uint8_t*)data, bytes);
  43. }
  44. }
  45. else if (PSI_STREAM_H264 == avtype || PSI_STREAM_H265 == avtype)
  46. {
  47. static int64_t v_pts = 0, v_dts = 0;
  48. //assert(0 == v_dts || dts >= v_dts);
  49. printf("[V][%d:%d] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, bytes: %u%s%s%s\n", program, stream, ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - v_pts) / 90, (int)(dts - v_dts) / 90, (unsigned int)bytes, flags & MPEG_FLAG_IDR_FRAME ? " [I]":"", flags & MPEG_FLAG_PACKET_CORRUPT ? " [X]" : "", flags & MPEG_FLAG_PACKET_LOST ? " [-]" : "");
  50. v_pts = pts;
  51. v_dts = dts;
  52. fwrite(data, 1, bytes, vfp);
  53. }
  54. else
  55. {
  56. static int64_t x_pts = 0, x_dts = 0;
  57. //assert(0 == x_dts || dts >= x_dts);
  58. printf("[%d][%d:%d] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d%s%s%s\n", avtype, program, stream, ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - x_pts) / 90, (int)(dts - x_dts) / 90, flags & MPEG_FLAG_IDR_FRAME ? " [I]" : "", flags & MPEG_FLAG_PACKET_CORRUPT ? " [X]" : "", flags & MPEG_FLAG_PACKET_LOST ? " [-]" : "");
  59. x_pts = pts;
  60. x_dts = dts;
  61. //assert(0);
  62. }
  63. return 0;
  64. }
  65. static void mpeg_ts_dec_testonstream(void* param, int stream, int codecid, const void* extra, int bytes, int finish)
  66. {
  67. printf("stream %d, codecid: %d, finish: %s\n", stream, codecid, finish ? "true" : "false");
  68. }
  69. void mpeg_ts_dec_test(const char* file)
  70. {
  71. unsigned char ptr[188];
  72. FILE* fp = fopen(file, "rb");
  73. vfp = fopen("v.h264", "wb");
  74. afp = fopen("a.aac", "wb");
  75. struct ts_demuxer_notify_t notify = {
  76. mpeg_ts_dec_testonstream,
  77. };
  78. ts_demuxer_t *ts = ts_demuxer_create(on_ts_packet, NULL);
  79. ts_demuxer_set_notify(ts, &notify, NULL);
  80. while (1 == fread(ptr, sizeof(ptr), 1, fp))
  81. {
  82. ts_demuxer_input(ts, ptr, sizeof(ptr));
  83. }
  84. ts_demuxer_flush(ts);
  85. ts_demuxer_destroy(ts);
  86. fclose(fp);
  87. fclose(vfp);
  88. fclose(afp);
  89. }