LCOV - code coverage report
Current view: top level - libavcodec - indeo5.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 239 351 68.1 %
Date: 2017-12-15 18:13:28 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*
       2             :  * Indeo Video Interactive v5 compatible decoder
       3             :  * Copyright (c) 2009 Maxim Poliakovski
       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             : /**
      23             :  * @file
      24             :  * Indeo Video Interactive version 5 decoder
      25             :  *
      26             :  * Indeo5 data is usually transported within .avi or .mov files.
      27             :  * Known FOURCCs: 'IV50'
      28             :  */
      29             : 
      30             : #define BITSTREAM_READER_LE
      31             : #include "avcodec.h"
      32             : #include "get_bits.h"
      33             : #include "ivi.h"
      34             : #include "ivi_dsp.h"
      35             : #include "indeo5data.h"
      36             : 
      37             : /**
      38             :  *  Indeo5 frame types.
      39             :  */
      40             : enum {
      41             :     FRAMETYPE_INTRA       = 0,
      42             :     FRAMETYPE_INTER       = 1,  ///< non-droppable P-frame
      43             :     FRAMETYPE_INTER_SCAL  = 2,  ///< droppable P-frame used in the scalability mode
      44             :     FRAMETYPE_INTER_NOREF = 3,  ///< droppable P-frame
      45             :     FRAMETYPE_NULL        = 4   ///< empty frame with no data
      46             : };
      47             : 
      48             : #define IVI5_PIC_SIZE_ESC       15
      49             : 
      50             : /**
      51             :  *  Decode Indeo5 GOP (Group of pictures) header.
      52             :  *  This header is present in key frames only.
      53             :  *  It defines parameters for all frames in a GOP.
      54             :  *
      55             :  *  @param[in,out] ctx    ptr to the decoder context
      56             :  *  @param[in]     avctx  ptr to the AVCodecContext
      57             :  *  @return         result code: 0 = OK, -1 = error
      58             :  */
      59           9 : static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
      60             : {
      61             :     int             result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
      62           9 :     int             quant_mat, blk_size_changed = 0;
      63             :     IVIBandDesc     *band, *band1, *band2;
      64             :     IVIPicConfig    pic_conf;
      65             : 
      66           9 :     ctx->gop_flags = get_bits(&ctx->gb, 8);
      67             : 
      68           9 :     ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
      69             : 
      70           9 :     if (ctx->gop_flags & IVI5_IS_PROTECTED)
      71           0 :         ctx->lock_word = get_bits_long(&ctx->gb, 32);
      72             : 
      73           9 :     tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
      74           9 :     if (tile_size > 256) {
      75           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
      76           0 :         return AVERROR_INVALIDDATA;
      77             :     }
      78             : 
      79             :     /* decode number of wavelet bands */
      80             :     /* num_levels * 3 + 1 */
      81           9 :     pic_conf.luma_bands   = get_bits(&ctx->gb, 2) * 3 + 1;
      82           9 :     pic_conf.chroma_bands = get_bits1(&ctx->gb)   * 3 + 1;
      83           9 :     is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
      84           9 :     if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
      85           0 :         av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
      86           0 :                pic_conf.luma_bands, pic_conf.chroma_bands);
      87           0 :         return AVERROR_INVALIDDATA;
      88             :     }
      89             : 
      90           9 :     pic_size_indx = get_bits(&ctx->gb, 4);
      91           9 :     if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
      92           0 :         pic_conf.pic_height = get_bits(&ctx->gb, 13);
      93           0 :         pic_conf.pic_width  = get_bits(&ctx->gb, 13);
      94             :     } else {
      95           9 :         pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
      96           9 :         pic_conf.pic_width  = ivi5_common_pic_sizes[pic_size_indx * 2    ] << 2;
      97             :     }
      98             : 
      99           9 :     if (ctx->gop_flags & 2) {
     100           0 :         avpriv_report_missing_feature(avctx, "YV12 picture format");
     101           0 :         return AVERROR_PATCHWELCOME;
     102             :     }
     103             : 
     104           9 :     pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
     105           9 :     pic_conf.chroma_width  = (pic_conf.pic_width  + 3) >> 2;
     106             : 
     107           9 :     if (!tile_size) {
     108           9 :         pic_conf.tile_height = pic_conf.pic_height;
     109           9 :         pic_conf.tile_width  = pic_conf.pic_width;
     110             :     } else {
     111           0 :         pic_conf.tile_height = pic_conf.tile_width = tile_size;
     112             :     }
     113             : 
     114             :     /* check if picture layout was changed and reallocate buffers */
     115           9 :     if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
     116           0 :         result = ff_ivi_init_planes(avctx, ctx->planes, &pic_conf, 0);
     117           0 :         if (result < 0) {
     118           0 :             av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
     119           0 :             return result;
     120             :         }
     121           0 :         ctx->pic_conf = pic_conf;
     122           0 :         ctx->is_scalable = is_scalable;
     123           0 :         blk_size_changed = 1; /* force reallocation of the internal structures */
     124             :     }
     125             : 
     126          27 :     for (p = 0; p <= 1; p++) {
     127          36 :         for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
     128          18 :             band = &ctx->planes[p].bands[i];
     129             : 
     130          18 :             band->is_halfpel = get_bits1(&ctx->gb);
     131             : 
     132          18 :             mb_size  = get_bits1(&ctx->gb);
     133          18 :             blk_size = 8 >> get_bits1(&ctx->gb);
     134          18 :             mb_size  = blk_size << !mb_size;
     135             : 
     136          18 :             if (p==0 && blk_size==4) {
     137           0 :                 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
     138           0 :                 return AVERROR_PATCHWELCOME;
     139             :             }
     140             : 
     141          18 :             blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
     142          18 :             if (blk_size_changed) {
     143           2 :                 band->mb_size  = mb_size;
     144           2 :                 band->blk_size = blk_size;
     145             :             }
     146             : 
     147          18 :             if (get_bits1(&ctx->gb)) {
     148           0 :                 avpriv_report_missing_feature(avctx, "Extended transform info");
     149           0 :                 return AVERROR_PATCHWELCOME;
     150             :             }
     151             : 
     152             :             /* select transform function and scan pattern according to plane and band number */
     153          18 :             switch ((p << 2) + i) {
     154           9 :             case 0:
     155           9 :                 band->inv_transform  = ff_ivi_inverse_slant_8x8;
     156           9 :                 band->dc_transform   = ff_ivi_dc_slant_2d;
     157           9 :                 band->scan           = ff_zigzag_direct;
     158           9 :                 band->transform_size = 8;
     159           9 :                 break;
     160             : 
     161           0 :             case 1:
     162           0 :                 band->inv_transform  = ff_ivi_row_slant8;
     163           0 :                 band->dc_transform   = ff_ivi_dc_row_slant;
     164           0 :                 band->scan           = ff_ivi_vertical_scan_8x8;
     165           0 :                 band->transform_size = 8;
     166           0 :                 break;
     167             : 
     168           0 :             case 2:
     169           0 :                 band->inv_transform  = ff_ivi_col_slant8;
     170           0 :                 band->dc_transform   = ff_ivi_dc_col_slant;
     171           0 :                 band->scan           = ff_ivi_horizontal_scan_8x8;
     172           0 :                 band->transform_size = 8;
     173           0 :                 break;
     174             : 
     175           0 :             case 3:
     176           0 :                 band->inv_transform  = ff_ivi_put_pixels_8x8;
     177           0 :                 band->dc_transform   = ff_ivi_put_dc_pixel_8x8;
     178           0 :                 band->scan           = ff_ivi_horizontal_scan_8x8;
     179           0 :                 band->transform_size = 8;
     180           0 :                 break;
     181             : 
     182           9 :             case 4:
     183           9 :                 band->inv_transform  = ff_ivi_inverse_slant_4x4;
     184           9 :                 band->dc_transform   = ff_ivi_dc_slant_2d;
     185           9 :                 band->scan           = ff_ivi_direct_scan_4x4;
     186           9 :                 band->transform_size = 4;
     187           9 :                 break;
     188             :             }
     189             : 
     190          27 :             band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
     191           9 :                                 band->inv_transform == ff_ivi_inverse_slant_4x4;
     192             : 
     193          18 :             if (band->transform_size != band->blk_size) {
     194           0 :                 av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
     195           0 :                 return AVERROR_INVALIDDATA;
     196             :             }
     197             : 
     198             :             /* select dequant matrix according to plane and band number */
     199          18 :             if (!p) {
     200           9 :                 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
     201             :             } else {
     202           9 :                 quant_mat = 5;
     203             :             }
     204             : 
     205          18 :             if (band->blk_size == 8) {
     206           9 :                 if(quant_mat >= 5){
     207           0 :                     av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
     208           0 :                     return -1;
     209             :                 }
     210           9 :                 band->intra_base  = &ivi5_base_quant_8x8_intra[quant_mat][0];
     211           9 :                 band->inter_base  = &ivi5_base_quant_8x8_inter[quant_mat][0];
     212           9 :                 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
     213           9 :                 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
     214             :             } else {
     215           9 :                 band->intra_base  = ivi5_base_quant_4x4_intra;
     216           9 :                 band->inter_base  = ivi5_base_quant_4x4_inter;
     217           9 :                 band->intra_scale = ivi5_scale_quant_4x4_intra;
     218           9 :                 band->inter_scale = ivi5_scale_quant_4x4_inter;
     219             :             }
     220             : 
     221          18 :             if (get_bits(&ctx->gb, 2)) {
     222           0 :                 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
     223           0 :                 return AVERROR_INVALIDDATA;
     224             :             }
     225             :         }
     226             :     }
     227             : 
     228             :     /* copy chroma parameters into the 2nd chroma plane */
     229          18 :     for (i = 0; i < pic_conf.chroma_bands; i++) {
     230           9 :         band1 = &ctx->planes[1].bands[i];
     231           9 :         band2 = &ctx->planes[2].bands[i];
     232             : 
     233           9 :         band2->width         = band1->width;
     234           9 :         band2->height        = band1->height;
     235           9 :         band2->mb_size       = band1->mb_size;
     236           9 :         band2->blk_size      = band1->blk_size;
     237           9 :         band2->is_halfpel    = band1->is_halfpel;
     238           9 :         band2->intra_base    = band1->intra_base;
     239           9 :         band2->inter_base    = band1->inter_base;
     240           9 :         band2->intra_scale   = band1->intra_scale;
     241           9 :         band2->inter_scale   = band1->inter_scale;
     242           9 :         band2->scan          = band1->scan;
     243           9 :         band2->inv_transform = band1->inv_transform;
     244           9 :         band2->dc_transform  = band1->dc_transform;
     245           9 :         band2->is_2d_trans   = band1->is_2d_trans;
     246           9 :         band2->transform_size= band1->transform_size;
     247             :     }
     248             : 
     249             :     /* reallocate internal structures if needed */
     250           9 :     if (blk_size_changed) {
     251           1 :         result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
     252           1 :                                    pic_conf.tile_height);
     253           1 :         if (result < 0) {
     254           0 :             av_log(avctx, AV_LOG_ERROR,
     255             :                    "Couldn't reallocate internal structures!\n");
     256           0 :             return result;
     257             :         }
     258             :     }
     259             : 
     260           9 :     if (ctx->gop_flags & 8) {
     261           0 :         if (get_bits(&ctx->gb, 3)) {
     262           0 :             av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
     263           0 :             return AVERROR_INVALIDDATA;
     264             :         }
     265             : 
     266           0 :         if (get_bits1(&ctx->gb))
     267           0 :             skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
     268             :     }
     269             : 
     270           9 :     align_get_bits(&ctx->gb);
     271             : 
     272           9 :     skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
     273             : 
     274             :     /* skip GOP extension if any */
     275           9 :     if (get_bits1(&ctx->gb)) {
     276             :         do {
     277           0 :             i = get_bits(&ctx->gb, 16);
     278           0 :         } while (i & 0x8000);
     279             :     }
     280             : 
     281           9 :     align_get_bits(&ctx->gb);
     282             : 
     283           9 :     return 0;
     284             : }
     285             : 
     286             : 
     287             : /**
     288             :  *  Skip a header extension.
     289             :  *
     290             :  *  @param[in,out]  gb  the GetBit context
     291             :  */
     292           0 : static inline int skip_hdr_extension(GetBitContext *gb)
     293             : {
     294             :     int i, len;
     295             : 
     296             :     do {
     297           0 :         len = get_bits(gb, 8);
     298           0 :         if (8*len > get_bits_left(gb))
     299           0 :             return AVERROR_INVALIDDATA;
     300           0 :         for (i = 0; i < len; i++) skip_bits(gb, 8);
     301           0 :     } while(len);
     302             : 
     303           0 :     return 0;
     304             : }
     305             : 
     306             : 
     307             : /**
     308             :  *  Decode Indeo5 picture header.
     309             :  *
     310             :  *  @param[in,out]  ctx    ptr to the decoder context
     311             :  *  @param[in]      avctx  ptr to the AVCodecContext
     312             :  *  @return         result code: 0 = OK, -1 = error
     313             :  */
     314         134 : static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
     315             : {
     316             :     int ret;
     317             : 
     318         134 :     if (get_bits(&ctx->gb, 5) != 0x1F) {
     319           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
     320           0 :         return AVERROR_INVALIDDATA;
     321             :     }
     322             : 
     323         134 :     ctx->prev_frame_type = ctx->frame_type;
     324         134 :     ctx->frame_type      = get_bits(&ctx->gb, 3);
     325         134 :     if (ctx->frame_type >= 5) {
     326           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
     327           0 :         return AVERROR_INVALIDDATA;
     328             :     }
     329             : 
     330         134 :     ctx->frame_num = get_bits(&ctx->gb, 8);
     331             : 
     332         134 :     if (ctx->frame_type == FRAMETYPE_INTRA) {
     333           9 :         if ((ret = decode_gop_header(ctx, avctx)) < 0) {
     334           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
     335           0 :             ctx->gop_invalid = 1;
     336           0 :             return ret;
     337             :         }
     338           9 :         ctx->gop_invalid = 0;
     339             :     }
     340             : 
     341         134 :     if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
     342           0 :         av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scalable stream\n");
     343           0 :         ctx->frame_type = FRAMETYPE_INTER;
     344           0 :         return AVERROR_INVALIDDATA;
     345             :     }
     346             : 
     347         134 :     if (ctx->frame_type != FRAMETYPE_NULL) {
     348         133 :         ctx->frame_flags = get_bits(&ctx->gb, 8);
     349             : 
     350         133 :         ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
     351             : 
     352         133 :         ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
     353             : 
     354             :         /* skip unknown extension if any */
     355         133 :         if (ctx->frame_flags & 0x20)
     356           0 :             skip_hdr_extension(&ctx->gb); /* XXX: untested */
     357             : 
     358             :         /* decode macroblock huffman codebook */
     359         133 :         ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40,
     360             :                                    IVI_MB_HUFF, &ctx->mb_vlc, avctx);
     361         133 :         if (ret < 0)
     362           0 :             return ret;
     363             : 
     364         133 :         skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
     365             :     }
     366             : 
     367         134 :     align_get_bits(&ctx->gb);
     368             : 
     369         134 :     return 0;
     370             : }
     371             : 
     372             : 
     373             : /**
     374             :  *  Decode Indeo5 band header.
     375             :  *
     376             :  *  @param[in,out]  ctx    ptr to the decoder context
     377             :  *  @param[in,out]  band   ptr to the band descriptor
     378             :  *  @param[in]      avctx  ptr to the AVCodecContext
     379             :  *  @return         result code: 0 = OK, -1 = error
     380             :  */
     381         399 : static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
     382             :                            AVCodecContext *avctx)
     383             : {
     384             :     int         i, ret;
     385             :     uint8_t     band_flags;
     386             : 
     387         399 :     band_flags = get_bits(&ctx->gb, 8);
     388             : 
     389         399 :     if (band_flags & 1) {
     390           0 :         band->is_empty = 1;
     391           0 :         return 0;
     392             :     }
     393             : 
     394         399 :     band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
     395             : 
     396         399 :     band->inherit_mv     = band_flags & 2;
     397         399 :     band->inherit_qdelta = band_flags & 8;
     398         399 :     band->qdelta_present = band_flags & 4;
     399         399 :     if (!band->qdelta_present) band->inherit_qdelta = 1;
     400             : 
     401             :     /* decode rvmap probability corrections if any */
     402         399 :     band->num_corr = 0; /* there are no corrections */
     403         399 :     if (band_flags & 0x10) {
     404         252 :         band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
     405         252 :         if (band->num_corr > 61) {
     406           0 :             av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
     407             :                    band->num_corr);
     408           0 :             return AVERROR_INVALIDDATA;
     409             :         }
     410             : 
     411             :         /* read correction pairs */
     412        2316 :         for (i = 0; i < band->num_corr * 2; i++)
     413        2064 :             band->corr[i] = get_bits(&ctx->gb, 8);
     414             :     }
     415             : 
     416             :     /* select appropriate rvmap table for this band */
     417         399 :     band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
     418             : 
     419             :     /* decode block huffman codebook */
     420         399 :     ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF,
     421             :                                &band->blk_vlc, avctx);
     422         399 :     if (ret < 0)
     423           0 :         return ret;
     424             : 
     425         399 :     band->checksum_present = get_bits1(&ctx->gb);
     426         399 :     if (band->checksum_present)
     427         399 :         band->checksum = get_bits(&ctx->gb, 16);
     428             : 
     429         399 :     band->glob_quant = get_bits(&ctx->gb, 5);
     430             : 
     431             :     /* skip unknown extension if any */
     432         399 :     if (band_flags & 0x20) { /* XXX: untested */
     433           0 :         align_get_bits(&ctx->gb);
     434           0 :         skip_hdr_extension(&ctx->gb);
     435             :     }
     436             : 
     437         399 :     align_get_bits(&ctx->gb);
     438             : 
     439         399 :     return 0;
     440             : }
     441             : 
     442             : 
     443             : /**
     444             :  *  Decode info (block type, cbp, quant delta, motion vector)
     445             :  *  for all macroblocks in the current tile.
     446             :  *
     447             :  *  @param[in,out]  ctx    ptr to the decoder context
     448             :  *  @param[in,out]  band   ptr to the band descriptor
     449             :  *  @param[in,out]  tile   ptr to the tile descriptor
     450             :  *  @param[in]      avctx  ptr to the AVCodecContext
     451             :  *  @return         result code: 0 = OK, -1 = error
     452             :  */
     453         399 : static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
     454             :                           IVITile *tile, AVCodecContext *avctx)
     455             : {
     456             :     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
     457             :                 mv_scale, blks_per_mb, s;
     458             :     IVIMbInfo   *mb, *ref_mb;
     459         399 :     int         row_offset = band->mb_size * band->pitch;
     460             : 
     461         399 :     mb     = tile->mbs;
     462         399 :     ref_mb = tile->ref_mbs;
     463         399 :     offs   = tile->ypos * band->pitch + tile->xpos;
     464             : 
     465         532 :     if (!ref_mb &&
     466         133 :         ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
     467           0 :         return AVERROR_INVALIDDATA;
     468             : 
     469         399 :     if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
     470           0 :         av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
     471           0 :                tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
     472           0 :         return AVERROR_INVALIDDATA;
     473             :     }
     474             : 
     475             :     /* scale factor for motion vectors */
     476         399 :     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
     477         399 :     mv_x = mv_y = 0;
     478             : 
     479        5187 :     for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
     480        4788 :         mb_offset = offs;
     481             : 
     482       76608 :         for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
     483       71820 :             mb->xpos     = x;
     484       71820 :             mb->ypos     = y;
     485       71820 :             mb->buf_offs = mb_offset;
     486             : 
     487       71820 :             if (get_bits1(&ctx->gb)) {
     488       27025 :                 if (ctx->frame_type == FRAMETYPE_INTRA) {
     489           0 :                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
     490           0 :                     return AVERROR_INVALIDDATA;
     491             :                 }
     492       27025 :                 mb->type = 1; /* empty macroblocks are always INTER */
     493       27025 :                 mb->cbp  = 0; /* all blocks are empty */
     494             : 
     495       27025 :                 mb->q_delta = 0;
     496       27025 :                 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
     497           0 :                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
     498             :                                            IVI_VLC_BITS, 1);
     499           0 :                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
     500             :                 }
     501             : 
     502       27025 :                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
     503       27025 :                 if (band->inherit_mv && ref_mb){
     504             :                     /* motion vector inheritance */
     505       19295 :                     if (mv_scale) {
     506       19295 :                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
     507       19295 :                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
     508             :                     } else {
     509           0 :                         mb->mv_x = ref_mb->mv_x;
     510           0 :                         mb->mv_y = ref_mb->mv_y;
     511             :                     }
     512             :                 }
     513             :             } else {
     514       44795 :                 if (band->inherit_mv && ref_mb) {
     515       28585 :                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
     516       16210 :                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
     517        1620 :                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
     518             :                 } else {
     519       14590 :                     mb->type = get_bits1(&ctx->gb);
     520             :                 }
     521             : 
     522       44795 :                 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
     523       44795 :                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
     524             : 
     525       44795 :                 mb->q_delta = 0;
     526       44795 :                 if (band->qdelta_present) {
     527           0 :                     if (band->inherit_qdelta) {
     528           0 :                         if (ref_mb) mb->q_delta = ref_mb->q_delta;
     529           0 :                     } else if (mb->cbp || (!band->plane && !band->band_num &&
     530           0 :                                            (ctx->frame_flags & 8))) {
     531           0 :                         mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
     532             :                                                IVI_VLC_BITS, 1);
     533           0 :                         mb->q_delta = IVI_TOSIGNED(mb->q_delta);
     534             :                     }
     535             :                 }
     536             : 
     537       44795 :                 if (!mb->type) {
     538       13239 :                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
     539             :                 } else {
     540       31556 :                     if (band->inherit_mv && ref_mb){
     541             :                         /* motion vector inheritance */
     542       39518 :                         if (mv_scale) {
     543       19759 :                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
     544       19759 :                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
     545             :                         } else {
     546           0 :                             mb->mv_x = ref_mb->mv_x;
     547           0 :                             mb->mv_y = ref_mb->mv_y;
     548             :                         }
     549             :                     } else {
     550             :                         /* decode motion vector deltas */
     551       11797 :                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
     552             :                                             IVI_VLC_BITS, 1);
     553       11797 :                         mv_y += IVI_TOSIGNED(mv_delta);
     554       11797 :                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
     555             :                                             IVI_VLC_BITS, 1);
     556       11797 :                         mv_x += IVI_TOSIGNED(mv_delta);
     557       11797 :                         mb->mv_x = mv_x;
     558       11797 :                         mb->mv_y = mv_y;
     559             :                     }
     560             :                 }
     561             :             }
     562             : 
     563       71820 :             s= band->is_halfpel;
     564       71820 :             if (mb->type)
     565      117162 :             if ( x +  (mb->mv_x   >>s) +                 (y+               (mb->mv_y   >>s))*band->pitch < 0 ||
     566       58581 :                  x + ((mb->mv_x+s)>>s) + band->mb_size - 1
     567       58581 :                    + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
     568           0 :                 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
     569           0 :                 return AVERROR_INVALIDDATA;
     570             :             }
     571             : 
     572       71820 :             mb++;
     573       71820 :             if (ref_mb)
     574       47880 :                 ref_mb++;
     575       71820 :             mb_offset += band->mb_size;
     576             :         }
     577             : 
     578        4788 :         offs += row_offset;
     579             :     }
     580             : 
     581         399 :     align_get_bits(&ctx->gb);
     582             : 
     583         399 :     return 0;
     584             : }
     585             : 
     586             : 
     587             : /**
     588             :  *  Switch buffers.
     589             :  *
     590             :  *  @param[in,out] ctx  ptr to the decoder context
     591             :  */
     592         134 : static void switch_buffers(IVI45DecContext *ctx)
     593             : {
     594         134 :     switch (ctx->prev_frame_type) {
     595         133 :     case FRAMETYPE_INTRA:
     596             :     case FRAMETYPE_INTER:
     597         133 :         ctx->buf_switch ^= 1;
     598         133 :         ctx->dst_buf = ctx->buf_switch;
     599         133 :         ctx->ref_buf = ctx->buf_switch ^ 1;
     600         133 :         break;
     601           0 :     case FRAMETYPE_INTER_SCAL:
     602           0 :         if (!ctx->inter_scal) {
     603           0 :             ctx->ref2_buf   = 2;
     604           0 :             ctx->inter_scal = 1;
     605             :         }
     606           0 :         FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
     607           0 :         ctx->ref_buf = ctx->ref2_buf;
     608           0 :         break;
     609           0 :     case FRAMETYPE_INTER_NOREF:
     610           0 :         break;
     611             :     }
     612             : 
     613         134 :     switch (ctx->frame_type) {
     614           9 :     case FRAMETYPE_INTRA:
     615           9 :         ctx->buf_switch = 0;
     616             :         /* FALLTHROUGH */
     617         133 :     case FRAMETYPE_INTER:
     618         133 :         ctx->inter_scal = 0;
     619         133 :         ctx->dst_buf = ctx->buf_switch;
     620         133 :         ctx->ref_buf = ctx->buf_switch ^ 1;
     621         133 :         break;
     622           1 :     case FRAMETYPE_INTER_SCAL:
     623             :     case FRAMETYPE_INTER_NOREF:
     624             :     case FRAMETYPE_NULL:
     625           1 :         break;
     626             :     }
     627         134 : }
     628             : 
     629             : 
     630         268 : static int is_nonnull_frame(IVI45DecContext *ctx)
     631             : {
     632         268 :     return ctx->frame_type != FRAMETYPE_NULL;
     633             : }
     634             : 
     635             : 
     636             : /**
     637             :  *  Initialize Indeo5 decoder.
     638             :  */
     639           2 : static av_cold int decode_init(AVCodecContext *avctx)
     640             : {
     641           2 :     IVI45DecContext  *ctx = avctx->priv_data;
     642             :     int             result;
     643             : 
     644           2 :     ff_ivi_init_static_vlc();
     645             : 
     646             :     /* copy rvmap tables in our context so we can apply changes to them */
     647           2 :     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
     648             : 
     649             :     /* set the initial picture layout according to the basic profile:
     650             :        there is only one band per plane (no scalability), only one tile (no local decoding)
     651             :        and picture format = YVU9 */
     652           2 :     ctx->pic_conf.pic_width     = avctx->width;
     653           2 :     ctx->pic_conf.pic_height    = avctx->height;
     654           2 :     ctx->pic_conf.chroma_width  = (avctx->width  + 3) >> 2;
     655           2 :     ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
     656           2 :     ctx->pic_conf.tile_width    = avctx->width;
     657           2 :     ctx->pic_conf.tile_height   = avctx->height;
     658           2 :     ctx->pic_conf.luma_bands    = ctx->pic_conf.chroma_bands = 1;
     659             : 
     660           2 :     result = ff_ivi_init_planes(avctx, ctx->planes, &ctx->pic_conf, 0);
     661           2 :     if (result) {
     662           0 :         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
     663           0 :         return AVERROR_INVALIDDATA;
     664             :     }
     665             : 
     666           2 :     ctx->buf_switch = 0;
     667           2 :     ctx->inter_scal = 0;
     668             : 
     669           2 :     ctx->decode_pic_hdr   = decode_pic_hdr;
     670           2 :     ctx->decode_band_hdr  = decode_band_hdr;
     671           2 :     ctx->decode_mb_info   = decode_mb_info;
     672           2 :     ctx->switch_buffers   = switch_buffers;
     673           2 :     ctx->is_nonnull_frame = is_nonnull_frame;
     674             : 
     675           2 :     ctx->is_indeo4 = 0;
     676             : 
     677           2 :     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
     678             : 
     679           2 :     return 0;
     680             : }
     681             : 
     682             : AVCodec ff_indeo5_decoder = {
     683             :     .name           = "indeo5",
     684             :     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
     685             :     .type           = AVMEDIA_TYPE_VIDEO,
     686             :     .id             = AV_CODEC_ID_INDEO5,
     687             :     .priv_data_size = sizeof(IVI45DecContext),
     688             :     .init           = decode_init,
     689             :     .close          = ff_ivi_decode_close,
     690             :     .decode         = ff_ivi_decode_frame,
     691             :     .capabilities   = AV_CODEC_CAP_DR1,
     692             : };

Generated by: LCOV version 1.13