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

VC1HeaderParser.cpp « parsers « LAVVideo « decoder - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b71d6e7edd06311e693eab7e16fcb3e72f593493 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *      Copyright (C) 2010-2015 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 "VC1HeaderParser.h"

#pragma warning( push )
#pragma warning( disable : 4101 )
extern "C" {
#define AVCODEC_X86_MATHOPS_H
#include "libavcodec/get_bits.h"
#include "libavcodec/unary.h"
extern __declspec(dllimport) const AVRational ff_vc1_pixel_aspect[16];
};
#pragma warning( pop )

/** Markers used in VC-1 AP frame data */
//@{
enum VC1Code{
  VC1_CODE_RES0       = 0x00000100,
  VC1_CODE_ENDOFSEQ   = 0x0000010A,
  VC1_CODE_SLICE,
  VC1_CODE_FIELD,
  VC1_CODE_FRAME,
  VC1_CODE_ENTRYPOINT,
  VC1_CODE_SEQHDR,
};
//@}

/** Available Profiles */
//@{
enum Profile {
  PROFILE_SIMPLE,
  PROFILE_MAIN,
  PROFILE_COMPLEX, ///< TODO: WMV9 specific
  PROFILE_ADVANCED
};
//@}

enum FrameCodingMode {
  PROGRESSIVE = 0,    ///<  in the bitstream is reported as 00b
  ILACE_FRAME,        ///<  in the bitstream is reported as 10b
  ILACE_FIELD         ///<  in the bitstream is reported as 11b
};

#define IS_MARKER(x) (((x) & ~0xFF) == VC1_CODE_RES0)

/** Find VC-1 marker in buffer
* @return position where next marker starts or end of buffer if no marker found
*/
static inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end)
{
  uint32_t mrk = 0xFFFFFFFF;

  if(end-src < 4) return end;
  while(src < end){
    mrk = (mrk << 8) | *src++;
    if(IS_MARKER(mrk))
      return src-4;
  }
  return end;
}

static inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
{
    int dsize = 0, i;

    if(size < 4){
        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
        return size;
    }
    for(i = 0; i < size; i++, src++) {
        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
            dst[dsize++] = src[1];
            src++;
            i++;
        } else
            dst[dsize++] = *src;
    }
    return dsize;
}

CVC1HeaderParser::CVC1HeaderParser(const BYTE *pData, size_t length, AVCodecID codec)
{
  memset(&hdr, 0, sizeof(hdr));
  ParseVC1Header(pData, length, codec);
}

CVC1HeaderParser::~CVC1HeaderParser(void)
{
}

void CVC1HeaderParser::ParseVC1Header(const BYTE *pData, size_t length, AVCodecID codec)
{
  GetBitContext gb;
  if (codec == AV_CODEC_ID_VC1) {
    if (length < 16)
      return;

    const uint8_t *start = pData;
    const uint8_t *end = start + length;
    const uint8_t *next = nullptr;

    int size, buf2_size;
    uint8_t *buf2;

    buf2 = (uint8_t *)av_mallocz(length + FF_INPUT_BUFFER_PADDING_SIZE);

    start = find_next_marker(start, end);
    next = start;

    for(; next < end; start = next) {
      next = find_next_marker(start + 4, end);
      size = (int)(next - start - 4);
      if(size <= 0) continue;
      buf2_size = vc1_unescape_buffer(start + 4, size, buf2);

      init_get_bits(&gb, buf2, buf2_size * 8);

      switch(AV_RB32(start)) {
        case VC1_CODE_SEQHDR:
          VC1ParseSequenceHeader(&gb);
          break;
      }
    }
    av_freep(&buf2);
  } else if (codec == AV_CODEC_ID_WMV3) {
    if (length < 4)
      return;
    init_get_bits8(&gb, pData, (int)length);
    VC1ParseSequenceHeader(&gb);
  }
}

