ffmpeg/libavcodec/pixlet.c libav/libavcodec/pixlet.c
  1 /*
  2  * Apple Pixlet decoder
  3  * Copyright (c) 2016 Paul B Mahol
  4  *
  5  * This file is part of FFmpeg.                                                                                                      
  6  *
  7  * FFmpeg is free software; you can redistribute it and/or                                                                           
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * FFmpeg is distributed in the hope that it will be useful,                                                                         
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with FFmpeg; if not, write to the Free Software                                                                     
 19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 20  */
 21 
 22 #include <stdint.h>
 23 
 24 #include "libavutil/imgutils.h"
 25 #include "libavutil/intmath.h"
 26 #include "libavutil/opt.h"
 27 
 28 #include "avcodec.h"
    -------------------------------------------------------------------------------------------------------------------------------------
 29 #include "bytestream.h"
 30 #include "get_bits.h"                                                                                                                
 31 #include "internal.h"
 32 #include "thread.h"
 33 #include "unary.h"
 34 
 35 #define NB_LEVELS 4
 36 
 37 #define PIXLET_MAGIC 0xDEADBEEF
 38 
 39 #define H 0
 40 #define V 1
 41 
 42 typedef struct SubBand {
 43     unsigned width, height;                                                                                                          
 44     unsigned size;                                                                                                                   
 45     unsigned x, y;                                                                                                                   
 46 } SubBand;
 47 
 48 typedef struct PixletContext {
 49     AVClass *class;
 50 
 51     GetByteContext gb;
 52     GetBitContext bc;                                                                                                                
 53 
 54     int levels;
 55     int depth;
 56     int w, h;                                                                                                                        
 57 
 58     int16_t *filter[2];
 59     int16_t *prediction;
 60     int64_t scaling[4][2][NB_LEVELS];
 61     SubBand band[4][NB_LEVELS * 3 + 1];
 62 } PixletContext;
 63 +--  2 lines: static av_cold int pixlet_init(AVCodecContext *avctx)----------------------------------------------------------------------
 65 {
 66     avctx->pix_fmt     = AV_PIX_FMT_YUV420P16;
 67     avctx->color_range = AVCOL_RANGE_JPEG;
 68     return 0;
 69 }
 70 
 71 static void free_buffers(AVCodecContext *avctx)                                                                                      
 72 {
 73     PixletContext *ctx = avctx->priv_data;
 74 
 75     av_freep(&ctx->filter[0]);
 76     av_freep(&ctx->filter[1]);
 77     av_freep(&ctx->prediction);
 78 }                                                                                                                                    
 79 
 80 static av_cold int pixlet_close(AVCodecContext *avctx)                                                                               
 81 {                                                                                                                                    
 82     PixletContext *ctx = avctx->priv_data;                                                                                           
 83     free_buffers(avctx);                                                                                                             
 84     ctx->w = 0;                                                                                                                      
 85     ctx->h = 0;                                                                                                                      
 86     return 0;
 87 }
 88 
 89 static int init_decoder(AVCodecContext *avctx)
 90 {
 91     PixletContext *ctx = avctx->priv_data;
 92 +--  4 lines: int i, plane;--------------------------------------------------------------------------------------------------------------
 96     ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
 97     if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
 98         return AVERROR(ENOMEM);
 99 
100     for (plane = 0; plane < 3; plane++) {
101         unsigned shift = plane > 0;
102         unsigned w     = ctx->w >> shift;                                                                                            
103         unsigned h     = ctx->h >> shift;                                                                                            
104 
105         ctx->band[plane][0].width  =  w >> NB_LEVELS;
106         ctx->band[plane][0].height =  h >> NB_LEVELS;
107         ctx->band[plane][0].size   = (w >> NB_LEVELS) * (h >> NB_LEVELS);
108 
109         for (i = 0; i < NB_LEVELS * 3; i++) {
110 +--  8 lines: unsigned scale = ctx->levels - (i / 3);------------------------------------------------------------------------------------
118         }
119     }
120 
121     return 0;
122 }
123 
124 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size,                                                            
125                            int width, ptrdiff_t stride)                                                                              
126 {
127     PixletContext *ctx = avctx->priv_data;
128     GetBitContext *bc = &ctx->bc;                                                                                                    
129     unsigned cnt1, nbits, k, j = 0, i = 0;
130     int64_t value, state = 3;
131     int rlen, escape, flag = 0;
132 
133     while (i < size) {
134         nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
135 
136         cnt1 = get_unary(bc, 0, 8);
137         if (cnt1 < 8) {
138             value = show_bits(bc, nbits);                                                                                            
139             if (value <= 1) {
140                 skip_bits(bc, nbits - 1);                                                                                            
141                 escape = ((1 << nbits) - 1) * cnt1;                                                                                  
142             } else {                                                                                                                 
143                 skip_bits(bc, nbits);                                                                                                
144                 escape = value + ((1 << nbits) - 1) * cnt1 - 1;                                                                      
145             }
    -------------------------------------------------------------------------------------------------------------------------------------
146         } else {
147             escape = get_bits(bc, 16);                                                                                               
148         }
149 
150         value    = -((escape + flag) & 1) | 1;
151         dst[j++] = value * ((escape + flag + 1) >> 1);
152         i++;
153         if (j == width) {
154             j    = 0;
155             dst += stride;
156         }
157         state = 120 * (escape + flag) + state - (120 * state >> 8);
158         flag  = 0;
159 
160         if (state * 4ULL > 0xFF || i >= size)                                                                                        
161             continue;
162 
163         nbits  = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
164         escape = av_mod_uintp2(16383, nbits);
165         cnt1   = get_unary(bc, 0, 8);
166         if (cnt1 > 7) {
167             rlen = get_bits(bc, 16);                                                                                                 
168         } else {
169             value = show_bits(bc, nbits);                                                                                            
170             if (value > 1) {                                                                                                         
171                 skip_bits(bc, nbits);                                                                                                
172                 rlen = value + escape * cnt1 - 1;                                                                                    
173             } else {                                                                                                                 
174                 skip_bits(bc, nbits - 1);                                                                                            
175                 rlen = escape * cnt1;                                                                                                
176             }
    -------------------------------------------------------------------------------------------------------------------------------------
177         }
178 
179         if (rlen > size - i)                                                                                                         
180             return AVERROR_INVALIDDATA;
181         i += rlen;
182 
183         for (k = 0; k < rlen; k++) {
184             dst[j++] = 0;
185             if (j == width) {
186 +--  3 lines: j    = 0;------------------------------------------------------------------------------------------------------------------
189         }
190 
191         state = 0;
192         flag  = rlen < 0xFFFF ? 1 : 0;
193     }
194 
195     align_get_bits(bc);                                                                                                              
196     return get_bits_count(bc) >> 3;                                                                                                  
197 }
198 
199 static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst,
200                             int size, int c, int a, int d,                                                                           
201                             int width, ptrdiff_t stride)
202 {
203     PixletContext *ctx = avctx->priv_data;
204     GetBitContext *bc = &ctx->bc;                                                                                                    
205     unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
206     int ret, escape, pfx, value, yflag, xflag, flag = 0;                                                                             
207     int64_t state = 3, tmp;                                                                                                          
208 
209     ret = init_get_bits8(bc, src, bytestream2_get_bytes_left(&ctx->gb));                                                             
210     if (ret < 0)
211         return ret;
212 
213     if (a ^ (a >> 31)) {                                                                                                             
214         nbits = 33 - ff_clz(a ^ (a >> 31));                                                                                          
    -------------------------------------------------------------------------------------------------------------------------------------
215         if (nbits > 16)
216             return AVERROR_INVALIDDATA;
217     } else {
218         nbits = 1;
219     }
220 
221 +--  3 lines: length = 25 - nbits;-------------------------------------------------------------------------------------------------------
224         if (state >> 8 != -3)
225             value = ff_clz((state >> 8) + 3) ^ 0x1F;
226         else
227             value = -1;
228 
229         cnt1 = get_unary(bc, 0, length);
230                                                                                                                                      
231         if (cnt1 >= length) {
232             cnt1 = get_bits(bc, nbits);                                                                                              
233         } else {
234             pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));                                                            
235             if (pfx < 1 || pfx > 25)                                                                                                 
236                 return AVERROR_INVALIDDATA;                                                                                          
237             cnt1 *= (1 << pfx) - 1;                                                                                                  
238             shbits = show_bits(bc, pfx);                                                                                             
239             if (shbits <= 1) {
240                 skip_bits(bc, pfx - 1);                                                                                              
241             } else {                                                                                                                 
242                 skip_bits(bc, pfx);                                                                                                  
243                 cnt1 += shbits - 1;                                                                                                  
244             }
    -------------------------------------------------------------------------------------------------------------------------------------
