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