2003-05-17 21:01:28 +00:00
|
|
|
/*
|
|
|
|
mkvmerge -- utility for splicing together matroska files
|
|
|
|
from component media subtypes
|
|
|
|
|
|
|
|
r_aac.h
|
|
|
|
|
|
|
|
Written by Moritz Bunkus <moritz@bunkus.org>
|
|
|
|
|
|
|
|
Distributed under the GPL
|
|
|
|
see the file COPYING for details
|
|
|
|
or visit http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\file
|
2003-06-15 14:03:28 +00:00
|
|
|
\version $Id$
|
2003-05-17 21:01:28 +00:00
|
|
|
\brief AAC demultiplexer module
|
2003-05-18 20:57:08 +00:00
|
|
|
\author Moritz Bunkus <moritz@bunkus.org>
|
2003-05-17 21:01:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "mkvmerge.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "r_aac.h"
|
|
|
|
#include "p_aac.h"
|
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
int aac_reader_c::probe_file(mm_io_c *mm_io, int64_t size) {
|
2003-05-17 21:01:28 +00:00
|
|
|
char buf[4096];
|
|
|
|
aac_header_t aacheader;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-17 21:01:28 +00:00
|
|
|
if (size < 4096)
|
|
|
|
return 0;
|
2003-05-23 06:34:58 +00:00
|
|
|
try {
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
|
|
|
if (mm_io->read(buf, 4096) != 4096)
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
|
|
|
} catch (exception &ex) {
|
2003-05-17 21:01:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-05-18 19:56:31 +00:00
|
|
|
if (parse_aac_adif_header((unsigned char *)buf, 4096, &aacheader))
|
|
|
|
return 1;
|
2003-06-19 19:32:50 +00:00
|
|
|
if (find_aac_header((unsigned char *)buf, 4096, &aacheader, false) != 0)
|
2003-05-17 21:01:28 +00:00
|
|
|
return 0;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
|
|
|
return 1;
|
2003-05-17 21:01:28 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 19:32:50 +00:00
|
|
|
#define INITCHUNKSIZE 16384
|
|
|
|
#define SINITCHUNKSIZE "16384"
|
|
|
|
|
2003-05-17 21:01:28 +00:00
|
|
|
aac_reader_c::aac_reader_c(track_info_t *nti) throw (error_c):
|
|
|
|
generic_reader_c(nti) {
|
2003-05-18 19:56:31 +00:00
|
|
|
int adif;
|
2003-05-17 21:01:28 +00:00
|
|
|
aac_header_t aacheader;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
try {
|
|
|
|
mm_io = new mm_io_c(ti->fname, MODE_READ);
|
|
|
|
mm_io->setFilePointer(0, seek_end);
|
|
|
|
size = mm_io->getFilePointer();
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
2003-06-19 19:32:50 +00:00
|
|
|
chunk = (unsigned char *)safemalloc(INITCHUNKSIZE);
|
|
|
|
if (mm_io->read(chunk, INITCHUNKSIZE) != INITCHUNKSIZE)
|
|
|
|
throw error_c("aac_reader: Could not read " SINITCHUNKSIZE " bytes.");
|
2003-05-23 06:34:58 +00:00
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
2003-06-19 19:32:50 +00:00
|
|
|
if (parse_aac_adif_header(chunk, INITCHUNKSIZE, &aacheader)) {
|
2003-05-23 06:34:58 +00:00
|
|
|
throw error_c("aac_reader: ADIF header files are not supported.");
|
|
|
|
adif = 1;
|
2003-06-19 19:32:50 +00:00
|
|
|
} else {
|
|
|
|
if (find_aac_header(chunk, INITCHUNKSIZE, &aacheader, emphasis_present)
|
|
|
|
!= 0)
|
|
|
|
throw error_c("aac_reader: No valid AAC packet found in the first "
|
|
|
|
SINITCHUNKSIZE " bytes.\n");
|
|
|
|
guess_adts_version();
|
|
|
|
fprintf(stdout, "emphasis_present: %s\n", emphasis_present ? "true" :
|
|
|
|
"false");
|
2003-05-23 06:34:58 +00:00
|
|
|
adif = 0;
|
2003-06-19 19:32:50 +00:00
|
|
|
}
|
2003-05-23 06:34:58 +00:00
|
|
|
bytes_processed = 0;
|
2003-06-15 10:03:09 +00:00
|
|
|
ti->id = 0; // ID for this track.
|
2003-05-23 06:34:58 +00:00
|
|
|
aacpacketizer = new aac_packetizer_c(this, aacheader.id, aacheader.profile,
|
|
|
|
aacheader.sample_rate,
|
2003-06-19 19:32:50 +00:00
|
|
|
aacheader.channels, ti,
|
|
|
|
emphasis_present);
|
2003-05-23 06:34:58 +00:00
|
|
|
} catch (exception &ex) {
|
|
|
|
throw error_c("aac_reader: Could not open the file.");
|
|
|
|
}
|
2003-05-17 21:01:28 +00:00
|
|
|
if (verbose)
|
2003-06-15 08:40:43 +00:00
|
|
|
fprintf(stdout, "Using AAC demultiplexer for %s.\n+-> Using "
|
2003-05-17 21:01:28 +00:00
|
|
|
"AAC output module for audio stream.\n", ti->fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
aac_reader_c::~aac_reader_c() {
|
2003-05-23 06:34:58 +00:00
|
|
|
delete mm_io;
|
2003-05-17 21:01:28 +00:00
|
|
|
if (chunk != NULL)
|
|
|
|
safefree(chunk);
|
|
|
|
if (aacpacketizer != NULL)
|
|
|
|
delete aacpacketizer;
|
|
|
|
}
|
|
|
|
|
2003-06-19 19:32:50 +00:00
|
|
|
// Try to guess if the MPEG4 header contains the emphasis field (2 bits)
|
|
|
|
void aac_reader_c::guess_adts_version() {
|
|
|
|
int pos;
|
|
|
|
aac_header_t aacheader;
|
|
|
|
|
|
|
|
emphasis_present = false;
|
|
|
|
|
|
|
|
// Due to the checks we do have an ADTS header at 0.
|
|
|
|
find_aac_header(chunk, INITCHUNKSIZE, &aacheader, emphasis_present);
|
|
|
|
if (aacheader.id != 0) // MPEG2
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Now make some sanity checks on the size field.
|
|
|
|
if (aacheader.bytes > 8192) {
|
|
|
|
emphasis_present = true; // Looks like it's borked.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks ok so far. See if the next ADTS is right behind this packet.
|
|
|
|
pos = find_aac_header(&chunk[aacheader.bytes], INITCHUNKSIZE -
|
|
|
|
aacheader.bytes, &aacheader, emphasis_present);
|
|
|
|
if (pos != 0) { // Not ok - what do we do now?
|
|
|
|
emphasis_present = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-17 21:01:28 +00:00
|
|
|
int aac_reader_c::read() {
|
|
|
|
int nread;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
nread = mm_io->read(chunk, 4096);
|
2003-05-17 21:01:28 +00:00
|
|
|
if (nread <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
aacpacketizer->process(chunk, nread);
|
|
|
|
bytes_processed += nread;
|
|
|
|
|
|
|
|
return EMOREDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_t *aac_reader_c::get_packet() {
|
|
|
|
return aacpacketizer->get_packet();
|
|
|
|
}
|
|
|
|
|
|
|
|
int aac_reader_c::display_priority() {
|
|
|
|
return DISPLAYPRIORITY_HIGH - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void aac_reader_c::display_progress() {
|
|
|
|
fprintf(stdout, "progress: %lld/%lld bytes (%d%%)\r",
|
|
|
|
bytes_processed, size,
|
|
|
|
(int)(bytes_processed * 100L / size));
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void aac_reader_c::set_headers() {
|
|
|
|
aacpacketizer->set_headers();
|
|
|
|
}
|
2003-06-12 23:05:49 +00:00
|
|
|
|
|
|
|
void aac_reader_c::identify() {
|
|
|
|
fprintf(stdout, "File '%s': container: AAC\nTrack ID 0: audio (AAC)\n",
|
|
|
|
ti->fname);
|
|
|
|
}
|