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 #include "mythcodeccontext.h"
45 
46 #ifdef USING_VDPAU
47 #include "videoout_vdpau.h"
48 extern "C" {
49 #include "libavcodec/vdpau.h"
50 }
51 #endif // USING_VDPAU
52 
53 #ifdef USING_DXVA2
54 #include "videoout_d3d.h"
55 #endif
56 
57 #ifdef USING_GLVAAPI
58 #include "videoout_openglvaapi.h"
59 #endif // USING_GLVAAPI
60 #ifdef USING_VAAPI
61 #include "vaapicontext.h"
62 #endif
63 
64 #ifdef USING_MEDIACODEC
65 #include "mediacodeccontext.h"
66 extern "C" {
67 #include "libavcodec/jni.h"
68 }
69 #include <QtAndroidExtras>
70 #endif
71 
72 #ifdef USING_VAAPI2
73 #include "vaapi2context.h"
74 #endif
75 
76 #ifdef USING_NVDEC
77 #include "nvdeccontext.h"
78 #endif
79 
80 extern "C" {
81 #include "libavutil/avutil.h"
82 #include "libavutil/error.h"
83 #include "libavutil/log.h"
84 #include "libavcodec/avcodec.h"
85 #include "libavformat/avformat.h"
86 #include "libavformat/avio.h"
87 #include "libavformat/internal.h"
88 #include "libswscale/swscale.h"
89 #include "libavformat/isom.h"
90 #include "ivtv_myth.h"
91 #include "libavutil/imgutils.h"
92 }
93 
94 #ifdef _MSC_VER
95 // MSVC isn't C99 compliant...
96 # ifdef AV_TIME_BASE_Q
97 # undef AV_TIME_BASE_Q
98 # endif
99 #define AV_TIME_BASE_Q GetAVTimeBaseQ()
100 
101 __inline AVRational GetAVTimeBaseQ()
102 {
103  AVRational av = {1, AV_TIME_BASE};
104  return av;
105 }
106 #endif
107 
108 #define LOC QString("AFD: ")
109 
110 // Maximum number of sequential invalid data packet errors
111 // before we try switching to software decoder
112 #define SEQ_PKT_ERR_MAX 10
113 
114 static const int max_video_queue_size = 220;
115 
116 static int cc608_parity(uint8_t byte);
117 static int cc608_good_parity(const int *parity_table, uint16_t data);
118 static void cc608_build_parity_table(int *parity_table);
119 
120 static bool silence_ffmpeg_logging = false;
121 
122 static QSize get_video_dim(const AVCodecContext &ctx)
123 {
124  return {ctx.width >> ctx.lowres, ctx.height >> ctx.lowres};
125 }
126 static float get_aspect(const AVCodecContext &ctx)
127 {
128  float aspect_ratio = 0.0F;
129 
130  if (ctx.sample_aspect_ratio.num && ctx.height)
131  {
132  aspect_ratio = av_q2d(ctx.sample_aspect_ratio) *
133  static_cast<double>(ctx.width);
134  aspect_ratio /= (float) ctx.height;
135  }
136 
137  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
138  {
139  if (ctx.height)
140  aspect_ratio = (float)ctx.width / (float)ctx.height;
141  else
142  aspect_ratio = 4.0F / 3.0F;
143  }
144 
145  return aspect_ratio;
146 }
147 static float get_aspect(H264Parser &p)
148 {
149  static const float default_aspect = 4.0F / 3.0F;
150  int asp = p.aspectRatio();
151  switch (asp)
152  {
153  case 0: return default_aspect;
154  case 2: return 4.0F / 3.0F;
155  case 3: return 16.0F / 9.0F;
156  case 4: return 2.21F;
157  default: break;
158  }
159 
160  float aspect_ratio = asp * 0.000001F;
161  if (aspect_ratio <= 0.0F || aspect_ratio > 6.0F)
162  {
163  if (p.pictureHeight() && p.pictureWidth())
164  {
165  aspect_ratio =
166  (float) p.pictureWidth() /(float) p.pictureHeight();
167  }
168  else
169  {
170  aspect_ratio = default_aspect;
171  }
172  }
173  return aspect_ratio;
174 }
175 
176 
177 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags);
178 void release_avf_buffer(void *opaque, uint8_t *data);
179 #ifdef USING_VDPAU
180 int get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic, int flags);
181 void release_avf_buffer_vdpau(void *opaque, uint8_t *data);
182 int render_wrapper_vdpau(struct AVCodecContext *s, AVFrame *src,
183  const VdpPictureInfo *info,
184  uint32_t count,
185  const VdpBitstreamBuffer *buffers);
186 #endif
187 #ifdef USING_DXVA2
188 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int flags);
189 #endif
190 #ifdef USING_VAAPI
191 int get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic, int flags);
192 #endif
193 #ifdef USING_VAAPI2
194 int get_avf_buffer_vaapi2(struct AVCodecContext *c, AVFrame *pic, int flags);
195 #endif
196 #ifdef USING_NVDEC
197 int get_avf_buffer_nvdec(struct AVCodecContext *c, AVFrame *pic, int flags);
198 #endif
199 
200 static int determinable_frame_size(struct AVCodecContext *avctx)
201 {
202  if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
203  avctx->codec_id == AV_CODEC_ID_MP1 ||
204  avctx->codec_id == AV_CODEC_ID_MP2 ||
205  avctx->codec_id == AV_CODEC_ID_MP3/* ||
206  avctx->codec_id == AV_CODEC_ID_CELT*/)
207  return 1;
208  return 0;
209 }
210 
211 static int has_codec_parameters(AVStream *st)
212 {
213  AVCodecContext *avctx = nullptr;
214 
215 #define FAIL(errmsg) do { \
216  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + (errmsg)); \
217  return 0; \
218 } while (false)
219 
220  switch (st->codecpar->codec_type)
221  {
222  case AVMEDIA_TYPE_AUDIO:
223  avctx = gCodecMap->getCodecContext(st);
224  if (!avctx)
225  FAIL("No codec for audio stream");
226  if (!avctx->frame_size && determinable_frame_size(avctx))
227  FAIL("unspecified frame size");
228  if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
229  FAIL("unspecified sample format");
230  if (!avctx->sample_rate)
231  FAIL("unspecified sample rate");
232  if (!avctx->channels)
233  FAIL("unspecified number of channels");
234  if (!st->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
235  FAIL("no decodable DTS frames");
236  break;
237  case AVMEDIA_TYPE_VIDEO:
238  avctx = gCodecMap->getCodecContext(st);
239  if (!avctx)
240  FAIL("No codec for video stream");
241  if (!avctx->width)
242  FAIL("unspecified size");
243  if (avctx->pix_fmt == AV_PIX_FMT_NONE)
244  FAIL("unspecified pixel format");
245  if (avctx->codec_id == AV_CODEC_ID_RV30 || avctx->codec_id == AV_CODEC_ID_RV40)
246  if (!st->sample_aspect_ratio.num && !avctx->sample_aspect_ratio.num && !st->codec_info_nb_frames)
247  FAIL("no frame in rv30/40 and no sar");
248  break;
249  case AVMEDIA_TYPE_SUBTITLE:
250  if (st->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !st->codecpar->width)
251  FAIL("unspecified size");
252  break;
253  case AVMEDIA_TYPE_DATA:
254  if(st->codecpar->codec_id == AV_CODEC_ID_NONE) return 1;
255  break;
256  default:
257  break;
258  }
259 
260  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
261  FAIL("unknown codec");
262  return 1;
263 }
264 
265 static bool force_sw_decode(AVCodecContext *avctx)
266 {
267  switch (avctx->codec_id)
268  {
269  case AV_CODEC_ID_H264:
270  switch (avctx->profile)
271  {
272  case FF_PROFILE_H264_HIGH_10:
273  case FF_PROFILE_H264_HIGH_10_INTRA:
274  case FF_PROFILE_H264_HIGH_422:
275  case FF_PROFILE_H264_HIGH_422_INTRA:
276  case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
277  case FF_PROFILE_H264_HIGH_444_INTRA:
278  case FF_PROFILE_H264_CAVLC_444:
279  return true;
280  default:
281  break;
282  }
283  break;
284  default:
285  break;
286  }
287  return false;
288 }
289 
290 static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
291 {
293  return;
294 
295  if (VERBOSE_LEVEL_NONE)
296  return;
297 
298  static QString full_line("");
299  static const int msg_len = 255;
300  static QMutex string_lock;
301  uint64_t verbose_mask = VB_LIBAV;
302  LogLevel_t verbose_level = LOG_EMERG;
303 
304  // determine mythtv debug level from av log level
305  switch (level)
306  {
307  case AV_LOG_PANIC:
308  verbose_level = LOG_EMERG;
309  verbose_mask |= VB_GENERAL;
310  break;
311  case AV_LOG_FATAL:
312  verbose_level = LOG_CRIT;
313  verbose_mask |= VB_GENERAL;
314  break;
315  case AV_LOG_ERROR:
316  verbose_level = LOG_ERR;
317  break;
318  case AV_LOG_WARNING:
319  verbose_level = LOG_WARNING;
320  break;
321  case AV_LOG_INFO:
322  verbose_level = LOG_INFO;
323  break;
324  case AV_LOG_VERBOSE:
325  case AV_LOG_DEBUG:
326  case AV_LOG_TRACE:
327  verbose_level = LOG_DEBUG;
328  break;
329  default:
330  return;
331  }
332 
333  if (!VERBOSE_LEVEL_CHECK(verbose_mask, verbose_level))
334  return;
335 
336  string_lock.lock();
337  if (full_line.isEmpty() && ptr) {
338  AVClass* avc = *(AVClass**)ptr;
339  full_line = QString("[%1 @ %2] ")
340  .arg(avc->item_name(ptr))
341  .arg((quintptr)avc, QT_POINTER_SIZE * 2, 16, QChar('0'));
342  }
343 
344  char str[msg_len+1];
345  int bytes = vsnprintf(str, msg_len+1, fmt, vl);
346 
347  // check for truncated messages and fix them
348  if (bytes > msg_len)
349  {
350  LOG(VB_GENERAL, LOG_WARNING,
351  QString("Libav log output truncated %1 of %2 bytes written")
352  .arg(msg_len).arg(bytes));
353  str[msg_len-1] = '\n';
354  }
355 
356  full_line += QString(str);
357  if (full_line.endsWith("\n"))
358  {
359  LOG(verbose_mask, verbose_level, full_line.trimmed());
360  full_line.truncate(0);
361  }
362  string_lock.unlock();
363 }
364 
365 static int get_canonical_lang(const char *lang_cstr)
366 {
367  if (lang_cstr[0] == '\0' || lang_cstr[1] == '\0')
368  {
369  return iso639_str3_to_key("und");
370  }
371  if (lang_cstr[2] == '\0')
372  {
373  QString tmp2 = lang_cstr;
374  QString tmp3 = iso639_str2_to_str3(tmp2);
375  int lang = iso639_str3_to_key(tmp3);
376  return iso639_key_to_canonical_key(lang);
377  }
378  int lang = iso639_str3_to_key(lang_cstr);
379  return iso639_key_to_canonical_key(lang);
380 }
381 
383 {
384  opts.decoders->append("ffmpeg");
385  (*opts.equiv_decoders)["ffmpeg"].append("nuppel");
386  (*opts.equiv_decoders)["ffmpeg"].append("dummy");
387 
388 #ifdef USING_VDPAU
389  opts.decoders->append("vdpau");
390  (*opts.equiv_decoders)["vdpau"].append("dummy");
391 #endif
392 #ifdef USING_DXVA2
393  opts.decoders->append("dxva2");
394  (*opts.equiv_decoders)["dxva2"].append("dummy");
395 #endif
396 
397 #ifdef USING_VAAPI
398  opts.decoders->append("vaapi");
399  (*opts.equiv_decoders)["vaapi"].append("dummy");
400 #endif
401 #ifdef USING_VAAPI2
402  opts.decoders->append("vaapi2");
403  (*opts.equiv_decoders)["vaapi2"].append("dummy");
404 #endif
405 #ifdef USING_NVDEC
406  opts.decoders->append("nvdec");
407  (*opts.equiv_decoders)["nvdec"].append("dummy");
408 #endif
409 #ifdef USING_MEDIACODEC
410  opts.decoders->append("mediacodec");
411  (*opts.equiv_decoders)["mediacodec"].append("dummy");
412 #endif
413 
415 }
416 
418  const ProgramInfo &pginfo,
419  PlayerFlags flags)
420  : DecoderBase(parent, pginfo),
421  m_is_db_ignored(gCoreContext->IsDatabaseIgnored()),
422  m_h264_parser(new H264Parser()),
423  playerFlags(flags),
424  // Closed Caption & Teletext decoders
425  m_ccd608(new CC608Decoder(parent->GetCC608Reader())),
426  m_ccd708(new CC708Decoder(parent->GetCC708Reader())),
427  m_ttd(new TeletextDecoder(parent->GetTeletextReader()))
428 {
429  memset(&m_readcontext, 0, sizeof(m_readcontext));
430  memset(m_ccX08_in_pmt, 0, sizeof(m_ccX08_in_pmt));
431  memset(m_ccX08_in_tracks, 0, sizeof(m_ccX08_in_tracks));
432 
433  m_audioSamples = (uint8_t *)av_mallocz(AudioOutput::MAX_SIZE_BUFFER);
435 
436  av_log_set_callback(myth_av_log);
437 
438  m_audioIn.sample_size = -32; // force SetupAudioStream to run once
440 
442 
444  m_audioReadAhead = gCoreContext->GetNumSetting("AudioReadAhead", 100);
445 
446  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("PlayerFlags: 0x%1, AudioReadAhead: %2 msec")
447  .arg(playerFlags, 0, 16).arg(m_audioReadAhead));
448 }
449 
451 {
452  while (!m_storedPackets.isEmpty())
453  {
454  AVPacket *pkt = m_storedPackets.takeFirst();
455  av_packet_unref(pkt);
456  delete pkt;
457  }
458 
459  CloseContext();
460  delete m_ccd608;
461  delete m_ccd708;
462  delete m_ttd;
463  delete m_private_dec;
464  delete m_h264_parser;
465  delete m_mythcodecctx;
466 
467  sws_freeContext(m_sws_ctx);
468 
469  av_freep(&m_audioSamples);
470 
471  delete m_avfRingBuffer;
472 
473  if (LCD *lcd = LCD::Get())
474  {
475  lcd->setAudioFormatLEDs(AUDIO_AC3, false);
476  lcd->setVideoFormatLEDs(VIDEO_MPG, false);
477  lcd->setVariousLEDs(VARIOUS_HDTV, false);
478  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
479  lcd->setSpeakerLEDs(SPEAKER_71, false); // should clear any and all speaker LEDs
480  }
481 }
482 
484 {
485  if (m_ic)
486  {
487  for (uint i = 0; i < m_ic->nb_streams; i++)
488  {
489  QMutexLocker locker(avcodeclock);
490  AVStream *st = m_ic->streams[i];
492  }
493  }
494 }
495 
497 {
498  if (m_ic)
499  {
500  CloseCodecs();
501 
502  AVInputFormat *fmt = m_ic->iformat;
503  m_ic->iformat->flags |= AVFMT_NOFILE;
504 
505  av_free(m_ic->pb->buffer);
506  av_free(m_ic->pb);
507  avformat_close_input(&m_ic);
508  m_ic = nullptr;
509  fmt->flags &= ~AVFMT_NOFILE;
510  }
511 
512  delete m_private_dec;
513  m_private_dec = nullptr;
514  m_h264_parser->Reset();
515 }
516 
517 static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
518 {
519  int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
520  return ((lsb - base_ts)&mask);
521 }
522 
524 {
525  int64_t start_pts = 0, pts;
526 
527  AVStream *st = nullptr;
528  for (uint i = 0; i < m_ic->nb_streams; i++)
529  {
530  AVStream *st1 = m_ic->streams[i];
531  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
532  {
533  st = st1;
534  break;
535  }
536  }
537  if (!st)
538  return 0;
539 
540  if (m_ic->start_time != AV_NOPTS_VALUE)
541  start_pts = av_rescale(m_ic->start_time,
542  st->time_base.den,
543  AV_TIME_BASE * (int64_t)st->time_base.num);
544 
545  pts = av_rescale(timecode / 1000.0,
546  st->time_base.den,
547  st->time_base.num);
548 
549  // adjust for start time and wrap
550  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
551 
552  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
553 }
554 
556  int64_t timecode)
557 {
558  int64_t start_pts = 0, pts;
559 
560  if (m_ic->start_time != AV_NOPTS_VALUE)
561  start_pts = av_rescale(m_ic->start_time,
562  st->time_base.den,
563  AV_TIME_BASE * (int64_t)st->time_base.num);
564 
565  pts = av_rescale(timecode / 1000.0,
566  st->time_base.den,
567  st->time_base.num);
568 
569  // adjust for start time and wrap
570  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
571 
572  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
573 }
574 
576 {
577  if (m_ic && m_ic->nb_chapters > 1)
578  return m_ic->nb_chapters;
579  return 0;
580 }
581 
582 void AvFormatDecoder::GetChapterTimes(QList<long long> &times)
583 {
584  int total = GetNumChapters();
585  if (!total)
586  return;
587 
588  for (int i = 0; i < total; i++)
589  {
590  int num = m_ic->chapters[i]->time_base.num;
591  int den = m_ic->chapters[i]->time_base.den;
592  int64_t start = m_ic->chapters[i]->start;
593  long double total_secs = (long double)start * (long double)num /
594  (long double)den;
595  times.push_back((long long)total_secs);
596  }
597 }
598 
599 int AvFormatDecoder::GetCurrentChapter(long long framesPlayed)
600 {
601  if (!GetNumChapters())
602  return 0;
603 
604  for (int i = (m_ic->nb_chapters - 1); i > -1 ; i--)
605  {
606  int num = m_ic->chapters[i]->time_base.num;
607  int den = m_ic->chapters[i]->time_base.den;
608  int64_t start = m_ic->chapters[i]->start;
609  long double total_secs = (long double)start * (long double)num /
610  (long double)den;
611  long long framenum = (long long)(total_secs * m_fps);
612  if (framesPlayed >= framenum)
613  {
614  LOG(VB_PLAYBACK, LOG_INFO, LOC +
615  QString("GetCurrentChapter(selected chapter %1 framenum %2)")
616  .arg(i + 1).arg(framenum));
617  return i + 1;
618  }
619  }
620  return 0;
621 }
622 
623 long long AvFormatDecoder::GetChapter(int chapter)
624 {
625  if (chapter < 1 || chapter > GetNumChapters())
626  return -1;
627 
628  int num = m_ic->chapters[chapter - 1]->time_base.num;
629  int den = m_ic->chapters[chapter - 1]->time_base.den;
630  int64_t start = m_ic->chapters[chapter - 1]->start;
631  long double total_secs = (long double)start * (long double)num /
632  (long double)den;
633  long long framenum = (long long)(total_secs * m_fps);
634  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("GetChapter %1: framenum %2")
635  .arg(chapter).arg(framenum));
636  return framenum;
637 }
638 
639 bool AvFormatDecoder::DoRewind(long long desiredFrame, bool discardFrames)
640 {
641  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("DoRewind(%1, %2 discard frames)")
642  .arg(desiredFrame).arg( discardFrames ? "do" : "don't" ));
643 
645  return DecoderBase::DoRewind(desiredFrame, discardFrames);
646 
647  m_dorewind = true;
648 
649  // avformat-based seeking
650  return DoFastForward(desiredFrame, discardFrames);
651 }
652 
653 bool AvFormatDecoder::DoFastForward(long long desiredFrame, bool discardFrames)
654 {
655  LOG(VB_PLAYBACK, LOG_INFO, LOC +
656  QString("DoFastForward(%1 (%2), %3 discard frames)")
657  .arg(desiredFrame).arg(m_framesPlayed)
658  .arg((discardFrames) ? "do" : "don't"));
659 
661  return DecoderBase::DoFastForward(desiredFrame, discardFrames);
662 
663  bool oldrawstate = m_getrawframes;
664  m_getrawframes = false;
665 
666  AVStream *st = nullptr;
667  for (uint i = 0; i < m_ic->nb_streams; i++)
668  {
669  AVStream *st1 = m_ic->streams[i];
670  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
671  {
672  st = st1;
673  break;
674  }
675  }
676 
677  int seekDelta = desiredFrame - m_framesPlayed;
678 
679  // avoid using av_frame_seek if we are seeking frame-by-frame when paused
680  if (seekDelta >= 0 && seekDelta < 2 && !m_dorewind && m_parent->GetPlaySpeed() == 0.0F)
681  {
682  SeekReset(m_framesPlayed, seekDelta, false, true);
684  m_getrawframes = oldrawstate;
685  return true;
686  }
687 
688  long long ts = 0;
689  if (m_ic->start_time != AV_NOPTS_VALUE)
690  ts = m_ic->start_time;
691 
692  // convert framenumber to normalized timestamp
693  long double seekts = desiredFrame * AV_TIME_BASE / m_fps;
694  ts += (long long)seekts;
695 
696  // XXX figure out how to do snapping in this case
697  bool exactseeks = DecoderBase::GetSeekSnap() == 0U;
698 
699  int flags = (m_dorewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
700 
701  if (av_seek_frame(m_ic, -1, ts, flags) < 0)
702  {
703  LOG(VB_GENERAL, LOG_ERR, LOC +
704  QString("av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
705  m_getrawframes = oldrawstate;
706  return false;
707  }
708 
709  int normalframes = 0;
710 
711  if (st && st->cur_dts != AV_NOPTS_VALUE)
712  {
713 
714  int64_t adj_cur_dts = st->cur_dts;
715 
716  if (m_ic->start_time != AV_NOPTS_VALUE)
717  {
718  int64_t st1 = av_rescale(m_ic->start_time,
719  st->time_base.den,
720  AV_TIME_BASE * (int64_t)st->time_base.num);
721  adj_cur_dts = lsb3full(adj_cur_dts, st1, st->pts_wrap_bits);
722  }
723 
724  int64_t adj_seek_dts = av_rescale(seekts,
725  st->time_base.den,
726  AV_TIME_BASE * (int64_t)st->time_base.num);
727 
728  int64_t max_dts = (st->pts_wrap_bits < 64) ? (1LL<<st->pts_wrap_bits)-1 : -1LL;
729 
730  // When seeking near the start of a stream the current dts is sometimes
731  // less than the start time which causes lsb3full to return adj_cur_dts
732  // close to the maximum dts value. If so, set adj_cur_dts to zero.
733  if (adj_seek_dts < max_dts / 64 && adj_cur_dts > max_dts / 2)
734  adj_cur_dts = 0;
735 
736  long long newts = av_rescale(adj_cur_dts,
737  (int64_t)AV_TIME_BASE *
738  (int64_t)st->time_base.num,
739  st->time_base.den);
740 
741  m_lastKey = (long long)((newts*(long double)m_fps)/AV_TIME_BASE);
743  m_fpsSkip = 0;
745 
746  normalframes = (exactseeks) ? desiredFrame - m_framesPlayed : 0;
747  normalframes = max(normalframes, 0);
748  m_no_dts_hack = false;
749  }
750  else
751  {
752  LOG(VB_GENERAL, LOG_INFO, LOC + "No DTS Seeking Hack!");
753  m_no_dts_hack = true;
754  m_framesPlayed = desiredFrame;
755  m_fpsSkip = 0;
756  m_framesRead = desiredFrame;
757  normalframes = 0;
758  }
759 
760  SeekReset(m_lastKey, normalframes, true, discardFrames);
761 
762  if (discardFrames)
764 
765  m_dorewind = false;
766 
767  m_getrawframes = oldrawstate;
768 
769  return true;
770 }
771 
772 void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
773  bool doflush, bool discardFrames)
774 {
775  if (!ringBuffer)
776  return; // nothing to reset...
777 
778  LOG(VB_PLAYBACK, LOG_INFO, LOC +
779  QString("SeekReset(%1, %2, %3 flush, %4 discard)")
780  .arg(newKey).arg(skipFrames)
781  .arg((doflush) ? "do" : "don't")
782  .arg((discardFrames) ? "do" : "don't"));
783 
784  DecoderBase::SeekReset(newKey, skipFrames, doflush, discardFrames);
785 
786  QMutexLocker locker(avcodeclock);
787 
788  if (doflush)
789  {
790  m_lastapts = 0;
791  m_lastvpts = 0;
792  m_lastccptsu = 0;
796  m_pts_detected = false;
797  m_reordered_pts_detected = false;
798 
799  ff_read_frame_flush(m_ic);
800 
801  // Only reset the internal state if we're using our seeking,
802  // not when using libavformat's seeking
804  {
805  m_ic->pb->pos = ringBuffer->GetReadPosition();
806  m_ic->pb->buf_ptr = m_ic->pb->buffer;
807  m_ic->pb->buf_end = m_ic->pb->buffer;
808  m_ic->pb->eof_reached = 0;
809  }
810 
811  // Flush the avcodec buffers
812  LOG(VB_PLAYBACK, LOG_INFO, LOC + "SeekReset() flushing");
813  for (uint i = 0; i < m_ic->nb_streams; i++)
814  {
815  AVCodecContext *enc = gCodecMap->hasCodecContext(m_ic->streams[i]);
816  // note that contexts that have not been opened have
817  // enc->internal = nullptr and cause a segfault in
818  // avcodec_flush_buffers
819  if (enc && enc->internal)
820  avcodec_flush_buffers(enc);
821  }
822  if (m_private_dec)
823  m_private_dec->Reset();
824  }
825 
826  // Discard all the queued up decoded frames
827  if (discardFrames)
828  m_parent->DiscardVideoFrames(doflush);
829 
830  if (doflush)
831  {
832  // Free up the stored up packets
833  while (!m_storedPackets.isEmpty())
834  {
835  AVPacket *pkt = m_storedPackets.takeFirst();
836  av_packet_unref(pkt);
837  delete pkt;
838  }
839 
840  m_prevgoppos = 0;
841  m_gopset = false;
842  if (!ringBuffer->IsDVD())
843  {
844  if (!m_no_dts_hack)
845  {
847  m_fpsSkip = 0;
849  }
850 
851  m_no_dts_hack = false;
852  }
853  }
854 
855  // Skip all the desired number of skipFrames
856 
857  // Some seeks can be very slow. The most common example comes
858  // from HD-PVR recordings, where keyframes are 128 frames apart
859  // and decoding (even hardware decoding) may not be much faster
860  // than realtime, causing some exact seeks to take 2-4 seconds.
861  // If exact seeking is not required, we take some shortcuts.
862  // First, we impose an absolute maximum time we are willing to
863  // spend (maxSeekTimeMs) on the forward frame-by-frame skip.
864  // After that much time has elapsed, we give up and stop the
865  // frame-by-frame seeking. Second, after skipping a few frames,
866  // we predict whether the situation is hopeless, i.e. the total
867  // skipping would take longer than giveUpPredictionMs, and if so,
868  // stop skipping right away.
869  bool exactSeeks = GetSeekSnap() == 0U;
870  const int maxSeekTimeMs = 200;
871  int profileFrames = 0;
873  for (; (skipFrames > 0 && !m_ateof &&
874  (exactSeeks || begin.elapsed() < maxSeekTimeMs));
875  --skipFrames, ++profileFrames)
876  {
879  {
881  m_decoded_video_frame = nullptr;
882  }
883  if (!exactSeeks && profileFrames >= 5 && profileFrames < 10)
884  {
885  const int giveUpPredictionMs = 400;
886  int remainingTimeMs =
887  skipFrames * (float)begin.elapsed() / profileFrames;
888  if (remainingTimeMs > giveUpPredictionMs)
889  {
890  LOG(VB_PLAYBACK, LOG_DEBUG,
891  QString("Frame-by-frame seeking would take "
892  "%1 ms to finish, skipping.").arg(remainingTimeMs));
893  break;
894  }
895  }
896  }
897 
898  if (doflush)
899  {
900  m_firstvpts = 0;
901  m_firstvptsinuse = true;
902  }
903 }
904 
906 {
907  if (!eof && m_ic && m_ic->pb)
908  {
909  LOG(VB_GENERAL, LOG_NOTICE, LOC +
910  QString("Resetting byte context eof (livetv %1 was eof %2)")
911  .arg(m_livetv).arg(m_ic->pb->eof_reached));
912  m_ic->pb->eof_reached = 0;
913  }
914  DecoderBase::SetEof(eof);
915 }
916 
917 void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset,
918  bool reset_file)
919 {
920  LOG(VB_PLAYBACK, LOG_INFO, LOC +
921  QString("Reset: Video %1, Seek %2, File %3")
922  .arg(reset_video_data).arg(seek_reset).arg(reset_file));
923 
924  if (seek_reset)
925  SeekReset(0, 0, true, false);
926 
927  DecoderBase::Reset(reset_video_data, false, reset_file);
928 
929  if (reset_video_data)
930  {
931  m_seen_gop = false;
932  m_seq_count = 0;
933  }
934 }
935 
937  const QString &filename, int testbufsize)
938 {
939  AVProbeData probe;
940  memset(&probe, 0, sizeof(AVProbeData));
941 
942  QByteArray fname = filename.toLatin1();
943  probe.filename = fname.constData();
944  probe.buf = (unsigned char *)testbuf;
945  probe.buf_size = testbufsize;
946 
947  int score = AVPROBE_SCORE_MAX/4;
948 
949  if (testbufsize + AVPROBE_PADDING_SIZE > kDecoderProbeBufferSize)
950  {
951  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
952  score = 0;
953  }
954  else if (testbufsize*2 >= kDecoderProbeBufferSize)
955  {
956  score--;
957  }
958 
959  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
960 
961  return av_probe_input_format2(&probe, static_cast<int>(true), &score) != nullptr;
962 }
963 
965 {
966  int buf_size = ringBuffer->BestBufferSize();
967  int streamed = ringBuffer->IsStreamed();
969  m_readcontext.flags = AVIO_FLAG_READ;
970  m_readcontext.is_streamed = streamed;
971  m_readcontext.max_packet_size = 0;
972  m_readcontext.priv_data = m_avfRingBuffer;
973  unsigned char* buffer = (unsigned char *)av_malloc(buf_size);
974  m_ic->pb = avio_alloc_context(buffer, buf_size, 0,
975  &m_readcontext,
979 
980  // We can always seek during LiveTV
981  m_ic->pb->seekable = !streamed || forceseek;
982  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Buffer size: %1 streamed %2 seekable %3")
983  .arg(buf_size).arg(streamed).arg(m_ic->pb->seekable));
984 }
985 
986 extern "C" void HandleStreamChange(void *data)
987 {
988  AvFormatDecoder *decoder =
989  reinterpret_cast<AvFormatDecoder*>(data);
990 
991  int cnt = decoder->m_ic->nb_streams;
992 
993  LOG(VB_PLAYBACK, LOG_INFO, LOC +
994  QString("streams_changed 0x%1 -- stream count %2")
995  .arg((uint64_t)data,0,16).arg(cnt));
996 
997  decoder->m_streams_changed = true;
998 }
999 
1001 {
1002  QMutexLocker lock(avcodeclock);
1003  int retval = avformat_find_stream_info(m_ic, nullptr);
1004  silence_ffmpeg_logging = false;
1005  // ffmpeg 3.0 is returning -1 code when there is a channel
1006  // change or some encoding error just after the start
1007  // of the file, but is has found the correct stream info
1008  // Set rc to 0 so that playing can continue.
1009  if (retval == -1)
1010  retval = 0;
1011  return retval;
1012 }
1013 
1028 int AvFormatDecoder::OpenFile(RingBuffer *rbuffer, bool novideo,
1029  char testbuf[kDecoderProbeBufferSize],
1030  int testbufsize)
1031 {
1032  CloseContext();
1033 
1034  ringBuffer = rbuffer;
1035 
1036  // Process frames immediately unless we're decoding
1037  // a DVD, in which case don't so that we don't show
1038  // anything whilst probing the data streams.
1040 
1041  delete m_avfRingBuffer;
1042  m_avfRingBuffer = new AVFRingBuffer(rbuffer);
1043 
1044  AVInputFormat *fmt = nullptr;
1045  QString fnames = ringBuffer->GetFilename();
1046  QByteArray fnamea = fnames.toLatin1();
1047  const char *filename = fnamea.constData();
1048 
1049  AVProbeData probe;
1050  memset(&probe, 0, sizeof(AVProbeData));
1051  probe.filename = filename;
1052  probe.buf = (unsigned char *)testbuf;
1053  if (testbufsize + AVPROBE_PADDING_SIZE <= kDecoderProbeBufferSize)
1054  probe.buf_size = testbufsize;
1055  else
1056  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
1057 
1058  memset(probe.buf + probe.buf_size, 0, AVPROBE_PADDING_SIZE);
1059 
1060  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + "OpenFile -- begin");
1061 
1062  fmt = av_probe_input_format(&probe, static_cast<int>(true));
1063  if (!fmt)
1064  {
1065  LOG(VB_GENERAL, LOG_ERR, LOC +
1066  QString("Probe failed for file: \"%1\".").arg(filename));
1067  return -1;
1068  }
1069 
1070  if (strcmp(fmt->name, "mpegts") == 0 &&
1071  gCoreContext->GetBoolSetting("FFMPEGTS", false))
1072  {
1073  AVInputFormat *fmt2 = av_find_input_format("mpegts-ffmpeg");
1074  if (fmt2)
1075  {
1076  fmt = fmt2;
1077  LOG(VB_GENERAL, LOG_INFO, LOC + "Using FFmpeg MPEG-TS demuxer (forced)");
1078  }
1079  }
1080 
1081  int err = 0;
1082  bool found = false;
1083  bool scanned = false;
1084 
1085  if (m_livetv)
1086  {
1087  // We try to open the file for up to 1.5 second using only buffer in memory
1088  MythTimer timer; timer.start();
1089 
1090  m_avfRingBuffer->SetInInit(true);
1091 
1092  while (!found && timer.elapsed() < 1500)
1093  {
1094  m_ic = avformat_alloc_context();
1095  if (!m_ic)
1096  {
1097  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
1098  return -1;
1099  }
1100 
1101  InitByteContext(true);
1102 
1103  err = avformat_open_input(&m_ic, filename, fmt, nullptr);
1104  if (err < 0)
1105  {
1106  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1107  QString("avformat_open_input failed for in ram data after %1ms, retrying in 50ms")
1108  .arg(timer.elapsed()));
1109  usleep(50 * 1000); // wait 50ms
1110  continue;
1111  }
1112 
1113  // Test if we can find all streams details in what has been found so far
1114  if (FindStreamInfo() < 0)
1115  {
1116  avformat_close_input(&m_ic);
1117  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1118  QString("FindStreamInfo failed for in ram data after %1ms, retrying in 50ms")
1119  .arg(timer.elapsed()));
1120  usleep(50 * 1000); // wait 50ms
1121  continue;
1122  }
1123 
1124  found = true;
1125 
1126  for (uint i = 0; i < m_ic->nb_streams; i++)
1127  {
1128  if (!has_codec_parameters(m_ic->streams[i]))
1129  {
1130  avformat_close_input(&m_ic);
1131  found = false;
1132  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1133  QString("Invalid streams found in ram data after %1ms, retrying in 50ms")
1134  .arg(timer.elapsed()));
1135  usleep(50 * 1000); // wait 50ms
1136  break;
1137  }
1138  }
1139  }
1140 
1141  m_avfRingBuffer->SetInInit(false);
1142 
1143  if (found)
1144  {
1145  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1146  QString("File successfully opened after %1ms")
1147  .arg(timer.elapsed()));
1148  }
1149  else
1150  {
1151  LOG(VB_PLAYBACK, LOG_WARNING, LOC +
1152  QString("No streams found in ram data after %1ms, defaulting to in-file")
1153  .arg(timer.elapsed()));
1154  }
1155  scanned = found;
1156  }
1157 
1158  // If we haven't opened the file so far, revert to old method
1159  while (!found)
1160  {
1161  m_ic = avformat_alloc_context();
1162  if (!m_ic)
1163  {
1164  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
1165  return -1;
1166  }
1167 
1168  InitByteContext();
1169 
1170  err = avformat_open_input(&m_ic, filename, fmt, nullptr);
1171  if (err < 0)
1172  {
1173  if (strcmp(fmt->name, "mpegts") == 0)
1174  {
1175  fmt = av_find_input_format("mpegts-ffmpeg");
1176  if (fmt)
1177  {
1178  LOG(VB_GENERAL, LOG_ERR, LOC +
1179  QString("avformat_open_input failed with '%1' error.").arg(err));
1180  LOG(VB_GENERAL, LOG_ERR, LOC +
1181  QString("Attempting using original FFmpeg MPEG-TS demuxer."));
1182  // ic would have been freed due to the earlier failure
1183  continue;
1184  }
1185  break;
1186  }
1187  }
1188  found = true;
1189  }
1190  if (err < 0)
1191  {
1192  LOG(VB_GENERAL, LOG_ERR, LOC +
1193  QString("avformat err(%1) on avformat_open_input call.").arg(err));
1194  m_ic = nullptr;
1195  return -1;
1196  }
1197 
1198  if (!scanned)
1199  {
1200  int ret = FindStreamInfo();
1201  if (ret < 0)
1202  {
1203  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not find codec parameters. " +
1204  QString("file was \"%1\".").arg(filename));
1205  avformat_close_input(&m_ic);
1206  m_ic = nullptr;
1207  return -1;
1208  }
1209  }
1210 
1211  m_ic->streams_changed = HandleStreamChange;
1212  m_ic->stream_change_data = this;
1213 
1214  fmt->flags &= ~AVFMT_NOFILE;
1215 
1216  if (!m_livetv && !ringBuffer->IsDisc())
1217  {
1218  // generate timings based on the video stream to avoid bogus ffmpeg
1219  // values for duration and bitrate
1221  }
1222 
1223  // FLAC, MP3 or M4A file may contains an artwork image, a single frame MJPEG,
1224  // we need to ignore it as we don't handle single frames or images in place of video
1225  // TODO: display single frame
1226  QString extension = QFileInfo(fnames).suffix();
1227  if (strcmp(fmt->name, "mp3") == 0 || strcmp(fmt->name, "flac") == 0 ||
1228  strcmp(fmt->name, "ogg") == 0 ||
1229  (extension.compare("m4a", Qt::CaseInsensitive) == 0))
1230  {
1231  novideo = true;
1232  }
1233 
1234  // Scan for the initial A/V streams
1235  int ret = ScanStreams(novideo);
1236  if (-1 == ret)
1237  return ret;
1238 
1239  AutoSelectTracks(); // This is needed for transcoder
1240 
1241 #ifdef USING_MHEG
1242  {
1243  int initialAudio = -1, initialVideo = -1;
1244  if (m_itv || (m_itv = m_parent->GetInteractiveTV()))
1245  m_itv->GetInitialStreams(initialAudio, initialVideo);
1246  if (initialAudio >= 0)
1247  SetAudioByComponentTag(initialAudio);
1248  if (initialVideo >= 0)
1249  SetVideoByComponentTag(initialVideo);
1250  }
1251 #endif // USING_MHEG
1252 
1253  // Try to get a position map from the recorder if we don't have one yet.
1255  {
1257  {
1260  {
1261  m_hasFullPositionMap = true;
1262  m_gopset = true;
1263  }
1264  }
1265  }
1266 
1267  // If watching pre-recorded television or video use the marked duration
1268  // from the db if it exists, else ffmpeg duration
1269  int64_t dur = 0;
1270 
1271  if (m_playbackinfo)
1272  {
1274  dur /= 1000;
1275  }
1276 
1277  if (dur == 0)
1278  {
1279  if ((m_ic->duration == AV_NOPTS_VALUE) &&
1280  (!m_livetv && !ringBuffer->IsDisc()))
1281  av_estimate_timings(m_ic, 0);
1282 
1283  dur = m_ic->duration / (int64_t)AV_TIME_BASE;
1284  }
1285 
1286  if (dur > 0 && !m_livetv && !m_watchingrecording)
1287  {
1288  m_parent->SetDuration((int)dur);
1289  }
1290 
1291  // If we don't have a position map, set up ffmpeg for seeking
1293  {
1294  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1295  "Recording has no position -- using libavformat seeking.");
1296 
1297  if (dur > 0)
1298  {
1299  m_parent->SetFileLength((int)(dur), (int)(dur * m_fps));
1300  }
1301  else
1302  {
1303  // the pvr-250 seems to over report the bitrate by * 2
1304  float bytespersec = (float)m_bitrate / 8 / 2;
1305  float secs = ringBuffer->GetRealFileSize() * 1.0F / bytespersec;
1306  m_parent->SetFileLength((int)(secs),
1307  (int)(secs * static_cast<float>(m_fps)));
1308  }
1309 
1310  // we will not see a position map from db or remote encoder,
1311  // set the gop interval to 15 frames. if we guess wrong, the
1312  // auto detection will change it.
1313  m_keyframedist = 15;
1315 
1316  if (strcmp(fmt->name, "avi") == 0)
1317  {
1318  // avi keyframes are too irregular
1319  m_keyframedist = 1;
1320  }
1321 
1322  m_dontSyncPositionMap = true;
1323  m_ic->build_index = 1;
1324  }
1325  // we have a position map, disable libavformat's seek index
1326  else
1327  m_ic->build_index = 0;
1328 
1329  av_dump_format(m_ic, 0, filename, 0);
1330 
1331  // print some useful information if playback debugging is on
1333  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Position map found");
1334  else if (m_recordingHasPositionMap)
1335  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Partial position map found");
1336  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1337  QString("Successfully opened decoder for file: \"%1\". novideo(%2)")
1338  .arg(filename).arg(novideo));
1339 
1340  // Print AVChapter information
1341  for (unsigned int i=0; i < m_ic->nb_chapters; i++)
1342  {
1343  int num = m_ic->chapters[i]->time_base.num;
1344  int den = m_ic->chapters[i]->time_base.den;
1345  int64_t start = m_ic->chapters[i]->start;
1346  long double total_secs = (long double)start * (long double)num /
1347  (long double)den;
1348  int hours = (int)total_secs / 60 / 60;
1349  int minutes = ((int)total_secs / 60) - (hours * 60);
1350  double secs = (double)total_secs -
1351  (double)(hours * 60 * 60 + minutes * 60);
1352  long long framenum = (long long)(total_secs * m_fps);
1353  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1354  QString("Chapter %1 found @ [%2:%3:%4]->%5")
1355  .arg(QString().sprintf("%02d", i + 1))
1356  .arg(QString().sprintf("%02d", hours))
1357  .arg(QString().sprintf("%02d", minutes))
1358  .arg(QString().sprintf("%06.3f", secs))
1359  .arg(framenum));
1360  }
1361 
1362  if (getenv("FORCE_DTS_TIMESTAMPS"))
1363  m_force_dts_timestamps = true;
1364 
1365  if (ringBuffer->IsDVD())
1366  {
1367  // Reset DVD playback and clear any of
1368  // our buffers so that none of the data
1369  // parsed so far to determine decoders
1370  // gets shown.
1372  return -1;
1373  ringBuffer->IgnoreWaitStates(false);
1374 
1375  Reset(true, true, true);
1376 
1377  // Now we're ready to process and show frames
1378  m_processFrames = true;
1379  }
1380 
1381 
1382  // Return true if recording has position map
1384 }
1385 
1386 float AvFormatDecoder::normalized_fps(AVStream *stream, AVCodecContext *enc)
1387 {
1388  float fps, avg_fps, codec_fps, container_fps, estimated_fps;
1389  avg_fps = codec_fps = container_fps = estimated_fps = 0.0F;
1390 
1391  if (stream->avg_frame_rate.den && stream->avg_frame_rate.num)
1392  avg_fps = av_q2d(stream->avg_frame_rate); // MKV default_duration
1393 
1394  if (enc->time_base.den && enc->time_base.num) // tbc
1395  codec_fps = 1.0 / av_q2d(enc->time_base) / enc->ticks_per_frame;
1396  // Some formats report fps waaay too high. (wrong time_base)
1397  if (codec_fps > 121.0F && (enc->time_base.den > 10000) &&
1398  (enc->time_base.num == 1))
1399  {
1400  enc->time_base.num = 1001; // seems pretty standard
1401  if (av_q2d(enc->time_base) > 0)
1402  codec_fps = 1.0 / av_q2d(enc->time_base);
1403  }
1404  if (stream->time_base.den && stream->time_base.num) // tbn
1405  container_fps = 1.0 / av_q2d(stream->time_base);
1406  if (stream->r_frame_rate.den && stream->r_frame_rate.num) // tbr
1407  estimated_fps = av_q2d(stream->r_frame_rate);
1408 
1409  // matroska demuxer sets the default_duration to avg_frame_rate
1410  // mov,mp4,m4a,3gp,3g2,mj2 demuxer sets avg_frame_rate
1411  if ((QString(m_ic->iformat->name).contains("matroska") ||
1412  QString(m_ic->iformat->name).contains("mov")) &&
1413  avg_fps < 121.0F && avg_fps > 3.0F)
1414  fps = avg_fps;
1415  else if (QString(m_ic->iformat->name).contains("avi") &&
1416  container_fps < 121.0F && container_fps > 3.0F)
1417  fps = container_fps; // avi uses container fps for timestamps
1418  else if (codec_fps < 121.0F && codec_fps > 3.0F)
1419  fps = codec_fps;
1420  else if (container_fps < 121.0F && container_fps > 3.0F)
1421  fps = container_fps;
1422  else if (estimated_fps < 121.0F && estimated_fps > 3.0F)
1423  fps = estimated_fps;
1424  else if (avg_fps < 121.0F && avg_fps > 3.0F)
1425  fps = avg_fps;
1426  else
1427  fps = 30000.0F / 1001.0F; // 29.97 fps
1428 
1429  if (fps != m_fps)
1430  {
1431  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1432  QString("Selected FPS is %1 (avg %2 codec %3 "
1433  "container %4 estimated %5)").arg(fps).arg(avg_fps)
1434  .arg(codec_fps).arg(container_fps).arg(estimated_fps));
1435  }
1436 
1437  return fps;
1438 }
1439 
1440 #ifdef USING_VDPAU
1441 static enum AVPixelFormat get_format_vdpau(struct AVCodecContext *avctx,
1442  const enum AVPixelFormat *valid_fmts)
1443 {
1444  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1445  MythPlayer *player = nullptr;
1446  VideoOutputVDPAU *videoOut = nullptr;
1447  if (nd)
1448  player = nd->GetPlayer();
1449  if (player)
1450  videoOut = (VideoOutputVDPAU*)(player->GetVideoOutput());
1451 
1452  if (videoOut)
1453  {
1454  static uint8_t *dummy[1] = { nullptr };
1455  avctx->hwaccel_context = player->GetDecoderContext(nullptr, dummy[0]);
1456  MythRenderVDPAU *render = videoOut->getRender();
1457  render->BindContext(avctx);
1458  if (avctx->hwaccel_context)
1459  {
1460  ((AVVDPAUContext*)(avctx->hwaccel_context))->render2 =
1462  }
1463  }
1464 
1465  while (*valid_fmts != AV_PIX_FMT_NONE) {
1466  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_VDPAU))
1467  return AV_PIX_FMT_VDPAU;
1468  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1469  return AV_PIX_FMT_YUV420P;
1470  valid_fmts++;
1471  }
1472  return AV_PIX_FMT_NONE;
1473 }
1474 #endif
1475 
1476 #ifdef USING_DXVA2
1477 // Declared separately to allow attribute
1478 static enum AVPixelFormat get_format_dxva2(struct AVCodecContext *,
1479  const enum AVPixelFormat *);
1480 
1481 enum AVPixelFormat get_format_dxva2(struct AVCodecContext *avctx,
1482  const enum AVPixelFormat *valid_fmts)
1483 {
1484  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1485  if (nd && nd->GetPlayer())
1486  {
1487  static uint8_t *dummy[1] = { nullptr };
1488  avctx->hwaccel_context =
1489  (dxva_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1490  }
1491 
1492  while (*valid_fmts != AV_PIX_FMT_NONE) {
1493  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_DXVA2_VLD))
1494  return AV_PIX_FMT_DXVA2_VLD;
1495  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1496  return AV_PIX_FMT_YUV420P;
1497  valid_fmts++;
1498  }
1499  return AV_PIX_FMT_NONE;
1500 }
1501 #endif
1502 
1503 #if defined(USING_VAAPI2) || defined(USING_NVDEC)
1504 static bool IS_VAAPI_PIX_FMT(enum AVPixelFormat fmt)
1505 {
1506  return fmt == AV_PIX_FMT_VAAPI_MOCO ||
1507  fmt == AV_PIX_FMT_VAAPI_IDCT ||
1508  fmt == AV_PIX_FMT_VAAPI_VLD;
1509 }
1510 #endif
1511 
1512 #ifdef USING_VAAPI
1513 // Declared separately to allow attribute
1514 static enum AVPixelFormat get_format_vaapi(struct AVCodecContext * /*avctx*/,
1515  const enum AVPixelFormat * /*valid_fmts*/);
1516 
1517 enum AVPixelFormat get_format_vaapi(struct AVCodecContext *avctx,
1518  const enum AVPixelFormat *valid_fmts)
1519 {
1520  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1521  if (nd && nd->GetPlayer())
1522  {
1523  static uint8_t *dummy[1] = { nullptr };
1524  avctx->hwaccel_context =
1525  (vaapi_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1526  }
1527 
1528  while (*valid_fmts != AV_PIX_FMT_NONE) {
1529  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_VAAPI_VLD))
1530  return AV_PIX_FMT_VAAPI_VLD;
1531  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1532  return AV_PIX_FMT_YUV420P;
1533  valid_fmts++;
1534  }
1535  return AV_PIX_FMT_NONE;
1536 }
1537 #endif
1538 
1539 #ifdef USING_VAAPI2
1540 static enum AVPixelFormat get_format_vaapi2(struct AVCodecContext */*avctx*/,
1541  const enum AVPixelFormat *valid_fmts)
1542 {
1543  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
1544  while (*valid_fmts != AV_PIX_FMT_NONE) {
1545  if (IS_VAAPI_PIX_FMT(*valid_fmts))
1546  {
1547  ret = *valid_fmts;
1548  break;
1549  }
1550  valid_fmts++;
1551  }
1552  return ret;
1553 }
1554 #endif
1555 
1556 #ifdef USING_NVDEC
1557 static enum AVPixelFormat get_format_nvdec(struct AVCodecContext */*avctx*/,
1558  const enum AVPixelFormat *valid_fmts)
1559 {
1560  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
1561  while (*valid_fmts != AV_PIX_FMT_NONE) {
1562  if (AV_PIX_FMT_CUDA == *valid_fmts)
1563  {
1564  ret = *valid_fmts;
1565  break;
1566  }
1567  valid_fmts++;
1568  }
1569  return ret;
1570 }
1571 #endif
1572 
1573 #ifdef USING_MEDIACODEC
1574 static enum AVPixelFormat get_format_mediacodec(struct AVCodecContext */*avctx*/,
1575  const enum AVPixelFormat *valid_fmts)
1576 {
1577  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
1578  while (*valid_fmts != AV_PIX_FMT_NONE) {
1579  if (*valid_fmts == AV_PIX_FMT_MEDIACODEC)
1580  {
1581  ret = AV_PIX_FMT_MEDIACODEC;
1582  break;
1583  }
1584  valid_fmts++;
1585  }
1586  return ret;
1587 }
1588 #endif
1589 
1590 
1591 static bool IS_DR1_PIX_FMT(const enum AVPixelFormat fmt)
1592 {
1593  switch (fmt)
1594  {
1595  case AV_PIX_FMT_YUV420P:
1596  case AV_PIX_FMT_YUVJ420P:
1597  return true;
1598  default:
1599  return false;
1600  }
1601 }
1602 
1603 void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
1604  bool selectedStream)
1605 {
1606  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1607  QString("InitVideoCodec() 0x%1 id(%2) type (%3).")
1608  .arg((uint64_t)enc,0,16)
1609  .arg(ff_codec_id_string(enc->codec_id))
1610  .arg(ff_codec_type_string(enc->codec_type)));
1611 
1612  if (ringBuffer && ringBuffer->IsDVD())
1613  m_directrendering = false;
1614 
1615  enc->opaque = (void *)this;
1616  enc->get_buffer2 = get_avf_buffer;
1617  enc->slice_flags = 0;
1618 
1619  enc->err_recognition = AV_EF_COMPLIANT;
1620  enc->workaround_bugs = FF_BUG_AUTODETECT;
1621  enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
1622  enc->idct_algo = FF_IDCT_AUTO;
1623  enc->debug = 0;
1624  // enc->error_rate = 0;
1625 
1626  const AVCodec *codec1 = enc->codec;
1627 
1628  if (selectedStream)
1629  {
1630  m_directrendering = true;
1631  }
1632 
1633  AVDictionaryEntry *metatag =
1634  av_dict_get(stream->metadata, "rotate", nullptr, 0);
1635  if (metatag && metatag->value && QString("180") == metatag->value)
1636  m_video_inverted = true;
1637 
1638 #ifdef USING_VDPAU
1640  {
1641  enc->get_buffer2 = get_avf_buffer_vdpau;
1642  enc->get_format = get_format_vdpau;
1643  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1644  }
1645  else
1646 #endif
1647 #ifdef USING_DXVA2
1648  if (CODEC_IS_DXVA2(codec1, enc))
1649  {
1650  enc->get_buffer2 = get_avf_buffer_dxva2;
1651  enc->get_format = get_format_dxva2;
1652  }
1653  else
1654 #endif
1655 #ifdef USING_VAAPI
1656  if (CODEC_IS_VAAPI(codec1, enc) && codec_is_vaapi(m_video_codec_id))
1657  {
1658  enc->get_buffer2 = get_avf_buffer_vaapi;
1659  enc->get_format = get_format_vaapi;
1660  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1661  }
1662  else
1663 #endif
1664 #ifdef USING_MEDIACODEC
1665  if (CODEC_IS_MEDIACODEC(codec1))
1666  {
1667  enc->get_format = get_format_mediacodec;
1668  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1669  }
1670  else
1671 #endif
1672 #ifdef USING_VAAPI2
1674  {
1675  enc->get_buffer2 = get_avf_buffer_vaapi2;
1676  enc->get_format = get_format_vaapi2;
1677  }
1678  else
1679 #endif
1680 #ifdef USING_NVDEC
1682  {
1683  enc->get_buffer2 = get_avf_buffer_nvdec;
1684  enc->get_format = get_format_nvdec;
1685  m_directrendering = false;
1686  }
1687  else
1688 #endif
1689  if (codec1 && codec1->capabilities & AV_CODEC_CAP_DR1)
1690  {
1691  // enc->flags |= CODEC_FLAG_EMU_EDGE;
1692  }
1693  else
1694  {
1695  if (selectedStream)
1696  m_directrendering = false;
1697  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1698  QString("Using software scaling to convert pixel format %1 for "
1699  "codec %2").arg(enc->pix_fmt)
1700  .arg(ff_codec_id_string(enc->codec_id)));
1701  }
1702 
1703  QString deinterlacer;
1704  if (m_mythcodecctx)
1705  deinterlacer = m_mythcodecctx->getDeinterlacerName();
1706  delete m_mythcodecctx;
1709  m_mythcodecctx->setStream(stream);
1710  m_mythcodecctx->setDeinterlacer(true,deinterlacer);
1711 
1712  int ret = m_mythcodecctx->HwDecoderInit(enc);
1713  if (ret < 0)
1714  {
1715  if (ret < 0)
1716  {
1717  char error[AV_ERROR_MAX_STRING_SIZE];
1718  LOG(VB_GENERAL, LOG_ERR, LOC +
1719  QString("HwDecoderInit unable to initialize hardware decoder: %1 (%2)")
1720  .arg(av_make_error_string(error, sizeof(error), ret))
1721  .arg(ret));
1722  // force it to switch to software decoding
1724  m_streams_changed = true;
1725  }
1726  }
1727 
1731  {
1732  if (codec1 &&
1733  ((AV_CODEC_ID_MPEG2VIDEO == codec1->id) ||
1734  (AV_CODEC_ID_MPEG1VIDEO == codec1->id)))
1735  {
1737  {
1738  uint total_blocks = (enc->height+15) / 16;
1739  enc->skip_top = (total_blocks+3) / 4;
1740  enc->skip_bottom = (total_blocks+3) / 4;
1741  }
1742 
1743  if (FlagIsSet(kDecodeLowRes))
1744  enc->lowres = 2; // 1 = 1/2 size, 2 = 1/4 size
1745  }
1746  else if (codec1 && (AV_CODEC_ID_H264 == codec1->id))
1747  {
1749  {
1750  enc->flags &= ~AV_CODEC_FLAG_LOOP_FILTER;
1751  enc->skip_loop_filter = AVDISCARD_ALL;
1752  }
1753  }
1754 
1756  {
1757  enc->skip_idct = AVDISCARD_ALL;
1758  }
1759  }
1760 
1761  if (selectedStream)
1762  {
1763  m_fps = normalized_fps(stream, enc);
1764  QSize dim = get_video_dim(*enc);
1765  int width = m_current_width = dim.width();
1766  int height = m_current_height = dim.height();
1767  m_current_aspect = get_aspect(*enc);
1768 
1769  if (!width || !height)
1770  {
1771  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1772  "InitVideoCodec invalid dimensions, resetting decoder.");
1773  width = 640;
1774  height = 480;
1775  m_fps = 29.97F;
1776  m_current_aspect = 4.0F / 3.0F;
1777  }
1778 
1780  const AVCodec *codec2 = enc->codec;
1781  QString codecName;
1782  if (codec2)
1783  codecName = codec2->name;
1784  m_parent->SetVideoParams(width, height, m_fps, kScan_Detect, codecName);
1785  if (LCD *lcd = LCD::Get())
1786  {
1787  LCDVideoFormatSet video_format;
1788 
1789  switch (enc->codec_id)
1790  {
1791  case AV_CODEC_ID_H263:
1792  case AV_CODEC_ID_MPEG4:
1793  case AV_CODEC_ID_MSMPEG4V1:
1794  case AV_CODEC_ID_MSMPEG4V2:
1795  case AV_CODEC_ID_MSMPEG4V3:
1796  case AV_CODEC_ID_H263P:
1797  case AV_CODEC_ID_H263I:
1798  video_format = VIDEO_DIVX;
1799  break;
1800  case AV_CODEC_ID_WMV1:
1801  case AV_CODEC_ID_WMV2:
1802  video_format = VIDEO_WMV;
1803  break;
1804 #if 0
1805  case AV_CODEC_ID_XVID:
1806  video_format = VIDEO_XVID;
1807  break;
1808 #endif
1809  default:
1810  video_format = VIDEO_MPG;
1811  break;
1812  }
1813 
1814  lcd->setVideoFormatLEDs(video_format, true);
1815 
1816  if(height >= 720)
1817  lcd->setVariousLEDs(VARIOUS_HDTV, true);
1818  else
1819  lcd->setVariousLEDs(VARIOUS_HDTV, false);
1820  }
1821  }
1822 }
1823 
1824 // CC Parity checking
1825 // taken from xine-lib libspucc
1826 
1827 static int cc608_parity(uint8_t byte)
1828 {
1829  int ones = 0;
1830 
1831  for (int i = 0; i < 7; i++)
1832  {
1833  if (byte & (1 << i))
1834  ones++;
1835  }
1836 
1837  return ones & 1;
1838 }
1839 
1840 // CC Parity checking
1841 // taken from xine-lib libspucc
1842 
1843 static void cc608_build_parity_table(int *parity_table)
1844 {
1845  for (uint8_t byte = 0; byte <= 127; byte++)
1846  {
1847  int parity_v = cc608_parity(byte);
1848  /* CC uses odd parity (i.e., # of 1's in byte is odd.) */
1849  parity_table[byte] = parity_v;
1850  parity_table[byte | 0x80] = (parity_v == 0 ? 1 : 0);
1851  }
1852 }
1853 
1854 // CC Parity checking
1855 // taken from xine-lib libspucc
1856 
1857 static int cc608_good_parity(const int *parity_table, uint16_t data)
1858 {
1859  bool ret = (parity_table[data & 0xff] != 0)
1860  && (parity_table[(data & 0xff00) >> 8] != 0);
1861  if (!ret)
1862  {
1863  LOG(VB_VBI, LOG_ERR, LOC +
1864  QString("VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
1865  }
1866  return ret ? 1 : 0;
1867 }
1868 
1870 {
1871  memset(m_ccX08_in_pmt, 0, sizeof(m_ccX08_in_pmt));
1872  m_pmt_tracks.clear();
1873  m_pmt_track_types.clear();
1874 
1875  // Figure out languages of ATSC captions
1876  if (!m_ic->cur_pmt_sect)
1877  {
1878  LOG(VB_GENERAL, LOG_DEBUG, LOC +
1879  "ScanATSCCaptionStreams() called with no PMT");
1880  return;
1881  }
1882 
1883  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
1884 
1885  uint i;
1886  for (i = 0; i < pmt.StreamCount(); i++)
1887  {
1888  // MythTV remaps OpenCable Video to normal video during recording
1889  // so "dvb" is the safest choice for system info type, since this
1890  // will ignore other uses of the same stream id in DVB countries.
1891  if (pmt.IsVideo(i, "dvb"))
1892  break;
1893  }
1894 
1895  if (!pmt.IsVideo(i, "dvb"))
1896  return;
1897 
1899  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1901 
1902  const desc_list_t desc_list2 = MPEGDescriptor::ParseOnlyInclude(
1903  pmt.ProgramInfo(), pmt.ProgramInfoLength(),
1905 
1906  desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
1907 
1908  for (size_t j = 0; j < desc_list.size(); j++)
1909  {
1910  const CaptionServiceDescriptor csd(desc_list[j]);
1911  if (!csd.IsValid())
1912  continue;
1913  for (uint k = 0; k < csd.ServicesCount(); k++)
1914  {
1915  int lang = csd.CanonicalLanguageKey(k);
1916  int type = csd.Type(k) ? 1 : 0;
1917  if (type)
1918  {
1919  StreamInfo si(av_index, lang, 0/*lang_idx*/,
1920  csd.CaptionServiceNumber(k),
1921  csd.EasyReader(k),
1922  csd.WideAspectRatio(k));
1923  uint key = csd.CaptionServiceNumber(k) + 4;
1924  m_ccX08_in_pmt[key] = true;
1925  m_pmt_tracks.push_back(si);
1927  }
1928  else
1929  {
1930  int line21 = csd.Line21Field(k) ? 3 : 1;
1931  StreamInfo si(av_index, lang, 0/*lang_idx*/, line21, 0);
1932  m_ccX08_in_pmt[line21-1] = true;
1933  m_pmt_tracks.push_back(si);
1935  }
1936  }
1937  }
1938 }
1939 
1941 {
1942  m_tracks[kTrackTypeCC608].clear();
1943  m_tracks[kTrackTypeCC708].clear();
1944  memset(m_ccX08_in_tracks, 0, sizeof(m_ccX08_in_tracks));
1945 
1946  uint pidx = 0, sidx = 0;
1947  map<int,uint> lang_cc_cnt[2];
1948  while (true)
1949  {
1950  bool pofr = pidx >= (uint)m_pmt_tracks.size();
1951  bool sofr = sidx >= (uint)m_stream_tracks.size();
1952  if (pofr && sofr)
1953  break;
1954 
1955  // choose lowest available next..
1956  // stream_id's of 608 and 708 streams alias, but this
1957  // is ok as we just want each list to be ordered.
1958  StreamInfo const *si = nullptr;
1959  int type = 0; // 0 if 608, 1 if 708
1960  bool isp = true; // if true use m_pmt_tracks next, else stream_tracks
1961 
1962  if (pofr && !sofr)
1963  isp = false;
1964  else if (!pofr && sofr)
1965  isp = true;
1966  else if (m_stream_tracks[sidx] < m_pmt_tracks[pidx])
1967  isp = false;
1968 
1969  if (isp)
1970  {
1971  si = &m_pmt_tracks[pidx];
1972  type = kTrackTypeCC708 == m_pmt_track_types[pidx] ? 1 : 0;
1973  pidx++;
1974  }
1975  else
1976  {
1977  si = &m_stream_tracks[sidx];
1978  type = kTrackTypeCC708 == m_stream_track_types[sidx] ? 1 : 0;
1979  sidx++;
1980  }
1981 
1982  StreamInfo nsi(*si);
1983  int lang_indx = lang_cc_cnt[type][nsi.m_language];
1984  lang_cc_cnt[type][nsi.m_language]++;
1985  nsi.m_language_index = lang_indx;
1986  m_tracks[(type) ? kTrackTypeCC708 : kTrackTypeCC608].push_back(nsi);
1987  int key = nsi.m_stream_id + ((type) ? 4 : -1);
1988  if (key < 0)
1989  {
1990  LOG(VB_GENERAL, LOG_ERR, LOC + "in_tracks key too small");
1991  }
1992  else
1993  {
1994  m_ccX08_in_tracks[key] = true;
1995  }
1996  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1997  QString("%1 caption service #%2 is in the %3 language.")
1998  .arg((type) ? "EIA-708" : "EIA-608")
1999  .arg(nsi.m_stream_id)
2000  .arg(iso639_key_toName(nsi.m_language)));
2001  }
2002 }
2003 
2005 {
2006  // ScanStreams() calls m_tracks[kTrackTypeTeletextCaptions].clear()
2007  if (!m_ic->cur_pmt_sect || !m_tracks[kTrackTypeTeletextCaptions].empty())
2008  return;
2009 
2010  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
2011 
2012  for (uint i = 0; i < pmt.StreamCount(); i++)
2013  {
2014  if (pmt.StreamType(i) != StreamID::PrivData)
2015  continue;
2016 
2018  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
2020 
2021  for (size_t j = 0; j < desc_list.size(); j++)
2022  {
2023  const TeletextDescriptor td(desc_list[j]);
2024  if (!td.IsValid())
2025  continue;
2026  for (uint k = 0; k < td.StreamCount(); k++)
2027  {
2028  int type = td.TeletextType(k);
2029  int language = td.CanonicalLanguageKey(k);
2030  int magazine = td.TeletextMagazineNum(k);
2031  if (magazine == 0)
2032  magazine = 8;
2033  int pagenum = td.TeletextPageNum(k);
2034  int lang_idx = (magazine << 8) | pagenum;
2035  StreamInfo si(av_index, language, lang_idx, 0, 0);
2036  if (type == 2 || type == 1)
2037  {
2038  TrackType track = (type == 2) ? kTrackTypeTeletextCaptions :
2040  m_tracks[track].push_back(si);
2041  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2042  QString("Teletext stream #%1 (%2) is in the %3 language"
2043  " on page %4 %5.")
2044  .arg(k).arg((type == 2) ? "Caption" : "Menu")
2045  .arg(iso639_key_toName(language))
2046  .arg(magazine).arg(pagenum));
2047  }
2048  }
2049  }
2050 
2051  // Assume there is only one multiplexed teletext stream in PMT..
2052  if (!m_tracks[kTrackTypeTeletextCaptions].empty())
2053  break;
2054  }
2055 }
2056 
2057 void AvFormatDecoder::ScanRawTextCaptions(int av_stream_index)
2058 {
2059  AVDictionaryEntry *metatag =
2060  av_dict_get(m_ic->streams[av_stream_index]->metadata, "language", nullptr,
2061  0);
2062  bool forced =
2063  (m_ic->streams[av_stream_index]->disposition & AV_DISPOSITION_FORCED) != 0;
2064  int lang = metatag ? get_canonical_lang(metatag->value) :
2065  iso639_str3_to_key("und");
2066  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2067  QString("Text Subtitle track #%1 is A/V stream #%2 "
2068  "and is in the %3 language(%4), forced=%5.")
2069  .arg(m_tracks[kTrackTypeRawText].size()).arg(av_stream_index)
2070  .arg(iso639_key_toName(lang)).arg(lang).arg(forced));
2071  StreamInfo si(av_stream_index, lang, 0, 0, 0, false, false, forced);
2072  m_tracks[kTrackTypeRawText].push_back(si);
2073 }
2074 
2080 {
2081  if (!m_ic || !m_ic->cur_pmt_sect)
2082  return;
2083 
2084  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
2085  return;
2086 
2087  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
2088 
2089  for (uint i = 0; i < pmt.StreamCount(); i++)
2090  {
2091  if (! StreamID::IsObjectCarousel(pmt.StreamType(i)))
2092  continue;
2093 
2094  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams Found Object Carousel in Stream %1").arg(QString::number(i)));
2095 
2097  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
2099 
2100  for (size_t j = 0; j < desc_list.size(); j++)
2101  {
2102  const unsigned char *desc = desc_list[j];
2103  desc++; // Skip tag
2104  uint length = *desc++;
2105  const unsigned char *endDesc = desc+length;
2106  uint dataBroadcastId = desc[0]<<8 | desc[1];
2107  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams dataBroadcastId %1").arg(QString::number(dataBroadcastId)));
2108  if (dataBroadcastId != 0x0106) // ETSI/UK Profile
2109  continue;
2110  desc += 2; // Skip data ID
2111  while (desc != endDesc)
2112  {
2113  uint appTypeCode = desc[0]<<8 | desc[1];
2114  desc += 3; // Skip app type code and boot priority hint
2115  uint appSpecDataLen = *desc++;
2116 #ifdef USING_MHEG
2117  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams AppTypeCode %1").arg(QString::number(appTypeCode)));
2118  if (appTypeCode == 0x101) // UK MHEG profile
2119  {
2120  const unsigned char *subDescEnd = desc + appSpecDataLen;
2121  while (desc < subDescEnd)
2122  {
2123  uint sub_desc_tag = *desc++;
2124  uint sub_desc_len = *desc++;
2125  // Network boot info sub-descriptor.
2126  if (sub_desc_tag == 1)
2127  m_itv->SetNetBootInfo(desc, sub_desc_len);
2128  desc += sub_desc_len;
2129  }
2130  }
2131  else
2132 #else
2133  (void) appTypeCode;
2134 #endif // USING_MHEG
2135  {
2136  desc += appSpecDataLen;
2137  }
2138  }
2139  }
2140  }
2141 }
2142 
2144 {
2145  bool unknownbitrate = false;
2146  int scanerror = 0;
2147  m_bitrate = 0;
2148 
2149  m_tracks[kTrackTypeAttachment].clear();
2150  m_tracks[kTrackTypeAudio].clear();
2151  m_tracks[kTrackTypeSubtitle].clear();
2154  m_tracks[kTrackTypeRawText].clear();
2155  if (!novideo)
2156  {
2157  // we will rescan video streams
2158  m_tracks[kTrackTypeVideo].clear();
2160  m_fps = 0;
2161  }
2162  map<int,uint> lang_sub_cnt;
2163  uint subtitleStreamCount = 0;
2164  map<int,uint> lang_aud_cnt;
2165  uint audioStreamCount = 0;
2166 
2167  if (ringBuffer && ringBuffer->IsDVD() &&
2169  {
2170  ringBuffer->DVD()->AudioStreamsChanged(false);
2172  }
2173 
2174  for (uint strm = 0; strm < m_ic->nb_streams; strm++)
2175  {
2176  AVCodecParameters *par = m_ic->streams[strm]->codecpar;
2177  AVCodecContext *enc = nullptr;
2178 
2179  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2180  QString("Stream #%1, has id 0x%2 codec id %3, "
2181  "type %4, bitrate %5 at 0x%6")
2182  .arg(strm).arg((uint64_t)m_ic->streams[strm]->id,0,16)
2183  .arg(ff_codec_id_string(par->codec_id))
2184  .arg(ff_codec_type_string(par->codec_type))
2185  .arg(par->bit_rate).arg((uint64_t)m_ic->streams[strm],0,16));
2186 
2187  switch (par->codec_type)
2188  {
2189  case AVMEDIA_TYPE_VIDEO:
2190  {
2191  if (!par->codec_id)
2192  {
2193  LOG(VB_GENERAL, LOG_ERR, LOC +
2194  QString("Stream #%1 has an unknown video "
2195  "codec id, skipping.").arg(strm));
2196  continue;
2197  }
2198 
2199  // ffmpeg does not return a bitrate for several codecs and
2200  // formats. Forcing it to 500000 ensures the ringbuffer does not
2201  // use optimisations for low bitrate (audio and data) streams.
2202  if (par->bit_rate == 0)
2203  {
2204  par->bit_rate = 500000;
2205  unknownbitrate = true;
2206  }
2207  m_bitrate += par->bit_rate;
2208 
2209  break;
2210  }
2211  case AVMEDIA_TYPE_AUDIO:
2212  {
2213  enc = gCodecMap->hasCodecContext(m_ic->streams[strm]);
2214  if (enc && enc->internal)
2215  {
2216  LOG(VB_GENERAL, LOG_WARNING, LOC +
2217  QString("Warning, audio codec 0x%1 id(%2) "
2218  "type (%3) already open, leaving it alone.")
2219  .arg((uint64_t)enc,0,16)
2220  .arg(ff_codec_id_string(enc->codec_id))
2221  .arg(ff_codec_type_string(enc->codec_type)));
2222  }
2223  LOG(VB_GENERAL, LOG_INFO, LOC +
2224  QString("codec %1 has %2 channels")
2225  .arg(ff_codec_id_string(par->codec_id))
2226  .arg(par->channels));
2227 
2228  m_bitrate += par->bit_rate;
2229  break;
2230  }
2231  case AVMEDIA_TYPE_SUBTITLE:
2232  {
2233  if (par->codec_id == AV_CODEC_ID_DVB_TELETEXT)
2234  ScanTeletextCaptions(strm);
2235  if (par->codec_id == AV_CODEC_ID_TEXT)
2236  ScanRawTextCaptions(strm);
2237  m_bitrate += par->bit_rate;
2238 
2239  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("subtitle codec (%1)")
2240  .arg(ff_codec_type_string(par->codec_type)));
2241  break;
2242  }
2243  case AVMEDIA_TYPE_DATA:
2244  {
2245  ScanTeletextCaptions(strm);
2246  m_bitrate += par->bit_rate;
2247  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("data codec (%1)")
2248  .arg(ff_codec_type_string(par->codec_type)));
2249  break;
2250  }
2251  case AVMEDIA_TYPE_ATTACHMENT:
2252  {
2253  if (par->codec_id == AV_CODEC_ID_TTF)
2254  m_tracks[kTrackTypeAttachment].push_back(
2255  StreamInfo(strm, 0, 0, m_ic->streams[strm]->id, 0));
2256  m_bitrate += par->bit_rate;
2257  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2258  QString("Attachment codec (%1)")
2259  .arg(ff_codec_type_string(par->codec_type)));
2260  break;
2261  }
2262  default:
2263  {
2264  m_bitrate += par->bit_rate;
2265  LOG(VB_PLAYBACK, LOG_ERR, LOC +
2266  QString("Unknown codec type (%1)")
2267  .arg(ff_codec_type_string(par->codec_type)));
2268  break;
2269  }
2270  }
2271 
2272  if (par->codec_type != AVMEDIA_TYPE_AUDIO &&
2273  par->codec_type != AVMEDIA_TYPE_SUBTITLE)
2274  continue;
2275 
2276  // skip DVB teletext and text subs, there is no libavcodec decoder
2277  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2278  (par->codec_id == AV_CODEC_ID_DVB_TELETEXT ||
2279  par->codec_id == AV_CODEC_ID_TEXT))
2280  continue;
2281 
2282  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Looking for decoder for %1")
2283  .arg(ff_codec_id_string(par->codec_id)));
2284 
2285  if (par->codec_id == AV_CODEC_ID_PROBE)
2286  {
2287  LOG(VB_GENERAL, LOG_ERR, LOC +
2288  QString("Probing of stream #%1 unsuccesful, ignoring.").arg(strm));
2289  continue;
2290  }
2291 
2292  if (!enc)
2293  enc = gCodecMap->getCodecContext(m_ic->streams[strm]);
2294 
2295  const AVCodec *codec = nullptr;
2296  if (enc)
2297  codec = enc->codec;
2298  else
2299  {
2300  LOG(VB_GENERAL, LOG_ERR, LOC +
2301  QString("Could not find decoder for codec (%1), ignoring.")
2302  .arg(ff_codec_id_string(par->codec_id)));
2303 
2304  // Nigel's bogus codec-debug. Dump the list of codecs & decoders,
2305  // and have one last attempt to find a decoder. This is usually
2306  // only caused by build problems, where libavcodec needs a rebuild
2307  if (VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY))
2308  {
2309  AVCodec *p = av_codec_next(nullptr);
2310  int i = 1;
2311  while (p)
2312  {
2313  QString msg;
2314 
2315  if (p->name[0] != '\0')
2316  msg = QString("Codec %1:").arg(p->name);
2317  else
2318  msg = QString("Codec %1, null name,").arg(strm);
2319 
2320  if (p->decode == nullptr)
2321  msg += "decoder is null";
2322 
2323  LOG(VB_LIBAV, LOG_INFO, LOC + msg);
2324 
2325  if (p->id == par->codec_id)
2326  {
2327  codec = p;
2328  break;
2329  }
2330 
2331  LOG(VB_LIBAV, LOG_INFO, LOC +
2332  QString("Codec 0x%1 != 0x%2") .arg(p->id, 0, 16)
2333  .arg(par->codec_id, 0, 16));
2334  p = av_codec_next(p);
2335  ++i;
2336  }
2337  }
2338  if (codec)
2339  enc = gCodecMap->getCodecContext(m_ic->streams[strm], codec);
2340  else
2341  continue;
2342  }
2343  if (!enc)
2344  continue;
2345 
2346  if (enc->codec && par->codec_id != enc->codec_id)
2347  {
2348  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2349  QString("Already opened codec not matching (%1 vs %2). Reopening")
2350  .arg(ff_codec_id_string(enc->codec_id))
2351  .arg(ff_codec_id_string(enc->codec->id)));
2352  gCodecMap->freeCodecContext(m_ic->streams[strm]);
2353  enc = gCodecMap->getCodecContext(m_ic->streams[strm]);
2354  }
2355  if (!OpenAVCodec(enc, codec))
2356  continue;
2357  if (!enc)
2358  continue;
2359 
2360  if (!IsValidStream(m_ic->streams[strm]->id))
2361  {
2362  /* Hide this stream if it's not valid in this context.
2363  * This can happen, for example, on a Blu-ray disc if there
2364  * are more physical streams than there is metadata about them.
2365  * (e.g. Despicable Me)
2366  */
2367  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2368  QString("Stream 0x%1 is not valid in this context - skipping")
2369  .arg(m_ic->streams[strm]->id, 4, 16));
2370  continue;
2371  }
2372 
2373  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2374  {
2375  bool forced = (m_ic->streams[strm]->disposition & AV_DISPOSITION_FORCED) != 0;
2376  int lang = GetSubtitleLanguage(subtitleStreamCount, strm);
2377  int lang_indx = lang_sub_cnt[lang]++;
2378  subtitleStreamCount++;
2379 
2380  m_tracks[kTrackTypeSubtitle].push_back(
2381  StreamInfo(strm, lang, lang_indx, m_ic->streams[strm]->id, 0, 0, false, false, forced));
2382 
2383  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2384  QString("Subtitle track #%1 is A/V stream #%2 "
2385  "and is in the %3 language(%4).")
2386  .arg(m_tracks[kTrackTypeSubtitle].size()).arg(strm)
2387  .arg(iso639_key_toName(lang)).arg(lang));
2388  }
2389 
2390  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
2391  {
2392  int lang = GetAudioLanguage(audioStreamCount, strm);
2394  int channels = par->channels;
2395  int lang_indx = lang_aud_cnt[lang]++;
2396  audioStreamCount++;
2397 
2398  if (enc->avcodec_dual_language)
2399  {
2400  m_tracks[kTrackTypeAudio].push_back(
2401  StreamInfo(strm, lang, lang_indx, m_ic->streams[strm]->id, channels,
2402  false, false, false, type));
2403  lang_indx = lang_aud_cnt[lang]++;
2404  m_tracks[kTrackTypeAudio].push_back(
2405  StreamInfo(strm, lang, lang_indx, m_ic->streams[strm]->id, channels,
2406  true, false, false, type));
2407  }
2408  else
2409  {
2410  int logical_stream_id;
2411  if (ringBuffer && ringBuffer->IsDVD())
2412  {
2413  logical_stream_id = ringBuffer->DVD()->GetAudioTrackNum(m_ic->streams[strm]->id);
2414  channels = ringBuffer->DVD()->GetNumAudioChannels(logical_stream_id);
2415  }
2416  else
2417  logical_stream_id = m_ic->streams[strm]->id;
2418 
2419  if (logical_stream_id == -1)
2420  {
2421  // This stream isn't mapped, so skip it
2422  continue;
2423  }
2424 
2425  m_tracks[kTrackTypeAudio].push_back(
2426  StreamInfo(strm, lang, lang_indx, logical_stream_id, channels,
2427  false, false, false, type));
2428  }
2429 
2430  LOG(VB_AUDIO, LOG_INFO, LOC +
2431  QString("Audio Track #%1, of type (%2) is A/V stream #%3 (id=0x%4) "
2432  "and has %5 channels in the %6 language(%7).")
2433  .arg(m_tracks[kTrackTypeAudio].size()).arg(toString(type))
2434  .arg(strm).arg(m_ic->streams[strm]->id,0,16).arg(enc->channels)
2435  .arg(iso639_key_toName(lang)).arg(lang));
2436  }
2437  }
2438 
2439  // Now find best video track to play
2440  if (!novideo && m_ic)
2441  {
2442  for(;;)
2443  {
2444  AVCodec *codec = nullptr;
2445  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2446  "Trying to select best video track");
2447 
2448  /*
2449  * Find the "best" stream in the file.
2450  *
2451  * The best stream is determined according to various heuristics as
2452  * the most likely to be what the user expects. If the decoder parameter
2453  * is not nullptr, av_find_best_stream will find the default decoder
2454  * for the stream's codec; streams for which no decoder can be found
2455  * are ignored.
2456  *
2457  * If av_find_best_stream returns successfully and decoder_ret is not nullptr,
2458  * then *decoder_ret is guaranteed to be set to a valid AVCodec.
2459  */
2460  int selTrack = av_find_best_stream(m_ic, AVMEDIA_TYPE_VIDEO,
2461  -1, -1, &codec, 0);
2462 
2463  if (selTrack < 0)
2464  {
2465  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2466  "No video track found/selected.");
2467  break;
2468  }
2469 
2471  gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
2472  AVCodecContext *enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
2473  StreamInfo si(selTrack, 0, 0, 0, 0);
2474 
2475  m_tracks[kTrackTypeVideo].push_back(si);
2477 
2478  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2479  QString("Selected track #%1 (id 0x%2 codec id %3, "
2480  "type %4, bitrate %5 at 0x%6)")
2481  .arg(selTrack).arg((uint64_t)m_ic->streams[selTrack]->id,0,16)
2482  .arg(ff_codec_id_string(enc->codec_id))
2483  .arg(ff_codec_type_string(enc->codec_type))
2484  .arg(enc->bit_rate).arg((uint64_t)m_ic->streams[selTrack],0,16));
2485 
2486  m_codec_is_mpeg = CODEC_IS_FFMPEG_MPEG(enc->codec_id);
2487 
2488  delete m_private_dec;
2489  m_private_dec = nullptr;
2490  m_h264_parser->Reset();
2491 
2492  QSize dim = get_video_dim(*enc);
2493  uint width = max(dim.width(), 16);
2494  uint height = max(dim.height(), 16);
2495  QString dec = "ffmpeg";
2496  uint thread_count = 1;
2497  QString codecName;
2498  if (enc->codec)
2499  codecName = enc->codec->name;
2500  if (enc->framerate.den && enc->framerate.num)
2501  m_fps = float(enc->framerate.num) / float(enc->framerate.den);
2502  else
2503  m_fps = 0;
2504  if (!m_is_db_ignored)
2505  {
2506  // VideoDisplayProfile vdp;
2507  m_videoDisplayProfile.SetInput(QSize(width, height),m_fps,codecName);
2509  thread_count = m_videoDisplayProfile.GetMaxCPUs();
2510  bool skip_loop_filter = m_videoDisplayProfile.IsSkipLoopEnabled();
2511  if (!skip_loop_filter)
2512  {
2513  enc->skip_loop_filter = AVDISCARD_NONKEY;
2514  }
2515  }
2516 
2518  int version = mpeg_version(enc->codec_id);
2519  if (version)
2521 
2522  // Check it's a codec we can decode using GPU
2523 #ifdef USING_OPENMAX
2524  // The OpenMAX decoder supports H264 high 10, 422 and 444 profiles
2525  if (dec != "openmax")
2526 #endif
2528  {
2529  bool wasgpu = dec != "ffmpeg";
2530  if (FlagIsSet(kDecodeAllowGPU) && force_sw_decode(enc) && wasgpu)
2531  {
2532  LOG(VB_GENERAL, LOG_WARNING, LOC +
2533  "Unsupported Video Profile - forcing software decode");
2534  }
2536  {
2537  LOG(VB_GENERAL, LOG_WARNING, LOC +
2538  "GPU decoding failed - forcing software decode");
2539  }
2540  m_averror_count = 0;
2541  dec = "ffmpeg";
2542  }
2543 
2545  {
2546  bool foundgpudecoder = false;
2547  Q_UNUSED(foundgpudecoder); // Prevent warning if no GPU decoders
2548 
2549 #ifdef USING_VDPAU
2550  MythCodecID vdpau_mcid;
2551  vdpau_mcid =
2552  VideoOutputVDPAU::GetBestSupportedCodec(width, height, dec,
2553  mpeg_version(enc->codec_id),
2554  false);
2555 
2556  if (codec_is_vdpau(vdpau_mcid))
2557  {
2558  m_video_codec_id = vdpau_mcid;
2559  // cppcheck-suppress unreadVariable
2560  foundgpudecoder = true;
2561  }
2562 #endif // USING_VDPAU
2563 #ifdef USING_GLVAAPI
2564  if (!foundgpudecoder)
2565  {
2566  MythCodecID vaapi_mcid;
2567  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2568  vaapi_mcid =
2570  mpeg_version(enc->codec_id),
2571  false,
2572  pix_fmt);
2573 
2574  if (codec_is_vaapi(vaapi_mcid))
2575  {
2576  m_video_codec_id = vaapi_mcid;
2577  enc->pix_fmt = pix_fmt;
2578  foundgpudecoder = true;
2579  }
2580  }
2581 #endif // USING_GLVAAPI
2582 #ifdef USING_DXVA2
2583  if (!foundgpudecode)
2584  {
2585  MythCodecID dxva2_mcid;
2586  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2588  width, height, dec, mpeg_version(enc->codec_id),
2589  false, pix_fmt);
2590 
2591  if (codec_is_dxva2(dxva2_mcid))
2592  {
2593  m_video_codec_id = dxva2_mcid;
2594  enc->pix_fmt = pix_fmt;
2595  // cppcheck-suppress unreadVariable
2596  foundgpudecoder = true;
2597  }
2598  }
2599 #endif // USING_DXVA2
2600 #ifdef USING_MEDIACODEC
2601  if (!foundgpudecoder)
2602  {
2603  MythCodecID mediacodec_mcid;
2604  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2605  mediacodec_mcid = MediaCodecContext::GetBestSupportedCodec(
2606  &codec, dec, mpeg_version(enc->codec_id),
2607  pix_fmt);
2608 
2609  if (codec_is_mediacodec(mediacodec_mcid))
2610  {
2611  gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
2612  enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
2613  m_video_codec_id = mediacodec_mcid;
2614  foundgpudecoder = true;
2615  }
2616  }
2617 #endif // USING_MEDIACODEC
2618 #ifdef USING_VAAPI2
2619  if (!foundgpudecoder)
2620  {
2621  MythCodecID vaapi2_mcid;
2622  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2624  &codec, dec, mpeg_version(enc->codec_id),
2625  pix_fmt);
2626 
2627  if (codec_is_vaapi2(vaapi2_mcid))
2628  {
2629  gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
2630  enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
2631  m_video_codec_id = vaapi2_mcid;
2632  foundgpudecoder = true;
2633  }
2634  }
2635 #endif // USING_VAAPI2
2636 #ifdef USING_NVDEC
2637  if (!foundgpudecoder)
2638  {
2639  MythCodecID nvdec_mcid;
2640  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2642  &codec, dec, mpeg_version(enc->codec_id),
2643  pix_fmt);
2644 
2645  if (codec_is_nvdec(nvdec_mcid))
2646  {
2647  gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
2648  enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
2649  m_video_codec_id = nvdec_mcid;
2650  //foundgpudecoder = true;
2651  }
2652  }
2653 #endif // USING_NVDEC
2654  }
2655  // default to mpeg2
2657  {
2658  LOG(VB_GENERAL, LOG_ERR, LOC +
2659  "Unknown video codec - defaulting to MPEG2");
2661  }
2662 
2663  // Use a PrivateDecoder if allowed in playerFlags AND matched
2664  // via the decoder name
2666  if (m_private_dec)
2667  thread_count = 1;
2668 
2669  m_use_frame_timing = false;
2670  if (! ringBuffer->IsDVD()
2675  || GetCodecDecoderName() == "openmax"))
2676  m_use_frame_timing = true;
2677 
2679  thread_count = 1;
2680 
2681  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2682  QString("Using %1 CPUs for decoding")
2683  .arg(HAVE_THREADS ? thread_count : 1));
2684 
2685  if (HAVE_THREADS)
2686  enc->thread_count = thread_count;
2687 
2688  InitVideoCodec(m_ic->streams[selTrack], enc, true);
2689 
2690  ScanATSCCaptionStreams(selTrack);
2692 
2693  LOG(VB_GENERAL, LOG_INFO, LOC +
2694  QString("Using %1 for video decoding")
2695  .arg(GetCodecDecoderName()));
2696 
2697  {
2698  QMutexLocker locker(avcodeclock);
2699 
2700  if (!OpenAVCodec(enc, codec))
2701  {
2702  scanerror = -1;
2703  break;
2704  }
2705  }
2706 
2707  break;
2708  }
2709  }
2710 
2711  if (m_ic && ((uint)m_ic->bit_rate > m_bitrate))
2712  m_bitrate = (uint)m_ic->bit_rate;
2713 
2714  if (m_bitrate > 0)
2715  {
2716  m_bitrate = (m_bitrate + 999) / 1000;
2717  if (ringBuffer)
2719  }
2720 
2721  // update RingBuffer buffer size
2722  if (ringBuffer)
2723  {
2724  ringBuffer->SetBufferSizeFactors(unknownbitrate,
2725  m_ic && QString(m_ic->iformat->name).contains("matroska"));
2726  }
2727 
2729 
2730  // Select a new track at the next opportunity.
2731  ResetTracks();
2732 
2733  // We have to do this here to avoid the NVP getting stuck
2734  // waiting on audio.
2735  if (m_audio->HasAudioIn() && m_tracks[kTrackTypeAudio].empty())
2736  {
2737  m_audio->SetAudioParams(FORMAT_NONE, -1, -1, AV_CODEC_ID_NONE, -1, false);
2738  m_audio->ReinitAudio();
2739  if (ringBuffer && ringBuffer->IsDVD())
2740  m_audioIn = AudioInfo();
2741  }
2742 
2743  // if we don't have a video stream we still need to make sure some
2744  // video params are set properly
2745  if (m_selectedTrack[kTrackTypeVideo].m_av_stream_index == -1)
2746  {
2747  QString tvformat = gCoreContext->GetSetting("TVFormat").toLower();
2748  if (tvformat == "ntsc" || tvformat == "ntsc-jp" ||
2749  tvformat == "pal-m" || tvformat == "atsc")
2750  {
2751  m_fps = 29.97;
2752  m_parent->SetVideoParams(-1, -1, 29.97);
2753  }
2754  else
2755  {
2756  m_fps = 25.0;
2757  m_parent->SetVideoParams(-1, -1, 25.0);
2758  }
2759  }
2760 
2761  if (m_parent->IsErrored())
2762  scanerror = -1;
2763 
2764  ScanDSMCCStreams();
2765 
2766  return scanerror;
2767 }
2768 
2769 bool AvFormatDecoder::OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
2770 {
2771  QMutexLocker locker(avcodeclock);
2772 
2773 #ifdef USING_MEDIACODEC
2774  if (QString("mediacodec") == codec->wrapper_name)
2775  av_jni_set_java_vm(QAndroidJniEnvironment::javaVM(), nullptr);
2776 #endif
2777  int ret = avcodec_open2(avctx, codec, nullptr);
2778  if (ret < 0)
2779  {
2780  char error[AV_ERROR_MAX_STRING_SIZE];
2781 
2782  av_make_error_string(error, sizeof(error), ret);
2783  LOG(VB_GENERAL, LOG_ERR, LOC +
2784  QString("Could not open codec 0x%1, id(%2) type(%3) "
2785  "ignoring. reason %4").arg((uint64_t)avctx,0,16)
2786  .arg(ff_codec_id_string(avctx->codec_id))
2787  .arg(ff_codec_type_string(avctx->codec_type))
2788  .arg(error));
2789  return false;
2790  }
2791 
2792  LOG(VB_GENERAL, LOG_INFO, LOC +
2793  QString("Opened codec 0x%1, id(%2) type(%3)")
2794  .arg((uint64_t)avctx,0,16)
2795  .arg(ff_codec_id_string(avctx->codec_id))
2796  .arg(ff_codec_type_string(avctx->codec_type)));
2797  return true;
2798 }
2799 
2801 {
2803 }
2804 
2805 bool AvFormatDecoder::DoRewindSeek(long long desiredFrame)
2806 {
2807  return DecoderBase::DoRewindSeek(desiredFrame);
2808 }
2809 
2810 void AvFormatDecoder::DoFastForwardSeek(long long desiredFrame, bool &needflush)
2811 {
2812  DecoderBase::DoFastForwardSeek(desiredFrame, needflush);
2813 }
2814 
2817 {
2818  for (uint i = 0; i < (uint) m_tracks[kTrackTypeTeletextCaptions].size(); i++)
2819  {
2820  if (m_tracks[kTrackTypeTeletextCaptions][i].m_language_index == lang_idx)
2821  {
2822  return m_tracks[kTrackTypeTeletextCaptions][i].m_language;
2823  }
2824  }
2825 
2826  return iso639_str3_to_key("und");
2827 }
2829 int AvFormatDecoder::GetSubtitleLanguage(uint subtitle_index, uint stream_index)
2830 {
2831  (void)subtitle_index;
2832  AVDictionaryEntry *metatag =
2833  av_dict_get(m_ic->streams[stream_index]->metadata, "language", nullptr, 0);
2834  return metatag ? get_canonical_lang(metatag->value) :
2835  iso639_str3_to_key("und");
2836 }
2837 
2839 int AvFormatDecoder::GetCaptionLanguage(TrackTypes trackType, int service_num)
2840 {
2841  int ret = -1;
2842  for (uint i = 0; i < (uint) m_pmt_track_types.size(); i++)
2843  {
2844  if ((m_pmt_track_types[i] == trackType) &&
2845  (m_pmt_tracks[i].m_stream_id == service_num))
2846  {
2847  ret = m_pmt_tracks[i].m_language;
2848  if (!iso639_is_key_undefined(ret))
2849  return ret;
2850  }
2851  }
2852 
2853  for (uint i = 0; i < (uint) m_stream_track_types.size(); i++)
2854  {
2855  if ((m_stream_track_types[i] == trackType) &&
2856  (m_stream_tracks[i].m_stream_id == service_num))
2857  {
2858  ret = m_stream_tracks[i].m_language;
2859  if (!iso639_is_key_undefined(ret))
2860  return ret;
2861  }
2862  }
2863 
2864  return ret;
2865 }
2866 
2867 int AvFormatDecoder::GetAudioLanguage(uint audio_index, uint stream_index)
2868 {
2869  return GetSubtitleLanguage(audio_index, stream_index);
2870 }
2871 
2873 {
2875  AVStream *stream = m_ic->streams[stream_index];
2876 
2877  if (m_ic->cur_pmt_sect) // mpeg-ts
2878  {
2879  const ProgramMapTable pmt(PSIPTable(m_ic->cur_pmt_sect));
2880  switch (pmt.GetAudioType(stream_index))
2881  {
2882  case 0x01 :
2884  break;
2885  case 0x02 :
2887  break;
2888  case 0x03 :
2890  break;
2891  case 0x00 :
2892  default:
2894  }
2895  }
2896  else // all other containers
2897  {
2898  // We only support labelling/filtering of these two types for now
2899  if (stream->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
2901  else if (stream->disposition & AV_DISPOSITION_COMMENT)
2903  else if (stream->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
2905  else if (stream->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
2907  }
2908 
2909  return type;
2910 }
2911 
2925 {
2926  QMutexLocker locker(avcodeclock);
2927 
2928  // Find the position of the streaminfo in m_tracks[kTrackTypeAudio]
2929  sinfo_vec_t::iterator current = m_tracks[kTrackTypeAudio].begin();
2930  for (; current != m_tracks[kTrackTypeAudio].end(); ++current)
2931  {
2932  if (current->m_av_stream_index == streamIndex)
2933  break;
2934  }
2935 
2936  if (current == m_tracks[kTrackTypeAudio].end())
2937  {
2938  LOG(VB_GENERAL, LOG_WARNING, LOC +
2939  QString("Invalid stream index passed to "
2940  "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
2941 
2942  return;
2943  }
2944 
2945  // Remove the extra substream or duplicate the current substream
2946  sinfo_vec_t::iterator next = current + 1;
2947  if (current->m_av_substream_index == -1)
2948  {
2949  // Split stream in two (Language I + Language II)
2950  StreamInfo lang1 = *current;
2951  StreamInfo lang2 = *current;
2952  lang1.m_av_substream_index = 0;
2953  lang2.m_av_substream_index = 1;
2954  *current = lang1;
2955  m_tracks[kTrackTypeAudio].insert(next, lang2);
2956  return;
2957  }
2958 
2959  if ((next == m_tracks[kTrackTypeAudio].end()) ||
2960  (next->m_av_stream_index != streamIndex))
2961  {
2962  QString msg = QString(
2963  "Expected substream 1 (Language I) of stream %1\n\t\t\t"
2964  "following substream 0, found end of list or another stream.")
2965  .arg(streamIndex);
2966 
2967  LOG(VB_GENERAL, LOG_WARNING, LOC + msg);
2968 
2969  return;
2970  }
2971 
2972  // Remove extra stream info
2973  StreamInfo stream = *current;
2974  stream.m_av_substream_index = -1;
2975  *current = stream;
2976  m_tracks[kTrackTypeAudio].erase(next);
2977 }
2978 
2984 {
2985  if (!m_audio->HasAudioIn())
2986  return;
2987 
2988  QMutexLocker locker(avcodeclock);
2989  for (uint i = 0; i < m_ic->nb_streams;)
2990  {
2991  AVStream *st = m_ic->streams[i];
2992  AVCodecContext *avctx = gCodecMap->hasCodecContext(st);
2993  if (avctx && avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2994  {
2996  av_remove_stream(m_ic, st->id, 0);
2997  i--;
2998  }
2999  else
3000  i++;
3001  }
3002 }
3003 
3004 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
3005 {
3006  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3007 
3008  if (!IS_DR1_PIX_FMT(c->pix_fmt))
3009  {
3010  nd->m_directrendering = false;
3011  return avcodec_default_get_buffer2(c, pic, flags);
3012  }
3013  nd->m_directrendering = true;
3014 
3015  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3016 
3017  if (!frame)
3018  return -1;
3019 
3020  for (int i = 0; i < 3; i++)
3021  {
3022  pic->data[i] = frame->buf + frame->offsets[i];
3023  pic->linesize[i] = frame->pitches[i];
3024  }
3025 
3026  pic->opaque = frame;
3027  pic->reordered_opaque = c->reordered_opaque;
3028 
3029  // Set release method
3030  AVBufferRef *buffer =
3031  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3032  pic->buf[0] = buffer;
3033 
3034  return 0;
3035 }
3036 
3037 void release_avf_buffer(void *opaque, uint8_t *data)
3038 {
3039  AvFormatDecoder *nd = (AvFormatDecoder *)opaque;
3040  VideoFrame *frame = (VideoFrame*)data;
3041 
3042  if (nd && nd->GetPlayer())
3043  nd->GetPlayer()->DeLimboFrame(frame);
3044 }
3045 
3046 #if defined(USING_VAAPI2) || defined(USING_NVDEC)
3047 static void dummy_release_avf_buffer(void * /*opaque*/, uint8_t * /*data*/)
3048 {
3049 }
3050 #endif
3051 
3052 #ifdef USING_VDPAU
3053 int get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3054 {
3055  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3056  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3057 
3058  struct vdpau_render_state *render = (struct vdpau_render_state *)frame->buf;
3060 
3061  for (int i = 0; i < 4; i++)
3062  {
3063  pic->data[i] = nullptr;
3064  pic->linesize[i] = 0;
3065  }
3066  pic->opaque = frame;
3067  pic->data[3] = (uint8_t*)(uintptr_t)render->surface;
3068  frame->pix_fmt = c->pix_fmt;
3069  pic->reordered_opaque = c->reordered_opaque;
3070 
3071  // Set release method
3072  AVBufferRef *buffer =
3073  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer_vdpau, nd, 0);
3074  pic->buf[0] = buffer;
3075 
3076  return 0;
3077 }
3078 
3079 void release_avf_buffer_vdpau(void *opaque, uint8_t *data)
3080 {
3081  AvFormatDecoder *nd = (AvFormatDecoder *)opaque;
3082  VideoFrame *frame = (VideoFrame*)data;
3083 
3084  struct vdpau_render_state *render = (struct vdpau_render_state *)frame->buf;
3086 
3087  if (nd && nd->GetPlayer())
3088  nd->GetPlayer()->DeLimboFrame(frame);
3089 }
3090 
3091 int render_wrapper_vdpau(struct AVCodecContext *s, AVFrame *src,
3092  const VdpPictureInfo *info,
3093  uint32_t count,
3094  const VdpBitstreamBuffer *buffers)
3095 {
3096  if (!src)
3097  return -1;
3098 
3099  if (s && src && s->opaque && src->opaque)
3100  {
3101  AvFormatDecoder *nd = (AvFormatDecoder *)(s->opaque);
3102  VideoFrame *frame = (VideoFrame *)src->opaque;
3103  struct vdpau_render_state data;
3104 
3105  data.surface = (VdpVideoSurface)(uintptr_t)src->data[3];
3106  data.bitstream_buffers_used = count;
3107  data.bitstream_buffers = (VdpBitstreamBuffer*)buffers;
3108 
3109  // Store information we will require in DrawSlice()
3110  frame->priv[0] = (unsigned char*)&data;
3111  frame->priv[1] = (unsigned char*)info;
3112 
3113  nd->GetPlayer()->DrawSlice(frame, 0, 0, 0, 0);
3114  }
3115  else
3116  {
3117  LOG(VB_GENERAL, LOG_ERR, LOC +
3118  "render_wrapper_vdpau called with bad avctx or src");
3119  }
3120 
3121  return 0;
3122 }
3123 #endif // USING_VDPAU
3124 
3125 #ifdef USING_DXVA2
3126 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3127 {
3128  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3129  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3130 
3131  for (int i = 0; i < 4; i++)
3132  {
3133  pic->data[i] = nullptr;
3134  pic->linesize[i] = 0;
3135  }
3136  pic->opaque = frame;
3137  frame->pix_fmt = c->pix_fmt;
3138  pic->reordered_opaque = c->reordered_opaque;
3139  pic->data[0] = (uint8_t*)frame->buf;
3140  pic->data[3] = (uint8_t*)frame->buf;
3141 
3142  // Set release method
3143  AVBufferRef *buffer =
3144  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3145  pic->buf[0] = buffer;
3146 
3147  return 0;
3148 }
3149 #endif
3150 
3151 #ifdef USING_VAAPI
3152 int get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3153 {
3154  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3155  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3156 
3157  for (int i = 0; i < 4; i++)
3158  {
3159  pic->data[i] = nullptr;
3160  pic->linesize[i] = 0;
3161  }
3162  pic->opaque = frame;
3163  frame->pix_fmt = c->pix_fmt;
3164 
3165  if (nd->GetPlayer())
3166  {
3167  nd->GetPlayer()->GetDecoderContext(frame->buf, pic->data[3]);
3168  }
3169 
3170  // Set release method
3171  AVBufferRef *buffer =
3172  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3173  pic->buf[0] = buffer;
3174 
3175  return 0;
3176 }
3177 #endif
3178 
3179 #ifdef USING_VAAPI2
3180 int get_avf_buffer_vaapi2(struct AVCodecContext *c, AVFrame *pic, int flags)
3181 {
3182  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3183  nd->m_directrendering = false;
3184  return avcodec_default_get_buffer2(c, pic, flags);
3185 }
3186 #endif
3187 #ifdef USING_NVDEC
3188 int get_avf_buffer_nvdec(struct AVCodecContext *c, AVFrame *pic, int flags)
3189 {
3190  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3191  nd->m_directrendering = false;
3192  return avcodec_default_get_buffer2(c, pic, flags);
3193 }
3194 #endif
3195 
3196 void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
3197 {
3198  if (!buf_size)
3199  return;
3200 
3201  // closed caption data
3202  //cc_data() {
3203  // reserved 1 0.0 1
3204  // process_cc_data_flag 1 0.1 bslbf
3205  bool process_cc_data = (buf[0] & 0x40) != 0;
3206  if (!process_cc_data)
3207  return; // early exit if process_cc_data_flag false
3208 
3209  // additional_data_flag 1 0.2 bslbf
3210  //bool additional_data = buf[0] & 0x20;
3211  // cc_count 5 0.3 uimsbf
3212  uint cc_count = buf[0] & 0x1f;
3213  // em_data 8 1.0
3214 
3215  if (buf_size < 2+(3*cc_count))
3216  return;
3217 
3218  DecodeCCx08(buf+2, cc_count*3, scte);
3219 }
3220 
3221 void AvFormatDecoder::DecodeCCx08(const uint8_t *buf, uint buf_size, bool scte)
3222 {
3223  if (buf_size < 3)
3224  return;
3225 
3226  bool had_608 = false, had_708 = false;
3227  for (uint cur = 0; cur + 3 < buf_size; cur += 3)
3228  {
3229  uint cc_code = buf[cur];
3230  bool cc_valid = (cc_code & 0x04) != 0U;
3231 
3232  uint data1 = buf[cur+1];
3233  uint data2 = buf[cur+2];
3234  uint data = (data2 << 8) | data1;
3235  uint cc_type = cc_code & 0x03;
3236 
3237  if (!cc_valid)
3238  {
3239  if (cc_type >= 0x2)
3241  continue;
3242  }
3243 
3244  if (scte || cc_type <= 0x1) // EIA-608 field-1/2
3245  {
3246  uint field;
3247  if (cc_type == 0x2)
3248  {
3249  // SCTE repeated field
3250  field = (m_last_scte_field == 0 ? 1 : 0);
3251  m_invert_scte_field = (m_invert_scte_field == 0 ? 1 : 0);
3252  }
3253  else
3254  {
3255  field = cc_type ^ m_invert_scte_field;
3256  }
3257 
3259  {
3260  // in film mode, we may start at the wrong field;
3261  // correct if XDS start/cont/end code is detected
3262  // (must be field 2)
3263  if (scte && field == 0 &&
3264  (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
3265  {
3266  if (cc_type == 1)
3267  m_invert_scte_field = 0;
3268  field = 1;
3269 
3270  // flush decoder
3271  m_ccd608->FormatCC(0, -1, -1);
3272  }
3273 
3274  had_608 = true;
3275  m_ccd608->FormatCCField(m_lastccptsu / 1000, field, data);
3276 
3277  m_last_scte_field = field;
3278  }
3279  }
3280  else
3281  {
3282  had_708 = true;
3283  m_ccd708->decode_cc_data(cc_type, data1, data2);
3284  }
3285  }
3286  UpdateCaptionTracksFromStreams(had_608, had_708);
3287 }
3288 
3290  bool check_608, bool check_708)
3291 {
3292  bool need_change_608 = false;
3293  bool seen_608[4];
3294  if (check_608)
3295  {
3296  m_ccd608->GetServices(15/*seconds*/, seen_608);
3297  for (uint i = 0; i < 4; i++)
3298  {
3299  need_change_608 |= (seen_608[i] && !m_ccX08_in_tracks[i]) ||
3300  (!seen_608[i] && m_ccX08_in_tracks[i] && !m_ccX08_in_pmt[i]);
3301  }
3302  }
3303 
3304  bool need_change_708 = false;
3305  bool seen_708[64];
3306  if (check_708 || need_change_608)
3307  {
3308  m_ccd708->services(15/*seconds*/, seen_708);
3309  for (uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
3310  {
3311  need_change_708 |= (seen_708[i] && !m_ccX08_in_tracks[i+4]) ||
3312  (!seen_708[i] && m_ccX08_in_tracks[i+4] && !m_ccX08_in_pmt[i+4]);
3313  }
3314  if (need_change_708 && !check_608)
3315  m_ccd608->GetServices(15/*seconds*/, seen_608);
3316  }
3317 
3318  if (!need_change_608 && !need_change_708)
3319  return;
3320 
3322 
3323  m_stream_tracks.clear();
3324  m_stream_track_types.clear();
3326  int lang = iso639_str3_to_key("und");
3327  for (uint i = 1; i < 64; i++)
3328  {
3329  if (seen_708[i] && !m_ccX08_in_pmt[i+4])
3330  {
3331  StreamInfo si(av_index, lang, 0/*lang_idx*/,
3332  i, 0, false/*easy*/, true/*wide*/);
3333  m_stream_tracks.push_back(si);
3335  }
3336  }
3337  for (uint i = 0; i < 4; i++)
3338  {
3339  if (seen_608[i] && !m_ccX08_in_pmt[i])
3340  {
3341  if (0==i)
3343  else if (2==i)
3345  else
3346  lang = iso639_str3_to_key("und");
3347 
3348  StreamInfo si(av_index, lang, 0/*lang_idx*/,
3349  i+1, 0, false/*easy*/, false/*wide*/);
3350  m_stream_tracks.push_back(si);
3352  }
3353  }
3355 }
3356 
3358  AVPacket *pkt, bool can_reliably_parse_keyframes)
3359 {
3360  if (m_prevgoppos != 0 && m_keyframedist != 1)
3361  {
3362  int tempKeyFrameDist = m_framesRead - 1 - m_prevgoppos;
3363  bool reset_kfd = false;
3364 
3365  if (!m_gopset || m_livetv) // gopset: we've seen 2 keyframes
3366  {
3367  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3368  "gopset not set, syncing positionMap");
3369  SyncPositionMap();
3370  if (tempKeyFrameDist > 0 && !m_livetv)
3371  {
3372  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3373  QString("Initial key frame distance: %1.")
3374  .arg(m_keyframedist));
3375  m_gopset = true;
3376  reset_kfd = true;
3377  }
3378  }
3379  else if (m_keyframedist != tempKeyFrameDist && tempKeyFrameDist > 0)
3380  {
3381  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3382  QString("Key frame distance changed from %1 to %2.")
3383  .arg(m_keyframedist).arg(tempKeyFrameDist));
3384  reset_kfd = true;
3385  }
3386 
3387  if (reset_kfd)
3388  {
3389  m_keyframedist = tempKeyFrameDist;
3391 
3393 
3394 #if 0
3395  // also reset length
3396  QMutexLocker locker(&m_positionMapLock);
3397  if (!m_positionMap.empty())
3398  {
3399  long long index = m_positionMap.back().index;
3400  long long totframes = index * m_keyframedist;
3401  uint length = (uint)((totframes * 1.0F) / m_fps);
3402  m_parent->SetFileLength(length, totframes);
3403  }
3404 #endif
3405  }
3406  }
3407 
3409 
3410  if (can_reliably_parse_keyframes &&
3412  {
3413  long long last_frame = 0;
3414  {
3415  QMutexLocker locker(&m_positionMapLock);
3416  if (!m_positionMap.empty())
3417  last_frame = m_positionMap.back().index;
3418  }
3419 
3420 #if 0
3421  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
3422  QString("framesRead: %1 last_frame: %2 keyframedist: %3")
3423  .arg(m_framesRead) .arg(last_frame) .arg(m_keyframedist));
3424 #endif
3425 
3426  // if we don't have an entry, fill it in with what we've just parsed
3427  if (m_framesRead > last_frame && m_keyframedist > 0)
3428  {
3429  long long startpos = pkt->pos;
3430 
3431  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3432  QString("positionMap[ %1 ] == %2.")
3433  .arg(m_framesRead).arg(startpos));
3434 
3435  PosMapEntry entry = {m_framesRead, m_framesRead, startpos};
3436 
3437  QMutexLocker locker(&m_positionMapLock);
3438  // Create a dummy positionmap entry for frame 0 so that
3439  // seeking will work properly. (See
3440  // DecoderBase::FindPosition() which subtracts
3441  // DecoderBase::indexOffset from each frame number.)
3442  if (m_positionMap.empty())
3443  {
3444  PosMapEntry dur = {0, 0, 0};
3445  m_positionMap.push_back(dur);
3446  }
3447  m_positionMap.push_back(entry);
3449  {
3451  llround(m_totalDuration.num * 1000.0 / m_totalDuration.den);
3453  }
3454  }
3455 
3456 #if 0
3457  // If we are > 150 frames in and saw no positionmap at all, reset
3458  // length based on the actual bitrate seen so far
3460  {
3461  m_bitrate = (int)((pkt->pos * 8 * m_fps) / (m_framesRead - 1));
3462  float bytespersec = (float)m_bitrate / 8;
3463  float secs = ringBuffer->GetRealFileSize() * 1.0 / bytespersec;
3464  m_parent->SetFileLength((int)(secs), (int)(secs * m_fps));
3465  }
3466 #endif
3467  }
3468 }
3469 
3470 #define SEQ_START 0x000001b3
3471 #define GOP_START 0x000001b8
3472 #define PICTURE_START 0x00000100
3473 #define SLICE_MIN 0x00000101
3474 #define SLICE_MAX 0x000001af
3475 #define SEQ_END_CODE 0x000001b7
3476 
3477 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
3478 {
3479  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3480  const uint8_t *bufptr = pkt->data;
3481  const uint8_t *bufend = pkt->data + pkt->size;
3482 
3483  while (bufptr < bufend)
3484  {
3485  bufptr = avpriv_find_start_code(bufptr, bufend, &m_start_code_state);
3486 
3487  float aspect_override = -1.0F;
3488  if (ringBuffer->IsDVD())
3489  aspect_override = ringBuffer->DVD()->GetAspectOverride();
3490 
3492  continue;
3494  {
3495  if (bufptr + 11 >= pkt->data + pkt->size)
3496  continue; // not enough valid data...
3497  SequenceHeader *seq = reinterpret_cast<SequenceHeader*>(
3498  const_cast<uint8_t*>(bufptr));
3499 
3500  uint width = seq->width() >> context->lowres;
3501  uint height = seq->height() >> context->lowres;
3502  m_current_aspect = seq->aspect(context->codec_id ==
3503  AV_CODEC_ID_MPEG1VIDEO);
3504  if (stream->sample_aspect_ratio.num)
3505  m_current_aspect = av_q2d(stream->sample_aspect_ratio) *
3506  width / height;
3507  if (aspect_override > 0.0F)
3508  m_current_aspect = aspect_override;
3509  float seqFPS = seq->fps();
3510 
3511  bool changed =
3512  (seqFPS > static_cast<float>(m_fps)+0.01F) ||
3513  (seqFPS < static_cast<float>(m_fps)-0.01F);
3514  changed |= (width != (uint)m_current_width );
3515  changed |= (height != (uint)m_current_height);
3516 
3517  if (changed)
3518  {
3519  if (m_private_dec)
3520  m_private_dec->Reset();
3521 
3522  m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
3523 
3524  m_current_width = width;
3525  m_current_height = height;
3526  m_fps = seqFPS;
3527 
3528  m_gopset = false;
3529  m_prevgoppos = 0;
3531  m_firstvptsinuse = true;
3532  m_faulty_pts = m_faulty_dts = 0;
3535  m_pts_detected = false;
3536  m_reordered_pts_detected = false;
3537 
3538  // fps debugging info
3539  float avFPS = normalized_fps(stream, context);
3540  if ((seqFPS > avFPS+0.01F) || (seqFPS < avFPS-0.01F))
3541  {
3542  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3543  QString("avFPS(%1) != seqFPS(%2)")
3544  .arg(avFPS).arg(seqFPS));
3545  }
3546  }
3547 
3548  m_seq_count++;
3549 
3550  if (!m_seen_gop && m_seq_count > 1)
3551  {
3552  HandleGopStart(pkt, true);
3553  pkt->flags |= AV_PKT_FLAG_KEY;
3554  }
3555  }
3556  else if (GOP_START == m_start_code_state)
3557  {
3558  HandleGopStart(pkt, true);
3559  m_seen_gop = true;
3560  pkt->flags |= AV_PKT_FLAG_KEY;
3561  }
3562  }
3563 }
3564 
3565 // Returns the number of frame starts identified in the packet.
3566 int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
3567 {
3568  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3569  const uint8_t *buf = pkt->data;
3570  const uint8_t *buf_end = pkt->data + pkt->size;
3571  int num_frames = 0;
3572 
3573  // crude NAL unit vs Annex B detection.
3574  // the parser only understands Annex B
3575  if (context->extradata && context->extradata_size >= 4)
3576  {
3577  int nal_size = 0;
3578  int size_length = (context->extradata[4] & 0x3) + 1;
3579 
3580  for (int i = 0; i < size_length; i++)
3581  nal_size += buf[i];
3582 
3583  if (nal_size)
3584  {
3585  if (pkt->flags & AV_PKT_FLAG_KEY)
3586  HandleGopStart(pkt, false);
3587  return 1;
3588  }
3589  }
3590 
3591  while (buf < buf_end)
3592  {
3593  buf += m_h264_parser->addBytes(buf, buf_end - buf, 0);
3594 
3595  if (m_h264_parser->stateChanged())
3596  {
3598  {
3599  if (m_h264_parser->onFrameStart())
3600  ++num_frames;
3601 
3603  continue;
3604  }
3605  else
3606  {
3607  continue;
3608  }
3609  }
3610  else
3611  {
3612  continue;
3613  }
3614 
3618  float seqFPS = m_h264_parser->frameRate();
3619 
3620  bool res_changed = ((width != (uint)m_current_width) ||
3621  (height != (uint)m_current_height));
3622  bool fps_changed =
3623  (seqFPS > 0.0F) &&
3624  ((seqFPS > static_cast<float>(m_fps) + 0.01F) ||
3625  (seqFPS < static_cast<float>(m_fps) - 0.01F));
3626 
3627  if (fps_changed || res_changed)
3628  {
3629  if (m_private_dec)
3630  m_private_dec->Reset();
3631 
3632  m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
3633 
3634  m_current_width = width;
3635  m_current_height = height;
3636 
3637  if (seqFPS > 0.0F)
3638  m_fps = seqFPS;
3639 
3640  m_gopset = false;
3641  m_prevgoppos = 0;
3643  m_firstvptsinuse = true;
3644  m_faulty_pts = m_faulty_dts = 0;
3647  m_pts_detected = false;
3648  m_reordered_pts_detected = false;
3649 
3650  // fps debugging info
3651  float avFPS = normalized_fps(stream, context);
3652  if ((seqFPS > avFPS+0.01F) || (seqFPS < avFPS-0.01F))
3653  {
3654  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3655  QString("avFPS(%1) != seqFPS(%2)")
3656  .arg(avFPS).arg(seqFPS));
3657  }
3658 
3659  }
3660 
3661  HandleGopStart(pkt, true);
3662  pkt->flags |= AV_PKT_FLAG_KEY;
3663  }
3664 
3665  return num_frames;
3666 }
3667 
3668 bool AvFormatDecoder::PreProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3669 {
3670  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3671  int num_frames = 1;
3672 
3673  if (CODEC_IS_FFMPEG_MPEG(context->codec_id))
3674  {
3675  MpegPreProcessPkt(curstream, pkt);
3676  }
3677  else if (CODEC_IS_H264(context->codec_id))
3678  {
3679  num_frames = H264PreProcessPkt(curstream, pkt);
3680  }
3681  else
3682  {
3683  if (pkt->flags & AV_PKT_FLAG_KEY)
3684  {
3685  HandleGopStart(pkt, false);
3686  m_seen_gop = true;
3687  }
3688  else
3689  {
3690  m_seq_count++;
3691  if (!m_seen_gop && m_seq_count > 1)
3692  {
3693  HandleGopStart(pkt, false);
3694  }
3695  }
3696  }
3697 
3698  if (m_framesRead == 0 && !m_justAfterChange &&
3699  !(pkt->flags & AV_PKT_FLAG_KEY))
3700  {
3701  av_packet_unref(pkt);
3702  return false;
3703  }
3704 
3705  m_framesRead += num_frames;
3706 
3708  {
3709  // The ffmpeg libraries represent a frame interval of a
3710  // 59.94fps video as 1501/90000 seconds, when it should
3711  // actually be 1501.5/90000 seconds.
3712  AVRational pkt_dur = AVRationalInit(pkt->duration);
3713  pkt_dur = av_mul_q(pkt_dur, curstream->time_base);
3714  if (pkt_dur.num == 1501 && pkt_dur.den == 90000)
3715  pkt_dur = AVRationalInit(1001, 60000); // 1501.5/90000
3716  m_totalDuration = av_add_q(m_totalDuration, pkt_dur);
3717  }
3718 
3719  m_justAfterChange = false;
3720 
3721  if (m_exitafterdecoded)
3722  m_gotVideoFrame = true;
3723 
3724  return true;
3725 }
3726 
3727 // Maximum retries - 500 = 5 seconds
3728 #define PACKET_MAX_RETRIES 5000
3729 #define RETRY_WAIT_TIME 10000 // microseconds
3730 bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3731 {
3732  int retryCount = 0;
3733  int gotpicture = 0;
3734  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3735  MythAVFrame mpa_pic;
3736  if (!mpa_pic)
3737  {
3738  return false;
3739  }
3740  mpa_pic->reordered_opaque = AV_NOPTS_VALUE;
3741 
3742  if (pkt->pts != AV_NOPTS_VALUE)
3743  m_pts_detected = true;
3744 
3745  bool tryAgain = true;
3746  bool sentPacket = false;
3747  while (tryAgain)
3748  {
3749  int ret, ret2 = 0;
3750  tryAgain = false;
3751  gotpicture = 0;
3752  avcodeclock->lock();
3753  if (m_private_dec)
3754  {
3755  if (QString(m_ic->iformat->name).contains("avi") || !m_pts_detected)
3756  pkt->pts = pkt->dts;
3757  // TODO disallow private decoders for dvd playback
3758  // N.B. we do not reparse the frame as it breaks playback for
3759  // everything but libmpeg2
3760  ret = m_private_dec->GetFrame(curstream, mpa_pic, &gotpicture, pkt);
3761  sentPacket = true;
3762  }
3763  else
3764  {
3765  if (!m_use_frame_timing)
3766  context->reordered_opaque = pkt->pts;
3767 
3768  // SUGGESTION
3769  // Now that avcodec_decode_video2 is deprecated and replaced
3770  // by 2 calls (receive frame and send packet), this could be optimized
3771  // into separate routines or separate threads.
3772  // Also now that it always consumes a whole buffer some code
3773  // in the caller may be able to be optimized.
3774 
3775  // FilteredReceiveFrame will call avcodec_receive_frame and
3776  // apply any codec-dependent filtering
3777  ret = m_mythcodecctx->FilteredReceiveFrame(context, mpa_pic);
3778 
3779  if (ret == 0)
3780  gotpicture = 1;
3781  else
3782  gotpicture = 0;
3783  if (ret == AVERROR(EAGAIN))
3784  ret = 0;
3785  // If we got a picture do not send the packet until we have
3786  // all available pictures
3787  if (ret==0 && !gotpicture)
3788  {
3789  ret2 = avcodec_send_packet(context, pkt);
3790  if (ret2 == AVERROR(EAGAIN))
3791  {
3792  tryAgain = true;
3793  ret2 = 0;
3794  }
3795  else
3796  {
3797  sentPacket = true;
3798  }
3799  }
3800  }
3801  avcodeclock->unlock();
3802 
3803  if (ret < 0 || ret2 < 0)
3804  {
3805  char error[AV_ERROR_MAX_STRING_SIZE];
3806  if (ret < 0)
3807  {
3808  LOG(VB_GENERAL, LOG_ERR, LOC +
3809  QString("video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
3810  .arg(av_make_error_string(error, sizeof(error), ret))
3811  .arg(ret).arg(gotpicture));
3812  }
3813  if (ret2 < 0)
3814  LOG(VB_GENERAL, LOG_ERR, LOC +
3815  QString("video avcodec_send_packet error: %1 (%2) gotpicture:%3")
3816  .arg(av_make_error_string(error, sizeof(error), ret2))
3817  .arg(ret2).arg(gotpicture));
3819  {
3820  // If erroring on GPU assist, try switching to software decode
3822  m_parent->SetErrored(QObject::tr("Video Decode Error"));
3823  else
3824  m_streams_changed = true;
3825  }
3826  if (ret == AVERROR_EXTERNAL || ret2 == AVERROR_EXTERNAL)
3827  m_streams_changed = true;
3828  return false;
3829  }
3830 
3831  if (tryAgain)
3832  {
3833  if (++retryCount > PACKET_MAX_RETRIES)
3834  {
3835  LOG(VB_GENERAL, LOG_ERR, LOC +
3836  QString("ERROR: Video decode buffering retries exceeded maximum"));
3837  return false;
3838  }
3839  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3840  QString("Video decode buffering retry"));
3841  usleep(RETRY_WAIT_TIME);
3842  }
3843  }
3844  // averror_count counts sequential errors, so if you have a successful
3845  // packet then reset it
3846  m_averror_count = 0;
3847  if (gotpicture)
3848  {
3849  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3850  QString("video timecodes packet-pts:%1 frame-pts:%2 packet-dts: %3 frame-dts:%4")
3851  .arg(pkt->pts).arg(mpa_pic->pts).arg(pkt->pts)
3852  .arg(mpa_pic->pkt_dts));
3853 
3854  if (!m_use_frame_timing)
3855  {
3856  int64_t pts = 0;
3857 
3858  // Detect faulty video timestamps using logic from ffplay.
3859  if (pkt->dts != AV_NOPTS_VALUE)
3860  {
3862  m_last_dts_for_fault_detection = pkt->dts;
3863  }
3864  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3865  {
3866  m_faulty_pts += (mpa_pic->reordered_opaque <= m_last_pts_for_fault_detection);
3867  m_last_pts_for_fault_detection = mpa_pic->reordered_opaque;
3868  m_reordered_pts_detected = true;
3869  }
3870 
3871  // Explicity use DTS for DVD since they should always be valid for every
3872  // frame and fixups aren't enabled for DVD.
3873  // Select reordered_opaque (PTS) timestamps if they are less faulty or the
3874  // the DTS timestamp is missing. Also use fixups for missing PTS instead of
3875  // DTS to avoid oscillating between PTS and DTS. Only select DTS if PTS is
3876  // more faulty or never detected.
3878  {
3879  if (pkt->dts != AV_NOPTS_VALUE)
3880  pts = pkt->dts;
3881  m_pts_selected = false;
3882  }
3883  else if (ringBuffer->IsDVD())
3884  {
3885  if (pkt->dts != AV_NOPTS_VALUE)
3886  pts = pkt->dts;
3887  m_pts_selected = false;
3888  }
3890  mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3891  {
3892  pts = mpa_pic->reordered_opaque;
3893  m_pts_selected = true;
3894  }
3896  {
3897  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3898  pts = mpa_pic->reordered_opaque;
3899  m_pts_selected = true;
3900  }
3901  else if (pkt->dts != AV_NOPTS_VALUE)
3902  {
3903  pts = pkt->dts;
3904  m_pts_selected = false;
3905  }
3906 
3907  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG, LOC +
3908  QString("video packet timestamps reordered %1 pts %2 dts %3 (%4)")
3909  .arg(mpa_pic->reordered_opaque).arg(pkt->pts).arg(pkt->dts)
3910  .arg((m_force_dts_timestamps) ? "dts forced" :
3911  (m_pts_selected) ? "reordered" : "dts"));
3912 
3913  mpa_pic->reordered_opaque = pts;
3914  }
3915  ProcessVideoFrame(curstream, mpa_pic);
3916  }
3917  if (!sentPacket)
3918  {
3919  // MythTV logic expects that only one frame is processed
3920  // Save the packet for later and return.
3921  AVPacket *newPkt = new AVPacket;
3922  memset(newPkt, 0, sizeof(AVPacket));
3923  av_init_packet(newPkt);
3924  av_packet_ref(newPkt, pkt);
3925  m_storedPackets.prepend(newPkt);
3926  }
3927  return true;
3928 }
3929 
3930 bool AvFormatDecoder::ProcessVideoFrame(AVStream *stream, AVFrame *mpa_pic)
3931 {
3932 
3933  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3934 
3935  // We need to mediate between ATSC and SCTE data when both are present. If
3936  // both are present, we generally want to prefer ATSC. However, there may
3937  // be large sections of the recording where ATSC is used and other sections
3938  // where SCTE is used. In that case, we want to allow a natural transition
3939  // from ATSC back to SCTE. We do this by allowing 10 consecutive SCTE
3940  // frames, without an intervening ATSC frame, to cause a switch back to
3941  // considering SCTE frames. The number 10 is somewhat arbitrarily chosen.
3942 
3943  uint cc_len = (uint) max(mpa_pic->scte_cc_len,0);
3944  uint8_t *cc_buf = mpa_pic->scte_cc_buf;
3945  bool scte = true;
3946 
3947  // If we saw SCTE, then decrement a nonzero ignore_scte count.
3948  if (cc_len > 0 && m_ignore_scte)
3949  --m_ignore_scte;
3950 
3951  // If both ATSC and SCTE caption data are available, prefer ATSC
3952  if ((mpa_pic->atsc_cc_len > 0) || m_ignore_scte)
3953  {
3954  cc_len = (uint) max(mpa_pic->atsc_cc_len, 0);
3955  cc_buf = mpa_pic->atsc_cc_buf;
3956  scte = false;
3957  // If we explicitly saw ATSC, then reset ignore_scte count.
3958  if (cc_len > 0)
3959  m_ignore_scte = 10;
3960  }
3961 
3962  // Decode CEA-608 and CEA-708 captions
3963  for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
3964  DecodeDTVCC(cc_buf + i, cc_len - i, scte);
3965 
3966  if (cc_len == 0) {
3967  // look for A53 captions
3968  AVFrameSideData *side_data = av_frame_get_side_data(mpa_pic, AV_FRAME_DATA_A53_CC);
3969  if (side_data && (side_data->size > 0)) {
3970  DecodeCCx08(side_data->data, side_data->size, false);
3971  }
3972  }
3973 
3974  VideoFrame *picframe = (VideoFrame *)(mpa_pic->opaque);
3975 
3977  {
3978  // Do nothing, we just want the pts, captions, subtites, etc.
3979  // So we can release the unconverted blank video frame to the
3980  // display queue.
3981  }
3982  else if (!m_directrendering)
3983  {
3984  AVFrame *tmp_frame = nullptr;
3985  AVFrame *use_frame = nullptr;
3986  VideoFrame *xf = picframe;
3987  picframe = m_parent->GetNextVideoFrame();
3988  unsigned char *buf = picframe->buf;
3989  bool used_picframe=false;
3990 #if defined(USING_VAAPI2) || defined(USING_NVDEC)
3991  if (AV_PIX_FMT_CUDA == (AVPixelFormat)mpa_pic->format
3992  || IS_VAAPI_PIX_FMT((AVPixelFormat)mpa_pic->format))
3993  {
3994  int ret = 0;
3995  tmp_frame = av_frame_alloc();
3996  use_frame = tmp_frame;
3997  /* retrieve data from GPU to CPU */
3998  AVPixelFormat *pixelformats = nullptr;
3999  ret = av_hwframe_transfer_get_formats(mpa_pic->hw_frames_ctx,
4000  AV_HWFRAME_TRANSFER_DIRECTION_FROM,
4001  &pixelformats, 0);
4002  if (ret==0)
4003  {
4004  for (AVPixelFormat *format = pixelformats; *format != AV_PIX_FMT_NONE; format++)
4005  {
4006  if (*format == AV_PIX_FMT_YUV420P)
4007  {
4008  // Retrieve the picture directly into the Video Frame Buffer
4009  used_picframe = true;
4010  use_frame->format = AV_PIX_FMT_YUV420P;
4011  for (int i = 0; i < 3; i++)
4012  {
4013  use_frame->data[i] = buf + picframe->offsets[i];
4014  use_frame->linesize[i] = picframe->pitches[i];
4015  }
4016  // Dummy release method - we do not want to free the buffer
4017  AVBufferRef *buffer =
4018  av_buffer_create((uint8_t*)picframe, 0, dummy_release_avf_buffer, this, 0);
4019  use_frame->buf[0] = buffer;
4020  use_frame->width = mpa_pic->width;
4021  use_frame->height = mpa_pic->height;
4022  break;
4023  }
4024  }
4025  }
4026  if ((ret = av_hwframe_transfer_data(use_frame, mpa_pic, 0)) < 0)
4027  {
4028  LOG(VB_GENERAL, LOG_ERR, LOC
4029  + QString("Error %1 transferring the data to system memory")
4030  .arg(ret));
4031  av_frame_free(&use_frame);
4032  return false;
4033  }
4034  av_freep(&pixelformats);
4035  }
4036  else
4037 #endif // USING_VAAPI2 || USING_NVDEC
4038  use_frame = mpa_pic;
4039 
4040  if (!used_picframe)
4041  {
4042  AVFrame tmppicture;
4043  av_image_fill_arrays(tmppicture.data, tmppicture.linesize,
4044  buf, AV_PIX_FMT_YUV420P, use_frame->width,
4045  use_frame->height, IMAGE_ALIGN);
4046  tmppicture.data[0] = buf + picframe->offsets[0];
4047  tmppicture.data[1] = buf + picframe->offsets[1];
4048  tmppicture.data[2] = buf + picframe->offsets[2];
4049  tmppicture.linesize[0] = picframe->pitches[0];
4050  tmppicture.linesize[1] = picframe->pitches[1];
4051  tmppicture.linesize[2] = picframe->pitches[2];
4052 
4053  QSize dim = get_video_dim(*context);
4054  m_sws_ctx = sws_getCachedContext(m_sws_ctx, use_frame->width,
4055  use_frame->height, (AVPixelFormat)use_frame->format,
4056  use_frame->width, use_frame->height,
4057  AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
4058  nullptr, nullptr, nullptr);
4059  if (!m_sws_ctx)
4060  {
4061  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate sws context");
4062  return false;
4063  }
4064  sws_scale(m_sws_ctx, use_frame->data, use_frame->linesize, 0, dim.height(),
4065  tmppicture.data, tmppicture.linesize);
4066  }
4067  if (xf)
4068  {
4069  // Set the frame flags, but then discard it
4070  // since we are not using it for display.
4071  xf->interlaced_frame = mpa_pic->interlaced_frame;
4072  xf->top_field_first = mpa_pic->top_field_first;
4074  xf->aspect = m_current_aspect;
4076  }
4077  if (tmp_frame)
4078  av_frame_free(&tmp_frame);
4079  }
4080  else if (!picframe)
4081  {
4082  LOG(VB_GENERAL, LOG_ERR, LOC + "NULL videoframe - direct rendering not"
4083  "correctly initialized.");
4084  return false;
4085  }
4086 
4087  long long pts;
4088  if (m_use_frame_timing)
4089  {
4090  pts = mpa_pic->pts;
4091  if (pts == AV_NOPTS_VALUE)
4092  pts = mpa_pic->pkt_dts;
4093  if (pts == AV_NOPTS_VALUE)
4094  pts = mpa_pic->reordered_opaque;
4095  if (pts == AV_NOPTS_VALUE)
4096  {
4097  LOG(VB_GENERAL, LOG_ERR, LOC + "No PTS found - unable to process video.");
4098  return false;
4099  }
4100  pts = (long long)(av_q2d(stream->time_base) *
4101  pts * 1000);
4102  }
4103  else
4104  pts = (long long)(av_q2d(stream->time_base) *
4105  mpa_pic->reordered_opaque * 1000);
4106 
4107  long long temppts = pts;
4108  // Validate the video pts against the last pts. If it's
4109  // a little bit smaller, equal or missing, compute
4110  // it from the last. Otherwise assume a wraparound.
4111  if (!ringBuffer->IsDVD() &&
4112  temppts <= m_lastvpts &&
4113  (temppts + (1000 / m_fps) > m_lastvpts || temppts <= 0))
4114  {
4115  temppts = m_lastvpts;
4116  temppts += (long long)(1000 / m_fps);
4117  // MPEG2/H264 frames can be repeated, update pts accordingly
4118  temppts += (long long)(mpa_pic->repeat_pict * 500 / m_fps);
4119  }
4120 
4121  // Calculate actual fps from the pts values.
4122  long long ptsdiff = temppts - m_lastvpts;
4123  double calcfps = 1000.0 / ptsdiff;
4124  if (calcfps < 121.0 && calcfps > 3.0)
4125  {
4126  // If fps has doubled due to frame-doubling deinterlace
4127  // Set fps to double value.
4128  double fpschange = calcfps / static_cast<double>(m_fps);
4129  int prior = m_fpsMultiplier;
4130  if (fpschange > 1.9 && fpschange < 2.1)
4131  m_fpsMultiplier = 2;
4132  if (fpschange > 0.9 && fpschange < 1.1)
4133  m_fpsMultiplier = 1;
4134  if (m_fpsMultiplier != prior)
4136  }
4137 
4138  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4139  QString("video timecode %1 %2 %3 %4%5")
4140  .arg(m_use_frame_timing ? mpa_pic->pts : mpa_pic->reordered_opaque).arg(pts)
4141  .arg(temppts).arg(m_lastvpts)
4142  .arg((pts != temppts) ? " fixup" : ""));
4143 
4144  if (picframe)
4145  {
4146  picframe->interlaced_frame = mpa_pic->interlaced_frame;
4147  picframe->top_field_first = mpa_pic->top_field_first;
4148  picframe->repeat_pict = mpa_pic->repeat_pict;
4149  picframe->disp_timecode = NormalizeVideoTimecode(stream, temppts);
4150  picframe->frameNumber = m_framesPlayed;
4151  picframe->aspect = m_current_aspect;
4152  picframe->dummy = 0;
4153  picframe->directrendering = m_directrendering ? 1 : 0;
4154 
4155  m_parent->ReleaseNextVideoFrame(picframe, temppts);
4156  }
4157 
4158  m_decoded_video_frame = picframe;
4159  m_gotVideoFrame = true;
4160  if (++m_fpsSkip >= m_fpsMultiplier)
4161  {
4162  ++m_framesPlayed;
4163  m_fpsSkip = 0;
4164  }
4165 
4166  m_lastvpts = temppts;
4167  if (!m_firstvpts && m_firstvptsinuse)
4168  m_firstvpts = temppts;
4169 
4170  return true;
4171 }
4172 
4179  const AVStream *stream, const AVPacket *pkt)
4180 {
4181  (void) stream;
4182 
4183  const uint8_t *buf = pkt->data;
4184  uint64_t linemask = 0;
4185  unsigned long long utc = m_lastccptsu;
4186 
4187  // [i]tv0 means there is a linemask
4188  // [I]TV0 means there is no linemask and all lines are present
4189  if ((buf[0]=='t') && (buf[1]=='v') && (buf[2] == '0'))
4190  {
4192  memcpy(&linemask, buf + 3, 8);
4193  buf += 11;
4194  }
4195  else if ((buf[0]=='T') && (buf[1]=='V') && (buf[2] == '0'))
4196  {
4197  linemask = 0xffffffffffffffffLL;
4198  buf += 3;
4199  }
4200  else
4201  {
4202  LOG(VB_VBI, LOG_ERR, LOC + QString("Unknown VBI data stream '%1%2%3'")
4203  .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
4204  return;
4205  }
4206 
4207  static const uint min_blank = 6;
4208  for (uint i = 0; i < 36; i++)
4209  {
4210  if (!((linemask >> i) & 0x1))
4211  continue;
4212 
4213  const uint line = ((i < 18) ? i : i-18) + min_blank;
4214  const uint field = (i<18) ? 0 : 1;
4215  const uint id2 = *buf & 0xf;
4216  switch (id2)
4217  {
4218  case VBI_TYPE_TELETEXT:
4219  // SECAM lines 6-23
4220  // PAL lines 6-22
4221  // NTSC lines 10-21 (rare)
4222  if (m_tracks[kTrackTypeTeletextMenu].empty())
4223  {
4224  StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
4225  m_tracks[kTrackTypeTeletextMenu].push_back(si);
4226  }
4227  m_ttd->Decode(buf+1, VBI_IVTV);
4228  break;
4229  case VBI_TYPE_CC:
4230  // PAL line 22 (rare)
4231  // NTSC line 21
4232  if (21 == line)
4233  {
4234  int data = (buf[2] << 8) | buf[1];
4236  m_ccd608->FormatCCField(utc/1000, field, data);
4237  utc += 33367;
4238  }
4239  break;
4240  case VBI_TYPE_VPS: // Video Programming System
4241  // PAL line 16
4242  m_ccd608->DecodeVPS(buf+1); // a.k.a. PDC
4243  break;
4244  case VBI_TYPE_WSS: // Wide Screen Signal
4245  // PAL line 23
4246  // NTSC line 20
4247  m_ccd608->DecodeWSS(buf+1);
4248  break;
4249  }
4250  buf += 43;
4251  }
4252  m_lastccptsu = utc;
4253  UpdateCaptionTracksFromStreams(true, false);
4254 }
4255 
4261  const AVStream* /*stream*/, const AVPacket *pkt)
4262 {
4263  const uint8_t *buf = pkt->data;
4264  const uint8_t *buf_end = pkt->data + pkt->size;
4265 
4266 
4267  while (buf < buf_end)
4268  {
4269  if (*buf == 0x10)
4270  {
4271  buf++; // skip
4272  }
4273  else if (*buf == 0x02)
4274  {
4275  buf += 4;
4276  if ((buf_end - buf) >= 42)
4277  m_ttd->Decode(buf, VBI_DVB);
4278  buf += 42;
4279  }
4280  else if (*buf == 0x03)
4281  {
4282  buf += 4;
4283  if ((buf_end - buf) >= 42)
4284  m_ttd->Decode(buf, VBI_DVB_SUBTITLE);
4285  buf += 42;
4286  }
4287  else if (*buf == 0xff)
4288  {
4289  buf += 3;
4290  }
4291  else
4292  {
4293  LOG(VB_VBI, LOG_ERR, LOC +
4294  QString("VBI: Unknown descriptor: %1").arg(*buf));
4295  buf += 46;
4296  }
4297  }
4298 }
4299 
4304  const AVStream *str, const AVPacket *pkt)
4305 {
4306 #ifdef USING_MHEG
4307  if (!m_itv && ! (m_itv = m_parent->GetInteractiveTV()))
4308  return;
4309 
4310  // The packet may contain several tables.
4311  uint8_t *data = pkt->data;
4312  int length = pkt->size;
4313  int componentTag, dataBroadcastId;
4314  unsigned carouselId;
4315  {
4316  QMutexLocker locker(avcodeclock);
4317  componentTag = str->component_tag;
4318  dataBroadcastId = str->data_id;
4319  carouselId = (unsigned) str->carousel_id;
4320  }
4321  while (length > 3)
4322  {
4323  uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
4324 
4325  if (sectionLen > length) // This may well be filler
4326  return;
4327 
4328  m_itv->ProcessDSMCCSection(data, sectionLen,
4329  componentTag, carouselId,
4330  dataBroadcastId);
4331  length -= sectionLen;
4332  data += sectionLen;
4333  }
4334 #else
4335  Q_UNUSED(str);
4336  Q_UNUSED(pkt);
4337 #endif // USING_MHEG
4338 }
4339 
4340 bool AvFormatDecoder::ProcessSubtitlePacket(AVStream *curstream, AVPacket *pkt)
4341 {
4342  if (!m_parent->GetSubReader(pkt->stream_index))
4343  return true;
4344 
4345  long long pts = 0;
4346 
4347  if (pkt->dts != AV_NOPTS_VALUE)
4348  pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
4349 
4350  avcodeclock->lock();
4352  bool isForcedTrack = m_selectedTrack[kTrackTypeSubtitle].m_forced;
4353  avcodeclock->unlock();
4354 
4355  int gotSubtitles = 0;
4356  AVSubtitle subtitle;
4357  memset(&subtitle, 0, sizeof(AVSubtitle));
4358 
4359  if (ringBuffer->IsDVD())
4360  {
4361  if (ringBuffer->DVD()->NumMenuButtons() > 0)
4362  {
4363  ringBuffer->DVD()->GetMenuSPUPkt(pkt->data, pkt->size,
4364  curstream->id, pts);
4365  }
4366  else
4367  {
4368  if (pkt->stream_index == subIdx)
4369  {
4370  QMutexLocker locker(avcodeclock);
4371  ringBuffer->DVD()->DecodeSubtitles(&subtitle, &gotSubtitles,
4372  pkt->data, pkt->size, pts);
4373  }
4374  }
4375  }
4376  else if (m_decodeAllSubtitles || pkt->stream_index == subIdx)
4377  {
4378  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
4379  QMutexLocker locker(avcodeclock);
4380  avcodec_decode_subtitle2(ctx, &subtitle, &gotSubtitles,
4381  pkt);
4382 
4383  subtitle.start_display_time += pts;
4384  subtitle.end_display_time += pts;
4385  }
4386 
4387  if (gotSubtitles)
4388  {
4389  if (isForcedTrack)
4390  subtitle.forced = static_cast<int>(true);
4391  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4392  QString("subtl timecode %1 %2 %3 %4")
4393  .arg(pkt->pts).arg(pkt->dts)
4394  .arg(subtitle.start_display_time)
4395  .arg(subtitle.end_display_time));
4396 
4397  bool forcedon = m_parent->GetSubReader(pkt->stream_index)->AddAVSubtitle(
4398  subtitle, curstream->codecpar->codec_id == AV_CODEC_ID_XSUB,
4400  m_parent->EnableForcedSubtitles(forcedon || isForcedTrack);
4401  }
4402 
4403  return true;
4404 }
4405 
4407 {
4408  if (!(m_decodeAllSubtitles ||
4409  m_selectedTrack[kTrackTypeRawText].m_av_stream_index == pkt->stream_index))
4410  {
4411  return false;
4412  }
4413 
4414  if (!m_parent->GetSubReader(pkt->stream_index+0x2000))
4415  return false;
4416 
4417  QTextCodec *codec = QTextCodec::codecForName("utf-8");
4418  QTextDecoder *dec = codec->makeDecoder();
4419  QString text = dec->toUnicode((const char*)pkt->data, pkt->size - 1);
4420  QStringList list = text.split('\n', QString::SkipEmptyParts);
4421  delete dec;
4422 
4423  m_parent->GetSubReader(pkt->stream_index+0x2000)->
4424  AddRawTextSubtitle(list, pkt->duration);
4425 
4426  return true;
4427 }
4428 
4429 bool AvFormatDecoder::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
4430  DecodeType decodetype)
4431 {
4432  enum AVCodecID codec_id = curstream->codecpar->codec_id;
4433 
4434  switch (codec_id)
4435  {
4436  case AV_CODEC_ID_MPEG2VBI:
4437  ProcessVBIDataPacket(curstream, pkt);
4438  break;
4439  case AV_CODEC_ID_DVB_VBI:
4440  ProcessDVBDataPacket(curstream, pkt);
4441  break;
4442  case AV_CODEC_ID_DSMCC_B:
4443  {
4444  ProcessDSMCCPacket(curstream, pkt);
4446  // Have to return regularly to ensure that the OSD is updated.
4447  // This applies both to MHEG and also channel browsing.
4448 #ifdef USING_MHEG
4449  if (!(decodetype & kDecodeVideo))
4451 #else
4452  Q_UNUSED(decodetype);
4453 #endif // USING_MHEG:
4454  break;
4455  }
4456  default:
4457  break;
4458  }
4459  return true;
4460 }
4461 
4463 {
4464  int ret = DecoderBase::SetTrack(type, trackNo);
4465 
4466  if (kTrackTypeAudio == type)
4467  {
4468  QString msg = SetupAudioStream() ? "" : "not ";
4469  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio stream type "+msg+"changed.");
4470  }
4471 
4472  return ret;
4473 }
4474 
4476 {
4477  if (!m_ic || trackNo >= m_tracks[type].size())
4478  return "";
4479 
4480  bool forced = m_tracks[type][trackNo].m_forced;
4481  int lang_key = m_tracks[type][trackNo].m_language;
4482  QString forcedString = forced ? QObject::tr(" (forced)") : "";
4483  if (kTrackTypeAudio == type)
4484  {
4485  QString msg = iso639_key_toName(lang_key);
4486 
4487  switch (m_tracks[type][trackNo].m_audio_type)
4488  {
4489  case kAudioTypeNormal :
4490  {
4491  int av_index = m_tracks[kTrackTypeAudio][trackNo].m_av_stream_index;
4492  AVStream *s = m_ic->streams[av_index];
4493 
4494  if (s)
4495  {
4496  AVCodecParameters *par = s->codecpar;
4497  AVCodecContext *ctx = gCodecMap->getCodecContext(s);
4498  if (par->codec_id == AV_CODEC_ID_MP3)
4499  msg += QString(" MP3");
4500  else if (ctx && ctx->codec)
4501  msg += QString(" %1").arg(ctx->codec->name).toUpper();
4502 
4503  int channels = 0;
4504  if (ringBuffer->IsDVD() || par->channels)
4505  channels = m_tracks[kTrackTypeAudio][trackNo].m_orig_num_channels;
4506 
4507  if (channels == 0)
4508  msg += QString(" ?ch");
4509  else if((channels > 4) && !(channels & 1))
4510  msg += QString(" %1.1ch").arg(channels - 1);
4511  else
4512  msg += QString(" %1ch").arg(channels);
4513  }
4514 
4515  break;
4516  }
4518  case kAudioTypeCommentary :
4520  case kAudioTypeCleanEffects :
4521  case kAudioTypeSpokenSubs :
4522  default :
4523  msg += QString(" (%1)")
4524  .arg(toString(m_tracks[type][trackNo].m_audio_type));
4525  break;
4526  }
4527 
4528  return QString("%1: %2").arg(trackNo + 1).arg(msg);
4529  }
4530  if (kTrackTypeSubtitle == type)
4531  {
4532  return QObject::tr("Subtitle") + QString(" %1: %2%3")
4533  .arg(trackNo + 1).arg(iso639_key_toName(lang_key))
4534  .arg(forcedString);
4535  }
4536  if (forced && kTrackTypeRawText == type)
4537  {
4538  return DecoderBase::GetTrackDesc(type, trackNo) + forcedString;
4539  }
4540  return DecoderBase::GetTrackDesc(type, trackNo);
4541 }
4542 
4544 {
4545  return m_ttd->GetDecoderType();
4546 }
4547 
4548 QString AvFormatDecoder::GetXDS(const QString &key) const
4549 {
4550  return m_ccd608->GetXDS(key);
4551 }
4552 
4553 QByteArray AvFormatDecoder::GetSubHeader(uint trackNo) const
4554 {
4555  if (trackNo >= m_tracks[kTrackTypeSubtitle].size())
4556  return QByteArray();
4557 
4558  int index = m_tracks[kTrackTypeSubtitle][trackNo].m_av_stream_index;
4559  AVCodecContext *ctx = gCodecMap->getCodecContext(m_ic->streams[index]);
4560  if (!ctx)
4561  return QByteArray();
4562 
4563  return QByteArray((char *)ctx->subtitle_header,
4564  ctx->subtitle_header_size);
4565 }
4566 
4567 void AvFormatDecoder::GetAttachmentData(uint trackNo, QByteArray &filename,
4568  QByteArray &data)
4569 {
4570  if (trackNo >= m_tracks[kTrackTypeAttachment].size())
4571  return;
4572 
4573  int index = m_tracks[kTrackTypeAttachment][trackNo].m_av_stream_index;
4574  AVDictionaryEntry *tag = av_dict_get(m_ic->streams[index]->metadata,
4575  "filename", nullptr, 0);
4576  if (tag)
4577  filename = QByteArray(tag->value);
4578  AVCodecParameters *par = m_ic->streams[index]->codecpar;
4579  data = QByteArray((char *)par->extradata, par->extradata_size);
4580 }
4581 
4583 {
4584  for (size_t i = 0; i < m_tracks[kTrackTypeAudio].size(); i++)
4585  {
4586  AVStream *s = m_ic->streams[m_tracks[kTrackTypeAudio][i].m_av_stream_index];
4587  if (s)
4588  {
4589  if ((s->component_tag == tag) ||
4590  ((tag <= 0) && s->component_tag <= 0))
4591  {
4592  return SetTrack(kTrackTypeAudio, i) != -1;
4593  }
4594  }
4595  }
4596  return false;
4597 }
4598 
4600 {
4601  for (uint i = 0; i < m_ic->nb_streams; i++)
4602  {
4603  AVStream *s = m_ic->streams[i];
4604  if (s)
4605  {
4606  if (s->component_tag == tag)
4607  {
4608  StreamInfo si(i, 0, 0, 0, 0);
4610  return true;
4611  }
4612  }
4613  }
4614  return false;
4615 }
4616 
4617 // documented in decoderbase.cpp
4619 {
4620  if (kTrackTypeAudio == type)
4621  return AutoSelectAudioTrack();
4622 
4624  return -1;
4625 
4627 }
4628 
4629 static vector<int> filter_lang(const sinfo_vec_t &tracks, int lang_key,
4630  const vector<int> &ftype)
4631 {
4632  vector<int> ret;
4633 
4634  vector<int>::const_iterator it = ftype.begin();
4635  for (; it != ftype.end(); ++it)
4636  {
4637  if ((lang_key < 0) || tracks[*it].m_language == lang_key)
4638  ret.push_back(*it);
4639  }
4640 
4641  return ret;
4642 }
4643 
4644 static vector<int> filter_type(const sinfo_vec_t &tracks, AudioTrackType type)
4645 {
4646  vector<int> ret;
4647 
4648  for (size_t i = 0; i < tracks.size(); i++)
4649  {
4650  if (tracks[i].m_audio_type == type)
4651  ret.push_back(i);
4652  }
4653 
4654  return ret;
4655 }
4656 
4657 int AvFormatDecoder::filter_max_ch(const AVFormatContext *ic,
4658  const sinfo_vec_t &tracks,
4659  const vector<int> &fs,
4660  enum AVCodecID codecId,
4661  int profile)
4662 {
4663  int selectedTrack = -1, max_seen = -1;
4664 
4665  vector<int>::const_iterator it = fs.begin();
4666  for (; it != fs.end(); ++it)
4667  {
4668  const int stream_index = tracks[*it].m_av_stream_index;
4669  AVCodecParameters *par = ic->streams[stream_index]->codecpar;
4670  if ((codecId == AV_CODEC_ID_NONE || codecId == par->codec_id) &&
4671  (max_seen < par->channels))
4672  {
4673  if (codecId == AV_CODEC_ID_DTS && profile > 0)
4674  {
4675  // we cannot decode dts-hd, so only select it if passthrough
4676  if (!DoPassThrough(par, true) || par->profile != profile)
4677  continue;
4678  }
4679  selectedTrack = *it;
4680  max_seen = par->channels;
4681  }
4682  }
4683 
4684  return selectedTrack;
4685 }
4686 
4734 {
4735  const sinfo_vec_t &atracks = m_tracks[kTrackTypeAudio];
4738  int &ctrack = m_currentTrack[kTrackTypeAudio];
4739 
4740  uint numStreams = atracks.size();
4741  if ((ctrack >= 0) && (ctrack < (int)numStreams))
4742  return ctrack; // audio already selected
4743 
4744 #if 0
4745  // enable this to print streams
4746  for (uint i = 0; i < atracks.size(); i++)
4747  {
4748  int idx = atracks[i].av_stream_index;
4749  AVCodecContext *codec_ctx = m_ic->streams[idx]->codec;
4750  AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
4751  codec_ctx->sample_rate, codec_ctx->channels,
4752  DoPassThrough(codec_ctx, true));
4753  LOG(VB_AUDIO, LOG_DEBUG, LOC + " * " + item.toString());
4754  }
4755 #endif
4756 
4757  int selTrack = (1 == numStreams) ? 0 : -1;
4758  int wlang = wtrack.m_language;
4759 
4760 
4761  if ((selTrack < 0) && (wtrack.m_av_substream_index >= 0))
4762  {
4763  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio sub-stream");
4764  // Dual stream without language information: choose
4765  // the previous substream that was kept in wtrack,
4766  // ignoring the stream index (which might have changed).
4767  int substream_index = wtrack.m_av_substream_index;
4768 
4769  for (uint i = 0; i < numStreams; i++)
4770  {
4771  if (atracks[i].m_av_substream_index == substream_index)
4772  {
4773  selTrack = i;
4774  break;
4775  }
4776  }
4777  }
4778 
4779  if ((selTrack < 0) && wlang >= -1 && numStreams)
4780  {
4781  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio track");
4782  // Try to reselect user selected audio stream.
4783  // This should find the stream after a commercial
4784  // break and in some cases after a channel change.
4785  uint windx = wtrack.m_language_index;
4786  for (uint i = 0; i < numStreams; i++)
4787  {
4788  if (wlang == atracks[i].m_language)
4789  {
4790  selTrack = i;
4791 
4792  if (windx == atracks[i].m_language_index)
4793  break;
4794  }
4795  }
4796  }
4797 
4798  if (selTrack < 0 && numStreams)
4799  {
4800  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select audio track (w/lang)");
4801 
4802  // Filter out commentary and audio description tracks
4803  vector<int> ftype = filter_type(atracks, kAudioTypeNormal);
4804 
4805  if (ftype.empty())
4806  {
4807  LOG(VB_AUDIO, LOG_WARNING, "No audio tracks matched the type filter, "
4808  "so trying all tracks.");
4809  for (int i = 0; i < static_cast<int>(atracks.size()); i++)
4810  ftype.push_back(i);
4811  }
4812 
4813  // try to get the language track matching the frontend language.
4814  QString language_key_convert = iso639_str2_to_str3(gCoreContext->GetLanguage());
4815  uint language_key = iso639_str3_to_key(language_key_convert);
4816  uint canonical_key = iso639_key_to_canonical_key(language_key);
4817 
4818  vector<int> flang = filter_lang(atracks, canonical_key, ftype);
4819 
4820  if (m_audio->CanDTSHD())
4821  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4822  FF_PROFILE_DTS_HD_MA);
4823  if (selTrack < 0)
4824  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4825 
4826  if (selTrack < 0 && m_audio->CanDTSHD())
4827  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4828  FF_PROFILE_DTS_HD_HRA);
4829  if (selTrack < 0)
4830  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4831 
4832  if (selTrack < 0)
4833  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4834 
4835  if (selTrack < 0)
4836  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4837 
4838  if (selTrack < 0)
4839  selTrack = filter_max_ch(m_ic, atracks, flang);
4840 
4841  // try to get best track for most preferred language
4842  // Set by the "Guide Data" language prefs in Appearance.
4843  if (selTrack < 0)
4844  {
4845  vector<int>::const_iterator it = m_languagePreference.begin();
4846  for (; it != m_languagePreference.end() && selTrack < 0; ++it)
4847  {
4848  flang = filter_lang(atracks, *it, ftype);
4849 
4850  if (m_audio->CanDTSHD())
4851  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4852  FF_PROFILE_DTS_HD_MA);
4853  if (selTrack < 0)
4854  selTrack = filter_max_ch(m_ic, atracks, flang,
4855  AV_CODEC_ID_TRUEHD);
4856 
4857  if (selTrack < 0 && m_audio->CanDTSHD())
4858  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4859  FF_PROFILE_DTS_HD_HRA);
4860 
4861  if (selTrack < 0)
4862  selTrack = filter_max_ch(m_ic, atracks, flang,
4863  AV_CODEC_ID_EAC3);
4864 
4865  if (selTrack < 0)
4866  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4867 
4868  if (selTrack < 0)
4869  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4870 
4871  if (selTrack < 0)
4872  selTrack = filter_max_ch(m_ic, atracks, flang);
4873  }
4874  }
4875 
4876  // could not select track based on user preferences (language)
4877  // try to select the default track
4878  if (selTrack < 0 && numStreams)
4879  {
4880  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select default track");
4881  for (size_t i = 0; i < atracks.size(); i++) {
4882  int idx = atracks[i].m_av_stream_index;
4883  if (m_ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
4884  {
4885  selTrack = i;
4886  break;
4887  }
4888  }
4889  }
4890 
4891  // try to get best track for any language
4892  if (selTrack < 0)
4893  {
4894  LOG(VB_AUDIO, LOG_INFO, LOC +
4895  "Trying to select audio track (wo/lang)");
4896  flang = filter_lang(atracks, -1, ftype);
4897 
4898  if (m_audio->CanDTSHD())
4899  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4900  FF_PROFILE_DTS_HD_MA);
4901  if (selTrack < 0)
4902  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4903 
4904  if (selTrack < 0 && m_audio->CanDTSHD())
4905  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS,
4906  FF_PROFILE_DTS_HD_HRA);
4907 
4908  if (selTrack < 0)
4909  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_EAC3);
4910 
4911  if (selTrack < 0)
4912  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_DTS);
4913 
4914  if (selTrack < 0)
4915  selTrack = filter_max_ch(m_ic, atracks, flang, AV_CODEC_ID_AC3);
4916 
4917  if (selTrack < 0)
4918  selTrack = filter_max_ch(m_ic, atracks, flang);
4919  }
4920  }
4921 
4922  if (selTrack < 0)
4923  {
4924  strack.m_av_stream_index = -1;
4925  if (ctrack != selTrack)
4926  {
4927  LOG(VB_AUDIO, LOG_INFO, LOC + "No suitable audio track exists.");
4928  ctrack = selTrack;
4929  }
4930  }
4931  else
4932  {
4933  ctrack = selTrack;
4934  strack = atracks[selTrack];
4935 
4936  if (wtrack.m_av_stream_index < 0)
4937  wtrack = strack;
4938 
4939  LOG(VB_AUDIO, LOG_INFO, LOC +
4940  QString("Selected track %1 (A/V Stream #%2)")
4941  .arg(GetTrackDesc(kTrackTypeAudio, ctrack))
4942  .arg(strack.m_av_stream_index));
4943  }
4944 
4945  SetupAudioStream();
4946  return selTrack;
4947 }
4948 
4949 static void extract_mono_channel(uint channel, AudioInfo *audioInfo,
4950  char *buffer, int bufsize)
4951 {
4952  // Only stereo -> mono (left or right) is supported
4953  if (audioInfo->channels != 2)
4954  return;
4955 
4956  if (channel >= (uint)audioInfo->channels)
4957  return;
4958 
4959  const uint samplesize = audioInfo->sample_size;
4960  const uint samples = bufsize / samplesize;
4961  const uint halfsample = samplesize >> 1;
4962 
4963  const char *from = (channel == 1) ? buffer + halfsample : buffer;
4964  char *to = (channel == 0) ? buffer + halfsample : buffer;
4965 
4966  for (uint sample = 0; sample < samples;
4967  (sample++), (from += samplesize), (to += samplesize))
4968  {
4969  memmove(to, from, halfsample);
4970  }
4971 }
4972 
4973 bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
4974  DecodeType decodetype)
4975 {
4976  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
4977  int ret = 0;
4978  int data_size = 0;
4979  bool firstloop = true;
4980  int decoded_size = -1;
4981 
4982  avcodeclock->lock();
4985  avcodeclock->unlock();
4986 
4987  AVPacket tmp_pkt;
4988  av_init_packet(&tmp_pkt);
4989  tmp_pkt.data = pkt->data;
4990  tmp_pkt.size = pkt->size;
4991 
4992  while (tmp_pkt.size > 0)
4993  {
4994  bool reselectAudioTrack = false;
4995 
4997  if (!m_audio->HasAudioIn())
4998  {
4999  LOG(VB_AUDIO, LOG_INFO, LOC +
5000  "Audio is disabled - trying to restart it");
5001  reselectAudioTrack = true;
5002  }
5004 
5005  // detect switches between stereo and dual languages
5006  bool wasDual = audSubIdx != -1;
5007  bool isDual = ctx->avcodec_dual_language != 0;
5008  if ((wasDual && !isDual) || (!wasDual && isDual))
5009  {
5011  reselectAudioTrack = true;
5012  }
5013 
5014  // detect channels on streams that need
5015  // to be decoded before we can know this
5016  bool already_decoded = false;
5017  if (!ctx->channels)
5018  {
5019  QMutexLocker locker(avcodeclock);
5020 
5021  if (DoPassThrough(curstream->codecpar, false) || !DecoderWillDownmix(ctx))
5022  {
5023  // for passthru or codecs for which the decoder won't downmix
5024  // let the decoder set the number of channels. For other codecs
5025  // we downmix if necessary in audiooutputbase
5026  ctx->request_channel_layout = 0;
5027  }
5028  else // No passthru, the decoder will downmix
5029  {
5030  ctx->request_channel_layout =
5031  av_get_default_channel_layout(m_audio->GetMaxChannels());
5032  if (ctx->codec_id == AV_CODEC_ID_AC3)
5033  ctx->channels = m_audio->GetMaxChannels();
5034  }
5035 
5036  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
5037  decoded_size = data_size;
5038  already_decoded = true;
5039  reselectAudioTrack |= ctx->channels;
5040  }
5041 
5042  if (reselectAudioTrack)
5043  {
5044  QMutexLocker locker(avcodeclock);
5050  }
5051 
5052  if (!(decodetype & kDecodeAudio) || (pkt->stream_index != audIdx)
5053  || !m_audio->HasAudioOut())
5054  break;
5055 
5056  if (firstloop && pkt->pts != AV_NOPTS_VALUE)
5057  m_lastapts = (long long)(av_q2d(curstream->time_base) * pkt->pts * 1000);
5058 
5059  if (!m_use_frame_timing)
5060  {
5061  // This code under certain conditions causes jump backwards to lose
5062  // audio.
5063  if (m_skipaudio && m_selectedTrack[kTrackTypeVideo].m_av_stream_index > -1)
5064  {
5065  if ((m_lastapts < m_lastvpts - (10.0F / m_fps)) || m_lastvpts == 0)
5066  break;
5067  m_skipaudio = false;
5068  }
5069 
5070  // skip any audio frames preceding first video frame
5072  {
5073  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
5074  QString("discarding early audio timecode %1 %2 %3")
5075  .arg(pkt->pts).arg(pkt->dts).arg(m_lastapts));
5076  break;
5077  }
5078  }
5079  m_firstvptsinuse = false;
5080 
5081  avcodeclock->lock();
5082  data_size = 0;
5083 
5084  // Check if the number of channels or sampling rate have changed
5085  if (ctx->sample_rate != m_audioOut.sample_rate ||
5086  ctx->channels != m_audioOut.channels ||
5088  ctx->bits_per_raw_sample) != m_audioOut.format)
5089  {
5090  LOG(VB_GENERAL, LOG_INFO, LOC + "Audio stream changed");
5091  if (ctx->channels != m_audioOut.channels)
5092  {
5093  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Number of audio channels changed from %1 to %2")
5094  .arg(m_audioOut.channels).arg(ctx->channels));
5095  }
5098  audIdx = -1;
5100  }
5101 
5102  if (m_audioOut.do_passthru)
5103  {
5104  if (!already_decoded)
5105  {
5107  {
5108  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
5109  decoded_size = data_size;
5110  }
5111  else
5112  {
5113  decoded_size = -1;
5114  }
5115  }
5116  memcpy(m_audioSamples, tmp_pkt.data, tmp_pkt.size);
5117  data_size = tmp_pkt.size;
5118  // We have processed all the data, there can't be any left
5119  tmp_pkt.size = 0;
5120  }
5121  else
5122  {
5123  if (!already_decoded)
5124  {
5125  if (DecoderWillDownmix(ctx))
5126  {
5127  ctx->request_channel_layout =
5128  av_get_default_channel_layout(m_audio->GetMaxChannels());
5129  }
5130  else
5131  {
5132  ctx->request_channel_layout = 0;
5133  }
5134 
5135  ret = m_audio->DecodeAudio(ctx, m_audioSamples, data_size, &tmp_pkt);
5136  decoded_size = data_size;
5137  }
5138  }
5139  avcodeclock->unlock();
5140 
5141  if (ret < 0)
5142  {
5143  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown audio decoding error");
5144  return false;
5145  }
5146 
5147  if (data_size <= 0)
5148  {
5149  tmp_pkt.data += ret;
5150  tmp_pkt.size -= ret;
5151  continue;
5152  }
5153 
5154  long long temppts = m_lastapts;
5155 
5156  if (audSubIdx != -1 && !m_audioOut.do_passthru)
5157  extract_mono_channel(audSubIdx, &m_audioOut,
5158  (char *)m_audioSamples, data_size);
5159 
5160  int samplesize = AudioOutputSettings::SampleSize(m_audio->GetFormat());
5161  int frames = (ctx->channels <= 0 || decoded_size < 0 || !samplesize) ? -1 :
5162  decoded_size / (ctx->channels * samplesize);
5163  m_audio->AddAudioData((char *)m_audioSamples, data_size, temppts, frames);
5165  {
5167  }
5168  else
5169  {
5170  m_lastapts += (long long)
5171  ((double)(frames * 1000) / ctx->sample_rate);
5172  }
5173 
5174  LOG(VB_TIMESTAMP, LOG_INFO, LOC + QString("audio timecode %1 %2 %3 %4")
5175  .arg(pkt->pts).arg(pkt->dts).arg(temppts).arg(m_lastapts));
5176 
5179 
5180  tmp_pkt.data += ret;
5181  tmp_pkt.size -= ret;
5182  firstloop = false;
5183  }
5184 
5185  return true;
5186 }
5187 
5188 // documented in decoderbase.h
5190 {
5191  AVPacket *pkt = nullptr;
5192  bool have_err = false;
5193 
5194  const DecodeType origDecodetype = decodetype;
5195 
5196  m_gotVideoFrame = false;
5197 
5198  m_frame_decoded = 0;
5199  m_decoded_video_frame = nullptr;
5200 
5201  m_allowedquit = false;
5202  bool storevideoframes = false;
5203 
5204  avcodeclock->lock();
5205  AutoSelectTracks();
5206  avcodeclock->unlock();
5207 
5208  m_skipaudio = (m_lastvpts == 0);
5209 
5210  if( !m_processFrames )
5211  {
5212  return false;
5213  }
5214 
5216  m_needDummyVideoFrames = false;
5217 
5218  if (!m_hasVideo && (decodetype & kDecodeVideo))
5219  {
5220  // NB This could be an issue if the video stream is not
5221  // detected initially as the video buffers will be filled.
5222  m_needDummyVideoFrames = true;
5223  decodetype = (DecodeType)((int)decodetype & ~kDecodeVideo);
5224  m_skipaudio = false;
5225  }
5226 
5228 
5231  {
5232  int got_picture = 0;
5233  AVStream *stream = m_ic->streams[m_selectedTrack[kTrackTypeVideo]
5235  MythAVFrame mpa_pic;
5236  if (!mpa_pic)
5237  {
5238  return false;
5239  }
5240 
5241  m_private_dec->GetFrame(stream, mpa_pic, &got_picture, nullptr);
5242  if (got_picture)
5243  ProcessVideoFrame(stream, mpa_pic);
5244  }
5245 
5246  while (!m_allowedquit)
5247  {
5248  if (decodetype & kDecodeAudio)
5249  {
5250  if (((m_currentTrack[kTrackTypeAudio] < 0) ||
5251  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)))
5252  {
5253  // disable audio request if there are no audio streams anymore
5254  // and we have video, otherwise allow decoding to stop
5255  if (m_hasVideo)
5256  decodetype = (DecodeType)((int)decodetype & ~kDecodeAudio);
5257  else
5258  m_allowedquit = true;
5259  }
5260  }
5261  else if ((origDecodetype & kDecodeAudio) &&
5262  (m_currentTrack[kTrackTypeAudio] >= 0) &&
5263  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index >= 0))
5264  {
5265  // Turn on audio decoding again if it was on originally
5266  // and an audio stream has now appeared. This can happen
5267  // in still DVD menus with audio
5268  decodetype = (DecodeType)((int)decodetype | kDecodeAudio);
5269  }
5270 
5272 
5273  if (m_gotVideoFrame)
5274  {
5275  if (decodetype == kDecodeNothing)
5276  {
5277  // no need to buffer audio or video if we
5278  // only care about building a keyframe map.
5279  // NB but allow for data only (MHEG) streams
5280  m_allowedquit = true;
5281  }
5282  else if ((decodetype & kDecodeAV) == kDecodeAV &&
5283  (m_storedPackets.count() < max_video_queue_size) &&
5284  // buffer audio to prevent audio buffer
5285  // underruns in case you are setting negative values
5286  // in Adjust Audio Sync.
5289  {
5290  storevideoframes = true;
5291  }
5292  else if (decodetype & kDecodeVideo)
5293  {
5294  if (m_storedPackets.count() >= max_video_queue_size)
5295  LOG(VB_GENERAL, LOG_WARNING, LOC +
5296  QString("Audio %1 ms behind video but already %2 "
5297  "video frames queued. AV-Sync might be broken.")
5298  .arg(m_lastvpts-m_lastapts).arg(m_storedPackets.count()));
5299  m_allowedquit = true;
5300  continue;
5301  }
5302  }
5303 
5304  if (!storevideoframes && m_storedPackets.count() > 0)
5305  {
5306  if (pkt)
5307  {
5308  av_packet_unref(pkt);
5309  delete pkt;
5310  }
5311  pkt = m_storedPackets.takeFirst();
5312  }
5313  else
5314  {
5315  if (!pkt)
5316  {
5317  pkt = new AVPacket;
5318  memset(pkt, 0, sizeof(AVPacket));
5319  av_init_packet(pkt);
5320  }
5321 
5322  int retval = 0;
5323  if (!m_ic || ((retval = ReadPacket(m_ic, pkt, storevideoframes)) < 0))
5324  {
5325  if (retval == -EAGAIN)
5326  continue;
5327 
5328  SetEof(true);
5329  delete pkt;
5330  char errbuf[256];
5331  QString errmsg;
5332  if (av_strerror(retval, errbuf, sizeof errbuf) == 0)
5333  errmsg = QString(errbuf);
5334  else
5335  errmsg = "UNKNOWN";
5336 
5337  LOG(VB_GENERAL, LOG_ERR, QString("decoding error %1 (%2)")
5338  .arg(errmsg).arg(retval));
5339  return false;
5340  }
5341 
5342  if (m_waitingForChange && pkt->pos >= m_readAdjust)
5343  FileChanged();
5344 
5345  if (pkt->pos > m_readAdjust)
5346  pkt->pos -= m_readAdjust;
5347  }
5348 
5349  if (!m_ic)
5350  {
5351  LOG(VB_GENERAL, LOG_ERR, LOC + "No context");
5352  av_packet_unref(pkt);
5353  continue;
5354  }
5355 
5356  if (pkt->stream_index >= (int)m_ic->nb_streams)
5357  {
5358  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream");
5359  av_packet_unref(pkt);
5360  continue;
5361  }
5362 
5363  AVStream *curstream = m_ic->streams[pkt->stream_index];
5364 
5365  if (!curstream)
5366  {
5367  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream (NULL)");
5368  av_packet_unref(pkt);
5369  continue;
5370  }
5371 
5372  enum AVMediaType codec_type = curstream->codecpar->codec_type;
5373 
5374  if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
5375  {
5376  // av_dup_packet(pkt);
5377  m_storedPackets.append(pkt);
5378  pkt = nullptr;
5379  continue;
5380  }
5381 
5382  if (codec_type == AVMEDIA_TYPE_VIDEO &&
5383  pkt->stream_index == m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
5384  {
5385  if (!PreProcessVideoPacket(curstream, pkt))
5386  continue;
5387 
5388  // If the resolution changed in XXXPreProcessPkt, we may
5389  // have a fatal error, so check for this before continuing.
5390  if (m_parent->IsErrored())
5391  {
5392  av_packet_unref(pkt);
5393  delete pkt;
5394  return false;
5395  }
5396  }
5397 
5398  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
5399  curstream->codecpar->codec_id == AV_CODEC_ID_TEXT)
5400  {
5401  ProcessRawTextPacket(pkt);
5402  av_packet_unref(pkt);
5403  continue;
5404  }
5405 
5406  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
5407  curstream->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT)
5408  {
5409  ProcessDVBDataPacket(curstream, pkt);
5410  av_packet_unref(pkt);
5411  continue;
5412  }
5413 
5414  if (codec_type == AVMEDIA_TYPE_DATA)
5415  {
5416  ProcessDataPacket(curstream, pkt, decodetype);
5417  av_packet_unref(pkt);
5418  continue;
5419  }
5420 
5421  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
5422  if (!ctx)
5423  {
5424  if (codec_type != AVMEDIA_TYPE_VIDEO)
5425  {
5426  LOG(VB_PLAYBACK, LOG_ERR, LOC +
5427  QString("No codec for stream index %1, type(%2) id(%3:%4)")
5428  .arg(pkt->stream_index)
5429  .arg(ff_codec_type_string(codec_type))
5430  .arg(ff_codec_id_string(curstream->codecpar->codec_id))
5431  .arg(curstream->codecpar->codec_id));
5432  // Process Stream Change in case we have no audio
5433  if (codec_type == AVMEDIA_TYPE_AUDIO && !m_audio->HasAudioIn())
5434  m_streams_changed = true;
5435  }
5436  av_packet_unref(pkt);
5437  continue;
5438  }
5439 
5440  have_err = false;
5441 
5442  switch (codec_type)
5443  {
5444  case AVMEDIA_TYPE_AUDIO:
5445  {
5446  if (!ProcessAudioPacket(curstream, pkt, decodetype))
5447  have_err = true;
5448  else
5450  break;
5451  }
5452 
5453  case AVMEDIA_TYPE_VIDEO:
5454  {
5455  if (pkt->stream_index != m_selectedTrack[kTrackTypeVideo].m_av_stream_index)
5456  {
5457  break;
5458  }
5459 
5460  if (pkt->pts != AV_NOPTS_VALUE)
5461  {
5462  m_lastccptsu = (long long)
5463  (av_q2d(curstream->time_base)*pkt->pts*1000000);
5464  }
5465 
5466  if (!(decodetype & kDecodeVideo))
5467  {
5468  m_framesPlayed++;
5469  m_gotVideoFrame = true;
5470  break;
5471  }
5472 
5473  if (!ProcessVideoPacket(curstream, pkt))
5474  have_err = true;
5475  break;
5476  }
5477 
5478  case AVMEDIA_TYPE_SUBTITLE:
5479  {
5480  if (!ProcessSubtitlePacket(curstream, pkt))
5481  have_err = true;
5482  break;
5483  }
5484 
5485  default:
5486  {
5487  LOG(VB_GENERAL, LOG_ERR, LOC +
5488  QString("Decoding - id(%1) type(%2)")
5489  .arg(ff_codec_id_string(ctx->codec_id))
5490  .arg(ff_codec_type_string(ctx->codec_type)));
5491  have_err = true;
5492  break;
5493  }
5494  }
5495 
5496  if (!have_err)
5497  m_frame_decoded = 1;
5498 
5499  av_packet_unref(pkt);
5500  }
5501 
5502  delete pkt;
5503  return true;
5504 }
5505 
5507  if (m_streams_changed)
5508  {
5509  SeekReset(0, 0, true, true);
5510  QMutexLocker locker(avcodeclock);
5511  ScanStreams(false);
5512  m_streams_changed=false;
5513  }
5514 }
5515 
5516 int AvFormatDecoder::ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &/*storePacket*/)
5517 {
5518  QMutexLocker locker(avcodeclock);
5519 
5520  return av_read_frame(ctx, pkt);
5521 }
5522 
5523 bool AvFormatDecoder::HasVideo(const AVFormatContext *ic)
5524 {
5525  if (ic && ic->cur_pmt_sect)
5526  {
5527  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
5528 
5529  for (uint i = 0; i < pmt.StreamCount(); i++)
5530  {
5531  // MythTV remaps OpenCable Video to normal video during recording
5532  // so "dvb" is the safest choice for system info type, since this
5533  // will ignore other uses of the same stream id in DVB countries.
5534  if (pmt.IsVideo(i, "dvb"))
5535  return true;
5536 
5537  // MHEG may explicitly select a private stream as video
5538  if ((i == (uint)m_selectedTrack[kTrackTypeVideo].m_av_stream_index) &&
5539  (pmt.StreamType(i) == StreamID::PrivData))
5540  {
5541  return true;
5542  }
5543  }
5544  }
5545 
5546  return GetTrackCount(kTrackTypeVideo) != 0U;
5547 }
5548 
5550 {
5551  while (m_needDummyVideoFrames && m_parent &&
5553  {
5555  if (!frame)
5556  return false;
5557 
5560  m_parent->DeLimboFrame(frame);
5561 
5562  frame->interlaced_frame = 0; // not interlaced
5563  frame->top_field_first = 1; // top field first
5564  frame->repeat_pict = 0; // not a repeated picture
5565  frame->frameNumber = m_framesPlayed;
5566  frame->dummy = 1;
5567 
5568  m_decoded_video_frame = frame;
5569  m_framesPlayed++;
5570  m_gotVideoFrame = true;
5571  }
5572  return true;
5573 }
5574 
5576 {
5577  if (m_private_dec)
5578  return m_private_dec->GetName();
5580 }
5581 
5583 {
5585  if (stream < 0 || !m_ic)
5586  return QString();
5587  return ff_codec_id_string(m_ic->streams[stream]->codecpar->codec_id);
5588 }
5589 
5591 {
5592  return nullptr; // TODO is this still needed
5593 }
5594 
5596 {
5597  if (m_selectedTrack[kTrackTypeAudio].m_av_stream_index < 0)
5598  {
5599  m_disable_passthru = disable;
5600  return;
5601  }
5602 
5603  if (disable != m_disable_passthru)
5604  {
5605  m_disable_passthru = disable;
5606  QString msg = (disable) ? "Disabling" : "Allowing";
5607  LOG(VB_AUDIO, LOG_INFO, LOC + msg + " pass through");
5608 
5609  // Force pass through state to be reanalyzed
5611  }
5612 }
5613 
5615 {
5616  QMutexLocker locker(avcodeclock);
5617 
5618  SetupAudioStream();
5619 }
5620 
5621 inline bool AvFormatDecoder::DecoderWillDownmix(const AVCodecContext *ctx)
5622 {
5623  // Until ffmpeg properly implements dialnorm
5624  // use Myth internal downmixer if machines has FPU/SSE
5626  return false;
5627  if (!m_audio->CanDownmix())
5628  return true;
5629  // use ffmpeg only for dolby codecs if we have to
5630  switch (ctx->codec_id)
5631  {
5632  case AV_CODEC_ID_AC3:
5633  case AV_CODEC_ID_TRUEHD:
5634  case AV_CODEC_ID_EAC3:
5635  return true;
5636  default:
5637  return false;
5638  }
5639 }
5640 
5641 bool AvFormatDecoder::DoPassThrough(const AVCodecParameters *par, bool withProfile)
5642 {
5643  bool passthru;
5644 
5645  // if withProfile == false, we will accept any DTS stream regardless
5646  // of its profile. We do so, so we can bitstream DTS-HD as DTS core
5647  if (!withProfile && par->codec_id == AV_CODEC_ID_DTS && !m_audio->CanDTSHD())
5648  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5649  par->codec_id, FF_PROFILE_DTS);
5650  else
5651  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5652  par->codec_id, par->profile);
5653 
5654  passthru &= !m_disable_passthru;
5655 
5656  return passthru;
5657 }
5658 
5667 {
5668  AudioInfo info; // no_audio
5669  AVStream *curstream = nullptr;
5670  AVCodecContext *ctx = nullptr;
5671  AudioInfo old_in = m_audioIn;
5672  int requested_channels;
5673 
5674  if ((m_currentTrack[kTrackTypeAudio] >= 0) && m_ic &&
5675  (m_selectedTrack[kTrackTypeAudio].m_av_stream_index <=
5676  (int) m_ic->nb_streams) &&
5677  (curstream = m_ic->streams[m_selectedTrack[kTrackTypeAudio]
5678  .m_av_stream_index]) &&
5679  (ctx = gCodecMap->getCodecContext(curstream)))
5680  {
5681  AudioFormat fmt =
5683  ctx->bits_per_raw_sample);
5684 
5685  if (av_sample_fmt_is_planar(ctx->sample_fmt))
5686  {
5687  LOG(VB_AUDIO, LOG_INFO, LOC + QString("Audio data is planar"));
5688  }
5689 
5690  if (fmt == FORMAT_NONE)
5691  {
5692  int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
5693  if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
5694  ctx->bits_per_raw_sample)
5695  bps = ctx->bits_per_raw_sample;
5696  LOG(VB_GENERAL, LOG_ERR, LOC +
5697  QString("Unsupported sample format with %1 bits").arg(bps));
5698  return false;
5699  }
5700 
5701  bool using_passthru = DoPassThrough(curstream->codecpar, false);
5702 
5703  requested_channels = ctx->channels;
5704  ctx->request_channel_layout =
5705  av_get_default_channel_layout(requested_channels);
5706 
5707  if (!using_passthru &&
5708  ctx->channels > (int)m_audio->GetMaxChannels() &&
5709  DecoderWillDownmix(ctx))
5710  {
5711  requested_channels = m_audio->GetMaxChannels();
5712  ctx->request_channel_layout =
5713  av_get_default_channel_layout(requested_channels);
5714  }
5715  else
5716  {
5717  ctx->request_channel_layout = 0;
5718  }
5719 
5720  info = AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
5721  requested_channels, using_passthru, ctx->channels,
5722  ctx->codec_id == AV_CODEC_ID_DTS ? ctx->profile : 0);
5723  }
5724 
5725  if (!ctx)
5726  {
5728  LOG(VB_PLAYBACK, LOG_INFO, LOC +
5729  "No codec context. Returning false");
5730  return false;
5731  }
5732 
5733  if (info == m_audioIn)
5734  return false;
5735 
5736  LOG(VB_AUDIO, LOG_INFO, LOC + "Initializing audio parms from " +
5737  QString("audio track #%1").arg(m_currentTrack[kTrackTypeAudio]+1));
5738 
5740 
5741  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio format changed " +
5742  QString("\n\t\t\tfrom %1 to %2")
5743  .arg(old_in.toString()).arg(m_audioOut.toString()));
5744 
5745  m_audio->SetAudioParams(m_audioOut.format, ctx->channels,
5746  requested_channels,
5749  m_audio->ReinitAudio();
5750  AudioOutput *audioOutput = m_audio->GetAudioOutput();
5751  if (audioOutput)
5752  audioOutput->SetSourceBitrate(ctx->bit_rate);
5753 
5754  if (LCD *lcd = LCD::Get())
5755  {
5756  LCDAudioFormatSet audio_format;
5757 
5758  switch (ctx->codec_id)
5759  {
5760  case AV_CODEC_ID_MP2:
5761  audio_format = AUDIO_MPEG2;
5762  break;
5763  case AV_CODEC_ID_MP3:
5764  audio_format = AUDIO_MP3;
5765  break;
5766  case AV_CODEC_ID_AC3:
5767  audio_format = AUDIO_AC3;
5768  break;
5769  case AV_CODEC_ID_DTS:
5770  audio_format = AUDIO_DTS;
5771  break;
5772  case AV_CODEC_ID_VORBIS:
5773  audio_format = AUDIO_OGG;
5774  break;
5775  case AV_CODEC_ID_WMAV1:
5776  audio_format = AUDIO_WMA;
5777  break;
5778  case AV_CODEC_ID_WMAV2:
5779  audio_format = AUDIO_WMA2;
5780  break;
5781  default:
5782  audio_format = AUDIO_WAV;
5783  break;
5784  }
5785 
5786  lcd->setAudioFormatLEDs(audio_format, true);
5787 
5788  if (m_audioOut.do_passthru)
5789  lcd->setVariousLEDs(VARIOUS_SPDIF, true);
5790  else
5791  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
5792 
5793  switch (m_audioIn.channels)
5794  {
5795  case 0:
5796  /* nb: aac and mp3 seem to be coming up 0 here, may point to an
5797  * avformatdecoder audio channel handling bug, per janneg */
5798  case 1:
5799  case 2:
5800  /* all audio codecs have at *least* one channel, but
5801  * LR is the fewest LED we can light up */
5802  lcd->setSpeakerLEDs(SPEAKER_LR, true);
5803  break;
5804  case 3:
5805  case 4:
5806  case 5:
5807  case 6:
5808  lcd->setSpeakerLEDs(SPEAKER_51, true);
5809  break;
5810  default:
5811  lcd->setSpeakerLEDs(SPEAKER_71, true);
5812  break;
5813  }
5814 
5815  }
5816  return true;
5817 }
5818 
5820 {
5821  int64_t start_time, end_time;
5822  int64_t duration;
5823  AVStream *st = nullptr;
5824 
5825  start_time = INT64_MAX;
5826  end_time = INT64_MIN;
5827 
5828  for (uint i = 0; i < ic->nb_streams; i++)
5829  {
5830  AVStream *st1 = ic->streams[i];
5831  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
5832  {
5833  st = st1;
5834  break;
5835  }
5836  }
5837  if (!st)
5838  return;
5839 
5840  duration = INT64_MIN;
5841  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
5842  int64_t start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
5843  if (start_time1 < start_time)
5844  start_time = start_time1;
5845  if (st->duration != AV_NOPTS_VALUE) {
5846  int64_t end_time1 = start_time1 +
5847  av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5848  if (end_time1 > end_time)
5849  end_time = end_time1;
5850  }
5851  }
5852  if (st->duration != AV_NOPTS_VALUE) {
5853  int64_t duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5854  if (duration1 > duration)
5855  duration = duration1;
5856  }
5857  if (start_time != INT64_MAX) {
5858  ic->start_time = start_time;
5859  if (end_time != INT64_MIN) {
5860  if (end_time - start_time > duration)
5861  duration = end_time - start_time;
5862  }
5863  }
5864  if (duration != INT64_MIN) {
5865  int64_t filesize;
5866  ic->duration = duration;
5867  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
5868  /* compute the bitrate */
5869  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
5870  (double)ic->duration;
5871  }
5872  }
5873 }
5874 
5875 /* vim: set expandtab tabstop=4 shiftwidth=4: */
virtual bool DoRewind(long long desiredFrame, bool discardFrames=true)
uint m_bitrate
Definition: decoderbase.h:306
uint32_t m_start_code_state
bool setDeinterlacer(bool enable, QString name=QString())
void DeLimboFrame(VideoFrame *frame)
virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt, DecodeType decodetype)
int pitches[3]
Y, U, & V pitches.
Definition: mythframe.h:63
int GetFreeVideoFrames(void) const
Returns the number of frames available for decoding onto.
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)
#define CODEC_IS_VAAPI(codec, enc)
Definition: mythcodecid.h:177
void SetKeyframeDistance(int keyframedistance)
Definition: mythplayer.cpp:680
void SetInInit(bool state)
MythPlayer * GetPlayer()
Definition: decoderbase.h:154
int CanonicalLanguageKey(int i) const
static MythCodecID GetBestSupportedCodec(uint width, uint height, const QString &decoder, uint stream_type, bool no_acceleration, AVPixelFormat &pix_fmt)
ISO 639-1 and ISO 639-2 support functions.
MythCodecID m_video_codec_id
bool m_forced
Definition: decoderbase.h:103
bool ProcessAudioPacket(AVStream *stream, AVPacket *pkt, DecodeType decodetype)
void SetDisablePassThrough(bool disable) override
Disables AC3/DTS pass through.
QString GetTrackDesc(uint type, uint trackNo) const override
MythRenderVDPAU * getRender() const
int filter_max_ch(const AVFormatContext *ic, const sinfo_vec_t &tracks, const vector< int > &fs, enum AVCodecID codecId=AV_CODEC_ID_NONE, int profile=-1)
void GetAttachmentData(uint trackNo, QByteArray &filename, QByteArray &data) override
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
void SetFrameRate(double fps)
Definition: mythplayer.cpp:884
int64_t ptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.c:78
int64_t LengthLastData(void)
struct SwsContext * m_sws_ctx
static MythCodecID GetBestSupportedCodec(uint width, uint height, const QString &decoder, uint stream_type, bool no_acceleration)
#define FF_VDPAU_STATE_USED_FOR_REFERENCE
static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
void SetErrored(const QString &reason)
const DVDRingBuffer * DVD(void) const
virtual int BestBufferSize(void)
#define VBI_TYPE_VPS
Definition: ivtv_myth.h:276
static MythCodecID GetBestSupportedCodec(AVCodec **ppCodec, const QString &decoder, uint stream_type, AVPixelFormat &pix_fmt)
void DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
PlayerFlags
Definition: mythplayer.h:88
uint8_t * m_audioSamples
long long m_readAdjust
Definition: decoderbase.h:349
virtual void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames)
Definition: decoderbase.cpp:73
static uint64_t samples[4]
Definition: element.c:45
bool onKeyFrameStart(void) const
Definition: H264Parser.h:155
#define VBI_TYPE_WSS
Definition: ivtv_myth.h:275
static int64_t AVF_Seek_Packet(void *opaque, int64_t offset, int whence)
virtual bool IsInDiscMenuOrStillFrame(void) const
VideoOutput * GetVideoOutput(void)
Definition: mythplayer.h:244