00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "stdafx.h"
00024
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <malloc.h>
00028 #include "libmpeg2.h"
00029 #include "..\..\..\DSUtil\vd.h"
00030
00031 #ifndef NULL
00032 #define NULL 0
00033 #endif
00034
00035
00036
00037 #define SEQ_EXT 2
00038 #define SEQ_DISPLAY_EXT 4
00039 #define QUANT_MATRIX_EXT 8
00040 #define COPYRIGHT_EXT 0x10
00041 #define PIC_DISPLAY_EXT 0x80
00042 #define PIC_CODING_EXT 0x100
00043
00044
00045 static const uint8_t default_intra_quantizer_matrix[64] = {
00046 8,
00047 16, 16,
00048 19, 16, 19,
00049 22, 22, 22, 22,
00050 22, 22, 26, 24, 26,
00051 27, 27, 27, 26, 26, 26,
00052 26, 27, 27, 27, 29, 29, 29,
00053 34, 34, 34, 29, 29, 29, 27, 27,
00054 29, 29, 32, 32, 34, 34, 37,
00055 38, 37, 35, 35, 34, 35,
00056 38, 38, 40, 40, 40,
00057 48, 48, 46, 46,
00058 56, 56, 58,
00059 69, 69,
00060 83
00061 };
00062
00063 static uint8_t mpeg2_scan_norm_2[64] = {
00064
00065 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
00066 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
00067 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
00068 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
00069 };
00070
00071 static uint8_t mpeg2_scan_alt_2[64] = {
00072
00073 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
00074 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
00075 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
00076 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
00077 };
00078
00079
00080 extern "C" uint8_t mpeg2_scan_norm[64];
00081 extern "C" uint8_t mpeg2_scan_alt[64];
00082 uint8_t mpeg2_scan_norm[64];
00083 uint8_t mpeg2_scan_alt[64];
00084
00085
00086
00087 #define W1 2841
00088 #define W2 2676
00089 #define W3 2408
00090 #define W5 1609
00091 #define W6 1108
00092 #define W7 565
00093
00094
00095
00096
00097
00098
00099
00100 static uint8_t mpeg2_clip[3840 * 2 + 256];
00101 #define CLIP(i) ((mpeg2_clip + 3840)[i])
00102
00103 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
00104 { \
00105 int tmp = W0 * (d0 + d1); \
00106 t0 = tmp + (W1 - W0) * d1; \
00107 t1 = tmp - (W1 + W0) * d0; \
00108 }
00109
00110 static void __inline idct_row(int16_t* block)
00111 {
00112 int d0, d1, d2, d3;
00113 int a0, a1, a2, a3, b0, b1, b2, b3;
00114 int t0, t1, t2, t3;
00115
00116
00117 if(!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] | ((int32_t *)block)[3]))
00118 {
00119 uint32_t tmp = (uint16_t) (block[0] << 3);
00120 tmp |= tmp << 16;
00121 ((int32_t *)block)[0] = tmp;
00122 ((int32_t *)block)[1] = tmp;
00123 ((int32_t *)block)[2] = tmp;
00124 ((int32_t *)block)[3] = tmp;
00125 return;
00126 }
00127
00128 d0 = (block[0] << 11) + 128;
00129 d1 = block[1];
00130 d2 = block[2] << 11;
00131 d3 = block[3];
00132 t0 = d0 + d2;
00133 t1 = d0 - d2;
00134 BUTTERFLY(t2, t3, W6, W2, d3, d1);
00135 a0 = t0 + t2;
00136 a1 = t1 + t3;
00137 a2 = t1 - t3;
00138 a3 = t0 - t2;
00139
00140 d0 = block[4];
00141 d1 = block[5];
00142 d2 = block[6];
00143 d3 = block[7];
00144 BUTTERFLY(t0, t1, W7, W1, d3, d0);
00145 BUTTERFLY(t2, t3, W3, W5, d1, d2);
00146 b0 = t0 + t2;
00147 b3 = t1 + t3;
00148 t0 -= t2;
00149 t1 -= t3;
00150 b1 = ((t0 + t1) * 181) >> 8;
00151 b2 = ((t0 - t1) * 181) >> 8;
00152
00153 block[0] = (a0 + b0) >> 8;
00154 block[1] = (a1 + b1) >> 8;
00155 block[2] = (a2 + b2) >> 8;
00156 block[3] = (a3 + b3) >> 8;
00157 block[4] = (a3 - b3) >> 8;
00158 block[5] = (a2 - b2) >> 8;
00159 block[6] = (a1 - b1) >> 8;
00160 block[7] = (a0 - b0) >> 8;
00161 }
00162
00163 static void __inline idct_col(int16_t* block)
00164 {
00165 int d0, d1, d2, d3;
00166 int a0, a1, a2, a3, b0, b1, b2, b3;
00167 int t0, t1, t2, t3;
00168
00169 d0 = (block[8*0] << 11) + 65536;
00170 d1 = block[8*1];
00171 d2 = block[8*2] << 11;
00172 d3 = block[8*3];
00173 t0 = d0 + d2;
00174 t1 = d0 - d2;
00175 BUTTERFLY(t2, t3, W6, W2, d3, d1);
00176 a0 = t0 + t2;
00177 a1 = t1 + t3;
00178 a2 = t1 - t3;
00179 a3 = t0 - t2;
00180
00181 d0 = block[8*4];
00182 d1 = block[8*5];
00183 d2 = block[8*6];
00184 d3 = block[8*7];
00185 BUTTERFLY(t0, t1, W7, W1, d3, d0);
00186 BUTTERFLY(t2, t3, W3, W5, d1, d2);
00187 b0 = t0 + t2;
00188 b3 = t1 + t3;
00189 t0 = (t0 - t2) >> 8;
00190 t1 = (t1 - t3) >> 8;
00191 b1 = (t0 + t1) * 181;
00192 b2 = (t0 - t1) * 181;
00193
00194 block[8*0] = (a0 + b0) >> 17;
00195 block[8*1] = (a1 + b1) >> 17;
00196 block[8*2] = (a2 + b2) >> 17;
00197 block[8*3] = (a3 + b3) >> 17;
00198 block[8*4] = (a3 - b3) >> 17;
00199 block[8*5] = (a2 - b2) >> 17;
00200 block[8*6] = (a1 - b1) >> 17;
00201 block[8*7] = (a0 - b0) >> 17;
00202 }
00203
00204 static void mpeg2_idct_copy_c(int16_t* block, uint8_t* dest, const int stride)
00205 {
00206 for(int i = 0; i < 8; i++) idct_row(block + 8 * i);
00207 for(int i = 0; i < 8; i++) idct_col(block + i);
00208 for(int i = 0; i < 8; i++)
00209 {
00210 dest[0] = CLIP(block[0]);
00211 dest[1] = CLIP(block[1]);
00212 dest[2] = CLIP(block[2]);
00213 dest[3] = CLIP(block[3]);
00214 dest[4] = CLIP(block[4]);
00215 dest[5] = CLIP(block[5]);
00216 dest[6] = CLIP(block[6]);
00217 dest[7] = CLIP(block[7]);
00218
00219 block[0] = 0; block[1] = 0; block[2] = 0; block[3] = 0;
00220 block[4] = 0; block[5] = 0; block[6] = 0; block[7] = 0;
00221
00222 dest += stride;
00223 block += 8;
00224 }
00225 }
00226
00227 static void mpeg2_idct_add_c(const int last, int16_t* block, uint8_t* dest, const int stride)
00228 {
00229 if(last != 129 || (block[0] & 7) == 4)
00230 {
00231 for(int i = 0; i < 8; i++) idct_row(block + 8 * i);
00232 for(int i = 0; i < 8; i++) idct_col(block + i);
00233 for(int i = 0; i < 8; i++)
00234 {
00235 dest[0] = CLIP(block[0] + dest[0]);
00236 dest[1] = CLIP(block[1] + dest[1]);
00237 dest[2] = CLIP(block[2] + dest[2]);
00238 dest[3] = CLIP(block[3] + dest[3]);
00239 dest[4] = CLIP(block[4] + dest[4]);
00240 dest[5] = CLIP(block[5] + dest[5]);
00241 dest[6] = CLIP(block[6] + dest[6]);
00242 dest[7] = CLIP(block[7] + dest[7]);
00243
00244 block[0] = 0; block[1] = 0; block[2] = 0; block[3] = 0;
00245 block[4] = 0; block[5] = 0; block[6] = 0; block[7] = 0;
00246
00247 dest += stride;
00248 block += 8;
00249 }
00250 }
00251 else
00252 {
00253 int DC = (block[0] + 4) >> 3;
00254 block[0] = block[63] = 0;
00255 for(int i = 0; i < 8; i++)
00256 {
00257 dest[0] = CLIP(DC + dest[0]);
00258 dest[1] = CLIP(DC + dest[1]);
00259 dest[2] = CLIP(DC + dest[2]);
00260 dest[3] = CLIP(DC + dest[3]);
00261 dest[4] = CLIP(DC + dest[4]);
00262 dest[5] = CLIP(DC + dest[5]);
00263 dest[6] = CLIP(DC + dest[6]);
00264 dest[7] = CLIP(DC + dest[7]);
00265 dest += stride;
00266 }
00267 }
00268 }
00269
00270 static void mpeg2_idct_init_c()
00271 {
00272 for(int i = -3840; i < 3840 + 256; i++)
00273 {
00274 CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
00275 }
00276
00277
00278
00279 for(int i = 0; i < 64; i++)
00280 {
00281 mpeg2_scan_norm_2[i] = ((mpeg2_scan_norm_2[i] & 0x36) >> 1) | ((mpeg2_scan_norm_2[i] & 0x09) << 2);
00282 mpeg2_scan_alt_2[i] = ((mpeg2_scan_alt_2[i] & 0x36) >> 1) | ((mpeg2_scan_alt_2[i] & 0x09) << 2);
00283 }
00284 }
00285
00286
00287
00288 #define avg2(a,b) ((a+b+1)>>1)
00289 #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
00290
00291 #define predict_o(i) (ref[i])
00292 #define predict_x(i) (avg2(ref[i], ref[i+1]))
00293 #define predict_y(i) (avg2(ref[i], (ref+stride)[i]))
00294 #define predict_xy(i) (avg4(ref[i], ref[i+1], (ref+stride)[i], (ref+stride)[i+1]))
00295
00296 #define put(predictor,i) dest[i] = predictor(i)
00297 #define avg(predictor,i) dest[i] = avg2(predictor(i), dest[i])
00298
00299
00300
00301 #define MC_FUNC(op,xy) \
00302 static void MC_##op##_##xy##_16_c (uint8_t* dest, const uint8_t* ref, const int stride, int height) \
00303 { \
00304 do { \
00305 op (predict_##xy, 0); \
00306 op (predict_##xy, 1); \
00307 op (predict_##xy, 2); \
00308 op (predict_##xy, 3); \
00309 op (predict_##xy, 4); \
00310 op (predict_##xy, 5); \
00311 op (predict_##xy, 6); \
00312 op (predict_##xy, 7); \
00313 op (predict_##xy, 8); \
00314 op (predict_##xy, 9); \
00315 op (predict_##xy, 10); \
00316 op (predict_##xy, 11); \
00317 op (predict_##xy, 12); \
00318 op (predict_##xy, 13); \
00319 op (predict_##xy, 14); \
00320 op (predict_##xy, 15); \
00321 ref += stride; \
00322 dest += stride; \
00323 } while (--height); \
00324 } \
00325 static void MC_##op##_##xy##_8_c (uint8_t * dest, const uint8_t * ref, const int stride, int height) \
00326 { \
00327 do { \
00328 op (predict_##xy, 0); \
00329 op (predict_##xy, 1); \
00330 op (predict_##xy, 2); \
00331 op (predict_##xy, 3); \
00332 op (predict_##xy, 4); \
00333 op (predict_##xy, 5); \
00334 op (predict_##xy, 6); \
00335 op (predict_##xy, 7); \
00336 ref += stride; \
00337 dest += stride; \
00338 } while (--height); \
00339 }
00340
00341
00342
00343 MC_FUNC(put,o)
00344 MC_FUNC(avg,o)
00345 MC_FUNC(put,x)
00346 MC_FUNC(avg,x)
00347 MC_FUNC(put,y)
00348 MC_FUNC(avg,y)
00349 MC_FUNC(put,xy)
00350 MC_FUNC(avg,xy)
00351
00352 #define MPEG2_MC_EXTERN(x) mpeg2_mc_t mpeg2_mc_##x = { \
00353 {MC_put_o_16_##x, MC_put_x_16_##x, MC_put_y_16_##x, MC_put_xy_16_##x, \
00354 MC_put_o_8_##x, MC_put_x_8_##x, MC_put_y_8_##x, MC_put_xy_8_##x}, \
00355 {MC_avg_o_16_##x, MC_avg_x_16_##x, MC_avg_y_16_##x, MC_avg_xy_16_##x, \
00356 MC_avg_o_8_##x, MC_avg_x_8_##x, MC_avg_y_8_##x, MC_avg_xy_8_##x} \
00357 };
00358
00359 MPEG2_MC_EXTERN(c)
00360
00361
00362
00363 extern "C" void mpeg2_idct_copy_mmx(int16_t* block, uint8_t* dest, const int stride);
00364 extern "C" void mpeg2_idct_add_mmx(const int last, int16_t* block, uint8_t* dest, const int stride);
00365
00366 static void mpeg2_idct_init_mmx()
00367 {
00368 for(int i = 0; i < 64; i++)
00369 {
00370 mpeg2_scan_norm_2[i] = (mpeg2_scan_norm_2[i] & 0x38) | ((mpeg2_scan_norm_2[i] & 6) >> 1) | ((mpeg2_scan_norm_2[i] & 1) << 2);
00371 mpeg2_scan_alt_2[i] = (mpeg2_scan_alt_2[i] & 0x38) | ((mpeg2_scan_alt_2[i] & 6) >> 1) | ((mpeg2_scan_alt_2[i] & 1) << 2);
00372 }
00373 }
00374
00375
00376
00377 extern "C" mpeg2_mc_t mpeg2_mc_mmx;
00378
00379
00380
00381 extern void mpeg2_idct_init_sse2();
00382 extern void mpeg2_idct_copy_sse2(int16_t* block, uint8_t* dest, const int stride);
00383 extern void mpeg2_idct_add_sse2(const int last, int16_t* block, uint8_t* dest, const int stride);
00384
00385
00386
00387 extern mpeg2_mc_t mpeg2_mc_sse2;
00388
00389
00390
00391 static void mpeg2_idct_init_null() {}
00392 static void mpeg2_idct_copy_null(int16_t* block, uint8_t* dest, const int stride) {}
00393 static void mpeg2_idct_add_null(const int last, int16_t* block, uint8_t* dest, const int stride) {}
00394
00395
00396
00397 static void MC_null(uint8_t* dest, const uint8_t* ref, const int stride, int height) {}
00398
00399 mpeg2_mc_t mpeg2_mc_null =
00400 {
00401 {MC_null, MC_null, MC_null, MC_null, MC_null, MC_null, MC_null, MC_null},
00402 {MC_null, MC_null, MC_null, MC_null, MC_null, MC_null, MC_null, MC_null}
00403 };
00404
00405
00406
00407 CMpeg2Dec::CMpeg2Dec()
00408 {
00409 m_shift = 0;
00410 m_is_display_initialized = 0;
00411 m_action = NULL;
00412 m_state = STATE_BUFFER;
00413 m_ext_state = 0;
00414
00415 m_chunk_buffer = m_chunk_start = m_chunk_ptr = NULL;
00416 m_code = 0;
00417
00418 m_pts_current = m_pts_previous = 0;
00419 m_num_pts = m_bytes_since_pts = 0;
00420
00421 m_first = 0;
00422 m_alloc_index = 0;
00423 m_first_decode_slice = m_nb_decode_slices = 0;
00424
00425 memset(&m_new_sequence, 0, sizeof(m_new_sequence));
00426 memset(&m_sequence, 0, sizeof(m_sequence));
00427 memset(&m_gop, 0, sizeof(m_gop));
00428 memset(&m_pictures, 0, sizeof(m_pictures));
00429 m_picture = NULL;
00430 memset(&m_fbuf, 0, sizeof(m_fbuf));
00431 memset(&m_fbuf_alloc, 0, sizeof(m_fbuf_alloc));
00432
00433 m_buf_start = m_buf_end = NULL;
00434
00435 m_display_offset_x = m_display_offset_y = 0;
00436
00437 m_copy_matrix = 0;
00438 memset(&m_intra_quantizer_matrix, 0, sizeof(m_intra_quantizer_matrix));
00439 memset(&m_non_intra_quantizer_matrix, 0, sizeof(m_non_intra_quantizer_matrix));
00440
00441
00442
00443 mpeg2_init();
00444 }
00445
00446 CMpeg2Dec::~CMpeg2Dec()
00447 {
00448 mpeg2_close();
00449 }
00450
00451 void CMpeg2Dec::mpeg2_init()
00452 {
00453 m_chunk_buffer = (uint8_t*)_aligned_malloc(BUFFER_SIZE + 4, 16);
00454 m_shift = 0xffffff00;
00455 m_code = 0xb4;
00456 m_action = &CMpeg2Dec::mpeg2_seek_sequence;
00457 m_sequence.width = (unsigned)-1;
00458 }
00459
00460 void CMpeg2Dec::mpeg2_close()
00461 {
00462
00463
00464
00465 mpeg2_header_state_init();
00466 _aligned_free(m_chunk_buffer);
00467 }
00468
00469
00470
00471 int CMpeg2Dec::skip_chunk(int bytes)
00472 {
00473 if(!bytes)
00474 return 0;
00475
00476 int len = 0;
00477
00478 uint8_t* current = m_buf_start;
00479 uint8_t* limit = current + bytes;
00480
00481 while(current < limit)
00482 {
00483 if(m_shift == 0x00000100)
00484 {
00485 m_shift = 0xffffff00;
00486 len = ++current - m_buf_start;
00487 break;
00488 }
00489
00490 m_shift = (m_shift | *current++) << 8;
00491 }
00492
00493 m_buf_start = current;
00494
00495 return len;
00496 }
00497
00498 int CMpeg2Dec::copy_chunk(int bytes)
00499 {
00500 if(!bytes)
00501 return 0;
00502
00503 int len = 0;
00504
00505
00506
00507
00508
00509 __asm
00510 {
00511 mov ebx, this
00512 mov esi, [ebx].m_buf_start
00513 mov edi, [ebx].m_chunk_ptr
00514 mov ecx, bytes
00515 mov edx, [ebx].m_shift
00516
00517 copy_chunk_loop:
00518
00519 cmp edx, 0x00000100
00520 jne copy_chunk_continue
00521 mov edx, 0xffffff00
00522
00523 inc edi
00524 mov [ebx].m_chunk_ptr, edi
00525
00526 inc esi
00527 mov eax, esi
00528 sub eax, [ebx].m_buf_start
00529 mov len, eax
00530
00531 jmp copy_chunk_end
00532
00533 copy_chunk_continue:
00534
00535 movzx eax, byte ptr [esi]
00536 or edx, eax
00537 shl edx, 8
00538 mov byte ptr [edi], al
00539 inc esi
00540 inc edi
00541 dec ecx
00542 jnz copy_chunk_loop
00543
00544 copy_chunk_end:
00545
00546 mov [ebx].m_buf_start, esi
00547 mov [ebx].m_shift, edx
00548 }
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569 return len;
00570 }
00571
00572 mpeg2_state_t CMpeg2Dec::seek_chunk()
00573 {
00574 int size = m_buf_end - m_buf_start;
00575
00576 if(int skipped = skip_chunk(size))
00577 {
00578 m_bytes_since_pts += skipped;
00579 m_code = m_buf_start[-1];
00580 return (mpeg2_state_t)-1;
00581 }
00582
00583 m_bytes_since_pts += size;
00584 return STATE_BUFFER;
00585 }
00586
00587 mpeg2_state_t CMpeg2Dec::seek_header()
00588 {
00589 while(m_code != 0xb3 && (m_code != 0xb7 && m_code != 0xb8 && m_code || m_sequence.width == (unsigned)-1))
00590 {
00591 if(seek_chunk() == STATE_BUFFER)
00592 return STATE_BUFFER;
00593 }
00594
00595 m_chunk_start = m_chunk_ptr = m_chunk_buffer;
00596
00597 return m_code
00598 ? mpeg2_parse_header()
00599 : mpeg2_header_picture_start();
00600 }
00601
00602 mpeg2_state_t CMpeg2Dec::seek_sequence()
00603 {
00604 mpeg2_header_state_init();
00605 m_action = &CMpeg2Dec::seek_header;
00606 return seek_header();
00607 }
00608
00609
00610
00611 void CMpeg2Dec::mpeg2_buffer(uint8_t* start, uint8_t* end)
00612 {
00613 m_buf_start = start;
00614 m_buf_end = end;
00615 }
00616
00617 int CMpeg2Dec::mpeg2_getpos()
00618 {
00619 return m_buf_end - m_buf_start;
00620 }
00621
00622
00623
00624 mpeg2_state_t CMpeg2Dec::mpeg2_parse()
00625 {
00626 if(m_action)
00627 {
00628 mpeg2_state_t state = (this->*m_action)();
00629 if((int)state >= 0)
00630 return state;
00631 }
00632
00633 while(1)
00634 {
00635 while((unsigned)(m_code - m_first_decode_slice) < m_nb_decode_slices)
00636 {
00637 int size_buffer = m_buf_end - m_buf_start;
00638 int size_chunk = (m_chunk_buffer + BUFFER_SIZE - m_chunk_ptr);
00639 int copied;
00640
00641 if(size_buffer <= size_chunk)
00642 {
00643 copied = copy_chunk(size_buffer);
00644 if(!copied)
00645 {
00646 m_bytes_since_pts += size_buffer;
00647 m_chunk_ptr += size_buffer;
00648 return STATE_BUFFER;
00649 }
00650 }
00651 else
00652 {
00653 copied = copy_chunk(size_chunk);
00654 if(!copied)
00655 {
00656
00657 m_bytes_since_pts += size_chunk;
00658 m_action = &CMpeg2Dec::seek_chunk;
00659 return STATE_INVALID;
00660 }
00661 }
00662
00663 m_bytes_since_pts += copied;
00664
00665 m_decoder.mpeg2_slice(m_code, m_chunk_start);
00666 m_code = m_buf_start[-1];
00667 m_chunk_ptr = m_chunk_start;
00668 }
00669
00670 if((unsigned)(m_code - 1) >= 0xb0 - 1)
00671 break;
00672 if(seek_chunk() == STATE_BUFFER)
00673 return STATE_BUFFER;
00674 }
00675
00676 switch(m_code)
00677 {
00678 case 0x00:
00679 m_action = &CMpeg2Dec::mpeg2_header_picture_start;
00680 return m_state;
00681 case 0xb7:
00682 m_action = &CMpeg2Dec::mpeg2_header_end;
00683 break;
00684 case 0xb3:
00685 case 0xb8:
00686 m_action = &CMpeg2Dec::mpeg2_parse_header;
00687 break;
00688 case 0xbe:
00689 m_action = &CMpeg2Dec::seek_chunk;
00690 return STATE_PADDING;
00691 default:
00692 m_action = &CMpeg2Dec::seek_chunk;
00693 return STATE_INVALID;
00694 }
00695
00696 if(m_state != STATE_SLICE)
00697 m_state = STATE_INVALID;
00698
00699 return m_state;
00700 }
00701
00702
00703 void CMpeg2Dec::mpeg2_skip(int skip)
00704 {
00705 m_first_decode_slice = 1;
00706 m_nb_decode_slices = skip ? 0 : (0xb0 - 1);
00707 }
00708
00709 void CMpeg2Dec::mpeg2_slice_region(int start, int end)
00710 {
00711 start = (start < 1) ? 1 : (start > 0xb0) ? 0xb0 : start;
00712 end = (end < start) ? start : (end > 0xb0) ? 0xb0 : end;
00713 m_first_decode_slice = start;
00714 m_nb_decode_slices = end - start;
00715 }
00716
00717 void CMpeg2Dec::mpeg2_pts(uint32_t pts)
00718 {
00719 m_pts_previous = m_pts_current;
00720 m_pts_current = pts;
00721 m_num_pts++;
00722 m_bytes_since_pts = 0;
00723 }
00724
00725
00726
00727
00728
00729 #define RECEIVED(code,state) (((state) << 8) + (code))
00730
00731 mpeg2_state_t CMpeg2Dec::mpeg2_seek_sequence()
00732 {
00733 mpeg2_header_state_init();
00734 m_action = &CMpeg2Dec::seek_header;
00735 return seek_header();
00736 }
00737
00738 mpeg2_state_t CMpeg2Dec::mpeg2_parse_header()
00739 {
00740 static int (CMpeg2Dec::* process_header[]) () =
00741 {
00742 &CMpeg2Dec::mpeg2_header_picture,
00743 &CMpeg2Dec::mpeg2_header_extension,
00744 &CMpeg2Dec::mpeg2_header_user_data,
00745 &CMpeg2Dec::mpeg2_header_sequence,
00746 NULL, NULL, NULL, NULL,
00747 &CMpeg2Dec::mpeg2_header_gop
00748 };
00749
00750 m_action = &CMpeg2Dec::mpeg2_parse_header;
00751
00752 while(1)
00753 {
00754 int size_buffer = m_buf_end - m_buf_start;
00755 int size_chunk = (m_chunk_buffer + BUFFER_SIZE - m_chunk_ptr);
00756 int copied;
00757 if(size_buffer <= size_chunk)
00758 {
00759 copied = copy_chunk(size_buffer);
00760 if(!copied)
00761 {
00762 m_bytes_since_pts += size_buffer;
00763 m_chunk_ptr += size_buffer;
00764 return STATE_BUFFER;
00765 }
00766 }
00767 else
00768 {
00769 copied = copy_chunk(size_chunk);
00770 if(!copied)
00771 {
00772
00773 m_bytes_since_pts += size_chunk;
00774 m_code = 0xb4;
00775 m_action = &CMpeg2Dec::seek_header;
00776 return STATE_INVALID;
00777 }
00778 }
00779 m_bytes_since_pts += copied;
00780
00781 if((this->*(process_header[m_code & 0x0b]))())
00782 {
00783 m_code = m_buf_start[-1];
00784 m_action = &CMpeg2Dec::seek_header;
00785 return STATE_INVALID;
00786 }
00787
00788 m_code = m_buf_start[-1];
00789
00790 switch(RECEIVED(m_code, m_state))
00791 {
00792
00793 case RECEIVED(0x00, STATE_SEQUENCE):
00794 m_action = &CMpeg2Dec::mpeg2_header_picture_start;
00795 case RECEIVED(0xb8, STATE_SEQUENCE):
00796 mpeg2_header_sequence_finalize();
00797 break;
00798
00799
00800 case RECEIVED (0x00, STATE_GOP):
00801 m_action = &CMpeg2Dec::mpeg2_header_picture_start;
00802 break;
00803 case RECEIVED (0x01, STATE_PICTURE):
00804 case RECEIVED (0x01, STATE_PICTURE_2ND):
00805 mpeg2_header_matrix_finalize();
00806 m_action = &CMpeg2Dec::mpeg2_header_slice_start;
00807 break;
00808
00809
00810 case RECEIVED (0xb2, STATE_SEQUENCE):
00811 case RECEIVED (0xb2, STATE_GOP):
00812 case RECEIVED (0xb2, STATE_PICTURE):
00813 case RECEIVED (0xb2, STATE_PICTURE_2ND):
00814 case RECEIVED (0xb5, STATE_SEQUENCE):
00815 case RECEIVED (0xb5, STATE_PICTURE):
00816 case RECEIVED (0xb5, STATE_PICTURE_2ND):
00817 m_chunk_ptr = m_chunk_start;
00818 continue;
00819
00820 default:
00821 m_action = &CMpeg2Dec::seek_header;
00822 return STATE_INVALID;
00823 }
00824
00825 m_chunk_start = m_chunk_ptr = m_chunk_buffer;
00826 return m_state;
00827 }
00828 }
00829
00830
00831
00832 void CMpeg2Dec::mpeg2_header_state_init()
00833 {
00834 if(m_sequence.width != (unsigned)-1)
00835 {
00836 m_sequence.width = (unsigned)-1;
00837 for(int i = 0; i < m_alloc_index; i++)
00838 _aligned_free(m_fbuf_alloc[i].buf[0]);
00839 }
00840
00841 m_decoder.m_scan = mpeg2_scan_norm_2;
00842 m_picture = m_pictures;
00843 m_fbuf[0] = &m_fbuf_alloc[0];
00844 m_fbuf[1] = &m_fbuf_alloc[1];
00845 m_fbuf[2] = &m_fbuf_alloc[2];
00846 m_first = true;
00847 m_alloc_index = 0;
00848 m_first_decode_slice = 1;
00849 m_nb_decode_slices = 0xb0 - 1;
00850 }
00851
00852 int CMpeg2Dec::mpeg2_header_sequence()
00853 {
00854 uint8_t* buffer = m_chunk_start;
00855 mpeg2_sequence_t* sequence = &m_new_sequence;
00856 static unsigned int frame_period[9] = {0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000};
00857
00858 if((buffer[6] & 0x20) != 0x20)
00859 return 1;
00860
00861 int i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
00862 if(!(sequence->display_width = sequence->picture_width = i >> 12))
00863 return 1;
00864 if(!(sequence->display_height = sequence->picture_height = i & 0xfff))
00865 return 1;
00866
00867 sequence->width = (sequence->picture_width + 15) & ~15;
00868 sequence->height = (sequence->picture_height + 15) & ~15;
00869 sequence->chroma_width = sequence->width >> 1;
00870 sequence->chroma_height = sequence->height >> 1;
00871 sequence->flags = (SEQ_FLAG_PROGRESSIVE_SEQUENCE | SEQ_VIDEO_FORMAT_UNSPECIFIED);
00872 sequence->pixel_width = buffer[3] >> 4;
00873 sequence->frame_period = 0;
00874 if((buffer[3] & 15) < 9) sequence->frame_period = frame_period[buffer[3] & 15];
00875 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6);
00876 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800;
00877 if(buffer[7] & 4) sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS;
00878
00879 m_copy_matrix = 3;
00880 if(buffer[7] & 2)
00881 {
00882 for(i = 0; i < 64; i++)
00883 m_intra_quantizer_matrix[mpeg2_scan_norm_2[i]] = (buffer[i+7] << 7) | (buffer[i+8] >> 1);
00884 buffer += 64;
00885 }
00886 else
00887 {
00888 for (i = 0; i < 64; i++)
00889 m_intra_quantizer_matrix[mpeg2_scan_norm_2[i]] = default_intra_quantizer_matrix[i];
00890 }
00891
00892 if(buffer[7] & 1)
00893 {
00894 for(i = 0; i < 64; i++)
00895 m_non_intra_quantizer_matrix[mpeg2_scan_norm_2[i]] = buffer[i+8];
00896 }
00897 else
00898 {
00899 for(i = 0; i < 64; i++)
00900 m_non_intra_quantizer_matrix[i] = 16;
00901 }
00902
00903 sequence->profile_level_id = 0x80;
00904 sequence->colour_primaries = 0;
00905 sequence->transfer_characteristics = 0;
00906 sequence->matrix_coefficients = 0;
00907
00908 m_ext_state = SEQ_EXT;
00909 m_state = STATE_SEQUENCE;
00910 m_display_offset_x = m_display_offset_y = 0;
00911
00912 m_info.Reset();
00913 m_info.m_gop = NULL;
00914
00915 return 0;
00916 }
00917
00918 int CMpeg2Dec::mpeg2_header_gop()
00919 {
00920 uint8_t* buffer = m_chunk_start;
00921 m_info.Reset();
00922 if(!(buffer[1] & 8))
00923 return 1;
00924 m_info.m_gop = &m_gop;
00925 m_gop.hours = (buffer[0] >> 2) & 31;
00926 m_gop.minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63;
00927 m_gop.seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63;
00928 m_gop.pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63;
00929 m_gop.flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6);
00930 m_state = STATE_GOP;
00931 return 0;
00932 }
00933
00934 mpeg2_state_t CMpeg2Dec::mpeg2_header_picture_start()
00935 {
00936 {
00937 mpeg2_picture_t* picture;
00938
00939 if(m_state != STATE_SLICE_1ST)
00940 {
00941 m_state = STATE_PICTURE;
00942 picture = m_pictures;
00943 if((m_decoder.m_coding_type != PIC_FLAG_CODING_TYPE_B) ^ (m_picture >= m_pictures + 2))
00944 picture += 2;
00945 }
00946 else
00947 {
00948 m_state = STATE_PICTURE_2ND;
00949 picture = m_picture + 1;
00950 }
00951
00952 m_picture = picture;
00953 }
00954
00955 m_picture->flags = 0;
00956
00957 if(m_num_pts)
00958 {
00959 if(m_bytes_since_pts >= 4)
00960 {
00961 m_num_pts = 0;
00962 m_picture->pts = m_pts_current;
00963 m_picture->flags = PIC_FLAG_PTS;
00964 }
00965 else if(m_num_pts > 1)
00966 {
00967 m_num_pts = 1;
00968 m_picture->pts = m_pts_previous;
00969 m_picture->flags = PIC_FLAG_PTS;
00970 }
00971 }
00972
00973 m_picture->display_offset[0].x =
00974 m_picture->display_offset[1].x =
00975 m_picture->display_offset[2].x = m_display_offset_x;
00976 m_picture->display_offset[0].y =
00977 m_picture->display_offset[1].y =
00978 m_picture->display_offset[2].y = m_display_offset_y;
00979
00980 return mpeg2_parse_header();
00981 }
00982
00983 int CMpeg2Dec::mpeg2_header_picture()
00984 {
00985 uint8_t* buffer = m_chunk_start;
00986 mpeg2_picture_t* picture = m_picture;
00987 int type;
00988 int low_delay;
00989
00990 type = (buffer [1] >> 3) & 7;
00991 low_delay = m_sequence.flags & SEQ_FLAG_LOW_DELAY;
00992
00993 if(m_state == STATE_PICTURE)
00994 {
00995 mpeg2_picture_t* other;
00996
00997 m_decoder.m_second_field = 0;
00998 other = m_pictures;
00999 if(other == picture)
01000 other += 2;
01001 if(m_decoder.m_coding_type != PIC_FLAG_CODING_TYPE_B)
01002 {
01003 m_fbuf[2] = m_fbuf[1];
01004 m_fbuf[1] = m_fbuf[0];
01005 }
01006 m_fbuf[0] = NULL;
01007 m_info.Reset();
01008 m_info.m_current_picture = picture;
01009 m_info.m_display_picture = picture;
01010 if(type != PIC_FLAG_CODING_TYPE_B)
01011 {
01012 if(!low_delay)
01013 {
01014 if(m_first) {
01015 m_info.m_display_picture = NULL;
01016 m_first = false;
01017 }
01018 else
01019 {
01020 m_info.m_display_picture = other;
01021 if(other->nb_fields == 1)
01022 m_info.m_display_picture_2nd = other + 1;
01023 m_info.m_display_fbuf = m_fbuf[1];
01024 }
01025 }
01026
01027 if(!low_delay + !NULL)
01028 m_info.m_discard_fbuf = m_fbuf[!low_delay + !NULL];
01029 }
01030
01031 while(m_alloc_index < 3)
01032 {
01033 mpeg2_fbuf_t* fbuf = &m_fbuf_alloc[m_alloc_index++];
01034 fbuf->id = NULL;
01035
01036 int size = m_decoder.m_width * m_decoder.m_height;
01037 fbuf->buf[0] = (uint8_t*)_aligned_malloc(6 * size >> 2, 16);
01038 fbuf->buf[1] = fbuf->buf[0] + size;
01039 fbuf->buf[2] = fbuf->buf[1] + (size >> 2);
01040 memset(fbuf->buf[0], 0x10, size);
01041 memset(fbuf->buf[1], 0x80, size >> 2);
01042 memset(fbuf->buf[2], 0x80, size >> 2);
01043 }
01044 mpeg2_set_fbuf(type);
01045 }
01046 else
01047 {
01048 m_decoder.m_second_field = 1;
01049 m_info.m_current_picture_2nd = picture;
01050 m_info.m_user_data = NULL; m_info.m_user_data_len = 0;
01051 if(low_delay || type == PIC_FLAG_CODING_TYPE_B)
01052 m_info.m_display_picture_2nd = picture;
01053 }
01054 m_ext_state = PIC_CODING_EXT;
01055
01056 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);
01057
01058 m_decoder.m_coding_type = type;
01059 picture->flags |= type;
01060
01061 if(type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B)
01062 {
01063
01064 m_decoder.m_f_motion.f_code[1] = (buffer[3] >> 2) & 1;
01065 m_decoder.m_f_motion.f_code[0] = (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
01066 m_decoder.m_b_motion.f_code[1] = (buffer[4] >> 6) & 1;
01067 m_decoder.m_b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
01068 }
01069
01070
01071
01072 picture->nb_fields = 2;
01073
01074 m_decoder.m_intra_dc_precision = 0;
01075 m_decoder.m_frame_pred_frame_dct = 1;
01076 m_decoder.m_q_scale_type = 0;
01077 m_decoder.m_concealment_motion_vectors = 0;
01078 m_decoder.m_scan = mpeg2_scan_norm_2;
01079 m_decoder.m_picture_structure = FRAME_PICTURE;
01080 m_copy_matrix = 0;
01081
01082 return 0;
01083 }
01084
01085 int CMpeg2Dec::mpeg2_header_extension()
01086 {
01087 static int (CMpeg2Dec::* parser[]) () =
01088 {
01089 NULL,
01090 &CMpeg2Dec::sequence_ext,
01091 &CMpeg2Dec::sequence_display_ext,
01092 &CMpeg2Dec::quant_matrix_ext,
01093 &CMpeg2Dec::copyright_ext,
01094 NULL, NULL,
01095 &CMpeg2Dec::picture_display_ext,
01096 &CMpeg2Dec::picture_coding_ext
01097 };
01098
01099 int ext, ext_bit;
01100
01101 ext = m_chunk_start[0] >> 4;
01102 ext_bit = 1 << ext;
01103
01104 if(!(m_ext_state & ext_bit))
01105 return 0;
01106 m_ext_state &= ~ext_bit;
01107 return (this->*parser[ext])();
01108 }
01109
01110 int CMpeg2Dec::mpeg2_header_user_data()
01111 {
01112 if(!m_info.m_user_data_len) m_info.m_user_data = m_chunk_start;
01113 else m_info.m_user_data_len += 3;
01114
01115 m_info.m_user_data_len += (m_chunk_ptr - 4 - m_chunk_start);
01116 m_chunk_start = m_chunk_ptr - 1;
01117
01118 return 0;
01119 }
01120
01121 void CMpeg2Dec::mpeg2_header_matrix_finalize()
01122 {
01123 if(m_copy_matrix & 1)
01124 memcpy(m_decoder.m_intra_quantizer_matrix, m_intra_quantizer_matrix, 64);
01125
01126 if(m_copy_matrix & 2)
01127 memcpy(m_decoder.m_non_intra_quantizer_matrix, m_non_intra_quantizer_matrix, 64);
01128 }
01129
01130 void mpeg2_sequence_t::finalize()
01131 {
01132 int w, h;
01133
01134 byte_rate *= 50;
01135
01136 if(flags & SEQ_FLAG_MPEG2)
01137 {
01138 switch(pixel_width)
01139 {
01140 case 1:
01141 pixel_width = pixel_height = 1; return;
01142 case 2:
01143 w = 4; h = 3; break;
01144 case 3:
01145 w = 16; h = 9; break;
01146 case 4:
01147 w = 221; h = 100; break;
01148 default:
01149 pixel_width = pixel_height = 0; return;
01150 }
01151
01152 w *= display_height;
01153 h *= display_width;
01154 }
01155 else
01156 {
01157 if(byte_rate == 50 * 0x3ffff)
01158 byte_rate = 0;
01159
01160 switch(pixel_width)
01161 {
01162 case 0: case 15:
01163 pixel_width = pixel_height = 0; return;
01164 case 1:
01165 pixel_width = pixel_height = 1; return;
01166 case 3:
01167 pixel_width = 64; pixel_height = 45; return;
01168 case 6:
01169 pixel_width = 32; pixel_height = 27; return;
01170 case 12:
01171 pixel_width = 8; pixel_height = 9; return;
01172 default:
01173 h = 88 * pixel_width + 1171;
01174 w = 2000;
01175 }
01176 }
01177
01178 pixel_width = w;
01179 pixel_height = h;
01180
01181
01182 while(w) {int tmp = w; w = h % tmp; h = tmp;}
01183
01184 pixel_width /= h;
01185 pixel_height /= h;
01186 }
01187
01188 void CMpeg2Dec::mpeg2_header_sequence_finalize()
01189 {
01190 mpeg2_sequence_t* sequence = &m_new_sequence;
01191
01192 sequence->finalize();
01193
01194 mpeg2_header_matrix_finalize();
01195
01196 m_decoder.m_mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2);
01197 m_decoder.m_width = sequence->width;
01198 m_decoder.m_height = sequence->height;
01199 m_decoder.m_vertical_position_extension = (sequence->picture_height > 2800);
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211 m_sequence.byte_rate = sequence->byte_rate;
01212 if(!memcmp(&m_sequence, sequence, FIELD_OFFSET(mpeg2_sequence_t, colour_primaries) ))
01213 {
01214 m_state = STATE_SEQUENCE_REPEATED;
01215 }
01216 else if(m_sequence.width != (unsigned)-1)
01217 {
01218 m_action = &CMpeg2Dec::mpeg2_seek_sequence;
01219 m_state = STATE_INVALID;
01220 return;
01221 }
01222 m_sequence = *sequence;
01223
01224 m_info.m_sequence = &m_sequence;
01225 }
01226
01227 mpeg2_state_t CMpeg2Dec::mpeg2_header_slice_start()
01228 {
01229 m_info.m_user_data = NULL;
01230 m_info.m_user_data_len = 0;
01231 m_state = (m_picture->nb_fields > 1 || m_state == STATE_PICTURE_2ND) ? STATE_SLICE : STATE_SLICE_1ST;
01232
01233 if(!m_nb_decode_slices)
01234 {
01235 m_picture->flags |= PIC_FLAG_SKIP;
01236 }
01237 else
01238 {
01239 int b_type = m_decoder.m_coding_type == B_TYPE;
01240 m_decoder.mpeg2_init_fbuf(m_fbuf[0]->buf, m_fbuf[b_type + 1]->buf, m_fbuf[b_type]->buf);
01241 }
01242
01243 m_action = NULL;
01244
01245 return (mpeg2_state_t)-1;
01246 }
01247
01248 mpeg2_state_t CMpeg2Dec::mpeg2_header_end()
01249 {
01250 mpeg2_picture_t* picture = m_pictures;
01251
01252 int b_type = m_decoder.m_coding_type == B_TYPE;
01253
01254 if((m_picture >= picture + 2) ^ b_type)
01255 picture = m_pictures + 2;
01256
01257 m_state = STATE_END;
01258 m_info.Reset();
01259
01260 if(!(m_sequence.flags & SEQ_FLAG_LOW_DELAY))
01261 {
01262 m_info.m_display_picture = picture;
01263 if(picture->nb_fields == 1)
01264 m_info.m_display_picture_2nd = picture + 1;
01265 m_info.m_display_fbuf = m_fbuf[b_type];
01266 m_info.m_discard_fbuf = m_fbuf[b_type + 1];
01267 }
01268 else
01269 {
01270 m_info.m_discard_fbuf = m_fbuf[b_type];
01271 }
01272
01273 m_action = &CMpeg2Dec::mpeg2_seek_sequence;
01274 m_first = true;
01275
01276 return STATE_END;
01277 }
01278
01279 void CMpeg2Dec::mpeg2_set_fbuf(int coding_type)
01280 {
01281 for(int i = 0; i < 3; i++)
01282 {
01283 if(m_fbuf[1] != &m_fbuf_alloc[i] && m_fbuf[2] != &m_fbuf_alloc[i])
01284 {
01285 m_fbuf[0] = &m_fbuf_alloc[i];
01286 m_info.m_current_fbuf = m_fbuf[0];
01287 if(coding_type == B_TYPE || (m_sequence.flags & SEQ_FLAG_LOW_DELAY))
01288 {
01289 if(coding_type == B_TYPE)
01290 m_info.m_discard_fbuf = m_fbuf[0];
01291 m_info.m_display_fbuf = m_fbuf[0];
01292 }
01293 break;
01294 }
01295 }
01296 }
01297
01298
01299
01300 int CMpeg2Dec::sequence_ext()
01301 {
01302 uint8_t* buffer = m_chunk_start;
01303 mpeg2_sequence_t* sequence = &m_new_sequence;
01304
01305 if(!(buffer[3]&1))
01306 return 1;
01307
01308 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4);
01309
01310 sequence->display_width =
01311 sequence->picture_width += ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000;
01312 sequence->display_height =
01313 sequence->picture_height += (buffer[2] << 7) & 0x3000;
01314
01315 sequence->width = (sequence->picture_width + 15) & ~15;
01316 sequence->height = (sequence->picture_height + 15) & ~15;
01317
01318 sequence->flags |= SEQ_FLAG_MPEG2;
01319
01320 if(!(buffer[1] & 8))
01321 {
01322 sequence->flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE;
01323 sequence->height = (sequence->height + 31) & ~31;
01324 }
01325
01326 if(buffer[5] & 0x80)
01327 {
01328 sequence->flags |= SEQ_FLAG_LOW_DELAY;
01329 }
01330
01331 sequence->chroma_width = sequence->width;
01332 sequence->chroma_height = sequence->height;
01333
01334 switch(buffer[1] & 6)
01335 {
01336 case 0:
01337 return 1;
01338 case 2:
01339 sequence->chroma_height >>= 1;
01340 case 4:
01341 sequence->chroma_width >>= 1;
01342 }
01343
01344 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000;
01345
01346 sequence->vbv_buffer_size |= buffer[4] << 21;
01347
01348 sequence->frame_period =
01349 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>2)&3)+1);
01350
01351 m_ext_state = SEQ_DISPLAY_EXT;
01352
01353 return 0;
01354 }
01355
01356 int CMpeg2Dec::sequence_display_ext()
01357 {
01358 uint8_t* buffer = m_chunk_start;
01359 mpeg2_sequence_t* sequence = &m_new_sequence;
01360
01361 uint32_t flags = (sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT);
01362 if(buffer[0] & 1)
01363 {
01364 flags |= SEQ_FLAG_COLOUR_DESCRIPTION;
01365 sequence->colour_primaries = buffer[1];
01366 sequence->transfer_characteristics = buffer[2];
01367 sequence->matrix_coefficients = buffer[3];
01368 buffer += 3;
01369 }
01370
01371 if(!(buffer[2] & 2))
01372 return 1;
01373
01374
01375
01376
01377 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2);
01378 sequence->display_height = ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3);
01379
01380 return 0;
01381 }
01382
01383 int CMpeg2Dec::quant_matrix_ext()
01384 {
01385 uint8_t* buffer = m_chunk_start;
01386
01387 if(buffer[0] & 8)
01388 {
01389 for(int i = 0; i < 64; i++)
01390 m_intra_quantizer_matrix[mpeg2_scan_norm_2[i]] = (buffer[i] << 5) | (buffer[i+1] >> 3);
01391 m_copy_matrix |= 1;
01392 buffer += 64;
01393 }
01394
01395 if(buffer[0] & 4)
01396 {
01397 for(int i = 0; i < 64; i++)
01398 m_non_intra_quantizer_matrix[mpeg2_scan_norm_2[i]] = (buffer[i] << 6) | (buffer[i+1] >> 2);
01399 m_copy_matrix |= 2;
01400 }
01401
01402 return 0;
01403 }
01404
01405 int CMpeg2Dec::copyright_ext()
01406 {
01407 return 0;
01408 }
01409
01410 int CMpeg2Dec::picture_display_ext()
01411 {
01412 uint8_t* buffer = m_chunk_start;
01413 mpeg2_picture_t* picture = m_picture;
01414
01415 int nb_pos = picture->nb_fields;
01416 if(m_sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)
01417 nb_pos >>= 1;
01418
01419 int i = 0;
01420
01421 for(; i < nb_pos; i++)
01422 {
01423 int x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) |
01424 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i);
01425 int y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) |
01426 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i);
01427 if(!(x&y&1))
01428 return 1;
01429
01430 picture->display_offset[i].x = m_display_offset_x = x >> 1;
01431 picture->display_offset[i].y = m_display_offset_y = y >> 1;
01432 }
01433
01434 for(; i < 3; i++)
01435 {
01436 picture->display_offset[i].x = m_display_offset_x;
01437 picture->display_offset[i].y = m_display_offset_y;
01438 }
01439
01440 return 0;
01441 }
01442
01443 int CMpeg2Dec::picture_coding_ext()
01444 {
01445 uint8_t* buffer = m_chunk_start;
01446 mpeg2_picture_t* picture = m_picture;
01447
01448
01449 m_decoder.m_f_motion.f_code[0] = (buffer[0] & 15) - 1;
01450 m_decoder.m_f_motion.f_code[1] = (buffer[1] >> 4) - 1;
01451 m_decoder.m_b_motion.f_code[0] = (buffer[1] & 15) - 1;
01452 m_decoder.m_b_motion.f_code[1] = (buffer[2] >> 4) - 1;
01453
01454 m_decoder.m_intra_dc_precision = (buffer[2] >> 2) & 3;
01455 m_decoder.m_picture_structure = buffer[2] & 3;
01456 switch(m_decoder.m_picture_structure)
01457 {
01458 case TOP_FIELD:
01459 picture->flags |= PIC_FLAG_TOP_FIELD_FIRST;
01460 case BOTTOM_FIELD:
01461 picture->nb_fields = 1;
01462 break;
01463 case FRAME_PICTURE:
01464 if(!(m_sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE))
01465 {
01466 picture->nb_fields = (buffer[3] & 2) ? 3 : 2;
01467 picture->flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0;
01468 }
01469 else
01470 {
01471 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2;
01472 }
01473 break;
01474 default:
01475 return 1;
01476 }
01477
01478 m_decoder.m_top_field_first = buffer[3] >> 7;
01479 m_decoder.m_frame_pred_frame_dct = (buffer[3] >> 6) & 1;
01480 m_decoder.m_concealment_motion_vectors = (buffer[3] >> 5) & 1;
01481 m_decoder.m_q_scale_type = (buffer[3] >> 4) & 1;
01482 m_decoder.m_intra_vlc_format = (buffer[3] >> 3) & 1;
01483 m_decoder.m_scan = (buffer[3] & 4) ? mpeg2_scan_alt_2 : mpeg2_scan_norm_2;
01484
01485 if(buffer[3] & 2)
01486 picture->flags |= PIC_FLAG_REPEAT_FIRST_FIELD;
01487
01488 picture->flags |= (buffer[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME : 0;
01489 if(buffer[4] & 0x40)
01490 picture->flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) & PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY;
01491
01492 m_ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT;
01493
01494 return 0;
01495 }
01496
01499
01500
01501
01502 #define GETWORD(bit_buf,shift,bit_ptr) \
01503 do { \
01504 bit_buf |= ((bit_ptr[0] << 8) | bit_ptr[1]) << (shift); \
01505 bit_ptr += 2; \
01506 } while (0)
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517 #define NEEDBITS \
01518 do { \
01519 if (bits > 0) { \
01520 GETWORD (bit_buf, bits, bit_ptr); \
01521 bits -= 16; \
01522 } \
01523 } while (0)
01524
01525
01526 #define DUMPBITS(num) \
01527 do { \
01528 bit_buf <<= (num); \
01529 bits += (num); \
01530 } while (0)
01531
01532
01533 #define UBITS(bit_buf,num) (((uint32_t)(bit_buf)) >> (32 - (num)))
01534
01535
01536 #define SBITS(bit_buf,num) (((int32_t)(bit_buf)) >> (32 - (num)))
01537
01538 typedef struct {
01539 uint8_t modes;
01540 uint8_t len;
01541 } MBtab;
01542
01543 typedef struct {
01544 uint8_t delta;
01545 uint8_t len;
01546 } MVtab;
01547
01548 typedef struct {
01549 int8_t dmv;
01550 uint8_t len;
01551 } DMVtab;
01552
01553 typedef struct {
01554 uint8_t cbp;
01555 uint8_t len;
01556 } CBPtab;
01557
01558 typedef struct {
01559 uint8_t size;
01560 uint8_t len;
01561 } DCtab;
01562
01563 typedef struct {
01564 uint8_t run;
01565 uint8_t level;
01566 uint8_t len;
01567 } DCTtab;
01568
01569 typedef struct {
01570 uint8_t mba;
01571 uint8_t len;
01572 } MBAtab;
01573
01574
01575 #define INTRA MACROBLOCK_INTRA
01576 #define QUANT MACROBLOCK_QUANT
01577
01578 static const MBtab MB_I [] = {
01579 {INTRA|QUANT, 2}, {INTRA, 1}
01580 };
01581
01582 #define MC MACROBLOCK_MOTION_FORWARD
01583 #define CODED MACROBLOCK_PATTERN
01584
01585 static const MBtab MB_P [] = {
01586 {INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA, 5},
01587 {MC, 3}, {MC, 3}, {MC, 3}, {MC, 3},
01588 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
01589 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
01590 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
01591 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
01592 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
01593 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}
01594 };
01595
01596 #define FWD MACROBLOCK_MOTION_FORWARD
01597 #define BWD MACROBLOCK_MOTION_BACKWARD
01598 #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
01599
01600 static const MBtab MB_B [] = {
01601 {0, 0}, {INTRA|QUANT, 6},
01602 {BWD|CODED|QUANT, 6}, {FWD|CODED|QUANT, 6},
01603 {INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5},
01604 {INTRA, 5}, {INTRA, 5},
01605 {FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD, 4},
01606 {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4},
01607 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
01608 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
01609 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
01610 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
01611 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
01612 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
01613 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
01614 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
01615 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
01616 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
01617 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
01618 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}
01619 };
01620
01621 #undef INTRA
01622 #undef QUANT
01623 #undef MC
01624 #undef CODED
01625 #undef FWD
01626 #undef BWD
01627 #undef INTER
01628
01629
01630 static const MVtab MV_4 [] = {
01631 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
01632 };
01633
01634 static const MVtab MV_10 [] = {
01635 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
01636 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
01637 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
01638 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
01639 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
01640 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
01641 };
01642
01643
01644 static const DMVtab DMV_2 [] = {
01645 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
01646 };
01647
01648
01649 static const CBPtab CBP_7 [] = {
01650 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
01651 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
01652 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
01653 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
01654 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
01655 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
01656 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
01657 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
01658 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
01659 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
01660 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
01661 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
01662 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
01663 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
01664 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
01665 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
01666 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
01667 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
01668 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
01669 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
01670 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
01671 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
01672 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
01673 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
01674 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
01675 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
01676 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
01677 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
01678 };
01679
01680 static const CBPtab CBP_9 [] = {
01681 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
01682 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
01683 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
01684 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
01685 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
01686 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
01687 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
01688 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
01689 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
01690 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
01691 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
01692 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
01693 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
01694 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
01695 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
01696 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
01697 };
01698
01699
01700 static const DCtab DC_lum_5 [] = {
01701 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
01702 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
01703 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
01704 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
01705 };
01706
01707 static const DCtab DC_chrom_5 [] = {
01708 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
01709 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
01710 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
01711 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
01712 };
01713
01714 static const DCtab DC_long [] = {
01715 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
01716 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
01717 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
01718 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
01719 };
01720
01721
01722 static const DCTtab DCT_16 [] = {
01723 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
01724 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
01725 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
01726 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
01727 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
01728 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
01729 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
01730 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
01731 };
01732
01733 static const DCTtab DCT_15 [] = {
01734 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
01735 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
01736 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
01737 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
01738 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
01739 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
01740 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
01741 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
01742 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
01743 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
01744 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
01745 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
01746 };
01747
01748 static const DCTtab DCT_13 [] = {
01749 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
01750 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
01751 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
01752 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
01753 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
01754 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
01755 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
01756 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
01757 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
01758 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
01759 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
01760 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
01761 };
01762
01763 static const DCTtab DCT_B14_10 [] = {
01764 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
01765 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
01766 };
01767
01768 static const DCTtab DCT_B14_8 [] = {
01769 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
01770 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
01771 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
01772 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
01773 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
01774 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
01775 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
01776 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
01777 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
01778 };
01779
01780 static const DCTtab DCT_B14AC_5 [] = {
01781 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
01782 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
01783 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01784 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
01785 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
01786 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01787 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
01788 };
01789
01790 static const DCTtab DCT_B14DC_5 [] = {
01791 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
01792 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
01793 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01794 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
01795 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
01796 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
01797 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
01798 };
01799
01800 static const DCTtab DCT_B15_10 [] = {
01801 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
01802 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
01803 };
01804
01805 static const DCTtab DCT_B15_8 [] = {
01806 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
01807 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
01808 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
01809 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
01810 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
01811 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
01812 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
01813 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
01814 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
01815 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
01816 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
01817 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
01818 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
01819 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
01820 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
01821 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01822 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01823 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01824 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01825 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01826 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01827 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01828 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
01829 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
01830 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
01831 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
01832 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
01833 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
01834 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
01835 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
01836 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
01837 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01838 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01839 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01840 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01841 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01842 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01843 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01844 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01845 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01846 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01847 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01848 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01849 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01850 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01851 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01852 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
01853 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01854 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01855 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01856 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01857 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01858 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01859 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01860 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
01861 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
01862 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
01863 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
01864 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
01865 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
01866 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
01867 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
01868 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
01869 };
01870
01871
01872 static const MBAtab MBA_5 [] = {
01873 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
01874 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
01875 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
01876 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
01877 };
01878
01879 static const MBAtab MBA_11 [] = {
01880 {32, 11}, {31, 11}, {30, 11}, {29, 11},
01881 {28, 11}, {27, 11}, {26, 11}, {25, 11},
01882 {24, 11}, {23, 11}, {22, 11}, {21, 11},
01883 {20, 10}, {20, 10}, {19, 10}, {19, 10},
01884 {18, 10}, {18, 10}, {17, 10}, {17, 10},
01885 {16, 10}, {16, 10}, {15, 10}, {15, 10},
01886 {14, 8}, {14, 8}, {14, 8}, {14, 8},
01887 {14, 8}, {14, 8}, {14, 8}, {14, 8},
01888 {13, 8}, {13, 8}, {13, 8}, {13, 8},
01889 {13, 8}, {13, 8}, {13, 8}, {13, 8},
01890 {12, 8}, {12, 8}, {12, 8}, {12, 8},
01891 {12, 8}, {12, 8}, {12, 8}, {12, 8},
01892 {11, 8}, {11, 8}, {11, 8}, {11, 8},
01893 {11, 8}, {11, 8}, {11, 8}, {11, 8},
01894 {10, 8}, {10, 8}, {10, 8}, {10, 8},
01895 {10, 8}, {10, 8}, {10, 8}, {10, 8},
01896 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
01897 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
01898 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
01899 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
01900 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
01901 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
01902 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
01903 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
01904 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
01905 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
01906 };
01907
01910
01911 static int non_linear_quantizer_scale [] = {
01912 0, 1, 2, 3, 4, 5, 6, 7,
01913 8, 10, 12, 14, 16, 18, 20, 22,
01914 24, 28, 32, 36, 40, 44, 48, 52,
01915 56, 64, 72, 80, 88, 96, 104, 112
01916 };
01917
01918 bool CMpeg2Decoder::m_idct_initialized = false;
01919
01920 CMpeg2Decoder::CMpeg2Decoder()
01921 {
01922 memset(&m_b_motion, 0, sizeof(m_b_motion));
01923 memset(&m_f_motion, 0, sizeof(m_f_motion));
01924
01925 m_DCTblock = (int16_t*)_aligned_malloc(64*sizeof(int16_t), 16);
01926 memset(m_DCTblock, 0, 64*sizeof(int16_t));
01927
01928 m_bitstream_buf = 0;
01929 m_bitstream_bits = 0;
01930 m_bitstream_ptr = NULL;
01931
01932 memset(&m_dest, 0, sizeof(m_dest));
01933 memset(&m_picture_dest, 0, sizeof(m_picture_dest));
01934
01935 m_offset = m_stride = m_uv_stride = 0;
01936 m_limit_x = m_limit_y_16 = m_limit_y_8 = m_limit_y = 0;
01937
01938 memset(&m_dc_dct_pred, 0, sizeof(m_dc_dct_pred));
01939
01940 m_quantizer_scale = m_dmv_offset = 0;
01941 m_v_offset = 0;
01942
01943 memset(&m_intra_quantizer_matrix, 0, sizeof(m_intra_quantizer_matrix));
01944 memset(&m_non_intra_quantizer_matrix, 0, sizeof(m_non_intra_quantizer_matrix));
01945
01946 m_width = m_height = 0;
01947 m_vertical_position_extension = 0;
01948
01949 m_coding_type = 0;
01950
01951 m_intra_dc_precision = 0;
01952 m_picture_structure = 0;
01953 m_frame_pred_frame_dct = 0;
01954 m_concealment_motion_vectors = 0;
01955 m_q_scale_type = 0;
01956 m_intra_vlc_format = 0;
01957 m_top_field_first = 0;
01958
01959 m_scan = NULL;
01960
01961 m_second_field = 0;
01962
01963 m_mpeg1 = 0;
01964
01965
01966
01967 if(g_cpuid.m_flags&CCpuID::sse2)
01968 {
01969 m_idct_init = mpeg2_idct_init_sse2;
01970 m_idct_copy = mpeg2_idct_copy_sse2;
01971 m_idct_add = mpeg2_idct_add_sse2;
01972 m_mc = &mpeg2_mc_sse2;
01973 }
01974 else if(g_cpuid.m_flags&CCpuID::mmx)
01975 {
01976 m_idct_init = mpeg2_idct_init_mmx;
01977 m_idct_copy = mpeg2_idct_copy_mmx;
01978 m_idct_add = mpeg2_idct_add_mmx;
01979 m_mc = &mpeg2_mc_mmx;
01980 }
01981 else
01982 {
01983 m_idct_init = mpeg2_idct_init_c;
01984 m_idct_copy = mpeg2_idct_copy_c;
01985 m_idct_add = mpeg2_idct_add_c;
01986 m_mc = &mpeg2_mc_c;
01987 }
01988
01989
01990
01991
01992
01993
01994 if(!m_idct_initialized)
01995 {
01996 m_idct_init();
01997 m_idct_initialized = true;
01998 }
01999 }
02000
02001 CMpeg2Decoder::~CMpeg2Decoder()
02002 {
02003 if(m_DCTblock) _aligned_free(m_DCTblock);
02004 }
02005
02006 #define bit_buf (m_bitstream_buf)
02007 #define bits (m_bitstream_bits)
02008 #define bit_ptr (m_bitstream_ptr)
02009
02010 int CMpeg2Decoder::get_macroblock_modes()
02011 {
02012 int macroblock_modes;
02013 const MBtab* tab;
02014
02015 switch(m_coding_type)
02016 {
02017 case P_TYPE:
02018 tab = MB_P + UBITS(bit_buf, 5);
02019 DUMPBITS(tab->len);
02020 macroblock_modes = tab->modes;
02021
02022 if(m_picture_structure != FRAME_PICTURE)
02023 {
02024 if(macroblock_modes & MACROBLOCK_MOTION_FORWARD)
02025 {
02026 macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
02027 DUMPBITS(2);
02028 }
02029
02030 return macroblock_modes;
02031 }
02032 else if(m_frame_pred_frame_dct)
02033 {
02034 if(macroblock_modes & MACROBLOCK_MOTION_FORWARD)
02035 macroblock_modes |= MC_FRAME;
02036
02037 return macroblock_modes;
02038 }
02039
02040 if(macroblock_modes & MACROBLOCK_MOTION_FORWARD)
02041 {
02042 macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
02043 DUMPBITS(2);
02044 }
02045
02046 if(macroblock_modes & (MACROBLOCK_INTRA|MACROBLOCK_PATTERN))
02047 {
02048 macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
02049 DUMPBITS(1);
02050 }
02051
02052 return macroblock_modes;
02053
02054 case B_TYPE:
02055 tab = MB_B + UBITS(bit_buf, 6);
02056 DUMPBITS(tab->len);
02057 macroblock_modes = tab->modes;
02058
02059 if(m_picture_structure != FRAME_PICTURE)
02060 {
02061 if(!(macroblock_modes & MACROBLOCK_INTRA))
02062 {
02063 macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
02064 DUMPBITS(2);
02065 }
02066
02067 return macroblock_modes;
02068 }
02069 else if(m_frame_pred_frame_dct)
02070 {
02071
02072 macroblock_modes |= MC_FRAME;
02073
02074 return macroblock_modes;
02075 }
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090 if(!(macroblock_modes & MACROBLOCK_INTRA))
02091 {
02092 macroblock_modes |= UBITS(bit_buf, 2) * MOTION_TYPE_BASE;
02093 DUMPBITS(2);
02094 }
02095
02096 if(macroblock_modes & (MACROBLOCK_INTRA|MACROBLOCK_PATTERN))
02097 {
02098 macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
02099 DUMPBITS(1);
02100 }
02101
02102 return macroblock_modes;
02103
02104 case I_TYPE:
02105 tab = MB_I + UBITS(bit_buf, 1);
02106 DUMPBITS(tab->len);
02107 macroblock_modes = tab->modes;
02108
02109 if(!m_frame_pred_frame_dct && m_picture_structure == FRAME_PICTURE)
02110 {
02111 macroblock_modes |= UBITS(bit_buf, 1) * DCT_TYPE_INTERLACED;
02112 DUMPBITS(1);
02113 }
02114
02115 return macroblock_modes;
02116
02117 case D_TYPE:
02118
02119 DUMPBITS(1);
02120 return MACROBLOCK_INTRA;
02121 }
02122
02123 return 0;
02124 }
02125
02126 int CMpeg2Decoder::get_quantizer_scale()
02127 {
02128 int quantizer_scale_code = UBITS(bit_buf, 5);
02129 DUMPBITS(5);
02130
02131 return m_q_scale_type
02132 ? non_linear_quantizer_scale[quantizer_scale_code]
02133 : (quantizer_scale_code << 1);
02134 }
02135
02136 int CMpeg2Decoder::get_motion_delta(const int f_code)
02137 {
02138 int delta;
02139 int sign;
02140 const MVtab* tab;
02141
02142 if(bit_buf & 0x80000000)
02143 {
02144 DUMPBITS(1);
02145 return 0;
02146 }
02147 else if(bit_buf >= 0x0c000000)
02148 {
02149 tab = MV_4 + UBITS(bit_buf, 4);
02150 delta = (tab->delta << f_code) + 1;
02151 bits += tab->len + f_code + 1;
02152 bit_buf <<= tab->len;
02153
02154 sign = SBITS(bit_buf, 1);
02155 bit_buf <<= 1;
02156
02157 if(f_code)
02158 {
02159 delta += UBITS(bit_buf, f_code);
02160 }
02161
02162 bit_buf <<= f_code;
02163
02164 return (delta ^ sign) - sign;
02165 }
02166 else
02167 {
02168 tab = MV_10 + UBITS(bit_buf, 10);
02169 delta = (tab->delta << f_code) + 1;
02170 bits += tab->len + 1;
02171 bit_buf <<= tab->len;
02172
02173 sign = SBITS(bit_buf, 1);
02174 bit_buf <<= 1;
02175
02176 if(f_code)
02177 {
02178 NEEDBITS;
02179 delta += UBITS(bit_buf, f_code);
02180 DUMPBITS(f_code);
02181 }
02182
02183 return (delta ^ sign) - sign;
02184 }
02185 }
02186
02187 int CMpeg2Decoder::bound_motion_vector(const int vector, const int f_code)
02188 {
02189 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
02190 }
02191
02192 int CMpeg2Decoder::get_dmv()
02193 {
02194 const DMVtab* tab = DMV_2 + UBITS(bit_buf, 2);
02195 DUMPBITS(tab->len);
02196 return tab->dmv;
02197 }
02198
02199 int CMpeg2Decoder::get_coded_block_pattern()
02200 {
02201 const CBPtab* tab;
02202
02203 NEEDBITS;
02204
02205 if(bit_buf >= 0x20000000)
02206 {
02207 tab = CBP_7 + (UBITS(bit_buf, 7) - 16);
02208 DUMPBITS(tab->len);
02209 return tab->cbp;
02210 }
02211 else
02212 {
02213 tab = CBP_9 + UBITS(bit_buf, 9);
02214 DUMPBITS(tab->len);
02215 return tab->cbp;
02216 }
02217 }
02218
02219 int CMpeg2Decoder::get_luma_dc_dct_diff()
02220 {
02221 const DCtab* tab;
02222 int size;
02223 int dc_diff;
02224
02225 if(bit_buf < 0xf8000000)
02226 {
02227 tab = DC_lum_5 + UBITS(bit_buf, 5);
02228 size = tab->size;
02229 if(size)
02230 {
02231 bits += tab->len + size;
02232 bit_buf <<= tab->len;
02233 dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
02234 bit_buf <<= size;
02235 return dc_diff;
02236 }
02237 else
02238 {
02239 DUMPBITS(3);
02240 return 0;
02241 }
02242 }
02243 else
02244 {
02245 tab = DC_long + (UBITS(bit_buf, 9) - 0x1e0);
02246 size = tab->size;
02247 DUMPBITS(tab->len);
02248 NEEDBITS;
02249 dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
02250 DUMPBITS(size);
02251 return dc_diff;
02252 }
02253 }
02254
02255 int CMpeg2Decoder::get_chroma_dc_dct_diff()
02256 {
02257 const DCtab* tab;
02258 int size;
02259 int dc_diff;
02260
02261 if(bit_buf < 0xf8000000)
02262 {
02263 tab = DC_chrom_5 + UBITS(bit_buf, 5);
02264 size = tab->size;
02265
02266 if(size)
02267 {
02268 bits += tab->len + size;
02269 bit_buf <<= tab->len;
02270 dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
02271 bit_buf <<= size;
02272 return dc_diff;
02273 }
02274 else
02275 {
02276 DUMPBITS(2);
02277 return 0;
02278 }
02279 }
02280 else
02281 {
02282 tab = DC_long + (UBITS(bit_buf, 10) - 0x3e0);
02283 size = tab->size;
02284 DUMPBITS(tab->len + 1);
02285 NEEDBITS;
02286 dc_diff = UBITS(bit_buf, size) - UBITS(SBITS(~bit_buf, 1), size);
02287 DUMPBITS(size);
02288 return dc_diff;
02289 }
02290 }
02291
02292 #undef bit_buf
02293 #undef bits
02294 #undef bit_ptr
02295
02296 #define SATURATE(val) \
02297 do { \
02298 if((uint32_t)(val + 2048) > 4095) \
02299 val = SBITS(val, 1) ^ 2047; \
02300 } while (0)
02301
02302 void CMpeg2Decoder::get_intra_block_B14()
02303 {
02304 int i, j;
02305 int val;
02306 const uint8_t* scan = m_scan;
02307 const uint8_t* quant_matrix = m_intra_quantizer_matrix;
02308 int quantizer_scale = m_quantizer_scale;
02309 int mismatch;
02310 const DCTtab* tab;
02311 uint32_t bit_buf;
02312 int bits;
02313 const uint8_t* bit_ptr;
02314 int16_t* dest;
02315
02316 dest = m_DCTblock;
02317 i = 0;
02318 mismatch = ~dest[0];
02319
02320 bit_buf = m_bitstream_buf;
02321 bits = m_bitstream_bits;
02322 bit_ptr = m_bitstream_ptr;
02323
02324 NEEDBITS;
02325
02326 while(1)
02327 {
02328 if(bit_buf >= 0x28000000)
02329 {
02330 tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
02331
02332 i += tab->run;
02333 if(i >= 64)
02334 break;
02335
02336 normal_code:
02337 j = scan[i];
02338 bit_buf <<= tab->len;
02339 bits += tab->len + 1;
02340 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
02341
02342
02343 val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
02344
02345 SATURATE(val);
02346 dest[j] = val;
02347 mismatch ^= val;
02348
02349 bit_buf <<= 1;
02350 NEEDBITS;
02351
02352 continue;
02353 }
02354 else if(bit_buf >= 0x04000000)
02355 {
02356 tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
02357
02358 i += tab->run;
02359 if(i < 64)
02360 goto normal_code;
02361
02362
02363
02364 i += UBITS(bit_buf << 6, 6) - 64;
02365 if(i >= 64)
02366 break;
02367
02368 j = scan[i];
02369
02370 DUMPBITS(12);
02371 NEEDBITS;
02372 val = (SBITS(bit_buf, 12) * quantizer_scale * quant_matrix[j]) / 16;
02373
02374 SATURATE(val);
02375 dest[j] = val;
02376 mismatch ^= val;
02377
02378 DUMPBITS(12);
02379 NEEDBITS;
02380
02381 continue;
02382 }
02383 else if(bit_buf >= 0x02000000)
02384 {
02385 tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
02386 i += tab->run;
02387 if(i < 64)
02388 goto normal_code;
02389 }
02390 else if(bit_buf >= 0x00800000)
02391 {
02392 tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
02393 i += tab->run;
02394 if(i < 64)
02395 goto normal_code;
02396 }
02397 else if(bit_buf >= 0x00200000)
02398 {
02399 tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
02400 i += tab->run;
02401 if(i < 64)
02402 goto normal_code;
02403 }
02404 else
02405 {
02406 tab = DCT_16 + UBITS(bit_buf, 16);
02407 bit_buf <<= 16;
02408 GETWORD (bit_buf, bits + 16, bit_ptr);
02409 i += tab->run;
02410 if(i < 64)
02411 goto normal_code;
02412 }
02413
02414 break;
02415 }
02416
02417 dest[63] ^= mismatch & 1;
02418 DUMPBITS(2);
02419 m_bitstream_buf = bit_buf;
02420 m_bitstream_bits = bits;
02421 m_bitstream_ptr = bit_ptr;
02422 }
02423
02424 void CMpeg2Decoder::get_intra_block_B15()
02425 {
02426 int i, j;
02427 int val;
02428 const uint8_t* scan = m_scan;
02429 const uint8_t* quant_matrix = m_intra_quantizer_matrix;
02430 int quantizer_scale = m_quantizer_scale;
02431 int mismatch;
02432 const DCTtab* tab;
02433 uint32_t bit_buf;
02434 int bits;
02435 const uint8_t* bit_ptr;
02436 int16_t* dest;
02437
02438 dest = m_DCTblock;
02439 i = 0;
02440 mismatch = ~dest[0];
02441
02442 bit_buf = m_bitstream_buf;
02443 bits = m_bitstream_bits;
02444 bit_ptr = m_bitstream_ptr;
02445
02446 NEEDBITS;
02447
02448 while(1)
02449 {
02450 if(bit_buf >= 0x04000000)
02451 {
02452 tab = DCT_B15_8 + (UBITS(bit_buf, 8) - 4);
02453
02454 i += tab->run;
02455 if(i < 64)
02456 {
02457 normal_code:
02458 j = scan[i];
02459 bit_buf <<= tab->len;
02460 bits += tab->len + 1;
02461 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
02462
02463
02464 val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
02465
02466 SATURATE(val);
02467 dest[j] = val;
02468 mismatch ^= val;
02469
02470 bit_buf <<= 1;
02471 NEEDBITS;
02472
02473 continue;
02474 }
02475 else
02476 {
02477
02478
02479
02480
02481
02482
02483
02484 i += UBITS(bit_buf << 6, 6) - 64;
02485 if(i >= 64)
02486 break;
02487
02488 j = scan[i];
02489
02490 DUMPBITS(12);
02491 NEEDBITS;
02492 val = (SBITS(bit_buf, 12) *
02493 quantizer_scale * quant_matrix[j]) / 16;
02494
02495 SATURATE(val);
02496 dest[j] = val;
02497 mismatch ^= val;
02498
02499 DUMPBITS(12);
02500 NEEDBITS;
02501
02502 continue;
02503 }
02504 }
02505 else if(bit_buf >= 0x02000000)
02506 {
02507 tab = DCT_B15_10 + (UBITS(bit_buf, 10) - 8);
02508 i += tab->run;
02509 if(i < 64)
02510 goto normal_code;
02511 }
02512 else if(bit_buf >= 0x00800000)
02513 {
02514 tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
02515 i += tab->run;
02516 if(i < 64)
02517 goto normal_code;
02518 }
02519 else if(bit_buf >= 0x00200000)
02520 {
02521 tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
02522 i += tab->run;
02523 if(i < 64)
02524 goto normal_code;
02525 }
02526 else
02527 {
02528 tab = DCT_16 + UBITS(bit_buf, 16);
02529 bit_buf <<= 16;
02530 GETWORD(bit_buf, bits + 16, bit_ptr);
02531 i += tab->run;
02532 if(i < 64)
02533 goto normal_code;
02534 }
02535
02536 break;
02537 }
02538
02539 dest[63] ^= mismatch & 1;
02540 DUMPBITS(4);
02541 m_bitstream_buf = bit_buf;
02542 m_bitstream_bits = bits;
02543 m_bitstream_ptr = bit_ptr;
02544 }
02545
02546 int CMpeg2Decoder::get_non_intra_block()
02547 {
02548 int i, j;
02549 int val;
02550 const uint8_t* scan = m_scan;
02551 const uint8_t* quant_matrix = m_non_intra_quantizer_matrix;
02552 int quantizer_scale = m_quantizer_scale;
02553 int mismatch;
02554 const DCTtab* tab;
02555 uint32_t bit_buf;
02556 int bits;
02557 const uint8_t* bit_ptr;
02558 int16_t* dest;
02559
02560 i = -1;
02561 mismatch = 1;
02562 dest = m_DCTblock;
02563
02564 bit_buf = m_bitstream_buf;
02565 bits = m_bitstream_bits;
02566 bit_ptr = m_bitstream_ptr;
02567
02568 NEEDBITS;
02569 if(bit_buf >= 0x28000000)
02570 {
02571 tab = DCT_B14DC_5 + (UBITS(bit_buf, 5) - 5);
02572 goto entry_1;
02573 }
02574 else
02575 {
02576 goto entry_2;
02577 }
02578
02579 while(1)
02580 {
02581 if(bit_buf >= 0x28000000)
02582 {
02583 tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
02584 entry_1:
02585 i += tab->run;
02586 if(i >= 64)
02587 break;
02588 normal_code:
02589 j = scan[i];
02590 bit_buf <<= tab->len;
02591 bits += tab->len + 1;
02592 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
02593
02594
02595 val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
02596
02597 SATURATE(val);
02598 dest[j] = val;
02599 mismatch ^= val;
02600
02601 bit_buf <<= 1;
02602 NEEDBITS;
02603
02604 continue;
02605 }
02606
02607 entry_2:
02608 if(bit_buf >= 0x04000000)
02609 {
02610 tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
02611
02612 i += tab->run;
02613 if(i < 64)
02614 goto normal_code;
02615
02616
02617
02618 i += UBITS(bit_buf << 6, 6) - 64;
02619 if(i >= 64)
02620 break;
02621
02622 j = scan[i];
02623
02624 DUMPBITS(12);
02625 NEEDBITS;
02626 val = 2 * (SBITS(bit_buf, 12) + SBITS(bit_buf, 1)) + 1;
02627 val = (val * quantizer_scale * quant_matrix[j]) / 32;
02628
02629 SATURATE(val);
02630 dest[j] = val;
02631 mismatch ^= val;
02632
02633 DUMPBITS(12);
02634 NEEDBITS;
02635
02636 continue;
02637 }
02638 else if(bit_buf >= 0x02000000)
02639 {
02640 tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
02641 i += tab->run;
02642 if(i < 64)
02643 goto normal_code;
02644 }
02645 else if(bit_buf >= 0x00800000)
02646 {
02647 tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
02648 i += tab->run;
02649 if(i < 64)
02650 goto normal_code;
02651 }
02652 else if(bit_buf >= 0x00200000)
02653 {
02654 tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
02655 i += tab->run;
02656 if(i < 64)
02657 goto normal_code;
02658 }
02659 else
02660 {
02661 tab = DCT_16 + UBITS(bit_buf, 16);
02662 bit_buf <<= 16;
02663 GETWORD (bit_buf, bits + 16, bit_ptr);
02664 i += tab->run;
02665 if(i < 64)
02666 goto normal_code;
02667 }
02668
02669 break;
02670 }
02671
02672 dest[63] ^= mismatch & 1;
02673 DUMPBITS(2);
02674 m_bitstream_buf = bit_buf;
02675 m_bitstream_bits = bits;
02676 m_bitstream_ptr = bit_ptr;
02677
02678 return i;
02679 }
02680
02681 void CMpeg2Decoder::get_mpeg1_intra_block()
02682 {
02683 int i, j;
02684 int val;
02685 const uint8_t* scan = m_scan;
02686 const uint8_t* quant_matrix = m_intra_quantizer_matrix;
02687 int quantizer_scale = m_quantizer_scale;
02688 const DCTtab* tab;
02689 uint32_t bit_buf;
02690 int bits;
02691 const uint8_t* bit_ptr;
02692 int16_t* dest;
02693
02694 i = 0;
02695 dest = m_DCTblock;
02696
02697 bit_buf = m_bitstream_buf;
02698 bits = m_bitstream_bits;
02699 bit_ptr = m_bitstream_ptr;
02700
02701 NEEDBITS;
02702
02703 while(1)
02704 {
02705 if(bit_buf >= 0x28000000)
02706 {
02707 tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
02708
02709 i += tab->run;
02710 if(i >= 64)
02711 break;
02712 normal_code:
02713 j = scan[i];
02714 bit_buf <<= tab->len;
02715 bits += tab->len + 1;
02716 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
02717
02718
02719 val = (val - 1) | 1;
02720
02721
02722 val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
02723
02724 SATURATE(val);
02725 dest[j] = val;
02726
02727 bit_buf <<= 1;
02728 NEEDBITS;
02729
02730 continue;
02731 }
02732 else if(bit_buf >= 0x04000000)
02733 {
02734 tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
02735
02736 i += tab->run;
02737 if(i < 64)
02738 goto normal_code;
02739
02740
02741
02742 i += UBITS(bit_buf << 6, 6) - 64;
02743 if(i >= 64)
02744 break;
02745
02746 j = scan[i];
02747
02748 DUMPBITS(12);
02749 NEEDBITS;
02750 val = SBITS(bit_buf, 8);
02751 if(!(val & 0x7f))
02752 {
02753 DUMPBITS(8);
02754 val = UBITS(bit_buf, 8) + 2 * val;
02755 }
02756 val = (val * quantizer_scale * quant_matrix[j]) / 16;
02757
02758
02759 val = (val + ~SBITS(val, 1)) | 1;
02760
02761 SATURATE(val);
02762 dest[j] = val;
02763
02764 DUMPBITS(8);
02765 NEEDBITS;
02766
02767 continue;
02768 }
02769 else if(bit_buf >= 0x02000000)
02770 {
02771 tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
02772 i += tab->run;
02773 if(i < 64)
02774 goto normal_code;
02775 }
02776 else if(bit_buf >= 0x00800000)
02777 {
02778 tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
02779 i += tab->run;
02780 if(i < 64)
02781 goto normal_code;
02782 }
02783 else if(bit_buf >= 0x00200000)
02784 {
02785 tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
02786 i += tab->run;
02787 if(i < 64)
02788 goto normal_code;
02789 }
02790 else
02791 {
02792 tab = DCT_16 + UBITS(bit_buf, 16);
02793 bit_buf <<= 16;
02794 GETWORD(bit_buf, bits + 16, bit_ptr);
02795 i += tab->run;
02796 if(i < 64)
02797 goto normal_code;
02798 }
02799
02800 break;
02801 }
02802
02803 DUMPBITS(2);
02804 m_bitstream_buf = bit_buf;
02805 m_bitstream_bits = bits;
02806 m_bitstream_ptr = bit_ptr;
02807 }
02808
02809 int CMpeg2Decoder::get_mpeg1_non_intra_block()
02810 {
02811 int i, j;
02812 int val;
02813 const uint8_t* scan = m_scan;
02814 const uint8_t* quant_matrix = m_non_intra_quantizer_matrix;
02815 int quantizer_scale = m_quantizer_scale;
02816 const DCTtab* tab;
02817 uint32_t bit_buf;
02818 int bits;
02819 const uint8_t* bit_ptr;
02820 int16_t* dest;
02821
02822 i = -1;
02823 dest = m_DCTblock;
02824
02825 bit_buf = m_bitstream_buf;
02826 bits = m_bitstream_bits;
02827 bit_ptr = m_bitstream_ptr;
02828
02829 NEEDBITS;
02830 if(bit_buf >= 0x28000000)
02831 {
02832 tab = DCT_B14DC_5 + (UBITS(bit_buf, 5) - 5);
02833 goto entry_1;
02834 }
02835 else
02836 {
02837 goto entry_2;
02838 }
02839
02840 while(1)
02841 {
02842 if(bit_buf >= 0x28000000)
02843 {
02844 tab = DCT_B14AC_5 + (UBITS(bit_buf, 5) - 5);
02845 entry_1:
02846 i += tab->run;
02847 if(i >= 64)
02848 break;
02849 normal_code:
02850 j = scan[i];
02851 bit_buf <<= tab->len;
02852 bits += tab->len + 1;
02853 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
02854
02855
02856 val = (val - 1) | 1;
02857
02858
02859 val = (val ^ SBITS(bit_buf, 1)) - SBITS(bit_buf, 1);
02860
02861 SATURATE(val);
02862 dest[j] = val;
02863
02864 bit_buf <<= 1;
02865 NEEDBITS;
02866
02867 continue;
02868 }
02869
02870 entry_2:
02871 if(bit_buf >= 0x04000000)
02872 {
02873 tab = DCT_B14_8 + (UBITS(bit_buf, 8) - 4);
02874
02875 i += tab->run;
02876 if(i < 64)
02877 goto normal_code;
02878
02879
02880
02881 i += UBITS(bit_buf << 6, 6) - 64;
02882 if(i >= 64)
02883 break;
02884
02885 j = scan[i];
02886
02887 DUMPBITS(12);
02888 NEEDBITS;
02889 val = SBITS(bit_buf, 8);
02890 if(!(val & 0x7f))
02891 {
02892 DUMPBITS(8);
02893 val = UBITS(bit_buf, 8) + 2 * val;
02894 }
02895 val = 2 * (val + SBITS(val, 1)) + 1;
02896 val = (val * quantizer_scale * quant_matrix[j]) / 32;
02897
02898
02899 val = (val + ~SBITS(val, 1)) | 1;
02900
02901 SATURATE(val);
02902 dest[j] = val;
02903
02904 DUMPBITS(8);
02905 NEEDBITS;
02906
02907 continue;
02908 }
02909 else if(bit_buf >= 0x02000000)
02910 {
02911 tab = DCT_B14_10 + (UBITS(bit_buf, 10) - 8);
02912 i += tab->run;
02913 if(i < 64)
02914 goto normal_code;
02915 }
02916 else if(bit_buf >= 0x00800000)
02917 {
02918 tab = DCT_13 + (UBITS(bit_buf, 13) - 16);
02919 i += tab->run;
02920 if(i < 64)
02921 goto normal_code;
02922 }
02923 else if(bit_buf >= 0x00200000)
02924 {
02925 tab = DCT_15 + (UBITS(bit_buf, 15) - 16);
02926 i += tab->run;
02927 if(i < 64)
02928 goto normal_code;
02929 }
02930 else
02931 {
02932 tab = DCT_16 + UBITS(bit_buf, 16);
02933 bit_buf <<= 16;
02934 GETWORD (bit_buf, bits + 16, bit_ptr);
02935 i += tab->run;
02936 if(i < 64)
02937 goto normal_code;
02938 }
02939
02940 break;
02941 }
02942
02943 DUMPBITS(2);
02944 m_bitstream_buf = bit_buf;
02945 m_bitstream_bits = bits;
02946 m_bitstream_ptr = bit_ptr;
02947
02948 return i;
02949 }
02950
02951 #define bit_buf (m_bitstream_buf)
02952 #define bits (m_bitstream_bits)
02953 #define bit_ptr (m_bitstream_ptr)
02954
02955 void CMpeg2Decoder::slice_intra_DCT(const int cc, uint8_t* dest, int stride)
02956 {
02957 NEEDBITS;
02958
02959
02960 m_dc_dct_pred[cc] += (cc == 0)
02961 ? get_luma_dc_dct_diff()
02962 : get_chroma_dc_dct_diff();
02963
02964 m_DCTblock[0] = m_dc_dct_pred[cc] << (3 - m_intra_dc_precision);
02965
02966 if(m_mpeg1)
02967 {
02968 if(m_coding_type != D_TYPE)
02969 get_mpeg1_intra_block();
02970 }
02971 else if(m_intra_vlc_format)
02972 {
02973 get_intra_block_B15();
02974 }
02975 else
02976 {
02977 get_intra_block_B14();
02978 }
02979
02980 m_idct_copy(m_DCTblock, dest, stride);
02981 }
02982
02983 void CMpeg2Decoder::slice_non_intra_DCT(uint8_t* dest, int stride)
02984 {
02985 int last = m_mpeg1
02986 ? get_mpeg1_non_intra_block()
02987 : get_non_intra_block ();
02988
02989 m_idct_add(last, m_DCTblock, dest, stride);
02990 }
02991
02992 #define MOTION(table,ref,motion_x,motion_y,size,y) \
02993 pos_x = 2 * m_offset + motion_x; \
02994 pos_y = 2 * m_v_offset + motion_y + 2 * y; \
02995 if(pos_x > m_limit_x) { \
02996 pos_x = ((int)pos_x < 0) ? 0 : m_limit_x; \
02997 motion_x = pos_x - 2 * m_offset; \
02998 } \
02999 if(pos_y > m_limit_y_ ## size) { \
03000 pos_y = ((int)pos_y < 0) ? 0 : m_limit_y_ ## size; \
03001 motion_y = pos_y - 2 * m_v_offset - 2 * y; \
03002 } \
03003 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
03004 table[xy_half] (m_dest[0] + y * m_stride + m_offset, \
03005 ref[0] + (pos_x >> 1) + (pos_y >> 1) * m_stride, \
03006 m_stride, size); \
03007 motion_x /= 2; motion_y /= 2; \
03008 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
03009 offset = (((m_offset + motion_x) >> 1) + \
03010 ((((m_v_offset + motion_y) >> 1) + y/2) * \
03011 m_uv_stride)); \
03012 table[4+xy_half] (m_dest[1] + y/2 * m_uv_stride + \
03013 (m_offset >> 1), ref[1] + offset, \
03014 m_uv_stride, size/2); \
03015 table[4+xy_half] (m_dest[2] + y/2 * m_uv_stride + \
03016 (m_offset >> 1), ref[2] + offset, \
03017 m_uv_stride, size/2)
03018
03019 #define MOTION_FIELD(table,ref,motion_x,motion_y,dest_field,op,src_field) \
03020 pos_x = 2 * m_offset + motion_x; \
03021 pos_y = m_v_offset + motion_y; \
03022 if(pos_x > m_limit_x) { \
03023 pos_x = ((int)pos_x < 0) ? 0 : m_limit_x; \
03024 motion_x = pos_x - 2 * m_offset; \
03025 } \
03026 if(pos_y > m_limit_y) { \
03027 pos_y = ((int)pos_y < 0) ? 0 : m_limit_y; \
03028 motion_y = pos_y - m_v_offset; \
03029 } \
03030 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
03031 table[xy_half] (m_dest[0] + dest_field * m_stride + \
03032 m_offset, \
03033 (ref[0] + (pos_x >> 1) + \
03034 ((pos_y op) + src_field) * m_stride), \
03035 2 * m_stride, 8); \
03036 motion_x /= 2; motion_y /= 2; \
03037 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
03038 offset = (((m_offset + motion_x) >> 1) + \
03039 (((m_v_offset >> 1) + (motion_y op) + src_field) * \
03040 m_uv_stride)); \
03041 table[4+xy_half] (m_dest[1] + dest_field * m_uv_stride + \
03042 (m_offset >> 1), ref[1] + offset, \
03043 2 * m_uv_stride, 4); \
03044 table[4+xy_half] (m_dest[2] + dest_field * m_uv_stride + \
03045 (m_offset >> 1), ref[2] + offset, \
03046 2 * m_uv_stride, 4)
03047
03048 void CMpeg2Decoder::motion_mp1(motion_t* motion, mpeg2_mc_fct * const * const table)
03049 {
03050 int motion_x, motion_y;
03051 unsigned int pos_x, pos_y, xy_half, offset;
03052
03053 NEEDBITS;
03054 motion_x = motion->pmv[0][0] + (get_motion_delta(motion->f_code[0]) << motion->f_code[1]);
03055 motion_x = bound_motion_vector(motion_x, motion->f_code[0] + motion->f_code[1]);
03056 motion->pmv[0][0] = motion_x;
03057
03058 NEEDBITS;
03059 motion_y = motion->pmv[0][1] + (get_motion_delta(motion->f_code[0]) << motion->f_code[1]);
03060 motion_y = bound_motion_vector(motion_y, motion->f_code[0] + motion->f_code[1]);
03061 motion->pmv[0][1] = motion_y;
03062
03063 MOTION(table, motion->ref[0], motion_x, motion_y, 16, 0);
03064 }
03065
03066 void CMpeg2Decoder::motion_fr_frame(motion_t* motion, mpeg2_mc_fct * const * const table)
03067 {
03068 int motion_x, motion_y;
03069 unsigned int pos_x, pos_y, xy_half, offset;
03070
03071 NEEDBITS;
03072 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03073 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03074 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
03075
03076 NEEDBITS;
03077 motion_y = motion->pmv[0][1] + get_motion_delta(motion->f_code[1]);
03078 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
03079 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
03080
03081 MOTION(table, motion->ref[0], motion_x, motion_y, 16, 0);
03082 }
03083
03084 void CMpeg2Decoder::motion_fr_field(motion_t* motion, mpeg2_mc_fct * const * const table)
03085 {
03086 int motion_x, motion_y, field;
03087 unsigned int pos_x, pos_y, xy_half, offset;
03088
03089 NEEDBITS;
03090 field = UBITS(bit_buf, 1);
03091 DUMPBITS(1);
03092
03093 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03094 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03095 motion->pmv[0][0] = motion_x;
03096
03097 NEEDBITS;
03098 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta(motion->f_code[1]);
03099
03100 motion->pmv[0][1] = motion_y << 1;
03101
03102 MOTION_FIELD(table, motion->ref[0], motion_x, motion_y, 0, & ~1, field);
03103
03104 NEEDBITS;
03105 field = UBITS(bit_buf, 1);
03106 DUMPBITS(1);
03107
03108 motion_x = motion->pmv[1][0] + get_motion_delta(motion->f_code[0]);
03109 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03110 motion->pmv[1][0] = motion_x;
03111
03112 NEEDBITS;
03113 motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta(motion->f_code[1]);
03114
03115 motion->pmv[1][1] = motion_y << 1;
03116
03117 MOTION_FIELD(table, motion->ref[0], motion_x, motion_y, 1, & ~1, field);
03118 }
03119
03120 void CMpeg2Decoder::motion_fr_dmv(motion_t* motion, mpeg2_mc_fct * const * const table)
03121 {
03122 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y;
03123 unsigned int pos_x, pos_y, xy_half, offset;
03124
03125 NEEDBITS;
03126 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03127 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03128 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
03129 NEEDBITS;
03130 dmv_x = get_dmv();
03131
03132 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta(motion->f_code[1]);
03133
03134 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;
03135 dmv_y = get_dmv();
03136
03137 m = m_top_field_first ? 1 : 3;
03138 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
03139 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1;
03140 MOTION_FIELD(m_mc->put, motion->ref[0], other_x, other_y, 0, | 1, 0);
03141
03142 m = m_top_field_first ? 3 : 1;
03143 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
03144 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1;
03145 MOTION_FIELD(m_mc->put, motion->ref[0], other_x, other_y, 1, & ~1, 0);
03146
03147 pos_x = 2 * m_offset + motion_x;
03148 pos_y = m_v_offset + motion_y;
03149 if(pos_x > m_limit_x)
03150 {
03151 pos_x = ((int)pos_x < 0) ? 0 : m_limit_x;
03152 motion_x = pos_x - 2 * m_offset;
03153 }
03154 if(pos_y > m_limit_y)
03155 {
03156 pos_y = ((int)pos_y < 0) ? 0 : m_limit_y;
03157 motion_y = pos_y - m_v_offset;
03158 }
03159 xy_half = ((pos_y & 1) << 1) | (pos_x & 1);
03160 offset = (pos_x >> 1) + (pos_y & ~1) * m_stride;
03161 m_mc->avg[xy_half](m_dest[0] + m_offset, motion->ref[0][0] + offset, 2 * m_stride, 8);
03162 m_mc->avg[xy_half](m_dest[0] + m_stride + m_offset, motion->ref[0][0] + m_stride + offset, 2 * m_stride, 8);
03163 motion_x /= 2;
03164 motion_y /= 2;
03165 xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
03166 offset = ((m_offset + motion_x) >> 1) + ((m_v_offset >> 1) + (motion_y & ~1)) * m_uv_stride;
03167 m_mc->avg[4+xy_half](m_dest[1] + (m_offset >> 1), motion->ref[0][1] + offset, 2 * m_uv_stride, 4);
03168 m_mc->avg[4+xy_half](m_dest[1] + m_uv_stride + (m_offset >> 1), motion->ref[0][1] + m_uv_stride + offset, 2 * m_uv_stride, 4);
03169 m_mc->avg[4+xy_half](m_dest[2] + (m_offset >> 1), motion->ref[0][2] + offset, 2 * m_uv_stride, 4);
03170 m_mc->avg[4+xy_half](m_dest[2] + m_uv_stride + (m_offset >> 1), motion->ref[0][2] + m_uv_stride + offset, 2 * m_uv_stride, 4);
03171 }
03172
03173 void CMpeg2Decoder::motion_reuse(motion_t* motion, mpeg2_mc_fct * const * const table)
03174 {
03175 int motion_x, motion_y;
03176 unsigned int pos_x, pos_y, xy_half, offset;
03177
03178 motion_x = motion->pmv[0][0];
03179 motion_y = motion->pmv[0][1];
03180
03181 MOTION(table, motion->ref[0], motion_x, motion_y, 16, 0);
03182 }
03183
03184 void CMpeg2Decoder::motion_zero(motion_t* motion, mpeg2_mc_fct * const * const table)
03185 {
03186 unsigned int offset;
03187
03188 table[0](m_dest[0] + m_offset, motion->ref[0][0] + m_offset + m_v_offset * m_stride, m_stride, 16);
03189 offset = (m_offset >> 1) + (m_v_offset >> 1) * m_uv_stride;
03190 table[4](m_dest[1] + (m_offset >> 1), motion->ref[0][1] + offset, m_uv_stride, 8);
03191 table[4](m_dest[2] + (m_offset >> 1), motion->ref[0][2] + offset, m_uv_stride, 8);
03192 }
03193
03194
03195 void CMpeg2Decoder::motion_fr_conceal()
03196 {
03197 int tmp;
03198
03199 NEEDBITS;
03200 tmp = (m_f_motion.pmv[0][0] + get_motion_delta(m_f_motion.f_code[0]));
03201 tmp = bound_motion_vector(tmp, m_f_motion.f_code[0]);
03202 m_f_motion.pmv[1][0] = m_f_motion.pmv[0][0] = tmp;
03203
03204 NEEDBITS;
03205 tmp = m_f_motion.pmv[0][1] + get_motion_delta(m_f_motion.f_code[1]);
03206 tmp = bound_motion_vector(tmp, m_f_motion.f_code[1]);
03207 m_f_motion.pmv[1][1] = m_f_motion.pmv[0][1] = tmp;
03208
03209 DUMPBITS(1);
03210 }
03211
03212 void CMpeg2Decoder::motion_fi_field(motion_t * motion, mpeg2_mc_fct * const * const table)
03213 {
03214 int motion_x, motion_y;
03215 uint8_t** ref_field;
03216 unsigned int pos_x, pos_y, xy_half, offset;
03217
03218 NEEDBITS;
03219 ref_field = motion->ref2[UBITS(bit_buf, 1)];
03220 DUMPBITS(1);
03221
03222 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03223 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
03224 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
03225
03226 NEEDBITS;
03227 motion_y = motion->pmv[0][1] + get_motion_delta(motion->f_code[1]);
03228 motion_y = bound_motion_vector(motion_y, motion->f_code[1]);
03229 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
03230
03231 MOTION(table, ref_field, motion_x, motion_y, 16, 0);
03232 }
03233
03234 void CMpeg2Decoder::motion_fi_16x8(motion_t* motion, mpeg2_mc_fct * const * const table)
03235 {
03236 int motion_x, motion_y;
03237 uint8_t** ref_field;
03238 unsigned int pos_x, pos_y, xy_half, offset;
03239
03240 NEEDBITS;
03241 ref_field = motion->ref2[UBITS(bit_buf, 1)];
03242 DUMPBITS(1);
03243
03244 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03245 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03246 motion->pmv[0][0] = motion_x;
03247
03248 NEEDBITS;
03249 motion_y = motion->pmv[0][1] + get_motion_delta(motion->f_code[1]);
03250 motion_y = bound_motion_vector(motion_y, motion->f_code[1]);
03251 motion->pmv[0][1] = motion_y;
03252
03253 MOTION(table, ref_field, motion_x, motion_y, 8, 0);
03254
03255 NEEDBITS;
03256 ref_field = motion->ref2[UBITS(bit_buf, 1)];
03257 DUMPBITS(1);
03258
03259 motion_x = motion->pmv[1][0] + get_motion_delta(motion->f_code[0]);
03260 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
03261 motion->pmv[1][0] = motion_x;
03262
03263 NEEDBITS;
03264 motion_y = motion->pmv[1][1] + get_motion_delta(motion->f_code[1]);
03265 motion_y = bound_motion_vector(motion_y, motion->f_code[1]);
03266 motion->pmv[1][1] = motion_y;
03267
03268 MOTION(table, ref_field, motion_x, motion_y, 8, 8);
03269 }
03270
03271 void CMpeg2Decoder::motion_fi_dmv(motion_t* motion, mpeg2_mc_fct * const * const table)
03272 {
03273 int motion_x, motion_y, other_x, other_y;
03274 unsigned int pos_x, pos_y, xy_half, offset;
03275
03276 NEEDBITS;
03277 motion_x = motion->pmv[0][0] + get_motion_delta(motion->f_code[0]);
03278 motion_x = bound_motion_vector(motion_x, motion->f_code[0]);
03279 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
03280 NEEDBITS;
03281 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv();
03282
03283 motion_y = motion->pmv[0][1] + get_motion_delta(motion->f_code[1]);
03284 motion_y = bound_motion_vector(motion_y, motion->f_code[1]);
03285 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
03286 other_y = ((motion_y + (motion_y > 0)) >> 1) + get_dmv () + m_dmv_offset;
03287
03288 MOTION(m_mc->put, motion->ref[0], motion_x, motion_y, 16, 0);
03289 MOTION(m_mc->avg, motion->ref[1], other_x, other_y, 16, 0);
03290 }
03291
03292 void CMpeg2Decoder::motion_fi_conceal()
03293 {
03294 int tmp;
03295
03296 NEEDBITS;
03297 DUMPBITS(1);
03298
03299 tmp = m_f_motion.pmv[0][0] + get_motion_delta (m_f_motion.f_code[0]);
03300 tmp = bound_motion_vector(tmp, m_f_motion.f_code[0]);
03301 m_f_motion.pmv[1][0] = m_f_motion.pmv[0][0] = tmp;
03302
03303 NEEDBITS;
03304 tmp = m_f_motion.pmv[0][1] + get_motion_delta(m_f_motion.f_code[1]);
03305 tmp = bound_motion_vector(tmp, m_f_motion.f_code[1]);
03306 m_f_motion.pmv[1][1] = m_f_motion.pmv[0][1] = tmp;
03307
03308 DUMPBITS(1);
03309 }
03310
03311 #define MOTION_CALL(routine, direction) \
03312 do { \
03313 if((direction) & MACROBLOCK_MOTION_FORWARD) \
03314 routine(&m_f_motion, m_mc->put); \
03315 if((direction) & MACROBLOCK_MOTION_BACKWARD) \
03316 routine(&m_b_motion, (direction & MACROBLOCK_MOTION_FORWARD) ? m_mc->avg : m_mc->put); \
03317 } while (0)
03318
03319 #define NEXT_MACROBLOCK \
03320 do { \
03321 m_offset += 16; \
03322 if(m_offset == m_width) { \
03323 m_dest[0] += 16 * m_stride; \
03324 m_dest[1] += 4 * m_stride; \
03325 m_dest[2] += 4 * m_stride; \
03326 m_v_offset += 16; \
03327 if(m_v_offset > m_limit_y) \
03328 return; \
03329 m_offset = 0; \
03330 } \
03331 } while (0)
03332
03333 void CMpeg2Decoder::mpeg2_init_fbuf(uint8_t* current_fbuf[3], uint8_t* forward_fbuf[3], uint8_t* backward_fbuf[3])
03334 {
03335 int offset, stride, height, bottom_field;
03336
03337 stride = m_width;
03338 bottom_field = (m_picture_structure == BOTTOM_FIELD);
03339 offset = bottom_field ? stride : 0;
03340 height = m_height;
03341
03342 m_picture_dest[0] = current_fbuf[0] + offset;
03343 m_picture_dest[1] = current_fbuf[1] + (offset >> 1);
03344 m_picture_dest[2] = current_fbuf[2] + (offset >> 1);
03345
03346 m_f_motion.ref[0][0] = forward_fbuf[0] + offset;
03347 m_f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
03348 m_f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
03349
03350 m_b_motion.ref[0][0] = backward_fbuf[0] + offset;
03351 m_b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
03352 m_b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
03353
03354 if(m_picture_structure != FRAME_PICTURE)
03355 {
03356 m_dmv_offset = bottom_field ? 1 : -1;
03357 m_f_motion.ref2[0] = m_f_motion.ref[bottom_field];
03358 m_f_motion.ref2[1] = m_f_motion.ref[!bottom_field];
03359 m_b_motion.ref2[0] = m_b_motion.ref[bottom_field];
03360 m_b_motion.ref2[1] = m_b_motion.ref[!bottom_field];
03361 offset = stride - offset;
03362
03363 if(m_second_field && (m_coding_type != B_TYPE))
03364 forward_fbuf = current_fbuf;
03365
03366 m_f_motion.ref[1][0] = forward_fbuf[0] + offset;
03367 m_f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
03368 m_f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
03369
03370 m_b_motion.ref[1][0] = backward_fbuf[0] + offset;
03371 m_b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
03372 m_b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
03373
03374 stride <<= 1;
03375 height >>= 1;
03376 }
03377
03378 m_stride = stride;
03379 m_uv_stride = stride >> 1;
03380 m_limit_x = 2 * m_width - 32;
03381 m_limit_y_16 = 2 * height - 32;
03382 m_limit_y_8 = 2 * height - 16;
03383 m_limit_y = height - 16;
03384 }
03385
03386 int CMpeg2Decoder::slice_init(int code)
03387 {
03388 int offset;
03389 const MBAtab* mba;
03390
03391 m_dc_dct_pred[0] = m_dc_dct_pred[1] =
03392 m_dc_dct_pred[2] = 128 << m_intra_dc_precision;
03393
03394 m_f_motion.pmv[0][0] = m_f_motion.pmv[0][1] = 0;
03395 m_f_motion.pmv[1][0] = m_f_motion.pmv[1][1] = 0;
03396 m_b_motion.pmv[0][0] = m_b_motion.pmv[0][1] = 0;
03397 m_b_motion.pmv[1][0] = m_b_motion.pmv[1][1] = 0;
03398
03399 if(m_vertical_position_extension)
03400 {
03401 code += UBITS(bit_buf, 3) << 7;
03402 DUMPBITS(3);
03403 }
03404
03405 m_v_offset = (code - 1) * 16;
03406 offset = (code - 1) * m_stride * 4;
03407
03408 m_dest[0] = m_picture_dest[0] + offset * 4;
03409 m_dest[1] = m_picture_dest[1] + offset;
03410 m_dest[2] = m_picture_dest[2] + offset;
03411
03412 m_quantizer_scale = get_quantizer_scale();
03413
03414
03415 while(bit_buf & 0x80000000)
03416 {
03417 DUMPBITS(9);
03418 NEEDBITS;
03419 }
03420
03421
03422 offset = 0;
03423 while(1)
03424 {
03425 if(bit_buf >= 0x08000000)
03426 {
03427 mba = MBA_5 + (UBITS(bit_buf, 6) - 2);
03428 break;
03429 }
03430 else if(bit_buf >= 0x01800000)
03431 {
03432 mba = MBA_11 + (UBITS(bit_buf, 12) - 24);
03433 break;
03434 }
03435 else
03436 {
03437 switch(UBITS(bit_buf, 12))
03438 {
03439 case 8:
03440 offset += 33;
03441 DUMPBITS(11);
03442 NEEDBITS;
03443 continue;
03444 case 15:
03445 bit_buf &= 0xfffff;
03446 DUMPBITS(11);
03447 NEEDBITS;
03448 continue;
03449 default:
03450 return 1;
03451 }
03452 }
03453 }
03454
03455 DUMPBITS(mba->len + 1);
03456 m_offset = (offset + mba->mba) << 4;
03457
03458 while(m_offset - m_width >= 0)
03459 {
03460 m_offset -= m_width;
03461 m_dest[0] += 16 * m_stride;
03462 m_dest[1] += 4 * m_stride;
03463 m_dest[2] += 4 * m_stride;
03464 m_v_offset += 16;
03465 }
03466
03467 if(m_v_offset > m_limit_y)
03468 return 1;
03469
03470 return 0;
03471 }
03472
03473 void CMpeg2Decoder::mpeg2_slice(int code, const uint8_t* buffer)
03474 {
03475 m_bitstream_buf = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
03476 m_bitstream_ptr = buffer + 4;
03477 m_bitstream_bits = -16;
03478
03479 if(slice_init(code))
03480 return;
03481
03482 while(1)
03483 {
03484 int macroblock_modes;
03485 int mba_inc;
03486 const MBAtab * mba;
03487
03488 NEEDBITS;
03489
03490 macroblock_modes = get_macroblock_modes();
03491
03492
03493 if(macroblock_modes & MACROBLOCK_QUANT)
03494 m_quantizer_scale = get_quantizer_scale();
03495
03496 if(macroblock_modes & MACROBLOCK_INTRA)
03497 {
03498 int DCT_offset, DCT_stride;
03499 int offset;
03500 uint8_t* dest_y;
03501
03502 if(m_concealment_motion_vectors)
03503 {
03504 if(m_picture_structure == FRAME_PICTURE) motion_fr_conceal();
03505 else motion_fi_conceal();
03506 }
03507 else
03508 {
03509 m_f_motion.pmv[0][0] = m_f_motion.pmv[0][1] = 0;
03510 m_f_motion.pmv[1][0] = m_f_motion.pmv[1][1] = 0;
03511 m_b_motion.pmv[0][0] = m_b_motion.pmv[0][1] = 0;
03512 m_b_motion.pmv[1][0] = m_b_motion.pmv[1][1] = 0;
03513 }
03514
03515 if(macroblock_modes & DCT_TYPE_INTERLACED)
03516 {
03517 DCT_offset = m_stride;
03518 DCT_stride = m_stride * 2;
03519 }
03520 else
03521 {
03522 DCT_offset = m_stride * 8;
03523 DCT_stride = m_stride;
03524 }
03525
03526 offset = m_offset;
03527 dest_y = m_dest[0] + offset;
03528 slice_intra_DCT(0, dest_y, DCT_stride);
03529 slice_intra_DCT(0, dest_y + 8, DCT_stride);
03530 slice_intra_DCT(0, dest_y + DCT_offset, DCT_stride);
03531 slice_intra_DCT(0, dest_y + DCT_offset + 8, DCT_stride);
03532 slice_intra_DCT(1, m_dest[1] + (offset >> 1), m_uv_stride);
03533 slice_intra_DCT (2, m_dest[2] + (offset >> 1), m_uv_stride);
03534
03535 if(m_coding_type == D_TYPE)
03536 {
03537 NEEDBITS;
03538 DUMPBITS(1);
03539 }
03540 }
03541 else
03542 {
03543 if(m_picture_structure == FRAME_PICTURE)
03544 {
03545 switch(macroblock_modes & MOTION_TYPE_MASK)
03546 {
03547 case MC_FRAME:
03548 if(m_mpeg1) MOTION_CALL(motion_mp1, macroblock_modes);
03549 else MOTION_CALL (motion_fr_frame, macroblock_modes);
03550 break;
03551
03552 case MC_FIELD:
03553 MOTION_CALL(motion_fr_field, macroblock_modes);
03554 break;
03555
03556 case MC_DMV:
03557 MOTION_CALL(motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
03558 break;
03559
03560 case 0:
03561
03562 m_f_motion.pmv[0][0] = 0;
03563 m_f_motion.pmv[0][1] = 0;
03564 m_f_motion.pmv[1][0] = 0;
03565 m_f_motion.pmv[1][1] = 0;
03566 MOTION_CALL(motion_zero, MACROBLOCK_MOTION_FORWARD);
03567 break;
03568 }
03569 }
03570 else
03571 {
03572 switch (macroblock_modes & MOTION_TYPE_MASK)
03573 {
03574 case MC_FIELD:
03575 MOTION_CALL(motion_fi_field, macroblock_modes);
03576 break;
03577
03578 case MC_16X8:
03579 MOTION_CALL(motion_fi_16x8, macroblock_modes);
03580 break;
03581
03582 case MC_DMV:
03583 MOTION_CALL(motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
03584 break;
03585
03586 case 0:
03587
03588 m_f_motion.pmv[0][0] = 0;
03589 m_f_motion.pmv[0][1] = 0;
03590 m_f_motion.pmv[1][0] = 0;
03591 m_f_motion.pmv[1][1] = 0;
03592 MOTION_CALL(motion_zero, MACROBLOCK_MOTION_FORWARD);
03593 break;
03594 }
03595 }
03596
03597 if(macroblock_modes & MACROBLOCK_PATTERN)
03598 {
03599 int coded_block_pattern;
03600 int DCT_offset, DCT_stride;
03601 int offset;
03602 uint8_t* dest_y;
03603
03604 if(macroblock_modes & DCT_TYPE_INTERLACED)
03605 {
03606 DCT_offset = m_stride;
03607 DCT_stride = m_stride * 2;
03608 }
03609 else
03610 {
03611 DCT_offset = m_stride * 8;
03612 DCT_stride = m_stride;
03613 }
03614
03615 coded_block_pattern = get_coded_block_pattern();
03616
03617 offset = m_offset;
03618 dest_y = m_dest[0] + offset;
03619
03620 if(coded_block_pattern & 0x20)
03621 slice_non_intra_DCT(dest_y, DCT_stride);
03622 if(coded_block_pattern & 0x10)
03623 slice_non_intra_DCT(dest_y + 8, DCT_stride);
03624 if(coded_block_pattern & 0x08)
03625 slice_non_intra_DCT(dest_y + DCT_offset, DCT_stride);
03626 if(coded_block_pattern & 0x04)
03627 slice_non_intra_DCT(dest_y + DCT_offset + 8, DCT_stride);
03628 if(coded_block_pattern & 0x2)
03629 slice_non_intra_DCT(m_dest[1] + (offset >> 1), m_uv_stride);
03630 if(coded_block_pattern & 0x1)
03631 slice_non_intra_DCT(m_dest[2] + (offset >> 1), m_uv_stride);
03632 }
03633
03634 m_dc_dct_pred[0] =
03635 m_dc_dct_pred[1] =
03636 m_dc_dct_pred[2] = 128 << m_intra_dc_precision;
03637 }
03638
03639 NEXT_MACROBLOCK;
03640
03641 NEEDBITS;
03642 mba_inc = 0;
03643 while(1)
03644 {
03645 if(bit_buf >= 0x10000000)
03646 {
03647 mba = MBA_5 + (UBITS(bit_buf, 5) - 2);
03648 break;
03649 }
03650 else if(bit_buf >= 0x03000000)
03651 {
03652 mba = MBA_11 + (UBITS(bit_buf, 11) - 24);
03653 break;
03654 }
03655 else
03656 {
03657 switch(UBITS(bit_buf, 11))
03658 {
03659 case 8:
03660 mba_inc += 33;
03661
03662 case 15:
03663 DUMPBITS(11);
03664 NEEDBITS;
03665 continue;
03666 default:
03667 return;
03668 }
03669 }
03670 }
03671
03672 DUMPBITS(mba->len);
03673 mba_inc += mba->mba;
03674
03675 if(mba_inc)
03676 {
03677 m_dc_dct_pred[0] =
03678 m_dc_dct_pred[1] =
03679 m_dc_dct_pred[2] = 128 << m_intra_dc_precision;
03680
03681 if(m_coding_type == P_TYPE)
03682 {
03683 m_f_motion.pmv[0][0] = m_f_motion.pmv[0][1] = 0;
03684 m_f_motion.pmv[1][0] = m_f_motion.pmv[1][1] = 0;
03685
03686 do {
03687 MOTION_CALL(motion_zero, MACROBLOCK_MOTION_FORWARD);
03688 NEXT_MACROBLOCK;
03689 } while(--mba_inc);
03690 }
03691 else
03692 {
03693 do {
03694 MOTION_CALL (motion_reuse, macroblock_modes);
03695 NEXT_MACROBLOCK;
03696 } while(--mba_inc);
03697 }
03698 }
03699 }
03700 }
03701
03702 #undef bit_buf
03703 #undef bits
03704 #undef bit_ptr
03705
03708
03709 CMpeg2Info::CMpeg2Info()
03710 {
03711 m_sequence = NULL;
03712 m_gop = NULL;
03713 Reset();
03714 }
03715
03716 CMpeg2Info::~CMpeg2Info()
03717 {
03718 }
03719
03720 void CMpeg2Info::Reset()
03721 {
03722 m_current_picture = m_current_picture_2nd = NULL;
03723 m_display_picture = m_display_picture_2nd = NULL;
03724 m_current_fbuf = m_display_fbuf = m_discard_fbuf = NULL;
03725 m_user_data = NULL;
03726 m_user_data_len = 0;
03727 }
03728