Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

226 řádky
7.6KB

  1. #include "mkv-reader.h"
  2. #include "mpeg4-hevc.h"
  3. #include "mpeg4-avc.h"
  4. #include "mpeg4-aac.h"
  5. #include "opus-head.h"
  6. #include "webm-vpx.h"
  7. #include "riff-acm.h"
  8. #include "aom-av1.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. static uint8_t s_packet[2 * 1024 * 1024];
  14. static uint8_t s_buffer[4 * 1024 * 1024];
  15. static FILE* s_vfp, * s_afp;
  16. static struct mpeg4_hevc_t s_hevc;
  17. static struct mpeg4_avc_t s_avc;
  18. static struct mpeg4_aac_t s_aac;
  19. static struct wave_format_t s_wav;
  20. static struct webm_vpx_t s_vpx;
  21. static struct opus_head_t s_opus;
  22. static struct aom_av1_t s_av1;
  23. static uint32_t s_aac_track = 0xFFFFFFFF;
  24. static uint32_t s_avc_track = 0xFFFFFFFF;
  25. static uint32_t s_av1_track = 0xFFFFFFFF;
  26. static uint32_t s_vpx_track = 0xFFFFFFFF;
  27. static uint32_t s_hevc_track = 0xFFFFFFFF;
  28. static uint32_t s_opus_track = 0xFFFFFFFF;
  29. static uint32_t s_mp3_track = 0xFFFFFFFF;
  30. static uint32_t s_subtitle_track = 0xFFFFFFFF;
  31. extern "C" const struct mkv_buffer_t* mkv_file_buffer(void);
  32. static inline const char* ftimestamp(int64_t timestamp, char* buf)
  33. {
  34. uint32_t t = (uint32_t)timestamp;
  35. sprintf(buf, "%02u:%02u:%02u.%03u", t / 3600000, (t / 60000) % 60, (t / 1000) % 60, t % 1000);
  36. return buf;
  37. }
  38. static void mkv_reader_test_onread(void* flv, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int flags)
  39. {
  40. static char s_pts[64], s_dts[64];
  41. static int64_t v_pts, v_dts;
  42. static int64_t a_pts, a_dts;
  43. static int64_t x_pts, x_dts;
  44. if (s_avc_track == track)
  45. {
  46. printf("[H264] 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]" : "");
  47. v_pts = pts;
  48. v_dts = dts;
  49. int n = h264_mp4toannexb(&s_avc, buffer, bytes, s_packet, sizeof(s_packet));
  50. fwrite(s_packet, 1, n, s_vfp);
  51. }
  52. else if (s_hevc_track == track)
  53. {
  54. uint8_t nalu_type = (((const uint8_t*)buffer)[3] >> 1) & 0x3F;
  55. uint8_t irap = 16 <= nalu_type && nalu_type <= 23;
  56. printf("[H265] pts: %s, dts: %s, diff: %03d/%03d, bytes: %u%s,%d\n", ftimestamp(pts, s_pts), ftimestamp(dts, s_dts), (int)(pts - v_pts), (int)(dts - v_dts), (unsigned int)bytes, flags ? " [I]" : "", (unsigned int)nalu_type);
  57. v_pts = pts;
  58. v_dts = dts;
  59. int n = h265_mp4toannexb(&s_hevc, buffer, bytes, s_packet, sizeof(s_packet));
  60. fwrite(s_packet, 1, n, s_vfp);
  61. }
  62. else if (s_av1_track == track)
  63. {
  64. printf("[AV1] 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]" : "");
  65. v_pts = pts;
  66. v_dts = dts;
  67. //int n = aom_av1_codec_configuration_record_save(&s_av1, s_packet, sizeof(s_packet));
  68. //fwrite(s_packet, 1, n, s_vfp);
  69. }
  70. else if (s_vpx_track == track)
  71. {
  72. printf("[VP9] 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]" : "");
  73. v_pts = pts;
  74. v_dts = dts;
  75. //int n = aom_av1_codec_configuration_record_save(&s_av1, s_packet, sizeof(s_packet));
  76. //fwrite(s_packet, 1, n, s_vfp);
  77. }
  78. else if (s_aac_track == track)
  79. {
  80. printf("[AAC] 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);
  81. a_pts = pts;
  82. a_dts = dts;
  83. //uint8_t adts[32];
  84. //int n = mpeg4_aac_adts_save(&s_aac, bytes, adts, sizeof(adts));
  85. //fwrite(adts, 1, n, s_afp);
  86. //fwrite(buffer, 1, bytes, s_afp);
  87. }
  88. else if (s_opus_track == track)
  89. {
  90. printf("[OPUS] 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);
  91. a_pts = pts;
  92. a_dts = dts;
  93. }
  94. else if (s_mp3_track == track)
  95. {
  96. printf("[MP3] 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);
  97. a_pts = pts;
  98. a_dts = dts;
  99. fwrite(buffer, 1, bytes, s_afp);
  100. }
  101. else if (s_subtitle_track == track)
  102. {
  103. static int64_t t_pts, t_dts;
  104. printf("[TEXT] 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);
  105. t_pts = pts;
  106. t_dts = dts;
  107. }
  108. else
  109. {
  110. 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]" : "");
  111. x_pts = pts;
  112. x_dts = dts;
  113. //assert(0);
  114. }
  115. }
  116. static void mkv_video_info(void* /*param*/, uint32_t track, enum mkv_codec_t codec, int /*width*/, int /*height*/, const void* extra, size_t bytes)
  117. {
  118. if (MKV_CODEC_VIDEO_H264 == codec)
  119. {
  120. s_vfp = fopen("v.h264", "wb");
  121. s_avc_track = track;
  122. mpeg4_avc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &s_avc);
  123. }
  124. else if (MKV_CODEC_VIDEO_H265 == codec)
  125. {
  126. s_vfp = fopen("v.h265", "wb");
  127. s_hevc_track = track;
  128. mpeg4_hevc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &s_hevc);
  129. }
  130. else if (MKV_CODEC_VIDEO_AV1 == codec)
  131. {
  132. s_vfp = fopen("v.obus", "wb");
  133. s_av1_track = track;
  134. aom_av1_codec_configuration_record_load((const uint8_t*)extra, bytes, &s_av1);
  135. }
  136. else if (MKV_CODEC_VIDEO_VP9 == codec || MKV_CODEC_VIDEO_VP8 == codec)
  137. {
  138. s_vfp = fopen("v.vp9", "wb");
  139. s_vpx_track = track;
  140. webm_vpx_codec_configuration_record_load((const uint8_t*)extra, bytes, &s_vpx);
  141. }
  142. else
  143. {
  144. const char* name = mkv_codec_find_name(codec);
  145. printf("unknow video %u-%s\n", (unsigned int)codec, name ? name : "*");
  146. //assert(0);
  147. }
  148. }
  149. static void mkv_audio_info(void* /*param*/, uint32_t track, enum mkv_codec_t codec, int channel_count, int /*bit_per_sample*/, int sample_rate, const void* extra, size_t bytes)
  150. {
  151. if (MKV_CODEC_AUDIO_AAC == codec)
  152. {
  153. s_afp = fopen("a.aac", "wb");
  154. s_aac_track = track;
  155. assert(bytes == mpeg4_aac_audio_specific_config_load((const uint8_t*)extra, bytes, &s_aac));
  156. assert(channel_count == s_aac.channels);
  157. s_aac.profile = MPEG4_AAC_LC;
  158. s_aac.channel_configuration = channel_count;
  159. s_aac.sampling_frequency_index = mpeg4_aac_audio_frequency_from(sample_rate);
  160. }
  161. else if (MKV_CODEC_AUDIO_OPUS == codec)
  162. {
  163. s_afp = fopen("a.opus", "wb");
  164. s_opus_track = track;
  165. assert(bytes == opus_head_load((const uint8_t*)extra, bytes, &s_opus));
  166. //assert(s_opus.input_sample_rate == 48000);
  167. }
  168. else if (MKV_CODEC_AUDIO_MP3 == codec || MKV_CODEC_AUDIO_MP1 == codec)
  169. {
  170. s_afp = fopen("a.mp3", "wb");
  171. s_mp3_track = track;
  172. }
  173. else if (MKV_CODEC_AUDIO_ACM == codec)
  174. {
  175. wave_format_load((const uint8_t*)extra, bytes, &s_wav);
  176. s_afp = fopen("a.pcm", "wb");
  177. s_mp3_track = track;
  178. }
  179. else
  180. {
  181. s_aac_track = track;
  182. s_aac.channel_configuration = channel_count;
  183. //s_aac.sampling_frequency_index = mpeg4_aac_audio_frequency_from(sample_rate);
  184. }
  185. }
  186. static void mkv_subtitle_info(void* /*param*/, uint32_t track, enum mkv_codec_t codec, const void* /*extra*/, size_t /*bytes*/)
  187. {
  188. s_subtitle_track = track;
  189. }
  190. void mkv_reader_test(const char* file)
  191. {
  192. FILE* fp = fopen(file, "rb");
  193. mkv_reader_t* mov = mkv_reader_create(mkv_file_buffer(), fp);
  194. uint64_t duration = mkv_reader_getduration(mov);
  195. struct mkv_reader_trackinfo_t info = { mkv_video_info, mkv_audio_info, mkv_subtitle_info };
  196. mkv_reader_getinfo(mov, &info, NULL);
  197. while (mkv_reader_read(mov, s_buffer, sizeof(s_buffer), mkv_reader_test_onread, NULL) > 0)
  198. {
  199. }
  200. duration /= 2;
  201. mkv_reader_seek(mov, (int64_t*)&duration);
  202. mkv_reader_destroy(mov);
  203. if (s_vfp) fclose(s_vfp);
  204. if (s_afp) fclose(s_afp);
  205. fclose(fp);
  206. }