rtjpeg.c
Go to the documentation of this file.
1 /*
2  * RTJpeg decoding functions
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "libavutil/common.h"
22 #include "get_bits.h"
23 #include "dsputil.h"
24 #include "rtjpeg.h"
25 
26 #define PUT_COEFF(c) \
27  i = scan[coeff--]; \
28  block[i] = (c) * quant[i];
29 
31 #define ALIGN(a) \
32  n = (-get_bits_count(gb)) & (a - 1); \
33  if (n) {skip_bits(gb, n);}
34 
47 static inline int get_block(GetBitContext *gb, DCTELEM *block, const uint8_t *scan,
48  const uint32_t *quant) {
49  int coeff, i, n;
50  int8_t ac;
51  uint8_t dc = get_bits(gb, 8);
52 
53  // block not coded
54  if (dc == 255)
55  return 0;
56 
57  // number of non-zero coefficients
58  coeff = get_bits(gb, 6);
59  if (get_bits_left(gb) < (coeff << 1))
60  return -1;
61 
62  // normally we would only need to clear the (63 - coeff) last values,
63  // but since we do not know where they are we just clear the whole block
64  memset(block, 0, 64 * sizeof(DCTELEM));
65 
66  // 2 bits per coefficient
67  while (coeff) {
68  ac = get_sbits(gb, 2);
69  if (ac == -2)
70  break; // continue with more bits
71  PUT_COEFF(ac);
72  }
73 
74  // 4 bits per coefficient
75  ALIGN(4);
76  if (get_bits_left(gb) < (coeff << 2))
77  return -1;
78  while (coeff) {
79  ac = get_sbits(gb, 4);
80  if (ac == -8)
81  break; // continue with more bits
82  PUT_COEFF(ac);
83  }
84 
85  // 8 bits per coefficient
86  ALIGN(8);
87  if (get_bits_left(gb) < (coeff << 3))
88  return -1;
89  while (coeff) {
90  ac = get_sbits(gb, 8);
91  PUT_COEFF(ac);
92  }
93 
94  PUT_COEFF(dc);
95  return 1;
96 }
97 
108  const uint8_t *buf, int buf_size) {
109  GetBitContext gb;
110  int w = c->w / 16, h = c->h / 16;
111  int x, y, ret;
112  uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
113  uint8_t *u = f->data[1], *v = f->data[2];
114 
115  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
116  return ret;
117 
118  for (y = 0; y < h; y++) {
119  for (x = 0; x < w; x++) {
120 #define BLOCK(quant, dst, stride) do { \
121  int res = get_block(&gb, block, c->scan, quant); \
122  if (res < 0) \
123  return res; \
124  if (res > 0) \
125  c->dsp->idct_put(dst, stride, block); \
126 } while (0)
127  DCTELEM *block = c->block;
128  BLOCK(c->lquant, y1, f->linesize[0]);
129  y1 += 8;
130  BLOCK(c->lquant, y1, f->linesize[0]);
131  y1 += 8;
132  BLOCK(c->lquant, y2, f->linesize[0]);
133  y2 += 8;
134  BLOCK(c->lquant, y2, f->linesize[0]);
135  y2 += 8;
136  BLOCK(c->cquant, u, f->linesize[1]);
137  u += 8;
138  BLOCK(c->cquant, v, f->linesize[2]);
139  v += 8;
140  }
141  y1 += 2 * 8 * (f->linesize[0] - w);
142  y2 += 2 * 8 * (f->linesize[0] - w);
143  u += 8 * (f->linesize[1] - w);
144  v += 8 * (f->linesize[2] - w);
145  }
146  return get_bits_count(&gb) / 8;
147 }
148 
161  int width, int height,
162  const uint32_t *lquant, const uint32_t *cquant) {
163  int i;
164  c->dsp = dsp;
165  for (i = 0; i < 64; i++) {
166  int z = ff_zigzag_direct[i];
167  int p = c->dsp->idct_permutation[i];
168  z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
169 
170  // permute the scan and quantization tables for the chosen idct
171  c->scan[i] = c->dsp->idct_permutation[z];
172  c->lquant[p] = lquant[i];
173  c->cquant[p] = cquant[i];
174  }
175  c->w = width;
176  c->h = height;
177 }