mkvtoolnix/p_pcm.cpp

172 lines
4.5 KiB
C++
Raw Normal View History

2003-02-24 13:12:27 +00:00
/*
mkvmerge -- utility for splicing together matroska files
from component media subtypes
p_pcm.cpp
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
\version \$Id: p_pcm.cpp,v 1.8 2003/03/06 23:38:37 mosu Exp $
2003-02-24 13:12:27 +00:00
\brief PCM output module
\author Moritz Bunkus <moritz @ bunkus.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "common.h"
#include "queue.h"
#include "p_pcm.h"
#include "KaxTracks.h"
#include "KaxTrackAudio.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif
pcm_packetizer_c::pcm_packetizer_c(unsigned long nsamples_per_sec,
2003-02-24 13:12:27 +00:00
int nchannels, int nbits_per_sample,
track_info_t *nti) throw (error_c):
q_c(nti) {
2003-02-24 13:12:27 +00:00
packetno = 0;
bps = nchannels * nbits_per_sample * nsamples_per_sec / 8;
tempbuf = (unsigned char *)malloc(bps + 128);
2003-02-24 13:12:27 +00:00
if (tempbuf == NULL)
die("malloc");
tempbuf_size = bps;
2003-02-24 13:12:27 +00:00
samples_per_sec = nsamples_per_sec;
channels = nchannels;
bits_per_sample = nbits_per_sample;
bytes_output = 0;
2003-02-28 13:00:26 +00:00
remaining_sync = 0;
2003-02-24 13:12:27 +00:00
set_header();
}
pcm_packetizer_c::~pcm_packetizer_c() {
if (tempbuf != NULL)
free(tempbuf);
}
// void pcm_packetizer_c::reset() {
// }
#define APCM "A_PCM16IN"
void pcm_packetizer_c::set_header() {
using namespace LIBMATROSKA_NAMESPACE;
if (kax_last_entry == NULL)
track_entry =
&GetChild<KaxTrackEntry>(static_cast<KaxTracks &>(*kax_tracks));
else
track_entry =
&GetNextChild<KaxTrackEntry>(static_cast<KaxTracks &>(*kax_tracks),
static_cast<KaxTrackEntry &>(*kax_last_entry));
kax_last_entry = track_entry;
if (serialno <= 0)
serialno = track_number++;
KaxTrackNumber &tnumber =
GetChild<KaxTrackNumber>(static_cast<KaxTrackEntry &>(*track_entry));
*(static_cast<EbmlUInteger *>(&tnumber)) = serialno;
*(static_cast<EbmlUInteger *>
(&GetChild<KaxTrackType>(static_cast<KaxTrackEntry &>(*track_entry)))) =
track_audio;
KaxCodecID &codec_id =
GetChild<KaxCodecID>(static_cast<KaxTrackEntry &>(*track_entry));
if (bits_per_sample != 16) {
fprintf(stderr, "Error: pcm_packetizer: Only files with 16bits per sample "
2003-02-24 13:12:27 +00:00
"are supported at the moment.\n");
exit(1);
}
codec_id.CopyBuffer((binary *)APCM, countof(APCM));
KaxTrackAudio &audio =
2003-02-24 13:12:27 +00:00
GetChild<KaxTrackAudio>(static_cast<KaxTrackEntry &>(*track_entry));
KaxAudioSamplingFreq &kax_freq = GetChild<KaxAudioSamplingFreq>(audio);
2003-02-24 13:12:27 +00:00
*(static_cast<EbmlFloat *>(&kax_freq)) = (float)samples_per_sec;
KaxAudioChannels &kax_chans = GetChild<KaxAudioChannels>(audio);
2003-02-24 13:12:27 +00:00
*(static_cast<EbmlUInteger *>(&kax_chans)) = channels;
}
int pcm_packetizer_c::process(unsigned char *buf, int size, int last_frame) {
2003-02-24 13:12:27 +00:00
int i, bytes_per_packet, remaining_bytes, complete_packets;
unsigned char *new_buf;
2003-02-24 13:12:27 +00:00
if (size > tempbuf_size) {
tempbuf = (unsigned char *)realloc(tempbuf, size + 128);
if (tempbuf == NULL)
die("realloc");
tempbuf_size = size;
2003-02-24 13:12:27 +00:00
}
2003-02-28 13:00:26 +00:00
new_buf = buf;
if (ti->async.displacement != 0) {
if (ti->async.displacement > 0) {
2003-02-28 13:00:26 +00:00
// Add silence.
int pad_size;
pad_size = bps * ti->async.displacement / 1000;
new_buf = (unsigned char *)malloc(size + pad_size);
2003-02-28 13:00:26 +00:00
if (new_buf == NULL)
die("malloc");
memset(new_buf, 0, pad_size);
memcpy(&new_buf[pad_size], buf, size);
size += pad_size;
} else
// Skip bytes.
remaining_sync = -1 * bps * ti->async.displacement / 1000;
ti->async.displacement = 0;
2003-02-28 13:00:26 +00:00
}
if (remaining_sync > 0) {
if (remaining_sync > size) {
remaining_sync -= size;
return EMOREDATA;
}
memmove(buf, &buf[remaining_sync], size - remaining_sync);
size -= remaining_sync;
remaining_sync = 0;
}
2003-02-24 13:12:27 +00:00
bytes_per_packet = bps / pcm_interleave;
complete_packets = size / bytes_per_packet;
remaining_bytes = size % bytes_per_packet;
for (i = 0; i < complete_packets; i++) {
2003-02-28 13:00:26 +00:00
add_packet(new_buf + i * bytes_per_packet, bytes_per_packet,
(bytes_output * 1000 / bps) * ti->async.linear);
2003-02-24 13:12:27 +00:00
bytes_output += bytes_per_packet;
2003-02-28 13:00:26 +00:00
packetno++;
2003-02-24 13:12:27 +00:00
}
if (remaining_bytes != 0) {
2003-02-28 13:00:26 +00:00
add_packet(new_buf + complete_packets * bytes_per_packet, remaining_bytes,
(bytes_output * 1000 / bps) * ti->async.linear);
2003-02-24 13:12:27 +00:00
bytes_output += remaining_bytes;
2003-02-28 13:00:26 +00:00
packetno++;
2003-02-24 13:12:27 +00:00
}
2003-02-28 13:00:26 +00:00
if (new_buf != buf)
free(new_buf);
2003-02-24 13:12:27 +00:00
if (last_frame)
return 0;
else
return EMOREDATA;
}