From adfe0c942e71545f003f9c4d148fbf5d220681bc Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 6 Dec 2011 14:50:32 +0100 Subject: [PATCH 1/6] Indeo 4 decoder Signed-off-by: Kostya Shishkov --- Changelog | 1 + doc/general.texi | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/indeo4.c | 823 ++++++++++++++++++++++++++++++++++++++++ libavcodec/indeo4data.h | 350 +++++++++++++++++ libavcodec/ivi_dsp.c | 149 +++++++- libavcodec/ivi_dsp.h | 39 +- libavcodec/version.h | 2 +- 9 files changed, 1364 insertions(+), 3 deletions(-) create mode 100644 libavcodec/indeo4.c create mode 100644 libavcodec/indeo4data.h diff --git a/Changelog b/Changelog index 263e6d939d..8176297040 100644 --- a/Changelog +++ b/Changelog @@ -111,6 +111,7 @@ easier to use. The changes are: - v410 QuickTime uncompressed 4:4:4 10-bit encoder and decoder - OpenMG Audio muxer - Simple segmenting muxer +- Indeo 4 decoder version 0.7: diff --git a/doc/general.texi b/doc/general.texi index 74f3cb6607..313d73f771 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -491,6 +491,7 @@ following image formats are supported: @item Intel H.263 @tab @tab X @item Intel Indeo 2 @tab @tab X @item Intel Indeo 3 @tab @tab X +@item Intel Indeo 4 @tab @tab X @item Intel Indeo 5 @tab @tab X @item Interplay C93 @tab @tab X @tab Used in the game Cyberia from Interplay. diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b426129af3..741df91527 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -191,6 +191,7 @@ OBJS-$(CONFIG_IFF_ILBM_DECODER) += iff.o OBJS-$(CONFIG_IMC_DECODER) += imc.o OBJS-$(CONFIG_INDEO2_DECODER) += indeo2.o OBJS-$(CONFIG_INDEO3_DECODER) += indeo3.o +OBJS-$(CONFIG_INDEO4_DECODER) += indeo4.o ivi_common.o ivi_dsp.o OBJS-$(CONFIG_INDEO5_DECODER) += indeo5.o ivi_common.o ivi_dsp.o OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER) += dpcm.o OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 8a09b90f47..3980982d26 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -128,6 +128,7 @@ void avcodec_register_all(void) REGISTER_DECODER (IFF_ILBM, iff_ilbm); REGISTER_DECODER (INDEO2, indeo2); REGISTER_DECODER (INDEO3, indeo3); + REGISTER_DECODER (INDEO4, indeo4); REGISTER_DECODER (INDEO5, indeo5); REGISTER_DECODER (INTERPLAY_VIDEO, interplay_video); REGISTER_ENCDEC (JPEGLS, jpegls); diff --git a/libavcodec/indeo4.c b/libavcodec/indeo4.c new file mode 100644 index 0000000000..8f6d2eab65 --- /dev/null +++ b/libavcodec/indeo4.c @@ -0,0 +1,823 @@ +/* + * Indeo Video Interactive v4 compatible decoder + * Copyright (c) 2009-2011 Maxim Poliakovski + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Indeo Video Interactive version 4 decoder + * + * Indeo 4 data is usually transported within .avi or .mov files. + * Known FOURCCs: 'IV41' + */ + +#define BITSTREAM_READER_LE +#include "avcodec.h" +#include "get_bits.h" +#include "dsputil.h" +#include "ivi_dsp.h" +#include "ivi_common.h" +#include "indeo4data.h" + +#define IVI4_STREAM_ANALYSER 0 +#define IVI4_DEBUG_CHECKSUM 0 + +/** + * Indeo 4 frame types. + */ +enum { + FRAMETYPE_INTRA = 0, + FRAMETYPE_BIDIR1 = 1, ///< bidirectional frame + FRAMETYPE_INTER = 2, ///< non-droppable P-frame + FRAMETYPE_BIDIR = 3, ///< bidirectional frame + FRAMETYPE_INTER_NOREF = 4, ///< droppable P-frame + FRAMETYPE_NULL_FIRST = 5, ///< empty frame with no data + FRAMETYPE_NULL_LAST = 6 ///< empty frame with no data +}; + +#define IVI4_PIC_SIZE_ESC 7 + + +typedef struct { + GetBitContext gb; + AVFrame frame; + RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables + + uint32_t frame_num; + int frame_type; + int prev_frame_type; ///< frame type of the previous frame + uint32_t data_size; ///< size of the frame data in bytes from picture header + int is_scalable; + int transp_status; ///< transparency mode status: 1 - enabled + + IVIPicConfig pic_conf; + IVIPlaneDesc planes[3]; ///< color planes + + int buf_switch; ///< used to switch between three buffers + int dst_buf; ///< buffer index for the currently decoded frame + int ref_buf; ///< inter frame reference buffer index + + IVIHuffTab mb_vlc; ///< current macroblock table descriptor + IVIHuffTab blk_vlc; ///< current block table descriptor + + uint16_t checksum; ///< frame checksum + + uint8_t rvmap_sel; + uint8_t in_imf; + uint8_t in_q; ///< flag for explicitly stored quantiser delta + uint8_t pic_glob_quant; + uint8_t unknown1; + +#if IVI4_STREAM_ANALYSER + uint8_t has_b_frames; + uint8_t has_transp; + uint8_t uses_tiling; + uint8_t uses_haar; + uint8_t uses_fullpel; +#endif +} IVI4DecContext; + + +struct { + InvTransformPtr *inv_trans; + DCTransformPtr *dc_trans; + int is_2d_trans; +} transforms[18] = { + { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 }, + { NULL, NULL, 0 }, /* inverse Haar 8x1 */ + { NULL, NULL, 0 }, /* inverse Haar 1x8 */ + { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 }, + { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 }, + { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 }, + { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 }, + { NULL, NULL, 0 }, /* inverse DCT 8x8 */ + { NULL, NULL, 0 }, /* inverse DCT 8x1 */ + { NULL, NULL, 0 }, /* inverse DCT 1x8 */ + { NULL, NULL, 0 }, /* inverse Haar 4x4 */ + { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 }, + { NULL, NULL, 0 }, /* no transform 4x4 */ + { NULL, NULL, 0 }, /* inverse Haar 1x4 */ + { NULL, NULL, 0 }, /* inverse Haar 4x1 */ + { NULL, NULL, 0 }, /* inverse slant 1x4 */ + { NULL, NULL, 0 }, /* inverse slant 4x1 */ + { NULL, NULL, 0 }, /* inverse DCT 4x4 */ +}; + +/** + * Decode subdivision of a plane. + * This is a simplified version that checks for two supported subdivisions: + * - 1 wavelet band per plane, size factor 1:1, code pattern: 3 + * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3 + * Anything else is either unsupported or corrupt. + * + * @param[in,out] gb the GetBit context + * @return number of wavelet bands or 0 on error + */ +static int decode_plane_subdivision(GetBitContext *gb) +{ + int i; + + switch (get_bits(gb, 2)) { + case 3: + return 1; + case 2: + for (i = 0; i < 4; i++) + if (get_bits(gb, 2) != 3) + return 0; + return 4; + default: + return 0; + } +} + +static inline int scale_tile_size(int def_size, int size_factor) +{ + return (size_factor == 15 ? def_size : (size_factor + 1) << 5); +} + +/** + * Decode Indeo 4 picture header. + * + * @param[in,out] ctx pointer to the decoder context + * @param[in] avctx pointer to the AVCodecContext + * @return result code: 0 = OK, negative number = error + */ +static int decode_pic_hdr(IVI4DecContext *ctx, AVCodecContext *avctx) +{ + int pic_size_indx, val, i, p; + IVIPicConfig pic_conf; + + if (get_bits(&ctx->gb, 18) != 0x3FFF8) { + av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); + return AVERROR_INVALIDDATA; + } + + ctx->prev_frame_type = ctx->frame_type; + ctx->frame_type = get_bits(&ctx->gb, 3); + if (ctx->frame_type == 7) { + av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type); + return AVERROR_INVALIDDATA; + } + +#if IVI4_STREAM_ANALYSER + if ( ctx->frame_type == FRAMETYPE_BIDIR1 + || ctx->frame_type == FRAMETYPE_BIDIR) + ctx->has_b_frames = 1; +#endif + + ctx->transp_status = get_bits1(&ctx->gb); +#if IVI4_STREAM_ANALYSER + if (ctx->transp_status) { + ctx->has_transp = 1; + } +#endif + + /* unknown bit: Mac decoder ignores this bit, XANIM returns error */ + if (get_bits1(&ctx->gb)) { + av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n"); + return AVERROR_INVALIDDATA; + } + + ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0; + + /* null frames don't contain anything else so we just return */ + if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) { + av_dlog(avctx, "Null frame encountered!\n"); + return 0; + } + + /* Check key lock status. If enabled - ignore lock word. */ + /* Usually we have to prompt the user for the password, but */ + /* we don't do that because Indeo 4 videos can be decoded anyway */ + if (get_bits1(&ctx->gb)) { + skip_bits_long(&ctx->gb, 32); + av_dlog(avctx, "Password-protected clip!\n"); + } + + pic_size_indx = get_bits(&ctx->gb, 3); + if (pic_size_indx == IVI4_PIC_SIZE_ESC) { + pic_conf.pic_height = get_bits(&ctx->gb, 16); + pic_conf.pic_width = get_bits(&ctx->gb, 16); + } else { + pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1]; + pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ]; + } + + /* Decode tile dimensions. */ + if (get_bits1(&ctx->gb)) { + pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4)); + pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4)); +#if IVI4_STREAM_ANALYSER + ctx->uses_tiling = 1; +#endif + } else { + pic_conf.tile_height = pic_conf.pic_height; + pic_conf.tile_width = pic_conf.pic_width; + } + + /* Decode chroma subsampling. We support only 4:4 aka YVU9. */ + if (get_bits(&ctx->gb, 2)) { + av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n"); + return AVERROR_INVALIDDATA; + } + pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; + pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; + + /* decode subdivision of the planes */ + pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb); + if (pic_conf.luma_bands) + pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb); + ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; + if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { + av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", + pic_conf.luma_bands, pic_conf.chroma_bands); + return AVERROR_INVALIDDATA; + } + + /* check if picture layout was changed and reallocate buffers */ + if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) { + if (ff_ivi_init_planes(ctx->planes, &pic_conf)) { + av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); + return AVERROR(ENOMEM); + } + + ctx->pic_conf = pic_conf; + + /* set default macroblock/block dimensions */ + for (p = 0; p <= 2; p++) { + for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { + ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4; + ctx->planes[p].bands[i].blk_size = !p ? 8 : 4; + } + } + + if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width, + ctx->pic_conf.tile_height)) { + av_log(avctx, AV_LOG_ERROR, + "Couldn't reallocate internal structures!\n"); + return AVERROR(ENOMEM); + } + } + + ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0; + + /* skip decTimeEst field if present */ + if (get_bits1(&ctx->gb)) + skip_bits(&ctx->gb, 8); + + /* decode macroblock and block huffman codebooks */ + if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) || + ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx)) + return AVERROR_INVALIDDATA; + + ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8; + + ctx->in_imf = get_bits1(&ctx->gb); + ctx->in_q = get_bits1(&ctx->gb); + + ctx->pic_glob_quant = get_bits(&ctx->gb, 5); + + /* TODO: ignore this parameter if unused */ + ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0; + + ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0; + + /* skip picture header extension if any */ + while (get_bits1(&ctx->gb)) { + av_dlog(avctx, "Pic hdr extension encountered!\n"); + val = get_bits(&ctx->gb, 8); + } + + if (get_bits1(&ctx->gb)) { + av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n"); + } + + align_get_bits(&ctx->gb); + + return 0; +} + + +/** + * Decode Indeo 4 band header. + * + * @param[in,out] ctx pointer to the decoder context + * @param[in,out] band pointer to the band descriptor + * @param[in] avctx pointer to the AVCodecContext + * @return result code: 0 = OK, negative number = error + */ +static int decode_band_hdr(IVI4DecContext *ctx, IVIBandDesc *band, + AVCodecContext *avctx) +{ + int plane, band_num, hdr_size, indx, transform_id, scan_indx; + int i; + + plane = get_bits(&ctx->gb, 2); + band_num = get_bits(&ctx->gb, 4); + if (band->plane != plane || band->band_num != band_num) { + av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n"); + return AVERROR_INVALIDDATA; + } + + band->is_empty = get_bits1(&ctx->gb); + if (!band->is_empty) { + hdr_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 4; + + band->is_halfpel = get_bits(&ctx->gb, 2); + if (band->is_halfpel >= 2) { + av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n", + band->is_halfpel); + return AVERROR_INVALIDDATA; + } +#if IVI4_STREAM_ANALYSER + if (!band->is_halfpel) + ctx->uses_fullpel = 1; +#endif + + band->checksum_present = get_bits1(&ctx->gb); + if (band->checksum_present) + band->checksum = get_bits(&ctx->gb, 16); + + indx = get_bits(&ctx->gb, 2); + if (indx == 3) { + av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n"); + return AVERROR_INVALIDDATA; + } + band->mb_size = 16 >> indx; + band->blk_size = 8 >> (indx >> 1); + + band->inherit_mv = get_bits1(&ctx->gb); + band->inherit_qdelta = get_bits1(&ctx->gb); + + band->glob_quant = get_bits(&ctx->gb, 5); + + if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) { + transform_id = get_bits(&ctx->gb, 5); + if (!transforms[transform_id].inv_trans) { + av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id); + return AVERROR_PATCHWELCOME; + } + if ((transform_id >= 7 && transform_id <= 9) || + transform_id == 17) { + av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n"); + return AVERROR_PATCHWELCOME; + } + +#if IVI4_STREAM_ANALYSER + if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10) + ctx->uses_haar = 1; +#endif + + band->inv_transform = transforms[transform_id].inv_trans; + band->dc_transform = transforms[transform_id].dc_trans; + band->is_2d_trans = transforms[transform_id].is_2d_trans; + + scan_indx = get_bits(&ctx->gb, 4); + if (scan_indx == 15) { + av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n"); + return AVERROR_INVALIDDATA; + } + band->scan = scan_index_to_tab[scan_indx]; + + band->quant_mat = get_bits(&ctx->gb, 5); + if (band->quant_mat == 31) { + av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n"); + return AVERROR_INVALIDDATA; + } + } + + /* decode block huffman codebook */ + if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, + &band->blk_vlc, avctx)) + return AVERROR_INVALIDDATA; + + /* select appropriate rvmap table for this band */ + band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8; + + /* decode rvmap probability corrections if any */ + band->num_corr = 0; /* there is no corrections */ + if (get_bits1(&ctx->gb)) { + band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ + if (band->num_corr > 61) { + av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", + band->num_corr); + return AVERROR_INVALIDDATA; + } + + /* read correction pairs */ + for (i = 0; i < band->num_corr * 2; i++) + band->corr[i] = get_bits(&ctx->gb, 8); + } + } + + if (band->blk_size == 8) { + band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0]; + band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0]; + } else { + band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0]; + band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0]; + } + + /* Indeo 4 doesn't use scale tables */ + band->intra_scale = NULL; + band->inter_scale = NULL; + + align_get_bits(&ctx->gb); + + return 0; +} + + +/** + * Decode information (block type, cbp, quant delta, motion vector) + * for all macroblocks in the current tile. + * + * @param[in,out] ctx pointer to the decoder context + * @param[in,out] band pointer to the band descriptor + * @param[in,out] tile pointer to the tile descriptor + * @param[in] avctx pointer to the AVCodecContext + * @return result code: 0 = OK, negative number = error + */ +static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band, + IVITile *tile, AVCodecContext *avctx) +{ + int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb, + mv_scale, mb_type_bits; + IVIMbInfo *mb, *ref_mb; + int row_offset = band->mb_size * band->pitch; + + mb = tile->mbs; + ref_mb = tile->ref_mbs; + offs = tile->ypos * band->pitch + tile->xpos; + + blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; + mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1; + + /* scale factor for motion vectors */ + mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); + mv_x = mv_y = 0; + + for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) { + mb_offset = offs; + + for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) { + mb->xpos = x; + mb->ypos = y; + mb->buf_offs = mb_offset; + + if (get_bits1(&ctx->gb)) { + if (ctx->frame_type == FRAMETYPE_INTRA) { + av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); + return AVERROR_INVALIDDATA; + } + mb->type = 1; /* empty macroblocks are always INTER */ + mb->cbp = 0; /* all blocks are empty */ + + mb->q_delta = 0; + if (!band->plane && !band->band_num && ctx->in_q) { + mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, + IVI_VLC_BITS, 1); + mb->q_delta = IVI_TOSIGNED(mb->q_delta); + } + + mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ + 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; + } + } + } else { + if (band->inherit_mv) { + mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ + } else if (ctx->frame_type == FRAMETYPE_INTRA) { + mb->type = 0; /* mb_type is always INTRA for intra-frames */ + } else { + mb->type = get_bits(&ctx->gb, mb_type_bits); + } + + mb->cbp = get_bits(&ctx->gb, blks_per_mb); + + mb->q_delta = 0; + if (band->inherit_qdelta) { + if (ref_mb) mb->q_delta = ref_mb->q_delta; + } else if (mb->cbp || (!band->plane && !band->band_num && + ctx->in_q)) { + mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, + IVI_VLC_BITS, 1); + mb->q_delta = IVI_TOSIGNED(mb->q_delta); + } + + if (!mb->type) { + mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ + } else { + 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; + } + } else { + /* decode motion vector deltas */ + mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, + IVI_VLC_BITS, 1); + mv_y += IVI_TOSIGNED(mv_delta); + mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, + IVI_VLC_BITS, 1); + mv_x += IVI_TOSIGNED(mv_delta); + mb->mv_x = mv_x; + mb->mv_y = mv_y; + } + } + } + + mb++; + if (ref_mb) + ref_mb++; + mb_offset += band->mb_size; + } + + offs += row_offset; + } + + align_get_bits(&ctx->gb); + + return 0; +} + + +/** + * Decode an Indeo 4 band. + * + * @param[in,out] ctx pointer to the decoder context + * @param[in,out] band pointer to the band descriptor + * @param[in] avctx pointer to the AVCodecContext + * @return result code: 0 = OK, negative number = error + */ +static int decode_band(IVI4DecContext *ctx, int plane_num, + IVIBandDesc *band, AVCodecContext *avctx) +{ + int result, i, t, pos, idx1, idx2; + IVITile *tile; + + band->buf = band->bufs[ctx->dst_buf]; + band->ref_buf = band->bufs[ctx->ref_buf]; + + result = decode_band_hdr(ctx, band, avctx); + if (result) { + av_log(avctx, AV_LOG_ERROR, "Error decoding band header\n"); + return result; + } + + if (band->is_empty) { + av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n"); + return AVERROR_INVALIDDATA; + } + + band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel]; + + /* apply corrections to the selected rvmap table if present */ + for (i = 0; i < band->num_corr; i++) { + idx1 = band->corr[i * 2]; + idx2 = band->corr[i * 2 + 1]; + FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); + FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); + } + + pos = get_bits_count(&ctx->gb); + + for (t = 0; t < band->num_tiles; t++) { + tile = &band->tiles[t]; + + tile->is_empty = get_bits1(&ctx->gb); + if (tile->is_empty) { + ff_ivi_process_empty_tile(avctx, band, tile, + (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3)); + av_dlog(avctx, "Empty tile encountered!\n"); + } else { + tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb); + if (!tile->data_size) { + av_log(avctx, AV_LOG_ERROR, "Tile data size is zero!\n"); + return AVERROR_INVALIDDATA; + } + + result = decode_mb_info(ctx, band, tile, avctx); + if (result < 0) + break; + + result = ff_ivi_decode_blocks(&ctx->gb, band, tile); + if (result < 0 || ((get_bits_count(&ctx->gb) - pos) >> 3) != tile->data_size) { + av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n"); + break; + } + + pos += tile->data_size << 3; // skip to next tile + } + } + + /* restore the selected rvmap table by applying its corrections in reverse order */ + for (i = band->num_corr - 1; i >= 0; i--) { + idx1 = band->corr[i * 2]; + idx2 = band->corr[i * 2 + 1]; + FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); + FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); + } + +#if defined(DEBUG) && IVI4_DEBUG_CHECKSUM + if (band->checksum_present) { + uint16_t chksum = ivi_calc_band_checksum(band); + if (chksum != band->checksum) { + av_log(avctx, AV_LOG_ERROR, + "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n", + band->plane, band->band_num, band->checksum, chksum); + } + } +#endif + + align_get_bits(&ctx->gb); + + return 0; +} + + +static av_cold int decode_init(AVCodecContext *avctx) +{ + IVI4DecContext *ctx = avctx->priv_data; + + ff_ivi_init_static_vlc(); + + /* copy rvmap tables in our context so we can apply changes to them */ + memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); + + /* Force allocation of the internal buffers */ + /* during picture header decoding. */ + ctx->pic_conf.pic_width = 0; + ctx->pic_conf.pic_height = 0; + + avctx->pix_fmt = PIX_FMT_YUV410P; + + return 0; +} + + +/** + * Rearrange decoding and reference buffers. + * + * @param[in,out] ctx pointer to the decoder context + */ +static void switch_buffers(IVI4DecContext *ctx) +{ + switch (ctx->prev_frame_type) { + case FRAMETYPE_INTRA: + case FRAMETYPE_INTER: + ctx->buf_switch ^= 1; + ctx->dst_buf = ctx->buf_switch; + ctx->ref_buf = ctx->buf_switch ^ 1; + break; + case FRAMETYPE_INTER_NOREF: + break; + } + + switch (ctx->frame_type) { + case FRAMETYPE_INTRA: + ctx->buf_switch = 0; + /* FALLTHROUGH */ + case FRAMETYPE_INTER: + ctx->dst_buf = ctx->buf_switch; + ctx->ref_buf = ctx->buf_switch ^ 1; + break; + case FRAMETYPE_INTER_NOREF: + case FRAMETYPE_NULL_FIRST: + case FRAMETYPE_NULL_LAST: + break; + } +} + + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + IVI4DecContext *ctx = avctx->priv_data; + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + int result, p, b; + + init_get_bits(&ctx->gb, buf, buf_size * 8); + + result = decode_pic_hdr(ctx, avctx); + if (result) { + av_log(avctx, AV_LOG_ERROR, "Error decoding picture header\n"); + return result; + } + + switch_buffers(ctx); + + if (ctx->frame_type < FRAMETYPE_NULL_FIRST) { + for (p = 0; p < 3; p++) { + for (b = 0; b < ctx->planes[p].num_bands; b++) { + result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx); + if (result) { + av_log(avctx, AV_LOG_ERROR, + "Error decoding band: %d, plane: %d\n", b, p); + return result; + } + } + } + } + + /* If the bidirectional mode is enabled, next I and the following P frame will */ + /* be sent together. Unfortunately the approach below seems to be the only way */ + /* to handle the B-frames mode. That's exactly the same Intel decoders do. */ + if (ctx->frame_type == FRAMETYPE_INTRA) { + while (get_bits(&ctx->gb, 8)); // skip version string + skip_bits_long(&ctx->gb, 64); // skip padding, TODO: implement correct 8-bytes alignment + if (get_bits_left(&ctx->gb) > 18 && show_bits(&ctx->gb, 18) == 0x3FFF8) + av_log(avctx, AV_LOG_ERROR, "Buffer contains IP frames!\n"); + } + + if (ctx->frame.data[0]) + avctx->release_buffer(avctx, &ctx->frame); + + ctx->frame.reference = 0; + if ((result = avctx->get_buffer(avctx, &ctx->frame)) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return result; + } + + if (ctx->is_scalable) { + ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4); + } else { + ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]); + } + + ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]); + ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]); + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = ctx->frame; + + return buf_size; +} + + +static av_cold int decode_close(AVCodecContext *avctx) +{ + IVI4DecContext *ctx = avctx->priv_data; + + ff_ivi_free_buffers(&ctx->planes[0]); + + if (ctx->frame.data[0]) + avctx->release_buffer(avctx, &ctx->frame); + +#if IVI4_STREAM_ANALYSER + if (ctx->is_scalable) + av_log(avctx, AV_LOG_ERROR, "This video uses scalability mode!\n"); + if (ctx->uses_tiling) + av_log(avctx, AV_LOG_ERROR, "This video uses local decoding!\n"); + if (ctx->has_b_frames) + av_log(avctx, AV_LOG_ERROR, "This video contains B-frames!\n"); + if (ctx->has_transp) + av_log(avctx, AV_LOG_ERROR, "Transparency mode is enabled!\n"); + if (ctx->uses_haar) + av_log(avctx, AV_LOG_ERROR, "This video uses Haar transform!\n"); + if (ctx->uses_fullpel) + av_log(avctx, AV_LOG_ERROR, "This video uses fullpel motion vectors!\n"); +#endif + + return 0; +} + + +AVCodec ff_indeo4_decoder = { + .name = "indeo4", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_INDEO4, + .priv_data_size = sizeof(IVI4DecContext), + .init = decode_init, + .close = decode_close, + .decode = decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"), +}; diff --git a/libavcodec/indeo4data.h b/libavcodec/indeo4data.h new file mode 100644 index 0000000000..affd9c6203 --- /dev/null +++ b/libavcodec/indeo4data.h @@ -0,0 +1,350 @@ +/* + * Indeo Video Interactive 4 compatible decoder + * Copyright (c) 2009-2010 Maxim Poliakovski + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * This file contains data needed for the Indeo 4 decoder. + */ + +#ifndef AVCODEC_INDEO4DATA_H +#define AVCODEC_INDEO4DATA_H + +#include +#include "dsputil.h" +#include "ivi_common.h" + +/** + * standard picture dimensions + */ +static const uint16_t ivi4_common_pic_sizes[14] = { + 640, 480, 320, 240, 160, 120, 704, 480, 352, 240, 352, 288, 176, 144 +}; + +/** + * Indeo 4 8x8 scan (zigzag) patterns + */ +static const uint8_t ivi4_alternate_scan_8x8[64] = { + 0, 8, 1, 9, 16, 24, 2, 3, 17, 25, 10, 11, 32, 40, 48, 56, + 4, 5, 6, 7, 33, 41, 49, 57, 18, 19, 26, 27, 12, 13, 14, 15, + 34, 35, 43, 42, 50, 51, 59, 58, 20, 21, 22, 23, 31, 30, 29, 28, + 36, 37, 38, 39, 47, 46, 45, 44, 52, 53, 54, 55, 63, 62, 61, 60 +}; + +static const uint8_t ivi4_alternate_scan_4x4[16] = { + 0, 1, 4, 5, 8, 12, 2, 3, 9, 13, 6, 7, 10, 11, 14, 15 +}; + +static const uint8_t ivi4_vertical_scan_4x4[16] = { + 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 +}; + +static const uint8_t ivi4_horizontal_scan_4x4[16] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 +}; + +static const uint8_t *scan_index_to_tab[15] = { + // for 8x8 transforms + ff_zigzag_direct, + ivi4_alternate_scan_8x8, + ff_ivi_horizontal_scan_8x8, + ff_ivi_vertical_scan_8x8, + ff_zigzag_direct, + + // for 4x4 transforms + ff_ivi_direct_scan_4x4, + ivi4_alternate_scan_4x4, + ivi4_vertical_scan_4x4, + ivi4_horizontal_scan_4x4, + ff_ivi_direct_scan_4x4, + + // TODO: check if those are needed + ff_ivi_horizontal_scan_8x8, + ff_ivi_horizontal_scan_8x8, + ff_ivi_horizontal_scan_8x8, + ff_ivi_horizontal_scan_8x8, + ff_ivi_horizontal_scan_8x8 +}; + +/** + * Indeo 4 dequant tables + */ +static uint16_t ivi4_quant_8x8_intra[9][64] = { + { + 43, 342, 385, 470, 555, 555, 598, 726, + 342, 342, 470, 513, 555, 598, 726, 769, + 385, 470, 555, 555, 598, 726, 726, 811, + 470, 470, 555, 555, 598, 726, 769, 854, + 470, 555, 555, 598, 683, 726, 854, 1025, + 555, 555, 598, 683, 726, 854, 1025, 1153, + 555, 555, 598, 726, 811, 982, 1195, 1451, + 555, 598, 726, 811, 982, 1195, 1451, 1793 + }, + { + 86, 1195, 2390, 2390, 4865, 4865, 4865, 4865, + 1195, 1195, 2390, 2390, 4865, 4865, 4865, 4865, + 2390, 2390, 4865, 4865, 6827, 6827, 6827, 6827, + 2390, 2390, 4865, 4865, 6827, 6827, 6827, 6827, + 4865, 4865, 6827, 6827, 6827, 6827, 6827, 6827, + 4865, 4865, 6827, 6827, 6827, 6827, 6827, 6827, + 4865, 4865, 6827, 6827, 6827, 6827, 6827, 6827, + 4865, 4865, 6827, 6827, 6827, 6827, 6827, 6827 + }, + { + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835, + 235, 1067, 1195, 1323, 1451, 1579, 1707, 1835 + }, + { + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414, + 1707, 1707, 3414, 3414, 3414, 3414, 3414, 3414 + }, + { + 897, 897, 897, 897, 897, 897, 897, 897, + 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, + 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, + 1409, 1409, 1409, 1409, 1409, 1409, 1409, 1409, + 1579, 1579, 1579, 1579, 1579, 1579, 1579, 1579, + 1750, 1750, 1750, 1750, 1750, 1750, 1750, 1750, + 1921, 1921, 1921, 1921, 1921, 1921, 1921, 1921, + 2091, 2091, 2091, 2091, 2091, 2091, 2091, 2091 + }, + { + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414 + }, + { + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390, + 2390, 2390, 2390, 2390, 2390, 2390, 2390, 2390 + }, + { + 22, 171, 214, 257, 257, 299, 299, 342, + 171, 171, 257, 257, 299, 299, 342, 385, + 214, 257, 257, 299, 299, 342, 342, 385, + 257, 257, 257, 299, 299, 342, 385, 427, + 257, 257, 299, 299, 342, 385, 427, 513, + 257, 299, 299, 342, 385, 427, 513, 598, + 299, 299, 299, 385, 385, 470, 598, 726, + 299, 299, 385, 385, 470, 598, 726, 897 + }, + { + 86, 598, 1195, 1195, 2390, 2390, 2390, 2390, + 598, 598, 1195, 1195, 2390, 2390, 2390, 2390, + 1195, 1195, 2390, 2390, 3414, 3414, 3414, 3414, + 1195, 1195, 2390, 2390, 3414, 3414, 3414, 3414, + 2390, 2390, 3414, 3414, 3414, 3414, 3414, 3414, + 2390, 2390, 3414, 3414, 3414, 3414, 3414, 3414, + 2390, 2390, 3414, 3414, 3414, 3414, 3414, 3414, + 2390, 2390, 3414, 3414, 3414, 3414, 3414, 3414 + } +}; + +static uint16_t ivi4_quant_8x8_inter[9][64] = { + { + 427, 427, 470, 427, 427, 427, 470, 470, + 427, 427, 470, 427, 427, 427, 470, 470, + 470, 470, 470, 470, 470, 470, 470, 470, + 427, 427, 470, 470, 427, 427, 470, 470, + 427, 427, 470, 427, 427, 427, 470, 470, + 427, 427, 470, 427, 427, 427, 470, 470, + 470, 470, 470, 470, 470, 470, 470, 470, + 470, 470, 470, 470, 470, 470, 470, 470 + }, + { + 1707, 1707, 2433, 2433, 3414, 3414, 3414, 3414, + 1707, 1707, 2433, 2433, 3414, 3414, 3414, 3414, + 2433, 2433, 3414, 3414, 4822, 4822, 4822, 4822, + 2433, 2433, 3414, 3414, 4822, 4822, 4822, 4822, + 3414, 3414, 4822, 4822, 3414, 3414, 3414, 3414, + 3414, 3414, 4822, 4822, 3414, 3414, 3414, 3414, + 3414, 3414, 4822, 4822, 3414, 3414, 3414, 3414, + 3414, 3414, 4822, 4822, 3414, 3414, 3414, 3414 + }, + { + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281, + 1195, 1195, 1281, 1238, 1195, 1195, 1281, 1281 + }, + { + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433, + 2433, 2433, 3414, 3414, 2433, 2433, 2433, 2433 + }, + { + 1195, 1195, 1195, 1195, 1195, 1195, 1195, 1195, + 1195, 1195, 1195, 1195, 1195, 1195, 1195, 1195, + 1281, 1281, 1281, 1281, 1281, 1281, 1281, 1281, + 1238, 1238, 1238, 1238, 1238, 1238, 1238, 1238, + 1195, 1195, 1195, 1195, 1195, 1195, 1195, 1195, + 1195, 1195, 1195, 1195, 1195, 1195, 1195, 1195, + 1281, 1281, 1281, 1281, 1281, 1281, 1281, 1281, + 1281, 1281, 1281, 1281, 1281, 1281, 1281, 1281 + }, + { + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433, + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 3414, 3414, 3414, 3414, 3414, 3414, 3414, 3414, + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433, + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433, + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433, + 2433, 2433, 2433, 2433, 2433, 2433, 2433, 2433 + }, + { + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707, + 1707, 1707, 1707, 1707, 1707, 1707, 1707, 1707 + }, + { + 86, 171, 171, 214, 214, 214, 214, 257, + 171, 171, 214, 214, 214, 214, 257, 257, + 171, 214, 214, 214, 214, 257, 257, 257, + 214, 214, 214, 214, 257, 257, 257, 299, + 214, 214, 214, 257, 257, 257, 299, 299, + 214, 214, 257, 257, 257, 299, 299, 299, + 214, 257, 257, 257, 299, 299, 299, 342, + 257, 257, 257, 299, 299, 299, 342, 342 + }, + { + 854, 854, 1195, 1195, 1707, 1707, 1707, 1707, + 854, 854, 1195, 1195, 1707, 1707, 1707, 1707, + 1195, 1195, 1707, 1707, 2390, 2390, 2390, 2390, + 1195, 1195, 1707, 1707, 2390, 2390, 2390, 2390, + 1707, 1707, 2390, 2390, 1707, 1707, 1707, 1707, + 1707, 1707, 2390, 2390, 1707, 1707, 1707, 1707, + 1707, 1707, 2390, 2390, 1707, 1707, 1707, 1707, + 1707, 1707, 2390, 2390, 1707, 1707, 1707, 1707 + } +}; + +static uint16_t ivi4_quant_4x4_intra[5][16] = { + { + 22, 214, 257, 299, + 214, 257, 299, 342, + 257, 299, 342, 427, + 299, 342, 427, 513 + }, + { + 129, 1025, 1451, 1451, + 1025, 1025, 1451, 1451, + 1451, 1451, 2049, 2049, + 1451, 1451, 2049, 2049 + }, + { + 43, 171, 171, 171, + 43, 171, 171, 171, + 43, 171, 171, 171, + 43, 171, 171, 171 + }, + { + 43, 43, 43, 43, + 171, 171, 171, 171, + 171, 171, 171, 171, + 171, 171, 171, 171 + }, + { + 43, 43, 43, 43, + 43, 43, 43, 43, + 43, 43, 43, 43, + 43, 43, 43, 43 + } +}; + +static uint16_t ivi4_quant_4x4_inter[5][16] = { + { + 107, 214, 257, 299, + 214, 257, 299, 299, + 257, 299, 299, 342, + 299, 299, 342, 342 + }, + { + 513, 1025, 1238, 1238, + 1025, 1025, 1238, 1238, + 1238, 1238, 1451, 1451, + 1238, 1238, 1451, 1451 + }, + { + 43, 171, 171, 171, + 43, 171, 171, 171, + 43, 171, 171, 171, + 43, 171, 171, 171 + }, + { + 43, 43, 43, 43, + 171, 171, 171, 171, + 171, 171, 171, 171, + 171, 171, 171, 171 + }, + { + 43, 43, 43, 43, + 43, 43, 43, 43, + 43, 43, 43, 43, + 43, 43, 43, 43 + } +}; + +/** + * Table for mapping quant matrix index from the bitstream + * into internal quant table number. + */ +static uint8_t quant_index_to_tab[22] = { + 0, 1, 0, 2, 1, 3, 0, 4, 1, 5, 0, 1, 6, 7, 8, // for 8x8 quant matrixes + 0, 1, 2, 2, 3, 3, 4 // for 4x4 quant matrixes +}; + +#endif /* AVCODEC_INDEO4DATA_H */ diff --git a/libavcodec/ivi_dsp.c b/libavcodec/ivi_dsp.c index 6b62dc8fdb..caad843c8b 100644 --- a/libavcodec/ivi_dsp.c +++ b/libavcodec/ivi_dsp.c @@ -1,7 +1,7 @@ /* * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) * - * Copyright (c) 2009 Maxim Poliakovski + * Copyright (c) 2009-2011 Maxim Poliakovski * * This file is part of Libav. * @@ -178,6 +178,153 @@ void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst, } } +void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst, + const int dst_pitch, const int num_bands) +{ + int x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3; + const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr; + int32_t pitch; + + /* all bands should have the same pitch */ + pitch = plane->bands[0].pitch; + + /* get pointers to the wavelet bands */ + b0_ptr = plane->bands[0].buf; + b1_ptr = plane->bands[1].buf; + b2_ptr = plane->bands[2].buf; + b3_ptr = plane->bands[3].buf; + + for (y = 0; y < plane->height; y += 2) { + for (x = 0, indx = 0; x < plane->width; x += 2, indx++) { + /* load coefficients */ + b0 = b0_ptr[indx]; //should be: b0 = (num_bands > 0) ? b0_ptr[indx] : 0; + b1 = b1_ptr[indx]; //should be: b1 = (num_bands > 1) ? b1_ptr[indx] : 0; + b2 = b2_ptr[indx]; //should be: b2 = (num_bands > 2) ? b2_ptr[indx] : 0; + b3 = b3_ptr[indx]; //should be: b3 = (num_bands > 3) ? b3_ptr[indx] : 0; + + /* haar wavelet recomposition */ + p0 = (b0 + b1 + b2 + b3 + 2) >> 2; + p1 = (b0 + b1 - b2 - b3 + 2) >> 2; + p2 = (b0 - b1 + b2 - b3 + 2) >> 2; + p3 = (b0 - b1 - b2 + b3 + 2) >> 2; + + /* bias, convert and output four pixels */ + dst[x] = av_clip_uint8(p0 + 128); + dst[x + 1] = av_clip_uint8(p1 + 128); + dst[dst_pitch + x] = av_clip_uint8(p2 + 128); + dst[dst_pitch + x + 1] = av_clip_uint8(p3 + 128); + }// for x + + dst += dst_pitch << 1; + + b0_ptr += pitch; + b1_ptr += pitch; + b2_ptr += pitch; + b3_ptr += pitch; + }// for y +} + +/** butterfly operation for the inverse Haar transform */ +#define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \ + t = (s1 - s2) >> 1;\ + o1 = (s1 + s2) >> 1;\ + o2 = t;\ + +/** inverse 8-point Haar transform */ +#define INV_HAAR8(s1, s5, s3, s7, s2, s4, s6, s8,\ + d1, d2, d3, d4, d5, d6, d7, d8,\ + t0, t1, t2, t3, t4, t5, t6, t7, t8) {\ + t1 = s1 << 1; t5 = s5 << 1;\ + IVI_HAAR_BFLY(t1, t5, t1, t5, t0); IVI_HAAR_BFLY(t1, s3, t1, t3, t0);\ + IVI_HAAR_BFLY(t5, s7, t5, t7, t0); IVI_HAAR_BFLY(t1, s2, t1, t2, t0);\ + IVI_HAAR_BFLY(t3, s4, t3, t4, t0); IVI_HAAR_BFLY(t5, s6, t5, t6, t0);\ + IVI_HAAR_BFLY(t7, s8, t7, t8, t0);\ + d1 = COMPENSATE(t1);\ + d2 = COMPENSATE(t2);\ + d3 = COMPENSATE(t3);\ + d4 = COMPENSATE(t4);\ + d5 = COMPENSATE(t5);\ + d6 = COMPENSATE(t6);\ + d7 = COMPENSATE(t7);\ + d8 = COMPENSATE(t8); } + +/** inverse 4-point Haar transform */ +#define INV_HAAR4(s1, s3, s5, s7) {\ + HAAR_BFLY(s1, s5); HAAR_BFLY(s1, s3); HAAR_BFLY(s5, s7);\ + s1 = COMPENSATE(s1);\ + s3 = COMPENSATE(s3);\ + s5 = COMPENSATE(s5);\ + s7 = COMPENSATE(s7); } + +void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch, + const uint8_t *flags) +{ + int i, shift, sp1, sp2, sp3, sp4; + const int32_t *src; + int32_t *dst; + int tmp[64]; + int t0, t1, t2, t3, t4, t5, t6, t7, t8; + + /* apply the InvHaar8 to all columns */ +#define COMPENSATE(x) (x) + src = in; + dst = tmp; + for (i = 0; i < 8; i++) { + if (flags[i]) { + /* pre-scaling */ + shift = !(i & 4); + sp1 = src[ 0] << shift; + sp2 = src[ 8] << shift; + sp3 = src[16] << shift; + sp4 = src[24] << shift; + INV_HAAR8( sp1, sp2, sp3, sp4, + src[32], src[40], src[48], src[56], + dst[ 0], dst[ 8], dst[16], dst[24], + dst[32], dst[40], dst[48], dst[56], + t0, t1, t2, t3, t4, t5, t6, t7, t8); + } else + dst[ 0] = dst[ 8] = dst[16] = dst[24] = + dst[32] = dst[40] = dst[48] = dst[56] = 0; + + src++; + dst++; + } +#undef COMPENSATE + + /* apply the InvHaar8 to all rows */ +#define COMPENSATE(x) (x) + src = tmp; + for (i = 0; i < 8; i++) { + if ( !src[0] && !src[1] && !src[2] && !src[3] + && !src[4] && !src[5] && !src[6] && !src[7]) { + memset(out, 0, 8 * sizeof(out[0])); + } else { + INV_HAAR8(src[0], src[1], src[2], src[3], + src[4], src[5], src[6], src[7], + out[0], out[1], out[2], out[3], + out[4], out[5], out[6], out[7], + t0, t1, t2, t3, t4, t5, t6, t7, t8); + } + src += 8; + out += pitch; + } +#undef COMPENSATE +} + +void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch, + int blk_size) +{ + int x, y; + int16_t dc_coeff; + + dc_coeff = (*in + 0) >> 3; + + for (y = 0; y < blk_size; out += pitch, y++) { + for (x = 0; x < blk_size; x++) + out[x] = dc_coeff; + } +} + /** butterfly operation for the inverse slant transform */ #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \ t = s1 - s2;\ diff --git a/libavcodec/ivi_dsp.h b/libavcodec/ivi_dsp.h index 9632966a9a..04385140f3 100644 --- a/libavcodec/ivi_dsp.h +++ b/libavcodec/ivi_dsp.h @@ -1,7 +1,7 @@ /* * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) * - * Copyright (c) 2009 Maxim Poliakovski + * Copyright (c) 2009-2011 Maxim Poliakovski * * This file is part of Libav. * @@ -43,6 +43,43 @@ void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst, const int dst_pitch, const int num_bands); +/** + * Haar wavelet recomposition filter for Indeo 4 + * + * @param[in] plane pointer to the descriptor of the plane being processed + * @param[out] dst pointer to the destination buffer + * @param[in] dst_pitch pitch of the destination buffer + * @param[in] num_bands number of wavelet bands to be processed + */ +void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst, + const int dst_pitch, const int num_bands); + +/** + * two-dimensional inverse Haar 8x8 transform for Indeo 4 + * + * @param[in] in pointer to the vector of transform coefficients + * @param[out] out pointer to the output buffer (frame) + * @param[in] pitch pitch to move to the next y line + * @param[in] flags pointer to the array of column flags: + * != 0 - non_empty column, 0 - empty one + * (this array must be filled by caller) + */ +void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch, + const uint8_t *flags); + +/** + * DC-only two-dimensional inverse Haar transform for Indeo 4. + * Performing the inverse transform in this case is equivalent to + * spreading DC_coeff >> 3 over the whole block. + * + * @param[in] in pointer to the dc coefficient + * @param[out] out pointer to the output buffer (frame) + * @param[in] pitch pitch to move to the next y line + * @param[in] blk_size transform block size + */ +void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch, + int blk_size); + /** * two-dimensional inverse slant 8x8 transform * diff --git a/libavcodec/version.h b/libavcodec/version.h index f664795824..9b4f6d71b8 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -21,7 +21,7 @@ #define AVCODEC_VERSION_H #define LIBAVCODEC_VERSION_MAJOR 53 -#define LIBAVCODEC_VERSION_MINOR 31 +#define LIBAVCODEC_VERSION_MINOR 32 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ From b5aecc6bc025461e016baad4486c4e1cacbd6263 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sat, 24 Dec 2011 01:57:34 +0000 Subject: [PATCH 2/6] avformat: Add SMJPEG demuxer. Signed-off-by: Paul B Mahol Signed-off-by: Anton Khirnov --- Changelog | 1 + doc/general.texi | 2 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/smjpeg.c | 187 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 6 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 libavformat/smjpeg.c diff --git a/Changelog b/Changelog index 8176297040..19ba73a940 100644 --- a/Changelog +++ b/Changelog @@ -112,6 +112,7 @@ easier to use. The changes are: - OpenMG Audio muxer - Simple segmenting muxer - Indeo 4 decoder +- SMJPEG demuxer version 0.7: diff --git a/doc/general.texi b/doc/general.texi index 313d73f771..8eaa97b17a 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -296,6 +296,8 @@ library: @tab Used in Sierra CD-ROM games. @item Smacker @tab @tab X @tab Multimedia format used by many games. +@item SMJPEG @tab @tab X + @tab Used in certain Loki game ports. @item Sony OpenMG (OMA) @tab X @tab X @tab Audio format used in Sony Sonic Stage and Sony Vegas. @item Sony PlayStation STR @tab @tab X diff --git a/libavformat/Makefile b/libavformat/Makefile index ee623d7b11..de440506ab 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -273,6 +273,7 @@ OBJS-$(CONFIG_SEGMENT_MUXER) += segment.o OBJS-$(CONFIG_SHORTEN_DEMUXER) += rawdec.o OBJS-$(CONFIG_SIFF_DEMUXER) += siff.o OBJS-$(CONFIG_SMACKER_DEMUXER) += smacker.o +OBJS-$(CONFIG_SMJPEG_DEMUXER) += smjpeg.o OBJS-$(CONFIG_SOL_DEMUXER) += sol.o pcm.o OBJS-$(CONFIG_SOX_DEMUXER) += soxdec.o pcm.o OBJS-$(CONFIG_SOX_MUXER) += soxenc.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 083ee08040..1debddbfc4 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -201,6 +201,7 @@ void av_register_all(void) REGISTER_DEMUXER (SHORTEN, shorten); REGISTER_DEMUXER (SIFF, siff); REGISTER_DEMUXER (SMACKER, smacker); + REGISTER_DEMUXER (SMJPEG, smjpeg); REGISTER_DEMUXER (SOL, sol); REGISTER_MUXDEMUX (SOX, sox); REGISTER_MUXDEMUX (SPDIF, spdif); diff --git a/libavformat/smjpeg.c b/libavformat/smjpeg.c new file mode 100644 index 0000000000..dc94361aa1 --- /dev/null +++ b/libavformat/smjpeg.c @@ -0,0 +1,187 @@ +/* + * SMJPEG demuxer + * Copyright (c) 2011 Paul B Mahol + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * This is a demuxer for Loki SDL Motion JPEG files + */ + +#include "avformat.h" +#include "internal.h" +#include "riff.h" + +static const AVCodecTag codec_smjpeg_video_tags[] = { + { CODEC_ID_MJPEG, MKTAG('J', 'F', 'I', 'F') }, + { CODEC_ID_NONE, 0 }, +}; + +static const AVCodecTag codec_smjpeg_audio_tags[] = { + { CODEC_ID_ADPCM_IMA_SMJPEG, MKTAG('A', 'P', 'C', 'M') }, + { CODEC_ID_PCM_S16LE, MKTAG('N', 'O', 'N', 'E') }, + { CODEC_ID_NONE, 0 }, +}; + +typedef struct SMJPEGContext { + int audio_stream_index; + int video_stream_index; +} SMJPEGContext; + +static int smjpeg_probe(AVProbeData *p) +{ + if (!memcmp(p->buf, "\x0\xaSMJPEG", 8)) + return AVPROBE_SCORE_MAX; + return 0; +} + +static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap) +{ + SMJPEGContext *sc = s->priv_data; + AVStream *ast = NULL, *vst = NULL; + AVIOContext *pb = s->pb; + uint32_t version, htype, hlength, duration; + char *comment; + + avio_skip(pb, 8); // magic + version = avio_rb32(pb); + if (version) + av_log_ask_for_sample(s, "unknown version %d\n", version); + + duration = avio_rb32(pb); // in msec + + while (!pb->eof_reached) { + htype = avio_rl32(pb); + switch (htype) { + case MKTAG('_', 'T', 'X', 'T'): + hlength = avio_rb32(pb); + if (!hlength || hlength > 512) + return AVERROR_INVALIDDATA; + comment = av_malloc(hlength + 1); + if (!comment) + return AVERROR(ENOMEM); + if (avio_read(pb, comment, hlength) != hlength) { + av_freep(&comment); + av_log(s, AV_LOG_ERROR, "error when reading comment\n"); + return AVERROR_INVALIDDATA; + } + comment[hlength] = 0; + av_dict_set(&s->metadata, "comment", comment, + AV_DICT_DONT_STRDUP_VAL); + break; + case MKTAG('_', 'S', 'N', 'D'): + if (ast) { + av_log_ask_for_sample(s, "multiple audio streams not supported\n"); + return AVERROR_INVALIDDATA; + } + hlength = avio_rb32(pb); + if (hlength < 8) + return AVERROR_INVALIDDATA; + ast = avformat_new_stream(s, 0); + if (!ast) + return AVERROR(ENOMEM); + ast->codec->codec_type = AVMEDIA_TYPE_AUDIO; + ast->codec->sample_rate = avio_rb16(pb); + ast->codec->bits_per_coded_sample = avio_r8(pb); + ast->codec->channels = avio_r8(pb); + ast->codec->codec_tag = avio_rl32(pb); + ast->codec->codec_id = ff_codec_get_id(codec_smjpeg_audio_tags, + ast->codec->codec_tag); + ast->duration = duration; + sc->audio_stream_index = ast->index; + avpriv_set_pts_info(ast, 32, 1, 1000); + avio_skip(pb, hlength - 8); + break; + case MKTAG('_', 'V', 'I', 'D'): + if (vst) { + av_log_ask_for_sample(s, "multiple video streams not supported\n"); + return AVERROR_INVALIDDATA; + } + hlength = avio_rb32(pb); + if (hlength < 12) + return AVERROR_INVALIDDATA; + avio_skip(pb, 4); // number of frames + vst = avformat_new_stream(s, 0); + if (!vst) + return AVERROR(ENOMEM); + vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; + vst->codec->width = avio_rb16(pb); + vst->codec->height = avio_rb16(pb); + vst->codec->codec_tag = avio_rl32(pb); + vst->codec->codec_id = ff_codec_get_id(codec_smjpeg_video_tags, + vst->codec->codec_tag); + vst->duration = duration; + sc->video_stream_index = vst->index; + avpriv_set_pts_info(vst, 32, 1, 1000); + avio_skip(pb, hlength - 12); + break; + case MKTAG('H', 'E', 'N', 'D'): + return 0; + default: + av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype); + return AVERROR_INVALIDDATA; + } + } + + return AVERROR_EOF; +} + +static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + SMJPEGContext *sc = s->priv_data; + uint32_t dtype, ret, size, timestamp; + + if (s->pb->eof_reached) + return AVERROR_EOF; + dtype = avio_rl32(s->pb); + switch (dtype) { + case MKTAG('s', 'n', 'd', 'D'): + timestamp = avio_rb32(s->pb); + size = avio_rb32(s->pb); + ret = av_get_packet(s->pb, pkt, size); + pkt->stream_index = sc->audio_stream_index; + pkt->pts = timestamp; + break; + case MKTAG('v', 'i', 'd', 'D'): + timestamp = avio_rb32(s->pb); + size = avio_rb32(s->pb); + ret = av_get_packet(s->pb, pkt, size); + pkt->stream_index = sc->video_stream_index; + pkt->pts = timestamp; + break; + case MKTAG('D', 'O', 'N', 'E'): + ret = AVERROR_EOF; + break; + default: + av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype); + ret = AVERROR_INVALIDDATA; + break; + } + return ret; +} + +AVInputFormat ff_smjpeg_demuxer = { + .name = "smjpeg", + .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"), + .priv_data_size = sizeof(SMJPEGContext), + .read_probe = smjpeg_probe, + .read_header = smjpeg_read_header, + .read_packet = smjpeg_read_packet, + .extensions = "mjpg", +}; diff --git a/libavformat/version.h b/libavformat/version.h index a841701e50..e113a24482 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,7 +30,7 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 53 -#define LIBAVFORMAT_VERSION_MINOR 18 +#define LIBAVFORMAT_VERSION_MINOR 19 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ From 6fa9843b4baf93a95be5ce7c6ad5c041992504bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= Date: Mon, 19 Dec 2011 20:03:24 +0100 Subject: [PATCH 3/6] mpegts: properly output large audio packets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not assume the audio packets being always smaller than DEFAULT_PES_PAYLOAD_SIZE. Signed-off-by: Jindřich Makovička Signed-off-by: Anton Khirnov --- libavformat/mpegtsenc.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index 57cfe4552b..48b1617016 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -1007,10 +1007,18 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt) } if (ts_st->payload_size + size > DEFAULT_PES_PAYLOAD_SIZE) { - mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, - ts_st->payload_pts, ts_st->payload_dts, - ts_st->payload_flags & AV_PKT_FLAG_KEY); - ts_st->payload_size = 0; + if (ts_st->payload_size) { + mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, + ts_st->payload_pts, ts_st->payload_dts, + ts_st->payload_flags & AV_PKT_FLAG_KEY); + ts_st->payload_size = 0; + } + if (size > DEFAULT_PES_PAYLOAD_SIZE) { + mpegts_write_pes(s, st, buf, size, pts, dts, + pkt->flags & AV_PKT_FLAG_KEY); + av_free(data); + return 0; + } } if (!ts_st->payload_size) { From 891ce00c8ddad096cc26d7aa55b4f1bc26a1f124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= Date: Mon, 19 Dec 2011 20:15:21 +0100 Subject: [PATCH 4/6] mpegts: adjustable minimum PES payload With the current default PES packet size, and very small audio bitrates, audio packet duration gets too long. For players, which wait for a whole audio packet (or more) it takes a very long time to start playing sound. For 24kbps audio, one PES packet is about 1 second long. On Motorola STBs, we observe about 3 second delay before the playback starts with the default setting. Signed-off-by: Anton Khirnov --- libavformat/mpegtsenc.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index 48b1617016..90f223273e 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -68,6 +68,7 @@ typedef struct MpegTSWrite { int tsid; int64_t first_pcr; int mux_rate; ///< set to 1 when VBR + int pes_payload_size; int transport_stream_id; int original_network_id; @@ -77,6 +78,10 @@ typedef struct MpegTSWrite { int start_pid; } MpegTSWrite; +/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */ +#define DEFAULT_PES_HEADER_FREQ 16 +#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170) + static const AVOption options[] = { { "mpegts_transport_stream_id", "Set transport_stream_id field.", offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT, {.dbl = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM}, @@ -89,6 +94,8 @@ static const AVOption options[] = { { "mpegts_start_pid", "Set the first pid.", offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, {.dbl = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM}, { "muxrate", NULL, offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, {1}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + { "pes_payload_size", "Minimum PES packet payload in bytes", + offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, {DEFAULT_PES_PAYLOAD_SIZE}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, { NULL }, }; @@ -188,10 +195,6 @@ static int mpegts_write_section1(MpegTSSection *s, int tid, int id, #define DEFAULT_PROVIDER_NAME "Libav" #define DEFAULT_SERVICE_NAME "Service01" -/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */ -#define DEFAULT_PES_HEADER_FREQ 16 -#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170) - /* we retransmit the SI info at this rate */ #define SDT_RETRANS_TIME 500 #define PAT_RETRANS_TIME 100 @@ -206,7 +209,7 @@ typedef struct MpegTSWriteStream { int64_t payload_pts; int64_t payload_dts; int payload_flags; - uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE]; + uint8_t *payload; ADTSContext *adts; } MpegTSWriteStream; @@ -453,6 +456,9 @@ static int mpegts_write_header(AVFormatContext *s) const char *provider_name; int *pids; + // round up to a whole number of TS packets + ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14; + ts->tsid = ts->transport_stream_id; ts->onid = ts->original_network_id; /* allocate a single DVB service */ @@ -489,6 +495,9 @@ static int mpegts_write_header(AVFormatContext *s) if (!ts_st) goto fail; st->priv_data = ts_st; + ts_st->payload = av_mallocz(ts->pes_payload_size); + if (!ts_st->payload) + goto fail; ts_st->service = service; /* MPEG pid values < 16 are reserved. Applications which set st->id in * this range are assigned a calculated pid. */ @@ -524,10 +533,10 @@ static int mpegts_write_header(AVFormatContext *s) st->codec->extradata_size > 0) { ts_st->adts = av_mallocz(sizeof(*ts_st->adts)); if (!ts_st->adts) - return AVERROR(ENOMEM); + goto fail; if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata, st->codec->extradata_size) < 0) - return -1; + goto fail; } } @@ -595,7 +604,13 @@ static int mpegts_write_header(AVFormatContext *s) fail: av_free(pids); for(i = 0;i < s->nb_streams; i++) { + MpegTSWriteStream *ts_st; st = s->streams[i]; + ts_st = st->priv_data; + if (ts_st) { + av_freep(&ts_st->payload); + av_freep(&ts_st->adts); + } av_freep(&st->priv_data); } return -1; @@ -924,6 +939,7 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt) int size = pkt->size; uint8_t *buf= pkt->data; uint8_t *data= NULL; + MpegTSWrite *ts = s->priv_data; MpegTSWriteStream *ts_st = st->priv_data; const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2; int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE; @@ -1006,14 +1022,14 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt) return 0; } - if (ts_st->payload_size + size > DEFAULT_PES_PAYLOAD_SIZE) { + if (ts_st->payload_size + size > ts->pes_payload_size) { if (ts_st->payload_size) { mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, ts_st->payload_pts, ts_st->payload_dts, ts_st->payload_flags & AV_PKT_FLAG_KEY); ts_st->payload_size = 0; } - if (size > DEFAULT_PES_PAYLOAD_SIZE) { + if (size > ts->pes_payload_size) { mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY); av_free(data); @@ -1052,6 +1068,7 @@ static int mpegts_write_end(AVFormatContext *s) ts_st->payload_pts, ts_st->payload_dts, ts_st->payload_flags & AV_PKT_FLAG_KEY); } + av_freep(&ts_st->payload); av_freep(&ts_st->adts); } avio_flush(s->pb); From 733b23c2e44cb67b90e04f337eb4a407d46f70d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= Date: Tue, 27 Dec 2011 20:43:16 -0500 Subject: [PATCH 5/6] fate: Add v210 codec regression tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Reimar Döffinger Signed-off-by: Anton Khirnov --- tests/codec-regression.sh | 5 +++++ tests/ref/vsynth1/v210 | 4 ++++ tests/ref/vsynth2/v210 | 4 ++++ 3 files changed, 13 insertions(+) create mode 100644 tests/ref/vsynth1/v210 create mode 100644 tests/ref/vsynth2/v210 diff --git a/tests/codec-regression.sh b/tests/codec-regression.sh index 57e5f07f05..e20bf6420d 100755 --- a/tests/codec-regression.sh +++ b/tests/codec-regression.sh @@ -278,6 +278,11 @@ do_video_encoding rgb.avi "-an -vcodec rawvideo -pix_fmt bgr24" do_video_decoding "" "-pix_fmt yuv420p" fi +if [ -n "$do_v210" ] ; then +do_video_encoding v210.avi "-an -c:v v210" +do_video_decoding "" "-pix_fmt yuv420p" +fi + if [ -n "$do_yuv" ] ; then do_video_encoding yuv.avi "-an -vcodec rawvideo -pix_fmt yuv420p" do_video_decoding "" "-pix_fmt yuv420p" diff --git a/tests/ref/vsynth1/v210 b/tests/ref/vsynth1/v210 new file mode 100644 index 0000000000..bb84c3ec98 --- /dev/null +++ b/tests/ref/vsynth1/v210 @@ -0,0 +1,4 @@ +dd6c870a2a52c9e75ce61c3670e710e7 *./tests/data/vsynth1/v210.avi +14752460 ./tests/data/vsynth1/v210.avi +50973792d3f1abe04a51ee0121f077f2 *./tests/data/v210.vsynth1.out.yuv +stddev: 1.85 PSNR: 42.78 MAXDIFF: 29 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth2/v210 b/tests/ref/vsynth2/v210 new file mode 100644 index 0000000000..31160bdc61 --- /dev/null +++ b/tests/ref/vsynth2/v210 @@ -0,0 +1,4 @@ +db0579bd46e1ba133ff86c0f7cdd761f *./tests/data/vsynth2/v210.avi +14752460 ./tests/data/vsynth2/v210.avi +a627fb50c8276200fd71383977d87ca3 *./tests/data/v210.vsynth2.out.yuv +stddev: 0.34 PSNR: 57.43 MAXDIFF: 6 bytes: 7603200/ 7603200 From 8f5216905f88510c9a74cd91a424902aa989b9a1 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Wed, 28 Dec 2011 15:04:44 -0500 Subject: [PATCH 6/6] v410dec: Check for sufficient input data. Fixes crash Signed-off-by: Michael Niedermayer Signed-off-by: Ronald S. Bultje --- libavcodec/v410dec.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libavcodec/v410dec.c b/libavcodec/v410dec.c index 71cbe0b355..54c9595826 100644 --- a/libavcodec/v410dec.c +++ b/libavcodec/v410dec.c @@ -55,6 +55,11 @@ static int v410_decode_frame(AVCodecContext *avctx, void *data, if (pic->data[0]) avctx->release_buffer(avctx, pic); + if (avpkt->size < 4 * avctx->height * avctx->width) { + av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n"); + return AVERROR(EINVAL); + } + pic->reference = 0; if (avctx->get_buffer(avctx, pic) < 0) {