brillcodec: upgrade libav version linked to qemu 80/18580/1
authorKitae Kim <kt920.kim@samsung.com>
Thu, 20 Mar 2014 12:00:05 +0000 (21:00 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Wed, 26 Mar 2014 07:36:17 +0000 (16:36 +0900)
Libav linked to qemu has been upgraded into 9.8 version from 0.7.2.
There are some changes because of this upgrade.
 - Libav is linked to qemu as a shared library
 - Encoding video and audio function are modified because of libav APIs.

Change-Id: Ibb1c7753f0ffa9b5d451ffd903fee040cc5d3ec0
Signed-off-by: Kitae Kim <kt920.kim@samsung.com>
package/build.macos-64
package/build.ubuntu-32
package/build.ubuntu-64
package/build.windows-32
package/pkginfo.manifest
tizen/src/Makefile
tizen/src/Makefile.tizen
tizen/src/hw/maru_board.c
tizen/src/hw/maru_brill_codec.c
tizen/src/hw/maru_brill_codec.h

index dcddca7..5f59bf9 100755 (executable)
@@ -36,7 +36,8 @@ prepare()
     BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/ncurses/include "
     BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/openssl/include "
     BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/pixman/include "
-    BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/zlib/include"
+    BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/zlib/include "
+    BUILD_CFLAGS+="--extra-cflags=-I$ROOTDIR/include"
 
     BUILD_LDFLAGS="--extra-ldflags=-L$ROOTDIR/curl/lib "
     BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/libffi/lib "
@@ -47,7 +48,8 @@ prepare()
     BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/ncurses/lib "
     BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/openssl/lib "
     BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/pixman/lib "
-    BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/zlib/lib"
+    BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/zlib/lib "
+    BUILD_LDFLAGS+="--extra-ldflags=-L$ROOTDIR/lib"
 }
 
 modify_files(){
index 9d9070c..2061a48 100755 (executable)
@@ -17,13 +17,13 @@ clean()
 # check build environment
 prepare()
 {
-    if [ "$JAVA_HOME" = "" ]
-    then
-        echo "Make sure that you have installed JDK"
-        echo "and then set installed JDK/bin path into JAVA_HOME"
-        echo "as a system environment variable on your PC!!"
-        exit 1
-    fi
+       if [ "$JAVA_HOME" = "" ]
+       then
+               echo "Make sure that you have installed JDK"
+               echo "and then set installed JDK/bin path into JAVA_HOME"
+               echo "as a system environment variable on your PC!!"
+               exit 1
+       fi
 
        REQUIRED_PKG="ant python zlib1g-dev libglib2.0-dev libsdl1.2-dev libasound2-dev \
         libx11-dev libv4l-dev libxcomposite-dev libpixman-1-dev libcurl4-gnutls-dev \
@@ -46,10 +46,12 @@ prepare()
 # build
 build()
 {
-        export PKG_CONFIG_PATH=${ROOTDIR}/remote/lib/pkgconfig:${PKG_CONFIG_PATH}
+       export PKG_CONFIG_PATH=${ROOTDIR}/remote/lib/pkgconfig:${PKG_CONFIG_PATH}
+       BUILD_CFLAGS=--extra-cflags="-I$ROOTDIR/include"
+       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/lib"
 
        cd $SRCDIR/tizen/
-       ./emulator_configure.sh x86
+       ./emulator_configure.sh x86 -e "$BUILD_CFLAGS $BUILD_LDFLAGS"
        make all_dibs
        if [ $? -eq 0 ]
        then
@@ -59,18 +61,18 @@ build()
                exit 1
        fi
 
-#      make install_dibs               
-#      make clean              
+#      make install_dibs
+#      make clean
 #
-#      ./emulator_configure.sh arm             
-#      make all_dibs           
-#      if [ $? -eq 0 ]         
-#      then            
-#              echo "arm build success"                
-#      else            
-#              echo "arm build failure"                
-#              exit 1          
-#      fi              
+#      ./emulator_configure.sh arm
+#      make all_dibs
+#      if [ $? -eq 0 ]
+#      then
+#              echo "arm build success"
+#      else
+#              echo "arm build failure"
+#              exit 1
+#      fi
 #      make install_dibs
 }
 
index 9d9070c..2061a48 100755 (executable)
@@ -17,13 +17,13 @@ clean()
 # check build environment
 prepare()
 {
-    if [ "$JAVA_HOME" = "" ]
-    then
-        echo "Make sure that you have installed JDK"
-        echo "and then set installed JDK/bin path into JAVA_HOME"
-        echo "as a system environment variable on your PC!!"
-        exit 1
-    fi
+       if [ "$JAVA_HOME" = "" ]
+       then
+               echo "Make sure that you have installed JDK"
+               echo "and then set installed JDK/bin path into JAVA_HOME"
+               echo "as a system environment variable on your PC!!"
+               exit 1
+       fi
 
        REQUIRED_PKG="ant python zlib1g-dev libglib2.0-dev libsdl1.2-dev libasound2-dev \
         libx11-dev libv4l-dev libxcomposite-dev libpixman-1-dev libcurl4-gnutls-dev \
@@ -46,10 +46,12 @@ prepare()
 # build
 build()
 {
-        export PKG_CONFIG_PATH=${ROOTDIR}/remote/lib/pkgconfig:${PKG_CONFIG_PATH}
+       export PKG_CONFIG_PATH=${ROOTDIR}/remote/lib/pkgconfig:${PKG_CONFIG_PATH}
+       BUILD_CFLAGS=--extra-cflags="-I$ROOTDIR/include"
+       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/lib"
 
        cd $SRCDIR/tizen/
-       ./emulator_configure.sh x86
+       ./emulator_configure.sh x86 -e "$BUILD_CFLAGS $BUILD_LDFLAGS"
        make all_dibs
        if [ $? -eq 0 ]
        then
@@ -59,18 +61,18 @@ build()
                exit 1
        fi
 
-#      make install_dibs               
-#      make clean              
+#      make install_dibs
+#      make clean
 #
-#      ./emulator_configure.sh arm             
-#      make all_dibs           
-#      if [ $? -eq 0 ]         
-#      then            
-#              echo "arm build success"                
-#      else            
-#              echo "arm build failure"                
-#              exit 1          
-#      fi              
+#      ./emulator_configure.sh arm
+#      make all_dibs
+#      if [ $? -eq 0 ]
+#      then
+#              echo "arm build success"
+#      else
+#              echo "arm build failure"
+#              exit 1
+#      fi
 #      make install_dibs
 }
 
index 92b25a3..63333be 100755 (executable)
@@ -46,11 +46,11 @@ prepare()
        BUILD_CFLAGS+=--extra-cflags="-I$ROOTDIR/libcurl-4_1.0.1/include "
        BUILD_CFLAGS+=--extra-cflags="-I$ROOTDIR/pixman_0.30.0/include"
        BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/lib "
-       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/apache-ant_1.9.2/lib "
-       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/SDL_1.2.15/lib "
-       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/gtk-bundle_2.24.10/lib "
-       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/libcurl-4_1.0.1/lib "
-       BUILD_LDFLAGS=--extra-ldflags="-L$ROOTDIR/pixman_0.30.0/lib"
+       BUILD_LDFLAGS+=--extra-ldflags="-L$ROOTDIR/apache-ant_1.9.2/lib "
+       BUILD_LDFLAGS+=--extra-ldflags="-L$ROOTDIR/SDL_1.2.15/lib "
+       BUILD_LDFLAGS+=--extra-ldflags="-L$ROOTDIR/gtk-bundle_2.24.10/lib "
+       BUILD_LDFLAGS+=--extra-ldflags="-L$ROOTDIR/libcurl-4_1.0.1/lib "
+       BUILD_LDFLAGS+=--extra-ldflags="-L$ROOTDIR/pixman_0.30.0/lib"
 }
 
 # build
index fc3278d..e29481b 100644 (file)
@@ -29,8 +29,8 @@ Description: Common binaries for Tizen Emulator
 Package: emulator-qemu-x86
 OS: ubuntu-32
 Build-host-os: ubuntu-32
-Build-dependency: emulator-lib [ ubuntu-32 ], emulator-spice-server-dev [ ubuntu-32 ]
-Install-dependency: emulator-kernel-x86 [ ubuntu-32 ], vgabios [ ubuntu-32 ], emulator-spice-server [ ubuntu-32 ]
+Build-dependency: emulator-lib [ ubuntu-32 ], emulator-spice-server-dev [ ubuntu-32 ], libav-dev [ ubuntu-32 ]
+Install-dependency: emulator-kernel-x86 [ ubuntu-32 ], vgabios [ ubuntu-32 ], emulator-spice-server [ ubuntu-32 ], libav [ ubuntu-32 ]
 Description: Tizen x86 Emulator
 
 #Package: emulator-qemu-arm
@@ -43,8 +43,8 @@ Description: Tizen x86 Emulator
 Package: emulator-qemu-x86
 OS: ubuntu-64
 Build-host-os: ubuntu-64
-Build-dependency: emulator-lib [ ubuntu-64 ], emulator-spice-server-dev [ ubuntu-64 ]
-Install-dependency: emulator-kernel-x86 [ ubuntu-64 ], vgabios [ ubuntu-64 ], emulator-spice-server [ ubuntu-64 ]
+Build-dependency: emulator-lib [ ubuntu-64 ], emulator-spice-server-dev [ ubuntu-64 ], libav-dev [ ubuntu-64]
+Install-dependency: emulator-kernel-x86 [ ubuntu-64 ], vgabios [ ubuntu-64 ], emulator-spice-server [ ubuntu-64 ], libav [ ubuntu-64 ]
 Description: Tizen x86 Emulator
 
 #Package: emulator-qemu-arm
@@ -57,8 +57,8 @@ Description: Tizen x86 Emulator
 Package: emulator-qemu-x86
 OS: windows-32, windows-64
 Build-host-os: windows-32
-Build-dependency: emulator-lib-dev [ windows-32 ], emulator-lib [ windows-32 ]
-Install-dependency: emulator-kernel-x86, vgabios
+Build-dependency: emulator-lib-dev [ windows-32 ], emulator-lib [ windows-32 ], libav-dev [ windows-32 ]
+Install-dependency: emulator-kernel-x86, vgabios, libav [ windows-32 ]
 Description: Tizen x86 Emulator
 
 #Package: emulator-qemu-arm
@@ -71,8 +71,8 @@ Description: Tizen x86 Emulator
 Package: emulator-qemu-x86
 OS: macos-64
 Build-host-os: macos-64
-Build-dependency: emulator-lib [ macos-64 ], emulator-lib-dev [ macos-64 ]
-Install-dependency: emulator-kernel-x86 [ macos-64 ], vgabios [ macos-64 ]
+Build-dependency: emulator-lib [ macos-64 ], emulator-lib-dev [ macos-64 ], libav-dev [ macos-64 ]
+Install-dependency: emulator-kernel-x86 [ macos-64 ], vgabios [ macos-64 ], libav [ macos-64 ]
 Description: Tizen x86 Emulator
 
 #Package: emulator-qemu-arm
index e52f38a..e846aa5 100644 (file)
@@ -157,6 +157,10 @@ ifdef CONFIG_DARWIN
                install_name_tool -change /opt/local/lib/libncurses.5.dylib @loader_path/libncurses.5.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
                install_name_tool -change /opt/local/lib/libcurl.4.dylib @loader_path/libcurl.4.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
                install_name_tool -change /opt/local/lib/libpixman-1.0.dylib @loader_path/libpixman-1.0.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavformat-emul.54.dylib @loader_path/libavformat-emul.54.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavcodec-emul.54.dylib @loader_path/libavcodec-emul.54.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavresample-emul.1.dylib @loader_path/libavresample-emul.1.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavutil-emul.52.dylib @loader_path/libavutil-emul.52.dylib $(EMUL_DIR)/bin/emulator-x86 ;\
                ;; \
        arm-softmmu) \
                ;; \
@@ -261,6 +265,10 @@ ifdef CONFIG_DARWIN
                install_name_tool -change /opt/local/lib/libncurses.5.dylib @loader_path/libncurses.5.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
                install_name_tool -change /opt/local/lib/libcurl.4.dylib @loader_path/libcurl.4.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
                install_name_tool -change /opt/local/lib/libpixman-1.0.dylib @loader_path/libpixman-1.0.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavformat-emul.54.dylib @loader_path/libavformat-emul.54.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavcodec-emul.54.dylib @loader_path/libavcodec-emul.54.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavresample-emul.1.dylib @loader_path/libavresample-emul.1.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
+               install_name_tool -change .//lib/libavutil-emul.52.dylib @loader_path/libavutil-emul.52.dylib $(DIBS_X86_DIR)/bin/emulator-x86 ;\
                ;; \
        arm-softmmu) \
                ;; \
