PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
libmpeg.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright (c) 2006-2007 Eugene Plotnikov <e-plotnikov@operamail.com>
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9# Based on refernce software of MSSG
10*/
11
12#include <malloc.h>
13#include <stdio.h>
14#include <string.h>
15#include <kernel.h>
16
17#include "libmpeg.h"
18#include "libmpeg_internal.h"
19
20static _MPEGContext s_MPEG12Ctx;
21static s64* s_pCurPTS;
22
23static void ( *LumaOp[ 8 ] ) ( u8* arg1, u16* arg2, int arg3, int arg4, int var1, int ta ) = {
24 _MPEG_put_luma, _MPEG_put_luma_X, _MPEG_put_luma_Y, _MPEG_put_luma_XY,
25 _MPEG_avg_luma, _MPEG_avg_luma_X, _MPEG_avg_luma_Y, _MPEG_avg_luma_XY
26};
27
28static void ( *ChromaOp[ 8 ] ) ( u8* arg1, u16* arg2, int arg3, int arg4, int var1, int ta ) = {
29 _MPEG_put_chroma, _MPEG_put_chroma_X, _MPEG_put_chroma_Y, _MPEG_put_chroma_XY,
30 _MPEG_avg_chroma, _MPEG_avg_chroma_X, _MPEG_avg_chroma_Y, _MPEG_avg_chroma_XY
31};
32
33static void ( *PutBlockOp[ 3 ] ) ( _MPEGMotions* ) = {
34 _MPEG_put_block_il, _MPEG_put_block_fr, _MPEG_put_block_fl
35};
36
37static void ( *AddBlockOp[ 2 ][ 2 ] ) ( _MPEGMotions* ) = {
38 { _MPEG_add_block_frfr, _MPEG_add_block_frfl },
39 { _MPEG_add_block_ilfl, _MPEG_add_block_ilfl }
40};
41
42static float s_FrameRate[ 16 ] = {
43 0.0F, ( ( 23.0F * 1000.0F ) / 1001.0F ),
44 24.0F, 25.0F, ( ( 30.0F * 1000.0F ) / 1001.0F ),
45 30.0F, 50.0F, ( ( 60.0F * 1000.0F ) / 1001.0F ),
46 60.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F
47};
48
49static void* ( *_init_cb ) ( void*, MPEGSequenceInfo* );
50static void* s_pInitCBParam;
51
52static void* _init_seq ( void );
53static void _destroy_seq ( void );
54
55static int _get_hdr ( void );
56
57static void _seq_header ( void );
58static void _gop_header ( void );
59static void _pic_header ( void );
60static void _ext_and_ud ( void );
61static void _ext_unknown ( void );
62static void _ext_seq ( void );
63static void _ext_seq_dsp ( void );
64static void _ext_qnt_mtx ( void );
65static void _ext_cpy_rgt ( void );
66static void _ext_seq_scl ( void );
67static void _ext_pic_dsp ( void );
68static void _ext_pic_cod ( void );
69static void _ext_pic_ssc ( void );
70static void _ext_pic_tsc ( void );
71static void _xtra_bitinf ( void );
72
73static int _get_next_picture ( void*, s64* );
74static int _get_first_picture ( void*, s64* );
75
76int ( *MPEG_Picture ) ( void*, s64* );
77
78static void ( *DoMC ) ( void );
79
80static void _mpeg12_picture_data ( void );
81static int _mpeg12_slice ( int );
82static int _mpeg12_dec_mb ( int*, int*, int[ 2 ][ 2 ][ 2 ], int [ 2 ][ 2 ], int[ 2 ] );
83
84void MPEG_Initialize (
85 int ( *apDataCB ) ( void* ), void* apDataCBParam,
86 void* ( *apInitCB ) ( void*, MPEGSequenceInfo* ), void* apInitCBParam,
87 s64* apCurPTS
88 ) {
89
90 memset ( &s_MPEG12Ctx, 0, sizeof ( s_MPEG12Ctx ) );
91
92 _MPEG_Initialize ( &s_MPEG12Ctx, apDataCB, apDataCBParam, &s_MPEG12Ctx.m_SI.m_fEOF );
93
94 _init_cb = apInitCB;
95 s_pInitCBParam = apInitCBParam;
96 s_pCurPTS = apCurPTS;
97
98 s_MPEG12Ctx.m_SI.m_FrameCnt = 0;
99 s_MPEG12Ctx.m_SI.m_fEOF = 0;
100 s_MPEG12Ctx.m_SI.m_Profile = -1;
101 s_MPEG12Ctx.m_SI.m_Level = -1;
102 s_MPEG12Ctx.m_SI.m_ChromaFmt = MPEG_CHROMA_FORMAT_420;
103 s_MPEG12Ctx.m_SI.m_VideoFmt = MPEG_VIDEO_FORMAT_UNSPEC;
104 s_MPEG12Ctx.m_fMPEG2 = 0;
105
106 s_MPEG12Ctx.m_MC[ 0 ].m_pSPRBlk = ( void* )0x70000000;
107 s_MPEG12Ctx.m_MC[ 0 ].m_pSPRRes = ( void* )0x70000300;
108 s_MPEG12Ctx.m_MC[ 0 ].m_pSPRMC = ( void* )0x70000600;
109 s_MPEG12Ctx.m_MC[ 1 ].m_pSPRBlk = ( void* )0x70001E00;
110 s_MPEG12Ctx.m_MC[ 1 ].m_pSPRRes = ( void* )0x70002100;
111 s_MPEG12Ctx.m_MC[ 1 ].m_pSPRMC = ( void* )0x70002400;
112
113 MPEG_Picture = _get_first_picture;
114
115} /* end MPEG_Initialize */
116
117void MPEG_Destroy ( void ) {
118
119 _destroy_seq ();
120 _MPEG_Destroy ();
121
122} /* end MPEG_Destroy */
123
124static void* _init_seq ( void ) {
125
126 int lMBWidth, lMBHeight;
127
128 if ( !s_MPEG12Ctx.m_fMPEG2 ) {
129 s_MPEG12Ctx.m_fProgSeq = 1;
130 s_MPEG12Ctx.m_PictStruct = _MPEG_PS_FRAME;
131 s_MPEG12Ctx.m_fFPFrmDCT = 1;
132 } /* end if */
133
134 lMBWidth = ( s_MPEG12Ctx.m_SI.m_Width + 15 ) / 16;
135 lMBHeight = s_MPEG12Ctx.m_fMPEG2 && !s_MPEG12Ctx.m_fProgSeq ? 2 * ( ( s_MPEG12Ctx.m_SI.m_Height + 31 ) / 32 )
136 : ( s_MPEG12Ctx.m_SI.m_Height + 15 ) / 16;
137
138 if ( lMBWidth != s_MPEG12Ctx.m_MBWidth ||
139 lMBHeight != s_MPEG12Ctx.m_MBHeight
140 ) {
141
142 int i, lSize;
143
144 if ( s_MPEG12Ctx.m_pFwdFrame ) _destroy_seq ();
145
146 s_MPEG12Ctx.m_MBWidth = lMBWidth;
147 s_MPEG12Ctx.m_MBHeight = lMBHeight;
148 s_MPEG12Ctx.m_SI.m_Width = lMBWidth << 4;
149 s_MPEG12Ctx.m_SI.m_Height = lMBHeight << 4;
150
151 s_MPEG12Ctx.m_MBStride = lMBWidth * sizeof ( _MPEGMacroBlock8 );
152
153 lSize = lMBWidth * ( lMBHeight + 1 ) * sizeof ( _MPEGMacroBlock8 ) + sizeof ( _MPEGMacroBlock8 );
154
155 s_MPEG12Ctx.m_pFwdFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
156 s_MPEG12Ctx.m_pBckFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
157 s_MPEG12Ctx.m_pAuxFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
158
159 s_MPEG12Ctx.m_pMBXY = ( _MPEGMBXY* )malloc (
160 sizeof ( _MPEGMBXY ) * ( s_MPEG12Ctx.m_MBCount = s_MPEG12Ctx.m_MBWidth * s_MPEG12Ctx.m_MBHeight )
161 );
162
163 for ( i = 0; i < s_MPEG12Ctx.m_MBCount; ++i ) {
164 s_MPEG12Ctx.m_pMBXY[ i ].m_X = i % lMBWidth;
165 s_MPEG12Ctx.m_pMBXY[ i ].m_Y = i / lMBWidth;
166 } /* end for */
167
168 } /* end if */
169
170 return _init_cb ( s_pInitCBParam, &s_MPEG12Ctx.m_SI );
171
172} /* end _init_seq */
173
174static void _destroy_seq ( void ) {
175
176 MPEG_Picture = _get_first_picture;
177
178 if ( s_MPEG12Ctx.m_pAuxFrame ) {
179 free ( s_MPEG12Ctx.m_pAuxFrame );
180 s_MPEG12Ctx.m_pAuxFrame = NULL;
181 } /* end if */
182
183 if ( s_MPEG12Ctx.m_pBckFrame ) {
184 free ( s_MPEG12Ctx.m_pBckFrame );
185 s_MPEG12Ctx.m_pBckFrame = NULL;
186 } /* end if */
187
188 if ( s_MPEG12Ctx.m_pFwdFrame ) {
189 free ( s_MPEG12Ctx.m_pFwdFrame );
190 s_MPEG12Ctx.m_pFwdFrame = NULL;
191 } /* end if */
192
193 if ( s_MPEG12Ctx.m_pMBXY ) {
194 free ( s_MPEG12Ctx.m_pMBXY );
195 s_MPEG12Ctx.m_pMBXY = NULL;
196 } /* end if */
197
198 s_MPEG12Ctx.m_MBWidth =
199 s_MPEG12Ctx.m_MBHeight = 0;
200
201} /* end _destroy_seq */
202
203static int _get_hdr ( void ) {
204
205 while ( 1 ) {
206
207 unsigned int lCode;
208 lCode = _MPEG_NextStartCode (); _MPEG_GetBits ( 32 );
209
210 switch ( lCode ) {
211
212 case _MPEG_CODE_SEQ_HDR:
213 _seq_header ();
214 break;
215
216 case _MPEG_CODE_GRP_START:
217 _gop_header ();
218 break;
219
220 case _MPEG_CODE_PIC_START:
221 _pic_header ();
222 return 1;
223
224 case _MPEG_CODE_SEQ_END:
225 MPEG_Picture = _get_first_picture;
226 return 0;
227 break;
228
229 } /* end switch */
230
231 } /* end while */
232
233} /* end _get_hdr */
234
235static void _seq_header ( void ) {
236
237 s_MPEG12Ctx.m_SI.m_Width = _MPEG_GetBits ( 12 );
238 s_MPEG12Ctx.m_SI.m_Height = _MPEG_GetBits ( 12 );
239
240 _MPEG_GetBits ( 4 ); /* aspect_ratio_information */
241 s_MPEG12Ctx.m_SI.m_MSPerFrame = ( int )(
242 ( 1000.0F / s_FrameRate[ s_MPEG12Ctx.m_FRCode = _MPEG_GetBits ( 4 ) ] ) + 0.5F
243 );
244 _MPEG_GetBits ( 18 ); /* bit_rate_value */
245 _MPEG_GetBits ( 1 ); /* marker_bit */
246 _MPEG_GetBits ( 10 ); /* vbv_buffer_size */
247 _MPEG_GetBits ( 1 ); /* constrained_parameters_flag */
248
249 if ( _MPEG_GetBits ( 1 ) )
250 _MPEG_SetQM ( 0 );
251 else _MPEG_SetDefQM ( 0 );
252
253 if ( _MPEG_GetBits ( 1 ) )
254 _MPEG_SetQM ( 1 );
255 else _MPEG_SetDefQM ( 1 );
256
257 _ext_and_ud ();
258
259} /* end _seq_header */
260
261static void _gop_header ( void ) {
262#ifdef _DEBUG
263 _MPEG_GetBits ( 1 ); /* drop_flag */
264 _MPEG_GetBits ( 5 ); /* hour */
265 _MPEG_GetBits ( 6 ); /* minute */
266 _MPEG_GetBits ( 1 ); /* marker_bit */
267 _MPEG_GetBits ( 6 ); /* sec */
268 _MPEG_GetBits ( 6 ); /* frame */
269 _MPEG_GetBits ( 1 ); /* closed_gop */
270 _MPEG_GetBits ( 1 ); /* broken_link */
271#else
272 _MPEG_GetBits ( 27 );
273#endif /* _DEBUG */
274 _ext_and_ud ();
275
276} /* end _gop_header */
277
278static void _pic_header ( void ) {
279
280 unsigned int lPicCT;
281
282 _MPEG_GetBits ( 10 ); /* temporal_reference */
283 lPicCT = _MPEG_GetBits ( 3 );
284 _MPEG_GetBits ( 16 ); /* vbv_delay */
285
286 if ( lPicCT == _MPEG_PT_P || lPicCT == _MPEG_PT_B ) {
287 s_MPEG12Ctx.m_FPFVector = _MPEG_GetBits ( 1 );
288 s_MPEG12Ctx.m_FwdFCode = _MPEG_GetBits ( 3 );
289 } /* end if */
290
291 if ( lPicCT == _MPEG_PT_B ) {
292 s_MPEG12Ctx.m_FPBVector = _MPEG_GetBits ( 1 );
293 s_MPEG12Ctx.m_BckFCode = _MPEG_GetBits ( 3 );
294 } /* end if */
295
296 _xtra_bitinf ();
297 _ext_and_ud ();
298
299 _MPEG_SetPCT ( s_MPEG12Ctx.m_PictCodingType = lPicCT );
300
301} /* end _pic_header */
302
303static void _ext_and_ud ( void ) {
304
305 int lCode, lXID;
306
307 lCode = _MPEG_NextStartCode ();
308
309 while ( lCode == _MPEG_CODE_EXTENSION || lCode == _MPEG_CODE_USER_DATA ) {
310
311 if ( lCode == _MPEG_CODE_EXTENSION ) {
312
313 _MPEG_GetBits ( 32 );
314 lXID = _MPEG_GetBits ( 4 );
315
316 switch ( lXID ) {
317
318 case _MPEG_XID_0:
319 _ext_unknown ();
320 break;
321
322 case _MPEG_XID_SEQUENCE:
323 _ext_seq ();
324 break;
325
326 case _MPEG_XID_DISPLAY:
327 _ext_seq_dsp ();
328 break;
329
330 case _MPEG_XID_QMATRIX:
331 _ext_qnt_mtx ();
332 break;
333
334 case _MPEG_XID_COPYRIGHT:
335 _ext_cpy_rgt ();
336 break;
337
338 case _MPEG_XID_SCALABLE:
339 _ext_seq_scl ();
340 break;
341
342 case _MPEG_XID_6:
343 _ext_unknown ();
344 break;
345
346 case _MPEG_XID_PIC_DSP:
347 _ext_pic_dsp ();
348 break;
349
350 case _MPEG_XID_PIC_COD:
351 _ext_pic_cod ();
352 break;
353
354 case _MPEG_XID_PIC_SSC:
355 _ext_pic_ssc ();
356 break;
357
358 case _MPEG_XID_PIC_TSC:
359 _ext_pic_tsc ();
360 break;
361
362 } /* end switch */
363
364 lCode = _MPEG_NextStartCode ();
365
366 } else { /* user data */
367
368 _MPEG_GetBits ( 32 );
369 lCode = _MPEG_NextStartCode ();
370
371 } /* end else */
372
373 } /* end while */
374
375} /* end _ext_and_ud */
376
377static void _ext_unknown ( void ) {
378
379
380} /* end _ext_unknown */
381
382static void _ext_seq ( void ) {
383
384 int lHSzX;
385 int lVSzX;
386 int lProfLevel;
387 int lFRXn, lFRXd;
388
389 s_MPEG12Ctx.m_fMPEG2 = 1;
390
391 *( volatile unsigned int* )0x10002010 &= 0xFF7FFFFF;
392
393 lProfLevel = _MPEG_GetBits ( 8 );
394 s_MPEG12Ctx.m_fProgSeq = _MPEG_GetBits ( 1 );
395 s_MPEG12Ctx.m_SI.m_ChromaFmt = _MPEG_GetBits ( 2 );
396 lHSzX = _MPEG_GetBits ( 2 );
397 lVSzX = _MPEG_GetBits ( 2 );
398#ifdef _DEBUG
399 _MPEG_GetBits ( 12 ); /* bit_rate_extension */
400 _MPEG_GetBits ( 1 ); /* marker_bit */
401 _MPEG_GetBits ( 8 ); /* vbv_buffer_size_extension */
402 _MPEG_GetBits ( 1 ); /* low_delay */
403 lFRXn = _MPEG_GetBits ( 2 );
404 lFRXd = _MPEG_GetBits ( 5 );
405#else
406 _MPEG_GetBits ( 22 );
407 lFRXn = _MPEG_GetBits ( 2 );
408 lFRXd = _MPEG_GetBits ( 5 );
409#endif /* _DEBUG */
410 s_MPEG12Ctx.m_SI.m_MSPerFrame = ( int )(
411 ( 1000.0F / (
412 s_FrameRate[ s_MPEG12Ctx.m_FRCode ] * ( ( lFRXn + 1.0F ) / ( lFRXd + 1.0F ) )
413 )
414 ) + 0.5F
415 );
416
417 if( ( lProfLevel >> 7 ) & 1 ) {
418
419 if ( ( lProfLevel & 15 ) == 5 ) {
420
421 s_MPEG12Ctx.m_SI.m_Profile = MPEG_PROFILE_422;
422 s_MPEG12Ctx.m_SI.m_Level = MPEG_LEVEL_MAIN;
423
424 } else s_MPEG12Ctx.m_SI.m_Profile = s_MPEG12Ctx.m_SI.m_Level = -1;
425
426 } else {
427
428 s_MPEG12Ctx.m_SI.m_Profile = lProfLevel >> 4;
429 s_MPEG12Ctx.m_SI.m_Level = lProfLevel & 0xF;
430
431 } /* end else */
432
433 s_MPEG12Ctx.m_SI.m_Width = ( lHSzX << 12 ) | ( s_MPEG12Ctx.m_SI.m_Width & 0x0FFF );
434 s_MPEG12Ctx.m_SI.m_Height = ( lVSzX << 12 ) | ( s_MPEG12Ctx.m_SI.m_Height & 0x0FFF );
435
436} /* end _ext_seq */
437
438static void _ext_seq_dsp ( void ) {
439
440 s_MPEG12Ctx.m_SI.m_VideoFmt = _MPEG_GetBits ( 3 );
441
442 if ( _MPEG_GetBits ( 1 ) ) { /* color_description */
443#ifdef _DEBUG
444 _MPEG_GetBits ( 8 ); /* color_primaries */
445 _MPEG_GetBits ( 8 ); /* transfer_characteristics */
446 _MPEG_GetBits ( 8 ); /* matrix_coefficients */
447#else
448 _MPEG_GetBits ( 24 );
449#endif /* _DEBUG */
450 } /* end if */
451#ifdef _DEBUG
452 _MPEG_GetBits ( 14 ); /* display_horizontal_size */
453 _MPEG_GetBits ( 1 ); /* marker_bit */
454 _MPEG_GetBits ( 14 ); /* display_vertical_size */
455#else
456 _MPEG_GetBits ( 29 );
457#endif /* _DEBUG */
458} /* end _ext_seq_dsp */
459
460static void _ext_qnt_mtx ( void ) {
461
462 int i;
463
464 if ( _MPEG_GetBits ( 1 ) ) _MPEG_SetQM ( 0 );
465 if ( _MPEG_GetBits ( 1 ) ) _MPEG_SetQM ( 1 );
466
467 if ( _MPEG_GetBits ( 1 ) ) for ( i = 0; i < 16; ++i ) _MPEG_GetBits ( 32 );
468 if ( _MPEG_GetBits ( 1 ) ) for ( i = 0; i < 16; ++i ) _MPEG_GetBits ( 32 );
469
470} /* end _ext_qnt_mtx */
471
472static void _ext_cpy_rgt ( void ) {
473#ifdef _DEBUG
474 _MPEG_GetBits ( 1 ); /* copyright_flag */
475 _MPEG_GetBits ( 8 ); /* copyright_identifier */
476 _MPEG_GetBits ( 1 ); /* original_or_copy */
477 _MPEG_GetBits ( 7 ); /* reserved_data */
478 _MPEG_GetBits ( 1 ); /* marker_bit */
479 _MPEG_GetBits ( 20 ); /* copyright_number_1 */
480 _MPEG_GetBits ( 1 ); /* marker_bit */
481 _MPEG_GetBits ( 22 ); /* copyright_number_2 */
482 _MPEG_GetBits ( 1 ); /* marker_bit */
483 _MPEG_GetBits ( 22 ); /* copyright_number_3 */
484#else
485 _MPEG_GetBits ( 32 );
486 _MPEG_GetBits ( 32 );
487 _MPEG_GetBits ( 20 );
488#endif /* _DEBUG */
489} /* end _ext_cop_rgt */
490
491static void _ext_seq_scl ( void ) {
492
493} /* end _ext_seq_scl */
494
495static void _ext_pic_dsp ( void ) {
496
497 int i;
498 int lnFCO;
499
500 if ( s_MPEG12Ctx.m_fProgSeq ) {
501
502 if ( s_MPEG12Ctx.m_fRepFF )
503 lnFCO = s_MPEG12Ctx.m_fTopFF ? 3 : 2;
504 else lnFCO = 1;
505
506 } else {
507
508 if ( s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME )
509 lnFCO = 1;
510 else lnFCO = s_MPEG12Ctx.m_fRepFF ? 3 : 2;
511
512 } /* end else */
513
514 for ( i = 0; i < lnFCO; ++i ) {
515 _MPEG_GetBits ( 16 ); /* frame_center_horizontal_offset[ i ] */
516 _MPEG_GetBits ( 1 ); /* marker_bit */
517 _MPEG_GetBits ( 16 ); /* frame_center_vertical_offset [ i ] */
518 _MPEG_GetBits ( 1 ); /* marker_bit */
519 } /* end for */
520
521} /* end _ext_pic_dsp */
522
523static void _ext_pic_cod ( void ) {
524
525 s_MPEG12Ctx.m_FCode[ 0 ][ 0 ] = _MPEG_GetBits ( 4 );
526 s_MPEG12Ctx.m_FCode[ 0 ][ 1 ] = _MPEG_GetBits ( 4 );
527 s_MPEG12Ctx.m_FCode[ 1 ][ 0 ] = _MPEG_GetBits ( 4 );
528 s_MPEG12Ctx.m_FCode[ 1 ][ 1 ] = _MPEG_GetBits ( 4 );
529 _MPEG_SetIDCP ();
530 s_MPEG12Ctx.m_PictStruct = _MPEG_GetBits ( 2 );
531 s_MPEG12Ctx.m_fTopFF = _MPEG_GetBits ( 1 );
532 s_MPEG12Ctx.m_fFPFrmDCT = _MPEG_GetBits ( 1 );
533 s_MPEG12Ctx.m_fConsMV = _MPEG_GetBits ( 1 );
534 _MPEG_SetQSTIVFAS ();
535 s_MPEG12Ctx.m_fRepFF = _MPEG_GetBits ( 1 );
536#ifdef _DEBUG
537 _MPEG_GetBits ( 1 ); /* chroma_420_type */
538 _MPEG_GetBits ( 1 ); /* progressive_frame */
539#else
540 _MPEG_GetBits ( 2 );
541#endif /* _DEBUG */
542 if ( _MPEG_GetBits ( 1 ) ) { /* composite_display_flag */
543#ifdef _DEBUG
544 _MPEG_GetBits ( 1 ); /* v_axis */
545 _MPEG_GetBits ( 3 ); /* field_sequence */
546 _MPEG_GetBits ( 1 ); /* sub_carrier */
547 _MPEG_GetBits ( 7 ); /* burst_amplitude */
548 _MPEG_GetBits ( 8 ); /* sub_carrier_phase */
549#else
550 _MPEG_GetBits ( 20 );
551#endif /* _DEBUG */
552 } /* end if */
553
554} /* end _ext_pic_cod */
555
556static void _ext_pic_ssc ( void ) {
557
558} /* end _ext_pic_ssc */
559
560static void _ext_pic_tsc ( void ) {
561
562} /* end _ext_pic_tsc */
563
564static void _xtra_bitinf ( void ) {
565
566 while ( _MPEG_GetBits ( 1 ) ) _MPEG_GetBits ( 8 );
567
568} /* end _xtra_bitinf */
569
570static int _get_first_picture ( void* apData, s64* apPTS ) {
571
572 int retVal = _get_hdr ();
573
574 if ( retVal ) {
575
576 s_MPEG12Ctx.m_SI.m_FrameCnt = 0;
577
578 apData = _init_seq ();
579 _mpeg12_picture_data ();
580
581 if ( s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME ) s_MPEG12Ctx.m_fSecField ^= 1;
582
583 MPEG_Picture = _get_next_picture;
584
585 if ( !s_MPEG12Ctx.m_fSecField ) ++s_MPEG12Ctx.m_SI.m_FrameCnt;
586
587 retVal = _get_next_picture ( apData, apPTS );
588
589 } /* end if */
590
591 return retVal;
592
593} /* end _get_first_picture */
594
595static int _get_next_picture ( void* apData, s64* apPTS ) {
596
597 int retVal;
598
599 while ( 1 ) {
600
601 if ( ( retVal = _get_hdr () ) ) {
602
603 int lfPic = 0;
604
605 _mpeg12_picture_data ();
606
607 if ( ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME || s_MPEG12Ctx.m_fSecField ) && s_MPEG12Ctx.m_SI.m_FrameCnt ) {
608
609 void* lpData;
610
611 if ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_B ) {
612 lpData = s_MPEG12Ctx.m_pAuxFrame;
613 *apPTS = s_MPEG12Ctx.m_AuxPTS;
614 } else {
615 lpData = s_MPEG12Ctx.m_pFwdFrame;
616 *apPTS = s_MPEG12Ctx.m_FwdPTS;
617 } /* end else */
618
619 lfPic = _MPEG_CSCImage ( lpData, apData, s_MPEG12Ctx.m_MBCount );
620
621 } /* end if */
622
623 if ( s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME ) s_MPEG12Ctx.m_fSecField ^= 1;
624
625 if ( !s_MPEG12Ctx.m_fSecField ) ++s_MPEG12Ctx.m_SI.m_FrameCnt;
626
627 if ( lfPic ) break;
628
629 } else break;
630
631 } /* end while */
632
633 return retVal;
634
635} /* end _get_next_picture */
636
637static void _mpeg12_do_next_mc ( void ) {
638
639 _MPEGMotions* lpMotions = &s_MPEG12Ctx.m_MC[ !s_MPEG12Ctx.m_CurMC ];
640 _MPEGMotion* lpMotion = &lpMotions -> m_Motion[ 0 ];
641
642 while ( lpMotion -> MC_Luma ) {
643
644 _MPEG_do_mc ( lpMotion );
645 ++lpMotion;
646
647 } /* end while */
648
649 lpMotions -> BlockOp ( lpMotions );
650
651} /* end _mpeg12_do_next_mc */
652
653static void _mpeg12_do_first_mc ( void ) {
654
655 DoMC = _mpeg12_do_next_mc;
656
657} /* end _mpeg12_do_first_mc */
658
659static void _mpeg12_picture_data ( void ) {
660
661 int lMBAMax = s_MPEG12Ctx.m_MBWidth * s_MPEG12Ctx.m_MBHeight;
662 _MPEGMacroBlock8* lpMB;
663 s64 lPTS;
664
665 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME && s_MPEG12Ctx.m_fSecField ) s_MPEG12Ctx.m_fSecField = 0;
666
667 if ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_B ) {
668 s_MPEG12Ctx.m_pCurFrame = s_MPEG12Ctx.m_pAuxFrame;
669 s_MPEG12Ctx.m_AuxPTS = *s_pCurPTS;
670 } else {
671 if ( !s_MPEG12Ctx.m_fSecField ) {
672 lpMB = s_MPEG12Ctx.m_pFwdFrame;
673 lPTS = s_MPEG12Ctx.m_FwdPTS;
674 s_MPEG12Ctx.m_pFwdFrame = s_MPEG12Ctx.m_pBckFrame;
675 s_MPEG12Ctx.m_FwdPTS = s_MPEG12Ctx.m_BckPTS;
676 s_MPEG12Ctx.m_pBckFrame = lpMB;
677 s_MPEG12Ctx.m_BckPTS = lPTS;
678 } /* end if */
679 s_MPEG12Ctx.m_pCurFrame = s_MPEG12Ctx.m_pBckFrame;
680 s_MPEG12Ctx.m_BckPTS = *s_pCurPTS;
681 } /* end else */
682 s_MPEG12Ctx.m_pCurFrameY = ( unsigned char* )s_MPEG12Ctx.m_pCurFrame;
683 s_MPEG12Ctx.m_pCurFrameCbCr = ( unsigned char* )s_MPEG12Ctx.m_pCurFrame + 256;
684 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD ) {
685 s_MPEG12Ctx.m_pCurFrameY += 16;
686 s_MPEG12Ctx.m_pCurFrameCbCr += 8;
687 } /* end if */
688
689 if ( s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME ) lMBAMax >>= 1;
690
691 s_MPEG12Ctx.m_CurMC = 0;
692 DoMC = _mpeg12_do_first_mc;
693
694 while ( _mpeg12_slice ( lMBAMax ) >= 0 );
695
696 _MPEG_WaitBDEC ();
697 DoMC ();
698
699} /* end _mpeg12_picture_data */
700
701static void _mpeg12_decode_motion_vector (
702 int* apPred, int aRSize, int aMotionCode, int aMotionResidual, int aFullPelVector
703 ) {
704
705 int lLim = 16 << aRSize;
706 int lVec = aFullPelVector ? *apPred >> 1 : *apPred;
707
708 if ( aMotionCode > 0 ) {
709
710 lVec += ( ( aMotionCode - 1 ) << aRSize ) + aMotionResidual + 1;
711
712 if ( lVec >= lLim ) lVec -= lLim + lLim;
713
714 } else if ( aMotionCode < 0 ) {
715
716 lVec -= ( ( -aMotionCode - 1 ) << aRSize ) + aMotionResidual + 1;
717
718 if ( lVec < -lLim ) lVec += lLim + lLim;
719
720 } /* end if */
721
722 *apPred = aFullPelVector ? lVec << 1 : lVec;
723
724} /* end _mpeg12_decode_motion_vector */
725
726static void _mpeg12_dual_prime_vector ( int aDMV[][ 2 ], const int* apDMVector, int aMVX, int aMVY ) {
727
728 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME ) {
729
730 if ( s_MPEG12Ctx.m_fTopFF ) {
731
732 aDMV[ 0 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
733 aDMV[ 0 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] - 1;
734
735 aDMV[ 1 ][ 0 ] = ( ( 3 * aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
736 aDMV[ 1 ][ 1 ] = ( ( 3 * aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] + 1;
737
738 } else {
739
740 aDMV[ 0 ][ 0 ] = ( ( 3 * aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
741 aDMV[ 0 ][ 1 ] = ( ( 3 * aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] - 1;
742
743 aDMV[ 1 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
744 aDMV[ 1 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] + 1;
745
746 } /* end else */
747
748 } else {
749
750 aDMV[ 0 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
751 aDMV[ 0 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ];
752
753 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_TOP_FIELD )
754 --aDMV[ 0 ][ 1 ];
755 else ++aDMV[ 0 ][ 1 ];
756
757 } /* end else */
758
759} /* end _mpeg12_dual_prime_vector */
760
761static void _mpeg12_motion_vector (
762 int* apPMV, int* apDMVector, int aHRSize, int aVRSize, int aDMV, int aMVScale, int aFullPelVector
763 ) {
764
765 int lMotionCode = _MPEG_GetMotionCode ();
766 int lMotionResidual = aHRSize && lMotionCode ? _MPEG_GetBits ( aHRSize ) : 0;
767
768 _mpeg12_decode_motion_vector (
769 &apPMV[ 0 ], aHRSize, lMotionCode, lMotionResidual, aFullPelVector
770 );
771
772 if ( aDMV ) apDMVector[ 0 ] = _MPEG_GetDMVector ();
773
774 s_MPEG12Ctx.m_fError = lMotionCode == -32768;
775
776 lMotionCode = _MPEG_GetMotionCode ();
777 lMotionResidual = aVRSize && lMotionCode ? _MPEG_GetBits ( aVRSize ) : 0;
778
779 if ( aMVScale ) apPMV[ 1 ] >>= 1;
780
781 _mpeg12_decode_motion_vector (
782 &apPMV[ 1 ], aVRSize, lMotionCode, lMotionResidual, aFullPelVector
783 );
784
785 if ( aMVScale ) apPMV[ 1 ] <<= 1;
786
787 if ( aDMV ) apDMVector[ 1 ] = _MPEG_GetDMVector ();
788
789 s_MPEG12Ctx.m_fError = lMotionCode == -32768;
790
791} /* end _mpeg12_motion_vector */
792
793static void _mpeg12_motion_vectors (
794 int aPMV[ 2 ][ 2 ][ 2 ], int aDMVector[ 2 ], int aMVFS[ 2 ][ 2 ], int aS, int anMV,
795 int aMVFmt, int aHRSize, int aVRSize, int aDMV, int aMVScale
796 ) {
797
798 if ( anMV == 1 ) {
799
800 if ( aMVFmt == _MPEG_MV_FIELD && !aDMV ) aMVFS[ 1 ][ aS ] = aMVFS[ 0 ][ aS ] = _MPEG_GetBits ( 1 );
801
802 _mpeg12_motion_vector ( aPMV[ 0 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
803
804 aPMV[ 1 ][ aS ][ 0 ] = aPMV[ 0 ][ aS ][ 0 ];
805 aPMV[ 1 ][ aS ][ 1 ] = aPMV[ 0 ][ aS ][ 1 ];
806
807 } else {
808
809 aMVFS[ 0 ][ aS ] = _MPEG_GetBits ( 1 );
810 _mpeg12_motion_vector ( aPMV[ 0 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
811
812 aMVFS[ 1 ][ aS ] = _MPEG_GetBits ( 1 );
813 _mpeg12_motion_vector ( aPMV[ 1 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
814
815 } /* end else */
816
817} /* end _mpeg12_motion_vectors */
818
819static int _mpeg12_dec_mb (
820 int* apMBType, int* apMotionType,
821 int aPMV[ 2 ][ 2 ][ 2 ], int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ]
822 ) {
823
824 int lMBType;
825 int lDMV;
826 int lMVScale;
827 int lnMV;
828 int lMVFmt;
829 int lDCType;
830 int lMotionType;
831 int lfIntra;
832
833 lMotionType = 0;
834 lMBType = _MPEG_GetMBType ();
835
836 if ( !lMBType ) return 0;
837
838 lfIntra = lMBType & _MPEG_MBT_INTRA;
839
840 if ( lMBType & ( _MPEG_MBT_MOTION_FORWARD | _MPEG_MBT_MOTION_BACKWARD ) ) {
841
842 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME )
843 lMotionType = s_MPEG12Ctx.m_fFPFrmDCT ? _MPEG_MC_FRAME : _MPEG_GetBits ( 2 );
844 else lMotionType = _MPEG_GetBits ( 2 );
845
846 } else if ( lfIntra && s_MPEG12Ctx.m_fConsMV )
847 lMotionType = ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME ) ? _MPEG_MC_FRAME : _MPEG_MC_FIELD;
848
849 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME ) {
850 lnMV = lMotionType == _MPEG_MC_FIELD ? 2 : 1;
851 lMVFmt = lMotionType == _MPEG_MC_FRAME ? _MPEG_MV_FRAME : _MPEG_MV_FIELD;
852 } else {
853 lnMV = ( lMotionType == _MPEG_MC_16X8 ) ? 2 : 1;
854 lMVFmt = _MPEG_MV_FIELD;
855 } /* end else */
856
857 lDMV = lMotionType == _MPEG_MC_DMV;
858 lMVScale = lMVFmt == _MPEG_MV_FIELD && s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME;
859 lDCType = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME &&
860 !s_MPEG12Ctx.m_fFPFrmDCT &&
861 lMBType & ( _MPEG_MBT_PATTERN | _MPEG_MBT_INTRA ) ? _MPEG_GetBits ( 1 ) : 0;
862
863 if ( lMBType & _MPEG_MBT_QUANT ) s_MPEG12Ctx.m_QScale = _MPEG_GetBits ( 5 );
864
865 if ( ( lMBType & _MPEG_MBT_MOTION_FORWARD ) ||
866 ( lfIntra && s_MPEG12Ctx.m_fConsMV )
867 ) {
868
869 if ( s_MPEG12Ctx.m_fMPEG2 )
870 _mpeg12_motion_vectors (
871 aPMV, aDMVector, aMVFS, 0, lnMV, lMVFmt,
872 s_MPEG12Ctx.m_FCode[ 0 ][ 0 ] - 1, s_MPEG12Ctx.m_FCode[ 0 ][ 1 ] - 1, lDMV, lMVScale
873 );
874 else _mpeg12_motion_vector (
875 aPMV[ 0 ][ 0 ], aDMVector, s_MPEG12Ctx.m_FwdFCode - 1, s_MPEG12Ctx.m_FwdFCode - 1,
876 0, 0, s_MPEG12Ctx.m_FPFVector
877 );
878 } /* end if */
879
880 if ( s_MPEG12Ctx.m_fError ) return 0;
881
882 if ( lMBType & _MPEG_MBT_MOTION_BACKWARD ) {
883
884 if ( s_MPEG12Ctx.m_fMPEG2 )
885 _mpeg12_motion_vectors (
886 aPMV, aDMVector, aMVFS, 1, lnMV, lMVFmt,
887 s_MPEG12Ctx.m_FCode[ 1 ][ 0 ] - 1, s_MPEG12Ctx.m_FCode[ 1 ][ 1 ] - 1, 0, lMVScale
888 );
889 else _mpeg12_motion_vector (
890 aPMV[ 0 ][ 1 ], aDMVector, s_MPEG12Ctx.m_BckFCode - 1, s_MPEG12Ctx.m_BckFCode - 1,
891 0, 0, s_MPEG12Ctx.m_FPBVector
892 );
893 } /* end if */
894
895 if ( s_MPEG12Ctx.m_fError ) return 0;
896
897 if ( lfIntra && s_MPEG12Ctx.m_fConsMV ) _MPEG_GetBits ( 1 );
898
899 if ( lMBType & ( _MPEG_MBT_INTRA | _MPEG_MBT_PATTERN ) )
900 _MPEG_BDEC ( lfIntra, s_MPEG12Ctx.m_fDCRst, lDCType, s_MPEG12Ctx.m_QScale, s_MPEG12Ctx.m_pCurMotions -> m_pSPRBlk );
901
902 s_MPEG12Ctx.m_fDCRst = !lfIntra;
903
904 if ( lfIntra && !s_MPEG12Ctx.m_fConsMV )
905 aPMV[ 0 ][ 0 ][ 0 ] = aPMV[ 0 ][ 0 ][ 1 ] =
906 aPMV[ 1 ][ 0 ][ 0 ] = aPMV[ 1 ][ 0 ][ 1 ] =
907 aPMV[ 0 ][ 1 ][ 0 ] = aPMV[ 0 ][ 1 ][ 1 ] =
908 aPMV[ 1 ][ 1 ][ 0 ] = aPMV[ 1 ][ 1 ][ 1 ] = 0;
909
910 if ( ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P ) &&
911 !( lMBType & ( _MPEG_MBT_MOTION_FORWARD | _MPEG_MBT_INTRA ) )
912 ) {
913
914 aPMV[ 0 ][ 0 ][ 0 ] = aPMV[ 0 ][ 0 ][ 1 ] =
915 aPMV[ 1 ][ 0 ][ 0 ] = aPMV[ 1 ][ 0 ][ 1 ] = 0;
916
917 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME )
918 lMotionType = _MPEG_MC_FRAME;
919 else {
920 lMotionType = _MPEG_MC_FIELD;
921 aMVFS[ 0 ][ 0 ] = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD;
922 } /* end else */
923
924 } /* end if */
925
926 *apMBType = lMBType;
927 *apMotionType = lMotionType;
928
929 return 1;
930
931} /* end _mpeg12_dec_mb */
932
933static void _mpeg12_get_ref (
934 _MPEGMacroBlock8* apMBSrc, int aX, int anY,
935 int aDX, int aDY, int aH, int aFSrc, int aFDst, int afAvg
936 ) {
937
938 int lfInt = ( aH & 8 ) >> 3;
939 int lDXY = ( ( aDY & 1 ) << 1 ) | ( aDX & 1 );
940 int lUVXY = ( ( aDX & 2 ) >> 1 ) | ( aDY & 2 );
941 int lSrcX = aX + ( aDX >> 1 );
942 int lSrcY = ( ( anY + ( aDY >> 1 ) ) << lfInt ) + aFSrc;
943 int lMBX = lSrcX >> 4;
944 int lMBY = lSrcY >> 4;
945 _MPEGMotion* lpMotion = &s_MPEG12Ctx.m_pCurMotions -> m_Motion[ s_MPEG12Ctx.m_pCurMotions -> m_nMotions++ ];
946
947 afAvg <<= 2;
948
949 __asm__ __volatile__(
950 ".set push\n\t"
951 ".set noat\n\t"
952 "pnor $v0, $zero, $zero\n\t"
953 "ld $at, %4\n\t"
954 "pextlw %0, %3, %0\n\t"
955 "paddw $at, $at, $v0\n\t"
956 "pmaxw %0, %0, $zero\n\t"
957 "pminw %0, %0, $at\n\t"
958 "dsrl32 %1, %0, 0\n\t"
959 "sll %0, %0, 0\n\t"
960 ".set pop\n\t"
961 : "=r"( lMBX ), "=r"( lMBY ) : "r"( lMBX ), "r"( lMBY ), "m"( s_MPEG12Ctx.m_MBWidth ) : "at", "v0"
962 );
963
964 lpMotion -> m_pSrc = ( unsigned char* )( apMBSrc + lMBX + lMBY * s_MPEG12Ctx.m_MBWidth );
965 lpMotion -> m_pDstY = ( short* )( s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes + ( aFDst << 5 ) );
966 lpMotion -> m_pDstCbCr = ( short* )( s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes + 512 + ( aFDst << 3 ) );
967 lpMotion -> m_X = lSrcX & 0xF;
968 lpMotion -> m_Y = lSrcY & 0xF;
969 lpMotion -> m_H = aH;
970 lpMotion -> m_fInt = lfInt;
971 lpMotion -> m_Field = aFSrc;
972 lpMotion -> MC_Luma = LumaOp [ lDXY + afAvg ];
973 lpMotion -> MC_Chroma = ChromaOp[ lUVXY + afAvg ];
974
975} /* end _mpeg12_get_ref */
976
977static void _mpeg12_get_refs (
978 int aBX, int aBY, int aMBType, int aMotionType, int aPMV[ 2 ][ 2 ][ 2 ],
979 int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ]
980 ) {
981
982 int lfAdd = 0;
983
984 s_MPEG12Ctx.m_pCurMotions -> m_nMotions = 0;
985
986 if ( ( aMBType & _MPEG_MBT_MOTION_FORWARD ) ||
987 ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P )
988 ) {
989
990 int lDMV[ 2 ][ 2 ];
991
992 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME ) {
993
994 if ( ( aMotionType == _MPEG_MC_FRAME ) ||
995 !( aMBType & _MPEG_MBT_MOTION_FORWARD )
996 ) {
997
998 _mpeg12_get_ref (
999 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ],
1000 16, 0, 0, 0
1001 );
1002
1003 } else if ( aMotionType == _MPEG_MC_FIELD ) {
1004
1005 _mpeg12_get_ref (
1006 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1007 8, aMVFS[ 0 ][ 0 ], 0, 0
1008 );
1009 _mpeg12_get_ref (
1010 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 1 ][ 0 ][ 0 ], aPMV[ 1 ][ 0 ][ 1 ] >> 1,
1011 8, aMVFS[ 1 ][ 0 ], 8, 0
1012 );
1013
1014 } else if ( aMotionType == _MPEG_MC_DMV ) {
1015
1016 _mpeg12_dual_prime_vector ( lDMV, aDMVector, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1 );
1017
1018 _mpeg12_get_ref (
1019 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1020 8, 0, 0, 0
1021 );
1022 _mpeg12_get_ref (
1023 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, lDMV[ 0 ][ 0 ], lDMV[ 0 ][ 1 ],
1024 8, 1, 0, 1
1025 );
1026 _mpeg12_get_ref (
1027 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1028 8, 1, 8, 0
1029 );
1030 _mpeg12_get_ref (
1031 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, lDMV[ 1 ][ 0 ], lDMV[ 1 ][ 1 ],
1032 8, 0, 8, 1
1033 );
1034
1035 } /* end if */
1036
1037 } else {
1038
1039 int lCurField;
1040 _MPEGMacroBlock8* lpMBSrc;
1041
1042 lCurField = ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD );
1043 lpMBSrc = ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P ) &&
1044 s_MPEG12Ctx.m_fSecField &&
1045 ( lCurField != aMVFS[ 0 ][ 0 ] ) ? s_MPEG12Ctx.m_pBckFrame
1046 : s_MPEG12Ctx.m_pFwdFrame;
1047
1048 if ( ( aMotionType == _MPEG_MC_FIELD ) || !( aMBType & _MPEG_MBT_MOTION_FORWARD ) ) {
1049
1050 _mpeg12_get_ref (
1051 lpMBSrc, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1052 aMVFS[ 0 ][ 0 ], 0, 0
1053 );
1054 _mpeg12_get_ref (
1055 lpMBSrc, aBX, aBY + 8, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1056 aMVFS[ 0 ][ 0 ], 8, 0
1057 );
1058
1059 } else if ( aMotionType == _MPEG_MC_16X8 ) {
1060
1061 _mpeg12_get_ref (
1062 lpMBSrc, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1063 aMVFS[ 0 ][ 0 ], 0, 0
1064 );
1065
1066 lpMBSrc = ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P ) &&
1067 s_MPEG12Ctx.m_fSecField &&
1068 ( lCurField != aMVFS[ 1 ][ 0 ] ) ? s_MPEG12Ctx.m_pBckFrame
1069 : s_MPEG12Ctx.m_pFwdFrame;
1070 _mpeg12_get_ref (
1071 lpMBSrc, aBX, aBY + 8, aPMV[ 1 ][ 0 ][ 0 ], aPMV[ 1 ][ 0 ][ 1 ], 8,
1072 aMVFS[ 1 ][ 0 ], 8, 0
1073 );
1074
1075 } else if ( aMotionType == _MPEG_MC_DMV ) {
1076
1077 lpMBSrc = s_MPEG12Ctx.m_fSecField ? s_MPEG12Ctx.m_pBckFrame : s_MPEG12Ctx.m_pFwdFrame;
1078
1079 _mpeg12_dual_prime_vector ( lDMV, aDMVector, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] );
1080
1081 _mpeg12_get_ref (
1082 s_MPEG12Ctx.m_pFwdFrame, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 16,
1083 lCurField, 0, 0
1084 );
1085 _mpeg12_get_ref (
1086 lpMBSrc, aBX, aBY, lDMV[ 0 ][ 0 ], lDMV[ 0 ][ 1 ], 16, !lCurField, 1, 1
1087 );
1088
1089 } /* end if */
1090
1091 } /* end else */
1092
1093 lfAdd = 1;
1094
1095 } /* end if */
1096
1097 if ( aMBType & _MPEG_MBT_MOTION_BACKWARD ) {
1098
1099 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME ) {
1100
1101 if ( aMotionType == _MPEG_MC_FRAME ) {
1102
1103 _mpeg12_get_ref (
1104 s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ],
1105 16, 0, 0, lfAdd
1106 );
1107
1108 } else {
1109
1110 _mpeg12_get_ref (
1111 s_MPEG12Ctx.m_pBckFrame, aBX, aBY >> 1, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ] >> 1,
1112 8, aMVFS[ 0 ][ 1 ], 0, lfAdd
1113 );
1114 _mpeg12_get_ref (
1115 s_MPEG12Ctx.m_pBckFrame, aBX, aBY >> 1, aPMV[ 1 ][ 1 ][ 0 ], aPMV[ 1 ][ 1 ][ 1 ] >> 1,
1116 8, aMVFS[ 1 ][ 1 ], 8, lfAdd
1117 );
1118
1119 } /* end else */
1120
1121 } else {
1122
1123 if ( aMotionType == _MPEG_MC_FIELD ) {
1124
1125 _mpeg12_get_ref (
1126 s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ], 8,
1127 aMVFS[ 0 ][ 1 ], 0, lfAdd
1128 );
1129 _mpeg12_get_ref (
1130 s_MPEG12Ctx.m_pBckFrame, aBX, aBY + 8, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ], 8,
1131 aMVFS[ 0 ][ 1 ], 8, lfAdd
1132 );
1133
1134 } else if ( aMotionType == _MPEG_MC_16X8 ) {
1135
1136 _mpeg12_get_ref (
1137 s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ],
1138 8, aMVFS[ 0 ][ 1 ], 0, lfAdd
1139 );
1140 _mpeg12_get_ref (
1141 s_MPEG12Ctx.m_pBckFrame, aBX, aBY + 8, aPMV[ 1 ][ 1 ][ 0 ], aPMV[ 1 ][ 1 ][ 1 ],
1142 8, aMVFS[ 1 ][ 1 ], 8, lfAdd
1143 );
1144
1145 } /* end if */
1146
1147 } /* end else */
1148
1149 } /* end if */
1150
1151 _MPEG_dma_ref_image (
1152 ( _MPEGMacroBlock8* )s_MPEG12Ctx.m_pCurMotions -> m_pSPRMC,
1153 &s_MPEG12Ctx.m_pCurMotions -> m_Motion[ 0 ],
1154 s_MPEG12Ctx.m_pCurMotions -> m_nMotions, s_MPEG12Ctx.m_MBWidth
1155 );
1156
1157} /* end _mpeg12_get_refs */
1158
1159static void _mpeg2_mc (
1160 int aMBA, int aMBType, int aMotionType, int aPMV[ 2 ][ 2 ][ 2 ],
1161 int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ], int aMBAI
1162 ){
1163
1164 int lBX, lBY;
1165 int lfField;
1166 int lfIntra;
1167 int lOffset;
1168 int lfNoSkip;
1169 int lfFiledMV;
1170
1171 lBX = s_MPEG12Ctx.m_pMBXY[ aMBA ].m_X;
1172 lBY = s_MPEG12Ctx.m_pMBXY[ aMBA ].m_Y;
1173 lfField = s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME;
1174 lOffset = ( lBY * ( s_MPEG12Ctx.m_MBWidth << lfField ) + lBX ) * sizeof ( _MPEGMacroBlock8 );
1175 lfIntra = aMBType & _MPEG_MBT_INTRA;
1176 lfNoSkip = aMBAI == 1;
1177 lfFiledMV = aMotionType & _MPEG_MC_FIELD;
1178
1179 s_MPEG12Ctx.m_pCurMotions -> m_Stride = s_MPEG12Ctx.m_MBStride;
1180 s_MPEG12Ctx.m_pCurMotions -> m_pMBDstY = UNCACHED_SEG( s_MPEG12Ctx.m_pCurFrameY + lOffset );
1181 s_MPEG12Ctx.m_pCurMotions -> m_pMBDstCbCr = UNCACHED_SEG( s_MPEG12Ctx.m_pCurFrameCbCr + lOffset );
1182
1183 lBX <<= 4;
1184 lBY <<= 4;
1185
1186 if ( !lfIntra ) {
1187 _mpeg12_get_refs ( lBX, lBY, aMBType, aMotionType, aPMV, aMVFS, aDMVector );
1188 if ( lfNoSkip && ( aMBType & _MPEG_MBT_PATTERN ) )
1189 s_MPEG12Ctx.m_pCurMotions -> BlockOp = AddBlockOp[ lfField ][ lfFiledMV ];
1190 else {
1191 s_MPEG12Ctx.m_pCurMotions -> m_pSrc = s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes;
1192 s_MPEG12Ctx.m_pCurMotions -> BlockOp = PutBlockOp[ !lfField + ( lfNoSkip && lfFiledMV && !lfField ) ];
1193 } /* end else */
1194 } else {
1195 s_MPEG12Ctx.m_pCurMotions -> m_Motion[ 0 ].MC_Luma = NULL;
1196 s_MPEG12Ctx.m_pCurMotions -> m_pSrc = s_MPEG12Ctx.m_pCurMotions -> m_pSPRBlk;
1197 s_MPEG12Ctx.m_pCurMotions -> BlockOp = PutBlockOp[ !lfField ];
1198 } /* end else */
1199
1200} /* end _mpeg2_mc */
1201
1202static int _mpeg12_slice ( int aMBAMax ) {
1203
1204 int lPMV [ 2 ][ 2 ][ 2 ];
1205 int lMVFS [ 2 ][ 2 ];
1206 int lDMVector[ 2 ];
1207 int lMBA, lMBAI, lMBType, lMotionType;
1208 int retVal;
1209
1210 s_MPEG12Ctx.m_fError = 0;
1211 lMBType = _MPEG_NextStartCode ();
1212
1213 if ( lMBType < _MPEG_CODE_SLICE_MIN || lMBType > _MPEG_CODE_SLICE_MAX ) return -1;
1214
1215 _MPEG_GetBits ( 32 );
1216
1217 s_MPEG12Ctx.m_QScale = _MPEG_GetBits ( 5 );
1218
1219 if ( _MPEG_GetBits ( 1 ) ) {
1220 _MPEG_GetBits ( 8 );
1221 _xtra_bitinf ();
1222 } /* end if */
1223
1224 lMBAI = _MPEG_GetMBAI ();
1225
1226 if ( lMBAI ) {
1227
1228 lMBA = ( ( lMBType & 255 ) - 1 ) * s_MPEG12Ctx.m_MBWidth + lMBAI - 1;
1229
1230 lMBAI =
1231 s_MPEG12Ctx.m_fDCRst = 1;
1232
1233 lPMV[ 0 ][ 0 ][ 0 ] = lPMV[ 0 ][ 0 ][ 1 ] = lPMV[ 1 ][ 0 ][ 0 ] = lPMV[ 1 ][ 0 ][ 1 ] = 0;
1234 lPMV[ 0 ][ 1 ][ 0 ] = lPMV[ 0 ][ 1 ][ 1 ] = lPMV[ 1 ][ 1 ][ 0 ] = lPMV[ 1 ][ 1 ][ 1 ] = 0;
1235
1236 } else return 0;
1237
1238 while ( 1 ) {
1239
1240 s_MPEG12Ctx.m_pCurMotions = &s_MPEG12Ctx.m_MC[ s_MPEG12Ctx.m_CurMC ];
1241
1242 if ( lMBA >= aMBAMax || !_MPEG_WaitBDEC () ) return -1;
1243
1244 if ( !lMBAI ) {
1245
1246 if ( !_MPEG_ShowBits ( 23 ) || s_MPEG12Ctx.m_fError ) {
1247resync:
1248 s_MPEG12Ctx.m_fError = 0;
1249 return 0;
1250
1251 } else {
1252
1253 lMBAI = _MPEG_GetMBAI ();
1254
1255 if ( !lMBAI ) goto resync;
1256
1257 } /* end else */
1258
1259 } /* end if */
1260
1261 if ( lMBA >= aMBAMax ) return -1;
1262
1263 if ( lMBAI == 1 ) {
1264
1265 retVal = _mpeg12_dec_mb ( &lMBType, &lMotionType, lPMV, lMVFS, lDMVector );
1266
1267 if ( retVal < 0 ) return retVal;
1268 if ( !retVal ) goto resync;
1269
1270 } else { /* skipped macroblock */
1271
1272 s_MPEG12Ctx.m_fDCRst = 1;
1273
1274 if ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P )
1275 lPMV[ 0 ][ 0 ][ 0 ] = lPMV[ 0 ][ 0 ][ 1 ] =
1276 lPMV[ 1 ][ 0 ][ 0 ] = lPMV[ 1 ][ 0 ][ 1 ] = 0;
1277 if ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME )
1278 lMotionType = _MPEG_MC_FRAME;
1279 else {
1280 lMotionType = _MPEG_MC_FIELD;
1281 lMVFS[ 0 ][ 0 ] =
1282 lMVFS[ 0 ][ 1 ] = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD;
1283 } /* end else */
1284
1285 lMBType &= ~_MPEG_MBT_INTRA;
1286
1287 } /* end else */
1288
1289 _mpeg2_mc ( lMBA, lMBType, lMotionType, lPMV, lMVFS, lDMVector, lMBAI );
1290
1291 DoMC ();
1292
1293 ++lMBA;
1294 --lMBAI;
1295
1296 s_MPEG12Ctx.m_CurMC ^= 1;
1297
1298 if ( lMBA >= aMBAMax ) return -1;
1299
1300 } /* end while */
1301
1302} /* end _mpeg12_slice */