]> git.sesse.net Git - ffmpeg/blob - libavcodec/rtjpeg.c
Add a few size checks when decoding rtjpeg blocks.
[ffmpeg] / libavcodec / rtjpeg.c
1 /*
2  * RTJpeg decoding functions
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
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
30 //! aligns the bitstream to the give power of two
31 #define ALIGN(a) \
32     n = (-get_bits_count(gb)) & (a - 1); \
33     if (n) {skip_bits(gb, n);}
34
35 /**
36  * \brief read one block from stream
37  * \param gb contains stream data
38  * \param block where data is written to
39  * \param scan array containing the mapping stream address -> block position
40  * \param quant quantization factors
41  *
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.
45  */
46 static inline int get_block(GetBitContext *gb, DCTELEM *block, const uint8_t *scan,
47                             const uint32_t *quant) {
48     int coeff, i, n;
49     int8_t ac;
50     uint8_t dc = get_bits(gb, 8);
51
52     // block not coded
53     if (dc == 255)
54        return 0;
55
56     // number of non-zero coefficients
57     coeff = get_bits(gb, 6);
58     if (get_bits_count(gb) + (coeff << 1) >= gb->size_in_bits)
59         return 0;
60
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(DCTELEM));
64
65     // 2 bits per coefficient
66     while (coeff) {
67         ac = get_sbits(gb, 2);
68         if (ac == -2)
69             break; // continue with more bits
70         PUT_COEFF(ac);
71     }
72
73     // 4 bits per coefficient
74     ALIGN(4);
75     if (get_bits_count(gb) + (coeff << 2) >= gb->size_in_bits)
76         return 0;
77     while (coeff) {
78         ac = get_sbits(gb, 4);
79         if (ac == -8)
80             break; // continue with more bits
81         PUT_COEFF(ac);
82     }
83
84     // 8 bits per coefficient
85     ALIGN(8);
86     if (get_bits_count(gb) + (coeff << 3) >= gb->size_in_bits)
87         return 0;
88     while (coeff) {
89         ac = get_sbits(gb, 8);
90         PUT_COEFF(ac);
91     }
92
93     PUT_COEFF(dc);
94     return 1;
95 }
96
97 /**
98  * \brief decode one rtjpeg YUV420 frame
99  * \param c context, must be initialized via 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
105  */
106 int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
107                                const uint8_t *buf, int buf_size) {
108     GetBitContext gb;
109     int w = c->w / 16, h = c->h / 16;
110     int x, y;
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];
113     init_get_bits(&gb, buf, buf_size * 8);
114     for (y = 0; y < h; y++) {
115         for (x = 0; x < w; x++) {
116             DCTELEM *block = c->block;
117             if (get_block(&gb, block, c->scan, c->lquant))
118                 c->dsp->idct_put(y1, f->linesize[0], block);
119             y1 += 8;
120             if (get_block(&gb, block, c->scan, c->lquant))
121                 c->dsp->idct_put(y1, f->linesize[0], block);
122             y1 += 8;
123             if (get_block(&gb, block, c->scan, c->lquant))
124                 c->dsp->idct_put(y2, f->linesize[0], block);
125             y2 += 8;
126             if (get_block(&gb, block, c->scan, c->lquant))
127                 c->dsp->idct_put(y2, f->linesize[0], block);
128             y2 += 8;
129             if (get_block(&gb, block, c->scan, c->cquant))
130                 c->dsp->idct_put(u, f->linesize[1], block);
131             u += 8;
132             if (get_block(&gb, block, c->scan, c->cquant))
133                 c->dsp->idct_put(v, f->linesize[2], block);
134             v += 8;
135         }
136         y1 += 2 * 8 * (f->linesize[0] - w);
137         y2 += 2 * 8 * (f->linesize[0] - w);
138         u += 8 * (f->linesize[1] - w);
139         v += 8 * (f->linesize[2] - w);
140     }
141     return get_bits_count(&gb) / 8;
142 }
143
144 /**
145  * \brief initialize an RTJpegContext, may be called multiple times
146  * \param c context to initialize
147  * \param dsp specifies the idct to use for decoding
148  * \param width width of image, will be rounded down to the nearest multiple
149  *              of 16 for decoding
150  * \param height height of image, will be rounded down to the nearest multiple
151  *              of 16 for decoding
152  * \param lquant luma quantization table to use
153  * \param cquant chroma quantization table to use
154  */
155 void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
156                         int width, int height,
157                         const uint32_t *lquant, const uint32_t *cquant) {
158     int i;
159     c->dsp = dsp;
160     for (i = 0; i < 64; i++) {
161         int z = ff_zigzag_direct[i];
162         int p = c->dsp->idct_permutation[i];
163         z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
164
165         // permute the scan and quantization tables for the chosen idct
166         c->scan[i] = c->dsp->idct_permutation[z];
167         c->lquant[p] = lquant[i];
168         c->cquant[p] = cquant[i];
169     }
170     c->w = width;
171     c->h = height;
172 }