2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include <Elementary.h>
21 #include <appcore-efl.h>
24 #include <media_codec.h>
25 #include <media_packet.h>
26 #include <media_packet_pool.h>
27 #include <tbm_surface.h>
31 #define PACKAGE "media_codec_test"
36 #define TEST_FILE_SIZE (10 * 1024 * 1024)
37 #define MAX_STRING_LEN 256
39 #define DEFAULT_SAMPPLERATE 44100
40 #define DEFAULT_CHANNEL 2
41 #define DEFAULT_BIT 16
42 #define DEFAULT_BITRATE 128
43 #define DEFAULT_SAMPLEBYTE 1024
44 #define ADTS_HEADER_SIZE 7
45 #define AMRNB_PCM_INPUT_SIZE 320
46 #define AMRWB_PCM_INPUT_SIZE 640
48 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
49 #define GET_IS_ENCODER(x) CHECK_BIT(x, 0)
50 #define GET_IS_DECODER(x) CHECK_BIT(x, 1)
51 #define GET_IS_HW(x) CHECK_BIT(x, 2)
52 #define ES_DEFAULT_VIDEO_PTS_OFFSET 33000000
53 #define CHECK_VALID_PACKET(state, expected_state) \
54 ((state & (expected_state)) == (expected_state))
56 #define AAC_CODECDATA_SIZE 16
58 unsigned char buf_adts[ADTS_HEADER_SIZE];
61 MC_EXIST_SPS = 1 << 0,
62 MC_EXIST_PPS = 1 << 1,
63 MC_EXIST_IDR = 1 << 2,
64 MC_EXIST_SLICE = 1 << 3,
66 MC_VALID_HEADER = (MC_EXIST_SPS | MC_EXIST_PPS),
67 MC_VALID_FIRST_SLICE = (MC_EXIST_SPS | MC_EXIST_PPS | MC_EXIST_IDR)
70 typedef struct _App App;
73 CURRENT_STATUS_MAINMENU,
74 CURRENT_STATUS_FILENAME,
75 CURRENT_STATUS_CREATE,
76 CURRENT_STATUS_DESTROY,
77 CURRENT_STATUS_SET_CODEC,
78 CURRENT_STATUS_SET_VDEC_INFO,
79 CURRENT_STATUS_SET_VENC_INFO,
80 CURRENT_STATUS_SET_ADEC_INFO,
81 CURRENT_STATUS_SET_AENC_INFO,
82 CURRENT_STATUS_PREPARE,
83 CURRENT_STATUS_UNPREPARE,
84 CURRENT_STATUS_PROCESS_INPUT,
85 CURRENT_STATUS_GET_OUTPUT,
86 CURRENT_STATUS_RESET_OUTPUT_BUFFER,
87 CURRENT_STATUS_SET_SIZE,
91 NAL_SLICE_NO_PARTITIONING = 1,
97 NAL_SEQUENCE_PARAMETER_SET,
98 NAL_PICTURE_PARAMETER_SET,
99 NAL_PICTURE_DELIMITER,
139 mediacodec_h mc_handle[MAX_HANDLE];
144 media_format_mimetype_e mime;
159 media_packet_h packet;
167 media_format_h fmt = NULL;
168 media_packet_pool_h pkt_pool = NULL;
170 /* Internal Functions */
171 static int _create_app(void *data);
172 static int _terminate_app(void *data);
173 static void displaymenu(void);
174 static void display_sub_basic();
176 static void _mediacodec_unprepare(App *app);
178 static void mc_hex_dump(char *desc, void *addr, int len);
179 static void decoder_output_dump(App *app, media_packet_h pkt);
180 static void output_dump(App *app, media_packet_h pkt);
183 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
185 int g_menu_state = CURRENT_STATUS_MAINMENU;
187 static int _create_app(void *data)
189 printf("My app is going alive!\n");
190 App *app = (App*)data;
192 g_mutex_init(&app->lock);
196 static int _terminate_app(void *data)
198 printf("My app is going gone!\n");
199 App *app = (App*)data;
201 g_mutex_clear(&app->lock);
206 struct appcore_ops ops = {
207 .create = _create_app,
208 .terminate = _terminate_app,
211 static const guint mp3types_bitrates[2][3][16] = {
213 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
214 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
215 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
218 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
219 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
220 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
224 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
225 {22050, 24000, 16000},
229 void h264_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
231 unsigned char val, zero_count;
232 unsigned char *pNal = app->data + app->offset;
233 int max = app->length - app->offset;
235 int nal_unit_type = 0;
263 if ((zero_count >= 2) && (val == 1))
273 read = (index - zero_count - 1);
275 nal_unit_type = *(app->data+app->offset+4) & 0x1F;
276 g_print("nal_unit_type : %x\n", nal_unit_type);
278 switch (nal_unit_type) {
279 case NAL_SEQUENCE_PARAMETER_SET:
280 g_print("nal_unit_type : SPS\n");
281 state |= MC_EXIST_SPS;
283 case NAL_PICTURE_PARAMETER_SET:
284 g_print("nal_unit_type : PPS\n");
285 state |= MC_EXIST_PPS;
289 g_print ("nal_unit_type : IDR\n");
290 state |= MC_EXIST_IDR;
292 case NAL_SLICE_NO_PARTITIONING:
293 case NAL_SLICE_PART_A:
294 case NAL_SLICE_PART_B:
295 case NAL_SLICE_PART_C:
296 state |= MC_EXIST_SLICE;
299 g_print ("nal_unit_type : %x", nal_unit_type);
303 init = CHECK_VALID_PACKET(state, MC_VALID_FIRST_SLICE) ? 1 : 0;
304 slice = CHECK_VALID_PACKET(state, MC_EXIST_SLICE) ? 1 : 0;
305 idr = CHECK_VALID_PACKET(state, MC_EXIST_IDR) ? 1 : 0;
306 g_print("status : %d, slice : %d, idr : %d\n", init, slice, idr);
308 if (init || idr || slice) {
312 *size = app->offset + read;
314 *data = app->data+app->offset;
319 *data = app->data+app->offset;
326 void h263_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
329 int read_size = 1, state = 1, bStart = 0;
331 unsigned char *pH263 = app->data + app->offset;
333 int max = app->length - app->offset;
337 read_size = (len - 1);
354 if ((val & 0xFC) == 0x80) {
367 app->offset += read_size;
371 void mpeg4_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
375 int state = 1, bType = 0;
377 unsigned char *pMpeg4 = app->data + app->offset;
379 int max = app->length - app->offset;
408 if (val == 0xB0 || val == 0xB6) {
414 if (have_frame && val == 0xB0)
424 app->offset += result;
429 * Extract Input data for AMR-NB/WB decoder
430 * - AMR-NB : mime type ("audio/AMR") / 8Khz / 1 ch / 16 bits
431 * - AMR-WB : mime type ("audio/AMR-WB") / 16Khz / 1 ch / 16 bits
433 int write_amr_header = 1; /* write magic number for AMR Header at one time */
434 static const char AMR_header[] = "#!AMR\n";
435 static const char AMRWB_header[] = "#!AMR-WB\n";
436 #define AMR_NB_MIME_HDR_SIZE 6
437 #define AMR_WB_MIME_HDR_SIZE 9
438 static const int block_size_nb[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
439 static const int block_size_wb[16] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
442 void amrdec_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
444 int readsize = 0, mode_temp;
445 unsigned int fsize, mode;
446 unsigned char *pAmr = app->data + app->offset;
447 /* change the below one to frame count */
448 if (app->offset == 0) {
449 if (!memcmp(pAmr, AMR_header, AMR_NB_MIME_HDR_SIZE)) {
450 blocksize_tbl = (int *)block_size_nb;
451 mode_temp = pAmr[AMR_NB_MIME_HDR_SIZE];
452 pAmr = pAmr + AMR_NB_MIME_HDR_SIZE;
453 app->offset += AMR_NB_MIME_HDR_SIZE;
455 if (!memcmp(pAmr, AMRWB_header, AMR_WB_MIME_HDR_SIZE)) {
456 blocksize_tbl = (int *)block_size_wb;
457 mode_temp = pAmr[AMR_WB_MIME_HDR_SIZE];
458 pAmr = pAmr + AMR_WB_MIME_HDR_SIZE;
459 app->offset += AMR_WB_MIME_HDR_SIZE;
461 g_print("[ERROR] AMR-NB/WB don't detected..\n");
467 if ((mode_temp & 0x83) == 0) {
468 mode = (mode_temp >> 3) & 0x0F; /* Yep. Retrieve the frame size */
469 fsize = blocksize_tbl[mode];
470 readsize = fsize + 1;
473 g_print("[FAIL] Not found amr frame sync.....\n");
477 app->offset += readsize;
482 void nv12_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
485 int offset = app->length - app->offset;
487 yuv_size = app->width * app->height * 3 / 2;
489 if (offset >= yuv_size)
493 *data = app->data + app->offset;
495 if (offset >= yuv_size)
501 void yuv_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
504 int offset = app->length - app->offset;
506 yuv_size = app->width * app->height * 3 / 2;
508 if (yuv_size >= offset)
512 *data = app->data + app->offset;
514 if (yuv_size >= offset)
519 app->offset += *size;
523 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
526 int offset = app->length - app->offset;
528 read_size = ((DEFAULT_SAMPLEBYTE * app->channel)*(app->bit/8) * 2);
531 *data = app->data + app->offset;
533 if (read_size >= offset)
538 app->offset += *size;
541 void amrenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
544 int offset = app->length - app->offset;
547 read_size = AMRNB_PCM_INPUT_SIZE;
549 read_size = AMRWB_PCM_INPUT_SIZE;
552 *data = app->data + app->offset;
554 if (read_size >= offset)
559 app->offset += *size;
563 * Extract Input data for AAC decoder
564 * (case of (LC profile) ADTS format)
565 * codec_data : Don't need
567 void aacdec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
570 int offset = app->length - app->offset;
571 unsigned char *pData = app->data + app->offset;
573 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
574 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
577 g_print("[FAIL] Not found aac frame sync.....\n");
581 *data = app->data + app->offset;
583 if (read_size >= offset)
588 app->offset += *size;
592 void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
596 guint padding, bitrate, lsf = 0, layer = 0, mpg25 = 0;
597 guint hdr_bitrate = 0, sf = 0;
598 int offset = app->length - app->offset;
599 unsigned char *pData = app->data + app->offset;
601 header = GST_READ_UINT32_BE(pData);
604 g_print ("[ERROR] read header size is 0\n");
608 /* if it's not a valid sync */
609 if ((header & 0xffe00000) != 0xffe00000) {
610 g_print ("[ERROR] invalid sync\n");
614 if (((header >> 19) & 3) == 0x1) {
615 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
618 if (header & (1 << 20)) {
619 lsf = (header & (1 << 19)) ? 0 : 1;
627 /* if it's an invalid layer */
628 if (!((header >> 17) & 3)) {
629 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
632 layer = 4 - ((header >> 17) & 0x3);
635 /* if it's an invalid bitrate */
636 if (((header >> 12) & 0xf) == 0xf) {
637 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
640 bitrate = (header >> 12) & 0xF;
641 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
642 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
643 if (hdr_bitrate == 0)
647 /* if it's an invalid samplerate */
648 if (((header >> 10) & 0x3) == 0x3) {
649 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
653 sf = (header >> 10) & 0x3;
654 sf = mp3types_freqs[lsf + mpg25][sf];
657 padding = (header >> 9) & 0x1;
661 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
664 read_size = (hdr_bitrate * 144) / sf + padding;
668 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
671 g_print("header : %d, read : %d\n", header, read_size);
674 *data = app->data + app->offset;
676 if (read_size >= offset)
681 app->offset += *size;
685 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
687 int readsize = 0, read_size = 0;
688 unsigned int header_size = ADTS_HEADER_SIZE;
689 unsigned char buffer[100000];
690 unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
691 int offset = app->length - app->offset;
692 unsigned char *pData = app->data + app->offset;
694 * It is not support full parsing MP4 container box.
695 * So It MUST start as RAW valid frame sequence.
696 * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
699 /* change the below one later */
700 if (app->offset == 0) {
702 * CAUTION : Codec data is needed only once in first time
703 * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
704 * So I use this data (byte) as hard coding for temporary our testing.
708 * The codec_data data is according to AudioSpecificConfig,
709 * ISO/IEC 14496-3, 1.6.2.1
711 * below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
712 * case : M4A - LC profile
713 * codec_data=(buffer)119056e5000000000000000000000000
714 * savs aac decoder get codec_data. size: 16 (Tag size : 5 byte)
715 * - codec data: profile : 2
716 * - codec data: samplrate: 48000
717 * - codec data: channels : 2
719 /* 2 bytes are mandatory */
720 codecdata[0] = 0x11; /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
721 codecdata[1] = 0x90; /* ex) (4bit) 2 (2ch) */
722 /* othter bytes are (optional) epconfig information */
728 * below example is test for using "TestSample-EAAC+.m4a"
730 * case : M4A - HE-AAC v1 and v2 profile
731 * codec_data=(buffer)138856e5a54880000000000000000000
732 * savs aac decoder get codec_data. size: 16 (Tag size : 7 byte)
733 * - codec data: profile : 2
734 * - codec data: samplrate: 22050
735 * - codec data: channels : 1
737 /* 2 bytes are mandatory */
738 codecdata[0] = 0x13; /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
739 codecdata[1] = 0x88; /* ex) (4bit) 1 (1ch) */
740 /* othter bytes are (optional) epconfig information */
748 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
749 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
750 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
753 g_print("[FAIL] Not found aac frame sync.....\n");
755 readsize = read_size - header_size;
756 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
757 read_size = readsize + AAC_CODECDATA_SIZE; /* return combination of (codec_data + raw_data) */
758 app->offset += header_size + readsize;
762 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
763 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
764 readsize = read_size - header_size;
765 memcpy(buffer, pData + 7, readsize); /* Make only RAW data, so exclude header 7 bytes */
766 read_size = readsize;
767 app->offset += header_size + readsize;
771 g_print("[FAIL] Not found aac frame sync. \n");
776 if (read_size >= offset)
784 * Extract Input data for AAC encoder
787 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
790 int offset = app->length - app->offset;
792 read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
794 if (read_size >= offset)
798 *data = app->data + app->offset;
800 if (read_size >= offset)
805 app->offset += *size;
809 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
812 g_print("Used input buffer = %p\n", pkt);
813 media_packet_destroy(pkt);
818 int _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
821 media_format_mimetype_e mime = 0;
822 encoder = GET_IS_ENCODER(flag) ? 1 : 0;
823 *hardware = GET_IS_HW(flag) ? 1 : 0;
824 app->is_encoder = encoder;
827 case MEDIACODEC_H264:
829 extractor = yuv_extractor;
830 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
832 extractor = h264_extractor;
833 mime = MEDIA_FORMAT_H264_SP;
836 case MEDIACODEC_MPEG4:
838 extractor = yuv_extractor;
839 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
841 extractor = mpeg4_extractor;
842 mime = MEDIA_FORMAT_MPEG4_SP;
845 case MEDIACODEC_H263:
847 extractor = h263_extractor;
848 mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
850 extractor = h263_extractor;
851 mime = MEDIA_FORMAT_H263P;
856 extractor = aacenc_extractor;
857 mime = MEDIA_FORMAT_PCM;
859 extractor = aacdec_extractor;
860 mime = MEDIA_FORMAT_AAC;
863 case MEDIACODEC_AAC_HE:
865 extractor = aacenc_extractor;
866 mime = MEDIA_FORMAT_PCM;
868 extractor = extract_input_aacdec_m4a_test;
869 mime = MEDIA_FORMAT_AAC_HE;
872 case MEDIACODEC_AAC_HE_PS:
875 extractor = mp3dec_extractor;
876 mime = MEDIA_FORMAT_MP3;
878 case MEDIACODEC_VORBIS:
880 case MEDIACODEC_FLAC:
882 case MEDIACODEC_WMAV1:
884 case MEDIACODEC_WMAV2:
886 case MEDIACODEC_WMAPRO:
888 case MEDIACODEC_WMALSL:
890 case MEDIACODEC_AMR_NB:
892 extractor = amrenc_extractor;
893 mime = MEDIA_FORMAT_PCM;
894 app->is_amr_nb = TRUE;
896 extractor = amrdec_extractor;
897 mime = MEDIA_FORMAT_AMR_NB;
900 case MEDIACODEC_AMR_WB:
902 extractor = amrenc_extractor;
903 mime = MEDIA_FORMAT_PCM;
904 app->is_amr_nb = FALSE;
906 extractor = amrdec_extractor;
907 mime = MEDIA_FORMAT_AMR_WB;
911 LOGE("NOT SUPPORTED!!!!");
917 static void _mediacodec_process_input(App *app)
920 bool have_frame = FALSE;
922 static guint64 pts = 0L;
923 void *buf_data_ptr = NULL;
924 media_packet_h pkt = NULL;
928 int stride_width, stride_height;
930 for (i = 0; i < app->frame; i++) {
931 g_print("----------read data------------\n");
933 extractor(app, &tmp, &read, &have_frame);
936 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
937 fprintf(stderr, "media_packet_create_alloc failed\n");
941 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
942 fprintf(stderr, "media_packet_set_pts failed\n");
946 if (app->type != VIDEO_ENC) {
947 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
948 media_packet_set_buffer_size(pkt, (uint64_t)read);
950 memcpy(buf_data_ptr, tmp, read);
951 g_print("tmp:%p, read:%d\n", tmp, read);
954 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
955 media_packet_get_video_stride_width(pkt, 0, &stride_width);
956 media_packet_get_video_stride_height(pkt, 0, &stride_height);
958 offset = stride_width*stride_height;
960 memcpy(buf_data_ptr, tmp, offset);
963 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
964 media_packet_get_video_stride_width(pkt, 1, &stride_width);
965 media_packet_get_video_stride_height(pkt, 1, &stride_height);
966 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
968 if (app->hardware == FALSE) {
970 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
971 media_packet_get_video_stride_width(pkt, 2, &stride_width);
972 media_packet_get_video_stride_height(pkt, 2, &stride_height);
974 offset += stride_width * stride_height;
977 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
980 mc_hex_dump("inbuf", tmp, 48);
982 ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
983 if (ret != MEDIACODEC_ERROR_NONE)
986 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
991 static gboolean read_data(App *app)
994 bool have_frame = FALSE;
996 static guint64 pts = 0L;
997 void *buf_data_ptr = NULL;
998 media_packet_h pkt = NULL;
1002 int stride_width, stride_height;
1004 if (app->offset == 0) {
1005 app->frame_count = 0;
1006 app->start = clock();
1009 g_print("----------read data------------\n");
1010 extractor(app, &tmp, &read, &have_frame);
1012 if (app->offset >= app->length - 4) {
1015 app->finish = clock();
1016 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1017 g_print("---------------------------\n");
1020 g_print("length : %d, offset : %d\n", app->length, app->offset);
1022 if (app->offset + len > app->length)
1023 len = app->length - app->offset;
1025 g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1029 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1030 fprintf(stderr, "media_packet_pool_aquire_packet failed\n");
1034 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1035 fprintf(stderr, "media_packet_create_alloc failed\n");
1039 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1040 fprintf(stderr, "media_packet_set_pts failed\n");
1044 if (app->type != VIDEO_ENC) {
1045 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1046 media_packet_set_buffer_size(pkt, (uint64_t)read);
1048 memcpy(buf_data_ptr, tmp, read);
1049 g_print("tmp:%p, read:%d\n", tmp, read);
1052 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1053 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1054 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1056 offset = stride_width*stride_height;
1058 memcpy(buf_data_ptr, tmp, offset);
1061 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1062 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1063 media_packet_get_video_stride_height(pkt, 1, &stride_height);
1064 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
1066 if (app->hardware == FALSE) {
1068 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1069 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1070 media_packet_get_video_stride_height(pkt, 2, &stride_height);
1072 offset += stride_width * stride_height;
1075 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
1078 mc_hex_dump("inbuf", tmp, 48);
1080 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1081 if (ret != MEDIACODEC_ERROR_NONE)
1084 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1090 static void start_feed(App *app)
1092 if (app->sourceid == 0) {
1093 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1094 g_print("start_feed\n");
1098 static void stop_feed(App *app)
1100 if (app->sourceid != 0) {
1101 g_source_remove(app->sourceid);
1103 g_print("stop_feed\n");
1107 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1109 g_print("_mediacodec_inbuf_used_cb!!!\n");
1111 media_packet_pool_release_packet(pkt_pool, pkt);
1113 media_packet_destroy(pkt);
1119 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1121 media_packet_h out_pkt = NULL;
1124 App *app = (App*)user_data;
1126 g_print("_mediacodec_outbuf_available_cb\n");
1128 g_mutex_lock(&app->lock);
1130 ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1132 if (ret != MEDIACODEC_ERROR_NONE)
1133 g_print("get_output failed\n");
1135 if (app->enable_dump) {
1136 if (app->type == VIDEO_DEC)
1137 decoder_output_dump(app, out_pkt);
1139 output_dump(app, out_pkt);
1145 g_mutex_unlock(&app->lock);
1147 media_packet_destroy(out_pkt);
1154 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1156 g_print("_mediacodec_buffer_status_cb %d\n", status);
1158 App *app = (App*)user_data;
1160 if (status == MEDIACODEC_NEED_DATA)
1162 else if (status == MEDIACODEC_ENOUGH_DATA)
1168 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1173 static bool _mediacodec_eos_cb(void *user_data)
1178 static void _mediacodec_prepare(App *app, bool frame_all)
1182 /* create instance */
1183 ret = mediacodec_create(&app->mc_handle[0]);
1184 if (ret != MEDIACODEC_ERROR_NONE) {
1185 g_print("mediacodec_create failed\n");
1190 ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1191 if (ret != MEDIACODEC_ERROR_NONE) {
1192 g_print("mediacodec_set_codec failed\n");
1196 app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1198 /* set codec info */
1199 ret = media_format_create(&fmt);
1201 switch (app->type) {
1203 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1204 media_format_set_video_mime(fmt, app->mime);
1205 media_format_set_video_width(fmt, app->width);
1206 media_format_set_video_height(fmt, app->height);
1209 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1210 media_format_set_video_mime(fmt, app->mime);
1211 media_format_set_video_width(fmt, app->width);
1212 media_format_set_video_height(fmt, app->height);
1213 media_format_set_video_avg_bps(fmt, app->target_bits);
1216 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1217 media_format_set_audio_mime(fmt, app->mime);
1218 media_format_set_audio_channel(fmt, app->channel);
1219 media_format_set_audio_samplerate(fmt, app->samplerate);
1220 media_format_set_audio_bit(fmt, app->bit);
1223 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1224 media_format_set_audio_mime(fmt, app->mime);
1225 media_format_set_audio_channel(fmt, app->channel);
1226 media_format_set_audio_samplerate(fmt, app->samplerate);
1227 media_format_set_audio_bit(fmt, app->bit);
1230 g_print("invaild type\n");
1234 if (ret != MEDIACODEC_ERROR_NONE) {
1235 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1240 mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1241 mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1243 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1244 mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1245 mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1248 ret = mediacodec_prepare(app->mc_handle[0]);
1249 if (ret != MEDIACODEC_ERROR_NONE) {
1250 g_print("mediacodec_prepare failed\n");
1255 /* get packet pool instance */
1256 ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1257 if (ret != MEDIA_PACKET_ERROR_NONE) {
1258 g_print("mediacodec_get_packet_pool failed\n");
1264 static void _mediacodec_unprepare(App *app)
1266 mediacodec_unprepare(app->mc_handle[0]);
1269 static void _mediacodec_destroy(App *app)
1272 if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1274 fprintf(stderr, "media_packet_pool_deallocatet failed\n");
1275 g_print("PKT POOL deallocation failed \n");
1278 g_print("PKT POOL deallocated! \n");
1280 if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1282 fprintf(stderr, " media_packet_pool_destroy failed\n");
1283 g_print("PKT POOL destroy failed \n");
1286 g_print("PKT POOL destroyed! \n");
1288 mediacodec_destroy(app->mc_handle[0]);
1291 static void input_filepath(char *filename, App *app)
1293 GError *error = NULL;
1296 app->file = g_mapped_file_new(filename, FALSE, &error);
1298 g_print("failed to open file : %s\n", error->message);
1299 g_error_free(error);
1303 app->length = g_mapped_file_get_length(app->file);
1304 app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1306 g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1311 void quit_program(App *app)
1313 media_format_unref(fmt);
1314 g_main_loop_quit(app->loop);
1319 void reset_menu_state()
1321 g_menu_state = CURRENT_STATUS_MAINMENU;
1325 void _interpret_main_menu(char *cmd, App *app)
1327 int len = strlen(cmd);
1329 if (strncmp(cmd, "a", 1) == 0)
1330 g_menu_state = CURRENT_STATUS_FILENAME;
1331 else if (strncmp(cmd, "o", 1) == 0)
1332 g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1333 else if (strncmp(cmd, "q", 1) == 0)
1336 g_print("unknown menu \n");
1337 } else if (len == 2) {
1338 if (strncmp(cmd, "pr", 2) == 0)
1339 _mediacodec_prepare(app, 0);
1340 else if (strncmp(cmd, "pa", 2) == 0)
1341 _mediacodec_prepare(app, 1);
1342 else if (strncmp(cmd, "sc", 2) == 0)
1343 g_menu_state = CURRENT_STATUS_SET_CODEC;
1344 else if (strncmp(cmd, "vd", 2) == 0)
1345 g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1346 else if (strncmp(cmd, "ve", 2) == 0)
1347 g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1348 else if (strncmp(cmd, "ad", 2) == 0)
1349 g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1350 else if (strncmp(cmd, "ae", 2) == 0)
1351 g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1352 else if (strncmp(cmd, "pi", 2) == 0)
1353 g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1354 else if (strncmp(cmd, "un", 2) == 0)
1355 _mediacodec_unprepare(app);
1356 else if (strncmp(cmd, "dt", 2) == 0)
1357 _mediacodec_destroy(app);
1358 else if (strncmp(cmd, "dp", 2) == 0) {
1359 if (!app->enable_dump) {
1360 app->enable_dump = TRUE;
1361 g_print("dump enabled\n");
1363 app->enable_dump = FALSE;
1364 g_print("dump disabled\n");
1367 display_sub_basic();
1369 g_print("unknown menu \n");
1375 static void displaymenu(void)
1377 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1378 display_sub_basic();
1379 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1380 g_print("*** input mediapath.\n");
1381 } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1382 g_print("*** Codec id : Select Codec ID Numbe (e.g. AAC_LC = 96)\n");
1383 g_print(" L16 = 16 (0x10)\n");
1384 g_print(" ALAW = 32 (0x20)\n");
1385 g_print(" ULAW = 48 (0x30)\n");
1386 g_print(" AMR_NB = 64 (0x40)\n");
1387 g_print(" AMR_WB = 65 (0x41)\n");
1388 g_print(" G729 = 80 (0x50)\n");
1389 g_print(" AAC_LC = 96 (0x60)\n");
1390 g_print(" AAC_HE = 97 (0x61)\n");
1391 g_print(" AAC_PS = 98 (0x62)\n");
1392 g_print(" MP3 = 112 (0x70)\n");
1393 g_print(" VORBIS = 128 (0x80)\n");
1394 g_print(" FLAC = 144 (0x90)\n");
1395 g_print(" WMAV1 = 160 (0xA0)\n");
1396 g_print(" WMAV2 = 161 (0xA1)\n");
1397 g_print(" WMAPRO = 162 (0xA2)\n");
1398 g_print(" WMALSL = 163 (0xA3)\n");
1399 g_print(" -------------------\n");
1400 g_print(" H261 = 101\n");
1401 g_print(" H263 = 102\n");
1402 g_print(" H264 = 103\n");
1403 g_print(" MJPEG = 104\n");
1404 g_print(" MPEG1 = 105\n");
1405 g_print(" MPEG2 = 106\n");
1406 g_print(" MPEG4 = 107\n");
1407 g_print(" -------------------\n");
1408 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1409 g_print(" CODEC : ENCODER = 1 DECODER = 2\n");
1410 g_print(" TYPE : HW = 4 SW = 8\n");
1411 g_print("*** input codec id, falgs.\n");
1412 } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1413 g_print("*** input video decode configure.(width, height)\n");
1414 } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1415 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1416 } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1417 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000, 2, 16))\n");
1418 } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1419 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000, 2, 16, 128000))\n");
1420 } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1421 g_print("*** input dec process number\n");
1422 } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1423 g_print("*** input get output buffer number\n");
1425 g_print("*** unknown status.\n");
1430 gboolean timeout_menu_display(void* data)
1437 static void interpret(char *cmd, App *app)
1439 switch (g_menu_state) {
1440 case CURRENT_STATUS_MAINMENU:
1441 _interpret_main_menu(cmd, app);
1443 case CURRENT_STATUS_FILENAME:
1444 input_filepath(cmd, app);
1447 case CURRENT_STATUS_SET_CODEC:
1460 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1461 tmp = strtol(cmd, ptr, 16);
1462 app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1464 app->codecid = 0x1000 + tmp;
1469 app->flag = atoi(cmd);
1478 case CURRENT_STATUS_SET_VDEC_INFO:
1483 app->width = atoi(cmd);
1487 app->height = atoi(cmd);
1488 app->type = VIDEO_DEC;
1498 case CURRENT_STATUS_SET_VENC_INFO:
1503 app->width = atoi(cmd);
1507 app->height = atoi(cmd);
1511 app->fps = atol(cmd);
1515 app->target_bits = atoi(cmd);
1516 app->type = VIDEO_ENC;
1526 case CURRENT_STATUS_SET_ADEC_INFO:
1531 app->samplerate = atoi(cmd);
1535 app->channel = atoi(cmd);
1539 app->bit = atoi(cmd);
1540 app->type = AUDIO_DEC;
1550 case CURRENT_STATUS_SET_AENC_INFO:
1555 app->samplerate = atoi(cmd);
1559 app->channel = atoi(cmd);
1563 app->bit = atoi(cmd);
1567 app->bitrate = atoi(cmd);
1568 app->type = AUDIO_ENC;
1578 case CURRENT_STATUS_PROCESS_INPUT:
1580 app->frame = atoi(cmd);
1581 _mediacodec_process_input(app);
1585 case CURRENT_STATUS_GET_OUTPUT:
1594 g_timeout_add(100, timeout_menu_display, 0);
1597 static void display_sub_basic()
1600 g_print("=========================================================================================\n");
1601 g_print(" media codec test\n");
1602 g_print("-----------------------------------------------------------------------------------------\n");
1603 g_print("a. Create \t\t");
1604 g_print("sc. Set codec \n");
1605 g_print("vd. Set vdec info \t");
1606 g_print("ve. Set venc info \n");
1607 g_print("ad. Set adec info \t");
1608 g_print("ae. Set aenc info \n");
1609 g_print("pr. Prepare \t");
1610 g_print("pa. Prepare and process all\t\t");
1611 g_print("pi. process input with num\n");
1612 g_print("o. Get output \t\t");
1613 g_print("rb. Reset output buffer \n");
1614 g_print("un. Unprepare \t\t");
1615 g_print("dt. Destroy \t\t");
1616 g_print("q. quite test suite \n");
1617 g_print("dp. enable dump \n");
1619 g_print("=========================================================================================\n");
1622 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1624 gchar buf[MAX_STRING_LEN];
1626 GError *error = NULL;
1627 App *context = (App*)data;
1629 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1632 interpret(buf, context);
1637 int main(int argc, char *argv[])
1641 GIOChannel *stdin_channel;
1642 stdin_channel = g_io_channel_unix_new(0);
1643 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1644 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1648 app->loop = g_main_loop_new(NULL, TRUE);
1649 app->timer = g_timer_new();
1650 g_main_loop_run(app->loop);
1656 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1661 void mc_hex_dump(char *desc, void *addr, int len)
1664 unsigned char buff[17];
1665 unsigned char *pc = (unsigned char *)addr;
1668 printf("%s:\n", desc);
1670 for (i = 0; i < len; i++) {
1672 if ((i % 16) == 0) {
1674 printf(" %s\n", buff);
1676 printf(" %04x ", i);
1679 printf(" %02x", pc[i]);
1681 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1684 buff[i % 16] = pc[i];
1685 buff[(i % 16) + 1] = '\0';
1688 while ((i % 16) != 0) {
1692 printf(" %s\n", buff);
1695 static void decoder_output_dump(App *app, media_packet_h pkt)
1699 int stride_width, stride_height;
1700 char filename[100] = {0};
1704 sprintf(filename, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1705 fp = fopen(filename, "ab");
1707 media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1708 media_packet_get_video_stride_width(pkt, 0, &stride_width);
1709 media_packet_get_video_stride_height(pkt, 0, &stride_height);
1710 printf("stride : %d, %d\n", stride_width, stride_height);
1712 for (i = 0; i < app->height; i++) {
1713 ret = fwrite(temp, app->width, 1, fp);
1714 temp += stride_width;
1717 if (app->hardware == TRUE) {
1718 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1719 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1720 for (i = 0; i < app->height/2; i++) {
1721 ret = fwrite(temp, app->width, 1, fp);
1722 temp += stride_width;
1725 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1726 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1727 for (i = 0; i < app->height/2; i++) {
1728 ret = fwrite(temp, app->width/2, 1, fp);
1729 temp += stride_width;
1732 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1733 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1734 for (i = 0; i < app->height/2; i++) {
1735 ret = fwrite(temp, app->width/2, 1, fp);
1736 temp += stride_width;
1740 g_print("codec dec output dumped!!%d\n", ret);
1746 * Add ADTS header at the beginning of each and every AAC packet.
1747 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
1748 * Note the packetLen must count in the ADTS header itself.
1750 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1752 int profile = 2; /* AAC LC (0x01) */
1753 int freqIdx = 3; /* 48KHz (0x03) */
1754 int chanCfg = 2; /* CPE (0x02) */
1756 if (app->samplerate == 96000) freqIdx = 0;
1757 else if (app->samplerate == 88200) freqIdx = 1;
1758 else if (app->samplerate == 64000) freqIdx = 2;
1759 else if (app->samplerate == 48000) freqIdx = 3;
1760 else if (app->samplerate == 44100) freqIdx = 4;
1761 else if (app->samplerate == 32000) freqIdx = 5;
1762 else if (app->samplerate == 24000) freqIdx = 6;
1763 else if (app->samplerate == 22050) freqIdx = 7;
1764 else if (app->samplerate == 16000) freqIdx = 8;
1765 else if (app->samplerate == 12000) freqIdx = 9;
1766 else if (app->samplerate == 11025) freqIdx = 10;
1767 else if (app->samplerate == 8000) freqIdx = 11;
1769 if ((app->channel == 1) || (app->channel == 2))
1770 chanCfg = app->channel;
1772 /* fill in ADTS data */
1773 buffer[0] = (char)0xFF;
1774 buffer[1] = (char)0xF1;
1775 buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1776 buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1777 buffer[4] = (char)((packetLen&0x7FF) >> 3);
1778 buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1779 buffer[6] = (char)0xFC;
1782 static void output_dump(App *app, media_packet_h pkt)
1786 char filename[100] = {0};
1789 char adts[100] = {0, };
1791 sprintf(filename, "/tmp/dec_output_dump_%d.out", app->type);
1792 fp = fopen(filename, "ab");
1794 media_packet_get_buffer_data_ptr(pkt, &temp);
1795 media_packet_get_buffer_size(pkt, &buf_size);
1796 g_print("output data : %p, size %d\n", temp, (int)buf_size);
1798 if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1799 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1800 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1801 g_print("adts appended\n");
1802 } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1) {
1803 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1804 g_print("%s - AMR_header write in first frame\n", __func__);
1805 fwrite(&AMR_header[0], 1, sizeof(AMR_header) - 1, fp); /* AMR-NB magic number */
1806 write_amr_header = 0;
1809 fwrite(temp, (int)buf_size, 1, fp);
1811 g_print("codec dec output dumped!!%d\n", ret);