FFmpeg
sheervideo.c
Go to the documentation of this file.
1 /*
2  * BitJazz SheerVideo decoder
3  * Copyright (c) 2016 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 
22 #define CACHED_BITSTREAM_READER !ARCH_X86_32
23 #define SHEER_VLC_BITS 12
24 
25 #include "libavutil/intreadwrite.h"
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "get_bits.h"
29 #include "thread.h"
30 #include "sheervideodata.h"
31 
32 typedef struct SheerVideoContext {
33  unsigned format;
34  int alt;
35  VLC vlc[2];
38 
39 static void decode_ca4i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
40 {
41  SheerVideoContext *s = avctx->priv_data;
42  uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
43  int x, y;
44 
45  dst_a = (uint16_t *)p->data[3];
46  dst_y = (uint16_t *)p->data[0];
47  dst_u = (uint16_t *)p->data[1];
48  dst_v = (uint16_t *)p->data[2];
49 
50  for (y = 0; y < avctx->height; y++) {
51  if (get_bits1(gb)) {
52  for (x = 0; x < avctx->width; x++) {
53  dst_a[x] = get_bits(gb, 10);
54  dst_y[x] = get_bits(gb, 10);
55  dst_u[x] = get_bits(gb, 10);
56  dst_v[x] = get_bits(gb, 10);
57  }
58  } else {
59  int pred[4] = { 502, 512, 512, 502 };
60 
61  for (x = 0; x < avctx->width; x++) {
62  int y, u, v, a;
63 
64  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
65  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
66  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
67  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
68 
69  dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
70  dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
71  dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
72  dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
73  }
74  }
75 
76  dst_y += p->linesize[0] / 2;
77  dst_u += p->linesize[1] / 2;
78  dst_v += p->linesize[2] / 2;
79  dst_a += p->linesize[3] / 2;
80  }
81 }
82 
83 static void decode_ca4p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
84 {
85  SheerVideoContext *s = avctx->priv_data;
86  uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
87  int x, y;
88 
89  dst_a = (uint16_t *)p->data[3];
90  dst_y = (uint16_t *)p->data[0];
91  dst_u = (uint16_t *)p->data[1];
92  dst_v = (uint16_t *)p->data[2];
93 
94  if (get_bits1(gb)) {
95  for (x = 0; x < avctx->width; x++) {
96  dst_a[x] = get_bits(gb, 10);
97  dst_y[x] = get_bits(gb, 10);
98  dst_u[x] = get_bits(gb, 10);
99  dst_v[x] = get_bits(gb, 10);
100  }
101  } else {
102  int pred[4] = { 502, 512, 512, 502 };
103 
104  for (x = 0; x < avctx->width; x++) {
105  int y, u, v, a;
106 
107  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
108  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
109  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
110  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
111 
112  dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
113  dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
114  dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
115  dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
116  }
117  }
118 
119  dst_y += p->linesize[0] / 2;
120  dst_u += p->linesize[1] / 2;
121  dst_v += p->linesize[2] / 2;
122  dst_a += p->linesize[3] / 2;
123 
124  for (y = 1; y < avctx->height; y++) {
125  if (get_bits1(gb)) {
126  for (x = 0; x < avctx->width; x++) {
127  dst_a[x] = get_bits(gb, 10);
128  dst_y[x] = get_bits(gb, 10);
129  dst_u[x] = get_bits(gb, 10);
130  dst_v[x] = get_bits(gb, 10);
131  }
132  } else {
133  int pred_TL[4], pred_L[4], pred_T[4];
134  int y, u, v, a;
135 
136  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
137  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
138  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
139  pred_TL[3] = pred_L[3] = dst_a[-p->linesize[3] / 2];
140 
141  for (x = 0; x < avctx->width; x++) {
142  pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
143  pred_T[1] = dst_u[-p->linesize[1] / 2 + x];
144  pred_T[2] = dst_v[-p->linesize[2] / 2 + x];
145  pred_T[3] = dst_a[-p->linesize[3] / 2 + x];
146 
147  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
148  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
149  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
150  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
151 
152  dst_a[x] = pred_L[3] = (a + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0x3ff;
153  dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
154  dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
155  dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
156 
157  pred_TL[0] = pred_T[0];
158  pred_TL[1] = pred_T[1];
159  pred_TL[2] = pred_T[2];
160  pred_TL[3] = pred_T[3];
161  }
162  }
163 
164  dst_y += p->linesize[0] / 2;
165  dst_u += p->linesize[1] / 2;
166  dst_v += p->linesize[2] / 2;
167  dst_a += p->linesize[3] / 2;
168  }
169 }
170 
171 static void decode_ybr10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
172 {
173  SheerVideoContext *s = avctx->priv_data;
174  uint16_t *dst_y, *dst_u, *dst_v;
175  int x, y;
176 
177  dst_y = (uint16_t *)p->data[0];
178  dst_u = (uint16_t *)p->data[1];
179  dst_v = (uint16_t *)p->data[2];
180 
181  for (y = 0; y < avctx->height; y++) {
182  if (get_bits1(gb)) {
183  for (x = 0; x < avctx->width; x++) {
184  dst_y[x] = get_bits(gb, 10);
185  dst_u[x] = get_bits(gb, 10);
186  dst_v[x] = get_bits(gb, 10);
187  }
188  } else {
189  int pred[4] = { 502, 512, 512, 512 };
190 
191  for (x = 0; x < avctx->width; x++) {
192  int y, u, v;
193 
194  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
195  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
196  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
197 
198  dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
199  dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
200  dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
201  }
202  }
203 
204  dst_y += p->linesize[0] / 2;
205  dst_u += p->linesize[1] / 2;
206  dst_v += p->linesize[2] / 2;
207  }
208 }
209 
210 static void decode_ybr10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
211 {
212  SheerVideoContext *s = avctx->priv_data;
213  uint16_t *dst_y, *dst_u, *dst_v;
214  int x, y;
215 
216  dst_y = (uint16_t *)p->data[0];
217  dst_u = (uint16_t *)p->data[1];
218  dst_v = (uint16_t *)p->data[2];
219 
220  if (get_bits1(gb)) {
221  for (x = 0; x < avctx->width; x++) {
222  dst_y[x] = get_bits(gb, 10);
223  dst_u[x] = get_bits(gb, 10);
224  dst_v[x] = get_bits(gb, 10);
225  }
226  } else {
227  int pred[4] = { 502, 512, 512, 512 };
228 
229  for (x = 0; x < avctx->width; x++) {
230  int y, u, v;
231 
232  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
233  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
234  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
235 
236  dst_y[x] = pred[0] = (y + pred[0]) & 0x3ff;
237  dst_u[x] = pred[1] = (u + pred[1]) & 0x3ff;
238  dst_v[x] = pred[2] = (v + pred[2]) & 0x3ff;
239  }
240  }
241 
242  dst_y += p->linesize[0] / 2;
243  dst_u += p->linesize[1] / 2;
244  dst_v += p->linesize[2] / 2;
245 
246  for (y = 1; y < avctx->height; y++) {
247  if (get_bits1(gb)) {
248  for (x = 0; x < avctx->width; x++) {
249  dst_y[x] = get_bits(gb, 10);
250  dst_u[x] = get_bits(gb, 10);
251  dst_v[x] = get_bits(gb, 10);
252  }
253  } else {
254  int pred_TL[4], pred_L[4], pred_T[4];
255  int y, u, v;
256 
257  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
258  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
259  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
260 
261  for (x = 0; x < avctx->width; x++) {
262  pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
263  pred_T[1] = dst_u[-p->linesize[1] / 2 + x];
264  pred_T[2] = dst_v[-p->linesize[2] / 2 + x];
265 
266  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
267  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
268  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
269 
270  dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
271  dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
272  dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
273 
274  pred_TL[0] = pred_T[0];
275  pred_TL[1] = pred_T[1];
276  pred_TL[2] = pred_T[2];
277  }
278  }
279 
280  dst_y += p->linesize[0] / 2;
281  dst_u += p->linesize[1] / 2;
282  dst_v += p->linesize[2] / 2;
283  }
284 }
285 
286 static void decode_yry10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
287 {
288  SheerVideoContext *s = avctx->priv_data;
289  uint16_t *dst_y, *dst_u, *dst_v;
290  int x, y;
291 
292  dst_y = (uint16_t *)p->data[0];
293  dst_u = (uint16_t *)p->data[1];
294  dst_v = (uint16_t *)p->data[2];
295 
296  for (y = 0; y < avctx->height; y++) {
297  if (get_bits1(gb)) {
298  for (x = 0; x < avctx->width; x += 2) {
299  dst_y[x ] = get_bits(gb, 10);
300  dst_u[x / 2] = get_bits(gb, 10);
301  dst_y[x + 1] = get_bits(gb, 10);
302  dst_v[x / 2] = get_bits(gb, 10);
303  }
304  } else {
305  int pred[4] = { 502, 512, 512, 0 };
306 
307  for (x = 0; x < avctx->width; x += 2) {
308  int y1, y2, u, v;
309 
310  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
311  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
312  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
313  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
314 
315  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0x3ff;
316  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0x3ff;
317  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
318  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0x3ff;
319  }
320  }
321 
322  dst_y += p->linesize[0] / 2;
323  dst_u += p->linesize[1] / 2;
324  dst_v += p->linesize[2] / 2;
325  }
326 }
327 
328 static void decode_yry10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
329 {
330  SheerVideoContext *s = avctx->priv_data;
331  uint16_t *dst_y, *dst_u, *dst_v;
332  int x, y;
333 
334  dst_y = (uint16_t *)p->data[0];
335  dst_u = (uint16_t *)p->data[1];
336  dst_v = (uint16_t *)p->data[2];
337 
338  if (get_bits1(gb)) {
339  for (x = 0; x < avctx->width; x += 2) {
340  dst_y[x ] = get_bits(gb, 10);
341  dst_u[x / 2] = get_bits(gb, 10);
342  dst_y[x + 1] = get_bits(gb, 10);
343  dst_v[x / 2] = get_bits(gb, 10);
344  }
345  } else {
346  int pred[4] = { 502, 512, 512, 0 };
347 
348  for (x = 0; x < avctx->width; x += 2) {
349  int y1, y2, u, v;
350 
351  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
352  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
353  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
354  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
355 
356  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0x3ff;
357  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0x3ff;
358  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
359  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0x3ff;
360  }
361  }
362 
363  dst_y += p->linesize[0] / 2;
364  dst_u += p->linesize[1] / 2;
365  dst_v += p->linesize[2] / 2;
366 
367  for (y = 1; y < avctx->height; y++) {
368  if (get_bits1(gb)) {
369  for (x = 0; x < avctx->width; x += 2) {
370  dst_y[x ] = get_bits(gb, 10);
371  dst_u[x / 2] = get_bits(gb, 10);
372  dst_y[x + 1] = get_bits(gb, 10);
373  dst_v[x / 2] = get_bits(gb, 10);
374  }
375  } else {
376  int pred_TL[6], pred_L[6], pred_T[6];
377  int y1, y2, u, v;
378 
379  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
380  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
381  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
382 
383  for (x = 0; x < avctx->width; x += 2) {
384  pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
385  pred_T[3] = dst_y[-p->linesize[0] / 2 + x + 1];
386  pred_T[1] = dst_u[-p->linesize[1] / 2 + x / 2];
387  pred_T[2] = dst_v[-p->linesize[2] / 2 + x / 2];
388 
389  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
390  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
391  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
392  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
393 
394  dst_y[x ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
395  dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0x3ff;
396  dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0x3ff;
397  dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0x3ff;
398 
399  pred_TL[0] = pred_T[3];
400  pred_TL[1] = pred_T[1];
401  pred_TL[2] = pred_T[2];
402  }
403  }
404 
405  dst_y += p->linesize[0] / 2;
406  dst_u += p->linesize[1] / 2;
407  dst_v += p->linesize[2] / 2;
408  }
409 }
410 
411 static void decode_ca2i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
412 {
413  SheerVideoContext *s = avctx->priv_data;
414  uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
415  int x, y;
416 
417  dst_y = (uint16_t *)p->data[0];
418  dst_u = (uint16_t *)p->data[1];
419  dst_v = (uint16_t *)p->data[2];
420  dst_a = (uint16_t *)p->data[3];
421 
422  for (y = 0; y < avctx->height; y++) {
423  if (get_bits1(gb)) {
424  for (x = 0; x < avctx->width; x += 2) {
425  dst_a[x ] = get_bits(gb, 10);
426  dst_y[x ] = get_bits(gb, 10);
427  dst_u[x / 2] = get_bits(gb, 10);
428  dst_a[x + 1] = get_bits(gb, 10);
429  dst_y[x + 1] = get_bits(gb, 10);
430  dst_v[x / 2] = get_bits(gb, 10);
431  }
432  } else {
433  int pred[4] = { 502, 512, 512, 502 };
434 
435  for (x = 0; x < avctx->width; x += 2) {
436  int y1, y2, u, v, a1, a2;
437 
438  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
439  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
440  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
441  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
442  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
443  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
444 
445  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0x3ff;
446  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0x3ff;
447  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
448  dst_a[x ] = pred[3] = (a1 + pred[3]) & 0x3ff;
449  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0x3ff;
450  dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0x3ff;
451  }
452  }
453 
454  dst_y += p->linesize[0] / 2;
455  dst_u += p->linesize[1] / 2;
456  dst_v += p->linesize[2] / 2;
457  dst_a += p->linesize[3] / 2;
458  }
459 }
460 
461 static void decode_ca2p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
462 {
463  SheerVideoContext *s = avctx->priv_data;
464  uint16_t *dst_y, *dst_u, *dst_v, *dst_a;
465  int x, y;
466 
467  dst_y = (uint16_t *)p->data[0];
468  dst_u = (uint16_t *)p->data[1];
469  dst_v = (uint16_t *)p->data[2];
470  dst_a = (uint16_t *)p->data[3];
471 
472  if (get_bits1(gb)) {
473  for (x = 0; x < avctx->width; x += 2) {
474  dst_a[x ] = get_bits(gb, 10);
475  dst_y[x ] = get_bits(gb, 10);
476  dst_u[x / 2] = get_bits(gb, 10);
477  dst_a[x + 1] = get_bits(gb, 10);
478  dst_y[x + 1] = get_bits(gb, 10);
479  dst_v[x / 2] = get_bits(gb, 10);
480  }
481  } else {
482  int pred[4] = { 502, 512, 512, 502 };
483 
484  for (x = 0; x < avctx->width; x += 2) {
485  int y1, y2, u, v, a1, a2;
486 
487  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
488  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
489  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
490  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
491  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
492  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
493 
494  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0x3ff;
495  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0x3ff;
496  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0x3ff;
497  dst_a[x ] = pred[3] = (a1 + pred[3]) & 0x3ff;
498  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0x3ff;
499  dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0x3ff;
500  }
501  }
502 
503  dst_y += p->linesize[0] / 2;
504  dst_u += p->linesize[1] / 2;
505  dst_v += p->linesize[2] / 2;
506  dst_a += p->linesize[3] / 2;
507 
508  for (y = 1; y < avctx->height; y++) {
509  if (get_bits1(gb)) {
510  for (x = 0; x < avctx->width; x += 2) {
511  dst_a[x ] = get_bits(gb, 10);
512  dst_y[x ] = get_bits(gb, 10);
513  dst_u[x / 2] = get_bits(gb, 10);
514  dst_a[x + 1] = get_bits(gb, 10);
515  dst_y[x + 1] = get_bits(gb, 10);
516  dst_v[x / 2] = get_bits(gb, 10);
517  }
518  } else {
519  int pred_TL[6], pred_L[6], pred_T[6];
520  int y1, y2, u, v, a1, a2;
521 
522  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0] / 2];
523  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1] / 2];
524  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2] / 2];
525  pred_TL[4] = pred_L[4] = dst_a[-p->linesize[3] / 2];
526 
527  for (x = 0; x < avctx->width; x += 2) {
528  pred_T[0] = dst_y[-p->linesize[0] / 2 + x];
529  pred_T[3] = dst_y[-p->linesize[0] / 2 + x + 1];
530  pred_T[1] = dst_u[-p->linesize[1] / 2 + x / 2];
531  pred_T[2] = dst_v[-p->linesize[2] / 2 + x / 2];
532  pred_T[4] = dst_a[-p->linesize[3] / 2 + x];
533  pred_T[5] = dst_a[-p->linesize[3] / 2 + x + 1];
534 
535  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
536  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
537  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
538  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
539  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
540  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
541 
542  dst_y[x ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
543  dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0x3ff;
544  dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0x3ff;
545  dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0x3ff;
546  dst_a[x ] = pred_L[4] = (a1 + ((3 * (pred_T[4] + pred_L[4]) - 2 * pred_TL[4]) >> 2)) & 0x3ff;
547  dst_a[x + 1] = pred_L[4] = (a2 + ((3 * (pred_T[5] + pred_L[4]) - 2 * pred_T[4]) >> 2)) & 0x3ff;
548 
549  pred_TL[0] = pred_T[3];
550  pred_TL[1] = pred_T[1];
551  pred_TL[2] = pred_T[2];
552  pred_TL[4] = pred_T[5];
553  }
554  }
555 
556  dst_y += p->linesize[0] / 2;
557  dst_u += p->linesize[1] / 2;
558  dst_v += p->linesize[2] / 2;
559  dst_a += p->linesize[3] / 2;
560  }
561 }
562 
563 static void decode_c82i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
564 {
565  SheerVideoContext *s = avctx->priv_data;
566  uint8_t *dst_y, *dst_u, *dst_v, *dst_a;
567  int x, y;
568 
569  dst_y = p->data[0];
570  dst_u = p->data[1];
571  dst_v = p->data[2];
572  dst_a = p->data[3];
573 
574  for (y = 0; y < avctx->height; y += 1) {
575  if (get_bits1(gb)) {
576  for (x = 0; x < avctx->width; x += 2) {
577  dst_a[x ] = get_bits(gb, 8);
578  dst_y[x ] = get_bits(gb, 8);
579  dst_u[x / 2] = get_bits(gb, 8);
580  dst_a[x + 1] = get_bits(gb, 8);
581  dst_y[x + 1] = get_bits(gb, 8);
582  dst_v[x / 2] = get_bits(gb, 8);
583  }
584  } else {
585  int pred[4] = { 125, -128, -128, 125 };
586 
587  for (x = 0; x < avctx->width; x += 2) {
588  int y1, y2, u, v, a1, a2;
589 
590  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
591  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
592  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
593  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
594  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
595  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
596 
597  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0xff;
598  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
599  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0xff;
600  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0xff;
601  dst_a[x ] = pred[3] = (a1 + pred[3]) & 0xff;
602  dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0xff;
603  }
604  }
605 
606  dst_y += p->linesize[0];
607  dst_u += p->linesize[1];
608  dst_v += p->linesize[2];
609  dst_a += p->linesize[3];
610  }
611 }
612 
613 static void decode_c82p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
614 {
615  SheerVideoContext *s = avctx->priv_data;
616  uint8_t *dst_y, *dst_u, *dst_v, *dst_a;
617  int x, y;
618 
619  dst_y = p->data[0];
620  dst_u = p->data[1];
621  dst_v = p->data[2];
622  dst_a = p->data[3];
623 
624  if (get_bits1(gb)) {
625  for (x = 0; x < avctx->width; x += 2) {
626  dst_a[x ] = get_bits(gb, 8);
627  dst_y[x ] = get_bits(gb, 8);
628  dst_u[x / 2] = get_bits(gb, 8);
629  dst_a[x + 1] = get_bits(gb, 8);
630  dst_y[x + 1] = get_bits(gb, 8);
631  dst_v[x / 2] = get_bits(gb, 8);
632  }
633  } else {
634  int pred[4] = { 125, -128, -128, 125 };
635 
636  for (x = 0; x < avctx->width; x += 2) {
637  int y1, y2, u, v, a1, a2;
638 
639  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
640  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
641  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
642  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
643  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
644  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
645 
646  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0xff;
647  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0xff;
648  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
649  dst_a[x ] = pred[3] = (a1 + pred[3]) & 0xff;
650  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0xff;
651  dst_a[x + 1] = pred[3] = (a2 + pred[3]) & 0xff;
652  }
653  }
654 
655  dst_y += p->linesize[0];
656  dst_u += p->linesize[1];
657  dst_v += p->linesize[2];
658  dst_a += p->linesize[3];
659 
660  for (y = 1; y < avctx->height; y++) {
661  if (get_bits1(gb)) {
662  for (x = 0; x < avctx->width; x += 2) {
663  dst_a[x ] = get_bits(gb, 8);
664  dst_y[x ] = get_bits(gb, 8);
665  dst_u[x / 2] = get_bits(gb, 8);
666  dst_a[x + 1] = get_bits(gb, 8);
667  dst_y[x + 1] = get_bits(gb, 8);
668  dst_v[x / 2] = get_bits(gb, 8);
669  }
670  } else {
671  int pred_TL[6], pred_L[6], pred_T[6];
672  int y1, y2, u, v, a1, a2;
673 
674  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
675  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
676  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
677  pred_TL[4] = pred_L[4] = dst_a[-p->linesize[3]];
678 
679  for (x = 0; x < avctx->width; x += 2) {
680  pred_T[0] = dst_y[-p->linesize[0] + x];
681  pred_T[3] = dst_y[-p->linesize[0] + x + 1];
682  pred_T[1] = dst_u[-p->linesize[1] + x / 2];
683  pred_T[2] = dst_v[-p->linesize[2] + x / 2];
684  pred_T[4] = dst_a[-p->linesize[3] + x];
685  pred_T[5] = dst_a[-p->linesize[3] + x + 1];
686 
687  a1 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
688  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
689  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
690  a2 = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
691  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
692  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
693 
694  dst_y[x ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
695  dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
696  dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
697  dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
698  dst_a[x ] = pred_L[4] = (a1 + ((3 * (pred_T[4] + pred_L[4]) - 2 * pred_TL[4]) >> 2)) & 0xff;
699  dst_a[x + 1] = pred_L[4] = (a2 + ((3 * (pred_T[5] + pred_L[4]) - 2 * pred_T[4]) >> 2)) & 0xff;
700 
701  pred_TL[0] = pred_T[3];
702  pred_TL[1] = pred_T[1];
703  pred_TL[2] = pred_T[2];
704  pred_TL[4] = pred_T[5];
705  }
706  }
707 
708  dst_y += p->linesize[0];
709  dst_u += p->linesize[1];
710  dst_v += p->linesize[2];
711  dst_a += p->linesize[3];
712  }
713 }
714 
715 static void decode_ybyr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
716 {
717  SheerVideoContext *s = avctx->priv_data;
718  uint8_t *dst_y, *dst_u, *dst_v;
719  int x, y;
720 
721  dst_y = p->data[0];
722  dst_u = p->data[1];
723  dst_v = p->data[2];
724 
725  if (get_bits1(gb)) {
726  for (x = 0; x < avctx->width; x += 2) {
727  dst_y[x ] = get_bits(gb, 8);
728  dst_u[x / 2] = get_bits(gb, 8) + 128;
729  dst_y[x + 1] = get_bits(gb, 8);
730  dst_v[x / 2] = get_bits(gb, 8) + 128;
731  }
732  } else {
733  int pred[4] = { -128, 128, 128, 0 };
734 
735  for (x = 0; x < avctx->width; x += 2) {
736  int y1, y2, u, v;
737 
738  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
739  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
740  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
741  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
742 
743  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0xff;
744  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0xff;
745  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
746  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0xff;
747  }
748  }
749 
750  dst_y += p->linesize[0];
751  dst_u += p->linesize[1];
752  dst_v += p->linesize[2];
753 
754  for (y = 1; y < avctx->height; y++) {
755  if (get_bits1(gb)) {
756  for (x = 0; x < avctx->width; x += 2) {
757  dst_y[x ] = get_bits(gb, 8);
758  dst_u[x / 2] = get_bits(gb, 8) + 128;
759  dst_y[x + 1] = get_bits(gb, 8);
760  dst_v[x / 2] = get_bits(gb, 8) + 128;
761  }
762  } else {
763  int pred_TL[4], pred_L[4], pred_T[4];
764  int y1, y2, u, v;
765 
766  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
767  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
768  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
769 
770  for (x = 0; x < avctx->width; x += 2) {
771  pred_T[0] = dst_y[-p->linesize[0] + x];
772  pred_T[3] = dst_y[-p->linesize[0] + x + 1];
773  pred_T[1] = dst_u[-p->linesize[1] + x / 2];
774  pred_T[2] = dst_v[-p->linesize[2] + x / 2];
775 
776  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
777  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
778  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
779  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
780 
781  dst_y[x ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
782  dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
783  dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
784  dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
785 
786  pred_TL[0] = pred_T[3];
787  pred_TL[1] = pred_T[1];
788  pred_TL[2] = pred_T[2];
789  }
790  }
791 
792  dst_y += p->linesize[0];
793  dst_u += p->linesize[1];
794  dst_v += p->linesize[2];
795  }
796 }
797 
798 static void decode_byryi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
799 {
800  SheerVideoContext *s = avctx->priv_data;
801  uint8_t *dst_y, *dst_u, *dst_v;
802  int x, y;
803 
804  dst_y = p->data[0];
805  dst_u = p->data[1];
806  dst_v = p->data[2];
807 
808  if (get_bits1(gb)) {
809  for (x = 0; x < avctx->width; x += 2) {
810  dst_y[x ] = get_bits(gb, 8);
811  dst_u[x / 2] = get_bits(gb, 8);
812  dst_y[x + 1] = get_bits(gb, 8);
813  dst_v[x / 2] = get_bits(gb, 8);
814  }
815  } else {
816  int pred[4] = { 125, -128, -128, 0 };
817 
818  for (x = 0; x < avctx->width; x += 2) {
819  int y1, y2, u, v;
820 
821  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
822  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
823  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
824  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
825 
826  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0xff;
827  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0xff;
828  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
829  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0xff;
830  }
831  }
832 
833  dst_y += p->linesize[0];
834  dst_u += p->linesize[1];
835  dst_v += p->linesize[2];
836 
837  for (y = 1; y < avctx->height; y++) {
838  if (get_bits1(gb)) {
839  for (x = 0; x < avctx->width; x += 2) {
840  dst_y[x ] = get_bits(gb, 8);
841  dst_u[x / 2] = get_bits(gb, 8);
842  dst_y[x + 1] = get_bits(gb, 8);
843  dst_v[x / 2] = get_bits(gb, 8);
844  }
845  } else {
846  int pred_L[4];
847  int y1, y2, u, v;
848 
849  pred_L[0] = dst_y[-p->linesize[0]];
850  pred_L[1] = dst_u[-p->linesize[1]];
851  pred_L[2] = dst_v[-p->linesize[2]];
852 
853  for (x = 0; x < avctx->width; x += 2) {
854  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
855  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
856  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
857  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
858 
859  dst_y[x ] = pred_L[0] = (y1 + pred_L[0]) & 0xff;
860  dst_u[x / 2] = pred_L[1] = (u + pred_L[1]) & 0xff;
861  dst_y[x + 1] = pred_L[0] = (y2 + pred_L[0]) & 0xff;
862  dst_v[x / 2] = pred_L[2] = (v + pred_L[2]) & 0xff;
863  }
864  }
865 
866  dst_y += p->linesize[0];
867  dst_u += p->linesize[1];
868  dst_v += p->linesize[2];
869  }
870 }
871 
872 static void decode_byry(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
873 {
874  SheerVideoContext *s = avctx->priv_data;
875  uint8_t *dst_y, *dst_u, *dst_v;
876  int x, y;
877 
878  dst_y = p->data[0];
879  dst_u = p->data[1];
880  dst_v = p->data[2];
881 
882  if (get_bits1(gb)) {
883  for (x = 0; x < avctx->width; x += 2) {
884  dst_y[x ] = get_bits(gb, 8);
885  dst_u[x / 2] = get_bits(gb, 8);
886  dst_y[x + 1] = get_bits(gb, 8);
887  dst_v[x / 2] = get_bits(gb, 8);
888  }
889  } else {
890  int pred[4] = { 125, -128, -128, 0 };
891 
892  for (x = 0; x < avctx->width; x += 2) {
893  int y1, y2, u, v;
894 
895  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
896  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
897  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
898  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
899 
900  dst_y[x ] = pred[0] = (y1 + pred[0]) & 0xff;
901  dst_u[x / 2] = pred[1] = (u + pred[1]) & 0xff;
902  dst_y[x + 1] = pred[0] = (y2 + pred[0]) & 0xff;
903  dst_v[x / 2] = pred[2] = (v + pred[2]) & 0xff;
904  }
905  }
906 
907  dst_y += p->linesize[0];
908  dst_u += p->linesize[1];
909  dst_v += p->linesize[2];
910 
911  for (y = 1; y < avctx->height; y++) {
912  if (get_bits1(gb)) {
913  for (x = 0; x < avctx->width; x += 2) {
914  dst_y[x ] = get_bits(gb, 8);
915  dst_u[x / 2] = get_bits(gb, 8);
916  dst_y[x + 1] = get_bits(gb, 8);
917  dst_v[x / 2] = get_bits(gb, 8);
918  }
919  } else {
920  int pred_TL[4], pred_L[4], pred_T[4];
921  int y1, y2, u, v;
922 
923  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
924  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
925  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
926 
927  for (x = 0; x < avctx->width; x += 2) {
928  pred_T[0] = dst_y[-p->linesize[0] + x];
929  pred_T[3] = dst_y[-p->linesize[0] + x + 1];
930  pred_T[1] = dst_u[-p->linesize[1] + x / 2];
931  pred_T[2] = dst_v[-p->linesize[2] + x / 2];
932 
933  y1 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
934  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
935  y2 = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
936  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
937 
938  dst_y[x ] = pred_L[0] = (y1 + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
939  dst_u[x / 2] = pred_L[1] = (u + (((pred_L[1] - pred_TL[1]) >> 1) + pred_T[1])) & 0xff;
940  dst_y[x + 1] = pred_L[0] = (y2 + ((3 * (pred_T[3] + pred_L[0]) - 2 * pred_T[0]) >> 2)) & 0xff;
941  dst_v[x / 2] = pred_L[2] = (v + (((pred_L[2] - pred_TL[2]) >> 1) + pred_T[2])) & 0xff;
942 
943  pred_TL[0] = pred_T[3];
944  pred_TL[1] = pred_T[1];
945  pred_TL[2] = pred_T[2];
946  }
947  }
948 
949  dst_y += p->linesize[0];
950  dst_u += p->linesize[1];
951  dst_v += p->linesize[2];
952  }
953 }
954 
955 static void decode_ybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
956 {
957  SheerVideoContext *s = avctx->priv_data;
958  uint8_t *dst_y, *dst_u, *dst_v;
959  int x, y;
960 
961  dst_y = p->data[0];
962  dst_u = p->data[1];
963  dst_v = p->data[2];
964 
965  if (get_bits1(gb)) {
966  for (x = 0; x < avctx->width; x++) {
967  dst_y[x] = get_bits(gb, 8);
968  dst_u[x] = get_bits(gb, 8);
969  dst_v[x] = get_bits(gb, 8);
970  }
971  } else {
972  int pred[4] = { s->alt ? 125 : -146, -128, -128, -128 };
973 
974  for (x = 0; x < avctx->width; x++) {
975  int y, u, v;
976 
977  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
978  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
979  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
980 
981  dst_y[x] = pred[0] = (y + pred[0]) & 0xff;
982  dst_u[x] = pred[1] = (u + pred[1]) & 0xff;
983  dst_v[x] = pred[2] = (v + pred[2]) & 0xff;
984  }
985  }
986 
987  dst_y += p->linesize[0];
988  dst_u += p->linesize[1];
989  dst_v += p->linesize[2];
990 
991  for (y = 1; y < avctx->height; y++) {
992  if (get_bits1(gb)) {
993  for (x = 0; x < avctx->width; x++) {
994  dst_y[x] = get_bits(gb, 8);
995  dst_u[x] = get_bits(gb, 8);
996  dst_v[x] = get_bits(gb, 8);
997  }
998  } else {
999  int pred_L[4];
1000  int y, u, v;
1001 
1002  pred_L[0] = dst_y[-p->linesize[0]];
1003  pred_L[1] = dst_u[-p->linesize[1]];
1004  pred_L[2] = dst_v[-p->linesize[2]];
1005 
1006  for (x = 0; x < avctx->width; x++) {
1007  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1008  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1009  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1010 
1011  dst_y[x] = pred_L[0] = (y + pred_L[0]) & 0xff;
1012  dst_u[x] = pred_L[1] = (u + pred_L[1]) & 0xff;
1013  dst_v[x] = pred_L[2] = (v + pred_L[2]) & 0xff;
1014  }
1015  }
1016 
1017  dst_y += p->linesize[0];
1018  dst_u += p->linesize[1];
1019  dst_v += p->linesize[2];
1020  }
1021 }
1022 
1023 static void decode_ybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1024 {
1025  SheerVideoContext *s = avctx->priv_data;
1026  uint8_t *dst_y, *dst_u, *dst_v;
1027  int x, y;
1028 
1029  dst_y = p->data[0];
1030  dst_u = p->data[1];
1031  dst_v = p->data[2];
1032 
1033  if (get_bits1(gb)) {
1034  for (x = 0; x < avctx->width; x++) {
1035  dst_y[x] = get_bits(gb, 8);
1036  dst_u[x] = get_bits(gb, 8);
1037  dst_v[x] = get_bits(gb, 8);
1038  }
1039  } else {
1040  int pred[4] = { s->alt ? 125 : -146, -128, -128, -128 };
1041 
1042  for (x = 0; x < avctx->width; x++) {
1043  int y, u, v;
1044 
1045  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1046  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1047  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1048 
1049  dst_y[x] = pred[0] = (y + pred[0]) & 0xff;
1050  dst_u[x] = pred[1] = (u + pred[1]) & 0xff;
1051  dst_v[x] = pred[2] = (v + pred[2]) & 0xff;
1052  }
1053  }
1054 
1055  dst_y += p->linesize[0];
1056  dst_u += p->linesize[1];
1057  dst_v += p->linesize[2];
1058 
1059  for (y = 1; y < avctx->height; y++) {
1060  if (get_bits1(gb)) {
1061  for (x = 0; x < avctx->width; x++) {
1062  dst_y[x] = get_bits(gb, 8);
1063  dst_u[x] = get_bits(gb, 8);
1064  dst_v[x] = get_bits(gb, 8);
1065  }
1066  } else {
1067  int pred_TL[4], pred_L[4], pred_T[4];
1068  int y, u, v;
1069 
1070  pred_TL[0] = pred_L[0] = dst_y[-p->linesize[0]];
1071  pred_TL[1] = pred_L[1] = dst_u[-p->linesize[1]];
1072  pred_TL[2] = pred_L[2] = dst_v[-p->linesize[2]];
1073 
1074  for (x = 0; x < avctx->width; x++) {
1075  pred_T[0] = dst_y[-p->linesize[0] + x];
1076  pred_T[1] = dst_u[-p->linesize[1] + x];
1077  pred_T[2] = dst_v[-p->linesize[2] + x];
1078 
1079  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1080  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1081  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1082 
1083  dst_y[x] = pred_L[0] = (y + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
1084  dst_u[x] = pred_L[1] = (u + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
1085  dst_v[x] = pred_L[2] = (v + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
1086 
1087  pred_TL[0] = pred_T[0];
1088  pred_TL[1] = pred_T[1];
1089  pred_TL[2] = pred_T[2];
1090  }
1091  }
1092 
1093  dst_y += p->linesize[0];
1094  dst_u += p->linesize[1];
1095  dst_v += p->linesize[2];
1096  }
1097 }
1098 
1099 static void decode_aybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1100 {
1101  SheerVideoContext *s = avctx->priv_data;
1102  uint8_t *dst_a, *dst_y, *dst_u, *dst_v;
1103  int x, y;
1104 
1105  dst_a = p->data[3];
1106  dst_y = p->data[0];
1107  dst_u = p->data[1];
1108  dst_v = p->data[2];
1109 
1110  if (get_bits1(gb)) {
1111  for (x = 0; x < avctx->width; x++) {
1112  dst_a[x] = get_bits(gb, 8);
1113  dst_y[x] = get_bits(gb, 8);
1114  dst_u[x] = get_bits(gb, 8);
1115  dst_v[x] = get_bits(gb, 8);
1116  }
1117  } else {
1118  int pred[4] = { 125, s->alt ? 125 : -146, -128, -128 };
1119 
1120  for (x = 0; x < avctx->width; x++) {
1121  int a, y, u, v;
1122 
1123  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1124  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1125  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1126  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1127 
1128  dst_a[x] = pred[0] = (a + pred[0]) & 0xff;
1129  dst_y[x] = pred[1] = (y + pred[1]) & 0xff;
1130  dst_u[x] = pred[2] = (u + pred[2]) & 0xff;
1131  dst_v[x] = pred[3] = (v + pred[3]) & 0xff;
1132  }
1133  }
1134 
1135  dst_a += p->linesize[3];
1136  dst_y += p->linesize[0];
1137  dst_u += p->linesize[1];
1138  dst_v += p->linesize[2];
1139 
1140  for (y = 1; y < avctx->height; y++) {
1141  if (get_bits1(gb)) {
1142  for (x = 0; x < avctx->width; x++) {
1143  dst_a[x] = get_bits(gb, 8);
1144  dst_y[x] = get_bits(gb, 8);
1145  dst_u[x] = get_bits(gb, 8);
1146  dst_v[x] = get_bits(gb, 8);
1147  }
1148  } else {
1149  int pred_L[4];
1150  int a, y, u, v;
1151 
1152  pred_L[0] = dst_a[-p->linesize[3]];
1153  pred_L[1] = dst_y[-p->linesize[0]];
1154  pred_L[2] = dst_u[-p->linesize[1]];
1155  pred_L[3] = dst_v[-p->linesize[2]];
1156 
1157  for (x = 0; x < avctx->width; x++) {
1158  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1159  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1160  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1161  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1162 
1163  dst_a[x] = pred_L[0] = (a + pred_L[0]) & 0xff;
1164  dst_y[x] = pred_L[1] = (y + pred_L[1]) & 0xff;
1165  dst_u[x] = pred_L[2] = (u + pred_L[2]) & 0xff;
1166  dst_v[x] = pred_L[3] = (v + pred_L[3]) & 0xff;
1167  }
1168  }
1169 
1170  dst_a += p->linesize[3];
1171  dst_y += p->linesize[0];
1172  dst_u += p->linesize[1];
1173  dst_v += p->linesize[2];
1174  }
1175 }
1176 
1177 static void decode_aybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1178 {
1179  SheerVideoContext *s = avctx->priv_data;
1180  uint8_t *dst_a, *dst_y, *dst_u, *dst_v;
1181  int x, y;
1182 
1183  dst_a = p->data[3];
1184  dst_y = p->data[0];
1185  dst_u = p->data[1];
1186  dst_v = p->data[2];
1187 
1188  if (get_bits1(gb)) {
1189  for (x = 0; x < avctx->width; x++) {
1190  dst_a[x] = get_bits(gb, 8);
1191  dst_y[x] = get_bits(gb, 8);
1192  dst_u[x] = get_bits(gb, 8);
1193  dst_v[x] = get_bits(gb, 8);
1194  }
1195  } else {
1196  int pred[4] = { 125, s->alt ? 125 : -146, -128, -128 };
1197 
1198  for (x = 0; x < avctx->width; x++) {
1199  int a, y, u, v;
1200 
1201  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1202  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1203  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1204  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1205 
1206  dst_a[x] = pred[0] = (a + pred[0]) & 0xff;
1207  dst_y[x] = pred[1] = (y + pred[1]) & 0xff;
1208  dst_u[x] = pred[2] = (u + pred[2]) & 0xff;
1209  dst_v[x] = pred[3] = (v + pred[3]) & 0xff;
1210  }
1211  }
1212 
1213  dst_a += p->linesize[3];
1214  dst_y += p->linesize[0];
1215  dst_u += p->linesize[1];
1216  dst_v += p->linesize[2];
1217 
1218  for (y = 1; y < avctx->height; y++) {
1219  if (get_bits1(gb)) {
1220  for (x = 0; x < avctx->width; x++) {
1221  dst_a[x] = get_bits(gb, 8);
1222  dst_y[x] = get_bits(gb, 8);
1223  dst_u[x] = get_bits(gb, 8);
1224  dst_v[x] = get_bits(gb, 8);
1225  }
1226  } else {
1227  int pred_TL[4], pred_L[4], pred_T[4];
1228  int a, y, u, v;
1229 
1230  pred_TL[0] = pred_L[0] = dst_a[-p->linesize[3]];
1231  pred_TL[1] = pred_L[1] = dst_y[-p->linesize[0]];
1232  pred_TL[2] = pred_L[2] = dst_u[-p->linesize[1]];
1233  pred_TL[3] = pred_L[3] = dst_v[-p->linesize[2]];
1234 
1235  for (x = 0; x < avctx->width; x++) {
1236  pred_T[0] = dst_a[-p->linesize[3] + x];
1237  pred_T[1] = dst_y[-p->linesize[0] + x];
1238  pred_T[2] = dst_u[-p->linesize[1] + x];
1239  pred_T[3] = dst_v[-p->linesize[2] + x];
1240 
1241  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1242  y = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1243  u = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1244  v = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1245 
1246  dst_a[x] = pred_L[0] = (a + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
1247  dst_y[x] = pred_L[1] = (y + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
1248  dst_u[x] = pred_L[2] = (u + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
1249  dst_v[x] = pred_L[3] = (v + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0xff;
1250 
1251  pred_TL[0] = pred_T[0];
1252  pred_TL[1] = pred_T[1];
1253  pred_TL[2] = pred_T[2];
1254  pred_TL[3] = pred_T[3];
1255  }
1256  }
1257 
1258  dst_a += p->linesize[3];
1259  dst_y += p->linesize[0];
1260  dst_u += p->linesize[1];
1261  dst_v += p->linesize[2];
1262  }
1263 }
1264 
1265 static void decode_argxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1266 {
1267  SheerVideoContext *s = avctx->priv_data;
1268  uint16_t *dst_r, *dst_g, *dst_b, *dst_a;
1269  int x, y;
1270 
1271  dst_r = (uint16_t *)p->data[2];
1272  dst_g = (uint16_t *)p->data[0];
1273  dst_b = (uint16_t *)p->data[1];
1274  dst_a = (uint16_t *)p->data[3];
1275 
1276  for (y = 0; y < avctx->height; y++) {
1277  if (get_bits1(gb)) {
1278  for (x = 0; x < avctx->width; x++) {
1279  dst_a[x] = get_bits(gb, 10);
1280  dst_r[x] = get_bits(gb, 10);
1281  dst_g[x] = get_bits(gb, 10);
1282  dst_b[x] = get_bits(gb, 10);
1283  }
1284  } else {
1285  int pred[4] = { 512, 512, 512, 512 };
1286 
1287  for (x = 0; x < avctx->width; x++) {
1288  int r, g, b, a;
1289 
1290  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1291  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1292  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1293  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1294 
1295  dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
1296  dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
1297  dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
1298  dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
1299  }
1300  }
1301 
1302  dst_r += p->linesize[2] / 2;
1303  dst_g += p->linesize[0] / 2;
1304  dst_b += p->linesize[1] / 2;
1305  dst_a += p->linesize[3] / 2;
1306  }
1307 }
1308 
1309 static void decode_argx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1310 {
1311  SheerVideoContext *s = avctx->priv_data;
1312  uint16_t *dst_r, *dst_g, *dst_b, *dst_a;
1313  int x, y;
1314 
1315  dst_r = (uint16_t *)p->data[2];
1316  dst_g = (uint16_t *)p->data[0];
1317  dst_b = (uint16_t *)p->data[1];
1318  dst_a = (uint16_t *)p->data[3];
1319 
1320  if (get_bits1(gb)) {
1321  for (x = 0; x < avctx->width; x++) {
1322  dst_a[x] = get_bits(gb, 10);
1323  dst_r[x] = get_bits(gb, 10);
1324  dst_g[x] = get_bits(gb, 10);
1325  dst_b[x] = get_bits(gb, 10);
1326  }
1327  } else {
1328  int pred[4] = { 512, 512, 512, 512 };
1329 
1330  for (x = 0; x < avctx->width; x++) {
1331  int r, g, b, a;
1332 
1333  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1334  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1335  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1336  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1337 
1338  dst_a[x] = pred[3] = (a + pred[3]) & 0x3ff;
1339  dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
1340  dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
1341  dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
1342  }
1343  }
1344 
1345  dst_r += p->linesize[2] / 2;
1346  dst_g += p->linesize[0] / 2;
1347  dst_b += p->linesize[1] / 2;
1348  dst_a += p->linesize[3] / 2;
1349 
1350  for (y = 1; y < avctx->height; y++) {
1351  if (get_bits1(gb)) {
1352  for (x = 0; x < avctx->width; x++) {
1353  dst_a[x] = get_bits(gb, 10);
1354  dst_r[x] = get_bits(gb, 10);
1355  dst_g[x] = get_bits(gb, 10);
1356  dst_b[x] = get_bits(gb, 10);
1357  }
1358  } else {
1359  int pred_TL[4], pred_L[4], pred_T[4];
1360  int r, g, b, a;
1361 
1362  pred_TL[0] = pred_L[0] = dst_r[-p->linesize[2] / 2];
1363  pred_TL[1] = pred_L[1] = dst_g[-p->linesize[0] / 2];
1364  pred_TL[2] = pred_L[2] = dst_b[-p->linesize[1] / 2];
1365  pred_TL[3] = pred_L[3] = dst_a[-p->linesize[3] / 2];
1366 
1367  for (x = 0; x < avctx->width; x++) {
1368  pred_T[0] = dst_r[-p->linesize[2] / 2 + x];
1369  pred_T[1] = dst_g[-p->linesize[0] / 2 + x];
1370  pred_T[2] = dst_b[-p->linesize[1] / 2 + x];
1371  pred_T[3] = dst_a[-p->linesize[3] / 2 + x];
1372 
1373  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1374  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1375  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1376  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1377 
1378  dst_a[x] = pred_L[3] = (a + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0x3ff;
1379  dst_r[x] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
1380  dst_g[x] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
1381  dst_b[x] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
1382 
1383  pred_TL[0] = pred_T[0];
1384  pred_TL[1] = pred_T[1];
1385  pred_TL[2] = pred_T[2];
1386  pred_TL[3] = pred_T[3];
1387  }
1388  }
1389 
1390  dst_r += p->linesize[2] / 2;
1391  dst_g += p->linesize[0] / 2;
1392  dst_b += p->linesize[1] / 2;
1393  dst_a += p->linesize[3] / 2;
1394  }
1395 }
1396 
1397 static void decode_rgbxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1398 {
1399  SheerVideoContext *s = avctx->priv_data;
1400  uint16_t *dst_r, *dst_g, *dst_b;
1401  int x, y;
1402 
1403  dst_r = (uint16_t *)p->data[2];
1404  dst_g = (uint16_t *)p->data[0];
1405  dst_b = (uint16_t *)p->data[1];
1406 
1407  for (y = 0; y < avctx->height; y++) {
1408  if (get_bits1(gb)) {
1409  for (x = 0; x < avctx->width; x++) {
1410  dst_r[x] = get_bits(gb, 10);
1411  dst_g[x] = get_bits(gb, 10);
1412  dst_b[x] = get_bits(gb, 10);
1413  }
1414  } else {
1415  int pred[4] = { 512, 512, 512, 0 };
1416 
1417  for (x = 0; x < avctx->width; x++) {
1418  int r, g, b;
1419 
1420  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1421  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1422  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1423 
1424  dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
1425  dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
1426  dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
1427  }
1428  }
1429 
1430  dst_r += p->linesize[2] / 2;
1431  dst_g += p->linesize[0] / 2;
1432  dst_b += p->linesize[1] / 2;
1433  }
1434 }
1435 
1436 static void decode_rgbx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1437 {
1438  SheerVideoContext *s = avctx->priv_data;
1439  uint16_t *dst_r, *dst_g, *dst_b;
1440  int x, y;
1441 
1442  dst_r = (uint16_t *)p->data[2];
1443  dst_g = (uint16_t *)p->data[0];
1444  dst_b = (uint16_t *)p->data[1];
1445 
1446  if (get_bits1(gb)) {
1447  for (x = 0; x < avctx->width; x++) {
1448  dst_r[x] = get_bits(gb, 10);
1449  dst_g[x] = get_bits(gb, 10);
1450  dst_b[x] = get_bits(gb, 10);
1451  }
1452  } else {
1453  int pred[4] = { 512, 512, 512, 0 };
1454 
1455  for (x = 0; x < avctx->width; x++) {
1456  int r, g, b;
1457 
1458  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1459  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1460  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1461 
1462  dst_r[x] = pred[0] = (r + pred[0]) & 0x3ff;
1463  dst_g[x] = pred[1] = (r + g + pred[1]) & 0x3ff;
1464  dst_b[x] = pred[2] = (r + g + b + pred[2]) & 0x3ff;
1465  }
1466  }
1467 
1468  dst_r += p->linesize[2] / 2;
1469  dst_g += p->linesize[0] / 2;
1470  dst_b += p->linesize[1] / 2;
1471 
1472  for (y = 1; y < avctx->height; y++) {
1473  if (get_bits1(gb)) {
1474  for (x = 0; x < avctx->width; x++) {
1475  dst_r[x] = get_bits(gb, 10);
1476  dst_g[x] = get_bits(gb, 10);
1477  dst_b[x] = get_bits(gb, 10);
1478  }
1479  } else {
1480  int pred_TL[4], pred_L[4], pred_T[4];
1481  int r, g, b;
1482 
1483  pred_TL[0] = pred_L[0] = dst_r[-p->linesize[2] / 2];
1484  pred_TL[1] = pred_L[1] = dst_g[-p->linesize[0] / 2];
1485  pred_TL[2] = pred_L[2] = dst_b[-p->linesize[1] / 2];
1486 
1487  for (x = 0; x < avctx->width; x++) {
1488  pred_T[0] = dst_r[-p->linesize[2] / 2 + x];
1489  pred_T[1] = dst_g[-p->linesize[0] / 2 + x];
1490  pred_T[2] = dst_b[-p->linesize[1] / 2 + x];
1491 
1492  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1493  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1494  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1495 
1496  dst_r[x] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0x3ff;
1497  dst_g[x] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0x3ff;
1498  dst_b[x] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0x3ff;
1499 
1500  pred_TL[0] = pred_T[0];
1501  pred_TL[1] = pred_T[1];
1502  pred_TL[2] = pred_T[2];
1503  }
1504  }
1505 
1506  dst_r += p->linesize[2] / 2;
1507  dst_g += p->linesize[0] / 2;
1508  dst_b += p->linesize[1] / 2;
1509  }
1510 }
1511 
1512 static void decode_argbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1513 {
1514  SheerVideoContext *s = avctx->priv_data;
1515  uint8_t *dst;
1516  int x, y;
1517 
1518  dst = p->data[0];
1519  if (get_bits1(gb)) {
1520  for (x = 0; x < avctx->width; x++) {
1521  dst[x * 4 + 0] = get_bits(gb, 8);
1522  dst[x * 4 + 1] = get_bits(gb, 8);
1523  dst[x * 4 + 2] = get_bits(gb, 8);
1524  dst[x * 4 + 3] = get_bits(gb, 8);
1525  }
1526  } else {
1527  int pred[4] = { -128, -128, -128, -128 };
1528 
1529  for (x = 0; x < avctx->width; x++) {
1530  int a, r, g, b;
1531 
1532  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1533  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1534  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1535  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1536 
1537  dst[4 * x + 0] = pred[0] = (a + pred[0]) & 0xff;
1538  dst[4 * x + 1] = pred[1] = (r + pred[1]) & 0xff;
1539  dst[4 * x + 2] = pred[2] = (r + g + pred[2]) & 0xff;
1540  dst[4 * x + 3] = pred[3] = (r + g + b + pred[3]) & 0xff;
1541  }
1542  }
1543 
1544  dst += p->linesize[0];
1545  for (y = 1; y < avctx->height; y++) {
1546  if (get_bits1(gb)) {
1547  for (x = 0; x < avctx->width; x++) {
1548  dst[x * 4 + 0] = get_bits(gb, 8);
1549  dst[x * 4 + 1] = get_bits(gb, 8);
1550  dst[x * 4 + 2] = get_bits(gb, 8);
1551  dst[x * 4 + 3] = get_bits(gb, 8);
1552  }
1553  } else {
1554  int pred_L[4];
1555  int a, r, g, b;
1556 
1557  pred_L[0] = dst[-p->linesize[0] + 0];
1558  pred_L[1] = dst[-p->linesize[0] + 1];
1559  pred_L[2] = dst[-p->linesize[0] + 2];
1560  pred_L[3] = dst[-p->linesize[0] + 3];
1561 
1562  for (x = 0; x < avctx->width; x++) {
1563  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1564  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1565  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1566  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1567 
1568  dst[4 * x + 0] = pred_L[0] = (a + pred_L[0]) & 0xff;
1569  dst[4 * x + 1] = pred_L[1] = (r + pred_L[1]) & 0xff;
1570  dst[4 * x + 2] = pred_L[2] = (r + g + pred_L[2]) & 0xff;
1571  dst[4 * x + 3] = pred_L[3] = (r + g + b + pred_L[3]) & 0xff;
1572  }
1573  }
1574  dst += p->linesize[0];
1575  }
1576 }
1577 
1578 static void decode_argb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1579 {
1580  SheerVideoContext *s = avctx->priv_data;
1581  uint8_t *dst;
1582  int x, y;
1583 
1584  dst = p->data[0];
1585  if (get_bits1(gb)) {
1586  for (x = 0; x < avctx->width; x++) {
1587  dst[x * 4 + 0] = get_bits(gb, 8);
1588  dst[x * 4 + 1] = get_bits(gb, 8);
1589  dst[x * 4 + 2] = get_bits(gb, 8);
1590  dst[x * 4 + 3] = get_bits(gb, 8);
1591  }
1592  } else {
1593  int pred[4] = { -128, -128, -128, -128 };
1594 
1595  for (x = 0; x < avctx->width; x++) {
1596  int a, r, g, b;
1597 
1598  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1599  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1600  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1601  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1602 
1603  dst[4 * x + 0] = pred[0] = (a + pred[0]) & 0xff;
1604  dst[4 * x + 1] = pred[1] = (r + pred[1]) & 0xff;
1605  dst[4 * x + 2] = pred[2] = (r + g + pred[2]) & 0xff;
1606  dst[4 * x + 3] = pred[3] = (r + g + b + pred[3]) & 0xff;
1607  }
1608  }
1609 
1610  dst += p->linesize[0];
1611  for (y = 1; y < avctx->height; y++) {
1612  if (get_bits1(gb)) {
1613  for (x = 0; x < avctx->width; x++) {
1614  dst[x * 4 + 0] = get_bits(gb, 8);
1615  dst[x * 4 + 1] = get_bits(gb, 8);
1616  dst[x * 4 + 2] = get_bits(gb, 8);
1617  dst[x * 4 + 3] = get_bits(gb, 8);
1618  }
1619  } else {
1620  int pred_TL[4], pred_L[4], pred_T[4];
1621  int a, r, g, b;
1622 
1623  pred_TL[0] = pred_L[0] = dst[-p->linesize[0] + 0];
1624  pred_TL[1] = pred_L[1] = dst[-p->linesize[0] + 1];
1625  pred_TL[2] = pred_L[2] = dst[-p->linesize[0] + 2];
1626  pred_TL[3] = pred_L[3] = dst[-p->linesize[0] + 3];
1627 
1628  for (x = 0; x < avctx->width; x++) {
1629  pred_T[0] = dst[-p->linesize[0] + 4 * x + 0];
1630  pred_T[1] = dst[-p->linesize[0] + 4 * x + 1];
1631  pred_T[2] = dst[-p->linesize[0] + 4 * x + 2];
1632  pred_T[3] = dst[-p->linesize[0] + 4 * x + 3];
1633 
1634  a = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1635  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1636  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1637  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1638 
1639  dst[4 * x + 0] = pred_L[0] = (a + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
1640  dst[4 * x + 1] = pred_L[1] = (r + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
1641  dst[4 * x + 2] = pred_L[2] = (r + g + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
1642  dst[4 * x + 3] = pred_L[3] = (r + g + b + ((3 * (pred_T[3] + pred_L[3]) - 2 * pred_TL[3]) >> 2)) & 0xff;
1643 
1644  pred_TL[0] = pred_T[0];
1645  pred_TL[1] = pred_T[1];
1646  pred_TL[2] = pred_T[2];
1647  pred_TL[3] = pred_T[3];
1648  }
1649  }
1650  dst += p->linesize[0];
1651  }
1652 }
1653 
1654 static void decode_rgbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1655 {
1656  SheerVideoContext *s = avctx->priv_data;
1657  uint8_t *dst;
1658  int x, y;
1659 
1660  dst = p->data[0];
1661  if (get_bits1(gb)) {
1662  for (x = 0; x < avctx->width; x++) {
1663  dst[x * 4 + 0] = get_bits(gb, 8);
1664  dst[x * 4 + 1] = get_bits(gb, 8);
1665  dst[x * 4 + 2] = get_bits(gb, 8);
1666  }
1667  } else {
1668  int pred[4] = { -128, -128, -128, -128 };
1669 
1670  for (x = 0; x < avctx->width; x++) {
1671  int r, g, b;
1672 
1673  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1674  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1675  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1676 
1677  dst[4 * x + 0] = pred[0] = (r + pred[0]) & 0xff;
1678  dst[4 * x + 1] = pred[1] = (r + g + pred[1]) & 0xff;
1679  dst[4 * x + 2] = pred[2] = (r + g + b + pred[2]) & 0xff;
1680  }
1681  }
1682 
1683  dst += p->linesize[0];
1684  for (y = 1; y < avctx->height; y++) {
1685  if (get_bits1(gb)) {
1686  for (x = 0; x < avctx->width; x++) {
1687  dst[x * 4 + 0] = get_bits(gb, 8);
1688  dst[x * 4 + 1] = get_bits(gb, 8);
1689  dst[x * 4 + 2] = get_bits(gb, 8);
1690  }
1691  } else {
1692  int pred_L[4];
1693  int r, g, b;
1694 
1695  pred_L[0] = dst[-p->linesize[0] + 0];
1696  pred_L[1] = dst[-p->linesize[0] + 1];
1697  pred_L[2] = dst[-p->linesize[0] + 2];
1698 
1699  for (x = 0; x < avctx->width; x++) {
1700  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1701  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1702  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1703 
1704  dst[4 * x + 0] = pred_L[0] = (r + pred_L[0]) & 0xff;
1705  dst[4 * x + 1] = pred_L[1] = (r + g + pred_L[1]) & 0xff;
1706  dst[4 * x + 2] = pred_L[2] = (r + g + b + pred_L[2]) & 0xff;
1707  }
1708  }
1709  dst += p->linesize[0];
1710  }
1711 }
1712 
1713 static void decode_rgb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
1714 {
1715  SheerVideoContext *s = avctx->priv_data;
1716  uint8_t *dst;
1717  int x, y;
1718 
1719  dst = p->data[0];
1720  if (get_bits1(gb)) {
1721  for (x = 0; x < avctx->width; x++) {
1722  dst[x * 4 + 0] = get_bits(gb, 8);
1723  dst[x * 4 + 1] = get_bits(gb, 8);
1724  dst[x * 4 + 2] = get_bits(gb, 8);
1725  }
1726  } else {
1727  int pred[4] = { -128, -128, -128, -128 };
1728 
1729  for (x = 0; x < avctx->width; x++) {
1730  int r, g, b;
1731 
1732  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1733  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1734  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1735 
1736  dst[4 * x + 0] = pred[0] = (r + pred[0]) & 0xff;
1737  dst[4 * x + 1] = pred[1] = (r + g + pred[1]) & 0xff;
1738  dst[4 * x + 2] = pred[2] = (r + g + b + pred[2]) & 0xff;
1739  }
1740  }
1741 
1742  dst += p->linesize[0];
1743  for (y = 1; y < avctx->height; y++) {
1744  if (get_bits1(gb)) {
1745  for (x = 0; x < avctx->width; x++) {
1746  dst[x * 4 + 0] = get_bits(gb, 8);
1747  dst[x * 4 + 1] = get_bits(gb, 8);
1748  dst[x * 4 + 2] = get_bits(gb, 8);
1749  }
1750  } else {
1751  int pred_TL[4], pred_L[4], pred_T[4];
1752  int r, g, b;
1753 
1754  pred_TL[0] = pred_L[0] = dst[-p->linesize[0] + 0];
1755  pred_TL[1] = pred_L[1] = dst[-p->linesize[0] + 1];
1756  pred_TL[2] = pred_L[2] = dst[-p->linesize[0] + 2];
1757 
1758  for (x = 0; x < avctx->width; x++) {
1759  pred_T[0] = dst[-p->linesize[0] + 4 * x + 0];
1760  pred_T[1] = dst[-p->linesize[0] + 4 * x + 1];
1761  pred_T[2] = dst[-p->linesize[0] + 4 * x + 2];
1762 
1763  r = get_vlc2(gb, s->vlc[0].table, SHEER_VLC_BITS, 2);
1764  g = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1765  b = get_vlc2(gb, s->vlc[1].table, SHEER_VLC_BITS, 2);
1766 
1767  dst[4 * x + 0] = pred_L[0] = (r + ((3 * (pred_T[0] + pred_L[0]) - 2 * pred_TL[0]) >> 2)) & 0xff;
1768  dst[4 * x + 1] = pred_L[1] = (r + g + ((3 * (pred_T[1] + pred_L[1]) - 2 * pred_TL[1]) >> 2)) & 0xff;
1769  dst[4 * x + 2] = pred_L[2] = (r + g + b + ((3 * (pred_T[2] + pred_L[2]) - 2 * pred_TL[2]) >> 2)) & 0xff;
1770 
1771  pred_TL[0] = pred_T[0];
1772  pred_TL[1] = pred_T[1];
1773  pred_TL[2] = pred_T[2];
1774  }
1775  }
1776  dst += p->linesize[0];
1777  }
1778 }
1779 
1780 static av_cold int build_vlc(VLC *vlc, const SheerTable *table)
1781 {
1782  const uint8_t *cur = table->lens;
1783  uint8_t lens[1024];
1784  unsigned count = 0;
1785 
1786  for (int step = 1, len = 1; len > 0; len += step) {
1787  unsigned new_count = count;
1788 
1789  if (len == 16) {
1790  new_count += table->nb_16s;
1791  step = -1;
1792  } else
1793  new_count += *cur++;
1794 
1795  for (; count < new_count; count++)
1796  lens[count] = len;
1797  }
1798 
1799  ff_free_vlc(vlc);
1800  return ff_init_vlc_from_lengths(vlc, SHEER_VLC_BITS, count,
1801  lens, sizeof(*lens), NULL, 0, 0, 0, 0, NULL);
1802 }
1803 
1804 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
1805  int *got_frame, AVPacket *avpkt)
1806 {
1807  SheerVideoContext *s = avctx->priv_data;
1808  const SheerTable *table;
1809  GetBitContext gb;
1810  unsigned format;
1811  int ret;
1812 
1813  if (avpkt->size <= 20)
1814  return AVERROR_INVALIDDATA;
1815 
1816  if (AV_RL32(avpkt->data) != MKTAG('S','h','i','r') &&
1817  AV_RL32(avpkt->data) != MKTAG('Z','w','a','k'))
1818  return AVERROR_INVALIDDATA;
1819 
1820  s->alt = 0;
1821  format = AV_RL32(avpkt->data + 16);
1822  av_log(avctx, AV_LOG_DEBUG, "format: %s\n", av_fourcc2str(format));
1823  switch (format) {
1824  case MKTAG(' ', 'R', 'G', 'B'):
1825  avctx->pix_fmt = AV_PIX_FMT_RGB0;
1826  s->decode_frame = decode_rgb;
1827  table = rgb;
1828  break;
1829  case MKTAG(' ', 'r', 'G', 'B'):
1830  avctx->pix_fmt = AV_PIX_FMT_RGB0;
1831  s->decode_frame = decode_rgbi;
1832  table = rgbi;
1833  break;
1834  case MKTAG('A', 'R', 'G', 'X'):
1835  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
1836  s->decode_frame = decode_argx;
1837  table = rgbx;
1838  break;
1839  case MKTAG('A', 'r', 'G', 'X'):
1840  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
1841  s->decode_frame = decode_argxi;
1842  table = rgbxi;
1843  break;
1844  case MKTAG('R', 'G', 'B', 'X'):
1845  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
1846  s->decode_frame = decode_rgbx;
1847  table = rgbx;
1848  break;
1849  case MKTAG('r', 'G', 'B', 'X'):
1850  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
1851  s->decode_frame = decode_rgbxi;
1852  table = rgbxi;
1853  break;
1854  case MKTAG('A', 'R', 'G', 'B'):
1855  avctx->pix_fmt = AV_PIX_FMT_ARGB;
1856  s->decode_frame = decode_argb;
1857  table = rgb;
1858  break;
1859  case MKTAG('A', 'r', 'G', 'B'):
1860  avctx->pix_fmt = AV_PIX_FMT_ARGB;
1861  s->decode_frame = decode_argbi;
1862  table = rgbi;
1863  break;
1864  case MKTAG('A', 'Y', 'B', 'R'):
1865  s->alt = 1;
1866  case MKTAG('A', 'Y', 'b', 'R'):
1867  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
1868  s->decode_frame = decode_aybr;
1869  table = ybr;
1870  break;
1871  case MKTAG('A', 'y', 'B', 'R'):
1872  s->alt = 1;
1873  case MKTAG('A', 'y', 'b', 'R'):
1874  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
1875  s->decode_frame = decode_aybri;
1876  table = ybri;
1877  break;
1878  case MKTAG(' ', 'Y', 'B', 'R'):
1879  s->alt = 1;
1880  case MKTAG(' ', 'Y', 'b', 'R'):
1881  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1882  s->decode_frame = decode_ybr;
1883  table = ybr;
1884  break;
1885  case MKTAG(' ', 'y', 'B', 'R'):
1886  s->alt = 1;
1887  case MKTAG(' ', 'y', 'b', 'R'):
1888  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1889  s->decode_frame = decode_ybri;
1890  table = ybri;
1891  break;
1892  case MKTAG('Y', 'B', 'R', 0x0a):
1893  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
1894  s->decode_frame = decode_ybr10;
1895  table = ybr10;
1896  break;
1897  case MKTAG('y', 'B', 'R', 0x0a):
1898  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
1899  s->decode_frame = decode_ybr10i;
1900  table = ybr10i;
1901  break;
1902  case MKTAG('C', 'A', '4', 'p'):
1903  avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
1904  s->decode_frame = decode_ca4p;
1905  table = ybr10;
1906  break;
1907  case MKTAG('C', 'A', '4', 'i'):
1908  avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
1909  s->decode_frame = decode_ca4i;
1910  table = ybr10i;
1911  break;
1912  case MKTAG('B', 'Y', 'R', 'Y'):
1913  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1914  s->decode_frame = decode_byry;
1915  table = byry;
1916  break;
1917  case MKTAG('B', 'Y', 'R', 'y'):
1918  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1919  s->decode_frame = decode_byryi;
1920  table = byryi;
1921  break;
1922  case MKTAG('Y', 'b', 'Y', 'r'):
1923  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1924  s->decode_frame = decode_ybyr;
1925  table = ybyr;
1926  break;
1927  case MKTAG('C', '8', '2', 'p'):
1928  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
1929  s->decode_frame = decode_c82p;
1930  table = byry;
1931  break;
1932  case MKTAG('C', '8', '2', 'i'):
1933  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
1934  s->decode_frame = decode_c82i;
1935  table = byryi;
1936  break;
1937  case MKTAG(0xa2, 'Y', 'R', 'Y'):
1938  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
1939  s->decode_frame = decode_yry10;
1940  table = yry10;
1941  break;
1942  case MKTAG(0xa2, 'Y', 'R', 'y'):
1943  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
1944  s->decode_frame = decode_yry10i;
1945  table = yry10i;
1946  break;
1947  case MKTAG('C', 'A', '2', 'p'):
1948  avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
1949  s->decode_frame = decode_ca2p;
1950  table = yry10;
1951  break;
1952  case MKTAG('C', 'A', '2', 'i'):
1953  avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
1954  s->decode_frame = decode_ca2i;
1955  table = yry10i;
1956  break;
1957  default:
1958  avpriv_request_sample(avctx, "unsupported format: 0x%X", format);
1959  return AVERROR_PATCHWELCOME;
1960  }
1961 
1962  if (s->format != format) {
1963  if ((ret = build_vlc(&s->vlc[0], &table[0])) < 0 ||
1964  (ret = build_vlc(&s->vlc[1], &table[1])) < 0) {
1965  s->format = 0;
1966  return ret;
1967  }
1968  s->format = format;
1969  }
1970  if (avpkt->size < 20 + avctx->width * avctx->height / 16) {
1971  av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
1972  return AVERROR_INVALIDDATA;
1973  }
1974 
1976  p->key_frame = 1;
1977 
1978  if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
1979  return ret;
1980 
1981  if ((ret = init_get_bits8(&gb, avpkt->data + 20, avpkt->size - 20)) < 0)
1982  return ret;
1983 
1984  s->decode_frame(avctx, p, &gb);
1985 
1986  *got_frame = 1;
1987 
1988  return avpkt->size;
1989 }
1990 
1992 {
1993  SheerVideoContext *s = avctx->priv_data;
1994 
1995  ff_free_vlc(&s->vlc[0]);
1996  ff_free_vlc(&s->vlc[1]);
1997 
1998  return 0;
1999 }
2000 
2002  .p.name = "sheervideo",
2003  CODEC_LONG_NAME("BitJazz SheerVideo"),
2004  .p.type = AVMEDIA_TYPE_VIDEO,
2005  .p.id = AV_CODEC_ID_SHEERVIDEO,
2006  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2007  .priv_data_size = sizeof(SheerVideoContext),
2008  .close = decode_end,
2010 };
decode_byry
static void decode_byry(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:872
decode_ca4i
static void decode_ca4i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:39
decode_argb
static void decode_argb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1578
decode_ybr
static void decode_ybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1023
build_vlc
static av_cold int build_vlc(VLC *vlc, const SheerTable *table)
Definition: sheervideo.c:1780
r
const char * r
Definition: vf_curves.c:126
decode_ybri
static void decode_ybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:955
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
SheerVideoContext
Definition: sheervideo.c:32
rgbxi
static const SheerTable rgbxi[2]
Definition: sheervideodata.h:164
AVPacket::data
uint8_t * data
Definition: packet.h:374
b
#define b
Definition: input.c:41
table
static const uint16_t table[]
Definition: prosumer.c:205
ybr
static const SheerTable ybr[2]
Definition: sheervideodata.h:54
FFCodec
Definition: codec_internal.h:127
decode_c82p
static void decode_c82p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:613
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:497
thread.h
decode_ca4p
static void decode_ca4p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:83
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
ff_sheervideo_decoder
const FFCodec ff_sheervideo_decoder
Definition: sheervideo.c:2001
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:325
AV_CODEC_ID_SHEERVIDEO
@ AV_CODEC_ID_SHEERVIDEO
Definition: codec_id.h:274
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
decode_yry10
static void decode_yry10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:328
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:475
GetBitContext
Definition: get_bits.h:107
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames. The frames must then be freed with ff_thread_release_buffer(). Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
decode_rgb
static void decode_rgb(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1713
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:422
SHEER_VLC_BITS
#define SHEER_VLC_BITS
Definition: sheervideo.c:23
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:462
a1
#define a1
Definition: regdef.h:47
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ybyr
static const SheerTable ybyr[2]
Definition: sheervideodata.h:65
sheervideodata.h
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:524
rgbx
static const SheerTable rgbx[2]
Definition: sheervideodata.h:109
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:479
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
SheerVideoContext::alt
int alt
Definition: sheervideo.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:256
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
g
const char * g
Definition: vf_curves.c:127
decode_rgbxi
static void decode_rgbxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1397
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
get_bits.h
decode_ybyr
static void decode_ybyr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:715
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:107
decode_byryi
static void decode_byryi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:798
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: sheervideo.c:1991
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
yry10i
static const SheerTable yry10i[2]
Definition: sheervideodata.h:131
decode_ca2p
static void decode_ca2p(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:461
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:378
decode_rgbx
static void decode_rgbx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1436
ff_init_vlc_from_lengths
int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: vlc.c:328
decode_yry10i
static void decode_yry10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:286
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:460
decode_aybri
static void decode_aybri(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1099
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:631
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: sheervideo.c:1804
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:427
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
yry10
static const SheerTable yry10[2]
Definition: sheervideodata.h:120
AVPacket::size
int size
Definition: packet.h:375
codec_internal.h
ybr10i
static const SheerTable ybr10i[2]
Definition: sheervideodata.h:87
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
ybri
static const SheerTable ybri[2]
Definition: sheervideodata.h:142
SheerVideoContext::format
unsigned format
Definition: sheervideo.c:33
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:498
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:256
decode_argbi
static void decode_argbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1512
ybr10
static const SheerTable ybr10[2]
Definition: sheervideodata.h:98
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
decode_rgbi
static void decode_rgbi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1654
decode_ybr10i
static void decode_ybr10i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:171
a2
#define a2
Definition: regdef.h:48
SheerTable
Definition: sheervideodata.h:27
byry
static const SheerTable byry[2]
Definition: sheervideodata.h:76
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:598
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: vlc.c:375
avcodec.h
decode_ybr10
static void decode_ybr10(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:210
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
decode_ca2i
static void decode_ca2i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:411
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:426
decode_argxi
static void decode_argxi(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1265
VLC
Definition: vlc.h:31
SheerVideoContext::vlc
VLC vlc[2]
Definition: sheervideo.c:35
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
rgbi
static const SheerTable rgbi[2]
Definition: sheervideodata.h:43
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
decode_c82i
static void decode_c82i(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:563
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
rgb
static const SheerTable rgb[2]
Definition: sheervideodata.h:32
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:375
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
SheerVideoContext::decode_frame
void(* decode_frame)(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:36
decode_argx
static void decode_argx(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1309
decode_aybr
static void decode_aybr(AVCodecContext *avctx, AVFrame *p, GetBitContext *gb)
Definition: sheervideo.c:1177
byryi
static const SheerTable byryi[2]
Definition: sheervideodata.h:153
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:354