annotate ituh263enc.c @ 11899:c1eed9efe386 libavcodec

psdec: Simplify filter addressing by incrementing the "in" pointer.
author alexc
date Sun, 20 Jun 2010 22:08:33 +0000
parents 7dd2a45249a9
children fdafbcef52f5
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 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 10851
diff changeset
26 * @file
10851
07dbba7c16ec Better title for the file.
michael
parents: 10843
diff changeset
27 * h263 bitstream encoder.
10828
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"
10843
f6afc7837f83 Add missing internal.h to files calling ff_match_2uint16().
astrange
parents: 10832
diff changeset
41 #include "internal.h"
10828
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
42
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
43 //#undef NDEBUG
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
44 //#include <assert.h>
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 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
47 * Table of number of bits a motion vector component needs.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
48 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
49 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
50
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
51 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
52 * Minimal fcode that a motion vector component would need.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
53 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
54 static uint8_t fcode_tab[MAX_MV*2+1];
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 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
57 * Minimal fcode that a motion vector component would need in umv.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
58 * All entries in this table are 1.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
59 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
60 static uint8_t umv_fcode_tab[MAX_MV*2+1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
61
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
62 //unified encoding tables for run length encoding of coefficients
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
63 //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
64 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
65 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
66 //#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
67 //#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
68 #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
69
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
70 static const uint8_t wrong_run[102] = {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
71 1, 2, 3, 5, 4, 10, 9, 8,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
72 11, 15, 17, 16, 23, 22, 21, 20,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
73 19, 18, 25, 24, 27, 26, 11, 7,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
74 6, 1, 2, 13, 2, 2, 2, 2,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
75 6, 12, 3, 9, 1, 3, 4, 3,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
76 7, 4, 1, 1, 5, 5, 14, 6,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
77 1, 7, 1, 8, 1, 1, 1, 1,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
78 10, 1, 1, 5, 9, 17, 25, 24,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
79 29, 33, 32, 41, 2, 23, 28, 31,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
80 3, 22, 30, 4, 27, 40, 8, 26,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
81 6, 39, 7, 38, 16, 37, 15, 10,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
82 11, 12, 13, 14, 1, 21, 20, 18,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
83 19, 2, 1, 34, 35, 36
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
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 * Returns the 4 bit value that specifies the given aspect ratio.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
88 * 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
89 * that the aspect will be stored explicitly later.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
90 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
91 av_const int ff_h263_aspect_to_info(AVRational aspect){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
92 int i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
93
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
94 if(aspect.num==0) aspect= (AVRational){1,1};
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
95
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
96 for(i=1; i<6; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
97 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
98 return i;
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 return FF_ASPECT_EXTENDED;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
103 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
104
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
105 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
106 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
107 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
108 int best_clock_code=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
109 int best_divisor=60;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
110 int best_error= INT_MAX;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
111
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
112 if(s->h263_plus){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
113 for(i=0; i<2; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
114 int div, error;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
115 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
116 div= av_clip(div, 1, 127);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
117 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
118 if(error < best_error){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
119 best_error= error;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
120 best_divisor= div;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
121 best_clock_code= i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
122 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
123 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
124 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
125 s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
126 coded_frame_rate= 1800000;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
127 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
128
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
129 align_put_bits(&s->pb);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
130
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
131 /* Update the pointer to last GOB */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
132 s->ptr_lastgob = put_bits_ptr(&s->pb);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
133 put_bits(&s->pb, 22, 0x20); /* PSC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
134 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
135 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
136 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
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 put_bits(&s->pb, 1, 1); /* marker */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
139 put_bits(&s->pb, 1, 0); /* h263 id */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
140 put_bits(&s->pb, 1, 0); /* split screen off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
141 put_bits(&s->pb, 1, 0); /* camera off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
142 put_bits(&s->pb, 1, 0); /* freeze picture release off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
143
10832
f20726a6d538 Add a function to match a 2 element vector of uint16_t and use it in h263 and svq1
michael
parents: 10828
diff changeset
144 format = ff_match_2uint16(h263_format, FF_ARRAY_ELEMS(h263_format), s->width, s->height);
10828
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
145 if (!s->h263_plus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
146 /* H.263v1 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
147 put_bits(&s->pb, 3, format);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
148 put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
149 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
150 of H.263v1 UMV implies to check the predicted MV after
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
151 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
152 put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
153 put_bits(&s->pb, 1, 0); /* SAC: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
154 put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
155 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
156 put_bits(&s->pb, 5, s->qscale);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
157 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
158 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
159 int ufep=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
160 /* H.263v2 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
161 /* H.263 Plus PTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
162
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
163 put_bits(&s->pb, 3, 7);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
164 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
165 if (format == 7)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
166 put_bits(&s->pb,3,6); /* Custom Source Format */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
167 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
168 put_bits(&s->pb, 3, format);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
169
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
170 put_bits(&s->pb,1, s->custom_pcf);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
171 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
172 put_bits(&s->pb,1,0); /* SAC: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
173 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
174 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
175 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
176 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
177 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
178 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
179 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
180 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
181 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
182 put_bits(&s->pb,3,0); /* Reserved */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
183
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
184 put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
185
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
186 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
187 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
188 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
189 put_bits(&s->pb,2,0); /* Reserved */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
190 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
191
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
192 /* This should be here if PLUSPTYPE */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
193 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
194
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
195 if (format == 7) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
196 /* Custom Picture Format (CPFMT) */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
197 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
198
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
199 put_bits(&s->pb,4,s->aspect_ratio_info);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
200 put_bits(&s->pb,9,(s->width >> 2) - 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
201 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
202 put_bits(&s->pb,9,(s->height >> 2));
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
203 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
204 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
205 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
208 if(s->custom_pcf){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
209 if(ufep){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
210 put_bits(&s->pb, 1, best_clock_code);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
211 put_bits(&s->pb, 7, best_divisor);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
212 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
213 put_sbits(&s->pb, 2, temp_ref>>8);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
214 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
215
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
216 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
217 if (s->umvplus)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
218 // 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
219 //FIXME check actual requested range
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
220 put_bits(&s->pb,2,1); /* unlimited */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
221 if(s->h263_slice_structured)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
222 put_bits(&s->pb,2,0); /* no weird submodes */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
223
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
224 put_bits(&s->pb, 5, s->qscale);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
225 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
226
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
227 put_bits(&s->pb, 1, 0); /* no PEI */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
228
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
229 if(s->h263_slice_structured){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
230 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
231
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
232 assert(s->mb_x == 0 && s->mb_y == 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
233 ff_h263_encode_mba(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
234
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
235 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
236 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
237
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
238 if(s->h263_aic){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
239 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
240 s->c_dc_scale_table= ff_aic_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
241 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
242 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
243 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
244 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
245 }
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 /**
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
248 * Encodes a group of blocks header.
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 void h263_encode_gob_header(MpegEncContext * s, int mb_line)
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 put_bits(&s->pb, 17, 1); /* GBSC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
253
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
254 if(s->h263_slice_structured){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
255 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
256
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
257 ff_h263_encode_mba(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
258
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
259 if(s->mb_num > 1583)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
260 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
261 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
262 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
263 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
264 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
265 int gob_number= mb_line / s->gob_index;
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, 5, gob_number); /* GN */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
268 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
269 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
270 }
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
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 * 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
275 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
276 void ff_clean_h263_qscales(MpegEncContext *s){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
277 int i;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
278 int8_t * const qscale_table= s->current_picture.qscale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
279
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
280 ff_init_qscale_tab(s);
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 for(i=1; i<s->mb_num; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
283 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
284 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
285 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
286 for(i=s->mb_num-2; i>=0; i--){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
287 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
288 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
289 }
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 if(s->codec_id != CODEC_ID_H263P){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
292 for(i=1; i<s->mb_num; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
293 int mb_xy= s->mb_index2xy[i];
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 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
296 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
297 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
298 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
299 }
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
302 static const int dquant_code[5]= {1,0,9,2,3};
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
303
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 * encodes a 8x8 block.
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
306 * @param block the 8x8 block
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
307 * @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
308 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
309 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
310 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
311 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
312 RLTable *rl;
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 rl = &ff_h263_rl_inter;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
315 if (s->mb_intra && !s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
316 /* DC coef */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
317 level = block[0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
318 /* 255 cannot be represented, so we clamp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
319 if (level > 254) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
320 level = 254;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
321 block[0] = 254;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
322 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
323 /* 0 cannot be represented also */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
324 else if (level < 1) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
325 level = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
326 block[0] = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
327 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
328 if (level == 128) //FIXME check rv10
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
329 put_bits(&s->pb, 8, 0xff);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
330 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
331 put_bits(&s->pb, 8, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
332 i = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
333 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
334 i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
335 if (s->h263_aic && s->mb_intra)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
336 rl = &rl_intra_aic;
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 if(s->alt_inter_vlc && !s->mb_intra){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
339 int aic_vlc_bits=0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
340 int inter_vlc_bits=0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
341 int wrong_pos=-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
342 int aic_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
343
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
344 last_index = s->block_last_index[n];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
345 last_non_zero = i - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
346 for (; i <= last_index; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
347 j = s->intra_scantable.permutated[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
348 level = block[j];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
349 if (level) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
350 run = i - last_non_zero - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
351 last = (i == last_index);
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(level<0) level= -level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
354
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
355 code = get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
356 aic_code = get_rl_index(&rl_intra_aic, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
357 inter_vlc_bits += rl->table_vlc[code][1]+1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
358 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
359
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
360 if (code == rl->n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
361 inter_vlc_bits += 1+6+8-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
362 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
363 if (aic_code == rl_intra_aic.n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
364 aic_vlc_bits += 1+6+8-1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
365 wrong_pos += run + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
366 }else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
367 wrong_pos += wrong_run[aic_code];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
368 last_non_zero = i;
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
371 i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
372 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
373 rl = &rl_intra_aic;
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
376
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
377 /* AC coefs */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
378 last_index = s->block_last_index[n];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
379 last_non_zero = i - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
380 for (; i <= last_index; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
381 j = s->intra_scantable.permutated[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
382 level = block[j];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
383 if (level) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
384 run = i - last_non_zero - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
385 last = (i == last_index);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
386 sign = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
387 slevel = level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
388 if (level < 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
389 sign = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
390 level = -level;
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 code = get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
393 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
394 if (code == rl->n) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
395 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
396 put_bits(&s->pb, 1, last);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
397 put_bits(&s->pb, 6, run);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
398
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
399 assert(slevel != 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
400
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
401 if(level < 128)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
402 put_sbits(&s->pb, 8, slevel);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
403 else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
404 put_bits(&s->pb, 8, 128);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
405 put_sbits(&s->pb, 5, slevel);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
406 put_sbits(&s->pb, 6, slevel>>5);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
407 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
408 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
409 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
410 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
411 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
412 put_bits(&s->pb, 1, sign);
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 last_non_zero = i;
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
417 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
418
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
419 /* Encode MV differences on H.263+ with Unrestricted MV mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
420 static void h263p_encode_umotion(MpegEncContext * s, int val)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
421 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
422 short sval = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
423 short i = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
424 short n_bits = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
425 short temp_val;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
426 int code = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
427 int tcode;
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 if ( val == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
430 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
431 else if (val == 1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
432 put_bits(&s->pb, 3, 0);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
433 else if (val == -1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
434 put_bits(&s->pb, 3, 2);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
435 else {
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 sval = ((val < 0) ? (short)(-val):(short)val);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
438 temp_val = sval;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
439
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
440 while (temp_val != 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
441 temp_val = temp_val >> 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
442 n_bits++;
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
445 i = n_bits - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
446 while (i > 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
447 tcode = (sval & (1 << (i-1))) >> (i-1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
448 tcode = (tcode << 1) | 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
449 code = (code << 2) | tcode;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
450 i--;
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 code = ((code << 1) | (val < 0)) << 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
453 put_bits(&s->pb, (2*n_bits)+1, code);
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
456
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
457 void h263_encode_mb(MpegEncContext * s,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
458 DCTELEM block[6][64],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
459 int motion_x, int motion_y)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
460 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
461 int cbpc, cbpy, i, cbp, pred_x, pred_y;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
462 int16_t pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
463 int16_t rec_intradc[6];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
464 int16_t *dc_ptr[6];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
465 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
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 if (!s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
468 /* compute cbp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
469 cbp= get_p_cbp(s, block, motion_x, motion_y);
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 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
472 /* skip macroblock */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
473 put_bits(&s->pb, 1, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
474 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
475 s->misc_bits++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
476 s->last_bits++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
477 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
478 s->skip_count++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
479
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
480 return;
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 put_bits(&s->pb, 1, 0); /* mb coded */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
483
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
484 cbpc = cbp & 3;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
485 cbpy = cbp >> 2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
486 if(s->alt_inter_vlc==0 || cbpc!=3)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
487 cbpy ^= 0xF;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
488 if(s->dquant) cbpc+= 8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
489 if(s->mv_type==MV_TYPE_16X16){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
490 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
491 ff_h263_inter_MCBPC_bits[cbpc],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
492 ff_h263_inter_MCBPC_code[cbpc]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
493
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
494 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
495 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
496 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
497
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
498 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
499 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
500 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
501
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
502 /* motion vectors: 16x16 mode */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
503 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
504
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
505 if (!s->umvplus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
506 ff_h263_encode_motion_vector(s, motion_x - pred_x,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
507 motion_y - pred_y, 1);
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 else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
510 h263p_encode_umotion(s, motion_x - pred_x);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
511 h263p_encode_umotion(s, motion_y - pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
512 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
513 /* To prevent Start Code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
514 put_bits(&s->pb,1,1);
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 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
517 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
518 ff_h263_inter_MCBPC_bits[cbpc+16],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
519 ff_h263_inter_MCBPC_code[cbpc+16]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
520 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
521 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
522 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
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 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
525 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
526 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
527
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
528 for(i=0; i<4; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
529 /* motion vectors: 8x8 mode*/
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
530 h263_pred_motion(s, i, 0, &pred_x, &pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
531
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
532 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
533 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
534 if (!s->umvplus) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
535 ff_h263_encode_motion_vector(s, motion_x - pred_x,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
536 motion_y - pred_y, 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
537 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
538 else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
539 h263p_encode_umotion(s, motion_x - pred_x);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
540 h263p_encode_umotion(s, motion_y - pred_y);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
541 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
542 /* To prevent Start Code emulation */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
543 put_bits(&s->pb,1,1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
544 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
545 }
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
548 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
549 s->mv_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
550 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
551 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
552 assert(s->mb_intra);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
553
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
554 cbp = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
555 if (s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
556 /* Predict DC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
557 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
558 int16_t level = block[i][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
559 int scale;
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 if(i<4) scale= s->y_dc_scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
562 else scale= s->c_dc_scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
563
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
564 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
565 level -= pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
566 /* Quant */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
567 if (level >= 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
568 level = (level + (scale>>1))/scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
569 else
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
570 level = (level - (scale>>1))/scale;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
571
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
572 /* AIC can change CBP */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
573 if (level == 0 && s->block_last_index[i] == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
574 s->block_last_index[i] = -1;
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(!s->modified_quant){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
577 if (level < -127)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
578 level = -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
579 else if (level > 127)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
580 level = 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
581 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
582
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
583 block[i][0] = level;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
584 /* Reconstruction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
585 rec_intradc[i] = scale*level + pred_dc;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
586 /* Oddify */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
587 rec_intradc[i] |= 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
588 //if ((rec_intradc[i] % 2) == 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
589 // rec_intradc[i]++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
590 /* Clipping */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
591 if (rec_intradc[i] < 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
592 rec_intradc[i] = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
593 else if (rec_intradc[i] > 2047)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
594 rec_intradc[i] = 2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
595
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
596 /* Update AC/DC tables */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
597 *dc_ptr[i] = rec_intradc[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
598 if (s->block_last_index[i] >= 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
599 cbp |= 1 << (5 - i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
600 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
601 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
602 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
603 /* compute cbp */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
604 if (s->block_last_index[i] >= 1)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
605 cbp |= 1 << (5 - i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
606 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
607 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
608
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
609 cbpc = cbp & 3;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
610 if (s->pict_type == FF_I_TYPE) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
611 if(s->dquant) cbpc+=4;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
612 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
613 ff_h263_intra_MCBPC_bits[cbpc],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
614 ff_h263_intra_MCBPC_code[cbpc]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
615 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
616 if(s->dquant) cbpc+=8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
617 put_bits(&s->pb, 1, 0); /* mb coded */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
618 put_bits(&s->pb,
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
619 ff_h263_inter_MCBPC_bits[cbpc + 4],
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
620 ff_h263_inter_MCBPC_code[cbpc + 4]);
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 if (s->h263_aic) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
623 /* XXX: currently, we do not try to use ac prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
624 put_bits(&s->pb, 1, 0); /* no AC prediction */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
625 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
626 cbpy = cbp >> 2;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
627 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
628 if(s->dquant)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
629 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
630
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
631 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
632 s->misc_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
633 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
634 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
635
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
636 for(i=0; i<6; i++) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
637 /* encode each block */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
638 h263_encode_block(s, block[i], i);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
639
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
640 /* Update INTRADC for decoding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
641 if (s->h263_aic && s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
642 block[i][0] = rec_intradc[i];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
643
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
644 }
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
647 if(interleaved_stats){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
648 if (!s->mb_intra) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
649 s->p_tex_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
650 s->f_count++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
651 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
652 s->i_tex_bits+= get_bits_diff(s);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
653 s->i_count++;
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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
656 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
657
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
658 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
659 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
660 int range, l, bit_size, sign, code, bits;
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 (val == 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
663 /* zero vector */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
664 code = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
665 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
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 bit_size = f_code - 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
668 range = 1 << bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
669 /* modulo encoding */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
670 l= INT_BIT - 6 - bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
671 val = (val<<l)>>l;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
672 sign = val>>31;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
673 val= (val^sign)-sign;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
674 sign&=1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
675
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
676 val--;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
677 code = (val >> bit_size) + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
678 bits = val & (range - 1);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
679
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
680 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
681 if (bit_size > 0) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
682 put_bits(&s->pb, bit_size, bits);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
683 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
684 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
685 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
686
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
687 static void init_mv_penalty_and_fcode(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
688 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
689 int f_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
690 int mv;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
691
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
692 for(f_code=1; f_code<=MAX_FCODE; f_code++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
693 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
694 int len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
695
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
696 if(mv==0) len= mvtab[0][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
697 else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
698 int val, bit_size, code;
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 bit_size = f_code - 1;
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 val=mv;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
703 if (val < 0)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
704 val = -val;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
705 val--;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
706 code = (val >> bit_size) + 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
707 if(code<33){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
708 len= mvtab[code][1] + 1 + bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
709 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
710 len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
711 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
712 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
713
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
714 mv_penalty[f_code][mv+MAX_MV]= len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
715 }
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
718 for(f_code=MAX_FCODE; f_code>0; f_code--){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
719 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
720 fcode_tab[mv+MAX_MV]= f_code;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
721 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
722 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
723
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
724 for(mv=0; mv<MAX_MV*2+1; mv++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
725 umv_fcode_tab[mv]= 1;
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 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
730 int slevel, run, last;
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 assert(MAX_LEVEL >= 64);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
733 assert(MAX_RUN >= 63);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
734
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
735 for(slevel=-64; slevel<64; slevel++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
736 if(slevel==0) continue;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
737 for(run=0; run<64; run++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
738 for(last=0; last<=1; last++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
739 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
740 int level= slevel < 0 ? -slevel : slevel;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
741 int sign= slevel < 0 ? 1 : 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
742 int bits, len, code;
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 len_tab[index]= 100;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
745
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
746 /* ESC0 */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
747 code= get_rl_index(rl, last, run, level);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
748 bits= rl->table_vlc[code][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
749 len= rl->table_vlc[code][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
750 bits=bits*2+sign; len++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
751
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
752 if(code!=rl->n && len < len_tab[index]){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
753 if(bits_tab) bits_tab[index]= bits;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
754 len_tab [index]= len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
755 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
756 /* ESC */
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
757 bits= rl->table_vlc[rl->n][0];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
758 len = rl->table_vlc[rl->n][1];
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
759 bits=bits*2+last; len++;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
760 bits=bits*64+run; len+=6;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
761 bits=bits*256+(level&0xff); len+=8;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
762
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
763 if(len < len_tab[index]){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
764 if(bits_tab) bits_tab[index]= bits;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
765 len_tab [index]= 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 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
768 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
769 }
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
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
772 void h263_encode_init(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
773 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
774 static int done = 0;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
775
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
776 if (!done) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
777 done = 1;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
778
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
779 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
780 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
781
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
782 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
783 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
784
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
785 init_mv_penalty_and_fcode(s);
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 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
788
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
789 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
790 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
791 if(s->h263_aic){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
792 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
793 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
794 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
795 s->ac_esc_length= 7+1+6+8;
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 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
798 switch(s->codec_id){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
799 case CODEC_ID_MPEG4:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
800 s->fcode_tab= fcode_tab;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
801 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
802 case CODEC_ID_H263P:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
803 if(s->umvplus)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
804 s->fcode_tab= umv_fcode_tab;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
805 if(s->modified_quant){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
806 s->min_qcoeff= -2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
807 s->max_qcoeff= 2047;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
808 }else{
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
809 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
810 s->max_qcoeff= 127;
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 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
813 //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
814 case CODEC_ID_FLV1:
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
815 if (s->h263_flv > 1) {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
816 s->min_qcoeff= -1023;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
817 s->max_qcoeff= 1023;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
818 } else {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
819 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
820 s->max_qcoeff= 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
821 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
822 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
823 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
824 break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
825 default: //nothing needed - default table already set in mpegvideo.c
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
826 s->min_qcoeff= -127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
827 s->max_qcoeff= 127;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
828 s->y_dc_scale_table=
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
829 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
830 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
831 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
832
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
833 void ff_h263_encode_mba(MpegEncContext *s)
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
834 {
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
835 int i, mb_pos;
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 for(i=0; i<6; i++){
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
838 if(s->mb_num-1 <= ff_mba_max[i]) break;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
839 }
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
840 mb_pos= s->mb_x + s->mb_width*s->mb_y;
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
841 put_bits(&s->pb, ff_mba_length[i], mb_pos);
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents:
diff changeset
842 }