Compare commits
3 Commits
main
...
decoder-ex
Author | SHA1 | Date | |
---|---|---|---|
|
9136a1b2cd | ||
|
f977241f6b | ||
|
19cabba1f2 |
15
Makefile.am
15
Makefile.am
@ -34,12 +34,23 @@ libfdk_aac_la_LDFLAGS = -version-info @FDK_AAC_VERSION@ -no-undefined \
|
||||
-export-symbols $(top_srcdir)/fdk-aac.sym
|
||||
|
||||
if EXAMPLE
|
||||
bin_PROGRAMS = aac-enc$(EXEEXT)
|
||||
bin_PROGRAMS = aac-enc$(EXEEXT) aac-dec$(EXEEXT)
|
||||
|
||||
aac_enc_LDADD = libfdk-aac.la
|
||||
aac_enc_SOURCES = aac-enc.c wavreader.c
|
||||
|
||||
noinst_HEADERS = wavreader.h
|
||||
aac_dec_LDADD = libfdk-aac.la
|
||||
aac_dec_SOURCES = aac-dec.c wavwriter.c
|
||||
|
||||
noinst_HEADERS = wavreader.h wavwriter.h
|
||||
|
||||
if HAVE_LIBAVFORMAT
|
||||
bin_PROGRAMS += m4a-dec$(EXEEXT)
|
||||
|
||||
m4a_dec_LDADD = $(libavformat_LIBS) libfdk-aac.la
|
||||
m4a_dec_CFLAGS = $(libavformat_CFLAGS)
|
||||
m4a_dec_SOURCES = m4a-dec.c wavwriter.c
|
||||
endif
|
||||
endif
|
||||
|
||||
AACDEC_SRC = \
|
||||
|
113
aac-dec.c
Normal file
113
aac-dec.c
Normal file
@ -0,0 +1,113 @@
|
||||
/* ------------------------------------------------------------------
|
||||
* Copyright (C) 2013 Martin Storsjo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
||||
* express or implied.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
* -------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "libAACdec/include/aacdecoder_lib.h"
|
||||
#include "wavwriter.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *infile, *outfile;
|
||||
FILE *in;
|
||||
void *wav = NULL;
|
||||
int output_size;
|
||||
uint8_t *output_buf;
|
||||
int16_t *decode_buf;
|
||||
HANDLE_AACDECODER handle;
|
||||
int frame_size = 0;
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "%s in.aac out.wav\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
infile = argv[1];
|
||||
outfile = argv[2];
|
||||
|
||||
handle = aacDecoder_Open(TT_MP4_ADTS, 1);
|
||||
in = fopen(infile, "rb");
|
||||
if (!in) {
|
||||
perror(infile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
output_size = 8*2*1024;
|
||||
output_buf = (uint8_t*) malloc(output_size);
|
||||
decode_buf = (int16_t*) malloc(output_size);
|
||||
|
||||
while (1) {
|
||||
uint8_t packet[10240], *ptr = packet;
|
||||
int n, packet_size, i;
|
||||
UINT valid;
|
||||
AAC_DECODER_ERROR err;
|
||||
n = fread(packet, 1, 7, in);
|
||||
if (n != 7)
|
||||
break;
|
||||
if (packet[0] != 0xff || (packet[1] & 0xf0) != 0xf0) {
|
||||
fprintf(stderr, "Not an ADTS packet\n");
|
||||
break;
|
||||
}
|
||||
packet_size = ((packet[3] & 0x03) << 11) | (packet[4] << 3) | (packet[5] >> 5);
|
||||
n = fread(packet + 7, 1, packet_size - 7, in);
|
||||
if (n != packet_size - 7) {
|
||||
fprintf(stderr, "Partial packet\n");
|
||||
break;
|
||||
}
|
||||
valid = packet_size;
|
||||
err = aacDecoder_Fill(handle, &ptr, &packet_size, &valid);
|
||||
if (err != AAC_DEC_OK) {
|
||||
fprintf(stderr, "Fill failed: %x\n", err);
|
||||
break;
|
||||
}
|
||||
err = aacDecoder_DecodeFrame(handle, decode_buf, output_size, 0);
|
||||
if (err == AAC_DEC_NOT_ENOUGH_BITS)
|
||||
continue;
|
||||
if (err != AAC_DEC_OK) {
|
||||
fprintf(stderr, "Decode failed: %x\n", err);
|
||||
continue;
|
||||
}
|
||||
if (!wav) {
|
||||
CStreamInfo *info = aacDecoder_GetStreamInfo(handle);
|
||||
if (!info || info->sampleRate <= 0) {
|
||||
fprintf(stderr, "No stream info\n");
|
||||
break;
|
||||
}
|
||||
frame_size = info->frameSize * info->numChannels;
|
||||
// Note, this probably doesn't return channels > 2 in the right order for wav
|
||||
wav = wav_write_open(outfile, info->sampleRate, 16, info->numChannels);
|
||||
if (!wav) {
|
||||
perror(outfile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < frame_size; i++) {
|
||||
uint8_t* out = &output_buf[2*i];
|
||||
out[0] = decode_buf[i] & 0xff;
|
||||
out[1] = decode_buf[i] >> 8;
|
||||
}
|
||||
wav_write_data(wav, output_buf, 2*frame_size);
|
||||
}
|
||||
free(output_buf);
|
||||
free(decode_buf);
|
||||
fclose(in);
|
||||
if (wav)
|
||||
wav_write_close(wav);
|
||||
aacDecoder_Close(handle);
|
||||
return 0;
|
||||
}
|
||||
|
@ -13,8 +13,13 @@ AC_ARG_ENABLE([example],
|
||||
[enable example encoding program (default is no)])],
|
||||
[example=$enableval], [example=no])
|
||||
|
||||
if test x$example = xyes; then
|
||||
PKG_CHECK_MODULES([libavformat], [libavformat], [libavformat=yes], [libavformat=no])
|
||||
fi
|
||||
|
||||
dnl Automake conditionals to set
|
||||
AM_CONDITIONAL(EXAMPLE, test x$example = xyes)
|
||||
AM_CONDITIONAL(HAVE_LIBAVFORMAT, test x$libavformat = xyes)
|
||||
|
||||
dnl Checks for programs.
|
||||
AC_PROG_CC
|
||||
@ -36,3 +41,7 @@ AC_SUBST(LIBS_PRIVATE)
|
||||
AC_CONFIG_FILES([Makefile
|
||||
fdk-aac.pc])
|
||||
AC_OUTPUT
|
||||
|
||||
if test x$example = xyes; then
|
||||
AC_MSG_NOTICE([Found libavformat: ${libavformat}])
|
||||
fi
|
||||
|
139
m4a-dec.c
Normal file
139
m4a-dec.c
Normal file
@ -0,0 +1,139 @@
|
||||
/* ------------------------------------------------------------------
|
||||
* Copyright (C) 2013 Martin Storsjo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
||||
* express or implied.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
* -------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "libAACdec/include/aacdecoder_lib.h"
|
||||
#include "wavwriter.h"
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 55
|
||||
#define AV_CODEC_ID_AAC CODEC_ID_AAC
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *infile, *outfile;
|
||||
AVFormatContext *in = NULL;
|
||||
AVStream *st = NULL;
|
||||
void *wav = NULL;
|
||||
int output_size, ret, i;
|
||||
uint8_t *output_buf;
|
||||
int16_t *decode_buf;
|
||||
HANDLE_AACDECODER handle;
|
||||
AAC_DECODER_ERROR err;
|
||||
int frame_size = 0;
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "%s in.m4a out.wav\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
infile = argv[1];
|
||||
outfile = argv[2];
|
||||
|
||||
av_register_all();
|
||||
avformat_network_init();
|
||||
ret = avformat_open_input(&in, infile, NULL, NULL);
|
||||
if (ret < 0) {
|
||||
char buf[100];
|
||||
av_strerror(ret, buf, sizeof(buf));
|
||||
fprintf(stderr, "%s: %s\n", infile, buf);
|
||||
return 1;
|
||||
}
|
||||
for (i = 0; i < in->nb_streams && !st; i++) {
|
||||
if (in->streams[i]->codec->codec_id == AV_CODEC_ID_AAC)
|
||||
st = in->streams[i];
|
||||
}
|
||||
if (!st) {
|
||||
fprintf(stderr, "No AAC stream found\n");
|
||||
return 1;
|
||||
}
|
||||
if (!st->codec->extradata_size) {
|
||||
fprintf(stderr, "No AAC ASC found\n");
|
||||
return 1;
|
||||
}
|
||||
handle = aacDecoder_Open(TT_MP4_RAW, 1);
|
||||
err = aacDecoder_ConfigRaw(handle, &st->codec->extradata, &st->codec->extradata_size);
|
||||
if (err != AAC_DEC_OK) {
|
||||
fprintf(stderr, "Unable to decode the ASC\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
output_size = 8*2*1024;
|
||||
output_buf = (uint8_t*) malloc(output_size);
|
||||
decode_buf = (int16_t*) malloc(output_size);
|
||||
|
||||
while (1) {
|
||||
int i;
|
||||
UINT valid;
|
||||
AVPacket pkt = { 0 };
|
||||
int ret = av_read_frame(in, &pkt);
|
||||
if (ret < 0) {
|
||||
if (ret == AVERROR(EAGAIN))
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (pkt.stream_index != st->index) {
|
||||
av_free_packet(&pkt);
|
||||
continue;
|
||||
}
|
||||
|
||||
valid = pkt.size;
|
||||
err = aacDecoder_Fill(handle, &pkt.data, &pkt.size, &valid);
|
||||
if (err != AAC_DEC_OK) {
|
||||
fprintf(stderr, "Fill failed: %x\n", err);
|
||||
break;
|
||||
}
|
||||
err = aacDecoder_DecodeFrame(handle, decode_buf, output_size, 0);
|
||||
av_free_packet(&pkt);
|
||||
if (err == AAC_DEC_NOT_ENOUGH_BITS)
|
||||
continue;
|
||||
if (err != AAC_DEC_OK) {
|
||||
fprintf(stderr, "Decode failed: %x\n", err);
|
||||
continue;
|
||||
}
|
||||
if (!wav) {
|
||||
CStreamInfo *info = aacDecoder_GetStreamInfo(handle);
|
||||
if (!info || info->sampleRate <= 0) {
|
||||
fprintf(stderr, "No stream info\n");
|
||||
break;
|
||||
}
|
||||
frame_size = info->frameSize * info->numChannels;
|
||||
// Note, this probably doesn't return channels > 2 in the right order for wav
|
||||
wav = wav_write_open(outfile, info->sampleRate, 16, info->numChannels);
|
||||
if (!wav) {
|
||||
perror(outfile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < frame_size; i++) {
|
||||
uint8_t* out = &output_buf[2*i];
|
||||
out[0] = decode_buf[i] & 0xff;
|
||||
out[1] = decode_buf[i] >> 8;
|
||||
}
|
||||
wav_write_data(wav, output_buf, 2*frame_size);
|
||||
}
|
||||
free(output_buf);
|
||||
free(decode_buf);
|
||||
avformat_close_input(&in);
|
||||
if (wav)
|
||||
wav_write_close(wav);
|
||||
aacDecoder_Close(handle);
|
||||
return 0;
|
||||
}
|
||||
|
111
wavwriter.c
Normal file
111
wavwriter.c
Normal file
@ -0,0 +1,111 @@
|
||||
/* ------------------------------------------------------------------
|
||||
* Copyright (C) 2009 Martin Storsjo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
||||
* express or implied.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
* -------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "wavwriter.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct wav_writer {
|
||||
FILE *wav;
|
||||
int data_length;
|
||||
|
||||
int sample_rate;
|
||||
int bits_per_sample;
|
||||
int channels;
|
||||
};
|
||||
|
||||
static void write_string(struct wav_writer* ww, const char *str) {
|
||||
fputc(str[0], ww->wav);
|
||||
fputc(str[1], ww->wav);
|
||||
fputc(str[2], ww->wav);
|
||||
fputc(str[3], ww->wav);
|
||||
}
|
||||
|
||||
static void write_int32(struct wav_writer* ww, int value) {
|
||||
fputc((value >> 0) & 0xff, ww->wav);
|
||||
fputc((value >> 8) & 0xff, ww->wav);
|
||||
fputc((value >> 16) & 0xff, ww->wav);
|
||||
fputc((value >> 24) & 0xff, ww->wav);
|
||||
}
|
||||
|
||||
static void write_int16(struct wav_writer* ww, int value) {
|
||||
fputc((value >> 0) & 0xff, ww->wav);
|
||||
fputc((value >> 8) & 0xff, ww->wav);
|
||||
}
|
||||
|
||||
static void write_header(struct wav_writer* ww, int length) {
|
||||
int bytes_per_frame, bytes_per_sec;
|
||||
write_string(ww, "RIFF");
|
||||
write_int32(ww, 4 + 8 + 16 + 8 + length);
|
||||
write_string(ww, "WAVE");
|
||||
|
||||
write_string(ww, "fmt ");
|
||||
write_int32(ww, 16);
|
||||
|
||||
bytes_per_frame = ww->bits_per_sample/8*ww->channels;
|
||||
bytes_per_sec = bytes_per_frame*ww->sample_rate;
|
||||
write_int16(ww, 1); // Format
|
||||
write_int16(ww, ww->channels); // Channels
|
||||
write_int32(ww, ww->sample_rate); // Samplerate
|
||||
write_int32(ww, bytes_per_sec); // Bytes per sec
|
||||
write_int16(ww, bytes_per_frame); // Bytes per frame
|
||||
write_int16(ww, ww->bits_per_sample); // Bits per sample
|
||||
|
||||
write_string(ww, "data");
|
||||
write_int32(ww, length);
|
||||
}
|
||||
|
||||
void* wav_write_open(const char *filename, int sample_rate, int bits_per_sample, int channels) {
|
||||
struct wav_writer* ww = (struct wav_writer*) malloc(sizeof(*ww));
|
||||
memset(ww, 0, sizeof(*ww));
|
||||
ww->wav = fopen(filename, "wb");
|
||||
if (ww->wav == NULL) {
|
||||
free(ww);
|
||||
return NULL;
|
||||
}
|
||||
ww->data_length = 0;
|
||||
ww->sample_rate = sample_rate;
|
||||
ww->bits_per_sample = bits_per_sample;
|
||||
ww->channels = channels;
|
||||
|
||||
write_header(ww, ww->data_length);
|
||||
return ww;
|
||||
}
|
||||
|
||||
void wav_write_close(void* obj) {
|
||||
struct wav_writer* ww = (struct wav_writer*) obj;
|
||||
if (ww->wav == NULL) {
|
||||
free(ww);
|
||||
return;
|
||||
}
|
||||
fseek(ww->wav, 0, SEEK_SET);
|
||||
write_header(ww, ww->data_length);
|
||||
fclose(ww->wav);
|
||||
free(ww);
|
||||
}
|
||||
|
||||
void wav_write_data(void* obj, const unsigned char* data, int length) {
|
||||
struct wav_writer* ww = (struct wav_writer*) obj;
|
||||
if (ww->wav == NULL)
|
||||
return;
|
||||
fwrite(data, length, 1, ww->wav);
|
||||
ww->data_length += length;
|
||||
}
|
||||
|
36
wavwriter.h
Normal file
36
wavwriter.h
Normal file
@ -0,0 +1,36 @@
|
||||
/* ------------------------------------------------------------------
|
||||
* Copyright (C) 2009 Martin Storsjo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
||||
* express or implied.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
* -------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef WAVWRITER_H
|
||||
#define WAVWRITER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void* wav_write_open(const char *filename, int sample_rate, int bits_per_sample, int channels);
|
||||
void wav_write_close(void* obj);
|
||||
|
||||
void wav_write_data(void* obj, const unsigned char* data, int length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user