2 * RTJpeg decoding functions
3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/common.h"
24 #include "bitstream.h"
27 #define PUT_COEFF(c) \
29 block[i] = (c) * quant[i];
31 /// aligns the bitstream to the given power of two
33 n = (-bitstream_tell(bc)) & (a - 1); \
35 bitstream_skip(bc, n);
38 * @brief read one block from stream
39 * @param bc contains stream data
40 * @param block where data is written to
41 * @param scan array containing the mapping stream address -> block position
42 * @param quant quantization factors
43 * @return 0 means the block is not coded, < 0 means an error occurred.
45 * Note: BitstreamContext is used to make the code simpler, since all data is
46 * aligned this could be done faster in a different way, e.g. as it is done
47 * in MPlayer libmpcodecs/native/rtjpegn.c.
49 static inline int get_block(BitstreamContext *bc, int16_t *block,
50 const uint8_t *scan, const uint32_t *quant)
54 uint8_t dc = bitstream_read(bc, 8);
60 // number of non-zero coefficients
61 coeff = bitstream_read(bc, 6);
62 if (bitstream_bits_left(bc) < (coeff << 1))
63 return AVERROR_INVALIDDATA;
65 // normally we would only need to clear the (63 - coeff) last values,
66 // but since we do not know where they are we just clear the whole block
67 memset(block, 0, 64 * sizeof(int16_t));
69 // 2 bits per coefficient
71 ac = bitstream_read_signed(bc, 2);
73 break; // continue with more bits
77 // 4 bits per coefficient
79 if (bitstream_bits_left(bc) < (coeff << 2))
80 return AVERROR_INVALIDDATA;
82 ac = bitstream_read_signed(bc, 4);
84 break; // continue with more bits
88 // 8 bits per coefficient
90 if (bitstream_bits_left(bc) < (coeff << 3))
91 return AVERROR_INVALIDDATA;
93 ac = bitstream_read_signed(bc, 8);
102 * @brief decode one rtjpeg YUV420 frame
103 * @param c context, must be initialized via ff_rtjpeg_decode_init
104 * @param f AVFrame to place decoded frame into. If parts of the frame
105 * are not coded they are left unchanged, so consider initializing it
106 * @param buf buffer containing input data
107 * @param buf_size length of input data in bytes
108 * @return number of bytes consumed from the input buffer
110 int ff_rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
111 const uint8_t *buf, int buf_size) {
113 int w = c->w / 16, h = c->h / 16;
115 uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
116 uint8_t *u = f->data[1], *v = f->data[2];
118 if ((ret = bitstream_init8(&bc, buf, buf_size)) < 0)
121 for (y = 0; y < h; y++) {
122 for (x = 0; x < w; x++) {
123 #define BLOCK(quant, dst, stride) do { \
124 int res = get_block(&bc, block, c->scan, quant); \
128 c->idsp.idct_put(dst, stride, block); \
130 int16_t *block = c->block;
131 BLOCK(c->lquant, y1, f->linesize[0]);
133 BLOCK(c->lquant, y1, f->linesize[0]);
135 BLOCK(c->lquant, y2, f->linesize[0]);
137 BLOCK(c->lquant, y2, f->linesize[0]);
139 BLOCK(c->cquant, u, f->linesize[1]);
141 BLOCK(c->cquant, v, f->linesize[2]);
144 y1 += 2 * 8 * (f->linesize[0] - w);
145 y2 += 2 * 8 * (f->linesize[0] - w);
146 u += 8 * (f->linesize[1] - w);
147 v += 8 * (f->linesize[2] - w);
149 return bitstream_tell(&bc) / 8;
153 * @brief initialize an RTJpegContext, may be called multiple times
154 * @param c context to initialize
155 * @param width width of image, will be rounded down to the nearest multiple
157 * @param height height of image, will be rounded down to the nearest multiple
159 * @param lquant luma quantization table to use
160 * @param cquant chroma quantization table to use
162 void ff_rtjpeg_decode_init(RTJpegContext *c, int width, int height,
163 const uint32_t *lquant, const uint32_t *cquant) {
165 for (i = 0; i < 64; i++) {
166 int p = c->idsp.idct_permutation[i];
167 c->lquant[p] = lquant[i];
168 c->cquant[p] = cquant[i];
174 void ff_rtjpeg_init(RTJpegContext *c, AVCodecContext *avctx)
178 ff_idctdsp_init(&c->idsp, avctx);
180 for (i = 0; i < 64; i++) {
181 int z = ff_zigzag_direct[i];
182 z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
184 // permute the scan and quantization tables for the chosen idct
185 c->scan[i] = c->idsp.idct_permutation[z];