<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Masahiko Matsuura</title>
    <description>The latest articles on DEV Community by Masahiko Matsuura (@small-teton).</description>
    <link>https://dev.to/small-teton</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4282021%2F1be873d9-1bea-4c6e-aa95-31aa714e4362.jpg</url>
      <title>DEV Community: Masahiko Matsuura</title>
      <link>https://dev.to/small-teton</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/small-teton"/>
    <language>en</language>
    <item>
      <title>Inspecting MPEG-TS packets bit by bit: a side project I started to debug timestamps</title>
      <dc:creator>Masahiko Matsuura</dc:creator>
      <pubDate>Fri, 04 Dec 2026 08:44:23 +0000</pubDate>
      <link>https://dev.to/small-teton/inspecting-mpeg-ts-packets-bit-by-bit-a-side-project-i-started-to-debug-timestamps-3a21</link>
      <guid>https://dev.to/small-teton/inspecting-mpeg-ts-packets-bit-by-bit-a-side-project-i-started-to-debug-timestamps-3a21</guid>
      <description>&lt;p&gt;In this article I'd like to introduce &lt;strong&gt;mpeg-ts-analyzer&lt;/strong&gt;, an open-source tool I have been building to analyze MPEG-2 Transport Streams.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/small-teton/mpeg-ts-analyzer" rel="noopener noreferrer"&gt;mpeg-ts-analyzer on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what it looks like. First, the command-line tool:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5s18fe55sl301o3a3mv9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5s18fe55sl301o3a3mv9.gif" alt="Terminal demo of the CLI checks" width="720" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the desktop GUI:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0nyzeokqr9al6z43k3hz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0nyzeokqr9al6z43k3hz.gif" alt="The GUI: Overview, Timeline, Bitrate, Packets and Detailed inspection" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;I used to work at a video equipment manufacturer. There, we used a commercial library, MainConcept, to create streams. Depending on how we tuned its parameters, such as the VBV buffer settings, a stream could end up outside the MPEG-2 TS specification. To find out why, I had to understand MPEG-2 TS in detail and look closely inside the streams.&lt;/p&gt;

&lt;p&gt;Go was getting a lot of attention at that time. So in 2016 I started writing this tool, partly to learn Go as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: PCR, DTS and PTS
&lt;/h2&gt;

&lt;p&gt;An MPEG-2 TS cuts video and audio into small 188-byte packets and mixes them into one stream, at a fine enough grain that both keep arriving a little at a time. Timestamps then control when each frame is decoded and shown, using the PCR as the reference time. This is how video and audio play without drifting apart:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PCR&lt;/strong&gt; (Program Clock Reference): the reference time. It is a sample of the program clock, sent regularly so that the decoder can recover and synchronize its own clock.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DTS&lt;/strong&gt; (Decoding Time Stamp): the time, on that clock, when a frame must be decoded.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PTS&lt;/strong&gt; (Presentation Time Stamp): the time, on that clock, when a frame is shown or a piece of audio is played. Because video and audio follow the same clock, they stay in sync.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnvxsblew7x54k7f6pcji.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnvxsblew7x54k7f6pcji.png" alt="PCR, DTS and PTS in one picture" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem I had was the gap between the PCR and the DTS/PTS. When a frame's data arrives, the clock recovered from the PCR gives the time of its arrival. The frame's DTS (or its PTS, if it has no DTS) must not be more than 1000 ms after that time. For ordinary video and audio, the specification does not allow data to stay in the decoder's buffer any longer than that.&lt;/p&gt;

&lt;p&gt;Depending on the encoder settings, this gap sometimes went over 1000 ms. The decoder we were targeting was strict about this limit, so we had to keep this gap within 1000 ms.&lt;/p&gt;

&lt;p&gt;The picture below shows the two cases. It is simplified: the arrival of a frame is drawn as a single moment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4dh3o1n09ii4dff9r0s1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4dh3o1n09ii4dff9r0s1.png" alt="The gap between the PCR and the DTS" width="799" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The first version: a CLI and bit-level dumps
&lt;/h2&gt;

&lt;p&gt;The first version was a command-line tool. From the start, my goal was to get the largest gap between the PCR and the DTS/PTS in a stream. So the tool printed the max gap, and also the max PCR interval. To see where a large gap came from, it could also print each PCR and DTS (or PTS) value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x000034e0 PCR: 0x018344a0[00940.000000ms]
0x000034e0 DTS: 0x00024090[01640.000000ms] (pid:0x100) (delay:700.000000ms)
0x0000359c PTS: 0x00023a3a[01621.977778ms] (pid:0x101) (delay:681.977778ms)
0x00004970 PCR: 0x01a43a20[01020.000000ms] (Interval:00080.000000ms)
0x00004970 DTS: 0x00025cb0[01720.000000ms] (pid:0x100) (delay:700.000000ms)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"delay" is the gap in the picture: how far the DTS or PTS is ahead of the PCR clock when the data arrives. "Interval" is the time from the previous PCR.&lt;/p&gt;

&lt;p&gt;Later, the CLI came to show the maximum values with a pass/fail verdict, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compliance Check Results:
Max PCR interval: 80.000000ms [OK, limit: &amp;lt;= 100.000000ms]
PCR discontinuities: 0 undeclared in 60 PCRs [OK]
PCR-PTS/DTS max gap: 726.666667ms [OK, limit: &amp;lt;= 1000.000000ms]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"PCR-PTS/DTS max gap" checks the gap in the picture against the 1000 ms limit, and "Max PCR interval" checks that consecutive PCR values are no more than 100 ms apart. So I can see at once whether the stream is within the limits.&lt;/p&gt;

