📄 main.c
字号:
fprintf(stderr, "Error opening file: %s\n", adts_fn); return 1; } } b.infile = fopen(aacfile, "rb"); if (b.infile == NULL) { /* unable to open file */ fprintf(stderr, "Error opening file: %s\n", aacfile); return 1; } fseek(b.infile, 0, SEEK_END); fileread = ftell(b.infile); fseek(b.infile, 0, SEEK_SET); if (!(b.buffer = (unsigned char*)malloc(FAAD_MIN_STREAMSIZE*MAX_CHANNELS))) { fprintf(stderr, "Memory allocation error\n"); return 0; } memset(b.buffer, 0, FAAD_MIN_STREAMSIZE*MAX_CHANNELS); bread = fread(b.buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, b.infile); b.bytes_into_buffer = bread; b.bytes_consumed = 0; b.file_offset = 0; if (bread != FAAD_MIN_STREAMSIZE*MAX_CHANNELS) b.at_eof = 1; tagsize = 0; if (!memcmp(b.buffer, "ID3", 3)) { /* high bit is not used */ tagsize = (b.buffer[6] << 21) | (b.buffer[7] << 14) | (b.buffer[8] << 7) | (b.buffer[9] << 0); tagsize += 10; advance_buffer(&b, tagsize); fill_buffer(&b); } hDecoder = NeAACDecOpen(); /* Set the default object type and samplerate */ /* This is useful for RAW AAC files */ config = NeAACDecGetCurrentConfiguration(hDecoder); if (def_srate) config->defSampleRate = def_srate; config->defObjectType = object_type; config->outputFormat = outputFormat; config->downMatrix = downMatrix; config->useOldADTSFormat = old_format; //config->dontUpSampleImplicitSBR = 1; NeAACDecSetConfiguration(hDecoder, config); /* get AAC infos for printing */ header_type = 0; if ((b.buffer[0] == 0xFF) && ((b.buffer[1] & 0xF6) == 0xF0)) { adts_parse(&b, &bitrate, &length); fseek(b.infile, tagsize, SEEK_SET); bread = fread(b.buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, b.infile); if (bread != FAAD_MIN_STREAMSIZE*MAX_CHANNELS) b.at_eof = 1; else b.at_eof = 0; b.bytes_into_buffer = bread; b.bytes_consumed = 0; b.file_offset = tagsize; header_type = 1; } else if (memcmp(b.buffer, "ADIF", 4) == 0) { int skip_size = (b.buffer[4] & 0x80) ? 9 : 0; bitrate = ((unsigned int)(b.buffer[4 + skip_size] & 0x0F)<<19) | ((unsigned int)b.buffer[5 + skip_size]<<11) | ((unsigned int)b.buffer[6 + skip_size]<<3) | ((unsigned int)b.buffer[7 + skip_size] & 0xE0); length = (float)fileread; if (length != 0) { length = ((float)length*8.f)/((float)bitrate) + 0.5f; } bitrate = (int)((float)bitrate/1000.0f + 0.5f); header_type = 2; } *song_length = length; fill_buffer(&b); if ((bread = NeAACDecInit(hDecoder, b.buffer, b.bytes_into_buffer, &samplerate, &channels)) < 0) { /* If some error initializing occured, skip the file */ fprintf(stderr, "Error initializing decoder library.\n"); if (b.buffer) free(b.buffer); NeAACDecClose(hDecoder); fclose(b.infile); return 1; } advance_buffer(&b, bread); fill_buffer(&b); /* print AAC file info */ fprintf(stderr, "%s file info:\n", aacfile); switch (header_type) { case 0: fprintf(stderr, "RAW\n\n"); break; case 1: fprintf(stderr, "ADTS, %.3f sec, %d kbps, %d Hz\n\n", length, bitrate, samplerate); break; case 2: fprintf(stderr, "ADIF, %.3f sec, %d kbps, %d Hz\n\n", length, bitrate, samplerate); break; } if (infoOnly) { NeAACDecClose(hDecoder); fclose(b.infile); if (b.buffer) free(b.buffer); return 0; } do { sample_buffer = NeAACDecDecode(hDecoder, &frameInfo, b.buffer, b.bytes_into_buffer); if (adts_out == 1) { int skip = (old_format) ? 8 : 7; adtsData = MakeAdtsHeader(&adtsDataSize, &frameInfo, old_format); /* write the adts header */ fwrite(adtsData, 1, adtsDataSize, adtsFile); /* write the frame data */ if (frameInfo.header_type == ADTS) fwrite(b.buffer + skip, 1, frameInfo.bytesconsumed - skip, adtsFile); else fwrite(b.buffer, 1, frameInfo.bytesconsumed, adtsFile); } /* update buffer indices */ advance_buffer(&b, frameInfo.bytesconsumed); if (frameInfo.error > 0) { fprintf(stderr, "Error: %s\n", NeAACDecGetErrorMessage(frameInfo.error)); } /* open the sound file now that the number of channels are known */ if (first_time && !frameInfo.error) { /* print some channel info */ print_channel_info(&frameInfo); if (!adts_out) { /* open output file */ if (!to_stdout) { aufile = open_audio_file(sndfile, frameInfo.samplerate, frameInfo.channels, outputFormat, fileType, aacChannelConfig2wavexChannelMask(&frameInfo)); } else { aufile = open_audio_file("-", frameInfo.samplerate, frameInfo.channels, outputFormat, fileType, aacChannelConfig2wavexChannelMask(&frameInfo)); } if (aufile == NULL) { if (b.buffer) free(b.buffer); NeAACDecClose(hDecoder); fclose(b.infile); return 0; } } else { fprintf(stderr, "Writing output MPEG-4 AAC ADTS file.\n\n"); } first_time = 0; } percent = min((int)(b.file_offset*100)/fileread, 100); if (percent > old_percent) { old_percent = percent; sprintf(percents, "%d%% decoding %s.", percent, aacfile); fprintf(stderr, "%s\r", percents);#ifdef _WIN32 SetConsoleTitle(percents);#endif } if ((frameInfo.error == 0) && (frameInfo.samples > 0) && (!adts_out)) { write_audio_file(aufile, sample_buffer, frameInfo.samples, 0); } /* fill buffer */ fill_buffer(&b); if (b.bytes_into_buffer == 0) sample_buffer = NULL; /* to make sure it stops now */ } while (sample_buffer != NULL); NeAACDecClose(hDecoder); if (adts_out == 1) { fclose(adtsFile); } fclose(b.infile); if (!first_time && !adts_out) close_audio_file(aufile); if (b.buffer) free(b.buffer); return frameInfo.error;}int GetAACTrack(mp4ff_t *infile){ /* find AAC track */ int i, rc; int numTracks = mp4ff_total_tracks(infile); for (i = 0; i < numTracks; i++) { unsigned char *buff = NULL; int buff_size = 0; mp4AudioSpecificConfig mp4ASC; mp4ff_get_decoder_config(infile, i, &buff, &buff_size); if (buff) { rc = NeAACDecAudioSpecificConfig(buff, buff_size, &mp4ASC); free(buff); if (rc < 0) continue; return i; } } /* can't decode this */ return -1;}unsigned long srates[] ={ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_stdout, int outputFormat, int fileType, int downMatrix, int noGapless, int infoOnly, int adts_out, float *song_length){ int track; unsigned long samplerate; unsigned char channels; void *sample_buffer; mp4ff_t *infile; long sampleId, numSamples; audio_file *aufile; FILE *mp4File; FILE *adtsFile; unsigned char *adtsData; int adtsDataSize; NeAACDecHandle hDecoder; NeAACDecConfigurationPtr config; NeAACDecFrameInfo frameInfo; mp4AudioSpecificConfig mp4ASC; unsigned char *buffer; int buffer_size; char percents[200]; int percent, old_percent = -1; int first_time = 1; /* for gapless decoding */ unsigned int useAacLength = 1; unsigned int initial = 1; unsigned int framesize; unsigned long timescale; /* initialise the callback structure */ mp4ff_callback_t *mp4cb = malloc(sizeof(mp4ff_callback_t)); mp4File = fopen(mp4file, "rb"); mp4cb->read = read_callback; mp4cb->seek = seek_callback; mp4cb->user_data = mp4File; hDecoder = NeAACDecOpen(); /* Set configuration */ config = NeAACDecGetCurrentConfiguration(hDecoder); config->outputFormat = outputFormat; config->downMatrix = downMatrix; //config->dontUpSampleImplicitSBR = 1; NeAACDecSetConfiguration(hDecoder, config); if (adts_out) { adtsFile = fopen(adts_fn, "wb"); if (adtsFile == NULL) { fprintf(stderr, "Error opening file: %s\n", adts_fn); return 1; } } infile = mp4ff_open_read(mp4cb); if (!infile) { /* unable to open file */ fprintf(stderr, "Error opening file: %s\n", mp4file); return 1; } if ((track = GetAACTrack(infile)) < 0) { fprintf(stderr, "Unable to find correct AAC sound track in the MP4 file.\n"); NeAACDecClose(hDecoder); mp4ff_close(infile); free(mp4cb); fclose(mp4File); return 1; } buffer = NULL; buffer_size = 0; mp4ff_get_decoder_config(infile, track, &buffer, &buffer_size); if(NeAACDecInit2(hDecoder, buffer, buffer_size, &samplerate, &channels) < 0) { /* If some error initializing occured, skip the file */ fprintf(stderr, "Error initializing decoder library.\n"); NeAACDecClose(hDecoder); mp4ff_close(infile); free(mp4cb); fclose(mp4File); return 1; } timescale = mp4ff_time_scale(infile, track); framesize = 1024; useAacLength = 0; if (buffer) { if (NeAACDecAudioSpecificConfig(buffer, buffer_size, &mp4ASC) >= 0) { if (mp4ASC.frameLengthFlag == 1) framesize = 960; if (mp4ASC.sbr_present_flag == 1) framesize *= 2; } free(buffer); } /* print some mp4 file info */ fprintf(stderr, "%s file info:\n\n", mp4file); { char *tag = NULL, *item = NULL; int k, j; char *ot[6] = { "NULL", "MAIN AAC", "LC AAC", "SSR AAC", "LTP AAC", "HE AAC" }; long samples = mp4ff_num_samples(infile, track); float f = 1024.0; float seconds; if (mp4ASC.sbr_present_flag == 1) { f = f * 2.0; } seconds = (float)samples*(float)(f-1.0)/(float)mp4ASC.samplingFrequency; *song_length = seconds; fprintf(stderr, "%s\t%.3f secs, %d ch, %d Hz\n\n", ot[(mp4ASC.objectTypeIndex > 5)?0:mp4ASC.objectTypeIndex], seconds, mp4ASC.channelsConfiguration, mp4ASC.samplingFrequency);#define PRINT_MP4_METADATA#ifdef PRINT_MP4_METADATA j = mp4ff_meta_get_num_items(infile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -