PR #26247 opened by jbuchbinder
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26247
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26247.patch

Adds a demuxer for `.asd` recordings written by the Dual-Vision (Rosco) 
hard-disk DVR / dashcam. Nothing could open these files before; the format is 
undocumented and was recovered from the recordings themselves.

## Format

A recording is an 84-byte file header followed by a flat chain of typed chunks. 
Each chunk carries the file offset of its predecessor, so the chain is 
self-describing and a truncated or corrupt file is detected while indexing 
rather than mid-decode.

| type   | content                                                    |
|--------|------------------------------------------------------------|
| 0xff01 | video: 26-byte header + one JPEG frame (Motion JPEG)        |
| 0xff03 | audio: 16-byte header + 8192-byte payload (2048 samples)    |
| 0xff00 | 22-byte trailer/telemetry record -  indexed and skipped      |
| 0xff02 | 94-byte GPS record with ASCII lat/lon - indexed and skipped |

Both chunk headers hold a timestamp as unix seconds plus microseconds. Packets 
take their PTS from that field with a `1/1000000` time base; nothing is derived 
from a frame counter, and `avg_frame_rate` is measured from count over span 
with `av_reduce()`.

A file holds two cameras alternating one frame per interval. The per-frame 
channel flag selects the stream: channel 1 is the front camera and is emitted 
as stream 0, channel 0 as stream 1. Audio is 8 kHz mono `pcm_s16le`, 256 ms per 
chunk.

Both the 16-bit samples and the recorder firmware vary: each sample sits in one 
half of a 32-bit word, and the word grid inside the audio chunk starts at 
either +18 or +20. That offset is not a property of the recorder; one recording 
here switches from +20 to +18 nine audio chunks in, so it is decided per chunk 
by counting which grid yields non-zero samples, carrying the previous decision 
through silent chunks. Reading at the wrong grid picks up padding and makes the 
file sound silent.

## Verification

Checked against all 11 recordings from the device (5.5–21 MB, 187 MB total),
each demuxed with the standalone build:

- every video frame decodes without error, and per-frame CRCs match an  
independent Python parse of the chunk chain frame for frame (both cameras);
- the demuxed audio matches the samples read from the container one for one, 
from 79,872 up to 454,656 samples per file, counts and stream metadata alone do 
not catch a two-byte sample offset, so this comparison is per sample;
- `-fps_mode passthrough` on the input when counting frames (default CFR output 
silently drops frames whose PTS do not fit a constant rate);
- negative cases: unrelated files (Matroska, bare JPEG) are not probed as  
`asd`; `-ss` seeks decode a frame without error.

No FATE test is included: the recordings come off a user's dashcam and are not 
distributable. The demuxer was validated with a local harness against the 
sample set above.

## Notes for review

- Registration is the two usual one-line edits (`libavformat/Makefile`, 
`libavformat/allformats.c`); `configure` needs no change since it derives the 
demuxer list by scanning the externs.
- The index walk is bounded (allocated growth, per-chunk size validated against 
the next offset, chain link must match) and returns `AVERROR_INVALIDDATA` 
instead of looping or reading past EOF.
- Audio is read into a buffer sized to the chunk and repacked inward; the 
packet is allocated at the output size before the 8192-byte payload is touched.
- Telemetry and GPS records are indexed only so the offsets are known; a 
follow-up can expose them as data streams without re-deriving the layout.
- `read_seek2` binary-searches the chunk index and honours 
`AVSEEK_FLAG_BACKWARD`.

## Commits

1. `avformat/asd: add Dual-Vision ASD dashcam demuxer` - format recovery, 
probe, index, packet and seek.
2. `avformat/asd: pick the audio sample word offset per chunk` - makes the 
sample-half decision per chunk after a recording was found switching grid 
mid-file; the first commit's per-file choice made half of that file silent.



From 07782ffda700324e7af78118e43c04264003abfa Mon Sep 17 00:00:00 2001
From: Jeffrey Buchbinder <[email protected]>
Date: Fri, 20 Nov 2026 18:47:48 -0500
Subject: [PATCH 1/2] avformat/asd: add Dual-Vision ASD dashcam demuxer

