2 * RTJpeg decoding functions
3 * Copyright (c) 2006 Reimar Doeffinger
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "bitstream.h"
24 #define PUT_COEFF(c) \
26 block[i] = (c) * quant[i];
28 //! aligns the bitstream to the give power of two
30 n = (-get_bits_count(gb)) & (a - 1); \
31 if (n) {skip_bits(gb, n);}
34 * \brief read one block from stream
35 * \param gb contains stream data
36 * \param block where data is written to
37 * \param scan array containing the mapping stream address -> block position
38 * \param quant quantization factors
40 * Note: GetBitContext is used to make the code simpler, since all data is
41 * aligned this could be done faster in a different way, e.g. as it is done
42 * in MPlayer libmpcodecs/native/RTjpegN.c
44 static inline int get_block(GetBitContext *gb, DCTELEM *block, uint8_t *scan,
48 uint8_t dc = get_bits(gb, 8);
54 // number of non-zero coefficients
55 coeff = get_bits(gb, 6);
56 // normally we would only need to clear the (63 - coeff) last values,
57 // but since we do not know where they are we just clear the whole block
58 memset(block, 0, 64 * sizeof(DCTELEM));
60 // 2 bits per coefficient
62 ac = get_sbits(gb, 2);
64 break; // continue with more bits
68 // 4 bits per coefficient
71 ac = get_sbits(gb, 4);
73 break; // continue with more bits
77 // 8 bits per coefficient
80 ac = get_sbits(gb, 8);
89 * \brief decode one rtjpeg YUV420 frame
90 * \param c context, must be initialized via rtjpeg_decode_init
91 * \param f AVFrame to place decoded frame into. If parts of the frame
92 * are not coded they are left unchanged, so consider initializing it
93 * \param buf buffer containing input data
94 * \param buf_size length of input data in bytes
95 * \return number of bytes consumed from the input buffer
97 int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
98 uint8_t *buf, int buf_size) {
100 int w = c->w / 16, h = c->h / 16;
102 void *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
103 void *u = f->data[1], *v = f->data[2];
104 init_get_bits(&gb, buf, buf_size * 8);
105 for (y = 0; y < h; y++) {
106 for (x = 0; x < w; x++) {
107 if (get_block(&gb, c->block, c->scan, c->lquant))
108 c->dsp->idct_put(y1, f->linesize[0], c->block);
110 if (get_block(&gb, c->block, c->scan, c->lquant))
111 c->dsp->idct_put(y1, f->linesize[0], c->block);
113 if (get_block(&gb, c->block, c->scan, c->lquant))
114 c->dsp->idct_put(y2, f->linesize[0], c->block);
116 if (get_block(&gb, c->block, c->scan, c->lquant))
117 c->dsp->idct_put(y2, f->linesize[0], c->block);
119 if (get_block(&gb, c->block, c->scan, c->cquant))
120 c->dsp->idct_put(u, f->linesize[1], c->block);
122 if (get_block(&gb, c->block, c->scan, c->cquant))
123 c->dsp->idct_put(v, f->linesize[2], c->block);
126 y1 += 2 * 8 * (f->linesize[0] - w);
127 y2 += 2 * 8 * (f->linesize[0] - w);
128 u += 8 * (f->linesize[1] - w);
129 v += 8 * (f->linesize[2] - w);
131 return get_bits_count(&gb) / 8;
135 * \brief initialize an RTJpegContext, may be called multiple times
136 * \param c context to initialize
137 * \param dsp specifies the idct to use for decoding
138 * \param width width of image, will be rounded down to the nearest multiple
140 * \param height height of image, will be rounded down to the nearest multiple
142 * \param lquant luma quantization table to use
143 * \param cquant chroma quantization table to use
145 void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
146 int width, int height,
147 uint32_t *lquant, uint32_t *cquant) {
150 for (i = 0; i < 64; i++) {
151 int z = ff_zigzag_direct[i];
152 int p = c->dsp->idct_permutation[i];
153 z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
155 // permute the scan and quantization tables for the chosen idct
156 c->scan[i] = c->dsp->idct_permutation[z];
157 c->lquant[p] = lquant[i];
158 c->cquant[p] = cquant[i];