]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ivi.c
fftools/ffmpeg: make specifying thread_queue_size turn on threaded input
[ffmpeg] / libavcodec / ivi.c
index 1a496d9b2d609a561a2448a448d0d1443401dea1..c5c50fb5c12d1fbad60723658fb8eb1b10e1c865 100644 (file)
@@ -30,7 +30,6 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/imgutils.h"
-#include "libavutil/timer.h"
 
 #define BITSTREAM_READER_LE
 #include "avcodec.h"
@@ -79,7 +78,7 @@ typedef void (*ivi_mc_avg_func) (int16_t *buf, const int16_t *ref_buf1,
                                  const int16_t *ref_buf2,
                                  ptrdiff_t pitch, int mc_type, int mc_type2);
 
-static int ivi_mc(IVIBandDesc *band, ivi_mc_func mc, ivi_mc_avg_func mc_avg,
+static int ivi_mc(const IVIBandDesc *band, ivi_mc_func mc, ivi_mc_avg_func mc_avg,
                   int offs, int mv_x, int mv_y, int mv_x2, int mv_y2,
                   int mc_type, int mc_type2)
 {
@@ -354,23 +353,11 @@ av_cold int ff_ivi_init_planes(AVCodecContext *avctx, IVIPlaneDesc *planes, cons
             band->height   = b_height;
             band->pitch    = width_aligned;
             band->aheight  = height_aligned;
-            band->bufs[0]  = av_mallocz(buf_size);
-            band->bufs[1]  = av_mallocz(buf_size);
+            av_assert0(!band->bufs[0] && !band->bufs[1] &&
+                       !band->bufs[2] && !band->bufs[3]);
             band->bufsize  = buf_size/2;
-            if (!band->bufs[0] || !band->bufs[1])
-                return AVERROR(ENOMEM);
+            av_assert0(buf_size % 2 == 0);
 
-            /* allocate the 3rd band buffer for scalability mode */
-            if (cfg->luma_bands > 1) {
-                band->bufs[2] = av_mallocz(buf_size);
-                if (!band->bufs[2])
-                    return AVERROR(ENOMEM);
-            }
-            if (is_indeo4) {
-                band->bufs[3]  = av_mallocz(buf_size);
-                if (!band->bufs[3])
-                    return AVERROR(ENOMEM);
-            }
             /* reset custom vlc */
             planes[p].bands[0].blk_vlc.cust_desc.num_rows = 0;
         }
@@ -379,7 +366,7 @@ av_cold int ff_ivi_init_planes(AVCodecContext *avctx, IVIPlaneDesc *planes, cons
     return 0;
 }
 
-static int ivi_init_tiles(IVIBandDesc *band, IVITile *ref_tile,
+static int ivi_init_tiles(const IVIBandDesc *band, IVITile *ref_tile,
                           int p, int b, int t_height, int t_width)
 {
     int x, y;
@@ -429,6 +416,10 @@ av_cold int ff_ivi_init_tiles(IVIPlaneDesc *planes,
         t_height = !p ? tile_height : (tile_height + 3) >> 2;
 
         if (!p && planes[0].num_bands == 4) {
+            if (t_width % 2 || t_height % 2) {
+                avpriv_request_sample(NULL, "Odd tiles");
+                return AVERROR_PATCHWELCOME;
+            }
             t_width  >>= 1;
             t_height >>= 1;
         }
@@ -484,7 +475,7 @@ static int ivi_dec_tile_data_size(GetBitContext *gb)
     if (get_bits1(gb)) {
         len = get_bits(gb, 8);
         if (len == 255)
-            len = get_bits_long(gb, 24);
+            len = get_bits(gb, 24);
     }
 
     /* align the bitstream reader on the byte boundary */
@@ -493,22 +484,16 @@ static int ivi_dec_tile_data_size(GetBitContext *gb)
     return len;
 }
 
-static int ivi_dc_transform(IVIBandDesc *band, int *prev_dc, int buf_offs,
+static int ivi_dc_transform(const IVIBandDesc *band, int *prev_dc, int buf_offs,
                             int blk_size)
 {
-    int buf_size = band->pitch * band->aheight - buf_offs;
-    int min_size = (blk_size - 1) * band->pitch + blk_size;
-
-    if (min_size > buf_size)
-        return AVERROR_INVALIDDATA;
-
     band->dc_transform(prev_dc, band->buf + buf_offs,
                        band->pitch, blk_size);
 
     return 0;
 }
 
-static int ivi_decode_coded_blocks(GetBitContext *gb, IVIBandDesc *band,
+static int ivi_decode_coded_blocks(GetBitContext *gb, const IVIBandDesc *band,
                                    ivi_mc_func mc, ivi_mc_avg_func mc_avg,
                                    int mv_x, int mv_y,
                                    int mv_x2, int mv_y2,
@@ -619,7 +604,7 @@ static int ivi_decode_coded_blocks(GetBitContext *gb, IVIBandDesc *band,
  *  @param[in]      tile  pointer to the tile descriptor
  *  @return     result code: 0 - OK, -1 = error (corrupted blocks data)
  */
-static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band,
+static int ivi_decode_blocks(GetBitContext *gb, const IVIBandDesc *band,
                              IVITile *tile, AVCodecContext *avctx)
 {
     int mbn, blk, num_blocks, blk_size, ret, is_intra;
@@ -732,6 +717,11 @@ static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band,
                 if (ret < 0)
                     return ret;
             } else {
+                int buf_size = band->pitch * band->aheight - buf_offs;
+                int min_size = (blk_size - 1) * band->pitch + blk_size;
+
+                if (min_size > buf_size)
+                    return AVERROR_INVALIDDATA;
                 /* block not coded */
                 /* for intra blocks apply the dc slant transform */
                 /* for inter - perform the motion compensation without delta */
@@ -766,7 +756,7 @@ static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band,
  *  @param[in]  tile      pointer to the tile descriptor
  *  @param[in]  mv_scale  scaling factor for motion vectors
  */
-static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
+static int ivi_process_empty_tile(AVCodecContext *avctx, const IVIBandDesc *band,
                                   IVITile *tile, int32_t mv_scale)
 {
     int             x, y, need_mc, mbn, blk, num_blocks, mv_x, mv_y, mc_type;
@@ -775,24 +765,29 @@ static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
     const int16_t   *src;
     int16_t         *dst;
     ivi_mc_func     mc_no_delta_func;
+    int             clear_first = !band->qdelta_present && !band->plane && !band->band_num;
+    int             mb_size     = band->mb_size;
+    int             xend        = tile->xpos + tile->width;
+    int             is_halfpel  = band->is_halfpel;
+    int             pitch       = band->pitch;
 
-    if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
+    if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, mb_size)) {
         av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches "
                "parameters %d in ivi_process_empty_tile()\n",
-               tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
+               tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, mb_size));
         return AVERROR_INVALIDDATA;
     }
 
-    offs       = tile->ypos * band->pitch + tile->xpos;
+    offs       = tile->ypos * pitch + tile->xpos;
     mb         = tile->mbs;
     ref_mb     = tile->ref_mbs;
-    row_offset = band->mb_size * band->pitch;
+    row_offset = mb_size * pitch;
     need_mc    = 0; /* reset the mc tracking flag */
 
-    for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
+    for (y = tile->ypos; y < (tile->ypos + tile->height); y += mb_size) {
         mb_offset = offs;
 
-        for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
+        for (x = tile->xpos; x < xend; x += mb_size) {
             mb->xpos     = x;
             mb->ypos     = y;
             mb->buf_offs = mb_offset;
@@ -800,54 +795,54 @@ static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
             mb->type = 1; /* set the macroblocks type = INTER */
             mb->cbp  = 0; /* all blocks are empty */
 
-            if (!band->qdelta_present && !band->plane && !band->band_num) {
+            if (clear_first) {
                 mb->q_delta = band->glob_quant;
                 mb->mv_x    = 0;
                 mb->mv_y    = 0;
             }
 
             if (ref_mb) {
-            if (band->inherit_qdelta)
-                mb->q_delta = ref_mb->q_delta;
-
-            if (band->inherit_mv) {
-                /* motion vector inheritance */
-                if (mv_scale) {
-                    mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
-                    mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
-                } else {
-                    mb->mv_x = ref_mb->mv_x;
-                    mb->mv_y = ref_mb->mv_y;
-                }
-                need_mc |= mb->mv_x || mb->mv_y; /* tracking non-zero motion vectors */
-                {
-                    int dmv_x, dmv_y, cx, cy;
-
-                    dmv_x = mb->mv_x >> band->is_halfpel;
-                    dmv_y = mb->mv_y >> band->is_halfpel;
-                    cx    = mb->mv_x &  band->is_halfpel;
-                    cy    = mb->mv_y &  band->is_halfpel;
-
-                    if (   mb->xpos + dmv_x < 0
-                        || mb->xpos + dmv_x + band->mb_size + cx > band->pitch
-                        || mb->ypos + dmv_y < 0
-                        || mb->ypos + dmv_y + band->mb_size + cy > band->aheight) {
-                        av_log(avctx, AV_LOG_ERROR, "MV out of bounds\n");
-                        return AVERROR_INVALIDDATA;
+                if (band->inherit_qdelta)
+                    mb->q_delta = ref_mb->q_delta;
+
+                if (band->inherit_mv) {
+                    /* motion vector inheritance */
+                    if (mv_scale) {
+                        mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
+                        mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
+                    } else {
+                        mb->mv_x = ref_mb->mv_x;
+                        mb->mv_y = ref_mb->mv_y;
+                    }
+                    need_mc |= mb->mv_x || mb->mv_y; /* tracking non-zero motion vectors */
+                    {
+                        int dmv_x, dmv_y, cx, cy;
+
+                        dmv_x = mb->mv_x >> is_halfpel;
+                        dmv_y = mb->mv_y >> is_halfpel;
+                        cx    = mb->mv_x &  is_halfpel;
+                        cy    = mb->mv_y &  is_halfpel;
+
+                        if (   mb->xpos + dmv_x < 0
+                            || mb->xpos + dmv_x + mb_size + cx > pitch
+                            || mb->ypos + dmv_y < 0
+                            || mb->ypos + dmv_y + mb_size + cy > band->aheight) {
+                            av_log(avctx, AV_LOG_ERROR, "MV out of bounds\n");
+                            return AVERROR_INVALIDDATA;
+                        }
                     }
                 }
-            }
                 ref_mb++;
             }
 
             mb++;
-            mb_offset += band->mb_size;
+            mb_offset += mb_size;
         } // for x
         offs += row_offset;
     } // for y
 
     if (band->inherit_mv && need_mc) { /* apply motion compensation if there is at least one non-zero motion vector */
-        num_blocks = (band->mb_size != band->blk_size) ? 4 : 1; /* number of blocks per mb */
+        num_blocks = (mb_size != band->blk_size) ? 4 : 1; /* number of blocks per mb */
         mc_no_delta_func = (band->blk_size == 8) ? ff_ivi_mc_8x8_no_delta
                                                  : ff_ivi_mc_4x4_no_delta;
 
@@ -864,7 +859,7 @@ static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
 
             for (blk = 0; blk < num_blocks; blk++) {
                 /* adjust block position in the buffer according with its number */
-                offs = mb->buf_offs + band->blk_size * ((blk & 1) + !!(blk & 2) * band->pitch);
+                offs = mb->buf_offs + band->blk_size * ((blk & 1) + !!(blk & 2) * pitch);
                 ret = ivi_mc(band, mc_no_delta_func, 0, offs,
                              mv_x, mv_y, 0, 0, mc_type, -1);
                 if (ret < 0)
@@ -873,12 +868,12 @@ static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
         }
     } else {
         /* copy data from the reference tile into the current one */
-        src = band->ref_buf + tile->ypos * band->pitch + tile->xpos;
-        dst = band->buf     + tile->ypos * band->pitch + tile->xpos;
+        src = band->ref_buf + tile->ypos * pitch + tile->xpos;
+        dst = band->buf     + tile->ypos * pitch + tile->xpos;
         for (y = 0; y < tile->height; y++) {
             memcpy(dst, src, tile->width*sizeof(band->buf[0]));
-            src += band->pitch;
-            dst += band->pitch;
+            src += pitch;
+            dst += pitch;
         }
     }
 
@@ -887,7 +882,7 @@ static int ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
 
 
 #ifdef DEBUG
-static uint16_t ivi_calc_band_checksum(IVIBandDesc *band)
+static uint16_t ivi_calc_band_checksum(const IVIBandDesc *band)
 {
     int         x, y;
     int16_t     *src, checksum;
@@ -937,6 +932,15 @@ static void ivi_output_plane(IVIPlaneDesc *plane, uint8_t *dst, ptrdiff_t dst_pi
     }
 }
 
+static void *prepare_buf(IVI45DecContext *ctx, IVIBandDesc *band, int i)
+{
+    if (ctx->pic_conf.luma_bands <= 1 && i == 2)
+        return NULL;
+    if (!band->bufs[i])
+        band->bufs[i] = av_mallocz(2 * band->bufsize);
+    return band->bufs[i];
+}
+
 /**
  *  Decode an Indeo 4 or 5 band.
  *
@@ -951,18 +955,22 @@ static int decode_band(IVI45DecContext *ctx,
     int         result, i, t, idx1, idx2, pos;
     IVITile     *tile;
 
-    band->buf     = band->bufs[ctx->dst_buf];
+    band->buf     = prepare_buf(ctx, band, ctx->dst_buf);
     if (!band->buf) {
         av_log(avctx, AV_LOG_ERROR, "Band buffer points to no data!\n");
         return AVERROR_INVALIDDATA;
     }
     if (ctx->is_indeo4 && ctx->frame_type == IVI4_FRAMETYPE_BIDIR) {
-        band->ref_buf   = band->bufs[ctx->b_ref_buf];
-        band->b_ref_buf = band->bufs[ctx->ref_buf];
+        band->ref_buf   = prepare_buf(ctx, band, ctx->b_ref_buf);
+        band->b_ref_buf = prepare_buf(ctx, band, ctx->ref_buf);
+        if (!band->b_ref_buf)
+            return AVERROR(ENOMEM);
     } else {
-        band->ref_buf   = band->bufs[ctx->ref_buf];
+        band->ref_buf   = prepare_buf(ctx, band, ctx->ref_buf);
         band->b_ref_buf = 0;
     }
+    if (!band->ref_buf)
+        return AVERROR(ENOMEM);
     band->data_ptr  = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
 
     result = ctx->decode_band_hdr(ctx, band, avctx);
@@ -1115,8 +1123,6 @@ int ff_ivi_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
     ctx->switch_buffers(ctx);
 
-    //{ START_TIMER;
-
     if (ctx->is_nonnull_frame(ctx)) {
         ctx->buf_invalid[ctx->dst_buf] = 1;
         for (p = 0; p < 3; p++) {
@@ -1142,8 +1148,6 @@ int ff_ivi_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
     if (ctx->buf_invalid[ctx->dst_buf])
         return -1;
 
-    //STOP_TIMER("decode_planes"); }
-
     if (!ctx->is_nonnull_frame(ctx))
         return buf_size;
 
@@ -1184,10 +1188,12 @@ int ff_ivi_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         left = get_bits_count(&ctx->gb) & 0x18;
         skip_bits_long(&ctx->gb, 64 - left);
         if (get_bits_left(&ctx->gb) > 18 &&
-            show_bits_long(&ctx->gb, 21) == 0xBFFF8) { // syncheader + inter type
+            show_bits(&ctx->gb, 21) == 0xBFFF8) { // syncheader + inter type
             AVPacket pkt;
             pkt.data = avpkt->data + (get_bits_count(&ctx->gb) >> 3);
             pkt.size = get_bits_left(&ctx->gb) >> 3;
+            ctx->got_p_frame = 0;
+            av_frame_unref(ctx->p_frame);
             ff_ivi_decode_frame(avctx, ctx->p_frame, &ctx->got_p_frame, &pkt);
         }
     }