Unified · Split
| @@ -4,33 +4,94 @@ · 3 unmodified lines | ||
| 4 | 4 | #include "common.h" |
| 5 | 5 | #include "packet.h" |
| 6 | 6 | |
| 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 | + | |
| 7 | 26 | int |
| 8 | 27 | aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts) |
| 9 | 28 | { |
| 10 | 29 | int i, chanc, ratei, objt, sz; |
| 11 | 30 | u16int x; |
| 31 | + char *c; | |
| 12 | 32 | |
| 13 | 33 | USED(ts); |
| 14 | 34 | 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 | − } | |
| 28 | 35 | ctx->adts[0] = 0xff; /* syncword */ |
| 29 | 36 | 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 | + | |
| 30 | 90 | ctx->adts[2] = (objt-1)<<6 | ratei<<2 | chanc>>2; /* object type, rate, channel config */ |
| 31 | 91 | ctx->adts[3] = (chanc&3)<<6; /* channel config */ |
| 32 | 92 | ctx->adts[6] = 0xfc; /* fullness, number of frames */ |
| 33 | 93 | } |
| 94 | + | |
| 34 | 95 | for(i = 0; i < np; i++, p++){ |
| 35 | 96 | sz = 7+p->sz; |
| 36 | 97 | 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 | ||
| 43 | 104 | |
| 44 | 105 | return 0; |
| 45 | 106 | err: |
| 46 | − werrstr("aacpacket: %r"); | |
| 107 | + werrstr("aacpacket: adts: %r"); | |
| 47 | 108 | return -1; |
| 48 | 109 | } |