2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include <Elementary.h>
21 #include <appcore-efl.h>
24 #include <media_codec.h>
25 #include <media_packet.h>
26 #include <tbm_surface.h>
30 #define PACKAGE "media_codec_test"
35 #define TEST_FILE_SIZE (10 * 1024 * 1024)
36 #define MAX_STRING_LEN 256
38 #define DEFAULT_SAMPPLERATE 44100
39 #define DEFAULT_CHANNEL 2
40 #define DEFAULT_BIT 16
41 #define DEFAULT_BITRATE 128
42 #define DEFAULT_SAMPLEBYTE 1024
43 #define ADTS_HEADER_SIZE 7
44 #define AMRNB_PCM_INPUT_SIZE 320
45 #define AMRWB_PCM_INPUT_SIZE 640
47 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
48 #define GET_IS_ENCODER(x) CHECK_BIT(x, 0)
49 #define GET_IS_DECODER(x) CHECK_BIT(x, 1)
50 #define GET_IS_HW(x) CHECK_BIT(x, 2)
51 #define ES_DEFAULT_VIDEO_PTS_OFFSET 33000000
52 #define CHECK_VALID_PACKET(state, expected_state) \
53 ((state & (expected_state)) == (expected_state))
55 #define AAC_CODECDATA_SIZE 16
57 static int samplebyte = DEFAULT_SAMPLEBYTE;
58 unsigned char buf_adts[ADTS_HEADER_SIZE];
61 MC_EXIST_SPS = 1 << 0,
62 MC_EXIST_PPS = 1 << 1,
63 MC_EXIST_IDR = 1 << 2,
64 MC_EXIST_SLICE = 1 << 3,
66 MC_VALID_HEADER = (MC_EXIST_SPS | MC_EXIST_PPS),
67 MC_VALID_FIRST_SLICE = (MC_EXIST_SPS | MC_EXIST_PPS | MC_EXIST_IDR)
70 typedef struct _App App;
73 CURRENT_STATUS_MAINMENU,
74 CURRENT_STATUS_FILENAME,
75 CURRENT_STATUS_CREATE,
76 CURRENT_STATUS_DESTROY,
77 CURRENT_STATUS_SET_CODEC,
78 CURRENT_STATUS_SET_VDEC_INFO,
79 CURRENT_STATUS_SET_VENC_INFO,
80 CURRENT_STATUS_SET_ADEC_INFO,
81 CURRENT_STATUS_SET_AENC_INFO,
82 CURRENT_STATUS_PREPARE,
83 CURRENT_STATUS_UNPREPARE,
84 CURRENT_STATUS_PROCESS_INPUT,
85 CURRENT_STATUS_GET_OUTPUT,
86 CURRENT_STATUS_RESET_OUTPUT_BUFFER,
87 CURRENT_STATUS_SET_SIZE,
91 NAL_SLICE_NO_PARTITIONING = 1,
97 NAL_SEQUENCE_PARAMETER_SET,
98 NAL_PICTURE_PARAMETER_SET,
99 NAL_PICTURE_DELIMITER,
132 bool is_video[MAX_HANDLE];
133 bool is_encoder[MAX_HANDLE];
137 mediacodec_h mc_handle[MAX_HANDLE];
142 media_format_mimetype_e mime;
157 media_packet_h packet;
165 media_format_h aenc_fmt = NULL;
166 media_format_h adec_fmt = NULL;
167 media_format_h vdec_fmt = NULL;
168 media_format_h venc_fmt = NULL;
174 /* Internal Functions */
175 static int _create_app(void *data);
176 static int _terminate_app(void *data);
177 static void displaymenu(void);
178 static void display_sub_basic();
181 static void mc_hex_dump(char *desc, void *addr, int len);
183 static void decoder_output_dump(App *app, media_packet_h pkt);
187 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
189 int g_menu_state = CURRENT_STATUS_MAINMENU;
191 static int _create_app(void *data)
193 printf("My app is going alive!\n");
194 App *app = (App*)data;
196 g_mutex_init(&app->lock);
200 static int _terminate_app(void *data)
202 printf("My app is going gone!\n");
203 App *app = (App*)data;
205 g_mutex_clear(&app->lock);
210 struct appcore_ops ops = {
211 .create = _create_app,
212 .terminate = _terminate_app,
215 static const guint mp3types_bitrates[2][3][16] = {
217 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
218 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
219 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
222 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
223 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
224 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
228 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
229 {22050, 24000, 16000},
233 void h264_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
235 unsigned char val, zero_count;
236 unsigned char *pNal = app->data + app->offset;
237 int max = app->length - app->offset;
239 int nal_unit_type = 0;
267 if ((zero_count >= 2) && (val == 1))
277 read = (index - zero_count - 1);
279 nal_unit_type = *(app->data+app->offset+4) & 0x1F;
280 g_print("nal_unit_type : %x\n", nal_unit_type);
282 switch (nal_unit_type) {
283 case NAL_SEQUENCE_PARAMETER_SET:
284 g_print("nal_unit_type : SPS\n");
285 state |= MC_EXIST_SPS;
287 case NAL_PICTURE_PARAMETER_SET:
288 g_print("nal_unit_type : PPS\n");
289 state |= MC_EXIST_PPS;
293 g_print ("nal_unit_type : IDR\n");
294 state |= MC_EXIST_IDR;
296 case NAL_SLICE_NO_PARTITIONING:
297 case NAL_SLICE_PART_A:
298 case NAL_SLICE_PART_B:
299 case NAL_SLICE_PART_C:
300 state |= MC_EXIST_SLICE;
303 g_print ("nal_unit_type : %x", nal_unit_type);
307 init = CHECK_VALID_PACKET(state, MC_VALID_FIRST_SLICE) ? 1 : 0;
308 slice = CHECK_VALID_PACKET(state, MC_EXIST_SLICE) ? 1 : 0;
309 idr = CHECK_VALID_PACKET(state, MC_EXIST_IDR) ? 1 : 0;
310 g_print("status : %d, slice : %d, idr : %d\n", init, slice, idr);
312 if (init || idr || slice) {
316 *size = app->offset + read;
318 *data = app->data+app->offset;
323 *data = app->data+app->offset;
330 void h263_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
333 int read_size = 1, state = 1, bStart = 0;
335 unsigned char *pH263 = app->data + app->offset;
337 int max = app->length - app->offset;
342 read_size = (len - 1);
360 if ((val & 0xFC) == 0x80) {
373 app->offset += read_size;
376 void mpeg4_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
380 int state = 1, bType = 0;
382 unsigned char *pMpeg4 = app->data + app->offset;
384 int max = app->length - app->offset;
413 if (val == 0xB0 || val == 0xB6) {
419 if (have_frame && val == 0xB0)
429 app->offset += result;
434 * Extract Input data for AMR-NB/WB decoder
435 * - AMR-NB : mime type ("audio/AMR") / 8Khz / 1 ch / 16 bits
436 * - AMR-WB : mime type ("audio/AMR-WB") / 16Khz / 1 ch / 16 bits
438 static const char AMR_header[] = "#!AMR\n";
439 static const char AMRWB_header[] = "#!AMR-WB\n";
440 #define AMR_NB_MIME_HDR_SIZE 6
441 #define AMR_WB_MIME_HDR_SIZE 9
442 static const int block_size_nb[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
443 static const int block_size_wb[16] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
446 void amrdec_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
448 int readsize = 0, mode_temp;
449 unsigned int fsize, mode;
450 unsigned char *pAmr = app->data + app->offset;
451 //change the below one to frame count
452 if (app->offset == 0) {
453 if (!memcmp(pAmr, AMR_header, AMR_NB_MIME_HDR_SIZE)) {
454 blocksize_tbl = (int *)block_size_nb;
455 mode_temp = pAmr[AMR_NB_MIME_HDR_SIZE];
456 pAmr = pAmr + AMR_NB_MIME_HDR_SIZE;
457 app->offset += AMR_NB_MIME_HDR_SIZE;
459 if (!memcmp(pAmr, AMRWB_header, AMR_WB_MIME_HDR_SIZE)) {
460 blocksize_tbl = (int *)block_size_wb;
461 mode_temp = pAmr[AMR_WB_MIME_HDR_SIZE];
462 pAmr = pAmr + AMR_WB_MIME_HDR_SIZE;
463 app->offset += AMR_WB_MIME_HDR_SIZE;
465 g_print("[ERROR] AMR-NB/WB don't detected..\n");
471 if ((mode_temp & 0x83) == 0) {
472 mode = (mode_temp >> 3) & 0x0F; /* Yep. Retrieve the frame size */
473 fsize = blocksize_tbl[mode];
474 readsize = fsize + 1;
477 g_print("[FAIL] Not found amr frame sync.....\n");
481 app->offset += readsize;
486 void nv12_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
489 int offset = app->length - app->offset;
491 yuv_size = app->width * app->height * 3 / 2;
493 if (offset >= yuv_size)
497 *data = app->data + app->offset;
499 if (offset >= yuv_size)
505 void yuv_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
508 int offset = app->length - app->offset;
510 yuv_size = app->width * app->height * 3 / 2;
512 if (yuv_size >= offset)
516 *data = app->data + app->offset;
518 if (yuv_size >= offset)
523 app->offset += *size;
527 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
530 int offset = app->length - app->offset;
532 read_size = ((samplebyte*app->channel)*(app->bit/8));
537 if (offset >= read_size)
542 app->offset += *size;
545 void amrenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
548 int offset = app->length - app->offset;
551 read_size = AMRNB_PCM_INPUT_SIZE;
553 read_size = AMRWB_PCM_INPUT_SIZE;
557 if (offset >= read_size)
562 app->offset += *size;
566 * Extract Input data for AAC decoder
567 * (case of (LC profile) ADTS format)
568 * codec_data : Don't need
570 void aacdec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
573 int offset = app->length - app->offset;
574 unsigned char *pData = app->data + app->offset;
576 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
577 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
580 g_print("[FAIL] Not found aac frame sync.....\n");
584 *data = app->data + app->offset;
586 if (read_size >= offset)
591 app->offset += *size;
595 void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
599 guint padding, bitrate, lsf=0, layer = 0, mpg25=0;
600 guint hdr_bitrate=0, sf=0;
601 int offset = app->length - app->offset;
602 unsigned char *pData = app->data + app->offset;
604 header = GST_READ_UINT32_BE(pData);
607 g_print ("[ERROR] read header size is 0\n");
611 /* if it's not a valid sync */
612 if ((header & 0xffe00000) != 0xffe00000) {
613 g_print ("[ERROR] invalid sync\n");
617 if (((header >> 19) & 3) == 0x1) {
618 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
621 if (header & (1 << 20)) {
622 lsf = (header & (1 << 19)) ? 0 : 1;
630 /* if it's an invalid layer */
631 if (!((header >> 17) & 3)) {
632 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
635 layer = 4 - ((header >> 17) & 0x3);
638 /* if it's an invalid bitrate */
639 if (((header >> 12) & 0xf) == 0xf) {
640 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
643 bitrate = (header >> 12) & 0xF;
644 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
645 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
646 if (hdr_bitrate == 0)
650 /* if it's an invalid samplerate */
651 if (((header >> 10) & 0x3) == 0x3) {
652 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
656 sf = (header >> 10) & 0x3;
657 sf = mp3types_freqs[lsf + mpg25][sf];
660 padding = (header >> 9) & 0x1;
664 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
667 read_size = (hdr_bitrate * 144) / sf + padding;
671 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
674 g_print("header : %d, read : %d\n", header, read_size);
677 *data = app->data + app->offset;
679 if (read_size >= offset)
684 app->offset += *size;
688 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
690 int readsize = 0, read_size = 0;
691 unsigned int header_size = ADTS_HEADER_SIZE;
692 unsigned char buffer[100000];
693 unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
694 int offset = app->length - app->offset;
695 unsigned char *pData = app->data + app->offset;
697 * It is not support full parsing MP4 container box.
698 * So It MUST start as RAW valid frame sequence.
699 * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
702 //change the below one later
703 if (app->offset == 0) {
705 * CAUTION : Codec data is needed only once in first time
706 * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
707 * So I use this data (byte) as hard coding for temporary our testing.
711 * The codec_data data is according to AudioSpecificConfig,
712 * ISO/IEC 14496-3, 1.6.2.1
714 * below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
715 * case : M4A - LC profile
716 * codec_data=(buffer)119056e5000000000000000000000000
717 * savs aac decoder get codec_data. size: 16 (Tag size : 5 byte)
718 * - codec data: profile : 2
719 * - codec data: samplrate: 48000
720 * - codec data: channels : 2
722 /* 2 bytes are mandatory */
723 codecdata[0] = 0x11; /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
724 codecdata[1] = 0x90; /* ex) (4bit) 2 (2ch) */
725 /* othter bytes are (optional) epconfig information */
731 * below example is test for using "TestSample-EAAC+.m4a"
733 * case : M4A - HE-AAC v1 and v2 profile
734 * codec_data=(buffer)138856e5a54880000000000000000000
735 * savs aac decoder get codec_data. size: 16 (Tag size : 7 byte)
736 * - codec data: profile : 2
737 * - codec data: samplrate: 22050
738 * - codec data: channels : 1
740 /* 2 bytes are mandatory */
741 codecdata[0] = 0x13; /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
742 codecdata[1] = 0x88; /* ex) (4bit) 1 (1ch) */
743 /* othter bytes are (optional) epconfig information */
751 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
752 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
753 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
756 g_print("[FAIL] Not found aac frame sync.....\n");
758 readsize = read_size - header_size;
759 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
760 read_size = readsize + AAC_CODECDATA_SIZE; //return combination of (codec_data + raw_data)
761 app->offset += header_size + readsize;
765 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
766 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
767 readsize = read_size - header_size;
768 memcpy(buffer, pData + 7, readsize); //Make only RAW data, so exclude header 7 bytes
769 read_size = readsize;
770 app->offset += header_size + readsize;
774 g_print("[FAIL] Not found aac frame sync. \n");
779 if (read_size >= offset)
787 * Extract Input data for AAC encoder
790 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
793 int offset = app->length - app->offset;
795 read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
797 if (read_size >= offset)
801 *data = app->data + app->offset;
803 if (read_size >= offset)
808 app->offset += *size;
812 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
815 g_print("Used input buffer = %p\n", pkt);
816 media_packet_destroy(pkt);
821 int _mediacodec_set_codec(int codecid, int flag, bool *hardware)
824 media_format_mimetype_e mime = 0;
825 encoder = GET_IS_ENCODER(flag) ? 1 : 0;
826 *hardware = GET_IS_HW(flag) ? 1 : 0;
829 case MEDIACODEC_H264:
831 extractor = yuv_extractor;
832 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
834 extractor = h264_extractor;
835 mime = MEDIA_FORMAT_H264_SP;
838 case MEDIACODEC_MPEG4:
840 extractor = yuv_extractor;
841 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
843 extractor = mpeg4_extractor;
844 mime = MEDIA_FORMAT_MPEG4_SP;
847 case MEDIACODEC_H263:
849 extractor = h263_extractor;
850 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
852 extractor = h263_extractor;
853 mime = MEDIA_FORMAT_H263P;
858 extractor = aacenc_extractor;
859 mime = MEDIA_FORMAT_PCM;
861 extractor = aacdec_extractor;
862 mime = MEDIA_FORMAT_AAC;
865 case MEDIACODEC_AAC_HE:
867 extractor = aacenc_extractor;
868 mime = MEDIA_FORMAT_PCM;
870 extractor = extract_input_aacdec_m4a_test;
871 mime = MEDIA_FORMAT_AAC_HE;
874 case MEDIACODEC_AAC_HE_PS:
877 extractor = mp3dec_extractor;
878 mime = MEDIA_FORMAT_MP3;
880 case MEDIACODEC_VORBIS:
882 case MEDIACODEC_FLAC:
884 case MEDIACODEC_WMAV1:
886 case MEDIACODEC_WMAV2:
888 case MEDIACODEC_WMAPRO:
890 case MEDIACODEC_WMALSL:
892 case MEDIACODEC_AMR_NB:
893 extractor = amrdec_extractor;
894 mime = MEDIA_FORMAT_AMR_NB;
896 case MEDIACODEC_AMR_WB:
897 extractor = amrdec_extractor;
898 mime = MEDIA_FORMAT_AMR_WB;
901 LOGE("NOT SUPPORTED!!!!");
907 static gboolean read_data(App *app)
910 bool have_frame = FALSE;
912 static guint64 pts = 0L;
913 void *buf_data_ptr = NULL;
914 media_packet_h pkt = NULL;
918 int stride_width, stride_height;
920 g_print("----------read data------------\n");
921 extractor(app, &tmp, &read, &have_frame);
923 if (app->offset >= app->length - 1) {
926 app->finish = clock();
927 g_main_loop_quit(app->loop);
930 g_print("length : %d, offset : %d\n", (int)app->length, (int)app->offset);
932 if (app->offset + len > app->length)
933 len = app->length - app->offset;
935 g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
938 if (media_packet_create_alloc(vdec_fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
939 fprintf(stderr, "media_packet_create_alloc failed\n");
943 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
944 fprintf(stderr, "media_packet_set_pts failed\n");
948 if (app->type != VIDEO_ENC) {
949 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
950 media_packet_set_buffer_size(pkt, (uint64_t)read);
952 memcpy(buf_data_ptr, tmp, read);
953 g_print("tmp:%p, read:%d\n", tmp, read);
956 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
957 media_packet_get_video_stride_width(pkt, 0, &stride_width);
958 media_packet_get_video_stride_height(pkt, 0, &stride_height);
960 offset = stride_width*stride_height;
962 memcpy(buf_data_ptr, tmp, offset);
965 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
966 media_packet_get_video_stride_width(pkt, 1, &stride_width);
967 media_packet_get_video_stride_height(pkt, 1, &stride_height);
968 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
970 if (app->hardware == FALSE) {
972 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
973 media_packet_get_video_stride_width(pkt, 2, &stride_width);
974 media_packet_get_video_stride_height(pkt, 2, &stride_height);
976 offset += stride_width * stride_height;
979 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
982 mc_hex_dump("inbuf", tmp, 48);
984 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
985 if (ret != MEDIACODEC_ERROR_NONE)
988 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
994 static void start_feed(App *app)
996 if (app->sourceid == 0) {
997 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
998 g_print("start_feed\n");
1002 static void stop_feed(App *app)
1004 if (app->sourceid != 0) {
1005 g_source_remove(app->sourceid);
1007 g_print("stop_feed\n");
1011 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1013 g_print("_mediacodec_inbuf_used_cb!!!\n");
1014 media_packet_destroy(pkt);
1018 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1020 media_packet_h out_pkt = NULL;
1023 App *app = (App*)user_data;
1025 g_print("_mediacodec_outbuf_available_cb\n");
1027 g_mutex_lock(&app->lock);
1029 ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1031 if (ret != MEDIACODEC_ERROR_NONE)
1032 g_print("get_output failed\n");
1037 int stride_width, stride_height;
1039 decoder_output_dump(app, out_pkt);
1040 media_packet_get_buffer_data_ptr(out_pkt, &data);
1041 media_packet_get_buffer_size(out_pkt, &buf_size);
1042 g_print("output data : %p, size %d\n", data, (int)buf_size);
1044 fwrite(data, 1, buf_size, fp_out);
1051 g_mutex_unlock(&app->lock);
1053 media_packet_destroy(out_pkt);
1060 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1062 g_print("_mediacodec_buffer_status_cb %d\n", status);
1064 App *app = (App*)user_data;
1066 if (status == MEDIACODEC_NEED_DATA)
1068 else if (status == MEDIACODEC_ENOUGH_DATA)
1074 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1079 static bool _mediacodec_eos_cb(void *user_data)
1084 static void _mediacodec_prepare(App *app)
1089 fp_out = fopen("/tmp/codec_dump.out", "wb");
1091 /* create instance */
1092 ret = mediacodec_create(&app->mc_handle[0]);
1093 if (ret != MEDIACODEC_ERROR_NONE) {
1094 g_print("mediacodec_create failed\n");
1099 ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1100 if (ret != MEDIACODEC_ERROR_NONE) {
1101 g_print("mediacodec_set_codec failed\n");
1106 app->mime = _mediacodec_set_codec(app->codecid, app->flag, &app->hardware);
1108 /* set codec info */
1109 ret = media_format_create(&vdec_fmt);
1111 switch (app->type) {
1113 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1114 media_format_set_video_mime(vdec_fmt, app->mime);
1115 media_format_set_video_width(vdec_fmt, app->width);
1116 media_format_set_video_height(vdec_fmt, app->height);
1119 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1120 media_format_set_video_mime(vdec_fmt, app->mime);
1121 media_format_set_video_width(vdec_fmt, app->width);
1122 media_format_set_video_height(vdec_fmt, app->height);
1123 media_format_set_video_avg_bps(vdec_fmt, app->target_bits);
1126 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1127 media_format_set_audio_mime(vdec_fmt, app->mime);
1128 media_format_set_audio_channel(vdec_fmt, app->channel);
1129 media_format_set_audio_samplerate(vdec_fmt, app->samplerate);
1130 media_format_set_audio_bit(vdec_fmt, app->bit);
1133 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1134 media_format_set_audio_mime(vdec_fmt, app->mime);
1135 media_format_set_audio_channel(vdec_fmt, app->channel);
1136 media_format_set_audio_samplerate(vdec_fmt, app->samplerate);
1137 media_format_set_audio_bit(vdec_fmt, app->bit);
1140 g_print("invaild type\n");
1144 if (ret != MEDIACODEC_ERROR_NONE) {
1145 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1150 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1151 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1152 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1153 mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1154 mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1157 ret = mediacodec_prepare(app->mc_handle[0]);
1158 if (ret != MEDIACODEC_ERROR_NONE) {
1159 g_print("mediacodec_prepare failed\n");
1163 app->frame_count = 0;
1164 app->start = clock();
1165 g_main_loop_run(app->loop);
1167 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1169 g_print("---------------------------\n");
1174 static void input_filepath(char *filename, App *app)
1176 GError *error = NULL;
1179 app->file = g_mapped_file_new(filename, FALSE, &error);
1181 g_print("failed to open file : %s\n", error->message);
1182 g_error_free(error);
1186 app->length = g_mapped_file_get_length(app->file);
1187 app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1189 g_print("len : %d, offset : %d, obj : %d", app->length, (int)app->offset, app->obj);
1204 void reset_menu_state()
1206 g_menu_state = CURRENT_STATUS_MAINMENU;
1210 void _interpret_main_menu(char *cmd, App *app)
1212 int len = strlen(cmd);
1214 if (strncmp(cmd, "a", 1) == 0)
1215 g_menu_state = CURRENT_STATUS_FILENAME;
1216 else if (strncmp(cmd, "o", 1) == 0)
1217 g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1218 else if (strncmp(cmd, "q", 1) == 0)
1221 g_print("unknown menu \n");
1222 } else if (len == 2) {
1223 if (strncmp(cmd, "pr", 2) == 0)
1224 _mediacodec_prepare(app);
1225 else if (strncmp(cmd, "sc", 2) == 0)
1226 g_menu_state = CURRENT_STATUS_SET_CODEC;
1227 else if (strncmp(cmd, "vd", 2) == 0)
1228 g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1229 else if (strncmp(cmd, "ve", 2) == 0)
1230 g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1231 else if (strncmp(cmd, "ad", 2) == 0)
1232 g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1233 else if (strncmp(cmd, "ae", 2) == 0)
1234 g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1235 else if (strncmp(cmd, "pi", 2) == 0)
1236 g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1238 display_sub_basic();
1240 g_print("unknown menu \n");
1246 static void displaymenu(void)
1248 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1249 display_sub_basic();
1250 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1251 g_print("*** input mediapath.\n");
1252 } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1253 g_print("*** Codec id : Select Codec ID Numbe (e.g. AAC_LC = 96)\n");
1254 g_print(" L16 = 16 (0x10)\n");
1255 g_print(" ALAW = 32 (0x20)\n");
1256 g_print(" ULAW = 48 (0x30)\n");
1257 g_print(" AMR_NB = 64 (0x40)\n");
1258 g_print(" AMR_WB = 65 (0x41)\n");
1259 g_print(" G729 = 80 (0x50)\n");
1260 g_print(" AAC_LC = 96 (0x60)\n");
1261 g_print(" AAC_HE = 97 (0x61)\n");
1262 g_print(" AAC_PS = 98 (0x62)\n");
1263 g_print(" MP3 = 112 (0x70)\n");
1264 g_print(" VORBIS = 128 (0x80)\n");
1265 g_print(" FLAC = 144 (0x90)\n");
1266 g_print(" WMAV1 = 160 (0xA0)\n");
1267 g_print(" WMAV2 = 161 (0xA1)\n");
1268 g_print(" WMAPRO = 162 (0xA2)\n");
1269 g_print(" WMALSL = 163 (0xA3)\n");
1270 g_print(" -------------------\n");
1271 g_print(" H261 = 101\n");
1272 g_print(" H263 = 102\n");
1273 g_print(" H264 = 103\n");
1274 g_print(" MJPEG = 104\n");
1275 g_print(" MPEG1 = 105\n");
1276 g_print(" MPEG2 = 106\n");
1277 g_print(" MPEG4 = 107\n");
1278 g_print(" -------------------\n");
1279 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1280 g_print(" CODEC : ENCODER = 1 DECODER = 2\n");
1281 g_print(" TYPE : HW = 4 SW = 8\n");
1282 g_print("*** input codec id, falgs.\n");
1283 } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1284 g_print("*** input video decode configure.(width, height)\n");
1285 } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1286 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1287 } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1288 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000, 2, 16))\n");
1289 } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1290 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000, 2, 16, 128000))\n");
1291 } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1292 g_print("*** input dec process number\n");
1293 } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1294 g_print("*** input get output buffer number\n");
1296 g_print("*** unknown status.\n");
1301 gboolean timeout_menu_display(void* data)
1308 static void interpret(char *cmd, App *app)
1310 switch (g_menu_state) {
1311 case CURRENT_STATUS_MAINMENU:
1312 _interpret_main_menu(cmd, app);
1314 case CURRENT_STATUS_FILENAME:
1315 input_filepath(cmd, app);
1318 case CURRENT_STATUS_SET_CODEC:
1331 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1332 tmp = strtol(cmd, ptr, 16);
1333 app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1335 app->codecid = 0x1000 + tmp;
1340 app->flag = atoi(cmd);
1349 case CURRENT_STATUS_SET_VDEC_INFO:
1354 app->width = atoi(cmd);
1358 app->height = atoi(cmd);
1359 app->type = VIDEO_DEC;
1369 case CURRENT_STATUS_SET_VENC_INFO:
1374 app->width = atoi(cmd);
1378 app->height = atoi(cmd);
1382 app->fps = atol(cmd);
1386 app->target_bits = atoi(cmd);
1387 app->type = VIDEO_ENC;
1397 case CURRENT_STATUS_SET_ADEC_INFO:
1402 app->samplerate = atoi(cmd);
1406 app->channel = atoi(cmd);
1410 app->bit = atoi(cmd);
1411 app->type = AUDIO_DEC;
1421 case CURRENT_STATUS_SET_AENC_INFO:
1426 app->samplerate = atoi(cmd);
1430 app->channel = atoi(cmd);
1434 app->bit = atoi(cmd);
1438 app->bitrate = atoi(cmd);
1439 app->type = AUDIO_ENC;
1449 case CURRENT_STATUS_PROCESS_INPUT:
1454 case CURRENT_STATUS_GET_OUTPUT:
1463 g_timeout_add(100, timeout_menu_display, 0);
1466 static void display_sub_basic()
1469 g_print("=========================================================================================\n");
1470 g_print(" media codec test\n");
1471 g_print("-----------------------------------------------------------------------------------------\n");
1472 g_print("a. Create \t\t");
1473 g_print("sc. Set codec \n");
1474 g_print("vd. Set vdec info \t");
1475 g_print("ve. Set venc info \n");
1476 g_print("ad. Set adec info \t");
1477 g_print("ae. Set aenc info \n");
1478 g_print("pr. Prepare \t\t");
1479 g_print("pi. Process input \n");
1480 g_print("o. Get output \t\t");
1481 g_print("rb. Reset output buffer \n");
1482 g_print("pa. Process all frames \n");
1483 g_print("un. Unprepare \t\t");
1484 g_print("dt. Destroy \t\t");
1485 g_print("q. quite test suite \t");
1487 g_print("=========================================================================================\n");
1490 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1492 gchar buf[MAX_STRING_LEN];
1494 GError *error = NULL;
1495 App *context = (App*)data;
1497 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1500 interpret(buf, context);
1505 int main(int argc, char *argv[])
1509 GIOChannel *stdin_channel;
1510 stdin_channel = g_io_channel_unix_new(0);
1511 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1512 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1515 app->loop = g_main_loop_new(NULL, TRUE);
1516 app->timer = g_timer_new();
1522 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1527 void mc_hex_dump(char *desc, void *addr, int len)
1530 unsigned char buff[17];
1531 unsigned char *pc = (unsigned char *)addr;
1534 printf("%s:\n", desc);
1536 for (i = 0; i < len; i++) {
1538 if ((i % 16) == 0) {
1540 printf(" %s\n", buff);
1542 printf(" %04x ", i);
1545 printf(" %02x", pc[i]);
1547 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1550 buff[i % 16] = pc[i];
1551 buff[(i % 16) + 1] = '\0';
1554 while ((i % 16) != 0) {
1558 printf(" %s\n", buff);
1562 static void decoder_output_dump(App *app, media_packet_h pkt)
1566 int stride_width, stride_height;
1567 char filename[100] = {0};
1571 sprintf(filename, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1572 fp = fopen(filename, "ab");
1574 media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1575 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1576 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1577 printf("stride : %d, %d\n", stride_width, stride_height);
1579 for (i = 0; i < app->height; i++) {
1580 ret = fwrite(temp, app->width, 1, fp);
1581 temp += stride_width;
1584 if (app->hardware == TRUE) {
1585 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1586 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1587 for (i = 0; i < app->height/2; i++) {
1588 ret = fwrite(temp, app->width, 1, fp);
1589 temp += stride_width;
1592 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1593 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1594 for (i = 0; i < app->height/2; i++) {
1595 ret = fwrite(temp, app->width/2, 1, fp);
1596 temp += stride_width;
1599 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1600 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1601 for (i = 0; i < app->height/2; i++) {
1602 ret = fwrite(temp, app->width/2, 1, fp);
1603 temp += stride_width;
1607 g_print("codec dec output dumped!!%d\n", ret);