index 1e87e44..a2a4dd1 100644 (file)
@@ -17,6 +17,9 @@ else
 CFLAGS += -g -O0
 endif
 
+# codec
+LIBS+= -lavformat-emul -lavcodec-emul -lavresample-emul -lavutil-emul
+
 ifdef CONFIG_WIN32
 LIBS += -lcurl -lopengl32 -lglu32 -lgdi32 -lpng14
 endif
@@ -119,7 +122,7 @@ obj-y += maru_virtio_nfc.o
 obj-y += maru_virtio_jack.o
 obj-y += maru_virtio_power.o
 obj-y += maru_virtio_esm.o
-#obj-y += maru_brill_codec.o
+obj-y += maru_brill_codec.o
 
 obj-$(CONFIG_PCI) += maru_camera_common_pci.o
 obj-$(CONFIG_LINUX) += maru_camera_linux_pci.o
index 0f1c31c..d3d6d1b 100644 (file)
@@ -119,7 +119,7 @@ static void maru_device_init(void)
 
     pci_maru_overlay_init(pci_bus);
     pci_maru_brightness_init(pci_bus);
-    // maru_brill_codec_pci_device_init(pci_bus);
+    maru_brill_codec_pci_device_init(pci_bus);
 
     if (enable_vigs || enable_yagl) {
         render_queue = work_queue_create();
index 484dcdf..7cdeeaa 100644 (file)
@@ -34,7 +34,7 @@
 MULTI_DEBUG_CHANNEL(qemu, brillcodec);
 
 // device
-#define CODEC_DEVICE_NAME   "brilcodec"
+#define CODEC_DEVICE_NAME   "brillcodec"
 #define CODEC_VERSION       2
 
 // device memory
@@ -952,7 +952,7 @@ static AVCodecContext *maru_brill_codec_alloc_context(MaruBrillCodecState *s, in
     TRACE("enter: %s\n", __func__);
 
     TRACE("allocate %d of context and frame.\n", index);
-    s->context[index].avctx = avcodec_alloc_context();
+    s->context[index].avctx = avcodec_alloc_context3(NULL);
     s->context[index].frame = avcodec_alloc_frame();
     s->context[index].opened_context = false;
 
@@ -1000,6 +1000,12 @@ static void read_codec_init_data(AVCodecContext *avctx, uint8_t *mem_buf)
     size = sizeof(video);
     serialize_video_data(&video, avctx);
 
+#if 0
+    memcpy(&audio, mem_buf + size, sizeof(int32_t) * 7);
+    size += (sizeof(int32_t) * 7);
+    memcpy(&audio.channel_layout, mem_buf + size, sizeof(audio.channel_layout));
+    size += sizeof(audio.channel_layout);
+#endif
     memcpy(&audio, mem_buf + size, sizeof(audio));
     size += sizeof(audio);
     serialize_audio_data(&audio, avctx);
@@ -1053,6 +1059,60 @@ static int write_codec_init_data(AVCodecContext *avctx, uint8_t *mem_buf)
     return size;
 }
 
+static uint8_t *resample_audio(AVCodecContext * avctx, AVFrame *samples, int *out_size)
+{
+    AVAudioResampleContext *avr = NULL;
+    uint8_t *resample_audio = NULL;
+    int buffer_size = 0, out_linesize = 0;
+    int nb_samples = samples->nb_samples;
+    int out_sample_fmt = avctx->sample_fmt - 5;
+
+    avr = avresample_alloc_context();
+    if (!avr) {
+        ERR("failed to allocate avresample context\n");
+        return NULL;
+    }
+
+    av_opt_set_int(avr, "in_channel_layout", avctx->channel_layout, 0);
+    av_opt_set_int(avr, "in_sample_fmt", avctx->sample_fmt, 0);
+    av_opt_set_int(avr, "in_sample_rate", avctx->sample_rate, 0);
+    av_opt_set_int(avr, "out_channel_layout", avctx->channel_layout, 0);
+    av_opt_set_int(avr, "out_sample_fmt", out_sample_fmt, 0);
+    av_opt_set_int(avr, "out_sample_rate", avctx->sample_rate, 0);
+
+    TRACE("open avresample context\n");
+    if (avresample_open(avr) < 0) {
+        ERR("failed to open avresample context\n");
+        avresample_free(&avr);
+        return NULL;
+    }
+
+    *out_size =
+        av_samples_get_buffer_size(&out_linesize, avctx->channels,
+                                    nb_samples, out_sample_fmt, 0);
+
+    resample_audio = av_mallocz(*out_size);
+    if (!resample_audio) {
+        ERR("failed to allocate resample buffer\n");
+        avresample_close(avr);
+        avresample_free(&avr);
+        return NULL;
+    }
+
+    buffer_size = avresample_convert(avr, &resample_audio,
+        out_linesize, nb_samples,
+        samples->data, samples->linesize[0],
+        samples->nb_samples);
+
+    TRACE("resample_audio out_size %d buffer_size %d\n", *out_size, buffer_size);
+
+    avresample_close(avr);
+    avresample_free(&avr);
+
+    return resample_audio;
+}
+
+
 // codec functions
 static bool codec_init(MaruBrillCodecState *s, int ctx_id, void *data_buf)
 {
@@ -1061,7 +1121,7 @@ static bool codec_init(MaruBrillCodecState *s, int ctx_id, void *data_buf)
     int size = 0, ret = -1;
     DeviceMemEntry *elem = NULL;
     uint8_t *tempbuf = NULL;
-    int tempbuf_size = 32;
+    int tempbuf_size = 0;
 
     TRACE("enter: %s\n", __func__);
 
@@ -1093,22 +1153,26 @@ static bool codec_init(MaruBrillCodecState *s, int ctx_id, void *data_buf)
             size = sizeof(int32_t) + 32; // buffer size of codec_name
             read_codec_init_data(avctx, elem->buf + size);
 
-            ret = avcodec_open(avctx, codec);
+            ret = avcodec_open2(avctx, codec, NULL);
             INFO("avcodec_open success! ret %d ctx_id %d\n", ret, ctx_id);
-            // TODO: check requested_close ?
+
+            tempbuf_size =
+                (sizeof(avctx->sample_fmt) + sizeof(avctx->frame_size)
+                + sizeof(int) + sizeof(avctx->extradata_size) + avctx->extradata_size);
 
             s->context[ctx_id].opened_context = true;
             s->context[ctx_id].parser_ctx =
                 maru_brill_codec_parser_init(avctx);
         } else {
             ERR("failed to find codec. ctx_id: %d\n", ctx_id);
+            ret = -1;
         }
     }
 
