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.

126 lines
4.2KB

  1. #include "mkv-reader.h"
  2. #include "mov-format.h"
  3. #include "mov-writer.h"
  4. #include "webm-vpx.h"
  5. #include "rtsp-payloads.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. static uint8_t s_buffer[4 * 1024 * 1024];
  11. static int s_audio_track;
  12. static int s_video_track;
  13. static int s_subtitle_track;
  14. extern "C" const struct mkv_buffer_t* mkv_file_buffer(void);
  15. extern "C" const struct mov_buffer_t* mov_file_buffer(void);
  16. static inline const char* ftimestamp(int64_t timestamp, char* buf)
  17. {
  18. uint32_t t = (uint32_t)timestamp;
  19. sprintf(buf, "%02u:%02u:%02u.%03u", t / 3600000, (t / 60000) % 60, (t / 1000) % 60, t % 1000);
  20. return buf;
  21. }
  22. static void mkv_reader_test_onread(void* mov, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int flags)
  23. {
  24. static char s_pts[64], s_dts[64];
  25. if (s_video_track == track)
  26. {
  27. static int64_t v_pts, v_dts;
  28. printf("[V] pts: %s, dts: %s, diff: %03d/%03d, bytes: %u%s\n", ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), (int)(pts - v_pts), (int)(dts - v_dts), (unsigned int)bytes, flags ? " [I]" : "");
  29. v_pts = pts;
  30. v_dts = dts;
  31. mov_writer_write((mov_writer_t*)mov, s_video_track-1, buffer, bytes, pts, dts, flags & MKV_FLAGS_KEYFRAME ? MOV_AV_FLAG_KEYFREAME : 0);
  32. }
  33. else if (s_audio_track == track)
  34. {
  35. static int64_t a_pts, a_dts;
  36. printf("[A] pts: %s, dts: %s, diff: %03d/%03d, bytes: %u\n", ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), (int)(pts - a_pts), (int)(dts - a_dts), (unsigned int)bytes);
  37. a_pts = pts;
  38. a_dts = dts;
  39. mov_writer_write((mov_writer_t*)mov, s_audio_track-1, buffer, bytes, pts, dts, 0);
  40. }
  41. else if (s_subtitle_track == track)
  42. {
  43. static int64_t t_pts, t_dts;
  44. printf("[S] pts: %s, dts: %s, diff: %03d/%03d, bytes: %u, text: %.*s\n", ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), (int)(pts - t_pts), (int)(dts - t_dts), (unsigned int)bytes, (int)bytes - 2, (const char*)buffer + 2);
  45. t_pts = pts;
  46. t_dts = dts;
  47. }
  48. else
  49. {
  50. static int64_t x_pts, x_dts;
  51. printf("[%d] pts: %s, dts: %s, diff: %03d/%03d, bytes: %u%s\n", track, ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), (int)(pts - x_pts), (int)(dts - x_dts), (unsigned int)bytes, flags ? " [I]" : "");
  52. x_pts = pts;
  53. x_dts = dts;
  54. //assert(0);
  55. }
  56. }
  57. static uint8_t mkv_codec_id_to_mov_object_id(enum mkv_codec_t codec)
  58. {
  59. int i = avpayload_find_by_mkv(codec);
  60. if (-1 == i)
  61. return 0;
  62. return s_payloads[i].mov;
  63. }
  64. static void mkv_video_info(void* mov, uint32_t track, enum mkv_codec_t codec, int width, int height, const void* extra, size_t bytes)
  65. {
  66. // TODO:
  67. if (bytes < 1 && (MKV_CODEC_VIDEO_VP8 == codec || MKV_CODEC_VIDEO_VP9 == codec))
  68. {
  69. int w, h;
  70. uint8_t buffer[128];
  71. struct webm_vpx_t vpx;
  72. memset(&vpx, 0, sizeof(vpx));
  73. webm_vpx_codec_configuration_record_from_vp9(&vpx, &w, &h, NULL, 0);
  74. bytes = webm_vpx_codec_configuration_record_save(&vpx, buffer, sizeof(buffer));
  75. extra = buffer; // override
  76. }
  77. s_video_track = track;
  78. int t = mov_writer_add_video((mov_writer_t*)mov, mkv_codec_id_to_mov_object_id(codec), width, height, extra, bytes);
  79. assert(t == s_video_track-1);
  80. }
  81. static void mkv_audio_info(void* mov, uint32_t track, enum mkv_codec_t codec, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes)
  82. {
  83. s_audio_track = track;
  84. int t = mov_writer_add_audio((mov_writer_t*)mov, mkv_codec_id_to_mov_object_id(codec), channel_count, bit_per_sample, sample_rate, extra, bytes);
  85. assert(t == s_audio_track-1);
  86. }
  87. static void mkv_subtitle_info(void* /*param*/, uint32_t track, enum mkv_codec_t codec, const void* /*extra*/, size_t /*bytes*/)
  88. {
  89. s_subtitle_track = track;
  90. }
  91. void mkv_2_mp4_test(const char* src, const char* mp4)
  92. {
  93. FILE* fp = fopen(src, "rb");
  94. mkv_reader_t* mkv = mkv_reader_create(mkv_file_buffer(), fp);
  95. uint64_t duration = mkv_reader_getduration(mkv);
  96. FILE* wfp = fopen(mp4, "wb");
  97. mov_writer_t* mov = mov_writer_create(mov_file_buffer(), wfp, 0);
  98. struct mkv_reader_trackinfo_t info = { mkv_video_info, mkv_audio_info, mkv_subtitle_info };
  99. mkv_reader_getinfo(mkv, &info, mov);
  100. while (mkv_reader_read(mkv, s_buffer, sizeof(s_buffer), mkv_reader_test_onread, mov) > 0)
  101. {
  102. }
  103. duration /= 2;
  104. mkv_reader_seek(mkv, (int64_t*)&duration);
  105. mov_writer_destroy(mov);
  106. mkv_reader_destroy(mkv);
  107. fclose(fp);
  108. fclose(wfp);
  109. }