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) {
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 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");
613 /* if it's not a valid sync */
614 if ((header & 0xffe00000) != 0xffe00000) {
615 g_print ("[ERROR] invalid sync\n");
619 if (((header >> 19) & 3) == 0x1) {
620 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
623 if (header & (1 << 20)) {
624 lsf = (header & (1 << 19)) ? 0 : 1;
632 /* if it's an invalid layer */
633 if (!((header >> 17) & 3)) {
634 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
637 layer = 4 - ((header >> 17) & 0x3);
640 /* if it's an invalid bitrate */
641 if (((header >> 12) & 0xf) == 0xf) {
642 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
645 bitrate = (header >> 12) & 0xF;
646 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
647 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
648 if (hdr_bitrate == 0)
652 /* if it's an invalid samplerate */
653 if (((header >> 10) & 0x3) == 0x3) {
654 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
658 sf = (header >> 10) & 0x3;
659 sf = mp3types_freqs[lsf + mpg25][sf];
662 padding = (header >> 9) & 0x1;
666 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
669 read_size = (hdr_bitrate * 144) / sf + padding;
673 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
676 g_print("header : %d, read : %d\n", header, read_size);
679 *data = app->data + app->offset;
681 if (read_size >= offset)
686 app->offset += *size;
690 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
692 int readsize = 0, read_size = 0;
693 unsigned int header_size = ADTS_HEADER_SIZE;
694 unsigned char buffer[100000];
695 unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
696 int offset = app->length - app->offset;
697 unsigned char *pData = app->data + app->offset;
699 * It is not support full parsing MP4 container box.
700 * So It MUST start as RAW valid frame sequence.
701 * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
704 /* change the below one later */
705 if (app->offset == 0) {
707 * CAUTION : Codec data is needed only once in first time
708 * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
709 * So I use this data (byte) as hard coding for temporary our testing.
713 * The codec_data data is according to AudioSpecificConfig,
714 * ISO/IEC 14496-3, 1.6.2.1
716 * below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
717 * case : M4A - LC profile
718 * codec_data=(buffer)119056e5000000000000000000000000
719 * savs aac decoder get codec_data. size: 16 (Tag size : 5 byte)
720 * - codec data: profile : 2
721 * - codec data: samplrate: 48000
722 * - codec data: channels : 2
724 /* 2 bytes are mandatory */
725 codecdata[0] = 0x11; /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
726 codecdata[1] = 0x90; /* ex) (4bit) 2 (2ch) */
727 /* othter bytes are (optional) epconfig information */
733 * below example is test for using "TestSample-EAAC+.m4a"
735 * case : M4A - HE-AAC v1 and v2 profile
736 * codec_data=(buffer)138856e5a54880000000000000000000
737 * savs aac decoder get codec_data. size: 16 (Tag size : 7 byte)
738 * - codec data: profile : 2
739 * - codec data: samplrate: 22050
740 * - codec data: channels : 1
742 /* 2 bytes are mandatory */
743 codecdata[0] = 0x13; /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
744 codecdata[1] = 0x88; /* ex) (4bit) 1 (1ch) */
745 /* othter bytes are (optional) epconfig information */
753 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
754 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
755 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
758 g_print("[FAIL] Not found aac frame sync.....\n");
760 readsize = read_size - header_size;
761 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
762 read_size = readsize + AAC_CODECDATA_SIZE; /* return combination of (codec_data + raw_data) */
763 app->offset += header_size + readsize;
767 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
768 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
769 readsize = read_size - header_size;
770 memcpy(buffer, pData + 7, readsize); /* Make only RAW data, so exclude header 7 bytes */
771 read_size = readsize;
772 app->offset += header_size + readsize;
776 g_print("[FAIL] Not found aac frame sync. \n");
781 if (read_size >= offset)
789 * Extract Input data for AAC encoder
792 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
795 int offset = app->length - app->offset;
797 read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
799 if (read_size >= offset)
803 *data = app->data + app->offset;
805 if (read_size >= offset)
810 app->offset += *size;
814 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
817 g_print("Used input buffer = %p\n", pkt);
818 media_packet_destroy(pkt);
823 int _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
826 media_format_mimetype_e mime = 0;
827 encoder = GET_IS_ENCODER(flag) ? 1 : 0;
828 *hardware = GET_IS_HW(flag) ? 1 : 0;
829 app->is_encoder = encoder;
832 case MEDIACODEC_H264:
834 extractor = yuv_extractor;
835 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
837 extractor = h264_extractor;
838 mime = MEDIA_FORMAT_H264_SP;
841 case MEDIACODEC_MPEG4:
843 extractor = yuv_extractor;
844 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
846 extractor = mpeg4_extractor;
847 mime = MEDIA_FORMAT_MPEG4_SP;
850 case MEDIACODEC_H263:
852 extractor = yuv_extractor;
853 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
855 extractor = h263_extractor;
856 mime = MEDIA_FORMAT_H263P;
861 extractor = aacenc_extractor;
862 mime = MEDIA_FORMAT_PCM;
864 extractor = aacdec_extractor;
865 mime = MEDIA_FORMAT_AAC;
868 case MEDIACODEC_AAC_HE:
870 extractor = aacenc_extractor;
871 mime = MEDIA_FORMAT_PCM;
873 extractor = extract_input_aacdec_m4a_test;
874 mime = MEDIA_FORMAT_AAC_HE;
877 case MEDIACODEC_AAC_HE_PS:
880 extractor = mp3dec_extractor;
881 mime = MEDIA_FORMAT_MP3;
883 case MEDIACODEC_VORBIS:
885 case MEDIACODEC_FLAC:
887 case MEDIACODEC_WMAV1:
889 case MEDIACODEC_WMAV2:
891 case MEDIACODEC_WMAPRO:
893 case MEDIACODEC_WMALSL:
895 case MEDIACODEC_AMR_NB:
897 extractor = amrenc_extractor;
898 mime = MEDIA_FORMAT_PCM;
899 app->is_amr_nb = TRUE;
901 extractor = amrdec_extractor;
902 mime = MEDIA_FORMAT_AMR_NB;
905 case MEDIACODEC_AMR_WB:
907 extractor = amrenc_extractor;
908 mime = MEDIA_FORMAT_PCM;
909 app->is_amr_nb = FALSE;
911 extractor = amrdec_extractor;
912 mime = MEDIA_FORMAT_AMR_WB;
916 LOGE("NOT SUPPORTED!!!!");
922 static void _mediacodec_process_input(App *app)
925 bool have_frame = FALSE;
927 static guint64 pts = 0L;
928 void *buf_data_ptr = NULL;
929 media_packet_h pkt = NULL;
936 for (i = 0; i < app->frame; i++) {
937 g_print("----------read data------------\n");
939 extractor(app, &tmp, &read, &have_frame);
943 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
944 g_print("media_packet_pool_aquire_packet failed\n");
948 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
949 g_print("media_packet_create_alloc failed\n");
954 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
955 g_print("media_packet_set_pts failed\n");
959 if (app->type != VIDEO_ENC) {
960 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
961 media_packet_set_buffer_size(pkt, (uint64_t)read);
963 memcpy(buf_data_ptr, tmp, read);
964 g_print("tmp:%p, read:%d\n", tmp, read);
967 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
968 media_packet_get_video_stride_width(pkt, 0, &stride_width);
969 offset = app->width*app->height;
971 for (i = 0; i < app->height; i++) {
972 memcpy(buf_data_ptr, tmp, app->width);
973 buf_data_ptr += stride_width;
977 if (app->hardware == TRUE) {
978 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
979 media_packet_get_video_stride_width(pkt, 1, &stride_width);
980 size = app->width * app->height / 2;
982 for (i = 0; i< app->height / 2; i++) {
983 memcpy(buf_data_ptr, tmp, app->width);
984 buf_data_ptr += stride_width;
989 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
990 media_packet_get_video_stride_width(pkt, 1, &stride_width);
991 size = (app->width>>1) * (app->height>>1);
993 for (i = 0; i < app->height/2; i++) {
994 memcpy(buf_data_ptr, tmp, app->width/2);
995 buf_data_ptr += stride_width;
1000 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1001 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1004 for (i = 0; i < app->height/2; i++) {
1005 memcpy(buf_data_ptr, tmp, app->width/2);
1006 buf_data_ptr += stride_width;
1007 tmp += app->width/2;
1012 mc_hex_dump("inbuf", tmp, 48);
1014 ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
1015 if (ret != MEDIACODEC_ERROR_NONE)
1018 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1023 static gboolean read_data(App *app)
1026 bool have_frame = FALSE;
1028 static guint64 pts = 0L;
1029 void *buf_data_ptr = NULL;
1030 media_packet_h pkt = NULL;
1038 if (app->offset == 0) {
1039 app->frame_count = 0;
1040 app->start = clock();
1043 g_print("----------read data------------\n");
1044 extractor(app, &tmp, &read, &have_frame);
1046 if (app->offset >= app->length - 4) {
1049 app->finish = clock();
1050 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1051 g_print("---------------------------\n");
1054 g_print("length : %d, offset : %d\n", app->length, app->offset);
1056 if (app->offset + len > app->length)
1057 len = app->length - app->offset;
1059 g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1063 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1064 g_print("media_packet_pool_aquire_packet failed\n");
1068 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1069 g_print("media_packet_create_alloc failed\n");
1073 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1074 g_print("media_packet_set_pts failed\n");
1079 if (app->type != VIDEO_ENC) {
1080 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1081 media_packet_set_buffer_size(pkt, (uint64_t)read);
1083 memcpy(buf_data_ptr, tmp, read);
1084 g_print("tmp:%p, read:%d\n", tmp, read);
1087 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1088 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1089 offset = app->width*app->height;
1091 for (i = 0; i < app->height; i++) {
1092 memcpy(buf_data_ptr, tmp, app->width);
1093 buf_data_ptr += stride_width;
1097 if (app->hardware == TRUE) {
1098 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1099 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1100 size = app->width * app->height>>1;
1102 for (i = 0; i < app->height>>1; i++) {
1103 memcpy(buf_data_ptr, tmp, app->width);
1104 buf_data_ptr += stride_width;
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>>1) * (app->height>>1);
1114 for (i = 0; i < app->height/2; i++) {
1115 memcpy(buf_data_ptr, tmp, app->width>>1);
1116 buf_data_ptr += stride_width;
1117 tmp += (app->width>>1);
1121 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1122 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1125 for (i = 0; i < app->height/2; i++) {
1126 memcpy(buf_data_ptr, tmp, app->width>>1);
1127 buf_data_ptr += stride_width;
1128 tmp += (app->width>>1);
1133 mc_hex_dump("inbuf", tmp, 48);
1135 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1136 if (ret != MEDIACODEC_ERROR_NONE)
1139 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1145 static void start_feed(App *app)
1147 if (app->sourceid == 0) {
1148 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1149 g_print("start_feed\n");
1153 static void stop_feed(App *app)
1155 if (app->sourceid != 0) {
1156 g_source_remove(app->sourceid);
1158 g_print("stop_feed\n");
1162 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1164 g_print("_mediacodec_inbuf_used_cb!!!\n");
1166 media_packet_pool_release_packet(pkt_pool, pkt);
1168 media_packet_destroy(pkt);
1174 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1176 media_packet_h out_pkt = NULL;
1179 App *app = (App*)user_data;
1181 g_print("_mediacodec_outbuf_available_cb\n");
1183 g_mutex_lock(&app->lock);
1185 ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1187 if (ret != MEDIACODEC_ERROR_NONE)
1188 g_print("get_output failed\n");
1190 if (app->enable_dump) {
1191 if (app->type == VIDEO_DEC)
1192 decoder_output_dump(app, out_pkt);
1194 output_dump(app, out_pkt);
1200 g_mutex_unlock(&app->lock);
1202 media_packet_destroy(out_pkt);
1209 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1211 g_print("_mediacodec_buffer_status_cb %d\n", status);
1213 App *app = (App*)user_data;
1215 if (status == MEDIACODEC_NEED_DATA)
1217 else if (status == MEDIACODEC_ENOUGH_DATA)
1223 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1228 static bool _mediacodec_eos_cb(void *user_data)
1233 void _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
1235 g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
1239 static void _mediacodec_prepare(App *app, bool frame_all)
1243 g_print("supported codec lists -internal-\n");
1244 mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
1246 /* create instance */
1247 ret = mediacodec_create(&app->mc_handle[0]);
1248 if (ret != MEDIACODEC_ERROR_NONE) {
1249 g_print("mediacodec_create failed\n");
1254 ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1255 if (ret != MEDIACODEC_ERROR_NONE) {
1256 g_print("mediacodec_set_codec failed\n");
1260 app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1262 /* set codec info */
1263 ret = media_format_create(&fmt);
1265 switch (app->type) {
1267 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1268 media_format_set_video_mime(fmt, app->mime);
1269 media_format_set_video_width(fmt, app->width);
1270 media_format_set_video_height(fmt, app->height);
1273 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1274 media_format_set_video_mime(fmt, app->mime);
1275 media_format_set_video_width(fmt, app->width);
1276 media_format_set_video_height(fmt, app->height);
1277 media_format_set_video_avg_bps(fmt, app->target_bits);
1280 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1281 media_format_set_audio_mime(fmt, app->mime);
1282 media_format_set_audio_channel(fmt, app->channel);
1283 media_format_set_audio_samplerate(fmt, app->samplerate);
1284 media_format_set_audio_bit(fmt, app->bit);
1287 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1288 media_format_set_audio_mime(fmt, app->mime);
1289 media_format_set_audio_channel(fmt, app->channel);
1290 media_format_set_audio_samplerate(fmt, app->samplerate);
1291 media_format_set_audio_bit(fmt, app->bit);
1294 g_print("invaild type\n");
1298 if (ret != MEDIACODEC_ERROR_NONE) {
1299 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1304 g_print("supported codec lists\n");
1305 mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
1306 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1307 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1309 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1310 mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1311 mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1314 ret = mediacodec_prepare(app->mc_handle[0]);
1315 if (ret != MEDIACODEC_ERROR_NONE) {
1316 g_print("mediacodec_prepare failed\n");
1321 /* get packet pool instance */
1322 ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1323 if (ret != MEDIA_PACKET_ERROR_NONE) {
1324 g_print("mediacodec_get_packet_pool failed\n");
1327 g_print("\n\nmediacodec start\n\n");
1332 static void _mediacodec_enc_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1334 /* release input raw packet */
1335 media_packet_destroy(pkt);
1338 /* this callback is called when the input buffer for codec has done to use */
1339 static void _mediacodec_dec_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1341 /* release input encoded packet */
1342 media_packet_destroy(pkt);
1345 static void _mediacodec_enc_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1347 App *app = (App*)user_data;
1349 mediacodec_h media_codec_handle = app->mc_handle[1];
1350 media_packet_h output_buf = NULL;
1351 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1352 /* decode encoded camera preview */
1353 mediacodec_process_input(app->mc_handle[0], output_buf, 0);
1356 static void _mediacodec_dec_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1358 App *app = (App*)user_data;
1360 mediacodec_h media_codec_handle = app->mc_handle[0];
1361 media_packet_h output_buf = NULL;
1363 mediacodec_get_output(media_codec_handle, &output_buf, 0);
1365 if (app->enable_dump)
1366 decoder_output_dump(app, output_buf);
1368 media_packet_destroy(output_buf);
1371 static void _media_packet_preview_cb(media_packet_h packet, void *user_data)
1373 App *app = user_data;
1374 g_mutex_lock(&app->lock);
1375 mediacodec_process_input(app->mc_handle[1], packet, 0);
1376 g_mutex_unlock(&app->lock);
1381 static void _mediacodec_camera_start(App *app)
1383 int default_format = CAMERA_PIXEL_FORMAT_NV12;
1386 app->hardware = TRUE;
1388 /*create decoder instance and setup */
1389 mediacodec_create(&app->mc_handle[0]);
1390 mediacodec_set_codec(app->mc_handle[0], MEDIACODEC_H264, MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW);
1391 mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1393 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], _mediacodec_dec_input_buffer_used_cb, NULL);
1394 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], _mediacodec_dec_output_buffer_available_cb, app);
1395 mediacodec_prepare(app->mc_handle[0]);
1397 /*create encoder instance and setup */
1398 mediacodec_create(&app->mc_handle[1]);
1399 mediacodec_set_codec(app->mc_handle[1], MEDIACODEC_H264, MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW);
1400 mediacodec_set_venc_info(app->mc_handle[1], app->width, app->height, 30, 1000);
1402 mediacodec_set_input_buffer_used_cb(app->mc_handle[1], _mediacodec_enc_input_buffer_used_cb, NULL);
1403 mediacodec_set_output_buffer_available_cb(app->mc_handle[1], _mediacodec_enc_output_buffer_available_cb, app);
1404 mediacodec_prepare(app->mc_handle[1]);
1406 /* create camera instance and setup and then start preview */
1407 camera_create(CAMERA_DEVICE_CAMERA0, &app->camera_handle);
1408 camera_set_media_packet_preview_cb(app->camera_handle, _media_packet_preview_cb, app);
1409 camera_get_preview_format(app->camera_handle, &default_format);
1410 camera_set_preview_format(app->camera_handle, default_format);
1411 camera_set_preview_resolution(app->camera_handle, app->width, app->height);
1412 camera_set_display(app->camera_handle, CAMERA_DISPLAY_TYPE_NONE, NULL);
1413 camera_start_preview(app->camera_handle);
1419 static void _mediacodec_camera_stop(App *app)
1421 camera_state_e camera_state = CAMERA_STATE_NONE;
1423 camera_get_state(app->camera_handle, &camera_state);
1424 camera_stop_preview(app->camera_handle);
1425 camera_destroy(app->camera_handle);
1427 mediacodec_unprepare(app->mc_handle[0]);
1428 mediacodec_unprepare(app->mc_handle[1]);
1429 mediacodec_destroy(app->mc_handle[0]);
1430 mediacodec_destroy(app->mc_handle[1]);
1434 static void _mediacodec_unprepare(App *app)
1436 mediacodec_unprepare(app->mc_handle[0]);
1439 static void _mediacodec_destroy(App *app)
1442 if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1444 g_print("media_packet_pool_deallocatet failed\n");
1448 if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1450 g_print(" media_packet_pool_destroy failed\n");
1453 g_print("media packet pool destroyed! \n");
1455 mediacodec_destroy(app->mc_handle[0]);
1458 static void input_filepath(char *filename, App *app)
1460 GError *error = NULL;
1463 app->file = g_mapped_file_new(filename, FALSE, &error);
1465 g_print("failed to open file : %s\n", error->message);
1466 g_error_free(error);
1470 app->length = g_mapped_file_get_length(app->file);
1471 app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1473 g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1478 void quit_program(App *app)
1480 media_format_unref(fmt);
1481 g_main_loop_quit(app->loop);
1486 void reset_menu_state()
1488 g_menu_state = CURRENT_STATUS_MAINMENU;
1492 void _interpret_main_menu(char *cmd, App *app)
1494 int len = strlen(cmd);
1496 if (strncmp(cmd, "a", 1) == 0)
1497 g_menu_state = CURRENT_STATUS_FILENAME;
1498 else if (strncmp(cmd, "o", 1) == 0)
1499 g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1500 else if (strncmp(cmd, "q", 1) == 0)
1503 g_print("unknown menu \n");
1504 } else if (len == 2) {
1505 if (strncmp(cmd, "pr", 2) == 0)
1506 _mediacodec_prepare(app, 0);
1507 else if (strncmp(cmd, "pa", 2) == 0)
1508 _mediacodec_prepare(app, 1);
1509 else if (strncmp(cmd, "sc", 2) == 0)
1510 g_menu_state = CURRENT_STATUS_SET_CODEC;
1511 else if (strncmp(cmd, "vd", 2) == 0)
1512 g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1513 else if (strncmp(cmd, "ve", 2) == 0)
1514 g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1515 else if (strncmp(cmd, "ad", 2) == 0)
1516 g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1517 else if (strncmp(cmd, "ae", 2) == 0)
1518 g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1519 else if (strncmp(cmd, "pi", 2) == 0)
1520 g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1521 else if (strncmp(cmd, "un", 2) == 0)
1522 _mediacodec_unprepare(app);
1523 else if (strncmp(cmd, "dt", 2) == 0)
1524 _mediacodec_destroy(app);
1525 else if (strncmp(cmd, "cr", 2) == 0)
1526 _mediacodec_camera_start(app);
1527 else if (strncmp(cmd, "ct", 2) == 0)
1528 _mediacodec_camera_stop(app);
1529 else if (strncmp(cmd, "dp", 2) == 0) {
1530 if (!app->enable_dump) {
1531 app->enable_dump = TRUE;
1532 g_print("dump enabled\n");
1534 app->enable_dump = FALSE;
1535 g_print("dump disabled\n");
1538 display_sub_basic();
1540 g_print("unknown menu \n");
1546 static void displaymenu(void)
1548 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1549 display_sub_basic();
1550 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1551 g_print("*** input mediapath.\n");
1552 } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1553 g_print("*** Codec id : Select Codec ID Numbe (e.g. AAC_LC = 96)\n");
1554 g_print(" L16 = 16 (0x10)\n");
1555 g_print(" ALAW = 32 (0x20)\n");
1556 g_print(" ULAW = 48 (0x30)\n");
1557 g_print(" AMR_NB = 64 (0x40)\n");
1558 g_print(" AMR_WB = 65 (0x41)\n");
1559 g_print(" G729 = 80 (0x50)\n");
1560 g_print(" AAC_LC = 96 (0x60)\n");
1561 g_print(" AAC_HE = 97 (0x61)\n");
1562 g_print(" AAC_PS = 98 (0x62)\n");
1563 g_print(" MP3 = 112 (0x70)\n");
1564 g_print(" VORBIS = 128 (0x80)\n");
1565 g_print(" FLAC = 144 (0x90)\n");
1566 g_print(" WMAV1 = 160 (0xA0)\n");
1567 g_print(" WMAV2 = 161 (0xA1)\n");
1568 g_print(" WMAPRO = 162 (0xA2)\n");
1569 g_print(" WMALSL = 163 (0xA3)\n");
1570 g_print(" -------------------\n");
1571 g_print(" H261 = 101\n");
1572 g_print(" H263 = 102\n");
1573 g_print(" H264 = 103\n");
1574 g_print(" MJPEG = 104\n");
1575 g_print(" MPEG1 = 105\n");
1576 g_print(" MPEG2 = 106\n");
1577 g_print(" MPEG4 = 107\n");
1578 g_print(" -------------------\n");
1579 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1580 g_print(" CODEC : ENCODER = 1 DECODER = 2\n");
1581 g_print(" TYPE : HW = 4 SW = 8\n");
1582 g_print("*** input codec id, falgs.\n");
1583 } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1584 g_print("*** input video decode configure.(width, height)\n");
1585 } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1586 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1587 } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1588 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000, 2, 16))\n");
1589 } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1590 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000, 2, 16, 128000))\n");
1591 } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1592 g_print("*** input dec process number\n");
1593 } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1594 g_print("*** input get output buffer number\n");
1596 g_print("*** unknown status.\n");
1601 gboolean timeout_menu_display(void* data)
1608 static void interpret(char *cmd, App *app)
1610 switch (g_menu_state) {
1611 case CURRENT_STATUS_MAINMENU:
1612 _interpret_main_menu(cmd, app);
1614 case CURRENT_STATUS_FILENAME:
1615 input_filepath(cmd, app);
1618 case CURRENT_STATUS_SET_CODEC:
1631 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1632 tmp = strtol(cmd, ptr, 16);
1633 app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1635 app->codecid = 0x1000 + tmp;
1640 app->flag = atoi(cmd);
1649 case CURRENT_STATUS_SET_VDEC_INFO:
1654 app->width = atoi(cmd);
1658 app->height = atoi(cmd);
1659 app->type = VIDEO_DEC;
1669 case CURRENT_STATUS_SET_VENC_INFO:
1674 app->width = atoi(cmd);
1678 app->height = atoi(cmd);
1682 app->fps = atol(cmd);
1686 app->target_bits = atoi(cmd);
1687 app->type = VIDEO_ENC;
1697 case CURRENT_STATUS_SET_ADEC_INFO:
1702 app->samplerate = atoi(cmd);
1706 app->channel = atoi(cmd);
1710 app->bit = atoi(cmd);
1711 app->type = AUDIO_DEC;
1721 case CURRENT_STATUS_SET_AENC_INFO:
1726 app->samplerate = atoi(cmd);
1730 app->channel = atoi(cmd);
1734 app->bit = atoi(cmd);
1738 app->bitrate = atoi(cmd);
1739 app->type = AUDIO_ENC;
1749 case CURRENT_STATUS_PROCESS_INPUT:
1751 app->frame = atoi(cmd);
1752 _mediacodec_process_input(app);
1756 case CURRENT_STATUS_GET_OUTPUT:
1765 g_timeout_add(100, timeout_menu_display, 0);
1768 static void display_sub_basic()
1771 g_print("=========================================================================================\n");
1772 g_print(" media codec test\n");
1773 g_print("-----------------------------------------------------------------------------------------\n");
1774 g_print("a. Create \t\t");
1775 g_print("sc. Set codec \n");
1776 g_print("vd. Set vdec info \t");
1777 g_print("ve. Set venc info \n");
1778 g_print("ad. Set adec info \t");
1779 g_print("ae. Set aenc info \n");
1780 g_print("pr. Prepare \t");
1781 g_print("pa. Prepare and process all\t\t");
1782 g_print("pi. process input with num\n");
1783 g_print("o. Get output \t\t");
1784 g_print("rb. Reset output buffer \n");
1785 g_print("un. Unprepare \t\t");
1786 g_print("dt. Destroy \t\t");
1787 g_print("q. quit test suite \n");
1788 g_print("dp. enable dump \n");
1789 g_print("-----------------------------------------------------------------------------------------\n");
1790 g_print("cr. camera preview -> encoder -> decoder\n");
1791 g_print("ct. quit camera test\n");
1793 g_print("=========================================================================================\n");
1796 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1798 gchar buf[MAX_STRING_LEN];
1800 GError *error = NULL;
1801 App *context = (App*)data;
1803 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1806 interpret(buf, context);
1811 int main(int argc, char *argv[])
1815 GIOChannel *stdin_channel;
1816 stdin_channel = g_io_channel_unix_new(0);
1817 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1818 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1822 app->loop = g_main_loop_new(NULL, TRUE);
1823 app->timer = g_timer_new();
1824 g_main_loop_run(app->loop);
1830 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1835 void mc_hex_dump(char *desc, void *addr, int len)
1838 unsigned char buff[17];
1839 unsigned char *pc = (unsigned char *)addr;
1842 g_print("%s:\n", desc);
1844 for (i = 0; i < len; i++) {
1846 if ((i % 16) == 0) {
1848 g_print(" %s\n", buff);
1850 g_print(" %04x ", i);
1853 g_print(" %02x", pc[i]);
1855 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1858 buff[i % 16] = pc[i];
1859 buff[(i % 16) + 1] = '\0';
1862 while ((i % 16) != 0) {
1866 g_print(" %s\n", buff);
1869 static void decoder_output_dump(App *app, media_packet_h pkt)
1873 int stride_width, stride_height;
1874 char filename[100] = {0};
1878 sprintf(filename, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1879 fp = fopen(filename, "ab");
1881 media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1882 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1883 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1884 g_print("stride : %d, %d\n", stride_width, stride_height);
1886 for (i = 0; i < app->height; i++) {
1887 ret = fwrite(temp, app->width, 1, fp);
1888 temp += stride_width;
1891 if (app->hardware == TRUE) {
1892 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1893 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1894 for (i = 0; i < app->height/2; i++) {
1895 ret = fwrite(temp, app->width, 1, fp);
1896 temp += stride_width;
1899 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1900 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1901 for (i = 0; i < app->height/2; i++) {
1902 ret = fwrite(temp, app->width/2, 1, fp);
1903 temp += stride_width;
1906 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1907 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1908 for (i = 0; i < app->height/2; i++) {
1909 ret = fwrite(temp, app->width/2, 1, fp);
1910 temp += stride_width;
1914 g_print("codec dec output dumped!!%d\n", ret);
1920 * Add ADTS header at the beginning of each and every AAC packet.
1921 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
1922 * Note the packetLen must count in the ADTS header itself.
1924 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1926 int profile = 2; /* AAC LC (0x01) */
1927 int freqIdx = 3; /* 48KHz (0x03) */
1928 int chanCfg = 2; /* CPE (0x02) */
1930 if (app->samplerate == 96000) freqIdx = 0;
1931 else if (app->samplerate == 88200) freqIdx = 1;
1932 else if (app->samplerate == 64000) freqIdx = 2;
1933 else if (app->samplerate == 48000) freqIdx = 3;
1934 else if (app->samplerate == 44100) freqIdx = 4;
1935 else if (app->samplerate == 32000) freqIdx = 5;
1936 else if (app->samplerate == 24000) freqIdx = 6;
1937 else if (app->samplerate == 22050) freqIdx = 7;
1938 else if (app->samplerate == 16000) freqIdx = 8;
1939 else if (app->samplerate == 12000) freqIdx = 9;
1940 else if (app->samplerate == 11025) freqIdx = 10;
1941 else if (app->samplerate == 8000) freqIdx = 11;
1943 if ((app->channel == 1) || (app->channel == 2))
1944 chanCfg = app->channel;
1946 /* fill in ADTS data */
1947 buffer[0] = (char)0xFF;
1948 buffer[1] = (char)0xF1;
1949 buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1950 buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1951 buffer[4] = (char)((packetLen&0x7FF) >> 3);
1952 buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1953 buffer[6] = (char)0xFC;
1956 static void output_dump(App *app, media_packet_h pkt)
1960 char filename[100] = {0};
1963 char adts[100] = {0, };
1965 sprintf(filename, "/tmp/dec_output_dump_%d.out", app->type);
1966 fp = fopen(filename, "ab");
1968 media_packet_get_buffer_data_ptr(pkt, &temp);
1969 media_packet_get_buffer_size(pkt, &buf_size);
1970 g_print("output data : %p, size %d\n", temp, (int)buf_size);
1972 if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1973 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1974 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1975 g_print("adts appended\n");
1976 } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1) {
1977 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1978 g_print("%s - AMR_header write in first frame\n", __func__);
1979 fwrite(&AMR_header[0], 1, sizeof(AMR_header) - 1, fp); /* AMR-NB magic number */
1980 write_amr_header = 0;
1983 fwrite(temp, (int)buf_size, 1, fp);
1985 g_print("codec dec output dumped!!%d\n", ret);
1990 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
1992 guint media_codec_id_u = (guint)media_codec_id;
1994 switch (media_codec_id_u) {
1995 case MEDIACODEC_L16:
1997 case MEDIACODEC_ALAW:
1999 case MEDIACODEC_ULAW:
2001 case MEDIACODEC_AMR_NB:
2003 case MEDIACODEC_AMR_WB:
2005 case MEDIACODEC_G729:
2007 case MEDIACODEC_AAC_LC:
2009 case MEDIACODEC_AAC_HE:
2011 case MEDIACODEC_AAC_HE_PS:
2013 case MEDIACODEC_MP3:
2015 case MEDIACODEC_VORBIS:
2017 case MEDIACODEC_FLAC:
2019 case MEDIACODEC_WMAV1:
2021 case MEDIACODEC_WMAV2:
2023 case MEDIACODEC_WMAPRO:
2025 case MEDIACODEC_WMALSL:
2027 case MEDIACODEC_H261:
2029 case MEDIACODEC_H263:
2031 case MEDIACODEC_H264:
2033 case MEDIACODEC_MJPEG:
2035 case MEDIACODEC_MPEG1:
2037 case MEDIACODEC_MPEG2:
2039 case MEDIACODEC_MPEG4:
2041 case MEDIACODEC_HEVC:
2043 case MEDIACODEC_VP8:
2045 case MEDIACODEC_VP9:
2047 case MEDIACODEC_VC1: