History

aac: reconstruct adts header from matroska elements

Sigrid Solveig Haflínudóttir committed 5 years ago

Commit f7bc1761 parent 5c91e881 file · +75 −14Browse files

Unified · Split

aac.c+75−14
@@ -4,33 +4,94 @@ · 3 unmodified lines
44 #include "common.h"
55 #include "packet.h"
66
7+static int ratecfg[] = {
8+ 96000,
9+ 88200,
10+ 64000,
11+ 48000,
12+ 44100,
13+ 32000,
14+ 24000,
15+ 22050,
16+ 16000,
17+ 12000,
18+ 11025,
19+ 8000,
20+ 7350,
21+ -1,
22+ -1,
23+ -1,
24+};
25+
726 int
827 aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts)
928 {
1029 int i, chanc, ratei, objt, sz;
1130 u16int x;
31+ char *c;
1232
1333 USED(ts);
1434 if(ctx->frid == 0){ /* set up ADTS */
15− if(ctx->codec.priv.sz < 2){
16− /* FIXME we could reconstruct it from codec mapping and Audio element */
17− werrstr("no codec private data to construct ADTS");
18− return -1;
19− }
20− x = ctx->codec.priv.data[0]<<8 | ctx->codec.priv.data[1];
21− chanc = (x>>3) & 0xf;
22− ratei = (x>>7) & 0xf;
23− objt = x>>11;
24− if(objt > 4){
25− werrstr("AAC object type %d, can't write ADTS", objt);
26− return -1;
27− }
2835 ctx->adts[0] = 0xff; /* syncword */
2936 ctx->adts[1] = 0xf1; /* syncword, mpeg4, no crc */
37+ if(ctx->codec.priv.sz >= 2){ /* prefer code private data */
38+ x = ctx->codec.priv.data[0]<<8 | ctx->codec.priv.data[1];
39+ chanc = (x>>3) & 0xf;
40+ ratei = (x>>7) & 0xf;
41+ objt = x>>11;
42+ if(chanc > 7){
43+ werrstr("channel config out of range: %d", chanc);
44+ goto err;
45+ }
46+ if(ratecfg[ratei] < 1){
47+ werrstr("invalid rate config: %d", ratei);
48+ goto err;
49+ }
50+ if(objt > 4){
51+ werrstr("object type out of range: %d", objt);
52+ goto err;
53+ }
54+ }else{
55+ for(ratei = 0; ratei < nelem(ratecfg) && ratecfg[ratei] != ctx->audio.samplerate; ratei++);
56+ if(ratei >= nelem(ratecfg) || ratecfg[ratei] < 1){
57+ werrstr("unsupported sample rate: %d", (int)ctx->audio.samplerate);
58+ goto err;
59+ }
60+
61+ c = ctx->codec.name;
62+ if(memcmp(c, "A_AAC/MPEG2/", 12) == 0){
63+ ctx->adts[1] |= 1<<3;
64+ }else if(memcmp(c, "A_AAC/MPEG4/", 12) != 0){
65+badcodec:
66+ werrstr("unsupported codec: %s", c);
67+ goto err;
68+ }
69+ if(strcmp(c+12, "MAIN") == 0)
70+ objt = 1;
71+ else if(memcmp(c+12, "LC", 2) == 2 && (c[14] == 0 || strcmp(c+14, "/SBR") == 0))
72+ objt = 2;
73+ else if(strcmp(c+12, "SSR") == 0)
74+ objt = 3;
75+ else if(strcmp(c+12, "LTP") == 0)
76+ objt = 4;
77+ else
78+ goto badcodec;
79+
80+ if(ctx->audio.channels >= 1 && ctx->audio.channels <= 6)
81+ chanc = ctx->audio.channels;
82+ else if(ctx->audio.channels == 8)
83+ chanc = 7;
84+ else{ /* FIXME is it possible to use 0 and provide a custom channel config? */
85+ werrstr("unsupported number of channels: %d", ctx->audio.channels);
86+ goto err;
87+ }
88+ }
89+
3090 ctx->adts[2] = (objt-1)<<6 | ratei<<2 | chanc>>2; /* object type, rate, channel config */
3191 ctx->adts[3] = (chanc&3)<<6; /* channel config */
3292 ctx->adts[6] = 0xfc; /* fullness, number of frames */
3393 }
94+
3495 for(i = 0; i < np; i++, p++){
3596 sz = 7+p->sz;
3697 ctx->adts[3] = ctx->adts[3]&(3<<6) | (sz>>11)&3; /* channels, frame length */
@@ -43,6 +104,6 @@ aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts) · 6 unmodified lines
43104
44105 return 0;
45106 err:
46− werrstr("aacpacket: %r");
107+ werrstr("aacpacket: adts: %r");
47108 return -1;
48109 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t