18 #include <QDataStream>
31 #define IO_BUFFER_SIZE 32768
43 QMutexLocker locker(&
s_lock);
47 void run(
void)
override
51 QMutexLocker locker(&
s_lock);
59 QMutexLocker locker(&
s_lock);
65 QMutexLocker locker(&
s_lock);
134 int Read(
void *data,
int size) {
140 return stream.readRawData(
static_cast<char*
>(data), size);
158 QMutexLocker locker(&
m_lock);
181 off_t remaining = bd->rbuffer_len - bd->rbuffer_cur;
184 buf_size = FFMIN(buf_size, remaining);
185 memcpy(buf, bd->rbuffer_text + bd->rbuffer_cur, buf_size);
186 bd->rbuffer_cur += buf_size;
199 offset = bd->rbuffer_cur + offset;
203 offset = bd->rbuffer_len - offset;
213 if ((offset < 0) || (offset > bd->rbuffer_len))
215 bd->rbuffer_cur = offset;
231 int ret = avcodec_decode_subtitle2(
m_decCtx, &sub, &got_sub_ptr, pkt);
237 sub.start_display_time = av_q2d(
m_stream->time_base) * pkt->dts * 1000;
238 sub.end_display_time = av_q2d(
m_stream->time_base) * (pkt->dts + pkt->duration) * 1000;
268 LOG(VB_VBI, LOG_INFO,
269 QString(
"Preparing to load subtitle file %1").arg(
m_fileName));
272 LOG(VB_VBI, LOG_INFO,
273 QString(
"Failed to load subtitle file %1").arg(
m_fileName));
284 LOG(VB_VBI, LOG_INFO,
285 QString(
"Failed to get file size for %1").arg(
m_fileName));
291 LOG(VB_VBI, LOG_INFO,
292 QString(
"Filesize unchanged (%1), not reloading subs (%2)")
297 LOG(VB_VBI, LOG_INFO,
298 QString(
"Preparing to read %1 subtitle bytes from %2")
301 sub_data.rbuffer_len = new_len;
302 sub_data.rbuffer_text =
new char[sub_data.rbuffer_len + 1];
303 sub_data.rbuffer_cur = 0;
306 int numread = rfile.
Read(sub_data.rbuffer_text, sub_data.rbuffer_len);
307 LOG(VB_VBI, LOG_INFO,
308 QString(
"Finished reading %1 subtitle bytes (requested %2)")
309 .arg(numread).arg(new_len));
312 AVFormatContext *fmt_ctx = avformat_alloc_context();
313 if (fmt_ctx ==
nullptr) {
314 LOG(VB_VBI, LOG_INFO,
"Couldn't allocate format context");
318 if (avio_ctx_buffer ==
nullptr)
320 LOG(VB_VBI, LOG_INFO,
"Couldn't allocate mamory for avio context");
321 avformat_free_context(fmt_ctx);
327 if(
int ret = avformat_open_input(&fmt_ctx,
nullptr,
nullptr,
nullptr); ret < 0) {
328 LOG(VB_VBI, LOG_INFO, QString(
"Couldn't open input context %1")
337 const AVCodec *codec {
nullptr};
338 int stream_num = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_SUBTITLE, -1, -1, &codec, 0);
339 if (stream_num < 0) {
340 LOG(VB_VBI, LOG_INFO, QString(
"Couldn't find subtitle stream. %1")
342 avformat_free_context(fmt_ctx);
345 m_stream = fmt_ctx->streams[stream_num];
347 LOG(VB_VBI, LOG_INFO, QString(
"Stream %1 is null").arg(stream_num));
348 avformat_free_context(fmt_ctx);
353 m_decCtx = avcodec_alloc_context3(codec);
355 LOG(VB_VBI, LOG_INFO, QString(
"Couldn't allocate decoder context"));
356 avformat_free_context(fmt_ctx);
359 if (avcodec_open2(
m_decCtx, codec,
nullptr) < 0) {
360 LOG(VB_VBI, LOG_INFO, QString(
"Couldn't open decoder context"));
362 avformat_free_context(fmt_ctx);
368 AVPacket *pkt = av_packet_alloc();
369 av_new_packet(pkt, 4096);
370 while (av_read_frame(fmt_ctx, pkt) >= 0)
380 pkt->data = pkt->buf->data;
381 pkt->size = pkt->buf->size;
391 LOG(VB_GENERAL, LOG_INFO, QString(
"Loaded %1 %2 subtitles from '%3'")
396 av_packet_free(&pkt);
398 avformat_free_context(fmt_ctx);
405 return {
reinterpret_cast<char*
>(
m_decCtx->subtitle_header),