Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD)
@ 2022-09-15 19:23 Andreas Rheinhardt
  2022-09-15 19:28 ` [FFmpeg-devel] [PATCH 2/9] avcodec/utils: Support APTX (HD) in av_get_audio_frame_duration() Andreas Rheinhardt
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:23 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

We have de- and encoders for APTX and APTX HD, yet not FATE tests.
This commit therefore adds a transcoding test to utilize them.

Furthermore, during creating these tests it turned out that
the duration is set incorrectly for APTX HD. This will be fixed
in a future commit.

(Thanks to Andriy Gelman for finding an issue in an earlier version
that used a 192kHz input sample which does not work reliably accross
platforms.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 tests/fate/audio.mak   |  7 +++++++
 tests/ref/fate/aptx    | 18 ++++++++++++++++++
 tests/ref/fate/aptx-hd | 15 +++++++++++++++
 3 files changed, 40 insertions(+)
 create mode 100644 tests/ref/fate/aptx
 create mode 100644 tests/ref/fate/aptx-hd

diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
index fd9905ca0a..9d39eeace3 100644
--- a/tests/fate/audio.mak
+++ b/tests/fate/audio.mak
@@ -1,3 +1,10 @@
+FATE_SAMPLES_AUDIO-$(call TRANSCODE, APTX, APTX, WAV_DEMUXER PCM_S16LE_DECODER ARESAMPLE_FILTER) += fate-aptx
+fate-aptx: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/luckynight_2ch_44kHz_s16.wav aptx "-af aresample -c aptx" "-af aresample -c:a pcm_s16le -t 0.25" "" "" "-f aptx -sample_rate 44100"
+
+FATE_SAMPLES_AUDIO-$(call TRANSCODE, APTX_HD, APTX_HD, WAV_DEMUXER PCM_S16LE_DECODER \
+                          ARESAMPLE_FILTER PCM_S32LE_ENCODER) += fate-aptx-hd
+fate-aptx-hd: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/luckynight_2ch_44kHz_s16.wav aptx_hd "-af aresample -c aptx_hd" "-af aresample -c:a pcm_s32le -t 0.25" "" "" "-f aptx_hd -sample_rate 44100"
+
 FATE_BINKAUDIO-$(call DEMDEC, BINK, BINKAUDIO_DCT) += fate-binkaudio-dct
 fate-binkaudio-dct: CMD = pcm -i $(TARGET_SAMPLES)/bink/binkaudio_dct.bik
 fate-binkaudio-dct: REF = $(SAMPLES)/bink/binkaudio_dct.pcm
diff --git a/tests/ref/fate/aptx b/tests/ref/fate/aptx
new file mode 100644
index 0000000000..4d20b7df9a
--- /dev/null
+++ b/tests/ref/fate/aptx
@@ -0,0 +1,18 @@
+b5d8a297c0e8d9854f19d9d3e8b82859 *tests/data/fate/aptx.aptx
+418950 tests/data/fate/aptx.aptx
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: 2 channels
+0,          0,          0,     1024,     4096, 0xcbb4ceff
+0,       1024,       1024,     1024,     4096, 0xa66533e7
+0,       2048,       2048,     1024,     4096, 0x4f22ec39
+0,       3072,       3072,     1024,     4096, 0x45f117f9
+0,       4096,       4096,     1024,     4096, 0xad6c0b7a
+0,       5120,       5120,     1024,     4096, 0x611618fd
+0,       6144,       6144,     1024,     4096, 0x0ec02f2b
+0,       7168,       7168,     1024,     4096, 0x2cf9ae5c
+0,       8192,       8192,     1024,     4096, 0xfb008ac0
+0,       9216,       9216,     1024,     4096, 0x25068495
+0,      10240,      10240,      785,     3140, 0x5a260589
diff --git a/tests/ref/fate/aptx-hd b/tests/ref/fate/aptx-hd
new file mode 100644
index 0000000000..0691f33c86
--- /dev/null
+++ b/tests/ref/fate/aptx-hd
@@ -0,0 +1,15 @@
+48ecaa81ee5adaaa62ed3ff6574b6666 *tests/data/fate/aptx-hd.aptx_hd
+628425 tests/data/fate/aptx-hd.aptx_hd
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s32le
+#sample_rate 0: 44100
+#channel_layout_name 0: 2 channels
+0,          0,          0,     1024,     8192, 0xa99888c6
+0,       1536,       1536,     1024,     8192, 0xc3e03a3c
+0,       3072,       3072,     1024,     8192, 0x3f06e090
+0,       4608,       4608,     1024,     8192, 0x92fb18f3
+0,       6144,       6144,     1024,     8192, 0x3d5603a2
+0,       7680,       7680,     1024,     8192, 0xcc3d3101
+0,       9216,       9216,     1024,     8192, 0xbcc022ef
+0,      10752,      10752,      273,     2184, 0x9873af57
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 2/9] avcodec/utils: Support APTX (HD) in av_get_audio_frame_duration()
  2022-09-15 19:23 [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD) Andreas Rheinhardt
@ 2022-09-15 19:28 ` Andreas Rheinhardt
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
  2022-09-18 20:46 ` [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD) Andreas Rheinhardt
  2 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

APTX decodes four bytes of input to four stereo samples; APTX HD
does the same with six bytes of input. So it can be easily supported
in av_get_audio_frame_duration().

This fixes invalid durations and (derived) timestamps of demuxed
APTX HD packets and therefore fixed the timestamp in the aptx-hd
FATE test.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/utils.c     |  4 ++++
 tests/ref/fate/aptx-hd | 17 ++++++++++-------
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 2f57418ff7..ba64aaf32d 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -682,6 +682,10 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
             return 256 * (frame_bytes / 64);
         if (id == AV_CODEC_ID_RA_144)
             return 160 * (frame_bytes / 20);
+        if (id == AV_CODEC_ID_APTX)
+            return 4 * (frame_bytes / 4);
+        if (id == AV_CODEC_ID_APTX_HD)
+            return 4 * (frame_bytes / 6);
 
         if (bps > 0) {
             /* calc from frame_bytes and bits_per_coded_sample */
diff --git a/tests/ref/fate/aptx-hd b/tests/ref/fate/aptx-hd
index 0691f33c86..498b9131a6 100644
--- a/tests/ref/fate/aptx-hd
+++ b/tests/ref/fate/aptx-hd
@@ -6,10 +6,13 @@
 #sample_rate 0: 44100
 #channel_layout_name 0: 2 channels
 0,          0,          0,     1024,     8192, 0xa99888c6
-0,       1536,       1536,     1024,     8192, 0xc3e03a3c
-0,       3072,       3072,     1024,     8192, 0x3f06e090
-0,       4608,       4608,     1024,     8192, 0x92fb18f3
-0,       6144,       6144,     1024,     8192, 0x3d5603a2
-0,       7680,       7680,     1024,     8192, 0xcc3d3101
-0,       9216,       9216,     1024,     8192, 0xbcc022ef
-0,      10752,      10752,      273,     2184, 0x9873af57
+0,       1024,       1024,     1024,     8192, 0xc3e03a3c
+0,       2048,       2048,     1024,     8192, 0x3f06e090
+0,       3072,       3072,     1024,     8192, 0x92fb18f3
+0,       4096,       4096,     1024,     8192, 0x3d5603a2
+0,       5120,       5120,     1024,     8192, 0xcc3d3101
+0,       6144,       6144,     1024,     8192, 0xbcc022ef
+0,       7168,       7168,     1024,     8192, 0x600cbb73
+0,       8192,       8192,     1024,     8192, 0xdc938cbb
+0,       9216,       9216,     1024,     8192, 0x37d968bc
+0,      10240,      10240,      785,     6280, 0x48243144
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 3/9] avcodec/aptx: Use AVCodecContext.frame_size according to the API
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 4/9] avformat/aptxdec: Don't set AVCodecParameters.frame_size Andreas Rheinhardt
                     ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Currently the APTX (HD) codecs set frame_size if unset and check
whether it is divisible by block_size (corresponding to block_align
as used by other codecs). But this is based upon a misunderstanding
of the API: frame_size is not in bytes, but in samples.

Said value is also not intended to be set by the user at all,
but set by encoders and (possibly) decoders if the number of channels
in a frame is constant. The latter condition is not fulfilled here,
so only set it for encoders. Given that the encoder can handle any
number of samples as long as it is divisible by four and given that
it worked to set a custom frame size before, the encoders accept
any multiple of four; otherwise the value is set to the value
that it already had for APTX: 1024 samples (per channel).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aptx.c    | 9 ---------
 libavcodec/aptxenc.c | 3 +++
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/libavcodec/aptx.c b/libavcodec/aptx.c
index 8e110acc97..ed814ad1c0 100644
--- a/libavcodec/aptx.c
+++ b/libavcodec/aptx.c
@@ -516,15 +516,6 @@ av_cold int ff_aptx_init(AVCodecContext *avctx)
     s->hd = avctx->codec->id == AV_CODEC_ID_APTX_HD;
     s->block_size = s->hd ? 6 : 4;
 
-    if (avctx->frame_size == 0)
-        avctx->frame_size = 256 * s->block_size;
-
-    if (avctx->frame_size % s->block_size) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Frame size must be a multiple of %d samples\n", s->block_size);
-        return AVERROR(EINVAL);
-    }
-
     for (chan = 0; chan < NB_CHANNELS; chan++) {
         Channel *channel = &s->channels[chan];
         for (subband = 0; subband < NB_SUBBANDS; subband++) {
diff --git a/libavcodec/aptxenc.c b/libavcodec/aptxenc.c
index f9347853d2..434a9abf0f 100644
--- a/libavcodec/aptxenc.c
+++ b/libavcodec/aptxenc.c
@@ -257,6 +257,9 @@ static av_cold int aptx_encode_init(AVCodecContext *avctx)
 
     ff_af_queue_init(avctx, &s->afq);
 
+    if (!avctx->frame_size || avctx->frame_size % 4)
+        avctx->frame_size = 1024;
+
     return ff_aptx_init(avctx);
 }
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 4/9] avformat/aptxdec: Don't set AVCodecParameters.frame_size
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 3/9] avcodec/aptx: Use AVCodecContext.frame_size according to the API Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 5/9] avformat/aptxdec: Don't set AV_PKT_FLAG_CORRUPT mistakenly Andreas Rheinhardt
                     ` (4 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

This field was misunderstood: It gives the number of samples
in a packet, not the number of bytes. Its usage was wrong for APTX HD.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/aptxdec.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libavformat/aptxdec.c b/libavformat/aptxdec.c
index 693316eeb0..aa86bfe330 100644
--- a/libavformat/aptxdec.c
+++ b/libavformat/aptxdec.c
@@ -58,7 +58,6 @@ static int aptx_read_header(AVFormatContext *s)
     st->codecpar->codec_id = AV_CODEC_ID_APTX;
     st->codecpar->bits_per_coded_sample = 4;
     st->codecpar->block_align = APTX_BLOCK_SIZE;
-    st->codecpar->frame_size = APTX_PACKET_SIZE;
     return 0;
 }
 
