summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/VC1BitstreamParser.cpp
blob: 8ac1b6e0dcb8725eb310d36e4f79b9d75a83ceee (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "VC1BitstreamParser.h"

#include "BitstreamReader.h"

enum
{
  VC1_PROFILE_SIMPLE,
  VC1_PROFILE_MAIN,
  VC1_PROFILE_RESERVED,
  VC1_PROFILE_ADVANCED,
  VC1_PROFILE_NOPROFILE
};

enum
{
  VC1_END_OF_SEQ = 0x0A,
  VC1_SLICE = 0x0B,
  VC1_FIELD = 0x0C,
  VC1_FRAME = 0x0D,
  VC1_ENTRYPOINT = 0x0E,
  VC1_SEQUENCE = 0x0F,
  VC1_SLICE_USER = 0x1B,
  VC1_FIELD_USER = 0x1C,
  VC1_FRAME_USER = 0x1D,
  VC1_ENTRY_POINT_USER = 0x1E,
  VC1_SEQUENCE_USER = 0x1F
};

enum
{
  VC1_FRAME_PROGRESSIVE = 0x0,
  VC1_FRAME_INTERLACE = 0x10,
  VC1_FIELD_INTERLACE = 0x11
};

CVC1BitstreamParser::CVC1BitstreamParser()
{
  Reset();
}

void CVC1BitstreamParser::Reset()
{
  m_Profile = VC1_PROFILE_NOPROFILE;
}

bool CVC1BitstreamParser::IsRecoveryPoint(const uint8_t *buf, int buf_size)
{
  return vc1_parse_frame(buf, buf + buf_size, true);
};

bool CVC1BitstreamParser::IsIFrame(const uint8_t *buf, int buf_size)
{
  return vc1_parse_frame(buf, buf + buf_size, false);
};

bool CVC1BitstreamParser::vc1_parse_frame(const uint8_t *buf, const uint8_t *buf_end, bool sequence_only)
{
  uint32_t state = -1;
  for (;;)
  {
    buf = find_start_code(buf, buf_end, &state);
    if (buf >= buf_end)
      break;
    if (buf[-1] == VC1_SEQUENCE)
    {
      if (m_Profile != VC1_PROFILE_NOPROFILE)
        return false;
      CBitstreamReader br(buf, buf_end - buf);
      // Read the profile
      m_Profile = static_cast<uint8_t>(br.ReadBits(2));
      if (m_Profile == VC1_PROFILE_ADVANCED)
      {
        br.SkipBits(39);
        m_AdvInterlace = br.ReadBits(1);
      }
      else
      {
        br.SkipBits(22);

        m_SimpleSkipBits = 2;
        if (br.ReadBits(1)) //rangered
          ++m_SimpleSkipBits;

        m_MaxBFrames = br.ReadBits(3);

        br.SkipBits(2); // quantizer
        if (br.ReadBits(1)) //finterpflag
          ++m_SimpleSkipBits;
      }
      if (sequence_only)
        return true;
    }
    else if (buf[-1] == VC1_FRAME)
    {
      CBitstreamReader br(buf, buf_end - buf);

      if (sequence_only)
        return false;
      if (m_Profile == VC1_PROFILE_ADVANCED)
      {
        uint8_t fcm;
        if (m_AdvInterlace) {
          fcm = br.ReadBits(1);
          if (fcm)
            fcm = br.ReadBits(1) + 1;
        }
        else
          fcm = VC1_FRAME_PROGRESSIVE;
        if (fcm == VC1_FIELD_INTERLACE) {
          uint8_t pic = br.ReadBits(3);
          return pic == 0x00 || pic == 0x01;
        }
        else
        {
          uint8_t pic(0);
          while (pic < 4 && br.ReadBits(1))++pic;
          return pic == 2;
        }
        return false;
      }
      else if (m_Profile != VC1_PROFILE_NOPROFILE)
      {
        br.SkipBits(m_SimpleSkipBits); // quantizer
        uint8_t pic(br.ReadBits(1));
        if (m_MaxBFrames) {
          if (!pic) {
            pic = br.ReadBits(1);
            return pic != 0;
          }
          else
            return false;
        }
        else
          return pic != 0;
      }
      else
        break;
    }
  }
  return false;
}