annotate ituh263enc.c @ 10838:95dac0e4703b libavcodec

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