{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://small-teton.github.io/mpeg-ts-analyzer/json-schema-packets-v1.json",
  "title": "mpeg-ts-analyzer packet documents v1",
  "description": "Schema for --format json --output packets (a page of packets) and --output packet (one packet decoded). 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 1.",
  "type": "object",
  "required": [
    "schema_version",
    "output",
    "input",
    "status",
    "warnings"
  ],
  "properties": {
    "schema_version": {
      "const": "1.0"
    },
    "output": {
      "enum": [
        "packets",
        "packet"
      ]
    },
    "input": {
      "$ref": "#/$defs/input"
    },
    "status": {
      "$ref": "json-schema-v2.json#/$defs/status",
      "description": "Current values: complete, failed."
    },
    "reason": {
      "$ref": "json-schema-v2.json#/$defs/reasonCode"
    },
    "error": {
      "type": "string",
      "description": "Human-readable error detail; not a stable matching interface."
    },
    "warnings": {
      "type": "array",
      "items": {
        "$ref": "json-schema-v2.json#/$defs/warning"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "output": {
            "const": "packets"
          }
        }
      },
      "then": {
        "$ref": "#/$defs/page"
      },
      "else": {
        "$ref": "#/$defs/detail"
      }
    }
  ],
  "$defs": {
    "offset": {
      "$ref": "json-schema-v2.json#/$defs/nonNegativeInteger"
    },
    "input": {
      "type": "object",
      "required": [
        "path",
        "file_size_bytes",
        "offset_bytes"
      ],
      "properties": {
        "path": {
          "type": "string"
        },
        "file_size_bytes": {
          "$ref": "#/$defs/offset"
        },
        "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": {
          "type": "integer",
          "description": "The requested --offset."
        }
      }
    },
    "page": {
      "type": "object",
      "required": [
        "end_of_file",
        "total_packets",
        "packets"
      ],
      "properties": {
        "first_offset_bytes": {
          "$ref": "#/$defs/offset",
          "description": "First packet boundary at or after --offset."
        },
        "next_offset_bytes": {
          "$ref": "#/$defs/offset",
          "description": "Offset to request for the following page; absent at the end of the file."
        },
        "end_of_file": {
          "type": "boolean"
        },
        "total_packets": {
          "$ref": "#/$defs/offset",
          "description": "Complete packets in the file."
        },
        "packets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/packetSummary"
          }
        }
      }
    },
    "packetSummary": {
      "type": "object",
      "required": [
        "offset_bytes",
        "valid",
        "pusi",
        "transport_error",
        "scrambling_control",
        "adaptation_field_control",
        "continuity_counter"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "#/$defs/offset"
        },
        "valid": {
          "type": "boolean",
          "description": "False when the packet has no sync byte, or has no transport_error_indicator and a header or adaptation field that cannot be parsed (for example a reserved adaptation_field_control); the other fields are then absent or zero. A packet with transport_error_indicator set and a sync byte is always valid."
        },
        "pid": {
          "$ref": "json-schema-v2.json#/$defs/pid"
        },
        "pusi": {
          "type": "boolean"
        },
        "transport_error": {
          "type": "boolean"
        },
        "scrambling_control": {
          "type": "integer",
          "minimum": 0,
          "maximum": 3
        },
        "adaptation_field_control": {
          "type": "integer",
          "minimum": 0,
          "maximum": 3
        },
        "continuity_counter": {
          "type": "integer",
          "minimum": 0,
          "maximum": 15
        },
        "pcr_ms": {
          "type": "number",
          "minimum": 0,
          "description": "PCR of the adaptation field, in milliseconds. For a packet with transport_error, read from the packet's bytes when PCR_flag is set and adaptation_field_length can hold it, without checking the rest of the field; the analysis does not use that PCR."
        },
        "sections": {
          "type": "array",
          "minItems": 1,
          "description": "Long-form sections (section_syntax_indicator 1) that start in this packet, in order, read from the packet's own bytes only: after the pointer_field of a payload_unit_start packet, each section whose 8-byte header fits in the packet and whose section_length is at least 9, until stuffing (0xFF), a short-form section, a shorter section_length or the end of the packet. Absent for other packets, including packets with transport_error or a scrambled payload. Nothing beyond the header is checked (the CRC32 may lie in a later packet); the psi_layout.sections entry that the PID, header and offset match in --output analysis gives the copy's status.",
          "items": {
            "$ref": "#/$defs/packetSection"
          }
        }
      }
    },
    "packetSection": {
      "type": "object",
      "required": [
        "table_id",
        "table_id_extension",
        "version_number",
        "current_next_indicator",
        "section_number",
        "last_section_number"
      ],
      "properties": {
        "table_id": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{2}$"
        },
        "table_id_extension": {
          "type": "integer",
          "minimum": 0,
          "maximum": 65535,
          "description": "transport_stream_id for a PAT, program_number for a PMT."
        },
        "version_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 31
        },
        "current_next_indicator": {
          "enum": [
            0,
            1
          ]
        },
        "section_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 255
        },
        "last_section_number": {
          "type": "integer",
          "minimum": 0,
          "maximum": 255
        }
      }
    },
    "detail": {
      "type": "object",
      "properties": {
        "packet": {
          "$ref": "#/$defs/decodedPacket"
        },
        "unit": {
          "$ref": "#/$defs/unit"
        }
      }
    },
    "decodedPacket": {
      "type": "object",
      "required": [
        "offset_bytes",
        "bytes",
        "layers"
      ],
      "properties": {
        "offset_bytes": {
          "$ref": "#/$defs/offset"
        },
        "bytes": {
          "type": "string",
          "pattern": "^([0-9a-f]{376}|[0-9a-f]{384}|[0-9a-f]{408})$",
          "description": "The 188, 192 or 204 physical packet bytes in hex, including an M2TS header or a 204-byte packet's trailer."
        },
        "layers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/layer"
          }
        }
      }
    },
    "layer": {
      "type": "object",
      "required": [
        "name",
        "fields"
      ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Current values: m2ts_header, ts_header, adaptation_field, payload, pes_header, pat, pmt, psi_section, trailer (the 16 raw trailer bytes of a 204-byte packet, after the TS packet; not decoded)."
        },
        "fields": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/field"
          }
        },
        "rows": {
          "type": "array",
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/field"
            }
          }
        },
        "truncated": {
          "type": "boolean",
          "description": "The structure continues past this packet; fields beyond it are not listed."
        },
        "bit_layout": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/layoutPart"
          },
          "description": "The wire layout of the structure within this packet: ordered, non-overlapping parts that together cover every bit of the structure up to its end or the end of the packet, including reserved, marker, prefix, stuffing and undecoded bits. Optional: absent from a CLI that predates it."
        }
      }
    },
    "layoutPart": {
      "type": "object",
      "required": [
        "id",
        "label",
        "kind",
        "bits",
        "value"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9_]+(\\.[a-z0-9_\\[\\]]+)*@[0-9]+$",
          "description": "Unique within the packet and stable for the same bytes: the layer, the structure path (for example pmt.stream[1].elementary_pid) and @ the first bit offset. Several parts may share a label such as reserved; the id tells them apart."
        },
        "label": {
          "type": "string",
          "description": "The syntax element name, for example reserved, marker_bit, PTS[29..15], program_clock_reference_base."
        },
        "kind": {
          "type": "string",
          "pattern": "^[a-z_]+$",
          "description": "Current values: value (a value or field), flag (a 1-bit indicator), reserved, marker (marker_bit and other fixed bits), prefix (sync_byte, start codes, the PTS/DTS prefix and the '10' of the optional PES header), stuffing, payload (data carried for another structure), unknown (bits this decoder does not interpret, or that are cut off). Consumers should treat an unrecognized kind as unknown."
        },
        "bits": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/bitRange"
          },
          "description": "The bits of the part, in wire order; packet-relative like field bits."
        },
        "value": {
          "type": "string",
          "description": "The bits as read: the linked field's value when the part has exactly its bits, otherwise binary digits for short fixed or value runs, a number, or a byte count."
        },
        "expected": {
          "type": "string",
          "description": "Present only where the specification fixes the bits and this decoder checks them, in the same notation as value (for stuffing, the value of every byte)."
        },
        "valid": {
          "type": "boolean",
          "description": "Present with expected: whether the bits are the expected ones."
        },
        "rule": {
          "type": "string",
          "description": "Present with expected: the specification rule that fixes the bits."
        },
        "field": {
          "type": "string",
          "description": "The decoded field (in fields, or in rows[row] when row is present) whose bytes hold the part. Several parts can share a field, such as the prefix, value runs and marker bits of a PTS."
        },
        "row": {
          "type": "integer",
          "minimum": 0,
          "description": "Index into rows of the table entry (PAT program, PMT stream) the part belongs to."
        }
      }
    },
    "field": {
      "type": "object",
      "required": [
        "name",
        "value",
        "byte_offset",
        "byte_length"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "value": {
          "type": "string"
        },
        "byte_offset": {
          "type": "integer",
          "minimum": 0,
          "maximum": 203,
          "description": "First byte of the field within bytes."
        },
        "byte_length": {
          "type": "integer",
          "minimum": 1,
          "maximum": 204
        },
        "bits": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/bitRange"
          },
          "description": "The bits the value was read from, in wire order, when they do not fill the byte span: for example a 13-bit PID, or a PTS whose value bits are separated by marker bits. Absent when the value uses every bit of byte_offset/byte_length."
        }
      }
    },
    "bitRange": {
      "type": "object",
      "required": [
        "bit_offset",
        "bit_length"
      ],
      "properties": {
        "bit_offset": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1535,
          "description": "Packet-relative bit offset: 0 is the most significant bit (bit 7) of the first byte of bytes, so bit 7 of byte i is i*8 and bit 0 is i*8+7."
        },
        "bit_length": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1536
        }
      }
    },
    "unit": {
      "type": "object",
      "required": [
        "kind",
        "position",
        "packets_in_range"
      ],
      "properties": {
        "kind": {
          "type": "string",
          "description": "Current values: pes, psi, unknown (started before the range), none (null packet or a packet without payload)."
        },
        "first_offset_bytes": {
          "$ref": "#/$defs/offset"
        },
        "started_before_range": {
          "type": "boolean"
        },
        "position": {
          "type": "integer",
          "minimum": 0,
          "description": "1-based position of the packet among the unit's packets in the range."
        },
        "packets_in_range": {
          "type": "integer",
          "minimum": 0
        },
        "previous_offset_bytes": {
          "$ref": "#/$defs/offset"
        },
        "next_offset_bytes": {
          "$ref": "#/$defs/offset"
        },
        "stream_id": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{2}$"
        },
        "pts_ms": {
          "type": "number",
          "minimum": 0
        },
        "dts_ms": {
          "type": "number",
          "minimum": 0
        },
        "table_id": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{2}$"
        },
        "header_packet_offsets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/offset"
          }
        }
      }
    }
  }
}
