Added audioconvert for supporting 16bit pcm when running audio encoder
[platform/core/api/mediacodec.git] / test / media_codec_test.c
index 2da9c6f..67f0f68 100755 (executable)
 #include <appcore-efl.h>
 #include <gst/gst.h>
 
+#include <tbm_surface.h>
+#include <dlog.h>
+#include <time.h>
 #include <camera.h>
 #include <media_codec.h>
 #include <media_packet.h>
 #include <media_packet_pool.h>
-#include <tbm_surface.h>
-#include <dlog.h>
-#include <time.h>
+#include <media_codec_internal.h>
 
 #define PACKAGE "media_codec_test"
 #define MAX_HANDLE                     4
@@ -55,6 +56,7 @@
        ((state & (expected_state)) == (expected_state))
 
 #define AAC_CODECDATA_SIZE    16
+#define USE_POOL       1
 
 unsigned char buf_adts[ADTS_HEADER_SIZE];
 
@@ -181,6 +183,7 @@ static void mc_hex_dump(char *desc, void *addr, int len);
 static void decoder_output_dump(App *app, media_packet_h pkt);
 static void output_dump(App *app, media_packet_h pkt);
 /* */
+const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id);
 
 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
 
@@ -188,7 +191,7 @@ int g_menu_state = CURRENT_STATUS_MAINMENU;
 
 static int _create_app(void *data)
 {
-       printf("My app is going alive!\n");
+       g_print("My app is going alive!\n");
        App *app = (App*)data;
 
        g_mutex_init(&app->lock);
@@ -197,7 +200,7 @@ static int _create_app(void *data)
 
 static int _terminate_app(void *data)
 {
-       printf("My app is going gone!\n");
+       g_print("My app is going gone!\n");
        App *app = (App*)data;
 
        g_mutex_clear(&app->lock);
@@ -413,7 +416,7 @@ void mpeg4_extractor(App * app, unsigned char **data, int *size, bool * have_fra
                                        goto DONE;
                                }
                                if (!bType) {
-                                       if (have_frame && val == 0xB0)
+                                       if (val == 0xB0)
                                                *have_frame = TRUE;
                                }
                                bType = val;
@@ -605,17 +608,20 @@ void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_fram
        if (header == 0) {
                g_print ("[ERROR] read header size is 0\n");
                *have_frame = FALSE;
+               return;
        }
 
        /* if it's not a valid sync */
        if ((header & 0xffe00000) != 0xffe00000) {
                g_print ("[ERROR] invalid sync\n");
                *have_frame = FALSE;
+               return;
        }
 
        if (((header >> 19) & 3) == 0x1) {
                g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
                *have_frame = FALSE;
+               return;
        } else {
                if (header & (1 << 20)) {
                        lsf = (header & (1 << 19)) ? 0 : 1;
@@ -630,6 +636,7 @@ void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_fram
        if (!((header >> 17) & 3)) {
                g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
                *have_frame = FALSE;
+               return;
        } else {
                layer = 4 - ((header >> 17) & 0x3);
        }
@@ -638,12 +645,15 @@ void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_fram
        if (((header >> 12) & 0xf) == 0xf) {
                g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
                *have_frame = FALSE;
+               return;
        } else {
                bitrate = (header >> 12) & 0xF;
                hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
                /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
-               if (hdr_bitrate == 0)
+               if (hdr_bitrate == 0) {
                        *have_frame = FALSE;
+                       return;
+               }
        }
 
        /* if it's an invalid samplerate */
@@ -768,6 +778,13 @@ void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, b
                read_size = readsize;
                app->offset += header_size + readsize;
 
+               if (app->offset > app->length) {
+                       read_size = 0;
+                       *have_frame = FALSE;
+                       g_print("[FAIL] offset error \n");
+                       return;
+               }
+
        } else {
                read_size = 0;
                g_print("[FAIL] Not found aac frame sync. \n");
@@ -856,7 +873,7 @@ int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
        case MEDIACODEC_AAC:
                if (encoder) {
                        extractor = aacenc_extractor;
-                       mime = MEDIA_FORMAT_PCM;
+                       mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
                } else {
                        extractor = aacdec_extractor;
                        mime = MEDIA_FORMAT_AAC;
@@ -865,7 +882,7 @@ int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
        case MEDIACODEC_AAC_HE:
                if (encoder) {
                        extractor = aacenc_extractor;
-                       mime = MEDIA_FORMAT_PCM;
+                       mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
                } else {
                        extractor = extract_input_aacdec_m4a_test;
                        mime = MEDIA_FORMAT_AAC_HE;
@@ -892,7 +909,7 @@ int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
        case MEDIACODEC_AMR_NB:
                if (encoder) {
                        extractor = amrenc_extractor;
-                       mime = MEDIA_FORMAT_PCM;
+                       mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
                        app->is_amr_nb = TRUE;
                } else {
                        extractor = amrdec_extractor;
@@ -902,7 +919,7 @@ int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
        case MEDIACODEC_AMR_WB:
                if (encoder) {
                        extractor = amrenc_extractor;
-                       mime = MEDIA_FORMAT_PCM;
+                       mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
                        app->is_amr_nb = FALSE;
                } else {
                        extractor = amrdec_extractor;
@@ -926,8 +943,9 @@ static void _mediacodec_process_input(App *app)
        media_packet_h pkt = NULL;
        unsigned char *tmp;
        int read;
+       int size;
        int offset;
-       int stride_width, stride_height;
+       int stride_width;
 
        for (i = 0; i < app->frame; i++) {
                g_print("----------read data------------\n");
@@ -935,13 +953,20 @@ static void _mediacodec_process_input(App *app)
                extractor(app, &tmp, &read, &have_frame);
 
                if (have_frame) {
-                       if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
-                               fprintf(stderr, "media_packet_create_alloc failed\n");
-                               return;
-                       }
+#ifdef USE_POOL
+               if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
+                       g_print("media_packet_pool_aquire_packet failed\n");
+                       return;
+               }
+#else
+               if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
+                       g_print("media_packet_create_alloc failed\n");
+                       return;
+               }
+#endif
 
                        if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
-                               fprintf(stderr, "media_packet_set_pts failed\n");
+                               g_print("media_packet_set_pts failed\n");
                                return;
                        }
 
@@ -955,28 +980,47 @@ static void _mediacodec_process_input(App *app)
                                /* Y */
                                media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
                                media_packet_get_video_stride_width(pkt, 0, &stride_width);
-                               media_packet_get_video_stride_height(pkt, 0, &stride_height);
+                               offset = app->width*app->height;
 
-                               offset = stride_width*stride_height;
+                               for (i = 0; i < app->height; i++) {
+                                       memcpy(buf_data_ptr, tmp, app->width);
+                                       buf_data_ptr += stride_width;
+                                       tmp += app->width;
+                               }
 
-                               memcpy(buf_data_ptr, tmp, offset);
+                               if (app->hardware == TRUE) {
+                                       media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
+                                       media_packet_get_video_stride_width(pkt, 1, &stride_width);
+                                       size = app->width * app->height / 2;
 
-                               /* UV or U*/
-                               media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
-                               media_packet_get_video_stride_width(pkt, 1, &stride_width);
-                               media_packet_get_video_stride_height(pkt, 1, &stride_height);
-                               memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
+                                       for (i = 0; i < app->height / 2; i++) {
+                                               memcpy(buf_data_ptr, tmp, app->width);
+                                               buf_data_ptr += stride_width;
+                                               tmp += app->width;
+                                       }
+                               } else {
+                                       /* U */
+                                       media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
+                                       media_packet_get_video_stride_width(pkt, 1, &stride_width);
+                                       size = (app->width>>1) * (app->height>>1);
+
+                                       for (i = 0; i < app->height/2; i++) {
+                                               memcpy(buf_data_ptr, tmp, app->width/2);
+                                               buf_data_ptr += stride_width;
+                                               tmp += app->width/2;
+                                       }
 
-                               if (app->hardware == FALSE) {
                                        /* V */
                                        media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
                                        media_packet_get_video_stride_width(pkt, 2, &stride_width);
-                                       media_packet_get_video_stride_height(pkt, 2, &stride_height);
+                                       offset += size;
 
-                                       offset += stride_width * stride_height;
+                                       for (i = 0; i < app->height/2; i++) {
+                                               memcpy(buf_data_ptr, tmp, app->width/2);
+                                               buf_data_ptr += stride_width;
+                                               tmp += app->width/2;
+                                       }
 
-
-                                       memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
                                }
                        }
                        mc_hex_dump("inbuf", tmp, 48);
@@ -999,9 +1043,11 @@ static gboolean read_data(App *app)
        void *buf_data_ptr = NULL;
        media_packet_h pkt = NULL;
        unsigned char *tmp;
+       int i;
        int read;
+       int size;
        int offset;
-       int stride_width, stride_height;
+       int stride_width;
 
        if (app->offset == 0) {
                app->frame_count = 0;
@@ -1029,20 +1075,21 @@ static gboolean read_data(App *app)
        if (have_frame) {
 #ifdef USE_POOL
                if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
-                       fprintf(stderr, "media_packet_pool_aquire_packet failed\n");
+                       g_print("media_packet_pool_aquire_packet failed\n");
                        return FALSE;
                }
 #else
                if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
-                       fprintf(stderr, "media_packet_create_alloc failed\n");
+                       g_print("media_packet_create_alloc failed\n");
                        return FALSE;
                }
 #endif
                if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
-                       fprintf(stderr, "media_packet_set_pts failed\n");
+                       g_print("media_packet_set_pts failed\n");
                        return FALSE;
                }
 
+
                if (app->type != VIDEO_ENC) {
                        media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
                        media_packet_set_buffer_size(pkt, (uint64_t)read);
@@ -1053,28 +1100,48 @@ static gboolean read_data(App *app)
                        /* Y */
                        media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
                        media_packet_get_video_stride_width(pkt, 0, &stride_width);
-                       media_packet_get_video_stride_height(pkt, 0, &stride_height);
+                       offset = app->width*app->height;
+
+                       for (i = 0; i < app->height; i++) {
+                               memcpy(buf_data_ptr, tmp, app->width);
+                               buf_data_ptr += stride_width;
+                               tmp += app->width;
+                       }
 
-                       offset = stride_width*stride_height;
+                       if (app->hardware == TRUE) {
+                               media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
+                               media_packet_get_video_stride_width(pkt, 1, &stride_width);
+                               size = app->width * app->height>>1;
 
-                       memcpy(buf_data_ptr, tmp, offset);
+                               for (i = 0; i < app->height>>1; i++) {
+                                       memcpy(buf_data_ptr, tmp, app->width);
+                                       buf_data_ptr += stride_width;
+                                       tmp += app->width;
+                               }
+
+                       } else {
+                               /* U */
+                               media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
+                               media_packet_get_video_stride_width(pkt, 1, &stride_width);
+                               size = (app->width>>1) * (app->height>>1);
 
-                       /* UV or U*/
-                       media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
-                       media_packet_get_video_stride_width(pkt, 1, &stride_width);
-                       media_packet_get_video_stride_height(pkt, 1, &stride_height);
-                       memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
+                               for (i = 0; i < app->height/2; i++) {
+                                       memcpy(buf_data_ptr, tmp, app->width>>1);
+                                       buf_data_ptr += stride_width;
+                                       tmp += (app->width>>1);
+                               }
 
-                       if (app->hardware == FALSE) {
                                /* V */
                                media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
                                media_packet_get_video_stride_width(pkt, 2, &stride_width);
-                               media_packet_get_video_stride_height(pkt, 2, &stride_height);
-
-                               offset += stride_width * stride_height;
+                               offset += size;
 
+                               for (i = 0; i < app->height/2; i++) {
+                                       memcpy(buf_data_ptr, tmp, app->width>>1);
+                                       buf_data_ptr += stride_width;
+                                       tmp += (app->width>>1);
+                               }
 
-                               memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
                        }
                }
                mc_hex_dump("inbuf", tmp, 48);
@@ -1177,10 +1244,19 @@ static bool _mediacodec_eos_cb(void *user_data)
        return TRUE;
 }
 
+gboolean  _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
+{
+       g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
+       return TRUE;
+}
+
 static void _mediacodec_prepare(App *app, bool frame_all)
 {
        int ret;
 
+       g_print("supported codec lists -internal-\n");
+       mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
+
        /* create instance */
        ret = mediacodec_create(&app->mc_handle[0]);
        if (ret  != MEDIACODEC_ERROR_NONE) {
@@ -1239,6 +1315,8 @@ static void _mediacodec_prepare(App *app, bool frame_all)
        }
 
        /* set callback */
+       g_print("supported codec lists\n");
+       mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
        mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
        mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
        if (frame_all)
@@ -1260,6 +1338,8 @@ static void _mediacodec_prepare(App *app, bool frame_all)
                g_print("mediacodec_get_packet_pool failed\n");
                return;
        }
+       g_print("\n\nmediacodec start\n\n");
+
        return;
 }
 
@@ -1375,19 +1455,16 @@ static void _mediacodec_destroy(App *app)
 #ifdef USE_POOL
        if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
 
-               fprintf(stderr, "media_packet_pool_deallocatet failed\n");
-               g_print("PKT POOL deallocation failed \n");
+               g_print("media_packet_pool_deallocatet failed\n");
                return;
        }
-       g_print("PKT POOL deallocated! \n");
 
        if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
 
-               fprintf(stderr, " media_packet_pool_destroy failed\n");
-               g_print("PKT POOL destroy failed \n");
+               g_print(" media_packet_pool_destroy failed\n");
                return;
        }
-       g_print("PKT POOL destroyed! \n");
+       g_print("media packet pool destroyed! \n");
 #endif
        mediacodec_destroy(app->mc_handle[0]);
 }
@@ -1686,7 +1763,9 @@ static void interpret(char *cmd, App *app)
        case CURRENT_STATUS_PROCESS_INPUT:
        {
                app->frame = atoi(cmd);
-               _mediacodec_process_input(app);
+
+               if (app->frame > 0 && app->frame < 10)
+                       _mediacodec_process_input(app);
                reset_menu_state();
        }
        break;
@@ -1776,18 +1855,18 @@ void mc_hex_dump(char *desc, void *addr, int len)
        unsigned char *pc = (unsigned char *)addr;
 
        if (desc != NULL)
-               printf("%s:\n", desc);
+               g_print("%s:\n", desc);
 
        for (i = 0; i < len; i++) {
 
                if ((i % 16) == 0) {
                        if (i != 0)
-                               printf("  %s\n", buff);
+                               g_print("  %s\n", buff);
 
-                       printf("  %04x ", i);
+                       g_print("  %04x ", i);
                }
 
-               printf(" %02x", pc[i]);
+               g_print(" %02x", pc[i]);
 
                if ((pc[i] < 0x20) || (pc[i] > 0x7e))
                        buff[i % 16] = '.';
@@ -1797,10 +1876,10 @@ void mc_hex_dump(char *desc, void *addr, int len)
        }
 
        while ((i % 16) != 0) {
-               printf("   ");
+               g_print("   ");
                i++;
        }
-       printf("  %s\n", buff);
+       g_print("  %s\n", buff);
 }
 
 static void decoder_output_dump(App *app, media_packet_h pkt)
@@ -1808,17 +1887,17 @@ static void decoder_output_dump(App *app, media_packet_h pkt)
        void *temp;
        int i = 0;
        int stride_width, stride_height;
-       char filename[100] = {0};
+       gchar filename[100] = {0};
        FILE *fp = NULL;
        int ret = 0;
 
-       sprintf(filename, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
+       g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
        fp = fopen(filename, "ab");
 
        media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
        media_packet_get_video_stride_width(pkt, 0, &stride_width);
        media_packet_get_video_stride_height(pkt, 0, &stride_height);
-       printf("stride : %d, %d\n", stride_width, stride_height);
+       g_print("stride : %d, %d\n", stride_width, stride_height);
 
        for (i = 0; i < app->height; i++) {
                ret = fwrite(temp, app->width, 1, fp);
@@ -1894,12 +1973,12 @@ static void output_dump(App *app, media_packet_h pkt)
 {
        void *temp;
        uint64_t buf_size;
-       char filename[100] = {0};
+       gchar filename[100] = {0};
        FILE *fp = NULL;
        int ret = 0;
        char adts[100] = {0, };
 
-       sprintf(filename, "/tmp/dec_output_dump_%d.out", app->type);
+       g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
        fp = fopen(filename, "ab");
 
        media_packet_get_buffer_data_ptr(pkt, &temp);
@@ -1923,3 +2002,68 @@ static void output_dump(App *app, media_packet_h pkt)
        fclose(fp);
 
 }
+
+const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
+{
+       guint media_codec_id_u = (guint)media_codec_id;
+
+       switch (media_codec_id_u) {
+       case MEDIACODEC_L16:
+               return "L16";
+       case MEDIACODEC_ALAW:
+               return "ALAW";
+       case MEDIACODEC_ULAW:
+               return "ULAW";
+       case MEDIACODEC_AMR_NB:
+               return "AMR_NB";
+       case MEDIACODEC_AMR_WB:
+               return "AMR_WB";
+       case MEDIACODEC_G729:
+               return "G729";
+       case MEDIACODEC_AAC_LC:
+               return "AAC_LC";
+       case MEDIACODEC_AAC_HE:
+               return "AAC_HE";
+       case MEDIACODEC_AAC_HE_PS:
+               return "AAC_HE_PS";
+       case MEDIACODEC_MP3:
+               return "MP3";
+       case MEDIACODEC_VORBIS:
+               return "VORBIS";
+       case MEDIACODEC_FLAC:
+               return "FLAC";
+       case MEDIACODEC_WMAV1:
+               return "WMAV1";
+       case MEDIACODEC_WMAV2:
+               return "WMAV2";
+       case MEDIACODEC_WMAPRO:
+               return "WMAPRO";
+       case MEDIACODEC_WMALSL:
+               return "WMALSL";
+       case MEDIACODEC_H261:
+               return "H261";
+       case MEDIACODEC_H263:
+               return "H263";
+       case MEDIACODEC_H264:
+               return "H264";
+       case MEDIACODEC_MJPEG:
+               return "MJPEG";
+       case MEDIACODEC_MPEG1:
+               return "MPEG1";
+       case MEDIACODEC_MPEG2:
+               return "MPEG2";
+       case MEDIACODEC_MPEG4:
+               return "MPEG4";
+       case MEDIACODEC_HEVC:
+               return "HEVC";
+       case MEDIACODEC_VP8:
+               return "VP8";
+       case MEDIACODEC_VP9:
+               return "VP9";
+       case MEDIACODEC_VC1:
+               return "VC1";
+       default:
+               return "NONE";
+       }
+}
+