]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/sp5xdec.c
indent
[frescor/ffmpeg.git] / libavcodec / sp5xdec.c
1 /*
2  * Sunplus JPEG decoder (SP5X)
3  * Copyright (c) 2003 Alex Beregszaszi
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
22 /**
23  * @file libavcodec/sp5xdec.c
24  * Sunplus JPEG decoder (SP5X).
25  */
26
27 #include "avcodec.h"
28 #include "mjpeg.h"
29 #include "mjpegdec.h"
30 #include "sp5x.h"
31
32
33 static int sp5x_decode_frame(AVCodecContext *avctx,
34                               void *data, int *data_size,
35                               AVPacket *avpkt)
36 {
37     const uint8_t *buf = avpkt->data;
38     int buf_size = avpkt->size;
39     AVPacket avpkt_recoded;
40 #if 0
41     MJpegDecodeContext *s = avctx->priv_data;
42 #endif
43     const int qscale = 5;
44     const uint8_t *buf_ptr, *buf_end;
45     uint8_t *recoded;
46     int i = 0, j = 0;
47
48     if (!avctx->width || !avctx->height)
49         return -1;
50
51     buf_ptr = buf;
52     buf_end = buf + buf_size;
53
54 #if 1
55     recoded = av_mallocz(buf_size + 1024);
56     if (!recoded)
57         return -1;
58
59     /* SOI */
60     recoded[j++] = 0xFF;
61     recoded[j++] = 0xD8;
62
63     memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
64     memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
65     memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
66     j += sizeof(sp5x_data_dqt);
67
68     memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
69     j += sizeof(sp5x_data_dht);
70
71     memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
72     AV_WB16(recoded+j+5, avctx->coded_height);
73     AV_WB16(recoded+j+7, avctx->coded_width);
74     j += sizeof(sp5x_data_sof);
75
76     memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
77     j += sizeof(sp5x_data_sos);
78
79     if(avctx->codec_id==CODEC_ID_AMV)
80         for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
81             recoded[j++] = buf[i];
82     else
83     for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
84     {
85         recoded[j++] = buf[i];
86         if (buf[i] == 0xff)
87             recoded[j++] = 0;
88     }
89
90     /* EOI */
91     recoded[j++] = 0xFF;
92     recoded[j++] = 0xD9;
93
94     avctx->flags &= ~CODEC_FLAG_EMU_EDGE;
95     av_init_packet(&avpkt_recoded);
96     avpkt_recoded.data = recoded;
97     avpkt_recoded.size = j;
98     i = ff_mjpeg_decode_frame(avctx, data, data_size, &avpkt_recoded);
99
100     av_free(recoded);
101
102 #else
103     /* SOF */
104     s->bits = 8;
105     s->width  = avctx->coded_width;
106     s->height = avctx->coded_height;
107     s->nb_components = 3;
108     s->component_id[0] = 0;
109     s->h_count[0] = 2;
110     s->v_count[0] = 2;
111     s->quant_index[0] = 0;
112     s->component_id[1] = 1;
113     s->h_count[1] = 1;
114     s->v_count[1] = 1;
115     s->quant_index[1] = 1;
116     s->component_id[2] = 2;
117     s->h_count[2] = 1;
118     s->v_count[2] = 1;
119     s->quant_index[2] = 1;
120     s->h_max = 2;
121     s->v_max = 2;
122
123     s->qscale_table = av_mallocz((s->width+15)/16);
124     avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
125     s->interlaced = 0;
126
127     s->picture.reference = 0;
128     if (avctx->get_buffer(avctx, &s->picture) < 0)
129     {
130         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
131         return -1;
132     }
133
134     s->picture.pict_type = FF_I_TYPE;
135     s->picture.key_frame = 1;
136
137     for (i = 0; i < 3; i++)
138         s->linesize[i] = s->picture.linesize[i] << s->interlaced;
139
140     /* DQT */
141     for (i = 0; i < 64; i++)
142     {
143         j = s->scantable.permutated[i];
144         s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
145     }
146     s->qscale[0] = FFMAX(
147         s->quant_matrixes[0][s->scantable.permutated[1]],
148         s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
149
150     for (i = 0; i < 64; i++)
151     {
152         j = s->scantable.permutated[i];
153         s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
154     }
155     s->qscale[1] = FFMAX(
156         s->quant_matrixes[1][s->scantable.permutated[1]],
157         s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
158
159     /* DHT */
160
161     /* SOS */
162     s->comp_index[0] = 0;
163     s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
164     s->h_scount[0] = s->h_count[0];
165     s->v_scount[0] = s->v_count[0];
166     s->dc_index[0] = 0;
167     s->ac_index[0] = 0;
168
169     s->comp_index[1] = 1;
170     s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
171     s->h_scount[1] = s->h_count[1];
172     s->v_scount[1] = s->v_count[1];
173     s->dc_index[1] = 1;
174     s->ac_index[1] = 1;
175
176     s->comp_index[2] = 2;
177     s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
178     s->h_scount[2] = s->h_count[2];
179     s->v_scount[2] = s->v_count[2];
180     s->dc_index[2] = 1;
181     s->ac_index[2] = 1;
182
183     for (i = 0; i < 3; i++)
184         s->last_dc[i] = 1024;
185
186     s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
187     s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
188
189     init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
190
191     return mjpeg_decode_scan(s);
192 #endif
193
194     return i;
195 }
196
197 AVCodec sp5x_decoder = {
198     "sp5x",
199     CODEC_TYPE_VIDEO,
200     CODEC_ID_SP5X,
201     sizeof(MJpegDecodeContext),
202     ff_mjpeg_decode_init,
203     NULL,
204     ff_mjpeg_decode_end,
205     sp5x_decode_frame,
206     CODEC_CAP_DR1,
207     NULL,
208     .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
209 };
210
211 AVCodec amv_decoder = {
212     "amv",
213     CODEC_TYPE_VIDEO,
214     CODEC_ID_AMV,
215     sizeof(MJpegDecodeContext),
216     ff_mjpeg_decode_init,
217     NULL,
218     ff_mjpeg_decode_end,
219     sp5x_decode_frame,
220     .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
221 };