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