r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / libmpeg3 / video / slice.c
blob7c54c36dab536fa984240e0538851b0af3ca9de0
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
4 #include <pthread.h>
5 #include <stdlib.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);
24 return 0;
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));
31 return 0;
34 int mpeg3_expand_slice_buffer(mpeg3_slice_buffer_t *slice_buffer)
36 int i;
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;
43 return 0;
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,
51 mpeg3video_t *video,
52 int comp,
53 int bx,
54 int by,
55 int dct_type,
56 int addflag)
58 int cc, i, iincr;
59 unsigned char *rfp;
60 short *bp;
61 int spar = slice->sparse[comp];
62 /* color component index */
63 cc = (comp < 4) ? 0 : (comp & 1) + 1;
65 if(cc == 0)
67 /* luminance */
68 if(video->pict_struct == FRAME_PICTURE)
70 if(dct_type)
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);
77 else
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;
85 else
87 /* field picture */
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);
93 else
95 /* chrominance */
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);
109 else
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;
117 else
119 /* field picture */
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];
128 if(addflag)
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]);
140 rfp += iincr;
141 bp += 8;
144 else
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);
156 rfp+= iincr;
157 bp += 8;
160 return 0;
163 int mpeg3_decode_slice(mpeg3_slice_t *slice)
165 mpeg3video_t *video = slice->video;
166 int comp;
167 int mb_type, cbp, motion_type = 0, dct_type;
168 int macroblock_address, mba_inc, mba_max;
169 int slice_vert_pos_ext;
170 unsigned int code;
171 int bx, by;
172 int dc_dct_pred[3];
173 int mv_count, mv_format, mvscale;
174 int pmv[2][2][2], mv_field_sel[2][2];
175 int dmv, dmvector[2];
176 int qs;
177 int stwtype, stwclass;
178 int snr_cbp;
179 int i;
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)
187 mba_max >>= 1;
189 /* macroblock address */
190 macroblock_address = 0;
191 /* first macroblock in slice is not skipped */
192 mba_inc = 0;
193 slice->fault = 0;
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;
204 for(i = 0;
205 slice_buffer->current_position < slice_buffer->buffer_size;
206 i++)
208 if(mba_inc == 0)
210 /* Done */
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;
217 if(i == 0)
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 */
222 mba_inc = 1;
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"); */
232 return 1;
235 /* not skipped */
236 if(mba_inc == 1)
238 mpeg3video_macroblock_modes(slice,
239 video,
240 &mb_type,
241 &stwtype,
242 &stwclass,
243 &motion_type,
244 &mv_count,
245 &mv_format,
246 &dmv,
247 &mvscale,
248 &dct_type);
250 if(slice->fault) return 1;
252 if(mb_type & MB_QUANT)
254 qs = mpeg3slice_getbits(slice_buffer, 5);
256 if(video->mpeg2)
257 slice->quant_scale = video->qscale_type ? mpeg3_non_linear_mquant_table[qs] : (qs << 1);
258 else
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;
266 /* motion vectors */
269 /* decode forward motion vectors */
270 if((mb_type & MB_FORWARD) || ((mb_type & MB_INTRA) && video->conceal_mv))
272 if(video->mpeg2)
273 mpeg3video_motion_vectors(slice,
274 video,
275 pmv,
276 dmvector,
277 mv_field_sel,
279 mv_count,
280 mv_format,
281 video->h_forw_r_size,
282 video->v_forw_r_size,
283 dmv,
284 mvscale);
285 else
286 mpeg3video_motion_vector(slice,
287 video,
288 pmv[0][0],
289 dmvector,
290 video->forw_r_size,
291 video->forw_r_size,
294 video->full_forw);
296 if(slice->fault) return 1;
298 /* decode backward motion vectors */
299 if(mb_type & MB_BACKWARD)
301 if(video->mpeg2)
302 mpeg3video_motion_vectors(slice,
303 video,
304 pmv,
305 dmvector,
306 mv_field_sel,
308 mv_count,
309 mv_format,
310 video->h_back_r_size,
311 video->v_back_r_size,
313 mvscale);
314 else
315 mpeg3video_motion_vector(slice,
316 video,
317 pmv[0][1],
318 dmvector,
319 video->back_r_size,
320 video->back_r_size,
323 video->full_back);
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);
341 else
342 if(video->chroma_format == CHROMA444)
344 /* coded_block_pattern_2 */
345 cbp = (cbp << 6) | mpeg3slice_getbits(slice_buffer, 6);
348 else
349 cbp = (mb_type & MB_INTRA) ? ((1 << video->blk_cnt) - 1) : 0;
351 if(slice->fault) return 1;
352 /* decode blocks */
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)
360 if(video->mpeg2)
361 mpeg3video_getmpg2intrablock(slice, video, comp, dc_dct_pred);
362 else
363 mpeg3video_getintrablock(slice, video, comp, dc_dct_pred);
365 else
367 if(video->mpeg2)
368 mpeg3video_getmpg2interblock(slice, video, comp);
369 else
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;
396 else
398 motion_type = MC_FIELD;
399 /* predict from field of same parity */
400 mv_field_sel[0][0] = (video->pict_struct == BOTTOM_FIELD);
404 if(stwclass == 4)
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;
411 else
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;
426 else
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;
437 /* clear MB_INTRA */
438 mb_type &= ~MB_INTRA;
440 /* no block data */
441 cbp = 0;
444 snr_cbp = 0;
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,
453 bx,
454 by,
455 mb_type,
456 motion_type,
457 pmv,
458 mv_field_sel,
459 dmvector,
460 stwtype);
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,
470 video,
471 comp,
472 bx,
473 by,
474 dct_type,
475 (mb_type & MB_INTRA) == 0);
479 /* advance to next macroblock */
480 macroblock_address++;
481 mba_inc--;
484 return 0;
487 void mpeg3_slice_loop(mpeg3_slice_t *slice)
489 mpeg3video_t *video = slice->video;
490 int result = 1;
492 while(!slice->done)
494 pthread_mutex_lock(&(slice->input_lock));
496 if(!slice->done)
498 /* Get a buffer to decode */
499 result = 1;
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)
508 result = 0;
509 break;
511 slice->current_buffer += slice->buffer_step;
514 else
516 while(slice->current_buffer >= slice->last_buffer)
518 if(!video->slice_buffers[slice->current_buffer].done &&
519 slice->current_buffer >= slice->last_buffer)
521 result = 0;
522 break;
524 slice->current_buffer += slice->buffer_step;
528 /* Got one */
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));
538 else
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)
552 pthread_attr_t attr;
553 pthread_mutexattr_t mutex_attr;
555 slice->video = video;
556 slice->done = 0;
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);
569 return 0;
572 int mpeg3_delete_slice_decoder(mpeg3_slice_t *slice)
574 slice->done = 1;
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));
579 return 0;