PageRenderTime 22ms CodeModel.GetById 17ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
21#if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
22  #include "config.h"
23#endif
24#include "DVDDemuxUtils.h"
25#include "DVDClock.h"
26#include "utils/log.h"
27#include "DllAvCodec.h"
28
29void CDVDDemuxUtils::FreeDemuxPacket(DemuxPacket* pPacket)
30{
31  if (pPacket)
32  {
33    try {
34      if (pPacket->pData) _aligned_free(pPacket->pData);
35      delete pPacket;
36    }
37    catch(...) {
38      CLog::Log(LOGERROR, "%s - Exception thrown while freeing packet", __FUNCTION__);
39    }
40  }
41}
42
43DemuxPacket* CDVDDemuxUtils::AllocateDemuxPacket(int iDataSize)
44{
45  DemuxPacket* pPacket = new DemuxPacket;
46  if (!pPacket) return NULL;
47
48  try
49  {
50    memset(pPacket, 0, sizeof(DemuxPacket));
51
52    if (iDataSize > 0)
53    {
54      // need to allocate a few bytes more.
55      // From avcodec.h (ffmpeg)
56      /**
57        * Required number of additionally allocated bytes at the end of the input bitstream for decoding.
58        * this is mainly needed because some optimized bitstream readers read
59        * 32 or 64 bit at once and could read over the end<br>
60        * Note, if the first 23 bits of the additional bytes are not 0 then damaged
61        * MPEG bitstreams could cause overread and segfault
62        */
63      pPacket->pData =(uint8_t*)_aligned_malloc(iDataSize + FF_INPUT_BUFFER_PADDING_SIZE, 16);
64      if (!pPacket->pData)
65      {
66        FreeDemuxPacket(pPacket);
67        return NULL;
68      }
69
70      // reset the last 8 bytes to 0;
71      memset(pPacket->pData + iDataSize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
72    }
73
74    // setup defaults
75    pPacket->dts       = DVD_NOPTS_VALUE;
76    pPacket->pts       = DVD_NOPTS_VALUE;
77    pPacket->iStreamId = -1;
78  }
79  catch(...)
80  {
81    CLog::Log(LOGERROR, "%s - Exception thrown", __FUNCTION__);
82    FreeDemuxPacket(pPacket);
83    pPacket = NULL;
84  }
85  return pPacket;
86}