Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AVC1AnnexBConverter.cpp « parsers « LAVVideo « decoder - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c621d92a6b61149063c29ec76f0cc010ed05a8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 *      Copyright (C) 2010-2013 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "stdafx.h"
#include "AVC1AnnexBConverter.h"

#include "libavutil/intreadwrite.h"

CAVC1AnnexBConverter::CAVC1AnnexBConverter(void)
{
}

CAVC1AnnexBConverter::~CAVC1AnnexBConverter(void)
{
}

static HRESULT alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size, const uint8_t *in, uint32_t in_size) {
  uint32_t offset = *poutbuf_size;
  uint8_t nal_header_size = offset ? 3 : 4;
  void *tmp;

  *poutbuf_size += in_size+nal_header_size;
  tmp = av_realloc(*poutbuf, *poutbuf_size);
  if (!tmp)
    return E_OUTOFMEMORY;
  *poutbuf = (uint8_t *)tmp;
  memcpy(*poutbuf+nal_header_size+offset, in, in_size);
  if (!offset) {
    AV_WB32(*poutbuf, 1);
  } else {
    (*poutbuf+offset)[0] = (*poutbuf+offset)[1] = 0;
    (*poutbuf+offset)[2] = 1;
  }

  return S_OK;
}

HRESULT CAVC1AnnexBConverter::Convert(BYTE **poutbuf, int *poutbuf_size, const BYTE *buf, int buf_size)
{
  int32_t nal_size;
  const uint8_t *buf_end = buf + buf_size;

  *poutbuf_size = 0;

  do {
    if (buf + m_NaluSize > buf_end)
      goto fail;

    if (m_NaluSize == 1) {
      nal_size = buf[0];
    } else if (m_NaluSize == 2) {
      nal_size = AV_RB16(buf);
    } else {
      nal_size = AV_RB32(buf);
      if (m_NaluSize == 3)
        nal_size >>= 8;
    }

    buf += m_NaluSize;

    if (buf + nal_size > buf_end || nal_size < 0)
      goto fail;

    if (FAILED(alloc_and_copy(poutbuf, poutbuf_size, buf, nal_size)))
      goto fail;

    buf += nal_size;
    buf_size -= (nal_size + m_NaluSize);
  } while (buf_size > 0);

  return S_OK;
fail:
  av_freep(poutbuf);
  return E_FAIL;
}