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