-    INFO("codec_init. channels %d sample_rate %d sample_fmt %d ch_layout %lld\n",
+    TRACE("codec_init. channels %d sample_rate %d sample_fmt %d ch_layout %lld\n",
         avctx->channels, avctx->sample_rate, avctx->sample_fmt, avctx->channel_layout);
 
-    tempbuf_size += (sizeof(avctx->extradata_size) + avctx->extradata_size);
+    tempbuf_size += sizeof(ret);
 
     tempbuf = g_malloc(tempbuf_size);
     if (!tempbuf) {
@@ -1122,8 +1186,6 @@ static bool codec_init(MaruBrillCodecState *s, int ctx_id, void *data_buf)
         } else {
             size += write_codec_init_data(avctx, tempbuf + size);
             TRACE("codec_init. copyback!! size %d\n", size);
-#if 1
-            // if (codec->encode) {
             {
                 memcpy(tempbuf + size, &avctx->extradata_size, sizeof(avctx->extradata_size));
                 size += sizeof(avctx->extradata_size);
@@ -1132,7 +1194,6 @@ static bool codec_init(MaruBrillCodecState *s, int ctx_id, void *data_buf)
                     size += avctx->extradata_size;
                 }
             }
-#endif
         }
     }
 
@@ -1172,12 +1233,6 @@ static bool codec_deinit(MaruBrillCodecState *s, int ctx_id, void *data_buf)
         s->context[ctx_id].avctx->extradata = NULL;
     }
 