@@ -70,7 +69,6 @@ static int aptx_hd_read_header(AVFormatContext *s)
     st->codecpar->codec_id = AV_CODEC_ID_APTX_HD;
     st->codecpar->bits_per_coded_sample = 6;
     st->codecpar->block_align = APTX_HD_BLOCK_SIZE;
-    st->codecpar->frame_size = APTX_HD_PACKET_SIZE;
     return 0;
 }
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 5/9] avformat/aptxdec: Don't set AV_PKT_FLAG_CORRUPT mistakenly
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 3/9] avcodec/aptx: Use AVCodecContext.frame_size according to the API Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 4/9] avformat/aptxdec: Don't set AVCodecParameters.frame_size Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 6/9] avcodec/aptxdec: Process data in complete blocks only Andreas Rheinhardt
                     ` (3 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Just because we try to put multiple units of block_align bytes
(the atomic units for APTX and APTX HD) into one packet
does not mean that packets with fewer units than the
one we wanted are corrupt; only those packets that are not
a multiple of block_align are.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/aptxdec.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavformat/aptxdec.c b/libavformat/aptxdec.c
index aa86bfe330..0637a8afde 100644
--- a/libavformat/aptxdec.c
+++ b/libavformat/aptxdec.c
@@ -74,12 +74,18 @@ static int aptx_hd_read_header(AVFormatContext *s)
 
 static int aptx_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    return av_get_packet(s->pb, pkt, APTX_PACKET_SIZE);
+    int ret = av_get_packet(s->pb, pkt, APTX_PACKET_SIZE);
+    if (ret >= 0 && !(ret % APTX_BLOCK_SIZE))
+        pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
+    return ret >= 0 ? 0 : ret;
 }
 
 static int aptx_hd_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    return av_get_packet(s->pb, pkt, APTX_HD_PACKET_SIZE);
+    int ret = av_get_packet(s->pb, pkt, APTX_HD_PACKET_SIZE);
+    if (ret >= 0 && !(ret % APTX_HD_BLOCK_SIZE))
+        pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
+    return ret >= 0 ? 0 : ret;
 }
 
 static const AVOption aptx_options[] = {
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 6/9] avcodec/aptxdec: Process data in complete blocks only
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
                     ` (2 preceding siblings ...)
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 5/9] avformat/aptxdec: Don't set AV_PKT_FLAG_CORRUPT mistakenly Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 7/9] avcodec/encode: Redo checks for small last audio frame Andreas Rheinhardt
                     ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

