comparison src/ffmpeg/libavcodec/rtjpeg.c @ 808:e8776388b02a trunk

[svn] - add ffmpeg
author nenolod
date Mon, 12 Mar 2007 11:18:54 -0700
parents
children
comparison
equal deleted inserted replaced
807:0f9c8d4d3ac4 808:e8776388b02a
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 "common.h"
22 #include "bitstream.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, uint8_t *scan,
47 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 // normally we would only need to clear the (63 - coeff) last values,
59 // but since we do not know where they are we just clear the whole block
60 memset(block, 0, 64 * sizeof(DCTELEM));
61
62 // 2 bits per coefficient
63 while (coeff) {
64 ac = get_sbits(gb, 2);
65 if (ac == -2)
66 break; // continue with more bits
67 PUT_COEFF(ac);
68 }
69
70 // 4 bits per coefficient
71 ALIGN(4);
72 while (coeff) {
73 ac = get_sbits(gb, 4);
74 if (ac == -8)
75 break; // continue with more bits
76 PUT_COEFF(ac);
77 }
78
79 // 8 bits per coefficient
80 ALIGN(8);
81 while (coeff) {
82 ac = get_sbits(gb, 8);
83 PUT_COEFF(ac);
84 }
85
86 PUT_COEFF(dc);
87 return 1;
88 }
89
90 /**
91 * \brief decode one rtjpeg YUV420 frame
92 * \param c context, must be initialized via rtjpeg_decode_init
93 * \param f AVFrame to place decoded frame into. If parts of the frame
94 * are not coded they are left unchanged, so consider initializing it
95 * \param buf buffer containing input data
96 * \param buf_size length of input data in bytes
97 * \return number of bytes consumed from the input buffer
98 */
99 int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
100 uint8_t *buf, int buf_size) {
101 GetBitContext gb;
102 int w = c->w / 16, h = c->h / 16;
103 int x, y;
104 void *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
105 void *u = f->data[1], *v = f->data[2];
106 init_get_bits(&gb, buf, buf_size * 8);
107 for (y = 0; y < h; y++) {
108 for (x = 0; x < w; x++) {
109 if (get_block(&gb, c->block, c->scan, c->lquant))
110 c->dsp->idct_put(y1, f->linesize[0], c->block);
111 y1 += 8;
112 if (get_block(&gb, c->block, c->scan, c->lquant))
113 c->dsp->idct_put(y1, f->linesize[0], c->block);
114 y1 += 8;
115 if (get_block(&gb, c->block, c->scan, c->lquant))
116 c->dsp->idct_put(y2, f->linesize[0], c->block);
117 y2 += 8;
118 if (get_block(&gb, c->block, c->scan, c->lquant))
119 c->dsp->idct_put(y2, f->linesize[0], c->block);
120 y2 += 8;
121 if (get_block(&gb, c->block, c->scan, c->cquant))
122 c->dsp->idct_put(u, f->linesize[1], c->block);
123 u += 8;
124 if (get_block(&gb, c->block, c->scan, c->cquant))
125 c->dsp->idct_put(v, f->linesize[2], c->block);
126 v += 8;
127 }
128 y1 += 2 * 8 * (f->linesize[0] - w);
129 y2 += 2 * 8 * (f->linesize[0] - w);
130 u += 8 * (f->linesize[1] - w);
131 v += 8 * (f->linesize[2] - w);
132 }
133 return get_bits_count(&gb) / 8;
134 }
135
136 /**
137 * \brief initialize an RTJpegContext, may be called multiple times
138 * \param c context to initialize
139 * \param dsp specifies the idct to use for decoding
140 * \param width width of image, will be rounded down to the nearest multiple
141 * of 16 for decoding
142 * \param height height of image, will be rounded down to the nearest multiple
143 * of 16 for decoding
144 * \param lquant luma quantization table to use
145 * \param cquant chroma quantization table to use
146 */
147 void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
148 int width, int height,
149 uint32_t *lquant, uint32_t *cquant) {
150 int i;
151 c->dsp = dsp;
152 for (i = 0; i < 64; i++) {
153 int z = ff_zigzag_direct[i];
154 int p = c->dsp->idct_permutation[i];
155 z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
156
157 // permute the scan and quantization tables for the chosen idct
158 c->scan[i] = c->dsp->idct_permutation[z];
159 c->lquant[p] = lquant[i];
160 c->cquant[p] = cquant[i];
161 }
162 c->w = width;
163 c->h = height;
164 }