X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fivi.c;h=cefaf77bfd36a2eb60bcce9c4298c522077b3cac;hb=3749eede66c3774799766b1f246afae8a6ffc9bb;hp=e9c4b9b831f21a605066b9811a199e82f1dd94a8;hpb=67d466d09b105b2b1d3d8da4c21d8975925741ae;p=ffmpeg diff --git a/libavcodec/ivi.c b/libavcodec/ivi.c index e9c4b9b831f..cefaf77bfd3 100644 --- a/libavcodec/ivi.c +++ b/libavcodec/ivi.c @@ -30,13 +30,12 @@ #include "libavutil/attributes.h" #include "libavutil/imgutils.h" -#include "libavutil/timer.h" +#include "libavutil/thread.h" #define BITSTREAM_READER_LE #include "avcodec.h" #include "get_bits.h" #include "internal.h" -#include "mathops.h" #include "ivi.h" #include "ivi_dsp.h" @@ -116,23 +115,6 @@ static int ivi_mc(const IVIBandDesc *band, ivi_mc_func mc, ivi_mc_avg_func mc_av return 0; } -/** - * Reverse "nbits" bits of the value "val" and return the result - * in the least significant bits. - */ -static uint16_t inv_bits(uint16_t val, int nbits) -{ - uint16_t res; - - if (nbits <= 8) { - res = ff_reverse[val] >> (8 - nbits); - } else - res = ((ff_reverse[val & 0xFF] << 8) + - (ff_reverse[val >> 8])) >> (16 - nbits); - - return res; -} - /* * Generate a huffman codebook from the given descriptor * and convert it into the FFmpeg VLC table. @@ -163,7 +145,7 @@ static int ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag) if (bits[pos] > IVI_VLC_BITS) return AVERROR_INVALIDDATA; /* invalid descriptor */ - codewords[pos] = inv_bits((prefix | j), bits[pos]); + codewords[pos] = prefix | j; if (!bits[pos]) bits[pos] = 1; @@ -173,17 +155,14 @@ static int ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag) /* number of codewords = pos */ return init_vlc(vlc, IVI_VLC_BITS, pos, bits, 1, 1, codewords, 2, 2, - (flag ? INIT_VLC_USE_NEW_STATIC : 0) | INIT_VLC_LE); + (flag ? INIT_VLC_USE_NEW_STATIC : 0) | INIT_VLC_OUTPUT_LE); } -av_cold void ff_ivi_init_static_vlc(void) +static av_cold void ivi_init_static_vlc(void) { int i; static VLC_TYPE table_data[8192 * 16][2]; - static int initialized_vlcs = 0; - if (initialized_vlcs) - return; for (i = 0; i < 8; i++) { ivi_mb_vlc_tabs[i].table = table_data + i * 2 * 8192; ivi_mb_vlc_tabs[i].table_allocated = 8192; @@ -194,7 +173,12 @@ av_cold void ff_ivi_init_static_vlc(void) ivi_create_huff_from_desc(&ivi_blk_huff_desc[i], &ivi_blk_vlc_tabs[i], 1); } - initialized_vlcs = 1; +} + +av_cold void ff_ivi_init_static_vlc(void) +{ + static AVOnce init_static_once = AV_ONCE_INIT; + ff_thread_once(&init_static_once, ivi_init_static_vlc); } /* @@ -284,18 +268,20 @@ static av_cold void ivi_free_buffers(IVIPlaneDesc *planes) int p, b, t; for (p = 0; p < 3; p++) { - if (planes[p].bands) - for (b = 0; b < planes[p].num_bands; b++) { - av_freep(&planes[p].bands[b].bufs[0]); - av_freep(&planes[p].bands[b].bufs[1]); - av_freep(&planes[p].bands[b].bufs[2]); - av_freep(&planes[p].bands[b].bufs[3]); - - if (planes[p].bands[b].blk_vlc.cust_tab.table) - ff_free_vlc(&planes[p].bands[b].blk_vlc.cust_tab); - for (t = 0; t < planes[p].bands[b].num_tiles; t++) - av_freep(&planes[p].bands[b].tiles[t].mbs); - av_freep(&planes[p].bands[b].tiles); + if (planes[p].bands) { + for (b = 0; b < planes[p].num_bands; b++) { + IVIBandDesc *band = &planes[p].bands[b]; + av_freep(&band->bufs[0]); + av_freep(&band->bufs[1]); + av_freep(&band->bufs[2]); + av_freep(&band->bufs[3]); + + if (band->blk_vlc.cust_tab.table) + ff_free_vlc(&band->blk_vlc.cust_tab); + for (t = 0; t < band->num_tiles; t++) + av_freep(&band->tiles[t].mbs); + av_freep(&band->tiles); + } } av_freep(&planes[p].bands); planes[p].num_bands = 0; @@ -354,23 +340,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; } @@ -429,6 +403,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; } @@ -451,8 +429,10 @@ av_cold int ff_ivi_init_tiles(IVIPlaneDesc *planes, av_freep(&band->tiles); band->tiles = av_mallocz_array(band->num_tiles, sizeof(IVITile)); - if (!band->tiles) + if (!band->tiles) { + band->num_tiles = 0; return AVERROR(ENOMEM); + } /* use the first luma band as reference for motion vectors * and quant */ @@ -484,7 +464,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 */ @@ -941,6 +921,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. * @@ -955,18 +944,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); @@ -1119,8 +1112,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++) { @@ -1146,8 +1137,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; @@ -1188,10 +1177,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); } }