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