ref: 7c20afca72c6d4f856556a386312072ddde5f826
parent: 31f2fad7a119fbd09485233b0794be6cbdaa6c31
author: Sigrid Haflínudóttir <[email protected]>
date: Mon Sep 21 09:42:55 EDT 2020
ebml/ogg: use discard padding, codec delay and seek preroll to calculate granule position
--- a/common.h
+++ b/common.h
@@ -27,6 +27,8 @@
struct Packetctx {
uvlong frid;
uvlong duration;
+ uvlong seekpreroll;
+ vlong discardpad;
struct {
char name[16];
vlong delay;
@@ -62,7 +64,6 @@
packet_f fpacket;
vlong timestampscale;
- vlong seekpreroll;
vlong perframe;
int tracknum;
int tracktype;
--- a/ebml.c
+++ b/ebml.c
@@ -242,9 +242,8 @@
{
int isebml, npackets, i;
vlong left, id, n, sz, bufsz, track, off, packetsz, x;
- uvlong ts;
+ uvlong ts, timestamp, endtracks, timestampscale;
uchar *buf;
- vlong timestamp, endtracks, timestampscale;
double duration;
Ebml e, te;
@@ -401,7 +400,7 @@
}
/* ns timestamp */
- ts = (timestamp + (s16int)(buf[0]<<8 | buf[1])) * timestampscale;
+ ts = (timestamp + (s16int)(buf[0]<<8 | buf[1])) * timestampscale - te.codec.delay;
if(te.fpacket(&out, &te, packets, npackets, ts) != 0)
goto err;
continue;
@@ -454,6 +453,8 @@
ebmlgetfloat(0x4489, duration)
else
ebmlgetnumber(0x73c5, e.trackuid)
+ else
+ ebmlgetnumber(0x75a2, te.discardpad)
//else
// ebmlgetnumber(0x9b, blockdur)
}
--- a/ogg.c
+++ b/ogg.c
@@ -62,6 +62,7 @@
if(Bwrite(out, h, ntot) != ntot)
return -1;
for(ip = 0; ip < np; ip++){
+ fprint(2, "%d %.*H\n", ip, min(8, p[ip].sz), p[ip].data);
if(Bwrite(out, p[ip].data, p[ip].sz) != p[ip].sz)
return -1;
}
@@ -80,8 +81,7 @@
int sgszs[4];
Packet onep;
- if(ctx->fmt == FmtOpus)
- ts += 80000000; /* pre-skip */
+ ts += ctx->seekpreroll - ctx->discardpad;
gr = (ts * 48) / 1000000;
r = 0;
@@ -137,7 +137,7 @@
goto err;
}
- if(np > 0 && packet(out, ctx, 0, p, np, gr) != 0)
+ if(np > 0 && packet(out, ctx, ctx->discardpad ? 4 : 0, p, np, gr) != 0)
goto err;
return 0;