void CVC1HeaderParser::VC1ParseSequenceHeader(GetBitContext *gb)
{
  hdr.profile = get_bits(gb, 2);

  if (hdr.profile == PROFILE_ADVANCED) {
    hdr.valid = 1;

    hdr.level = get_bits(gb, 3);
    skip_bits(gb, 2); // Chroma Format, only 1 should be set for 4:2:0
    skip_bits(gb, 3); // frmrtq_postproc
    skip_bits(gb, 5); // bitrtq_postproc
    skip_bits1(gb);   // postprocflag

    hdr.width = (get_bits(gb, 12) + 1) << 1;
    hdr.height = (get_bits(gb, 12) + 1) << 1;

    hdr.broadcast = get_bits1(gb);    // broadcast
    hdr.interlaced = get_bits1(gb);   // interlaced

    skip_bits1(gb); // tfcntrflag
    skip_bits1(gb); // finterpflag
    skip_bits1(gb); // reserved
    skip_bits1(gb); // psf

    if (get_bits1(gb)) { // Display Info
      int w, h, ar = 0;
      w = get_bits(gb, 14) + 1;
      h = get_bits(gb, 14) + 1;
      if (get_bits1(gb))
        ar = get_bits(gb, 4);
      if (ar && ar < 14) {
        hdr.ar = ff_vc1_pixel_aspect[ar];
      } else if (ar == 15) {
        w = get_bits(gb, 8) + 1;
        h = get_bits(gb, 8) + 1;
        hdr.ar.num = w;
        hdr.ar.den = h;
      } else {
        av_reduce(&hdr.ar.num, &hdr.ar.den, (int64_t)hdr.height * w, (int64_t)hdr.width * h, INT_MAX);
      }
    }

    // TODO: add other fields
  } else {
    hdr.valid = 1;
    hdr.old_interlaced = get_bits1(gb); // res_y411
    skip_bits1(gb); // res_sprite

    skip_bits(gb, 3); // frmrtq_postproc
    skip_bits(gb, 5); // bitrtq_postproc
    skip_bits1(gb);   // loop_filter
    skip_bits1(gb);   // res_x8
    skip_bits1(gb);   // multires
    skip_bits1(gb);   // rest_fasttx
    skip_bits1(gb);   // fastuvmc
    skip_bits1(gb);   // extended_mv
    skip_bits(gb, 2); // dquant
    skip_bits1(gb);   // vstransform
    skip_bits1(gb);   // res_transtab
    skip_bits1(gb);   // overlap
    skip_bits1(gb);   // resync marker
    hdr.rangered = get_bits1(gb);
    hdr.bframes = get_bits(gb, 3);
    skip_bits(gb, 2); // quant mode
    hdr.finterp = get_bits1(gb);
  }
}

AVPictureType CVC1HeaderParser::ParseVC1PictureType(const uint8_t *buf, int buflen)
{
  AVPictureType pictype = AV_PICTURE_TYPE_NONE;
  int skipped = 0;
  const BYTE *framestart = buf;
  if (IS_MARKER(AV_RB32(buf))) {
    framestart = nullptr;
    const BYTE *start, *end, *next;
    next = buf;
    for (start = buf, end = buf + buflen; next < end; start = next) {
      if (AV_RB32(start) == VC1_CODE_FRAME) {
        framestart = start + 4;
        break;
      }
      next = find_next_marker(start + 4, end);
    }
  }
  if (framestart) {
    GetBitContext gb;
    init_get_bits8(&gb, framestart, (int)(buflen - (framestart - buf)));
    if (hdr.profile == PROFILE_ADVANCED) {
      int fcm = PROGRESSIVE;
      if (hdr.interlaced)
        fcm = decode012(&gb);
      if (fcm == ILACE_FIELD) {
        int fptype = get_bits(&gb, 3);
        pictype = (fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
        if (fptype & 4) // B-picture
          pictype = (fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
      } else {
        switch (get_unary(&gb, 0, 4)) {
        case 0:
          pictype = AV_PICTURE_TYPE_P;
          break;
        case 1:
          pictype = AV_PICTURE_TYPE_B;
          break;
        case 2:
          pictype = AV_PICTURE_TYPE_I;
          break;
        case 3:
          pictype = AV_PICTURE_TYPE_BI;
          break;
        case 4:
          pictype = AV_PICTURE_TYPE_P; // skipped pic
          skipped = 1;
          break;
        }
      }
    } else {
      if (hdr.finterp)
        skip_bits1(&gb);
      skip_bits(&gb, 2); // framecnt
      if (hdr.rangered)
        skip_bits1(&gb);
      int pic = get_bits1(&gb);
      if (hdr.bframes) {
        if (!pic) {
          if (get_bits1(&gb)) {
            pictype = AV_PICTURE_TYPE_I;
          } else {
            pictype = AV_PICTURE_TYPE_B;
          }
        } else {
          pictype = AV_PICTURE_TYPE_P;
        }
      } else {
        pictype = pic ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
      }
    }
  }
  return pictype;
}