MythTV  master
avformatdecoder.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <algorithm>
3 #include <cassert>
4 #include <cmath>
5 #include <cstdint>
6 #include <iostream>
7 #include <unistd.h>
8 using namespace std;
9 
10 #include <QTextCodec>
11 #include <QFileInfo>
12 
13 // MythTV headers
14 #include "mythtvexp.h"
15 #include "mythconfig.h"
16 #include "avformatdecoder.h"
17 #include "privatedecoder.h"
18 #include "audiooutput.h"
19 #include "audiooutpututil.h"
20 #include "ringbuffer.h"
21 #include "mythplayer.h"
22 #include "remoteencoder.h"
23 #include "programinfo.h"
24 #include "mythcorecontext.h"
25 #include "mythdbcon.h"
26 #include "iso639.h"
27 #include "mpegtables.h"
28 #include "atscdescriptors.h"
29 #include "dvbdescriptors.h"
30 #include "cc608decoder.h"
31 #include "cc708decoder.h"
32 #include "teletextdecoder.h"
33 #include "subtitlereader.h"
34 #include "interactivetv.h"
35 #include "videodisplayprofile.h"
36 #include "mythuihelper.h"
37 #include "DVD/dvdringbuffer.h"
38 #include "Bluray/bdringbuffer.h"
39 #include "mythavutil.h"
40 
41 #include "lcddevice.h"
42 
43 #include "audiooutput.h"
44 
45 #ifdef USING_MEDIACODEC
46 extern "C" {
47 #include "libavcodec/jni.h"
48 }
49 #include <QtAndroidExtras>
50 #endif
51 
52 extern "C" {
53 #include "libavutil/avutil.h"
54 #include "libavutil/error.h"
55 #include "libavutil/log.h"
56 #include "libavcodec/avcodec.h"
57 #include "libavformat/avformat.h"
58 #include "libavformat/avio.h"
59 #include "libavformat/internal.h"
60 #include "libswscale/swscale.h"
61 #include "libavformat/isom.h"
62 #include "ivtv_myth.h"
63 #include "libavutil/imgutils.h"
64 #include "libavutil/display.h"
65 }
66 
67 #ifdef _MSC_VER
68 // MSVC isn't C99 compliant...
69 # ifdef AV_TIME_BASE_Q
70 # undef AV_TIME_BASE_Q
71 # endif
72 #define AV_TIME_BASE_Q GetAVTimeBaseQ()
73 
74 __inline AVRational GetAVTimeBaseQ()
75 {
76  AVRational av = {1, AV_TIME_BASE};
77  return av;
78 }
79 #endif
80 
81 #define LOC QString("AFD: ")
82 
83 // Maximum number of sequential invalid data packet errors before we try
84 // switching to software decoder. Packet errors are often seen when using
85 // hardware contexts and, for example, seeking. Hence this needs to be high and
86 // is probably best removed as it is treating the symptoms and not the cause
87 // (e.g. VAAPI2 does not currently do any hardware support checks).
88 // See also comment in MythCodecMap::freeCodecContext re trying to free an
89 // active hardware context when it is errored.
90 #define SEQ_PKT_ERR_MAX 50
91 
92 static const int max_video_queue_size = 220;
93 
94 static int cc608_parity(uint8_t byte);
95 static int cc608_good_parity(const int *parity_table, uint16_t data);
96 static void cc608_build_parity_table(int *parity_table);
97 
98 static bool silence_ffmpeg_logging = false;
99 
100 static QSize get_video_dim(const AVCodecContext &ctx)
101 {
102  return {ctx.width >> ctx.lowres, ctx.height >> ctx.lowres};
103 }
104 static float get_aspect(const AVCodecContext &ctx)
105 {
106  float aspect_ratio = 0.0F;
107 
108  if (ctx.sample_aspect_ratio.num && ctx.height)
109  {
110  aspect_ratio = av_q2d(ctx.sample_aspect_ratio) *
111  static_cast<double>(ctx.width);
112  aspect_ratio /= (float) ctx.height;
113  }
114 
115  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
116  {
117  if (ctx.height)
118  aspect_ratio = (float)ctx.width / (float)ctx.height;
119  else
120  aspect_ratio = 4.0F / 3.0F;
121  }
122 
123  return aspect_ratio;
124 }
125 static float get_aspect(H264Parser &p)
126 {
127  static constexpr float kDefaultAspect = 4.0F / 3.0F;
128  int asp = p.aspectRatio();
129  switch (asp)
130  {
131  case 0: return kDefaultAspect;
132  case 2: return 4.0F / 3.0F;
133  case 3: return 16.0F / 9.0F;
134  case 4: return 2.21F;
135  default: break;
136  }
137 
138  float aspect_ratio = asp * 0.000001F;
139  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
140  {
141  if (p.pictureHeight() && p.pictureWidth())
142  {
143  aspect_ratio =
144  (float) p.pictureWidth() /(float) p.pictureHeight();
145  }
146  else
147  {
148  aspect_ratio = kDefaultAspect;
149  }
150  }
151  return aspect_ratio;
152 }
153 
154 
155 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags);
156 #ifdef USING_DXVA2
157 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int flags);
158 #endif
159 
160 // currently unused
161 //static int determinable_frame_size(struct AVCodecContext *avctx)
162 //{
163 // if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
164 // avctx->codec_id == AV_CODEC_ID_MP1 ||
165 // avctx->codec_id == AV_CODEC_ID_MP2 ||
166 // avctx->codec_id == AV_CODEC_ID_MP3/* ||
167 // avctx->codec_id == AV_CODEC_ID_CELT*/)
168 // return 1;
169 // return 0;
170 //}
171 
172 #define FAIL(errmsg) do { \
173  LOG(VB_PLAYBACK, LOG_INFO, LOC + (errmsg)); \
174  return false; \
175 } while (false)
176 
177 static bool StreamHasRequiredParameters(AVStream *Stream)
178 {
179  AVCodecContext *avctx = nullptr;
180  switch (Stream->codecpar->codec_type)
181  {
182  // We fail on video first as this is generally the most serious error
183  // and if we have video, we usually have everything else
184  case AVMEDIA_TYPE_VIDEO:
185  avctx = gCodecMap->getCodecContext(Stream);
186  if (!avctx)
187  FAIL("No codec for video stream");
188  if (!Stream->codecpar->width || !Stream->codecpar->height)
189  FAIL("Unspecified video size");
190  if (Stream->codecpar->format == AV_PIX_FMT_NONE)
191  FAIL("Unspecified video pixel format");
192  if (avctx->codec_id == AV_CODEC_ID_RV30 || avctx->codec_id == AV_CODEC_ID_RV40)
193  if (!Stream->sample_aspect_ratio.num && !avctx->sample_aspect_ratio.num && !Stream->codec_info_nb_frames)
194  FAIL("No frame in rv30/40 and no sar");
195  break;
196  case AVMEDIA_TYPE_AUDIO:
197  avctx = gCodecMap->getCodecContext(Stream);
198  if (!avctx)
199  FAIL("No codec for audio stream");
200 
201  // These checks are currently disabled as they continually fail but
202  // codec initialisation is fine - which just delays live tv startup.
203  // The worst offender appears to be audio description channel...
204 
205  //if (!Stream->codecpar->frame_size && determinable_frame_size(avctx))
206  // FAIL("Unspecified audio frame size");
207  //if (Stream->codecpar->format == AV_SAMPLE_FMT_NONE)
208  // FAIL("Unspecified audio sample format");
209  //if (!Stream->codecpar->sample_rate)
210  // FAIL("Unspecified audio sample rate");
211  //if (!Stream->codecpar->channels)
212  // FAIL("Unspecified number of audio channels");
213  if (!Stream->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
214  FAIL("No decodable DTS frames");
215  break;
216 
217  case AVMEDIA_TYPE_SUBTITLE:
218  if (Stream->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !Stream->codecpar->width)
219  FAIL("Unspecified subtitle size");
220  break;
221  case AVMEDIA_TYPE_DATA:
222  if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
223  return true;
224  break;
225  default:
226  break;
227  }
228 
229  if (Stream->codecpar->codec_id == AV_CODEC_ID_NONE)
230  FAIL("Unknown codec");
231  return true;
232 }
233 
234 static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
235 {
237  return;
238 
239  if (VERBOSE_LEVEL_NONE)
240  return;
241 
242  static QString s_fullLine("");
243  static constexpr int kMsgLen = 255;
244  static QMutex s_stringLock;
245  uint64_t verbose_mask = VB_LIBAV;
246  LogLevel_t verbose_level = LOG_EMERG;
247 
248  // determine mythtv debug level from av log level
249  switch (level)
250  {
251  case AV_LOG_PANIC:
252  verbose_level = LOG_EMERG;
253  verbose_mask |= VB_GENERAL;
254  break;
255  case AV_LOG_FATAL:
256  verbose_level = LOG_CRIT;
257  verbose_mask |= VB_GENERAL;
258  break;
259  case AV_LOG_ERROR:
260  verbose_level = LOG_ERR;
261  break;
262  case AV_LOG_WARNING:
263  verbose_level = LOG_WARNING;
264  break;
265  case AV_LOG_INFO:
266  verbose_level = LOG_INFO;
267  break;
268  case AV_LOG_VERBOSE:
269  case AV_LOG_DEBUG:
270  case AV_LOG_TRACE:
271  verbose_level = LOG_DEBUG;
272  break;
273  default:
274  return;
275  }
276 
277  if (!VERBOSE_LEVEL_CHECK(verbose_mask, verbose_level))
278  return;
279 
280  s_stringLock.lock();
281  if (s_fullLine.isEmpty() && ptr) {
282  AVClass* avc = *(AVClass**)ptr;
283  s_fullLine = QString("[%1 @ %2] ")
284  .arg(avc->item_name(ptr))
285  .arg((quintptr)avc, QT_POINTER_SIZE * 2, 16, QChar('0'));
286  }
287 
288  char str[kMsgLen+1];
289  int bytes = vsnprintf(str, kMsgLen+1, fmt, vl);
290 
291  // check for truncated messages and fix them
292  if (bytes > kMsgLen)
293  {
294  LOG(VB_GENERAL, LOG_WARNING,
295  QString("Libav log output truncated %1 of %2 bytes written")
296  .arg(kMsgLen).arg(bytes));
297  str[kMsgLen-1] = '\n';
298  }
299 
300  s_fullLine += QString(str);
301  if (s_fullLine.endsWith("\n"))
302  {
303  LOG(verbose_mask, verbose_level, s_fullLine.trimmed());
304  s_fullLine.truncate(0);
305  }
306  s_stringLock.unlock();
307 }
308 
309 static int get_canonical_lang(const char *lang_cstr)
310 {
311  if (lang_cstr[0] == '\0' || lang_cstr[1] == '\0')
312  {
313  return iso639_str3_to_key("und");
314  }
315  if (lang_cstr[2] == '\0')
316  {
317  QString tmp2 = lang_cstr;
318  QString tmp3 = iso639_str2_to_str3(tmp2);
319  int lang = iso639_str3_to_key(tmp3);
320  return iso639_key_to_canonical_key(lang);
321  }
322  int lang = iso639_str3_to_key(lang_cstr);
323  return iso639_key_to_canonical_key(lang);
324 }
325 
327 {
328  opts.decoders->append("ffmpeg");
329  (*opts.equiv_decoders)["ffmpeg"].append("nuppel");
330  (*opts.equiv_decoders)["ffmpeg"].append("dummy");
331 
334 }
335 
337  const ProgramInfo &pginfo,
338  PlayerFlags flags)
339  : DecoderBase(parent, pginfo),
340  m_isDbIgnored(gCoreContext->IsDatabaseIgnored()),
341  m_h264Parser(new H264Parser()),
342  m_playerFlags(flags),
343  // Closed Caption & Teletext decoders
344  m_ccd608(new CC608Decoder(parent->GetCC608Reader())),
345  m_ccd708(new CC708Decoder(parent->GetCC708Reader())),
346  m_ttd(new TeletextDecoder(parent->GetTeletextReader()))
347 {
348  m_audioSamples = (uint8_t *)av_mallocz(AudioOutput::kMaxSizeBuffer);
350 
351  av_log_set_callback(myth_av_log);
352 
353  m_audioIn.m_sampleSize = -32;// force SetupAudioStream to run once
355 
357 
359  m_audioReadAhead = gCoreContext->GetNumSetting("AudioReadAhead", 100);
360 
361  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("PlayerFlags: 0x%1, AudioReadAhead: %2 msec")
362  .arg(m_playerFlags, 0, 16).arg(m_audioReadAhead));
363 }
364 
366 {
367  while (!m_storedPackets.isEmpty())
368  {
369  AVPacket *pkt = m_storedPackets.takeFirst();
370  av_packet_unref(pkt);
371  delete pkt;
372  }
373 
374  CloseContext();
375  delete m_ccd608;
376  delete m_ccd708;
377  delete m_ttd;
378  delete m_privateDec;
379  delete m_h264Parser;
380  delete m_mythCodecCtx;
381 
382  sws_freeContext(m_swsCtx);
383 
384  av_freep(&m_audioSamples);
385 
386  delete m_avfRingBuffer;
387 
388  if (LCD *lcd = LCD::Get())
389  {
390  lcd->setAudioFormatLEDs(AUDIO_AC3, false);
391  lcd->setVideoFormatLEDs(VIDEO_MPG, false);
392  lcd->setVariousLEDs(VARIOUS_HDTV, false);
393  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
394  lcd->setSpeakerLEDs(SPEAKER_71, false); // should clear any and all speaker LEDs
395  }
396 }
397 
399 {
400  if (m_ic)
401  {
402  for (uint i = 0; i < m_ic->nb_streams; i++)
403  {
404  QMutexLocker locker(avcodeclock);
405  AVStream *st = m_ic->streams[i];
407  }
408  }
409 }
410 
412 {
413  if (m_ic)
414  {
415  CloseCodecs();
416 
417  AVInputFormat *fmt = m_ic->iformat;
418  m_ic->iformat->flags |= AVFMT_NOFILE;
419 
420  av_free(m_ic->pb->buffer);
421  av_free(m_ic->pb);
422  avformat_close_input(&m_ic);
423  m_ic = nullptr;
424  fmt->flags &= ~AVFMT_NOFILE;
425  }
426 
427  delete m_privateDec;
428  m_privateDec = nullptr;
429  m_h264Parser->Reset();
430 }
431 
432 static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
433 {
434  int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
435  return ((lsb - base_ts)&mask);
436 }
437 
439 {
440  int64_t start_pts = 0;
441 
442  AVStream *st = nullptr;
443  for (uint i = 0; i < m_ic->nb_streams; i++)
444  {
445  AVStream *st1 = m_ic->streams[i];
446  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
447  {
448  st = st1;
449  break;
450  }
451  }
452  if (!st)
453  return 0;
454 
455  if (m_ic->start_time != AV_NOPTS_VALUE)
456  {
457  start_pts = av_rescale(m_ic->start_time,
458  st->time_base.den,
459  AV_TIME_BASE * (int64_t)st->time_base.num);
460  }
461 
462  int64_t pts = av_rescale(timecode / 1000.0,
463  st->time_base.den,
464  st->time_base.num);
465 
466  // adjust for start time and wrap
467  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
468 
469  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
470 }
471 
473  int64_t timecode)
474 {
475  int64_t start_pts = 0;
476 
477  if (m_ic->start_time != AV_NOPTS_VALUE)
478  {
479  start_pts = av_rescale(m_ic->start_time,
480  st->time_base.den,
481  AV_TIME_BASE * (int64_t)st->time_base.num);
482  }
483 
484  int64_t pts = av_rescale(timecode / 1000.0,
485  st->time_base.den,
486  st->time_base.num);
487 
488  // adjust for start time and wrap
489  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
490 
491  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
492 }
493 
495 {
496  if (m_ic && m_ic->nb_chapters > 1)
497  return m_ic->nb_chapters;
498  return 0;
499 }
500 
501 void AvFormatDecoder::GetChapterTimes(QList<long long> &times)
502 {
503  int total = GetNumChapters();
504  if (!total)
505  return;
506 
507  for (int i = 0; i < total; i++)
508  {
509  int num = m_ic->chapters[i]->time_base.num;
510  int den = m_ic->chapters[i]->time_base.den;
511  int64_t start = m_ic->chapters[i]->start;
512  long double total_secs = (long double)start * (long double)num /
513  (long double)den;
514  times.push_back((long long)total_secs);
515  }
516 }
517 
518 int AvFormatDecoder::GetCurrentChapter(long long framesPlayed)
519 {
520  if (!GetNumChapters())
521  return 0;
522 
523  for (int i = (m_ic->nb_chapters - 1); i > -1 ; i--)
524  {
525  int num = m_ic->chapters[i]->time_base.num;
526  int den = m_ic->chapters[i]->time_base.den;
527  int64_t start = m_ic->chapters[i]->start;
528  long double total_secs = (long double)start * (long double)num /
529  (long double)den;
530  auto framenum = (long long)(total_secs * m_fps);
531  if (framesPlayed >= framenum)
532  {
533  LOG(VB_PLAYBACK, LOG_INFO, LOC +
534  QString("GetCurrentChapter(selected chapter %1 framenum %2)")
535  .arg(i + 1).arg(framenum));
536  return i + 1;
537  }
538  }
539  return 0;
540 }
541 
542 long long AvFormatDecoder::GetChapter(int chapter)
543 {
544  if (chapter < 1 || chapter > GetNumChapters())
545  return -1;
546 
547  int num = m_ic->chapters[chapter - 1]->time_base.num;
548  int den = m_ic->chapters[chapter - 1]->time_base.den;
549  int64_t start = m_ic->chapters[chapter - 1]->start;
550  long double total_secs = (long double)start * (long double)num /
551  (long double)den;
552  auto framenum = (long long)(total_secs * m_fps);
553  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("GetChapter %1: framenum %2")
554  .arg(chapter).arg(framenum));
555  return framenum;
556 }
557 
558 bool AvFormatDecoder::DoRewind(long long desiredFrame, bool discardFrames)
559 {
560  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("DoRewind(%1, %2 discard frames)")
561  .arg(desiredFrame).arg( discardFrames ? "do" : "don't" ));
562 
564  return DecoderBase::DoRewind(desiredFrame, discardFrames);
565 
566  m_doRewind = true;
567 
568  // avformat-based seeking
569  return DoFastForward(desiredFrame, discardFrames);
570 }
571 
572 bool AvFormatDecoder::DoFastForward(long long desiredFrame, bool discardFrames)
573 {
574  LOG(VB_PLAYBACK, LOG_INFO, LOC +
575  QString("DoFastForward(%1 (%2), %3 discard frames)")
576  .arg(desiredFrame).arg(m_framesPlayed)
577  .arg((discardFrames) ? "do" : "don't"));
578 
580  return DecoderBase::DoFastForward(desiredFrame, discardFrames);
581 
582  bool oldrawstate = m_getRawFrames;
583  m_getRawFrames = false;
584 
585  AVStream *st = nullptr;
586  for (uint i = 0; i < m_ic->nb_streams; i++)
587  {
588  AVStream *st1 = m_ic->streams[i];
589  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
590  {
591  st = st1;
592  break;
593  }
594  }
595 
596  int seekDelta = desiredFrame - m_framesPlayed;
597 
598  // avoid using av_frame_seek if we are seeking frame-by-frame when paused
599  if (seekDelta >= 0 && seekDelta < 2 && !m_doRewind && m_parent->GetPlaySpeed() == 0.0F)
600  {
601  SeekReset(m_framesPlayed, seekDelta, false, true);
603  m_getRawFrames = oldrawstate;
604  return true;
605  }
606 
607  long long ts = 0;
608  if (m_ic->start_time != AV_NOPTS_VALUE)
609  ts = m_ic->start_time;
610 
611  // convert framenumber to normalized timestamp
612  long double seekts = desiredFrame * AV_TIME_BASE / m_fps;
613  ts += (long long)seekts;
614 
615  // XXX figure out how to do snapping in this case
616  bool exactseeks = DecoderBase::GetSeekSnap() == 0U;
617 
618  int flags = (m_doRewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
619 
620  if (av_seek_frame(m_ic, -1, ts, flags) < 0)
621  {
622  LOG(VB_GENERAL, LOG_ERR, LOC +
623  QString("av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
624  m_getRawFrames = oldrawstate;
625  return false;
626  }
627 
628  int normalframes = 0;
629 
630  if (st && st->cur_dts != AV_NOPTS_VALUE)
631  {
632 
633  int64_t adj_cur_dts = st->cur_dts;
634 
635  if (m_ic->start_time != AV_NOPTS_VALUE)
636  {
637  int64_t st1 = av_rescale(m_ic->start_time,
638  st->time_base.den,
639  AV_TIME_BASE * (int64_t)st->time_base.num);
640  adj_cur_dts = lsb3full(adj_cur_dts, st1, st->pts_wrap_bits);
641  }
642 
643  int64_t adj_seek_dts = av_rescale(seekts,
644  st->time_base.den,
645  AV_TIME_BASE * (int64_t)st->time_base.num);
646 
647  int64_t max_dts = (st->pts_wrap_bits < 64) ? (1LL<<st->pts_wrap_bits)-1 : -1LL;
648 
649  // When seeking near the start of a stream the current dts is sometimes
650  // less than the start time which causes lsb3full to return adj_cur_dts
651  // close to the maximum dts value. If so, set adj_cur_dts to zero.
652  if (adj_seek_dts < max_dts / 64 && adj_cur_dts > max_dts / 2)
653  adj_cur_dts = 0;
654 
655  long long newts = av_rescale(adj_cur_dts,
656  (int64_t)AV_TIME_BASE *
657  (int64_t)st->time_base.num,
658  st->time_base.den);
659 
660  m_lastKey = (long long)((newts*(long double)m_fps)/AV_TIME_BASE);
662  m_fpsSkip = 0;
664 
665  normalframes = (exactseeks) ? desiredFrame - m_framesPlayed : 0;
666  normalframes = max(normalframes, 0);
667  m_noDtsHack = false;
668  }
669  else
670  {
671  LOG(VB_GENERAL, LOG_INFO, LOC + "No DTS Seeking Hack!");
672  m_noDtsHack = true;
673  m_framesPlayed = desiredFrame;
674  m_fpsSkip = 0;
675  m_framesRead = desiredFrame;
676  normalframes = 0;
677  }
678 
679  SeekReset(m_lastKey, normalframes, true, discardFrames);
680 
681  if (discardFrames)
683 
684  m_doRewind = false;
685 
686  m_getRawFrames = oldrawstate;
687 
688  return true;
689 }
690 
691 void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
692  bool doflush, bool discardFrames)
693 {
694  if (!m_ringBuffer)
695  return; // nothing to reset...
696 
697  LOG(VB_PLAYBACK, LOG_INFO, LOC +
698  QString("SeekReset(%1, %2, %3 flush, %4 discard)")
699  .arg(newKey).arg(skipFrames)
700  .arg((doflush) ? "do" : "don't")
701  .arg((discardFrames) ? "do" : "don't"));
702 
703  DecoderBase::SeekReset(newKey, skipFrames, doflush, discardFrames);
704 
705  QMutexLocker locker(avcodeclock);
706 
707  // Discard all the queued up decoded frames
708  if (discardFrames)
709  {
710  bool releaseall = m_mythCodecCtx ? (m_mythCodecCtx->DecoderWillResetOnFlush() ||
711  m_mythCodecCtx->DecoderNeedsReset(nullptr)) : false;
712  m_parent->DiscardVideoFrames(doflush, doflush && releaseall);
713  }
714 
715  if (doflush)
716  {
717  m_lastAPts = 0;
718  m_lastVPts = 0;
719  m_lastCcPtsu = 0;
720  m_faultyPts = m_faultyDts = 0;
723  m_ptsDetected = false;
724  m_reorderedPtsDetected = false;
725 
726  ff_read_frame_flush(m_ic);
727 
728  // Only reset the internal state if we're using our seeking,
729  // not when using libavformat's seeking
731  {
732  m_ic->pb->pos = m_ringBuffer->GetReadPosition();
733  m_ic->pb->buf_ptr = m_ic->pb->buffer;
734  m_ic->pb->buf_end = m_ic->pb->buffer;
735  m_ic->pb->eof_reached = 0;
736  }
737 
738  // Flush the avcodec buffers
739  LOG(VB_PLAYBACK, LOG_INFO, LOC + "SeekReset() flushing");
740  for (uint i = 0; i < m_ic->nb_streams; i++)
741  {
742  AVCodecContext *enc = gCodecMap->hasCodecContext(m_ic->streams[i]);
743  // note that contexts that have not been opened have
744  // enc->internal = nullptr and cause a segfault in
745  // avcodec_flush_buffers
746  if (enc && enc->internal)
747  avcodec_flush_buffers(enc);
748  }
749  if (m_privateDec)
750  m_privateDec->Reset();
751 
752  // Free up the stored up packets
753  while (!m_storedPackets.isEmpty())
754  {
755  AVPacket *pkt = m_storedPackets.takeFirst();
756  av_packet_unref(pkt);
757  delete pkt;
758  }
759 
760  m_prevGopPos = 0;
761  m_gopSet = false;
762  if (!m_ringBuffer->IsDVD())
763  {
764  if (!m_noDtsHack)
765  {
767  m_fpsSkip = 0;
769  }
770 
771  m_noDtsHack = false;
772  }
773  }
774 
775  // Skip all the desired number of skipFrames
776 
777  // Some seeks can be very slow. The most common example comes
778  // from HD-PVR recordings, where keyframes are 128 frames apart
779  // and decoding (even hardware decoding) may not be much faster
780  // than realtime, causing some exact seeks to take 2-4 seconds.
781  // If exact seeking is not required, we take some shortcuts.
782  // First, we impose an absolute maximum time we are willing to
783  // spend (maxSeekTimeMs) on the forward frame-by-frame skip.
784  // After that much time has elapsed, we give up and stop the
785  // frame-by-frame seeking. Second, after skipping a few frames,
786  // we predict whether the situation is hopeless, i.e. the total
787  // skipping would take longer than giveUpPredictionMs, and if so,
788  // stop skipping right away.
789  bool exactSeeks = GetSeekSnap() == 0U;
790  const int maxSeekTimeMs = 200;
791  int profileFrames = 0;
793  for (; (skipFrames > 0 && !m_atEof &&
794  (exactSeeks || begin.elapsed() < maxSeekTimeMs));
795  --skipFrames, ++profileFrames)
796  {
797  // TODO this won't work well in conjunction with the MythTimer
798  // above...
799  QElapsedTimer getframetimer;
800  getframetimer.start();
801  bool retry = true;
802  while (retry && !getframetimer.hasExpired(100))
803  {
804  retry = false;
805  GetFrame(kDecodeVideo, retry);
806  if (retry)
807  usleep(1000);
808  }
809 
811  {
813  m_decodedVideoFrame = nullptr;
814  }
815  if (!exactSeeks && profileFrames >= 5 && profileFrames < 10)
816  {
817  const int giveUpPredictionMs = 400;
818  int remainingTimeMs =
819  skipFrames * (float)begin.elapsed() / profileFrames;
820  if (remainingTimeMs > giveUpPredictionMs)
821  {
822  LOG(VB_PLAYBACK, LOG_DEBUG,
823  QString("Frame-by-frame seeking would take "
824  "%1 ms to finish, skipping.").arg(remainingTimeMs));
825  break;
826  }
827  }
828  }
829 
830  if (doflush)
831  {
832  m_firstVPts = 0;
833  m_firstVPtsInuse = true;
834  }
835 }
836 
838 {
839  if (!eof && m_ic && m_ic->pb)
840  {
841  LOG(VB_GENERAL, LOG_NOTICE, LOC +
842  QString("Resetting byte context eof (livetv %1 was eof %2)")
843  .arg(m_livetv).arg(m_ic->pb->eof_reached));
844  m_ic->pb->eof_reached = 0;
845  }
846  DecoderBase::SetEof(eof);
847 }
848 
849 void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset,
850  bool reset_file)
851 {
852  LOG(VB_PLAYBACK, LOG_INFO, LOC +
853  QString("Reset: Video %1, Seek %2, File %3")
854  .arg(reset_video_data).arg(seek_reset).arg(reset_file));
855 
856  if (seek_reset)
857  SeekReset(0, 0, true, false);
858 
859  DecoderBase::Reset(reset_video_data, false, reset_file);
860 
861  if (reset_video_data)
862  {
863  m_seenGop = false;
864  m_seqCount = 0;
865  }
866 }
867 
869  const QString &filename, int testbufsize)
870 {
871  AVProbeData probe;
872  memset(&probe, 0, sizeof(AVProbeData));
873 
874  QByteArray fname = filename.toLatin1();
875  probe.filename = fname.constData();
876  probe.buf = (unsigned char *)testbuf;
877  probe.buf_size = testbufsize;
878 
879  int score = AVPROBE_SCORE_MAX/4;
880 
881  if (testbufsize + AVPROBE_PADDING_SIZE > kDecoderProbeBufferSize)
882  {
883  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
884  score = 0;
885  }
886  else if (testbufsize*2 >= kDecoderProbeBufferSize)
887  {
888  score--;
889  }
890 
891  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
892 
893  return av_probe_input_format2(&probe, static_cast<int>(true), &score) != nullptr;
894 }
895 
897 {
898  int buf_size = m_ringBuffer->BestBufferSize();
899  int streamed = m_ringBuffer->IsStreamed();
901  m_readContext.flags = AVIO_FLAG_READ;
902  m_readContext.is_streamed = streamed;
903  m_readContext.max_packet_size = 0;
904  m_readContext.priv_data = m_avfRingBuffer;
905  auto *buffer = (unsigned char *)av_malloc(buf_size);
906  m_ic->pb = avio_alloc_context(buffer, buf_size, 0,
907  &m_readContext,
911 
912  // We can always seek during LiveTV
913  m_ic->pb->seekable = !streamed || forceseek;
914  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Buffer size: %1 Streamed %2 Seekable %3 Available %4")
915  .arg(buf_size).arg(streamed).arg(m_ic->pb->seekable).arg(m_ringBuffer->GetReadBufAvail()));
916 }
917 
918 extern "C" void HandleStreamChange(void *data)
919 {
920  auto *decoder = reinterpret_cast<AvFormatDecoder*>(data);
921 
922  int cnt = decoder->m_ic->nb_streams;
923 
924  LOG(VB_PLAYBACK, LOG_INFO, LOC +
925  QString("streams_changed 0x%1 -- stream count %2")
926  .arg((uint64_t)data,0,16).arg(cnt));
927 
928  decoder->m_streamsChanged = true;
929 }
930 
932 {
933  QMutexLocker lock(avcodeclock);
934  int retval = avformat_find_stream_info(m_ic, nullptr);
935  silence_ffmpeg_logging = false;
936  // ffmpeg 3.0 is returning -1 code when there is a channel
937  // change or some encoding error just after the start
938  // of the file, but is has found the correct stream info
939  // Set rc to 0 so that playing can continue.
940  if (retval == -1)
941  retval = 0;
942  return retval;
943 }
944 
959 int AvFormatDecoder::OpenFile(RingBuffer *rbuffer, bool novideo,
960  char testbuf[kDecoderProbeBufferSize],
961  int testbufsize)
962 {
963  CloseContext();
964 
965  m_ringBuffer = rbuffer;
966 
967  // Process frames immediately unless we're decoding
968  // a DVD, in which case don't so that we don't show
969  // anything whilst probing the data streams.
971 
972  delete m_avfRingBuffer;
973  m_avfRingBuffer = new AVFRingBuffer(rbuffer);
974 
975  AVInputFormat *fmt = nullptr;
976  QString fnames = m_ringBuffer->GetFilename();
977  QByteArray fnamea = fnames.toLatin1();
978  const char *filename = fnamea.constData();
979 
980  AVProbeData probe;
981  memset(&probe, 0, sizeof(AVProbeData));
982  probe.filename = filename;
983  probe.buf = reinterpret_cast<unsigned char *>(testbuf);
984  if (testbufsize + AVPROBE_PADDING_SIZE <= kDecoderProbeBufferSize)
985  probe.buf_size = testbufsize;
986  else
987  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
988  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
989 
990  fmt = av_probe_input_format(&probe, static_cast<int>(true));
991  if (!fmt)
992  {
993  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Probe failed for '%1'").arg(filename));
994  return -1;
995  }
996 
997  if (strcmp(fmt->name, "mpegts") == 0 &&
998  gCoreContext->GetBoolSetting("FFMPEGTS", false))
999  {
1000  AVInputFormat *fmt2 = av_find_input_format("mpegts-ffmpeg");
1001  if (fmt2)
1002  {
1003  fmt = fmt2;
1004  LOG(VB_GENERAL, LOG_INFO, LOC + "Using FFmpeg MPEG-TS demuxer (forced)");
1005  }
1006  }
1007 
1008  int err = 0;
1009  bool scancomplete = false;
1010  int remainingscans = 5;
1011 
1012  while (!scancomplete && remainingscans--)
1013  {
1014  bool found = false;
1015 
1016  // With live tv, the ringbufer may contain insufficient data for complete
1017  // initialisation so we try a few times with a slight pause each time to
1018  // allow extra data to become available. In the worst case scenarios, the
1019  // stream may not have a keyframe for 4-5 seconds.
1020  // As a last resort, we will try and fallback to the original FFmpeg MPEG-TS
1021  // demuxer if it is not already used.
1022  // For regular videos, this shouldn't be an issue as the complete file
1023  // should be available - though we try a little harder for streamed formats
1024  int retries = m_livetv || m_ringBuffer->IsStreamed() ? 50 : 10;
1025 
1026  while (!found && --retries)
1027  {
1028  m_ic = avformat_alloc_context();
1029  if (!m_ic)
1030  {
1031  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
1032  return -1;
1033  }
1034 
1037 
1038  err = avformat_open_input(&m_ic, filename, fmt, nullptr);
1039  if (err < 0)
1040  {
1041  char error[AV_ERROR_MAX_STRING_SIZE];
1042  av_make_error_string(error, sizeof(error), err);
1043  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Failed to open input ('%1')").arg(error));
1044 
1045  // note - m_ic (AVFormatContext) is freed on failure
1046  if (retries > 1)
1047  {
1048  // wait a little to buffer more data
1049  // 50*0.1 = 5 seconds max
1050  QThread::usleep(100000);
1051  // resets the read position
1052  m_avfRingBuffer->SetInInit(false);
1053  continue;
1054  }
1055 
1056  if (strcmp(fmt->name, "mpegts") == 0)
1057  {
1058  fmt = av_find_input_format("mpegts-ffmpeg");
1059  if (fmt)
1060  {
1061  LOG(VB_GENERAL, LOG_ERR, LOC + "Attempting to use original FFmpeg MPEG-TS demuxer.");
1062  // resets the read position
1063  m_avfRingBuffer->SetInInit(false);
1064  continue;
1065  }
1066  break;
1067  }
1068  }
1069  found = true;
1070  }
1071 
1072  if (err < 0)
1073  {
1074  LOG(VB_GENERAL, LOG_ERR, LOC + "Fatal error opening input. Aborting");
1075  m_ic = nullptr;
1076  return -1;
1077  }
1078 
1079  // With certain streams, we don't get a complete stream analysis and the video
1080  // codec/frame format is not fully detected. This can have various consequences - from
1081  // failed playback to not enabling hardware decoding (as the frame formt is not valid).
1082  // Bump the duration (FFmpeg defaults to 5 seconds) to 60 seconds. This should
1083  // not impact performance as in the vast majority of cases the scan is completed
1084  // within a second or two (seconds in this case referring to stream duration - not the time
1085  // it takes to complete the scan).
1086  m_ic->max_analyze_duration = 60 * AV_TIME_BASE;
1087 
1089  err = FindStreamInfo();
1090  if (err < 0)
1091  {
1092  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find codec parameters for '%1'").arg(filename));
1093  CloseContext();
1094  return -1;
1095  }
1096  m_avfRingBuffer->SetInInit(false);
1097 
1098  // final sanity check that scanned streams are valid for live tv
1099  scancomplete = true;
1100  for (uint i = 0; m_livetv && (i < m_ic->nb_streams); i++)
1101  {
1102  if (!StreamHasRequiredParameters(m_ic->streams[i]))
1103  {
1104  scancomplete = false;
1105  if (remainingscans)
1106  {
1107  CloseContext();
1108  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Stream scan incomplete - retrying");
1109  QThread::usleep(250000); // wait 250ms
1110  }
1111  break;
1112  }
1113  }
1114  }
1115 
1116  if (!scancomplete)
1117  LOG(VB_GENERAL, LOG_WARNING, LOC + "Scan incomplete - playback may not work");
1118 
1119  m_ic->streams_changed = HandleStreamChange;
1120  m_ic->stream_change_data = this;
1121 
1122  fmt->flags &= ~AVFMT_NOFILE;
1123 
1124  if (!m_livetv && !m_ringBuffer->IsDisc())
1125  {
1126  // generate timings based on the video stream to avoid bogus ffmpeg
1127  // values for duration and bitrate
1129  }
1130 
1131  // FLAC, MP3 or M4A file may contains an artwork image, a single frame MJPEG,
1132  // we need to ignore it as we don't handle single frames or images in place of video
1133  // TODO: display single frame
1134  QString extension = QFileInfo(fnames).suffix();
1135  if (strcmp(fmt->name, "mp3") == 0 || strcmp(fmt->name, "flac") == 0 ||
1136  strcmp(fmt->name, "ogg") == 0 ||
1137  (extension.compare("m4a", Qt::CaseInsensitive) == 0))
1138  {
1139  novideo = true;
1140  }
1141 
1142  // Scan for the initial A/V streams
1143  err = ScanStreams(novideo);
1144  if (-1 == err)
1145  {
1146  CloseContext();
1147  return err;
1148  }
1149 
1150  AutoSelectTracks(); // This is needed for transcoder
1151 
1152 #ifdef USING_MHEG
1153  {
1154  int initialAudio = -1;
1155  int initialVideo = -1;
1156  if (m_itv || (m_itv = m_parent->GetInteractiveTV()))
1157  m_itv->GetInitialStreams(initialAudio, initialVideo);
1158  if (initialAudio >= 0)
1159  SetAudioByComponentTag(initialAudio);
1160  if (initialVideo >= 0)
1161  SetVideoByComponentTag(initialVideo);
1162  }
1163 #endif // USING_MHEG
1164 
1165  // Try to get a position map from the recorder if we don't have one yet.
1167  {
1169  {
1172  {
1173  m_hasFullPositionMap = true;
1174  m_gopSet = true;
1175  }
1176  }
1177  }
1178 
1179  // If watching pre-recorded television or video use the marked duration
1180  // from the db if it exists, else ffmpeg duration
1181  int64_t dur = 0;
1182 
1183  if (m_playbackInfo)
1184  {
1186  dur /= 1000;
1187  }
1188 
1189  if (dur == 0)
1190  {
1191  if ((m_ic->duration == AV_NOPTS_VALUE) &&
1192  (!m_livetv && !m_ringBuffer->IsDisc()))
1193  av_estimate_timings(m_ic, 0);
1194 
1195  dur = m_ic->duration / (int64_t)AV_TIME_BASE;
1196  }
1197 
1198  if (dur > 0 && !m_livetv && !m_watchingRecording)
1199  {
1200  m_parent->SetDuration((int)dur);
1201  }
1202 
1203  // If we don't have a position map, set up ffmpeg for seeking
1205  {
1206  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1207  "Recording has no position -- using libavformat seeking.");
1208 
1209  if (dur > 0)
1210  {
1211  m_parent->SetFileLength((int)(dur), (int)(dur * m_fps));
1212  }
1213  else
1214  {
1215  // the pvr-250 seems to over report the bitrate by * 2
1216  float bytespersec = (float)m_bitrate / 8 / 2;
1217  float secs = m_ringBuffer->GetRealFileSize() * 1.0F / bytespersec;
1218  m_parent->SetFileLength((int)(secs),
1219  (int)(secs * static_cast<float>(m_fps)));
1220  }
1221 
1222  // we will not see a position map from db or remote encoder,
1223  // set the gop interval to 15 frames. if we guess wrong, the
1224  // auto detection will change it.
1225  m_keyframeDist = 15;
1227 
1228  if (strcmp(fmt->name, "avi") == 0)
1229  {
1230  // avi keyframes are too irregular
1231  m_keyframeDist = 1;
1232  }
1233 
1234  m_dontSyncPositionMap = true;
1235  m_ic->build_index = 1;
1236  }
1237  // we have a position map, disable libavformat's seek index
1238  else
1239  m_ic->build_index = 0;
1240 
1241  av_dump_format(m_ic, 0, filename, 0);
1242 
1243  // print some useful information if playback debugging is on
1245  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Position map found");
1246  else if (m_recordingHasPositionMap)
1247  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Partial position map found");
1248  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1249  QString("Successfully opened decoder for file: \"%1\". novideo(%2)")
1250  .arg(filename).arg(novideo));
1251 
1252  // Print AVChapter information
1253  for (unsigned int i=0; i < m_ic->nb_chapters; i++)
1254  {
1255  int num = m_ic->chapters[i]->time_base.num;
1256  int den = m_ic->chapters[i]->time_base.den;
1257  int64_t start = m_ic->chapters[i]->start;
1258  long double total_secs = (long double)start * (long double)num /
1259  (long double)den;
1260  int hours = (int)total_secs / 60 / 60;
1261  int minutes = ((int)total_secs / 60) - (hours * 60);
1262  double secs = (double)total_secs -
1263  (double)(hours * 60 * 60 + minutes * 60);
1264  auto framenum = (long long)(total_secs * m_fps);
1265  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1266  QString("Chapter %1 found @ [%2:%3:%4]->%5")
1267  .arg(i + 1, 2,10,QChar('0'))
1268  .arg(hours, 2,10,QChar('0'))
1269  .arg(minutes, 2,10,QChar('0'))
1270  .arg(secs, 6,'f',3,QChar('0'))
1271  .arg(framenum));
1272  }
1273 
1274  if (getenv("FORCE_DTS_TIMESTAMPS"))
1275  m_forceDtsTimestamps = true;
1276 
1277  if (m_ringBuffer->IsDVD())
1278  {
1279  // Reset DVD playback and clear any of
1280  // our buffers so that none of the data
1281  // parsed so far to determine decoders
1282  // gets shown.
1284  return -1;
1286 
1287  Reset(true, true, true);
1288 
1289  // Now we're ready to process and show frames
1290  m_processFrames = true;
1291  }
1292 
1293 
1294  // Return true if recording has position map
1296 }
1297 
1298 float AvFormatDecoder::GetVideoFrameRate(AVStream *Stream, AVCodecContext *Context, bool Sanitise)
1299 {
1300  double avg_fps = 0.0;
1301  double codec_fps = 0.0;
1302  double container_fps = 0.0;
1303  double estimated_fps = 0.0;
1304 
1305  if (Stream->avg_frame_rate.den && Stream->avg_frame_rate.num)
1306  avg_fps = av_q2d(Stream->avg_frame_rate); // MKV default_duration
1307 
1308  if (Context->time_base.den && Context->time_base.num) // tbc
1309  codec_fps = 1.0 / av_q2d(Context->time_base) / Context->ticks_per_frame;
1310  // Some formats report fps waaay too high. (wrong time_base)
1311  if (codec_fps > 121.0 && (Context->time_base.den > 10000) &&
1312  (Context->time_base.num == 1))
1313  {
1314  Context->time_base.num = 1001; // seems pretty standard
1315  if (av_q2d(Context->time_base) > 0)
1316  codec_fps = 1.0 / av_q2d(Context->time_base);
1317  }
1318  if (Stream->time_base.den && Stream->time_base.num) // tbn
1319  container_fps = 1.0 / av_q2d(Stream->time_base);
1320  if (Stream->r_frame_rate.den && Stream->r_frame_rate.num) // tbr
1321  estimated_fps = av_q2d(Stream->r_frame_rate);
1322 
1323  // build a list of possible rates, best first
1324  vector<double> rates;
1325 
1326  // matroska demuxer sets the default_duration to avg_frame_rate
1327  // mov,mp4,m4a,3gp,3g2,mj2 demuxer sets avg_frame_rate
1328  if ((QString(m_ic->iformat->name).contains("matroska") ||
1329  QString(m_ic->iformat->name).contains("mov")) &&
1330  avg_fps < 121.0 && avg_fps > 3.0)
1331  {
1332  rates.emplace_back(avg_fps);
1333  }
1334 
1335  if (QString(m_ic->iformat->name).contains("avi") &&
1336  container_fps < 121.0 && container_fps > 3.0)
1337  {
1338  // avi uses container fps for timestamps
1339  rates.emplace_back(container_fps);
1340  }
1341 
1342  if (codec_fps < 121.0 && codec_fps > 3.0)
1343  rates.emplace_back(codec_fps);
1344 
1345  if (container_fps < 121.0 && container_fps > 3.0)
1346  rates.emplace_back(container_fps);
1347 
1348  if (avg_fps < 121.0 && avg_fps > 3.0)
1349  rates.emplace_back(avg_fps);
1350 
1351  // certain H.264 interlaced streams are detected at 2x using estimated (i.e. wrong)
1352  if (estimated_fps < 121.0 && estimated_fps > 3.0)
1353  rates.emplace_back(estimated_fps);
1354 
1355  // last resort
1356  rates.emplace_back(static_cast<float>(30000.0 / 1001.0));
1357 
1358  // debug
1359  if (!qFuzzyCompare(static_cast<float>(rates.front()), m_fps))
1360  {
1361  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1362  QString("Selected FPS: %1 (Avg:%2 Mult:%3 Codec:%4 Container:%5 Estimated:%6)")
1363  .arg(static_cast<double>(rates.front())).arg(avg_fps)
1364  .arg(m_fpsMultiplier).arg(codec_fps).arg(container_fps).arg(estimated_fps));
1365  }
1366 
1367  auto IsStandard = [](double Rate)
1368  {
1369  // List of known, standards based frame rates
1370  static const vector<double> s_normRates =
1371  {
1372  24000.0 / 1001.0, 23.976, 25.0, 30000.0 / 1001.0,
1373  29.97, 30.0, 50.0, 60000.0 / 1001.0, 59.94, 60.0, 100.0,
1374  120000.0 / 1001.0, 119.88, 120.0
1375  };
1376 
1377  if (Rate > 1.0 && Rate < 121.0)
1378  for (auto rate : s_normRates)
1379  if (qFuzzyCompare(rate, Rate))
1380  return true;
1381  return false;
1382  };
1383 
1384  // If the first choice rate is unusual, see if there is something more 'usual'
1385  if (Sanitise && !IsStandard(rates.front()))
1386  {
1387  for (auto rate : rates)
1388  {
1389  if (IsStandard(rate))
1390  {
1391  LOG(VB_GENERAL, LOG_INFO, LOC + QString("%1 is non-standard. Selecting %2 instead.")
1392  .arg(rates.front()).arg(rate));
1393  return static_cast<float>(rate);
1394  }
1395  }
1396  }
1397 
1398  return static_cast<float>(rates.front());
1399 }
1400 
1401 #ifdef USING_DXVA2
1402 // Declared separately to allow attribute
1403 static enum AVPixelFormat get_format_dxva2(struct AVCodecContext *,
1404  const enum AVPixelFormat *);
1405 
1406 enum AVPixelFormat get_format_dxva2(struct AVCodecContext *avctx,
1407  const enum AVPixelFormat *valid_fmts)
1408 {
1409  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1410  if (nd && nd->GetPlayer())
1411  {
1412  static uint8_t *dummy[1] = { nullptr };
1413  avctx->hwaccel_context =
1414  (dxva_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1415  }
1416 
1417  while (*valid_fmts != AV_PIX_FMT_NONE) {
1418  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_DXVA2_VLD))
1419  return AV_PIX_FMT_DXVA2_VLD;
1420  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1421  return AV_PIX_FMT_YUV420P;
1422  valid_fmts++;
1423  }
1424  return AV_PIX_FMT_NONE;
1425 }
1426 #endif
1427 
1429 {
1430  switch (Context->codec_id)
1431  {
1432  case AV_CODEC_ID_H264:
1433  {
1434  int result = 16;
1435  if (Context->extradata && (Context->extradata_size >= 7))
1436  {
1437  uint8_t offset = 0;
1438  if (Context->extradata[0] == 1)
1439  offset = 9; // avCC
1440  else if (AV_RB24(Context->extradata) == 0x01) // Annex B - 3 byte startcode 0x000001
1441  offset = 4;
1442  else if (AV_RB32(Context->extradata) == 0x01) // Annex B - 4 byte startcode 0x00000001
1443  offset= 5;
1444 
1445  if (offset)
1446  {
1448  bool dummy = false;
1449  parser.parse_SPS(Context->extradata + offset,
1450  static_cast<uint>(Context->extradata_size - offset), dummy, result);
1451  }
1452  }
1453  return result;
1454  }
1455  case AV_CODEC_ID_H265: return 16;
1456  case AV_CODEC_ID_VP9: return 8;
1457  case AV_CODEC_ID_VP8: return 3;
1458  default: break;
1459  }
1460  return 2;
1461 }
1462 
1463 void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
1464  bool selectedStream)
1465 {
1466  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1467  QString("InitVideoCodec ID:%1 Type:%2 Size:%3x%4")
1468  .arg(ff_codec_id_string(enc->codec_id))
1469  .arg(ff_codec_type_string(enc->codec_type))
1470  .arg(enc->width).arg(enc->height));
1471 
1472  if (m_ringBuffer && m_ringBuffer->IsDVD())
1473  m_directRendering = false;
1474 
1475  enc->opaque = static_cast<void*>(this);
1476  enc->get_buffer2 = get_avf_buffer;
1477  enc->slice_flags = 0;
1478 
1479  enc->err_recognition = AV_EF_COMPLIANT;
1480  enc->workaround_bugs = FF_BUG_AUTODETECT;
1481  enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
1482  enc->idct_algo = FF_IDCT_AUTO;
1483  enc->debug = 0;
1484  // enc->error_rate = 0;
1485 
1486  const AVCodec *codec1 = enc->codec;
1487 
1488  if (selectedStream)
1489  m_directRendering = true;
1490 
1491  // retrieve rotation information
1492  uint8_t* displaymatrix = av_stream_get_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, nullptr);
1493  if (displaymatrix)
1494  m_videoRotation = static_cast<int>(-av_display_rotation_get(reinterpret_cast<int32_t*>(displaymatrix)));
1495  else
1496  m_videoRotation = 0;
1497 
1498  delete m_mythCodecCtx;
1500  m_mythCodecCtx->InitVideoCodec(enc, selectedStream, m_directRendering);
1501  if (m_mythCodecCtx->HwDecoderInit(enc) < 0)
1502  {
1503  // force it to switch to software decoding
1505  m_streamsChanged = true;
1506  }
1507  else
1508  {
1509  bool doublerate = m_parent->CanSupportDoubleRate();
1511  }
1512 
1516  {
1517  if (codec1 &&
1518  ((AV_CODEC_ID_MPEG2VIDEO == codec1->id) ||
1519  (AV_CODEC_ID_MPEG1VIDEO == codec1->id)))
1520  {
1522  {
1523  uint total_blocks = (enc->height+15) / 16;
1524  enc->skip_top = (total_blocks+3) / 4;
1525  enc->skip_bottom = (total_blocks+3) / 4;
1526  }
1527 
1528  if (FlagIsSet(kDecodeLowRes))
1529  enc->lowres = 2; // 1 = 1/2 size, 2 = 1/4 size
1530  }
1531  else if (codec1 && (AV_CODEC_ID_H264 == codec1->id))
1532  {
1534  {
1535  enc->flags &= ~AV_CODEC_FLAG_LOOP_FILTER;
1536  enc->skip_loop_filter = AVDISCARD_ALL;
1537  }
1538  }
1539 
1541  {
1542  enc->skip_idct = AVDISCARD_ALL;
1543  }
1544  }
1545 
1546  if (selectedStream)
1547  {
1548  m_fps = GetVideoFrameRate(stream, enc, true);
1549  QSize dim = get_video_dim(*enc);
1550  int width = m_currentWidth = dim.width();
1551  int height = m_currentHeight = dim.height();
1552  m_currentAspect = get_aspect(*enc);
1553 
1554  if (!width || !height)
1555  {
1556  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1557  "InitVideoCodec invalid dimensions, resetting decoder.");
1558  width = 640;
1559  height = 480;
1560  m_fps = 29.97F;
1561  m_currentAspect = 4.0F / 3.0F;
1562  }
1563 
1565  const AVCodec *codec2 = enc->codec;
1566  QString codecName;
1567  if (codec2)
1568  codecName = codec2->name;
1569  m_parent->SetVideoParams(width, height, static_cast<double>(m_fps),
1571  kScan_Detect, codecName);
1572  if (LCD *lcd = LCD::Get())
1573  {
1574  LCDVideoFormatSet video_format = VIDEO_MPG;
1575 
1576  switch (enc->codec_id)
1577  {
1578  case AV_CODEC_ID_H263:
1579  case AV_CODEC_ID_MPEG4:
1580  case AV_CODEC_ID_MSMPEG4V1:
1581  case AV_CODEC_ID_MSMPEG4V2:
1582  case AV_CODEC_ID_MSMPEG4V3:
1583  case AV_CODEC_ID_H263P:
1584  case AV_CODEC_ID_H263I:
1585  video_format = VIDEO_DIVX;
1586  break;
1587  case AV_CODEC_ID_WMV1:
1588  case AV_CODEC_ID_WMV2:
1589  video_format = VIDEO_WMV;
1590  break;
1591 #if 0
1592  case AV_CODEC_ID_XVID:
1593  video_format = VIDEO_XVID;
1594  break;
1595 #endif
1596  default:
1597  video_format = VIDEO_MPG;
1598  break;
1599  }
1600 
1601  lcd->setVideoFormatLEDs(video_format, true);
1602 
1603  if(height >= 720)
1604  lcd->setVariousLEDs(VARIOUS_HDTV, true);
1605  else
1606  lcd->setVariousLEDs(VARIOUS_HDTV, false);
1607  }
1608  }
1609 }
1610 
1611 // CC Parity checking
1612 // taken from xine-lib libspucc
1613 
1614 static int cc608_parity(uint8_t byte)
1615 {
1616  int ones = 0;
1617 
1618  for (int i = 0; i < 7; i++)
1619  {
1620  if (byte & (1 << i))
1621  ones++;
1622  }
1623 
1624  return ones & 1;
1625 }
1626 
1627 // CC Parity checking
1628 // taken from xine-lib libspucc
1629 
1630 static void cc608_build_parity_table(int *parity_table)
1631 {
1632  for (uint8_t byte = 0; byte <= 127; byte++)
1633  {
1634  int parity_v = cc608_parity(byte);
1635  /* CC uses odd parity (i.e., # of 1's in byte is odd.) */
1636  parity_table[byte] = parity_v;
1637  parity_table[byte | 0x80] = (parity_v == 0 ? 1 : 0);
1638  }
1639 }
1640 
1641 // CC Parity checking
1642 // taken from xine-lib libspucc
1643 
1644 static int cc608_good_parity(const int *parity_table, uint16_t data)
1645 {
1646  bool ret = (parity_table[data & 0xff] != 0)
1647  && (parity_table[(data & 0xff00) >> 8] != 0);
1648  if (!ret)
1649  {
1650  LOG(VB_VBI, LOG_ERR, LOC +
1651  QString("VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
1652  }
1653  return ret ? 1 : 0;
1654 }
1655 
1657 {
1658  memset(m_ccX08InPmt, 0, sizeof(m_ccX08InPmt));
1659  m_pmtTracks.clear();
1660  m_pmtTrackTypes.clear();
1661 
1662  // Figure out languages of ATSC captions
1663  if (!m_ic->cur_pmt_sect)
1664  {
1665  LOG(VB_GENERAL, LOG_DEBUG, LOC +
1666  "ScanATSCCaptionStreams() called with no PMT");
1667  return;
1668  }
1669 
1670  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
1671 
1672  uint i = 0;
1673  for (i = 0; i < pmt.StreamCount(); i++)
1674  {
1675  // MythTV remaps OpenCable Video to normal video during recording
1676  // so "dvb" is the safest choice for system info type, since this
1677  // will ignore other uses of the same stream id in DVB countries.
1678  if (pmt.IsVideo(i, "dvb"))
1679  break;
1680  }
1681 
1682  if (!pmt.IsVideo(i, "dvb"))
1683  return;
1684 
1686  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1688 
1689  const desc_list_t desc_list2 = MPEGDescriptor::ParseOnlyInclude(
1690  pmt.ProgramInfo(), pmt.ProgramInfoLength(),
1692 
1693  desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
1694 
1695  for (auto & desc : desc_list)
1696  {
1697  const CaptionServiceDescriptor csd(desc);
1698  if (!csd.IsValid())
1699  continue;
1700  for (uint k = 0; k < csd.ServicesCount(); k++)
1701  {
1702  int lang = csd.CanonicalLanguageKey(k);
1703  int type = csd.Type(k) ? 1 : 0;
1704  if (type)
1705  {
1706  StreamInfo si(av_index, lang, 0/*lang_idx*/,
1707  csd.CaptionServiceNumber(k),
1708  csd.EasyReader(k),
1709  csd.WideAspectRatio(k));
1710  uint key = csd.CaptionServiceNumber(k) + 4;
1711  m_ccX08InPmt[key] = true;
1712  m_pmtTracks.push_back(si);
1713  m_pmtTrackTypes.push_back(kTrackTypeCC708);
1714  }
1715  else
1716  {
1717  int line21 = csd.Line21Field(k) ? 3 : 1;
1718  StreamInfo si(av_index, lang, 0/*lang_idx*/, line21, 0);
1719  m_ccX08InPmt[line21-1] = true;
1720  m_pmtTracks.push_back(si);
1721  m_pmtTrackTypes.push_back(kTrackTypeCC608);
1722  }
1723  }
1724  }
1725 }
1726 
1728 {
1729  m_tracks[kTrackTypeCC608].clear();
1730  m_tracks[kTrackTypeCC708].clear();
1731  memset(m_ccX08InTracks, 0, sizeof(m_ccX08InTracks));
1732 
1733  uint pidx = 0;
1734  uint sidx = 0;
1735  map<int,uint> lang_cc_cnt[2];
1736  while (true)
1737  {
1738  bool pofr = pidx >= (uint)m_pmtTracks.size();
1739  bool sofr = sidx >= (uint)m_streamTracks.size();
1740  if (pofr && sofr)
1741  break;
1742 
1743  // choose lowest available next..
1744  // stream_id's of 608 and 708 streams alias, but this
1745  // is ok as we just want each list to be ordered.
1746  StreamInfo const *si = nullptr;
1747  int type = 0; // 0 if 608, 1 if 708
1748  bool isp = true; // if true use m_pmtTracks next, else stream_tracks
1749 
1750  if (pofr && !sofr)
1751  isp = false; // NOLINT(bugprone-branch-clone)
1752  else if (!pofr && sofr)
1753  isp = true;
1754  else if (m_streamTracks[sidx] < m_pmtTracks[pidx])
1755  isp = false;
1756 
1757  if (isp)
1758  {
1759  si = &m_pmtTracks[pidx];
1760  type = kTrackTypeCC708 == m_pmtTrackTypes[pidx] ? 1 : 0;
1761  pidx++;
1762  }
1763  else
1764  {
1765  si = &m_streamTracks[sidx];
1766  type = kTrackTypeCC708 == m_streamTrackTypes[sidx] ? 1 : 0;
1767  sidx++;
1768  }
1769 
1770  StreamInfo nsi(*si);
1771  int lang_indx = lang_cc_cnt[type][nsi.m_language];
1772  lang_cc_cnt[type][nsi.m_language]++;
1773  nsi.m_language_index = lang_indx;
1774  m_tracks[(type) ? kTrackTypeCC708 : kTrackTypeCC608].push_back(nsi);
1775  int key = nsi.m_stream_id + ((type) ? 4 : -1);
1776  if (key < 0)
1777  {
1778  LOG(VB_GENERAL, LOG_ERR, LOC + "in_tracks key too small");
1779  }
1780  else
1781  {
1782  m_ccX08InTracks[key] = true;
1783  }
1784  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1785  QString("%1 caption service #%2 is in the %3 language.")
1786  .arg((type) ? "EIA-708" : "EIA-608")
1787  .arg(nsi.m_stream_id)
1788  .arg(iso639_key_toName(nsi.m_language)));
1789  }
1790 }
1791 
1793 {
1794  // ScanStreams() calls m_tracks[kTrackTypeTeletextCaptions].clear()
1795  if (!m_ic->cur_pmt_sect || !m_tracks[kTrackTypeTeletextCaptions].empty())
1796  return;
1797 
1798  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
1799 
1800  for (uint i = 0; i < pmt.StreamCount(); i++)
1801  {
1802  if (pmt.StreamType(i) != StreamID::PrivData)
1803  continue;
1804 
1806  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1808 
1809  for (auto desc : desc_list)
1810  {
1811  const TeletextDescriptor td(desc);
1812  if (!td.IsValid())
1813  continue;
1814  for (uint k = 0; k < td.StreamCount(); k++)
1815  {
1816  int type = td.TeletextType(k);
1817  int language = td.CanonicalLanguageKey(k);
1818  int magazine = td.TeletextMagazineNum(k);
1819  if (magazine == 0)
1820  magazine = 8;
1821  int pagenum = td.TeletextPageNum(k);
1822  int lang_idx = (magazine << 8) | pagenum;
1823  StreamInfo si(av_index, language, lang_idx, 0, 0);
1824  if (type == 2 || type == 1)
1825  {
1826  TrackType track = (type == 2) ? kTrackTypeTeletextCaptions :
1828  m_tracks[track].push_back(si);
1829  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1830  QString("Teletext stream #%1 (%2) is in the %3 language"
1831  " on page %4 %5.")
1832  .arg(k).arg((type == 2) ? "Caption" : "Menu")
1833  .arg(iso639_key_toName(language))
1834  .arg(magazine).arg(pagenum));
1835  }
1836  }
1837  }
1838 
1839  // Assume there is only one multiplexed teletext stream in PMT..
1840  if (!m_tracks[kTrackTypeTeletextCaptions].empty())
1841  break;
1842  }
1843 }
1844 
1845 void AvFormatDecoder::ScanRawTextCaptions(int av_stream_index)
1846 {
1847  AVDictionaryEntry *metatag =
1848  av_dict_get(m_ic->streams[av_stream_index]->metadata, "language", nullptr,
1849  0);
1850  bool forced =
1851  (m_ic->streams[av_stream_index]->disposition & AV_DISPOSITION_FORCED) != 0;
1852  int lang = metatag ? get_canonical_lang(metatag->value) :
1853  iso639_str3_to_key("und");
1854  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1855  QString("Text Subtitle track #%1 is A/V stream #%2 "
1856  "and is in the %3 language(%4), forced=%5.")
1857  .arg(m_tracks[kTrackTypeRawText].size()).arg(av_stream_index)
1858  .arg(iso639_key_toName(lang)).arg(lang).arg(forced));
1859  StreamInfo si(av_stream_index, lang, 0, 0, 0, false, false, forced);
1860  m_tracks[kTrackTypeRawText].push_back(si);
1861 }
1862 
1868 {
1869  if (!m_ic || !m_ic->cur_pmt_sect)
1870  return;
1871 
1872  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
1873  return;
1874 
1875  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
1876 
1877  for (uint i = 0; i < pmt.StreamCount(); i++)
1878  {
1879  if (! StreamID::IsObjectCarousel(pmt.StreamType(i)))
1880  continue;
1881 
1882  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams Found Object Carousel in Stream %1").arg(QString::number(i)));
1883 
1885  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1887 
1888  for (auto desc : desc_list)
1889  {
1890  desc++; // Skip tag
1891  uint length = *desc++;
1892  const unsigned char *endDesc = desc+length;
1893  uint dataBroadcastId = desc[0]<<8 | desc[1];
1894  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams dataBroadcastId %1").arg(QString::number(dataBroadcastId)));
1895  if (dataBroadcastId != 0x0106) // ETSI/UK Profile
1896  continue;
1897  desc += 2; // Skip data ID
1898  while (desc != endDesc)
1899  {
1900  uint appTypeCode = desc[0]<<8 | desc[1];
1901  desc += 3; // Skip app type code and boot priority hint
1902  uint appSpecDataLen = *desc++;
1903 #ifdef USING_MHEG
1904  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams AppTypeCode %1").arg(QString::number(appTypeCode)));
1905  if (appTypeCode == 0x101) // UK MHEG profile
1906  {
1907  const unsigned char *subDescEnd = desc + appSpecDataLen;
1908  while (desc < subDescEnd)
1909  {
1910  uint sub_desc_tag = *desc++;
1911  uint sub_desc_len = *desc++;
1912  // Network boot info sub-descriptor.
1913  if (sub_desc_tag == 1)
1914  m_itv->SetNetBootInfo(desc, sub_desc_len);
1915  desc += sub_desc_len;
1916  }
1917  }
1918  else
1919 #else
1920  (void) appTypeCode;
1921 #endif // USING_MHEG
1922  {
1923  desc += appSpecDataLen;
1924  }
1925  }
1926  }
1927  }
1928 }
1929 
1931 {
1932  bool unknownbitrate = false;
1933  int scanerror = 0;
1934  m_bitrate = 0;
1935 
1936  m_tracks[kTrackTypeAttachment].clear();
1937  m_tracks[kTrackTypeAudio].clear();
1938  m_tracks[kTrackTypeSubtitle].clear();
1941  m_tracks[kTrackTypeRawText].clear();
1942  if (!novideo)
1943  {
1944  // we will rescan video streams
1945  m_tracks[kTrackTypeVideo].clear();
1947  m_fps = 0;
1948  }
1949  map<int,uint> lang_sub_cnt;
1950  uint subtitleStreamCount = 0;
1951  map<int,uint> lang_aud_cnt;
1952  uint audioStreamCount = 0;
1953 
1954  if (m_ringBuffer && m_ringBuffer->IsDVD() &&
1956  {
1959  }
1960 
1961  for (uint strm = 0; strm < m_ic->nb_streams; strm++)
1962  {
1963  AVCodecParameters *par = m_ic->streams[strm]->codecpar;
1964  AVCodecContext *enc = nullptr;
1965 
1966  QString codectype(ff_codec_type_string(par->codec_type));
1967  if (par->codec_type == AVMEDIA_TYPE_VIDEO)
1968  codectype += QString("(%1x%2)").arg(par->width).arg(par->height);
1969  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1970  QString("Stream #%1: ID: 0x%2 Codec ID: %3 Type: %4 Bitrate: %5")
1971  .arg(strm).arg(static_cast<uint64_t>(m_ic->streams[strm]->id), 0, 16)
1972  .arg(ff_codec_id_string(par->codec_id))
1973  .arg(codectype).arg(par->bit_rate));
1974 
1975  switch (par->codec_type)
1976  {
1977  case AVMEDIA_TYPE_VIDEO:
1978  {
1979  // reset any potentially errored hardware decoders
1981  {
1982  if (gCodecMap->hasCodecContext(m_ic->streams[strm]))
1983  {
1984  AVCodecContext* ctx = gCodecMap->getCodecContext(m_ic->streams[strm]);
1985  if (ctx && (ctx->hw_frames_ctx || ctx->hw_device_ctx))
1986  gCodecMap->freeCodecContext(m_ic->streams[strm]);
1987  }
1988  }
1989 
1990  if (!par->codec_id)
1991  {
1992  LOG(VB_GENERAL, LOG_ERR, LOC +
1993  QString("Stream #%1 has an unknown video "
1994  "codec id, skipping.").arg(strm));
1995  continue;
1996  }
1997 
1998  // ffmpeg does not return a bitrate for several codecs and
1999  // formats. Forcing it to 500000 ensures the ringbuffer does not
2000  // use optimisations for low bitrate (audio and data) streams.
2001  if (par->bit_rate == 0)
2002  {
2003  par->bit_rate = 500000;
2004  unknownbitrate = true;
2005  }
2006  m_bitrate += par->bit_rate;
2007 
2008  break;
2009  }
2010  case AVMEDIA_TYPE_AUDIO:
2011  {
2012  enc = gCodecMap->hasCodecContext(m_ic->streams[strm]);
2013  if (enc && enc->internal)
2014  {
2015  LOG(VB_GENERAL, LOG_WARNING, LOC +
2016  QString("Warning, audio codec 0x%1 id(%2) "
2017  "type (%3) already open, leaving it alone.")
2018  .arg(reinterpret_cast<unsigned long long>(enc), 0, 16)
2019  .arg(ff_codec_id_string(enc->codec_id))
2020  .arg(ff_codec_type_string(enc->codec_type)));
2021  }
2022  LOG(VB_GENERAL, LOG_INFO, LOC +
2023  QString("codec %1 has %2 channels")
2024  .arg(ff_codec_id_string(par->codec_id))
2025  .arg(par->channels));
2026 
2027  m_bitrate += par->bit_rate;
2028  break;
2029  }
2030  case AVMEDIA_TYPE_SUBTITLE:
2031  {
2032  if (par->codec_id == AV_CODEC_ID_DVB_TELETEXT)
2033  ScanTeletextCaptions(static_cast<int>(strm));
2034  if (par->codec_id == AV_CODEC_ID_TEXT)
2035  ScanRawTextCaptions(static_cast<int>(strm));
2036  m_bitrate += par->bit_rate;
2037 
2038  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("subtitle codec (%1)")
2039  .arg(ff_codec_type_string(par->codec_type)));
2040  break;
2041  }
2042  case AVMEDIA_TYPE_DATA:
2043  {
2044  ScanTeletextCaptions(static_cast<int>(strm));
2045  m_bitrate += par->bit_rate;
2046  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("data codec (%1)")
2047  .arg(ff_codec_type_string(par->codec_type)));
2048  break;
2049  }
2050  case AVMEDIA_TYPE_ATTACHMENT:
2051  {
2052  if (par->codec_id == AV_CODEC_ID_TTF)
2053  m_tracks[kTrackTypeAttachment].push_back(
2054  StreamInfo(static_cast<int>(strm), 0, 0, m_ic->streams[strm]->id, 0));
2055  m_bitrate += par->bit_rate;
2056  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2057  QString("Attachment codec (%1)")
2058  .arg(ff_codec_type_string(par->codec_type)));
2059  break;
2060  }
2061  default:
2062  {
2063  m_bitrate += par->bit_rate;
2064  LOG(VB_PLAYBACK, LOG_ERR, LOC +
2065  QString("Unknown codec type (%1)")
2066  .arg(ff_codec_type_string(par->codec_type)));
2067  break;
2068  }
2069  }
2070 
2071  if (par->codec_type != AVMEDIA_TYPE_AUDIO &&
2072  par->codec_type != AVMEDIA_TYPE_SUBTITLE)
2073  continue;
2074 
2075  // skip DVB teletext and text subs, there is no libavcodec decoder
2076  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2077  (par->codec_id == AV_CODEC_ID_DVB_TELETEXT ||
2078  par->codec_id == AV_CODEC_ID_TEXT))
2079  continue;
2080 
2081  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Looking for decoder for %1")
2082  .arg(ff_codec_id_string(par->codec_id)));
2083 
2084  if (par->codec_id == AV_CODEC_ID_PROBE)
2085  {
2086  LOG(VB_GENERAL, LOG_ERR, LOC +
2087  QString("Probing of stream #%1 unsuccesful, ignoring.").arg(strm));
2088  continue;
2089  }
2090 
2091  if (!enc)
2092  enc = gCodecMap->getCodecContext(m_ic->streams[strm]);
2093 
2094  const AVCodec *codec = nullptr;
2095  if (enc)
2096  codec = enc->codec;
2097  else
2098  {
2099  LOG(VB_GENERAL, LOG_ERR, LOC +
2100  QString("Could not find decoder for codec (%1), ignoring.")
2101  .arg(ff_codec_id_string(par->codec_id)));
2102 
2103  // Nigel's bogus codec-debug. Dump the list of codecs & decoders,
2104  // and have one last attempt to find a decoder. This is usually
2105  // only caused by build problems, where libavcodec needs a rebuild
2106  if (VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY))
2107  {
2108  void *opaque = nullptr;
2109  const AVCodec *p = av_codec_iterate(&opaque);
2110  int i = 1;
2111  while (p)
2112  {
2113  QString msg;
2114 
2115  if (p->name[0] != '\0')
2116  msg = QString("Codec %1:").arg(p->name);
2117  else
2118  msg = QString("Codec %1, null name,").arg(strm);
2119 
2120  if (p->decode == nullptr)
2121  msg += "decoder is null";
2122 
2123  LOG(VB_LIBAV, LOG_INFO, LOC + msg);
2124 
2125  if (p->id == par->codec_id)
2126  {
2127  codec = p;
2128  break;
2129  }
2130 
2131  LOG(VB_LIBAV, LOG_INFO, LOC +
2132  QString("Codec 0x%1 != 0x%2") .arg(p->id, 0, 16)
2133  .arg(par->codec_id, 0, 16));
2134  p = av_codec_iterate(&opaque);
2135  ++i;
2136  }
2137  }
2138  if (codec)
2139  enc = gCodecMap->getCodecContext(m_ic->streams[strm], codec);
2140  else
2141  continue;
2142  }
2143  if (!enc)
2144  continue;
2145 
2146  if (enc->codec && par->codec_id != enc->codec_id)
2147  {
2148  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2149  QString("Already opened codec not matching (%1 vs %2). Reopening")
2150  .arg(ff_codec_id_string(enc->codec_id))
2151  .arg(ff_codec_id_string(enc->codec->id)));
2152  gCodecMap->freeCodecContext(m_ic->streams[strm]);
2153  enc = gCodecMap->getCodecContext(m_ic->streams[strm]);
2154  }
2155  if (!OpenAVCodec(enc, codec))
2156  continue;
2157  if (!enc)
2158  continue;
2159 
2160  if (!IsValidStream(m_ic->streams[strm]->id))
2161  {
2162  /* Hide this stream if it's not valid in this context.
2163  * This can happen, for example, on a Blu-ray disc if there
2164  * are more physical streams than there is metadata about them.
2165  * (e.g. Despicable Me)
2166  */
2167  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2168  QString("Stream 0x%1 is not valid in this context - skipping")
2169  .arg(m_ic->streams[strm]->id, 4, 16));
2170  continue;
2171  }
2172 
2173  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2174  {
2175  bool forced = (m_ic->streams[strm]->disposition & AV_DISPOSITION_FORCED) != 0;
2176  int lang = GetSubtitleLanguage(subtitleStreamCount, strm);
2177  uint lang_indx = lang_sub_cnt[lang]++;
2178  subtitleStreamCount++;
2179 
2180  m_tracks[kTrackTypeSubtitle].push_back(
2181  StreamInfo(static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, 0, 0, false, false, forced));
2182 
2183  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2184  QString("Subtitle track #%1 is A/V stream #%2 "
2185  "and is in the %3 language(%4).")
2186  .arg(m_tracks[kTrackTypeSubtitle].size()).arg(strm)
2187  .arg(iso639_key_toName(lang)).arg(lang));
2188  }
2189 
2190  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
2191  {
2192  int lang = GetAudioLanguage(audioStreamCount, strm);
2194  int channels = par->channels;
2195  uint lang_indx = lang_aud_cnt[lang]++;
2196  audioStreamCount++;
2197 
2198  if (enc->avcodec_dual_language)
2199  {
2200  m_tracks[kTrackTypeAudio].push_back(
2201  StreamInfo(static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, channels,
2202  false, false, false, type));
2203  lang_indx = lang_aud_cnt[lang]++;
2204  m_tracks[kTrackTypeAudio].push_back(
2205  StreamInfo(static_cast<int>(strm), lang, lang_indx, m_ic->streams[strm]->id, channels,
2206  true, false, false, type));
2207  }
2208  else
2209  {
2210  int logical_stream_id = 0;
2211  if (m_ringBuffer && m_ringBuffer->IsDVD())
2212  {
2213  logical_stream_id = m_ringBuffer->DVD()->GetAudioTrackNum(m_ic->streams[strm]->id);
2214  channels = m_ringBuffer->DVD()->GetNumAudioChannels(logical_stream_id);
2215  }
2216  else
2217  logical_stream_id = m_ic->streams[strm]->id;
2218 
2219  if (logical_stream_id == -1)
2220  {
2221  // This stream isn't mapped, so skip it
2222  continue;
2223  }
2224 
2225  m_tracks[kTrackTypeAudio].push_back(
2226  StreamInfo(static_cast<int>(strm), lang, lang_indx, logical_stream_id, channels,
2227  false, false, false, type));
2228  }
2229 
2230  LOG(VB_AUDIO, LOG_INFO, LOC +
2231  QString("Audio Track #%1, of type (%2) is A/V stream #%3 (id=0x%4) "
2232  "and has %5 channels in the %6 language(%7).")
2233  .arg(m_tracks[kTrackTypeAudio].size()).arg(toString(type))
2234  .arg(strm).arg(m_ic->streams[strm]->id,0,16).arg(enc->channels)
2235  .arg(iso639_key_toName(lang)).arg(lang));
2236  }
2237  }
2238 
2239  // Now find best video track to play
2240  m_resetHardwareDecoders = false;
2241  if (!novideo && m_ic)
2242  {
2243  for(;;)
2244  {
2245  AVCodec *codec = nullptr;
2246  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Trying to select best video track");
2247 
2248  /*
2249  * Find the "best" stream in the file.
2250  *
2251  * The best stream is determined according to various heuristics as
2252  * the most likely to be what the user expects. If the decoder parameter
2253  * is not nullptr, av_find_best_stream will find the default decoder
2254  * for the stream's codec; streams for which no decoder can be found
2255  * are ignored.
2256  *
2257  * If av_find_best_stream returns successfully and decoder_ret is not nullptr,
2258  * then *decoder_ret is guaranteed to be set to a valid AVCodec.
2259  */
2260  int selTrack = av_find_best_stream(m_ic, AVMEDIA_TYPE_VIDEO,
2261  -1, -1, &codec, 0);
2262 
2263  if (selTrack < 0)
2264  {
2265  LOG(VB_PLAYBACK, LOG_INFO, LOC + "No video track found/selected.");
2266  break;
2267  }
2268 
2269  AVStream *stream = m_ic->streams[selTrack];
2271  gCodecMap->freeCodecContext(stream);
2272  AVCodecContext *enc = gCodecMap->getCodecContext(stream, codec);
2273  StreamInfo si(selTrack, 0, 0, 0, 0);
2274 
2275  m_tracks[kTrackTypeVideo].push_back(si);
2277 
2278  QString codectype(ff_codec_type_string(enc->codec_type));
2279  if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2280  codectype += QString("(%1x%2)").arg(enc->width).arg(enc->height);
2281  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2282  QString("Selected track #%1: ID: 0x%2 Codec ID: %3 Profile: %4 Type: %5 Bitrate: %6")
2283  .arg(selTrack).arg(static_cast<uint64_t>(stream->id), 0, 16)
2284  .arg(ff_codec_id_string(enc->codec_id))
2285  .arg(enc->profile).arg(codectype).arg(enc->bit_rate));
2286 
2287  // If ScanStreams has been called on a stream change triggered by a
2288  // decoder error - because the decoder does not handle resolution
2289  // changes gracefully (NVDEC and maybe MediaCodec) - then the stream/codec
2290  // will still contain the old resolution but the AVCodecContext will
2291  // have been updated. This causes mayhem for a second or two.
2292  if ((enc->width != stream->codecpar->width) || (enc->height != stream->codecpar->height))
2293  {
2294  LOG(VB_GENERAL, LOG_INFO, LOC + QString(
2295  "Video resolution mismatch: Context: %1x%2 Stream: %3x%4 Codec: %5 Stream change: %6")
2296  .arg(enc->width).arg(enc->height)
2297  .arg(stream->codecpar->width).arg(stream->codecpar->height)
2299  }
2300 
2301  delete m_privateDec;
2302  m_privateDec = nullptr;
2303  m_h264Parser->Reset();
2304 
2305  QSize dim = get_video_dim(*enc);
2306  int width = max(dim.width(), 16);
2307  int height = max(dim.height(), 16);
2308  QString dec = "ffmpeg";
2309  uint thread_count = 1;
2310  QString codecName;
2311  if (enc->codec)
2312  codecName = enc->codec->name;
2313  if (enc->framerate.den && enc->framerate.num)
2314  m_fps = float(enc->framerate.num) / float(enc->framerate.den);
2315  else
2316  m_fps = 0;
2317  if (!m_isDbIgnored)
2318  {
2319  m_videoDisplayProfile.SetInput(QSize(width, height), m_fps, codecName);
2321  thread_count = m_videoDisplayProfile.GetMaxCPUs();
2322  bool skip_loop_filter = m_videoDisplayProfile.IsSkipLoopEnabled();
2323  if (!skip_loop_filter)
2324  enc->skip_loop_filter = AVDISCARD_NONKEY;
2325  }
2326 
2328  uint version = mpeg_version(enc->codec_id);
2329  if (version)
2330  m_videoCodecId = static_cast<MythCodecID>(kCodec_MPEG1 + version - 1);
2331 
2333  {
2334  if (FlagIsSet(kDecodeAllowGPU) && (dec != "ffmpeg"))
2335  {
2336  LOG(VB_GENERAL, LOG_WARNING, LOC + QString(
2337  "GPU/hardware decoder '%1' failed - forcing software decode")
2338  .arg(dec));
2339  }
2340  m_averrorCount = 0;
2341  dec = "ffmpeg";
2342  }
2343 
2344  bool foundgpudecoder = false;
2346  {
2347  // We need to set this so that MythyCodecContext can callback
2348  // to the player in use to check interop support.
2349  enc->opaque = static_cast<void*>(this);
2350  MythCodecID hwcodec = MythCodecContext::FindDecoder(dec, stream, &enc, &codec);
2351  if (hwcodec != kCodec_NONE)
2352  {
2353  // the context may have changed
2354  enc->opaque = static_cast<void*>(this);
2355  m_videoCodecId = hwcodec;
2356  foundgpudecoder = true;
2357  }
2358  }
2359 
2360  // default to mpeg2
2361  if (m_videoCodecId == kCodec_NONE)
2362  {
2363  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown video codec - defaulting to MPEG2");
2365  }
2366 
2367  // Use a PrivateDecoder if allowed in playerFlags AND matched
2368  // via the decoder name
2370  if (m_privateDec)
2371  thread_count = 1;
2372 
2373  // N.B. MediaCodec and NVDEC require frame timing
2374  m_useFrameTiming = false;
2378  {
2379  m_useFrameTiming = true;
2380  }
2381 
2383  thread_count = 1;
2384 
2385  if (HAVE_THREADS)
2386  {
2387  // All of our callbacks are thread safe. This should improve
2388  // concurrency with software decode
2389  enc->thread_safe_callbacks = 1;
2390 
2391  // Only use a single thread for hardware decoding. There is no
2392  // performance improvement with multithreaded hardware decode
2393  // and asynchronous callbacks create issues with decoders that
2394  // use AVHWFrameContext where we need to release video resources
2395  // before they are recreated
2396  if (!foundgpudecoder)
2397  {
2398  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Using %1 CPUs for decoding")
2399  .arg(HAVE_THREADS ? thread_count : 1));
2400  enc->thread_count = static_cast<int>(thread_count);
2401  }
2402  }
2403 
2404  InitVideoCodec(stream, enc, true);
2405 
2406  ScanATSCCaptionStreams(selTrack);
2408 
2409  LOG(VB_GENERAL, LOG_INFO, LOC +
2410  QString("Using %1 for video decoding").arg(GetCodecDecoderName()));
2411  {
2412  QMutexLocker locker(avcodeclock);
2413  m_mythCodecCtx->SetDecoderOptions(enc, codec);
2414  if (!OpenAVCodec(enc, codec))
2415  {
2416  scanerror = -1;
2417  break;
2418  }
2419  }
2420  break;
2421  }
2422  }
2423 
2424  if (m_ic && (static_cast<uint>(m_ic->bit_rate) > m_bitrate))
2425  m_bitrate = static_cast<uint>(m_ic->bit_rate);
2426 
2427  if (m_bitrate > 0)
2428  {
2429  m_bitrate = (m_bitrate + 999) / 1000;
2430  if (m_ringBuffer)
2432  }
2433 
2434  // update RingBuffer buffer size
2435  if (m_ringBuffer)
2436  {
2437  m_ringBuffer->SetBufferSizeFactors(unknownbitrate,
2438  m_ic && QString(m_ic->iformat->name).contains("matroska"));
2439  }
2440 
2442 
2443  // Select a new track at the next opportunity.
2444  ResetTracks();
2445 
2446  // We have to do this here to avoid the NVP getting stuck
2447  // waiting on audio.
2448  if (m_audio->HasAudioIn() && m_tracks[kTrackTypeAudio].empty())
2449  {
2450  m_audio->SetAudioParams(FORMAT_NONE, -1, -1, AV_CODEC_ID_NONE, -1, false);
2451  m_audio->ReinitAudio();
2452  if (m_ringBuffer && m_ringBuffer->IsDVD())
2453  m_audioIn = AudioInfo();
2454  }
2455 
2456  // if we don't have a video stream we still need to make sure some
2457  // video params are set properly
2458  if (m_selectedTrack[kTrackTypeVideo].m_av_stream_index == -1)
2459  {
2460  m_fps = 23.97F; // minimum likey display refresh rate
2461  // N.B. we know longer need a 'dummy' frame to render overlays into
2462  m_parent->SetVideoParams(0, 0, 23.97, 1.0F, false, 0);
2463  }
2464 
2465  if (m_parent->IsErrored())
2466  scanerror = -1;
2467 
2468  ScanDSMCCStreams();
2469 
2470  return scanerror;
2471 }
2472 
2473 bool AvFormatDecoder::OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
2474 {
2475  QMutexLocker locker(avcodeclock);
2476 
2477 #ifdef USING_MEDIACODEC
2478  if (QString("mediacodec") == codec->wrapper_name)
2479  av_jni_set_java_vm(QAndroidJniEnvironment::javaVM(), nullptr);
2480 #endif
2481  int ret = avcodec_open2(avctx, codec, nullptr);
2482  if (ret < 0)
2483  {
2484  char error[AV_ERROR_MAX_STRING_SIZE];
2485 
2486  av_make_error_string(error, sizeof(error), ret);
2487  LOG(VB_GENERAL, LOG_ERR, LOC +
2488  QString("Could not open codec 0x%1, id(%2) type(%3) "
2489  "ignoring. reason %4").arg((uint64_t)avctx,0,16)
2490  .arg(ff_codec_id_string(avctx->codec_id))
2491  .arg(ff_codec_type_string(avctx->codec_type))
2492  .arg(error));
2493  return false;
2494  }
2495 
2496  LOG(VB_GENERAL, LOG_INFO, LOC +
2497  QString("Opened codec 0x%1, id(%2) type(%3)")
2498  .arg((uint64_t)avctx,0,16)
2499  .arg(ff_codec_id_string(avctx->codec_id))
2500  .arg(ff_codec_type_string(avctx->codec_type)));
2501  return true;
2502 }
2503 
2505 {
2507 }
2508 
2509 bool AvFormatDecoder::DoRewindSeek(long long desiredFrame)
2510 {
2511  return DecoderBase::DoRewindSeek(desiredFrame);
2512 }
2513 
2514 void AvFormatDecoder::DoFastForwardSeek(long long desiredFrame, bool &needflush)
2515 {
2516  DecoderBase::DoFastForwardSeek(desiredFrame, needflush);
2517 }
2518 
2521 {
2522  for (const auto & si : m_tracks[kTrackTypeTeletextCaptions])
2523  {
2524  if (si.m_language_index == lang_idx)
2525  {
2526  return si.m_language;
2527  }
2528  }
2529 
2530  return iso639_str3_to_key("und");
2531 }
2533 int AvFormatDecoder::GetSubtitleLanguage(uint subtitle_index, uint stream_index)
2534 {
2535  (void)subtitle_index;
2536  AVDictionaryEntry *metatag =
2537  av_dict_get(m_ic->streams[stream_index]->metadata, "language", nullptr, 0);
2538  return metatag ? get_canonical_lang(metatag->value) :
2539  iso639_str3_to_key("und");
2540 }
2541 
2543 int AvFormatDecoder::GetCaptionLanguage(TrackType trackType, int service_num)
2544 {
2545  int ret = -1;
2546  for (uint i = 0; i < (uint) m_pmtTrackTypes.size(); i++)
2547  {
2548  if ((m_pmtTrackTypes[i] == trackType) &&
2549  (m_pmtTracks[i].m_stream_id == service_num))
2550  {
2551  ret = m_pmtTracks[i].m_language;
2552  if (!iso639_is_key_undefined(ret))
2553  return ret;
2554  }
2555  }
2556 
2557  for (uint i = 0; i < (uint) m_streamTrackTypes.size(); i++)
2558  {
2559  if ((m_streamTrackTypes[i] == trackType) &&
2560  (m_streamTracks[i].m_stream_id == service_num))
2561  {
2562  ret = m_streamTracks[i].m_language;
2563  if (!iso639_is_key_undefined(ret))
2564  return ret;
2565  }
2566  }
2567 
2568  return ret;
2569 }
2570 
2571 int AvFormatDecoder::GetAudioLanguage(uint audio_index, uint stream_index)
2572 {
2573  return GetSubtitleLanguage(audio_index, stream_index);
2574 }
2575 
2577 {
2579  AVStream *stream = m_ic->streams[stream_index];
2580 
2581  if (m_ic->cur_pmt_sect) // mpeg-ts
2582  {
2583  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
2584  switch (pmt.GetAudioType(stream_index))
2585  {
2586  case 0x01 :
2588  break;
2589  case 0x02 :
2591  break;
2592  case 0x03 :
2594  break;
2595  case 0x00 :
2596  default:
2598  }
2599  }
2600  else // all other containers
2601  {
2602  // We only support labelling/filtering of these two types for now
2603  if (stream->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
2605  else if (stream->disposition & AV_DISPOSITION_COMMENT)
2607  else if (stream->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
2609  else if (stream->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
2611  }
2612 
2613  return type;
2614 }
2615 
2629 {
2630  QMutexLocker locker(avcodeclock);
2631 
2632  // Find the position of the streaminfo in m_tracks[kTrackTypeAudio]
2633  auto current = m_tracks[kTrackTypeAudio].begin();
2634  for (; current != m_tracks[kTrackTypeAudio].end(); ++current)
2635  {
2636  if (current->m_av_stream_index == streamIndex)
2637  break;
2638  }
2639 
2640  if (current == m_tracks[kTrackTypeAudio].end())
2641  {
2642  LOG(VB_GENERAL, LOG_WARNING, LOC +
2643  QString("Invalid stream index passed to "
2644  "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
2645 
2646  return;
2647  }
2648 
2649  // Remove the extra substream or duplicate the current substream
2650  auto next = current + 1;
2651  if (current->m_av_substream_index == -1)
2652  {
2653  // Split stream in two (Language I + Language II)
2654  StreamInfo lang1 = *current;
2655  StreamInfo lang2 = *current;
2656  lang1.m_av_substream_index = 0;
2657  lang2.m_av_substream_index = 1;
2658  *current = lang1;
2659  m_tracks[kTrackTypeAudio].insert(next, lang2);
2660  return;
2661  }
2662 
2663  if ((next == m_tracks[kTrackTypeAudio].end()) ||
2664  (next->m_av_stream_index != streamIndex))
2665  {
2666  QString msg = QString(
2667  "Expected substream 1 (Language I) of stream %1\n\t\t\t"
2668  "following substream 0, found end of list or another stream.")
2669  .arg(streamIndex);
2670 
2671  LOG(VB_GENERAL, LOG_WARNING, LOC + msg);
2672 
2673  return;
2674  }
2675 
2676  // Remove extra stream info
2677  StreamInfo stream = *current;
2678  stream.m_av_substream_index = -1;
2679  *current = stream;
2680  m_tracks[kTrackTypeAudio].erase(next);
2681 }
2682 
2688 {
2689  if (!m_audio->HasAudioIn())
2690  return;
2691 
2692  QMutexLocker locker(avcodeclock);
2693  for (uint i = 0; i < m_ic->nb_streams;)
2694  {
2695  AVStream *st = m_ic->streams[i];
2696  AVCodecContext *avctx = gCodecMap->hasCodecContext(st);
2697  if (avctx && avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2698  {
2700  av_remove_stream(m_ic, st->id, 0);
2701  i--;
2702  }
2703  else
2704  i++;
2705  }
2706 }
2707 
2708 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
2709 {
2710  auto *decoder = static_cast<AvFormatDecoder*>(c->opaque);
2712  VideoFrameType* supported = decoder->GetPlayer()->DirectRenderFormats();
2713  bool found = false;
2714  while (*supported != FMT_NONE)
2715  {
2716  if (*supported == type)
2717  {
2718  found = true;
2719  break;
2720  }
2721  supported++;
2722  }
2723 
2724  if (!found)
2725  {
2726  decoder->m_directRendering = false;
2727  return avcodec_default_get_buffer2(c, pic, flags);
2728  }
2729 
2730  decoder->m_directRendering = true;
2731  VideoFrame *frame = decoder->GetPlayer()->GetNextVideoFrame();
2732  if (!frame)
2733  return -1;
2734 
2735  // We pre-allocate frames to certain alignments. If the coded size differs from
2736  // those alignments then re-allocate the frame. Changes in frame type (e.g.
2737  // YV12 to NV12) are always reallocated.
2738  int width = (frame->width + MYTH_WIDTH_ALIGNMENT - 1) & ~(MYTH_WIDTH_ALIGNMENT - 1);
2739  int height = (frame->height + MYTH_HEIGHT_ALIGNMENT - 1) & ~(MYTH_HEIGHT_ALIGNMENT - 1);
2740 
2741  if ((frame->codec != type) || (pic->width > width) || (pic->height > height))
2742  {
2743  if (!VideoBuffers::ReinitBuffer(frame, type, decoder->m_videoCodecId, pic->width, pic->height))
2744  return -1;
2745  // NB the video frame may now have a new size which is currenly not an issue
2746  // as the underlying size has already been passed through to the player.
2747  // But may cause issues down the line. We should add coded_width/height to
2748  // VideoFrame as well to ensure consistentency through the rendering
2749  // pipeline.
2750  // NB remember to compare coded width/height - not 'true' values - otherwsie
2751  // we continually re-allocate the frame.
2752  //frame->width = c->width;
2753  //frame->height = c->height;
2754  }
2755 
2756  frame->colorshifted = 0;
2757  uint max = planes(frame->codec);
2758  for (uint i = 0; i < 3; i++)
2759  {
2760  pic->data[i] = (i < max) ? (frame->buf + frame->offsets[i]) : nullptr;
2761  pic->linesize[i] = frame->pitches[i];
2762  }
2763 
2764  pic->opaque = frame;
2765  pic->reordered_opaque = c->reordered_opaque;
2766 
2767  // Set release method
2768  AVBufferRef *buffer = av_buffer_create(reinterpret_cast<uint8_t*>(frame), 0,
2769  [](void* Opaque, uint8_t* Data)
2770  {
2771  auto *avfd = static_cast<AvFormatDecoder*>(Opaque);
2772  auto *vf = reinterpret_cast<VideoFrame*>(Data);
2773  if (avfd && avfd->GetPlayer())
2774  avfd->GetPlayer()->DeLimboFrame(vf);
2775  }
2776  , decoder, 0);
2777  pic->buf[0] = buffer;
2778 
2779  return 0;
2780 }
2781 
2782 #ifdef USING_DXVA2
2783 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
2784 {
2785  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
2786  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
2787 
2788  for (int i = 0; i < 4; i++)
2789  {
2790  pic->data[i] = nullptr;
2791  pic->linesize[i] = 0;
2792  }
2793  pic->opaque = frame;
2794  frame->pix_fmt = c->pix_fmt;
2795  pic->reordered_opaque = c->reordered_opaque;
2796  pic->data[0] = (uint8_t*)frame->buf;
2797  pic->data[3] = (uint8_t*)frame->buf;
2798 
2799  // Set release method
2800  AVBufferRef *buffer =
2801  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
2802  pic->buf[0] = buffer;
2803 
2804  return 0;
2805 }
2806 #endif
2807 
2808 void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
2809 {
2810  if (!buf_size)
2811  return;
2812 
2813  // closed caption data
2814  //cc_data() {
2815  // reserved 1 0.0 1
2816  // process_cc_data_flag 1 0.1 bslbf
2817  bool process_cc_data = (buf[0] & 0x40) != 0;
2818  if (!process_cc_data)
2819  return; // early exit if process_cc_data_flag false
2820 
2821  // additional_data_flag 1 0.2 bslbf
2822  //bool additional_data = buf[0] & 0x20;
2823  // cc_count 5 0.3 uimsbf
2824  uint cc_count = buf[0] & 0x1f;
2825  // em_data 8 1.0
2826 
2827  if (buf_size < 2+(3*cc_count))
2828  return;
2829 
2830  DecodeCCx08(buf+2, cc_count*3, scte);
2831 }
2832 
2833 void AvFormatDecoder::DecodeCCx08(const uint8_t *buf, uint buf_size, bool scte)
2834 {
2835  if (buf_size < 3)
2836  return;
2837 
2838  bool had_608 = false;
2839  bool had_708 = false;
2840  for (uint cur = 0; cur + 3 < buf_size; cur += 3)
2841  {
2842  uint cc_code = buf[cur];
2843  bool cc_valid = (cc_code & 0x04) != 0U;
2844 
2845  uint data1 = buf[cur+1];
2846  uint data2 = buf[cur+2];
2847  uint data = (data2 << 8) | data1;
2848  uint cc_type = cc_code & 0x03;
2849 
2850  if (!cc_valid)
2851  {
2852  if (cc_type >= 0x2)
2854  continue;
2855  }
2856 
2857  if (scte || cc_type <= 0x1) // EIA-608 field-1/2
2858  {
2859  uint field = 0;
2860  if (cc_type == 0x2)
2861  {
2862  // SCTE repeated field
2863  field = (m_lastScteField == 0 ? 1 : 0);
2864  m_invertScteField = (m_invertScteField == 0 ? 1 : 0);
2865  }
2866  else
2867  {
2868  field = cc_type ^ m_invertScteField;
2869  }
2870 
2872  {
2873  // in film mode, we may start at the wrong field;
2874  // correct if XDS start/cont/end code is detected
2875  // (must be field 2)
2876  if (scte && field == 0 &&
2877  (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
2878  {
2879  if (cc_type == 1)
2880  m_invertScteField = 0;
2881  field = 1;
2882 
2883  // flush decoder
2884  m_ccd608->FormatCC(0, -1, -1);
2885  }
2886 
2887  had_608 = true;
2888  m_ccd608->FormatCCField(m_lastCcPtsu / 1000, field, data);
2889 
2890  m_lastScteField = field;
2891  }
2892  }
2893  else
2894  {
2895  had_708 = true;
2896  m_ccd708->decode_cc_data(cc_type, data1, data2);
2897  }
2898  }
2899  UpdateCaptionTracksFromStreams(had_608, had_708);
2900 }
2901 
2903  bool check_608, bool check_708)
2904 {
2905  bool need_change_608 = false;
2906  bool seen_608[4];
2907  if (check_608)
2908  {
2909  m_ccd608->GetServices(15/*seconds*/, seen_608);
2910  for (uint i = 0; i < 4; i++)
2911  {
2912  need_change_608 |= (seen_608[i] && !m_ccX08InTracks[i]) ||
2913  (!seen_608[i] && m_ccX08InTracks[i] && !m_ccX08InPmt[i]);
2914  }
2915  }
2916 
2917  bool need_change_708 = false;
2918  bool seen_708[64];
2919  if (check_708 || need_change_608)
2920  {
2921  m_ccd708->services(15/*seconds*/, seen_708);
2922  for (uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
2923  {
2924  need_change_708 |= (seen_708[i] && !m_ccX08InTracks[i+4]) ||
2925  (!seen_708[i] && m_ccX08InTracks[i+4] && !m_ccX08InPmt[i+4]);
2926  }
2927  if (need_change_708 && !check_608)
2928  m_ccd608->GetServices(15/*seconds*/, seen_608);
2929  }
2930 
2931  if (!need_change_608 && !need_change_708)
2932  return;
2933 
2935 
2936  m_streamTracks.clear();
2937  m_streamTrackTypes.clear();
2939  int lang = iso639_str3_to_key("und");
2940  for (uint i = 1; i < 64; i++)
2941  {
2942  if (seen_708[i] && !m_ccX08InPmt[i+4])
2943  {
2944  StreamInfo si(av_index, lang, 0/*lang_idx*/,
2945  i, 0, false/*easy*/, true/*wide*/);
2946  m_streamTracks.push_back(si);
2948  }
2949  }
2950  for (uint i = 0; i < 4; i++)
2951  {
2952  if (seen_608[i] && !m_ccX08InPmt[i])
2953  {
2954  if (0==i)
2956  else if (2==i)
2958  else
2959  lang = iso639_str3_to_key("und");
2960 
2961  StreamInfo si(av_index, lang, 0/*lang_idx*/,
2962  i+1, 0, false/*easy*/, false/*wide*/);
2963  m_streamTracks.push_back(si);
2965  }
2966  }
2968 }
2969 
2971  AVPacket *pkt, bool can_reliably_parse_keyframes)
2972 {
2973  if (m_prevGopPos != 0 && m_keyframeDist != 1)
2974  {
2975  int tempKeyFrameDist = m_framesRead - 1 - m_prevGopPos;
2976  bool reset_kfd = false;
2977 
2978  if (!m_gopSet || m_livetv) // gopset: we've seen 2 keyframes
2979  {
2980  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2981  "gopset not set, syncing positionMap");
2982  SyncPositionMap();
2983  if (tempKeyFrameDist > 0 && !m_livetv)
2984  {
2985  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2986  QString("Initial key frame distance: %1.")
2987  .arg(m_keyframeDist));
2988  m_gopSet = true;
2989  reset_kfd = true;
2990  }
2991  }
2992  else if (m_keyframeDist != tempKeyFrameDist && tempKeyFrameDist > 0)
2993  {
2994  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2995  QString("Key frame distance changed from %1 to %2.")
2996  .arg(m_keyframeDist).arg(tempKeyFrameDist));
2997  reset_kfd = true;
2998  }
2999 
3000  if (reset_kfd)
3001  {
3002  m_keyframeDist = tempKeyFrameDist;
3004 
3006 
3007 #if 0
3008  // also reset length
3009  QMutexLocker locker(&m_positionMapLock);
3010  if (!m_positionMap.empty())
3011  {
3012  long long index = m_positionMap.back().index;
3013  long long totframes = index * m_keyframeDist;
3014  uint length = (uint)((totframes * 1.0F) / m_fps);
3015  m_parent->SetFileLength(length, totframes);
3016  }
3017 #endif
3018  }
3019  }
3020 
3022 
3023  if (can_reliably_parse_keyframes &&
3025  {
3026  long long last_frame = 0;
3027  {
3028  QMutexLocker locker(&m_positionMapLock);
3029  if (!m_positionMap.empty())
3030  last_frame = m_positionMap.back().index;
3031  }
3032 
3033 #if 0
3034  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
3035  QString("framesRead: %1 last_frame: %2 keyframedist: %3")
3036  .arg(m_framesRead) .arg(last_frame) .arg(m_keyframeDist));
3037 #endif
3038 
3039  // if we don't have an entry, fill it in with what we've just parsed
3040  if (m_framesRead > last_frame && m_keyframeDist > 0)
3041  {
3042  long long startpos = pkt->pos;
3043 
3044  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3045  QString("positionMap[ %1 ] == %2.")
3046  .arg(m_framesRead).arg(startpos));
3047 
3048  PosMapEntry entry = {m_framesRead, m_framesRead, startpos};
3049 
3050  QMutexLocker locker(&m_positionMapLock);
3051  // Create a dummy positionmap entry for frame 0 so that
3052  // seeking will work properly. (See
3053  // DecoderBase::FindPosition() which subtracts
3054  // DecoderBase::indexOffset from each frame number.)
3055  if (m_positionMap.empty())
3056  {
3057  PosMapEntry dur = {0, 0, 0};
3058  m_positionMap.push_back(dur);
3059  }
3060  m_positionMap.push_back(entry);
3062  {
3064  llround(m_totalDuration.num * 1000.0 / m_totalDuration.den);
3066  }
3067  }
3068 
3069 #if 0
3070  // If we are > 150 frames in and saw no positionmap at all, reset
3071  // length based on the actual bitrate seen so far
3073  {
3074  m_bitrate = (int)((pkt->pos * 8 * m_fps) / (m_framesRead - 1));
3075  float bytespersec = (float)m_bitrate / 8;
3076  float secs = m_ringBuffer->GetRealFileSize() * 1.0F / bytespersec;
3077  m_parent->SetFileLength((int)(secs), (int)(secs * m_fps));
3078  }
3079 #endif
3080  }
3081 }
3082 
3083 #define SEQ_START 0x000001b3
3084 #define GOP_START 0x000001b8
3085 #define PICTURE_START 0x00000100
3086 #define SLICE_MIN 0x00000101
3087 #define SLICE_MAX 0x000001af
3088 #define SEQ_END_CODE 0x000001b7
3089 
3090 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
3091 {
3092  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3093  const uint8_t *bufptr = pkt->data;
3094  const uint8_t *bufend = pkt->data + pkt->size;
3095 
3096  while (bufptr < bufend)
3097  {
3098  bufptr = avpriv_find_start_code(bufptr, bufend, &m_startCodeState);
3099 
3100  float aspect_override = -1.0F;
3101  if (m_ringBuffer->IsDVD())
3102  aspect_override = m_ringBuffer->DVD()->GetAspectOverride();
3103 
3105  continue;
3106 
3107  if (SEQ_START == m_startCodeState)
3108  {
3109  if (bufptr + 11 >= pkt->data + pkt->size)
3110  continue; // not enough valid data...
3111  const auto *seq = reinterpret_cast<const SequenceHeader*>(bufptr);
3112 
3113  int width = static_cast<int>(seq->width()) >> context->lowres;
3114  int height = static_cast<int>(seq->height()) >> context->lowres;
3115  float aspect = seq->aspect(context->codec_id == AV_CODEC_ID_MPEG1VIDEO);
3116  if (stream->sample_aspect_ratio.num)
3117  aspect = static_cast<float>(av_q2d(stream->sample_aspect_ratio) *
3118  width / height);
3119  float seqFPS = seq->fps();
3120 
3121  bool changed = (width != m_currentWidth );
3122  changed |= (height != m_currentHeight);
3123  changed |= (seqFPS > static_cast<float>(m_fps)+0.01F) ||
3124  (seqFPS < static_cast<float>(m_fps)-0.01F);
3125 
3126  // some hardware decoders (e.g. VAAPI MPEG2) will reset when the aspect
3127  // ratio changes
3128  bool forceaspectchange = !qFuzzyCompare(m_currentAspect + 10.0F, aspect + 10.0F) &&
3130 
3131  m_currentAspect = aspect;
3132 
3133  // N.B. this will break aspect ratio change detection above
3134  if (aspect_override > 0.0F)
3135  m_currentAspect = aspect_override;
3136 
3137  if (changed || forceaspectchange)
3138  {
3139  if (m_privateDec)
3140  m_privateDec->Reset();
3141 
3142  // N.B. We now set the default scan to kScan_Ignore as interlaced detection based on frame
3143  // size and rate is extremely error prone and FFmpeg gets it right far more often.
3144  // As for H.264, if a decoder deinterlacer is in operation - the stream must be progressive
3145  bool doublerate = false;
3146  bool decoderdeint = m_mythCodecCtx && m_mythCodecCtx->IsDeinterlacing(doublerate, true);
3147  m_parent->SetVideoParams(width, height, static_cast<double>(seqFPS), m_currentAspect,
3148  forceaspectchange, 2,
3149  decoderdeint ? kScan_Progressive : kScan_Ignore);
3150 
3151  m_currentWidth = width;
3152  m_currentHeight = height;
3153  m_fps = seqFPS;
3154 
3155  m_gopSet = false;
3156  m_prevGopPos = 0;
3158  m_firstVPtsInuse = true;
3159  m_faultyPts = m_faultyDts = 0;
3162  m_ptsDetected = false;
3163  m_reorderedPtsDetected = false;
3164 
3165  // fps debugging info
3166  float avFPS = GetVideoFrameRate(stream, context);
3167  if ((seqFPS > avFPS+0.01F) || (seqFPS < avFPS-0.01F))
3168  {
3169  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("avFPS(%1) != seqFPS(%2)")
3170  .arg(static_cast<double>(avFPS)).arg(static_cast<double>(seqFPS)));
3171  }
3172  }
3173 
3174  m_seqCount++;
3175 
3176  if (!m_seenGop && m_seqCount > 1)
3177  {
3178  HandleGopStart(pkt, true);
3179  pkt->flags |= AV_PKT_FLAG_KEY;
3180  }
3181  }
3182  else if (GOP_START == m_startCodeState)
3183  {
3184  HandleGopStart(pkt, true);
3185  m_seenGop = true;
3186  pkt->flags |= AV_PKT_FLAG_KEY;
3187  }
3188  }
3189 }
3190 
3191 // Returns the number of frame starts identified in the packet.
3192 int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
3193 {
3194  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3195  const uint8_t *buf = pkt->data;
3196  const uint8_t *buf_end = pkt->data + pkt->size;
3197  int num_frames = 0;
3198 
3199  // The parser only understands Annex B/bytestream format - so check for avCC
3200  // format (starts with 0x01) and rely on FFmpeg keyframe detection
3201  if (context->extradata && (context->extradata_size >= 7) && (context->extradata[0] == 0x01))
3202  {
3203  if (pkt->flags & AV_PKT_FLAG_KEY)
3204  HandleGopStart(pkt, false);
3205  return 1;
3206  }
3207 
3208  while (buf < buf_end)
3209  {
3210  buf += m_h264Parser->addBytes(buf, static_cast<unsigned int>(buf_end - buf), 0);
3211 
3212  if (m_h264Parser->stateChanged())
3213  {
3215  {
3216  if (m_h264Parser->onFrameStart())
3217  ++num_frames;
3218 
3219  if (!m_h264Parser->onKeyFrameStart())
3220  continue;
3221  }
3222  else
3223  {
3224  continue;
3225  }
3226  }
3227  else
3228  {
3229  continue;
3230  }
3231 
3232  float aspect = get_aspect(*m_h264Parser);
3233  int width = static_cast<int>(m_h264Parser->pictureWidthCropped());
3234  int height = static_cast<int>(m_h264Parser->pictureHeightCropped());
3235  double seqFPS = m_h264Parser->frameRate();
3236 
3237  bool res_changed = ((width != m_currentWidth) || (height != m_currentHeight));
3238  bool fps_changed = (seqFPS > 0.0) && ((seqFPS > static_cast<double>(m_fps) + 0.01) ||
3239  (seqFPS < static_cast<double>(m_fps) - 0.01));
3240  bool forcechange = !qFuzzyCompare(aspect + 10.0F, m_currentAspect) &&
3242  m_currentAspect = aspect;
3243 
3244  if (fps_changed || res_changed || forcechange)
3245  {
3246  if (m_privateDec)
3247  m_privateDec->Reset();
3248 
3249  // N.B. we now set the default scan to kScan_Ignore as interlaced detection based on frame
3250  // size and rate is extremely error prone and FFmpeg gets it right far more often.
3251  // N.B. if a decoder deinterlacer is in use - the stream must be progressive
3252  bool doublerate = false;
3253  bool decoderdeint = m_mythCodecCtx->IsDeinterlacing(doublerate, true);
3254  m_parent->SetVideoParams(width, height, seqFPS, m_currentAspect, forcechange,
3255  static_cast<int>(m_h264Parser->getRefFrames()),
3256  decoderdeint ? kScan_Progressive : kScan_Ignore);
3257 
3258  m_currentWidth = width;
3259  m_currentHeight = height;
3260 
3261  if (seqFPS > 0.0)
3262  m_fps = static_cast<float>(seqFPS);
3263 
3264  m_gopSet = false;
3265  m_prevGopPos = 0;
3267  m_firstVPtsInuse = true;
3268  m_faultyPts = m_faultyDts = 0;
3271  m_ptsDetected = false;
3272  m_reorderedPtsDetected = false;
3273 
3274  // fps debugging info
3275  auto avFPS = static_cast<double>(GetVideoFrameRate(stream, context));
3276  if ((seqFPS > avFPS + 0.01) || (seqFPS < avFPS - 0.01))
3277  {
3278  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3279  QString("avFPS(%1) != seqFPS(%2)").arg(avFPS).arg(seqFPS));
3280  }
3281  }
3282 
3283  HandleGopStart(pkt, true);
3284  pkt->flags |= AV_PKT_FLAG_KEY;
3285  }
3286 
3287  return num_frames;
3288 }
3289 
3290 bool AvFormatDecoder::PreProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3291 {
3292  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3293  int num_frames = 1;
3294 
3295  if (CODEC_IS_FFMPEG_MPEG(context->codec_id))
3296  {
3297  MpegPreProcessPkt(curstream, pkt);
3298  }
3299  else if (CODEC_IS_H264(context->codec_id))
3300  {
3301  num_frames = H264PreProcessPkt(curstream, pkt);
3302  }
3303  else
3304  {
3305  if (pkt->flags & AV_PKT_FLAG_KEY)
3306  {
3307  HandleGopStart(pkt, false);
3308  m_seenGop = true;
3309  }
3310  else
3311  {
3312  m_seqCount++;
3313  if (!m_seenGop && m_seqCount > 1)
3314  {
3315  HandleGopStart(pkt, false);
3316  }
3317  }
3318  }
3319 
3320  if (m_framesRead == 0 && !m_justAfterChange &&
3321  !(pkt->flags & AV_PKT_FLAG_KEY))
3322  {
3323  av_packet_unref(pkt);
3324  return false;
3325  }
3326 
3327  m_framesRead += num_frames;
3328 
3330  {
3331  // The ffmpeg libraries represent a frame interval of a
3332  // 59.94fps video as 1501/90000 seconds, when it should
3333  // actually be 1501.5/90000 seconds.
3334  AVRational pkt_dur = AVRationalInit(pkt->duration);
3335  pkt_dur = av_mul_q(pkt_dur, curstream->time_base);
3336  if (pkt_dur.num == 1501 && pkt_dur.den == 90000)
3337  pkt_dur = AVRationalInit(1001, 60000); // 1501.5/90000
3338  m_totalDuration = av_add_q(m_totalDuration, pkt_dur);
3339  }
3340 
3341  m_justAfterChange = false;
3342 
3343  if (m_exitAfterDecoded)
3344  m_gotVideoFrame = true;
3345 
3346  return true;
3347 }
3348 
3349 bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt, bool &Retry)
3350 {
3351  int ret = 0;
3352  int gotpicture = 0;
3353  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3354  MythAVFrame mpa_pic;
3355  if (!mpa_pic)
3356  return false;
3357  mpa_pic->reordered_opaque = AV_NOPTS_VALUE;
3358 
3359  if (pkt->pts != AV_NOPTS_VALUE)
3360  m_ptsDetected = true;
3361 
3362  bool sentPacket = false;
3363  int ret2 = 0;
3364  avcodeclock->lock();
3365  if (m_privateDec)
3366  {
3367  if (QString(m_ic->iformat->name).contains("avi") || !m_ptsDetected)
3368  pkt->pts = pkt->dts;
3369  // TODO disallow private decoders for dvd playback
3370  // N.B. we do not reparse the frame as it breaks playback for
3371  // everything but libmpeg2
3372  ret = m_privateDec->GetFrame(curstream, mpa_pic, &gotpicture, pkt);
3373  sentPacket = true;
3374  }
3375  else
3376  {
3377  if (!m_useFrameTiming)
3378  context->reordered_opaque = pkt->pts;
3379 
3380  // SUGGESTION
3381  // Now that avcodec_decode_video2 is deprecated and replaced
3382  // by 2 calls (receive frame and send packet), this could be optimized
3383  // into separate routines or separate threads.
3384  // Also now that it always consumes a whole buffer some code
3385  // in the caller may be able to be optimized.
3386 
3387  // FilteredReceiveFrame will call avcodec_receive_frame and
3388  // apply any codec-dependent filtering
3389  ret = m_mythCodecCtx->FilteredReceiveFrame(context, mpa_pic);
3390 
3391  if (ret == 0)
3392  gotpicture = 1;
3393  else
3394  gotpicture = 0;
3395  if (ret == AVERROR(EAGAIN))
3396  ret = 0;
3397  // If we got a picture do not send the packet until we have
3398  // all available pictures
3399  if (ret==0 && !gotpicture)
3400  {
3401  ret2 = avcodec_send_packet(context, pkt);
3402  if (ret2 == AVERROR(EAGAIN))
3403  {
3404  Retry = true;
3405  ret2 = 0;
3406  }
3407  else
3408  {
3409  sentPacket = true;
3410  }
3411  }
3412  }
3413  avcodeclock->unlock();
3414 
3415  if (ret < 0 || ret2 < 0)
3416  {
3417  char error[AV_ERROR_MAX_STRING_SIZE];
3418  if (ret < 0)
3419  {
3420  LOG(VB_GENERAL, LOG_ERR, LOC +
3421  QString("video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
3422  .arg(av_make_error_string(error, sizeof(error), ret))
3423  .arg(ret).arg(gotpicture));
3424  }
3425 
3426  if (ret2 < 0)
3427  {
3428  LOG(VB_GENERAL, LOG_ERR, LOC +
3429  QString("video avcodec_send_packet error: %1 (%2) gotpicture:%3")
3430  .arg(av_make_error_string(error, sizeof(error), ret2))
3431  .arg(ret2).arg(gotpicture));
3432  }
3433 
3435  {
3436  // If erroring on GPU assist, try switching to software decode
3438  m_parent->SetErrored(QObject::tr("Video Decode Error"));
3439  else
3440  m_streamsChanged = true;
3441  }
3442 
3443  if (m_mythCodecCtx->DecoderNeedsReset(context))
3444  {
3445  LOG(VB_GENERAL, LOG_INFO, LOC + "Decoder needs reset");
3447  }
3448 
3449  if (ret == AVERROR_EXTERNAL || ret2 == AVERROR_EXTERNAL)
3450  {
3451  LOG(VB_PLAYBACK, LOG_INFO, LOC + "FFmpeg external library error - assuming streams changed");
3452  m_streamsChanged = true;
3453  }
3454 
3455  return false;
3456  }
3457 
3458  // averror_count counts sequential errors, so if you have a successful
3459  // packet then reset it
3460  m_averrorCount = 0;
3461  if (gotpicture)
3462  {
3463  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3464  QString("video timecodes packet-pts:%1 frame-pts:%2 packet-dts: %3 frame-dts:%4")
3465  .arg(pkt->pts).arg(mpa_pic->pts).arg(pkt->pts)
3466  .arg(mpa_pic->pkt_dts));
3467 
3468  if (!m_useFrameTiming)
3469  {
3470  int64_t pts = 0;
3471 
3472  // Detect faulty video timestamps using logic from ffplay.
3473  if (pkt->dts != AV_NOPTS_VALUE)
3474  {
3475  m_faultyDts += (pkt->dts <= m_lastDtsForFaultDetection);
3476  m_lastDtsForFaultDetection = pkt->dts;
3477  }
3478  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3479  {
3480  m_faultyPts += (mpa_pic->reordered_opaque <= m_lastPtsForFaultDetection);
3481  m_lastPtsForFaultDetection = mpa_pic->reordered_opaque;
3482  m_reorderedPtsDetected = true;
3483  }
3484 
3485  // Explicity use DTS for DVD since they should always be valid for every
3486  // frame and fixups aren't enabled for DVD.
3487  // Select reordered_opaque (PTS) timestamps if they are less faulty or the
3488  // the DTS timestamp is missing. Also use fixups for missing PTS instead of
3489  // DTS to avoid oscillating between PTS and DTS. Only select DTS if PTS is
3490  // more faulty or never detected.
3492  {
3493  if (pkt->dts != AV_NOPTS_VALUE)
3494  pts = pkt->dts;
3495  m_ptsSelected = false;
3496  }
3497  else if (m_privateDec && m_privateDec->NeedsReorderedPTS() &&
3498  mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3499  {
3500  pts = mpa_pic->reordered_opaque;
3501  m_ptsSelected = true;
3502  }
3504  {
3505  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3506  pts = mpa_pic->reordered_opaque;
3507  m_ptsSelected = true;
3508  }
3509  else if (pkt->dts != AV_NOPTS_VALUE)
3510  {
3511  pts = pkt->dts;
3512  m_ptsSelected = false;
3513  }
3514 
3515  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG, LOC +
3516  QString("video packet timestamps reordered %1 pts %2 dts %3 (%4)")
3517  .arg(mpa_pic->reordered_opaque).arg(pkt->pts).arg(pkt->dts)
3518  .arg((m_forceDtsTimestamps) ? "dts forced" :
3519  (m_ptsSelected) ? "reordered" : "dts"));
3520 
3521  mpa_pic->reordered_opaque = pts;
3522  }
3523  ProcessVideoFrame(curstream, mpa_pic);
3524  }
3525 
3526  if (!sentPacket)
3527  {
3528  // MythTV logic expects that only one frame is processed
3529  // Save the packet for later and return.
3530  auto *newPkt = new AVPacket;
3531  memset(newPkt, 0, sizeof(AVPacket));
3532  av_init_packet(newPkt);
3533  av_packet_ref(newPkt, pkt);
3534  m_storedPackets.prepend(newPkt);
3535  }
3536  return true;
3537 }
3538 
3539 bool AvFormatDecoder::ProcessVideoFrame(AVStream *Stream, AVFrame *AvFrame)
3540 {
3541 
3542  AVCodecContext *context = gCodecMap->getCodecContext(Stream);
3543 
3544  // We need to mediate between ATSC and SCTE data when both are present. If
3545  // both are present, we generally want to prefer ATSC. However, there may
3546  // be large sections of the recording where ATSC is used and other sections
3547  // where SCTE is used. In that case, we want to allow a natural transition
3548  // from ATSC back to SCTE. We do this by allowing 10 consecutive SCTE
3549  // frames, without an intervening ATSC frame, to cause a switch back to
3550  // considering SCTE frames. The number 10 is somewhat arbitrarily chosen.
3551 
3552  uint cc_len = static_cast<uint>(max(AvFrame->scte_cc_len,0));
3553  uint8_t *cc_buf = AvFrame->scte_cc_buf;
3554  bool scte = true;
3555 
3556  // If we saw SCTE, then decrement a nonzero ignore_scte count.
3557  if (cc_len > 0 && m_ignoreScte)
3558  --m_ignoreScte;
3559 
3560  // If both ATSC and SCTE caption data are available, prefer ATSC
3561  if ((AvFrame->atsc_cc_len > 0) || m_ignoreScte)
3562  {
3563  cc_len = static_cast<uint>(max(AvFrame->atsc_cc_len, 0));
3564  cc_buf = AvFrame->atsc_cc_buf;
3565  scte = false;
3566  // If we explicitly saw ATSC, then reset ignore_scte count.
3567  if (cc_len > 0)
3568  m_ignoreScte = 10;
3569  }
3570 
3571  // Decode CEA-608 and CEA-708 captions
3572  for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
3573  DecodeDTVCC(cc_buf + i, cc_len - i, scte);
3574 
3575  if (cc_len == 0) {
3576  // look for A53 captions
3577  AVFrameSideData *side_data = av_frame_get_side_data(AvFrame, AV_FRAME_DATA_A53_CC);
3578  if (side_data && (side_data->size > 0)) {
3579  DecodeCCx08(side_data->data, static_cast<uint>(side_data->size), false);
3580  }
3581  }
3582 
3583  auto *frame = static_cast<VideoFrame*>(AvFrame->opaque);
3584  if (frame)
3585  frame->directrendering = m_directRendering;
3586 
3588  {
3589  // Do nothing, we just want the pts, captions, subtites, etc.
3590  // So we can release the unconverted blank video frame to the
3591  // display queue.
3592  if (frame)
3593  frame->directrendering = 0;
3594  }
3595  else if (!m_directRendering)
3596  {
3597  VideoFrame *oldframe = frame;
3598  frame = m_parent->GetNextVideoFrame();
3599  frame->directrendering = 0;
3600 
3601  if (!m_mythCodecCtx->RetrieveFrame(context, frame, AvFrame))
3602  {
3603  AVFrame tmppicture;
3604  av_image_fill_arrays(tmppicture.data, tmppicture.linesize,
3605  frame->buf, AV_PIX_FMT_YUV420P, AvFrame->width,
3606  AvFrame->height, IMAGE_ALIGN);
3607  tmppicture.data[0] = frame->buf + frame->offsets[0];
3608  tmppicture.data[1] = frame->buf + frame->offsets[1];
3609  tmppicture.data[2] = frame->buf + frame->offsets[2];
3610  tmppicture.linesize[0] = frame->pitches[0];
3611  tmppicture.linesize[1] = frame->pitches[1];
3612  tmppicture.linesize[2] = frame->pitches[2];
3613 
3614  QSize dim = get_video_dim(*context);
3615  m_swsCtx = sws_getCachedContext(m_swsCtx, AvFrame->width,
3616  AvFrame->height, static_cast<AVPixelFormat>(AvFrame->format),
3617  AvFrame->width, AvFrame->height,
3618  AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
3619  nullptr, nullptr, nullptr);
3620  if (!m_swsCtx)
3621  {
3622  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate sws context");
3623  return false;
3624  }
3625  sws_scale(m_swsCtx, AvFrame->data, AvFrame->linesize, 0, dim.height(),
3626  tmppicture.data, tmppicture.linesize);
3627  }
3628 
3629  // Discard any old VideoFrames
3630  if (oldframe)
3631  {
3632  // Set the frame flags, but then discard it
3633  // since we are not using it for display.
3634  oldframe->pause_frame = 0;
3635  oldframe->interlaced_frame = AvFrame->interlaced_frame;
3636  oldframe->top_field_first = AvFrame->top_field_first;
3637  oldframe->interlaced_reversed = 0;
3638  oldframe->colorspace = AvFrame->colorspace;
3639  oldframe->colorrange = AvFrame->color_range;
3640  oldframe->colorprimaries = AvFrame->color_primaries;
3641  oldframe->colortransfer = AvFrame->color_trc;
3642  oldframe->chromalocation = AvFrame->chroma_location;
3643  oldframe->frameNumber = m_framesPlayed;
3644  oldframe->frameCounter = m_frameCounter++;
3645  oldframe->aspect = m_currentAspect;
3646  oldframe->deinterlace_inuse = DEINT_NONE;
3647  oldframe->deinterlace_inuse2x = 0;
3648  oldframe->decoder_deinterlaced = 0;
3649  oldframe->rotation = m_videoRotation;
3650  m_parent->DiscardVideoFrame(oldframe);
3651  }
3652  }
3653 
3654  if (!frame)
3655  {
3656  LOG(VB_GENERAL, LOG_ERR, LOC + "NULL videoframe - direct rendering not "
3657  "correctly initialized.");
3658  return false;
3659  }
3660 
3661  long long pts = 0;
3662  if (m_useFrameTiming)
3663  {
3664  pts = AvFrame->pts;
3665  if (pts == AV_NOPTS_VALUE)
3666  pts = AvFrame->pkt_dts;
3667  if (pts == AV_NOPTS_VALUE)
3668  pts = AvFrame->reordered_opaque;
3669  if (pts == AV_NOPTS_VALUE)
3670  {
3671  LOG(VB_GENERAL, LOG_ERR, LOC + "No PTS found - unable to process video.");
3672  return false;
3673  }
3674  pts = static_cast<long long>(av_q2d(Stream->time_base) * pts * 1000);
3675  }
3676  else
3677  pts = static_cast<long long>(av_q2d(Stream->time_base) * AvFrame->reordered_opaque * 1000);
3678 
3679  long long temppts = pts;
3680  // Validate the video pts against the last pts. If it's
3681  // a little bit smaller, equal or missing, compute
3682  // it from the last. Otherwise assume a wraparound.
3683  if (!m_ringBuffer->IsDVD() &&
3684  temppts <= m_lastVPts &&
3685  (temppts + (1000 / m_fps) > m_lastVPts || temppts <= 0))
3686  {
3687  temppts = m_lastVPts;
3688  temppts += static_cast<long long>(1000 / m_fps);
3689  // MPEG2/H264 frames can be repeated, update pts accordingly
3690  temppts += static_cast<long long>(AvFrame->repeat_pict * 500 / m_fps);
3691  }
3692 
3693  // Calculate actual fps from the pts values.
3694  long long ptsdiff = temppts - m_lastVPts;
3695  double calcfps = 1000.0 / ptsdiff;
3696  if (calcfps < 121.0 && calcfps > 3.0)
3697  {
3698  // If fps has doubled due to frame-doubling deinterlace
3699  // Set fps to double value.
3700  double fpschange = calcfps / static_cast<double>(m_fps);
3701  int prior = m_fpsMultiplier;
3702  if (fpschange > 1.9 && fpschange < 2.1)
3703  m_fpsMultiplier = 2;
3704  if (fpschange > 0.9 && fpschange < 1.1)
3705  m_fpsMultiplier = 1;
3706  if (m_fpsMultiplier != prior)
3707  m_parent->SetFrameRate(static_cast<double>(m_fps));
3708  }
3709 
3710  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3711  QString("video timecode %1 %2 %3 %4%5")
3712  .arg(m_useFrameTiming ? AvFrame->pts : AvFrame->reordered_opaque).arg(pts)
3713  .arg(temppts).arg(m_lastVPts)
3714  .arg((pts != temppts) ? " fixup" : ""));
3715 
3716  if (frame)
3717  {
3718  frame->interlaced_frame = AvFrame->interlaced_frame;
3719  frame->top_field_first = AvFrame->top_field_first;
3720  frame->interlaced_reversed = 0;
3721  frame->repeat_pict = AvFrame->repeat_pict;
3722  frame->disp_timecode = NormalizeVideoTimecode(Stream, temppts);
3723  frame->frameNumber = m_framesPlayed;
3724  frame->frameCounter = m_frameCounter++;
3725  frame->aspect = m_currentAspect;
3726  frame->dummy = 0;
3727  frame->pause_frame = 0;
3728  frame->colorspace = AvFrame->colorspace;
3729  frame->colorrange = AvFrame->color_range;
3730  frame->colorprimaries = AvFrame->color_primaries;
3731  frame->colortransfer = AvFrame->color_trc;
3732  frame->chromalocation = AvFrame->chroma_location;
3733  frame->pix_fmt = AvFrame->format;
3734  frame->deinterlace_inuse = DEINT_NONE;
3735  frame->deinterlace_inuse2x = 0;
3736  frame->decoder_deinterlaced = 0;
3737  frame->rotation = m_videoRotation;
3738  m_parent->ReleaseNextVideoFrame(frame, temppts);
3739  m_mythCodecCtx->PostProcessFrame(context, frame);
3740  }
3741 
3742  m_decodedVideoFrame = frame;
3743  m_gotVideoFrame = true;
3744  if (++m_fpsSkip >= m_fpsMultiplier)
3745  {
3746  ++m_framesPlayed;
3747  m_fpsSkip = 0;
3748  }
3749 
3750  m_lastVPts = temppts;
3751  if (!m_firstVPts && m_firstVPtsInuse)
3752  m_firstVPts = temppts;
3753 
3754  return true;
3755 }
3756 
3763  const AVStream *stream, const AVPacket *pkt)
3764 {
3765  (void) stream;
3766 
3767  const uint8_t *buf = pkt->data;
3768  uint64_t linemask = 0;
3769  unsigned long long utc = m_lastCcPtsu;
3770 
3771  // [i]tv0 means there is a linemask
3772  // [I]TV0 means there is no linemask and all lines are present
3773  if ((buf[0]=='t') && (buf[1]=='v') && (buf[2] == '0'))
3774  {
3776  memcpy(&linemask, buf + 3, 8);
3777  buf += 11;
3778  }
3779  else if ((buf[0]=='T') && (buf[1]=='V') && (buf[2] == '0'))
3780  {
3781  linemask = 0xffffffffffffffffLL;
3782  buf += 3;
3783  }
3784  else
3785  {
3786  LOG(VB_VBI, LOG_ERR, LOC + QString("Unknown VBI data stream '%1%2%3'")
3787  .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
3788  return;
3789  }
3790 
3791  static constexpr uint kMinBlank = 6;
3792  for (uint i = 0; i < 36; i++)
3793  {
3794  if (!((linemask >> i) & 0x1))
3795  continue;
3796 
3797  const uint line = ((i < 18) ? i : i-18) + kMinBlank;
3798  const uint field = (i<18) ? 0 : 1;
3799  const uint id2 = *buf & 0xf;
3800  switch (id2)
3801  {
3802  case VBI_TYPE_TELETEXT:
3803  // SECAM lines 6-23
3804  // PAL lines 6-22
3805  // NTSC lines 10-21 (rare)
3806  if (m_tracks[kTrackTypeTeletextMenu].empty())
3807  {
3808  StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
3809  m_tracks[kTrackTypeTeletextMenu].push_back(si);
3810  }
3811  m_ttd->Decode(buf+1, VBI_IVTV);
3812  break;
3813  case VBI_TYPE_CC:
3814  // PAL line 22 (rare)
3815  // NTSC line 21
3816  if (21 == line)
3817  {
3818  int data = (buf[2] << 8) | buf[1];
3820  m_ccd608->FormatCCField(utc/1000, field, data);
3821  utc += 33367;
3822  }
3823  break;
3824  case VBI_TYPE_VPS: // Video Programming System
3825  // PAL line 16
3826  m_ccd608->DecodeVPS(buf+1); // a.k.a. PDC
3827  break;
3828  case VBI_TYPE_WSS: // Wide Screen Signal
3829  // PAL line 23
3830  // NTSC line 20
3831  m_ccd608->DecodeWSS(buf+1);
3832  break;
3833  }
3834  buf += 43;
3835  }
3836  m_lastCcPtsu = utc;
3837  UpdateCaptionTracksFromStreams(true, false);
3838 }
3839 
3845  const AVStream* /*stream*/, const AVPacket *pkt)
3846 {
3847  const uint8_t *buf = pkt->data;
3848  const uint8_t *buf_end = pkt->data + pkt->size;
3849 
3850 
3851  while (buf < buf_end)
3852  {
3853  if (*buf == 0x10)
3854  {
3855  buf++; // skip
3856  }
3857  else if (*buf == 0x02)
3858  {
3859  buf += 4;
3860  if ((buf_end - buf) >= 42)
3861  m_ttd->Decode(buf, VBI_DVB);
3862  buf += 42;
3863  }
3864  else if (*buf == 0x03)
3865  {
3866  buf += 4;
3867  if ((buf_end - buf) >= 42)
3868  m_ttd->Decode(buf, VBI_DVB_SUBTITLE);
3869  buf += 42;
3870  }
3871  else if (*buf == 0xff)
3872  {
3873  buf += 3;
3874  }
3875  else
3876  {
3877  LOG(VB_VBI, LOG_ERR, LOC +
3878  QString("VBI: Unknown descriptor: %1").arg(*buf));
3879  buf += 46;
3880  }
3881  }
3882 }
3883 
3888  const AVStream *str, const AVPacket *pkt)
3889 {
3890 #ifdef USING_MHEG
3891  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
3892  return;
3893 
3894  // The packet may contain several tables.
3895  uint8_t *data = pkt->data;
3896  int length = pkt->size;
3897  int componentTag = 0;
3898  int dataBroadcastId = 0;
3899  unsigned carouselId = 0;
3900  {
3901  QMutexLocker locker(avcodeclock);
3902  componentTag = str->component_tag;
3903  dataBroadcastId = str->data_id;
3904  carouselId = (unsigned) str->carousel_id;
3905  }
3906  while (length > 3)
3907  {
3908  uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
3909 
3910  if (sectionLen > length) // This may well be filler
3911  return;
3912 
3913  m_itv->ProcessDSMCCSection(data, sectionLen,
3914  componentTag, carouselId,
3915  dataBroadcastId);
3916  length -= sectionLen;
3917  data += sectionLen;
3918  }
3919 #else
3920  Q_UNUSED(str);
3921  Q_UNUSED(pkt);
3922 #endif // USING_MHEG
3923 }
3924 
3925 bool AvFormatDecoder::ProcessSubtitlePacket(AVStream *curstream, AVPacket *pkt)
3926 {
3927  if (!m_parent->GetSubReader(pkt->stream_index))
3928  return true;
3929 
3930  long long pts = 0;
3931 
3932  if (pkt->dts != AV_NOPTS_VALUE)
3933  pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
3934 
3935  avcodeclock->lock();
3937  bool isForcedTrack = m_selectedTrack[kTrackTypeSubtitle].m_forced;
3938  avcodeclock->unlock();
3939 
3940  int gotSubtitles = 0;
3941  AVSubtitle subtitle;
3942  memset(&subtitle, 0, sizeof(AVSubtitle));
3943 
3944  if (m_ringBuffer->IsDVD())
3945  {
3946  if (m_ringBuffer->DVD()->NumMenuButtons() > 0)
3947  {
3948  m_ringBuffer->DVD()->GetMenuSPUPkt(pkt->data, pkt->size,
3949  curstream->id, pts);
3950  }
3951  else
3952  {
3953  if (pkt->stream_index == subIdx)
3954  {
3955  QMutexLocker locker(avcodeclock);
3956  m_ringBuffer->DVD()->DecodeSubtitles(&subtitle, &gotSubtitles,
3957  pkt->data, pkt->size, pts);
3958  }
3959  }
3960  }
3961  else if (m_decodeAllSubtitles || pkt->stream_index == subIdx)
3962  {
3963  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
3964  QMutexLocker locker(avcodeclock);
3965  avcodec_decode_subtitle2(ctx, &subtitle, &gotSubtitles,
3966  pkt);
3967 
3968  subtitle.start_display_time += pts;
3969  subtitle.end_display_time += pts;
3970  }
3971 
3972  if (gotSubtitles)
3973  {
3974  if (isForcedTrack)
3975  subtitle.forced = static_cast<int>(true);
3976  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3977  QString("subtl timecode %1 %2 %3 %4")
3978  .arg(pkt->pts).arg(pkt->dts)
3979  .arg(subtitle.start_display_time)
3980  .arg(subtitle.end_display_time));
3981 
3982  bool forcedon = m_parent->GetSubReader(pkt->stream_index)->AddAVSubtitle(
3983  subtitle, curstream->codecpar->codec_id == AV_CODEC_ID_XSUB,
3985  m_parent->EnableForcedSubtitles(forcedon || isForcedTrack);
3986  }
3987 
3988  return true;
3989 }
3990 
3992 {
3993  if (!(m_decodeAllSubtitles ||
3994  m_selectedTrack[kTrackTypeRawText].m_av_stream_index == pkt->stream_index))
3995  {
3996  return false;
3997  }
3998 
3999  if (!m_parent->GetSubReader(pkt->stream_index+0x2000))
4000  return false;
4001 
4002  QTextCodec *codec = QTextCodec::codecForName("utf-8");
4003  QTextDecoder *dec = codec->makeDecoder();
4004  QString text = dec->toUnicode((const char*)pkt->data, pkt->size - 1);
4005  QStringList list = text.split('\n', QString::SkipEmptyParts);
4006  delete dec;
4007 
4008  m_parent->GetSubReader(pkt->stream_index+0x2000)->
4009  AddRawTextSubtitle(list, pkt->duration);
4010 
4011  return true;
4012 }
4013 
4014 bool AvFormatDecoder::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
4015  DecodeType decodetype)
4016 {
4017  enum AVCodecID codec_id = curstream->codecpar->codec_id;
4018 
4019  switch (codec_id)
4020  {
4021  case AV_CODEC_ID_MPEG2VBI:
4022  ProcessVBIDataPacket(curstream, pkt);
4023  break;
4024  case AV_CODEC_ID_DVB_VBI:
4025  ProcessDVBDataPacket(curstream, pkt);
4026  break;
4027  case AV_CODEC_ID_DSMCC_B:
4028  {
4029  ProcessDSMCCPacket(curstream, pkt);
4031  // Have to return regularly to ensure that the OSD is updated.
4032  // This applies both to MHEG and also channel browsing.
4033 #ifdef USING_MHEG
4034  if (!(decodetype & kDecodeVideo))
4036 #else
4037  Q_UNUSED(decodetype);
4038 #endif // USING_MHEG:
4039  break;
4040  }
4041  default:
4042  break;
4043  }
4044  return true;
4045 }
4046 
4048 {
4049  int ret = DecoderBase::SetTrack(type, trackNo);
4050 
4051  if (kTrackTypeAudio == type)
4052  {
4053  QString msg = SetupAudioStream() ? "" : "not ";
4054  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio stream type "+msg+"changed.");
4055  }
4056 
4057  return ret;
4058 }
4059 
4061 {
4062  if (!m_ic || trackNo >= m_tracks[type].size())
4063  return "";
4064 
4065  bool forced = m_tracks[type][trackNo].m_forced;
4066  int lang_key = m_tracks[type][trackNo].m_language;
4067  QString forcedString = forced ? QObject::tr(" (forced)") : "";
4068  if (kTrackTypeAudio == type)
4069  {
4070  QString msg = iso639_key_toName(lang_key);
4071 
4072  switch (m_tracks[type][trackNo].m_audio_type)
4073  {
4074  case kAudioTypeNormal :
4075  {
4076  int av_index = m_tracks[kTrackTypeAudio][trackNo].m_av_stream_index;
4077  AVStream *s = m_ic->streams[av_index];
4078 
4079  if (s)
4080  {
4081  AVCodecParameters *par = s->codecpar;
4082  AVCodecContext *ctx = gCodecMap->getCodecContext(s);
4083  if (par->codec_id == AV_CODEC_ID_MP3)
4084  msg += QString(" MP3");
4085  else if (ctx && ctx->codec)
4086  msg += QString(" %1").arg(ctx->codec->name).toUpper();
4087 
4088  int channels = 0;
4089  if (m_ringBuffer->IsDVD() || par->channels)
4090  channels = m_tracks[kTrackTypeAudio][trackNo].m_orig_num_channels;
4091 
4092  if (channels == 0)
4093  msg += QString(" ?ch");
4094  else if((channels > 4) && !(channels & 1))
4095  msg += QString(" %1.1ch").arg(channels - 1);
4096  else
4097  msg += QString(" %1ch").arg(channels);
4098  }
4099 
4100  break;
4101  }
4103  case kAudioTypeCommentary :
4105  case kAudioTypeCleanEffects :
4106  case kAudioTypeSpokenSubs :
4107  default :
4108  msg += QString(" (%1)")
4109  .arg(toString(m_tracks[type][trackNo].m_audio_type));
4110  break;
4111  }
4112 
4113  return QString("%1: %2").arg(trackNo + 1).arg(msg);
4114  }
4115  if (kTrackTypeSubtitle == type)
4116  {
4117  return QObject::tr("Subtitle") + QString(" %1: %2%3")
4118  .arg(trackNo + 1).arg(iso639_key_toName(lang_key))
4119  .arg(forcedString);
4120  }
4121  if (forced && kTrackTypeRawText == type)
4122  {
4123  return DecoderBase::GetTrackDesc(type, trackNo) + forcedString;
4124  }
4125  return DecoderBase::GetTrackDesc(type, trackNo);
4126 }
4127 
4129 {
4130  return m_ttd->GetDecoderType();
4131 }
4132 
4133 QString AvFormatDecoder::GetXDS(const QString &key) const
4134 {
4135  return m_ccd608->GetXDS(key);
4136 }
4137 
4138 QByteArray AvFormatDecoder::GetSubHeader(uint trackNo) const
4139 {
4140  if (trackNo >= m_tracks[kTrackTypeSubtitle].size())
4141  return QByteArray();
4142 
4143  int index = m_tracks[kTrackTypeSubtitle][trackNo].m_av_stream_index;
4144  AVCodecContext *ctx = gCodecMap->getCodecContext(m_ic->streams[index]);
4145  if (!ctx)
4146  return QByteArray();
4147 
4148  return QByteArray((char *)ctx->subtitle_header,
4149  ctx->subtitle_header_size);
4150 }
4151 
4153  QByteArray &data)
4154 {
4155  if (trackNo >= m_tracks[kTrackTypeAttachment].size())
4156  return;
4157 
4158  int index = m_tracks[kTrackTypeAttachment][trackNo].m_av_stream_index;
4159  AVDictionaryEntry *tag = av_dict_get(m_ic->streams[index]->metadata,
4160  "filename", nullptr, 0);
4161  if (tag)
4162  filename = QByteArray(tag->value);
4163  AVCodecParameters *par = m_ic->streams[index]->codecpar;
4164  data = QByteArray((char *)par->extradata, par->extradata_size);
4165 }
4166 
4168 {
4169  for (size_t i = 0; i < m_tracks[kTrackTypeAudio].size(); i++)
4170  {
4171  AVStream *s = m_ic->streams[m_tracks[kTrackTypeAudio][i].m_av_stream_index];
4172  if (s)
4173  {
4174  if ((s->component_tag == tag) ||
4175  ((tag <= 0) && s->component_tag <= 0))
4176  {
4177  return SetTrack(kTrackTypeAudio, i) != -1;
4178  }
4179  }
4180  }
4181  return false;
4182 }
4183 
4185 {
4186  for (uint i = 0; i < m_ic->nb_streams; i++)
4187  {
4188  AVStream *s = m_ic->streams[i];
4189  if (s)
4190  {
4191  if (s->component_tag == tag)
4192  {
4193  StreamInfo si(i, 0, 0, 0, 0);
4195  return true;
4196  }
4197  }
4198  }
4199  return false;
4200 }
4201 
4202 // documented in decoderbase.cpp
4204 {
4205  if (kTrackTypeAudio == type)
4206  return AutoSelectAudioTrack();
4207 
4209  return -1;
4210 
4212 }
4213 
4214 static vector<int> filter_lang(const sinfo_vec_t &tracks, int lang_key,
4215  const vector<int> &ftype)
4216 {
4217  vector<int> ret;
4218 
4219  for (int index : ftype)
4220  {
4221  if ((lang_key < 0) || tracks[index].m_language == lang_key)
4222  ret.push_back(index);
4223  }
4224 
4225  return ret;
4226 }
4227 
4228 static vector<int> filter_type(const sinfo_vec_t &tracks, AudioTrackType type)
4229 {
4230  vector<int> ret;
4231 
4232  for (size_t i = 0; i < tracks.size(); i++)
4233  {
4234  if (tracks[i].m_audio_type == type)
4235  ret.push_back(i);
4236  }
4237 
4238  return ret;
4239 }
4240 
4241 int AvFormatDecoder::filter_max_ch(const AVFormatContext *ic,
4242  const sinfo_vec_t &tracks,
4243  const vector<int> &fs,
4244  enum AVCodecID codecId,
4245  int profile)
4246 {
4247  int selectedTrack = -1;
4248  int max_seen = -1;
4249 
4250  for (int f : fs)
4251  {
4252  const int stream_index = tracks[f].m_av_stream_index;
4253  AVCodecParameters *par = ic->streams[stream_index]->codecpar;
4254  if ((codecId == AV_CODEC_ID_NONE || codecId == par->codec_id) &&
4255  (max_seen < par->channels))
4256  {
4257  if (codecId == AV_CODEC_ID_DTS && profile > 0)
4258  {
4259  // we cannot decode dts-hd, so only select it if passthrough
4260  if (!DoPassThrough(par, true) || par->profile != profile)
4261  continue;
4262  }
4263  selectedTrack = f;
4264  max_seen = par->channels;
4265  }
4266  }
4267 
4268  return selectedTrack;
4269 }
4270 
4318 {
4319  const sinfo_vec_t &atracks = m_tracks[kTrackTypeAudio];
4322  int &ctrack = m_currentTrack[kTrackTypeAudio];
4323 
4324  uint numStreams = atracks.size();
4325  if ((ctrack >= 0) && (ctrack < (int)numStreams))
4326  return ctrack; // audio already selected
4327 
4328 #if 0
4329  // enable this to print streams
4330  for (const auto & track : atracks)
4331  {
4332  int idx = track.m_av_stream_index;
4333  AVCodecContext *codec_ctx = m_ic->streams[idx]->codec;
4334  AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
4335  codec_ctx->sample_rate, codec_ctx->channels,
4336  DoPassThrough(codec_ctx, true));
4337  LOG(VB_AUDIO, LOG_DEBUG, LOC + " * " + item.toString());
4338  }
4339 #endif
4340 
4341  int selTrack = (1 == numStreams) ? 0 : -1;
4342  int wlang = wtrack.m_language;
4343 
4344 
4345  if ((selTrack < 0) && (wtrack.m_av_substream_index >= 0))
4346  {
4347  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio sub-stream");
4348  // Dual stream without language information: choose
4349  // the previous substream that was kept in wtrack,
4350  // ignoring the stream index (which might have changed).
4351  int substream_index = wtrack.m_av_substream_index;
4352 
4353  for (uint i = 0; i < numStreams; i++)
4354  {
4355  if (atracks[i].m_av_substream_index == substream_index)
4356  {
4357  selTrack = i;
4358  break;
4359  }
4360  }
4361  }
4362 
4363  if ((selTrack < 0) && wlang >= -1 && numStreams)
4364  {
4365  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio track");
4366  // Try to reselect user selected audio stream.
4367  // This should find the stream after a commercial
4368  // break and in some cases after a channel change.
4369  uint windx = wtrack.m_language_index;
4370  for (uint i = 0; i < numStreams; i++)
4371  {
4372  if (wlang == atracks[i].m_language)
4373  {
4374  selTrack = i;
4375 
4376  if (windx == atracks[i].m_language_index)
4377  break;
4378  }
4379  }
4380  }
4381 
4382  if (selTrack < 0 && numStreams)
4383  {
4384  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select audio track (w/lang)");
4385 
4386  // Filter out commentary and audio description tracks
4387  vector<int> ftype = filter_type(atracks, kAudioTypeNormal);
4388 
4389  if (ftype.empty())
4390  {
4391  LOG(VB_AUDIO, LOG_WARNING, "No audio tracks matched the type filter, "
4392  "so trying all tracks.");
4393  for (int i = 0; i < static_cast<int>(atracks.size()); i++)
4394  ftype.push_back(i);
4395  }
4396 
4397  // try to get the language track matching the frontend language.
4398  QString language_key_convert = iso639_str2_to_str3(gCoreContext->GetLanguage());
4399  uint language_key = iso639_str3_to_key(language_key_convert);
4400  uint canonical_key = iso639_key_to_canonical_key(language_key);
4401 
4402  vector<int> flang = filter_lang(atracks, canonical_key, ftype);
4403 
4404  if (m_audio->CanDTSHD())
4405  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4406  FF_PROFILE_DTS_HD_MA);
4407  if (selTrack < 0)
4408  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4409 
4410  if (selTrack < 0 && m_audio->CanDTSHD())
4411  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4412  FF_PROFILE_DTS_HD_HRA);
4413  if (selTrack < 0)
4414  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4415 
4416  if (selTrack < 0)
4417  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4418 
4419  if (selTrack < 0)
4420  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4421 
4422  if (selTrack < 0)
4423  selTrack = filter_max_ch(m_ic, atracks, flang);
4424 
4425  // try to get best track for most preferred language
4426  // Set by the "Guide Data" language prefs in Appearance.
4427  if (selTrack < 0)
4428  {
4429  vector<int>::const_iterator it = m_languagePreference.begin();
4430  for (; it != m_languagePreference.end() && selTrack < 0; ++it)
4431  {
4432  flang = filter_lang(atracks, *it, ftype);
4433 
4434  if (m_audio->CanDTSHD())
4435  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4436  FF_PROFILE_DTS_HD_MA);
4437  if (selTrack < 0)
4438  selTrack = filter_max_ch(m_ic, atracks, flang,
4439  AV_CODEC_ID_TRUEHD);
4440 
4441  if (selTrack < 0 && m_audio->CanDTSHD())
4442  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4443  FF_PROFILE_DTS_HD_HRA);
4444 
4445  if (selTrack < 0)
4446  selTrack = filter_max_ch(m_ic, atracks, flang,
4447  AV_CODEC_ID_EAC3);
4448 
4449  if (selTrack < 0)
4450  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4451 
4452  if (selTrack < 0)
4453  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4454 
4455  if (selTrack < 0)
4456  selTrack = filter_max_ch(m_ic, atracks, flang);
4457  }
4458  }
4459 
4460  // could not select track based on user preferences (language)
4461  // try to select the default track
4462  if (selTrack < 0 && numStreams)
4463  {
4464  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select default track");
4465  for (size_t i = 0; i < atracks.size(); i++) {
4466  int idx = atracks[i].m_av_stream_index;
4467  if (m_ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
4468  {
4469  selTrack = i;
4470  break;
4471  }
4472  }
4473  }
4474 
4475  // try to get best track for any language
4476  if (selTrack < 0)
4477  {
4478  LOG(VB_AUDIO, LOG_INFO, LOC +
4479  "Trying to select audio track (wo/lang)");
4480  flang = filter_lang(atracks, -1, ftype);
4481 
4482  if (m_audio->CanDTSHD())
4483  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4484  FF_PROFILE_DTS_HD_MA);
4485  if (selTrack < 0)
4486  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4487 
4488  if (selTrack < 0 && m_audio->CanDTSHD())
4489  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4490  FF_PROFILE_DTS_HD_HRA);
4491 
4492  if (selTrack < 0)
4493  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4494 
4495  if (selTrack < 0)
4496  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4497 
4498  if (selTrack < 0)
4499  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4500 
4501  if (selTrack < 0)
4502  selTrack = filter_max_ch(m_ic, atracks, flang);
4503  }
4504  }
4505 
4506  if (selTrack < 0)
4507  {
4508  strack.m_av_stream_index = -1;
4509  if (ctrack != selTrack)
4510  {
4511  LOG(VB_AUDIO, LOG_INFO, LOC + "No suitable audio track exists.");
4512  ctrack = selTrack;
4513  }
4514  }
4515  else
4516  {
4517  ctrack = selTrack;
4518  strack = atracks[selTrack];
4519 
4520  if (wtrack.m_av_stream_index < 0)
4521  wtrack = strack;
4522 
4523  LOG(VB_AUDIO, LOG_INFO, LOC +
4524  QString("Selected track %1 (A/V Stream #%2)")
4525  .arg(GetTrackDesc(kTrackTypeAudio, ctrack))
4526  .arg(strack.m_av_stream_index));
4527  }
4528 
4529  SetupAudioStream();
4530  return selTrack;
4531 }
4532 
4533 static void extract_mono_channel(uint channel, AudioInfo *audioInfo,
4534  char *buffer, int bufsize)
4535 {
4536  // Only stereo -> mono (left or right) is supported
4537  if (audioInfo->m_channels != 2)
4538  return;
4539 
4540  if (channel >= (uint)audioInfo->m_channels)
4541  return;
4542 
4543  const uint samplesize = audioInfo->m_sampleSize;
4544  const uint samples = bufsize / samplesize;
4545  const uint halfsample = samplesize >> 1;
4546 
4547  const char *from = (channel == 1) ? buffer + halfsample : buffer;
4548  char *to = (channel == 0) ? buffer + halfsample : buffer;
4549 
4550  for (uint sample = 0; sample < samples;
4551  (sample++), (from += samplesize), (to += samplesize))
4552  {
4553  memmove(to, from, halfsample);
4554  }
4555 }
4556 
4557 bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
4558  DecodeType decodetype)
4559 {
4560  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
4561  int ret = 0;
4562  int data_size = 0;
4563  bool firstloop = true;
4564  int decoded_size = -1;
4565 
4566  avcodeclock->lock();
4569  avcodeclock->unlock();
4570 
4571  AVPacket tmp_pkt;
4572  av_init_packet(&tmp_pkt);
4573  tmp_pkt.data = pkt->data;
4574  tmp_pkt.size = pkt->size;
4575 
4576  while (tmp_pkt.size > 0)
4577  {
4578  bool reselectAudioTrack = false;
4579 
4581  if (!m_audio->HasAudioIn())
4582  {
4583  LOG(VB_AUDIO, LOG_INFO, LOC +
4584  "Audio is disabled - trying to restart it");
4585  reselectAudioTrack = true;
4586  }
4588 
4589  // detect switches between stereo and dual languages
4590  bool wasDual = audSubIdx != -1;
4591  bool isDual = ctx->avcodec_dual_language != 0;
4592  if ((wasDual && !isDual) || (!wasDual && isDual))
4593  {
4595  reselectAudioTrack = true;
4596  }
4597 
4598  // detect channels on streams that need
4599  // to be decoded before we can know this
4600  bool already_decoded = false;
4601  if (!ctx->channels)
4602  {
4603  QMutexLocker locker(avcodeclock);
4604 
4605  if (DoPassThrough(curstream->codecpar, false) || !DecoderWillDownmix(ctx))
4606  {
4607  // for passthru or codecs for which the decoder won't downmix
4608  // let the decoder set the number of channels. For other codecs
4609  // we downmix if necessary in audiooutputbase
4610  ctx->request_channel_layout = 0;
4611  }
4612  else // No passthru, the decoder will downmix
4613  {
4614  ctx->request_channel_layout =
4615  av_get_default_channel_layout(m_audio->GetMaxChannels());
4616  if (ctx->codec_id == AV_CODEC_ID_AC3)
4617  ctx->channels = m_audio->GetMaxChannels();
4618  }
4619 
4620  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
4621  decoded_size = data_size;
4622  already_decoded = true;
4623  reselectAudioTrack |= ctx->channels;
4624  }
4625 
4626  if (reselectAudioTrack)
4627  {
4628  QMutexLocker locker(avcodeclock);
4634  }
4635 
4636  if (!(decodetype & kDecodeAudio) || (pkt->stream_index != audIdx)
4637  || !m_audio->HasAudioOut())
4638  break;
4639 
4640  if (firstloop && pkt->pts != AV_NOPTS_VALUE)
4641  m_lastAPts = (long long)(av_q2d(curstream->time_base) * pkt->pts * 1000);
4642 
4643  if (!m_useFrameTiming)
4644  {
4645  // This code under certain conditions causes jump backwards to lose
4646  // audio.
4647  if (m_skipAudio && m_selectedTrack[kTrackTypeVideo].m_av_stream_index > -1)
4648  {
4649  if ((m_lastAPts < m_lastVPts - (10.0F / m_fps)) || m_lastVPts == 0)
4650  break;
4651  m_skipAudio = false;
4652  }
4653 
4654  // skip any audio frames preceding first video frame
4656  {
4657  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4658  QString("discarding early audio timecode %1 %2 %3")
4659  .arg(pkt->pts).arg(pkt->dts).arg(m_lastAPts));
4660  break;
4661  }
4662  }
4663  m_firstVPtsInuse = false;
4664 
4665  avcodeclock->lock();
4666  data_size = 0;
4667 
4668  // Check if the number of channels or sampling rate have changed
4669  if (ctx->sample_rate != m_audioOut.m_sampleRate ||
4670  ctx->channels != m_audioOut.m_channels ||
4672  ctx->bits_per_raw_sample) != m_audioOut.format)
4673  {
4674  LOG(VB_GENERAL, LOG_INFO, LOC + "Audio stream changed");
4675  if (ctx->channels != m_audioOut.m_channels)
4676  {
4677  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Number of audio channels changed from %1 to %2")
4678  .arg(m_audioOut.m_channels).arg(ctx->channels));
4679  }
4682  audIdx = -1;
4684  }
4685 
4687  {
4688  if (!already_decoded)
4689  {
4691  {
4692  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
4693  decoded_size = data_size;
4694  }
4695  else
4696  {
4697  decoded_size = -1;
4698  }
4699  }
4700  memcpy(m_audioSamples, tmp_pkt.data, tmp_pkt.size);
4701  data_size = tmp_pkt.size;
4702  // We have processed all the data, there can't be any left
4703  tmp_pkt.size = 0;
4704  }
4705  else
4706  {
4707  if (!already_decoded)
4708  {
4709  if (DecoderWillDownmix(ctx))
4710  {
4711  ctx->request_channel_layout =
4712  av_get_default_channel_layout(m_audio->GetMaxChannels());
4713  }
4714  else
4715  {
4716  ctx->request_channel_layout = 0;
4717  }
4718 
4719  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
4720  decoded_size = data_size;
4721  }
4722  }
4723  avcodeclock->unlock();
4724 
4725  if (ret < 0)
4726  {
4727  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown audio decoding error");
4728  return false;
4729  }
4730 
4731  if (data_size <= 0)
4732  {
4733  tmp_pkt.data += ret;
4734  tmp_pkt.size -= ret;
4735  continue;
4736  }
4737 
4738  long long temppts = m_lastAPts;
4739 
4740  if (audSubIdx != -1 && !m_audioOut.m_doPassthru)
4741  extract_mono_channel(audSubIdx, &m_audioOut,
4742  (char *)m_audioSamples, data_size);
4743 
4744  int samplesize = AudioOutputSettings::SampleSize(m_audio->GetFormat());
4745  int frames = (ctx->channels <= 0 || decoded_size < 0 || !samplesize) ? -1 :
4746  decoded_size / (ctx->channels * samplesize);
4747  m_audio->AddAudioData((char *)m_audioSamples, data_size, temppts, frames);
4749  {
4751  }
4752  else
4753  {
4754  m_lastAPts += (long long)
4755  ((double)(frames * 1000) / ctx->sample_rate);
4756  }
4757 
4758  LOG(VB_TIMESTAMP, LOG_INFO, LOC + QString("audio timecode %1 %2 %3 %4")
4759  .arg(pkt->pts).arg(pkt->dts).arg(temppts).arg(m_lastAPts));
4760 
4763 
4764  tmp_pkt.data += ret;
4765  tmp_pkt.size -= ret;
4766  firstloop = false;
4767  }
4768 
4769  return true;
4770 }
4771 
4772 // documented in decoderbase.h
4773 bool AvFormatDecoder::GetFrame(DecodeType decodetype, bool &Retry)
4774 {
4775  AVPacket *pkt = nullptr;
4776  bool have_err = false;
4777 
4778  const DecodeType origDecodetype = decodetype;
4779 
4780  m_gotVideoFrame = false;
4781 
4782  m_frameDecoded = 0;
4783  m_decodedVideoFrame = nullptr;
4784 
4785  m_allowedQuit = false;
4786  bool storevideoframes = false;
4787 
4788  avcodeclock->lock();
4789  AutoSelectTracks();
4790  avcodeclock->unlock();
4791 
4792  m_skipAudio = (m_lastVPts == 0);
4793 
4794  if( !m_processFrames )
4795  {
4796  return false;
4797  }
4798 
4800  m_needDummyVideoFrames = false;
4801 
4802  if (!m_hasVideo && (decodetype & kDecodeVideo))
4803  {
4804  // NB This could be an issue if the video stream is not
4805  // detected initially as the video buffers will be filled.
4806  m_needDummyVideoFrames = true;
4807  decodetype = (DecodeType)((int)decodetype & ~kDecodeVideo);
4808  m_skipAudio = false;
4809  }
4810 
4812 
4815  {
4816  int got_picture = 0;
4817  AVStream *stream = m_ic->streams[m_selectedTrack[kTrackTypeVideo]
4819  MythAVFrame mpa_pic;
4820  if (!mpa_pic)
4821  return false;
4822 
4823  m_privateDec->GetFrame(stream, mpa_pic, &got_picture, nullptr);
4824  if (got_picture)
4825  ProcessVideoFrame(stream, mpa_pic);
4826  }
4827 
4828  while (!m_allowedQuit)
4829  {
4830  if (decodetype & kDecodeAudio)
4831  {
4832  if (((m_currentTrack[kTrackTypeAudio] < 0) ||
4833  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)))
4834  {
4835  // disable audio request if there are no audio streams anymore
4836  // and we have video, otherwise allow decoding to stop
4837  if (m_hasVideo)
4838  decodetype = (DecodeType)((int)decodetype & ~kDecodeAudio);
4839  else
4840  m_allowedQuit = true;
4841  }
4842  }
4843  else if ((origDecodetype & kDecodeAudio) &&
4844  (m_currentTrack[kTrackTypeAudio] >= 0) &&
4845  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index >= 0))
4846  {
4847  // Turn on audio decoding again if it was on originally
4848  // and an audio stream has now appeared. This can happen
4849  // in still DVD menus with audio
4850  decodetype = (DecodeType)((int)decodetype | kDecodeAudio);
4851  }
4852 
4854 
4855  if (m_gotVideoFrame)
4856  {
4857  if (decodetype == kDecodeNothing)
4858  {
4859  // no need to buffer audio or video if we
4860  // only care about building a keyframe map.
4861  // NB but allow for data only (MHEG) streams
4862  m_allowedQuit = true;
4863  }
4864  else if ((decodetype & kDecodeAV) == kDecodeAV &&
4865  (m_storedPackets.count() < max_video_queue_size) &&
4866  // buffer audio to prevent audio buffer
4867  // underruns in case you are setting negative values
4868  // in Adjust Audio Sync.
4871  {
4872  storevideoframes = true;
4873  }
4874  else if (decodetype & kDecodeVideo)
4875  {
4876  if (m_storedPackets.count() >= max_video_queue_size)
4877  {
4878  LOG(VB_GENERAL, LOG_WARNING, LOC +
4879  QString("Audio %1 ms behind video but already %2 "
4880  "video frames queued. AV-Sync might be broken.")
4881  .arg(m_lastVPts-m_lastAPts).arg(m_storedPackets.count()));
4882  }
4883  m_allowedQuit = true;
4884  continue;
4885  }
4886  }
4887 
4888  if (!storevideoframes && m_storedPackets.count() > 0)
4889  {
4890  if (pkt)
4891  {
4892  av_packet_unref(pkt);
4893  delete pkt;
4894  }
4895  pkt = m_storedPackets.takeFirst();
4896  }
4897  else
4898  {
4899  if (!pkt)
4900  {
4901  pkt = new AVPacket;
4902  memset(pkt, 0, sizeof(AVPacket));
4903  av_init_packet(pkt);
4904  }
4905 
4906  int retval = 0;
4907  if (!m_ic || ((retval = ReadPacket(m_ic, pkt, storevideoframes)) < 0))
4908  {
4909  if (retval == -EAGAIN)
4910  continue;
4911 
4912  SetEof(true);
4913  delete pkt;
4914  char errbuf[256];
4915  QString errmsg;
4916  if (av_strerror(retval, errbuf, sizeof errbuf) == 0)
4917  errmsg = QString(errbuf);
4918  else
4919  errmsg = "UNKNOWN";
4920 
4921  LOG(VB_GENERAL, LOG_ERR, QString("decoding error %1 (%2)")
4922  .arg(errmsg).arg(retval));
4923  return false;
4924  }
4925 
4926  if (m_waitingForChange && pkt->pos >= m_readAdjust)
4927  FileChanged();
4928 
4929  if (pkt->pos > m_readAdjust)
4930  pkt->pos -= m_readAdjust;
4931  }
4932 
4933  if (!m_ic)
4934  {
4935  LOG(VB_GENERAL, LOG_ERR, LOC + "No context");
4936  av_packet_unref(pkt);
4937  continue;
4938  }
4939 
4940  if (pkt->stream_index >= (int)m_ic->nb_streams)
4941  {
4942  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream");
4943  av_packet_unref(pkt);
4944  continue;
4945  }
4946 
4947  AVStream *curstream = m_ic->streams[pkt->stream_index];
4948 
4949  if (!curstream)
4950  {
4951  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream (NULL)");
4952  av_packet_unref(pkt);
4953  continue;
4954  }
4955 
4956  enum AVMediaType codec_type = curstream->codecpar->codec_type;
4957 
4958  if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
4959  {
4960  // av_dup_packet(pkt);
4961  m_storedPackets.append(pkt);
4962  pkt = nullptr;
4963  continue;
4964  }
4965 
4966  if (codec_type == AVMEDIA_TYPE_VIDEO &&
4967  pkt->stream_index == m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
4968  {
4969  if (!PreProcessVideoPacket(curstream, pkt))
4970  continue;
4971 
4972  // If the resolution changed in XXXPreProcessPkt, we may
4973  // have a fatal error, so check for this before continuing.
4974  if (m_parent->IsErrored())
4975  {
4976  av_packet_unref(pkt);
4977  delete pkt;
4978  return false;
4979  }
4980  }
4981 
4982  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4983  curstream->codecpar->codec_id == AV_CODEC_ID_TEXT)
4984  {
4985  ProcessRawTextPacket(pkt);
4986  av_packet_unref(pkt);
4987  continue;
4988  }
4989 
4990  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
4991  curstream->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT)
4992  {
4993  ProcessDVBDataPacket(curstream, pkt);
4994  av_packet_unref(pkt);
4995  continue;
4996  }
4997 
4998  if (codec_type == AVMEDIA_TYPE_DATA)
4999  {
5000  ProcessDataPacket(curstream, pkt, decodetype);
5001  av_packet_unref(pkt);
5002  continue;
5003  }
5004 
5005  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
5006  if (!ctx)
5007  {
5008  if (codec_type != AVMEDIA_TYPE_VIDEO)
5009  {
5010  LOG(VB_PLAYBACK, LOG_ERR, LOC +
5011  QString("No codec for stream index %1, type(%2) id(%3:%4)")
5012  .arg(pkt->stream_index)
5013  .arg(ff_codec_type_string(codec_type))
5014  .arg(ff_codec_id_string(curstream->codecpar->codec_id))
5015  .arg(curstream->codecpar->codec_id));
5016  // Process Stream Change in case we have no audio
5017  if (codec_type == AVMEDIA_TYPE_AUDIO && !m_audio->HasAudioIn())
5018  m_streamsChanged = true;
5019  }
5020  av_packet_unref(pkt);
5021  continue;
5022  }
5023 
5024  have_err = false;
5025 
5026  switch (codec_type)
5027  {
5028  case AVMEDIA_TYPE_AUDIO:
5029  {
5030  if (!ProcessAudioPacket(curstream, pkt, decodetype))
5031  have_err = true;
5032  else
5034  break;
5035  }
5036 
5037  case AVMEDIA_TYPE_VIDEO:
5038  {
5039  if (pkt->stream_index != m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
5040  {
5041  break;
5042  }
5043 
5044  if (pkt->pts != AV_NOPTS_VALUE)
5045  {
5046  m_lastCcPtsu = (long long)
5047  (av_q2d(curstream->time_base)*pkt->pts*1000000);
5048  }
5049 
5050  if (!(decodetype & kDecodeVideo))
5051  {
5052  m_framesPlayed++;
5053  m_gotVideoFrame = true;
5054  break;
5055  }
5056 
5057  if (!ProcessVideoPacket(curstream, pkt, Retry))
5058  have_err = true;
5059  break;
5060  }
5061 
5062  case AVMEDIA_TYPE_SUBTITLE:
5063  {
5064  if (!ProcessSubtitlePacket(curstream, pkt))
5065  have_err = true;
5066  break;
5067  }
5068 
5069  default:
5070  {
5071  LOG(VB_GENERAL, LOG_ERR, LOC +
5072  QString("Decoding - id(%1) type(%2)")
5073  .arg(ff_codec_id_string(ctx->codec_id))
5074  .arg(ff_codec_type_string(ctx->codec_type)));
5075  have_err = true;
5076  break;
5077  }
5078  }
5079 
5080  if (!have_err && !Retry)
5081  m_frameDecoded = 1;
5082  av_packet_unref(pkt);
5083  if (Retry)
5084  break;
5085  }
5086 
5087  delete pkt;
5088  return true;
5089 }
5090 
5092 {
5093  if (m_streamsChanged)
5094  {
5095  SeekReset(0, 0, true, true);
5096  avcodeclock->lock();
5097  ScanStreams(false);
5098  avcodeclock->unlock();
5099  m_streamsChanged = false;
5100  }
5101 }
5102 
5103 int AvFormatDecoder::ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &/*storePacket*/)
5104 {
5105  QMutexLocker locker(avcodeclock);
5106 
5107  return av_read_frame(ctx, pkt);
5108 }
5109 
5110 bool AvFormatDecoder::HasVideo(const AVFormatContext *ic)
5111 {
5112  if (ic && ic->cur_pmt_sect)
5113  {
5114  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
5115 
5116  for (uint i = 0; i < pmt.StreamCount(); i++)
5117  {
5118  // MythTV remaps OpenCable Video to normal video during recording
5119  // so "dvb" is the safest choice for system info type, since this
5120  // will ignore other uses of the same stream id in DVB countries.
5121  if (pmt.IsVideo(i, "dvb"))
5122  return true;
5123 
5124  // MHEG may explicitly select a private stream as video
5125  if ((i == (uint)m_selectedTrack[kTrackTypeVideo].m_av_stream_index) &&
5126  (pmt.StreamType(i) == StreamID::PrivData))
5127  {
5128  return true;
5129  }
5130  }
5131  }
5132 
5133  return GetTrackCount(kTrackTypeVideo) != 0U;
5134 }
5135 
5137 {
5138  while (m_needDummyVideoFrames && m_parent &&
5140  {
5142  if (!frame)
5143  return false;
5144 
5145  clear(frame);
5146  frame->dummy = 1;
5148  m_parent->DeLimboFrame(frame);
5149 
5150  frame->interlaced_frame = 0; // not interlaced
5151  frame->top_field_first = 1; // top field first
5152  frame->interlaced_reversed = 0;
5153  frame->repeat_pict = 0; // not a repeated picture
5154  frame->frameNumber = m_framesPlayed;
5155  frame->frameCounter = m_frameCounter++;
5156  frame->dummy = 1;
5157  frame->pause_frame = 0;
5158  frame->colorspace = AVCOL_SPC_BT709;
5159  frame->colorrange = AVCOL_RANGE_MPEG;
5160  frame->colorprimaries = AVCOL_PRI_BT709;
5161  frame->colortransfer = AVCOL_TRC_BT709;
5162  frame->chromalocation = AVCHROMA_LOC_LEFT;
5163  frame->deinterlace_inuse = DEINT_NONE;
5164  frame->deinterlace_inuse2x = 0;
5165  frame->decoder_deinterlaced = 0;
5166  frame->rotation = 0;
5167 
5168  m_decodedVideoFrame = frame;
5169  m_framesPlayed++;
5170  m_gotVideoFrame = true;
5171  }
5172  return true;
5173 }
5174 
5176 {
5177  if (m_privateDec)
5178  return m_privateDec->GetName();
5180 }
5181 
5183 {
5185  if (stream < 0 || !m_ic)
5186  return QString();
5187  return ff_codec_id_string(m_ic->streams[stream]->codecpar->codec_id);
5188 }
5189 
5191 {
5192  if (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)
5193  {
5194  m_disablePassthru = disable;
5195  return;
5196  }
5197 
5198  if (disable != m_disablePassthru)
5199  {
5200  m_disablePassthru = disable;
5201  QString msg = (disable) ? "Disabling" : "Allowing";
5202  LOG(VB_AUDIO, LOG_INFO, LOC + msg + " pass through");
5203 
5204  // Force pass through state to be reanalyzed
5206  }
5207 }
5208 
5210 {
5211  QMutexLocker locker(avcodeclock);
5212 
5213  SetupAudioStream();
5214 }
5215 
5216 inline bool AvFormatDecoder::DecoderWillDownmix(const AVCodecContext *ctx)
5217 {
5218  // Until ffmpeg properly implements dialnorm
5219  // use Myth internal downmixer if machines has FPU/SSE
5221  return false;
5222  if (!m_audio->CanDownmix())
5223  return true;
5224  // use ffmpeg only for dolby codecs if we have to
5225  switch (ctx->codec_id)
5226  {
5227  case AV_CODEC_ID_AC3:
5228  case AV_CODEC_ID_TRUEHD:
5229  case AV_CODEC_ID_EAC3:
5230  return true;
5231  default:
5232  return false;
5233  }
5234 }
5235 
5236 bool AvFormatDecoder::DoPassThrough(const AVCodecParameters *par, bool withProfile)
5237 {
5238  bool passthru = false;
5239 
5240  // if withProfile == false, we will accept any DTS stream regardless
5241  // of its profile. We do so, so we can bitstream DTS-HD as DTS core
5242  if (!withProfile && par->codec_id == AV_CODEC_ID_DTS && !m_audio->CanDTSHD())
5243  {
5244  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5245  par->codec_id, FF_PROFILE_DTS);
5246  }
5247  else
5248  {
5249  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5250  par->codec_id, par->profile);
5251  }
5252 
5253  passthru &= !m_disablePassthru;
5254 
5255  return passthru;
5256 }
5257 
5266 {
5267  AudioInfo info; // no_audio
5268  AVStream *curstream = nullptr;
5269  AVCodecContext *ctx = nullptr;
5270  AudioInfo old_in = m_audioIn;
5271  int requested_channels = 0;
5272 
5273  if ((m_currentTrack[kTrackTypeAudio] >= 0) && m_ic &&
5274  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index <=
5275  (int) m_ic->nb_streams) &&
5276  (curstream = m_ic->streams[m_selectedTrack[kTrackTypeAudio]
5277  .m_av_stream_index]) &&
5278  (ctx = gCodecMap->getCodecContext(curstream)))
5279  {
5280  AudioFormat fmt =
5282  ctx->bits_per_raw_sample);
5283 
5284  if (av_sample_fmt_is_planar(ctx->sample_fmt))
5285  {
5286  LOG(VB_AUDIO, LOG_INFO, LOC + QString("Audio data is planar"));
5287  }
5288 
5289  if (fmt == FORMAT_NONE)
5290  {
5291  int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
5292  if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
5293  ctx->bits_per_raw_sample)
5294  bps = ctx->bits_per_raw_sample;
5295  LOG(VB_GENERAL, LOG_ERR, LOC +
5296  QString("Unsupported sample format with %1 bits").arg(bps));
5297  return false;
5298  }
5299 
5300  bool using_passthru = DoPassThrough(curstream->codecpar, false);
5301 
5302  requested_channels = ctx->channels;
5303  ctx->request_channel_layout =
5304  av_get_default_channel_layout(requested_channels);
5305 
5306  if (!using_passthru &&
5307  ctx->channels > (int)m_audio->GetMaxChannels() &&
5308  DecoderWillDownmix(ctx))
5309  {
5310  requested_channels = m_audio->GetMaxChannels();
5311  ctx->request_channel_layout =
5312  av_get_default_channel_layout(requested_channels);
5313  }
5314  else
5315  {
5316  ctx->request_channel_layout = 0;
5317  }
5318 
5319  info = AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
5320  requested_channels, using_passthru, ctx->channels,
5321  ctx->codec_id == AV_CODEC_ID_DTS ? ctx->profile : 0);
5322  }
5323 
5324  if (!ctx)
5325  {
5327  LOG(VB_PLAYBACK, LOG_INFO, LOC +
5328  "No codec context. Returning false");
5329  return false;
5330  }
5331 
5332  if (info == m_audioIn)
5333  return false;
5334 
5335  LOG(VB_AUDIO, LOG_INFO, LOC + "Initializing audio parms from " +
5336  QString("audio track #%1").arg(m_currentTrack[kTrackTypeAudio]+1));
5337 
5338  m_audioOut = m_audioIn = info;
5339 
5340  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio format changed " +
5341  QString("\n\t\t\tfrom %1 to %2")
5342  .arg(old_in.toString()).arg(m_audioOut.toString()));
5343 
5344  m_audio->SetAudioParams(m_audioOut.format, ctx->channels,
5345  requested_channels,
5348  m_audio->ReinitAudio();
5349  AudioOutput *audioOutput = m_audio->GetAudioOutput();
5350  if (audioOutput)
5351  audioOutput->SetSourceBitrate(ctx->bit_rate);
5352 
5353  if (LCD *lcd = LCD::Get())
5354  {
5355  LCDAudioFormatSet audio_format = AUDIO_MP3;
5356 
5357  switch (ctx->codec_id)
5358  {
5359  case AV_CODEC_ID_MP2:
5360  audio_format = AUDIO_MPEG2;
5361  break;
5362  case AV_CODEC_ID_MP3:
5363  audio_format = AUDIO_MP3;
5364  break;
5365  case AV_CODEC_ID_AC3:
5366  audio_format = AUDIO_AC3;
5367  break;
5368  case AV_CODEC_ID_DTS:
5369  audio_format = AUDIO_DTS;
5370  break;
5371  case AV_CODEC_ID_VORBIS:
5372  audio_format = AUDIO_OGG;
5373  break;
5374  case AV_CODEC_ID_WMAV1:
5375  audio_format = AUDIO_WMA;
5376  break;
5377  case AV_CODEC_ID_WMAV2:
5378  audio_format = AUDIO_WMA2;
5379  break;
5380  default:
5381  audio_format = AUDIO_WAV;
5382  break;
5383  }
5384 
5385  lcd->setAudioFormatLEDs(audio_format, true);
5386 
5388  lcd->setVariousLEDs(VARIOUS_SPDIF, true);
5389  else
5390  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
5391 
5392  switch (m_audioIn.m_channels)
5393  {
5394  case 0:
5395  /* nb: aac and mp3 seem to be coming up 0 here, may point to an
5396  * avformatdecoder audio channel handling bug, per janneg */
5397  case 1:
5398  case 2:
5399  /* all audio codecs have at *least* one channel, but
5400  * LR is the fewest LED we can light up */
5401  lcd->setSpeakerLEDs(SPEAKER_LR, true);
5402  break;
5403  case 3:
5404  case 4:
5405  case 5:
5406  case 6:
5407  lcd->setSpeakerLEDs(SPEAKER_51, true);
5408  break;
5409  default:
5410  lcd->setSpeakerLEDs(SPEAKER_71, true);
5411  break;
5412  }
5413 
5414  }
5415  return true;
5416 }
5417 
5419 {
5420  int64_t start_time = INT64_MAX;
5421  int64_t end_time = INT64_MIN;
5422  AVStream *st = nullptr;
5423 
5424  for (uint i = 0; i < ic->nb_streams; i++)
5425  {
5426  AVStream *st1 = ic->streams[i];
5427  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
5428  {
5429  st = st1;
5430  break;
5431  }
5432  }
5433  if (!st)
5434  return;
5435 
5436  int64_t duration = INT64_MIN;
5437  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
5438  int64_t start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
5439  if (start_time1 < start_time)
5440  start_time = start_time1;
5441  if (st->duration != AV_NOPTS_VALUE) {
5442  int64_t end_time1 = start_time1 +
5443  av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5444  if (end_time1 > end_time)
5445  end_time = end_time1;
5446  }
5447  }
5448  if (st->duration != AV_NOPTS_VALUE) {
5449  int64_t duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5450  if (duration1 > duration)
5451  duration = duration1;
5452  }
5453  if (start_time != INT64_MAX) {
5454  ic->start_time = start_time;
5455  if (end_time != INT64_MIN) {
5456  if (end_time - start_time > duration)
5457  duration = end_time - start_time;
5458  }
5459  }
5460  if (duration != INT64_MIN) {
5461  int64_t filesize = 0;
5462  ic->duration = duration;
5463  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
5464  /* compute the bitrate */
5465  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
5466  (double)ic->duration;
5467  }
5468  }
5469 }
5470 
5471 /* vim: set expandtab tabstop=4 shiftwidth=4: */
virtual bool DoRewind(long long desiredFrame, bool discardFrames=true)
uint m_bitrate
Definition: decoderbase.h:304
void DeLimboFrame(VideoFrame *frame)
virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt, DecodeType decodetype)
int GetFreeVideoFrames(void) const
Returns the number of frames available for decoding onto.
Definition: mythplayer.cpp:889
int AutoSelectTrack(uint type) override
Select best track.
#define VBI_TYPE_TELETEXT
Definition: ivtv_myth.h:273
virtual bool Reset(void)=0
QString GetXDS(const QString &key) const
bool ProcessSubtitlePacket(AVStream *stream, AVPacket *pkt)
void SetKeyframeDistance(int keyframedistance)
Definition: mythplayer.cpp:538
void SetInInit(bool state)
int pause_frame
Definition: mythframe.h:158
#define codec_sw_copy(id)
Definition: mythcodecid.h:332
MythPlayer * GetPlayer()
Definition: decoderbase.h:154
int pitches[3]
Y, U, & V pitches.
Definition: mythframe.h:159
virtual bool DecoderWillResetOnAspect(void)
int CanonicalLanguageKey(int i) const
ISO 639-1 and ISO 639-2 support functions.
bool m_forced
Definition: decoderbase.h:103
bool ProcessAudioPacket(AVStream *stream, AVPacket *pkt, DecodeType decodetype)
void SetDisablePassThrough(bool disable) override
Disables AC3/DTS pass through.
QString GetTrackDesc(uint type, uint trackNo) const override
H264Parser * m_h264Parser
int filter_max_ch(const AVFormatContext *ic, const sinfo_vec_t &tracks, const vector< int > &fs, enum AVCodecID codecId=AV_CODEC_ID_NONE, int profile=-1)
void GetAttachmentData(uint trackNo, QByteArray &filename, QByteArray &data) override
void services(uint seconds, bool seen[64]) const
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
void SetFrameRate(double fps)
Definition: mythplayer.cpp:681
void SetInput(const QSize &Size, float Framerate=0, const QString &CodecName=QString())
int64_t ptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.c:78
int64_t LengthLastData(void)
static const int kMaxSizeBuffer
kMaxSizeBuffer is the maximum size of a buffer to be used with DecodeAudio
Definition: audiooutput.h:194
static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
void SetErrored(const QString &reason)
int m_videoRotation
Definition: decoderbase.h:350
const DVDRingBuffer * DVD(void) const
virtual int BestBufferSize(void)
void DiscardVideoFrames(bool KeyFrame, bool Flushed)
Places frames in the available frames queue.
Definition: mythplayer.cpp:963
#define VBI_TYPE_VPS
Definition: ivtv_myth.h:276
void DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
int GetReadBufAvail() const
Returns number of bytes available for reading from buffer.
Definition: ringbuffer.cpp:490
PlayerFlags
Definition: mythplayer.h:86
uint8_t * m_audioSamples
long long m_readAdjust
Definition: decoderbase.h:348
virtual void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames)
Definition: decoderbase.cpp:72
static uint64_t samples[4]
Definition: element.c:45
bool onKeyFrameStart(void) const
Definition: H264Parser.h:154
static void GetDecoders(RenderOptions &opts)
#define VBI_TYPE_WSS
Definition: ivtv_myth.h:275
unsigned char * buf
Definition: mythframe.h:139
static int64_t AVF_Seek_Packet(void *opaque, int64_t offset, int whence)
virtual bool IsInDiscMenuOrStillFrame(void) const
QHash< QString, Action * > Context
Definition: action.h:77
int m_currentTrack[kTrackTypeCount]
Definition: decoderbase.h:354
QString toString(MarkTypes type)
< DVB packet
Definition: vbilut.h:24
unsigned long long m_frameCounter
Definition: decoderbase.h:308
VideoFrame * GetNextVideoFrame(void)
Removes a frame from the available queue for decoding onto.
Definition: mythplayer.cpp:914
bool m_exitAfterDecoded
Definition: decoderbase.h:321
virtual void PostProcessFrame(AVCodecContext *, VideoFrame *)
#define LOC
QString get_decoder_name(MythCodecID codec_id)
virtual bool DecoderNeedsReset(AVCodecContext *)
InteractiveTV * m_itv
MHEG/MHP decoder.
MythCodecID
Definition: mythcodecid.h:10
int AutoSelectAudioTrack(void)
Selects the best audio track.
uint32_t QueryTotalDuration(void) const
If present this loads the total duration in milliseconds of the main video stream from recordedmarkup...
uint64_t GetSeekSnap(void) const
Definition: decoderbase.h:140
static void error(const char *str,...)
Definition: vbi.c:42
bool DecodeSubtitles(AVSubtitle *sub, int *gotSubtitles, const uint8_t *spu_pkt, int buf_size, uint32_t startTime)
generate dvd subtitle bitmap or dvd menu bitmap.
bool Type(int i) const
virtual void SetDecoderOptions(AVCodecContext *, AVCodec *)
friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
void ResetTracks(void)
Definition: decoderbase.h:395
bool m_hasFullPositionMap
Definition: decoderbase.h:324
AudioTrackType
Definition: decoderbase.h:55
void RemoveAudioStreams()
remove audio streams from the context used by dvd code during title transitions to remove stale audio...
void SetAudioParams(AudioFormat format, int orig_channels, int channels, AVCodecID codec, int samplerate, bool passthru, int codec_profile=-1)
Set audio output parameters.
AVCodecID m_codecId
VideoFrameType codec
Definition: mythframe.h:138
bool SetVideoByComponentTag(int tag) override
static float get_aspect(const AVCodecContext &ctx)
QString GetCodecDecoderName(void) const override
void SetupAudioStreamSubIndexes(int streamIndex)
Reacts to DUAL/STEREO changes on the fly and fix streams.
QString iso639_key_toName(int iso639_2)
Converts a canonical key to language name in English.
Definition: iso639.cpp:110
CC708Decoder * m_ccd708
int OpenFile(RingBuffer *rbuffer, bool novideo, char testbuf[kDecoderProbeBufferSize], int testbufsize=kDecoderProbeBufferSize) override
Open our file and set up or audio and video parameters.
MarkTypes m_positionMapType
Definition: decoderbase.h:327
int colorrange
Definition: mythframe.h:165
int top_field_first
1 if top field is first.
Definition: mythframe.h:153
struct AVFrame AVFrame
QString GetFilename(void) const
Returns name of file used by this RingBuffer.
virtual bool ProcessVideoPacket(AVStream *stream, AVPacket *pkt, bool &Retry)
void Decode(const unsigned char *buf, int vbimode)
Decodes teletext data.
int directrendering
1 if managed by FFmpeg
Definition: mythframe.h:163
VideoFrameType PixelFormatToFrameType(AVPixelFormat fmt)
Definition: mythavutil.cpp:68
VideoFrameType
Definition: mythframe.h:23
AudioInfo m_audioOut
bool m_getRawFrames
Definition: decoderbase.h:342
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString ReinitAudio(void)
void ForceSetupAudioStream(void) override
QString GetXDS(const QString &key) const override
static int iso639_str3_to_key(const unsigned char *iso639_2)
Definition: iso639.h:63
static bool OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
static int GetMaxReferenceFrames(AVCodecContext *Context)
vector< int > m_languagePreference
language preferences for auto-selection of streams
Definition: decoderbase.h:359
#define SLICE_MIN
#define GOP_START
int interlaced_frame
1 if interlaced.
Definition: mythframe.h:152
virtual int GetTeletextLanguage(uint lang_idx) const
Returns TeleText language.
void DecodeVPS(const unsigned char *buf)
virtual int GetSubtitleLanguage(uint subtitle_index, uint stream_index)
Returns DVD Subtitle language.
static void av_update_stream_timings_video(AVFormatContext *ic)
int iso639_key_to_canonical_key(int iso639_2)
Definition: iso639.cpp:120
static uint planes(VideoFrameType Type)
Definition: mythframe.h:567
static PrivateDecoder * Create(const QString &decoder, PlayerFlags flags, AVCodecContext *avctx)
virtual QString GetName(void)=0
void MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
Preprocess a packet, setting the video parms if necessary.
void freeCodecContext(const AVStream *stream)
Definition: mythavutil.cpp:557
int m_cc608ParityTable[256]
AudioPlayer * m_audio
Definition: decoderbase.h:295
int H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
static bool ReinitBuffer(VideoFrame *Frame, VideoFrameType Type, MythCodecID CodecID, int Width, int Height)
void SetDuration(int duration)
Definition: mythplayer.cpp:696
int rotation
Definition: mythframe.h:171
void UpdateFramesPlayed(void) override
float aspect
Definition: mythframe.h:143
static MythCodecID FindDecoder(const QString &Decoder, AVStream *Stream, AVCodecContext **Context, AVCodec **Codec)
void SetBufferSizeFactors(bool estbitrate, bool matroska)
Tells RingBuffer that the raw bitrate may be innacurate and the underlying container is matroska,...
Definition: ringbuffer.cpp:351
bool GetFrame(DecodeType Type, bool &Retry) override
Demux, preprocess and possibly decode a frame of video/audio.
#define VBI_TYPE_CC
Definition: ivtv_myth.h:274
static int cc608_good_parity(const int *parity_table, uint16_t data)
void UpdateCaptionTracksFromStreams(bool check_608, bool check_708)
long long m_lastCcPtsu
#define MYTH_HEIGHT_ALIGNMENT
Definition: mythframe.h:22
void SetFramesPlayed(uint64_t played)
Definition: mythplayer.cpp:878
void GetInitialStreams(int &audioTag, int &videoTag)
#define FlagIsSet(arg)
Definition: mythplayer.h:103
vector< const unsigned char * > desc_list_t
MythCodecID m_videoCodecId
AVFRingBuffer * m_avfRingBuffer
virtual int FilteredReceiveFrame(AVCodecContext *Context, AVFrame *Frame)
Retrieve and process/filter AVFrame.
void HandleStreamChange(void *data)
virtual void SetEof(bool eof)
Definition: decoderbase.h:134
uint StreamCount(void) const
int m_codecProfile
static LCD * Get(void)
Definition: lcddevice.cpp:65
void GetServices(uint seconds, bool seen[4]) const
bool IsBufferAlmostFull(void)
uint pictureWidthCropped(void) const
void Reset(void)
Definition: H264Parser.cpp:103
MythDeintType deinterlace_inuse
Definition: mythframe.h:175
void SetFileLength(int total, int frames)
Definition: mythplayer.cpp:690
void HandleGopStart(AVPacket *pkt, bool can_reliably_parse_keyframes)
Update our position map, keyframe distance, and the like.
bool HasVideo(const AVFormatContext *ic)
uint32_t m_startCodeState
bool IsDVD(void) const
Holds information on recordings and videos.
Definition: programinfo.h:67
void SetEof(bool eof) override
uint StreamInfoLength(uint i) const
Definition: mpegtables.h:718
uint StreamType(uint i) const
Definition: mpegtables.h:712
ProgramInfo * m_playbackInfo
Definition: decoderbase.h:294
bool DoRewind(long long desiredFrame, bool discardFrames=true) override
int colorprimaries
Definition: mythframe.h:166
bool AddAVSubtitle(AVSubtitle &subtitle, bool fix_position, bool allow_forced)
#define VERBOSE_LEVEL_NONE
Definition: mythlogging.h:22
int colortransfer
Defin