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
|
2003-06-15 14:03:28 +00:00
|
|
|
\version $Id$
|
2003-02-24 13:12:27 +00:00
|
|
|
\brief PCM output module
|
2003-05-18 20:57:08 +00:00
|
|
|
\author Moritz Bunkus <moritz@bunkus.org>
|
2003-02-24 13:12:27 +00:00
|
|
|
*/
|
2003-05-20 06:30:25 +00:00
|
|
|
|
2003-02-24 13:12:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2003-04-18 10:08:24 +00:00
|
|
|
#include "mkvmerge.h"
|
2003-02-24 13:12:27 +00:00
|
|
|
#include "common.h"
|
2003-04-18 10:28:14 +00:00
|
|
|
#include "pr_generic.h"
|
2003-02-24 13:12:27 +00:00
|
|
|
#include "p_pcm.h"
|
2003-04-18 12:00:46 +00:00
|
|
|
#include "matroska.h"
|
2003-02-24 13:12:27 +00:00
|
|
|
|
2003-05-02 20:11:34 +00:00
|
|
|
using namespace LIBMATROSKA_NAMESPACE;
|
|
|
|
|
2003-05-05 21:55:02 +00:00
|
|
|
pcm_packetizer_c::pcm_packetizer_c(generic_reader_c *nreader,
|
|
|
|
unsigned long nsamples_per_sec,
|
2003-02-24 13:12:27 +00:00
|
|
|
int nchannels, int nbits_per_sample,
|
2003-03-05 13:51:20 +00:00
|
|
|
track_info_t *nti) throw (error_c):
|
2003-05-05 21:55:02 +00:00
|
|
|
generic_packetizer_c(nreader, nti) {
|
2003-02-24 13:12:27 +00:00
|
|
|
packetno = 0;
|
|
|
|
bps = nchannels * nbits_per_sample * nsamples_per_sec / 8;
|
2003-05-05 18:37:36 +00:00
|
|
|
tempbuf = (unsigned char *)safemalloc(bps + 128);
|
2003-03-05 17:44:32 +00:00
|
|
|
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-05-11 09:05:55 +00:00
|
|
|
|
|
|
|
set_track_type(track_audio);
|
2003-06-29 17:56:38 +00:00
|
|
|
set_track_default_duration_ns((int64_t)(1000000000.0 * ti->async.linear /
|
|
|
|
pcm_interleave));
|
2003-02-24 13:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pcm_packetizer_c::~pcm_packetizer_c() {
|
|
|
|
if (tempbuf != NULL)
|
2003-05-05 18:37:36 +00:00
|
|
|
safefree(tempbuf);
|
2003-02-24 13:12:27 +00:00
|
|
|
}
|
|
|
|
|
2003-05-02 21:49:42 +00:00
|
|
|
void pcm_packetizer_c::set_headers() {
|
2003-04-29 16:23:12 +00:00
|
|
|
set_codec_id(MKV_A_PCM);
|
2003-04-18 12:00:46 +00:00
|
|
|
set_audio_sampling_freq((float)samples_per_sec);
|
|
|
|
set_audio_channels(channels);
|
2003-04-29 16:23:12 +00:00
|
|
|
set_audio_bit_depth(bits_per_sample);
|
2003-02-24 13:12:27 +00:00
|
|
|
|
2003-05-02 21:49:42 +00:00
|
|
|
generic_packetizer_c::set_headers();
|
2003-02-24 13:12:27 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 11:23:54 +00:00
|
|
|
int pcm_packetizer_c::process(unsigned char *buf, int size,
|
2003-04-18 13:21:11 +00:00
|
|
|
int64_t, int64_t, int64_t, int64_t) {
|
2003-02-24 13:12:27 +00:00
|
|
|
int i, bytes_per_packet, remaining_bytes, complete_packets;
|
2003-03-04 10:16:28 +00:00
|
|
|
unsigned char *new_buf;
|
2003-02-24 13:12:27 +00:00
|
|
|
|
2003-05-25 15:35:39 +00:00
|
|
|
debug_enter("pcm_packetizer_c::process");
|
|
|
|
|
2003-05-20 06:30:25 +00:00
|
|
|
if (size > tempbuf_size) {
|
2003-05-05 18:37:36 +00:00
|
|
|
tempbuf = (unsigned char *)saferealloc(tempbuf, size + 128);
|
2003-03-05 17:44:32 +00:00
|
|
|
tempbuf_size = size;
|
2003-02-24 13:12:27 +00:00
|
|
|
}
|
|
|
|
|
2003-02-28 13:00:26 +00:00
|
|
|
new_buf = buf;
|
|
|
|
|
2003-03-05 13:51:20 +00:00
|
|
|
if (ti->async.displacement != 0) {
|
|
|
|
if (ti->async.displacement > 0) {
|
2003-02-28 13:00:26 +00:00
|
|
|
// Add silence.
|
|
|
|
int pad_size;
|
|
|
|
|
2003-03-05 13:51:20 +00:00
|
|
|
pad_size = bps * ti->async.displacement / 1000;
|
2003-05-05 18:37:36 +00:00
|
|
|
new_buf = (unsigned char *)safemalloc(size + pad_size);
|
2003-02-28 13:00:26 +00:00
|
|
|
memset(new_buf, 0, pad_size);
|
|
|
|
memcpy(&new_buf[pad_size], buf, size);
|
|
|
|
size += pad_size;
|
|
|
|
} else
|
|
|
|
// Skip bytes.
|
2003-03-05 13:51:20 +00:00
|
|
|
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;
|
2003-05-25 15:35:39 +00:00
|
|
|
debug_leave("pcm_packetizer_c::process");
|
2003-02-28 13:00:26 +00:00
|
|
|
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,
|
2003-05-06 07:51:24 +00:00
|
|
|
(int64_t)((bytes_output * 1000 / bps) * ti->async.linear),
|
|
|
|
(int64_t)(bytes_per_packet * 1000.0 * ti->async.linear / bps));
|
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,
|
2003-05-06 07:51:24 +00:00
|
|
|
(int64_t)((bytes_output * 1000 / bps) * ti->async.linear),
|
|
|
|
(int64_t)(remaining_bytes * 1000.0 * ti->async.linear / bps));
|
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)
|
2003-05-05 18:37:36 +00:00
|
|
|
safefree(new_buf);
|
2003-02-28 13:00:26 +00:00
|
|
|
|
2003-05-25 15:35:39 +00:00
|
|
|
debug_leave("pcm_packetizer_c::process");
|
|
|
|
|
2003-04-11 11:23:54 +00:00
|
|
|
return EMOREDATA;
|
2003-02-24 13:12:27 +00:00
|
|
|
}
|
2003-05-25 15:35:39 +00:00
|
|
|
|
|
|
|
void pcm_packetizer_c::dump_debug_info() {
|
|
|
|
fprintf(stderr, "DBG> pcm_packetizer_c: queue: %d\n", packet_queue.size());
|
|
|
|
}
|