245         }
246 
247         xflag = flag + cnt1;
248         yflag = xflag;
249 
250         if (flag + cnt1 == 0) {
251             value = 0;
252         } else {
253             xflag &= 1u;
254             tmp    = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);                                                                     
255             value  = xflag + (tmp ^ -xflag);
256         }
257 
258         i++;
259         dst[j++] = value;
260         if (j == width) {
261             j    = 0;
262             dst += stride;
263         }
264         state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);                                               
265 
266         flag = 0;
267 
268         if ((uint64_t)state > 0xFF / 4 || i >= size)                                                                                 
269             continue;
270 
271         pfx    = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
272         escape = av_mod_uintp2(16383, pfx);
273         cnt1   = get_unary(bc, 0, 8);
274         if (cnt1 < 8) {
275             if (pfx < 1 || pfx > 25)
276                 return AVERROR_INVALIDDATA;
277 
278             value = show_bits(bc, pfx);                                                                                              
279             if (value > 1) {                                                                                                         
280                 skip_bits(bc, pfx);                                                                                                  
281                 rlen = value + escape * cnt1 - 1;                                                                                    
282             } else {                                                                                                                 
283                 skip_bits(bc, pfx - 1);                                                                                              
284                 rlen = escape * cnt1;                                                                                                
285             }
    -------------------------------------------------------------------------------------------------------------------------------------
286         } else {
287             if (get_bits1(bc))                                                                                                       
288                 value = get_bits(bc, 16);                                                                                            
289             else
290                 value = get_bits(bc, 8);                                                                                             
291 
292             rlen = value + 8 * escape;
293         }
294 
295         if (rlen > 0xFFFF || i + rlen > size)
296             return AVERROR_INVALIDDATA;
297 +--  8 lines: i += rlen;-----------------------------------------------------------------------------------------------------------------
305         }
306 
307         state = 0;
308         flag  = rlen < 0xFFFF ? 1 : 0;
309     }
310 
311     align_get_bits(bc);                                                                                                              
312     return get_bits_count(bc) >> 3;                                                                                                  
313 }
314 
315 static int read_highpass(AVCodecContext *avctx, uint8_t *ptr,
316                          int plane, AVFrame *frame)
317 {
318     PixletContext *ctx = avctx->priv_data;
319 +--  5 lines: ptrdiff_t stride = frame->linesize[plane] / 2;-----------------------------------------------------------------------------
324         int32_t b = bytestream2_get_be32(&ctx->gb);
325         int32_t c = bytestream2_get_be32(&ctx->gb);
326         int32_t d = bytestream2_get_be32(&ctx->gb);
327         int16_t *dest = (int16_t *)frame->data[plane] +
328                         ctx->band[plane][i + 1].x +
329                         ctx->band[plane][i + 1].y * stride;
330         unsigned size = ctx->band[plane][i + 1].size;                                                                                
331         uint32_t magic = bytestream2_get_be32(&ctx->gb);
332 
333         if (magic != PIXLET_MAGIC) {
334             av_log(avctx, AV_LOG_ERROR,
335                    "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",                                                      
336                    magic, plane, i);
337             return AVERROR_INVALIDDATA;
338         }
339 
340         if (a == INT32_MIN)                                                                                                          
341             return AVERROR_INVALIDDATA;                                                                                              
342                                                                                                                                      
343         ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,                                                  
344                                c, (b >= FFABS(a)) ? b : a, d,                                                                        
345                                ctx->band[plane][i + 1].width, stride);
346         if (ret < 0) {
347             av_log(avctx, AV_LOG_ERROR,
348                    "error in highpass coefficients for plane %d, band %d\n",
349                    plane, i);
350             return ret;
351         }
352         bytestream2_skip(&ctx->gb, ret);
353     }
354 
355     return 0;
356 }
357 
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
358 static void lowpass_prediction(int16_t *dst, int16_t *pred,
359                                int width, int height, ptrdiff_t stride)                                                              
360 {
361     int16_t val;                                                                                                                     
362     int i, j;
363 
364     memset(pred, 0, width * sizeof(*pred));
365 
366     for (i = 0; i < height; i++) {
367         val    = pred[0] + dst[0];                                                                                                   
368         dst[0] = pred[0] = val;                                                                                                      
369         for (j = 1; j < width; j++) {                                                                                                
370             val     = pred[j] + dst[j];                                                                                              
371             dst[j]  = pred[j] = val;                                                                                                 
372             dst[j] += dst[j-1];                                                                                                      
373         }                                                                                                                            
374         dst += stride;
375     }
376 }
377 
378 static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)                                                      
379 {
380     int16_t *low, *high, *ll, *lh, *hl, *hh;
381     int hsize, i, j;
382     int64_t value;
383 
384     hsize = size >> 1;
385 +-- 33 lines: low   = tmp + 4;-----------------------------------------------------------------------------------------------------------
418                 (int64_t) high[i - 1] *  INT64_C(303700064);
419         dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
420     }
421 }
422 
423 static void reconstruction(AVCodecContext *avctx, int16_t *dest,
424                            unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,                                         
425                            int64_t *scaling_h, int64_t *scaling_v)
426 {
427     PixletContext *ctx = avctx->priv_data;
428     unsigned scaled_width, scaled_height;
429     int16_t *ptr, *tmp;
430     int i, j, k;
431 
432     scaled_width  = width  >> nb_levels;                                                                                             
433     scaled_height = height >> nb_levels;                                                                                             
434     tmp           = ctx->filter[0];
435 
436     for (i = 0; i < nb_levels; i++) {                                                                                                
437         int64_t scale_v = scaling_v[i];
438         int64_t scale_h = scaling_h[i];
439         scaled_width  <<= 1;
440         scaled_height <<= 1;
441 
442         ptr = dest;
443 +-- 17 lines: for (j = 0; j < scaled_height; j++) {--------------------------------------------------------------------------------------
460                 ptr += stride;
461             }
462         }
463     }
464 }
465 
466 static void postprocess_luma(AVFrame *frame, int w, int h, int depth)                                                                
467 {
468     uint16_t *dsty = (uint16_t *)frame->data[0];
469     int16_t *srcy  = (int16_t *)frame->data[0];
470     ptrdiff_t stridey = frame->linesize[0] / 2;
471     int i, j;
472 
473 +-- 58 lines: for (j = 0; j < h; j++) {--------------------------------------------------------------------------------------------------
531 
532     bytestream2_skip(&ctx->gb, 4);
533 
534     dst    = (int16_t *)frame->data[plane];
535     dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
536 
537     ret = init_get_bits8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),                                                         
538                          bytestream2_get_bytes_left(&ctx->gb));                                                                      
539     if (ret < 0)
540         return ret;
541 
542     ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
543                           ctx->band[plane][0].width - 1, 0);
544     if (ret < 0) {
545 +-- 31 lines: av_log(avctx, AV_LOG_ERROR,------------------------------------------------------------------------------------------------
576         return ret;
577 
578     lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
579                        ctx->band[plane][0].height, stride);
580 
581     reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
582                    ctx->h >> shift, stride, NB_LEVELS, ctx->scaling[plane][H],                                                       
583                    ctx->scaling[plane][V]);
584 
585     return 0;
586 }
587 
588 static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
589 +--  6 lines: int *got_frame, AVPacket *avpkt)-------------------------------------------------------------------------------------------
595     uint32_t pktsize;
596 
597     bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
598 
599     pktsize = bytestream2_get_be32(&ctx->gb);
600     if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
601         av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);                                                     
602         return AVERROR_INVALIDDATA;
603     }
604 
605     version = bytestream2_get_le32(&ctx->gb);
606     if (version != 1)
607         avpriv_request_sample(avctx, "Version %d", version);
608 +--  3 lines: bytestream2_skip(&ctx->gb, 4);---------------------------------------------------------------------------------------------
611         return AVERROR_INVALIDDATA;
612     bytestream2_skip(&ctx->gb, 4);
613 
614     width  = bytestream2_get_be32(&ctx->gb);
615     height = bytestream2_get_be32(&ctx->gb);
616 
617     if (    width > INT_MAX - (1U << (NB_LEVELS + 1))                                                                                
618         || height > INT_MAX - (1U << (NB_LEVELS + 1)))                                                                               
619         return AVERROR_INVALIDDATA;                                                                                                  
620                                                                                                                                      
621     w = FFALIGN(width,  1 << (NB_LEVELS + 1));
622     h = FFALIGN(height, 1 << (NB_LEVELS + 1));
623 
624     ctx->levels = bytestream2_get_be32(&ctx->gb);
625     if (ctx->levels != NB_LEVELS)
626         return AVERROR_INVALIDDATA;
627 +--  6 lines: ctx->depth = bytestream2_get_be32(&ctx->gb);-------------------------------------------------------------------------------
633     ret = ff_set_dimensions(avctx, w, h);
634     if (ret < 0)
635         return ret;
636     avctx->width  = width;
637     avctx->height = height;
638 
    -------------------------------------------------------------------------------------------------------------------------------------
