On 1/25/16, Paul B Mahol <one...@gmail.com> wrote:
> Hi,
>
> nonworking patch atttached.
>

Working patch attached.
From 1e6ed69925ff05a934b2fe68830512246e616367 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <one...@gmail.com>
Date: Mon, 25 Jan 2016 18:39:23 +0100
Subject: [PATCH] avcodec: multithread audio frame encoding for wavpack and
 alac

Signed-off-by: Paul B Mahol <one...@gmail.com>
---
 libavcodec/alacenc.c              |  2 +-
 libavcodec/frame_thread_encoder.c | 14 +++++++++++---
 libavcodec/frame_thread_encoder.h |  2 +-
 libavcodec/utils.c                | 14 ++++++++++----
 libavcodec/wavpackenc.c           |  8 ++++----
 5 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index a87c373..7ada570 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -653,7 +653,7 @@ AVCodec ff_alac_encoder = {
     .init           = alac_encode_init,
     .encode2        = alac_encode_frame,
     .close          = alac_encode_close,
-    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
+    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_FRAME_THREADS,
     .channel_layouts = ff_alac_channel_layouts,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
                                                      AV_SAMPLE_FMT_S16P,
diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index f4d35f9..d54e3de 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -83,7 +83,14 @@ static void * attribute_align_arg worker(void *v){
         pthread_mutex_unlock(&c->task_fifo_mutex);
         frame = task.indata;
 
-        ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
+        switch (avctx->codec_type) {
+        case AVMEDIA_TYPE_AUDIO:
+            ret = avcodec_encode_audio2(avctx, pkt, frame, &got_packet);
+            break;
+        case AVMEDIA_TYPE_VIDEO:
+            ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
+            break;
+        }
         pthread_mutex_lock(&c->buffer_mutex);
         av_frame_unref(frame);
         pthread_mutex_unlock(&c->buffer_mutex);
@@ -115,7 +122,8 @@ int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
 
 
     if(   !(avctx->thread_type & FF_THREAD_FRAME)
-       || !(avctx->codec->capabilities & AV_CODEC_CAP_INTRA_ONLY))
+       || (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
+            !(avctx->codec->capabilities & AV_CODEC_CAP_INTRA_ONLY)))
         return 0;
 
     if(   !avctx->thread_count
@@ -239,7 +247,7 @@ void ff_frame_thread_encoder_free(AVCodecContext *avctx){
     av_freep(&avctx->internal->frame_thread_encoder);
 }
 
-int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
+int ff_thread_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
     ThreadContext *c = avctx->internal->frame_thread_encoder;
     Task task;
     int ret;
diff --git a/libavcodec/frame_thread_encoder.h b/libavcodec/frame_thread_encoder.h
index 1da0ce1..fdaaea5 100644
--- a/libavcodec/frame_thread_encoder.h
+++ b/libavcodec/frame_thread_encoder.h
@@ -22,5 +22,5 @@
 
 int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options);
 void ff_frame_thread_encoder_free(AVCodecContext *avctx);
-int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr);
+int ff_thread_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index abddd6f..ea73e8a 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1317,7 +1317,9 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (!HAVE_THREADS)
         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
 
-    if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
+    if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec) && (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
+                                                                             (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
+                                                                             (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)))) {
         ff_unlock_avcodec(codec); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
         ff_lock_avcodec(avctx, codec);
@@ -1704,6 +1706,10 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
 
     *got_packet_ptr = 0;
 
+    if (CONFIG_FRAME_THREAD_ENCODER &&
+        avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
+        return ff_thread_encode_frame(avctx, avpkt, frame, got_packet_ptr);
+
     if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
         av_packet_unref(avpkt);
         av_init_packet(avpkt);
@@ -1843,9 +1849,9 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
 
     *got_packet_ptr = 0;
 
-    if(CONFIG_FRAME_THREAD_ENCODER &&
-       avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
-        return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
+    if (CONFIG_FRAME_THREAD_ENCODER &&
+        avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
+        return ff_thread_encode_frame(avctx, avpkt, frame, got_packet_ptr);
 
     if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
         avctx->stats_out[0] = '\0';
diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c
index 979b921..80f88dc 100644
--- a/libavcodec/wavpackenc.c
+++ b/libavcodec/wavpackenc.c
@@ -78,7 +78,6 @@ typedef struct WavPackEncodeContext {
     PutBitContext pb;
     int block_samples;
     int buffer_size;
-    int sample_index;
     int stereo, stereo_in;
     int ch_offset;
 
@@ -114,6 +113,7 @@ typedef struct WavPackEncodeContext {
     uint8_t float_flags, float_shift, float_max_exp, max_exp;
     int32_t shifted_ones, shifted_zeros, shifted_both;
     int32_t false_zeros, neg_zeros, ordata;
+    int64_t pts;
 
     int num_terms, shift, joint_stereo, false_stereo;
     int num_decorrs, num_passes, best_decorr, mask_decorr;
@@ -2593,7 +2593,7 @@ static int wavpack_encode_block(WavPackEncodeContext *s,
     bytestream2_put_le16(&pb, 0x410);
     bytestream2_put_le16(&pb, 0);
     bytestream2_put_le32(&pb, 0);
-    bytestream2_put_le32(&pb, s->sample_index);
+    bytestream2_put_le32(&pb, s->pts);
     bytestream2_put_le32(&pb, nb_samples);
     bytestream2_put_le32(&pb, s->flags);
     bytestream2_put_le32(&pb, crc);
@@ -2869,6 +2869,7 @@ static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     int buf_size, ret;
     uint8_t *buf;
 
+    s->pts           = frame->pts;
     s->block_samples = frame->nb_samples;
     av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
                           sizeof(int32_t) * s->block_samples);
@@ -2913,7 +2914,6 @@ static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         buf      += ret;
         buf_size -= ret;
     }
-    s->sample_index += frame->nb_samples;
 
     avpkt->pts      = frame->pts;
     avpkt->size     = buf - avpkt->data;
@@ -2981,7 +2981,7 @@ AVCodec ff_wavpack_encoder = {
     .init           = wavpack_encode_init,
     .encode2        = wavpack_encode_frame,
     .close          = wavpack_encode_close,
-    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
+    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_FRAME_THREADS,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
                                                      AV_SAMPLE_FMT_S16P,
                                                      AV_SAMPLE_FMT_S32P,
-- 
1.9.1

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

Reply via email to