The Dual-Vision (Rosco) hard-disk DVR stores each recording as one .asd
file: an 84-byte header followed by a chain of typed chunks that are
linked backwards through a "previous chunk offset" field.

  0xff01  video, 26-byte header + one JPEG (Motion JPEG)
  0xff03  audio, 16-byte header + 2048 16-bit samples in 32-bit slots
  0xff00  telemetry record, skipped
  0xff02  GPS record, skipped

Both chunk headers carry a timestamp as unix seconds plus microseconds,
which becomes the packet PTS in a 1/1000000 time base.

A file holds two cameras, one frame each per interval: the per-frame
channel flag 1 is the front camera and is emitted as stream 0, channel 0
as stream 1. Audio is 8 kHz mono; the 16-bit sample occupies the low or
the high half of each 32-bit word depending on the recorder firmware, so
the used half is detected from the first audio chunk.

Verified against 11 recordings from the device (5.5-21 MB): every
demuxed frame decodes without error and matches an independent parse of
the chunk chain frame for frame, and the audio packet list matches
exactly.
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/asddec.c     | 422 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 libavformat/asddec.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 30e3b334411..a578edcbfbf 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -141,6 +141,7 @@ OBJS-$(CONFIG_ARGO_ASF_MUXER)            += argo_asf.o
 OBJS-$(CONFIG_ARGO_BRP_DEMUXER)          += argo_brp.o argo_asf.o
 OBJS-$(CONFIG_ARGO_CVG_DEMUXER)          += argo_cvg.o
 OBJS-$(CONFIG_ARGO_CVG_MUXER)            += argo_cvg.o
+OBJS-$(CONFIG_ASD_DEMUXER)               += asddec.o
 OBJS-$(CONFIG_ASF_DEMUXER)               += asfdec_f.o asf.o asfcrypt.o \
                                             asf_tags.o
 OBJS-$(CONFIG_ASF_MUXER)                 += asfenc.o asf.o asf_tags.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index db5656c7dbc..89ae773f69b 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -80,6 +80,7 @@ extern const FFOutputFormat ff_argo_asf_muxer;
 extern const FFInputFormat  ff_argo_brp_demuxer;
 extern const FFInputFormat  ff_argo_cvg_demuxer;
 extern const FFOutputFormat ff_argo_cvg_muxer;
+extern const FFInputFormat  ff_asd_demuxer;
 extern const FFInputFormat  ff_asf_demuxer;
 extern const FFOutputFormat ff_asf_muxer;
 extern const FFInputFormat  ff_ass_demuxer;
