00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/common.h"
00022 #include "get_bits.h"
00023 #include "dsputil.h"
00024 #include "rtjpeg.h"
00025
00026 #define PUT_COEFF(c) \
00027 i = scan[coeff--]; \
00028 block[i] = (c) * quant[i];
00029
00031 #define ALIGN(a) \
00032 n = (-get_bits_count(gb)) & (a - 1); \
00033 if (n) {skip_bits(gb, n);}
00034
00047 static inline int get_block(GetBitContext *gb, DCTELEM *block, const uint8_t *scan,
00048 const uint32_t *quant) {
00049 int coeff, i, n;
00050 int8_t ac;
00051 uint8_t dc = get_bits(gb, 8);
00052
00053
00054 if (dc == 255)
00055 return 0;
00056
00057
00058 coeff = get_bits(gb, 6);
00059 if (get_bits_left(gb) < (coeff << 1))
00060 return -1;
00061
00062
00063
00064 memset(block, 0, 64 * sizeof(DCTELEM));
00065
00066
00067 while (coeff) {
00068 ac = get_sbits(gb, 2);
00069 if (ac == -2)
00070 break;
00071 PUT_COEFF(ac);
00072 }
00073
00074
00075 ALIGN(4);
00076 if (get_bits_left(gb) < (coeff << 2))
00077 return -1;
00078 while (coeff) {
00079 ac = get_sbits(gb, 4);
00080 if (ac == -8)
00081 break;
00082 PUT_COEFF(ac);
00083 }
00084
00085
00086 ALIGN(8);
00087 if (get_bits_left(gb) < (coeff << 3))
00088 return -1;
00089 while (coeff) {
00090 ac = get_sbits(gb, 8);
00091 PUT_COEFF(ac);
00092 }
00093
00094 PUT_COEFF(dc);
00095 return 1;
00096 }
00097
00107 int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
00108 const uint8_t *buf, int buf_size) {
00109 GetBitContext gb;
00110 int w = c->w / 16, h = c->h / 16;
00111 int x, y;
00112 uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
00113 uint8_t *u = f->data[1], *v = f->data[2];
00114 init_get_bits(&gb, buf, buf_size * 8);
00115 for (y = 0; y < h; y++) {
00116 for (x = 0; x < w; x++) {
00117 #define BLOCK(quant, dst, stride) do { \
00118 int res = get_block(&gb, block, c->scan, quant); \
00119 if (res < 0) \
00120 return res; \
00121 if (res > 0) \
00122 c->dsp->idct_put(dst, stride, block); \
00123 } while (0)
00124 DCTELEM *block = c->block;
00125 BLOCK(c->lquant, y1, f->linesize[0]);
00126 y1 += 8;
00127 BLOCK(c->lquant, y1, f->linesize[0]);
00128 y1 += 8;
00129 BLOCK(c->lquant, y2, f->linesize[0]);
00130 y2 += 8;
00131 BLOCK(c->lquant, y2, f->linesize[0]);
00132 y2 += 8;
00133 BLOCK(c->cquant, u, f->linesize[1]);
00134 u += 8;
00135 BLOCK(c->cquant, v, f->linesize[2]);
00136 v += 8;
00137 }
00138 y1 += 2 * 8 * (f->linesize[0] - w);
00139 y2 += 2 * 8 * (f->linesize[0] - w);
00140 u += 8 * (f->linesize[1] - w);
00141 v += 8 * (f->linesize[2] - w);
00142 }
00143 return get_bits_count(&gb) / 8;
00144 }
00145
00157 void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
00158 int width, int height,
00159 const uint32_t *lquant, const uint32_t *cquant) {
00160 int i;
00161 c->dsp = dsp;
00162 for (i = 0; i < 64; i++) {
00163 int z = ff_zigzag_direct[i];
00164 int p = c->dsp->idct_permutation[i];
00165 z = ((z << 3) | (z >> 3)) & 63;
00166
00167
00168 c->scan[i] = c->dsp->idct_permutation[z];
00169 c->lquant[p] = lquant[i];
00170 c->cquant[p] = cquant[i];
00171 }
00172 c->w = width;
00173 c->h = height;
00174 }