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