MythTV  master
thumbfinder.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * vim: set expandtab tabstop=4 shiftwidth=4:
3  *
4  * Original Project
5  * MythTV http://www.mythtv.org
6  *
7  * Copyright (c) 2004, 2005 John Pullan <john@pullan.org>
8  * Copyright (c) 2009, Janne Grunau <janne-mythtv@grunau.be>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24  *
25  */
26 
27 // c++
28 #include <cerrno>
29 #include <cmath>
30 #include <cstdlib>
31 #include <iostream>
32 #include <sys/stat.h>
33 
34 // qt
35 #include <QApplication>
36 #include <QDomDocument>
37 #include <QFile>
38 #include <QDir>
39 #include <QPainter>
40 
41 // myth
42 #include <mythcontext.h>
43 #include <mythdbcon.h>
44 #include <programinfo.h>
45 #include <mythuihelper.h>
46 #include <mythmainwindow.h>
47 #include <mythdialogbox.h>
48 #include <mythdirs.h>
49 #include <mythmiscutil.h>
50 #include <mythuitext.h>
51 #include <mythuibutton.h>
52 #include <mythuiimage.h>
53 #include <mythuibuttonlist.h>
54 #include <mythimage.h>
55 #include <mythconfig.h>
56 
57 extern "C" {
58 #include "libavutil/imgutils.h"
59 }
60 
61 #ifndef INT64_C // Used in FFmpeg headers to define some constants
62 #define INT64_C(v) (v ## LL)
63 #endif
64 
65 // mytharchive
66 #include "thumbfinder.h"
67 
68 // the amount to seek before the required frame
69 #define PRE_SEEK_AMOUNT 50
70 
72 {
73  {"frame", -1},
74  {"1 second", 1},
75  {"5 seconds", 5},
76  {"10 seconds", 10},
77  {"30 seconds", 30},
78  {"1 minute", 60},
79  {"5 minutes", 300},
80  {"10 minutes", 600},
81  {"Cut Point", -2},
82 };
83 
84 int SeekAmountsCount = sizeof(SeekAmounts) / sizeof(SeekAmounts[0]);
85 
87  const QString &menuTheme)
88  :MythScreenType(parent, "ThumbFinder"),
89  m_archiveItem(archiveItem),
90  m_thumbCount(getChapterCount(menuTheme)),
91  m_thumbDir(createThumbDir())
92 {
93  // copy thumbList so we can abandon changes if required
94  m_thumbList.clear();
95  for (const auto *item : qAsConst(m_archiveItem->thumbList))
96  {
97  auto *thumb = new ThumbImage;
98  *thumb = *item;
99  m_thumbList.append(thumb);
100  }
101 }
102 
104 {
105  getThumbImages();
106 }
107 
109 {
110  while (!m_thumbList.isEmpty())
111  delete m_thumbList.takeFirst();
112  m_thumbList.clear();
113 
114  closeAVCodec();
115 }
116 
118 {
119  // Load the theme for this screen
120  bool foundtheme = LoadWindowFromXML("mythburn-ui.xml", "thumbfinder", this);
121  if (!foundtheme)
122  return false;
123 
124  bool err = false;
125  UIUtilE::Assign(this, m_frameImage, "frameimage", &err);
126  UIUtilE::Assign(this, m_positionImage, "positionimage", &err);
127  UIUtilE::Assign(this, m_imageGrid, "thumblist", &err);
128  UIUtilE::Assign(this, m_saveButton, "save_button", &err);
129  UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
130  UIUtilE::Assign(this, m_frameButton, "frame_button", &err);
131  UIUtilE::Assign(this, m_seekAmountText, "seekamount", &err);
132  UIUtilE::Assign(this, m_currentPosText, "currentpos", &err);
133 
134  if (err)
135  {
136  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'mythburn'");
137  return false;
138  }
139 
140  connect(m_imageGrid, SIGNAL(itemSelected(MythUIButtonListItem *)),
141  this, SLOT(gridItemChanged(MythUIButtonListItem *)));
142 
143  connect(m_saveButton, SIGNAL(Clicked()), this, SLOT(savePressed()));
144  connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(cancelPressed()));
145 
146  connect(m_frameButton, SIGNAL(Clicked()), this, SLOT(updateThumb()));
147 
149 
150  BuildFocusList();
151 
153 
154  return true;
155 }
156 
157 bool ThumbFinder::keyPressEvent(QKeyEvent *event)
158 {
159  if (GetFocusWidget()->keyPressEvent(event))
160  return true;
161 
162  QStringList actions;
163  bool handled = GetMythMainWindow()->TranslateKeyPress("Archive", event, actions);
164 
165  for (int i = 0; i < actions.size() && !handled; i++)
166  {
167  QString action = actions[i];
168  handled = true;
169 
170  if (action == "MENU")
171  {
172  NextPrevWidgetFocus(true);
173  return true;
174  }
175 
176  if (action == "ESCAPE")
177  {
178  ShowMenu();
179  return true;
180  }
181 
182  if (action == "0" || action == "1" || action == "2" || action == "3" ||
183  action == "4" || action == "5" || action == "6" || action == "7" ||
184  action == "8" || action == "9")
185  {
187  int itemNo = m_imageGrid->GetCurrentPos();
188  ThumbImage *thumb = m_thumbList.at(itemNo);
189  if (thumb)
190  seekToFrame(thumb->frame);
191  return true;
192  }
193 
194  if (GetFocusWidget() == m_frameButton)
195  {
196  if (action == "UP")
197  {
198  changeSeekAmount(true);
199  }
200  else if (action == "DOWN")
201  {
202  changeSeekAmount(false);
203  }
204  else if (action == "LEFT")
205  {
206  seekBackward();
207  }
208  else if (action == "RIGHT")
209  {
210  seekForward();
211  }
212  else if (action == "SELECT")
213  {
214  updateThumb();
215  }
216  else
217  handled = false;
218  }
219  else
220  handled = false;
221  }
222 
223  if (!handled && MythScreenType::keyPressEvent(event))
224  handled = true;
225 
226  return handled;
227 }
228 
229 int ThumbFinder::getChapterCount(const QString &menuTheme)
230 {
231  QString filename = GetShareDir() + "mytharchive/themes/" +
232  menuTheme + "/theme.xml";
233  QDomDocument doc("mydocument");
234  QFile file(filename);
235 
236  if (!file.open(QIODevice::ReadOnly))
237  {
238  LOG(VB_GENERAL, LOG_ERR, "Failed to open theme file: " + filename);
239  return 0; //??
240  }
241  if (!doc.setContent(&file))
242  {
243  file.close();
244  LOG(VB_GENERAL, LOG_ERR, "Failed to parse theme file: " + filename);
245  return 0;
246  }
247  file.close();
248 
249  QDomNodeList chapterNodeList = doc.elementsByTagName("chapter");
250 
251  return chapterNodeList.count();
252 }
253 
255 {
257 
258  if (progInfo && m_archiveItem->hasCutlist)
259  progInfo->QueryCutList(m_deleteMap);
260  delete progInfo;
261 
262  if (m_deleteMap.isEmpty())
263  {
264  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder::loadCutList: Got an empty delete map");
265  return;
266  }
267 
268  // if the first mark is a end mark then add the start mark at the beginning
269  frm_dir_map_t::const_iterator it = m_deleteMap.begin();
270  if (it.value() == MARK_CUT_END)
271  m_deleteMap.insert(0, MARK_CUT_START);
272 
273 
274  // if the last mark is a start mark then add the end mark at the end
275  it = m_deleteMap.end();
276  --it;
277  if (it != m_deleteMap.end())
278  {
279  if (it.value() == MARK_CUT_START)
281  }
282 }
283 
285 {
286  // copy the thumb details to the archiveItem
287  while (!m_archiveItem->thumbList.isEmpty())
288  delete m_archiveItem->thumbList.takeFirst();
289  m_archiveItem->thumbList.clear();
290 
291  for (const auto *item : qAsConst(m_thumbList))
292  {
293  auto *thumb = new ThumbImage;
294  *thumb = *item;
295  m_archiveItem->thumbList.append(thumb);
296  }
297 
298  Close();
299 }
300 
302 {
303  Close();
304 }
305 
307 {
308  int64_t pos = m_currentPTS - m_firstIFramePTS;
309  int64_t frame = pos / m_frameTime;
310 
311  if (m_currentPosText)
312  m_currentPosText->SetText(frameToTime(frame, true));
313 
314  updatePositionBar(frame);
315 }
316 
318 {
319  if (up)
320  {
321  m_currentSeek++;
323  m_currentSeek = 0;
324  }
325  else
326  {
327  m_currentSeek--;
328  if (m_currentSeek < 0)
330  }
331 
333 }
334 
336 {
337  (void) item;
338 
339  int itemNo = m_imageGrid->GetCurrentPos();
340  ThumbImage *thumb = m_thumbList.at(itemNo);
341  if (thumb)
342  seekToFrame(thumb->frame);
343 }
344 
346 {
347  QString thumbDir = getTempDirectory() + "config/thumbs";
348 
349  // make sure the thumb directory exists
350  QDir dir(thumbDir);
351  if (!dir.exists())
352  {
353  dir.mkdir(thumbDir);
354  if( chmod(qPrintable(thumbDir), 0777) != 0 )
355  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: Failed to change permissions"
356  " on thumb directory: " + ENO);
357  }
358 
359  QString path;
360  for (int x = 1; dir.exists(); x++)
361  {
362  path = thumbDir + QString("/%1").arg(x);
363  dir.setPath(path);
364  }
365 
366  dir.mkdir(path);
367  if( chmod(qPrintable(path), 0777) != 0 )
368  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: Failed to change permissions on "
369  "thumb directory: %1" + ENO);
370 
371  return path;
372 }
373 
375 {
376  int itemNo = m_imageGrid->GetCurrentPos();
378 
379  ThumbImage *thumb = m_thumbList.at(itemNo);
380  if (!thumb)
381  return;
382 
383  // copy current frame image to the selected thumb image
384  QString imageFile = thumb->filename;
385  QFile dst(imageFile);
386  QFile src(m_frameFile);
387  copy(dst, src);
388 
389  item->SetImage(imageFile, "", true);
390 
391  // update the image grid item
392  int64_t pos = (int) ((m_currentPTS - m_startPTS) / m_frameTime);
393  thumb->frame = pos - m_offset;
394  if (itemNo != 0)
395  {
396  thumb->caption = frameToTime(thumb->frame);
397  item->SetText(thumb->caption);
398  }
399 
401 }
402 
403 QString ThumbFinder::frameToTime(int64_t frame, bool addFrame) const
404 {
405  int sec = (int) (frame / m_fps);
406  frame = frame - (int) (sec * m_fps);
407 
408  QString str = MythFormatTime(sec, "HH:mm:ss");
409  if (addFrame)
410  str += QString(".%1").arg(frame,10,2,QChar('0'));
411  return str;
412 }
413 
415 {
417  {
418  LOG(VB_GENERAL, LOG_ERR,
419  QString("ThumbFinder:: Failed to get file details for %1")
420  .arg(m_archiveItem->filename));
421  return false;
422  }
423 
425  return false;
426 
427  if (m_archiveItem->type == "Recording")
428  loadCutList();
429 
430  // calculate the file duration taking the cut list into account
432 
433  QString origFrameFile = m_frameFile;
434 
435  m_updateFrame = true;
436  getFrameImage();
437 
438  int chapterLen = 0;
439  if (m_thumbCount)
440  chapterLen = m_finalDuration / m_thumbCount;
441  else
442  chapterLen = m_finalDuration;
443 
444  QString thumbList = "";
445  m_updateFrame = false;
446 
447  // add title thumb
448  m_frameFile = m_thumbDir + "/title.jpg";
449  ThumbImage *thumb = nullptr;
450 
451  if (!m_thumbList.empty())
452  {
453  // use the thumb details in the thumbList if already available
454  thumb = m_thumbList.at(0);
455  }
456 
457  if (!thumb)
458  {
459  // no thumb available create a new one
460  thumb = new ThumbImage;
461  thumb->filename = m_frameFile;
462  thumb->frame = (int64_t) 0;
463  thumb->caption = "Title";
464  m_thumbList.append(thumb);
465  }
466  else
467  m_frameFile = thumb->filename;
468 
469  seekToFrame(thumb->frame);
470  getFrameImage();
471 
472  new MythUIButtonListItem(m_imageGrid, thumb->caption, thumb->filename);
473 
474  QCoreApplication::processEvents();
475 
476  for (int x = 1; x <= m_thumbCount; x++)
477  {
478  m_frameFile = m_thumbDir + QString("/chapter-%1.jpg").arg(x);
479 
480  thumb = nullptr;
481 
482  if (m_archiveItem->thumbList.size() > x)
483  {
484  // use the thumb details in the archiveItem if already available
485  thumb = m_archiveItem->thumbList.at(x);
486  }
487 
488  if (!thumb)
489  {
490  int chapter = chapterLen * (x - 1);
491  int hour = chapter / 3600;
492  int min = (chapter % 3600) / 60;
493  int sec = chapter % 60;
494  QString time = QString::asprintf("%02d:%02d:%02d", hour, min, sec);
495 
496  auto frame = (int64_t) (chapter * ceil(m_fps));
497 
498  // no thumb available create a new one
499  thumb = new ThumbImage;
500  thumb->filename = m_frameFile;
501  thumb->frame = frame;
502  thumb->caption = time;
503  m_thumbList.append(thumb);
504  }
505  else
506  m_frameFile = thumb->filename;
507 
508  seekToFrame(thumb->frame);
509  QCoreApplication::processEvents();
510  getFrameImage();
511  QCoreApplication::processEvents();
512  new MythUIButtonListItem(m_imageGrid, thumb->caption, thumb->filename);
513  QCoreApplication::processEvents();
514  }
515 
516  m_frameFile = origFrameFile;
517  seekToFrame(0);
518 
519  m_updateFrame = true;
520 
522 
524 
525  return true;
526 }
527 
528 bool ThumbFinder::initAVCodec(const QString &inFile)
529 {
530  // Open recording
531  LOG(VB_JOBQUEUE, LOG_INFO, QString("ThumbFinder: Opening '%1'")
532  .arg(inFile));
533 
534  if (!m_inputFC.Open(inFile))
535  {
536  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder, Couldn't open input file" + ENO);
537  return false;
538  }
539 
540  // Getting stream information
541  int ret = avformat_find_stream_info(m_inputFC, nullptr);
542  if (ret < 0)
543  {
544  LOG(VB_GENERAL, LOG_ERR,
545  QString("Couldn't get stream info, error #%1").arg(ret));
546  return false;
547  }
548 
549  av_dump_format(m_inputFC, 0, qPrintable(inFile), 0);
550 
551  // find the first video stream
552  m_videostream = -1;
553 
554  for (uint i = 0; i < m_inputFC->nb_streams; i++)
555  {
556  AVStream *st = m_inputFC->streams[i];
557  if (m_inputFC->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
558  {
559  m_startTime = -1;
560  if (m_inputFC->streams[i]->start_time != (int) AV_NOPTS_VALUE)
561  m_startTime = m_inputFC->streams[i]->start_time;
562  else
563  {
564  LOG(VB_GENERAL, LOG_ERR,
565  "ThumbFinder: Failed to get start time");
566  return false;
567  }
568 
569  m_videostream = i;
570  m_frameWidth = st->codecpar->width;
571  m_frameHeight = st->codecpar->height;
572  if (st->r_frame_rate.den && st->r_frame_rate.num)
573  m_fps = av_q2d(st->r_frame_rate);
574  else
575  m_fps = 1/av_q2d(st->time_base);
576  break;
577  }
578  }
579 
580  if (m_videostream == -1)
581  {
582  LOG(VB_GENERAL, LOG_ERR, "Couldn't find a video stream");
583  return false;
584  }
585 
586  // get the codec context for the video stream
588  m_codecCtx->debug_mv = 0;
589  m_codecCtx->debug = 0;
590  m_codecCtx->workaround_bugs = 1;
591  m_codecCtx->lowres = 0;
592  m_codecCtx->idct_algo = FF_IDCT_AUTO;
593  m_codecCtx->skip_frame = AVDISCARD_DEFAULT;
594  m_codecCtx->skip_idct = AVDISCARD_DEFAULT;
595  m_codecCtx->skip_loop_filter = AVDISCARD_DEFAULT;
596  m_codecCtx->err_recognition = AV_EF_CAREFUL;
597  m_codecCtx->error_concealment = 3;
598 
599  // get decoder for video stream
600  m_codec = avcodec_find_decoder(m_codecCtx->codec_id);
601 
602  if (m_codec == nullptr)
603  {
604  LOG(VB_GENERAL, LOG_ERR,
605  "ThumbFinder: Couldn't find codec for video stream");
606  return false;
607  }
608 
609  // open codec
610  if (avcodec_open2(m_codecCtx, m_codec, nullptr) < 0)
611  {
612  LOG(VB_GENERAL, LOG_ERR,
613  "ThumbFinder: Couldn't open codec for video stream");
614  return false;
615  }
616 
617  // allocate temp buffer
618  int bufflen = m_frameWidth * m_frameHeight * 4;
619  m_outputbuf = new unsigned char[bufflen];
620 
621  m_frameFile = getTempDirectory() + "work/frame.jpg";
622 
625  return true;
626 }
627 
629 {
630  if (m_deleteMap.isEmpty() || !m_archiveItem->useCutlist)
631  return frameNumber;
632 
633  int diff = 0;
634  frm_dir_map_t::const_iterator it = m_deleteMap.find(frameNumber);
635 
636  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
637  {
638  int start = it.key();
639 
640  ++it;
641  if (it == m_deleteMap.end())
642  {
643  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
644  break;
645  }
646 
647  int end = it.key();
648 
649  if (start <= frameNumber + diff)
650  diff += end - start;
651  }
652 
653  m_offset = diff;
654  return frameNumber + diff;
655 }
656 
657 bool ThumbFinder::seekToFrame(int frame, bool checkPos)
658 {
659  // make sure the frame is not in a cut point
660  if (checkPos)
661  frame = checkFramePosition(frame);
662 
663  // seek to a position PRE_SEEK_AMOUNT frames before the required frame
664  int64_t timestamp = m_startTime + (frame * m_frameTime) -
666  int64_t requiredPTS = m_startPTS + (frame * m_frameTime);
667 
668  if (timestamp < m_startTime)
669  timestamp = m_startTime;
670 
671  if (av_seek_frame(m_inputFC, m_videostream, timestamp, AVSEEK_FLAG_ANY) < 0)
672  {
673  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder::SeekToFrame: seek failed") ;
674  return false;
675  }
676 
677  avcodec_flush_buffers(m_codecCtx);
678  getFrameImage(true, requiredPTS);
679 
680  return true;
681 }
682 
684 {
685  int64_t currentFrame = (m_currentPTS - m_startPTS) / m_frameTime;
686 
687  int inc = SeekAmounts[m_currentSeek].amount;
688 
689  if (inc == -1)
690  inc = 1;
691  else if (inc == -2)
692  {
693  int pos = 0;
694  frm_dir_map_t::const_iterator it;
695  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
696  {
697  if (it.key() > (uint64_t)currentFrame)
698  {
699  pos = it.key();
700  break;
701  }
702  }
703  // seek to next cutpoint
704  m_offset = 0;
705  seekToFrame(pos, false);
706  return true;
707  }
708  else
709  inc = (int) (inc * ceil(m_fps));
710 
711  int64_t newFrame = currentFrame + inc - m_offset;
712  if (newFrame == currentFrame + 1)
713  getFrameImage(false);
714  else
715  seekToFrame(newFrame);
716 
717  return true;
718 }
719 
721 {
722  int64_t currentFrame = (m_currentPTS - m_startPTS) / m_frameTime;
723 
724  int inc = SeekAmounts[m_currentSeek].amount;
725  if (inc == -1)
726  inc = -1;
727  else if (inc == -2)
728  {
729  // seek to previous cut point
730  frm_dir_map_t::const_iterator it;
731  int pos = 0;
732  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
733  {
734  if (it.key() >= (uint64_t)currentFrame)
735  break;
736 
737  pos = it.key();
738  }
739 
740  // seek to next cutpoint
741  m_offset = 0;
742  seekToFrame(pos, false);
743  return true;
744  }
745  else
746  inc = (int) (-inc * ceil(m_fps));
747 
748  int64_t newFrame = currentFrame + inc - m_offset;
749  seekToFrame(newFrame);
750 
751  return true;
752 }
753 
754 bool ThumbFinder::getFrameImage(bool needKeyFrame, int64_t requiredPTS)
755 {
756  AVPacket pkt;
757  AVFrame orig;
758  AVFrame retbuf;
759  memset(&orig, 0, sizeof(AVFrame));
760  memset(&retbuf, 0, sizeof(AVFrame));
761 
762  av_init_packet(&pkt);
763 
764  bool frameFinished = false;
765  int frameCount = 0;
766  bool gotKeyFrame = false;
767 
768  while (av_read_frame(m_inputFC, &pkt) >= 0 && !frameFinished)
769  {
770  if (pkt.stream_index == m_videostream)
771  {
772  frameCount++;
773 
774  int keyFrame = pkt.flags & AV_PKT_FLAG_KEY;
775 
776  if (m_startPTS == -1 && pkt.dts != AV_NOPTS_VALUE)
777  {
778  m_startPTS = pkt.dts;
779  m_frameTime = pkt.duration;
780  }
781 
782  if (keyFrame)
783  gotKeyFrame = true;
784 
785  if (!gotKeyFrame && needKeyFrame)
786  {
787  av_packet_unref(&pkt);
788  continue;
789  }
790 
791  if (m_firstIFramePTS == -1)
792  m_firstIFramePTS = pkt.dts;
793 
794  av_frame_unref(m_frame);
795  frameFinished = false;
796  int ret = avcodec_receive_frame(m_codecCtx, m_frame);
797  if (ret == 0)
798  frameFinished = true;
799  if (ret == 0 || ret == AVERROR(EAGAIN))
800  avcodec_send_packet(m_codecCtx, &pkt);
801  if (requiredPTS != -1 && pkt.dts != AV_NOPTS_VALUE && pkt.dts < requiredPTS)
802  frameFinished = false;
803 
804  m_currentPTS = pkt.dts;
805  }
806 
807  av_packet_unref(&pkt);
808  }
809 
810  if (frameFinished)
811  {
812  av_image_fill_arrays(retbuf.data, retbuf.linesize, m_outputbuf,
813  AV_PIX_FMT_RGB32, m_frameWidth, m_frameHeight, IMAGE_ALIGN);
814  AVFrame *tmp = m_frame;
815 
816  m_deinterlacer->DeinterlaceSingle(tmp, tmp);
817 
818  m_copy.Copy(&retbuf, AV_PIX_FMT_RGB32, tmp, m_codecCtx->pix_fmt,
820 
822  QImage::Format_RGB32);
823 
824  QByteArray ffile = m_frameFile.toLocal8Bit();
825  if (!img.save(ffile.constData(), "JPEG"))
826  {
827  LOG(VB_GENERAL, LOG_ERR, "Failed to save thumb: " + m_frameFile);
828  }
829 
830  if (m_updateFrame)
831  {
832  MythImage *mimage =
834  mimage->Assign(img);
835  m_frameImage->SetImage(mimage);
836  mimage->DecrRef();
837  }
838 
840  }
841 
842  return true;
843 }
844 
846 {
847  delete[] m_outputbuf;
848 
849  // close the codec
851 
852  // close the video file
853  m_inputFC.Close();
854 }
855 
857 {
858  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
859  auto *menuPopup = new MythDialogBox(tr("Menu"), popupStack, "actionmenu");
860 
861  if (menuPopup->Create())
862  popupStack->AddScreen(menuPopup);
863 
864  menuPopup->SetReturnEvent(this, "action");
865 
866  menuPopup->AddButton(tr("Exit, Save Thumbnails"), SLOT(savePressed()));
867  menuPopup->AddButton(tr("Exit, Don't Save Thumbnails"), SLOT(cancelPressed()));
868 }
869 
871 {
872  if (!m_positionImage)
873  return;
874 
875  QSize size = m_positionImage->GetArea().size();
876  auto *pixmap = new QPixmap(size.width(), size.height());
877 
878  QPainter p(pixmap);
879  QBrush brush(Qt::green);
880 
881  p.setBrush(brush);
882  p.setPen(Qt::NoPen);
883  p.fillRect(0, 0, size.width(), size.height(), brush);
884 
885  frm_dir_map_t::const_iterator it;
886 
887  brush.setColor(Qt::red);
888 
889  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
890  {
891  double startdelta = size.width();
892  if (it.key() != 0)
893  startdelta = (m_archiveItem->duration * m_fps) / it.key();
894 
895  ++it;
896  if (it == m_deleteMap.end())
897  {
898  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
899  break;
900  }
901 
902  double enddelta = size.width();
903  if (it.key() != 0)
904  enddelta = (m_archiveItem->duration * m_fps) / it.key();
905 
906  int start = (int) (size.width() / startdelta);
907  int end = (int) (size.width() / enddelta);
908  p.fillRect(start - 1, 0, end - start, size.height(), brush);
909  }
910 
911  if (frame == 0)
912  frame = 1;
913  brush.setColor(Qt::yellow);
914  int pos = (int) (size.width() / ((m_archiveItem->duration * m_fps) / frame));
915  p.fillRect(pos, 0, 3, size.height(), brush);
916 
918  image->Assign(*pixmap);
919  m_positionImage->SetImage(image);
920 
921  p.end();
922  delete pixmap;
923 }
924 
926 {
927  if (m_archiveItem->type == "Recording")
928  {
930  {
931  frm_dir_map_t::const_iterator it;
932 
933  int cutLen = 0;
934 
935  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
936  {
937  int start = it.key();
938 
939  ++it;
940  if (it == m_deleteMap.end())
941  {
942  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
943  break;
944  }
945 
946  int end = it.key();
947  cutLen += end - start;
948  }
949  return m_archiveItem->duration - (int) (cutLen / m_fps);
950  }
951  }
952 
953  return m_archiveItem->duration;
954 }
void updateCurrentPos(void)
QString filename
Definition: archiveutil.h:47
struct SeekAmount SeekAmounts[]
Definition: thumbfinder.cpp:71
MythAVCopy m_copy
Definition: thumbfinder.h:86
int m_videostream
Definition: thumbfinder.h:94
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void SetImage(MythImage *img)
Should not be used unless absolutely necessary since it bypasses the image caching and threaded loade...
QString caption
Definition: archiveutil.h:46
bool getThumbImages(void)
void cancelPressed(void)
int64_t m_currentPTS
Definition: thumbfinder.h:98
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
int Copy(VideoFrame *dst, const VideoFrame *src)
Definition: mythavutil.cpp:330
QString getTempDirectory(bool showError)
Definition: archiveutil.cpp:71
AVCodecContext * m_codecCtx
Definition: thumbfinder.h:82
QString m_frameFile
Definition: thumbfinder.h:91
bool hasCutlist
Definition: archiveutil.h:70
void SetRedraw(void)
Definition: mythuitype.cpp:293
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:55
bool useCutlist
Definition: archiveutil.h:71
Basic menu dialog, message and a list of options.
int m_frameWidth
Definition: thumbfinder.h:92
ArchiveItem * m_archiveItem
Definition: thumbfinder.h:106
struct AVFrame AVFrame
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
bool Create(void) override
QDomDocument doc("MYTHARCHIVEITEM")
MythScreenStack * GetStack(const QString &stackname)
QString MythFormatTime(int secs, QString fmt)
Format a seconds time value.
int64_t m_startTime
Definition: thumbfinder.h:96
#define PRE_SEEK_AMOUNT
Definition: thumbfinder.cpp:69
QString name
Definition: thumbfinder.h:24
bool getFileDetails(ArchiveItem *a)
static guint32 * tmp
Definition: goom_core.cpp:30
long long copy(QFile &dst, QFile &src, uint block_size)
Copies src file to dst file.
void changeSeekAmount(bool up)
void freeCodecContext(const AVStream *stream)
Definition: mythavutil.cpp:554
frm_dir_map_t m_deleteMap
Definition: thumbfinder.h:102
MythAVFrame m_frame
Definition: thumbfinder.h:85
void BuildFocusList(void)
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
qint64 frame
Definition: archiveutil.h:48
int64_t m_firstIFramePTS
Definition: thumbfinder.h:99
QString filename
Definition: archiveutil.h:60
virtual void Close()
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Holds information on recordings and videos.
Definition: programinfo.h:67
MythUIButton * m_frameButton
Definition: thumbfinder.h:112
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
int m_currentSeek
Definition: thumbfinder.h:95
int checkFramePosition(int frameNumber)
MythUIButtonList * m_imageGrid
Definition: thumbfinder.h:117
int SeekAmountsCount
Definition: thumbfinder.cpp:84
int calcFinalDuration(void)
bool initAVCodec(const QString &inFile)
AVCodecContext * getCodecContext(const AVStream *stream, const AVCodec *pCodec=nullptr, bool nullCodec=false)
Definition: mythavutil.cpp:512
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:864
virtual bool NextPrevWidgetFocus(bool up_or_down)
QString GetShareDir(void)
Definition: mythdirs.cpp:222
MythUIButton * m_saveButton
Definition: thumbfinder.h:113
bool getFrameImage(bool needKeyFrame=true, int64_t requiredPTS=-1)
RemoteAVFormatContext m_inputFC
Definition: thumbfinder.h:81
void gridItemChanged(MythUIButtonListItem *item)
bool seekToFrame(int frame, bool checkPos=true)
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
unsigned int uint
Definition: compat.h:140
MythPainter * GetCurrentPainter()
bool m_updateFrame
Definition: thumbfinder.h:101
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:69
QString frameToTime(int64_t frame, bool addFrame=false) const
QScopedPointer< MythPictureDeinterlacer > m_deinterlacer
Definition: thumbfinder.h:87
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
QString m_thumbDir
Definition: thumbfinder.h:109
MythUIType * GetFocusWidget(void) const
QString type
Definition: archiveutil.h:54
ThumbFinder(MythScreenStack *parent, ArchiveItem *archiveItem, const QString &menuTheme)
Definition: thumbfinder.cpp:86
void ShowMenu(void) override
MythMainWindow * GetMythMainWindow(void)
void savePressed(void)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
MythPictureDeinterlacer simple deinterlacer based on FFmpeg's yadif filter.
Definition: mythavutil.h:168
void loadCutList(void)
MythUIImage * m_positionImage
Definition: thumbfinder.h:116
float m_fps
Definition: thumbfinder.h:89
void closeAVCodec()
MythUIButton * m_cancelButton
Definition: thumbfinder.h:114
MythUIText * m_currentPosText
Definition: thumbfinder.h:119
void updateThumb(void)
static QString createThumbDir(void)
~ThumbFinder() override
MythCodecMap m_codecMap
Definition: thumbfinder.h:83
static int getChapterCount(const QString &menuTheme)
QList< ThumbImage * > m_thumbList
Definition: thumbfinder.h:108
void SetItemCurrent(MythUIButtonListItem *item)
MythUIImage * m_frameImage
Definition: thumbfinder.h:115
bool SetFocusWidget(MythUIType *widget=nullptr)
int m_thumbCount
Definition: thumbfinder.h:107
ProgramInfo * getProgramInfoForFile(const QString &inFile)
unsigned char * m_outputbuf
Definition: thumbfinder.h:90
Screen in which all other widgets are contained and rendered.
void Assign(const QImage &img)
Definition: mythimage.cpp:80
int m_frameHeight
Definition: thumbfinder.h:93
int GetCurrentPos() const
AVCodec * m_codec
Definition: thumbfinder.h:84
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
bool QueryCutList(frm_dir_map_t &delMap, bool loadAutosave=false) const
int64_t m_startPTS
Definition: thumbfinder.h:97
QList< ThumbImage * > thumbList
Definition: archiveutil.h:73
bool seekForward()
bool seekBackward()
void updatePositionBar(int64_t frame)
MythUIText * m_seekAmountText
Definition: thumbfinder.h:118
MythUIButtonListItem * GetItemCurrent() const
int m_finalDuration
Definition: thumbfinder.h:103
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23