+// There are seemingly no direct indicators of sample rate; you just get
+// one frame's worth and have to guess from that.
+int guess_sample_rate(const VideoFormat &video_format, size_t len)
+{
+ size_t num_samples = len / 3 / 8;
+ size_t num_samples_per_second = num_samples * video_format.frame_rate_nom / video_format.frame_rate_den;
+
+ // See if we match or are very close to any of the mandatory HDMI sample rates.
+ const int candidate_sample_rates[] = { 32000, 44100, 48000 };
+ for (int rate : candidate_sample_rates) {
+ if (abs(int(num_samples_per_second) - rate) < 50) {
+ return rate;
+ }
+ }
+
+ fprintf(stderr, "%ld samples at %d/%d fps (%ld Hz) matches no known sample rate, assuming 48000 Hz\n",
+ num_samples, video_format.frame_rate_nom, video_format.frame_rate_den, num_samples_per_second);
+ return 48000;
+}
+