1 /*
2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
36
38 #define V2_INTRA_CBPC_VLC_BITS 3
39 #define V2_MB_TYPE_VLC_BITS 7
41 #define V2_MV_VLC_BITS 9
42 #define TEX_VLC_BITS 9
43
44 #define DEFAULT_INTER_INDEX 3
45
48 {
49 int i;
50
51 if (n < 4) {
52 i= 0;
53 } else {
54 i= n-3;
55 }
56
59 }
60
61 /****************************************/
62 /* decoding stuff */
63
71
72 /* This is identical to H.263 except that its range is multiplied by 2. */
74 {
76
78 ff_dlog(s,
"MV code %d at %d %d pred: %d\n", code, s->
mb_x,s->
mb_y, pred);
79 if (code < 0)
80 return 0xffff;
81
82 if (code == 0)
85 shift = f_code - 1;
86 val = code;
87 if (shift) {
88 val = (val - 1) << shift;
90 val++;
91 }
92 if (sign)
94
96 if (val <= -64)
97 val += 64;
98 else if (val >= 64)
99 val -= 64;
100
102 }
103
105 {
106 int cbp, code, i;
108
112 /* skip mb */
114 for(i=0;i<6;i++)
122 return 0;
123 }
124 }
125
128 else
130 if(code<0 || code>7){
132 return -1;
133 }
134
136
137 cbp = code & 0x3;
138 } else {
142 else
144 if(cbp<0 || cbp>3){
146 return -1;
147 }
148 }
149
151 int mx, my, cbpy;
152
154 if(cbpy<0){
156 return -1;
157 }
158
159 cbp|= cbpy<<2;
161
165
171 } else {
172 int v;
176 if (v < 0) {
178 return -1;
179 }
180 cbp|= v<<2;
181 } else{
184 if (v < 0) {
186 return -1;
187 }
188 cbp|= v<<2;
190 }
192 }
193
195 for (i = 0; i < 6; i++) {
197 {
199 return -1;
200 }
201 }
202 return 0;
203 }
204
206 {
207 int cbp, code, i;
210
213
217 /* skip mb */
219 for(i=0;i<6;i++)
227
228 return 0;
229 }
230 }
231
233 if (code < 0)
234 return -1;
235 //s->mb_intra = (code & 0x40) ? 0 : 1;
237
238 cbp = code & 0x3f;
239 } else {
242 if (code < 0)
243 return -1;
244 /* predict coded block pattern */
245 cbp = 0;
246 for(i=0;i<6;i++) {
247 int val = ((code >> (5 - i)) & 1);
248 if (i < 4) {
252 }
253 cbp |= val << (5 - i);
254 }
255 }
256
258 int mx, my;
262 }
265 return -1;
271 } else {
273 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
281 }
285 }
286 }
287
289 for (i = 0; i < 6; i++) {
291 {
293 return -1;
294 }
295 }
296
297 return 0;
298 }
299
300 /* init all vlc decoding tables */
302 {
304 static volatile int done = 0;
305 int i, ret;
307
309 return ret;
310
312 return -1;
313
315
316 if (!done) {
319 }
326
335
348
355
365
378
382
386 done = 1;
387 }
388
390 case 1:
391 case 2:
393 break;
394 case 3:
395 case 4:
397 break;
398 case 5:
399 if (CONFIG_WMV2_DECODER)
401 case 6:
402 //FIXME + TODO VC1 decode mb
403 break;
404 }
405
407
408 return 0;
409 }
410
412 {
413 int code;
414
417 if(start_code!=0x00000100){
419 return -1;
420 }
421
423 }
424
429 return -1;
430 }
434 return -1;
435 }
436
442 return -1;
443 }
444
446 }else{
447 /* 0x17: one slice, 0x18: two slices, ... */
448 if (code < 0x17){
450 return -1;
451 }
452
454 }
455
457 case 1:
458 case 2:
461
463 break;
464 case 3:
467
469 break;
470 case 4:
472
475
479 }
480
483 break;
484 }
494 } else {
496 case 1:
497 case 2:
500 else
506 break;
507 case 3:
511
513
515 break;
516 case 4:
518
521
525 }
526
528
531 break;
532 }
533
543
546 }else{
548 }
549 }
552
555
556 return 0;
557 }
558
560 {
563 /* the alt_bitstream reader could read over the end so we need to check it */
564 if(left>=length && left<length+8)
565 {
570 else
572 }
573 else if(left<length+8)
574 {
578 }
579 else
580 {
582 }
583
584 return 0;
585 }
586
588 {
590
592 if (n < 4) {
594 } else {
596 }
597 if (level < 0) {
599 *dir_ptr = 0;
600 return -1;
601 }
602 level-=256;
603 }else{ //FIXME optimize use unified tables & index
604 if (n < 4) {
606 } else {
608 }
609 if (level < 0){
611 *dir_ptr = 0;
612 return -1;
613 }
614
619 } else if (level != 0) {
622 }
623 }
624
629
630 /* update predictor */
632 }else{
633 int16_t *dc_val;
636
637 /* update predictor */
638 if (n < 4) {
640 } else {
642 }
643 }
644
646 }
647
649 int n,
int coded,
const uint8_t *scan_table)
650 {
655 int qmul, qadd;
656
658 qmul=1;
659 qadd=0;
660
661 /* DC coef */
663
664 if (level < 0){
667 }
668 if (n < 4) {
673 }
674 } else {
679 }
680 }
682
684 i = 0;
685 if (!coded) {
686 goto not_coded;
687 }
689 if (dc_pred_dir == 0)
691 else
693 } else {
695 }
697 } else {
699 qadd = (s->
qscale - 1) | 1;
700 i = -1;
702
704 run_diff = 0;
705 else
706 run_diff = 1;
707
708 if (!coded) {
710 return 0;
711 }
712 if(!scan_table)
715 }
716 {
718 for(;;) {
721 if (level==0) {
722 int cache;
724 /* escape */
727 /* third escape */
735 }else{
736 int sign;
739 int ll;
744 if(ll==0){
746 }
747 }else{
748 ll=2;
750 ll++;
752 }
754 }
755
759 }
762
765
768 if(sign) level= -
level;
769 }
770
771 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
772 if (level>0) level= level * qmul + qadd;
773 else level= level * qmul - qadd;
774 i+= run + 1;
775 if(last) i+=192;
776 } else {
777 /* second escape */
780 i+= run + rl->
max_run[run>>7][level/qmul] + run_diff;
//FIXME opt indexing
783 }
784 } else {
785 /* first escape */
789 level = level + rl->
max_level[run>>7][(run-1)&63] * qmul;
//FIXME opt indexing
792 }
793 } else {
797 }
798 if (i > 62){
799 i-= 192;
800 if(i&(~63)){
802 if (((i + 192 == 64 && level / qmul == -1) ||
804 left >= 0) {
806 i = 63;
807 break;
808 }else{
810 return -1;
811 }
812 }
813
814 block[scan_table[i]] =
level;
815 break;
816 }
817
818 block[scan_table[i]] =
level;
819 }
821 }
822 not_coded:
826 i = 63; /* XXX: not optimal */
827 }
828 }
831
832 return 0;
833 }
834
836 int *mx_ptr, int *my_ptr)
837 {
839 int code, mx, my;
840
842
844 if (code < 0){
846 return -1;
847 }
851 } else {
854 }
855
856 mx += *mx_ptr - 32;
857 my += *my_ptr - 32;
858 /* WARNING : they do not do exactly modulo encoding */
859 if (mx <= -64)
860 mx += 64;
861 else if (mx >= 64)
862 mx -= 64;
863
864 if (my <= -64)
865 my += 64;
866 else if (my >= 64)
867 my -= 64;
868 *mx_ptr = mx;
869 *my_ptr = my;
870 return 0;
871 }
872
884 .max_lowres = 3,
888 },
889 };
890
902 .max_lowres = 3,
906 },
907 };
908
920 .max_lowres = 3,
924 },
925 };
926
938 .max_lowres = 3,
942 },
943 };
#define DEFAULT_INTER_INDEX
const char const char void * val
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
ScanTable intra_v_scantable
static int shift(int a, int b)
static int msmpeg4v2_decode_motion(MpegEncContext *s, int pred, int f_code)
VLC ff_mb_non_intra_vlc[4]
uint32_t ff_v2_dc_lum_table[512][2]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
const uint32_t ff_table0_dc_lum[120][2]
static av_cold int init(AVCodecContext *avctx)
VLC ff_h263_intra_MCBPC_vlc
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
uint32_t ff_v2_dc_chroma_table[512][2]
#define SKIP_COUNTER(name, gb, num)
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
void(* clear_blocks)(int16_t *blocks)
static VLC v2_dc_chroma_vlc
#define AV_EF_BITSTREAM
detect bitstream specification deviations
#define MB_NON_INTRA_VLC_BITS
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
int8_t * max_run[2]
encoding & decoding
const uint8_t ff_v2_intra_cbpc[4][2]
int ff_msmpeg4_decode_block(MpegEncContext *s, int16_t *block, int n, int coded, const uint8_t *scan_table)
uint8_t ff_static_rl_table_store[NB_RL_TABLES][2][2 *MAX_RUN+MAX_LEVEL+3]
#define FF_DEBUG_PICT_INFO
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
Picture current_picture
copy of the current picture structure.
static int get_bits_count(const GetBitContext *s)
int8_t * max_level[2]
encoding & decoding
const uint32_t ff_table0_dc_chroma[120][2]
int mb_height
number of MBs horizontally & vertically
static int msmpeg4v1_pred_dc(MpegEncContext *s, int n, int32_t **dc_val_ptr)
static int get_bits_left(GetBitContext *gb)
#define UPDATE_CACHE(name, gb)
const uint16_t ff_msmp4_mb_i_table[64][2]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int last_dc[3]
last DC values for MPEG-1
static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
int rl_chroma_table_index
int ff_msmpeg4_decode_picture_header(MpegEncContext *s)
int mb_skipped
MUST BE SET only during DECODING.
void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
Predict the ac.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define INTER_MCBPC_VLC_BITS
const char * name
Name of the codec implementation.
#define CLOSE_READER(name, gb)
#define INIT_VLC_RL(rl, static_size)
#define V2_INTRA_CBPC_VLC_BITS
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
#define SKIP_BITS(name, gb, num)
int ff_h263_decode_init(AVCodecContext *avctx)
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
VLC ff_h263_inter_MCBPC_vlc
int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
const uint8_t ff_v2_mb_type[8][2]
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
const uint8_t * table_mvx
int width
picture width / height.
const uint8_t * table_mv_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static int msmpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
#define LAST_SKIP_BITS(name, gb, num)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
int block_last_index[12]
last non zero coefficient in block
const uint32_t ff_table1_dc_chroma[120][2]
RL_VLC_ELEM * rl_vlc[32]
decoding only
#define SHOW_UBITS(name, gb, num)
int ff_msmpeg4_decode_ext_header(MpegEncContext *s, int buf_size)
static const float pred[4]
static const int8_t mv[256][2]
AVCodec ff_msmpeg4v1_decoder
#define MV_TYPE_16X16
1 vector for the whole mb
Libavcodec external API header.
const uint32_t(*const [WMV2_INTER_CBP_TABLE_COUNT] ff_wmv2_inter_table)[2]
AVCodec ff_msmpeg4v2_decoder
main external API structure.
static VLC v2_intra_cbpc_vlc
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
#define OPEN_READER(name, gb)
const uint16_t * table_mv_code
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
const uint32_t ff_table1_dc_lum[120][2]
static unsigned int get_bits1(GetBitContext *s)
#define V2_MB_TYPE_VLC_BITS
ScanTable intra_h_scantable
static void skip_bits(GetBitContext *s, int n)
#define GET_CACHE(name, gb)
#define MB_INTRA_VLC_BITS
int slice_height
in macroblocks
#define SKIP_CACHE(name, gb, num)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
static enum AVPixelFormat pix_fmts[]
RLTable ff_rl_table[NB_RL_TABLES]
static VLC v2_mb_type_vlc
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
struct AVCodecContext * avctx
#define SHOW_SBITS(name, gb, num)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
AVCodec ff_msmpeg4v3_decoder
int ff_h263_decode_end(AVCodecContext *avctx)
int16_t(* block)[64]
points to one of the following blocks
static int decode012(GetBitContext *gb)
VLC_TYPE(* table)[2]
code, bits
int64_t bit_rate
wanted bit rate
int ff_msmpeg4_decode_motion(MpegEncContext *s, int *mx_ptr, int *my_ptr)
int chroma_qscale
chroma QP
#define INTER_INTRA_VLC_BITS
uint32_t * mb_type
types and macros are defined in mpegutils.h
const uint8_t * table_mvy
ScanTable inter_scantable
if inter == intra then intra should be used to reduce the cache usage
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
int(* decode_mb)(struct MpegEncContext *s, int16_t block[12][64])
int h263_aic_dir
AIC direction: 0 = left, 1 = top.
int ff_wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
AVPixelFormat
Pixel format.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
const uint8_t ff_table_inter_intra[4][2]
VLC ff_msmp4_dc_luma_vlc[2]
#define INTRA_MCBPC_VLC_BITS
av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
VLC ff_msmp4_dc_chroma_vlc[2]
const uint8_t ff_mvtab[33][2]