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);
644 /* if it's an invalid bitrate */
645 if (((header >> 12) & 0xf) == 0xf) {
646 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
650 bitrate = (header >> 12) & 0xF;
651 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
652 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
653 if (hdr_bitrate == 0) {
659 /* if it's an invalid samplerate */
660 if (((header >> 10) & 0x3) == 0x3) {
661 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
665 sf = (header >> 10) & 0x3;
666 sf = mp3types_freqs[lsf + mpg25][sf];
669 padding = (header >> 9) & 0x1;
673 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
676 read_size = (hdr_bitrate * 144) / sf + padding;
680 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
683 g_print("header : %d, read : %d\n", header, read_size);
686 *data = app->data + app->offset;
688 if (read_size >= offset)
693 app->offset += *size;
697 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
699 int readsize = 0, read_size = 0;
700 unsigned int header_size = ADTS_HEADER_SIZE;
701 unsigned char buffer[100000];
702 unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
703 int offset = app->length - app->offset;
704 unsigned char *pData = app->data + app->offset;
706 * It is not support full parsing MP4 container box.
707 * So It MUST start as RAW valid frame sequence.
708 * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
711 /* change the below one later */
712 if (app->offset == 0) {
714 * CAUTION : Codec data is needed only once in first time
715 * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
716 * So I use this data (byte) as hard coding for temporary our testing.
720 * The codec_data data is according to AudioSpecificConfig,
721 * ISO/IEC 14496-3, 1.6.2.1
723 * below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
724 * case : M4A - LC profile
725 * codec_data=(buffer)119056e5000000000000000000000000
726 * savs aac decoder get codec_data. size: 16 (Tag size : 5 byte)
727 * - codec data: profile : 2
728 * - codec data: samplrate: 48000
729 * - codec data: channels : 2
731 /* 2 bytes are mandatory */
732 codecdata[0] = 0x11; /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
733 codecdata[1] = 0x90; /* ex) (4bit) 2 (2ch) */
734 /* othter bytes are (optional) epconfig information */
740 * below example is test for using "TestSample-EAAC+.m4a"
742 * case : M4A - HE-AAC v1 and v2 profile
743 * codec_data=(buffer)138856e5a54880000000000000000000
744 * savs aac decoder get codec_data. size: 16 (Tag size : 7 byte)
745 * - codec data: profile : 2
746 * - codec data: samplrate: 22050
747 * - codec data: channels : 1
749 /* 2 bytes are mandatory */
750 codecdata[0] = 0x13; /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
751 codecdata[1] = 0x88; /* ex) (4bit) 1 (1ch) */
752 /* othter bytes are (optional) epconfig information */
760 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
761 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
762 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
765 g_print("[FAIL] Not found aac frame sync.....\n");
767 readsize = read_size - header_size;
768 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
769 read_size = readsize + AAC_CODECDATA_SIZE; /* return combination of (codec_data + raw_data) */
770 app->offset += header_size + readsize;
774 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
775 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
776 readsize = read_size - header_size;
777 memcpy(buffer, pData + 7, readsize); /* Make only RAW data, so exclude header 7 bytes */
778 read_size = readsize;
779 app->offset += header_size + readsize;
781 if (app->offset > app->length) {
784 g_print("[FAIL] offset error \n");
790 g_print("[FAIL] Not found aac frame sync. \n");
795 if (read_size >= offset)
803 * Extract Input data for AAC encoder
806 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
809 int offset = app->length - app->offset;
811 read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
813 if (read_size >= offset)
817 *data = app->data + app->offset;
819 if (read_size >= offset)
824 app->offset += *size;
828 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
831 g_print("Used input buffer = %p\n", pkt);
832 media_packet_destroy(pkt);
837 int _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
840 media_format_mimetype_e mime = 0;
841 encoder = GET_IS_ENCODER(flag) ? 1 : 0;
842 *hardware = GET_IS_HW(flag) ? 1 : 0;
843 app->is_encoder = encoder;
846 case MEDIACODEC_H264:
848 extractor = yuv_extractor;
849 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
851 extractor = h264_extractor;
852 mime = MEDIA_FORMAT_H264_SP;
855 case MEDIACODEC_MPEG4:
857 extractor = yuv_extractor;
858 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
860 extractor = mpeg4_extractor;
861 mime = MEDIA_FORMAT_MPEG4_SP;
864 case MEDIACODEC_H263:
866 extractor = yuv_extractor;
867 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
869 extractor = h263_extractor;
870 mime = MEDIA_FORMAT_H263P;
875 extractor = aacenc_extractor;
876 mime = MEDIA_FORMAT_PCM_F32LE; /* FIXME need to check according to verdor */
878 extractor = aacdec_extractor;
879 mime = MEDIA_FORMAT_AAC;
882 case MEDIACODEC_AAC_HE:
884 extractor = aacenc_extractor;
885 mime = MEDIA_FORMAT_PCM_F32LE; /* FIXME need to check according to verdor */
887 extractor = extract_input_aacdec_m4a_test;
888 mime = MEDIA_FORMAT_AAC_HE;
891 case MEDIACODEC_AAC_HE_PS:
894 extractor = mp3dec_extractor;
895 mime = MEDIA_FORMAT_MP3;
897 case MEDIACODEC_VORBIS:
899 case MEDIACODEC_FLAC:
901 case MEDIACODEC_WMAV1:
903 case MEDIACODEC_WMAV2:
905 case MEDIACODEC_WMAPRO:
907 case MEDIACODEC_WMALSL:
909 case MEDIACODEC_AMR_NB:
911 extractor = amrenc_extractor;
912 mime = MEDIA_FORMAT_PCM_F32LE; /* FIXME need to check according to verdor */
913 app->is_amr_nb = TRUE;
915 extractor = amrdec_extractor;
916 mime = MEDIA_FORMAT_AMR_NB;
919 case MEDIACODEC_AMR_WB:
921 extractor = amrenc_extractor;
922 mime = MEDIA_FORMAT_PCM_F32LE; /* FIXME need to check according to verdor */
923 app->is_amr_nb = FALSE;
925 extractor = amrdec_extractor;
926 mime = MEDIA_FORMAT_AMR_WB;
930 LOGE("NOT SUPPORTED!!!!");
936 static void _mediacodec_process_input(App *app)
939 bool have_frame = FALSE;
941 static guint64 pts = 0L;
942 void *buf_data_ptr = NULL;
943 media_packet_h pkt = NULL;
950 for (i = 0; i < app->frame; i++) {
951 g_print("----------read data------------\n");
953 extractor(app, &tmp, &read, &have_frame);
957 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
958 g_print("media_packet_pool_aquire_packet failed\n");
962 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
963 g_print("media_packet_create_alloc failed\n");
968 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
969 g_print("media_packet_set_pts failed\n");
973 if (app->type != VIDEO_ENC) {
974 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
975 media_packet_set_buffer_size(pkt, (uint64_t)read);
977 memcpy(buf_data_ptr, tmp, read);
978 g_print("tmp:%p, read:%d\n", tmp, read);
981 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
982 media_packet_get_video_stride_width(pkt, 0, &stride_width);
983 offset = app->width*app->height;
985 for (i = 0; i < app->height; i++) {
986 memcpy(buf_data_ptr, tmp, app->width);
987 buf_data_ptr += stride_width;
991 if (app->hardware == TRUE) {
992 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
993 media_packet_get_video_stride_width(pkt, 1, &stride_width);
994 size = app->width * app->height / 2;
996 for (i = 0; i < app->height / 2; i++) {
997 memcpy(buf_data_ptr, tmp, app->width);
998 buf_data_ptr += stride_width;
1003 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1004 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1005 size = (app->width>>1) * (app->height>>1);
1007 for (i = 0; i < app->height/2; i++) {
1008 memcpy(buf_data_ptr, tmp, app->width/2);
1009 buf_data_ptr += stride_width;
1010 tmp += app->width/2;
1014 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1015 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1018 for (i = 0; i < app->height/2; i++) {
1019 memcpy(buf_data_ptr, tmp, app->width/2);
1020 buf_data_ptr += stride_width;
1021 tmp += app->width/2;
1026 mc_hex_dump("inbuf", tmp, 48);
1028 ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
1029 if (ret != MEDIACODEC_ERROR_NONE)
1032 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1037 static gboolean read_data(App *app)
1040 bool have_frame = FALSE;
1042 static guint64 pts = 0L;
1043 void *buf_data_ptr = NULL;
1044 media_packet_h pkt = NULL;
1052 if (app->offset == 0) {
1053 app->frame_count = 0;
1054 app->start = clock();
1057 g_print("----------read data------------\n");
1058 extractor(app, &tmp, &read, &have_frame);
1060 if (app->offset >= app->length - 4) {
1063 app->finish = clock();
1064 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1065 g_print("---------------------------\n");
1068 g_print("length : %d, offset : %d\n", app->length, app->offset);
1070 if (app->offset + len > app->length)
1071 len = app->length - app->offset;
1073 g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1077 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1078 g_print("media_packet_pool_aquire_packet failed\n");
1082 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1083 g_print("media_packet_create_alloc failed\n");
1087 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1088 g_print("media_packet_set_pts failed\n");
1093 if (app->type != VIDEO_ENC) {
1094 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1095 media_packet_set_buffer_size(pkt, (uint64_t)read);
1097 memcpy(buf_data_ptr, tmp, read);
1098 g_print("tmp:%p, read:%d\n", tmp, read);
1101 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1102 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1103 offset = app->width*app->height;
1105 for (i = 0; i < app->height; i++) {
1106 memcpy(buf_data_ptr, tmp, app->width);
1107 buf_data_ptr += stride_width;
1111 if (app->hardware == TRUE) {
1112 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1113 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1114 size = app->width * app->height>>1;
1116 for (i = 0; i < app->height>>1; i++) {
1117 memcpy(buf_data_ptr, tmp, app->width);
1118 buf_data_ptr += stride_width;
1124 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1125 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1126 size = (app->width>>1) * (app->height>>1);
1128 for (i = 0; i < app->height/2; i++) {
1129 memcpy(buf_data_ptr, tmp, app->width>>1);
1130 buf_data_ptr += stride_width;
1131 tmp += (app->width>>1);
1135 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1136 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1139 for (i = 0; i < app->height/2; i++) {
1140 memcpy(buf_data_ptr, tmp, app->width>>1);
1141 buf_data_ptr += stride_width;
1142 tmp += (app->width>>1);
1147 mc_hex_dump("inbuf", tmp, 48);
1149 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1150 if (ret != MEDIACODEC_ERROR_NONE)
1153 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1159 static void start_feed(App *app)
1161 if (app->sourceid == 0) {
1162 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1163 g_print("start_feed\n");
1167 static void stop_feed(App *app)
1169 if (app->sourceid != 0) {
1170 g_source_remove(app->sourceid);
1172 g_print("stop_feed\n");
1176 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1178 g_print("_mediacodec_inbuf_used_cb!!!\n");
1180 media_packet_pool_release_packet(pkt_pool, pkt);
1182 media_packet_destroy(pkt);
1188 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1190 media_packet_h out_pkt = NULL;
1193 App *app = (App*)user_data;
1195 g_print("_mediacodec_outbuf_available_cb\n");
1197 g_mutex_lock(&app->lock);
1199 ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1201 if (ret != MEDIACODEC_ERROR_NONE)
1202 g_print("get_output failed\n");
1204 if (app->enable_dump) {
1205 if (app->type == VIDEO_DEC)
1206 decoder_output_dump(app, out_pkt);
1208 output_dump(app, out_pkt);
1214 g_mutex_unlock(&app->lock);
1216 media_packet_destroy(out_pkt);
1223 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1225 g_print("_mediacodec_buffer_status_cb %d\n", status);
1227 App *app = (App*)user_data;
1229 if (status == MEDIACODEC_NEED_DATA)
1231 else if (status == MEDIACODEC_ENOUGH_DATA)
1237 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1242 static bool _mediacodec_eos_cb(void *user_data)
1247 gboolean _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
1249 g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
1253 static void _mediacodec_prepare(App *app, bool frame_all)
1257 g_print("supported codec lists -internal-\n");
1258 mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
1260 /* create instance */
1261 ret = mediacodec_create(&app->mc_handle[0]);
1262 if (ret != MEDIACODEC_ERROR_NONE) {
1263 g_print("mediacodec_create failed\n");
1268 ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1269 if (ret != MEDIACODEC_ERROR_NONE) {
1270 g_print("mediacodec_set_codec failed\n");
1274 app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1276 /* set codec info */
1277 ret = media_format_create(&fmt);
1279 switch (app->type) {
1281 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1282 media_format_set_video_mime(fmt, app->mime);
1283 media_format_set_video_width(fmt, app->width);
1284 media_format_set_video_height(fmt, app->height);
1287 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1288 media_format_set_video_mime(fmt, app->mime);
1289 media_format_set_video_width(fmt, app->width);
1290 media_format_set_video_height(fmt, app->height);
1291 media_format_set_video_avg_bps(fmt, app->target_bits);
1294 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1295 media_format_set_audio_mime(fmt, app->mime);
1296 media_format_set_audio_channel(fmt, app->channel);
1297 media_format_set_audio_samplerate(fmt, app->samplerate);
1298 media_format_set_audio_bit(fmt, app->bit);
1301 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1302 media_format_set_audio_mime(fmt, app->mime);
1303 media_format_set_audio_channel(fmt, app->channel);
1304 media_format_set_audio_samplerate(fmt, app->samplerate);
1305 media_format_set_audio_bit(fmt, app->bit);
1308 g_print("invaild type\n");
1312 if (ret != MEDIACODEC_ERROR_NONE) {
1313 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1318 g_print("supported codec lists\n");
1319 mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
1320 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1321 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1323 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1324 mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1325 mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1328 ret = mediacodec_prepare(app->mc_handle[0]);
1329 if (ret != MEDIACODEC_ERROR_NONE) {
1330 g_print("mediacodec_prepare failed\n");
1335 /* get packet pool instance */
1336 ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1337 if (ret != MEDIA_PACKET_ERROR_NONE) {
1338 g_print("mediacodec_get_packet_pool failed\n");
1341 g_print("\n\nmediacodec start\n\n");
1346 static void _mediacodec_enc_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1348 /* release input raw packet */
1349 media_packet_destroy(pkt);
1352 /* this callback is called when the input buffer for codec has done to use */
1353 static void _mediacodec_dec_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1355 /* release input encoded packet */
1356 media_packet_destroy(pkt);
1359 static void _mediacodec_enc_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1361 App *app = (App*)user_data;
1363 mediacodec_h media_codec_handle = app->mc_handle[1];
1364 media_packet_h output_buf = NULL;
1365 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1366 /* decode encoded camera preview */
1367 mediacodec_process_input(app->mc_handle[0], output_buf, 0);
1370 static void _mediacodec_dec_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1372 App *app = (App*)user_data;
1374 mediacodec_h media_codec_handle = app->mc_handle[0];
1375 media_packet_h output_buf = NULL;
1377 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1379 if (app->enable_dump)
1380 decoder_output_dump(app, output_buf);
1382 media_packet_destroy(output_buf);
1385 static void _media_packet_preview_cb(media_packet_h packet, void *user_data)
1387 App *app = user_data;
1388 g_mutex_lock(&app->lock);
1389 mediacodec_process_input(app->mc_handle[1], packet, 0);
1390 g_mutex_unlock(&app->lock);
1395 static void _mediacodec_camera_start(App *app)
1397 int default_format = CAMERA_PIXEL_FORMAT_NV12;
1400 app->hardware = TRUE;
1402 /*create decoder instance and setup */
1403 mediacodec_create(&app->mc_handle[0]);
1404 mediacodec_set_codec(app->mc_handle[0], MEDIACODEC_H264, MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW);
1405 mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1407 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], _mediacodec_dec_input_buffer_used_cb, NULL);
1408 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], _mediacodec_dec_output_buffer_available_cb, app);
1409 mediacodec_prepare(app->mc_handle[0]);
1411 /*create encoder instance and setup */
1412 mediacodec_create(&app->mc_handle[1]);
1413 mediacodec_set_codec(app->mc_handle[1], MEDIACODEC_H264, MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW);
1414 mediacodec_set_venc_info(app->mc_handle[1], app->width, app->height, 30, 1000);
1416 mediacodec_set_input_buffer_used_cb(app->mc_handle[1], _mediacodec_enc_input_buffer_used_cb, NULL);
1417 mediacodec_set_output_buffer_available_cb(app->mc_handle[1], _mediacodec_enc_output_buffer_available_cb, app);
1418 mediacodec_prepare(app->mc_handle[1]);
1420 /* create camera instance and setup and then start preview */
1421 camera_create(CAMERA_DEVICE_CAMERA0, &app->camera_handle);
1422 camera_set_media_packet_preview_cb(app->camera_handle, _media_packet_preview_cb, app);
1423 camera_get_preview_format(app->camera_handle, &default_format);
1424 camera_set_preview_format(app->camera_handle, default_format);
1425 camera_set_preview_resolution(app->camera_handle, app->width, app->height);
1426 camera_set_display(app->camera_handle, CAMERA_DISPLAY_TYPE_NONE, NULL);
1427 camera_start_preview(app->camera_handle);
1433 static void _mediacodec_camera_stop(App *app)
1435 camera_state_e camera_state = CAMERA_STATE_NONE;
1437 camera_get_state(app->camera_handle, &camera_state);
1438 camera_stop_preview(app->camera_handle);
1439 camera_destroy(app->camera_handle);
1441 mediacodec_unprepare(app->mc_handle[0]);
1442 mediacodec_unprepare(app->mc_handle[1]);
1443 mediacodec_destroy(app->mc_handle[0]);
1444 mediacodec_destroy(app->mc_handle[1]);
1448 static void _mediacodec_unprepare(App *app)
1450 mediacodec_unprepare(app->mc_handle[0]);
1453 static void _mediacodec_destroy(App *app)
1456 if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1458 g_print("media_packet_pool_deallocatet failed\n");
1462 if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1464 g_print(" media_packet_pool_destroy failed\n");
1467 g_print("media packet pool destroyed! \n");
1469 mediacodec_destroy(app->mc_handle[0]);
1472 static void input_filepath(char *filename, App *app)
1474 GError *error = NULL;
1477 app->file = g_mapped_file_new(filename, FALSE, &error);
1479 g_print("failed to open file : %s\n", error->message);
1480 g_error_free(error);
1484 app->length = g_mapped_file_get_length(app->file);
1485 app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1487 g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1492 void quit_program(App *app)
1494 media_format_unref(fmt);
1495 g_main_loop_quit(app->loop);
1500 void reset_menu_state()
1502 g_menu_state = CURRENT_STATUS_MAINMENU;
1506 void _interpret_main_menu(char *cmd, App *app)
1508 int len = strlen(cmd);
1510 if (strncmp(cmd, "a", 1) == 0)
1511 g_menu_state = CURRENT_STATUS_FILENAME;
1512 else if (strncmp(cmd, "o", 1) == 0)
1513 g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1514 else if (strncmp(cmd, "q", 1) == 0)
1517 g_print("unknown menu \n");
1518 } else if (len == 2) {
1519 if (strncmp(cmd, "pr", 2) == 0)
1520 _mediacodec_prepare(app, 0);
1521 else if (strncmp(cmd, "pa", 2) == 0)
1522 _mediacodec_prepare(app, 1);
1523 else if (strncmp(cmd, "sc", 2) == 0)
1524 g_menu_state = CURRENT_STATUS_SET_CODEC;
1525 else if (strncmp(cmd, "vd", 2) == 0)
1526 g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1527 else if (strncmp(cmd, "ve", 2) == 0)
1528 g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1529 else if (strncmp(cmd, "ad", 2) == 0)
1530 g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1531 else if (strncmp(cmd, "ae", 2) == 0)
1532 g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1533 else if (strncmp(cmd, "pi", 2) == 0)
1534 g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1535 else if (strncmp(cmd, "un", 2) == 0)
1536 _mediacodec_unprepare(app);
1537 else if (strncmp(cmd, "dt", 2) == 0)
1538 _mediacodec_destroy(app);
1539 else if (strncmp(cmd, "cr", 2) == 0)
1540 _mediacodec_camera_start(app);
1541 else if (strncmp(cmd, "ct", 2) == 0)
1542 _mediacodec_camera_stop(app);
1543 else if (strncmp(cmd, "dp", 2) == 0) {
1544 if (!app->enable_dump) {
1545 app->enable_dump = TRUE;
1546 g_print("dump enabled\n");
1548 app->enable_dump = FALSE;
1549 g_print("dump disabled\n");
1552 display_sub_basic();
1554 g_print("unknown menu \n");
1560 static void displaymenu(void)
1562 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1563 display_sub_basic();
1564 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1565 g_print("*** input mediapath.\n");
1566 } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1567 g_print("*** Codec id : Select Codec ID Numbe (e.g. AAC_LC = 96)\n");
1568 g_print(" L16 = 16 (0x10)\n");
1569 g_print(" ALAW = 32 (0x20)\n");
1570 g_print(" ULAW = 48 (0x30)\n");
1571 g_print(" AMR_NB = 64 (0x40)\n");
1572 g_print(" AMR_WB = 65 (0x41)\n");
1573 g_print(" G729 = 80 (0x50)\n");
1574 g_print(" AAC_LC = 96 (0x60)\n");
1575 g_print(" AAC_HE = 97 (0x61)\n");
1576 g_print(" AAC_PS = 98 (0x62)\n");
1577 g_print(" MP3 = 112 (0x70)\n");
1578 g_print(" VORBIS = 128 (0x80)\n");
1579 g_print(" FLAC = 144 (0x90)\n");
1580 g_print(" WMAV1 = 160 (0xA0)\n");
1581 g_print(" WMAV2 = 161 (0xA1)\n");
1582 g_print(" WMAPRO = 162 (0xA2)\n");
1583 g_print(" WMALSL = 163 (0xA3)\n");
1584 g_print(" -------------------\n");
1585 g_print(" H261 = 101\n");
1586 g_print(" H263 = 102\n");
1587 g_print(" H264 = 103\n");
1588 g_print(" MJPEG = 104\n");
1589 g_print(" MPEG1 = 105\n");
1590 g_print(" MPEG2 = 106\n");
1591 g_print(" MPEG4 = 107\n");
1592 g_print(" -------------------\n");
1593 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1594 g_print(" CODEC : ENCODER = 1 DECODER = 2\n");
1595 g_print(" TYPE : HW = 4 SW = 8\n");
1596 g_print("*** input codec id, falgs.\n");
1597 } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1598 g_print("*** input video decode configure.(width, height)\n");
1599 } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1600 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1601 } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1602 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000, 2, 16))\n");
1603 } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1604 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000, 2, 16, 128000))\n");
1605 } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1606 g_print("*** input dec process number\n");
1607 } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1608 g_print("*** input get output buffer number\n");
1610 g_print("*** unknown status.\n");
1615 gboolean timeout_menu_display(void* data)
1622 static void interpret(char *cmd, App *app)
1624 switch (g_menu_state) {
1625 case CURRENT_STATUS_MAINMENU:
1626 _interpret_main_menu(cmd, app);
1628 case CURRENT_STATUS_FILENAME:
1629 input_filepath(cmd, app);
1632 case CURRENT_STATUS_SET_CODEC:
1645 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1646 tmp = strtol(cmd, ptr, 16);
1647 app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1649 app->codecid = 0x1000 + tmp;
1654 app->flag = atoi(cmd);
1663 case CURRENT_STATUS_SET_VDEC_INFO:
1668 app->width = atoi(cmd);
1672 app->height = atoi(cmd);
1673 app->type = VIDEO_DEC;
1683 case CURRENT_STATUS_SET_VENC_INFO:
1688 app->width = atoi(cmd);
1692 app->height = atoi(cmd);
1696 app->fps = atol(cmd);
1700 app->target_bits = atoi(cmd);
1701 app->type = VIDEO_ENC;
1711 case CURRENT_STATUS_SET_ADEC_INFO:
1716 app->samplerate = atoi(cmd);
1720 app->channel = atoi(cmd);
1724 app->bit = atoi(cmd);
1725 app->type = AUDIO_DEC;
1735 case CURRENT_STATUS_SET_AENC_INFO:
1740 app->samplerate = atoi(cmd);
1744 app->channel = atoi(cmd);
1748 app->bit = atoi(cmd);
1752 app->bitrate = atoi(cmd);
1753 app->type = AUDIO_ENC;
1763 case CURRENT_STATUS_PROCESS_INPUT:
1765 app->frame = atoi(cmd);
1767 if (app->frame > 0 && app->frame < 10)
1768 _mediacodec_process_input(app);
1772 case CURRENT_STATUS_GET_OUTPUT:
1781 g_timeout_add(100, timeout_menu_display, 0);
1784 static void display_sub_basic()
1787 g_print("=========================================================================================\n");
1788 g_print(" media codec test\n");
1789 g_print("-----------------------------------------------------------------------------------------\n");
1790 g_print("a. Create \t\t");
1791 g_print("sc. Set codec \n");
1792 g_print("vd. Set vdec info \t");
1793 g_print("ve. Set venc info \n");
1794 g_print("ad. Set adec info \t");
1795 g_print("ae. Set aenc info \n");
1796 g_print("pr. Prepare \t");
1797 g_print("pa. Prepare and process all\t\t");
1798 g_print("pi. process input with num\n");
1799 g_print("o. Get output \t\t");
1800 g_print("rb. Reset output buffer \n");
1801 g_print("un. Unprepare \t\t");
1802 g_print("dt. Destroy \t\t");
1803 g_print("q. quit test suite \n");
1804 g_print("dp. enable dump \n");
1805 g_print("-----------------------------------------------------------------------------------------\n");
1806 g_print("cr. camera preview -> encoder -> decoder\n");
1807 g_print("ct. quit camera test\n");
1809 g_print("=========================================================================================\n");
1812 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1814 gchar buf[MAX_STRING_LEN];
1816 GError *error = NULL;
1817 App *context = (App*)data;
1819 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1822 interpret(buf, context);
1827 int main(int argc, char *argv[])
1831 GIOChannel *stdin_channel;
1832 stdin_channel = g_io_channel_unix_new(0);
1833 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1834 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1838 app->loop = g_main_loop_new(NULL, TRUE);
1839 app->timer = g_timer_new();
1840 g_main_loop_run(app->loop);
1846 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1851 void mc_hex_dump(char *desc, void *addr, int len)
1854 unsigned char buff[17];
1855 unsigned char *pc = (unsigned char *)addr;
1858 g_print("%s:\n", desc);
1860 for (i = 0; i < len; i++) {
1862 if ((i % 16) == 0) {
1864 g_print(" %s\n", buff);
1866 g_print(" %04x ", i);
1869 g_print(" %02x", pc[i]);
1871 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1874 buff[i % 16] = pc[i];
1875 buff[(i % 16) + 1] = '\0';
1878 while ((i % 16) != 0) {
1882 g_print(" %s\n", buff);
1885 static void decoder_output_dump(App *app, media_packet_h pkt)
1889 int stride_width, stride_height;
1890 gchar filename[100] = {0};
1894 g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1895 fp = fopen(filename, "ab");
1897 media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1898 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1899 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1900 g_print("stride : %d, %d\n", stride_width, stride_height);
1902 for (i = 0; i < app->height; i++) {
1903 ret = fwrite(temp, app->width, 1, fp);
1904 temp += stride_width;
1907 if (app->hardware == TRUE) {
1908 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1909 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1910 for (i = 0; i < app->height/2; i++) {
1911 ret = fwrite(temp, app->width, 1, fp);
1912 temp += stride_width;
1915 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1916 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1917 for (i = 0; i < app->height/2; i++) {
1918 ret = fwrite(temp, app->width/2, 1, fp);
1919 temp += stride_width;
1922 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1923 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1924 for (i = 0; i < app->height/2; i++) {
1925 ret = fwrite(temp, app->width/2, 1, fp);
1926 temp += stride_width;
1930 g_print("codec dec output dumped!!%d\n", ret);
1936 * Add ADTS header at the beginning of each and every AAC packet.
1937 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
1938 * Note the packetLen must count in the ADTS header itself.
1940 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1942 int profile = 2; /* AAC LC (0x01) */
1943 int freqIdx = 3; /* 48KHz (0x03) */
1944 int chanCfg = 2; /* CPE (0x02) */
1946 if (app->samplerate == 96000) freqIdx = 0;
1947 else if (app->samplerate == 88200) freqIdx = 1;
1948 else if (app->samplerate == 64000) freqIdx = 2;
1949 else if (app->samplerate == 48000) freqIdx = 3;
1950 else if (app->samplerate == 44100) freqIdx = 4;
1951 else if (app->samplerate == 32000) freqIdx = 5;
1952 else if (app->samplerate == 24000) freqIdx = 6;
1953 else if (app->samplerate == 22050) freqIdx = 7;
1954 else if (app->samplerate == 16000) freqIdx = 8;
1955 else if (app->samplerate == 12000) freqIdx = 9;
1956 else if (app->samplerate == 11025) freqIdx = 10;
1957 else if (app->samplerate == 8000) freqIdx = 11;
1959 if ((app->channel == 1) || (app->channel == 2))
1960 chanCfg = app->channel;
1962 /* fill in ADTS data */
1963 buffer[0] = (char)0xFF;
1964 buffer[1] = (char)0xF1;
1965 buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1966 buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1967 buffer[4] = (char)((packetLen&0x7FF) >> 3);
1968 buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1969 buffer[6] = (char)0xFC;
1972 static void output_dump(App *app, media_packet_h pkt)
1976 gchar filename[100] = {0};
1979 char adts[100] = {0, };
1981 g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
1982 fp = fopen(filename, "ab");
1984 media_packet_get_buffer_data_ptr(pkt, &temp);
1985 media_packet_get_buffer_size(pkt, &buf_size);
1986 g_print("output data : %p, size %d\n", temp, (int)buf_size);
1988 if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1989 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1990 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1991 g_print("adts appended\n");
1992 } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1) {
1993 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1994 g_print("%s - AMR_header write in first frame\n", __func__);
1995 fwrite(&AMR_header[0], 1, sizeof(AMR_header) - 1, fp); /* AMR-NB magic number */
1996 write_amr_header = 0;
1999 fwrite(temp, (int)buf_size, 1, fp);
2001 g_print("codec dec output dumped!!%d\n", ret);
2006 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
2008 guint media_codec_id_u = (guint)media_codec_id;
2010 switch (media_codec_id_u) {
2011 case MEDIACODEC_L16:
2013 case MEDIACODEC_ALAW:
2015 case MEDIACODEC_ULAW:
2017 case MEDIACODEC_AMR_NB:
2019 case MEDIACODEC_AMR_WB:
2021 case MEDIACODEC_G729:
2023 case MEDIACODEC_AAC_LC:
2025 case MEDIACODEC_AAC_HE:
2027 case MEDIACODEC_AAC_HE_PS:
2029 case MEDIACODEC_MP3:
2031 case MEDIACODEC_VORBIS:
2033 case MEDIACODEC_FLAC:
2035 case MEDIACODEC_WMAV1:
2037 case MEDIACODEC_WMAV2:
2039 case MEDIACODEC_WMAPRO:
2041 case MEDIACODEC_WMALSL:
2043 case MEDIACODEC_H261:
2045 case MEDIACODEC_H263:
2047 case MEDIACODEC_H264:
2049 case MEDIACODEC_MJPEG:
2051 case MEDIACODEC_MPEG1:
2053 case MEDIACODEC_MPEG2:
2055 case MEDIACODEC_MPEG4:
2057 case MEDIACODEC_HEVC:
2059 case MEDIACODEC_VP8:
2061 case MEDIACODEC_VP9:
2063 case MEDIACODEC_VC1: