From b0d0e0e025fc696270ef59a451d9ee93e2ba27b5 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Sun, 28 Jul 2019 22:27:10 +0200
Subject: [PATCH 1/2] avcodec/dstdec: add slice threading support

---
 libavcodec/dstdec.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavcodec/dstdec.c b/libavcodec/dstdec.c
index 0614c99c4b..c62f6b77e4 100644
--- a/libavcodec/dstdec.c
+++ b/libavcodec/dstdec.c
@@ -221,6 +221,19 @@ static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *
     }
 }
 
+static int dsd_channel(AVCodecContext *avctx, void *tdata, int j, int threadnr)
+{
+    DSTContext *s = avctx->priv_data;
+    AVFrame *frame = tdata;
+    float *pcm = (float *)frame->data[0];
+
+    ff_dsd2pcm_translate(&s->dsdctx[j], frame->nb_samples, 0,
+                         frame->data[0] + j * 4,
+                         avctx->channels * 4, pcm + j, avctx->channels);
+
+    return 0;
+}
+
 static int decode_frame(AVCodecContext *avctx, void *data,
                         int *got_frame_ptr, AVPacket *avpkt)
 {
@@ -235,7 +248,6 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     ArithCoder *ac = &s->ac;
     AVFrame *frame = data;
     uint8_t *dsd;
-    float *pcm;
     int ret;
 
     if (avpkt->size <= 1)
@@ -245,7 +257,6 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
     dsd = frame->data[0];
-    pcm = (float *)frame->data[0];
 
     if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
         return ret;
@@ -349,11 +360,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     }
 
 dsd:
-    for (i = 0; i < avctx->channels; i++) {
-        ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
-                             frame->data[0] + i * 4,
-                             avctx->channels * 4, pcm + i, avctx->channels);
-    }
+    avctx->execute2(avctx, dsd_channel, frame, NULL, avctx->channels);
 
     *got_frame_ptr = 1;
 
@@ -368,7 +375,7 @@ AVCodec ff_dst_decoder = {
     .priv_data_size = sizeof(DSTContext),
     .init           = decode_init,
     .decode         = decode_frame,
-    .capabilities   = AV_CODEC_CAP_DR1,
+    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
                                                       AV_SAMPLE_FMT_NONE },
 };
-- 
2.22.0