-    if (avctx->palctrl) {
-        TRACE("free context palctrl \n");
-        av_free(avctx->palctrl);
-        s->context[ctx_id].avctx->palctrl = NULL;
-    }
-
     if (frame) {
         TRACE("free frame\n");
         av_free(frame);
@@ -1217,7 +1272,7 @@ static bool codec_flush_buffers(MaruBrillCodecState *s, int ctx_id, void *data_b
         ERR("%d of AVCodec is NULL.\n", ctx_id);
         ret = false;
     } else {
-        INFO("flush %d context of buffers.\n", ctx_id);
+        TRACE("flush %d context of buffers.\n", ctx_id);
         avcodec_flush_buffers(avctx);
     }
 
@@ -1285,7 +1340,6 @@ static bool codec_decode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
         len =
             avcodec_decode_video2(avctx, picture, &got_picture, &avpkt);
         TRACE("decode_video. in_size %d len %d, frame_size %d\n", avpkt.size, len, got_picture);
-        // TODO: check requested_close ?
     }
 
     tempbuf_size =
@@ -1358,7 +1412,6 @@ static bool codec_picture_copy (MaruBrillCodecState *s, int ctx_id, void *elem)
             TRACE("picture size: %d\n", pict_size);
             av_picture_copy(&dst, src, avctx->pix_fmt,
                             avctx->width, avctx->height);