The APTX (HD) decoder decodes blocks of four (six) bytes to four
output samples. It makes no sense to handle incomplete blocks:
They would just lead to synchronization errors, in which case
the complete frame is discarded. So only handle complete blocks.
This also avoids reading from the packet's padding and writing
into the frame's padding.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aptxdec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/aptxdec.c b/libavcodec/aptxdec.c
index 878c9ffe1b..d254b3026b 100644
--- a/libavcodec/aptxdec.c
+++ b/libavcodec/aptxdec.c
@@ -151,7 +151,7 @@ static int aptx_decode_frame(AVCodecContext *avctx, AVFrame *frame,
     /* get output buffer */
     frame->ch_layout.nb_channels = NB_CHANNELS;
     frame->format = AV_SAMPLE_FMT_S32P;
-    frame->nb_samples = 4 * avpkt->size / s->block_size;
+    frame->nb_samples = 4 * (avpkt->size / s->block_size);
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 7/9] avcodec/encode: Redo checks for small last audio frame
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
                     ` (3 preceding siblings ...)
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 6/9] avcodec/aptxdec: Process data in complete blocks only Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 8/9] avcodec/encode: Enable encoders to control padding of last frame Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 9/9] avcodec/aptxenc: Process data in complete blocks of four samples only Andreas Rheinhardt
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

In particular, check that there is only one small last frame
in case the encoder has the AV_CODEC_CAP_SMALL_LAST_FRAME set.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/encode.c   | 21 +++++++++------------
 libavcodec/internal.h |  4 ++--
 2 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index ade4d458e7..9bd9f9bc08 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -157,6 +157,7 @@ static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
 
 fail:
     av_frame_unref(frame);
+    s->internal->last_audio_frame = 0;
     return ret;
 }
 
@@ -392,28 +393,24 @@ static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
             avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
 
         /* check for valid frame size */
-        if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
-            if (src->nb_samples > avctx->frame_size) {
-                av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
-                return AVERROR(EINVAL);
-            }
-        } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
+        if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
             /* if we already got an undersized frame, that must have been the last */
             if (avctx->internal->last_audio_frame) {
                 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
                 return AVERROR(EINVAL);
             }
-
+            if (src->nb_samples > avctx->frame_size) {
+                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
+                return AVERROR(EINVAL);
+            }
             if (src->nb_samples < avctx->frame_size) {
+                avctx->internal->last_audio_frame = 1;
+                if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
                 ret = pad_last_frame(avctx, dst, src);
                 if (ret < 0)
                     return ret;
-
-                avctx->internal->last_audio_frame = 1;
                 goto finish;
-            } else if (src->nb_samples > avctx->frame_size) {
-                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
-                return AVERROR(EINVAL);
+                }
             }
         }
     }
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 35e3dd303a..45aec38a60 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -57,8 +57,8 @@ typedef struct AVCodecInternal {
     int is_copy;
 
     /**
-     * An audio frame with less than required samples has been submitted and
-     * padded with silence. Reject all subsequent frames.
+     * An audio frame with less than required samples has been submitted (and
+     * potentially padded with silence). Reject all subsequent frames.
      */
     int last_audio_frame;
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 8/9] avcodec/encode: Enable encoders to control padding of last frame
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
                     ` (4 preceding siblings ...)
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 7/9] avcodec/encode: Redo checks for small last audio frame Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 9/9] avcodec/aptxenc: Process data in complete blocks of four samples only Andreas Rheinhardt
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Some audio codecs work with atomic units that decode to a fixed
number of audio samples with this number being so small that it is
common to put multiple of these atoms into one packet. In these
cases it makes no sense to pad the last frame to the big frame_size,
so allow encoders to set the number of samples that they want
the last frame to be padded to instead.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/encode.c   | 17 +++++++++++------
 libavcodec/internal.h |  6 ++++++
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 9bd9f9bc08..049b71c6f4 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -127,12 +127,12 @@ static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
 /**
  * Pad last frame with silence.
  */
-static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
+static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
 {
     int ret;
 
     frame->format         = src->format;
-    frame->nb_samples     = s->frame_size;
+    frame->nb_samples     = out_samples;
     ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
     if (ret < 0)
         goto fail;
@@ -406,10 +406,15 @@ static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
             if (src->nb_samples < avctx->frame_size) {
                 avctx->internal->last_audio_frame = 1;
                 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
-                ret = pad_last_frame(avctx, dst, src);
-                if (ret < 0)
-                    return ret;
-                goto finish;
+                    int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
+                    int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
+
+                    if (out_samples != src->nb_samples) {
+                        ret = pad_last_frame(avctx, dst, src, out_samples);
+                        if (ret < 0)
+                            return ret;
+                        goto finish;
+                    }
                 }
             }
         }
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 45aec38a60..76a6ea6bc6 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -62,6 +62,12 @@ typedef struct AVCodecInternal {
      */
     int last_audio_frame;
 
+    /**
+     * Audio encoders can set this flag during init to indicate that they
+     * want the small last frame to be padded to a multiple of pad_samples.
+     */
+    int pad_samples;
+
     AVBufferRef *pool;
 
     void *thread_ctx;
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 9/9] avcodec/aptxenc: Process data in complete blocks of four samples only
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
                     ` (5 preceding siblings ...)
  2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 8/9] avcodec/encode: Enable encoders to control padding of last frame Andreas Rheinhardt
@ 2022-09-15 19:28   ` Andreas Rheinhardt
  6 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-15 19:28 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Do this by setting AVCodecInternal.pad_samples.