diff --git a/libavformat/asddec.c b/libavformat/asddec.c
new file mode 100644
index 00000000000..9dce5fc0e68
--- /dev/null
+++ b/libavformat/asddec.c
@@ -0,0 +1,422 @@
+/*
+ * Dual-Vision "ASD" dashcam recorder demuxer
+ *
+ * The Dual-Vision (Rosco) hard-disk DVR stores each recording as a single
+ * .asd file: an 84-byte file header followed by a chain of typed chunks that
+ * are linked backwards through a "previous chunk offset" field.  Chunk types
+ * seen in the wild:
+ *
+ *   0xff01  video, 26-byte header + JPEG payload (Motion JPEG, one frame)
+ *   0xff03  audio, 16-byte header + 8192-byte payload (2048 16-bit samples)
+ *   0xff00  22-byte trailer/telemetry record (skipped)
+ *   0xff02  94-byte GPS record with ASCII latitude/longitude (skipped)
+ *
+ * Video chunk header:
+ *
+ *   +0  u16  type (0xff01)          +10 u16  camera channel (1 / 0)
+ *   +2  u32  previous chunk offset  +14 u32  payload size
+ *   +6  u16  width                  +18 u32  timestamp, seconds
+ *   +8  u16  height                 +22 u32  timestamp, microseconds
+ *
+ * Audio chunk header:
+ *
+ *   +0  u16  type (0xff03)          +10 u32  timestamp, microseconds
+ *   +2  u32  previous chunk offset  +14 u16  payload size (0x2000)
+ *   +6  u32  timestamp, seconds     +16 2 bytes of padding
+ *
+ * The two cameras alternate one frame each, so a file holds two independent
+ * 640x480 MJPEG streams.  The audio payload holds one 16-bit sample per 32-bit
+ * word; which half of the word carries the sample depends on the recorder
+ * firmware, so it is detected from the first audio chunk instead of assumed.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
+#include "libavutil/avutil.h"
+#include "avformat.h"
+#include "demux.h"
+#include "internal.h"
+
+#define ASD_FILE_HEADER_SIZE   0x54
+#define ASD_VIDEO_HEADER_SIZE  26
+#define ASD_AUDIO_HEADER_SIZE  16
+#define ASD_AUDIO_PAYLOAD_SIZE 8192
+#define ASD_AUDIO_SAMPLES      (ASD_AUDIO_PAYLOAD_SIZE / 4)
+#define ASD_AUDIO_CHUNK_SIZE   (ASD_AUDIO_HEADER_SIZE + ASD_AUDIO_PAYLOAD_SIZE 
+ 2)
+#define ASD_META_SIZE          22
+#define ASD_GPS_SIZE           94
+
+#define ASD_AUDIO_RATE         8000
+#define ASD_AUDIO_DURATION     (int64_t)(ASD_AUDIO_SAMPLES * AV_TIME_BASE / 
ASD_AUDIO_RATE)
+
+enum ASDChunkType {
+    ASD_CHUNK_META  = 0xff00,
+    ASD_CHUNK_VIDEO = 0xff01,
+    ASD_CHUNK_GPS   = 0xff02,
+    ASD_CHUNK_AUDIO = 0xff03,
+};
+
+enum ASDStream {
+    ASD_STREAM_CAM_A = 0,
+    ASD_STREAM_CAM_B = 1,
+    ASD_STREAM_AUDIO = 2,
+    ASD_STREAM_NB,
+};
+
+typedef struct ASDChunk {
+    int64_t  pos;
+    int64_t  pts;
+    uint32_t size;
+    uint16_t type;
+    int      stream;
+} ASDChunk;
+
+typedef struct ASDStreamStats {
+    int64_t first_pts;
+    int64_t last_pts;
+    int     nb_frames;
+} ASDStreamStats;
+
+typedef struct ASDContext {
+    ASDChunk       *chunks;
+    unsigned        nb_chunks;
+    unsigned        cur_chunk;
+    ASDStreamStats  stats[ASD_STREAM_NB];
+    int             sample_shift;   /* 2 when the sample is the high half of 
the word */
+    int             width, height;
+} ASDContext;
+
+static int asd_probe(const AVProbeData *p)
+{
+    if (p->buf_size < ASD_FILE_HEADER_SIZE)
+        return 0;
+
+    /* version 0x0811, followed by a constant 0x00030000 */
+    if (AV_RL32(p->buf) != 0x00000811 || AV_RL32(p->buf + 4) != 0x00030000)
+        return 0;
+
+    switch (AV_RL16(p->buf + ASD_FILE_HEADER_SIZE)) {
+    case ASD_CHUNK_META:
+    case ASD_CHUNK_VIDEO:
+    case ASD_CHUNK_GPS:
+    case ASD_CHUNK_AUDIO:
+        return AVPROBE_SCORE_MAX;
+    }
+
+    return 0;
+}
+
+/* Called with the read position just past the type and previous-offset 
fields. */
+static int asd_chunk_size(AVIOContext *pb, uint16_t type, uint32_t *size)
+{
+    switch (type) {
+    case ASD_CHUNK_VIDEO:
+        avio_skip(pb, 8);
+        *size = ASD_VIDEO_HEADER_SIZE + avio_rl32(pb);
+        return 0;
+    case ASD_CHUNK_AUDIO:
+        *size = ASD_AUDIO_CHUNK_SIZE;
+        return 0;
+    case ASD_CHUNK_META:
+        *size = ASD_META_SIZE;
+        return 0;
+    case ASD_CHUNK_GPS:
+        *size = ASD_GPS_SIZE;
+        return 0;
+    }
+
+    return AVERROR_INVALIDDATA;
+}
+
+static void asd_add_stats(ASDContext *c, int stream, int64_t pts)
+{
+    ASDStreamStats *stats = &c->stats[stream];
+
+    if (!stats->nb_frames)
+        stats->first_pts = pts;
+    stats->last_pts = pts;
+    stats->nb_frames++;
+}
+
+/* Walk the chunk chain and index every video and audio chunk. */
+static int asd_build_index(AVFormatContext *s)
+{
+    ASDContext *c = s->priv_data;
+    AVIOContext *pb = s->pb;
+    unsigned nb_alloc = 0;
+    int64_t pos = ASD_FILE_HEADER_SIZE, prev = 0;
+    int ret;
+
+    for (;;) {
+        uint16_t type;
+        uint32_t size, link;
+
+        if (avio_feof(pb))
+            break;
+
+        if ((ret = avio_seek(pb, pos, SEEK_SET)) < 0)
+            return ret;
+
+        type = avio_rl16(pb);
+        link = avio_rl32(pb);
+
+        if (type != ASD_CHUNK_META && type != ASD_CHUNK_VIDEO &&
+            type != ASD_CHUNK_GPS   && type != ASD_CHUNK_AUDIO)
+            break;
+
+        /* The chain is self-describing: every chunk points at its 
predecessor. */
+        if (pos != ASD_FILE_HEADER_SIZE && link != prev)
+            break;
+
+        if ((ret = asd_chunk_size(pb, type, &size)) < 0)
+            return ret;
+        if (avio_tell(pb) > pos + size)
+            return AVERROR_INVALIDDATA;
+
+        if (type == ASD_CHUNK_VIDEO || type == ASD_CHUNK_AUDIO) {
+            ASDChunk *chunk;
+
+            if (c->nb_chunks == nb_alloc) {
+                nb_alloc = nb_alloc ? nb_alloc * 2 : 256;
+                chunk = av_realloc_array(c->chunks, nb_alloc, sizeof(*chunk));
+                if (!chunk)
+                    return AVERROR(ENOMEM);
+                c->chunks = chunk;
+            }
+
+            chunk = &c->chunks[c->nb_chunks++];
+            chunk->pos  = pos;
+            chunk->size = size;
+            chunk->type = type;
+
+            if (type == ASD_CHUNK_VIDEO) {
+                unsigned channel, w, h;
+
+                avio_seek(pb, pos + 6, SEEK_SET);
+                w       = avio_rl16(pb);
+                h       = avio_rl16(pb);
+                channel = avio_rl16(pb);
+                /* +12 zero, +14 payload size (already consumed) */
+                avio_skip(pb, 6);
+                chunk->pts = (int64_t)avio_rl32(pb) * AV_TIME_BASE + 
avio_rl32(pb);
+                /* channel 1 is the front camera; it is emitted as stream 0 */
+                chunk->stream = channel & 1 ? ASD_STREAM_CAM_A : 
ASD_STREAM_CAM_B;
+
+                if (!c->width) {
+                    c->width  = w;
+                    c->height = h;
+                } else if (c->width != w || c->height != h) {
+                    av_log(s, AV_LOG_WARNING,
+                           "resolution change %dx%d -> %ux%u at %"PRId64"\n",
+                           c->width, c->height, w, h, pos);
+                    c->width  = w;
+                    c->height = h;
+                }
+            } else {
+                avio_seek(pb, pos + 6, SEEK_SET);
+                chunk->pts    = (int64_t)avio_rl32(pb) * AV_TIME_BASE + 
avio_rl32(pb);
+                chunk->stream = ASD_STREAM_AUDIO;
+            }
+
+            asd_add_stats(c, chunk->stream, chunk->pts);
+        }
+
+        prev = pos;
+        pos += size;
+    }
+
+    if (!c->nb_chunks || !c->stats[ASD_STREAM_AUDIO].nb_frames) {
+        av_log(s, AV_LOG_ERROR, "no chunks found\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    return 0;
+}
+
+/* The 16-bit sample sits in the low or the high half of each 32-bit word. */
+static void asd_detect_sample_shift(AVFormatContext *s)
+{
+    ASDContext *c = s->priv_data;
+    uint8_t buf[256];
+
+    c->sample_shift = 0;
+
+    for (unsigned i = 0; i < c->nb_chunks; i++) {
+        unsigned low = 0, high = 0;
+
+        if (c->chunks[i].type != ASD_CHUNK_AUDIO)
+            continue;
+
+        if (avio_seek(s->pb, c->chunks[i].pos + ASD_AUDIO_HEADER_SIZE, 
SEEK_SET) < 0)
+            break;
+        if (avio_read(s->pb, buf, sizeof(buf)) != sizeof(buf))
+            break;
+
+        for (unsigned j = 0; j < sizeof(buf) / 4; j++) {
+            low  += !!AV_RL16(buf + 4 * j);
+            high += !!AV_RL16(buf + 4 * j + 2);
+        }
+
+        c->sample_shift = high > low ? 2 : 0;
+        return;
+    }
+}
+
+static int asd_read_header(AVFormatContext *s)
+{
+    ASDContext *c = s->priv_data;
+    AVIOContext *pb = s->pb;
+    AVStream *st;
+    int ret;
+
+    avio_skip(pb, ASD_FILE_HEADER_SIZE);
+
+    if ((ret = asd_build_index(s)) < 0)
+        return ret;
+
+    asd_detect_sample_shift(s);
+
+    for (int i = ASD_STREAM_CAM_A; i <= ASD_STREAM_CAM_B; i++) {
+        const ASDStreamStats *stats = &c->stats[i];
+
+        st = avformat_new_stream(s, NULL);
+        if (!st)
+            return AVERROR(ENOMEM);
+        st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+        st->codecpar->codec_id   = AV_CODEC_ID_MJPEG;
+        st->codecpar->width      = c->width;
+        st->codecpar->height     = c->height;
+        avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE);
+
+        if (stats->nb_frames > 1) {
+            int64_t span = stats->last_pts - stats->first_pts;
+
+            if (span > 0)
+                av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
+                          (int64_t)stats->nb_frames * AV_TIME_BASE, span, 
INT_MAX);
+        }
+        st->r_frame_rate = st->avg_frame_rate;
+        st->start_time   = stats->nb_frames ? stats->first_pts : 0;
+        av_dict_set(&st->metadata, "title",
+                    i == ASD_STREAM_CAM_A ? "front" : "rear", 0);
+    }
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id    = AV_CODEC_ID_PCM_S16LE;
+    st->codecpar->sample_rate = ASD_AUDIO_RATE;
+    st->codecpar->block_align = 2;
+    st->codecpar->bit_rate    = ASD_AUDIO_RATE * 16;
+    av_channel_layout_copy(&st->codecpar->ch_layout, 
&(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO);
+    avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE);
+    st->start_time = c->stats[ASD_STREAM_AUDIO].first_pts;
+    av_dict_set(&st->metadata, "title", "audio", 0);
+
+    s->start_time = c->chunks[0].pts;
+    s->duration   = c->chunks[c->nb_chunks - 1].pts - c->chunks[0].pts;
+
+    return 0;
+}
+
+static int asd_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    ASDContext *c = s->priv_data;
+    AVIOContext *pb = s->pb;
+    ASDChunk *chunk;
+    int ret;
+
+    if (c->cur_chunk >= c->nb_chunks)
+        return AVERROR_EOF;
+
+    chunk = &c->chunks[c->cur_chunk++];
+
+    if ((ret = avio_seek(pb, chunk->pos, SEEK_SET)) < 0)
+        return ret;
+
+    if (chunk->type == ASD_CHUNK_VIDEO) {
+        avio_skip(pb, ASD_VIDEO_HEADER_SIZE);
+        if ((ret = av_get_packet(pb, pkt, chunk->size - 
ASD_VIDEO_HEADER_SIZE)) < 0)
+            return ret;
+        pkt->duration = 0;
+    } else {
+        uint8_t payload[ASD_AUDIO_PAYLOAD_SIZE];
+        unsigned i;
+
+        avio_skip(pb, ASD_AUDIO_HEADER_SIZE);
+        if (avio_read(pb, payload, ASD_AUDIO_PAYLOAD_SIZE) != 
ASD_AUDIO_PAYLOAD_SIZE)
+            return AVERROR_INVALIDDATA;
+        if ((ret = av_new_packet(pkt, ASD_AUDIO_SAMPLES * 2)) < 0)
+            return ret;
+        /* keep the used half of each 32-bit word, drop the padding half */
+        for (i = 0; i < ASD_AUDIO_SAMPLES; i++)
+            AV_WL16(pkt->data + 2 * i, AV_RL16(payload + 4 * i + 
c->sample_shift));
+
+        pkt->duration = ASD_AUDIO_DURATION;
+    }
+
+    pkt->stream_index = chunk->stream;
+    pkt->pts          = chunk->pts;
+    pkt->dts          = chunk->pts;
+
+    return 0;
+}
+
+static int asd_read_seek(AVFormatContext *s, int stream_index,
+                         int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
+{
+    ASDContext *c = s->priv_data;
+    unsigned lo = 0, hi = c->nb_chunks, target = c->nb_chunks;
+
+    if (ts < 0)
+        return AVERROR(EINVAL);
+
+    /* chunks are stored in file order; timestamps never go backwards */
+    while (lo < hi) {
+        unsigned mid = lo + (hi - lo) / 2;
+
+        if (c->chunks[mid].pts < ts)
+            lo = mid + 1;
+        else {
+            target = mid;
+            hi = mid;
+        }
+    }
+
+    if (target >= c->nb_chunks) {
+        if (!(flags & AVSEEK_FLAG_BACKWARD))
+            return AVERROR(EINVAL);
+        target = c->nb_chunks - 1;
+    }
+
+    c->cur_chunk = target;
+    return 0;
+}
+
+const FFInputFormat ff_asd_demuxer = {
+    .p.name         = "asd",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("Dual-Vision ASD dashcam 
recording"),
+    .p.extensions   = "asd",
+    .priv_data_size = sizeof(ASDContext),
+    .read_probe     = asd_probe,
+    .read_header    = asd_read_header,
+    .read_packet    = asd_read_packet,
+    .read_seek2     = asd_read_seek,
+};
-- 
2.54.1


From 2406d33d3012d87a566f113e7402b28b756373dc Mon Sep 17 00:00:00 2001
From: Jeffrey Buchbinder <[email protected]>
Date: Fri, 20 Nov 2026 18:51:11 -0500
Subject: [PATCH 2/2] avformat/asd: pick the audio sample word offset per chunk

The 16-bit samples are the low half of 4-byte words, but the grid those
words start on inside the audio chunk is either +18 or +20, and it is
not a property of the recorder: one recording here uses +20 for its
first nine audio chunks and +18 for the remaining 213.

Decide the offset per audio chunk by counting which grid yields
non-zero samples, keeping the previous choice when the chunk is silent.
Reading at the wrong grid returns padding bytes and makes the file sound
silent, which is exactly how this was found.

Verified on all 11 recordings: the demuxed audio now matches the samples
read from the container one for one (79,872-454,656 samples per file),
and per-frame video CRCs still match an independent parse of the chunk
chain.
---
 libavformat/asddec.c | 57 ++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 31 deletions(-)

diff --git a/libavformat/asddec.c b/libavformat/asddec.c
index 9dce5fc0e68..bb62d0450c2 100644
--- a/libavformat/asddec.c
+++ b/libavformat/asddec.c
@@ -62,6 +62,12 @@
 #define ASD_META_SIZE          22
 #define ASD_GPS_SIZE           94
 
+/* the 16-bit samples are the low half of 4-byte words; this is the offset of
+ * that word grid, relative to the payload buffer (chunk + 16), which is 2 or 4
+ * depending on the recording */
+#define ASD_AUDIO_SAMPLE_OFF_A 2
+#define ASD_AUDIO_SAMPLE_OFF_B 4
+
 #define ASD_AUDIO_RATE         8000
 #define ASD_AUDIO_DURATION     (int64_t)(ASD_AUDIO_SAMPLES * AV_TIME_BASE / 
ASD_AUDIO_RATE)
 
@@ -98,7 +104,7 @@ typedef struct ASDContext {
     unsigned        nb_chunks;
     unsigned        cur_chunk;
     ASDStreamStats  stats[ASD_STREAM_NB];
-    int             sample_shift;   /* 2 when the sample is the high half of 
the word */
+    int             sample_off;     /* payload offset of the sample word grid 
*/
     int             width, height;
 } ASDContext;
 
@@ -249,33 +255,20 @@ static int asd_build_index(AVFormatContext *s)
     return 0;
 }
 
