67f0f68d2630339eea343005ad8eae8307907cbc
[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 <tbm_surface.h>
25 #include <dlog.h>
26 #include <time.h>
27 #include <camera.h>
28 #include <media_codec.h>
29 #include <media_packet.h>
30 #include <media_packet_pool.h>
31 #include <media_codec_internal.h>
32
33 #define PACKAGE "media_codec_test"
34 #define MAX_HANDLE                      4
35 #if 0
36 #define DUMP_OUTBUF           1
37 #endif
38 #define TEST_FILE_SIZE        (10 * 1024 * 1024)
39 #define MAX_STRING_LEN        256
40
41 #define DEFAULT_SAMPPLERATE   44100
42 #define DEFAULT_CHANNEL             2
43 #define DEFAULT_BIT                           16
44 #define DEFAULT_BITRATE       128
45 #define DEFAULT_SAMPLEBYTE        1024
46 #define ADTS_HEADER_SIZE      7
47 #define AMRNB_PCM_INPUT_SIZE      320
48 #define AMRWB_PCM_INPUT_SIZE      640
49
50 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
51 #define GET_IS_ENCODER(x) CHECK_BIT(x, 0)
52 #define GET_IS_DECODER(x) CHECK_BIT(x, 1)
53 #define GET_IS_HW(x) CHECK_BIT(x, 2)
54 #define ES_DEFAULT_VIDEO_PTS_OFFSET 33000000
55 #define CHECK_VALID_PACKET(state, expected_state) \
56         ((state & (expected_state)) == (expected_state))
57
58 #define AAC_CODECDATA_SIZE    16
59 #define USE_POOL        1
60
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         gint length;
124         gint offset;
125         gint 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;
136         bool is_encoder;
137         bool hardware;
138         bool enable_dump;
139         int frame;
140         type_e type;
141         camera_h camera_handle;
142         /* video */
143         mediacodec_h mc_handle[MAX_HANDLE];
144         guint width;
145         guint height;
146         guint fps;
147         guint target_bits;
148         media_format_mimetype_e mime;
149
150         /* Audio */
151         guint samplerate;
152         guint channel;
153         guint bit;
154         guint bitrate;
155         bool is_amr_nb;
156
157
158         /* Render */
159         guint w;
160         guint h;
161         Evas_Object *win;
162         Evas_Object *img;
163         media_packet_h packet;
164         Ecore_Pipe *pipe;
165         GList *packet_list;
166         GMutex lock;
167 };
168
169 App s_app;
170
171 media_format_h fmt = NULL;
172 media_packet_pool_h pkt_pool = NULL;
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 static void _mediacodec_unprepare(App *app);
181 /* For debugging */
182 static void mc_hex_dump(char *desc, void *addr, int len);
183 static void decoder_output_dump(App *app, media_packet_h pkt);
184 static void output_dump(App *app, media_packet_h pkt);
185 /* */
186 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id);
187
188 void (*extractor)(App *app, unsigned char** data, int *size, bool *have_frame);
189
190 int g_menu_state = CURRENT_STATUS_MAINMENU;
191
192 static int _create_app(void *data)
193 {
194         g_print("My app is going alive!\n");
195         App *app = (App*)data;
196
197         g_mutex_init(&app->lock);
198         return 0;
199 }
200
201 static int _terminate_app(void *data)
202 {
203         g_print("My app is going gone!\n");
204         App *app = (App*)data;
205
206         g_mutex_clear(&app->lock);
207         return 0;
208 }
209
210
211 struct appcore_ops ops = {
212         .create = _create_app,
213         .terminate = _terminate_app,
214 };
215
216 static const guint mp3types_bitrates[2][3][16] = {
217         {
218                 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
219                 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
220                 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
221         },
222         {
223                 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
224                 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
225                 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
226         },
227 };
228
229 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
230         {22050, 24000, 16000},
231         {11025, 12000, 8000}
232 };
233
234 void h264_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
235 {
236         unsigned char val, zero_count;
237         unsigned char *pNal = app->data + app->offset;
238         int max = app->length - app->offset;
239         int index = 0;
240         int nal_unit_type = 0;
241         bool init;
242         bool slice;
243         bool idr;
244         static int state;
245         int read;
246
247         zero_count = 0;
248
249         val = pNal[index++];
250         while (!val) {
251                 zero_count++;
252                 val = pNal[index++];
253         }
254
255         zero_count = 0;
256
257         while (1) {
258                 if (index >= max) {
259                         read = (index - 1);
260                         goto DONE;
261                 }
262
263                 val = pNal[index++];
264
265                 if (!val)
266                         zero_count++;
267                 else {
268                         if ((zero_count >= 2) && (val == 1))
269                                 break;
270                         else
271                                 zero_count = 0;
272                 }
273         }
274
275         if (zero_count > 3)
276                 zero_count = 3;
277
278         read = (index - zero_count - 1);
279
280         nal_unit_type = *(app->data+app->offset+4) & 0x1F;
281         g_print("nal_unit_type : %x\n", nal_unit_type);
282
283         switch (nal_unit_type) {
284         case NAL_SEQUENCE_PARAMETER_SET:
285                 g_print("nal_unit_type : SPS\n");
286                 state |= MC_EXIST_SPS;
287                 break;
288         case NAL_PICTURE_PARAMETER_SET:
289                 g_print("nal_unit_type : PPS\n");
290                 state |= MC_EXIST_PPS;
291                 break;
292         case NAL_SLICE_IDR:
293         case NAL_SEI:
294                 g_print ("nal_unit_type : IDR\n");
295                 state |= MC_EXIST_IDR;
296                 break;
297         case NAL_SLICE_NO_PARTITIONING:
298         case NAL_SLICE_PART_A:
299         case NAL_SLICE_PART_B:
300         case NAL_SLICE_PART_C:
301                 state |= MC_EXIST_SLICE;
302                 break;
303         default:
304                 g_print ("nal_unit_type : %x", nal_unit_type);
305                 break;
306         }
307
308         init = CHECK_VALID_PACKET(state, MC_VALID_FIRST_SLICE) ? 1 : 0;
309         slice = CHECK_VALID_PACKET(state, MC_EXIST_SLICE) ? 1 : 0;
310         idr = CHECK_VALID_PACKET(state, MC_EXIST_IDR) ? 1 : 0;
311         g_print("status : %d, slice : %d, idr : %d\n", init, slice, idr);
312
313         if (init || idr || slice) {
314                 *have_frame = TRUE;
315                 if (init) {
316                         *data = app->data;
317                         *size = app->offset + read;
318                 } else {
319                         *data = app->data+app->offset;
320                         *size = read;
321                 }
322                 state = 0;
323         } else {
324                 *data = app->data+app->offset;
325                 *size = read;
326         }
327 DONE:
328         app->offset += read;
329 }
330
331 void h263_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
332 {
333         int len = 0;
334         int read_size = 1, state = 1, bStart = 0;
335         unsigned char val;
336         unsigned char *pH263 = app->data + app->offset;
337         *data = pH263;
338         int max = app->length - app->offset;
339
340         while (1) {
341                 if (len >= max) {
342                         read_size = (len - 1);
343                         goto DONE;
344                 }
345                 val = pH263[len++];
346                 switch (state) {
347                 case 1:
348                         if (val == 0x00)
349                                 state++;
350                         break;
351                 case 2:
352                         if (val == 0x00)
353                                 state++;
354                         else
355                                 state = 1;
356                         break;
357                 case 3:
358                         state = 1;
359                         if ((val & 0xFC) == 0x80) {
360                                 if (bStart) {
361                                         read_size = len - 3;
362                                         goto DONE;
363                                 } else {
364                                         bStart = 1;
365                                 }
366                         }
367                         break;
368                 }
369         }
370  DONE:
371         *size = read_size;
372         app->offset += read_size;
373         *have_frame = TRUE;
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                         result = (len - 1);
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 (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 int write_amr_header = 1;                   /* write  magic number for AMR Header at one time */
439 static const char AMR_header[] = "#!AMR\n";
440 static const char AMRWB_header[] = "#!AMR-WB\n";
441 #define AMR_NB_MIME_HDR_SIZE          6
442 #define AMR_WB_MIME_HDR_SIZE          9
443 static const int block_size_nb[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
444 static const int block_size_wb[16] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
445
446 int *blocksize_tbl;
447 void amrdec_extractor(App * app, unsigned char **data, int *size, bool * have_frame)
448 {
449         int readsize = 0, mode_temp;
450         unsigned int fsize, mode;
451         unsigned char *pAmr = app->data + app->offset;
452         /* change the below one to frame count */
453         if (app->offset == 0) {
454                 if (!memcmp(pAmr, AMR_header, AMR_NB_MIME_HDR_SIZE)) {
455                         blocksize_tbl = (int *)block_size_nb;
456                         mode_temp = pAmr[AMR_NB_MIME_HDR_SIZE];
457                         pAmr = pAmr + AMR_NB_MIME_HDR_SIZE;
458                         app->offset += AMR_NB_MIME_HDR_SIZE;
459                 } else {
460                         if (!memcmp(pAmr, AMRWB_header, AMR_WB_MIME_HDR_SIZE)) {
461                                 blocksize_tbl = (int *)block_size_wb;
462                                 mode_temp = pAmr[AMR_WB_MIME_HDR_SIZE];
463                                 pAmr = pAmr + AMR_WB_MIME_HDR_SIZE;
464                                 app->offset += AMR_WB_MIME_HDR_SIZE;
465                         } else {
466                                 g_print("[ERROR] AMR-NB/WB don't detected..\n");
467                                 return;
468                         }
469                 }
470         }
471         mode_temp = pAmr[0];
472         if ((mode_temp & 0x83) == 0) {
473                 mode = (mode_temp >> 3) & 0x0F; /* Yep. Retrieve the frame size */
474                 fsize = blocksize_tbl[mode];
475                 readsize = fsize + 1;
476         } else {
477                 readsize = 0;
478                 g_print("[FAIL] Not found amr frame sync.....\n");
479         }
480
481         *size = readsize;
482         app->offset += readsize;
483         *data = pAmr;
484         *have_frame = TRUE;
485 }
486
487 void nv12_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
488 {
489         int yuv_size;
490         int offset = app->length - app->offset;
491
492         yuv_size = app->width * app->height * 3 / 2;
493
494         if (offset >= yuv_size)
495                 *size = offset;
496
497         *have_frame = TRUE;
498         *data = app->data + app->offset;
499
500         if (offset >= yuv_size)
501                 *size = offset;
502         else
503                 *size = yuv_size;
504 }
505
506 void yuv_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
507 {
508         int yuv_size;
509         int offset = app->length - app->offset;
510
511         yuv_size = app->width * app->height * 3 / 2;
512
513         if (yuv_size >= offset)
514                 *size = offset;
515
516         *have_frame = TRUE;
517         *data = app->data + app->offset;
518
519         if (yuv_size >= offset)
520                 *size = offset;
521         else
522                 *size = yuv_size;
523
524         app->offset += *size;
525
526 }
527
528 void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
529 {
530         int read_size;
531         int offset = app->length - app->offset;
532
533         read_size = ((DEFAULT_SAMPLEBYTE * app->channel)*(app->bit/8) * 2);
534
535         *have_frame = TRUE;
536         *data = app->data + app->offset;
537
538         if (read_size >= offset)
539                 *size = offset;
540         else
541                 *size = read_size;
542
543         app->offset += *size;
544 }
545
546 void amrenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
547 {
548         int read_size;
549         int offset = app->length - app->offset;
550
551         if (app->is_amr_nb)
552                 read_size = AMRNB_PCM_INPUT_SIZE;
553         else
554                 read_size = AMRWB_PCM_INPUT_SIZE;
555
556         *have_frame = TRUE;
557         *data = app->data + app->offset;
558
559         if (read_size >= offset)
560                 *size = offset;
561         else
562                 *size = read_size;
563
564         app->offset += *size;
565 }
566
567 /**
568  * Extract Input data for AAC decoder
569  * (case of (LC profile) ADTS format)
570  * codec_data : Don't need
571  **/
572 void aacdec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
573 {
574         int read_size;
575         int offset = app->length - app->offset;
576         unsigned char *pData = app->data + app->offset;
577
578         if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
579                 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
580         } else {
581                 read_size = 0;
582                 g_print("[FAIL] Not found aac frame sync.....\n");
583         }
584
585         *have_frame = TRUE;
586         *data = app->data + app->offset;
587
588         if (read_size >= offset)
589                 *size = offset;
590         else
591                 *size = read_size;
592
593         app->offset += *size;
594
595 }
596
597 void mp3dec_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
598 {
599         int read_size;
600         guint header;
601         guint padding, bitrate, lsf = 0, layer = 0, mpg25 = 0;
602         guint hdr_bitrate = 0, sf = 0;
603         int offset = app->length - app->offset;
604         unsigned char *pData = app->data + app->offset;
605
606         header = GST_READ_UINT32_BE(pData);
607
608         if (header == 0) {
609                 g_print ("[ERROR] read header size is 0\n");
610                 *have_frame = FALSE;
611                 return;
612         }
613
614         /* if it's not a valid sync */
615         if ((header & 0xffe00000) != 0xffe00000) {
616                 g_print ("[ERROR] invalid sync\n");
617                 *have_frame = FALSE;
618                 return;
619         }
620
621         if (((header >> 19) & 3) == 0x1) {
622                 g_print ("[ERROR] invalid MPEG version: %d\n", (header >> 19) & 3);
623                 *have_frame = FALSE;
624                 return;
625         } else {
626                 if (header & (1 << 20)) {
627                         lsf = (header & (1 << 19)) ? 0 : 1;
628                         mpg25 = 0;
629                 } else {
630                         lsf = 1;
631                         mpg25 = 1;
632                 }
633         }
634
635         /* if it's an invalid layer */
636         if (!((header >> 17) & 3)) {
637                 g_print("[ERROR] invalid layer: %d\n", (header >> 17) & 3);
638                 *have_frame = FALSE;
639                 return;
640         } else {
641                 layer = 4 - ((header >> 17) & 0x3);
642         }
643
644         /* if it's an invalid bitrate */
645         if (((header >> 12) & 0xf) == 0xf) {
646                 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
647                 *have_frame = FALSE;
648                 return;
649         } else {
650                 bitrate = (header >> 12) & 0xF;
651                 hdr_bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
652                 /* The caller has ensured we have a valid header, so bitrate can't be zero here. */
653                 if (hdr_bitrate == 0) {
654                         *have_frame = FALSE;
655                         return;
656                 }
657         }
658
659         /* if it's an invalid samplerate */
660         if (((header >> 10) & 0x3) == 0x3) {
661                 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
662                 *have_frame = FALSE;
663                 return;
664         } else {
665                 sf = (header >> 10) & 0x3;
666                 sf = mp3types_freqs[lsf + mpg25][sf];
667         }
668
669         padding = (header >> 9) & 0x1;
670
671         switch (layer) {
672         case 1:
673                 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
674                 break;
675         case 2:
676                 read_size = (hdr_bitrate * 144) / sf + padding;
677                 break;
678         default:
679         case 3:
680                 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
681                 break;
682         }
683         g_print("header : %d, read : %d\n", header, read_size);
684
685         *have_frame = TRUE;
686         *data = app->data + app->offset;
687
688         if (read_size >= offset)
689                 *size = offset;
690         else
691                 *size = read_size;
692
693         app->offset += *size;
694 }
695
696 #if 1
697 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
698 {
699         int readsize = 0, read_size = 0;
700         unsigned int header_size = ADTS_HEADER_SIZE;
701         unsigned char buffer[100000];
702         unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
703         int offset = app->length - app->offset;
704         unsigned char *pData = app->data + app->offset;
705         /*
706          * It is not support full parsing MP4 container box.
707          * So It MUST start as RAW valid frame sequence.
708          * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
709          */
710
711         /* change the below one later */
712         if (app->offset == 0) {
713                 /*
714                  * CAUTION : Codec data is needed only once  in first time
715                  * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
716                  * So I use this data (byte) as hard coding for temporary our testing.
717                  */
718 #if 1
719                 /*
720                  * The codec_data data is according to AudioSpecificConfig,
721                  *  ISO/IEC 14496-3, 1.6.2.1
722                  *
723                  *  below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
724                  * case : M4A - LC profile
725                  * codec_data=(buffer)119056e5000000000000000000000000
726                  * savs aac decoder get codec_data. size: 16  (Tag size : 5 byte)
727                  *     - codec data: profile  : 2
728                  *     - codec data: samplrate: 48000
729                  *     - codec data: channels : 2
730                  */
731                 /* 2 bytes are mandatory */
732                 codecdata[0] = 0x11;         /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
733                 codecdata[1] = 0x90;         /* ex) (4bit) 2 (2ch) */
734                 /* othter bytes are (optional) epconfig information */
735                 codecdata[2] = 0x56;
736                 codecdata[3] = 0xE5;
737                 codecdata[4] = 0x00;
738 #else
739                 /*
740                  *  below example is test for using "TestSample-EAAC+.m4a"
741                  *
742                  * case : M4A - HE-AAC v1 and v2 profile
743                  * codec_data=(buffer)138856e5a54880000000000000000000
744                  * savs aac decoder get codec_data. size: 16  (Tag size : 7 byte)
745                  *     - codec data: profile  : 2
746                  *     - codec data: samplrate: 22050
747                  *     - codec data: channels : 1
748                  */
749                 /* 2 bytes are mandatory */
750                 codecdata[0] = 0x13;         /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
751                 codecdata[1] = 0x88;         /* ex) (4bit) 1 (1ch) */
752                 /* othter bytes are (optional) epconfig information */
753                 codecdata[2] = 0x56;
754                 codecdata[3] = 0xE5;
755                 codecdata[4] = 0xA5;
756                 codecdata[5] = 0x48;
757                 codecdata[6] = 0x80;
758 #endif
759
760                 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
761                 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
762                         read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
763                 } else {
764                         read_size = 0;
765                         g_print("[FAIL] Not found aac frame sync.....\n");
766                 }
767                 readsize = read_size - header_size;
768                 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
769                 read_size = readsize + AAC_CODECDATA_SIZE;      /* return combination of (codec_data + raw_data) */
770                 app->offset += header_size + readsize;
771                 goto DONE;
772         }
773
774         if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
775                 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
776                 readsize = read_size - header_size;
777                 memcpy(buffer, pData + 7, readsize);    /* Make only RAW data, so exclude header 7 bytes */
778                 read_size = readsize;
779                 app->offset += header_size + readsize;
780
781                 if (app->offset > app->length) {
782                         read_size = 0;
783                         *have_frame = FALSE;
784                         g_print("[FAIL] offset error \n");
785                         return;
786                 }
787
788         } else {
789                 read_size = 0;
790                 g_print("[FAIL] Not found aac frame sync. \n");
791         }
792  DONE:
793         *data = buffer;
794         *have_frame = TRUE;
795         if (read_size >= offset)
796                 *size = offset;
797         else
798                 *size = read_size;
799 }
800 #endif
801
802 /**
803  * Extract Input data for AAC encoder
804  **/
805 /*
806    void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
807    {
808    int read_size;
809    int offset = app->length - app->offset;
810
811    read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
812
813    if (read_size >= offset)
814  *size = offset;
815
816  *have_frame = TRUE;
817  *data = app->data + app->offset;
818
819  if (read_size >= offset)
820  *size = offset;
821  else
822  *size = read_size;
823
824  app->offset += *size;
825  }
826  */
827 #if 0
828 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
829 {
830         if (pkt != NULL) {
831                 g_print("Used input buffer = %p\n", pkt);
832                 media_packet_destroy(pkt);
833         }
834         return;
835 }
836 #endif
837 int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
838 {
839         bool encoder;
840         media_format_mimetype_e mime = 0;
841         encoder = GET_IS_ENCODER(flag) ? 1 : 0;
842         *hardware = GET_IS_HW(flag) ? 1 : 0;
843         app->is_encoder = encoder;
844
845         switch (codecid) {
846         case MEDIACODEC_H264:
847                 if (encoder) {
848                         extractor = yuv_extractor;
849                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
850                 } else {
851                         extractor = h264_extractor;
852                         mime = MEDIA_FORMAT_H264_SP;
853                 }
854                 break;
855         case MEDIACODEC_MPEG4:
856                 if (encoder) {
857                         extractor = yuv_extractor;
858                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
859                 } else {
860                         extractor = mpeg4_extractor;
861                         mime = MEDIA_FORMAT_MPEG4_SP;
862                 }
863                 break;
864         case MEDIACODEC_H263:
865                 if (encoder) {
866                         extractor = yuv_extractor;
867                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
868                 } else {
869                         extractor = h263_extractor;
870                         mime = MEDIA_FORMAT_H263P;
871                 }
872                 break;
873         case MEDIACODEC_AAC:
874                 if (encoder) {
875                         extractor = aacenc_extractor;
876                         mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
877                 } else {
878                         extractor = aacdec_extractor;
879                         mime = MEDIA_FORMAT_AAC;
880                 }
881                 break;
882         case MEDIACODEC_AAC_HE:
883                 if (encoder) {
884                         extractor = aacenc_extractor;
885                         mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
886                 } else {
887                         extractor = extract_input_aacdec_m4a_test;
888                         mime = MEDIA_FORMAT_AAC_HE;
889                 }
890                 break;
891         case MEDIACODEC_AAC_HE_PS:
892                 break;
893         case MEDIACODEC_MP3:
894                 extractor = mp3dec_extractor;
895                 mime = MEDIA_FORMAT_MP3;
896                 break;
897         case MEDIACODEC_VORBIS:
898                 break;
899         case MEDIACODEC_FLAC:
900                 break;
901         case MEDIACODEC_WMAV1:
902                 break;
903         case MEDIACODEC_WMAV2:
904                 break;
905         case MEDIACODEC_WMAPRO:
906                 break;
907         case MEDIACODEC_WMALSL:
908                 break;
909         case MEDIACODEC_AMR_NB:
910                 if (encoder) {
911                         extractor = amrenc_extractor;
912                         mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
913                         app->is_amr_nb = TRUE;
914                 } else {
915                         extractor = amrdec_extractor;
916                         mime = MEDIA_FORMAT_AMR_NB;
917                 }
918                 break;
919         case MEDIACODEC_AMR_WB:
920                 if (encoder) {
921                         extractor = amrenc_extractor;
922                         mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to check according to verdor */
923                         app->is_amr_nb = FALSE;
924                 } else {
925                         extractor = amrdec_extractor;
926                         mime = MEDIA_FORMAT_AMR_WB;
927                 }
928                 break;
929         default:
930                 LOGE("NOT SUPPORTED!!!!");
931                 break;
932         }
933         return mime;
934 }
935
936 static void _mediacodec_process_input(App *app)
937 {
938         int i;
939         bool have_frame = FALSE;
940         int ret;
941         static guint64 pts = 0L;
942         void *buf_data_ptr = NULL;
943         media_packet_h pkt = NULL;
944         unsigned char *tmp;
945         int read;
946         int size;
947         int offset;
948         int stride_width;
949
950         for (i = 0; i < app->frame; i++) {
951                 g_print("----------read data------------\n");
952
953                 extractor(app, &tmp, &read, &have_frame);
954
955                 if (have_frame) {
956 #ifdef USE_POOL
957                 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
958                         g_print("media_packet_pool_aquire_packet failed\n");
959                         return;
960                 }
961 #else
962                 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
963                         g_print("media_packet_create_alloc failed\n");
964                         return;
965                 }
966 #endif
967
968                         if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
969                                 g_print("media_packet_set_pts failed\n");
970                                 return;
971                         }
972
973                         if (app->type != VIDEO_ENC) {
974                                 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
975                                 media_packet_set_buffer_size(pkt, (uint64_t)read);
976
977                                 memcpy(buf_data_ptr, tmp, read);
978                                 g_print("tmp:%p, read:%d\n", tmp, read);
979                         } else {
980                                 /* Y */
981                                 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
982                                 media_packet_get_video_stride_width(pkt, 0, &stride_width);
983                                 offset = app->width*app->height;
984
985                                 for (i = 0; i < app->height; i++) {
986                                         memcpy(buf_data_ptr, tmp, app->width);
987                                         buf_data_ptr += stride_width;
988                                         tmp += app->width;
989                                 }
990
991                                 if (app->hardware == TRUE) {
992                                         media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
993                                         media_packet_get_video_stride_width(pkt, 1, &stride_width);
994                                         size = app->width * app->height / 2;
995
996                                         for (i = 0; i < app->height / 2; i++) {
997                                                 memcpy(buf_data_ptr, tmp, app->width);
998                                                 buf_data_ptr += stride_width;
999                                                 tmp += app->width;
1000                                         }
1001                                 } else {
1002                                         /* U */
1003                                         media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1004                                         media_packet_get_video_stride_width(pkt, 1, &stride_width);
1005                                         size = (app->width>>1) * (app->height>>1);
1006
1007                                         for (i = 0; i < app->height/2; i++) {
1008                                                 memcpy(buf_data_ptr, tmp, app->width/2);
1009                                                 buf_data_ptr += stride_width;
1010                                                 tmp += app->width/2;
1011                                         }
1012
1013                                         /* V */
1014                                         media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1015                                         media_packet_get_video_stride_width(pkt, 2, &stride_width);
1016                                         offset += size;
1017
1018                                         for (i = 0; i < app->height/2; i++) {
1019                                                 memcpy(buf_data_ptr, tmp, app->width/2);
1020                                                 buf_data_ptr += stride_width;
1021                                                 tmp += app->width/2;
1022                                         }
1023
1024                                 }
1025                         }
1026                         mc_hex_dump("inbuf", tmp, 48);
1027
1028                         ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
1029                         if (ret != MEDIACODEC_ERROR_NONE)
1030                                 return;
1031
1032                         pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1033                 }
1034         }
1035 }
1036
1037 static gboolean read_data(App *app)
1038 {
1039         guint len = 0;
1040         bool have_frame = FALSE;
1041         int ret;
1042         static guint64 pts = 0L;
1043         void *buf_data_ptr = NULL;
1044         media_packet_h pkt = NULL;
1045         unsigned char *tmp;
1046         int i;
1047         int read;
1048         int size;
1049         int offset;
1050         int stride_width;
1051
1052         if (app->offset == 0) {
1053                 app->frame_count = 0;
1054                 app->start = clock();
1055         }
1056
1057         g_print("----------read data------------\n");
1058         extractor(app, &tmp, &read, &have_frame);
1059
1060         if (app->offset >= app->length - 4) {
1061                 /* EOS */
1062                 g_print("EOS\n");
1063                 app->finish = clock();
1064                 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1065                 g_print("---------------------------\n");
1066                 return FALSE;
1067         }
1068         g_print("length : %d, offset : %d\n", app->length, app->offset);
1069
1070         if (app->offset + len > app->length)
1071                 len = app->length - app->offset;
1072
1073         g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1074
1075         if (have_frame) {
1076 #ifdef USE_POOL
1077                 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1078                         g_print("media_packet_pool_aquire_packet failed\n");
1079                         return FALSE;
1080                 }
1081 #else
1082                 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1083                         g_print("media_packet_create_alloc failed\n");
1084                         return FALSE;
1085                 }
1086 #endif
1087                 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1088                         g_print("media_packet_set_pts failed\n");
1089                         return FALSE;
1090                 }
1091
1092
1093                 if (app->type != VIDEO_ENC) {
1094                         media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1095                         media_packet_set_buffer_size(pkt, (uint64_t)read);
1096
1097                         memcpy(buf_data_ptr, tmp, read);
1098                         g_print("tmp:%p, read:%d\n", tmp, read);
1099                 } else {
1100                         /* Y */
1101                         media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1102                         media_packet_get_video_stride_width(pkt, 0, &stride_width);
1103                         offset = app->width*app->height;
1104
1105                         for (i = 0; i < app->height; i++) {
1106                                 memcpy(buf_data_ptr, tmp, app->width);
1107                                 buf_data_ptr += stride_width;
1108                                 tmp += app->width;
1109                         }
1110
1111                         if (app->hardware == TRUE) {
1112                                 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1113                                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1114                                 size = app->width * app->height>>1;
1115
1116                                 for (i = 0; i < app->height>>1; i++) {
1117                                         memcpy(buf_data_ptr, tmp, app->width);
1118                                         buf_data_ptr += stride_width;
1119                                         tmp += app->width;
1120                                 }
1121
1122                         } else {
1123                                 /* U */
1124                                 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1125                                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1126                                 size = (app->width>>1) * (app->height>>1);
1127
1128                                 for (i = 0; i < app->height/2; i++) {
1129                                         memcpy(buf_data_ptr, tmp, app->width>>1);
1130                                         buf_data_ptr += stride_width;
1131                                         tmp += (app->width>>1);
1132                                 }
1133
1134                                 /* V */
1135                                 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1136                                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1137                                 offset += size;
1138
1139                                 for (i = 0; i < app->height/2; i++) {
1140                                         memcpy(buf_data_ptr, tmp, app->width>>1);
1141                                         buf_data_ptr += stride_width;
1142                                         tmp += (app->width>>1);
1143                                 }
1144
1145                         }
1146                 }
1147                 mc_hex_dump("inbuf", tmp, 48);
1148
1149                 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1150                 if (ret != MEDIACODEC_ERROR_NONE)
1151                         return FALSE;
1152
1153                 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1154         }
1155
1156         return TRUE;
1157 }
1158
1159 static void start_feed(App *app)
1160 {
1161         if (app->sourceid == 0) {
1162                 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1163                 g_print("start_feed\n");
1164         }
1165 }
1166
1167 static void stop_feed(App *app)
1168 {
1169         if (app->sourceid != 0) {
1170                 g_source_remove(app->sourceid);
1171                 app->sourceid = 0;
1172                 g_print("stop_feed\n");
1173         }
1174 }
1175
1176 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1177 {
1178         g_print("_mediacodec_inbuf_used_cb!!!\n");
1179 #ifdef USE_POOL
1180         media_packet_pool_release_packet(pkt_pool, pkt);
1181 #else
1182         media_packet_destroy(pkt);
1183 #endif
1184
1185         return TRUE;
1186 }
1187
1188 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1189 {
1190         media_packet_h out_pkt = NULL;
1191         int ret;
1192
1193         App *app = (App*)user_data;
1194
1195         g_print("_mediacodec_outbuf_available_cb\n");
1196
1197         g_mutex_lock(&app->lock);
1198
1199         ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1200
1201         if (ret != MEDIACODEC_ERROR_NONE)
1202                 g_print("get_output failed\n");
1203
1204         if (app->enable_dump) {
1205                 if (app->type == VIDEO_DEC)
1206                         decoder_output_dump(app, out_pkt);
1207                 else
1208                         output_dump(app, out_pkt);
1209         }
1210
1211         app->frame_count++;
1212
1213
1214         g_mutex_unlock(&app->lock);
1215
1216         media_packet_destroy(out_pkt);
1217         out_pkt = NULL;
1218         g_print("done\n");
1219
1220         return TRUE;
1221 }
1222
1223 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1224 {
1225         g_print("_mediacodec_buffer_status_cb %d\n", status);
1226
1227         App *app = (App*)user_data;
1228
1229         if (status == MEDIACODEC_NEED_DATA)
1230                 start_feed(app);
1231         else if (status == MEDIACODEC_ENOUGH_DATA)
1232                 stop_feed(app);
1233
1234         return TRUE;
1235 }
1236
1237 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1238 {
1239         return TRUE;
1240 }
1241
1242 static bool _mediacodec_eos_cb(void *user_data)
1243 {
1244         return TRUE;
1245 }
1246
1247 gboolean  _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
1248 {
1249         g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
1250         return TRUE;
1251 }
1252
1253 static void _mediacodec_prepare(App *app, bool frame_all)
1254 {
1255         int ret;
1256
1257         g_print("supported codec lists -internal-\n");
1258         mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
1259
1260         /* create instance */
1261         ret = mediacodec_create(&app->mc_handle[0]);
1262         if (ret  != MEDIACODEC_ERROR_NONE) {
1263                 g_print("mediacodec_create  failed\n");
1264                 return;
1265         }
1266
1267         /* set codec */
1268         ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1269         if (ret  != MEDIACODEC_ERROR_NONE) {
1270                 g_print("mediacodec_set_codec failed\n");
1271                 return;
1272         }
1273
1274         app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1275
1276         /* set codec info */
1277         ret = media_format_create(&fmt);
1278
1279         switch (app->type) {
1280         case VIDEO_DEC:
1281                 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1282                 media_format_set_video_mime(fmt, app->mime);
1283                 media_format_set_video_width(fmt, app->width);
1284                 media_format_set_video_height(fmt, app->height);
1285                 break;
1286         case VIDEO_ENC:
1287                 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1288                 media_format_set_video_mime(fmt, app->mime);
1289                 media_format_set_video_width(fmt, app->width);
1290                 media_format_set_video_height(fmt, app->height);
1291                 media_format_set_video_avg_bps(fmt, app->target_bits);
1292                 break;
1293         case AUDIO_DEC:
1294                 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1295                 media_format_set_audio_mime(fmt, app->mime);
1296                 media_format_set_audio_channel(fmt, app->channel);
1297                 media_format_set_audio_samplerate(fmt, app->samplerate);
1298                 media_format_set_audio_bit(fmt, app->bit);
1299                 break;
1300         case AUDIO_ENC:
1301                 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1302                 media_format_set_audio_mime(fmt, app->mime);
1303                 media_format_set_audio_channel(fmt, app->channel);
1304                 media_format_set_audio_samplerate(fmt, app->samplerate);
1305                 media_format_set_audio_bit(fmt, app->bit);
1306                 break;
1307         default:
1308                 g_print("invaild type\n");
1309                 break;
1310         }
1311
1312         if (ret  != MEDIACODEC_ERROR_NONE) {
1313                 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1314                 return;
1315         }
1316
1317         /* set callback */
1318         g_print("supported codec lists\n");
1319         mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
1320         mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1321         mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1322         if (frame_all)
1323                 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1324         mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1325         mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1326
1327         /* prepare */
1328         ret = mediacodec_prepare(app->mc_handle[0]);
1329         if (ret  != MEDIACODEC_ERROR_NONE) {
1330                 g_print("mediacodec_prepare failed\n");
1331                 return;
1332         }
1333
1334
1335 /* get packet pool instance */
1336         ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1337         if (ret != MEDIA_PACKET_ERROR_NONE) {
1338                 g_print("mediacodec_get_packet_pool failed\n");
1339                 return;
1340         }
1341         g_print("\n\nmediacodec start\n\n");
1342
1343         return;
1344 }
1345
1346 static void _mediacodec_enc_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1347 {
1348         /* release input raw packet */
1349         media_packet_destroy(pkt);
1350 }
1351
1352 /* this callback is called when the input buffer for codec has done to use */
1353 static void _mediacodec_dec_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1354 {
1355         /* release input encoded packet */
1356         media_packet_destroy(pkt);
1357 }
1358
1359 static void _mediacodec_enc_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1360 {
1361         App *app = (App*)user_data;
1362
1363         mediacodec_h media_codec_handle = app->mc_handle[1];
1364         media_packet_h output_buf = NULL;
1365         mediacodec_get_output(media_codec_handle, &output_buf, 0);
1366         /* decode encoded camera preview */
1367         mediacodec_process_input(app->mc_handle[0], output_buf, 0);
1368 }
1369
1370 static void _mediacodec_dec_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1371 {
1372         App *app = (App*)user_data;
1373
1374         mediacodec_h media_codec_handle = app->mc_handle[0];
1375         media_packet_h output_buf = NULL;
1376
1377         mediacodec_get_output(media_codec_handle, &output_buf, 0);
1378
1379         if (app->enable_dump)
1380                 decoder_output_dump(app, output_buf);
1381
1382         media_packet_destroy(output_buf);
1383 }
1384
1385 static void _media_packet_preview_cb(media_packet_h packet, void *user_data)
1386 {
1387         App *app = user_data;
1388         g_mutex_lock(&app->lock);
1389         mediacodec_process_input(app->mc_handle[1], packet, 0);
1390         g_mutex_unlock(&app->lock);
1391
1392         return;
1393 }
1394
1395 static void _mediacodec_camera_start(App *app)
1396 {
1397         int default_format = CAMERA_PIXEL_FORMAT_NV12;
1398         app->width = 640;
1399         app->height = 480;
1400         app->hardware = TRUE;
1401
1402         /*create decoder instance and setup */
1403         mediacodec_create(&app->mc_handle[0]);
1404         mediacodec_set_codec(app->mc_handle[0], MEDIACODEC_H264, MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW);
1405         mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1406         /* set callback */
1407         mediacodec_set_input_buffer_used_cb(app->mc_handle[0], _mediacodec_dec_input_buffer_used_cb, NULL);
1408         mediacodec_set_output_buffer_available_cb(app->mc_handle[0], _mediacodec_dec_output_buffer_available_cb, app);
1409         mediacodec_prepare(app->mc_handle[0]);
1410
1411         /*create encoder instance and setup */
1412         mediacodec_create(&app->mc_handle[1]);
1413         mediacodec_set_codec(app->mc_handle[1], MEDIACODEC_H264, MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW);
1414         mediacodec_set_venc_info(app->mc_handle[1], app->width, app->height, 30, 1000);
1415         /* set callback */
1416         mediacodec_set_input_buffer_used_cb(app->mc_handle[1], _mediacodec_enc_input_buffer_used_cb, NULL);
1417         mediacodec_set_output_buffer_available_cb(app->mc_handle[1], _mediacodec_enc_output_buffer_available_cb, app);
1418         mediacodec_prepare(app->mc_handle[1]);
1419
1420         /* create camera instance and setup and then start preview */
1421         camera_create(CAMERA_DEVICE_CAMERA0, &app->camera_handle);
1422         camera_set_media_packet_preview_cb(app->camera_handle, _media_packet_preview_cb, app);
1423         camera_get_preview_format(app->camera_handle, &default_format);
1424         camera_set_preview_format(app->camera_handle, default_format);
1425         camera_set_preview_resolution(app->camera_handle, app->width, app->height);
1426         camera_set_display(app->camera_handle, CAMERA_DISPLAY_TYPE_NONE, NULL);
1427         camera_start_preview(app->camera_handle);
1428
1429         return;
1430 }
1431
1432
1433 static void _mediacodec_camera_stop(App *app)
1434 {
1435         camera_state_e camera_state = CAMERA_STATE_NONE;
1436
1437         camera_get_state(app->camera_handle, &camera_state);
1438         camera_stop_preview(app->camera_handle);
1439         camera_destroy(app->camera_handle);
1440
1441         mediacodec_unprepare(app->mc_handle[0]);
1442         mediacodec_unprepare(app->mc_handle[1]);
1443         mediacodec_destroy(app->mc_handle[0]);
1444         mediacodec_destroy(app->mc_handle[1]);
1445         return;
1446 }
1447
1448 static void _mediacodec_unprepare(App *app)
1449 {
1450         mediacodec_unprepare(app->mc_handle[0]);
1451 }
1452
1453 static void _mediacodec_destroy(App *app)
1454 {
1455 #ifdef USE_POOL
1456         if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1457
1458                 g_print("media_packet_pool_deallocatet failed\n");
1459                 return;
1460         }
1461
1462         if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1463
1464                 g_print(" media_packet_pool_destroy failed\n");
1465                 return;
1466         }
1467         g_print("media packet pool destroyed! \n");
1468 #endif
1469         mediacodec_destroy(app->mc_handle[0]);
1470 }
1471
1472 static void input_filepath(char *filename, App *app)
1473 {
1474         GError *error = NULL;
1475
1476         app->obj++;
1477         app->file = g_mapped_file_new(filename, FALSE, &error);
1478         if (error) {
1479                 g_print("failed to open file : %s\n", error->message);
1480                 g_error_free(error);
1481                 return;
1482         }
1483
1484         app->length = g_mapped_file_get_length(app->file);
1485         app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1486         app->offset = 0;
1487         g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1488
1489         return;
1490 }
1491
1492 void quit_program(App *app)
1493 {
1494                 media_format_unref(fmt);
1495                 g_main_loop_quit(app->loop);
1496                 elm_exit();
1497
1498 }
1499
1500 void reset_menu_state()
1501 {
1502         g_menu_state = CURRENT_STATUS_MAINMENU;
1503         return;
1504 }
1505
1506 void _interpret_main_menu(char *cmd, App *app)
1507 {
1508         int len =  strlen(cmd);
1509         if (len == 1) {
1510                 if (strncmp(cmd, "a", 1) == 0)
1511                         g_menu_state = CURRENT_STATUS_FILENAME;
1512                 else if (strncmp(cmd, "o", 1) == 0)
1513                         g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1514                 else if (strncmp(cmd, "q", 1) == 0)
1515                         quit_program(app);
1516                 else
1517                         g_print("unknown menu \n");
1518         } else if (len == 2) {
1519                 if (strncmp(cmd, "pr", 2) == 0)
1520                         _mediacodec_prepare(app, 0);
1521                 else if (strncmp(cmd, "pa", 2) == 0)
1522                         _mediacodec_prepare(app, 1);
1523                 else if (strncmp(cmd, "sc", 2) == 0)
1524                         g_menu_state = CURRENT_STATUS_SET_CODEC;
1525                 else if (strncmp(cmd, "vd", 2) == 0)
1526                         g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1527                 else if (strncmp(cmd, "ve", 2) == 0)
1528                         g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1529                 else if (strncmp(cmd, "ad", 2) == 0)
1530                         g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1531                 else if (strncmp(cmd, "ae", 2) == 0)
1532                         g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1533                 else if (strncmp(cmd, "pi", 2) == 0)
1534                         g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1535                 else if (strncmp(cmd, "un", 2) == 0)
1536                         _mediacodec_unprepare(app);
1537                 else if (strncmp(cmd, "dt", 2) == 0)
1538                         _mediacodec_destroy(app);
1539                 else if (strncmp(cmd, "cr", 2) == 0)
1540                         _mediacodec_camera_start(app);
1541                 else if (strncmp(cmd, "ct", 2) == 0)
1542                         _mediacodec_camera_stop(app);
1543                 else if (strncmp(cmd, "dp", 2) == 0) {
1544                         if (!app->enable_dump) {
1545                                 app->enable_dump = TRUE;
1546                                 g_print("dump enabled\n");
1547                         } else {
1548                                 app->enable_dump = FALSE;
1549                                 g_print("dump disabled\n");
1550                         }
1551                 } else
1552                         display_sub_basic();
1553         } else {
1554                 g_print("unknown menu \n");
1555         }
1556
1557         return;
1558 }
1559
1560 static void displaymenu(void)
1561 {
1562         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1563                 display_sub_basic();
1564         } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1565                 g_print("*** input mediapath.\n");
1566         } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1567                 g_print("*** Codec id : Select Codec ID Numbe  (e.g. AAC_LC = 96)\n");
1568                 g_print("               L16    =  16 (0x10)\n");
1569                 g_print("               ALAW   =  32 (0x20)\n");
1570                 g_print("               ULAW   =  48 (0x30)\n");
1571                 g_print("               AMR_NB =  64 (0x40)\n");
1572                 g_print("               AMR_WB =  65 (0x41)\n");
1573                 g_print("               G729   =  80 (0x50)\n");
1574                 g_print("               AAC_LC =  96 (0x60)\n");
1575                 g_print("               AAC_HE =  97 (0x61)\n");
1576                 g_print("               AAC_PS =  98 (0x62)\n");
1577                 g_print("               MP3    = 112 (0x70)\n");
1578                 g_print("               VORBIS = 128 (0x80)\n");
1579                 g_print("               FLAC   = 144 (0x90)\n");
1580                 g_print("               WMAV1  = 160 (0xA0)\n");
1581                 g_print("               WMAV2  = 161 (0xA1)\n");
1582                 g_print("               WMAPRO = 162 (0xA2)\n");
1583                 g_print("               WMALSL = 163 (0xA3)\n");
1584                 g_print("               -------------------\n");
1585                 g_print("               H261   = 101\n");
1586                 g_print("               H263   = 102\n");
1587                 g_print("               H264   = 103\n");
1588                 g_print("               MJPEG  = 104\n");
1589                 g_print("               MPEG1  = 105\n");
1590                 g_print("               MPEG2  = 106\n");
1591                 g_print("               MPEG4  = 107\n");
1592                 g_print("               -------------------\n");
1593                 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1594                 g_print("               CODEC : ENCODER =  1       DECODER =  2\n");
1595                 g_print("               TYPE  : HW      =  4       SW      =  8\n");
1596                 g_print("*** input codec id, falgs.\n");
1597         } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1598                 g_print("*** input video decode configure.(width, height)\n");
1599         } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1600                 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1601         } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1602                 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000,  2, 16))\n");
1603         } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1604                 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000,  2, 16, 128000))\n");
1605         } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1606                 g_print("*** input dec process number\n");
1607         } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1608                 g_print("*** input get output buffer number\n");
1609         } else {
1610                 g_print("*** unknown status.\n");
1611         }
1612         g_print(" >>> ");
1613 }
1614
1615 gboolean timeout_menu_display(void* data)
1616 {
1617         displaymenu();
1618         return FALSE;
1619 }
1620
1621
1622 static void interpret(char *cmd, App *app)
1623 {
1624         switch (g_menu_state) {
1625         case CURRENT_STATUS_MAINMENU:
1626                 _interpret_main_menu(cmd, app);
1627                 break;
1628         case CURRENT_STATUS_FILENAME:
1629                 input_filepath(cmd, app);
1630                 reset_menu_state();
1631                 break;
1632         case CURRENT_STATUS_SET_CODEC:
1633         {
1634                 int tmp;
1635                 static int cnt = 0;
1636                 char **ptr = NULL;
1637                 switch (cnt) {
1638                 case 0:
1639                         tmp = atoi(cmd);
1640
1641                         if (tmp > 100 &&
1642                                 (tmp != 112) &&
1643                                 (tmp != 128) &&
1644                                 (tmp != 144) &&
1645                                 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1646                                         tmp = strtol(cmd, ptr, 16);
1647                                         app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1648                         } else
1649                                 app->codecid = 0x1000 + tmp;
1650
1651                         cnt++;
1652                         break;
1653                 case 1:
1654                         app->flag = atoi(cmd);
1655                         cnt = 0;
1656                         reset_menu_state();
1657                         break;
1658                 default:
1659                         break;
1660                 }
1661         }
1662         break;
1663         case CURRENT_STATUS_SET_VDEC_INFO:
1664         {
1665                 static int cnt = 0;
1666                 switch (cnt) {
1667                 case 0:
1668                         app->width = atoi(cmd);
1669                         cnt++;
1670                         break;
1671                 case 1:
1672                         app->height = atoi(cmd);
1673                         app->type = VIDEO_DEC;
1674
1675                         reset_menu_state();
1676                         cnt = 0;
1677                         break;
1678                 default:
1679                         break;
1680                 }
1681         }
1682         break;
1683         case CURRENT_STATUS_SET_VENC_INFO:
1684         {
1685                 static int cnt = 0;
1686                 switch (cnt) {
1687                 case 0:
1688                         app->width = atoi(cmd);
1689                         cnt++;
1690                         break;
1691                 case 1:
1692                         app->height = atoi(cmd);
1693                         cnt++;
1694                         break;
1695                 case 2:
1696                         app->fps = atol(cmd);
1697                         cnt++;
1698                         break;
1699                 case 3:
1700                         app->target_bits = atoi(cmd);
1701                         app->type = VIDEO_ENC;
1702
1703                         reset_menu_state();
1704                         cnt = 0;
1705                         break;
1706                 default:
1707                         break;
1708                 }
1709         }
1710         break;
1711         case CURRENT_STATUS_SET_ADEC_INFO:
1712         {
1713                 static int cnt = 0;
1714                 switch (cnt) {
1715                 case 0:
1716                         app->samplerate = atoi(cmd);
1717                         cnt++;
1718                         break;
1719                 case 1:
1720                         app->channel = atoi(cmd);
1721                         cnt++;
1722                         break;
1723                 case 2:
1724                         app->bit = atoi(cmd);
1725                         app->type = AUDIO_DEC;
1726
1727                         reset_menu_state();
1728                         cnt = 0;
1729                         break;
1730                 default:
1731                         break;
1732                 }
1733         }
1734         break;
1735         case CURRENT_STATUS_SET_AENC_INFO:
1736         {
1737                 static int cnt = 0;
1738                 switch (cnt) {
1739                 case 0:
1740                         app->samplerate = atoi(cmd);
1741                         cnt++;
1742                         break;
1743                 case 1:
1744                         app->channel = atoi(cmd);
1745                         cnt++;
1746                         break;
1747                 case 2:
1748                         app->bit = atoi(cmd);
1749                         cnt++;
1750                         break;
1751                 case 3:
1752                         app->bitrate = atoi(cmd);
1753                         app->type = AUDIO_ENC;
1754
1755                         reset_menu_state();
1756                         cnt = 0;
1757                         break;
1758                 default:
1759                         break;
1760                 }
1761         }
1762         break;
1763         case CURRENT_STATUS_PROCESS_INPUT:
1764         {
1765                 app->frame = atoi(cmd);
1766
1767                 if (app->frame > 0 && app->frame < 10)
1768                         _mediacodec_process_input(app);
1769                 reset_menu_state();
1770         }
1771         break;
1772         case CURRENT_STATUS_GET_OUTPUT:
1773         {
1774                 reset_menu_state();
1775         }
1776         break;
1777         default:
1778                 break;
1779         }
1780
1781         g_timeout_add(100, timeout_menu_display, 0);
1782 }
1783
1784 static void display_sub_basic()
1785 {
1786         g_print("\n");
1787         g_print("=========================================================================================\n");
1788         g_print("                                    media codec test\n");
1789         g_print("-----------------------------------------------------------------------------------------\n");
1790         g_print("a. Create \t\t");
1791         g_print("sc. Set codec \n");
1792         g_print("vd. Set vdec info \t");
1793         g_print("ve. Set venc info \n");
1794         g_print("ad. Set adec info \t");
1795         g_print("ae. Set aenc info \n");
1796         g_print("pr. Prepare \t");
1797         g_print("pa. Prepare and process all\t\t");
1798         g_print("pi. process input with num\n");
1799         g_print("o. Get output \t\t");
1800         g_print("rb. Reset output buffer \n");
1801         g_print("un. Unprepare \t\t");
1802         g_print("dt. Destroy \t\t");
1803         g_print("q. quit test suite \n");
1804         g_print("dp. enable dump \n");
1805         g_print("-----------------------------------------------------------------------------------------\n");
1806         g_print("cr. camera preview -> encoder -> decoder\n");
1807         g_print("ct. quit camera test\n");
1808         g_print("\n");
1809         g_print("=========================================================================================\n");
1810 }
1811
1812 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1813 {
1814         gchar buf[MAX_STRING_LEN];
1815         gsize read;
1816         GError *error = NULL;
1817         App *context = (App*)data;
1818
1819         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1820         buf[read] = '\0';
1821         g_strstrip(buf);
1822         interpret(buf, context);
1823
1824         return TRUE;
1825 }
1826
1827 int main(int argc, char *argv[])
1828 {
1829         App *app = &s_app;
1830
1831         GIOChannel *stdin_channel;
1832         stdin_channel = g_io_channel_unix_new(0);
1833         g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1834         g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1835
1836
1837         displaymenu();
1838         app->loop = g_main_loop_new(NULL, TRUE);
1839         app->timer = g_timer_new();
1840         g_main_loop_run(app->loop);
1841
1842
1843
1844         ops.data = app;
1845
1846         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1847 }
1848
1849
1850
1851 void mc_hex_dump(char *desc, void *addr, int len)
1852 {
1853         int i;
1854         unsigned char buff[17];
1855         unsigned char *pc = (unsigned char *)addr;
1856
1857         if (desc != NULL)
1858                 g_print("%s:\n", desc);
1859
1860         for (i = 0; i < len; i++) {
1861
1862                 if ((i % 16) == 0) {
1863                         if (i != 0)
1864                                 g_print("  %s\n", buff);
1865
1866                         g_print("  %04x ", i);
1867                 }
1868
1869                 g_print(" %02x", pc[i]);
1870
1871                 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1872                         buff[i % 16] = '.';
1873                 else
1874                         buff[i % 16] = pc[i];
1875                 buff[(i % 16) + 1] = '\0';
1876         }
1877
1878         while ((i % 16) != 0) {
1879                 g_print("   ");
1880                 i++;
1881         }
1882         g_print("  %s\n", buff);
1883 }
1884
1885 static void decoder_output_dump(App *app, media_packet_h pkt)
1886 {
1887         void *temp;
1888         int i = 0;
1889         int stride_width, stride_height;
1890         gchar filename[100] = {0};
1891         FILE *fp = NULL;
1892         int ret = 0;
1893
1894         g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1895         fp = fopen(filename, "ab");
1896
1897         media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1898         media_packet_get_video_stride_width(pkt, 0, &stride_width);
1899         media_packet_get_video_stride_height(pkt, 0, &stride_height);
1900         g_print("stride : %d, %d\n", stride_width, stride_height);
1901
1902         for (i = 0; i < app->height; i++) {
1903                 ret = fwrite(temp, app->width, 1, fp);
1904                 temp += stride_width;
1905         }
1906
1907         if (app->hardware == TRUE) {
1908                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1909                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1910                 for (i = 0; i < app->height/2; i++) {
1911                         ret = fwrite(temp, app->width, 1, fp);
1912                         temp += stride_width;
1913                 }
1914         } else {
1915                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1916                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1917                 for (i = 0; i < app->height/2; i++) {
1918                         ret = fwrite(temp, app->width/2, 1, fp);
1919                         temp += stride_width;
1920                 }
1921
1922                 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1923                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1924                 for (i = 0; i < app->height/2; i++) {
1925                         ret = fwrite(temp, app->width/2, 1, fp);
1926                         temp += stride_width;
1927                 }
1928         }
1929
1930         g_print("codec dec output dumped!!%d\n", ret);
1931         fclose(fp);
1932
1933 }
1934
1935 /**
1936  *  Add ADTS header at the beginning of each and every AAC packet.
1937  *  This is needed as MediaCodec encoder generates a packet of raw AAC data.
1938  *  Note the packetLen must count in the ADTS header itself.
1939  **/
1940 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1941 {
1942         int profile = 2;    /* AAC LC (0x01) */
1943         int freqIdx = 3;    /* 48KHz (0x03) */
1944         int chanCfg = 2;    /* CPE (0x02) */
1945
1946         if (app->samplerate == 96000) freqIdx = 0;
1947         else if (app->samplerate == 88200) freqIdx = 1;
1948         else if (app->samplerate == 64000) freqIdx = 2;
1949         else if (app->samplerate == 48000) freqIdx = 3;
1950         else if (app->samplerate == 44100) freqIdx = 4;
1951         else if (app->samplerate == 32000) freqIdx = 5;
1952         else if (app->samplerate == 24000) freqIdx = 6;
1953         else if (app->samplerate == 22050) freqIdx = 7;
1954         else if (app->samplerate == 16000) freqIdx = 8;
1955         else if (app->samplerate == 12000) freqIdx = 9;
1956         else if (app->samplerate == 11025) freqIdx = 10;
1957         else if (app->samplerate == 8000) freqIdx = 11;
1958
1959         if ((app->channel == 1) || (app->channel == 2))
1960                 chanCfg = app->channel;
1961
1962         /* fill in ADTS data */
1963         buffer[0] = (char)0xFF;
1964         buffer[1] = (char)0xF1;
1965         buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1966         buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1967         buffer[4] = (char)((packetLen&0x7FF) >> 3);
1968         buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1969         buffer[6] = (char)0xFC;
1970 }
1971
1972 static void output_dump(App *app, media_packet_h pkt)
1973 {
1974         void *temp;
1975         uint64_t buf_size;
1976         gchar filename[100] = {0};
1977         FILE *fp = NULL;
1978         int ret = 0;
1979         char adts[100] = {0, };
1980
1981         g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
1982         fp = fopen(filename, "ab");
1983
1984         media_packet_get_buffer_data_ptr(pkt, &temp);
1985         media_packet_get_buffer_size(pkt, &buf_size);
1986         g_print("output data : %p, size %d\n", temp, (int)buf_size);
1987
1988         if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1989                 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1990                 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1991                 g_print("adts appended\n");
1992         } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1)       {
1993                 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1994                 g_print("%s - AMR_header write in first frame\n", __func__);
1995                 fwrite(&AMR_header[0], 1, sizeof(AMR_header)   - 1, fp);         /* AMR-NB magic number */
1996                 write_amr_header = 0;
1997         }
1998
1999         fwrite(temp, (int)buf_size, 1, fp);
2000
2001         g_print("codec dec output dumped!!%d\n", ret);
2002         fclose(fp);
2003
2004 }
2005
2006 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
2007 {
2008         guint media_codec_id_u = (guint)media_codec_id;
2009
2010         switch (media_codec_id_u) {
2011         case MEDIACODEC_L16:
2012                 return "L16";
2013         case MEDIACODEC_ALAW:
2014                 return "ALAW";
2015         case MEDIACODEC_ULAW:
2016                 return "ULAW";
2017         case MEDIACODEC_AMR_NB:
2018                 return "AMR_NB";
2019         case MEDIACODEC_AMR_WB:
2020                 return "AMR_WB";
2021         case MEDIACODEC_G729:
2022                 return "G729";
2023         case MEDIACODEC_AAC_LC:
2024                 return "AAC_LC";
2025         case MEDIACODEC_AAC_HE:
2026                 return "AAC_HE";
2027         case MEDIACODEC_AAC_HE_PS:
2028                 return "AAC_HE_PS";
2029         case MEDIACODEC_MP3:
2030                 return "MP3";
2031         case MEDIACODEC_VORBIS:
2032                 return "VORBIS";
2033         case MEDIACODEC_FLAC:
2034                 return "FLAC";
2035         case MEDIACODEC_WMAV1:
2036                 return "WMAV1";
2037         case MEDIACODEC_WMAV2:
2038                 return "WMAV2";
2039         case MEDIACODEC_WMAPRO:
2040                 return "WMAPRO";
2041         case MEDIACODEC_WMALSL:
2042                 return "WMALSL";
2043         case MEDIACODEC_H261:
2044                 return "H261";
2045         case MEDIACODEC_H263:
2046                 return "H263";
2047         case MEDIACODEC_H264:
2048                 return "H264";
2049         case MEDIACODEC_MJPEG:
2050                 return "MJPEG";
2051         case MEDIACODEC_MPEG1:
2052                 return "MPEG1";
2053         case MEDIACODEC_MPEG2:
2054                 return "MPEG2";
2055         case MEDIACODEC_MPEG4:
2056                 return "MPEG4";
2057         case MEDIACODEC_HEVC:
2058                 return "HEVC";
2059         case MEDIACODEC_VP8:
2060                 return "VP8";
2061         case MEDIACODEC_VP9:
2062                 return "VP9";
2063         case MEDIACODEC_VC1:
2064                 return "VC1";
2065         default:
2066                 return "NONE";
2067         }
2068 }
2069