Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* LEAD MCMP decoder |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2023 Peter Ross |
5 |
|
|
* |
6 |
|
|
* This file is part of FFmpeg. |
7 |
|
|
* |
8 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
9 |
|
|
* modify it under the terms of the GNU Lesser General Public |
10 |
|
|
* License as published by the Free Software Foundation; either |
11 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
|
* Lesser General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include "avcodec.h" |
24 |
|
|
#include "blockdsp.h" |
25 |
|
|
#include "codec_internal.h" |
26 |
|
|
#include "copy_block.h" |
27 |
|
|
#include "decode.h" |
28 |
|
|
#include "get_bits.h" |
29 |
|
|
#include "idctdsp.h" |
30 |
|
|
#include "jpegquanttables.h" |
31 |
|
|
#include "jpegtables.h" |
32 |
|
|
#include "leaddata.h" |
33 |
|
|
#include "libavutil/mem.h" |
34 |
|
|
#include "libavutil/mem_internal.h" |
35 |
|
|
#include "libavutil/thread.h" |
36 |
|
|
|
37 |
|
|
#define LUMA_DC_BITS 9 |
38 |
|
|
#define CHROMA_DC_BITS 11 |
39 |
|
|
#define LUMA_AC_BITS 10 |
40 |
|
|
#define CHROMA_AC_BITS 10 |
41 |
|
|
|
42 |
|
|
static VLCElem luma_dc_vlc[1 << LUMA_DC_BITS]; |
43 |
|
|
static VLCElem chroma_dc_vlc[1 << CHROMA_DC_BITS]; |
44 |
|
|
static VLCElem luma_ac_vlc[1160]; |
45 |
|
|
static VLCElem chroma_ac_vlc[1160]; |
46 |
|
|
|
47 |
|
✗ |
static av_cold void lead_init_static_data(void) |
48 |
|
|
{ |
49 |
|
✗ |
VLC_INIT_STATIC_TABLE_FROM_LENGTHS(luma_dc_vlc, LUMA_DC_BITS, FF_ARRAY_ELEMS(luma_dc_len), |
50 |
|
|
luma_dc_len, 1, |
51 |
|
|
NULL, 0, 0, |
52 |
|
|
0, 0); |
53 |
|
✗ |
VLC_INIT_STATIC_TABLE_FROM_LENGTHS(chroma_dc_vlc, CHROMA_DC_BITS, FF_ARRAY_ELEMS(chroma_dc_len), |
54 |
|
|
chroma_dc_len, 1, |
55 |
|
|
NULL, 0, 0, |
56 |
|
|
0, 0); |
57 |
|
✗ |
VLC_INIT_STATIC_TABLE_FROM_LENGTHS(luma_ac_vlc, LUMA_AC_BITS, FF_ARRAY_ELEMS(luma_ac_len), |
58 |
|
|
luma_ac_len, 1, |
59 |
|
|
ff_mjpeg_val_ac_luminance, 1, 1, |
60 |
|
|
0, 0); |
61 |
|
✗ |
VLC_INIT_STATIC_TABLE_FROM_LENGTHS(chroma_ac_vlc, CHROMA_AC_BITS, FF_ARRAY_ELEMS(chroma_ac_len), |
62 |
|
|
chroma_ac_len, 1, |
63 |
|
|
ff_mjpeg_val_ac_chrominance, 1, 1, |
64 |
|
|
0, 0); |
65 |
|
✗ |
} |
66 |
|
|
|
67 |
|
|
typedef struct LeadContext { |
68 |
|
|
uint8_t *bitstream_buf; |
69 |
|
|
unsigned int bitstream_buf_size; |
70 |
|
|
BlockDSPContext bdsp; |
71 |
|
|
IDCTDSPContext idsp; |
72 |
|
|
uint8_t permutated_scantable[64]; |
73 |
|
|
} LeadContext; |
74 |
|
|
|
75 |
|
✗ |
static av_cold int lead_decode_init(AVCodecContext * avctx) |
76 |
|
|
{ |
77 |
|
|
static AVOnce init_static_once = AV_ONCE_INIT; |
78 |
|
✗ |
LeadContext *s = avctx->priv_data; |
79 |
|
|
|
80 |
|
✗ |
if (avctx->extradata_size < 20) |
81 |
|
✗ |
return AVERROR_INVALIDDATA; |
82 |
|
|
|
83 |
|
✗ |
ff_blockdsp_init(&s->bdsp); |
84 |
|
✗ |
ff_idctdsp_init(&s->idsp, avctx); |
85 |
|
✗ |
ff_permute_scantable(s->permutated_scantable, ff_zigzag_direct, s->idsp.idct_permutation); |
86 |
|
|
|
87 |
|
✗ |
ff_thread_once(&init_static_once, lead_init_static_data); |
88 |
|
|
|
89 |
|
✗ |
return 0; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
✗ |
static void calc_dequant(uint16_t * dequant, const uint8_t * quant_tbl, int q) |
93 |
|
|
{ |
94 |
|
✗ |
for (int i = 0; i < 64; i++) |
95 |
|
✗ |
dequant[i] = av_clip(q * quant_tbl[ff_zigzag_direct[i]] / 50, 2, 32767); |
96 |
|
✗ |
} |
97 |
|
|
|
98 |
|
✗ |
static int decode_block(LeadContext * s, GetBitContext * gb, |
99 |
|
|
const VLCElem * dc_table, int dc_bits, const VLCElem * ac_table, int ac_bits, |
100 |
|
|
int16_t * dc_pred, const uint16_t * dequant, |
101 |
|
|
uint8_t * dst, int stride) |
102 |
|
|
{ |
103 |
|
|
DECLARE_ALIGNED(32, int16_t, block)[64]; |
104 |
|
|
int size; |
105 |
|
|
|
106 |
|
✗ |
s->bdsp.clear_block(block); |
107 |
|
|
|
108 |
|
✗ |
if (get_bits_left(gb) <= 0) |
109 |
|
✗ |
return AVERROR_INVALIDDATA; |
110 |
|
|
|
111 |
|
✗ |
size = get_vlc2(gb, dc_table, dc_bits, 1); |
112 |
|
✗ |
if (size < 0) |
113 |
|
✗ |
return AVERROR_INVALIDDATA; |
114 |
|
|
|
115 |
|
✗ |
if (size) |
116 |
|
✗ |
*dc_pred += get_xbits(gb, size); |
117 |
|
|
|
118 |
|
✗ |
block[0] = (1 << 10) + *dc_pred * dequant[0]; |
119 |
|
|
|
120 |
|
✗ |
for (int i = 1; i < 64; i++) { |
121 |
|
✗ |
int symbol = get_vlc2(gb, ac_table, ac_bits, 2); |
122 |
|
✗ |
if (symbol < 0) |
123 |
|
✗ |
return AVERROR_INVALIDDATA; |
124 |
|
|
|
125 |
|
✗ |
if (!symbol) |
126 |
|
✗ |
break; |
127 |
|
|
|
128 |
|
✗ |
i += symbol >> 4; |
129 |
|
✗ |
if (i >= 64) |
130 |
|
✗ |
return AVERROR_INVALIDDATA; |
131 |
|
|
|
132 |
|
✗ |
size = symbol & 0xF; |
133 |
|
✗ |
if (size) |
134 |
|
✗ |
block[s->permutated_scantable[i]] = get_xbits(gb, size) * dequant[i]; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
✗ |
s->idsp.idct_put(dst, stride, block); |
138 |
|
✗ |
return 0; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
✗ |
static int lead_decode_frame(AVCodecContext *avctx, AVFrame * frame, |
142 |
|
|
int * got_frame, AVPacket * avpkt) |
143 |
|
|
{ |
144 |
|
✗ |
LeadContext *s = avctx->priv_data; |
145 |
|
✗ |
const uint8_t * buf = avpkt->data; |
146 |
|
✗ |
int ret, format, zero = 0, yuv20p_half = 0, fields = 1, q, size; |
147 |
|
|
GetBitContext gb; |
148 |
|
✗ |
int16_t dc_pred[3] = {0, 0, 0}; |
149 |
|
|
uint16_t dequant[2][64]; |
150 |
|
|
|
151 |
|
✗ |
if (avpkt->size < 8) |
152 |
|
✗ |
return AVERROR_INVALIDDATA; |
153 |
|
|
|
154 |
|
✗ |
format = AV_RL16(buf + 4); |
155 |
|
✗ |
switch(format) { |
156 |
|
✗ |
case 0x0: |
157 |
|
✗ |
zero = 1; |
158 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
159 |
|
✗ |
break; |
160 |
|
✗ |
case 0x8000: |
161 |
|
✗ |
yuv20p_half = 1; |
162 |
|
|
// fall-through |
163 |
|
✗ |
case 0x1000: |
164 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
165 |
|
✗ |
break; |
166 |
|
✗ |
case 0x2000: |
167 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_YUV444P; |
168 |
|
✗ |
break; |
169 |
|
✗ |
case 0x2006: |
170 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_YUV444P; |
171 |
|
✗ |
fields = 2; |
172 |
|
✗ |
break; |
173 |
|
✗ |
default: |
174 |
|
✗ |
avpriv_request_sample(avctx, "unsupported format 0x%x", format); |
175 |
|
✗ |
return AVERROR_PATCHWELCOME; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
✗ |
q = AV_RL16(buf + 6); |
179 |
|
✗ |
calc_dequant(dequant[0], ff_mjpeg_std_luminance_quant_tbl, q); |
180 |
|
✗ |
calc_dequant(dequant[1], ff_mjpeg_std_chrominance_quant_tbl, q); |
181 |
|
|
|
182 |
|
✗ |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
183 |
|
✗ |
return ret; |
184 |
|
|
|
185 |
|
✗ |
av_fast_padded_malloc(&s->bitstream_buf, &s->bitstream_buf_size, avpkt->size - 8); |
186 |
|
✗ |
if (!s->bitstream_buf) |
187 |
|
✗ |
return AVERROR(ENOMEM); |
188 |
|
|
|
189 |
|
✗ |
size = 0; |
190 |
|
✗ |
for (int i = 8; i < avpkt->size; i++) { |
191 |
|
✗ |
int src = buf[i] ^ 0x80; |
192 |
|
✗ |
s->bitstream_buf[size++] = src; |
193 |
|
✗ |
if (src == 0xFF && i + 1 < avpkt->size && (buf[i + 1] ^ 0x80) == 0x00) |
194 |
|
✗ |
i++; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
✗ |
ret = init_get_bits8(&gb, s->bitstream_buf, size); |
198 |
|
✗ |
if (ret < 0) |
199 |
|
✗ |
return ret; |
200 |
|
|
|
201 |
|
✗ |
if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && zero) { |
202 |
|
✗ |
for (int mb_y = 0; mb_y < avctx->height / 8; mb_y++) |
203 |
|
✗ |
for (int mb_x = 0; mb_x < avctx->width / 16; mb_x++) |
204 |
|
✗ |
for (int b = 0; b < 4; b++) { |
205 |
|
✗ |
int luma_block = 2; |
206 |
|
✗ |
const VLCElem * dc_vlc = b < luma_block ? luma_dc_vlc : chroma_dc_vlc; |
207 |
|
✗ |
int dc_bits = b < luma_block ? LUMA_DC_BITS : CHROMA_DC_BITS; |
208 |
|
✗ |
const VLCElem * ac_vlc = b < luma_block ? luma_ac_vlc : chroma_ac_vlc; |
209 |
|
✗ |
int ac_bits = b < luma_block ? LUMA_AC_BITS : CHROMA_AC_BITS; |
210 |
|
✗ |
int plane = b < luma_block ? 0 : b - 1; |
211 |
|
|
int x, y, yclip; |
212 |
|
|
|
213 |
|
✗ |
if (b < luma_block) { |
214 |
|
✗ |
y = 8*mb_y + 8*(b >> 1); |
215 |
|
✗ |
x = 16*mb_x + 8*(b & 1); |
216 |
|
✗ |
yclip = 0; |
217 |
|
|
} else { |
218 |
|
✗ |
y = 4*mb_y; |
219 |
|
✗ |
x = 8*mb_x; |
220 |
|
✗ |
yclip = y + 8 >= avctx->height / 2; |
221 |
|
|
} |
222 |
|
|
|
223 |
|
✗ |
if (yclip) { |
224 |
|
|
uint8_t tmp[64]; |
225 |
|
✗ |
ret = decode_block(s, &gb, dc_vlc, dc_bits, ac_vlc, ac_bits, |
226 |
|
✗ |
dc_pred + plane, dequant[!(b < 4)], tmp, 8); |
227 |
|
✗ |
for (int yy = 0; yy < 8 && y + yy < avctx->height / 2; yy++) |
228 |
|
✗ |
memcpy(frame->data[plane] + (y+yy)*frame->linesize[plane] + x, tmp + yy, 8); |
229 |
|
|
} else { |
230 |
|
✗ |
ret = decode_block(s, &gb, dc_vlc, dc_bits, ac_vlc, ac_bits, |
231 |
|
✗ |
dc_pred + plane, dequant[!(b < 4)], |
232 |
|
✗ |
frame->data[plane] + y*frame->linesize[plane] + x, |
233 |
|
|
frame->linesize[plane]); |
234 |
|
|
} |
235 |
|
✗ |
if (ret < 0) |
236 |
|
✗ |
return ret; |
237 |
|
|
} |
238 |
|
✗ |
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) { |
239 |
|
✗ |
for (int mb_y = 0; mb_y < (avctx->height + 15) / 16; mb_y++) |
240 |
|
✗ |
for (int mb_x = 0; mb_x < (avctx->width + 15) / 16; mb_x++) |
241 |
|
✗ |
for (int b = 0; b < (yuv20p_half ? 4 : 6); b++) { |
242 |
|
✗ |
int luma_block = yuv20p_half ? 2 : 4; |
243 |
|
✗ |
const VLCElem * dc_vlc = b < luma_block ? luma_dc_vlc : chroma_dc_vlc; |
244 |
|
✗ |
int dc_bits = b < luma_block ? LUMA_DC_BITS : CHROMA_DC_BITS; |
245 |
|
✗ |
const VLCElem * ac_vlc = b < luma_block ? luma_ac_vlc : chroma_ac_vlc; |
246 |
|
✗ |
int ac_bits = b < luma_block ? LUMA_AC_BITS : CHROMA_AC_BITS; |
247 |
|
✗ |
int plane = b < luma_block ? 0 : b - (yuv20p_half ? 1 : 3); |
248 |
|
|
int x, y; |
249 |
|
|
|
250 |
|
✗ |
if (b < luma_block) { |
251 |
|
✗ |
y = 16*mb_y + 8*(b >> 1); |
252 |
|
✗ |
x = 16*mb_x + 8*(b & 1); |
253 |
|
|
} else { |
254 |
|
✗ |
y = 8*mb_y; |
255 |
|
✗ |
x = 8*mb_x; |
256 |
|
|
} |
257 |
|
|
|
258 |
|
✗ |
ret = decode_block(s, &gb, dc_vlc, dc_bits, ac_vlc, ac_bits, |
259 |
|
✗ |
dc_pred + plane, dequant[!(b < 4)], |
260 |
|
✗ |
frame->data[plane] + y*frame->linesize[plane] + x, |
261 |
|
✗ |
(yuv20p_half && b < 2 ? 2 : 1) * frame->linesize[plane]); |
262 |
|
✗ |
if (ret < 0) |
263 |
|
✗ |
return ret; |
264 |
|
|
|
265 |
|
✗ |
if (yuv20p_half && b < 2) |
266 |
|
✗ |
copy_block8(frame->data[plane] + (y + 1)*frame->linesize[plane] + x, |
267 |
|
✗ |
frame->data[plane] + y*frame->linesize[plane] + x, |
268 |
|
✗ |
2*frame->linesize[plane], 2*frame->linesize[plane], 8); |
269 |
|
|
} |
270 |
|
|
} else { |
271 |
|
✗ |
for (int f = 0; f < fields; f++) |
272 |
|
✗ |
for (int j = 0; j < (avctx->height + 7) / fields / 8; j++) |
273 |
|
✗ |
for (int i = 0; i < (avctx->width + 7) / 8; i++) |
274 |
|
✗ |
for (int plane = 0; plane < 3; plane++) { |
275 |
|
✗ |
const VLCElem * dc_vlc = !plane ? luma_dc_vlc : chroma_dc_vlc; |
276 |
|
✗ |
int dc_bits = !plane ? LUMA_DC_BITS : CHROMA_DC_BITS; |
277 |
|
✗ |
const VLCElem * ac_vlc = !plane ? luma_ac_vlc : chroma_ac_vlc; |
278 |
|
✗ |
int ac_bits = !plane ? LUMA_AC_BITS : CHROMA_AC_BITS; |
279 |
|
|
|
280 |
|
✗ |
ret = decode_block(s, &gb, dc_vlc, dc_bits, ac_vlc, ac_bits, |
281 |
|
✗ |
dc_pred + plane, dequant[!!plane], |
282 |
|
✗ |
frame->data[plane] + (f + 8*j*fields)*frame->linesize[plane] + 8*i, |
283 |
|
✗ |
fields * frame->linesize[plane]); |
284 |
|
✗ |
if (ret < 0) |
285 |
|
✗ |
return ret; |
286 |
|
|
} |
287 |
|
|
} |
288 |
|
|
|
289 |
|
✗ |
*got_frame = 1; |
290 |
|
|
|
291 |
|
✗ |
return avpkt->size; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
✗ |
static av_cold int lead_decode_end(AVCodecContext * avctx) |
295 |
|
|
{ |
296 |
|
✗ |
LeadContext *s = avctx->priv_data; |
297 |
|
|
|
298 |
|
✗ |
av_freep(&s->bitstream_buf); |
299 |
|
|
|
300 |
|
✗ |
return 0; |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
const FFCodec ff_lead_decoder = { |
304 |
|
|
.p.name = "lead", |
305 |
|
|
CODEC_LONG_NAME("LEAD MCMP"), |
306 |
|
|
.p.type = AVMEDIA_TYPE_VIDEO, |
307 |
|
|
.p.id = AV_CODEC_ID_LEAD, |
308 |
|
|
.priv_data_size = sizeof(LeadContext), |
309 |
|
|
.init = lead_decode_init, |
310 |
|
|
.close = lead_decode_end, |
311 |
|
|
FF_CODEC_DECODE_CB(lead_decode_frame), |
312 |
|
|
.p.capabilities = AV_CODEC_CAP_DR1, |
313 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
314 |
|
|
}; |
315 |
|
|
|