annotate ituh263enc.c @ 10830:34739b965809 libavcodec

Make some tables static again, i mistakely thought they would be used in several places.
author michael
date Sat, 09 Jan 2010 17:22:37 +0000
parents d0657e337f91
children f20726a6d538
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10828
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
1 /*
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
2 * ITU H263 bitstream encoder
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
3 * Copyright (c) 2000,2001 Fabrice Bellard
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
4 * H263+ support.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
5 * Copyright (c) 2001 Juan J. Sierralta P
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
6 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
7 *
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
8 * This file is part of FFmpeg.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
9 *
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
10 * FFmpeg is free software; you can redistribute it and/or
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
11 * modify it under the terms of the GNU Lesser General Public
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
12 * License as published by the Free Software Foundation; either
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
13 * version 2.1 of the License, or (at your option) any later version.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
14 *
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
15 * FFmpeg is distributed in the hope that it will be useful,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
18 * Lesser General Public License for more details.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
19 *
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
20 * You should have received a copy of the GNU Lesser General Public
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
21 * License along with FFmpeg; if not, write to the Free Software
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
23 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
24
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
25 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
26 * @file libavcodec/ituh263enc.c
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
27 * h263 encoder.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
28 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
29
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
30 //#define DEBUG
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
31 #include <limits.h>
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
32
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
33 #include "dsputil.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
34 #include "avcodec.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
35 #include "mpegvideo.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
36 #include "h263.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
37 #include "mathops.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
38 #include "unary.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
39 #include "flv.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
40 #include "mpeg4video.h"
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
41
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
42 //#undef NDEBUG
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
43 //#include <assert.h>
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
44
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
45 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
46 * Table of number of bits a motion vector component needs.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
47 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
48 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
49
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
50 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
51 * Minimal fcode that a motion vector component would need.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
52 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
53 static uint8_t fcode_tab[MAX_MV*2+1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
54
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
55 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
56 * Minimal fcode that a motion vector component would need in umv.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
57 * All entries in this table are 1.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
58 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
59 static uint8_t umv_fcode_tab[MAX_MV*2+1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
60
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
61 //unified encoding tables for run length encoding of coefficients
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
62 //unified in the sense that the specification specifies the encoding in several steps.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
63 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
64 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
65 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
66 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
67 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
68
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
69 static const uint8_t wrong_run[102] = {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
70 1, 2, 3, 5, 4, 10, 9, 8,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
71 11, 15, 17, 16, 23, 22, 21, 20,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
72 19, 18, 25, 24, 27, 26, 11, 7,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
73 6, 1, 2, 13, 2, 2, 2, 2,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
74 6, 12, 3, 9, 1, 3, 4, 3,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
75 7, 4, 1, 1, 5, 5, 14, 6,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
76 1, 7, 1, 8, 1, 1, 1, 1,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
77 10, 1, 1, 5, 9, 17, 25, 24,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
78 29, 33, 32, 41, 2, 23, 28, 31,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
79 3, 22, 30, 4, 27, 40, 8, 26,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
80 6, 39, 7, 38, 16, 37, 15, 10,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
81 11, 12, 13, 14, 1, 21, 20, 18,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
82 19, 2, 1, 34, 35, 36
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
83 };
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
84
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
85 int h263_get_picture_format(int width, int height)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
86 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
87 if (width == 128 && height == 96)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
88 return 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
89 else if (width == 176 && height == 144)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
90 return 2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
91 else if (width == 352 && height == 288)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
92 return 3;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
93 else if (width == 704 && height == 576)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
94 return 4;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
95 else if (width == 1408 && height == 1152)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
96 return 5;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
97 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
98 return 7;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
99 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
100
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
101 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
102 * Returns the 4 bit value that specifies the given aspect ratio.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
103 * This may be one of the standard aspect ratios or it specifies
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
104 * that the aspect will be stored explicitly later.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
105 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
106 av_const int ff_h263_aspect_to_info(AVRational aspect){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
107 int i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
108
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
109 if(aspect.num==0) aspect= (AVRational){1,1};
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
110
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
111 for(i=1; i<6; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
112 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
113 return i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
114 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
115 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
116
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
117 return FF_ASPECT_EXTENDED;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
118 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
119
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
120 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
121 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
122 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
123 int best_clock_code=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
124 int best_divisor=60;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
125 int best_error= INT_MAX;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
126
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
127 if(s->h263_plus){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
128 for(i=0; i<2; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
129 int div, error;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
130 div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
131 div= av_clip(div, 1, 127);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
132 error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
133 if(error < best_error){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
134 best_error= error;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
135 best_divisor= div;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
136 best_clock_code= i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
137 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
138 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
139 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
140 s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
141 coded_frame_rate= 1800000;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
142 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
143
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
144 align_put_bits(&s->pb);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
145
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
146 /* Update the pointer to last GOB */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
147 s->ptr_lastgob = put_bits_ptr(&s->pb);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
148 put_bits(&s->pb, 22, 0x20); /* PSC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
149 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
150 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
151 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
152
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
153 put_bits(&s->pb, 1, 1); /* marker */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
154 put_bits(&s->pb, 1, 0); /* h263 id */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
155 put_bits(&s->pb, 1, 0); /* split screen off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
156 put_bits(&s->pb, 1, 0); /* camera off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
157 put_bits(&s->pb, 1, 0); /* freeze picture release off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
158
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
159 format = h263_get_picture_format(s->width, s->height);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
160 if (!s->h263_plus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
161 /* H.263v1 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
162 put_bits(&s->pb, 3, format);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
163 put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
164 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
165 of H.263v1 UMV implies to check the predicted MV after
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
166 calculation of the current MB to see if we're on the limits */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
167 put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
168 put_bits(&s->pb, 1, 0); /* SAC: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
169 put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
170 put_bits(&s->pb, 1, 0); /* only I/P frames, no PB frame */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
171 put_bits(&s->pb, 5, s->qscale);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
172 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
173 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
174 int ufep=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
175 /* H.263v2 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
176 /* H.263 Plus PTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
177
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
178 put_bits(&s->pb, 3, 7);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
179 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
180 if (format == 7)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
181 put_bits(&s->pb,3,6); /* Custom Source Format */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
182 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
183 put_bits(&s->pb, 3, format);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
184
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
185 put_bits(&s->pb,1, s->custom_pcf);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
186 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
187 put_bits(&s->pb,1,0); /* SAC: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
188 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
189 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
190 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
191 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
192 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
193 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
194 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
195 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
196 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
197 put_bits(&s->pb,3,0); /* Reserved */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
198
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
199 put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
200
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
201 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
202 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
203 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
204 put_bits(&s->pb,2,0); /* Reserved */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
205 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
206
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
207 /* This should be here if PLUSPTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
208 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
209
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
210 if (format == 7) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
211 /* Custom Picture Format (CPFMT) */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
212 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
213
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
214 put_bits(&s->pb,4,s->aspect_ratio_info);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
215 put_bits(&s->pb,9,(s->width >> 2) - 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
216 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
217 put_bits(&s->pb,9,(s->height >> 2));
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
218 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
219 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
220 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
221 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
222 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
223 if(s->custom_pcf){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
224 if(ufep){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
225 put_bits(&s->pb, 1, best_clock_code);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
226 put_bits(&s->pb, 7, best_divisor);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
227 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
228 put_sbits(&s->pb, 2, temp_ref>>8);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
229 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
230
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
231 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
232 if (s->umvplus)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
233 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
234 //FIXME check actual requested range
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
235 put_bits(&s->pb,2,1); /* unlimited */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
236 if(s->h263_slice_structured)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
237 put_bits(&s->pb,2,0); /* no weird submodes */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
238
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
239 put_bits(&s->pb, 5, s->qscale);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
240 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
241
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
242 put_bits(&s->pb, 1, 0); /* no PEI */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
243
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
244 if(s->h263_slice_structured){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
245 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
246
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
247 assert(s->mb_x == 0 && s->mb_y == 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
248 ff_h263_encode_mba(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
249
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
250 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
251 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
252
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
253 if(s->h263_aic){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
254 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
255 s->c_dc_scale_table= ff_aic_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
256 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
257 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
258 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
259 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
260 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
261
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
262 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
263 * Encodes a group of blocks header.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
264 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
265 void h263_encode_gob_header(MpegEncContext * s, int mb_line)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
266 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
267 put_bits(&s->pb, 17, 1); /* GBSC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
268
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
269 if(s->h263_slice_structured){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
270 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
271
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
272 ff_h263_encode_mba(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
273
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
274 if(s->mb_num > 1583)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
275 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
276 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
277 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
278 put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
279 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
280 int gob_number= mb_line / s->gob_index;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
281
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
282 put_bits(&s->pb, 5, gob_number); /* GN */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
283 put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
284 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
285 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
286 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
287
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
288 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
289 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
290 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
291 void ff_clean_h263_qscales(MpegEncContext *s){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
292 int i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
293 int8_t * const qscale_table= s->current_picture.qscale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
294
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
295 ff_init_qscale_tab(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
296
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
297 for(i=1; i<s->mb_num; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
298 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
299 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
300 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
301 for(i=s->mb_num-2; i>=0; i--){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
302 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
303 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
304 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
305
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
306 if(s->codec_id != CODEC_ID_H263P){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
307 for(i=1; i<s->mb_num; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
308 int mb_xy= s->mb_index2xy[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
309
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
310 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
311 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
312 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
313 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
314 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
315 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
316
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
317 static const int dquant_code[5]= {1,0,9,2,3};
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
318
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
319 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
320 * encodes a 8x8 block.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
321 * @param block the 8x8 block
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
322 * @param n block index (0-3 are luma, 4-5 are chroma)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
323 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
324 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
325 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
326 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
327 RLTable *rl;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
328
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
329 rl = &ff_h263_rl_inter;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
330 if (s->mb_intra && !s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
331 /* DC coef */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
332 level = block[0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
333 /* 255 cannot be represented, so we clamp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
334 if (level > 254) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
335 level = 254;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
336 block[0] = 254;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
337 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
338 /* 0 cannot be represented also */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
339 else if (level < 1) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
340 level = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
341 block[0] = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
342 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
343 if (level == 128) //FIXME check rv10
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
344 put_bits(&s->pb, 8, 0xff);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
345 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
346 put_bits(&s->pb, 8, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
347 i = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
348 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
349 i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
350 if (s->h263_aic && s->mb_intra)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
351 rl = &rl_intra_aic;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
352
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
353 if(s->alt_inter_vlc && !s->mb_intra){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
354 int aic_vlc_bits=0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
355 int inter_vlc_bits=0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
356 int wrong_pos=-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
357 int aic_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
358
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
359 last_index = s->block_last_index[n];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
360 last_non_zero = i - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
361 for (; i <= last_index; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
362 j = s->intra_scantable.permutated[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
363 level = block[j];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
364 if (level) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
365 run = i - last_non_zero - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
366 last = (i == last_index);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
367
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
368 if(level<0) level= -level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
369
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
370 code = get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
371 aic_code = get_rl_index(&rl_intra_aic, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
372 inter_vlc_bits += rl->table_vlc[code][1]+1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
373 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
374
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
375 if (code == rl->n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
376 inter_vlc_bits += 1+6+8-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
377 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
378 if (aic_code == rl_intra_aic.n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
379 aic_vlc_bits += 1+6+8-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
380 wrong_pos += run + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
381 }else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
382 wrong_pos += wrong_run[aic_code];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
383 last_non_zero = i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
384 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
385 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
386 i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
387 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
388 rl = &rl_intra_aic;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
389 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
390 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
391
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
392 /* AC coefs */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
393 last_index = s->block_last_index[n];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
394 last_non_zero = i - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
395 for (; i <= last_index; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
396 j = s->intra_scantable.permutated[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
397 level = block[j];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
398 if (level) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
399 run = i - last_non_zero - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
400 last = (i == last_index);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
401 sign = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
402 slevel = level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
403 if (level < 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
404 sign = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
405 level = -level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
406 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
407 code = get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
408 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
409 if (code == rl->n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
410 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
411 put_bits(&s->pb, 1, last);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
412 put_bits(&s->pb, 6, run);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
413
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
414 assert(slevel != 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
415
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
416 if(level < 128)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
417 put_sbits(&s->pb, 8, slevel);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
418 else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
419 put_bits(&s->pb, 8, 128);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
420 put_sbits(&s->pb, 5, slevel);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
421 put_sbits(&s->pb, 6, slevel>>5);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
422 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
423 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
424 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
425 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
426 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
427 put_bits(&s->pb, 1, sign);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
428 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
429 last_non_zero = i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
430 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
431 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
432 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
433
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
434 /* Encode MV differences on H.263+ with Unrestricted MV mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
435 static void h263p_encode_umotion(MpegEncContext * s, int val)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
436 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
437 short sval = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
438 short i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
439 short n_bits = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
440 short temp_val;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
441 int code = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
442 int tcode;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
443
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
444 if ( val == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
445 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
446 else if (val == 1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
447 put_bits(&s->pb, 3, 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
448 else if (val == -1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
449 put_bits(&s->pb, 3, 2);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
450 else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
451
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
452 sval = ((val < 0) ? (short)(-val):(short)val);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
453 temp_val = sval;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
454
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
455 while (temp_val != 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
456 temp_val = temp_val >> 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
457 n_bits++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
458 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
459
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
460 i = n_bits - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
461 while (i > 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
462 tcode = (sval & (1 << (i-1))) >> (i-1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
463 tcode = (tcode << 1) | 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
464 code = (code << 2) | tcode;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
465 i--;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
466 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
467 code = ((code << 1) | (val < 0)) << 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
468 put_bits(&s->pb, (2*n_bits)+1, code);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
469 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
470 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
471
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
472 void h263_encode_mb(MpegEncContext * s,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
473 DCTELEM block[6][64],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
474 int motion_x, int motion_y)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
475 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
476 int cbpc, cbpy, i, cbp, pred_x, pred_y;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
477 int16_t pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
478 int16_t rec_intradc[6];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
479 int16_t *dc_ptr[6];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
480 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
481
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
482 if (!s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
483 /* compute cbp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
484 cbp= get_p_cbp(s, block, motion_x, motion_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
485
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
486 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
487 /* skip macroblock */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
488 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
489 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
490 s->misc_bits++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
491 s->last_bits++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
492 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
493 s->skip_count++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
494
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
495 return;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
496 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
497 put_bits(&s->pb, 1, 0); /* mb coded */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
498
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
499 cbpc = cbp & 3;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
500 cbpy = cbp >> 2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
501 if(s->alt_inter_vlc==0 || cbpc!=3)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
502 cbpy ^= 0xF;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
503 if(s->dquant) cbpc+= 8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
504 if(s->mv_type==MV_TYPE_16X16){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
505 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
506 ff_h263_inter_MCBPC_bits[cbpc],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
507 ff_h263_inter_MCBPC_code[cbpc]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
508
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
509 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
510 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
511 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
512
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
513 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
514 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
515 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
516
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
517 /* motion vectors: 16x16 mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
518 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
519
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
520 if (!s->umvplus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
521 ff_h263_encode_motion_vector(s, motion_x - pred_x,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
522 motion_y - pred_y, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
523 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
524 else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
525 h263p_encode_umotion(s, motion_x - pred_x);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
526 h263p_encode_umotion(s, motion_y - pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
527 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
528 /* To prevent Start Code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
529 put_bits(&s->pb,1,1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
530 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
531 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
532 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
533 ff_h263_inter_MCBPC_bits[cbpc+16],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
534 ff_h263_inter_MCBPC_code[cbpc+16]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
535 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
536 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
537 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
538
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
539 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
540 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
541 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
542
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
543 for(i=0; i<4; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
544 /* motion vectors: 8x8 mode*/
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
545 h263_pred_motion(s, i, 0, &pred_x, &pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
546
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
547 motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
548 motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
549 if (!s->umvplus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
550 ff_h263_encode_motion_vector(s, motion_x - pred_x,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
551 motion_y - pred_y, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
552 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
553 else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
554 h263p_encode_umotion(s, motion_x - pred_x);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
555 h263p_encode_umotion(s, motion_y - pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
556 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
557 /* To prevent Start Code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
558 put_bits(&s->pb,1,1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
559 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
560 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
561 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
562
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
563 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
564 s->mv_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
565 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
566 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
567 assert(s->mb_intra);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
568
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
569 cbp = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
570 if (s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
571 /* Predict DC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
572 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
573 int16_t level = block[i][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
574 int scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
575
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
576 if(i<4) scale= s->y_dc_scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
577 else scale= s->c_dc_scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
578
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
579 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
580 level -= pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
581 /* Quant */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
582 if (level >= 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
583 level = (level + (scale>>1))/scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
584 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
585 level = (level - (scale>>1))/scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
586
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
587 /* AIC can change CBP */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
588 if (level == 0 && s->block_last_index[i] == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
589 s->block_last_index[i] = -1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
590
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
591 if(!s->modified_quant){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
592 if (level < -127)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
593 level = -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
594 else if (level > 127)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
595 level = 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
596 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
597
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
598 block[i][0] = level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
599 /* Reconstruction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
600 rec_intradc[i] = scale*level + pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
601 /* Oddify */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
602 rec_intradc[i] |= 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
603 //if ((rec_intradc[i] % 2) == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
604 // rec_intradc[i]++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
605 /* Clipping */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
606 if (rec_intradc[i] < 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
607 rec_intradc[i] = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
608 else if (rec_intradc[i] > 2047)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
609 rec_intradc[i] = 2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
610
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
611 /* Update AC/DC tables */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
612 *dc_ptr[i] = rec_intradc[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
613 if (s->block_last_index[i] >= 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
614 cbp |= 1 << (5 - i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
615 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
616 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
617 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
618 /* compute cbp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
619 if (s->block_last_index[i] >= 1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
620 cbp |= 1 << (5 - i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
621 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
622 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
623
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
624 cbpc = cbp & 3;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
625 if (s->pict_type == FF_I_TYPE) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
626 if(s->dquant) cbpc+=4;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
627 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
628 ff_h263_intra_MCBPC_bits[cbpc],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
629 ff_h263_intra_MCBPC_code[cbpc]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
630 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
631 if(s->dquant) cbpc+=8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
632 put_bits(&s->pb, 1, 0); /* mb coded */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
633 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
634 ff_h263_inter_MCBPC_bits[cbpc + 4],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
635 ff_h263_inter_MCBPC_code[cbpc + 4]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
636 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
637 if (s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
638 /* XXX: currently, we do not try to use ac prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
639 put_bits(&s->pb, 1, 0); /* no AC prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
640 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
641 cbpy = cbp >> 2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
642 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
643 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
644 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
645
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
646 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
647 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
648 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
649 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
650
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
651 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
652 /* encode each block */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
653 h263_encode_block(s, block[i], i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
654
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
655 /* Update INTRADC for decoding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
656 if (s->h263_aic && s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
657 block[i][0] = rec_intradc[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
658
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
659 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
660 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
661
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
662 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
663 if (!s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
664 s->p_tex_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
665 s->f_count++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
666 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
667 s->i_tex_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
668 s->i_count++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
669 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
670 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
671 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
672
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
673 void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
674 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
675 int range, l, bit_size, sign, code, bits;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
676
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
677 if (val == 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
678 /* zero vector */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
679 code = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
680 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
681 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
682 bit_size = f_code - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
683 range = 1 << bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
684 /* modulo encoding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
685 l= INT_BIT - 6 - bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
686 val = (val<<l)>>l;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
687 sign = val>>31;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
688 val= (val^sign)-sign;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
689 sign&=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
690
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
691 val--;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
692 code = (val >> bit_size) + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
693 bits = val & (range - 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
694
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
695 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
696 if (bit_size > 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
697 put_bits(&s->pb, bit_size, bits);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
698 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
699 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
700 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
701
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
702 static void init_mv_penalty_and_fcode(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
703 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
704 int f_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
705 int mv;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
706
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
707 for(f_code=1; f_code<=MAX_FCODE; f_code++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
708 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
709 int len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
710
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
711 if(mv==0) len= mvtab[0][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
712 else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
713 int val, bit_size, code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
714
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
715 bit_size = f_code - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
716
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
717 val=mv;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
718 if (val < 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
719 val = -val;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
720 val--;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
721 code = (val >> bit_size) + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
722 if(code<33){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
723 len= mvtab[code][1] + 1 + bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
724 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
725 len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
726 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
727 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
728
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
729 mv_penalty[f_code][mv+MAX_MV]= len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
730 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
731 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
732
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
733 for(f_code=MAX_FCODE; f_code>0; f_code--){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
734 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
735 fcode_tab[mv+MAX_MV]= f_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
736 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
737 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
738
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
739 for(mv=0; mv<MAX_MV*2+1; mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
740 umv_fcode_tab[mv]= 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
741 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
742 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
743
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
744 static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
745 int slevel, run, last;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
746
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
747 assert(MAX_LEVEL >= 64);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
748 assert(MAX_RUN >= 63);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
749
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
750 for(slevel=-64; slevel<64; slevel++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
751 if(slevel==0) continue;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
752 for(run=0; run<64; run++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
753 for(last=0; last<=1; last++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
754 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
755 int level= slevel < 0 ? -slevel : slevel;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
756 int sign= slevel < 0 ? 1 : 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
757 int bits, len, code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
758
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
759 len_tab[index]= 100;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
760
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
761 /* ESC0 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
762 code= get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
763 bits= rl->table_vlc[code][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
764 len= rl->table_vlc[code][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
765 bits=bits*2+sign; len++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
766
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
767 if(code!=rl->n && len < len_tab[index]){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
768 if(bits_tab) bits_tab[index]= bits;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
769 len_tab [index]= len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
770 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
771 /* ESC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
772 bits= rl->table_vlc[rl->n][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
773 len = rl->table_vlc[rl->n][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
774 bits=bits*2+last; len++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
775 bits=bits*64+run; len+=6;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
776 bits=bits*256+(level&0xff); len+=8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
777
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
778 if(len < len_tab[index]){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
779 if(bits_tab) bits_tab[index]= bits;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
780 len_tab [index]= len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
781 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
782 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
783 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
784 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
785 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
786
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
787 void h263_encode_init(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
788 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
789 static int done = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
790
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
791 if (!done) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
792 done = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
793
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
794 init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
795 init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
796
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
797 init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
798 init_uni_h263_rl_tab(&ff_h263_rl_inter , NULL, uni_h263_inter_rl_len);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
799
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
800 init_mv_penalty_and_fcode(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
801 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
802 s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
803
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
804 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
805 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
806 if(s->h263_aic){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
807 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
808 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
809 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
810 s->ac_esc_length= 7+1+6+8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
811
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
812 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
813 switch(s->codec_id){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
814 case CODEC_ID_MPEG4:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
815 s->fcode_tab= fcode_tab;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
816 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
817 case CODEC_ID_H263P:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
818 if(s->umvplus)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
819 s->fcode_tab= umv_fcode_tab;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
820 if(s->modified_quant){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
821 s->min_qcoeff= -2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
822 s->max_qcoeff= 2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
823 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
824 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
825 s->max_qcoeff= 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
826 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
827 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
828 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
829 case CODEC_ID_FLV1:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
830 if (s->h263_flv > 1) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
831 s->min_qcoeff= -1023;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
832 s->max_qcoeff= 1023;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
833 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
834 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
835 s->max_qcoeff= 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
836 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
837 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
838 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
839 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
840 default: //nothing needed - default table already set in mpegvideo.c
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
841 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
842 s->max_qcoeff= 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
843 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
844 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
845 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
846 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
847
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
848 void ff_h263_encode_mba(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
849 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
850 int i, mb_pos;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
851
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
852 for(i=0; i<6; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
853 if(s->mb_num-1 <= ff_mba_max[i]) break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
854 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
855 mb_pos= s->mb_x + s->mb_width*s->mb_y;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
856 put_bits(&s->pb, ff_mba_length[i], mb_pos);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
857 }