[Title] Upgrade codec module as async i/o and modify source according to coding conve...
authorKitae Kim <kt920.kim@samsung.com>
Wed, 8 Aug 2012 02:21:33 +0000 (11:21 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Wed, 8 Aug 2012 02:21:33 +0000 (11:21 +0900)
[Type] enhancement
[Module] emulator / codec
[Priority] major
[CQ#]
[Redmine#] Interal Issue.
[Problem] While testing HTML5 VideoTag, emulator could be locked up.
[Cause] sychronous i/o and use fixed offset for mapped memory.
[Solution] async i/o and flexible offset.
[TestCase]

package/pkginfo.manifest
tizen/src/hw/maru_codec.c
tizen/src/hw/maru_codec.h

index 0715c88e74b321a7b6c42a2c352df7aa299560e0..e17c1392807fa3f4adb330c7f45508cf4680f093 100644 (file)
@@ -1,4 +1,4 @@
-Version: 1.3.10
+Version: 1.3.14
 Maintainer: Yeong-Kyoon Lee<yeongkyoon.lee@samsung.com>
 Source: emulator
 
index 42e6e46b6de540775bb55273de80e604703a6efc..49ae2c26fd8d407295ce02ad69eb31ba16ed6b38 100644 (file)
@@ -32,7 +32,7 @@
 #include "qemu-common.h"
 
 #define MARU_CODEC_DEV_NAME     "codec"
-#define MARU_CODEC_VERSION      "1.1"
+#define MARU_CODEC_VERSION      "1.2"
 
 /*  Needs 16M to support 1920x1080 video resolution.
  *  Output size for encoding has to be greater than (width * height * 6)
 #define MARU_CODEC_REG_SIZE     (256)
 
 #define MARU_ROUND_UP_16(num)   (((num) + 15) & ~15)
+#define CODEC_COPY_DATA
+#define CODEC_IRQ 0x7f
 
 /* define debug channel */
 MULTI_DEBUG_CHANNEL(qemu, marucodec);
 
-static int paramCount = 0;
-static int ctxArrIndex = 0;
+void codec_thread_init(SVCodecState *s)
+{
+    TRACE("Enter, %s\n", __func__);
 
-#define CODEC_COPY_DATA
+    qemu_thread_create(&s->thread_id, codec_worker_thread, (void *)s);
+
+    TRACE("Leave, %s\n", __func__);
+}
+
+void codec_thread_exit(SVCodecState *s)
+{
+    TRACE("Enter, %s\n", __func__);
+
+    TRACE("destroy mutex and conditional.\n");
+    qemu_mutex_destroy(&s->thread_mutex);
+    qemu_cond_destroy(&s->thread_cond);
+
+/*  qemu_thread_exit((void*)0); */
+
+    TRACE("Leave, %s\n", __func__);
+}
+
+void wake_codec_worker_thread(SVCodecState *s)
+{
+    TRACE("Enter, %s\n", __func__);
+
+    qemu_cond_signal(&s->thread_cond);
+    TRACE("[%d] sent a conditional signal to a worker thread.\n", __LINE__);
+
+    TRACE("Leave, %s\n", __func__);
+}
+
+void *codec_worker_thread(void *opaque)
+{
+    SVCodecState *s = (SVCodecState *)opaque;
+
+    TRACE("Enter, %s\n", __func__);
+
+    qemu_mutex_lock(&s->thread_mutex);
+
+    while (1) {
+        TRACE("[%d] wait conditional signal.\n", __LINE__);
+        qemu_cond_wait(&s->thread_cond, &s->thread_mutex);
+
+        TRACE("[%d] wake up a worker thread.\n", __LINE__);
+        /* decode and encode */
+        if (s->ctxArr[s->codecParam.ctxIndex].avctx->codec->decode) {
+            decode_codec(s);
+        } else {
+            encode_codec(s);
+        }
+
+        s->thread_state = CODEC_IRQ;
+        qemu_bh_schedule(s->tx_bh);
+    }
+    qemu_mutex_unlock(&s->thread_mutex);
+
+    TRACE("Leave, %s\n", __func__);
+}
+
+/* needs context index, how to get it? */
+int decode_codec(SVCodecState *s)
+{
+    AVCodecContext *avctx;
+    uint32_t len = 0, ctxIndex;
+
+    TRACE("Enter, %s\n", __func__);
+
+    qemu_mutex_unlock(&s->thread_mutex);
+/*  qemu_mutex_lock(&s->thread_mutex); */
+
+    ctxIndex = s->codecParam.ctxIndex;
+    avctx = s->ctxArr[ctxIndex].avctx;
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        len = qemu_avcodec_decode_video(s, ctxIndex);
+    } else {
+        len = qemu_avcodec_decode_audio(s, ctxIndex);
+    }
+
+/*  qemu_mutex_unlock(&s->thread_mutex); */
+    qemu_mutex_lock(&s->thread_mutex);
+
+    TRACE("Leave, %s\n", __func__);
+
+    return len;
+}
+
+int encode_codec(SVCodecState *s)
+{
+    AVCodecContext *avctx;
+    uint32_t len = 0, ctxIndex;
+
+    TRACE("Enter, %s\n", __func__);
+
+    qemu_mutex_unlock(&s->thread_mutex);
+/*  qemu_mutex_lock(&s->thread_mutex); */
+
+    ctxIndex = s->codecParam.ctxIndex;
+    avctx = s->ctxArr[ctxIndex].avctx;
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        len = qemu_avcodec_encode_video(s, ctxIndex);
+    } else {
+        len = qemu_avcodec_encode_audio(s, ctxIndex);
+    }
+
+/*  qemu_mutex_unlock(&s->thread_mutex); */
+    qemu_mutex_lock(&s->thread_mutex);
+
+    TRACE("Leave, %s\n", __func__);
+
+    return len;
+}
 
-static int qemu_serialize_rational(const AVRational* elem, uint8_t* buff)
+static int qemu_serialize_rational(const AVRational *elem, uint8_t *buff)
 {
     int size = 0;
 
@@ -64,11 +174,11 @@ static int qemu_serialize_rational(const AVRational* elem, uint8_t* buff)
     return size;
 }
 
-static int qemu_deserialize_rational(const uint8_t* buff, AVRational* elem)
+static int qemu_deserialize_rational(const uint8_t *buff, AVRational *elem)
 {
     int size = 0;
 
-//    memset(elem, 0, sizeof(*elem));
+    memset(elem, 0, sizeof(*elem));
 
     memcpy(&elem->num, buff + size, sizeof(elem->num));
     size += sizeof(elem->num);
@@ -78,9 +188,9 @@ static int qemu_deserialize_rational(const uint8_t* buff, AVRational* elem)
     return size;
 }
 
-static int qemu_serialize_frame (const AVFrame* elem, uint8_t* buff)
+static int qemu_serialize_frame(const AVFrame *elem, uint8_t *buff)
 {
-       int size = 0;
+    int size = 0;
 
     memcpy(buff + size, &elem->key_frame, sizeof(elem->key_frame));
     size += sizeof(elem->key_frame);
@@ -88,9 +198,11 @@ static int qemu_serialize_frame (const AVFrame* elem, uint8_t* buff)
     size += sizeof(elem->pict_type);
     memcpy(buff + size, &elem->pts, sizeof(elem->pts));
     size += sizeof(elem->pts);
-    memcpy(buff + size, &elem->coded_picture_number, sizeof(elem->coded_picture_number));
+    memcpy(buff + size, &elem->coded_picture_number,
+            sizeof(elem->coded_picture_number));
     size += sizeof(elem->coded_picture_number);
-    memcpy(buff + size, &elem->display_picture_number, sizeof(elem->display_picture_number));
+    memcpy(buff + size, &elem->display_picture_number,
+            sizeof(elem->display_picture_number));
     size += sizeof(elem->display_picture_number);
     memcpy(buff + size, &elem->quality, sizeof(elem->quality));
     size += sizeof(elem->quality);
@@ -98,21 +210,23 @@ static int qemu_serialize_frame (const AVFrame* elem, uint8_t* buff)
     size += sizeof(elem->age);
     memcpy(buff + size, &elem->reference, sizeof(elem->reference));
     size += sizeof(elem->reference);
-    memcpy(buff + size, &elem->reordered_opaque, sizeof(elem->reordered_opaque));
+    memcpy(buff + size, &elem->reordered_opaque,
+            sizeof(elem->reordered_opaque));
     size += sizeof(elem->reordered_opaque);
     memcpy(buff + size, &elem->repeat_pict, sizeof(elem->repeat_pict));
     size += sizeof(elem->repeat_pict);
-    memcpy(buff + size, &elem->interlaced_frame, sizeof(elem->interlaced_frame));
+    memcpy(buff + size, &elem->interlaced_frame,
+            sizeof(elem->interlaced_frame));
     size += sizeof(elem->interlaced_frame);
 
-       return size;
+    return size;
 }
 
-static int qemu_deserialize_frame (const uint8_t* buff, AVFrame* elem)
+static int qemu_deserialize_frame(const uint8_t *buff, AVFrame *elem)
 {
     int size = 0;
 
-//    memset(elem, 0, sizeof(*elem));
+    memset(elem, 0, sizeof(*elem));
 
     memcpy(&elem->linesize, buff + size, sizeof(elem->linesize));
     size += sizeof(elem->linesize);
@@ -122,9 +236,11 @@ static int qemu_deserialize_frame (const uint8_t* buff, AVFrame* elem)
     size += sizeof(elem->pict_type);
     memcpy(&elem->pts, buff + size, sizeof(elem->pts));
     size += sizeof(elem->pts);
-    memcpy(&elem->coded_picture_number, buff + size, sizeof(elem->coded_picture_number));
+    memcpy(&elem->coded_picture_number, buff + size,
+            sizeof(elem->coded_picture_number));
     size += sizeof(elem->coded_picture_number);
-    memcpy(&elem->display_picture_number, buff + size, sizeof(elem->display_picture_number));
+    memcpy(&elem->display_picture_number, buff + size,
+            sizeof(elem->display_picture_number));
     size += sizeof(elem->display_picture_number);
     memcpy(&elem->quality, buff + size, sizeof(elem->quality));
     size += sizeof(elem->quality);
@@ -134,7 +250,8 @@ static int qemu_deserialize_frame (const uint8_t* buff, AVFrame* elem)
     size += sizeof(elem->reference);
     memcpy(&elem->qstride, buff + size, sizeof(elem->qstride));
     size += sizeof(elem->qstride);
-    memcpy(&elem->motion_subsample_log2, buff + size, sizeof(elem->motion_subsample_log2));
+    memcpy(&elem->motion_subsample_log2, buff + size,
+            sizeof(elem->motion_subsample_log2));
     size += sizeof(elem->motion_subsample_log2);
     memcpy(&elem->error, buff + size, sizeof(elem->error));
     size += sizeof(elem->error);
@@ -144,37 +261,40 @@ static int qemu_deserialize_frame (const uint8_t* buff, AVFrame* elem)
     size += sizeof(elem->repeat_pict);
     memcpy(&elem->qscale_type, buff + size, sizeof(elem->qscale_type));
     size += sizeof(elem->qscale_type);
-    memcpy(&elem->interlaced_frame, buff + size, sizeof(elem->interlaced_frame));
+    memcpy(&elem->interlaced_frame, buff + size,
+            sizeof(elem->interlaced_frame));
     size += sizeof(elem->interlaced_frame);
     memcpy(&elem->top_field_first, buff + size, sizeof(elem->top_field_first));
     size += sizeof(elem->top_field_first);
-    memcpy(&elem->palette_has_changed, buff + size, sizeof(elem->palette_has_changed));
+    memcpy(&elem->palette_has_changed, buff + size,
+            sizeof(elem->palette_has_changed));
     size += sizeof(elem->palette_has_changed);
     memcpy(&elem->buffer_hints, buff + size, sizeof(elem->buffer_hints));
     size += sizeof(elem->buffer_hints);
-    memcpy(&elem->reordered_opaque, buff + size, sizeof(elem->reordered_opaque));
+    memcpy(&elem->reordered_opaque, buff + size,
+            sizeof(elem->reordered_opaque));
     size += sizeof(elem->reordered_opaque);
 
     return size;
 }
 
-void qemu_parser_init (SVCodecState *s, int ctxIndex)
+void qemu_parser_init(SVCodecState *s, int ctxIndex)
 {
     TRACE("[%s] Enter\n", __func__);
 
-    s->ctxArr[ctxIndex].pParserBuffer = NULL;
-    s->ctxArr[ctxIndex].bParser = false;
+    s->ctxArr[ctxIndex].parserBuffer = NULL;
+    s->ctxArr[ctxIndex].parser_use = false;
 
     TRACE("[%s] Leave\n", __func__);
 }
 
-void qemu_codec_close (SVCodecState *s, uint32_t value)
+void qemu_codec_close(SVCodecState *s, uint32_t value)
 {
     int i;
     int ctxIndex = 0;
 
     TRACE("[%s] Enter\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
     for (i = 0; i < CODEC_MAX_CONTEXT; i++) {
         if (s->ctxArr[i].nFileValue == value) {
@@ -187,11 +307,13 @@ void qemu_codec_close (SVCodecState *s, uint32_t value)
     s->ctxArr[ctxIndex].bUsed = false;
     qemu_parser_init(s, ctxIndex);
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
+
     TRACE("[%s] Leave\n", __func__);
 }
 
-void qemu_restore_context (AVCodecContext *dst, AVCodecContext *src)
+#ifndef CODEC_COPY_DATA
+void qemu_restore_context(AVCodecContext *dst, AVCodecContext *src)
 {
     TRACE("[%s] Enter\n", __func__);
 
@@ -225,29 +347,36 @@ void qemu_restore_context (AVCodecContext *dst, AVCodecContext *src)
 
     TRACE("[%s] Leave\n", __func__);
 }
+#endif
 
-void qemu_get_codec_ver (SVCodecState *s, int ctxIndex)
+void qemu_get_codec_ver(SVCodecState *s)
 {
     char codec_ver[32];
     off_t offset;
+    int ctxIndex;
 
     offset = s->codecParam.mmapOffset;
+    ctxIndex = s->codecParam.ctxIndex;
+
+    printf("offset:%d\n", offset);
 
     memset(codec_ver, 0, 32);
-       pstrcpy(codec_ver, sizeof(codec_ver), MARU_CODEC_VERSION);
+    pstrcpy(codec_ver, sizeof(codec_ver), MARU_CODEC_VERSION);
+
     TRACE("%d of codec_version:%s\n", ctxIndex, codec_ver);
-    memcpy((uint8_t*)s->vaddr + offset, codec_ver, 32);
+    memcpy((uint8_t *)s->vaddr + offset, codec_ver, sizeof(codec_ver));
 }
 
 /* void av_register_all() */
-void qemu_av_register_all (void)
+void qemu_av_register_all(void)
 {
     av_register_all();
     TRACE("av_register_all\n");
 }
 
-/* int avcodec_default_get_buffer (AVCodecContext *s, AVFrame *pic) */
-int qemu_avcodec_get_buffer (AVCodecContext *context, AVFrame *picture)
+#if 0
+/* int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic) */
+int qemu_avcodec_get_buffer(AVCodecContext *context, AVFrame *picture)
 {
     int ret;
     TRACE("avcodec_default_get_buffer\n");
@@ -260,15 +389,16 @@ int qemu_avcodec_get_buffer (AVCodecContext *context, AVFrame *picture)
     return ret;
 }
 
-/* void avcodec_default_release_buffer (AVCodecContext *ctx, AVFrame *frame) */
-void qemu_avcodec_release_buffer (AVCodecContext *context, AVFrame *picture)
+/* void avcodec_default_release_buffer(AVCodecContext *ctx, AVFrame *frame) */
+void qemu_avcodec_release_buffer(AVCodecContext *context, AVFrame *picture)
 {
     TRACE("avcodec_default_release_buffer\n");
     avcodec_default_release_buffer(context, picture);
 }
+#endif
 
-/* int avcodec_open (AVCodecContext *avctx, AVCodec *codec) */
-int qemu_avcodec_open (SVCodecState *s, int ctxIndex)
+/* int avcodec_open(AVCodecContext *avctx, AVCodec *codec) */
+int qemu_avcodec_open(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
 #ifndef CODEC_COPY_DATA
@@ -281,12 +411,12 @@ int qemu_avcodec_open (SVCodecState *s, int ctxIndex)
     int bEncode = 0;
     int size = 0;
 
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (!avctx) {
         ERR("[%s] %d of AVCodecContext is NULL!\n", __func__, ctxIndex);
-        pthread_mutex_unlock(&s->codec_mutex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return ret;
     }
 
@@ -297,66 +427,80 @@ int qemu_avcodec_open (SVCodecState *s, int ctxIndex)
 #ifndef CODEC_COPY_DATA
     size = sizeof(AVCodecContext);
     memcpy(&tempCtx, avctx, size);
-    memcpy(avctx, (uint8_t*)s->vaddr + offset, size);
+    memcpy(avctx, (uint8_t *)s->vaddr + offset, size);
     qemu_restore_context(avctx, &tempCtx);
-    memcpy(&codec_id, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&codec_id, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&bEncode, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&bEncode, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
 #else
-    memcpy(&avctx->bit_rate, (uint8_t*)s->vaddr + offset, sizeof(int));
+    memcpy(&avctx->bit_rate, (uint8_t *)s->vaddr + offset, sizeof(int));
     size = sizeof(int);
-    memcpy(&avctx->bit_rate_tolerance, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->bit_rate_tolerance,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->flags, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->flags, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    size += qemu_deserialize_rational((uint8_t*)s->vaddr + offset + size, &avctx->time_base);
-    memcpy(&avctx->width, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    size += qemu_deserialize_rational((uint8_t *)s->vaddr + offset + size,
+                                        &avctx->time_base);
+    memcpy(&avctx->width, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->height, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->height, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->gop_size, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->gop_size, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->pix_fmt, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->pix_fmt, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->sample_rate, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->sample_rate,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->channels, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->channels, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->codec_tag, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->codec_tag, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->block_align, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->block_align,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->rc_strategy, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->rc_strategy,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->strict_std_compliance, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->strict_std_compliance,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->rc_qsquish, (uint8_t*)s->vaddr + offset + size, sizeof(float));
+    memcpy(&avctx->rc_qsquish,
+            (uint8_t *)s->vaddr + offset + size, sizeof(float));
     size += sizeof(float);
-    size += qemu_deserialize_rational((uint8_t*)s->vaddr + offset + size, &avctx->sample_aspect_ratio);
-    memcpy(&avctx->qmin, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    size += qemu_deserialize_rational((uint8_t *)s->vaddr + offset + size,
+            &avctx->sample_aspect_ratio);
+    memcpy(&avctx->qmin, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->qmax, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->qmax, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->pre_me, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->pre_me, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->trellis, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->trellis, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&avctx->extradata_size, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->extradata_size,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&codec_id, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&codec_id, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
-    memcpy(&bEncode, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&bEncode, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
 #endif
-    TRACE("Context Index:%d, width:%d, height:%d\n", ctxIndex, avctx->width, avctx->height);
+    TRACE("Context Index:%d, width:%d, height:%d\n",
+            ctxIndex, avctx->width, avctx->height);
 
     if (avctx->extradata_size > 0) {
-        avctx->extradata = (uint8_t*)av_malloc(avctx->extradata_size);
-        memcpy(avctx->extradata, (uint8_t*)s->vaddr + offset + size, avctx->extradata_size);
+        avctx->extradata = (uint8_t *)av_malloc(avctx->extradata_size +
+                             MARU_ROUND_UP_16(FF_INPUT_BUFFER_PADDING_SIZE));
+        memcpy(avctx->extradata,
+                (uint8_t *)s->vaddr + offset + size, avctx->extradata_size);
+        size += avctx->extradata_size;
     } else {
         TRACE("[%s] allocate dummy extradata\n", __func__);
-        avctx->extradata = av_mallocz (MARU_ROUND_UP_16(FF_INPUT_BUFFER_PADDING_SIZE));
+        avctx->extradata =
+                av_mallocz(MARU_ROUND_UP_16(FF_INPUT_BUFFER_PADDING_SIZE));
     }
 
     if (bEncode) {
@@ -371,8 +515,10 @@ int qemu_avcodec_open (SVCodecState *s, int ctxIndex)
         ERR("[%s] failed to find codec of %d\n", __func__, codec_id);
     }
 
+#if 0
     avctx->get_buffer = qemu_avcodec_get_buffer;
     avctx->release_buffer = qemu_avcodec_release_buffer;
+#endif
 
     ret = avcodec_open(avctx, codec);
     if (ret != 0) {
@@ -385,91 +531,96 @@ int qemu_avcodec_open (SVCodecState *s, int ctxIndex)
     }
 
 #ifndef CODEC_COPY_DATA
-    memcpy((uint8_t*)s->vaddr + offset, avctx, sizeof(AVCodecContext));
-    memcpy((uint8_t*)s->vaddr + offset + sizeof(AVCodecContext), &ret, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset, avctx, sizeof(AVCodecContext));
+    memcpy((uint8_t *)s->vaddr + offset + sizeof(AVCodecContext),
+            &ret, sizeof(int));
 #else
-    memcpy((uint8_t*)s->vaddr + offset, &avctx->pix_fmt, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset, &avctx->pix_fmt, sizeof(int));
     size = sizeof(int);
-    size += qemu_serialize_rational(&avctx->time_base, (uint8_t*)s->vaddr + offset + size);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->channels, sizeof(int));
+    size += qemu_serialize_rational(&avctx->time_base,
+            (uint8_t *)s->vaddr + offset + size);
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->channels, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->sample_fmt, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->sample_fmt, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->codec_type, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->codec_type, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->codec_id, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->codec_id, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->coded_width, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->coded_width, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->coded_height, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->coded_height, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->ticks_per_frame, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->ticks_per_frame, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->chroma_sample_location, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->chroma_sample_location, sizeof(int));
     size += sizeof(int);
 #if 0
-    memcpy((uint8_t*)s->vaddr + offset + size, avctx->priv_data, codec->priv_data_size);
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            avctx->priv_data, codec->priv_data_size);
     size += codec->priv_data_size;
 #endif
-    memcpy((uint8_t*)s->vaddr + offset + size, &ret, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &ret, sizeof(int));
+    size += sizeof(int);
 #endif
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
+
+    TRACE("Leave, %s\n", __func__);
     return ret;
 }
 
-/* int avcodec_close (AVCodecContext *avctx) */
-int qemu_avcodec_close (SVCodecState* s, int ctxIndex)
+/* int avcodec_close(AVCodecContext *avctx) */
+int qemu_avcodec_close(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
     off_t offset;
     int ret = -1;
 
-    TRACE("Enter\n");
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("Enter, %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
     offset = s->codecParam.mmapOffset;
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (!avctx) {
         ERR("[%s] %d of AVCodecContext is NULL\n", __func__, ctxIndex);
-        memcpy((uint8_t*)s->vaddr + offset, &ret, sizeof(int));
-           pthread_mutex_unlock(&s->codec_mutex);
+        memcpy((uint8_t *)s->vaddr + offset, &ret, sizeof(int));
+        qemu_mutex_unlock(&s->thread_mutex);
         return ret;
     }
 
-#if 0
-    if ((avctx->codec_type == AVMEDIA_TYPE_VIDEO) && gTempBuffer) {
-        av_free(gTempBuffer);
-        gTempBuffer = NULL;
-    }
-#endif
-
     ret = avcodec_close(avctx);
     TRACE("after avcodec_close. ret:%d\n", ret);
 
-    memcpy((uint8_t*)s->vaddr + offset, &ret, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset, &ret, sizeof(int));
+
+    qemu_mutex_unlock(&s->thread_mutex);
 
-    pthread_mutex_unlock(&s->codec_mutex);
     TRACE("[%s] Leave\n", __func__);
     return ret;
 }
 
 /* AVCodecContext* avcodec_alloc_context (void) */
-void qemu_avcodec_alloc_context (SVCodecState* s)
+void qemu_avcodec_alloc_context(SVCodecState *s)
 {
     off_t offset;
     int index;
 
     TRACE("[%s] Enter\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
     offset = s->codecParam.mmapOffset;
 
     for (index = 0; index < CODEC_MAX_CONTEXT; index++) {
         if (s->ctxArr[index].bUsed == false) {
             TRACE("[%s] Succeeded to get context[%d].\n", __func__, index);
-            ctxArrIndex = index;
             break;
         }
         TRACE("[%s] Failed to get context[%d].\n", __func__, index);
@@ -478,121 +629,103 @@ void qemu_avcodec_alloc_context (SVCodecState* s)
     if (index == CODEC_MAX_CONTEXT) {
         ERR("[%s] Failed to get available codec context from now\n", __func__);
         ERR("[%s] Try to run codec again\n", __func__);
+        qemu_mutex_unlock(&s->thread_mutex);
         return;
     }
 
-    TRACE("[%s] context index :%d.\n", __func__, ctxArrIndex);
-
-    s->ctxArr[ctxArrIndex].pAVCtx = avcodec_alloc_context();
-    s->ctxArr[ctxArrIndex].nFileValue = s->codecParam.fileIndex;
-    s->ctxArr[ctxArrIndex].bUsed = true;
-    memcpy((uint8_t*)s->vaddr + offset, &ctxArrIndex, sizeof(int));
-    qemu_parser_init(s, ctxArrIndex);
-
-    pthread_mutex_unlock(&s->codec_mutex);
-
-    TRACE("[%s] Leave\n", __func__);
-}
+    TRACE("[%s] context index :%d.\n", __func__, index);
+    s->ctxArr[index].avctx = avcodec_alloc_context();
+    s->ctxArr[index].nFileValue = s->codecParam.fileIndex;
+    s->ctxArr[index].bUsed = true;
+    memcpy((uint8_t *)s->vaddr + offset, &index, sizeof(int));
+    qemu_parser_init(s, index);
 
-/* AVFrame *avcodec_alloc_frame (void) */
-void qemu_avcodec_alloc_frame (SVCodecState* s)
-{
-    TRACE("[%s] Enter\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
 
-    s->ctxArr[ctxArrIndex].pFrame = avcodec_alloc_frame();
-    pthread_mutex_unlock(&s->codec_mutex);
     TRACE("[%s] Leave\n", __func__);
 }
 
-/* void av_free (void *ptr) */
-void qemu_av_free_context (SVCodecState* s, int ctxIndex)
+/* AVFrame *avcodec_alloc_frame(void) */
+void qemu_avcodec_alloc_frame(SVCodecState *s, int ctxIndex)
 {
-    AVCodecContext *avctx;
+    AVFrame *frame = NULL;
 
-    pthread_mutex_lock(&s->codec_mutex);
-
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
-    if (avctx) {
-        av_free(avctx);
-        s->ctxArr[ctxIndex].pAVCtx = NULL;
-    }
-    pthread_mutex_unlock(&s->codec_mutex);
-    TRACE("free AVCodecContext\n");
-}
-
-void qemu_av_free_picture (SVCodecState* s, int ctxIndex)
-{
-    AVFrame *avframe;
-
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("[%s] Enter\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avframe = s->ctxArr[ctxIndex].pFrame;
-    if (avframe) {
-        av_free(avframe);
-        s->ctxArr[ctxIndex].pFrame = NULL;
+    frame = avcodec_alloc_frame();
+    if (!frame) {
+        ERR("[%s] Failed to allocate frame\n", __func__);
     }
+    s->ctxArr[ctxIndex].frame = frame;
 
-    pthread_mutex_unlock(&s->codec_mutex);
-    TRACE("free AVFrame\n");
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("[%s] Leave\n", __func__);
 }
 
-void qemu_av_free_palctrl (SVCodecState* s, int ctxIndex)
+/* void av_free(void *ptr) */
+void qemu_av_free(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
+    AVFrame *avframe;
 
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("enter %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    avctx = s->ctxArr[ctxIndex].avctx;
+    avframe = s->ctxArr[ctxIndex].frame;
 
-    if (avctx->palctrl) {
+    if (avctx && avctx->palctrl) {
         av_free(avctx->palctrl);
         avctx->palctrl = NULL;
     }
-    pthread_mutex_unlock(&s->codec_mutex);
-    TRACE("free AVCodecContext palctrl\n");
-}
-
-void qemu_av_free_extradata (SVCodecState* s, int ctxIndex)
-{
-    AVCodecContext *avctx;
-
-    pthread_mutex_lock(&s->codec_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
-    if (avctx && avctx->extradata && avctx->extradata_size > 0) {
+    if (avctx && avctx->extradata) {
+        TRACE("free extradata\n");
         av_free(avctx->extradata);
         avctx->extradata = NULL;
     }
 
-    pthread_mutex_unlock(&s->codec_mutex);
-    TRACE("free AVCodecContext extradata\n");
+    if (avctx) {
+        TRACE("free codec context\n");
+        av_free(avctx);
+        s->ctxArr[ctxIndex].avctx = NULL;
+    }
+
+    if (avframe) {
+        TRACE("free codec frame\n");
+        av_free(avframe);
+        s->ctxArr[ctxIndex].frame = NULL;
+    }
+
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("leave %s\n", __func__);
 }
 
-/* void avcodec_flush_buffers (AVCodecContext *avctx) */
-void qemu_avcodec_flush_buffers (SVCodecState* s, int ctxIndex)
+/* void avcodec_flush_buffers(AVCodecContext *avctx) */
+void qemu_avcodec_flush_buffers(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
 
     TRACE("Enter\n");
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (avctx) {
         avcodec_flush_buffers(avctx);
     } else {
         ERR("[%s] %d of AVCodecContext is NULL\n", __func__, ctxIndex);
     }
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
     TRACE("[%s] Leave\n", __func__);
 }
 
-/* int avcodec_decode_video (AVCodecContext *avctx, AVFrame *picture,
+/* int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  *                          int *got_picture_ptr, const uint8_t *buf,
  *                          int buf_size)
  */
-int qemu_avcodec_decode_video (SVCodecState* s, int ctxIndex)
+int qemu_avcodec_decode_video(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
 #ifndef CODEC_COPY_DATA
@@ -602,55 +735,59 @@ int qemu_avcodec_decode_video (SVCodecState* s, int ctxIndex)
     AVPacket avpkt;
     int got_picture_ptr;
     uint8_t *buf;
-    uint8_t *pParserBuffer;
-    bool bParser;
+    uint8_t *parserBuffer;
+    bool parser_use;
     int buf_size;
     int size = 0;
     int ret = -1;
     off_t offset;
 
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("Enter, %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
     TRACE("[%s] Video Context Index : %d\n", __func__, ctxIndex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
-    picture = s->ctxArr[ctxIndex].pFrame;
+    avctx = s->ctxArr[ctxIndex].avctx;
+    picture = s->ctxArr[ctxIndex].frame;
     if (!avctx || !picture) {
-        ERR("[%s] %d of AVCodecContext or AVFrame is NULL!\n", __func__, ctxIndex);
-        pthread_mutex_unlock(&s->codec_mutex);
+        ERR("[%s] %d of Context or Frame is NULL!\n", __func__, ctxIndex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return ret;
     }
 
     offset = s->codecParam.mmapOffset;
 
-    pParserBuffer = s->ctxArr[ctxIndex].pParserBuffer;
-    bParser = s->ctxArr[ctxIndex].bParser;
-    TRACE("[%s] Parser Buffer : %p, Parser:%d\n", __func__, pParserBuffer, bParser);
+    parserBuffer = s->ctxArr[ctxIndex].parserBuffer;
+    parser_use = s->ctxArr[ctxIndex].parser_use;
+    TRACE("[%s] Parser Buffer : %p, Parser:%d\n", __func__,
+            parserBuffer, parser_use);
 
 #ifndef CODEC_COPY_DATA
     memcpy(&tmpCtx, avctx, sizeof(AVCodecContext));
-    memcpy(avctx, (uint8_t*)s->vaddr + offset, sizeof(AVCodecContext));
+    memcpy(avctx, (uint8_t *)s->vaddr + offset, sizeof(AVCodecContext));
     size = sizeof(AVCodecContext);
     qemu_restore_context(avctx, &tmpCtx);
 #else
-    memcpy(&avctx->reordered_opaque, (uint8_t*)s->vaddr + offset, sizeof(int64_t));
+    memcpy(&avctx->reordered_opaque,
+            (uint8_t *)s->vaddr + offset, sizeof(int64_t));
     size = sizeof(int64_t);
-    memcpy(&avctx->skip_frame, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&avctx->skip_frame,
+            (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
 #endif
-    memcpy(&buf_size, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&buf_size, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
 
     picture->reordered_opaque = avctx->reordered_opaque;
-    TRACE("input buffer size : %d\n", buf_size);
 
-    if (pParserBuffer && bParser) {
-        buf = pParserBuffer;
+    if (parserBuffer && parser_use) {
+        buf = parserBuffer;
     } else if (buf_size > 0) {
         TRACE("[%s] not use parser, codec_id:%x\n", __func__, avctx->codec_id);
-//      buf = av_mallocz(buf_size);
-//      memcpy(buf, (uint8_t*)s->vaddr + offset + size, buf_size);
-        buf = (uint8_t*)s->vaddr + offset + size;
+/*      buf = av_mallocz(buf_size);
+        memcpy(buf, (uint8_t *)s->vaddr + offset + size, buf_size); */
+        buf = (uint8_t *)s->vaddr + offset + size;
+        size += buf_size;
     } else {
         TRACE("There is no input buffer\n");
         buf = NULL;
@@ -659,116 +796,127 @@ int qemu_avcodec_decode_video (SVCodecState* s, int ctxIndex)
     memset(&avpkt, 0, sizeof(AVPacket));
     avpkt.data = buf;
     avpkt.size = buf_size;
+    TRACE("packet buf:%p, size:%d\n", buf, buf_size);
 
-    TRACE("[%s] before avcodec_decode_video\n", __func__);
     ret = avcodec_decode_video2(avctx, picture, &got_picture_ptr, &avpkt);
-    TRACE("[%s] after avcodec_decode_video, ret:%d\n", __func__, ret);
 
+    TRACE("[%s] after decoding video, ret:%d\n", __func__, ret);
     if (ret < 0) {
         ERR("[%s] failed to decode video!!, ret:%d\n", __func__, ret);
-    }
-    if (got_picture_ptr == 0) {
-        TRACE("[%s] There is no frame\n", __func__);
+    } else {
+        if (ret == 0) {
+            INFO("[%s] no frame\n", __func__);
+        }
+        TRACE("decoded frame number:%d\n", avctx->frame_number);
     }
 
 #ifndef CODEC_COPY_DATA
     size = sizeof(AVCodecContext);
-    memcpy((uint8_t*)s->vaddr + offset, avctx, size);
+    memcpy((uint8_t *)s->vaddr + offset, avctx, size);
 #else
-    memcpy((uint8_t*)s->vaddr + offset, &avctx->pix_fmt, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset, &avctx->pix_fmt, sizeof(int));
     size = sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->time_base, sizeof(AVRational));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->time_base, sizeof(AVRational));
     size += sizeof(AVRational);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->width, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->width, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->height, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->height, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->has_b_frames, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->has_b_frames, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->frame_number, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->frame_number, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->sample_aspect_ratio, sizeof(AVRational));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->sample_aspect_ratio, sizeof(AVRational));
     size += sizeof(AVRational);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->internal_buffer_count, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->internal_buffer_count, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->profile, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->profile, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->level, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->level, sizeof(int));
     size += sizeof(int);
 #endif
-       size += qemu_serialize_frame(picture, (uint8_t*)s->vaddr + offset + size);
+    size += qemu_serialize_frame(picture, (uint8_t *)s->vaddr + offset + size);
 
-    memcpy((uint8_t*)s->vaddr + offset + size, &got_picture_ptr, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &got_picture_ptr, sizeof(int));
+    size += sizeof(int);
+    memcpy((uint8_t *)s->vaddr + offset + size, &ret, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &ret, sizeof(int));
-
-//  av_free(buf);
 
 #if 0
-    if (pParserBuffer && bParser) {
+    memcpy((uint8_t *)s->vaddr + offset + size, dst.data[0], numbytes);
+    av_free(buffer);
+
+/*    av_free(buf); */
+
+    if (parserBuffer && parser_use) {
         TRACE("[%s] Free input buffer after decoding video\n", __func__);
-        TRACE("[%s] input buffer : %p, %p\n", __func__, avpkt.data, pParserBuffer);
+        TRACE("[%s] input buffer : %p, %p\n",
+            __func__, avpkt.data, parserBuffer);
         av_free(avpkt.data);
-        s->ctxArr[ctxIndex].pParserBuffer = NULL;
+        s->ctxArr[ctxIndex].parserBuffer = NULL;
     }
 #endif
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("Leave, %s\n", __func__);
+
     return ret;
 }
 
-/* int avcodec_encode_video (AVCodecContext *avctx, uint8_t *buf,
+/* int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf,
  *                          int buf_size, const AVFrame *pict)
  */
-int qemu_avcodec_encode_video (SVCodecState* s, int ctxIndex)
+int qemu_avcodec_encode_video(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx = NULL;
     AVFrame *pict = NULL;
     uint8_t *inputBuf = NULL;
-//  uint8_t *outBuf = NULL;
-    int outputBufSize = 0;
+    int outbufSize = 0;
     int numBytes = 0;
     int bPict = -1;
     int size = 0;
     int ret = -1;
     off_t offset;
 
-    pthread_mutex_lock(&s->codec_mutex);
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
-    pict = s->ctxArr[ctxIndex].pFrame;
+    TRACE("Enter, %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
+
+    avctx = s->ctxArr[ctxIndex].avctx;
+    pict = s->ctxArr[ctxIndex].frame;
     if (!avctx || !pict) {
-        ERR("[%s] %d of AVCodecContext or AVFrame is NULL\n", __func__, ctxIndex);
-        pthread_mutex_unlock(&s->codec_mutex);
+        ERR("[%s] %d of Context or Frame is NULL\n", __func__, ctxIndex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return ret;
     }
 
     offset = s->codecParam.mmapOffset;
 
     size = sizeof(int);
-    memcpy(&bPict, (uint8_t*)s->vaddr + offset, size);
+    memcpy(&bPict, (uint8_t *)s->vaddr + offset, size);
     TRACE("[%s] avframe is :%d\n", __func__, bPict);
 
     if (bPict == 0) {
-        memcpy(&outputBufSize, (uint8_t*)s->vaddr + offset + size, size);
+        memcpy(&outbufSize, (uint8_t *)s->vaddr + offset + size, size);
         size += sizeof(int);
-               size += qemu_deserialize_frame((uint8_t*)s->vaddr + offset + size, pict);
+        size +=
+            qemu_deserialize_frame((uint8_t *)s->vaddr + offset + size, pict);
 
-        numBytes = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
+        numBytes =
+            avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
         TRACE("[%s] input buffer size :%d\n", __func__, numBytes);
 
-        inputBuf = (uint8_t*)s->vaddr + offset + size;
+        inputBuf = (uint8_t *)s->vaddr + offset + size;
         if (!inputBuf) {
             ERR("[%s] failed to get input buffer\n", __func__);
             return ret;
         }
-#if 0
-        outBuf = av_malloc(outputBufSize);
-        if (!outBuf) {
-            ERR("failed to allocate output buffer as many as :%d\n", outputBufSize);
-        }
-#endif
         ret = avpicture_fill((AVPicture*)pict, inputBuf, avctx->pix_fmt,
-                avctx->width, avctx->height);
+                            avctx->width, avctx->height);
         if (ret < 0) {
             ERR("after avpicture_fill, ret:%d\n", ret);
         }
@@ -779,75 +927,77 @@ int qemu_avcodec_encode_video (SVCodecState* s, int ctxIndex)
         pict = NULL;
     }
 
-    TRACE("before encoding video\n");
-    ret = avcodec_encode_video (avctx, (uint8_t*)s->vaddr + offset, outputBufSize, pict);
-//  ret = avcodec_encode_video (avctx, outBuf, outputBufSize, pict);
-    TRACE("after encoding video, ret:%d, pts:%lld\n", ret, pict->pts);
+    ret = avcodec_encode_video(avctx, (uint8_t *)s->vaddr + offset,
+                                outbufSize, pict);
+    TRACE("encode video, ret:%d, pts:%lld, outbuf size:%d\n",
+            ret, pict->pts, outbufSize);
 
     if (ret < 0) {
-        ERR("[%d] failed to encode video, ret:%d\n", __LINE__, ret);
+        ERR("[%d] failed to encode video.\n", __LINE__);
     }
 
-//  memcpy((uint8_t*)s->vaddr + offset, outBuf, sizeof(int));
-//  size = outputBufSize;
-    memcpy((uint8_t*)s->vaddr + offset + outputBufSize, &ret, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + outbufSize, &ret, sizeof(int));
+
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("Leave, %s\n", __func__);
 
-    pthread_mutex_unlock(&s->codec_mutex);
     return ret;
 }
 
 /* 
- *  int avcodec_decode_audio3 (AVCodecContext *avctx, int16_t *samples,
- *                             int *frame_size_ptr, AVPacket *avpkt)
+ *  int avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
+ *                            int *frame_size_ptr, AVPacket *avpkt)
  */
-int qemu_avcodec_decode_audio (SVCodecState *s, int ctxIndex)
+int qemu_avcodec_decode_audio(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext *avctx;
     AVPacket avpkt;
     int16_t *samples;
     int frame_size_ptr;
     uint8_t *buf;
-    uint8_t *pParserBuffer;
-    bool bParser;
+    uint8_t *parserBuffer;
+    bool parser_use;
     int buf_size, outbuf_size;
     int size;
     int ret = -1;
     off_t offset;
 
-    TRACE("Audio Context Index : %d\n", ctxIndex);
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("Enter, %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    TRACE("Audio Context Index : %d\n", ctxIndex);
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (!avctx) {
-        ERR("[%s] %d of AVCodecContext is NULL!\n", __func__, ctxIndex);
-        pthread_mutex_unlock(&s->codec_mutex);
+        ERR("[%s] %d of Context is NULL!\n", __func__, ctxIndex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return ret;
     }
 
-       if (!avctx->codec) {
-               ERR("[%s] %d of AVCodec is NULL\n", __func__, ctxIndex);
-               pthread_mutex_unlock(&s->codec_mutex);
-               return ret;
-       }
+    if (!avctx->codec) {
+        ERR("[%s] %d of Codec is NULL\n", __func__, ctxIndex);
+        qemu_mutex_unlock(&s->thread_mutex);
+        return ret;
+    }
 
     offset = s->codecParam.mmapOffset;
 
-    pParserBuffer = s->ctxArr[ctxIndex].pParserBuffer;
-    bParser = s->ctxArr[ctxIndex].bParser;
-    TRACE("[%s] Parser Buffer : %p, Parser:%d\n", __func__, pParserBuffer, bParser);
+    parserBuffer = s->ctxArr[ctxIndex].parserBuffer;
+    parser_use = s->ctxArr[ctxIndex].parser_use;
 
-    memcpy(&buf_size, (uint8_t*)s->vaddr + offset, sizeof(int));
+    memcpy(&buf_size, (uint8_t *)s->vaddr + offset, sizeof(int));
     size = sizeof(int);
     TRACE("input buffer size : %d\n", buf_size);
 
-    if (pParserBuffer && bParser) {
-        TRACE("[%s] use parser, buf:%p codec_id:%x\n", __func__, pParserBuffer, avctx->codec_id);
-        buf = pParserBuffer;
+    if (parserBuffer && parser_use) {
+        TRACE("[%s] use parser, buf:%p codec_id:%x\n",
+                __func__, parserBuffer, avctx->codec_id);
+        buf = parserBuffer;
     } else if (buf_size > 0) {
         TRACE("[%s] not use parser, codec_id:%x\n", __func__, avctx->codec_id);
-        buf = (uint8_t*)s->vaddr + offset + size;
+        buf = (uint8_t *)s->vaddr + offset + size;
+        size += buf_size;
     } else {
-        TRACE("There is no input buffer\n");
+        TRACE("no input buffer\n");
         buf = NULL;
     }
 
@@ -860,26 +1010,29 @@ int qemu_avcodec_decode_audio (SVCodecState *s, int ctxIndex)
     samples = av_malloc(frame_size_ptr);
 
     ret = avcodec_decode_audio3(avctx, samples, &frame_size_ptr, &avpkt);
-    TRACE("After decoding audio!, ret:%d\n", ret);
+    TRACE("after decoding audio!, ret:%d\n", ret);
 
 #ifndef CODEC_COPY_DATA
     size = sizeof(AVCodecContext);
-    memcpy((uint8_t*)s->vaddr + offset, avctx, size);
+    memcpy((uint8_t *)s->vaddr + offset, avctx, size);
 #else
-    memcpy((uint8_t*)s->vaddr + offset, &avctx->bit_rate, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset, &avctx->bit_rate, sizeof(int));
     size = sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->sub_id, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &avctx->sub_id, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->frame_size, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->frame_size, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &avctx->frame_number, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size,
+            &avctx->frame_number, sizeof(int));
     size += sizeof(int);
 #endif
-    memcpy((uint8_t*)s->vaddr + offset + size, samples, outbuf_size);
+    memcpy((uint8_t *)s->vaddr + offset + size, samples, outbuf_size);
     size += outbuf_size;
-    memcpy((uint8_t*)s->vaddr + offset + size, &frame_size_ptr, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &frame_size_ptr, sizeof(int));
+    size += sizeof(int);
+    memcpy((uint8_t *)s->vaddr + offset + size, &ret, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &ret, sizeof(int));
 
     TRACE("before free input buffer and output buffer!\n");
     if (samples) {
@@ -887,29 +1040,28 @@ int qemu_avcodec_decode_audio (SVCodecState *s, int ctxIndex)
         samples = NULL;
     }
 
-    if (pParserBuffer && bParser) {
+    if (parserBuffer && parser_use) {
         TRACE("[%s] free parser buf\n", __func__);
         av_free(avpkt.data);
-        s->ctxArr[ctxIndex].pParserBuffer = NULL;
+        s->ctxArr[ctxIndex].parserBuffer = NULL;
     }
 
-    pthread_mutex_unlock(&s->codec_mutex);
-
+    qemu_mutex_unlock(&s->thread_mutex);
     TRACE("[%s] Leave\n", __func__);
 
     return ret;
 }
 
-int qemu_avcodec_encode_audio (SVCodecState *s, int ctxIndex)
+int qemu_avcodec_encode_audio(SVCodecState *s, int ctxIndex)
 {
     WARN("[%s] Does not support audio encoder using FFmpeg\n", __func__);
     return 0;
 }
 
-/* void av_picture_copy (AVPicture *dst, const AVPicture *src,
+/* void av_picture_copy(AVPicture *dst, const AVPicture *src,
  *                      enum PixelFormat pix_fmt, int width, int height)
  */
-void qemu_av_picture_copy (SVCodecState* s, int ctxIndex)
+void qemu_av_picture_copy(SVCodecState *s, int ctxIndex)
 {
     AVCodecContext* avctx;
     AVPicture dst;
@@ -920,82 +1072,86 @@ void qemu_av_picture_copy (SVCodecState* s, int ctxIndex)
     off_t offset;
 
     TRACE("Enter :%s\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
-    src = (AVPicture*)s->ctxArr[ctxIndex].pFrame;
+    avctx = s->ctxArr[ctxIndex].avctx;
+    src = (AVPicture *)s->ctxArr[ctxIndex].frame;
     if (!avctx && !src) {
-        ERR("[%s] %d of AVCodecContext or AVFrame is NULL\n", __func__, ctxIndex);
-           pthread_mutex_unlock(&s->codec_mutex);
+        ERR("[%s] %d of context or frame is NULL\n", __func__, ctxIndex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return;
     }
 
     offset = s->codecParam.mmapOffset;
 
-    numBytes = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
+    numBytes =
+        avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
     if (numBytes < 0 ) {
-        ERR("[%s] failed to get size of pixel format:%d\n", __func__, avctx->pix_fmt);
+        ERR("[%s] failed to get size of pixel format:%d\n",
+            __func__, avctx->pix_fmt);
     }
     TRACE("After avpicture_get_size:%d\n", numBytes);
 
-#if 0
-    if (!gTempBuffer) {
-        buffer = (uint8_t*)av_mallocz(numBytes * sizeof(uint8_t));
-        gTempBuffer = buffer;
-    } else {
-        buffer = gTempBuffer;
+    buffer = (uint8_t *)av_mallocz(numBytes);
+    if (!buffer) {
+        ERR("[%s] failed to allocate memory\n");
+        qemu_mutex_unlock(&s->thread_mutex);
+        return;
     }
-#endif
-    buffer = (uint8_t*)av_mallocz(numBytes);
-    ret = avpicture_fill(&dst, buffer, avctx->pix_fmt, avctx->width, avctx->height);
-    TRACE("[%s] After avpicture_fill, ret:%d\n", __func__, ret);
+
+    ret = avpicture_fill(&dst, buffer, avctx->pix_fmt,
+                        avctx->width, avctx->height);
     av_picture_copy(&dst, src, avctx->pix_fmt, avctx->width, avctx->height);
-    memcpy((uint8_t*)s->vaddr + offset, dst.data[0], numBytes);
-    TRACE("After copy image buffer from host to guest.\n");
+    memcpy((uint8_t *)s->vaddr + offset, dst.data[0], numBytes);
+    TRACE("after copy image buffer from host to guest.\n");
 
-    av_free(buffer);
+    if (buffer) {
+        av_free(buffer);
+    }
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
     TRACE("Leave :%s\n", __func__);
 }
 
-/* AVCodecParserContext *av_parser_init (int codec_id) */
-void qemu_av_parser_init (SVCodecState* s, int ctxIndex)
+/* AVCodecParserContext *av_parser_init(int codec_id) */
+void qemu_av_parser_init(SVCodecState *s, int ctxIndex)
 {
     AVCodecParserContext *parserctx = NULL;
     AVCodecContext *avctx;
 
     TRACE("Enter :%s\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (!avctx) {
         ERR("[%s] %d of AVCodecContext is NULL!!\n", __func__, ctxIndex);
-           pthread_mutex_unlock(&s->codec_mutex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return;
     }
 
-    TRACE("before av_parser_init, codec_type:%d codec_id:%x\n", avctx->codec_type, avctx->codec_id);
+    TRACE("before av_parser_init, codec_type:%d codec_id:%x\n",
+            avctx->codec_type, avctx->codec_id);
+
     parserctx = av_parser_init(avctx->codec_id);
     if (parserctx) {
-        TRACE("[%s] Using parser %p\n", __func__, parserctx);
-        s->ctxArr[ctxIndex].bParser = true;
+        TRACE("[%s] using parser\n", __func__);
+        s->ctxArr[ctxIndex].parser_use = true;
     } else {
-        TRACE("[%s] No parser for codec\n", __func__);
-        s->ctxArr[ctxIndex].bParser = false;
+        TRACE("[%s] no parser\n", __func__);
+        s->ctxArr[ctxIndex].parser_use = false;
     }
-    s->ctxArr[ctxIndex].pParserCtx = parserctx;
+    s->ctxArr[ctxIndex].parserctx = parserctx;
 
-    pthread_mutex_unlock(&s->codec_mutex);
+    qemu_mutex_unlock(&s->thread_mutex);
     TRACE("[%s] Leave\n", __func__);
 }
 
 /* int av_parser_parse(AVCodecParserContext *s, AVCodecContext *avctx,
- *                      uint8_t **poutbuf, int *poutbuf_size,
- *                      const uint8_t *buf, int buf_size,
- *                      int64_t pts, int64_t dts)
+ *                     uint8_t **poutbuf, int *poutbuf_size,
+ *                     const uint8_t *buf, int buf_size,
+ *                     int64_t pts, int64_t dts)
  */
-int qemu_av_parser_parse (SVCodecState *s, int ctxIndex)
+int qemu_av_parser_parse(SVCodecState *s, int ctxIndex)
 {
     AVCodecParserContext *parserctx = NULL;
 #ifndef CODEC_COPY_DATA
@@ -1013,51 +1169,54 @@ int qemu_av_parser_parse (SVCodecState *s, int ctxIndex)
     off_t offset;
 
     TRACE("Enter %s\n", __func__);
-    pthread_mutex_lock(&s->codec_mutex);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    parserctx = s->ctxArr[ctxIndex].pParserCtx;
-    avctx = s->ctxArr[ctxIndex].pAVCtx;
+    parserctx = s->ctxArr[ctxIndex].parserctx;
+    avctx = s->ctxArr[ctxIndex].avctx;
     if (!avctx) {
         ERR("[%s] %d of AVCodecContext is NULL\n", __func__, ctxIndex);
-               pthread_mutex_unlock(&s->codec_mutex);
-               return ret;
+        qemu_mutex_unlock(&s->thread_mutex);
+        return ret;
     }
 
     if (!parserctx) {
         ERR("[%s] %d of AVCodecParserContext is NULL\n", __func__, ctxIndex);
-               pthread_mutex_unlock(&s->codec_mutex);
-               return ret;
+        qemu_mutex_unlock(&s->thread_mutex);
+        return ret;
     }
 
-
     offset = s->codecParam.mmapOffset;
 
 #ifndef CODEC_COPY_DATA
     memcpy(&tmpParser, parserctx, sizeof(AVCodecParserContext));
-    memcpy(parserctx, (uint8_t*)s->vaddr + offset, sizeof(AVCodecParserContext));
+    memcpy(parserctx, (uint8_t *)s->vaddr + offset,
+        sizeof(AVCodecParserContext));
     size = sizeof(AVCodecParserContext);
     parserctx->priv_data = tmpParser.priv_data;
     parserctx->parser = tmpParser.parser;
 #else
-    memcpy(&parserctx->pts, (uint8_t*)s->vaddr + offset, sizeof(int64_t));
+    memcpy(&parserctx->pts,
+        (uint8_t *)s->vaddr + offset, sizeof(int64_t));
     size = sizeof(int64_t);
-    memcpy(&parserctx->dts, (uint8_t*)s->vaddr + offset + size, sizeof(int64_t));
+    memcpy(&parserctx->dts,
+        (uint8_t *)s->vaddr + offset + size, sizeof(int64_t));
     size += sizeof(int64_t);
-    memcpy(&parserctx->pos, (uint8_t*)s->vaddr + offset + size, sizeof(int64_t));
+    memcpy(&parserctx->pos,
+        (uint8_t *)s->vaddr + offset + size, sizeof(int64_t));
     size += sizeof(int64_t);
 #endif
-    memcpy(&pts, (uint8_t*)s->vaddr + offset + size, sizeof(int64_t));
+    memcpy(&pts, (uint8_t *)s->vaddr + offset + size, sizeof(int64_t));
     size += sizeof(int64_t);
-    memcpy(&dts, (uint8_t*)s->vaddr + offset + size, sizeof(int64_t));
+    memcpy(&dts, (uint8_t *)s->vaddr + offset + size, sizeof(int64_t));
     size += sizeof(int64_t);
-    memcpy(&pos, (uint8_t*)s->vaddr + offset + size, sizeof(int64_t));
+    memcpy(&pos, (uint8_t *)s->vaddr + offset + size, sizeof(int64_t));
     size += sizeof(int64_t);
-    memcpy(&inbuf_size, (uint8_t*)s->vaddr + offset + size, sizeof(int));
+    memcpy(&inbuf_size, (uint8_t *)s->vaddr + offset + size, sizeof(int));
     size += sizeof(int);
 
     if (inbuf_size > 0) {
         inbuf = av_mallocz(inbuf_size);
-        memcpy(inbuf, (uint8_t*)s->vaddr + offset + size, inbuf_size);
+        memcpy(inbuf, (uint8_t *)s->vaddr + offset + size, inbuf_size);
     } else {
         inbuf = NULL;
         INFO("[%s] input buffer size is zero.\n", __func__);
@@ -1066,28 +1225,27 @@ int qemu_av_parser_parse (SVCodecState *s, int ctxIndex)
     TRACE("[%s] inbuf:%p inbuf_size :%d\n", __func__, inbuf, inbuf_size);
     ret = av_parser_parse2(parserctx, avctx, &poutbuf, &poutbuf_size,
                            inbuf, inbuf_size, pts, dts, pos);
-    TRACE("after parsing, output buffer size :%d, ret:%d\n", poutbuf_size, ret);
+    TRACE("after parsing, outbuf size :%d, ret:%d\n", poutbuf_size, ret);
 
     if (poutbuf) {
-        s->ctxArr[ctxIndex].pParserBuffer = poutbuf;
+        s->ctxArr[ctxIndex].parserBuffer = poutbuf;
     }
 
     TRACE("[%s] inbuf : %p, outbuf : %p\n", __func__, inbuf, poutbuf);
 #ifndef CODEC_COPY_DATA
-    memcpy((uint8_t*)s->vaddr + offset, parserctx, sizeof(AVCodecParserContext));
+    memcpy((uint8_t *)s->vaddr + offset,
+            parserctx, sizeof(AVCodecParserContext));
     size = sizeof(AVCodecParserContext);
 #else
-    memcpy((uint8_t*)s->vaddr + offset, &parserctx->pts, sizeof(int64_t));
+    memcpy((uint8_t *)s->vaddr + offset, &parserctx->pts, sizeof(int64_t));
     size = sizeof(int64_t);
 #endif
-//    memcpy((uint8_t*)s->vaddr + offset + size, avctx, sizeof(AVCodecContext));
-//    size += sizeof(AVCodecContext);
-    memcpy((uint8_t*)s->vaddr + offset + size, &poutbuf_size, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &poutbuf_size, sizeof(int));
     size += sizeof(int);
-    memcpy((uint8_t*)s->vaddr + offset + size, &ret, sizeof(int));
+    memcpy((uint8_t *)s->vaddr + offset + size, &ret, sizeof(int));
     size += sizeof(int);
     if (poutbuf && poutbuf_size > 0) {
-        memcpy((uint8_t*)s->vaddr + offset + size, poutbuf, poutbuf_size);
+        memcpy((uint8_t *)s->vaddr + offset + size, poutbuf, poutbuf_size);
     } else {
         av_free(inbuf);
     }
@@ -1099,96 +1257,98 @@ int qemu_av_parser_parse (SVCodecState *s, int ctxIndex)
     }
 #endif
 
-    pthread_mutex_unlock(&s->codec_mutex);
-    TRACE("[%s]Leave\n", __func__);
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("Leave, %s\n", __func__);
+
     return ret;
 }
 
-/* void av_parser_close (AVCodecParserContext *s) */
-void qemu_av_parser_close (SVCodecState *s, int ctxIndex)
+/* void av_parser_close(AVCodecParserContext *s) */
+void qemu_av_parser_close(SVCodecState *s, int ctxIndex)
 {
     AVCodecParserContext *parserctx;
 
-    TRACE("av_parser_close\n");
-    pthread_mutex_lock(&s->codec_mutex);
+    TRACE("Enter, %s\n", __func__);
+    qemu_mutex_lock(&s->thread_mutex);
 
-    parserctx = s->ctxArr[ctxIndex].pParserCtx;
+    parserctx = s->ctxArr[ctxIndex].parserctx;
     if (!parserctx) {
         ERR("AVCodecParserContext is NULL\n");
-        pthread_mutex_unlock(&s->codec_mutex);
+        qemu_mutex_unlock(&s->thread_mutex);
         return;
     }
     av_parser_close(parserctx);
-    pthread_mutex_unlock(&s->codec_mutex);
+
+    qemu_mutex_unlock(&s->thread_mutex);
+    TRACE("Leave, %s\n", __func__);
 }
 
-int codec_operate (uint32_t apiIndex, uint32_t ctxIndex, SVCodecState *state)
+int codec_operate(uint32_t apiIndex, uint32_t ctxIndex, SVCodecState *s)
 {
     int ret = -1;
 
     TRACE("[%s] context : %d\n", __func__, ctxIndex);
-
     switch (apiIndex) {
-        /* FFMPEG API */
-        case EMUL_AV_REGISTER_ALL:
-            qemu_av_register_all();
-            break;
-        case EMUL_AVCODEC_OPEN:
-            ret = qemu_avcodec_open(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_CLOSE:
-            ret = qemu_avcodec_close(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_ALLOC_CONTEXT:
-            qemu_avcodec_alloc_context(state);
-            break;
-        case EMUL_AVCODEC_ALLOC_FRAME:
-            qemu_avcodec_alloc_frame(state);
-            break;
-        case EMUL_AV_FREE_CONTEXT:
-            qemu_av_free_context(state, ctxIndex);
-            break;
-        case EMUL_AV_FREE_FRAME:
-            qemu_av_free_picture(state, ctxIndex);
-            break;
-        case EMUL_AV_FREE_PALCTRL:
-            qemu_av_free_palctrl(state, ctxIndex);
-            break;
-        case EMUL_AV_FREE_EXTRADATA:
-            qemu_av_free_extradata(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_FLUSH_BUFFERS:
-            qemu_avcodec_flush_buffers(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_DECODE_VIDEO:
-            ret = qemu_avcodec_decode_video(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_ENCODE_VIDEO:
-            ret = qemu_avcodec_encode_video(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_DECODE_AUDIO:
-            ret = qemu_avcodec_decode_audio(state, ctxIndex);
-            break;
-        case EMUL_AVCODEC_ENCODE_AUDIO:
-            ret = qemu_avcodec_encode_audio(state, ctxIndex);
-            break;
-        case EMUL_AV_PICTURE_COPY:
-            qemu_av_picture_copy(state, ctxIndex);
-            break;
-        case EMUL_AV_PARSER_INIT:
-            qemu_av_parser_init(state, ctxIndex);
-            break;
-        case EMUL_AV_PARSER_PARSE:
-            ret = qemu_av_parser_parse(state, ctxIndex);
-            break;
-        case EMUL_AV_PARSER_CLOSE:
-            qemu_av_parser_close(state, ctxIndex);
-            break;
-        case EMUL_GET_CODEC_VER:
-            qemu_get_codec_ver(state, ctxIndex);
-            break;
-        default:
-            WARN("The api index does not exsit!. api index:%d\n", apiIndex);
+    /* FFMPEG API */
+    case EMUL_AV_REGISTER_ALL:
+        qemu_av_register_all();
+        break;
+    case EMUL_AVCODEC_OPEN:
+        ret = qemu_avcodec_open(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_CLOSE:
+        ret = qemu_avcodec_close(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_ALLOC_CONTEXT:
+        qemu_avcodec_alloc_context(s);
+        break;
+    case EMUL_AVCODEC_ALLOC_FRAME:
+        qemu_avcodec_alloc_frame(s, ctxIndex);
+        break;
+    case EMUL_AV_FREE:
+        qemu_av_free(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_FLUSH_BUFFERS:
+        qemu_avcodec_flush_buffers(s, ctxIndex);
+        break;
+#ifndef CODEC_THREAD
+    case EMUL_AVCODEC_DECODE_VIDEO:
+    case EMUL_AVCODEC_ENCODE_VIDEO:
+    case EMUL_AVCODEC_DECODE_AUDIO:
+    case EMUL_AVCODEC_ENCODE_AUDIO:
+        wake_codec_worker_thread(s);
+        break;
+#else
+    case EMUL_AVCODEC_DECODE_VIDEO:
+        ret = qemu_avcodec_decode_video(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_ENCODE_VIDEO:
+        ret = qemu_avcodec_encode_video(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_DECODE_AUDIO:
+        ret = qemu_avcodec_decode_audio(s, ctxIndex);
+        break;
+    case EMUL_AVCODEC_ENCODE_AUDIO:
+        ret = qemu_avcodec_encode_audio(s, ctxIndex);
+        break;
+#endif
+    case EMUL_AV_PICTURE_COPY:
+        qemu_av_picture_copy(s, ctxIndex);
+        break;
+    case EMUL_AV_PARSER_INIT:
+        qemu_av_parser_init(s, ctxIndex);
+        break;
+    case EMUL_AV_PARSER_PARSE:
+        ret = qemu_av_parser_parse(s, ctxIndex);
+        break;
+    case EMUL_AV_PARSER_CLOSE:
+        qemu_av_parser_close(s, ctxIndex);
+        break;
+    case EMUL_GET_CODEC_VER:
+        qemu_get_codec_ver(s);
+        break;
+    default:
+        WARN("api index %d does not exist!.\n", apiIndex);
     }
     return ret;
 }
@@ -1196,48 +1356,58 @@ int codec_operate (uint32_t apiIndex, uint32_t ctxIndex, SVCodecState *state)
 /*
  *  Codec Device APIs
  */
-uint64_t codec_read (void *opaque, target_phys_addr_t addr, unsigned size)
+uint64_t codec_read(void *opaque, target_phys_addr_t addr, unsigned size)
 {
+    SVCodecState *s = (SVCodecState *)opaque;
+    uint64_t ret = 0;
+
     switch (addr) {
-        default:
-            ERR("There is no avaiable command for %s\n", MARU_CODEC_DEV_NAME);
+    case CODEC_QUERY_STATE:
+        ret = s->thread_state;
+        qemu_irq_lower(s->dev.irq[0]);
+        break;
+    default:
+        ERR("There is no avaiable command for %s\n", MARU_CODEC_DEV_NAME);
     }
-    return 0;
+
+    return ret;
 }
 
-void codec_write (void *opaque, target_phys_addr_t addr, uint64_t value, unsigned size)
+void codec_write(void *opaque, target_phys_addr_t addr,
+                uint64_t value, unsigned size)
 {
     int ret = -1;
-    SVCodecState *state = (SVCodecState*)opaque;
+    SVCodecState *s = (SVCodecState *)opaque;
+
+/*  qemu_mutex_lock(&s->thread_mutex); */
 
     switch (addr) {
-        case CODEC_API_INDEX:
-            ret = codec_operate(value, state->codecParam.ctxIndex, state);
-            paramCount = 0;
-            break;
-        case CODEC_IN_PARAM:
-            state->codecParam.in_args[paramCount++] = value;
-            break;
-        case CODEC_RETURN_VALUE:
-            state->codecParam.ret_args = value;
-            break;
-        case CODEC_CONTEXT_INDEX:
-            state->codecParam.ctxIndex = value;
-            TRACE("Context Index : %d\n", state->codecParam.ctxIndex);
-            break;
-        case CODEC_MMAP_OFFSET:
-            state->codecParam.mmapOffset = value * MARU_CODEC_MMAP_MEM_SIZE;
-            TRACE("MMAP Offset :%d\n", state->codecParam.mmapOffset);
-            break;
-        case CODEC_FILE_INDEX:
-            state->codecParam.fileIndex = value;
-            break;
-        case CODEC_CLOSED:
-            qemu_codec_close(state, value);
-            break;
-        default:
-            ERR("There is no avaiable command for %s\n", MARU_CODEC_DEV_NAME);
+    case CODEC_API_INDEX:
+        ret = codec_operate(value, s->codecParam.ctxIndex, s);
+        break;
+    case CODEC_QUERY_STATE:
+        s->thread_state = value;
+        TRACE("worker thread s: %d\n", s->thread_state);
+        break;
+    case CODEC_CONTEXT_INDEX:
+        s->codecParam.ctxIndex = value;
+        TRACE("Context Index: %d\n", s->codecParam.ctxIndex);
+        break;
+    case CODEC_MMAP_OFFSET:
+/*      s->codecParam.mmapOffset = value * MARU_CODEC_MMAP_MEM_SIZE; */
+        s->codecParam.mmapOffset = value;
+        TRACE("MMAP Offset: %d\n", s->codecParam.mmapOffset);
+        break;
+    case CODEC_FILE_INDEX:
+        s->codecParam.fileIndex = value;
+        break;
+    case CODEC_CLOSED:
+        qemu_codec_close(s, value);
+        break;
+    default:
+        ERR("There is no avaiable command for %s\n", MARU_CODEC_DEV_NAME);
     }
+/*  qemu_mutex_unlock(&s->thread_mutex); */
 }
 
 static const MemoryRegionOps codec_mmio_ops = {
@@ -1246,7 +1416,24 @@ static const MemoryRegionOps codec_mmio_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
-static int codec_initfn (PCIDevice *dev)
+static void codec_tx_bh(void *opaque)
+{
+    SVCodecState *s = (SVCodecState *)opaque;
+
+    TRACE("Enter, %s\n", __func__);
+
+    /* raise irq as soon as a worker thread had finished a job*/
+    if (s->thread_state) {
+        TRACE("raise codec irq. state:%d\n", s->thread_state);
+        qemu_irq_raise(s->dev.irq[0]);
+/*      s->thread_state = 0;
+        qemu_bh_schedule(s->tx_bh); */
+    }
+
+    TRACE("Leave, %s\n", __func__);
+}
+
+static int codec_initfn(PCIDevice *dev)
 {
     SVCodecState *s = DO_UPCAST(SVCodecState, dev, dev);
     uint8_t *pci_conf = s->dev.config;
@@ -1254,14 +1441,23 @@ static int codec_initfn (PCIDevice *dev)
     INFO("[%s] device init\n", __func__);
 
     memset(&s->codecParam, 0, sizeof(SVCodecParam));
-    pthread_mutex_init(&s->codec_mutex, NULL);
+/*    pthread_mutex_init(&s->codec_mutex, NULL); */
+#ifndef CODEC_THREAD
+    qemu_mutex_init(&s->thread_mutex);
+    qemu_cond_init(&s->thread_cond);
+
+    codec_thread_init(s);
 
-    pci_config_set_interrupt_pin(pci_conf, 2);
+    s->tx_bh = qemu_bh_new(codec_tx_bh, s);
+#endif
+
+    pci_config_set_interrupt_pin(pci_conf, 1);
 
     memory_region_init_ram(&s->vram, NULL, "codec.ram", MARU_CODEC_MEM_SIZE);
     s->vaddr = memory_region_get_ram_ptr(&s->vram);
 
-    memory_region_init_io (&s->mmio, &codec_mmio_ops, s, "codec-mmio", MARU_CODEC_REG_SIZE);
+    memory_region_init_io(&s->mmio, &codec_mmio_ops, s,
+                        "codec-mmio", MARU_CODEC_REG_SIZE);
 
     pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
     pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mmio);
@@ -1269,20 +1465,22 @@ static int codec_initfn (PCIDevice *dev)
     return 0;
 }
 
-static int codec_exitfn (PCIDevice *dev)
+static int codec_exitfn(PCIDevice *dev)
 {
     SVCodecState *s = DO_UPCAST(SVCodecState, dev, dev);
     INFO("[%s] device exit\n", __func__);
 
-    memory_region_destroy (&s->vram);
-    memory_region_destroy (&s->mmio);
+    qemu_bh_delete(s->tx_bh);
+
+    memory_region_destroy(&s->vram);
+    memory_region_destroy(&s->mmio);
     return 0;
 }
 
-int codec_init (PCIBus *bus)
+int codec_init(PCIBus *bus)
 {
     INFO("[%s] device create\n", __func__);
-    pci_create_simple (bus, -1, MARU_CODEC_DEV_NAME);
+    pci_create_simple(bus, -1, MARU_CODEC_DEV_NAME);
     return 0;
 }
 
@@ -1297,7 +1495,7 @@ static PCIDeviceInfo codec_info = {
     .class_id       = PCI_CLASS_MULTIMEDIA_AUDIO,
 };
 
-static void codec_register (void)
+static void codec_register(void)
 {
     pci_qdev_register(&codec_info);
 }
index d1a1dec71af95466fe154f7ceb3e7608ee7842d6..4787fb30fe1a896f56806b9c680a5fdb3eba6829 100644 (file)
@@ -6,7 +6,6 @@
  * Contact:
  *  Kitae Kim <kt920.kim@samsung.com>
  *  SeokYeon Hwang <syeon.hwang@samsung.com>
- *  DongKyun Yun <dk77.yun@samsung.com>
  *  YeongKyoon Lee <yeongkyoon.lee@samsung.com>
  *
  * This program is free software; you can redistribute it and/or
  */
 
 #include <stdio.h>
-#include <pthread.h>
 #include <sys/types.h>
 #include "hw.h"
 #include "kvm.h"
 #include "pci.h"
 #include "pci_ids.h"
+#include "qemu-thread.h"
 #include "tizen/src/debug_ch.h"
 #include "maru_pci_ids.h"
 
 #include <libavformat/avformat.h>
 
-#define CODEC_MAX_CONTEXT   10
+#define CODEC_MAX_CONTEXT   20
 #define CODEC_COPY_DATA
 
 /*
  *  Codec Device Structures
  */
-
 typedef struct _SVCodecParam {
     uint32_t        apiIndex;
     uint32_t        ctxIndex;
-    uint32_t        in_args[20];
-    uint32_t        ret_args;
     uint32_t        mmapOffset;
     uint32_t        fileIndex;
 } SVCodecParam;
 
 typedef struct _SVCodecContext {
-    AVCodecContext          *pAVCtx;
-    AVFrame                 *pFrame;
-    AVCodecParserContext    *pParserCtx;
-    uint8_t                 *pParserBuffer;
-    bool                    bParser;
+    AVCodecContext          *avctx;
+    AVFrame                 *frame;
+    AVCodecParserContext    *parserctx;
+    uint8_t                 *parserBuffer;
+    bool                    parser_use;
     bool                    bUsed;
     uint32_t                nFileValue;
 } SVCodecContext;
@@ -70,25 +66,29 @@ typedef struct _SVCodecState {
     PCIDevice           dev;
     SVCodecContext      ctxArr[CODEC_MAX_CONTEXT];
     SVCodecParam        codecParam;
-    pthread_mutex_t     codec_mutex;
 
     int                 mmioIndex;
     uint32_t            mem_addr;
     uint32_t            mmio_addr;
 
-    uint8_t*            vaddr;
+    uint8_t             *vaddr;
     MemoryRegion        vram;
     MemoryRegion        mmio;
+
+    QEMUBH              *tx_bh;
+    QemuThread          thread_id;
+    QemuMutex           thread_mutex;
+    QemuCond            thread_cond;
+    uint8_t             thread_state;
 } SVCodecState;
 
 enum {
     CODEC_API_INDEX         = 0x00,
-    CODEC_IN_PARAM          = 0x04,
-    CODEC_RETURN_VALUE      = 0x08,
-    CODEC_CONTEXT_INDEX     = 0x0c,
-    CODEC_MMAP_OFFSET       = 0x10,
-    CODEC_FILE_INDEX        = 0x14,
-    CODEC_CLOSED            = 0x18,
+    CODEC_QUERY_STATE       = 0x04,
+    CODEC_CONTEXT_INDEX     = 0x08,
+    CODEC_MMAP_OFFSET       = 0x0c,
+    CODEC_FILE_INDEX        = 0x10,
+    CODEC_CLOSED            = 0x14,
 };
 
 enum {
@@ -97,10 +97,7 @@ enum {
     EMUL_AVCODEC_ALLOC_FRAME,
     EMUL_AVCODEC_OPEN,
     EMUL_AVCODEC_CLOSE,
-    EMUL_AV_FREE_CONTEXT,
-    EMUL_AV_FREE_FRAME,
-    EMUL_AV_FREE_PALCTRL,
-    EMUL_AV_FREE_EXTRADATA,
+    EMUL_AV_FREE,
     EMUL_AVCODEC_FLUSH_BUFFERS,
     EMUL_AVCODEC_DECODE_VIDEO,
     EMUL_AVCODEC_ENCODE_VIDEO,
@@ -115,66 +112,50 @@ enum {
 
 
 /*
- *  Codec Device APIs
+ *  Codec Thread Functions
  */
-int codec_init (PCIBus *bus);
-
-uint64_t codec_read (void *opaque, target_phys_addr_t addr, unsigned size);
+void codec_thread_init(SVCodecState *s);
+void codec_thread_exit(SVCodecState *s);
+void *codec_worker_thread(void *opaque);
+void wake_codec_worker_thread(SVCodecState *s);
+int decode_codec(SVCodecState *s);
+int encode_codec(SVCodecState *s);
 
+/*
+ *  Codec Device Functions
+ */
+int codec_init(PCIBus *bus);
+uint64_t codec_read(void *opaque, target_phys_addr_t addr, unsigned size);
 void codec_write (void *opaque, target_phys_addr_t addr, uint64_t value, unsigned size);
-
 int codec_operate(uint32_t apiIndex, uint32_t ctxIndex, SVCodecState *state);
 
 /*
- *  Codec Helper APIs
+ *  Codec Helper Functions
  */
-void qemu_parser_init (SVCodecState *s, int ctxIndex);
-
-void qemu_restore_context (AVCodecContext *dst, AVCodecContext *src);
+void qemu_parser_init(SVCodecState *s, int ctxIndex);
+void qemu_codec_close(SVCodecState *s, uint32_t value);
+void qemu_get_codec_ver(SVCodecState *s);
+#ifndef CODEC_COPY_DATA
+void qemu_restore_context(AVCodecContext *dst, AVCodecContext *src);
+#endif
 
-void qemu_codec_close (SVCodecState *s, uint32_t value);
-
-void qemu_get_codec_ver (SVCodecState *s, int ctxIndex);
 /*
- *  FFMPEG APIs
+ *  FFMPEG Functions
  */
-
-void qemu_av_register_all (void);
-
-int qemu_avcodec_open (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_close (SVCodecState *s, int ctxIndex);
-
-void qemu_avcodec_alloc_context (SVCodecState *s);
-
-void qemu_avcodec_alloc_frame (SVCodecState *s);
-
-void qemu_av_free_context (SVCodecState* s, int ctxIndex);
-
-void qemu_av_free_picture (SVCodecState* s, int ctxIndex);
-
-void qemu_av_free_palctrl (SVCodecState* s, int ctxIndex);
-
-void qemu_av_free_extradata (SVCodecState* s, int ctxIndex);
-
-void qemu_avcodec_flush_buffers (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_decode_video (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_encode_video (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_decode_audio (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_encode_audio (SVCodecState *s, int ctxIndex);
-
-void qemu_av_picture_copy (SVCodecState *s, int ctxIndex);
-
-void qemu_av_parser_init (SVCodecState *s, int ctxIndex);
-
-int qemu_av_parser_parse (SVCodecState *s, int ctxIndex);
-
-void qemu_av_parser_close (SVCodecState *s, int ctxIndex);
-
-int qemu_avcodec_get_buffer (AVCodecContext *context, AVFrame *picture);
-
-void qemu_avcodec_release_buffer (AVCodecContext *context, AVFrame *picture);
+void qemu_av_register_all(void);
+int qemu_avcodec_open(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_close(SVCodecState *s, int ctxIndex);
+void qemu_avcodec_alloc_context(SVCodecState *s);
+void qemu_avcodec_alloc_frame(SVCodecState *s, int ctxIndex);
+void qemu_avcodec_flush_buffers(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_decode_video(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_encode_video(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_decode_audio(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_encode_audio(SVCodecState *s, int ctxIndex);
+void qemu_av_picture_copy(SVCodecState *s, int ctxIndex);
+void qemu_av_parser_init(SVCodecState *s, int ctxIndex);
+int qemu_av_parser_parse(SVCodecState *s, int ctxIndex);
+void qemu_av_parser_close(SVCodecState *s, int ctxIndex);
+int qemu_avcodec_get_buffer(AVCodecContext *context, AVFrame *picture);
+void qemu_avcodec_release_buffer(AVCodecContext *context, AVFrame *picture);
+void qemu_av_free(SVCodecState *s, int ctxIndex);