[FFmpeg-devel,v1,2/2] avcodec/v308dec: add the slice threading support

Submitted by lance.lmwang@gmail.com on Aug. 30, 2019, 3:53 p.m.

Details

Message ID 20190830155357.27747-2-lance.lmwang@gmail.com
State New
Headers show

Commit Message

lance.lmwang@gmail.com Aug. 30, 2019, 3:53 p.m.
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavcodec/v308dec.c | 74 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 19 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/v308dec.c b/libavcodec/v308dec.c
index dd53fbd..07e5e24 100644
--- a/libavcodec/v308dec.c
+++ b/libavcodec/v308dec.c
@@ -22,6 +22,13 @@ 
 #include "avcodec.h"
 #include "internal.h"
 
+#define MAX_SLICES 32
+typedef struct ThreadData {
+    AVFrame *frame;
+    uint8_t *buf;
+    int stride;
+} ThreadData;
+
 static av_cold int v308_decode_init(AVCodecContext *avctx)
 {
     avctx->pix_fmt = AV_PIX_FMT_YUV444P;
@@ -32,14 +39,53 @@  static av_cold int v308_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
+static int v308_decode_slice(AVCodecContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+    ThreadData *td = arg;
+    AVFrame *pic = td->frame;
+    int stride = td->stride;
+    int thread_count = av_clip(avctx->thread_count, 1, MAX_SLICES);
+    int slice_h = avctx->height / thread_count;
+    int slice_m = avctx->height % thread_count;
+    int slice_start = jobnr * slice_h;
+    int slice_end = slice_start + slice_h;
+    const uint8_t *src = td->buf + stride * slice_start;
+    uint8_t *y, *u, *v;
+    int i, j;
+
+     /* add the remaining slice for the last job */
+    if (jobnr == thread_count - 1)
+        slice_end += slice_m;
+
+    y = pic->data[0] + slice_start * pic->linesize[0];
+    u = pic->data[1] + slice_start * pic->linesize[1];
+    v = pic->data[2] + slice_start * pic->linesize[2];
+
+    for (i = slice_start; i < avctx->height; i++) {
+        for (j = 0; j < avctx->width; j++) {
+            v[j] = *src++;
+            y[j] = *src++;
+            u[j] = *src++;
+        }
+
+        y += pic->linesize[0];
+        u += pic->linesize[1];
+        v += pic->linesize[2];
+    }
+
+    return 0;
+}
+
 static int v308_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
+    ThreadData td;
     AVFrame *pic = data;
-    const uint8_t *src = avpkt->data;
-    uint8_t *y, *u, *v;
-    int i, j, ret;
+    uint8_t *src = avpkt->data;
+    int ret;
+    int thread_count = av_clip(avctx->thread_count, 1, MAX_SLICES);
 
+    td.stride = avctx->width * 3;
     if (avpkt->size < 3 * avctx->height * avctx->width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
@@ -51,21 +97,9 @@  static int v308_decode_frame(AVCodecContext *avctx, void *data,
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
 
-    y = pic->data[0];
-    u = pic->data[1];
-    v = pic->data[2];
-
-    for (i = 0; i < avctx->height; i++) {
-        for (j = 0; j < avctx->width; j++) {
-            v[j] = *src++;
-            y[j] = *src++;
-            u[j] = *src++;
-        }
-
-        y += pic->linesize[0];
-        u += pic->linesize[1];
-        v += pic->linesize[2];
-    }
+    td.buf = src;
+    td.frame = pic;
+    avctx->execute2(avctx, v308_decode_slice, &td, NULL, thread_count);
 
     *got_frame = 1;
 
@@ -79,5 +113,7 @@  AVCodec ff_v308_decoder = {
     .id           = AV_CODEC_ID_V308,
     .init         = v308_decode_init,
     .decode       = v308_decode_frame,
-    .capabilities = AV_CODEC_CAP_DR1,
+    .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
+    .caps_internal= FF_CODEC_CAP_INIT_THREADSAFE |
+                      FF_CODEC_CAP_INIT_CLEANUP,
 };