Mercurial > libavcodec.hg
annotate mpeg12.c @ 1162:8c15d82c1893 libavcodec
some static -> dynamic alloc & 16->8 bit
author | michaelni |
---|---|
date | Mon, 31 Mar 2003 11:13:14 +0000 |
parents | 9a5c29575902 |
children | 0e3c0c4a7b3d |
rev | line source |
---|---|
0 | 1 /* |
1106 | 2 * MPEG1 codec / MPEG2 decoder |
429 | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
0 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file mpeg12.c | |
22 * MPEG1 codec / MPEG2 decoder. | |
23 */ | |
24 | |
76 | 25 //#define DEBUG |
0 | 26 #include "avcodec.h" |
27 #include "dsputil.h" | |
28 #include "mpegvideo.h" | |
29 | |
30 #include "mpeg12data.h" | |
31 | |
694 | 32 #if 1 |
33 #define PRINT_QP(a, b) {} | |
34 #else | |
35 #define PRINT_QP(a, b) printf(a, b) | |
36 #endif | |
37 | |
0 | 38 /* Start codes. */ |
39 #define SEQ_END_CODE 0x000001b7 | |
40 #define SEQ_START_CODE 0x000001b3 | |
41 #define GOP_START_CODE 0x000001b8 | |
42 #define PICTURE_START_CODE 0x00000100 | |
43 #define SLICE_MIN_START_CODE 0x00000101 | |
44 #define SLICE_MAX_START_CODE 0x000001af | |
45 #define EXT_START_CODE 0x000001b5 | |
46 #define USER_START_CODE 0x000001b2 | |
47 | |
552 | 48 #define DC_VLC_BITS 9 |
49 #define MV_VLC_BITS 9 | |
50 #define MBINCR_VLC_BITS 9 | |
51 #define MB_PAT_VLC_BITS 9 | |
52 #define MB_PTYPE_VLC_BITS 6 | |
53 #define MB_BTYPE_VLC_BITS 6 | |
54 #define TEX_VLC_BITS 9 | |
55 | |
0 | 56 static void mpeg1_encode_block(MpegEncContext *s, |
57 DCTELEM *block, | |
58 int component); | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
59 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added |
0 | 60 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); |
711 | 61 static inline int mpeg1_decode_block_inter(MpegEncContext *s, |
62 DCTELEM *block, | |
63 int n); | |
64 static inline int mpeg1_decode_block_intra(MpegEncContext *s, | |
0 | 65 DCTELEM *block, |
66 int n); | |
714 | 67 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
0 | 68 DCTELEM *block, |
69 int n); | |
714 | 70 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
0 | 71 DCTELEM *block, |
72 int n); | |
73 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); | |
74 | |
947 | 75 #ifdef CONFIG_ENCODERS |
1162 | 76 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; |
1064 | 77 static uint8_t fcode_tab[MAX_MV*2+1]; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
78 |
947 | 79 static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2]; |
80 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2]; | |
81 #endif | |
82 | |
740 | 83 static inline int get_bits_diff(MpegEncContext *s){ |
84 int bits,ret; | |
85 | |
86 bits= get_bit_count(&s->pb); | |
87 ret= bits - s->last_bits; | |
88 s->last_bits=bits; | |
89 | |
90 return ret; | |
91 } | |
92 | |
552 | 93 static void init_2d_vlc_rl(RLTable *rl) |
94 { | |
620
a5aa53b6e648
warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michaelni
parents:
617
diff
changeset
|
95 int i; |
552 | 96 |
97 init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, | |
98 &rl->table_vlc[0][1], 4, 2, | |
99 &rl->table_vlc[0][0], 4, 2); | |
100 | |
101 | |
102 rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); | |
103 for(i=0; i<rl->vlc.table_size; i++){ | |
104 int code= rl->vlc.table[i][0]; | |
105 int len = rl->vlc.table[i][1]; | |
106 int level, run; | |
107 | |
108 if(len==0){ // illegal code | |
109 run= 65; | |
110 level= MAX_LEVEL; | |
111 }else if(len<0){ //more bits needed | |
112 run= 0; | |
113 level= code; | |
114 }else{ | |
115 if(code==rl->n){ //esc | |
116 run= 65; | |
117 level= 0; | |
118 }else if(code==rl->n+1){ //eob | |
711 | 119 run= 0; |
120 level= 127; | |
552 | 121 }else{ |
122 run= rl->table_run [code] + 1; | |
123 level= rl->table_level[code]; | |
124 } | |
125 } | |
126 rl->rl_vlc[0][i].len= len; | |
127 rl->rl_vlc[0][i].level= level; | |
128 rl->rl_vlc[0][i].run= run; | |
129 } | |
130 } | |
131 | |
947 | 132 static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){ |
133 int i; | |
134 | |
135 for(i=0; i<128; i++){ | |
136 int level= i-64; | |
137 int run; | |
138 for(run=0; run<64; run++){ | |
139 int len, bits, code; | |
140 | |
141 int alevel= ABS(level); | |
142 int sign= (level>>31)&1; | |
143 | |
144 if (alevel > rl->max_level[0][run]) | |
145 code= 111; /*rl->n*/ | |
146 else | |
147 code= rl->index_run[0][run] + alevel - 1; | |
148 | |
149 if (code < 111 /* rl->n */) { | |
150 /* store the vlc & sign at once */ | |
151 len= mpeg1_vlc[code][1]+1; | |
152 bits= (mpeg1_vlc[code][0]<<1) + sign; | |
153 } else { | |
154 len= mpeg1_vlc[111/*rl->n*/][1]+6; | |
155 bits= mpeg1_vlc[111/*rl->n*/][0]<<6; | |
156 | |
157 bits|= run; | |
158 if (alevel < 128) { | |
159 bits<<=8; len+=8; | |
160 bits|= level & 0xff; | |
161 } else { | |
162 bits<<=16; len+=16; | |
163 bits|= level & 0xff; | |
164 if (level < 0) { | |
165 bits|= 0x8001 + level + 255; | |
166 } else { | |
167 bits|= level & 0xffff; | |
168 } | |
169 } | |
170 } | |
171 | |
172 uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits; | |
173 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len; | |
174 } | |
175 } | |
176 } | |
552 | 177 |
0 | 178 static void put_header(MpegEncContext *s, int header) |
179 { | |
180 align_put_bits(&s->pb); | |
236 | 181 put_bits(&s->pb, 16, header>>16); |
182 put_bits(&s->pb, 16, header&0xFFFF); | |
0 | 183 } |
184 | |
185 /* put sequence header if needed */ | |
186 static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
187 { | |
188 unsigned int vbv_buffer_size; | |
1 | 189 unsigned int fps, v; |
918 | 190 int n, i; |
1064 | 191 uint64_t time_code; |
918 | 192 float best_aspect_error= 1E10; |
193 float aspect_ratio= s->avctx->aspect_ratio; | |
194 | |
195 if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA) | |
0 | 196 |
903 | 197 if (s->current_picture.key_frame) { |
0 | 198 /* mpeg1 header repeated every gop */ |
199 put_header(s, SEQ_START_CODE); | |
200 | |
201 /* search closest frame rate */ | |
202 { | |
203 int i, dmin, d; | |
204 s->frame_rate_index = 0; | |
205 dmin = 0x7fffffff; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
206 for(i=1;i<14;i++) { |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
207 if(s->avctx->strict_std_compliance >= 0 && i>=9) break; |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
208 |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
209 d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]); |
0 | 210 if (d < dmin) { |
211 dmin = d; | |
212 s->frame_rate_index = i; | |
213 } | |
214 } | |
215 } | |
216 | |
217 put_bits(&s->pb, 12, s->width); | |
218 put_bits(&s->pb, 12, s->height); | |
918 | 219 |
220 for(i=1; i<15; i++){ | |
221 float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio); | |
222 error= ABS(error); | |
223 | |
224 if(error < best_aspect_error){ | |
225 best_aspect_error= error; | |
226 s->aspect_ratio_info= i; | |
227 } | |
228 } | |
229 | |
230 put_bits(&s->pb, 4, s->aspect_ratio_info); | |
0 | 231 put_bits(&s->pb, 4, s->frame_rate_index); |
1138 | 232 v = (s->bit_rate + 399) / 400; |
0 | 233 if (v > 0x3ffff) |
234 v = 0x3ffff; | |
235 put_bits(&s->pb, 18, v); | |
236 put_bits(&s->pb, 1, 1); /* marker */ | |
639
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
237 |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
238 if(s->avctx->rc_buffer_size) |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
239 vbv_buffer_size = s->avctx->rc_buffer_size; |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
240 else |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
241 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */ |
f449913e8419
new vbv calculation patch by (Henry Mason <talus25 at speakeasy dot net>) with slight modification by me
michaelni
parents:
620
diff
changeset
|
242 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; |
0 | 243 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); |
244 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ | |
245 put_bits(&s->pb, 1, 0); /* no custom intra matrix */ | |
246 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */ | |
247 | |
248 put_header(s, GOP_START_CODE); | |
249 put_bits(&s->pb, 1, 0); /* do drop frame */ | |
250 /* time code : we must convert from the real frame rate to a | |
251 fake mpeg frame rate in case of low frame rate */ | |
252 fps = frame_rate_tab[s->frame_rate_index]; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
253 time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE; |
0 | 254 s->gop_picture_number = s->fake_picture_number; |
1064 | 255 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); |
256 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); | |
0 | 257 put_bits(&s->pb, 1, 1); |
1064 | 258 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
259 put_bits(&s->pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE)); |
0 | 260 put_bits(&s->pb, 1, 1); /* closed gop */ |
261 put_bits(&s->pb, 1, 0); /* broken link */ | |
262 } | |
263 | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
264 if (s->avctx->frame_rate < (24 * s->avctx->frame_rate_base) && s->picture_number > 0) { |
0 | 265 /* insert empty P pictures to slow down to the desired |
266 frame rate. Each fake pictures takes about 20 bytes */ | |
267 fps = frame_rate_tab[s->frame_rate_index]; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
268 n = av_rescale((int64_t)s->picture_number * s->avctx->frame_rate_base, fps, s->avctx->frame_rate) / MPEG1_FRAME_RATE_BASE - 1; |
0 | 269 while (s->fake_picture_number < n) { |
270 mpeg1_skip_picture(s, s->fake_picture_number - | |
271 s->gop_picture_number); | |
272 s->fake_picture_number++; | |
273 } | |
274 | |
275 } | |
276 } | |
277 | |
1160 | 278 static inline void encode_mb_skip_run(MpegEncContext *s, int run){ |
279 while (run >= 33) { | |
280 put_bits(&s->pb, 11, 0x008); | |
281 run -= 33; | |
282 } | |
283 put_bits(&s->pb, mbAddrIncrTable[run][1], | |
284 mbAddrIncrTable[run][0]); | |
285 } | |
0 | 286 |
287 /* insert a fake P picture */ | |
288 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) | |
289 { | |
290 /* mpeg1 picture header */ | |
291 put_header(s, PICTURE_START_CODE); | |
292 /* temporal reference */ | |
293 put_bits(&s->pb, 10, pict_num & 0x3ff); | |
294 | |
295 put_bits(&s->pb, 3, P_TYPE); | |
296 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
297 | |
298 put_bits(&s->pb, 1, 1); /* integer coordinates */ | |
299 put_bits(&s->pb, 3, 1); /* forward_f_code */ | |
300 | |
301 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
302 | |
303 /* only one slice */ | |
304 put_header(s, SLICE_MIN_START_CODE); | |
305 put_bits(&s->pb, 5, 1); /* quantizer scale */ | |
306 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
307 | |
1160 | 308 encode_mb_skip_run(s, 0); |
0 | 309 |
310 /* empty macroblock */ | |
311 put_bits(&s->pb, 3, 1); /* motion only */ | |
312 | |
313 /* zero motion x & y */ | |
314 put_bits(&s->pb, 1, 1); | |
315 put_bits(&s->pb, 1, 1); | |
316 | |
317 /* output a number of empty slice */ | |
1160 | 318 encode_mb_skip_run(s, s->mb_width * s->mb_height - 2); |
0 | 319 |
320 /* empty macroblock */ | |
321 put_bits(&s->pb, 3, 1); /* motion only */ | |
322 | |
323 /* zero motion x & y */ | |
324 put_bits(&s->pb, 1, 1); | |
325 put_bits(&s->pb, 1, 1); | |
326 } | |
327 | |
498 | 328 static void common_init(MpegEncContext *s) |
329 { | |
330 s->y_dc_scale_table= | |
331 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
332 } | |
333 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
334 #ifdef CONFIG_ENCODERS |
1160 | 335 |
336 void ff_mpeg1_encode_slice_header(MpegEncContext *s){ | |
337 put_header(s, SLICE_MIN_START_CODE + s->mb_y); | |
338 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */ | |
339 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
340 } | |
341 | |
342 void ff_mpeg1_clean_buffers(MpegEncContext *s){ | |
343 s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
344 s->last_dc[1] = s->last_dc[0]; | |
345 s->last_dc[2] = s->last_dc[0]; | |
346 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
347 } | |
348 | |
0 | 349 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) |
350 { | |
351 mpeg1_encode_sequence_header(s); | |
352 | |
353 /* mpeg1 picture header */ | |
354 put_header(s, PICTURE_START_CODE); | |
355 /* temporal reference */ | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
356 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
357 // RAL: s->picture_number instead of s->fake_picture_number |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
358 put_bits(&s->pb, 10, (s->picture_number - |
0 | 359 s->gop_picture_number) & 0x3ff); |
276
1e2f9ef286d4
- Fix pts calculation on mpeg mux (A/V sync) - Thanks to Lennert Buytenhek
pulento
parents:
275
diff
changeset
|
360 s->fake_picture_number++; |
0 | 361 |
362 put_bits(&s->pb, 3, s->pict_type); | |
363 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
364 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
365 // RAL: Forward f_code also needed for B frames |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
366 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
0 | 367 put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
368 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
369 } | |
370 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
371 // RAL: Backward f_code necessary for B frames |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
372 if (s->pict_type == B_TYPE) { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
373 put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
374 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
375 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
376 |
0 | 377 put_bits(&s->pb, 1, 0); /* extra bit picture */ |
378 | |
1160 | 379 s->mb_y=0; |
380 ff_mpeg1_encode_slice_header(s); | |
0 | 381 } |
382 | |
383 void mpeg1_encode_mb(MpegEncContext *s, | |
384 DCTELEM block[6][64], | |
385 int motion_x, int motion_y) | |
386 { | |
1160 | 387 int i, cbp; |
388 const int mb_x = s->mb_x; | |
389 const int mb_y = s->mb_y; | |
390 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; | |
0 | 391 |
392 /* compute cbp */ | |
393 cbp = 0; | |
394 for(i=0;i<6;i++) { | |
395 if (s->block_last_index[i] >= 0) | |
396 cbp |= 1 << (5 - i); | |
397 } | |
398 | |
1160 | 399 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) && |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
400 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) || |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
401 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
402 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
1160 | 403 s->mb_skip_run++; |
694 | 404 s->qscale -= s->dquant; |
740 | 405 s->skip_count++; |
406 s->misc_bits++; | |
407 s->last_bits++; | |
0 | 408 } else { |
1160 | 409 if(first_mb){ |
410 assert(s->mb_skip_run == 0); | |
411 encode_mb_skip_run(s, s->mb_x); | |
412 }else{ | |
413 encode_mb_skip_run(s, s->mb_skip_run); | |
0 | 414 } |
415 | |
416 if (s->pict_type == I_TYPE) { | |
694 | 417 if(s->dquant && cbp){ |
418 put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */ | |
419 put_bits(&s->pb, 5, s->qscale); | |
420 }else{ | |
421 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ | |
422 s->qscale -= s->dquant; | |
423 } | |
740 | 424 s->misc_bits+= get_bits_diff(s); |
425 s->i_count++; | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
426 } else if (s->mb_intra) { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
427 if(s->dquant && cbp){ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
428 put_bits(&s->pb, 6, 0x01); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
429 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
430 }else{ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
431 put_bits(&s->pb, 5, 0x03); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
432 s->qscale -= s->dquant; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
433 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
434 s->misc_bits+= get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
435 s->i_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
436 s->last_mv[0][0][0] = |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
437 s->last_mv[0][0][1] = 0; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
438 } else if (s->pict_type == P_TYPE) { |
0 | 439 if (cbp != 0) { |
440 if (motion_x == 0 && motion_y == 0) { | |
694 | 441 if(s->dquant){ |
442 put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */ | |
443 put_bits(&s->pb, 5, s->qscale); | |
444 }else{ | |
445 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */ | |
446 } | |
740 | 447 s->misc_bits+= get_bits_diff(s); |
0 | 448 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
449 } else { | |
694 | 450 if(s->dquant){ |
451 put_bits(&s->pb, 5, 2); /* motion + cbp */ | |
452 put_bits(&s->pb, 5, s->qscale); | |
453 }else{ | |
454 put_bits(&s->pb, 1, 1); /* motion + cbp */ | |
455 } | |
740 | 456 s->misc_bits+= get_bits_diff(s); |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
457 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
458 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added |
740 | 459 s->mv_bits+= get_bits_diff(s); |
0 | 460 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
461 } | |
462 } else { | |
463 put_bits(&s->pb, 3, 1); /* motion only */ | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
464 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
465 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added |
694 | 466 s->qscale -= s->dquant; |
740 | 467 s->mv_bits+= get_bits_diff(s); |
0 | 468 } |
740 | 469 s->f_count++; |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
470 } else |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
471 { // RAL: All the following bloc added for B frames: |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
472 if (cbp != 0) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
473 { // With coded bloc pattern |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
474 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
475 { // Bi-directional motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
476 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
477 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
478 put_bits(&s->pb, 5, 2); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
479 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
480 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
481 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
482 put_bits(&s->pb, 2, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
483 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
484 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
485 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
486 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
487 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
488 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
489 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
490 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
491 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
492 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
493 else if (s->mv_dir == MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
494 { // Backward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
495 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
496 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
497 put_bits(&s->pb, 6, 2); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
498 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
499 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
500 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
501 put_bits(&s->pb, 3, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
502 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
503 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
504 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
505 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
506 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
507 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
508 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
509 else if (s->mv_dir == MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
510 { // Forward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
511 if (s->dquant) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
512 { // With QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
513 put_bits(&s->pb, 6, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
514 put_bits(&s->pb, 5, s->qscale); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
515 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
516 else // Without QScale |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
517 put_bits(&s->pb, 4, 3); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
518 s->misc_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
519 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
520 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
521 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
522 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
523 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
524 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
525 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
526 else |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
527 { // No coded bloc pattern |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
528 if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
529 { // Bi-directional motion |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
530 put_bits(&s->pb, 2, 2); /* backward & forward motion */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
531 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
532 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
533 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
534 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
535 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
536 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
537 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
538 else if (s->mv_dir == MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
539 { // Backward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
540 put_bits(&s->pb, 3, 2); /* backward motion only */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
541 mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
542 mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
543 s->b_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
544 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
545 else if (s->mv_dir == MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
546 { // Forward motion |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
547 put_bits(&s->pb, 4, 2); /* forward motion only */ |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
548 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
549 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
550 s->f_count++; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
551 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
552 s->qscale -= s->dquant; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
553 s->mv_bits += get_bits_diff(s); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
554 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
555 // End of bloc from RAL |
0 | 556 } |
557 for(i=0;i<6;i++) { | |
558 if (cbp & (1 << (5 - i))) { | |
559 mpeg1_encode_block(s, block[i], i); | |
560 } | |
561 } | |
1160 | 562 s->mb_skip_run = 0; |
740 | 563 if(s->mb_intra) |
564 s->i_tex_bits+= get_bits_diff(s); | |
565 else | |
566 s->p_tex_bits+= get_bits_diff(s); | |
0 | 567 } |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
568 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
569 // RAL: By this: |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
570 if (s->mv_dir & MV_DIR_FORWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
571 { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
572 s->last_mv[0][0][0]= s->mv[0][0][0]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
573 s->last_mv[0][0][1]= s->mv[0][0][1]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
574 } |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
575 if (s->mv_dir & MV_DIR_BACKWARD) |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
576 { |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
577 s->last_mv[1][0][0]= s->mv[1][0][0]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
578 s->last_mv[1][0][1]= s->mv[1][0][1]; |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
579 } |
0 | 580 } |
581 | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
582 // RAL: Parameter added: f_or_b_code |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
583 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) |
0 | 584 { |
585 int code, bit_size, l, m, bits, range, sign; | |
586 | |
587 if (val == 0) { | |
588 /* zero vector */ | |
589 code = 0; | |
590 put_bits(&s->pb, | |
591 mbMotionVectorTable[0][1], | |
592 mbMotionVectorTable[0][0]); | |
593 } else { | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
594 bit_size = f_or_b_code - 1; |
0 | 595 range = 1 << bit_size; |
596 /* modulo encoding */ | |
597 l = 16 * range; | |
598 m = 2 * l; | |
599 if (val < -l) { | |
600 val += m; | |
601 } else if (val >= l) { | |
602 val -= m; | |
603 } | |
604 | |
605 if (val >= 0) { | |
606 val--; | |
607 code = (val >> bit_size) + 1; | |
608 bits = val & (range - 1); | |
609 sign = 0; | |
610 } else { | |
611 val = -val; | |
612 val--; | |
613 code = (val >> bit_size) + 1; | |
614 bits = val & (range - 1); | |
615 sign = 1; | |
616 } | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
617 |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
618 assert(code > 0 && code <= 16); |
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
619 |
0 | 620 put_bits(&s->pb, |
621 mbMotionVectorTable[code][1], | |
622 mbMotionVectorTable[code][0]); | |
1051
e5a9dbf597d4
mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
michaelni
parents:
1025
diff
changeset
|
623 |
0 | 624 put_bits(&s->pb, 1, sign); |
625 if (bit_size > 0) { | |
626 put_bits(&s->pb, bit_size, bits); | |
627 } | |
628 } | |
629 } | |
630 | |
498 | 631 void ff_mpeg1_encode_init(MpegEncContext *s) |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
632 { |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
633 static int done=0; |
498 | 634 |
635 common_init(s); | |
636 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
637 if(!done){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
638 int f_code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
639 int mv; |
498 | 640 int i; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
641 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
642 done=1; |
498 | 643 init_rl(&rl_mpeg1); |
947 | 644 |
498 | 645 for(i=0; i<64; i++) |
646 { | |
647 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i]; | |
648 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i]; | |
649 } | |
947 | 650 |
651 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len); | |
498 | 652 |
653 /* build unified dc encoding tables */ | |
654 for(i=-255; i<256; i++) | |
655 { | |
656 int adiff, index; | |
657 int bits, code; | |
658 int diff=i; | |
659 | |
660 adiff = ABS(diff); | |
661 if(diff<0) diff--; | |
662 index = vlc_dc_table[adiff]; | |
663 | |
664 bits= vlc_dc_lum_bits[index] + index; | |
665 code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)); | |
666 mpeg1_lum_dc_uni[i+255]= bits + (code<<8); | |
667 | |
668 bits= vlc_dc_chroma_bits[index] + index; | |
669 code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)); | |
670 mpeg1_chr_dc_uni[i+255]= bits + (code<<8); | |
671 } | |
672 | |
1162 | 673 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); |
674 | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
675 for(f_code=1; f_code<=MAX_FCODE; f_code++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
676 for(mv=-MAX_MV; mv<=MAX_MV; mv++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
677 int len; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
678 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
679 if(mv==0) len= mbMotionVectorTable[0][1]; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
680 else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
681 int val, bit_size, range, code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
682 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
683 bit_size = s->f_code - 1; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
684 range = 1 << bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
685 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
686 val=mv; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
687 if (val < 0) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
688 val = -val; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
689 val--; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
690 code = (val >> bit_size) + 1; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
691 if(code<17){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
692 len= mbMotionVectorTable[code][1] + 1 + bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
693 }else{ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
694 len= mbMotionVectorTable[16][1] + 2 + bit_size; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
695 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
696 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
697 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
698 mv_penalty[f_code][mv+MAX_MV]= len; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
699 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
700 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
701 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
702 |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
703 for(f_code=MAX_FCODE; f_code>0; f_code--){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
704 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){ |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
705 fcode_tab[mv+MAX_MV]= f_code; |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
706 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
707 } |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
708 } |
936 | 709 s->me.mv_penalty= mv_penalty; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
710 s->fcode_tab= fcode_tab; |
344 | 711 s->min_qcoeff=-255; |
712 s->max_qcoeff= 255; | |
947 | 713 s->intra_ac_vlc_length= |
714 s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len; | |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
277
diff
changeset
|
715 } |
498 | 716 |
0 | 717 static inline void encode_dc(MpegEncContext *s, int diff, int component) |
718 { | |
719 if (component == 0) { | |
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
720 put_bits( |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
721 &s->pb, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
722 mpeg1_lum_dc_uni[diff+255]&0xFF, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
723 mpeg1_lum_dc_uni[diff+255]>>8); |
0 | 724 } else { |
237
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
725 put_bits( |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
726 &s->pb, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
727 mpeg1_chr_dc_uni[diff+255]&0xFF, |
75123d30f862
optimized encode_dc() (+2% speed on P3 for mpeg1 intra only encodings)
michaelni
parents:
236
diff
changeset
|
728 mpeg1_chr_dc_uni[diff+255]>>8); |
0 | 729 } |
730 } | |
731 | |
732 static void mpeg1_encode_block(MpegEncContext *s, | |
733 DCTELEM *block, | |
734 int n) | |
735 { | |
736 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | |
737 int code, component; | |
236 | 738 // RLTable *rl = &rl_mpeg1; |
0 | 739 |
740 last_index = s->block_last_index[n]; | |
741 | |
742 /* DC coef */ | |
743 if (s->mb_intra) { | |
744 component = (n <= 3 ? 0 : n - 4 + 1); | |
745 dc = block[0]; /* overflow is impossible */ | |
746 diff = dc - s->last_dc[component]; | |
747 encode_dc(s, diff, component); | |
748 s->last_dc[component] = dc; | |
749 i = 1; | |
750 } else { | |
751 /* encode the first coefficient : needs to be done here because | |
752 it is handled slightly differently */ | |
753 level = block[0]; | |
754 if (abs(level) == 1) { | |
1064 | 755 code = ((uint32_t)level >> 31); /* the sign bit */ |
0 | 756 put_bits(&s->pb, 2, code | 0x02); |
757 i = 1; | |
758 } else { | |
759 i = 0; | |
760 last_non_zero = -1; | |
761 goto next_coef; | |
762 } | |
763 } | |
764 | |
765 /* now quantify & encode AC coefs */ | |
766 last_non_zero = i - 1; | |
236 | 767 |
0 | 768 for(;i<=last_index;i++) { |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
769 j = s->intra_scantable.permutated[i]; |
0 | 770 level = block[j]; |
771 next_coef: | |
772 #if 0 | |
773 if (level != 0) | |
774 dprintf("level[%d]=%d\n", i, level); | |
775 #endif | |
776 /* encode using VLC */ | |
777 if (level != 0) { | |
778 run = i - last_non_zero - 1; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
779 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
780 alevel= level; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
781 MASK_ABS(sign, alevel) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
782 sign&=1; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
783 |
236 | 784 // code = get_rl_index(rl, 0, run, alevel); |
947 | 785 if (alevel <= mpeg1_max_level[0][run]){ |
236 | 786 code= mpeg1_index_run[0][run] + alevel - 1; |
787 /* store the vlc & sign at once */ | |
788 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); | |
0 | 789 } else { |
236 | 790 /* escape seems to be pretty rare <5% so i dont optimize it */ |
791 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); | |
0 | 792 /* escape: only clip in this case */ |
793 put_bits(&s->pb, 6, run); | |
794 if (alevel < 128) { | |
795 put_bits(&s->pb, 8, level & 0xff); | |
796 } else { | |
797 if (level < 0) { | |
798 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
799 } else { | |
800 put_bits(&s->pb, 16, level & 0xffff); | |
801 } | |
802 } | |
803 } | |
804 last_non_zero = i; | |
805 } | |
806 } | |
807 /* end of block */ | |
808 put_bits(&s->pb, 2, 0x2); | |
809 } | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
810 #endif //CONFIG_ENCODERS |
0 | 811 |
812 /******************************************/ | |
813 /* decoding */ | |
814 | |
815 static VLC dc_lum_vlc; | |
816 static VLC dc_chroma_vlc; | |
817 static VLC mv_vlc; | |
818 static VLC mbincr_vlc; | |
819 static VLC mb_ptype_vlc; | |
820 static VLC mb_btype_vlc; | |
821 static VLC mb_pat_vlc; | |
822 | |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
823 static void init_vlcs(MpegEncContext *s) |
0 | 824 { |
825 static int done = 0; | |
826 | |
827 if (!done) { | |
494 | 828 done = 1; |
0 | 829 |
568 | 830 init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, |
0 | 831 vlc_dc_lum_bits, 1, 1, |
832 vlc_dc_lum_code, 2, 2); | |
568 | 833 init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12, |
0 | 834 vlc_dc_chroma_bits, 1, 1, |
835 vlc_dc_chroma_code, 2, 2); | |
545 | 836 init_vlc(&mv_vlc, MV_VLC_BITS, 17, |
0 | 837 &mbMotionVectorTable[0][1], 2, 1, |
838 &mbMotionVectorTable[0][0], 2, 1); | |
545 | 839 init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35, |
0 | 840 &mbAddrIncrTable[0][1], 2, 1, |
841 &mbAddrIncrTable[0][0], 2, 1); | |
545 | 842 init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63, |
0 | 843 &mbPatTable[0][1], 2, 1, |
844 &mbPatTable[0][0], 2, 1); | |
845 | |
545 | 846 init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, |
0 | 847 &table_mb_ptype[0][1], 2, 1, |
848 &table_mb_ptype[0][0], 2, 1); | |
545 | 849 init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, |
0 | 850 &table_mb_btype[0][1], 2, 1, |
851 &table_mb_btype[0][0], 2, 1); | |
852 init_rl(&rl_mpeg1); | |
853 init_rl(&rl_mpeg2); | |
552 | 854 |
855 init_2d_vlc_rl(&rl_mpeg1); | |
856 init_2d_vlc_rl(&rl_mpeg2); | |
0 | 857 } |
858 } | |
859 | |
860 static inline int get_dmv(MpegEncContext *s) | |
861 { | |
21 | 862 if(get_bits1(&s->gb)) |
863 return 1 - (get_bits1(&s->gb) << 1); | |
0 | 864 else |
865 return 0; | |
866 } | |
867 | |
54 | 868 static inline int get_qscale(MpegEncContext *s) |
869 { | |
870 int qscale; | |
871 if (s->mpeg2) { | |
872 if (s->q_scale_type) { | |
873 qscale = non_linear_qscale[get_bits(&s->gb, 5)]; | |
874 } else { | |
875 qscale = get_bits(&s->gb, 5) << 1; | |
876 } | |
877 } else { | |
878 /* for mpeg1, we use the generic unquant code */ | |
879 qscale = get_bits(&s->gb, 5); | |
880 } | |
881 return qscale; | |
882 } | |
883 | |
0 | 884 /* motion type (for mpeg2) */ |
885 #define MT_FIELD 1 | |
886 #define MT_FRAME 2 | |
887 #define MT_16X8 2 | |
888 #define MT_DMV 3 | |
889 | |
890 static int mpeg_decode_mb(MpegEncContext *s, | |
891 DCTELEM block[6][64]) | |
892 { | |
751 | 893 int i, j, k, cbp, val, mb_type, motion_type; |
0 | 894 |
895 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y); | |
896 | |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
897 assert(s->mb_skiped==0); |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
898 |
1160 | 899 if (s->mb_skip_run-- != 0) { |
0 | 900 /* skip mb */ |
901 s->mb_intra = 0; | |
902 for(i=0;i<6;i++) | |
903 s->block_last_index[i] = -1; | |
904 s->mv_type = MV_TYPE_16X16; | |
905 if (s->pict_type == P_TYPE) { | |
906 /* if P type, zero motion vector is implied */ | |
907 s->mv_dir = MV_DIR_FORWARD; | |
908 s->mv[0][0][0] = s->mv[0][0][1] = 0; | |
909 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0; | |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
910 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0; |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
911 s->mb_skiped = 1; |
0 | 912 } else { |
913 /* if B type, reuse previous vectors and directions */ | |
914 s->mv[0][0][0] = s->last_mv[0][0][0]; | |
915 s->mv[0][0][1] = s->last_mv[0][0][1]; | |
916 s->mv[1][0][0] = s->last_mv[1][0][0]; | |
917 s->mv[1][0][1] = s->last_mv[1][0][1]; | |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
918 |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
919 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) |
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
920 s->mb_skiped = 1; |
0 | 921 } |
930 | 922 |
0 | 923 return 0; |
924 } | |
925 | |
926 switch(s->pict_type) { | |
927 default: | |
928 case I_TYPE: | |
21 | 929 if (get_bits1(&s->gb) == 0) { |
930 if (get_bits1(&s->gb) == 0) | |
0 | 931 return -1; |
932 mb_type = MB_QUANT | MB_INTRA; | |
933 } else { | |
934 mb_type = MB_INTRA; | |
935 } | |
936 break; | |
937 case P_TYPE: | |
545 | 938 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1); |
568 | 939 if (mb_type < 0){ |
940 fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y); | |
0 | 941 return -1; |
568 | 942 } |
0 | 943 break; |
944 case B_TYPE: | |
545 | 945 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1); |
568 | 946 if (mb_type < 0){ |
947 fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y); | |
0 | 948 return -1; |
568 | 949 } |
0 | 950 break; |
951 } | |
952 dprintf("mb_type=%x\n", mb_type); | |
953 motion_type = 0; /* avoid warning */ | |
954 if (mb_type & (MB_FOR|MB_BACK)) { | |
955 /* get additionnal motion vector type */ | |
956 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) | |
957 motion_type = MT_FRAME; | |
958 else | |
959 motion_type = get_bits(&s->gb, 2); | |
960 } | |
961 /* compute dct type */ | |
962 if (s->picture_structure == PICT_FRAME && | |
963 !s->frame_pred_frame_dct && | |
964 (mb_type & (MB_PAT | MB_INTRA))) { | |
21 | 965 s->interlaced_dct = get_bits1(&s->gb); |
0 | 966 #ifdef DEBUG |
967 if (s->interlaced_dct) | |
968 printf("interlaced_dct\n"); | |
969 #endif | |
970 } else { | |
971 s->interlaced_dct = 0; /* frame based */ | |
972 } | |
973 | |
974 if (mb_type & MB_QUANT) { | |
54 | 975 s->qscale = get_qscale(s); |
0 | 976 } |
977 if (mb_type & MB_INTRA) { | |
978 if (s->concealment_motion_vectors) { | |
979 /* just parse them */ | |
980 if (s->picture_structure != PICT_FRAME) | |
21 | 981 skip_bits1(&s->gb); /* field select */ |
0 | 982 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0); |
983 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0); | |
984 } | |
985 s->mb_intra = 1; | |
986 cbp = 0x3f; | |
987 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | |
988 } else { | |
989 s->mb_intra = 0; | |
990 cbp = 0; | |
991 } | |
992 /* special case of implicit zero motion vector */ | |
993 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) { | |
994 s->mv_dir = MV_DIR_FORWARD; | |
995 s->mv_type = MV_TYPE_16X16; | |
996 s->last_mv[0][0][0] = 0; | |
997 s->last_mv[0][0][1] = 0; | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
998 s->last_mv[0][1][0] = 0; |
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
999 s->last_mv[0][1][1] = 0; |
0 | 1000 s->mv[0][0][0] = 0; |
1001 s->mv[0][0][1] = 0; | |
1002 } else if (mb_type & (MB_FOR | MB_BACK)) { | |
1003 /* motion vectors */ | |
1004 s->mv_dir = 0; | |
1005 for(i=0;i<2;i++) { | |
1006 if (mb_type & (MB_FOR >> i)) { | |
1007 s->mv_dir |= (MV_DIR_FORWARD >> i); | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
1008 dprintf("motion_type=%d\n", motion_type); |
0 | 1009 switch(motion_type) { |
1010 case MT_FRAME: /* or MT_16X8 */ | |
1011 if (s->picture_structure == PICT_FRAME) { | |
1012 /* MT_FRAME */ | |
1013 s->mv_type = MV_TYPE_16X16; | |
1014 for(k=0;k<2;k++) { | |
1015 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1016 s->last_mv[i][0][k]); | |
1017 s->last_mv[i][0][k] = val; | |
1018 s->last_mv[i][1][k] = val; | |
1019 /* full_pel: only for mpeg1 */ | |
1020 if (s->full_pel[i]) | |
1021 val = val << 1; | |
1022 s->mv[i][0][k] = val; | |
1023 dprintf("mv%d: %d\n", k, val); | |
1024 } | |
1025 } else { | |
1026 /* MT_16X8 */ | |
1027 s->mv_type = MV_TYPE_16X8; | |
1028 for(j=0;j<2;j++) { | |
21 | 1029 s->field_select[i][j] = get_bits1(&s->gb); |
0 | 1030 for(k=0;k<2;k++) { |
1031 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1032 s->last_mv[i][j][k]); | |
1033 s->last_mv[i][j][k] = val; | |
1034 s->mv[i][j][k] = val; | |
1035 } | |
1036 } | |
1037 } | |
1038 break; | |
1039 case MT_FIELD: | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1040 s->mv_type = MV_TYPE_FIELD; |
0 | 1041 if (s->picture_structure == PICT_FRAME) { |
1042 for(j=0;j<2;j++) { | |
21 | 1043 s->field_select[i][j] = get_bits1(&s->gb); |
0 | 1044 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0], |
1045 s->last_mv[i][j][0]); | |
1046 s->last_mv[i][j][0] = val; | |
1047 s->mv[i][j][0] = val; | |
1048 dprintf("fmx=%d\n", val); | |
1049 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
1050 s->last_mv[i][j][1] >> 1); | |
1051 s->last_mv[i][j][1] = val << 1; | |
1052 s->mv[i][j][1] = val; | |
1053 dprintf("fmy=%d\n", val); | |
1054 } | |
1055 } else { | |
21 | 1056 s->field_select[i][0] = get_bits1(&s->gb); |
0 | 1057 for(k=0;k<2;k++) { |
1058 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
1059 s->last_mv[i][0][k]); | |
1060 s->last_mv[i][0][k] = val; | |
1061 s->last_mv[i][1][k] = val; | |
1062 s->mv[i][0][k] = val; | |
1063 } | |
1064 } | |
1065 break; | |
1066 case MT_DMV: | |
1067 { | |
1068 int dmx, dmy, mx, my, m; | |
1069 | |
1070 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], | |
1071 s->last_mv[i][0][0]); | |
1072 s->last_mv[i][0][0] = mx; | |
1073 s->last_mv[i][1][0] = mx; | |
1074 dmx = get_dmv(s); | |
1075 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
1076 s->last_mv[i][0][1] >> 1); | |
1077 dmy = get_dmv(s); | |
1078 s->mv_type = MV_TYPE_DMV; | |
1079 /* XXX: totally broken */ | |
1080 if (s->picture_structure == PICT_FRAME) { | |
1081 s->last_mv[i][0][1] = my << 1; | |
1082 s->last_mv[i][1][1] = my << 1; | |
1083 | |
1084 m = s->top_field_first ? 1 : 3; | |
1085 /* top -> top pred */ | |
1086 s->mv[i][0][0] = mx; | |
1087 s->mv[i][0][1] = my << 1; | |
1088 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
1089 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1; | |
1090 m = 4 - m; | |
1091 s->mv[i][2][0] = mx; | |
1092 s->mv[i][2][1] = my << 1; | |
1093 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
1094 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; | |
1095 } else { | |
1096 s->last_mv[i][0][1] = my; | |
1097 s->last_mv[i][1][1] = my; | |
1098 s->mv[i][0][0] = mx; | |
1099 s->mv[i][0][1] = my; | |
1100 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx; | |
1101 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 | |
1102 /* + 2 * cur_field */; | |
1103 } | |
1104 } | |
1105 break; | |
1106 } | |
1107 } | |
1108 } | |
1109 } | |
1110 | |
1111 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) { | |
21 | 1112 skip_bits1(&s->gb); /* marker */ |
0 | 1113 } |
1114 | |
1115 if (mb_type & MB_PAT) { | |
545 | 1116 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1); |
568 | 1117 if (cbp < 0){ |
1118 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y); | |
0 | 1119 return -1; |
568 | 1120 } |
0 | 1121 cbp++; |
1122 } | |
1123 dprintf("cbp=%x\n", cbp); | |
1124 | |
1125 if (s->mpeg2) { | |
1126 if (s->mb_intra) { | |
1127 for(i=0;i<6;i++) { | |
711 | 1128 if (mpeg2_decode_block_intra(s, block[i], i) < 0) |
1129 return -1; | |
0 | 1130 } |
1131 } else { | |
1132 for(i=0;i<6;i++) { | |
711 | 1133 if (cbp & 32) { |
0 | 1134 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) |
1135 return -1; | |
391 | 1136 } else { |
1137 s->block_last_index[i] = -1; | |
0 | 1138 } |
711 | 1139 cbp+=cbp; |
0 | 1140 } |
1141 } | |
1142 } else { | |
711 | 1143 if (s->mb_intra) { |
1144 for(i=0;i<6;i++) { | |
1145 if (mpeg1_decode_block_intra(s, block[i], i) < 0) | |
0 | 1146 return -1; |
711 | 1147 } |
1148 }else{ | |
1149 for(i=0;i<6;i++) { | |
1150 if (cbp & 32) { | |
1151 if (mpeg1_decode_block_inter(s, block[i], i) < 0) | |
1152 return -1; | |
1153 } else { | |
1154 s->block_last_index[i] = -1; | |
1155 } | |
1156 cbp+=cbp; | |
0 | 1157 } |
1158 } | |
1159 } | |
1160 return 0; | |
1161 } | |
1162 | |
1163 /* as h263, but only 17 codes */ | |
1164 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) | |
1165 { | |
1166 int code, sign, val, m, l, shift; | |
1167 | |
545 | 1168 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); |
0 | 1169 if (code < 0) { |
1170 return 0xffff; | |
1171 } | |
1172 if (code == 0) { | |
1173 return pred; | |
1174 } | |
21 | 1175 sign = get_bits1(&s->gb); |
0 | 1176 shift = fcode - 1; |
1177 val = (code - 1) << shift; | |
1178 if (shift > 0) | |
1179 val |= get_bits(&s->gb, shift); | |
1180 val++; | |
1181 if (sign) | |
1182 val = -val; | |
1183 val += pred; | |
1184 | |
1185 /* modulo decoding */ | |
1186 l = (1 << shift) * 16; | |
1187 m = 2 * l; | |
1188 if (val < -l) { | |
1189 val += m; | |
1190 } else if (val >= l) { | |
1191 val -= m; | |
1192 } | |
1193 return val; | |
1194 } | |
1195 | |
1196 static inline int decode_dc(MpegEncContext *s, int component) | |
1197 { | |
1198 int code, diff; | |
1199 | |
1200 if (component == 0) { | |
568 | 1201 code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2); |
0 | 1202 } else { |
568 | 1203 code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2); |
0 | 1204 } |
568 | 1205 if (code < 0){ |
1206 fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y); | |
0 | 1207 return 0xffff; |
568 | 1208 } |
0 | 1209 if (code == 0) { |
1210 diff = 0; | |
1211 } else { | |
1212 diff = get_bits(&s->gb, code); | |
1213 if ((diff & (1 << (code - 1))) == 0) | |
1214 diff = (-1 << code) | (diff + 1); | |
1215 } | |
1216 return diff; | |
1217 } | |
1218 | |
711 | 1219 static inline int mpeg1_decode_block_intra(MpegEncContext *s, |
0 | 1220 DCTELEM *block, |
1221 int n) | |
1222 { | |
1223 int level, dc, diff, i, j, run; | |
711 | 1224 int component; |
0 | 1225 RLTable *rl = &rl_mpeg1; |
1064 | 1226 uint8_t * const scantable= s->intra_scantable.permutated; |
1227 const uint16_t *quant_matrix= s->intra_matrix; | |
711 | 1228 const int qscale= s->qscale; |
0 | 1229 |
711 | 1230 /* DC coef */ |
1231 component = (n <= 3 ? 0 : n - 4 + 1); | |
1232 diff = decode_dc(s, component); | |
1233 if (diff >= 0xffff) | |
1234 return -1; | |
1235 dc = s->last_dc[component]; | |
1236 dc += diff; | |
1237 s->last_dc[component] = dc; | |
1238 block[0] = dc<<3; | |
1239 dprintf("dc=%d diff=%d\n", dc, diff); | |
1240 i = 0; | |
1241 { | |
1242 OPEN_READER(re, &s->gb); | |
1243 /* now quantify & encode AC coefs */ | |
1244 for(;;) { | |
1245 UPDATE_CACHE(re, &s->gb); | |
1246 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1247 | |
1248 if(level == 127){ | |
1249 break; | |
1250 } else if(level != 0) { | |
1251 i += run; | |
1252 j = scantable[i]; | |
1253 level= (level*qscale*quant_matrix[j])>>3; | |
1254 level= (level-1)|1; | |
1255 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1256 LAST_SKIP_BITS(re, &s->gb, 1); | |
1257 } else { | |
1258 /* escape */ | |
1259 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1260 UPDATE_CACHE(re, &s->gb); | |
1261 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); | |
1262 if (level == -128) { | |
1263 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); | |
1264 } else if (level == 0) { | |
1265 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); | |
1266 } | |
1267 i += run; | |
1268 j = scantable[i]; | |
1269 if(level<0){ | |
1270 level= -level; | |
1271 level= (level*qscale*quant_matrix[j])>>3; | |
1272 level= (level-1)|1; | |
1273 level= -level; | |
1274 }else{ | |
1275 level= (level*qscale*quant_matrix[j])>>3; | |
1276 level= (level-1)|1; | |
1277 } | |
1278 } | |
1279 if (i > 63){ | |
1280 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1281 return -1; | |
1282 } | |
1283 | |
1284 block[j] = level; | |
1285 } | |
1286 CLOSE_READER(re, &s->gb); | |
1287 } | |
1288 s->block_last_index[n] = i; | |
1289 return 0; | |
1290 } | |
1291 | |
1292 static inline int mpeg1_decode_block_inter(MpegEncContext *s, | |
1293 DCTELEM *block, | |
1294 int n) | |
1295 { | |
1296 int level, i, j, run; | |
1297 RLTable *rl = &rl_mpeg1; | |
1064 | 1298 uint8_t * const scantable= s->intra_scantable.permutated; |
1299 const uint16_t *quant_matrix= s->inter_matrix; | |
711 | 1300 const int qscale= s->qscale; |
1301 | |
1302 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1303 int v; |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1304 OPEN_READER(re, &s->gb); |
711 | 1305 i = -1; |
0 | 1306 /* special case for the first coef. no need to add a second vlc table */ |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1307 UPDATE_CACHE(re, &s->gb); |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1308 v= SHOW_UBITS(re, &s->gb, 2); |
0 | 1309 if (v & 2) { |
714 | 1310 LAST_SKIP_BITS(re, &s->gb, 2); |
711 | 1311 level= (3*qscale*quant_matrix[0])>>4; |
1312 level= (level-1)|1; | |
1313 if(v&1) | |
1314 level= -level; | |
714 | 1315 block[0] = level; |
711 | 1316 i++; |
0 | 1317 } |
714 | 1318 |
711 | 1319 /* now quantify & encode AC coefs */ |
1320 for(;;) { | |
1321 UPDATE_CACHE(re, &s->gb); | |
1322 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1323 | |
1324 if(level == 127){ | |
1325 break; | |
1326 } else if(level != 0) { | |
1327 i += run; | |
1328 j = scantable[i]; | |
1329 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1330 level= (level-1)|1; | |
1331 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1332 LAST_SKIP_BITS(re, &s->gb, 1); | |
1333 } else { | |
1334 /* escape */ | |
1335 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1336 UPDATE_CACHE(re, &s->gb); | |
1337 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); | |
1338 if (level == -128) { | |
1339 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); | |
1340 } else if (level == 0) { | |
1341 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); | |
1342 } | |
1343 i += run; | |
1344 j = scantable[i]; | |
1345 if(level<0){ | |
1346 level= -level; | |
1347 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1348 level= (level-1)|1; | |
1349 level= -level; | |
1350 }else{ | |
1351 level= ((level*2+1)*qscale*quant_matrix[j])>>4; | |
1352 level= (level-1)|1; | |
1353 } | |
1354 } | |
1355 if (i > 63){ | |
1356 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1357 return -1; | |
1358 } | |
0 | 1359 |
711 | 1360 block[j] = level; |
0 | 1361 } |
711 | 1362 CLOSE_READER(re, &s->gb); |
0 | 1363 } |
711 | 1364 s->block_last_index[n] = i; |
0 | 1365 return 0; |
1366 } | |
1367 | |
1368 /* Also does unquantization here, since I will never support mpeg2 | |
1369 encoding */ | |
714 | 1370 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, |
1371 DCTELEM *block, | |
1372 int n) | |
0 | 1373 { |
1374 int level, i, j, run; | |
1375 RLTable *rl = &rl_mpeg1; | |
1064 | 1376 uint8_t * const scantable= s->intra_scantable.permutated; |
1377 const uint16_t *quant_matrix; | |
714 | 1378 const int qscale= s->qscale; |
0 | 1379 int mismatch; |
1380 | |
1381 mismatch = 1; | |
1382 | |
1383 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1384 int v; |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1385 OPEN_READER(re, &s->gb); |
714 | 1386 i = -1; |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1387 if (n < 4) |
714 | 1388 quant_matrix = s->inter_matrix; |
0 | 1389 else |
714 | 1390 quant_matrix = s->chroma_inter_matrix; |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1391 |
0 | 1392 /* special case for the first coef. no need to add a second vlc table */ |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1393 UPDATE_CACHE(re, &s->gb); |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1394 v= SHOW_UBITS(re, &s->gb, 2); |
0 | 1395 if (v & 2) { |
714 | 1396 LAST_SKIP_BITS(re, &s->gb, 2); |
1397 level= (3*qscale*quant_matrix[0])>>5; | |
1398 if(v&1) | |
1399 level= -level; | |
1400 block[0] = level; | |
1401 mismatch ^= level; | |
1402 i++; | |
1403 } | |
1404 | |
1405 /* now quantify & encode AC coefs */ | |
1406 for(;;) { | |
1407 UPDATE_CACHE(re, &s->gb); | |
1408 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1409 | |
1410 if(level == 127){ | |
1411 break; | |
1412 } else if(level != 0) { | |
1413 i += run; | |
1414 j = scantable[i]; | |
1415 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1416 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1417 LAST_SKIP_BITS(re, &s->gb, 1); | |
1418 } else { | |
1419 /* escape */ | |
1420 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1421 UPDATE_CACHE(re, &s->gb); | |
1422 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); | |
1423 | |
1424 i += run; | |
1425 j = scantable[i]; | |
1426 if(level<0){ | |
1427 level= ((-level*2+1)*qscale*quant_matrix[j])>>5; | |
1428 level= -level; | |
1429 }else{ | |
1430 level= ((level*2+1)*qscale*quant_matrix[j])>>5; | |
1431 } | |
1432 } | |
1433 if (i > 63){ | |
1434 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1435 return -1; | |
1436 } | |
1437 | |
1438 mismatch ^= level; | |
1439 block[j] = level; | |
0 | 1440 } |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
498
diff
changeset
|
1441 CLOSE_READER(re, &s->gb); |
0 | 1442 } |
1443 block[63] ^= (mismatch & 1); | |
714 | 1444 |
0 | 1445 s->block_last_index[n] = i; |
1446 return 0; | |
1447 } | |
1448 | |
714 | 1449 static inline int mpeg2_decode_block_intra(MpegEncContext *s, |
1450 DCTELEM *block, | |
1451 int n) | |
0 | 1452 { |
1453 int level, dc, diff, i, j, run; | |
714 | 1454 int component; |
0 | 1455 RLTable *rl; |
1064 | 1456 uint8_t * const scantable= s->intra_scantable.permutated; |
1457 const uint16_t *quant_matrix; | |
714 | 1458 const int qscale= s->qscale; |
0 | 1459 int mismatch; |
1460 | |
1461 /* DC coef */ | |
714 | 1462 if (n < 4){ |
1463 quant_matrix = s->intra_matrix; | |
1464 component = 0; | |
1465 }else{ | |
1466 quant_matrix = s->chroma_intra_matrix; | |
1467 component = n - 3; | |
1468 } | |
0 | 1469 diff = decode_dc(s, component); |
1470 if (diff >= 0xffff) | |
1471 return -1; | |
1472 dc = s->last_dc[component]; | |
1473 dc += diff; | |
1474 s->last_dc[component] = dc; | |
1475 block[0] = dc << (3 - s->intra_dc_precision); | |
1476 dprintf("dc=%d\n", block[0]); | |
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
1477 mismatch = block[0] ^ 1; |
714 | 1478 i = 0; |
0 | 1479 if (s->intra_vlc_format) |
1480 rl = &rl_mpeg2; | |
1481 else | |
1482 rl = &rl_mpeg1; | |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1483 |
714 | 1484 { |
1485 OPEN_READER(re, &s->gb); | |
1486 /* now quantify & encode AC coefs */ | |
1487 for(;;) { | |
1488 UPDATE_CACHE(re, &s->gb); | |
1489 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); | |
1490 | |
1491 if(level == 127){ | |
1492 break; | |
1493 } else if(level != 0) { | |
1494 i += run; | |
1495 j = scantable[i]; | |
1496 level= (level*qscale*quant_matrix[j])>>4; | |
1497 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
1498 LAST_SKIP_BITS(re, &s->gb, 1); | |
1499 } else { | |
1500 /* escape */ | |
1501 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); | |
1502 UPDATE_CACHE(re, &s->gb); | |
1503 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); | |
1504 i += run; | |
1505 j = scantable[i]; | |
1506 if(level<0){ | |
1507 level= (-level*qscale*quant_matrix[j])>>4; | |
1508 level= -level; | |
1509 }else{ | |
1510 level= (level*qscale*quant_matrix[j])>>4; | |
1511 } | |
1512 } | |
1513 if (i > 63){ | |
1514 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
1515 return -1; | |
1516 } | |
1517 | |
1518 mismatch^= level; | |
1519 block[j] = level; | |
568 | 1520 } |
714 | 1521 CLOSE_READER(re, &s->gb); |
0 | 1522 } |
714 | 1523 block[63]^= mismatch&1; |
1524 | |
0 | 1525 s->block_last_index[n] = i; |
1526 return 0; | |
1527 } | |
1528 | |
1529 /* compressed picture size */ | |
1530 #define PICTURE_BUFFER_SIZE 100000 | |
1531 | |
1532 typedef struct Mpeg1Context { | |
1533 MpegEncContext mpeg_enc_ctx; | |
1064 | 1534 uint32_t header_state; |
0 | 1535 int start_code; /* current start code */ |
1064 | 1536 uint8_t buffer[PICTURE_BUFFER_SIZE]; |
1537 uint8_t *buf_ptr; | |
0 | 1538 int buffer_size; |
1539 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1540 int repeat_field; /* true if we must repeat the field */ |
0 | 1541 } Mpeg1Context; |
1542 | |
1543 static int mpeg_decode_init(AVCodecContext *avctx) | |
1544 { | |
1545 Mpeg1Context *s = avctx->priv_data; | |
498 | 1546 |
558 | 1547 s->mpeg_enc_ctx.flags= avctx->flags; |
498 | 1548 common_init(&s->mpeg_enc_ctx); |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
1549 init_vlcs(&s->mpeg_enc_ctx); |
0 | 1550 |
1551 s->header_state = 0xff; | |
1552 s->mpeg_enc_ctx_allocated = 0; | |
1553 s->buffer_size = PICTURE_BUFFER_SIZE; | |
1554 s->start_code = -1; | |
1555 s->buf_ptr = s->buffer; | |
1556 s->mpeg_enc_ctx.picture_number = 0; | |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1557 s->repeat_field = 0; |
344 | 1558 s->mpeg_enc_ctx.codec_id= avctx->codec->id; |
0 | 1559 return 0; |
1560 } | |
1561 | |
1562 /* return the 8 bit start code value and update the search | |
1563 state. Return -1 if no start code found */ | |
1064 | 1564 static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end, |
1565 uint32_t *header_state) | |
0 | 1566 { |
1064 | 1567 uint8_t *buf_ptr; |
0 | 1568 unsigned int state, v; |
1569 int val; | |
1570 | |
1571 state = *header_state; | |
1572 buf_ptr = *pbuf_ptr; | |
1573 while (buf_ptr < buf_end) { | |
1574 v = *buf_ptr++; | |
1575 if (state == 0x000001) { | |
1576 state = ((state << 8) | v) & 0xffffff; | |
1577 val = state; | |
1578 goto found; | |
1579 } | |
1580 state = ((state << 8) | v) & 0xffffff; | |
1581 } | |
1582 val = -1; | |
1583 found: | |
1584 *pbuf_ptr = buf_ptr; | |
1585 *header_state = state; | |
1586 return val; | |
1587 } | |
1588 | |
1589 static int mpeg1_decode_picture(AVCodecContext *avctx, | |
1064 | 1590 uint8_t *buf, int buf_size) |
0 | 1591 { |
1592 Mpeg1Context *s1 = avctx->priv_data; | |
1593 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1594 int ref, f_code; | |
1595 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1596 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1597 |
1598 ref = get_bits(&s->gb, 10); /* temporal ref */ | |
1599 s->pict_type = get_bits(&s->gb, 3); | |
45
933cc4acab5c
fixed mpeg1 last block bug (mb stuffing code was not included in vlc table...)
glantau
parents:
38
diff
changeset
|
1600 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); |
872 | 1601 |
21 | 1602 skip_bits(&s->gb, 16); |
0 | 1603 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
21 | 1604 s->full_pel[0] = get_bits1(&s->gb); |
0 | 1605 f_code = get_bits(&s->gb, 3); |
1606 if (f_code == 0) | |
1607 return -1; | |
1608 s->mpeg_f_code[0][0] = f_code; | |
1609 s->mpeg_f_code[0][1] = f_code; | |
1610 } | |
1611 if (s->pict_type == B_TYPE) { | |
21 | 1612 s->full_pel[1] = get_bits1(&s->gb); |
0 | 1613 f_code = get_bits(&s->gb, 3); |
1614 if (f_code == 0) | |
1615 return -1; | |
1616 s->mpeg_f_code[1][0] = f_code; | |
1617 s->mpeg_f_code[1][1] = f_code; | |
1618 } | |
903 | 1619 s->current_picture.pict_type= s->pict_type; |
1620 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
911 | 1621 |
0 | 1622 s->y_dc_scale = 8; |
1623 s->c_dc_scale = 8; | |
1624 s->first_slice = 1; | |
1625 return 0; | |
1626 } | |
1627 | |
1628 static void mpeg_decode_sequence_extension(MpegEncContext *s) | |
1629 { | |
1630 int horiz_size_ext, vert_size_ext; | |
917 | 1631 int bit_rate_ext, vbv_buf_ext; |
0 | 1632 int frame_rate_ext_n, frame_rate_ext_d; |
917 | 1633 float aspect; |
0 | 1634 |
21 | 1635 skip_bits(&s->gb, 8); /* profil and level */ |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1636 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ |
21 | 1637 skip_bits(&s->gb, 2); /* chroma_format */ |
0 | 1638 horiz_size_ext = get_bits(&s->gb, 2); |
1639 vert_size_ext = get_bits(&s->gb, 2); | |
1640 s->width |= (horiz_size_ext << 12); | |
1641 s->height |= (vert_size_ext << 12); | |
1642 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ | |
1643 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; | |
21 | 1644 skip_bits1(&s->gb); /* marker */ |
0 | 1645 vbv_buf_ext = get_bits(&s->gb, 8); |
917 | 1646 s->low_delay = get_bits1(&s->gb); |
0 | 1647 frame_rate_ext_n = get_bits(&s->gb, 2); |
1648 frame_rate_ext_d = get_bits(&s->gb, 5); | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1649 av_reduce( |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1650 &s->avctx->frame_rate, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1651 &s->avctx->frame_rate_base, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1652 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1), |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1653 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1), |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1654 1<<30); |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1655 |
0 | 1656 dprintf("sequence extension\n"); |
1657 s->mpeg2 = 1; | |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
1658 s->avctx->sub_id = 2; /* indicates mpeg2 found */ |
917 | 1659 |
1660 aspect= mpeg2_aspect[s->aspect_ratio_info]; | |
1661 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height); | |
1662 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect; | |
0 | 1663 } |
1664 | |
1665 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | |
1666 { | |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1667 int i, v, j; |
0 | 1668 |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1669 dprintf("matrix extension\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1670 |
21 | 1671 if (get_bits1(&s->gb)) { |
0 | 1672 for(i=0;i<64;i++) { |
1673 v = get_bits(&s->gb, 8); | |
1092 | 1674 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1675 s->intra_matrix[j] = v; |
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1676 s->chroma_intra_matrix[j] = v; |
0 | 1677 } |
1678 } | |
21 | 1679 if (get_bits1(&s->gb)) { |
0 | 1680 for(i=0;i<64;i++) { |
1681 v = get_bits(&s->gb, 8); | |
1092 | 1682 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
344 | 1683 s->inter_matrix[j] = v; |
1684 s->chroma_inter_matrix[j] = v; | |
0 | 1685 } |
1686 } | |
21 | 1687 if (get_bits1(&s->gb)) { |
0 | 1688 for(i=0;i<64;i++) { |
1689 v = get_bits(&s->gb, 8); | |
1092 | 1690 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1691 s->chroma_intra_matrix[j] = v; |
0 | 1692 } |
1693 } | |
21 | 1694 if (get_bits1(&s->gb)) { |
0 | 1695 for(i=0;i<64;i++) { |
1696 v = get_bits(&s->gb, 8); | |
1092 | 1697 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; |
344 | 1698 s->chroma_inter_matrix[j] = v; |
0 | 1699 } |
1700 } | |
1701 } | |
1702 | |
1703 static void mpeg_decode_picture_coding_extension(MpegEncContext *s) | |
1704 { | |
1705 s->full_pel[0] = s->full_pel[1] = 0; | |
1706 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4); | |
1707 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4); | |
1708 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4); | |
1709 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4); | |
1710 s->intra_dc_precision = get_bits(&s->gb, 2); | |
1711 s->picture_structure = get_bits(&s->gb, 2); | |
21 | 1712 s->top_field_first = get_bits1(&s->gb); |
1713 s->frame_pred_frame_dct = get_bits1(&s->gb); | |
1714 s->concealment_motion_vectors = get_bits1(&s->gb); | |
1715 s->q_scale_type = get_bits1(&s->gb); | |
1716 s->intra_vlc_format = get_bits1(&s->gb); | |
1717 s->alternate_scan = get_bits1(&s->gb); | |
1718 s->repeat_first_field = get_bits1(&s->gb); | |
1719 s->chroma_420_type = get_bits1(&s->gb); | |
1720 s->progressive_frame = get_bits1(&s->gb); | |
715
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1721 |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1722 if(s->picture_structure == PICT_FRAME) |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1723 s->first_field=0; |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1724 else{ |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1725 s->first_field ^= 1; |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1726 memset(s->mbskip_table, 0, s->mb_width*s->mb_height); |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1727 } |
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1728 |
715
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1729 if(s->alternate_scan){ |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1730 ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1731 ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1732 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1733 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1734 }else{ |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1735 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1736 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1737 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1738 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan); |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1739 } |
8b3ccabfce4a
move scantable init from block-decode to header parser
michaelni
parents:
714
diff
changeset
|
1740 |
0 | 1741 /* composite display not parsed */ |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1742 dprintf("intra_dc_precision=%d\n", s->intra_dc_precision); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1743 dprintf("picture_structure=%d\n", s->picture_structure); |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
1744 dprintf("top field first=%d\n", s->top_field_first); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
1745 dprintf("repeat first field=%d\n", s->repeat_first_field); |
0 | 1746 dprintf("conceal=%d\n", s->concealment_motion_vectors); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1747 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1748 dprintf("alternate_scan=%d\n", s->alternate_scan); |
0 | 1749 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct); |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
1750 dprintf("progressive_frame=%d\n", s->progressive_frame); |
0 | 1751 } |
1752 | |
1753 static void mpeg_decode_extension(AVCodecContext *avctx, | |
1064 | 1754 uint8_t *buf, int buf_size) |
0 | 1755 { |
1756 Mpeg1Context *s1 = avctx->priv_data; | |
1757 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1758 int ext_type; | |
1759 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1760 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1761 |
1762 ext_type = get_bits(&s->gb, 4); | |
1763 switch(ext_type) { | |
1764 case 0x1: | |
1765 /* sequence ext */ | |
1766 mpeg_decode_sequence_extension(s); | |
1767 break; | |
1768 case 0x3: | |
1769 /* quant matrix extension */ | |
1770 mpeg_decode_quant_matrix_extension(s); | |
1771 break; | |
1772 case 0x8: | |
1773 /* picture extension */ | |
1774 mpeg_decode_picture_coding_extension(s); | |
1775 break; | |
1776 } | |
1777 } | |
1778 | |
826 | 1779 #define DECODE_SLICE_FATAL_ERROR -2 |
1780 #define DECODE_SLICE_ERROR -1 | |
1781 #define DECODE_SLICE_OK 0 | |
1782 #define DECODE_SLICE_EOP 1 | |
1783 | |
1784 /** | |
1785 * decodes a slice. | |
1786 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br> | |
1787 * DECODE_SLICE_ERROR if the slice is damaged<br> | |
1788 * DECODE_SLICE_OK if this slice is ok<br> | |
1789 * DECODE_SLICE_EOP if the end of the picture is reached | |
1790 */ | |
0 | 1791 static int mpeg_decode_slice(AVCodecContext *avctx, |
925 | 1792 AVFrame *pict, |
0 | 1793 int start_code, |
1064 | 1794 uint8_t *buf, int buf_size) |
0 | 1795 { |
1796 Mpeg1Context *s1 = avctx->priv_data; | |
1797 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
1798 int ret; | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1799 const int field_pic= s->picture_structure != PICT_FRAME; |
0 | 1800 |
1801 start_code = (start_code - 1) & 0xff; | |
568 | 1802 if (start_code >= s->mb_height){ |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
1803 fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height); |
826 | 1804 return DECODE_SLICE_ERROR; |
568 | 1805 } |
1160 | 1806 |
1807 ff_mpeg1_clean_buffers(s); | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1808 |
0 | 1809 /* start frame decoding */ |
1138 | 1810 if (s->first_slice) { |
1811 if(s->first_field || s->picture_structure==PICT_FRAME){ | |
771
d4cc92144266
handle direct rendering buffer allocation failure
michaelni
parents:
751
diff
changeset
|
1812 if(MPV_frame_start(s, avctx) < 0) |
826 | 1813 return DECODE_SLICE_FATAL_ERROR; |
1085
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1814 /* first check if we must repeat the frame */ |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1815 s->current_picture.repeat_pict = 0; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1816 |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1817 if (s->repeat_first_field) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1818 if (s->progressive_sequence) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1819 if (s->top_field_first) |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1820 s->current_picture.repeat_pict = 4; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1821 else |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1822 s->current_picture.repeat_pict = 2; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1823 } else if (s->progressive_frame) { |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1824 s->current_picture.repeat_pict = 1; |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1825 } |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1826 } |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1827 // printf("%d \n", s->current_picture.repeat_pict); |
9acf4b552047
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
1084
diff
changeset
|
1828 |
930 | 1829 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ |
1021
2d7c9f5738de
trying to fix mb skip bug in mpeg1/2 if slices are not used
michaelni
parents:
947
diff
changeset
|
1830 printf("qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n", |
930 | 1831 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1], |
1832 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), | |
1833 s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"", | |
1834 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors, | |
1835 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :""); | |
1836 } | |
1138 | 1837 }else{ //second field |
1838 int i; | |
1839 for(i=0; i<4; i++){ | |
1840 s->current_picture.data[i] = s->current_picture_ptr->data[i]; | |
1841 if(s->picture_structure == PICT_BOTTOM_FIELD){ | |
1842 s->current_picture.data[i] += s->current_picture_ptr->linesize[i]; | |
1843 } | |
1844 } | |
1845 } | |
0 | 1846 } |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1847 s->first_slice = 0; |
0 | 1848 |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1849 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1850 |
54 | 1851 s->qscale = get_qscale(s); |
0 | 1852 /* extra slice info */ |
21 | 1853 while (get_bits1(&s->gb) != 0) { |
1854 skip_bits(&s->gb, 8); | |
0 | 1855 } |
1856 | |
716 | 1857 s->mb_x=0; |
1858 for(;;) { | |
1859 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); | |
1860 if (code < 0) | |
1861 return -1; /* error = end of slice, but empty slice is bad or?*/ | |
1862 if (code >= 33) { | |
1863 if (code == 33) { | |
1864 s->mb_x += 33; | |
1865 } | |
1866 /* otherwise, stuffing, nothing to do */ | |
1867 } else { | |
1868 s->mb_x += code; | |
1869 break; | |
1870 } | |
1871 } | |
1160 | 1872 |
716 | 1873 s->mb_y = start_code; |
1160 | 1874 s->mb_skip_run= 0; |
716 | 1875 |
0 | 1876 for(;;) { |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
844
diff
changeset
|
1877 s->dsp.clear_blocks(s->block[0]); |
716 | 1878 |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1879 ret = mpeg_decode_mb(s, s->block); |
0 | 1880 dprintf("ret=%d\n", ret); |
1881 if (ret < 0) | |
1882 return -1; | |
1138 | 1883 |
716 | 1884 MPV_decode_mb(s, s->block); |
1885 | |
1886 if (++s->mb_x >= s->mb_width) { | |
1098
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1887 if(s->picture_structure==PICT_FRAME){ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1888 ff_draw_horiz_band(s, 16*s->mb_y, 16); |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1889 }else{ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1890 if(!s->first_field){ |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1891 ff_draw_horiz_band(s, 32*s->mb_y, 32); |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1892 } |
b7f267d168b7
mpeg2 field pictures + sliced mode (doesnt work with mplayer though, dunno why)
michaelni
parents:
1096
diff
changeset
|
1893 } |
716 | 1894 |
1895 s->mb_x = 0; | |
1896 s->mb_y++; | |
694 | 1897 PRINT_QP("%s", "\n"); |
716 | 1898 } |
694 | 1899 PRINT_QP("%2d", s->qscale); |
716 | 1900 |
1901 /* skip mb handling */ | |
1160 | 1902 if (s->mb_skip_run == -1) { |
716 | 1903 /* read again increment */ |
1160 | 1904 s->mb_skip_run = 0; |
716 | 1905 for(;;) { |
1906 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); | |
1907 if (code < 0) | |
1908 goto eos; /* error = end of slice */ | |
1909 if (code >= 33) { | |
1910 if (code == 33) { | |
1160 | 1911 s->mb_skip_run += 33; |
716 | 1912 } |
1913 /* otherwise, stuffing, nothing to do */ | |
1914 } else { | |
1160 | 1915 s->mb_skip_run += code; |
716 | 1916 break; |
1917 } | |
1918 } | |
1919 } | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1920 if(s->mb_y<<field_pic >= s->mb_height){ |
716 | 1921 fprintf(stderr, "slice too long\n"); |
826 | 1922 return DECODE_SLICE_ERROR; |
716 | 1923 } |
0 | 1924 } |
716 | 1925 eos: //end of slice |
1926 | |
305
1de85b419387
emms was missing, found by juanjo but he didnt commit it?!
michaelni
parents:
296
diff
changeset
|
1927 emms_c(); |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1928 //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number); |
0 | 1929 /* end of slice reached */ |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
1930 if (s->mb_y<<field_pic == s->mb_height && !s->first_field) { |
0 | 1931 /* end of image */ |
903 | 1932 |
1933 if(s->mpeg2) | |
1934 s->qscale >>=1; | |
0 | 1935 |
1936 MPV_frame_end(s); | |
1937 | |
924 | 1938 if (s->pict_type == B_TYPE || s->low_delay) { |
925 | 1939 *pict= *(AVFrame*)&s->current_picture; |
0 | 1940 } else { |
903 | 1941 s->picture_number++; |
0 | 1942 /* latency of 1 frame for I and P frames */ |
1943 /* XXX: use another variable than picture_number */ | |
1138 | 1944 if (s->last_picture_ptr == NULL) { |
903 | 1945 return DECODE_SLICE_OK; |
0 | 1946 } else { |
925 | 1947 *pict= *(AVFrame*)&s->last_picture; |
0 | 1948 } |
1949 } | |
903 | 1950 return DECODE_SLICE_EOP; |
0 | 1951 } else { |
826 | 1952 return DECODE_SLICE_OK; |
0 | 1953 } |
1954 } | |
1955 | |
1956 static int mpeg1_decode_sequence(AVCodecContext *avctx, | |
1064 | 1957 uint8_t *buf, int buf_size) |
0 | 1958 { |
1959 Mpeg1Context *s1 = avctx->priv_data; | |
1960 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1961 int width, height, i, v, j; |
917 | 1962 float aspect; |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
1963 |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1021
diff
changeset
|
1964 init_get_bits(&s->gb, buf, buf_size*8); |
0 | 1965 |
1966 width = get_bits(&s->gb, 12); | |
1967 height = get_bits(&s->gb, 12); | |
917 | 1968 s->aspect_ratio_info= get_bits(&s->gb, 4); |
1969 if(!s->mpeg2){ | |
1970 aspect= mpeg1_aspect[s->aspect_ratio_info]; | |
1971 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height); | |
1972 } | |
1973 | |
0 | 1974 s->frame_rate_index = get_bits(&s->gb, 4); |
1975 if (s->frame_rate_index == 0) | |
1976 return -1; | |
1977 s->bit_rate = get_bits(&s->gb, 18) * 400; | |
21 | 1978 if (get_bits1(&s->gb) == 0) /* marker */ |
0 | 1979 return -1; |
1980 if (width <= 0 || height <= 0 || | |
1981 (width % 2) != 0 || (height % 2) != 0) | |
1982 return -1; | |
1983 if (width != s->width || | |
1984 height != s->height) { | |
1985 /* start new mpeg1 context decoding */ | |
1986 s->out_format = FMT_MPEG1; | |
1987 if (s1->mpeg_enc_ctx_allocated) { | |
1988 MPV_common_end(s); | |
1989 } | |
1990 s->width = width; | |
1991 s->height = height; | |
924 | 1992 avctx->has_b_frames= 1; |
69 | 1993 s->avctx = avctx; |
0 | 1994 avctx->width = width; |
1995 avctx->height = height; | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1996 av_reduce( |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1997 &avctx->frame_rate, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1998 &avctx->frame_rate_base, |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
1999 frame_rate_tab[s->frame_rate_index], |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
2000 MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
2001 1<<30 |
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
2002 ); |
0 | 2003 avctx->bit_rate = s->bit_rate; |
2004 | |
2005 if (MPV_common_init(s) < 0) | |
2006 return -1; | |
2007 s1->mpeg_enc_ctx_allocated = 1; | |
2008 } | |
2009 | |
21 | 2010 skip_bits(&s->gb, 10); /* vbv_buffer_size */ |
2011 skip_bits(&s->gb, 1); | |
0 | 2012 |
2013 /* get matrix */ | |
21 | 2014 if (get_bits1(&s->gb)) { |
0 | 2015 for(i=0;i<64;i++) { |
2016 v = get_bits(&s->gb, 8); | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2017 j = s->intra_scantable.permutated[i]; |
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
2018 s->intra_matrix[j] = v; |
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
2019 s->chroma_intra_matrix[j] = v; |
0 | 2020 } |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2021 #ifdef DEBUG |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2022 dprintf("intra matrix present\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2023 for(i=0;i<64;i++) |
710
97377ab86647
forgot zigzag_direct[] behind #ifdef DEBUG (found by Klaas-Pieter Vlieg <vlieg at eurescom dot de>)
michaelni
parents:
706
diff
changeset
|
2024 dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2025 printf("\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2026 #endif |
0 | 2027 } else { |
2028 for(i=0;i<64;i++) { | |
1092 | 2029 int j= s->dsp.idct_permutation[i]; |
533
3c07cf9595de
adding ff prefix to avoid global name conficts with xvid (patch by Marko Kreen <marko at l-t.ee>)
michaelni
parents:
520
diff
changeset
|
2030 v = ff_mpeg1_default_intra_matrix[i]; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2031 s->intra_matrix[j] = v; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2032 s->chroma_intra_matrix[j] = v; |
0 | 2033 } |
2034 } | |
21 | 2035 if (get_bits1(&s->gb)) { |
0 | 2036 for(i=0;i<64;i++) { |
2037 v = get_bits(&s->gb, 8); | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2038 j = s->intra_scantable.permutated[i]; |
344 | 2039 s->inter_matrix[j] = v; |
2040 s->chroma_inter_matrix[j] = v; | |
0 | 2041 } |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2042 #ifdef DEBUG |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2043 dprintf("non intra matrix present\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2044 for(i=0;i<64;i++) |
710
97377ab86647
forgot zigzag_direct[] behind #ifdef DEBUG (found by Klaas-Pieter Vlieg <vlieg at eurescom dot de>)
michaelni
parents:
706
diff
changeset
|
2045 dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]); |
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2046 printf("\n"); |
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
2047 #endif |
0 | 2048 } else { |
2049 for(i=0;i<64;i++) { | |
1092 | 2050 int j= s->dsp.idct_permutation[i]; |
533
3c07cf9595de
adding ff prefix to avoid global name conficts with xvid (patch by Marko Kreen <marko at l-t.ee>)
michaelni
parents:
520
diff
changeset
|
2051 v = ff_mpeg1_default_non_intra_matrix[i]; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2052 s->inter_matrix[j] = v; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
694
diff
changeset
|
2053 s->chroma_inter_matrix[j] = v; |
0 | 2054 } |
2055 } | |
2056 | |
2057 /* we set mpeg2 parameters so that it emulates mpeg1 */ | |
2058 s->progressive_sequence = 1; | |
2059 s->progressive_frame = 1; | |
2060 s->picture_structure = PICT_FRAME; | |
2061 s->frame_pred_frame_dct = 1; | |
2062 s->mpeg2 = 0; | |
401
e20655449d4a
mpeg1/2 identifier - fixed frame rate for some bad mpeg1 streams
glantau
parents:
391
diff
changeset
|
2063 avctx->sub_id = 1; /* indicates mpeg1 */ |
0 | 2064 return 0; |
2065 } | |
2066 | |
1084 | 2067 static void mpeg_decode_user_data(AVCodecContext *avctx, |
2068 const uint8_t *buf, int buf_size) | |
2069 { | |
2070 const uint8_t *p; | |
2071 int len, flags; | |
2072 p = buf; | |
2073 len = buf_size; | |
2074 | |
2075 /* we parse the DTG active format information */ | |
2076 if (len >= 5 && | |
2077 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') { | |
2078 flags = p[4]; | |
2079 p += 5; | |
2080 len -= 5; | |
2081 if (flags & 0x80) { | |
2082 /* skip event id */ | |
2083 if (len < 2) | |
2084 return; | |
2085 p += 2; | |
2086 len -= 2; | |
2087 } | |
2088 if (flags & 0x40) { | |
2089 if (len < 1) | |
2090 return; | |
2091 avctx->dtg_active_format = p[0] & 0x0f; | |
2092 } | |
2093 } | |
2094 } | |
2095 | |
0 | 2096 /* handle buffering and image synchronisation */ |
2097 static int mpeg_decode_frame(AVCodecContext *avctx, | |
2098 void *data, int *data_size, | |
1064 | 2099 uint8_t *buf, int buf_size) |
0 | 2100 { |
2101 Mpeg1Context *s = avctx->priv_data; | |
1064 | 2102 uint8_t *buf_end, *buf_ptr, *buf_start; |
0 | 2103 int len, start_code_found, ret, code, start_code, input_size; |
925 | 2104 AVFrame *picture = data; |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2105 MpegEncContext *s2 = &s->mpeg_enc_ctx; |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2106 |
0 | 2107 dprintf("fill_buffer\n"); |
2108 | |
2109 *data_size = 0; | |
344 | 2110 |
0 | 2111 /* special case for last picture */ |
2112 if (buf_size == 0) { | |
2113 if (s2->picture_number > 0) { | |
925 | 2114 *picture= *(AVFrame*)&s2->next_picture; |
903 | 2115 |
925 | 2116 *data_size = sizeof(AVFrame); |
0 | 2117 } |
2118 return 0; | |
2119 } | |
2120 | |
2121 buf_ptr = buf; | |
2122 buf_end = buf + buf_size; | |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2123 |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2124 #if 0 |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2125 if (s->repeat_field % 2 == 1) { |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2126 s->repeat_field++; |
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2127 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number, |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2128 // s2->picture_number, s->repeat_field); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2129 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) { |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2130 *data_size = sizeof(AVPicture); |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2131 goto the_end; |
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2132 } |
267
e10840e4f773
- Bug fix MPEG-2 decoder to handle "repeat_first_field" (Telecine)
pulento
parents:
241
diff
changeset
|
2133 } |
383
e6b64bc3bc87
- repeat_pict meaning changed, now it signals the extra delay for the
pulento
parents:
377
diff
changeset
|
2134 #endif |
0 | 2135 while (buf_ptr < buf_end) { |
2136 buf_start = buf_ptr; | |
2137 /* find start next code */ | |
2138 code = find_start_code(&buf_ptr, buf_end, &s->header_state); | |
2139 if (code >= 0) { | |
2140 start_code_found = 1; | |
2141 } else { | |
2142 start_code_found = 0; | |
2143 } | |
2144 /* copy to buffer */ | |
2145 len = buf_ptr - buf_start; | |
2146 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
2147 /* data too big : flush */ | |
2148 s->buf_ptr = s->buffer; | |
2149 if (start_code_found) | |
2150 s->start_code = code; | |
2151 } else { | |
2152 memcpy(s->buf_ptr, buf_start, len); | |
2153 s->buf_ptr += len; | |
842
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
826
diff
changeset
|
2154 if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found) |
647
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2155 && s->buf_ptr+4<s->buffer+s->buffer_size){ |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2156 start_code_found= 1; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2157 code= 0x1FF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2158 s->header_state=0xFF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2159 s->buf_ptr[0]=0; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2160 s->buf_ptr[1]=0; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2161 s->buf_ptr[2]=1; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2162 s->buf_ptr[3]=0xFF; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2163 s->buf_ptr+=4; |
22b22723805e
support decoding of the last mpeg "packet" even if no startcode is immedeatly afterwards (fixes bugs with mpeg in avi)
michaelni
parents:
639
diff
changeset
|
2164 } |
0 | 2165 if (start_code_found) { |
2166 /* prepare data for next start code */ | |
2167 input_size = s->buf_ptr - s->buffer; | |
2168 start_code = s->start_code; | |
2169 s->buf_ptr = s->buffer; | |
2170 s->start_code = code; | |
2171 switch(start_code) { | |
2172 case SEQ_START_CODE: | |
2173 mpeg1_decode_sequence(avctx, s->buffer, | |
2174 input_size); | |
2175 break; | |
2176 | |
2177 case PICTURE_START_CODE: | |
2178 /* we have a complete image : we try to decompress it */ | |
2179 mpeg1_decode_picture(avctx, | |
2180 s->buffer, input_size); | |
2181 break; | |
2182 case EXT_START_CODE: | |
2183 mpeg_decode_extension(avctx, | |
2184 s->buffer, input_size); | |
2185 break; | |
1084 | 2186 case USER_START_CODE: |
2187 mpeg_decode_user_data(avctx, | |
2188 s->buffer, input_size); | |
2189 break; | |
0 | 2190 default: |
2191 if (start_code >= SLICE_MIN_START_CODE && | |
911 | 2192 start_code <= SLICE_MAX_START_CODE) { |
917 | 2193 |
2194 /* skip b frames if we dont have reference frames */ | |
1138 | 2195 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break; |
917 | 2196 /* skip b frames if we are in a hurry */ |
2197 if(avctx->hurry_up && s2->pict_type==B_TYPE) break; | |
2198 /* skip everything if we are in a hurry>=5 */ | |
2199 if(avctx->hurry_up>=5) break; | |
2200 | |
0 | 2201 ret = mpeg_decode_slice(avctx, picture, |
2202 start_code, s->buffer, input_size); | |
1096
5e6e505d8997
field picture decoding support (16x16 MC blocks only as i dont have any samples which use other modes ...)
michaelni
parents:
1092
diff
changeset
|
2203 |
826 | 2204 if (ret == DECODE_SLICE_EOP) { |
0 | 2205 *data_size = sizeof(AVPicture); |
2206 goto the_end; | |
825 | 2207 }else if(ret<0){ |
890
653d9419ea01
dont put flies in the stdout soup patch by (Agent Smith <asmith at wgz dot com>)
michaelni
parents:
885
diff
changeset
|
2208 fprintf(stderr,"Error while decoding slice\n"); |
826 | 2209 if(ret==DECODE_SLICE_FATAL_ERROR) return -1; |
0 | 2210 } |
2211 } | |
2212 break; | |
2213 } | |
2214 } | |
2215 } | |
2216 } | |
2217 the_end: | |
2218 return buf_ptr - buf; | |
2219 } | |
2220 | |
2221 static int mpeg_decode_end(AVCodecContext *avctx) | |
2222 { | |
2223 Mpeg1Context *s = avctx->priv_data; | |
2224 | |
2225 if (s->mpeg_enc_ctx_allocated) | |
2226 MPV_common_end(&s->mpeg_enc_ctx); | |
2227 return 0; | |
2228 } | |
2229 | |
2230 AVCodec mpeg_decoder = { | |
2231 "mpegvideo", | |
2232 CODEC_TYPE_VIDEO, | |
2233 CODEC_ID_MPEG1VIDEO, | |
2234 sizeof(Mpeg1Context), | |
2235 mpeg_decode_init, | |
2236 NULL, | |
2237 mpeg_decode_end, | |
2238 mpeg_decode_frame, | |
842
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
826
diff
changeset
|
2239 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, |
0 | 2240 }; |