From b8f02f5b4eeae4bd332e1c0eee75807ba6f5b431 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 6 Nov 2011 15:27:55 -0500 Subject: [PATCH 01/12] dsputil: use cpuflags in x86 versions of vector_clip_int32() --- libavcodec/x86/dsputil_mmx.c | 8 +++---- libavcodec/x86/dsputil_yasm.asm | 40 +++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/libavcodec/x86/dsputil_mmx.c b/libavcodec/x86/dsputil_mmx.c index 959a2c2e7e..dd6cbf51ad 100644 --- a/libavcodec/x86/dsputil_mmx.c +++ b/libavcodec/x86/dsputil_mmx.c @@ -2419,9 +2419,9 @@ void ff_vector_clip_int32_mmx (int32_t *dst, const int32_t *src, int32_t min int32_t max, unsigned int len); void ff_vector_clip_int32_sse2 (int32_t *dst, const int32_t *src, int32_t min, int32_t max, unsigned int len); -void ff_vector_clip_int32_sse2_int(int32_t *dst, const int32_t *src, int32_t min, +void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src, int32_t min, int32_t max, unsigned int len); -void ff_vector_clip_int32_sse41 (int32_t *dst, const int32_t *src, int32_t min, +void ff_vector_clip_int32_sse4 (int32_t *dst, const int32_t *src, int32_t min, int32_t max, unsigned int len); void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) @@ -2877,7 +2877,7 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) c->scalarproduct_int16 = ff_scalarproduct_int16_sse2; c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2; if (mm_flags & AV_CPU_FLAG_ATOM) { - c->vector_clip_int32 = ff_vector_clip_int32_sse2_int; + c->vector_clip_int32 = ff_vector_clip_int32_int_sse2; } else { c->vector_clip_int32 = ff_vector_clip_int32_sse2; } @@ -2909,7 +2909,7 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) if (mm_flags & AV_CPU_FLAG_SSE4 && HAVE_SSE) { #if HAVE_YASM - c->vector_clip_int32 = ff_vector_clip_int32_sse41; + c->vector_clip_int32 = ff_vector_clip_int32_sse4; #endif } diff --git a/libavcodec/x86/dsputil_yasm.asm b/libavcodec/x86/dsputil_yasm.asm index fe96d8b12b..8e3cbdc03d 100644 --- a/libavcodec/x86/dsputil_yasm.asm +++ b/libavcodec/x86/dsputil_yasm.asm @@ -1055,9 +1055,14 @@ emu_edge mmx ; int32_t max, unsigned int len) ;----------------------------------------------------------------------------- -%macro VECTOR_CLIP_INT32 4 -cglobal vector_clip_int32_%1, 5,5,%2, dst, src, min, max, len -%ifidn %1, sse2 +; %1 = number of xmm registers used +; %2 = number of inline load/process/store loops per asm loop +; %3 = process 4*mmsize (%3=0) or 8*mmsize (%3=1) bytes per loop +; %4 = CLIPD function takes min/max as float instead of int (CLIPD_SSE2) +; %5 = suffix +%macro VECTOR_CLIP_INT32 4-5 +cglobal vector_clip_int32%5, 5,5,%2, dst, src, min, max, len +%if %4 cvtsi2ss m4, minm cvtsi2ss m5, maxm %else @@ -1068,12 +1073,12 @@ cglobal vector_clip_int32_%1, 5,5,%2, dst, src, min, max, len SPLATD m5 .loop: %assign %%i 1 -%rep %3 +%rep %2 mova m0, [srcq+mmsize*0*%%i] mova m1, [srcq+mmsize*1*%%i] mova m2, [srcq+mmsize*2*%%i] mova m3, [srcq+mmsize*3*%%i] -%if %4 +%if %3 mova m7, [srcq+mmsize*4*%%i] mova m8, [srcq+mmsize*5*%%i] mova m9, [srcq+mmsize*6*%%i] @@ -1083,7 +1088,7 @@ cglobal vector_clip_int32_%1, 5,5,%2, dst, src, min, max, len CLIPD m1, m4, m5, m6 CLIPD m2, m4, m5, m6 CLIPD m3, m4, m5, m6 -%if %4 +%if %3 CLIPD m7, m4, m5, m6 CLIPD m8, m4, m5, m6 CLIPD m9, m4, m5, m6 @@ -1093,7 +1098,7 @@ cglobal vector_clip_int32_%1, 5,5,%2, dst, src, min, max, len mova [dstq+mmsize*1*%%i], m1 mova [dstq+mmsize*2*%%i], m2 mova [dstq+mmsize*3*%%i], m3 -%if %4 +%if %3 mova [dstq+mmsize*4*%%i], m7 mova [dstq+mmsize*5*%%i], m8 mova [dstq+mmsize*6*%%i], m9 @@ -1101,25 +1106,26 @@ cglobal vector_clip_int32_%1, 5,5,%2, dst, src, min, max, len %endif %assign %%i %%i+1 %endrep - add srcq, mmsize*4*(%3+%4) - add dstq, mmsize*4*(%3+%4) - sub lend, mmsize*(%3+%4) + add srcq, mmsize*4*(%2+%3) + add dstq, mmsize*4*(%2+%3) + sub lend, mmsize*(%2+%3) jg .loop REP_RET %endmacro -INIT_MMX +INIT_MMX mmx %define SPLATD SPLATD_MMX %define CLIPD CLIPD_MMX -VECTOR_CLIP_INT32 mmx, 0, 1, 0 -INIT_XMM +VECTOR_CLIP_INT32 0, 1, 0, 0 +INIT_XMM sse2 %define SPLATD SPLATD_SSE2 -VECTOR_CLIP_INT32 sse2_int, 6, 1, 0 +VECTOR_CLIP_INT32 6, 1, 0, 0, _int %define CLIPD CLIPD_SSE2 -VECTOR_CLIP_INT32 sse2, 6, 2, 0 +VECTOR_CLIP_INT32 6, 2, 0, 1 +INIT_XMM sse4 %define CLIPD CLIPD_SSE41 %ifdef m8 -VECTOR_CLIP_INT32 sse41, 11, 1, 1 +VECTOR_CLIP_INT32 11, 1, 1, 0 %else -VECTOR_CLIP_INT32 sse41, 6, 1, 0 +VECTOR_CLIP_INT32 6, 1, 0, 0 %endif From 4b3dc857e416f9ec187cb14fd133226456ba7f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sun, 6 Nov 2011 23:50:39 +0200 Subject: [PATCH 02/12] rtsp: Discard the dynamic handler, if it has an alloc function which failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/rtsp.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index c673c35572..84cf922c26 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -172,8 +172,11 @@ static void init_rtp_handler(RTPDynamicProtocolHandler *handler, return; codec->codec_id = handler->codec_id; rtsp_st->dynamic_handler = handler; - if (handler->alloc) + if (handler->alloc) { rtsp_st->dynamic_protocol_context = handler->alloc(); + if (!rtsp_st->dynamic_protocol_context) + rtsp_st->dynamic_handler = NULL; + } } /* parse the rtpmap description: /[/] */ From ed307e2659f2db81a434afece905383fdceb9b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sun, 6 Nov 2011 23:51:19 +0200 Subject: [PATCH 03/12] rdt: Check the return value of avformat_open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If it failed, return NULL. This avoids trying to use an half-initialized RDTDemuxContext. Signed-off-by: Martin Storsjö --- libavformat/rdt.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libavformat/rdt.c b/libavformat/rdt.c index 3f161f618c..f492805d13 100644 --- a/libavformat/rdt.c +++ b/libavformat/rdt.c @@ -524,7 +524,11 @@ rdt_new_context (void) { PayloadContext *rdt = av_mallocz(sizeof(PayloadContext)); - avformat_open_input(&rdt->rmctx, "", &ff_rdt_demuxer, NULL); + int ret = avformat_open_input(&rdt->rmctx, "", &ff_rdt_demuxer, NULL); + if (ret < 0) { + av_free(rdt); + return NULL; + } return rdt; } From 088268911621fb27e2ab78280fca253b11efe3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 7 Nov 2011 00:25:03 +0200 Subject: [PATCH 04/12] rdt: Set AVFMT_NOFILE on ff_rdt_demuxer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes rdt work again, which has been broken since 603b8bc2a109978c8499b06d2556f1433306eca7. This commit made opening a demuxer without a file (or in this case, with a filename which can't be opened) fail, unless the demuxer actually declared AVFMT_NOFILE. Signed-off-by: Martin Storsjö --- libavformat/rmdec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c index 7eaecf0469..e3b3e5ac32 100644 --- a/libavformat/rmdec.c +++ b/libavformat/rmdec.c @@ -972,4 +972,5 @@ AVInputFormat ff_rdt_demuxer = { .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"), .priv_data_size = sizeof(RMDemuxContext), .read_close = rm_read_close, + .flags = AVFMT_NOFILE, }; From 87892ef8a6b3052dcdaa6b50e1e1835b7d666b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 7 Nov 2011 14:09:06 +0200 Subject: [PATCH 05/12] avformat: Revise wording MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It might make sense not to make the function completely mandatory immediately at the next bump, which might be quite soon after the function was introduced. Signed-off-by: Martin Storsjö --- libavformat/avformat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 43563031c1..2fa93af31c 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1748,8 +1748,8 @@ const AVClass *avformat_get_class(void); * but recommended, since it avoids the overhead of implicitly * doing the setup for each session. * - * Since the next major version bump, calling this function will become - * mandatory if using network protocols. + * Calling this function will become mandatory if using network + * protocols at some major version bump. */ int avformat_network_init(void); From ce145690b6a53dd9d5134c6d4a5c6d401bc69514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 7 Nov 2011 13:57:33 +0200 Subject: [PATCH 06/12] avformat: Warn about using network functions without calling avformat_network_init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is to make developers aware of the fact that they will start using the new init function at some point. Signed-off-by: Martin Storsjö --- libavformat/network.c | 7 +++++++ libavformat/network.h | 1 + libavformat/utils.c | 1 + 3 files changed, 9 insertions(+) diff --git a/libavformat/network.c b/libavformat/network.c index d84dcaf219..f8403aef83 100644 --- a/libavformat/network.c +++ b/libavformat/network.c @@ -119,8 +119,15 @@ void ff_tls_deinit(void) avpriv_unlock_avformat(); } +int ff_network_inited_globally; + int ff_network_init(void) { + if (!ff_network_inited_globally) + av_log(NULL, AV_LOG_WARNING, "Using network protocols without global " + "network initialization. Please use " + "avformat_network_init(), this will " + "become mandatory later.\n"); #if HAVE_WINSOCK2_H WSADATA wsaData; if (WSAStartup(MAKEWORD(1,1), &wsaData)) diff --git a/libavformat/network.h b/libavformat/network.h index df79f4598f..72d01d2986 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -56,6 +56,7 @@ int ff_neterrno(void); int ff_socket_nonblock(int socket, int enable); +extern int ff_network_inited_globally; int ff_network_init(void); void ff_network_close(void); diff --git a/libavformat/utils.c b/libavformat/utils.c index 8d0ae050d8..cb472c925a 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -3967,6 +3967,7 @@ int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_co int avformat_network_init(void) { #if CONFIG_NETWORK + ff_network_inited_globally = 1; int ret; if ((ret = ff_network_init()) < 0) return ret; From a1684cf82d1aa35de0ae97724477501f92395c2b Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Thu, 3 Nov 2011 15:57:23 -0700 Subject: [PATCH 07/12] msmpeg4: Don't set up run-level info for level 0. run: The number of zero coefficients preceding a non-zero coefficient, in the scan order. The absolute value of the non-zero coefficient is called "level". The run-level code makes illegal reads when trying to set up tables for nonsense level 0. --- libavcodec/msmpeg4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/msmpeg4.c b/libavcodec/msmpeg4.c index 93191ccf10..84e6249858 100644 --- a/libavcodec/msmpeg4.c +++ b/libavcodec/msmpeg4.c @@ -266,7 +266,7 @@ av_cold void ff_msmpeg4_encode_init(MpegEncContext *s) for(i=0; i Date: Thu, 3 Nov 2011 15:56:37 -0700 Subject: [PATCH 08/12] mpeg12enc: Don't set up run-level info for level 0. run: The number of zero coefficients preceding a non-zero coefficient, in the scan order. The absolute value of the non-zero coefficient is called "level". The run-level code makes illegal reads when trying to set up tables for nonsense level 0. --- libavcodec/mpeg12enc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c index 5dedabd268..26d26c7217 100644 --- a/libavcodec/mpeg12enc.c +++ b/libavcodec/mpeg12enc.c @@ -74,6 +74,8 @@ static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){ for(i=0; i<128; i++){ int level= i-64; int run; + if (!level) + continue; for(run=0; run<64; run++){ int len, bits, code; From 090aaaf75280ed42a5e5c7424c366d5b6bab8dde Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Thu, 3 Nov 2011 16:02:17 -0700 Subject: [PATCH 09/12] mpeg12enc: Remove write-only variables. --- libavcodec/mpeg12enc.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c index 26d26c7217..17097db909 100644 --- a/libavcodec/mpeg12enc.c +++ b/libavcodec/mpeg12enc.c @@ -77,10 +77,9 @@ static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){ if (!level) continue; for(run=0; run<64; run++){ - int len, bits, code; + int len, code; int alevel= FFABS(level); - int sign= (level>>31)&1; if (alevel > rl->max_level[0][run]) code= 111; /*rl->n*/ @@ -88,25 +87,15 @@ static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){ code= rl->index_run[0][run] + alevel - 1; if (code < 111 /* rl->n */) { - /* store the vlc & sign at once */ + /* length of vlc and sign */ len= rl->table_vlc[code][1]+1; - bits= (rl->table_vlc[code][0]<<1) + sign; } else { len= rl->table_vlc[111/*rl->n*/][1]+6; - bits= rl->table_vlc[111/*rl->n*/][0]<<6; - bits|= run; if (alevel < 128) { - bits<<=8; len+=8; - bits|= level & 0xff; + len += 8; } else { - bits<<=16; len+=16; - bits|= level & 0xff; - if (level < 0) { - bits|= 0x8001 + level + 255; - } else { - bits|= level & 0xffff; - } + len += 16; } } From 44729bc0204fd8bdc29c234fc663229e44420b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 7 Nov 2011 21:06:01 +0200 Subject: [PATCH 10/12] matroskaenc: Make sure the seekhead struct is freed even on seek failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The caller expects the seekhead struct to be freed when calling matroska_write_seekhead. Currently, the structure is leaked if the seek fails. Signed-off-by: Martin Storsjö --- libavformat/matroskaenc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 5edd2be91c..0667947e09 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -317,9 +317,12 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead) currentpos = avio_tell(pb); - if (seekhead->reserved_size > 0) - if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) - return -1; + if (seekhead->reserved_size > 0) { + if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) { + currentpos = -1; + goto fail; + } + } metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size); for (i = 0; i < seekhead->num_entries; i++) { @@ -343,6 +346,7 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead) currentpos = seekhead->filepos; } +fail: av_free(seekhead->entries); av_free(seekhead); From f545e0067751b764917be53520d233db1bbb945d Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 1 Nov 2011 11:58:44 +0100 Subject: [PATCH 11/12] BMV demuxer and decoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- Changelog | 1 + doc/general.texi | 3 + libavcodec/Makefile | 2 + libavcodec/allcodecs.c | 2 + libavcodec/avcodec.h | 2 + libavcodec/bmv.c | 360 +++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/bmv.c | 136 +++++++++++++++ libavformat/version.h | 2 +- 11 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 libavcodec/bmv.c create mode 100644 libavformat/bmv.c diff --git a/Changelog b/Changelog index 3027c913eb..5387a6522f 100644 --- a/Changelog +++ b/Changelog @@ -99,6 +99,7 @@ easier to use. The changes are: AVCodecContext deprecated. Codec private options should be used instead. - Properly working defaults in libx264 wrapper, support for native presets. - Encrypted OMA files support +- Discworld II BMV decoding support version 0.7: diff --git a/doc/general.texi b/doc/general.texi index 24869c3208..3187f2cc78 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -67,6 +67,7 @@ library: @item Brute Force & Ignorance @tab @tab X @tab Used in the game Flash Traffic: City of Angels. @item BWF @tab X @tab X +@item Discworld II BMV @tab @tab X @item Interplay C93 @tab @tab X @tab Used in the game Cyberia from Interplay. @item Delphine Software International CIN @tab @tab X @@ -376,6 +377,7 @@ following image formats are supported: @tab AVS1-P2, JiZhun profile, encoding through external library libxavs @item Delphine Software International CIN video @tab @tab X @tab Codec used in Delphine Software International games. +@item Discworld II BMV Video @tab @tab X @item Cinepak @tab @tab X @item Cirrus Logic AccuPak @tab @tab X @tab fourcc: CLJR @@ -608,6 +610,7 @@ following image formats are supported: @tab Used in Bink and Smacker files in many games. @item Delphine Software International CIN audio @tab @tab X @tab Codec used in Delphine Software International games. +@item Discworld II BMV Audio @tab @tab X @item COOK @tab @tab X @tab All versions except 5.1 are supported. @item DCA (DTS Coherent Acoustics) @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 421072ee88..413b04d142 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -94,6 +94,8 @@ OBJS-$(CONFIG_BINKAUDIO_DCT_DECODER) += binkaudio.o wma.o OBJS-$(CONFIG_BINKAUDIO_RDFT_DECODER) += binkaudio.o wma.o OBJS-$(CONFIG_BMP_DECODER) += bmp.o msrledec.o OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o +OBJS-$(CONFIG_BMV_VIDEO_DECODER) += bmv.o +OBJS-$(CONFIG_BMV_AUDIO_DECODER) += bmv.o OBJS-$(CONFIG_C93_DECODER) += c93.o OBJS-$(CONFIG_CAVS_DECODER) += cavs.o cavsdec.o cavsdsp.o \ mpeg12data.o mpegvideo.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 23d2252f98..2beb81c0b8 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -81,6 +81,7 @@ void avcodec_register_all(void) REGISTER_DECODER (BFI, bfi); REGISTER_DECODER (BINK, bink); REGISTER_ENCDEC (BMP, bmp); + REGISTER_DECODER (BMV_VIDEO, bmv_video); REGISTER_DECODER (C93, c93); REGISTER_DECODER (CAVS, cavs); REGISTER_DECODER (CDGRAPHICS, cdgraphics); @@ -242,6 +243,7 @@ void avcodec_register_all(void) REGISTER_DECODER (ATRAC3, atrac3); REGISTER_DECODER (BINKAUDIO_DCT, binkaudio_dct); REGISTER_DECODER (BINKAUDIO_RDFT, binkaudio_rdft); + REGISTER_DECODER (BMV_AUDIO, bmv_audio); REGISTER_DECODER (COOK, cook); REGISTER_DECODER (DCA, dca); REGISTER_DECODER (DSICINAUDIO, dsicinaudio); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index eda1fab9ce..f169bec37e 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -219,6 +219,7 @@ enum CodecID { CODEC_ID_G729, #endif CODEC_ID_UTVIDEO, + CODEC_ID_BMV_VIDEO, /* various PCM "codecs" */ CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs @@ -359,6 +360,7 @@ enum CodecID { CODEC_ID_8SVX_EXP, CODEC_ID_8SVX_FIB, #endif + CODEC_ID_BMV_AUDIO, /* subtitle codecs */ CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. diff --git a/libavcodec/bmv.c b/libavcodec/bmv.c new file mode 100644 index 0000000000..86d1e913a2 --- /dev/null +++ b/libavcodec/bmv.c @@ -0,0 +1,360 @@ +/* + * Discworld II BMV video and audio decoder + * Copyright (c) 2011 Konstantin Shishkov + * + * 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 "bytestream.h" + +enum BMVFlags{ + BMV_NOP = 0, + BMV_END, + BMV_DELTA, + BMV_INTRA, + + BMV_SCROLL = 0x04, + BMV_PALETTE = 0x08, + BMV_COMMAND = 0x10, + BMV_AUDIO = 0x20, + BMV_EXT = 0x40, + BMV_PRINT = 0x80 +}; + +#define SCREEN_WIDE 640 +#define SCREEN_HIGH 429 + +typedef struct BMVDecContext { + AVCodecContext *avctx; + AVFrame pic; + + uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)]; + uint32_t pal[256]; + const uint8_t *stream; +} BMVDecContext; + +#define NEXT_BYTE(v) v = forward ? v + 1 : v - 1; + +static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off) +{ + int val, saved_val = 0; + int tmplen = src_len; + const uint8_t *src, *source_end = source + src_len; + uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH; + uint8_t *dst, *dst_end; + int len, mask; + int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0); + int read_two_nibbles, flag; + int advance_mode; + int mode = 0; + int i; + + if (src_len <= 0) + return -1; + + if (forward) { + src = source; + dst = frame; + dst_end = frame_end; + } else { + src = source + src_len - 1; + dst = frame_end - 1; + dst_end = frame - 1; + } + for (;;) { + int shift = 0; + flag = 0; + + /* The mode/len decoding is a bit strange: + * values are coded as variable-length codes with nibble units, + * code end is signalled by two top bits in the nibble being nonzero. + * And since data is bytepacked and we read two nibbles at a time, + * we may get a nibble belonging to the next code. + * Hence this convoluted loop. + */ + if (!mode || (tmplen == 4)) { + if (src < source || src >= source_end) + return -1; + val = *src; + read_two_nibbles = 1; + } else { + val = saved_val; + read_two_nibbles = 0; + } + if (!(val & 0xC)) { + for (;;) { + if (!read_two_nibbles) { + if (src < source || src >= source_end) + return -1; + shift += 2; + val |= *src << shift; + if (*src & 0xC) + break; + } + // two upper bits of the nibble is zero, + // so shift top nibble value down into their place + read_two_nibbles = 0; + shift += 2; + mask = (1 << shift) - 1; + val = ((val >> 2) & ~mask) | (val & mask); + NEXT_BYTE(src); + if ((val & (0xC << shift))) { + flag = 1; + break; + } + } + } else if (mode) { + flag = tmplen != 4; + } + if (flag) { + tmplen = 4; + } else { + saved_val = val >> (4 + shift); + tmplen = 0; + val &= (1 << (shift + 4)) - 1; + NEXT_BYTE(src); + } + advance_mode = val & 1; + len = (val >> 1) - 1; + mode += 1 + advance_mode; + if (mode >= 4) + mode -= 3; + if (FFABS(dst_end - dst) < len) + return -1; + switch (mode) { + case 1: + if (forward) { + if (dst - frame + SCREEN_WIDE < frame_off || + frame_end - dst < frame_off + len) + return -1; + for (i = 0; i < len; i++) + dst[i] = dst[frame_off + i]; + dst += len; + } else { + dst -= len; + if (dst - frame + SCREEN_WIDE < frame_off || + frame_end - dst < frame_off + len) + return -1; + for (i = len - 1; i >= 0; i--) + dst[i] = dst[frame_off + i]; + } + break; + case 2: + if (forward) { + if (source + src_len - src < len) + return -1; + memcpy(dst, src, len); + dst += len; + src += len; + } else { + if (src - source < len) + return -1; + dst -= len; + src -= len; + memcpy(dst, src, len); + } + break; + case 3: + val = forward ? dst[-1] : dst[1]; + if (forward) { + memset(dst, val, len); + dst += len; + } else { + dst -= len; + memset(dst, val, len); + } + break; + default: + break; + } + if (dst == dst_end) + return 0; + } + return 0; +} + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt) +{ + BMVDecContext * const c = avctx->priv_data; + int type, scr_off; + int i; + uint8_t *srcptr, *outptr; + + c->stream = pkt->data; + type = bytestream_get_byte(&c->stream); + if (type & BMV_AUDIO) { + int blobs = bytestream_get_byte(&c->stream); + if (pkt->size < blobs * 65 + 2) { + av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n"); + return AVERROR_INVALIDDATA; + } + c->stream += blobs * 65; + } + if (type & BMV_COMMAND) { + int command_size = (type & BMV_PRINT) ? 8 : 10; + if (c->stream - pkt->data + command_size > pkt->size) { + av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n"); + return AVERROR_INVALIDDATA; + } + c->stream += command_size; + } + if (type & BMV_PALETTE) { + if (c->stream - pkt->data > pkt->size - 768) { + av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n"); + return AVERROR_INVALIDDATA; + } + for (i = 0; i < 256; i++) + c->pal[i] = bytestream_get_be24(&c->stream); + } + if (type & BMV_SCROLL) { + if (c->stream - pkt->data > pkt->size - 2) { + av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n"); + return AVERROR_INVALIDDATA; + } + scr_off = (int16_t)bytestream_get_le16(&c->stream); + } else if ((type & BMV_INTRA) == BMV_INTRA) { + scr_off = -640; + } else { + scr_off = 0; + } + + if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) { + av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n"); + return AVERROR_INVALIDDATA; + } + + memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE); + c->pic.palette_has_changed = type & BMV_PALETTE; + + outptr = c->pic.data[0]; + srcptr = c->frame; + + for (i = 0; i < avctx->height; i++) { + memcpy(outptr, srcptr, avctx->width); + srcptr += avctx->width; + outptr += c->pic.linesize[0]; + } + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = c->pic; + + /* always report that the buffer was completely consumed */ + return pkt->size; +} + +static av_cold int decode_init(AVCodecContext *avctx) +{ + BMVDecContext * const c = avctx->priv_data; + + c->avctx = avctx; + avctx->pix_fmt = PIX_FMT_PAL8; + + c->pic.reference = 1; + if (avctx->get_buffer(avctx, &c->pic) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + c->frame = c->frame_base + 640; + + return 0; +} + +static av_cold int decode_end(AVCodecContext *avctx) +{ + BMVDecContext *c = avctx->priv_data; + + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + + return 0; +} + +static const int bmv_aud_mults[16] = { + 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32 +}; + +static av_cold int bmv_aud_decode_init(AVCodecContext *avctx) +{ + + if (avctx->channels != 2) { + av_log(avctx, AV_LOG_INFO, "invalid number of channels\n"); + return AVERROR(EINVAL); + } + + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + + return 0; +} + +static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + int blocks = 0, total_blocks, i; + int out_size; + int16_t *output_samples = data; + int scale[2]; + + total_blocks = *buf++; + if (buf_size < total_blocks * 65 + 1) { + av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n", + total_blocks * 65 + 1, buf_size); + return AVERROR_INVALIDDATA; + } + out_size = total_blocks * 64 * sizeof(*output_samples); + if (*data_size < out_size) { + av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n"); + return AVERROR(EINVAL); + } + + for (blocks = 0; blocks < total_blocks; blocks++) { + uint8_t code = *buf++; + code = (code >> 1) | (code << 7); + scale[0] = bmv_aud_mults[code & 0xF]; + scale[1] = bmv_aud_mults[code >> 4]; + for (i = 0; i < 32; i++) { + *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5); + *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5); + } + } + + *data_size = out_size; + return buf_size; +} + +AVCodec ff_bmv_video_decoder = { + .name = "bmv_video", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_BMV_VIDEO, + .priv_data_size = sizeof(BMVDecContext), + .init = decode_init, + .close = decode_end, + .decode = decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"), +}; + +AVCodec ff_bmv_audio_decoder = { + .name = "bmv_audio", + .type = AVMEDIA_TYPE_AUDIO, + .id = CODEC_ID_BMV_AUDIO, + .init = bmv_aud_decode_init, + .decode = bmv_aud_decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"), +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 3fc418e466..5e07ec28e3 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 16 +#define LIBAVCODEC_VERSION_MINOR 17 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavformat/Makefile b/libavformat/Makefile index 54bc8a1645..0c7d258640 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -46,6 +46,7 @@ OBJS-$(CONFIG_AVS_DEMUXER) += avs.o vocdec.o voc.o OBJS-$(CONFIG_BETHSOFTVID_DEMUXER) += bethsoftvid.o OBJS-$(CONFIG_BFI_DEMUXER) += bfi.o OBJS-$(CONFIG_BINK_DEMUXER) += bink.o +OBJS-$(CONFIG_BMV_DEMUXER) += bmv.o OBJS-$(CONFIG_C93_DEMUXER) += c93.o vocdec.o voc.o OBJS-$(CONFIG_CAF_DEMUXER) += cafdec.o caf.o mov.o riff.o isom.o OBJS-$(CONFIG_CAVSVIDEO_DEMUXER) += cavsvideodec.o rawdec.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 01900e8a73..00924c8a11 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -70,6 +70,7 @@ void av_register_all(void) REGISTER_DEMUXER (BETHSOFTVID, bethsoftvid); REGISTER_DEMUXER (BFI, bfi); REGISTER_DEMUXER (BINK, bink); + REGISTER_DEMUXER (BMV, bmv); REGISTER_DEMUXER (C93, c93); REGISTER_DEMUXER (CAF, caf); REGISTER_MUXDEMUX (CAVSVIDEO, cavsvideo); diff --git a/libavformat/bmv.c b/libavformat/bmv.c new file mode 100644 index 0000000000..fb41712868 --- /dev/null +++ b/libavformat/bmv.c @@ -0,0 +1,136 @@ +/* + * Discworld II BMV demuxer + * Copyright (c) 2011 Konstantin Shishkov. + * + * 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 "avformat.h" + +enum BMVFlags { + BMV_NOP = 0, + BMV_END, + BMV_DELTA, + BMV_INTRA, + + BMV_AUDIO = 0x20, +}; + +typedef struct BMVContext { + uint8_t *packet; + int size; + int get_next; + int64_t audio_pos; +} BMVContext; + +static int bmv_read_header(AVFormatContext *s, AVFormatParameters *ap) +{ + AVStream *st, *ast; + BMVContext *c = s->priv_data; + + st = avformat_new_stream(s, 0); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_type = AVMEDIA_TYPE_VIDEO; + st->codec->codec_id = CODEC_ID_BMV_VIDEO; + st->codec->width = 640; + st->codec->height = 429; + st->codec->pix_fmt = PIX_FMT_PAL8; + av_set_pts_info(st, 16, 1, 12); + ast = avformat_new_stream(s, 0); + if (!ast) + return AVERROR(ENOMEM); + ast->codec->codec_type = AVMEDIA_TYPE_AUDIO; + ast->codec->codec_id = CODEC_ID_BMV_AUDIO; + ast->codec->channels = 2; + ast->codec->sample_rate = 22050; + av_set_pts_info(ast, 16, 1, 22050); + + c->get_next = 1; + c->audio_pos = 0; + return 0; +} + +static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + BMVContext *c = s->priv_data; + int type; + void *tmp; + + while (c->get_next) { + if (s->pb->eof_reached) + return AVERROR_EOF; + type = avio_r8(s->pb); + if (type == BMV_NOP) + continue; + if (type == BMV_END) + return AVERROR_EOF; + c->size = avio_rl24(s->pb); + if (!c->size) + return AVERROR_INVALIDDATA; + tmp = av_realloc(c->packet, c->size + 1); + if (!tmp) + return AVERROR(ENOMEM); + c->packet = tmp; + c->packet[0] = type; + if (avio_read(s->pb, c->packet + 1, c->size) != c->size) + return AVERROR(EIO); + if (type & BMV_AUDIO) { + int audio_size = c->packet[1] * 65 + 1; + if (audio_size >= c->size) { + av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n", + audio_size, c->size); + return AVERROR_INVALIDDATA; + } + if (av_new_packet(pkt, audio_size) < 0) + return AVERROR(ENOMEM); + memcpy(pkt->data, c->packet + 1, pkt->size); + pkt->stream_index = 1; + pkt->pts = c->audio_pos; + pkt->duration = c->packet[1] * 32; + c->audio_pos += pkt->duration; + c->get_next = 0; + return pkt->size; + } else + break; + } + if (av_new_packet(pkt, c->size + 1) < 0) + return AVERROR(ENOMEM); + pkt->stream_index = 0; + c->get_next = 1; + memcpy(pkt->data, c->packet, pkt->size); + return pkt->size; +} + +static int bmv_read_close(AVFormatContext *s) +{ + BMVContext *c = s->priv_data; + + av_freep(&c->packet); + + return 0; +} + +AVInputFormat ff_bmv_demuxer = { + .name = "bmv", + .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"), + .priv_data_size = sizeof(BMVContext), + .read_header = bmv_read_header, + .read_packet = bmv_read_packet, + .read_close = bmv_read_close, + .extensions = "bmv" +}; diff --git a/libavformat/version.h b/libavformat/version.h index 48f2303f9e..ec5d1fda03 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -24,7 +24,7 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 53 -#define LIBAVFORMAT_VERSION_MINOR 13 +#define LIBAVFORMAT_VERSION_MINOR 14 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ From 1c8c41ff079953d332f61a9d26656e4e36af606c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 7 Nov 2011 23:07:39 +0200 Subject: [PATCH 12/12] avformat: Avoid a warning about mixed declarations and code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index cb472c925a..309fb97a0d 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -3967,8 +3967,8 @@ int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_co int avformat_network_init(void) { #if CONFIG_NETWORK - ff_network_inited_globally = 1; int ret; + ff_network_inited_globally = 1; if ((ret = ff_network_init()) < 0) return ret; ff_tls_init();