Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

105 строки
3.1KB

  1. #include "mpeg-ps.h"
  2. #include "mpeg-types.h"
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include <string.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 onpacket(void* /*param*/, 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_G711A == avtype || PSI_STREAM_AUDIO_G711U == 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] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, size: %u\n", ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - a_pts) / 90, (int)(dts - a_dts) / 90, (unsigned int)bytes);
  32. a_pts = pts;
  33. a_dts = dts;
  34. fwrite(data, 1, bytes, afp);
  35. }
  36. else if (PSI_STREAM_H264 == avtype || PSI_STREAM_H265 == avtype || PSI_STREAM_VIDEO_SVAC == avtype)
  37. {
  38. static int64_t v_pts = 0, v_dts = 0;
  39. assert(0 == v_dts || dts >= v_dts);
  40. printf("[V] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, size: %u%s\n", 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]": "");
  41. v_pts = pts;
  42. v_dts = dts;
  43. fwrite(data, 1, bytes, vfp);
  44. }
  45. else
  46. {
  47. //assert(0);
  48. static int64_t x_pts = 0, x_dts = 0;
  49. if (PTS_NO_VALUE == dts)
  50. dts = pts;
  51. //assert(0 == x_dts || dts >= x_dts);
  52. printf("[X] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d\n", ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - x_pts), (int)(dts - x_dts));
  53. x_pts = pts;
  54. x_dts = dts;
  55. }
  56. return 0;
  57. }
  58. static void mpeg_ps_dec_testonstream(void* param, int stream, int codecid, const void* extra, int bytes, int finish)
  59. {
  60. printf("stream %d, codecid: %d, finish: %s\n", stream, codecid, finish ? "true" : "false");
  61. }
  62. static uint8_t s_packet[2 * 1024 * 1024];
  63. void mpeg_ps_dec_test(const char* file)
  64. {
  65. FILE* fp = fopen(file, "rb");
  66. vfp = fopen("v.h264", "wb");
  67. afp = fopen("a.aac", "wb");
  68. struct ps_demuxer_notify_t notify = {
  69. mpeg_ps_dec_testonstream,
  70. };
  71. ps_demuxer_t* ps = ps_demuxer_create(onpacket, NULL);
  72. ps_demuxer_set_notify(ps, &notify, NULL);
  73. size_t n, i = 0, r = 0;
  74. while ((n = fread(s_packet + i, 1, sizeof(s_packet) - i, fp)) > 0)
  75. {
  76. r = ps_demuxer_input(ps, s_packet, n + i);
  77. memmove(s_packet, s_packet + r, n + i - r);
  78. i = n + i - r;
  79. }
  80. while (i > 0 && r > 0)
  81. {
  82. r = ps_demuxer_input(ps, s_packet, i);
  83. memmove(s_packet, s_packet + r, i - r);
  84. i -= r;
  85. }
  86. ps_demuxer_destroy(ps);
  87. fclose(fp);
  88. fclose(vfp);
  89. fclose(afp);
  90. }