-/* The 16-bit sample sits in the low or the high half of each 32-bit word. */
-static void asd_detect_sample_shift(AVFormatContext *s)
+/* Pick the word grid whose low halves look like samples.  A silent chunk 
cannot
+ * tell the two offsets apart, so the last decided one is kept. */
+static int asd_pick_sample_off(ASDContext *c, const uint8_t *payload)
 {
-    ASDContext *c = s->priv_data;
-    uint8_t buf[256];
+    unsigned nonzero[2] = { 0, 0 };
+    const unsigned offs[2] = { ASD_AUDIO_SAMPLE_OFF_A, ASD_AUDIO_SAMPLE_OFF_B 
};
 
-    c->sample_shift = 0;
+    for (int b = 0; b < 2; b++)
+        for (unsigned j = 0; j < ASD_AUDIO_SAMPLES; j++)
+            nonzero[b] += !!AV_RL16(payload + offs[b] + 4 * j);
 
-    for (unsigned i = 0; i < c->nb_chunks; i++) {
-        unsigned low = 0, high = 0;
-
-        if (c->chunks[i].type != ASD_CHUNK_AUDIO)
-            continue;
-
-        if (avio_seek(s->pb, c->chunks[i].pos + ASD_AUDIO_HEADER_SIZE, 
SEEK_SET) < 0)
-            break;
-        if (avio_read(s->pb, buf, sizeof(buf)) != sizeof(buf))
-            break;
-
-        for (unsigned j = 0; j < sizeof(buf) / 4; j++) {
-            low  += !!AV_RL16(buf + 4 * j);
-            high += !!AV_RL16(buf + 4 * j + 2);
-        }
-
-        c->sample_shift = high > low ? 2 : 0;
-        return;
-    }
+    if (nonzero[0] == nonzero[1])
+        return c->sample_off;
+    return nonzero[1] > nonzero[0] ? ASD_AUDIO_SAMPLE_OFF_B : 
ASD_AUDIO_SAMPLE_OFF_A;
 }
 
 static int asd_read_header(AVFormatContext *s)
