Author Topic: BASSenc_AAC (AAC encoding)  (Read 19772 times)

Ian @ un4seen

  • Administrator
  • Posts: 26177
BASSenc_AAC (AAC encoding)
« on: 9 Aug '19 - 16:06 »
Here's a little AAC encoding add-on based on the FDK AAC encoder:

   www.un4seen.com/stuff/bassenc_aac.zip
   www.un4seen.com/stuff/bassenc_aac-linux.zip
   www.un4seen.com/stuff/bassenc_aac-linux-arm.zip
   www.un4seen.com/stuff/bassenc_aac-android.zip

No documentation yet, but the API is the same as the other encoding add-ons, with these "options" settings available:

  --object-type <value> ... "value" can be: 2/5/23/29/39. See here for descriptions: www.wikipedia.org/wiki/MPEG-4_Part_3#MPEG-4_Audio_Object_Types
  --vbr <value> ... "value" can be 0 (CBR) or 1-5 (VBR levels).
  --bitrate <value> ... bitrate (bps) for CBR.

The default is type 2 CBR with the bitrate based on the sample rate and channel count. No tagging or MP4 options currently, so it'll probably be mostly useful for streaming purposes (eg. with BASS_Encode_CastInit and BASS_Encode_ServerInit) rather than file writing.

AAC is patented and a licence is required to use it in commercial products. A licence can be obtained from Via:

   https://www.via-corp.com/licensing/aac/
« Last Edit: 26 Jul '22 - 17:59 by Ian @ un4seen »

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #1 on: 21 Aug '19 - 14:22 »
An Android version has now been added in the 1st post.

kremk

  • Posts: 4
Re: BASSenc_AAC (AAC encoding)
« Reply #2 on: 28 Mar '20 - 14:22 »
Thank you for the add-on, it works very well with object-type 2 and 5 but when I try 29 for Parametric Stereo it returns BASS_ERROR_UNKNOWN on BASS_Encode_AAC_Start. Do I need to set some flags or something before starting? I've tried 24 to 128 bitrates with BASSEncode.BASS_ENCODE_PAUSE flag only.

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #3 on: 30 Mar '20 - 17:54 »
Is the source stream stereo? BASS_Encode_AAC_Start currently gives a BASS_ERROR_UNKNOWN error if not. An extra check has been added for the next update so that it gives a more helpful BASS_ERROR_FORMAT error then instead.

kremk

  • Posts: 4
Re: BASSenc_AAC (AAC encoding)
« Reply #4 on: 31 Mar '20 - 13:56 »
Thank you, stereo was the issue as you said, now it's working well. I just forgot to switch back to stereo after testing speaker flags :)

scarboni

  • Posts: 53
Re: BASSenc_AAC (AAC encoding)
« Reply #5 on: 18 Feb '21 - 14:09 »
Hello ! I see that there is no library file for macos, but, it is also written that " The AAC/MP4 format is supported as standard by BASS via the OS's codecs on macOS and iOS", what does that mean exactly if we want to encode using AAC ?

Thank you !

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #6 on: 18 Feb '21 - 16:05 »
The BASSenc_AAC add-on isn't currently available for macOS/iOS because they provide AAC encoding support as standard, which can be accessed via BASSenc's BASS_Encode_StartCA and BASS_Encode_StartCAFile functions. Please see the BASSenc documentation for details. You can also check the CONVERT.C example that's included in the BASSenc package for a little demonstration.

lordbarba

  • Posts: 11
Re: BASSenc_AAC (AAC encoding)
« Reply #7 on: 19 May '21 - 14:14 »
Does it work as AAC+ encoder on arm architecture?

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #8 on: 19 May '21 - 15:54 »
Yes, it should do. You would put "--object-type 5" in the options for HE-AAC.

mhuellwegen

  • Posts: 20
Re: BASSenc_AAC (AAC encoding)
« Reply #9 on: 6 Mar '22 - 16:15 »
Many thanks for the great BASSenc_ACC add-on.

I just start using

   BASS_Encode_AAC_Start(stream1, "--object-type 2 --vbr 0 --bitrate 96000", 0, SELF_Encode_AAC_Proc, &self);

to join encoded Data in my own callback function

   CALLBACK SELF_Encode_AAC_Proc(HENCODE handle, DWORD channel, const void* buffer, DWORD length, QWORD offset, void* user) {

      ... some code here ...

   }



to generate an stereo.m3u8 playlist and in this context also the stereo-00001.ts, stereo-00002.ts, ... files on the fly for HLS streaming this via IIS Webserver.



Currently I managed to successfully interpret the ADTS header (the first 7 Bytes of the buffer on each CALLBACK call).

But I failed at the point joining multiple CALLBACK calls to e.g. ~4 seconds of 48000 samples @ 96000 bits/s stereo audio, calculate the exact audio length in seconds so i can save/deliver the ready to go .m3u8 and .ts file on the fly.
The main problem is that i do not know how many CALLBACK calls buffer to join to get ~4 seconds audio and then get te exact seconds (with e.g. 6 decimal places) since i have to declarate the exact duration time in the .m3u8 file.

Sample .m3u8 file:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:5      or 4??
#EXTINF:4.010667,
stereo-00000.ts
#EXTINF:3.989333,
stereo-00001.ts
#EXTINF:4.010667,
stereo-00002.ts
#EXTINF:3.989333,
stereo-00003.ts




Can anyone give me any hints on how to do this?

Thanx in advance,
Martin


Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #10 on: 8 Mar '22 - 17:55 »
Each ADTS frame contains 1024 samples, so if you count the number frames received in your callback function then you can calculate the duration (multiply by 1024 and divide by the sample rate).

You can get the size of an ADTS frame (eg. to reach the next frame) like this:

Code: [Select]
size = ((head[3] & 0x3) << 11) | (head[4] << 3) | (head[5] >> 5);

Where "head" is a byte pointer to the ADTS header.

mhuellwegen

  • Posts: 20
Re: BASSenc_AAC (AAC encoding)
« Reply #11 on: 8 Mar '22 - 18:48 »
@Ian,

thanx a lot for your important hint.

Since i realized that it does not depend on the CALLBACKs buffer length parameter,
but on the 1024 samples divide by 48000/s in my case and multiplied with my targeted duration of about 4 sec as you mensioned,
i also realized that i will get an odd block count of 187,5 for exact 4 secs as well which means i will first join 187 blocks (3,989333333 exact seconds) and then 188 blocks (4,0106666 exact seconds)
and will get two ts-segments of totaly 375 blocks representing 8,0000 exact seconds of audio, if i am correct now.

This is what i've also noticed in lots of .m3u8 files as well, that odd and even segments have 3,989333 and 4,010666 seconds in that context.

I will try this soon and if someone is interested in i will report next time, how i managed to create a bunch of memory mapped files,
one for the playlist.m3u8 and the others as segment-0001.ts, -0002.ts, -0003.ts files and how i picked them up
by an IIS hosted default.aspx (.NET) page to deliver playlist and ts-segments on the fly.

Hanuman

  • Posts: 114
Re: BASSenc_AAC (AAC encoding)
« Reply #12 on: 26 Jul '22 - 03:38 »
While you provide no binary for iOS, iOS built-in encoder takes different parameters:
- ftype: File format identifier.
- atype: Audio data format identifier.
- bitrate: The bitrate in bits per second... 0 = the codec's default bitrate.

This makes developing a cross-platform a bit more complicated... Since the built-in function has limited features, perhaps you could release a version for iOS as well?

Wanting to use iOS built-in function also makes me have to worry about deploying the correct version of ManagedBass... which I suspect it's not doing currently.
« Last Edit: 26 Jul '22 - 03:44 by Hanuman »

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #13 on: 26 Jul '22 - 17:59 »
The AAC format has patent licensing requirements, so it makes sense to use the OS's already licensed encoder (and decoder) whenever possible, eg. on iOS.

Hanuman

  • Posts: 114
Re: BASSenc_AAC (AAC encoding)
« Reply #14 on: 27 Jul '22 - 22:11 »
The encoded audio fails to play in most media players. Is it a raw audio stream without a valid file container?

