package com.foxx.androidcast.media;

/*********************************************************************
 * EncodedStreamProbe.java
 * Created at: Sat 16 May 2026 18:50:36 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class EncodedStreamProbe
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 39 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	4d03743f602aa3294bf05780ad25348f3037ffbbdb439eb265a6281f0b21a300
**********************************************************************/
import android.media.MediaFormat;

/** Inspects encoded bitstream headers for diagnostics. */
public final class EncodedStreamProbe {
    private EncodedStreamProbe() {}

    public static String describeKeyframe(String mime, byte[] annexB) {
        if (annexB == null || annexB.length < 5) {
            return "empty";
        }
        int offset = 0;
        if (annexB[0] == 0 && annexB[1] == 0 && annexB[2] == 0 && annexB[3] == 1) {
            offset = 4;
        } else if (annexB[0] == 0 && annexB[1] == 0 && annexB[2] == 1) {
            offset = 3;
        }
        if (offset >= annexB.length) {
            return "no start code";
        }
        if (MediaFormat.MIMETYPE_VIDEO_HEVC.equals(mime)) {
            int nalType = (annexB[offset] >> 1) & 0x3F;
            return "HEVC NAL type " + nalType + " (" + annexB.length + " B)";
        }
        if (MediaFormat.MIMETYPE_VIDEO_AVC.equals(mime)) {
            int nalType = annexB[offset] & 0x1F;
            return "AVC NAL type " + nalType + " (" + annexB.length + " B)";
        }
        return "encoded " + annexB.length + " B";
    }
}
