diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..eac1758ed9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +.config +.version +*.o +*.d +*.exe +*.ho +*-example +*-test +*_g +config.* +doc/*.1 +doc/*.html +doc/*.pod +doxy +ffmpeg +ffplay +ffprobe +ffserver +libavcodec/libavcodec* +libavcore/libavcore* +libavdevice/libavdevice* +libavfilter/libavfilter* +libavformat/libavformat* +libavutil/avconfig.h +libavutil/libavutil* +libpostproc/libpostproc* +libswscale/libswscale* +tests/audiogen +tests/base64 +tests/data +tests/rotozoom +tests/seek_test +tests/tiny_psnr +tests/videogen +tests/vsynth1 +tests/vsynth2 +tools/cws2fws +tools/graph2dot +tools/lavfi-showfiltfmts +tools/pktdumper +tools/probetest +tools/qt-faststart +tools/trasher +tools/trasher*.d +version.h diff --git a/Changelog b/Changelog index c454c1596d..6f753216f5 100644 --- a/Changelog +++ b/Changelog @@ -1,14 +1,158 @@ Entries are sorted chronologically from oldest to youngest within each release, releases are sorted from youngest to oldest. +version 0.6.6: +- id3v2: fix skipping extended header in id3v2.4 +- nsvdec: Fix use of uninitialized streams +- nsvdec: Be more careful with av_malloc() +- nsvdec: Propagate errors +- dv: check stype +- dv: Fix null pointer dereference due to ach=0 +- dv: Fix small stack overread related to CVE-2011-3929 and CVE-2011-3936. +- atrac3: Fix crash in tonal component decoding +- mjpegbdec: Fix overflow in SOS +- kgv1dec: Increase offsets array size so it is large enough +- motionpixels: decode only the 111 complete frames for fate +- motionpixels: Clip YUV values after applying a gradient +- celp filters: Do not read earlier than the start of the 'out' vector +- vqavideo: return error if image size is not a multiple of block size (CVE-2012-0947) +- dpcm: ignore extra unpaired bytes in stereo streams (CVE-2011-3951) +- aacsbr: prevent out of bounds memcpy() (CVE-2012-0850) +- h264: Add check for invalid chroma_format_idc (CVE-2012-0851) +- adpcm: ADPCM Electronic Arts has always two channels (CVE-2012-0852) +- shorten: remove VLA and check for buffer overflow +- shorten: Prevent block size from increasing +- shorten: Fix out of bound writes in fix_bitshift() +- shorten: check for realloc failure (CVE-2012-0858) +- shorten: Use separate pointers for the allocated memory for decoded samples (CVE-2012-0858) +- kmvc: Check palsize (CVE-2011-3952) +- qdm2: clip array indices returned by qdm2_get_vlc() +- configure: properly check for mingw-w64 through installed headers +- mingw32: properly check if vfw capture is supported by the system headers +- mingw32: merge checks for mingw-w64 and mingw32-runtime >= 3.15 into one +- dxva2: define required feature selection macros +- png: check bit depth for PAL8/Y400A pixel formats +- ea: check chunk_size for validity +- tqi: Pass errors from the MB decoder -version : + +version 0.6.5: +- vorbis: An additional defense in the Vorbis codec. (CVE-2011-3895) +- vorbisdec: Fix decoding bug with channel handling. +- matroskadec: Fix a bug where a pointer was cached to an array that might + later move due to a realloc(). (CVE-2011-3893) +- vorbis: Avoid some out-of-bounds reads. (CVE-2011-3893) +- vp3: fix oob read for negative tokens and memleaks on error, (CVE-2011-3892) +- vp3: fix streams with non-zero last coefficient. + + +version 0.6.4: +- 4xm: Add a check in decode_i_frame to prevent buffer overreads +- wma: initialize prev_block_len_bits, next_block_len_bits, and block_len_bits. +- swscale: #include "libavutil/mathematics.h" +- vp3dec: Check coefficient index in vp3_dequant() (CVE-2011-4352) +- svq1dec: call avcodec_set_dimensions() after dimensions changed. (CVE-2011-4579) +- commits related to CVE-2011-4353: + - vp6: Fix illegal read. + - vp6: Reset the internal state when aborting key frames header parsing + - vp6: Check for huffman tree build errors + - vp6: partially propagate huffman tree building errors during coeff model parsing and fix misspelling +- commits related to CVE-2011-4351: + - qdm2: check output buffer size before decoding + - Fix qdm2 decoder packet handling to match the api + - Fix out of bound reads in the QDM2 decoder. + - Check for out of bound writes in the QDM2 decoder. +- vmd: fix segfaults on corruped streams (CVE-2011-4364) +- rv34: Check for invalid slice offsets +- rv34: Fix potential overreads +- rv34: Avoid NULL dereference on corrupted bitstream +- rv10: Reject slices that does not have the same type as the first one +- oggdec: fix out of bound write in the ogg demuxer +- smacker: fix a few off by 1 errors +- Check for invalid VLC value in smacker decoder. +- Check and propagate errors when VLC trees cannot be built in smacker decoder. +- Fixed off by one packet size allocation in the smacker demuxer. +- Check for invalid packet size in the smacker demuxer. +- ape demuxer: fix segfault on memory allocation failure. +- Fixed size given to init_get_bits() in xan decoder. +- smacker demuxer: handle possible av_realloc() failure. +- Fixed segfault with wavpack decoder on corrupted decorrelation terms sub-blocks. +- indeo2: fail if input buffer too small +- indeo2: init_get_bits size in bits instead of bytes +- wavpack: Check error codes rather than working around error conditions. +- Fixed invalid writes and reads in wavpack decoder on corrupted bitstreams. +- cpu detection: avoid a signed overflow +- h264: correct implicit weight table computation for long ref pics +- h264: correct the check for invalid long term frame index in MMCO decode +- rv10/20: tell decoder to use edge emulation +- flvenc: use int64_t to store offsets +- VC-1: fix reading of custom PAR. +- h264: notice memory allocation failure +- libx264: do not set pic quality if no frame is output +- mxfdec: Include FF_INPUT_BUFFER_PADDING_SIZE when allocating extradata. +- rv30: return AVERROR(EINVAL) instead of EINVAL +- Do not decode RV30 files if the extradata is too small +- aacps: skip some memcpy() if src and dst would be equal +- mpegts: fix Continuity Counter error detection +- alsa: fallback to buffer_size/4 for period_size. +- mxfenc: fix ignored drop flag in binary timecode representation. +- dca: set AVCodecContext frame_size for DTS audio +- H.264: fix overreads of qscale_table +- postprocess.c: filter name needs to be double 0 terminated +- Replace strncpy() with av_strlcpy() in libpostproc. +- jpegdec: actually search for and parse RSTn +- riff: Add mpgv MPEG-2 fourcc +- Added M701 codec_tag for mpeg2video + + +version 0.6.3: + +- fix compilation with --enable-hardcoded-tables +- mjpeg: Detect overreads in mjpeg_decode_scan() and error out. +- aac: add headers needed for log2f() +- lavf: inspect more frames for fps when container time base is coarse +- AMV: disable DR1 and don't override EMU_EDGE + (addresses http://seclists.org/bugtraq/2011/Apr/257) +- Fix memory (re)allocation in matroskadec.c (MSVR11-011/CVE-2011-3504) +- Fix some crashes with invalid bitstreams in the CAVS decoder + (CVE-2011-3362, CVE-2011-3973, CVE-2011-3974) +- Compilation fixes for gcc-4.6, testsuite now passes again +- Fix a heap corruption issue in the OGG decoder +- Backported the Android VisualOn AAC encoder wrapper from 0.7.2 + + +version 0.6.3: + +- AMV: Fix possibly exploitable crash. +- Fix apparently exploitable race condition. + (addresses http://seclists.org/bugtraq/2011/Apr/257) + +version 0.6.2: + +- fix compilation with --enable-hardcoded-tables +- Fix invalid reads in VC-1 decoding (related to CVE-2011-0723) +- Do not attempt to decode APE file with no frames + (adresses http://packetstorm.linuxsecurity.com/1103-exploits/vlc105-dos.txt) + + +version 0.6.1: + +- fix autodetection of E-AC-3 substream samples +- performance fix for seekable HTTP +- backport AAC-HE v2 from trunk +- add missing VP80 fourcc code for the VP8 codec +- small documentation fixes +- fix several potentially exploitable issues in the FLIC decoder + (addresses CVE-2010-3429) + + +version 0.6: - PB-frame decoding for H.263 - deprecated vhook subsystem removed - deprecated old scaler removed - VQF demuxer -- Alpha channel scaler +- alpha channel scaler - PCX encoder - RTP packetization of H.263 - RTP packetization of AMR @@ -70,6 +214,17 @@ version : - Psygnosis YOP demuxer and video decoder - spectral extension support in the E-AC-3 decoder - unsharp video filter +- RTP hinting in the mov/3gp/mp4 muxer +- Dirac in Ogg demuxing +- seek to keyframes in Ogg +- 4:2:2 and 4:4:4 Theora decoding +- 35% faster VP3/Theora decoding +- faster AAC decoding +- faster H.264 decoding +- WebM support in Matroska de/muxer +- low overhead Ogg muxing +- VP8 de/encoding via libvpx +- CODEC_CAP_EXPERIMENTAL added diff --git a/MAINTAINERS b/MAINTAINERS index f9891fed87..c558bad2c7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8,7 +8,6 @@ FFmpeg code. Project Leader ============== -Michael Niedermayer final design decisions diff --git a/RELEASE b/RELEASE new file mode 100644 index 0000000000..746ddb480e --- /dev/null +++ b/RELEASE @@ -0,0 +1,212 @@ +Release Notes +============= + +* 0.6 "Works with HTML5" June, 2010 + +General notes +------------- + +This release focuses on improvements for the new multimedia elements in HTML5. +The H.264 and Theora decoders are now significantly faster, the Vorbis decoder +has seen important updates and this release supports Google's newly released +libvpx library for the VP8 codec and WebM container. + +Other important changes are additions of decoders including, but not limited to, +Intel Indeo 5, WMA Pro, WMA Voice and HE-AAC. + +See the Changelog file for a list of significant changes. + +Please note that our policy on bug reports has not changed. We still only accept +bug reports against HEAD of the FFmpeg trunk repository. If you are experiencing +any issues with any formally released version of FFmpeg, please try a current +version of the development code to check if the issue still exists. If it does, +make your report against the development code following the usual bug reporting +guidelines. + + +API and other notable Changes +----------------------------- + +Please see the file doc/APIchanges for programmer-centric information. + +Notable changes: +- deprecated vhook subsystem removed +- deprecated old scaler removed +- nonfree libamr support for AMR-NB/WB decoding/encoding removed +- RTMP support in libavformat +- -formats option split into -formats, -codecs, -bsfs, and -protocols +- ffprobe tool +- RTMP/RTMPT/RTMPS/RTMPE/RTMPTE protocol support via librtmp +- CODEC_CAP_EXPERIMENTAL added + + +Added Codecs: +------------- + +- VQF demuxer +- PCX encoder +- CorePNG decoding support +- 8088flex TMV demuxer and decoder +- enable symbol versioning by default for linkers that support it +- V210 decoder and encoder +- QCP demuxer +- SoX native format muxer and demuxer +- AMR-NB decoding/encoding, AMR-WB decoding via OpenCORE libraries +- DPX image decoder +- Electronic Arts Madcow decoder +- DivX (XSUB) subtitle encoder +- experimental AAC encoder +- Wave64 demuxer +- IEC-61937 compatible Muxer +- TwinVQ decoder +- Bluray (PGS) subtitle decoder +- LPCM support in MPEG-TS (HDMV RID as found on Blu-ray disks) +- WMA Pro decoder +- Core Audio Format demuxer +- Atrac1 decoder +- MD STUDIO audio demuxer +- RF64 support in WAV demuxer +- MPEG-4 Audio Lossless Coding (ALS) decoder +- IV8 demuxer +- CDG demuxer and decoder +- R210 decoder +- Auravision Aura 1 and 2 decoders +- Deluxe Paint Animation playback system +- SIPR decoder +- Adobe Filmstrip muxer and demuxer +- RTP packetization and depacketization of H.263 and AMR +- Bink demuxer and audio/video decoders +- IFF PBM/ILBM bitmap decoder +- Indeo 5 decoder +- WMA Voice decoder +- AMR-NB decoder +- RTSP muxer +- HE-AAC v1 decoder +- Kega Game Video (KGV1) decoder +- Psygnosis YOP demuxer and video decoder +- RTP hinting in the mov/3gp/mp4 muxer +- VP8 decoding via libvpx + + +Notable license related changes +------------------------------- + +- remaining GPL parts in AC-3 decoder converted to LGPL +- libswscale can now be compiled in LGPL mode + + + +* 0.6.1 + +General notes +------------- + +This point release includes some updates to make the 0.6 release series usable +for users that need to retain the existing behavior as closely as possible. +The changes follow below: + +Bugfixes +-------- + +- fix autodetection of E-AC-3 substream samples +- performance fix for seekable HTTP +- add missing VP80 fourcc code for the VP8 codec +- small documentation fixes +- fix several potentially exploitable issues in the FLIC decoder + (addresses CVE-2010-3429) + + +HE-AAC v2 backport +------------------ + +This release includes a backport of the AAC decoder from trunk, which +enables proper playback of HE-AAC v2 media. + + +* 0.6.2 + +General notes +------------- + +This is a maintenance-only release that addresses a small number of security +and portability issues. Distributors and system integrators are encouraged +to update and share their patches against this branch. + +Security fixes +-------------- + +Programming errors in container and codec implementations may lead to +denial of service or the execution of arbitrary code if the user is +tricked into opening a malformed media file or stream. + +Affected and updated have been the implementations of the following +codecs and container formats: + + - VC1 decoder (Change related to CVE-2011-0723) + - APE decoder (cf. http://packetstorm.linuxsecurity.com/1103-exploits/vlc105-dos.txt) + + +* 0.6.3 + +General notes +------------- + +This is a mostly maintenance-only release that addresses a small number +of bugs such as security and compilation issues. Moreover, this release +has been updated to work with gcc-4.6 and the VisualOn AAC encoder has +been backported from the Libav 0.7.2 release. Distributors and system +integrators are encouraged to update and share their patches against +this branch. +For a full list of changes please see the Changelog file. + + +* 0.6.4 + +General notes +------------- + +This mostly maintenance-only release that addresses a number a number of +bugs such as security and compilation issues that have been brought to +our attention. Among other (rather minor) fixes, this release features +fixes for the QDM2 decoder (CVE-2011-4351), vp3 decoder (CVE-2011-4352), +DoS in the VP5/VP6 decoders (CVE-2011-4353), a buffer overflow in the +Sierra VMD decoder CVE-2011-4364, and a safety fix in the svq1 decoder +(CVE-2011-4579). + +Distributors and system integrators are encouraged +to update and share their patches against this branch. For a full list +of changes please see the Changelog file. + + +* 0.6.5 + +General notes +------------- + +This mostly maintenance-only release that addresses a number a number of +bugs such as security and compilation issues that have been brought to +our attention. Among other (rather minor) fixes, this release features +fixes for the VP3 decoder (CVE-2011-3892), vorbis decoder, and matroska +demuxer (CVE-2011-3893 and CVE-2011-3895). + +Distributors and system integrators are encouraged +to update and share their patches against this branch. For a full list +of changes please see the Changelog file. + + +* 0.6.6 + +General notes +------------- + +This mostly maintenance-only release that addresses a number a number of +bugs such as security and compilation issues that have been brought to +our attention. Among other fixes, this release features includes +security updates for the, DV decoder (CVE-2011-3929 and CVE-2011-3936), +VQA Video Decoder (CVE-2012-0947), DPCM codecs (CVE-2011-3951), AAC SBR +(CVE-2012-0850), H.264 (CVE-2012-0851), ADPCM (CVE-2012-0852), Shorten +(CVE-2012-0858), and the KMVC decoder (CVE-2011-3952). + +Distributors and system integrators are encouraged +to update and share their patches against this branch. For a full list +of changes please see the Changelog file or the git commit history. diff --git a/VERSION b/VERSION new file mode 100644 index 0000000000..2228cad41f --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.6.7 diff --git a/cmdutils.c b/cmdutils.c index e6efc491d7..2349b7052e 100644 --- a/cmdutils.c +++ b/cmdutils.c @@ -292,17 +292,11 @@ void set_context_opts(void *ctx, void *opts_ctx, int flags) void print_error(const char *filename, int err) { char errbuf[128]; + const char *errbuf_ptr = errbuf; - switch(err) { -#if CONFIG_NETWORK - case FF_NETERROR(EPROTONOSUPPORT): - fprintf(stderr, "%s: Unsupported network protocol\n", filename); - break; -#endif - default: - av_strerror(err, errbuf, sizeof(errbuf)); - fprintf(stderr, "%s: %s\n", filename, errbuf); - } + if (av_strerror(err, errbuf, sizeof(errbuf)) < 0) + errbuf_ptr = strerror(AVUNERROR(err)); + fprintf(stderr, "%s: %s\n", filename, errbuf_ptr); } #define PRINT_LIB_VERSION(outstream,libname,LIBNAME,indent) \ @@ -615,6 +609,11 @@ void show_pix_fmts(void) "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n" "-----\n"); +#if !CONFIG_SWSCALE +# define sws_isSupportedInput(x) 0 +# define sws_isSupportedOutput(x) 0 +#endif + for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) { const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt]; printf("%c%c%c%c%c %-16s %d %2d\n", @@ -642,7 +641,7 @@ int read_yesno(void) int read_file(const char *filename, char **bufptr, size_t *size) { - FILE *f = fopen(filename, "r"); + FILE *f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno)); diff --git a/cmdutils.h b/cmdutils.h index ad8cefdeee..5656370610 100644 --- a/cmdutils.h +++ b/cmdutils.h @@ -134,6 +134,15 @@ void parse_options(int argc, char **argv, const OptionDef *options, void set_context_opts(void *ctx, void *opts_ctx, int flags); +/** + * Prints an error message to stderr, indicating filename and a human + * readable description of the error code err. + * + * If strerror_r() is not available the use of this function in a + * multithreaded application may be unsafe. + * + * @see av_strerror() + */ void print_error(const char *filename, int err); void list_fmts(void (*get_fmt_string)(char *buf, int buf_size, int fmt), int nb_fmts); diff --git a/configure b/configure index 5d1ba2b006..9d30ed92a8 100755 --- a/configure +++ b/configure @@ -181,8 +181,10 @@ External library support: --enable-libschroedinger enable Dirac support via libschroedinger [no] --enable-libspeex enable Speex decoding via libspeex [no] --enable-libtheora enable Theora encoding via libtheora [no] + --enable-libvo-aacenc enable AAC encoding via libvo-aacenc [no] --enable-libvorbis enable Vorbis encoding via libvorbis, native implementation exists [no] + --enable-libvpx enable VP8 support via libvpx [no] --enable-libx264 enable H.264 encoding via x264 [no] --enable-libxvid enable Xvid encoding via xvidcore, native MPEG-4/Xvid encoder exists [no] @@ -252,6 +254,8 @@ EOF exit 0 } +quotes='""' + log(){ echo "$@" >> $logfile } @@ -926,7 +930,9 @@ CONFIG_LIST=" libschroedinger libspeex libtheora + libvo_aacenc libvorbis + libvpx libx264 libxvid lpc @@ -1031,6 +1037,7 @@ HAVE_LIST=" dlfcn_h dlopen dos_paths + dxva_h ebp_available ebx_available exp2 @@ -1084,6 +1091,9 @@ HAVE_LIST=" struct_sockaddr_in6 struct_sockaddr_sa_len struct_sockaddr_storage + symver + symver_gnu_asm + symver_asm_label sys_mman_h sys_resource_h sys_select_h @@ -1198,6 +1208,8 @@ fast_unaligned_if_any="armv6 ppc x86" need_memalign="altivec neon sse" inline_asm_deps="!tms470" +symver_if_any="symver_asm_label symver_gnu_asm" + # subsystems mdct_select="fft" rdft_select="fft" @@ -1334,7 +1346,7 @@ libdirac_decoder_deps="libdirac !libschroedinger" libdirac_encoder_deps="libdirac" libfaac_encoder_deps="libfaac" libfaad_decoder_deps="libfaad" -libfaadbin_decoder_extralibs='$ldl' +libfaadbin_extralibs='$ldl' libgsm_decoder_deps="libgsm" libgsm_encoder_deps="libgsm" libgsm_ms_decoder_deps="libgsm" @@ -1348,7 +1360,10 @@ libschroedinger_decoder_deps="libschroedinger" libschroedinger_encoder_deps="libschroedinger" libspeex_decoder_deps="libspeex" libtheora_encoder_deps="libtheora" +libvo_aacenc_encoder_deps="libvo_aacenc" libvorbis_encoder_deps="libvorbis" +libvpx_decoder_deps="libvpx" +libvpx_encoder_deps="libvpx" libx264_encoder_deps="libx264" libxvid_encoder_deps="libxvid" @@ -1398,7 +1413,7 @@ oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" v4l_indev_deps="linux_videodev_h" v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h" -vfwcap_indev_deps="capCreateCaptureWindow" +vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines" vfwcap_indev_extralibs="-lavicap32" x11_grab_device_indev_deps="x11grab XShmCreateImage" x11_grab_device_indev_extralibs="-lX11 -lXext -lXfixes" @@ -2195,7 +2210,7 @@ case $target_os in LIBTARGET=i386 if enabled x86_64; then enable malloc_aligned - LIBTARGET=x64 + LIBTARGET="i386:x86-64" elif enabled arm; then LIBTARGET=arm fi @@ -2205,7 +2220,7 @@ case $target_os in SLIBSUF=".dll" SLIBNAME_WITH_VERSION='$(SLIBPREF)$(FULLNAME)-$(LIBVERSION)$(SLIBSUF)' SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)' - SLIB_EXTRA_CMD='-lib.exe /machine:$(LIBTARGET) /def:$$(@:$(SLIBSUF)=.def) /out:$(SUBDIR)$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.lib)' + SLIB_EXTRA_CMD=-'$(DLLTOOL) -m $(LIBTARGET) -d $$(@:$(SLIBSUF)=.def) -l $(SUBDIR)$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.lib) -D $(SLIBNAME_WITH_MAJOR)' SLIB_INSTALL_EXTRA_CMD='-install -m 644 $(SUBDIR)$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.lib) "$(SHLIBDIR)/$(SLIBNAME:$(SLIBSUF)=.lib)"; \ install -m 644 $(SUBDIR)$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.lib) "$(SHLIBDIR)/$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.lib)"; \ install -d "$(LIBDIR)"; \ @@ -2213,15 +2228,12 @@ case $target_os in SLIB_UNINSTALL_EXTRA_CMD='rm -f "$(SHLIBDIR)/$(SLIBNAME:$(SLIBSUF)=.lib)"' SHFLAGS='-shared -Wl,--output-def,$$(@:$(SLIBSUF)=.def) -Wl,--out-implib,$(SUBDIR)lib$(SLIBNAME:$(SLIBSUF)=.dll.a) -Wl,--enable-runtime-pseudo-reloc -Wl,--enable-auto-image-base' objformat="win32" + dlltool="${cross_prefix}dlltool" enable dos_paths check_cflags -fno-common - if ! enabled x86_64; then - check_cpp_condition _mingw.h "(__MINGW32_MAJOR_VERSION > 3) || (__MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION >= 15)" || + check_cpp_condition _mingw.h "defined (__MINGW64_VERSION_MAJOR) || (__MINGW32_MAJOR_VERSION > 3) \ + || (__MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION >= 15)" || die "ERROR: MinGW runtime version must be >= 3.15." - enabled_any avisynth vfwcap_indev && - { check_cpp_condition w32api.h "(__W32API_MAJOR_VERSION > 3) || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION >= 13)" || - die "ERROR: avisynth and vfwcap_indev require w32api version 3.13 or later."; } - fi ;; cygwin*) target_os=cygwin @@ -2335,6 +2347,7 @@ die_license_disabled nonfree libfaac die_license_disabled version3 libopencore_amrnb die_license_disabled version3 libopencore_amrwb +die_license_disabled version3 libvo_aacenc enabled version3 && { enabled gpl && enable gplv3 || enable lgplv3; } @@ -2406,11 +2419,6 @@ EOF enabled_all armv6t2 shared !pic && enable_pic -elif enabled ia64; then - - # HACK: currently fails to build if .bss is > 4MB and shared libs are built - enabled shared && enable_weak hardcoded_tables - elif enabled mips; then check_asm loongson '"dmult.g $1, $2, $3"' @@ -2536,6 +2544,7 @@ check_func_headers windows.h VirtualAlloc check_header conio.h check_header dlfcn.h +check_header dxva.h check_header dxva2api.h check_header malloc.h check_header poll.h @@ -2611,12 +2620,15 @@ enabled libnut && require libnut libnut.h nut_demuxer_init -lnut enabled libopencore_amrnb && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb -lm enabled libopencore_amrwb && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb -lm enabled libopenjpeg && require libopenjpeg openjpeg.h opj_version -lopenjpeg -enabled librtmp && require librtmp librtmp/rtmp.h RTMP_Init -lrtmp +enabled librtmp && require librtmp librtmp/rtmp.h RTMP_Init $(pkg-config --libs librtmp) enabled libschroedinger && add_cflags $(pkg-config --cflags schroedinger-1.0) && require libschroedinger schroedinger/schro.h schro_init $(pkg-config --libs schroedinger-1.0) enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex enabled libtheora && require libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg +enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc enabled libvorbis && require libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg +enabled libvpx && require2 libvpx "vpx/vpx_decoder.h vpx/vp8dx.h" "vpx_codec_dec_init_ver" -lvpx && + require2 libvpx "vpx/vpx_encoder.h vpx/vp8cx.h" "vpx_codec_enc_init_ver" -lvpx enabled libx264 && require libx264 x264.h x264_encoder_encode -lx264 -lm && { check_cpp_condition x264.h "X264_BUILD >= 83" || die "ERROR: libx264 version must be >= 0.83."; } @@ -2675,6 +2687,9 @@ check_header linux/videodev2.h check_header sys/videoio.h check_func_headers "windows.h vfw.h" capCreateCaptureWindow "$vfwcap_indev_extralibs" +# check that WM_CAP_DRIVER_CONNECT is defined to the proper value +# w32api 3.12 had it defined wrong +check_cpp_condition vfw.h "WM_CAP_DRIVER_CONNECT > WM_USER" && enable vfwcap_defines # check for ioctl_meteor.h, ioctl_bt848.h and alternatives { check_header dev/bktr/ioctl_meteor.h && @@ -2731,8 +2746,17 @@ check_ldflags '-Wl,-rpath-link,\$(BUILD_ROOT)/libpostproc -Wl,-rpath-link,\$(BUI check_ldflags -Wl,-Bsymbolic echo "X{};" > $TMPV -test_ldflags -Wl,--version-script,$TMPV && +if test_ldflags -Wl,--version-script,$TMPV; then append SHFLAGS '-Wl,--version-script,\$(SUBDIR)lib\$(NAME).ver' + check_cc <flags & CODEC_FLAG_PASS1) { - f = fopen(logfilename, "w"); + f = fopen(logfilename, "wb"); if (!f) { fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno)); av_exit(1); @@ -2912,7 +2912,7 @@ static int opt_input_ts_offset(const char *opt, const char *arg) return 0; } -static enum CodecID find_codec_or_die(const char *name, int type, int encoder) +static enum CodecID find_codec_or_die(const char *name, int type, int encoder, int strict) { const char *codec_string = encoder ? "encoder" : "decoder"; AVCodec *codec; @@ -2930,6 +2930,19 @@ static enum CodecID find_codec_or_die(const char *name, int type, int encoder) fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name); av_exit(1); } + if(codec->capabilities & CODEC_CAP_EXPERIMENTAL && + strict > FF_COMPLIANCE_EXPERIMENTAL) { + fprintf(stderr, "%s '%s' is experimental and might produce bad " + "results.\nAdd '-strict experimental' if you want to use it.\n", + codec_string, codec->name); + codec = encoder ? + avcodec_find_encoder(codec->id) : + avcodec_find_decoder(codec->id); + if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL)) + fprintf(stderr, "Or use the non experimental %s '%s'.\n", + codec_string, codec->name); + av_exit(1); + } return codec->id; } @@ -2977,9 +2990,15 @@ static void opt_input_file(const char *filename) set_context_opts(ic, avformat_opts, AV_OPT_FLAG_DECODING_PARAM); - ic->video_codec_id = find_codec_or_die(video_codec_name , AVMEDIA_TYPE_VIDEO , 0); - ic->audio_codec_id = find_codec_or_die(audio_codec_name , AVMEDIA_TYPE_AUDIO , 0); - ic->subtitle_codec_id= find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0); + ic->video_codec_id = + find_codec_or_die(video_codec_name , AVMEDIA_TYPE_VIDEO , 0, + avcodec_opts[AVMEDIA_TYPE_VIDEO ]->strict_std_compliance); + ic->audio_codec_id = + find_codec_or_die(audio_codec_name , AVMEDIA_TYPE_AUDIO , 0, + avcodec_opts[AVMEDIA_TYPE_AUDIO ]->strict_std_compliance); + ic->subtitle_codec_id= + find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0, + avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->strict_std_compliance); ic->flags |= AVFMT_FLAG_NONBLOCK; if(pgmyuv_compatibility_hack) @@ -3208,7 +3227,8 @@ static void new_video_stream(AVFormatContext *oc) AVRational fps= frame_rate.num ? frame_rate : (AVRational){25,1}; if (video_codec_name) { - codec_id = find_codec_or_die(video_codec_name, AVMEDIA_TYPE_VIDEO, 1); + codec_id = find_codec_or_die(video_codec_name, AVMEDIA_TYPE_VIDEO, 1, + video_enc->strict_std_compliance); codec = avcodec_find_encoder_by_name(video_codec_name); output_codecs[nb_ocodecs] = codec; } else { @@ -3339,7 +3359,8 @@ static void new_audio_stream(AVFormatContext *oc) set_context_opts(audio_enc, avcodec_opts[AVMEDIA_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM); if (audio_codec_name) { - codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1); + codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1, + audio_enc->strict_std_compliance); codec = avcodec_find_encoder_by_name(audio_codec_name); output_codecs[nb_ocodecs] = codec; } else { @@ -3399,7 +3420,8 @@ static void new_subtitle_stream(AVFormatContext *oc) st->stream_copy = 1; } else { set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM); - subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1); + subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1, + subtitle_enc->strict_std_compliance); output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name); } nb_ocodecs++; diff --git a/ffplay.c b/ffplay.c index e84e290be7..e79e17bd24 100644 --- a/ffplay.c +++ b/ffplay.c @@ -1593,7 +1593,9 @@ static int input_get_buffer(AVCodecContext *codec, AVFrame *pic) unsigned hshift = i == 0 ? 0 : av_pix_fmt_descriptors[ref->pic->format].log2_chroma_w; unsigned vshift = i == 0 ? 0 : av_pix_fmt_descriptors[ref->pic->format].log2_chroma_h; - ref->data[i] += (edge >> hshift) + ((edge * ref->linesize[i]) >> vshift); + if (ref->data[i]) { + ref->data[i] += (edge >> hshift) + ((edge * ref->linesize[i]) >> vshift); + } pic->data[i] = ref->data[i]; pic->linesize[i] = ref->linesize[i]; } diff --git a/ffserver.c b/ffserver.c index e81245d9d3..a1bd5262f0 100644 --- a/ffserver.c +++ b/ffserver.c @@ -847,6 +847,8 @@ static void close_connection(HTTPContext *c) ctx = c->rtp_ctx[i]; if (ctx) { av_write_trailer(ctx); + av_metadata_free(&ctx->metadata); + av_free(ctx->streams[0]); av_free(ctx); } h = c->rtp_handles[i]; @@ -2280,6 +2282,7 @@ static int http_prepare_data(HTTPContext *c) http_log("Error writing output header\n"); return -1; } + av_metadata_free(&c->fmt_ctx.metadata); len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer); c->buffer_ptr = c->pb_buffer; @@ -2343,7 +2346,7 @@ static int http_prepare_data(HTTPContext *c) } } for(i=0;istream->nb_streams;i++) { - if (c->feed_streams[i] == pkt.stream_index) { + if (c->stream->feed_streams[i] == pkt.stream_index) { AVStream *st = c->fmt_in->streams[source_index]; pkt.stream_index = i; if (pkt.flags & AV_PKT_FLAG_KEY && @@ -2879,7 +2882,7 @@ static int rtsp_parse_request(HTTPContext *c) if (*p == '\n') p++; while (*p != '\0') { - p1 = strchr(p, '\n'); + p1 = memchr(p, '\n', (char *)c->buffer_ptr - p); if (!p1) break; p2 = p1; @@ -2946,6 +2949,8 @@ static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer, snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d", inet_ntoa(stream->multicast_ip), stream->multicast_port, stream->multicast_ttl); + } else { + snprintf(avc->filename, 1024, "rtp://0.0.0.0"); } for(i = 0; i < stream->nb_streams; i++) { @@ -2954,6 +2959,7 @@ static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer, } *pbuffer = av_mallocz(2048); avf_sdp_create(&avc, 1, *pbuffer, 2048); + av_metadata_free(&avc->metadata); av_free(avc); return strlen(*pbuffer); @@ -3006,10 +3012,12 @@ static void rtsp_cmd_describe(HTTPContext *c, const char *url) return; } rtsp_reply_header(c, RTSP_STATUS_OK); + url_fprintf(c->pb, "Content-Base: %s/\r\n", url); url_fprintf(c->pb, "Content-Type: application/sdp\r\n"); url_fprintf(c->pb, "Content-Length: %d\r\n", content_length); url_fprintf(c->pb, "\r\n"); put_buffer(c->pb, content, content_length); + av_free(content); } static HTTPContext *find_rtp_session(const char *session_id) @@ -3376,7 +3384,6 @@ static int rtp_new_av_stream(HTTPContext *c, st = av_mallocz(sizeof(AVStream)); if (!st) goto fail; - st->codec= avcodec_alloc_context(); ctx->nb_streams = 1; ctx->streams[0] = st; @@ -3452,16 +3459,28 @@ static int rtp_new_av_stream(HTTPContext *c, /********************************************************************/ /* ffserver initialization */ -static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec) +static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec, int copy) { AVStream *fst; fst = av_mallocz(sizeof(AVStream)); if (!fst) return NULL; - fst->codec= avcodec_alloc_context(); + if (copy) { + fst->codec= avcodec_alloc_context(); + memcpy(fst->codec, codec, sizeof(AVCodecContext)); + if (codec->extradata_size) { + fst->codec->extradata = av_malloc(codec->extradata_size); + memcpy(fst->codec->extradata, codec->extradata, + codec->extradata_size); + } + } else { + /* live streams must use the actual feed's codec since it may be + * updated later to carry extradata needed by the streams. + */ + fst->codec = codec; + } fst->priv_data = av_mallocz(sizeof(FeedData)); - memcpy(fst->codec, codec, sizeof(AVCodecContext)); fst->index = stream->nb_streams; av_set_pts_info(fst, 33, 1, 90000); stream->streams[stream->nb_streams++] = fst; @@ -3503,7 +3522,7 @@ static int add_av_stream(FFStream *feed, AVStream *st) } } - fst = add_av_stream1(feed, av); + fst = add_av_stream1(feed, av, 0); if (!fst) return -1; return feed->nb_streams - 1; @@ -3614,7 +3633,7 @@ static void build_file_streams(void) extract_mpeg4_header(infile); for(i=0;inb_streams;i++) - add_av_stream1(stream, infile->streams[i]->codec); + add_av_stream1(stream, infile->streams[i]->codec, 1); av_close_input_file(infile); } @@ -3681,7 +3700,7 @@ static void build_feed_streams(void) ccs = ss->codec; #define CHECK_CODEC(x) (ccf->x != ccs->x) - if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) { + if (CHECK_CODEC(codec_id) || CHECK_CODEC(codec_type)) { http_log("Codecs do not match for stream %d\n", i); matches = 0; } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) { diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c index 219850302c..f6e50e9d5c 100644 --- a/libavcodec/4xm.c +++ b/libavcodec/4xm.c @@ -641,9 +641,18 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length){ uint16_t *dst= (uint16_t*)f->current_picture.data[0]; const int stride= f->current_picture.linesize[0]>>1; const unsigned int bitstream_size= AV_RL32(buf); - const int token_count av_unused = AV_RL32(buf + bitstream_size + 8); - unsigned int prestream_size= 4*AV_RL32(buf + bitstream_size + 4); - const uint8_t *prestream= buf + bitstream_size + 12; + int token_count av_unused; + unsigned int prestream_size; + const uint8_t *prestream; + + if (length < bitstream_size + 12) { + av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n"); + return AVERROR_INVALIDDATA; + } + + token_count = AV_RL32(buf + bitstream_size + 8); + prestream_size = 4 * AV_RL32(buf + bitstream_size + 4); + prestream = buf + bitstream_size + 12; if(prestream_size + bitstream_size + 12 != length || bitstream_size > (1<<26) diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 1885adeccc..4afce9a243 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -43,7 +43,7 @@ OBJS-$(CONFIG_VAAPI) += vaapi.o OBJS-$(CONFIG_VDPAU) += vdpau.o # decoders/encoders/hardware accelerators -OBJS-$(CONFIG_AAC_DECODER) += aac.o aactab.o aacsbr.o +OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o aacps.o OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \ aacpsy.o aactab.o \ psymodel.o iirfilter.o \ @@ -501,6 +501,8 @@ OBJS-$(CONFIG_OGG_DEMUXER) += flacdec.o flacdata.o flac.o \ dirac.o mpeg12data.o OBJS-$(CONFIG_OGG_MUXER) += xiph.o flacdec.o flacdata.o flac.o OBJS-$(CONFIG_RTP_MUXER) += mpegvideo.o +OBJS-$(CONFIG_WEBM_MUXER) += xiph.o mpeg4audio.o \ + flacdec.o flacdata.o flac.o # external codec libraries OBJS-$(CONFIG_LIBDIRAC_DECODER) += libdiracdec.o @@ -525,6 +527,9 @@ OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER) += libschroedingerenc.o \ OBJS-$(CONFIG_LIBSPEEX_DECODER) += libspeexdec.o OBJS-$(CONFIG_LIBTHEORA_ENCODER) += libtheoraenc.o OBJS-$(CONFIG_LIBVORBIS_ENCODER) += libvorbis.o +OBJS-$(CONFIG_LIBVO_AACENC_ENCODER) += libvo-aacenc.o mpeg4audio.o +OBJS-$(CONFIG_LIBVPX_DECODER) += libvpxdec.o +OBJS-$(CONFIG_LIBVPX_ENCODER) += libvpxenc.o OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvidff.o libxvid_rc.o @@ -641,7 +646,9 @@ $(SUBDIR)%_tables.h: $(SUBDIR)%_tablegen$(HOSTEXESUF) $(M)./$< > $@ ifdef CONFIG_HARDCODED_TABLES -$(SUBDIR)aac.o: $(SUBDIR)cbrt_tables.h +$(SUBDIR)aacdec.o: $(SUBDIR)cbrt_tables.h +$(SUBDIR)aacps.o: $(SUBDIR)aacps_tables.h +$(SUBDIR)aactab.o: $(SUBDIR)aac_tables.h $(SUBDIR)dv.o: $(SUBDIR)dv_tables.h $(SUBDIR)mdct.o: $(SUBDIR)mdct_tables.h $(SUBDIR)mpegaudiodec.o: $(SUBDIR)mpegaudio_tables.h diff --git a/libavcodec/aac.h b/libavcodec/aac.h index 1a8aa6c145..94f578ff38 100644 --- a/libavcodec/aac.h +++ b/libavcodec/aac.h @@ -38,12 +38,6 @@ #include -#define AAC_INIT_VLC_STATIC(num, size) \ - INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \ - ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \ - ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \ - size); - #define MAX_CHANNELS 64 #define MAX_ELEM_ID 16 @@ -241,7 +235,7 @@ typedef struct { * main AAC context */ typedef struct { - AVCodecContext * avccontext; + AVCodecContext *avctx; MPEG4AudioConfig m4ac; @@ -255,8 +249,9 @@ typedef struct { enum ChannelPosition che_pos[4][MAX_ELEM_ID]; /**< channel element channel mapping with the * first index as the first 4 raw data block types */ - ChannelElement * che[4][MAX_ELEM_ID]; - ChannelElement * tag_che_map[4][MAX_ELEM_ID]; + ChannelElement *che[4][MAX_ELEM_ID]; + ChannelElement *tag_che_map[4][MAX_ELEM_ID]; + uint8_t tags_seen_this_frame[4][MAX_ELEM_ID]; int tags_mapped; /** @} */ diff --git a/libavcodec/aac_tablegen.c b/libavcodec/aac_tablegen.c new file mode 100644 index 0000000000..de1c9578a7 --- /dev/null +++ b/libavcodec/aac_tablegen.c @@ -0,0 +1,39 @@ +/* + * Generate a header file for hardcoded AAC tables + * + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#define CONFIG_HARDCODED_TABLES 0 +#include "aac_tablegen.h" +#include "tableprint.h" + +int main(void) +{ + ff_aac_tableinit(); + + write_fileheader(); + + printf("const float ff_aac_pow2sf_tab[428] = {\n"); + write_float_array(ff_aac_pow2sf_tab, 428); + printf("};\n"); + + return 0; +} diff --git a/libavcodec/aac_tablegen.h b/libavcodec/aac_tablegen.h new file mode 100644 index 0000000000..630071dd1d --- /dev/null +++ b/libavcodec/aac_tablegen.h @@ -0,0 +1,42 @@ +/* + * Header file for hardcoded AAC tables + * + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AAC_TABLEGEN_H +#define AAC_TABLEGEN_H + +#include "aac_tablegen_decl.h" + +#if CONFIG_HARDCODED_TABLES +#include "libavcodec/aac_tables.h" +#else +#include "../libavutil/mathematics.h" +float ff_aac_pow2sf_tab[428]; + +void ff_aac_tableinit(void) +{ + int i; + for (i = 0; i < 428; i++) + ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.); +} +#endif /* CONFIG_HARDCODED_TABLES */ + +#endif /* AAC_TABLEGEN_H */ diff --git a/libavcodec/aac_tablegen_decl.h b/libavcodec/aac_tablegen_decl.h new file mode 100644 index 0000000000..7157c80461 --- /dev/null +++ b/libavcodec/aac_tablegen_decl.h @@ -0,0 +1,34 @@ +/* + * Header file for hardcoded AAC tables + * + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AAC_TABLEGEN_INIT_H +#define AAC_TABLEGEN_INIT_H + +#if CONFIG_HARDCODED_TABLES +#define ff_aac_tableinit() +extern const float ff_aac_pow2sf_tab[428]; +#else +void ff_aac_tableinit(void); +extern float ff_aac_pow2sf_tab[428]; +#endif /* CONFIG_HARDCODED_TABLES */ + +#endif /* AAC_TABLEGEN_INIT_H */ diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index e19fa3803f..08c4801821 100644 --- a/libavcodec/aaccoder.c +++ b/libavcodec/aaccoder.c @@ -35,6 +35,7 @@ #include "aac.h" #include "aacenc.h" #include "aactab.h" +#include "libavutil/libm.h" /** bits needed to code codebook run value for long windows */ static const uint8_t run_value_bits_long[64] = { @@ -763,7 +764,7 @@ static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, const float *scaled = s->scoefs + start; const int size = sce->ics.swb_sizes[g]; int scf, prev_scf, step; - int min_scf = 0, max_scf = 255; + int min_scf = -1, max_scf = 256; float curdiff; if (maxq[w*16+g] < 21.544) { sce->zeroes[w*16+g] = 1; @@ -797,21 +798,23 @@ static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, } prev_scf = scf; curdiff = fabsf(dist - uplim[w*16+g]); - if (curdiff == 0.0f) + if (curdiff <= 1.0f) step = 0; else - step = fabsf(log2(curdiff)); + step = log2(curdiff); if (dist > uplim[w*16+g]) step = -step; + scf += step; + scf = av_clip_uint8(scf); + step = scf - prev_scf; if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) { - sce->sf_idx[w*16+g] = scf; + sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf); break; } - scf += step; if (step > 0) - min_scf = scf; + min_scf = prev_scf; else - max_scf = scf; + max_scf = prev_scf; } start += size; } diff --git a/libavcodec/aac.c b/libavcodec/aacdec.c similarity index 92% rename from libavcodec/aac.c rename to libavcodec/aacdec.c index ac8c9c6ec4..e375d1f98b 100644 --- a/libavcodec/aac.c +++ b/libavcodec/aacdec.c @@ -67,7 +67,7 @@ * Y (not in this code) Layer-2 * Y (not in this code) Layer-3 * N SinuSoidal Coding (Transient, Sinusoid, Noise) - * N (planned) Parametric Stereo + * Y Parametric Stereo * N Direct Stream Transfer * * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication. @@ -113,6 +113,22 @@ static const char overread_err[] = "Input buffer exhausted before END element fo static ChannelElement *get_che(AACContext *ac, int type, int elem_id) { + /* Some buggy encoders appear to set all elem_ids to zero and rely on + channels always occurring in the same order. This is expressly forbidden + by the spec but we will try to work around it. + */ + int err_printed = 0; + while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) { + if (ac->output_configured < OC_LOCKED && !err_printed) { + av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n"); + err_printed = 1; + } + elem_id++; + } + if (elem_id == MAX_ELEM_ID) + return NULL; + ac->tags_seen_this_frame[type][elem_id] = 1; + if (ac->tag_che_map[type][elem_id]) { return ac->tag_che_map[type][elem_id]; } @@ -127,8 +143,8 @@ static ChannelElement *get_che(AACContext *ac, int type, int elem_id) } case 6: /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1] - instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have - encountered such a stream, transfer the LFE[0] element to SCE[1] */ + instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have + encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */ if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) { ac->tags_mapped++; return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0]; @@ -184,7 +200,8 @@ static av_cold int che_configure(AACContext *ac, ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr); if (type != TYPE_CCE) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret; - if (type == TYPE_CPE) { + if (type == TYPE_CPE || + (type == TYPE_SCE && ac->m4ac.ps == 1)) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret; } } @@ -209,9 +226,10 @@ static av_cold int output_configure(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], int channel_config, enum OCStatus oc_type) { - AVCodecContext *avctx = ac->avccontext; + AVCodecContext *avctx = ac->avctx; int i, type, channels = 0, ret; + if (new_che_pos != che_pos) memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); if (channel_config) { @@ -292,7 +310,7 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL sampling_index = get_bits(gb, 4); if (ac->m4ac.sampling_index != sampling_index) - av_log(ac->avccontext, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n"); + av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n"); num_front = get_bits(gb, 4); num_side = get_bits(gb, 4); @@ -323,7 +341,7 @@ static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_EL /* comment field, first byte is length */ comment_len = get_bits(gb, 8) * 8; if (get_bits_left(gb) < comment_len) { - av_log(ac->avccontext, AV_LOG_ERROR, overread_err); + av_log(ac->avctx, AV_LOG_ERROR, overread_err); return -1; } skip_bits_long(gb, comment_len); @@ -343,7 +361,7 @@ static av_cold int set_default_channel_config(AACContext *ac, int channel_config) { if (channel_config < 1 || channel_config > 7) { - av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n", + av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n", channel_config); return -1; } @@ -388,7 +406,7 @@ static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb, int extension_flag, ret; if (get_bits1(gb)) { // frameLengthFlag - av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1); + av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1); return -1; } @@ -452,9 +470,11 @@ static int decode_audio_specific_config(AACContext *ac, void *data, if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0) return -1; if (ac->m4ac.sampling_index > 12) { - av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); + av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); return -1; } + if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1) + ac->m4ac.ps = 1; skip_bits_long(&gb, i); @@ -465,7 +485,7 @@ static int decode_audio_specific_config(AACContext *ac, void *data, return -1; break; default: - av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n", + av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n", ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type); return -1; } @@ -508,20 +528,25 @@ static void reset_predictor_group(PredictorState *ps, int group_num) reset_predict_state(&ps[i]); } -static av_cold int aac_decode_init(AVCodecContext *avccontext) +#define AAC_INIT_VLC_STATIC(num, size) \ + INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \ + ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \ + ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \ + size); + +static av_cold int aac_decode_init(AVCodecContext *avctx) { - AACContext *ac = avccontext->priv_data; - int i; + AACContext *ac = avctx->priv_data; - ac->avccontext = avccontext; - ac->m4ac.sample_rate = avccontext->sample_rate; + ac->avctx = avctx; + ac->m4ac.sample_rate = avctx->sample_rate; - if (avccontext->extradata_size > 0) { - if (decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size)) + if (avctx->extradata_size > 0) { + if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size)) return -1; } - avccontext->sample_fmt = SAMPLE_FMT_S16; + avctx->sample_fmt = SAMPLE_FMT_S16; AAC_INIT_VLC_STATIC( 0, 304); AAC_INIT_VLC_STATIC( 1, 270); @@ -537,7 +562,7 @@ static av_cold int aac_decode_init(AVCodecContext *avccontext) ff_aac_sbr_init(); - dsputil_init(&ac->dsp, avccontext); + dsputil_init(&ac->dsp, avctx); ac->random_state = 0x1f2e3d4c; @@ -555,10 +580,7 @@ static av_cold int aac_decode_init(AVCodecContext *avccontext) ac->sf_offset = 60; } -#if !CONFIG_HARDCODED_TABLES - for (i = 0; i < 428; i++) - ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.); -#endif /* CONFIG_HARDCODED_TABLES */ + ff_aac_tableinit(); INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code), ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]), @@ -591,7 +613,7 @@ static int skip_data_stream_element(AACContext *ac, GetBitContext *gb) align_get_bits(gb); if (get_bits_left(gb) < 8 * count) { - av_log(ac->avccontext, AV_LOG_ERROR, overread_err); + av_log(ac->avctx, AV_LOG_ERROR, overread_err); return -1; } skip_bits_long(gb, 8 * count); @@ -605,7 +627,7 @@ static int decode_prediction(AACContext *ac, IndividualChannelStream *ics, if (get_bits1(gb)) { ics->predictor_reset_group = get_bits(gb, 5); if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) { - av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n"); return -1; } } @@ -624,7 +646,7 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, GetBitContext *gb, int common_window) { if (get_bits1(gb)) { - av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n"); memset(ics, 0, sizeof(IndividualChannelStream)); return -1; } @@ -665,11 +687,11 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, return -1; } } else if (ac->m4ac.object_type == AOT_AAC_LC) { - av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n"); memset(ics, 0, sizeof(IndividualChannelStream)); return -1; } else { - av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1); + av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1); memset(ics, 0, sizeof(IndividualChannelStream)); return -1; } @@ -677,7 +699,7 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, } if (ics->max_sfb > ics->num_swb) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Number of scalefactor bands in group (%d) exceeds limit (%d).\n", ics->max_sfb, ics->num_swb); memset(ics, 0, sizeof(IndividualChannelStream)); @@ -708,18 +730,18 @@ static int decode_band_types(AACContext *ac, enum BandType band_type[120], int sect_len_incr; int sect_band_type = get_bits(gb, 4); if (sect_band_type == 12) { - av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n"); + av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n"); return -1; } while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1) sect_end += sect_len_incr; sect_end += sect_len_incr; if (get_bits_left(gb) < 0) { - av_log(ac->avccontext, AV_LOG_ERROR, overread_err); + av_log(ac->avctx, AV_LOG_ERROR, overread_err); return -1; } if (sect_end > ics->max_sfb) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Number of bands (%d) exceeds limit (%d).\n", sect_end, ics->max_sfb); return -1; @@ -764,7 +786,7 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb, for (; i < run_end; i++, idx++) { offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; if (offset[2] > 255U) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "%s (%d) out of range.\n", sf_str[2], offset[2]); return -1; } @@ -777,7 +799,7 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb, else offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; if (offset[1] > 255U) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "%s (%d) out of range.\n", sf_str[1], offset[1]); return -1; } @@ -787,7 +809,7 @@ static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb, for (; i < run_end; i++, idx++) { offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; if (offset[0] > 255U) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "%s (%d) out of range.\n", sf_str[0], offset[0]); return -1; } @@ -844,7 +866,7 @@ static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns, tns->length[w][filt] = get_bits(gb, 6 - 2 * is8); if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) { - av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n", + av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n", tns->order[w][filt], tns_max_order); tns->order[w][filt] = 0; return -1; @@ -1163,7 +1185,7 @@ static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024], b = 31 - av_log2(~b); if (b > 8) { - av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n"); + av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n"); return -1; } @@ -1216,7 +1238,7 @@ static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024], return 0; err_cb_overflow: - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n", band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]); return -1; @@ -1337,18 +1359,18 @@ static int decode_ics(AACContext *ac, SingleChannelElement *sce, if (!scale_flag) { if ((pulse_present = get_bits1(gb))) { if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { - av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n"); return -1; } if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) { - av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n"); return -1; } } if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics)) return -1; if (get_bits1(gb)) { - av_log_missing_feature(ac->avccontext, "SSR", 1); + av_log_missing_feature(ac->avctx, "SSR", 1); return -1; } } @@ -1448,7 +1470,7 @@ static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe) cpe->ch[1].ics.use_kb_window[1] = i; ms_present = get_bits(gb, 2); if (ms_present == 3) { - av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n"); return -1; } else if (ms_present) decode_mid_side_stereo(cpe, gb, ms_present); @@ -1635,16 +1657,20 @@ static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt, crc_flag++; case EXT_SBR_DATA: if (!che) { - av_log(ac->avccontext, AV_LOG_ERROR, "SBR was found before the first channel element.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n"); return res; } else if (!ac->m4ac.sbr) { - av_log(ac->avccontext, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n"); skip_bits_long(gb, 8 * cnt - 4); return res; } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) { - av_log(ac->avccontext, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n"); skip_bits_long(gb, 8 * cnt - 4); return res; + } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) { + ac->m4ac.sbr = 1; + ac->m4ac.ps = 1; + output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured); } else { ac->m4ac.sbr = 1; } @@ -1728,7 +1754,7 @@ static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float // imdct if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) - av_log(ac->avccontext, AV_LOG_WARNING, + av_log(ac->avctx, AV_LOG_WARNING, "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. " "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n"); for (i = 0; i < 1024; i += 128) @@ -1794,7 +1820,7 @@ static void apply_dependent_coupling(AACContext *ac, const float *src = cce->ch[0].coeffs; int g, i, group, k, idx = 0; if (ac->m4ac.object_type == AOT_AAC_LTP) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Dependent coupling is not supported together with LTP\n"); return; } @@ -1924,57 +1950,65 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb) } else if (ac->output_configured != OC_LOCKED) { ac->output_configured = OC_NONE; } - if (ac->output_configured != OC_LOCKED) + if (ac->output_configured != OC_LOCKED) { ac->m4ac.sbr = -1; + ac->m4ac.ps = -1; + } ac->m4ac.sample_rate = hdr_info.sample_rate; ac->m4ac.sampling_index = hdr_info.sampling_index; ac->m4ac.object_type = hdr_info.object_type; - if (!ac->avccontext->sample_rate) - ac->avccontext->sample_rate = hdr_info.sample_rate; + if (!ac->avctx->sample_rate) + ac->avctx->sample_rate = hdr_info.sample_rate; if (hdr_info.num_aac_frames == 1) { if (!hdr_info.crc_absent) skip_bits(gb, 16); } else { - av_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0); + av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0); return -1; } } return size; } -static int aac_decode_frame(AVCodecContext *avccontext, void *data, +static int aac_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; - AACContext *ac = avccontext->priv_data; + AACContext *ac = avctx->priv_data; ChannelElement *che = NULL, *che_prev = NULL; GetBitContext gb; enum RawDataBlockType elem_type, elem_type_prev = TYPE_END; int err, elem_id, data_size_tmp; int buf_consumed; - int samples = 1024, multiplier; + int samples = 0, multiplier; + int buf_offset; init_get_bits(&gb, buf, buf_size * 8); if (show_bits(&gb, 12) == 0xfff) { if (parse_adts_frame_header(ac, &gb) < 0) { - av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n"); + av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n"); return -1; } if (ac->m4ac.sampling_index > 12) { - av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); + av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); return -1; } } + memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame)); // parse while ((elem_type = get_bits(&gb, 3)) != TYPE_END) { elem_id = get_bits(&gb, 4); - if (elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) { - av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id); - return -1; + if (elem_type < TYPE_DSE) { + if (!(che=get_che(ac, elem_type, elem_id))) { + av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", + elem_type, elem_id); + return -1; + } + samples = 1024; } switch (elem_type) { @@ -2005,7 +2039,7 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, if ((err = decode_pce(ac, new_che_pos, &gb))) break; if (ac->output_configured > OC_TRIAL_PCE) - av_log(avccontext, AV_LOG_ERROR, + av_log(avctx, AV_LOG_ERROR, "Not evaluating a further program_config_element as this construct is dubious at best.\n"); else err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE); @@ -2016,7 +2050,7 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, if (elem_id == 15) elem_id += get_bits(&gb, 8) - 1; if (get_bits_left(&gb) < 8 * elem_id) { - av_log(avccontext, AV_LOG_ERROR, overread_err); + av_log(avctx, AV_LOG_ERROR, overread_err); return -1; } while (elem_id > 0) @@ -2036,7 +2070,7 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, return err; if (get_bits_left(&gb) < 3) { - av_log(avccontext, AV_LOG_ERROR, overread_err); + av_log(avctx, AV_LOG_ERROR, overread_err); return -1; } } @@ -2046,31 +2080,36 @@ static int aac_decode_frame(AVCodecContext *avccontext, void *data, multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0; samples <<= multiplier; if (ac->output_configured < OC_LOCKED) { - avccontext->sample_rate = ac->m4ac.sample_rate << multiplier; - avccontext->frame_size = samples; + avctx->sample_rate = ac->m4ac.sample_rate << multiplier; + avctx->frame_size = samples; } - data_size_tmp = samples * avccontext->channels * sizeof(int16_t); + data_size_tmp = samples * avctx->channels * sizeof(int16_t); if (*data_size < data_size_tmp) { - av_log(avccontext, AV_LOG_ERROR, + av_log(avctx, AV_LOG_ERROR, "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n", *data_size, data_size_tmp); return -1; } *data_size = data_size_tmp; - ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avccontext->channels); + if (samples) + ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels); if (ac->output_configured) ac->output_configured = OC_LOCKED; buf_consumed = (get_bits_count(&gb) + 7) >> 3; - return buf_size > buf_consumed ? buf_consumed : buf_size; + for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++) + if (buf[buf_offset]) + break; + + return buf_size > buf_offset ? buf_consumed : buf_size; } -static av_cold int aac_decode_close(AVCodecContext *avccontext) +static av_cold int aac_decode_close(AVCodecContext *avctx) { - AACContext *ac = avccontext->priv_data; + AACContext *ac = avctx->priv_data; int i, type; for (i = 0; i < MAX_ELEM_ID; i++) { diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index ff68f84e7b..8c913e0dbc 100644 --- a/libavcodec/aacenc.c +++ b/libavcodec/aacenc.c @@ -174,6 +174,10 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", avctx->profile); return -1; } + if (1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * avctx->channels) { + av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n"); + return -1; + } s->samplerate_index = i; dsputil_init(&s->dsp, avctx); @@ -197,13 +201,11 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) lengths[1] = ff_aac_num_swb_128[i]; ff_psy_init(&s->psy, avctx, 2, sizes, lengths); s->psypp = ff_psy_preprocess_init(avctx); - s->coder = &ff_aac_coders[0]; + s->coder = &ff_aac_coders[2]; s->lambda = avctx->global_quality ? avctx->global_quality : 120; -#if !CONFIG_HARDCODED_TABLES - for (i = 0; i < 428; i++) - ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.); -#endif /* CONFIG_HARDCODED_TABLES */ + + ff_aac_tableinit(); if (avctx->channels > 5) av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. " @@ -230,25 +232,21 @@ static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s, s->output[i] = sce->saved[i]; } if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) { - j = channel; - for (i = 0; i < 1024; i++, j += avctx->channels) { + for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) { s->output[i+1024] = audio[j] * lwindow[1024 - i - 1]; sce->saved[i] = audio[j] * lwindow[i]; } } else { - j = channel; - for (i = 0; i < 448; i++, j += avctx->channels) + for (i = 0, j = channel; i < 448; i++, j += avctx->channels) s->output[i+1024] = audio[j]; - for (i = 448; i < 576; i++, j += avctx->channels) + for (; i < 576; i++, j += avctx->channels) s->output[i+1024] = audio[j] * swindow[576 - i - 1]; memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448); - j = channel; - for (i = 0; i < 1024; i++, j += avctx->channels) + for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) sce->saved[i] = audio[j]; } ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output); } else { - j = channel; for (k = 0; k < 1024; k += 128) { for (i = 448 + k; i < 448 + k + 256; i++) s->output[i - 448 - k] = (i < 1024) @@ -258,8 +256,7 @@ static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s, s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128); ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output); } - j = channel; - for (i = 0; i < 1024; i++, j += avctx->channels) + for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) sce->saved[i] = audio[j]; } } @@ -557,6 +554,8 @@ static int aac_encode_frame(AVCodecContext *avctx, chans = tag == TYPE_CPE ? 2 : 1; cpe = &s->cpe[i]; for (j = 0; j < chans; j++) { + s->cur_channel = start_ch + j; + ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]); s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda); } cpe->common_window = 0; @@ -572,6 +571,7 @@ static int aac_encode_frame(AVCodecContext *avctx, } } } + s->cur_channel = start_ch; if (cpe->common_window && s->coder->search_for_ms) s->coder->search_for_ms(s, cpe, s->lambda); adjust_frame_information(s, cpe, chans); @@ -586,7 +586,6 @@ static int aac_encode_frame(AVCodecContext *avctx, } for (j = 0; j < chans; j++) { s->cur_channel = start_ch + j; - ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]); encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window); } start_ch += chans; @@ -639,7 +638,7 @@ AVCodec aac_encoder = { aac_encode_init, aac_encode_frame, aac_encode_end, - .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY, + .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), }; diff --git a/libavcodec/aacenc.h b/libavcodec/aacenc.h index e99be98048..86c68d3e16 100644 --- a/libavcodec/aacenc.h +++ b/libavcodec/aacenc.h @@ -64,7 +64,7 @@ typedef struct AACEncContext { int cur_channel; int last_frame; float lambda; - DECLARE_ALIGNED(16, int, qcoefs)[96][2]; ///< quantized coefficients + DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients DECLARE_ALIGNED(16, float, scoefs)[1024]; ///< scaled coefficients } AACEncContext; diff --git a/libavcodec/aacps.c b/libavcodec/aacps.c new file mode 100644 index 0000000000..3cfd18d959 --- /dev/null +++ b/libavcodec/aacps.c @@ -0,0 +1,1040 @@ +/* + * MPEG-4 Parametric Stereo decoding functions + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "libavutil/common.h" +#include "libavutil/mathematics.h" +#include "avcodec.h" +#include "get_bits.h" +#include "aacps.h" +#include "aacps_tablegen.h" +#include "aacpsdata.c" + +#define PS_BASELINE 0 //< Operate in Baseline PS mode + //< Baseline implies 10 or 20 stereo bands, + //< mixing mode A, and no ipd/opd + +#define numQMFSlots 32 //numTimeSlots * RATE + +static const int8_t num_env_tab[2][4] = { + { 0, 1, 2, 4, }, + { 1, 2, 3, 4, }, +}; + +static const int8_t nr_iidicc_par_tab[] = { + 10, 20, 34, 10, 20, 34, +}; + +static const int8_t nr_iidopd_par_tab[] = { + 5, 11, 17, 5, 11, 17, +}; + +enum { + huff_iid_df1, + huff_iid_dt1, + huff_iid_df0, + huff_iid_dt0, + huff_icc_df, + huff_icc_dt, + huff_ipd_df, + huff_ipd_dt, + huff_opd_df, + huff_opd_dt, +}; + +static const int huff_iid[] = { + huff_iid_df0, + huff_iid_df1, + huff_iid_dt0, + huff_iid_dt1, +}; + +static VLC vlc_ps[10]; + +/** + * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ + * Inter-channel Phase Difference/Overall Phase Difference parameters from the + * bitstream. + * + * @param avctx contains the current codec context + * @param gb pointer to the input bitstream + * @param ps pointer to the Parametric Stereo context + * @param par pointer to the parameter to be read + * @param e envelope to decode + * @param dt 1: time delta-coded, 0: frequency delta-coded + */ +#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \ +static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \ + int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \ +{ \ + int b, num = ps->nr_ ## PAR ## _par; \ + VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \ + if (dt) { \ + int e_prev = e ? e - 1 : ps->num_env_old - 1; \ + e_prev = FFMAX(e_prev, 0); \ + for (b = 0; b < num; b++) { \ + int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \ + if (MASK) val &= MASK; \ + PAR[e][b] = val; \ + if (ERR_CONDITION) \ + goto err; \ + } \ + } else { \ + int val = 0; \ + for (b = 0; b < num; b++) { \ + val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \ + if (MASK) val &= MASK; \ + PAR[e][b] = val; \ + if (ERR_CONDITION) \ + goto err; \ + } \ + } \ + return 0; \ +err: \ + av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \ + return -1; \ +} + +READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant) +READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U) +READ_PAR_DATA(ipdopd, 0, 0x07, 0) + +static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id) +{ + int e; + int count = get_bits_count(gb); + + if (ps_extension_id) + return 0; + + ps->enable_ipdopd = get_bits1(gb); + if (ps->enable_ipdopd) { + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt); + dt = get_bits1(gb); + read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt); + } + } + skip_bits1(gb); //reserved_ps + return get_bits_count(gb) - count; +} + +static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist) +{ + int i; + for (i = 0; i < PS_MAX_NR_IPDOPD; i++) { + opd_hist[i] = 0; + ipd_hist[i] = 0; + } +} + +int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left) +{ + int e; + int bit_count_start = get_bits_count(gb_host); + int header; + int bits_consumed; + GetBitContext gbc = *gb_host, *gb = &gbc; + + header = get_bits1(gb); + if (header) { //enable_ps_header + ps->enable_iid = get_bits1(gb); + if (ps->enable_iid) { + int iid_mode = get_bits(gb, 3); + if (iid_mode > 5) { + av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n", + iid_mode); + goto err; + } + ps->nr_iid_par = nr_iidicc_par_tab[iid_mode]; + ps->iid_quant = iid_mode > 2; + ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode]; + } + ps->enable_icc = get_bits1(gb); + if (ps->enable_icc) { + ps->icc_mode = get_bits(gb, 3); + if (ps->icc_mode > 5) { + av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n", + ps->icc_mode); + goto err; + } + ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode]; + } + ps->enable_ext = get_bits1(gb); + } + + ps->frame_class = get_bits1(gb); + ps->num_env_old = ps->num_env; + ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)]; + + ps->border_position[0] = -1; + if (ps->frame_class) { + for (e = 1; e <= ps->num_env; e++) + ps->border_position[e] = get_bits(gb, 5); + } else + for (e = 1; e <= ps->num_env; e++) + ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1; + + if (ps->enable_iid) { + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt)) + goto err; + } + } else + memset(ps->iid_par, 0, sizeof(ps->iid_par)); + + if (ps->enable_icc) + for (e = 0; e < ps->num_env; e++) { + int dt = get_bits1(gb); + if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt)) + goto err; + } + else + memset(ps->icc_par, 0, sizeof(ps->icc_par)); + + if (ps->enable_ext) { + int cnt = get_bits(gb, 4); + if (cnt == 15) { + cnt += get_bits(gb, 8); + } + cnt *= 8; + while (cnt > 7) { + int ps_extension_id = get_bits(gb, 2); + cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id); + } + if (cnt < 0) { + av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d", cnt); + goto err; + } + skip_bits(gb, cnt); + } + + ps->enable_ipdopd &= !PS_BASELINE; + + //Fix up envelopes + if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) { + //Create a fake envelope + int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1; + if (source >= 0 && source != ps->num_env) { + if (ps->enable_iid) { + memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0])); + } + if (ps->enable_icc) { + memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0])); + } + if (ps->enable_ipdopd) { + memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0])); + memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0])); + } + } + ps->num_env++; + ps->border_position[ps->num_env] = numQMFSlots - 1; + } + + + ps->is34bands_old = ps->is34bands; + if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc)) + ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) || + (ps->enable_icc && ps->nr_icc_par == 34); + + //Baseline + if (!ps->enable_ipdopd) { + memset(ps->ipd_par, 0, sizeof(ps->ipd_par)); + memset(ps->opd_par, 0, sizeof(ps->opd_par)); + } + + if (header) + ps->start = 1; + + bits_consumed = get_bits_count(gb) - bit_count_start; + if (bits_consumed <= bits_left) { + skip_bits_long(gb_host, bits_consumed); + return bits_consumed; + } + av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed); +err: + ps->start = 0; + skip_bits_long(gb_host, bits_left); + return bits_left; +} + +/** Split one subband into 2 subsubbands with a symmetric real filter. + * The filter must have its non-center even coefficients equal to zero. */ +static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse) +{ + int i, j; + for (i = 0; i < len; i++, in++) { + float re_in = filter[6] * in[6][0]; //real inphase + float re_op = 0.0f; //real out of phase + float im_in = filter[6] * in[6][1]; //imag inphase + float im_op = 0.0f; //imag out of phase + for (j = 0; j < 6; j += 2) { + re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]); + im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]); + } + out[ reverse][i][0] = re_in + re_op; + out[ reverse][i][1] = im_in + im_op; + out[!reverse][i][0] = re_in - re_op; + out[!reverse][i][1] = im_in - im_op; + } +} + +/** Split one subband into 6 subsubbands with a complex filter */ +static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len) +{ + int i, j, ssb; + int N = 8; + float temp[8][2]; + + for (i = 0; i < len; i++, in++) { + for (ssb = 0; ssb < N; ssb++) { + float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1]; + for (j = 0; j < 6; j++) { + float in0_re = in[j][0]; + float in0_im = in[j][1]; + float in1_re = in[12-j][0]; + float in1_im = in[12-j][1]; + sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im); + sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re); + } + temp[ssb][0] = sum_re; + temp[ssb][1] = sum_im; + } + out[0][i][0] = temp[6][0]; + out[0][i][1] = temp[6][1]; + out[1][i][0] = temp[7][0]; + out[1][i][1] = temp[7][1]; + out[2][i][0] = temp[0][0]; + out[2][i][1] = temp[0][1]; + out[3][i][0] = temp[1][0]; + out[3][i][1] = temp[1][1]; + out[4][i][0] = temp[2][0] + temp[5][0]; + out[4][i][1] = temp[2][1] + temp[5][1]; + out[5][i][0] = temp[3][0] + temp[4][0]; + out[5][i][1] = temp[3][1] + temp[4][1]; + } +} + +static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len) +{ + int i, j, ssb; + + for (i = 0; i < len; i++, in++) { + for (ssb = 0; ssb < N; ssb++) { + float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1]; + for (j = 0; j < 6; j++) { + float in0_re = in[j][0]; + float in0_im = in[j][1]; + float in1_re = in[12-j][0]; + float in1_im = in[12-j][1]; + sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im); + sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re); + } + out[ssb][i][0] = sum_re; + out[ssb][i][1] = sum_im; + } + } +} + +static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len) +{ + int i, j; + for (i = 0; i < 5; i++) { + for (j = 0; j < 38; j++) { + in[i][j+6][0] = L[0][j][i]; + in[i][j+6][1] = L[1][j][i]; + } + } + if (is34) { + hybrid4_8_12_cx(in[0], out, f34_0_12, 12, len); + hybrid4_8_12_cx(in[1], out+12, f34_1_8, 8, len); + hybrid4_8_12_cx(in[2], out+20, f34_2_4, 4, len); + hybrid4_8_12_cx(in[3], out+24, f34_2_4, 4, len); + hybrid4_8_12_cx(in[4], out+28, f34_2_4, 4, len); + for (i = 0; i < 59; i++) { + for (j = 0; j < len; j++) { + out[i+32][j][0] = L[0][j][i+5]; + out[i+32][j][1] = L[1][j][i+5]; + } + } + } else { + hybrid6_cx(in[0], out, f20_0_8, len); + hybrid2_re(in[1], out+6, g1_Q2, len, 1); + hybrid2_re(in[2], out+8, g1_Q2, len, 0); + for (i = 0; i < 61; i++) { + for (j = 0; j < len; j++) { + out[i+10][j][0] = L[0][j][i+3]; + out[i+10][j][1] = L[1][j][i+3]; + } + } + } + //update in_buf + for (i = 0; i < 5; i++) { + memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0])); + } +} + +static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len) +{ + int i, n; + if (is34) { + for (n = 0; n < len; n++) { + memset(out[0][n], 0, 5*sizeof(out[0][n][0])); + memset(out[1][n], 0, 5*sizeof(out[1][n][0])); + for (i = 0; i < 12; i++) { + out[0][n][0] += in[ i][n][0]; + out[1][n][0] += in[ i][n][1]; + } + for (i = 0; i < 8; i++) { + out[0][n][1] += in[12+i][n][0]; + out[1][n][1] += in[12+i][n][1]; + } + for (i = 0; i < 4; i++) { + out[0][n][2] += in[20+i][n][0]; + out[1][n][2] += in[20+i][n][1]; + out[0][n][3] += in[24+i][n][0]; + out[1][n][3] += in[24+i][n][1]; + out[0][n][4] += in[28+i][n][0]; + out[1][n][4] += in[28+i][n][1]; + } + } + for (i = 0; i < 59; i++) { + for (n = 0; n < len; n++) { + out[0][n][i+5] = in[i+32][n][0]; + out[1][n][i+5] = in[i+32][n][1]; + } + } + } else { + for (n = 0; n < len; n++) { + out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] + + in[3][n][0] + in[4][n][0] + in[5][n][0]; + out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] + + in[3][n][1] + in[4][n][1] + in[5][n][1]; + out[0][n][1] = in[6][n][0] + in[7][n][0]; + out[1][n][1] = in[6][n][1] + in[7][n][1]; + out[0][n][2] = in[8][n][0] + in[9][n][0]; + out[1][n][2] = in[8][n][1] + in[9][n][1]; + } + for (i = 0; i < 61; i++) { + for (n = 0; n < len; n++) { + out[0][n][i+3] = in[i+10][n][0]; + out[1][n][i+3] = in[i+10][n][1]; + } + } + } +} + +/// All-pass filter decay slope +#define DECAY_SLOPE 0.05f +/// Number of frequency bands that can be addressed by the parameter index, b(k) +static const int NR_PAR_BANDS[] = { 20, 34 }; +/// Number of frequency bands that can be addressed by the sub subband index, k +static const int NR_BANDS[] = { 71, 91 }; +/// Start frequency band for the all-pass filter decay slope +static const int DECAY_CUTOFF[] = { 10, 32 }; +/// Number of all-pass filer bands +static const int NR_ALLPASS_BANDS[] = { 30, 50 }; +/// First stereo band using the short one sample delay +static const int SHORT_DELAY_BAND[] = { 42, 62 }; + +/** Table 8.46 */ +static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full) +{ + int b; + if (full) + b = 9; + else { + b = 4; + par_mapped[10] = 0; + } + for (; b >= 0; b--) { + par_mapped[2*b+1] = par_mapped[2*b] = par[b]; + } +} + +static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full) +{ + par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3; + par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3; + par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3; + par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3; + par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2; + par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2; + par_mapped[ 6] = par[10]; + par_mapped[ 7] = par[11]; + par_mapped[ 8] = ( par[12] + par[13]) / 2; + par_mapped[ 9] = ( par[14] + par[15]) / 2; + par_mapped[10] = par[16]; + if (full) { + par_mapped[11] = par[17]; + par_mapped[12] = par[18]; + par_mapped[13] = par[19]; + par_mapped[14] = ( par[20] + par[21]) / 2; + par_mapped[15] = ( par[22] + par[23]) / 2; + par_mapped[16] = ( par[24] + par[25]) / 2; + par_mapped[17] = ( par[26] + par[27]) / 2; + par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4; + par_mapped[19] = ( par[32] + par[33]) / 2; + } +} + +static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC]) +{ + par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f; + par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f; + par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f; + par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f; + par[ 4] = ( par[ 6] + par[ 7]) * 0.5f; + par[ 5] = ( par[ 8] + par[ 9]) * 0.5f; + par[ 6] = par[10]; + par[ 7] = par[11]; + par[ 8] = ( par[12] + par[13]) * 0.5f; + par[ 9] = ( par[14] + par[15]) * 0.5f; + par[10] = par[16]; + par[11] = par[17]; + par[12] = par[18]; + par[13] = par[19]; + par[14] = ( par[20] + par[21]) * 0.5f; + par[15] = ( par[22] + par[23]) * 0.5f; + par[16] = ( par[24] + par[25]) * 0.5f; + par[17] = ( par[26] + par[27]) * 0.5f; + par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f; + par[19] = ( par[32] + par[33]) * 0.5f; +} + +static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full) +{ + if (full) { + par_mapped[33] = par[9]; + par_mapped[32] = par[9]; + par_mapped[31] = par[9]; + par_mapped[30] = par[9]; + par_mapped[29] = par[9]; + par_mapped[28] = par[9]; + par_mapped[27] = par[8]; + par_mapped[26] = par[8]; + par_mapped[25] = par[8]; + par_mapped[24] = par[8]; + par_mapped[23] = par[7]; + par_mapped[22] = par[7]; + par_mapped[21] = par[7]; + par_mapped[20] = par[7]; + par_mapped[19] = par[6]; + par_mapped[18] = par[6]; + par_mapped[17] = par[5]; + par_mapped[16] = par[5]; + } else { + par_mapped[16] = 0; + } + par_mapped[15] = par[4]; + par_mapped[14] = par[4]; + par_mapped[13] = par[4]; + par_mapped[12] = par[4]; + par_mapped[11] = par[3]; + par_mapped[10] = par[3]; + par_mapped[ 9] = par[2]; + par_mapped[ 8] = par[2]; + par_mapped[ 7] = par[2]; + par_mapped[ 6] = par[2]; + par_mapped[ 5] = par[1]; + par_mapped[ 4] = par[1]; + par_mapped[ 3] = par[1]; + par_mapped[ 2] = par[0]; + par_mapped[ 1] = par[0]; + par_mapped[ 0] = par[0]; +} + +static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full) +{ + if (full) { + par_mapped[33] = par[19]; + par_mapped[32] = par[19]; + par_mapped[31] = par[18]; + par_mapped[30] = par[18]; + par_mapped[29] = par[18]; + par_mapped[28] = par[18]; + par_mapped[27] = par[17]; + par_mapped[26] = par[17]; + par_mapped[25] = par[16]; + par_mapped[24] = par[16]; + par_mapped[23] = par[15]; + par_mapped[22] = par[15]; + par_mapped[21] = par[14]; + par_mapped[20] = par[14]; + par_mapped[19] = par[13]; + par_mapped[18] = par[12]; + par_mapped[17] = par[11]; + } + par_mapped[16] = par[10]; + par_mapped[15] = par[ 9]; + par_mapped[14] = par[ 9]; + par_mapped[13] = par[ 8]; + par_mapped[12] = par[ 8]; + par_mapped[11] = par[ 7]; + par_mapped[10] = par[ 6]; + par_mapped[ 9] = par[ 5]; + par_mapped[ 8] = par[ 5]; + par_mapped[ 7] = par[ 4]; + par_mapped[ 6] = par[ 4]; + par_mapped[ 5] = par[ 3]; + par_mapped[ 4] = (par[ 2] + par[ 3]) / 2; + par_mapped[ 3] = par[ 2]; + par_mapped[ 2] = par[ 1]; + par_mapped[ 1] = (par[ 0] + par[ 1]) / 2; + par_mapped[ 0] = par[ 0]; +} + +static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC]) +{ + par[33] = par[19]; + par[32] = par[19]; + par[31] = par[18]; + par[30] = par[18]; + par[29] = par[18]; + par[28] = par[18]; + par[27] = par[17]; + par[26] = par[17]; + par[25] = par[16]; + par[24] = par[16]; + par[23] = par[15]; + par[22] = par[15]; + par[21] = par[14]; + par[20] = par[14]; + par[19] = par[13]; + par[18] = par[12]; + par[17] = par[11]; + par[16] = par[10]; + par[15] = par[ 9]; + par[14] = par[ 9]; + par[13] = par[ 8]; + par[12] = par[ 8]; + par[11] = par[ 7]; + par[10] = par[ 6]; + par[ 9] = par[ 5]; + par[ 8] = par[ 5]; + par[ 7] = par[ 4]; + par[ 6] = par[ 4]; + par[ 5] = par[ 3]; + par[ 4] = (par[ 2] + par[ 3]) * 0.5f; + par[ 3] = par[ 2]; + par[ 2] = par[ 1]; + par[ 1] = (par[ 0] + par[ 1]) * 0.5f; + par[ 0] = par[ 0]; +} + +static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34) +{ + float power[34][PS_QMF_TIME_SLOTS] = {{0}}; + float transient_gain[34][PS_QMF_TIME_SLOTS]; + float *peak_decay_nrg = ps->peak_decay_nrg; + float *power_smooth = ps->power_smooth; + float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth; + float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay; + float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay; + const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20; + const float peak_decay_factor = 0.76592833836465f; + const float transient_impact = 1.5f; + const float a_smooth = 0.25f; //< Smoothing coefficient + int i, k, m, n; + int n0 = 0, nL = 32; + static const int link_delay[] = { 3, 4, 5 }; + static const float a[] = { 0.65143905753106f, + 0.56471812200776f, + 0.48954165955695f }; + + if (is34 != ps->is34bands_old) { + memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg)); + memset(ps->power_smooth, 0, sizeof(ps->power_smooth)); + memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth)); + memset(ps->delay, 0, sizeof(ps->delay)); + memset(ps->ap_delay, 0, sizeof(ps->ap_delay)); + } + + for (n = n0; n < nL; n++) { + for (k = 0; k < NR_BANDS[is34]; k++) { + int i = k_to_i[k]; + power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1]; + } + } + + //Transient detection + for (i = 0; i < NR_PAR_BANDS[is34]; i++) { + for (n = n0; n < nL; n++) { + float decayed_peak = peak_decay_factor * peak_decay_nrg[i]; + float denom; + peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]); + power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]); + peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]); + denom = transient_impact * peak_decay_diff_smooth[i]; + transient_gain[i][n] = (denom > power_smooth[i]) ? + power_smooth[i] / denom : 1.0f; + } + } + + //Decorrelation and transient reduction + // PS_AP_LINKS - 1 + // ----- + // | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k] + //H[k][z] = z^-2 * phi_fract[k] * | | ---------------------------------------------------------------- + // | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m] + // m = 0 + //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z] + for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) { + int b = k_to_i[k]; + float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]); + float ag[PS_AP_LINKS]; + g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f); + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (m = 0; m < PS_AP_LINKS; m++) { + memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0])); + ag[m] = a[m] * g_decay_slope; + } + for (n = n0; n < nL; n++) { + float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] - + delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1]; + float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] + + delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0]; + for (m = 0; m < PS_AP_LINKS; m++) { + float a_re = ag[m] * in_re; + float a_im = ag[m] * in_im; + float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0]; + float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1]; + float fractional_delay_re = Q_fract_allpass[is34][k][m][0]; + float fractional_delay_im = Q_fract_allpass[is34][k][m][1]; + ap_delay[k][m][n+5][0] = in_re; + ap_delay[k][m][n+5][1] = in_im; + in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re; + in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im; + ap_delay[k][m][n+5][0] += ag[m] * in_re; + ap_delay[k][m][n+5][1] += ag[m] * in_im; + } + out[k][n][0] = transient_gain[b][n] * in_re; + out[k][n][1] = transient_gain[b][n] * in_im; + } + } + for (; k < SHORT_DELAY_BAND[is34]; k++) { + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (n = n0; n < nL; n++) { + //H = delay 14 + out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0]; + out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1]; + } + } + for (; k < NR_BANDS[is34]; k++) { + memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0])); + memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0])); + for (n = n0; n < nL; n++) { + //H = delay 1 + out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0]; + out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1]; + } + } +} + +static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC], + int8_t (*par)[PS_MAX_NR_IIDICC], + int num_par, int num_env, int full) +{ + int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped; + int e; + if (num_par == 20 || num_par == 11) { + for (e = 0; e < num_env; e++) { + map_idx_20_to_34(par_mapped[e], par[e], full); + } + } else if (num_par == 10 || num_par == 5) { + for (e = 0; e < num_env; e++) { + map_idx_10_to_34(par_mapped[e], par[e], full); + } + } else { + *p_par_mapped = par; + } +} + +static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC], + int8_t (*par)[PS_MAX_NR_IIDICC], + int num_par, int num_env, int full) +{ + int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped; + int e; + if (num_par == 34 || num_par == 17) { + for (e = 0; e < num_env; e++) { + map_idx_34_to_20(par_mapped[e], par[e], full); + } + } else if (num_par == 10 || num_par == 5) { + for (e = 0; e < num_env; e++) { + map_idx_10_to_20(par_mapped[e], par[e], full); + } + } else { + *p_par_mapped = par; + } +} + +static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34) +{ + int e, b, k, n; + + float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11; + float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12; + float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21; + float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22; + int8_t *opd_hist = ps->opd_hist; + int8_t *ipd_hist = ps->ipd_hist; + int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; + int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf; + int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf; + int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf; + int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf; + const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20; + const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB; + + //Remapping + if (ps->num_env_old) { + memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0])); + memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0])); + memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0])); + memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0])); + memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0])); + memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0])); + memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0])); + memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0])); + } + + if (is34) { + remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1); + remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1); + if (ps->enable_ipdopd) { + remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0); + remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0); + } + if (!ps->is34bands_old) { + map_val_20_to_34(H11[0][0]); + map_val_20_to_34(H11[1][0]); + map_val_20_to_34(H12[0][0]); + map_val_20_to_34(H12[1][0]); + map_val_20_to_34(H21[0][0]); + map_val_20_to_34(H21[1][0]); + map_val_20_to_34(H22[0][0]); + map_val_20_to_34(H22[1][0]); + ipdopd_reset(ipd_hist, opd_hist); + } + } else { + remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1); + remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1); + if (ps->enable_ipdopd) { + remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0); + remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0); + } + if (ps->is34bands_old) { + map_val_34_to_20(H11[0][0]); + map_val_34_to_20(H11[1][0]); + map_val_34_to_20(H12[0][0]); + map_val_34_to_20(H12[1][0]); + map_val_34_to_20(H21[0][0]); + map_val_34_to_20(H21[1][0]); + map_val_34_to_20(H22[0][0]); + map_val_34_to_20(H22[1][0]); + ipdopd_reset(ipd_hist, opd_hist); + } + } + + //Mixing + for (e = 0; e < ps->num_env; e++) { + for (b = 0; b < NR_PAR_BANDS[is34]; b++) { + float h11, h12, h21, h22; + h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0]; + h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1]; + h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2]; + h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3]; + if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) { + //The spec say says to only run this smoother when enable_ipdopd + //is set but the reference decoder appears to run it constantly + float h11i, h12i, h21i, h22i; + float ipd_adj_re, ipd_adj_im; + int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b]; + int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b]; + float opd_re = pd_re_smooth[opd_idx]; + float opd_im = pd_im_smooth[opd_idx]; + float ipd_re = pd_re_smooth[ipd_idx]; + float ipd_im = pd_im_smooth[ipd_idx]; + opd_hist[b] = opd_idx & 0x3F; + ipd_hist[b] = ipd_idx & 0x3F; + + ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im; + ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im; + h11i = h11 * opd_im; + h11 = h11 * opd_re; + h12i = h12 * ipd_adj_im; + h12 = h12 * ipd_adj_re; + h21i = h21 * opd_im; + h21 = h21 * opd_re; + h22i = h22 * ipd_adj_im; + h22 = h22 * ipd_adj_re; + H11[1][e+1][b] = h11i; + H12[1][e+1][b] = h12i; + H21[1][e+1][b] = h21i; + H22[1][e+1][b] = h22i; + } + H11[0][e+1][b] = h11; + H12[0][e+1][b] = h12; + H21[0][e+1][b] = h21; + H22[0][e+1][b] = h22; + } + for (k = 0; k < NR_BANDS[is34]; k++) { + float h11r, h12r, h21r, h22r; + float h11i, h12i, h21i, h22i; + float h11r_step, h12r_step, h21r_step, h22r_step; + float h11i_step, h12i_step, h21i_step, h22i_step; + int start = ps->border_position[e]; + int stop = ps->border_position[e+1]; + float width = 1.f / (stop - start); + b = k_to_i[k]; + h11r = H11[0][e][b]; + h12r = H12[0][e][b]; + h21r = H21[0][e][b]; + h22r = H22[0][e][b]; + if (!PS_BASELINE && ps->enable_ipdopd) { + //Is this necessary? ps_04_new seems unchanged + if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) { + h11i = -H11[1][e][b]; + h12i = -H12[1][e][b]; + h21i = -H21[1][e][b]; + h22i = -H22[1][e][b]; + } else { + h11i = H11[1][e][b]; + h12i = H12[1][e][b]; + h21i = H21[1][e][b]; + h22i = H22[1][e][b]; + } + } + //Interpolation + h11r_step = (H11[0][e+1][b] - h11r) * width; + h12r_step = (H12[0][e+1][b] - h12r) * width; + h21r_step = (H21[0][e+1][b] - h21r) * width; + h22r_step = (H22[0][e+1][b] - h22r) * width; + if (!PS_BASELINE && ps->enable_ipdopd) { + h11i_step = (H11[1][e+1][b] - h11i) * width; + h12i_step = (H12[1][e+1][b] - h12i) * width; + h21i_step = (H21[1][e+1][b] - h21i) * width; + h22i_step = (H22[1][e+1][b] - h22i) * width; + } + for (n = start + 1; n <= stop; n++) { + //l is s, r is d + float l_re = l[k][n][0]; + float l_im = l[k][n][1]; + float r_re = r[k][n][0]; + float r_im = r[k][n][1]; + h11r += h11r_step; + h12r += h12r_step; + h21r += h21r_step; + h22r += h22r_step; + if (!PS_BASELINE && ps->enable_ipdopd) { + h11i += h11i_step; + h12i += h12i_step; + h21i += h21i_step; + h22i += h22i_step; + + l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im; + l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re; + r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im; + r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re; + } else { + l[k][n][0] = h11r*l_re + h21r*r_re; + l[k][n][1] = h11r*l_im + h21r*r_im; + r[k][n][0] = h12r*l_re + h22r*r_re; + r[k][n][1] = h12r*l_im + h22r*r_im; + } + } + } + } +} + +int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top) +{ + float Lbuf[91][32][2]; + float Rbuf[91][32][2]; + const int len = 32; + int is34 = ps->is34bands; + + top += NR_BANDS[is34] - 64; + memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0])); + if (top < NR_ALLPASS_BANDS[is34]) + memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0])); + + hybrid_analysis(Lbuf, ps->in_buf, L, is34, len); + decorrelation(ps, Rbuf, Lbuf, is34); + stereo_processing(ps, Lbuf, Rbuf, is34); + hybrid_synthesis(L, Lbuf, is34, len); + hybrid_synthesis(R, Rbuf, is34, len); + + return 0; +} + +#define PS_INIT_VLC_STATIC(num, size) \ + INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \ + ps_tmp[num].ps_bits, 1, 1, \ + ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \ + size); + +#define PS_VLC_ROW(name) \ + { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) } + +av_cold void ff_ps_init(void) { + // Syntax initialization + static const struct { + const void *ps_codes, *ps_bits; + const unsigned int table_size, elem_size; + } ps_tmp[] = { + PS_VLC_ROW(huff_iid_df1), + PS_VLC_ROW(huff_iid_dt1), + PS_VLC_ROW(huff_iid_df0), + PS_VLC_ROW(huff_iid_dt0), + PS_VLC_ROW(huff_icc_df), + PS_VLC_ROW(huff_icc_dt), + PS_VLC_ROW(huff_ipd_df), + PS_VLC_ROW(huff_ipd_dt), + PS_VLC_ROW(huff_opd_df), + PS_VLC_ROW(huff_opd_dt), + }; + + PS_INIT_VLC_STATIC(0, 1544); + PS_INIT_VLC_STATIC(1, 832); + PS_INIT_VLC_STATIC(2, 1024); + PS_INIT_VLC_STATIC(3, 1036); + PS_INIT_VLC_STATIC(4, 544); + PS_INIT_VLC_STATIC(5, 544); + PS_INIT_VLC_STATIC(6, 512); + PS_INIT_VLC_STATIC(7, 512); + PS_INIT_VLC_STATIC(8, 512); + PS_INIT_VLC_STATIC(9, 512); + + ps_tableinit(); +} + +av_cold void ff_ps_ctx_init(PSContext *ps) +{ +} diff --git a/libavcodec/aacps.h b/libavcodec/aacps.h new file mode 100644 index 0000000000..5fc211a986 --- /dev/null +++ b/libavcodec/aacps.h @@ -0,0 +1,82 @@ +/* + * MPEG-4 Parametric Stereo definitions and declarations + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_PS_H +#define AVCODEC_PS_H + +#include + +#include "avcodec.h" +#include "get_bits.h" + +#define PS_MAX_NUM_ENV 5 +#define PS_MAX_NR_IIDICC 34 +#define PS_MAX_NR_IPDOPD 17 +#define PS_MAX_SSB 91 +#define PS_MAX_AP_BANDS 50 +#define PS_QMF_TIME_SLOTS 32 +#define PS_MAX_DELAY 14 +#define PS_AP_LINKS 3 +#define PS_MAX_AP_DELAY 5 + +typedef struct { + int start; + int enable_iid; + int iid_quant; + int nr_iid_par; + int nr_ipdopd_par; + int enable_icc; + int icc_mode; + int nr_icc_par; + int enable_ext; + int frame_class; + int num_env_old; + int num_env; + int enable_ipdopd; + int border_position[PS_MAX_NUM_ENV+1]; + int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; // + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#define CONFIG_HARDCODED_TABLES 0 +#include "aacps_tablegen.h" +#include "tableprint.h" + +void write_float_3d_array (const void *p, int b, int c, int d) +{ + int i; + const float *f = p; + for (i = 0; i < b; i++) { + printf("{\n"); + write_float_2d_array(f, c, d); + printf("},\n"); + f += c * d; + } +} + +void write_float_4d_array (const void *p, int a, int b, int c, int d) +{ + int i; + const float *f = p; + for (i = 0; i < a; i++) { + printf("{\n"); + write_float_3d_array(f, b, c, d); + printf("},\n"); + f += b * c * d; + } +} + +int main(void) +{ + ps_tableinit(); + + write_fileheader(); + + printf("static const float pd_re_smooth[8*8*8] = {\n"); + write_float_array(pd_re_smooth, 8*8*8); + printf("};\n"); + printf("static const float pd_im_smooth[8*8*8] = {\n"); + write_float_array(pd_im_smooth, 8*8*8); + printf("};\n"); + + printf("static const float HA[46][8][4] = {\n"); + write_float_3d_array(HA, 46, 8, 4); + printf("};\n"); + printf("static const float HB[46][8][4] = {\n"); + write_float_3d_array(HB, 46, 8, 4); + printf("};\n"); + + printf("static const float f20_0_8[8][7][2] = {\n"); + write_float_3d_array(f20_0_8, 8, 7, 2); + printf("};\n"); + printf("static const float f34_0_12[12][7][2] = {\n"); + write_float_3d_array(f34_0_12, 12, 7, 2); + printf("};\n"); + printf("static const float f34_1_8[8][7][2] = {\n"); + write_float_3d_array(f34_1_8, 8, 7, 2); + printf("};\n"); + printf("static const float f34_2_4[4][7][2] = {\n"); + write_float_3d_array(f34_2_4, 4, 7, 2); + printf("};\n"); + + printf("static const float Q_fract_allpass[2][50][3][2] = {\n"); + write_float_4d_array(Q_fract_allpass, 2, 50, 3, 2); + printf("};\n"); + printf("static const float phi_fract[2][50][2] = {\n"); + write_float_3d_array(phi_fract, 2, 50, 2); + printf("};\n"); + + return 0; +} diff --git a/libavcodec/aacps_tablegen.h b/libavcodec/aacps_tablegen.h new file mode 100644 index 0000000000..3c1b27decd --- /dev/null +++ b/libavcodec/aacps_tablegen.h @@ -0,0 +1,212 @@ +/* + * Header file for hardcoded Parametric Stereo tables + * + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AACPS_TABLEGEN_H +#define AACPS_TABLEGEN_H + +#include + +#if CONFIG_HARDCODED_TABLES +#define ps_tableinit() +#include "libavcodec/aacps_tables.h" +#else +#include "../libavutil/common.h" +#include "../libavutil/mathematics.h" +#define NR_ALLPASS_BANDS20 30 +#define NR_ALLPASS_BANDS34 50 +#define PS_AP_LINKS 3 +static float pd_re_smooth[8*8*8]; +static float pd_im_smooth[8*8*8]; +static float HA[46][8][4]; +static float HB[46][8][4]; +static float f20_0_8 [ 8][7][2]; +static float f34_0_12[12][7][2]; +static float f34_1_8 [ 8][7][2]; +static float f34_2_4 [ 4][7][2]; +static float Q_fract_allpass[2][50][3][2]; +static float phi_fract[2][50][2]; + +static const float g0_Q8[] = { + 0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f, + 0.09885108575264f, 0.11793710567217f, 0.125f +}; + +static const float g0_Q12[] = { + 0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f, + 0.07428313801106f, 0.08100347892914f, 0.08333333333333f +}; + +static const float g1_Q8[] = { + 0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f, + 0.10307344158036f, 0.12222452249753f, 0.125f +}; + +static const float g2_Q4[] = { + -0.05908211155639f, -0.04871498374946f, 0.0f, 0.07778723915851f, + 0.16486303567403f, 0.23279856662996f, 0.25f +}; + +static void make_filters_from_proto(float (*filter)[7][2], const float *proto, int bands) +{ + int q, n; + for (q = 0; q < bands; q++) { + for (n = 0; n < 7; n++) { + double theta = 2 * M_PI * (q + 0.5) * (n - 6) / bands; + filter[q][n][0] = proto[n] * cos(theta); + filter[q][n][1] = proto[n] * -sin(theta); + } + } +} + +static void ps_tableinit(void) +{ + static const float ipdopd_sin[] = { 0, M_SQRT1_2, 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2 }; + static const float ipdopd_cos[] = { 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2, 0, M_SQRT1_2 }; + int pd0, pd1, pd2; + + static const float iid_par_dequant[] = { + //iid_par_dequant_default + 0.05623413251903, 0.12589254117942, 0.19952623149689, 0.31622776601684, + 0.44668359215096, 0.63095734448019, 0.79432823472428, 1, + 1.25892541179417, 1.58489319246111, 2.23872113856834, 3.16227766016838, + 5.01187233627272, 7.94328234724282, 17.7827941003892, + //iid_par_dequant_fine + 0.00316227766017, 0.00562341325190, 0.01, 0.01778279410039, + 0.03162277660168, 0.05623413251903, 0.07943282347243, 0.11220184543020, + 0.15848931924611, 0.22387211385683, 0.31622776601684, 0.39810717055350, + 0.50118723362727, 0.63095734448019, 0.79432823472428, 1, + 1.25892541179417, 1.58489319246111, 1.99526231496888, 2.51188643150958, + 3.16227766016838, 4.46683592150963, 6.30957344480193, 8.91250938133745, + 12.5892541179417, 17.7827941003892, 31.6227766016838, 56.2341325190349, + 100, 177.827941003892, 316.227766016837, + }; + static const float icc_invq[] = { + 1, 0.937, 0.84118, 0.60092, 0.36764, 0, -0.589, -1 + }; + static const float acos_icc_invq[] = { + 0, 0.35685527, 0.57133466, 0.92614472, 1.1943263, M_PI/2, 2.2006171, M_PI + }; + int iid, icc; + + int k, m; + static const int8_t f_center_20[] = { + -3, -1, 1, 3, 5, 7, 10, 14, 18, 22, + }; + static const int8_t f_center_34[] = { + 2, 6, 10, 14, 18, 22, 26, 30, + 34,-10, -6, -2, 51, 57, 15, 21, + 27, 33, 39, 45, 54, 66, 78, 42, + 102, 66, 78, 90,102,114,126, 90, + }; + static const float fractional_delay_links[] = { 0.43f, 0.75f, 0.347f }; + const float fractional_delay_gain = 0.39f; + + for (pd0 = 0; pd0 < 8; pd0++) { + float pd0_re = ipdopd_cos[pd0]; + float pd0_im = ipdopd_sin[pd0]; + for (pd1 = 0; pd1 < 8; pd1++) { + float pd1_re = ipdopd_cos[pd1]; + float pd1_im = ipdopd_sin[pd1]; + for (pd2 = 0; pd2 < 8; pd2++) { + float pd2_re = ipdopd_cos[pd2]; + float pd2_im = ipdopd_sin[pd2]; + float re_smooth = 0.25f * pd0_re + 0.5f * pd1_re + pd2_re; + float im_smooth = 0.25f * pd0_im + 0.5f * pd1_im + pd2_im; + float pd_mag = 1 / sqrt(im_smooth * im_smooth + re_smooth * re_smooth); + pd_re_smooth[pd0*64+pd1*8+pd2] = re_smooth * pd_mag; + pd_im_smooth[pd0*64+pd1*8+pd2] = im_smooth * pd_mag; + } + } + } + + for (iid = 0; iid < 46; iid++) { + float c = iid_par_dequant[iid]; //icc_mode < 3)*/ { + float alpha = 0.5f * acos_icc_invq[icc]; + float beta = alpha * (c1 - c2) * (float)M_SQRT1_2; + HA[iid][icc][0] = c2 * cosf(beta + alpha); + HA[iid][icc][1] = c1 * cosf(beta - alpha); + HA[iid][icc][2] = c2 * sinf(beta + alpha); + HA[iid][icc][3] = c1 * sinf(beta - alpha); + } /* else */ { + float alpha, gamma, mu, rho; + float alpha_c, alpha_s, gamma_c, gamma_s; + rho = FFMAX(icc_invq[icc], 0.05f); + alpha = 0.5f * atan2f(2.0f * c * rho, c*c - 1.0f); + mu = c + 1.0f / c; + mu = sqrtf(1 + (4 * rho * rho - 4)/(mu * mu)); + gamma = atanf(sqrtf((1.0f - mu)/(1.0f + mu))); + if (alpha < 0) alpha += M_PI/2; + alpha_c = cosf(alpha); + alpha_s = sinf(alpha); + gamma_c = cosf(gamma); + gamma_s = sinf(gamma); + HB[iid][icc][0] = M_SQRT2 * alpha_c * gamma_c; + HB[iid][icc][1] = M_SQRT2 * alpha_s * gamma_c; + HB[iid][icc][2] = -M_SQRT2 * alpha_s * gamma_s; + HB[iid][icc][3] = M_SQRT2 * alpha_c * gamma_s; + } + } + } + + for (k = 0; k < NR_ALLPASS_BANDS20; k++) { + double f_center, theta; + if (k < FF_ARRAY_ELEMS(f_center_20)) + f_center = f_center_20[k] * 0.125; + else + f_center = k - 6.5f; + for (m = 0; m < PS_AP_LINKS; m++) { + theta = -M_PI * fractional_delay_links[m] * f_center; + Q_fract_allpass[0][k][m][0] = cos(theta); + Q_fract_allpass[0][k][m][1] = sin(theta); + } + theta = -M_PI*fractional_delay_gain*f_center; + phi_fract[0][k][0] = cos(theta); + phi_fract[0][k][1] = sin(theta); + } + for (k = 0; k < NR_ALLPASS_BANDS34; k++) { + double f_center, theta; + if (k < FF_ARRAY_ELEMS(f_center_34)) + f_center = f_center_34[k] / 24.; + else + f_center = k - 26.5f; + for (m = 0; m < PS_AP_LINKS; m++) { + theta = -M_PI * fractional_delay_links[m] * f_center; + Q_fract_allpass[1][k][m][0] = cos(theta); + Q_fract_allpass[1][k][m][1] = sin(theta); + } + theta = -M_PI*fractional_delay_gain*f_center; + phi_fract[1][k][0] = cos(theta); + phi_fract[1][k][1] = sin(theta); + } + + make_filters_from_proto(f20_0_8, g0_Q8, 8); + make_filters_from_proto(f34_0_12, g0_Q12, 12); + make_filters_from_proto(f34_1_8, g1_Q8, 8); + make_filters_from_proto(f34_2_4, g2_Q4, 4); +} +#endif /* CONFIG_HARDCODED_TABLES */ + +#endif /* AACPS_TABLEGEN_H */ diff --git a/libavcodec/aacpsdata.c b/libavcodec/aacpsdata.c new file mode 100644 index 0000000000..7431caebc6 --- /dev/null +++ b/libavcodec/aacpsdata.c @@ -0,0 +1,163 @@ +/* + * MPEG-4 Parametric Stereo data tables + * Copyright (c) 2010 Alex Converse + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +static const uint8_t huff_iid_df1_bits[] = { + 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 16, 16, 15, 14, 14, + 13, 12, 12, 11, 10, 10, 8, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 17, 18, 17, 18, 18, + 18, 18, 18, 18, 18, 18, 18, +}; + +static const uint32_t huff_iid_df1_codes[] = { + 0x01FEB4, 0x01FEB5, 0x01FD76, 0x01FD77, 0x01FD74, 0x01FD75, 0x01FE8A, + 0x01FE8B, 0x01FE88, 0x00FE80, 0x01FEB6, 0x00FE82, 0x00FEB8, 0x007F42, + 0x007FAE, 0x003FAF, 0x001FD1, 0x001FE9, 0x000FE9, 0x0007EA, 0x0007FB, + 0x0003FB, 0x0001FB, 0x0001FF, 0x00007C, 0x00003C, 0x00001C, 0x00000C, + 0x000000, 0x000001, 0x000001, 0x000002, 0x000001, 0x00000D, 0x00001D, + 0x00003D, 0x00007D, 0x0000FC, 0x0001FC, 0x0003FC, 0x0003F4, 0x0007EB, + 0x000FEA, 0x001FEA, 0x001FD6, 0x003FD0, 0x007FAF, 0x007F43, 0x00FEB9, + 0x00FE83, 0x01FEB7, 0x00FE81, 0x01FE89, 0x01FE8E, 0x01FE8F, 0x01FE8C, + 0x01FE8D, 0x01FEB2, 0x01FEB3, 0x01FEB0, 0x01FEB1, +}; + +static const uint8_t huff_iid_dt1_bits[] = { + 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 13, + 13, 13, 12, 12, 11, 10, 9, 9, 7, 6, 5, 3, 1, 2, 5, 6, 7, 8, + 9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, +}; + +static const uint16_t huff_iid_dt1_codes[] = { + 0x004ED4, 0x004ED5, 0x004ECE, 0x004ECF, 0x004ECC, 0x004ED6, 0x004ED8, + 0x004F46, 0x004F60, 0x002718, 0x002719, 0x002764, 0x002765, 0x00276D, + 0x0027B1, 0x0013B7, 0x0013D6, 0x0009C7, 0x0009E9, 0x0009ED, 0x0004EE, + 0x0004F7, 0x000278, 0x000139, 0x00009A, 0x00009F, 0x000020, 0x000011, + 0x00000A, 0x000003, 0x000001, 0x000000, 0x00000B, 0x000012, 0x000021, + 0x00004C, 0x00009B, 0x00013A, 0x000279, 0x000270, 0x0004EF, 0x0004E2, + 0x0009EA, 0x0009D8, 0x0013D7, 0x0013D0, 0x0027B2, 0x0027A2, 0x00271A, + 0x00271B, 0x004F66, 0x004F67, 0x004F61, 0x004F47, 0x004ED9, 0x004ED7, + 0x004ECD, 0x004ED2, 0x004ED3, 0x004ED0, 0x004ED1, +}; + +static const uint8_t huff_iid_df0_bits[] = { + 17, 17, 17, 17, 16, 15, 13, 10, 9, 7, 6, 5, 4, 3, 1, 3, 4, 5, + 6, 6, 8, 11, 13, 14, 14, 15, 17, 18, 18, +}; + +static const uint32_t huff_iid_df0_codes[] = { + 0x01FFFB, 0x01FFFC, 0x01FFFD, 0x01FFFA, 0x00FFFC, 0x007FFC, 0x001FFD, + 0x0003FE, 0x0001FE, 0x00007E, 0x00003C, 0x00001D, 0x00000D, 0x000005, + 0x000000, 0x000004, 0x00000C, 0x00001C, 0x00003D, 0x00003E, 0x0000FE, + 0x0007FE, 0x001FFC, 0x003FFC, 0x003FFD, 0x007FFD, 0x01FFFE, 0x03FFFE, + 0x03FFFF, +}; + +static const uint8_t huff_iid_dt0_bits[] = { + 19, 19, 19, 20, 20, 20, 17, 15, 12, 10, 8, 6, 4, 2, 1, 3, 5, 7, + 9, 11, 13, 14, 17, 19, 20, 20, 20, 20, 20, +}; + +static const uint32_t huff_iid_dt0_codes[] = { + 0x07FFF9, 0x07FFFA, 0x07FFFB, 0x0FFFF8, 0x0FFFF9, 0x0FFFFA, 0x01FFFD, + 0x007FFE, 0x000FFE, 0x0003FE, 0x0000FE, 0x00003E, 0x00000E, 0x000002, + 0x000000, 0x000006, 0x00001E, 0x00007E, 0x0001FE, 0x0007FE, 0x001FFE, + 0x003FFE, 0x01FFFC, 0x07FFF8, 0x0FFFFB, 0x0FFFFC, 0x0FFFFD, 0x0FFFFE, + 0x0FFFFF, +}; + +static const uint8_t huff_icc_df_bits[] = { + 14, 14, 12, 10, 7, 5, 3, 1, 2, 4, 6, 8, 9, 11, 13, +}; + +static const uint16_t huff_icc_df_codes[] = { + 0x3FFF, 0x3FFE, 0x0FFE, 0x03FE, 0x007E, 0x001E, 0x0006, 0x0000, + 0x0002, 0x000E, 0x003E, 0x00FE, 0x01FE, 0x07FE, 0x1FFE, +}; + +static const uint8_t huff_icc_dt_bits[] = { + 14, 13, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12, 14, +}; + +static const uint16_t huff_icc_dt_codes[] = { + 0x3FFE, 0x1FFE, 0x07FE, 0x01FE, 0x007E, 0x001E, 0x0006, 0x0000, + 0x0002, 0x000E, 0x003E, 0x00FE, 0x03FE, 0x0FFE, 0x3FFF, +}; + +static const uint8_t huff_ipd_df_bits[] = { + 1, 3, 4, 4, 4, 4, 4, 4, +}; + +static const uint8_t huff_ipd_df_codes[] = { + 0x01, 0x00, 0x06, 0x04, 0x02, 0x03, 0x05, 0x07, +}; + +static const uint8_t huff_ipd_dt_bits[] = { + 1, 3, 4, 5, 5, 4, 4, 3, +}; + +static const uint8_t huff_ipd_dt_codes[] = { + 0x01, 0x02, 0x02, 0x03, 0x02, 0x00, 0x03, 0x03, +}; + +static const uint8_t huff_opd_df_bits[] = { + 1, 3, 4, 4, 5, 5, 4, 3, +}; + +static const uint8_t huff_opd_df_codes[] = { + 0x01, 0x01, 0x06, 0x04, 0x0F, 0x0E, 0x05, 0x00, +}; + +static const uint8_t huff_opd_dt_bits[] = { + 1, 3, 4, 5, 5, 4, 4, 3, +}; + +static const uint8_t huff_opd_dt_codes[] = { + 0x01, 0x02, 0x01, 0x07, 0x06, 0x00, 0x02, 0x03, +}; + +static const int8_t huff_offset[] = { + 30, 30, + 14, 14, + 7, 7, + 0, 0, + 0, 0, +}; + +///Table 8.48 +static const int8_t k_to_i_20[] = { + 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, + 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 +}; +///Table 8.49 +static const int8_t k_to_i_34[] = { + 0, 1, 2, 3, 4, 5, 6, 6, 7, 2, 1, 0, 10, 10, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 9, 14, 11, 12, 13, 14, 15, 16, 13, 16, 17, 18, 19, 20, 21, + 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, + 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 +}; + +static const float g1_Q2[] = { + 0.0f, 0.01899487526049f, 0.0f, -0.07293139167538f, + 0.0f, 0.30596630545168f, 0.5f +}; diff --git a/libavcodec/aacsbr.c b/libavcodec/aacsbr.c index 0de81a5025..a8ce7ce9a2 100644 --- a/libavcodec/aacsbr.c +++ b/libavcodec/aacsbr.c @@ -31,6 +31,8 @@ #include "aacsbr.h" #include "aacsbrdata.h" #include "fft.h" +#include "aacps.h" +#include "libavutil/libm.h" #include #include @@ -71,9 +73,6 @@ enum { static VLC vlc_sbr[10]; static const int8_t vlc_sbr_lav[10] = { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 }; -static DECLARE_ALIGNED(16, float, analysis_cos_pre)[64]; -static DECLARE_ALIGNED(16, float, analysis_sin_pre)[64]; -static DECLARE_ALIGNED(16, float, analysis_cossin_post)[32][2]; static const DECLARE_ALIGNED(16, float, zero64)[64]; #define SBR_INIT_VLC_STATIC(num, size) \ @@ -87,7 +86,7 @@ static const DECLARE_ALIGNED(16, float, zero64)[64]; av_cold void ff_aac_sbr_init(void) { - int n, k; + int n; static const struct { const void *sbr_codes, *sbr_bits; const unsigned int table_size, elem_size; @@ -116,16 +115,6 @@ av_cold void ff_aac_sbr_init(void) SBR_INIT_VLC_STATIC(8, 592); SBR_INIT_VLC_STATIC(9, 512); - for (n = 0; n < 64; n++) { - float pre = M_PI * n / 64; - analysis_cos_pre[n] = cosf(pre); - analysis_sin_pre[n] = sinf(pre); - } - for (k = 0; k < 32; k++) { - float post = M_PI * (k + 0.5) / 128; - analysis_cossin_post[k][0] = 4.0 * cosf(post); - analysis_cossin_post[k][1] = -4.0 * sinf(post); - } for (n = 1; n < 320; n++) sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n]; sbr_qmf_window_us[384] = -sbr_qmf_window_us[384]; @@ -133,6 +122,8 @@ av_cold void ff_aac_sbr_init(void) for (n = 0; n < 320; n++) sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n]; + + ff_ps_init(); } av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr) @@ -142,13 +133,14 @@ av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr) sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128); sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128); ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64); - ff_rdft_init(&sbr->rdft, 6, IDFT_R2C); + ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0); + ff_ps_ctx_init(&sbr->ps); } av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr) { ff_mdct_end(&sbr->mdct); - ff_rdft_end(&sbr->rdft); + ff_mdct_end(&sbr->mdct_ana); } static int qsort_comparison_function_int16(const void *a, const void *b) @@ -293,15 +285,15 @@ static void make_bands(int16_t* bands, int start, int stop, int num_bands) bands[num_bands-1] = stop - previous; } -static int check_n_master(AVCodecContext *avccontext, int n_master, int bs_xover_band) +static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band) { // Requirements (14496-3 sp04 p205) if (n_master <= 0) { - av_log(avccontext, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master); + av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master); return -1; } if (bs_xover_band >= n_master) { - av_log(avccontext, AV_LOG_ERROR, + av_log(avctx, AV_LOG_ERROR, "Invalid bitstream, crossover band index beyond array bounds: %d\n", bs_xover_band); return -1; @@ -349,7 +341,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, sbr_offset_ptr = sbr_offset[5]; break; default: - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Unsupported sample rate for SBR: %d\n", sbr->sample_rate); return -1; } @@ -367,7 +359,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, } else if (spectrum->bs_stop_freq == 15) { sbr->k[2] = 3*sbr->k[0]; } else { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq); return -1; } @@ -382,18 +374,17 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, max_qmf_subbands = 32; if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]); return -1; } if (!spectrum->bs_freq_scale) { - unsigned int dk; - int k2diff; + int dk, k2diff; dk = spectrum->bs_alter_scale + 1; sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1; - if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; for (k = 1; k <= sbr->n_master; k++) @@ -402,7 +393,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk; if (k2diff < 0) { sbr->f_master[1]--; - sbr->f_master[2]-= (k2diff < 1); + sbr->f_master[2]-= (k2diff < -1); } else if (k2diff) { sbr->f_master[sbr->n_master]++; } @@ -428,7 +419,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2; if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205) - av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0); + av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0); return -1; } @@ -442,7 +433,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, vk0[0] = sbr->k[0]; for (k = 1; k <= num_bands_0; k++) { if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205) - av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]); + av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]); return -1; } vk0[k] += vk0[k-1]; @@ -472,14 +463,14 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, vk1[0] = sbr->k[1]; for (k = 1; k <= num_bands_1; k++) { if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205) - av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]); + av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]); return -1; } vk1[k] += vk1[k-1]; } sbr->n_master = num_bands_0 + num_bands_1; - if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; memcpy(&sbr->f_master[0], vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0])); @@ -488,7 +479,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, } else { sbr->n_master = num_bands_0; - if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) + if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0])); } @@ -524,7 +515,7 @@ static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr) // illegal however the Coding Technologies decoder check stream has a final // count of 6 patches if (sbr->num_patches > 5) { - av_log(ac->avccontext, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches); + av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches); return -1; } @@ -563,12 +554,12 @@ static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr) // Requirements (14496-3 sp04 p205) if (sbr->kx[1] + sbr->m[1] > 64) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]); return -1; } if (sbr->kx[1] > 32) { - av_log(ac->avccontext, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]); + av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]); return -1; } @@ -580,7 +571,7 @@ static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr) sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands * log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3 if (sbr->n_q > 5) { - av_log(ac->avccontext, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q); + av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q); return -1; } @@ -638,7 +629,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, ch_data->bs_amp_res = 0; if (ch_data->bs_num_env > 4) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n", ch_data->bs_num_env); return -1; @@ -693,7 +684,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1; if (ch_data->bs_num_env > 5) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n", ch_data->bs_num_env); return -1; @@ -714,7 +705,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, } if (bs_pointer > ch_data->bs_num_env + 1) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n", bs_pointer); return -1; @@ -722,7 +713,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, for (i = 1; i <= ch_data->bs_num_env; i++) { if (ch_data->t_env[i-1] > ch_data->t_env[i]) { - av_log(ac->avccontext, AV_LOG_ERROR, "Non monotone time borders\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n"); return -1; } } @@ -903,25 +894,24 @@ static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, GetBitContext *gb, int bs_extension_id, int *num_bits_left) { -//TODO - implement ps_data for parametric stereo parsing switch (bs_extension_id) { case EXTENSION_ID_PS: if (!ac->m4ac.ps) { - av_log(ac->avccontext, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n"); + av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n"); skip_bits_long(gb, *num_bits_left); // bs_fill_bits *num_bits_left = 0; } else { -#if 0 - *num_bits_left -= ff_ps_data(gb, ps); +#if 1 + *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left); #else - av_log_missing_feature(ac->avccontext, "Parametric Stereo is", 0); + av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0); skip_bits_long(gb, *num_bits_left); // bs_fill_bits *num_bits_left = 0; #endif } break; default: - av_log_missing_feature(ac->avccontext, "Reserved SBR extensions are", 1); + av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1); skip_bits_long(gb, *num_bits_left); // bs_fill_bits *num_bits_left = 0; break; @@ -1006,7 +996,7 @@ static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr, return get_bits_count(gb) - cnt; } } else { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac); sbr->start = 0; return get_bits_count(gb) - cnt; @@ -1021,6 +1011,11 @@ static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr, num_bits_left -= 2; read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id } + if (num_bits_left < 0) { + av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n"); + } + if (num_bits_left > 0) + skip_bits(gb, num_bits_left); } return get_bits_count(gb) - cnt; @@ -1033,7 +1028,7 @@ static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr) if (err >= 0) err = sbr_make_f_derived(ac, sbr); if (err < 0) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "SBR reset failed. Switching SBR to pure upsampling mode.\n"); sbr->start = 0; } @@ -1085,7 +1080,7 @@ int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr, bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3); if (bytes_read > cnt) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read); } return cnt; @@ -1139,7 +1134,7 @@ static void sbr_dequant(SpectralBandReplication *sbr, int id_aac) * @param x pointer to the beginning of the first sample window * @param W array of complex-valued samples split into subbands */ -static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in, float *x, +static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x, float z[320], float W[2][32][32][2], float scale) { @@ -1152,23 +1147,23 @@ static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in memcpy(x+288, in, 1024*sizeof(*x)); for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames // are not supported - float re, im; dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320); for (k = 0; k < 64; k++) { float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256]; - z[k] = f * analysis_cos_pre[k]; - z[k+64] = f; + z[k] = f; } - ff_rdft_calc(rdft, z); - re = z[0] * 0.5f; - im = 0.5f * dsp->scalarproduct_float(z+64, analysis_sin_pre, 64); - W[1][i][0][0] = re * analysis_cossin_post[0][0] - im * analysis_cossin_post[0][1]; - W[1][i][0][1] = re * analysis_cossin_post[0][1] + im * analysis_cossin_post[0][0]; + //Shuffle to IMDCT + z[64] = z[0]; for (k = 1; k < 32; k++) { - re = z[2*k ] - re; - im = z[2*k+1] - im; - W[1][i][k][0] = re * analysis_cossin_post[k][0] - im * analysis_cossin_post[k][1]; - W[1][i][k][1] = re * analysis_cossin_post[k][1] + im * analysis_cossin_post[k][0]; + z[64+2*k-1] = z[ k]; + z[64+2*k ] = -z[64-k]; + } + z[64+63] = z[32]; + + ff_imdct_half(mdct, z, z+64); + for (k = 0; k < 32; k++) { + W[1][i][k][0] = -z[63-k]; + W[1][i][k][1] = z[k]; } x += 32; } @@ -1179,7 +1174,7 @@ static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in * (14496-3 sp04 p206) */ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct, - float *out, float X[2][32][64], + float *out, float X[2][38][64], float mdct_buf[2][64], float *v0, int *v_off, const unsigned int div, float bias, float scale) @@ -1187,31 +1182,33 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct, int i, n; const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us; int scale_and_bias = scale != 1.0f || bias != 0.0f; + const int step = 128 >> div; float *v; for (i = 0; i < 32; i++) { - if (*v_off == 0) { + if (*v_off < step) { int saved_samples = (1280 - 128) >> div; memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float)); - *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div); + *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step; } else { - *v_off -= 128 >> div; + *v_off -= step; } v = v0 + *v_off; - for (n = 1; n < 64 >> div; n+=2) { - X[1][i][n] = -X[1][i][n]; - } - if (div) { - memset(X[0][i]+32, 0, 32*sizeof(float)); - memset(X[1][i]+32, 0, 32*sizeof(float)); - } - ff_imdct_half(mdct, mdct_buf[0], X[0][i]); - ff_imdct_half(mdct, mdct_buf[1], X[1][i]); if (div) { for (n = 0; n < 32; n++) { - v[ n] = -mdct_buf[0][63 - 2*n] + mdct_buf[1][2*n ]; - v[ 63 - n] = mdct_buf[0][62 - 2*n] + mdct_buf[1][2*n + 1]; + X[0][i][ n] = -X[0][i][n]; + X[0][i][32+n] = X[1][i][31-n]; + } + ff_imdct_half(mdct, mdct_buf[0], X[0][i]); + for (n = 0; n < 32; n++) { + v[ n] = mdct_buf[0][63 - 2*n]; + v[63 - n] = -mdct_buf[0][62 - 2*n]; } } else { + for (n = 1; n < 64; n+=2) { + X[1][i][n] = -X[1][i][n]; + } + ff_imdct_half(mdct, mdct_buf[0], X[0][i]); + ff_imdct_half(mdct, mdct_buf[1], X[1][i]); for (n = 0; n < 64; n++) { v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ]; v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ]; @@ -1380,7 +1377,7 @@ static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, g--; if (g < 0) { - av_log(ac->avccontext, AV_LOG_ERROR, + av_log(ac->avctx, AV_LOG_ERROR, "ERROR : no subband found for frequency %d\n", k); return -1; } @@ -1414,7 +1411,7 @@ static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr, } /// Generate the subband filtered lowband -static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64], +static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64], const float X_low[32][40][2], const float Y[2][38][64][2], int ch) { @@ -1436,7 +1433,7 @@ static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64], } for (k = 0; k < sbr->kx[1]; k++) { - for (i = i_Temp; i < i_f; i++) { + for (i = i_Temp; i < 38; i++) { X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0]; X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1]; } @@ -1730,7 +1727,7 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, } for (ch = 0; ch < nch; ch++) { /* decode channel */ - sbr_qmf_analysis(&ac->dsp, &sbr->rdft, ch ? R : L, sbr->data[ch].analysis_filterbank_samples, + sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples, (float*)sbr->qmf_filter_scratch, sbr->data[ch].W, 1/(-1024 * ac->sf_scale)); sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W); @@ -1752,6 +1749,16 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac, /* synthesis */ sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch); } + + if (ac->m4ac.ps == 1) { + if (sbr->ps.start) { + ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]); + } else { + memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0])); + } + nch = 2; + } + sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch, sbr->data[0].synthesis_filterbank_samples, &sbr->data[0].synthesis_filterbank_samples_offset, diff --git a/libavcodec/aactab.c b/libavcodec/aactab.c index eaa5486822..17102a66f5 100644 --- a/libavcodec/aactab.c +++ b/libavcodec/aactab.c @@ -29,6 +29,7 @@ #include "libavutil/mem.h" #include "aac.h" +#include "aac_tablegen.h" #include @@ -1204,129 +1205,3 @@ const uint8_t ff_tns_max_bands_128[] = { 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14 }; // @} - - -#if CONFIG_HARDCODED_TABLES - -/** - * Table of pow(2, (i - 200)/4.) used for different purposes depending on the - * range of indices to the table: - * [ 0, 255] scale factor decoding when using C dsp.float_to_int16 - * [60, 315] scale factor decoding when using SIMD dsp.float_to_int16 - * [45, 300] intensity stereo position decoding mapped in reverse order i.e. 0->300, 1->299, ..., 254->46, 255->45 - */ -const float ff_aac_pow2sf_tab[428] = { - 8.88178420e-16, 1.05622810e-15, 1.25607397e-15, 1.49373210e-15, - 1.77635684e-15, 2.11245619e-15, 2.51214793e-15, 2.98746420e-15, - 3.55271368e-15, 4.22491238e-15, 5.02429587e-15, 5.97492839e-15, - 7.10542736e-15, 8.44982477e-15, 1.00485917e-14, 1.19498568e-14, - 1.42108547e-14, 1.68996495e-14, 2.00971835e-14, 2.38997136e-14, - 2.84217094e-14, 3.37992991e-14, 4.01943669e-14, 4.77994272e-14, - 5.68434189e-14, 6.75985982e-14, 8.03887339e-14, 9.55988543e-14, - 1.13686838e-13, 1.35197196e-13, 1.60777468e-13, 1.91197709e-13, - 2.27373675e-13, 2.70394393e-13, 3.21554936e-13, 3.82395417e-13, - 4.54747351e-13, 5.40788785e-13, 6.43109871e-13, 7.64790834e-13, - 9.09494702e-13, 1.08157757e-12, 1.28621974e-12, 1.52958167e-12, - 1.81898940e-12, 2.16315514e-12, 2.57243948e-12, 3.05916334e-12, - 3.63797881e-12, 4.32631028e-12, 5.14487897e-12, 6.11832668e-12, - 7.27595761e-12, 8.65262056e-12, 1.02897579e-11, 1.22366534e-11, - 1.45519152e-11, 1.73052411e-11, 2.05795159e-11, 2.44733067e-11, - 2.91038305e-11, 3.46104823e-11, 4.11590317e-11, 4.89466134e-11, - 5.82076609e-11, 6.92209645e-11, 8.23180635e-11, 9.78932268e-11, - 1.16415322e-10, 1.38441929e-10, 1.64636127e-10, 1.95786454e-10, - 2.32830644e-10, 2.76883858e-10, 3.29272254e-10, 3.91572907e-10, - 4.65661287e-10, 5.53767716e-10, 6.58544508e-10, 7.83145814e-10, - 9.31322575e-10, 1.10753543e-09, 1.31708902e-09, 1.56629163e-09, - 1.86264515e-09, 2.21507086e-09, 2.63417803e-09, 3.13258326e-09, - 3.72529030e-09, 4.43014173e-09, 5.26835606e-09, 6.26516652e-09, - 7.45058060e-09, 8.86028346e-09, 1.05367121e-08, 1.25303330e-08, - 1.49011612e-08, 1.77205669e-08, 2.10734243e-08, 2.50606661e-08, - 2.98023224e-08, 3.54411338e-08, 4.21468485e-08, 5.01213321e-08, - 5.96046448e-08, 7.08822677e-08, 8.42936970e-08, 1.00242664e-07, - 1.19209290e-07, 1.41764535e-07, 1.68587394e-07, 2.00485328e-07, - 2.38418579e-07, 2.83529071e-07, 3.37174788e-07, 4.00970657e-07, - 4.76837158e-07, 5.67058141e-07, 6.74349576e-07, 8.01941314e-07, - 9.53674316e-07, 1.13411628e-06, 1.34869915e-06, 1.60388263e-06, - 1.90734863e-06, 2.26823256e-06, 2.69739830e-06, 3.20776526e-06, - 3.81469727e-06, 4.53646513e-06, 5.39479661e-06, 6.41553051e-06, - 7.62939453e-06, 9.07293026e-06, 1.07895932e-05, 1.28310610e-05, - 1.52587891e-05, 1.81458605e-05, 2.15791864e-05, 2.56621220e-05, - 3.05175781e-05, 3.62917210e-05, 4.31583729e-05, 5.13242441e-05, - 6.10351562e-05, 7.25834421e-05, 8.63167458e-05, 1.02648488e-04, - 1.22070312e-04, 1.45166884e-04, 1.72633492e-04, 2.05296976e-04, - 2.44140625e-04, 2.90333768e-04, 3.45266983e-04, 4.10593953e-04, - 4.88281250e-04, 5.80667537e-04, 6.90533966e-04, 8.21187906e-04, - 9.76562500e-04, 1.16133507e-03, 1.38106793e-03, 1.64237581e-03, - 1.95312500e-03, 2.32267015e-03, 2.76213586e-03, 3.28475162e-03, - 3.90625000e-03, 4.64534029e-03, 5.52427173e-03, 6.56950324e-03, - 7.81250000e-03, 9.29068059e-03, 1.10485435e-02, 1.31390065e-02, - 1.56250000e-02, 1.85813612e-02, 2.20970869e-02, 2.62780130e-02, - 3.12500000e-02, 3.71627223e-02, 4.41941738e-02, 5.25560260e-02, - 6.25000000e-02, 7.43254447e-02, 8.83883476e-02, 1.05112052e-01, - 1.25000000e-01, 1.48650889e-01, 1.76776695e-01, 2.10224104e-01, - 2.50000000e-01, 2.97301779e-01, 3.53553391e-01, 4.20448208e-01, - 5.00000000e-01, 5.94603558e-01, 7.07106781e-01, 8.40896415e-01, - 1.00000000e+00, 1.18920712e+00, 1.41421356e+00, 1.68179283e+00, - 2.00000000e+00, 2.37841423e+00, 2.82842712e+00, 3.36358566e+00, - 4.00000000e+00, 4.75682846e+00, 5.65685425e+00, 6.72717132e+00, - 8.00000000e+00, 9.51365692e+00, 1.13137085e+01, 1.34543426e+01, - 1.60000000e+01, 1.90273138e+01, 2.26274170e+01, 2.69086853e+01, - 3.20000000e+01, 3.80546277e+01, 4.52548340e+01, 5.38173706e+01, - 6.40000000e+01, 7.61092554e+01, 9.05096680e+01, 1.07634741e+02, - 1.28000000e+02, 1.52218511e+02, 1.81019336e+02, 2.15269482e+02, - 2.56000000e+02, 3.04437021e+02, 3.62038672e+02, 4.30538965e+02, - 5.12000000e+02, 6.08874043e+02, 7.24077344e+02, 8.61077929e+02, - 1.02400000e+03, 1.21774809e+03, 1.44815469e+03, 1.72215586e+03, - 2.04800000e+03, 2.43549617e+03, 2.89630938e+03, 3.44431172e+03, - 4.09600000e+03, 4.87099234e+03, 5.79261875e+03, 6.88862343e+03, - 8.19200000e+03, 9.74198469e+03, 1.15852375e+04, 1.37772469e+04, - 1.63840000e+04, 1.94839694e+04, 2.31704750e+04, 2.75544937e+04, - 3.27680000e+04, 3.89679387e+04, 4.63409500e+04, 5.51089875e+04, - 6.55360000e+04, 7.79358775e+04, 9.26819000e+04, 1.10217975e+05, - 1.31072000e+05, 1.55871755e+05, 1.85363800e+05, 2.20435950e+05, - 2.62144000e+05, 3.11743510e+05, 3.70727600e+05, 4.40871900e+05, - 5.24288000e+05, 6.23487020e+05, 7.41455200e+05, 8.81743800e+05, - 1.04857600e+06, 1.24697404e+06, 1.48291040e+06, 1.76348760e+06, - 2.09715200e+06, 2.49394808e+06, 2.96582080e+06, 3.52697520e+06, - 4.19430400e+06, 4.98789616e+06, 5.93164160e+06, 7.05395040e+06, - 8.38860800e+06, 9.97579232e+06, 1.18632832e+07, 1.41079008e+07, - 1.67772160e+07, 1.99515846e+07, 2.37265664e+07, 2.82158016e+07, - 3.35544320e+07, 3.99031693e+07, 4.74531328e+07, 5.64316032e+07, - 6.71088640e+07, 7.98063385e+07, 9.49062656e+07, 1.12863206e+08, - 1.34217728e+08, 1.59612677e+08, 1.89812531e+08, 2.25726413e+08, - 2.68435456e+08, 3.19225354e+08, 3.79625062e+08, 4.51452825e+08, - 5.36870912e+08, 6.38450708e+08, 7.59250125e+08, 9.02905651e+08, - 1.07374182e+09, 1.27690142e+09, 1.51850025e+09, 1.80581130e+09, - 2.14748365e+09, 2.55380283e+09, 3.03700050e+09, 3.61162260e+09, - 4.29496730e+09, 5.10760567e+09, 6.07400100e+09, 7.22324521e+09, - 8.58993459e+09, 1.02152113e+10, 1.21480020e+10, 1.44464904e+10, - 1.71798692e+10, 2.04304227e+10, 2.42960040e+10, 2.88929808e+10, - 3.43597384e+10, 4.08608453e+10, 4.85920080e+10, 5.77859616e+10, - 6.87194767e+10, 8.17216907e+10, 9.71840160e+10, 1.15571923e+11, - 1.37438953e+11, 1.63443381e+11, 1.94368032e+11, 2.31143847e+11, - 2.74877907e+11, 3.26886763e+11, 3.88736064e+11, 4.62287693e+11, - 5.49755814e+11, 6.53773525e+11, 7.77472128e+11, 9.24575386e+11, - 1.09951163e+12, 1.30754705e+12, 1.55494426e+12, 1.84915077e+12, - 2.19902326e+12, 2.61509410e+12, 3.10988851e+12, 3.69830155e+12, - 4.39804651e+12, 5.23018820e+12, 6.21977702e+12, 7.39660309e+12, - 8.79609302e+12, 1.04603764e+13, 1.24395540e+13, 1.47932062e+13, - 1.75921860e+13, 2.09207528e+13, 2.48791081e+13, 2.95864124e+13, - 3.51843721e+13, 4.18415056e+13, 4.97582162e+13, 5.91728247e+13, - 7.03687442e+13, 8.36830112e+13, 9.95164324e+13, 1.18345649e+14, - 1.40737488e+14, 1.67366022e+14, 1.99032865e+14, 2.36691299e+14, - 2.81474977e+14, 3.34732045e+14, 3.98065730e+14, 4.73382598e+14, - 5.62949953e+14, 6.69464090e+14, 7.96131459e+14, 9.46765196e+14, - 1.12589991e+15, 1.33892818e+15, 1.59226292e+15, 1.89353039e+15, - 2.25179981e+15, 2.67785636e+15, 3.18452584e+15, 3.78706078e+15, - 4.50359963e+15, 5.35571272e+15, 6.36905167e+15, 7.57412156e+15, - 9.00719925e+15, 1.07114254e+16, 1.27381033e+16, 1.51482431e+16, - 1.80143985e+16, 2.14228509e+16, 2.54762067e+16, 3.02964863e+16, - 3.60287970e+16, 4.28457018e+16, 5.09524134e+16, 6.05929725e+16, - 7.20575940e+16, 8.56914035e+16, 1.01904827e+17, 1.21185945e+17, -}; - -#else - -float ff_aac_pow2sf_tab[428]; - -#endif /* CONFIG_HARDCODED_TABLES */ diff --git a/libavcodec/aactab.h b/libavcodec/aactab.h index 283d6c3a2d..c6213999b5 100644 --- a/libavcodec/aactab.h +++ b/libavcodec/aactab.h @@ -32,6 +32,7 @@ #include "libavutil/mem.h" #include "aac.h" +#include "aac_tablegen_decl.h" #include @@ -73,10 +74,4 @@ extern const uint16_t * const ff_swb_offset_128 [13]; extern const uint8_t ff_tns_max_bands_1024[13]; extern const uint8_t ff_tns_max_bands_128 [13]; -#if CONFIG_HARDCODED_TABLES -extern const float ff_aac_pow2sf_tab[428]; -#else -extern float ff_aac_pow2sf_tab[428]; -#endif /* CONFIG_HARDCODED_TABLES */ - #endif /* AVCODEC_AACTAB_H */ diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c index 856f97aafb..6cd666b4fb 100644 --- a/libavcodec/ac3_parser.c +++ b/libavcodec/ac3_parser.c @@ -181,7 +181,7 @@ static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info, hdr_info->samples = hdr.num_blocks * 256; if(hdr.bitstream_id>10) hdr_info->codec_id = CODEC_ID_EAC3; - else + else if (hdr_info->codec_id == CODEC_ID_NONE) hdr_info->codec_id = CODEC_ID_AC3; *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT); diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index 0f9052ec26..f2f6e5ce4d 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1268,7 +1268,7 @@ static int decode_audio_block(AC3DecodeContext *s, int blk) } /* apply spectral extension to high frequency bins */ - if (s->spx_in_use) { + if (s->spx_in_use && CONFIG_EAC3_DECODER) { ff_eac3_apply_spectral_extension(s); } diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 68bd656aa6..2cf2d20f46 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -678,17 +678,23 @@ static int adpcm_encode_frame(AVCodecContext *avctx, static av_cold int adpcm_decode_init(AVCodecContext * avctx) { ADPCMContext *c = avctx->priv_data; + unsigned int min_channels = 1; unsigned int max_channels = 2; switch(avctx->codec->id) { + case CODEC_ID_ADPCM_EA: + min_channels = 2; + break; case CODEC_ID_ADPCM_EA_R1: case CODEC_ID_ADPCM_EA_R2: case CODEC_ID_ADPCM_EA_R3: max_channels = 6; break; } - if(avctx->channels > max_channels){ - return -1; + + if (avctx->channels < min_channels || avctx->channels > max_channels) { + av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n"); + return AVERROR(EINVAL); } switch(avctx->codec->id) { diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 6db65cfdde..245a77be78 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -253,7 +253,7 @@ void avcodec_register_all(void) REGISTER_DECODER (TTA, tta); REGISTER_DECODER (TWINVQ, twinvq); REGISTER_DECODER (VMDAUDIO, vmdaudio); - REGISTER_ENCDEC (VORBIS, vorbis); + REGISTER_DECODER (VORBIS, vorbis); REGISTER_DECODER (WAVPACK, wavpack); REGISTER_DECODER (WMAPRO, wmapro); REGISTER_ENCDEC (WMAV1, wmav1); @@ -343,7 +343,9 @@ void avcodec_register_all(void) REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger); REGISTER_DECODER (LIBSPEEX, libspeex); REGISTER_ENCODER (LIBTHEORA, libtheora); + REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc); REGISTER_ENCODER (LIBVORBIS, libvorbis); + REGISTER_ENCDEC (LIBVPX, libvpx); REGISTER_ENCODER (LIBX264, libx264); REGISTER_ENCODER (LIBXVID, libxvid); diff --git a/libavcodec/api-example.c b/libavcodec/api-example.c index fb48b1f6d6..f34075e666 100644 --- a/libavcodec/api-example.c +++ b/libavcodec/api-example.c @@ -39,6 +39,8 @@ #include "libavutil/mathematics.h" #define INBUF_SIZE 4096 +#define AUDIO_INBUF_SIZE 20480 +#define AUDIO_REFILL_THRESH 4096 /* * Audio encoding example @@ -118,7 +120,7 @@ static void audio_decode_example(const char *outfilename, const char *filename) int out_size, len; FILE *f, *outfile; uint8_t *outbuf; - uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; + uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; AVPacket avpkt; av_init_packet(&avpkt); @@ -155,25 +157,32 @@ static void audio_decode_example(const char *outfilename, const char *filename) /* decode until eof */ avpkt.data = inbuf; - for(;;) { - avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); - if (avpkt.size == 0) - break; + avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); - avpkt.data = inbuf; - while (avpkt.size > 0) { - out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; - len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); - if (len < 0) { - fprintf(stderr, "Error while decoding\n"); - exit(1); - } - if (out_size > 0) { - /* if a frame has been decoded, output it */ - fwrite(outbuf, 1, out_size, outfile); - } - avpkt.size -= len; - avpkt.data += len; + while (avpkt.size > 0) { + out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; + len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); + if (len < 0) { + fprintf(stderr, "Error while decoding\n"); + exit(1); + } + if (out_size > 0) { + /* if a frame has been decoded, output it */ + fwrite(outbuf, 1, out_size, outfile); + } + avpkt.size -= len; + avpkt.data += len; + if (avpkt.size < AUDIO_REFILL_THRESH) { + /* Refill the input buffer, to avoid trying to decode + * incomplete frames. Instead of this, one could also use + * a parser, or use a proper container format through + * libavformat. */ + memmove(inbuf, avpkt.data, avpkt.size); + avpkt.data = inbuf; + len = fread(avpkt.data + avpkt.size, 1, + AUDIO_INBUF_SIZE - avpkt.size, f); + if (len > 0) + avpkt.size += len; } } diff --git a/libavcodec/arm/int_neon.S b/libavcodec/arm/int_neon.S index e8023e0686..f86bf53f95 100644 --- a/libavcodec/arm/int_neon.S +++ b/libavcodec/arm/int_neon.S @@ -67,10 +67,10 @@ function ff_scalarproduct_int16_neon, export=1 3: vpadd.s32 d16, d0, d1 vpadd.s32 d17, d2, d3 - vpadd.s32 d10, d4, d5 - vpadd.s32 d11, d6, d7 + vpadd.s32 d18, d4, d5 + vpadd.s32 d19, d6, d7 vpadd.s32 d0, d16, d17 - vpadd.s32 d1, d10, d11 + vpadd.s32 d1, d18, d19 vpadd.s32 d2, d0, d1 vpaddl.s32 d3, d2 vmov.32 r0, d3[0] @@ -107,10 +107,10 @@ function ff_scalarproduct_and_madd_int16_neon, export=1 vpadd.s32 d16, d0, d1 vpadd.s32 d17, d2, d3 - vpadd.s32 d10, d4, d5 - vpadd.s32 d11, d6, d7 + vpadd.s32 d18, d4, d5 + vpadd.s32 d19, d6, d7 vpadd.s32 d0, d16, d17 - vpadd.s32 d1, d10, d11 + vpadd.s32 d1, d18, d19 vpadd.s32 d2, d0, d1 vpaddl.s32 d3, d2 vmov.32 r0, d3[0] diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c index 5179c345cf..8452853d7b 100644 --- a/libavcodec/atrac3.c +++ b/libavcodec/atrac3.c @@ -393,6 +393,8 @@ static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent for (k=0; k= 64) + return AVERROR_INVALIDDATA; pComponent[component_count].pos = j * 64 + (get_bits(gb,6)); max_coded_values = 1024 - pComponent[component_count].pos; coded_values = coded_values_per_component + 1; diff --git a/libavcodec/audioconvert.h b/libavcodec/audioconvert.h index e0f9bb06e2..81b6cded39 100644 --- a/libavcodec/audioconvert.h +++ b/libavcodec/audioconvert.h @@ -103,9 +103,9 @@ void av_audio_convert_free(AVAudioConvert *ctx); /** * Convert between audio sample formats * @param[in] out array of output buffers for each channel. set to NULL to ignore processing of the given channel. - * @param[in] out_stride distance between consecutive input samples (measured in bytes) + * @param[in] out_stride distance between consecutive output samples (measured in bytes) * @param[in] in array of input buffers for each channel - * @param[in] in_stride distance between consecutive output samples (measured in bytes) + * @param[in] in_stride distance between consecutive input samples (measured in bytes) * @param len length of audio frame size (measured in samples) */ int av_audio_convert(AVAudioConvert *ctx, diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index b6437473e6..974e87cc53 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -30,8 +30,8 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 52 -#define LIBAVCODEC_VERSION_MINOR 66 -#define LIBAVCODEC_VERSION_MICRO 0 +#define LIBAVCODEC_VERSION_MINOR 72 +#define LIBAVCODEC_VERSION_MICRO 2 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ @@ -210,6 +210,7 @@ enum CodecID { CODEC_ID_IFF_BYTERUN1, CODEC_ID_KGV1, CODEC_ID_YOP, + CODEC_ID_VP8, /* various PCM "codecs" */ CODEC_ID_PCM_S16LE= 0x10000, @@ -643,6 +644,11 @@ typedef struct RcOverride{ * as a last resort. */ #define CODEC_CAP_SUBFRAMES 0x0100 +/** + * Codec is experimental and is thus avoided in favor of non experimental + * encoders + */ +#define CODEC_CAP_EXPERIMENTAL 0x0200 //The following defines may change, don't expect compatibility if you use them. #define MB_TYPE_INTRA4x4 0x0001 @@ -3303,12 +3309,20 @@ unsigned avcodec_get_edge_width(void); * Modifies width and height values so that they will result in a memory * buffer that is acceptable for the codec if you do not use any horizontal * padding. + * + * May only be used if a codec with CODEC_CAP_DR1 has been opened. + * If CODEC_FLAG_EMU_EDGE is not set, the dimensions must have been increased + * according to avcodec_get_edge_width() before. */ void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height); /** * Modifies width and height values so that they will result in a memory * buffer that is acceptable for the codec if you also ensure that all * line sizes are a multiple of the respective linesize_align[i]. + * + * May only be used if a codec with CODEC_CAP_DR1 has been opened. + * If CODEC_FLAG_EMU_EDGE is not set, the dimensions must have been increased + * according to avcodec_get_edge_width() before. */ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]); @@ -3464,8 +3478,8 @@ attribute_deprecated int avcodec_decode_video(AVCodecContext *avctx, AVFrame *pi * * @param avctx the codec context * @param[out] picture The AVFrame in which the decoded video frame will be stored. - * Use avcodec_alloc_frame to get an AVFrame, the codec will call - * get_buffer to get memory for the actual image data. + * Use avcodec_alloc_frame to get an AVFrame, the codec will + * allocate memory for the actual bitmap. * @param[in] avpkt The input AVpacket containing the input buffer. * You can create such packet with av_init_packet() and by then setting * data and size, some decoders might in addition need other fields like diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 9d6307c217..626f33a4a2 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -130,12 +130,14 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb, r++; mask = -(level_code & 1); level = (level^mask) - mask; - } else { + } else if (level_code >= 0) { level = r->rltab[level_code][0]; if(!level) //end of block signal break; run = r->rltab[level_code][1]; r += r->rltab[level_code][2]; + } else { + break; } level_buf[i] = level; run_buf[i] = run; @@ -189,7 +191,8 @@ static inline int decode_residual_inter(AVSContext *h) { static int decode_mb_i(AVSContext *h, int cbp_code) { GetBitContext *gb = &h->s.gb; - int block, pred_mode_uv; + unsigned pred_mode_uv; + int block; uint8_t top[18]; uint8_t *left = NULL; uint8_t *d; @@ -445,6 +448,8 @@ static inline int check_for_slice(AVSContext *h) { if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) { skip_bits_long(gb,24+align); h->stc = get_bits(gb,8); + if (h->stc >= h->mb_height) + return 0; decode_slice_header(h,gb); return 1; } @@ -659,7 +664,7 @@ static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size, buf_end = buf + buf_size; for(;;) { buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc); - if(stc & 0xFFFFFE00) + if((stc & 0xFFFFFE00) || buf_ptr == buf_end) return FFMAX(0, buf_ptr - buf - s->parse_context.last_index); input_size = (buf_end - buf_ptr)*8; switch(stc) { diff --git a/libavcodec/cdgraphics.c b/libavcodec/cdgraphics.c index c174aa9d98..8d8aea437d 100644 --- a/libavcodec/cdgraphics.c +++ b/libavcodec/cdgraphics.c @@ -280,6 +280,10 @@ static int cdg_decode_frame(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n"); return AVERROR(EINVAL); } + if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) { + av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n"); + return AVERROR(EINVAL); + } ret = avctx->reget_buffer(avctx, &cc->frame); if (ret) { diff --git a/libavcodec/celp_filters.c b/libavcodec/celp_filters.c index 26a62eed14..0f8e071735 100644 --- a/libavcodec/celp_filters.c +++ b/libavcodec/celp_filters.c @@ -133,9 +133,8 @@ void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, out2 -= val * old_out2; out3 -= val * old_out3; - old_out3 = out[-5]; - for (i = 5; i <= filter_length; i += 2) { + old_out3 = out[-i]; val = filter_coeffs[i-1]; out0 -= val * old_out3; @@ -154,7 +153,6 @@ void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, FFSWAP(float, old_out0, old_out2); old_out1 = old_out3; - old_out3 = out[-i-2]; } tmp0 = out0; diff --git a/libavcodec/dca.c b/libavcodec/dca.c index 10bc956e98..493971384f 100644 --- a/libavcodec/dca.c +++ b/libavcodec/dca.c @@ -1224,6 +1224,7 @@ static int dca_decode_frame(AVCodecContext * avctx, //set AVCodec values with parsed data avctx->sample_rate = s->sample_rate; avctx->bit_rate = s->bit_rate; + avctx->frame_size = s->sample_blocks * 32; channels = s->prim_channels + !!s->lfe; diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 7c5351686e..8cc8fb56a2 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -169,6 +169,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, int in, out = 0; int predictor[2]; int channel_number = 0; + int stereo = s->channels - 1; short *output_samples = data; int shift[2]; unsigned char byte; @@ -177,6 +178,9 @@ static int dpcm_decode_frame(AVCodecContext *avctx, if (!buf_size) return 0; + if (stereo && (buf_size & 1)) + buf_size--; + // almost every DPCM variant expands one byte of data into two if(*data_size/2 < buf_size) return -1; @@ -295,7 +299,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, } *data_size = out * sizeof(short); - return buf_size; + return avpkt->size; } #define DPCM_DECODER(id, name, long_name_) \ diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c index 07013240c0..060a73cce6 100644 --- a/libavcodec/dsputil.c +++ b/libavcodec/dsputil.c @@ -3217,7 +3217,7 @@ static void clear_blocks_c(DCTELEM *blocks) static void add_bytes_c(uint8_t *dst, uint8_t *src, int w){ long i; - for(i=0; i<=w-sizeof(long); i+=sizeof(long)){ + for(i=0; i<=w-(int)sizeof(long); i+=sizeof(long)){ long a = *(long*)(src+i); long b = *(long*)(dst+i); *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80); @@ -3228,7 +3228,7 @@ static void add_bytes_c(uint8_t *dst, uint8_t *src, int w){ static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){ long i; - for(i=0; i<=w-sizeof(long); i+=sizeof(long)){ + for(i=0; i<=w-(int)sizeof(long); i+=sizeof(long)){ long a = *(long*)(src1+i); long b = *(long*)(src2+i); *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80); @@ -3253,7 +3253,7 @@ static void diff_bytes_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){ } }else #endif - for(i=0; i<=w-sizeof(long); i+=sizeof(long)){ + for(i=0; i<=w-(int)sizeof(long); i+=sizeof(long)){ long a = *(long*)(src1+i); long b = *(long*)(src2+i); *(long*)(dst+i) = ((a|pb_80) - (b&pb_7f)) ^ ((a^b^pb_80)&pb_80); diff --git a/libavcodec/dxva2_internal.h b/libavcodec/dxva2_internal.h index a9be7a07e7..fcf45bc664 100644 --- a/libavcodec/dxva2_internal.h +++ b/libavcodec/dxva2_internal.h @@ -23,7 +23,16 @@ #ifndef AVCODEC_DXVA_INTERNAL_H #define AVCODEC_DXVA_INTERNAL_H +#define _WIN32_WINNT 0x0600 +#define COBJMACROS + +#include "config.h" + #include "dxva2.h" +#if HAVE_DXVA_H +#include +#endif + #include "avcodec.h" #include "mpegvideo.h" diff --git a/libavcodec/eatqi.c b/libavcodec/eatqi.c index 7f1901d309..53f21a2c84 100644 --- a/libavcodec/eatqi.c +++ b/libavcodec/eatqi.c @@ -59,12 +59,15 @@ static av_cold int tqi_decode_init(AVCodecContext *avctx) return 0; } -static void tqi_decode_mb(MpegEncContext *s, DCTELEM (*block)[64]) +static int tqi_decode_mb(MpegEncContext *s, DCTELEM (*block)[64]) { int n; s->dsp.clear_blocks(block[0]); for (n=0; n<6; n++) - ff_mpeg1_decode_block_intra(s, block[n], n); + if (ff_mpeg1_decode_block_intra(s, block[n], n) < 0) + return -1; + + return 0; } static inline void tqi_idct_put(TqiContext *t, DCTELEM (*block)[64]) @@ -136,7 +139,8 @@ static int tqi_decode_frame(AVCodecContext *avctx, for (s->mb_y=0; s->mb_y<(avctx->height+15)/16; s->mb_y++) for (s->mb_x=0; s->mb_x<(avctx->width+15)/16; s->mb_x++) { - tqi_decode_mb(s, t->block); + if (tqi_decode_mb(s, t->block) < 0) + break; tqi_idct_put(t, t->block); } diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c index 12056e1208..7a4f4d383d 100644 --- a/libavcodec/ffv1.c +++ b/libavcodec/ffv1.c @@ -437,7 +437,7 @@ static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], in int run_mode=0; if(s->ac){ - if(c->bytestream_end - c->bytestream < w*20){ + if(c->bytestream_end - c->bytestream < w*35){ av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } diff --git a/libavcodec/flicvideo.c b/libavcodec/flicvideo.c index 429ded53b2..b63d7d4498 100644 --- a/libavcodec/flicvideo.c +++ b/libavcodec/flicvideo.c @@ -159,7 +159,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, int pixel_skip; int pixel_countdown; unsigned char *pixels; - int pixel_limit; + unsigned int pixel_limit; s->frame.reference = 1; s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; @@ -253,10 +253,13 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets); } else if ((line_packets & 0xC000) == 0x8000) { // "last byte" opcode - pixels[y_ptr + s->frame.linesize[0] - 1] = line_packets & 0xff; + pixel_ptr= y_ptr + s->frame.linesize[0] - 1; + CHECK_PIXEL_PTR(0); + pixels[pixel_ptr] = line_packets & 0xff; } else { compressed_lines--; pixel_ptr = y_ptr; + CHECK_PIXEL_PTR(0); pixel_countdown = s->avctx->width; for (i = 0; i < line_packets; i++) { /* account for the skip bytes */ @@ -268,7 +271,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, byte_run = -byte_run; palette_idx1 = buf[stream_ptr++]; palette_idx2 = buf[stream_ptr++]; - CHECK_PIXEL_PTR(byte_run); + CHECK_PIXEL_PTR(byte_run * 2); for (j = 0; j < byte_run; j++, pixel_countdown -= 2) { pixels[pixel_ptr++] = palette_idx1; pixels[pixel_ptr++] = palette_idx2; @@ -298,6 +301,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, stream_ptr += 2; while (compressed_lines > 0) { pixel_ptr = y_ptr; + CHECK_PIXEL_PTR(0); pixel_countdown = s->avctx->width; line_packets = buf[stream_ptr++]; if (line_packets > 0) { @@ -453,7 +457,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, int pixel_countdown; unsigned char *pixels; int pixel; - int pixel_limit; + unsigned int pixel_limit; s->frame.reference = 1; s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; @@ -503,6 +507,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, } else { compressed_lines--; pixel_ptr = y_ptr; + CHECK_PIXEL_PTR(0); pixel_countdown = s->avctx->width; for (i = 0; i < line_packets; i++) { /* account for the skip bytes */ @@ -514,13 +519,13 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, byte_run = -byte_run; pixel = AV_RL16(&buf[stream_ptr]); stream_ptr += 2; - CHECK_PIXEL_PTR(byte_run); + CHECK_PIXEL_PTR(2 * byte_run); for (j = 0; j < byte_run; j++, pixel_countdown -= 2) { *((signed short*)(&pixels[pixel_ptr])) = pixel; pixel_ptr += 2; } } else { - CHECK_PIXEL_PTR(byte_run); + CHECK_PIXEL_PTR(2 * byte_run); for (j = 0; j < byte_run; j++, pixel_countdown--) { *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]); stream_ptr += 2; @@ -611,7 +616,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, if (byte_run > 0) { pixel = AV_RL16(&buf[stream_ptr]); stream_ptr += 2; - CHECK_PIXEL_PTR(byte_run); + CHECK_PIXEL_PTR(2 * byte_run); for (j = 0; j < byte_run; j++) { *((signed short*)(&pixels[pixel_ptr])) = pixel; pixel_ptr += 2; @@ -622,7 +627,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, } } else { /* copy pixels if byte_run < 0 */ byte_run = -byte_run; - CHECK_PIXEL_PTR(byte_run); + CHECK_PIXEL_PTR(2 * byte_run); for (j = 0; j < byte_run; j++) { *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]); stream_ptr += 2; diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index f5df5560c5..c80cfcfa2d 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -428,7 +428,9 @@ retry: if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){ if(s->stream_codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") || - s->codec_tag == AV_RL32("RMP4")) + s->codec_tag == AV_RL32("RMP4") || + s->codec_tag == AV_RL32("SIPP") + ) s->xvid_build= 0; #if 0 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1 diff --git a/libavcodec/h264.c b/libavcodec/h264.c index e4654f0435..02ff83309f 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -1448,15 +1448,17 @@ static void implicit_weight_table(H264Context *h, int field){ for(ref0=ref_start; ref0 < ref_count0; ref0++){ int poc0 = h->ref_list[0][ref0].poc; for(ref1=ref_start; ref1 < ref_count1; ref1++){ - int poc1 = h->ref_list[1][ref1].poc; - int td = av_clip(poc1 - poc0, -128, 127); - int w= 32; - if(td){ - int tb = av_clip(cur_poc - poc0, -128, 127); - int tx = (16384 + (FFABS(td) >> 1)) / td; - int dist_scale_factor = (tb*tx + 32) >> 8; - if(dist_scale_factor >= -64 && dist_scale_factor <= 128) - w = 64 - dist_scale_factor; + int w = 32; + if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) { + int poc1 = h->ref_list[1][ref1].poc; + int td = av_clip(poc1 - poc0, -128, 127); + if(td){ + int tb = av_clip(cur_poc - poc0, -128, 127); + int tx = (16384 + (FFABS(td) >> 1)) / td; + int dist_scale_factor = (tb*tx + 32) >> 8; + if(dist_scale_factor >= -64 && dist_scale_factor <= 128) + w = 64 - dist_scale_factor; + } } if(field<0){ h->implicit_weight[ref0][ref1][0]= @@ -1797,6 +1799,12 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ else s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3); + if (FFALIGN(s->avctx->width, 16) == s->width && + FFALIGN(s->avctx->height, 16) == s->height) { + s->width = s->avctx->width; + s->height = s->avctx->height; + } + if (s->context_initialized && ( s->width != s->avctx->width || s->height != s->avctx->height || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) { @@ -1840,7 +1848,10 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ h->prev_interlaced_frame = 1; init_scan_tables(h); - ff_h264_alloc_tables(h); + if (ff_h264_alloc_tables(h) < 0) { + av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n"); + return AVERROR(ENOMEM); + } for(i = 1; i < s->avctx->thread_count; i++) { H264Context *c; diff --git a/libavcodec/h264.h b/libavcodec/h264.h index c6563af35e..72bc390ce5 100644 --- a/libavcodec/h264.h +++ b/libavcodec/h264.h @@ -1048,7 +1048,7 @@ static void fill_decode_caches(H264Context *h, int mb_type){ AV_ZERO32(h->mv_cache [list][scan8[0] + 4 - 1*8]); h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE; } - if(h->ref_cache[list][scan8[0] + 4 - 1*8] < 0){ + if(h->ref_cache[list][scan8[0] + 2 - 1*8] < 0 || h->ref_cache[list][scan8[0] + 4 - 1*8] < 0){ if(USES_LIST(topleft_type, list)){ const int b_xy = h->mb2b_xy [topleft_xy] + 3 + h->b_stride + (h->topleft_partition & 2*h->b_stride); const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2); diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index 936418a8a0..0c92b36f65 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -55,7 +55,9 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, int keyframe) { H264BSFContext *ctx = bsfc->priv_data; uint8_t unit_type; - uint32_t nal_size, cumul_size = 0; + int32_t nal_size; + uint32_t cumul_size = 0; + const uint8_t *buf_end = buf + buf_size; /* nothing to filter */ if (!avctx->extradata || avctx->extradata_size < 6) { @@ -109,6 +111,9 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, *poutbuf_size = 0; *poutbuf = NULL; do { + if (buf + ctx->length_size > buf_end) + goto fail; + if (ctx->length_size == 1) nal_size = buf[0]; else if (ctx->length_size == 2) @@ -119,6 +124,9 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, buf += ctx->length_size; unit_type = *buf & 0x1f; + if (buf + nal_size > buf_end || nal_size < 0) + goto fail; + /* prepend only to the first type 5 NAL unit of an IDR picture */ if (ctx->first_idr && unit_type == 5) { alloc_and_copy(poutbuf, poutbuf_size, @@ -139,6 +147,11 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, } while (cumul_size < buf_size); return 1; + +fail: + av_freep(poutbuf); + *poutbuf_size = 0; + return AVERROR(EINVAL); } static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc) diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c index e5ae403204..f36e38d959 100644 --- a/libavcodec/h264_ps.c +++ b/libavcodec/h264_ps.c @@ -205,6 +205,12 @@ static inline int decode_vui_parameters(H264Context *h, SPS *sps){ sps->num_reorder_frames= get_ue_golomb(&s->gb); get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/ + if(s->gb.size_in_bits < get_bits_count(&s->gb)){ + av_log(h->s.avctx, AV_LOG_ERROR, "Overread VUI by %d bits\n", get_bits_count(&s->gb) - s->gb.size_in_bits); + sps->num_reorder_frames=0; + sps->bitstream_restriction_flag= 0; + } + if(sps->num_reorder_frames > 16U /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){ av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", sps->num_reorder_frames); return -1; @@ -291,8 +297,12 @@ int ff_h264_decode_seq_parameter_set(H264Context *h){ if(sps->profile_idc >= 100){ //high profile sps->chroma_format_idc= get_ue_golomb_31(&s->gb); - if(sps->chroma_format_idc == 3) + if (sps->chroma_format_idc > 3U) { + av_log(h->s.avctx, AV_LOG_ERROR, "chroma_format_idc %d is illegal\n", sps->chroma_format_idc); + goto fail; + } else if(sps->chroma_format_idc == 3) { sps->residual_color_transform_flag = get_bits1(&s->gb); + } sps->bit_depth_luma = get_ue_golomb(&s->gb) + 8; sps->bit_depth_chroma = get_ue_golomb(&s->gb) + 8; sps->transform_bypass = get_bits1(&s->gb); diff --git a/libavcodec/h264_refs.c b/libavcodec/h264_refs.c index ed715c6b03..21c14d8d26 100644 --- a/libavcodec/h264_refs.c +++ b/libavcodec/h264_refs.c @@ -657,7 +657,7 @@ int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){ } if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){ unsigned int long_arg= get_ue_golomb_31(gb); - if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){ + if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){ av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode); return -1; } diff --git a/libavcodec/huffyuv.c b/libavcodec/huffyuv.c index 7764c35181..883abac18e 100644 --- a/libavcodec/huffyuv.c +++ b/libavcodec/huffyuv.c @@ -279,12 +279,13 @@ static void generate_joint_tables(HYuvContext *s){ for(i=y=0; y<256; y++){ int len0 = s->len[0][y]; int limit = VLC_BITS - len0; - if(limit <= 0) + if(limit <= 0 || !len0) continue; for(u=0; u<256; u++){ int len1 = s->len[p][u]; - if(len1 > limit) + if (len1 > limit || !len1) continue; + assert(i < (1 << VLC_BITS)); len[i] = len0 + len1; bits[i] = (s->bits[0][y] << len1) + s->bits[p][u]; symbols[i] = (y<<8) + u; @@ -306,18 +307,19 @@ static void generate_joint_tables(HYuvContext *s){ for(i=0, g=-16; g<16; g++){ int len0 = s->len[p0][g&255]; int limit0 = VLC_BITS - len0; - if(limit0 < 2) + if (limit0 < 2 || !len0) continue; for(b=-16; b<16; b++){ int len1 = s->len[p1][b&255]; int limit1 = limit0 - len1; - if(limit1 < 1) + if (limit1 < 1 || !len1) continue; code = (s->bits[p0][g&255] << len1) + s->bits[p1][b&255]; for(r=-16; r<16; r++){ int len2 = s->len[2][r&255]; - if(len2 > limit1) + if (len2 > limit1 || !len2) continue; + assert(i < (1 << VLC_BITS)); len[i] = len0 + len1 + len2; bits[i] = (code << len2) + s->bits[2][r&255]; if(s->decorrelate){ @@ -341,6 +343,7 @@ static void generate_joint_tables(HYuvContext *s){ static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length){ GetBitContext gb; int i; + int ret; init_get_bits(&gb, src, length*8); @@ -356,7 +359,8 @@ printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j); } #endif free_vlc(&s->vlc[i]); - init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0); + if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0)) < 0) + return ret; } generate_joint_tables(s); @@ -368,6 +372,7 @@ static int read_old_huffman_tables(HYuvContext *s){ #if 1 GetBitContext gb; int i; + int ret; init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8); if(read_len_table(s->len[0], &gb)<0) @@ -388,7 +393,8 @@ static int read_old_huffman_tables(HYuvContext *s){ for(i=0; i<3; i++){ free_vlc(&s->vlc[i]); - init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0); + if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0)) < 0) + return ret; } generate_joint_tables(s); diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c index 8f789c4ae0..ae8ec16543 100644 --- a/libavcodec/imgconvert.c +++ b/libavcodec/imgconvert.c @@ -851,7 +851,8 @@ static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask, /* find exact color match with smallest size */ dst_pix_fmt = PIX_FMT_NONE; min_dist = 0x7fffffff; - for(i = 0;i < PIX_FMT_NB; i++) { + /* test only the first 64 pixel formats to avoid undefined behaviour */ + for (i = 0; i < 64; i++) { if (pix_fmt_mask & (1ULL << i)) { loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask; if (loss == 0) { diff --git a/libavcodec/indeo2.c b/libavcodec/indeo2.c index a3d6c80081..819c94bea1 100644 --- a/libavcodec/indeo2.c +++ b/libavcodec/indeo2.c @@ -156,6 +156,13 @@ static int ir2_decode_frame(AVCodecContext *avctx, return -1; } + start = 48; /* hardcoded for now */ + + if (start >= buf_size) { + av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size); + return AVERROR_INVALIDDATA; + } + s->decode_delta = buf[18]; /* decide whether frame uses deltas or not */ @@ -163,9 +170,8 @@ static int ir2_decode_frame(AVCodecContext *avctx, for (i = 0; i < buf_size; i++) buf[i] = av_reverse[buf[i]]; #endif - start = 48; /* hardcoded for now */ - init_get_bits(&s->gb, buf + start, buf_size - start); + init_get_bits(&s->gb, buf + start, (buf_size - start) * 8); if (s->decode_delta) { /* intraframe */ ir2_decode_plane(s, avctx->width, avctx->height, diff --git a/libavcodec/intelh263dec.c b/libavcodec/intelh263dec.c index 5e451294da..182b4955cc 100644 --- a/libavcodec/intelh263dec.c +++ b/libavcodec/intelh263dec.c @@ -72,7 +72,7 @@ int ff_intel_h263_decode_picture_header(MpegEncContext *s) } if(get_bits(&s->gb, 2)) av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n"); - s->loop_filter = get_bits1(&s->gb); + s->loop_filter = get_bits1(&s->gb) * !s->avctx->lowres; if(get_bits1(&s->gb)) av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n"); if(get_bits1(&s->gb)) diff --git a/libavcodec/ituh263dec.c b/libavcodec/ituh263dec.c index 8b5d9391b2..618eac3e44 100644 --- a/libavcodec/ituh263dec.c +++ b/libavcodec/ituh263dec.c @@ -961,6 +961,8 @@ int h263_decode_picture_header(MpegEncContext *s) s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */ s->loop_filter= get_bits1(&s->gb); s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter; + if(s->avctx->lowres) + s->loop_filter = 0; s->h263_slice_structured= get_bits1(&s->gb); if (get_bits1(&s->gb) != 0) { diff --git a/libavcodec/ivi_common.h b/libavcodec/ivi_common.h index 9d0eac1a34..64793e479e 100644 --- a/libavcodec/ivi_common.h +++ b/libavcodec/ivi_common.h @@ -89,7 +89,7 @@ typedef struct { uint32_t buf_offs; ///< address in the output buffer for this mb uint8_t type; ///< macroblock type: 0 - INTRA, 1 - INTER uint8_t cbp; ///< coded block pattern - uint8_t q_delta; ///< quant delta + int8_t q_delta; ///< quant delta int8_t mv_x; ///< motion vector (x component) int8_t mv_y; ///< motion vector (y component) } IVIMbInfo; diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c index 6b7dd94647..4dbc88b288 100644 --- a/libavcodec/jpeglsdec.c +++ b/libavcodec/jpeglsdec.c @@ -143,6 +143,8 @@ static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RI ret = ret >> 1; } + if(FFABS(ret) > 0xFFFF) + return -0x10000; /* update state */ state->A[Q] += FFABS(ret) - RItype; ret *= state->twonear; diff --git a/libavcodec/kgv1dec.c b/libavcodec/kgv1dec.c index 5af6b3b2bb..494aa11b0b 100644 --- a/libavcodec/kgv1dec.c +++ b/libavcodec/kgv1dec.c @@ -38,7 +38,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac const uint8_t *buf = avpkt->data; const uint8_t *buf_end = buf + avpkt->size; KgvContext * const c = avctx->priv_data; - int offsets[7]; + int offsets[8]; uint16_t *out, *prev; int outcnt = 0, maxcnt; int w, h, i; @@ -68,7 +68,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac return -1; c->prev = prev; - for (i = 0; i < 7; i++) + for (i = 0; i < 8; i++) offsets[i] = -1; while (outcnt < maxcnt && buf_end - 2 > buf) { diff --git a/libavcodec/kmvc.c b/libavcodec/kmvc.c index 3e8dccd363..c8de28b3e5 100644 --- a/libavcodec/kmvc.c +++ b/libavcodec/kmvc.c @@ -33,6 +33,7 @@ #define KMVC_KEYFRAME 0x80 #define KMVC_PALETTE 0x40 #define KMVC_METHOD 0x0F +#define MAX_PALSIZE 256 /* * Decoder context @@ -43,7 +44,7 @@ typedef struct KmvcContext { int setpal; int palsize; - uint32_t pal[256]; + uint32_t pal[MAX_PALSIZE]; uint8_t *cur, *prev; uint8_t *frm0, *frm1; } KmvcContext; @@ -365,6 +366,10 @@ static av_cold int decode_init(AVCodecContext * avctx) c->palsize = 127; } else { c->palsize = AV_RL16(avctx->extradata + 10); + if (c->palsize >= MAX_PALSIZE) { + av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n"); + return AVERROR_INVALIDDATA; + } } if (avctx->extradata_size == 1036) { // palette in extradata diff --git a/libavcodec/libvo-aacenc.c b/libavcodec/libvo-aacenc.c new file mode 100644 index 0000000000..da4a7bf07d --- /dev/null +++ b/libavcodec/libvo-aacenc.c @@ -0,0 +1,130 @@ +/* + * AAC encoder wrapper + * Copyright (c) 2010 Martin Storsjo + * + * 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 +#include + +#include "avcodec.h" +#include "mpeg4audio.h" + +typedef struct AACContext { + VO_AUDIO_CODECAPI codec_api; + VO_HANDLE handle; + VO_MEM_OPERATOR mem_operator; + VO_CODEC_INIT_USERDATA user_data; +} AACContext; + +static av_cold int aac_encode_init(AVCodecContext *avctx) +{ + AACContext *s = avctx->priv_data; + AACENC_PARAM params = { 0 }; + int index; + + avctx->coded_frame = avcodec_alloc_frame(); + avctx->frame_size = 1024; + + voGetAACEncAPI(&s->codec_api); + + s->mem_operator.Alloc = cmnMemAlloc; + s->mem_operator.Copy = cmnMemCopy; + s->mem_operator.Free = cmnMemFree; + s->mem_operator.Set = cmnMemSet; + s->mem_operator.Check = cmnMemCheck; + s->user_data.memflag = VO_IMF_USERMEMOPERATOR; + s->user_data.memData = &s->mem_operator; + s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data); + + params.sampleRate = avctx->sample_rate; + params.bitRate = avctx->bit_rate; + params.nChannels = avctx->channels; + params.adtsUsed = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER); + if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, ¶ms) + != VO_ERR_NONE) { + av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n"); + return AVERROR(EINVAL); + } + + for (index = 0; index < 16; index++) + if (avctx->sample_rate == ff_mpeg4audio_sample_rates[index]) + break; + if (index == 16) { + av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", + avctx->sample_rate); + return AVERROR(ENOSYS); + } + if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { + avctx->extradata_size = 2; + avctx->extradata = av_mallocz(avctx->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!avctx->extradata) + return AVERROR(ENOMEM); + + avctx->extradata[0] = 0x02 << 3 | index >> 1; + avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3; + } + return 0; +} + +static int aac_encode_close(AVCodecContext *avctx) +{ + AACContext *s = avctx->priv_data; + + s->codec_api.Uninit(s->handle); + av_freep(&avctx->coded_frame); + + return 0; +} + +static int aac_encode_frame(AVCodecContext *avctx, + unsigned char *frame/*out*/, + int buf_size, void *data/*in*/) +{ + AACContext *s = avctx->priv_data; + VO_CODECBUFFER input = { 0 }, output = { 0 }; + VO_AUDIO_OUTPUTINFO output_info = { { 0 } }; + + input.Buffer = data; + input.Length = 2 * avctx->channels * avctx->frame_size; + output.Buffer = frame; + output.Length = buf_size; + + s->codec_api.SetInputData(s->handle, &input); + if (s->codec_api.GetOutputData(s->handle, &output, &output_info) + != VO_ERR_NONE) { + av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n"); + return AVERROR(EINVAL); + } + return output.Length; +} + +AVCodec libvo_aacenc_encoder = { + "libvo_aacenc", + CODEC_TYPE_AUDIO, + CODEC_ID_AAC, + sizeof(AACContext), + aac_encode_init, + aac_encode_frame, + aac_encode_close, + NULL, + .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, + .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC"), +}; + diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c new file mode 100644 index 0000000000..0464d12e71 --- /dev/null +++ b/libavcodec/libvpxdec.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2010, Google, Inc. + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * VP8 decoder support via libvpx + */ + +#define VPX_CODEC_DISABLE_COMPAT 1 +#include +#include + +#include "avcodec.h" + +typedef struct VP8DecoderContext { + struct vpx_codec_ctx decoder; +} VP8Context; + +static av_cold int vp8_init(AVCodecContext *avctx) +{ + VP8Context *ctx = avctx->priv_data; + const struct vpx_codec_iface *iface = &vpx_codec_vp8_dx_algo; + struct vpx_codec_dec_cfg deccfg = { + /* token partitions+1 would be a decent choice */ + .threads = FFMIN(avctx->thread_count, 16) + }; + + av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str()); + av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config()); + + if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) { + const char *error = vpx_codec_error(&ctx->decoder); + av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n", + error); + return AVERROR(EINVAL); + } + + avctx->pix_fmt = PIX_FMT_YUV420P; + return 0; +} + +static int vp8_decode(AVCodecContext *avctx, + void *data, int *data_size, AVPacket *avpkt) +{ + VP8Context *ctx = avctx->priv_data; + AVFrame *picture = data; + const void *iter = NULL; + struct vpx_image *img; + + if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) != + VPX_CODEC_OK) { + const char *error = vpx_codec_error(&ctx->decoder); + const char *detail = vpx_codec_error_detail(&ctx->decoder); + + av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error); + if (detail) + av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", + detail); + return AVERROR_INVALIDDATA; + } + + if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) { + if (img->fmt != VPX_IMG_FMT_I420) { + av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n", + img->fmt); + return AVERROR_INVALIDDATA; + } + + if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) { + av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n", + avctx->width, avctx->height, img->d_w, img->d_h); + if (avcodec_check_dimensions(avctx, img->d_w, img->d_h)) + return AVERROR_INVALIDDATA; + avcodec_set_dimensions(avctx, img->d_w, img->d_h); + } + picture->data[0] = img->planes[0]; + picture->data[1] = img->planes[1]; + picture->data[2] = img->planes[2]; + picture->data[3] = NULL; + picture->linesize[0] = img->stride[0]; + picture->linesize[1] = img->stride[1]; + picture->linesize[2] = img->stride[2]; + picture->linesize[3] = 0; + *data_size = sizeof(AVPicture); + } + return avpkt->size; +} + +static av_cold int vp8_free(AVCodecContext *avctx) +{ + VP8Context *ctx = avctx->priv_data; + vpx_codec_destroy(&ctx->decoder); + return 0; +} + +AVCodec libvpx_decoder = { + "libvpx", + AVMEDIA_TYPE_VIDEO, + CODEC_ID_VP8, + sizeof(VP8Context), + vp8_init, + NULL, /* encode */ + vp8_free, + vp8_decode, + 0, /* capabilities */ + .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), +}; diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c new file mode 100644 index 0000000000..fa393b8f42 --- /dev/null +++ b/libavcodec/libvpxenc.c @@ -0,0 +1,489 @@ +/* + * Copyright (c) 2010, Google, Inc. + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * VP8 encoder support via libvpx + */ + +#define VPX_DISABLE_CTRL_TYPECHECKS 1 +#define VPX_CODEC_DISABLE_COMPAT 1 +#include +#include + +#include "avcodec.h" +#include "libavutil/base64.h" + +/** + * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h. + * One encoded frame returned from the library. + */ +struct FrameListData { + void *buf; /**≤ compressed data buffer */ + size_t sz; /**≤ length of compressed data */ + int64_t pts; /**≤ time stamp to show frame + (in timebase units) */ + unsigned long duration; /**≤ duration to show frame + (in timebase units) */ + uint32_t flags; /**≤ flags for this frame */ + struct FrameListData *next; +}; + +typedef struct VP8EncoderContext { + struct vpx_codec_ctx encoder; + struct vpx_image rawimg; + struct vpx_fixed_buf twopass_stats; + unsigned long deadline; //i.e., RT/GOOD/BEST + struct FrameListData *coded_frame_list; +} VP8Context; + +/** String mappings for enum vp8e_enc_control_id */ +static const char *ctlidstr[] = { + [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY", + [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE", + [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE", + [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP", + [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP", + [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE", + [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED", + [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF", + [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY", + [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS", + [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD", + [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS", + [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER", + [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES", + [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH", + [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE", +}; + +static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc) +{ + VP8Context *ctx = avctx->priv_data; + const char *error = vpx_codec_error(&ctx->encoder); + const char *detail = vpx_codec_error_detail(&ctx->encoder); + + av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error); + if (detail) + av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail); +} + +static av_cold void dump_enc_cfg(AVCodecContext *avctx, + const struct vpx_codec_enc_cfg *cfg) +{ + int width = -30; + int level = AV_LOG_DEBUG; + + av_log(avctx, level, "vpx_codec_enc_cfg\n"); + av_log(avctx, level, "generic settings\n" + " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n" + " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n", + width, "g_usage:", cfg->g_usage, + width, "g_threads:", cfg->g_threads, + width, "g_profile:", cfg->g_profile, + width, "g_w:", cfg->g_w, + width, "g_h:", cfg->g_h, + width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den, + width, "g_error_resilient:", cfg->g_error_resilient, + width, "g_pass:", cfg->g_pass, + width, "g_lag_in_frames:", cfg->g_lag_in_frames); + av_log(avctx, level, "rate control settings\n" + " %*s%u\n %*s%u\n %*s%u\n %*s%u\n" + " %*s%d\n %*s%p(%zu)\n %*s%u\n", + width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh, + width, "rc_resize_allowed:", cfg->rc_resize_allowed, + width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh, + width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh, + width, "rc_end_usage:", cfg->rc_end_usage, + width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz, + width, "rc_target_bitrate:", cfg->rc_target_bitrate); + av_log(avctx, level, "quantizer settings\n" + " %*s%u\n %*s%u\n", + width, "rc_min_quantizer:", cfg->rc_min_quantizer, + width, "rc_max_quantizer:", cfg->rc_max_quantizer); + av_log(avctx, level, "bitrate tolerance\n" + " %*s%u\n %*s%u\n", + width, "rc_undershoot_pct:", cfg->rc_undershoot_pct, + width, "rc_overshoot_pct:", cfg->rc_overshoot_pct); + av_log(avctx, level, "decoder buffer model\n" + " %*s%u\n %*s%u\n %*s%u\n", + width, "rc_buf_sz:", cfg->rc_buf_sz, + width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz, + width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz); + av_log(avctx, level, "2 pass rate control settings\n" + " %*s%u\n %*s%u\n %*s%u\n", + width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct, + width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct, + width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct); + av_log(avctx, level, "keyframing settings\n" + " %*s%d\n %*s%u\n %*s%u\n", + width, "kf_mode:", cfg->kf_mode, + width, "kf_min_dist:", cfg->kf_min_dist, + width, "kf_max_dist:", cfg->kf_max_dist); + av_log(avctx, level, "\n"); +} + +static void coded_frame_add(void *list, struct FrameListData *cx_frame) +{ + struct FrameListData **p = list; + + while (*p != NULL) + p = &(*p)->next; + *p = cx_frame; + cx_frame->next = NULL; +} + +static av_cold void free_coded_frame(struct FrameListData *cx_frame) +{ + av_freep(&cx_frame->buf); + av_freep(&cx_frame); +} + +static av_cold void free_frame_list(struct FrameListData *list) +{ + struct FrameListData *p = list; + + while (p) { + list = list->next; + free_coded_frame(p); + p = list; + } +} + +static av_cold int codecctl_int(AVCodecContext *avctx, + enum vp8e_enc_control_id id, int val) +{ + VP8Context *ctx = avctx->priv_data; + char buf[80]; + int width = -30; + int res; + + snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); + av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val); + + res = vpx_codec_control(&ctx->encoder, id, val); + if (res != VPX_CODEC_OK) { + snprintf(buf, sizeof(buf), "Failed to set %s codec control", + ctlidstr[id]); + log_encoder_error(avctx, buf); + } + + return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL); +} + +static av_cold int vp8_free(AVCodecContext *avctx) +{ + VP8Context *ctx = avctx->priv_data; + + vpx_codec_destroy(&ctx->encoder); + av_freep(&ctx->twopass_stats.buf); + av_freep(&avctx->coded_frame); + av_freep(&avctx->stats_out); + free_frame_list(ctx->coded_frame_list); + return 0; +} + +static av_cold int vp8_init(AVCodecContext *avctx) +{ + VP8Context *ctx = avctx->priv_data; + const struct vpx_codec_iface *iface = &vpx_codec_vp8_cx_algo; + int cpuused = 3; + struct vpx_codec_enc_cfg enccfg; + int res; + + av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str()); + av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config()); + + if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n", + vpx_codec_err_to_string(res)); + return AVERROR(EINVAL); + } + dump_enc_cfg(avctx, &enccfg); + + enccfg.g_w = avctx->width; + enccfg.g_h = avctx->height; + enccfg.g_timebase.num = avctx->time_base.num; + enccfg.g_timebase.den = avctx->time_base.den; + enccfg.g_threads = avctx->thread_count; + + if (avctx->flags & CODEC_FLAG_PASS1) + enccfg.g_pass = VPX_RC_FIRST_PASS; + else if (avctx->flags & CODEC_FLAG_PASS2) + enccfg.g_pass = VPX_RC_LAST_PASS; + else + enccfg.g_pass = VPX_RC_ONE_PASS; + + if (avctx->rc_min_rate == avctx->rc_max_rate && + avctx->rc_min_rate == avctx->bit_rate) + enccfg.rc_end_usage = VPX_CBR; + enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000, + AV_ROUND_NEAR_INF); + + //convert [1,51] -> [0,63] + enccfg.rc_min_quantizer = ((avctx->qmin * 5 + 1) >> 2) - 1; + enccfg.rc_max_quantizer = ((avctx->qmax * 5 + 1) >> 2) - 1; + + if (avctx->keyint_min == avctx->gop_size) + enccfg.kf_mode = VPX_KF_FIXED; + //_enc_init() will balk if kf_min_dist is set in this case + if (enccfg.kf_mode != VPX_KF_AUTO) + enccfg.kf_min_dist = avctx->keyint_min; + enccfg.kf_max_dist = avctx->gop_size; + + if (enccfg.g_pass == VPX_RC_FIRST_PASS) + enccfg.g_lag_in_frames = 0; + else if (enccfg.g_pass == VPX_RC_LAST_PASS) { + int decode_size; + + if (!avctx->stats_in) { + av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n"); + return AVERROR_INVALIDDATA; + } + + ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4; + ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz); + if (!ctx->twopass_stats.buf) { + av_log(avctx, AV_LOG_ERROR, + "Stat buffer alloc (%zu bytes) failed\n", + ctx->twopass_stats.sz); + return AVERROR(ENOMEM); + } + decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in, + ctx->twopass_stats.sz); + if (decode_size < 0) { + av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); + return AVERROR_INVALIDDATA; + } + + ctx->twopass_stats.sz = decode_size; + enccfg.rc_twopass_stats_in = ctx->twopass_stats; + } + + ctx->deadline = VPX_DL_GOOD_QUALITY; + + dump_enc_cfg(avctx, &enccfg); + /* Construct Encoder Context */ + res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0); + if (res != VPX_CODEC_OK) { + log_encoder_error(avctx, "Failed to initialize encoder"); + return AVERROR(EINVAL); + } + + //codec control failures are currently treated only as warnings + av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n"); + codecctl_int(avctx, VP8E_SET_CPUUSED, cpuused); + codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction); + + //provide dummy value to initialize wrapper, values will be updated each _encode() + vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1, + (unsigned char*)1); + + avctx->coded_frame = avcodec_alloc_frame(); + if (!avctx->coded_frame) { + av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n"); + vp8_free(avctx); + return AVERROR(ENOMEM); + } + return 0; +} + +static inline void cx_pktcpy(struct FrameListData *dst, + const struct vpx_codec_cx_pkt *src) +{ + dst->pts = src->data.frame.pts; + dst->duration = src->data.frame.duration; + dst->flags = src->data.frame.flags; + dst->sz = src->data.frame.sz; + dst->buf = src->data.frame.buf; +} + +/** + * Store coded frame information in format suitable for return from encode(). + * + * Write buffer information from @a cx_frame to @a buf & @a buf_size. + * Timing/frame details to @a coded_frame. + * @return Frame size written to @a buf on success + * @return AVERROR(EINVAL) on error + */ +static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, + uint8_t *buf, int buf_size, AVFrame *coded_frame) +{ + if ((int) cx_frame->sz <= buf_size) { + buf_size = cx_frame->sz; + memcpy(buf, cx_frame->buf, buf_size); + coded_frame->pts = cx_frame->pts; + coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY); + + if (coded_frame->key_frame) + coded_frame->pict_type = FF_I_TYPE; + else + coded_frame->pict_type = FF_P_TYPE; + } else { + av_log(avctx, AV_LOG_ERROR, + "Compressed frame larger than storage provided! (%zu/%d)\n", + cx_frame->sz, buf_size); + return AVERROR(EINVAL); + } + return buf_size; +} + +/** + * Queue multiple output frames from the encoder, returning the front-most. + * In cases where vpx_codec_get_cx_data() returns more than 1 frame append + * the frame queue. Return the head frame if available. + * @return Stored frame size + * @return AVERROR(EINVAL) on output size error + * @return AVERROR(ENOMEM) on coded frame queue data allocation error + */ +static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size, + AVFrame *coded_frame) +{ + VP8Context *ctx = avctx->priv_data; + const struct vpx_codec_cx_pkt *pkt; + const void *iter = NULL; + int size = 0; + + if (ctx->coded_frame_list) { + struct FrameListData *cx_frame = ctx->coded_frame_list; + /* return the leading frame if we've already begun queueing */ + size = storeframe(avctx, cx_frame, buf, buf_size, coded_frame); + if (size < 0) + return AVERROR(EINVAL); + ctx->coded_frame_list = cx_frame->next; + free_coded_frame(cx_frame); + } + + /* consume all available output from the encoder before returning. buffers + are only good through the next vpx_codec call */ + while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) { + switch (pkt->kind) { + case VPX_CODEC_CX_FRAME_PKT: + if (!size) { + struct FrameListData cx_frame; + + /* avoid storing the frame when the list is empty and we haven't yet + provided a frame for output */ + assert(!ctx->coded_frame_list); + cx_pktcpy(&cx_frame, pkt); + size = storeframe(avctx, &cx_frame, buf, buf_size, coded_frame); + if (size < 0) + return AVERROR(EINVAL); + } else { + struct FrameListData *cx_frame = + av_malloc(sizeof(struct FrameListData)); + + if (!cx_frame) { + av_log(avctx, AV_LOG_ERROR, + "Frame queue element alloc failed\n"); + return AVERROR(ENOMEM); + } + cx_pktcpy(cx_frame, pkt); + cx_frame->buf = av_malloc(cx_frame->sz); + + if (!cx_frame->buf) { + av_log(avctx, AV_LOG_ERROR, + "Data buffer alloc (%zu bytes) failed\n", + cx_frame->sz); + return AVERROR(ENOMEM); + } + memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz); + coded_frame_add(&ctx->coded_frame_list, cx_frame); + } + break; + case VPX_CODEC_STATS_PKT: { + struct vpx_fixed_buf *stats = &ctx->twopass_stats; + stats->buf = av_realloc(stats->buf, + stats->sz + pkt->data.twopass_stats.sz); + if (!stats->buf) { + av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n"); + return AVERROR(ENOMEM); + } + memcpy((uint8_t*)stats->buf + stats->sz, + pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz); + stats->sz += pkt->data.twopass_stats.sz; + break; + } + case VPX_CODEC_PSNR_PKT: //FIXME add support for CODEC_FLAG_PSNR + case VPX_CODEC_CUSTOM_PKT: + //ignore unsupported/unrecognized packet types + break; + } + } + + return size; +} + +static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size, + void *data) +{ + VP8Context *ctx = avctx->priv_data; + AVFrame *frame = data; + struct vpx_image *rawimg = NULL; + int64_t timestamp = 0; + int res, coded_size; + + if (frame) { + rawimg = &ctx->rawimg; + rawimg->planes[VPX_PLANE_Y] = frame->data[0]; + rawimg->planes[VPX_PLANE_U] = frame->data[1]; + rawimg->planes[VPX_PLANE_V] = frame->data[2]; + rawimg->stride[VPX_PLANE_Y] = frame->linesize[0]; + rawimg->stride[VPX_PLANE_U] = frame->linesize[1]; + rawimg->stride[VPX_PLANE_V] = frame->linesize[2]; + timestamp = frame->pts; + } + + res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp, + avctx->ticks_per_frame, 0, ctx->deadline); + if (res != VPX_CODEC_OK) { + log_encoder_error(avctx, "Error encoding frame"); + return AVERROR_INVALIDDATA; + } + coded_size = queue_frames(avctx, buf, buf_size, avctx->coded_frame); + + if (!frame && avctx->flags & CODEC_FLAG_PASS1) { + unsigned int b64_size = ((ctx->twopass_stats.sz + 2) / 3) * 4 + 1; + + avctx->stats_out = av_malloc(b64_size); + if (!avctx->stats_out) { + av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n", + b64_size); + return AVERROR(ENOMEM); + } + av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf, + ctx->twopass_stats.sz); + } + return coded_size; +} + +AVCodec libvpx_encoder = { + "libvpx", + AVMEDIA_TYPE_VIDEO, + CODEC_ID_VP8, + sizeof(VP8Context), + vp8_init, + vp8_encode, + vp8_free, + NULL, + CODEC_CAP_DELAY, + .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE}, + .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), +}; diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index df7b2e806b..e247255ce5 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -127,7 +127,8 @@ static int X264_frame(AVCodecContext *ctx, uint8_t *buf, } x4->out_pic.key_frame = pic_out.b_keyframe; - x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; + if (bufsize) + x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; return bufsize; } diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c index b418f5734c..324806e5e2 100644 --- a/libavcodec/mjpegbdec.c +++ b/libavcodec/mjpegbdec.c @@ -59,6 +59,9 @@ read_header: s->restart_count = 0; s->mjpb_skiptosod = 0; + if (buf_end - buf_ptr >= 1 << 28) + return AVERROR_INVALIDDATA; + init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8); skip_bits(&hgb, 32); /* reserved zeros */ @@ -109,8 +112,8 @@ read_header: av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs); if (sos_offs) { -// init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8); - init_get_bits(&s->gb, buf_ptr+sos_offs, field_size*8); + init_get_bits(&s->gb, buf_ptr + sos_offs, + 8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs)); s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16)); s->start_code = SOS; ff_mjpeg_decode_sos(s); diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c index 7f57af905c..edfdd56057 100644 --- a/libavcodec/mjpegdec.c +++ b/libavcodec/mjpegdec.c @@ -792,6 +792,10 @@ static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, i if (s->restart_interval && !s->restart_count) s->restart_count = s->restart_interval; + if(get_bits_count(&s->gb)>s->gb.size_in_bits){ + av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits); + return -1; + } for(i=0;irestart_interval && !--s->restart_count) { + if (s->restart_interval && show_bits(&s->gb, 8) == 0xFF){ /* skip RSTn */ + --s->restart_count; align_get_bits(&s->gb); - skip_bits(&s->gb, 16); /* skip RSTn */ + while(show_bits(&s->gb, 8) == 0xFF) + skip_bits(&s->gb, 8); + skip_bits(&s->gb, 8); for (i=0; ilast_dc[i] = 1024; } diff --git a/libavcodec/mlpdec.c b/libavcodec/mlpdec.c index 9169948f51..80fbbdb07e 100644 --- a/libavcodec/mlpdec.c +++ b/libavcodec/mlpdec.c @@ -62,6 +62,9 @@ typedef struct SubStream { //! For each channel output by the matrix, the output channel to map it to uint8_t ch_assign[MAX_CHANNELS]; + //! Channel coding parameters for channels in the substream + ChannelParams channel_params[MAX_CHANNELS]; + //! The left shift applied to random noise in 0x31ea substreams. uint8_t noise_shift; //! The current seed value for the pseudorandom noise generator(s). @@ -137,8 +140,6 @@ typedef struct MLPDecodeContext { SubStream substream[MAX_SUBSTREAMS]; - ChannelParams channel_params[MAX_CHANNELS]; - int matrix_changed; int filter_changed[MAX_CHANNELS][NUM_FILTERS]; @@ -173,8 +174,8 @@ static av_cold void init_static(void) static inline int32_t calculate_sign_huff(MLPDecodeContext *m, unsigned int substr, unsigned int ch) { - ChannelParams *cp = &m->channel_params[ch]; SubStream *s = &m->substream[substr]; + ChannelParams *cp = &s->channel_params[ch]; int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch]; int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1); int32_t sign_huff_offset = cp->huff_offset; @@ -202,7 +203,7 @@ static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp, m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp); for (channel = s->min_channel; channel <= s->max_channel; channel++) { - ChannelParams *cp = &m->channel_params[channel]; + ChannelParams *cp = &s->channel_params[channel]; int codebook = cp->codebook; int quant_step_size = s->quant_step_size[channel]; int lsb_bits = cp->huff_lsbs - quant_step_size; @@ -450,7 +451,7 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp, memset(s->quant_step_size, 0, sizeof(s->quant_step_size)); for (ch = s->min_channel; ch <= s->max_channel; ch++) { - ChannelParams *cp = &m->channel_params[ch]; + ChannelParams *cp = &s->channel_params[ch]; cp->filter_params[FIR].order = 0; cp->filter_params[IIR].order = 0; cp->filter_params[FIR].shift = 0; @@ -472,9 +473,11 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp, /** Read parameters for one of the prediction filters. */ static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, - unsigned int channel, unsigned int filter) + unsigned int substr, unsigned int channel, + unsigned int filter) { - FilterParams *fp = &m->channel_params[channel].filter_params[filter]; + SubStream *s = &m->substream[substr]; + FilterParams *fp = &s->channel_params[channel].filter_params[filter]; const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER; const char fchar = filter ? 'I' : 'F'; int i, order; @@ -497,7 +500,7 @@ static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, fp->order = order; if (order > 0) { - int32_t *fcoeff = m->channel_params[channel].coeff[filter]; + int32_t *fcoeff = s->channel_params[channel].coeff[filter]; int coeff_bits, coeff_shift; fp->shift = get_bits(gbp, 4); @@ -610,19 +613,19 @@ static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitCo static int read_channel_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp, unsigned int ch) { - ChannelParams *cp = &m->channel_params[ch]; + SubStream *s = &m->substream[substr]; + ChannelParams *cp = &s->channel_params[ch]; FilterParams *fir = &cp->filter_params[FIR]; FilterParams *iir = &cp->filter_params[IIR]; - SubStream *s = &m->substream[substr]; if (s->param_presence_flags & PARAM_FIR) if (get_bits1(gbp)) - if (read_filter_params(m, gbp, ch, FIR) < 0) + if (read_filter_params(m, gbp, substr, ch, FIR) < 0) return -1; if (s->param_presence_flags & PARAM_IIR) if (get_bits1(gbp)) - if (read_filter_params(m, gbp, ch, IIR) < 0) + if (read_filter_params(m, gbp, substr, ch, IIR) < 0) return -1; if (fir->order + iir->order > 8) { @@ -697,7 +700,7 @@ static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp, if (s->param_presence_flags & PARAM_QUANTSTEP) if (get_bits1(gbp)) for (ch = 0; ch <= s->max_channel; ch++) { - ChannelParams *cp = &m->channel_params[ch]; + ChannelParams *cp = &s->channel_params[ch]; s->quant_step_size[ch] = get_bits(gbp, 4); @@ -721,12 +724,12 @@ static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel) { SubStream *s = &m->substream[substr]; - const int32_t *fircoeff = m->channel_params[channel].coeff[FIR]; + const int32_t *fircoeff = s->channel_params[channel].coeff[FIR]; int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER]; int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE; int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE; - FilterParams *fir = &m->channel_params[channel].filter_params[FIR]; - FilterParams *iir = &m->channel_params[channel].filter_params[IIR]; + FilterParams *fir = &s->channel_params[channel].filter_params[FIR]; + FilterParams *iir = &s->channel_params[channel].filter_params[IIR]; unsigned int filter_shift = fir->shift; int32_t mask = MSB_MASK(s->quant_step_size[channel]); diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c index 9bc5e2035d..4af56abe37 100644 --- a/libavcodec/motionpixels.c +++ b/libavcodec/motionpixels.c @@ -53,6 +53,11 @@ static av_cold int mp_decode_init(AVCodecContext *avctx) { MotionPixelsContext *mp = avctx->priv_data; + if(avctx->extradata_size < 2){ + av_log(avctx, AV_LOG_ERROR, "extradata too small\n"); + return AVERROR_INVALIDDATA; + } + motionpixels_tableinit(); mp->avctx = avctx; dsputil_init(&mp->dsp, avctx); @@ -188,10 +193,13 @@ static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y) p = mp_get_yuv_from_rgb(mp, x - 1, y); } else { p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb)); + p.y = av_clip(p.y, 0, 31); if ((x & 3) == 0) { if ((y & 3) == 0) { p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb)); + p.v = av_clip(p.v, -32, 31); p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb)); + p.u = av_clip(p.u, -32, 31); mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p; } else { p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v; @@ -215,9 +223,12 @@ static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb) p = mp_get_yuv_from_rgb(mp, 0, y); } else { p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb)); + p.y = av_clip(p.y, 0, 31); if ((y & 3) == 0) { p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb)); + p.v = av_clip(p.v, -32, 31); p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb)); + p.u = av_clip(p.u, -32, 31); } mp->vpt[y] = p; mp_set_rgb_from_yuv(mp, 0, y, &p); diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c index 40383a8143..25fad80284 100644 --- a/libavcodec/mpegaudiodec.c +++ b/libavcodec/mpegaudiodec.c @@ -1315,23 +1315,32 @@ static int mp_decode_layer2(MPADecodeContext *s) return 3 * 12; } -static inline void lsf_sf_expand(int *slen, +#define SPLIT(dst,sf,n)\ + if(n==3){\ + int m= (sf*171)>>9;\ + dst= sf - 3*m;\ + sf=m;\ + }else if(n==4){\ + dst= sf&3;\ + sf>>=2;\ + }else if(n==5){\ + int m= (sf*205)>>10;\ + dst= sf - 5*m;\ + sf=m;\ + }else if(n==6){\ + int m= (sf*171)>>10;\ + dst= sf - 6*m;\ + sf=m;\ + }else{\ + dst=0;\ + } + +static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2, int n3) { - if (n3) { - slen[3] = sf % n3; - sf /= n3; - } else { - slen[3] = 0; - } - if (n2) { - slen[2] = sf % n2; - sf /= n2; - } else { - slen[2] = 0; - } - slen[1] = sf % n1; - sf /= n1; + SPLIT(slen[3], sf, n3) + SPLIT(slen[2], sf, n2) + SPLIT(slen[1], sf, n1) slen[0] = sf; } diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index e11fee8ffc..695290abc2 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -282,9 +282,10 @@ int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){ } FF_ALLOCZ_OR_GOTO(s->avctx, pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2, fail) //the +2 is for the slice end check - FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table , mb_array_size * sizeof(uint8_t) , fail) + FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base , (big_mb_num + s->mb_stride) * sizeof(uint8_t) , fail) FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail) pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1; + pic->qscale_table = pic->qscale_table_base + 2*s->mb_stride + 1; if(s->out_format == FMT_H264){ for(i=0; i<2; i++){ FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail) @@ -335,7 +336,7 @@ static void free_picture(MpegEncContext *s, Picture *pic){ av_freep(&pic->mc_mb_var); av_freep(&pic->mb_mean); av_freep(&pic->mbskip_table); - av_freep(&pic->qscale_table); + av_freep(&pic->qscale_table_base); av_freep(&pic->mb_type_base); av_freep(&pic->dct_coeff); av_freep(&pic->pan_scan); diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 8cd20b7036..9a4912cc40 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -86,6 +86,7 @@ typedef struct Picture{ * halfpel luma planes. */ uint8_t *interpolated[3]; + int8_t *qscale_table_base; int16_t (*motion_val_base[2])[2]; uint32_t *mb_type_base; #define MB_TYPE_INTRA MB_TYPE_INTRA4x4 //default mb_type if there is just one type diff --git a/libavcodec/mpegvideo_common.h b/libavcodec/mpegvideo_common.h index 73106664f9..258f7cdaa7 100644 --- a/libavcodec/mpegvideo_common.h +++ b/libavcodec/mpegvideo_common.h @@ -727,7 +727,8 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s, 0, 0, 0, ref_picture, pix_op, qpix_op, s->mv[dir][0][0], s->mv[dir][0][1], 16); - }else if(!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) && s->mspel){ + } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) && + s->mspel && s->codec_id == CODEC_ID_WMV2) { ff_mspel_motion(s, dest_y, dest_cb, dest_cr, ref_picture, pix_op, s->mv[dir][0][0], s->mv[dir][0][1], 16); diff --git a/libavcodec/nuv.c b/libavcodec/nuv.c index 791f450913..6f832cc27a 100644 --- a/libavcodec/nuv.c +++ b/libavcodec/nuv.c @@ -184,17 +184,18 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, } if (c->codec_frameheader) { int w, h, q; - if (buf_size < 12) { + if (buf_size < RTJPEG_HEADER_SIZE || buf[4] != RTJPEG_HEADER_SIZE || + buf[5] != RTJPEG_FILE_VERSION) { av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n"); - return -1; + return AVERROR_INVALIDDATA; } w = AV_RL16(&buf[6]); h = AV_RL16(&buf[8]); q = buf[10]; if (!codec_reinit(avctx, w, h, q)) return -1; - buf = &buf[12]; - buf_size -= 12; + buf = &buf[RTJPEG_HEADER_SIZE]; + buf_size -= RTJPEG_HEADER_SIZE; } if (keyframe && c->pic.data[0]) diff --git a/libavcodec/options.c b/libavcodec/options.c index 6835352c30..97d705ebc0 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -488,9 +488,6 @@ int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src) dest->slice_offset = NULL; dest->internal_buffer = NULL; dest->hwaccel = NULL; - dest->execute = NULL; - dest->execute2 = NULL; - dest->reget_buffer = NULL; dest->thread_opaque = NULL; /* reallocate values that should be allocated separately */ diff --git a/libavcodec/parser.c b/libavcodec/parser.c index 864b5f2d2a..1d8eb3a36c 100644 --- a/libavcodec/parser.c +++ b/libavcodec/parser.c @@ -271,8 +271,10 @@ int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_s if(next == END_NOT_FOUND){ void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); - if(!new_buffer) + if(!new_buffer) { + pc->index = 0; return AVERROR(ENOMEM); + } pc->buffer = new_buffer; memcpy(&pc->buffer[pc->index], *buf, *buf_size); pc->index += *buf_size; @@ -285,9 +287,11 @@ int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_s /* append to buffer */ if(pc->index){ void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); - - if(!new_buffer) + if(!new_buffer) { + pc->overread_index = + pc->index = 0; return AVERROR(ENOMEM); + } pc->buffer = new_buffer; memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); pc->index = 0; diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 03859a9913..24b0fd4cc7 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -487,9 +487,11 @@ static int decode_frame(AVCodecContext *avctx, } else if (s->bit_depth == 1 && s->color_type == PNG_COLOR_TYPE_GRAY) { avctx->pix_fmt = PIX_FMT_MONOBLACK; - } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) { + } else if (s->bit_depth == 8 && + s->color_type == PNG_COLOR_TYPE_PALETTE) { avctx->pix_fmt = PIX_FMT_PAL8; - } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { + } else if (s->bit_depth == 8 && + s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { avctx->pix_fmt = PIX_FMT_Y400A; } else { goto fail; diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c index 6451fbe91f..946f7d29f7 100644 --- a/libavcodec/qdm2.c +++ b/libavcodec/qdm2.c @@ -75,6 +75,7 @@ do { \ #define SAMPLES_NEEDED_2(why) \ av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why); +#define QDM2_MAX_FRAME_SIZE 512 typedef int8_t sb_int8_array[2][30][64]; @@ -167,7 +168,7 @@ typedef struct { /// I/O data const uint8_t *compressed_data; int compressed_size; - float output_buffer[1024]; + float output_buffer[QDM2_MAX_FRAME_SIZE * 2]; /// Synthesis filter DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512*2]; @@ -882,9 +883,13 @@ static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int l break; case 30: - if (BITS_LEFT(length,gb) >= 4) - samples[0] = type30_dequant[qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1)]; - else + if (BITS_LEFT(length,gb) >= 4) { + unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1); + if (index < FF_ARRAY_ELEMS(type30_dequant)) { + samples[0] = type30_dequant[index]; + } else + samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); + } else samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); run = 1; @@ -898,8 +903,12 @@ static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int l type34_predictor = samples[0]; type34_first = 0; } else { - samples[0] = type34_delta[qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1)] / type34_div + type34_predictor; - type34_predictor = samples[0]; + unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1); + if (index < FF_ARRAY_ELEMS(type34_delta)) { + samples[0] = type34_delta[index] / type34_div + type34_predictor; + type34_predictor = samples[0]; + } else + samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); } } else { samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); @@ -1355,6 +1364,8 @@ static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext * return; local_int_14 = (offset >> local_int_8); + if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table)) + return; if (q->nb_channels > 1) { channel = get_bits1(gb); @@ -1799,6 +1810,8 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx) avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata); extradata += 4; + if (s->channels > MPA_MAX_CHANNELS) + return AVERROR_INVALIDDATA; avctx->sample_rate = AV_RB32(extradata); extradata += 4; @@ -1820,6 +1833,8 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx) // something like max decodable tones s->group_order = av_log2(s->group_size) + 1; s->frame_size = s->group_size / 16; // 16 iterations per super block + if (s->frame_size > QDM2_MAX_FRAME_SIZE) + return AVERROR_INVALIDDATA; s->sub_sampling = s->fft_order - 7; s->frequency_range = 255 / (1 << (2 - s->sub_sampling)); @@ -1883,7 +1898,7 @@ static av_cold int qdm2_decode_close(AVCodecContext *avctx) } -static void qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out) +static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out) { int ch, i; const int frame_size = (q->frame_size * q->channels); @@ -1919,7 +1934,7 @@ static void qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out) if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) { SAMPLES_NEEDED_2("has errors, and C list is not empty") - return; + return -1; } } @@ -1940,6 +1955,8 @@ static void qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out) out[i] = value; } + + return 0; } @@ -1950,25 +1967,33 @@ static int qdm2_decode_frame(AVCodecContext *avctx, const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; QDM2Context *s = avctx->priv_data; + int16_t *out = data; + int i, out_size; if(!buf) return 0; if(buf_size < s->checksum_size) return -1; - *data_size = s->channels * s->frame_size * sizeof(int16_t); + out_size = 16 * s->channels * s->frame_size * + av_get_bits_per_sample_format(avctx->sample_fmt)/8; + if (*data_size < out_size) { + av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n"); + return AVERROR(EINVAL); + } av_log(avctx, AV_LOG_DEBUG, "decode(%d): %p[%d] -> %p[%d]\n", buf_size, buf, s->checksum_size, data, *data_size); - qdm2_decode(s, buf, data); - - // reading only when next superblock found - if (s->sub_packet == 0) { - return s->checksum_size; + for (i = 0; i < 16; i++) { + if (qdm2_decode(s, buf, out) < 0) + return -1; + out += s->channels * s->frame_size; } - return 0; + *data_size = out_size; + + return buf_size; } AVCodec qdm2_decoder = diff --git a/libavcodec/rpza.c b/libavcodec/rpza.c index e103f525d2..a1bef87922 100644 --- a/libavcodec/rpza.c +++ b/libavcodec/rpza.c @@ -83,7 +83,7 @@ static void rpza_decode_stream(RpzaContext *s) unsigned short *pixels = (unsigned short *)s->frame.data[0]; int row_ptr = 0; - int pixel_ptr = 0; + int pixel_ptr = -4; int block_ptr; int pixel_x, pixel_y; int total_blocks; @@ -139,6 +139,7 @@ static void rpza_decode_stream(RpzaContext *s) colorA = AV_RB16 (&s->buf[stream_ptr]); stream_ptr += 2; while (n_blocks--) { + ADVANCE_BLOCK() block_ptr = row_ptr + pixel_ptr; for (pixel_y = 0; pixel_y < 4; pixel_y++) { for (pixel_x = 0; pixel_x < 4; pixel_x++){ @@ -147,7 +148,6 @@ static void rpza_decode_stream(RpzaContext *s) } block_ptr += row_inc; } - ADVANCE_BLOCK(); } break; @@ -184,6 +184,7 @@ static void rpza_decode_stream(RpzaContext *s) color4[2] |= ((21 * ta + 11 * tb) >> 5); while (n_blocks--) { + ADVANCE_BLOCK(); block_ptr = row_ptr + pixel_ptr; for (pixel_y = 0; pixel_y < 4; pixel_y++) { index = s->buf[stream_ptr++]; @@ -194,12 +195,12 @@ static void rpza_decode_stream(RpzaContext *s) } block_ptr += row_inc; } - ADVANCE_BLOCK(); } break; /* Fill block with 16 colors */ case 0x00: + ADVANCE_BLOCK(); block_ptr = row_ptr + pixel_ptr; for (pixel_y = 0; pixel_y < 4; pixel_y++) { for (pixel_x = 0; pixel_x < 4; pixel_x++){ @@ -213,7 +214,6 @@ static void rpza_decode_stream(RpzaContext *s) } block_ptr += row_inc; } - ADVANCE_BLOCK(); break; /* Unknown opcode */ diff --git a/libavcodec/rtjpeg.h b/libavcodec/rtjpeg.h index 4bcb9f70ca..73d41f481d 100644 --- a/libavcodec/rtjpeg.h +++ b/libavcodec/rtjpeg.h @@ -25,6 +25,9 @@ #include #include "dsputil.h" +#define RTJPEG_FILE_VERSION 0 +#define RTJPEG_HEADER_SIZE 12 + typedef struct { int w, h; DSPContext *dsp; diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c index b6ca031dfa..af0d16c022 100644 --- a/libavcodec/rv10.c +++ b/libavcodec/rv10.c @@ -454,6 +454,7 @@ static av_cold int rv10_decode_init(AVCodecContext *avctx) s->avctx= avctx; s->out_format = FMT_H263; s->codec_id= avctx->codec_id; + avctx->flags |= CODEC_FLAG_EMU_EDGE; s->orig_width = s->width = avctx->coded_width; s->orig_height= s->height = avctx->coded_height; @@ -558,6 +559,11 @@ static int rv10_decode_packet(AVCodecContext *avctx, if(MPV_frame_start(s, avctx) < 0) return -1; ff_er_frame_start(s); + } else { + if (s->current_picture_ptr->pict_type != s->pict_type) { + av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n"); + return -1; + } } dprintf(avctx, "qscale=%d\n", s->qscale); diff --git a/libavcodec/rv30.c b/libavcodec/rv30.c index 22a5dd5b64..ada3b2aeaf 100644 --- a/libavcodec/rv30.c +++ b/libavcodec/rv30.c @@ -256,6 +256,7 @@ static av_cold int rv30_decode_init(AVCodecContext *avctx) if(avctx->extradata_size - 8 < (r->rpr - 1) * 2){ av_log(avctx, AV_LOG_ERROR, "Insufficient extradata - need at least %d bytes, got %d\n", 6 + r->rpr * 2, avctx->extradata_size); + return AVERROR(EINVAL); } r->parse_slice_header = rv30_parse_slice_header; r->decode_intra_types = rv30_decode_intra_types; diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index 88652f9fe1..e6fa3cfd8f 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -1439,12 +1439,14 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, slice_count = (*buf++) + 1; slices_hdr = buf + 4; buf += 8 * slice_count; + buf_size -= 1 + 8 * slice_count; }else slice_count = avctx->slice_count; //parse first slice header to check whether this frame can be decoded - if(get_slice_offset(avctx, slices_hdr, 0) > buf_size){ - av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n"); + if(get_slice_offset(avctx, slices_hdr, 0) < 0 || + get_slice_offset(avctx, slices_hdr, 0) > buf_size){ + av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n"); return -1; } init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), buf_size-get_slice_offset(avctx, slices_hdr, 0)); @@ -1459,7 +1461,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==FF_B_TYPE) || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=FF_I_TYPE) || avctx->skip_frame >= AVDISCARD_ALL) - return buf_size; + return avpkt->size; /* skip everything if we are in a hurry>=5 */ if(avctx->hurry_up>=5) return buf_size; @@ -1472,8 +1474,8 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, else size= get_slice_offset(avctx, slices_hdr, i+1) - offset; - if(offset > buf_size){ - av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n"); + if(offset < 0 || offset > buf_size || size < 0){ + av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n"); break; } @@ -1494,7 +1496,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, break; } - if(last){ + if(last && s->current_picture_ptr){ if(r->loop_filter) r->loop_filter(r, s->mb_height - 1); ff_er_frame_end(s); @@ -1511,7 +1513,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, } s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...) } - return buf_size; + return avpkt->size; } av_cold int ff_rv34_decode_end(AVCodecContext *avctx) diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h index 2d9f4a519d..1c87a2e564 100644 --- a/libavcodec/sbr.h +++ b/libavcodec/sbr.h @@ -31,6 +31,7 @@ #include #include "fft.h" +#include "aacps.h" /** * Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the previous header. @@ -133,6 +134,7 @@ typedef struct { ///The number of frequency bands in f_master unsigned n_master; SBRData data[2]; + PSContext ps; ///N_Low and N_High respectively, the number of frequency bands for low and high resolution unsigned n[2]; ///Number of noise floor bands @@ -157,7 +159,7 @@ typedef struct { ///QMF output of the HF generator float X_high[64][40][2]; ///QMF values of the reconstructed signal - DECLARE_ALIGNED(16, float, X)[2][2][32][64]; + DECLARE_ALIGNED(16, float, X)[2][2][38][64]; ///Zeroth coefficient used to filter the subband signals float alpha0[64][2]; ///First coefficient used to filter the subband signals @@ -176,7 +178,7 @@ typedef struct { float s_m[7][48]; float gain[7][48]; DECLARE_ALIGNED(16, float, qmf_filter_scratch)[5][64]; - RDFTContext rdft; + FFTContext mdct_ana; FFTContext mdct; } SpectralBandReplication; diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c index 7e17f18e17..49708ef54c 100644 --- a/libavcodec/shorten.c +++ b/libavcodec/shorten.c @@ -82,7 +82,9 @@ typedef struct ShortenContext { int channels; int32_t *decoded[MAX_CHANNELS]; + int32_t *decoded_base[MAX_CHANNELS]; int32_t *offset[MAX_CHANNELS]; + int *coeffs; uint8_t *bitstream; int bitstream_size; int bitstream_index; @@ -112,6 +114,9 @@ static av_cold int shorten_decode_init(AVCodecContext * avctx) static int allocate_buffers(ShortenContext *s) { int i, chan; + int *coeffs; + void *tmp_ptr; + for (chan=0; chanchannels; chan++) { if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){ av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n"); @@ -122,13 +127,26 @@ static int allocate_buffers(ShortenContext *s) return -1; } - s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean)); + tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean)); + if (!tmp_ptr) + return AVERROR(ENOMEM); + s->offset[chan] = tmp_ptr; - s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap)); + tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) * + sizeof(s->decoded_base[0][0])); + if (!tmp_ptr) + return AVERROR(ENOMEM); + s->decoded_base[chan] = tmp_ptr; for (i=0; inwrap; i++) - s->decoded[chan][i] = 0; - s->decoded[chan] += s->nwrap; + s->decoded_base[chan][i] = 0; + s->decoded[chan] = s->decoded_base[chan] + s->nwrap; } + + coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs)); + if (!coeffs) + return AVERROR(ENOMEM); + s->coeffs = coeffs; + return 0; } @@ -147,7 +165,7 @@ static void fix_bitshift(ShortenContext *s, int32_t *buffer) if (s->bitshift != 0) for (i = 0; i < s->blocksize; i++) - buffer[s->nwrap + i] <<= s->bitshift; + buffer[i] <<= s->bitshift; } @@ -253,7 +271,7 @@ static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, i static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order) { int sum, i, j; - int coeffs[pred_order]; + int *coeffs = s->coeffs; for (i=0; igb, LPCQUANT); @@ -277,8 +295,15 @@ static int shorten_decode_frame(AVCodecContext *avctx, int i, input_buf_size = 0; int16_t *samples = data; if(s->max_framesize == 0){ + void *tmp_ptr; s->max_framesize= 1024; // should hopefully be enough for the first header - s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize); + tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, + s->max_framesize); + if (!tmp_ptr) { + av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n"); + return AVERROR(ENOMEM); + } + s->bitstream = tmp_ptr; } if(1 && s->max_framesize){//FIXME truncated @@ -427,6 +452,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, case FN_QLPC: { int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE); + if (pred_order > s->nwrap) { + av_log(avctx, AV_LOG_ERROR, + "invalid pred_order %d\n", + pred_order); + return -1; + } for (i=0; idecoded[channel][i - pred_order] -= coffset; decode_subframe_lpc(s, channel, residual_size, pred_order); @@ -471,9 +502,15 @@ static int shorten_decode_frame(AVCodecContext *avctx, case FN_BITSHIFT: s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE); break; - case FN_BLOCKSIZE: - s->blocksize = get_uint(s, av_log2(s->blocksize)); + case FN_BLOCKSIZE: { + int blocksize = get_uint(s, av_log2(s->blocksize)); + if (blocksize > s->blocksize) { + av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n"); + return AVERROR_PATCHWELCOME; + } + s->blocksize = blocksize; break; + } case FN_QUIT: *data_size = 0; return buf_size; @@ -510,11 +547,12 @@ static av_cold int shorten_decode_close(AVCodecContext *avctx) int i; for (i = 0; i < s->channels; i++) { - s->decoded[i] -= s->nwrap; - av_freep(&s->decoded[i]); + s->decoded[i] = NULL; + av_freep(&s->decoded_base[i]); av_freep(&s->offset[i]); } av_freep(&s->bitstream); + av_freep(&s->coeffs); return 0; } diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c index 1d85f68907..e2c1a8776c 100644 --- a/libavcodec/smacker.c +++ b/libavcodec/smacker.c @@ -133,11 +133,13 @@ static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx return -1; } b1 = get_bits_count(gb); - i1 = get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3); + i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0; b1 = get_bits_count(gb) - b1; b2 = get_bits_count(gb); - i2 = get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3); + i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0; b2 = get_bits_count(gb) - b2; + if (i1 < 0 || i2 < 0) + return -1; val = ctx->recode1[i1] | (ctx->recode2[i2] << 8); if(val == ctx->escapes[0]) { ctx->last[0] = hc->current; @@ -289,7 +291,8 @@ static int decode_header_trees(SmackVContext *smk) { smk->mmap_tbl[0] = 0; smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; } else { - smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); + if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size)) + return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); @@ -297,7 +300,8 @@ static int decode_header_trees(SmackVContext *smk) { smk->mclr_tbl[0] = 0; smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; } else { - smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); + if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size)) + return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); @@ -305,7 +309,8 @@ static int decode_header_trees(SmackVContext *smk) { smk->full_tbl[0] = 0; smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; } else { - smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); + if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size)) + return -1; } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); @@ -313,7 +318,8 @@ static int decode_header_trees(SmackVContext *smk) { smk->type_tbl[0] = 0; smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; } else { - smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); + if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size)) + return -1; } return 0; @@ -523,8 +529,8 @@ static av_cold int decode_init(AVCodecContext *avctx) return -1; } - decode_header_trees(c); - + if (decode_header_trees(c)) + return -1; return 0; } @@ -619,9 +625,9 @@ static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, if(bits) { //decode 16-bit data for(i = stereo; i >= 0; i--) pred[i] = bswap_16(get_bits(&gb, 16)); - for(i = 0; i < stereo; i++) + for(i = 0; i <= stereo; i++) *samples++ = pred[i]; - for(i = 0; i < unp_size / 2; i++) { + for(; i < unp_size / 2; i++) { if(i & stereo) { if(vlc[2].table) res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); @@ -653,9 +659,9 @@ static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, } else { //8-bit data for(i = stereo; i >= 0; i--) pred[i] = get_bits(&gb, 8); - for(i = 0; i < stereo; i++) + for(i = 0; i <= stereo; i++) *samples8++ = pred[i]; - for(i = 0; i < unp_size; i++) { + for(; i < unp_size; i++) { if(i & stereo){ if(vlc[1].table) res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); diff --git a/libavcodec/sp5xdec.c b/libavcodec/sp5xdec.c index 754926a4ce..0899902d5d 100644 --- a/libavcodec/sp5xdec.c +++ b/libavcodec/sp5xdec.c @@ -90,7 +90,6 @@ static int sp5x_decode_frame(AVCodecContext *avctx, recoded[j++] = 0xFF; recoded[j++] = 0xD9; - avctx->flags &= ~CODEC_FLAG_EMU_EDGE; av_init_packet(&avpkt_recoded); avpkt_recoded.data = recoded; avpkt_recoded.size = j; @@ -216,6 +215,6 @@ AVCodec amv_decoder = { NULL, ff_mjpeg_decode_end, sp5x_decode_frame, - CODEC_CAP_DR1, + 0, .long_name = NULL_IF_CONFIG_SMALL("AMV Video"), }; diff --git a/libavcodec/svq1dec.c b/libavcodec/svq1dec.c index 2aa28ab0b0..8c6f0c8f62 100644 --- a/libavcodec/svq1dec.c +++ b/libavcodec/svq1dec.c @@ -678,6 +678,7 @@ static int svq1_decode_frame(AVCodecContext *avctx, #endif return result; } + avcodec_set_dimensions(avctx, s->width, s->height); //FIXME this avoids some confusion for "B frames" without 2 references //this should be removed after libavcodec can handle more flexible picture types & ordering diff --git a/libavcodec/tableprint.c b/libavcodec/tableprint.c index e39606bb15..0f422eb888 100644 --- a/libavcodec/tableprint.c +++ b/libavcodec/tableprint.c @@ -33,6 +33,7 @@ WRITE_1D_FUNC(float, float, "%.18e", 3) WRITE_2D_FUNC(int8, int8_t) WRITE_2D_FUNC(uint8, uint8_t) WRITE_2D_FUNC(uint32, uint32_t) +WRITE_2D_FUNC(float, float) void write_fileheader(void) { printf("/* This file was generated by libavcodec/tableprint */\n"); diff --git a/libavcodec/tableprint.h b/libavcodec/tableprint.h index d81af97e95..76ab8028b5 100644 --- a/libavcodec/tableprint.h +++ b/libavcodec/tableprint.h @@ -66,6 +66,7 @@ void write_float_array (const float *, int); void write_int8_2d_array (const void *, int, int); void write_uint8_2d_array (const void *, int, int); void write_uint32_2d_array(const void *, int, int); +void write_float_2d_array (const void *, int, int); /** \} */ // end of printfuncs group /** Write a standard file header */ diff --git a/libavcodec/truemotion1.c b/libavcodec/truemotion1.c index 4306917912..f8c98a0502 100644 --- a/libavcodec/truemotion1.c +++ b/libavcodec/truemotion1.c @@ -518,6 +518,10 @@ hres,vres,i,i%vres (0 < i < 4) } #define APPLY_C_PREDICTOR() \ + if(index > 1023){\ + av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \ + return; \ + }\ predictor_pair = s->c_predictor_table[index]; \ horiz_pred += (predictor_pair >> 1); \ if (predictor_pair & 1) { \ @@ -535,6 +539,10 @@ hres,vres,i,i%vres (0 < i < 4) index++; #define APPLY_C_PREDICTOR_24() \ + if(index > 1023){\ + av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \ + return; \ + }\ predictor_pair = s->c_predictor_table[index]; \ horiz_pred += (predictor_pair >> 1); \ if (predictor_pair & 1) { \ @@ -553,6 +561,10 @@ hres,vres,i,i%vres (0 < i < 4) #define APPLY_Y_PREDICTOR() \ + if(index > 1023){\ + av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \ + return; \ + }\ predictor_pair = s->y_predictor_table[index]; \ horiz_pred += (predictor_pair >> 1); \ if (predictor_pair & 1) { \ @@ -570,6 +582,10 @@ hres,vres,i,i%vres (0 < i < 4) index++; #define APPLY_Y_PREDICTOR_24() \ + if(index > 1023){\ + av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \ + return; \ + }\ predictor_pair = s->y_predictor_table[index]; \ horiz_pred += (predictor_pair >> 1); \ if (predictor_pair & 1) { \ diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 87facec8f4..014427d59f 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -724,14 +724,18 @@ av_cold int avcodec_close(AVCodecContext *avctx) AVCodec *avcodec_find_encoder(enum CodecID id) { - AVCodec *p; + AVCodec *p, *experimental=NULL; p = first_avcodec; while (p) { - if (p->encode != NULL && p->id == id) - return p; + if (p->encode != NULL && p->id == id) { + if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) { + experimental = p; + } else + return p; + } p = p->next; } - return NULL; + return experimental; } AVCodec *avcodec_find_encoder_by_name(const char *name) diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c index 7d00072b7a..1ed79af0bd 100644 --- a/libavcodec/vc1.c +++ b/libavcodec/vc1.c @@ -467,8 +467,8 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb) if(ar && ar < 14){ v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar]; }else if(ar == 15){ - w = get_bits(gb, 8); - h = get_bits(gb, 8); + w = get_bits(gb, 8) + 1; + h = get_bits(gb, 8) + 1; v->s.avctx->sample_aspect_ratio = (AVRational){w, h}; } av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den); diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index 52392c3f76..8d31e8bc1e 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -1365,7 +1365,7 @@ static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, if (index != vc1_ac_sizes[codingset] - 1) { run = vc1_index_decode_table[codingset][index][0]; level = vc1_index_decode_table[codingset][index][1]; - lst = index >= vc1_last_decode_table[codingset]; + lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0; if(get_bits1(gb)) level = -level; } else { @@ -3328,6 +3328,7 @@ AVCodec vc1_decoder = { vc1_decode_frame, CODEC_CAP_DR1 | CODEC_CAP_DELAY, NULL, + .flush = ff_mpeg_flush, .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"), .pix_fmts = ff_hwaccel_pixfmt_list_420 }; @@ -3344,6 +3345,7 @@ AVCodec wmv3_decoder = { vc1_decode_frame, CODEC_CAP_DR1 | CODEC_CAP_DELAY, NULL, + .flush = ff_mpeg_flush, .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"), .pix_fmts = ff_hwaccel_pixfmt_list_420 }; diff --git a/libavcodec/vmdav.c b/libavcodec/vmdav.c index 4914d2a09a..2c97d3f707 100644 --- a/libavcodec/vmdav.c +++ b/libavcodec/vmdav.c @@ -72,9 +72,11 @@ typedef struct VmdVideoContext { #define QUEUE_SIZE 0x1000 #define QUEUE_MASK 0x0FFF -static void lz_unpack(const unsigned char *src, unsigned char *dest, int dest_len) +static void lz_unpack(const unsigned char *src, int src_len, + unsigned char *dest, int dest_len) { const unsigned char *s; + unsigned int s_len; unsigned char *d; unsigned char *d_end; unsigned char queue[QUEUE_SIZE]; @@ -87,13 +89,16 @@ static void lz_unpack(const unsigned char *src, unsigned char *dest, int dest_le unsigned int i, j; s = src; + s_len = src_len; d = dest; d_end = d + dest_len; dataleft = AV_RL32(s); - s += 4; + s += 4; s_len -= 4; memset(queue, 0x20, QUEUE_SIZE); + if (s_len < 4) + return; if (AV_RL32(s) == 0x56781234) { - s += 4; + s += 4; s_len -= 4; qpos = 0x111; speclen = 0xF + 3; } else { @@ -101,32 +106,41 @@ static void lz_unpack(const unsigned char *src, unsigned char *dest, int dest_le speclen = 100; /* no speclen */ } - while (dataleft > 0) { - tag = *s++; + while (dataleft > 0 && s_len > 0) { + tag = *s++; s_len--; if ((tag == 0xFF) && (dataleft > 8)) { - if (d + 8 > d_end) + if (d + 8 > d_end || s_len < 8) return; for (i = 0; i < 8; i++) { queue[qpos++] = *d++ = *s++; qpos &= QUEUE_MASK; } + s_len -= 8; dataleft -= 8; } else { for (i = 0; i < 8; i++) { if (dataleft == 0) break; if (tag & 0x01) { - if (d + 1 > d_end) + if (d + 1 > d_end || s_len < 1) return; queue[qpos++] = *d++ = *s++; qpos &= QUEUE_MASK; dataleft--; + s_len--; } else { + if (s_len < 2) + return; chainofs = *s++; chainofs |= ((*s & 0xF0) << 4); chainlen = (*s++ & 0x0F) + 3; - if (chainlen == speclen) + s_len -= 2; + if (chainlen == speclen) { + if (s_len < 1) + return; chainlen = *s++ + 0xF + 3; + s_len--; + } if (d + chainlen > d_end) return; for (j = 0; j < chainlen; j++) { @@ -143,7 +157,7 @@ static void lz_unpack(const unsigned char *src, unsigned char *dest, int dest_le } static int rle_unpack(const unsigned char *src, unsigned char *dest, - int src_len, int dest_len) + int src_count, int src_size, int dest_len) { const unsigned char *ps; unsigned char *pd; @@ -152,31 +166,40 @@ static int rle_unpack(const unsigned char *src, unsigned char *dest, ps = src; pd = dest; - if (src_len & 1) + if (src_count & 1) { + if (src_size < 1) + return 0; *pd++ = *ps++; + src_size--; + } - src_len >>= 1; + src_count >>= 1; i = 0; do { + if (src_size < 1) + break; l = *ps++; + src_size--; if (l & 0x80) { l = (l & 0x7F) * 2; - if (pd + l > dest_end) + if (pd + l > dest_end || src_size < l) return ps - src; memcpy(pd, ps, l); ps += l; + src_size -= l; pd += l; } else { - if (pd + i > dest_end) + if (pd + i > dest_end || src_size < 2) return ps - src; for (i = 0; i < l; i++) { *pd++ = ps[0]; *pd++ = ps[1]; } ps += 2; + src_size -= 2; } i += l; - } while (i < src_len); + } while (i < src_count); return ps - src; } @@ -191,6 +214,7 @@ static void vmd_decode(VmdVideoContext *s) const unsigned char *p = s->buf + 16; const unsigned char *pb; + unsigned int pb_size; unsigned char meth; unsigned char *dp; /* pointer to current frame */ unsigned char *pp; /* pointer to previous frame */ @@ -205,6 +229,16 @@ static void vmd_decode(VmdVideoContext *s) frame_y = AV_RL16(&s->buf[8]); frame_width = AV_RL16(&s->buf[10]) - frame_x + 1; frame_height = AV_RL16(&s->buf[12]) - frame_y + 1; + if (frame_x < 0 || frame_width < 0 || + frame_x >= s->avctx->width || + frame_width > s->avctx->width || + frame_x + frame_width > s->avctx->width) + return; + if (frame_y < 0 || frame_height < 0 || + frame_y >= s->avctx->height || + frame_height > s->avctx->height || + frame_y + frame_height > s->avctx->height) + return; if ((frame_width == s->avctx->width && frame_height == s->avctx->height) && (frame_x || frame_y)) { @@ -217,8 +251,9 @@ static void vmd_decode(VmdVideoContext *s) /* if only a certain region will be updated, copy the entire previous * frame before the decode */ - if (frame_x || frame_y || (frame_width != s->avctx->width) || - (frame_height != s->avctx->height)) { + if (s->prev_frame.data[0] && + (frame_x || frame_y || (frame_width != s->avctx->width) || + (frame_height != s->avctx->height))) { memcpy(s->frame.data[0], s->prev_frame.data[0], s->avctx->height * s->frame.linesize[0]); @@ -236,14 +271,19 @@ static void vmd_decode(VmdVideoContext *s) } s->size -= (256 * 3 + 2); } - if (s->size >= 0) { + if (s->size > 0) { /* originally UnpackFrame in VAG's code */ pb = p; - meth = *pb++; + pb_size = s->buf + s->size - pb; + if (pb_size < 1) + return; + meth = *pb++; pb_size--; if (meth & 0x80) { - lz_unpack(pb, s->unpack_buffer, s->unpack_buffer_size); + lz_unpack(pb, pb_size, + s->unpack_buffer, s->unpack_buffer_size); meth &= 0x7F; pb = s->unpack_buffer; + pb_size = s->unpack_buffer_size; } dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x]; @@ -254,17 +294,21 @@ static void vmd_decode(VmdVideoContext *s) for (i = 0; i < frame_height; i++) { ofs = 0; do { + if (pb_size < 1) + return; len = *pb++; + pb_size--; if (len & 0x80) { len = (len & 0x7F) + 1; - if (ofs + len > frame_width) + if (ofs + len > frame_width || pb_size < len) return; memcpy(&dp[ofs], pb, len); pb += len; + pb_size -= len; ofs += len; } else { /* interframe pixel copy */ - if (ofs + len + 1 > frame_width) + if (ofs + len + 1 > frame_width || !s->prev_frame.data[0]) return; memcpy(&dp[ofs], &pp[ofs], len + 1); ofs += len + 1; @@ -282,8 +326,11 @@ static void vmd_decode(VmdVideoContext *s) case 2: for (i = 0; i < frame_height; i++) { + if (pb_size < frame_width) + return; memcpy(dp, pb, frame_width); pb += frame_width; + pb_size -= frame_width; dp += s->frame.linesize[0]; pp += s->prev_frame.linesize[0]; } @@ -293,18 +340,27 @@ static void vmd_decode(VmdVideoContext *s) for (i = 0; i < frame_height; i++) { ofs = 0; do { + if (pb_size < 1) + return; len = *pb++; + pb_size--; if (len & 0x80) { len = (len & 0x7F) + 1; + if (pb_size < 1) + return; if (*pb++ == 0xFF) - len = rle_unpack(pb, &dp[ofs], len, frame_width - ofs); - else + len = rle_unpack(pb, &dp[ofs], len, pb_size, frame_width - ofs); + else { + if (pb_size < len) + return; memcpy(&dp[ofs], pb, len); + } pb += len; + pb_size -= 1 + len; ofs += len; } else { /* interframe pixel copy */ - if (ofs + len + 1 > frame_width) + if (ofs + len + 1 > frame_width || !s->prev_frame.data[0]) return; memcpy(&dp[ofs], &pp[ofs], len + 1); ofs += len + 1; diff --git a/libavcodec/vorbis.c b/libavcodec/vorbis.c index 47388d8302..5f0ddda32f 100644 --- a/libavcodec/vorbis.c +++ b/libavcodec/vorbis.c @@ -123,7 +123,8 @@ int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num) return 0; } -void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) +int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext, + vorbis_floor1_entry *list, int values) { int i; list[0].sort = 0; @@ -147,6 +148,11 @@ void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) for (i = 0; i < values - 1; i++) { int j; for (j = i + 1; j < values; j++) { + if (list[i].x == list[j].x) { + av_log(avccontext, AV_LOG_ERROR, + "Duplicate value found in floor 1 X coordinates\n"); + return AVERROR_INVALIDDATA; + } if (list[list[i].sort].x > list[list[j].sort].x) { int tmp = list[i].sort; list[i].sort = list[j].sort; @@ -154,9 +160,10 @@ void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) } } } + return 0; } -static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1, +static inline void render_line_unrolled(intptr_t x, uint8_t y, int x1, intptr_t sy, int ady, int adx, float *buf) { @@ -179,7 +186,7 @@ static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1, } } -static void render_line(int x0, int y0, int x1, int y1, float *buf) +static void render_line(int x0, uint8_t y0, int x1, int y1, float *buf) { int dy = y1 - y0; int adx = x1 - x0; @@ -189,10 +196,10 @@ static void render_line(int x0, int y0, int x1, int y1, float *buf) if (ady*2 <= adx) { // optimized common case render_line_unrolled(x0, y0, x1, sy, ady, adx, buf); } else { - int base = dy / adx; - int x = x0; - int y = y0; - int err = -adx; + int base = dy / adx; + int x = x0; + uint8_t y = y0; + int err = -adx; ady -= FFABS(base) * adx; while (++x < x1) { y += base; @@ -210,7 +217,8 @@ void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values, uint_fast16_t *y_list, int *flag, int multiplier, float *out, int samples) { - int lx, ly, i; + int lx, i; + uint8_t ly; lx = 0; ly = y_list[0] * multiplier; for (i = 1; i < values; i++) { diff --git a/libavcodec/vorbis.h b/libavcodec/vorbis.h index ce9bead425..18a826eee8 100644 --- a/libavcodec/vorbis.h +++ b/libavcodec/vorbis.h @@ -35,7 +35,8 @@ typedef struct { uint_fast16_t high; } vorbis_floor1_entry; -void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values); +int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext, + vorbis_floor1_entry *list, int values); unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n); // x^(1/n) int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num); void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values, diff --git a/libavcodec/vorbis_dec.c b/libavcodec/vorbis_dec.c index 8c56400c77..a2ae854699 100644 --- a/libavcodec/vorbis_dec.c +++ b/libavcodec/vorbis_dec.c @@ -61,8 +61,8 @@ typedef struct vorbis_floor0_s vorbis_floor0; typedef struct vorbis_floor1_s vorbis_floor1; struct vorbis_context_s; typedef -uint_fast8_t (* vorbis_floor_decode_func) - (struct vorbis_context_s *, vorbis_floor_data *, float *); +int (* vorbis_floor_decode_func) + (struct vorbis_context_s *, vorbis_floor_data *, float *); typedef struct { uint_fast8_t floor_type; vorbis_floor_decode_func decode; @@ -453,11 +453,11 @@ static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc) // Process floors part -static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc, - vorbis_floor_data *vfu, float *vec); +static int vorbis_floor0_decode(vorbis_context *vc, + vorbis_floor_data *vfu, float *vec); static void create_map(vorbis_context *vc, uint_fast8_t floor_number); -static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, - vorbis_floor_data *vfu, float *vec); +static int vorbis_floor1_decode(vorbis_context *vc, + vorbis_floor_data *vfu, float *vec); static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) { GetBitContext *gb = &vc->gb; @@ -477,6 +477,7 @@ static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) if (floor_setup->floor_type == 1) { uint_fast8_t maximum_class = 0; uint_fast8_t rangebits; + uint_fast32_t rangemax; uint_fast16_t floor1_values = 2; floor_setup->decode = vorbis_floor1_decode; @@ -530,8 +531,15 @@ static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) rangebits = get_bits(gb, 4); + rangemax = (1 << rangebits); + if (rangemax > vc->blocksize[1] / 2) { + av_log(vc->avccontext, AV_LOG_ERROR, + "Floor value is too large for blocksize: %d (%d)\n", + rangemax, vc->blocksize[1] / 2); + return -1; + } floor_setup->data.t1.list[0].x = 0; - floor_setup->data.t1.list[1].x = (1 << rangebits); + floor_setup->data.t1.list[1].x = rangemax; for (j = 0; j < floor_setup->data.t1.partitions; ++j) { for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) { @@ -542,7 +550,11 @@ static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) } // Precalculate order of x coordinates - needed for decode - ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim); + if (ff_vorbis_ready_floor1_list(vc->avccontext, + floor_setup->data.t1.list, + floor_setup->data.t1.x_list_dim)) { + return AVERROR_INVALIDDATA; + } } else if (floor_setup->floor_type == 0) { uint_fast8_t max_codebook_dim = 0; @@ -648,7 +660,7 @@ static int vorbis_parse_setup_hdr_residues(vorbis_context *vc) res_setup->partition_size = get_bits(gb, 24) + 1; /* Validations to prevent a buffer overflow later. */ if (res_setup->begin>res_setup->end || - res_setup->end>vc->blocksize[1] / (res_setup->type == 2 ? 1 : 2) || + res_setup->end > (res_setup->type == 2 ? vc->avccontext->channels : 1) * vc->blocksize[1] / 2 || (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) { av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2); return -1; @@ -1002,8 +1014,8 @@ static av_cold int vorbis_decode_init(AVCodecContext *avccontext) // Read and decode floor -static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc, - vorbis_floor_data *vfu, float *vec) +static int vorbis_floor0_decode(vorbis_context *vc, + vorbis_floor_data *vfu, float *vec) { vorbis_floor0 *vf = &vfu->t0; float *lsp = vf->lsp; @@ -1027,6 +1039,9 @@ static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc, } AV_DEBUG("floor0 dec: booknumber: %u\n", book_idx); codebook = vc->codebooks[vf->book_list[book_idx]]; + /* Invalid codebook! */ + if (!codebook.codevectors) + return -1; while (lsp_lenorder) { int vec_off; @@ -1112,8 +1127,8 @@ static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc, return 0; } -static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, - vorbis_floor_data *vfu, float *vec) +static int vorbis_floor1_decode(vorbis_context *vc, + vorbis_floor_data *vfu, float *vec) { vorbis_floor1 *vf = &vfu->t1; GetBitContext *gb = &vc->gb; @@ -1251,6 +1266,7 @@ static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, uint_fast8_t *do_not_decode, float *vec, uint_fast16_t vlen, + unsigned ch_left, int vr_type) { GetBitContext *gb = &vc->gb; @@ -1262,6 +1278,7 @@ static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, uint_fast8_t ch_used; uint_fast8_t i,j,l; uint_fast16_t k; + unsigned max_output = (ch - 1) * vlen; if (vr_type == 2) { for (j = 1; j < ch; ++j) @@ -1269,8 +1286,15 @@ static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, if (do_not_decode[0]) return 0; ch_used = 1; + max_output += vr->end / ch; } else { ch_used = ch; + max_output += vr->end; + } + + if (max_output > ch_left * vlen) { + av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n"); + return -1; } AV_DEBUG(" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c); @@ -1392,14 +1416,16 @@ static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr, uint_fast8_t ch, uint_fast8_t *do_not_decode, - float *vec, uint_fast16_t vlen) + float *vec, uint_fast16_t vlen, + unsigned ch_left) + { if (vr->type == 2) - return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 2); + return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2); else if (vr->type == 1) - return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 1); + return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1); else if (vr->type == 0) - return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 0); + return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0); else { av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n"); return -1; @@ -1462,6 +1488,8 @@ static int vorbis_parse_audio_packet(vorbis_context *vc) uint_fast8_t res_num = 0; int_fast16_t retlen = 0; float fadd_bias = vc->add_bias; + unsigned ch_left = vc->audio_channels; + unsigned vlen; if (get_bits1(gb)) { av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n"); @@ -1480,24 +1508,32 @@ static int vorbis_parse_audio_packet(vorbis_context *vc) blockflag = vc->modes[mode_number].blockflag; blocksize = vc->blocksize[blockflag]; + vlen = blocksize / 2; if (blockflag) skip_bits(gb, 2); // previous_window, next_window - memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ? - memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ? + memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ? + memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ? // Decode floor for (i = 0; i < vc->audio_channels; ++i) { vorbis_floor *floor; + int ret; if (mapping->submaps > 1) { floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]]; } else { floor = &vc->floors[mapping->submap_floor[0]]; } - no_residue[i] = floor->decode(vc, &floor->data, ch_floor_ptr); - ch_floor_ptr += blocksize / 2; + ret = floor->decode(vc, &floor->data, ch_floor_ptr); + + if (ret < 0) { + av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n"); + return -1; + } + no_residue[i] = ret; + ch_floor_ptr += vlen; } // Nonzero vector propagate @@ -1514,6 +1550,7 @@ static int vorbis_parse_audio_packet(vorbis_context *vc) for (i = 0; i < mapping->submaps; ++i) { vorbis_residue *residue; uint_fast8_t ch = 0; + int ret; for (j = 0; j < vc->audio_channels; ++j) { if ((mapping->submaps == 1) || (i == mapping->mux[j])) { @@ -1528,9 +1565,18 @@ static int vorbis_parse_audio_packet(vorbis_context *vc) } } residue = &vc->residues[mapping->submap_residue[i]]; - vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2); + if (ch_left < ch) { + av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n"); + return -1; + } + if (ch) { + ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left); + if (ret < 0) + return ret; + } - ch_res_ptr += ch * blocksize / 2; + ch_res_ptr += ch * vlen; + ch_left -= ch; } // Inverse coupling diff --git a/libavcodec/vorbis_enc.c b/libavcodec/vorbis_enc.c index 2bc0a24096..2fb41d16eb 100644 --- a/libavcodec/vorbis_enc.c +++ b/libavcodec/vorbis_enc.c @@ -302,7 +302,8 @@ static void create_vorbis_context(vorbis_enc_context *venc, }; fc->list[i].x = a[i - 2]; } - ff_vorbis_ready_floor1_list(fc->list, fc->values); + if (ff_vorbis_ready_floor1_list(avccontext, fc->list, fc->values)) + return AVERROR(EINVAL); venc->nresidues = 1; venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues); @@ -1099,7 +1100,7 @@ AVCodec vorbis_encoder = { vorbis_encode_init, vorbis_encode_frame, vorbis_encode_close, - .capabilities= CODEC_CAP_DELAY, + .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, .long_name = NULL_IF_CONFIG_SMALL("Vorbis"), }; diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index c08de6ca2c..5e13bdcba1 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -884,7 +884,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, /* decode a VLC into a token */ token = get_vlc2(gb, vlc_table, 11, 3); /* use the token to get a zero run, a coefficient, and an eob run */ - if (token <= 6) { + if ((unsigned) token <= 6U) { eob_run = eob_run_base[token]; if (eob_run_get_bits[token]) eob_run += get_bits(gb, eob_run_get_bits[token]); @@ -902,7 +902,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, coeff_i += eob_run; eob_run = 0; } - } else { + } else if (token >= 0) { bits_to_get = coeff_get_bits[token]; if (bits_to_get) bits_to_get = get_bits(gb, bits_to_get); @@ -936,6 +936,10 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, for (i = coeff_index+1; i <= coeff_index+zero_run; i++) s->num_coded_frags[plane][i]--; coeff_i++; + } else { + av_log(s->avctx, AV_LOG_ERROR, + "Invalid token %d\n", token); + return -1; } } @@ -985,6 +989,8 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) /* unpack the Y plane DC coefficients */ residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, 0, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; /* reverse prediction of the Y-plane DC coefficients */ reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); @@ -992,8 +998,12 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) /* unpack the C plane DC coefficients */ residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 1, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 2, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; /* reverse prediction of the C-plane DC coefficients */ if (!(s->avctx->flags & CODEC_FLAG_GRAY)) @@ -1030,11 +1040,17 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) for (i = 1; i <= 63; i++) { residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, 0, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 1, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 2, residual_eob_run); + if (residual_eob_run < 0) + return residual_eob_run; } return 0; @@ -1285,6 +1301,10 @@ static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, case 1: // zero run s->dct_tokens[plane][i]++; i += (token >> 2) & 0x7f; + if (i > 63) { + av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n"); + return i; + } block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; i++; break; @@ -1296,6 +1316,8 @@ static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, return i; } } while (i < 64); + // return value is expected to be a valid level + i--; end: // the actual DC+prediction is in the fragment structure block[0] = frag->dc * s->qmat[0][inter][plane][0]; diff --git a/libavcodec/vp5.c b/libavcodec/vp5.c index 1479344ba4..703a1c3389 100644 --- a/libavcodec/vp5.c +++ b/libavcodec/vp5.c @@ -116,7 +116,7 @@ static void vp5_parse_vector_models(VP56Context *s) model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7); } -static void vp5_parse_coeff_models(VP56Context *s) +static int vp5_parse_coeff_models(VP56Context *s) { VP56RangeCoder *c = &s->c; VP56Model *model = s->modelp; @@ -160,6 +160,7 @@ static void vp5_parse_coeff_models(VP56Context *s) for (ctx=0; ctx<6; ctx++) for (node=0; node<5; node++) model->coeff_acct[pt][ct][cg][ctx][node] = av_clip(((model->coeff_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254); + return 0; } static void vp5_parse_coeff(VP56Context *s) diff --git a/libavcodec/vp56.c b/libavcodec/vp56.c index 74fe5ff5f4..ebfd00634c 100644 --- a/libavcodec/vp56.c +++ b/libavcodec/vp56.c @@ -537,7 +537,8 @@ int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size, s->mb_type = VP56_MB_INTER_NOVEC_PF; } - s->parse_coeff_models(s); + if (s->parse_coeff_models(s)) + goto next; memset(s->prev_dc, 0, sizeof(s->prev_dc)); s->prev_dc[1][VP56_FRAME_CURRENT] = 128; @@ -601,6 +602,7 @@ int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size, } } + next: if (p->key_frame || golden_frame) { if (s->framep[VP56_FRAME_GOLDEN]->data[0] && s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2]) diff --git a/libavcodec/vp56.h b/libavcodec/vp56.h index 89eba0563e..ad5849ac17 100644 --- a/libavcodec/vp56.h +++ b/libavcodec/vp56.h @@ -41,7 +41,7 @@ typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src, typedef void (*VP56ParseCoeff)(VP56Context *s); typedef void (*VP56DefaultModelsInit)(VP56Context *s); typedef void (*VP56ParseVectorModels)(VP56Context *s); -typedef void (*VP56ParseCoeffModels)(VP56Context *s); +typedef int (*VP56ParseCoeffModels)(VP56Context *s); typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf, int buf_size, int *golden_frame); diff --git a/libavcodec/vp6.c b/libavcodec/vp6.c index 58c31f965b..1fe24cf28c 100644 --- a/libavcodec/vp6.c +++ b/libavcodec/vp6.c @@ -136,8 +136,11 @@ static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size, if (coeff_offset) { buf += coeff_offset; buf_size -= coeff_offset; - if (buf_size < 0) + if (buf_size < 0) { + if (s->framep[VP56_FRAME_CURRENT]->key_frame) + avcodec_set_dimensions(s->avctx, 0, 0); return 0; + } if (s->use_huffman) { s->parse_coeff = vp6_parse_coeff_huffman; init_get_bits(&s->gb, buf, buf_size<<3); @@ -212,8 +215,8 @@ static int vp6_huff_cmp(const void *va, const void *vb) return (a->count - b->count)*16 + (b->sym - a->sym); } -static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], - const uint8_t *map, unsigned size, VLC *vlc) +static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], + const uint8_t *map, unsigned size, VLC *vlc) { Node nodes[2*size], *tmp = &nodes[size]; int a, b, i; @@ -228,12 +231,12 @@ static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], } free_vlc(vlc); - /* then build the huffman tree accodring to probabilities */ - ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp, - FF_HUFFMAN_FLAG_HNODE_FIRST); + /* then build the huffman tree according to probabilities */ + return ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp, + FF_HUFFMAN_FLAG_HNODE_FIRST); } -static void vp6_parse_coeff_models(VP56Context *s) +static int vp6_parse_coeff_models(VP56Context *s) { VP56RangeCoder *c = &s->c; VP56Model *model = s->modelp; @@ -278,15 +281,18 @@ static void vp6_parse_coeff_models(VP56Context *s) if (s->use_huffman) { for (pt=0; pt<2; pt++) { - vp6_build_huff_tree(s, model->coeff_dccv[pt], - vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]); - vp6_build_huff_tree(s, model->coeff_runv[pt], - vp6_huff_run_map, 9, &s->runv_vlc[pt]); + if (vp6_build_huff_tree(s, model->coeff_dccv[pt], + vp6_huff_coeff_map, 12, &s->dccv_vlc[pt])) + return -1; + if (vp6_build_huff_tree(s, model->coeff_runv[pt], + vp6_huff_run_map, 9, &s->runv_vlc[pt])) + return -1; for (ct=0; ct<3; ct++) for (cg = 0; cg < 6; cg++) - vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg], - vp6_huff_coeff_map, 12, - &s->ract_vlc[pt][ct][cg]); + if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg], + vp6_huff_coeff_map, 12, + &s->ract_vlc[pt][ct][cg])) + return -1; } memset(s->nb_null, 0, sizeof(s->nb_null)); } else { @@ -296,6 +302,7 @@ static void vp6_parse_coeff_models(VP56Context *s) for (node=0; node<5; node++) model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255); } + return 0; } static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect) @@ -366,7 +373,7 @@ static void vp6_parse_coeff_huffman(VP56Context *s) if (b > 3) pt = 1; vlc_coeff = &s->dccv_vlc[pt]; - for (coeff_idx=0; coeff_idx<64; ) { + for (coeff_idx = 0;;) { int run = 1; if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) { s->nb_null[coeff_idx][pt]--; @@ -403,6 +410,8 @@ static void vp6_parse_coeff_huffman(VP56Context *s) } } coeff_idx+=run; + if (coeff_idx >= 64) + break; cg = FFMIN(vp6_coeff_groups[coeff_idx], 3); vlc_coeff = &s->ract_vlc[pt][ct][cg]; } @@ -430,7 +439,8 @@ static void vp6_parse_coeff(VP56Context *s) model1 = model->coeff_dccv[pt]; model2 = model->coeff_dcct[pt][ctx]; - for (coeff_idx=0; coeff_idx<64; ) { + coeff_idx = 0; + for (;;) { if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) { /* parse a coeff */ if (vp56_rac_get_prob(c, model2[2])) { @@ -471,8 +481,10 @@ static void vp6_parse_coeff(VP56Context *s) run += vp56_rac_get_prob(c, model3[i+8]) << i; } } - - cg = vp6_coeff_groups[coeff_idx+=run]; + coeff_idx += run; + if (coeff_idx >= 64) + break; + cg = vp6_coeff_groups[coeff_idx]; model1 = model2 = model->coeff_ract[pt][ct][cg]; } diff --git a/libavcodec/vqavideo.c b/libavcodec/vqavideo.c index a1698394ef..12f4dbcaca 100644 --- a/libavcodec/vqavideo.c +++ b/libavcodec/vqavideo.c @@ -162,6 +162,12 @@ static av_cold int vqa_decode_init(AVCodecContext *avctx) return -1; } + if (s->width & (s->vector_width - 1) || + s->height & (s->vector_height - 1)) { + av_log(avctx, AV_LOG_ERROR, "Image size not multiple of block size\n"); + return AVERROR_INVALIDDATA; + } + /* allocate codebooks */ s->codebook_size = MAX_CODEBOOK_SIZE; s->codebook = av_malloc(s->codebook_size); diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c index 7358d29735..43f28a67d4 100644 --- a/libavcodec/wavpack.c +++ b/libavcodec/wavpack.c @@ -275,7 +275,14 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int } }else{ t = get_unary_0_33(gb); - if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1)); + if(t >= 2){ + if(get_bits_left(gb) < t-1) + goto error; + t = get_bits(gb, t - 1) | (1 << (t-1)); + }else{ + if(get_bits_left(gb) < 0) + goto error; + } ctx->zeroes = t; if(ctx->zeroes){ memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median)); @@ -286,24 +293,24 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int } } - if(get_bits_count(gb) >= ctx->data_size){ - *last = 1; - return 0; - } - if(ctx->zero){ t = 0; ctx->zero = 0; }else{ t = get_unary_0_33(gb); - if(get_bits_count(gb) >= ctx->data_size){ - *last = 1; - return 0; - } + if(get_bits_left(gb) < 0) + goto error; if(t == 16) { t2 = get_unary_0_33(gb); - if(t2 < 2) t += t2; - else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1)); + if(t2 < 2){ + if(get_bits_left(gb) < 0) + goto error; + t += t2; + }else{ + if(get_bits_left(gb) < t2 - 1) + goto error; + t += get_bits(gb, t2 - 1) | (1 << (t2 - 1)); + } } if(ctx->one){ @@ -343,9 +350,13 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int } if(!c->error_limit){ ret = base + get_tail(gb, add); + if (get_bits_left(gb) <= 0) + goto error; }else{ int mid = (base*2 + add + 1) >> 1; while(add > c->error_limit){ + if(get_bits_left(gb) <= 0) + goto error; if(get_bits1(gb)){ add -= (mid - base); base = mid; @@ -359,6 +370,10 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int if(ctx->hybrid_bitrate) c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level); return sign ? ~ret : ret; + +error: + *last = 1; + return 0; } static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S) @@ -368,7 +383,7 @@ static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S) if(s->extra_bits){ S <<= s->extra_bits; - if(s->got_extra_bits){ + if(s->got_extra_bits && get_bits_left(&s->gb_extra_bits) >= s->extra_bits){ S |= get_bits(&s->gb_extra_bits, s->extra_bits); *crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16); } @@ -559,7 +574,10 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d count++; }while(!last && count < s->max_samples); - s->samples_left -= count; + if (last) + s->samples_left = 0; + else + s->samples_left -= count; if(!s->samples_left){ if(crc != s->CRC){ av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); @@ -632,7 +650,10 @@ static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst count++; }while(!last && count < s->samples); - s->samples_left -= count; + if (last) + s->samples_left = 0; + else + s->samples_left -= count; if(!s->samples_left){ if(crc != s->CRC){ av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); @@ -755,12 +776,13 @@ static int wavpack_decode_frame(AVCodecContext *avctx, } switch(id & WP_IDF_MASK){ case WP_ID_DECTERMS: - s->terms = size; - if(s->terms > MAX_TERMS){ + if(size > MAX_TERMS){ av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n"); + s->terms = 0; buf += ssize; continue; } + s->terms = size; for(i = 0; i < s->terms; i++) { s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5; s->decorr[s->terms - i - 1].delta = *buf >> 5; @@ -976,6 +998,9 @@ static int wavpack_decode_frame(AVCodecContext *avctx, else samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT); + if (samplecount < 0) + return -1; + }else{ if(avctx->sample_fmt == SAMPLE_FMT_S16) samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16); @@ -984,11 +1009,14 @@ static int wavpack_decode_frame(AVCodecContext *avctx, else samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT); + if (samplecount < 0) + return -1; + if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){ int16_t *dst = (int16_t*)samples + samplecount * 2; int16_t *src = (int16_t*)samples + samplecount; int cnt = samplecount; - while(cnt--){ + while(cnt-- > 0){ *--dst = *--src; *--dst = *src; } @@ -997,7 +1025,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx, int32_t *dst = (int32_t*)samples + samplecount * 2; int32_t *src = (int32_t*)samples + samplecount; int cnt = samplecount; - while(cnt--){ + while(cnt-- > 0){ *--dst = *--src; *--dst = *src; } @@ -1006,7 +1034,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx, float *dst = (float*)samples + samplecount * 2; float *src = (float*)samples + samplecount; int cnt = samplecount; - while(cnt--){ + while(cnt-- > 0){ *--dst = *--src; *--dst = *src; } diff --git a/libavcodec/wma.c b/libavcodec/wma.c index 65780459aa..45d431a2b1 100644 --- a/libavcodec/wma.c +++ b/libavcodec/wma.c @@ -135,6 +135,9 @@ int ff_wma_init(AVCodecContext *avctx, int flags2) /* compute MDCT block size */ s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0); + s->next_block_len_bits = s->frame_len_bits; + s->prev_block_len_bits = s->frame_len_bits; + s->block_len_bits = s->frame_len_bits; s->frame_len = 1 << s->frame_len_bits; if (s->use_variable_block_len) { diff --git a/libavcodec/wnv1.c b/libavcodec/wnv1.c index 56634d10ac..27026c5bd7 100644 --- a/libavcodec/wnv1.c +++ b/libavcodec/wnv1.c @@ -70,6 +70,11 @@ static int decode_frame(AVCodecContext *avctx, int prev_y = 0, prev_u = 0, prev_v = 0; uint8_t *rbuf; + if(buf_size<=8) { + av_log(avctx, AV_LOG_ERROR, "buf_size %d is too small\n", buf_size); + return AVERROR_INVALIDDATA; + } + rbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); if(!rbuf){ av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); diff --git a/libavcodec/x86/cpuid.c b/libavcodec/x86/cpuid.c index 1ed4d2e7e3..832b9909dc 100644 --- a/libavcodec/x86/cpuid.c +++ b/libavcodec/x86/cpuid.c @@ -98,7 +98,7 @@ int mm_support(void) if(max_ext_level >= 0x80000001){ cpuid(0x80000001, eax, ebx, ecx, ext_caps); - if (ext_caps & (1<<31)) + if (ext_caps & (1U<<31)) rval |= FF_MM_3DNOW; if (ext_caps & (1<<30)) rval |= FF_MM_3DNOWEXT; diff --git a/libavcodec/x86/dsputil_yasm.asm b/libavcodec/x86/dsputil_yasm.asm index e2478a4845..1a3bd1ae66 100644 --- a/libavcodec/x86/dsputil_yasm.asm +++ b/libavcodec/x86/dsputil_yasm.asm @@ -417,7 +417,7 @@ cglobal scalarproduct_float_sse, 3,3,2, v1, v2, offset shufps xmm0, xmm0, 1 addss xmm0, xmm1 %ifndef ARCH_X86_64 - movd r0m, xmm0 + movss r0m, xmm0 fld dword r0m %endif RET diff --git a/libavcodec/x86/fft_sse.c b/libavcodec/x86/fft_sse.c index a4cce69db1..eaca44a34f 100644 --- a/libavcodec/x86/fft_sse.c +++ b/libavcodec/x86/fft_sse.c @@ -23,7 +23,7 @@ #include "libavcodec/dsputil.h" #include "fft.h" -DECLARE_ALIGNED(16, static const int, m1m1m1m1)[4] = +DECLARE_ASM_CONST(16, int, ff_m1m1m1m1)[4] = { 1 << 31, 1 << 31, 1 << 31, 1 << 31 }; void ff_fft_dispatch_sse(FFTComplex *z, int nbits); @@ -183,7 +183,7 @@ void ff_imdct_calc_sse(FFTContext *s, FFTSample *output, const FFTSample *input) j = -n; k = n-16; __asm__ volatile( - "movaps %4, %%xmm7 \n" + "movaps "MANGLE(ff_m1m1m1m1)", %%xmm7 \n" "1: \n" "movaps (%2,%1), %%xmm0 \n" "movaps (%3,%0), %%xmm1 \n" @@ -196,8 +196,7 @@ void ff_imdct_calc_sse(FFTContext *s, FFTSample *output, const FFTSample *input) "add $16, %0 \n" "jl 1b \n" :"+r"(j), "+r"(k) - :"r"(output+n4), "r"(output+n4*3), - "m"(*m1m1m1m1) + :"r"(output+n4), "r"(output+n4*3) ); } diff --git a/libavcodec/x86/h264dsp_mmx.c b/libavcodec/x86/h264dsp_mmx.c index ab6e0834c8..fd16a02d6a 100644 --- a/libavcodec/x86/h264dsp_mmx.c +++ b/libavcodec/x86/h264dsp_mmx.c @@ -1138,7 +1138,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin int h=8;\ __asm__ volatile(\ "pxor %%mm7, %%mm7 \n\t"\ - "movq %5, %%mm6 \n\t"\ + "movq "MANGLE(ff_pw_5)", %%mm6\n\t"\ "1: \n\t"\ "movq (%0), %%mm0 \n\t"\ "movq 1(%0), %%mm2 \n\t"\ @@ -1172,7 +1172,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin "punpcklbw %%mm7, %%mm5 \n\t"\ "paddw %%mm3, %%mm2 \n\t"\ "paddw %%mm5, %%mm4 \n\t"\ - "movq %6, %%mm5 \n\t"\ + "movq "MANGLE(ff_pw_16)", %%mm5\n\t"\ "paddw %%mm5, %%mm2 \n\t"\ "paddw %%mm5, %%mm4 \n\t"\ "paddw %%mm2, %%mm0 \n\t"\ @@ -1186,7 +1186,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin "decl %2 \n\t"\ " jnz 1b \n\t"\ : "+a"(src), "+c"(dst), "+g"(h)\ - : "d"((x86_reg)srcStride), "S"((x86_reg)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\ + : "d"((x86_reg)srcStride), "S"((x86_reg)dstStride)\ : "memory"\ );\ }\ @@ -1640,7 +1640,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin int h=8;\ __asm__ volatile(\ "pxor %%xmm7, %%xmm7 \n\t"\ - "movdqa %5, %%xmm6 \n\t"\ + "movdqa "MANGLE(ff_pw_5)", %%xmm6\n\t"\ "1: \n\t"\ "lddqu -2(%0), %%xmm1 \n\t"\ "movdqa %%xmm1, %%xmm0 \n\t"\ @@ -1660,7 +1660,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin "paddw %%xmm4, %%xmm1 \n\t"\ "psllw $2, %%xmm2 \n\t"\ "psubw %%xmm1, %%xmm2 \n\t"\ - "paddw %6, %%xmm0 \n\t"\ + "paddw "MANGLE(ff_pw_16)", %%xmm0\n\t"\ "pmullw %%xmm6, %%xmm2 \n\t"\ "paddw %%xmm0, %%xmm2 \n\t"\ "psraw $5, %%xmm2 \n\t"\ @@ -1671,8 +1671,7 @@ static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uin "decl %2 \n\t"\ " jnz 1b \n\t"\ : "+a"(src), "+c"(dst), "+g"(h)\ - : "D"((x86_reg)srcStride), "S"((x86_reg)dstStride),\ - "m"(ff_pw_5), "m"(ff_pw_16)\ + : "D"((x86_reg)srcStride), "S"((x86_reg)dstStride)\ : "memory"\ );\ }\ diff --git a/libavcodec/x86/mathops.h b/libavcodec/x86/mathops.h index 010cfb70bb..5949dfe3d4 100644 --- a/libavcodec/x86/mathops.h +++ b/libavcodec/x86/mathops.h @@ -31,17 +31,17 @@ "imull %3 \n\t"\ "shrdl %4, %%edx, %%eax \n\t"\ : "=a"(rt), "=d"(dummy)\ - : "a" ((int)ra), "rm" ((int)rb), "i"(shift));\ + : "a" ((int)(ra)), "rm" ((int)(rb)), "i"(shift));\ rt; }) #define MULH(ra, rb) \ ({ int rt, dummy;\ - __asm__ ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" ((int)ra), "rm" ((int)rb));\ + __asm__ ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" ((int)(ra)), "rm" ((int)(rb)));\ rt; }) #define MUL64(ra, rb) \ ({ int64_t rt;\ - __asm__ ("imull %2\n\t" : "=A"(rt) : "a" ((int)ra), "g" ((int)rb));\ + __asm__ ("imull %2\n\t" : "=A"(rt) : "a" ((int)(ra)), "g" ((int)(rb)));\ rt; }) #endif diff --git a/libavcodec/xan.c b/libavcodec/xan.c index 3f6aa8cee5..9f20b7f280 100644 --- a/libavcodec/xan.c +++ b/libavcodec/xan.c @@ -88,17 +88,18 @@ static av_cold int xan_decode_init(AVCodecContext *avctx) return 0; } -static int xan_huffman_decode(unsigned char *dest, const unsigned char *src, - int dest_len) +static int xan_huffman_decode(unsigned char *dest, int dest_len, + const unsigned char *src, int src_len) { unsigned char byte = *src++; unsigned char ival = byte + 0x16; const unsigned char * ptr = src + byte*2; + int ptr_len = src_len - 1 - byte*2; unsigned char val = ival; unsigned char *dest_end = dest + dest_len; GetBitContext gb; - init_get_bits(&gb, ptr, 0); // FIXME: no src size available + init_get_bits(&gb, ptr, ptr_len * 8); while ( val != 0x16 ) { val = src[val - 0x17 + get_bits1(&gb) * byte]; @@ -263,7 +264,8 @@ static void xan_wc3_decode_frame(XanContext *s) { vector_segment = s->buf + AV_RL16(&s->buf[4]); imagedata_segment = s->buf + AV_RL16(&s->buf[6]); - xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size); + xan_huffman_decode(opcode_buffer, opcode_buffer_size, + huffman_segment, s->size - (huffman_segment - s->buf) ); if (imagedata_segment[0] == 2) xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size); diff --git a/libavcodec/yop.c b/libavcodec/yop.c index 1eb76b128e..7f933b5f46 100644 --- a/libavcodec/yop.c +++ b/libavcodec/yop.c @@ -89,6 +89,11 @@ static av_cold int yop_decode_init(AVCodecContext *avctx) return -1; } + if (!avctx->extradata) { + av_log(avctx, AV_LOG_ERROR, "extradata missing\n"); + return AVERROR_INVALIDDATA; + } + avctx->pix_fmt = PIX_FMT_PAL8; s->num_pal_colors = avctx->extradata[0]; @@ -198,6 +203,11 @@ static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size, if (s->frame.data[0]) avctx->release_buffer(avctx, &s->frame); + if (avpkt->size < 4 + 3*s->num_pal_colors) { + av_log(avctx, AV_LOG_ERROR, "packet of size %d too small\n", avpkt->size); + return AVERROR_INVALIDDATA; + } + ret = avctx->get_buffer(avctx, &s->frame); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); @@ -213,6 +223,10 @@ static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size, s->low_nibble = NULL; is_odd_frame = avpkt->data[0]; + if(is_odd_frame>1){ + av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame); + return AVERROR_INVALIDDATA; + } firstcolor = s->first_color[is_odd_frame]; palette = (uint32_t *)s->frame.data[1]; diff --git a/libavdevice/alsa-audio-common.c b/libavdevice/alsa-audio-common.c index 38cb0de0ef..12195de93e 100644 --- a/libavdevice/alsa-audio-common.c +++ b/libavdevice/alsa-audio-common.c @@ -129,6 +129,8 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, } snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL); + if (!period_size) + period_size = buffer_size / 4; res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL); if (res < 0) { av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n", diff --git a/libavfilter/formats.c b/libavfilter/formats.c index 2a9bdb0bd0..8b8bf75962 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -44,6 +44,9 @@ AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b) AVFilterFormats *ret; unsigned i, j, k = 0; + if (a == b) + return a; + ret = av_mallocz(sizeof(AVFilterFormats)); /* merge list of formats */ diff --git a/libavformat/Makefile b/libavformat/Makefile index 43a4a2e0d4..7a2d00fb45 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -114,7 +114,7 @@ OBJS-$(CONFIG_MM_DEMUXER) += mm.o OBJS-$(CONFIG_MMF_DEMUXER) += mmf.o raw.o OBJS-$(CONFIG_MMF_MUXER) += mmf.o riff.o OBJS-$(CONFIG_MOV_DEMUXER) += mov.o riff.o isom.o -OBJS-$(CONFIG_MOV_MUXER) += movenc.o riff.o isom.o avc.o +OBJS-$(CONFIG_MOV_MUXER) += movenc.o riff.o isom.o avc.o movenchint.o OBJS-$(CONFIG_MP2_MUXER) += mp3.o id3v1.o OBJS-$(CONFIG_MP3_DEMUXER) += mp3.o id3v1.o id3v2.o OBJS-$(CONFIG_MP3_MUXER) += mp3.o id3v1.o id3v2.o @@ -255,6 +255,9 @@ OBJS-$(CONFIG_W64_DEMUXER) += wav.o riff.o raw.o OBJS-$(CONFIG_WAV_DEMUXER) += wav.o riff.o raw.o OBJS-$(CONFIG_WAV_MUXER) += wav.o riff.o OBJS-$(CONFIG_WC3_DEMUXER) += wc3movie.o +OBJS-$(CONFIG_WEBM_MUXER) += matroskaenc.o matroska.o \ + riff.o isom.o avc.o \ + flacenc_header.o OBJS-$(CONFIG_WSAUD_DEMUXER) += westwood.o OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood.o OBJS-$(CONFIG_WV_DEMUXER) += wv.o apetag.o id3v1.o diff --git a/libavformat/aea.c b/libavformat/aea.c index 4b85f91068..518995c4b0 100644 --- a/libavformat/aea.c +++ b/libavformat/aea.c @@ -46,10 +46,9 @@ static int aea_read_probe(AVProbeData *p) /* Check so that the redundant bsm bytes and info bytes are valid * the block size mode bytes have to be the same * the info bytes have to be the same - * the block size mode and info byte can't be the same */ - if (bsm_s == bsm_e && inb_s == inb_e && bsm_s != inb_s) - return AVPROBE_SCORE_MAX / 2; + if (bsm_s == bsm_e && inb_s == inb_e) + return AVPROBE_SCORE_MAX / 4 + 1; } return 0; } diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 27dba103d2..27a955594b 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -203,6 +203,7 @@ void av_register_all(void) REGISTER_DEMUXER (W64, w64); REGISTER_MUXDEMUX (WAV, wav); REGISTER_DEMUXER (WC3, wc3); + REGISTER_MUXER (WEBM, webm); REGISTER_DEMUXER (WSAUD, wsaud); REGISTER_DEMUXER (WSVQA, wsvqa); REGISTER_DEMUXER (WV, wv); diff --git a/libavformat/ape.c b/libavformat/ape.c index 91acf7240d..56a9c78a39 100644 --- a/libavformat/ape.c +++ b/libavformat/ape.c @@ -242,6 +242,10 @@ static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap) url_fskip(pb, ape->wavheaderlength); } + if(!ape->totalframes){ + av_log(s, AV_LOG_ERROR, "No frames in the file!\n"); + return AVERROR(EINVAL); + } if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){ av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes); return -1; @@ -259,6 +263,8 @@ static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap) if (ape->seektablelength > 0) { ape->seektable = av_malloc(ape->seektablelength); + if (!ape->seektable) + return AVERROR(ENOMEM); for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++) ape->seektable[i] = get_le32(pb); } diff --git a/libavformat/asfdec.c b/libavformat/asfdec.c index bbc27305e2..8aea8c7ef6 100644 --- a/libavformat/asfdec.c +++ b/libavformat/asfdec.c @@ -204,9 +204,10 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) get_byte(pb); memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid)); for(;;) { + uint64_t gpos= url_ftell(pb); get_guid(pb, &g); gsize = get_le64(pb); - dprintf(s, "%08"PRIx64": ", url_ftell(pb) - 24); + dprintf(s, "%08"PRIx64": ", gpos); print_guid(&g); dprintf(s, " size=0x%"PRIx64"\n", gsize); if (!guidcmp(&g, &ff_asf_data_header)) { @@ -584,8 +585,10 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n"); } } - url_fseek(pb, gsize - 24, SEEK_CUR); } + if(url_ftell(pb) != gpos + gsize) + av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", url_ftell(pb)-gpos, gsize); + url_fseek(pb, gpos + gsize, SEEK_SET); } get_guid(pb, &g); get_le64(pb); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 9928e84dad..dbd22bb80d 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -22,8 +22,8 @@ #define AVFORMAT_AVFORMAT_H #define LIBAVFORMAT_VERSION_MAJOR 52 -#define LIBAVFORMAT_VERSION_MINOR 62 -#define LIBAVFORMAT_VERSION_MICRO 0 +#define LIBAVFORMAT_VERSION_MINOR 64 +#define LIBAVFORMAT_VERSION_MICRO 2 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ @@ -653,6 +653,7 @@ typedef struct AVFormatContext { #define AVFMT_FLAG_IGNDTS 0x0008 ///< Ignore DTS on frames that contain both DTS & PTS #define AVFMT_FLAG_NOFILLIN 0x0010 ///< Do not infer any values from other values, just return what is stored in the container #define AVFMT_FLAG_NOPARSE 0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled +#define AVFMT_FLAG_RTP_HINT 0x0040 ///< Add RTP hinting to the output file int loop_input; /** decoding: size of data to probe; encoding: unused. */ @@ -868,8 +869,22 @@ void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload); */ void av_register_all(void); -/** codec tag <-> codec id */ +/** + * Gets the CodecID for the given codec tag tag. + * If no codec id is found returns CODEC_ID_NONE. + * + * @param tags list of supported codec_id-codec_tag pairs, as stored + * in AVInputFormat.codec_tag and AVOutputFormat.codec_tag + */ enum CodecID av_codec_get_id(const struct AVCodecTag * const *tags, unsigned int tag); + +/** + * Gets the codec tag for the given codec id id. + * If no codec tag is found returns 0. + * + * @param tags list of supported codec_id-codec_tag pairs, as stored + * in AVInputFormat.codec_tag and AVOutputFormat.codec_tag + */ unsigned int av_codec_get_tag(const struct AVCodecTag * const *tags, enum CodecID id); /* media file input */ diff --git a/libavformat/avidec.c b/libavformat/avidec.c index e866a93480..485c4eb93a 100644 --- a/libavformat/avidec.c +++ b/libavformat/avidec.c @@ -650,6 +650,16 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) avi_load_index(s); avi->index_loaded = 1; avi->non_interleaved |= guess_ni_flag(s); + for(i=0; inb_streams; i++){ + AVStream *st = s->streams[i]; + if(st->nb_index_entries) + break; + } + if(i==s->nb_streams && avi->non_interleaved) { + av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n"); + avi->non_interleaved=0; + } + if(avi->non_interleaved) { av_log(s, AV_LOG_INFO, "non-interleaved AVI\n"); clean_index(s); diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 8684903464..2a2fb78169 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -27,6 +27,13 @@ #define IO_BUFFER_SIZE 32768 +/** + * Do seeks within this distance ahead of the current buffer by skipping + * data instead of calling the protocol seek function, for seekable + * protocols. + */ +#define SHORT_SEEK_THRESHOLD 4096 + static void fill_buffer(ByteIOContext *s); #if LIBAVFORMAT_VERSION_MAJOR >= 53 static int url_resetbuf(ByteIOContext *s, int flags); @@ -152,7 +159,9 @@ int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence) offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) { /* can do the seek inside the buffer */ s->buf_ptr = s->buffer + offset1; - } else if(s->is_streamed && !s->write_flag && offset1 >= 0 && + } else if ((s->is_streamed || + offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) && + !s->write_flag && offset1 >= 0 && (whence != SEEK_END || force)) { while(s->pos < offset && !s->eof_reached) fill_buffer(s); diff --git a/libavformat/dv.c b/libavformat/dv.c index b6f9c6ae54..d65d4fffbd 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -125,10 +125,14 @@ static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4], /* We work with 720p frames split in half, thus even frames have * channels 0,1 and odd 2,3. */ ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0; - pcm = ppcm[ipcm++]; /* for each DIF channel */ for (chan = 0; chan < sys->n_difchan; chan++) { + /* next stereo channel (50Mbps and 100Mbps only) */ + pcm = ppcm[ipcm++]; + if (!pcm) + break; + /* for each DIF segment */ for (i = 0; i < sys->difseg_size; i++) { frame += 6 * 80; /* skip DIF segment header */ @@ -176,11 +180,6 @@ static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4], frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ } } - - /* next stereo channel (50Mbps and 100Mbps only) */ - pcm = ppcm[ipcm++]; - if (!pcm) - break; } return size; @@ -202,6 +201,12 @@ static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame) stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */ quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ + if (stype > 3) { + av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype); + c->ach = 0; + return 0; + } + /* note: ach counts PAIRS of channels (i.e. stereo channels) */ ach = ((int[4]){ 1, 0, 2, 4})[stype]; if (ach == 1 && quant && freq == 2) @@ -335,7 +340,8 @@ int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate; ppcm[i] = c->audio_buf[i]; } - dv_extract_audio(buf, ppcm, c->sys); + if (c->ach) + dv_extract_audio(buf, ppcm, c->sys); /* We work with 720p frames split in half, thus even frames have * channels 0,1 and odd 2,3. */ diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c index 86d7f91e66..5f485290c8 100644 --- a/libavformat/electronicarts.c +++ b/libavformat/electronicarts.c @@ -457,12 +457,17 @@ static int ea_read_packet(AVFormatContext *s, while (!packet_read) { chunk_type = get_le32(pb); - chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8; + chunk_size = ea->big_endian ? get_be32(pb) : get_le32(pb); + if (chunk_size <= 8) + return AVERROR_INVALIDDATA; + chunk_size -= 8; switch (chunk_type) { /* audio data */ case ISNh_TAG: /* header chunk also contains data; skip over the header portion*/ + if (chunk_size < 32) + return AVERROR_INVALIDDATA; url_fskip(pb, 32); chunk_size -= 32; case ISNd_TAG: diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index c351117132..fa6a1d13d0 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -162,7 +162,7 @@ static int flv_write_header(AVFormatContext *s) AVCodecContext *audio_enc = NULL, *video_enc = NULL; int i; double framerate = 0.0; - int metadata_size_pos, data_size; + int64_t metadata_size_pos, data_size; for(i=0; inb_streams; i++){ AVCodecContext *enc = s->streams[i]->codec; diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c index 6fa11db6c0..8c1c0710e7 100644 --- a/libavformat/id3v2.c +++ b/libavformat/id3v2.c @@ -187,8 +187,17 @@ void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags) goto error; } - if (isv34 && flags & 0x40) /* Extended header present, just skip over it */ - url_fskip(s->pb, get_size(s->pb, 4)); + if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */ + int extlen = get_size(s->pb, 4); + if (version == 4) + extlen -= 4; // in v2.4 the length includes the length field we just read + + if (extlen < 0) { + reason = "invalid extended header length"; + goto error; + } + url_fskip(s->pb, extlen); + } while (len >= taghdrlen) { if (isv34) { diff --git a/libavformat/internal.h b/libavformat/internal.h index 789988019d..a395c2f24b 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -137,4 +137,21 @@ int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt, ...); +/** + * Appends the media-specific SDP fragment for the media stream c + * to the buffer buff. + * + * Note, the buffer needs to be initialized, since it is appended to + * existing content. + * + * @param buff the buffer to append the SDP fragment to + * @param size the size of the buff buffer + * @param c the AVCodecContext of the media to describe + * @param dest_addr the destination address of the media stream, may be NULL + * @param port the destination port of the media stream, 0 if unknown + * @param ttl the time to live of the stream, 0 if not multicast + */ +void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, + const char *dest_addr, int port, int ttl); + #endif /* AVFORMAT_INTERNAL_H */ diff --git a/libavformat/matroska.c b/libavformat/matroska.c index 214825a180..dac4735a50 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -76,6 +76,7 @@ const CodecTags ff_mkv_codec_tags[]={ {"V_SNOW" , CODEC_ID_SNOW}, {"V_THEORA" , CODEC_ID_THEORA}, {"V_UNCOMPRESSED" , CODEC_ID_RAWVIDEO}, + {"V_VP8" , CODEC_ID_VP8}, {"" , CODEC_ID_NONE} }; diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 4e6f375c5f..3efed30fb5 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -505,6 +505,8 @@ static EbmlSyntax matroska_clusters[] = { { 0 } }; +static const char *matroska_doctypes[] = { "matroska", "webm" }; + /* * Return: Whether we reached the end of a level in the hierarchy or not. */ @@ -757,11 +759,15 @@ static int ebml_parse_elem(MatroskaDemuxContext *matroska, uint32_t id = syntax->id; uint64_t length; int res; + void *newelem; data = (char *)data + syntax->data_offset; if (syntax->list_elem_size) { EbmlList *list = data; - list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); + newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); + if (!newelem) + return AVERROR(ENOMEM); + list->elem = newelem; data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; memset(data, 0, syntax->list_elem_size); list->nb_elem++; @@ -823,8 +829,7 @@ static void ebml_free(EbmlSyntax *syntax, void *data) static int matroska_probe(AVProbeData *p) { uint64_t total = 0; - int len_mask = 0x80, size = 1, n = 1; - static const char probe_data[] = "matroska"; + int len_mask = 0x80, size = 1, n = 1, i; /* EBML header? */ if (AV_RB32(p->buf) != EBML_ID_HEADER) @@ -846,15 +851,19 @@ static int matroska_probe(AVProbeData *p) if (p->buf_size < 4 + size + total) return 0; - /* The header must contain the document type 'matroska'. For now, + /* The header should contain a known document type. For now, * we don't parse the whole header but simply check for the * availability of that array of characters inside the header. * Not fully fool-proof, but good enough. */ - for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++) - if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1)) - return AVPROBE_SCORE_MAX; + for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { + int probelen = strlen(matroska_doctypes[i]); + for (n = 4+size; n <= 4+size+total-probelen; n++) + if (!memcmp(p->buf+n, matroska_doctypes[i], probelen)) + return AVPROBE_SCORE_MAX; + } - return 0; + // probably valid EBML header but no recognized doctype + return AVPROBE_SCORE_MAX/2; } static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, @@ -878,6 +887,7 @@ static int matroska_decode_buffer(uint8_t** buf, int* buf_size, uint8_t* data = *buf; int isize = *buf_size; uint8_t* pkt_data = NULL; + uint8_t* newpktdata; int pkt_size = isize; int result = 0; int olen; @@ -904,7 +914,12 @@ static int matroska_decode_buffer(uint8_t** buf, int* buf_size, zstream.avail_in = isize; do { pkt_size *= 3; - pkt_data = av_realloc(pkt_data, pkt_size); + newpktdata = av_realloc(pkt_data, pkt_size); + if (!newpktdata) { + inflateEnd(&zstream); + goto failed; + } + pkt_data = newpktdata; zstream.avail_out = pkt_size - zstream.total_out; zstream.next_out = pkt_data + zstream.total_out; result = inflate(&zstream, Z_NO_FLUSH); @@ -925,7 +940,12 @@ static int matroska_decode_buffer(uint8_t** buf, int* buf_size, bzstream.avail_in = isize; do { pkt_size *= 3; - pkt_data = av_realloc(pkt_data, pkt_size); + newpktdata = av_realloc(pkt_data, pkt_size); + if (!newpktdata) { + BZ2_bzDecompressEnd(&bzstream); + goto failed; + } + pkt_data = newpktdata; bzstream.avail_out = pkt_size - bzstream.total_out_lo32; bzstream.next_out = pkt_data + bzstream.total_out_lo32; result = BZ2_bzDecompress(&bzstream); @@ -980,13 +1000,17 @@ static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska, } } -static void matroska_merge_packets(AVPacket *out, AVPacket *in) +static int matroska_merge_packets(AVPacket *out, AVPacket *in) { - out->data = av_realloc(out->data, out->size+in->size); + void *newdata = av_realloc(out->data, out->size+in->size); + if (!newdata) + return AVERROR(ENOMEM); + out->data = newdata; memcpy(out->data+out->size, in->data, in->size); out->size += in->size; av_destruct_packet(in); av_free(in); + return 0; } static void matroska_convert_tag(AVFormatContext *s, EbmlList *list, @@ -1050,13 +1074,13 @@ static void matroska_convert_tags(AVFormatContext *s) static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) { EbmlList *seekhead_list = &matroska->seekhead; - MatroskaSeekhead *seekhead = seekhead_list->elem; uint32_t level_up = matroska->level_up; int64_t before_pos = url_ftell(matroska->ctx->pb); MatroskaLevel level; int i; for (i=0; inb_elem; i++) { + MatroskaSeekhead *seekhead = seekhead_list->elem; int64_t offset = seekhead[i].pos + matroska->segment_start; if (seekhead[i].pos <= before_pos @@ -1139,14 +1163,21 @@ static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap) /* First read the EBML header. */ if (ebml_parse(matroska, ebml_syntax, &ebml) || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t) - || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska") - || ebml.doctype_version > 2) { + || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) { av_log(matroska->ctx, AV_LOG_ERROR, "EBML header using unsupported features\n" "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", ebml.version, ebml.doctype, ebml.doctype_version); + ebml_free(ebml_syntax, &ebml); return AVERROR_PATCHWELCOME; } + for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) + if (!strcmp(ebml.doctype, matroska_doctypes[i])) + break; + if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { + av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); + } + av_metadata_set2(&s->metadata, "doctype", ebml.doctype, 0); ebml_free(ebml_syntax, &ebml); /* The next thing is a segment. */ @@ -1390,6 +1421,8 @@ static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap) 255); if (st->codec->codec_id != CODEC_ID_H264) st->need_parsing = AVSTREAM_PARSE_HEADERS; + if (track->default_duration) + st->avg_frame_rate = av_d2q(1000000000.0/track->default_duration, INT_MAX); } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->sample_rate = track->audio.out_samplerate; @@ -1480,11 +1513,13 @@ static int matroska_deliver_packet(MatroskaDemuxContext *matroska, memcpy(pkt, matroska->packets[0], sizeof(AVPacket)); av_free(matroska->packets[0]); if (matroska->num_packets > 1) { + void *newpackets; memmove(&matroska->packets[0], &matroska->packets[1], (matroska->num_packets - 1) * sizeof(AVPacket *)); - matroska->packets = - av_realloc(matroska->packets, (matroska->num_packets - 1) * - sizeof(AVPacket *)); + newpackets = av_realloc(matroska->packets, + (matroska->num_packets - 1) * sizeof(AVPacket *)); + if (newpackets) + matroska->packets = newpackets; } else { av_freep(&matroska->packets); } @@ -1833,10 +1868,11 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index, if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE && !tracks[i].stream->discard != AVDISCARD_ALL) { index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD); - if (index_sub >= 0 - && st->index_entries[index_sub].pos < st->index_entries[index_min].pos - && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale) - index_min = index_sub; + while(index_sub >= 0 + && index_min >= 0 + && tracks[i].stream->index_entries[index_sub].pos < st->index_entries[index_min].pos + && st->index_entries[index].timestamp - tracks[i].stream->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale) + index_min--; } } diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 76c74173c5..c004bd6464 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -65,7 +65,11 @@ typedef struct { int write_dts; } mkv_track; +#define MODE_MATROSKAv2 0x01 +#define MODE_WEBM 0x02 + typedef struct MatroskaMuxContext { + int mode; ByteIOContext *dyn_bc; ebml_master segment; int64_t segment_offset; @@ -544,7 +548,6 @@ static int mkv_write_tracks(AVFormatContext *s) put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1); put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1); put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet) - put_ebml_float(pb, MATROSKA_ID_TRACKTIMECODESCALE, 1.0); if ((tag = av_metadata_get(st->metadata, "title", NULL, 0))) put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value); @@ -564,9 +567,17 @@ static int mkv_write_tracks(AVFormatContext *s) } } + if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 || + codec->codec_id == CODEC_ID_VORBIS)) { + av_log(s, AV_LOG_ERROR, + "Only VP8 video and Vorbis audio are supported for WebM.\n"); + return AVERROR(EINVAL); + } + switch (codec->codec_type) { case AVMEDIA_TYPE_VIDEO: put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO); + put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9); if (!native_id && ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) && @@ -684,6 +695,9 @@ static int mkv_write_header(AVFormatContext *s) AVMetadataTag *tag; int ret; + if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM; + else mkv->mode = MODE_MATROSKAv2; + mkv->md5_ctx = av_mallocz(av_md5_size); av_md5_init(mkv->md5_ctx); mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks)); @@ -693,7 +707,7 @@ static int mkv_write_header(AVFormatContext *s) put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1); put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4); put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8); - put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska"); + put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name); put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2); put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2); end_ebml_master(pb, ebml_header); @@ -736,8 +750,10 @@ static int mkv_write_header(AVFormatContext *s) ret = mkv_write_tracks(s); if (ret < 0) return ret; - ret = mkv_write_chapters(s); - if (ret < 0) return ret; + if (mkv->mode != MODE_WEBM) { + ret = mkv_write_chapters(s); + if (ret < 0) return ret; + } if (url_is_streamed(s->pb)) mkv_write_seekhead(pb, mkv->main_seekhead); @@ -974,6 +990,7 @@ static int mkv_write_trailer(AVFormatContext *s) return 0; } +#if CONFIG_MATROSKA_MUXER AVOutputFormat matroska_muxer = { "matroska", NULL_IF_CONFIG_SMALL("Matroska file format"), @@ -989,7 +1006,25 @@ AVOutputFormat matroska_muxer = { .codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0}, .subtitle_codec = CODEC_ID_TEXT, }; +#endif +#if CONFIG_WEBM_MUXER +AVOutputFormat webm_muxer = { + "webm", + NULL_IF_CONFIG_SMALL("WebM file format"), + "video/webm", + "webm", + sizeof(MatroskaMuxContext), + CODEC_ID_VORBIS, + CODEC_ID_VP8, + mkv_write_header, + mkv_write_packet, + mkv_write_trailer, + .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, +}; +#endif + +#if CONFIG_MATROSKA_AUDIO_MUXER AVOutputFormat matroska_audio_muxer = { "matroska", NULL_IF_CONFIG_SMALL("Matroska file format"), @@ -1004,3 +1039,4 @@ AVOutputFormat matroska_audio_muxer = { .flags = AVFMT_GLOBALHEADER, .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0}, }; +#endif diff --git a/libavformat/mov.c b/libavformat/mov.c index e37bcf5f57..f1cde0b767 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -144,7 +144,7 @@ static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) switch (atom.type) { case MKTAG(0xa9,'n','a','m'): key = "title"; break; case MKTAG(0xa9,'a','u','t'): - case MKTAG(0xa9,'A','R','T'): key = "author"; break; + case MKTAG(0xa9,'A','R','T'): key = "artist"; break; case MKTAG(0xa9,'w','r','t'): key = "composer"; break; case MKTAG( 'c','p','r','t'): case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; @@ -1820,9 +1820,6 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) #if CONFIG_H263_DECODER case CODEC_ID_H263: #endif -#if CONFIG_H264_DECODER - case CODEC_ID_H264: -#endif #if CONFIG_MPEG4_DECODER case CODEC_ID_MPEG4: #endif diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 9f10aed193..00f69902c4 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "movenc.h" #include "avformat.h" #include "riff.h" #include "avio.h" @@ -28,69 +29,12 @@ #include "avc.h" #include "libavcodec/get_bits.h" #include "libavcodec/put_bits.h" +#include "internal.h" +#include "libavutil/avstring.h" #undef NDEBUG #include -#define MOV_INDEX_CLUSTER_SIZE 16384 -#define MOV_TIMESCALE 1000 - -#define MODE_MP4 0x01 -#define MODE_MOV 0x02 -#define MODE_3GP 0x04 -#define MODE_PSP 0x08 // example working PSP command line: -// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4 -#define MODE_3G2 0x10 -#define MODE_IPOD 0x20 - -typedef struct MOVIentry { - unsigned int size; - uint64_t pos; - unsigned int samplesInChunk; - unsigned int entries; - int cts; - int64_t dts; -#define MOV_SYNC_SAMPLE 0x0001 -#define MOV_PARTIAL_SYNC_SAMPLE 0x0002 - uint32_t flags; -} MOVIentry; - -typedef struct MOVIndex { - int mode; - int entry; - unsigned timescale; - uint64_t time; - int64_t trackDuration; - long sampleCount; - long sampleSize; - int hasKeyframes; -#define MOV_TRACK_CTTS 0x0001 -#define MOV_TRACK_STPS 0x0002 - uint32_t flags; - int language; - int trackID; - int tag; ///< stsd fourcc - AVCodecContext *enc; - - int vosLen; - uint8_t *vosData; - MOVIentry *cluster; - int audio_vbr; - int height; ///< active picture (w/o VBI) height for D-10/IMX - uint32_t tref_tag; - int tref_id; ///< trackID of the referenced track -} MOVTrack; - -typedef struct MOVMuxContext { - int mode; - int64_t time; - int nb_streams; - int chapter_track; ///< qt chapter track number - int64_t mdat_pos; - uint64_t mdat_size; - MOVTrack *tracks; -} MOVMuxContext; - //FIXME support 64 bit variant with wide placeholders static int64_t updateSize(ByteIOContext *pb, int64_t pos) { @@ -864,6 +808,26 @@ static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track) return updateSize(pb, pos); } +static int mov_write_rtp_tag(ByteIOContext *pb, MOVTrack *track) +{ + int64_t pos = url_ftell(pb); + put_be32(pb, 0); /* size */ + put_tag(pb, "rtp "); + put_be32(pb, 0); /* Reserved */ + put_be16(pb, 0); /* Reserved */ + put_be16(pb, 1); /* Data-reference index */ + + put_be16(pb, 1); /* Hint track version */ + put_be16(pb, 1); /* Highest compatible version */ + put_be32(pb, track->max_packet_size); /* Max packet size */ + + put_be32(pb, 12); /* size */ + put_tag(pb, "tims"); + put_be32(pb, track->timescale); + + return updateSize(pb, pos); +} + static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track) { int64_t pos = url_ftell(pb); @@ -877,6 +841,8 @@ static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track) mov_write_audio_tag(pb, track); else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) mov_write_subtitle_tag(pb, track); + else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) + mov_write_rtp_tag(pb, track); return updateSize(pb, pos); } @@ -976,7 +942,8 @@ static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track) put_tag(pb, "stbl"); mov_write_stsd_tag(pb, track); mov_write_stts_tag(pb, track); - if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && + if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO || + track->enc->codec_tag == MKTAG('r','t','p',' ')) && track->hasKeyframes && track->hasKeyframes < track->entry) mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE); if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS) @@ -1063,6 +1030,9 @@ static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track) if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl"; else hdlr_type = "text"; descr = "SubtitleHandler"; + } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) { + hdlr_type = "hint"; + descr = "HintHandler"; } } @@ -1082,6 +1052,21 @@ static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track) return updateSize(pb, pos); } +static int mov_write_hmhd_tag(ByteIOContext *pb) +{ + /* This atom must be present, but leaving the values at zero + * seems harmless. */ + put_be32(pb, 28); /* size */ + put_tag(pb, "hmhd"); + put_be32(pb, 0); /* version, flags */ + put_be16(pb, 0); /* maxPDUsize */ + put_be16(pb, 0); /* avgPDUsize */ + put_be32(pb, 0); /* maxbitrate */ + put_be32(pb, 0); /* avgbitrate */ + put_be32(pb, 0); /* reserved */ + return 28; +} + static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track) { int64_t pos = url_ftell(pb); @@ -1094,6 +1079,8 @@ static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track) else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) { if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb); else mov_write_nmhd_tag(pb); + } else if (track->tag == MKTAG('r','t','p',' ')) { + mov_write_hmhd_tag(pb); } if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */ mov_write_hdlr_tag(pb, NULL); @@ -1249,6 +1236,25 @@ static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov) return 0x34; } +static int mov_write_udta_sdp(ByteIOContext *pb, AVCodecContext *ctx, int index) +{ + char buf[1000] = ""; + int len; + + ff_sdp_write_media(buf, sizeof(buf), ctx, NULL, 0, 0); + av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index); + len = strlen(buf); + + put_be32(pb, len + 24); + put_tag (pb, "udta"); + put_be32(pb, len + 16); + put_tag (pb, "hnti"); + put_be32(pb, len + 8); + put_tag (pb, "sdp "); + put_buffer(pb, buf, len); + return len + 24; +} + static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st) { int64_t pos = url_ftell(pb); @@ -1262,6 +1268,8 @@ static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st) mov_write_mdia_tag(pb, track); if (track->mode == MODE_PSP) mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box + if (track->tag == MKTAG('r','t','p',' ')) + mov_write_udta_sdp(pb, track->rtp_ctx->streams[0]->codec, track->trackID); return updateSize(pb, pos); } @@ -1444,7 +1452,7 @@ static int mov_write_ilst_tag(ByteIOContext *pb, MOVMuxContext *mov, put_be32(pb, 0); /* size */ put_tag(pb, "ilst"); mov_write_string_metadata(s, pb, "\251nam", "title" , 1); - mov_write_string_metadata(s, pb, "\251ART", "author" , 1); + mov_write_string_metadata(s, pb, "\251ART", "artist" , 1); mov_write_string_metadata(s, pb, "aART", "album_artist", 1); mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1); mov_write_string_metadata(s, pb, "\251alb", "album" , 1); @@ -1571,6 +1579,7 @@ static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov, return ret; if (mov->mode & MODE_3GP) { + mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist"); mov_write_3gp_udta_tag(pb_buf, s, "titl", "title"); mov_write_3gp_udta_tag(pb_buf, s, "auth", "author"); mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre"); @@ -1676,6 +1685,13 @@ static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov, mov->tracks[i].tref_tag = MKTAG('c','h','a','p'); mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID; } + for (i = 0; i < mov->nb_streams; i++) { + if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) { + mov->tracks[i].tref_tag = MKTAG('h','i','n','t'); + mov->tracks[i].tref_id = + mov->tracks[mov->tracks[i].src_track].trackID; + } + } mov_write_mvhd_tag(pb, mov); //mov_write_iods_tag(pb, mov); @@ -1843,7 +1859,7 @@ static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags) return 0; } -static int mov_write_packet(AVFormatContext *s, AVPacket *pkt) +int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) { MOVMuxContext *mov = s->priv_data; ByteIOContext *pb = s->pb; @@ -1936,6 +1952,9 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt) mov->mdat_size += size; put_flush_packet(pb); + + if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) + ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry); return 0; } @@ -1968,7 +1987,7 @@ static void mov_create_chapter_track(AVFormatContext *s, int tracknum) pkt.data = av_malloc(pkt.size); AV_WB16(pkt.data, len); memcpy(pkt.data+2, t->value, len); - mov_write_packet(s, &pkt); + ff_mov_write_packet(s, &pkt); av_freep(&pkt.data); } } @@ -1978,7 +1997,7 @@ static int mov_write_header(AVFormatContext *s) { ByteIOContext *pb = s->pb; MOVMuxContext *mov = s->priv_data; - int i; + int i, hint_track = 0; if (url_is_streamed(s->pb)) { av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n"); @@ -2009,6 +2028,18 @@ static int mov_write_header(AVFormatContext *s) if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters) mov->chapter_track = mov->nb_streams++; + if (s->flags & AVFMT_FLAG_RTP_HINT) { + /* Add hint tracks for each audio and video stream */ + hint_track = mov->nb_streams; + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO || + st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { + mov->nb_streams++; + } + } + } + mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks)); if (!mov->tracks) return AVERROR(ENOMEM); @@ -2029,6 +2060,9 @@ static int mov_write_header(AVFormatContext *s) "codec not currently supported in container\n", i); goto error; } + /* If hinting of this track is enabled by a later hint track, + * this is updated. */ + track->hint_track = -1; if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){ if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') || track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') || @@ -2083,6 +2117,18 @@ static int mov_write_header(AVFormatContext *s) if (mov->chapter_track) mov_create_chapter_track(s, mov->chapter_track); + if (s->flags & AVFMT_FLAG_RTP_HINT) { + /* Initialize the hint tracks for each audio and video stream */ + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO || + st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { + ff_mov_init_hinting(s, hint_track, i); + hint_track++; + } + } + } + put_flush_packet(pb); return 0; @@ -2119,6 +2165,8 @@ static int mov_write_trailer(AVFormatContext *s) av_freep(&mov->tracks[mov->chapter_track].enc); for (i=0; inb_streams; i++) { + if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) + ff_mov_close_hinting(&mov->tracks[i]); av_freep(&mov->tracks[i].cluster); if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData); @@ -2142,7 +2190,7 @@ AVOutputFormat mov_muxer = { CODEC_ID_AAC, CODEC_ID_MPEG4, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0}, @@ -2158,7 +2206,7 @@ AVOutputFormat tgp_muxer = { CODEC_ID_AMR_NB, CODEC_ID_H263, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER, .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0}, @@ -2174,7 +2222,7 @@ AVOutputFormat mp4_muxer = { CODEC_ID_AAC, CODEC_ID_MPEG4, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0}, @@ -2190,7 +2238,7 @@ AVOutputFormat psp_muxer = { CODEC_ID_AAC, CODEC_ID_MPEG4, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER, .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0}, @@ -2206,7 +2254,7 @@ AVOutputFormat tg2_muxer = { CODEC_ID_AMR_NB, CODEC_ID_H263, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER, .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0}, @@ -2222,7 +2270,7 @@ AVOutputFormat ipod_muxer = { CODEC_ID_AAC, CODEC_ID_H264, mov_write_header, - mov_write_packet, + ff_mov_write_packet, mov_write_trailer, .flags = AVFMT_GLOBALHEADER, .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0}, diff --git a/libavformat/movenc.h b/libavformat/movenc.h new file mode 100644 index 0000000000..182c5edc8c --- /dev/null +++ b/libavformat/movenc.h @@ -0,0 +1,120 @@ +/* + * MOV, 3GP, MP4 muxer + * Copyright (c) 2003 Thomas Raivio + * Copyright (c) 2004 Gildas Bazin + * Copyright (c) 2009 Baptiste Coudurier + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFORMAT_MOVENC_H +#define AVFORMAT_MOVENC_H + +#include "avformat.h" + +#define MOV_INDEX_CLUSTER_SIZE 16384 +#define MOV_TIMESCALE 1000 + +#define RTP_MAX_PACKET_SIZE 1450 + +#define MODE_MP4 0x01 +#define MODE_MOV 0x02 +#define MODE_3GP 0x04 +#define MODE_PSP 0x08 // example working PSP command line: +// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4 +#define MODE_3G2 0x10 +#define MODE_IPOD 0x20 + +typedef struct MOVIentry { + unsigned int size; + uint64_t pos; + unsigned int samplesInChunk; + unsigned int entries; + int cts; + int64_t dts; +#define MOV_SYNC_SAMPLE 0x0001 +#define MOV_PARTIAL_SYNC_SAMPLE 0x0002 + uint32_t flags; +} MOVIentry; + +typedef struct HintSample { + uint8_t *data; + int size; + int sample_number; + int offset; + int own_data; +} HintSample; + +typedef struct { + int size; + int len; + HintSample *samples; +} HintSampleQueue; + +typedef struct MOVIndex { + int mode; + int entry; + unsigned timescale; + uint64_t time; + int64_t trackDuration; + long sampleCount; + long sampleSize; + int hasKeyframes; +#define MOV_TRACK_CTTS 0x0001 +#define MOV_TRACK_STPS 0x0002 + uint32_t flags; + int language; + int trackID; + int tag; ///< stsd fourcc + AVCodecContext *enc; + + int vosLen; + uint8_t *vosData; + MOVIentry *cluster; + int audio_vbr; + int height; ///< active picture (w/o VBI) height for D-10/IMX + uint32_t tref_tag; + int tref_id; ///< trackID of the referenced track + + int hint_track; ///< the track that hints this track, -1 if no hint track is set + int src_track; ///< the track that this hint track describes + AVFormatContext *rtp_ctx; ///< the format context for the hinting rtp muxer + uint32_t prev_rtp_ts; + int64_t cur_rtp_ts_unwrapped; + uint32_t max_packet_size; + + HintSampleQueue sample_queue; +} MOVTrack; + +typedef struct MOVMuxContext { + int mode; + int64_t time; + int nb_streams; + int chapter_track; ///< qt chapter track number + int64_t mdat_pos; + uint64_t mdat_size; + MOVTrack *tracks; +} MOVMuxContext; + +int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt); + +int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index); +int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, + int track_index, int sample); +void ff_mov_close_hinting(MOVTrack *track); + +#endif /* AVFORMAT_MOVENC_H */ diff --git a/libavformat/movenchint.c b/libavformat/movenchint.c new file mode 100644 index 0000000000..d90ac67911 --- /dev/null +++ b/libavformat/movenchint.c @@ -0,0 +1,504 @@ +/* + * MOV, 3GP, MP4 muxer RTP hinting + * Copyright (c) 2010 Martin Storsjo + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "movenc.h" +#include "libavutil/intreadwrite.h" + +int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index) +{ + MOVMuxContext *mov = s->priv_data; + MOVTrack *track = &mov->tracks[index]; + MOVTrack *src_track = &mov->tracks[src_index]; + AVStream *src_st = s->streams[src_index]; + int ret = AVERROR(ENOMEM); + AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL); + + track->tag = MKTAG('r','t','p',' '); + track->src_track = src_index; + + if (!rtp_format) { + ret = AVERROR(ENOENT); + goto fail; + } + + track->enc = avcodec_alloc_context(); + if (!track->enc) + goto fail; + track->enc->codec_type = AVMEDIA_TYPE_DATA; + track->enc->codec_tag = track->tag; + + track->rtp_ctx = avformat_alloc_context(); + if (!track->rtp_ctx) + goto fail; + track->rtp_ctx->oformat = rtp_format; + if (!av_new_stream(track->rtp_ctx, 0)) + goto fail; + + /* Copy stream parameters */ + track->rtp_ctx->streams[0]->sample_aspect_ratio = + src_st->sample_aspect_ratio; + + /* Remove the allocated codec context, link to the original one + * instead, to give the rtp muxer access to codec parameters. */ + av_free(track->rtp_ctx->streams[0]->codec); + track->rtp_ctx->streams[0]->codec = src_st->codec; + + if ((ret = url_open_dyn_packet_buf(&track->rtp_ctx->pb, + RTP_MAX_PACKET_SIZE)) < 0) + goto fail; + ret = av_write_header(track->rtp_ctx); + if (ret) + goto fail; + + /* Copy the RTP AVStream timebase back to the hint AVStream */ + track->timescale = track->rtp_ctx->streams[0]->time_base.den; + + /* Mark the hinted track that packets written to it should be + * sent to this track for hinting. */ + src_track->hint_track = index; + return 0; +fail: + av_log(s, AV_LOG_WARNING, + "Unable to initialize hinting of stream %d\n", src_index); + if (track->rtp_ctx && track->rtp_ctx->pb) { + uint8_t *buf; + url_close_dyn_buf(track->rtp_ctx->pb, &buf); + av_free(buf); + } + if (track->rtp_ctx && track->rtp_ctx->streams[0]) { + av_metadata_free(&track->rtp_ctx->streams[0]->metadata); + av_free(track->rtp_ctx->streams[0]); + } + if (track->rtp_ctx) { + av_metadata_free(&track->rtp_ctx->metadata); + av_free(track->rtp_ctx->priv_data); + av_freep(&track->rtp_ctx); + } + av_freep(&track->enc); + /* Set a default timescale, to avoid crashes in dump_format */ + track->timescale = 90000; + return ret; +} + +/** + * Remove the first sample from the sample queue. + */ +static void sample_queue_pop(HintSampleQueue *queue) +{ + if (queue->len <= 0) + return; + if (queue->samples[0].own_data) + av_free(queue->samples[0].data); + queue->len--; + memmove(queue->samples, queue->samples + 1, sizeof(HintSample)*queue->len); +} + +/** + * Empty the sample queue, releasing all memory. + */ +static void sample_queue_free(HintSampleQueue *queue) +{ + int i; + for (i = 0; i < queue->len; i++) + if (queue->samples[i].own_data) + av_free(queue->samples[i].data); + av_freep(&queue->samples); + queue->len = 0; + queue->size = 0; +} + +/** + * Add a reference to the sample data to the sample queue. The data is + * not copied. sample_queue_retain should be called before pkt->data + * is reused/freed. + */ +static void sample_queue_push(HintSampleQueue *queue, AVPacket *pkt, int sample) +{ + /* No need to keep track of smaller samples, since describing them + * with immediates is more efficient. */ + if (pkt->size <= 14) + return; + if (!queue->samples || queue->len >= queue->size) { + HintSample* samples; + queue->size += 10; + samples = av_realloc(queue->samples, sizeof(HintSample)*queue->size); + if (!samples) + return; + queue->samples = samples; + } + queue->samples[queue->len].data = pkt->data; + queue->samples[queue->len].size = pkt->size; + queue->samples[queue->len].sample_number = sample; + queue->samples[queue->len].offset = 0; + queue->samples[queue->len].own_data = 0; + queue->len++; +} + +/** + * Make local copies of all referenced sample data in the queue. + */ +static void sample_queue_retain(HintSampleQueue *queue) +{ + int i; + for (i = 0; i < queue->len; ) { + HintSample *sample = &queue->samples[i]; + if (!sample->own_data) { + uint8_t* ptr = av_malloc(sample->size); + if (!ptr) { + /* Unable to allocate memory for this one, remove it */ + memmove(queue->samples + i, queue->samples + i + 1, + sizeof(HintSample)*(queue->len - i - 1)); + queue->len--; + continue; + } + memcpy(ptr, sample->data, sample->size); + sample->data = ptr; + sample->own_data = 1; + } + i++; + } +} + +/** + * Find matches of needle[n_pos ->] within haystack. If a sufficiently + * large match is found, matching bytes before n_pos are included + * in the match, too (within the limits of the arrays). + * + * @param haystack buffer that may contain parts of needle + * @param h_len length of the haystack buffer + * @param needle buffer containing source data that have been used to + * construct haystack + * @param n_pos start position in needle used for looking for matches + * @param n_len length of the needle buffer + * @param match_h_offset_ptr offset of the first matching byte within haystack + * @param match_n_offset_ptr offset of the first matching byte within needle + * @param match_len_ptr length of the matched segment + * @return 0 if a match was found, < 0 if no match was found + */ +static int match_segments(const uint8_t *haystack, int h_len, + const uint8_t *needle, int n_pos, int n_len, + int *match_h_offset_ptr, int *match_n_offset_ptr, + int *match_len_ptr) +{ + int h_pos; + for (h_pos = 0; h_pos < h_len; h_pos++) { + int match_len = 0; + int match_h_pos, match_n_pos; + + /* Check how many bytes match at needle[n_pos] and haystack[h_pos] */ + while (h_pos + match_len < h_len && n_pos + match_len < n_len && + needle[n_pos + match_len] == haystack[h_pos + match_len]) + match_len++; + if (match_len <= 8) + continue; + + /* If a sufficiently large match was found, try to expand + * the matched segment backwards. */ + match_h_pos = h_pos; + match_n_pos = n_pos; + while (match_n_pos > 0 && match_h_pos > 0 && + needle[match_n_pos - 1] == haystack[match_h_pos - 1]) { + match_n_pos--; + match_h_pos--; + match_len++; + } + if (match_len <= 14) + continue; + *match_h_offset_ptr = match_h_pos; + *match_n_offset_ptr = match_n_pos; + *match_len_ptr = match_len; + return 0; + } + return -1; +} + +/** + * Look for segments in samples in the sample queue matching the data + * in ptr. Samples not matching are removed from the queue. If a match + * is found, the next time it will look for matches starting from the + * end of the previous matched segment. + * + * @param data data to find matches for in the sample queue + * @param len length of the data buffer + * @param queue samples used for looking for matching segments + * @param pos the offset in data of the matched segment + * @param match_sample the number of the sample that contained the match + * @param match_offset the offset of the matched segment within the sample + * @param match_len the length of the matched segment + * @return 0 if a match was found, < 0 if no match was found + */ +static int find_sample_match(const uint8_t *data, int len, + HintSampleQueue *queue, int *pos, + int *match_sample, int *match_offset, + int *match_len) +{ + while (queue->len > 0) { + HintSample *sample = &queue->samples[0]; + /* If looking for matches in a new sample, skip the first 5 bytes, + * since they often may be modified/removed in the output packet. */ + if (sample->offset == 0 && sample->size > 5) + sample->offset = 5; + + if (match_segments(data, len, sample->data, sample->offset, + sample->size, pos, match_offset, match_len) == 0) { + *match_sample = sample->sample_number; + /* Next time, look for matches at this offset, with a little + * margin to this match. */ + sample->offset = *match_offset + *match_len + 5; + if (sample->offset + 10 >= sample->size) + sample_queue_pop(queue); /* Not enough useful data left */ + return 0; + } + + if (sample->offset < 10 && sample->size > 20) { + /* No match found from the start of the sample, + * try from the middle of the sample instead. */ + sample->offset = sample->size/2; + } else { + /* No match for this sample, remove it */ + sample_queue_pop(queue); + } + } + return -1; +} + +static void output_immediate(const uint8_t *data, int size, + ByteIOContext *out, int *entries) +{ + while (size > 0) { + int len = size; + if (len > 14) + len = 14; + put_byte(out, 1); /* immediate constructor */ + put_byte(out, len); /* amount of valid data */ + put_buffer(out, data, len); + data += len; + size -= len; + + for (; len < 14; len++) + put_byte(out, 0); + + (*entries)++; + } +} + +static void output_match(ByteIOContext *out, int match_sample, + int match_offset, int match_len, int *entries) +{ + put_byte(out, 2); /* sample constructor */ + put_byte(out, 0); /* track reference */ + put_be16(out, match_len); + put_be32(out, match_sample); + put_be32(out, match_offset); + put_be16(out, 1); /* bytes per block */ + put_be16(out, 1); /* samples per block */ + (*entries)++; +} + +static void describe_payload(const uint8_t *data, int size, + ByteIOContext *out, int *entries, + HintSampleQueue *queue) +{ + /* Describe the payload using different constructors */ + while (size > 0) { + int match_sample, match_offset, match_len, pos; + if (find_sample_match(data, size, queue, &pos, &match_sample, + &match_offset, &match_len) < 0) + break; + output_immediate(data, pos, out, entries); + data += pos; + size -= pos; + output_match(out, match_sample, match_offset, match_len, entries); + data += match_len; + size -= match_len; + } + output_immediate(data, size, out, entries); +} + +/** + * Write an RTP hint (that may contain one or more RTP packets) + * for the packets in data. data contains one or more packets with a + * BE32 size header. + * + * @param out buffer where the hints are written + * @param data buffer containing RTP packets + * @param size the size of the data buffer + * @param trk the MOVTrack for the hint track + * @param pts pointer where the timestamp for the written RTP hint is stored + * @return the number of RTP packets in the written hint + */ +static int write_hint_packets(ByteIOContext *out, const uint8_t *data, + int size, MOVTrack *trk, int64_t *pts) +{ + int64_t curpos; + int64_t count_pos, entries_pos; + int count = 0, entries; + + count_pos = url_ftell(out); + /* RTPsample header */ + put_be16(out, 0); /* packet count */ + put_be16(out, 0); /* reserved */ + + while (size > 4) { + uint32_t packet_len = AV_RB32(data); + uint16_t seq; + uint32_t ts; + + data += 4; + size -= 4; + if (packet_len > size || packet_len <= 12) + break; + if (data[1] >= 200 && data[1] <= 204) { + /* RTCP packet, just skip */ + data += packet_len; + size -= packet_len; + continue; + } + + if (packet_len > trk->max_packet_size) + trk->max_packet_size = packet_len; + + seq = AV_RB16(&data[2]); + ts = AV_RB32(&data[4]); + + if (trk->prev_rtp_ts == 0) + trk->prev_rtp_ts = ts; + /* Unwrap the 32-bit RTP timestamp that wraps around often + * into a not (as often) wrapping 64-bit timestamp. */ + trk->cur_rtp_ts_unwrapped += (int32_t) (ts - trk->prev_rtp_ts); + trk->prev_rtp_ts = ts; + if (*pts == AV_NOPTS_VALUE) + *pts = trk->cur_rtp_ts_unwrapped; + + count++; + /* RTPpacket header */ + put_be32(out, 0); /* relative_time */ + put_buffer(out, data, 2); /* RTP header */ + put_be16(out, seq); /* RTPsequenceseed */ + put_be16(out, 0); /* reserved + flags */ + entries_pos = url_ftell(out); + put_be16(out, 0); /* entry count */ + + data += 12; + size -= 12; + packet_len -= 12; + + entries = 0; + /* Write one or more constructors describing the payload data */ + describe_payload(data, packet_len, out, &entries, &trk->sample_queue); + data += packet_len; + size -= packet_len; + + curpos = url_ftell(out); + url_fseek(out, entries_pos, SEEK_SET); + put_be16(out, entries); + url_fseek(out, curpos, SEEK_SET); + } + + curpos = url_ftell(out); + url_fseek(out, count_pos, SEEK_SET); + put_be16(out, count); + url_fseek(out, curpos, SEEK_SET); + return count; +} + +int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, + int track_index, int sample) +{ + MOVMuxContext *mov = s->priv_data; + MOVTrack *trk = &mov->tracks[track_index]; + AVFormatContext *rtp_ctx = trk->rtp_ctx; + uint8_t *buf = NULL; + int size; + ByteIOContext *hintbuf = NULL; + AVPacket hint_pkt; + AVPacket local_pkt; + int ret = 0, count; + + if (!rtp_ctx) + return AVERROR(ENOENT); + if (!rtp_ctx->pb) + return AVERROR(ENOMEM); + + sample_queue_push(&trk->sample_queue, pkt, sample); + + /* Feed the packet to the RTP muxer */ + local_pkt = *pkt; + local_pkt.stream_index = 0; + local_pkt.pts = av_rescale_q(pkt->pts, + s->streams[pkt->stream_index]->time_base, + rtp_ctx->streams[0]->time_base); + local_pkt.dts = av_rescale_q(pkt->dts, + s->streams[pkt->stream_index]->time_base, + rtp_ctx->streams[0]->time_base); + av_write_frame(rtp_ctx, &local_pkt); + + /* Fetch the output from the RTP muxer, open a new output buffer + * for next time. */ + size = url_close_dyn_buf(rtp_ctx->pb, &buf); + if ((ret = url_open_dyn_packet_buf(&rtp_ctx->pb, + RTP_MAX_PACKET_SIZE)) < 0) + goto done; + + if (size <= 0) + goto done; + + /* Open a buffer for writing the hint */ + if ((ret = url_open_dyn_buf(&hintbuf)) < 0) + goto done; + av_init_packet(&hint_pkt); + count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts); + av_freep(&buf); + + /* Write the hint data into the hint track */ + hint_pkt.size = size = url_close_dyn_buf(hintbuf, &buf); + hint_pkt.data = buf; + hint_pkt.pts = hint_pkt.dts; + hint_pkt.stream_index = track_index; + if (pkt->flags & AV_PKT_FLAG_KEY) + hint_pkt.flags |= AV_PKT_FLAG_KEY; + if (count > 0) + ff_mov_write_packet(s, &hint_pkt); +done: + av_free(buf); + sample_queue_retain(&trk->sample_queue); + return ret; +} + +void ff_mov_close_hinting(MOVTrack *track) { + AVFormatContext* rtp_ctx = track->rtp_ctx; + uint8_t *ptr; + + av_freep(&track->enc); + sample_queue_free(&track->sample_queue); + if (!rtp_ctx) + return; + if (rtp_ctx->pb) { + av_write_trailer(rtp_ctx); + url_close_dyn_buf(rtp_ctx->pb, &ptr); + av_free(ptr); + } + av_metadata_free(&rtp_ctx->streams[0]->metadata); + av_metadata_free(&rtp_ctx->metadata); + av_free(rtp_ctx->streams[0]); + av_freep(&rtp_ctx); +} + diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index c92a9c2585..93bb47d837 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -807,6 +807,16 @@ static int mpegts_push_data(MpegTSFilter *filter, pes->data_index += buf_size; } buf_size = 0; + /* emit complete packets with known packet size + * decreases demuxer delay for infrequent packets like subtitles from + * a couple of seconds to milliseconds for properly muxed files. + * total_size is the number of bytes following pes_packet_length + * in the pes header, i.e. not counting the first 6 bytes */ + if (pes->total_size < MAX_PES_PAYLOAD && + pes->pes_header_size + pes->data_index == pes->total_size + 6) { + ts->stop_parse = 1; + new_pes_packet(pes, ts->pkt); + } break; case MPEGTS_SKIP: buf_size = 0; @@ -1130,7 +1140,7 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet) { AVFormatContext *s = ts->stream; MpegTSFilter *tss; - int len, pid, cc, cc_ok, afc, is_start; + int len, pid, cc, expected_cc, cc_ok, afc, is_start; const uint8_t *p, *p_end; int64_t pos; @@ -1148,7 +1158,8 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet) /* continuity check (currently not used) */ cc = (packet[3] & 0xf); - cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc)); + expected_cc = (packet[3] & 0x10) ? (tss->last_cc + 1) & 0x0f : tss->last_cc; + cc_ok = (tss->last_cc < 0) || (expected_cc == cc); tss->last_cc = cc; /* skip adaptation field */ diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 168fd8d69f..a601007aec 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -592,7 +592,7 @@ static int mxf_read_generic_descriptor(MXFDescriptor *descriptor, ByteIOContext default: /* Private uid used by SONY C0023S01.mxf */ if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) { - descriptor->extradata = av_malloc(size); + descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); if (!descriptor->extradata) return -1; descriptor->extradata_size = size; diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c index ab381189c9..ab11df692e 100644 --- a/libavformat/mxfenc.c +++ b/libavformat/mxfenc.c @@ -1536,7 +1536,7 @@ static const uint8_t system_metadata_package_set_key[] = { 0x06,0x0E,0x2B,0x34,0 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps) { return (0 << 31) | // color frame flag - (0 << 30) | // drop frame flag + (drop << 30) | // drop frame flag ( ((frame % fps) / 10) << 28) | // tens of frames ( ((frame % fps) % 10) << 24) | // units of frames (0 << 23) | // field phase (NTSC), b0 (PAL) diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c index 44e5097d59..169e95e321 100644 --- a/libavformat/nsvdec.c +++ b/libavformat/nsvdec.c @@ -319,7 +319,9 @@ static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap) char *token, *value; char quote; - p = strings = av_mallocz(strings_size + 1); + p = strings = av_mallocz((size_t)strings_size + 1); + if (!p) + return AVERROR(ENOMEM); endp = strings + strings_size; get_buffer(pb, strings, strings_size); while (p < endp) { @@ -354,6 +356,8 @@ static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap) if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t)) return -1; nsv->nsvs_file_offset = av_malloc((unsigned)table_entries_used * sizeof(uint32_t)); + if (!nsv->nsvs_file_offset) + return AVERROR(ENOMEM); for(i=0;insvs_file_offset[i] = get_le32(pb) + size; @@ -361,6 +365,8 @@ static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap) if(table_entries > table_entries_used && get_le32(pb) == MKTAG('T','O','C','2')) { nsv->nsvs_timestamps = av_malloc((unsigned)table_entries_used*sizeof(uint32_t)); + if (!nsv->nsvs_timestamps) + return AVERROR(ENOMEM); for(i=0;insvs_timestamps[i] = get_le32(pb); } @@ -530,11 +536,16 @@ static int nsv_read_header(AVFormatContext *s, AVFormatParameters *ap) for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) { if (nsv_resync(s) < 0) return -1; - if (nsv->state == NSV_FOUND_NSVF) + if (nsv->state == NSV_FOUND_NSVF) { err = nsv_parse_NSVf_header(s, ap); + if (err < 0) + return err; + } /* we need the first NSVs also... */ if (nsv->state == NSV_FOUND_NSVS) { err = nsv_parse_NSVs_header(s, ap); + if (err < 0) + return err; break; /* we just want the first one */ } } @@ -609,12 +620,12 @@ null_chunk_retry: } /* map back streams to v,a */ - if (s->streams[0]) + if (s->nb_streams > 0) st[s->streams[0]->id] = s->streams[0]; - if (s->streams[1]) + if (s->nb_streams > 1) st[s->streams[1]->id] = s->streams[1]; - if (vsize/* && st[NSV_ST_VIDEO]*/) { + if (vsize && st[NSV_ST_VIDEO]) { nst = st[NSV_ST_VIDEO]->priv_data; pkt = &nsv->ahead[NSV_ST_VIDEO]; av_get_packet(pb, pkt, vsize); @@ -629,7 +640,7 @@ null_chunk_retry: if(st[NSV_ST_VIDEO]) ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++; - if (asize/*st[NSV_ST_AUDIO]*/) { + if (asize && st[NSV_ST_AUDIO]) { nst = st[NSV_ST_AUDIO]->priv_data; pkt = &nsv->ahead[NSV_ST_AUDIO]; /* read raw audio specific header on the first audio chunk... */ diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c index 522affbb57..5d5cd557b3 100644 --- a/libavformat/nutdec.c +++ b/libavformat/nutdec.c @@ -334,7 +334,8 @@ static int decode_stream_header(NUTContext *nut){ return -1; } if(class<3 && st->codec->codec_id == CODEC_ID_NONE) - av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); + av_log(s, AV_LOG_ERROR, "Unknown codec tag '0x%04x' for stream number %d\n", + (unsigned int)tmp, stream_id); GET_V(stc->time_base_id , tmp < nut->time_base_count); GET_V(stc->msb_pts_shift , tmp < 16); diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c index f24f697ada..bd00d1a4e6 100644 --- a/libavformat/oggdec.c +++ b/libavformat/oggdec.c @@ -94,14 +94,24 @@ ogg_restore (AVFormatContext * s, int discard) ogg->state = ost->next; if (!discard){ + struct ogg_stream *old_streams = ogg->streams; + for (i = 0; i < ogg->nstreams; i++) av_free (ogg->streams[i].buf); url_fseek (bc, ost->pos, SEEK_SET); ogg->curidx = ost->curidx; ogg->nstreams = ost->nstreams; - memcpy(ogg->streams, ost->streams, - ost->nstreams * sizeof(*ogg->streams)); + ogg->streams = av_realloc (ogg->streams, + ogg->nstreams * sizeof (*ogg->streams)); + + if (ogg->streams) { + memcpy(ogg->streams, ost->streams, + ost->nstreams * sizeof(*ogg->streams)); + } else { + av_free(old_streams); + ogg->nstreams = 0; + } } av_free (ost); @@ -577,20 +587,19 @@ ogg_read_close (AVFormatContext * s) } -static int64_t -ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg, - int64_t pos_limit) +static int64_t ogg_read_timestamp (AVFormatContext * s, int stream_index, + int64_t * pos_arg, int64_t pos_limit) { struct ogg *ogg = s->priv_data; - struct ogg_stream *os = ogg->streams + stream_index; ByteIOContext *bc = s->pb; int64_t pts = AV_NOPTS_VALUE; - int i; + int i = -1; url_fseek(bc, *pos_arg, SEEK_SET); ogg_reset(ogg); while (url_ftell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) { if (i == stream_index) { + struct ogg_stream *os = ogg->streams + stream_index; pts = ogg_calc_pts(s, i, NULL); if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY)) pts = AV_NOPTS_VALUE; @@ -615,6 +624,7 @@ static int ogg_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp os->keyframe_seek = 1; ret = av_seek_frame_binary(s, stream_index, timestamp, flags); + os = ogg->streams + stream_index; if (ret < 0) os->keyframe_seek = 0; return ret; @@ -642,4 +652,5 @@ AVInputFormat ogg_demuxer = { ogg_read_timestamp, .extensions = "ogg", .metadata_conv = ff_vorbiscomment_metadata_conv, + .flags = AVFMT_GENERIC_INDEX, }; diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c index 36ea21cd41..70264a4383 100644 --- a/libavformat/oggenc.c +++ b/libavformat/oggenc.c @@ -27,6 +27,18 @@ #include "internal.h" #include "vorbiscomment.h" +#define MAX_PAGE_SIZE 65025 + +typedef struct { + int64_t granule; + int stream_index; + uint8_t flags; + uint8_t segments_count; + uint8_t segments[255]; + uint8_t data[MAX_PAGE_SIZE]; + uint16_t size; +} OGGPage; + typedef struct { int64_t duration; unsigned page_counter; @@ -37,9 +49,19 @@ typedef struct { int64_t last_kf_pts; int vrev; int eos; - unsigned packet_count; ///< number of packet buffered + unsigned page_count; ///< number of page buffered + OGGPage page; ///< current page } OGGStreamContext; +typedef struct OGGPageList { + OGGPage page; + struct OGGPageList *next; +} OGGPageList; + +typedef struct { + OGGPageList *page_list; +} OGGContext; + static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset) { int64_t pos = url_ftell(s->pb); @@ -49,39 +71,122 @@ static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset) url_fseek(s->pb, pos, SEEK_SET); } -static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size, - int64_t granule, int stream_index, int flags) +static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags) { - OGGStreamContext *oggstream = s->streams[stream_index]->priv_data; + OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data; int64_t crc_offset; - int page_segments, i; - - if (size >= 255*255) { - granule = -1; - size = 255*255; - } else if (oggstream->eos) - flags |= 4; - - page_segments = FFMIN(size/255 + 1, 255); init_checksum(s->pb, ff_crc04C11DB7_update, 0); put_tag(s->pb, "OggS"); put_byte(s->pb, 0); - put_byte(s->pb, flags); - put_le64(s->pb, granule); - put_le32(s->pb, stream_index); + put_byte(s->pb, page->flags | extra_flags); + put_le64(s->pb, page->granule); + put_le32(s->pb, page->stream_index); put_le32(s->pb, oggstream->page_counter++); crc_offset = url_ftell(s->pb); put_le32(s->pb, 0); // crc - put_byte(s->pb, page_segments); - for (i = 0; i < page_segments-1; i++) - put_byte(s->pb, 255); - put_byte(s->pb, size - (page_segments-1)*255); - put_buffer(s->pb, data, size); + put_byte(s->pb, page->segments_count); + put_buffer(s->pb, page->segments, page->segments_count); + put_buffer(s->pb, page->data, page->size); ogg_update_checksum(s, crc_offset); put_flush_packet(s->pb); - return size; + oggstream->page_count--; +} + +static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, OGGPage *page) +{ + if (oggstream->kfgshift) + return (page->granule>>oggstream->kfgshift) + + (page->granule & ((1<kfgshift)-1)); + else + return page->granule; +} + +static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page) +{ + AVStream *st2 = s->streams[next->stream_index]; + AVStream *st = s->streams[page->stream_index]; + int64_t next_granule, cur_granule; + + if (next->granule == -1 || page->granule == -1) + return 0; + + next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next), + st2->time_base, AV_TIME_BASE_Q); + cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page), + st ->time_base, AV_TIME_BASE_Q); + return next_granule > cur_granule; +} + +static int ogg_reset_cur_page(OGGStreamContext *oggstream) +{ + oggstream->page.granule = -1; + oggstream->page.flags = 0; + oggstream->page.segments_count = 0; + oggstream->page.size = 0; + return 0; +} + +static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream) +{ + OGGContext *ogg = s->priv_data; + OGGPageList **p = &ogg->page_list; + OGGPageList *l = av_mallocz(sizeof(*l)); + + if (!l) + return AVERROR(ENOMEM); + l->page = oggstream->page; + + oggstream->page_count++; + ogg_reset_cur_page(oggstream); + + while (*p) { + if (ogg_compare_granule(s, &(*p)->page, &l->page)) + break; + p = &(*p)->next; + } + l->next = *p; + *p = l; + + return 0; +} + +static int ogg_buffer_data(AVFormatContext *s, AVStream *st, + uint8_t *data, unsigned size, int64_t granule) +{ + OGGStreamContext *oggstream = st->priv_data; + int total_segments = size / 255 + 1; + uint8_t *p = data; + int i, segments, len; + + for (i = 0; i < total_segments; ) { + OGGPage *page = &oggstream->page; + + segments = FFMIN(total_segments - i, 255 - page->segments_count); + + if (i && !page->segments_count) + page->flags |= 1; // continued packet + + memset(page->segments+page->segments_count, 255, segments - 1); + page->segments_count += segments - 1; + + len = FFMIN(size, segments*255); + page->segments[page->segments_count++] = len - (segments-1)*255; + memcpy(page->data+page->size, p, len); + p += len; + size -= len; + i += segments; + page->size += len; + + if (i == total_segments) + page->granule = granule; + + if (page->segments_count == 255) { + ogg_buffer_page(s, oggstream); + } + } + return 0; } static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact, @@ -195,6 +300,7 @@ static int ogg_write_header(AVFormatContext *s) return -1; } oggstream = av_mallocz(sizeof(*oggstream)); + oggstream->page.stream_index = i; st->priv_data = oggstream; if (st->codec->codec_id == CODEC_ID_FLAC) { int err = ogg_build_flac_headers(st->codec, oggstream, @@ -232,25 +338,54 @@ static int ogg_write_header(AVFormatContext *s) } } } - for (i = 0; i < 3; i++) { - for (j = 0; j < s->nb_streams; j++) { - AVStream *st = s->streams[j]; - OGGStreamContext *oggstream = st->priv_data; - if (oggstream && oggstream->header_len[i]) { - ogg_write_page(s, oggstream->header[i], oggstream->header_len[i], - 0, st->index, i ? 0 : 2); // bos - } + + for (j = 0; j < s->nb_streams; j++) { + OGGStreamContext *oggstream = s->streams[j]->priv_data; + ogg_buffer_data(s, s->streams[j], oggstream->header[0], + oggstream->header_len[0], 0); + oggstream->page.flags |= 2; // bos + ogg_buffer_page(s, oggstream); + } + for (j = 0; j < s->nb_streams; j++) { + AVStream *st = s->streams[j]; + OGGStreamContext *oggstream = st->priv_data; + for (i = 1; i < 3; i++) { + if (oggstream && oggstream->header_len[i]) + ogg_buffer_data(s, st, oggstream->header[i], + oggstream->header_len[i], 0); } + ogg_buffer_page(s, oggstream); } return 0; } +static void ogg_write_pages(AVFormatContext *s, int flush) +{ + OGGContext *ogg = s->priv_data; + OGGPageList *next, *p; + + if (!ogg->page_list) + return; + + for (p = ogg->page_list; p; ) { + OGGStreamContext *oggstream = + s->streams[p->page.stream_index]->priv_data; + if (oggstream->page_count < 2 && !flush) + break; + ogg_write_page(s, &p->page, + flush && oggstream->page_count == 1 ? 4 : 0); // eos + next = p->next; + av_freep(&p); + p = next; + } + ogg->page_list = p; +} + static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; OGGStreamContext *oggstream = st->priv_data; - uint8_t *ptr = pkt->data; - int ret, size = pkt->size; + int ret; int64_t granule; if (st->codec->codec_id == CODEC_ID_THEORA) { @@ -268,72 +403,26 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) } else granule = pkt->pts + pkt->duration; oggstream->duration = granule; - do { - ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data); - ptr += ret; size -= ret; - } while (size > 0 || ret == 255*255); // need to output a last nil page + + ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule); + if (ret < 0) + return ret; + + ogg_write_pages(s, 0); return 0; } -static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt) -{ - AVStream *st2 = s->streams[next->stream_index]; - AVStream *st = s->streams[pkt ->stream_index]; - - int64_t next_granule = av_rescale_q(next->pts + next->duration, - st2->time_base, AV_TIME_BASE_Q); - int64_t cur_granule = av_rescale_q(pkt ->pts + pkt ->duration, - st ->time_base, AV_TIME_BASE_Q); - return next_granule > cur_granule; -} - -static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) -{ - OGGStreamContext *ogg; - int i, stream_count = 0; - int interleaved = 0; - - if (pkt) { - ff_interleave_add_packet(s, pkt, ogg_compare_granule); - ogg = s->streams[pkt->stream_index]->priv_data; - ogg->packet_count++; - } - - for (i = 0; i < s->nb_streams; i++) { - ogg = s->streams[i]->priv_data; - stream_count += !!ogg->packet_count; - interleaved += ogg->packet_count > 1; - } - - if ((s->nb_streams == stream_count && interleaved == stream_count) || - (flush && stream_count)) { - AVPacketList *pktl= s->packet_buffer; - *out= pktl->pkt; - s->packet_buffer = pktl->next; - - ogg = s->streams[out->stream_index]->priv_data; - if (flush && ogg->packet_count == 1) - ogg->eos = 1; - ogg->packet_count--; - - if(!s->packet_buffer) - s->packet_buffer_end= NULL; - - if(s->streams[out->stream_index]->last_in_packet_buffer == pktl) - s->streams[out->stream_index]->last_in_packet_buffer= NULL; - - av_freep(&pktl); - return 1; - } else { - av_init_packet(out); - return 0; - } -} - static int ogg_write_trailer(AVFormatContext *s) { int i; + + /* flush current page */ + for (i = 0; i < s->nb_streams; i++) + ogg_buffer_page(s, s->streams[i]->priv_data); + + ogg_write_pages(s, 1); + for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; OGGStreamContext *oggstream = st->priv_data; @@ -352,12 +441,11 @@ AVOutputFormat ogg_muxer = { NULL_IF_CONFIG_SMALL("Ogg"), "application/ogg", "ogg,ogv,spx", - 0, + sizeof(OGGContext), CODEC_ID_FLAC, CODEC_ID_THEORA, ogg_write_header, ogg_write_packet, ogg_write_trailer, - .interleave_packet = ogg_interleave_per_granule, .metadata_conv = ff_vorbiscomment_metadata_conv, }; diff --git a/libavformat/options.c b/libavformat/options.c index 85d793ec09..0c1ac2d866 100644 --- a/libavformat/options.c +++ b/libavformat/options.c @@ -49,6 +49,7 @@ static const AVOption options[]={ {"nofillin", "do not fill in missing values that can be exactly calculated", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_NOFILLIN, INT_MIN, INT_MAX, D, "fflags"}, {"noparse", "disable AVParsers, this needs nofillin too", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_NOPARSE, INT_MIN, INT_MAX, D, "fflags"}, {"igndts", "ingore dts", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_IGNDTS, INT_MIN, INT_MAX, D, "fflags"}, +{"rtphint", "add rtp hinting", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_RTP_HINT, INT_MIN, INT_MAX, E, "fflags"}, #if LIBAVFORMAT_VERSION_INT < (53<<16) {"track", " set the track number", OFFSET(track), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E}, {"year", "set the year", OFFSET(year), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, E}, diff --git a/libavformat/riff.c b/libavformat/riff.c index 340fd760c0..86899805fa 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -80,6 +80,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { CODEC_ID_MPEG4, MKTAG('V', 'S', 'P', 'X') }, { CODEC_ID_MPEG4, MKTAG('U', 'L', 'D', 'X') }, { CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'V') }, + { CODEC_ID_MPEG4, MKTAG('S', 'I', 'P', 'P') }, /* Samsung SHR-6040 */ { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */ { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') }, @@ -122,6 +123,8 @@ const AVCodecTag ff_codec_bmp_tags[] = { { CODEC_ID_MPEG2VIDEO, MKTAG('L', 'M', 'P', '2') }, /* Lead MPEG2 in avi */ { CODEC_ID_MPEG2VIDEO, MKTAG('s', 'l', 'i', 'f') }, { CODEC_ID_MPEG2VIDEO, MKTAG('E', 'M', '2', 'V') }, + { CODEC_ID_MPEG2VIDEO, MKTAG('M', '7', '0', '1') }, /* Matrox MPEG2 intra-only */ + { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', 'v') }, { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') }, { CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') }, { CODEC_ID_MJPEG, MKTAG('d', 'm', 'b', '1') }, @@ -182,6 +185,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') }, { CODEC_ID_VP6F, MKTAG('V', 'P', '6', 'F') }, { CODEC_ID_VP6F, MKTAG('F', 'L', 'V', '4') }, + { CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') }, { CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') }, { CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') }, { CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') }, diff --git a/libavformat/rtmppkt.c b/libavformat/rtmppkt.c index 58c3abe345..b998423184 100644 --- a/libavformat/rtmppkt.c +++ b/libavformat/rtmppkt.c @@ -275,11 +275,11 @@ int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end) data++; break; } - if (data + size >= data_end || data + size < data) + if (size < 0 || size >= data_end - data) return -1; data += size; t = ff_amf_tag_size(data, data_end); - if (t < 0 || data + t >= data_end) + if (t < 0 || t >= data_end - data) return -1; data += t; } @@ -308,7 +308,7 @@ int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end, int size = bytestream_get_be16(&data); if (!size) break; - if (data + size >= data_end || data + size < data) + if (size < 0 || size >= data_end - data) return -1; data += size; if (size == namelen && !memcmp(data-size, name, namelen)) { @@ -329,7 +329,7 @@ int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end, return 0; } len = ff_amf_tag_size(data, data_end); - if (len < 0 || data + len >= data_end || data + len < data) + if (len < 0 || len >= data_end - data) return -1; data += len; } @@ -359,7 +359,7 @@ static const char* rtmp_packet_type(int type) static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end) { - int size; + unsigned int size; char buf[1024]; if (data >= data_end) @@ -378,7 +378,7 @@ static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *d } else { size = bytestream_get_be32(&data); } - size = FFMIN(size, 1023); + size = FFMIN(size, sizeof(buf) - 1); memcpy(buf, data, size); buf[size] = 0; av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf); @@ -391,22 +391,21 @@ static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *d case AMF_DATA_TYPE_OBJECT: av_log(ctx, AV_LOG_DEBUG, " {\n"); for (;;) { - int size = bytestream_get_be16(&data); int t; - memcpy(buf, data, size); - buf[size] = 0; + size = bytestream_get_be16(&data); + av_strlcpy(buf, data, FFMIN(sizeof(buf), size + 1)); if (!size) { av_log(ctx, AV_LOG_DEBUG, " }\n"); data++; break; } - if (data + size >= data_end || data + size < data) + if (size >= data_end - data) return; data += size; av_log(ctx, AV_LOG_DEBUG, " %s: ", buf); ff_amf_tag_contents(ctx, data, data_end); t = ff_amf_tag_size(data, data_end); - if (t < 0 || data + t >= data_end) + if (t < 0 || t >= data_end - data) return; data += t; } diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index fec91f11b8..6dbd796d31 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -1337,6 +1337,8 @@ static int rtsp_read_play(AVFormatContext *s) RTSPStream *rtsp_st = rt->rtsp_streams[i]; RTPDemuxContext *rtpctx = rtsp_st->transport_priv; AVStream *st = NULL; + if (!rtpctx) + continue; if (rtsp_st->stream_index >= 0) st = s->streams[rtsp_st->stream_index]; rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE; diff --git a/libavformat/sdp.c b/libavformat/sdp.c index a8cab6a68f..6bf05dbe39 100644 --- a/libavformat/sdp.c +++ b/libavformat/sdp.c @@ -294,7 +294,7 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, return buff; } -static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl) +void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl) { const char *type; int payload_type; @@ -352,7 +352,7 @@ int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size) resolve_destination(dst, sizeof(dst)); } for (j = 0; j < ac[i]->nb_streams; j++) { - sdp_write_media(buff, size, + ff_sdp_write_media(buff, size, ac[i]->streams[j]->codec, dst[0] ? dst : NULL, (port > 0) ? port + j * 2 : 0, ttl); if (port <= 0) { @@ -369,4 +369,9 @@ int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size) { return AVERROR(ENOSYS); } + +void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, + const char *dest_addr, int port, int ttl) +{ +} #endif diff --git a/libavformat/smacker.c b/libavformat/smacker.c index 0dcc286556..5cefe7bb4e 100644 --- a/libavformat/smacker.c +++ b/libavformat/smacker.c @@ -289,10 +289,15 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) if(flags & 1) { int size; size = get_le32(s->pb) - 4; + uint8_t *tmpbuf; + frame_size -= size; frame_size -= 4; smk->curstream++; - smk->bufs[smk->curstream] = av_realloc(smk->bufs[smk->curstream], size); + tmpbuf = av_realloc(smk->bufs[smk->curstream], size); + if (!tmpbuf) + return AVERROR(ENOMEM); + smk->bufs[smk->curstream] = tmpbuf; smk->buf_sizes[smk->curstream] = size; ret = get_buffer(s->pb, smk->bufs[smk->curstream], size); if(ret != size) @@ -301,7 +306,9 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) } flags >>= 1; } - if (av_new_packet(pkt, frame_size + 768)) + if (frame_size < 0) + return AVERROR_INVALIDDATA; + if (av_new_packet(pkt, frame_size + 769)) return AVERROR(ENOMEM); if(smk->frm_size[smk->cur_frame] & 1) palchange |= 2; diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c index 1a1a9ab808..769f4e3e0d 100644 --- a/libavformat/swfenc.c +++ b/libavformat/swfenc.c @@ -498,8 +498,10 @@ static int swf_write_trailer(AVFormatContext *s) put_le32(pb, file_size); url_fseek(pb, swf->duration_pos, SEEK_SET); put_le16(pb, swf->video_frame_number); + if (swf->vframes_pos) { url_fseek(pb, swf->vframes_pos, SEEK_SET); put_le16(pb, swf->video_frame_number); + } url_fseek(pb, file_size, SEEK_SET); } return 0; diff --git a/libavformat/utils.c b/libavformat/utils.c index 9e6279a06b..b73d98473f 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -282,8 +282,38 @@ AVInputFormat *av_find_input_format(const char *short_name) return NULL; } -/* memory handling */ +#if LIBAVFORMAT_VERSION_MAJOR < 53 && CONFIG_SHARED && HAVE_SYMVER +FF_SYMVER(void, av_destruct_packet_nofree, (AVPacket *pkt), "LIBAVFORMAT_52") +{ + av_destruct_packet_nofree(pkt); +} +FF_SYMVER(void, av_destruct_packet, (AVPacket *pkt), "LIBAVFORMAT_52") +{ + av_destruct_packet(pkt); +} + +FF_SYMVER(int, av_new_packet, (AVPacket *pkt, int size), "LIBAVFORMAT_52") +{ + return av_new_packet(pkt, size); +} + +FF_SYMVER(int, av_dup_packet, (AVPacket *pkt), "LIBAVFORMAT_52") +{ + return av_dup_packet(pkt); +} + +FF_SYMVER(void, av_free_packet, (AVPacket *pkt), "LIBAVFORMAT_52") +{ + av_free_packet(pkt); +} + +FF_SYMVER(void, av_init_packet, (AVPacket *pkt), "LIBAVFORMAT_52") +{ + av_log(NULL, AV_LOG_WARNING, "Diverting av_*_packet function calls to libavcodec. Recompile to improve performance\n"); + av_init_packet(pkt); +} +#endif int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size) { @@ -2175,12 +2205,20 @@ int av_find_stream_info(AVFormatContext *ic) /* check if one codec still needs to be handled */ for(i=0;inb_streams;i++) { + int fps_analyze_framecount = 20; + st = ic->streams[i]; if (!has_codec_parameters(st->codec)) break; + /* if the timebase is coarse (like the usual millisecond precision + of mkv), we need to analyze more frames to reliably arrive at + the correct fps */ + if (av_q2d(st->time_base) > 0.0005) + fps_analyze_framecount *= 2; /* variable fps and no guess at the real fps */ if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num) - && duration_count[i]<20 && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) + && duration_count[i] < fps_analyze_framecount + && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) break; if(st->parser && st->parser->parser->split && !st->codec->extradata) break; @@ -2201,7 +2239,7 @@ int av_find_stream_info(AVFormatContext *ic) /* we did not get all the codec info, but we read too much data */ if (read_size >= ic->probesize) { ret = count; - av_log(ic, AV_LOG_WARNING, "MAX_READ_SIZE:%d reached\n", ic->probesize); + av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize); break; } diff --git a/libavutil/avutil.h b/libavutil/avutil.h index b2c49283b0..e9e07b92fd 100644 --- a/libavutil/avutil.h +++ b/libavutil/avutil.h @@ -41,7 +41,7 @@ #define LIBAVUTIL_VERSION_MAJOR 50 #define LIBAVUTIL_VERSION_MINOR 15 -#define LIBAVUTIL_VERSION_MICRO 0 +#define LIBAVUTIL_VERSION_MICRO 1 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \ diff --git a/libavutil/bswap.h b/libavutil/bswap.h index 2d36f2e2ea..6dd2a3511a 100644 --- a/libavutil/bswap.h +++ b/libavutil/bswap.h @@ -43,8 +43,8 @@ #endif #define AV_BSWAP16C(x) (((x) << 8 & 0xff00) | ((x) >> 8 & 0x00ff)) -#define AV_BSWAP32C(x) (AV_BSWAP16C(x) << 16 | AV_BSWAP16C(x >> 16)) -#define AV_BSWAP64C(x) (AV_BSWAP32C(x) << 32 | AV_BSWAP32C(x >> 32)) +#define AV_BSWAP32C(x) (AV_BSWAP16C(x) << 16 | AV_BSWAP16C((x) >> 16)) +#define AV_BSWAP64C(x) (AV_BSWAP32C(x) << 32 | AV_BSWAP32C((x) >> 32)) #define AV_BSWAPC(s, x) AV_BSWAP##s##C(x) diff --git a/libavutil/error.c b/libavutil/error.c index 3dd38a351c..b6d6019061 100644 --- a/libavutil/error.c +++ b/libavutil/error.c @@ -36,8 +36,10 @@ int av_strerror(int errnum, char *errbuf, size_t errbuf_size) } else { #if HAVE_STRERROR_R ret = strerror_r(AVUNERROR(errnum), errbuf, errbuf_size); +#else + ret = -1; #endif - if (!HAVE_STRERROR_R || ret < 0) + if (ret < 0) snprintf(errbuf, errbuf_size, "Error number %d occurred", errnum); } diff --git a/libavutil/error.h b/libavutil/error.h index a1b9ccc050..13a9a35930 100644 --- a/libavutil/error.h +++ b/libavutil/error.h @@ -64,7 +64,8 @@ * error message indicating the errnum provided to errbuf. * * @param errbuf_size the size in bytes of errbuf - * @return 0 on success, a negative value otherwise + * @return 0 on success, a negative value if a description for errnum + * cannot be found */ int av_strerror(int errnum, char *errbuf, size_t errbuf_size); diff --git a/libavutil/internal.h b/libavutil/internal.h index 70cf2ffef9..fe8a6fa6b5 100644 --- a/libavutil/internal.h +++ b/libavutil/internal.h @@ -204,4 +204,15 @@ # define NULL_IF_CONFIG_SMALL(x) x #endif +#if HAVE_SYMVER_ASM_LABEL +# define FF_SYMVER(type, name, args, ver) \ + type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver); \ + type ff_##name args +#elif HAVE_SYMVER_GNU_ASM +# define FF_SYMVER(type, name, args, ver) \ + __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver); \ + type ff_##name args; \ + type ff_##name args +#endif + #endif /* AVUTIL_INTERNAL_H */ diff --git a/libavutil/log.c b/libavutil/log.c index 9a8b66ee4d..b08072cd7b 100644 --- a/libavutil/log.c +++ b/libavutil/log.c @@ -59,7 +59,8 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) { static int print_prefix=1; static int count; - static char line[1024], prev[1024]; + static char prev[1024]; + char line[1024]; static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9}; AVClass* avc= ptr ? *(AVClass**)ptr : NULL; if(level>av_log_level) diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h index ef06049b12..d976f343ba 100644 --- a/libavutil/pixfmt.h +++ b/libavutil/pixfmt.h @@ -68,8 +68,8 @@ enum PixelFormat { PIX_FMT_YUV410P, ///< planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples) PIX_FMT_YUV411P, ///< planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) PIX_FMT_GRAY8, ///< Y , 8bpp - PIX_FMT_MONOWHITE, ///< Y , 1bpp, 0 is white, 1 is black - PIX_FMT_MONOBLACK, ///< Y , 1bpp, 0 is black, 1 is white + PIX_FMT_MONOWHITE, ///< Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb + PIX_FMT_MONOBLACK, ///< Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb PIX_FMT_PAL8, ///< 8 bit with PIX_FMT_RGB32 palette PIX_FMT_YUVJ420P, ///< planar YUV 4:2:0, 12bpp, full scale (JPEG) PIX_FMT_YUVJ422P, ///< planar YUV 4:2:2, 16bpp, full scale (JPEG) @@ -79,12 +79,12 @@ enum PixelFormat { PIX_FMT_UYVY422, ///< packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1 PIX_FMT_UYYVYY411, ///< packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3 PIX_FMT_BGR8, ///< packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb) - PIX_FMT_BGR4, ///< packed RGB 1:2:1, 4bpp, (msb)1B 2G 1R(lsb) + PIX_FMT_BGR4, ///< packed RGB 1:2:1 bitstream, 4bpp, (msb)1B 2G 1R(lsb), a byte contains two pixels, the first pixel in the byte is the one composed by the 4 msb bits PIX_FMT_BGR4_BYTE, ///< packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb) PIX_FMT_RGB8, ///< packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb) - PIX_FMT_RGB4, ///< packed RGB 1:2:1, 4bpp, (msb)1R 2G 1B(lsb) + PIX_FMT_RGB4, ///< packed RGB 1:2:1 bitstream, 4bpp, (msb)1R 2G 1B(lsb), a byte contains two pixels, the first pixel in the byte is the one composed by the 4 msb bits PIX_FMT_RGB4_BYTE, ///< packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb) - PIX_FMT_NV12, ///< planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 for UV + PIX_FMT_NV12, ///< planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (first byte U and the following byte V) PIX_FMT_NV21, ///< as above, but U and V bytes are swapped PIX_FMT_ARGB, ///< packed ARGB 8:8:8:8, 32bpp, ARGBARGB... @@ -102,8 +102,8 @@ enum PixelFormat { PIX_FMT_VDPAU_MPEG2,///< MPEG-2 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers PIX_FMT_VDPAU_WMV3,///< WMV3 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers PIX_FMT_VDPAU_VC1, ///< VC-1 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers - PIX_FMT_RGB48BE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, big-endian - PIX_FMT_RGB48LE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, little-endian + PIX_FMT_RGB48BE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big-endian + PIX_FMT_RGB48LE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as little-endian PIX_FMT_RGB565BE, ///< packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian PIX_FMT_RGB565LE, ///< packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian diff --git a/libavutil/rational.c b/libavutil/rational.c index 3217b7daeb..3e8b885d49 100644 --- a/libavutil/rational.c +++ b/libavutil/rational.c @@ -98,6 +98,8 @@ AVRational av_d2q(double d, int max){ #define LOG2 0.69314718055994530941723212145817656807550013436025 int exponent= FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0); int64_t den= 1LL << (61 - exponent); + if (isnan(d)) + return (AVRational){0,0}; av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max); return a; diff --git a/libpostproc/postprocess.c b/libpostproc/postprocess.c index 92c822b772..d2dc6bcae1 100644 --- a/libpostproc/postprocess.c +++ b/libpostproc/postprocess.c @@ -86,6 +86,7 @@ try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks //#define DEBUG_BRIGHTNESS #include "postprocess.h" #include "postprocess_internal.h" +#include "libavutil/avstring.h" unsigned postproc_version(void) { @@ -766,7 +767,8 @@ pp_mode *pp_get_mode_by_name_and_quality(const char *name, int quality) ppMode->maxClippedThreshold= 0.01; ppMode->error=0; - strncpy(temp, name, GET_MODE_BUFFER_SIZE); + memset(temp, 0, GET_MODE_BUFFER_SIZE); + av_strlcpy(temp, name, GET_MODE_BUFFER_SIZE - 1); av_log(NULL, AV_LOG_DEBUG, "pp: %s\n", name); @@ -822,7 +824,7 @@ pp_mode *pp_get_mode_by_name_and_quality(const char *name, int quality) plen= strlen(p); spaceLeft= p - temp + plen; - if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE){ + if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE - 1){ ppMode->error++; break; } diff --git a/libswscale/bfin/internal_bfin.S b/libswscale/bfin/internal_bfin.S index 98f69ef53f..5af46540a8 100644 --- a/libswscale/bfin/internal_bfin.S +++ b/libswscale/bfin/internal_bfin.S @@ -479,9 +479,6 @@ DEFUN(uyvytoyv12, mL3, (const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8 r1 = [fp + ARG_srcStride]; r2 = r0 + r1; - r1 += -8; // i0,i1 is pre read need to correct - m0 = r1; - i0 = r0; // uyvy_T even i1 = r2; // uyvy_B odd @@ -494,6 +491,12 @@ DEFUN(uyvytoyv12, mL3, (const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8 p4 = p4 >> 1; p5 = p5 >> 2; + r2 = r0 << 1; + r1 = r1 << 1; + r1 = r1 - r2; // srcStride + (srcStride - 2*width) + r1 += -8; // i0,i1 is pre read need to correct + m0 = r1; + r2 = [fp + ARG_chromStride]; r0 = r0 >> 1; r2 = r2 - r0; @@ -549,8 +552,6 @@ DEFUN(yuyvtoyv12, mL3, (const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8 r1 = [fp + ARG_srcStride]; r2 = r0 + r1; - r1 += -8; // i0,i1 is pre read need to correct - m0 = r1; i0 = r0; // uyvy_T even i1 = r2; // uyvy_B odd @@ -564,6 +565,12 @@ DEFUN(yuyvtoyv12, mL3, (const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8 p4 = p4 >> 1; p5 = p5 >> 2; + r2 = r0 << 1; + r1 = r1 << 1; + r1 = r1 - r2; // srcStride + (srcStride - 2*width) + r1 += -8; // i0,i1 is pre read need to correct + m0 = r1; + r2 = [fp + ARG_chromStride]; r0 = r0 >> 1; r2 = r2 - r0; diff --git a/libswscale/rgb2rgb.c b/libswscale/rgb2rgb.c index 7d1cefca3a..b506b12edc 100644 --- a/libswscale/rgb2rgb.c +++ b/libswscale/rgb2rgb.c @@ -207,31 +207,15 @@ void sws_rgb2rgb_init(int flags) rgb2rgb_init_C(); } -/** - * Convert the palette to the same packet 32-bit format as the palette - */ +#if LIBSWSCALE_VERSION_MAJOR < 1 void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) { - long i; - - for (i=0; i dst format: ABC - */ void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) { - long i; - - for (i=0; i +#include "libswscale/swscale.h" +#include "libavutil/avutil.h" + /* A full collection of RGB to RGB(BGR) converters */ extern void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size); extern void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size); @@ -66,12 +69,15 @@ void shuffle_bytes_2103(const uint8_t *src, uint8_t *dst, long src_size); void shuffle_bytes_3012(const uint8_t *src, uint8_t *dst, long src_size); void shuffle_bytes_3210(const uint8_t *src, uint8_t *dst, long src_size); -void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); -void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); -void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); -void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); -void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); -void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); +#if LIBSWSCALE_VERSION_MAJOR < 1 +/* deprecated, use the public versions in swscale.h */ +attribute_deprecated void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); +attribute_deprecated void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); + +/* totally deprecated, please fix code that uses this */ +attribute_deprecated void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); +attribute_deprecated void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette); +#endif /** * Height should be a multiple of 2 and width should be a multiple of 16. diff --git a/libswscale/swscale.c b/libswscale/swscale.c index 2b02dd3dfd..81f475c6e2 100644 --- a/libswscale/swscale.c +++ b/libswscale/swscale.c @@ -1424,12 +1424,12 @@ static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], if (usePal(srcFormat)) { switch (dstFormat) { - case PIX_FMT_RGB32 : conv = palette8topacked32; break; - case PIX_FMT_BGR32 : conv = palette8topacked32; break; - case PIX_FMT_BGR32_1: conv = palette8topacked32; break; - case PIX_FMT_RGB32_1: conv = palette8topacked32; break; - case PIX_FMT_RGB24 : conv = palette8topacked24; break; - case PIX_FMT_BGR24 : conv = palette8topacked24; break; + case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break; + case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break; + case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break; + case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break; + case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break; + case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break; } } @@ -1962,3 +1962,26 @@ int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[] return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride); } #endif + +/* Convert the palette to the same packed 32-bit format as the palette */ +void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) +{ + long i; + + for (i=0; i dst format: ABC */ +void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) +{ + long i; + + for (i=0; i