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