e94e0118bf9b4fdd36831b7535eefe5ed2cca645
[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                 if (layer < 1) {
644                         *have_frame = FALSE;
645                         return;
646                 }
647         }
648
649         /* if it's an invalid bitrate */
650         if (((header >> 12) & 0xf) == 0xf) {
651                 g_print ("[ERROR] invalid bitrate: %d\n", (header >> 12) & 0xf);
652                 *have_frame = FALSE;
653                 return;
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                         return;
661                 }
662         }
663
664         /* if it's an invalid samplerate */
665         if (((header >> 10) & 0x3) == 0x3) {
666                 g_print ("[ERROR] invalid samplerate: %d\n", (header >> 10) & 0x3);
667                 *have_frame = FALSE;
668                 return;
669         } else {
670                 sf = (header >> 10) & 0x3;
671                 sf = mp3types_freqs[lsf + mpg25][sf];
672         }
673
674         padding = (header >> 9) & 0x1;
675
676         switch (layer) {
677         case 1:
678                 read_size = 4 * ((hdr_bitrate * 12) / sf + padding);
679                 break;
680         case 2:
681                 read_size = (hdr_bitrate * 144) / sf + padding;
682                 break;
683         default:
684         case 3:
685                 read_size = (hdr_bitrate * 144) / (sf << lsf) + padding;
686                 break;
687         }
688         g_print("header : %d, read : %d\n", header, read_size);
689
690         *have_frame = TRUE;
691         *data = app->data + app->offset;
692
693         if (read_size >= offset)
694                 *size = offset;
695         else
696                 *size = read_size;
697
698         app->offset += *size;
699 }
700
701 #if 1
702 void extract_input_aacdec_m4a_test(App * app, unsigned char **data, int *size, bool * have_frame)
703 {
704         int readsize = 0, read_size = 0;
705         unsigned int header_size = ADTS_HEADER_SIZE;
706         unsigned char buffer[100000];
707         unsigned char codecdata[AAC_CODECDATA_SIZE] = { 0, };
708         int offset = app->length - app->offset;
709         unsigned char *pData = app->data + app->offset;
710         /*
711          * It is not support full parsing MP4 container box.
712          * So It MUST start as RAW valid frame sequence.
713          * Testsuit that are not guaranteed to be available on functionality of all General DEMUXER/PARSER.
714          */
715
716         /* change the below one later */
717         if (app->offset == 0) {
718                 /*
719                  * CAUTION : Codec data is needed only once  in first time
720                  * Codec data is made(or extracted) by MP4 demuxer in 'esds' box.
721                  * So I use this data (byte) as hard coding for temporary our testing.
722                  */
723 #if 1
724                 /*
725                  * The codec_data data is according to AudioSpecificConfig,
726                  *  ISO/IEC 14496-3, 1.6.2.1
727                  *
728                  *  below example is test for using "test.aac" or "TestSample-AAC-LC.m4a"
729                  * case : M4A - LC profile
730                  * codec_data=(buffer)119056e5000000000000000000000000
731                  * savs aac decoder get codec_data. size: 16  (Tag size : 5 byte)
732                  *     - codec data: profile  : 2
733                  *     - codec data: samplrate: 48000
734                  *     - codec data: channels : 2
735                  */
736                 /* 2 bytes are mandatory */
737                 codecdata[0] = 0x11;         /* ex) (5bit) 2 (LC) / (4bit) 3 (48khz)*/
738                 codecdata[1] = 0x90;         /* ex) (4bit) 2 (2ch) */
739                 /* othter bytes are (optional) epconfig information */
740                 codecdata[2] = 0x56;
741                 codecdata[3] = 0xE5;
742                 codecdata[4] = 0x00;
743 #else
744                 /*
745                  *  below example is test for using "TestSample-EAAC+.m4a"
746                  *
747                  * case : M4A - HE-AAC v1 and v2 profile
748                  * codec_data=(buffer)138856e5a54880000000000000000000
749                  * savs aac decoder get codec_data. size: 16  (Tag size : 7 byte)
750                  *     - codec data: profile  : 2
751                  *     - codec data: samplrate: 22050
752                  *     - codec data: channels : 1
753                  */
754                 /* 2 bytes are mandatory */
755                 codecdata[0] = 0x13;         /* ex) (5bit) 2 (LC) / (4bit) 9 (22khz) */
756                 codecdata[1] = 0x88;         /* ex) (4bit) 1 (1ch) */
757                 /* othter bytes are (optional) epconfig information */
758                 codecdata[2] = 0x56;
759                 codecdata[3] = 0xE5;
760                 codecdata[4] = 0xA5;
761                 codecdata[5] = 0x48;
762                 codecdata[6] = 0x80;
763 #endif
764
765                 memcpy(buffer, codecdata, AAC_CODECDATA_SIZE);
766                 if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
767                         read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
768                 } else {
769                         read_size = 0;
770                         g_print("[FAIL] Not found aac frame sync.....\n");
771                 }
772                 readsize = read_size - header_size;
773                 memcpy(buffer + AAC_CODECDATA_SIZE, pData + 7, readsize);
774                 read_size = readsize + AAC_CODECDATA_SIZE;      /* return combination of (codec_data + raw_data) */
775                 app->offset += header_size + readsize;
776                 goto DONE;
777         }
778
779         if ((pData != NULL) && (pData[0] == 0xff) && ((pData[1] & 0xf6) == 0xf0)) {
780                 read_size = ((pData[3] & 0x03) << 11) | (pData[4] << 3) | ((pData[5] & 0xe0) >> 5);
781                 readsize = read_size - header_size;
782                 memcpy(buffer, pData + 7, readsize);    /* Make only RAW data, so exclude header 7 bytes */
783                 read_size = readsize;
784                 app->offset += header_size + readsize;
785
786         } else {
787                 read_size = 0;
788                 g_print("[FAIL] Not found aac frame sync. \n");
789         }
790  DONE:
791         *data = buffer;
792         *have_frame = TRUE;
793         if (read_size >= offset)
794                 *size = offset;
795         else
796                 *size = read_size;
797 }
798 #endif
799
800 /**
801  * Extract Input data for AAC encoder
802  **/
803 /*
804    void aacenc_extractor(App *app, unsigned char **data, int *size, bool *have_frame)
805    {
806    int read_size;
807    int offset = app->length - app->offset;
808
809    read_size = ((DEFAULT_SAMPLEBYTE*DEFAULT_CHANNEL)*(DEFAULT_BIT/8));
810
811    if (read_size >= offset)
812  *size = offset;
813
814  *have_frame = TRUE;
815  *data = app->data + app->offset;
816
817  if (read_size >= offset)
818  *size = offset;
819  else
820  *size = read_size;
821
822  app->offset += *size;
823  }
824  */
825 #if 0
826 static void _mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
827 {
828         if (pkt != NULL) {
829                 g_print("Used input buffer = %p\n", pkt);
830                 media_packet_destroy(pkt);
831         }
832         return;
833 }
834 #endif
835 int  _mediacodec_set_codec(App *app, int codecid, int flag, bool *hardware)
836 {
837         bool encoder;
838         media_format_mimetype_e mime = 0;
839         encoder = GET_IS_ENCODER(flag) ? 1 : 0;
840         *hardware = GET_IS_HW(flag) ? 1 : 0;
841         app->is_encoder = encoder;
842
843         switch (codecid) {
844         case MEDIACODEC_H264:
845                 if (encoder) {
846                         extractor = yuv_extractor;
847                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
848                 } else {
849                         extractor = h264_extractor;
850                         mime = MEDIA_FORMAT_H264_SP;
851                 }
852                 break;
853         case MEDIACODEC_MPEG4:
854                 if (encoder) {
855                         extractor = yuv_extractor;
856                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
857                 } else {
858                         extractor = mpeg4_extractor;
859                         mime = MEDIA_FORMAT_MPEG4_SP;
860                 }
861                 break;
862         case MEDIACODEC_H263:
863                 if (encoder) {
864                         extractor = yuv_extractor;
865                         mime = *hardware ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
866                 } else {
867                         extractor = h263_extractor;
868                         mime = MEDIA_FORMAT_H263P;
869                 }
870                 break;
871         case MEDIACODEC_AAC:
872                 if (encoder) {
873                         extractor = aacenc_extractor;
874                         mime = MEDIA_FORMAT_PCM;
875                 } else {
876                         extractor = aacdec_extractor;
877                         mime = MEDIA_FORMAT_AAC;
878                 }
879                 break;
880         case MEDIACODEC_AAC_HE:
881                 if (encoder) {
882                         extractor = aacenc_extractor;
883                         mime = MEDIA_FORMAT_PCM;
884                 } else {
885                         extractor = extract_input_aacdec_m4a_test;
886                         mime = MEDIA_FORMAT_AAC_HE;
887                 }
888                 break;
889         case MEDIACODEC_AAC_HE_PS:
890                 break;
891         case MEDIACODEC_MP3:
892                 extractor = mp3dec_extractor;
893                 mime = MEDIA_FORMAT_MP3;
894                 break;
895         case MEDIACODEC_VORBIS:
896                 break;
897         case MEDIACODEC_FLAC:
898                 break;
899         case MEDIACODEC_WMAV1:
900                 break;
901         case MEDIACODEC_WMAV2:
902                 break;
903         case MEDIACODEC_WMAPRO:
904                 break;
905         case MEDIACODEC_WMALSL:
906                 break;
907         case MEDIACODEC_AMR_NB:
908                 if (encoder) {
909                         extractor = amrenc_extractor;
910                         mime = MEDIA_FORMAT_PCM;
911                         app->is_amr_nb = TRUE;
912                 } else {
913                         extractor = amrdec_extractor;
914                         mime = MEDIA_FORMAT_AMR_NB;
915                 }
916                 break;
917         case MEDIACODEC_AMR_WB:
918                 if (encoder) {
919                         extractor = amrenc_extractor;
920                         mime = MEDIA_FORMAT_PCM;
921                         app->is_amr_nb = FALSE;
922                 } else {
923                         extractor = amrdec_extractor;
924                         mime = MEDIA_FORMAT_AMR_WB;
925                 }
926                 break;
927         default:
928                 LOGE("NOT SUPPORTED!!!!");
929                 break;
930         }
931         return mime;
932 }
933
934 static void _mediacodec_process_input(App *app)
935 {
936         int i;
937         bool have_frame = FALSE;
938         int ret;
939         static guint64 pts = 0L;
940         void *buf_data_ptr = NULL;
941         media_packet_h pkt = NULL;
942         unsigned char *tmp;
943         int read;
944         int size;
945         int offset;
946         int stride_width;
947
948         for (i = 0; i < app->frame; i++) {
949                 g_print("----------read data------------\n");
950
951                 extractor(app, &tmp, &read, &have_frame);
952
953                 if (have_frame) {
954 #ifdef USE_POOL
955                 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
956                         g_print("media_packet_pool_aquire_packet failed\n");
957                         return;
958                 }
959 #else
960                 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
961                         g_print("media_packet_create_alloc failed\n");
962                         return;
963                 }
964 #endif
965
966                         if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
967                                 g_print("media_packet_set_pts failed\n");
968                                 return;
969                         }
970
971                         if (app->type != VIDEO_ENC) {
972                                 media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
973                                 media_packet_set_buffer_size(pkt, (uint64_t)read);
974
975                                 memcpy(buf_data_ptr, tmp, read);
976                                 g_print("tmp:%p, read:%d\n", tmp, read);
977                         } else {
978                                 /* Y */
979                                 media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
980                                 media_packet_get_video_stride_width(pkt, 0, &stride_width);
981                                 offset = app->width*app->height;
982
983                                 for (i = 0; i < app->height; i++) {
984                                         memcpy(buf_data_ptr, tmp, app->width);
985                                         buf_data_ptr += stride_width;
986                                         tmp += app->width;
987                                 }
988
989                                 if (app->hardware == TRUE) {
990                                         media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
991                                         media_packet_get_video_stride_width(pkt, 1, &stride_width);
992                                         size = app->width * app->height / 2;
993
994                                         for (i = 0; i < app->height / 2; i++) {
995                                                 memcpy(buf_data_ptr, tmp, app->width);
996                                                 buf_data_ptr += stride_width;
997                                                 tmp += app->width;
998                                         }
999                                 } else {
1000                                         /* U */
1001                                         media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1002                                         media_packet_get_video_stride_width(pkt, 1, &stride_width);
1003                                         size = (app->width>>1) * (app->height>>1);
1004
1005                                         for (i = 0; i < app->height/2; i++) {
1006                                                 memcpy(buf_data_ptr, tmp, app->width/2);
1007                                                 buf_data_ptr += stride_width;
1008                                                 tmp += app->width/2;
1009                                         }
1010
1011                                         /* V */
1012                                         media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1013                                         media_packet_get_video_stride_width(pkt, 2, &stride_width);
1014                                         offset += size;
1015
1016                                         for (i = 0; i < app->height/2; i++) {
1017                                                 memcpy(buf_data_ptr, tmp, app->width/2);
1018                                                 buf_data_ptr += stride_width;
1019                                                 tmp += app->width/2;
1020                                         }
1021
1022                                 }
1023                         }
1024                         mc_hex_dump("inbuf", tmp, 48);
1025
1026                         ret = mediacodec_process_input(app->mc_handle[0], pkt, 1000);
1027                         if (ret != MEDIACODEC_ERROR_NONE)
1028                                 return;
1029
1030                         pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1031                 }
1032         }
1033 }
1034
1035 static gboolean read_data(App *app)
1036 {
1037         guint len = 0;
1038         bool have_frame = FALSE;
1039         int ret;
1040         static guint64 pts = 0L;
1041         void *buf_data_ptr = NULL;
1042         media_packet_h pkt = NULL;
1043         unsigned char *tmp;
1044         int i;
1045         int read;
1046         int size;
1047         int offset;
1048         int stride_width;
1049
1050         if (app->offset == 0) {
1051                 app->frame_count = 0;
1052                 app->start = clock();
1053         }
1054
1055         g_print("----------read data------------\n");
1056         extractor(app, &tmp, &read, &have_frame);
1057
1058         if (app->offset >= app->length - 4) {
1059                 /* EOS */
1060                 g_print("EOS\n");
1061                 app->finish = clock();
1062                 g_print("Average FPS = %3.3f\n", ((double)app->frame_count*1000000/(app->finish - app->start)));
1063                 g_print("---------------------------\n");
1064                 return FALSE;
1065         }
1066         g_print("length : %d, offset : %d\n", app->length, app->offset);
1067
1068         if (app->offset + len > app->length)
1069                 len = app->length - app->offset;
1070
1071         g_print("%p, %d, have_frame :%d, read: %d\n", tmp, (int)read, have_frame, read);
1072
1073         if (have_frame) {
1074 #ifdef USE_POOL
1075                 if (media_packet_pool_acquire_packet(pkt_pool, &pkt, -1) != MEDIA_PACKET_ERROR_NONE) {
1076                         g_print("media_packet_pool_aquire_packet failed\n");
1077                         return FALSE;
1078                 }
1079 #else
1080                 if (media_packet_create_alloc(fmt, NULL, NULL, &pkt) != MEDIA_PACKET_ERROR_NONE) {
1081                         g_print("media_packet_create_alloc failed\n");
1082                         return FALSE;
1083                 }
1084 #endif
1085                 if (media_packet_set_pts(pkt, (uint64_t)(pts)) != MEDIA_PACKET_ERROR_NONE) {
1086                         g_print("media_packet_set_pts failed\n");
1087                         return FALSE;
1088                 }
1089
1090
1091                 if (app->type != VIDEO_ENC) {
1092                         media_packet_get_buffer_data_ptr(pkt, &buf_data_ptr);
1093                         media_packet_set_buffer_size(pkt, (uint64_t)read);
1094
1095                         memcpy(buf_data_ptr, tmp, read);
1096                         g_print("tmp:%p, read:%d\n", tmp, read);
1097                 } else {
1098                         /* Y */
1099                         media_packet_get_video_plane_data_ptr(pkt, 0, &buf_data_ptr);
1100                         media_packet_get_video_stride_width(pkt, 0, &stride_width);
1101                         offset = app->width*app->height;
1102
1103                         for (i = 0; i < app->height; i++) {
1104                                 memcpy(buf_data_ptr, tmp, app->width);
1105                                 buf_data_ptr += stride_width;
1106                                 tmp += app->width;
1107                         }
1108
1109                         if (app->hardware == TRUE) {
1110                                 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1111                                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1112                                 size = app->width * app->height>>1;
1113
1114                                 for (i = 0; i < app->height>>1; i++) {
1115                                         memcpy(buf_data_ptr, tmp, app->width);
1116                                         buf_data_ptr += stride_width;
1117                                         tmp += app->width;
1118                                 }
1119
1120                         } else {
1121                                 /* U */
1122                                 media_packet_get_video_plane_data_ptr(pkt, 1, &buf_data_ptr);
1123                                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1124                                 size = (app->width>>1) * (app->height>>1);
1125
1126                                 for (i = 0; i < app->height/2; i++) {
1127                                         memcpy(buf_data_ptr, tmp, app->width>>1);
1128                                         buf_data_ptr += stride_width;
1129                                         tmp += (app->width>>1);
1130                                 }
1131
1132                                 /* V */
1133                                 media_packet_get_video_plane_data_ptr(pkt, 2, &buf_data_ptr);
1134                                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1135                                 offset += size;
1136
1137                                 for (i = 0; i < app->height/2; i++) {
1138                                         memcpy(buf_data_ptr, tmp, app->width>>1);
1139                                         buf_data_ptr += stride_width;
1140                                         tmp += (app->width>>1);
1141                                 }
1142
1143                         }
1144                 }
1145                 mc_hex_dump("inbuf", tmp, 48);
1146
1147                 ret = mediacodec_process_input(app->mc_handle[0], pkt, 0);
1148                 if (ret != MEDIACODEC_ERROR_NONE)
1149                         return FALSE;
1150
1151                 pts += ES_DEFAULT_VIDEO_PTS_OFFSET;
1152         }
1153
1154         return TRUE;
1155 }
1156
1157 static void start_feed(App *app)
1158 {
1159         if (app->sourceid == 0) {
1160                 app->sourceid = g_idle_add((GSourceFunc)read_data, app);
1161                 g_print("start_feed\n");
1162         }
1163 }
1164
1165 static void stop_feed(App *app)
1166 {
1167         if (app->sourceid != 0) {
1168                 g_source_remove(app->sourceid);
1169                 app->sourceid = 0;
1170                 g_print("stop_feed\n");
1171         }
1172 }
1173
1174 static gboolean _mediacodec_inbuf_used_cb(media_packet_h pkt, void *user_data)
1175 {
1176         g_print("_mediacodec_inbuf_used_cb!!!\n");
1177 #ifdef USE_POOL
1178         media_packet_pool_release_packet(pkt_pool, pkt);
1179 #else
1180         media_packet_destroy(pkt);
1181 #endif
1182
1183         return TRUE;
1184 }
1185
1186 static bool _mediacodec_outbuf_available_cb(media_packet_h pkt, void *user_data)
1187 {
1188         media_packet_h out_pkt = NULL;
1189         int ret;
1190
1191         App *app = (App*)user_data;
1192
1193         g_print("_mediacodec_outbuf_available_cb\n");
1194
1195         g_mutex_lock(&app->lock);
1196
1197         ret = mediacodec_get_output(app->mc_handle[0], &out_pkt, 0);
1198
1199         if (ret != MEDIACODEC_ERROR_NONE)
1200                 g_print("get_output failed\n");
1201
1202         if (app->enable_dump) {
1203                 if (app->type == VIDEO_DEC)
1204                         decoder_output_dump(app, out_pkt);
1205                 else
1206                         output_dump(app, out_pkt);
1207         }
1208
1209         app->frame_count++;
1210
1211
1212         g_mutex_unlock(&app->lock);
1213
1214         media_packet_destroy(out_pkt);
1215         out_pkt = NULL;
1216         g_print("done\n");
1217
1218         return TRUE;
1219 }
1220
1221 static bool _mediacodec_buffer_status_cb(mediacodec_status_e status, void *user_data)
1222 {
1223         g_print("_mediacodec_buffer_status_cb %d\n", status);
1224
1225         App *app = (App*)user_data;
1226
1227         if (status == MEDIACODEC_NEED_DATA)
1228                 start_feed(app);
1229         else if (status == MEDIACODEC_ENOUGH_DATA)
1230                 stop_feed(app);
1231
1232         return TRUE;
1233 }
1234
1235 static bool _mediacodec_error_cb(mediacodec_error_e error, void *user_data)
1236 {
1237         return TRUE;
1238 }
1239
1240 static bool _mediacodec_eos_cb(void *user_data)
1241 {
1242         return TRUE;
1243 }
1244
1245 void _foreach_cb(mediacodec_codec_type_e codec_type, void *user_data)
1246 {
1247         g_print("codec type : %x %s\n", codec_type, codec_type_to_string(codec_type));
1248         return;
1249 }
1250
1251 static void _mediacodec_prepare(App *app, bool frame_all)
1252 {
1253         int ret;
1254
1255         g_print("supported codec lists -internal-\n");
1256         mediacodec_foreach_supported_codec_static((mediacodec_supported_codec_cb)_foreach_cb, app);
1257
1258         /* create instance */
1259         ret = mediacodec_create(&app->mc_handle[0]);
1260         if (ret  != MEDIACODEC_ERROR_NONE) {
1261                 g_print("mediacodec_create  failed\n");
1262                 return;
1263         }
1264
1265         /* set codec */
1266         ret = mediacodec_set_codec(app->mc_handle[0], app->codecid, app->flag);
1267         if (ret  != MEDIACODEC_ERROR_NONE) {
1268                 g_print("mediacodec_set_codec failed\n");
1269                 return;
1270         }
1271
1272         app->mime = _mediacodec_set_codec(app, app->codecid, app->flag, &app->hardware);
1273
1274         /* set codec info */
1275         ret = media_format_create(&fmt);
1276
1277         switch (app->type) {
1278         case VIDEO_DEC:
1279                 ret = mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1280                 media_format_set_video_mime(fmt, app->mime);
1281                 media_format_set_video_width(fmt, app->width);
1282                 media_format_set_video_height(fmt, app->height);
1283                 break;
1284         case VIDEO_ENC:
1285                 ret = mediacodec_set_venc_info(app->mc_handle[0], app->width, app->height, app->fps, app->target_bits);
1286                 media_format_set_video_mime(fmt, app->mime);
1287                 media_format_set_video_width(fmt, app->width);
1288                 media_format_set_video_height(fmt, app->height);
1289                 media_format_set_video_avg_bps(fmt, app->target_bits);
1290                 break;
1291         case AUDIO_DEC:
1292                 ret = mediacodec_set_adec_info(app->mc_handle[0], app->samplerate, app->channel, app->bit);
1293                 media_format_set_audio_mime(fmt, app->mime);
1294                 media_format_set_audio_channel(fmt, app->channel);
1295                 media_format_set_audio_samplerate(fmt, app->samplerate);
1296                 media_format_set_audio_bit(fmt, app->bit);
1297                 break;
1298         case AUDIO_ENC:
1299                 ret = mediacodec_set_aenc_info(app->mc_handle[0], app->samplerate, app->channel, app->bit, app->bitrate);
1300                 media_format_set_audio_mime(fmt, app->mime);
1301                 media_format_set_audio_channel(fmt, app->channel);
1302                 media_format_set_audio_samplerate(fmt, app->samplerate);
1303                 media_format_set_audio_bit(fmt, app->bit);
1304                 break;
1305         default:
1306                 g_print("invaild type\n");
1307                 break;
1308         }
1309
1310         if (ret  != MEDIACODEC_ERROR_NONE) {
1311                 g_print("mediacodec_set_xxxc(%d)_info failed\n", app->type);
1312                 return;
1313         }
1314
1315         /* set callback */
1316         g_print("supported codec lists\n");
1317         mediacodec_foreach_supported_codec(app->mc_handle[0], (mediacodec_supported_codec_cb)_foreach_cb, app);
1318         mediacodec_set_input_buffer_used_cb(app->mc_handle[0], (mediacodec_input_buffer_used_cb)_mediacodec_inbuf_used_cb, NULL);
1319         mediacodec_set_output_buffer_available_cb(app->mc_handle[0], (mediacodec_output_buffer_available_cb) _mediacodec_outbuf_available_cb, app);
1320         if (frame_all)
1321                 mediacodec_set_buffer_status_cb(app->mc_handle[0], (mediacodec_buffer_status_cb) _mediacodec_buffer_status_cb, app);
1322         mediacodec_set_eos_cb(app->mc_handle[0], (mediacodec_eos_cb)_mediacodec_eos_cb, NULL);
1323         mediacodec_set_error_cb(app->mc_handle[0], (mediacodec_error_cb)_mediacodec_error_cb, NULL);
1324
1325         /* prepare */
1326         ret = mediacodec_prepare(app->mc_handle[0]);
1327         if (ret  != MEDIACODEC_ERROR_NONE) {
1328                 g_print("mediacodec_prepare failed\n");
1329                 return;
1330         }
1331
1332
1333 /* get packet pool instance */
1334         ret = mediacodec_get_packet_pool(app->mc_handle[0], &pkt_pool);
1335         if (ret != MEDIA_PACKET_ERROR_NONE) {
1336                 g_print("mediacodec_get_packet_pool failed\n");
1337                 return;
1338         }
1339         g_print("\n\nmediacodec start\n\n");
1340
1341         return;
1342 }
1343
1344 static void _mediacodec_enc_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1345 {
1346         /* release input raw packet */
1347         media_packet_destroy(pkt);
1348 }
1349
1350 /* this callback is called when the input buffer for codec has done to use */
1351 static void _mediacodec_dec_input_buffer_used_cb(media_packet_h pkt, void *user_data)
1352 {
1353         /* release input encoded packet */
1354         media_packet_destroy(pkt);
1355 }
1356
1357 static void _mediacodec_enc_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1358 {
1359         App *app = (App*)user_data;
1360
1361         mediacodec_h media_codec_handle = app->mc_handle[1];
1362         media_packet_h output_buf = NULL;
1363         mediacodec_get_output(media_codec_handle, &output_buf, 0);
1364         /* decode encoded camera preview */
1365         mediacodec_process_input(app->mc_handle[0], output_buf, 0);
1366 }
1367
1368 static void _mediacodec_dec_output_buffer_available_cb(media_packet_h pkt, void *user_data)
1369 {
1370         App *app = (App*)user_data;
1371
1372         mediacodec_h media_codec_handle = app->mc_handle[0];
1373         media_packet_h output_buf = NULL;
1374
1375         mediacodec_get_output(media_codec_handle, &output_buf, 0);
1376
1377         if (app->enable_dump)
1378                 decoder_output_dump(app, output_buf);
1379
1380         media_packet_destroy(output_buf);
1381 }
1382
1383 static void _media_packet_preview_cb(media_packet_h packet, void *user_data)
1384 {
1385         App *app = user_data;
1386         g_mutex_lock(&app->lock);
1387         mediacodec_process_input(app->mc_handle[1], packet, 0);
1388         g_mutex_unlock(&app->lock);
1389
1390         return;
1391 }
1392
1393 static void _mediacodec_camera_start(App *app)
1394 {
1395         int default_format = CAMERA_PIXEL_FORMAT_NV12;
1396         app->width = 640;
1397         app->height = 480;
1398         app->hardware = TRUE;
1399
1400         /*create decoder instance and setup */
1401         mediacodec_create(&app->mc_handle[0]);
1402         mediacodec_set_codec(app->mc_handle[0], MEDIACODEC_H264, MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW);
1403         mediacodec_set_vdec_info(app->mc_handle[0], app->width, app->height);
1404         /* set callback */
1405         mediacodec_set_input_buffer_used_cb(app->mc_handle[0], _mediacodec_dec_input_buffer_used_cb, NULL);
1406         mediacodec_set_output_buffer_available_cb(app->mc_handle[0], _mediacodec_dec_output_buffer_available_cb, app);
1407         mediacodec_prepare(app->mc_handle[0]);
1408
1409         /*create encoder instance and setup */
1410         mediacodec_create(&app->mc_handle[1]);
1411         mediacodec_set_codec(app->mc_handle[1], MEDIACODEC_H264, MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW);
1412         mediacodec_set_venc_info(app->mc_handle[1], app->width, app->height, 30, 1000);
1413         /* set callback */
1414         mediacodec_set_input_buffer_used_cb(app->mc_handle[1], _mediacodec_enc_input_buffer_used_cb, NULL);
1415         mediacodec_set_output_buffer_available_cb(app->mc_handle[1], _mediacodec_enc_output_buffer_available_cb, app);
1416         mediacodec_prepare(app->mc_handle[1]);
1417
1418         /* create camera instance and setup and then start preview */
1419         camera_create(CAMERA_DEVICE_CAMERA0, &app->camera_handle);
1420         camera_set_media_packet_preview_cb(app->camera_handle, _media_packet_preview_cb, app);
1421         camera_get_preview_format(app->camera_handle, &default_format);
1422         camera_set_preview_format(app->camera_handle, default_format);
1423         camera_set_preview_resolution(app->camera_handle, app->width, app->height);
1424         camera_set_display(app->camera_handle, CAMERA_DISPLAY_TYPE_NONE, NULL);
1425         camera_start_preview(app->camera_handle);
1426
1427         return;
1428 }
1429
1430
1431 static void _mediacodec_camera_stop(App *app)
1432 {
1433         camera_state_e camera_state = CAMERA_STATE_NONE;
1434
1435         camera_get_state(app->camera_handle, &camera_state);
1436         camera_stop_preview(app->camera_handle);
1437         camera_destroy(app->camera_handle);
1438
1439         mediacodec_unprepare(app->mc_handle[0]);
1440         mediacodec_unprepare(app->mc_handle[1]);
1441         mediacodec_destroy(app->mc_handle[0]);
1442         mediacodec_destroy(app->mc_handle[1]);
1443         return;
1444 }
1445
1446 static void _mediacodec_unprepare(App *app)
1447 {
1448         mediacodec_unprepare(app->mc_handle[0]);
1449 }
1450
1451 static void _mediacodec_destroy(App *app)
1452 {
1453 #ifdef USE_POOL
1454         if (media_packet_pool_deallocate(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1455
1456                 g_print("media_packet_pool_deallocatet failed\n");
1457                 return;
1458         }
1459
1460         if (media_packet_pool_destroy(pkt_pool) != MEDIA_PACKET_ERROR_NONE) {
1461
1462                 g_print(" media_packet_pool_destroy failed\n");
1463                 return;
1464         }
1465         g_print("media packet pool destroyed! \n");
1466 #endif
1467         mediacodec_destroy(app->mc_handle[0]);
1468 }
1469
1470 static void input_filepath(char *filename, App *app)
1471 {
1472         GError *error = NULL;
1473
1474         app->obj++;
1475         app->file = g_mapped_file_new(filename, FALSE, &error);
1476         if (error) {
1477                 g_print("failed to open file : %s\n", error->message);
1478                 g_error_free(error);
1479                 return;
1480         }
1481
1482         app->length = g_mapped_file_get_length(app->file);
1483         app->data = (guint8 *)g_mapped_file_get_contents(app->file);
1484         app->offset = 0;
1485         g_print("len : %d, offset : %d, obj : %d", app->length, app->offset, app->obj);
1486
1487         return;
1488 }
1489
1490 void quit_program(App *app)
1491 {
1492                 media_format_unref(fmt);
1493                 g_main_loop_quit(app->loop);
1494                 elm_exit();
1495
1496 }
1497
1498 void reset_menu_state()
1499 {
1500         g_menu_state = CURRENT_STATUS_MAINMENU;
1501         return;
1502 }
1503
1504 void _interpret_main_menu(char *cmd, App *app)
1505 {
1506         int len =  strlen(cmd);
1507         if (len == 1) {
1508                 if (strncmp(cmd, "a", 1) == 0)
1509                         g_menu_state = CURRENT_STATUS_FILENAME;
1510                 else if (strncmp(cmd, "o", 1) == 0)
1511                         g_menu_state = CURRENT_STATUS_GET_OUTPUT;
1512                 else if (strncmp(cmd, "q", 1) == 0)
1513                         quit_program(app);
1514                 else
1515                         g_print("unknown menu \n");
1516         } else if (len == 2) {
1517                 if (strncmp(cmd, "pr", 2) == 0)
1518                         _mediacodec_prepare(app, 0);
1519                 else if (strncmp(cmd, "pa", 2) == 0)
1520                         _mediacodec_prepare(app, 1);
1521                 else if (strncmp(cmd, "sc", 2) == 0)
1522                         g_menu_state = CURRENT_STATUS_SET_CODEC;
1523                 else if (strncmp(cmd, "vd", 2) == 0)
1524                         g_menu_state = CURRENT_STATUS_SET_VDEC_INFO;
1525                 else if (strncmp(cmd, "ve", 2) == 0)
1526                         g_menu_state = CURRENT_STATUS_SET_VENC_INFO;
1527                 else if (strncmp(cmd, "ad", 2) == 0)
1528                         g_menu_state = CURRENT_STATUS_SET_ADEC_INFO;
1529                 else if (strncmp(cmd, "ae", 2) == 0)
1530                         g_menu_state = CURRENT_STATUS_SET_AENC_INFO;
1531                 else if (strncmp(cmd, "pi", 2) == 0)
1532                         g_menu_state = CURRENT_STATUS_PROCESS_INPUT;
1533                 else if (strncmp(cmd, "un", 2) == 0)
1534                         _mediacodec_unprepare(app);
1535                 else if (strncmp(cmd, "dt", 2) == 0)
1536                         _mediacodec_destroy(app);
1537                 else if (strncmp(cmd, "cr", 2) == 0)
1538                         _mediacodec_camera_start(app);
1539                 else if (strncmp(cmd, "ct", 2) == 0)
1540                         _mediacodec_camera_stop(app);
1541                 else if (strncmp(cmd, "dp", 2) == 0) {
1542                         if (!app->enable_dump) {
1543                                 app->enable_dump = TRUE;
1544                                 g_print("dump enabled\n");
1545                         } else {
1546                                 app->enable_dump = FALSE;
1547                                 g_print("dump disabled\n");
1548                         }
1549                 } else
1550                         display_sub_basic();
1551         } else {
1552                 g_print("unknown menu \n");
1553         }
1554
1555         return;
1556 }
1557
1558 static void displaymenu(void)
1559 {
1560         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
1561                 display_sub_basic();
1562         } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
1563                 g_print("*** input mediapath.\n");
1564         } else if (g_menu_state == CURRENT_STATUS_SET_CODEC) {
1565                 g_print("*** Codec id : Select Codec ID Numbe  (e.g. AAC_LC = 96)\n");
1566                 g_print("               L16    =  16 (0x10)\n");
1567                 g_print("               ALAW   =  32 (0x20)\n");
1568                 g_print("               ULAW   =  48 (0x30)\n");
1569                 g_print("               AMR_NB =  64 (0x40)\n");
1570                 g_print("               AMR_WB =  65 (0x41)\n");
1571                 g_print("               G729   =  80 (0x50)\n");
1572                 g_print("               AAC_LC =  96 (0x60)\n");
1573                 g_print("               AAC_HE =  97 (0x61)\n");
1574                 g_print("               AAC_PS =  98 (0x62)\n");
1575                 g_print("               MP3    = 112 (0x70)\n");
1576                 g_print("               VORBIS = 128 (0x80)\n");
1577                 g_print("               FLAC   = 144 (0x90)\n");
1578                 g_print("               WMAV1  = 160 (0xA0)\n");
1579                 g_print("               WMAV2  = 161 (0xA1)\n");
1580                 g_print("               WMAPRO = 162 (0xA2)\n");
1581                 g_print("               WMALSL = 163 (0xA3)\n");
1582                 g_print("               -------------------\n");
1583                 g_print("               H261   = 101\n");
1584                 g_print("               H263   = 102\n");
1585                 g_print("               H264   = 103\n");
1586                 g_print("               MJPEG  = 104\n");
1587                 g_print("               MPEG1  = 105\n");
1588                 g_print("               MPEG2  = 106\n");
1589                 g_print("               MPEG4  = 107\n");
1590                 g_print("               -------------------\n");
1591                 g_print("*** Flags : Select Combination Number (e.g. DEOCDER + TYPE_SW = 10)\n");
1592                 g_print("               CODEC : ENCODER =  1       DECODER =  2\n");
1593                 g_print("               TYPE  : HW      =  4       SW      =  8\n");
1594                 g_print("*** input codec id, falgs.\n");
1595         } else if (g_menu_state == CURRENT_STATUS_SET_VDEC_INFO) {
1596                 g_print("*** input video decode configure.(width, height)\n");
1597         } else if (g_menu_state == CURRENT_STATUS_SET_VENC_INFO) {
1598                 g_print("*** input video encode configure.(width, height, fps, target_bits)\n");
1599         } else if (g_menu_state == CURRENT_STATUS_SET_ADEC_INFO) {
1600                 g_print("*** input audio decode configure.(samplerate, channel, bit (e.g. 48000,  2, 16))\n");
1601         } else if (g_menu_state == CURRENT_STATUS_SET_AENC_INFO) {
1602                 g_print("*** input audio encode configure.(samplerate, channel, bit, bitrate (e.g. 48000,  2, 16, 128000))\n");
1603         } else if (g_menu_state == CURRENT_STATUS_PROCESS_INPUT) {
1604                 g_print("*** input dec process number\n");
1605         } else if (g_menu_state == CURRENT_STATUS_GET_OUTPUT) {
1606                 g_print("*** input get output buffer number\n");
1607         } else {
1608                 g_print("*** unknown status.\n");
1609         }
1610         g_print(" >>> ");
1611 }
1612
1613 gboolean timeout_menu_display(void* data)
1614 {
1615         displaymenu();
1616         return FALSE;
1617 }
1618
1619
1620 static void interpret(char *cmd, App *app)
1621 {
1622         switch (g_menu_state) {
1623         case CURRENT_STATUS_MAINMENU:
1624                 _interpret_main_menu(cmd, app);
1625                 break;
1626         case CURRENT_STATUS_FILENAME:
1627                 input_filepath(cmd, app);
1628                 reset_menu_state();
1629                 break;
1630         case CURRENT_STATUS_SET_CODEC:
1631         {
1632                 int tmp;
1633                 static int cnt = 0;
1634                 char **ptr = NULL;
1635                 switch (cnt) {
1636                 case 0:
1637                         tmp = atoi(cmd);
1638
1639                         if (tmp > 100 &&
1640                                 (tmp != 112) &&
1641                                 (tmp != 128) &&
1642                                 (tmp != 144) &&
1643                                 (tmp != 160) && (tmp != 161) && (tmp != 162) && (tmp != 163)) {
1644                                         tmp = strtol(cmd, ptr, 16);
1645                                         app->codecid = 0x2000 + ((tmp & 0xFF) << 4);
1646                         } else
1647                                 app->codecid = 0x1000 + tmp;
1648
1649                         cnt++;
1650                         break;
1651                 case 1:
1652                         app->flag = atoi(cmd);
1653                         cnt = 0;
1654                         reset_menu_state();
1655                         break;
1656                 default:
1657                         break;
1658                 }
1659         }
1660         break;
1661         case CURRENT_STATUS_SET_VDEC_INFO:
1662         {
1663                 static int cnt = 0;
1664                 switch (cnt) {
1665                 case 0:
1666                         app->width = atoi(cmd);
1667                         cnt++;
1668                         break;
1669                 case 1:
1670                         app->height = atoi(cmd);
1671                         app->type = VIDEO_DEC;
1672
1673                         reset_menu_state();
1674                         cnt = 0;
1675                         break;
1676                 default:
1677                         break;
1678                 }
1679         }
1680         break;
1681         case CURRENT_STATUS_SET_VENC_INFO:
1682         {
1683                 static int cnt = 0;
1684                 switch (cnt) {
1685                 case 0:
1686                         app->width = atoi(cmd);
1687                         cnt++;
1688                         break;
1689                 case 1:
1690                         app->height = atoi(cmd);
1691                         cnt++;
1692                         break;
1693                 case 2:
1694                         app->fps = atol(cmd);
1695                         cnt++;
1696                         break;
1697                 case 3:
1698                         app->target_bits = atoi(cmd);
1699                         app->type = VIDEO_ENC;
1700
1701                         reset_menu_state();
1702                         cnt = 0;
1703                         break;
1704                 default:
1705                         break;
1706                 }
1707         }
1708         break;
1709         case CURRENT_STATUS_SET_ADEC_INFO:
1710         {
1711                 static int cnt = 0;
1712                 switch (cnt) {
1713                 case 0:
1714                         app->samplerate = atoi(cmd);
1715                         cnt++;
1716                         break;
1717                 case 1:
1718                         app->channel = atoi(cmd);
1719                         cnt++;
1720                         break;
1721                 case 2:
1722                         app->bit = atoi(cmd);
1723                         app->type = AUDIO_DEC;
1724
1725                         reset_menu_state();
1726                         cnt = 0;
1727                         break;
1728                 default:
1729                         break;
1730                 }
1731         }
1732         break;
1733         case CURRENT_STATUS_SET_AENC_INFO:
1734         {
1735                 static int cnt = 0;
1736                 switch (cnt) {
1737                 case 0:
1738                         app->samplerate = atoi(cmd);
1739                         cnt++;
1740                         break;
1741                 case 1:
1742                         app->channel = atoi(cmd);
1743                         cnt++;
1744                         break;
1745                 case 2:
1746                         app->bit = atoi(cmd);
1747                         cnt++;
1748                         break;
1749                 case 3:
1750                         app->bitrate = atoi(cmd);
1751                         app->type = AUDIO_ENC;
1752
1753                         reset_menu_state();
1754                         cnt = 0;
1755                         break;
1756                 default:
1757                         break;
1758                 }
1759         }
1760         break;
1761         case CURRENT_STATUS_PROCESS_INPUT:
1762         {
1763                 app->frame = atoi(cmd);
1764
1765                 if (app->frame > 0)
1766                         _mediacodec_process_input(app);
1767                 reset_menu_state();
1768         }
1769         break;
1770         case CURRENT_STATUS_GET_OUTPUT:
1771         {
1772                 reset_menu_state();
1773         }
1774         break;
1775         default:
1776                 break;
1777         }
1778
1779         g_timeout_add(100, timeout_menu_display, 0);
1780 }
1781
1782 static void display_sub_basic()
1783 {
1784         g_print("\n");
1785         g_print("=========================================================================================\n");
1786         g_print("                                    media codec test\n");
1787         g_print("-----------------------------------------------------------------------------------------\n");
1788         g_print("a. Create \t\t");
1789         g_print("sc. Set codec \n");
1790         g_print("vd. Set vdec info \t");
1791         g_print("ve. Set venc info \n");
1792         g_print("ad. Set adec info \t");
1793         g_print("ae. Set aenc info \n");
1794         g_print("pr. Prepare \t");
1795         g_print("pa. Prepare and process all\t\t");
1796         g_print("pi. process input with num\n");
1797         g_print("o. Get output \t\t");
1798         g_print("rb. Reset output buffer \n");
1799         g_print("un. Unprepare \t\t");
1800         g_print("dt. Destroy \t\t");
1801         g_print("q. quit test suite \n");
1802         g_print("dp. enable dump \n");
1803         g_print("-----------------------------------------------------------------------------------------\n");
1804         g_print("cr. camera preview -> encoder -> decoder\n");
1805         g_print("ct. quit camera test\n");
1806         g_print("\n");
1807         g_print("=========================================================================================\n");
1808 }
1809
1810 gboolean input(GIOChannel *channel, GIOCondition cond, gpointer data)
1811 {
1812         gchar buf[MAX_STRING_LEN];
1813         gsize read;
1814         GError *error = NULL;
1815         App *context = (App*)data;
1816
1817         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1818         buf[read] = '\0';
1819         g_strstrip(buf);
1820         interpret(buf, context);
1821
1822         return TRUE;
1823 }
1824
1825 int main(int argc, char *argv[])
1826 {
1827         App *app = &s_app;
1828
1829         GIOChannel *stdin_channel;
1830         stdin_channel = g_io_channel_unix_new(0);
1831         g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1832         g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, app);
1833
1834
1835         displaymenu();
1836         app->loop = g_main_loop_new(NULL, TRUE);
1837         app->timer = g_timer_new();
1838         g_main_loop_run(app->loop);
1839
1840
1841
1842         ops.data = app;
1843
1844         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
1845 }
1846
1847
1848
1849 void mc_hex_dump(char *desc, void *addr, int len)
1850 {
1851         int i;
1852         unsigned char buff[17];
1853         unsigned char *pc = (unsigned char *)addr;
1854
1855         if (desc != NULL)
1856                 g_print("%s:\n", desc);
1857
1858         for (i = 0; i < len; i++) {
1859
1860                 if ((i % 16) == 0) {
1861                         if (i != 0)
1862                                 g_print("  %s\n", buff);
1863
1864                         g_print("  %04x ", i);
1865                 }
1866
1867                 g_print(" %02x", pc[i]);
1868
1869                 if ((pc[i] < 0x20) || (pc[i] > 0x7e))
1870                         buff[i % 16] = '.';
1871                 else
1872                         buff[i % 16] = pc[i];
1873                 buff[(i % 16) + 1] = '\0';
1874         }
1875
1876         while ((i % 16) != 0) {
1877                 g_print("   ");
1878                 i++;
1879         }
1880         g_print("  %s\n", buff);
1881 }
1882
1883 static void decoder_output_dump(App *app, media_packet_h pkt)
1884 {
1885         void *temp;
1886         int i = 0;
1887         int stride_width, stride_height;
1888         gchar filename[100] = {0};
1889         FILE *fp = NULL;
1890         int ret = 0;
1891
1892         g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d_%d.yuv", app->width, app->height);
1893         fp = fopen(filename, "ab");
1894
1895         media_packet_get_video_plane_data_ptr(pkt, 0, &temp);
1896         media_packet_get_video_stride_width(pkt, 0, &stride_width);
1897         media_packet_get_video_stride_height(pkt, 0, &stride_height);
1898         g_print("stride : %d, %d\n", stride_width, stride_height);
1899
1900         for (i = 0; i < app->height; i++) {
1901                 ret = fwrite(temp, app->width, 1, fp);
1902                 temp += stride_width;
1903         }
1904
1905         if (app->hardware == TRUE) {
1906                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1907                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1908                 for (i = 0; i < app->height/2; i++) {
1909                         ret = fwrite(temp, app->width, 1, fp);
1910                         temp += stride_width;
1911                 }
1912         } else {
1913                 media_packet_get_video_plane_data_ptr(pkt, 1, &temp);
1914                 media_packet_get_video_stride_width(pkt, 1, &stride_width);
1915                 for (i = 0; i < app->height/2; i++) {
1916                         ret = fwrite(temp, app->width/2, 1, fp);
1917                         temp += stride_width;
1918                 }
1919
1920                 media_packet_get_video_plane_data_ptr(pkt, 2, &temp);
1921                 media_packet_get_video_stride_width(pkt, 2, &stride_width);
1922                 for (i = 0; i < app->height/2; i++) {
1923                         ret = fwrite(temp, app->width/2, 1, fp);
1924                         temp += stride_width;
1925                 }
1926         }
1927
1928         g_print("codec dec output dumped!!%d\n", ret);
1929         fclose(fp);
1930
1931 }
1932
1933 /**
1934  *  Add ADTS header at the beginning of each and every AAC packet.
1935  *  This is needed as MediaCodec encoder generates a packet of raw AAC data.
1936  *  Note the packetLen must count in the ADTS header itself.
1937  **/
1938 void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
1939 {
1940         int profile = 2;    /* AAC LC (0x01) */
1941         int freqIdx = 3;    /* 48KHz (0x03) */
1942         int chanCfg = 2;    /* CPE (0x02) */
1943
1944         if (app->samplerate == 96000) freqIdx = 0;
1945         else if (app->samplerate == 88200) freqIdx = 1;
1946         else if (app->samplerate == 64000) freqIdx = 2;
1947         else if (app->samplerate == 48000) freqIdx = 3;
1948         else if (app->samplerate == 44100) freqIdx = 4;
1949         else if (app->samplerate == 32000) freqIdx = 5;
1950         else if (app->samplerate == 24000) freqIdx = 6;
1951         else if (app->samplerate == 22050) freqIdx = 7;
1952         else if (app->samplerate == 16000) freqIdx = 8;
1953         else if (app->samplerate == 12000) freqIdx = 9;
1954         else if (app->samplerate == 11025) freqIdx = 10;
1955         else if (app->samplerate == 8000) freqIdx = 11;
1956
1957         if ((app->channel == 1) || (app->channel == 2))
1958                 chanCfg = app->channel;
1959
1960         /* fill in ADTS data */
1961         buffer[0] = (char)0xFF;
1962         buffer[1] = (char)0xF1;
1963         buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
1964         buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
1965         buffer[4] = (char)((packetLen&0x7FF) >> 3);
1966         buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
1967         buffer[6] = (char)0xFC;
1968 }
1969
1970 static void output_dump(App *app, media_packet_h pkt)
1971 {
1972         void *temp;
1973         uint64_t buf_size;
1974         gchar filename[100] = {0};
1975         FILE *fp = NULL;
1976         int ret = 0;
1977         char adts[100] = {0, };
1978
1979         g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
1980         fp = fopen(filename, "ab");
1981
1982         media_packet_get_buffer_data_ptr(pkt, &temp);
1983         media_packet_get_buffer_size(pkt, &buf_size);
1984         g_print("output data : %p, size %d\n", temp, (int)buf_size);
1985
1986         if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
1987                 add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
1988                 fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
1989                 g_print("adts appended\n");
1990         } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1)       {
1991                 /* This is used only AMR encoder case for adding AMR masic header in only first frame */
1992                 g_print("%s - AMR_header write in first frame\n", __func__);
1993                 fwrite(&AMR_header[0], 1, sizeof(AMR_header)   - 1, fp);         /* AMR-NB magic number */
1994                 write_amr_header = 0;
1995         }
1996
1997         fwrite(temp, (int)buf_size, 1, fp);
1998
1999         g_print("codec dec output dumped!!%d\n", ret);
2000         fclose(fp);
2001
2002 }
2003
2004 const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
2005 {
2006         guint media_codec_id_u = (guint)media_codec_id;
2007
2008         switch (media_codec_id_u) {
2009         case MEDIACODEC_L16:
2010                 return "L16";
2011         case MEDIACODEC_ALAW:
2012                 return "ALAW";
2013         case MEDIACODEC_ULAW:
2014                 return "ULAW";
2015         case MEDIACODEC_AMR_NB:
2016                 return "AMR_NB";
2017         case MEDIACODEC_AMR_WB:
2018                 return "AMR_WB";
2019         case MEDIACODEC_G729:
2020                 return "G729";
2021         case MEDIACODEC_AAC_LC:
2022                 return "AAC_LC";
2023         case MEDIACODEC_AAC_HE:
2024                 return "AAC_HE";
2025         case MEDIACODEC_AAC_HE_PS:
2026                 return "AAC_HE_PS";
2027         case MEDIACODEC_MP3:
2028                 return "MP3";
2029         case MEDIACODEC_VORBIS:
2030                 return "VORBIS";
2031         case MEDIACODEC_FLAC:
2032                 return "FLAC";
2033         case MEDIACODEC_WMAV1:
2034                 return "WMAV1";
2035         case MEDIACODEC_WMAV2:
2036                 return "WMAV2";
2037         case MEDIACODEC_WMAPRO:
2038                 return "WMAPRO";
2039         case MEDIACODEC_WMALSL:
2040                 return "WMALSL";
2041         case MEDIACODEC_H261:
2042                 return "H261";
2043         case MEDIACODEC_H263:
2044                 return "H263";
2045         case MEDIACODEC_H264:
2046                 return "H264";
2047         case MEDIACODEC_MJPEG:
2048                 return "MJPEG";
2049         case MEDIACODEC_MPEG1:
2050                 return "MPEG1";
2051         case MEDIACODEC_MPEG2:
2052                 return "MPEG2";
2053         case MEDIACODEC_MPEG4:
2054                 return "MPEG4";
2055         case MEDIACODEC_HEVC:
2056                 return "HEVC";
2057         case MEDIACODEC_VP8:
2058                 return "VP8";
2059         case MEDIACODEC_VP9:
2060                 return "VP9";
2061         case MEDIACODEC_VC1:
2062                 return "VC1";
2063         default:
2064                 return "NONE";
2065         }
2066 }
2067