Fixed svace issue -prevent division by zero
[platform/core/api/mediacodec.git] / test / media_codec_test.c
1 /*
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <glib.h>
20 #include <Elementary.h>
21 #include <appcore-efl.h>
22 #include <gst/gst.h>
23
24 #include <media_codec.h>
25 #include <media_packet.h>
26 #include <tbm_surface.h>
27 #include <dlog.h>
28 #include <time.h>
29
30 #define PACKAGE "media_codec_test"
31 #define MAX_HANDLE                      4
32 #if 0
33 #define DUMP_OUTBUF           1
34 #endif
35 #define TEST_FILE_SIZE        (10 * 1024 * 1024)
36 #define MAX_STRING_LEN        256
37
38 #define DEFAULT_SAMPPLERATE   44100
39 #define DEFAULT_CHANNEL             2
40 #define DEFAULT_BIT                           16
41 #define DEFAULT_BITRATE       128
42 #define DEFAULT_SAMPLEBYTE        1024
43 #define ADTS_HEADER_SIZE      7
44 #define AMRNB_PCM_INPUT_SIZE      320
45 #define AMRWB_PCM_INPUT_SIZE      640
46
47 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
48 #define GET_IS_ENCODER(x) CHECK_BIT(x, 0)
49 #define GET_IS_DECODER(x) CHECK_BIT(x, 1)
50 #define GET_IS_HW(x) CHECK_BIT(x, 2)
51 #define ES_DEFAULT_VIDEO_PTS_OFFSET 33000000
52 #define CHECK_VALID_PACKET(state, expected_state) \
53         ((state & (expected_state)) == (expected_state))
54
55 #define AAC_CODECDATA_SIZE    16
56
57 static int samplebyte = DEFAULT_SAMPLEBYTE;
58 unsigned char buf_adts[ADTS_HEADER_SIZE];
59
60 enum {
61         MC_EXIST_SPS    = 1 << 0,
62         MC_EXIST_PPS    = 1 << 1,
63         MC_EXIST_IDR    = 1 << 2,
64         MC_EXIST_SLICE  = 1 << 3,
65
66         MC_VALID_HEADER = (MC_EXIST_SPS | MC_EXIST_PPS),
67         MC_VALID_FIRST_SLICE = (MC_EXIST_SPS | MC_EXIST_PPS | MC_EXIST_IDR)
68 };
69
70 typedef struct _App App;
71
72 enum {
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,
88 };
89
90 typedef enum {
91         NAL_SLICE_NO_PARTITIONING = 1,
92         NAL_SLICE_PART_A,
93         NAL_SLICE_PART_B,
94         NAL_SLICE_PART_C,
95         NAL_SLICE_IDR,
96         NAL_SEI,
97         NAL_SEQUENCE_PARAMETER_SET,
98         NAL_PICTURE_PARAMETER_SET,
99         NAL_PICTURE_DELIMITER,
100         NAL_END_OF_SEQUENCE,
101         NAL_END_OF_STREAM,
102         NAL_FILLER_DATA,
103         NAL_PREFIX_SVC = 14
104 } nal_unit_type;
105
106 typedef enum {
107         VIDEO_DEC,
108         VIDEO_ENC,
109         AUDIO_DEC,
110         AUDIO_ENC
111 } type_e;
112
113
114 struct _App {
115         GMainLoop *loop;
116         guint sourceid;
117
118         GMappedFile *file;
119         guint8 *data;
120         gsize length;
121         guint64 offset;
122         guint obj;
123
124         GTimer *timer;
125         long start;
126         long finish;
127         long process_time;
128         int frame_count;
129
130         int codecid;
131         int flag;
132         bool is_video[MAX_HANDLE];
133         bool is_encoder[MAX_HANDLE];
134         bool hardware;
135         type_e type;
136         /* video */
137         mediacodec_h mc_handle[MAX_HANDLE];
138         guint width;
139         guint height;
140         guint fps;
141         guint target_bits;
142         media_format_mimetype_e mime;
143
144         /* Audio */
145         guint samplerate;
146         guint channel;
147         guint bit;
148         guint bitrate;
149         bool is_amr_nb;
150
151
152         /* Render */
153         guint w;
154         guint h;
155         Evas_Object *win;
156         Evas_Object *img;
157         media_packet_h packet;
158         Ecore_Pipe *pipe;
159         GList *packet_list;
160         GMutex lock;
161 };
162
163 App s_app;
164
165 media_format_h aenc_fmt = NULL;
166 media_format_h adec_fmt = NULL;
167 media_format_h vdec_fmt = NULL;
168 media_format_h venc_fmt = NULL;
169
170 #if DUMP_OUTBUF
171 FILE *fp_out = NULL;
172 #endif
173
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();
179
180 /* For debugging */
181 static void mc_hex_dump(char *desc, void *addr, int len);
182 #if DUMP_OUTBUF
183 static void decoder_output_dump(App *app, media_packet_h pkt);
184 #endif
185 /* */
186
187 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
188
189 int g_menu_state = CURRENT_STATUS_MAINMENU;
190
191 static int _create_app(void *data)
192 {
193         printf("My app is going alive!\n");
194         App *app = (App*)data;
195
196         g_mutex_init(&app->lock);
197         return 0;
198 }
199
200 static int _terminate_app(void *data)
201 {
202         printf("My app is going gone!\n");
203         App *app = (App*)data;
204
205         g_mutex_clear(&app->lock);
206         return 0;
207 }
208
209
210 struct appcore_ops ops = {
211         .create = _create_app,
212         .terminate = _terminate_app,
213 };
214
215 static const guint mp3types_bitrates[2][3][16] = {
216         {
217                 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
218                 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
219                 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
220         },
221         {
222                 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
223                 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
224                 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
225         },
226 };
227
228 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
229         {22050, 24000, 16000},
230         {11025, 12000, 8000}
231 };
232
233 void h264_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
234 {
235         unsigned char val, zero_count;
236         unsigned char *pNal = app->data + app->offset;
237         int max = app->length - app->offset;
238         int index = 0;
239         int nal_unit_type = 0;
240         bool init;
241         bool slice;
242         bool idr;
243         static int state;
244         int read;
245
246         zero_count = 0;
247
248         val = pNal[index++];
249         while (!val) {
250                 zero_count++;
251                 val = pNal[index++];
252         }
253
254         zero_count = 0;
255
256         while (1) {
257                 if (index >= max) {
258                         read = (index - 1);
259                         goto DONE;
260                 }
261
262                 val = pNal[index++];
263
264                 if (!val)
265                         zero_count++;
266                 else {
267                         if ((zero_count >= 2) && (val == 1))
268                                 break;
269                         else
270                                 zero_count = 0;
271                 }
272         }
273
274         if (zero_count > 3)
275                 zero_count = 3;
276
277         read = (index - zero_count - 1);
278
279         nal_unit_type = *(app->data+app->offset+4) & 0x1F;
280         g_print("nal_unit_type : %x\n", nal_unit_type);
281
282         switch (nal_unit_type) {
283         case NAL_SEQUENCE_PARAMETER_SET:
284                 g_print("nal_unit_type : SPS\n");
285                 state |= MC_EXIST_SPS;
286                 break;
287         case NAL_PICTURE_PARAMETER_SET:
288                 g_print("nal_unit_type : PPS\n");
289                 state |= MC_EXIST_PPS;
290                 break;
291         case NAL_SLICE_IDR:
292         case NAL_SEI:
293                 g_print ("nal_unit_type : IDR\n");
294                 state |= MC_EXIST_IDR;
295                 break;
296         case NAL_SLICE_NO_PARTITIONING:
297         case NAL_SLICE_PART_A:
298         case NAL_SLICE_PART_B:
299         case NAL_SLICE_PART_C:
300                 state |= MC_EXIST_SLICE;
301                 break;
302         default:
303                 g_print ("nal_unit_type : %x", nal_unit_type);
304                 break;
305         }
306
307         init = CHECK_VALID_PACKET(state, MC_VALID_FIRST_SLICE) ? 1 : 0;
308         slice = CHECK_VALID_PACKET(state, MC_EXIST_SLICE) ? 1 : 0;
309         idr = CHECK_VALID_PACKET(state, MC_EXIST_IDR) ? 1 : 0;
310         g_print("status : %d, slice : %d, idr : %d\n", init, slice, idr);
311
312         if (init || idr || slice) {
313                 *have_frame = TRUE;
314                 if (init) {
315                         *data = app->data;
316                         *size = app->offset + read;
317                 } else {
318                         *data = app->data+app->offset;
319                         *size = read;
320                 }
321                 state = 0;
322         } else {
323                 *data = app->data+app->offset;
324                 *size = read;
325         }
326 DONE:
327         app->offset += read;
328 }
329
330 void h263_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
331 {
332         int len = 0;
333         int read_size = 1, state = 1, bStart = 0;
334         unsigned char val;
335         unsigned char *pH263 = app->data + app->offset;
336         *data = pH263;
337         int max = app->length - app->offset;
338         *have_frame = TRUE;
339
340         while (1) {
341                 if (len >= max) {
342                         read_size = (len - 1);
343                         *have_frame = FALSE;
344                         goto DONE;
345                 }
346                 val = pH263[len++];
347                 switch (state) {
348                 case 1:
349                         if (val == 0x00)
350                                 state++;
351                         break;
352                 case 2:
353                         if (val == 0x00)
354                                 state++;
355                         else
356                                 state = 1;
357                         break;
358                 case 3:
359                         state = 1;
360                         if ((val & 0xFC) == 0x80) {
361                                 if (bStart) {
362                                         read_size = len - 3;
363                                         goto DONE;
364                                 } else {
365                                         bStart = 1;
366                                 }
367                         }
368                         break;
369                 }
370         }
371  DONE:
372         *size = read_size;
373         app->offset += read_size;
374 }
375
376 void mpeg4_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
377 {
378         int len = 0;
379         int result = 0;
380         int state = 1, bType = 0;
381         unsigned char val;
382         unsigned char *pMpeg4 = app->data + app->offset;
383         *data = pMpeg4;
384         int max = app->length - app->offset;
385
386         while (1) {
387                 if (len >= max) {
388                         *have_frame = FALSE;
389                         goto DONE;
390                 }
391
392                 val = pMpeg4[len++];
393
394                 switch (state) {
395                 case 1:
396                         if (val == 0x00)
397                                 state++;
398                         break;
399                 case 2:
400                         if (val == 0x00)
401                                 state++;
402                         else
403                                 state = 1;
404                         break;
405                 case 3:
406                         if (val == 0x01) {
407                                 state++;
408                         } else
409                                 state = 1;
410                         break;
411                 case 4:
412                         state = 1;
413                         if (val == 0xB0 || val == 0xB6) {
414                                 if (bType == 0xB6) {
415                                         result = len - 4;
416                                         goto DONE;
417                                 }
418                                 if (!bType) {
419                                         if (have_frame && val == 0xB0)
420                                                 *have_frame = TRUE;
421                                 }
422                                 bType = val;
423                         }
424                         break;
425                 }
426         }
427  DONE:
428         *size = result;
429         app->offset += result;
430         *have_frame = TRUE;
431 }
432
433 /**
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
437   **/
438 static const char AMR_header[] = "#!AMR\n";
439 static const char AMRWB_header[] = "#!AMR-WB\n";
440 #define AMR_NB_MIME_HDR_SIZE          6
441 #define AMR_WB_MIME_HDR_SIZE          9
442 static const int block_size_nb[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
443 static const int block_size_wb[16] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
444
445 int *blocksize_tbl;
446 void amrdec_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
447 {
448         int readsize = 0, mode_temp;
449         unsigned int fsize, mode;
450         unsigned char *pAmr = app->data + app->offset;
451         //change the below one to frame count
452         if (app->offset == 0) {
453                 if (!memcmp(pAmr, AMR_header, AMR_NB_MIME_HDR_SIZE)) {
454                         blocksize_tbl = (int *)block_size_nb;
455                         mode_temp = pAmr[AMR_NB_MIME_HDR_SIZE];
456                         pAmr = pAmr + AMR_NB_MIME_HDR_SIZE;
457                         app->offset += AMR_NB_MIME_HDR_SIZE;
458                 } else {
459                         if (!memcmp(pAmr, AMRWB_header, AMR_WB_MIME_HDR_SIZE)) {
460                                 blocksize_tbl = (int *)block_size_wb;
461                                 mode_temp = pAmr[AMR_WB_MIME_HDR_SIZE];
462                                 pAmr = pAmr + AMR_WB_MIME_HDR_SIZE;
463                                 app->offset += AMR_WB_MIME_HDR_SIZE;
464                         } else {
465                                 g_print("[ERROR] AMR-NB/WB don't detected..\n");
466                                 return;
467                         }
468                 }
469         }
470         mode_temp = pAmr[0];
471         if ((mode_temp & 0x83) == 0) {
472                 mode = (mode_temp >> 3) & 0x0F; /* Yep. Retrieve the frame size */
473                 fsize = blocksize_tbl[mode];
474                 readsize = fsize + 1;
475         } else {
476                 readsize = 0;
477                 g_print("[FAIL] Not found amr frame sync.....\n");
478         }
479
480         *size = readsize;
481         app->offset += readsize;
482         *data = pAmr;
483         *have_frame = TRUE;
484 }
485
486 void nv12_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
487 {
488         int yuv_size;
489         int offset = app->length - app->offset;
490
491         yuv_size = app->width * app->height * 3 / 2;
492
493         if (offset >= yuv_size)
494                 *size = offset;
495
496         *have_frame = TRUE;
497         *data = app->data + app->offset;
498
499         if (offset >= yuv_size)
500                 *size = offset;
501         else
502                 *size = yuv_size;
503 }
504
505 void yuv_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
506 {
507         int yuv_size;
508         int offset = app->length - app->offset;
509
510         yuv_size = app->width * app->height * 3 / 2;
511
512         if (yuv_size >= offset)
513                 *size = offset;
514
515         *have_frame = TRUE;
516         *data = app->data + app->offset;
517
518         if (yuv_size >= offset)
519                 *size = offset;
520         else
521                 *size = yuv_size;
522
523         app->offset += *size;
524
525 }
526
527 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
528 {
529         int read_size;
530         int offset = app->length - app->offset;
531
532         read_size = ((samplebyte*app->channel)*(app->bit/8));
533
534
535         *have_frame = TRUE;
536
537         if (offset >= read_size)
538                 *size = offset;
539         else
540                 *size = read_size;
541
542         app->offset += *size;
543 }
544
545 void amrenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
546 {
547         int read_size;
548         int offset = app->length - app->offset;
549
550         if (app->is_amr_nb)
551                 read_size = AMRNB_PCM_INPUT_SIZE;
552         else
553                 read_size = AMRWB_PCM_INPUT_SIZE;
554
555         *have_frame = TRUE;
556
557         if (offset >= read_size)
558                 *size = offset;
559         else
560                 *size = read_size;
561
562         app->offset += *size;
563 }
564
565 /**
566  * Extract Input data for AAC decoder
567  * (case of (LC profile) ADTS format)
568  * codec_data : Don't need
569  **/
570 void aacdec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
571 {
572         int read_size;
573         int offset = app->length - app->offset;
574         unsigned char *pData = app->data + app->offset;
575
576         if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
577                 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
578         } else {
579                 read_size = 0;
580                 g_print("[FAIL] Not found aac frame sync.....\n");
581         }
582
583         *have_frame = TRUE;
584         *data = app->data + app->offset;
585
586         if (read_size >= offset)
587                 *size = offset;
588         else
589                 *size = read_size;
590
591         app->offset += *size;
592
593 }
594
595 void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
596 {
597         int read_size;
598         guint header;
599         guint padding, bitrate, lsf=0, layer = 0, mpg25=0;
600         guint hdr_bitrate=0, sf=0;
601         int offset = app->length - app->offset;
602         unsigned char *pData = app->data + app->offset;
603
604         header = GST_READ_UINT32_BE(pData);
605
606         if (header == 0) {
607                 g_print ("[ERROR] read header size is 0\n");
608                 *have_frame = FALSE;
609         }
610
611         /* if it's not a valid sync */
612         if ((header & 0xffe00000) != 0xffe00000) {
613                 g_print ("[ERROR] invalid sync\n");
614                 *have_frame = FALSE;
615         }
616
617         if (((header >> 19) & 3) == 0x1) {
618                 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
619                 *have_frame = FALSE;
620         } else {
621                 if (header & (1 << 20)) {
622                         lsf = (header & (1 << 19)) ? 0 : 1;
623                         mpg25 = 0;
624                 } else {
625                         lsf = 1;
626                         mpg25 = 1;
627                 }
628         }
629
630         /* if it's an invalid layer */
631         if (!((header >> 17) & 3)) {
632                 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
633                 *have_frame = FALSE;
634         } else {
635                 layer = 4 - ((header >> 17) & 0x3);
636         }
637
638         /* if it's an invalid bitrate */
639         if (((header >> 12) & 0xf) == 0xf) {
640                 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
641                 *have_frame = FALSE;
642         } else {
643                 bitrate = (header >> 12) & 0xF;
644                 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
645                 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
646                 if (hdr_bitrate == 0)
647                         *have_frame = FALSE;
648         }
649
650         /* if it's an invalid samplerate */
651         if (((header >> 10) & 0x3) == 0x3) {
652                 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
653                 *have_frame = FALSE;
654                 return;
655         } else {
656                 sf = (header >> 10) & 0x3;
657                 sf = mp3types_freqs[lsf + mpg25][sf];
658         }
659
660         padding = (header >> 9) & 0x1;
661
662         switch (layer) {
663         case 1:
664                 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
665                 break;
666         case 2:
667                 read_size = (hdr_bitrate * 144) / sf + padding;
668                 break;
669         default:
670         case 3:
671                 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
672                 break;
673         }
674         g_print("header : %d, read : %d\n", header, read_size);
675
676         *have_frame = TRUE;
677         *data = app->data + app->offset;
678
679         if (read_size >= offset)
680                 *size = offset;
681         else
682                 *size = read_size;
683
684         app->offset += *size;
685 }
686
687 #if 1
688 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
689 {
690         int readsize = 0, read_size = 0;
691         unsigned int header_size = ADTS_HEADER_SIZE;
692         unsigned char buffer[100000];
693         unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
694         int offset = app->length - app->offset;
695         unsigned char *pData = app->data + app->offset;
696         /*
697          * It is not support full parsing MP4 container box.
698          * So It MUST start as RAW valid frame sequence.
699          * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
700          */
701
702         //change the below one later
703         if (app->offset == 0) {
704                 /*
705                  * CAUTION : Codec data is needed only once  in first time
706                  * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
707                  * So I use this data (byte) as hard coding for temporary our testing.
708                  */
709 #if 1
710                 /*
711                  * The codec_data data is according to AudioSpecificConfig,
712                  *  ISO/IEC 14496-3, 1.6.2.1
713                  *
714                  *  below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
715                  * case : M4A - LC profile
716                  * codec_data=(buffer)119056e5000000000000000000000000
717                  * savs aac decoder get codec_data. size: 16  (Tag size : 5 byte)
718                  *     - codec data: profile  : 2
719                  *     - codec data: samplrate: 48000
720                  *     - codec data: channels : 2
721                  */
722                 /* 2 bytes are mandatory */
723                 codecdata[0] = 0x11;         /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
724                 codecdata[1] = 0x90;         /* ex) (4bit) 2 (2ch) */
725                 /* othter bytes are (optional) epconfig information */
726                 codecdata[2] = 0x56;
727                 codecdata[3] = 0xE5;
728                 codecdata[4] = 0x00;
729 #else
730                 /*
731                  *  below example is test for using "TestSample-EAAC+.m4a"
732                  *
733                  * case : M4A - HE-AAC v1 and v2 profile
734                  * codec_data=(buffer)138856e5a54880000000000000000000
735                  * savs aac decoder get codec_data. size: 16  (Tag size : 7 byte)
736                  *     - codec data: profile  : 2
737                  *     - codec data: samplrate: 22050
738                  *     - codec data: channels : 1
739                  */
740                 /* 2 bytes are mandatory */
741                 codecdata[0] = 0x13;         /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
742                 codecdata[1] = 0x88;         /* ex) (4bit) 1 (1ch) */
743                 /* othter bytes are (optional) epconfig information */
744                 codecdata[2] = 0x56;
745                 codecdata[3] = 0xE5;
746                 codecdata[4] = 0xA5;
747                 codecdata[5] = 0x48;
748                 codecdata[6] = 0x80;
749 #endif
750
751                 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
752                 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
753                         read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
754                 } else {
755                         read_size = 0;
756                         g_print("[FAIL] Not found aac frame sync.....\n");
757                 }
758                 readsize = read_size - header_size;
759                 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
760                 read_size = readsize + AAC_CODECDATA_SIZE;      //return combination of (codec_data + raw_data)
761                 app->offset += header_size + readsize;
762                 goto DONE;
763         }
764
765         if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
766                 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
767                 readsize = read_size - header_size;
768                 memcpy(buffer, pData + 7, readsize);    //Make only RAW data, so exclude header 7 bytes
769                 read_size = readsize;
770                 app->offset += header_size + readsize;
771
772         } else {
773                 read_size = 0;
774                 g_print("[FAIL] Not found aac frame sync. \n");
775         }
776  DONE:
777         *data = buffer;
778         *have_frame = TRUE;
779         if (read_size >= offset)
780                 *size = offset;
781         else
782                 *size = read_size;
783 }
784 #endif
785
786 /**
787  * Extract Input data for AAC encoder
788  **/
789 /*
790    void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
791    {
792    int read_size;
793    int offset = app->length - app->offset;
794
795    read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
796
797    if (read_size >= offset)
798  *size = offset;
799
800  *have_frame = TRUE;
801  *data = app->data + app->offset;
802
803  if (read_size >= offset)
804  *size = offset;
805  else
806  *size = read_size;
807
808  app->offset += *size;
809  }
810  */
811 #if 0
812 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
813 {
814         if (pkt != NULL) {
815                 g_print("Used input buffer = %p\n", pkt);
816                 media_packet_destroy(pkt);
817         }
818         return;
819 }
820 #endif
821 int  _mediacodec_set_codec(int codecid, int flag, bool *hardware)
822 {
823         bool encoder;
824         media_format_mimetype_e mime = 0;
825         encoder = GET_IS_ENCODER(flag) ? 1 : 0;
826         *hardware = GET_IS_HW(flag) ? 1 : 0;
827
828         switch (codecid) {
829         case MEDIACODEC_H264:
830                 if (encoder) {
831                         extractor = yuv_extractor;
832                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
833                 } else {
834                         extractor = h264_extractor;
835                         mime = MEDIA_FORMAT_H264_SP;
836                 }
837                 break;
838         case MEDIACODEC_MPEG4:
839                 if (encoder) {
840                         extractor = yuv_extractor;
841                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
842                 } else {
843                         extractor = mpeg4_extractor;
844                         mime = MEDIA_FORMAT_MPEG4_SP;
845                 }
846                 break;
847         case MEDIACODEC_H263:
848                 if (encoder) {
849                         extractor = h263_extractor;
850                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
851                 } else {
852                         extractor = h263_extractor;
853                         mime = MEDIA_FORMAT_H263P;
854                 }
855                 break;
856         case MEDIACODEC_AAC:
857                 if (encoder) {
858                         extractor = aacenc_extractor;
859                         mime = MEDIA_FORMAT_PCM;
860                 } else {
861                         extractor = aacdec_extractor;
862                         mime = MEDIA_FORMAT_AAC;
863                 }
864                 break;
865         case MEDIACODEC_AAC_HE:
866                 if (encoder) {
867                         extractor = aacenc_extractor;
868                         mime = MEDIA_FORMAT_PCM;
869                 } else {
870                         extractor = extract_input_aacdec_m4a_test;
871                         mime = MEDIA_FORMAT_AAC_HE;
872                 }
873                 break;
874         case MEDIACODEC_AAC_HE_PS:
875                 break;
876         case MEDIACODEC_MP3:
877                 extractor = mp3dec_extractor;
878                 mime = MEDIA_FORMAT_MP3;
879                 break;
880         case MEDIACODEC_VORBIS:
881                 break;
882         case MEDIACODEC_FLAC:
883                 break;
884         case MEDIACODEC_WMAV1:
885                 break;
886         case MEDIACODEC_WMAV2:
887                 break;
888         case MEDIACODEC_WMAPRO:
889                 break;
890         case MEDIACODEC_WMALSL:
891                 break;
892         case MEDIACODEC_AMR_NB:
893                 extractor = amrdec_extractor;
894                 mime = MEDIA_FORMAT_AMR_NB;
895                 break;
896         case MEDIACODEC_AMR_WB:
897                 extractor = amrdec_extractor;
898                 mime = MEDIA_FORMAT_AMR_WB;
899                 break;
900         default:
901                 LOGE("NOT SUPPORTED!!!!");
902                 break;
903         }
904         return mime;
905 }
906
907 static gboolean read_data(App *app)
908 {
909         guint len = 0;
910         bool have_frame = FALSE;
911         int ret;
912         static guint64 pts = 0L;
913         void *buf_data_ptr = NULL;
914         media_packet_h pkt = NULL;
915         unsigned char *tmp;
916         int read;
917         int offset;
918         int stride_width, stride_height;
919
920         g_print("----------read data------------\n");
921         extractor(app, &tmp, &read, &have_frame);
922
923         if (app->offset >= app->length - 1) {
924                 /* EOS */
925                 g_print("EOS\n");
926                 app->finish = clock();
927                 g_main_loop_quit(app->loop);
928                 return FALSE;
929         }
930         g_print("length : %d, offset : %d\n", (int)app->length, (int)app->offset);
931
932         if (app->offset + len > app->length)
933                 len = app->length - app->offset;
934
935         g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
936
937         if (have_frame) {
938                 if (media_packet_create_alloc(vdec_fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
939                         fprintf(stderr, "media_packet_create_alloc failed\n");
940                         return FALSE;
941                 }
942
943                 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
944                         fprintf(stderr, "media_packet_set_pts failed\n");
945                         return FALSE;
946                 }
947
948                 if (app->type != VIDEO_ENC) {
949                         media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
950                         media_packet_set_buffer_size(pkt, (uint64_t)read);
951
952                         memcpy(buf_data_ptr, tmp, read);
953                         g_print("tmp:%p, read:%d\n", tmp, read);
954                 } else {
955                         /* Y */
956                         media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
957                         media_packet_get_video_stride_width(pkt, 0, &stride_width);
958                         media_packet_get_video_stride_height(pkt, 0, &stride_height);
959
960                         offset = stride_width*stride_height;
961
962                         memcpy(buf_data_ptr, tmp, offset);
963
964                         /* UV or U*/
965                         media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
966                         media_packet_get_video_stride_width(pkt, 1, &stride_width);
967                         media_packet_get_video_stride_height(pkt, 1, &stride_height);
968                         memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
969
970                         if (app->hardware == FALSE) {
971                                 /* V */
972                                 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
973                                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
974                                 media_packet_get_video_stride_height(pkt, 2, &stride_height);
975
976                                 offset += stride_width * stride_height;
977
978
979                                 memcpy(buf_data_ptr, tmp + offset, stride_width*stride_height);
980                         }
981                 }
982                 mc_hex_dump("inbuf", tmp, 48);
983
984                 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
985                 if (ret != MEDIACODEC_ERROR_NONE)
986                         return FALSE;
987
988                 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
989         }
990
991         return TRUE;
992 }
993
994 static void start_feed(App *app)
995 {
996         if (app->sourceid == 0) {
997                 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
998                 g_print("start_feed\n");
999         }
1000 }
1001
1002 static void stop_feed(App *app)
1003 {
1004         if (app->sourceid != 0) {
1005                 g_source_remove(app->sourceid);
1006                 app->sourceid = 0;
1007                 g_print("stop_feed\n");
1008         }
1009 }
1010
1011 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1012 {
1013         g_print("_mediacodec_inbuf_used_cb!!!\n");
1014         media_packet_destroy(pkt);
1015         return TRUE;
1016 }
1017
1018 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1019 {
1020         media_packet_h out_pkt = NULL;
1021         int ret;
1022
1023         App *app = (App*)user_data;
1024
1025         g_print("_mediacodec_outbuf_available_cb\n");
1026
1027         g_mutex_lock(&app->lock);
1028
1029         ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1030
1031         if (ret != MEDIACODEC_ERROR_NONE)
1032                 g_print("get_output failed\n");
1033
1034 #if DUMP_OUTBUF
1035         void *data;
1036         int buf_size;
1037         int stride_width, stride_height;
1038
1039         decoder_output_dump(app, out_pkt);
1040         media_packet_get_buffer_data_ptr(out_pkt, &data);
1041         media_packet_get_buffer_size(out_pkt, &buf_size);
1042         g_print("output data : %p, size %d\n", data, (int)buf_size);
1043
1044         fwrite(data, 1, buf_size, fp_out);
1045
1046 #endif
1047
1048         app->frame_count++;
1049
1050
1051         g_mutex_unlock(&app->lock);
1052
1053         media_packet_destroy(out_pkt);
1054         out_pkt = NULL;
1055         g_print("done\n");
1056
1057         return TRUE;
1058 }
1059
1060 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1061 {
1062         g_print("_mediacodec_buffer_status_cb %d\n", status);
1063
1064         App *app = (App*)user_data;
1065
1066         if (status == MEDIACODEC_NEED_DATA)
1067                 start_feed(app);
1068         else if (status == MEDIACODEC_ENOUGH_DATA)
1069                 stop_feed(app);
1070
1071         return TRUE;
1072 }
1073
1074 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1075 {
1076         return TRUE;
1077 }
1078
1079 static bool _mediacodec_eos_cb(void *user_data)
1080 {
1081         return TRUE;
1082 }
1083
1084 static void _mediacodec_prepare(App *app)
1085 {
1086         int ret;
1087
1088 #if DUMP_OUTBUF
1089         fp_out = fopen("/tmp/codec_dump.out", "wb");
1090 #endif
1091         /* create instance */
1092         ret = mediacodec_create(&app->mc_handle[0]);
1093         if (ret  != MEDIACODEC_ERROR_NONE) {
1094                 g_print("mediacodec_create  failed\n");
1095                 return;
1096         }
1097
1098         /* set codec */
1099         ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1100         if (ret  != MEDIACODEC_ERROR_NONE) {
1101                 g_print("mediacodec_set_codec failed\n");
1102                 return;
1103         }
1104
1105
1106         app->mime = _mediacodec_set_codec(app->codecid, app->flag, &app->hardware);
1107
1108         /* set codec info */
1109         ret = media_format_create(&vdec_fmt);
1110
1111         switch (app->type) {
1112         case VIDEO_DEC:
1113                 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1114                 media_format_set_video_mime(vdec_fmt, app->mime);
1115                 media_format_set_video_width(vdec_fmt, app->width);
1116                 media_format_set_video_height(vdec_fmt, app->height);
1117                 break;
1118         case VIDEO_ENC:
1119                 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1120                 media_format_set_video_mime(vdec_fmt, app->mime);
1121                 media_format_set_video_width(vdec_fmt, app->width);
1122                 media_format_set_video_height(vdec_fmt, app->height);
1123                 media_format_set_video_avg_bps(vdec_fmt, app->target_bits);
1124                 break;
1125         case AUDIO_DEC:
1126                 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1127                 media_format_set_audio_mime(vdec_fmt, app->mime);
1128                 media_format_set_audio_channel(vdec_fmt, app->channel);
1129                 media_format_set_audio_samplerate(vdec_fmt, app->samplerate);
1130                 media_format_set_audio_bit(vdec_fmt, app->bit);
1131                 break;
1132         case AUDIO_ENC:
1133                 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1134                 media_format_set_audio_mime(vdec_fmt, app->mime);
1135                 media_format_set_audio_channel(vdec_fmt, app->channel);
1136                 media_format_set_audio_samplerate(vdec_fmt, app->samplerate);
1137                 media_format_set_audio_bit(vdec_fmt, app->bit);
1138                 break;
1139         default:
1140                 g_print("invaild type\n");
1141                 break;
1142         }
1143
1144         if (ret  != MEDIACODEC_ERROR_NONE) {
1145                 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1146                 return;
1147         }
1148
1149         /* set callback */
1150         mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1151         mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1152         mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1153         mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1154         mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1155
1156         /* prepare */
1157         ret = mediacodec_prepare(app->mc_handle[0]);
1158         if (ret  != MEDIACODEC_ERROR_NONE) {
1159                 g_print("mediacodec_prepare failed\n");
1160                 return;
1161         }
1162
1163         app->frame_count = 0;
1164         app->start = clock();
1165         g_main_loop_run(app->loop);
1166
1167         g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1168
1169         g_print("---------------------------\n");
1170
1171         return;
1172 }
1173
1174 static void input_filepath(char *filename, App *app)
1175 {
1176         GError *error = NULL;
1177
1178         app->obj++;
1179         app->file = g_mapped_file_new(filename, FALSE, &error);
1180         if (error) {
1181                 g_print("failed to open file : %s\n", error->message);
1182                 g_error_free(error);
1183                 return;
1184         }
1185
1186         app->length = g_mapped_file_get_length(app->file);
1187         app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1188         app->offset = 0;
1189         g_print("len : %d, offset : %d, obj : %d", app->length, (int)app->offset, app->obj);
1190
1191         return;
1192 }
1193
1194 void quit_program()
1195 {
1196 #if DUMP_OUTBUF
1197         if (fp_out)
1198                 fclose(fp_out);
1199 #endif
1200                 elm_exit();
1201
1202 }
1203
1204 void reset_menu_state()
1205 {
1206         g_menu_state = CURRENT_STATUS_MAINMENU;
1207         return;
1208 }
1209
1210 void _interpret_main_menu(char *cmd, App *app)
1211 {
1212         int len =  strlen(cmd);
1213         if (len == 1) {
1214                 if (strncmp(cmd, "a", 1) == 0)
1215                         g_menu_state = CURRENT_STATUS_FILENAME;
1216                 else if (strncmp(cmd, "o", 1) == 0)
1217                         g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1218                 else if (strncmp(cmd, "q", 1) == 0)
1219                         quit_program();
1220                 else
1221                         g_print("unknown menu \n");
1222         } else if (len == 2) {
1223                 if (strncmp(cmd, "pr", 2) == 0)
1224                         _mediacodec_prepare(app);
1225                 else if (strncmp(cmd, "sc", 2) == 0)
1226                         g_menu_state = CURRENT_STATUS_SET_CODEC;
1227                 else if (strncmp(cmd, "vd", 2) == 0)
1228                         g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1229                 else if (strncmp(cmd, "ve", 2) == 0)
1230                         g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1231                 else if (strncmp(cmd, "ad", 2) == 0)
1232                         g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1233                 else if (strncmp(cmd, "ae", 2) == 0)
1234                         g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1235                 else if (strncmp(cmd, "pi", 2) == 0)
1236                         g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1237                 else
1238                         display_sub_basic();
1239         } else {
1240                 g_print("unknown menu \n");
1241         }
1242
1243         return;
1244 }
1245
1246 static void displaymenu(void)
1247 {
1248         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1249                 display_sub_basic();
1250         } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1251                 g_print("*** input mediapath.\n");
1252         } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1253                 g_print("*** Codec id : Select Codec ID Numbe  (e.g. AAC_LC = 96)\n");
1254                 g_print("               L16    =  16 (0x10)\n");
1255                 g_print("               ALAW   =  32 (0x20)\n");
1256                 g_print("               ULAW   =  48 (0x30)\n");
1257                 g_print("               AMR_NB =  64 (0x40)\n");
1258                 g_print("               AMR_WB =  65 (0x41)\n");
1259                 g_print("               G729   =  80 (0x50)\n");
1260                 g_print("               AAC_LC =  96 (0x60)\n");
1261                 g_print("               AAC_HE =  97 (0x61)\n");
1262                 g_print("               AAC_PS =  98 (0x62)\n");
1263                 g_print("               MP3    = 112 (0x70)\n");
1264                 g_print("               VORBIS = 128 (0x80)\n");
1265                 g_print("               FLAC   = 144 (0x90)\n");
1266                 g_print("               WMAV1  = 160 (0xA0)\n");
1267                 g_print("               WMAV2  = 161 (0xA1)\n");
1268                 g_print("               WMAPRO = 162 (0xA2)\n");
1269                 g_print("               WMALSL = 163 (0xA3)\n");
1270                 g_print("               -------------------\n");
1271                 g_print("               H261   = 101\n");
1272                 g_print("               H263   = 102\n");
1273                 g_print("               H264   = 103\n");
1274                 g_print("               MJPEG  = 104\n");
1275                 g_print("               MPEG1  = 105\n");
1276                 g_print("               MPEG2  = 106\n");
1277                 g_print("               MPEG4  = 107\n");
1278                 g_print("               -------------------\n");
1279                 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1280                 g_print("               CODEC : ENCODER =  1       DECODER =  2\n");
1281                 g_print("               TYPE  : HW      =  4       SW      =  8\n");
1282                 g_print("*** input codec id, falgs.\n");
1283         } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1284                 g_print("*** input video decode configure.(width, height)\n");
1285         } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1286                 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1287         } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1288                 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000,  2, 16))\n");
1289         } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1290                 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000,  2, 16, 128000))\n");
1291         } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1292                 g_print("*** input dec process number\n");
1293         } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1294                 g_print("*** input get output buffer number\n");
1295         } else {
1296                 g_print("*** unknown status.\n");
1297         }
1298         g_print(" >>> ");
1299 }
1300
1301 gboolean timeout_menu_display(void* data)
1302 {
1303         displaymenu();
1304         return FALSE;
1305 }
1306
1307
1308 static void interpret(char *cmd, App *app)
1309 {
1310         switch (g_menu_state) {
1311         case CURRENT_STATUS_MAINMENU:
1312                 _interpret_main_menu(cmd, app);
1313                 break;
1314         case CURRENT_STATUS_FILENAME:
1315                 input_filepath(cmd, app);
1316                 reset_menu_state();
1317                 break;
1318         case CURRENT_STATUS_SET_CODEC:
1319         {
1320                 int tmp;
1321                 static int cnt = 0;
1322                 char **ptr = NULL;
1323                 switch (cnt) {
1324                 case 0:
1325                         tmp = atoi(cmd);
1326
1327                         if (tmp > 100 &&
1328                                 (tmp != 112) &&
1329                                 (tmp != 128) &&
1330                                 (tmp != 144) &&
1331                                 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1332                                         tmp = strtol(cmd, ptr, 16);
1333                                         app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1334                         } else
1335                                 app->codecid = 0x1000 + tmp;
1336
1337                         cnt++;
1338                         break;
1339                 case 1:
1340                         app->flag = atoi(cmd);
1341                         cnt = 0;
1342                         reset_menu_state();
1343                         break;
1344                 default:
1345                         break;
1346                 }
1347         }
1348         break;
1349         case CURRENT_STATUS_SET_VDEC_INFO:
1350         {
1351                 static int cnt = 0;
1352                 switch (cnt) {
1353                 case 0:
1354                         app->width = atoi(cmd);
1355                         cnt++;
1356                         break;
1357                 case 1:
1358                         app->height = atoi(cmd);
1359                         app->type = VIDEO_DEC;
1360
1361                         reset_menu_state();
1362                         cnt = 0;
1363                         break;
1364                 default:
1365                         break;
1366                 }
1367         }
1368         break;
1369         case CURRENT_STATUS_SET_VENC_INFO:
1370         {
1371                 static int cnt = 0;
1372                 switch (cnt) {
1373                 case 0:
1374                         app->width = atoi(cmd);
1375                         cnt++;
1376                         break;
1377                 case 1:
1378                         app->height = atoi(cmd);
1379                         cnt++;
1380                         break;
1381                 case 2:
1382                         app->fps = atol(cmd);
1383                         cnt++;
1384                         break;
1385                 case 3:
1386                         app->target_bits = atoi(cmd);
1387                         app->type = VIDEO_ENC;
1388
1389                         reset_menu_state();
1390                         cnt = 0;
1391                         break;
1392                 default:
1393                         break;
1394                 }
1395         }
1396         break;
1397         case CURRENT_STATUS_SET_ADEC_INFO:
1398         {
1399                 static int cnt = 0;
1400                 switch (cnt) {
1401                 case 0:
1402                         app->samplerate = atoi(cmd);
1403                         cnt++;
1404                         break;
1405                 case 1:
1406                         app->channel = atoi(cmd);
1407                         cnt++;
1408                         break;
1409                 case 2:
1410                         app->bit = atoi(cmd);
1411                         app->type = AUDIO_DEC;
1412
1413                         reset_menu_state();
1414                         cnt = 0;
1415                         break;
1416                 default:
1417                         break;
1418                 }
1419         }
1420         break;
1421         case CURRENT_STATUS_SET_AENC_INFO:
1422         {
1423                 static int cnt = 0;
1424                 switch (cnt) {
1425                 case 0:
1426                         app->samplerate = atoi(cmd);
1427                         cnt++;
1428                         break;
1429                 case 1:
1430                         app->channel = atoi(cmd);
1431                         cnt++;
1432                         break;
1433                 case 2:
1434                         app->bit = atoi(cmd);
1435                         cnt++;
1436                         break;
1437                 case 3:
1438                         app->bitrate = atoi(cmd);
1439                         app->type = AUDIO_ENC;
1440
1441                         reset_menu_state();
1442                         cnt = 0;
1443                         break;
1444                 default:
1445                         break;
1446                 }
1447         }
1448         break;
1449         case CURRENT_STATUS_PROCESS_INPUT:
1450         {
1451                 reset_menu_state();
1452         }
1453         break;
1454         case CURRENT_STATUS_GET_OUTPUT:
1455         {
1456                 reset_menu_state();
1457         }
1458         break;
1459         default:
1460                 break;
1461         }
1462
1463         g_timeout_add(100, timeout_menu_display, 0);
1464 }
1465
1466 static void display_sub_basic()
1467 {
1468         g_print("\n");
1469         g_print("=========================================================================================\n");
1470         g_print("                                    media codec test\n");
1471         g_print("-----------------------------------------------------------------------------------------\n");
1472         g_print("a. Create \t\t");
1473         g_print("sc. Set codec \n");
1474         g_print("vd. Set vdec info \t");
1475         g_print("ve. Set venc info \n");
1476         g_print("ad. Set adec info \t");
1477         g_print("ae. Set aenc info \n");
1478         g_print("pr. Prepare \t\t");
1479         g_print("pi. Process input \n");
1480         g_print("o. Get output \t\t");
1481         g_print("rb. Reset output buffer \n");
1482         g_print("pa. Process all frames \n");
1483         g_print("un. Unprepare \t\t");
1484         g_print("dt. Destroy \t\t");
1485         g_print("q. quite test suite \t");
1486         g_print("\n");
1487         g_print("=========================================================================================\n");
1488 }
1489
1490 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1491 {
1492         gchar buf[MAX_STRING_LEN];
1493         gsize read;
1494         GError *error = NULL;
1495         App *context = (App*)data;
1496
1497         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1498         buf[read] = '\0';
1499         g_strstrip(buf);
1500         interpret(buf, context);
1501
1502         return TRUE;
1503 }
1504
1505 int main(int argc, char *argv[])
1506 {
1507         App *app = &s_app;
1508
1509         GIOChannel *stdin_channel;
1510         stdin_channel = g_io_channel_unix_new(0);
1511         g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1512         g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1513
1514
1515         app->loop = g_main_loop_new(NULL, TRUE);
1516         app->timer = g_timer_new();
1517
1518         displaymenu();
1519
1520         ops.data = app;
1521
1522         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1523 }
1524
1525
1526
1527 void mc_hex_dump(char *desc, void *addr, int len)
1528 {
1529         int i;
1530         unsigned char buff[17];
1531         unsigned char *pc = (unsigned char *)addr;
1532
1533         if (desc != NULL)
1534                 printf("%s:\n", desc);
1535
1536         for (i = 0; i < len; i++) {
1537
1538                 if ((i % 16) == 0) {
1539                         if (i != 0)
1540                                 printf("  %s\n", buff);
1541
1542                         printf("  %04x ", i);
1543                 }
1544
1545                 printf(" %02x", pc[i]);
1546
1547                 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1548                         buff[i % 16] = '.';
1549                 else
1550                         buff[i % 16] = pc[i];
1551                 buff[(i % 16) + 1] = '\0';
1552         }
1553
1554         while ((i % 16) != 0) {
1555                 printf("   ");
1556                 i++;
1557         }
1558         printf("  %s\n", buff);
1559 }
1560
1561 #if DUMP_OUTBUF
1562 static void decoder_output_dump(App *app, media_packet_h pkt)
1563 {
1564         void *temp;
1565         int i = 0;
1566         int stride_width, stride_height;
1567         char filename[100] = {0};
1568         FILE *fp = NULL;
1569         int ret = 0;
1570
1571         sprintf(filename, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1572         fp = fopen(filename, "ab");
1573
1574         media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1575         media_packet_get_video_stride_width(pkt, 0, &stride_width);
1576         media_packet_get_video_stride_height(pkt, 0, &stride_height);
1577         printf("stride : %d, %d\n", stride_width, stride_height);
1578
1579         for (i = 0; i < app->height; i++) {
1580                 ret = fwrite(temp, app->width, 1, fp);
1581                 temp += stride_width;
1582         }
1583
1584         if (app->hardware == TRUE) {
1585                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1586                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1587                 for (i = 0; i < app->height/2; i++) {
1588                         ret = fwrite(temp, app->width, 1, fp);
1589                         temp += stride_width;
1590                 }
1591         } else {
1592                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1593                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1594                 for (i = 0; i < app->height/2; i++) {
1595                         ret = fwrite(temp, app->width/2, 1, fp);
1596                         temp += stride_width;
1597                 }
1598
1599                 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1600                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1601                 for (i = 0; i < app->height/2; i++) {
1602                         ret = fwrite(temp, app->width/2, 1, fp);
1603                         temp += stride_width;
1604                 }
1605         }
1606
1607         g_print("codec dec output dumped!!%d\n", ret);
1608         fclose(fp);
1609
1610 }
1611 #endif