If that's the case, it's useful to then muxing into a container using FFMPEG, but not so useful on its own.

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #15 on: 28 Jul '22 - 11:38 »
BASSenc_AAC produces AAC data in an ADTS container. It's what *.AAC files generally are, as well as what Shoutcast/Icecast AAC streams use. It should be supported by any AAC player. Are the media players that you tried able to play other *.AAC files? If so, please confirm what parameters you're using with BASSenc_AAC and on what platform.

Hanuman

  • Posts: 114
Re: BASSenc_AAC (AAC encoding)
« Reply #16 on: 28 Jul '22 - 14:09 »
It fails with MPV player; which normally has pretty good codec support normally. I encoded with default parameters on Linux.

WAIT -- my MPV installation is broken :D It doesn't play anything at all. Let me fix that first
« Last Edit: 28 Jul '22 - 15:49 by Hanuman »

Hanuman

  • Posts: 114
Re: BASSenc_AAC (AAC encoding)
« Reply #17 on: 28 Jul '22 - 16:10 »
OK the file works in MPV. Sorry for the false alarm! (and the mpv-full branch is broken in the repo; mpv alone works)

So the only thing missing are tags then.

One detail to pay attention to: --bitrate is bps instead of kbps so need to be multiplied by 1024. --bitrate 320 gave me somewhat bad quality...
« Last Edit: 28 Jul '22 - 16:53 by Hanuman »

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #18 on: 29 Jul '22 - 14:18 »
Good to hear that you've got it working now.

Regarding tagging, ADTS doesn't include tag support but ID3 tags should generally work fine with it. So you could try using an ID3 tagging library to add tags to the AAC file afterwards (or perhaps even before if you use an ENCODEPROCEX callback to append the encoded data). The more standard way to include tags would be to use an MP4 container instead, but BASSenc_AAC doesn't currently have that option.

kafffee

  • Posts: 291
Re: BASSenc_AAC (AAC encoding)
« Reply #19 on: 21 Aug '23 - 08:55 »
Quote
No tagging or MP4 options currently, so it'll probably be mostly useful for streaming purposes (eg. with BASS_Encode_CastInit and BASS_Encode_ServerInit) rather than file writing.

So ripping CDs and/or encoding of a mixer channel and write it to a file is not supported yet?

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: BASSenc_AAC (AAC encoding)
« Reply #20 on: 21 Aug '23 - 10:41 »
You can write an AAC file (see BASS_Encode_AAC_StartFile) but you can't currently set any tags while doing so. If tags are wanted, you could perhaps use an ID3 tagging library to add them to the written file afterwards.

kafffee

  • Posts: 291
Re: BASSenc_AAC (AAC encoding)
« Reply #21 on: 21 Aug '23 - 11:48 »
Cool okay, I already do so :-)

orange

  • Posts: 13
Re: BASSenc_AAC (AAC encoding)
« Reply #22 on: 4 Nov '23 - 22:32 »
I have tried all other formats, opus, ogg, and others, but the same result is compressing the audio, sending it, then receiving it and playing it. The problem is with playback with all libraries.

am use android java

                encodeHandle=     BASS_Encode_AAC_Start(recordChannel, "--object-type 2 --vbr 0 --bitrate 32000", 0,  encodeProcx,null);

 public static com.un4seen.bass.BASSenc.ENCODEPROCEX  encodeProcx = (int handle, int channel, ByteBuffer buffer, int length, long offset, Object user) -> {
        byte[] data = new byte[length];
      buffer.get(data);
     sendEncodedData(data);

    };

 ByteBuffer byteBuffer = ByteBuffer.wrap(encodedData);
        int channel = BASS_AAC_StreamCreateFile(byteBuffer, 0, encodedData.length, 0);

 BASS.BASS_ChannelPlay(channel, false);

I tried every method but the error appears every time I run it
error 41
« Last Edit: 5 Nov '23 - 16:56 by orange »

Chris

  • Posts: 2217
Re: BASSenc_AAC (AAC encoding)
« Reply #23 on: 5 Nov '23 - 10:26 »
Hi is that a Typo ?
You are encoding in AAC

and decoding in Opus?


orange

  • Posts: 13
Re: BASSenc_AAC (AAC encoding)
« Reply #24 on: 5 Nov '23 - 16:55 »
No, no, by mistake, I sent this code. I also use aac decompression