2003-03-06 23:39:40 +00:00
|
|
|
/*
|
|
|
|
mkvmerge -- utility for splicing together matroska files
|
|
|
|
from component media subtypes
|
|
|
|
|
|
|
|
r_srt.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-03-06 23:39:40 +00:00
|
|
|
\brief Subripper subtitle reader
|
2003-05-18 20:57:08 +00:00
|
|
|
\author Moritz Bunkus <moritz@bunkus.org>
|
2003-03-06 23:39:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "pr_generic.h"
|
|
|
|
#include "p_textsubs.h"
|
2003-04-18 12:00:46 +00:00
|
|
|
#include "matroska.h"
|
2003-03-06 23:39:40 +00:00
|
|
|
|
2003-05-02 20:11:34 +00:00
|
|
|
using namespace LIBMATROSKA_NAMESPACE;
|
|
|
|
|
2003-05-05 21:55:02 +00:00
|
|
|
textsubs_packetizer_c::textsubs_packetizer_c(generic_reader_c *nreader,
|
2003-06-19 21:44:27 +00:00
|
|
|
const char *ncodec_id,
|
2003-06-19 14:25:42 +00:00
|
|
|
const void *nglobal_data,
|
2003-06-21 23:24:07 +00:00
|
|
|
int nglobal_size, bool nrecode,
|
2003-05-05 21:55:02 +00:00
|
|
|
track_info_t *nti)
|
|
|
|
throw (error_c): generic_packetizer_c(nreader, nti) {
|
2003-03-06 23:39:40 +00:00
|
|
|
packetno = 0;
|
2003-06-21 23:24:07 +00:00
|
|
|
recode = nrecode;
|
|
|
|
if (recode)
|
|
|
|
cc_utf8 = utf8_init(ti->sub_charset);
|
2003-06-19 14:25:42 +00:00
|
|
|
global_size = nglobal_size;
|
2003-06-19 21:44:27 +00:00
|
|
|
global_data = safememdup(nglobal_data, global_size);
|
|
|
|
codec_id = safestrdup(ncodec_id);
|
2003-05-11 09:05:55 +00:00
|
|
|
|
|
|
|
set_track_type(track_subtitle);
|
2003-03-06 23:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
textsubs_packetizer_c::~textsubs_packetizer_c() {
|
2003-06-19 14:25:42 +00:00
|
|
|
safefree(global_data);
|
2003-03-06 23:39:40 +00:00
|
|
|
}
|
|
|
|
|
2003-05-02 21:49:42 +00:00
|
|
|
void textsubs_packetizer_c::set_headers() {
|
2003-06-19 21:44:27 +00:00
|
|
|
set_codec_id(codec_id);
|
2003-06-19 14:25:42 +00:00
|
|
|
if (global_data != NULL)
|
|
|
|
set_codec_private((unsigned char *)global_data, global_size);
|
2003-04-18 12:00:46 +00:00
|
|
|
|
2003-05-02 21:49:42 +00:00
|
|
|
generic_packetizer_c::set_headers();
|
2003-04-20 21:22:19 +00:00
|
|
|
|
|
|
|
track_entry->EnableLacing(false);
|
2003-03-06 23:39:40 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 11:23:40 +00:00
|
|
|
int textsubs_packetizer_c::process(unsigned char *_subs, int, int64_t start,
|
2003-04-18 13:21:11 +00:00
|
|
|
int64_t length, int64_t, int64_t) {
|
2003-03-06 23:39:40 +00:00
|
|
|
int num_newlines;
|
2003-05-29 18:50:03 +00:00
|
|
|
char *subs, *idx1, *idx2, *utf8_subs;
|
2003-04-20 21:22:19 +00:00
|
|
|
int64_t end;
|
2003-03-06 23:39:40 +00:00
|
|
|
|
2003-04-11 11:23:40 +00:00
|
|
|
end = start + length;
|
2003-03-06 23:39:40 +00:00
|
|
|
// Adjust the start and end values according to the audio adjustment.
|
|
|
|
start += ti->async.displacement;
|
|
|
|
start = (int64_t)(ti->async.linear * start);
|
|
|
|
end += ti->async.displacement;
|
|
|
|
end = (int64_t)(ti->async.linear * end);
|
|
|
|
|
|
|
|
if (end < 0)
|
|
|
|
return EMOREDATA;
|
|
|
|
else if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
|
2003-04-20 21:22:19 +00:00
|
|
|
if (length < 0) {
|
2003-03-06 23:39:40 +00:00
|
|
|
fprintf(stderr, "Warning: textsubs_packetizer: Ignoring an entry which "
|
|
|
|
"starts after it ends.\n");
|
|
|
|
return EMOREDATA;
|
|
|
|
}
|
|
|
|
|
2003-04-20 21:22:19 +00:00
|
|
|
// Count the number of lines.
|
2003-04-11 11:23:40 +00:00
|
|
|
idx1 = (char *)_subs;
|
2003-03-06 23:39:40 +00:00
|
|
|
subs = NULL;
|
|
|
|
num_newlines = 0;
|
|
|
|
while (*idx1 != 0) {
|
|
|
|
if (*idx1 == '\n')
|
|
|
|
num_newlines++;
|
|
|
|
idx1++;
|
|
|
|
}
|
2003-05-05 18:37:36 +00:00
|
|
|
subs = (char *)safemalloc(strlen((char *)_subs) + num_newlines * 2 + 1);
|
2003-03-06 23:39:40 +00:00
|
|
|
|
2003-04-20 21:22:19 +00:00
|
|
|
// Unify the new lines into DOS style newlines.
|
2003-04-11 11:23:40 +00:00
|
|
|
idx1 = (char *)_subs;
|
2003-03-06 23:39:40 +00:00
|
|
|
idx2 = subs;
|
|
|
|
while (*idx1 != 0) {
|
|
|
|
if (*idx1 == '\n') {
|
|
|
|
*idx2 = '\r';
|
|
|
|
idx2++;
|
|
|
|
*idx2 = '\n';
|
|
|
|
idx2++;
|
|
|
|
} else if (*idx1 != '\r') {
|
|
|
|
*idx2 = *idx1;
|
|
|
|
idx2++;
|
|
|
|
}
|
|
|
|
idx1++;
|
|
|
|
}
|
|
|
|
if (idx2 != subs) {
|
|
|
|
while (((idx2 - 1) != subs) &&
|
|
|
|
((*(idx2 - 1) == '\n') || (*(idx2 - 1) == '\r'))) {
|
|
|
|
*idx2 = 0;
|
|
|
|
idx2--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*idx2 = 0;
|
|
|
|
|
2003-06-21 23:24:07 +00:00
|
|
|
if (recode) {
|
|
|
|
utf8_subs = to_utf8(cc_utf8, subs);
|
|
|
|
add_packet((unsigned char *)utf8_subs, strlen(utf8_subs), start, length,
|
|
|
|
1, -1, -1);
|
|
|
|
safefree(utf8_subs);
|
|
|
|
} else
|
|
|
|
add_packet((unsigned char *)subs, strlen(subs), start, length, 1, -1, -1);
|
2003-03-06 23:39:40 +00:00
|
|
|
|
2003-05-05 18:37:36 +00:00
|
|
|
safefree(subs);
|
2003-03-06 23:39:40 +00:00
|
|
|
|
|
|
|
return EMOREDATA;
|
|
|
|
}
|
2003-05-25 15:35:39 +00:00
|
|
|
|
|
|
|
void textsubs_packetizer_c::dump_debug_info() {
|
|
|
|
fprintf(stderr, "DBG> textsubs_packetizer_c: queue: %d\n",
|
|
|
|
packet_queue.size());
|
|
|
|
}
|