14 #include "libavutil/avutil.h"
15 #include "libavutil/error.h"
16 #include "libavutil/intreadwrite.h"
17 #include "libavutil/log.h"
18 #include "libavutil/opt.h"
19 #include "libavcodec/avcodec.h"
20 #include "libavformat/avformat.h"
21 #include "libavformat/avio.h"
22 #include "libswscale/swscale.h"
23 #include "libavutil/stereo3d.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/display.h"
28 #ifdef USING_MEDIACODEC // Android
30 #include "libavcodec/jni.h"
32 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
33 #include <QtAndroidExtras>
35 #include <QJniEnvironment>
36 #define QAndroidJniEnvironment QJniEnvironment
64 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
67 #include <QStringDecoder>
80 #include "libmythbase/mythconfig.h"
109 # ifdef AV_TIME_BASE_Q
110 # undef AV_TIME_BASE_Q
112 #define AV_TIME_BASE_Q GetAVTimeBaseQ()
114 __inline AVRational GetAVTimeBaseQ()
116 AVRational av = {1, AV_TIME_BASE};
121 #define LOC QString("AFD: ")
131 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
141 return {ctx.width >> ctx.lowres, ctx.height >> ctx.lowres};
145 float aspect_ratio = 0.0F;
147 if (ctx.sample_aspect_ratio.num && ctx.height)
149 aspect_ratio = av_q2d(ctx.sample_aspect_ratio) *
150 static_cast<double>(ctx.width);
151 aspect_ratio /= (float) ctx.height;
154 if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
157 aspect_ratio = (float)ctx.width / (
float)ctx.height;
159 aspect_ratio = 4.0F / 3.0F;
166 static constexpr
float kDefaultAspect = 4.0F / 3.0F;
167 int asp =
p.aspectRatio();
170 case 0:
return kDefaultAspect;
171 case 2:
return 4.0F / 3.0F;
172 case 3:
return 16.0F / 9.0F;
173 case 4:
return 2.21F;
177 float aspect_ratio = asp * 0.000001F;
178 if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
180 if (
p.pictureHeight() &&
p.pictureWidth())
183 (float)
p.pictureWidth() /(float)
p.pictureHeight();
187 aspect_ratio = kDefaultAspect;
208 #define FAIL(errmsg) do { \
209 LOG(VB_PLAYBACK, LOG_INFO, LOC + (errmsg)); \
220 switch (Stream->codecpar->codec_type)
224 case AVMEDIA_TYPE_VIDEO:
226 FAIL(
"No codec for video stream");
227 if (!Stream->codecpar->width || !Stream->codecpar->height)
228 FAIL(
"Unspecified video size");
229 if (Stream->codecpar->format == AV_PIX_FMT_NONE)
230 FAIL(
"Unspecified video pixel format");
237 case AVMEDIA_TYPE_AUDIO:
239 FAIL(
"No codec for audio stream");
257 case AVMEDIA_TYPE_SUBTITLE:
258 if (Stream->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !Stream->codecpar->width)
259 FAIL(
"Unspecified subtitle size");
261 case AVMEDIA_TYPE_DATA:
262 if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
269 if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
270 FAIL(
"Unknown codec");
274 static void myth_av_log(
void *ptr,
int level,
const char* fmt, va_list vl)
282 static QString s_fullLine(
"");
283 static QMutex s_stringLock;
284 uint64_t verbose_mask = VB_LIBAV;
285 LogLevel_t verbose_level = LOG_EMERG;
291 verbose_level = LOG_EMERG;
292 verbose_mask |= VB_GENERAL;
295 verbose_level = LOG_CRIT;
296 verbose_mask |= VB_GENERAL;
299 verbose_level = LOG_ERR;
302 verbose_level = LOG_WARNING;
305 verbose_level = LOG_INFO;
310 verbose_level = LOG_DEBUG;
320 if (s_fullLine.isEmpty() && ptr) {
321 AVClass* avc = *(AVClass**)ptr;
322 s_fullLine = QString(
"[%1 @ %2] ")
323 .arg(avc->item_name(ptr))
324 .arg((quintptr)avc, QT_POINTER_SIZE * 2, 16, QChar(
'0'));
327 s_fullLine += QString::vasprintf(fmt, vl);
328 if (s_fullLine.endsWith(
"\n"))
330 LOG(verbose_mask, verbose_level, s_fullLine.trimmed());
331 s_fullLine.truncate(0);
333 s_stringLock.unlock();
338 if (lang_cstr[0] ==
'\0' || lang_cstr[1] ==
'\0')
342 if (lang_cstr[2] ==
'\0')
344 QString tmp2 = lang_cstr;
362 case AVMEDIA_TYPE_UNKNOWN:
return "Unknown";
363 case AVMEDIA_TYPE_VIDEO:
return "Video";
364 case AVMEDIA_TYPE_AUDIO:
return "Audio";
365 case AVMEDIA_TYPE_DATA:
return "Data";
366 case AVMEDIA_TYPE_SUBTITLE:
return "Subtitle";
367 case AVMEDIA_TYPE_ATTACHMENT:
return "Attachment";
368 default:
return "Invalid Codec Type";
378 m_playerFlags(flags),
398 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"PlayerFlags: 0x%1, AudioReadAhead: %2 msec")
407 av_packet_free(&pkt);
425 lcd->setAudioFormatLEDs(
AUDIO_AC3,
false);
426 lcd->setVideoFormatLEDs(
VIDEO_MPG,
false);
443 for (
uint i = 0; i <
m_ic->nb_streams; i++)
445 AVStream *st =
m_ic->streams[i];
458 av_free(
m_ic->pb->buffer);
460 avformat_close_input(&
m_ic);
466 static int64_t
lsb3full(int64_t lsb, int64_t base_ts,
int lsb_bits)
468 int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
469 return ((lsb - base_ts)&mask);
474 int64_t start_pts = 0;
476 AVStream *st =
nullptr;
477 for (
uint i = 0; i <
m_ic->nb_streams; i++)
479 AVStream *st1 =
m_ic->streams[i];
480 if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
489 if (
m_ic->start_time != AV_NOPTS_VALUE)
491 start_pts = av_rescale(
m_ic->start_time,
493 AV_TIME_BASE * (int64_t)st->time_base.num);
496 int64_t
pts = av_rescale(timecode.count() / 1000.0,
507 std::chrono::milliseconds timecode)
509 int64_t start_pts = 0;
511 if (
m_ic->start_time != AV_NOPTS_VALUE)
513 start_pts = av_rescale(
m_ic->start_time,
515 AV_TIME_BASE * (int64_t)st->time_base.num);
518 int64_t
pts = av_rescale(timecode.count() / 1000.0,
531 return m_ic->nb_chapters;
541 for (
int i = 0; i < total; i++)
543 int num =
m_ic->chapters[i]->time_base.num;
544 int den =
m_ic->chapters[i]->time_base.den;
545 int64_t start =
m_ic->chapters[i]->start;
546 long double total_secs = (
long double)start * (
long double)num /
548 times.push_back(std::chrono::seconds((
long long)total_secs));
557 for (
int i = (
m_ic->nb_chapters - 1); i > -1 ; i--)
559 int num =
m_ic->chapters[i]->time_base.num;
560 int den =
m_ic->chapters[i]->time_base.den;
561 int64_t start =
m_ic->chapters[i]->start;
562 long double total_secs = (
long double)start * (
long double)num /
564 auto framenum = (
long long)(total_secs *
m_fps);
565 if (framesPlayed >= framenum)
567 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
568 QString(
"GetCurrentChapter(selected chapter %1 framenum %2)")
569 .arg(i + 1).arg(framenum));
581 int num =
m_ic->chapters[chapter - 1]->time_base.num;
582 int den =
m_ic->chapters[chapter - 1]->time_base.den;
583 int64_t start =
m_ic->chapters[chapter - 1]->start;
584 long double total_secs = (
long double)start * (
long double)num /
586 auto framenum = (
long long)(total_secs *
m_fps);
587 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"GetChapter %1: framenum %2")
588 .arg(chapter).arg(framenum));
594 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"DoRewind(%1, %2 discard frames)")
595 .arg(desiredFrame).arg( discardFrames ?
"do" :
"don't" ));
608 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
609 QString(
"DoFastForward(%1 (%2), %3 discard frames)")
611 .arg((discardFrames) ?
"do" :
"don't"));
622 if (seekDelta >= 0 && seekDelta < 2 && !m_doRewind && m_parent->GetPlaySpeed() == 0.0F)
631 if (
m_ic->start_time != AV_NOPTS_VALUE)
632 ts =
m_ic->start_time;
635 long double seekts = desiredFrame * AV_TIME_BASE /
m_fps;
636 ts += (
long long)seekts;
641 int flags = (
m_doRewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
643 if (av_seek_frame(
m_ic, -1, ts, flags) < 0)
645 LOG(VB_GENERAL, LOG_ERR,
LOC +
646 QString(
"av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
651 reader->SeekFrame(ts, flags);
653 int normalframes = 0;
675 bool doflush,
bool discardFrames)
680 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
681 QString(
"SeekReset(%1, %2, %3 flush, %4 discard)")
682 .arg(newKey).arg(skipFrames)
683 .arg((doflush) ?
"do" :
"don't",
684 (discardFrames) ?
"do" :
"don't"));
709 avformat_flush(
m_ic);
716 m_ic->pb->buf_ptr =
m_ic->pb->buffer;
717 m_ic->pb->buf_end =
m_ic->pb->buffer;
718 m_ic->pb->eof_reached = 0;
722 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"SeekReset() flushing");
723 for (
uint i = 0; i <
m_ic->nb_streams; i++)
729 if (enc && enc->internal)
730 avcodec_flush_buffers(enc);
737 av_packet_free(&pkt);
759 static constexpr std::chrono::milliseconds maxSeekTimeMs { 200ms };
760 int profileFrames = 0;
762 for (; (skipFrames > 0 && !
m_atEof &&
763 (exactSeeks || begin.
elapsed() < maxSeekTimeMs));
764 --skipFrames, ++profileFrames)
768 QElapsedTimer getframetimer;
769 getframetimer.start();
771 while (retry && !getframetimer.hasExpired(100))
776 std::this_thread::sleep_for(1ms);
784 if (!exactSeeks && profileFrames >= 5 && profileFrames < 10)
786 const int giveUpPredictionMs = 400;
787 int remainingTimeMs =
788 skipFrames * (float)begin.
elapsed().count() / profileFrames;
789 if (remainingTimeMs > giveUpPredictionMs)
791 LOG(VB_PLAYBACK, LOG_DEBUG,
792 QString(
"Frame-by-frame seeking would take "
793 "%1 ms to finish, skipping.").arg(remainingTimeMs));
810 LOG(VB_GENERAL, LOG_NOTICE,
LOC +
811 QString(
"Resetting byte context eof (livetv %1 was eof %2)")
813 m_ic->pb->eof_reached = 0;
821 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
822 QString(
"Reset: Video %1, Seek %2, File %3")
823 .arg(reset_video_data).arg(seek_reset).arg(reset_file));
830 if (reset_video_data)
840 memset(&probe, 0,
sizeof(AVProbeData));
842 QByteArray fname =
filename.toLatin1();
843 probe.filename = fname.constData();
844 probe.buf = (
unsigned char *)testbuf.data();
845 probe.buf_size = testbuf.size();
847 int score = AVPROBE_SCORE_MAX/4;
859 memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
861 return av_probe_input_format2(&probe,
static_cast<int>(
true), &score) !=
nullptr;
873 auto *buffer = (
unsigned char *)av_malloc(buf_size);
874 m_ic->pb = avio_alloc_context(buffer, buf_size, 0,
881 m_ic->pb->seekable =
static_cast<int>(!streamed || forceseek);
882 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Buffer size: %1 Streamed %2 Seekable %3 Available %4")
890 int cnt = decoder->
m_ic->nb_streams;
892 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
893 QString(
"streams_changed 0x%1 -- stream count %2")
894 .arg((uint64_t)data,0,16).arg(cnt));
896 decoder->m_streamsChanged =
true;
902 int retval = avformat_find_stream_info(
m_ic,
nullptr);
943 const AVInputFormat *fmt =
nullptr;
945 QByteArray fnamea = fnames.toLatin1();
946 const char *
filename = fnamea.constData();
949 memset(&probe, 0,
sizeof(AVProbeData));
951 probe.buf =
reinterpret_cast<unsigned char *
>(testbuf.data());
953 probe.buf_size = testbuf.size();
956 memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
958 fmt = av_probe_input_format(&probe,
static_cast<int>(
true));
961 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Probe failed for '%1'").arg(
filename));
965 if (strcmp(fmt->name,
"mpegts") == 0 &&
968 const AVInputFormat *fmt2 = av_find_input_format(
"mpegts-ffmpeg");
972 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Using FFmpeg MPEG-TS demuxer (forced)");
977 bool scancomplete =
false;
978 int remainingscans = 5;
980 while (!scancomplete && remainingscans--)
994 while (!found && --retries)
996 m_ic = avformat_alloc_context();
999 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Could not allocate format context.");
1006 err = avformat_open_input(&
m_ic,
filename, fmt,
nullptr);
1010 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Failed to open input ('%1')")
1018 QThread::usleep(100000);
1024 if (strcmp(fmt->name,
"mpegts") == 0)
1026 fmt = av_find_input_format(
"mpegts-ffmpeg");
1029 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Attempting to use original FFmpeg MPEG-TS demuxer.");
1042 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Fatal error opening input. Aborting");
1054 m_ic->max_analyze_duration = 60LL * AV_TIME_BASE;
1060 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Could not find codec parameters for '%1'").arg(
filename));
1067 scancomplete =
true;
1072 scancomplete =
false;
1076 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Stream scan incomplete - retrying");
1077 QThread::usleep(250000);
1085 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Scan incomplete - playback may not work");
1088 m_ic->stream_change_data =
this;
1100 QString extension = QFileInfo(fnames).suffix();
1101 if (strcmp(fmt->name,
"mp3") == 0 || strcmp(fmt->name,
"flac") == 0 ||
1102 strcmp(fmt->name,
"ogg") == 0 ||
1103 (extension.compare(
"m4a", Qt::CaseInsensitive) == 0))
1120 int initialAudio = -1;
1121 int initialVideo = -1;
1124 if (initialAudio >= 0)
1126 if (initialVideo >= 0)
1129 #endif // USING_MHEG
1147 std::chrono::seconds dur = 0s;
1156 dur = duration_cast<std::chrono::seconds>(av_duration(
m_ic->duration));
1167 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1168 "Recording has no position -- using libavformat seeking.");
1177 float bytespersec = (float)
m_bitrate / 8 / 2;
1180 (int)(secs *
static_cast<float>(
m_fps)));
1189 if (strcmp(fmt->name,
"avi") == 0)
1202 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Position map found");
1204 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Partial position map found");
1205 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1206 QString(
"Successfully opened decoder for file: \"%1\". novideo(%2)")
1210 for (
unsigned int i=0; i <
m_ic->nb_chapters; i++)
1212 int num =
m_ic->chapters[i]->time_base.num;
1213 int den =
m_ic->chapters[i]->time_base.den;
1214 int64_t start =
m_ic->chapters[i]->start;
1215 auto total_secs =
static_cast<long double>(start) *
static_cast<long double>(num) /
1216 static_cast<long double>(den);
1218 auto framenum =
static_cast<long long>(total_secs *
static_cast<long double>(
m_fps));
1219 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1220 QString(
"Chapter %1 found @ [%2]->%3")
1223 QString::number(framenum)));
1226 if (qEnvironmentVariableIsSet(
"FORCE_DTS_TIMESTAMPS"))
1239 Reset(
true,
true,
true);
1253 double avg_fps = (Stream->avg_frame_rate.den == 0) ? 0.0 : av_q2d(Stream->avg_frame_rate);
1254 double codec_fps = av_q2d(Context->framerate);
1255 double container_fps = (Stream->time_base.num == 0) ? 0.0 : av_q2d(av_inv_q(Stream->time_base));
1257 double estimated_fps = (Stream->r_frame_rate.den == 0) ? 0.0 : av_q2d(Stream->r_frame_rate);
1260 std::vector<double> rates;
1265 if (QString(
m_ic->iformat->name).contains(
"matroska") ||
1266 QString(
m_ic->iformat->name).contains(
"mov"))
1268 rates.emplace_back(avg_fps);
1272 if (QString(
m_ic->iformat->name).contains(
"avi"))
1274 rates.emplace_back(container_fps);
1277 rates.emplace_back(codec_fps);
1278 rates.emplace_back(container_fps);
1279 rates.emplace_back(avg_fps);
1281 rates.emplace_back(estimated_fps);
1283 rates.emplace_back(30000.0 / 1001.0);
1285 auto invalid_fps = [](
double rate) {
return rate < 3.0 || rate > 121.0; };
1286 rates.erase(std::remove_if(rates.begin(), rates.end(), invalid_fps), rates.end());
1288 auto FuzzyEquals = [](
double First,
double Second) {
return std::abs(First - Second) < 0.03; };
1291 if (!FuzzyEquals(rates.front(),
m_fps))
1293 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1294 QString(
"Selected FPS: %1 (Avg:%2 Mult:%3 Codec:%4 Container:%5 Estimated:%6)")
1295 .arg(
static_cast<double>(rates.front())).arg(avg_fps)
1296 .arg(
m_fpsMultiplier).arg(codec_fps).arg(container_fps).arg(estimated_fps));
1299 auto IsStandard = [&FuzzyEquals](
double Rate)
1302 static const std::set<double> k_standard_rates =
1321 if (Rate > 23.0 && Rate < 121.0)
1323 for (
auto standard_rate : k_standard_rates)
1324 if (FuzzyEquals(Rate, standard_rate))
1333 double detected = rates.front();
1334 if (Sanitise && !IsStandard(detected))
1336 for (
auto rate : rates)
1338 if (IsStandard(rate))
1340 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"%1 is non-standard - using %2 instead.")
1341 .arg(rates.front()).arg(rate));
1349 if (rate > 33.0 && detected < 33.0)
1351 double half = rate / 2.0;
1352 if (std::abs(half - detected) < (half * 0.1))
1354 LOG(VB_GENERAL, LOG_INFO,
LOC +
1355 QString(
"Assuming %1 is a better choice than %2")
1356 .arg(half).arg(rate));
1357 return static_cast<float>(half);
1360 return static_cast<float>(rate);
1365 return static_cast<float>(detected);
1370 switch (Context->codec_id)
1372 case AV_CODEC_ID_H264:
1375 if (Context->extradata && (Context->extradata_size >= 7))
1378 if (Context->extradata[0] == 1)
1380 else if (AV_RB24(Context->extradata) == 0x01)
1382 else if (
AV_RB32(Context->extradata) == 0x01)
1389 parser.parse_SPS(Context->extradata + offset,
1390 static_cast<uint>(Context->extradata_size - offset), dummy, result);
1395 case AV_CODEC_ID_H265:
return 16;
1396 case AV_CODEC_ID_VP9:
return 8;
1397 case AV_CODEC_ID_VP8:
return 3;
1403 bool selectedStream)
1405 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1406 QString(
"InitVideoCodec ID:%1 Type:%2 Size:%3x%4")
1407 .arg(avcodec_get_name(enc->codec_id),
1409 .arg(enc->width).arg(enc->height));
1414 enc->opaque =
static_cast<void*
>(
this);
1416 enc->slice_flags = 0;
1418 enc->err_recognition = AV_EF_COMPLIANT;
1419 enc->workaround_bugs = FF_BUG_AUTODETECT;
1420 enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
1421 enc->idct_algo = FF_IDCT_AUTO;
1425 const AVCodec *codec1 = enc->codec;
1431 uint8_t* displaymatrix = av_stream_get_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX,
nullptr);
1433 m_videoRotation =
static_cast<int>(-av_display_rotation_get(
reinterpret_cast<int32_t*
>(displaymatrix)));
1438 uint8_t* stereo3d = av_stream_get_side_data(stream, AV_PKT_DATA_STEREO3D,
nullptr);
1441 auto * avstereo =
reinterpret_cast<AVStereo3D*
>(stereo3d);
1466 bool doublerate =
true;
1470 if (codec1 && ((AV_CODEC_ID_MPEG2VIDEO == codec1->id) ||
1471 (AV_CODEC_ID_MPEG1VIDEO == codec1->id)))
1475 int total_blocks = (enc->height + 15) / 16;
1476 enc->skip_top = (total_blocks + 3) / 4;
1477 enc->skip_bottom = (total_blocks + 3) / 4;
1485 enc->flags &= ~AV_CODEC_FLAG_LOOP_FILTER;
1486 enc->skip_loop_filter = AVDISCARD_ALL;
1490 enc->skip_idct = AVDISCARD_ALL;
1502 if (!width || !height)
1504 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1505 "InitVideoCodec invalid dimensions, resetting decoder.");
1513 const AVCodec *codec2 = enc->codec;
1516 codecName = codec2->name;
1524 switch (enc->codec_id)
1526 case AV_CODEC_ID_H263:
1527 case AV_CODEC_ID_MPEG4:
1528 case AV_CODEC_ID_MSMPEG4V1:
1529 case AV_CODEC_ID_MSMPEG4V2:
1530 case AV_CODEC_ID_MSMPEG4V3:
1531 case AV_CODEC_ID_H263P:
1532 case AV_CODEC_ID_H263I:
1535 case AV_CODEC_ID_WMV1:
1536 case AV_CODEC_ID_WMV2:
1540 case AV_CODEC_ID_XVID:
1549 lcd->setVideoFormatLEDs(video_format,
true);
1561 static constexpr std::array<uint8_t, 256> odd_parity_LUT
1563 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1564 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1565 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1566 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1567 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1568 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1569 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1570 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1571 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1572 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1573 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1574 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1575 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1576 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1577 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1578 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1580 bool ret = (odd_parity_LUT[data & 0xff] == 1) &&
1581 (odd_parity_LUT[(data & 0xff00) >> 8] == 1);
1584 LOG(VB_VBI, LOG_ERR,
LOC +
1585 QString(
"VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
1599 if (!
m_ic->pmt_section)
1601 LOG(VB_GENERAL, LOG_DEBUG,
LOC +
1602 "ScanATSCCaptionStreams() called with no PMT");
1607 if (!pmt_buffer.has_buffer())
1613 bool video_found =
false;
1637 desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
1639 for (
auto & desc : desc_list)
1684 std::array<std::map<int,uint>,2> lang_cc_cnt;
1701 else if (!pofr && sofr)
1727 LOG(VB_GENERAL, LOG_ERR,
LOC +
"in_tracks key too small");
1733 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1734 QString(
"%1 caption service #%2 is in the %3 language.")
1735 .arg((
type) ?
"EIA-708" :
"EIA-608")
1750 if (!pmt_buffer.has_buffer())
1765 for (
const auto *desc : desc_list)
1778 int lang_idx = (magazine << 8) | pagenum;
1779 StreamInfo si(av_index, language, lang_idx, 0, 0);
1785 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1786 QString(
"Teletext stream #%1 (%2) is in the %3 language"
1788 .arg(QString::number(k),
1789 (
type == 2) ?
"Caption" :
"Menu",
1791 QString::number(magazine),
1792 QString::number(pagenum)));
1807 AVDictionaryEntry *metatag =
1808 av_dict_get(
m_ic->streams[av_stream_index]->metadata,
"language",
nullptr,
1810 bool forced = (
m_ic->streams[av_stream_index]->disposition & AV_DISPOSITION_FORCED) != 0;
1813 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1814 QString(
"Text Subtitle track #%1 is A/V stream #%2 "
1815 "and is in the %3 language(%4), forced=%5.")
1818 StreamInfo si(av_stream_index, lang, 0, 0, 0,
false,
false, forced);
1835 if (!pmt_buffer.has_buffer())
1846 LOG(VB_DSMCC, LOG_NOTICE, QString(
"ScanDSMCCStreams Found Object Carousel in Stream %1").arg(QString::number(i)));
1852 for (
const auto *desc : desc_list)
1855 uint length = *desc++;
1856 const unsigned char *endDesc = desc+length;
1857 uint dataBroadcastId = desc[0]<<8 | desc[1];
1858 LOG(VB_DSMCC, LOG_NOTICE, QString(
"ScanDSMCCStreams dataBroadcastId %1").arg(QString::number(dataBroadcastId)));
1859 if (dataBroadcastId != 0x0106)
1862 while (desc != endDesc)
1864 [[maybe_unused]]
uint appTypeCode = desc[0]<<8 | desc[1];
1866 uint appSpecDataLen = *desc++;
1868 LOG(VB_DSMCC, LOG_NOTICE, QString(
"ScanDSMCCStreams AppTypeCode %1").arg(QString::number(appTypeCode)));
1869 if (appTypeCode == 0x101)
1871 const unsigned char *subDescEnd = desc + appSpecDataLen;
1872 while (desc < subDescEnd)
1874 uint sub_desc_tag = *desc++;
1875 uint sub_desc_len = *desc++;
1877 if (sub_desc_tag == 1)
1879 desc += sub_desc_len;
1883 #endif // USING_MHEG
1885 desc += appSpecDataLen;
1897 bool unknownbitrate =
false;
1914 std::map<int,uint> lang_sub_cnt;
1915 uint subtitleStreamCount = 0;
1916 std::map<int,uint> lang_aud_cnt;
1917 uint audioStreamCount = 0;
1926 if (
m_ic ==
nullptr)
1929 for (
uint strm = 0; strm <
m_ic->nb_streams; strm++)
1931 AVCodecParameters *par =
m_ic->streams[strm]->codecpar;
1932 AVCodecContext *enc =
nullptr;
1935 if (par->codec_type == AVMEDIA_TYPE_VIDEO)
1936 codectype += QString(
"(%1x%2)").arg(par->width).arg(par->height);
1937 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
1938 QString(
"Stream #%1: ID: 0x%2 Codec ID: %3 Type: %4 Bitrate: %5")
1939 .arg(strm).arg(
static_cast<uint64_t
>(
m_ic->streams[strm]->id), 0, 16)
1940 .arg(avcodec_get_name(par->codec_id),
1941 codectype).arg(par->bit_rate));
1943 switch (par->codec_type)
1945 case AVMEDIA_TYPE_VIDEO:
1953 if (ctx && (ctx->hw_frames_ctx || ctx->hw_device_ctx))
1960 LOG(VB_GENERAL, LOG_ERR,
LOC +
1961 QString(
"Stream #%1 has an unknown video "
1962 "codec id, skipping.").arg(strm));
1974 if (par->bit_rate == 0)
1976 static constexpr int64_t s_baseBitrate { 1000000LL };
1978 if (par->width && par->height)
1980 static const int s_baseSize = 1920 * 1080;
1981 multiplier = ((par->width * par->height) + s_baseSize - 1) / s_baseSize;
1985 par->bit_rate = s_baseBitrate * multiplier;
1986 unknownbitrate =
true;
1992 case AVMEDIA_TYPE_AUDIO:
1995 if (enc && enc->internal)
1997 LOG(VB_GENERAL, LOG_WARNING,
LOC +
1998 QString(
"Warning, audio codec 0x%1 id(%2) "
1999 "type (%3) already open, leaving it alone.")
2000 .arg(
reinterpret_cast<unsigned long long>(enc), 0, 16)
2001 .arg(avcodec_get_name(enc->codec_id),
2004 LOG(VB_GENERAL, LOG_INFO,
LOC +
2005 QString(
"codec %1 has %2 channels")
2006 .arg(avcodec_get_name(par->codec_id))
2007 .arg(par->ch_layout.nb_channels));
2012 case AVMEDIA_TYPE_SUBTITLE:
2014 if (par->codec_id == AV_CODEC_ID_DVB_TELETEXT)
2016 if (par->codec_id == AV_CODEC_ID_TEXT)
2020 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"subtitle codec (%1)")
2024 case AVMEDIA_TYPE_DATA:
2028 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"data codec (%1)")
2032 case AVMEDIA_TYPE_ATTACHMENT:
2034 if (par->codec_id == AV_CODEC_ID_TTF)
2036 static_cast<int>(strm), 0, 0,
m_ic->streams[strm]->id, 0);
2038 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2039 QString(
"Attachment codec (%1)")
2046 LOG(VB_PLAYBACK, LOG_ERR,
LOC +
2047 QString(
"Unknown codec type (%1)")
2053 if (par->codec_type != AVMEDIA_TYPE_AUDIO &&
2054 par->codec_type != AVMEDIA_TYPE_SUBTITLE)
2058 if (par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2059 (par->codec_id == AV_CODEC_ID_DVB_TELETEXT ||
2060 par->codec_id == AV_CODEC_ID_TEXT))
2063 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Looking for decoder for %1")
2064 .arg(avcodec_get_name(par->codec_id)));
2066 if (par->codec_id == AV_CODEC_ID_PROBE)
2068 LOG(VB_GENERAL, LOG_ERR,
LOC +
2069 QString(
"Probing of stream #%1 unsuccesful, ignoring.").arg(strm));
2076 const AVCodec *codec =
nullptr;
2081 LOG(VB_GENERAL, LOG_ERR,
LOC +
2082 QString(
"Could not find decoder for codec (%1), ignoring.")
2083 .arg(avcodec_get_name(par->codec_id)));
2090 void *opaque =
nullptr;
2091 const AVCodec *
p = av_codec_iterate(&opaque);
2096 if (
p->name[0] !=
'\0')
2097 msg = QString(
"Codec %1:").arg(
p->name);
2099 msg = QString(
"Codec %1, null name,").arg(strm);
2101 LOG(VB_LIBAV, LOG_INFO,
LOC + msg);
2103 if (
p->id == par->codec_id)
2109 LOG(VB_LIBAV, LOG_INFO,
LOC +
2110 QString(
"Codec 0x%1 != 0x%2") .arg(
p->id, 0, 16)
2111 .arg(par->codec_id, 0, 16));
2112 p = av_codec_iterate(&opaque);
2123 if (enc->codec && par->codec_id != enc->codec_id)
2125 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2126 QString(
"Already opened codec not matching (%1 vs %2). Reopening")
2127 .arg(avcodec_get_name(enc->codec_id),
2128 avcodec_get_name(enc->codec->id)));
2144 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2145 QString(
"Stream 0x%1 is not valid in this context - skipping")
2146 .arg(
m_ic->streams[strm]->id, 4, 16));
2150 if (par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2152 bool forced = (
m_ic->streams[strm]->disposition & AV_DISPOSITION_FORCED) != 0;
2154 uint lang_indx = lang_sub_cnt[lang]++;
2155 subtitleStreamCount++;
2158 static_cast<int>(strm), lang, lang_indx,
m_ic->streams[strm]->id, 0, 0,
false,
false, forced);
2160 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2161 QString(
"Subtitle track #%1 is A/V stream #%2 "
2162 "and is in the %3 language(%4).")
2167 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
2171 int channels = par->ch_layout.nb_channels;
2172 uint lang_indx = lang_aud_cnt[lang]++;
2175 if (enc->avcodec_dual_language)
2178 static_cast<int>(strm), lang, lang_indx,
m_ic->streams[strm]->id, channels,
2179 false,
false,
false,
type);
2180 lang_indx = lang_aud_cnt[lang]++;
2182 static_cast<int>(strm), lang, lang_indx,
m_ic->streams[strm]->id, channels,
2183 true,
false,
false,
type);
2187 int logical_stream_id = 0;
2194 logical_stream_id =
m_ic->streams[strm]->id;
2196 if (logical_stream_id == -1)
2203 static_cast<int>(strm), lang, lang_indx, logical_stream_id, channels,
2204 false,
false,
false,
type);
2207 LOG(VB_AUDIO, LOG_INFO,
LOC +
2208 QString(
"Audio Track #%1, of type (%2) is A/V stream #%3 (id=0x%4) "
2209 "and has %5 channels in the %6 language(%7).")
2211 .arg(strm).arg(
m_ic->streams[strm]->id,0,16).arg(enc->ch_layout.nb_channels)
2222 const AVCodec *codec =
nullptr;
2223 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Trying to select best video track");
2237 int selTrack = av_find_best_stream(
m_ic, AVMEDIA_TYPE_VIDEO,
2242 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"No video track found/selected.");
2246 AVStream *stream =
m_ic->streams[selTrack];
2256 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2257 codectype += QString(
"(%1x%2)").arg(enc->width).arg(enc->height);
2258 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2259 QString(
"Selected track #%1: ID: 0x%2 Codec ID: %3 Profile: %4 Type: %5 Bitrate: %6")
2260 .arg(selTrack).arg(
static_cast<uint64_t
>(stream->id), 0, 16)
2261 .arg(avcodec_get_name(enc->codec_id),
2262 avcodec_profile_name(enc->codec_id, enc->profile),
2264 QString::number(enc->bit_rate)));
2271 if ((enc->width != stream->codecpar->width) || (enc->height != stream->codecpar->height))
2273 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
2274 "Video resolution mismatch: Context: %1x%2 Stream: %3x%4 Codec: %5 Stream change: %6")
2275 .arg(enc->width).arg(enc->height)
2276 .arg(stream->codecpar->width).arg(stream->codecpar->height)
2283 int width = std::max(dim.width(), 16);
2284 int height = std::max(dim.height(), 16);
2285 QString dec =
"ffmpeg";
2286 uint thread_count = 1;
2289 codecName = enc->codec->name;
2294 if (enc->framerate.den && enc->framerate.num)
2295 m_fps = float(enc->framerate.num) / float(enc->framerate.den);
2299 bool foundgpudecoder =
false;
2300 QStringList unavailabledecoders;
2310 LOG(VB_GENERAL, LOG_WARNING,
LOC + QString(
2311 "GPU/hardware decoder '%1' failed - forcing software decode")
2318 while (unavailabledecoders.size() < 10)
2322 if (!unavailabledecoders.isEmpty())
2324 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Unavailable decoders: %1")
2325 .arg(unavailabledecoders.join(
",")));
2331 if (!skip_loop_filter)
2332 enc->skip_loop_filter = AVDISCARD_NONKEY;
2340 if (
version && allowgpu && dec !=
"ffmpeg")
2344 enc->opaque =
static_cast<void*
>(
this);
2349 enc->opaque =
static_cast<void*
>(
this);
2351 foundgpudecoder =
true;
2356 unavailabledecoders.append(dec);
2364 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Unknown video codec - defaulting to MPEG2");
2389 if (!foundgpudecoder)
2391 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Using %1 CPUs for decoding")
2392 .arg(HAVE_THREADS ? thread_count : 1));
2393 enc->thread_count =
static_cast<int>(thread_count);
2427 QString(
m_ic->iformat->name).contains(
"matroska"));
2465 #ifdef USING_MEDIACODEC
2466 if (QString(
"mediacodec") == codec->wrapper_name)
2467 av_jni_set_java_vm(QAndroidJniEnvironment::javaVM(),
nullptr);
2469 int ret = avcodec_open2(avctx, codec,
nullptr);
2474 LOG(VB_GENERAL, LOG_ERR,
LOC +
2475 QString(
"Could not open codec 0x%1, id(%2) type(%3) "
2476 "ignoring. reason %4").arg((uint64_t)avctx,0,16)
2477 .arg(avcodec_get_name(avctx->codec_id),
2483 LOG(VB_GENERAL, LOG_INFO,
LOC +
2484 QString(
"Opened codec 0x%1, id(%2) type(%3)")
2485 .arg((uint64_t)avctx,0,16)
2486 .arg(avcodec_get_name(avctx->codec_id),
2511 if (si.m_language_index == Index)
2512 return si.m_language;
2519 AVDictionaryEntry *metatag = av_dict_get(
m_ic->streams[StreamIndex]->metadata,
"language",
nullptr, 0);
2562 AVStream *stream =
m_ic->streams[StreamIndex];
2566 if (stream->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
2568 else if (stream->disposition & AV_DISPOSITION_COMMENT)
2570 else if (stream->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
2572 else if (stream->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
2599 if (
current->m_av_stream_index == streamIndex)
2605 LOG(VB_GENERAL, LOG_WARNING,
LOC +
2606 QString(
"Invalid stream index passed to "
2607 "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
2614 if (
current->m_av_substream_index == -1)
2627 (next->m_av_stream_index != streamIndex))
2629 QString msg = QString(
2630 "Expected substream 1 (Language I) of stream %1\n\t\t\t"
2631 "following substream 0, found end of list or another stream.")
2634 LOG(VB_GENERAL, LOG_WARNING,
LOC + msg);
2656 for (
uint i = 0; i <
m_ic->nb_streams;)
2658 AVStream *st =
m_ic->streams[i];
2660 if (avctx && avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2663 av_remove_stream(
m_ic, st->id, 0);
2676 if (!std::any_of(decoder->m_renderFormats->cbegin(), decoder->m_renderFormats->cend(),
2677 [&
type](
auto Format) { return type == Format; }))
2679 decoder->m_directRendering =
false;
2680 return avcodec_default_get_buffer2(c, pic, flags);
2683 decoder->m_directRendering =
true;
2684 MythVideoFrame *frame = decoder->GetPlayer()->GetNextVideoFrame();
2694 if ((frame->
m_type !=
type) || (pic->width > width) || (pic->height > height))
2711 for (
uint i = 0; i < 3; i++)
2717 pic->opaque = frame;
2718 pic->reordered_opaque = c->reordered_opaque;
2721 AVBufferRef *buffer = av_buffer_create(
reinterpret_cast<uint8_t*
>(frame), 0,
2722 [](
void* Opaque, uint8_t* Data)
2726 if (avfd && avfd->GetPlayer())
2727 avfd->GetPlayer()->DeLimboFrame(vf);
2730 pic->buf[0] = buffer;
2741 for (
int i = 0; i < 4; i++)
2743 pic->data[i] =
nullptr;
2744 pic->linesize[i] = 0;
2746 pic->opaque = frame;
2748 pic->reordered_opaque = c->reordered_opaque;
2749 pic->data[0] = (uint8_t*)frame->
m_buffer;
2750 pic->data[3] = (uint8_t*)frame->
m_buffer;
2753 AVBufferRef *buffer =
2754 av_buffer_create((uint8_t*)frame, 0,
2755 [](
void* Opaque, uint8_t* Data)
2763 pic->buf[0] = buffer;
2778 bool process_cc_data = (buf[0] & 0x40) != 0;
2779 if (!process_cc_data)
2785 uint cc_count = buf[0] & 0x1f;
2788 if (buf_size < 2+(3*cc_count))
2799 bool had_608 =
false;
2800 bool had_708 =
false;
2801 for (
uint cur = 0; cur + 2 < buf_size; cur += 3)
2803 uint cc_code = buf[cur];
2804 bool cc_valid = (cc_code & 0x04) != 0U;
2806 uint data1 = buf[cur+1];
2807 uint data2 = buf[cur+2];
2808 uint data = (data2 << 8) | data1;
2809 uint cc_type = cc_code & 0x03;
2818 if (scte || cc_type <= 0
x1)
2837 if (scte && field == 0 &&
2838 (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
2864 bool check_608,
bool check_708)
2866 bool need_change_608 =
false;
2871 for (
uint i = 0; i < 4; i++)
2878 bool need_change_708 =
false;
2880 if (check_708 || need_change_608)
2883 for (
uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
2888 if (need_change_708 && !check_608)
2892 if (!need_change_608 && !need_change_708)
2903 for (
uint i = 1; i < 64; i++)
2907 StreamInfo si(av_index, lang, 0, i, 0,
false,
true);
2912 for (
uint i = 0; i < 4; i++)
2923 StreamInfo si(av_index, lang, 0, i+1, 0,
false,
false);
2933 AVPacket *pkt,
bool can_reliably_parse_keyframes)
2938 bool reset_kfd =
false;
2942 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2943 "gopset not set, syncing positionMap");
2945 if (tempKeyFrameDist > 0 && !
m_livetv)
2947 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2948 QString(
"Initial key frame distance: %1.")
2954 else if (
m_keyframeDist != tempKeyFrameDist && tempKeyFrameDist > 0)
2956 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
2957 QString(
"Key frame distance changed from %1 to %2.")
2985 if (can_reliably_parse_keyframes &&
2988 long long last_frame = 0;
2996 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
2997 QString(
"framesRead: %1 last_frame: %2 keyframedist: %3")
3004 long long startpos = pkt->pos;
3006 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO,
LOC +
3007 QString(
"positionMap[ %1 ] == %2.")
3037 float bytespersec = (float)
m_bitrate / 8;
3055 const uint8_t *bufptr = pkt->data;
3056 const uint8_t *bufend = pkt->data + pkt->size;
3058 while (bufptr < bufend)
3062 float aspect_override = -1.0F;
3071 if (bufptr + 11 >= pkt->data + pkt->size)
3073 const auto *seq =
reinterpret_cast<const SequenceHeader*
>(bufptr);
3075 int width =
static_cast<int>(seq->width()) >> context->lowres;
3076 int height =
static_cast<int>(seq->height()) >> context->lowres;
3077 float aspect = seq->
aspect(context->codec_id == AV_CODEC_ID_MPEG1VIDEO);
3078 if (stream->sample_aspect_ratio.num)
3079 aspect =
static_cast<float>(av_q2d(stream->sample_aspect_ratio) * width / height);
3080 if (aspect_override > 0.0F)
3081 aspect = aspect_override;
3082 float seqFPS = seq->fps();
3086 changed |= (seqFPS >
static_cast<float>(
m_fps)+0.01F) ||
3087 (seqFPS < static_cast<float>(
m_fps)-0.01F);
3091 bool forceaspectchange = !qFuzzyCompare(
m_currentAspect + 10.0F, aspect + 10.0F) &&
3095 if (changed || forceaspectchange)
3100 bool doublerate =
false;
3103 forceaspectchange, 2,
3106 if (context->hw_frames_ctx)
3107 if (context->internal)
3108 avcodec_flush_buffers(context);
3127 if ((seqFPS > avFPS+0.01F) || (seqFPS < avFPS-0.01F))
3129 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"avFPS(%1) != seqFPS(%2)")
3130 .arg(
static_cast<double>(avFPS)).arg(
static_cast<double>(seqFPS)));
3139 pkt->flags |= AV_PKT_FLAG_KEY;
3146 pkt->flags |= AV_PKT_FLAG_KEY;
3155 const uint8_t *buf = pkt->data;
3156 const uint8_t *buf_end = pkt->data + pkt->size;
3161 if (context->extradata && (context->extradata_size >= 7) && (context->extradata[0] == 0x01))
3163 if (pkt->flags & AV_PKT_FLAG_KEY)
3168 while (buf < buf_end)
3199 bool fps_changed = (seqFPS > 0.0) && ((seqFPS >
static_cast<double>(
m_fps) + 0.01) ||
3200 (seqFPS < static_cast<double>(
m_fps) - 0.01));
3205 if (fps_changed || res_changed || forcechange)
3210 bool doublerate =
false;
3223 if (context->hw_frames_ctx && (forcechange || res_changed))
3224 if (context->internal)
3225 avcodec_flush_buffers(context);
3231 m_fps =
static_cast<float>(seqFPS);
3246 if ((seqFPS > avFPS + 0.01) || (seqFPS < avFPS - 0.01))
3248 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
3249 QString(
"avFPS(%1) != seqFPS(%2)").arg(avFPS).arg(seqFPS));
3254 pkt->flags |= AV_PKT_FLAG_KEY;
3275 if (pkt->flags & AV_PKT_FLAG_KEY)
3291 !(pkt->flags & AV_PKT_FLAG_KEY))
3293 av_packet_unref(pkt);
3305 pkt_dur = av_mul_q(pkt_dur, curstream->time_base);
3306 if (pkt_dur.num == 1501 && pkt_dur.den == 90000)
3327 mpa_pic->reordered_opaque = AV_NOPTS_VALUE;
3329 if (pkt->pts != AV_NOPTS_VALUE)
3332 bool sentPacket =
false;
3337 context->reordered_opaque = pkt->pts;
3354 if (ret == AVERROR(EAGAIN))
3358 if (ret==0 && !gotpicture)
3360 ret2 = avcodec_send_packet(context, pkt);
3361 if (ret2 == AVERROR(EAGAIN))
3373 if (ret < 0 || ret2 < 0)
3378 LOG(VB_GENERAL, LOG_ERR,
LOC +
3379 QString(
"video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
3381 .arg(ret).arg(gotpicture));
3386 LOG(VB_GENERAL, LOG_ERR,
LOC +
3387 QString(
"video avcodec_send_packet error: %1 (%2) gotpicture:%3")
3389 .arg(ret2).arg(gotpicture));
3403 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Decoder needs reset");
3407 if (ret == AVERROR_EXTERNAL || ret2 == AVERROR_EXTERNAL)
3409 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"FFmpeg external library error - assuming streams changed");
3421 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO,
LOC +
3422 QString(
"video timecodes packet-pts:%1 frame-pts:%2 packet-dts: %3 frame-dts:%4")
3423 .arg(pkt->pts).arg(mpa_pic->pts).arg(pkt->pts)
3424 .arg(mpa_pic->pkt_dts));
3431 if (pkt->dts != AV_NOPTS_VALUE)
3437 if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3453 if (pkt->dts != AV_NOPTS_VALUE)
3459 if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3460 pts = mpa_pic->reordered_opaque;
3463 else if (pkt->dts != AV_NOPTS_VALUE)
3469 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG,
LOC +
3470 QString(
"video packet timestamps reordered %1 pts %2 dts %3 (%4)")
3471 .arg(mpa_pic->reordered_opaque).arg(pkt->pts).arg(pkt->dts)
3475 mpa_pic->reordered_opaque =
pts;
3484 auto *newPkt = av_packet_clone(pkt);
3504 uint cc_len =
static_cast<uint>(std::max(AvFrame->scte_cc_len,0));
3505 uint8_t *cc_buf = AvFrame->scte_cc_buf;
3515 cc_len =
static_cast<uint>(std::max(AvFrame->atsc_cc_len, 0));
3516 cc_buf = AvFrame->atsc_cc_buf;
3524 for (
uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
3530 auto * side_data = av_frame_get_side_data(AvFrame, AV_FRAME_DATA_A53_CC);
3531 if (side_data && (side_data->size > 0))
3532 DecodeCCx08(side_data->data,
static_cast<uint>(side_data->size),
false);
3545 frame->m_directRendering =
false;
3556 av_image_fill_arrays(tmppicture.data, tmppicture.linesize,
3557 frame->m_buffer, AV_PIX_FMT_YUV420P, AvFrame->width,
3558 AvFrame->height, IMAGE_ALIGN);
3559 tmppicture.data[0] = frame->m_buffer + frame->m_offsets[0];
3560 tmppicture.data[1] = frame->m_buffer + frame->m_offsets[1];
3561 tmppicture.data[2] = frame->m_buffer + frame->m_offsets[2];
3562 tmppicture.linesize[0] = frame->m_pitches[0];
3563 tmppicture.linesize[1] = frame->m_pitches[1];
3564 tmppicture.linesize[2] = frame->m_pitches[2];
3568 AvFrame->height,
static_cast<AVPixelFormat
>(AvFrame->format),
3569 AvFrame->width, AvFrame->height,
3570 AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
3571 nullptr,
nullptr,
nullptr);
3574 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to allocate sws context");
3577 sws_scale(
m_swsCtx, AvFrame->data, AvFrame->linesize, 0, dim.height(),
3578 tmppicture.data, tmppicture.linesize);
3613 LOG(VB_GENERAL, LOG_ERR,
LOC +
"NULL videoframe - direct rendering not "
3614 "correctly initialized.");
3618 std::chrono::milliseconds
pts = 0ms;
3621 long long av_pts = AvFrame->pts;
3622 if (av_pts == AV_NOPTS_VALUE)
3623 av_pts = AvFrame->pkt_dts;
3624 if (av_pts == AV_NOPTS_VALUE)
3625 av_pts = AvFrame->reordered_opaque;
3626 if (av_pts == AV_NOPTS_VALUE)
3628 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No PTS found - unable to process video.");
3636 std::chrono::milliseconds temppts =
pts;
3653 double calcfps = 1000.0 /
ptsdiff.count();
3654 if (calcfps < 121.0 && calcfps > 3.0)
3658 double fpschange = calcfps /
static_cast<double>(
m_fps);
3660 if (fpschange > 1.9 && fpschange < 2.1)
3662 if (fpschange > 0.9 && fpschange < 1.1)
3668 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO,
LOC +
3669 QString(
"video timecode %1 %2 %3 %4%5")
3671 .arg(
pts.count()).arg(temppts.count()).arg(
m_lastVPts.count())
3672 .arg((
pts != temppts) ?
" fixup" :
""));
3674 frame->m_interlaced = AvFrame->interlaced_frame;
3675 frame->m_topFieldFirst = AvFrame->top_field_first != 0;
3677 frame->m_repeatPic = AvFrame->repeat_pict != 0;
3682 frame->m_colorspace = AvFrame->colorspace;
3683 frame->m_colorrange = AvFrame->color_range;
3684 frame->m_colorprimaries = AvFrame->color_primaries;
3685 frame->m_colortransfer = AvFrame->color_trc;
3686 frame->m_chromalocation = AvFrame->chroma_location;
3687 frame->m_pixFmt = AvFrame->format;
3692 frame->m_dummy =
false;
3693 frame->m_pauseFrame =
false;
3694 frame->m_deinterlaceInuse2x =
false;
3695 frame->m_alreadyDeinterlaced =
false;
3696 frame->m_interlacedReverse =
false;
3726 [[maybe_unused]]
const AVStream *stream,
const AVPacket *pkt)
3728 const uint8_t *buf = pkt->data;
3729 uint64_t linemask = 0;
3734 if ((buf[0]==
't') && (buf[1]==
'v') && (buf[2] ==
'0'))
3737 memcpy(&linemask, buf + 3, 8);
3740 else if ((buf[0]==
'T') && (buf[1]==
'V') && (buf[2] ==
'0'))
3742 linemask = 0xffffffffffffffffLL;
3747 LOG(VB_VBI, LOG_ERR,
LOC + QString(
"Unknown VBI data stream '%1%2%3'")
3748 .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
3752 static constexpr
uint kMinBlank = 6;
3753 for (
uint i = 0; i < 36; i++)
3755 if (!((linemask >> i) & 0
x1))
3758 const uint line = ((i < 18) ? i : i-18) + kMinBlank;
3759 const uint field = (i<18) ? 0 : 1;
3760 const uint id2 = *buf & 0xf;
3770 StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
3783 int data = (buf[2] << 8) | buf[1];
3810 const AVStream* ,
const AVPacket *pkt)
3819 const uint8_t *buf = pkt->data;
3820 const uint8_t *buf_end = pkt->data + pkt->size;
3822 if (*buf >= 0x10 && *buf <= 0x1F)
3828 LOG(VB_VBI, LOG_WARNING,
LOC +
3829 QString(
"VBI: Unknown data_identier: %1 discarded").arg(*buf));
3834 while (buf < buf_end)
3839 if ((buf_end - buf) >= 42)
3843 else if (*buf == 0x03)
3846 if ((buf_end - buf) >= 42)
3850 else if (*buf == 0xff)
3856 LOG(VB_VBI, LOG_WARNING,
LOC +
3857 QString(
"VBI: Unsupported data_unit_id: %1 discarded").arg(*buf));
3867 [[maybe_unused]]
const AVPacket *pkt)
3874 uint8_t *data = pkt->data;
3875 int length = pkt->size;
3876 int componentTag = 0;
3877 int dataBroadcastId = 0;
3878 unsigned carouselId = 0;
3881 componentTag = str->component_tag;
3882 dataBroadcastId = str->data_id;
3883 carouselId = (unsigned) str->carousel_id;
3888 uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
3890 if (sectionLen > length)
3894 componentTag, carouselId,
3896 length -= sectionLen;
3899 #endif // USING_MHEG
3907 long long pts = pkt->pts;
3908 if (
pts == AV_NOPTS_VALUE)
3910 if (
pts == AV_NOPTS_VALUE)
3912 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No PTS found - unable to process subtitle.");
3915 pts =
static_cast<long long>(av_q2d(curstream->time_base) *
pts * 1000);
3921 bool isForcedTrack =
false;
3924 int gotSubtitles = 0;
3925 AVSubtitle subtitle;
3926 memset(&subtitle, 0,
sizeof(AVSubtitle));
3933 curstream->id,
pts);
3937 if (pkt->stream_index == subIdx)
3941 pkt->data, pkt->size,
pts);
3947 || pkt->stream_index == forcedSubIdx)
3951 avcodec_decode_subtitle2(ctx, &subtitle, &gotSubtitles, pkt);
3954 subtitle.start_display_time +=
pts;
3955 subtitle.end_display_time +=
pts;
3957 if (pkt->stream_index != subIdx)
3958 isForcedTrack =
true;
3965 for (
unsigned i = 0; i < subtitle.num_rects; i++)
3967 subtitle.rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
3970 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO,
LOC +
3971 QString(
"subtl timecode %1 %2 %3 %4")
3972 .arg(pkt->pts).arg(pkt->dts)
3973 .arg(subtitle.start_display_time)
3974 .arg(subtitle.end_display_time));
3977 subtitle, curstream->codecpar->codec_id == AV_CODEC_ID_XSUB,
3991 auto id =
static_cast<uint>(Packet->stream_index + 0x2000);
3995 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
3996 const auto * codec = QTextCodec::codecForName(
"utf-8");
3997 auto text = codec->toUnicode(
reinterpret_cast<const char *
>(Packet->data), Packet->size - 1);
3999 auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
4000 QString text = toUtf16.decode(Packet->data);
4002 auto list = text.split(
'\n', Qt::SkipEmptyParts);
4010 enum AVCodecID codec_id = curstream->codecpar->codec_id;
4014 case AV_CODEC_ID_MPEG2VBI:
4017 case AV_CODEC_ID_DVB_VBI:
4020 case AV_CODEC_ID_DSMCC_B:
4029 #endif // USING_MHEG:
4045 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Audio stream type " + msg +
"changed.");
4059 QString forcedString = forced ? QObject::tr(
" (forced)") :
"";
4061 int av_index =
m_tracks[
type][TrackNo].m_av_stream_index;
4062 AVStream *stream {
nullptr };
4063 if (av_index >= 0 && av_index < (
int)
m_ic->nb_streams)
4064 stream =
m_ic->streams[av_index];
4065 AVDictionaryEntry *entry =
4066 stream ? av_dict_get(stream->metadata,
"title",
nullptr, 0) :
nullptr;
4067 QString user_title = entry ? QString(R
"( "%1")").arg(entry->value) : "";
4079 AVCodecParameters *par = stream->codecpar;
4081 if (par->codec_id == AV_CODEC_ID_MP3)
4082 msg += QString(
" MP3");
4083 else if (ctx && ctx->codec)
4084 msg += QString(
" %1").arg(ctx->codec->name).toUpper();
4085 if (!user_title.isEmpty())
4093 msg += QString(
" ?ch");
4094 else if((channels > 4) && !(channels & 1))
4095 msg += QString(
" %1.1ch").arg(channels - 1);
4097 msg += QString(
" %1ch").arg(channels);
4107 if (!user_title.isEmpty())
4110 msg += QString(
" (%1)")
4114 return QString(
"%1: %2").arg(TrackNo + 1).arg(msg);
4118 return QObject::tr(
"Subtitle") + QString(
" %1: %2%3%4")
4119 .arg(QString::number(TrackNo + 1),
4147 return ctx ? QByteArray(
reinterpret_cast<char*
>(ctx->subtitle_header), ctx->subtitle_header_size) :
4158 AVDictionaryEntry *tag = av_dict_get(
m_ic->streams[index]->metadata,
"filename",
nullptr, 0);
4160 Filename = QByteArray(tag->value);
4161 AVCodecParameters *par =
m_ic->streams[index]->codecpar;
4162 Data = QByteArray(
reinterpret_cast<char*
>(par->extradata), par->extradata_size);
4172 if ((stream->component_tag == Tag) || ((Tag <= 0) && stream->component_tag <= 0))
4181 for (
uint i = 0; i <
m_ic->nb_streams; i++)
4183 AVStream *stream =
m_ic->streams[i];
4186 if (stream->component_tag == Tag)
4188 StreamInfo si(
static_cast<int>(i), 0, 0, 0, 0);
4210 const std::vector<int> &ftype)
4212 std::vector<int> ret;
4214 for (
int index : ftype)
4216 if ((lang_key < 0) || tracks[index].m_language == lang_key)
4217 ret.push_back(index);
4225 std::vector<int> ret;
4227 for (
size_t i = 0; i < tracks.size(); i++)
4229 if (tracks[i].m_audio_type ==
type)
4238 const std::vector<int>&fs,
4239 enum AVCodecID codecId,
4242 int selectedTrack = -1;
4247 const int stream_index = tracks[f].m_av_stream_index;
4248 AVCodecParameters *par = ic->streams[stream_index]->codecpar;
4249 if ((codecId == AV_CODEC_ID_NONE || codecId == par->codec_id) &&
4250 (max_seen < par->ch_layout.nb_channels))
4252 if (codecId == AV_CODEC_ID_DTS &&
profile > 0)
4259 max_seen = par->ch_layout.nb_channels;
4263 return selectedTrack;
4321 uint numStreams = atracks.size();
4322 if ((ctrack >= 0) && (ctrack < (
int)numStreams))
4327 for (
const auto & track : atracks)
4329 int idx = track.m_av_stream_index;
4330 AVCodecContext *codec_ctx =
m_ic->streams[idx]->codec;
4331 AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
4332 codec_ctx->sample_rate, codec_ctx->channels,
4338 int selTrack = (1 == numStreams) ? 0 : -1;
4343 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Trying to reselect audio sub-stream");
4349 for (
uint i = 0; i < numStreams; i++)
4351 if (atracks[i].m_av_substream_index == substream_index)
4359 if ((selTrack < 0) && wlang >= -1 && numStreams)
4361 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Trying to reselect audio track");
4366 for (
uint i = 0; i < numStreams; i++)
4368 if (wlang == atracks[i].m_language)
4372 if (windx == atracks[i].m_language_index)
4378 if (selTrack < 0 && numStreams)
4380 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Trying to select audio track (w/lang)");
4387 LOG(VB_AUDIO, LOG_WARNING,
"No audio tracks matched the type filter, "
4388 "so trying all tracks.");
4389 for (
int i = 0; i < static_cast<int>(atracks.size()); i++)
4398 std::vector<int> flang =
filter_lang(atracks, canonical_key, ftype);
4402 FF_PROFILE_DTS_HD_MA);
4406 if (selTrack < 0 && m_audio->CanDTSHD())
4408 FF_PROFILE_DTS_HD_HRA);
4432 FF_PROFILE_DTS_HD_MA);
4435 AV_CODEC_ID_TRUEHD);
4437 if (selTrack < 0 && m_audio->CanDTSHD())
4439 FF_PROFILE_DTS_HD_HRA);
4460 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Trying to select default track");
4461 for (
size_t i = 0; i < atracks.size(); i++) {
4462 int idx = atracks[i].m_av_stream_index;
4463 if (
m_ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
4474 LOG(VB_AUDIO, LOG_INFO,
LOC +
4475 "Trying to select audio track (wo/lang)");
4480 FF_PROFILE_DTS_HD_MA);
4484 if (selTrack < 0 && m_audio->CanDTSHD())
4486 FF_PROFILE_DTS_HD_HRA);
4505 if (ctrack != selTrack)
4507 LOG(VB_AUDIO, LOG_INFO,
LOC +
"No suitable audio track exists.");
4514 strack = atracks[selTrack];
4519 LOG(VB_AUDIO, LOG_INFO,
LOC + QString(
"Selected track %1 (A/V Stream #%2)")
4528 char *buffer,
int bufsize)
4539 const uint halfsample = samplesize >> 1;
4541 const char *from = (channel == 1) ? buffer + halfsample : buffer;
4542 char *to = (channel == 0) ? buffer + halfsample : buffer;
4545 (sample++), (from += samplesize), (to += samplesize))
4547 memmove(to, from, halfsample);
4557 bool firstloop =
true;
4558 int decoded_size = -1;
4565 AVPacket *tmp_pkt = av_packet_alloc();
4566 tmp_pkt->data = pkt->data;
4567 tmp_pkt->size = pkt->size;
4568 while (tmp_pkt->size > 0)
4570 bool reselectAudioTrack =
false;
4575 LOG(VB_AUDIO, LOG_INFO,
LOC +
4576 "Audio is disabled - trying to restart it");
4577 reselectAudioTrack =
true;
4582 bool wasDual = audSubIdx != -1;
4583 bool isDual = ctx->avcodec_dual_language != 0;
4584 if ((wasDual && !isDual) || (!wasDual && isDual))
4587 reselectAudioTrack =
true;
4592 bool already_decoded =
false;
4593 if (!ctx->ch_layout.nb_channels)
4605 AVChannelLayout channel_layout;
4607 av_opt_set_chlayout(ctx->priv_data,
"downmix", &channel_layout, 0);
4609 if (ctx->codec_id == AV_CODEC_ID_AC3)
4614 decoded_size = data_size;
4615 already_decoded =
true;
4616 reselectAudioTrack |= ctx->ch_layout.nb_channels;
4620 if (reselectAudioTrack)
4630 if (!(decodetype &
kDecodeAudio) || (pkt->stream_index != audIdx)
4634 if (firstloop && pkt->pts != AV_NOPTS_VALUE)
4652 LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO,
LOC +
4653 QString(
"discarding early audio timecode %1 %2 %3")
4654 .arg(pkt->pts).arg(pkt->dts).arg(
m_lastAPts.count()));
4668 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Audio stream changed");
4671 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Number of audio channels changed from %1 to %2")
4683 if (!already_decoded)
4688 decoded_size = data_size;
4696 data_size = tmp_pkt->size;
4702 if (!already_decoded)
4706 AVChannelLayout channel_layout;
4708 av_opt_set_chlayout(ctx->priv_data,
"downmix", &channel_layout, 0);
4712 decoded_size = data_size;
4719 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Unknown audio decoding error");
4720 av_packet_free(&tmp_pkt);
4726 tmp_pkt->data += ret;
4727 tmp_pkt->size -= ret;
4731 std::chrono::milliseconds temppts =
m_lastAPts;
4738 int frames = (ctx->ch_layout.nb_channels <= 0 || decoded_size < 0 || !samplesize) ? -1 :
4739 decoded_size / (ctx->ch_layout.nb_channels * samplesize);
4748 ((
double)(frames * 1000) / ctx->sample_rate);
4751 LOG(VB_TIMESTAMP, LOG_INFO,
LOC + QString(
"audio timecode %1 %2 %3 %4")
4752 .arg(pkt->pts).arg(pkt->dts).arg(temppts.count()).arg(
m_lastAPts.count()));
4757 tmp_pkt->data += ret;
4758 tmp_pkt->size -= ret;
4762 av_packet_free(&tmp_pkt);
4769 AVPacket *pkt =
nullptr;
4770 bool have_err =
false;
4772 const DecodeType origDecodetype = decodetype;
4780 bool storevideoframes =
false;
4849 storevideoframes =
true;
4855 LOG(VB_GENERAL, LOG_WARNING,
LOC +
4856 QString(
"Audio %1 ms behind video but already %2 "
4857 "video frames queued. AV-Sync might be broken.")
4868 av_packet_free(&pkt);
4874 pkt = av_packet_alloc();
4879 if (retval == -EAGAIN)
4883 av_packet_free(&pkt);
4884 std::string errbuf(256,
'\0');
4887 errmsg = QString::fromStdString(errbuf);
4891 LOG(VB_GENERAL, LOG_ERR, QString(
"decoding error %1 (%2)")
4892 .arg(errmsg).arg(retval));
4905 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No context");
4906 av_packet_unref(pkt);
4910 if (pkt->stream_index >= (
int)
m_ic->nb_streams)
4912 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Bad stream");
4913 av_packet_unref(pkt);
4917 AVStream *curstream =
m_ic->streams[pkt->stream_index];
4921 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Bad stream (NULL)");
4922 av_packet_unref(pkt);
4926 enum AVMediaType codec_type = curstream->codecpar->codec_type;
4928 if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
4936 if (codec_type == AVMEDIA_TYPE_VIDEO &&
4946 av_packet_free(&pkt);
4951 if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4952 curstream->codecpar->codec_id == AV_CODEC_ID_TEXT)
4955 av_packet_unref(pkt);
4959 if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4960 curstream->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT)
4963 av_packet_unref(pkt);
4967 if (codec_type == AVMEDIA_TYPE_DATA)
4970 av_packet_unref(pkt);
4977 if (codec_type != AVMEDIA_TYPE_VIDEO)
4979 LOG(VB_PLAYBACK, LOG_ERR,
LOC +
4980 QString(
"No codec for stream index %1, type(%2) id(%3:%4)")
4981 .arg(pkt->stream_index)
4983 avcodec_get_name(curstream->codecpar->codec_id))
4984 .arg(curstream->codecpar->codec_id));
4989 av_packet_unref(pkt);
4997 case AVMEDIA_TYPE_AUDIO:
5006 case AVMEDIA_TYPE_VIDEO:
5013 if (pkt->pts != AV_NOPTS_VALUE)
5016 (av_q2d(curstream->time_base)*pkt->pts*1000000);
5031 case AVMEDIA_TYPE_SUBTITLE:
5040 LOG(VB_GENERAL, LOG_ERR,
LOC +
5041 QString(
"Decoding - id(%1) type(%2)")
5042 .arg(avcodec_get_name(ctx->codec_id),
5049 if (!have_err && !Retry)
5051 av_packet_unref(pkt);
5056 av_packet_free(&pkt);
5064 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"StreamChangeCheck skip SeekReset"));
5074 int result = av_read_frame(ctx, pkt);
5081 if (ic && ic->pmt_section)
5084 if (!pmt_buffer.has_buffer())
5143 if (stream < 0 || !
m_ic)
5145 return avcodec_get_name(
m_ic->streams[stream]->codecpar->codec_id);
5159 QString msg = (disable) ?
"Disabling" :
"Allowing";
5160 LOG(VB_AUDIO, LOG_INFO,
LOC + msg +
" pass through");
5183 switch (ctx->codec_id)
5185 case AV_CODEC_ID_AC3:
5186 case AV_CODEC_ID_TRUEHD:
5187 case AV_CODEC_ID_EAC3:
5188 case AV_CODEC_ID_MLP:
5189 case AV_CODEC_ID_DTS:
5198 bool passthru =
false;
5202 if (!withProfile && par->codec_id == AV_CODEC_ID_DTS && !
m_audio->
CanDTSHD())
5205 par->codec_id, FF_PROFILE_DTS);
5210 par->codec_id, par->profile);
5226 AVStream *curstream =
nullptr;
5227 AVCodecContext *ctx =
nullptr;
5229 int requested_channels = 0;
5233 (
int)
m_ic->nb_streams) &&
5235 .m_av_stream_index]) &&
5240 ctx->bits_per_raw_sample);
5242 if (av_sample_fmt_is_planar(ctx->sample_fmt))
5244 LOG(VB_AUDIO, LOG_INFO,
LOC + QString(
"Audio data is planar"));
5249 int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
5250 if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
5251 ctx->bits_per_raw_sample)
5252 bps = ctx->bits_per_raw_sample;
5253 LOG(VB_GENERAL, LOG_ERR,
LOC +
5254 QString(
"Unsupported sample format with %1 bits").arg(bps));
5258 bool using_passthru =
DoPassThrough(curstream->codecpar,
false);
5260 requested_channels = ctx->ch_layout.nb_channels;
5262 if (!using_passthru &&
5268 AVChannelLayout channel_layout;
5269 av_channel_layout_default(&channel_layout, requested_channels);
5270 av_opt_set_chlayout(ctx->priv_data,
"downmix", &channel_layout, 0);
5273 info =
AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
5274 requested_channels, using_passthru, ctx->ch_layout.nb_channels,
5275 ctx->codec_id == AV_CODEC_ID_DTS ? ctx->profile : 0);
5281 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"No codec context. Returning false");
5288 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Initializing audio parms from " +
5293 LOG(VB_AUDIO, LOG_INFO,
LOC +
"Audio format changed " +
5294 QString(
"\n\t\t\tfrom %1 to %2")
5310 switch (ctx->codec_id)
5312 case AV_CODEC_ID_MP2:
5315 case AV_CODEC_ID_MP3:
5318 case AV_CODEC_ID_AC3:
5321 case AV_CODEC_ID_DTS:
5324 case AV_CODEC_ID_VORBIS:
5327 case AV_CODEC_ID_WMAV1:
5330 case AV_CODEC_ID_WMAV2:
5338 lcd->setAudioFormatLEDs(audio_format,
true);
5373 int64_t start_time = INT64_MAX;
5374 int64_t end_time = INT64_MIN;
5375 AVStream *st =
nullptr;
5377 for (
uint i = 0; i < ic->nb_streams; i++)
5379 AVStream *st1 = ic->streams[i];
5380 if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
5389 int64_t duration = INT64_MIN;
5390 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
5391 int64_t start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
5392 if (start_time1 < start_time)
5393 start_time = start_time1;
5394 if (st->duration != AV_NOPTS_VALUE) {
5395 int64_t end_time1 = start_time1 +
5396 av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5397 if (end_time1 > end_time)
5398 end_time = end_time1;
5401 if (st->duration != AV_NOPTS_VALUE) {
5402 int64_t duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5403 if (duration1 > duration)
5404 duration = duration1;
5406 if (start_time != INT64_MAX) {
5407 ic->start_time = start_time;
5408 if (end_time != INT64_MIN) {
5409 if (end_time - start_time > duration)
5410 duration = end_time - start_time;
5413 if (duration != INT64_MIN) {
5414 int64_t filesize = 0;
5415 ic->duration = duration;
5416 if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
5418 ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
5419 (
double)ic->duration;