FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffv1dec_template.c
Go to the documentation of this file.
1 /*
2  * FFV1 decoder template
3  *
4  * Copyright (c) 2003-2016 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 static av_always_inline int RENAME(decode_line)(FFV1Context *s, int w,
24  TYPE *sample[2],
25  int plane_index, int bits)
26 {
27  PlaneContext *const p = &s->plane[plane_index];
28  RangeCoder *const c = &s->c;
29  int x;
30  int run_count = 0;
31  int run_mode = 0;
32  int run_index = s->run_index;
33 
34  if (is_input_end(s))
35  return AVERROR_INVALIDDATA;
36 
37  if (s->slice_coding_mode == 1) {
38  int i;
39  for (x = 0; x < w; x++) {
40  int v = 0;
41  for (i=0; i<bits; i++) {
42  uint8_t state = 128;
43  v += v + get_rac(c, &state);
44  }
45  sample[1][x] = v;
46  }
47  return 0;
48  }
49 
50  for (x = 0; x < w; x++) {
51  int diff, context, sign;
52 
53  context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x);
54  if (context < 0) {
55  context = -context;
56  sign = 1;
57  } else
58  sign = 0;
59 
60  av_assert2(context < p->context_count);
61 
62  if (s->ac != AC_GOLOMB_RICE) {
63  diff = get_symbol_inline(c, p->state[context], 1);
64  } else {
65  if (context == 0 && run_mode == 0)
66  run_mode = 1;
67 
68  if (run_mode) {
69  if (run_count == 0 && run_mode == 1) {
70  if (get_bits1(&s->gb)) {
71  run_count = 1 << ff_log2_run[run_index];
72  if (x + run_count <= w)
73  run_index++;
74  } else {
75  if (ff_log2_run[run_index])
76  run_count = get_bits(&s->gb, ff_log2_run[run_index]);
77  else
78  run_count = 0;
79  if (run_index)
80  run_index--;
81  run_mode = 2;
82  }
83  }
84  run_count--;
85  if (run_count < 0) {
86  run_mode = 0;
87  run_count = 0;
88  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
89  bits);
90  if (diff >= 0)
91  diff++;
92  } else
93  diff = 0;
94  } else
95  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
96 
97  ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
98  run_count, run_index, run_mode, x, get_bits_count(&s->gb));
99  }
100 
101  if (sign)
102  diff = -(unsigned)diff;
103 
104  sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
105  }
106  s->run_index = run_index;
107  return 0;
108 }
109 
110 static void RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
111 {
112  int x, y, p;
113  TYPE *sample[4][2];
114  int lbd = s->avctx->bits_per_raw_sample <= 8;
115  int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
116  int offset = 1 << bits;
117 
118  for (x = 0; x < 4; x++) {
119  sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
120  sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
121  }
122 
123  s->run_index = 0;
124 
125  memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
126 
127  for (y = 0; y < h; y++) {
128  for (p = 0; p < 3 + s->transparency; p++) {
129  TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
130 
131  sample[p][0] = sample[p][1];
132  sample[p][1] = temp;
133 
134  sample[p][1][-1]= sample[p][0][0 ];
135  sample[p][0][ w]= sample[p][0][w-1];
136  if (lbd && s->slice_coding_mode == 0)
137  RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
138  else
139  RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
140  }
141  for (x = 0; x < w; x++) {
142  int g = sample[0][1][x];
143  int b = sample[1][1][x];
144  int r = sample[2][1][x];
145  int a = sample[3][1][x];
146 
147  if (s->slice_coding_mode != 1) {
148  b -= offset;
149  r -= offset;
150  g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
151  b += g;
152  r += g;
153  }
154 
155  if (lbd)
156  *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
157  else if (sizeof(TYPE) == 4) {
158  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
159  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
160  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
161  } else {
162  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
163  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
164  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
165  }
166  }
167  }
168 }
const uint8_t ff_log2_run[41]
Definition: bitstream.c:39
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static struct @260 state
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
else temp
Definition: vf_mcdeint.c:256
const char * g
Definition: vf_curves.c:112
#define RENAME(name)
Definition: ffv1.h:205
const char * b
Definition: vf_curves.c:113
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:42
#define SUINT
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:174
#define src
Definition: vp8dsp.c:254
#define sample
static int is_input_end(FFV1Context *s)
Definition: ffv1dec.c:96
uint8_t bits
Definition: crc.c:296
uint8_t
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:119
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
#define ff_dlog(a,...)
VlcState * vlc_state
Definition: ffv1.h:73
const char * r
Definition: vf_curves.c:111
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition: ffv1dec.c:71
int run_index
Definition: ffv1.h:109
int context_count[MAX_QUANT_TABLES]
Definition: ffv1.h:106
#define TYPE
Definition: ffv1.h:204
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
#define AC_GOLOMB_RICE
Definition: ffv1.h:56
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static double c[64]
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:72
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_always_inline
Definition: attributes.h:39