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.

118 lines
2.7KB

  1. #include "flv-demuxer.h"
  2. #include "flv-reader.h"
  3. #include "flv-proto.h"
  4. #include <assert.h>
  5. #include <stdio.h>
  6. static unsigned char packet[8 * 1024 * 1024];
  7. static FILE* aac;
  8. static FILE* h264;
  9. inline const char* ftimestamp(uint32_t t, char* buf)
  10. {
  11. sprintf(buf, "%02u:%02u:%02u.%03u", t / 3600000, (t / 60000) % 60, (t / 1000) % 60, t % 1000);
  12. return buf;
  13. }
  14. inline size_t get_adts_length(const uint8_t* data, size_t bytes)
  15. {
  16. assert(bytes >= 6);
  17. return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] >> 5) & 0x07);
  18. }
  19. inline char flv_type(int type)
  20. {
  21. switch (type)
  22. {
  23. case FLV_AUDIO_ASC:
  24. case FLV_AUDIO_OPUS_HEAD:
  25. return 'a';
  26. case FLV_AUDIO_AAC:
  27. case FLV_AUDIO_MP3:
  28. case FLV_AUDIO_OPUS:
  29. return 'A';
  30. case FLV_VIDEO_AVCC:
  31. case FLV_VIDEO_HVCC:
  32. return 'v';
  33. case FLV_VIDEO_H264:
  34. case FLV_VIDEO_H265:
  35. return 'V';
  36. default: return '*';
  37. }
  38. }
  39. static int onFLV(void* /*param*/, int codec, const void* data, size_t bytes, uint32_t pts, uint32_t dts, int flags)
  40. {
  41. static char s_pts[64], s_dts[64];
  42. static uint32_t v_pts = 0, v_dts = 0;
  43. static uint32_t a_pts = 0, a_dts = 0;
  44. printf("[%c] pts: %s, dts: %s, %u, cts: %d, ", flv_type(codec), ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), dts, (int)(pts - dts));
  45. if (FLV_AUDIO_AAC == codec)
  46. {
  47. printf("diff: %03d/%03d", (int)(pts - a_pts), (int)(dts - a_dts));
  48. a_pts = pts;
  49. a_dts = dts;
  50. assert(bytes == get_adts_length((const uint8_t*)data, bytes));
  51. fwrite(data, bytes, 1, aac);
  52. }
  53. else if (FLV_VIDEO_H264 == codec || FLV_VIDEO_H265 == codec || FLV_VIDEO_AV1 == codec)
  54. {
  55. printf("diff: %03d/%03d %s", (int)(pts - v_pts), (int)(dts - v_dts), flags ? "[I]" : "");
  56. v_pts = pts;
  57. v_dts = dts;
  58. fwrite(data, bytes, 1, h264);
  59. }
  60. else if (FLV_AUDIO_MP3 == codec || FLV_AUDIO_OPUS == codec)
  61. {
  62. fwrite(data, bytes, 1, aac);
  63. }
  64. else if (FLV_AUDIO_ASC == codec || FLV_AUDIO_OPUS_HEAD == codec || FLV_VIDEO_AVCC == codec || FLV_VIDEO_HVCC == codec || FLV_VIDEO_AV1C == codec)
  65. {
  66. // nothing to do
  67. }
  68. else if ((3 << 4) == codec)
  69. {
  70. fwrite(data, bytes, 1, aac);
  71. }
  72. else
  73. {
  74. // nothing to do
  75. assert(FLV_SCRIPT_METADATA == codec);
  76. }
  77. printf("\n");
  78. return 0;
  79. }
  80. // flv_reader_test("53340.flv");
  81. void flv_reader_test(const char* file)
  82. {
  83. aac = fopen("audio.aac", "wb");
  84. h264 = fopen("video.h264", "wb");
  85. void* reader = flv_reader_create(file);
  86. flv_demuxer_t* flv = flv_demuxer_create(onFLV, NULL);
  87. int type, r;
  88. size_t taglen;
  89. uint32_t timestamp;
  90. while (1 == flv_reader_read(reader, &type, &timestamp, &taglen, packet, sizeof(packet)))
  91. {
  92. r = flv_demuxer_input(flv, type, packet, taglen, timestamp);
  93. if (r < 0)
  94. {
  95. assert(0);
  96. }
  97. }
  98. flv_demuxer_destroy(flv);
  99. flv_reader_destroy(reader);
  100. fclose(aac);
  101. fclose(h264);
  102. }