0
|
1 /*
|
|
2 * libbpg
|
33
|
3 *
|
0
|
4 * Copyright (c) 2014 Fabrice Bellard
|
|
5 *
|
|
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7 * of this software and associated documentation files (the "Software"), to deal
|
|
8 * in the Software without restriction, including without limitation the rights
|
|
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 * copies of the Software, and to permit persons to whom the Software is
|
|
11 * furnished to do so, subject to the following conditions:
|
|
12 *
|
|
13 * The above copyright notice and this permission notice shall be included in
|
|
14 * all copies or substantial portions of the Software.
|
|
15 *
|
|
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22 * THE SOFTWARE.
|
|
23 */
|
|
24 #include <math.h>
|
|
25 #ifdef EMSCRIPTEN
|
|
26 #include <emscripten.h>
|
|
27 #endif
|
|
28
|
|
29 #include <libavutil/opt.h>
|
|
30 #include <libavcodec/avcodec.h>
|
|
31 #include <libavutil/common.h>
|
|
32
|
|
33 /* The following global defines are used:
|
|
34 - USE_VAR_BIT_DEPTH : support of bit depth > 8 bits
|
33
|
35 - USE_PRED : support of animations
|
0
|
36 */
|
33
|
37
|
0
|
38 #ifndef EMSCRIPTEN
|
|
39 #define USE_RGB48 /* support all pixel formats */
|
|
40 //#define DEBUG
|
|
41 #endif
|
|
42
|
|
43 #if !defined(DEBUG)
|
|
44 #define NDEBUG
|
|
45 #endif
|
|
46
|
|
47 #include <assert.h>
|
|
48 #include "libbpg.h"
|
|
49
|
|
50 #define BPG_HEADER_MAGIC 0x425047fb
|
|
51
|
33
|
52 #define ITAPS2 4
|
0
|
53 #define ITAPS (2 * ITAPS2) /* number of taps of the interpolation filter */
|
|
54
|
|
55 #ifdef USE_VAR_BIT_DEPTH
|
|
56 typedef uint16_t PIXEL;
|
|
57 #else
|
|
58 typedef uint8_t PIXEL;
|
|
59 #endif
|
|
60
|
|
61 #define MAX_DATA_SIZE ((1 << 30) - 1)
|
|
62
|
|
63 typedef struct {
|
|
64 int c_shift;
|
|
65 int c_rnd;
|
|
66 int c_one;
|
|
67 int y_one, y_offset;
|
|
68 int c_r_cr, c_g_cb, c_g_cr, c_b_cb;
|
|
69 int c_center;
|
|
70 int bit_depth;
|
|
71 int limited_range;
|
|
72 } ColorConvertState;
|
|
73
|
33
|
74 typedef void ColorConvertFunc(ColorConvertState *s,
|
0
|
75 uint8_t *dst, const PIXEL *y_ptr,
|
|
76 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
77 int n, int incr);
|
|
78
|
|
79 struct BPGDecoderContext {
|
|
80 AVCodecContext *dec_ctx;
|
|
81 AVCodecContext *alpha_dec_ctx;
|
|
82 AVFrame *frame;
|
|
83 AVFrame *alpha_frame;
|
|
84 int w, h;
|
|
85 BPGImageFormatEnum format;
|
|
86 uint8_t c_h_phase; /* only used for 422 and 420 */
|
|
87 uint8_t has_alpha; /* true if alpha or W plane */
|
|
88 uint8_t bit_depth;
|
|
89 uint8_t has_w_plane;
|
|
90 uint8_t limited_range;
|
|
91 uint8_t premultiplied_alpha;
|
|
92 uint8_t has_animation;
|
|
93 BPGColorSpaceEnum color_space;
|
|
94 uint8_t keep_extension_data; /* true if the extension data must be
|
|
95 kept during parsing */
|
|
96 uint8_t decode_animation; /* true if animation decoding is enabled */
|
|
97 BPGExtensionData *first_md;
|
|
98
|
|
99 /* animation */
|
|
100 uint16_t loop_count;
|
|
101 uint16_t frame_delay_num;
|
|
102 uint16_t frame_delay_den;
|
|
103 uint8_t *input_buf;
|
|
104 int input_buf_pos;
|
|
105 int input_buf_len;
|
|
106
|
|
107 /* the following is used for format conversion */
|
|
108 uint8_t output_inited;
|
|
109 BPGDecoderOutputFormat out_fmt;
|
|
110 uint8_t is_rgba;
|
|
111 uint8_t is_16bpp;
|
|
112 uint8_t is_cmyk;
|
|
113 int y; /* current line */
|
|
114 int w2, h2;
|
|
115 const uint8_t *y_buf, *cb_buf, *cr_buf, *a_buf;
|
|
116 int y_linesize, cb_linesize, cr_linesize, a_linesize;
|
|
117 PIXEL *cb_buf2, *cr_buf2, *cb_buf3[ITAPS], *cr_buf3[ITAPS];
|
|
118 int16_t *c_buf4;
|
|
119 ColorConvertState cvt;
|
|
120 ColorConvertFunc *cvt_func;
|
|
121 };
|
|
122
|
|
123 /* ffmpeg utilities */
|
|
124 #ifdef USE_AV_LOG
|
|
125 void av_log(void* avcl, int level, const char *fmt, ...)
|
|
126 {
|
|
127 #ifdef DEBUG
|
|
128 va_list ap;
|
|
129
|
|
130 va_start(ap, fmt);
|
|
131 vfprintf(stderr, fmt, ap);
|
|
132 va_end(ap);
|
|
133 #endif
|
|
134 }
|
|
135
|
|
136 void avpriv_report_missing_feature(void *avc, const char *msg, ...)
|
|
137 {
|
|
138 #ifdef DEBUG
|
|
139 va_list ap;
|
|
140
|
|
141 va_start(ap, msg);
|
|
142 vfprintf(stderr, msg, ap);
|
|
143 va_end(ap);
|
|
144 #endif
|
|
145 }
|
|
146 #endif /* USE_AV_LOG */
|
|
147
|
|
148 /* return < 0 if error, otherwise the consumed length */
|
|
149 static int get_ue32(uint32_t *pv, const uint8_t *buf, int len)
|
|
150 {
|
|
151 const uint8_t *p;
|
|
152 uint32_t v;
|
|
153 int a;
|
|
154
|
33
|
155 if (len <= 0)
|
0
|
156 return -1;
|
|
157 p = buf;
|
|
158 a = *p++;
|
|
159 len--;
|
|
160 if (a < 0x80) {
|
|
161 *pv = a;
|
|
162 return 1;
|
|
163 } else if (a == 0x80) {
|
|
164 /* we don't accept non canonical encodings */
|
|
165 return -1;
|
|
166 }
|
|
167 v = a & 0x7f;
|
|
168 for(;;) {
|
|
169 if (len <= 0)
|
|
170 return -1;
|
|
171 a = *p++;
|
|
172 len--;
|
|
173 v = (v << 7) | (a & 0x7f);
|
|
174 if (!(a & 0x80))
|
|
175 break;
|
|
176 }
|
|
177 *pv = v;
|
|
178 return p - buf;
|
|
179 }
|
|
180
|
|
181 static int get_ue(uint32_t *pv, const uint8_t *buf, int len)
|
|
182 {
|
|
183 int ret;
|
|
184 ret = get_ue32(pv, buf, len);
|
|
185 if (ret < 0)
|
|
186 return ret;
|
|
187 /* limit the maximum size to avoid overflows in buffer
|
|
188 computations */
|
|
189 if (*pv > MAX_DATA_SIZE)
|
|
190 return -1;
|
|
191 return ret;
|
|
192 }
|
|
193
|
|
194 static int build_msps(uint8_t **pbuf, int *pbuf_len,
|
31
|
195 const uint8_t *input_data, uint32_t input_data_len1,
|
0
|
196 int width, int height, int chroma_format_idc,
|
|
197 int bit_depth)
|
|
198 {
|
|
199 int idx, msps_len, ret, buf_len, i;
|
31
|
200 uint32_t len, input_data_len = input_data_len1;
|
0
|
201 uint8_t *buf, *msps_buf;
|
|
202
|
|
203 *pbuf = NULL;
|
|
204
|
|
205 /* build the modified SPS header to please libavcodec */
|
|
206 ret = get_ue(&len, input_data, input_data_len);
|
|
207 if (ret < 0)
|
|
208 return -1;
|
|
209 input_data += ret;
|
|
210 input_data_len -= ret;
|
33
|
211
|
0
|
212 if (len > input_data_len)
|
|
213 return -1;
|
|
214
|
|
215 msps_len = 1 + 4 + 4 + 1 + len;
|
|
216 msps_buf = av_malloc(msps_len);
|
|
217 idx = 0;
|
|
218 msps_buf[idx++] = chroma_format_idc;
|
|
219 msps_buf[idx++] = (width >> 24);
|
|
220 msps_buf[idx++] = (width >> 16);
|
|
221 msps_buf[idx++] = (width >> 8);
|
|
222 msps_buf[idx++] = (width >> 0);
|
|
223 msps_buf[idx++] = (height >> 24);
|
|
224 msps_buf[idx++] = (height >> 16);
|
|
225 msps_buf[idx++] = (height >> 8);
|
|
226 msps_buf[idx++] = (height >> 0);
|
|
227 msps_buf[idx++] = bit_depth - 8;
|
|
228 memcpy(msps_buf + idx, input_data, len);
|
|
229 idx += len;
|
|
230 assert(idx == msps_len);
|
|
231 input_data += len;
|
|
232 input_data_len -= len;
|
33
|
233
|
0
|
234 buf_len = 4 + 2 + msps_len * 2 + 4 + (input_data_len - len);
|
|
235 buf = av_malloc(buf_len);
|
|
236
|
|
237 idx = 0;
|
|
238 /* NAL header */
|
|
239 buf[idx++] = 0x00;
|
|
240 buf[idx++] = 0x00;
|
|
241 buf[idx++] = 0x00;
|
33
|
242 buf[idx++] = 0x01;
|
0
|
243 buf[idx++] = (48 << 1); /* application specific NAL unit type */
|
|
244 buf[idx++] = 1;
|
|
245
|
|
246 /* add the modified SPS with the correct escape codes */
|
|
247 i = 0;
|
|
248 while (i < msps_len) {
|
|
249 if ((i + 1) < msps_len && msps_buf[i] == 0 && msps_buf[i + 1] == 0) {
|
|
250 buf[idx++] = 0x00;
|
|
251 buf[idx++] = 0x00;
|
|
252 buf[idx++] = 0x03;
|
|
253 i += 2;
|
|
254 } else {
|
|
255 buf[idx++] = msps_buf[i++];
|
|
256 }
|
|
257 }
|
|
258 /* the last byte cannot be 0 */
|
|
259 if (idx == 0 || buf[idx - 1] == 0x00)
|
|
260 buf[idx++] = 0x80;
|
|
261 av_free(msps_buf);
|
33
|
262
|
0
|
263 *pbuf_len = idx;
|
|
264 *pbuf = buf;
|
|
265 return input_data_len1 - input_data_len;
|
|
266 }
|
|
267
|
|
268 /* return the position of the end of the NAL or -1 if error */
|
|
269 static int find_nal_end(const uint8_t *buf, int buf_len, int has_startcode)
|
|
270 {
|
|
271 int idx;
|
|
272
|
|
273 idx = 0;
|
|
274 if (has_startcode) {
|
|
275 if (buf_len >= 4 &&
|
|
276 buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1) {
|
|
277 idx = 4;
|
|
278 } else if (buf_len >= 3 &&
|
|
279 buf[0] == 0 && buf[1] == 0 && buf[2] == 1) {
|
|
280 idx = 3;
|
|
281 } else {
|
|
282 return -1;
|
|
283 }
|
|
284 }
|
|
285 /* NAL header */
|
|
286 if (idx + 2 > buf_len)
|
|
287 return -1;
|
|
288 /* find the last byte */
|
|
289 for(;;) {
|
|
290 if (idx + 2 >= buf_len) {
|
|
291 idx = buf_len;
|
|
292 break;
|
|
293 }
|
|
294 if (buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 1)
|
|
295 break;
|
|
296 if (idx + 3 < buf_len &&
|
|
297 buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0 && buf[idx + 3] == 1)
|
|
298 break;
|
|
299 idx++;
|
|
300 }
|
|
301 return idx;
|
|
302 }
|
|
303
|
|
304 typedef struct {
|
|
305 uint8_t *buf;
|
|
306 int size;
|
|
307 int len;
|
|
308 } DynBuf;
|
|
309
|
|
310 static void dyn_buf_init(DynBuf *s)
|
|
311 {
|
|
312 s->buf = NULL;
|
|
313 s->size = 0;
|
|
314 s->len = 0;
|
|
315 }
|
|
316
|
|
317 static int dyn_buf_resize(DynBuf *s, int size)
|
|
318 {
|
|
319 int new_size;
|
|
320 uint8_t *new_buf;
|
|
321
|
|
322 if (size <= s->size)
|
|
323 return 0;
|
|
324 new_size = (s->size * 3) / 2;
|
|
325 if (new_size < size)
|
|
326 new_size = size;
|
|
327 new_buf = av_realloc(s->buf, new_size);
|
33
|
328 if (!new_buf)
|
0
|
329 return -1;
|
|
330 s->buf = new_buf;
|
|
331 s->size = new_size;
|
|
332 return 0;
|
|
333 }
|
|
334
|
|
335 static int dyn_buf_push(DynBuf *s, const uint8_t *data, int len)
|
|
336 {
|
|
337 if (dyn_buf_resize(s, s->len + len) < 0)
|
|
338 return -1;
|
|
339 memcpy(s->buf + s->len, data, len);
|
|
340 s->len += len;
|
|
341 return 0;
|
|
342 }
|
|
343
|
|
344 extern AVCodec ff_hevc_decoder;
|
|
345
|
|
346 static int hevc_decode_init1(DynBuf *pbuf, AVFrame **pframe,
|
33
|
347 AVCodecContext **pc,
|
0
|
348 const uint8_t *buf, int buf_len,
|
|
349 int width, int height, int chroma_format_idc,
|
|
350 int bit_depth)
|
|
351 {
|
|
352 AVCodec *codec;
|
|
353 AVCodecContext *c;
|
|
354 AVFrame *frame;
|
|
355 uint8_t *nal_buf;
|
|
356 int nal_len, ret, ret1;
|
|
357
|
|
358 ret = build_msps(&nal_buf, &nal_len, buf, buf_len,
|
|
359 width, height, chroma_format_idc, bit_depth);
|
|
360 if (ret < 0)
|
|
361 return -1;
|
|
362 ret1 = dyn_buf_push(pbuf, nal_buf, nal_len);
|
|
363 av_free(nal_buf);
|
|
364 if (ret1 < 0)
|
|
365 return -1;
|
33
|
366
|
0
|
367 codec = &ff_hevc_decoder;
|
|
368
|
|
369 c = avcodec_alloc_context3(codec);
|
33
|
370 if (!c)
|
0
|
371 return -1;
|
|
372 frame = av_frame_alloc();
|
33
|
373 if (!frame)
|
0
|
374 return -1;
|
|
375 /* for testing: use the MD5 or CRC in SEI to check the decoded bit
|
|
376 stream. */
|
33
|
377 c->err_recognition |= AV_EF_CRCCHECK;
|
0
|
378 /* open it */
|
|
379 if (avcodec_open2(c, codec, NULL) < 0) {
|
|
380 av_frame_free(&frame);
|
|
381 return -1;
|
|
382 }
|
|
383 *pc = c;
|
|
384 *pframe = frame;
|
|
385 return ret;
|
|
386 }
|
|
387
|
|
388 static int hevc_write_frame(AVCodecContext *avctx,
|
|
389 AVFrame *frame,
|
|
390 uint8_t *buf, int buf_len)
|
|
391 {
|
|
392 AVPacket avpkt;
|
|
393 int len, got_frame;
|
|
394
|
|
395 av_init_packet(&avpkt);
|
|
396 avpkt.data = (uint8_t *)buf;
|
|
397 avpkt.size = buf_len;
|
|
398 /* avoid using uninitialized data */
|
|
399 memset(buf + buf_len, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
|
400 len = avcodec_decode_video2(avctx, frame, &got_frame, &avpkt);
|
|
401 if (len < 0 || !got_frame)
|
|
402 return -1;
|
|
403 else
|
|
404 return 0;
|
|
405 }
|
|
406
|
|
407 static int hevc_decode_frame_internal(BPGDecoderContext *s,
|
|
408 DynBuf *abuf, DynBuf *cbuf,
|
|
409 const uint8_t *buf, int buf_len1,
|
|
410 int first_nal)
|
|
411 {
|
|
412 int nal_len, start, nal_buf_len, ret, nuh_layer_id, buf_len, has_alpha;
|
|
413 int nut, frame_start_found[2];
|
|
414 DynBuf *pbuf;
|
|
415 uint8_t *nal_buf;
|
|
416
|
|
417 has_alpha = (s->alpha_dec_ctx != NULL);
|
|
418 buf_len = buf_len1;
|
|
419 frame_start_found[0] = 0;
|
|
420 frame_start_found[1] = 0;
|
|
421 while (buf_len > 0) {
|
|
422 if (buf_len < (first_nal ? 3 : 0) + 2)
|
|
423 goto fail;
|
|
424 if (first_nal)
|
|
425 start = 0;
|
|
426 else
|
|
427 start = 3 + (buf[2] == 0);
|
|
428 if (buf_len < start + 3)
|
|
429 goto fail;
|
|
430 nuh_layer_id = ((buf[start] & 1) << 5) | (buf[start + 1] >> 3);
|
|
431 nut = (buf[start] >> 1) & 0x3f;
|
|
432 #if 0
|
33
|
433 printf("nal: type=%d layer_id=%d fs=%d %d\n",
|
0
|
434 nut, nuh_layer_id, frame_start_found[0], frame_start_found[1]);
|
|
435 #endif
|
|
436 /* Note: we assume the alpha and color data are correctly
|
|
437 interleaved */
|
|
438 if ((nut >= 32 && nut <= 35) || nut == 39 || nut >= 41) {
|
|
439 if (frame_start_found[0] && frame_start_found[has_alpha])
|
|
440 break;
|
33
|
441 } else if ((nut <= 9 || (nut >= 16 && nut <= 21)) &&
|
0
|
442 start + 2 < buf_len && (buf[start + 2] & 0x80)) {
|
|
443 /* first slice segment */
|
|
444 if (frame_start_found[0] && frame_start_found[has_alpha])
|
|
445 break;
|
|
446 if (has_alpha && nuh_layer_id == 1)
|
|
447 frame_start_found[1] = 1;
|
|
448 else
|
|
449 frame_start_found[0] = 1;
|
|
450 }
|
33
|
451
|
0
|
452 nal_len = find_nal_end(buf, buf_len, !first_nal);
|
|
453 if (nal_len < 0)
|
|
454 goto fail;
|
|
455 nal_buf_len = nal_len - start + 3;
|
|
456 if (has_alpha && nuh_layer_id == 1)
|
|
457 pbuf = abuf;
|
|
458 else
|
|
459 pbuf = cbuf;
|
|
460 if (dyn_buf_resize(pbuf, pbuf->len + nal_buf_len) < 0)
|
|
461 goto fail;
|
|
462 nal_buf = pbuf->buf + pbuf->len;
|
|
463 nal_buf[0] = 0x00;
|
|
464 nal_buf[1] = 0x00;
|
|
465 nal_buf[2] = 0x01;
|
|
466 memcpy(nal_buf + 3, buf + start, nal_len - start);
|
|
467 if (has_alpha && nuh_layer_id == 1)
|
|
468 nal_buf[4] &= 0x7;
|
|
469 pbuf->len += nal_buf_len;
|
|
470 buf += nal_len;
|
|
471 buf_len -= nal_len;
|
|
472 first_nal = 0;
|
|
473 }
|
33
|
474
|
0
|
475 if (s->alpha_dec_ctx) {
|
|
476 if (dyn_buf_resize(abuf, abuf->len + FF_INPUT_BUFFER_PADDING_SIZE) < 0)
|
|
477 goto fail;
|
|
478 ret = hevc_write_frame(s->alpha_dec_ctx, s->alpha_frame, abuf->buf, abuf->len);
|
|
479 if (ret < 0)
|
|
480 goto fail;
|
|
481 }
|
|
482
|
|
483 if (dyn_buf_resize(cbuf, cbuf->len + FF_INPUT_BUFFER_PADDING_SIZE) < 0)
|
|
484 goto fail;
|
|
485 ret = hevc_write_frame(s->dec_ctx, s->frame, cbuf->buf, cbuf->len);
|
|
486 if (ret < 0)
|
|
487 goto fail;
|
|
488 ret = buf_len1 - buf_len;
|
|
489 done:
|
|
490 return ret;
|
|
491 fail:
|
|
492 ret = -1;
|
|
493 goto done;
|
|
494 }
|
|
495
|
|
496 /* decode the first frame */
|
|
497 static int hevc_decode_start(BPGDecoderContext *s,
|
|
498 const uint8_t *buf, int buf_len1,
|
|
499 int width, int height, int chroma_format_idc,
|
|
500 int bit_depth, int has_alpha)
|
|
501 {
|
|
502 int ret, buf_len;
|
|
503 DynBuf abuf_s, *abuf = &abuf_s;
|
|
504 DynBuf cbuf_s, *cbuf = &cbuf_s;
|
|
505
|
|
506 dyn_buf_init(abuf);
|
|
507 dyn_buf_init(cbuf);
|
|
508
|
|
509 buf_len = buf_len1;
|
|
510 if (has_alpha) {
|
|
511 ret = hevc_decode_init1(abuf, &s->alpha_frame, &s->alpha_dec_ctx,
|
|
512 buf, buf_len, width, height, 0, bit_depth);
|
|
513 if (ret < 0)
|
|
514 goto fail;
|
|
515 buf += ret;
|
|
516 buf_len -= ret;
|
|
517 }
|
33
|
518
|
0
|
519 ret = hevc_decode_init1(cbuf, &s->frame, &s->dec_ctx,
|
33
|
520 buf, buf_len, width, height, chroma_format_idc,
|
0
|
521 bit_depth);
|
|
522 if (ret < 0)
|
|
523 goto fail;
|
|
524 buf += ret;
|
|
525 buf_len -= ret;
|
33
|
526
|
0
|
527 ret = hevc_decode_frame_internal(s, abuf, cbuf, buf, buf_len, 1);
|
|
528 av_free(abuf->buf);
|
|
529 av_free(cbuf->buf);
|
|
530 if (ret < 0)
|
|
531 goto fail;
|
|
532 buf_len -= ret;
|
|
533 return buf_len1 - buf_len;
|
|
534 fail:
|
|
535 return -1;
|
|
536 }
|
|
537
|
|
538 #ifdef USE_PRED
|
|
539 static int hevc_decode_frame(BPGDecoderContext *s,
|
|
540 const uint8_t *buf, int buf_len)
|
|
541 {
|
|
542 int ret;
|
|
543 DynBuf abuf_s, *abuf = &abuf_s;
|
|
544 DynBuf cbuf_s, *cbuf = &cbuf_s;
|
|
545
|
|
546 dyn_buf_init(abuf);
|
|
547 dyn_buf_init(cbuf);
|
|
548 ret = hevc_decode_frame_internal(s, abuf, cbuf, buf, buf_len, 0);
|
|
549 av_free(abuf->buf);
|
|
550 av_free(cbuf->buf);
|
|
551 return ret;
|
|
552 }
|
|
553 #endif
|
|
554
|
|
555 static void hevc_decode_end(BPGDecoderContext *s)
|
|
556 {
|
|
557 if (s->alpha_dec_ctx) {
|
|
558 avcodec_close(s->alpha_dec_ctx);
|
|
559 av_free(s->alpha_dec_ctx);
|
|
560 s->alpha_dec_ctx = NULL;
|
|
561 }
|
|
562 if (s->dec_ctx) {
|
|
563 avcodec_close(s->dec_ctx);
|
|
564 av_free(s->dec_ctx);
|
|
565 s->dec_ctx = NULL;
|
|
566 }
|
|
567 }
|
|
568
|
|
569 uint8_t *bpg_decoder_get_data(BPGDecoderContext *img, int *pline_size, int plane)
|
|
570 {
|
|
571 int c_count;
|
33
|
572 if (img->format == BPG_FORMAT_GRAY)
|
0
|
573 c_count = 1;
|
|
574 else
|
|
575 c_count = 3;
|
|
576 if (plane < c_count) {
|
|
577 *pline_size = img->frame->linesize[plane];
|
|
578 return img->frame->data[plane];
|
|
579 } else if (img->has_alpha && plane == c_count) {
|
|
580 *pline_size = img->alpha_frame->linesize[0];
|
|
581 return img->alpha_frame->data[0];
|
|
582 } else {
|
|
583 *pline_size = 0;
|
|
584 return NULL;
|
|
585 }
|
|
586 }
|
|
587
|
|
588 int bpg_decoder_get_info(BPGDecoderContext *img, BPGImageInfo *p)
|
|
589 {
|
|
590 if (!img->frame)
|
|
591 return -1;
|
|
592 p->width = img->w;
|
|
593 p->height = img->h;
|
|
594 p->format = img->format;
|
|
595 p->has_alpha = img->has_alpha && !img->has_w_plane;
|
|
596 p->premultiplied_alpha = img->premultiplied_alpha;
|
|
597 p->has_w_plane = img->has_w_plane;
|
|
598 p->limited_range = img->limited_range;
|
|
599 p->color_space = img->color_space;
|
|
600 p->bit_depth = img->bit_depth;
|
|
601 p->has_animation = img->has_animation;
|
|
602 p->loop_count = img->loop_count;
|
|
603 return 0;
|
|
604 }
|
|
605
|
|
606 static inline int clamp_pix(int a, int pixel_max)
|
|
607 {
|
|
608 if (a < 0)
|
|
609 return 0;
|
|
610 else if (a > pixel_max)
|
|
611 return pixel_max;
|
|
612 else
|
|
613 return a;
|
|
614 }
|
|
615
|
|
616 static inline int clamp8(int a)
|
|
617 {
|
|
618 if (a < 0)
|
|
619 return 0;
|
|
620 else if (a > 255)
|
|
621 return 255;
|
|
622 else
|
|
623 return a;
|
|
624 }
|
|
625
|
|
626 /* 8 tap Lanczos interpolator (phase=0, symmetric) */
|
|
627 #define IP0C0 40
|
|
628 #define IP0C1 (-11)
|
|
629 #define IP0C2 4
|
|
630 #define IP0C3 (-1)
|
|
631
|
|
632 /* 7 tap Lanczos interpolator (phase=0.5) */
|
|
633 #define IP1C0 (-1)
|
|
634 #define IP1C1 4
|
|
635 #define IP1C2 (-10)
|
|
636 #define IP1C3 57
|
|
637 #define IP1C4 18
|
|
638 #define IP1C5 (-6)
|
|
639 #define IP1C6 2
|
|
640
|
|
641 /* interpolate by a factor of two assuming chroma is aligned with the
|
|
642 luma samples. */
|
|
643 static void interp2p0_simple(PIXEL *dst, const PIXEL *src, int n, int bit_depth)
|
|
644 {
|
|
645 int pixel_max;
|
|
646
|
|
647 pixel_max = (1 << bit_depth) - 1;
|
|
648 while (n >= 2) {
|
|
649 dst[0] = src[0];
|
33
|
650 dst[1] = clamp_pix(((src[-3] + src[4]) * IP0C3 +
|
|
651 (src[-2] + src[3]) * IP0C2 +
|
|
652 (src[-1] + src[2]) * IP0C1 +
|
0
|
653 (src[0] + src[1]) * IP0C0 + 32) >> 6, pixel_max);
|
|
654 dst += 2;
|
|
655 src++;
|
|
656 n -= 2;
|
|
657 }
|
|
658 if (n) {
|
|
659 dst[0] = src[0];
|
|
660 }
|
|
661 }
|
|
662
|
|
663 static void interp2p0_simple16(PIXEL *dst, const int16_t *src, int n, int bit_depth)
|
|
664 {
|
|
665 int shift1, offset1, shift0, offset0, pixel_max;
|
|
666
|
|
667 pixel_max = (1 << bit_depth) - 1;
|
|
668 shift0 = 14 - bit_depth;
|
|
669 offset0 = (1 << shift0) >> 1;
|
|
670 shift1 = 20 - bit_depth;
|
|
671 offset1 = 1 << (shift1 - 1);
|
|
672
|
|
673 while (n >= 2) {
|
|
674 dst[0] = clamp_pix((src[0] + offset0) >> shift0, pixel_max);
|
33
|
675 dst[1] = clamp_pix(((src[-3] + src[4]) * IP0C3 +
|
|
676 (src[-2] + src[3]) * IP0C2 +
|
|
677 (src[-1] + src[2]) * IP0C1 +
|
0
|
678 (src[0] + src[1]) * IP0C0 + offset1) >> shift1,
|
|
679 pixel_max);
|
|
680 dst += 2;
|
|
681 src++;
|
|
682 n -= 2;
|
|
683 }
|
|
684 if (n) {
|
|
685 dst[0] = clamp_pix((src[0] + offset0) >> shift0, pixel_max);
|
|
686 }
|
|
687 }
|
|
688
|
|
689 /* interpolate by a factor of two assuming chroma is between the luma
|
|
690 samples. */
|
|
691 static void interp2p1_simple(PIXEL *dst, const PIXEL *src, int n, int bit_depth)
|
|
692 {
|
|
693 int pixel_max, a0, a1, a2, a3, a4, a5, a6;
|
|
694
|
|
695 pixel_max = (1 << bit_depth) - 1;
|
|
696
|
|
697 a1 = src[-3];
|
|
698 a2 = src[-2];
|
|
699 a3 = src[-1];
|
|
700 a4 = src[0];
|
|
701 a5 = src[1];
|
|
702 a6 = src[2];
|
|
703
|
|
704 while (n >= 2) {
|
|
705 a0 = a1;
|
|
706 a1 = a2;
|
|
707 a2 = a3;
|
|
708 a3 = a4;
|
|
709 a4 = a5;
|
|
710 a5 = a6;
|
|
711 a6 = src[3];
|
33
|
712 dst[0] = clamp_pix((a0 * IP1C6 + a1 * IP1C5 + a2 * IP1C4 + a3 * IP1C3 +
|
|
713 a4 * IP1C2 + a5 * IP1C1 + a6 * IP1C0 + 32) >> 6,
|
0
|
714 pixel_max);
|
|
715 dst[1] = clamp_pix((a0 * IP1C0 + a1 * IP1C1 + a2 * IP1C2 + a3 * IP1C3 +
|
33
|
716 a4 * IP1C4 + a5 * IP1C5 + a6 * IP1C6 + 32) >> 6,
|
0
|
717 pixel_max);
|
|
718 dst += 2;
|
|
719 src++;
|
|
720 n -= 2;
|
|
721 }
|
|
722 if (n) {
|
|
723 a0 = a1;
|
|
724 a1 = a2;
|
|
725 a2 = a3;
|
|
726 a3 = a4;
|
|
727 a4 = a5;
|
|
728 a5 = a6;
|
|
729 a6 = src[3];
|
33
|
730 dst[0] = clamp_pix((a0 * IP1C6 + a1 * IP1C5 + a2 * IP1C4 + a3 * IP1C3 +
|
|
731 a4 * IP1C2 + a5 * IP1C1 + a6 * IP1C0 + 32) >> 6,
|
0
|
732 pixel_max);
|
|
733 }
|
|
734 }
|
|
735
|
33
|
736 static void interp2p1_simple16(PIXEL *dst, const int16_t *src, int n,
|
0
|
737 int bit_depth)
|
|
738 {
|
|
739 int shift, offset, pixel_max, a0, a1, a2, a3, a4, a5, a6;
|
|
740
|
|
741 pixel_max = (1 << bit_depth) - 1;
|
|
742 shift = 20 - bit_depth;
|
|
743 offset = 1 << (shift - 1);
|
|
744
|
|
745 a1 = src[-3];
|
|
746 a2 = src[-2];
|
|
747 a3 = src[-1];
|
|
748 a4 = src[0];
|
|
749 a5 = src[1];
|
|
750 a6 = src[2];
|
|
751
|
|
752 while (n >= 2) {
|
|
753 a0 = a1;
|
|
754 a1 = a2;
|
|
755 a2 = a3;
|
|
756 a3 = a4;
|
|
757 a4 = a5;
|
|
758 a5 = a6;
|
|
759 a6 = src[3];
|
|
760 dst[0] = clamp_pix((a0 * IP1C6 + a1 * IP1C5 + a2 * IP1C4 + a3 * IP1C3 +
|
|
761 a4 * IP1C2 + a5 * IP1C1 + a6 * IP1C0 + offset) >> shift,
|
|
762 pixel_max);
|
|
763 dst[1] = clamp_pix((a0 * IP1C0 + a1 * IP1C1 + a2 * IP1C2 + a3 * IP1C3 +
|
|
764 a4 * IP1C4 + a5 * IP1C5 + a6 * IP1C6 + offset) >> shift,
|
|
765 pixel_max);
|
|
766 dst += 2;
|
|
767 src++;
|
|
768 n -= 2;
|
|
769 }
|
|
770 if (n) {
|
|
771 a0 = a1;
|
|
772 a1 = a2;
|
|
773 a2 = a3;
|
|
774 a3 = a4;
|
|
775 a4 = a5;
|
|
776 a5 = a6;
|
|
777 a6 = src[3];
|
|
778 dst[0] = clamp_pix((a0 * IP1C6 + a1 * IP1C5 + a2 * IP1C4 + a3 * IP1C3 +
|
33
|
779 a4 * IP1C2 + a5 * IP1C1 + a6 * IP1C0 + offset) >> shift,
|
0
|
780 pixel_max);
|
|
781 }
|
|
782 }
|
|
783
|
|
784 /* tmp_buf is a temporary buffer of length (n2 + 2 * ITAPS2 - 1) */
|
|
785 static void interp2_h(PIXEL *dst, const PIXEL *src, int n, int bit_depth,
|
|
786 int phase, PIXEL *tmp_buf)
|
|
787 {
|
|
788 PIXEL *src1 = tmp_buf, v;
|
|
789 int i, n2;
|
|
790
|
|
791 /* add extra pixels and do the interpolation (XXX: could go faster) */
|
|
792 n2 = (n + 1) / 2;
|
|
793 memcpy(src1 + ITAPS2 - 1, src, n2 * sizeof(PIXEL));
|
|
794
|
|
795 v = src[0];
|
|
796 for(i = 0; i < ITAPS2 - 1; i++)
|
|
797 src1[i] = v;
|
|
798
|
|
799 v = src[n2 - 1];
|
|
800 for(i = 0; i < ITAPS2; i++)
|
|
801 src1[ITAPS2 - 1 + n2 + i] = v;
|
|
802 if (phase == 0)
|
|
803 interp2p0_simple(dst, src1 + ITAPS2 - 1, n, bit_depth);
|
|
804 else
|
|
805 interp2p1_simple(dst, src1 + ITAPS2 - 1, n, bit_depth);
|
|
806 }
|
|
807
|
|
808 /* y_pos is the position of the sample '0' in the 'src' circular
|
|
809 buffer. tmp_buf is a temporary buffer of length (n2 + 2 * ITAPS2 - 1) */
|
|
810 static void interp2_vh(PIXEL *dst, PIXEL **src, int n, int y_pos,
|
|
811 int16_t *tmp_buf, int bit_depth, int frac_pos,
|
|
812 int c_h_phase)
|
|
813 {
|
|
814 const PIXEL *src0, *src1, *src2, *src3, *src4, *src5, *src6;
|
|
815 int i, n2, shift, rnd;
|
|
816 int16_t v;
|
|
817
|
|
818 src0 = src[(y_pos - 3) & 7];
|
|
819 src1 = src[(y_pos - 2) & 7];
|
|
820 src2 = src[(y_pos - 1) & 7];
|
|
821 src3 = src[(y_pos + 0) & 7];
|
|
822 src4 = src[(y_pos + 1) & 7];
|
|
823 src5 = src[(y_pos + 2) & 7];
|
|
824 src6 = src[(y_pos + 3) & 7];
|
|
825
|
|
826 /* vertical interpolation first */
|
|
827 shift = bit_depth - 8;
|
|
828 rnd = (1 << shift) >> 1;
|
|
829 n2 = (n + 1) / 2;
|
|
830 if (frac_pos == 0) {
|
|
831 for(i = 0; i < n2; i++) {
|
33
|
832 tmp_buf[ITAPS2 - 1 + i] =
|
|
833 (src0[i] * IP1C6 + src1[i] * IP1C5 +
|
|
834 src2[i] * IP1C4 + src3[i] * IP1C3 +
|
|
835 src4[i] * IP1C2 + src5[i] * IP1C1 +
|
0
|
836 src6[i] * IP1C0 + rnd) >> shift;
|
|
837 }
|
|
838 } else {
|
|
839 for(i = 0; i < n2; i++) {
|
33
|
840 tmp_buf[ITAPS2 - 1 + i] =
|
|
841 (src0[i] * IP1C0 + src1[i] * IP1C1 +
|
|
842 src2[i] * IP1C2 + src3[i] * IP1C3 +
|
|
843 src4[i] * IP1C4 + src5[i] * IP1C5 +
|
0
|
844 src6[i] * IP1C6 + rnd) >> shift;
|
|
845 }
|
|
846 }
|
|
847
|
|
848 /* then horizontal interpolation */
|
|
849 v = tmp_buf[ITAPS2 - 1];
|
|
850 for(i = 0; i < ITAPS2 - 1; i++)
|
|
851 tmp_buf[i] = v;
|
|
852 v = tmp_buf[ITAPS2 - 1 + n2 - 1];
|
|
853 for(i = 0; i < ITAPS2; i++)
|
|
854 tmp_buf[ITAPS2 - 1 + n2 + i] = v;
|
|
855 if (c_h_phase == 0)
|
|
856 interp2p0_simple16(dst, tmp_buf + ITAPS2 - 1, n, bit_depth);
|
|
857 else
|
|
858 interp2p1_simple16(dst, tmp_buf + ITAPS2 - 1, n, bit_depth);
|
|
859 }
|
|
860
|
|
861 static void ycc_to_rgb24(ColorConvertState *s, uint8_t *dst, const PIXEL *y_ptr,
|
|
862 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
863 int n, int incr)
|
|
864 {
|
|
865 uint8_t *q = dst;
|
|
866 int y_val, cb_val, cr_val, x;
|
|
867 int c_r_cr, c_g_cb, c_g_cr, c_b_cb, rnd, shift, center, c_one;
|
|
868
|
|
869 c_r_cr = s->c_r_cr;
|
|
870 c_g_cb = s->c_g_cb;
|
|
871 c_g_cr = s->c_g_cr;
|
|
872 c_b_cb = s->c_b_cb;
|
|
873 c_one = s->y_one;
|
|
874 rnd = s->y_offset;
|
|
875 shift = s->c_shift;
|
|
876 center = s->c_center;
|
|
877 for(x = 0; x < n; x++) {
|
|
878 y_val = y_ptr[x] * c_one;
|
|
879 cb_val = cb_ptr[x] - center;
|
|
880 cr_val = cr_ptr[x] - center;
|
|
881 q[0] = clamp8((y_val + c_r_cr * cr_val + rnd) >> shift);
|
|
882 q[1] = clamp8((y_val - c_g_cb * cb_val - c_g_cr * cr_val + rnd) >> shift);
|
|
883 q[2] = clamp8((y_val + c_b_cb * cb_val + rnd) >> shift);
|
|
884 q += incr;
|
|
885 }
|
|
886 }
|
|
887
|
33
|
888 static void ycgco_to_rgb24(ColorConvertState *s,
|
0
|
889 uint8_t *dst, const PIXEL *y_ptr,
|
|
890 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
891 int n, int incr)
|
|
892 {
|
|
893 uint8_t *q = dst;
|
|
894 int y_val, cb_val, cr_val, x;
|
|
895 int rnd, shift, center, c_one;
|
|
896
|
|
897 c_one = s->y_one;
|
|
898 rnd = s->y_offset;
|
|
899 shift = s->c_shift;
|
|
900 center = s->c_center;
|
|
901 for(x = 0; x < n; x++) {
|
|
902 y_val = y_ptr[x];
|
|
903 cb_val = cb_ptr[x] - center;
|
|
904 cr_val = cr_ptr[x] - center;
|
|
905 q[0] = clamp8(((y_val - cb_val + cr_val) * c_one + rnd) >> shift);
|
|
906 q[1] = clamp8(((y_val + cb_val) * c_one + rnd) >> shift);
|
|
907 q[2] = clamp8(((y_val - cb_val - cr_val) * c_one + rnd) >> shift);
|
|
908 q += incr;
|
|
909 }
|
|
910 }
|
|
911
|
|
912 /* c = c * alpha */
|
33
|
913 static void alpha_combine8(ColorConvertState *s,
|
0
|
914 uint8_t *dst, const PIXEL *a_ptr, int n, int incr)
|
|
915 {
|
|
916 uint8_t *q = dst;
|
|
917 int x, a_val, shift, rnd;
|
|
918
|
|
919 shift = s->bit_depth;
|
|
920 rnd = 1 << (shift - 1);
|
|
921 for(x = 0; x < n; x++) {
|
|
922 a_val = a_ptr[x];
|
|
923 /* XXX: not accurate enough */
|
|
924 q[0] = (q[0] * a_val + rnd) >> shift;
|
|
925 q[1] = (q[1] * a_val + rnd) >> shift;
|
|
926 q[2] = (q[2] * a_val + rnd) >> shift;
|
|
927 q += incr;
|
|
928 }
|
|
929 }
|
|
930
|
|
931 static uint32_t divide8_table[256];
|
|
932
|
|
933 #define DIV8_BITS 16
|
|
934
|
|
935 static void alpha_divide8_init(void)
|
|
936 {
|
|
937 int i;
|
|
938 for(i = 1; i < 256; i++) {
|
|
939 /* Note: the 128 is added to have 100% correct results for all
|
|
940 the values */
|
|
941 divide8_table[i] = ((255 << DIV8_BITS) + (i / 2) + 128) / i;
|
|
942 }
|
|
943 }
|
|
944
|
|
945 static inline unsigned int comp_divide8(unsigned int val, unsigned int alpha,
|
|
946 unsigned int alpha_inv)
|
|
947 {
|
|
948 if (val >= alpha)
|
|
949 return 255;
|
|
950 return (val * alpha_inv + (1 << (DIV8_BITS - 1))) >> DIV8_BITS;
|
|
951 }
|
|
952
|
|
953 /* c = c / alpha */
|
|
954 static void alpha_divide8(uint8_t *dst, int n)
|
|
955 {
|
|
956 static int inited;
|
|
957 uint8_t *q = dst;
|
|
958 int x;
|
|
959 unsigned int a_val, a_inv;
|
|
960
|
|
961 if (!inited) {
|
|
962 inited = 1;
|
|
963 alpha_divide8_init();
|
|
964 }
|
|
965
|
|
966 for(x = 0; x < n; x++) {
|
|
967 a_val = q[3];
|
|
968 if (a_val == 0) {
|
|
969 q[0] = 255;
|
|
970 q[1] = 255;
|
|
971 q[2] = 255;
|
|
972 } else {
|
|
973 a_inv = divide8_table[a_val];
|
|
974 q[0] = comp_divide8(q[0], a_val, a_inv);
|
|
975 q[1] = comp_divide8(q[1], a_val, a_inv);
|
|
976 q[2] = comp_divide8(q[2], a_val, a_inv);
|
|
977 }
|
|
978 q += 4;
|
|
979 }
|
|
980 }
|
|
981
|
33
|
982 static void gray_to_rgb24(ColorConvertState *s,
|
0
|
983 uint8_t *dst, const PIXEL *y_ptr,
|
|
984 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
985 int n, int incr)
|
|
986 {
|
|
987 uint8_t *q = dst;
|
|
988 int x, y_val, c, rnd, shift;
|
31
|
989 (void) cb_ptr;
|
|
990 (void) cr_ptr;
|
0
|
991
|
|
992 if (s->bit_depth == 8 && !s->limited_range) {
|
|
993 for(x = 0; x < n; x++) {
|
|
994 y_val = y_ptr[x];
|
|
995 q[0] = y_val;
|
|
996 q[1] = y_val;
|
|
997 q[2] = y_val;
|
|
998 q += incr;
|
|
999 }
|
|
1000 } else {
|
|
1001 c = s->y_one;
|
|
1002 rnd = s->y_offset;
|
|
1003 shift = s->c_shift;
|
|
1004 for(x = 0; x < n; x++) {
|
|
1005 y_val = clamp8((y_ptr[x] * c + rnd) >> shift);
|
|
1006 q[0] = y_val;
|
|
1007 q[1] = y_val;
|
|
1008 q[2] = y_val;
|
|
1009 q += incr;
|
|
1010 }
|
|
1011 }
|
|
1012 }
|
|
1013
|
|
1014 static void rgb_to_rgb24(ColorConvertState *s, uint8_t *dst, const PIXEL *y_ptr,
|
|
1015 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
1016 int n, int incr)
|
|
1017 {
|
|
1018 uint8_t *q = dst;
|
|
1019 int x, c, rnd, shift;
|
|
1020
|
|
1021 if (s->bit_depth == 8 && !s->limited_range) {
|
|
1022 for(x = 0; x < n; x++) {
|
|
1023 q[0] = cr_ptr[x];
|
|
1024 q[1] = y_ptr[x];
|
|
1025 q[2] = cb_ptr[x];
|
|
1026 q += incr;
|
|
1027 }
|
|
1028 } else {
|
|
1029 c = s->y_one;
|
|
1030 rnd = s->y_offset;
|
|
1031 shift = s->c_shift;
|
|
1032 for(x = 0; x < n; x++) {
|
|
1033 q[0] = clamp8((cr_ptr[x] * c + rnd) >> shift);
|
|
1034 q[1] = clamp8((y_ptr[x] * c + rnd) >> shift);
|
|
1035 q[2] = clamp8((cb_ptr[x] * c + rnd) >> shift);
|
|
1036 q += incr;
|
|
1037 }
|
|
1038 }
|
|
1039 }
|
|
1040
|
|
1041 static void put_dummy_gray8(uint8_t *dst, int n, int incr)
|
|
1042 {
|
|
1043 int x;
|
|
1044 for(x = 0; x < n; x++) {
|
|
1045 dst[0] = 0xff;
|
|
1046 dst += incr;
|
|
1047 }
|
|
1048 }
|
|
1049
|
33
|
1050 static void gray_to_gray8(ColorConvertState *s,
|
0
|
1051 uint8_t *dst, const PIXEL *y_ptr,
|
|
1052 int n, int incr)
|
|
1053 {
|
|
1054 uint8_t *q = dst;
|
|
1055 int x, y_val, c, rnd, shift;
|
|
1056
|
|
1057 if (s->bit_depth == 8) {
|
|
1058 for(x = 0; x < n; x++) {
|
|
1059 y_val = y_ptr[x];
|
|
1060 q[0] = y_val;
|
|
1061 q += incr;
|
|
1062 }
|
|
1063 } else {
|
|
1064 c = s->c_one;
|
|
1065 rnd = s->c_rnd;
|
|
1066 shift = s->c_shift;
|
|
1067 for(x = 0; x < n; x++) {
|
|
1068 y_val = (y_ptr[x] * c + rnd) >> shift;
|
|
1069 q[0] = y_val;
|
|
1070 q += incr;
|
|
1071 }
|
|
1072 }
|
|
1073 }
|
|
1074
|
|
1075 static ColorConvertFunc *cs_to_rgb24[BPG_CS_COUNT] = {
|
|
1076 ycc_to_rgb24,
|
|
1077 rgb_to_rgb24,
|
|
1078 ycgco_to_rgb24,
|
|
1079 ycc_to_rgb24,
|
|
1080 ycc_to_rgb24,
|
|
1081 };
|
|
1082
|
|
1083 #ifdef USE_RGB48
|
|
1084
|
|
1085 /* 16 bit output */
|
|
1086
|
|
1087 static inline int clamp16(int a)
|
|
1088 {
|
|
1089 if (a < 0)
|
|
1090 return 0;
|
|
1091 else if (a > 65535)
|
|
1092 return 65535;
|
|
1093 else
|
|
1094 return a;
|
|
1095 }
|
|
1096
|
|
1097 static void ycc_to_rgb48(ColorConvertState *s, uint8_t *dst, const PIXEL *y_ptr,
|
|
1098 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
1099 int n, int incr)
|
|
1100 {
|
|
1101 uint16_t *q = (uint16_t *)dst;
|
|
1102 int y_val, cb_val, cr_val, x;
|
|
1103 int c_r_cr, c_g_cb, c_g_cr, c_b_cb, rnd, shift, center, c_one;
|
|
1104
|
|
1105 c_r_cr = s->c_r_cr;
|
|
1106 c_g_cb = s->c_g_cb;
|
|
1107 c_g_cr = s->c_g_cr;
|
|
1108 c_b_cb = s->c_b_cb;
|
|
1109 c_one = s->y_one;
|
|
1110 rnd = s->y_offset;
|
|
1111 shift = s->c_shift;
|
|
1112 center = s->c_center;
|
|
1113 for(x = 0; x < n; x++) {
|
|
1114 y_val = y_ptr[x] * c_one;
|
|
1115 cb_val = cb_ptr[x] - center;
|
|
1116 cr_val = cr_ptr[x] - center;
|
|
1117 q[0] = clamp16((y_val + c_r_cr * cr_val + rnd) >> shift);
|
|
1118 q[1] = clamp16((y_val - c_g_cb * cb_val - c_g_cr * cr_val + rnd) >> shift);
|
|
1119 q[2] = clamp16((y_val + c_b_cb * cb_val + rnd) >> shift);
|
|
1120 q += incr;
|
|
1121 }
|
|
1122 }
|
|
1123
|
33
|
1124 static void ycgco_to_rgb48(ColorConvertState *s,
|
0
|
1125 uint8_t *dst, const PIXEL *y_ptr,
|
|
1126 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
1127 int n, int incr)
|
|
1128 {
|
|
1129 uint16_t *q = (uint16_t *)dst;
|
|
1130 int y_val, cb_val, cr_val, x;
|
|
1131 int rnd, shift, center, c_one;
|
|
1132
|
|
1133 c_one = s->y_one;
|
|
1134 rnd = s->y_offset;
|
|
1135 shift = s->c_shift;
|
|
1136 center = s->c_center;
|
|
1137 for(x = 0; x < n; x++) {
|
|
1138 y_val = y_ptr[x];
|
|
1139 cb_val = cb_ptr[x] - center;
|
|
1140 cr_val = cr_ptr[x] - center;
|
|
1141 q[0] = clamp16(((y_val - cb_val + cr_val) * c_one + rnd) >> shift);
|
|
1142 q[1] = clamp16(((y_val + cb_val) * c_one + rnd) >> shift);
|
|
1143 q[2] = clamp16(((y_val - cb_val - cr_val) * c_one + rnd) >> shift);
|
|
1144 q += incr;
|
|
1145 }
|
|
1146 }
|
|
1147
|
33
|
1148 static void gray_to_rgb48(ColorConvertState *s,
|
0
|
1149 uint8_t *dst, const PIXEL *y_ptr,
|
|
1150 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
1151 int n, int incr)
|
|
1152 {
|
|
1153 uint16_t *q = (uint16_t *)dst;
|
|
1154 int x, y_val, c, rnd, shift;
|
31
|
1155 (void) cb_ptr;
|
|
1156 (void) cr_ptr;
|
0
|
1157
|
|
1158 c = s->y_one;
|
|
1159 rnd = s->y_offset;
|
|
1160 shift = s->c_shift;
|
|
1161 for(x = 0; x < n; x++) {
|
|
1162 y_val = clamp16((y_ptr[x] * c + rnd) >> shift);
|
|
1163 q[0] = y_val;
|
|
1164 q[1] = y_val;
|
|
1165 q[2] = y_val;
|
|
1166 q += incr;
|
|
1167 }
|
|
1168 }
|
|
1169
|
33
|
1170 static void gray_to_gray16(ColorConvertState *s,
|
0
|
1171 uint16_t *dst, const PIXEL *y_ptr,
|
|
1172 int n, int incr)
|
|
1173 {
|
|
1174 uint16_t *q = dst;
|
|
1175 int x, y_val, c, rnd, shift;
|
|
1176
|
|
1177 c = s->c_one;
|
|
1178 rnd = s->c_rnd;
|
|
1179 shift = s->c_shift;
|
|
1180 for(x = 0; x < n; x++) {
|
|
1181 y_val = (y_ptr[x] * c + rnd) >> shift;
|
|
1182 q[0] = y_val;
|
|
1183 q += incr;
|
|
1184 }
|
|
1185 }
|
|
1186
|
33
|
1187 static void luma_to_gray16(ColorConvertState *s,
|
0
|
1188 uint16_t *dst, const PIXEL *y_ptr,
|
|
1189 int n, int incr)
|
|
1190 {
|
|
1191 uint16_t *q = dst;
|
|
1192 int x, y_val, c, rnd, shift;
|
|
1193
|
|
1194 c = s->y_one;
|
|
1195 rnd = s->y_offset;
|
|
1196 shift = s->c_shift;
|
|
1197 for(x = 0; x < n; x++) {
|
|
1198 y_val = clamp16((y_ptr[x] * c + rnd) >> shift);
|
|
1199 q[0] = y_val;
|
|
1200 q += incr;
|
|
1201 }
|
|
1202 }
|
|
1203
|
33
|
1204 static void rgb_to_rgb48(ColorConvertState *s,
|
0
|
1205 uint8_t *dst, const PIXEL *y_ptr,
|
|
1206 const PIXEL *cb_ptr, const PIXEL *cr_ptr,
|
|
1207 int n, int incr)
|
|
1208 {
|
|
1209 luma_to_gray16(s, (uint16_t *)dst + 1, y_ptr, n, incr);
|
|
1210 luma_to_gray16(s, (uint16_t *)dst + 2, cb_ptr, n, incr);
|
|
1211 luma_to_gray16(s, (uint16_t *)dst + 0, cr_ptr, n, incr);
|
|
1212 }
|
|
1213
|
|
1214 static void put_dummy_gray16(uint16_t *dst, int n, int incr)
|
|
1215 {
|
|
1216 int x;
|
|
1217 for(x = 0; x < n; x++) {
|
|
1218 dst[0] = 0xffff;
|
|
1219 dst += incr;
|
|
1220 }
|
|
1221 }
|
|
1222
|
|
1223 /* c = c * alpha */
|
33
|
1224 static void alpha_combine16(ColorConvertState *s,
|
0
|
1225 uint16_t *dst, const PIXEL *a_ptr, int n, int incr)
|
|
1226 {
|
|
1227 uint16_t *q = dst;
|
|
1228 int x, a_val, shift, rnd;
|
|
1229
|
|
1230 shift = s->bit_depth;
|
|
1231 rnd = 1 << (shift - 1);
|
|
1232 for(x = 0; x < n; x++) {
|
|
1233 a_val = a_ptr[x];
|
|
1234 /* XXX: not accurate enough */
|
|
1235 q[0] = (q[0] * a_val + rnd) >> shift;
|
|
1236 q[1] = (q[1] * a_val + rnd) >> shift;
|
|
1237 q[2] = (q[2] * a_val + rnd) >> shift;
|
|
1238 q += incr;
|
|
1239 }
|
|
1240 }
|
|
1241
|
|
1242 #define DIV16_BITS 15
|
|
1243
|
|
1244 static unsigned int comp_divide16(unsigned int val, unsigned int alpha,
|
|
1245 unsigned int alpha_inv)
|
|
1246 {
|
|
1247 if (val >= alpha)
|
|
1248 return 65535;
|
|
1249 return (val * alpha_inv + (1 << (DIV16_BITS - 1))) >> DIV16_BITS;
|
|
1250 }
|
|
1251
|
|
1252 /* c = c / alpha */
|
|
1253 static void alpha_divide16(uint16_t *dst, int n)
|
|
1254 {
|
|
1255 uint16_t *q = dst;
|
|
1256 int x;
|
|
1257 unsigned int a_val, a_inv;
|
|
1258
|
|
1259 for(x = 0; x < n; x++) {
|
|
1260 a_val = q[3];
|
|
1261 if (a_val == 0) {
|
|
1262 q[0] = 65535;
|
|
1263 q[1] = 65535;
|
|
1264 q[2] = 65535;
|
|
1265 } else {
|
|
1266 a_inv = ((65535 << DIV16_BITS) + (a_val / 2)) / a_val;
|
|
1267 q[0] = comp_divide16(q[0], a_val, a_inv);
|
|
1268 q[1] = comp_divide16(q[1], a_val, a_inv);
|
|
1269 q[2] = comp_divide16(q[2], a_val, a_inv);
|
|
1270 }
|
|
1271 q += 4;
|
|
1272 }
|
|
1273 }
|
|
1274
|
|
1275 static void gray_one_minus8(uint8_t *dst, int n, int incr)
|
|
1276 {
|
|
1277 int x;
|
|
1278 for(x = 0; x < n; x++) {
|
|
1279 dst[0] = 255 - dst[0];
|
|
1280 dst += incr;
|
|
1281 }
|
|
1282 }
|
|
1283
|
|
1284 static void gray_one_minus16(uint16_t *dst, int n, int incr)
|
|
1285 {
|
|
1286 int x;
|
|
1287 for(x = 0; x < n; x++) {
|
|
1288 dst[0] = 65535 - dst[0];
|
|
1289 dst += incr;
|
|
1290 }
|
|
1291 }
|
|
1292
|
|
1293 static ColorConvertFunc *cs_to_rgb48[BPG_CS_COUNT] = {
|
|
1294 ycc_to_rgb48,
|
|
1295 rgb_to_rgb48,
|
|
1296 ycgco_to_rgb48,
|
|
1297 ycc_to_rgb48,
|
|
1298 ycc_to_rgb48,
|
|
1299 };
|
|
1300 #endif
|
|
1301
|
33
|
1302 static void convert_init(ColorConvertState *s,
|
0
|
1303 int in_bit_depth, int out_bit_depth,
|
|
1304 BPGColorSpaceEnum color_space,
|
|
1305 int limited_range)
|
|
1306 {
|
|
1307 int c_shift, in_pixel_max, out_pixel_max;
|
|
1308 double mult, k_r, k_b, mult_y, mult_c;
|
|
1309
|
|
1310 c_shift = 30 - out_bit_depth;
|
|
1311 in_pixel_max = (1 << in_bit_depth) - 1;
|
|
1312 out_pixel_max = (1 << out_bit_depth) - 1;
|
|
1313 mult = (double)out_pixel_max * (1 << c_shift) / (double)in_pixel_max;
|
|
1314 if (limited_range) {
|
33
|
1315 mult_y = (double)out_pixel_max * (1 << c_shift) /
|
0
|
1316 (double)(219 << (in_bit_depth - 8));
|
33
|
1317 mult_c = (double)out_pixel_max * (1 << c_shift) /
|
0
|
1318 (double)(224 << (in_bit_depth - 8));
|
|
1319 } else {
|
|
1320 mult_y = mult;
|
|
1321 mult_c = mult;
|
|
1322 }
|
|
1323 switch(color_space) {
|
|
1324 case BPG_CS_YCbCr:
|
|
1325 k_r = 0.299;
|
|
1326 k_b = 0.114;
|
|
1327 goto convert_ycc;
|
|
1328 case BPG_CS_YCbCr_BT709:
|
33
|
1329 k_r = 0.2126;
|
0
|
1330 k_b = 0.0722;
|
|
1331 goto convert_ycc;
|
|
1332 case BPG_CS_YCbCr_BT2020:
|
|
1333 k_r = 0.2627;
|
|
1334 k_b = 0.0593;
|
|
1335 convert_ycc:
|
|
1336 s->c_r_cr = lrint(2*(1-k_r) * mult_c);
|
|
1337 s->c_g_cb = lrint(2*k_b*(1-k_b)/(1-k_b-k_r) * mult_c);
|
|
1338 s->c_g_cr = lrint(2*k_r*(1-k_r)/(1-k_b-k_r) * mult_c);
|
|
1339 s->c_b_cb = lrint(2*(1-k_b) * mult_c);
|
|
1340 break;
|
|
1341 default:
|
|
1342 break;
|
|
1343 }
|
|
1344 s->c_one = lrint(mult);
|
|
1345 s->c_shift = c_shift;
|
|
1346 s->c_rnd = (1 << (c_shift - 1));
|
|
1347 s->c_center = 1 << (in_bit_depth - 1);
|
|
1348 if (limited_range) {
|
|
1349 s->y_one = lrint(mult_y);
|
|
1350 s->y_offset = -(16 << (in_bit_depth - 8)) * s->y_one + s->c_rnd;
|
|
1351 } else {
|
|
1352 s->y_one = s->c_one;
|
|
1353 s->y_offset = s->c_rnd;
|
|
1354 }
|
|
1355 s->bit_depth = in_bit_depth;
|
|
1356 s->limited_range = limited_range;
|
|
1357 }
|
|
1358
|
|
1359 static int bpg_decoder_output_init(BPGDecoderContext *s,
|
|
1360 BPGDecoderOutputFormat out_fmt)
|
|
1361 {
|
|
1362 int i;
|
|
1363
|
|
1364 #ifdef USE_RGB48
|
|
1365 if ((unsigned)out_fmt > BPG_OUTPUT_FORMAT_CMYK64)
|
|
1366 return -1;
|
|
1367 #else
|
|
1368 if ((unsigned)out_fmt > BPG_OUTPUT_FORMAT_RGBA32)
|
|
1369 return -1;
|
|
1370 #endif
|
|
1371 s->is_rgba = (out_fmt == BPG_OUTPUT_FORMAT_RGBA32 ||
|
|
1372 out_fmt == BPG_OUTPUT_FORMAT_RGBA64);
|
|
1373 s->is_16bpp = (out_fmt == BPG_OUTPUT_FORMAT_RGB48 ||
|
|
1374 out_fmt == BPG_OUTPUT_FORMAT_RGBA64 ||
|
|
1375 out_fmt == BPG_OUTPUT_FORMAT_CMYK64);
|
|
1376 s->is_cmyk = (out_fmt == BPG_OUTPUT_FORMAT_CMYK32 ||
|
|
1377 out_fmt == BPG_OUTPUT_FORMAT_CMYK64);
|
33
|
1378
|
0
|
1379 if (s->format == BPG_FORMAT_420 || s->format == BPG_FORMAT_422) {
|
|
1380 s->w2 = (s->w + 1) / 2;
|
|
1381 s->h2 = (s->h + 1) / 2;
|
|
1382 s->cb_buf2 = av_malloc(s->w * sizeof(PIXEL));
|
|
1383 s->cr_buf2 = av_malloc(s->w * sizeof(PIXEL));
|
|
1384 /* Note: too large if 422 and sizeof(PIXEL) = 1 */
|
|
1385 s->c_buf4 = av_malloc((s->w2 + 2 * ITAPS2 - 1) * sizeof(int16_t));
|
|
1386
|
|
1387 if (s->format == BPG_FORMAT_420) {
|
|
1388 for(i = 0; i < ITAPS; i++) {
|
|
1389 s->cb_buf3[i] = av_malloc(s->w2 * sizeof(PIXEL));
|
|
1390 s->cr_buf3[i] = av_malloc(s->w2 * sizeof(PIXEL));
|
|
1391 }
|
|
1392 }
|
|
1393 }
|
|
1394 convert_init(&s->cvt, s->bit_depth, s->is_16bpp ? 16 : 8,
|
|
1395 s->color_space, s->limited_range);
|
|
1396
|
|
1397 if (s->format == BPG_FORMAT_GRAY) {
|
|
1398 #ifdef USE_RGB48
|
|
1399 if (s->is_16bpp) {
|
|
1400 s->cvt_func = gray_to_rgb48;
|
33
|
1401 } else
|
0
|
1402 #endif
|
|
1403 {
|
|
1404 s->cvt_func = gray_to_rgb24;
|
|
1405 }
|
|
1406 } else {
|
|
1407 #ifdef USE_RGB48
|
|
1408 if (s->is_16bpp) {
|
|
1409 s->cvt_func = cs_to_rgb48[s->color_space];
|
|
1410 } else
|
|
1411 #endif
|
|
1412 {
|
|
1413 s->cvt_func = cs_to_rgb24[s->color_space];
|
|
1414 }
|
|
1415 }
|
|
1416 return 0;
|
|
1417 }
|
|
1418
|
|
1419 static void bpg_decoder_output_end(BPGDecoderContext *s)
|
|
1420 {
|
|
1421 int i;
|
|
1422
|
|
1423 av_free(s->cb_buf2);
|
|
1424 av_free(s->cr_buf2);
|
|
1425 for(i = 0; i < ITAPS; i++) {
|
|
1426 av_free(s->cb_buf3[i]);
|
|
1427 av_free(s->cr_buf3[i]);
|
|
1428 }
|
|
1429 av_free(s->c_buf4);
|
|
1430 }
|
|
1431
|
|
1432 int bpg_decoder_start(BPGDecoderContext *s, BPGDecoderOutputFormat out_fmt)
|
|
1433 {
|
|
1434 int ret, c_idx;
|
|
1435
|
|
1436 if (!s->frame)
|
|
1437 return -1;
|
33
|
1438
|
0
|
1439 if (!s->output_inited) {
|
|
1440 /* first frame is already decoded */
|
|
1441 ret = bpg_decoder_output_init(s, out_fmt);
|
|
1442 if (ret)
|
|
1443 return ret;
|
|
1444 s->output_inited = 1;
|
|
1445 s->out_fmt = out_fmt;
|
|
1446 } else {
|
|
1447 #ifdef USE_PRED
|
|
1448 if (s->has_animation && s->decode_animation) {
|
|
1449 if (out_fmt != s->out_fmt)
|
|
1450 return -1;
|
|
1451 if (s->input_buf_pos >= s->input_buf_len) {
|
|
1452 return -1;
|
|
1453 } else {
|
33
|
1454 ret = hevc_decode_frame(s, s->input_buf + s->input_buf_pos,
|
0
|
1455 s->input_buf_len - s->input_buf_pos);
|
|
1456 if (ret < 0)
|
|
1457 return -1;
|
|
1458 s->input_buf_pos += ret;
|
|
1459 }
|
33
|
1460 } else
|
0
|
1461 #endif
|
|
1462 {
|
|
1463 return -1;
|
|
1464 }
|
|
1465 }
|
|
1466 s->y_buf = bpg_decoder_get_data(s, &s->y_linesize, 0);
|
|
1467 if (s->format != BPG_FORMAT_GRAY) {
|
|
1468 s->cb_buf = bpg_decoder_get_data(s, &s->cb_linesize, 1);
|
|
1469 s->cr_buf = bpg_decoder_get_data(s, &s->cr_linesize, 2);
|
|
1470 c_idx = 3;
|
|
1471 } else {
|
|
1472 c_idx = 1;
|
|
1473 }
|
|
1474 if (s->has_alpha)
|
|
1475 s->a_buf = bpg_decoder_get_data(s, &s->a_linesize, c_idx);
|
|
1476 else
|
|
1477 s->a_buf = NULL;
|
|
1478 s->y = 0;
|
|
1479 return 0;
|
|
1480 }
|
|
1481
|
|
1482 void bpg_decoder_get_frame_duration(BPGDecoderContext *s, int *pnum, int *pden)
|
|
1483 {
|
|
1484 #ifdef USE_PRED
|
|
1485 if (s->frame && s->has_animation) {
|
|
1486 *pnum = s->frame_delay_num * (s->frame->pts);
|
|
1487 *pden = s->frame_delay_den;
|
33
|
1488 } else
|
0
|
1489 #endif
|
|
1490 {
|
|
1491 *pnum = 0;
|
|
1492 *pden = 1;
|
|
1493 }
|
|
1494 }
|
|
1495
|
|
1496 int bpg_decoder_get_line(BPGDecoderContext *s, void *rgb_line1)
|
|
1497 {
|
|
1498 uint8_t *rgb_line = rgb_line1;
|
|
1499 int w, y, pos, y2, y1, incr, y_frac;
|
|
1500 PIXEL *y_ptr, *cb_ptr, *cr_ptr, *a_ptr;
|
|
1501
|
|
1502 y = s->y;
|
33
|
1503 if ((unsigned)y >= s->h)
|
0
|
1504 return -1;
|
|
1505 w = s->w;
|
33
|
1506
|
0
|
1507 y_ptr = (PIXEL *)(s->y_buf + y * s->y_linesize);
|
|
1508 incr = 3 + (s->is_rgba || s->is_cmyk);
|
|
1509 switch(s->format) {
|
|
1510 case BPG_FORMAT_GRAY:
|
|
1511 s->cvt_func(&s->cvt, rgb_line, y_ptr, NULL, NULL, w, incr);
|
|
1512 break;
|
|
1513 case BPG_FORMAT_420:
|
|
1514 if (y == 0) {
|
|
1515 int i;
|
|
1516 /* init the vertical interpolation buffer */
|
|
1517 for(i = 0; i < ITAPS; i++) {
|
|
1518 y1 = i;
|
|
1519 if (y1 > ITAPS2)
|
|
1520 y1 -= ITAPS;
|
|
1521 if (y1 < 0)
|
|
1522 y1 = 0;
|
|
1523 else if (y1 >= s->h2)
|
|
1524 y1 = s->h2 - 1;
|
|
1525 cb_ptr = (PIXEL *)(s->cb_buf + y1 * s->cb_linesize);
|
|
1526 cr_ptr = (PIXEL *)(s->cr_buf + y1 * s->cr_linesize);
|
|
1527 memcpy(s->cb_buf3[i], cb_ptr, s->w2 * sizeof(PIXEL));
|
|
1528 memcpy(s->cr_buf3[i], cr_ptr, s->w2 * sizeof(PIXEL));
|
|
1529 }
|
|
1530 }
|
|
1531 y2 = y >> 1;
|
|
1532 pos = y2 % ITAPS;
|
|
1533 y_frac = y & 1;
|
|
1534 interp2_vh(s->cb_buf2, s->cb_buf3, w, pos, s->c_buf4,
|
|
1535 s->bit_depth, y_frac, s->c_h_phase);
|
|
1536 interp2_vh(s->cr_buf2, s->cr_buf3, w, pos, s->c_buf4,
|
|
1537 s->bit_depth, y_frac, s->c_h_phase);
|
|
1538 if (y_frac) {
|
|
1539 /* add a new line in the circular buffer */
|
|
1540 pos = (pos + ITAPS2 + 1) % ITAPS;
|
|
1541 y1 = y2 + ITAPS2 + 1;
|
|
1542 if (y1 >= s->h2)
|
|
1543 y1 = s->h2 - 1;
|
|
1544 cb_ptr = (PIXEL *)(s->cb_buf + y1 * s->cb_linesize);
|
|
1545 cr_ptr = (PIXEL *)(s->cr_buf + y1 * s->cr_linesize);
|
|
1546 memcpy(s->cb_buf3[pos], cb_ptr, s->w2 * sizeof(PIXEL));
|
|
1547 memcpy(s->cr_buf3[pos], cr_ptr, s->w2 * sizeof(PIXEL));
|
|
1548 }
|
|
1549 s->cvt_func(&s->cvt, rgb_line, y_ptr, s->cb_buf2, s->cr_buf2, w, incr);
|
|
1550 break;
|
|
1551 case BPG_FORMAT_422:
|
|
1552 cb_ptr = (PIXEL *)(s->cb_buf + y * s->cb_linesize);
|
|
1553 cr_ptr = (PIXEL *)(s->cr_buf + y * s->cr_linesize);
|
33
|
1554 interp2_h(s->cb_buf2, cb_ptr, w, s->bit_depth, s->c_h_phase,
|
0
|
1555 (PIXEL *)s->c_buf4);
|
|
1556 interp2_h(s->cr_buf2, cr_ptr, w, s->bit_depth, s->c_h_phase,
|
|
1557 (PIXEL *)s->c_buf4);
|
|
1558 s->cvt_func(&s->cvt, rgb_line, y_ptr, s->cb_buf2, s->cr_buf2, w, incr);
|
|
1559 break;
|
|
1560 case BPG_FORMAT_444:
|
|
1561 cb_ptr = (PIXEL *)(s->cb_buf + y * s->cb_linesize);
|
|
1562 cr_ptr = (PIXEL *)(s->cr_buf + y * s->cr_linesize);
|
|
1563 s->cvt_func(&s->cvt, rgb_line, y_ptr, cb_ptr, cr_ptr, w, incr);
|
|
1564 break;
|
|
1565 default:
|
|
1566 return -1;
|
|
1567 }
|
|
1568
|
|
1569 /* alpha output or CMYK handling */
|
|
1570 #ifdef USE_RGB48
|
|
1571 if (s->is_cmyk) {
|
|
1572 int i;
|
|
1573 /* convert RGBW to CMYK */
|
|
1574 if (s->is_16bpp) {
|
|
1575 if (!s->has_w_plane)
|
|
1576 put_dummy_gray16((uint16_t *)rgb_line + 3, w, 4);
|
|
1577 for(i = 0; i < 4; i++)
|
|
1578 gray_one_minus16((uint16_t *)rgb_line + i, w, 4);
|
|
1579 } else {
|
|
1580 if (!s->has_w_plane)
|
|
1581 put_dummy_gray8(rgb_line + 3, w, 4);
|
|
1582 for(i = 0; i < 4; i++)
|
|
1583 gray_one_minus8(rgb_line + i, w, 4);
|
|
1584 }
|
|
1585 } else
|
|
1586 #endif
|
|
1587 if (s->has_w_plane) {
|
|
1588 a_ptr = (PIXEL *)(s->a_buf + y * s->a_linesize);
|
|
1589 #ifdef USE_RGB48
|
|
1590 if (s->is_16bpp) {
|
|
1591 alpha_combine16(&s->cvt, (uint16_t *)rgb_line, a_ptr, w, incr);
|
|
1592 if (s->is_rgba)
|
|
1593 put_dummy_gray16((uint16_t *)rgb_line + 3, w, 4);
|
|
1594 } else
|
|
1595 #endif
|
|
1596 {
|
|
1597 alpha_combine8(&s->cvt, rgb_line, a_ptr, w, incr);
|
|
1598 if (s->is_rgba)
|
|
1599 put_dummy_gray8(rgb_line + 3, w, 4);
|
|
1600 }
|
|
1601 } else if (s->is_rgba) {
|
|
1602 #ifdef USE_RGB48
|
|
1603 if (s->is_16bpp) {
|
|
1604 if (s->has_alpha) {
|
|
1605 a_ptr = (PIXEL *)(s->a_buf + y * s->a_linesize);
|
33
|
1606 gray_to_gray16(&s->cvt,
|
0
|
1607 (uint16_t *)rgb_line + 3, a_ptr, w, 4);
|
|
1608 if (s->premultiplied_alpha)
|
|
1609 alpha_divide16((uint16_t *)rgb_line, w);
|
|
1610 } else {
|
|
1611 put_dummy_gray16((uint16_t *)rgb_line + 3, w, 4);
|
|
1612 }
|
|
1613 } else
|
|
1614 #endif
|
|
1615 {
|
|
1616 if (s->has_alpha) {
|
|
1617 a_ptr = (PIXEL *)(s->a_buf + y * s->a_linesize);
|
|
1618 gray_to_gray8(&s->cvt, rgb_line + 3, a_ptr, w, 4);
|
|
1619 if (s->premultiplied_alpha)
|
|
1620 alpha_divide8((uint8_t *)rgb_line, w);
|
|
1621 } else {
|
|
1622 put_dummy_gray8(rgb_line + 3, w, 4);
|
|
1623 }
|
|
1624 }
|
|
1625 }
|
|
1626
|
|
1627 /* go to next line */
|
|
1628 s->y++;
|
|
1629 return 0;
|
|
1630 }
|
|
1631
|
|
1632 BPGDecoderContext *bpg_decoder_open(void)
|
|
1633 {
|
|
1634 BPGDecoderContext *s;
|
|
1635
|
|
1636 s = av_mallocz(sizeof(BPGDecoderContext));
|
|
1637 if (!s)
|
|
1638 return NULL;
|
|
1639 return s;
|
|
1640 }
|
|
1641
|
|
1642 typedef struct {
|
|
1643 uint32_t width, height;
|
|
1644 BPGImageFormatEnum format;
|
|
1645 uint8_t has_alpha;
|
|
1646 uint8_t bit_depth;
|
|
1647 uint8_t has_w_plane;
|
|
1648 uint8_t premultiplied_alpha;
|
|
1649 uint8_t limited_range;
|
|
1650 uint8_t has_animation;
|
|
1651 uint16_t loop_count;
|
|
1652 uint16_t frame_delay_num;
|
|
1653 uint16_t frame_delay_den;
|
|
1654 BPGColorSpaceEnum color_space;
|
|
1655 uint32_t hevc_data_len;
|
|
1656 BPGExtensionData *first_md;
|
|
1657 } BPGHeaderData;
|
|
1658
|
|
1659 static int bpg_decode_header(BPGHeaderData *h,
|
|
1660 const uint8_t *buf, int buf_len,
|
|
1661 int header_only, int load_extensions)
|
|
1662 {
|
|
1663 int idx, flags1, flags2, has_extension, ret, alpha1_flag, alpha2_flag;
|
|
1664 uint32_t extension_data_len;
|
|
1665
|
|
1666 if (buf_len < 6)
|
|
1667 return -1;
|
|
1668 /* check magic */
|
|
1669 if (buf[0] != ((BPG_HEADER_MAGIC >> 24) & 0xff) ||
|
|
1670 buf[1] != ((BPG_HEADER_MAGIC >> 16) & 0xff) ||
|
|
1671 buf[2] != ((BPG_HEADER_MAGIC >> 8) & 0xff) ||
|
|
1672 buf[3] != ((BPG_HEADER_MAGIC >> 0) & 0xff))
|
|
1673 return -1;
|
|
1674 idx = 4;
|
|
1675 flags1 = buf[idx++];
|
|
1676 h->format = flags1 >> 5;
|
|
1677 if (h->format > 5)
|
|
1678 return -1;
|
|
1679 alpha1_flag = (flags1 >> 4) & 1;
|
|
1680 h->bit_depth = (flags1 & 0xf) + 8;
|
|
1681 if (h->bit_depth > 14)
|
|
1682 return -1;
|
|
1683 flags2 = buf[idx++];
|
|
1684 h->color_space = (flags2 >> 4) & 0xf;
|
|
1685 has_extension = (flags2 >> 3) & 1;
|
|
1686 alpha2_flag = (flags2 >> 2) & 1;
|
|
1687 h->limited_range = (flags2 >> 1) & 1;
|
|
1688 h->has_animation = flags2 & 1;
|
|
1689 h->loop_count = 0;
|
|
1690 h->frame_delay_num = 0;
|
|
1691 h->frame_delay_den = 0;
|
|
1692 h->has_alpha = 0;
|
|
1693 h->has_w_plane = 0;
|
|
1694 h->premultiplied_alpha = 0;
|
33
|
1695
|
0
|
1696 if (alpha1_flag) {
|
|
1697 h->has_alpha = 1;
|
|
1698 h->premultiplied_alpha = alpha2_flag;
|
|
1699 } else if (alpha2_flag) {
|
|
1700 h->has_alpha = 1;
|
|
1701 h->has_w_plane = 1;
|
|
1702 }
|
|
1703
|
33
|
1704 if (h->color_space >= BPG_CS_COUNT ||
|
0
|
1705 (h->format == BPG_FORMAT_GRAY && h->color_space != 0) ||
|
|
1706 (h->has_w_plane && h->format == BPG_FORMAT_GRAY))
|
|
1707 return -1;
|
|
1708 ret = get_ue(&h->width, buf + idx, buf_len - idx);
|
|
1709 if (ret < 0)
|
|
1710 return -1;
|
|
1711 idx += ret;
|
|
1712 ret = get_ue(&h->height, buf + idx, buf_len - idx);
|
|
1713 if (ret < 0)
|
|
1714 return -1;
|
|
1715 idx += ret;
|
|
1716 if (h->width == 0 || h->height == 0)
|
|
1717 return -1;
|
|
1718 if (header_only)
|
|
1719 return idx;
|
|
1720
|
|
1721 ret = get_ue(&h->hevc_data_len, buf + idx, buf_len - idx);
|
|
1722 if (ret < 0)
|
|
1723 return -1;
|
|
1724 idx += ret;
|
33
|
1725
|
0
|
1726 extension_data_len = 0;
|
|
1727 if (has_extension) {
|
|
1728 ret = get_ue(&extension_data_len, buf + idx, buf_len - idx);
|
|
1729 if (ret < 0)
|
|
1730 return -1;
|
|
1731 idx += ret;
|
|
1732 }
|
|
1733
|
|
1734 h->first_md = NULL;
|
|
1735 if (has_extension) {
|
|
1736 int ext_end;
|
|
1737
|
|
1738 ext_end = idx + extension_data_len;
|
|
1739 if (ext_end > buf_len)
|
|
1740 return -1;
|
|
1741 if (load_extensions || h->has_animation) {
|
|
1742 BPGExtensionData *md, **plast_md;
|
|
1743 uint32_t tag, buf_len;
|
|
1744
|
|
1745 plast_md = &h->first_md;
|
|
1746 while (idx < ext_end) {
|
|
1747 ret = get_ue32(&tag, buf + idx, ext_end - idx);
|
33
|
1748 if (ret < 0)
|
0
|
1749 goto fail;
|
|
1750 idx += ret;
|
|
1751
|
|
1752 ret = get_ue(&buf_len, buf + idx, ext_end - idx);
|
33
|
1753 if (ret < 0)
|
0
|
1754 goto fail;
|
|
1755 idx += ret;
|
33
|
1756
|
0
|
1757 if (idx + buf_len > ext_end) {
|
|
1758 fail:
|
|
1759 bpg_decoder_free_extension_data(h->first_md);
|
|
1760 return -1;
|
|
1761 }
|
|
1762 if (h->has_animation && tag == BPG_EXTENSION_TAG_ANIM_CONTROL) {
|
|
1763 int idx1;
|
|
1764 uint32_t loop_count, frame_delay_num, frame_delay_den;
|
|
1765
|
|
1766 idx1 = idx;
|
|
1767 ret = get_ue(&loop_count, buf + idx1, ext_end - idx1);
|
33
|
1768 if (ret < 0)
|
0
|
1769 goto fail;
|
|
1770 idx1 += ret;
|
|
1771 ret = get_ue(&frame_delay_num, buf + idx1, ext_end - idx1);
|
33
|
1772 if (ret < 0)
|
0
|
1773 goto fail;
|
|
1774 idx1 += ret;
|
|
1775 ret = get_ue(&frame_delay_den, buf + idx1, ext_end - idx1);
|
33
|
1776 if (ret < 0)
|
0
|
1777 goto fail;
|
|
1778 idx1 += ret;
|
|
1779 if (frame_delay_num == 0 || frame_delay_den == 0 ||
|
|
1780 (uint16_t)frame_delay_num != frame_delay_num ||
|
|
1781 (uint16_t)frame_delay_den != frame_delay_den ||
|
|
1782 (uint16_t)loop_count != loop_count)
|
|
1783 goto fail;
|
|
1784 h->loop_count = loop_count;
|
|
1785 h->frame_delay_num = frame_delay_num;
|
|
1786 h->frame_delay_den = frame_delay_den;
|
|
1787 }
|
|
1788 if (load_extensions) {
|
|
1789 md = av_malloc(sizeof(BPGExtensionData));
|
|
1790 md->tag = tag;
|
|
1791 md->buf_len = buf_len;
|
|
1792 md->next = NULL;
|
|
1793 *plast_md = md;
|
|
1794 plast_md = &md->next;
|
33
|
1795
|
0
|
1796 md->buf = av_malloc(md->buf_len);
|
|
1797 memcpy(md->buf, buf + idx, md->buf_len);
|
|
1798 }
|
|
1799 idx += buf_len;
|
|
1800 }
|
|
1801 } else
|
|
1802 {
|
|
1803 /* skip extension data */
|
|
1804 idx += extension_data_len;
|
|
1805 }
|
|
1806 }
|
|
1807
|
|
1808 /* must have animation control extension for animations */
|
|
1809 if (h->has_animation && h->frame_delay_num == 0)
|
|
1810 goto fail;
|
|
1811
|
|
1812 if (h->hevc_data_len == 0)
|
|
1813 h->hevc_data_len = buf_len - idx;
|
33
|
1814
|
0
|
1815 return idx;
|
|
1816 }
|
|
1817
|
|
1818 int bpg_decoder_decode(BPGDecoderContext *img, const uint8_t *buf, int buf_len)
|
|
1819 {
|
|
1820 int idx, has_alpha, bit_depth, color_space, ret;
|
|
1821 uint32_t width, height;
|
|
1822 BPGHeaderData h_s, *h = &h_s;
|
|
1823
|
|
1824 idx = bpg_decode_header(h, buf, buf_len, 0, img->keep_extension_data);
|
|
1825 if (idx < 0)
|
|
1826 return idx;
|
|
1827 width = h->width;
|
|
1828 height = h->height;
|
|
1829 has_alpha = h->has_alpha;
|
|
1830 color_space = h->color_space;
|
|
1831 bit_depth = h->bit_depth;
|
33
|
1832
|
0
|
1833 img->w = width;
|
|
1834 img->h = height;
|
|
1835 img->format = h->format;
|
|
1836 if (h->format == BPG_FORMAT_422_VIDEO) {
|
|
1837 img->format = BPG_FORMAT_422;
|
|
1838 img->c_h_phase = 0;
|
|
1839 } else if (h->format == BPG_FORMAT_420_VIDEO) {
|
|
1840 img->format = BPG_FORMAT_420;
|
|
1841 img->c_h_phase = 0;
|
|
1842 } else {
|
|
1843 img->format = h->format;
|
|
1844 img->c_h_phase = 1;
|
|
1845 }
|
|
1846 img->has_alpha = has_alpha;
|
|
1847 img->premultiplied_alpha = h->premultiplied_alpha;
|
|
1848 img->has_w_plane = h->has_w_plane;
|
|
1849 img->limited_range = h->limited_range;
|
|
1850 img->color_space = color_space;
|
|
1851 img->bit_depth = bit_depth;
|
|
1852 img->has_animation = h->has_animation;
|
|
1853 img->loop_count = h->loop_count;
|
|
1854 img->frame_delay_num = h->frame_delay_num;
|
|
1855 img->frame_delay_den = h->frame_delay_den;
|
|
1856
|
|
1857 img->first_md = h->first_md;
|
|
1858
|
|
1859 if (idx + h->hevc_data_len > buf_len)
|
|
1860 goto fail;
|
|
1861
|
|
1862 /* decode the first frame */
|
|
1863 ret = hevc_decode_start(img, buf + idx, buf_len - idx,
|
|
1864 width, height, img->format, bit_depth, has_alpha);
|
|
1865 if (ret < 0)
|
|
1866 goto fail;
|
|
1867 idx += ret;
|
|
1868
|
|
1869 #ifdef USE_PRED
|
|
1870 /* XXX: add an option to avoid decoding animations ? */
|
|
1871 img->decode_animation = 1;
|
33
|
1872 if (img->has_animation && img->decode_animation) {
|
0
|
1873 int len;
|
|
1874 /* keep trailing bitstream to decode the next frames */
|
|
1875 len = buf_len - idx;
|
|
1876 img->input_buf = av_malloc(len);
|
|
1877 if (!img->input_buf)
|
|
1878 goto fail;
|
|
1879 memcpy(img->input_buf, buf + idx, len);
|
|
1880 img->input_buf_len = len;
|
|
1881 img->input_buf_pos = 0;
|
33
|
1882 } else
|
0
|
1883 #endif
|
|
1884 {
|
|
1885 hevc_decode_end(img);
|
|
1886 }
|
|
1887 if (img->frame->width < img->w || img->frame->height < img->h)
|
|
1888 goto fail;
|
|
1889 img->y = -1;
|
|
1890 return 0;
|
|
1891
|
|
1892 fail:
|
|
1893 av_frame_free(&img->frame);
|
|
1894 av_frame_free(&img->alpha_frame);
|
|
1895 bpg_decoder_free_extension_data(img->first_md);
|
|
1896 img->first_md = NULL;
|
|
1897 return -1;
|
|
1898 }
|
|
1899
|
|
1900 void bpg_decoder_close(BPGDecoderContext *s)
|
|
1901 {
|
|
1902 bpg_decoder_output_end(s);
|
|
1903 av_free(s->input_buf);
|
|
1904 hevc_decode_end(s);
|
|
1905 av_frame_free(&s->frame);
|
|
1906 av_frame_free(&s->alpha_frame);
|
|
1907 bpg_decoder_free_extension_data(s->first_md);
|
|
1908 av_free(s);
|
|
1909 }
|
|
1910
|
|
1911 void bpg_decoder_free_extension_data(BPGExtensionData *first_md)
|
|
1912 {
|
|
1913 #ifndef EMSCRIPTEN
|
|
1914 BPGExtensionData *md, *md_next;
|
33
|
1915
|
0
|
1916 for(md = first_md; md != NULL; md = md_next) {
|
|
1917 md_next = md->next;
|
|
1918 av_free(md->buf);
|
|
1919 av_free(md);
|
|
1920 }
|
|
1921 #endif
|
|
1922 }
|
|
1923
|
|
1924 #ifndef EMSCRIPTEN
|
|
1925 void bpg_decoder_keep_extension_data(BPGDecoderContext *s, int enable)
|
|
1926 {
|
|
1927 s->keep_extension_data = enable;
|
|
1928 }
|
|
1929
|
|
1930 BPGExtensionData *bpg_decoder_get_extension_data(BPGDecoderContext *s)
|
|
1931 {
|
|
1932 return s->first_md;
|
|
1933 }
|
|
1934
|
33
|
1935 int bpg_decoder_get_info_from_buf(BPGImageInfo *p,
|
0
|
1936 BPGExtensionData **pfirst_md,
|
|
1937 const uint8_t *buf, int buf_len)
|
|
1938 {
|
|
1939 BPGHeaderData h_s, *h = &h_s;
|
|
1940 int parse_extension;
|
|
1941
|
|
1942 parse_extension = (pfirst_md != NULL);
|
33
|
1943 if (bpg_decode_header(h, buf, buf_len,
|
0
|
1944 !parse_extension, parse_extension) < 0)
|
|
1945 return -1;
|
|
1946 p->width = h->width;
|
|
1947 p->height = h->height;
|
|
1948 p->format = h->format;
|
|
1949 p->has_alpha = h->has_alpha && !h->has_w_plane;
|
|
1950 p->premultiplied_alpha = h->premultiplied_alpha;
|
|
1951 p->has_w_plane = h->has_w_plane;
|
|
1952 p->limited_range = h->limited_range;
|
|
1953 p->color_space = h->color_space;
|
|
1954 p->bit_depth = h->bit_depth;
|
|
1955 p->has_animation = h->has_animation;
|
|
1956 p->loop_count = h->loop_count;
|
|
1957 if (pfirst_md)
|
|
1958 *pfirst_md = h->first_md;
|
|
1959 return 0;
|
|
1960 }
|
|
1961 #endif
|