mkvtoolnix/p_dts.cpp

256 lines
7.3 KiB
C++
Raw Normal View History

/*
mkvmerge -- utility for splicing together matroska files
from component media subtypes
p_dts.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
\version \$Id: p_dts.cpp,v 1.6 2003/05/20 06:30:24 mosu Exp $
\brief DTS output module
2003-05-18 20:57:08 +00:00
\author Moritz Bunkus <moritz@bunkus.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "pr_generic.h"
#include "dts_common.h"
#include "p_dts.h"
#include "matroska.h"
using namespace LIBMATROSKA_NAMESPACE;
bool operator!=(const dts_header_t &l, const dts_header_t &r) {
2003-05-18 20:53:33 +00:00
//if (l.frametype != r.frametype) return true;
//if (l.deficit_sample_count != r.deficit_sample_count) return true;
if (l.crc_present != r.crc_present)
return true;
2003-05-18 20:53:33 +00:00
if (l.num_pcm_sample_blocks != r.num_pcm_sample_blocks)
return true;
2003-05-18 20:53:33 +00:00
if (l.frame_byte_size != r.frame_byte_size)
return true;
2003-05-18 20:53:33 +00:00
if (l.audio_channels != r.audio_channels)
return true;
2003-05-18 20:53:33 +00:00
if (l.core_sampling_frequency != r.core_sampling_frequency)
return true;
2003-05-18 20:53:33 +00:00
if (l.transmission_bitrate != r.transmission_bitrate)
return true;
2003-05-18 20:53:33 +00:00
if (l.embedded_down_mix != r.embedded_down_mix)
return true;
2003-05-18 20:53:33 +00:00
if (l.embedded_dynamic_range != r.embedded_dynamic_range)
return true;
2003-05-18 20:53:33 +00:00
if (l.embedded_time_stamp != r.embedded_time_stamp)
return true;
2003-05-18 20:53:33 +00:00
if (l.auxiliary_data != r.auxiliary_data)
return true;
2003-05-18 20:53:33 +00:00
if (l.hdcd_master != r.hdcd_master)
return true;
2003-05-18 20:53:33 +00:00
if (l.extension_audio_descriptor != r.extension_audio_descriptor)
return true;
2003-05-18 20:53:33 +00:00
if (l.extended_coding != r.extended_coding)
return true;
2003-05-18 20:53:33 +00:00
if (l.audio_sync_word_in_sub_sub != r.audio_sync_word_in_sub_sub)
return true;
2003-05-18 20:53:33 +00:00
if (l.lfe_type != r.lfe_type)
return true;
2003-05-18 20:53:33 +00:00
if (l.predictor_history_flag != r.predictor_history_flag)
return true;
2003-05-18 20:53:33 +00:00
if (l.multirate_interpolator != r.multirate_interpolator)
return true;
2003-05-18 20:53:33 +00:00
if (l.encoder_software_revision != r.encoder_software_revision)
return true;
2003-05-18 20:53:33 +00:00
if (l.copy_history != r.copy_history)
return true;
2003-05-18 20:53:33 +00:00
if (l.source_pcm_resolution != r.source_pcm_resolution)
return true;
2003-05-18 20:53:33 +00:00
if (l.source_surround_in_es != r.source_surround_in_es)
return true;
2003-05-18 20:53:33 +00:00
if (l.front_sum_difference != r.front_sum_difference)
return true;
2003-05-18 20:53:33 +00:00
if (l.surround_sum_difference != r.surround_sum_difference)
return true;
2003-05-18 20:53:33 +00:00
if (l.dialog_normalization_gain != r.dialog_normalization_gain)
return true;
2003-05-18 20:53:33 +00:00
return false;
}
dts_packetizer_c::dts_packetizer_c(generic_reader_c *nreader,
const dts_header_t &dtsheader,
track_info_t *nti)
throw (error_c): generic_packetizer_c(nreader, nti) {
//packetno = 0;
samples_written = 0;
2003-05-20 06:27:08 +00:00
bytes_written = 0;
packet_buffer = NULL;
buffer_size = 0;
skipping_is_normal = false;
first_header = dtsheader;
last_header = dtsheader;
set_track_type(track_audio);
duplicate_data_on_add(false);
}
dts_packetizer_c::~dts_packetizer_c() {
2003-05-20 06:27:08 +00:00
fprintf(stderr,"wrote %lld bytes DTS data equivalent of %lld PCM samples at "
"%d Hz, that is %f seconds of sound\n",
bytes_written, samples_written, first_header.core_sampling_frequency,
((double)samples_written) /
(double)first_header.core_sampling_frequency);
safefree(packet_buffer);
}
void dts_packetizer_c::add_to_buffer(unsigned char *buf, int size) {
unsigned char *new_buffer;
new_buffer = (unsigned char *)saferealloc(packet_buffer, buffer_size + size);
memcpy(new_buffer + buffer_size, buf, size);
packet_buffer = new_buffer;
buffer_size += size;
}
int dts_packetizer_c::dts_packet_available() {
int pos;
dts_header_t dtsheader;
if (packet_buffer == NULL)
return 0;
pos = find_dts_header(packet_buffer, buffer_size, &dtsheader);
if (pos < 0)
return 0;
return 1;
}
void dts_packetizer_c::remove_dts_packet(int pos, int framesize) {
int new_size;
unsigned char *temp_buf;
new_size = buffer_size - (pos + framesize);
if (new_size != 0)
temp_buf = (unsigned char *)safememdup(&packet_buffer[pos + framesize],
new_size);
else
temp_buf = NULL;
safefree(packet_buffer);
packet_buffer = temp_buf;
buffer_size = new_size;
}
unsigned char *dts_packetizer_c::get_dts_packet(dts_header_t &dtsheader) {
int pos;
unsigned char *buf;
double pims;
if (packet_buffer == NULL)
return 0;
pos = find_dts_header(packet_buffer, buffer_size, &dtsheader);
if (pos < 0)
return 0;
if ((pos + dtsheader.frame_byte_size) > buffer_size)
return 0;
if (dtsheader != last_header) {
fprintf(stderr,"DTS header information changed! - New format:\n");
print_dts_header(&dtsheader);
last_header = dtsheader;
}
pims = get_dts_packet_length_in_milliseconds(&dtsheader);
if (ti->async.displacement < 0) {
/*
* DTS audio synchronization. displacement < 0 means skipping an
* appropriate number of packets at the beginning.
*/
ti->async.displacement += (int)pims;
if (ti->async.displacement > -(pims / 2))
ti->async.displacement = 0;
remove_dts_packet(pos, dtsheader.frame_byte_size);
return 0;
}
if (verbose && (pos > 0) && !skipping_is_normal)
fprintf(stdout, "dts_packetizer: skipping %d bytes (no valid DTS header "
"found). This might make audio/video go out of sync, but this "
"stream is damaged.\n", pos);
buf = (unsigned char *)safememdup(packet_buffer + pos,
dtsheader.frame_byte_size);
if (ti->async.displacement > 0) {
/*
* DTS audio synchronization. displacement > 0 is solved by duplicating
* the very first DTS packet as often as necessary. I cannot create
* a packet with total silence because I don't know how, and simply
* settings the packet's values to 0 does not work as the DTS header
* contains a CRC of its data.
*/
ti->async.displacement -= (int)pims;
if (ti->async.displacement < (pims / 2))
ti->async.displacement = 0;
return buf;
}
remove_dts_packet(pos, dtsheader.frame_byte_size);
return buf;
}
void dts_packetizer_c::set_headers() {
set_codec_id(MKV_A_DTS);
set_audio_sampling_freq((float)first_header.core_sampling_frequency);
set_audio_channels(first_header.audio_channels);
generic_packetizer_c::set_headers();
}
int dts_packetizer_c::process(unsigned char *buf, int size,
int64_t timecode, int64_t, int64_t, int64_t) {
int64_t my_timecode;
if (timecode != -1) my_timecode = timecode;
add_to_buffer(buf, size);
dts_header_t dtsheader;
unsigned char *packet;
while ((packet = get_dts_packet(dtsheader)) != NULL) {
int64_t packet_len_in_ms =
(int64_t)get_dts_packet_length_in_milliseconds(&dtsheader);
if (timecode == -1)
my_timecode = (int64_t)(((double)samples_written*1000.0) /
2003-05-18 20:53:33 +00:00
((double)dtsheader.core_sampling_frequency));
// fprintf(stderr,"DTS packet timecode %lld len %lld\n", my_timecode,
// packet_len_in_ms);
add_packet(packet, dtsheader.frame_byte_size, my_timecode,
packet_len_in_ms);
2003-05-20 06:27:08 +00:00
bytes_written += dtsheader.frame_byte_size;
samples_written += get_dts_packet_length_in_core_samples(&dtsheader);
}
return EMOREDATA;
}