&lt;p&gt;I wrote the bit-level parser myself. Fields in MPEG-2 TS have odd sizes, like 1, 3, 13 or 33 bits. Many of them start in the middle of a byte and cross byte boundaries, and the parser often has to skip reserved bits. So I wrote a small bit reader that keeps a bit position, and reads a field in three parts: the head (the rest of the current byte), the middle (whole bytes) and the tail (the top bits of the last byte). Then it joins them into one value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbqzvhpw12xfuftiaafeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbqzvhpw12xfuftiaafeb.png" alt="Reading a field that crosses byte boundaries in three parts" width="799" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A timestamp is only correct if the parser that reads it is correct. To check my parser, I also added dump options that print the parsed result of each header, field by field, so that I could compare it with the bits of the packet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;===============================================================
 TS Header
===============================================================
transport_error_indicator       : 0
payload_unit_start_indicator    : 1
transport_priority              : 1
pid                             : 0x100
transport_scrambling_control    : 0
adaptation_field_control        : 1
continuity_counter              : 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was enough for my original goal, so for several years I did not change the tool much.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then AI came along
&lt;/h2&gt;

&lt;p&gt;Today I work as a web engineer, and I don't have much time for this project. But when AI-assisted development became popular, I found that I could finally do what I had wanted to do, in a small amount of time. Since I started using AI in 2026, the project has had more than 300 commits. In the seven years before that, it had about 60.&lt;/p&gt;

&lt;p&gt;I used Claude Code and Codex, and let them develop and review each other's work. Reference tools such as TSDuck already exist, so I compared new measurements with an independent implementation wherever I could, and confirmed that the results were valid. So I could move fast with AI without giving up on accuracy.&lt;/p&gt;

&lt;p&gt;Now the tool can check whether a stream follows the specification from many angles, not only the timestamps. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;continuity counters of every PID&lt;/li&gt;
&lt;li&gt;PAT/PMT repetition, CRC and version changes&lt;/li&gt;
&lt;li&gt;PIDs referenced by the PAT or PMT but missing from the stream, and PIDs that are not referenced&lt;/li&gt;
&lt;li&gt;PES headers and PTS repetition&lt;/li&gt;
&lt;li&gt;sync loss, transport errors and scrambled packets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A GUI would clearly make all of this easier to see. But building one takes a lot of work, and I did not have enough free time outside my job, so I had given up on it. With AI, I could build it in a short time, in Rust with egui, an immediate-mode GUI library for Rust that runs on macOS, Windows and Linux. The GUI itself took only about one week of actual work.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it can do now
&lt;/h2&gt;

&lt;p&gt;The CLI shows all the checks listed above in one report. The same results are available as JSON with a published schema, so you can use it in CI. The old dumps are still there, too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bit-level inspection in a GUI
&lt;/h3&gt;

&lt;p&gt;The GUI runs the CLI and shows the result in four linked views: Overview, Timeline, Bitrate and Packets.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F55p53jlkf4gw8x179ilr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F55p53jlkf4gw8x179ilr.png" alt="The GUI with its Overview, Timeline, Bitrate and Packets views" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The part I like most is &lt;strong&gt;Detailed inspection&lt;/strong&gt;. It lays out one packet bit by bit, in the same way ISO/IEC 13818-1 draws its syntax tables. This is the view I wanted back when I was writing those field-by-field dumps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpq9r105w0v6vpgnnsjmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpq9r105w0v6vpgnnsjmk.png" alt="Detailed inspection of a PMT packet" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see every bit of the packet, including reserved bits, marker bits, prefixes, stuffing, and bytes the analyzer does not decode. So you can check the data side by side with the specification. I think this makes checking much easier while you are still learning the specification.&lt;/p&gt;

&lt;p&gt;For monitoring a whole service or deep conformance work, TSDuck is the better tool. This tool is for looking closely at a small part of a stream.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;I recommend starting with the GUI. There are installers and binaries for macOS, Windows and Linux on the &lt;a href="https://github.com/small-teton/mpeg-ts-analyzer/releases" rel="noopener noreferrer"&gt;releases page&lt;/a&gt;, and the CLI is available there as well. See the &lt;a href="https://small-teton.github.io/mpeg-ts-analyzer/getting-started/installation/" rel="noopener noreferrer"&gt;installation guide&lt;/a&gt; for the details. To learn how to use it, see the &lt;a href="https://small-teton.github.io/mpeg-ts-analyzer/gui/overview/" rel="noopener noreferrer"&gt;GUI guide&lt;/a&gt; and the &lt;a href="https://small-teton.github.io/mpeg-ts-analyzer/cli/usage/" rel="noopener noreferrer"&gt;CLI usage guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you do not have a TS file at hand, the repository has &lt;a href="https://github.com/small-teton/mpeg-ts-analyzer/tree/master/sample_data" rel="noopener noreferrer"&gt;sample streams&lt;/a&gt;, including some with intentional errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback welcome
&lt;/h2&gt;

&lt;p&gt;If this tool helps someone who has the same kind of problem I had, I will be happy. I would especially like to hear from people who work with transport streams. If you find a wrong result, or have an idea for a useful check or feature, please open an issue on &lt;a href="https://github.com/small-teton/mpeg-ts-analyzer/issues" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/small-teton/mpeg-ts-analyzer" rel="noopener noreferrer"&gt;mpeg-ts-analyzer on GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>go</category>
      <category>rust</category>
      <category>video</category>
    </item>
  </channel>
</rss>
