Unexpected aac audio duration
Hi all, I'm trying to encode audio with ffmpeg aac encoder. An example with brown noise input follows: ffmpeg -f lavfi -i anoisesrc=c=brown:a=0.05:d=1:r=8000:n=1024 -t 0.560 -ac 8000 -codec aac -b:a 16k -f adts out.aac The result is shown below: ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 7 (Ubuntu 7.3.0-16ubuntu3) configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared libavutil 55. 78.100 / 55. 78.100 libavcodec 57.107.100 / 57.107.100 libavformat 57. 83.100 / 57. 83.100 libavdevice 57. 10.100 / 57. 10.100 libavfilter 6.107.100 / 6.107.100 libavresample 3. 7. 0 / 3. 7. 0 libswscale 4. 8.100 / 4. 8.100 libswresample 2. 9.100 / 2. 9.100 libpostproc 54. 7.100 / 54. 7.100 Input #0, lavfi, from 'anoisesrc=c=brown:a=0.05:d=1:r=8000:n=1024': Duration: N/A, start: 0.000000, bitrate: 512 kb/s Stream #0:0: Audio: pcm_f64le, 8000 Hz, mono, dbl, 512 kb/s Stream mapping: Stream #0:0 -> #0:0 (pcm_f64le (native) -> aac (native)) Press [q] to stop, [?] for help Output #0, adts, to 'out.aac': Metadata: encoder : Lavf57.83.100 Stream #0:0: Audio: aac (LC), 8000 Hz, mono, fltp, 16 kb/s Metadata: encoder : Lavc57.107.100 aac size= 1kB time=00:00:00.64 bitrate= 18.7kbits/s speed=76.3x video:0kB audio:1kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.892562% [aac @ 0x5563feb38820] Qavg: 11006.257 Due to the aac frames size (1024) and 8KHz sample rate the duration of each aac frame is 1024/8000 = 0.128 seconds. The required duration (-t 0.56) cannot be divided by 0.128 so it is correctly rounded to the nearest frame (5*0.128 = 0.64). The result, as reported by ffmpeg "time=00:00:00.64", should have a duration of 0.64 seconds. But if I decode the aac to raw pcm I get a different duration: ffmpeg -i out.aac -c pcm_s16le -f s16le out_aac.raw [...] Output #0, s16le, to 'out_aac.raw': Metadata: encoder : Lavf57.83.100 Stream #0:0: Audio: pcm_s16le, 8000 Hz, mono, s16, 128 kb/s Metadata: encoder : Lavc57.107.100 pcm_s16le size= 12kB time=00:00:00.76 bitrate= 128.0kbits/s speed=1.26e+03x video:0kB audio:12kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000% It seems that an aac frame has been added to the output (6*0.128 = 0.768). I have also checked the file size (12288 byte). This means 6144 samples. With 8KHz the duration is 6144 / 8000 = 0.768 seconds. I have noticed the same behaviour with longer durations, without the anoisesrc source (file input) and also with muxer (e.g. HLS) based on aac encoder. Below an example based on previously generated out.aac file (0.768 seconds) and HLS muxer: ffmpeg -i out.aac -b:a 16k -hls_time 1.2 -hls_list_size 0 -hls_segment_type mpegts out.m3u8 [...] Output #0, hls, to 'out.m3u8': Metadata: encoder : Lavf57.83.100 Stream #0:0: Audio: aac (LC), 8000 Hz, mono, fltp, 16 kb/s Metadata: encoder : Lavc57.107.100 aac [hls @ 0x55f0b7d84840] Opening 'out.m3u8.tmp' for writing size=N/A time=00:00:00.76 bitrate=N/A speed= 113x video:0kB audio:3kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown [aac @ 0x55f0b7d837c0] Qavg: 19275.893 The input samples count is divisible by 1024. However, the output duration is increased of another frame (7*0.128 = 0.896) as shown by the m3u8 file: #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:1 #EXT-X-MEDIA-SEQUENCE:0 #EXTINF:0.896000, out0.ts #EXT-X-ENDLIST I would like to ask if this the expected behaviour of aac encoder. And if so, is it possible to get an output with the correct duration (eventually rounded to the first aac frame)? Thanks for any help, Nadir
You are most likely seeing an effect of the aac priming delay, extra samples are added before the actual content, its normal and decoders should account for it (the priming silence having negative presentation timestamps). Unfortunately they don't always agree on how many samples the delay should be (iirc some expect/use 1024 samples but then other things like apple tools expect 2112) which can sometimes cause issues. Sorry I know this doesn't fix what you are seeing but hopefully the background info is useful. -- Bryce McLeod Codory Software Limited email: bryce.mcleod@codory.com skype: bmcleodcd web: http://www.codory.com Codory Software Limited is registered in England and Wales, no. 08711579. Registered office: First Floor, Telecom House 125-135 Preston Road, Brighton, BN1 6AF.
Hi Bryce, thank you very much for the information. I have checked the presence of the 1024 samples padding in libavcodec/aacenc.c file (avctx->initial_padding = 1024). Furthermore, I drew the audio waveform with an online tool. It shows the initial priming delay frame, too. Just two more questions to better understand the management process. 1) ffmpeg doesn't seem to be aware about the priming delay presence: the decoded pcm contains the padding samples and the same happens with HLS chunks. Is this correct? Shouldn't it be discarded? 2) the priming delay frame doesn't have a negative PTS (see below the output of ffprobe). There is another way to check for it? ./ffprobe -show_frames test/out.aac ffprobe version N-96789-g56df8296f5 Copyright (c) 2007-2020 the FFmpeg developers built with gcc 7 (Ubuntu 7.4.0-1ubuntu1~18.04.1) configuration: libavutil 56. 41.100 / 56. 41.100 libavcodec 58. 71.100 / 58. 71.100 libavformat 58. 38.101 / 58. 38.101 libavdevice 58. 9.103 / 58. 9.103 libavfilter 7. 76.100 / 7. 76.100 libswscale 5. 6.100 / 5. 6.100 libswresample 3. 6.100 / 3. 6.100 [aac @ 0x55c3b0149d80] Estimating duration from bitrate, this may be inaccurate Input #0, aac, from 'test/out.aac': Duration: 00:00:14.14, bitrate: 0 kb/s Stream #0:0: Audio: aac (LC), 8000 Hz, mono, fltp, 0 kb/s [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=0 pkt_pts_time=0.000000 pkt_dts=0 pkt_dts_time=0.000000 best_effort_timestamp=0 best_effort_timestamp_time=0.000000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=0 pkt_size=262 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=3612672 pkt_pts_time=0.128000 pkt_dts=3612672 pkt_dts_time=0.128000 best_effort_timestamp=3612672 best_effort_timestamp_time=0.128000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=262 pkt_size=273 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=7225344 pkt_pts_time=0.256000 pkt_dts=7225344 pkt_dts_time=0.256000 best_effort_timestamp=7225344 best_effort_timestamp_time=0.256000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=535 pkt_size=232 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=10838016 pkt_pts_time=0.384000 pkt_dts=10838016 pkt_dts_time=0.384000 best_effort_timestamp=10838016 best_effort_timestamp_time=0.384000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=767 pkt_size=271 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=14450688 pkt_pts_time=0.512000 pkt_dts=14450688 pkt_dts_time=0.512000 best_effort_timestamp=14450688 best_effort_timestamp_time=0.512000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=1038 pkt_size=276 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] [FRAME] media_type=audio stream_index=0 key_frame=1 pkt_pts=18063360 pkt_pts_time=0.640000 pkt_dts=18063360 pkt_dts_time=0.640000 best_effort_timestamp=18063360 best_effort_timestamp_time=0.640000 pkt_duration=3612672 pkt_duration_time=0.128000 pkt_pos=1314 pkt_size=12 sample_fmt=fltp nb_samples=1024 channels=1 channel_layout=mono [/FRAME] Thanks in advance for any help you are able to provide. Nadir
participants (2)
-
Bryce McLeod -
naraimo@libero.it