]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/jpeglsdec.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / jpeglsdec.c
index 5308b744df1fde9a8c1a7c31575bb7d1fadffab7..bf51d63c3aa71e644fa6aa066e87ddc55d69321d 100644 (file)
@@ -108,9 +108,8 @@ int ff_jpegls_decode_lse(MJpegDecodeContext *s)
         if (s->palette_index > maxtab)
             return AVERROR_INVALIDDATA;
 
-        if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
-            (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) {
-            uint32_t *pal = (uint32_t *)s->picture_ptr->data[1];
+        if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
+            uint32_t *pal = s->palette;
             int shift = 0;
 
             if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) {
@@ -118,7 +117,6 @@ int ff_jpegls_decode_lse(MJpegDecodeContext *s)
                 shift = 8 - s->avctx->bits_per_raw_sample;
             }
 
-            s->picture_ptr->format =
             s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
             for (i=s->palette_index; i<=maxtab; i++) {
                 uint8_t k = i << shift;
@@ -149,7 +147,7 @@ static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
 {
     int k, ret;
 
-    for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
+    for (k = 0; ((unsigned)state->N[Q] << k) < state->A[Q]; k++)
         ;
 
 #ifdef JLS_BROKEN
@@ -222,7 +220,7 @@ static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
 /**
  * Decode one line of image
  */
-static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
+static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s,
                                   void *last, void *dst, int last2, int w,
                                   int stride, int comp, int bits)
 {
@@ -234,7 +232,7 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
         int err, pred;
 
         if (get_bits_left(&s->gb) <= 0)
-            return;
+            return AVERROR_INVALIDDATA;
 
         /* compute gradients */
         Ra = x ? R(dst, x - stride) : R(last, x);
@@ -263,11 +261,11 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
                 }
                 /* if EOL reached, we stop decoding */
                 if (r != 1 << ff_log2_run[state->run_index[comp]])
-                    return;
+                    return 0;
                 if (state->run_index[comp] < 31)
                     state->run_index[comp]++;
                 if (x + stride > w)
-                    return;
+                    return 0;
             }
             /* decode aborted run */
             r = ff_log2_run[state->run_index[comp]];
@@ -284,7 +282,7 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
             if (x >= w) {
                 av_log(NULL, AV_LOG_ERROR, "run overflow\n");
                 av_assert0(x <= w);
-                return;
+                return AVERROR_INVALIDDATA;
             }
 
             /* decode run termination value */
@@ -341,6 +339,8 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
         W(dst, x, pred);
         x += stride;
     }
+
+    return 0;
 }
 
 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
@@ -348,21 +348,24 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
 {
     int i, t = 0;
     uint8_t *zero, *last, *cur;
-    JLSState *state;
+    JLSState *state = s->jls_state;
     int off = 0, stride = 1, width, shift, ret = 0;
+    int decoded_height = 0;
 
+    if (!state) {
+        state = av_malloc(sizeof(*state));
+        if (!state)
+            return AVERROR(ENOMEM);
+        s->jls_state = state;
+    }
     zero = av_mallocz(s->picture_ptr->linesize[0]);
     if (!zero)
         return AVERROR(ENOMEM);
     last = zero;
     cur  = s->picture_ptr->data[0];
 
-    state = av_mallocz(sizeof(JLSState));
-    if (!state) {
-        av_free(zero);
-        return AVERROR(ENOMEM);
-    }
     /* initialize JPEG-LS state from JPEG parameters */
+    memset(state, 0, sizeof(*state));
     state->near   = near;
     state->bpp    = (s->bits < 2) ? 2 : s->bits;
     state->maxval = s->maxval;
@@ -407,13 +410,16 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
         width  = s->width * stride;
         cur   += off;
         for (i = 0; i < s->height; i++) {
+            int ret;
             if (s->bits <= 8) {
-                ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
+                ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
                 t = last[0];
             } else {
-                ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
+                ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
                 t = *((uint16_t *)last);
             }
+            if (ret < 0)
+                break;
             last = cur;
             cur += s->picture_ptr->linesize[0];
 
@@ -422,6 +428,7 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
                 skip_bits(&s->gb, 16); /* skip RSTn */
             }
         }
+        decoded_height = i;
     } else if (ilv == 1) { /* line interleaving */
         int j;
         int Rc[3] = { 0, 0, 0 };
@@ -429,9 +436,12 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
         memset(cur, 0, s->picture_ptr->linesize[0]);
         width = s->width * stride;
         for (i = 0; i < s->height; i++) {
+            int ret;
             for (j = 0; j < stride; j++) {
-                ls_decode_line(state, s, last + j, cur + j,
+                ret = ls_decode_line(state, s, last + j, cur + j,
                                Rc[j], width, stride, j, 8);
+                if (ret < 0)
+                    break;
                 Rc[j] = last[j];
 
                 if (s->restart_interval && !--s->restart_count) {
@@ -439,9 +449,12 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
                     skip_bits(&s->gb, 16); /* skip RSTn */
                 }
             }
+            if (ret < 0)
+                break;
             last = cur;
             cur += s->picture_ptr->linesize[0];
         }
+        decoded_height = i;
     } else if (ilv == 2) { /* sample interleaving */
         avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
         ret = AVERROR_PATCHWELCOME;
@@ -507,7 +520,7 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
         if (s->bits <= 8) {
             uint8_t *src = s->picture_ptr->data[0];
 
-            for (i = 0; i < s->height; i++) {
+            for (i = 0; i < decoded_height; i++) {
                 for (x = off; x < w; x += stride)
                     src[x] <<= shift;
                 src += s->picture_ptr->linesize[0];
@@ -515,7 +528,7 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
         } else {
             uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
 
-            for (i = 0; i < s->height; i++) {
+            for (i = 0; i < decoded_height; i++) {
                 for (x = 0; x < w; x++)
                     src[x] <<= shift;
                 src += s->picture_ptr->linesize[0] / 2;
@@ -524,13 +537,12 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
     }
 
 end:
-    av_free(state);
     av_free(zero);
 
     return ret;
 }
 
-AVCodec ff_jpegls_decoder = {
+const AVCodec ff_jpegls_decoder = {
     .name           = "jpegls",
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
     .type           = AVMEDIA_TYPE_VIDEO,
@@ -538,7 +550,8 @@ AVCodec ff_jpegls_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    .decode         = ff_mjpeg_decode_frame,
+    .receive_frame  = ff_mjpeg_receive_frame,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                      FF_CODEC_CAP_SETS_PKT_DTS,
 };