/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxUtils.cpp

http://github.com/xbmc/xbmc · C++ · 86 lines · 49 code · 7 blank · 30 comment · 6 complexity · 9cb38635026919815f3963567db3ce77 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
  21. #include "config.h"
  22. #endif
  23. #include "DVDDemuxUtils.h"
  24. #include "DVDClock.h"
  25. #include "utils/log.h"
  26. #include "DllAvCodec.h"
  27. void CDVDDemuxUtils::FreeDemuxPacket(DemuxPacket* pPacket)
  28. {
  29. if (pPacket)
  30. {
  31. try {
  32. if (pPacket->pData) _aligned_free(pPacket->pData);
  33. delete pPacket;
  34. }
  35. catch(...) {
  36. CLog::Log(LOGERROR, "%s - Exception thrown while freeing packet", __FUNCTION__);
  37. }
  38. }
  39. }
  40. DemuxPacket* CDVDDemuxUtils::AllocateDemuxPacket(int iDataSize)
  41. {
  42. DemuxPacket* pPacket = new DemuxPacket;
  43. if (!pPacket) return NULL;
  44. try
  45. {
  46. memset(pPacket, 0, sizeof(DemuxPacket));
  47. if (iDataSize > 0)
  48. {
  49. // need to allocate a few bytes more.
  50. // From avcodec.h (ffmpeg)
  51. /**
  52. * Required number of additionally allocated bytes at the end of the input bitstream for decoding.
  53. * this is mainly needed because some optimized bitstream readers read
  54. * 32 or 64 bit at once and could read over the end<br>
  55. * Note, if the first 23 bits of the additional bytes are not 0 then damaged
  56. * MPEG bitstreams could cause overread and segfault
  57. */
  58. pPacket->pData =(uint8_t*)_aligned_malloc(iDataSize + FF_INPUT_BUFFER_PADDING_SIZE, 16);
  59. if (!pPacket->pData)
  60. {
  61. FreeDemuxPacket(pPacket);
  62. return NULL;
  63. }
  64. // reset the last 8 bytes to 0;
  65. memset(pPacket->pData + iDataSize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  66. }
  67. // setup defaults
  68. pPacket->dts = DVD_NOPTS_VALUE;
  69. pPacket->pts = DVD_NOPTS_VALUE;
  70. pPacket->iStreamId = -1;
  71. }
  72. catch(...)
  73. {
  74. CLog::Log(LOGERROR, "%s - Exception thrown", __FUNCTION__);
  75. FreeDemuxPacket(pPacket);
  76. pPacket = NULL;
  77. }
  78. return pPacket;
  79. }