1 /*
2 * MidiVid decoder
3 * Copyright (c) 2019 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
25
26 #define BITSTREAM_READER_LE
32
35
39
42
44 {
48 uint16_t nb_vectors, intra_flag;
49 const uint8_t *vec;
50 const uint8_t *mask_start;
52 uint32_t mask_size;
53 int idx9bits = 0;
54 int idx9val = 0;
55 uint32_t nb_blocks;
56
57 nb_vectors = bytestream2_get_le16(gb);
58 intra_flag = !!bytestream2_get_le16(gb);
59 if (intra_flag) {
60 nb_blocks = (avctx->
width / 2) * (avctx->
height / 2);
61 } else {
62 int ret, skip_linesize, padding;
63
64 nb_blocks = bytestream2_get_le32(gb);
65 skip_linesize = avctx->
width >> 1;
69
72
78
79 for (
int y = 0; y < avctx->
height >> 2; y++) {
80 for (
int x = 0; x < avctx->
width >> 2; x++) {
82
83 skip[(y*2) *skip_linesize + x*2 ] =
flag;
84 skip[(y*2) *skip_linesize + x*2+1] =
flag;
85 skip[(y*2+1)*skip_linesize + x*2 ] =
flag;
86 skip[(y*2+1)*skip_linesize + x*2+1] =
flag;
87 }
89 }
90 }
91
96 if (nb_vectors > 256) {
101 }
102
104
105 for (
int y = avctx->
height - 2; y >= 0; y -= 2) {
106 uint8_t *dsty =
frame->data[0] + y *
frame->linesize[0];
107 uint8_t *dstu =
frame->data[1] + y *
frame->linesize[1];
108 uint8_t *dstv =
frame->data[2] + y *
frame->linesize[2];
109
110 for (
int x = 0; x < avctx->
width; x += 2) {
111 int idx;
112
113 if (!intra_flag && *
skip++)
114 continue;
117 if (nb_vectors <= 256) {
118 idx = bytestream2_get_byte(gb);
119 } else {
120 if (idx9bits == 0) {
121 idx9val = bytestream2_get_byte(&idx9);
122 idx9bits = 8;
123 }
124 idx9bits--;
125 idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
126 }
127 if (idx >= nb_vectors)
129
130 dsty[x +
frame->linesize[0]] = vec[idx * 12 + 0];
131 dsty[x+1+
frame->linesize[0]] = vec[idx * 12 + 3];
132 dsty[x] = vec[idx * 12 + 6];
133 dsty[x+1] = vec[idx * 12 + 9];
134
135 dstu[x +
frame->linesize[1]] = vec[idx * 12 + 1];
136 dstu[x+1+
frame->linesize[1]] = vec[idx * 12 + 4];
137 dstu[x] = vec[idx * 12 + 7];
138 dstu[x+1] = vec[idx * 12 +10];
139
140 dstv[x +
frame->linesize[2]] = vec[idx * 12 + 2];
141 dstv[x+1+
frame->linesize[2]] = vec[idx * 12 + 5];
142 dstv[x] = vec[idx * 12 + 8];
143 dstv[x+1] = vec[idx * 12 +11];
144 }
145 }
146
147 return intra_flag;
148 }
149
151 {
152 uint8_t *dst_start = dst;
153 uint8_t *dst_end = dst +
size;
154
156 int op = bytestream2_get_le16(gb);
157
158 for (
int i = 0;
i < 16;
i++) {
160 int s0 = bytestream2_get_byte(gb);
161 int s1 = bytestream2_get_byte(gb);
163 int length = (
s0 & 0xF) + 3;
164
165 if (dst + length > dst_end ||
169 for (int j = 0; j < length; j++) {
171 }
172 }
173 dst += length;
174 } else {
175 if (dst >= dst_end)
177 *dst++ = bytestream2_get_byte(gb);
178 }
180 }
181 }
182
183 return dst - dst_start;
184 }
185
188 {
192 int ret,
key, uncompressed;
193
194 if (avpkt->
size <= 13)
196
199 uncompressed = bytestream2_get_le32(gb);
200
201 if (!uncompressed) {
203 if (!
s->uncompressed)
205
210 }
211
214
216
220
223
226 *got_frame = 1;
227
229 }
230
232 {
235
238
243 }
244
246
253
254 return 0;
255 }
256
258 {
260
262 }
263
265 {
267
271
272 return 0;
273 }
274
287 };