summaryrefslogtreecommitdiff
path: root/mfx_stub/bitstream.c
blob: 058cd755c3b17f1ec6f19b6a217d1affd7f58f3d (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
/*
 * Copyright (C) 2024 Mikhail Burakov. This file is part of receiver.
 *
 * receiver 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 3 of the License, or
 * (at your option) any later version.
 *
 * receiver 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 receiver.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "bitstream.h"

#include <string.h>

static uint64_t BitstreamReadBit(struct Bitstream* bitstream) {
  size_t shift = 7 - (bitstream->offset & 0x7);
  if (shift == 7) {
    size_t byte;
  repeat:
    byte = bitstream->offset >> 3;
    if (byte >= bitstream->size) longjmp(bitstream->trap, 1);
    bitstream->cache = (bitstream->cache << 8) | bitstream->data[byte];
    if (bitstream->offset >= 24 && (bitstream->cache & 0xffffff) == 3) {
      bitstream->offset += 8;
      bitstream->epb_count++;
      goto repeat;
    }
  }
  bitstream->offset++;
  return bitstream->cache >> shift & 0x1;
}

uint64_t BitstreamReadU(struct Bitstream* bitstream, size_t size) {
  uint64_t result = 0;
  for (; size; size--) {
    result = (result << 1) | BitstreamReadBit(bitstream);
  }
  return result;
}

uint64_t BitstreamReadUE(struct Bitstream* bitstream) {
  size_t size = 0;
  while (!BitstreamReadBit(bitstream)) size++;
  return (BitstreamReadU(bitstream, size) | (1 << size)) - 1;
}

int64_t BitstreamReadSE(struct Bitstream* bitstream) {
  uint64_t result = BitstreamReadUE(bitstream);
  int64_t sign = (int64_t)((result & 1) << 1) - 1;
  return sign * (int64_t)((result + 1) >> 1);
}

void BitstreamByteAlign(struct Bitstream* bitstream) {
  bitstream->offset = (bitstream->offset + 7) & ~(size_t)7;
}

bool BitstreamReadNalu(struct Bitstream* bitstream, struct Bitstream* output) {
  if (bitstream->offset & 0x7) return false;
  size_t byte_offset = bitstream->offset >> 3;
  static const uint8_t kStartCodePrefix[] = {0, 0, 0, 1};
  if (bitstream->size - byte_offset < sizeof(kStartCodePrefix)) {
    return false;
  }
  const uint8_t* data = bitstream->data + byte_offset;
  if (memcmp(bitstream->data + byte_offset, kStartCodePrefix,
             sizeof(kStartCodePrefix))) {
    return false;
  }
  data += sizeof(kStartCodePrefix);
  byte_offset += sizeof(kStartCodePrefix);
  size_t max_size = bitstream->size - byte_offset;
  uint8_t* next =
      memmem(data, max_size, kStartCodePrefix, sizeof(kStartCodePrefix));
  size_t size = next ? (size_t)(next - data) : max_size;
  bitstream->offset = (byte_offset + size) << 3;
  *output = BitstreamCreate(data, size);
  return true;
}

bool BitstreamAvail(const struct Bitstream* bitstream) {
  return bitstream->offset < (bitstream->size << 3);
}