@@ -290,8 +283,6 @@ static int asd_read_header(AVFormatContext *s)
     if ((ret = asd_build_index(s)) < 0)
         return ret;
 
-    asd_detect_sample_shift(s);
-
     for (int i = ASD_STREAM_CAM_A; i <= ASD_STREAM_CAM_B; i++) {
         const ASDStreamStats *stats = &c->stats[i];
 
@@ -357,17 +348,21 @@ static int asd_read_packet(AVFormatContext *s, AVPacket 
*pkt)
             return ret;
         pkt->duration = 0;
     } else {
-        uint8_t payload[ASD_AUDIO_PAYLOAD_SIZE];
+        uint8_t payload[ASD_AUDIO_CHUNK_SIZE];
         unsigned i;
 
+        if (chunk->size > sizeof(payload))
+            return AVERROR_INVALIDDATA;
         avio_skip(pb, ASD_AUDIO_HEADER_SIZE);
-        if (avio_read(pb, payload, ASD_AUDIO_PAYLOAD_SIZE) != 
ASD_AUDIO_PAYLOAD_SIZE)
+        if (avio_read(pb, payload, chunk->size - ASD_AUDIO_HEADER_SIZE) !=
+            chunk->size - ASD_AUDIO_HEADER_SIZE)
             return AVERROR_INVALIDDATA;
         if ((ret = av_new_packet(pkt, ASD_AUDIO_SAMPLES * 2)) < 0)
             return ret;
-        /* keep the used half of each 32-bit word, drop the padding half */
+        c->sample_off = asd_pick_sample_off(c, payload);
+        /* keep the sample half of each 4-byte word, drop the padding */
         for (i = 0; i < ASD_AUDIO_SAMPLES; i++)
-            AV_WL16(pkt->data + 2 * i, AV_RL16(payload + 4 * i + 
c->sample_shift));
+            AV_WL16(pkt->data + 2 * i, AV_RL16(payload + c->sample_off + 4 * 
i));
 
         pkt->duration = ASD_AUDIO_DURATION;
     }
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to