2 * RTJpeg decoding functions
3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/common.h"
25 #define PUT_COEFF(c) \
27 block[i] = (c) * quant[i];
29 /// aligns the bitstream to the given power of two
31 n = (-get_bits_count(gb)) & (a - 1); \
32 if (n) {skip_bits(gb, n);}
35 * @brief read one block from stream
36 * @param gb contains stream data
37 * @param block where data is written to
38 * @param scan array containing the mapping stream address -> block position
39 * @param quant quantization factors
40 * @return 0 means the block is not coded, < 0 means an error occurred.
42 * Note: GetBitContext is used to make the code simpler, since all data is
43 * aligned this could be done faster in a different way, e.g. as it is done
44 * in MPlayer libmpcodecs/native/rtjpegn.c.
46 static inline int get_block(GetBitContext
*gb
, int16_t *block
, const uint8_t *scan
,
47 const uint32_t *quant
) {
50 uint8_t dc
= get_bits(gb
, 8);
56 // number of non-zero coefficients
57 coeff
= get_bits(gb
, 6);
58 if (get_bits_left(gb
) < (coeff
<< 1))
59 return AVERROR_INVALIDDATA
;
61 // normally we would only need to clear the (63 - coeff) last values,
62 // but since we do not know where they are we just clear the whole block
63 memset(block
, 0, 64 * sizeof(int16_t));
65 // 2 bits per coefficient
67 ac
= get_sbits(gb
, 2);
69 break; // continue with more bits
73 // 4 bits per coefficient
75 if (get_bits_left(gb
) < (coeff
<< 2))
76 return AVERROR_INVALIDDATA
;
78 ac
= get_sbits(gb
, 4);
80 break; // continue with more bits
84 // 8 bits per coefficient
86 if (get_bits_left(gb
) < (coeff
<< 3))
87 return AVERROR_INVALIDDATA
;
89 ac
= get_sbits(gb
, 8);
98 * @brief decode one rtjpeg YUV420 frame
99 * @param c context, must be initialized via ff_rtjpeg_decode_init
100 * @param f AVFrame to place decoded frame into. If parts of the frame
101 * are not coded they are left unchanged, so consider initializing it
102 * @param buf buffer containing input data
103 * @param buf_size length of input data in bytes
104 * @return number of bytes consumed from the input buffer
106 int ff_rtjpeg_decode_frame_yuv420(RTJpegContext
*c
, AVFrame
*f
,
107 const uint8_t *buf
, int buf_size
) {
109 int w
= c
->w
/ 16, h
= c
->h
/ 16;
111 uint8_t *y1
= f
->data
[0], *y2
= f
->data
[0] + 8 * f
->linesize
[0];
112 uint8_t *u
= f
->data
[1], *v
= f
->data
[2];
114 if ((ret
= init_get_bits8(&gb
, buf
, buf_size
)) < 0)
117 for (y
= 0; y
< h
; y
++) {
118 for (x
= 0; x
< w
; x
++) {
119 #define BLOCK(quant, dst, stride) do { \
120 int res = get_block(&gb, block, c->scan, quant); \
124 c->idsp.idct_put(dst, stride, block); \
126 int16_t *block
= c
->block
;
127 BLOCK(c
->lquant
, y1
, f
->linesize
[0]);
129 BLOCK(c
->lquant
, y1
, f
->linesize
[0]);
131 BLOCK(c
->lquant
, y2
, f
->linesize
[0]);
133 BLOCK(c
->lquant
, y2
, f
->linesize
[0]);
135 BLOCK(c
->cquant
, u
, f
->linesize
[1]);
137 BLOCK(c
->cquant
, v
, f
->linesize
[2]);
140 y1
+= 2 * 8 * (f
->linesize
[0] - w
);
141 y2
+= 2 * 8 * (f
->linesize
[0] - w
);
142 u
+= 8 * (f
->linesize
[1] - w
);
143 v
+= 8 * (f
->linesize
[2] - w
);
145 return get_bits_count(&gb
) / 8;
149 * @brief initialize an RTJpegContext, may be called multiple times
150 * @param c context to initialize
151 * @param width width of image, will be rounded down to the nearest multiple
153 * @param height height of image, will be rounded down to the nearest multiple
155 * @param lquant luma quantization table to use
156 * @param cquant chroma quantization table to use
158 void ff_rtjpeg_decode_init(RTJpegContext
*c
, int width
, int height
,
159 const uint32_t *lquant
, const uint32_t *cquant
) {
161 for (i
= 0; i
< 64; i
++) {
162 int p
= c
->idsp
.idct_permutation
[i
];
163 c
->lquant
[p
] = lquant
[i
];
164 c
->cquant
[p
] = cquant
[i
];
170 void ff_rtjpeg_init(RTJpegContext
*c
, AVCodecContext
*avctx
)
174 ff_idctdsp_init(&c
->idsp
, avctx
);
176 for (i
= 0; i
< 64; i
++) {
177 int z
= ff_zigzag_direct
[i
];
178 z
= ((z
<< 3) | (z
>> 3)) & 63; // rtjpeg uses a transposed variant
180 // permute the scan and quantization tables for the chosen idct
181 c
->scan
[i
] = c
->idsp
.idct_permutation
[z
];