{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://small-teton.github.io/mpeg-ts-analyzer/json-schema-analysis-v2.json",
  "title": "mpeg-ts-analyzer range analysis v2",
  "description": "Schema for --format json --output analysis: every program of the analyzed range in one document, with time series. Shared definitions come from the summary report schema (json-schema-v2.json). Objects permit additive fields and status strings permit additive lowercase values within schema version 2.",
  "type": "object",
  "required": [
    "schema_version",
    "output",
    "input",
    "status",
    "continuity_events",
    "programs",
    "warnings"
  ],
  "properties": {
    "schema_version": {
      "const": "2.0"
    },
    "output": {
      "const": "analysis"
    },
    "input": {
      "$ref": "#/$defs/input"
    },
    "status": {
      "$ref": "json-schema-v2.json#/$defs/status",
      "description": "Current values: complete, incomplete, failed."
    },
    "reason": {
      "$ref": "json-schema-v2.json#/$defs/reasonCode",
      "description": "Stable machine-readable reason code. In addition to the summary report's codes: no_program_analyzed."
    },
    "error": {
      "type": "string",
      "description": "Human-readable error detail; not a stable matching interface."
    },
    "acquisition": {
      "$ref": "json-schema-v2.json#/$defs/acquisition",
      "description": "Evidence of a failed initial acquisition, as in the summary report."
    },
    "analysis_start_offset_bytes": {
      "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
      "description": "Offset where the scan started: the earliest program's analysis start. Absent when no program was analyzed."
    },
    "analysis_end_offset_bytes": {
      "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
      "description": "Offset where the scan ended."
    },
    "sync": {
      "$ref": "json-schema-v2.json#/$defs/sync",
      "description": "TS synchronization of the scan for the whole multiplex."
    },
    "continuity_counter": {
      "$ref": "json-schema-v2.json#/$defs/continuity",
      "description": "Continuity-counter summary for the whole multiplex."
    },
    "continuity_events": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/continuityEvent"
      }
    },
    "transport_errors": {
      "$ref": "json-schema-v2.json#/$defs/affectedPackets",
      "description": "Packets with transport_error_indicator set, for the whole multiplex, from the first packet of the range. Absent when no program was analyzed. Only the fixed 4-byte TS header of such a packet is read, so a malformed adaptation field or a reserved adaptation_field_control does not stop the count; each pid is the one in the damaged header."
    },
    "scrambled_packets": {
      "$ref": "json-schema-v2.json#/$defs/affectedPackets",
      "description": "Packets with a non-zero transport_scrambling_control, for the whole multiplex, from the first packet of the range. Absent when no program was analyzed."
    },
    "psi_health": {
      "$ref": "json-schema-v2.json#/$defs/psiHealth",
      "description": "Availability and repetition of the PAT and of the PMT of every announced program, from the first packet of the range. Absent when no program was analyzed."
    },
    "pid_references": {
      "$ref": "json-schema-v2.json#/$defs/pidReferences",
      "description": "Referenced, missing and unreferenced PIDs for the whole multiplex, from the first packet of the range. Absent when no program was analyzed."
    },
    "pes_headers": {
      "$ref": "json-schema-v2.json#/$defs/pesHeaders",
      "description": "PES header checks of every elementary stream of the analyzed programs. Absent when no program was analyzed."
    },
    "pts_repetition": {
      "$ref": "json-schema-v2.json#/$defs/ptsRepetition",
      "description": "Intervals between PES packets carrying a PTS on each video and audio stream of the analyzed programs. Absent when no program was analyzed."
    },
    "multiplexing": {
      "$ref": "json-schema-v2.json#/$defs/multiplexing",
      "description": "Report-only multiplexing diagnostics: per-PID maximum packet gaps for the whole multiplex, from the first packet of the range, and the mux lead and A/V lead difference of every elementary stream of the analyzed programs. Absent when no program was analyzed."
    },
    "programs": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/program"
      }
    },
    "psi_transitions": {
      "type": "array",
      "description": "PAT and PMT version changes applied during the scan, in stream order, for the PAT and the analyzed programs. Absent when no change was applied. programs describes the first complete PAT/PMT set.",
      "items": {
        "$ref": "json-schema-v2.json#/$defs/psiTransition"
      }
    },
    "psi_layout": {
      "$ref": "#/$defs/psiLayout",
      "description": "PSI layout of the range: PAT and PMT sections seen, layouts in effect and observed PIDs. Absent when no program was analyzed."
    },
    "warnings": {
      "type": "array",
      "items": {
        "$ref": "json-schema-v2.json#/$defs/warning"
      }
    }
  },
  "$defs": {
    "input": {
      "type": "object",
      "required": [
        "path",
        "file_size_bytes",
        "offset_bytes"
      ],
      "properties": {
        "path": {
          "type": "string"
        },
        "file_size_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "packet_size_bytes": {
          "enum": [188, 192, 204],
          "description": "Physical packet size: 188 (TS), 192 (M2TS: 4-byte TP_extra_header, then the TS packet) or 204 (the TS packet, then a 16-byte trailer)."
        },
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "limit_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        }
      }
    },
    "continuityEvent": {
      "type": "object",
      "required": [
        "offset_bytes",
        "pid",
        "expected_cc",
        "actual_cc"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "expected_cc": {
          "type": "integer",
          "minimum": 0,
          "maximum": 15
        },
        "actual_cc": {
          "type": "integer",
          "minimum": 0,
          "maximum": 15
        }
      }
    },
    "program": {
      "description": "A program of the summary report's program shape, plus its analysis. Analysis fields are absent for a program whose PMT was not found (status incomplete, reason pmt_parse_failed).",
      "allOf": [
        {
          "$ref": "json-schema-v2.json#/$defs/program"
        }
      ],
      "properties": {
        "analysis_start_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset of the first packet analyzed for this program: just after its PMT."
        },
        "analysis_end_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset after the last packet analyzed for this program, when a PAT version change removed it before the end of the scan. Absent otherwise."
        },
        "average_bitrate_bps": {
          "type": "number",
          "minimum": 0
        },
        "compliance": {
          "$ref": "json-schema-v2.json#/$defs/compliance"
        },
        "timestamp_anomalies": {
          "$ref": "json-schema-v2.json#/$defs/timestampAnomalies"
        },
        "pcr_jitter": {
          "$ref": "json-schema-v2.json#/$defs/pcrJitter"
        },
        "bitrate": {
          "$ref": "json-schema-v2.json#/$defs/bitrate"
        },
        "series": {
          "$ref": "#/$defs/series"
        }
      }
    },
    "series": {
      "description": "Time series of one program. elapsed_ms is PCR time since the program's first PCR in the range; it does not advance across a PCR discontinuity, matching the 1-second bitrate windows.",
      "type": "object",
      "required": [
        "pcr",
        "pes",
        "timestamp_events",
        "pcr_jitter",
        "bitrate"
      ],
      "properties": {
        "pcr": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pcrPoint"
          }
        },
        "pes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pesPoint"
          }
        },
        "timestamp_events": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/timestampEvent"
          }
        },
        "pcr_jitter": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/jitterPoint"
          },
          "description": "The byte_position estimate of every measured PCR, in stream order (pcr_jitter.estimate.model)."
        },
        "bitrate": {
          "$ref": "#/$defs/bitrateWindows"
        }
      }
    },
    "pcrPoint": {
      "type": "object",
      "required": [
        "offset_bytes",
        "elapsed_ms",
        "pcr_ms"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "elapsed_ms": {
          "type": "number",
          "minimum": 0
        },
        "pcr_ms": {
          "type": "number",
          "minimum": 0,
          "description": "PCR value in milliseconds."
        },
        "interval_ms": {
          "type": "number",
          "minimum": 0,
          "description": "Interval to the previous PCR; absent when none was measured (first PCR, discontinuity)."
        },
        "discontinuity": {
          "type": "boolean",
          "description": "True when this PCR starts a new timing segment."
        }
      }
    },
    "pesPoint": {
      "type": "object",
      "required": [
        "pid",
        "offset_bytes"
      ],
      "properties": {
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "elapsed_ms": {
          "type": "number",
          "minimum": 0,
          "description": "Elapsed time of the last PCR before the PES; absent before the first PCR."
        },
        "pts_ms": {
          "type": "number",
          "minimum": 0
        },
        "dts_ms": {
          "type": "number",
          "minimum": 0
        },
        "pcr_gap_ms": {
          "type": "number",
          "description": "PCR to DTS (or PTS) gap, when two PCRs of one timing segment bracket the PES; read modulo 2^33 as the signed difference nearest to zero."
        }
      }
    },
    "timestampEvent": {
      "type": "object",
      "required": [
        "pid",
        "offset_bytes",
        "kind",
        "message"
      ],
      "properties": {
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "kind": {
          "type": "string",
          "description": "Current values: backward, wraparound, forward_gap, dts_after_pts."
        },
        "message": {
          "type": "string"
        }
      }
    },
    "jitterPoint": {
      "type": "object",
      "required": [
        "offset_bytes",
        "jitter_ns"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "jitter_ns": {
          "type": "number"
        }
      }
    },
    "bitrateWindows": {
      "type": "object",
      "required": [
        "window_seconds",
        "duration_seconds",
        "pids"
      ],
      "properties": {
        "window_seconds": {
          "const": 1
        },
        "duration_seconds": {
          "type": "array",
          "description": "Length in seconds of each window of elapsed time, in order. Every window is 1 second except a shorter last one, so the sum is the measured duration.",
          "items": {
            "type": "number",
            "exclusiveMinimum": 0,
            "maximum": 1
          }
        },
        "pids": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "pid",
              "label",
              "bytes",
              "bps"
            ],
            "properties": {
              "pid": {
                "$ref": "json-schema-v2.json#/$defs/pid"
              },
              "label": {
                "type": "string"
              },
              "bytes": {
                "type": "array",
                "description": "Bytes of the PID in each window of duration_seconds. Their sum times 8, divided by the sum of duration_seconds, is the PID's average bitrate.",
                "items": {
                  "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
                }
              },
              "bps": {
                "type": "array",
                "description": "Bitrate in each window of duration_seconds (bytes * 8 / duration), in order.",
                "items": {
                  "type": "number",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    },
    "psiLayout": {
      "type": "object",
      "required": [
        "sections",
        "layouts",
        "pids"
      ],
      "properties": {
        "sections": {
          "type": "array",
          "description": "PAT sections on PID 0x0000 and PMT sections (table_id 0x02) on every PMT PID that the first complete PAT or an applied PAT change announced, including PMT sections sent before that PAT, from the first packet of the range to the end of the scan, ordered by first occurrence. Each entry is a run: a valid section identical to the previous one with the same pid, table, table_id_extension, current_next_indicator and section_number extends that entry; an invalid section, or data the section reader dropped on the PID, ends every valid run of the PID; consecutive invalid copies with the same invalid_reason and header form one entry, which anything else on the PID ends. Entries grow with table changes and PSI errors, not with retransmissions or the range length. Lookup: a section start listed in a packet of --output packets matches at most one entry, the one with the packet's pid, a matching table (table_id 0x00 for pat, 0x02 for pmt), the same header fields and offset_bytes <= packet offset <= last_offset_bytes; copies of other sections on the same PID can lie inside that interval.",
          "items": {
            "$ref": "#/$defs/psiSection"
          }
        },
        "layouts": {
          "type": "array",
          "description": "Layouts in effect, in stream order. They cover the range without gaps: the first starts at the first packet of the range, each later one at the packet after its transitions, and each ends where the next starts or at analysis_end_offset_bytes.",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/psiLayoutEntry"
          }
        },
        "pids": {
          "type": "array",
          "description": "Every PID seen from the first packet of the range to the end of the scan, in PID order.",
          "items": {
            "$ref": "#/$defs/observedPid"
          }
        }
      }
    },
    "psiSection": {
      "type": "object",
      "description": "A run of copies of one PAT or PMT section, or of consecutive invalid copies. Header fields are absent only when the section has no long-form header (an invalid section with invalid_reason malformed).",
      "required": [
        "offset_bytes",
        "last_offset_bytes",
        "count",
        "pid",
        "table",
        "status"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset of the packet in which the first copy of the run starts."
        },
        "last_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset of the packet in which the last copy of the run starts."
        },
        "count": {
          "type": "integer",
          "minimum": 1
        },
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "table": {
          "$ref": "json-schema-v2.json#/$defs/status",
          "description": "Current values: pat, pmt."
        },
        "status": {
          "$ref": "json-schema-v2.json#/$defs/status",
          "description": "Current values: applied (part of a table the analysis applied: the first complete PAT/PMT or a psi_transitions entry), retransmission (identical to a section of an applied table, including a late replay of a superseded version), not_applied (valid but never applied: an incomplete or conflicting version, current_next_indicator 0, or the PMT of a program that is not analyzed), invalid."
        },
        "invalid_reason": {
          "$ref": "json-schema-v2.json#/$defs/status",
          "description": "Present when status is invalid. Current values: malformed, invalid_crc32, section_number_beyond_last, parse_error."
        },
        "transport_stream_id": {
          "type": "integer",
          "minimum": 0,
          "maximum": 65535,
          "description": "PAT only."
        },
        "program_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 65535,
          "description": "PMT only."
        },
        "version_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 31
        },
        "section_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 255
        },
        "last_section_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 255
        },
        "current_next_indicator": {
          "enum": [
            0,
            1
          ]
        }
      }
    },
    "psiLayoutEntry": {
      "type": "object",
      "description": "The PAT and the PMTs in effect over [offset_bytes, end_offset_bytes).",
      "required": [
        "offset_bytes",
        "end_offset_bytes",
        "transition_indices",
        "pat_version",
        "pat_offset_bytes",
        "programs"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "First packet the layout applies to. The first layout starts at the first packet of the range, before its tables were complete; pat_offset_bytes and pmt_offset_bytes say where they were."
        },
        "end_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset where the next layout starts or the scan ended."
        },
        "transition_indices": {
          "type": "array",
          "description": "Indices into psi_transitions of the changes that start this layout, all completed by the packet before offset_bytes. Empty for the first layout.",
          "items": {
            "type": "integer",
            "minimum": 0
          }
        },
        "pat_version": {
          "type": "integer",
          "minimum": 0,
          "maximum": 31
        },
        "pat_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset of the packet that completed the PAT in effect. For the first layout, the first complete PAT of the range."
        },
        "programs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/layoutProgram"
          }
        }
      }
    },
    "layoutProgram": {
      "type": "object",
      "description": "A program of the PAT in effect. pmt_version, pmt_offset_bytes and pcr_pid are absent, and elementary_streams is empty, when no PMT of the program is known: its PMT was not found, or the program was added by a later PAT and is not analyzed.",
      "required": [
        "program_number",
        "pmt_pid",
        "elementary_streams"
      ],
      "properties": {
        "program_number": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "pmt_pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "pmt_version": {
          "type": "integer",
          "minimum": 0,
          "maximum": 31
        },
        "pmt_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger",
          "description": "Offset of the packet that completed the PMT in effect. For the first layout, where discovery completed the PMT the analysis started from; discovery reads a PMT only after the first complete PAT, so an earlier copy can appear in sections."
        },
        "pcr_pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "elementary_streams": {
          "type": "array",
          "items": {
            "$ref": "json-schema-v2.json#/$defs/elementaryStream"
          }
        }
      }
    },
    "observedPid": {
      "type": "object",
      "required": [
        "pid",
        "packet_count",
        "first_offset_bytes",
        "last_offset_bytes",
        "roles"
      ],
      "properties": {
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "packet_count": {
          "type": "integer",
          "minimum": 1
        },
        "first_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "last_offset_bytes": {
          "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
        },
        "roles": {
          "type": "array",
          "minItems": 1,
          "description": "What the PID carries in any of the layouts, each role once. A PID no layout lists has the single role unannounced.",
          "items": {
            "$ref": "#/$defs/pidRole"
          }
        }
      }
    },
    "pidRole": {
      "type": "object",
      "required": [
        "kind"
      ],
      "properties": {
        "kind": {
          "$ref": "json-schema-v2.json#/$defs/status",
          "description": "Current values: pat, pmt, pcr, elementary_stream, null, unannounced."
        },
        "program_number": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535,
          "description": "For pmt, pcr and elementary_stream."
        },
        "stream_type": {
          "$ref": "json-schema-v2.json#/$defs/streamType",
          "description": "For elementary_stream."
        },
        "stream_type_description": {
          "type": "string",
          "description": "For elementary_stream."
        }
      }
    }
  }
}