This prevents reading into the frame's padding and writing
into the packet's padding.

This actually happened in our FATE tests (where the number of samples
is 2 mod 4), which therefore needed to be updated.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aptxenc.c   | 6 ++++--
 tests/ref/fate/aptx    | 4 ++--
 tests/ref/fate/aptx-hd | 4 ++--
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/aptxenc.c b/libavcodec/aptxenc.c
index 434a9abf0f..114e286fe2 100644
--- a/libavcodec/aptxenc.c
+++ b/libavcodec/aptxenc.c
@@ -27,6 +27,7 @@
 #include "audio_frame_queue.h"
 #include "codec_internal.h"
 #include "encode.h"
+#include "internal.h"
 
 typedef struct AptXEncContext {
     AptXContext common;
@@ -259,6 +260,7 @@ static av_cold int aptx_encode_init(AVCodecContext *avctx)
 
     if (!avctx->frame_size || avctx->frame_size % 4)
         avctx->frame_size = 1024;
+    avctx->internal->pad_samples = 4;
 
     return ff_aptx_init(avctx);
 }
@@ -269,7 +271,7 @@ const FFCodec ff_aptx_encoder = {
     CODEC_LONG_NAME("aptX (Audio Processing Technology for Bluetooth)"),
     .p.type                = AVMEDIA_TYPE_AUDIO,
     .p.id                  = AV_CODEC_ID_APTX,
-    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
+    .p.capabilities        = AV_CODEC_CAP_DR1,
     .priv_data_size        = sizeof(AptXEncContext),
     .init                  = aptx_encode_init,
     FF_CODEC_ENCODE_CB(aptx_encode_frame),
@@ -290,7 +292,7 @@ const FFCodec ff_aptx_hd_encoder = {
     CODEC_LONG_NAME("aptX HD (Audio Processing Technology for Bluetooth)"),
     .p.type                = AVMEDIA_TYPE_AUDIO,
     .p.id                  = AV_CODEC_ID_APTX_HD,
-    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
+    .p.capabilities        = AV_CODEC_CAP_DR1,
     .priv_data_size        = sizeof(AptXEncContext),
     .init                  = aptx_encode_init,
     FF_CODEC_ENCODE_CB(aptx_encode_frame),
diff --git a/tests/ref/fate/aptx b/tests/ref/fate/aptx
index 4d20b7df9a..7917399297 100644
--- a/tests/ref/fate/aptx
+++ b/tests/ref/fate/aptx
@@ -1,5 +1,5 @@
-b5d8a297c0e8d9854f19d9d3e8b82859 *tests/data/fate/aptx.aptx
-418950 tests/data/fate/aptx.aptx
+97dfd7f32d34fb74337c0f6c66e26e2d *tests/data/fate/aptx.aptx
+418952 tests/data/fate/aptx.aptx
 #tb 0: 1/44100
 #media_type 0: audio
 #codec_id 0: pcm_s16le
diff --git a/tests/ref/fate/aptx-hd b/tests/ref/fate/aptx-hd
index 498b9131a6..d74c1059c2 100644
--- a/tests/ref/fate/aptx-hd
+++ b/tests/ref/fate/aptx-hd
@@ -1,5 +1,5 @@
-48ecaa81ee5adaaa62ed3ff6574b6666 *tests/data/fate/aptx-hd.aptx_hd
-628425 tests/data/fate/aptx-hd.aptx_hd
+6126e414af1acb193e24fdf33c7cac2e *tests/data/fate/aptx-hd.aptx_hd
+628428 tests/data/fate/aptx-hd.aptx_hd
 #tb 0: 1/44100
 #media_type 0: audio
 #codec_id 0: pcm_s32le
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD)
  2022-09-15 19:23 [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD) Andreas Rheinhardt
  2022-09-15 19:28 ` [FFmpeg-devel] [PATCH 2/9] avcodec/utils: Support APTX (HD) in av_get_audio_frame_duration() Andreas Rheinhardt
       [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
@ 2022-09-18 20:46 ` Andreas Rheinhardt
  2 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-09-18 20:46 UTC (permalink / raw)
  To: ffmpeg-devel

Andreas Rheinhardt:
> We have de- and encoders for APTX and APTX HD, yet not FATE tests.
> This commit therefore adds a transcoding test to utilize them.
> 
> Furthermore, during creating these tests it turned out that
> the duration is set incorrectly for APTX HD. This will be fixed
> in a future commit.
> 
> (Thanks to Andriy Gelman for finding an issue in an earlier version
> that used a 192kHz input sample which does not work reliably accross
> platforms.)
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  tests/fate/audio.mak   |  7 +++++++
>  tests/ref/fate/aptx    | 18 ++++++++++++++++++
>  tests/ref/fate/aptx-hd | 15 +++++++++++++++
>  3 files changed, 40 insertions(+)
>  create mode 100644 tests/ref/fate/aptx
>  create mode 100644 tests/ref/fate/aptx-hd
> 
> diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
> index fd9905ca0a..9d39eeace3 100644
> --- a/tests/fate/audio.mak
> +++ b/tests/fate/audio.mak
> @@ -1,3 +1,10 @@
> +FATE_SAMPLES_AUDIO-$(call TRANSCODE, APTX, APTX, WAV_DEMUXER PCM_S16LE_DECODER ARESAMPLE_FILTER) += fate-aptx
> +fate-aptx: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/luckynight_2ch_44kHz_s16.wav aptx "-af aresample -c aptx" "-af aresample -c:a pcm_s16le -t 0.25" "" "" "-f aptx -sample_rate 44100"
> +
> +FATE_SAMPLES_AUDIO-$(call TRANSCODE, APTX_HD, APTX_HD, WAV_DEMUXER PCM_S16LE_DECODER \
> +                          ARESAMPLE_FILTER PCM_S32LE_ENCODER) += fate-aptx-hd
> +fate-aptx-hd: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/luckynight_2ch_44kHz_s16.wav aptx_hd "-af aresample -c aptx_hd" "-af aresample -c:a pcm_s32le -t 0.25" "" "" "-f aptx_hd -sample_rate 44100"
> +
>  FATE_BINKAUDIO-$(call DEMDEC, BINK, BINKAUDIO_DCT) += fate-binkaudio-dct
>  fate-binkaudio-dct: CMD = pcm -i $(TARGET_SAMPLES)/bink/binkaudio_dct.bik
>  fate-binkaudio-dct: REF = $(SAMPLES)/bink/binkaudio_dct.pcm
> diff --git a/tests/ref/fate/aptx b/tests/ref/fate/aptx
> new file mode 100644
> index 0000000000..4d20b7df9a
> --- /dev/null
> +++ b/tests/ref/fate/aptx
> @@ -0,0 +1,18 @@
> +b5d8a297c0e8d9854f19d9d3e8b82859 *tests/data/fate/aptx.aptx
> +418950 tests/data/fate/aptx.aptx
> +#tb 0: 1/44100
> +#media_type 0: audio
> +#codec_id 0: pcm_s16le
> +#sample_rate 0: 44100
> +#channel_layout_name 0: 2 channels
> +0,          0,          0,     1024,     4096, 0xcbb4ceff
> +0,       1024,       1024,     1024,     4096, 0xa66533e7
> +0,       2048,       2048,     1024,     4096, 0x4f22ec39
> +0,       3072,       3072,     1024,     4096, 0x45f117f9
> +0,       4096,       4096,     1024,     4096, 0xad6c0b7a
> +0,       5120,       5120,     1024,     4096, 0x611618fd
> +0,       6144,       6144,     1024,     4096, 0x0ec02f2b
> +0,       7168,       7168,     1024,     4096, 0x2cf9ae5c
> +0,       8192,       8192,     1024,     4096, 0xfb008ac0
> +0,       9216,       9216,     1024,     4096, 0x25068495
> +0,      10240,      10240,      785,     3140, 0x5a260589
> diff --git a/tests/ref/fate/aptx-hd b/tests/ref/fate/aptx-hd
> new file mode 100644
> index 0000000000..0691f33c86
> --- /dev/null
> +++ b/tests/ref/fate/aptx-hd
> @@ -0,0 +1,15 @@
> +48ecaa81ee5adaaa62ed3ff6574b6666 *tests/data/fate/aptx-hd.aptx_hd
> +628425 tests/data/fate/aptx-hd.aptx_hd
> +#tb 0: 1/44100
> +#media_type 0: audio
> +#codec_id 0: pcm_s32le
> +#sample_rate 0: 44100
> +#channel_layout_name 0: 2 channels
> +0,          0,          0,     1024,     8192, 0xa99888c6
> +0,       1536,       1536,     1024,     8192, 0xc3e03a3c
> +0,       3072,       3072,     1024,     8192, 0x3f06e090
> +0,       4608,       4608,     1024,     8192, 0x92fb18f3
> +0,       6144,       6144,     1024,     8192, 0x3d5603a2
> +0,       7680,       7680,     1024,     8192, 0xcc3d3101
> +0,       9216,       9216,     1024,     8192, 0xbcc022ef
> +0,      10752,      10752,      273,     2184, 0x9873af57

Will apply this patchset tomorrow unless there are objections.

- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-09-18 20:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-15 19:23 [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD) Andreas Rheinhardt
2022-09-15 19:28 ` [FFmpeg-devel] [PATCH 2/9] avcodec/utils: Support APTX (HD) in av_get_audio_frame_duration() Andreas Rheinhardt
     [not found] ` <20220915192809.2761301-1-andreas.rheinhardt@outlook.com>
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 3/9] avcodec/aptx: Use AVCodecContext.frame_size according to the API Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 4/9] avformat/aptxdec: Don't set AVCodecParameters.frame_size Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 5/9] avformat/aptxdec: Don't set AV_PKT_FLAG_CORRUPT mistakenly Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 6/9] avcodec/aptxdec: Process data in complete blocks only Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 7/9] avcodec/encode: Redo checks for small last audio frame Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 8/9] avcodec/encode: Enable encoders to control padding of last frame Andreas Rheinhardt
2022-09-15 19:28   ` [FFmpeg-devel] [PATCH 9/9] avcodec/aptxenc: Process data in complete blocks of four samples only Andreas Rheinhardt
2022-09-18 20:46 ` [FFmpeg-devel] [PATCH 1/9] fate/audio: Add tests for APTX (HD) Andreas Rheinhardt

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git