2003-02-16 00:05:07 +00:00
|
|
|
/*
|
2003-02-24 12:31:17 +00:00
|
|
|
mkvmerge -- utility for splicing together matroska files
|
2003-02-16 00:05:07 +00:00
|
|
|
from component media subtypes
|
|
|
|
|
2003-02-24 12:31:17 +00:00
|
|
|
r_mp3.h
|
2003-02-16 00:05:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2003-02-24 12:31:17 +00:00
|
|
|
/*!
|
|
|
|
\file
|
2003-06-15 14:03:28 +00:00
|
|
|
\version $Id$
|
2003-02-24 12:31:17 +00:00
|
|
|
\brief MP3 reader module
|
2003-05-18 20:57:08 +00:00
|
|
|
\author Moritz Bunkus <moritz@bunkus.org>
|
2003-05-18 20:40:11 +00:00
|
|
|
\author Peter Niemayer <niemayer@isg.de>
|
2003-02-24 12:31:17 +00:00
|
|
|
*/
|
|
|
|
|
2003-02-16 00:05:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2003-05-19 21:07:07 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <sys/unistd.h> // Needed for swab()
|
2003-05-21 22:17:33 +00:00
|
|
|
#elif defined LIBEBML_GCC2
|
|
|
|
#define __USE_XOPEN
|
|
|
|
#include <unistd.h>
|
2003-05-19 21:07:07 +00:00
|
|
|
#endif
|
|
|
|
|
2003-04-18 10:08:24 +00:00
|
|
|
#include "mkvmerge.h"
|
2003-02-24 12:31:17 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "r_wav.h"
|
|
|
|
#include "p_pcm.h"
|
2003-05-18 20:40:11 +00:00
|
|
|
#include "p_dts.h"
|
|
|
|
#include "dts_common.h"
|
2003-02-16 00:05:07 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <avilib.h> // for wave_header
|
|
|
|
}
|
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
void dts_14_to_dts_16(unsigned short * src, const unsigned long srcwords,
|
|
|
|
unsigned short * dst) {
|
2003-05-18 20:53:33 +00:00
|
|
|
// srcwords has to be a multiple of 8!
|
2003-05-20 06:27:08 +00:00
|
|
|
// you will get (srcbytes >> 3)*7 destination words!
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
const unsigned long l = srcwords >> 3;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
for (unsigned long b = 0; b < l; b++) {
|
|
|
|
unsigned short src_0 = (src[0]>>8) | (src[0]<<8);
|
|
|
|
unsigned short src_1 = (src[1]>>8) | (src[1]<<8);
|
|
|
|
// 14 + 2
|
|
|
|
unsigned short dst_0 = (src_0 << 2) | ((src_1 & 0x3fff) >> 12);
|
|
|
|
dst[0] = (dst_0>>8) | (dst_0<<8);
|
|
|
|
// 12 + 4
|
|
|
|
unsigned short src_2 = (src[2]>>8) | (src[2]<<8);
|
|
|
|
unsigned short dst_1 = (src_1 << 4) | ((src_2 & 0x3fff) >> 10);
|
|
|
|
dst[1] = (dst_1>>8) | (dst_1<<8);
|
|
|
|
// 10 + 6
|
|
|
|
unsigned short src_3 = (src[3]>>8) | (src[3]<<8);
|
|
|
|
unsigned short dst_2 = (src_2 << 6) | ((src_3 & 0x3fff) >> 8);
|
|
|
|
dst[2] = (dst_2>>8) | (dst_2<<8);
|
|
|
|
// 8 + 8
|
|
|
|
unsigned short src_4 = (src[4]>>8) | (src[4]<<8);
|
|
|
|
unsigned short dst_3 = (src_3 << 8) | ((src_4 & 0x3fff) >> 6);
|
|
|
|
dst[3] = (dst_3>>8) | (dst_3<<8);
|
|
|
|
// 6 + 10
|
|
|
|
unsigned short src_5 = (src[5]>>8) | (src[5]<<8);
|
|
|
|
unsigned short dst_4 = (src_4 << 10) | ((src_5 & 0x3fff) >> 4);
|
|
|
|
dst[4] = (dst_4>>8) | (dst_4<<8);
|
|
|
|
// 4 + 12
|
|
|
|
unsigned short src_6 = (src[6]>>8) | (src[6]<<8);
|
|
|
|
unsigned short dst_5 = (src_5 << 12) | ((src_6 & 0x3fff) >> 2);
|
|
|
|
dst[5] = (dst_5>>8) | (dst_5<<8);
|
|
|
|
// 2 + 14
|
|
|
|
unsigned short src_7 = (src[7]>>8) | (src[7]<<8);
|
|
|
|
unsigned short dst_6 = (src_6 << 14) | ((src_7 & 0x3fff) >> 2);
|
|
|
|
dst[6] = (dst_6>>8) | (dst_6<<8);
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
dst += 7;
|
|
|
|
src += 8;
|
|
|
|
}
|
2003-05-18 20:40:11 +00:00
|
|
|
}
|
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
int wav_reader_c::probe_file(mm_io_c *mm_io, int64_t size) {
|
2003-02-16 00:05:07 +00:00
|
|
|
wave_header wheader;
|
2003-05-18 20:40:11 +00:00
|
|
|
|
2003-02-16 00:05:07 +00:00
|
|
|
if (size < sizeof(wave_header))
|
|
|
|
return 0;
|
2003-05-23 06:34:58 +00:00
|
|
|
try {
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
|
|
|
if (mm_io->read((char *)&wheader, sizeof(wheader)) != sizeof(wheader))
|
|
|
|
return 0;
|
|
|
|
mm_io->setFilePointer(0, seek_beginning);
|
|
|
|
} catch (exception &ex) {
|
2003-02-16 00:05:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (strncmp((char *)wheader.riff.id, "RIFF", 4) ||
|
|
|
|
strncmp((char *)wheader.riff.wave_id, "WAVE", 4) ||
|
|
|
|
strncmp((char *)wheader.data.id, "data", 4))
|
|
|
|
return 0;
|
2003-05-18 20:40:11 +00:00
|
|
|
|
2003-05-20 06:30:25 +00:00
|
|
|
return 1;
|
2003-02-16 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
2003-03-05 13:51:20 +00:00
|
|
|
wav_reader_c::wav_reader_c(track_info_t *nti) throw (error_c):
|
|
|
|
generic_reader_c(nti) {
|
2003-04-13 15:23:03 +00:00
|
|
|
int64_t size;
|
2003-05-18 20:40:11 +00:00
|
|
|
|
|
|
|
pcmpacketizer = 0;
|
|
|
|
dtspacketizer = 0;
|
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);
|
|
|
|
} catch (exception &ex) {
|
|
|
|
throw error_c("wav_reader: Could not open the source file.");
|
|
|
|
}
|
|
|
|
if (!wav_reader_c::probe_file(mm_io, size))
|
2003-02-16 00:05:07 +00:00
|
|
|
throw error_c("wav_reader: Source is not a valid WAVE file.");
|
2003-05-23 06:34:58 +00:00
|
|
|
if (mm_io->read(&wheader, sizeof(wheader)) != sizeof(wheader))
|
2003-02-16 00:05:07 +00:00
|
|
|
throw error_c("wav_reader: could not read WAVE header.");
|
|
|
|
bps = wheader.common.wChannels * wheader.common.wBitsPerSample *
|
2003-05-18 20:40:11 +00:00
|
|
|
wheader.common.dwSamplesPerSec / 8;
|
2003-05-05 18:37:36 +00:00
|
|
|
chunk = (unsigned char *)safemalloc(bps + 1);
|
2003-02-16 00:05:07 +00:00
|
|
|
bytes_processed = 0;
|
2003-06-15 10:03:09 +00:00
|
|
|
ti->id = 0; // ID for this track.
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
{
|
|
|
|
// check wether .wav file contains DTS data...
|
2003-05-20 06:30:25 +00:00
|
|
|
unsigned short obuf[max_dts_packet_size/2];
|
|
|
|
unsigned short buf[2][max_dts_packet_size/2];
|
2003-05-18 20:53:33 +00:00
|
|
|
int cur_buf = 0;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
long rlen = mm_io->read(obuf, max_dts_packet_size);
|
|
|
|
mm_io->setFilePointer(sizeof(wheader), seek_beginning);
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
for (dts_swap_bytes = 0; dts_swap_bytes < 2; dts_swap_bytes++) {
|
|
|
|
memcpy(buf[cur_buf], obuf, rlen);
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
if (dts_swap_bytes) {
|
|
|
|
swab(buf[cur_buf], buf[cur_buf^1], rlen);
|
|
|
|
cur_buf ^= 1;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
for (dts_14_16 = 0; dts_14_16 < 2; dts_14_16++) {
|
2003-05-20 06:27:08 +00:00
|
|
|
long erlen = rlen;
|
2003-05-18 20:53:33 +00:00
|
|
|
if (dts_14_16) {
|
|
|
|
unsigned long words = rlen / (8*sizeof(short));
|
|
|
|
dts_14_to_dts_16(buf[cur_buf], words*8, buf[cur_buf^1]);
|
|
|
|
cur_buf ^= 1;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
dts_header_t dtsheader;
|
2003-05-20 06:27:08 +00:00
|
|
|
int pos = find_dts_header((const unsigned char *)buf[cur_buf], erlen,
|
2003-05-18 20:40:11 +00:00
|
|
|
&dtsheader);
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
if (pos >= 0) {
|
2003-06-12 23:05:49 +00:00
|
|
|
if (verbose) {
|
|
|
|
fprintf(stderr,"Using WAV demultiplexer for %s.\n"
|
|
|
|
"+-> Using DTS output module for audio stream. %s %s\n",
|
|
|
|
ti->fname, (dts_swap_bytes)? "(bytes swapped)" : "",
|
|
|
|
(dts_14_16)? "(DTS14 encoded)" : "(DTS16 encoded)");
|
|
|
|
print_dts_header(&dtsheader);
|
|
|
|
is_dts = true;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
dtspacketizer = new dts_packetizer_c(this, dtsheader, ti);
|
|
|
|
// .wav's with DTS are always filled up with other stuff to match
|
2003-05-18 20:40:11 +00:00
|
|
|
// the bitrate...
|
2003-05-18 20:53:33 +00:00
|
|
|
dtspacketizer->skipping_is_normal = true;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-18 20:40:11 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
if (dtspacketizer)
|
2003-05-18 20:40:11 +00:00
|
|
|
break;
|
2003-05-18 20:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
if (!dtspacketizer) {
|
2003-05-18 20:53:33 +00:00
|
|
|
pcmpacketizer = new pcm_packetizer_c(this, wheader.common.dwSamplesPerSec,
|
2003-05-18 20:40:11 +00:00
|
|
|
wheader.common.wChannels,
|
|
|
|
wheader.common.wBitsPerSample, ti);
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:53:33 +00:00
|
|
|
if (verbose)
|
2003-05-18 20:40:11 +00:00
|
|
|
fprintf(stdout, "Using WAV demultiplexer for %s.\n+-> Using "
|
2003-05-18 20:53:33 +00:00
|
|
|
"PCM output module for audio stream.\n", ti->fname);
|
2003-06-12 23:05:49 +00:00
|
|
|
is_dts = false;
|
2003-05-18 20:40:11 +00:00
|
|
|
}
|
2003-02-16 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wav_reader_c::~wav_reader_c() {
|
2003-05-23 06:34:58 +00:00
|
|
|
delete mm_io;
|
2003-02-16 00:05:07 +00:00
|
|
|
if (chunk != NULL)
|
2003-05-05 18:37:36 +00:00
|
|
|
safefree(chunk);
|
2003-02-16 00:05:07 +00:00
|
|
|
if (pcmpacketizer != NULL)
|
|
|
|
delete pcmpacketizer;
|
2003-05-18 20:40:11 +00:00
|
|
|
if (dtspacketizer != NULL)
|
|
|
|
delete dtspacketizer;
|
2003-02-16 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wav_reader_c::read() {
|
2003-05-18 20:40:11 +00:00
|
|
|
if (pcmpacketizer) {
|
|
|
|
int nread;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-23 06:34:58 +00:00
|
|
|
nread = mm_io->read(chunk, bps);
|
2003-05-18 20:40:11 +00:00
|
|
|
if (nread <= 0)
|
|
|
|
return 0;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
pcmpacketizer->process(chunk, nread);
|
2003-02-24 12:31:17 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
bytes_processed += nread;
|
2003-02-16 00:05:07 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
if (nread != bps)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return EMOREDATA;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
if (dtspacketizer) {
|
2003-05-20 06:30:25 +00:00
|
|
|
unsigned short buf[2][max_dts_packet_size/2];
|
2003-05-18 20:40:11 +00:00
|
|
|
int cur_buf = 0;
|
2003-05-23 06:34:58 +00:00
|
|
|
long rlen = mm_io->read(buf[cur_buf], max_dts_packet_size);
|
2003-05-18 20:40:11 +00:00
|
|
|
|
|
|
|
if (rlen <= 0)
|
|
|
|
return 0;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
if (dts_swap_bytes) {
|
|
|
|
swab(buf[cur_buf], buf[cur_buf^1], rlen);
|
|
|
|
cur_buf ^= 1;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-20 06:27:08 +00:00
|
|
|
long erlen = rlen;
|
2003-05-18 20:40:11 +00:00
|
|
|
if (dts_14_16) {
|
|
|
|
unsigned long words = rlen / (8*sizeof(short));
|
|
|
|
//if (words*8*sizeof(short) != rlen) {
|
2003-05-18 20:53:33 +00:00
|
|
|
// unaligned problem, should not happen...
|
2003-05-18 20:40:11 +00:00
|
|
|
//}
|
|
|
|
dts_14_to_dts_16(buf[cur_buf], words*8, buf[cur_buf^1]);
|
|
|
|
cur_buf ^= 1;
|
2003-05-20 06:27:08 +00:00
|
|
|
erlen = words * 7 * sizeof(short);
|
2003-05-18 20:40:11 +00:00
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-20 06:27:08 +00:00
|
|
|
dtspacketizer->process((unsigned char *) (buf[cur_buf]), erlen);
|
|
|
|
|
|
|
|
bytes_processed += rlen;
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
if (rlen != max_dts_packet_size)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return EMOREDATA;
|
|
|
|
}
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-05-18 20:40:11 +00:00
|
|
|
return 0;
|
2003-02-16 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
2003-02-24 12:31:17 +00:00
|
|
|
packet_t *wav_reader_c::get_packet() {
|
2003-05-18 20:40:11 +00:00
|
|
|
if (pcmpacketizer)
|
|
|
|
return pcmpacketizer->get_packet();
|
|
|
|
else if (dtspacketizer)
|
|
|
|
return dtspacketizer->get_packet();
|
|
|
|
|
|
|
|
return NULL;
|
2003-02-16 00:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wav_reader_c::display_priority() {
|
|
|
|
return DISPLAYPRIORITY_HIGH - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wav_reader_c::display_progress() {
|
|
|
|
int samples = (wheader.riff.len - sizeof(wheader) + 8) / bps;
|
|
|
|
fprintf(stdout, "progress: %d/%d seconds (%d%%)\r",
|
|
|
|
(int)(bytes_processed / bps), (int)samples,
|
|
|
|
(int)(bytes_processed * 100L / bps / samples));
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2003-05-02 21:49:42 +00:00
|
|
|
|
|
|
|
void wav_reader_c::set_headers() {
|
2003-05-18 20:40:11 +00:00
|
|
|
if (pcmpacketizer)
|
|
|
|
pcmpacketizer->set_headers();
|
|
|
|
if (dtspacketizer)
|
|
|
|
dtspacketizer->set_headers();
|
2003-05-02 21:49:42 +00:00
|
|
|
}
|
2003-06-12 23:05:49 +00:00
|
|
|
|
|
|
|
void wav_reader_c::identify() {
|
|
|
|
fprintf(stdout, "File '%s': container: WAV\nTrack ID 0: audio (%s)\n",
|
|
|
|
ti->fname, is_dts ? "DTS" : "PCM");
|
|
|
|
}
|