639     if (ctx->w != w || ctx->h != h) {
640         free_buffers(avctx);                                                                                                         
641         ctx->w = w;
642         ctx->h = h;
643 
644         ret = init_decoder(avctx);
645         if (ret < 0) {
646             free_buffers(avctx);                                                                                                     
647             ctx->w = 0;
648             ctx->h = 0;
649             return ret;
650         }
651     }
652 
653     bytestream2_skip(&ctx->gb, 8);
654 
655     p->pict_type = AV_PICTURE_TYPE_I;                                                                                                
656     p->key_frame = 1;                                                                                                                
657     p->color_range = AVCOL_RANGE_JPEG;                                                                                               
658                                                                                                                                      
659     ret = ff_thread_get_buffer(avctx, &frame, 0);
660     if (ret < 0)
661         return ret;
662 
663     for (i = 0; i < 3; i++) {
664         ret = decode_plane(avctx, i, avpkt, frame.f);
665 +--  2 lines: if (ret < 0)---------------------------------------------------------------------------------------------------------------
667         if (avctx->flags & AV_CODEC_FLAG_GRAY)
668             break;
669     }
670 
671     postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
672     postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
673 
674     *got_frame = 1;
675 
676     return pktsize;
677 }
678 
679 +-- 30 lines: #if HAVE_THREADS-----------------------------------------------------------------------------------------------------------
  1 /*
  2  * Apple Pixlet decoder
  3  * Copyright (c) 2016 Paul B Mahol
  4  *
  5  * This file is part of Libav.                                                                                                       
  6  *
  7  * Libav is free software; you can redistribute it and/or                                                                            
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * Libav is distributed in the hope that it will be useful,                                                                          
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with Libav; if not, write to the Free Software                                                                      
 19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 20  */
 21 
 22 #include <stdint.h>
 23 
 24 #include "libavutil/imgutils.h"
 25 #include "libavutil/intmath.h"
 26 #include "libavutil/opt.h"
 27 
 28 #include "avcodec.h"
 29 #include "bitstream.h"                                                                                                               
 30 #include "bytestream.h"
    -------------------------------------------------------------------------------------------------------------------------------------
 31 #include "internal.h"
 32 #include "thread.h"
 33 #include "unary.h"
 34 
 35 #define NB_LEVELS 4
 36 
 37 #define PIXLET_MAGIC 0xDEADBEEF
 38 
 39 #define H 0
 40 #define V 1
 41 
 42 typedef struct SubBand {
 43     size_t width, height;                                                                                                            
 44     size_t size;                                                                                                                     
 45     size_t x, y;                                                                                                                     
 46 } SubBand;
 47 
 48 typedef struct PixletContext {
 49     AVClass *class;
 50 
 51     GetByteContext gb;
 52     BitstreamContext bc;                                                                                                             
 53 
 54     int levels;
 55     int depth;
 56     size_t w, h;                                                                                                                     
 57 
 58     int16_t *filter[2];
 59     int16_t *prediction;
 60     int64_t scaling[4][2][NB_LEVELS];
 61     SubBand band[4][NB_LEVELS * 3 + 1];
 62 } PixletContext;
 63 +--  2 lines: static av_cold int pixlet_init(AVCodecContext *avctx)----------------------------------------------------------------------
 65 {
 66     avctx->pix_fmt     = AV_PIX_FMT_YUV420P16;
 67     avctx->color_range = AVCOL_RANGE_JPEG;
 68     return 0;
 69 }
 70 
 71 static av_cold int pixlet_close(AVCodecContext *avctx)                                                                               
 72 {
 73     PixletContext *ctx = avctx->priv_data;
 74 
 75     av_freep(&ctx->filter[0]);
 76     av_freep(&ctx->filter[1]);
 77     av_freep(&ctx->prediction);
    -------------------------------------------------------------------------------------------------------------------------------------
 78 
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
 79     return 0;
 80 }
 81 
 82 static int init_decoder(AVCodecContext *avctx)
 83 {
 84     PixletContext *ctx = avctx->priv_data;
 85 +--  4 lines: int i, plane;--------------------------------------------------------------------------------------------------------------
 89     ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
 90     if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
 91         return AVERROR(ENOMEM);
 92 
 93     for (plane = 0; plane < 3; plane++) {
 94         unsigned shift = plane > 0;
 95         size_t w       = ctx->w >> shift;                                                                                            
 96         size_t h       = ctx->h >> shift;                                                                                            
 97 
 98         ctx->band[plane][0].width  =  w >> NB_LEVELS;
 99         ctx->band[plane][0].height =  h >> NB_LEVELS;
100         ctx->band[plane][0].size   = (w >> NB_LEVELS) * (h >> NB_LEVELS);
101 
102         for (i = 0; i < NB_LEVELS * 3; i++) {
103 +--  8 lines: unsigned scale = ctx->levels - (i / 3);------------------------------------------------------------------------------------
111         }
112     }
113 
114     return 0;
115 }
116 
117 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, size_t size,                                                         
118                            size_t width, ptrdiff_t stride)                                                                           
119 {
120     PixletContext *ctx = avctx->priv_data;
121     BitstreamContext *bc = &ctx->bc;                                                                                                 
122     unsigned cnt1, nbits, k, j = 0, i = 0;
123     int64_t value, state = 3;
124     int rlen, escape, flag = 0;
125 
126     while (i < size) {
127         nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
128 
129         cnt1 = get_unary(bc, 0, 8);
130         if (cnt1 < 8) {
131             value = bitstream_read(bc, nbits);                                                                                       
132             if (value <= 1) {
133                 bitstream_unget(bc, value & 1, 1);                                                                                   
134                 value = 1;                                                                                                           
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
135             }
136             escape = value + ((1 << nbits) - 1) * cnt1 - 1;                                                                          
137         } else {
138             escape = bitstream_read(bc, 16);                                                                                         
139         }
140 
141         value    = -((escape + flag) & 1) | 1;
142         dst[j++] = value * ((escape + flag + 1) >> 1);
143         i++;
144         if (j == width) {
145             j    = 0;
146             dst += stride;
147         }
148         state = 120 * (escape + flag) + state - (120 * state >> 8);
149         flag  = 0;
150 
151         if (state * 4 > 0xFF || i >= size)                                                                                           
152             continue;
153 
154         nbits  = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
155         escape = av_mod_uintp2(16383, nbits);
156         cnt1   = get_unary(bc, 0, 8);
157         if (cnt1 > 7) {
158             rlen = bitstream_read(bc, 16);                                                                                           
159         } else {
160             value = bitstream_read(bc, nbits);                                                                                       
161             if (value <= 1) {                                                                                                        
162                 bitstream_unget(bc, value & 1, 1);                                                                                   
163                 value = 1;                                                                                                           
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
164             }
165             rlen = value + escape * cnt1 - 1;                                                                                        
166         }
167 
168         if (i + rlen > size)                                                                                                         
169             return AVERROR_INVALIDDATA;
170         i += rlen;
171 
172         for (k = 0; k < rlen; k++) {
173             dst[j++] = 0;
174             if (j == width) {
175 +--  3 lines: j    = 0;------------------------------------------------------------------------------------------------------------------
178         }
179 
180         state = 0;
181         flag  = rlen < 0xFFFF ? 1 : 0;
182     }
183 
184     bitstream_align(bc);                                                                                                             
185     return bitstream_tell(bc) >> 3;                                                                                                  
186 }
187 
188 static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst,
189                             int size, int64_t c, int a, int64_t d,                                                                   
190                             int width, ptrdiff_t stride)
191 {
192     PixletContext *ctx = avctx->priv_data;
193     BitstreamContext *bc = &ctx->bc;                                                                                                 
194     unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
195     int ret, escape, pfx, cthulu, yflag, xflag, flag = 0;                                                                            
196     int64_t state = 3, value, tmp;                                                                                                   
197 
198     ret = bitstream_init8(bc, src, bytestream2_get_bytes_left(&ctx->gb));                                                            
199     if (ret < 0)
200         return ret;
201 
202     cthulu = (a >= 0) + (a ^ (a >> 31)) - (a >> 31);                                                                                 
203     if (cthulu != 1) {                                                                                                               
204         nbits = 33 - ff_clz(cthulu - 1);                                                                                             
205         if (nbits > 16)
206             return AVERROR_INVALIDDATA;
207     } else {
208         nbits = 1;
209     }
210 
211 +--  3 lines: length = 25 - nbits;-------------------------------------------------------------------------------------------------------
214         if (state >> 8 != -3)
215             value = ff_clz((state >> 8) + 3) ^ 0x1F;
216         else
217             value = -1;
218 
219         cnt1 = get_unary(bc, 0, length);
    -------------------------------------------------------------------------------------------------------------------------------------
220         if (cnt1 >= length) {
221             cnt1 = bitstream_read(bc, nbits);                                                                                        
222         } else {
223             pfx    = 14 + (((value - 14) >> 32) & (value - 14));                                                                     
224             cnt1  *= (1 << pfx) - 1;                                                                                                 
225                                                                                                                                      
226             shbits = bitstream_read(bc, pfx);                                                                                        
    -------------------------------------------------------------------------------------------------------------------------------------
227             if (shbits <= 1) {
228                 bitstream_unget(bc, shbits & 1, 1);                                                                                  
229                 shbits = 1;                                                                                                          
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
230             }
231             cnt1 += shbits - 1;                                                                                                      
232         }
233 
234         xflag = flag + cnt1;
235         yflag = xflag;
236 
237         if (flag + cnt1 == 0) {
238             value = 0;
239         } else {
240             xflag &= 1u;
241             tmp    = c * ((yflag + 1) >> 1) + (c >> 1);                                                                              
242             value  = xflag + (tmp ^ -xflag);
243         }
244 
245         i++;
246         dst[j++] = value;
247         if (j == width) {
248             j    = 0;
249             dst += stride;
250         }
251         state += d * yflag - (d * state >> 8);                                                                                       
252 
253         flag = 0;
254 
255         if (state * 4 > 0xFF || i >= size)                                                                                           
256             continue;
257 
258         pfx    = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
259         escape = av_mod_uintp2(16383, pfx);
260         cnt1   = get_unary(bc, 0, 8);
261         if (cnt1 < 8) {
262             if (pfx < 1 || pfx > 25)
263                 return AVERROR_INVALIDDATA;
264 
265             value = bitstream_read(bc, pfx);                                                                                         
266             if (value <= 1) {                                                                                                        
267                 bitstream_unget(bc, value & 1, 1);                                                                                   
268                 value = 1;                                                                                                           
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
269             }
270             rlen = value + escape * cnt1 - 1;                                                                                        
271         } else {
272             if (bitstream_read_bit(bc))                                                                                              
273                 value = bitstream_read(bc, 16);                                                                                      
274             else
275                 value = bitstream_read(bc, 8);                                                                                       
276 
277             rlen = value + 8 * escape;
278         }
279 
280         if (rlen > 0xFFFF || i + rlen > size)
281             return AVERROR_INVALIDDATA;
282 +--  8 lines: i += rlen;-----------------------------------------------------------------------------------------------------------------
290         }
291 
292         state = 0;
293         flag  = rlen < 0xFFFF ? 1 : 0;
294     }
295 
296     bitstream_align(bc);                                                                                                             
297     return bitstream_tell(bc) >> 3;                                                                                                  
298 }
299 
300 static int read_highpass(AVCodecContext *avctx, uint8_t *ptr,
301                          int plane, AVFrame *frame)
302 {
303     PixletContext *ctx = avctx->priv_data;
304 +--  5 lines: ptrdiff_t stride = frame->linesize[plane] / 2;-----------------------------------------------------------------------------
309         int32_t b = bytestream2_get_be32(&ctx->gb);
310         int32_t c = bytestream2_get_be32(&ctx->gb);
311         int32_t d = bytestream2_get_be32(&ctx->gb);
312         int16_t *dest = (int16_t *)frame->data[plane] +
313                         ctx->band[plane][i + 1].x +
314                         ctx->band[plane][i + 1].y * stride;
315         size_t size = ctx->band[plane][i + 1].size;                                                                                  
316         uint32_t magic = bytestream2_get_be32(&ctx->gb);
317 
318         if (magic != PIXLET_MAGIC) {
319             av_log(avctx, AV_LOG_ERROR,
320                    "wrong magic number: 0x%"PRIX32" for plane %d, band %d\n",                                                        
321                    magic, plane, i);
322             return AVERROR_INVALIDDATA;
323         }
324 
325         ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest,                                                        
326                                size, c, (b >= FFABS(a)) ? b : a, d,                                                                  
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
327                                ctx->band[plane][i + 1].width, stride);
328         if (ret < 0) {
329             av_log(avctx, AV_LOG_ERROR,
330                    "error in highpass coefficients for plane %d, band %d\n",
331                    plane, i);
332             return ret;
333         }
334         bytestream2_skip(&ctx->gb, ret);
335     }
336 
337     return 0;
338 }
339 
340 static void line_add_sat_s16(int16_t *dst, const int16_t *src, size_t len)                                                           
341 {                                                                                                                                    
342     int i;                                                                                                                           
343     for (i = 0; i < len; i++) {                                                                                                      
344         int val = dst[i] + src[i];                                                                                                   
345         dst[i] = av_clip_int16(val);                                                                                                 
346     }                                                                                                                                
347 }                                                                                                                                    
348                                                                                                                                      
349 static void lowpass_prediction(int16_t *dst, int16_t *pred,
350                                size_t width, size_t height, ptrdiff_t stride)                                                        
351 {
    -------------------------------------------------------------------------------------------------------------------------------------
352     int i, j;
353 
354     memset(pred, 0, width * sizeof(*pred));
355 
356     for (i = 0; i < height; i++) {
357         line_add_sat_s16(pred, dst, width);                                                                                          
358         dst[0] = pred[0];                                                                                                            
359         for (j = 1; j < width; j++)                                                                                                  
360             dst[j] = pred[j] + dst[j - 1];                                                                                           
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
361         dst += stride;
362     }
363 }
364 
365 static void filterfn(int16_t *dest, int16_t *tmp, size_t size, int64_t scale)                                                        
366 {
367     int16_t *low, *high, *ll, *lh, *hl, *hh;
368     int hsize, i, j;
369     int64_t value;
370 
371     hsize = size >> 1;
372 +-- 33 lines: low   = tmp + 4;-----------------------------------------------------------------------------------------------------------
405                 (int64_t) high[i - 1] *  INT64_C(303700064);
406         dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
407     }
408 }
409 
410 static void reconstruction(AVCodecContext *avctx, int16_t *dest,
411                            size_t width, size_t height, ptrdiff_t stride,                                                            
412                            int64_t *scaling_h, int64_t *scaling_v)
413 {
414     PixletContext *ctx = avctx->priv_data;
415     unsigned scaled_width, scaled_height;
416     int16_t *ptr, *tmp;
417     int i, j, k;
418 
419     scaled_width  = width  >> NB_LEVELS;                                                                                             
420     scaled_height = height >> NB_LEVELS;                                                                                             
421     tmp           = ctx->filter[0];
422 
423     for (i = 0; i < NB_LEVELS; i++) {                                                                                                
424         int64_t scale_v = scaling_v[i];
425         int64_t scale_h = scaling_h[i];
426         scaled_width  <<= 1;
427         scaled_height <<= 1;
428 
429         ptr = dest;
430 +-- 17 lines: for (j = 0; j < scaled_height; j++) {--------------------------------------------------------------------------------------
447                 ptr += stride;
448             }
449         }
450     }
451 }
452 
453 static void postprocess_luma(AVFrame *frame, size_t w, size_t h, int depth)                                                          
454 {
455     uint16_t *dsty = (uint16_t *)frame->data[0];
456     int16_t *srcy  = (int16_t *)frame->data[0];
457     ptrdiff_t stridey = frame->linesize[0] / 2;
458     int i, j;
459 
460 +-- 58 lines: for (j = 0; j < h; j++) {--------------------------------------------------------------------------------------------------
518 
519     bytestream2_skip(&ctx->gb, 4);
520 
521     dst    = (int16_t *)frame->data[plane];
522     dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
523 
524     ret = bitstream_init8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),                                                        
525                           bytestream2_get_bytes_left(&ctx->gb));                                                                     
526     if (ret < 0)
527         return ret;
528 
529     ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
530                           ctx->band[plane][0].width - 1, 0);
531     if (ret < 0) {
532 +-- 31 lines: av_log(avctx, AV_LOG_ERROR,------------------------------------------------------------------------------------------------
563         return ret;
564 
565     lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
566                        ctx->band[plane][0].height, stride);
567 
568     reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
569                    ctx->h >> shift, stride, ctx->scaling[plane][H],                                                                  
570                    ctx->scaling[plane][V]);
571 
572     return 0;
573 }
574 
575 static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
576 +--  6 lines: int *got_frame, AVPacket *avpkt)-------------------------------------------------------------------------------------------
582     uint32_t pktsize;
583 
584     bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
585 
586     pktsize = bytestream2_get_be32(&ctx->gb);
587     if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
588         av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32".\n", pktsize);                                                    
589         return AVERROR_INVALIDDATA;
590     }
591 
592     version = bytestream2_get_le32(&ctx->gb);
593     if (version != 1)
594         avpriv_request_sample(avctx, "Version %d", version);
595 +--  3 lines: bytestream2_skip(&ctx->gb, 4);---------------------------------------------------------------------------------------------
598         return AVERROR_INVALIDDATA;
599     bytestream2_skip(&ctx->gb, 4);
600 
601     width  = bytestream2_get_be32(&ctx->gb);
602     height = bytestream2_get_be32(&ctx->gb);
603 
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
604     w = FFALIGN(width,  1 << (NB_LEVELS + 1));
605     h = FFALIGN(height, 1 << (NB_LEVELS + 1));
606 
607     ctx->levels = bytestream2_get_be32(&ctx->gb);
608     if (ctx->levels != NB_LEVELS)
609         return AVERROR_INVALIDDATA;
610 +--  6 lines: ctx->depth = bytestream2_get_be32(&ctx->gb);-------------------------------------------------------------------------------
616     ret = ff_set_dimensions(avctx, w, h);
617     if (ret < 0)
618         return ret;
619     avctx->width  = width;
620     avctx->height = height;
621 
622     /* reinit should dimensions change */                                                                                            
623     if (ctx->w != w || ctx->h != h) {
624         pixlet_close(avctx);                                                                                                         
625         ctx->w = w;
626         ctx->h = h;
627 
628         ret = init_decoder(avctx);
629         if (ret < 0) {
630             pixlet_close(avctx);                                                                                                     
631             ctx->w = 0;
632             ctx->h = 0;
633             return ret;
634         }
635     }
636 
637     bytestream2_skip(&ctx->gb, 8);
638 
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------------
639     ret = ff_thread_get_buffer(avctx, &frame, 0);
640     if (ret < 0)
641         return ret;
642 
643     for (i = 0; i < 3; i++) {
644         ret = decode_plane(avctx, i, avpkt, frame.f);
645 +--  2 lines: if (ret < 0)---------------------------------------------------------------------------------------------------------------
647         if (avctx->flags & AV_CODEC_FLAG_GRAY)
648             break;
649     }
650 
651     postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
652     postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
653                                                                                                                                      
654     p->pict_type   = AV_PICTURE_TYPE_I;                                                                                              
655     p->color_range = AVCOL_RANGE_JPEG;                                                                                               
656     p->key_frame   = 1;                                                                                                              
657 
658     *got_frame = 1;
659 
660     return pktsize;
661 }
662 
663 +-- 30 lines: #if HAVE_THREADS-----------------------------------------------------------------------------------------------------------