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