+
+/**
+ * Add ADTS header at the beginning of each and every AAC packet.
+ * This is needed as MediaCodec encoder generates a packet of raw AAC data.
+ * Note the packetLen must count in the ADTS header itself.
+ **/
+void add_adts_header_for_aacenc(App *app, char *buffer, int packetLen)
+{
+ int profile = 2; /* AAC LC (0x01) */
+ int freqIdx = 3; /* 48KHz (0x03) */
+ int chanCfg = 2; /* CPE (0x02) */
+
+ if (app->samplerate == 96000) freqIdx = 0;
+ else if (app->samplerate == 88200) freqIdx = 1;
+ else if (app->samplerate == 64000) freqIdx = 2;
+ else if (app->samplerate == 48000) freqIdx = 3;
+ else if (app->samplerate == 44100) freqIdx = 4;
+ else if (app->samplerate == 32000) freqIdx = 5;
+ else if (app->samplerate == 24000) freqIdx = 6;
+ else if (app->samplerate == 22050) freqIdx = 7;
+ else if (app->samplerate == 16000) freqIdx = 8;
+ else if (app->samplerate == 12000) freqIdx = 9;
+ else if (app->samplerate == 11025) freqIdx = 10;
+ else if (app->samplerate == 8000) freqIdx = 11;
+
+ if ((app->channel == 1) || (app->channel == 2))
+ chanCfg = app->channel;
+
+ /* fill in ADTS data */
+ buffer[0] = (char)0xFF;
+ buffer[1] = (char)0xF1;
+ buffer[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
+ buffer[3] = (char)(((chanCfg&3)<<6) + (packetLen>>11));
+ buffer[4] = (char)((packetLen&0x7FF) >> 3);
+ buffer[5] = (char)(((packetLen&7)<<5) + 0x1F);
+ buffer[6] = (char)0xFC;
+}
+
+static void output_dump(App *app, media_packet_h pkt)
+{
+ void *temp;
+ uint64_t buf_size;
+ gchar filename[100] = {0};
+ FILE *fp = NULL;
+ int ret = 0;
+ char adts[100] = {0, };
+
+ g_snprintf(filename, MAX_STRING_LEN, "/tmp/dec_output_dump_%d.out", app->type);
+ fp = fopen(filename, "ab");
+
+ media_packet_get_buffer_data_ptr(pkt, &temp);
+ media_packet_get_buffer_size(pkt, &buf_size);
+ g_print("output data : %p, size %d\n", temp, (int)buf_size);
+
+ if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AAC_LC) {
+ add_adts_header_for_aacenc(app, adts, (buf_size + ADTS_HEADER_SIZE));
+ fwrite(&adts, 1, ADTS_HEADER_SIZE, fp);
+ g_print("adts appended\n");
+ } else if (app->is_encoder && buf_size > 0 && app->codecid == MEDIACODEC_AMR_NB && write_amr_header == 1) {
+ /* This is used only AMR encoder case for adding AMR masic header in only first frame */
+ g_print("%s - AMR_header write in first frame\n", __func__);
+ fwrite(&AMR_header[0], 1, sizeof(AMR_header) - 1, fp); /* AMR-NB magic number */
+ write_amr_header = 0;
+ }
+
+ fwrite(temp, (int)buf_size, 1, fp);
+
+ g_print("codec dec output dumped!!%d\n", ret);
+ fclose(fp);
+
+}
+
+const char* codec_type_to_string(mediacodec_codec_type_e media_codec_id)
+{
+ guint media_codec_id_u = (guint)media_codec_id;
+
+ switch (media_codec_id_u) {
+ case MEDIACODEC_L16:
+ return "L16";
+ case MEDIACODEC_ALAW:
+ return "ALAW";
+ case MEDIACODEC_ULAW:
+ return "ULAW";
+ case MEDIACODEC_AMR_NB:
+ return "AMR_NB";
+ case MEDIACODEC_AMR_WB:
+ return "AMR_WB";
+ case MEDIACODEC_G729:
+ return "G729";
+ case MEDIACODEC_AAC_LC:
+ return "AAC_LC";
+ case MEDIACODEC_AAC_HE:
+ return "AAC_HE";
+ case MEDIACODEC_AAC_HE_PS:
+ return "AAC_HE_PS";
+ case MEDIACODEC_MP3:
+ return "MP3";
+ case MEDIACODEC_VORBIS:
+ return "VORBIS";
+ case MEDIACODEC_FLAC:
+ return "FLAC";
+ case MEDIACODEC_WMAV1:
+ return "WMAV1";
+ case MEDIACODEC_WMAV2:
+ return "WMAV2";
+ case MEDIACODEC_WMAPRO:
+ return "WMAPRO";
+ case MEDIACODEC_WMALSL:
+ return "WMALSL";
+ case MEDIACODEC_H261:
+ return "H261";
+ case MEDIACODEC_H263:
+ return "H263";
+ case MEDIACODEC_H264:
+ return "H264";
+ case MEDIACODEC_MJPEG:
+ return "MJPEG";
+ case MEDIACODEC_MPEG1:
+ return "MPEG1";
+ case MEDIACODEC_MPEG2:
+ return "MPEG2";
+ case MEDIACODEC_MPEG4:
+ return "MPEG4";
+ case MEDIACODEC_HEVC:
+ return "HEVC";
+ case MEDIACODEC_VP8:
+ return "VP8";
+ case MEDIACODEC_VP9:
+ return "VP9";
+ case MEDIACODEC_VC1:
+ return "VC1";
+ default:
+ return "NONE";
+ }
+}
+