-            // TODO: check requested_close ?
 
             tempbuf = g_malloc0(pict_size);
             if (!tempbuf) {
@@ -1382,14 +1435,19 @@ static bool codec_decode_audio(MaruBrillCodecState *s, int ctx_id, void *data_bu
 {
     AVCodecContext *avctx;
     AVPacket avpkt;
-    int16_t *samples = NULL;
+    AVFrame *samples = NULL;
     uint8_t *inbuf = NULL;
     int inbuf_size = 0, size = 0;
-    int len = -1, frame_size_ptr = 0;
+    int len = -1, got_frame = 0;
+
     DeviceMemEntry *elem = NULL;
     uint8_t *tempbuf = NULL;
     int tempbuf_size = 0;
 
+    uint8_t *outbuf = NULL;
+    int buffer_size = 0;
+    int out_sample_fmt = -1;
+
     TRACE("enter: %s\n", __func__);
 
     elem = (DeviceMemEntry *)data_buf;
@@ -1412,35 +1470,44 @@ static bool codec_decode_audio(MaruBrillCodecState *s, int ctx_id, void *data_bu
     avpkt.size = inbuf_size;
 
     avctx = s->context[ctx_id].avctx;
+    samples = s->context[ctx_id].frame;
     if (!avctx) {
         ERR("[%s] %d of AVCodecContext is NULL!\n", __func__, ctx_id);
     } else if (!avctx->codec) {
         ERR("%d of AVCodec is NULL.\n", ctx_id);
     } else {
-        frame_size_ptr = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-        samples = av_mallocz(frame_size_ptr);
         if (!samples) {
-            ERR("failed to allocate an outbuf of audio.\n");
+            if (!(samples = avcodec_alloc_frame())) {
+                ERR("failed to allocate decoded audio samples.\n");
+                len = -1;
+            }
         } else {
-            len =
-                avcodec_decode_audio3(avctx, samples, &frame_size_ptr, &avpkt);
+            avcodec_get_frame_defaults(samples);
+
+            len = avcodec_decode_audio4(avctx, samples, &got_frame, &avpkt);
 
             TRACE("decode_audio. len %d, channel_layout %lld, frame_size %d\n",
-                len, avctx->channel_layout, frame_size_ptr);
-            // TODO: check requested_close ?
+                len, avctx->channel_layout, got_frame);
+            if (got_frame) {
+                if (av_sample_fmt_is_planar(avctx->sample_fmt)) {
+                    out_sample_fmt = avctx->sample_fmt - 5;
+
+                    outbuf = resample_audio (avctx, samples, &buffer_size);
+                } else {
+                    // not planar format
+                }
+            }
         }
-
     }
 
-    tempbuf_size = (sizeof(len) + sizeof(frame_size_ptr));
-
+    tempbuf_size = (sizeof(len) + sizeof(got_frame));
     if (len < 0) {
-        ERR("failed to decode audio. ctx_id: %d len: %d got_frame: %d\n", ctx_id, len, frame_size_ptr);
-        frame_size_ptr = 0;
+        ERR("failed to decode audio. ctx_id: %d len: %d got_frame: %d\n", ctx_id, len, got_frame);
+        got_frame = 0;
     } else {
-        tempbuf_size +=
-            (sizeof(avctx->sample_rate) + sizeof(avctx->channels) +
-            sizeof(avctx->channel_layout) + frame_size_ptr);
+        tempbuf_size += (sizeof(out_sample_fmt) + sizeof(avctx->sample_rate)
+                        + sizeof(avctx->channels) +  sizeof(avctx->channel_layout)
+                        + sizeof(buffer_size) + buffer_size);
     }
 
     tempbuf = g_malloc(tempbuf_size);
@@ -1449,27 +1516,31 @@ static bool codec_decode_audio(MaruBrillCodecState *s, int ctx_id, void *data_bu
     } else {
         memcpy(tempbuf, &len, sizeof(len));
         size = sizeof(len);
-        memcpy(tempbuf + size, &frame_size_ptr, sizeof(frame_size_ptr));
-        size += sizeof(frame_size_ptr);
-        if (frame_size_ptr) {
+        memcpy(tempbuf + size, &got_frame, sizeof(got_frame));
+        size += sizeof(got_frame);
+        if (got_frame) {
+            memcpy(tempbuf + size, &out_sample_fmt, sizeof(out_sample_fmt));
+            size += sizeof(out_sample_fmt);
             memcpy(tempbuf + size, &avctx->sample_rate, sizeof(avctx->sample_rate));
             size += sizeof(avctx->sample_rate);
             memcpy(tempbuf + size, &avctx->channels, sizeof(avctx->channels));
             size += sizeof(avctx->channels);
             memcpy(tempbuf + size, &avctx->channel_layout, sizeof(avctx->channel_layout));
             size += sizeof(avctx->channel_layout);
-            memcpy(tempbuf + size, samples, frame_size_ptr);
-        }
-    }
 
-    if (samples) {
-        av_free(samples);
+            memcpy(tempbuf + size, &buffer_size, sizeof(buffer_size));
+            size += sizeof(buffer_size);
+
+            if (outbuf) {
+                memcpy(tempbuf + size, outbuf, buffer_size);
+                av_free(outbuf);
+            }
+        }
     }
 
     maru_brill_codec_push_writequeue(s, tempbuf, tempbuf_size, ctx_id);
 
     TRACE("leave: %s\n", __func__);
-
     return true;
 }
 
@@ -1477,9 +1548,10 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
 {
     AVCodecContext *avctx = NULL;
     AVFrame *pict = NULL;
+    AVPacket avpkt;
     uint8_t *inbuf = NULL, *outbuf = NULL;
-    int inbuf_size, outbuf_size, len = -1;
-    int ret, size = 0;
+    int inbuf_size = 0, outbuf_size = 0;
+    int got_frame = 0, ret = 0, size = 0;
     int64_t in_timestamp = 0;
     int coded_frame = 0, key_frame = 0;
 
@@ -1537,12 +1609,17 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
                 (avctx->width * avctx->height * 6) + FF_MIN_BUFFER_SIZE;
 
             outbuf = g_malloc0(outbuf_size);
+
+            av_init_packet(&avpkt);
+            avpkt.data = outbuf;
+            avpkt.size = outbuf_size;
+
             if (!outbuf) {
                 ERR("failed to allocate a buffer of encoding video.\n");
             } else {
-                len = avcodec_encode_video(avctx, outbuf, outbuf_size, pict);
+                ret = avcodec_encode_video2(avctx, &avpkt, pict, &got_frame);
 
-                TRACE("encode video. len %d pts %lld\n", len, pict->pts);
+                TRACE("encode video. ret %d got_picture %d outbuf_size %d\n", ret, got_frame, avpkt.size);
                 if (avctx->coded_frame) {
                     TRACE("encode video. keyframe %d\n", avctx->coded_frame->key_frame);
                 }
@@ -1550,11 +1627,11 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
         }
     }
 
-    tempbuf_size = sizeof(len);
-    if (len < 0) {
-        ERR("failed to encode video. ctx_id: %d len: %d\n", ctx_id, len);
+    tempbuf_size = sizeof(ret);
+    if (ret < 0) {
+        ERR("failed to encode video. ctx_id %d ret %d\n", ctx_id, ret);
     } else {
-        tempbuf_size += len + sizeof(coded_frame) + sizeof(key_frame);
+        tempbuf_size += avpkt.size + sizeof(coded_frame) + sizeof(key_frame);
     }
 
     // write encoded video data
@@ -1562,10 +1639,10 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
     if (!tempbuf) {
         ERR("encode video. failed to allocate encoded out buffer.\n");
     } else {
-        memcpy(tempbuf, &len, sizeof(len));
-        size = sizeof(len);
+        memcpy(tempbuf, &avpkt.size, sizeof(avpkt.size));
+        size = sizeof(avpkt.size);
 
-        if ((len > 0) && outbuf) {
+        if ((got_frame) && outbuf) {
             // inform gstreamer plugin about the status of encoded frames
             // A flag for output buffer in gstreamer is depending on the status.
             if (avctx->coded_frame) {
@@ -1577,7 +1654,7 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
             size += sizeof(coded_frame);
             memcpy(tempbuf + size, &key_frame, sizeof(key_frame));
             size += sizeof(key_frame);
-            memcpy(tempbuf + size, outbuf, len);
+            memcpy(tempbuf + size, outbuf, avpkt.size);
         }
     }
 
@@ -1595,9 +1672,10 @@ static bool codec_encode_video(MaruBrillCodecState *s, int ctx_id, void *data_bu
 static bool codec_encode_audio(MaruBrillCodecState *s, int ctx_id, void *data_buf)
 {
     AVCodecContext *avctx;
+    AVPacket avpkt;
     uint8_t *inbuf = NULL, *outbuf = NULL;
     int32_t inbuf_size = 0, max_size = 0;
-    int len = -1, size = 0;
+    int ret = 0, got_pkt = 0, size = 0;
 
     DeviceMemEntry *elem = NULL;
     uint8_t *tempbuf = NULL;
@@ -1633,33 +1711,37 @@ static bool codec_encode_audio(MaruBrillCodecState *s, int ctx_id, void *data_bu
     } else {
         outbuf = g_malloc0(max_size + FF_MIN_BUFFER_SIZE);
 //        outbuf = g_malloc0(max_size);
+
+        av_init_packet(&avpkt);
+        avpkt.data = outbuf;
+        avpkt.size = max_size;
+
         if (!outbuf) {
             ERR("failed to allocate a buffer of encoding audio.\n");
         } else {
-            len =
-                avcodec_encode_audio(avctx, outbuf, max_size, (short *)inbuf);
-            INFO("encoding audio. len: %d\n", len);
+            ret = avcodec_encode_audio2(avctx, &avpkt, (const AVFrame *)inbuf, &got_pkt);
+            TRACE("encode audio. ret %d got_pkt %d outbuf_size %d\n", ret, got_pkt, avpkt.size);
         }
     }
 
-    tempbuf_size = sizeof(len);
-    if (len < 0) {
-        ERR("failed to encode audio. ctx_id: %d len: %d\n", ctx_id, len);
+    tempbuf_size = sizeof(ret);
+    if (ret < 0) {
+        ERR("failed to encode audio. ctx_id %d ret %d\n", ctx_id, ret);
     } else {
         // tempbuf_size += (max_size); // len;
-        tempbuf_size += len;
+        tempbuf_size += avpkt.size;
     }
 
     // write encoded audio data
     tempbuf = g_malloc0(tempbuf_size);
     if (!tempbuf) {
-        ERR("encode_audio. failed to allocate encoded out buffer.\n");
+        ERR("encode audio. failed to allocate encoded out buffer.\n");
     } else {
-        memcpy(tempbuf, &len, sizeof(len));
-        size = sizeof(len);
-        if ((len > 0) && outbuf) {
+        memcpy(tempbuf, &avpkt.size, sizeof(avpkt.size));
+        size = sizeof(avpkt.size);
+        if (got_pkt && outbuf) {
             // memcpy(tempbuf + size, outbuf, max_size); // len);
-            memcpy(tempbuf + size, outbuf, len);
+            memcpy(tempbuf + size, outbuf, avpkt.size);
         }
     }
 
index eeebbab..b7130cb 100644 (file)
 #include "osutil.h"
 #include "tizen/src/debug_ch.h"
 #include "maru_device_ids.h"
+
 #include "libavformat/avformat.h"
+#include "libavresample/avresample.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 
 #define CODEC_CONTEXT_MAX           1024