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