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