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