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