]> git.sesse.net Git - ffmpeg/blob - libavcodec/rtjpeg.c
install opt.h
[ffmpeg] / libavcodec / rtjpeg.c
1 /*
2  * RTJpeg decoding functions
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
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.
9  *
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.
14  *
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
18  */
19 #include "common.h"
20 #include "bitstream.h"
21 #include "dsputil.h"
22 #include "rtjpeg.h"
23
24 #define PUT_COEFF(c) \
25     i = scan[coeff--]; \
26     block[i] = (c) * quant[i];
27
28 //! aligns the bitstream to the give power of two
29 #define ALIGN(a) \
30     n = (-get_bits_count(gb)) & (a - 1); \
31     if (n) {skip_bits(gb, n);}
32
33 /**
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
39  *
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
43  */
44 static inline int get_block(GetBitContext *gb, DCTELEM *block, uint8_t *scan,
45                             uint32_t *quant) {
46     int coeff, i, n;
47     int8_t ac;
48     uint8_t dc = get_bits(gb, 8);
49
50     // block not coded
51     if (dc == 255)
52        return 0;
53
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));
59
60     // 2 bits per coefficient
61     while (coeff) {
62         ac = get_sbits(gb, 2);
63         if (ac == -2)
64             break; // continue with more bits
65         PUT_COEFF(ac);
66     }
67
68     // 4 bits per coefficient
69     ALIGN(4);
70     while (coeff) {
71         ac = get_sbits(gb, 4);
72         if (ac == -8)
73             break; // continue with more bits
74         PUT_COEFF(ac);
75     }
76
77     // 8 bits per coefficient
78     ALIGN(8);
79     while (coeff) {
80         ac = get_sbits(gb, 8);
81         PUT_COEFF(ac);
82     }
83
84     PUT_COEFF(dc);
85     return 1;
86 }
87
88 /**
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
96  */
97 int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
98                                uint8_t *buf, int buf_size) {
99     GetBitContext gb;
100     int w = c->w / 16, h = c->h / 16;
101     int x, y;
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);
109             y1 += 8;
110             if (get_block(&gb, c->block, c->scan, c->lquant))
111                 c->dsp->idct_put(y1, f->linesize[0], c->block);
112             y1 += 8;
113             if (get_block(&gb, c->block, c->scan, c->lquant))
114                 c->dsp->idct_put(y2, f->linesize[0], c->block);
115             y2 += 8;
116             if (get_block(&gb, c->block, c->scan, c->lquant))
117                 c->dsp->idct_put(y2, f->linesize[0], c->block);
118             y2 += 8;
119             if (get_block(&gb, c->block, c->scan, c->cquant))
120                 c->dsp->idct_put(u, f->linesize[1], c->block);
121             u += 8;
122             if (get_block(&gb, c->block, c->scan, c->cquant))
123                 c->dsp->idct_put(v, f->linesize[2], c->block);
124             v += 8;
125         }
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);
130     }
131     return get_bits_count(&gb) / 8;
132 }
133
134 /**
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
139  *              of 16 for decoding
140  * \param height height of image, will be rounded down to the nearest multiple
141  *              of 16 for decoding
142  * \param lquant luma quantization table to use
143  * \param cquant chroma quantization table to use
144  */
145 void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
146                         int width, int height,
147                         uint32_t *lquant, uint32_t *cquant) {
148     int i;
149     c->dsp = dsp;
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
154
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];
159     }
160     c->w = width;
161     c->h = height;
162 }