diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2012-03-18 23:12:35 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2012-03-18 23:39:42 +0100 |
commit | bae053fca4cf662a223821f1e1fe43236e1cf2ff (patch) | |
tree | 0a3c448cd41c5beaaae48a6a61430c9d6277bb7f /libavcodec | |
parent | 7f4c5ab8a1cc5e7be8ce70edfcaa6f926791376b (diff) | |
parent | a56fba502e9087c204b7d6cdc8e12d623f77d66d (diff) | |
download | ffmpeg-bae053fca4cf662a223821f1e1fe43236e1cf2ff.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master:
fate: make compare() function compatible with POSIX bc
Update Janne's email address.
APIchanges: Replace Subversion revision numbers by Git hashes.
bytestream: Eliminate one level of pointless macro indirection.
xwd: convert to bytestream2.
vqavideo: port to bytestream2 API
Read preset files with suffix .avpreset
prores: allow user to set fixed quantiser
lavf: remove some disabled code.
lavf: only set average frame rate for video.
lavf: remove a pointless check.
avcodec: add XBM encoder
Conflicts:
Changelog
cmdutils.c
cmdutils.h
doc/APIchanges
libavcodec/Makefile
libavcodec/avcodec.h
libavcodec/version.h
libavcodec/vqavideo.c
libavformat/img2enc.c
libavformat/utils.c
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/Makefile | 1 | ||||
-rw-r--r-- | libavcodec/aacdec.c | 2 | ||||
-rw-r--r-- | libavcodec/allcodecs.c | 1 | ||||
-rw-r--r-- | libavcodec/avcodec.h | 1 | ||||
-rw-r--r-- | libavcodec/bytestream.h | 30 | ||||
-rw-r--r-- | libavcodec/proresenc_kostya.c | 114 | ||||
-rw-r--r-- | libavcodec/version.h | 2 | ||||
-rw-r--r-- | libavcodec/vqavideo.c | 201 | ||||
-rw-r--r-- | libavcodec/xbmenc.c | 86 | ||||
-rw-r--r-- | libavcodec/xwddec.c | 66 |
10 files changed, 305 insertions, 199 deletions
diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 69ba63228a..08a5728185 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -482,6 +482,7 @@ OBJS-$(CONFIG_XAN_DPCM_DECODER) += dpcm.o OBJS-$(CONFIG_XAN_WC3_DECODER) += xan.o OBJS-$(CONFIG_XAN_WC4_DECODER) += xxan.o OBJS-$(CONFIG_XBIN_DECODER) += bintext.o cga_data.o +OBJS-$(CONFIG_XBM_ENCODER) += xbmenc.o OBJS-$(CONFIG_XL_DECODER) += xl.o OBJS-$(CONFIG_XSUB_DECODER) += xsubdec.o OBJS-$(CONFIG_XSUB_ENCODER) += xsubenc.o diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index d220f14933..d91ee917d9 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -5,7 +5,7 @@ * * AAC LATM decoder * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz> - * Copyright (c) 2010 Janne Grunau <janne-ffmpeg@jannau.net> + * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net> * * This file is part of FFmpeg. * diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 52a67f34bd..26ea1dc791 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -249,6 +249,7 @@ void avcodec_register_all(void) REGISTER_DECODER (WNV1, wnv1); REGISTER_DECODER (XAN_WC3, xan_wc3); REGISTER_DECODER (XAN_WC4, xan_wc4); + REGISTER_ENCODER (XBM, xbm); REGISTER_DECODER (XL, xl); REGISTER_ENCDEC (XWD, xwd); REGISTER_ENCDEC (Y41P, y41p); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index a3d5614fc3..235ebf2ec6 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -247,6 +247,7 @@ enum CodecID { CODEC_ID_V410, CODEC_ID_XWD, CODEC_ID_CDXL, + CODEC_ID_XBM, CODEC_ID_Y41P = MKBETAG('Y','4','1','P'), CODEC_ID_ESCAPE130 = MKBETAG('E','1','3','0'), CODEC_ID_AVRP = MKBETAG('A','V','R','P'), diff --git a/libavcodec/bytestream.h b/libavcodec/bytestream.h index 2d9ca47508..07463f1e3d 100644 --- a/libavcodec/bytestream.h +++ b/libavcodec/bytestream.h @@ -23,6 +23,7 @@ #ifndef AVCODEC_BYTESTREAM_H #define AVCODEC_BYTESTREAM_H +#include <stdint.h> #include <string.h> #include "libavutil/common.h" @@ -37,7 +38,7 @@ typedef struct { int eof; } PutByteContext; -#define DEF_T(type, name, bytes, read, write) \ +#define DEF(type, name, bytes, read, write) \ static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \ { \ (*b) += bytes; \ @@ -80,24 +81,15 @@ static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \ return read(g->buffer); \ } -#define DEF(name, bytes, read, write) \ - DEF_T(unsigned int, name, bytes, read, write) -#define DEF64(name, bytes, read, write) \ - DEF_T(uint64_t, name, bytes, read, write) - -DEF64(le64, 8, AV_RL64, AV_WL64) -DEF (le32, 4, AV_RL32, AV_WL32) -DEF (le24, 3, AV_RL24, AV_WL24) -DEF (le16, 2, AV_RL16, AV_WL16) -DEF64(be64, 8, AV_RB64, AV_WB64) -DEF (be32, 4, AV_RB32, AV_WB32) -DEF (be24, 3, AV_RB24, AV_WB24) -DEF (be16, 2, AV_RB16, AV_WB16) -DEF (byte, 1, AV_RB8 , AV_WB8 ) - -#undef DEF -#undef DEF64 -#undef DEF_T +DEF(uint64_t, le64, 8, AV_RL64, AV_WL64) +DEF(unsigned int, le32, 4, AV_RL32, AV_WL32) +DEF(unsigned int, le24, 3, AV_RL24, AV_WL24) +DEF(unsigned int, le16, 2, AV_RL16, AV_WL16) +DEF(uint64_t, be64, 8, AV_RB64, AV_WB64) +DEF(unsigned int, be32, 4, AV_RB32, AV_WB32) +DEF(unsigned int, be24, 3, AV_RB24, AV_WB24) +DEF(unsigned int, be16, 2, AV_RB16, AV_WB16) +DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8) #if HAVE_BIGENDIAN # define bytestream2_get_ne16 bytestream2_get_be16 diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c index 16e64d1d08..d46401b754 100644 --- a/libavcodec/proresenc_kostya.c +++ b/libavcodec/proresenc_kostya.c @@ -184,6 +184,7 @@ typedef struct ProresContext { int num_slices; int num_planes; int bits_per_mb; + int force_quant; char *vendor; int quant_sel; @@ -397,7 +398,9 @@ static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, int plane_factor, is_chroma; uint16_t *qmat; - if (quant < MAX_STORED_Q) { + if (ctx->force_quant) { + qmat = ctx->quants[0]; + } else if (quant < MAX_STORED_Q) { qmat = ctx->quants[quant]; } else { qmat = ctx->custom_q; @@ -750,21 +753,23 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, // slices for (y = 0; y < ctx->mb_height; y++) { mbs_per_slice = ctx->mbs_per_slice; - for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) { - while (ctx->mb_width - x < mbs_per_slice) - mbs_per_slice >>= 1; - q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y, - mbs_per_slice); - } + if (!ctx->force_quant) { + for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) { + while (ctx->mb_width - x < mbs_per_slice) + mbs_per_slice >>= 1; + q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y, + mbs_per_slice); + } - for (x = ctx->slices_width - 1; x >= 0; x--) { - ctx->slice_q[x] = ctx->nodes[q].quant; - q = ctx->nodes[q].prev_node; + for (x = ctx->slices_width - 1; x >= 0; x--) { + ctx->slice_q[x] = ctx->nodes[q].quant; + q = ctx->nodes[q].prev_node; + } } mbs_per_slice = ctx->mbs_per_slice; for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) { - q = ctx->slice_q[mb]; + q = ctx->force_quant ? ctx->force_quant : ctx->slice_q[mb]; while (ctx->mb_width - x < mbs_per_slice) mbs_per_slice >>= 1; @@ -859,27 +864,66 @@ static av_cold int encode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - if (!ctx->bits_per_mb) { - for (i = 0; i < NUM_MB_LIMITS - 1; i++) - if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height) - break; - ctx->bits_per_mb = ctx->profile_info->br_tab[i]; - } else if (ctx->bits_per_mb < 128) { - av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n"); - return AVERROR_INVALIDDATA; + ctx->force_quant = avctx->global_quality / FF_QP2LAMBDA; + if (!ctx->force_quant) { + if (!ctx->bits_per_mb) { + for (i = 0; i < NUM_MB_LIMITS - 1; i++) + if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height) + break; + ctx->bits_per_mb = ctx->profile_info->br_tab[i]; + } else if (ctx->bits_per_mb < 128) { + av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n"); + return AVERROR_INVALIDDATA; + } + + min_quant = ctx->profile_info->min_quant; + max_quant = ctx->profile_info->max_quant; + for (i = min_quant; i < MAX_STORED_Q; i++) { + for (j = 0; j < 64; j++) + ctx->quants[i][j] = ctx->quant_mat[j] * i; + } + + ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH + * sizeof(*ctx->nodes)); + if (!ctx->nodes) { + encode_close(avctx); + return AVERROR(ENOMEM); + } + for (i = min_quant; i < max_quant + 2; i++) { + ctx->nodes[i].prev_node = -1; + ctx->nodes[i].bits = 0; + ctx->nodes[i].score = 0; + } + + ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q)); + if (!ctx->slice_q) { + encode_close(avctx); + return AVERROR(ENOMEM); + } + } else { + int ls = 0; + + if (ctx->force_quant > 64) { + av_log(avctx, AV_LOG_ERROR, "too large quantiser, maximum is 64\n"); + return AVERROR_INVALIDDATA; + } + + for (j = 0; j < 64; j++) { + ctx->quants[0][j] = ctx->quant_mat[j] * ctx->force_quant; + ls += av_log2((1 << 11) / ctx->quants[0][j]) * 2 + 1; + } + + ctx->bits_per_mb = ls * 8; + if (ctx->chroma_factor == CFACTOR_Y444) + ctx->bits_per_mb += ls * 4; + if (ctx->num_planes == 4) + ctx->bits_per_mb += ls * 4; } ctx->frame_size = ctx->num_slices * (2 + 2 * ctx->num_planes + (2 * mps * ctx->bits_per_mb) / 8) + 200; - min_quant = ctx->profile_info->min_quant; - max_quant = ctx->profile_info->max_quant; - for (i = min_quant; i < MAX_STORED_Q; i++) { - for (j = 0; j < 64; j++) - ctx->quants[i][j] = ctx->quant_mat[j] * i; - } - avctx->codec_tag = ctx->profile_info->tag; av_log(avctx, AV_LOG_DEBUG, "profile %d, %d slices, %d bits per MB\n", @@ -887,24 +931,6 @@ static av_cold int encode_init(AVCodecContext *avctx) av_log(avctx, AV_LOG_DEBUG, "estimated frame size %d\n", ctx->frame_size); - ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH - * sizeof(*ctx->nodes)); - if (!ctx->nodes) { - encode_close(avctx); - return AVERROR(ENOMEM); - } - for (i = min_quant; i < max_quant + 2; i++) { - ctx->nodes[i].prev_node = -1; - ctx->nodes[i].bits = 0; - ctx->nodes[i].score = 0; - } - - ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q)); - if (!ctx->slice_q) { - encode_close(avctx); - return AVERROR(ENOMEM); - } - return 0; } diff --git a/libavcodec/version.h b/libavcodec/version.h index 95fbd00450..c91ac2298b 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -21,7 +21,7 @@ #define AVCODEC_VERSION_H #define LIBAVCODEC_VERSION_MAJOR 54 -#define LIBAVCODEC_VERSION_MINOR 10 +#define LIBAVCODEC_VERSION_MINOR 11 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavcodec/vqavideo.c b/libavcodec/vqavideo.c index ba70a11c59..6579b8f004 100644 --- a/libavcodec/vqavideo.c +++ b/libavcodec/vqavideo.c @@ -70,10 +70,10 @@ #include "libavutil/intreadwrite.h" #include "libavutil/imgutils.h" #include "avcodec.h" +#include "bytestream.h" #define PALETTE_COUNT 256 #define VQA_HEADER_SIZE 0x2A -#define CHUNK_PREAMBLE_SIZE 8 /* allocate the maximum vector space, regardless of the file version: * (0xFF00 codebook vectors + 0x100 solid pixel vectors) * (4x4 pixels/block) */ @@ -94,9 +94,7 @@ typedef struct VqaContext { AVCodecContext *avctx; AVFrame frame; - - const unsigned char *buf; - int size; + GetByteContext gb; uint32_t palette[PALETTE_COUNT]; @@ -123,7 +121,6 @@ typedef struct VqaContext { static av_cold int vqa_decode_init(AVCodecContext *avctx) { VqaContext *s = avctx->priv_data; - unsigned char *vqa_header; int i, j, codebook_index; s->avctx = avctx; @@ -136,21 +133,20 @@ static av_cold int vqa_decode_init(AVCodecContext *avctx) } /* load up the VQA parameters from the header */ - vqa_header = (unsigned char *)s->avctx->extradata; - s->vqa_version = vqa_header[0]; + s->vqa_version = s->avctx->extradata[0]; if (s->vqa_version < 1 || s->vqa_version > 3) { av_log(s->avctx, AV_LOG_ERROR, " VQA video: unsupported version %d\n", s->vqa_version); return -1; } - s->width = AV_RL16(&vqa_header[6]); - s->height = AV_RL16(&vqa_header[8]); + s->width = AV_RL16(&s->avctx->extradata[6]); + s->height = AV_RL16(&s->avctx->extradata[8]); if(av_image_check_size(s->width, s->height, 0, avctx)){ s->width= s->height= 0; return -1; } - s->vector_width = vqa_header[10]; - s->vector_height = vqa_header[11]; - s->partial_count = s->partial_countdown = vqa_header[13]; + s->vector_width = s->avctx->extradata[10]; + s->vector_height = s->avctx->extradata[11]; + s->partial_count = s->partial_countdown = s->avctx->extradata[13]; /* the vector dimensions have to meet very stringent requirements */ if ((s->vector_width != 4) || @@ -205,90 +201,88 @@ fail: av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \ av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \ dest_index, count, dest_size); \ - return; \ + return AVERROR_INVALIDDATA; \ + } + +#define CHECK_COPY(idx) \ + if (idx < 0 || idx + count > dest_size) { \ + av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \ + av_log(NULL, AV_LOG_ERROR, " VQA video: current src_pos = %d, count = %d, dest_size = %d\n", \ + src_pos, count, dest_size); \ + return AVERROR_INVALIDDATA; \ } -static void decode_format80(const unsigned char *src, int src_size, + +static int decode_format80(GetByteContext *gb, int src_size, unsigned char *dest, int dest_size, int check_size) { - int src_index = 0; int dest_index = 0; - int count; + int count, opcode, start; int src_pos; unsigned char color; int i; - while (src_index < src_size) { - - av_dlog(NULL, " opcode %02X: ", src[src_index]); + start = bytestream2_tell(gb); + while (bytestream2_tell(gb) - start < src_size) { + opcode = bytestream2_get_byte(gb); + av_dlog(NULL, " opcode %02X: ", opcode); /* 0x80 means that frame is finished */ - if (src[src_index] == 0x80) - return; + if (opcode == 0x80) + return 0; if (dest_index >= dest_size) { av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n", dest_index, dest_size); - return; + return AVERROR_INVALIDDATA; } - if (src[src_index] == 0xFF) { + if (opcode == 0xFF) { - src_index++; - count = AV_RL16(&src[src_index]); - src_index += 2; - src_pos = AV_RL16(&src[src_index]); - src_index += 2; + count = bytestream2_get_le16(gb); + src_pos = bytestream2_get_le16(gb); av_dlog(NULL, "(1) copy %X bytes from absolute pos %X\n", count, src_pos); CHECK_COUNT(); - if (src_pos + count > dest_size) - return; + CHECK_COPY(src_pos); for (i = 0; i < count; i++) dest[dest_index + i] = dest[src_pos + i]; dest_index += count; - } else if (src[src_index] == 0xFE) { + } else if (opcode == 0xFE) { - src_index++; - count = AV_RL16(&src[src_index]); - src_index += 2; - color = src[src_index++]; + count = bytestream2_get_le16(gb); + color = bytestream2_get_byte(gb); av_dlog(NULL, "(2) set %X bytes to %02X\n", count, color); CHECK_COUNT(); memset(&dest[dest_index], color, count); dest_index += count; - } else if ((src[src_index] & 0xC0) == 0xC0) { + } else if ((opcode & 0xC0) == 0xC0) { - count = (src[src_index++] & 0x3F) + 3; - src_pos = AV_RL16(&src[src_index]); - src_index += 2; + count = (opcode & 0x3F) + 3; + src_pos = bytestream2_get_le16(gb); av_dlog(NULL, "(3) copy %X bytes from absolute pos %X\n", count, src_pos); CHECK_COUNT(); - if (src_pos + count > dest_size) - return; + CHECK_COPY(src_pos); for (i = 0; i < count; i++) dest[dest_index + i] = dest[src_pos + i]; dest_index += count; - } else if (src[src_index] > 0x80) { + } else if (opcode > 0x80) { - count = src[src_index++] & 0x3F; + count = opcode & 0x3F; av_dlog(NULL, "(4) copy %X bytes from source to dest\n", count); CHECK_COUNT(); - memcpy(&dest[dest_index], &src[src_index], count); - src_index += count; + bytestream2_get_buffer(gb, &dest[dest_index], count); dest_index += count; } else { - count = ((src[src_index] & 0x70) >> 4) + 3; - src_pos = AV_RB16(&src[src_index]) & 0x0FFF; - src_index += 2; + count = ((opcode & 0x70) >> 4) + 3; + src_pos = bytestream2_get_byte(gb) | ((opcode & 0x0F) << 8); av_dlog(NULL, "(5) copy %X bytes from relpos %X\n", count, src_pos); CHECK_COUNT(); - if (dest_index < src_pos) - return; + CHECK_COPY(dest_index - src_pos); for (i = 0; i < count; i++) dest[dest_index + i] = dest[dest_index - src_pos + i]; dest_index += count; @@ -303,9 +297,11 @@ static void decode_format80(const unsigned char *src, int src_size, if (dest_index < dest_size) av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n", dest_index, dest_size); + + return 0; // let's display what we decoded anyway } -static void vqa_decode_chunk(VqaContext *s) +static int vqa_decode_chunk(VqaContext *s) { unsigned int chunk_type; unsigned int chunk_size; @@ -314,6 +310,7 @@ static void vqa_decode_chunk(VqaContext *s) int i; unsigned char r, g, b; int index_shift; + int res; int cbf0_chunk = -1; int cbfz_chunk = -1; @@ -333,17 +330,11 @@ static void vqa_decode_chunk(VqaContext *s) int hibytes = s->decode_buffer_size / 2; /* first, traverse through the frame and find the subchunks */ - while (index + CHUNK_PREAMBLE_SIZE <= s->size) { - unsigned next_index; + while (bytestream2_get_bytes_left(&s->gb) >= 8) { - chunk_type = AV_RB32(&s->buf[index]); - chunk_size = AV_RB32(&s->buf[index + 4]); - byte_skip = chunk_size & 0x01; - next_index = index + CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip; - if (next_index > s->size) { - av_log(s->avctx, AV_LOG_ERROR, "Dropping incomplete chunk\n"); - break; - } + chunk_type = bytestream2_get_be32u(&s->gb); + index = bytestream2_tell(&s->gb); + chunk_size = bytestream2_get_be32u(&s->gb); switch (chunk_type) { @@ -384,7 +375,9 @@ static void vqa_decode_chunk(VqaContext *s) chunk_type); break; } - index = next_index; + + byte_skip = chunk_size & 0x01; + bytestream2_skip(&s->gb, chunk_size + byte_skip); } /* next, deal with the palette */ @@ -392,7 +385,7 @@ static void vqa_decode_chunk(VqaContext *s) /* a chunk should not have both chunk types */ av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n"); - return; + return AVERROR_INVALIDDATA; } /* decompress the palette chunk */ @@ -405,19 +398,19 @@ static void vqa_decode_chunk(VqaContext *s) /* convert the RGB palette into the machine's endian format */ if (cpl0_chunk != -1) { - chunk_size = AV_RB32(&s->buf[cpl0_chunk + 4]); + bytestream2_seek(&s->gb, cpl0_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); /* sanity check the palette size */ if (chunk_size / 3 > 256) { av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n", chunk_size / 3); - return; + return AVERROR_INVALIDDATA; } - cpl0_chunk += CHUNK_PREAMBLE_SIZE; for (i = 0; i < chunk_size / 3; i++) { /* scale by 4 to transform 6-bit palette -> 8-bit */ - r = s->buf[cpl0_chunk++] * 4; - g = s->buf[cpl0_chunk++] * 4; - b = s->buf[cpl0_chunk++] * 4; + r = bytestream2_get_byteu(&s->gb) * 4; + g = bytestream2_get_byteu(&s->gb) * 4; + b = bytestream2_get_byteu(&s->gb) * 4; s->palette[i] = 0xFF << 24 | r << 16 | g << 8 | b; s->palette[i] |= s->palette[i] >> 6 & 0x30303; } @@ -428,31 +421,32 @@ static void vqa_decode_chunk(VqaContext *s) /* a chunk should not have both chunk types */ av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n"); - return; + return AVERROR_INVALIDDATA; } /* decompress the full codebook chunk */ if (cbfz_chunk != -1) { - chunk_size = AV_RB32(&s->buf[cbfz_chunk + 4]); - cbfz_chunk += CHUNK_PREAMBLE_SIZE; - decode_format80(&s->buf[cbfz_chunk], chunk_size, - s->codebook, s->codebook_size, 0); + bytestream2_seek(&s->gb, cbfz_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); + if ((res = decode_format80(&s->gb, chunk_size, s->codebook, + s->codebook_size, 0)) < 0) + return res; } /* copy a full codebook */ if (cbf0_chunk != -1) { - chunk_size = AV_RB32(&s->buf[cbf0_chunk + 4]); + bytestream2_seek(&s->gb, cbf0_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); /* sanity check the full codebook size */ if (chunk_size > MAX_CODEBOOK_SIZE) { av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n", chunk_size); - return; + return AVERROR_INVALIDDATA; } - cbf0_chunk += CHUNK_PREAMBLE_SIZE; - memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size); + bytestream2_get_buffer(&s->gb, s->codebook, chunk_size); } /* decode the frame */ @@ -460,13 +454,14 @@ static void vqa_decode_chunk(VqaContext *s) /* something is wrong if there is no VPTZ chunk */ av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n"); - return; + return AVERROR_INVALIDDATA; } - chunk_size = AV_RB32(&s->buf[vptz_chunk + 4]); - vptz_chunk += CHUNK_PREAMBLE_SIZE; - decode_format80(&s->buf[vptz_chunk], chunk_size, - s->decode_buffer, s->decode_buffer_size, 1); + bytestream2_seek(&s->gb, vptz_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); + if ((res = decode_format80(&s->gb, chunk_size, + s->decode_buffer, s->decode_buffer_size, 1)) < 0) + return res; /* render the final PAL8 frame */ if (s->vector_height == 4) @@ -530,17 +525,17 @@ static void vqa_decode_chunk(VqaContext *s) if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) { /* a chunk should not have both chunk types */ av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n"); - return; + return AVERROR_INVALIDDATA; } if (cbp0_chunk != -1) { - chunk_size = AV_RB32(&s->buf[cbp0_chunk + 4]); - cbp0_chunk += CHUNK_PREAMBLE_SIZE; + bytestream2_seek(&s->gb, cbp0_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); /* accumulate partial codebook */ - memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index], - &s->buf[cbp0_chunk], chunk_size); + bytestream2_get_buffer(&s->gb, &s->next_codebook_buffer[s->next_codebook_buffer_index], + chunk_size); s->next_codebook_buffer_index += chunk_size; s->partial_countdown--; @@ -558,39 +553,39 @@ static void vqa_decode_chunk(VqaContext *s) if (cbpz_chunk != -1) { - chunk_size = AV_RB32(&s->buf[cbpz_chunk + 4]); - cbpz_chunk += CHUNK_PREAMBLE_SIZE; + bytestream2_seek(&s->gb, cbpz_chunk, SEEK_SET); + chunk_size = bytestream2_get_be32(&s->gb); /* accumulate partial codebook */ - memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index], - &s->buf[cbpz_chunk], chunk_size); + bytestream2_get_buffer(&s->gb, &s->next_codebook_buffer[s->next_codebook_buffer_index], + chunk_size); s->next_codebook_buffer_index += chunk_size; s->partial_countdown--; if (s->partial_countdown == 0) { + GetByteContext gb; + bytestream2_init(&gb, s->next_codebook_buffer, s->next_codebook_buffer_index); /* decompress codebook */ - decode_format80(s->next_codebook_buffer, - s->next_codebook_buffer_index, - s->codebook, s->codebook_size, 0); + if ((res = decode_format80(&gb, s->next_codebook_buffer_index, + s->codebook, s->codebook_size, 0)) < 0) + return res; /* reset accounting */ s->next_codebook_buffer_index = 0; s->partial_countdown = s->partial_count; } } + + return 0; } static int vqa_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { - const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; VqaContext *s = avctx->priv_data; - - s->buf = buf; - s->size = buf_size; + int res; if (s->frame.data[0]) avctx->release_buffer(avctx, &s->frame); @@ -600,7 +595,9 @@ static int vqa_decode_frame(AVCodecContext *avctx, return -1; } - vqa_decode_chunk(s); + bytestream2_init(&s->gb, avpkt->data, avpkt->size); + if ((res = vqa_decode_chunk(s)) < 0) + return res; /* make the palette available on the way out */ memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4); @@ -610,7 +607,7 @@ static int vqa_decode_frame(AVCodecContext *avctx, *(AVFrame*)data = s->frame; /* report that the buffer was completely consumed */ - return buf_size; + return avpkt->size; } static av_cold int vqa_decode_end(AVCodecContext *avctx) diff --git a/libavcodec/xbmenc.c b/libavcodec/xbmenc.c new file mode 100644 index 0000000000..2df6ae0d77 --- /dev/null +++ b/libavcodec/xbmenc.c @@ -0,0 +1,86 @@ +/* + * XBM image format + * + * Copyright (c) 2012 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 + */ + +#include "avcodec.h" +#include "internal.h" + +static av_cold int xbm_encode_init(AVCodecContext *avctx) +{ + avctx->coded_frame = avcodec_alloc_frame(); + if (!avctx->coded_frame) + return AVERROR(ENOMEM); + avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; + + return 0; +} + +static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *p, int *got_packet) +{ + int i, j, ret, size, linesize; + uint8_t *ptr, *buf; + + linesize = (avctx->width + 7) / 8; + size = avctx->height * (linesize * 7 + 2) + 110; + if ((ret = ff_alloc_packet(pkt, size)) < 0) { + av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n"); + return ret; + } + + buf = pkt->data; + ptr = p->data[0]; + + buf += snprintf(buf, 32, "#define image_width %u\n", avctx->width); + buf += snprintf(buf, 33, "#define image_height %u\n", avctx->height); + buf += snprintf(buf, 40, "static unsigned char image_bits[] = {\n"); + for (i = 0; i < avctx->height; i++) { + for (j = 0; j < linesize; j++) + buf += snprintf(buf, 7, " 0x%02X,", av_reverse[*ptr++]); + ptr += p->linesize[0] - linesize; + buf += snprintf(buf, 2, "\n"); + } + buf += snprintf(buf, 5, " };\n"); + + pkt->size = buf - pkt->data; + pkt->flags |= AV_PKT_FLAG_KEY; + *got_packet = 1; + return 0; +} + +static av_cold int xbm_encode_close(AVCodecContext *avctx) +{ + av_freep(&avctx->coded_frame); + + return 0; +} + +AVCodec ff_xbm_encoder = { + .name = "xbm", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_XBM, + .init = xbm_encode_init, + .encode2 = xbm_encode_frame, + .close = xbm_encode_close, + .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_MONOWHITE, + PIX_FMT_NONE }, + .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"), +}; diff --git a/libavcodec/xwddec.c b/libavcodec/xwddec.c index 97f3a6a979..2879358aa4 100644 --- a/libavcodec/xwddec.c +++ b/libavcodec/xwddec.c @@ -45,43 +45,43 @@ static int xwd_decode_frame(AVCodecContext *avctx, void *data, uint32_t pixformat, pixdepth, bunit, bitorder, bpad; uint32_t rgb[3]; uint8_t *ptr; + GetByteContext gb; if (buf_size < XWD_HEADER_SIZE) return AVERROR_INVALIDDATA; - header_size = bytestream_get_be32(&buf); - if (buf_size < header_size) - return AVERROR_INVALIDDATA; + bytestream2_init(&gb, buf, buf_size); + header_size = bytestream2_get_be32u(&gb); - version = bytestream_get_be32(&buf); + version = bytestream2_get_be32u(&gb); if (version != XWD_VERSION) { av_log(avctx, AV_LOG_ERROR, "unsupported version\n"); return AVERROR_INVALIDDATA; } - if (header_size < XWD_HEADER_SIZE) { + if (buf_size < header_size || header_size < XWD_HEADER_SIZE) { av_log(avctx, AV_LOG_ERROR, "invalid header size\n"); return AVERROR_INVALIDDATA; } - pixformat = bytestream_get_be32(&buf); - pixdepth = bytestream_get_be32(&buf); - avctx->width = bytestream_get_be32(&buf); - avctx->height = bytestream_get_be32(&buf); - xoffset = bytestream_get_be32(&buf); - be = bytestream_get_be32(&buf); - bunit = bytestream_get_be32(&buf); - bitorder = bytestream_get_be32(&buf); - bpad = bytestream_get_be32(&buf); - bpp = bytestream_get_be32(&buf); - lsize = bytestream_get_be32(&buf); - vclass = bytestream_get_be32(&buf); - rgb[0] = bytestream_get_be32(&buf); - rgb[1] = bytestream_get_be32(&buf); - rgb[2] = bytestream_get_be32(&buf); - buf += 8; - ncolors = bytestream_get_be32(&buf); - buf += header_size - (XWD_HEADER_SIZE - 20); + pixformat = bytestream2_get_be32u(&gb); + pixdepth = bytestream2_get_be32u(&gb); + avctx->width = bytestream2_get_be32u(&gb); + avctx->height = bytestream2_get_be32u(&gb); + xoffset = bytestream2_get_be32u(&gb); + be = bytestream2_get_be32u(&gb); + bunit = bytestream2_get_be32u(&gb); + bitorder = bytestream2_get_be32u(&gb); + bpad = bytestream2_get_be32u(&gb); + bpp = bytestream2_get_be32u(&gb); + lsize = bytestream2_get_be32u(&gb); + vclass = bytestream2_get_be32u(&gb); + rgb[0] = bytestream2_get_be32u(&gb); + rgb[1] = bytestream2_get_be32u(&gb); + rgb[2] = bytestream2_get_be32u(&gb); + bytestream2_skipu(&gb, 8); + ncolors = bytestream2_get_be32u(&gb); + bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20)); av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n", pixformat, pixdepth, bunit, bitorder, bpad); @@ -143,7 +143,7 @@ static int xwd_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } - if (buf_size < header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize) { + if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + avctx->height * lsize) { av_log(avctx, AV_LOG_ERROR, "input buffer too small\n"); return AVERROR_INVALIDDATA; } @@ -192,7 +192,7 @@ static int xwd_decode_frame(AVCodecContext *avctx, void *data, else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000) avctx->pix_fmt = be ? PIX_FMT_ABGR : PIX_FMT_RGBA; } - buf += ncolors * XWD_CMAP_SIZE; + bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE); break; default: av_log(avctx, AV_LOG_ERROR, "invalid visual class\n"); @@ -222,11 +222,13 @@ static int xwd_decode_frame(AVCodecContext *avctx, void *data, for (i = 0; i < ncolors; i++) { - buf += 4; // skip colormap entry number - red = *buf; buf += 2; - green = *buf; buf += 2; - blue = *buf; buf += 2; - buf += 2; // skip bitmask flag and padding + bytestream2_skipu(&gb, 4); // skip colormap entry number + red = bytestream2_get_byteu(&gb); + bytestream2_skipu(&gb, 1); + green = bytestream2_get_byteu(&gb); + bytestream2_skipu(&gb, 1); + blue = bytestream2_get_byteu(&gb); + bytestream2_skipu(&gb, 3); // skip bitmask flag and padding dst[i] = red << 16 | green << 8 | blue; } @@ -234,8 +236,8 @@ static int xwd_decode_frame(AVCodecContext *avctx, void *data, ptr = p->data[0]; for (i = 0; i < avctx->height; i++) { - bytestream_get_buffer(&buf, ptr, rsize); - buf += lsize - rsize; + bytestream2_get_bufferu(&gb, ptr, rsize); + bytestream2_skipu(&gb, lsize - rsize); ptr += p->linesize[0]; } |