+bool changed_since(const std::string &pathname, const timespec &ts)
+{
+ if (ts.tv_sec < 0) {
+ return false;
+ }
+ struct stat buf;
+ if (stat(pathname.c_str(), &buf) != 0) {
+ fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", pathname.c_str());
+ return false;
+ }
+ return (buf.st_mtim.tv_sec != ts.tv_sec || buf.st_mtim.tv_nsec != ts.tv_nsec);
+}
+
+bool is_full_range(const AVPixFmtDescriptor *desc)
+{
+ // This is horrible, but there's no better way that I know of.
+ return (strchr(desc->name, 'j') != nullptr);
+}
+
+AVPixelFormat decide_dst_format(AVPixelFormat src_format, bmusb::PixelFormat dst_format_type)
+{
+ if (dst_format_type == bmusb::PixelFormat_8BitBGRA) {
+ return AV_PIX_FMT_BGRA;
+ }
+ if (dst_format_type == FFmpegCapture::PixelFormat_NV12) {
+ return AV_PIX_FMT_NV12;
+ }
+
+ assert(dst_format_type == bmusb::PixelFormat_8BitYCbCrPlanar);
+
+ // If this is a non-Y'CbCr format, just convert to 4:4:4 Y'CbCr
+ // and be done with it. It's too strange to spend a lot of time on.
+ // (Let's hope there's no alpha.)
+ const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_format);
+ if (src_desc == nullptr ||
+ src_desc->nb_components != 3 ||
+ (src_desc->flags & AV_PIX_FMT_FLAG_RGB)) {
+ return AV_PIX_FMT_YUV444P;
+ }
+
+ // The best for us would be Cb and Cr together if possible,
+ // but FFmpeg doesn't support that except in the special case of
+ // NV12, so we need to go to planar even for the case of NV12.
+ // Thus, look for the closest (but no worse) 8-bit planar Y'CbCr format
+ // that matches in color range. (This will also include the case of
+ // the source format already being acceptable.)
+ bool src_full_range = is_full_range(src_desc);
+ const char *best_format = "yuv444p";
+ unsigned best_score = numeric_limits<unsigned>::max();
+ for (const AVPixFmtDescriptor *desc = av_pix_fmt_desc_next(nullptr);
+ desc;
+ desc = av_pix_fmt_desc_next(desc)) {
+ // Find planar Y'CbCr formats only.
+ if (desc->nb_components != 3) continue;
+ if (desc->flags & AV_PIX_FMT_FLAG_RGB) continue;
+ if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) continue;
+ if (desc->comp[0].plane != 0 ||
+ desc->comp[1].plane != 1 ||
+ desc->comp[2].plane != 2) continue;
+
+ // 8-bit formats only.
+ if (desc->flags & AV_PIX_FMT_FLAG_BE) continue;
+ if (desc->comp[0].depth != 8) continue;
+
+ // Same or better chroma resolution only.
+ int chroma_w_diff = desc->log2_chroma_w - src_desc->log2_chroma_w;
+ int chroma_h_diff = desc->log2_chroma_h - src_desc->log2_chroma_h;
+ if (chroma_w_diff < 0 || chroma_h_diff < 0)
+ continue;
+
+ // Matching full/limited range only.
+ if (is_full_range(desc) != src_full_range)
+ continue;
+
+ // Pick something with as little excess chroma resolution as possible.
+ unsigned score = (1 << (chroma_w_diff)) << chroma_h_diff;
+ if (score < best_score) {
+ best_score = score;
+ best_format = desc->name;
+ }
+ }
+ return av_get_pix_fmt(best_format);
+}
+
+YCbCrFormat decode_ycbcr_format(const AVPixFmtDescriptor *desc, const AVFrame *frame)
+{
+ YCbCrFormat format;
+ AVColorSpace colorspace = av_frame_get_colorspace(frame);
+ switch (colorspace) {
+ case AVCOL_SPC_BT709:
+ format.luma_coefficients = YCBCR_REC_709;
+ break;
+ case AVCOL_SPC_BT470BG:
+ case AVCOL_SPC_SMPTE170M:
+ case AVCOL_SPC_SMPTE240M:
+ format.luma_coefficients = YCBCR_REC_601;
+ break;
+ case AVCOL_SPC_BT2020_NCL:
+ format.luma_coefficients = YCBCR_REC_2020;
+ break;
+ case AVCOL_SPC_UNSPECIFIED:
+ format.luma_coefficients = (frame->height >= 720 ? YCBCR_REC_709 : YCBCR_REC_601);
+ break;
+ default:
+ fprintf(stderr, "Unknown Y'CbCr coefficient enum %d from FFmpeg; choosing Rec. 709.\n",
+ colorspace);
+ format.luma_coefficients = YCBCR_REC_709;
+ break;
+ }
+
+ format.full_range = is_full_range(desc);
+ format.num_levels = 1 << desc->comp[0].depth;
+ format.chroma_subsampling_x = 1 << desc->log2_chroma_w;
+ format.chroma_subsampling_y = 1 << desc->log2_chroma_h;
+
+ switch (frame->chroma_location) {
+ case AVCHROMA_LOC_LEFT:
+ format.cb_x_position = 0.0;
+ format.cb_y_position = 0.5;
+ break;
+ case AVCHROMA_LOC_CENTER:
+ format.cb_x_position = 0.5;
+ format.cb_y_position = 0.5;
+ break;
+ case AVCHROMA_LOC_TOPLEFT:
+ format.cb_x_position = 0.0;
+ format.cb_y_position = 0.0;
+ break;
+ case AVCHROMA_LOC_TOP:
+ format.cb_x_position = 0.5;
+ format.cb_y_position = 0.0;
+ break;
+ case AVCHROMA_LOC_BOTTOMLEFT:
+ format.cb_x_position = 0.0;
+ format.cb_y_position = 1.0;
+ break;
+ case AVCHROMA_LOC_BOTTOM:
+ format.cb_x_position = 0.5;
+ format.cb_y_position = 1.0;
+ break;
+ default:
+ fprintf(stderr, "Unknown chroma location coefficient enum %d from FFmpeg; choosing Rec. 709.\n",
+ frame->chroma_location);
+ format.cb_x_position = 0.5;
+ format.cb_y_position = 0.5;
+ break;
+ }
+
+ format.cr_x_position = format.cb_x_position;
+ format.cr_y_position = format.cb_y_position;
+ return format;
+}
+