ref: ae4f2427e78904f57323f3618f110e2a8b36a602
parent: d33fb00ca66876c6e663ab1c69e812fdf2e66ae4
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Feb 8 06:52:32 EST 2021
matroska, aac: construct ADTS from codec private data
--- a/aac.c
+++ b/aac.c
@@ -7,11 +7,39 @@
int
aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts)
{
- int i;
+ int i, chanc, ratei, objt, sz;
+ u16int x;
USED(ts);
+ if(ctx->frid == 0){ /* set up ADTS */
+ if(ctx->codec.priv.sz < 2){
+ /* FIXME we could reconstruct it from codec mapping and Audio element */
+ werrstr("no codec private data to construct ADTS");
+ return -1;
+ }
+ x = ctx->codec.priv.data[0]<<8 | ctx->codec.priv.data[1];
+ chanc = (x>>3) & 0xf;
+ ratei = (x>>7) & 0xf;
+ objt = x>>11;
+ if(objt > 4){
+ werrstr("AAC object type %d, can't write ADTS", objt);
+ return -1;
+ }
+ sz = 0; /* will be filled in later */
+ ctx->adts[0] = 0xff; /* syncword */
+ ctx->adts[1] = 0xf1; /* syncword, mpeg4, no crc */
+ ctx->adts[2] = (objt-1)<<6 | ratei<<2 | chanc>>2; /* object type, rate, channel config */
+ ctx->adts[3] = (chanc&3)<<6 | (sz>>11)&3; /* channel config, frame length */
+ ctx->adts[4] = sz>>3; /* frame length */
+ ctx->adts[5] = (sz&7)<<5 | 0x1f; /* frame length, fullness */
+ ctx->adts[6] = 0xfc; /* fullness, number of frames */
+ }
for(i = 0; i < np; i++, p++){
- if(Bwrite(out, p->data, p->sz) != p->sz)
+ sz = 7+p->sz;
+ ctx->adts[3] = ctx->adts[3]&~3 | (sz>>11)&3; /* channels, frame length */
+ ctx->adts[4] = sz>>3; /* frame length */
+ ctx->adts[5] = (sz&7)<<5 | 0x1f; /* frame length, fullness */
+ if(Bwrite(out, ctx->adts, 7) != 7 || Bwrite(out, p->data, p->sz) != p->sz)
goto err;
}
ctx->frid++;
--- a/ogg.c
+++ b/ogg.c
@@ -90,7 +90,7 @@
dsz = ctx->codec.priv.sz;
/* id/codec setup header always goes first */
- if(ctx->codec.priv.sz > 0){ /* if we have codec setup data, write it */
+ if(dsz > 0){ /* if we have codec setup data, write it */
if(ctx->fmt == FmtVorbis || ctx->fmt == FmtTheora){
i = *d++;
dsz--;
--- a/packet.h
+++ b/packet.h
@@ -39,6 +39,7 @@
int channels;
int bps;
}audio;
+ uchar adts[7];
};
int aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts);