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 <tbm_surface.h>
28 #include <media_codec.h>
29 #include <media_packet.h>
30 #include <media_packet_pool.h>
31 #include <media_codec_internal.h>
33 #define PACKAGE "media_codec_test"
38 #define TEST_FILE_SIZE (10 * 1024 * 1024)
39 #define MAX_STRING_LEN 256
41 #define DEFAULT_SAMPPLERATE 44100
42 #define DEFAULT_CHANNEL 2
43 #define DEFAULT_BIT 16
44 #define DEFAULT_BITRATE 128
45 #define DEFAULT_SAMPLEBYTE 1024
46 #define ADTS_HEADER_SIZE 7
47 #define AMRNB_PCM_INPUT_SIZE 320
48 #define AMRWB_PCM_INPUT_SIZE 640
50 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
51 #define GET_IS_ENCODER(x) CHECK_BIT(x, 0)
52 #define GET_IS_DECODER(x) CHECK_BIT(x, 1)
53 #define GET_IS_HW(x) CHECK_BIT(x, 2)
54 #define ES_DEFAULT_VIDEO_PTS_OFFSET 33000000
55 #define CHECK_VALID_PACKET(state, expected_state) \
56 ((state & (expected_state)) == (expected_state))
58 #define AAC_CODECDATA_SIZE 16
61 unsigned char buf_adts[ADTS_HEADER_SIZE];
64 MC_EXIST_SPS = 1 << 0,
65 MC_EXIST_PPS = 1 << 1,
66 MC_EXIST_IDR = 1 << 2,
67 MC_EXIST_SLICE = 1 << 3,
69 MC_VALID_HEADER = (MC_EXIST_SPS | MC_EXIST_PPS),
70 MC_VALID_FIRST_SLICE = (MC_EXIST_SPS | MC_EXIST_PPS | MC_EXIST_IDR)
73 typedef struct _App App;
76 CURRENT_STATUS_MAINMENU,
77 CURRENT_STATUS_FILENAME,
78 CURRENT_STATUS_CREATE,
79 CURRENT_STATUS_DESTROY,
80 CURRENT_STATUS_SET_CODEC,
81 CURRENT_STATUS_SET_VDEC_INFO,
82 CURRENT_STATUS_SET_VENC_INFO,
83 CURRENT_STATUS_SET_ADEC_INFO,
84 CURRENT_STATUS_SET_AENC_INFO,
85 CURRENT_STATUS_PREPARE,
86 CURRENT_STATUS_UNPREPARE,
87 CURRENT_STATUS_PROCESS_INPUT,
88 CURRENT_STATUS_GET_OUTPUT,
89 CURRENT_STATUS_RESET_OUTPUT_BUFFER,
90 CURRENT_STATUS_SET_SIZE,
94 NAL_SLICE_NO_PARTITIONING = 1,
100 NAL_SEQUENCE_PARAMETER_SET,
101 NAL_PICTURE_PARAMETER_SET,
102 NAL_PICTURE_DELIMITER,
141 camera_h camera_handle;
143 mediacodec_h mc_handle[MAX_HANDLE];
148 media_format_mimetype_e mime;
163 media_packet_h packet;
171 media_format_h fmt = NULL;
172 media_packet_pool_h pkt_pool = 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();
180 static void _mediacodec_unprepare(App *app);
182 static void mc_hex_dump(char *desc, void *addr, int len);
183 static void decoder_output_dump(App *app, media_packet_h pkt);
184 static void output_dump(App *app, media_packet_h pkt);
186 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id);
188 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
190 int g_menu_state = CURRENT_STATUS_MAINMENU;
192 static int _create_app(void *data)
194 g_print("My app is going alive!\n");
195 App *app = (App*)data;
197 g_mutex_init(&app->lock);
201 static int _terminate_app(void *data)
203 g_print("My app is going gone!\n");
204 App *app = (App*)data;
206 g_mutex_clear(&app->lock);
211 struct appcore_ops ops = {
212 .create = _create_app,
213 .terminate = _terminate_app,
216 static const guint mp3types_bitrates[2][3][16] = {
218 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
219 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
220 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
223 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
224 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
225 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
229 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
230 {22050, 24000, 16000},
234 void h264_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
236 unsigned char val, zero_count;
237 unsigned char *pNal = app->data + app->offset;
238 int max = app->length - app->offset;
240 int nal_unit_type = 0;
268 if ((zero_count >= 2) && (val == 1))
278 read = (index - zero_count - 1);
280 nal_unit_type = *(app->data+app->offset+4) & 0x1F;
281 g_print("nal_unit_type : %x\n", nal_unit_type);
283 switch (nal_unit_type) {
284 case NAL_SEQUENCE_PARAMETER_SET:
285 g_print("nal_unit_type : SPS\n");
286 state |= MC_EXIST_SPS;
288 case NAL_PICTURE_PARAMETER_SET:
289 g_print("nal_unit_type : PPS\n");
290 state |= MC_EXIST_PPS;
294 g_print ("nal_unit_type : IDR\n");
295 state |= MC_EXIST_IDR;
297 case NAL_SLICE_NO_PARTITIONING:
298 case NAL_SLICE_PART_A:
299 case NAL_SLICE_PART_B:
300 case NAL_SLICE_PART_C:
301 state |= MC_EXIST_SLICE;
304 g_print ("nal_unit_type : %x", nal_unit_type);
308 init = CHECK_VALID_PACKET(state, MC_VALID_FIRST_SLICE) ? 1 : 0;
309 slice = CHECK_VALID_PACKET(state, MC_EXIST_SLICE) ? 1 : 0;
310 idr = CHECK_VALID_PACKET(state, MC_EXIST_IDR) ? 1 : 0;
311 g_print("status : %d, slice : %d, idr : %d\n", init, slice, idr);
313 if (init || idr || slice) {
317 *size = app->offset + read;
319 *data = app->data+app->offset;
324 *data = app->data+app->offset;
331 void h263_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
334 int read_size = 1, state = 1, bStart = 0;
336 unsigned char *pH263 = app->data + app->offset;
338 int max = app->length - app->offset;
342 read_size = (len - 1);
359 if ((val & 0xFC) == 0x80) {
372 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) {
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 int write_amr_header = 1; /* write magic number for AMR Header at one time */
439 static const char AMR_header[] = "#!AMR\n";
440 static const char AMRWB_header[] = "#!AMR-WB\n";
441 #define AMR_NB_MIME_HDR_SIZE 6
442 #define AMR_WB_MIME_HDR_SIZE 9
443 static const int block_size_nb[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
444 static const int block_size_wb[16] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
447 void amrdec_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
449 int readsize = 0, mode_temp;
450 unsigned int fsize, mode;
451 unsigned char *pAmr = app->data + app->offset;
452 /* change the below one to frame count */
453 if (app->offset == 0) {
454 if (!memcmp(pAmr, AMR_header, AMR_NB_MIME_HDR_SIZE)) {
455 blocksize_tbl = (int *)block_size_nb;
456 mode_temp = pAmr[AMR_NB_MIME_HDR_SIZE];
457 pAmr = pAmr + AMR_NB_MIME_HDR_SIZE;
458 app->offset += AMR_NB_MIME_HDR_SIZE;
460 if (!memcmp(pAmr, AMRWB_header, AMR_WB_MIME_HDR_SIZE)) {
461 blocksize_tbl = (int *)block_size_wb;
462 mode_temp = pAmr[AMR_WB_MIME_HDR_SIZE];
463 pAmr = pAmr + AMR_WB_MIME_HDR_SIZE;
464 app->offset += AMR_WB_MIME_HDR_SIZE;
466 g_print("[ERROR] AMR-NB/WB don't detected..\n");
472 if ((mode_temp & 0x83) == 0) {
473 mode = (mode_temp >> 3) & 0x0F; /* Yep. Retrieve the frame size */
474 fsize = blocksize_tbl[mode];
475 readsize = fsize + 1;
478 g_print("[FAIL] Not found amr frame sync.....\n");
482 app->offset += readsize;
487 void nv12_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
490 int offset = app->length - app->offset;
492 yuv_size = app->width * app->height * 3 / 2;
494 if (offset >= yuv_size)
498 *data = app->data + app->offset;
500 if (offset >= yuv_size)
506 void yuv_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
509 int offset = app->length - app->offset;
511 yuv_size = app->width * app->height * 3 / 2;
513 if (yuv_size >= offset)
517 *data = app->data + app->offset;
519 if (yuv_size >= offset)
524 app->offset += *size;
528 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
531 int offset = app->length - app->offset;
533 read_size = ((DEFAULT_SAMPLEBYTE * app->channel)*(app->bit/8) * 2);
536 *data = app->data + app->offset;
538 if (read_size >= offset)
543 app->offset += *size;
546 void amrenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
549 int offset = app->length - app->offset;
552 read_size = AMRNB_PCM_INPUT_SIZE;
554 read_size = AMRWB_PCM_INPUT_SIZE;
557 *data = app->data + app->offset;
559 if (read_size >= offset)
564 app->offset += *size;
568 * Extract Input data for AAC decoder
569 * (case of (LC profile) ADTS format)
570 * codec_data : Don't need
572 void aacdec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
575 int offset = app->length - app->offset;
576 unsigned char *pData = app->data + app->offset;
578 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
579 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
582 g_print("[FAIL] Not found aac frame sync.....\n");
586 *data = app->data + app->offset;
588 if (read_size >= offset)
593 app->offset += *size;
597 void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
601 guint padding, bitrate, lsf = 0, layer = 0, mpg25 = 0;
602 guint hdr_bitrate = 0, sf = 0;
603 int offset = app->length - app->offset;
604 unsigned char *pData = app->data + app->offset;
606 header = GST_READ_UINT32_BE(pData);
609 g_print ("[ERROR] read header size is 0\n");
614 /* if it's not a valid sync */
615 if ((header & 0xffe00000) != 0xffe00000) {
616 g_print ("[ERROR] invalid sync\n");
621 if (((header >> 19) & 3) == 0x1) {
622 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
626 if (header & (1 << 20)) {
627 lsf = (header & (1 << 19)) ? 0 : 1;
635 /* if it's an invalid layer */
636 if (!((header >> 17) & 3)) {
637 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
641 layer = 4 - ((header >> 17) & 0x3);
649 /* if it's an invalid bitrate */
650 if (((header >> 12) & 0xf) == 0xf) {
651 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
655 bitrate = (header >> 12) & 0xF;
656 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
657 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
658 if (hdr_bitrate == 0) {
664 /* if it's an invalid samplerate */
665 if (((header >> 10) & 0x3) == 0x3) {
666 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
670 sf = (header >> 10) & 0x3;
671 sf = mp3types_freqs[lsf + mpg25][sf];
674 padding = (header >> 9) & 0x1;
678 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
681 read_size = (hdr_bitrate * 144) / sf + padding;
685 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
688 g_print("header : %d, read : %d\n", header, read_size);
691 *data = app->data + app->offset;
693 if (read_size >= offset)
698 app->offset += *size;
702 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
704 int readsize = 0, read_size = 0;
705 unsigned int header_size = ADTS_HEADER_SIZE;
706 unsigned char buffer[100000];
707 unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
708 int offset = app->length - app->offset;
709 unsigned char *pData = app->data + app->offset;
711 * It is not support full parsing MP4 container box.
712 * So It MUST start as RAW valid frame sequence.
713 * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
716 /* change the below one later */
717 if (app->offset == 0) {
719 * CAUTION : Codec data is needed only once in first time
720 * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
721 * So I use this data (byte) as hard coding for temporary our testing.
725 * The codec_data data is according to AudioSpecificConfig,
726 * ISO/IEC 14496-3, 1.6.2.1
728 * below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
729 * case : M4A - LC profile
730 * codec_data=(buffer)119056e5000000000000000000000000
731 * savs aac decoder get codec_data. size: 16 (Tag size : 5 byte)
732 * - codec data: profile : 2
733 * - codec data: samplrate: 48000
734 * - codec data: channels : 2
736 /* 2 bytes are mandatory */
737 codecdata[0] = 0x11; /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
738 codecdata[1] = 0x90; /* ex) (4bit) 2 (2ch) */
739 /* othter bytes are (optional) epconfig information */
745 * below example is test for using "TestSample-EAAC+.m4a"
747 * case : M4A - HE-AAC v1 and v2 profile
748 * codec_data=(buffer)138856e5a54880000000000000000000
749 * savs aac decoder get codec_data. size: 16 (Tag size : 7 byte)
750 * - codec data: profile : 2
751 * - codec data: samplrate: 22050
752 * - codec data: channels : 1
754 /* 2 bytes are mandatory */
755 codecdata[0] = 0x13; /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
756 codecdata[1] = 0x88; /* ex) (4bit) 1 (1ch) */
757 /* othter bytes are (optional) epconfig information */
765 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
766 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
767 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
770 g_print("[FAIL] Not found aac frame sync.....\n");
772 readsize = read_size - header_size;
773 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
774 read_size = readsize + AAC_CODECDATA_SIZE; /* return combination of (codec_data + raw_data) */
775 app->offset += header_size + readsize;
779 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
780 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
781 readsize = read_size - header_size;
782 memcpy(buffer, pData + 7, readsize); /* Make only RAW data, so exclude header 7 bytes */
783 read_size = readsize;
784 app->offset += header_size + readsize;
788 g_print("[FAIL] Not found aac frame sync. \n");
793 if (read_size >= offset)
801 * Extract Input data for AAC encoder
804 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
807 int offset = app->length - app->offset;
809 read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
811 if (read_size >= offset)
815 *data = app->data + app->offset;
817 if (read_size >= offset)
822 app->offset += *size;
826 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
829 g_print("Used input buffer = %p\n", pkt);
830 media_packet_destroy(pkt);
835 int _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
838 media_format_mimetype_e mime = 0;
839 encoder = GET_IS_ENCODER(flag) ? 1 : 0;
840 *hardware = GET_IS_HW(flag) ? 1 : 0;
841 app->is_encoder = encoder;
844 case MEDIACODEC_H264:
846 extractor = yuv_extractor;
847 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
849 extractor = h264_extractor;
850 mime = MEDIA_FORMAT_H264_SP;
853 case MEDIACODEC_MPEG4:
855 extractor = yuv_extractor;
856 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
858 extractor = mpeg4_extractor;
859 mime = MEDIA_FORMAT_MPEG4_SP;
862 case MEDIACODEC_H263:
864 extractor = yuv_extractor;
865 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
867 extractor = h263_extractor;
868 mime = MEDIA_FORMAT_H263P;
873 extractor = aacenc_extractor;
874 mime = MEDIA_FORMAT_PCM;
876 extractor = aacdec_extractor;
877 mime = MEDIA_FORMAT_AAC;
880 case MEDIACODEC_AAC_HE:
882 extractor = aacenc_extractor;
883 mime = MEDIA_FORMAT_PCM;
885 extractor = extract_input_aacdec_m4a_test;
886 mime = MEDIA_FORMAT_AAC_HE;
889 case MEDIACODEC_AAC_HE_PS:
892 extractor = mp3dec_extractor;
893 mime = MEDIA_FORMAT_MP3;
895 case MEDIACODEC_VORBIS:
897 case MEDIACODEC_FLAC:
899 case MEDIACODEC_WMAV1:
901 case MEDIACODEC_WMAV2:
903 case MEDIACODEC_WMAPRO:
905 case MEDIACODEC_WMALSL:
907 case MEDIACODEC_AMR_NB:
909 extractor = amrenc_extractor;
910 mime = MEDIA_FORMAT_PCM;
911 app->is_amr_nb = TRUE;
913 extractor = amrdec_extractor;
914 mime = MEDIA_FORMAT_AMR_NB;
917 case MEDIACODEC_AMR_WB:
919 extractor = amrenc_extractor;
920 mime = MEDIA_FORMAT_PCM;
921 app->is_amr_nb = FALSE;
923 extractor = amrdec_extractor;
924 mime = MEDIA_FORMAT_AMR_WB;
928 LOGE("NOT SUPPORTED!!!!");
934 static void _mediacodec_process_input(App *app)
937 bool have_frame = FALSE;
939 static guint64 pts = 0L;
940 void *buf_data_ptr = NULL;
941 media_packet_h pkt = NULL;
948 for (i = 0; i < app->frame; i++) {
949 g_print("----------read data------------\n");
951 extractor(app, &tmp, &read, &have_frame);
955 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
956 g_print("media_packet_pool_aquire_packet failed\n");
960 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
961 g_print("media_packet_create_alloc failed\n");
966 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
967 g_print("media_packet_set_pts failed\n");
971 if (app->type != VIDEO_ENC) {
972 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
973 media_packet_set_buffer_size(pkt, (uint64_t)read);
975 memcpy(buf_data_ptr, tmp, read);
976 g_print("tmp:%p, read:%d\n", tmp, read);
979 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
980 media_packet_get_video_stride_width(pkt, 0, &stride_width);
981 offset = app->width*app->height;
983 for (i = 0; i < app->height; i++) {
984 memcpy(buf_data_ptr, tmp, app->width);
985 buf_data_ptr += stride_width;
989 if (app->hardware == TRUE) {
990 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
991 media_packet_get_video_stride_width(pkt, 1, &stride_width);
992 size = app->width * app->height / 2;
994 for (i = 0; i < app->height / 2; i++) {
995 memcpy(buf_data_ptr, tmp, app->width);
996 buf_data_ptr += stride_width;
1001 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1002 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1003 size = (app->width>>1) * (app->height>>1);
1005 for (i = 0; i < app->height/2; i++) {
1006 memcpy(buf_data_ptr, tmp, app->width/2);
1007 buf_data_ptr += stride_width;
1008 tmp += app->width/2;
1012 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1013 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1016 for (i = 0; i < app->height/2; i++) {
1017 memcpy(buf_data_ptr, tmp, app->width/2);
1018 buf_data_ptr += stride_width;
1019 tmp += app->width/2;
1024 mc_hex_dump("inbuf", tmp, 48);
1026 ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
1027 if (ret != MEDIACODEC_ERROR_NONE)
1030 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1035 static gboolean read_data(App *app)
1038 bool have_frame = FALSE;
1040 static guint64 pts = 0L;
1041 void *buf_data_ptr = NULL;
1042 media_packet_h pkt = NULL;
1050 if (app->offset == 0) {
1051 app->frame_count = 0;
1052 app->start = clock();
1055 g_print("----------read data------------\n");
1056 extractor(app, &tmp, &read, &have_frame);
1058 if (app->offset >= app->length - 4) {
1061 app->finish = clock();
1062 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1063 g_print("---------------------------\n");
1066 g_print("length : %d, offset : %d\n", app->length, app->offset);
1068 if (app->offset + len > app->length)
1069 len = app->length - app->offset;
1071 g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1075 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1076 g_print("media_packet_pool_aquire_packet failed\n");
1080 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1081 g_print("media_packet_create_alloc failed\n");
1085 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1086 g_print("media_packet_set_pts failed\n");
1091 if (app->type != VIDEO_ENC) {
1092 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1093 media_packet_set_buffer_size(pkt, (uint64_t)read);
1095 memcpy(buf_data_ptr, tmp, read);
1096 g_print("tmp:%p, read:%d\n", tmp, read);
1099 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1100 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1101 offset = app->width*app->height;
1103 for (i = 0; i < app->height; i++) {
1104 memcpy(buf_data_ptr, tmp, app->width);
1105 buf_data_ptr += stride_width;
1109 if (app->hardware == TRUE) {
1110 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1111 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1112 size = app->width * app->height>>1;
1114 for (i = 0; i < app->height>>1; i++) {
1115 memcpy(buf_data_ptr, tmp, app->width);
1116 buf_data_ptr += stride_width;
1122 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1123 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1124 size = (app->width>>1) * (app->height>>1);
1126 for (i = 0; i < app->height/2; i++) {
1127 memcpy(buf_data_ptr, tmp, app->width>>1);
1128 buf_data_ptr += stride_width;
1129 tmp += (app->width>>1);
1133 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1134 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1137 for (i = 0; i < app->height/2; i++) {
1138 memcpy(buf_data_ptr, tmp, app->width>>1);
1139 buf_data_ptr += stride_width;
1140 tmp += (app->width>>1);
1145 mc_hex_dump("inbuf", tmp, 48);
1147 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1148 if (ret != MEDIACODEC_ERROR_NONE)
1151 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1157 static void start_feed(App *app)
1159 if (app->sourceid == 0) {
1160 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1161 g_print("start_feed\n");
1165 static void stop_feed(App *app)
1167 if (app->sourceid != 0) {
1168 g_source_remove(app->sourceid);
1170 g_print("stop_feed\n");
1174 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1176 g_print("_mediacodec_inbuf_used_cb!!!\n");
1178 media_packet_pool_release_packet(pkt_pool, pkt);
1180 media_packet_destroy(pkt);
1186 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1188 media_packet_h out_pkt = NULL;
1191 App *app = (App*)user_data;
1193 g_print("_mediacodec_outbuf_available_cb\n");
1195 g_mutex_lock(&app->lock);
1197 ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1199 if (ret != MEDIACODEC_ERROR_NONE)
1200 g_print("get_output failed\n");
1202 if (app->enable_dump) {
1203 if (app->type == VIDEO_DEC)
1204 decoder_output_dump(app, out_pkt);
1206 output_dump(app, out_pkt);
1212 g_mutex_unlock(&app->lock);
1214 media_packet_destroy(out_pkt);
1221 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1223 g_print("_mediacodec_buffer_status_cb %d\n", status);
1225 App *app = (App*)user_data;
1227 if (status == MEDIACODEC_NEED_DATA)
1229 else if (status == MEDIACODEC_ENOUGH_DATA)
1235 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1240 static bool _mediacodec_eos_cb(void *user_data)
1245 void _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
1247 g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
1251 static void _mediacodec_prepare(App *app, bool frame_all)
1255 g_print("supported codec lists -internal-\n");
1256 mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
1258 /* create instance */
1259 ret = mediacodec_create(&app->mc_handle[0]);
1260 if (ret != MEDIACODEC_ERROR_NONE) {
1261 g_print("mediacodec_create failed\n");
1266 ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1267 if (ret != MEDIACODEC_ERROR_NONE) {
1268 g_print("mediacodec_set_codec failed\n");
1272 app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1274 /* set codec info */
1275 ret = media_format_create(&fmt);
1277 switch (app->type) {
1279 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1280 media_format_set_video_mime(fmt, app->mime);
1281 media_format_set_video_width(fmt, app->width);
1282 media_format_set_video_height(fmt, app->height);
1285 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1286 media_format_set_video_mime(fmt, app->mime);
1287 media_format_set_video_width(fmt, app->width);
1288 media_format_set_video_height(fmt, app->height);
1289 media_format_set_video_avg_bps(fmt, app->target_bits);
1292 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1293 media_format_set_audio_mime(fmt, app->mime);
1294 media_format_set_audio_channel(fmt, app->channel);
1295 media_format_set_audio_samplerate(fmt, app->samplerate);
1296 media_format_set_audio_bit(fmt, app->bit);
1299 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1300 media_format_set_audio_mime(fmt, app->mime);
1301 media_format_set_audio_channel(fmt, app->channel);
1302 media_format_set_audio_samplerate(fmt, app->samplerate);
1303 media_format_set_audio_bit(fmt, app->bit);
1306 g_print("invaild type\n");
1310 if (ret != MEDIACODEC_ERROR_NONE) {
1311 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1316 g_print("supported codec lists\n");
1317 mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
1318 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1319 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1321 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1322 mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1323 mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1326 ret = mediacodec_prepare(app->mc_handle[0]);
1327 if (ret != MEDIACODEC_ERROR_NONE) {
1328 g_print("mediacodec_prepare failed\n");
1333 /* get packet pool instance */
1334 ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1335 if (ret != MEDIA_PACKET_ERROR_NONE) {
1336 g_print("mediacodec_get_packet_pool failed\n");
1339 g_print("\n\nmediacodec start\n\n");
1344 static void _mediacodec_enc_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1346 /* release input raw packet */
1347 media_packet_destroy(pkt);
1350 /* this callback is called when the input buffer for codec has done to use */
1351 static void _mediacodec_dec_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1353 /* release input encoded packet */
1354 media_packet_destroy(pkt);
1357 static void _mediacodec_enc_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1359 App *app = (App*)user_data;
1361 mediacodec_h media_codec_handle = app->mc_handle[1];
1362 media_packet_h output_buf = NULL;
1363 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1364 /* decode encoded camera preview */
1365 mediacodec_process_input(app->mc_handle[0], output_buf, 0);
1368 static void _mediacodec_dec_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1370 App *app = (App*)user_data;
1372 mediacodec_h media_codec_handle = app->mc_handle[0];
1373 media_packet_h output_buf = NULL;
1375 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1377 if (app->enable_dump)
1378 decoder_output_dump(app, output_buf);
1380 media_packet_destroy(output_buf);
1383 static void _media_packet_preview_cb(media_packet_h packet, void *user_data)
1385 App *app = user_data;
1386 g_mutex_lock(&app->lock);
1387 mediacodec_process_input(app->mc_handle[1], packet, 0);
1388 g_mutex_unlock(&app->lock);
1393 static void _mediacodec_camera_start(App *app)
1395 int default_format = CAMERA_PIXEL_FORMAT_NV12;
1398 app->hardware = TRUE;
1400 /*create decoder instance and setup */
1401 mediacodec_create(&app->mc_handle[0]);
1402 mediacodec_set_codec(app->mc_handle[0], MEDIACODEC_H264, MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW);
1403 mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1405 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], _mediacodec_dec_input_buffer_used_cb, NULL);
1406 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], _mediacodec_dec_output_buffer_available_cb, app);
1407 mediacodec_prepare(app->mc_handle[0]);
1409 /*create encoder instance and setup */
1410 mediacodec_create(&app->mc_handle[1]);
1411 mediacodec_set_codec(app->mc_handle[1], MEDIACODEC_H264, MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW);
1412 mediacodec_set_venc_info(app->mc_handle[1], app->width, app->height, 30, 1000);
1414 mediacodec_set_input_buffer_used_cb(app->mc_handle[1], _mediacodec_enc_input_buffer_used_cb, NULL);
1415 mediacodec_set_output_buffer_available_cb(app->mc_handle[1], _mediacodec_enc_output_buffer_available_cb, app);
1416 mediacodec_prepare(app->mc_handle[1]);
1418 /* create camera instance and setup and then start preview */
1419 camera_create(CAMERA_DEVICE_CAMERA0, &app->camera_handle);
1420 camera_set_media_packet_preview_cb(app->camera_handle, _media_packet_preview_cb, app);
1421 camera_get_preview_format(app->camera_handle, &default_format);
1422 camera_set_preview_format(app->camera_handle, default_format);
1423 camera_set_preview_resolution(app->camera_handle, app->width, app->height);
1424 camera_set_display(app->camera_handle, CAMERA_DISPLAY_TYPE_NONE, NULL);
1425 camera_start_preview(app->camera_handle);
1431 static void _mediacodec_camera_stop(App *app)
1433 camera_state_e camera_state = CAMERA_STATE_NONE;
1435 camera_get_state(app->camera_handle, &camera_state);
1436 camera_stop_preview(app->camera_handle);
1437 camera_destroy(app->camera_handle);
1439 mediacodec_unprepare(app->mc_handle[0]);
1440 mediacodec_unprepare(app->mc_handle[1]);
1441 mediacodec_destroy(app->mc_handle[0]);
1442 mediacodec_destroy(app->mc_handle[1]);
1446 static void _mediacodec_unprepare(App *app)
1448 mediacodec_unprepare(app->mc_handle[0]);
1451 static void _mediacodec_destroy(App *app)
1454 if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1456 g_print("media_packet_pool_deallocatet failed\n");
1460 if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1462 g_print(" media_packet_pool_destroy failed\n");
1465 g_print("media packet pool destroyed! \n");
1467 mediacodec_destroy(app->mc_handle[0]);
1470 static void input_filepath(char *filename, App *app)
1472 GError *error = NULL;
1475 app->file = g_mapped_file_new(filename, FALSE, &error);
1477 g_print("failed to open file : %s\n", error->message);
1478 g_error_free(error);
1482 app->length = g_mapped_file_get_length(app->file);
1483 app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1485 g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1490 void quit_program(App *app)
1492 media_format_unref(fmt);
1493 g_main_loop_quit(app->loop);
1498 void reset_menu_state()
1500 g_menu_state = CURRENT_STATUS_MAINMENU;
1504 void _interpret_main_menu(char *cmd, App *app)
1506 int len = strlen(cmd);
1508 if (strncmp(cmd, "a", 1) == 0)
1509 g_menu_state = CURRENT_STATUS_FILENAME;
1510 else if (strncmp(cmd, "o", 1) == 0)
1511 g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1512 else if (strncmp(cmd, "q", 1) == 0)
1515 g_print("unknown menu \n");
1516 } else if (len == 2) {
1517 if (strncmp(cmd, "pr", 2) == 0)
1518 _mediacodec_prepare(app, 0);
1519 else if (strncmp(cmd, "pa", 2) == 0)
1520 _mediacodec_prepare(app, 1);
1521 else if (strncmp(cmd, "sc", 2) == 0)
1522 g_menu_state = CURRENT_STATUS_SET_CODEC;
1523 else if (strncmp(cmd, "vd", 2) == 0)
1524 g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1525 else if (strncmp(cmd, "ve", 2) == 0)
1526 g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1527 else if (strncmp(cmd, "ad", 2) == 0)
1528 g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1529 else if (strncmp(cmd, "ae", 2) == 0)
1530 g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1531 else if (strncmp(cmd, "pi", 2) == 0)
1532 g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1533 else if (strncmp(cmd, "un", 2) == 0)
1534 _mediacodec_unprepare(app);
1535 else if (strncmp(cmd, "dt", 2) == 0)
1536 _mediacodec_destroy(app);
1537 else if (strncmp(cmd, "cr", 2) == 0)
1538 _mediacodec_camera_start(app);
1539 else if (strncmp(cmd, "ct", 2) == 0)
1540 _mediacodec_camera_stop(app);
1541 else if (strncmp(cmd, "dp", 2) == 0) {
1542 if (!app->enable_dump) {
1543 app->enable_dump = TRUE;
1544 g_print("dump enabled\n");
1546 app->enable_dump = FALSE;
1547 g_print("dump disabled\n");
1550 display_sub_basic();
1552 g_print("unknown menu \n");
1558 static void displaymenu(void)
1560 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1561 display_sub_basic();
1562 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1563 g_print("*** input mediapath.\n");
1564 } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1565 g_print("*** Codec id : Select Codec ID Numbe (e.g. AAC_LC = 96)\n");
1566 g_print(" L16 = 16 (0x10)\n");
1567 g_print(" ALAW = 32 (0x20)\n");
1568 g_print(" ULAW = 48 (0x30)\n");
1569 g_print(" AMR_NB = 64 (0x40)\n");
1570 g_print(" AMR_WB = 65 (0x41)\n");
1571 g_print(" G729 = 80 (0x50)\n");
1572 g_print(" AAC_LC = 96 (0x60)\n");
1573 g_print(" AAC_HE = 97 (0x61)\n");
1574 g_print(" AAC_PS = 98 (0x62)\n");
1575 g_print(" MP3 = 112 (0x70)\n");
1576 g_print(" VORBIS = 128 (0x80)\n");
1577 g_print(" FLAC = 144 (0x90)\n");
1578 g_print(" WMAV1 = 160 (0xA0)\n");
1579 g_print(" WMAV2 = 161 (0xA1)\n");
1580 g_print(" WMAPRO = 162 (0xA2)\n");
1581 g_print(" WMALSL = 163 (0xA3)\n");
1582 g_print(" -------------------\n");
1583 g_print(" H261 = 101\n");
1584 g_print(" H263 = 102\n");
1585 g_print(" H264 = 103\n");
1586 g_print(" MJPEG = 104\n");
1587 g_print(" MPEG1 = 105\n");
1588 g_print(" MPEG2 = 106\n");
1589 g_print(" MPEG4 = 107\n");
1590 g_print(" -------------------\n");
1591 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1592 g_print(" CODEC : ENCODER = 1 DECODER = 2\n");
1593 g_print(" TYPE : HW = 4 SW = 8\n");
1594 g_print("*** input codec id, falgs.\n");
1595 } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1596 g_print("*** input video decode configure.(width, height)\n");
1597 } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1598 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1599 } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1600 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000, 2, 16))\n");
1601 } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1602 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000, 2, 16, 128000))\n");
1603 } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1604 g_print("*** input dec process number\n");
1605 } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1606 g_print("*** input get output buffer number\n");
1608 g_print("*** unknown status.\n");
1613 gboolean timeout_menu_display(void* data)
1620 static void interpret(char *cmd, App *app)
1622 switch (g_menu_state) {
1623 case CURRENT_STATUS_MAINMENU:
1624 _interpret_main_menu(cmd, app);
1626 case CURRENT_STATUS_FILENAME:
1627 input_filepath(cmd, app);
1630 case CURRENT_STATUS_SET_CODEC:
1643 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1644 tmp = strtol(cmd, ptr, 16);
1645 app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1647 app->codecid = 0x1000 + tmp;
1652 app->flag = atoi(cmd);
1661 case CURRENT_STATUS_SET_VDEC_INFO:
1666 app->width = atoi(cmd);
1670 app->height = atoi(cmd);
1671 app->type = VIDEO_DEC;
1681 case CURRENT_STATUS_SET_VENC_INFO:
1686 app->width = atoi(cmd);
1690 app->height = atoi(cmd);
1694 app->fps = atol(cmd);
1698 app->target_bits = atoi(cmd);
1699 app->type = VIDEO_ENC;
1709 case CURRENT_STATUS_SET_ADEC_INFO:
1714 app->samplerate = atoi(cmd);
1718 app->channel = atoi(cmd);
1722 app->bit = atoi(cmd);
1723 app->type = AUDIO_DEC;
1733 case CURRENT_STATUS_SET_AENC_INFO:
1738 app->samplerate = atoi(cmd);
1742 app->channel = atoi(cmd);
1746 app->bit = atoi(cmd);
1750 app->bitrate = atoi(cmd);
1751 app->type = AUDIO_ENC;
1761 case CURRENT_STATUS_PROCESS_INPUT:
1763 app->frame = atoi(cmd);
1766 _mediacodec_process_input(app);
1770 case CURRENT_STATUS_GET_OUTPUT:
1779 g_timeout_add(100, timeout_menu_display, 0);
1782 static void display_sub_basic()
1785 g_print("=========================================================================================\n");
1786 g_print(" media codec test\n");
1787 g_print("-----------------------------------------------------------------------------------------\n");
1788 g_print("a. Create \t\t");
1789 g_print("sc. Set codec \n");
1790 g_print("vd. Set vdec info \t");
1791 g_print("ve. Set venc info \n");
1792 g_print("ad. Set adec info \t");
1793 g_print("ae. Set aenc info \n");
1794 g_print("pr. Prepare \t");
1795 g_print("pa. Prepare and process all\t\t");
1796 g_print("pi. process input with num\n");
1797 g_print("o. Get output \t\t");
1798 g_print("rb. Reset output buffer \n");
1799 g_print("un. Unprepare \t\t");
1800 g_print("dt. Destroy \t\t");
1801 g_print("q. quit test suite \n");
1802 g_print("dp. enable dump \n");
1803 g_print("-----------------------------------------------------------------------------------------\n");
1804 g_print("cr. camera preview -> encoder -> decoder\n");
1805 g_print("ct. quit camera test\n");
1807 g_print("=========================================================================================\n");
1810 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1812 gchar buf[MAX_STRING_LEN];
1814 GError *error = NULL;
1815 App *context = (App*)data;
1817 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1820 interpret(buf, context);
1825 int main(int argc, char *argv[])
1829 GIOChannel *stdin_channel;
1830 stdin_channel = g_io_channel_unix_new(0);
1831 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1832 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1836 app->loop = g_main_loop_new(NULL, TRUE);
1837 app->timer = g_timer_new();
1838 g_main_loop_run(app->loop);
1844 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1849 void mc_hex_dump(char *desc, void *addr, int len)
1852 unsigned char buff[17];
1853 unsigned char *pc = (unsigned char *)addr;
1856 g_print("%s:\n", desc);
1858 for (i = 0; i < len; i++) {
1860 if ((i % 16) == 0) {
1862 g_print(" %s\n", buff);
1864 g_print(" %04x ", i);
1867 g_print(" %02x", pc[i]);
1869 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1872 buff[i % 16] = pc[i];
1873 buff[(i % 16) + 1] = '\0';
1876 while ((i % 16) != 0) {
1880 g_print(" %s\n", buff);
1883 static void decoder_output_dump(App *app, media_packet_h pkt)
1887 int stride_width, stride_height;
1888 gchar filename[100] = {0};
1892 g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1893 fp = fopen(filename, "ab");
1895 media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1896 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1897 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1898 g_print("stride : %d, %d\n", stride_width, stride_height);
1900 for (i = 0; i < app->height; i++) {
1901 ret = fwrite(temp, app->width, 1, fp);
1902 temp += stride_width;
1905 if (app->hardware == TRUE) {
1906 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1907 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1908 for (i = 0; i < app->height/2; i++) {
1909 ret = fwrite(temp, app->width, 1, fp);
1910 temp += stride_width;
1913 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1914 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1915 for (i = 0; i < app->height/2; i++) {
1916 ret = fwrite(temp, app->width/2, 1, fp);
1917 temp += stride_width;
1920 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1921 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1922 for (i = 0; i < app->height/2; i++) {
1923 ret = fwrite(temp, app->width/2, 1, fp);
1924 temp += stride_width;
1928 g_print("codec dec output dumped!!%d\n", ret);
1934 * Add ADTS header at the beginning of each and every AAC packet.
1935 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
1936 * Note the packetLen must count in the ADTS header itself.
1938 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1940 int profile = 2; /* AAC LC (0x01) */
1941 int freqIdx = 3; /* 48KHz (0x03) */
1942 int chanCfg = 2; /* CPE (0x02) */
1944 if (app->samplerate == 96000) freqIdx = 0;
1945 else if (app->samplerate == 88200) freqIdx = 1;
1946 else if (app->samplerate == 64000) freqIdx = 2;
1947 else if (app->samplerate == 48000) freqIdx = 3;
1948 else if (app->samplerate == 44100) freqIdx = 4;
1949 else if (app->samplerate == 32000) freqIdx = 5;
1950 else if (app->samplerate == 24000) freqIdx = 6;
1951 else if (app->samplerate == 22050) freqIdx = 7;
1952 else if (app->samplerate == 16000) freqIdx = 8;
1953 else if (app->samplerate == 12000) freqIdx = 9;
1954 else if (app->samplerate == 11025) freqIdx = 10;
1955 else if (app->samplerate == 8000) freqIdx = 11;
1957 if ((app->channel == 1) || (app->channel == 2))
1958 chanCfg = app->channel;
1960 /* fill in ADTS data */
1961 buffer[0] = (char)0xFF;
1962 buffer[1] = (char)0xF1;
1963 buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1964 buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1965 buffer[4] = (char)((packetLen&0x7FF) >> 3);
1966 buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1967 buffer[6] = (char)0xFC;
1970 static void output_dump(App *app, media_packet_h pkt)
1974 gchar filename[100] = {0};
1977 char adts[100] = {0, };
1979 g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
1980 fp = fopen(filename, "ab");
1982 media_packet_get_buffer_data_ptr(pkt, &temp);
1983 media_packet_get_buffer_size(pkt, &buf_size);
1984 g_print("output data : %p, size %d\n", temp, (int)buf_size);
1986 if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1987 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1988 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1989 g_print("adts appended\n");
1990 } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1) {
1991 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1992 g_print("%s - AMR_header write in first frame\n", __func__);
1993 fwrite(&AMR_header[0], 1, sizeof(AMR_header) - 1, fp); /* AMR-NB magic number */
1994 write_amr_header = 0;
1997 fwrite(temp, (int)buf_size, 1, fp);
1999 g_print("codec dec output dumped!!%d\n", ret);
2004 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
2006 guint media_codec_id_u = (guint)media_codec_id;
2008 switch (media_codec_id_u) {
2009 case MEDIACODEC_L16:
2011 case MEDIACODEC_ALAW:
2013 case MEDIACODEC_ULAW:
2015 case MEDIACODEC_AMR_NB:
2017 case MEDIACODEC_AMR_WB:
2019 case MEDIACODEC_G729:
2021 case MEDIACODEC_AAC_LC:
2023 case MEDIACODEC_AAC_HE:
2025 case MEDIACODEC_AAC_HE_PS:
2027 case MEDIACODEC_MP3:
2029 case MEDIACODEC_VORBIS:
2031 case MEDIACODEC_FLAC:
2033 case MEDIACODEC_WMAV1:
2035 case MEDIACODEC_WMAV2:
2037 case MEDIACODEC_WMAPRO:
2039 case MEDIACODEC_WMALSL:
2041 case MEDIACODEC_H261:
2043 case MEDIACODEC_H263:
2045 case MEDIACODEC_H264:
2047 case MEDIACODEC_MJPEG:
2049 case MEDIACODEC_MPEG1:
2051 case MEDIACODEC_MPEG2:
2053 case MEDIACODEC_MPEG4:
2055 case MEDIACODEC_HEVC:
2057 case MEDIACODEC_VP8:
2059 case MEDIACODEC_VP9:
2061 case MEDIACODEC_VC1: