From 97c50cb77949856573d7f5f5a3c28cb73e61e011 Mon Sep 17 00:00:00 2001 From: Mike Melanson Date: Tue, 19 Apr 2005 05:16:45 +0000 Subject: sync to FFmpeg build 4752 CVS patchset: 7463 CVS date: 2005/04/19 05:16:45 --- CREDITS | 2 +- src/libffmpeg/libavcodec/4xm.c | 44 +- src/libffmpeg/libavcodec/8bps.c | 16 +- src/libffmpeg/libavcodec/Makefile.am | 23 +- src/libffmpeg/libavcodec/aasc.c | 174 + src/libffmpeg/libavcodec/adpcm.c | 140 + src/libffmpeg/libavcodec/adx.c | 3 + src/libffmpeg/libavcodec/alac.c | 833 ++++ src/libffmpeg/libavcodec/alpha/dsputil_alpha_asm.S | 3 - .../libavcodec/alpha/motion_est_mvi_asm.S | 3 - src/libffmpeg/libavcodec/asv1.c | 35 +- src/libffmpeg/libavcodec/avcodec.h | 445 ++- src/libffmpeg/libavcodec/bitstream.c | 287 ++ src/libffmpeg/libavcodec/bitstream.h | 854 ++++ src/libffmpeg/libavcodec/bswap.h | 27 +- src/libffmpeg/libavcodec/cabac.c | 53 +- src/libffmpeg/libavcodec/cabac.h | 115 +- src/libffmpeg/libavcodec/cinepak.c | 53 +- src/libffmpeg/libavcodec/cljr.c | 7 +- src/libffmpeg/libavcodec/common.h | 895 +---- src/libffmpeg/libavcodec/dpcm.c | 93 + src/libffmpeg/libavcodec/dsputil.c | 478 ++- src/libffmpeg/libavcodec/dsputil.h | 76 +- src/libffmpeg/libavcodec/dv.c | 14 +- src/libffmpeg/libavcodec/dvdata.h | 6 +- src/libffmpeg/libavcodec/error_resilience.c | 34 +- src/libffmpeg/libavcodec/eval.c | 167 +- src/libffmpeg/libavcodec/ffv1.c | 229 +- src/libffmpeg/libavcodec/flac.c | 42 +- src/libffmpeg/libavcodec/flicvideo.c | 2 +- src/libffmpeg/libavcodec/g726.c | 64 +- src/libffmpeg/libavcodec/golomb.h | 89 +- src/libffmpeg/libavcodec/h261.c | 1041 +++++ src/libffmpeg/libavcodec/h261data.h | 136 + src/libffmpeg/libavcodec/h263.c | 1739 +++++---- src/libffmpeg/libavcodec/h263dec.c | 79 +- src/libffmpeg/libavcodec/h264.c | 2926 ++++++++++---- src/libffmpeg/libavcodec/h264data.h | 6 +- src/libffmpeg/libavcodec/h264idct.c | 70 + src/libffmpeg/libavcodec/huffyuv.c | 352 +- src/libffmpeg/libavcodec/i386/cputest.c | 117 +- src/libffmpeg/libavcodec/i386/dsputil_mmx.c | 1244 +++++- src/libffmpeg/libavcodec/i386/dsputil_mmx_avg.h | 601 ++- src/libffmpeg/libavcodec/i386/dsputil_mmx_rnd.h | 179 +- src/libffmpeg/libavcodec/i386/fdct_mmx.c | 6 +- src/libffmpeg/libavcodec/i386/mmx.h | 6 + src/libffmpeg/libavcodec/i386/motion_est_mmx.c | 121 +- src/libffmpeg/libavcodec/i386/mpegvideo_mmx.c | 145 +- .../libavcodec/i386/mpegvideo_mmx_template.c | 47 +- src/libffmpeg/libavcodec/idcinvideo.c | 2 +- src/libffmpeg/libavcodec/imgconvert.c | 323 +- src/libffmpeg/libavcodec/imgresample.c | 79 +- src/libffmpeg/libavcodec/indeo3.c | 98 +- src/libffmpeg/libavcodec/integer.c | 26 + src/libffmpeg/libavcodec/interplayvideo.c | 2 +- src/libffmpeg/libavcodec/jrevdct.c | 470 +-- src/libffmpeg/libavcodec/lcl.c | 82 +- src/libffmpeg/libavcodec/libpostproc/mangle.h | 9 + src/libffmpeg/libavcodec/libpostproc/postprocess.c | 266 +- .../libpostproc/postprocess_altivec_template.c | 721 +++- .../libavcodec/libpostproc/postprocess_internal.h | 10 + .../libavcodec/libpostproc/postprocess_template.c | 1477 ++++--- src/libffmpeg/libavcodec/loco.c | 285 ++ src/libffmpeg/libavcodec/mace.c | 34 +- src/libffmpeg/libavcodec/mdec.c | 11 +- src/libffmpeg/libavcodec/mem.c | 33 +- src/libffmpeg/libavcodec/mjpeg.c | 201 +- src/libffmpeg/libavcodec/motion_est.c | 126 +- src/libffmpeg/libavcodec/motion_est_template.c | 88 +- src/libffmpeg/libavcodec/mpeg12.c | 1326 ++++--- src/libffmpeg/libavcodec/mpegaudio.h | 21 + src/libffmpeg/libavcodec/mpegaudiodec.c | 975 +++-- src/libffmpeg/libavcodec/mpegvideo.c | 1136 ++++-- src/libffmpeg/libavcodec/mpegvideo.h | 50 +- src/libffmpeg/libavcodec/msmpeg4.c | 718 ++-- src/libffmpeg/libavcodec/msmpeg4data.h | 10 +- src/libffmpeg/libavcodec/msrle.c | 4 - src/libffmpeg/libavcodec/msvideo1.c | 4 - src/libffmpeg/libavcodec/parser.c | 69 +- src/libffmpeg/libavcodec/pcm.c | 20 + src/libffmpeg/libavcodec/qdrw.c | 158 + src/libffmpeg/libavcodec/qpeg.c | 302 ++ src/libffmpeg/libavcodec/qtrle.c | 96 +- src/libffmpeg/libavcodec/ra144.c | 2 +- src/libffmpeg/libavcodec/ra288.c | 2 +- src/libffmpeg/libavcodec/rangecoder.c | 178 + src/libffmpeg/libavcodec/rangecoder.h | 125 + src/libffmpeg/libavcodec/ratecontrol.c | 32 +- src/libffmpeg/libavcodec/rational.c | 12 + src/libffmpeg/libavcodec/rational.h | 18 +- src/libffmpeg/libavcodec/raw.c | 24 +- src/libffmpeg/libavcodec/rpza.c | 4 - src/libffmpeg/libavcodec/rv10.c | 90 +- src/libffmpeg/libavcodec/shorten.c | 521 +++ src/libffmpeg/libavcodec/smc.c | 6 +- src/libffmpeg/libavcodec/snow.c | 3996 +++++++++++++++++++ src/libffmpeg/libavcodec/sparc/dsputil_vis.c | 34 +- src/libffmpeg/libavcodec/svq1.c | 71 +- src/libffmpeg/libavcodec/svq3.c | 67 +- src/libffmpeg/libavcodec/truemotion1.c | 394 +- src/libffmpeg/libavcodec/tscc.c | 330 ++ src/libffmpeg/libavcodec/ulti.c | 428 ++ src/libffmpeg/libavcodec/ulti_cb.h | 4098 ++++++++++++++++++++ src/libffmpeg/libavcodec/utils.c | 283 +- src/libffmpeg/libavcodec/vcr1.c | 7 +- src/libffmpeg/libavcodec/vmdav.c | 11 +- src/libffmpeg/libavcodec/vp3.c | 171 +- src/libffmpeg/libavcodec/vp3data.h | 11 + src/libffmpeg/libavcodec/vqavideo.c | 4 + src/libffmpeg/libavcodec/wmadec.c | 15 +- src/libffmpeg/libavcodec/wmv2.c | 6 +- src/libffmpeg/libavcodec/wnv1.c | 144 + src/libffmpeg/libavcodec/ws-snd1.c | 145 + src/libffmpeg/libavcodec/xan.c | 3 + src/libffmpeg/libavcodec/xl.c | 138 + 115 files changed, 27867 insertions(+), 6856 deletions(-) create mode 100644 src/libffmpeg/libavcodec/aasc.c create mode 100644 src/libffmpeg/libavcodec/alac.c create mode 100755 src/libffmpeg/libavcodec/bitstream.c create mode 100644 src/libffmpeg/libavcodec/bitstream.h create mode 100644 src/libffmpeg/libavcodec/h261.c create mode 100755 src/libffmpeg/libavcodec/h261data.h create mode 100755 src/libffmpeg/libavcodec/h264idct.c create mode 100644 src/libffmpeg/libavcodec/loco.c create mode 100644 src/libffmpeg/libavcodec/qdrw.c create mode 100644 src/libffmpeg/libavcodec/qpeg.c create mode 100644 src/libffmpeg/libavcodec/rangecoder.c create mode 100644 src/libffmpeg/libavcodec/rangecoder.h create mode 100644 src/libffmpeg/libavcodec/shorten.c create mode 100644 src/libffmpeg/libavcodec/snow.c create mode 100644 src/libffmpeg/libavcodec/tscc.c create mode 100755 src/libffmpeg/libavcodec/ulti.c create mode 100755 src/libffmpeg/libavcodec/ulti_cb.h create mode 100644 src/libffmpeg/libavcodec/wnv1.c create mode 100644 src/libffmpeg/libavcodec/ws-snd1.c create mode 100644 src/libffmpeg/libavcodec/xl.c diff --git a/CREDITS b/CREDITS index 35ae72796..8028e1f34 100644 --- a/CREDITS +++ b/CREDITS @@ -12,7 +12,7 @@ updates (the word 'maintainer' is intentionally avoided here). project version mediator ----------------------------------------------------------------------- -ffmpeg build 4715 Mike Melanson +ffmpeg build 4752 Mike Melanson goom 2k4-dev21 gsm610 1.0.10 Mike Melanson liba52 0.7.4 diff --git a/src/libffmpeg/libavcodec/4xm.c b/src/libffmpeg/libavcodec/4xm.c index fd84f8968..6932d52ab 100644 --- a/src/libffmpeg/libavcodec/4xm.c +++ b/src/libffmpeg/libavcodec/4xm.c @@ -220,17 +220,12 @@ static void idct(DCTELEM block[64]){ } static void init_vlcs(FourXContext *f){ - static int done = 0; int i; - if (!done) { - done = 1; - - for(i=0; i<4; i++){ - init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, - &block_type_tab[i][0][1], 2, 1, - &block_type_tab[i][0][0], 2, 1); - } + for(i=0; i<4; i++){ + init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, + &block_type_tab[i][0][1], 2, 1, + &block_type_tab[i][0][0], 2, 1, 1); } } @@ -328,13 +323,19 @@ static int decode_p_frame(FourXContext *f, uint8_t *buf, int length){ uint16_t *src= (uint16_t*)f->last_picture.data[0]; uint16_t *dst= (uint16_t*)f->current_picture.data[0]; const int stride= f->current_picture.linesize[0]>>1; - const int bitstream_size= get32(buf+8); - const int bytestream_size= get32(buf+16); - const int wordstream_size= get32(buf+12); + const unsigned int bitstream_size= get32(buf+8); + const unsigned int bytestream_size= get32(buf+16); + const unsigned int wordstream_size= get32(buf+12); - if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length) + if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length + || bitstream_size > (1<<26) + || bytestream_size > (1<<26) + || wordstream_size > (1<<26) + ){ av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size, bitstream_size+ bytestream_size+ wordstream_size - length); + return -1; + } f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE); f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4); @@ -544,7 +545,7 @@ static uint8_t *read_huffman_tables(FourXContext *f, uint8_t * const buf){ init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, len_tab , 1, 1, - bits_tab, 4, 4); + bits_tab, 4, 4, 0); return ptr; } @@ -555,13 +556,17 @@ static int decode_i_frame(FourXContext *f, uint8_t *buf, int length){ const int height= f->avctx->height; uint16_t *dst= (uint16_t*)f->current_picture.data[0]; const int stride= f->current_picture.linesize[0]>>1; - const int bitstream_size= get32(buf); + const unsigned int bitstream_size= get32(buf); const int token_count __attribute__((unused)) = get32(buf + bitstream_size + 8); - int prestream_size= 4*get32(buf + bitstream_size + 4); + unsigned int prestream_size= 4*get32(buf + bitstream_size + 4); uint8_t *prestream= buf + bitstream_size + 12; - if(prestream_size + bitstream_size + 12 != length) + if(prestream_size + bitstream_size + 12 != length + || bitstream_size > (1<<26) + || prestream_size > (1<<26)){ av_log(f->avctx, AV_LOG_ERROR, "size missmatch %d %d %d\n", prestream_size, bitstream_size, length); + return -1; + } prestream= read_huffman_tables(f, prestream); @@ -600,11 +605,6 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p, temp; int i, frame_4cc, frame_size; - /* special case for last picture */ - if (buf_size == 0) { - return 0; - } - frame_4cc= get32(buf); if(buf_size != get32(buf+4)+8){ av_log(f->avctx, AV_LOG_ERROR, "size missmatch %d %d\n", buf_size, get32(buf+4)); diff --git a/src/libffmpeg/libavcodec/8bps.c b/src/libffmpeg/libavcodec/8bps.c index 9509f42ad..3898ac5dd 100644 --- a/src/libffmpeg/libavcodec/8bps.c +++ b/src/libffmpeg/libavcodec/8bps.c @@ -61,7 +61,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 { EightBpsContext * const c = (EightBpsContext *)avctx->priv_data; unsigned char *encoded = (unsigned char *)buf; - unsigned char *pixptr; + unsigned char *pixptr, *pixptr_end; unsigned int height = avctx->height; // Real image height unsigned int dlen, p, row; unsigned char *lp, *dp; @@ -70,11 +70,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 unsigned int planes = c->planes; unsigned char *planemap = c->planemap; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; - if(c->pic.data[0]) avctx->release_buffer(avctx, &c->pic); @@ -101,18 +96,23 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 /* Decode a plane */ for(row = 0; row < height; row++) { pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p]; + pixptr_end = pixptr + c->pic.linesize[0]; dlen = be2me_16(*(unsigned short *)(lp+row*2)); /* Decode a row of this plane */ while(dlen > 0) { if ((count = *dp++) <= 127) { count++; dlen -= count + 1; + if (pixptr + count * px_inc > pixptr_end) + break; while(count--) { *pixptr = *dp++; pixptr += px_inc; } } else { count = 257 - count; + if (pixptr + count * px_inc > pixptr_end) + break; while(count--) { *pixptr = *dp; pixptr += px_inc; @@ -155,6 +155,10 @@ static int decode_init(AVCodecContext *avctx) c->pic.data[0] = NULL; + if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { + return 1; + } + switch (avctx->bits_per_sample) { case 8: avctx->pix_fmt = PIX_FMT_PAL8; diff --git a/src/libffmpeg/libavcodec/Makefile.am b/src/libffmpeg/libavcodec/Makefile.am index 5d9c0d2d0..8b4ae4fef 100644 --- a/src/libffmpeg/libavcodec/Makefile.am +++ b/src/libffmpeg/libavcodec/Makefile.am @@ -17,11 +17,13 @@ noinst_LTLIBRARIES = libavcodec.la libavcodec_la_SOURCES = \ 4xm.c \ 8bps.c \ + aasc.c \ adpcm.c \ + alac.c \ asv1.c \ + bitstream.c \ cabac.c \ cinepak.c \ - common.c \ cyuv.c \ dpcm.c \ dsputil.c \ @@ -33,9 +35,11 @@ libavcodec_la_SOURCES = \ flicvideo.c \ fft.c \ golomb.c \ + h261.c \ h263.c \ h263dec.c \ h264.c \ + h264idct.c \ huffyuv.c \ idcinvideo.c \ imgconvert.c \ @@ -46,6 +50,7 @@ libavcodec_la_SOURCES = \ jfdctint.c \ jrevdct.c \ lcl.c \ + loco.c \ mdct.c \ mace.c \ mem.c \ @@ -57,21 +62,27 @@ libavcodec_la_SOURCES = \ msmpeg4.c \ msrle.c \ msvideo1.c \ - opts.c \ parser.c \ pcm.c \ + qdrw.c \ + qpeg.c \ qtrle.c \ ra144.c \ ra288.c \ + rangecoder.c \ ratecontrol.c \ rational.c \ roqvideo.c \ rpza.c \ rv10.c \ + shorten.c \ simple_idct.c \ smc.c \ + snow.c \ svq1.c \ + tscc.c \ truemotion1.c \ + ulti.c \ utils.c \ vcr1.c \ vmdav.c \ @@ -79,7 +90,9 @@ libavcodec_la_SOURCES = \ vp3dsp.c \ vqavideo.c \ wmadec.c \ - xan.c + wnv1.c \ + xan.c \ + xl.c libavcodec_la_LDFLAGS = \ $(top_builddir)/src/libffmpeg/libavcodec/armv4l/libavcodec_armv4l.la \ @@ -92,6 +105,7 @@ libavcodec_la_LDFLAGS = \ noinst_HEADERS = \ avcodec.h \ + bitstream.h \ bswap.h \ cabac.h \ common.h \ @@ -103,6 +117,7 @@ noinst_HEADERS = \ imgconvert_template.h \ indeo3data.h \ integer.h \ + h261data.h \ h263data.h \ h264data.h \ mpeg4data.h \ @@ -114,11 +129,13 @@ noinst_HEADERS = \ msmpeg4data.h \ ra144.h \ ra288.h \ + rangecoder.h \ rational.h \ simple_idct.h \ sp5x.h \ svq1_cb.h \ svq1_vlc.h \ truemotion1data.h \ + ulti_cb.h \ vp3data.h \ wmadata.h diff --git a/src/libffmpeg/libavcodec/aasc.c b/src/libffmpeg/libavcodec/aasc.c new file mode 100644 index 000000000..d2419e98c --- /dev/null +++ b/src/libffmpeg/libavcodec/aasc.c @@ -0,0 +1,174 @@ +/* + * Autodesc RLE Decoder + * Copyright (C) 2005 the ffmpeg project + * + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file aasc.c + * Autodesc RLE Video Decoder by Konstantin Shishkov + */ + +#include +#include +#include + +#include "common.h" +#include "avcodec.h" +#include "dsputil.h" + +typedef struct AascContext { + AVCodecContext *avctx; + AVFrame frame; +} AascContext; + +#define FETCH_NEXT_STREAM_BYTE() \ + if (stream_ptr >= buf_size) \ + { \ + av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \ + break; \ + } \ + stream_byte = buf[stream_ptr++]; + +static int aasc_decode_init(AVCodecContext *avctx) +{ + AascContext *s = (AascContext *)avctx->priv_data; + + s->avctx = avctx; + + avctx->pix_fmt = PIX_FMT_BGR24; + avctx->has_b_frames = 0; + s->frame.data[0] = NULL; + + return 0; +} + +static int aasc_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + AascContext *s = (AascContext *)avctx->priv_data; + int stream_ptr = 4; + unsigned char rle_code; + unsigned char stream_byte; + int pixel_ptr = 0; + int row_dec, row_ptr; + int frame_size; + int i; + + s->frame.reference = 1; + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if (avctx->reget_buffer(avctx, &s->frame)) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return -1; + } + + row_dec = s->frame.linesize[0]; + row_ptr = (s->avctx->height - 1) * row_dec; + frame_size = row_dec * s->avctx->height; + + while (row_ptr >= 0) { + FETCH_NEXT_STREAM_BYTE(); + rle_code = stream_byte; + if (rle_code == 0) { + /* fetch the next byte to see how to handle escape code */ + FETCH_NEXT_STREAM_BYTE(); + if (stream_byte == 0) { + /* line is done, goto the next one */ + row_ptr -= row_dec; + pixel_ptr = 0; + } else if (stream_byte == 1) { + /* decode is done */ + break; + } else if (stream_byte == 2) { + /* reposition frame decode coordinates */ + FETCH_NEXT_STREAM_BYTE(); + pixel_ptr += stream_byte; + FETCH_NEXT_STREAM_BYTE(); + row_ptr -= stream_byte * row_dec; + } else { + /* copy pixels from encoded stream */ + if ((pixel_ptr + stream_byte > avctx->width * 3) || + (row_ptr < 0)) { + av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n"); + break; + } + + rle_code = stream_byte; + if (stream_ptr + rle_code > buf_size) { + av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n"); + break; + } + + for (i = 0; i < rle_code; i++) { + FETCH_NEXT_STREAM_BYTE(); + s->frame.data[0][row_ptr + pixel_ptr] = stream_byte; + pixel_ptr++; + } + if (rle_code & 1) + stream_ptr++; + } + } else { + /* decode a run of data */ + if ((pixel_ptr + rle_code > avctx->width * 3) || + (row_ptr < 0)) { + av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n"); + break; + } + + FETCH_NEXT_STREAM_BYTE(); + + while(rle_code--) { + s->frame.data[0][row_ptr + pixel_ptr] = stream_byte; + pixel_ptr++; + } + } + } + + /* one last sanity check on the way out */ + if (stream_ptr < buf_size) + av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n", + stream_ptr, buf_size); + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = s->frame; + + /* report that the buffer was completely consumed */ + return buf_size; +} + +static int aasc_decode_end(AVCodecContext *avctx) +{ + AascContext *s = (AascContext *)avctx->priv_data; + + /* release the last frame */ + if (s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); + + return 0; +} + +AVCodec aasc_decoder = { + "aasc", + CODEC_TYPE_VIDEO, + CODEC_ID_AASC, + sizeof(AascContext), + aasc_decode_init, + NULL, + aasc_decode_end, + aasc_decode_frame, + CODEC_CAP_DR1, +}; diff --git a/src/libffmpeg/libavcodec/adpcm.c b/src/libffmpeg/libavcodec/adpcm.c index 0755e24fe..043c4d4b2 100644 --- a/src/libffmpeg/libavcodec/adpcm.c +++ b/src/libffmpeg/libavcodec/adpcm.c @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "avcodec.h" +#include "bitstream.h" /** * @file adpcm.c @@ -103,6 +104,19 @@ static int ea_adpcm_table[] = { 3, 4, 7, 8, 10, 11, 0, -1, -3, -4 }; +static int ct_adpcm_table[8] = { + 0x00E6, 0x00E6, 0x00E6, 0x00E6, + 0x0133, 0x0199, 0x0200, 0x0266 +}; + +// padded to zero where table size is less then 16 +static int swf_index_tables[4][16] = { + /*2*/ { -1, 2 }, + /*3*/ { -1, -1, 2, 4 }, + /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 }, + /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 } +}; + /* end of tables */ typedef struct ADPCMChannelStatus { @@ -124,6 +138,10 @@ typedef struct ADPCMContext { int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */ ADPCMChannelStatus status[2]; short sample_buffer[32]; /* hold left samples while waiting for right samples */ + + /* SWF only */ + int nb_bits; + int nb_samples; } ADPCMContext; /* XXX: implement encoding */ @@ -361,6 +379,9 @@ static int adpcm_decode_init(AVCodecContext * avctx) c->status[0].step = c->status[1].step = 0; switch(avctx->codec->id) { + case CODEC_ID_ADPCM_CT: + c->status[0].step = c->status[1].step = 511; + break; default: break; } @@ -411,6 +432,37 @@ static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble) return (short)predictor; } +static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble) +{ + int predictor; + int sign, delta, diff; + int new_step; + + sign = nibble & 8; + delta = nibble & 7; + /* perform direct multiplication instead of series of jumps proposed by + * the reference ADPCM implementation since modern CPUs can do the mults + * quickly enough */ + diff = ((2 * delta + 1) * c->step) >> 3; + predictor = c->predictor; + /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */ + if(sign) + predictor = ((predictor * 254) >> 8) - diff; + else + predictor = ((predictor * 254) >> 8) + diff; + /* calculate new step and clamp it to range 511..32767 */ + new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8; + c->step = new_step; + if(c->step < 511) + c->step = 511; + if(c->step > 32767) + c->step = 32767; + + CLAMP_TO_SHORT(predictor); + c->predictor = predictor; + return (short)predictor; +} + static void xa_decode(short *out, const unsigned char *in, ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc) { @@ -840,6 +892,92 @@ static int adpcm_decode_frame(AVCodecContext *avctx, src++; } break; + case CODEC_ID_ADPCM_CT: + while (src < buf + buf_size) { + if (st) { + *samples++ = adpcm_ct_expand_nibble(&c->status[0], + (src[0] >> 4) & 0x0F); + *samples++ = adpcm_ct_expand_nibble(&c->status[1], + src[0] & 0x0F); + } else { + *samples++ = adpcm_ct_expand_nibble(&c->status[0], + (src[0] >> 4) & 0x0F); + *samples++ = adpcm_ct_expand_nibble(&c->status[0], + src[0] & 0x0F); + } + src++; + } + break; + case CODEC_ID_ADPCM_SWF: + { + GetBitContext gb; + int *table; + int k0, signmask; + int size = buf_size*8; + + init_get_bits(&gb, buf, size); + + // first frame, read bits & inital values + if (!c->nb_bits) + { + c->nb_bits = get_bits(&gb, 2)+2; +// av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits); + } + + table = swf_index_tables[c->nb_bits-2]; + k0 = 1 << (c->nb_bits-2); + signmask = 1 << (c->nb_bits-1); + + while (get_bits_count(&gb) <= size) + { + int i; + + c->nb_samples++; + // wrap around at every 4096 samples... + if ((c->nb_samples & 0xfff) == 1) + { + for (i = 0; i <= st; i++) + { + *samples++ = c->status[i].predictor = get_sbits(&gb, 16); + c->status[i].step_index = get_bits(&gb, 6); + } + } + + // similar to IMA adpcm + for (i = 0; i <= st; i++) + { + int delta = get_bits(&gb, c->nb_bits); + int step = step_table[c->status[i].step_index]; + long vpdiff = 0; // vpdiff = (delta+0.5)*step/4 + int k = k0; + + do { + if (delta & k) + vpdiff += step; + step >>= 1; + k >>= 1; + } while(k); + vpdiff += step; + + if (delta & signmask) + c->status[i].predictor -= vpdiff; + else + c->status[i].predictor += vpdiff; + + c->status[i].step_index += table[delta & (~signmask)]; + + c->status[i].step_index = clip(c->status[i].step_index, 0, 88); + c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767); + + *samples++ = c->status[i].predictor; + } + } + +// src += get_bits_count(&gb)*8; + src += size; + + break; + } default: return -1; } @@ -895,5 +1033,7 @@ ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm); ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea); +ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct); +ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf); #undef ADPCM_CODEC diff --git a/src/libffmpeg/libavcodec/adx.c b/src/libffmpeg/libavcodec/adx.c index e41a75726..a52575c13 100644 --- a/src/libffmpeg/libavcodec/adx.c +++ b/src/libffmpeg/libavcodec/adx.c @@ -314,6 +314,7 @@ static int adx_decode_init(AVCodecContext * avctx) return 0; } +#if 0 static void dump(unsigned char *buf,size_t len) { int i; @@ -324,6 +325,8 @@ static void dump(unsigned char *buf,size_t len) } av_log(NULL, AV_LOG_ERROR, "\n"); } +#endif + static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf0, int buf_size) diff --git a/src/libffmpeg/libavcodec/alac.c b/src/libffmpeg/libavcodec/alac.c new file mode 100644 index 000000000..5ae2e00f4 --- /dev/null +++ b/src/libffmpeg/libavcodec/alac.c @@ -0,0 +1,833 @@ +/* + * ALAC (Apple Lossless Audio Codec) decoder + * Copyright (c) 2005 David Hammerton + * All rights reserved. + * + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file alac.c + * ALAC (Apple Lossless Audio Codec) decoder + * @author 2005 David Hammerton + * + * For more information on the ALAC format, visit: + * http://crazney.net/programs/itunes/alac.html + * + * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be + * passed through the extradata[_size] fields. This atom is tacked onto + * the end of an 'alac' stsd atom and has the following format: + * bytes 0-3 atom size (0x24), big-endian + * bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd) + * bytes 8-35 data bytes needed by decoder + */ + + +#include "avcodec.h" +#include "bitstream.h" + +#define ALAC_EXTRADATA_SIZE 36 + +typedef struct { + + AVCodecContext *avctx; + GetBitContext gb; + /* init to 0; first frame decode should initialize from extradata and + * set this to 1 */ + int context_initialized; + + int samplesize; + int numchannels; + int bytespersample; + + /* buffers */ + int32_t *predicterror_buffer_a; + int32_t *predicterror_buffer_b; + + int32_t *outputsamples_buffer_a; + int32_t *outputsamples_buffer_b; + + /* stuff from setinfo */ + uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */ + uint8_t setinfo_7a; /* 0x00 */ + uint8_t setinfo_sample_size; /* 0x10 */ + uint8_t setinfo_rice_historymult; /* 0x28 */ + uint8_t setinfo_rice_initialhistory; /* 0x0a */ + uint8_t setinfo_rice_kmodifier; /* 0x0e */ + uint8_t setinfo_7f; /* 0x02 */ + uint16_t setinfo_80; /* 0x00ff */ + uint32_t setinfo_82; /* 0x000020e7 */ + uint32_t setinfo_86; /* 0x00069fe4 */ + uint32_t setinfo_8a_rate; /* 0x0000ac44 */ + /* end setinfo stuff */ + +} ALACContext; + +static void allocate_buffers(ALACContext *alac) +{ + alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4); + alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4); + + alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4); + alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4); +} + +void alac_set_info(ALACContext *alac) +{ + unsigned char *ptr = alac->avctx->extradata; + + ptr += 4; /* size */ + ptr += 4; /* alac */ + ptr += 4; /* 0 ? */ + + alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */ + ptr += 4; + alac->setinfo_7a = *ptr++; + alac->setinfo_sample_size = *ptr++; + alac->setinfo_rice_historymult = *ptr++; + alac->setinfo_rice_initialhistory = *ptr++; + alac->setinfo_rice_kmodifier = *ptr++; + alac->setinfo_7f = *ptr++; + alac->setinfo_80 = BE_16(ptr); + ptr += 2; + alac->setinfo_82 = BE_32(ptr); + ptr += 4; + alac->setinfo_86 = BE_32(ptr); + ptr += 4; + alac->setinfo_8a_rate = BE_32(ptr); + ptr += 4; + + allocate_buffers(alac); +} + +/* hideously inefficient. could use a bitmask search, + * alternatively bsr on x86, + */ +static int count_leading_zeros(int32_t input) +{ + int i = 0; + while (!(0x80000000 & input) && i < 32) { + i++; + input = input << 1; + } + return i; +} + +void bastardized_rice_decompress(ALACContext *alac, + int32_t *output_buffer, + int output_size, + int readsamplesize, /* arg_10 */ + int rice_initialhistory, /* arg424->b */ + int rice_kmodifier, /* arg424->d */ + int rice_historymult, /* arg424->c */ + int rice_kmodifier_mask /* arg424->e */ + ) +{ + int output_count; + unsigned int history = rice_initialhistory; + int sign_modifier = 0; + + for (output_count = 0; output_count < output_size; output_count++) { + int32_t x = 0; + int32_t x_modified; + int32_t final_val; + + /* read x - number of 1s before 0 represent the rice */ + while (x <= 8 && get_bits1(&alac->gb)) { + x++; + } + + + if (x > 8) { /* RICE THRESHOLD */ + /* use alternative encoding */ + int32_t value; + + value = get_bits(&alac->gb, readsamplesize); + + /* mask value to readsamplesize size */ + if (readsamplesize != 32) + value &= (0xffffffff >> (32 - readsamplesize)); + + x = value; + } else { + /* standard rice encoding */ + int extrabits; + int k; /* size of extra bits */ + + /* read k, that is bits as is */ + k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3); + + if (k < 0) + k += rice_kmodifier; + else + k = rice_kmodifier; + + if (k != 1) { + extrabits = show_bits(&alac->gb, k); + + /* multiply x by 2^k - 1, as part of their strange algorithm */ + x = (x << k) - x; + + if (extrabits > 1) { + x += extrabits - 1; + get_bits(&alac->gb, k); + } else { + get_bits(&alac->gb, k - 1); + } + } + } + + x_modified = sign_modifier + x; + final_val = (x_modified + 1) / 2; + if (x_modified & 1) final_val *= -1; + + output_buffer[output_count] = final_val; + + sign_modifier = 0; + + /* now update the history */ + history += (x_modified * rice_historymult) + - ((history * rice_historymult) >> 9); + + if (x_modified > 0xffff) + history = 0xffff; + + /* special case: there may be compressed blocks of 0 */ + if ((history < 128) && (output_count+1 < output_size)) { + int block_size; + + sign_modifier = 1; + + x = 0; + while (x <= 8 && get_bits1(&alac->gb)) { + x++; + } + + if (x > 8) { + block_size = get_bits(&alac->gb, 16); + block_size &= 0xffff; + } else { + int k; + int extrabits; + + k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24; + + extrabits = show_bits(&alac->gb, k); + + block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x + + extrabits - 1; + + if (extrabits < 2) { + x = 1 - extrabits; + block_size += x; + get_bits(&alac->gb, k - 1); + } else { + get_bits(&alac->gb, k); + } + } + + if (block_size > 0) { + memset(&output_buffer[output_count+1], 0, block_size * 4); + output_count += block_size; + + } + + if (block_size > 0xffff) + sign_modifier = 0; + + history = 0; + } + } +} + +#define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits)) + +#define SIGN_ONLY(v) \ + ((v < 0) ? (-1) : \ + ((v > 0) ? (1) : \ + (0))) + +static void predictor_decompress_fir_adapt(int32_t *error_buffer, + int32_t *buffer_out, + int output_size, + int readsamplesize, + int16_t *predictor_coef_table, + int predictor_coef_num, + int predictor_quantitization) +{ + int i; + + /* first sample always copies */ + *buffer_out = *error_buffer; + + if (!predictor_coef_num) { + if (output_size <= 1) return; + memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4); + return; + } + + if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */ + /* second-best case scenario for fir decompression, + * error describes a small difference from the previous sample only + */ + if (output_size <= 1) return; + for (i = 0; i < output_size - 1; i++) { + int32_t prev_value; + int32_t error_value; + + prev_value = buffer_out[i]; + error_value = error_buffer[i+1]; + buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize); + } + return; + } + + /* read warm-up samples */ + if (predictor_coef_num > 0) { + int i; + for (i = 0; i < predictor_coef_num; i++) { + int32_t val; + + val = buffer_out[i] + error_buffer[i+1]; + + val = SIGN_EXTENDED32(val, readsamplesize); + + buffer_out[i+1] = val; + } + } + +#if 0 + /* 4 and 8 are very common cases (the only ones i've seen). these + * should be unrolled and optimised + */ + if (predictor_coef_num == 4) { + /* FIXME: optimised general case */ + return; + } + + if (predictor_coef_table == 8) { + /* FIXME: optimised general case */ + return; + } +#endif + + + /* general case */ + if (predictor_coef_num > 0) { + for (i = predictor_coef_num + 1; + i < output_size; + i++) { + int j; + int sum = 0; + int outval; + int error_val = error_buffer[i]; + + for (j = 0; j < predictor_coef_num; j++) { + sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * + predictor_coef_table[j]; + } + + outval = (1 << (predictor_quantitization-1)) + sum; + outval = outval >> predictor_quantitization; + outval = outval + buffer_out[0] + error_val; + outval = SIGN_EXTENDED32(outval, readsamplesize); + + buffer_out[predictor_coef_num+1] = outval; + + if (error_val > 0) { + int predictor_num = predictor_coef_num - 1; + + while (predictor_num >= 0 && error_val > 0) { + int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + int sign = SIGN_ONLY(val); + + predictor_coef_table[predictor_num] -= sign; + + val *= sign; /* absolute value */ + + error_val -= ((val >> predictor_quantitization) * + (predictor_coef_num - predictor_num)); + + predictor_num--; + } + } else if (error_val < 0) { + int predictor_num = predictor_coef_num - 1; + + while (predictor_num >= 0 && error_val < 0) { + int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; + int sign = - SIGN_ONLY(val); + + predictor_coef_table[predictor_num] -= sign; + + val *= sign; /* neg value */ + + error_val -= ((val >> predictor_quantitization) * + (predictor_coef_num - predictor_num)); + + predictor_num--; + } + } + + buffer_out++; + } + } +} + +void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b, + int16_t *buffer_out, + int numchannels, int numsamples, + uint8_t interlacing_shift, + uint8_t interlacing_leftweight) +{ + int i; + if (numsamples <= 0) return; + + /* weighted interlacing */ + if (interlacing_leftweight) { + for (i = 0; i < numsamples; i++) { + int32_t difference, midright; + int16_t left; + int16_t right; + + midright = buffer_a[i]; + difference = buffer_b[i]; + + + right = midright - ((difference * interlacing_leftweight) >> interlacing_shift); + left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift)) + + difference; + + buffer_out[i*numchannels] = left; + buffer_out[i*numchannels + 1] = right; + } + + return; + } + + /* otherwise basic interlacing took place */ + for (i = 0; i < numsamples; i++) { + int16_t left, right; + + left = buffer_a[i]; + right = buffer_b[i]; + + buffer_out[i*numchannels] = left; + buffer_out[i*numchannels + 1] = right; + } +} + +static int alac_decode_frame(AVCodecContext *avctx, + void *outbuffer, int *outputsize, + uint8_t *inbuffer, int input_buffer_size) +{ + ALACContext *alac = avctx->priv_data; + + int channels; + int32_t outputsamples; + + /* short-circuit null buffers */ + if (!inbuffer || !input_buffer_size) + return input_buffer_size; + + /* initialize from the extradata */ + if (!alac->context_initialized) { + if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) { + av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n", + ALAC_EXTRADATA_SIZE); + return input_buffer_size; + } + alac_set_info(alac); + alac->context_initialized = 1; + } + + outputsamples = alac->setinfo_max_samples_per_frame; + + init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8); + + channels = get_bits(&alac->gb, 3); + + *outputsize = outputsamples * alac->bytespersample; + + switch(channels) { + case 0: { /* 1 channel */ + int hassize; + int isnotcompressed; + int readsamplesize; + + int wasted_bytes; + int ricemodifier; + + + /* 2^result = something to do with output waiting. + * perhaps matters if we read > 1 frame in a pass? + */ + get_bits(&alac->gb, 4); + + get_bits(&alac->gb, 12); /* unknown, skip 12 bits */ + + hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */ + + wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */ + + isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */ + + if (hassize) { + /* now read the number of samples, + * as a 32bit integer */ + outputsamples = get_bits(&alac->gb, 32); + *outputsize = outputsamples * alac->bytespersample; + } + + readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8); + + if (!isnotcompressed) { + /* so it is compressed */ + int16_t predictor_coef_table[32]; + int predictor_coef_num; + int prediction_type; + int prediction_quantitization; + int i; + + /* skip 16 bits, not sure what they are. seem to be used in + * two channel case */ + get_bits(&alac->gb, 8); + get_bits(&alac->gb, 8); + + prediction_type = get_bits(&alac->gb, 4); + prediction_quantitization = get_bits(&alac->gb, 4); + + ricemodifier = get_bits(&alac->gb, 3); + predictor_coef_num = get_bits(&alac->gb, 5); + + /* read the predictor table */ + for (i = 0; i < predictor_coef_num; i++) { + predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16); + } + + if (wasted_bytes) { + /* these bytes seem to have something to do with + * > 2 channel files. + */ + av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n"); + } + + bastardized_rice_decompress(alac, + alac->predicterror_buffer_a, + outputsamples, + readsamplesize, + alac->setinfo_rice_initialhistory, + alac->setinfo_rice_kmodifier, + ricemodifier * alac->setinfo_rice_historymult / 4, + (1 << alac->setinfo_rice_kmodifier) - 1); + + if (prediction_type == 0) { + /* adaptive fir */ + predictor_decompress_fir_adapt(alac->predicterror_buffer_a, + alac->outputsamples_buffer_a, + outputsamples, + readsamplesize, + predictor_coef_table, + predictor_coef_num, + prediction_quantitization); + } else { + av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type); + /* i think the only other prediction type (or perhaps this is just a + * boolean?) runs adaptive fir twice.. like: + * predictor_decompress_fir_adapt(predictor_error, tempout, ...) + * predictor_decompress_fir_adapt(predictor_error, outputsamples ...) + * little strange.. + */ + } + + } else { + /* not compressed, easy case */ + if (readsamplesize <= 16) { + int i; + for (i = 0; i < outputsamples; i++) { + int32_t audiobits = get_bits(&alac->gb, readsamplesize); + + audiobits = SIGN_EXTENDED32(audiobits, readsamplesize); + + alac->outputsamples_buffer_a[i] = audiobits; + } + } else { + int i; + for (i = 0; i < outputsamples; i++) { + int32_t audiobits; + + audiobits = get_bits(&alac->gb, 16); + /* special case of sign extension.. + * as we'll be ORing the low 16bits into this */ + audiobits = audiobits << 16; + audiobits = audiobits >> (32 - readsamplesize); + + audiobits |= get_bits(&alac->gb, readsamplesize - 16); + + alac->outputsamples_buffer_a[i] = audiobits; + } + } + /* wasted_bytes = 0; // unused */ + } + + switch(alac->setinfo_sample_size) { + case 16: { + int i; + for (i = 0; i < outputsamples; i++) { + int16_t sample = alac->outputsamples_buffer_a[i]; + be2me_16(sample); + ((int16_t*)outbuffer)[i * alac->numchannels] = sample; + } + break; + } + case 20: + case 24: + case 32: + av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); + break; + default: + break; + } + break; + } + case 1: { /* 2 channels */ + int hassize; + int isnotcompressed; + int readsamplesize; + + int wasted_bytes; + + uint8_t interlacing_shift; + uint8_t interlacing_leftweight; + + /* 2^result = something to do with output waiting. + * perhaps matters if we read > 1 frame in a pass? + */ + get_bits(&alac->gb, 4); + + get_bits(&alac->gb, 12); /* unknown, skip 12 bits */ + + hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */ + + wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */ + + isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */ + + if (hassize) { + /* now read the number of samples, + * as a 32bit integer */ + outputsamples = get_bits(&alac->gb, 32); + *outputsize = outputsamples * alac->bytespersample; + } + + readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1; + + if (!isnotcompressed) { + /* compressed */ + int16_t predictor_coef_table_a[32]; + int predictor_coef_num_a; + int prediction_type_a; + int prediction_quantitization_a; + int ricemodifier_a; + + int16_t predictor_coef_table_b[32]; + int predictor_coef_num_b; + int prediction_type_b; + int prediction_quantitization_b; + int ricemodifier_b; + + int i; + + interlacing_shift = get_bits(&alac->gb, 8); + interlacing_leftweight = get_bits(&alac->gb, 8); + + /******** channel 1 ***********/ + prediction_type_a = get_bits(&alac->gb, 4); + prediction_quantitization_a = get_bits(&alac->gb, 4); + + ricemodifier_a = get_bits(&alac->gb, 3); + predictor_coef_num_a = get_bits(&alac->gb, 5); + + /* read the predictor table */ + for (i = 0; i < predictor_coef_num_a; i++) { + predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16); + } + + /******** channel 2 *********/ + prediction_type_b = get_bits(&alac->gb, 4); + prediction_quantitization_b = get_bits(&alac->gb, 4); + + ricemodifier_b = get_bits(&alac->gb, 3); + predictor_coef_num_b = get_bits(&alac->gb, 5); + + /* read the predictor table */ + for (i = 0; i < predictor_coef_num_b; i++) { + predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16); + } + + /*********************/ + if (wasted_bytes) { + /* see mono case */ + av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n"); + } + + /* channel 1 */ + bastardized_rice_decompress(alac, + alac->predicterror_buffer_a, + outputsamples, + readsamplesize, + alac->setinfo_rice_initialhistory, + alac->setinfo_rice_kmodifier, + ricemodifier_a * alac->setinfo_rice_historymult / 4, + (1 << alac->setinfo_rice_kmodifier) - 1); + + if (prediction_type_a == 0) { + /* adaptive fir */ + predictor_decompress_fir_adapt(alac->predicterror_buffer_a, + alac->outputsamples_buffer_a, + outputsamples, + readsamplesize, + predictor_coef_table_a, + predictor_coef_num_a, + prediction_quantitization_a); + } else { + /* see mono case */ + av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a); + } + + /* channel 2 */ + bastardized_rice_decompress(alac, + alac->predicterror_buffer_b, + outputsamples, + readsamplesize, + alac->setinfo_rice_initialhistory, + alac->setinfo_rice_kmodifier, + ricemodifier_b * alac->setinfo_rice_historymult / 4, + (1 << alac->setinfo_rice_kmodifier) - 1); + + if (prediction_type_b == 0) { + /* adaptive fir */ + predictor_decompress_fir_adapt(alac->predicterror_buffer_b, + alac->outputsamples_buffer_b, + outputsamples, + readsamplesize, + predictor_coef_table_b, + predictor_coef_num_b, + prediction_quantitization_b); + } else { + av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b); + } + } else { + /* not compressed, easy case */ + if (alac->setinfo_sample_size <= 16) { + int i; + for (i = 0; i < outputsamples; i++) { + int32_t audiobits_a, audiobits_b; + + audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size); + audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size); + + audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size); + audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size); + + alac->outputsamples_buffer_a[i] = audiobits_a; + alac->outputsamples_buffer_b[i] = audiobits_b; + } + } else { + int i; + for (i = 0; i < outputsamples; i++) { + int32_t audiobits_a, audiobits_b; + + audiobits_a = get_bits(&alac->gb, 16); + audiobits_a = audiobits_a << 16; + audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size); + audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16); + + audiobits_b = get_bits(&alac->gb, 16); + audiobits_b = audiobits_b << 16; + audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size); + audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16); + + alac->outputsamples_buffer_a[i] = audiobits_a; + alac->outputsamples_buffer_b[i] = audiobits_b; + } + } + /* wasted_bytes = 0; */ + interlacing_shift = 0; + interlacing_leftweight = 0; + } + + switch(alac->setinfo_sample_size) { + case 16: { + deinterlace_16(alac->outputsamples_buffer_a, + alac->outputsamples_buffer_b, + (int16_t*)outbuffer, + alac->numchannels, + outputsamples, + interlacing_shift, + interlacing_leftweight); + break; + } + case 20: + case 24: + case 32: + av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); + break; + default: + break; + } + + break; + } + } + + return input_buffer_size; +} + +static int alac_decode_init(AVCodecContext * avctx) +{ + ALACContext *alac = avctx->priv_data; + alac->avctx = avctx; + alac->context_initialized = 0; + + alac->samplesize = alac->avctx->bits_per_sample; + alac->numchannels = alac->avctx->channels; + alac->bytespersample = (alac->samplesize / 8) * alac->numchannels; + + return 0; +} + +static int alac_decode_close(AVCodecContext *avctx) +{ + ALACContext *alac = avctx->priv_data; + + av_free(alac->predicterror_buffer_a); + av_free(alac->predicterror_buffer_b); + + av_free(alac->outputsamples_buffer_a); + av_free(alac->outputsamples_buffer_b); + + return 0; +} + +AVCodec alac_decoder = { + "alac", + CODEC_TYPE_AUDIO, + CODEC_ID_ALAC, + sizeof(ALACContext), + alac_decode_init, + NULL, + alac_decode_close, + alac_decode_frame, +}; diff --git a/src/libffmpeg/libavcodec/alpha/dsputil_alpha_asm.S b/src/libffmpeg/libavcodec/alpha/dsputil_alpha_asm.S index 7ec6757d7..6519a9590 100644 --- a/src/libffmpeg/libavcodec/alpha/dsputil_alpha_asm.S +++ b/src/libffmpeg/libavcodec/alpha/dsputil_alpha_asm.S @@ -23,9 +23,6 @@ */ #include "regdef.h" -#ifdef HAVE_AV_CONFIG_H -#include "config.h" -#endif /* Some nicer register names. */ #define ta t10 diff --git a/src/libffmpeg/libavcodec/alpha/motion_est_mvi_asm.S b/src/libffmpeg/libavcodec/alpha/motion_est_mvi_asm.S index 0042e7e82..9e6b75f53 100644 --- a/src/libffmpeg/libavcodec/alpha/motion_est_mvi_asm.S +++ b/src/libffmpeg/libavcodec/alpha/motion_est_mvi_asm.S @@ -18,9 +18,6 @@ */ #include "regdef.h" -#ifdef HAVE_AV_CONFIG_H -#include "config.h" -#endif /* Some nicer register names. */ #define ta t10 diff --git a/src/libffmpeg/libavcodec/asv1.c b/src/libffmpeg/libavcodec/asv1.c index 2ab729c17..4ab2518ab 100644 --- a/src/libffmpeg/libavcodec/asv1.c +++ b/src/libffmpeg/libavcodec/asv1.c @@ -63,7 +63,7 @@ static const uint8_t scantab[64]={ }; -static const uint8_t reverse[256]={ +const uint8_t ff_reverse[256]={ 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0, 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8, 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4, @@ -137,29 +137,29 @@ static void init_vlcs(ASV1Context *a){ init_vlc(&ccp_vlc, VLC_BITS, 17, &ccp_tab[0][1], 2, 1, - &ccp_tab[0][0], 2, 1); + &ccp_tab[0][0], 2, 1, 1); init_vlc(&dc_ccp_vlc, VLC_BITS, 8, &dc_ccp_tab[0][1], 2, 1, - &dc_ccp_tab[0][0], 2, 1); + &dc_ccp_tab[0][0], 2, 1, 1); init_vlc(&ac_ccp_vlc, VLC_BITS, 16, &ac_ccp_tab[0][1], 2, 1, - &ac_ccp_tab[0][0], 2, 1); + &ac_ccp_tab[0][0], 2, 1, 1); init_vlc(&level_vlc, VLC_BITS, 7, &level_tab[0][1], 2, 1, - &level_tab[0][0], 2, 1); + &level_tab[0][0], 2, 1, 1); init_vlc(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63, &asv2_level_tab[0][1], 2, 1, - &asv2_level_tab[0][0], 2, 1); + &asv2_level_tab[0][0], 2, 1, 1); } } //FIXME write a reversed bitstream reader to avoid the double reverse static inline int asv2_get_bits(GetBitContext *gb, int n){ - return reverse[ get_bits(gb, n) << (8-n) ]; + return ff_reverse[ get_bits(gb, n) << (8-n) ]; } static inline void asv2_put_bits(PutBitContext *pb, int n, int v){ - put_bits(pb, n, reverse[ v << (8-n) ]); + put_bits(pb, n, ff_reverse[ v << (8-n) ]); } static inline int asv1_get_level(GetBitContext *gb){ @@ -339,8 +339,13 @@ static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){ return 0; } -static inline void encode_mb(ASV1Context *a, DCTELEM block[6][64]){ +static inline int encode_mb(ASV1Context *a, DCTELEM block[6][64]){ int i; + + if(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb)>>3) < 30*16*16*3/2/8){ + av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + return -1; + } if(a->avctx->codec_id == CODEC_ID_ASV1){ for(i=0; i<6; i++) @@ -349,6 +354,7 @@ static inline void encode_mb(ASV1Context *a, DCTELEM block[6][64]){ for(i=0; i<6; i++) asv2_encode_block(a, block[i]); } + return 0; } static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){ @@ -403,11 +409,6 @@ static int decode_frame(AVCodecContext *avctx, AVFrame * const p= (AVFrame*)&a->picture; int mb_x, mb_y; - /* special case for last picture */ - if (buf_size == 0) { - return 0; - } - if(p->data[0]) avctx->release_buffer(avctx, p); @@ -426,7 +427,7 @@ static int decode_frame(AVCodecContext *avctx, else{ int i; for(i=0; ibitstream_buffer[i]= reverse[ buf[i] ]; + a->bitstream_buffer[i]= ff_reverse[ buf[i] ]; } init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8); @@ -527,7 +528,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, else{ int i; for(i=0; i<4*size; i++) - buf[i]= reverse[ buf[i] ]; + buf[i]= ff_reverse[ buf[i] ]; } return size*4; @@ -573,7 +574,7 @@ static int decode_init(AVCodecContext *avctx){ } p->qstride= a->mb_width; - p->qscale_table= av_mallocz( p->qstride * a->mb_height); + p->qscale_table= av_malloc( p->qstride * a->mb_height); p->quality= (32*scale + a->inv_qscale/2)/a->inv_qscale; memset(p->qscale_table, p->quality, p->qstride*a->mb_height); diff --git a/src/libffmpeg/libavcodec/avcodec.h b/src/libffmpeg/libavcodec/avcodec.h index 26fee27ef..064f58df2 100644 --- a/src/libffmpeg/libavcodec/avcodec.h +++ b/src/libffmpeg/libavcodec/avcodec.h @@ -15,16 +15,9 @@ extern "C" { #include "rational.h" #include /* size_t */ -/* FIXME: We cannot use ffmpeg's XvMC capabilities, since that would require - * linking the ffmpeg plugin against XvMC libraries, which is a bad thing, - * since they are output dependend. - * The correct fix would be to reimplement the XvMC functions libavcodec uses - * and do the necessary talking with our XvMC output plugin there. */ -#undef HAVE_XVMC - -#define FFMPEG_VERSION_INT 0x000408 -#define FFMPEG_VERSION "0.4.8" -#define LIBAVCODEC_BUILD 4715 +#define FFMPEG_VERSION_INT 0x000409 +#define FFMPEG_VERSION "0.4.9-pre1" +#define LIBAVCODEC_BUILD 4752 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT #define LIBAVCODEC_VERSION FFMPEG_VERSION @@ -36,18 +29,22 @@ extern "C" { #define AV_NOPTS_VALUE int64_t_C(0x8000000000000000) #define AV_TIME_BASE 1000000 +/* FIXME: We cannot use ffmpeg's XvMC capabilities, since that would require + * linking the ffmpeg plugin against XvMC libraries, which is a bad thing, + * since they are output dependend. + * The correct fix would be to reimplement the XvMC functions libavcodec uses + * and do the necessary talking with our XvMC output plugin there. */ +#undef HAVE_XVMC + enum CodecID { CODEC_ID_NONE, CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO, /* prefered ID for MPEG Video 1 or 2 decoding */ CODEC_ID_MPEG2VIDEO_XVMC, + CODEC_ID_H261, CODEC_ID_H263, CODEC_ID_RV10, CODEC_ID_RV20, - CODEC_ID_MP2, - CODEC_ID_MP3, /* prefered ID for MPEG Audio layer 1, 2 or3 decoding */ - CODEC_ID_VORBIS, - CODEC_ID_AC3, CODEC_ID_MJPEG, CODEC_ID_MJPEGB, CODEC_ID_LJPEG, @@ -65,19 +62,12 @@ enum CodecID { CODEC_ID_SVQ1, CODEC_ID_SVQ3, CODEC_ID_DVVIDEO, - CODEC_ID_DVAUDIO, - CODEC_ID_WMAV1, - CODEC_ID_WMAV2, - CODEC_ID_MACE3, - CODEC_ID_MACE6, CODEC_ID_HUFFYUV, CODEC_ID_CYUV, CODEC_ID_H264, CODEC_ID_INDEO3, CODEC_ID_VP3, CODEC_ID_THEORA, - CODEC_ID_AAC, - CODEC_ID_MPEG4AAC, CODEC_ID_ASV1, CODEC_ID_ASV2, CODEC_ID_FFV1, @@ -100,13 +90,33 @@ enum CodecID { CODEC_ID_FLIC, CODEC_ID_TRUEMOTION1, CODEC_ID_VMDVIDEO, - CODEC_ID_VMDAUDIO, CODEC_ID_MSZH, CODEC_ID_ZLIB, CODEC_ID_QTRLE, + CODEC_ID_SNOW, + CODEC_ID_TSCC, + CODEC_ID_ULTI, + CODEC_ID_QDRAW, + CODEC_ID_VIXL, + CODEC_ID_QPEG, + CODEC_ID_XVID, + CODEC_ID_PNG, + CODEC_ID_PPM, + CODEC_ID_PBM, + CODEC_ID_PGM, + CODEC_ID_PGMYUV, + CODEC_ID_PAM, + CODEC_ID_FFVHUFF, + CODEC_ID_RV30, + CODEC_ID_RV40, + CODEC_ID_VC9, + CODEC_ID_WMV3, + CODEC_ID_LOCO, + CODEC_ID_WNV1, + CODEC_ID_AASC, /* various pcm "codecs" */ - CODEC_ID_PCM_S16LE, + CODEC_ID_PCM_S16LE= 0x10000, CODEC_ID_PCM_S16BE, CODEC_ID_PCM_U16LE, CODEC_ID_PCM_U16BE, @@ -116,7 +126,7 @@ enum CodecID { CODEC_ID_PCM_ALAW, /* various adpcm codecs */ - CODEC_ID_ADPCM_IMA_QT, + CODEC_ID_ADPCM_IMA_QT= 0x11000, CODEC_ID_ADPCM_IMA_WAV, CODEC_ID_ADPCM_IMA_DK3, CODEC_ID_ADPCM_IMA_DK4, @@ -128,23 +138,48 @@ enum CodecID { CODEC_ID_ADPCM_ADX, CODEC_ID_ADPCM_EA, CODEC_ID_ADPCM_G726, + CODEC_ID_ADPCM_CT, + CODEC_ID_ADPCM_SWF, - /* AMR */ - CODEC_ID_AMR_NB, + /* AMR */ + CODEC_ID_AMR_NB= 0x12000, CODEC_ID_AMR_WB, /* RealAudio codecs*/ - CODEC_ID_RA_144, + CODEC_ID_RA_144= 0x13000, CODEC_ID_RA_288, /* various DPCM codecs */ - CODEC_ID_ROQ_DPCM, + CODEC_ID_ROQ_DPCM= 0x14000, CODEC_ID_INTERPLAY_DPCM, CODEC_ID_XAN_DPCM, + CODEC_ID_SOL_DPCM, + CODEC_ID_MP2= 0x15000, + CODEC_ID_MP3, /* prefered ID for MPEG Audio layer 1, 2 or3 decoding */ + CODEC_ID_AAC, + CODEC_ID_MPEG4AAC, + CODEC_ID_AC3, + CODEC_ID_DTS, + CODEC_ID_VORBIS, + CODEC_ID_DVAUDIO, + CODEC_ID_WMAV1, + CODEC_ID_WMAV2, + CODEC_ID_MACE3, + CODEC_ID_MACE6, + CODEC_ID_VMDAUDIO, + CODEC_ID_SONIC, + CODEC_ID_SONIC_LS, CODEC_ID_FLAC, + CODEC_ID_MP3ADU, + CODEC_ID_MP3ON4, + CODEC_ID_SHORTEN, + CODEC_ID_ALAC, + CODEC_ID_WESTWOOD_SND1, + + CODEC_ID_OGGTHEORA= 0x16000, - CODEC_ID_MPEG2TS, /* _FAKE_ codec to indicate a raw MPEG2 transport + CODEC_ID_MPEG2TS= 0x20000, /* _FAKE_ codec to indicate a raw MPEG2 transport stream (only used by libavformat) */ }; @@ -178,7 +213,7 @@ enum CodecType { */ enum PixelFormat { PIX_FMT_YUV420P, ///< Planar YUV 4:2:0 (1 Cr & Cb sample per 2x2 Y samples) - PIX_FMT_YUV422, + PIX_FMT_YUV422, ///< Packed pixel, Y0 Cb Y1 Cr PIX_FMT_RGB24, ///< Packed pixel, 3 bytes per pixel, RGBRGB... PIX_FMT_BGR24, ///< Packed pixel, 3 bytes per pixel, BGRBGR... PIX_FMT_YUV422P, ///< Planar YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples) @@ -197,12 +232,17 @@ enum PixelFormat { PIX_FMT_YUVJ444P, ///< Planar YUV 4:4:4 full scale (jpeg) PIX_FMT_XVMC_MPEG2_MC,///< XVideo Motion Acceleration via common packet passing(xvmc_render.h) PIX_FMT_XVMC_MPEG2_IDCT, + PIX_FMT_UYVY422, ///< Packed pixel, Cb Y0 Cr Y1 + PIX_FMT_UYVY411, ///< Packed pixel, Cb Y0 Y1 Cr Y2 Y3 PIX_FMT_NB, }; /* currently unused, may be used if 24/32 bits samples ever supported */ enum SampleFormat { SAMPLE_FMT_S16 = 0, ///< signed 16 bits + SAMPLE_FMT_S32, ///< signed 32 bits + SAMPLE_FMT_FLT, ///< float + SAMPLE_FMT_DBL, ///< double }; /* in bytes */ @@ -217,6 +257,12 @@ enum SampleFormat { */ #define FF_INPUT_BUFFER_PADDING_SIZE 8 +/** + * minimum encoding buffer size. + * used to avoid some checks during header writing + */ +#define FF_MIN_BUFFER_SIZE 16384 + /* motion estimation type, EPZS by default */ enum Motion_Est_ID { ME_ZERO = 1, @@ -227,6 +273,14 @@ enum Motion_Est_ID { ME_X1 }; +enum AVRounding { + AV_ROUND_ZERO = 0, ///< round toward zero + AV_ROUND_INF = 1, ///< round away from zero + AV_ROUND_DOWN = 2, ///< round toward -infinity + AV_ROUND_UP = 3, ///< round toward +infinity + AV_ROUND_NEAR_INF = 5, ///< round to nearest and halfway cases away from zero +}; + typedef struct RcOverride{ int start_frame; int end_frame; @@ -281,6 +335,10 @@ extern int motion_estimation_method; #define CODEC_FLAG_INTERLACED_ME 0x20000000 ///< interlaced motion estimation #define CODEC_FLAG_SVCD_SCAN_OFFSET 0x40000000 ///< will reserve space for SVCD scan offset user data #define CODEC_FLAG_CLOSED_GOP 0x80000000 +#define CODEC_FLAG2_FAST 0x00000001 ///< allow non spec compliant speedup tricks +#define CODEC_FLAG2_STRICT_GOP 0x00000002 ///< strictly enforce GOP size +#define CODEC_FLAG2_NO_OUTPUT 0x00000004 ///< skip bitstream encoding + /* Unsupported options : * Syntax Arithmetic coding (SAC) * Reference Picture Selection @@ -300,6 +358,11 @@ extern int motion_estimation_method; #define CODEC_CAP_TRUNCATED 0x0008 /* codec can export data for HW decoding (XvMC) */ #define CODEC_CAP_HWACCEL 0x0010 +/** + * codec has a non zero delay and needs to be feeded with NULL at the end to get the delayed data. + * if this is not set, the codec is guranteed to never be feeded with NULL data + */ +#define CODEC_CAP_DELAY 0x0020 //the following defines might change, so dont expect compatibility if u use them #define MB_TYPE_INTRA4x4 0x0001 @@ -448,7 +511,14 @@ typedef struct AVPanScan{ uint8_t *mbskip_table;\ \ /**\ - * Motion vector table\ + * Motion vector table.\ + * @code\ + * example:\ + * int mv_sample_log2= 4 - motion_subsample_log2;\ + * int mb_width= (width+15)>>4;\ + * int mv_stride= (mb_width << mv_sample_log2) + 1;\ + * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];\ + * @endcode\ * - encoding: set by user\ * - decoding: set by lavc\ */\ @@ -463,7 +533,8 @@ typedef struct AVPanScan{ uint32_t *mb_type;\ \ /**\ - * Macroblock size: (0->16x16, 1->8x8, 2-> 4x4, 3-> 2x2)\ + * log2 of the size of the block which a single vector in motion_val represents: \ + * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)\ * - encoding: unused\ * - decoding: set by lavc\ */\ @@ -645,6 +716,8 @@ typedef struct AVCodecContext { * mjpeg: huffman tables * rv10: additional flags * mpeg4: global headers (they can be in the bitstream or here) + * the allocated memory should be FF_INPUT_BUFFER_PADDING_SIZE bytes larger + * then extradata_size to avoid prolems if its read with the bitstream reader * - encoding: set/allocated/freed by lavc. * - decoding: set/allocated/freed by user. */ @@ -662,9 +735,11 @@ typedef struct AVCodecContext { int frame_rate; /** - * width / height. + * picture width / height. * - encoding: MUST be set by user. - * - decoding: set by user if known, codec should override / dynamically change if needed + * - decoding: set by lavc. + * Note, for compatibility its possible to set this instead of + * coded_width/height before decoding */ int width, height; @@ -679,10 +754,7 @@ typedef struct AVCodecContext { /** * pixel format, see PIX_FMT_xxx. - * - encoding: FIXME: used by ffmpeg to decide whether an pix_fmt - * conversion is in order. This only works for - * codecs with one supported pix_fmt, we should - * do something for a generic case as well. + * - encoding: set by user. * - decoding: set by lavc. */ enum PixelFormat pix_fmt; @@ -714,7 +786,13 @@ typedef struct AVCodecContext { /* audio only */ int sample_rate; ///< samples per sec int channels; - int sample_fmt; ///< sample format, currenly unused + + /** + * audio sample format. + * - encoding: set by user. + * - decoding: set by lavc. + */ + enum SampleFormat sample_fmt; ///< sample format, currenly unused /* the following data should not be initialized */ int frame_size; ///< in samples, initialized when calling 'init' @@ -798,8 +876,10 @@ typedef struct AVCodecContext { /* The RTP callcack: This function is called */ /* every time the encoder as a packet to send */ /* Depends on the encoder if the data starts */ - /* with a Start Code (it should) H.263 does */ - void (*rtp_callback)(struct AVCodecContext *avctx, void *data, int size, int packet_number); + /* with a Start Code (it should) H.263 does. */ + /* mb_nb contains the number of macroblocks */ + /* encoded in the RTP payload */ + void (*rtp_callback)(struct AVCodecContext *avctx, void *data, int size, int mb_nb); /* statistics, used for 2-pass encoding */ int mv_bits; @@ -839,7 +919,7 @@ typedef struct AVCodecContext { /** * workaround bugs in encoders which sometimes cannot be detected automatically. - * - encoding: unused + * - encoding: set by user * - decoding: set by user */ int workaround_bugs; @@ -857,6 +937,7 @@ typedef struct AVCodecContext { #define FF_BUG_EDGE 1024 #define FF_BUG_HPEL_CHROMA 2048 #define FF_BUG_DC_CLIP 4096 +#define FF_BUG_MS 8192 ///< workaround various bugs in microsofts broken decoders //#define FF_BUG_FAKE_SCALABILITY 16 //autodetection should work 100% /** @@ -921,8 +1002,8 @@ typedef struct AVCodecContext { void (*release_buffer)(struct AVCodecContext *c, AVFrame *pic); /** - * is 1 if the decoded stream contains b frames, 0 otherwise. - * - encoding: unused + * if 1 the stream has a 1 frame delay during decoding. + * - encoding: set by lavc * - decoding: set by lavc */ int has_b_frames; @@ -1096,6 +1177,7 @@ typedef struct AVCodecContext { #define FF_IDCT_ALTIVEC 8 #define FF_IDCT_SH4 9 #define FF_IDCT_SIMPLEARM 10 +#define FF_IDCT_H264 11 /** * slice count. @@ -1136,6 +1218,7 @@ typedef struct AVCodecContext { #define FF_MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */ #define FF_MM_SSE 0x0008 /* SSE functions */ #define FF_MM_SSE2 0x0010 /* PIV SSE2 functions */ +#define FF_MM_3DNOWEXT 0x0020 /* AMD 3DNowExt */ #endif /* HAVE_MMX */ /** @@ -1211,14 +1294,14 @@ typedef struct AVCodecContext { /** * minimum MB quantizer. - * - encoding: set by user. + * - encoding: unused * - decoding: unused */ int mb_qmin; /** * maximum MB quantizer. - * - encoding: set by user. + * - encoding: unused * - decoding: unused */ int mb_qmax; @@ -1257,6 +1340,10 @@ typedef struct AVCodecContext { #define FF_CMP_ZERO 7 #define FF_CMP_VSAD 8 #define FF_CMP_VSSE 9 +#define FF_CMP_NSSE 10 +#define FF_CMP_W53 11 +#define FF_CMP_W97 12 +#define FF_CMP_DCTMAX 13 #define FF_CMP_CHROMA 256 /** @@ -1596,11 +1683,120 @@ typedef struct AVCodecContext { int mb_threshold; /** - * + * precision of the intra dc coefficient - 8. * - encoding: set by user * - decoding: unused */ int intra_dc_precision; + + /** + * noise vs. sse weight for the nsse comparsion function. + * - encoding: set by user + * - decoding: unused + */ + int nsse_weight; + + /** + * number of macroblock rows at the top which are skiped. + * - encoding: unused + * - decoding: set by user + */ + int skip_top; + + /** + * number of macroblock rows at the bottom which are skiped. + * - encoding: unused + * - decoding: set by user + */ + int skip_bottom; + + /** + * profile + * - encoding: set by user + * - decoding: set by lavc + */ + int profile; +#define FF_PROFILE_UNKNOWN -99 + + /** + * level + * - encoding: set by user + * - decoding: set by lavc + */ + int level; +#define FF_LEVEL_UNKNOWN -99 + + /** + * low resolution decoding. 1-> 1/2 size, 2->1/4 size + * - encoding: unused + * - decoding: set by user + */ + int lowres; + + /** + * bitsream width / height. may be different from width/height if lowres + * or other things are used + * - encoding: unused + * - decoding: set by user before init if known, codec should override / dynamically change if needed + */ + int coded_width, coded_height; + + /** + * frame skip threshold + * - encoding: set by user + * - decoding: unused + */ + int frame_skip_threshold; + + /** + * frame skip factor + * - encoding: set by user + * - decoding: unused + */ + int frame_skip_factor; + + /** + * frame skip exponent + * - encoding: set by user + * - decoding: unused + */ + int frame_skip_exp; + + /** + * frame skip comparission function + * - encoding: set by user. + * - decoding: unused + */ + int frame_skip_cmp; + + /** + * border processing masking. raises the quantizer for mbs on the borders + * of the picture. + * - encoding: set by user + * - decoding: unused + */ + float border_masking; + + /** + * minimum MB lagrange multipler. + * - encoding: set by user. + * - decoding: unused + */ + int mb_lmin; + + /** + * maximum MB lagrange multipler. + * - encoding: set by user. + * - decoding: unused + */ + int mb_lmax; + + /** + * + * - encoding: set by user. + * - decoding: unused + */ + int me_penalty_compensation; } AVCodecContext; @@ -1639,22 +1835,13 @@ typedef struct AVOption { #define FF_OPT_MAX_DEPTH 10 } AVOption; -/** - * Parse option(s) and sets fields in passed structure - * @param strct structure where the parsed results will be written - * @param list list with AVOptions - * @param opts string with options for parsing - */ -int avoption_parse(void* strct, const AVOption* list, const char* opts); - - /** * AVCodec. */ typedef struct AVCodec { const char *name; enum CodecType type; - int id; + enum CodecID id; int priv_data_size; int (*init)(AVCodecContext *); int (*encode)(AVCodecContext *, uint8_t *buf, int buf_size, void *data); @@ -1662,7 +1849,7 @@ typedef struct AVCodec { int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, uint8_t *buf, int buf_size); int capabilities; - const AVOption *options; + void *dummy; // FIXME remove next time we break binary compatibility struct AVCodec *next; void (*flush)(AVCodecContext *); const AVRational *supported_framerates; ///array of supported framerates, or NULL if any, array is terminated by {0,0} @@ -1703,9 +1890,12 @@ extern AVCodec ac3_encoder; extern AVCodec mp2_encoder; extern AVCodec mp3lame_encoder; extern AVCodec oggvorbis_encoder; +extern AVCodec oggtheora_encoder; extern AVCodec faac_encoder; +extern AVCodec xvid_encoder; extern AVCodec mpeg1video_encoder; extern AVCodec mpeg2video_encoder; +extern AVCodec h261_encoder; extern AVCodec h263_encoder; extern AVCodec h263p_encoder; extern AVCodec flv_encoder; @@ -1714,6 +1904,12 @@ extern AVCodec rv20_encoder; extern AVCodec dvvideo_encoder; extern AVCodec mjpeg_encoder; extern AVCodec ljpeg_encoder; +extern AVCodec png_encoder; +extern AVCodec ppm_encoder; +extern AVCodec pgm_encoder; +extern AVCodec pgmyuv_encoder; +extern AVCodec pbm_encoder; +extern AVCodec pam_encoder; extern AVCodec mpeg4_encoder; extern AVCodec msmpeg4v1_encoder; extern AVCodec msmpeg4v2_encoder; @@ -1721,22 +1917,30 @@ extern AVCodec msmpeg4v3_encoder; extern AVCodec wmv1_encoder; extern AVCodec wmv2_encoder; extern AVCodec huffyuv_encoder; +extern AVCodec ffvhuff_encoder; extern AVCodec h264_encoder; extern AVCodec asv1_encoder; extern AVCodec asv2_encoder; extern AVCodec vcr1_encoder; extern AVCodec ffv1_encoder; +extern AVCodec snow_encoder; extern AVCodec mdec_encoder; extern AVCodec zlib_encoder; +extern AVCodec sonic_encoder; +extern AVCodec sonic_ls_encoder; extern AVCodec svq1_encoder; +extern AVCodec x264_encoder; extern AVCodec h263_decoder; +extern AVCodec h261_decoder; extern AVCodec mpeg4_decoder; extern AVCodec msmpeg4v1_decoder; extern AVCodec msmpeg4v2_decoder; extern AVCodec msmpeg4v3_decoder; extern AVCodec wmv1_decoder; extern AVCodec wmv2_decoder; +extern AVCodec vc9_decoder; +extern AVCodec wmv3_decoder; extern AVCodec mpeg1video_decoder; extern AVCodec mpeg2video_decoder; extern AVCodec mpegvideo_decoder; @@ -1745,6 +1949,8 @@ extern AVCodec h263i_decoder; extern AVCodec flv_decoder; extern AVCodec rv10_decoder; extern AVCodec rv20_decoder; +extern AVCodec rv30_decoder; +extern AVCodec rv40_decoder; extern AVCodec svq1_decoder; extern AVCodec svq3_decoder; extern AVCodec dvvideo_decoder; @@ -1753,12 +1959,17 @@ extern AVCodec wmav2_decoder; extern AVCodec mjpeg_decoder; extern AVCodec mjpegb_decoder; extern AVCodec sp5x_decoder; +extern AVCodec png_decoder; extern AVCodec mp2_decoder; extern AVCodec mp3_decoder; +extern AVCodec mp3adu_decoder; +extern AVCodec mp3on4_decoder; extern AVCodec mace3_decoder; extern AVCodec mace6_decoder; extern AVCodec huffyuv_decoder; +extern AVCodec ffvhuff_decoder; extern AVCodec oggvorbis_decoder; +extern AVCodec oggtheora_decoder; extern AVCodec cyuv_decoder; extern AVCodec h264_decoder; extern AVCodec indeo3_decoder; @@ -1775,6 +1986,7 @@ extern AVCodec asv2_decoder; extern AVCodec vcr1_decoder; extern AVCodec cljr_decoder; extern AVCodec ffv1_decoder; +extern AVCodec snow_decoder; extern AVCodec fourxm_decoder; extern AVCodec mdec_decoder; extern AVCodec roq_decoder; @@ -1799,8 +2011,21 @@ extern AVCodec ra_288_decoder; extern AVCodec roq_dpcm_decoder; extern AVCodec interplay_dpcm_decoder; extern AVCodec xan_dpcm_decoder; +extern AVCodec sol_dpcm_decoder; +extern AVCodec sonic_decoder; extern AVCodec qtrle_decoder; extern AVCodec flac_decoder; +extern AVCodec tscc_decoder; +extern AVCodec ulti_decoder; +extern AVCodec qdraw_decoder; +extern AVCodec xl_decoder; +extern AVCodec qpeg_decoder; +extern AVCodec shorten_decoder; +extern AVCodec loco_decoder; +extern AVCodec wnv1_decoder; +extern AVCodec aasc_decoder; +extern AVCodec alac_decoder; +extern AVCodec ws_snd1_decoder; /* pcm codecs */ #define PCM_CODEC(id, name) \ @@ -1830,6 +2055,8 @@ PCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); PCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); PCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea); PCM_CODEC(CODEC_ID_ADPCM_G726, adpcm_g726); +PCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct); +PCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf); #undef PCM_CODEC @@ -1839,10 +2066,12 @@ extern AVCodec rawvideo_decoder; /* the following codecs use external GPL libs */ extern AVCodec ac3_decoder; +extern AVCodec dts_decoder; /* resample.c */ struct ReSampleContext; +struct AVResampleContext; typedef struct ReSampleContext ReSampleContext; @@ -1851,6 +2080,11 @@ ReSampleContext *audio_resample_init(int output_channels, int input_channels, int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples); void audio_resample_close(ReSampleContext *s); +struct AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_length, int log2_phase_count, int linear, double cutoff); +int av_resample(struct AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx); +void av_resample_compensate(struct AVResampleContext *c, int sample_delta, int compensation_distance); +void av_resample_close(struct AVResampleContext *c); + /* YUV420 format is assumed ! */ struct ImgReSampleContext; @@ -1894,7 +2128,9 @@ int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height, int avpicture_get_size(int pix_fmt, int width, int height); void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift); const char *avcodec_get_pix_fmt_name(int pix_fmt); +void avcodec_set_dimensions(AVCodecContext *s, int width, int height); enum PixelFormat avcodec_get_pix_fmt(const char* name); +unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat p); #define FF_LOSS_RESOLUTION 0x0001 /* loss due to resolution change */ #define FF_LOSS_DEPTH 0x0002 /* loss due to color depth change */ @@ -1948,6 +2184,7 @@ int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic); void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic); int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic); void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height); +int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h); enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt); int avcodec_thread_init(AVCodecContext *s, int thread_count); @@ -1982,6 +2219,8 @@ void avcodec_register_all(void); void avcodec_flush_buffers(AVCodecContext *avctx); +void avcodec_default_free_buffers(AVCodecContext *s); + /* misc usefull functions */ /** @@ -1998,87 +2237,16 @@ char av_get_pict_type_char(int pict_type); int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max); /** - * rescale a 64bit integer. + * rescale a 64bit integer with rounding to nearest. * a simple a*b/c isnt possible as it can overflow */ int64_t av_rescale(int64_t a, int64_t b, int64_t c); - /** - * Interface for 0.5.0 version - * - * do not even think about it's usage for this moment - */ - -typedef struct { - /// compressed size used from given memory buffer - int size; - /// I/P/B frame type - int frame_type; -} avc_enc_result_t; - -/** - * Commands - * order can't be changed - once it was defined - */ -typedef enum { - // general commands - AVC_OPEN_BY_NAME = 0xACA000, - AVC_OPEN_BY_CODEC_ID, - AVC_OPEN_BY_FOURCC, - AVC_CLOSE, - - AVC_FLUSH, - // pin - struct { uint8_t* src, uint_t src_size } - // pout - struct { AVPicture* img, consumed_bytes, - AVC_DECODE, - // pin - struct { AVPicture* img, uint8_t* dest, uint_t dest_size } - // pout - uint_t used_from_dest_size - AVC_ENCODE, - - // query/get video commands - AVC_GET_VERSION = 0xACB000, - AVC_GET_WIDTH, - AVC_GET_HEIGHT, - AVC_GET_DELAY, - AVC_GET_QUANT_TABLE, - // ... - - // query/get audio commands - AVC_GET_FRAME_SIZE = 0xABC000, - - // maybe define some simple structure which - // might be passed to the user - but they can't - // contain any codec specific parts and these - // calls are usualy necessary only few times - - // set video commands - AVC_SET_WIDTH = 0xACD000, - AVC_SET_HEIGHT, - - // set video encoding commands - AVC_SET_FRAME_RATE = 0xACD800, - AVC_SET_QUALITY, - AVC_SET_HURRY_UP, - - // set audio commands - AVC_SET_SAMPLE_RATE = 0xACE000, - AVC_SET_CHANNELS, - -} avc_cmd_t; - -/** - * \param handle allocated private structure by libavcodec - * for initialization pass NULL - will be returned pout - * user is supposed to know nothing about its structure - * \param cmd type of operation to be performed - * \param pint input parameter - * \param pout output parameter - * - * \returns command status - eventually for query command it might return - * integer resulting value + * rescale a 64bit integer with specified rounding. + * a simple a*b/c isnt possible as it can overflow */ -int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout); +int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding); /* frame parsing */ typedef struct AVCodecParserContext { @@ -2107,7 +2275,7 @@ typedef struct AVCodecParserContext { } AVCodecParserContext; typedef struct AVCodecParser { - int codec_ids[3]; /* several codec IDs are permitted */ + int codec_ids[5]; /* several codec IDs are permitted */ int priv_data_size; int (*parser_init)(AVCodecParserContext *s); int (*parser_parse)(AVCodecParserContext *s, @@ -2131,8 +2299,11 @@ void av_parser_close(AVCodecParserContext *s); extern AVCodecParser mpegvideo_parser; extern AVCodecParser mpeg4video_parser; +extern AVCodecParser h261_parser; extern AVCodecParser h263_parser; extern AVCodecParser h264_parser; +extern AVCodecParser mjpeg_parser; +extern AVCodecParser pnm_parser; extern AVCodecParser mpegaudio_parser; extern AVCodecParser ac3_parser; @@ -2148,6 +2319,7 @@ void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size); /* call av_free_static to release all staticaly allocated tables */ void av_free_static(void); void *av_mallocz_static(unsigned int size); +void *av_realloc_static(void *ptr, unsigned int size); /* add by bero : in adx.c */ int is_adx(const unsigned char *buf,size_t bufsize); @@ -2164,7 +2336,12 @@ void img_copy(AVPicture *dst, const AVPicture *src, #define AV_LOG_INFO 1 #define AV_LOG_DEBUG 2 +#ifdef __GNUC__ extern void av_log(void*, int level, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 3, 4))); +#else +extern void av_log(void*, int level, const char *fmt, ...); +#endif + extern void av_vlog(void*, int level, const char *fmt, va_list); extern int av_log_get_level(void); extern void av_log_set_level(int); diff --git a/src/libffmpeg/libavcodec/bitstream.c b/src/libffmpeg/libavcodec/bitstream.c new file mode 100755 index 000000000..2678772c4 --- /dev/null +++ b/src/libffmpeg/libavcodec/bitstream.c @@ -0,0 +1,287 @@ +/* + * Common bit i/o utils + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer + * + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * alternative bitstream reader & writer by Michael Niedermayer + */ + +/** + * @file bitstream.c + * bitstream api. + */ + +#include "avcodec.h" +#include "bitstream.h" + +void align_put_bits(PutBitContext *s) +{ +#ifdef ALT_BITSTREAM_WRITER + put_bits(s,( - s->index) & 7,0); +#else + put_bits(s,s->bit_left & 7,0); +#endif +} + +void put_string(PutBitContext * pbc, char *s, int put_zero) +{ + while(*s){ + put_bits(pbc, 8, *s); + s++; + } + if(put_zero) + put_bits(pbc, 8, 0); +} + +/* bit input functions */ + +/** + * reads 0-32 bits. + */ +unsigned int get_bits_long(GetBitContext *s, int n){ + if(n<=17) return get_bits(s, n); + else{ + int ret= get_bits(s, 16) << (n-16); + return ret | get_bits(s, n-16); + } +} + +/** + * shows 0-32 bits. + */ +unsigned int show_bits_long(GetBitContext *s, int n){ + if(n<=17) return show_bits(s, n); + else{ + GetBitContext gb= *s; + int ret= get_bits_long(s, n); + *s= gb; + return ret; + } +} + +void align_get_bits(GetBitContext *s) +{ + int n= (-get_bits_count(s)) & 7; + if(n) skip_bits(s, n); +} + +int check_marker(GetBitContext *s, const char *msg) +{ + int bit= get_bits1(s); + if(!bit) + av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg); + + return bit; +} + +/* VLC decoding */ + +//#define DEBUG_VLC + +#define GET_DATA(v, table, i, wrap, size) \ +{\ + const uint8_t *ptr = (const uint8_t *)table + i * wrap;\ + switch(size) {\ + case 1:\ + v = *(const uint8_t *)ptr;\ + break;\ + case 2:\ + v = *(const uint16_t *)ptr;\ + break;\ + default:\ + v = *(const uint32_t *)ptr;\ + break;\ + }\ +} + + +static int alloc_table(VLC *vlc, int size, int use_static) +{ + int index; + index = vlc->table_size; + vlc->table_size += size; + if (vlc->table_size > vlc->table_allocated) { + vlc->table_allocated += (1 << vlc->bits); + if(use_static) + vlc->table = av_realloc_static(vlc->table, + sizeof(VLC_TYPE) * 2 * vlc->table_allocated); + else + vlc->table = av_realloc(vlc->table, + sizeof(VLC_TYPE) * 2 * vlc->table_allocated); + if (!vlc->table) + return -1; + } + return index; +} + +static int build_table(VLC *vlc, int table_nb_bits, + int nb_codes, + const void *bits, int bits_wrap, int bits_size, + const void *codes, int codes_wrap, int codes_size, + uint32_t code_prefix, int n_prefix, int use_static) +{ + int i, j, k, n, table_size, table_index, nb, n1, index; + uint32_t code; + VLC_TYPE (*table)[2]; + + table_size = 1 << table_nb_bits; + table_index = alloc_table(vlc, table_size, use_static); +#ifdef DEBUG_VLC + printf("new table index=%d size=%d code_prefix=%x n=%d\n", + table_index, table_size, code_prefix, n_prefix); +#endif + if (table_index < 0) + return -1; + table = &vlc->table[table_index]; + + for(i=0;i 0 && (code >> n) == code_prefix) { + if (n <= table_nb_bits) { + /* no need to add another table */ + j = (code << (table_nb_bits - n)) & (table_size - 1); + nb = 1 << (table_nb_bits - n); + for(k=0;k> n) & ((1 << table_nb_bits) - 1); +#ifdef DEBUG_VLC + printf("%4x: n=%d (subtable)\n", + j, n); +#endif + /* compute table size */ + n1 = -table[j][1]; //bits + if (n > n1) + n1 = n; + table[j][1] = -n1; //bits + } + } + } + + /* second pass : fill auxillary tables recursively */ + for(i=0;i table_nb_bits) { + n = table_nb_bits; + table[i][1] = -n; //bits + } + index = build_table(vlc, n, nb_codes, + bits, bits_wrap, bits_size, + codes, codes_wrap, codes_size, + (code_prefix << table_nb_bits) | i, + n_prefix + table_nb_bits, use_static); + if (index < 0) + return -1; + /* note: realloc has been done, so reload tables */ + table = &vlc->table[table_index]; + table[i][0] = index; //code + } + } + return table_index; +} + + +/* Build VLC decoding tables suitable for use with get_vlc(). + + 'nb_bits' set thee decoding table size (2^nb_bits) entries. The + bigger it is, the faster is the decoding. But it should not be too + big to save memory and L1 cache. '9' is a good compromise. + + 'nb_codes' : number of vlcs codes + + 'bits' : table which gives the size (in bits) of each vlc code. + + 'codes' : table which gives the bit pattern of of each vlc code. + + 'xxx_wrap' : give the number of bytes between each entry of the + 'bits' or 'codes' tables. + + 'xxx_size' : gives the number of bytes of each entry of the 'bits' + or 'codes' tables. + + 'wrap' and 'size' allows to use any memory configuration and types + (byte/word/long) to store the 'bits' and 'codes' tables. + + 'use_static' should be set to 1 for tables, which should be freed + with av_free_static(), 0 if free_vlc() will be used. +*/ +int init_vlc(VLC *vlc, int nb_bits, int nb_codes, + const void *bits, int bits_wrap, int bits_size, + const void *codes, int codes_wrap, int codes_size, + int use_static) +{ + vlc->bits = nb_bits; + if(!use_static) { + vlc->table = NULL; + vlc->table_allocated = 0; + vlc->table_size = 0; + } else { + /* Static tables are initially always NULL, return + if vlc->table != NULL to avoid double allocation */ + if(vlc->table) + return 0; + } + +#ifdef DEBUG_VLC + printf("build table nb_codes=%d\n", nb_codes); +#endif + + if (build_table(vlc, nb_bits, nb_codes, + bits, bits_wrap, bits_size, + codes, codes_wrap, codes_size, + 0, 0, use_static) < 0) { + av_free(vlc->table); + return -1; + } + return 0; +} + + +void free_vlc(VLC *vlc) +{ + av_free(vlc->table); +} + diff --git a/src/libffmpeg/libavcodec/bitstream.h b/src/libffmpeg/libavcodec/bitstream.h new file mode 100644 index 000000000..fd69915d8 --- /dev/null +++ b/src/libffmpeg/libavcodec/bitstream.h @@ -0,0 +1,854 @@ +/** + * @file bitstream.h + * bitstream api header. + */ + +#ifndef BITSTREAM_H +#define BITSTREAM_H + +//#define ALT_BITSTREAM_WRITER +//#define ALIGNED_BITSTREAM_WRITER + +#define ALT_BITSTREAM_READER +//#define LIBMPEG2_BITSTREAM_READER +//#define A32_BITSTREAM_READER +#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO + +extern const uint8_t ff_reverse[256]; + +#if defined(ARCH_X86) || defined(ARCH_X86_64) +// avoid +32 for shift optimization (gcc should do that ...) +static inline int32_t NEG_SSR32( int32_t a, int8_t s){ + asm ("sarl %1, %0\n\t" + : "+r" (a) + : "ic" ((uint8_t)(-s)) + ); + return a; +} +static inline uint32_t NEG_USR32(uint32_t a, int8_t s){ + asm ("shrl %1, %0\n\t" + : "+r" (a) + : "ic" ((uint8_t)(-s)) + ); + return a; +} +#else +# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) +# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) +#endif + +/* bit output */ + +/* buf and buf_end must be present and used by every alternative writer. */ +typedef struct PutBitContext { +#ifdef ALT_BITSTREAM_WRITER + uint8_t *buf, *buf_end; + int index; +#else + uint32_t bit_buf; + int bit_left; + uint8_t *buf, *buf_ptr, *buf_end; +#endif +} PutBitContext; + +static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) +{ + s->buf = buffer; + s->buf_end = s->buf + buffer_size; +#ifdef ALT_BITSTREAM_WRITER + s->index=0; + ((uint32_t*)(s->buf))[0]=0; +// memset(buffer, 0, buffer_size); +#else + s->buf_ptr = s->buf; + s->bit_left=32; + s->bit_buf=0; +#endif +} + +/* return the number of bits output */ +static inline int put_bits_count(PutBitContext *s) +{ +#ifdef ALT_BITSTREAM_WRITER + return s->index; +#else + return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left; +#endif +} + +/* pad the end of the output stream with zeros */ +static inline void flush_put_bits(PutBitContext *s) +{ +#ifdef ALT_BITSTREAM_WRITER + align_put_bits(s); +#else + s->bit_buf<<= s->bit_left; + while (s->bit_left < 32) { + /* XXX: should test end of buffer */ + *s->buf_ptr++=s->bit_buf >> 24; + s->bit_buf<<=8; + s->bit_left+=8; + } + s->bit_left=32; + s->bit_buf=0; +#endif +} + +void align_put_bits(PutBitContext *s); +void put_string(PutBitContext * pbc, char *s, int put_zero); + +/* bit input */ +/* buffer, buffer_end and size_in_bits must be present and used by every reader */ +typedef struct GetBitContext { + const uint8_t *buffer, *buffer_end; +#ifdef ALT_BITSTREAM_READER + int index; +#elif defined LIBMPEG2_BITSTREAM_READER + uint8_t *buffer_ptr; + uint32_t cache; + int bit_count; +#elif defined A32_BITSTREAM_READER + uint32_t *buffer_ptr; + uint32_t cache0; + uint32_t cache1; + int bit_count; +#endif + int size_in_bits; +} GetBitContext; + +#define VLC_TYPE int16_t + +typedef struct VLC { + int bits; + VLC_TYPE (*table)[2]; ///< code, bits + int table_size, table_allocated; +} VLC; + +typedef struct RL_VLC_ELEM { + int16_t level; + int8_t len; + uint8_t run; +} RL_VLC_ELEM; + +#ifdef ARCH_SPARC +#define UNALIGNED_STORES_ARE_BAD +#endif + +/* used to avoid missaligned exceptions on some archs (alpha, ...) */ +#if defined(ARCH_X86) || defined(ARCH_X86_64) +# define unaligned32(a) (*(const uint32_t*)(a)) +#else +# ifdef __GNUC__ +static inline uint32_t unaligned32(const void *v) { + struct Unaligned { + uint32_t i; + } __attribute__((packed)); + + return ((const struct Unaligned *) v)->i; +} +# elif defined(__DECC) +static inline uint32_t unaligned32(const void *v) { + return *(const __unaligned uint32_t *) v; +} +# else +static inline uint32_t unaligned32(const void *v) { + return *(const uint32_t *) v; +} +# endif +#endif //!ARCH_X86 + +#ifndef ALT_BITSTREAM_WRITER +static inline void put_bits(PutBitContext *s, int n, unsigned int value) +{ + unsigned int bit_buf; + int bit_left; + +#ifdef STATS + st_out_bit_counts[st_current_index] += n; +#endif + // printf("put_bits=%d %x\n", n, value); + assert(n == 32 || value < (1U << n)); + + bit_buf = s->bit_buf; + bit_left = s->bit_left; + + // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); + /* XXX: optimize */ + if (n < bit_left) { + bit_buf = (bit_buf<> (n - bit_left); +#ifdef UNALIGNED_STORES_ARE_BAD + if (3 & (intptr_t) s->buf_ptr) { + s->buf_ptr[0] = bit_buf >> 24; + s->buf_ptr[1] = bit_buf >> 16; + s->buf_ptr[2] = bit_buf >> 8; + s->buf_ptr[3] = bit_buf ; + } else +#endif + *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); + //printf("bitbuf = %08x\n", bit_buf); + s->buf_ptr+=4; + bit_left+=32 - n; + bit_buf = value; + } + + s->bit_buf = bit_buf; + s->bit_left = bit_left; +} +#endif + + +#ifdef ALT_BITSTREAM_WRITER +static inline void put_bits(PutBitContext *s, int n, unsigned int value) +{ +# ifdef ALIGNED_BITSTREAM_WRITER +# if defined(ARCH_X86) || defined(ARCH_X86_64) + asm volatile( + "movl %0, %%ecx \n\t" + "xorl %%eax, %%eax \n\t" + "shrdl %%cl, %1, %%eax \n\t" + "shrl %%cl, %1 \n\t" + "movl %0, %%ecx \n\t" + "shrl $3, %%ecx \n\t" + "andl $0xFFFFFFFC, %%ecx \n\t" + "bswapl %1 \n\t" + "orl %1, (%2, %%ecx) \n\t" + "bswapl %%eax \n\t" + "addl %3, %0 \n\t" + "movl %%eax, 4(%2, %%ecx) \n\t" + : "=&r" (s->index), "=&r" (value) + : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n)) + : "%eax", "%ecx" + ); +# else + int index= s->index; + uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5); + + value<<= 32-n; + + ptr[0] |= be2me_32(value>>(index&31)); + ptr[1] = be2me_32(value<<(32-(index&31))); +//if(n>24) printf("%d %d\n", n, value); + index+= n; + s->index= index; +# endif +# else //ALIGNED_BITSTREAM_WRITER +# if defined(ARCH_X86) || defined(ARCH_X86_64) + asm volatile( + "movl $7, %%ecx \n\t" + "andl %0, %%ecx \n\t" + "addl %3, %%ecx \n\t" + "negl %%ecx \n\t" + "shll %%cl, %1 \n\t" + "bswapl %1 \n\t" + "movl %0, %%ecx \n\t" + "shrl $3, %%ecx \n\t" + "orl %1, (%%ecx, %2) \n\t" + "addl %3, %0 \n\t" + "movl $0, 4(%%ecx, %2) \n\t" + : "=&r" (s->index), "=&r" (value) + : "r" (s->buf), "r" (n), "0" (s->index), "1" (value) + : "%ecx" + ); +# else + int index= s->index; + uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3)); + + ptr[0] |= be2me_32(value<<(32-n-(index&7) )); + ptr[1] = 0; +//if(n>24) printf("%d %d\n", n, value); + index+= n; + s->index= index; +# endif +# endif //!ALIGNED_BITSTREAM_WRITER +} +#endif + + +static inline uint8_t* pbBufPtr(PutBitContext *s) +{ +#ifdef ALT_BITSTREAM_WRITER + return s->buf + (s->index>>3); +#else + return s->buf_ptr; +#endif +} + +/** + * + * PutBitContext must be flushed & aligned to a byte boundary before calling this. + */ +static inline void skip_put_bytes(PutBitContext *s, int n){ + assert((put_bits_count(s)&7)==0); +#ifdef ALT_BITSTREAM_WRITER + FIXME may need some cleaning of the buffer + s->index += n<<3; +#else + assert(s->bit_left==32); + s->buf_ptr += n; +#endif +} + +/** + * skips the given number of bits. + * must only be used if the actual values in the bitstream dont matter + */ +static inline void skip_put_bits(PutBitContext *s, int n){ +#ifdef ALT_BITSTREAM_WRITER + s->index += n; +#else + s->bit_left -= n; + s->buf_ptr-= s->bit_left>>5; + s->bit_left &= 31; +#endif +} + +/** + * Changes the end of the buffer. + */ +static inline void set_put_bits_buffer_size(PutBitContext *s, int size){ + s->buf_end= s->buf + size; +} + +/* Bitstream reader API docs: +name + abritary name which is used as prefix for the internal variables + +gb + getbitcontext + +OPEN_READER(name, gb) + loads gb into local variables + +CLOSE_READER(name, gb) + stores local vars in gb + +UPDATE_CACHE(name, gb) + refills the internal cache from the bitstream + after this call at least MIN_CACHE_BITS will be available, + +GET_CACHE(name, gb) + will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) + +SHOW_UBITS(name, gb, num) + will return the nest num bits + +SHOW_SBITS(name, gb, num) + will return the nest num bits and do sign extension + +SKIP_BITS(name, gb, num) + will skip over the next num bits + note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER + +SKIP_CACHE(name, gb, num) + will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) + +SKIP_COUNTER(name, gb, num) + will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) + +LAST_SKIP_CACHE(name, gb, num) + will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing + +LAST_SKIP_BITS(name, gb, num) + is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER + +for examples see get_bits, show_bits, skip_bits, get_vlc +*/ + +static inline int unaligned32_be(const void *v) +{ +#ifdef CONFIG_ALIGN + const uint8_t *p=v; + return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]); +#else + return be2me_32( unaligned32(v)); //original +#endif +} + +#ifdef ALT_BITSTREAM_READER +# define MIN_CACHE_BITS 25 + +# define OPEN_READER(name, gb)\ + int name##_index= (gb)->index;\ + int name##_cache= 0;\ + +# define CLOSE_READER(name, gb)\ + (gb)->index= name##_index;\ + +# define UPDATE_CACHE(name, gb)\ + name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ + +# define SKIP_CACHE(name, gb, num)\ + name##_cache <<= (num);\ + +// FIXME name? +# define SKIP_COUNTER(name, gb, num)\ + name##_index += (num);\ + +# define SKIP_BITS(name, gb, num)\ + {\ + SKIP_CACHE(name, gb, num)\ + SKIP_COUNTER(name, gb, num)\ + }\ + +# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) +# define LAST_SKIP_CACHE(name, gb, num) ; + +# define SHOW_UBITS(name, gb, num)\ + NEG_USR32(name##_cache, num) + +# define SHOW_SBITS(name, gb, num)\ + NEG_SSR32(name##_cache, num) + +# define GET_CACHE(name, gb)\ + ((uint32_t)name##_cache) + +static inline int get_bits_count(GetBitContext *s){ + return s->index; +} +#elif defined LIBMPEG2_BITSTREAM_READER +//libmpeg2 like reader + +# define MIN_CACHE_BITS 17 + +# define OPEN_READER(name, gb)\ + int name##_bit_count=(gb)->bit_count;\ + int name##_cache= (gb)->cache;\ + uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\ + +# define CLOSE_READER(name, gb)\ + (gb)->bit_count= name##_bit_count;\ + (gb)->cache= name##_cache;\ + (gb)->buffer_ptr= name##_buffer_ptr;\ + +#ifdef LIBMPEG2_BITSTREAM_READER_HACK + +# define UPDATE_CACHE(name, gb)\ + if(name##_bit_count >= 0){\ + name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\ + name##_buffer_ptr += 2;\ + name##_bit_count-= 16;\ + }\ + +#else + +# define UPDATE_CACHE(name, gb)\ + if(name##_bit_count >= 0){\ + name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\ + name##_buffer_ptr+=2;\ + name##_bit_count-= 16;\ + }\ + +#endif + +# define SKIP_CACHE(name, gb, num)\ + name##_cache <<= (num);\ + +# define SKIP_COUNTER(name, gb, num)\ + name##_bit_count += (num);\ + +# define SKIP_BITS(name, gb, num)\ + {\ + SKIP_CACHE(name, gb, num)\ + SKIP_COUNTER(name, gb, num)\ + }\ + +# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) +# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) + +# define SHOW_UBITS(name, gb, num)\ + NEG_USR32(name##_cache, num) + +# define SHOW_SBITS(name, gb, num)\ + NEG_SSR32(name##_cache, num) + +# define GET_CACHE(name, gb)\ + ((uint32_t)name##_cache) + +static inline int get_bits_count(GetBitContext *s){ + return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; +} + +#elif defined A32_BITSTREAM_READER + +# define MIN_CACHE_BITS 32 + +# define OPEN_READER(name, gb)\ + int name##_bit_count=(gb)->bit_count;\ + uint32_t name##_cache0= (gb)->cache0;\ + uint32_t name##_cache1= (gb)->cache1;\ + uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ + +# define CLOSE_READER(name, gb)\ + (gb)->bit_count= name##_bit_count;\ + (gb)->cache0= name##_cache0;\ + (gb)->cache1= name##_cache1;\ + (gb)->buffer_ptr= name##_buffer_ptr;\ + +# define UPDATE_CACHE(name, gb)\ + if(name##_bit_count > 0){\ + const uint32_t next= be2me_32( *name##_buffer_ptr );\ + name##_cache0 |= NEG_USR32(next,name##_bit_count);\ + name##_cache1 |= next<buffer_ptr - s->buffer)*8 - 32 + s->bit_count; +} + +#endif + +/** + * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). + * if MSB not set it is negative + * @param n length in bits + * @author BERO + */ +static inline int get_xbits(GetBitContext *s, int n){ + register int tmp; + register int32_t cache; + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + cache = GET_CACHE(re,s); + if ((int32_t)cache<0) { //MSB=1 + tmp = NEG_USR32(cache,n); + } else { + // tmp = (-1<index+=n for the ALT_READER :)) + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + LAST_SKIP_BITS(re, s, n) + CLOSE_READER(re, s) +} + +static inline unsigned int get_bits1(GetBitContext *s){ +#ifdef ALT_BITSTREAM_READER + int index= s->index; + uint8_t result= s->buffer[ index>>3 ]; + result<<= (index&0x07); + result>>= 8 - 1; + index++; + s->index= index; + + return result; +#else + return get_bits(s, 1); +#endif +} + +static inline unsigned int show_bits1(GetBitContext *s){ + return show_bits(s, 1); +} + +static inline void skip_bits1(GetBitContext *s){ + skip_bits(s, 1); +} + +/** + * init GetBitContext. + * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits + * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end + * @param bit_size the size of the buffer in bits + */ +static inline void init_get_bits(GetBitContext *s, + const uint8_t *buffer, int bit_size) +{ + const int buffer_size= (bit_size+7)>>3; + + s->buffer= buffer; + s->size_in_bits= bit_size; + s->buffer_end= buffer + buffer_size; +#ifdef ALT_BITSTREAM_READER + s->index=0; +#elif defined LIBMPEG2_BITSTREAM_READER +#ifdef LIBMPEG2_BITSTREAM_READER_HACK + if ((int)buffer&1) { + /* word alignment */ + s->cache = (*buffer++)<<24; + s->buffer_ptr = buffer; + s->bit_count = 16-8; + } else +#endif + { + s->buffer_ptr = buffer; + s->bit_count = 16; + s->cache = 0; + } +#elif defined A32_BITSTREAM_READER + s->buffer_ptr = (uint32_t*)buffer; + s->bit_count = 32; + s->cache0 = 0; + s->cache1 = 0; +#endif + { + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + UPDATE_CACHE(re, s) + CLOSE_READER(re, s) + } +#ifdef A32_BITSTREAM_READER + s->cache1 = 0; +#endif +} + +int check_marker(GetBitContext *s, const char *msg); +void align_get_bits(GetBitContext *s); +int init_vlc(VLC *vlc, int nb_bits, int nb_codes, + const void *bits, int bits_wrap, int bits_size, + const void *codes, int codes_wrap, int codes_size, + int use_static); +void free_vlc(VLC *vlc); + +/** + * + * if the vlc code is invalid and max_depth=1 than no bits will be removed + * if the vlc code is invalid and max_depth>1 than the number of bits removed + * is undefined + */ +#define GET_VLC(code, name, gb, table, bits, max_depth)\ +{\ + int n, index, nb_bits;\ +\ + index= SHOW_UBITS(name, gb, bits);\ + code = table[index][0];\ + n = table[index][1];\ +\ + if(max_depth > 1 && n < 0){\ + LAST_SKIP_BITS(name, gb, bits)\ + UPDATE_CACHE(name, gb)\ +\ + nb_bits = -n;\ +\ + index= SHOW_UBITS(name, gb, nb_bits) + code;\ + code = table[index][0];\ + n = table[index][1];\ + if(max_depth > 2 && n < 0){\ + LAST_SKIP_BITS(name, gb, nb_bits)\ + UPDATE_CACHE(name, gb)\ +\ + nb_bits = -n;\ +\ + index= SHOW_UBITS(name, gb, nb_bits) + code;\ + code = table[index][0];\ + n = table[index][1];\ + }\ + }\ + SKIP_BITS(name, gb, n)\ +} + +#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\ +{\ + int n, index, nb_bits;\ +\ + index= SHOW_UBITS(name, gb, bits);\ + level = table[index].level;\ + n = table[index].len;\ +\ + if(max_depth > 1 && n < 0){\ + SKIP_BITS(name, gb, bits)\ + if(need_update){\ + UPDATE_CACHE(name, gb)\ + }\ +\ + nb_bits = -n;\ +\ + index= SHOW_UBITS(name, gb, nb_bits) + level;\ + level = table[index].level;\ + n = table[index].len;\ + }\ + run= table[index].run;\ + SKIP_BITS(name, gb, n)\ +} + +// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly +static inline int get_vlc(GetBitContext *s, VLC *vlc) +{ + int code; + VLC_TYPE (*table)[2]= vlc->table; + + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + + GET_VLC(code, re, s, table, vlc->bits, 3) + + CLOSE_READER(re, s) + return code; +} + +/** + * parses a vlc code, faster then get_vlc() + * @param bits is the number of bits which will be read at once, must be + * identical to nb_bits in init_vlc() + * @param max_depth is the number of times bits bits must be readed to completly + * read the longest vlc code + * = (max_vlc_length + bits - 1) / bits + */ +static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], + int bits, int max_depth) +{ + int code; + + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + + GET_VLC(code, re, s, table, bits, max_depth) + + CLOSE_READER(re, s) + return code; +} + +//#define TRACE + +#ifdef TRACE +#include "avcodec.h" +static inline void print_bin(int bits, int n){ + int i; + + for(i=n-1; i>=0; i--){ + av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1); + } + for(i=n; i<24; i++) + av_log(NULL, AV_LOG_DEBUG, " "); +} + +static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ + int r= get_bits(s, n); + + print_bin(r, n); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line); + return r; +} +static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){ + int show= show_bits(s, 24); + int pos= get_bits_count(s); + int r= get_vlc2(s, table, bits, max_depth); + int len= get_bits_count(s) - pos; + int bits2= show>>(24-len); + + print_bin(bits2, len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line); + return r; +} +static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ + int show= show_bits(s, n); + int r= get_xbits(s, n); + + print_bin(show, n); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line); + return r; +} + +#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) + +#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__) + +#else //TRACE +#define tprintf(...) {} +#endif + +static inline int decode012(GetBitContext *gb){ + int n; + n = get_bits1(gb); + if (n == 0) + return 0; + else + return get_bits1(gb) + 1; +} + +#endif /* BITSTREAM_H */ diff --git a/src/libffmpeg/libavcodec/bswap.h b/src/libffmpeg/libavcodec/bswap.h index 460f7abd4..eb1d87a55 100644 --- a/src/libffmpeg/libavcodec/bswap.h +++ b/src/libffmpeg/libavcodec/bswap.h @@ -10,17 +10,23 @@ #include #else -#ifdef ARCH_X86 -static inline unsigned short ByteSwap16(unsigned short x) +#ifdef ARCH_X86_64 +# define LEGACY_REGS "=Q" +#else +# define LEGACY_REGS "=q" +#endif + +#if defined(ARCH_X86) || defined(ARCH_X86_64) +static inline uint16_t ByteSwap16(uint16_t x) { __asm("xchgb %b0,%h0" : - "=q" (x) : + LEGACY_REGS (x) : "0" (x)); return x; } #define bswap_16(x) ByteSwap16(x) -static inline unsigned int ByteSwap32(unsigned int x) +static inline uint32_t ByteSwap32(uint32_t x) { #if __CPU__ > 386 __asm("bswap %0": @@ -29,21 +35,28 @@ static inline unsigned int ByteSwap32(unsigned int x) __asm("xchgb %b0,%h0\n" " rorl $16,%0\n" " xchgb %b0,%h0": - "=q" (x) : + LEGACY_REGS (x) : #endif "0" (x)); return x; } #define bswap_32(x) ByteSwap32(x) -static inline unsigned long long int ByteSwap64(unsigned long long int x) +static inline uint64_t ByteSwap64(uint64_t x) { +#ifdef ARCH_X86_64 + __asm("bswap %0": + "=r" (x) : + "0" (x)); + return x; +#else register union { __extension__ uint64_t __ll; uint32_t __l[2]; } __x; asm("xchgl %0,%1": "=r"(__x.__l[0]),"=r"(__x.__l[1]): - "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32)))); + "0"(bswap_32((uint32_t)x)),"1"(bswap_32((uint32_t)(x>>32)))); return __x.__ll; +#endif } #define bswap_64(x) ByteSwap64(x) diff --git a/src/libffmpeg/libavcodec/cabac.c b/src/libffmpeg/libavcodec/cabac.c index 9d56e23fc..9a598fa47 100644 --- a/src/libffmpeg/libavcodec/cabac.c +++ b/src/libffmpeg/libavcodec/cabac.c @@ -26,6 +26,7 @@ #include #include "common.h" +#include "bitstream.h" #include "cabac.h" const uint8_t ff_h264_lps_range[64][4]= { @@ -69,6 +70,25 @@ const uint8_t ff_h264_lps_state[64]= { 36,36,37,37,37,38,38,63, }; +const uint8_t ff_h264_norm_shift[256]= { + 8,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + /** * * @param buf_size size of buf in bits @@ -95,10 +115,14 @@ void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){ c->bytestream= buf; c->bytestream_end= buf + buf_size; - c->low= *c->bytestream++; - c->low= (c->low<<9) + ((*c->bytestream++)<<1); - c->range= 0x1FE00; - c->bits_left= 7; +#if CABAC_BITS == 16 + c->low = (*c->bytestream++)<<18; + c->low+= (*c->bytestream++)<<10; +#else + c->low = (*c->bytestream++)<<10; +#endif + c->low+= ((*c->bytestream++)<<2) + 2; + c->range= 0x1FE<<(CABAC_BITS + 1); } void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], @@ -107,8 +131,8 @@ void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], for(i=0; ilps_range[2*i+0][j]= - c->lps_range[2*i+1][j]= lps_range[i][j]; + c->lps_range[2*i+0][j+4]= + c->lps_range[2*i+1][j+4]= lps_range[i][j]; } c->mps_state[2*i+0]= 2*mps_state[i]; @@ -126,6 +150,9 @@ void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], #if 0 //selftest #define SIZE 10240 + +#include "avcodec.h" + int main(){ CABACContext c; uint8_t b[9*SIZE]; @@ -173,33 +200,33 @@ STOP_TIMER("put_cabac_ueg") for(i=0; i +#define CABAC_BITS 8 +#define CABAC_MASK ((1<lps_range[*state][((c->range)>>6)&3]; + int RangeLPS= c->lps_range[*state][c->range>>6]; if(bit == ((*state)&1)){ c->range -= RangeLPS; @@ -249,63 +253,102 @@ static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int ma } } +static void refill(CABACContext *c){ + if(c->bytestream < c->bytestream_end) +#if CABAC_BITS == 16 + c->low+= ((c->bytestream[0]<<9) + (c->bytestream[1])<<1); +#else + c->low+= c->bytestream[0]<<1; +#endif + c->low -= CABAC_MASK; + c->bytestream+= CABAC_BITS/8; +} + +#if 0 /* all use commented */ +static void refill2(CABACContext *c){ + int i, x; + + x= c->low ^ (c->low-1); + i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)]; + + x= -CABAC_MASK; + + if(c->bytestream < c->bytestream_end) +#if CABAC_BITS == 16 + x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); +#else + x+= c->bytestream[0]<<1; +#endif + + c->low += x<bytestream+= CABAC_BITS/8; +} +#endif + static inline void renorm_cabac_decoder(CABACContext *c){ - while(c->range < 0x10000){ + while(c->range < (0x200 << CABAC_BITS)){ c->range+= c->range; c->low+= c->low; - if(--c->bits_left == 0){ - if(c->bytestream < c->bytestream_end) - c->low+= *c->bytestream; - c->bytestream++; - c->bits_left= 8; - } + if(!(c->low & CABAC_MASK)) + refill(c); } } +static inline void renorm_cabac_decoder_once(CABACContext *c){ + int mask= (c->range - (0x200 << CABAC_BITS))>>31; + c->range+= c->range&mask; + c->low += c->low &mask; + if(!(c->low & CABAC_MASK)) + refill(c); +} + static inline int get_cabac(CABACContext *c, uint8_t * const state){ - int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8; - int bit; + int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1); + int bit, lps_mask attribute_unused; c->range -= RangeLPS; +#if 1 if(c->low < c->range){ bit= (*state)&1; *state= c->mps_state[*state]; + renorm_cabac_decoder_once(c); }else{ +// int shift= ff_h264_norm_shift[RangeLPS>>17]; bit= ((*state)&1)^1; c->low -= c->range; - c->range = RangeLPS; *state= c->lps_state[*state]; + c->range = RangeLPS; + renorm_cabac_decoder(c); +/* c->range = RangeLPS<low <<= shift; + if(!(c->low & 0xFFFF)){ + refill2(c); + }*/ } - renorm_cabac_decoder(c); +#else + lps_mask= (c->range - c->low)>>31; - return bit; -} - -static inline int get_cabac_static(CABACContext *c, int RangeLPS){ - int bit; + c->low -= c->range & lps_mask; + c->range += (RangeLPS - c->range) & lps_mask; - c->range -= RangeLPS; - if(c->low < c->range){ - bit= 0; - }else{ - bit= 1; - c->low -= c->range; - c->range = RangeLPS; - } - renorm_cabac_decoder(c); + bit= ((*state)^lps_mask)&1; + *state= c->mps_state[(*state) - (128&lps_mask)]; + lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)]; + c->range<<= lps_mask; + c->low <<= lps_mask; + if(!(c->low & CABAC_MASK)) + refill2(c); +#endif + return bit; } static inline int get_cabac_bypass(CABACContext *c){ c->low += c->low; - if(--c->bits_left == 0){ - if(c->bytestream < c->bytestream_end) - c->low+= *c->bytestream; - c->bytestream++; - c->bits_left= 8; - } + if(!(c->low & CABAC_MASK)) + refill(c); if(c->low < c->range){ return 0; @@ -320,9 +363,9 @@ static inline int get_cabac_bypass(CABACContext *c){ * @return the number of bytes read or 0 if no end */ static inline int get_cabac_terminate(CABACContext *c){ - c->range -= 2<<8; + c->range -= 4<low < c->range){ - renorm_cabac_decoder(c); + renorm_cabac_decoder_once(c); return 0; }else{ return c->bytestream - c->bytestream_start; diff --git a/src/libffmpeg/libavcodec/cinepak.c b/src/libffmpeg/libavcodec/cinepak.c index da9a8127f..3c560fdc5 100644 --- a/src/libffmpeg/libavcodec/cinepak.c +++ b/src/libffmpeg/libavcodec/cinepak.c @@ -35,7 +35,6 @@ #include "avcodec.h" #include "dsputil.h" -#define PALETTE_COUNT 256 typedef struct { uint8_t y0, y1, y2, y3; @@ -63,7 +62,6 @@ typedef struct CinepakContext { int width, height; - unsigned char palette[PALETTE_COUNT * 4]; int palette_video; cvid_strip_t strips[MAX_STRIPS]; @@ -177,28 +175,28 @@ static int cinepak_decode_vectors (CinepakContext *s, cvid_strip_t *strip, s->frame.data[2][iv[0]] = codebook->v; } - s->frame.data[0][iy[0] + 2] = codebook->y0; - s->frame.data[0][iy[0] + 3] = codebook->y0; - s->frame.data[0][iy[1] + 2] = codebook->y0; - s->frame.data[0][iy[1] + 3] = codebook->y0; + s->frame.data[0][iy[0] + 2] = codebook->y1; + s->frame.data[0][iy[0] + 3] = codebook->y1; + s->frame.data[0][iy[1] + 2] = codebook->y1; + s->frame.data[0][iy[1] + 3] = codebook->y1; if (!s->palette_video) { s->frame.data[1][iu[0] + 1] = codebook->u; s->frame.data[2][iv[0] + 1] = codebook->v; } - s->frame.data[0][iy[2] + 0] = codebook->y0; - s->frame.data[0][iy[2] + 1] = codebook->y0; - s->frame.data[0][iy[3] + 0] = codebook->y0; - s->frame.data[0][iy[3] + 1] = codebook->y0; + s->frame.data[0][iy[2] + 0] = codebook->y2; + s->frame.data[0][iy[2] + 1] = codebook->y2; + s->frame.data[0][iy[3] + 0] = codebook->y2; + s->frame.data[0][iy[3] + 1] = codebook->y2; if (!s->palette_video) { s->frame.data[1][iu[1]] = codebook->u; s->frame.data[2][iv[1]] = codebook->v; } - s->frame.data[0][iy[2] + 2] = codebook->y0; - s->frame.data[0][iy[2] + 3] = codebook->y0; - s->frame.data[0][iy[3] + 2] = codebook->y0; - s->frame.data[0][iy[3] + 3] = codebook->y0; + s->frame.data[0][iy[2] + 2] = codebook->y3; + s->frame.data[0][iy[2] + 3] = codebook->y3; + s->frame.data[0][iy[3] + 2] = codebook->y3; + s->frame.data[0][iy[3] + 3] = codebook->y3; if (!s->palette_video) { s->frame.data[1][iu[1] + 1] = codebook->u; s->frame.data[2][iv[1] + 1] = codebook->v; @@ -361,22 +359,20 @@ static int cinepak_decode (CinepakContext *s) static int cinepak_decode_init(AVCodecContext *avctx) { CinepakContext *s = (CinepakContext *)avctx->priv_data; -/* - int i; - unsigned char r, g, b; - unsigned char *raw_palette; - unsigned int *palette32; -*/ s->avctx = avctx; s->width = (avctx->width + 3) & ~3; s->height = (avctx->height + 3) & ~3; -// check for paletted data -s->palette_video = 0; - + // check for paletted data + if ((avctx->palctrl == NULL) || (avctx->bits_per_sample == 40)) { + s->palette_video = 0; + avctx->pix_fmt = PIX_FMT_YUV420P; + } else { + s->palette_video = 1; + avctx->pix_fmt = PIX_FMT_PAL8; + } - avctx->pix_fmt = PIX_FMT_YUV420P; avctx->has_b_frames = 0; dsputil_init(&s->dsp, avctx); @@ -404,6 +400,15 @@ static int cinepak_decode_frame(AVCodecContext *avctx, cinepak_decode(s); + if (s->palette_video) { + memcpy (s->frame.data[1], avctx->palctrl->palette, AVPALETTE_SIZE); + if (avctx->palctrl->palette_changed) { + s->frame.palette_has_changed = 1; + avctx->palctrl->palette_changed = 0; + } else + s->frame.palette_has_changed = 0; + } + *data_size = sizeof(AVFrame); *(AVFrame*)data = s->frame; diff --git a/src/libffmpeg/libavcodec/cljr.c b/src/libffmpeg/libavcodec/cljr.c index df1f79851..8072eee18 100644 --- a/src/libffmpeg/libavcodec/cljr.c +++ b/src/libffmpeg/libavcodec/cljr.c @@ -43,11 +43,6 @@ static int decode_frame(AVCodecContext *avctx, AVFrame * const p= (AVFrame*)&a->picture; int x, y; - /* special case for last picture */ - if (buf_size == 0) { - return 0; - } - if(p->data[0]) avctx->release_buffer(avctx, p); @@ -124,12 +119,14 @@ static int decode_init(AVCodecContext *avctx){ return 0; } +#if 0 static int encode_init(AVCodecContext *avctx){ common_init(avctx); return 0; } +#endif AVCodec cljr_decoder = { "cljr", diff --git a/src/libffmpeg/libavcodec/common.h b/src/libffmpeg/libavcodec/common.h index 5cf9f2c73..b9e89be46 100644 --- a/src/libffmpeg/libavcodec/common.h +++ b/src/libffmpeg/libavcodec/common.h @@ -15,14 +15,6 @@ # define CONFIG_WIN32 #endif -//#define ALT_BITSTREAM_WRITER -//#define ALIGNED_BITSTREAM_WRITER - -#define ALT_BITSTREAM_READER -//#define LIBMPEG2_BITSTREAM_READER -//#define A32_BITSTREAM_READER -#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO - #ifndef M_PI #define M_PI 3.14159265358979323846 #endif @@ -35,6 +27,7 @@ # include # include # include +# include # ifndef __BEOS__ # include # else @@ -66,14 +59,6 @@ #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr } #define AVOPTION_END() AVOPTION_SUB(NULL) -struct AVOption; -#ifdef HAVE_MMX -extern const struct AVOption avoptions_common[3 + 5]; -#else -extern const struct AVOption avoptions_common[3]; -#endif -extern const struct AVOption avoptions_workaround_bug[11]; - #endif /* HAVE_AV_CONFIG_H */ /* Suppress restrict if it was not defined in config.h. */ @@ -97,6 +82,14 @@ extern const struct AVOption avoptions_workaround_bug[11]; #endif #endif +#ifndef attribute_unused +#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) +# define attribute_unused __attribute__((unused)) +#else +# define attribute_unused +#endif +#endif + #ifndef EMULATE_INTTYPES # include #else @@ -116,6 +109,18 @@ extern const struct AVOption avoptions_workaround_bug[11]; # endif /* other OS */ #endif /* HAVE_INTTYPES_H */ +#ifndef INT16_MIN +#define INT16_MIN (-0x7fff-1) +#endif + +#ifndef INT16_MAX +#define INT16_MAX 0x7fff +#endif + +#ifndef INT64_MIN +#define INT64_MIN (-0x7fffffffffffffffLL-1) +#endif + #ifndef INT64_MAX #define INT64_MAX int64_t_C(9223372036854775807) #endif @@ -134,6 +139,14 @@ typedef unsigned int uint_fast16_t; typedef unsigned int uint_fast32_t; #endif +#ifndef INT_BIT +# if INT_MAX != 2147483647 +# define INT_BIT 64 +# else +# define INT_BIT 32 +# endif +#endif + #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS) static inline float floorf(float f) { return floor(f); @@ -208,11 +221,20 @@ static inline float floorf(float f) { # include "bswap.h" +// Use rip-relative addressing if compiling PIC code on x86-64. # if defined(__MINGW32__) || defined(__CYGWIN__) || \ defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__)) -# define MANGLE(a) "_" #a +# if defined(ARCH_X86_64) && defined(PIC) +# define MANGLE(a) "_" #a"(%%rip)" +# else +# define MANGLE(a) "_" #a +# endif # else -# define MANGLE(a) #a +# if defined(ARCH_X86_64) && defined(PIC) +# define MANGLE(a) #a"(%%rip)" +# else +# define MANGLE(a) #a +# endif # endif /* debug stuff */ @@ -252,7 +274,7 @@ inline void dprintf(const char* fmt,...) {} extern const uint32_t inverse[256]; -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_X86_64) # define FASTDIV(a,b) \ ({\ int ret,dmy;\ @@ -269,819 +291,6 @@ extern const uint32_t inverse[256]; # define FASTDIV(a,b) ((a)/(b)) #endif -#ifdef ARCH_X86 -// avoid +32 for shift optimization (gcc should do that ...) -static inline int32_t NEG_SSR32( int32_t a, int8_t s){ - asm ("sarl %1, %0\n\t" - : "+r" (a) - : "ic" ((uint8_t)(-s)) - ); - return a; -} -static inline uint32_t NEG_USR32(uint32_t a, int8_t s){ - asm ("shrl %1, %0\n\t" - : "+r" (a) - : "ic" ((uint8_t)(-s)) - ); - return a; -} -#else -# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) -# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) -#endif - -/* bit output */ - -struct PutBitContext; - -typedef void (*WriteDataFunc)(void *, uint8_t *, int); - -/* buf and buf_end must be present and used by every alternative writer. */ -typedef struct PutBitContext { -#ifdef ALT_BITSTREAM_WRITER - uint8_t *buf, *buf_end; - int index; -#else - uint32_t bit_buf; - int bit_left; - uint8_t *buf, *buf_ptr, *buf_end; -#endif -} PutBitContext; - -static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) -{ - s->buf = buffer; - s->buf_end = s->buf + buffer_size; -#ifdef ALT_BITSTREAM_WRITER - s->index=0; - ((uint32_t*)(s->buf))[0]=0; -// memset(buffer, 0, buffer_size); -#else - s->buf_ptr = s->buf; - s->bit_left=32; - s->bit_buf=0; -#endif -} - -/* return the number of bits output */ -static inline int put_bits_count(PutBitContext *s) -{ -#ifdef ALT_BITSTREAM_WRITER - return s->index; -#else - return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left; -#endif -} - -/* pad the end of the output stream with zeros */ -static inline void flush_put_bits(PutBitContext *s) -{ -#ifdef ALT_BITSTREAM_WRITER - align_put_bits(s); -#else - s->bit_buf<<= s->bit_left; - while (s->bit_left < 32) { - /* XXX: should test end of buffer */ - *s->buf_ptr++=s->bit_buf >> 24; - s->bit_buf<<=8; - s->bit_left+=8; - } - s->bit_left=32; - s->bit_buf=0; -#endif -} - -void align_put_bits(PutBitContext *s); -void put_string(PutBitContext * pbc, char *s, int put_zero); - -/* bit input */ -/* buffer, buffer_end and size_in_bits must be present and used by every reader */ -typedef struct GetBitContext { - const uint8_t *buffer, *buffer_end; -#ifdef ALT_BITSTREAM_READER - int index; -#elif defined LIBMPEG2_BITSTREAM_READER - uint8_t *buffer_ptr; - uint32_t cache; - int bit_count; -#elif defined A32_BITSTREAM_READER - uint32_t *buffer_ptr; - uint32_t cache0; - uint32_t cache1; - int bit_count; -#endif - int size_in_bits; -} GetBitContext; - -#define VLC_TYPE int16_t - -typedef struct VLC { - int bits; - VLC_TYPE (*table)[2]; ///< code, bits - int table_size, table_allocated; -} VLC; - -typedef struct RL_VLC_ELEM { - int16_t level; - int8_t len; - uint8_t run; -} RL_VLC_ELEM; - -#ifdef ARCH_SPARC -#define UNALIGNED_STORES_ARE_BAD -#endif - -/* used to avoid missaligned exceptions on some archs (alpha, ...) */ -#ifdef ARCH_X86 -# define unaligned32(a) (*(uint32_t*)(a)) -#else -# ifdef __GNUC__ -static inline uint32_t unaligned32(const void *v) { - struct Unaligned { - uint32_t i; - } __attribute__((packed)); - - return ((const struct Unaligned *) v)->i; -} -# elif defined(__DECC) -static inline uint32_t unaligned32(const void *v) { - return *(const __unaligned uint32_t *) v; -} -# else -static inline uint32_t unaligned32(const void *v) { - return *(const uint32_t *) v; -} -# endif -#endif //!ARCH_X86 - -#ifndef ALT_BITSTREAM_WRITER -static inline void put_bits(PutBitContext *s, int n, unsigned int value) -{ - unsigned int bit_buf; - int bit_left; - -#ifdef STATS - st_out_bit_counts[st_current_index] += n; -#endif - // printf("put_bits=%d %x\n", n, value); - assert(n == 32 || value < (1U << n)); - - bit_buf = s->bit_buf; - bit_left = s->bit_left; - - // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); - /* XXX: optimize */ - if (n < bit_left) { - bit_buf = (bit_buf<> (n - bit_left); -#ifdef UNALIGNED_STORES_ARE_BAD - if (3 & (intptr_t) s->buf_ptr) { - s->buf_ptr[0] = bit_buf >> 24; - s->buf_ptr[1] = bit_buf >> 16; - s->buf_ptr[2] = bit_buf >> 8; - s->buf_ptr[3] = bit_buf ; - } else -#endif - *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); - //printf("bitbuf = %08x\n", bit_buf); - s->buf_ptr+=4; - bit_left+=32 - n; - bit_buf = value; - } - - s->bit_buf = bit_buf; - s->bit_left = bit_left; -} -#endif - - -#ifdef ALT_BITSTREAM_WRITER -static inline void put_bits(PutBitContext *s, int n, unsigned int value) -{ -# ifdef ALIGNED_BITSTREAM_WRITER -# ifdef ARCH_X86 - asm volatile( - "movl %0, %%ecx \n\t" - "xorl %%eax, %%eax \n\t" - "shrdl %%cl, %1, %%eax \n\t" - "shrl %%cl, %1 \n\t" - "movl %0, %%ecx \n\t" - "shrl $3, %%ecx \n\t" - "andl $0xFFFFFFFC, %%ecx \n\t" - "bswapl %1 \n\t" - "orl %1, (%2, %%ecx) \n\t" - "bswapl %%eax \n\t" - "addl %3, %0 \n\t" - "movl %%eax, 4(%2, %%ecx) \n\t" - : "=&r" (s->index), "=&r" (value) - : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n)) - : "%eax", "%ecx" - ); -# else - int index= s->index; - uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5); - - value<<= 32-n; - - ptr[0] |= be2me_32(value>>(index&31)); - ptr[1] = be2me_32(value<<(32-(index&31))); -//if(n>24) printf("%d %d\n", n, value); - index+= n; - s->index= index; -# endif -# else //ALIGNED_BITSTREAM_WRITER -# ifdef ARCH_X86 - asm volatile( - "movl $7, %%ecx \n\t" - "andl %0, %%ecx \n\t" - "addl %3, %%ecx \n\t" - "negl %%ecx \n\t" - "shll %%cl, %1 \n\t" - "bswapl %1 \n\t" - "movl %0, %%ecx \n\t" - "shrl $3, %%ecx \n\t" - "orl %1, (%%ecx, %2) \n\t" - "addl %3, %0 \n\t" - "movl $0, 4(%%ecx, %2) \n\t" - : "=&r" (s->index), "=&r" (value) - : "r" (s->buf), "r" (n), "0" (s->index), "1" (value) - : "%ecx" - ); -# else - int index= s->index; - uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3)); - - ptr[0] |= be2me_32(value<<(32-n-(index&7) )); - ptr[1] = 0; -//if(n>24) printf("%d %d\n", n, value); - index+= n; - s->index= index; -# endif -# endif //!ALIGNED_BITSTREAM_WRITER -} -#endif - - -static inline uint8_t* pbBufPtr(PutBitContext *s) -{ -#ifdef ALT_BITSTREAM_WRITER - return s->buf + (s->index>>3); -#else - return s->buf_ptr; -#endif -} - -/** - * - * PutBitContext must be flushed & aligned to a byte boundary before calling this. - */ -static inline void skip_put_bytes(PutBitContext *s, int n){ - assert((put_bits_count(s)&7)==0); -#ifdef ALT_BITSTREAM_WRITER - FIXME may need some cleaning of the buffer - s->index += n<<3; -#else - assert(s->bit_left==32); - s->buf_ptr += n; -#endif -} - -/** - * Changes the end of the buffer. - */ -static inline void set_put_bits_buffer_size(PutBitContext *s, int size){ - s->buf_end= s->buf + size; -} - -/* Bitstream reader API docs: -name - abritary name which is used as prefix for the internal variables - -gb - getbitcontext - -OPEN_READER(name, gb) - loads gb into local variables - -CLOSE_READER(name, gb) - stores local vars in gb - -UPDATE_CACHE(name, gb) - refills the internal cache from the bitstream - after this call at least MIN_CACHE_BITS will be available, - -GET_CACHE(name, gb) - will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) - -SHOW_UBITS(name, gb, num) - will return the nest num bits - -SHOW_SBITS(name, gb, num) - will return the nest num bits and do sign extension - -SKIP_BITS(name, gb, num) - will skip over the next num bits - note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER - -SKIP_CACHE(name, gb, num) - will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) - -SKIP_COUNTER(name, gb, num) - will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) - -LAST_SKIP_CACHE(name, gb, num) - will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing - -LAST_SKIP_BITS(name, gb, num) - is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER - -for examples see get_bits, show_bits, skip_bits, get_vlc -*/ - -static inline int unaligned32_be(const void *v) -{ -#ifdef CONFIG_ALIGN - const uint8_t *p=v; - return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]); -#else - return be2me_32( unaligned32(v)); //original -#endif -} - -#ifdef ALT_BITSTREAM_READER -# define MIN_CACHE_BITS 25 - -# define OPEN_READER(name, gb)\ - int name##_index= (gb)->index;\ - int name##_cache= 0;\ - -# define CLOSE_READER(name, gb)\ - (gb)->index= name##_index;\ - -# define UPDATE_CACHE(name, gb)\ - name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ - -# define SKIP_CACHE(name, gb, num)\ - name##_cache <<= (num);\ - -// FIXME name? -# define SKIP_COUNTER(name, gb, num)\ - name##_index += (num);\ - -# define SKIP_BITS(name, gb, num)\ - {\ - SKIP_CACHE(name, gb, num)\ - SKIP_COUNTER(name, gb, num)\ - }\ - -# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) -# define LAST_SKIP_CACHE(name, gb, num) ; - -# define SHOW_UBITS(name, gb, num)\ - NEG_USR32(name##_cache, num) - -# define SHOW_SBITS(name, gb, num)\ - NEG_SSR32(name##_cache, num) - -# define GET_CACHE(name, gb)\ - ((uint32_t)name##_cache) - -static inline int get_bits_count(GetBitContext *s){ - return s->index; -} -#elif defined LIBMPEG2_BITSTREAM_READER -//libmpeg2 like reader - -# define MIN_CACHE_BITS 17 - -# define OPEN_READER(name, gb)\ - int name##_bit_count=(gb)->bit_count;\ - int name##_cache= (gb)->cache;\ - uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\ - -# define CLOSE_READER(name, gb)\ - (gb)->bit_count= name##_bit_count;\ - (gb)->cache= name##_cache;\ - (gb)->buffer_ptr= name##_buffer_ptr;\ - -#ifdef LIBMPEG2_BITSTREAM_READER_HACK - -# define UPDATE_CACHE(name, gb)\ - if(name##_bit_count >= 0){\ - name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\ - ((uint16_t*)name##_buffer_ptr)++;\ - name##_bit_count-= 16;\ - }\ - -#else - -# define UPDATE_CACHE(name, gb)\ - if(name##_bit_count >= 0){\ - name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\ - name##_buffer_ptr+=2;\ - name##_bit_count-= 16;\ - }\ - -#endif - -# define SKIP_CACHE(name, gb, num)\ - name##_cache <<= (num);\ - -# define SKIP_COUNTER(name, gb, num)\ - name##_bit_count += (num);\ - -# define SKIP_BITS(name, gb, num)\ - {\ - SKIP_CACHE(name, gb, num)\ - SKIP_COUNTER(name, gb, num)\ - }\ - -# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) -# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) - -# define SHOW_UBITS(name, gb, num)\ - NEG_USR32(name##_cache, num) - -# define SHOW_SBITS(name, gb, num)\ - NEG_SSR32(name##_cache, num) - -# define GET_CACHE(name, gb)\ - ((uint32_t)name##_cache) - -static inline int get_bits_count(GetBitContext *s){ - return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; -} - -#elif defined A32_BITSTREAM_READER - -# define MIN_CACHE_BITS 32 - -# define OPEN_READER(name, gb)\ - int name##_bit_count=(gb)->bit_count;\ - uint32_t name##_cache0= (gb)->cache0;\ - uint32_t name##_cache1= (gb)->cache1;\ - uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ - -# define CLOSE_READER(name, gb)\ - (gb)->bit_count= name##_bit_count;\ - (gb)->cache0= name##_cache0;\ - (gb)->cache1= name##_cache1;\ - (gb)->buffer_ptr= name##_buffer_ptr;\ - -# define UPDATE_CACHE(name, gb)\ - if(name##_bit_count > 0){\ - const uint32_t next= be2me_32( *name##_buffer_ptr );\ - name##_cache0 |= NEG_USR32(next,name##_bit_count);\ - name##_cache1 |= next<buffer_ptr - s->buffer)*8 - 32 + s->bit_count; -} - -#endif - -/** - * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). - * if MSB not set it is negative - * @param n length in bits - * @author BERO - */ -static inline int get_xbits(GetBitContext *s, int n){ - register int tmp; - register int32_t cache; - OPEN_READER(re, s) - UPDATE_CACHE(re, s) - cache = GET_CACHE(re,s); - if ((int32_t)cache<0) { //MSB=1 - tmp = NEG_USR32(cache,n); - } else { - // tmp = (-1<index+=n for the ALT_READER :)) - OPEN_READER(re, s) - UPDATE_CACHE(re, s) - LAST_SKIP_BITS(re, s, n) - CLOSE_READER(re, s) -} - -static inline unsigned int get_bits1(GetBitContext *s){ -#ifdef ALT_BITSTREAM_READER - int index= s->index; - uint8_t result= s->buffer[ index>>3 ]; - result<<= (index&0x07); - result>>= 8 - 1; - index++; - s->index= index; - - return result; -#else - return get_bits(s, 1); -#endif -} - -static inline unsigned int show_bits1(GetBitContext *s){ - return show_bits(s, 1); -} - -static inline void skip_bits1(GetBitContext *s){ - skip_bits(s, 1); -} - -/** - * init GetBitContext. - * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits - * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end - * @param bit_size the size of the buffer in bits - */ -static inline void init_get_bits(GetBitContext *s, - const uint8_t *buffer, int bit_size) -{ - const int buffer_size= (bit_size+7)>>3; - - s->buffer= buffer; - s->size_in_bits= bit_size; - s->buffer_end= buffer + buffer_size; -#ifdef ALT_BITSTREAM_READER - s->index=0; -#elif defined LIBMPEG2_BITSTREAM_READER -#ifdef LIBMPEG2_BITSTREAM_READER_HACK - if ((int)buffer&1) { - /* word alignment */ - s->cache = (*buffer++)<<24; - s->buffer_ptr = buffer; - s->bit_count = 16-8; - } else -#endif - { - s->buffer_ptr = buffer; - s->bit_count = 16; - s->cache = 0; - } -#elif defined A32_BITSTREAM_READER - s->buffer_ptr = (uint32_t*)buffer; - s->bit_count = 32; - s->cache0 = 0; - s->cache1 = 0; -#endif - { - OPEN_READER(re, s) - UPDATE_CACHE(re, s) - UPDATE_CACHE(re, s) - CLOSE_READER(re, s) - } -#ifdef A32_BITSTREAM_READER - s->cache1 = 0; -#endif -} - -int check_marker(GetBitContext *s, const char *msg); -void align_get_bits(GetBitContext *s); -int init_vlc(VLC *vlc, int nb_bits, int nb_codes, - const void *bits, int bits_wrap, int bits_size, - const void *codes, int codes_wrap, int codes_size); -void free_vlc(VLC *vlc); - -/** - * - * if the vlc code is invalid and max_depth=1 than no bits will be removed - * if the vlc code is invalid and max_depth>1 than the number of bits removed - * is undefined - */ -#define GET_VLC(code, name, gb, table, bits, max_depth)\ -{\ - int n, index, nb_bits;\ -\ - index= SHOW_UBITS(name, gb, bits);\ - code = table[index][0];\ - n = table[index][1];\ -\ - if(max_depth > 1 && n < 0){\ - LAST_SKIP_BITS(name, gb, bits)\ - UPDATE_CACHE(name, gb)\ -\ - nb_bits = -n;\ -\ - index= SHOW_UBITS(name, gb, nb_bits) + code;\ - code = table[index][0];\ - n = table[index][1];\ - if(max_depth > 2 && n < 0){\ - LAST_SKIP_BITS(name, gb, nb_bits)\ - UPDATE_CACHE(name, gb)\ -\ - nb_bits = -n;\ -\ - index= SHOW_UBITS(name, gb, nb_bits) + code;\ - code = table[index][0];\ - n = table[index][1];\ - }\ - }\ - SKIP_BITS(name, gb, n)\ -} - -#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\ -{\ - int n, index, nb_bits;\ -\ - index= SHOW_UBITS(name, gb, bits);\ - level = table[index].level;\ - n = table[index].len;\ -\ - if(max_depth > 1 && n < 0){\ - LAST_SKIP_BITS(name, gb, bits)\ - UPDATE_CACHE(name, gb)\ -\ - nb_bits = -n;\ -\ - index= SHOW_UBITS(name, gb, nb_bits) + level;\ - level = table[index].level;\ - n = table[index].len;\ - }\ - run= table[index].run;\ - SKIP_BITS(name, gb, n)\ -} - -// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly -static inline int get_vlc(GetBitContext *s, VLC *vlc) -{ - int code; - VLC_TYPE (*table)[2]= vlc->table; - - OPEN_READER(re, s) - UPDATE_CACHE(re, s) - - GET_VLC(code, re, s, table, vlc->bits, 3) - - CLOSE_READER(re, s) - return code; -} - -/** - * parses a vlc code, faster then get_vlc() - * @param bits is the number of bits which will be read at once, must be - * identical to nb_bits in init_vlc() - * @param max_depth is the number of times bits bits must be readed to completly - * read the longest vlc code - * = (max_vlc_length + bits - 1) / bits - */ -static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], - int bits, int max_depth) -{ - int code; - - OPEN_READER(re, s) - UPDATE_CACHE(re, s) - - GET_VLC(code, re, s, table, bits, max_depth) - - CLOSE_READER(re, s) - return code; -} - -//#define TRACE - -#ifdef TRACE - -static inline void print_bin(int bits, int n){ - int i; - - for(i=n-1; i>=0; i--){ - printf("%d", (bits>>i)&1); - } - for(i=n; i<24; i++) - printf(" "); -} - -static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){ - int r= get_bits(s, n); - - print_bin(r, n); - printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line); - return r; -} -static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){ - int show= show_bits(s, 24); - int pos= get_bits_count(s); - int r= get_vlc2(s, table, bits, max_depth); - int len= get_bits_count(s) - pos; - int bits2= show>>(24-len); - - print_bin(bits2, len); - - printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line); - return r; -} -static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){ - int show= show_bits(s, n); - int r= get_xbits(s, n); - - print_bin(show, n); - printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line); - return r; -} - -#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) -#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__) -#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) -#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) -#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) - -#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__) - -#else //TRACE -#define tprintf(...) {} -#endif - /* define it to include statistics code (useful only for optimizing codec efficiency */ //#define STATS @@ -1220,7 +429,7 @@ static inline int ff_get_fourcc(const char *s){ #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24)) -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_X86_64) #define MASK_ABS(mask, level)\ asm volatile(\ "cdq \n\t"\ @@ -1254,8 +463,18 @@ if((y)<(x)){\ } #endif -#ifdef ARCH_X86 -static inline long long rdtsc() +#if defined(ARCH_X86) || defined(ARCH_X86_64) +#if defined(ARCH_X86_64) +static inline uint64_t rdtsc(void) +{ + uint64_t a, d; + asm volatile( "rdtsc\n\t" + : "=a" (a), "=d" (d) + ); + return (d << 32) | (a & 0xffffffff); +} +#else +static inline long long rdtsc(void) { long long l; asm volatile( "rdtsc\n\t" @@ -1263,6 +482,7 @@ static inline long long rdtsc() ); return l; } +#endif #define START_TIMER \ uint64_t tend;\ @@ -1283,6 +503,9 @@ tend= rdtsc();\ av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\ }\ } +#else +#define START_TIMER +#define STOP_TIMER(id) {} #endif #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d) @@ -1294,6 +517,8 @@ tend= rdtsc();\ #define time time_is_forbidden_due_to_security_issues #define rand rand_is_forbidden_due_to_state_trashing #define srand srand_is_forbidden_due_to_state_trashing +#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf +#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H)) #define printf please_use_av_log #define fprintf please_use_av_log diff --git a/src/libffmpeg/libavcodec/dpcm.c b/src/libffmpeg/libavcodec/dpcm.c index b80604e5f..487203ae9 100644 --- a/src/libffmpeg/libavcodec/dpcm.c +++ b/src/libffmpeg/libavcodec/dpcm.c @@ -24,6 +24,7 @@ * Xan DPCM decoder by Mario Brito (mbrito@student.dei.uc.pt) * for more information on the specific data formats, visit: * http://www.pcisys.net/~melanson/codecs/simpleaudio.html + * SOL DPCMs implemented by Konstantin Shishkov * * Note about using the Xan DPCM decoder: Xan DPCM is used in AVI files * found in the Wing Commander IV computer game. These AVI files contain @@ -39,6 +40,8 @@ typedef struct DPCMContext { int channels; short roq_square_array[256]; + long sample[2];//for SOL_DPCM + int *sol_table;//for SOL_DPCM } DPCMContext; #define SATURATE_S16(x) if (x < -32768) x = -32768; \ @@ -81,6 +84,32 @@ static int interplay_delta_table[] = { }; +static int sol_table_old[16] = + { 0x0, 0x1, 0x2 , 0x3, 0x6, 0xA, 0xF, 0x15, + -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0}; + +static int sol_table_new[16] = + { 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, + 0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15}; + +static int sol_table_16[128] = { + 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080, + 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120, + 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0, + 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230, + 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280, + 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0, + 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320, + 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370, + 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0, + 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480, + 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700, + 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00, + 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000 +}; + + + static int dpcm_decode_init(AVCodecContext *avctx) { DPCMContext *s = avctx->priv_data; @@ -88,6 +117,7 @@ static int dpcm_decode_init(AVCodecContext *avctx) short square; s->channels = avctx->channels; + s->sample[0] = s->sample[1] = 0; switch(avctx->codec->id) { @@ -100,6 +130,26 @@ static int dpcm_decode_init(AVCodecContext *avctx) } break; + + case CODEC_ID_SOL_DPCM: + switch(avctx->codec_tag){ + case 1: + s->sol_table=sol_table_old; + s->sample[0] = s->sample[1] = 0x80; + break; + case 2: + s->sol_table=sol_table_new; + s->sample[0] = s->sample[1] = 0x80; + break; + case 3: + s->sol_table=sol_table_16; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Unknown SOL subcodec\n"); + return -1; + } + break; + default: break; } @@ -203,6 +253,38 @@ static int dpcm_decode_frame(AVCodecContext *avctx, channel_number ^= s->channels - 1; } break; + case CODEC_ID_SOL_DPCM: + in = 0; + if (avctx->codec_tag != 3) { + while (in < buf_size) { + int n1, n2; + n1 = (buf[in] >> 4) & 0xF; + n2 = buf[in++] & 0xF; + s->sample[0] += s->sol_table[n1]; + if (s->sample[0] < 0) s->sample[0] = 0; + if (s->sample[0] > 255) s->sample[0] = 255; + output_samples[out++] = (s->sample[0] - 128) << 8; + s->sample[s->channels - 1] += s->sol_table[n2]; + if (s->sample[s->channels - 1] < 0) s->sample[s->channels - 1] = 0; + if (s->sample[s->channels - 1] > 255) s->sample[s->channels - 1] = 255; + output_samples[out++] = (s->sample[s->channels - 1] - 128) << 8; + } + } else { + while (in < buf_size) { + int n; + n = buf[in++]; + if (n & 0x80) s->sample[channel_number] -= s->sol_table[n & 0x7F]; + else s->sample[channel_number] += s->sol_table[n & 0x7F]; + SATURATE_S16(s->sample[channel_number]); + output_samples[out++] = s->sample[channel_number]; + /* toggle channel */ + channel_number ^= s->channels - 1; + } + } + break; + + default: + break; } *data_size = out * sizeof(short); @@ -241,3 +323,14 @@ AVCodec xan_dpcm_decoder = { NULL, dpcm_decode_frame, }; + +AVCodec sol_dpcm_decoder = { + "sol_dpcm", + CODEC_TYPE_AUDIO, + CODEC_ID_SOL_DPCM, + sizeof(DPCMContext), + dpcm_decode_init, + NULL, + NULL, + dpcm_decode_frame, +}; diff --git a/src/libffmpeg/libavcodec/dsputil.c b/src/libffmpeg/libavcodec/dsputil.c index b1252251a..926832ff1 100644 --- a/src/libffmpeg/libavcodec/dsputil.c +++ b/src/libffmpeg/libavcodec/dsputil.c @@ -31,8 +31,11 @@ #include "simple_idct.h" #include "faandct.h" -uint8_t cropTbl[256 + 2 * MAX_NEG_CROP]; -uint32_t squareTbl[512]; +/* snow.c */ +void ff_spatial_dwt(int *buffer, int width, int height, int stride, int type, int decomposition_count); + +uint8_t cropTbl[256 + 2 * MAX_NEG_CROP] = {0, }; +uint32_t squareTbl[512] = {0, }; const uint8_t ff_zigzag_direct[64] = { 0, 1, 8, 16, 9, 2, 3, 10, @@ -59,7 +62,7 @@ const uint8_t ff_zigzag248_direct[64] = { }; /* not permutated inverse zigzag_direct + 1 for MMX quantizer */ -uint16_t __align8 inv_zigzag_direct16[64]; +uint16_t __align8 inv_zigzag_direct16[64] = {0, }; const uint8_t ff_alternate_horizontal_scan[64] = { 0, 1, 2, 3, 8, 9, 16, 17, @@ -219,6 +222,23 @@ static void bswap_buf(uint32_t *dst, uint32_t *src, int w){ } } +static int sse4_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) +{ + int s, i; + uint32_t *sq = squareTbl + 256; + + s = 0; + for (i = 0; i < h; i++) { + s += sq[pix1[0] - pix2[0]]; + s += sq[pix1[1] - pix2[1]]; + s += sq[pix1[2] - pix2[2]]; + s += sq[pix1[3] - pix2[3]]; + pix1 += line_size; + pix2 += line_size; + } + return s; +} + static int sse8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) { int s, i; @@ -270,6 +290,103 @@ static int sse16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) return s; } + +static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){ + int s, i, j; + const int dec_count= w==8 ? 3 : 4; + int tmp[16*16]; +#if 0 + int level, ori; + static const int scale[2][2][4][4]={ + { + { + //8x8 dec=3 + {268, 239, 239, 213}, + { 0, 224, 224, 152}, + { 0, 135, 135, 110}, + },{ + //16x16 dec=4 + {344, 310, 310, 280}, + { 0, 320, 320, 228}, + { 0, 175, 175, 136}, + { 0, 129, 129, 102}, + } + },{ + {//FIXME 5/3 + //8x8 dec=3 + {275, 245, 245, 218}, + { 0, 230, 230, 156}, + { 0, 138, 138, 113}, + },{ + //16x16 dec=4 + {352, 317, 317, 286}, + { 0, 328, 328, 233}, + { 0, 180, 180, 140}, + { 0, 132, 132, 105}, + } + } + }; +#endif + + for (i = 0; i < h; i++) { + for (j = 0; j < w; j+=4) { + tmp[16*i+j+0] = (pix1[j+0] - pix2[j+0])<<4; + tmp[16*i+j+1] = (pix1[j+1] - pix2[j+1])<<4; + tmp[16*i+j+2] = (pix1[j+2] - pix2[j+2])<<4; + tmp[16*i+j+3] = (pix1[j+3] - pix2[j+3])<<4; + } + pix1 += line_size; + pix2 += line_size; + } + ff_spatial_dwt(tmp, w, h, 16, type, dec_count); + + s=0; +#if 0 + for(level=0; level>1 : 0; + int size= 1<=0); + + return s>>2; +} + +static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){ + return w_c(v, pix1, pix2, line_size, 8, h, 1); +} + +static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){ + return w_c(v, pix1, pix2, line_size, 8, h, 0); +} + +static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){ + return w_c(v, pix1, pix2, line_size, 16, h, 1); +} + +static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){ + return w_c(v, pix1, pix2, line_size, 16, h, 0); +} + static void get_pixels_c(DCTELEM *restrict block, const uint8_t *pixels, int line_size) { int i; @@ -332,6 +449,40 @@ static void put_pixels_clamped_c(const DCTELEM *block, uint8_t *restrict pixels, } } +static void put_pixels_clamped4_c(const DCTELEM *block, uint8_t *restrict pixels, + int line_size) +{ + int i; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + /* read the pixels */ + for(i=0;i<4;i++) { + pixels[0] = cm[block[0]]; + pixels[1] = cm[block[1]]; + pixels[2] = cm[block[2]]; + pixels[3] = cm[block[3]]; + + pixels += line_size; + block += 8; + } +} + +static void put_pixels_clamped2_c(const DCTELEM *block, uint8_t *restrict pixels, + int line_size) +{ + int i; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + /* read the pixels */ + for(i=0;i<2;i++) { + pixels[0] = cm[block[0]]; + pixels[1] = cm[block[1]]; + + pixels += line_size; + block += 8; + } +} + static void put_signed_pixels_clamped_c(const DCTELEM *block, uint8_t *restrict pixels, int line_size) @@ -373,6 +524,38 @@ static void add_pixels_clamped_c(const DCTELEM *block, uint8_t *restrict pixels, block += 8; } } + +static void add_pixels_clamped4_c(const DCTELEM *block, uint8_t *restrict pixels, + int line_size) +{ + int i; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + /* read the pixels */ + for(i=0;i<4;i++) { + pixels[0] = cm[pixels[0] + block[0]]; + pixels[1] = cm[pixels[1] + block[1]]; + pixels[2] = cm[pixels[2] + block[2]]; + pixels[3] = cm[pixels[3] + block[3]]; + pixels += line_size; + block += 8; + } +} + +static void add_pixels_clamped2_c(const DCTELEM *block, uint8_t *restrict pixels, + int line_size) +{ + int i; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + /* read the pixels */ + for(i=0;i<2;i++) { + pixels[0] = cm[pixels[0] + block[0]]; + pixels[1] = cm[pixels[1] + block[1]]; + pixels += line_size; + block += 8; + } +} #if 0 #define PIXOP2(OPNAME, OP) \ @@ -2031,7 +2214,6 @@ static void OPNAME ## h264_qpel16_hv_lowpass(uint8_t *dst, int16_t *tmp, uint8_t OPNAME ## h264_qpel8_hv_lowpass(dst , tmp , src , dstStride, tmpStride, srcStride);\ OPNAME ## h264_qpel8_hv_lowpass(dst+8, tmp+8, src+8, dstStride, tmpStride, srcStride);\ src += 8*srcStride;\ - tmp += 8*tmpStride;\ dst += 8*dstStride;\ OPNAME ## h264_qpel8_hv_lowpass(dst , tmp , src , dstStride, tmpStride, srcStride);\ OPNAME ## h264_qpel8_hv_lowpass(dst+8, tmp+8, src+8, dstStride, tmpStride, srcStride);\ @@ -2195,6 +2377,77 @@ H264_MC(avg_, 16) #undef op2_put #endif +#define op_scale1(x) block[x] = clip_uint8( (block[x]*weight + offset) >> log2_denom ) +#define op_scale2(x) dst[x] = clip_uint8( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1)) +#define H264_WEIGHT(W,H) \ +static void weight_h264_pixels ## W ## x ## H ## _c(uint8_t *block, int stride, int log2_denom, int weight, int offset){ \ + int attribute_unused x, y; \ + offset <<= log2_denom; \ + if(log2_denom) offset += 1<<(log2_denom-1); \ + for(y=0; y> 1; \ + offset = ((offset << 1) + 1) << log2_denom; \ + for(y=0; y>2; + src[7+y*stride] = (temp[7+y*8] + 2)>>2; + for(x=1; x<7; x++){ + xy = y * stride + x; + yz = y * 8 + x; + src[xy] = (temp[yz-1] + 2*temp[yz] + temp[yz+1] + 8)>>4; + } + } +} + static inline int pix_abs16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) { int s, i; @@ -2560,6 +2840,56 @@ static int pix_abs8_xy2_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, return s; } +static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h){ + int score1=0; + int score2=0; + int x,y; + + for(y=0; yavctx->nsse_weight; + else return score1 + ABS(score2)*8; +} + +static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h){ + int score1=0; + int score2=0; + int x,y; + + for(y=0; yavctx->nsse_weight; + else return score1 + ABS(score2)*8; +} + static int try_8x8basis_c(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){ int i; unsigned int sum=0; @@ -2635,6 +2965,9 @@ void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type){ case FF_CMP_DCT: cmp[i]= c->dct_sad[i]; break; + case FF_CMP_DCTMAX: + cmp[i]= c->dct_max[i]; + break; case FF_CMP_PSNR: cmp[i]= c->quant_psnr[i]; break; @@ -2653,6 +2986,15 @@ void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type){ case FF_CMP_ZERO: cmp[i]= zero_cmp; break; + case FF_CMP_NSSE: + cmp[i]= c->nsse[i]; + break; + case FF_CMP_W53: + cmp[i]= c->w53[i]; + break; + case FF_CMP_W97: + cmp[i]= c->w97[i]; + break; default: av_log(NULL, AV_LOG_ERROR,"internal error in cmp function selection\n"); } @@ -2849,6 +3191,23 @@ static int dct_sad8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2 return sum; } +static int dct_max8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ + MpegEncContext * const s= (MpegEncContext *)c; + uint64_t __align8 aligned_temp[sizeof(DCTELEM)*64/8]; + DCTELEM * const temp= (DCTELEM*)aligned_temp; + int sum=0, i; + + assert(h==8); + + s->dsp.diff_pixels(temp, src1, src2, stride); + s->dsp.fdct(temp); + + for(i=0; i<64; i++) + sum= FFMAX(sum, ABS(temp[i])); + + return sum; +} + void simple_idct(DCTELEM *block); //FIXME static int quant_psnr8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ @@ -3078,6 +3437,7 @@ static int vsse16_c(/*MpegEncContext*/ void *c, uint8_t *s1, uint8_t *s2, int st WARPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c) WARPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c) WARPER8_16_SQ(dct_sad8x8_c, dct_sad16_c) +WARPER8_16_SQ(dct_max8x8_c, dct_max16_c) WARPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c) WARPER8_16_SQ(rd8x8_c, rd16_c) WARPER8_16_SQ(bit8x8_c, bit16_c) @@ -3095,6 +3455,41 @@ static void ff_jref_idct_add(uint8_t *dest, int line_size, DCTELEM *block) add_pixels_clamped_c(block, dest, line_size); } +static void ff_jref_idct4_put(uint8_t *dest, int line_size, DCTELEM *block) +{ + j_rev_dct4 (block); + put_pixels_clamped4_c(block, dest, line_size); +} +static void ff_jref_idct4_add(uint8_t *dest, int line_size, DCTELEM *block) +{ + j_rev_dct4 (block); + add_pixels_clamped4_c(block, dest, line_size); +} + +static void ff_jref_idct2_put(uint8_t *dest, int line_size, DCTELEM *block) +{ + j_rev_dct2 (block); + put_pixels_clamped2_c(block, dest, line_size); +} +static void ff_jref_idct2_add(uint8_t *dest, int line_size, DCTELEM *block) +{ + j_rev_dct2 (block); + add_pixels_clamped2_c(block, dest, line_size); +} + +static void ff_jref_idct1_put(uint8_t *dest, int line_size, DCTELEM *block) +{ + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + dest[0] = cm[(block[0] + 4)>>3]; +} +static void ff_jref_idct1_add(uint8_t *dest, int line_size, DCTELEM *block) +{ + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + dest[0] = cm[dest[0] + ((block[0] + 4)>>3)]; +} + /* init static data */ void dsputil_static_init(void) { @@ -3133,18 +3528,42 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) } #endif //CONFIG_ENCODERS - if(avctx->idct_algo==FF_IDCT_INT){ - c->idct_put= ff_jref_idct_put; - c->idct_add= ff_jref_idct_add; - c->idct = j_rev_dct; - c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM; - }else{ //accurate/default - c->idct_put= simple_idct_put; - c->idct_add= simple_idct_add; - c->idct = simple_idct; + if(avctx->lowres==1){ + if(avctx->idct_algo==FF_IDCT_INT || avctx->idct_algo==FF_IDCT_AUTO){ + c->idct_put= ff_jref_idct4_put; + c->idct_add= ff_jref_idct4_add; + }else{ + c->idct_put= ff_h264_lowres_idct_put_c; + c->idct_add= ff_h264_lowres_idct_add_c; + } + c->idct = j_rev_dct4; + c->idct_permutation_type= FF_NO_IDCT_PERM; + }else if(avctx->lowres==2){ + c->idct_put= ff_jref_idct2_put; + c->idct_add= ff_jref_idct2_add; + c->idct = j_rev_dct2; + c->idct_permutation_type= FF_NO_IDCT_PERM; + }else if(avctx->lowres==3){ + c->idct_put= ff_jref_idct1_put; + c->idct_add= ff_jref_idct1_add; + c->idct = j_rev_dct1; c->idct_permutation_type= FF_NO_IDCT_PERM; + }else{ + if(avctx->idct_algo==FF_IDCT_INT){ + c->idct_put= ff_jref_idct_put; + c->idct_add= ff_jref_idct_add; + c->idct = j_rev_dct; + c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM; + }else{ //accurate/default + c->idct_put= simple_idct_put; + c->idct_add= simple_idct_add; + c->idct = simple_idct; + c->idct_permutation_type= FF_NO_IDCT_PERM; + } } + c->h264_idct_add= ff_h264_idct_add_c; + /* VP3 DSP support */ c->vp3_dsp_init = vp3_dsp_init_c; c->vp3_idct = vp3_idct_c; @@ -3259,6 +3678,27 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_c; c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_c; + c->weight_h264_pixels_tab[0]= weight_h264_pixels16x16_c; + c->weight_h264_pixels_tab[1]= weight_h264_pixels16x8_c; + c->weight_h264_pixels_tab[2]= weight_h264_pixels8x16_c; + c->weight_h264_pixels_tab[3]= weight_h264_pixels8x8_c; + c->weight_h264_pixels_tab[4]= weight_h264_pixels8x4_c; + c->weight_h264_pixels_tab[5]= weight_h264_pixels4x8_c; + c->weight_h264_pixels_tab[6]= weight_h264_pixels4x4_c; + c->weight_h264_pixels_tab[7]= weight_h264_pixels4x2_c; + c->weight_h264_pixels_tab[8]= weight_h264_pixels2x4_c; + c->weight_h264_pixels_tab[9]= weight_h264_pixels2x2_c; + c->biweight_h264_pixels_tab[0]= biweight_h264_pixels16x16_c; + c->biweight_h264_pixels_tab[1]= biweight_h264_pixels16x8_c; + c->biweight_h264_pixels_tab[2]= biweight_h264_pixels8x16_c; + c->biweight_h264_pixels_tab[3]= biweight_h264_pixels8x8_c; + c->biweight_h264_pixels_tab[4]= biweight_h264_pixels8x4_c; + c->biweight_h264_pixels_tab[5]= biweight_h264_pixels4x8_c; + c->biweight_h264_pixels_tab[6]= biweight_h264_pixels4x4_c; + c->biweight_h264_pixels_tab[7]= biweight_h264_pixels4x2_c; + c->biweight_h264_pixels_tab[8]= biweight_h264_pixels2x4_c; + c->biweight_h264_pixels_tab[9]= biweight_h264_pixels2x2_c; + c->put_mspel_pixels_tab[0]= put_mspel8_mc00_c; c->put_mspel_pixels_tab[1]= put_mspel8_mc10_c; c->put_mspel_pixels_tab[2]= put_mspel8_mc20_c; @@ -3275,10 +3715,12 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) SET_CMP_FUNC(hadamard8_diff) c->hadamard8_diff[4]= hadamard8_intra16_c; SET_CMP_FUNC(dct_sad) + SET_CMP_FUNC(dct_max) c->sad[0]= pix_abs16_c; c->sad[1]= pix_abs8_c; c->sse[0]= sse16_c; c->sse[1]= sse8_c; + c->sse[2]= sse4_c; SET_CMP_FUNC(quant_psnr) SET_CMP_FUNC(rd) SET_CMP_FUNC(bit) @@ -3286,7 +3728,13 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) c->vsad[4]= vsad_intra16_c; c->vsse[0]= vsse16_c; c->vsse[4]= vsse_intra16_c; - + c->nsse[0]= nsse16_c; + c->nsse[1]= nsse8_c; + c->w53[0]= w53_16_c; + c->w53[1]= w53_8_c; + c->w97[0]= w97_16_c; + c->w97[1]= w97_8_c; + c->add_bytes= add_bytes_c; c->diff_bytes= diff_bytes_c; c->sub_hfyu_median_prediction= sub_hfyu_median_prediction_c; @@ -3295,6 +3743,8 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) c->h263_h_loop_filter= h263_h_loop_filter_c; c->h263_v_loop_filter= h263_v_loop_filter_c; + c->h261_loop_filter= h261_loop_filter_c; + c->try_8x8basis= try_8x8basis_c; c->add_8x8basis= add_8x8basis_c; diff --git a/src/libffmpeg/libavcodec/dsputil.h b/src/libffmpeg/libavcodec/dsputil.h index adb4679e0..c728a24d6 100644 --- a/src/libffmpeg/libavcodec/dsputil.h +++ b/src/libffmpeg/libavcodec/dsputil.h @@ -45,11 +45,18 @@ void ff_jpeg_fdct_islow (DCTELEM *data); void ff_fdct248_islow (DCTELEM *data); void j_rev_dct (DCTELEM *data); +void j_rev_dct4 (DCTELEM *data); +void j_rev_dct2 (DCTELEM *data); +void j_rev_dct1 (DCTELEM *data); void ff_fdct_mmx(DCTELEM *block); void ff_fdct_mmx2(DCTELEM *block); void ff_fdct_sse2(DCTELEM *block); +void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride); +void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block); +void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block); + /* encoding scans */ extern const uint8_t ff_alternate_horizontal_scan[64]; extern const uint8_t ff_alternate_vertical_scan[64]; @@ -57,7 +64,7 @@ extern const uint8_t ff_zigzag_direct[64]; extern const uint8_t ff_zigzag248_direct[64]; /* pixel operations */ -#define MAX_NEG_CROP 384 +#define MAX_NEG_CROP 1024 /* temporary */ extern uint32_t squareTbl[512]; @@ -101,6 +108,8 @@ typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const ui typedef void (*tpel_mc_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int w, int h); typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride); typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y); +typedef void (*h264_weight_func)(uint8_t *block, int stride, int log2_denom, int weight, int offset); +typedef void (*h264_biweight_func)(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offsetd, int offsets); #define DEF_OLD_QPEL(name)\ void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\ @@ -165,12 +174,17 @@ typedef struct DSPContext { me_cmp_func rd[5]; me_cmp_func vsad[5]; me_cmp_func vsse[5]; + me_cmp_func nsse[5]; + me_cmp_func w53[5]; + me_cmp_func w97[5]; + me_cmp_func dct_max[5]; me_cmp_func me_pre_cmp[5]; me_cmp_func me_cmp[5]; me_cmp_func me_sub_cmp[5]; me_cmp_func mb_cmp[5]; me_cmp_func ildct_cmp[5]; //only width 16 used + me_cmp_func frame_skip_cmp[5]; //only width 8 used /** * Halfpel motion compensation with rounding (a+b+1)>>1. @@ -206,7 +220,7 @@ typedef struct DSPContext { * @param line_size number of bytes in a horizontal line of block * @param h height */ - op_pixels_func put_no_rnd_pixels_tab[2][4]; + op_pixels_func put_no_rnd_pixels_tab[4][4]; /** * Halfpel motion compensation with no rounding (a+b)>>1. @@ -218,7 +232,7 @@ typedef struct DSPContext { * @param line_size number of bytes in a horizontal line of block * @param h height */ - op_pixels_func avg_no_rnd_pixels_tab[2][4]; + op_pixels_func avg_no_rnd_pixels_tab[4][4]; void (*put_no_rnd_pixels_l2[2])(uint8_t *block/*align width (8 or 16)*/, const uint8_t *a/*align 1*/, const uint8_t *b/*align 1*/, int line_size, int h); @@ -249,6 +263,9 @@ typedef struct DSPContext { qpel_mc_func put_h264_qpel_pixels_tab[3][16]; qpel_mc_func avg_h264_qpel_pixels_tab[3][16]; + h264_weight_func weight_h264_pixels_tab[10]; + h264_biweight_func biweight_h264_pixels_tab[10]; + me_cmp_func pix_abs[2][4]; /* huffyuv specific */ @@ -264,6 +281,8 @@ typedef struct DSPContext { void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale); void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale); + void (*h261_loop_filter)(uint8_t *src, int stride); + /* (I)DCT */ void (*fdct)(DCTELEM *block/* align 16*/); void (*fdct248)(DCTELEM *block/* align 16*/); @@ -325,7 +344,8 @@ typedef struct DSPContext { */ void (*vp3_idct)(int16_t *input_data, int16_t *dequant_matrix, int coeff_count, DCTELEM *output_samples); - + + void (*h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride); } DSPContext; void dsputil_static_init(void); @@ -351,6 +371,29 @@ static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b) return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1); } +static inline int get_penalty_factor(int lambda, int lambda2, int type){ + switch(type&0xFF){ + default: + case FF_CMP_SAD: + return lambda>>FF_LAMBDA_SHIFT; + case FF_CMP_DCT: + return (3*lambda)>>(FF_LAMBDA_SHIFT+1); + case FF_CMP_W53: + return (4*lambda)>>(FF_LAMBDA_SHIFT); + case FF_CMP_W97: + return (2*lambda)>>(FF_LAMBDA_SHIFT); + case FF_CMP_SATD: + return (2*lambda)>>FF_LAMBDA_SHIFT; + case FF_CMP_RD: + case FF_CMP_PSNR: + case FF_CMP_SSE: + case FF_CMP_NSSE: + return lambda2>>FF_LAMBDA_SHIFT; + case FF_CMP_BIT: + return 1; + } +} + /** * Empty mmx state. * this must be called between any dsp function and float/double code. @@ -373,6 +416,7 @@ int mm_support(void); #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */ #define MM_SSE 0x0008 /* SSE functions */ #define MM_SSE2 0x0010 /* PIV SSE2 functions */ +#define MM_3DNOWEXT 0x0020 /* AMD 3DNowExt */ extern int mm_flags; @@ -393,6 +437,7 @@ static inline void emms(void) } #define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx); void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx); @@ -402,6 +447,7 @@ void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx); /* This is to use 4 bytes read to the IDCT pointers for some 'zero' line optimizations */ #define __align8 __attribute__ ((aligned (4))) +#define STRIDE_ALIGN 4 void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx); @@ -409,6 +455,7 @@ void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx); /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */ #define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx); @@ -416,11 +463,13 @@ void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx); /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */ #define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx); #elif defined(ARCH_ALPHA) #define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx); @@ -437,24 +486,28 @@ extern int mm_flags; #endif #define __align8 __attribute__ ((aligned (16))) +#define STRIDE_ALIGN 16 void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx); #elif defined(HAVE_MMI) #define __align8 __attribute__ ((aligned (16))) +#define STRIDE_ALIGN 16 void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx); #elif defined(ARCH_SH4) #define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx); #else -#define __align8 +#define __align8 __attribute__ ((aligned (8))) +#define STRIDE_ALIGN 8 #endif @@ -558,11 +611,20 @@ static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int st /* XXX: add ISOC specific test to avoid specific BSD testing. */ /* better than nothing implementation. */ /* btw, rintf() is existing on fbsd too -- alex */ -static inline long int lrintf(float x) +static always_inline long int lrintf(float x) { #ifdef CONFIG_WIN32 +# ifdef ARCH_X86 + int32_t i; + asm volatile( + "fistpl %0\n\t" + : "=m" (i) : "t" (x) : "st" + ); + return i; +# else /* XXX: incorrect, but make it compile */ - return (int)(x); + return (int)(x + (x < 0 ? -0.5 : 0.5)); +# endif #else return (int)(rint(x)); #endif diff --git a/src/libffmpeg/libavcodec/dv.c b/src/libffmpeg/libavcodec/dv.c index 8e359e361..94440ed5c 100644 --- a/src/libffmpeg/libavcodec/dv.c +++ b/src/libffmpeg/libavcodec/dv.c @@ -144,7 +144,7 @@ static int dvvideo_init(AVCodecContext *avctx) /* NOTE: as a trick, we use the fact the no codes are unused to accelerate the parsing of partial codes */ init_vlc(&dv_vlc, TEX_VLC_BITS, j, - new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2); + new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0); dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); if (!dv_rl_vlc) { @@ -263,6 +263,7 @@ static const int mb_area_start[5] = { 1, 6, 21, 43, 64 }; #ifndef ALT_BITSTREAM_READER #warning only works with ALT_BITSTREAM_READER +static int re_index; //Hack to make it compile #endif static inline int get_bits_left(GetBitContext *s) @@ -394,8 +395,7 @@ static inline void dv_decode_video_segment(DVVideoContext *s, init_get_bits(&gb, buf_ptr, last_index); /* get the dc */ - dc = get_bits(&gb, 9); - dc = (dc << (32 - 9)) >> (32 - 9); + dc = get_sbits(&gb, 9); dct_mode = get_bits1(&gb); mb->dct_mode = dct_mode; mb->scan_table = s->dv_zigzag[dct_mode]; @@ -889,10 +889,6 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, { DVVideoContext *s = avctx->priv_data; - /* special case for last picture */ - if(buf_size==0) - return 0; - s->sys = dv_frame_profile(buf); if (!s->sys || buf_size < s->sys->frame_size) return -1; /* NOTE: we only accept several full frames */ @@ -932,7 +928,9 @@ static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, s->sys = dv_codec_profile(c); if (!s->sys) return -1; - + if(buf_size < s->sys->frame_size) + return -1; + c->pix_fmt = s->sys->pix_fmt; s->picture = *((AVFrame *)data); diff --git a/src/libffmpeg/libavcodec/dvdata.h b/src/libffmpeg/libavcodec/dvdata.h index e60d99448..acda751d6 100644 --- a/src/libffmpeg/libavcodec/dvdata.h +++ b/src/libffmpeg/libavcodec/dvdata.h @@ -1299,7 +1299,7 @@ static const DVprofile dv_profiles[] = { .frame_rate_base = 1001, .height = 480, .width = 720, - .sar = {{72, 79}, {96, 79}}, + .sar = {{10, 11}, {40, 33}}, .video_place = dv_place_411, .pix_fmt = PIX_FMT_YUV411P, .audio_stride = 90, @@ -1315,7 +1315,7 @@ static const DVprofile dv_profiles[] = { .ltc_divisor = 25, .height = 576, .width = 720, - .sar = {{128, 117}, {512, 351}}, + .sar = {{59, 54}, {118, 81}}, .video_place = dv_place_420, .pix_fmt = PIX_FMT_YUV420P, .audio_stride = 108, @@ -1331,7 +1331,7 @@ static const DVprofile dv_profiles[] = { .ltc_divisor = 25, .height = 576, .width = 720, - .sar = {{128, 117}, {512, 351}}, + .sar = {{59, 54}, {118, 81}}, .video_place = dv_place_411P, .pix_fmt = PIX_FMT_YUV411P, .audio_stride = 108, diff --git a/src/libffmpeg/libavcodec/error_resilience.c b/src/libffmpeg/libavcodec/error_resilience.c index b7aeebddf..b0d22ddf9 100644 --- a/src/libffmpeg/libavcodec/error_resilience.c +++ b/src/libffmpeg/libavcodec/error_resilience.c @@ -652,7 +652,7 @@ void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int en s->error_status_table[start_xy] |= VP_START; - if(start_xy > 0 && s->avctx->thread_count <= 1){ + if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){ int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ]; prev_status &= ~ VP_START; @@ -661,31 +661,34 @@ void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int en } void ff_er_frame_end(MpegEncContext *s){ - int i, mb_x, mb_y, error, error_type; + int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error; int distance; int threshold_part[4]= {100,100,100}; int threshold= 50; int is_intra_likely; + int size = s->b8_stride * 2 * s->mb_height; + Picture *pic= s->current_picture_ptr; - if(!s->error_resilience || s->error_count==0) return; + if(!s->error_resilience || s->error_count==0 || + s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return; - av_log(s->avctx, AV_LOG_INFO, "concealing errors\n"); - if(s->current_picture.motion_val[0] == NULL){ - int size = s->b8_stride * 2 * s->mb_height; - Picture *pic= s->current_picture_ptr; - av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n"); for(i=0; i<2; i++){ pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t)); - pic->motion_val_base[i]= av_mallocz((size+2) * 2 * sizeof(uint16_t)); - pic->motion_val[i]= pic->motion_val_base[i]+2; + pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t)); + pic->motion_val[i]= pic->motion_val_base[i]+4; } pic->motion_subsample_log2= 3; s->current_picture= *s->current_picture_ptr; } + for(i=0; i<2; i++){ + if(pic->ref_index[i]) + memset(pic->ref_index[i], 0, size * sizeof(uint8_t)); + } + if(s->avctx->debug&FF_DEBUG_ER){ for(mb_y=0; mb_ymb_height; mb_y++){ for(mb_x=0; mb_xmb_width; mb_x++){ @@ -816,6 +819,17 @@ void ff_er_frame_end(MpegEncContext *s){ } } #endif + + dc_error= ac_error= mv_error=0; + for(i=0; imb_num; i++){ + const int mb_xy= s->mb_index2xy[i]; + error= s->error_status_table[mb_xy]; + if(error&DC_ERROR) dc_error ++; + if(error&AC_ERROR) ac_error ++; + if(error&MV_ERROR) mv_error ++; + } + av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error); + is_intra_likely= is_intra_more_likely(s); /* set unknown mb-type to most likely */ diff --git a/src/libffmpeg/libavcodec/eval.c b/src/libffmpeg/libavcodec/eval.c index aead600e8..330781581 100644 --- a/src/libffmpeg/libavcodec/eval.c +++ b/src/libffmpeg/libavcodec/eval.c @@ -42,10 +42,7 @@ #define M_PI 3.14159265358979323846 #endif -#define STACK_SIZE 100 - typedef struct Parser{ - double stack[STACK_SIZE]; int stack_index; char *s; double *const_value; @@ -57,25 +54,7 @@ typedef struct Parser{ void *opaque; } Parser; -static void evalExpression(Parser *p); - -static void push(Parser *p, double d){ - if(p->stack_index+1>= STACK_SIZE){ - av_log(NULL, AV_LOG_ERROR, "stack overflow in the parser\n"); - return; - } - p->stack[ p->stack_index++ ]= d; -//printf("push %f\n", d); fflush(stdout); -} - -static double pop(Parser *p){ - if(p->stack_index<=0){ - av_log(NULL, AV_LOG_ERROR, "stack underflow in the parser\n"); - return NAN; - } -//printf("pop\n"); fflush(stdout); - return p->stack[ --p->stack_index ]; -} +static double evalExpression(Parser *p); static int strmatch(const char *s, const char *prefix){ int i; @@ -85,7 +64,7 @@ static int strmatch(const char *s, const char *prefix){ return 1; } -static void evalPrimary(Parser *p){ +static double evalPrimary(Parser *p){ double d, d2=NAN; char *next= p->s; int i; @@ -93,36 +72,32 @@ static void evalPrimary(Parser *p){ /* number */ d= strtod(p->s, &next); if(next != p->s){ - push(p, d); p->s= next; - return; + return d; } /* named constants */ - for(i=0; p->const_name[i]; i++){ + for(i=0; p->const_name && p->const_name[i]; i++){ if(strmatch(p->s, p->const_name[i])){ - push(p, p->const_value[i]); p->s+= strlen(p->const_name[i]); - return; + return p->const_value[i]; } } p->s= strchr(p->s, '('); if(p->s==NULL){ av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next); - return; + return NAN; } p->s++; // "(" - evalExpression(p); - d= pop(p); + d= evalExpression(p); if(p->s[0]== ','){ p->s++; // "," - evalExpression(p); - d2= pop(p); + d2= evalExpression(p); } if(p->s[0] != ')'){ av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next); - return; + return NAN; } p->s++; // ")" @@ -144,96 +119,67 @@ static void evalPrimary(Parser *p){ else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; + else if( strmatch(next, "(" ) ) d= d; // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; else{ - int error=1; for(i=0; p->func1_name && p->func1_name[i]; i++){ if(strmatch(next, p->func1_name[i])){ - d= p->func1[i](p->opaque, d); - error=0; - break; + return p->func1[i](p->opaque, d); } } for(i=0; p->func2_name && p->func2_name[i]; i++){ if(strmatch(next, p->func2_name[i])){ - d= p->func2[i](p->opaque, d, d2); - error=0; - break; + return p->func2[i](p->opaque, d, d2); } } - if(error){ - av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); - return; - } + av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); + return NAN; } - - push(p, d); + + return d; } - -static void evalPow(Parser *p){ - int neg= 0; - if(p->s[0]=='+') p->s++; - - if(p->s[0]=='-'){ - neg= 1; - p->s++; - } - - if(p->s[0]=='('){ - p->s++;; - evalExpression(p); - if(p->s[0]!=')') - av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n"); - p->s++; - }else{ - evalPrimary(p); - } - - if(neg) push(p, -pop(p)); +static double evalPow(Parser *p){ + int sign= (*p->s == '+') - (*p->s == '-'); + p->s += sign&1; + return (sign|1) * evalPrimary(p); } -static void evalFactor(Parser *p){ - evalPow(p); +static double evalFactor(Parser *p){ + double ret= evalPow(p); while(p->s[0]=='^'){ - double d; - p->s++; - evalPow(p); - d= pop(p); - push(p, pow(pop(p), d)); + ret= pow(ret, evalPow(p)); } + return ret; } -static void evalTerm(Parser *p){ - evalFactor(p); +static double evalTerm(Parser *p){ + double ret= evalFactor(p); while(p->s[0]=='*' || p->s[0]=='/'){ - int inv= p->s[0]=='/'; - double d; - - p->s++; - evalFactor(p); - d= pop(p); - if(inv) d= 1.0/d; - push(p, d * pop(p)); + if(*p->s++ == '*') ret*= evalFactor(p); + else ret/= evalFactor(p); } + return ret; } -static void evalExpression(Parser *p){ - evalTerm(p); - while(p->s[0]=='+' || p->s[0]=='-'){ - int sign= p->s[0]=='-'; - double d; +static double evalExpression(Parser *p){ + double ret= 0; - p->s++; - evalTerm(p); - d= pop(p); - if(sign) d= -d; - push(p, d + pop(p)); - } + if(p->stack_index <= 0) //protect against stack overflows + return NAN; + p->stack_index--; + + do{ + ret += evalTerm(p); + }while(*p->s == '+' || *p->s == '-'); + + p->stack_index++; + + return ret; } double ff_eval(char *s, double *const_value, const char **const_name, @@ -242,7 +188,7 @@ double ff_eval(char *s, double *const_value, const char **const_name, void *opaque){ Parser p; - p.stack_index=0; + p.stack_index=100; p.s= s; p.const_value= const_value; p.const_name = const_name; @@ -252,6 +198,29 @@ double ff_eval(char *s, double *const_value, const char **const_name, p.func2_name = func2_name; p.opaque = opaque; - evalExpression(&p); - return pop(&p); + return evalExpression(&p); +} + +#ifdef TEST +#undef printf +static double const_values[]={ + M_PI, + M_E, + 0 +}; +static const char *const_names[]={ + "PI", + "E", + 0 +}; +main(){ + int i; + printf("%f == 12.7\n", ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL)); + + for(i=0; i<1050; i++){ + START_TIMER + ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL); + STOP_TIMER("ff_eval") + } } +#endif diff --git a/src/libffmpeg/libavcodec/ffv1.c b/src/libffmpeg/libavcodec/ffv1.c index a85baea4b..6a4c6ed3f 100644 --- a/src/libffmpeg/libavcodec/ffv1.c +++ b/src/libffmpeg/libavcodec/ffv1.c @@ -25,9 +25,10 @@ */ #include "common.h" +#include "bitstream.h" #include "avcodec.h" #include "dsputil.h" -#include "cabac.h" +#include "rangecoder.h" #include "golomb.h" #define MAX_PLANES 4 @@ -164,7 +165,7 @@ typedef struct PlaneContext{ typedef struct FFV1Context{ AVCodecContext *avctx; - CABACContext c; + RangeCoder c; GetBitContext gb; PutBitContext pb; int version; @@ -218,57 +219,52 @@ static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *l return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]; } -/** - * put - */ -static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed, int max_exp){ +static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){ int i; if(v){ const int a= ABS(v); const int e= av_log2(a); - - put_cabac(c, state+0, 0); + put_rac(c, state+0, 0); + assert(e<=9); + for(i=0; i=0; i--){ - put_cabac(c, state+16+e+i, (a>>i)&1); //17..29 - } - if(is_signed) - put_cabac(c, state+9 + e, v < 0); //9..16 + for(i=e-1; i>=0; i--){ + put_rac(c, state+22+i, (a>>i)&1); //22..31 } + + if(is_signed) + put_rac(c, state+11 + e, v < 0); //11..21 }else{ - put_cabac(c, state+0, 1); + put_rac(c, state+0, 1); } } -static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed, int max_exp){ - if(get_cabac(c, state+0)) +static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){ + if(get_rac(c, state+0)) return 0; else{ - int i, e; - - for(e=0; e=0; i--){ - a += get_cabac(c, state+16+e+i)<=0; i--){ + a += a + get_rac(c, state+22 + i); //22..31 } - return -(1<drift + state->count)>>31); #endif - code = -2*code-1; - code^= (code>>31); //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k); - set_ur_golomb(pb, code, k, 12, bits); + set_sr_golomb(pb, code, k, 12, bits); update_vlc_state(state, v); } @@ -344,13 +338,9 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int assert(k<=8); - v= get_ur_golomb(gb, k, 12, bits); + v= get_sr_golomb(gb, k, 12, bits); //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); - v++; - if(v&1) v= (v>>1); - else v= -(v>>1); - #if 0 // JPEG LS if(k==0 && 2*state->drift <= - state->count) v ^= (-1); #else @@ -364,14 +354,26 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int return ret; } -static inline void encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){ +static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){ PlaneContext * const p= &s->plane[plane_index]; - CABACContext * const c= &s->c; + RangeCoder * const c= &s->c; int x; int run_index= s->run_index; int run_count=0; int run_mode=0; + if(s->ac){ + if(c->bytestream_end - c->bytestream < w*20){ + av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + return -1; + } + }else{ + if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){ + av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + return -1; + } + } + for(x=0; xac){ - put_symbol(c, p->state[context], diff, 1, bits-1); + put_symbol(c, p->state[context], diff, 1); }else{ if(context == 0) run_mode=1; @@ -426,11 +428,13 @@ static inline void encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], i put_bits(&s->pb, 1, 1); } s->run_index= run_index; + + return 0; } static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ int x,y,i; - const int ring_size=2; + const int ring_size= s->avctx->context_model ? 3 : 2; int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size]; s->run_index=0; @@ -453,7 +457,7 @@ static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){ int x, y, p, i; - const int ring_size=2; + const int ring_size= s->avctx->context_model ? 3 : 2; int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size]; s->run_index=0; @@ -490,32 +494,35 @@ static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int st } } -static void write_quant_table(CABACContext *c, int16_t *quant_table){ +static void write_quant_table(RangeCoder *c, int16_t *quant_table){ int last=0; int i; - uint8_t state[CONTEXT_SIZE]={0}; + uint8_t state[CONTEXT_SIZE]; + memset(state, 128, sizeof(state)); for(i=1; i<128 ; i++){ if(quant_table[i] != quant_table[i-1]){ - put_symbol(c, state, i-last-1, 0, 7); + put_symbol(c, state, i-last-1, 0); last= i; } } - put_symbol(c, state, i-last-1, 0, 7); + put_symbol(c, state, i-last-1, 0); } static void write_header(FFV1Context *f){ - uint8_t state[CONTEXT_SIZE]={0}; + uint8_t state[CONTEXT_SIZE]; int i; - CABACContext * const c= &f->c; + RangeCoder * const c= &f->c; - put_symbol(c, state, f->version, 0, 7); - put_symbol(c, state, f->avctx->coder_type, 0, 7); - put_symbol(c, state, f->colorspace, 0, 7); //YUV cs type - put_cabac(c, state, 1); //chroma planes - put_symbol(c, state, f->chroma_h_shift, 0, 7); - put_symbol(c, state, f->chroma_v_shift, 0, 7); - put_cabac(c, state, 0); //no transparency plane + memset(state, 128, sizeof(state)); + + put_symbol(c, state, f->version, 0); + put_symbol(c, state, f->avctx->coder_type, 0); + put_symbol(c, state, f->colorspace, 0); //YUV cs type + put_rac(c, state, 1); //chroma planes + put_symbol(c, state, f->chroma_h_shift, 0); + put_symbol(c, state, f->chroma_v_shift, 0); + put_rac(c, state, 0); //no transparency plane for(i=0; i<5; i++) write_quant_table(c, f->quant_table[i]); @@ -543,6 +550,12 @@ static int encode_init(AVCodecContext *avctx) FFV1Context *s = avctx->priv_data; int i; + if(avctx->strict_std_compliance >= 0){ + av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n" + "use vstrict=-1 / -strict -1 to use it anyway\n"); + return -1; + } + common_init(avctx); s->version=0; @@ -609,13 +622,12 @@ static void clear_state(FFV1Context *f){ for(i=0; iplane_count; i++){ PlaneContext *p= &f->plane[i]; - p->interlace_bit_state[0]= 0; - p->interlace_bit_state[1]= 0; + p->interlace_bit_state[0]= 128; + p->interlace_bit_state[1]= 128; for(j=0; jcontext_count; j++){ if(f->ac){ - memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE); - p->state[j][7] = 2*62; + memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE); }else{ p->vlc_state[j].drift= 0; p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2); @@ -628,39 +640,33 @@ static void clear_state(FFV1Context *f){ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ FFV1Context *f = avctx->priv_data; - CABACContext * const c= &f->c; + RangeCoder * const c= &f->c; AVFrame *pict = data; const int width= f->width; const int height= f->height; AVFrame * const p= &f->picture; int used_count= 0; + uint8_t keystate=128; + + ff_init_range_encoder(c, buf, buf_size); +// ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); + ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); - if(avctx->strict_std_compliance >= 0){ - av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n" - "use vstrict=-1 to use it anyway\n"); - return -1; - } - - ff_init_cabac_encoder(c, buf, buf_size); - ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); - c->lps_state[2] = 1; - c->lps_state[3] = 0; - *p = *pict; p->pict_type= FF_I_TYPE; if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){ - put_cabac_bypass(c, 1); + put_rac(c, &keystate, 1); p->key_frame= 1; write_header(f); clear_state(f); }else{ - put_cabac_bypass(c, 0); + put_rac(c, &keystate, 0); p->key_frame= 0; } if(!f->ac){ - used_count += put_cabac_terminate(c, 1); + used_count += ff_rac_terminate(c); //printf("pos=%d\n", used_count); init_put_bits(&f->pb, buf + used_count, buf_size - used_count); } @@ -681,7 +687,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, f->picture_number++; if(f->ac){ - return put_cabac_terminate(c, 1); + return ff_rac_terminate(c); }else{ flush_put_bits(&f->pb); //nicer padding FIXME return used_count + (put_bits_count(&f->pb)+7)/8; @@ -709,7 +715,7 @@ static int encode_end(AVCodecContext *avctx) static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){ PlaneContext * const p= &s->plane[plane_index]; - CABACContext * const c= &s->c; + RangeCoder * const c= &s->c; int x; int run_count=0; int run_mode=0; @@ -726,9 +732,9 @@ static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], i sign=0; - if(s->ac) - diff= get_symbol(c, p->state[context], 1, bits-1); - else{ + if(s->ac){ + diff= get_symbol(c, p->state[context], 1); + }else{ if(context == 0 && run_mode==0) run_mode=1; if(run_mode){ @@ -833,13 +839,15 @@ static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int st } } -static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){ +static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){ int v; int i=0; - uint8_t state[CONTEXT_SIZE]={0}; + uint8_t state[CONTEXT_SIZE]; + + memset(state, 128, sizeof(state)); for(v=0; i<128 ; v++){ - int len= get_symbol(c, state, 0, 7) + 1; + int len= get_symbol(c, state, 0) + 1; if(len + i > 128) return -1; @@ -860,17 +868,19 @@ static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){ } static int read_header(FFV1Context *f){ - uint8_t state[CONTEXT_SIZE]={0}; + uint8_t state[CONTEXT_SIZE]; int i, context_count; - CABACContext * const c= &f->c; + RangeCoder * const c= &f->c; - f->version= get_symbol(c, state, 0, 7); - f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7); - f->colorspace= get_symbol(c, state, 0, 7); //YUV cs type - get_cabac(c, state); //no chroma = false - f->chroma_h_shift= get_symbol(c, state, 0, 7); - f->chroma_v_shift= get_symbol(c, state, 0, 7); - get_cabac(c, state); //transparency plane + memset(state, 128, sizeof(state)); + + f->version= get_symbol(c, state, 0); + f->ac= f->avctx->coder_type= get_symbol(c, state, 0); + f->colorspace= get_symbol(c, state, 0); //YUV cs type + get_rac(c, state); //no chroma = false + f->chroma_h_shift= get_symbol(c, state, 0); + f->chroma_v_shift= get_symbol(c, state, 0); + get_rac(c, state); //transparency plane f->plane_count= 2; if(f->colorspace==0){ @@ -879,7 +889,7 @@ static int read_header(FFV1Context *f){ case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break; case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break; case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break; - case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break; + case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break; default: av_log(f->avctx, AV_LOG_ERROR, "format not supported\n"); return -1; @@ -900,7 +910,7 @@ static int read_header(FFV1Context *f){ context_count=1; for(i=0; i<5; i++){ context_count*= read_quant_table(c, f->quant_table[i], context_count); - if(context_count < 0){ + if(context_count < 0 || context_count > 32768){ av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n"); return -1; } @@ -933,26 +943,21 @@ static int decode_init(AVCodecContext *avctx) static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ FFV1Context *f = avctx->priv_data; - CABACContext * const c= &f->c; + RangeCoder * const c= &f->c; const int width= f->width; const int height= f->height; AVFrame * const p= &f->picture; int bytes_read; + uint8_t keystate= 128; AVFrame *picture = data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; - - ff_init_cabac_decoder(c, buf, buf_size); - ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); - c->lps_state[2] = 1; - c->lps_state[3] = 0; + ff_init_range_decoder(c, buf, buf_size); + ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); p->pict_type= FF_I_TYPE; //FIXME I vs. P - if(get_cabac_bypass(c)){ + if(get_rac(c, &keystate)){ p->key_frame= 1; read_header(f); clear_state(f); @@ -970,8 +975,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac); if(!f->ac){ - bytes_read = get_cabac_terminate(c); - if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); + bytes_read = c->bytestream - c->bytestream_start - 1; + if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME //printf("pos=%d\n", bytes_read); init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read); } else { @@ -1000,7 +1005,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 *data_size = sizeof(AVFrame); if(f->ac){ - bytes_read= get_cabac_terminate(c); + bytes_read= c->bytestream - c->bytestream_start - 1; if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n"); }else{ bytes_read+= (get_bits_count(&f->gb)+7)/8; diff --git a/src/libffmpeg/libavcodec/flac.c b/src/libffmpeg/libavcodec/flac.c index 464d7999d..894da9384 100644 --- a/src/libffmpeg/libavcodec/flac.c +++ b/src/libffmpeg/libavcodec/flac.c @@ -34,6 +34,7 @@ #include #include "avcodec.h" +#include "bitstream.h" #include "golomb.h" #undef NDEBUG @@ -142,6 +143,29 @@ static int64_t get_utf8(GetBitContext *gb) return val; } +#if 0 +static int skip_utf8(GetBitContext *gb) +{ + int ones=0, bytes; + + while(get_bits1(gb)) + ones++; + + if (ones==0) bytes=0; + else if(ones==1) return -1; + else bytes= ones - 1; + + skip_bits(gb, 7-ones); + while(bytes--){ + const int tmp = get_bits(gb, 8); + + if((tmp>>6) != 2) + return -1; + } + return 0; +} +#endif + static int get_crc8(const uint8_t *buf, int count){ int crc=0; int i; @@ -569,12 +593,12 @@ static int flac_decode_frame(AVCodecContext *avctx, int16_t *samples = data; if(s->max_framesize == 0){ - s->max_framesize= 8192; // should hopefully be enough for the first header + s->max_framesize= 65536; // should hopefully be enough for the first header s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize); } if(1 && s->max_framesize){//FIXME truncated - buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size); + buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0); input_buf_size= buf_size; if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){ @@ -612,10 +636,20 @@ static int flac_decode_frame(AVCodecContext *avctx, if(metadata_size){ switch(metadata_type) { - case METADATA_TYPE_STREAMINFO: + case METADATA_TYPE_STREAMINFO:{ metadata_streaminfo(s); + + /* Buffer might have been reallocated, reinit bitreader */ + if(buf != &s->bitstream[s->bitstream_index]) + { + int bits_count = get_bits_count(&s->gb); + buf= &s->bitstream[s->bitstream_index]; + init_get_bits(&s->gb, buf, buf_size*8); + skip_bits(&s->gb, bits_count); + } + dump_headers(s); - break; + break;} default: for(i=0; igb, 8); diff --git a/src/libffmpeg/libavcodec/flicvideo.c b/src/libffmpeg/libavcodec/flicvideo.c index 99825cebc..92cb8bd0b 100644 --- a/src/libffmpeg/libavcodec/flicvideo.c +++ b/src/libffmpeg/libavcodec/flicvideo.c @@ -176,7 +176,7 @@ static int flic_decode_frame(AVCodecContext *avctx, for (j = 0; j < color_changes; j++) { /* wrap around, for good measure */ - if (palette_ptr >= 256) + if ((unsigned)palette_ptr >= 256) palette_ptr = 0; r = buf[stream_ptr++] << color_shift; diff --git a/src/libffmpeg/libavcodec/g726.c b/src/libffmpeg/libavcodec/g726.c index c016f32cf..bc9374d3e 100644 --- a/src/libffmpeg/libavcodec/g726.c +++ b/src/libffmpeg/libavcodec/g726.c @@ -22,16 +22,18 @@ #include #include "avcodec.h" #include "common.h" +#include "bitstream.h" -/* +/** + * G.726 11bit float. * G.726 Standard uses rather odd 11bit floating point arithmentic for * numerous occasions. It's a mistery to me why they did it this way * instead of simply using 32bit integer arithmetic. */ typedef struct Float11 { - int sign; /* 1bit sign */ - int exp; /* 4bit exponent */ - int mant; /* 6bit mantissa */ + int sign; /**< 1bit sign */ + int exp; /**< 4bit exponent */ + int mant; /**< 6bit mantissa */ } Float11; static inline Float11* i2f(int16_t i, Float11* f) @@ -61,35 +63,35 @@ static inline int sgn(int value) } typedef struct G726Tables { - int bits; /* bits per sample */ - int* quant; /* quantization table */ - int* iquant; /* inverse quantization table */ - int* W; /* special table #1 ;-) */ - int* F; /* special table #2 */ + int bits; /**< bits per sample */ + int* quant; /**< quantization table */ + int* iquant; /**< inverse quantization table */ + int* W; /**< special table #1 ;-) */ + int* F; /**< special table #2 */ } G726Tables; typedef struct G726Context { - G726Tables* tbls; /* static tables needed for computation */ + G726Tables* tbls; /**< static tables needed for computation */ - Float11 sr[2]; /* prev. reconstructed samples */ - Float11 dq[6]; /* prev. difference */ - int a[2]; /* second order predictor coeffs */ - int b[6]; /* sixth order predictor coeffs */ - int pk[2]; /* signs of prev. 2 sez + dq */ + Float11 sr[2]; /**< prev. reconstructed samples */ + Float11 dq[6]; /**< prev. difference */ + int a[2]; /**< second order predictor coeffs */ + int b[6]; /**< sixth order predictor coeffs */ + int pk[2]; /**< signs of prev. 2 sez + dq */ - int ap; /* scale factor control */ - int yu; /* fast scale factor */ - int yl; /* slow scale factor */ - int dms; /* short average magnitude of F[i] */ - int dml; /* long average magnitude of F[i] */ - int td; /* tone detect */ - - int se; /* estimated signal for the next iteration */ - int sez; /* estimated second order prediction */ - int y; /* quantizer scaling factor for the next iteration */ + int ap; /**< scale factor control */ + int yu; /**< fast scale factor */ + int yl; /**< slow scale factor */ + int dms; /**< short average magnitude of F[i] */ + int dml; /**< long average magnitude of F[i] */ + int td; /**< tone detect */ + + int se; /**< estimated signal for the next iteration */ + int sez; /**< estimated second order prediction */ + int y; /**< quantizer scaling factor for the next iteration */ } G726Context; -static int quant_tbl16[] = /* 16kbit/s 2bits per sample */ +static int quant_tbl16[] = /**< 16kbit/s 2bits per sample */ { 260, INT_MAX }; static int iquant_tbl16[] = { 116, 365, 365, 116 }; @@ -98,7 +100,7 @@ static int W_tbl16[] = static int F_tbl16[] = { 0, 7, 7, 0 }; -static int quant_tbl24[] = /* 24kbit/s 3bits per sample */ +static int quant_tbl24[] = /**< 24kbit/s 3bits per sample */ { 7, 217, 330, INT_MAX }; static int iquant_tbl24[] = { INT_MIN, 135, 273, 373, 373, 273, 135, INT_MIN }; @@ -107,7 +109,7 @@ static int W_tbl24[] = static int F_tbl24[] = { 0, 1, 2, 7, 7, 2, 1, 0 }; -static int quant_tbl32[] = /* 32kbit/s 4bits per sample */ +static int quant_tbl32[] = /**< 32kbit/s 4bits per sample */ { -125, 79, 177, 245, 299, 348, 399, INT_MAX }; static int iquant_tbl32[] = { INT_MIN, 4, 135, 213, 273, 323, 373, 425, @@ -118,7 +120,7 @@ static int W_tbl32[] = static int F_tbl32[] = { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 }; -static int quant_tbl40[] = /* 40kbit/s 5bits per sample */ +static int quant_tbl40[] = /**< 40kbit/s 5bits per sample */ { -122, -16, 67, 138, 197, 249, 297, 338, 377, 412, 444, 474, 501, 527, 552, INT_MAX }; static int iquant_tbl40[] = @@ -142,7 +144,7 @@ static G726Tables G726Tables_pool[] = { 5, quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }}; -/* +/** * Para 4.2.2 page 18: Adaptive quantizer. */ static inline uint8_t quant(G726Context* c, int d) @@ -168,7 +170,7 @@ static inline uint8_t quant(G726Context* c, int d) return i; } -/* +/** * Para 4.2.3 page 22: Inverse adaptive quantizer. */ static inline int16_t inverse_quant(G726Context* c, int i) diff --git a/src/libffmpeg/libavcodec/golomb.h b/src/libffmpeg/libavcodec/golomb.h index cd8bdd38d..1204a52e2 100644 --- a/src/libffmpeg/libavcodec/golomb.h +++ b/src/libffmpeg/libavcodec/golomb.h @@ -1,6 +1,7 @@ /* * exp golomb vlc stuff * Copyright (c) 2003 Michael Niedermayer + * Copyright (c) 2004 Alex Beregszaszi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,7 +23,7 @@ * @file golomb.h * @brief * exp golomb vlc stuff - * @author Michael Niedermayer + * @author Michael Niedermayer and Alex Beregszaszi */ #define INVALID_VLC 0x80000000 @@ -80,7 +81,10 @@ static inline int svq3_get_ue_golomb(GetBitContext *gb){ return ff_interleaved_ue_golomb_vlc_code[buf]; }else{ - buf|=1; + LAST_SKIP_BITS(re, gb, 8); + UPDATE_CACHE(re, gb); + buf |= 1 | (GET_CACHE(re, gb) >> 8); + if((buf & 0xAAAAAAAA) == 0) return INVALID_VLC; @@ -88,7 +92,7 @@ static inline int svq3_get_ue_golomb(GetBitContext *gb){ buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); } - LAST_SKIP_BITS(re, gb, 63 - 2*log); + LAST_SKIP_BITS(re, gb, 63 - 2*log - 8); CLOSE_READER(re, gb); return ((buf << log) >> log) - 1; @@ -163,7 +167,10 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){ return ff_interleaved_se_golomb_vlc_code[buf]; }else{ - buf |=1; + LAST_SKIP_BITS(re, gb, 8); + UPDATE_CACHE(re, gb); + buf |= 1 | (GET_CACHE(re, gb) >> 8); + if((buf & 0xAAAAAAAA) == 0) return INVALID_VLC; @@ -171,7 +178,7 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){ buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); } - LAST_SKIP_BITS(re, gb, 63 - 2*log); + LAST_SKIP_BITS(re, gb, 63 - 2*log - 8); CLOSE_READER(re, gb); return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; @@ -257,16 +264,50 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int } /** - * read unsigned golomb rice code (flac). + * read signed golomb rice code (ffv1). + */ +static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){ + int v= get_ur_golomb(gb, k, limit, esc_len); + + v++; + if (v&1) return v>>1; + else return -(v>>1); + +// return (v>>1) ^ -(v&1); +} + +/** + * read signed golomb rice code (flac). */ static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){ int v= get_ur_golomb_jpegls(gb, k, limit, esc_len); return (v>>1) ^ -(v&1); } +/** + * read unsigned golomb rice code (shorten). + */ +static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){ + return get_ur_golomb_jpegls(gb, k, INT_MAX, 0); +} + +/** + * read signed golomb rice code (shorten). + */ +static inline int get_sr_golomb_shorten(GetBitContext* gb, int k) +{ + int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0); + if (uvar & 1) + return ~(uvar >> 1); + else + return uvar >> 1; +} + + + #ifdef TRACE -static inline int get_ue(GetBitContext *s, char *file, char *func, int line){ +static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){ int show= show_bits(s, 24); int pos= get_bits_count(s); int i= get_ue_golomb(s); @@ -275,12 +316,12 @@ static inline int get_ue(GetBitContext *s, char *file, char *func, int line){ print_bin(bits, len); - printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); return i; } -static inline int get_se(GetBitContext *s, char *file, char *func, int line){ +static inline int get_se(GetBitContext *s, char *file, const char *func, int line){ int show= show_bits(s, 24); int pos= get_bits_count(s); int i= get_se_golomb(s); @@ -289,12 +330,12 @@ static inline int get_se(GetBitContext *s, char *file, char *func, int line){ print_bin(bits, len); - printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); return i; } -static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){ +static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){ int show= show_bits(s, 24); int pos= get_bits_count(s); int i= get_te0_golomb(s, r); @@ -303,7 +344,7 @@ static inline int get_te(GetBitContext *s, int r, char *file, char *func, int li print_bin(bits, len); - printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); return i; } @@ -400,3 +441,27 @@ static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int lim put_bits(pb, esc_len, i - 1); } } + +/** + * write signed golomb rice code (ffv1). + */ +static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ + int v; + + v = -2*i-1; + v ^= (v>>31); + + set_ur_golomb(pb, v, k, limit, esc_len); +} + +/** + * write signed golomb rice code (flac). + */ +static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){ + int v; + + v = -2*i-1; + v ^= (v>>31); + + set_ur_golomb_jpegls(pb, v, k, limit, esc_len); +} diff --git a/src/libffmpeg/libavcodec/h261.c b/src/libffmpeg/libavcodec/h261.c new file mode 100644 index 000000000..aceebaa38 --- /dev/null +++ b/src/libffmpeg/libavcodec/h261.c @@ -0,0 +1,1041 @@ +/* + * H261 decoder + * Copyright (c) 2002-2004 Michael Niedermayer + * Copyright (c) 2004 Maarten Daniels + * + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file h261.c + * h261codec. + */ + +#include "common.h" +#include "dsputil.h" +#include "avcodec.h" +#include "mpegvideo.h" +#include "h261data.h" + + +#define H261_MBA_VLC_BITS 9 +#define H261_MTYPE_VLC_BITS 6 +#define H261_MV_VLC_BITS 7 +#define H261_CBP_VLC_BITS 9 +#define TCOEFF_VLC_BITS 9 + +#define MBA_STUFFING 33 +#define MBA_STARTCODE 34 +#define IS_FIL(a) ((a)&MB_TYPE_H261_FIL) + +/** + * H261Context + */ +typedef struct H261Context{ + MpegEncContext s; + + int current_mba; + int previous_mba; + int mba_diff; + int mtype; + int current_mv_x; + int current_mv_y; + int gob_number; + int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read +}H261Context; + +void ff_h261_loop_filter(MpegEncContext *s){ + H261Context * h= (H261Context*)s; + const int linesize = s->linesize; + const int uvlinesize= s->uvlinesize; + uint8_t *dest_y = s->dest[0]; + uint8_t *dest_cb= s->dest[1]; + uint8_t *dest_cr= s->dest[2]; + + if(!(IS_FIL (h->mtype))) + return; + + s->dsp.h261_loop_filter(dest_y , linesize); + s->dsp.h261_loop_filter(dest_y + 8, linesize); + s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize); + s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize); + s->dsp.h261_loop_filter(dest_cb, uvlinesize); + s->dsp.h261_loop_filter(dest_cr, uvlinesize); +} + +static int ff_h261_get_picture_format(int width, int height){ + // QCIF + if (width == 176 && height == 144) + return 0; + // CIF + else if (width == 352 && height == 288) + return 1; + // ERROR + else + return -1; +} + +static void h261_encode_block(H261Context * h, DCTELEM * block, + int n); +static int h261_decode_block(H261Context *h, DCTELEM *block, + int n, int coded); + +void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){ + H261Context * h = (H261Context *) s; + int format, temp_ref; + + align_put_bits(&s->pb); + + /* Update the pointer to last GOB */ + s->ptr_lastgob = pbBufPtr(&s->pb); + + put_bits(&s->pb, 20, 0x10); /* PSC */ + + temp_ref= s->picture_number * (int64_t)30000 * s->avctx->frame_rate_base / + (1001 * (int64_t)s->avctx->frame_rate); + put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */ + + put_bits(&s->pb, 1, 0); /* split screen off */ + put_bits(&s->pb, 1, 0); /* camera off */ + put_bits(&s->pb, 1, 0); /* freeze picture release off */ + + format = ff_h261_get_picture_format(s->width, s->height); + + put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */ + + put_bits(&s->pb, 1, 0); /* still image mode */ + put_bits(&s->pb, 1, 0); /* reserved */ + + put_bits(&s->pb, 1, 0); /* no PEI */ + if(format == 0) + h->gob_number = -1; + else + h->gob_number = 0; + h->current_mba = 0; +} + +/** + * Encodes a group of blocks header. + */ +static void h261_encode_gob_header(MpegEncContext * s, int mb_line){ + H261Context * h = (H261Context *)s; + if(ff_h261_get_picture_format(s->width, s->height) == 0){ + h->gob_number+=2; // QCIF + } + else{ + h->gob_number++; // CIF + } + put_bits(&s->pb, 16, 1); /* GBSC */ + put_bits(&s->pb, 4, h->gob_number); /* GN */ + put_bits(&s->pb, 5, s->qscale); /* GQUANT */ + put_bits(&s->pb, 1, 0); /* no GEI */ + h->current_mba = 0; + h->previous_mba = 0; + h->current_mv_x=0; + h->current_mv_y=0; +} + +void ff_h261_reorder_mb_index(MpegEncContext* s){ + int index= s->mb_x + s->mb_y*s->mb_width; + + if(index % 33 == 0) + h261_encode_gob_header(s,0); + + /* for CIF the GOB's are fragmented in the middle of a scanline + that's why we need to adjust the x and y index of the macroblocks */ + if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF + s->mb_x = index % 11 ; index /= 11; + s->mb_y = index % 3 ; index /= 3; + s->mb_x+= 11*(index % 2); index /= 2; + s->mb_y+= 3*index; + + ff_init_block_index(s); + ff_update_block_index(s); + } +} + +static void h261_encode_motion(H261Context * h, int val){ + MpegEncContext * const s = &h->s; + int sign, code; + if(val==0){ + code = 0; + put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]); + } + else{ + if(val > 15) + val -=32; + if(val < -16) + val+=32; + sign = val < 0; + code = sign ? -val : val; + put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]); + put_bits(&s->pb,1,sign); + } +} + +static inline int get_cbp(MpegEncContext * s, + DCTELEM block[6][64]) +{ + int i, cbp; + cbp= 0; + for (i = 0; i < 6; i++) { + if (s->block_last_index[i] >= 0) + cbp |= 1 << (5 - i); + } + return cbp; +} +void ff_h261_encode_mb(MpegEncContext * s, + DCTELEM block[6][64], + int motion_x, int motion_y) +{ + H261Context * h = (H261Context *)s; + int mvd, mv_diff_x, mv_diff_y, i, cbp; + cbp = 63; // avoid warning + mvd = 0; + + h->current_mba++; + h->mtype = 0; + + if (!s->mb_intra){ + /* compute cbp */ + cbp= get_cbp(s, block); + + /* mvd indicates if this block is motion compensated */ + mvd = motion_x | motion_y; + + if((cbp | mvd | s->dquant ) == 0) { + /* skip macroblock */ + s->skip_count++; + h->current_mv_x=0; + h->current_mv_y=0; + return; + } + } + + /* MB is not skipped, encode MBA */ + put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]); + + /* calculate MTYPE */ + if(!s->mb_intra){ + h->mtype++; + + if(mvd || s->loop_filter) + h->mtype+=3; + if(s->loop_filter) + h->mtype+=3; + if(cbp || s->dquant) + h->mtype++; + assert(h->mtype > 1); + } + + if(s->dquant) + h->mtype++; + + put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]); + + h->mtype = h261_mtype_map[h->mtype]; + + if(IS_QUANT(h->mtype)){ + ff_set_qscale(s,s->qscale+s->dquant); + put_bits(&s->pb, 5, s->qscale); + } + + if(IS_16X16(h->mtype)){ + mv_diff_x = (motion_x >> 1) - h->current_mv_x; + mv_diff_y = (motion_y >> 1) - h->current_mv_y; + h->current_mv_x = (motion_x >> 1); + h->current_mv_y = (motion_y >> 1); + h261_encode_motion(h,mv_diff_x); + h261_encode_motion(h,mv_diff_y); + } + + h->previous_mba = h->current_mba; + + if(HAS_CBP(h->mtype)){ + put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]); + } + for(i=0; i<6; i++) { + /* encode each block */ + h261_encode_block(h, block[i], i); + } + + if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){ + h->current_mv_x=0; + h->current_mv_y=0; + } +} + +void ff_h261_encode_init(MpegEncContext *s){ + static int done = 0; + + if (!done) { + done = 1; + init_rl(&h261_rl_tcoeff, 1); + } + + s->min_qcoeff= -127; + s->max_qcoeff= 127; + s->y_dc_scale_table= + s->c_dc_scale_table= ff_mpeg1_dc_scale_table; +} + + +/** + * encodes a 8x8 block. + * @param block the 8x8 block + * @param n block index (0-3 are luma, 4-5 are chroma) + */ +static void h261_encode_block(H261Context * h, DCTELEM * block, int n){ + MpegEncContext * const s = &h->s; + int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code; + RLTable *rl; + + rl = &h261_rl_tcoeff; + if (s->mb_intra) { + /* DC coef */ + level = block[0]; + /* 255 cannot be represented, so we clamp */ + if (level > 254) { + level = 254; + block[0] = 254; + } + /* 0 cannot be represented also */ + else if (level < 1) { + level = 1; + block[0] = 1; + } + if (level == 128) + put_bits(&s->pb, 8, 0xff); + else + put_bits(&s->pb, 8, level); + i = 1; + } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){ + //special case + put_bits(&s->pb,2,block[0]>0 ? 2 : 3 ); + i = 1; + } else { + i = 0; + } + + /* AC coefs */ + last_index = s->block_last_index[n]; + last_non_zero = i - 1; + for (; i <= last_index; i++) { + j = s->intra_scantable.permutated[i]; + level = block[j]; + if (level) { + run = i - last_non_zero - 1; + last = (i == last_index); + sign = 0; + slevel = level; + if (level < 0) { + sign = 1; + level = -level; + } + code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level); + if(run==0 && level < 16) + code+=1; + put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); + if (code == rl->n) { + put_bits(&s->pb, 6, run); + assert(slevel != 0); + assert(level <= 127); + put_bits(&s->pb, 8, slevel & 0xff); + } else { + put_bits(&s->pb, 1, sign); + } + last_non_zero = i; + } + } + if(last_index > -1){ + put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK + } +} + +/***********************************************/ +/* decoding */ + +static VLC h261_mba_vlc; +static VLC h261_mtype_vlc; +static VLC h261_mv_vlc; +static VLC h261_cbp_vlc; + +void init_vlc_rl(RLTable *rl, int use_static); + +static void h261_decode_init_vlc(H261Context *h){ + static int done = 0; + + if(!done){ + done = 1; + init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35, + h261_mba_bits, 1, 1, + h261_mba_code, 1, 1, 1); + init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, + h261_mtype_bits, 1, 1, + h261_mtype_code, 1, 1, 1); + init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17, + &h261_mv_tab[0][1], 2, 1, + &h261_mv_tab[0][0], 2, 1, 1); + init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63, + &h261_cbp_tab[0][1], 2, 1, + &h261_cbp_tab[0][0], 2, 1, 1); + init_rl(&h261_rl_tcoeff, 1); + init_vlc_rl(&h261_rl_tcoeff, 1); + } +} + +static int h261_decode_init(AVCodecContext *avctx){ + H261Context *h= avctx->priv_data; + MpegEncContext * const s = &h->s; + + // set defaults + MPV_decode_defaults(s); + s->avctx = avctx; + + s->width = s->avctx->coded_width; + s->height = s->avctx->coded_height; + s->codec_id = s->avctx->codec->id; + + s->out_format = FMT_H261; + s->low_delay= 1; + avctx->pix_fmt= PIX_FMT_YUV420P; + + s->codec_id= avctx->codec->id; + + h261_decode_init_vlc(h); + + h->gob_start_code_skipped = 0; + + return 0; +} + +/** + * decodes the group of blocks header or slice header. + * @return <0 if an error occured + */ +static int h261_decode_gob_header(H261Context *h){ + unsigned int val; + MpegEncContext * const s = &h->s; + + if ( !h->gob_start_code_skipped ){ + /* Check for GOB Start Code */ + val = show_bits(&s->gb, 15); + if(val) + return -1; + + /* We have a GBSC */ + skip_bits(&s->gb, 16); + } + + h->gob_start_code_skipped = 0; + + h->gob_number = get_bits(&s->gb, 4); /* GN */ + s->qscale = get_bits(&s->gb, 5); /* GQUANT */ + + /* Check if gob_number is valid */ + if (s->mb_height==18){ //cif + if ((h->gob_number<=0) || (h->gob_number>12)) + return -1; + } + else{ //qcif + if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5)) + return -1; + } + + /* GEI */ + while (get_bits1(&s->gb) != 0) { + skip_bits(&s->gb, 8); + } + + if(s->qscale==0) + return -1; + + // For the first transmitted macroblock in a GOB, MBA is the absolute address. For + // subsequent macroblocks, MBA is the difference between the absolute addresses of + // the macroblock and the last transmitted macroblock. + h->current_mba = 0; + h->mba_diff = 0; + + return 0; +} + +/** + * decodes the group of blocks / video packet header. + * @return <0 if no resync found + */ +static int ff_h261_resync(H261Context *h){ + MpegEncContext * const s = &h->s; + int left, ret; + + if ( h->gob_start_code_skipped ){ + ret= h261_decode_gob_header(h); + if(ret>=0) + return 0; + } + else{ + if(show_bits(&s->gb, 15)==0){ + ret= h261_decode_gob_header(h); + if(ret>=0) + return 0; + } + //ok, its not where its supposed to be ... + s->gb= s->last_resync_gb; + align_get_bits(&s->gb); + left= s->gb.size_in_bits - get_bits_count(&s->gb); + + for(;left>15+1+4+5; left-=8){ + if(show_bits(&s->gb, 15)==0){ + GetBitContext bak= s->gb; + + ret= h261_decode_gob_header(h); + if(ret>=0) + return 0; + + s->gb= bak; + } + skip_bits(&s->gb, 8); + } + } + + return -1; +} + +/** + * decodes skipped macroblocks + * @return 0 + */ +static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 ) +{ + MpegEncContext * const s = &h->s; + int i; + + s->mb_intra = 0; + + for(i=mba1; imb_x= ((h->gob_number-1) % 2) * 11 + i % 11; + s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11; + xy = s->mb_x + s->mb_y * s->mb_stride; + ff_init_block_index(s); + ff_update_block_index(s); + s->dsp.clear_blocks(s->block[0]); + + for(j=0;j<6;j++) + s->block_last_index[j] = -1; + + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_16X16; + s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; + s->mv[0][0][0] = 0; + s->mv[0][0][1] = 0; + s->mb_skiped = 1; + h->mtype &= ~MB_TYPE_H261_FIL; + + MPV_decode_mb(s, s->block); + } + + return 0; +} + +static int decode_mv_component(GetBitContext *gb, int v){ + int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); + + /* check if mv_diff is valid */ + if ( mv_diff < 0 ) + return v; + + mv_diff = mvmap[mv_diff]; + + if(mv_diff && !get_bits1(gb)) + mv_diff= -mv_diff; + + v += mv_diff; + if (v <=-16) v+= 32; + else if(v >= 16) v-= 32; + + return v; +} + +static int h261_decode_mb(H261Context *h){ + MpegEncContext * const s = &h->s; + int i, cbp, xy; + + cbp = 63; + // Read mba + do{ + h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2); + + /* Check for slice end */ + /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ + if (h->mba_diff == MBA_STARTCODE){ // start code + h->gob_start_code_skipped = 1; + return SLICE_END; + } + } + while( h->mba_diff == MBA_STUFFING ); // stuffing + + if ( h->mba_diff < 0 ){ + if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits ) + return SLICE_END; + + av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); + return SLICE_ERROR; + } + + h->mba_diff += 1; + h->current_mba += h->mba_diff; + + if ( h->current_mba > MBA_STUFFING ) + return SLICE_ERROR; + + s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11); + s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11); + xy = s->mb_x + s->mb_y * s->mb_stride; + ff_init_block_index(s); + ff_update_block_index(s); + s->dsp.clear_blocks(s->block[0]); + + // Read mtype + h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); + h->mtype = h261_mtype_map[h->mtype]; + + // Read mquant + if ( IS_QUANT ( h->mtype ) ){ + ff_set_qscale(s, get_bits(&s->gb, 5)); + } + + s->mb_intra = IS_INTRA4x4(h->mtype); + + // Read mv + if ( IS_16X16 ( h->mtype ) ){ + // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the + // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the + // following three situations: + // 1) evaluating MVD for macroblocks 1, 12 and 23; + // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1; + // 3) MTYPE of the previous macroblock was not MC. + if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) || + ( h->mba_diff != 1)) + { + h->current_mv_x = 0; + h->current_mv_y = 0; + } + + h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x); + h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y); + }else{ + h->current_mv_x = 0; + h->current_mv_y = 0; + } + + // Read cbp + if ( HAS_CBP( h->mtype ) ){ + cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1; + } + + if(s->mb_intra){ + s->current_picture.mb_type[xy]= MB_TYPE_INTRA; + goto intra; + } + + //set motion vectors + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_16X16; + s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; + s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation + s->mv[0][0][1] = h->current_mv_y * 2; + +intra: + /* decode each block */ + if(s->mb_intra || HAS_CBP(h->mtype)){ + for (i = 0; i < 6; i++) { + if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){ + return SLICE_ERROR; + } + cbp+=cbp; + } + } + + MPV_decode_mb(s, s->block); + + return SLICE_OK; +} + +/** + * decodes a macroblock + * @return <0 if an error occured + */ +static int h261_decode_block(H261Context * h, DCTELEM * block, + int n, int coded) +{ + MpegEncContext * const s = &h->s; + int code, level, i, j, run; + RLTable *rl = &h261_rl_tcoeff; + const uint8_t *scan_table; + + // For the variable length encoding there are two code tables, one being used for + // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second + // for all other LEVELs except the first one in INTRA blocks which is fixed length + // coded with 8 bits. + // NOTE: the two code tables only differ in one VLC so we handle that manually. + scan_table = s->intra_scantable.permutated; + if (s->mb_intra){ + /* DC coef */ + level = get_bits(&s->gb, 8); + // 0 (00000000b) and -128 (10000000b) are FORBIDDEN + if((level&0x7F) == 0){ + av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); + return -1; + } + // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111. + if (level == 255) + level = 128; + block[0] = level; + i = 1; + }else if(coded){ + // Run Level Code + // EOB Not possible for first level when cbp is available (that's why the table is different) + // 0 1 1s + // * * 0* + int check = show_bits(&s->gb, 2); + i = 0; + if ( check & 0x2 ){ + skip_bits(&s->gb, 2); + block[0] = ( check & 0x1 ) ? -1 : 1; + i = 1; + } + }else{ + i = 0; + } + if(!coded){ + s->block_last_index[n] = i - 1; + return 0; + } + for(;;){ + code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2); + if (code < 0){ + av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); + return -1; + } + if (code == rl->n) { + /* escape */ + // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level. + run = get_bits(&s->gb, 6); + level = get_sbits(&s->gb, 8); + }else if(code == 0){ + break; + }else{ + run = rl->table_run[code]; + level = rl->table_level[code]; + if (get_bits1(&s->gb)) + level = -level; + } + i += run; + if (i >= 64){ + av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y); + return -1; + } + j = scan_table[i]; + block[j] = level; + i++; + } + s->block_last_index[n] = i-1; + return 0; +} + +/** + * decodes the H261 picture header. + * @return <0 if no startcode found + */ +int h261_decode_picture_header(H261Context *h){ + MpegEncContext * const s = &h->s; + int format, i; + uint32_t startcode= 0; + + for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){ + startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF; + + if(startcode == 0x10) + break; + } + + if (startcode != 0x10){ + av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n"); + return -1; + } + + /* temporal reference */ + s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */ + + /* PTYPE starts here */ + skip_bits1(&s->gb); /* split screen off */ + skip_bits1(&s->gb); /* camera off */ + skip_bits1(&s->gb); /* freeze picture release off */ + + format = get_bits1(&s->gb); + + //only 2 formats possible + if (format == 0){//QCIF + s->width = 176; + s->height = 144; + s->mb_width = 11; + s->mb_height = 9; + }else{//CIF + s->width = 352; + s->height = 288; + s->mb_width = 22; + s->mb_height = 18; + } + + s->mb_num = s->mb_width * s->mb_height; + + skip_bits1(&s->gb); /* still image mode off */ + skip_bits1(&s->gb); /* Reserved */ + + /* PEI */ + while (get_bits1(&s->gb) != 0){ + skip_bits(&s->gb, 8); + } + + // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does + // not contain all I-blocks (e.g. when a packet is lost) + s->pict_type = P_TYPE; + + h->gob_number = 0; + return 0; +} + +static int h261_decode_gob(H261Context *h){ + MpegEncContext * const s = &h->s; + + ff_set_qscale(s, s->qscale); + + /* decode mb's */ + while(h->current_mba <= MBA_STUFFING) + { + int ret; + /* DCT & quantize */ + ret= h261_decode_mb(h); + if(ret<0){ + if(ret==SLICE_END){ + h261_decode_mb_skipped(h, h->current_mba, 33); + return 0; + } + av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride); + return -1; + } + + h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); + } + + return -1; +} + +static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){ + int vop_found, i, j; + uint32_t state; + + vop_found= pc->frame_start_found; + state= pc->state; + + for(i=0; i>j)&0xFFFFF) == 0x00010){ + i++; + vop_found=1; + break; + } + } + } + if(vop_found){ + for(; i>j)&0xFFFFF) == 0x00010){ + pc->frame_start_found=0; + pc->state= state>>(2*8); + return i-1; + } + } + } + } + + pc->frame_start_found= vop_found; + pc->state= state; + return END_NOT_FOUND; +} + +static int h261_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + ParseContext *pc = s->priv_data; + int next; + + next= h261_find_frame_end(pc,avctx, buf, buf_size); + if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + *poutbuf = (uint8_t *)buf; + *poutbuf_size = buf_size; + return next; +} + +/** + * returns the number of bytes consumed for building the current frame + */ +static int get_consumed_bytes(MpegEncContext *s, int buf_size){ + int pos= get_bits_count(&s->gb)>>3; + if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...) + if(pos+10>buf_size) pos=buf_size; // oops ;) + + return pos; +} + +static int h261_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + H261Context *h= avctx->priv_data; + MpegEncContext *s = &h->s; + int ret; + AVFrame *pict = data; + +#ifdef DEBUG + printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); + printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); +#endif + s->flags= avctx->flags; + s->flags2= avctx->flags2; + + h->gob_start_code_skipped=0; + +retry: + + init_get_bits(&s->gb, buf, buf_size*8); + + if(!s->context_initialized){ + if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix + return -1; + } + + //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there + if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ + int i= ff_find_unused_picture(s, 0); + s->current_picture_ptr= &s->picture[i]; + } + + ret = h261_decode_picture_header(h); + + /* skip if the header was thrashed */ + if (ret < 0){ + av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); + return -1; + } + + if (s->width != avctx->coded_width || s->height != avctx->coded_height){ + ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat + s->parse_context.buffer=0; + MPV_common_end(s); + s->parse_context= pc; + } + if (!s->context_initialized) { + avcodec_set_dimensions(avctx, s->width, s->height); + + goto retry; + } + + // for hurry_up==5 + s->current_picture.pict_type= s->pict_type; + s->current_picture.key_frame= s->pict_type == I_TYPE; + + /* skip everything if we are in a hurry>=5 */ + if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size); + + if(MPV_frame_start(s, avctx) < 0) + return -1; + + ff_er_frame_start(s); + + /* decode each macroblock */ + s->mb_x=0; + s->mb_y=0; + + while(h->gob_number < (s->mb_height==18 ? 12 : 5)){ + if(ff_h261_resync(h)<0) + break; + h261_decode_gob(h); + } + MPV_frame_end(s); + +assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); +assert(s->current_picture.pict_type == s->pict_type); + *pict= *(AVFrame*)s->current_picture_ptr; + ff_print_debug_info(s, pict); + + /* Return the Picture timestamp as the frame number */ + /* we substract 1 because it is added on utils.c */ + avctx->frame_number = s->picture_number - 1; + + *data_size = sizeof(AVFrame); + + return get_consumed_bytes(s, buf_size); +} + +static int h261_decode_end(AVCodecContext *avctx) +{ + H261Context *h= avctx->priv_data; + MpegEncContext *s = &h->s; + + MPV_common_end(s); + return 0; +} + +#ifdef CONFIG_ENCODERS +AVCodec h261_encoder = { + "h261", + CODEC_TYPE_VIDEO, + CODEC_ID_H261, + sizeof(H261Context), + MPV_encode_init, + MPV_encode_picture, + MPV_encode_end, +}; +#endif + +AVCodec h261_decoder = { + "h261", + CODEC_TYPE_VIDEO, + CODEC_ID_H261, + sizeof(H261Context), + h261_decode_init, + NULL, + h261_decode_end, + h261_decode_frame, + CODEC_CAP_DR1, +}; + +AVCodecParser h261_parser = { + { CODEC_ID_H261 }, + sizeof(ParseContext), + NULL, + h261_parse, + ff_parse_close, +}; diff --git a/src/libffmpeg/libavcodec/h261data.h b/src/libffmpeg/libavcodec/h261data.h new file mode 100755 index 000000000..9ea991b23 --- /dev/null +++ b/src/libffmpeg/libavcodec/h261data.h @@ -0,0 +1,136 @@ +/** + * @file h261data.h + * H.261 tables. + */ +#define MB_TYPE_H261_FIL 0x800000 + +// H.261 VLC table for macroblock addressing +const uint8_t h261_mba_code[35] = { + 1, 3, 2, 3, + 2, 3, 2, 7, + 6, 11, 10, 9, + 8, 7, 6, 23, + 22, 21, 20, 19, + 18, 35, 34, 33, + 32, 31, 30, 29, + 28, 27, 26, 25, + 24, + 15, //(MBA stuffing) + 1 //(start code) +}; + +const uint8_t h261_mba_bits[35] = { + 1, 3, 3, 4, + 4, 5, 5, 7, + 7, 8, 8, 8, + 8, 8, 8, 10, + 10, 10, 10, 10, + 10, 11, 11, 11, + 11, 11, 11, 11, + 11, 11, 11, 11, + 11, + 11, //(MBA stuffing) + 16 //(start code) +}; + +//H.261 VLC table for macroblock type +const uint8_t h261_mtype_code[10] = { + 1, 1, 1, 1, + 1, 1, 1, 1, + 1, 1 +}; + +const uint8_t h261_mtype_bits[10] = { + 4, 7, 1, 5, + 9, 8, 10, 3, + 2, 6 +}; + +static const int h261_mtype_map[10]= { + MB_TYPE_INTRA4x4, + MB_TYPE_INTRA4x4 | MB_TYPE_QUANT, + MB_TYPE_CBP, + MB_TYPE_QUANT | MB_TYPE_CBP, + MB_TYPE_16x16, + MB_TYPE_CBP | MB_TYPE_16x16, + MB_TYPE_QUANT | MB_TYPE_CBP | MB_TYPE_16x16, + MB_TYPE_16x16 | MB_TYPE_H261_FIL, + MB_TYPE_CBP | MB_TYPE_16x16 | MB_TYPE_H261_FIL, + MB_TYPE_QUANT | MB_TYPE_CBP | MB_TYPE_16x16 | MB_TYPE_H261_FIL +}; + +//H.261 VLC table for motion vectors +const uint8_t h261_mv_tab[17][2] = { + {1,1}, {1,2}, {1,3}, {1,4}, {3,6}, {5,7}, {4,7}, {3,7}, + {11,9}, {10,9}, {9,9}, {17,10}, {16,10}, {15,10}, {14,10}, {13,10}, {12,10} +}; + +static const int mvmap[17] = +{ + 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16 +}; + +//H.261 VLC table for coded block pattern +const uint8_t h261_cbp_tab[63][2] = +{ + {11,5}, {9,5}, {13,6}, {13,4}, {23,7}, {19,7}, {31,8}, {12,4}, + {22,7}, {18,7}, {30,8}, {19,5}, {27,8}, {23,8}, {19,8}, {11,4}, + {21,7}, {17,7}, {29,8}, {17,5}, {25,8}, {21,8}, {17,8}, {15,6}, + {15,8}, {13,8}, {3,9}, {15,5}, {11,8}, {7,8}, {7,9}, {10,4}, + {20,7}, {16,7}, {28,8}, {14,6}, {14,8}, {12,8}, {2,9}, {16,5}, + {24,8}, {20,8}, {16,8}, {14,5}, {10,8}, {6,8}, {6,9}, {18,5}, + {26,8}, {22,8}, {18,8}, {13,5}, {9,8}, {5,8}, {5,9}, {12,5}, + {8,8}, {4,8}, {4,9}, {7,3}, {10,5}, {8,5}, {12,6} +}; + +//H.261 VLC table for transform coefficients +const uint16_t h261_tcoeff_vlc[65][2] = { +{ 0x2, 2 }, { 0x3, 2 },{ 0x4, 4 },{ 0x5, 5 }, +{ 0x6, 7 },{ 0x26, 8 },{ 0x21, 8 },{ 0xa, 10 }, +{ 0x1d, 12 },{ 0x18, 12 },{ 0x13, 12 },{ 0x10 , 12 }, +{ 0x1a, 13},{ 0x19, 13 }, { 0x18, 13 }, { 0x17, 13 }, +{ 0x3, 3 }, { 0x6, 6 }, { 0x25 , 8 }, { 0xc, 10 }, +{ 0x1b, 12 }, { 0x16, 13 }, { 0x15, 13 }, { 0x5, 4}, +{ 0x4, 7}, { 0xb, 10 }, { 0x14, 12 }, { 0x14, 13 }, +{ 0x7, 5 }, { 0x24, 8 }, { 0x1c, 12 }, { 0x13, 13 }, +{ 0x6, 5 }, { 0xf, 10 }, { 0x12, 12}, { 0x7, 6}, +{ 0x9 , 10 }, { 0x12, 13 }, { 0x5, 6 }, { 0x1e, 12 }, +{ 0x4, 6 }, { 0x15, 12 }, { 0x7, 7 }, { 0x11, 12}, +{ 0x5, 7 }, { 0x11, 13 }, { 0x27, 8 }, { 0x10, 13 }, +{ 0x23, 8 }, { 0x22, 8 }, { 0x20, 8 }, { 0xe , 10 }, +{ 0xd, 10 }, { 0x8, 10 },{ 0x1f, 12 }, { 0x1a, 12 }, +{ 0x19, 12 }, { 0x17, 12 }, { 0x16, 12}, { 0x1f, 13}, +{ 0x1e, 13 }, { 0x1d, 13 }, { 0x1c, 13}, { 0x1b, 13}, +{ 0x1, 6 } //escape +}; + +const int8_t h261_tcoeff_level[64] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 1, 2, 3, 4, 5, 6, 7, 1, + 2, 3, 4, 5, 1, 2, 3, 4, + 1, 2, 3, 1, 2, 3, 1, 2, + 1, 2, 1, 2, 1, 2, 1, 2, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1 +}; + +const int8_t h261_tcoeff_run[64] = { + 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 2, 2, + 2, 2, 2, 3, 3, 3, 3, 4, + 4, 4, 5, 5, 5, 6, 6, 7, + 7, 8, 8, 9, 9, 10, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26 +}; + +static RLTable h261_rl_tcoeff = { + 64, + 64, + h261_tcoeff_vlc, + h261_tcoeff_run, + h261_tcoeff_level, +}; diff --git a/src/libffmpeg/libavcodec/h263.c b/src/libffmpeg/libavcodec/h263.c index dd14a7bef..81c3648f1 100644 --- a/src/libffmpeg/libavcodec/h263.c +++ b/src/libffmpeg/libavcodec/h263.c @@ -57,20 +57,27 @@ static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n); static void h263p_encode_umotion(MpegEncContext * s, int val); +static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, + int n, int dc, uint8_t *scan_table, + PutBitContext *dc_pb, PutBitContext *ac_pb); #endif static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); static int h263p_decode_umotion(MpegEncContext * s, int pred); static int h263_decode_block(MpegEncContext * s, DCTELEM * block, int n, int coded); +static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, int n, int coded, int intra, int rvlc); +static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, + uint8_t *scan_table); static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr); #ifdef CONFIG_ENCODERS static void mpeg4_encode_visual_object_header(MpegEncContext * s); static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number); #endif //CONFIG_ENCODERS static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb); +static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding); #ifdef CONFIG_ENCODERS static uint8_t uni_DCtab_lum_len[512]; @@ -86,6 +93,8 @@ static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2]; static uint8_t uni_mpeg4_intra_rl_len [64*64*2*2]; static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2]; static uint8_t uni_mpeg4_inter_rl_len [64*64*2*2]; +static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2]; +static uint8_t uni_h263_inter_rl_len [64*64*2*2]; //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level)) //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64) #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level)) @@ -656,87 +665,36 @@ void ff_h263_update_motion_val(MpegEncContext * s){ } } -/** - * predicts the dc. - * encoding quantized level -> quantized diff - * decoding quantized diff -> quantized level - * @param n block index (0-3 are luma, 4-5 are chroma) - * @param dir_ptr pointer to an integer where the prediction direction will be stored - */ -static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding) -{ - int a, b, c, wrap, pred, scale, ret; - uint16_t *dc_val; - - /* find prediction */ - if (n < 4) { - scale = s->y_dc_scale; - } else { - scale = s->c_dc_scale; - } - if(IS_3IV1) - scale= 8; - - wrap= s->block_wrap[n]; - dc_val = s->dc_val[0] + s->block_index[n]; - - /* B C - * A X - */ - a = dc_val[ - 1]; - b = dc_val[ - 1 - wrap]; - c = dc_val[ - wrap]; +#ifdef CONFIG_ENCODERS - /* outside slice handling (we cant do that by memset as we need the dc for error resilience) */ - if(s->first_slice_line && n!=3){ - if(n!=2) b=c= 1024; - if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024; - } - if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){ - if(n==0 || n==4 || n==5) - b=1024; - } +static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){ + int l, bit_size, code; - if (abs(a - b) < abs(b - c)) { - pred = c; - *dir_ptr = 1; /* top */ + if (val == 0) { + return mvtab[0][1]; } else { - pred = a; - *dir_ptr = 0; /* left */ + bit_size = f_code - 1; + /* modulo encoding */ + l= INT_BIT - 6 - bit_size; + val = (val<>l; + val--; + code = (val >> bit_size) + 1; + + return mvtab[code][1] + 1 + bit_size; } - /* we assume pred is positive */ - pred = FASTDIV((pred + (scale >> 1)), scale); +} - if(encoding){ - ret = level - pred; +static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){ + if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){ + skip_put_bits(&s->pb, + h263_get_motion_length(s, x, f_code) + +h263_get_motion_length(s, y, f_code)); }else{ - level += pred; - ret= level; - if(s->error_resilience>=3){ - if(level<0){ - av_log(s->avctx, AV_LOG_ERROR, "dc<0 at %dx%d\n", s->mb_x, s->mb_y); - return -1; - } - if(level*scale > 2048 + scale){ - av_log(s->avctx, AV_LOG_ERROR, "dc overflow at %dx%d\n", s->mb_x, s->mb_y); - return -1; - } - } - } - level *=scale; - if(level&(~2047)){ - if(level<0) - level=0; - else if(!(s->workaround_bugs&FF_BUG_DC_CLIP)) - level=2047; + ff_h263_encode_motion(s, x, f_code); + ff_h263_encode_motion(s, y, f_code); } - dc_val[0]= level; - - return ret; } -#ifdef CONFIG_ENCODERS - static inline int get_p_cbp(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y){ @@ -836,169 +794,33 @@ static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64], return cbp; } -/** - * encodes the dc value. - * @param n block index (0-3 are luma, 4-5 are chroma) - */ -static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n) -{ -#if 1 -// if(level<-255 || level>255) printf("dc overflow\n"); - level+=256; - if (n < 4) { - /* luminance */ - put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]); - } else { - /* chrominance */ - put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]); - } -#else - int size, v; - /* find number of bits */ - size = 0; - v = abs(level); - while (v) { - v >>= 1; - size++; - } - - if (n < 4) { - /* luminance */ - put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]); - } else { - /* chrominance */ - put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]); - } - - /* encode remaining bits */ - if (size > 0) { - if (level < 0) - level = (-level) ^ ((1 << size) - 1); - put_bits(&s->pb, size, level); - if (size > 8) - put_bits(&s->pb, 1, 1); - } -#endif -} - -/** - * encodes a 8x8 block - * @param n block index (0-3 are luma, 4-5 are chroma) - */ -static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, - uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb) -{ - int i, last_non_zero; -#if 0 //variables for the outcommented version - int code, sign, last; -#endif - const RLTable *rl; - uint32_t *bits_tab; - uint8_t *len_tab; - const int last_index = s->block_last_index[n]; - - if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away - /* mpeg4 based DC predictor */ - mpeg4_encode_dc(dc_pb, intra_dc, n); - if(last_index<1) return; - i = 1; - rl = &rl_intra; - bits_tab= uni_mpeg4_intra_rl_bits; - len_tab = uni_mpeg4_intra_rl_len; - } else { - if(last_index<0) return; - i = 0; - rl = &rl_inter; - bits_tab= uni_mpeg4_inter_rl_bits; - len_tab = uni_mpeg4_inter_rl_len; - } - - /* AC coefs */ - last_non_zero = i - 1; -#if 1 - for (; i < last_index; i++) { - int level = block[ scan_table[i] ]; - if (level) { - int run = i - last_non_zero - 1; - level+=64; - if((level&(~127)) == 0){ - const int index= UNI_MPEG4_ENC_INDEX(0, run, level); - put_bits(ac_pb, len_tab[index], bits_tab[index]); - }else{ //ESC3 - put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1); +static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64], int intra_dc[6], + uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){ + int i; + + if(scan_table){ + if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){ + for (i = 0; i < 6; i++) { + skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i])); + } + }else{ + /* encode each block */ + for (i = 0; i < 6; i++) { + mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb); } - last_non_zero = i; - } - } - /*if(i<=last_index)*/{ - int level = block[ scan_table[i] ]; - int run = i - last_non_zero - 1; - level+=64; - if((level&(~127)) == 0){ - const int index= UNI_MPEG4_ENC_INDEX(1, run, level); - put_bits(ac_pb, len_tab[index], bits_tab[index]); - }else{ //ESC3 - put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1); } - } -#else - for (; i <= last_index; i++) { - const int slevel = block[ scan_table[i] ]; - if (slevel) { - int level; - int run = i - last_non_zero - 1; - last = (i == last_index); - sign = 0; - level = slevel; - if (level < 0) { - sign = 1; - level = -level; - } - code = get_rl_index(rl, last, run, level); - put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); - if (code == rl->n) { - int level1, run1; - level1 = level - rl->max_level[last][run]; - if (level1 < 1) - goto esc2; - code = get_rl_index(rl, last, run, level1); - if (code == rl->n) { - esc2: - put_bits(ac_pb, 1, 1); - if (level > MAX_LEVEL) - goto esc3; - run1 = run - rl->max_run[last][level] - 1; - if (run1 < 0) - goto esc3; - code = get_rl_index(rl, last, run1, level); - if (code == rl->n) { - esc3: - /* third escape */ - put_bits(ac_pb, 1, 1); - put_bits(ac_pb, 1, last); - put_bits(ac_pb, 6, run); - put_bits(ac_pb, 1, 1); - put_bits(ac_pb, 12, slevel & 0xfff); - put_bits(ac_pb, 1, 1); - } else { - /* second escape */ - put_bits(ac_pb, 1, 0); - put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); - put_bits(ac_pb, 1, sign); - } - } else { - /* first escape */ - put_bits(ac_pb, 1, 0); - put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); - put_bits(ac_pb, 1, sign); - } - } else { - put_bits(ac_pb, 1, sign); + }else{ + if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){ + for (i = 0; i < 6; i++) { + skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated)); } - last_non_zero = i; - } + }else{ + /* encode each block */ + for (i = 0; i < 6; i++) { + mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb); + } + } } -#endif } void mpeg4_encode_mb(MpegEncContext * s, @@ -1089,23 +911,22 @@ void mpeg4_encode_mb(MpegEncContext * s, if(mb_type == 0){ assert(s->mv_dir & MV_DIRECT); - ff_h263_encode_motion(s, motion_x, 1); - ff_h263_encode_motion(s, motion_y, 1); + ff_h263_encode_motion_vector(s, motion_x, motion_y, 1); s->b_count++; s->f_count++; }else{ assert(mb_type > 0 && mb_type < 4); if(s->mv_type != MV_TYPE_FIELD){ if(s->mv_dir & MV_DIR_FORWARD){ - ff_h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); - ff_h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); + ff_h263_encode_motion_vector(s, s->mv[0][0][0] - s->last_mv[0][0][0], + s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0]; s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1]; s->f_count++; } if(s->mv_dir & MV_DIR_BACKWARD){ - ff_h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); - ff_h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); + ff_h263_encode_motion_vector(s, s->mv[1][0][0] - s->last_mv[1][0][0], + s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0]; s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1]; s->b_count++; @@ -1121,8 +942,8 @@ void mpeg4_encode_mb(MpegEncContext * s, } if(s->mv_dir & MV_DIR_FORWARD){ for(i=0; i<2; i++){ - ff_h263_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); - ff_h263_encode_motion(s, s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code); + ff_h263_encode_motion_vector(s, s->mv[0][i][0] - s->last_mv[0][i][0] , + s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code); s->last_mv[0][i][0]= s->mv[0][i][0]; s->last_mv[0][i][1]= s->mv[0][i][1]*2; } @@ -1130,8 +951,8 @@ void mpeg4_encode_mb(MpegEncContext * s, } if(s->mv_dir & MV_DIR_BACKWARD){ for(i=0; i<2; i++){ - ff_h263_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code); - ff_h263_encode_motion(s, s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code); + ff_h263_encode_motion_vector(s, s->mv[1][i][0] - s->last_mv[1][i][0] , + s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code); s->last_mv[1][i][0]= s->mv[1][i][0]; s->last_mv[1][i][1]= s->mv[1][i][1]*2; } @@ -1144,10 +965,7 @@ void mpeg4_encode_mb(MpegEncContext * s, s->mv_bits+= get_bits_diff(s); } - /* encode each block */ - for (i = 0; i < 6; i++) { - mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, &s->pb); - } + mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb); if(interleaved_stats){ s->p_tex_bits+= get_bits_diff(s); @@ -1231,8 +1049,8 @@ void mpeg4_encode_mb(MpegEncContext * s, /* motion vectors: 16x16 mode */ h263_pred_motion(s, 0, 0, &pred_x, &pred_y); - ff_h263_encode_motion(s, motion_x - pred_x, s->f_code); - ff_h263_encode_motion(s, motion_y - pred_y, s->f_code); + ff_h263_encode_motion_vector(s, motion_x - pred_x, + motion_y - pred_y, s->f_code); }else if(s->mv_type==MV_TYPE_FIELD){ if(s->dquant) cbpc+= 8; put_bits(&s->pb, @@ -1259,10 +1077,10 @@ void mpeg4_encode_mb(MpegEncContext * s, put_bits(&s->pb, 1, s->field_select[0][0]); put_bits(&s->pb, 1, s->field_select[0][1]); - ff_h263_encode_motion(s, s->mv[0][0][0] - pred_x, s->f_code); - ff_h263_encode_motion(s, s->mv[0][0][1] - pred_y, s->f_code); - ff_h263_encode_motion(s, s->mv[0][1][0] - pred_x, s->f_code); - ff_h263_encode_motion(s, s->mv[0][1][1] - pred_y, s->f_code); + ff_h263_encode_motion_vector(s, s->mv[0][0][0] - pred_x, + s->mv[0][0][1] - pred_y, s->f_code); + ff_h263_encode_motion_vector(s, s->mv[0][1][0] - pred_x, + s->mv[0][1][1] - pred_y, s->f_code); }else{ assert(s->mv_type==MV_TYPE_8X8); put_bits(&s->pb, @@ -1283,8 +1101,8 @@ void mpeg4_encode_mb(MpegEncContext * s, /* motion vectors: 8x8 mode*/ h263_pred_motion(s, i, 0, &pred_x, &pred_y); - ff_h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x, s->f_code); - ff_h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code); + ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x, + s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code); } } @@ -1292,10 +1110,7 @@ void mpeg4_encode_mb(MpegEncContext * s, s->mv_bits+= get_bits_diff(s); } - /* encode each block */ - for (i = 0; i < 6; i++) { - mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, tex_pb); - } + mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb); if(interleaved_stats){ s->p_tex_bits+= get_bits_diff(s); @@ -1357,10 +1172,7 @@ void mpeg4_encode_mb(MpegEncContext * s, s->misc_bits+= get_bits_diff(s); } - /* encode each block */ - for (i = 0; i < 6; i++) { - mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb); - } + mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb); if(interleaved_stats){ s->i_tex_bits+= get_bits_diff(s); @@ -1373,8 +1185,6 @@ void mpeg4_encode_mb(MpegEncContext * s, } } - - void h263_encode_mb(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y) @@ -1426,8 +1236,8 @@ void h263_encode_mb(MpegEncContext * s, h263_pred_motion(s, 0, 0, &pred_x, &pred_y); if (!s->umvplus) { - ff_h263_encode_motion(s, motion_x - pred_x, 1); - ff_h263_encode_motion(s, motion_y - pred_y, 1); + ff_h263_encode_motion_vector(s, motion_x - pred_x, + motion_y - pred_y, 1); } else { h263p_encode_umotion(s, motion_x - pred_x); @@ -1455,8 +1265,8 @@ void h263_encode_mb(MpegEncContext * s, motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0]; motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1]; if (!s->umvplus) { - ff_h263_encode_motion(s, motion_x - pred_x, 1); - ff_h263_encode_motion(s, motion_y - pred_y, 1); + ff_h263_encode_motion_vector(s, motion_x - pred_x, + motion_y - pred_y, 1); } else { h263p_encode_umotion(s, motion_x - pred_x); @@ -1636,7 +1446,7 @@ void ff_h263_loop_filter(MpegEncContext * s){ const int chroma_qp= s->chroma_qscale_table[qp_dt]; s->dsp.h263_h_loop_filter(dest_y -8*linesize , linesize, qp_dt); s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp); - s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp); + s->dsp.h263_h_loop_filter(dest_cr-8*uvlinesize, uvlinesize, chroma_qp); } } } @@ -1871,30 +1681,12 @@ void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code) bit_size = f_code - 1; range = 1 << bit_size; /* modulo encoding */ - l = range * 32; -#if 1 - val+= l; - val&= 2*l-1; - val-= l; + l= INT_BIT - 6 - bit_size; + val = (val<>l; sign = val>>31; val= (val^sign)-sign; sign&=1; -#else - if (val < -l) { - val += 2*l; - } else if (val >= l) { - val -= 2*l; - } - assert(val>=-l && val= 0) { - sign = 0; - } else { - val = -val; - sign = 1; - } -#endif val--; code = (val >> bit_size) + 1; bits = val & (range - 1); @@ -1904,7 +1696,6 @@ void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code) put_bits(&s->pb, bit_size, bits); } } - } /* Encode MV differences on H.263+ with Unrestricted MV mode */ @@ -2135,6 +1926,49 @@ static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_ } } +static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){ + int slevel, run, last; + + assert(MAX_LEVEL >= 64); + assert(MAX_RUN >= 63); + + for(slevel=-64; slevel<64; slevel++){ + if(slevel==0) continue; + for(run=0; run<64; run++){ + for(last=0; last<=1; last++){ + const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64); + int level= slevel < 0 ? -slevel : slevel; + int sign= slevel < 0 ? 1 : 0; + int bits, len, code; + + len_tab[index]= 100; + + /* ESC0 */ + code= get_rl_index(rl, last, run, level); + bits= rl->table_vlc[code][0]; + len= rl->table_vlc[code][1]; + bits=bits*2+sign; len++; + + if(code!=rl->n && len < len_tab[index]){ + if(bits_tab) bits_tab[index]= bits; + len_tab [index]= len; + } + /* ESC */ + bits= rl->table_vlc[rl->n][0]; + len = rl->table_vlc[rl->n][1]; + bits=bits*2+last; len++; + bits=bits*64+run; len+=6; + bits=bits*256+(level&0xff); len+=8; + + if(len < len_tab[index]){ + if(bits_tab) bits_tab[index]= bits; + len_tab [index]= len; + } + } + } + } +} + void h263_encode_init(MpegEncContext *s) { static int done = 0; @@ -2144,17 +1978,28 @@ void h263_encode_init(MpegEncContext *s) init_uni_dc_tab(); - init_rl(&rl_inter); - init_rl(&rl_intra); - init_rl(&rl_intra_aic); + init_rl(&rl_inter, 1); + init_rl(&rl_intra, 1); + init_rl(&rl_intra_aic, 1); init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len); init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len); + init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len); + init_uni_h263_rl_tab(&rl_inter , NULL, uni_h263_inter_rl_len); + init_mv_penalty_and_fcode(s); } s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p + s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len; + s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64; + if(s->h263_aic){ + s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len; + s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64; + } + s->ac_esc_length= 7+1+6+8; + // use fcodes >1 only for mpeg4 & h263 & h263p FIXME switch(s->codec_id){ case CODEC_ID_MPEG4: @@ -2176,7 +2021,8 @@ void h263_encode_init(MpegEncContext *s) s->avctx->extradata= av_malloc(1024); init_put_bits(&s->pb, s->avctx->extradata, 1024); - mpeg4_encode_visual_object_header(s); + if(!(s->workaround_bugs & FF_BUG_MS)) + mpeg4_encode_visual_object_header(s); mpeg4_encode_vol_header(s, 0, 0); // ff_mpeg4_stuffing(&s->pb); ? @@ -2371,11 +2217,13 @@ void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){ if(s->pict_type==B_TYPE){ s->pb_time= s->pp_time - (s->last_non_b_time - s->time); + assert(s->pb_time > 0 && s->pb_time < s->pp_time); }else{ s->last_time_base= s->time_base; s->time_base= time_div; s->pp_time= s->time - s->last_non_b_time; s->last_non_b_time= s->time; + assert(picture_number==0 || s->pp_time > 0); } } @@ -2413,13 +2261,26 @@ static void mpeg4_encode_visual_object_header(MpegEncContext * s){ int profile_and_level_indication; int vo_ver_id; - if(s->max_b_frames || s->quarter_sample){ - profile_and_level_indication= 0xF1; // adv simple level 1 + if(s->avctx->profile != FF_PROFILE_UNKNOWN){ + profile_and_level_indication = s->avctx->profile << 4; + }else if(s->max_b_frames || s->quarter_sample){ + profile_and_level_indication= 0xF0; // adv simple + }else{ + profile_and_level_indication= 0x00; // simple + } + + if(s->avctx->level != FF_LEVEL_UNKNOWN){ + profile_and_level_indication |= s->avctx->level; + }else{ + profile_and_level_indication |= 1; //level 1 + } + + if(profile_and_level_indication>>4 == 0xF){ vo_ver_id= 5; }else{ - profile_and_level_indication= 0x01; // simple level 1 vo_ver_id= 1; } + //FIXME levels put_bits(&s->pb, 16, 0); @@ -2460,9 +2321,13 @@ static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_n put_bits(&s->pb, 1, 0); /* random access vol */ put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */ - put_bits(&s->pb, 1, 1); /* is obj layer id= yes */ - put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */ - put_bits(&s->pb, 3, 1); /* is obj layer priority */ + if(s->workaround_bugs & FF_BUG_MS) { + put_bits(&s->pb, 1, 0); /* is obj layer id= no */ + } else { + put_bits(&s->pb, 1, 1); /* is obj layer id= yes */ + put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */ + put_bits(&s->pb, 3, 1); /* is obj layer priority */ + } aspect_to_info(s, s->avctx->sample_aspect_ratio); @@ -2472,13 +2337,13 @@ static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_n put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den); } - if(s->low_delay){ - put_bits(&s->pb, 1, 1); /* vol control parameters= yes */ - put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */ + if(s->workaround_bugs & FF_BUG_MS) { // + put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */ + } else { + put_bits(&s->pb, 1, 1); /* vol control parameters= yes */ + put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */ put_bits(&s->pb, 1, s->low_delay); - put_bits(&s->pb, 1, 0); /* vbv parameters= no */ - }else{ - put_bits(&s->pb, 1, 0); /* vol control parameters= no */ + put_bits(&s->pb, 1, 0); /* vbv parameters= no */ } put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */ @@ -2549,7 +2414,8 @@ void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number) if(s->strict_std_compliance < 2 || picture_number==0) //HACK, the reference sw is buggy mpeg4_encode_vol_header(s, 0, 0); } - mpeg4_encode_gop_header(s); + if(!(s->workaround_bugs & FF_BUG_MS)) + mpeg4_encode_gop_header(s); } s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE; @@ -2612,6 +2478,84 @@ void ff_set_qscale(MpegEncContext * s, int qscale) s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; } +/** + * predicts the dc. + * encoding quantized level -> quantized diff + * decoding quantized diff -> quantized level + * @param n block index (0-3 are luma, 4-5 are chroma) + * @param dir_ptr pointer to an integer where the prediction direction will be stored + */ +static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding) +{ + int a, b, c, wrap, pred, scale, ret; + uint16_t *dc_val; + + /* find prediction */ + if (n < 4) { + scale = s->y_dc_scale; + } else { + scale = s->c_dc_scale; + } + if(IS_3IV1) + scale= 8; + + wrap= s->block_wrap[n]; + dc_val = s->dc_val[0] + s->block_index[n]; + + /* B C + * A X + */ + a = dc_val[ - 1]; + b = dc_val[ - 1 - wrap]; + c = dc_val[ - wrap]; + + /* outside slice handling (we cant do that by memset as we need the dc for error resilience) */ + if(s->first_slice_line && n!=3){ + if(n!=2) b=c= 1024; + if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024; + } + if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){ + if(n==0 || n==4 || n==5) + b=1024; + } + + if (abs(a - b) < abs(b - c)) { + pred = c; + *dir_ptr = 1; /* top */ + } else { + pred = a; + *dir_ptr = 0; /* left */ + } + /* we assume pred is positive */ + pred = FASTDIV((pred + (scale >> 1)), scale); + + if(encoding){ + ret = level - pred; + }else{ + level += pred; + ret= level; + if(s->error_resilience>=3){ + if(level<0){ + av_log(s->avctx, AV_LOG_ERROR, "dc<0 at %dx%d\n", s->mb_x, s->mb_y); + return -1; + } + if(level*scale > 2048 + scale){ + av_log(s->avctx, AV_LOG_ERROR, "dc overflow at %dx%d\n", s->mb_x, s->mb_y); + return -1; + } + } + } + level *=scale; + if(level&(~2047)){ + if(level<0) + level=0; + else if(!(s->workaround_bugs&FF_BUG_DC_CLIP)) + level=2047; + } + dc_val[0]= level; + + return ret; +} /** * predicts the ac. @@ -2675,35 +2619,94 @@ void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n, #ifdef CONFIG_ENCODERS +/** + * encodes the dc value. + * @param n block index (0-3 are luma, 4-5 are chroma) + */ +static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n) +{ +#if 1 +// if(level<-255 || level>255) printf("dc overflow\n"); + level+=256; + if (n < 4) { + /* luminance */ + put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]); + } else { + /* chrominance */ + put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]); + } +#else + int size, v; + /* find number of bits */ + size = 0; + v = abs(level); + while (v) { + v >>= 1; + size++; + } + if (n < 4) { + /* luminance */ + put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]); + } else { + /* chrominance */ + put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]); + } + /* encode remaining bits */ + if (size > 0) { + if (level < 0) + level = (-level) ^ ((1 << size) - 1); + put_bits(&s->pb, size, level); + if (size > 8) + put_bits(&s->pb, 1, 1); + } +#endif +} +static inline int mpeg4_get_dc_length(int level, int n){ + if (n < 4) { + return uni_DCtab_lum_len[level + 256]; + } else { + return uni_DCtab_chrom_len[level + 256]; + } +} -static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, - uint8_t *scan_table) +/** + * encodes a 8x8 block + * @param n block index (0-3 are luma, 4-5 are chroma) + */ +static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, + uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb) { int i, last_non_zero; +#if 0 //variables for the outcommented version + int code, sign, last; +#endif const RLTable *rl; + uint32_t *bits_tab; uint8_t *len_tab; const int last_index = s->block_last_index[n]; - int len=0; if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away /* mpeg4 based DC predictor */ - //mpeg4_encode_dc(dc_pb, intra_dc, n); //FIXME - if(last_index<1) return len; + mpeg4_encode_dc(dc_pb, intra_dc, n); + if(last_index<1) return; i = 1; rl = &rl_intra; + bits_tab= uni_mpeg4_intra_rl_bits; len_tab = uni_mpeg4_intra_rl_len; } else { - if(last_index<0) return 0; + if(last_index<0) return; i = 0; rl = &rl_inter; + bits_tab= uni_mpeg4_inter_rl_bits; len_tab = uni_mpeg4_inter_rl_len; } /* AC coefs */ last_non_zero = i - 1; +#if 1 for (; i < last_index; i++) { int level = block[ scan_table[i] ]; if (level) { @@ -2711,9 +2714,9 @@ static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, in level+=64; if((level&(~127)) == 0){ const int index= UNI_MPEG4_ENC_INDEX(0, run, level); - len += len_tab[index]; + put_bits(ac_pb, len_tab[index], bits_tab[index]); }else{ //ESC3 - len += 7+2+1+6+1+12+1; + put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1); } last_non_zero = i; } @@ -2724,50 +2727,166 @@ static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, in level+=64; if((level&(~127)) == 0){ const int index= UNI_MPEG4_ENC_INDEX(1, run, level); - len += len_tab[index]; + put_bits(ac_pb, len_tab[index], bits_tab[index]); }else{ //ESC3 - len += 7+2+1+6+1+12+1; + put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1); } } - - return len; -} - -#endif - - -/***********************************************/ -/* decoding */ - -static VLC intra_MCBPC_vlc; -static VLC inter_MCBPC_vlc; -static VLC cbpy_vlc; -static VLC mv_vlc; -static VLC dc_lum, dc_chrom; -static VLC sprite_trajectory; -static VLC mb_type_b_vlc; -static VLC h263_mbtype_b_vlc; -static VLC cbpc_b_vlc; - -void init_vlc_rl(RLTable *rl) -{ - int i, q; - - init_vlc(&rl->vlc, 9, rl->n + 1, - &rl->table_vlc[0][1], 4, 2, - &rl->table_vlc[0][0], 4, 2); - - - for(q=0; q<32; q++){ - int qmul= q*2; - int qadd= (q-1)|1; - +#else + for (; i <= last_index; i++) { + const int slevel = block[ scan_table[i] ]; + if (slevel) { + int level; + int run = i - last_non_zero - 1; + last = (i == last_index); + sign = 0; + level = slevel; + if (level < 0) { + sign = 1; + level = -level; + } + code = get_rl_index(rl, last, run, level); + put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); + if (code == rl->n) { + int level1, run1; + level1 = level - rl->max_level[last][run]; + if (level1 < 1) + goto esc2; + code = get_rl_index(rl, last, run, level1); + if (code == rl->n) { + esc2: + put_bits(ac_pb, 1, 1); + if (level > MAX_LEVEL) + goto esc3; + run1 = run - rl->max_run[last][level] - 1; + if (run1 < 0) + goto esc3; + code = get_rl_index(rl, last, run1, level); + if (code == rl->n) { + esc3: + /* third escape */ + put_bits(ac_pb, 1, 1); + put_bits(ac_pb, 1, last); + put_bits(ac_pb, 6, run); + put_bits(ac_pb, 1, 1); + put_bits(ac_pb, 12, slevel & 0xfff); + put_bits(ac_pb, 1, 1); + } else { + /* second escape */ + put_bits(ac_pb, 1, 0); + put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); + put_bits(ac_pb, 1, sign); + } + } else { + /* first escape */ + put_bits(ac_pb, 1, 0); + put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); + put_bits(ac_pb, 1, sign); + } + } else { + put_bits(ac_pb, 1, sign); + } + last_non_zero = i; + } + } +#endif +} + +static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, + uint8_t *scan_table) +{ + int i, last_non_zero; + const RLTable *rl; + uint8_t *len_tab; + const int last_index = s->block_last_index[n]; + int len=0; + + if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away + /* mpeg4 based DC predictor */ + len += mpeg4_get_dc_length(intra_dc, n); + if(last_index<1) return len; + i = 1; + rl = &rl_intra; + len_tab = uni_mpeg4_intra_rl_len; + } else { + if(last_index<0) return 0; + i = 0; + rl = &rl_inter; + len_tab = uni_mpeg4_inter_rl_len; + } + + /* AC coefs */ + last_non_zero = i - 1; + for (; i < last_index; i++) { + int level = block[ scan_table[i] ]; + if (level) { + int run = i - last_non_zero - 1; + level+=64; + if((level&(~127)) == 0){ + const int index= UNI_MPEG4_ENC_INDEX(0, run, level); + len += len_tab[index]; + }else{ //ESC3 + len += 7+2+1+6+1+12+1; + } + last_non_zero = i; + } + } + /*if(i<=last_index)*/{ + int level = block[ scan_table[i] ]; + int run = i - last_non_zero - 1; + level+=64; + if((level&(~127)) == 0){ + const int index= UNI_MPEG4_ENC_INDEX(1, run, level); + len += len_tab[index]; + }else{ //ESC3 + len += 7+2+1+6+1+12+1; + } + } + + return len; +} + +#endif + + +/***********************************************/ +/* decoding */ + +static VLC intra_MCBPC_vlc; +static VLC inter_MCBPC_vlc; +static VLC cbpy_vlc; +static VLC mv_vlc; +static VLC dc_lum, dc_chrom; +static VLC sprite_trajectory; +static VLC mb_type_b_vlc; +static VLC h263_mbtype_b_vlc; +static VLC cbpc_b_vlc; + +void init_vlc_rl(RLTable *rl, int use_static) +{ + int i, q; + + /* Return if static table is already initialized */ + if(use_static && rl->rl_vlc[0]) + return; + + init_vlc(&rl->vlc, 9, rl->n + 1, + &rl->table_vlc[0][1], 4, 2, + &rl->table_vlc[0][0], 4, 2, use_static); + + + for(q=0; q<32; q++){ + int qmul= q*2; + int qadd= (q-1)|1; + if(q==0){ qmul=1; qadd=0; } - - rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); + if(use_static) + rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); + else + rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); for(i=0; ivlc.table_size; i++){ int code= rl->vlc.table[i][0]; int len = rl->vlc.table[i][1]; @@ -2808,44 +2927,44 @@ void h263_decode_init_vlc(MpegEncContext *s) init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9, intra_MCBPC_bits, 1, 1, - intra_MCBPC_code, 1, 1); + intra_MCBPC_code, 1, 1, 1); init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28, inter_MCBPC_bits, 1, 1, - inter_MCBPC_code, 1, 1); + inter_MCBPC_code, 1, 1, 1); init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, &cbpy_tab[0][1], 2, 1, - &cbpy_tab[0][0], 2, 1); + &cbpy_tab[0][0], 2, 1, 1); init_vlc(&mv_vlc, MV_VLC_BITS, 33, &mvtab[0][1], 2, 1, - &mvtab[0][0], 2, 1); - init_rl(&rl_inter); - init_rl(&rl_intra); - init_rl(&rvlc_rl_inter); - init_rl(&rvlc_rl_intra); - init_rl(&rl_intra_aic); - init_vlc_rl(&rl_inter); - init_vlc_rl(&rl_intra); - init_vlc_rl(&rvlc_rl_inter); - init_vlc_rl(&rvlc_rl_intra); - init_vlc_rl(&rl_intra_aic); + &mvtab[0][0], 2, 1, 1); + init_rl(&rl_inter, 1); + init_rl(&rl_intra, 1); + init_rl(&rvlc_rl_inter, 1); + init_rl(&rvlc_rl_intra, 1); + init_rl(&rl_intra_aic, 1); + init_vlc_rl(&rl_inter, 1); + init_vlc_rl(&rl_intra, 1); + init_vlc_rl(&rvlc_rl_inter, 1); + init_vlc_rl(&rvlc_rl_intra, 1); + init_vlc_rl(&rl_intra_aic, 1); init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */, &DCtab_lum[0][1], 2, 1, - &DCtab_lum[0][0], 2, 1); + &DCtab_lum[0][0], 2, 1, 1); init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */, &DCtab_chrom[0][1], 2, 1, - &DCtab_chrom[0][0], 2, 1); + &DCtab_chrom[0][0], 2, 1, 1); init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, &sprite_trajectory_tab[0][1], 4, 2, - &sprite_trajectory_tab[0][0], 4, 2); + &sprite_trajectory_tab[0][0], 4, 2, 1); init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, &mb_type_b_tab[0][1], 2, 1, - &mb_type_b_tab[0][0], 2, 1); + &mb_type_b_tab[0][0], 2, 1, 1); init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15, &h263_mbtype_b_tab[0][1], 2, 1, - &h263_mbtype_b_tab[0][0], 2, 1); + &h263_mbtype_b_tab[0][0], 2, 1, 1); init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4, &cbpc_b_tab[0][1], 2, 1, - &cbpc_b_tab[0][0], 2, 1); + &cbpc_b_tab[0][0], 2, 1, 1); } } @@ -2955,7 +3074,7 @@ void ff_mpeg4_init_partitions(MpegEncContext *s) uint8_t *start= pbBufPtr(&s->pb); uint8_t *end= s->pb.buf_end; int size= end - start; - int pb_size = (((int)start + size/3)&(~3)) - (int)start; + int pb_size = (((long)start + size/3)&(~3)) - (long)start; int tex_size= (size - 2*pb_size)&(~3); set_put_bits_buffer_size(&s->pb, pb_size); @@ -3273,53 +3392,6 @@ static inline int get_amv(MpegEncContext *s, int n){ return sum; } -/** - * decodes the dc value. - * @param n block index (0-3 are luma, 4-5 are chroma) - * @param dir_ptr the prediction direction will be stored here - * @return the quantized dc - */ -static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) -{ - int level, code; - - if (n < 4) - code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1); - else - code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1); - if (code < 0 || code > 9 /* && s->nbit<9 */){ - av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); - return -1; - } - if (code == 0) { - level = 0; - } else { - if(IS_3IV1){ - if(code==1) - level= 2*get_bits1(&s->gb)-1; - else{ - if(get_bits1(&s->gb)) - level = get_bits(&s->gb, code-1) + (1<<(code-1)); - else - level = -get_bits(&s->gb, code-1) - (1<<(code-1)); - } - }else{ - level = get_xbits(&s->gb, code); - } - - if (code > 8){ - if(get_bits1(&s->gb)==0){ /* marker */ - if(s->error_resilience>=2){ - av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n"); - return -1; - } - } - } - } - - return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0); -} - /** * decodes first partition. * @return number of MBs decoded or <0 if an error occured @@ -3572,314 +3644,57 @@ static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){ return 0; } -/** - * decodes the first & second partition - * @return <0 if error (and sets error type in the error_status_table) - */ -int ff_mpeg4_decode_partitions(MpegEncContext *s) -{ - int mb_num; - const int part_a_error= s->pict_type==I_TYPE ? (DC_ERROR|MV_ERROR) : MV_ERROR; - const int part_a_end = s->pict_type==I_TYPE ? (DC_END |MV_END) : MV_END; - - mb_num= mpeg4_decode_partition_a(s); - if(mb_num<0){ - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error); - return -1; - } - - if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){ - av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n"); - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error); - return -1; - } - - s->mb_num_left= mb_num; - - if(s->pict_type==I_TYPE){ - while(show_bits(&s->gb, 9) == 1) - skip_bits(&s->gb, 9); - if(get_bits_long(&s->gb, 19)!=DC_MARKER){ - av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y); - return -1; - } - }else{ - while(show_bits(&s->gb, 10) == 1) - skip_bits(&s->gb, 10); - if(get_bits(&s->gb, 17)!=MOTION_MARKER){ - av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y); - return -1; - } - } - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end); - - if( mpeg4_decode_partition_b(s, mb_num) < 0){ - if(s->pict_type==P_TYPE) - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, DC_ERROR); - return -1; - }else{ - if(s->pict_type==P_TYPE) - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, DC_END); - } - - return 0; -} - -/** - * decodes a block. - * @return <0 if an error occured - */ -static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, - int n, int coded, int intra, int rvlc) -{ - int level, i, last, run; - int dc_pred_dir; - RLTable * rl; - RL_VLC_ELEM * rl_vlc; - const uint8_t * scan_table; - int qmul, qadd; - - //Note intra & rvlc should be optimized away if this is inlined - - if(intra) { - if(s->qscale < s->intra_dc_threshold){ - /* DC coef */ - if(s->partitioned_frame){ - level = s->dc_val[0][ s->block_index[n] ]; - if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale); - else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale); - dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<ac_pred) { - if (dc_pred_dir == 0) - scan_table = s->intra_v_scantable.permutated; /* left */ - else - scan_table = s->intra_h_scantable.permutated; /* top */ - } else { - scan_table = s->intra_scantable.permutated; - } - qmul=1; - qadd=0; - } else { - i = -1; - if (!coded) { - s->block_last_index[n] = i; - return 0; - } - if(rvlc) rl = &rvlc_rl_inter; - else rl = &rl_inter; - - scan_table = s->intra_scantable.permutated; - - if(s->mpeg_quant){ - qmul=1; - qadd=0; - if(rvlc){ - rl_vlc = rvlc_rl_inter.rl_vlc[0]; - }else{ - rl_vlc = rl_inter.rl_vlc[0]; - } - }else{ - qmul = s->qscale << 1; - qadd = (s->qscale - 1) | 1; - if(rvlc){ - rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale]; - }else{ - rl_vlc = rl_inter.rl_vlc[s->qscale]; - } - } - } - { - OPEN_READER(re, &s->gb); - for(;;) { - UPDATE_CACHE(re, &s->gb); - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); - if (level==0) { - /* escape */ - if(rvlc){ - if(SHOW_UBITS(re, &s->gb, 1)==0){ - av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n"); - return -1; - }; SKIP_CACHE(re, &s->gb, 1); - - last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); - run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); - SKIP_COUNTER(re, &s->gb, 1+1+6); - UPDATE_CACHE(re, &s->gb); - - if(SHOW_UBITS(re, &s->gb, 1)==0){ - av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n"); - return -1; - }; SKIP_CACHE(re, &s->gb, 1); - - level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11); - - if(SHOW_UBITS(re, &s->gb, 5)!=0x10){ - av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n"); - return -1; - }; SKIP_CACHE(re, &s->gb, 5); - - level= level * qmul + qadd; - level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1); - SKIP_COUNTER(re, &s->gb, 1+11+5+1); - - i+= run + 1; - if(last) i+=192; - }else{ - int cache; - cache= GET_CACHE(re, &s->gb); - - if(IS_3IV1) - cache ^= 0xC0000000; - - if (cache&0x80000000) { - if (cache&0x40000000) { - int ulevel; - - /* third escape */ - SKIP_CACHE(re, &s->gb, 2); - last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); - run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); - SKIP_COUNTER(re, &s->gb, 2+1+6); - UPDATE_CACHE(re, &s->gb); - - if(IS_3IV1){ - level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12); - }else{ - if(SHOW_UBITS(re, &s->gb, 1)==0){ - av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n"); - return -1; - }; SKIP_CACHE(re, &s->gb, 1); - - level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12); - - if(SHOW_UBITS(re, &s->gb, 1)==0){ - av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n"); - return -1; - }; LAST_SKIP_CACHE(re, &s->gb, 1); - - SKIP_COUNTER(re, &s->gb, 1+12+1); - } - - if(s->mpeg_quant){ - if(intra) ulevel= level*s->qscale*s->intra_matrix[scan_table[1]]; - else ulevel= level*s->qscale*s->inter_matrix[scan_table[0]]; - }else - ulevel= level*s->qscale*16; - if(ulevel>1030*16 || ulevel<-1030*16){ - av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale); - return -1; - } - -#if 0 - if(s->error_resilience >= FF_ER_COMPLIANT){ - const int abs_level= ABS(level); - if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ - const int run1= run - rl->max_run[last][abs_level] - 1; - if(abs_level <= rl->max_level[last][run]){ - av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); - return -1; - } - if(s->error_resilience > FF_ER_COMPLIANT){ - if(abs_level <= rl->max_level[last][run]*2){ - fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); - return -1; - } - if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){ - fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); - return -1; - } - } - } - } -#endif - if (level>0) level= level * qmul + qadd; - else level= level * qmul - qadd; - - i+= run + 1; - if(last) i+=192; - } else { - /* second escape */ -#if MIN_CACHE_BITS < 20 - LAST_SKIP_BITS(re, &s->gb, 2); - UPDATE_CACHE(re, &s->gb); -#else - SKIP_BITS(re, &s->gb, 2); -#endif - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); - i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing - level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); - LAST_SKIP_BITS(re, &s->gb, 1); - } - } else { - /* first escape */ -#if MIN_CACHE_BITS < 19 - LAST_SKIP_BITS(re, &s->gb, 1); - UPDATE_CACHE(re, &s->gb); -#else - SKIP_BITS(re, &s->gb, 1); -#endif - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); - i+= run; - level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing - level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); - LAST_SKIP_BITS(re, &s->gb, 1); - } - } - } else { - i+= run; - level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); - LAST_SKIP_BITS(re, &s->gb, 1); - } - if (i > 62){ - i-= 192; - if(i&(~63)){ - av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); - return -1; - } - - block[scan_table[i]] = level; - break; - } - - block[scan_table[i]] = level; +/** + * decodes the first & second partition + * @return <0 if error (and sets error type in the error_status_table) + */ +int ff_mpeg4_decode_partitions(MpegEncContext *s) +{ + int mb_num; + const int part_a_error= s->pict_type==I_TYPE ? (DC_ERROR|MV_ERROR) : MV_ERROR; + const int part_a_end = s->pict_type==I_TYPE ? (DC_END |MV_END) : MV_END; + + mb_num= mpeg4_decode_partition_a(s); + if(mb_num<0){ + ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error); + return -1; + } + + if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){ + av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n"); + ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error); + return -1; } - CLOSE_READER(re, &s->gb); - } - not_coded: - if (intra) { - if(s->qscale >= s->intra_dc_threshold){ - block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0); - - if(i == -1) i=0; - } - mpeg4_pred_ac(s, block, n, dc_pred_dir); - if (s->ac_pred) { - i = 63; /* XXX: not optimal */ + s->mb_num_left= mb_num; + + if(s->pict_type==I_TYPE){ + while(show_bits(&s->gb, 9) == 1) + skip_bits(&s->gb, 9); + if(get_bits_long(&s->gb, 19)!=DC_MARKER){ + av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y); + return -1; + } + }else{ + while(show_bits(&s->gb, 10) == 1) + skip_bits(&s->gb, 10); + if(get_bits(&s->gb, 17)!=MOTION_MARKER){ + av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y); + return -1; } } - s->block_last_index[n] = i; - return 0; + ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end); + + if( mpeg4_decode_partition_b(s, mb_num) < 0){ + if(s->pict_type==P_TYPE) + ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, DC_ERROR); + return -1; + }else{ + if(s->pict_type==P_TYPE) + ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, DC_END); + } + + return 0; } /** @@ -4681,8 +4496,8 @@ static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) /* modulo decoding */ if (!s->h263_long_vectors) { - l = 1 << (f_code + 4); - val = ((val + l)&(l*2-1)) - l; + l = INT_BIT - 5 - f_code; + val = (val<>l; } else { /* horrible h263 long vector mode */ if (pred < -31 && val < -63) @@ -4755,103 +4570,401 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block, } else { s->rv10_first_dc_coded[component] = 1; } - } else { - level = get_bits(&s->gb, 8); - if (level == 255) - level = 128; - } + } else { + level = get_bits(&s->gb, 8); + if (level == 255) + level = 128; + } + }else{ + level = get_bits(&s->gb, 8); + if((level&0x7F) == 0){ + av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); + if(s->error_resilience >= FF_ER_COMPLIANT) + return -1; + } + if (level == 255) + level = 128; + } + block[0] = level; + i = 1; + } else { + i = 0; + } + if (!coded) { + if (s->mb_intra && s->h263_aic) + goto not_coded; + s->block_last_index[n] = i - 1; + return 0; + } +retry: + for(;;) { + code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); + if (code < 0){ + av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); + return -1; + } + if (code == rl->n) { + /* escape */ + if (s->h263_flv > 1) { + int is11 = get_bits1(&s->gb); + last = get_bits1(&s->gb); + run = get_bits(&s->gb, 6); + if(is11){ + level = get_sbits(&s->gb, 11); + } else { + level = get_sbits(&s->gb, 7); + } + } else { + last = get_bits1(&s->gb); + run = get_bits(&s->gb, 6); + level = (int8_t)get_bits(&s->gb, 8); + if(level == -128){ + if (s->codec_id == CODEC_ID_RV10) { + /* XXX: should patch encoder too */ + level = get_sbits(&s->gb, 12); + }else{ + level = get_bits(&s->gb, 5); + level |= get_sbits(&s->gb, 6)<<5; + } + } + } + } else { + run = rl->table_run[code]; + level = rl->table_level[code]; + last = code >= rl->last; + if (get_bits1(&s->gb)) + level = -level; + } + i += run; + if (i >= 64){ + if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){ + //looks like a hack but no, its the way its supposed to work ... + rl = &rl_intra_aic; + i = 0; + s->gb= gb; + memset(block, 0, sizeof(DCTELEM)*64); + goto retry; + } + av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra); + return -1; + } + j = scan_table[i]; + block[j] = level; + if (last) + break; + i++; + } +not_coded: + if (s->mb_intra && s->h263_aic) { + h263_pred_acdc(s, block, n); + i = 63; + } + s->block_last_index[n] = i; + return 0; +} + +/** + * decodes the dc value. + * @param n block index (0-3 are luma, 4-5 are chroma) + * @param dir_ptr the prediction direction will be stored here + * @return the quantized dc + */ +static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) +{ + int level, code; + + if (n < 4) + code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1); + else + code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1); + if (code < 0 || code > 9 /* && s->nbit<9 */){ + av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); + return -1; + } + if (code == 0) { + level = 0; + } else { + if(IS_3IV1){ + if(code==1) + level= 2*get_bits1(&s->gb)-1; + else{ + if(get_bits1(&s->gb)) + level = get_bits(&s->gb, code-1) + (1<<(code-1)); + else + level = -get_bits(&s->gb, code-1) - (1<<(code-1)); + } + }else{ + level = get_xbits(&s->gb, code); + } + + if (code > 8){ + if(get_bits1(&s->gb)==0){ /* marker */ + if(s->error_resilience>=2){ + av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n"); + return -1; + } + } + } + } + + return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0); +} + +/** + * decodes a block. + * @return <0 if an error occured + */ +static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, + int n, int coded, int intra, int rvlc) +{ + int level, i, last, run; + int dc_pred_dir; + RLTable * rl; + RL_VLC_ELEM * rl_vlc; + const uint8_t * scan_table; + int qmul, qadd; + + //Note intra & rvlc should be optimized away if this is inlined + + if(intra) { + if(s->qscale < s->intra_dc_threshold){ + /* DC coef */ + if(s->partitioned_frame){ + level = s->dc_val[0][ s->block_index[n] ]; + if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale); + else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale); + dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<ac_pred) { + if (dc_pred_dir == 0) + scan_table = s->intra_v_scantable.permutated; /* left */ + else + scan_table = s->intra_h_scantable.permutated; /* top */ + } else { + scan_table = s->intra_scantable.permutated; + } + qmul=1; + qadd=0; + } else { + i = -1; + if (!coded) { + s->block_last_index[n] = i; + return 0; + } + if(rvlc) rl = &rvlc_rl_inter; + else rl = &rl_inter; + + scan_table = s->intra_scantable.permutated; + + if(s->mpeg_quant){ + qmul=1; + qadd=0; + if(rvlc){ + rl_vlc = rvlc_rl_inter.rl_vlc[0]; + }else{ + rl_vlc = rl_inter.rl_vlc[0]; + } }else{ - level = get_bits(&s->gb, 8); - if((level&0x7F) == 0){ - av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); - if(s->error_resilience >= FF_ER_COMPLIANT) - return -1; + qmul = s->qscale << 1; + qadd = (s->qscale - 1) | 1; + if(rvlc){ + rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale]; + }else{ + rl_vlc = rl_inter.rl_vlc[s->qscale]; } - if (level == 255) - level = 128; } - block[0] = level; - i = 1; - } else { - i = 0; } - if (!coded) { - if (s->mb_intra && s->h263_aic) - goto not_coded; - s->block_last_index[n] = i - 1; - return 0; - } -retry: + { + OPEN_READER(re, &s->gb); for(;;) { - code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); - if (code < 0){ - av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); - return -1; - } - if (code == rl->n) { - /* escape */ - if (s->h263_flv > 1) { - int is11 = get_bits1(&s->gb); - last = get_bits1(&s->gb); - run = get_bits(&s->gb, 6); - if(is11){ - level = get_sbits(&s->gb, 11); - } else { - level = get_sbits(&s->gb, 7); - } - } else { - last = get_bits1(&s->gb); - run = get_bits(&s->gb, 6); - level = (int8_t)get_bits(&s->gb, 8); - if(level == -128){ - if (s->codec_id == CODEC_ID_RV10) { - /* XXX: should patch encoder too */ - level = get_sbits(&s->gb, 12); + UPDATE_CACHE(re, &s->gb); + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); + if (level==0) { + /* escape */ + if(rvlc){ + if(SHOW_UBITS(re, &s->gb, 1)==0){ + av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n"); + return -1; + }; SKIP_CACHE(re, &s->gb, 1); + + last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); + run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); + SKIP_COUNTER(re, &s->gb, 1+1+6); + UPDATE_CACHE(re, &s->gb); + + if(SHOW_UBITS(re, &s->gb, 1)==0){ + av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n"); + return -1; + }; SKIP_CACHE(re, &s->gb, 1); + + level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11); + + if(SHOW_UBITS(re, &s->gb, 5)!=0x10){ + av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n"); + return -1; + }; SKIP_CACHE(re, &s->gb, 5); + + level= level * qmul + qadd; + level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1); + SKIP_COUNTER(re, &s->gb, 1+11+5+1); + + i+= run + 1; + if(last) i+=192; + }else{ + int cache; + cache= GET_CACHE(re, &s->gb); + + if(IS_3IV1) + cache ^= 0xC0000000; + + if (cache&0x80000000) { + if (cache&0x40000000) { + /* third escape */ + SKIP_CACHE(re, &s->gb, 2); + last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); + run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6); + SKIP_COUNTER(re, &s->gb, 2+1+6); + UPDATE_CACHE(re, &s->gb); + + if(IS_3IV1){ + level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12); }else{ - level = get_bits(&s->gb, 5); - level |= get_sbits(&s->gb, 6)<<5; + if(SHOW_UBITS(re, &s->gb, 1)==0){ + av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n"); + return -1; + }; SKIP_CACHE(re, &s->gb, 1); + + level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12); + + if(SHOW_UBITS(re, &s->gb, 1)==0){ + av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n"); + return -1; + }; LAST_SKIP_CACHE(re, &s->gb, 1); + + SKIP_COUNTER(re, &s->gb, 1+12+1); + } + +#if 0 + if(s->error_resilience >= FF_ER_COMPLIANT){ + const int abs_level= ABS(level); + if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ + const int run1= run - rl->max_run[last][abs_level] - 1; + if(abs_level <= rl->max_level[last][run]){ + av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); + return -1; + } + if(s->error_resilience > FF_ER_COMPLIANT){ + if(abs_level <= rl->max_level[last][run]*2){ + fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); + return -1; + } + if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){ + fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); + return -1; + } + } + } } +#endif + if (level>0) level= level * qmul + qadd; + else level= level * qmul - qadd; + + if((unsigned)(level + 2048) > 4095){ + if(s->error_resilience > FF_ER_COMPLIANT){ + if(level > 2560 || level<-2560){ + av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale); + return -1; + } + } + level= level<0 ? -2048 : 2047; + } + + i+= run + 1; + if(last) i+=192; + } else { + /* second escape */ +#if MIN_CACHE_BITS < 20 + LAST_SKIP_BITS(re, &s->gb, 2); + UPDATE_CACHE(re, &s->gb); +#else + SKIP_BITS(re, &s->gb, 2); +#endif + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); + i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing + level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); + LAST_SKIP_BITS(re, &s->gb, 1); } + } else { + /* first escape */ +#if MIN_CACHE_BITS < 19 + LAST_SKIP_BITS(re, &s->gb, 1); + UPDATE_CACHE(re, &s->gb); +#else + SKIP_BITS(re, &s->gb, 1); +#endif + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); + i+= run; + level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing + level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); + LAST_SKIP_BITS(re, &s->gb, 1); } + } } else { - run = rl->table_run[code]; - level = rl->table_level[code]; - last = code >= rl->last; - if (get_bits1(&s->gb)) - level = -level; + i+= run; + level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); + LAST_SKIP_BITS(re, &s->gb, 1); } - i += run; - if (i >= 64){ - if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){ - //looks like a hack but no, its the way its supposed to work ... - rl = &rl_intra_aic; - i = 0; - s->gb= gb; - memset(block, 0, sizeof(DCTELEM)*64); - goto retry; + if (i > 62){ + i-= 192; + if(i&(~63)){ + av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); + return -1; } - av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y); - return -1; - } - j = scan_table[i]; - block[j] = level; - if (last) + + block[scan_table[i]] = level; break; - i++; + } + + block[scan_table[i]] = level; } -not_coded: - if (s->mb_intra && s->h263_aic) { - h263_pred_acdc(s, block, n); - i = 63; + CLOSE_READER(re, &s->gb); + } + not_coded: + if (intra) { + if(s->qscale >= s->intra_dc_threshold){ + block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0); + + if(i == -1) i=0; + } + + mpeg4_pred_ac(s, block, n, dc_pred_dir); + if (s->ac_pred) { + i = 63; /* XXX: not optimal */ + } } s->block_last_index[n] = i; return 0; } - - - - /* most is hardcoded. should extend to handle all h263 streams */ int h263_decode_picture_header(MpegEncContext *s) { @@ -5434,7 +5547,7 @@ static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){ skip_bits1(gb); /* marker */ height = get_bits(gb, 13); skip_bits1(gb); /* marker */ - if(width && height){ /* they should be non zero but who knows ... */ + if(width && height && !(s->width && s->avctx->codec_tag == ff_get_fourcc("MP4S"))){ /* they should be non zero but who knows ... */ s->width = width; s->height = height; // printf("width/height: %d %d\n", width, height); @@ -5738,7 +5851,8 @@ static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){ /* vop coded */ if (get_bits1(gb) != 1){ - av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n"); + if(s->avctx->debug&FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n"); return FRAME_SKIPED; } //printf("time %d %d %d || %Ld %Ld %Ld\n", s->time_increment_bits, s->time_increment_resolution, s->time_base, @@ -5874,13 +5988,20 @@ int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb) /* search next start code */ align_get_bits(gb); + + if(s->avctx->codec_tag == ff_get_fourcc("WV1F") && show_bits(gb, 24) == 0x575630){ + skip_bits(gb, 24); + if(get_bits(gb, 8) == 0xF0) + return decode_vop_header(s, gb); + } + startcode = 0xff; for(;;) { v = get_bits(gb, 8); startcode = ((startcode << 8) | v) & 0xffffffff; if(get_bits_count(gb) >= gb->size_in_bits){ - if(gb->size_in_bits==8 && s->divx_version){ + if(gb->size_in_bits==8 && (s->divx_version || s->xvid_build)){ av_log(s->avctx, AV_LOG_ERROR, "frame skip %d\n", gb->size_in_bits); return FRAME_SKIPED; //divx bug }else @@ -6059,7 +6180,7 @@ int flv_h263_decode_picture_header(MpegEncContext *s) width = height = 0; break; } - if ((width == 0) || (height == 0)) + if(avcodec_check_dimensions(s->avctx, width, height)) return -1; s->width = width; s->height = height; @@ -6068,7 +6189,7 @@ int flv_h263_decode_picture_header(MpegEncContext *s) s->dropable= s->pict_type > P_TYPE; if (s->dropable) s->pict_type = P_TYPE; - + skip_bits1(&s->gb); /* deblocking flag */ s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); @@ -6085,7 +6206,7 @@ int flv_h263_decode_picture_header(MpegEncContext *s) if(s->avctx->debug & FF_DEBUG_PICT_INFO){ av_log(s->avctx, AV_LOG_DEBUG, "%c esc_type:%d, qp:%d num:%d\n", - av_get_pict_type_char(s->pict_type), s->h263_flv-1, s->qscale, s->picture_number); + s->dropable ? 'D' : av_get_pict_type_char(s->pict_type), s->h263_flv-1, s->qscale, s->picture_number); } s->y_dc_scale_table= diff --git a/src/libffmpeg/libavcodec/h263dec.c b/src/libffmpeg/libavcodec/h263dec.c index 1ffefa1b2..da2bd54a2 100644 --- a/src/libffmpeg/libavcodec/h263dec.c +++ b/src/libffmpeg/libavcodec/h263dec.c @@ -37,8 +37,8 @@ int ff_h263_decode_init(AVCodecContext *avctx) s->avctx = avctx; s->out_format = FMT_H263; - s->width = avctx->width; - s->height = avctx->height; + s->width = avctx->coded_width; + s->height = avctx->coded_height; s->workaround_bugs= avctx->workaround_bugs; // set defaults @@ -85,6 +85,11 @@ int ff_h263_decode_init(AVCodecContext *avctx) s->h263_pred = 1; s->msmpeg4_version=5; break; + case CODEC_ID_WMV3: + s->h263_msmpeg4 = 1; + s->h263_pred = 1; + s->msmpeg4_version=6; + break; case CODEC_ID_H263I: break; case CODEC_ID_FLV1: @@ -139,6 +144,7 @@ static int get_consumed_bytes(MpegEncContext *s, int buf_size){ static int decode_slice(MpegEncContext *s){ const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; + const int mb_size= 16>>s->avctx->lowres; s->last_resync_gb= s->gb; s->first_slice_line= 1; @@ -214,7 +220,7 @@ static int decode_slice(MpegEncContext *s){ if(++s->mb_x >= s->mb_width){ s->mb_x=0; - ff_draw_horiz_band(s, s->mb_y*16, 16); + ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); s->mb_y++; } return 0; @@ -234,7 +240,7 @@ static int decode_slice(MpegEncContext *s){ ff_h263_loop_filter(s); } - ff_draw_horiz_band(s, s->mb_y*16, 16); + ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); s->mb_x= 0; } @@ -254,18 +260,25 @@ static int decode_slice(MpegEncContext *s){ if(bits_left==0){ s->padding_bug_score+=16; - }else if(bits_left>8){ - s->padding_bug_score++; } else if(bits_left != 1){ int v= show_bits(&s->gb, 8); v|= 0x7F >> (7-(bits_count&7)); - if(v==0x7F) + if(v==0x7F && bits_left<=8) s->padding_bug_score--; + else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16) + s->padding_bug_score+= 4; else s->padding_bug_score++; } } + + if(s->workaround_bugs&FF_BUG_AUTODETECT){ + if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version || !s->resync_marker)*/) + s->workaround_bugs |= FF_BUG_NO_PADDING; + else + s->workaround_bugs &= ~FF_BUG_NO_PADDING; + } // handle formats which dont have unique end markers if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly @@ -515,13 +528,13 @@ retry: if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0) s->divx_version= 400; //divx 4 } + + if(s->xvid_build && s->divx_version){ + s->divx_version= + s->divx_build= 0; + } if(s->workaround_bugs&FF_BUG_AUTODETECT){ - s->workaround_bugs &= ~FF_BUG_NO_PADDING; - - if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker)) - s->workaround_bugs |= FF_BUG_NO_PADDING; - if(s->avctx->codec_tag == ff_get_fourcc("XVIX")) s->workaround_bugs|= FF_BUG_XVID_ILACE; @@ -623,13 +636,21 @@ retry: fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale); } #endif - + +#ifdef HAVE_MMX + if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & MM_MMX) && !(s->flags&CODEC_FLAG_BITEXACT)){ + avctx->idct_algo= FF_IDCT_LIBMPEG2MMX; + avctx->coded_width= 0; // force reinit + } +#endif + /* After H263 & mpeg4 header decode we have the height, width,*/ /* and other parameters. So then we could init the picture */ /* FIXME: By the way H263 decoder is evolving it should have */ /* an H263EncContext */ - if ( s->width != avctx->width || s->height != avctx->height) { + if ( s->width != avctx->coded_width + || s->height != avctx->coded_height) { /* H.263 could change picture size any time */ ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat s->parse_context.buffer=0; @@ -637,8 +658,7 @@ retry: s->parse_context= pc; } if (!s->context_initialized) { - avctx->width = s->width; - avctx->height = s->height; + avcodec_set_dimensions(avctx, s->width, s->height); goto retry; } @@ -651,7 +671,7 @@ retry: s->current_picture.key_frame= s->pict_type == I_TYPE; /* skip b frames if we dont have reference frames */ - if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size); + if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size); /* skip b frames if we are in a hurry */ if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size); /* skip everything if we are in a hurry>=5 */ @@ -709,9 +729,8 @@ retry: if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){ int current_pos= get_bits_count(&s->gb)>>3; int startcode_found=0; - - if( buf_size - current_pos > 5 - && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){ + + if(buf_size - current_pos > 5){ int i; for(i=current_pos; ibitstream_buffer= av_fast_realloc( + s->bitstream_buffer, + &s->allocated_bitstream_buffer_size, + buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); s->bitstream_buffer_size= buf_size - current_pos; } @@ -760,12 +783,6 @@ printf("%Ld\n", rdtsc()-time); return get_consumed_bytes(s, buf_size); } -static const AVOption mpeg4_decoptions[] = -{ - AVOPTION_SUB(avoptions_workaround_bug), - AVOPTION_END() -}; - AVCodec mpeg4_decoder = { "mpeg4", CODEC_TYPE_VIDEO, @@ -775,8 +792,7 @@ AVCodec mpeg4_decoder = { NULL, ff_h263_decode_end, ff_h263_decode_frame, - CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, - .options = mpeg4_decoptions, + CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, .flush= ff_mpeg_flush, }; @@ -789,7 +805,7 @@ AVCodec h263_decoder = { NULL, ff_h263_decode_end, ff_h263_decode_frame, - CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, + CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, .flush= ff_mpeg_flush, }; @@ -803,7 +819,6 @@ AVCodec msmpeg4v1_decoder = { ff_h263_decode_end, ff_h263_decode_frame, CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, - mpeg4_decoptions, }; AVCodec msmpeg4v2_decoder = { @@ -816,7 +831,6 @@ AVCodec msmpeg4v2_decoder = { ff_h263_decode_end, ff_h263_decode_frame, CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, - mpeg4_decoptions, }; AVCodec msmpeg4v3_decoder = { @@ -829,7 +843,6 @@ AVCodec msmpeg4v3_decoder = { ff_h263_decode_end, ff_h263_decode_frame, CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, - .options = mpeg4_decoptions, }; AVCodec wmv1_decoder = { @@ -842,7 +855,6 @@ AVCodec wmv1_decoder = { ff_h263_decode_end, ff_h263_decode_frame, CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, - mpeg4_decoptions, }; AVCodec h263i_decoder = { @@ -855,7 +867,6 @@ AVCodec h263i_decoder = { ff_h263_decode_end, ff_h263_decode_frame, CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, - mpeg4_decoptions, }; AVCodec flv_decoder = { diff --git a/src/libffmpeg/libavcodec/h264.c b/src/libffmpeg/libavcodec/h264.c index 889900362..39ee4c6a2 100644 --- a/src/libffmpeg/libavcodec/h264.c +++ b/src/libffmpeg/libavcodec/h264.c @@ -87,6 +87,8 @@ typedef struct SPS{ uint32_t time_scale; int fixed_frame_rate_flag; short offset_for_ref_frame[256]; //FIXME dyn aloc? + int bitstream_restriction_flag; + int num_reorder_frames; }SPS; /** @@ -151,10 +153,12 @@ typedef struct H264Context{ uint8_t *rbsp_buffer; int rbsp_buffer_size; - // AVC - int is_avc; // != 0 if data is avc variant of h264 - int got_avcC; // flag to parse avcC data only once - int nal_length_size; // Number of bytes used for nal length (1, 2 or 4) + /** + * Used to parse AVC variant of h264 + */ + int is_avc; ///< this flag is != 0 if codec is avc1 + int got_avcC; ///< flag used to parse avcC data only once + int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4) int chroma_qp; //QPc @@ -163,6 +167,9 @@ typedef struct H264Context{ //prediction stuff int chroma_pred_mode; int intra16x16_pred_mode; + + int top_mb_xy; + int left_mb_xy[2]; int8_t intra4x4_pred_mode_cache[5*8]; int8_t (*intra4x4_pred_mode)[8]; @@ -173,21 +180,21 @@ typedef struct H264Context{ unsigned int top_samples_available; unsigned int topright_samples_available; unsigned int left_samples_available; - uint8_t (*top_border)[16+2*8]; - uint8_t left_border[17+2*9]; + uint8_t (*top_borders[2])[16+2*8]; + uint8_t left_border[2*(17+2*9)]; /** * non zero coeff count cache. * is 64 if not available. */ - uint8_t non_zero_count_cache[6*8]; + uint8_t non_zero_count_cache[6*8] __align8; uint8_t (*non_zero_count)[16]; /** * Motion vector cache. */ - int16_t mv_cache[2][5*8][2]; - int8_t ref_cache[2][5*8]; + int16_t mv_cache[2][5*8][2] __align8; + int8_t ref_cache[2][5*8] __align8; #define LIST_NOT_USED -1 //FIXME rename? #define PART_NOT_AVAILABLE -2 @@ -196,12 +203,15 @@ typedef struct H264Context{ */ int mv_cache_clean[2]; - int block_offset[16+8]; - int chroma_subblock_offset[16]; //FIXME remove + /** + * block_offset[ 0..23] for frame macroblocks + * block_offset[24..47] for field macroblocks + */ + int block_offset[2*(16+8)]; uint16_t *mb2b_xy; //FIXME are these 4 a good idea? uint16_t *mb2b8_xy; - int b_stride; + int b_stride; //FIXME use s->b4_stride int b8_stride; int halfpel_flag; @@ -226,6 +236,7 @@ typedef struct H264Context{ int slice_type_fixed; //interlacing specific flags + int mb_aff_frame; int mb_field_decoding_flag; int sub_mb_type[4]; @@ -253,12 +264,15 @@ typedef struct H264Context{ int max_pic_num; //Weighted pred stuff + int use_weight; + int use_weight_chroma; int luma_log2_weight_denom; int chroma_log2_weight_denom; int luma_weight[2][16]; int luma_offset[2][16]; int chroma_weight[2][16][2]; int chroma_offset[2][16][2]; + int implicit_weight[16][16]; //deblock int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0 @@ -268,16 +282,20 @@ typedef struct H264Context{ int redundant_pic_count; int direct_spatial_mv_pred; + int dist_scale_factor[16]; + int map_col_to_list0[2][16]; /** * num_ref_idx_l0/1_active_minus1 + 1 */ int ref_count[2];// FIXME split for AFF - Picture *short_ref[16]; - Picture *long_ref[16]; + Picture *short_ref[32]; + Picture *long_ref[32]; Picture default_ref_list[2][32]; Picture ref_list[2][32]; //FIXME size? Picture field_ref_list[2][32]; //FIXME size? + Picture *delayed_pic[16]; //FIXME size? + Picture *delayed_output_pic; /** * memory management control operations buffer. @@ -305,11 +323,15 @@ typedef struct H264Context{ /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */ uint16_t *cbp_table; + int top_cbp; + int left_cbp; /* chroma_pred_mode for i4x4 or i16x16, else 0 */ uint8_t *chroma_pred_mode_table; int last_qscale_diff; int16_t (*mvd_table[2])[2]; - int16_t mvd_cache[2][5*8][2]; + int16_t mvd_cache[2][5*8][2] __align8; + uint8_t *direct_table; + uint8_t direct_cache[5*8]; }H264Context; @@ -324,7 +346,7 @@ static VLC run7_vlc; static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp); static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc); -static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr); +static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize); static inline uint32_t pack16to32(int a, int b){ #ifdef WORDS_BIGENDIAN @@ -336,8 +358,8 @@ static inline uint32_t pack16to32(int a, int b){ /** * fill a rectangle. - * @param h height of the recatangle, should be a constant - * @param w width of the recatangle, should be a constant + * @param h height of the rectangle, should be a constant + * @param w width of the rectangle, should be a constant * @param size the size of val (1 or 4), should be a constant */ static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined @@ -347,6 +369,7 @@ static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t v w *= size; stride *= size; + assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0); //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it if(w==2 && h==2){ *(uint16_t*)(p + 0)= @@ -397,38 +420,110 @@ static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t v assert(0); } -static inline void fill_caches(H264Context *h, int mb_type){ +static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){ MpegEncContext * const s = &h->s; const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; int topleft_xy, top_xy, topright_xy, left_xy[2]; int topleft_type, top_type, topright_type, left_type[2]; - int left_block[4]; + int left_block[8]; int i; //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it - if(h->sps.mb_aff){ - //FIXME - topleft_xy = 0; /* avoid warning */ - top_xy = 0; /* avoid warning */ - topright_xy = 0; /* avoid warning */ + top_xy = mb_xy - s->mb_stride; + topleft_xy = top_xy - 1; + topright_xy= top_xy + 1; + left_xy[1] = left_xy[0] = mb_xy-1; + left_block[0]= 0; + left_block[1]= 1; + left_block[2]= 2; + left_block[3]= 3; + left_block[4]= 7; + left_block[5]= 10; + left_block[6]= 8; + left_block[7]= 11; + if(h->mb_aff_frame){ + const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride; + const int top_pair_xy = pair_xy - s->mb_stride; + const int topleft_pair_xy = top_pair_xy - 1; + const int topright_pair_xy = top_pair_xy + 1; + const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]); + const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]); + const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]); + const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]); + const int curr_mb_frame_flag = !IS_INTERLACED(mb_type); + const int bottom = (s->mb_y & 1); + tprintf("fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag); + if (bottom + ? !curr_mb_frame_flag // bottom macroblock + : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock + ) { + top_xy -= s->mb_stride; + } + if (bottom + ? !curr_mb_frame_flag // bottom macroblock + : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock + ) { + topleft_xy -= s->mb_stride; + } + if (bottom + ? !curr_mb_frame_flag // bottom macroblock + : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock + ) { + topright_xy -= s->mb_stride; + } + if (left_mb_frame_flag != curr_mb_frame_flag) { + left_xy[1] = left_xy[0] = pair_xy - 1; + if (curr_mb_frame_flag) { + if (bottom) { + left_block[0]= 2; + left_block[1]= 2; + left_block[2]= 3; + left_block[3]= 3; + left_block[4]= 8; + left_block[5]= 11; + left_block[6]= 8; + left_block[7]= 11; + } else { + left_block[0]= 0; + left_block[1]= 0; + left_block[2]= 1; + left_block[3]= 1; + left_block[4]= 7; + left_block[5]= 10; + left_block[6]= 7; + left_block[7]= 10; + } + } else { + left_xy[1] += s->mb_stride; + //left_block[0]= 0; + left_block[1]= 2; + left_block[2]= 0; + left_block[3]= 2; + //left_block[4]= 7; + left_block[5]= 10; + left_block[6]= 7; + left_block[7]= 10; + } + } + } + + h->top_mb_xy = top_xy; + h->left_mb_xy[0] = left_xy[0]; + h->left_mb_xy[1] = left_xy[1]; + if(for_deblock){ + topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0; + top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0; + topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0; + left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0; + left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0; }else{ - topleft_xy = mb_xy-1 - s->mb_stride; - top_xy = mb_xy - s->mb_stride; - topright_xy= mb_xy+1 - s->mb_stride; - left_xy[0] = mb_xy-1; - left_xy[1] = mb_xy-1; - left_block[0]= 0; - left_block[1]= 1; - left_block[2]= 2; - left_block[3]= 3; - } - - topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0; - top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0; - topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0; - left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0; - left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0; + topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0; + top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0; + topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0; + left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0; + left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0; + } if(IS_INTRA(mb_type)){ h->topleft_samples_available= @@ -462,10 +557,10 @@ static inline void fill_caches(H264Context *h, int mb_type){ h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3]; }else{ int pred; - if(IS_INTRA16x16(top_type) || (IS_INTER(top_type) && !h->pps.constrained_intra_pred)) - pred= 2; - else{ + if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred)) pred= -1; + else{ + pred= 2; } h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode_cache[5+8*0]= @@ -478,10 +573,10 @@ static inline void fill_caches(H264Context *h, int mb_type){ h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]]; }else{ int pred; - if(IS_INTRA16x16(left_type[i]) || (IS_INTER(left_type[i]) && !h->pps.constrained_intra_pred)) - pred= 2; - else{ + if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred)) pred= -1; + else{ + pred= 2; } h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred; @@ -501,16 +596,17 @@ static inline void fill_caches(H264Context *h, int mb_type){ */ //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec) if(top_type){ - h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][0]; - h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][1]; - h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][2]; + h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4]; + h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5]; + h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6]; h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3]; - h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][7]; + h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9]; h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8]; - h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][10]; + h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12]; h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11]; + }else{ h->non_zero_count_cache[4+8*0]= h->non_zero_count_cache[5+8*0]= @@ -521,44 +617,61 @@ static inline void fill_caches(H264Context *h, int mb_type){ h->non_zero_count_cache[2+8*0]= h->non_zero_count_cache[1+8*3]= - h->non_zero_count_cache[2+8*3]= 64; + h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64; + } - - if(left_type[0]){ - h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][6]; - h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][5]; - h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][9]; //FIXME left_block - h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][12]; - }else{ - h->non_zero_count_cache[3+8*1]= - h->non_zero_count_cache[3+8*2]= - h->non_zero_count_cache[0+8*1]= - h->non_zero_count_cache[0+8*4]= 64; + + for (i=0; i<2; i++) { + if(left_type[i]){ + h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]]; + h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]]; + h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]]; + h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]]; + }else{ + h->non_zero_count_cache[3+8*1 + 2*8*i]= + h->non_zero_count_cache[3+8*2 + 2*8*i]= + h->non_zero_count_cache[0+8*1 + 8*i]= + h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64; + } } - - if(left_type[1]){ - h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][4]; - h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][3]; - h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][8]; - h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][11]; - }else{ - h->non_zero_count_cache[3+8*3]= - h->non_zero_count_cache[3+8*4]= - h->non_zero_count_cache[0+8*2]= - h->non_zero_count_cache[0+8*5]= 64; + + if( h->pps.cabac ) { + // top_cbp + if(top_type) { + h->top_cbp = h->cbp_table[top_xy]; + } else if(IS_INTRA(mb_type)) { + h->top_cbp = 0x1C0; + } else { + h->top_cbp = 0; + } + // left_cbp + if (left_type[0]) { + h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0; + } else if(IS_INTRA(mb_type)) { + h->left_cbp = 0x1C0; + } else { + h->left_cbp = 0; + } + if (left_type[0]) { + h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1; + } + if (left_type[1]) { + h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3; + } } - + #if 1 - if(IS_INTER(mb_type)){ + //FIXME direct mb can skip much of this + if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){ int list; for(list=0; list<2; list++){ - if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){ + if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !for_deblock){ /*if(!h->mv_cache_clean[list]){ memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all? memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t)); h->mv_cache_clean[list]= 1; }*/ - continue; //FIXME direct mode ... + continue; } h->mv_cache_clean[list]= 0; @@ -630,6 +743,9 @@ static inline void fill_caches(H264Context *h, int mb_type){ h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE; } + if(for_deblock) + continue; + h->ref_cache[list][scan8[5 ]+1] = h->ref_cache[list][scan8[7 ]+1] = h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else) @@ -683,9 +799,35 @@ static inline void fill_caches(H264Context *h, int mb_type){ *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else) *(uint32_t*)h->mvd_cache [list][scan8[4 ]]= *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0; + + if(h->slice_type == B_TYPE){ + fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1); + + if(IS_DIRECT(top_type)){ + *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101; + }else if(IS_8X8(top_type)){ + int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride; + h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy]; + h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1]; + }else{ + *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0; + } + + //FIXME interlacing + if(IS_DIRECT(left_type[0])){ + h->direct_cache[scan8[0] - 1 + 0*8]= + h->direct_cache[scan8[0] - 1 + 2*8]= 1; + }else if(IS_8X8(left_type[0])){ + int b8_xy = h->mb2b8_xy[left_xy[0]] + 1; + h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy]; + h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride]; + }else{ + h->direct_cache[scan8[0] - 1 + 0*8]= + h->direct_cache[scan8[0] - 1 + 2*8]= 0; + } + } } } -//FIXME } #endif } @@ -747,8 +889,10 @@ static inline int check_intra_pred_mode(H264Context *h, int mode){ static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1}; static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8}; - if(mode < 0 || mode > 6) + if(mode < 0 || mode > 6) { + av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y); return -1; + } if(!(h->top_samples_available&0x8000)){ mode= top[ mode ]; @@ -788,21 +932,21 @@ static inline void write_back_non_zero_count(H264Context *h){ MpegEncContext * const s = &h->s; const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; - h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[4+8*4]; - h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[5+8*4]; - h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[6+8*4]; + h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1]; + h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2]; + h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3]; h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4]; - h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[7+8*3]; - h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[7+8*2]; - h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[7+8*1]; + h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4]; + h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4]; + h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4]; - h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[1+8*2]; + h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2]; h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2]; - h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[2+8*1]; + h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1]; - h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[1+8*5]; + h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5]; h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5]; - h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[2+8*4]; + h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4]; } /** @@ -864,6 +1008,7 @@ static inline void pred_motion(H264Context * const h, int n, int part_width, int diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width); match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref); + tprintf("pred_motion match_count=%d\n", match_count); if(match_count > 1){ //most common *mx= mid_pred(A[0], B[0], C[0]); *my= mid_pred(A[1], B[1], C[1]); @@ -902,7 +1047,7 @@ static inline void pred_16x8_motion(H264Context * const h, int n, int list, int const int top_ref= h->ref_cache[list][ scan8[0] - 8 ]; const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ]; - tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list); + tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list); if(top_ref == ref){ *mx= B[0]; @@ -913,7 +1058,7 @@ static inline void pred_16x8_motion(H264Context * const h, int n, int list, int const int left_ref= h->ref_cache[list][ scan8[8] - 1 ]; const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ]; - tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); + tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); if(left_ref == ref){ *mx= A[0]; @@ -937,7 +1082,7 @@ static inline void pred_8x16_motion(H264Context * const h, int n, int list, int const int left_ref= h->ref_cache[list][ scan8[0] - 1 ]; const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ]; - tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); + tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); if(left_ref == ref){ *mx= A[0]; @@ -950,7 +1095,7 @@ static inline void pred_8x16_motion(H264Context * const h, int n, int list, int diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2); - tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list); + tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list); if(diagonal_ref == ref){ *mx= C[0]; @@ -967,7 +1112,7 @@ static inline void pred_pskip_motion(H264Context * const h, int * const mx, int const int top_ref = h->ref_cache[0][ scan8[0] - 8 ]; const int left_ref= h->ref_cache[0][ scan8[0] - 1 ]; - tprintf("pred_pskip: (%d) (%d) at %2d %2d", top_ref, left_ref, h->s.mb_x, h->s.mb_y); + tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y); if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0) @@ -982,6 +1127,226 @@ static inline void pred_pskip_motion(H264Context * const h, int * const mx, int return; } +static inline void direct_dist_scale_factor(H264Context * const h){ + const int poc = h->s.current_picture_ptr->poc; + const int poc1 = h->ref_list[1][0].poc; + int i; + for(i=0; iref_count[0]; i++){ + int poc0 = h->ref_list[0][i].poc; + int td = clip(poc1 - poc0, -128, 127); + if(td == 0 /* FIXME || pic0 is a long-term ref */){ + h->dist_scale_factor[i] = 256; + }else{ + int tb = clip(poc - poc0, -128, 127); + int tx = (16384 + (ABS(td) >> 1)) / td; + h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023); + } + } +} +static inline void direct_ref_list_init(H264Context * const h){ + MpegEncContext * const s = &h->s; + Picture * const ref1 = &h->ref_list[1][0]; + Picture * const cur = s->current_picture_ptr; + int list, i, j; + if(cur->pict_type == I_TYPE) + cur->ref_count[0] = 0; + if(cur->pict_type != B_TYPE) + cur->ref_count[1] = 0; + for(list=0; list<2; list++){ + cur->ref_count[list] = h->ref_count[list]; + for(j=0; jref_count[list]; j++) + cur->ref_poc[list][j] = h->ref_list[list][j].poc; + } + if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred) + return; + for(list=0; list<2; list++){ + for(i=0; iref_count[list]; i++){ + const int poc = ref1->ref_poc[list][i]; + h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE; + for(j=0; jref_count[list]; j++) + if(h->ref_list[list][j].poc == poc){ + h->map_col_to_list0[list][i] = j; + break; + } + } + } +} + +static inline void pred_direct_motion(H264Context * const h, int *mb_type){ + MpegEncContext * const s = &h->s; + const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; + const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride; + const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; + const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy]; + const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy]; + const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy]; + const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy]; + const int is_b8x8 = IS_8X8(*mb_type); + int sub_mb_type; + int i8, i4; + + if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){ + /* FIXME save sub mb types from previous frames (or derive from MVs) + * so we know exactly what block size to use */ + sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */ + *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1; + }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){ + sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */ + *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */ + }else{ + sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */ + *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1; + } + if(!is_b8x8) + *mb_type |= MB_TYPE_DIRECT2; + + tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col); + + if(h->direct_spatial_mv_pred){ + int ref[2]; + int mv[2][2]; + int list; + + /* ref = min(neighbors) */ + for(list=0; list<2; list++){ + int refa = h->ref_cache[list][scan8[0] - 1]; + int refb = h->ref_cache[list][scan8[0] - 8]; + int refc = h->ref_cache[list][scan8[0] - 8 + 4]; + if(refc == -2) + refc = h->ref_cache[list][scan8[0] - 8 - 1]; + ref[list] = refa; + if(ref[list] < 0 || (refb < ref[list] && refb >= 0)) + ref[list] = refb; + if(ref[list] < 0 || (refc < ref[list] && refc >= 0)) + ref[list] = refc; + if(ref[list] < 0) + ref[list] = -1; + } + + if(ref[0] < 0 && ref[1] < 0){ + ref[0] = ref[1] = 0; + mv[0][0] = mv[0][1] = + mv[1][0] = mv[1][1] = 0; + }else{ + for(list=0; list<2; list++){ + if(ref[list] >= 0) + pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]); + else + mv[list][0] = mv[list][1] = 0; + } + } + + if(ref[1] < 0){ + *mb_type &= ~MB_TYPE_P0L1; + sub_mb_type &= ~MB_TYPE_P0L1; + }else if(ref[0] < 0){ + *mb_type &= ~MB_TYPE_P0L0; + sub_mb_type &= ~MB_TYPE_P0L0; + } + + if(IS_16X16(*mb_type)){ + fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1); + fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1); + if(!IS_INTRA(mb_type_col) && l1ref0[0] == 0 && + ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1){ + if(ref[0] > 0) + fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4); + else + fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4); + if(ref[1] > 0) + fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4); + else + fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4); + }else{ + fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4); + fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4); + } + }else{ + for(i8=0; i8<4; i8++){ + const int x8 = i8&1; + const int y8 = i8>>1; + + if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) + continue; + h->sub_mb_type[i8] = sub_mb_type; + + fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4); + fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4); + fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1); + fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1); + + /* col_zero_flag */ + if(!IS_INTRA(mb_type_col) && l1ref0[x8 + y8*h->b8_stride] == 0){ + for(i4=0; i4<4; i4++){ + const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride]; + if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){ + if(ref[0] == 0) + *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0; + if(ref[1] == 0) + *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0; + } + } + } + } + } + }else{ /* direct temporal mv pred */ + if(IS_16X16(*mb_type)){ + fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1); + if(IS_INTRA(mb_type_col)){ + fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); + fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4); + fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4); + }else{ + const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]] + : h->map_col_to_list0[1][l1ref1[0]]; + const int dist_scale_factor = h->dist_scale_factor[ref0]; + const int16_t *mv_col = l1mv0[0]; + int mv_l0[2]; + mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8; + mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8; + fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1); + fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4); + fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4); + } + }else{ + for(i8=0; i8<4; i8++){ + const int x8 = i8&1; + const int y8 = i8>>1; + int ref0, dist_scale_factor; + + if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) + continue; + h->sub_mb_type[i8] = sub_mb_type; + if(IS_INTRA(mb_type_col)){ + fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1); + fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); + fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4); + fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); + continue; + } + + ref0 = l1ref0[x8 + y8*h->b8_stride]; + if(ref0 >= 0) + ref0 = h->map_col_to_list0[0][ref0]; + else + ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]]; + dist_scale_factor = h->dist_scale_factor[ref0]; + + fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1); + fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); + for(i4=0; i4<4; i4++){ + const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride]; + int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]]; + mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8; + mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8; + *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = + pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]); + } + } + } + } +} + static inline void write_back_motion(H264Context *h, int mb_type){ MpegEncContext * const s = &h->s; const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; @@ -990,7 +1355,7 @@ static inline void write_back_motion(H264Context *h, int mb_type){ for(list=0; list<2; list++){ int y; - if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){ + if(!USES_LIST(mb_type, list)){ if(1){ //FIXME skip or never read if mb_type doesnt use it for(y=0; y<4; y++){ *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= @@ -1004,10 +1369,10 @@ static inline void write_back_motion(H264Context *h, int mb_type){ } } for(y=0; y<2; y++){ - *(uint16_t*)s->current_picture.motion_val[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101; + *(uint16_t*)&s->current_picture.ref_index[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101; } } - continue; //FIXME direct mode ... + continue; } for(y=0; y<4; y++){ @@ -1025,6 +1390,14 @@ static inline void write_back_motion(H264Context *h, int mb_type){ s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y]; } } + + if(h->slice_type == B_TYPE && h->pps.cabac){ + if(IS_8X8(mb_type)){ + h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0; + h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0; + h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0; + } + } } /** @@ -1315,46 +1688,12 @@ static void chroma_dc_dct_c(DCTELEM *block){ /** * gets the chroma qp. */ -static inline int get_chroma_qp(H264Context *h, int qscale){ +static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){ - return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)]; + return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)]; } -/** - * - */ -static void h264_add_idct_c(uint8_t *dst, DCTELEM *block, int stride){ - int i; - uint8_t *cm = cropTbl + MAX_NEG_CROP; - - block[0] += 32; - - for(i=0; i<4; i++){ - const int z0= block[0 + 4*i] + block[2 + 4*i]; - const int z1= block[0 + 4*i] - block[2 + 4*i]; - const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i]; - const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1); - - block[0 + 4*i]= z0 + z3; - block[1 + 4*i]= z1 + z2; - block[2 + 4*i]= z1 - z2; - block[3 + 4*i]= z0 - z3; - } - - for(i=0; i<4; i++){ - const int z0= block[i + 4*0] + block[i + 4*2]; - const int z1= block[i + 4*0] - block[i + 4*2]; - const int z2= (block[i + 4*1]>>1) - block[i + 4*3]; - const int z3= block[i + 4*1] + (block[i + 4*3]>>1); - - dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ]; - dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ]; - dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ]; - dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ]; - } -} - #if 0 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){ int i; @@ -1998,7 +2337,7 @@ static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7); } -static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta, +static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int x_offset, int y_offset, qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put, @@ -2032,9 +2371,95 @@ static inline void mc_part(H264Context *h, int n, int square, int chroma_height, } } +static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta, + uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, + int x_offset, int y_offset, + qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put, + h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op, + h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg, + int list0, int list1){ + MpegEncContext * const s = &h->s; + + dest_y += 2*x_offset + 2*y_offset*s-> linesize; + dest_cb += x_offset + y_offset*s->uvlinesize; + dest_cr += x_offset + y_offset*s->uvlinesize; + x_offset += 8*s->mb_x; + y_offset += 8*s->mb_y; + + if(list0 && list1){ + /* don't optimize for luma-only case, since B-frames usually + * use implicit weights => chroma too. */ + uint8_t *tmp_cb = s->obmc_scratchpad; + uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize; + uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize; + int refn0 = h->ref_cache[0][ scan8[n] ]; + int refn1 = h->ref_cache[1][ scan8[n] ]; + + mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0, + dest_y, dest_cb, dest_cr, + x_offset, y_offset, qpix_put, chroma_put); + mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1, + tmp_y, tmp_cb, tmp_cr, + x_offset, y_offset, qpix_put, chroma_put); + + if(h->use_weight == 2){ + int weight0 = h->implicit_weight[refn0][refn1]; + int weight1 = 64 - weight0; + luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0, 0); + chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0); + chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0); + }else{ + luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom, + h->luma_weight[0][refn0], h->luma_weight[1][refn1], + h->luma_offset[0][refn0], h->luma_offset[1][refn1]); + chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom, + h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0], + h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]); + chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom, + h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1], + h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]); + } + }else{ + int list = list1 ? 1 : 0; + int refn = h->ref_cache[list][ scan8[n] ]; + Picture *ref= &h->ref_list[list][refn]; + mc_dir_part(h, ref, n, square, chroma_height, delta, list, + dest_y, dest_cb, dest_cr, x_offset, y_offset, + qpix_put, chroma_put); + + luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom, + h->luma_weight[list][refn], h->luma_offset[list][refn]); + if(h->use_weight_chroma){ + chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom, + h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]); + chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom, + h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]); + } + } +} + +static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta, + uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, + int x_offset, int y_offset, + qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put, + qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg, + h264_weight_func *weight_op, h264_biweight_func *weight_avg, + int list0, int list1){ + if((h->use_weight==2 && list0 && list1 + && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32)) + || h->use_weight==1) + mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr, + x_offset, y_offset, qpix_put, chroma_put, + weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1); + else + mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr, + x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1); +} + static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put), - qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){ + qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg), + h264_weight_func *weight_op, h264_biweight_func *weight_avg){ MpegEncContext * const s = &h->s; const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; const int mb_type= s->current_picture.mb_type[mb_xy]; @@ -2044,20 +2469,25 @@ static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t if(IS_16X16(mb_type)){ mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0, qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0], + &weight_op[0], &weight_avg[0], IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); }else if(IS_16X8(mb_type)){ mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0, qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], + &weight_op[1], &weight_avg[1], IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4, qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], + &weight_op[1], &weight_avg[1], IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); }else if(IS_8X16(mb_type)){ mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0, qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], + &weight_op[2], &weight_avg[2], IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0, qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], + &weight_op[2], &weight_avg[2], IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); }else{ int i; @@ -2073,20 +2503,25 @@ static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t if(IS_SUB_8X8(sub_mb_type)){ mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset, qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], + &weight_op[3], &weight_avg[3], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); }else if(IS_SUB_8X4(sub_mb_type)){ mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset, qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], + &weight_op[4], &weight_avg[4], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2, qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], + &weight_op[4], &weight_avg[4], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); }else if(IS_SUB_4X8(sub_mb_type)){ mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset, qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], + &weight_op[5], &weight_avg[5], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset, qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], + &weight_op[5], &weight_avg[5], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); }else{ int j; @@ -2096,6 +2531,7 @@ static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t int sub_y_offset= y_offset + (j&2); mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset, qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], + &weight_op[6], &weight_avg[6], IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); } } @@ -2112,33 +2548,33 @@ static void decode_init_vlc(H264Context *h){ init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5, &chroma_dc_coeff_token_len [0], 1, 1, - &chroma_dc_coeff_token_bits[0], 1, 1); + &chroma_dc_coeff_token_bits[0], 1, 1, 1); for(i=0; i<4; i++){ init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17, &coeff_token_len [i][0], 1, 1, - &coeff_token_bits[i][0], 1, 1); + &coeff_token_bits[i][0], 1, 1, 1); } for(i=0; i<3; i++){ init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4, &chroma_dc_total_zeros_len [i][0], 1, 1, - &chroma_dc_total_zeros_bits[i][0], 1, 1); + &chroma_dc_total_zeros_bits[i][0], 1, 1, 1); } for(i=0; i<15; i++){ init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16, &total_zeros_len [i][0], 1, 1, - &total_zeros_bits[i][0], 1, 1); + &total_zeros_bits[i][0], 1, 1, 1); } for(i=0; i<6; i++){ init_vlc(&run_vlc[i], RUN_VLC_BITS, 7, &run_len [i][0], 1, 1, - &run_bits[i][0], 1, 1); + &run_bits[i][0], 1, 1, 1); } init_vlc(&run7_vlc, RUN7_VLC_BITS, 16, &run_len [6][0], 1, 1, - &run_bits[6][0], 1, 1); + &run_bits[6][0], 1, 1, 1); } } @@ -2184,13 +2620,17 @@ static void free_tables(H264Context *h){ av_freep(&h->cbp_table); av_freep(&h->mvd_table[0]); av_freep(&h->mvd_table[1]); + av_freep(&h->direct_table); av_freep(&h->non_zero_count); av_freep(&h->slice_table_base); - av_freep(&h->top_border); + av_freep(&h->top_borders[1]); + av_freep(&h->top_borders[0]); h->slice_table= NULL; av_freep(&h->mb2b_xy); av_freep(&h->mb2b8_xy); + + av_freep(&h->s.obmc_scratchpad); } /** @@ -2206,13 +2646,15 @@ static int alloc_tables(H264Context *h){ CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t)) CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t)) - CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t)) + CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t)) + CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t)) + CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t)) if( h->pps.cabac ) { CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t)) - CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t)) CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t)); CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t)); + CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t)); } memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t)); @@ -2230,7 +2672,9 @@ static int alloc_tables(H264Context *h){ h->mb2b8_xy[mb_xy]= b8_xy; } } - + + s->obmc_scratchpad = NULL; + return 0; fail: free_tables(h); @@ -2269,15 +2713,12 @@ static int decode_init(AVCodecContext *avctx){ decode_init_vlc(h); - if(avctx->codec_tag != 0x31637661) // avc1 - h->is_avc = 0; - else { - if((avctx->extradata_size == 0) || (avctx->extradata == NULL)) { - av_log(avctx, AV_LOG_ERROR, "AVC codec requires avcC data\n"); - return -1; - } + if(avctx->extradata_size > 0 && avctx->extradata && + *(char *)avctx->extradata == 1){ h->is_avc = 1; h->got_avcC = 0; + } else { + h->is_avc = 0; } return 0; @@ -2289,19 +2730,25 @@ static void frame_start(H264Context *h){ MPV_frame_start(s, s->avctx); ff_er_frame_start(s); - h->mmco_index=0; assert(s->linesize && s->uvlinesize); for(i=0; i<16; i++){ h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3); - h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3); + h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3); } for(i=0; i<4; i++){ h->block_offset[16+i]= h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3); + h->block_offset[24+16+i]= + h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3); } + /* can't be in alloc_tables because linesize isn't known there. + * FIXME: redo bipred weight to not require extra buffer? */ + if(!s->obmc_scratchpad) + s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize); + // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1; } @@ -2313,23 +2760,25 @@ static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src src_cb -= uvlinesize; src_cr -= uvlinesize; - h->left_border[0]= h->top_border[s->mb_x][15]; + // There is two lines saved, the line above the the top macroblock of a pair, + // and the line above the bottom macroblock + h->left_border[0]= h->top_borders[0][s->mb_x][15]; for(i=1; i<17; i++){ h->left_border[i]= src_y[15+i* linesize]; } - *(uint64_t*)(h->top_border[s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize); - *(uint64_t*)(h->top_border[s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize); if(!(s->flags&CODEC_FLAG_GRAY)){ - h->left_border[17 ]= h->top_border[s->mb_x][16+7]; - h->left_border[17+9]= h->top_border[s->mb_x][24+7]; + h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7]; + h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7]; for(i=1; i<9; i++){ h->left_border[i+17 ]= src_cb[7+i*uvlinesize]; h->left_border[i+17+9]= src_cr[7+i*uvlinesize]; } - *(uint64_t*)(h->top_border[s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize); - *(uint64_t*)(h->top_border[s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize); } } @@ -2357,8 +2806,8 @@ b= t; } if(deblock_top){ - XCHG(*(uint64_t*)(h->top_border[s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg); - XCHG(*(uint64_t*)(h->top_border[s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1); } if(!(s->flags&CODEC_FLAG_GRAY)){ @@ -2369,8 +2818,93 @@ b= t; } } if(deblock_top){ - XCHG(*(uint64_t*)(h->top_border[s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1); - XCHG(*(uint64_t*)(h->top_border[s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1); + } + } +} + +static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){ + MpegEncContext * const s = &h->s; + int i; + + src_y -= 2 * linesize; + src_cb -= 2 * uvlinesize; + src_cr -= 2 * uvlinesize; + + // There is two lines saved, the line above the the top macroblock of a pair, + // and the line above the bottom macroblock + h->left_border[0]= h->top_borders[0][s->mb_x][15]; + h->left_border[1]= h->top_borders[1][s->mb_x][15]; + for(i=2; i<34; i++){ + h->left_border[i]= src_y[15+i* linesize]; + } + + *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize); + *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize); + *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize); + + if(!(s->flags&CODEC_FLAG_GRAY)){ + h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7]; + h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7]; + h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7]; + h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7]; + for(i=2; i<18; i++){ + h->left_border[i+34 ]= src_cb[7+i*uvlinesize]; + h->left_border[i+34+18]= src_cr[7+i*uvlinesize]; + } + *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize); + *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize); + *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize); + *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize); + } +} + +static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){ + MpegEncContext * const s = &h->s; + int temp8, i; + uint64_t temp64; + int deblock_left = (s->mb_x > 0); + int deblock_top = (s->mb_y > 0); + + tprintf("xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize); + + src_y -= 2 * linesize + 1; + src_cb -= 2 * uvlinesize + 1; + src_cr -= 2 * uvlinesize + 1; + +#define XCHG(a,b,t,xchg)\ +t= a;\ +if(xchg)\ + a= b;\ +b= t; + + if(deblock_left){ + for(i = (!deblock_top)<<1; i<34; i++){ + XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg); + } + } + + if(deblock_top){ + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg); + XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1); + } + + if(!(s->flags&CODEC_FLAG_GRAY)){ + if(deblock_left){ + for(i = (!deblock_top) << 1; i<18; i++){ + XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg); + XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg); + } + } + if(deblock_top){ + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1); + XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1); } } } @@ -2384,13 +2918,12 @@ static void hl_decode_mb(H264Context *h){ uint8_t *dest_y, *dest_cb, *dest_cr; int linesize, uvlinesize /*dct_offset*/; int i; + int *block_offset = &h->block_offset[0]; + const unsigned int bottom = mb_y & 1; if(!s->decode) return; - if(s->mb_skiped){ - } - dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16; dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; @@ -2398,10 +2931,11 @@ static void hl_decode_mb(H264Context *h){ if (h->mb_field_decoding_flag) { linesize = s->linesize * 2; uvlinesize = s->uvlinesize * 2; + block_offset = &h->block_offset[24]; if(mb_y&1){ //FIXME move out of this func? dest_y -= s->linesize*15; - dest_cb-= s->linesize*7; - dest_cr-= s->linesize*7; + dest_cb-= s->uvlinesize*7; + dest_cr-= s->uvlinesize*7; } } else { linesize = s->linesize; @@ -2409,130 +2943,215 @@ static void hl_decode_mb(H264Context *h){ // dct_offset = s->linesize * 16; } - if(IS_INTRA(mb_type)){ - if(h->deblocking_filter) - xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1); - - if(!(s->flags&CODEC_FLAG_GRAY)){ - h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize); - h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize); - } - - if(IS_INTRA4x4(mb_type)){ - if(!s->encoding){ - for(i=0; i<16; i++){ - uint8_t * const ptr= dest_y + h->block_offset[i]; - uint8_t *topright= ptr + 4 - linesize; - const int topright_avail= (h->topright_samples_available<intra4x4_pred_mode_cache[ scan8[i] ]; - int tr; - - if(!topright_avail){ - /* xine: avoid (negative) buffer overflow */ - tr= (!mb_y && linesize > h->block_offset[i]) ? - ptr[3]*0x01010101 : - ptr[3 - linesize]*0x01010101; - topright= (uint8_t*) &tr; - }else if(i==5 && h->deblocking_filter){ - tr= *(uint32_t*)h->top_border[mb_x+1]; - topright= (uint8_t*) &tr; - } + if (IS_INTRA_PCM(mb_type)) { + unsigned int x, y; - h->pred4x4[ dir ](ptr, topright, linesize); - if(h->non_zero_count_cache[ scan8[i] ]){ - if(s->codec_id == CODEC_ID_H264) - h264_add_idct_c(ptr, h->mb + i*16, linesize); - else - svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0); - } + // The pixels are stored in h->mb array in the same order as levels, + // copy them in output in the correct order. + for(i=0; i<16; i++) { + for (y=0; y<4; y++) { + for (x=0; x<4; x++) { + *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x]; } } - }else{ - h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize); - if(s->codec_id == CODEC_ID_H264) - h264_luma_dc_dequant_idct_c(h->mb, s->qscale); - else - svq3_luma_dc_dequant_idct_c(h->mb, s->qscale); } - if(h->deblocking_filter) - xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0); - }else if(s->codec_id == CODEC_ID_H264){ - hl_motion(h, dest_y, dest_cb, dest_cr, - s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, - s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab); - } - - - if(!IS_INTRA4x4(mb_type)){ - if(s->codec_id == CODEC_ID_H264){ - for(i=0; i<16; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below - uint8_t * const ptr= dest_y + h->block_offset[i]; - h264_add_idct_c(ptr, h->mb + i*16, linesize); + for(i=16; i<16+4; i++) { + for (y=0; y<4; y++) { + for (x=0; x<4; x++) { + *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x]; } } - }else{ - for(i=0; i<16; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below - uint8_t * const ptr= dest_y + h->block_offset[i]; - svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0); + } + for(i=20; i<20+4; i++) { + for (y=0; y<4; y++) { + for (x=0; x<4; x++) { + *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x]; } } } - } - - if(!(s->flags&CODEC_FLAG_GRAY)){ - chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp); - chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp); - if(s->codec_id == CODEC_ID_H264){ - for(i=16; i<16+4; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ - uint8_t * const ptr= dest_cb + h->block_offset[i]; - h264_add_idct_c(ptr, h->mb + i*16, uvlinesize); + } else { + if(IS_INTRA(mb_type)){ + if(h->deblocking_filter) { + if (h->mb_aff_frame) { + if (!bottom) + xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1); + } else { + xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1); } } - for(i=20; i<20+4; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ - uint8_t * const ptr= dest_cr + h->block_offset[i]; - h264_add_idct_c(ptr, h->mb + i*16, uvlinesize); - } + + if(!(s->flags&CODEC_FLAG_GRAY)){ + h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize); + h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize); } - }else{ - for(i=16; i<16+4; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ - uint8_t * const ptr= dest_cb + h->block_offset[i]; - svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); + + if(IS_INTRA4x4(mb_type)){ + if(!s->encoding){ + for(i=0; i<16; i++){ + uint8_t * const ptr= dest_y + block_offset[i]; + uint8_t *topright; + const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ]; + int tr; + + if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){ + const int topright_avail= (h->topright_samples_available<deblocking_filter){ + tr= *(uint32_t*)h->top_borders[h->mb_aff_frame ? IS_INTERLACED(mb_type) ? bottom : 1 : 0][mb_x+1]; + topright= (uint8_t*) &tr; + }else + topright= ptr + 4 - linesize; + }else + topright= NULL; + + h->pred4x4[ dir ](ptr, topright, linesize); + if(h->non_zero_count_cache[ scan8[i] ]){ + if(s->codec_id == CODEC_ID_H264) + s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize); + else + svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0); + } + } } + }else{ + h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize); + if(s->codec_id == CODEC_ID_H264) + h264_luma_dc_dequant_idct_c(h->mb, s->qscale); + else + svq3_luma_dc_dequant_idct_c(h->mb, s->qscale); } - for(i=20; i<20+4; i++){ - if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ - uint8_t * const ptr= dest_cr + h->block_offset[i]; - svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); + if(h->deblocking_filter) { + if (h->mb_aff_frame) { + if (bottom) { + uint8_t *pair_dest_y = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize ) + mb_x * 16; + uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8; + uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8; + s->mb_y--; + xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0); + s->mb_y++; + } + } else { + xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0); } } + }else if(s->codec_id == CODEC_ID_H264){ + hl_motion(h, dest_y, dest_cb, dest_cr, + s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, + s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab, + s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab); } - } - if(h->deblocking_filter) { - backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize); - filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr); - } -} -/** - * fills the default_ref_list. - */ -static int fill_default_ref_list(H264Context *h){ - MpegEncContext * const s = &h->s; + + if(!IS_INTRA4x4(mb_type)){ + if(s->codec_id == CODEC_ID_H264){ + for(i=0; i<16; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below + uint8_t * const ptr= dest_y + block_offset[i]; + s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize); + } + } + }else{ + for(i=0; i<16; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below + uint8_t * const ptr= dest_y + block_offset[i]; + svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0); + } + } + } + } + + if(!(s->flags&CODEC_FLAG_GRAY)){ + chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp); + chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp); + if(s->codec_id == CODEC_ID_H264){ + for(i=16; i<16+4; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ + uint8_t * const ptr= dest_cb + block_offset[i]; + s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize); + } + } + for(i=20; i<20+4; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ + uint8_t * const ptr= dest_cr + block_offset[i]; + s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize); + } + } + }else{ + for(i=16; i<16+4; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ + uint8_t * const ptr= dest_cb + block_offset[i]; + svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); + } + } + for(i=20; i<20+4; i++){ + if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ + uint8_t * const ptr= dest_cr + block_offset[i]; + svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); + } + } + } + } + } + if(h->deblocking_filter) { + if (h->mb_aff_frame) { + const int mb_y = s->mb_y - 1; + uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr; + const int mb_xy= mb_x + mb_y*s->mb_stride; + const int mb_type_top = s->current_picture.mb_type[mb_xy]; + const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride]; + uint8_t tmp = s->current_picture.data[1][384]; + if (!bottom) return; + pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16; + pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; + pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; + + backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize); + // TODO deblock a pair + // top + s->mb_y--; + tprintf("call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y); + fill_caches(h, mb_type_top, 1); //FIXME dont fill stuff which isnt used by filter_mb + filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize); + if (tmp != s->current_picture.data[1][384]) { + tprintf("modified pixel 8,1 (1)\n"); + } + // bottom + s->mb_y++; + tprintf("call mbaff filter_mb\n"); + fill_caches(h, mb_type_bottom, 1); //FIXME dont fill stuff which isnt used by filter_mb + filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize); + if (tmp != s->current_picture.data[1][384]) { + tprintf("modified pixel 8,1 (2)\n"); + } + } else { + tprintf("call filter_mb\n"); + backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize); + fill_caches(h, mb_type, 1); //FIXME dont fill stuff which isnt used by filter_mb + filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize); + } + } +} + +/** + * fills the default_ref_list. + */ +static int fill_default_ref_list(H264Context *h){ + MpegEncContext * const s = &h->s; int i; - Picture sorted_short_ref[16]; + int smallest_poc_greater_than_current = -1; + Picture sorted_short_ref[32]; if(h->slice_type==B_TYPE){ int out_i; int limit= -1; + /* sort frame according to poc in B slice */ for(out_i=0; out_ishort_ref_count; out_i++){ int best_i=-1; - int best_poc=-1; + int best_poc=INT_MAX; for(i=0; ishort_ref_count; i++){ const int poc= h->short_ref[i]->poc; @@ -2546,37 +3165,47 @@ static int fill_default_ref_list(H264Context *h){ limit= best_poc; sorted_short_ref[out_i]= *h->short_ref[best_i]; + tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num); + if (-1 == smallest_poc_greater_than_current) { + if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) { + smallest_poc_greater_than_current = out_i; + } + } } } if(s->picture_structure == PICT_FRAME){ if(h->slice_type==B_TYPE){ - const int current_poc= s->current_picture_ptr->poc; int list; + tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current); + // find the largest poc for(list=0; list<2; list++){ - int index=0; - - for(i=0; ishort_ref_count && index < h->ref_count[list]; i++){ - const int i2= list ? h->short_ref_count - i - 1 : i; - const int poc= sorted_short_ref[i2].poc; - - if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit - - if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){ - h->default_ref_list[list][index ]= sorted_short_ref[i2]; - h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num; + int index = 0; + int j= -99; + int step= list ? -1 : 1; + + for(i=0; ishort_ref_count && index < h->ref_count[list]; i++, j+=step) { + while(j<0 || j>= h->short_ref_count){ + step = -step; + j= smallest_poc_greater_than_current + (step>>1); } + if(sorted_short_ref[j].reference != 3) continue; + h->default_ref_list[list][index ]= sorted_short_ref[j]; + h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num; } - for(i=0; ilong_ref_count && index < h->ref_count[ list ]; i++){ + for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){ + if(h->long_ref[i] == NULL) continue; if(h->long_ref[i]->reference != 3) continue; h->default_ref_list[ list ][index ]= *h->long_ref[i]; h->default_ref_list[ list ][index++].pic_id= i;; } - if(h->long_ref_count > 1 && h->short_ref_count==0){ + if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){ + // swap the two first elements of L1 when + // L0 and L1 are identical Picture temp= h->default_ref_list[1][0]; h->default_ref_list[1][0] = h->default_ref_list[1][1]; h->default_ref_list[1][0] = temp; @@ -2587,12 +3216,13 @@ static int fill_default_ref_list(H264Context *h){ } }else{ int index=0; - for(i=0; ishort_ref_count && index < h->ref_count[0]; i++){ + for(i=0; ishort_ref_count; i++){ if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit h->default_ref_list[0][index ]= *h->short_ref[i]; h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num; } - for(i=0; ilong_ref_count && index < h->ref_count[0]; i++){ + for(i = 0; i < 16; i++){ + if(h->long_ref[i] == NULL) continue; if(h->long_ref[i]->reference != 3) continue; h->default_ref_list[0][index ]= *h->long_ref[i]; h->default_ref_list[0][index++].pic_id= i;; @@ -2606,13 +3236,28 @@ static int fill_default_ref_list(H264Context *h){ //FIXME second field balh } } +#ifdef TRACE + for (i=0; iref_count[0]; i++) { + tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]); + } + if(h->slice_type==B_TYPE){ + for (i=0; iref_count[1]; i++) { + tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]); + } + } +#endif return 0; } +static void print_short_term(H264Context *h); +static void print_long_term(H264Context *h); + static int decode_ref_pic_list_reordering(H264Context *h){ MpegEncContext * const s = &h->s; int list; + print_short_term(h); + print_long_term(h); if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func for(list=0; list<2; list++){ @@ -2626,7 +3271,10 @@ static int decode_ref_pic_list_reordering(H264Context *h){ int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb); int pic_id; int i; + Picture *ref = NULL; + if(reordering_of_pic_nums_idc==3) + break; if(index >= h->ref_count[list]){ av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n"); @@ -2646,32 +3294,23 @@ static int decode_ref_pic_list_reordering(H264Context *h){ else pred+= abs_diff_pic_num; pred &= h->max_pic_num - 1; - for(i= h->ref_count[list]-1; i>=index; i--){ - if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0) + for(i= h->short_ref_count-1; i>=0; i--){ + ref = h->short_ref[i]; + if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer break; } }else{ pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx - - for(i= h->ref_count[list]-1; i>=index; i--){ - if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1) - break; - } + ref = h->long_ref[pic_id]; } - if(i < index){ + if (i < 0) { av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n"); memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME - }else if(i > index){ - Picture tmp= h->ref_list[list][i]; - for(; i>index; i--){ - h->ref_list[list][i]= h->ref_list[list][i-1]; - } - h->ref_list[list][index]= tmp; + } else { + h->ref_list[list][index]= *ref; } - }else if(reordering_of_pic_nums_idc==3) - break; - else{ + }else{ av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n"); return -1; } @@ -2680,15 +3319,24 @@ static int decode_ref_pic_list_reordering(H264Context *h){ if(h->slice_type!=B_TYPE) break; } + + if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred) + direct_dist_scale_factor(h); + direct_ref_list_init(h); return 0; } static int pred_weight_table(H264Context *h){ MpegEncContext * const s = &h->s; int list, i; + int luma_def, chroma_def; + h->use_weight= 0; + h->use_weight_chroma= 0; h->luma_log2_weight_denom= get_ue_golomb(&s->gb); h->chroma_log2_weight_denom= get_ue_golomb(&s->gb); + luma_def = 1<luma_log2_weight_denom; + chroma_def = 1<chroma_log2_weight_denom; for(list=0; list<2; list++){ for(i=0; iref_count[list]; i++){ @@ -2698,6 +3346,12 @@ static int pred_weight_table(H264Context *h){ if(luma_weight_flag){ h->luma_weight[list][i]= get_se_golomb(&s->gb); h->luma_offset[list][i]= get_se_golomb(&s->gb); + if( h->luma_weight[list][i] != luma_def + || h->luma_offset[list][i] != 0) + h->use_weight= 1; + }else{ + h->luma_weight[list][i]= luma_def; + h->luma_offset[list][i]= 0; } chroma_weight_flag= get_bits1(&s->gb); @@ -2706,28 +3360,91 @@ static int pred_weight_table(H264Context *h){ for(j=0; j<2; j++){ h->chroma_weight[list][i][j]= get_se_golomb(&s->gb); h->chroma_offset[list][i][j]= get_se_golomb(&s->gb); + if( h->chroma_weight[list][i][j] != chroma_def + || h->chroma_offset[list][i][j] != 0) + h->use_weight_chroma= 1; + } + }else{ + int j; + for(j=0; j<2; j++){ + h->chroma_weight[list][i][j]= chroma_def; + h->chroma_offset[list][i][j]= 0; } } } if(h->slice_type != B_TYPE) break; } + h->use_weight= h->use_weight || h->use_weight_chroma; return 0; } +static void implicit_weight_table(H264Context *h){ + MpegEncContext * const s = &h->s; + int ref0, ref1; + int cur_poc = s->current_picture_ptr->poc; + + if( h->ref_count[0] == 1 && h->ref_count[1] == 1 + && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){ + h->use_weight= 0; + h->use_weight_chroma= 0; + return; + } + + h->use_weight= 2; + h->use_weight_chroma= 2; + h->luma_log2_weight_denom= 5; + h->chroma_log2_weight_denom= 5; + + /* FIXME: MBAFF */ + for(ref0=0; ref0 < h->ref_count[0]; ref0++){ + int poc0 = h->ref_list[0][ref0].poc; + for(ref1=0; ref1 < h->ref_count[1]; ref1++){ + int poc1 = h->ref_list[1][ref1].poc; + int td = clip(poc1 - poc0, -128, 127); + if(td){ + int tb = clip(cur_poc - poc0, -128, 127); + int tx = (16384 + (ABS(td) >> 1)) / td; + int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2; + if(dist_scale_factor < -64 || dist_scale_factor > 128) + h->implicit_weight[ref0][ref1] = 32; + else + h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor; + }else + h->implicit_weight[ref0][ref1] = 32; + } + } +} + +static inline void unreference_pic(H264Context *h, Picture *pic){ + int i; + pic->reference=0; + if(pic == h->delayed_output_pic) + pic->reference=1; + else{ + for(i = 0; h->delayed_pic[i]; i++) + if(pic == h->delayed_pic[i]){ + pic->reference=1; + break; + } + } +} + /** - * instantaneos decoder refresh. + * instantaneous decoder refresh. */ static void idr(H264Context *h){ int i; - for(i=0; ilong_ref_count; i++){ - h->long_ref[i]->reference=0; - h->long_ref[i]= NULL; + for(i=0; i<16; i++){ + if (h->long_ref[i] != NULL) { + unreference_pic(h, h->long_ref[i]); + h->long_ref[i]= NULL; + } } h->long_ref_count=0; for(i=0; ishort_ref_count; i++){ - h->short_ref[i]->reference=0; + unreference_pic(h, h->short_ref[i]); h->short_ref[i]= NULL; } h->short_ref_count=0; @@ -2765,23 +3482,49 @@ static Picture * remove_short(H264Context *h, int frame_num){ static Picture * remove_long(H264Context *h, int i){ Picture *pic; - if(i >= h->long_ref_count) return NULL; pic= h->long_ref[i]; - if(pic==NULL) return NULL; - h->long_ref[i]= NULL; - memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*)); - h->long_ref_count--; + if(pic) h->long_ref_count--; return pic; } +/** + * print short term list + */ +static void print_short_term(H264Context *h) { + uint32_t i; + if(h->s.avctx->debug&FF_DEBUG_MMCO) { + av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n"); + for(i=0; ishort_ref_count; i++){ + Picture *pic= h->short_ref[i]; + av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]); + } + } +} + +/** + * print long term list + */ +static void print_long_term(H264Context *h) { + uint32_t i; + if(h->s.avctx->debug&FF_DEBUG_MMCO) { + av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n"); + for(i = 0; i < 16; i++){ + Picture *pic= h->long_ref[i]; + if (pic) { + av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]); + } + } + } +} + /** * Executes the reference picture marking (memory management control operations). */ static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ MpegEncContext * const s = &h->s; - int i; + int i, j; int current_is_long=0; Picture *pic; @@ -2796,23 +3539,24 @@ static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ case MMCO_SHORT2UNUSED: pic= remove_short(h, mmco[i].short_frame_num); if(pic==NULL) return -1; - pic->reference= 0; + unreference_pic(h, pic); break; case MMCO_SHORT2LONG: pic= remove_long(h, mmco[i].long_index); - if(pic) pic->reference=0; + if(pic) unreference_pic(h, pic); h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num); h->long_ref[ mmco[i].long_index ]->long_ref=1; + h->long_ref_count++; break; case MMCO_LONG2UNUSED: pic= remove_long(h, mmco[i].long_index); if(pic==NULL) return -1; - pic->reference= 0; + unreference_pic(h, pic); break; case MMCO_LONG: pic= remove_long(h, mmco[i].long_index); - if(pic) pic->reference=0; + if(pic) unreference_pic(h, pic); h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr; h->long_ref[ mmco[i].long_index ]->long_ref=1; @@ -2822,22 +3566,20 @@ static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ break; case MMCO_SET_MAX_LONG: assert(mmco[i].long_index <= 16); - while(mmco[i].long_index < h->long_ref_count){ - pic= remove_long(h, mmco[i].long_index); - pic->reference=0; - } - while(mmco[i].long_index > h->long_ref_count){ - h->long_ref[ h->long_ref_count++ ]= NULL; + // just remove the long term which index is greater than new max + for(j = mmco[i].long_index; j<16; j++){ + pic = remove_long(h, j); + if (pic) unreference_pic(h, pic); } break; case MMCO_RESET: while(h->short_ref_count){ pic= remove_short(h, h->short_ref[0]->frame_num); - pic->reference=0; + unreference_pic(h, pic); } - while(h->long_ref_count){ - pic= remove_long(h, h->long_ref_count-1); - pic->reference=0; + for(j = 0; j < 16; j++) { + pic= remove_long(h, j); + if(pic) unreference_pic(h, pic); } break; default: assert(0); @@ -2847,7 +3589,7 @@ static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ if(!current_is_long){ pic= remove_short(h, s->current_picture_ptr->frame_num); if(pic){ - pic->reference=0; + unreference_pic(h, pic); av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n"); } @@ -2859,6 +3601,8 @@ static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ h->short_ref_count++; } + print_short_term(h); + print_long_term(h); return 0; } @@ -2877,7 +3621,7 @@ static int decode_ref_pic_marking(H264Context *h){ } }else{ if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag - for(i= h->mmco_index; igb);; h->mmco[i].opcode= opcode; @@ -2900,6 +3644,8 @@ static int decode_ref_pic_marking(H264Context *h){ av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode); return -1; } + if(opcode == MMCO_END) + break; } h->mmco_index= i; }else{ @@ -3010,23 +3756,32 @@ static int decode_slice_header(H264Context *h){ int first_mb_in_slice, pps_id; int num_ref_idx_active_override_flag; static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE}; + int slice_type; + int default_ref_list_done = 0; s->current_picture.reference= h->nal_ref_idc != 0; + s->dropable= h->nal_ref_idc == 0; first_mb_in_slice= get_ue_golomb(&s->gb); - h->slice_type= get_ue_golomb(&s->gb); - if(h->slice_type > 9){ + slice_type= get_ue_golomb(&s->gb); + if(slice_type > 9){ av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y); + return -1; } - if(h->slice_type > 4){ - h->slice_type -= 5; + if(slice_type > 4){ + slice_type -= 5; h->slice_type_fixed=1; }else h->slice_type_fixed=0; - h->slice_type= slice_type_map[ h->slice_type ]; - + slice_type= slice_type_map[ slice_type ]; + if (slice_type == I_TYPE + || (h->slice_num != 0 && slice_type == h->slice_type) ) { + default_ref_list_done = 1; + } + h->slice_type= slice_type; + s->pict_type= h->slice_type; // to make a few old func happy, its wrong though pps_id= get_ue_golomb(&s->gb); @@ -3047,14 +3802,11 @@ static int decode_slice_header(H264Context *h){ } s->mb_width= h->sps.mb_width; - s->mb_height= h->sps.mb_height; + s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag); - h->b_stride= s->mb_width*4; - h->b8_stride= s->mb_width*2; + h->b_stride= s->mb_width*4 + 1; + h->b8_stride= s->mb_width*2 + 1; - s->mb_x = first_mb_in_slice % s->mb_width; - s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW - s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right ); if(h->sps.frame_mbs_only_flag) s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom); @@ -3075,6 +3827,8 @@ static int decode_slice_header(H264Context *h){ s->avctx->width = s->width; s->avctx->height = s->height; s->avctx->sample_aspect_ratio= h->sps.sar; + if(!s->avctx->sample_aspect_ratio.den) + s->avctx->sample_aspect_ratio.den = 1; if(h->sps.timing_info_present_flag && h->sps.fixed_frame_rate_flag){ s->avctx->frame_rate = h->sps.time_scale; @@ -3082,22 +3836,29 @@ static int decode_slice_header(H264Context *h){ } } - if(first_mb_in_slice == 0){ + if(h->slice_num == 0){ frame_start(h); } s->current_picture_ptr->frame_num= //FIXME frame_num cleanup h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num); + h->mb_aff_frame = 0; if(h->sps.frame_mbs_only_flag){ s->picture_structure= PICT_FRAME; }else{ - if(get_bits1(&s->gb)) //field_pic_flag + if(get_bits1(&s->gb)) { //field_pic_flag s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag - else + } else { s->picture_structure= PICT_FRAME; + first_mb_in_slice <<= 1; + h->mb_aff_frame = h->sps.mb_aff; + } } + s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width; + s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width; + if(s->picture_structure==PICT_FRAME){ h->curr_pic_num= h->frame_num; h->max_pic_num= 1<< h->sps.log2_max_frame_num; @@ -3153,7 +3914,7 @@ static int decode_slice_header(H264Context *h){ } } - if(first_mb_in_slice == 0){ + if(!default_ref_list_done){ fill_default_ref_list(h); } @@ -3162,6 +3923,10 @@ static int decode_slice_header(H264Context *h){ if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE )) || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) ) pred_weight_table(h); + else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE) + implicit_weight_table(h); + else + h->use_weight = 0; if(s->current_picture.reference) decode_ref_pic_marking(h); @@ -3175,6 +3940,7 @@ static int decode_slice_header(H264Context *h){ av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale); return -1; } + h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); //FIXME qscale / qp ... stuff if(h->slice_type == SP_TYPE){ get_bits1(&s->gb); /* sp_for_switch_flag */ @@ -3202,15 +3968,21 @@ static int decode_slice_header(H264Context *h){ slice_group_change_cycle= get_bits(&s->gb, ?); #endif + h->slice_num++; + if(s->avctx->debug&FF_DEBUG_PICT_INFO){ - av_log(h->s.avctx, AV_LOG_DEBUG, "mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n", + av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n", + h->slice_num, + (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"), first_mb_in_slice, av_get_pict_type_char(h->slice_type), pps_id, h->frame_num, s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1], h->ref_count[0], h->ref_count[1], s->qscale, - h->deblocking_filter + h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2, + h->use_weight, + h->use_weight==1 && h->use_weight_chroma ? "c" : "" ); } @@ -3231,7 +4003,7 @@ static inline int get_level_prefix(GetBitContext *gb){ log= 32 - av_log2(buf); #ifdef TRACE print_bin(buf>>(32-log), log); - printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__); + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__); #endif LAST_SKIP_BITS(re, gb, log); @@ -3380,6 +4152,55 @@ static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, in return 0; } +/** + * decodes a P_SKIP or B_SKIP macroblock + */ +static void decode_mb_skip(H264Context *h){ + MpegEncContext * const s = &h->s; + const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; + int mb_type; + + memset(h->non_zero_count[mb_xy], 0, 16); + memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui + + if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){ + h->mb_field_decoding_flag= get_bits1(&s->gb); + } + if(h->mb_field_decoding_flag) + mb_type|= MB_TYPE_INTERLACED; + + if( h->slice_type == B_TYPE ) + { + // just for fill_caches. pred_direct_motion will set the real mb_type + mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP; + + fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ... + pred_direct_motion(h, &mb_type); + if(h->pps.cabac){ + fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4); + fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4); + } + } + else + { + int mx, my; + mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP; + + fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ... + pred_pskip_motion(h, &mx, &my); + fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); + fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4); + if(h->pps.cabac) + fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4); + } + + write_back_motion(h, mb_type); + s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP; + s->current_picture.qscale_table[mb_xy]= s->qscale; + h->slice_table[ mb_xy ]= h->slice_num; + h->prev_mb_skiped= 1; +} + /** * decodes a macroblock * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed @@ -3399,40 +4220,15 @@ static int decode_mb_cavlc(H264Context *h){ s->mb_skip_run= get_ue_golomb(&s->gb); if (s->mb_skip_run--) { - int mx, my; - /* skip mb */ -//FIXME b frame - mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0; - - memset(h->non_zero_count[mb_xy], 0, 16); - memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui - - if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){ - h->mb_field_decoding_flag= get_bits1(&s->gb); - } - - if(h->mb_field_decoding_flag) - mb_type|= MB_TYPE_INTERLACED; - - fill_caches(h, mb_type); //FIXME check what is needed and what not ... - pred_pskip_motion(h, &mx, &my); - fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); - fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4); - write_back_motion(h, mb_type); - - s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type - s->current_picture.qscale_table[mb_xy]= s->qscale; - h->slice_table[ mb_xy ]= h->slice_num; - - h->prev_mb_skiped= 1; + decode_mb_skip(h); return 0; } } - if(h->sps.mb_aff /* && !field pic FIXME needed? */){ - if((s->mb_y&1)==0) + if(h->mb_aff_frame){ + if ( ((s->mb_y&1) == 0) || h->prev_mb_skiped) h->mb_field_decoding_flag = get_bits1(&s->gb); }else - h->mb_field_decoding_flag=0; //FIXME som ed note ?! + h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME); h->prev_mb_skiped= 0; @@ -3473,43 +4269,44 @@ decode_intra_mb: h->slice_table[ mb_xy ]= h->slice_num; if(IS_INTRA_PCM(mb_type)){ - const uint8_t *ptr; - int x, y; + unsigned int x, y; // we assume these blocks are very rare so we dont optimize it align_get_bits(&s->gb); - ptr= s->gb.buffer + get_bits_count(&s->gb); - + // The pixels are stored in the same order as levels in h->mb array. for(y=0; y<16; y++){ - const int index= 4*(y&3) + 64*(y>>2); + const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3); for(x=0; x<16; x++){ - h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); + tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8)); + h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8); } } for(y=0; y<8; y++){ const int index= 256 + 4*(y&3) + 32*(y>>2); for(x=0; x<8; x++){ - h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); + tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8)); + h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8); } } for(y=0; y<8; y++){ const int index= 256 + 64 + 4*(y&3) + 32*(y>>2); for(x=0; x<8; x++){ - h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); + tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8)); + h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8); } } - skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers - - //FIXME deblock filter, non_zero_count_cache init ... + // In deblocking, the quantiser is 0 + s->current_picture.qscale_table[mb_xy]= 0; + h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0); + // All coeffs are presents memset(h->non_zero_count[mb_xy], 16, 16); - s->current_picture.qscale_table[mb_xy]= s->qscale; return 0; } - fill_caches(h, mb_type); + fill_caches(h, mb_type, 0); //mb_pred if(IS_INTRA(mb_type)){ @@ -3561,6 +4358,9 @@ decode_intra_mb: sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type; } + if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1]) + || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) + pred_direct_motion(h, &mb_type); }else{ assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ? for(i=0; i<4; i++){ @@ -3575,10 +4375,14 @@ decode_intra_mb: } for(list=0; list<2; list++){ - const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; + int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; if(ref_count == 0) continue; + if (h->mb_aff_frame && h->mb_field_decoding_flag) { + ref_count <<= 1; + } for(i=0; i<4; i++){ - if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ + if(IS_DIRECT(h->sub_mb_type[i])) continue; + if(IS_DIR(h->sub_mb_type[i], 0, list)){ ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip? }else{ //FIXME @@ -3592,10 +4396,11 @@ decode_intra_mb: if(ref_count == 0) continue; for(i=0; i<4; i++){ + if(IS_DIRECT(h->sub_mb_type[i])) continue; h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]= h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i]; - if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ + if(IS_DIR(h->sub_mb_type[i], 0, list)){ const int sub_mb_type= h->sub_mb_type[i]; const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1; for(j=0; jcurrent_picture.mb_type[mb_xy]= mb_type; + }else{ int list, mx, my, i; //FIXME we should set ref_idx_l? to 0 if we use that later ... if(IS_16X16(mb_type)){ for(list=0; list<2; list++){ - if(h->ref_count[0]>0){ + if(h->ref_count[list]>0){ if(IS_DIR(mb_type, 0, list)){ const int val= get_te0_golomb(&s->gb, h->ref_count[list]); fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1); - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1); } } for(list=0; list<2; list++){ @@ -3651,7 +4460,8 @@ decode_intra_mb: tprintf("final mv:%d %d\n", mx, my); fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); - } + }else + fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4); } } else if(IS_16X8(mb_type)){ @@ -3661,7 +4471,8 @@ decode_intra_mb: if(IS_DIR(mb_type, i, list)){ const int val= get_te0_golomb(&s->gb, h->ref_count[list]); fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1); - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1); } } } @@ -3674,7 +4485,8 @@ decode_intra_mb: tprintf("final mv:%d %d\n", mx, my); fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4); - } + }else + fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4); } } }else{ @@ -3685,7 +4497,8 @@ decode_intra_mb: if(IS_DIR(mb_type, i, list)){ //FIXME optimize const int val= get_te0_golomb(&s->gb, h->ref_count[list]); fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1); - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1); } } } @@ -3698,7 +4511,8 @@ decode_intra_mb: tprintf("final mv:%d %d\n", mx, my); fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4); - } + }else + fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4); } } } @@ -3749,7 +4563,7 @@ decode_intra_mb: else s->qscale-= 52; } - h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale); + h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); if(IS_INTRA16x16(mb_type)){ if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){ return -1; //FIXME continue if partotioned and other retirn -1 too @@ -3819,41 +4633,71 @@ decode_intra_mb: return 0; } -static int decode_cabac_mb_type( H264Context *h ) { +static int decode_cabac_field_decoding_flag(H264Context *h) { MpegEncContext * const s = &h->s; + const int mb_x = s->mb_x; + const int mb_y = s->mb_y & ~1; + const int mba_xy = mb_x - 1 + mb_y *s->mb_stride; + const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride; - if( h->slice_type == I_TYPE ) { - const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; - int ctx = 0; - int mb_type; + unsigned int ctx = 0; + + if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) { + ctx += 1; + } + if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) { + ctx += 1; + } + + return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] ); +} - if( s->mb_x > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-1] ) ) +static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) { + uint8_t *state= &h->cabac_state[ctx_base]; + int mb_type; + + if(intra_slice){ + MpegEncContext * const s = &h->s; + const int mba_xy = h->left_mb_xy[0]; + const int mbb_xy = h->top_mb_xy; + int ctx=0; + if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) ) ctx++; - if( s->mb_y > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-s->mb_stride] ) ) + if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) ) ctx++; - - if( get_cabac( &h->cabac, &h->cabac_state[3+ctx] ) == 0 ) + if( get_cabac( &h->cabac, &state[ctx] ) == 0 ) + return 0; /* I4x4 */ + state += 2; + }else{ + if( get_cabac( &h->cabac, &state[0] ) == 0 ) return 0; /* I4x4 */ + } - if( get_cabac_terminate( &h->cabac ) ) - return 25; /* PCM */ + if( get_cabac_terminate( &h->cabac ) ) + return 25; /* PCM */ - mb_type = 1; /* I16x16 */ - if( get_cabac( &h->cabac, &h->cabac_state[3+3] ) ) - mb_type += 12; /* cbp_luma != 0 */ + mb_type = 1; /* I16x16 */ + if( get_cabac( &h->cabac, &state[1] ) ) + mb_type += 12; /* cbp_luma != 0 */ - if( get_cabac( &h->cabac, &h->cabac_state[3+4] ) ) { - if( get_cabac( &h->cabac, &h->cabac_state[3+5] ) ) - mb_type += 4 * 2; /* cbp_chroma == 2 */ - else - mb_type += 4 * 1; /* cbp_chroma == 1 */ - } - if( get_cabac( &h->cabac, &h->cabac_state[3+6] ) ) - mb_type += 2; - if( get_cabac( &h->cabac, &h->cabac_state[3+7] ) ) - mb_type += 1; - return mb_type; + if( get_cabac( &h->cabac, &state[2] ) ) { + if( get_cabac( &h->cabac, &state[2+intra_slice] ) ) + mb_type += 4 * 2; /* cbp_chroma == 2 */ + else + mb_type += 4 * 1; /* cbp_chroma == 1 */ + } + if( get_cabac( &h->cabac, &state[3+intra_slice] ) ) + mb_type += 2; + if( get_cabac( &h->cabac, &state[3+2*intra_slice] ) ) + mb_type += 1; + return mb_type; +} +static int decode_cabac_mb_type( H264Context *h ) { + MpegEncContext * const s = &h->s; + + if( h->slice_type == I_TYPE ) { + return decode_cabac_intra_mb_type(h, 3, 1); } else if( h->slice_type == P_TYPE ) { if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) { /* P-type */ @@ -3869,31 +4713,45 @@ static int decode_cabac_mb_type( H264Context *h ) { return 1; /* P_L0_D16x8; */ } } else { - int mb_type; - /* I-type */ - if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 ) - return 5+0; /* I_4x4 */ - if( get_cabac_terminate( &h->cabac ) ) - return 5+25; /*I_PCM */ - mb_type = 5+1; /* I16x16 */ - if( get_cabac( &h->cabac, &h->cabac_state[17+1] ) ) - mb_type += 12; /* cbp_luma != 0 */ - - if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) ) { - if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) ) - mb_type += 4 * 2; /* cbp_chroma == 2 */ - else - mb_type += 4 * 1; /* cbp_chroma == 1 */ - } - if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) ) - mb_type += 2; - if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) ) - mb_type += 1; + return decode_cabac_intra_mb_type(h, 17, 0) + 5; + } + } else if( h->slice_type == B_TYPE ) { + const int mba_xy = h->left_mb_xy[0]; + const int mbb_xy = h->top_mb_xy; + int ctx = 0; + int bits; - return mb_type; + if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ) + && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) ) + ctx++; + if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ) + && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) ) + ctx++; + + if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) ) + return 0; /* B_Direct_16x16 */ + + if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) { + return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */ } + + bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3; + bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2; + bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1; + bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ); + if( bits < 8 ) + return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */ + else if( bits == 13 ) { + return decode_cabac_intra_mb_type(h, 32, 0) + 23; + } else if( bits == 14 ) + return 11; /* B_L1_L0_8x16 */ + else if( bits == 15 ) + return 22; /* B_8x8 */ + + bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] ); + return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */ } else { - /* TODO do others frames types */ + /* TODO SI/SP frames? */ return -1; } } @@ -3905,9 +4763,9 @@ static int decode_cabac_mb_skip( H264Context *h) { const int mbb_xy = mb_xy - s->mb_stride; int ctx = 0; - if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mba_xy] ) ) + if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )) ctx++; - if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ) ) + if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] )) ctx++; if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE) @@ -3935,18 +4793,16 @@ static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) { } static int decode_cabac_mb_chroma_pre_mode( H264Context *h) { - MpegEncContext * const s = &h->s; - const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; - const int mba_xy = mb_xy - 1; - const int mbb_xy = mb_xy - s->mb_stride; + const int mba_xy = h->left_mb_xy[0]; + const int mbb_xy = h->top_mb_xy; int ctx = 0; /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */ - if( s->mb_x > 0 && h->chroma_pred_mode_table[mba_xy] != 0 ) + if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 ) ctx++; - if( s->mb_y > 0 && h->chroma_pred_mode_table[mbb_xy] != 0 ) + if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 ) ctx++; if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 ) @@ -3975,16 +4831,13 @@ static const uint8_t block_idx_xy[4][4] = { static int decode_cabac_mb_cbp_luma( H264Context *h) { MpegEncContext * const s = &h->s; - const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; int cbp = 0; int i8x8; - h->cbp_table[mb_xy] = 0; /* FIXME aaahahahah beurk */ - for( i8x8 = 0; i8x8 < 4; i8x8++ ) { - int mba_xy = -1; - int mbb_xy = -1; + int cbp_a = -1; + int cbp_b = -1; int x, y; int ctx = 0; @@ -3992,51 +4845,45 @@ static int decode_cabac_mb_cbp_luma( H264Context *h) { y = block_idx_y[4*i8x8]; if( x > 0 ) - mba_xy = mb_xy; - else if( s->mb_x > 0 ) - mba_xy = mb_xy - 1; + cbp_a = cbp; + else if( s->mb_x > 0 && (h->slice_table[h->left_mb_xy[0]] == h->slice_num)) { + cbp_a = h->left_cbp; + tprintf("cbp_a = left_cbp = %x\n", cbp_a); + } if( y > 0 ) - mbb_xy = mb_xy; - else if( s->mb_y > 0 ) - mbb_xy = mb_xy - s->mb_stride; + cbp_b = cbp; + else if( s->mb_y > 0 && (h->slice_table[h->top_mb_xy] == h->slice_num)) { + cbp_b = h->top_cbp; + tprintf("cbp_b = top_cbp = %x\n", cbp_b); + } /* No need to test for skip as we put 0 for skip block */ - if( mba_xy >= 0 ) { + /* No need to test for IPCM as we put 1 for IPCM block */ + if( cbp_a >= 0 ) { int i8x8a = block_idx_xy[(x-1)&0x03][y]/4; - if( ((h->cbp_table[mba_xy] >> i8x8a)&0x01) == 0 ) + if( ((cbp_a >> i8x8a)&0x01) == 0 ) ctx++; } - if( mbb_xy >= 0 ) { + if( cbp_b >= 0 ) { int i8x8b = block_idx_xy[x][(y-1)&0x03]/4; - if( ((h->cbp_table[mbb_xy] >> i8x8b)&0x01) == 0 ) + if( ((cbp_b >> i8x8b)&0x01) == 0 ) ctx += 2; } if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) { cbp |= 1 << i8x8; - h->cbp_table[mb_xy] = cbp; /* FIXME aaahahahah beurk */ } } return cbp; } static int decode_cabac_mb_cbp_chroma( H264Context *h) { - MpegEncContext * const s = &h->s; - const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; int ctx; int cbp_a, cbp_b; - /* No need to test for skip */ - if( s->mb_x > 0 ) - cbp_a = (h->cbp_table[mb_xy-1]>>4)&0x03; - else - cbp_a = -1; - - if( s->mb_y > 0 ) - cbp_b = (h->cbp_table[mb_xy-s->mb_stride]>>4)&0x03; - else - cbp_b = -1; + cbp_a = (h->left_cbp>>4)&0x03; + cbp_b = (h-> top_cbp>>4)&0x03; ctx = 0; if( cbp_a > 0 ) ctx++; @@ -4047,10 +4894,7 @@ static int decode_cabac_mb_cbp_chroma( H264Context *h) { ctx = 4; if( cbp_a == 2 ) ctx++; if( cbp_b == 2 ) ctx += 2; - if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) ) - return 2; - else - return 1; + return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ); } static int decode_cabac_mb_dqp( H264Context *h) { MpegEncContext * const s = &h->s; @@ -4063,7 +4907,7 @@ static int decode_cabac_mb_dqp( H264Context *h) { else mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride; - if( mbn_xy >= 0 && h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) ) + if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) ) ctx++; while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) { @@ -4079,7 +4923,7 @@ static int decode_cabac_mb_dqp( H264Context *h) { else return -(val + 1)/2; } -static int decode_cabac_mb_sub_type( H264Context *h ) { +static int decode_cabac_p_mb_sub_type( H264Context *h ) { if( get_cabac( &h->cabac, &h->cabac_state[21] ) ) return 0; /* 8x8 */ if( !get_cabac( &h->cabac, &h->cabac_state[22] ) ) @@ -4088,6 +4932,22 @@ static int decode_cabac_mb_sub_type( H264Context *h ) { return 2; /* 4x8 */ return 3; /* 4x4 */ } +static int decode_cabac_b_mb_sub_type( H264Context *h ) { + int type; + if( !get_cabac( &h->cabac, &h->cabac_state[36] ) ) + return 0; /* B_Direct_8x8 */ + if( !get_cabac( &h->cabac, &h->cabac_state[37] ) ) + return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */ + type = 3; + if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) { + if( get_cabac( &h->cabac, &h->cabac_state[39] ) ) + return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */ + type += 4; + } + type += 2*get_cabac( &h->cabac, &h->cabac_state[39] ); + type += get_cabac( &h->cabac, &h->cabac_state[39] ); + return type; +} static int decode_cabac_mb_ref( H264Context *h, int list, int n ) { int refa = h->ref_cache[list][scan8[n] - 1]; @@ -4095,10 +4955,17 @@ static int decode_cabac_mb_ref( H264Context *h, int list, int n ) { int ref = 0; int ctx = 0; - if( refa > 0 ) - ctx++; - if( refb > 0 ) - ctx += 2; + if( h->slice_type == B_TYPE) { + if( refa > 0 && !h->direct_cache[scan8[n] - 1] ) + ctx++; + if( refb > 0 && !h->direct_cache[scan8[n] - 8] ) + ctx += 2; + } else { + if( refa > 0 ) + ctx++; + if( refb > 0 ) + ctx += 2; + } while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) { ref++; @@ -4114,8 +4981,7 @@ static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) { int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) + abs( h->mvd_cache[list][scan8[n] - 8][l] ); int ctxbase = (l == 0) ? 40 : 47; - int ctx; - int mvd = 0; + int ctx, mvd; if( amvd < 3 ) ctx = 0; @@ -4124,11 +4990,14 @@ static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) { else ctx = 1; + if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx])) + return 0; + + mvd= 1; + ctx= 3; while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) { mvd++; - if( ctx < 3 ) - ctx = 3; - else if( ctx < 6 ) + if( ctx < 6 ) ctx++; } @@ -4143,132 +5012,52 @@ static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) { mvd += 1 << k; } } - if( mvd != 0 && get_cabac_bypass( &h->cabac ) ) - return -mvd; - return mvd; + if( get_cabac_bypass( &h->cabac ) ) return -mvd; + else return mvd; } - -static int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) { - MpegEncContext * const s = &h->s; - const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; - int mba_xy = -1; - int mbb_xy = -1; - - int nza = -1; - int nzb = -1; +static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) { + int nza, nzb; int ctx = 0; if( cat == 0 ) { - if( s->mb_x > 0 ) { - mba_xy = mb_xy - 1; - if( IS_INTRA16x16(s->current_picture.mb_type[mba_xy] ) ) - nza = h->cbp_table[mba_xy]&0x100; - } - if( s->mb_y > 0 ) { - mbb_xy = mb_xy - s->mb_stride; - if( IS_INTRA16x16(s->current_picture.mb_type[mbb_xy] ) ) - nzb = h->cbp_table[mbb_xy]&0x100; - } + nza = h->left_cbp&0x100; + nzb = h-> top_cbp&0x100; } else if( cat == 1 || cat == 2 ) { - int i8x8a, i8x8b; - int x, y; - - x = block_idx_x[idx]; - y = block_idx_y[idx]; - - if( x > 0 ) - mba_xy = mb_xy; - else if( s->mb_x > 0 ) - mba_xy = mb_xy - 1; - - if( y > 0 ) - mbb_xy = mb_xy; - else if( s->mb_y > 0 ) - mbb_xy = mb_xy - s->mb_stride; - - /* No need to test for skip */ - if( mba_xy >= 0 ) { - i8x8a = block_idx_xy[(x-1)&0x03][y]/4; - - if( !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) && - ((h->cbp_table[mba_xy]&0x0f)>>i8x8a)) - nza = h->non_zero_count_cache[scan8[idx] - 1]; - } - - if( mbb_xy >= 0 ) { - i8x8b = block_idx_xy[x][(y-1)&0x03]/4; - - if( !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) && - ((h->cbp_table[mbb_xy]&0x0f)>>i8x8b)) - nzb = h->non_zero_count_cache[scan8[idx] - 8]; - } + nza = h->non_zero_count_cache[scan8[idx] - 1]; + nzb = h->non_zero_count_cache[scan8[idx] - 8]; } else if( cat == 3 ) { - if( s->mb_x > 0 ) { - mba_xy = mb_xy - 1; - - if( !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) && - (h->cbp_table[mba_xy]&0x30) ) - nza = (h->cbp_table[mba_xy]>>(6+idx))&0x01; - } - if( s->mb_y > 0 ) { - mbb_xy = mb_xy - s->mb_stride; - - if( !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) && - (h->cbp_table[mbb_xy]&0x30) ) - nzb = (h->cbp_table[mbb_xy]>>(6+idx))&0x01; - } - } else if( cat == 4 ) { - int idxc = idx % 4 ; - if( idxc == 1 || idxc == 3 ) - mba_xy = mb_xy; - else if( s->mb_x > 0 ) - mba_xy = mb_xy -1; - - if( idxc == 2 || idxc == 3 ) - mbb_xy = mb_xy; - else if( s->mb_y > 0 ) - mbb_xy = mb_xy - s->mb_stride; - - if( mba_xy >= 0 && - !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) && - (h->cbp_table[mba_xy]&0x30) == 0x20 ) - nza = h->non_zero_count_cache[scan8[16+idx] - 1]; - - if( mbb_xy >= 0 && - !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) && - (h->cbp_table[mbb_xy]&0x30) == 0x20 ) - nzb = h->non_zero_count_cache[scan8[16+idx] - 8]; - } - - if( ( mba_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) || - ( mba_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) ) || - nza > 0 ) + nza = (h->left_cbp>>(6+idx))&0x01; + nzb = (h-> top_cbp>>(6+idx))&0x01; + } else { + assert(cat == 4); + nza = h->non_zero_count_cache[scan8[16+idx] - 1]; + nzb = h->non_zero_count_cache[scan8[16+idx] - 8]; + } + + if( nza > 0 ) ctx++; - if( ( mbb_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) || - ( mbb_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) ) || - nzb > 0 ) + if( nzb > 0 ) ctx += 2; return ctx + 4 * cat; } -static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) { +static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) { const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride; const uint16_t *qmul= dequant_coeff[qp]; + static const int significant_coeff_flag_field_offset[2] = { 105, 277 }; + static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 }; static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 }; - static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 }; - static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 }; + static const int coeff_abs_level_m1_offset[5] = {227+ 0, 227+10, 227+20, 227+30, 227+39 }; - int coeff[16]; + int index[16]; - int last = 0; + int i, last; int coeff_count = 0; - int nz[16] = {0}; - int i; - int abslevel1 = 0; + int abslevel1 = 1; int abslevelgt1 = 0; /* cat: 0-> DC 16x16 n = 0 @@ -4288,96 +5077,103 @@ static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n return 0; } - while( last < max_coeff - 1 ) { - int ctx = FFMIN( last, max_coeff - 2 ); - - if( get_cabac( &h->cabac, &h->cabac_state[105+significant_coeff_flag_offset[cat]+ctx] ) == 0 ) { - nz[last++] = 0; - } - else { - nz[last++] = 1; - coeff_count++; - if( get_cabac( &h->cabac, &h->cabac_state[166+last_significant_coeff_flag_offset[cat]+ctx] ) ) { - while( last < max_coeff ) { - nz[last++] = 0; - } + for(last= 0; last < max_coeff - 1; last++) { + if( get_cabac( &h->cabac, &h->cabac_state[significant_coeff_flag_field_offset[h->mb_field_decoding_flag]+significant_coeff_flag_offset[cat]+last] )) { + index[coeff_count++] = last; + if( get_cabac( &h->cabac, &h->cabac_state[last_significant_coeff_flag_field_offset[h->mb_field_decoding_flag]+significant_coeff_flag_offset[cat]+last] ) ) { + last= max_coeff; break; } } } if( last == max_coeff -1 ) { - nz[last++] = 1; - coeff_count++; + index[coeff_count++] = last; } + assert(coeff_count > 0); - if( cat == 0 && coeff_count > 0 ) + if( cat == 0 ) h->cbp_table[mb_xy] |= 0x100; else if( cat == 1 || cat == 2 ) h->non_zero_count_cache[scan8[n]] = coeff_count; - else if( cat == 3 && coeff_count > 0 ) + else if( cat == 3 ) h->cbp_table[mb_xy] |= 0x40 << n; - else if( cat == 4 ) + else { + assert( cat == 4 ); h->non_zero_count_cache[scan8[16+n]] = coeff_count; + } for( i = coeff_count - 1; i >= 0; i-- ) { - int coeff_abs_m1; + int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + coeff_abs_level_m1_offset[cat]; + int j= scantable[index[i]]; - int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 + 1 )) + coeff_abs_level_m1_offset[cat]; - - if( get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) == 0 ) { - coeff_abs_m1 = 0; + if( get_cabac( &h->cabac, &h->cabac_state[ctx] ) == 0 ) { + if( cat == 0 || cat == 3 ) { + if( get_cabac_bypass( &h->cabac ) ) block[j] = -1; + else block[j] = 1; + }else{ + if( get_cabac_bypass( &h->cabac ) ) block[j] = -qmul[j]; + else block[j] = qmul[j]; + } + + abslevel1++; } else { - coeff_abs_m1 = 1; + int coeff_abs = 2; ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat]; - while( coeff_abs_m1 < 14 && get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) ) { - coeff_abs_m1++; + while( coeff_abs < 15 && get_cabac( &h->cabac, &h->cabac_state[ctx] ) ) { + coeff_abs++; } - } - if( coeff_abs_m1 >= 14 ) { - int j = 0; - while( get_cabac_bypass( &h->cabac ) ) { - coeff_abs_m1 += 1 << j; - j++; + if( coeff_abs >= 15 ) { + int j = 0; + while( get_cabac_bypass( &h->cabac ) ) { + coeff_abs += 1 << j; + j++; + } + + while( j-- ) { + if( get_cabac_bypass( &h->cabac ) ) + coeff_abs += 1 << j ; + } } - while( j-- ) { - if( get_cabac_bypass( &h->cabac ) ) - coeff_abs_m1 += 1 << j ; + if( cat == 0 || cat == 3 ) { + if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs; + else block[j] = coeff_abs; + }else{ + if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs * qmul[j]; + else block[j] = coeff_abs * qmul[j]; } - } - if( get_cabac_bypass( &h->cabac ) ) - coeff[i] = -1 *( coeff_abs_m1 + 1 ); - else - coeff[i] = coeff_abs_m1 + 1; - - if( coeff_abs_m1 == 0 ) - abslevel1++; - else + abslevelgt1++; - } - - if( cat == 0 || cat == 3 ) { /* DC */ - int j; - for( i = 0, j = 0; j < coeff_count; i++ ) { - if( nz[i] ) { - block[scantable[i]] = coeff[j]; - - j++; - } } + } + return 0; +} - } else { /* AC */ - int j; - for( i = 0, j = 0; j < coeff_count; i++ ) { - if( nz[i] ) { - block[scantable[i]] = coeff[j] * qmul[scantable[i]]; - - j++; - } +void inline compute_mb_neighboors(H264Context *h) +{ + MpegEncContext * const s = &h->s; + const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; + h->top_mb_xy = mb_xy - s->mb_stride; + h->left_mb_xy[0] = mb_xy - 1; + if(h->mb_aff_frame){ + const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride; + const int top_pair_xy = pair_xy - s->mb_stride; + const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]); + const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]); + const int curr_mb_frame_flag = !h->mb_field_decoding_flag; + const int bottom = (s->mb_y & 1); + if (bottom + ? !curr_mb_frame_flag // bottom macroblock + : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock + ) { + h->top_mb_xy -= s->mb_stride; + } + if (left_mb_frame_flag != curr_mb_frame_flag) { + h->left_mb_xy[0] = pair_xy - 1; } } - return 0; + return; } /** @@ -4391,61 +5187,43 @@ static int decode_mb_cabac(H264Context *h) { s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?) - if( h->slice_type == B_TYPE ) { - av_log( h->s.avctx, AV_LOG_ERROR, "B-frame not supported with CABAC\n" ); - return -1; - } - if( h->sps.mb_aff ) { - av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" ); - return -1; - } - + tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) { /* read skip flags */ if( decode_cabac_mb_skip( h ) ) { - int mx, my; + decode_mb_skip(h); - /* skip mb */ - mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP; - - memset(h->non_zero_count[mb_xy], 0, 16); - memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui -#if 0 - if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){ - h->mb_field_decoding_flag= get_bits1(&s->gb); - } - if(h->mb_field_decoding_flag) - mb_type|= MB_TYPE_INTERLACED; -#endif - - fill_caches(h, mb_type); //FIXME check what is needed and what not ... - pred_pskip_motion(h, &mx, &my); - fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); - fill_rectangle( h->mvd_cache[0][scan8[0]], 4, 4, 8, pack16to32(0,0), 4); - fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4); - write_back_motion(h, mb_type); - - s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type - s->current_picture.qscale_table[mb_xy]= s->qscale; - h->slice_table[ mb_xy ]= h->slice_num; h->cbp_table[mb_xy] = 0; h->chroma_pred_mode_table[mb_xy] = 0; h->last_qscale_diff = 0; - h->prev_mb_skiped= 1; - return 0; } } + if(h->mb_aff_frame){ + if ( ((s->mb_y&1) == 0) || h->prev_mb_skiped) + h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h); + }else + h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME); + h->prev_mb_skiped = 0; + compute_mb_neighboors(h); if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) { av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" ); return -1; } - if( h->slice_type == P_TYPE ) { + if( h->slice_type == B_TYPE ) { + if( mb_type < 23 ){ + partition_count= b_mb_type_info[mb_type].partition_count; + mb_type= b_mb_type_info[mb_type].type; + }else{ + mb_type -= 23; + goto decode_intra_mb; + } + } else if( h->slice_type == P_TYPE ) { if( mb_type < 5) { partition_count= p_mb_type_info[mb_type].partition_count; mb_type= p_mb_type_info[mb_type].type; @@ -4461,23 +5239,59 @@ decode_intra_mb: h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode; mb_type= i_mb_type_info[mb_type].type; } -#if 0 if(h->mb_field_decoding_flag) mb_type |= MB_TYPE_INTERLACED; -#endif s->current_picture.mb_type[mb_xy]= mb_type; h->slice_table[ mb_xy ]= h->slice_num; if(IS_INTRA_PCM(mb_type)) { - /* TODO */ - h->cbp_table[mb_xy] = 0xf +4*2; + const uint8_t *ptr; + unsigned int x, y; + + // We assume these blocks are very rare so we dont optimize it. + // FIXME The two following lines get the bitstream position in the cabac + // decode, I think it should be done by a function in cabac.h (or cabac.c). + ptr= h->cabac.bytestream; + if (h->cabac.low&0x1) ptr-=CABAC_BITS/8; + + // The pixels are stored in the same order as levels in h->mb array. + for(y=0; y<16; y++){ + const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3); + for(x=0; x<16; x++){ + tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr); + h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++; + } + } + for(y=0; y<8; y++){ + const int index= 256 + 4*(y&3) + 32*(y>>2); + for(x=0; x<8; x++){ + tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr); + h->mb[index + (x&3) + 16*(x>>2)]= *ptr++; + } + } + for(y=0; y<8; y++){ + const int index= 256 + 64 + 4*(y&3) + 32*(y>>2); + for(x=0; x<8; x++){ + tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr); + h->mb[index + (x&3) + 16*(x>>2)]= *ptr++; + } + } + + ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr); + + // All blocks are presents + h->cbp_table[mb_xy] = 0x1ef; h->chroma_pred_mode_table[mb_xy] = 0; - s->current_picture.qscale_table[mb_xy]= s->qscale; - return -1; + // In deblocking, the quantiser is 0 + s->current_picture.qscale_table[mb_xy]= 0; + h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0); + // All coeffs are presents + memset(h->non_zero_count[mb_xy], 16, 16); + return 0; } - fill_caches(h, mb_type); + fill_caches(h, mb_type, 0); if( IS_INTRA( mb_type ) ) { if( IS_INTRA4x4( mb_type ) ) { @@ -4502,17 +5316,34 @@ decode_intra_mb: } else if( partition_count == 4 ) { int i, j, sub_partition_count[4], list, ref[2][4]; - /* Only P-frame */ - for( i = 0; i < 4; i++ ) { - h->sub_mb_type[i] = decode_cabac_mb_sub_type( h ); - sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; - h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; + if( h->slice_type == B_TYPE ) { + for( i = 0; i < 4; i++ ) { + h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h ); + sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; + h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type; + } + if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1]) + || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) { + pred_direct_motion(h, &mb_type); + if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) { + for( i = 0; i < 4; i++ ) + if( IS_DIRECT(h->sub_mb_type[i]) ) + fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 ); + } + } + } else { + for( i = 0; i < 4; i++ ) { + h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h ); + sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; + h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; + } } for( list = 0; list < 2; list++ ) { if( h->ref_count[list] > 0 ) { for( i = 0; i < 4; i++ ) { - if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ + if(IS_DIRECT(h->sub_mb_type[i])) continue; + if(IS_DIR(h->sub_mb_type[i], 0, list)){ if( h->ref_count[list] > 1 ) ref[list][i] = decode_cabac_mb_ref( h, list, 4*i ); else @@ -4528,6 +5359,10 @@ decode_intra_mb: for(list=0; list<2; list++){ for(i=0; i<4; i++){ + if(IS_DIRECT(h->sub_mb_type[i])){ + fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4); + continue; + } h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]; if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ @@ -4584,7 +5419,12 @@ decode_intra_mb: } } } - } else if( !IS_DIRECT(mb_type) ) { + } else if( IS_DIRECT(mb_type) ) { + pred_direct_motion(h, &mb_type); + s->current_picture.mb_type[mb_xy]= mb_type; + fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4); + fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4); + } else { int list, mx, my, i, mpx, mpy; if(IS_16X16(mb_type)){ for(list=0; list<2; list++){ @@ -4593,7 +5433,8 @@ decode_intra_mb: const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0; fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1); } - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); } for(list=0; list<2; list++){ if(IS_DIR(mb_type, 0, list)){ @@ -4605,7 +5446,8 @@ decode_intra_mb: fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4); fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); - } + }else + fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4); } } else if(IS_16X8(mb_type)){ @@ -4615,7 +5457,8 @@ decode_intra_mb: if(IS_DIR(mb_type, i, list)){ const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0; fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1); - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1); } } } @@ -4629,6 +5472,9 @@ decode_intra_mb: fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4); fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4); + }else{ + fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4); + fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4); } } } @@ -4640,7 +5486,8 @@ decode_intra_mb: if(IS_DIR(mb_type, i, list)){ //FIXME optimize const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0; fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1); - } + }else + fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1); } } } @@ -4654,6 +5501,9 @@ decode_intra_mb: tprintf("final mv:%d %d\n", mx, my); fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4); fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4); + }else{ + fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4); + fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4); } } } @@ -4690,7 +5540,7 @@ decode_intra_mb: if(s->qscale<0) s->qscale+= 52; else s->qscale-= 52; } - h->chroma_qp = get_chroma_qp(h, s->qscale); + h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale); if( IS_INTRA16x16( mb_type ) ) { int i; @@ -4748,7 +5598,10 @@ decode_intra_mb: nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0; } } else { - memset( &h->non_zero_count_cache[8], 0, 8*5 ); + uint8_t * const nnz= &h->non_zero_count_cache[0]; + fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1); + nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] = + nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0; } s->current_picture.qscale_table[mb_xy]= s->qscale; @@ -4799,6 +5652,7 @@ static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4] i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_edgev i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], pix[-2], pix[-1], pix[0], pix[1]); } pix += stride; } @@ -4845,6 +5699,7 @@ static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4] pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; } + tprintf("filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]); } pix += stride; } @@ -4879,6 +5734,7 @@ static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_edgecv i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); } pix += stride; } @@ -4896,6 +5752,7 @@ static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */ pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */ + tprintf("filter_mb_edgecv i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); } pix += stride; } @@ -4903,6 +5760,160 @@ static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4 } } +static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) { + int i; + for( i = 0; i < 16; i++, pix += stride) { + int index_a; + int alpha; + int beta; + + int qp_index; + int bS_index = (i >> 1); + if (h->mb_field_decoding_flag) { + bS_index &= ~1; + bS_index |= (i & 1); + } + + if( bS[bS_index] == 0 ) { + continue; + } + + qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3); + index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 ); + alpha = alpha_table[index_a]; + beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )]; + + + if( bS[bS_index] < 4 ) { + const int tc0 = tc0_table[index_a][bS[bS_index] - 1]; + /* 4px edge length */ + const int p0 = pix[-1]; + const int p1 = pix[-2]; + const int p2 = pix[-3]; + const int q0 = pix[0]; + const int q1 = pix[1]; + const int q2 = pix[2]; + + if( ABS( p0 - q0 ) < alpha && + ABS( p1 - p0 ) < beta && + ABS( q1 - q0 ) < beta ) { + int tc = tc0; + int i_delta; + + if( ABS( p2 - p0 ) < beta ) { + pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 ); + tc++; + } + if( ABS( q2 - q0 ) < beta ) { + pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 ); + tc++; + } + + i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); + pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ + pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); + } + }else{ + /* 4px edge length */ + const int p0 = pix[-1]; + const int p1 = pix[-2]; + const int p2 = pix[-3]; + + const int q0 = pix[0]; + const int q1 = pix[1]; + const int q2 = pix[2]; + + if( ABS( p0 - q0 ) < alpha && + ABS( p1 - p0 ) < beta && + ABS( q1 - q0 ) < beta ) { + + if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){ + if( ABS( p2 - p0 ) < beta) + { + const int p3 = pix[-4]; + /* p0', p1', p2' */ + pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3; + pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2; + pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3; + } else { + /* p0' */ + pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; + } + if( ABS( q2 - q0 ) < beta) + { + const int q3 = pix[3]; + /* q0', q1', q2' */ + pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3; + pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2; + pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3; + } else { + /* q0' */ + pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; + } + }else{ + /* p0', q0' */ + pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; + pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; + } + tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]); + } + } + } +} +static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) { + int i; + for( i = 0; i < 8; i++, pix += stride) { + int index_a; + int alpha; + int beta; + + int qp_index; + int bS_index = i; + + if( bS[bS_index] == 0 ) { + continue; + } + + qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3); + index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 ); + alpha = alpha_table[index_a]; + beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )]; + if( bS[bS_index] < 4 ) { + const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1; + /* 2px edge length (because we use same bS than the one for luma) */ + const int p0 = pix[-1]; + const int p1 = pix[-2]; + const int q0 = pix[0]; + const int q1 = pix[1]; + + if( ABS( p0 - q0 ) < alpha && + ABS( p1 - p0 ) < beta && + ABS( q1 - q0 ) < beta ) { + const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); + + pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ + pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); + } + }else{ + const int p0 = pix[-1]; + const int p1 = pix[-2]; + const int q0 = pix[0]; + const int q1 = pix[1]; + + if( ABS( p0 - q0 ) < alpha && + ABS( p1 - p0 ) < beta && + ABS( q1 - q0 ) < beta ) { + + pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */ + pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */ + tprintf("filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]); + } + } + } +} + static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) { int i, d; const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 ); @@ -4946,6 +5957,7 @@ static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4] i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */ pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]); } pix++; } @@ -4990,6 +6002,7 @@ static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4] pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2; } + tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]); } pix++; } @@ -5028,6 +6041,7 @@ static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4 pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */ pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ + tprintf("filter_mb_edgech i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3*pix_next], p1, p0, q0, q1, pix[2*pix_next], pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]); } pix++; } @@ -5045,6 +6059,7 @@ static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4 pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */ pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */ + tprintf("filter_mb_edgech i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], pix[-3*pix_next], p1, p0, q0, q1, pix[2*pix_next], pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]); } pix++; } @@ -5052,43 +6067,167 @@ static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4 } } -static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) { +static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) { MpegEncContext * const s = &h->s; const int mb_xy= mb_x + mb_y*s->mb_stride; - int linesize, uvlinesize; + int first_vertical_edge_done = 0; int dir; - /* FIXME Implement deblocking filter for field MB */ - if( h->sps.mb_aff ) { - return; - } - linesize = s->linesize; - uvlinesize = s->uvlinesize; + if (h->mb_aff_frame + // left mb is in picture + && h->slice_table[mb_xy-1] != 255 + // and current and left pair do not have the same interlaced type + && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1])) + // and left mb is in the same slice if deblocking_filter == 2 + && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) { + /* First vertical edge is different in MBAFF frames + * There are 8 differents bS to compute and 2 differents Qp + */ + int bS[8]; + int qp[2]; + int chroma_qp[2]; + + int i; + first_vertical_edge_done = 1; + for( i = 0; i < 8; i++ ) { + int y = i>>1; + int b_idx= 8 + 4 + 8*y; + int bn_idx= b_idx - 1; + int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1]; + + if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) || + IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) { + bS[i] = 4; + } else if( h->non_zero_count_cache[b_idx] != 0 || + h->non_zero_count_cache[bn_idx] != 0 ) { + bS[i] = 2; + } else { + /* FIXME: A given frame may occupy more than one position in + * the reference list. So we should compare the frame numbers, + * not the indices in the ref list. */ + int l; + bS[i] = 0; + for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) { + if( h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] || + ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 || + ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) { + bS[i] = 1; + break; + } + } + } + } + if(bS[0]+bS[1]+bS[2]+bS[3] != 0) { + // Do not use s->qscale as luma quantiser because it has not the same + // value in IPCM macroblocks. + qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1; + chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) + + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1; + qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1; + chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) + + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1; + + /* Filter edge */ + tprintf("filter mb:%d/%d MBAFF, QPy:%d/%d, QPc:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], chroma_qp[0], chroma_qp[1], linesize, uvlinesize); + { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); } + filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp ); + filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp ); + filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp ); + } + } /* dir : 0 -> vertical edge, 1 -> horizontal edge */ for( dir = 0; dir < 2; dir++ ) { - int start = 0; int edge; + const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy; + int start = h->slice_table[mbm_xy] == 255 ? 1 : 0; - /* test picture boundary */ - if( ( dir == 0 && mb_x == 0 ) || ( dir == 1 && mb_y == 0 ) ) { + if (first_vertical_edge_done) { start = 1; + first_vertical_edge_done = 0; } - /* FIXME test slice boundary */ - if( h->deblocking_filter == 2 ) { - } + + if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy]) + start = 1; /* Calculate bS */ for( edge = start; edge < 4; edge++ ) { - /* mbn_xy: neighbour macroblock (how that works for field ?) */ - int mbn_xy = edge > 0 ? mb_xy : ( dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride ); + /* mbn_xy: neighbour macroblock */ + int mbn_xy = edge > 0 ? mb_xy : mbm_xy; int bS[4]; int qp; + if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0) + && !IS_INTERLACED(s->current_picture.mb_type[mb_xy]) + && IS_INTERLACED(s->current_picture.mb_type[mbn_xy]) + ) { + // This is a special case in the norm where the filtering must + // be done twice (one each of the field) even if we are in a + // frame macroblock. + // + unsigned int tmp_linesize = 2 * linesize; + unsigned int tmp_uvlinesize = 2 * uvlinesize; + int mbn_xy = mb_xy - 2 * s->mb_stride; + int qp, chroma_qp; + + // first filtering + if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) || + IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) { + bS[0] = bS[1] = bS[2] = bS[3] = 3; + } else { + // TODO + assert(0); + } + /* Filter edge */ + // Do not use s->qscale as luma quantiser because it has not the same + // value in IPCM macroblocks. + qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; + tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize); + { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); } + filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp ); + chroma_qp = ( h->chroma_qp + + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; + filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp ); + filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp ); + + // second filtering + mbn_xy += s->mb_stride; + if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) || + IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) { + bS[0] = bS[1] = bS[2] = bS[3] = 3; + } else { + // TODO + assert(0); + } + /* Filter edge */ + // Do not use s->qscale as luma quantiser because it has not the same + // value in IPCM macroblocks. + qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; + tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize); + { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); } + filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp ); + chroma_qp = ( h->chroma_qp + + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; + filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp ); + filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp ); + continue; + } if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) || IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) { - bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 ); + int value; + if (edge == 0) { + if ( (!IS_INTERLACED(s->current_picture.mb_type[mb_xy]) && !IS_INTERLACED(s->current_picture.mb_type[mbm_xy])) + || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0)) + ) { + value = 4; + } else { + value = 3; + } + } else { + value = 3; + } + bS[0] = bS[1] = bS[2] = bS[3] = value; } else { int i; for( i = 0; i < 4; i++ ) { @@ -5101,17 +6240,21 @@ static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8 h->non_zero_count_cache[bn_idx] != 0 ) { bS[i] = 2; } - else if( h->slice_type == P_TYPE ) { - if( h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx] || - ABS( h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] ) >= 4 || - ABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= 4 ) - bS[i] = 1; - else - bS[i] = 0; - } - else { - /* FIXME Add support for B frame */ - return; + else + { + /* FIXME: A given frame may occupy more than one position in + * the reference list. So we should compare the frame numbers, + * not the indices in the ref list. */ + int l; + bS[i] = 0; + for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) { + if( h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] || + ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 || + ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) { + bS[i] = 1; + break; + } + } } } @@ -5120,12 +6263,17 @@ static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8 } /* Filter edge */ - qp = ( s->qscale + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; + // Do not use s->qscale as luma quantiser because it has not the same + // value in IPCM macroblocks. + qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; + //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]); + tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize); + { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); } if( dir == 0 ) { filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp ); if( (edge&1) == 0 ) { int chroma_qp = ( h->chroma_qp + - get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp ); filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp ); } @@ -5133,7 +6281,7 @@ static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp ); if( (edge&1) == 0 ) { int chroma_qp = ( h->chroma_qp + - get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; + get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); } @@ -5175,20 +6323,20 @@ static int decode_slice(H264Context *h){ for(;;){ int ret = decode_mb_cabac(h); - int eos = get_cabac_terminate( &h->cabac ); /* End of Slice flag */ + int eos; if(ret>=0) hl_decode_mb(h); /* XXX: useless as decode_mb_cabac it doesn't support that ... */ - if( ret >= 0 && h->sps.mb_aff ) { //FIXME optimal? or let mb_decode decode 16x32 ? + if( ret >= 0 && h->mb_aff_frame ) { //FIXME optimal? or let mb_decode decode 16x32 ? s->mb_y++; if(ret>=0) ret = decode_mb_cabac(h); - eos = get_cabac_terminate( &h->cabac ); hl_decode_mb(h); s->mb_y--; } + eos = get_cabac_terminate( &h->cabac ); if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) { av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y); @@ -5199,22 +6347,17 @@ static int decode_slice(H264Context *h){ if( ++s->mb_x >= s->mb_width ) { s->mb_x = 0; ff_draw_horiz_band(s, 16*s->mb_y, 16); - if( ++s->mb_y >= s->mb_height ) { - tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); + ++s->mb_y; + if(h->mb_aff_frame) { + ++s->mb_y; } } if( eos || s->mb_y >= s->mb_height ) { + tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); return 0; } -#if 0 - /* TODO test over-reading in cabac code */ - else if( read too much in h->cabac ) { - ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); - return -1; - } -#endif } } else { @@ -5223,7 +6366,7 @@ static int decode_slice(H264Context *h){ if(ret>=0) hl_decode_mb(h); - if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ? + if(ret>=0 && h->mb_aff_frame){ //FIXME optimal? or let mb_decode decode 16x32 ? s->mb_y++; ret = decode_mb_cavlc(h); @@ -5241,7 +6384,11 @@ static int decode_slice(H264Context *h){ if(++s->mb_x >= s->mb_width){ s->mb_x=0; ff_draw_horiz_band(s, 16*s->mb_y, 16); - if(++s->mb_y >= s->mb_height){ + ++s->mb_y; + if(h->mb_aff_frame) { + ++s->mb_y; + } + if(s->mb_y >= s->mb_height){ tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); if(get_bits_count(&s->gb) == s->gb.size_in_bits ) { @@ -5257,6 +6404,7 @@ static int decode_slice(H264Context *h){ } if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){ + tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); if(get_bits_count(&s->gb) == s->gb.size_in_bits ){ ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); @@ -5318,9 +6466,27 @@ static int decode_slice(H264Context *h){ return -1; //not reached } +static inline void decode_hrd_parameters(H264Context *h, SPS *sps){ + MpegEncContext * const s = &h->s; + int cpb_count, i; + cpb_count = get_ue_golomb(&s->gb) + 1; + get_bits(&s->gb, 4); /* bit_rate_scale */ + get_bits(&s->gb, 4); /* cpb_size_scale */ + for(i=0; igb); /* bit_rate_value_minus1 */ + get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */ + get_bits1(&s->gb); /* cbr_flag */ + } + get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */ + get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */ + get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */ + get_bits(&s->gb, 5); /* time_offset_length */ +} + static inline int decode_vui_parameters(H264Context *h, SPS *sps){ MpegEncContext * const s = &h->s; int aspect_ratio_info_present_flag, aspect_ratio_idc; + int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag; aspect_ratio_info_present_flag= get_bits1(&s->gb); @@ -5367,29 +6533,27 @@ static inline int decode_vui_parameters(H264Context *h, SPS *sps){ sps->fixed_frame_rate_flag = get_bits1(&s->gb); } -#if 0 -| nal_hrd_parameters_present_flag |0 |u(1) | -| if( nal_hrd_parameters_present_flag = = 1) | | | -| hrd_parameters( ) | | | -| vcl_hrd_parameters_present_flag |0 |u(1) | -| if( vcl_hrd_parameters_present_flag = = 1) | | | -| hrd_parameters( ) | | | -| if( ( nal_hrd_parameters_present_flag = = 1 | || | | -| | | | -|( vcl_hrd_parameters_present_flag = = 1 ) ) | | | -| low_delay_hrd_flag |0 |u(1) | -| bitstream_restriction_flag |0 |u(1) | -| if( bitstream_restriction_flag ) { |0 |u(1) | -| motion_vectors_over_pic_boundaries_flag |0 |u(1) | -| max_bytes_per_pic_denom |0 |ue(v) | -| max_bits_per_mb_denom |0 |ue(v) | -| log2_max_mv_length_horizontal |0 |ue(v) | -| log2_max_mv_length_vertical |0 |ue(v) | -| num_reorder_frames |0 |ue(v) | -| max_dec_frame_buffering |0 |ue(v) | -| } | | | -|} | | | -#endif + nal_hrd_parameters_present_flag = get_bits1(&s->gb); + if(nal_hrd_parameters_present_flag) + decode_hrd_parameters(h, sps); + vcl_hrd_parameters_present_flag = get_bits1(&s->gb); + if(vcl_hrd_parameters_present_flag) + decode_hrd_parameters(h, sps); + if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) + get_bits1(&s->gb); /* low_delay_hrd_flag */ + get_bits1(&s->gb); /* pic_struct_present_flag */ + + sps->bitstream_restriction_flag = get_bits1(&s->gb); + if(sps->bitstream_restriction_flag){ + get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */ + get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */ + get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */ + get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */ + get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */ + sps->num_reorder_frames = get_ue_golomb(&s->gb); + get_ue_golomb(&s->gb); /* max_dec_frame_buffering */ + } + return 0; } @@ -5403,14 +6567,15 @@ static inline int decode_seq_parameter_set(H264Context *h){ get_bits1(&s->gb); //constraint_set0_flag get_bits1(&s->gb); //constraint_set1_flag get_bits1(&s->gb); //constraint_set2_flag - get_bits(&s->gb, 5); // reserved + get_bits1(&s->gb); //constraint_set3_flag + get_bits(&s->gb, 4); // reserved level_idc= get_bits(&s->gb, 8); sps_id= get_ue_golomb(&s->gb); sps= &h->sps_buffer[ sps_id ]; sps->profile_idc= profile_idc; sps->level_idc= level_idc; - + sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4; sps->poc_type= get_ue_golomb(&s->gb); @@ -5431,9 +6596,16 @@ static inline int decode_seq_parameter_set(H264Context *h){ } sps->ref_frame_count= get_ue_golomb(&s->gb); + if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){ + av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n"); + } sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb); sps->mb_width= get_ue_golomb(&s->gb) + 1; sps->mb_height= get_ue_golomb(&s->gb) + 1; + if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 || + avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height)) + return -1; + sps->frame_mbs_only_flag= get_bits1(&s->gb); if(!sps->frame_mbs_only_flag) sps->mb_aff= get_bits1(&s->gb); @@ -5561,23 +6733,34 @@ static inline int decode_picture_parameter_set(H264Context *h){ * finds the end of the current frame in the bitstream. * @return the position of the first byte of the next frame, or -1 */ -static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ +static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){ int i; uint32_t state; + ParseContext *pc = &(h->s.parse_context); //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]); // mb_addr= pc->mb_addr - 1; state= pc->state; - //FIXME this will fail with slices - for(i=0; iframe_start_found, i); if(pc->frame_start_found){ - pc->state=-1; - pc->frame_start_found= 0; - return i-3; + // If there isn't one more byte in the buffer + // the test on first_mb_in_slice cannot be done yet + // do it at next call. + if (i >= buf_size) break; + if (buf[i] & 0x80) { + // first_mb_in_slice is 0, probably the first nal of a new + // slice + tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i); + pc->state=-1; + pc->frame_start_found= 0; + return i-4; + } } - pc->frame_start_found= 1; + pc->frame_start_found = 1; } + if (istate= state; @@ -5589,10 +6772,11 @@ static int h264_parse(AVCodecParserContext *s, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size) { - ParseContext *pc = s->priv_data; + H264Context *h = s->priv_data; + ParseContext *pc = &h->s.parse_context; int next; - next= find_frame_end(pc, buf, buf_size); + next= find_frame_end(h, buf, buf_size); if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { *poutbuf = NULL; @@ -5615,6 +6799,7 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ printf("%X ", buf[i]); } #endif + h->slice_num = 0; for(;;){ int consumed; int dst_length; @@ -5626,8 +6811,7 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ if(buf_index >= buf_size) break; nalsize = 0; for(i = 0; i < h->nal_length_size; i++) - nalsize = (nalsize << 8) | buf[buf_index + i]; - buf_index += h->nal_length_size; + nalsize = (nalsize << 8) | buf[buf_index++]; } else { // start code prefix search for(; buf_index + 3 < buf_size; buf_index++){ @@ -5641,12 +6825,12 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ buf_index+=3; } - ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index); + ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index); if(ptr[dst_length - 1] == 0) dst_length--; bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1); if(s->avctx->debug&FF_DEBUG_STARTCODE){ - av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length); + av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", h->nal_unit_type, buf_index, buf_size, dst_length); } if (h->is_avc && (nalsize != consumed)) @@ -5698,7 +6882,8 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ if(s->flags& CODEC_FLAG_LOW_DELAY) s->low_delay=1; - avctx->has_b_frames= !s->low_delay; + if(avctx->has_b_frames < 2) + avctx->has_b_frames= !s->low_delay; break; case NAL_PPS: init_get_bits(&s->gb, ptr, bit_length); @@ -5713,13 +6898,12 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ default: av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type); } - - //FIXME move after where irt is set - s->current_picture.pict_type= s->pict_type; - s->current_picture.key_frame= s->pict_type == I_TYPE; } if(!s->current_picture_ptr) return buf_index; //no frame + + s->current_picture_ptr->pict_type= s->pict_type; + s->current_picture_ptr->key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE; h->prev_frame_num_offset= h->frame_num_offset; h->prev_frame_num= h->frame_num; @@ -5729,8 +6913,6 @@ static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ } if(s->current_picture_ptr->reference) execute_ref_pic_marking(h, h->mmco, h->mmco_index); - else - assert(h->mmco_index==0); ff_er_frame_end(s); @@ -5774,7 +6956,7 @@ static int decode_frame(AVCodecContext *avctx, } if(s->flags&CODEC_FLAG_TRUNCATED){ - int next= find_frame_end(&s->parse_context, buf, buf_size); + int next= find_frame_end(h, buf, buf_size); if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 ) return buf_size; @@ -5782,7 +6964,7 @@ static int decode_frame(AVCodecContext *avctx, } if(h->is_avc && !h->got_avcC) { - int i, cnt, poffs; + int i, cnt, nalsize; unsigned char *p = avctx->extradata; if(avctx->extradata_size < 7) { av_log(avctx, AV_LOG_ERROR, "avcC too short\n"); @@ -5795,27 +6977,29 @@ static int decode_frame(AVCodecContext *avctx, /* sps and pps in the avcC always have length coded with 2 bytes, so put a fake nal_length_size = 2 while parsing them */ h->nal_length_size = 2; - poffs = 6; // Decode sps from avcC cnt = *(p+5) & 0x1f; // Number of sps + p += 6; for (i = 0; i < cnt; i++) { - if(decode_nal_units(h, p + poffs, BE_16(p + poffs) + 2) != BE_16(p + poffs) + 2) { + nalsize = BE_16(p) + 2; + if(decode_nal_units(h, p, nalsize) != nalsize) { av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i); return -1; } - poffs += BE_16(p + poffs) + 2; + p += nalsize; } // Decode pps from avcC - cnt = *(p + poffs++); // Number of pps + cnt = *(p++); // Number of pps for (i = 0; i < cnt; i++) { - if(decode_nal_units(h, p + poffs, BE_16(p + poffs) + 2) != BE_16(p + poffs) + 2) { + nalsize = BE_16(p) + 2; + if(decode_nal_units(h, p, nalsize) != nalsize) { av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i); return -1; } - poffs += BE_16(p + poffs) + 2; + p += nalsize; } // Now store right nal length size, that will be use to parse all other nals - h->nal_length_size = ((*(p+4))&0x03)+1; + h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1; // Do not reparse avcC h->got_avcC = 1; } @@ -5832,21 +7016,83 @@ static int decode_frame(AVCodecContext *avctx, //FIXME do something with unavailable reference frames // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size); -#if 0 - if(s->pict_type==B_TYPE || s->low_delay){ - *pict= *(AVFrame*)&s->current_picture; - } else { - *pict= *(AVFrame*)&s->last_picture; - } -#endif if(!s->current_picture_ptr){ av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n"); return -1; } - *pict= *(AVFrame*)&s->current_picture; //FIXME - ff_print_debug_info(s, pict); + { + Picture *out = s->current_picture_ptr; +#if 0 //decode order + *data_size = sizeof(AVFrame); +#else + /* Sort B-frames into display order */ + Picture *cur = s->current_picture_ptr; + Picture *prev = h->delayed_output_pic; + int out_idx = 0; + int pics = 0; + int out_of_order; + int cross_idr = 0; + int dropped_frame = 0; + int i; + + if(h->sps.bitstream_restriction_flag + && s->avctx->has_b_frames < h->sps.num_reorder_frames){ + s->avctx->has_b_frames = h->sps.num_reorder_frames; + s->low_delay = 0; + } + + while(h->delayed_pic[pics]) pics++; + h->delayed_pic[pics++] = cur; + if(cur->reference == 0) + cur->reference = 1; + + for(i=0; h->delayed_pic[i]; i++) + if(h->delayed_pic[i]->key_frame) + cross_idr = 1; + + out = h->delayed_pic[0]; + for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++) + if(h->delayed_pic[i]->poc < out->poc){ + out = h->delayed_pic[i]; + out_idx = i; + } + + out_of_order = !cross_idr && prev && out->poc < prev->poc; + if(prev && pics <= s->avctx->has_b_frames) + out = prev; + else if((out_of_order && pics-1 == s->avctx->has_b_frames) + || (s->low_delay && + ((!cross_idr && prev && out->poc > prev->poc + 2) + || cur->pict_type == B_TYPE))) + { + s->low_delay = 0; + s->avctx->has_b_frames++; + out = prev; + } + else if(out_of_order) + out = prev; + + if(out_of_order || pics > s->avctx->has_b_frames){ + dropped_frame = (out != h->delayed_pic[out_idx]); + for(i=out_idx; h->delayed_pic[i]; i++) + h->delayed_pic[i] = h->delayed_pic[i+1]; + } + + if(prev == out && !dropped_frame) + *data_size = 0; + else + *data_size = sizeof(AVFrame); + if(prev && prev != out && prev->reference == 1) + prev->reference = 0; + h->delayed_output_pic = out; +#endif + + *pict= *(AVFrame*)out; + } + assert(pict->data[0]); + ff_print_debug_info(s, pict); //printf("out %d\n", (int)pict->data[0]); #if 0 //? @@ -5854,12 +7100,6 @@ static int decode_frame(AVCodecContext *avctx, /* we substract 1 because it is added on utils.c */ avctx->frame_number = s->picture_number - 1; #endif -#if 0 - /* dont output the last pic after seeking */ - if(s->last_picture_ptr || s->low_delay) - //Note this isnt a issue as a IDR pic should flush teh buffers -#endif - *data_size = sizeof(AVFrame); return get_consumed_bytes(s, buf_index, buf_size); } #if 0 @@ -5970,7 +7210,7 @@ int main(){ } // printf("\n"); - h264_add_idct_c(ref, block, 4); + s->dsp.h264_idct_add(ref, block, 4); /* for(j=0; j<16; j++){ printf("%d ", ref[j]); } @@ -6079,12 +7319,12 @@ AVCodec h264_decoder = { NULL, decode_end, decode_frame, - /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, + /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, }; AVCodecParser h264_parser = { { CODEC_ID_H264 }, - sizeof(ParseContext), + sizeof(H264Context), NULL, h264_parse, ff_parse_close, diff --git a/src/libffmpeg/libavcodec/h264data.h b/src/libffmpeg/libavcodec/h264data.h index 5480becd4..21d2260e8 100644 --- a/src/libffmpeg/libavcodec/h264data.h +++ b/src/libffmpeg/libavcodec/h264data.h @@ -353,8 +353,8 @@ static const PMbInfo p_mb_type_info[5]={ {MB_TYPE_16x16|MB_TYPE_P0L0 , 1}, {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P1L0, 2}, {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P1L0, 2}, -{MB_TYPE_8x8 , 4}, -{MB_TYPE_8x8 |MB_TYPE_REF0 , 4}, +{MB_TYPE_8x8 |MB_TYPE_P0L0|MB_TYPE_P1L0, 4}, +{MB_TYPE_8x8 |MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_REF0, 4}, }; static const PMbInfo p_sub_mb_type_info[4]={ @@ -387,7 +387,7 @@ static const PMbInfo b_mb_type_info[23]={ {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, }, {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, }, {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, }, -{MB_TYPE_8x8 , 4, }, +{MB_TYPE_8x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 4, }, }; static const PMbInfo b_sub_mb_type_info[13]={ diff --git a/src/libffmpeg/libavcodec/h264idct.c b/src/libffmpeg/libavcodec/h264idct.c new file mode 100755 index 000000000..551197d37 --- /dev/null +++ b/src/libffmpeg/libavcodec/h264idct.c @@ -0,0 +1,70 @@ +/* + * H.264 IDCT + * Copyright (c) 2004 Michael Niedermayer + * + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/** + * @file h264-idct.c + * H.264 IDCT. + * @author Michael Niedermayer + */ + +#include "dsputil.h" + +static always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){ + int i; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + block[0] += 1<<(shift-1); + + for(i=0; i<4; i++){ + const int z0= block[0 + block_stride*i] + block[2 + block_stride*i]; + const int z1= block[0 + block_stride*i] - block[2 + block_stride*i]; + const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i]; + const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1); + + block[0 + block_stride*i]= z0 + z3; + block[1 + block_stride*i]= z1 + z2; + block[2 + block_stride*i]= z1 - z2; + block[3 + block_stride*i]= z0 - z3; + } + + for(i=0; i<4; i++){ + const int z0= block[i + block_stride*0] + block[i + block_stride*2]; + const int z1= block[i + block_stride*0] - block[i + block_stride*2]; + const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3]; + const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1); + + dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ]; + dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ]; + dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ]; + dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ]; + } +} + +void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){ + idct_internal(dst, block, stride, 4, 6, 1); +} + +void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){ + idct_internal(dst, block, stride, 8, 3, 1); +} + +void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){ + idct_internal(dst, block, stride, 8, 3, 0); +} diff --git a/src/libffmpeg/libavcodec/huffyuv.c b/src/libffmpeg/libavcodec/huffyuv.c index ecc6a5fa2..5b496c512 100644 --- a/src/libffmpeg/libavcodec/huffyuv.c +++ b/src/libffmpeg/libavcodec/huffyuv.c @@ -27,11 +27,22 @@ */ #include "common.h" +#include "bitstream.h" #include "avcodec.h" #include "dsputil.h" #define VLC_BITS 11 +#ifdef WORDS_BIGENDIAN +#define B 3 +#define G 2 +#define R 1 +#else +#define B 0 +#define G 1 +#define R 2 +#endif + typedef enum Predictor{ LEFT= 0, PLANE, @@ -51,15 +62,17 @@ typedef struct HYuvContext{ int bgr32; //use bgr32 instead of bgr24 int width, height; int flags; + int context; int picture_number; int last_slice_end; - uint8_t __align8 temp[3][2560]; + uint8_t *temp[3]; uint64_t stats[3][256]; uint8_t len[3][256]; uint32_t bits[3][256]; VLC vlc[3]; AVFrame picture; - uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution + uint8_t *bitstream_buffer; + int bitstream_buffer_size; DSPContext dsp; }HYuvContext; @@ -157,13 +170,13 @@ static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, b= *blue; for(i=0; i 32) break; + if(len >= 32) break; dst[i]= len; } @@ -296,10 +309,11 @@ for(j=0; j<256; j++){ printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j); } #endif - init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4); + free_vlc(&s->vlc[i]); + init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0); } - return 0; + return (get_bits_count(&gb)+7)/8; } static int read_old_huffman_tables(HYuvContext *s){ @@ -322,8 +336,10 @@ static int read_old_huffman_tables(HYuvContext *s){ memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t)); memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t)); - for(i=0; i<3; i++) - init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4); + 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); + } return 0; #else @@ -332,22 +348,44 @@ static int read_old_huffman_tables(HYuvContext *s){ #endif } -static int decode_init(AVCodecContext *avctx) -{ +static void alloc_temp(HYuvContext *s){ + int i; + + if(s->bitstream_bpp<24){ + for(i=0; i<3; i++){ + s->temp[i]= av_malloc(s->width + 16); + } + }else{ + s->temp[0]= av_malloc(4*s->width + 16); + } +} + +static int common_init(AVCodecContext *avctx){ HYuvContext *s = avctx->priv_data; - int width, height; s->avctx= avctx; s->flags= avctx->flags; dsputil_init(&s->dsp, avctx); - width= s->width= avctx->width; - height= s->height= avctx->height; + s->width= avctx->width; + s->height= avctx->height; + assert(s->width>0 && s->height>0); + + return 0; +} + +static int decode_init(AVCodecContext *avctx) +{ + HYuvContext *s = avctx->priv_data; + + common_init(avctx); + memset(s->vlc, 0, 3*sizeof(VLC)); + avctx->coded_frame= &s->picture; + s->interlaced= s->height > 288; s->bgr32=1; - assert(width && height); //if(avctx->extradata) // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size); if(avctx->extradata_size){ @@ -359,7 +397,7 @@ s->bgr32=1; s->version=0; if(s->version==2){ - int method; + int method, interlace; method= ((uint8_t*)avctx->extradata)[0]; s->decorrelate= method&64 ? 1 : 0; @@ -367,6 +405,9 @@ s->bgr32=1; s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1]; if(s->bitstream_bpp==0) s->bitstream_bpp= avctx->bits_per_sample&~7; + interlace= (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4; + s->interlaced= (interlace==1) ? 1 : (interlace==2) ? 0 : s->interlaced; + s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0; if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0) return -1; @@ -394,13 +435,12 @@ s->bgr32=1; break; } s->bitstream_bpp= avctx->bits_per_sample & ~7; + s->context= 0; if(read_old_huffman_tables(s) < 0) return -1; } - s->interlaced= height > 288; - switch(s->bitstream_bpp){ case 12: avctx->pix_fmt = PIX_FMT_YUV420P; @@ -424,14 +464,16 @@ s->bgr32=1; assert(0); } -// printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); + alloc_temp(s); +// av_log(NULL, AV_LOG_DEBUG, "pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); + return 0; } -static void store_table(HYuvContext *s, uint8_t *len){ +static int store_table(HYuvContext *s, uint8_t *len, uint8_t *buf){ int i; - int index= s->avctx->extradata_size; + int index= 0; for(i=0; i<256;){ int val= len[i]; @@ -442,43 +484,31 @@ static void store_table(HYuvContext *s, uint8_t *len){ assert(val < 32 && val >0 && repeat<256 && repeat>0); if(repeat>7){ - ((uint8_t*)s->avctx->extradata)[index++]= val; - ((uint8_t*)s->avctx->extradata)[index++]= repeat; + buf[index++]= val; + buf[index++]= repeat; }else{ - ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5); + buf[index++]= val | (repeat<<5); } } - s->avctx->extradata_size= index; + return index; } static int encode_init(AVCodecContext *avctx) { HYuvContext *s = avctx->priv_data; - int i, j, width, height; + int i, j; - s->avctx= avctx; - s->flags= avctx->flags; - - dsputil_init(&s->dsp, avctx); - - width= s->width= avctx->width; - height= s->height= avctx->height; - - assert(width && height); + common_init(avctx); - avctx->extradata= av_mallocz(1024*30); - avctx->stats_out= av_mallocz(1024*30); + avctx->extradata= av_mallocz(1024*30); // 256*3+4 == 772 + avctx->stats_out= av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132 s->version=2; avctx->coded_frame= &s->picture; switch(avctx->pix_fmt){ case PIX_FMT_YUV420P: - if(avctx->strict_std_compliance>=0){ - av_log(avctx, AV_LOG_ERROR, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n"); - return -1; - } s->bitstream_bpp= 12; break; case PIX_FMT_YUV422P: @@ -491,10 +521,36 @@ static int encode_init(AVCodecContext *avctx) avctx->bits_per_sample= s->bitstream_bpp; s->decorrelate= s->bitstream_bpp >= 24; s->predictor= avctx->prediction_method; + s->interlaced= avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0; + if(avctx->context_model==1){ + s->context= avctx->context_model; + if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){ + av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n"); + return -1; + } + }else s->context= 0; + + if(avctx->codec->id==CODEC_ID_HUFFYUV){ + if(avctx->pix_fmt==PIX_FMT_YUV420P){ + av_log(avctx, AV_LOG_ERROR, "Error: YV12 is not supported by huffyuv; use vcodec=ffvhuff or format=422p\n"); + return -1; + } + if(avctx->context_model){ + av_log(avctx, AV_LOG_ERROR, "Error: per-frame huffman tables are not supported by huffyuv; use vcodec=ffvhuff\n"); + return -1; + } + if(s->interlaced != ( s->height > 288 )) + av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n"); + }else if(avctx->strict_std_compliance>=0){ + av_log(avctx, AV_LOG_ERROR, "This codec is under development; files encoded with it may not be decodeable with future versions!!! Set vstrict=-1 / -strict -1 to use it anyway.\n"); + return -1; + } ((uint8_t*)avctx->extradata)[0]= s->predictor; ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp; - ((uint8_t*)avctx->extradata)[2]= + ((uint8_t*)avctx->extradata)[2]= s->interlaced ? 0x10 : 0x20; + if(s->context) + ((uint8_t*)avctx->extradata)[2]|= 0x40; ((uint8_t*)avctx->extradata)[3]= 0; s->avctx->extradata_size= 4; @@ -533,17 +589,28 @@ static int encode_init(AVCodecContext *avctx) return -1; } - store_table(s, s->len[i]); + s->avctx->extradata_size+= + store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]); } - for(i=0; i<3; i++) - for(j=0; j<256; j++) - s->stats[i][j]= 0; + if(s->context){ + for(i=0; i<3; i++){ + int pels = s->width*s->height / (i?40:10); + for(j=0; j<256; j++){ + int d= FFMIN(j, 256-j); + s->stats[i][j]= pels/(d+1); + } + } + }else{ + for(i=0; i<3; i++) + for(j=0; j<256; j++) + s->stats[i][j]= 0; + } - s->interlaced= height > 288; - // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); + alloc_temp(s); + s->picture_number=0; return 0; @@ -573,9 +640,14 @@ static void decode_gray_bitstream(HYuvContext *s, int count){ } } -static void encode_422_bitstream(HYuvContext *s, int count){ +static int encode_422_bitstream(HYuvContext *s, int count){ int i; + if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 2*4*count){ + av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + return -1; + } + count/=2; if(s->flags&CODEC_FLAG_PASS1){ for(i=0; istats[0][ s->temp[0][2*i+1] ]++; s->stats[2][ s->temp[2][ i ] ]++; } + } + if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT) + return 0; + if(s->context){ + for(i=0; istats[0][ s->temp[0][2*i ] ]++; + put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); + s->stats[1][ s->temp[1][ i ] ]++; + put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); + s->stats[0][ s->temp[0][2*i+1] ]++; + put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); + s->stats[2][ s->temp[2][ i ] ]++; + put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); + } }else{ for(i=0; ipb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); @@ -592,23 +678,41 @@ static void encode_422_bitstream(HYuvContext *s, int count){ put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); } } + return 0; } -static void encode_gray_bitstream(HYuvContext *s, int count){ +static int encode_gray_bitstream(HYuvContext *s, int count){ int i; + if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 4*count){ + av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + return -1; + } + count/=2; if(s->flags&CODEC_FLAG_PASS1){ for(i=0; istats[0][ s->temp[0][2*i ] ]++; s->stats[0][ s->temp[0][2*i+1] ]++; } + } + if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT) + return 0; + + if(s->context){ + for(i=0; istats[0][ s->temp[0][2*i ] ]++; + put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); + s->stats[0][ s->temp[0][2*i+1] ]++; + put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); + } }else{ for(i=0; ipb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); } } + return 0; } static void decode_bgr_bitstream(HYuvContext *s, int count){ @@ -617,30 +721,30 @@ static void decode_bgr_bitstream(HYuvContext *s, int count){ if(s->decorrelate){ if(s->bitstream_bpp==24){ for(i=0; itemp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); - s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; - s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; + s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); + s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G]; + s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G]; } }else{ for(i=0; itemp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); - s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; - s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; + s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); + s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G]; + s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G]; get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! } } }else{ if(s->bitstream_bpp==24){ for(i=0; itemp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); - s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); - s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); + s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); + s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); + s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); } }else{ for(i=0; itemp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); - s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); - s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); + s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); + s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); + s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! } } @@ -681,17 +785,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 const int height= s->height; int fake_ystride, fake_ustride, fake_vstride; AVFrame * const p= &s->picture; + int table_size= 0; AVFrame *picture = data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; + s->bitstream_buffer= av_fast_realloc(s->bitstream_buffer, &s->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4); - init_get_bits(&s->gb, s->bitstream_buffer, buf_size*8); - if(p->data[0]) avctx->release_buffer(avctx, p); @@ -700,6 +801,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } + + if(s->context){ + table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size); + if(table_size < 0) + return -1; + } + + init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8); fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0]; fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1]; @@ -858,14 +967,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 const int last_line= (height-1)*p->linesize[0]; if(s->bitstream_bpp==32){ - p->data[0][last_line+3]= get_bits(&s->gb, 8); - leftr= p->data[0][last_line+2]= get_bits(&s->gb, 8); - leftg= p->data[0][last_line+1]= get_bits(&s->gb, 8); - leftb= p->data[0][last_line+0]= get_bits(&s->gb, 8); + skip_bits(&s->gb, 8); + leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8); + leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8); + leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8); }else{ - leftr= p->data[0][last_line+2]= get_bits(&s->gb, 8); - leftg= p->data[0][last_line+1]= get_bits(&s->gb, 8); - leftb= p->data[0][last_line+0]= get_bits(&s->gb, 8); + leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8); + leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8); + leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8); skip_bits(&s->gb, 8); } @@ -881,7 +990,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb); if(s->predictor == PLANE){ - if((y&s->interlaced)==0){ + if((y&s->interlaced)==0 && yheight-1-s->interlaced){ s->dsp.add_bytes(p->data[0] + p->linesize[0]*y, p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride); } @@ -906,11 +1015,23 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 return (get_bits_count(&s->gb)+31)/32*4; } +static int common_end(HYuvContext *s){ + int i; + + for(i=0; i<3; i++){ + av_freep(&s->temp[i]); + } + return 0; +} + static int decode_end(AVCodecContext *avctx) { HYuvContext *s = avctx->priv_data; int i; + common_end(s); + av_freep(&s->bitstream_buffer); + for(i=0; i<3; i++){ free_vlc(&s->vlc[i]); } @@ -928,14 +1049,27 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1]; const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2]; AVFrame * const p= &s->picture; - int i, size; + int i, j, size=0; - init_put_bits(&s->pb, buf, buf_size); - *p = *pict; p->pict_type= FF_I_TYPE; p->key_frame= 1; + if(s->context){ + for(i=0; i<3; i++){ + generate_len_table(s->len[i], s->stats[i], 256); + if(generate_bits_table(s->bits[i], s->len[i])<0) + return -1; + size+= store_table(s, s->len[i], &buf[size]); + } + + for(i=0; i<3; i++) + for(j=0; j<256; j++) + s->stats[i][j] >>= 1; + } + + init_put_bits(&s->pb, buf+size, buf_size-size); + if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){ int lefty, leftu, leftv, y, cy; @@ -963,8 +1097,8 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, } lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty); - leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ystride, 2, leftu); - leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_ystride, 2, leftv); + leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ustride, 2, leftu); + leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_vstride, 2, leftv); encode_422_bitstream(s, 4); @@ -1026,11 +1160,11 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, if(s->predictor == PLANE && s->interlaced < cy){ s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width); s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2); - s->dsp.diff_bytes(s->temp[2] + 1250, vdst, vdst - fake_vstride, width2); + s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2); lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty); leftu= sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu); - leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + 1250, width2, leftv); + leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv); }else{ lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty); leftu= sub_left_prediction(s, s->temp[1], udst, width2, leftu); @@ -1045,23 +1179,27 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, } emms_c(); - size= (put_bits_count(&s->pb)+31)/32; + size+= (put_bits_count(&s->pb)+31)/8; + size/= 4; if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){ int j; char *p= avctx->stats_out; + char *end= p + 1024*30; for(i=0; i<3; i++){ for(j=0; j<256; j++){ - sprintf(p, "%llu ", s->stats[i][j]); + snprintf(p, end-p, "%llu ", s->stats[i][j]); p+= strlen(p); s->stats[i][j]= 0; } - sprintf(p, "\n"); + snprintf(p, end-p, "\n"); p++; } - }else{ + } + if(!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)){ flush_put_bits(&s->pb); s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); + avctx->stats_out[0] = '\0'; } s->picture_number++; @@ -1071,7 +1209,9 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, static int encode_end(AVCodecContext *avctx) { -// HYuvContext *s = avctx->priv_data; + HYuvContext *s = avctx->priv_data; + + common_end(s); av_freep(&avctx->extradata); av_freep(&avctx->stats_out); @@ -1079,12 +1219,6 @@ static int encode_end(AVCodecContext *avctx) return 0; } -static const AVOption huffyuv_options[] = -{ - AVOPTION_CODEC_INT("prediction_method", "prediction_method", prediction_method, 0, 2, 0), - AVOPTION_END() -}; - AVCodec huffyuv_decoder = { "huffyuv", CODEC_TYPE_VIDEO, @@ -1098,6 +1232,19 @@ AVCodec huffyuv_decoder = { NULL }; +AVCodec ffvhuff_decoder = { + "ffvhuff", + CODEC_TYPE_VIDEO, + CODEC_ID_FFVHUFF, + sizeof(HYuvContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, + NULL +}; + #ifdef CONFIG_ENCODERS AVCodec huffyuv_encoder = { @@ -1108,7 +1255,16 @@ AVCodec huffyuv_encoder = { encode_init, encode_frame, encode_end, - .options = huffyuv_options, +}; + +AVCodec ffvhuff_encoder = { + "ffvhuff", + CODEC_TYPE_VIDEO, + CODEC_ID_FFVHUFF, + sizeof(HYuvContext), + encode_init, + encode_frame, + encode_end, }; #endif //CONFIG_ENCODERS diff --git a/src/libffmpeg/libavcodec/i386/cputest.c b/src/libffmpeg/libavcodec/i386/cputest.c index b50d653c4..593e0550d 100644 --- a/src/libffmpeg/libavcodec/i386/cputest.c +++ b/src/libffmpeg/libavcodec/i386/cputest.c @@ -4,12 +4,20 @@ #include #include "../dsputil.h" +#ifdef ARCH_X86_64 +# define REG_b "rbx" +# define REG_S "rsi" +#else +# define REG_b "ebx" +# define REG_S "esi" +#endif + /* ebx saving is necessary for PIC. gcc seems unable to see it alone */ #define cpuid(index,eax,ebx,ecx,edx)\ __asm __volatile\ - ("movl %%ebx, %%esi\n\t"\ + ("mov %%"REG_b", %%"REG_S"\n\t"\ "cpuid\n\t"\ - "xchgl %%ebx, %%esi"\ + "xchg %%"REG_b", %%"REG_S\ : "=a" (eax), "=S" (ebx),\ "=c" (ecx), "=d" (edx)\ : "0" (index)); @@ -17,82 +25,72 @@ /* Function to test if multimedia instructions are supported... */ int mm_support(void) { - int rval; + int rval = 0; int eax, ebx, ecx, edx; + int max_std_level, max_ext_level, std_caps=0, ext_caps=0; + long a, c; __asm__ __volatile__ ( /* See if CPUID instruction is supported ... */ /* ... Get copies of EFLAGS into eax and ecx */ "pushf\n\t" - "popl %0\n\t" - "movl %0, %1\n\t" + "pop %0\n\t" + "mov %0, %1\n\t" /* ... Toggle the ID bit in one copy and store */ /* to the EFLAGS reg */ - "xorl $0x200000, %0\n\t" + "xor $0x200000, %0\n\t" "push %0\n\t" "popf\n\t" /* ... Get the (hopefully modified) EFLAGS */ "pushf\n\t" - "popl %0\n\t" - : "=a" (eax), "=c" (ecx) + "pop %0\n\t" + : "=a" (a), "=c" (c) : : "cc" ); - if (eax == ecx) + if (a == c) return 0; /* CPUID not supported */ - - cpuid(0, eax, ebx, ecx, edx); - if (ebx == 0x756e6547 && - edx == 0x49656e69 && - ecx == 0x6c65746e) { - - /* intel */ - inteltest: - cpuid(1, eax, ebx, ecx, edx); - if ((edx & 0x00800000) == 0) - return 0; - rval = MM_MMX; - if (edx & 0x02000000) + cpuid(0, max_std_level, ebx, ecx, edx); + + if(max_std_level >= 1){ + cpuid(1, eax, ebx, ecx, std_caps); + if (std_caps & (1<<23)) + rval |= MM_MMX; + if (std_caps & (1<<25)) rval |= MM_MMXEXT | MM_SSE; - if (edx & 0x04000000) + if (std_caps & (1<<26)) rval |= MM_SSE2; - return rval; - } else if (ebx == 0x68747541 && + } + + cpuid(0x80000000, max_ext_level, ebx, ecx, edx); + + if(max_ext_level >= 0x80000001){ + cpuid(0x80000001, eax, ebx, ecx, ext_caps); + if (ext_caps & (1<<31)) + rval |= MM_3DNOW; + if (ext_caps & (1<<30)) + rval |= MM_3DNOWEXT; + if (ext_caps & (1<<23)) + rval |= MM_MMX; + } + + cpuid(0, eax, ebx, ecx, edx); + if ( ebx == 0x68747541 && edx == 0x69746e65 && ecx == 0x444d4163) { /* AMD */ - cpuid(0x80000000, eax, ebx, ecx, edx); - if ((unsigned)eax < 0x80000001) - goto inteltest; - cpuid(0x80000001, eax, ebx, ecx, edx); - if ((edx & 0x00800000) == 0) - return 0; - rval = MM_MMX; - if (edx & 0x80000000) - rval |= MM_3DNOW; - if (edx & 0x00400000) + if(ext_caps & (1<<22)) rval |= MM_MMXEXT; - return rval; } else if (ebx == 0x746e6543 && edx == 0x48727561 && ecx == 0x736c7561) { /* "CentaurHauls" */ /* VIA C3 */ - cpuid(0x80000000, eax, ebx, ecx, edx); - if ((unsigned)eax < 0x80000001) - goto inteltest; - cpuid(0x80000001, eax, ebx, ecx, edx); - rval = 0; - if( edx & ( 1 << 31) ) - rval |= MM_3DNOW; - if( edx & ( 1 << 23) ) - rval |= MM_MMX; - if( edx & ( 1 << 24) ) + if(ext_caps & (1<<24)) rval |= MM_MMXEXT; - return rval; } else if (ebx == 0x69727943 && edx == 0x736e4978 && ecx == 0x64616574) { @@ -105,18 +103,21 @@ int mm_support(void) According to the table, the only CPU which supports level 2 is also the only one which supports extended CPUID levels. */ - if (eax != 2) - goto inteltest; - cpuid(0x80000001, eax, ebx, ecx, edx); - if ((eax & 0x00800000) == 0) - return 0; - rval = MM_MMX; - if (eax & 0x01000000) + if (eax < 2) + return rval; + if (ext_caps & (1<<24)) rval |= MM_MMXEXT; - return rval; - } else { - return 0; } +#if 0 + av_log(NULL, AV_LOG_DEBUG, "%s%s%s%s%s%s\n", + (rval&MM_MMX) ? "MMX ":"", + (rval&MM_MMXEXT) ? "MMX2 ":"", + (rval&MM_SSE) ? "SSE ":"", + (rval&MM_SSE2) ? "SSE2 ":"", + (rval&MM_3DNOW) ? "3DNow ":"", + (rval&MM_3DNOWEXT) ? "3DNowExt ":""); +#endif + return rval; } #ifdef __TEST__ @@ -124,7 +125,7 @@ int main ( void ) { int mm_flags; mm_flags = mm_support(); - printf("mm_support = 0x%08u\n",mm_flags); + printf("mm_support = 0x%08X\n",mm_flags); return 0; } #endif diff --git a/src/libffmpeg/libavcodec/i386/dsputil_mmx.c b/src/libffmpeg/libavcodec/i386/dsputil_mmx.c index 11504e225..550122673 100644 --- a/src/libffmpeg/libavcodec/i386/dsputil_mmx.c +++ b/src/libffmpeg/libavcodec/i386/dsputil_mmx.c @@ -22,6 +22,7 @@ #include "../dsputil.h" #include "../simple_idct.h" +#include "../mpegvideo.h" #include "mmx.h" //#undef NDEBUG @@ -38,7 +39,9 @@ static const uint64_t mm_wtwo attribute_used __attribute__ ((aligned(8))) = 0x00 static const uint64_t ff_pw_20 attribute_used __attribute__ ((aligned(8))) = 0x0014001400140014ULL; static const uint64_t ff_pw_3 attribute_used __attribute__ ((aligned(8))) = 0x0003000300030003ULL; +static const uint64_t ff_pw_5 attribute_used __attribute__ ((aligned(8))) = 0x0005000500050005ULL; static const uint64_t ff_pw_16 attribute_used __attribute__ ((aligned(8))) = 0x0010001000100010ULL; +static const uint64_t ff_pw_32 attribute_used __attribute__ ((aligned(8))) = 0x0020002000200020ULL; static const uint64_t ff_pw_15 attribute_used __attribute__ ((aligned(8))) = 0x000F000F000F000FULL; static const uint64_t ff_pb_FC attribute_used __attribute__ ((aligned(8))) = 0xFCFCFCFCFCFCFCFCULL; @@ -184,7 +187,7 @@ static const uint64_t ff_pb_FC attribute_used __attribute__ ((aligned(8))) = 0xF static void get_pixels_mmx(DCTELEM *block, const uint8_t *pixels, int line_size) { asm volatile( - "movl $-128, %%eax \n\t" + "mov $-128, %%"REG_a" \n\t" "pxor %%mm7, %%mm7 \n\t" ".balign 16 \n\t" "1: \n\t" @@ -196,16 +199,16 @@ static void get_pixels_mmx(DCTELEM *block, const uint8_t *pixels, int line_size) "punpckhbw %%mm7, %%mm1 \n\t" "punpcklbw %%mm7, %%mm2 \n\t" "punpckhbw %%mm7, %%mm3 \n\t" - "movq %%mm0, (%1, %%eax)\n\t" - "movq %%mm1, 8(%1, %%eax)\n\t" - "movq %%mm2, 16(%1, %%eax)\n\t" - "movq %%mm3, 24(%1, %%eax)\n\t" - "addl %3, %0 \n\t" - "addl $32, %%eax \n\t" + "movq %%mm0, (%1, %%"REG_a")\n\t" + "movq %%mm1, 8(%1, %%"REG_a")\n\t" + "movq %%mm2, 16(%1, %%"REG_a")\n\t" + "movq %%mm3, 24(%1, %%"REG_a")\n\t" + "add %3, %0 \n\t" + "add $32, %%"REG_a" \n\t" "js 1b \n\t" : "+r" (pixels) - : "r" (block+64), "r" (line_size), "r" (line_size*2) - : "%eax" + : "r" (block+64), "r" ((long)line_size), "r" ((long)line_size*2) + : "%"REG_a ); } @@ -213,7 +216,7 @@ static inline void diff_pixels_mmx(DCTELEM *block, const uint8_t *s1, const uint { asm volatile( "pxor %%mm7, %%mm7 \n\t" - "movl $-128, %%eax \n\t" + "mov $-128, %%"REG_a" \n\t" ".balign 16 \n\t" "1: \n\t" "movq (%0), %%mm0 \n\t" @@ -226,15 +229,15 @@ static inline void diff_pixels_mmx(DCTELEM *block, const uint8_t *s1, const uint "punpckhbw %%mm7, %%mm3 \n\t" "psubw %%mm2, %%mm0 \n\t" "psubw %%mm3, %%mm1 \n\t" - "movq %%mm0, (%2, %%eax)\n\t" - "movq %%mm1, 8(%2, %%eax)\n\t" - "addl %3, %0 \n\t" - "addl %3, %1 \n\t" - "addl $16, %%eax \n\t" + "movq %%mm0, (%2, %%"REG_a")\n\t" + "movq %%mm1, 8(%2, %%"REG_a")\n\t" + "add %3, %0 \n\t" + "add %3, %1 \n\t" + "add $16, %%"REG_a" \n\t" "jnz 1b \n\t" : "+r" (s1), "+r" (s2) - : "r" (block+64), "r" (stride) - : "%eax" + : "r" (block+64), "r" ((long)stride) + : "%"REG_a ); } #endif //CONFIG_ENCODERS @@ -265,7 +268,7 @@ void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size "movq %%mm2, (%0, %1)\n\t" "movq %%mm4, (%0, %1, 2)\n\t" "movq %%mm6, (%0, %2)\n\t" - ::"r" (pix), "r" (line_size), "r" (line_size*3), "m"(*p) + ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "m"(*p) :"memory"); pix += line_size*4; p += 32; @@ -290,7 +293,7 @@ void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size "movq %%mm2, (%0, %1)\n\t" "movq %%mm4, (%0, %1, 2)\n\t" "movq %%mm6, (%0, %2)\n\t" - ::"r" (pix), "r" (line_size), "r" (line_size*3), "r"(p) + ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "r"(p) :"memory"); } @@ -353,36 +356,62 @@ void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size } while (--i); } +static void put_pixels4_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h) +{ + __asm __volatile( + "lea (%3, %3), %%"REG_a" \n\t" + ".balign 8 \n\t" + "1: \n\t" + "movd (%1), %%mm0 \n\t" + "movd (%1, %3), %%mm1 \n\t" + "movd %%mm0, (%2) \n\t" + "movd %%mm1, (%2, %3) \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" + "movd (%1), %%mm0 \n\t" + "movd (%1, %3), %%mm1 \n\t" + "movd %%mm0, (%2) \n\t" + "movd %%mm1, (%2, %3) \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" + "subl $4, %0 \n\t" + "jnz 1b \n\t" + : "+g"(h), "+r" (pixels), "+r" (block) + : "r"((long)line_size) + : "%"REG_a, "memory" + ); +} + static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" : "+g"(h), "+r" (pixels), "+r" (block) - : "r"(line_size) - : "%eax", "memory" + : "r"((long)line_size) + : "%"REG_a, "memory" ); } static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" @@ -393,8 +422,8 @@ static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_siz "movq %%mm4, 8(%2) \n\t" "movq %%mm1, (%2, %3) \n\t" "movq %%mm5, 8(%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq 8(%1), %%mm4 \n\t" "movq (%1, %3), %%mm1 \n\t" @@ -403,13 +432,13 @@ static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_siz "movq %%mm4, 8(%2) \n\t" "movq %%mm1, (%2, %3) \n\t" "movq %%mm5, 8(%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" : "+g"(h), "+r" (pixels), "+r" (block) - : "r"(line_size) - : "%eax", "memory" + : "r"((long)line_size) + : "%"REG_a, "memory" ); } @@ -417,16 +446,16 @@ static void clear_blocks_mmx(DCTELEM *blocks) { __asm __volatile( "pxor %%mm7, %%mm7 \n\t" - "movl $-128*6, %%eax \n\t" + "mov $-128*6, %%"REG_a" \n\t" "1: \n\t" - "movq %%mm7, (%0, %%eax) \n\t" - "movq %%mm7, 8(%0, %%eax) \n\t" - "movq %%mm7, 16(%0, %%eax) \n\t" - "movq %%mm7, 24(%0, %%eax) \n\t" - "addl $32, %%eax \n\t" + "movq %%mm7, (%0, %%"REG_a") \n\t" + "movq %%mm7, 8(%0, %%"REG_a") \n\t" + "movq %%mm7, 16(%0, %%"REG_a") \n\t" + "movq %%mm7, 24(%0, %%"REG_a") \n\t" + "add $32, %%"REG_a" \n\t" " js 1b \n\t" - : : "r" (((int)blocks)+128*6) - : "%eax" + : : "r" (((uint8_t *)blocks)+128*6) + : "%"REG_a ); } @@ -434,7 +463,7 @@ static void clear_blocks_mmx(DCTELEM *blocks) static int pix_sum16_mmx(uint8_t * pix, int line_size){ const int h=16; int sum; - int index= -line_size*h; + long index= -line_size*h; __asm __volatile( "pxor %%mm7, %%mm7 \n\t" @@ -452,7 +481,7 @@ static int pix_sum16_mmx(uint8_t * pix, int line_size){ "paddw %%mm2, %%mm3 \n\t" "paddw %%mm1, %%mm3 \n\t" "paddw %%mm3, %%mm6 \n\t" - "addl %3, %1 \n\t" + "add %3, %1 \n\t" " js 1b \n\t" "movq %%mm6, %%mm5 \n\t" "psrlq $32, %%mm6 \n\t" @@ -463,7 +492,7 @@ static int pix_sum16_mmx(uint8_t * pix, int line_size){ "movd %%mm6, %0 \n\t" "andl $0xFFFF, %0 \n\t" : "=&r" (sum), "+r" (index) - : "r" (pix - index), "r" (line_size) + : "r" (pix - index), "r" ((long)line_size) ); return sum; @@ -471,7 +500,7 @@ static int pix_sum16_mmx(uint8_t * pix, int line_size){ #endif //CONFIG_ENCODERS static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){ - int i=0; + long i=0; asm volatile( "1: \n\t" "movq (%1, %0), %%mm0 \n\t" @@ -482,11 +511,11 @@ static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){ "movq 8(%2, %0), %%mm1 \n\t" "paddb %%mm0, %%mm1 \n\t" "movq %%mm1, 8(%2, %0) \n\t" - "addl $16, %0 \n\t" - "cmpl %3, %0 \n\t" + "add $16, %0 \n\t" + "cmp %3, %0 \n\t" " jb 1b \n\t" : "+r" (i) - : "r"(src), "r"(dst), "r"(w-15) + : "r"(src), "r"(dst), "r"((long)w-15) ); for(; iavctx->nsse_weight; + else return score1 + ABS(score2)*8; +} + +static int nsse8_mmx(MpegEncContext *c, uint8_t * pix1, uint8_t * pix2, int line_size, int h) { + int score1= sse8_mmx(c, pix1, pix2, line_size, h); + int score2= hf_noise8_mmx(pix1, line_size, h) - hf_noise8_mmx(pix2, line_size, h); + + if(c) return score1 + ABS(score2)*c->avctx->nsse_weight; + else return score1 + ABS(score2)*8; +} + static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) { int tmp; @@ -779,7 +1102,7 @@ static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_si #define SUM(in0, in1, out0, out1) \ "movq (%0), %%mm2\n"\ "movq 8(%0), %%mm3\n"\ - "addl %2,%0\n"\ + "add %2,%0\n"\ "movq %%mm2, " #out0 "\n"\ "movq %%mm3, " #out1 "\n"\ "psubusb " #in0 ", %%mm2\n"\ @@ -806,7 +1129,7 @@ static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_si "pxor %%mm7,%%mm7\n" "movq (%0),%%mm0\n" "movq 8(%0),%%mm1\n" - "addl %2,%0\n" + "add %2,%0\n" "subl $2, %%ecx\n" SUM(%%mm0, %%mm1, %%mm4, %%mm5) "1:\n" @@ -826,7 +1149,7 @@ static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_si "paddw %%mm6,%%mm0\n" "movd %%mm0,%1\n" : "+r" (pix), "=r"(tmp) - : "r" (line_size) , "m" (h) + : "r" ((long)line_size) , "m" (h) : "%ecx"); return tmp & 0xFFFF; } @@ -841,7 +1164,7 @@ static int vsad_intra16_mmx2(void *v, uint8_t * pix, uint8_t * dummy, int line_s #define SUM(in0, in1, out0, out1) \ "movq (%0), " #out0 "\n"\ "movq 8(%0), " #out1 "\n"\ - "addl %2,%0\n"\ + "add %2,%0\n"\ "psadbw " #out0 ", " #in0 "\n"\ "psadbw " #out1 ", " #in1 "\n"\ "paddw " #in1 ", " #in0 "\n"\ @@ -853,7 +1176,7 @@ static int vsad_intra16_mmx2(void *v, uint8_t * pix, uint8_t * dummy, int line_s "pxor %%mm7,%%mm7\n" "movq (%0),%%mm0\n" "movq 8(%0),%%mm1\n" - "addl %2,%0\n" + "add %2,%0\n" "subl $2, %%ecx\n" SUM(%%mm0, %%mm1, %%mm4, %%mm5) "1:\n" @@ -867,7 +1190,7 @@ static int vsad_intra16_mmx2(void *v, uint8_t * pix, uint8_t * dummy, int line_s "movd %%mm6,%1\n" : "+r" (pix), "=r"(tmp) - : "r" (line_size) , "m" (h) + : "r" ((long)line_size) , "m" (h) : "%ecx"); return tmp; } @@ -885,8 +1208,8 @@ static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, in "movq (%1)," #out0 "\n"\ "movq 8(%0),%%mm3\n"\ "movq 8(%1)," #out1 "\n"\ - "addl %3,%0\n"\ - "addl %3,%1\n"\ + "add %3,%0\n"\ + "add %3,%1\n"\ "psubb " #out0 ", %%mm2\n"\ "psubb " #out1 ", %%mm3\n"\ "pxor %%mm7, %%mm2\n"\ @@ -921,8 +1244,8 @@ static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, in "movq (%1),%%mm2\n" "movq 8(%0),%%mm1\n" "movq 8(%1),%%mm3\n" - "addl %3,%0\n" - "addl %3,%1\n" + "add %3,%0\n" + "add %3,%1\n" "subl $2, %%ecx\n" "psubb %%mm2, %%mm0\n" "psubb %%mm3, %%mm1\n" @@ -946,7 +1269,7 @@ static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, in "paddw %%mm6,%%mm0\n" "movd %%mm0,%2\n" : "+r" (pix1), "+r" (pix2), "=r"(tmp) - : "r" (line_size) , "m" (h) + : "r" ((long)line_size) , "m" (h) : "%ecx"); return tmp & 0x7FFF; } @@ -964,8 +1287,8 @@ static int vsad16_mmx2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, i "movq (%1),%%mm2\n"\ "movq 8(%0)," #out1 "\n"\ "movq 8(%1),%%mm3\n"\ - "addl %3,%0\n"\ - "addl %3,%1\n"\ + "add %3,%0\n"\ + "add %3,%1\n"\ "psubb %%mm2, " #out0 "\n"\ "psubb %%mm3, " #out1 "\n"\ "pxor %%mm7, " #out0 "\n"\ @@ -985,8 +1308,8 @@ static int vsad16_mmx2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, i "movq (%1),%%mm2\n" "movq 8(%0),%%mm1\n" "movq 8(%1),%%mm3\n" - "addl %3,%0\n" - "addl %3,%1\n" + "add %3,%0\n" + "add %3,%1\n" "subl $2, %%ecx\n" "psubb %%mm2, %%mm0\n" "psubb %%mm3, %%mm1\n" @@ -1004,14 +1327,14 @@ static int vsad16_mmx2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, i "movd %%mm6,%2\n" : "+r" (pix1), "+r" (pix2), "=r"(tmp) - : "r" (line_size) , "m" (h) + : "r" ((long)line_size) , "m" (h) : "%ecx"); return tmp; } #undef SUM static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){ - int i=0; + long i=0; asm volatile( "1: \n\t" "movq (%2, %0), %%mm0 \n\t" @@ -1022,20 +1345,20 @@ static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){ "movq 8(%1, %0), %%mm1 \n\t" "psubb %%mm0, %%mm1 \n\t" "movq %%mm1, 8(%3, %0) \n\t" - "addl $16, %0 \n\t" - "cmpl %4, %0 \n\t" + "add $16, %0 \n\t" + "cmp %4, %0 \n\t" " jb 1b \n\t" : "+r" (i) - : "r"(src1), "r"(src2), "r"(dst), "r"(w-15) + : "r"(src1), "r"(src2), "r"(dst), "r"((long)w-15) ); for(; iavg_ ## postfix1 = avg_ ## postfix2; static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){ - int i=0; + long i=0; assert(ABS(scale) < 256); scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT; @@ -2025,8 +2859,8 @@ static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[6 "paddd %%mm1, %%mm0 \n\t" "psrld $4, %%mm0 \n\t" "paddd %%mm0, %%mm7 \n\t" - "addl $16, %0 \n\t" - "cmpl $128, %0 \n\t" //FIXME optimize & bench + "add $16, %0 \n\t" + "cmp $128, %0 \n\t" //FIXME optimize & bench " jb 1b \n\t" "movq %%mm7, %%mm6 \n\t" "psrlq $32, %%mm7 \n\t" @@ -2041,7 +2875,7 @@ static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[6 } static void add_8x8basis_mmx(int16_t rem[64], int16_t basis[64], int scale){ - int i=0; + long i=0; if(ABS(scale) < 256){ scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT; @@ -2064,8 +2898,8 @@ static void add_8x8basis_mmx(int16_t rem[64], int16_t basis[64], int scale){ "paddw 8(%2, %0), %%mm1 \n\t" "movq %%mm0, (%2, %0) \n\t" "movq %%mm1, 8(%2, %0) \n\t" - "addl $16, %0 \n\t" - "cmpl $128, %0 \n\t" //FIXME optimize & bench + "add $16, %0 \n\t" + "cmp $128, %0 \n\t" //FIXME optimize & bench " jb 1b \n\t" : "+r" (i) @@ -2146,23 +2980,24 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) } } #endif //CONFIG_ENCODERS - - if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){ - c->idct_put= ff_simple_idct_put_mmx; - c->idct_add= ff_simple_idct_add_mmx; - c->idct = ff_simple_idct_mmx; - c->idct_permutation_type= FF_SIMPLE_IDCT_PERM; - }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){ - if(mm_flags & MM_MMXEXT){ - c->idct_put= ff_libmpeg2mmx2_idct_put; - c->idct_add= ff_libmpeg2mmx2_idct_add; - c->idct = ff_mmxext_idct; - }else{ - c->idct_put= ff_libmpeg2mmx_idct_put; - c->idct_add= ff_libmpeg2mmx_idct_add; - c->idct = ff_mmx_idct; + if(avctx->lowres==0){ + if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){ + c->idct_put= ff_simple_idct_put_mmx; + c->idct_add= ff_simple_idct_add_mmx; + c->idct = ff_simple_idct_mmx; + c->idct_permutation_type= FF_SIMPLE_IDCT_PERM; + }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){ + if(mm_flags & MM_MMXEXT){ + c->idct_put= ff_libmpeg2mmx2_idct_put; + c->idct_add= ff_libmpeg2mmx2_idct_add; + c->idct = ff_mmxext_idct; + }else{ + c->idct_put= ff_libmpeg2mmx_idct_put; + c->idct_add= ff_libmpeg2mmx_idct_add; + c->idct = ff_mmx_idct; + } + c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM; } - c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM; } /* VP3 optimized DSP functions */ @@ -2235,8 +3070,11 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) c->pix_norm1 = pix_norm1_mmx; c->sse[0] = sse16_mmx; + c->sse[1] = sse8_mmx; c->vsad[4]= vsad_intra16_mmx; + c->nsse[0] = nsse16_mmx; + c->nsse[1] = nsse8_mmx; if(!(avctx->flags & CODEC_FLAG_BITEXACT)){ c->vsad[0] = vsad16_mmx; } @@ -2319,6 +3157,33 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_mmx2) #endif +//FIXME 3dnow too +#define dspfunc(PFX, IDX, NUM) \ + c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_mmx2; \ + c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_mmx2; \ + c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_mmx2; \ + c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_mmx2; \ + c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_mmx2; \ + c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_mmx2; \ + c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_mmx2; \ + c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_mmx2 + + dspfunc(put_h264_qpel, 0, 16); + dspfunc(put_h264_qpel, 1, 8); + dspfunc(put_h264_qpel, 2, 4); + dspfunc(avg_h264_qpel, 0, 16); + dspfunc(avg_h264_qpel, 1, 8); + dspfunc(avg_h264_qpel, 2, 4); +#undef dspfunc + #ifdef CONFIG_ENCODERS c->sub_hfyu_median_prediction= sub_hfyu_median_prediction_mmx2; #endif //CONFIG_ENCODERS @@ -2378,6 +3243,31 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_3dnow) SET_QPEL_FUNC(qpel_pixels_tab[1][14], qpel8_mc23_3dnow) SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_3dnow) + +#define dspfunc(PFX, IDX, NUM) \ + c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_3dnow; \ + c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_3dnow; \ + c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_3dnow; \ + c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_3dnow; \ + c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_3dnow; \ + c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_3dnow; \ + c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_3dnow; \ + c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_3dnow + + dspfunc(put_h264_qpel, 0, 16); + dspfunc(put_h264_qpel, 1, 8); + dspfunc(put_h264_qpel, 2, 4); + dspfunc(avg_h264_qpel, 0, 16); + dspfunc(avg_h264_qpel, 1, 8); + dspfunc(avg_h264_qpel, 2, 4); } } diff --git a/src/libffmpeg/libavcodec/i386/dsputil_mmx_avg.h b/src/libffmpeg/libavcodec/i386/dsputil_mmx_avg.h index 052aad75c..c70891304 100644 --- a/src/libffmpeg/libavcodec/i386/dsputil_mmx_avg.h +++ b/src/libffmpeg/libavcodec/i386/dsputil_mmx_avg.h @@ -28,7 +28,7 @@ static void DEF(put_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" @@ -36,59 +36,305 @@ static void DEF(put_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_ PAVGB" 1(%1, %3), %%mm1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" PAVGB" 1(%1), %%mm0 \n\t" PAVGB" 1(%1, %3), %%mm1 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } -static __attribute__((unused)) void DEF(put_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +static void DEF(put_pixels4_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) { __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movd (%1), %%mm0 \n\t" + "movd (%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $4, %2 \n\t" + PAVGB" %%mm1, %%mm0 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movd (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movd (%1), %%mm1 \n\t" + "movd (%2), %%mm2 \n\t" + "movd 4(%2), %%mm3 \n\t" + "add %4, %1 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movd %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "movd (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movd (%1), %%mm1 \n\t" + "movd 8(%2), %%mm2 \n\t" + "movd 12(%2), %%mm3 \n\t" + "add %4, %1 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movd %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "add $16, %2 \n\t" + "subl $4, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +} + + +static void DEF(put_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq (%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $8, %2 \n\t" + PAVGB" %%mm1, %%mm0 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movq (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movq (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" (%2), %%mm0 \n\t" + PAVGB" 8(%2), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movq %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "movq (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movq (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" 16(%2), %%mm0 \n\t" + PAVGB" 24(%2), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movq %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" + "subl $4, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) + :"r"(src1Stride), "r"(dstStride) + :"memory");*/ +} + +static void DEF(put_no_rnd_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "pcmpeqb %%mm6, %%mm6 \n\t" + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq (%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $8, %2 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + PAVGB" %%mm1, %%mm0 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movq (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movq (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + "movq (%2), %%mm2 \n\t" + "movq 8(%2), %%mm3 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "pxor %%mm6, %%mm2 \n\t" + "pxor %%mm6, %%mm3 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movq %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "movq (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movq (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + "movq 16(%2), %%mm2 \n\t" + "movq 24(%2), %%mm3 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "pxor %%mm6, %%mm2 \n\t" + "pxor %%mm6, %%mm3 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "movq %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" + "subl $4, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) + :"r"(src1Stride), "r"(dstStride) + :"memory");*/ +} + +static void DEF(avg_pixels4_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movd (%1), %%mm0 \n\t" + "movd (%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $4, %2 \n\t" + PAVGB" %%mm1, %%mm0 \n\t" + PAVGB" (%3), %%mm0 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movd (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movd (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" (%2), %%mm0 \n\t" + PAVGB" 4(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + PAVGB" (%3), %%mm1 \n\t" + "movd %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "movd (%1), %%mm0 \n\t" + "add %4, %1 \n\t" + "movd (%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" 8(%2), %%mm0 \n\t" + PAVGB" 12(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" + "movd %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + PAVGB" (%3), %%mm1 \n\t" + "movd %%mm1, (%3) \n\t" + "add %5, %3 \n\t" + "add $16, %2 \n\t" + "subl $4, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +} + + +static void DEF(avg_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq (%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $8, %2 \n\t" + PAVGB" %%mm1, %%mm0 \n\t" + PAVGB" (%3), %%mm0 \n\t" + "movq %%mm0, (%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" "movq (%1), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGB" (%2), %%mm0 \n\t" PAVGB" 8(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" "movq %%mm0, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" + PAVGB" (%3), %%mm1 \n\t" "movq %%mm1, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq (%1), %%mm0 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" "movq (%1), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGB" 16(%2), %%mm0 \n\t" PAVGB" 24(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" "movq %%mm0, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" + PAVGB" (%3), %%mm1 \n\t" "movq %%mm1, (%3) \n\t" - "addl %5, %3 \n\t" - "addl $32, %2 \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" - :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) :"r"(src1Stride), "r"(dstStride) - :"memory"); + :"memory");*/ } static void DEF(put_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" @@ -102,8 +348,8 @@ static void DEF(put_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line "movq %%mm1, (%2, %3) \n\t" "movq %%mm2, 8(%2) \n\t" "movq %%mm3, 8(%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm1 \n\t" "movq 8(%1), %%mm2 \n\t" @@ -112,45 +358,192 @@ static void DEF(put_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line PAVGB" 1(%1, %3), %%mm1 \n\t" PAVGB" 9(%1), %%mm2 \n\t" PAVGB" 9(%1, %3), %%mm3 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" "movq %%mm2, 8(%2) \n\t" "movq %%mm3, 8(%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); +} + +static void DEF(put_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + PAVGB" (%2), %%mm0 \n\t" + PAVGB" 8(%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $16, %2 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" (%2), %%mm0 \n\t" + PAVGB" 8(%2), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "add %4, %1 \n\t" + PAVGB" 16(%2), %%mm0 \n\t" + PAVGB" 24(%2), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" + "subl $2, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) + :"r"(src1Stride), "r"(dstStride) + :"memory");*/ } -static __attribute__((unused)) void DEF(put_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +static void DEF(avg_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) { __asm __volatile( + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + PAVGB" (%2), %%mm0 \n\t" + PAVGB" 8(%2), %%mm1 \n\t" + "add %4, %1 \n\t" + "add $16, %2 \n\t" + PAVGB" (%3), %%mm0 \n\t" + PAVGB" 8(%3), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq 8(%1), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGB" (%2), %%mm0 \n\t" PAVGB" 8(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" + PAVGB" 8(%3), %%mm1 \n\t" "movq %%mm0, (%3) \n\t" "movq %%mm1, 8(%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq (%1), %%mm0 \n\t" "movq 8(%1), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGB" 16(%2), %%mm0 \n\t" PAVGB" 24(%2), %%mm1 \n\t" + PAVGB" (%3), %%mm0 \n\t" + PAVGB" 8(%3), %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" + "subl $2, %0 \n\t" + "jnz 1b \n\t" +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) + :"r"(src1Stride), "r"(dstStride) + :"memory");*/ +} + +static void DEF(put_no_rnd_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) +{ + __asm __volatile( + "pcmpeqb %%mm6, %%mm6\n\t" + "testl $1, %0 \n\t" + " jz 1f \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "movq (%2), %%mm2 \n\t" + "movq 8(%2), %%mm3 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "pxor %%mm6, %%mm2 \n\t" + "pxor %%mm6, %%mm3 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "add %4, %1 \n\t" + "add $16, %2 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "decl %0 \n\t" + "1: \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "add %4, %1 \n\t" + "movq (%2), %%mm2 \n\t" + "movq 8(%2), %%mm3 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "pxor %%mm6, %%mm2 \n\t" + "pxor %%mm6, %%mm3 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "movq %%mm0, (%3) \n\t" + "movq %%mm1, 8(%3) \n\t" + "add %5, %3 \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "add %4, %1 \n\t" + "movq 16(%2), %%mm2 \n\t" + "movq 24(%2), %%mm3 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" + "pxor %%mm6, %%mm2 \n\t" + "pxor %%mm6, %%mm3 \n\t" + PAVGB" %%mm2, %%mm0 \n\t" + PAVGB" %%mm3, %%mm1 \n\t" + "pxor %%mm6, %%mm0 \n\t" + "pxor %%mm6, %%mm1 \n\t" "movq %%mm0, (%3) \n\t" "movq %%mm1, 8(%3) \n\t" - "addl %5, %3 \n\t" - "addl $32, %2 \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" "subl $2, %0 \n\t" "jnz 1b \n\t" - :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) +#ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used + :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#else + :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) +#endif + :"S"((long)src1Stride), "D"((long)dstStride) + :"memory"); +//the following should be used, though better not with gcc ... +/* :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst) :"r"(src1Stride), "r"(dstStride) - :"memory"); + :"memory");*/ } /* GL: this function does incorrect rounding if overflow */ @@ -158,13 +551,13 @@ static void DEF(put_no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, in { MOVQ_BONE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm2 \n\t" "movq 1(%1), %%mm1 \n\t" "movq 1(%1, %3), %%mm3 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %1 \n\t" "psubusb %%mm6, %%mm0 \n\t" "psubusb %%mm6, %%mm2 \n\t" PAVGB" %%mm1, %%mm0 \n\t" @@ -175,50 +568,50 @@ static void DEF(put_no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, in "movq 1(%1), %%mm1 \n\t" "movq (%1, %3), %%mm2 \n\t" "movq 1(%1, %3), %%mm3 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" "psubusb %%mm6, %%mm0 \n\t" "psubusb %%mm6, %%mm2 \n\t" PAVGB" %%mm1, %%mm0 \n\t" PAVGB" %%mm3, %%mm2 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm2, (%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } static void DEF(put_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" - "subl %3, %2 \n\t" + "sub %3, %2 \n\t" "1: \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm2 \n\t" - "addl %%eax, %1 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" %%mm1, %%mm0 \n\t" PAVGB" %%mm2, %%mm1 \n\t" "movq %%mm0, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" %%mm1, %%mm2 \n\t" PAVGB" %%mm0, %%mm1 \n\t" "movq %%mm2, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" - "addl %%eax, %2 \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D" (block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } /* GL: this function does incorrect rounding if overflow */ @@ -226,39 +619,39 @@ static void DEF(put_no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, in { MOVQ_BONE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" - "subl %3, %2 \n\t" + "sub %3, %2 \n\t" "1: \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm2 \n\t" - "addl %%eax, %1 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" + "add %%"REG_a", %1 \n\t" "psubusb %%mm6, %%mm1 \n\t" PAVGB" %%mm1, %%mm0 \n\t" PAVGB" %%mm2, %%mm1 \n\t" "movq %%mm0, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" "psubusb %%mm6, %%mm1 \n\t" PAVGB" %%mm1, %%mm2 \n\t" PAVGB" %%mm0, %%mm1 \n\t" "movq %%mm2, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" - "addl %%eax, %2 \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D" (block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } static void DEF(avg_pixels8)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "1: \n\t" "movq (%2), %%mm0 \n\t" "movq (%2, %3), %%mm1 \n\t" @@ -266,27 +659,27 @@ static void DEF(avg_pixels8)(uint8_t *block, const uint8_t *pixels, int line_siz PAVGB" (%1, %3), %%mm1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%2), %%mm0 \n\t" "movq (%2, %3), %%mm1 \n\t" PAVGB" (%1), %%mm0 \n\t" PAVGB" (%1, %3), %%mm1 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } static void DEF(avg_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm2 \n\t" @@ -294,63 +687,63 @@ static void DEF(avg_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_ PAVGB" 1(%1, %3), %%mm2 \n\t" PAVGB" (%2), %%mm0 \n\t" PAVGB" (%2, %3), %%mm2 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %1 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm2, (%2, %3) \n\t" "movq (%1), %%mm0 \n\t" "movq (%1, %3), %%mm2 \n\t" PAVGB" 1(%1), %%mm0 \n\t" PAVGB" 1(%1, %3), %%mm2 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" (%2), %%mm0 \n\t" PAVGB" (%2, %3), %%mm2 \n\t" "movq %%mm0, (%2) \n\t" "movq %%mm2, (%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } static void DEF(avg_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" - "subl %3, %2 \n\t" + "sub %3, %2 \n\t" "1: \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm2 \n\t" - "addl %%eax, %1 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" %%mm1, %%mm0 \n\t" PAVGB" %%mm2, %%mm1 \n\t" "movq (%2, %3), %%mm3 \n\t" - "movq (%2, %%eax), %%mm4 \n\t" + "movq (%2, %%"REG_a"), %%mm4 \n\t" PAVGB" %%mm3, %%mm0 \n\t" PAVGB" %%mm4, %%mm1 \n\t" "movq %%mm0, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm0 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" PAVGB" %%mm1, %%mm2 \n\t" PAVGB" %%mm0, %%mm1 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" "movq (%2, %3), %%mm3 \n\t" - "movq (%2, %%eax), %%mm4 \n\t" + "movq (%2, %%"REG_a"), %%mm4 \n\t" PAVGB" %%mm3, %%mm2 \n\t" PAVGB" %%mm4, %%mm1 \n\t" "movq %%mm2, (%2, %3) \n\t" - "movq %%mm1, (%2, %%eax) \n\t" - "addl %%eax, %2 \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } // Note this is not correctly rounded, but this function is only used for b frames so it doesnt matter @@ -358,17 +751,17 @@ static void DEF(avg_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line { MOVQ_BONE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" PAVGB" 1(%1), %%mm0 \n\t" ".balign 8 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" "movq (%1, %3), %%mm1 \n\t" "psubusb %%mm6, %%mm2 \n\t" PAVGB" 1(%1, %3), %%mm1 \n\t" - PAVGB" 1(%1, %%eax), %%mm2 \n\t" - "addl %%eax, %1 \n\t" + PAVGB" 1(%1, %%"REG_a"), %%mm2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" %%mm1, %%mm0 \n\t" PAVGB" %%mm2, %%mm1 \n\t" PAVGB" (%2), %%mm0 \n\t" @@ -376,23 +769,23 @@ static void DEF(avg_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm0 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" PAVGB" 1(%1, %3), %%mm1 \n\t" - PAVGB" 1(%1, %%eax), %%mm0 \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" + PAVGB" 1(%1, %%"REG_a"), %%mm0 \n\t" + "add %%"REG_a", %2 \n\t" + "add %%"REG_a", %1 \n\t" PAVGB" %%mm1, %%mm2 \n\t" PAVGB" %%mm0, %%mm1 \n\t" PAVGB" (%2), %%mm2 \n\t" PAVGB" (%2, %3), %%mm1 \n\t" "movq %%mm2, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r" (line_size) - :"%eax", "memory"); + :"r" ((long)line_size) + :"%"REG_a, "memory"); } //FIXME the following could be optimized too ... diff --git a/src/libffmpeg/libavcodec/i386/dsputil_mmx_rnd.h b/src/libffmpeg/libavcodec/i386/dsputil_mmx_rnd.h index 1b79aa56a..20ea1b59e 100644 --- a/src/libffmpeg/libavcodec/i386/dsputil_mmx_rnd.h +++ b/src/libffmpeg/libavcodec/i386/dsputil_mmx_rnd.h @@ -27,7 +27,7 @@ static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line { MOVQ_BFE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" @@ -37,8 +37,8 @@ static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%2) \n\t" "movq %%mm5, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq 1(%1), %%mm1 \n\t" "movq (%1, %3), %%mm2 \n\t" @@ -46,13 +46,13 @@ static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%2) \n\t" "movq %%mm5, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r"(line_size) - :"eax", "memory"); + :"r"((long)line_size) + :REG_a, "memory"); } static void DEF(put, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) @@ -63,37 +63,37 @@ static void DEF(put, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int " jz 1f \n\t" "movq (%1), %%mm0 \n\t" "movq (%2), %%mm1 \n\t" - "addl %4, %1 \n\t" - "addl $8, %2 \n\t" + "add %4, %1 \n\t" + "add $8, %2 \n\t" PAVGB(%%mm0, %%mm1, %%mm4, %%mm6) "movq %%mm4, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "decl %0 \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" "movq (%2), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" "movq (%1), %%mm2 \n\t" "movq 8(%2), %%mm3 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq %%mm5, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq (%1), %%mm0 \n\t" "movq 16(%2), %%mm1 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" "movq (%1), %%mm2 \n\t" "movq 24(%2), %%mm3 \n\t" - "addl %4, %1 \n\t" - "addl $32, %2 \n\t" + "add %4, %1 \n\t" + "add $32, %2 \n\t" PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq %%mm5, (%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used @@ -101,7 +101,7 @@ static void DEF(put, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int #else :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) #endif - :"S"(src1Stride), "D"(dstStride) + :"S"((long)src1Stride), "D"((long)dstStride) :"memory"); } @@ -109,7 +109,7 @@ static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, int lin { MOVQ_BFE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1), %%mm0 \n\t" @@ -126,8 +126,8 @@ static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, int lin PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, 8(%2) \n\t" "movq %%mm5, 8(%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1), %%mm0 \n\t" "movq 1(%1), %%mm1 \n\t" "movq (%1, %3), %%mm2 \n\t" @@ -142,13 +142,13 @@ static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, int lin PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, 8(%2) \n\t" "movq %%mm5, 8(%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r"(line_size) - :"eax", "memory"); + :"r"((long)line_size) + :REG_a, "memory"); } static void DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h) @@ -161,12 +161,12 @@ static void DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, in "movq (%2), %%mm1 \n\t" "movq 8(%1), %%mm2 \n\t" "movq 8(%2), %%mm3 \n\t" - "addl %4, %1 \n\t" - "addl $16, %2 \n\t" + "add %4, %1 \n\t" + "add $16, %2 \n\t" PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%3) \n\t" "movq %%mm5, 8(%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "decl %0 \n\t" ".balign 8 \n\t" "1: \n\t" @@ -174,21 +174,21 @@ static void DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, in "movq (%2), %%mm1 \n\t" "movq 8(%1), %%mm2 \n\t" "movq 8(%2), %%mm3 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%3) \n\t" "movq %%mm5, 8(%3) \n\t" - "addl %5, %3 \n\t" + "add %5, %3 \n\t" "movq (%1), %%mm0 \n\t" "movq 16(%2), %%mm1 \n\t" "movq 8(%1), %%mm2 \n\t" "movq 24(%2), %%mm3 \n\t" - "addl %4, %1 \n\t" + "add %4, %1 \n\t" PAVGBP(%%mm0, %%mm1, %%mm4, %%mm2, %%mm3, %%mm5) "movq %%mm4, (%3) \n\t" "movq %%mm5, 8(%3) \n\t" - "addl %5, %3 \n\t" - "addl $32, %2 \n\t" + "add %5, %3 \n\t" + "add $32, %2 \n\t" "subl $2, %0 \n\t" "jnz 1b \n\t" #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used @@ -196,7 +196,7 @@ static void DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, in #else :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst) #endif - :"S"(src1Stride), "D"(dstStride) + :"S"((long)src1Stride), "D"((long)dstStride) :"memory"); } @@ -204,29 +204,29 @@ static void DEF(put, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line { MOVQ_BFE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax),%%mm2 \n\t" + "movq (%1, %%"REG_a"),%%mm2 \n\t" PAVGBP(%%mm1, %%mm0, %%mm4, %%mm2, %%mm1, %%mm5) "movq %%mm4, (%2) \n\t" "movq %%mm5, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax),%%mm0 \n\t" + "movq (%1, %%"REG_a"),%%mm0 \n\t" PAVGBP(%%mm1, %%mm2, %%mm4, %%mm0, %%mm1, %%mm5) "movq %%mm4, (%2) \n\t" "movq %%mm5, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r"(line_size) - :"eax", "memory"); + :"r"((long)line_size) + :REG_a, "memory"); } static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h) @@ -244,12 +244,12 @@ static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "punpckhbw %%mm7, %%mm5 \n\t" "paddusw %%mm0, %%mm4 \n\t" "paddusw %%mm1, %%mm5 \n\t" - "xorl %%eax, %%eax \n\t" - "addl %3, %1 \n\t" + "xor %%"REG_a", %%"REG_a" \n\t" + "add %3, %1 \n\t" ".balign 8 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq 1(%1, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq 1(%1, %%"REG_a"), %%mm2 \n\t" "movq %%mm0, %%mm1 \n\t" "movq %%mm2, %%mm3 \n\t" "punpcklbw %%mm7, %%mm0 \n\t" @@ -265,11 +265,11 @@ static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "psrlw $2, %%mm4 \n\t" "psrlw $2, %%mm5 \n\t" "packuswb %%mm5, %%mm4 \n\t" - "movq %%mm4, (%2, %%eax) \n\t" - "addl %3, %%eax \n\t" + "movq %%mm4, (%2, %%"REG_a") \n\t" + "add %3, %%"REG_a" \n\t" - "movq (%1, %%eax), %%mm2 \n\t" // 0 <-> 2 1 <-> 3 - "movq 1(%1, %%eax), %%mm4 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" // 0 <-> 2 1 <-> 3 + "movq 1(%1, %%"REG_a"), %%mm4 \n\t" "movq %%mm2, %%mm3 \n\t" "movq %%mm4, %%mm5 \n\t" "punpcklbw %%mm7, %%mm2 \n\t" @@ -285,17 +285,36 @@ static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "psrlw $2, %%mm0 \n\t" "psrlw $2, %%mm1 \n\t" "packuswb %%mm1, %%mm0 \n\t" - "movq %%mm0, (%2, %%eax) \n\t" - "addl %3, %%eax \n\t" + "movq %%mm0, (%2, %%"REG_a") \n\t" + "add %3, %%"REG_a" \n\t" "subl $2, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels) - :"D"(block), "r"(line_size) - :"eax", "memory"); + :"D"(block), "r"((long)line_size) + :REG_a, "memory"); } // avg_pixels +static void DEF(avg, pixels4)(uint8_t *block, const uint8_t *pixels, int line_size, int h) +{ + MOVQ_BFE(mm6); + JUMPALIGN(); + do { + __asm __volatile( + "movd %0, %%mm0 \n\t" + "movd %1, %%mm1 \n\t" + PAVGB(%%mm0, %%mm1, %%mm2, %%mm6) + "movd %%mm2, %0 \n\t" + :"+m"(*block) + :"m"(*pixels) + :"memory"); + pixels += line_size; + block += line_size; + } + while (--h); +} + // in case more speed is needed - unroling would certainly help static void DEF(avg, pixels8)(uint8_t *block, const uint8_t *pixels, int line_size, int h) { @@ -437,12 +456,12 @@ static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line { MOVQ_BFE(mm6); __asm __volatile( - "lea (%3, %3), %%eax \n\t" + "lea (%3, %3), %%"REG_a" \n\t" "movq (%1), %%mm0 \n\t" ".balign 8 \n\t" "1: \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" PAVGBP(%%mm1, %%mm0, %%mm4, %%mm2, %%mm1, %%mm5) "movq (%2), %%mm3 \n\t" PAVGB(%%mm3, %%mm4, %%mm0, %%mm6) @@ -450,11 +469,11 @@ static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line PAVGB(%%mm3, %%mm5, %%mm1, %%mm6) "movq %%mm0, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "movq (%1, %3), %%mm1 \n\t" - "movq (%1, %%eax), %%mm0 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" PAVGBP(%%mm1, %%mm2, %%mm4, %%mm0, %%mm1, %%mm5) "movq (%2), %%mm3 \n\t" PAVGB(%%mm3, %%mm4, %%mm2, %%mm6) @@ -462,14 +481,14 @@ static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line PAVGB(%%mm3, %%mm5, %%mm1, %%mm6) "movq %%mm2, (%2) \n\t" "movq %%mm1, (%2, %3) \n\t" - "addl %%eax, %1 \n\t" - "addl %%eax, %2 \n\t" + "add %%"REG_a", %1 \n\t" + "add %%"REG_a", %2 \n\t" "subl $4, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels), "+D"(block) - :"r"(line_size) - :"eax", "memory"); + :"r"((long)line_size) + :REG_a, "memory"); } // this routine is 'slightly' suboptimal but mostly unused @@ -488,12 +507,12 @@ static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "punpckhbw %%mm7, %%mm5 \n\t" "paddusw %%mm0, %%mm4 \n\t" "paddusw %%mm1, %%mm5 \n\t" - "xorl %%eax, %%eax \n\t" - "addl %3, %1 \n\t" + "xor %%"REG_a", %%"REG_a" \n\t" + "add %3, %1 \n\t" ".balign 8 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq 1(%1, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq 1(%1, %%"REG_a"), %%mm2 \n\t" "movq %%mm0, %%mm1 \n\t" "movq %%mm2, %%mm3 \n\t" "punpcklbw %%mm7, %%mm0 \n\t" @@ -508,16 +527,16 @@ static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "paddusw %%mm1, %%mm5 \n\t" "psrlw $2, %%mm4 \n\t" "psrlw $2, %%mm5 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" "packuswb %%mm5, %%mm4 \n\t" "pcmpeqd %%mm2, %%mm2 \n\t" "paddb %%mm2, %%mm2 \n\t" PAVGB(%%mm3, %%mm4, %%mm5, %%mm2) - "movq %%mm5, (%2, %%eax) \n\t" - "addl %3, %%eax \n\t" + "movq %%mm5, (%2, %%"REG_a") \n\t" + "add %3, %%"REG_a" \n\t" - "movq (%1, %%eax), %%mm2 \n\t" // 0 <-> 2 1 <-> 3 - "movq 1(%1, %%eax), %%mm4 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" // 0 <-> 2 1 <-> 3 + "movq 1(%1, %%"REG_a"), %%mm4 \n\t" "movq %%mm2, %%mm3 \n\t" "movq %%mm4, %%mm5 \n\t" "punpcklbw %%mm7, %%mm2 \n\t" @@ -532,19 +551,19 @@ static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int lin "paddusw %%mm5, %%mm1 \n\t" "psrlw $2, %%mm0 \n\t" "psrlw $2, %%mm1 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" "packuswb %%mm1, %%mm0 \n\t" "pcmpeqd %%mm2, %%mm2 \n\t" "paddb %%mm2, %%mm2 \n\t" PAVGB(%%mm3, %%mm0, %%mm1, %%mm2) - "movq %%mm1, (%2, %%eax) \n\t" - "addl %3, %%eax \n\t" + "movq %%mm1, (%2, %%"REG_a") \n\t" + "add %3, %%"REG_a" \n\t" "subl $2, %0 \n\t" "jnz 1b \n\t" :"+g"(h), "+S"(pixels) - :"D"(block), "r"(line_size) - :"eax", "memory"); + :"D"(block), "r"((long)line_size) + :REG_a, "memory"); } //FIXME optimize diff --git a/src/libffmpeg/libavcodec/i386/fdct_mmx.c b/src/libffmpeg/libavcodec/i386/fdct_mmx.c index 7f348329a..aacbe5743 100644 --- a/src/libffmpeg/libavcodec/i386/fdct_mmx.c +++ b/src/libffmpeg/libavcodec/i386/fdct_mmx.c @@ -47,13 +47,13 @@ static const int16_t ocos_4_16[4] ATTR_ALIGN(8) = { 23170, 23170, 23170, 23170, //cos * (2<<15) + 0.5 }; -static const long long fdct_one_corr ATTR_ALIGN(8) = 0x0001000100010001LL; +static const int64_t fdct_one_corr ATTR_ALIGN(8) = 0x0001000100010001LL; -static const long fdct_r_row[2] ATTR_ALIGN(8) = {RND_FRW_ROW, RND_FRW_ROW }; +static const int32_t fdct_r_row[2] ATTR_ALIGN(8) = {RND_FRW_ROW, RND_FRW_ROW }; struct { - const long fdct_r_row_sse2[4] ATTR_ALIGN(16); + const int32_t fdct_r_row_sse2[4] ATTR_ALIGN(16); } fdct_r_row_sse2 ATTR_ALIGN(16)= {{ RND_FRW_ROW, RND_FRW_ROW, RND_FRW_ROW, RND_FRW_ROW diff --git a/src/libffmpeg/libavcodec/i386/mmx.h b/src/libffmpeg/libavcodec/i386/mmx.h index ad684bc5a..f0ef1b79e 100644 --- a/src/libffmpeg/libavcodec/i386/mmx.h +++ b/src/libffmpeg/libavcodec/i386/mmx.h @@ -5,6 +5,12 @@ #ifndef AVCODEC_I386MMX_H #define AVCODEC_I386MMX_H +#ifdef ARCH_X86_64 +# define REG_a "rax" +#else +# define REG_a "eax" +#endif + /* * The type of an value that fits in an MMX register (note that long * long constant values MUST be suffixed by LL and unsigned long long diff --git a/src/libffmpeg/libavcodec/i386/motion_est_mmx.c b/src/libffmpeg/libavcodec/i386/motion_est_mmx.c index 39246d905..1b90f8e40 100644 --- a/src/libffmpeg/libavcodec/i386/motion_est_mmx.c +++ b/src/libffmpeg/libavcodec/i386/motion_est_mmx.c @@ -20,6 +20,7 @@ * mostly by Michael Niedermayer */ #include "../dsputil.h" +#include "mmx.h" static const __attribute__ ((aligned(8))) uint64_t round_tab[3]={ 0x0000000000000000ULL, @@ -31,19 +32,19 @@ static attribute_used __attribute__ ((aligned(8))) uint64_t bone= 0x010101010101 static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) { - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm2 \n\t" - "movq (%2, %%eax), %%mm4 \n\t" - "addl %3, %%eax \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm2 \n\t" + "movq (%2, %%"REG_a"), %%mm4 \n\t" + "add %3, %%"REG_a" \n\t" "psubusb %%mm0, %%mm2 \n\t" "psubusb %%mm4, %%mm0 \n\t" - "movq (%1, %%eax), %%mm1 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" - "movq (%2, %%eax), %%mm5 \n\t" + "movq (%1, %%"REG_a"), %%mm1 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" + "movq (%2, %%"REG_a"), %%mm5 \n\t" "psubusb %%mm1, %%mm3 \n\t" "psubusb %%mm5, %%mm1 \n\t" "por %%mm2, %%mm0 \n\t" @@ -58,116 +59,116 @@ static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) "paddw %%mm3, %%mm2 \n\t" "paddw %%mm2, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %3, %%eax \n\t" + "add %3, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1 - len), "r" (blk2 - len), "r" (stride) + : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride) ); } static inline void sad8_1_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h) { - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm2 \n\t" "psadbw %%mm2, %%mm0 \n\t" - "addl %3, %%eax \n\t" - "movq (%1, %%eax), %%mm1 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" + "add %3, %%"REG_a" \n\t" + "movq (%1, %%"REG_a"), %%mm1 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" "psadbw %%mm1, %%mm3 \n\t" "paddw %%mm3, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %3, %%eax \n\t" + "add %3, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1 - len), "r" (blk2 - len), "r" (stride) + : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride) ); } static inline void sad8_2_mmx2(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h) { - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm2 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm2 \n\t" "pavgb %%mm2, %%mm0 \n\t" - "movq (%3, %%eax), %%mm2 \n\t" + "movq (%3, %%"REG_a"), %%mm2 \n\t" "psadbw %%mm2, %%mm0 \n\t" - "addl %4, %%eax \n\t" - "movq (%1, %%eax), %%mm1 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" + "add %4, %%"REG_a" \n\t" + "movq (%1, %%"REG_a"), %%mm1 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" "pavgb %%mm1, %%mm3 \n\t" - "movq (%3, %%eax), %%mm1 \n\t" + "movq (%3, %%"REG_a"), %%mm1 \n\t" "psadbw %%mm1, %%mm3 \n\t" "paddw %%mm3, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %4, %%eax \n\t" + "add %4, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" (stride) + : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride) ); } static inline void sad8_4_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h) { //FIXME reuse src - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "movq "MANGLE(bone)", %%mm5 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm2 \n\t" - "movq 1(%1, %%eax), %%mm1 \n\t" - "movq 1(%2, %%eax), %%mm3 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm2 \n\t" + "movq 1(%1, %%"REG_a"), %%mm1 \n\t" + "movq 1(%2, %%"REG_a"), %%mm3 \n\t" "pavgb %%mm2, %%mm0 \n\t" "pavgb %%mm1, %%mm3 \n\t" "psubusb %%mm5, %%mm3 \n\t" "pavgb %%mm3, %%mm0 \n\t" - "movq (%3, %%eax), %%mm2 \n\t" + "movq (%3, %%"REG_a"), %%mm2 \n\t" "psadbw %%mm2, %%mm0 \n\t" - "addl %4, %%eax \n\t" - "movq (%1, %%eax), %%mm1 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" - "movq 1(%1, %%eax), %%mm2 \n\t" - "movq 1(%2, %%eax), %%mm4 \n\t" + "add %4, %%"REG_a" \n\t" + "movq (%1, %%"REG_a"), %%mm1 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" + "movq 1(%1, %%"REG_a"), %%mm2 \n\t" + "movq 1(%2, %%"REG_a"), %%mm4 \n\t" "pavgb %%mm3, %%mm1 \n\t" "pavgb %%mm4, %%mm2 \n\t" "psubusb %%mm5, %%mm2 \n\t" "pavgb %%mm1, %%mm2 \n\t" - "movq (%3, %%eax), %%mm1 \n\t" + "movq (%3, %%"REG_a"), %%mm1 \n\t" "psadbw %%mm1, %%mm2 \n\t" "paddw %%mm2, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %4, %%eax \n\t" + "add %4, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len), "r" (stride) + : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len), "r" ((long)stride) ); } static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h) { - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm1 \n\t" - "movq (%1, %%eax), %%mm2 \n\t" - "movq (%2, %%eax), %%mm3 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm2 \n\t" + "movq (%2, %%"REG_a"), %%mm3 \n\t" "punpcklbw %%mm7, %%mm0 \n\t" "punpcklbw %%mm7, %%mm1 \n\t" "punpckhbw %%mm7, %%mm2 \n\t" "punpckhbw %%mm7, %%mm3 \n\t" "paddw %%mm0, %%mm1 \n\t" "paddw %%mm2, %%mm3 \n\t" - "movq (%3, %%eax), %%mm4 \n\t" - "movq (%3, %%eax), %%mm2 \n\t" + "movq (%3, %%"REG_a"), %%mm4 \n\t" + "movq (%3, %%"REG_a"), %%mm2 \n\t" "paddw %%mm5, %%mm1 \n\t" "paddw %%mm5, %%mm3 \n\t" "psrlw $1, %%mm1 \n\t" @@ -181,21 +182,21 @@ static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int "punpckhbw %%mm7, %%mm1 \n\t" "paddw %%mm1, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %4, %%eax \n\t" + "add %4, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" (stride) + : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride) ); } static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) { - int len= -(stride*h); + long len= -(stride*h); asm volatile( ".balign 16 \n\t" "1: \n\t" - "movq (%1, %%eax), %%mm0 \n\t" - "movq (%2, %%eax), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm0 \n\t" + "movq (%2, %%"REG_a"), %%mm1 \n\t" "movq %%mm0, %%mm4 \n\t" "movq %%mm1, %%mm2 \n\t" "punpcklbw %%mm7, %%mm0 \n\t" @@ -204,8 +205,8 @@ static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) "punpckhbw %%mm7, %%mm2 \n\t" "paddw %%mm1, %%mm0 \n\t" "paddw %%mm2, %%mm4 \n\t" - "movq 1(%1, %%eax), %%mm2 \n\t" - "movq 1(%2, %%eax), %%mm3 \n\t" + "movq 1(%1, %%"REG_a"), %%mm2 \n\t" + "movq 1(%2, %%"REG_a"), %%mm3 \n\t" "movq %%mm2, %%mm1 \n\t" "punpcklbw %%mm7, %%mm2 \n\t" "punpckhbw %%mm7, %%mm1 \n\t" @@ -216,8 +217,8 @@ static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) "punpckhbw %%mm7, %%mm4 \n\t" "paddw %%mm3, %%mm2 \n\t" "paddw %%mm4, %%mm1 \n\t" - "movq (%3, %%eax), %%mm3 \n\t" - "movq (%3, %%eax), %%mm4 \n\t" + "movq (%3, %%"REG_a"), %%mm3 \n\t" + "movq (%3, %%"REG_a"), %%mm4 \n\t" "paddw %%mm5, %%mm2 \n\t" "paddw %%mm5, %%mm1 \n\t" "psrlw $2, %%mm2 \n\t" @@ -231,10 +232,10 @@ static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h) "punpckhbw %%mm7, %%mm2 \n\t" "paddw %%mm2, %%mm0 \n\t" "paddw %%mm0, %%mm6 \n\t" - "addl %4, %%eax \n\t" + "add %4, %%"REG_a" \n\t" " js 1b \n\t" : "+a" (len) - : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" (stride) + : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" ((long)stride) ); } diff --git a/src/libffmpeg/libavcodec/i386/mpegvideo_mmx.c b/src/libffmpeg/libavcodec/i386/mpegvideo_mmx.c index f19de73d6..70c81f675 100644 --- a/src/libffmpeg/libavcodec/i386/mpegvideo_mmx.c +++ b/src/libffmpeg/libavcodec/i386/mpegvideo_mmx.c @@ -23,6 +23,7 @@ #include "../dsputil.h" #include "../mpegvideo.h" #include "../avcodec.h" +#include "mmx.h" extern uint8_t zigzag_direct_noperm[64]; extern uint16_t inv_zigzag_direct16[64]; @@ -34,7 +35,7 @@ static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x000 static void dct_unquantize_h263_intra_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int level, qmul, qadd, nCoeffs; + long level, qmul, qadd, nCoeffs; qmul = qscale << 1; @@ -97,7 +98,7 @@ asm volatile( "movq %%mm0, (%0, %3) \n\t" "movq %%mm1, 8(%0, %3) \n\t" - "addl $16, %3 \n\t" + "add $16, %3 \n\t" "jng 1b \n\t" ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(-nCoeffs)) : "memory" @@ -109,7 +110,7 @@ asm volatile( static void dct_unquantize_h263_inter_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int qmul, qadd, nCoeffs; + long qmul, qadd, nCoeffs; qmul = qscale << 1; qadd = (qscale - 1) | 1; @@ -160,7 +161,7 @@ asm volatile( "movq %%mm0, (%0, %3) \n\t" "movq %%mm1, 8(%0, %3) \n\t" - "addl $16, %3 \n\t" + "add $16, %3 \n\t" "jng 1b \n\t" ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(-nCoeffs)) : "memory" @@ -200,7 +201,7 @@ asm volatile( static void dct_unquantize_mpeg1_intra_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int nCoeffs; + long nCoeffs; const uint16_t *quant_matrix; int block0; @@ -220,13 +221,13 @@ asm volatile( "movd %2, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" - "movl %3, %%eax \n\t" + "mov %3, %%"REG_a" \n\t" ".balign 16\n\t" "1: \n\t" - "movq (%0, %%eax), %%mm0 \n\t" - "movq 8(%0, %%eax), %%mm1 \n\t" - "movq (%1, %%eax), %%mm4 \n\t" - "movq 8(%1, %%eax), %%mm5 \n\t" + "movq (%0, %%"REG_a"), %%mm0 \n\t" + "movq 8(%0, %%"REG_a"), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm4 \n\t" + "movq 8(%1, %%"REG_a"), %%mm5 \n\t" "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i] "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i] "pxor %%mm2, %%mm2 \n\t" @@ -241,8 +242,8 @@ asm volatile( "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q "pxor %%mm4, %%mm4 \n\t" "pxor %%mm5, %%mm5 \n\t" // FIXME slow - "pcmpeqw (%0, %%eax), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 - "pcmpeqw 8(%0, %%eax), %%mm5 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0 "psraw $3, %%mm0 \n\t" "psraw $3, %%mm1 \n\t" "psubw %%mm7, %%mm0 \n\t" @@ -255,13 +256,13 @@ asm volatile( "psubw %%mm3, %%mm1 \n\t" "pandn %%mm0, %%mm4 \n\t" "pandn %%mm1, %%mm5 \n\t" - "movq %%mm4, (%0, %%eax) \n\t" - "movq %%mm5, 8(%0, %%eax) \n\t" + "movq %%mm4, (%0, %%"REG_a") \n\t" + "movq %%mm5, 8(%0, %%"REG_a") \n\t" - "addl $16, %%eax \n\t" + "add $16, %%"REG_a" \n\t" "js 1b \n\t" ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs) - : "%eax", "memory" + : "%"REG_a, "memory" ); block[0]= block0; } @@ -269,7 +270,7 @@ asm volatile( static void dct_unquantize_mpeg1_inter_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int nCoeffs; + long nCoeffs; const uint16_t *quant_matrix; assert(s->block_last_index[n]>=0); @@ -283,13 +284,13 @@ asm volatile( "movd %2, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" - "movl %3, %%eax \n\t" + "mov %3, %%"REG_a" \n\t" ".balign 16\n\t" "1: \n\t" - "movq (%0, %%eax), %%mm0 \n\t" - "movq 8(%0, %%eax), %%mm1 \n\t" - "movq (%1, %%eax), %%mm4 \n\t" - "movq 8(%1, %%eax), %%mm5 \n\t" + "movq (%0, %%"REG_a"), %%mm0 \n\t" + "movq 8(%0, %%"REG_a"), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm4 \n\t" + "movq 8(%1, %%"REG_a"), %%mm5 \n\t" "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i] "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i] "pxor %%mm2, %%mm2 \n\t" @@ -308,8 +309,8 @@ asm volatile( "pmullw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q "pxor %%mm4, %%mm4 \n\t" "pxor %%mm5, %%mm5 \n\t" // FIXME slow - "pcmpeqw (%0, %%eax), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 - "pcmpeqw 8(%0, %%eax), %%mm5 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0 "psraw $4, %%mm0 \n\t" "psraw $4, %%mm1 \n\t" "psubw %%mm7, %%mm0 \n\t" @@ -322,20 +323,20 @@ asm volatile( "psubw %%mm3, %%mm1 \n\t" "pandn %%mm0, %%mm4 \n\t" "pandn %%mm1, %%mm5 \n\t" - "movq %%mm4, (%0, %%eax) \n\t" - "movq %%mm5, 8(%0, %%eax) \n\t" + "movq %%mm4, (%0, %%"REG_a") \n\t" + "movq %%mm5, 8(%0, %%"REG_a") \n\t" - "addl $16, %%eax \n\t" + "add $16, %%"REG_a" \n\t" "js 1b \n\t" ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs) - : "%eax", "memory" + : "%"REG_a, "memory" ); } static void dct_unquantize_mpeg2_intra_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int nCoeffs; + long nCoeffs; const uint16_t *quant_matrix; int block0; @@ -355,13 +356,13 @@ asm volatile( "movd %2, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" - "movl %3, %%eax \n\t" + "mov %3, %%"REG_a" \n\t" ".balign 16\n\t" "1: \n\t" - "movq (%0, %%eax), %%mm0 \n\t" - "movq 8(%0, %%eax), %%mm1 \n\t" - "movq (%1, %%eax), %%mm4 \n\t" - "movq 8(%1, %%eax), %%mm5 \n\t" + "movq (%0, %%"REG_a"), %%mm0 \n\t" + "movq 8(%0, %%"REG_a"), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm4 \n\t" + "movq 8(%1, %%"REG_a"), %%mm5 \n\t" "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i] "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i] "pxor %%mm2, %%mm2 \n\t" @@ -376,8 +377,8 @@ asm volatile( "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q "pxor %%mm4, %%mm4 \n\t" "pxor %%mm5, %%mm5 \n\t" // FIXME slow - "pcmpeqw (%0, %%eax), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 - "pcmpeqw 8(%0, %%eax), %%mm5 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0 "psraw $3, %%mm0 \n\t" "psraw $3, %%mm1 \n\t" "pxor %%mm2, %%mm0 \n\t" @@ -386,13 +387,13 @@ asm volatile( "psubw %%mm3, %%mm1 \n\t" "pandn %%mm0, %%mm4 \n\t" "pandn %%mm1, %%mm5 \n\t" - "movq %%mm4, (%0, %%eax) \n\t" - "movq %%mm5, 8(%0, %%eax) \n\t" + "movq %%mm4, (%0, %%"REG_a") \n\t" + "movq %%mm5, 8(%0, %%"REG_a") \n\t" - "addl $16, %%eax \n\t" + "add $16, %%"REG_a" \n\t" "jng 1b \n\t" ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs) - : "%eax", "memory" + : "%"REG_a, "memory" ); block[0]= block0; //Note, we dont do mismatch control for intra as errors cannot accumulate @@ -401,7 +402,7 @@ asm volatile( static void dct_unquantize_mpeg2_inter_mmx(MpegEncContext *s, DCTELEM *block, int n, int qscale) { - int nCoeffs; + long nCoeffs; const uint16_t *quant_matrix; assert(s->block_last_index[n]>=0); @@ -416,13 +417,13 @@ asm volatile( "movd %2, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" "packssdw %%mm6, %%mm6 \n\t" - "movl %3, %%eax \n\t" + "mov %3, %%"REG_a" \n\t" ".balign 16\n\t" "1: \n\t" - "movq (%0, %%eax), %%mm0 \n\t" - "movq 8(%0, %%eax), %%mm1 \n\t" - "movq (%1, %%eax), %%mm4 \n\t" - "movq 8(%1, %%eax), %%mm5 \n\t" + "movq (%0, %%"REG_a"), %%mm0 \n\t" + "movq 8(%0, %%"REG_a"), %%mm1 \n\t" + "movq (%1, %%"REG_a"), %%mm4 \n\t" + "movq 8(%1, %%"REG_a"), %%mm5 \n\t" "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i] "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i] "pxor %%mm2, %%mm2 \n\t" @@ -441,8 +442,8 @@ asm volatile( "paddw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q "pxor %%mm4, %%mm4 \n\t" "pxor %%mm5, %%mm5 \n\t" // FIXME slow - "pcmpeqw (%0, %%eax), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 - "pcmpeqw 8(%0, %%eax), %%mm5 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0 + "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0 "psrlw $4, %%mm0 \n\t" "psrlw $4, %%mm1 \n\t" "pxor %%mm2, %%mm0 \n\t" @@ -453,10 +454,10 @@ asm volatile( "pandn %%mm1, %%mm5 \n\t" "pxor %%mm4, %%mm7 \n\t" "pxor %%mm5, %%mm7 \n\t" - "movq %%mm4, (%0, %%eax) \n\t" - "movq %%mm5, 8(%0, %%eax) \n\t" + "movq %%mm4, (%0, %%"REG_a") \n\t" + "movq %%mm5, 8(%0, %%"REG_a") \n\t" - "addl $16, %%eax \n\t" + "add $16, %%"REG_a" \n\t" "jng 1b \n\t" "movd 124(%0, %3), %%mm0 \n\t" "movq %%mm7, %%mm6 \n\t" @@ -471,7 +472,7 @@ asm volatile( "movd %%mm0, 124(%0, %3) \n\t" ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (-2*nCoeffs) - : "%eax", "memory" + : "%"REG_a, "memory" ); } @@ -499,11 +500,11 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) "punpckhwd %%mm1, %%mm1 \n\t" "punpckhdq %%mm1, %%mm1 \n\t" "movq %%mm1, (%0, %2) \n\t" - "addl %1, %0 \n\t" - "cmpl %3, %0 \n\t" + "add %1, %0 \n\t" + "cmp %3, %0 \n\t" " jb 1b \n\t" : "+r" (ptr) - : "r" (wrap), "r" (width), "r" (ptr + wrap*height) + : "r" ((long)wrap), "r" ((long)width), "r" (ptr + wrap*height) ); } else @@ -522,11 +523,11 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) "punpckhdq %%mm1, %%mm1 \n\t" "movq %%mm1, (%0, %2) \n\t" "movq %%mm1, 8(%0, %2) \n\t" - "addl %1, %0 \n\t" - "cmpl %3, %0 \n\t" + "add %1, %0 \n\t" + "cmp %3, %0 \n\t" " jb 1b \n\t" : "+r" (ptr) - : "r" (wrap), "r" (width), "r" (ptr + wrap*height) + : "r" ((long)wrap), "r" ((long)width), "r" (ptr + wrap*height) ); } @@ -540,11 +541,11 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) "movq %%mm0, (%0, %2) \n\t" "movq %%mm0, (%0, %2, 2) \n\t" "movq %%mm0, (%0, %3) \n\t" - "addl $8, %0 \n\t" - "cmpl %4, %0 \n\t" + "add $8, %0 \n\t" + "cmp %4, %0 \n\t" " jb 1b \n\t" : "+r" (ptr) - : "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w) + : "r" ((long)buf - (long)ptr - w), "r" ((long)-wrap), "r" ((long)-wrap*3), "r" (ptr+width+2*w) ); ptr= last_line + (i + 1) * wrap - w; asm volatile( @@ -554,11 +555,11 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) "movq %%mm0, (%0, %2) \n\t" "movq %%mm0, (%0, %2, 2) \n\t" "movq %%mm0, (%0, %3) \n\t" - "addl $8, %0 \n\t" - "cmpl %4, %0 \n\t" + "add $8, %0 \n\t" + "cmp %4, %0 \n\t" " jb 1b \n\t" : "+r" (ptr) - : "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w) + : "r" ((long)last_line - (long)ptr - w), "r" ((long)wrap), "r" ((long)wrap*3), "r" (ptr+width+2*w) ); } } @@ -607,10 +608,10 @@ static void denoise_dct_mmx(MpegEncContext *s, DCTELEM *block){ "movq %%mm2, 8(%1) \n\t" "movq %%mm5, 16(%1) \n\t" "movq %%mm3, 24(%1) \n\t" - "addl $16, %0 \n\t" - "addl $32, %1 \n\t" - "addl $16, %2 \n\t" - "cmpl %3, %0 \n\t" + "add $16, %0 \n\t" + "add $32, %1 \n\t" + "add $16, %2 \n\t" + "cmp %3, %0 \n\t" " jb 1b \n\t" : "+r" (block), "+r" (sum), "+r" (offset) : "r"(block+64) @@ -661,10 +662,10 @@ static void denoise_dct_sse2(MpegEncContext *s, DCTELEM *block){ "movdqa %%xmm6, 16(%1) \n\t" "movdqa %%xmm5, 32(%1) \n\t" "movdqa %%xmm0, 48(%1) \n\t" - "addl $32, %0 \n\t" - "addl $64, %1 \n\t" - "addl $32, %2 \n\t" - "cmpl %3, %0 \n\t" + "add $32, %0 \n\t" + "add $64, %1 \n\t" + "add $32, %2 \n\t" + "cmp %3, %0 \n\t" " jb 1b \n\t" : "+r" (block), "+r" (sum), "+r" (offset) : "r"(block+64) diff --git a/src/libffmpeg/libavcodec/i386/mpegvideo_mmx_template.c b/src/libffmpeg/libavcodec/i386/mpegvideo_mmx_template.c index d4ed61ecb..c9354dc1b 100644 --- a/src/libffmpeg/libavcodec/i386/mpegvideo_mmx_template.c +++ b/src/libffmpeg/libavcodec/i386/mpegvideo_mmx_template.c @@ -36,7 +36,8 @@ static int RENAME(dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow) { - int level=0, last_non_zero_p1, q; //=0 is cuz gcc says uninitalized ... + long last_non_zero_p1; + int level=0, q; //=0 is cuz gcc says uninitalized ... const uint16_t *qmat, *bias; __align8 int16_t temp_block[64]; @@ -58,7 +59,7 @@ static int RENAME(dct_quantize)(MpegEncContext *s, if (!s->h263_aic) { #if 1 asm volatile ( - "imul %%ecx \n\t" + "mul %%ecx \n\t" : "=d" (level), "=a"(dummy) : "a" ((block[0]>>2) + q), "c" (inverse[q<<1]) ); @@ -87,21 +88,21 @@ static int RENAME(dct_quantize)(MpegEncContext *s, qmat = s->q_inter_matrix16[qscale][0]; } - if(s->out_format == FMT_H263 && s->mpeg_quant==0){ + if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){ asm volatile( - "movd %%eax, %%mm3 \n\t" // last_non_zero_p1 + "movd %%"REG_a", %%mm3 \n\t" // last_non_zero_p1 SPREADW(%%mm3) "pxor %%mm7, %%mm7 \n\t" // 0 "pxor %%mm4, %%mm4 \n\t" // 0 "movq (%2), %%mm5 \n\t" // qmat[0] "pxor %%mm6, %%mm6 \n\t" "psubw (%3), %%mm6 \n\t" // -bias[0] - "movl $-128, %%eax \n\t" + "mov $-128, %%"REG_a" \n\t" ".balign 16 \n\t" "1: \n\t" "pxor %%mm1, %%mm1 \n\t" // 0 - "movq (%1, %%eax), %%mm0 \n\t" // block[i] + "movq (%1, %%"REG_a"), %%mm0 \n\t" // block[i] "pcmpgtw %%mm0, %%mm1 \n\t" // block[i] <= 0 ? 0xFF : 0x00 "pxor %%mm1, %%mm0 \n\t" "psubw %%mm1, %%mm0 \n\t" // ABS(block[i]) @@ -110,13 +111,13 @@ static int RENAME(dct_quantize)(MpegEncContext *s, "por %%mm0, %%mm4 \n\t" "pxor %%mm1, %%mm0 \n\t" "psubw %%mm1, %%mm0 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i]) - "movq %%mm0, (%5, %%eax) \n\t" + "movq %%mm0, (%5, %%"REG_a") \n\t" "pcmpeqw %%mm7, %%mm0 \n\t" // out==0 ? 0xFF : 0x00 - "movq (%4, %%eax), %%mm1 \n\t" - "movq %%mm7, (%1, %%eax) \n\t" // 0 + "movq (%4, %%"REG_a"), %%mm1 \n\t" + "movq %%mm7, (%1, %%"REG_a") \n\t" // 0 "pandn %%mm1, %%mm0 \n\t" PMAXW(%%mm0, %%mm3) - "addl $8, %%eax \n\t" + "add $8, %%"REG_a" \n\t" " js 1b \n\t" "movq %%mm3, %%mm0 \n\t" "psrlq $32, %%mm3 \n\t" @@ -124,8 +125,8 @@ static int RENAME(dct_quantize)(MpegEncContext *s, "movq %%mm3, %%mm0 \n\t" "psrlq $16, %%mm3 \n\t" PMAXW(%%mm0, %%mm3) - "movd %%mm3, %%eax \n\t" - "movzbl %%al, %%eax \n\t" // last_non_zero_p1 + "movd %%mm3, %%"REG_a" \n\t" + "movzb %%al, %%"REG_a" \n\t" // last_non_zero_p1 : "+a" (last_non_zero_p1) : "r" (block+64), "r" (qmat), "r" (bias), "r" (inv_zigzag_direct16+64), "r" (temp_block+64) @@ -142,32 +143,32 @@ static int RENAME(dct_quantize)(MpegEncContext *s, ); }else{ // FMT_H263 asm volatile( - "movd %%eax, %%mm3 \n\t" // last_non_zero_p1 + "movd %%"REG_a", %%mm3 \n\t" // last_non_zero_p1 SPREADW(%%mm3) "pxor %%mm7, %%mm7 \n\t" // 0 "pxor %%mm4, %%mm4 \n\t" // 0 - "movl $-128, %%eax \n\t" + "mov $-128, %%"REG_a" \n\t" ".balign 16 \n\t" "1: \n\t" "pxor %%mm1, %%mm1 \n\t" // 0 - "movq (%1, %%eax), %%mm0 \n\t" // block[i] + "movq (%1, %%"REG_a"), %%mm0 \n\t" // block[i] "pcmpgtw %%mm0, %%mm1 \n\t" // block[i] <= 0 ? 0xFF : 0x00 "pxor %%mm1, %%mm0 \n\t" "psubw %%mm1, %%mm0 \n\t" // ABS(block[i]) - "movq (%3, %%eax), %%mm6 \n\t" // bias[0] + "movq (%3, %%"REG_a"), %%mm6 \n\t" // bias[0] "paddusw %%mm6, %%mm0 \n\t" // ABS(block[i]) + bias[0] - "movq (%2, %%eax), %%mm5 \n\t" // qmat[i] + "movq (%2, %%"REG_a"), %%mm5 \n\t" // qmat[i] "pmulhw %%mm5, %%mm0 \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16 "por %%mm0, %%mm4 \n\t" "pxor %%mm1, %%mm0 \n\t" "psubw %%mm1, %%mm0 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i]) - "movq %%mm0, (%5, %%eax) \n\t" + "movq %%mm0, (%5, %%"REG_a") \n\t" "pcmpeqw %%mm7, %%mm0 \n\t" // out==0 ? 0xFF : 0x00 - "movq (%4, %%eax), %%mm1 \n\t" - "movq %%mm7, (%1, %%eax) \n\t" // 0 + "movq (%4, %%"REG_a"), %%mm1 \n\t" + "movq %%mm7, (%1, %%"REG_a") \n\t" // 0 "pandn %%mm1, %%mm0 \n\t" PMAXW(%%mm0, %%mm3) - "addl $8, %%eax \n\t" + "add $8, %%"REG_a" \n\t" " js 1b \n\t" "movq %%mm3, %%mm0 \n\t" "psrlq $32, %%mm3 \n\t" @@ -175,8 +176,8 @@ static int RENAME(dct_quantize)(MpegEncContext *s, "movq %%mm3, %%mm0 \n\t" "psrlq $16, %%mm3 \n\t" PMAXW(%%mm0, %%mm3) - "movd %%mm3, %%eax \n\t" - "movzbl %%al, %%eax \n\t" // last_non_zero_p1 + "movd %%mm3, %%"REG_a" \n\t" + "movzb %%al, %%"REG_a" \n\t" // last_non_zero_p1 : "+a" (last_non_zero_p1) : "r" (block+64), "r" (qmat+64), "r" (bias+64), "r" (inv_zigzag_direct16+64), "r" (temp_block+64) diff --git a/src/libffmpeg/libavcodec/idcinvideo.c b/src/libffmpeg/libavcodec/idcinvideo.c index e53246bd1..f5df5a49d 100644 --- a/src/libffmpeg/libavcodec/idcinvideo.c +++ b/src/libffmpeg/libavcodec/idcinvideo.c @@ -192,7 +192,7 @@ static void idcin_decode_vlcs(IdcinContext *s) while(node_num >= HUF_TOKENS) { if(!bit_pos) { - if(dat_pos > s->size) { + if(dat_pos >= s->size) { av_log(s->avctx, AV_LOG_ERROR, "Huffman decode error.\n"); return; } diff --git a/src/libffmpeg/libavcodec/imgconvert.c b/src/libffmpeg/libavcodec/imgconvert.c index b351d2219..1ba723a95 100644 --- a/src/libffmpeg/libavcodec/imgconvert.c +++ b/src/libffmpeg/libavcodec/imgconvert.c @@ -97,6 +97,14 @@ static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = { .depth = 8, .x_chroma_shift = 1, .y_chroma_shift = 0, }, + [PIX_FMT_UYVY422] = { + .name = "uyvy422", + .nb_channels = 1, + .color_type = FF_COLOR_YUV, + .pixel_type = FF_PIXEL_PACKED, + .depth = 8, + .x_chroma_shift = 1, .y_chroma_shift = 0, + }, [PIX_FMT_YUV410P] = { .name = "yuv410p", .nb_channels = 3, @@ -213,6 +221,20 @@ static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = { .pixel_type = FF_PIXEL_PALETTE, .depth = 8, }, + [PIX_FMT_XVMC_MPEG2_MC] = { + .name = "xvmcmc", + }, + [PIX_FMT_XVMC_MPEG2_IDCT] = { + .name = "xvmcidct", + }, + [PIX_FMT_UYVY411] = { + .name = "uyvy411", + .nb_channels = 1, + .color_type = FF_COLOR_YUV, + .pixel_type = FF_PIXEL_PACKED, + .depth = 8, + .x_chroma_shift = 2, .y_chroma_shift = 0, + }, }; void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift) @@ -246,6 +268,9 @@ int avpicture_fill(AVPicture *picture, uint8_t *ptr, int size, w2, h2, size2; PixFmtInfo *pinfo; + if(avcodec_check_dimensions(NULL, width, height)) + goto fail; + pinfo = &pix_fmt_info[pix_fmt]; size = width * height; switch(pix_fmt) { @@ -288,6 +313,18 @@ int avpicture_fill(AVPicture *picture, uint8_t *ptr, picture->data[2] = NULL; picture->linesize[0] = width * 2; return size * 2; + case PIX_FMT_UYVY422: + picture->data[0] = ptr; + picture->data[1] = NULL; + picture->data[2] = NULL; + picture->linesize[0] = width * 2; + return size * 2; + case PIX_FMT_UYVY411: + picture->data[0] = ptr; + picture->data[1] = NULL; + picture->data[2] = NULL; + picture->linesize[0] = width + width/2; + return size + size/2; case PIX_FMT_GRAY8: picture->data[0] = ptr; picture->data[1] = NULL; @@ -310,6 +347,7 @@ int avpicture_fill(AVPicture *picture, uint8_t *ptr, picture->linesize[1] = 4; return size2 + 256 * 4; default: +fail: picture->data[0] = NULL; picture->data[1] = NULL; picture->data[2] = NULL; @@ -326,13 +364,17 @@ int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height, const unsigned char* s; int size = avpicture_get_size(pix_fmt, width, height); - if (size > dest_size) + if (size > dest_size || size < 0) return -1; if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) { - if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 || - pix_fmt == PIX_FMT_RGB555) - w = width * 2; + if (pix_fmt == PIX_FMT_YUV422 || + pix_fmt == PIX_FMT_UYVY422 || + pix_fmt == PIX_FMT_RGB565 || + pix_fmt == PIX_FMT_RGB555) + w = width * 2; + else if (pix_fmt == PIX_FMT_UYVY411) + w = width + width/2; else if (pix_fmt == PIX_FMT_PAL8) w = width; else @@ -342,7 +384,7 @@ int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height, h = height; } else { data_planes = pf->nb_channels; - w = width; + w = (width*pf->depth + 7)/8; h = height; } @@ -439,10 +481,14 @@ static int avg_bits_per_pixel(int pix_fmt) case FF_PIXEL_PACKED: switch(pix_fmt) { case PIX_FMT_YUV422: + case PIX_FMT_UYVY422: case PIX_FMT_RGB565: case PIX_FMT_RGB555: bits = 16; break; + case PIX_FMT_UYVY411: + bits = 12; + break; default: bits = pf->depth * pf->nb_channels; break; @@ -551,10 +597,14 @@ void img_copy(AVPicture *dst, const AVPicture *src, case FF_PIXEL_PACKED: switch(pix_fmt) { case PIX_FMT_YUV422: + case PIX_FMT_UYVY422: case PIX_FMT_RGB565: case PIX_FMT_RGB555: bits = 16; break; + case PIX_FMT_UYVY411: + bits = 12; + break; default: bits = pf->depth * pf->nb_channels; break; @@ -649,6 +699,98 @@ static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src, } } +static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + const uint8_t *p, *p1; + uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1; + int w; + + p1 = src->data[0]; + + lum1 = dst->data[0]; + cb1 = dst->data[1]; + cr1 = dst->data[2]; + + for(;height >= 1; height -= 2) { + p = p1; + lum = lum1; + cb = cb1; + cr = cr1; + for(w = width; w >= 2; w -= 2) { + lum[0] = p[1]; + cb[0] = p[0]; + lum[1] = p[3]; + cr[0] = p[2]; + p += 4; + lum += 2; + cb++; + cr++; + } + if (w) { + lum[0] = p[1]; + cb[0] = p[0]; + cr[0] = p[2]; + cb++; + cr++; + } + p1 += src->linesize[0]; + lum1 += dst->linesize[0]; + if (height>1) { + p = p1; + lum = lum1; + for(w = width; w >= 2; w -= 2) { + lum[0] = p[1]; + lum[1] = p[3]; + p += 4; + lum += 2; + } + if (w) { + lum[0] = p[1]; + } + p1 += src->linesize[0]; + lum1 += dst->linesize[0]; + } + cb1 += dst->linesize[1]; + cr1 += dst->linesize[2]; + } +} + + +static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + const uint8_t *p, *p1; + uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1; + int w; + + p1 = src->data[0]; + lum1 = dst->data[0]; + cb1 = dst->data[1]; + cr1 = dst->data[2]; + for(;height > 0; height--) { + p = p1; + lum = lum1; + cb = cb1; + cr = cr1; + for(w = width; w >= 2; w -= 2) { + lum[0] = p[1]; + cb[0] = p[0]; + lum[1] = p[3]; + cr[0] = p[2]; + p += 4; + lum += 2; + cb++; + cr++; + } + p1 += src->linesize[0]; + lum1 += dst->linesize[0]; + cb1 += dst->linesize[1]; + cr1 += dst->linesize[2]; + } +} + + static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src, int width, int height) { @@ -715,6 +857,141 @@ static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src, } } +static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + uint8_t *p, *p1; + const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1; + int w; + + p1 = dst->data[0]; + lum1 = src->data[0]; + cb1 = src->data[1]; + cr1 = src->data[2]; + for(;height > 0; height--) { + p = p1; + lum = lum1; + cb = cb1; + cr = cr1; + for(w = width; w >= 2; w -= 2) { + p[1] = lum[0]; + p[0] = cb[0]; + p[3] = lum[1]; + p[2] = cr[0]; + p += 4; + lum += 2; + cb++; + cr++; + } + p1 += dst->linesize[0]; + lum1 += src->linesize[0]; + cb1 += src->linesize[1]; + cr1 += src->linesize[2]; + } +} + +static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + const uint8_t *p, *p1; + uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1; + int w; + + p1 = src->data[0]; + lum1 = dst->data[0]; + cb1 = dst->data[1]; + cr1 = dst->data[2]; + for(;height > 0; height--) { + p = p1; + lum = lum1; + cb = cb1; + cr = cr1; + for(w = width; w >= 4; w -= 4) { + cb[0] = p[0]; + lum[0] = p[1]; + lum[1] = p[2]; + cr[0] = p[3]; + lum[2] = p[4]; + lum[3] = p[5]; + p += 6; + lum += 4; + cb++; + cr++; + } + p1 += src->linesize[0]; + lum1 += dst->linesize[0]; + cb1 += dst->linesize[1]; + cr1 += dst->linesize[2]; + } +} + + +static void yuv420p_to_yuv422(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + int w, h; + uint8_t *line1, *line2, *linesrc = dst->data[0]; + uint8_t *lum1, *lum2, *lumsrc = src->data[0]; + uint8_t *cb1, *cb2 = src->data[1]; + uint8_t *cr1, *cr2 = src->data[2]; + + for(h = height / 2; h--;) { + line1 = linesrc; + line2 = linesrc + dst->linesize[0]; + + lum1 = lumsrc; + lum2 = lumsrc + src->linesize[0]; + + cb1 = cb2; + cr1 = cr2; + + for(w = width / 2; w--;) { + *line1++ = *lum1++; *line2++ = *lum2++; + *line1++ = *line2++ = *cb1++; + *line1++ = *lum1++; *line2++ = *lum2++; + *line1++ = *line2++ = *cr1++; + } + + linesrc += dst->linesize[0] * 2; + lumsrc += src->linesize[0] * 2; + cb2 += src->linesize[1]; + cr2 += src->linesize[2]; + } +} + +static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src, + int width, int height) +{ + int w, h; + uint8_t *line1, *line2, *linesrc = dst->data[0]; + uint8_t *lum1, *lum2, *lumsrc = src->data[0]; + uint8_t *cb1, *cb2 = src->data[1]; + uint8_t *cr1, *cr2 = src->data[2]; + + for(h = height / 2; h--;) { + line1 = linesrc; + line2 = linesrc + dst->linesize[0]; + + lum1 = lumsrc; + lum2 = lumsrc + src->linesize[0]; + + cb1 = cb2; + cr1 = cr2; + + for(w = width / 2; w--;) { + *line1++ = *line2++ = *cb1++; + *line1++ = *lum1++; *line2++ = *lum2++; + *line1++ = *line2++ = *cr1++; + *line1++ = *lum1++; *line2++ = *lum2++; + } + + linesrc += dst->linesize[0] * 2; + lumsrc += src->linesize[0] * 2; + cb2 += src->linesize[1]; + cr2 += src->linesize[2]; + } +} + #define SCALEBITS 10 #define ONE_HALF (1 << (SCALEBITS - 1)) #define FIX(x) ((int) ((x) * (1<convert) { - /* specific convertion routine */ + /* specific conversion routine */ ce->convert(dst, src, dst_width, dst_height); return 0; } @@ -1838,6 +2139,14 @@ int img_convert(AVPicture *dst, int dst_pix_fmt, dst_pix_fmt == PIX_FMT_YUV422) { /* specific case: convert to YUV422P first */ int_pix_fmt = PIX_FMT_YUV422P; + } else if (src_pix_fmt == PIX_FMT_UYVY422 || + dst_pix_fmt == PIX_FMT_UYVY422) { + /* specific case: convert to YUV422P first */ + int_pix_fmt = PIX_FMT_YUV422P; + } else if (src_pix_fmt == PIX_FMT_UYVY411 || + dst_pix_fmt == PIX_FMT_UYVY411) { + /* specific case: convert to YUV411P first */ + int_pix_fmt = PIX_FMT_YUV411P; } else if ((src_pix->color_type == FF_COLOR_GRAY && src_pix_fmt != PIX_FMT_GRAY8) || (dst_pix->color_type == FF_COLOR_GRAY && diff --git a/src/libffmpeg/libavcodec/imgresample.c b/src/libffmpeg/libavcodec/imgresample.c index 14fdb1059..2c7e1120a 100644 --- a/src/libffmpeg/libavcodec/imgresample.c +++ b/src/libffmpeg/libavcodec/imgresample.c @@ -55,6 +55,8 @@ struct ImgReSampleContext { uint8_t *line_buf; }; +void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type); + static inline int get_phase(int pos) { return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1); @@ -540,39 +542,6 @@ static void component_resample(ImgReSampleContext *s, } } -/* XXX: the following filter is quite naive, but it seems to suffice - for 4 taps */ -static void build_filter(int16_t *filter, float factor) -{ - int ph, i, v; - float x, y, tab[NB_TAPS], norm, mult; - - /* if upsampling, only need to interpolate, no filter */ - if (factor > 1.0) - factor = 1.0; - - for(ph=0;ph= UINT_MAX / (LINE_BUF_HEIGHT + NB_TAPS)) + return NULL; s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS)); if (!s->line_buf) goto fail; @@ -617,10 +588,10 @@ ImgReSampleContext *img_resample_full_init(int owidth, int oheight, s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / s->pad_owidth; s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / s->pad_oheight; - build_filter(&s->h_filters[0][0], (float) s->pad_owidth / - (float) (iwidth - leftBand - rightBand)); - build_filter(&s->v_filters[0][0], (float) s->pad_oheight / - (float) (iheight - topBand - bottomBand)); + av_build_filter(&s->h_filters[0][0], (float) s->pad_owidth / + (float) (iwidth - leftBand - rightBand), NB_TAPS, NB_PHASES, 1<v_filters[0][0], (float) s->pad_oheight / + (float) (iheight - topBand - bottomBand), NB_TAPS, NB_PHASES, 1< /* input */ #define XSIZE 256 @@ -698,11 +655,11 @@ static void dump_filter(int16_t *filter) int i, ph; for(ph=0;phh_filters[0][0]); component_resample(s, img1, xsize, xsize, ysize, img + 50 * XSIZE, XSIZE, XSIZE, YSIZE - 100); img_resample_close(s); - sprintf(buf, "/tmp/out%d.pgm", i); + snprintf(buf, sizeof(buf), "/tmp/out%d.pgm", i); save_pgm(buf, img1, xsize, ysize); } /* mmx test */ #ifdef HAVE_MMX - printf("MMX test\n"); + av_log(NULL, AV_LOG_INFO, "MMX test\n"); fact = 0.72; xsize = (int)(XSIZE * fact); ysize = (int)(YSIZE * fact); @@ -793,10 +750,10 @@ int main(int argc, char **argv) component_resample(s, img2, xsize, xsize, ysize, img, XSIZE, XSIZE, YSIZE); if (memcmp(img1, img2, xsize * ysize) != 0) { - fprintf(stderr, "mmx error\n"); + av_log(NULL, AV_LOG_ERROR, "mmx error\n"); exit(1); } - printf("MMX OK\n"); + av_log(NULL, AV_LOG_INFO, "MMX OK\n"); #endif return 0; } diff --git a/src/libffmpeg/libavcodec/indeo3.c b/src/libffmpeg/libavcodec/indeo3.c index 14ff02858..351af2191 100644 --- a/src/libffmpeg/libavcodec/indeo3.c +++ b/src/libffmpeg/libavcodec/indeo3.c @@ -95,13 +95,16 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char *cur, long fflags2, unsigned char *hdr, unsigned char *buf2, int min_width_160); +#ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) +#endif /* ---------------------------------------------------------------------- */ static void iv_alloc_frames(Indeo3DecodeContext *s) { int luma_width, luma_height, luma_pixels, chroma_width, chroma_height, - chroma_pixels, bufsize, i; + chroma_pixels, i; + unsigned int bufsize; luma_width = (s->width + 3) & (~3); luma_height = (s->height + 3) & (~3); @@ -195,6 +198,10 @@ static unsigned long iv_decode_frame(Indeo3DecodeContext *s, hdr_height = le2me_16(*(uint16_t *)buf_pos); buf_pos += 2; hdr_width = le2me_16(*(uint16_t *)buf_pos); + + if(avcodec_check_dimensions(NULL, hdr_width, hdr_height)) + return -1; + buf_pos += 2; chroma_height = ((hdr_height >> 2) + 3) & 0x7ffc; chroma_width = ((hdr_width >> 2) + 3) & 0x7ffc; @@ -223,6 +230,9 @@ static unsigned long iv_decode_frame(Indeo3DecodeContext *s, hdr_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos, min(hdr_width, 160)); + if (!(s->avctx->flags & CODEC_FLAG_GRAY)) + { + buf_pos = buf + 16 + offs2; offs = le2me_32(*(uint32_t *)buf_pos); buf_pos += 4; @@ -239,6 +249,8 @@ static unsigned long iv_decode_frame(Indeo3DecodeContext *s, chroma_height, buf_pos + offs * 2, fflags2, hdr_pos, buf_pos, min(chroma_width, 40)); + } + return 8; } @@ -304,7 +316,7 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, unsigned char bit_buf; unsigned long bit_pos, lv, lv1, lv2; long *width_tbl, width_tbl_arr[10]; - char *ref_vectors; + signed char *ref_vectors; unsigned char *cur_frm_pos, *ref_frm_pos, *cp, *cp2; uint32_t *cur_lp, *ref_lp; const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2]; @@ -312,6 +324,7 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, ustr_t strip_tbl[20], *strip; int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width, rle_v1, rle_v2, rle_v3; + unsigned short res; bit_buf = 0; ref_vectors = NULL; @@ -446,14 +459,15 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, switch(correction_type_sp[0][k]) { case 0: - *cur_lp = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1); lp2++; break; case 1: - ((unsigned short *)cur_lp)[0] = ((((unsigned short *)(ref_lp))[0] >> 1) - + correction_lp[lp2 & 0x01][*buf1++]) << 1; - ((unsigned short *)cur_lp)[1] = ((((unsigned short *)(ref_lp))[1] >> 1) - + correction_lp[lp2 & 0x01][k]) << 1; + res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1; + ((unsigned short *)cur_lp)[0] = le2me_16(res); + res = ((le2me_16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + ((unsigned short *)cur_lp)[1] = le2me_16(res); + buf1++; lp2++; break; case 2: @@ -548,23 +562,25 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, switch(correction_type_sp[lp2 & 0x01][k]) { case 0: - cur_lp[width_tbl[1]] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1); if(lp2 > 0 || flag1 == 0 || strip->ypos != 0) cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE; else - cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1); lp2++; break; case 1: - ((unsigned short *)cur_lp)[width_tbl[2]] = - ((((unsigned short *)ref_lp)[0] >> 1) + correction_lp[lp2 & 0x01][*buf1++]) << 1; - ((unsigned short *)cur_lp)[width_tbl[2]+1] = - ((((unsigned short *)ref_lp)[1] >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1; + ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res); + res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res); + if(lp2 > 0 || flag1 == 0 || strip->ypos != 0) cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE; else cur_lp[0] = cur_lp[width_tbl[1]]; + buf1++; lp2++; break; @@ -660,16 +676,23 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, lv1 = ref_lp[0]; lv2 = ref_lp[1]; if(lp2 == 0 && flag1 != 0) { +#ifdef WORDS_BIGENDIAN + lv1 = lv1 & 0xFF00FF00; + lv1 = (lv1 >> 8) | lv1; + lv2 = lv2 & 0xFF00FF00; + lv2 = (lv2 >> 8) | lv2; +#else lv1 = lv1 & 0x00FF00FF; lv1 = (lv1 << 8) | lv1; lv2 = lv2 & 0x00FF00FF; lv2 = (lv2 << 8) | lv2; +#endif } switch(correction_type_sp[lp2 & 0x01][k]) { case 0: - cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1; - cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1; + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1); + cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1); if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) { cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE; cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE; @@ -681,8 +704,8 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, break; case 1: - cur_lp[width_tbl[1]] = ((lv1 >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1++]) << 1; - cur_lp[width_tbl[1]+1] = ((lv2 >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1; + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1); + cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1); if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) { cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE; cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE; @@ -690,6 +713,7 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, cur_lp[0] = cur_lp[width_tbl[1]]; cur_lp[1] = cur_lp[width_tbl[1]+1]; } + buf1++; lp2++; break; @@ -824,20 +848,20 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, case 0: lv1 = correctionloworder_lp[lp2 & 0x01][k]; lv2 = correctionhighorder_lp[lp2 & 0x01][k]; - cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1; - cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1; - cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1; - cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1; + cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1); + cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1); + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1); + cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1); lp2++; break; case 1: lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++]; lv2 = correctionloworder_lp[lp2 & 0x01][k]; - cur_lp[0] = ((ref_lp[0] >> 1) + lv1) << 1; - cur_lp[1] = ((ref_lp[1] >> 1) + lv2) << 1; - cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + lv1) << 1; - cur_lp[width_tbl[1]+1] = ((ref_lp[width_tbl[1]+1] >> 1) + lv2) << 1; + cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1); + cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1); + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1); + cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1); lp2++; break; @@ -925,18 +949,22 @@ static void iv_Decode_Chunk(Indeo3DecodeContext *s, switch(correction_type_sp[lp2 & 0x01][k]) { case 0: - cur_lp[0] = ((*ref_lp >> 1) + correction_lp[lp2 & 0x01][k]) << 1; - cur_lp[width_tbl[1]] = ((ref_lp[width_tbl[1]] >> 1) + correction_lp[lp2 & 0x01][k]) << 1; + cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1); + cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1); lp2++; break; case 1: lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]); lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]); - ((unsigned short *)cur_lp)[0] = ((((unsigned short *)ref_lp)[0] >> 1) + lv1) << 1; - ((unsigned short *)cur_lp)[1] = ((((unsigned short *)ref_lp)[1] >> 1) + lv2) << 1; - ((unsigned short *)cur_lp)[width_tbl[2]] = ((((unsigned short *)ref_lp)[width_tbl[2]] >> 1) + lv1) << 1; - ((unsigned short *)cur_lp)[width_tbl[2]+1] = ((((unsigned short *)ref_lp)[width_tbl[2]+1] >> 1) + lv2) << 1; + res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1); + ((unsigned short *)cur_lp)[0] = le2me_16(res); + res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1); + ((unsigned short *)cur_lp)[1] = le2me_16(res); + res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1); + ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res); + res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1); + ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res); lp2++; break; @@ -1056,11 +1084,6 @@ static int indeo3_decode_frame(AVCodecContext *avctx, unsigned char *src, *dest; int y; - /* no supplementary picture */ - if (buf_size == 0) { - return 0; - } - iv_decode_frame(s, buf, buf_size); if(s->frame.data[0]) @@ -1080,6 +1103,8 @@ static int indeo3_decode_frame(AVCodecContext *avctx, dest += s->frame.linesize[0]; } + if (!(s->avctx->flags & CODEC_FLAG_GRAY)) + { src = s->cur_frame->Ubuf; dest = s->frame.data[1]; for (y = 0; y < s->height / 4; y++) { @@ -1095,6 +1120,7 @@ static int indeo3_decode_frame(AVCodecContext *avctx, src += s->cur_frame->uv_w; dest += s->frame.linesize[2]; } + } *data_size=sizeof(AVFrame); *(AVFrame*)data= s->frame; diff --git a/src/libffmpeg/libavcodec/integer.c b/src/libffmpeg/libavcodec/integer.c index 025560f9e..38a826f86 100644 --- a/src/libffmpeg/libavcodec/integer.c +++ b/src/libffmpeg/libavcodec/integer.c @@ -47,6 +47,10 @@ AVInteger av_sub_i(AVInteger a, AVInteger b){ return a; } +/** + * returns the rounded down value of the logarithm of base 2 of the given AVInteger. + * this is simply the index of the most significant bit which is 1. Or 0 of all bits are 0 + */ int av_log2_i(AVInteger a){ int i; @@ -78,6 +82,9 @@ AVInteger av_mul_i(AVInteger a, AVInteger b){ return out; } +/** + * returns 0 if a==b, 1 if a>b and -1 if a= 0; rowctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any row in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * row DCT calculations can be simplified this way. + */ + + register int *idataptr = (int*)dataptr; + + d0 = dataptr[0]; + d2 = dataptr[1]; + d4 = dataptr[2]; + d6 = dataptr[3]; + + if ((d2 | d4 | d6) == 0) { + /* AC terms all zero */ + if (d0) { + /* Compute a 32 bit value to assign. */ + DCTELEM dcval = (DCTELEM) (d0 << PASS1_BITS); + register int v = (dcval & 0xffff) | ((dcval << 16) & 0xffff0000); + + idataptr[0] = v; + idataptr[1] = v; + } + + dataptr += DCTSTRIDE; /* advance pointer to next row */ + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[0] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS); + dataptr[1] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS); + dataptr[2] = (DCTELEM) DESCALE(tmp12, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSTRIDE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + dataptr = data; + for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) { + /* Columns of zeroes can be exploited in the same way as we did with rows. + * However, the row calculation has created many nonzero AC terms, so the + * simplification applies less often (typically 5% to 10% of the time). + * On machines with very fast multiplication, it's possible that the + * test takes more time than it's worth. In that case this section + * may be commented out. + */ + + d0 = dataptr[DCTSTRIDE*0]; + d2 = dataptr[DCTSTRIDE*1]; + d4 = dataptr[DCTSTRIDE*2]; + d6 = dataptr[DCTSTRIDE*3]; + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[DCTSTRIDE*0] = tmp10 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*1] = tmp11 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*2] = tmp12 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*3] = tmp13 >> (CONST_BITS+PASS1_BITS+3); + + dataptr++; /* advance pointer to next column */ + } +} + +void j_rev_dct2(DCTBLOCK data){ + int d00, d01, d10, d11; + + data[0] += 4; + d00 = data[0+0*DCTSTRIDE] + data[1+0*DCTSTRIDE]; + d01 = data[0+0*DCTSTRIDE] - data[1+0*DCTSTRIDE]; + d10 = data[0+1*DCTSTRIDE] + data[1+1*DCTSTRIDE]; + d11 = data[0+1*DCTSTRIDE] - data[1+1*DCTSTRIDE]; + + data[0+0*DCTSTRIDE]= (d00 + d10)>>3; + data[1+0*DCTSTRIDE]= (d01 + d11)>>3; + data[0+1*DCTSTRIDE]= (d00 - d10)>>3; + data[1+1*DCTSTRIDE]= (d01 - d11)>>3; +} + +void j_rev_dct1(DCTBLOCK data){ + data[0] = (data[0] + 4)>>3; +} + #undef FIX #undef CONST_BITS diff --git a/src/libffmpeg/libavcodec/lcl.c b/src/libffmpeg/libavcodec/lcl.c index 9a8591a89..a15a10769 100644 --- a/src/libffmpeg/libavcodec/lcl.c +++ b/src/libffmpeg/libavcodec/lcl.c @@ -41,6 +41,7 @@ #include #include "common.h" +#include "bitstream.h" #include "avcodec.h" #ifdef CONFIG_ZLIB @@ -144,14 +145,15 @@ static inline unsigned char get_r (unsigned char yq, signed char rq) -static int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr) +static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize) { unsigned char *destptr_bak = destptr; + unsigned char *destptr_end = destptr + destsize; unsigned char mask = 0; unsigned char maskbit = 0; unsigned int ofs, cnt; - while (srclen > 0) { + while ((srclen > 0) && (destptr < destptr_end)) { if (maskbit == 0) { mask = *(srcptr++); maskbit = 8; @@ -159,6 +161,8 @@ static int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destp continue; } if ((mask & (1 << (--maskbit))) == 0) { + if (destptr + 4 > destptr_end) + break; *(int*)destptr = *(int*)srcptr; srclen -= 4; destptr += 4; @@ -171,6 +175,9 @@ static int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destp ofs &= 0x7ff; srclen -= 2; cnt *= 4; + if (destptr + cnt > destptr_end) { + cnt = destptr_end - destptr; + } for (; cnt > 0; cnt--) { *(destptr) = *(destptr - ofs); destptr++; @@ -193,7 +200,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 { LclContext * const c = (LclContext *)avctx->priv_data; unsigned char *encoded = (unsigned char *)buf; - int pixel_ptr; + unsigned int pixel_ptr; int row, col; unsigned char *outptr; unsigned int width = avctx->width; // Real image width @@ -205,11 +212,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 #ifdef CONFIG_ZLIB int zret; // Zlib return code #endif - int len = buf_size; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; + unsigned int len = buf_size; if(c->pic.data[0]) avctx->release_buffer(avctx, &c->pic); @@ -231,24 +234,29 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 if (c->flags & FLAG_MULTITHREAD) { mthread_inlen = *((unsigned int*)encoded); mthread_outlen = *((unsigned int*)(encoded+4)); - mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf); + if (mthread_outlen > c->decomp_size) // this should not happen + mthread_outlen = c->decomp_size; + mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size); if (mthread_outlen != mszh_dlen) { av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n", mthread_outlen, mszh_dlen); + return -1; } mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen, - c->decomp_buf + mthread_outlen); - if ((c->decomp_size - mthread_outlen) != mszh_dlen) { + c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen); + if (mthread_outlen != mszh_dlen) { av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n", - c->decomp_size - mthread_outlen, mszh_dlen); + mthread_outlen, mszh_dlen); + return -1; } encoded = c->decomp_buf; len = c->decomp_size; } else { - mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf); + mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size); if (c->decomp_size != mszh_dlen) { av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n", c->decomp_size, mszh_dlen); + return -1; } encoded = c->decomp_buf; len = mszh_dlen; @@ -277,10 +285,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 if (c->flags & FLAG_MULTITHREAD) { mthread_inlen = *((unsigned int*)encoded); mthread_outlen = *((unsigned int*)(encoded+4)); + if (mthread_outlen > c->decomp_size) + mthread_outlen = c->decomp_size; c->zstream.next_in = encoded + 8; c->zstream.avail_in = mthread_inlen; c->zstream.next_out = c->decomp_buf; - c->zstream.avail_out = mthread_outlen; + c->zstream.avail_out = c->decomp_size; zret = inflate(&(c->zstream), Z_FINISH); if ((zret != Z_OK) && (zret != Z_STREAM_END)) { av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret); @@ -289,6 +299,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) { av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n", mthread_outlen, c->zstream.total_out); + return -1; } zret = inflateReset(&(c->zstream)); if (zret != Z_OK) { @@ -298,15 +309,16 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 c->zstream.next_in = encoded + 8 + mthread_inlen; c->zstream.avail_in = len - mthread_inlen; c->zstream.next_out = c->decomp_buf + mthread_outlen; - c->zstream.avail_out = mthread_outlen; + c->zstream.avail_out = c->decomp_size - mthread_outlen; zret = inflate(&(c->zstream), Z_FINISH); if ((zret != Z_OK) && (zret != Z_STREAM_END)) { av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret); return -1; } - if ((c->decomp_size - mthread_outlen) != (unsigned int)(c->zstream.total_out)) { + if (mthread_outlen != (unsigned int)(c->zstream.total_out)) { av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n", - c->decomp_size - mthread_outlen, c->zstream.total_out); + mthread_outlen, c->zstream.total_out); + return -1; } } else { c->zstream.next_in = encoded; @@ -321,6 +333,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) { av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n", c->decomp_size, c->zstream.total_out); + return -1; } } encoded = c->decomp_buf; @@ -567,13 +580,20 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret); return -1; } - c->zstream.next_in = p->data[0]; - c->zstream.avail_in = c->decomp_size; c->zstream.next_out = c->comp_buf; c->zstream.avail_out = c->max_comp_size; + for(i = avctx->height - 1; i >= 0; i--) { + c->zstream.next_in = p->data[0]+p->linesize[0]*i; + c->zstream.avail_in = avctx->width*3; + zret = deflate(&(c->zstream), Z_NO_FLUSH); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); + return -1; + } + } zret = deflate(&(c->zstream), Z_FINISH); - if ((zret != Z_OK) && (zret != Z_STREAM_END)) { + if (zret != Z_STREAM_END) { av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); return -1; } @@ -596,7 +616,9 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, static int decode_init(AVCodecContext *avctx) { LclContext * const c = (LclContext *)avctx->priv_data; - int basesize = avctx->width * avctx->height; + unsigned int basesize = avctx->width * avctx->height; + unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3); + unsigned int max_decomp_size; int zret; // Zlib return code c->avctx = avctx; @@ -614,6 +636,10 @@ static int decode_init(AVCodecContext *avctx) return 1; } + if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { + return 1; + } + /* Check codec type */ if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) || ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) { @@ -624,26 +650,32 @@ static int decode_init(AVCodecContext *avctx) switch (c->imgtype = *((char *)avctx->extradata + 4)) { case IMGTYPE_YUV111: c->decomp_size = basesize * 3; + max_decomp_size = max_basesize * 3; av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n"); break; case IMGTYPE_YUV422: c->decomp_size = basesize * 2; + max_decomp_size = max_basesize * 2; av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n"); break; case IMGTYPE_RGB24: c->decomp_size = basesize * 3; + max_decomp_size = max_basesize * 3; av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n"); break; case IMGTYPE_YUV411: c->decomp_size = basesize / 2 * 3; + max_decomp_size = max_basesize / 2 * 3; av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n"); break; case IMGTYPE_YUV211: c->decomp_size = basesize * 2; + max_decomp_size = max_basesize * 2; av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n"); break; case IMGTYPE_YUV420: c->decomp_size = basesize / 2 * 3; + max_decomp_size = max_basesize / 2 * 3; av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n"); break; default: @@ -698,9 +730,8 @@ static int decode_init(AVCodecContext *avctx) } /* Allocate decompression buffer */ - /* 4*8 max overflow space for mszh decomp algorithm */ if (c->decomp_size) { - if ((c->decomp_buf = av_malloc(c->decomp_size+4*8)) == NULL) { + if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) { av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); return 1; } @@ -785,7 +816,7 @@ static int encode_init(AVCodecContext *avctx) ((uint8_t*)avctx->extradata)[4]= c->imgtype; ((uint8_t*)avctx->extradata)[5]= c->compression; ((uint8_t*)avctx->extradata)[6]= c->flags; - ((uint8_t*)avctx->extradata)[7]= 0; + ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB; c->avctx->extradata_size= 8; c->zstream.zalloc = Z_NULL; @@ -843,7 +874,7 @@ static int encode_end(AVCodecContext *avctx) LclContext *c = avctx->priv_data; av_freep(&avctx->extradata); - av_freep(c->comp_buf); + av_freep(&c->comp_buf); #ifdef CONFIG_ZLIB deflateEnd(&(c->zstream)); #endif @@ -886,7 +917,6 @@ AVCodec zlib_encoder = { encode_init, encode_frame, encode_end, -// .options = lcl_options, }; #endif //CONFIG_ENCODERS diff --git a/src/libffmpeg/libavcodec/libpostproc/mangle.h b/src/libffmpeg/libavcodec/libpostproc/mangle.h index f3894cc33..aa09cd6bf 100644 --- a/src/libffmpeg/libavcodec/libpostproc/mangle.h +++ b/src/libffmpeg/libavcodec/libpostproc/mangle.h @@ -8,12 +8,21 @@ #define __MANGLE_H /* Feel free to add more to the list, eg. a.out IMO */ +/* Use rip-relative addressing if compiling PIC code on x86-64. */ #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__OS2__) || \ (defined(__OpenBSD__) && !defined(__ELF__)) +#if defined(ARCH_X86_64) && defined(PIC) +#define MANGLE(a) "_" #a"(%%rip)" +#else #define MANGLE(a) "_" #a +#endif +#else +#if defined(ARCH_X86_64) && defined(PIC) +#define MANGLE(a) #a"(%%rip)" #else #define MANGLE(a) #a #endif +#endif #endif /* !__MANGLE_H */ diff --git a/src/libffmpeg/libavcodec/libpostproc/postprocess.c b/src/libffmpeg/libavcodec/libpostproc/postprocess.c index a03ff133d..e7ca0191d 100644 --- a/src/libffmpeg/libavcodec/libpostproc/postprocess.c +++ b/src/libffmpeg/libavcodec/libpostproc/postprocess.c @@ -29,10 +29,11 @@ isVertDC Ec Ec Ec isVertMinMaxOk Ec Ec Ec doVertLowPass E e e Ec doVertDefFilter Ec Ec e e Ec -isHorizDC Ec Ec -isHorizMinMaxOk a E -doHorizLowPass E e e -doHorizDefFilter Ec Ec e e +isHorizDC Ec Ec Ec +isHorizMinMaxOk a E Ec +doHorizLowPass E e e Ec +doHorizDefFilter Ec Ec e e Ec +do_a_deblock Ec E Ec E deRing E e e* Ecp Vertical RKAlgo1 E a a Horizontal RKAlgo1 a a @@ -42,7 +43,7 @@ LinIpolDeinterlace e E E* CubicIpolDeinterlace a e e* LinBlendDeinterlace e E E* MedianDeinterlace# E Ec Ec -TempDeNoiser# E e e +TempDeNoiser# E e e Ec * i dont have a 3dnow CPU -> its untested, but noone said it doesnt work so it seems to work # more or less selfinvented filters so the exactness isnt too meaningfull @@ -91,6 +92,10 @@ try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks #include "mangle.h" //FIXME should be supressed +#ifdef HAVE_ALTIVEC_H +#include +#endif + #ifndef HAVE_MEMALIGN #define memalign(a,b) malloc(b) #endif @@ -108,12 +113,15 @@ try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) # define attribute_used __attribute__((used)) +# define always_inline __attribute__((always_inline)) inline #else # define attribute_used +# define always_inline inline #endif -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_X86_64) static uint64_t __attribute__((aligned(8))) attribute_used w05= 0x0005000500050005LL; +static uint64_t __attribute__((aligned(8))) attribute_used w04= 0x0004000400040004LL; static uint64_t __attribute__((aligned(8))) attribute_used w20= 0x0020002000200020LL; static uint64_t __attribute__((aligned(8))) attribute_used b00= 0x0000000000000000LL; static uint64_t __attribute__((aligned(8))) attribute_used b01= 0x0101010101010101LL; @@ -122,7 +130,6 @@ static uint64_t __attribute__((aligned(8))) attribute_used b08= 0x080808080808 static uint64_t __attribute__((aligned(8))) attribute_used b80= 0x8080808080808080LL; #endif - static uint8_t clip_table[3*256]; static uint8_t * const clip_tab= clip_table + 256; @@ -139,6 +146,8 @@ static struct PPFilter filters[]= {"vr", "rkvdeblock", 1, 2, 4, V_RK1_FILTER},*/ {"h1", "x1hdeblock", 1, 1, 3, H_X1_FILTER}, {"v1", "x1vdeblock", 1, 2, 4, V_X1_FILTER}, + {"ha", "ahdeblock", 1, 1, 3, H_A_DEBLOCK}, + {"va", "avdeblock", 1, 2, 4, V_A_DEBLOCK}, {"dr", "dering", 1, 5, 6, DERING}, {"al", "autolevels", 0, 1, 2, LEVEL_FIX}, {"lb", "linblenddeint", 1, 1, 4, LINEAR_BLEND_DEINT_FILTER}, @@ -154,15 +163,16 @@ static struct PPFilter filters[]= static char *replaceTable[]= { - "default", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400", - "de", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400", - "fast", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400", - "fa", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400", + "default", "hdeblock:a,vdeblock:a,dering:a", + "de", "hdeblock:a,vdeblock:a,dering:a", + "fast", "x1hdeblock:a,x1vdeblock:a,dering:a", + "fa", "x1hdeblock:a,x1vdeblock:a,dering:a", + "ac", "ha:a:128:7,va:a,dering:a", NULL //End Marker }; -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_X86_64) static inline void prefetchnta(void *p) { asm volatile( "prefetchnta (%0)\n\t" @@ -372,32 +382,32 @@ static inline void doHorizDefFilter_C(uint8_t dst[], int stride, PPContext *c) */ static inline void doHorizLowPass_C(uint8_t dst[], int stride, PPContext *c) { - int y; for(y=0; yQP ? dst[-1] : dst[0]; const int last= ABS(dst[8] - dst[7]) < c->QP ? dst[8] : dst[7]; - int sums[9]; - sums[0] = first + dst[0]; - sums[1] = dst[0] + dst[1]; - sums[2] = dst[1] + dst[2]; - sums[3] = dst[2] + dst[3]; - sums[4] = dst[3] + dst[4]; - sums[5] = dst[4] + dst[5]; - sums[6] = dst[5] + dst[6]; - sums[7] = dst[6] + dst[7]; - sums[8] = dst[7] + last; - - dst[0]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4; - dst[1]= ((dst[1]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4; - dst[2]= ((dst[2]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4; - dst[3]= ((dst[3]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4; - dst[4]= ((dst[4]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4; - dst[5]= ((dst[5]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4; - dst[6]= (((last + dst[6])<<2) + ((dst[7] + sums[5])<<1) + sums[3] + 8)>>4; - dst[7]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4; + int sums[10]; + sums[0] = 4*first + dst[0] + dst[1] + dst[2] + 4; + sums[1] = sums[0] - first + dst[3]; + sums[2] = sums[1] - first + dst[4]; + sums[3] = sums[2] - first + dst[5]; + sums[4] = sums[3] - first + dst[6]; + sums[5] = sums[4] - dst[0] + dst[7]; + sums[6] = sums[5] - dst[1] + last; + sums[7] = sums[6] - dst[2] + last; + sums[8] = sums[7] - dst[3] + last; + sums[9] = sums[8] - dst[4] + last; + + dst[0]= (sums[0] + sums[2] + 2*dst[0])>>4; + dst[1]= (sums[1] + sums[3] + 2*dst[1])>>4; + dst[2]= (sums[2] + sums[4] + 2*dst[2])>>4; + dst[3]= (sums[3] + sums[5] + 2*dst[3])>>4; + dst[4]= (sums[4] + sums[6] + 2*dst[4])>>4; + dst[5]= (sums[5] + sums[7] + 2*dst[5])>>4; + dst[6]= (sums[6] + sums[8] + 2*dst[6])>>4; + dst[7]= (sums[7] + sums[9] + 2*dst[7])>>4; dst+= stride; } @@ -469,6 +479,111 @@ static inline void horizX1Filter(uint8_t *src, int stride, int QP) } } +/** + * accurate deblock filter + */ +static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPContext *c){ + int y; + const int QP= c->QP; + const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1; + const int dcThreshold= dcOffset*2 + 1; +//START_TIMER + src+= step*4; // src points to begin of the 8x8 Block + for(y=0; y<8; y++){ + int numEq= 0; + + if(((unsigned)(src[-1*step] - src[0*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 0*step] - src[1*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 1*step] - src[2*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 2*step] - src[3*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 3*step] - src[4*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 4*step] - src[5*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 5*step] - src[6*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 6*step] - src[7*step] + dcOffset)) < dcThreshold) numEq++; + if(((unsigned)(src[ 7*step] - src[8*step] + dcOffset)) < dcThreshold) numEq++; + if(numEq > c->ppMode.flatnessThreshold){ + int min, max, x; + + if(src[0] > src[step]){ + max= src[0]; + min= src[step]; + }else{ + max= src[step]; + min= src[0]; + } + for(x=2; x<8; x+=2){ + if(src[x*step] > src[(x+1)*step]){ + if(src[x *step] > max) max= src[ x *step]; + if(src[(x+1)*step] < min) min= src[(x+1)*step]; + }else{ + if(src[(x+1)*step] > max) max= src[(x+1)*step]; + if(src[ x *step] < min) min= src[ x *step]; + } + } + if(max-min < 2*QP){ + const int first= ABS(src[-1*step] - src[0]) < QP ? src[-1*step] : src[0]; + const int last= ABS(src[8*step] - src[7*step]) < QP ? src[8*step] : src[7*step]; + + int sums[10]; + sums[0] = 4*first + src[0*step] + src[1*step] + src[2*step] + 4; + sums[1] = sums[0] - first + src[3*step]; + sums[2] = sums[1] - first + src[4*step]; + sums[3] = sums[2] - first + src[5*step]; + sums[4] = sums[3] - first + src[6*step]; + sums[5] = sums[4] - src[0*step] + src[7*step]; + sums[6] = sums[5] - src[1*step] + last; + sums[7] = sums[6] - src[2*step] + last; + sums[8] = sums[7] - src[3*step] + last; + sums[9] = sums[8] - src[4*step] + last; + + src[0*step]= (sums[0] + sums[2] + 2*src[0*step])>>4; + src[1*step]= (sums[1] + sums[3] + 2*src[1*step])>>4; + src[2*step]= (sums[2] + sums[4] + 2*src[2*step])>>4; + src[3*step]= (sums[3] + sums[5] + 2*src[3*step])>>4; + src[4*step]= (sums[4] + sums[6] + 2*src[4*step])>>4; + src[5*step]= (sums[5] + sums[7] + 2*src[5*step])>>4; + src[6*step]= (sums[6] + sums[8] + 2*src[6*step])>>4; + src[7*step]= (sums[7] + sums[9] + 2*src[7*step])>>4; + } + }else{ + const int middleEnergy= 5*(src[4*step] - src[3*step]) + 2*(src[2*step] - src[5*step]); + + if(ABS(middleEnergy) < 8*QP) + { + const int q=(src[3*step] - src[4*step])/2; + const int leftEnergy= 5*(src[2*step] - src[1*step]) + 2*(src[0*step] - src[3*step]); + const int rightEnergy= 5*(src[6*step] - src[5*step]) + 2*(src[4*step] - src[7*step]); + + int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) ); + d= MAX(d, 0); + + d= (5*d + 32) >> 6; + d*= SIGN(-middleEnergy); + + if(q>0) + { + d= d<0 ? 0 : d; + d= d>q ? q : d; + } + else + { + d= d>0 ? 0 : d; + d= dcpuCaps & PP_CPU_CAPS_MMX2) postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c); @@ -586,7 +691,7 @@ static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int #else #ifdef ARCH_POWERPC #ifdef HAVE_ALTIVEC - else if(c->cpuCaps & PP_CPU_CAPS_ALTIVEC) + if(c->cpuCaps & PP_CPU_CAPS_ALTIVEC) postProcess_altivec(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c); else #endif @@ -614,24 +719,21 @@ static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int /* -pp Command line Help */ char *pp_help= -"[: