1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
7 #define CLIP(x) ((x) >= 0 ? ((x) < 255 ? (x) : 255) : 0)
10 int mpeg3_new_slice_buffer(mpeg3_slice_buffer_t
*slice_buffer
)
12 pthread_mutexattr_t mutex_attr
;
14 slice_buffer
->data
= malloc(1024);
15 slice_buffer
->buffer_size
= 0;
16 slice_buffer
->buffer_allocation
= 1024;
17 slice_buffer
->current_position
= 0;
18 slice_buffer
->bits_size
= 0;
19 slice_buffer
->bits
= 0;
20 slice_buffer
->done
= 0;
21 pthread_mutexattr_init(&mutex_attr
);
22 // pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
23 pthread_mutex_init(&(slice_buffer
->completion_lock
), &mutex_attr
);
27 int mpeg3_delete_slice_buffer(mpeg3_slice_buffer_t
*slice_buffer
)
29 free(slice_buffer
->data
);
30 pthread_mutex_destroy(&(slice_buffer
->completion_lock
));
34 int mpeg3_expand_slice_buffer(mpeg3_slice_buffer_t
*slice_buffer
)
37 unsigned char *new_buffer
= malloc(slice_buffer
->buffer_allocation
* 2);
38 for(i
= 0; i
< slice_buffer
->buffer_size
; i
++)
39 new_buffer
[i
] = slice_buffer
->data
[i
];
40 free(slice_buffer
->data
);
41 slice_buffer
->data
= new_buffer
;
42 slice_buffer
->buffer_allocation
*= 2;
46 /* limit coefficients to -2048..2047 */
48 /* move/add 8x8-Block from block[comp] to refframe */
50 static inline int mpeg3video_addblock(mpeg3_slice_t
*slice
,
61 int spar
= slice
->sparse
[comp
];
62 /* color component index */
63 cc
= (comp
< 4) ? 0 : (comp
& 1) + 1;
68 if(video
->pict_struct
== FRAME_PICTURE
)
72 /* field DCT coding */
73 rfp
= video
->newframe
[0] +
74 video
->coded_picture_width
* (by
+ ((comp
& 2) >> 1)) + bx
+ ((comp
& 1) << 3);
75 iincr
= (video
->coded_picture_width
<< 1);
79 /* frame DCT coding */
80 rfp
= video
->newframe
[0] +
81 video
->coded_picture_width
* (by
+ ((comp
& 2) << 2)) + bx
+ ((comp
& 1) << 3);
82 iincr
= video
->coded_picture_width
;
88 rfp
= video
->newframe
[0] +
89 (video
->coded_picture_width
<< 1) * (by
+ ((comp
& 2) << 2)) + bx
+ ((comp
& 1) << 3);
90 iincr
= (video
->coded_picture_width
<< 1);
97 /* scale coordinates */
98 if(video
->chroma_format
!= CHROMA444
) bx
>>= 1;
99 if(video
->chroma_format
== CHROMA420
) by
>>= 1;
100 if(video
->pict_struct
== FRAME_PICTURE
)
102 if(dct_type
&& (video
->chroma_format
!= CHROMA420
))
104 /* field DCT coding */
105 rfp
= video
->newframe
[cc
]
106 + video
->chrom_width
* (by
+ ((comp
& 2) >> 1)) + bx
+ (comp
& 8);
107 iincr
= (video
->chrom_width
<< 1);
111 /* frame DCT coding */
112 rfp
= video
->newframe
[cc
]
113 + video
->chrom_width
* (by
+ ((comp
& 2) << 2)) + bx
+ (comp
& 8);
114 iincr
= video
->chrom_width
;
120 rfp
= video
->newframe
[cc
]
121 + (video
->chrom_width
<< 1) * (by
+ ((comp
& 2) << 2)) + bx
+ (comp
& 8);
122 iincr
= (video
->chrom_width
<< 1);
126 bp
= slice
->block
[comp
];
130 for(i
= 0; i
< 8; i
++)
132 rfp
[0] = CLIP(bp
[0] + rfp
[0]);
133 rfp
[1] = CLIP(bp
[1] + rfp
[1]);
134 rfp
[2] = CLIP(bp
[2] + rfp
[2]);
135 rfp
[3] = CLIP(bp
[3] + rfp
[3]);
136 rfp
[4] = CLIP(bp
[4] + rfp
[4]);
137 rfp
[5] = CLIP(bp
[5] + rfp
[5]);
138 rfp
[6] = CLIP(bp
[6] + rfp
[6]);
139 rfp
[7] = CLIP(bp
[7] + rfp
[7]);
146 for(i
= 0; i
< 8; i
++)
148 rfp
[0] = CLIP(bp
[0] + 128);
149 rfp
[1] = CLIP(bp
[1] + 128);
150 rfp
[2] = CLIP(bp
[2] + 128);
151 rfp
[3] = CLIP(bp
[3] + 128);
152 rfp
[4] = CLIP(bp
[4] + 128);
153 rfp
[5] = CLIP(bp
[5] + 128);
154 rfp
[6] = CLIP(bp
[6] + 128);
155 rfp
[7] = CLIP(bp
[7] + 128);
163 int mpeg3_decode_slice(mpeg3_slice_t
*slice
)
165 mpeg3video_t
*video
= slice
->video
;
167 int mb_type
, cbp
, motion_type
= 0, dct_type
;
168 int macroblock_address
, mba_inc
, mba_max
;
169 int slice_vert_pos_ext
;
173 int mv_count
, mv_format
, mvscale
;
174 int pmv
[2][2][2], mv_field_sel
[2][2];
175 int dmv
, dmvector
[2];
177 int stwtype
, stwclass
;
180 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
182 /* number of macroblocks per picture */
183 mba_max
= video
->mb_width
* video
->mb_height
;
185 /* field picture has half as many macroblocks as frame */
186 if(video
->pict_struct
!= FRAME_PICTURE
)
189 /* macroblock address */
190 macroblock_address
= 0;
191 /* first macroblock in slice is not skipped */
195 code
= mpeg3slice_getbits(slice_buffer
, 32);
196 /* decode slice header (may change quant_scale) */
197 slice_vert_pos_ext
= mpeg3video_getslicehdr(slice
, video
);
199 /* reset all DC coefficient and motion vector predictors */
200 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
201 pmv
[0][0][0] = pmv
[0][0][1] = pmv
[1][0][0] = pmv
[1][0][1] = 0;
202 pmv
[0][1][0] = pmv
[0][1][1] = pmv
[1][1][0] = pmv
[1][1][1] = 0;
205 slice_buffer
->current_position
< slice_buffer
->buffer_size
;
211 if(!mpeg3slice_showbits(slice_buffer
, 23)) return 0;
212 /* decode macroblock address increment */
213 mba_inc
= mpeg3video_get_macroblock_address(slice
);
215 if(slice
->fault
) return 1;
219 /* Get the macroblock_address */
220 macroblock_address
= ((slice_vert_pos_ext
<< 7) + (code
& 255) - 1) * video
->mb_width
+ mba_inc
- 1;
221 /* first macroblock in slice: not skipped */
226 if(slice
->fault
) return 1;
228 if(macroblock_address
>= mba_max
)
230 /* mba_inc points beyond picture dimensions */
231 /*fprintf(stderr, "mpeg3_decode_slice: too many macroblocks in picture\n"); */
238 mpeg3video_macroblock_modes(slice
,
250 if(slice
->fault
) return 1;
252 if(mb_type
& MB_QUANT
)
254 qs
= mpeg3slice_getbits(slice_buffer
, 5);
257 slice
->quant_scale
= video
->qscale_type
? mpeg3_non_linear_mquant_table
[qs
] : (qs
<< 1);
259 slice
->quant_scale
= qs
;
261 if(video
->scalable_mode
== SC_DP
)
262 /* make sure quant_scale is valid */
263 slice
->quant_scale
= slice
->quant_scale
;
269 /* decode forward motion vectors */
270 if((mb_type
& MB_FORWARD
) || ((mb_type
& MB_INTRA
) && video
->conceal_mv
))
273 mpeg3video_motion_vectors(slice
,
281 video
->h_forw_r_size
,
282 video
->v_forw_r_size
,
286 mpeg3video_motion_vector(slice
,
296 if(slice
->fault
) return 1;
298 /* decode backward motion vectors */
299 if(mb_type
& MB_BACKWARD
)
302 mpeg3video_motion_vectors(slice
,
310 video
->h_back_r_size
,
311 video
->v_back_r_size
,
315 mpeg3video_motion_vector(slice
,
326 if(slice
->fault
) return 1;
328 /* remove marker_bit */
329 if((mb_type
& MB_INTRA
) && video
->conceal_mv
)
330 mpeg3slice_flushbit(slice_buffer
);
332 /* macroblock_pattern */
333 if(mb_type
& MB_PATTERN
)
335 cbp
= mpeg3video_get_cbp(slice
);
336 if(video
->chroma_format
== CHROMA422
)
338 /* coded_block_pattern_1 */
339 cbp
= (cbp
<< 2) | mpeg3slice_getbits2(slice_buffer
);
342 if(video
->chroma_format
== CHROMA444
)
344 /* coded_block_pattern_2 */
345 cbp
= (cbp
<< 6) | mpeg3slice_getbits(slice_buffer
, 6);
349 cbp
= (mb_type
& MB_INTRA
) ? ((1 << video
->blk_cnt
) - 1) : 0;
351 if(slice
->fault
) return 1;
353 mpeg3video_clearblock(slice
, 0, video
->blk_cnt
);
354 for(comp
= 0; comp
< video
->blk_cnt
; comp
++)
356 if(cbp
& (1 << (video
->blk_cnt
- comp
- 1)))
358 if(mb_type
& MB_INTRA
)
361 mpeg3video_getmpg2intrablock(slice
, video
, comp
, dc_dct_pred
);
363 mpeg3video_getintrablock(slice
, video
, comp
, dc_dct_pred
);
368 mpeg3video_getmpg2interblock(slice
, video
, comp
);
370 mpeg3video_getinterblock(slice
, video
, comp
);
372 if(slice
->fault
) return 1;
376 /* reset intra_dc predictors */
377 if(!(mb_type
& MB_INTRA
))
378 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
380 /* reset motion vector predictors */
381 if((mb_type
& MB_INTRA
) && !video
->conceal_mv
)
383 /* intra mb without concealment motion vectors */
384 pmv
[0][0][0] = pmv
[0][0][1] = pmv
[1][0][0] = pmv
[1][0][1] = 0;
385 pmv
[0][1][0] = pmv
[0][1][1] = pmv
[1][1][0] = pmv
[1][1][1] = 0;
388 if((video
->pict_type
== P_TYPE
) && !(mb_type
& (MB_FORWARD
| MB_INTRA
)))
390 /* non-intra mb without forward mv in a P picture */
391 pmv
[0][0][0] = pmv
[0][0][1] = pmv
[1][0][0] = pmv
[1][0][1] = 0;
393 /* derive motion_type */
394 if(video
->pict_struct
== FRAME_PICTURE
)
395 motion_type
= MC_FRAME
;
398 motion_type
= MC_FIELD
;
399 /* predict from field of same parity */
400 mv_field_sel
[0][0] = (video
->pict_struct
== BOTTOM_FIELD
);
406 /* purely spatially predicted macroblock */
407 pmv
[0][0][0] = pmv
[0][0][1] = pmv
[1][0][0] = pmv
[1][0][1] = 0;
408 pmv
[0][1][0] = pmv
[0][1][1] = pmv
[1][1][0] = pmv
[1][1][1] = 0;
413 /* mba_inc!=1: skipped macroblock */
414 mpeg3video_clearblock(slice
, 0, video
->blk_cnt
);
416 /* reset intra_dc predictors */
417 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
419 /* reset motion vector predictors */
420 if(video
->pict_type
== P_TYPE
)
421 pmv
[0][0][0] = pmv
[0][0][1] = pmv
[1][0][0] = pmv
[1][0][1] = 0;
423 /* derive motion_type */
424 if(video
->pict_struct
== FRAME_PICTURE
)
425 motion_type
= MC_FRAME
;
428 motion_type
= MC_FIELD
;
429 /* predict from field of same parity */
430 mv_field_sel
[0][0] = mv_field_sel
[0][1] = (video
->pict_struct
== BOTTOM_FIELD
);
433 /* skipped I are spatial-only predicted, */
434 /* skipped P and B are temporal-only predicted */
435 stwtype
= (video
->pict_type
== I_TYPE
) ? 8 : 0;
438 mb_type
&= ~MB_INTRA
;
446 /* pixel coordinates of top left corner of current macroblock */
447 bx
= 16 * (macroblock_address
% video
->mb_width
);
448 by
= 16 * (macroblock_address
/ video
->mb_width
);
450 /* motion compensation */
451 if(!(mb_type
& MB_INTRA
))
452 mpeg3video_reconstruct(video
,
462 /* copy or add block data into picture */
463 for(comp
= 0; comp
< video
->blk_cnt
; comp
++)
465 if((cbp
| snr_cbp
) & (1 << (video
->blk_cnt
- 1 - comp
)))
467 mpeg3video_idct_conversion(slice
->block
[comp
]);
469 mpeg3video_addblock(slice
,
475 (mb_type
& MB_INTRA
) == 0);
479 /* advance to next macroblock */
480 macroblock_address
++;
487 void mpeg3_slice_loop(mpeg3_slice_t
*slice
)
489 mpeg3video_t
*video
= slice
->video
;
494 pthread_mutex_lock(&(slice
->input_lock
));
498 /* Get a buffer to decode */
500 pthread_mutex_lock(&(video
->slice_lock
));
501 if(slice
->buffer_step
> 0)
503 while(slice
->current_buffer
<= slice
->last_buffer
)
505 if(!video
->slice_buffers
[slice
->current_buffer
].done
&&
506 slice
->current_buffer
<= slice
->last_buffer
)
511 slice
->current_buffer
+= slice
->buffer_step
;
516 while(slice
->current_buffer
>= slice
->last_buffer
)
518 if(!video
->slice_buffers
[slice
->current_buffer
].done
&&
519 slice
->current_buffer
>= slice
->last_buffer
)
524 slice
->current_buffer
+= slice
->buffer_step
;
529 if(!result
&& slice
->current_buffer
>= 0 && slice
->current_buffer
< video
->total_slice_buffers
)
531 slice
->slice_buffer
= &(video
->slice_buffers
[slice
->current_buffer
]);
532 slice
->slice_buffer
->done
= 1;
533 pthread_mutex_unlock(&(video
->slice_lock
));
534 pthread_mutex_unlock(&(slice
->input_lock
));
535 mpeg3_decode_slice(slice
);
536 pthread_mutex_unlock(&(slice
->slice_buffer
->completion_lock
));
539 /* Finished with all */
541 pthread_mutex_unlock(&(slice
->completion_lock
));
542 pthread_mutex_unlock(&(video
->slice_lock
));
546 pthread_mutex_unlock(&(slice
->output_lock
));
550 int mpeg3_new_slice_decoder(void *video
, mpeg3_slice_t
*slice
)
553 pthread_mutexattr_t mutex_attr
;
555 slice
->video
= video
;
557 pthread_mutexattr_init(&mutex_attr
);
558 // pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
559 pthread_mutex_init(&(slice
->input_lock
), &mutex_attr
);
560 pthread_mutex_lock(&(slice
->input_lock
));
561 pthread_mutex_init(&(slice
->output_lock
), &mutex_attr
);
562 pthread_mutex_lock(&(slice
->output_lock
));
563 pthread_mutex_init(&(slice
->completion_lock
), &mutex_attr
);
564 pthread_mutex_lock(&(slice
->completion_lock
));
566 pthread_attr_init(&attr
);
567 pthread_create(&(slice
->tid
), &attr
, (void*)mpeg3_slice_loop
, slice
);
572 int mpeg3_delete_slice_decoder(mpeg3_slice_t
*slice
)
575 pthread_mutex_unlock(&(slice
->input_lock
));
576 pthread_join(slice
->tid
, 0);
577 pthread_mutex_destroy(&(slice
->input_lock
));
578 pthread_mutex_destroy(&(slice
->output_lock
));