1 /* putpic.c, block and motion vector encoding routines */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
35 /* output motion vectors (6.2.5.2, 6.3.16.2)
37 * this routine also updates the predictions for motion vectors (PMV)
40 static void putmvs(slice_engine_t *engine,
51 hor_f_code = picture->back_hor_f_code;
52 vert_f_code = picture->back_vert_f_code;
56 hor_f_code = picture->forw_hor_f_code;
57 vert_f_code = picture->forw_vert_f_code;
61 if(picture->pict_struct == FRAME_PICTURE)
63 if(mb->motion_type == MC_FRAME)
65 /* frame prediction */
66 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
67 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
68 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
69 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
72 if (mb->motion_type == MC_FIELD)
74 /* field prediction */
75 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
76 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
77 putmv(engine, (mb->MV[0][back][1] >> 1) - (PMV[0][back][1] >> 1), vert_f_code);
78 slice_putbits(engine, mb->mv_field_sel[1][back], 1);
79 putmv(engine, mb->MV[1][back][0] - PMV[1][back][0], hor_f_code);
80 putmv(engine, (mb->MV[1][back][1] >> 1) - (PMV[1][back][1] >> 1), vert_f_code);
81 PMV[0][back][0] = mb->MV[0][back][0];
82 PMV[0][back][1] = mb->MV[0][back][1];
83 PMV[1][back][0] = mb->MV[1][back][0];
84 PMV[1][back][1] = mb->MV[1][back][1];
88 /* dual prime prediction */
89 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
90 putdmv(engine, mb->dmvector[0]);
91 putmv(engine, (mb->MV[0][back][1] >> 1) - (PMV[0][back][1] >> 1), vert_f_code);
92 putdmv(engine, mb->dmvector[1]);
93 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
94 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
100 if(mb->motion_type == MC_FIELD)
102 /* field prediction */
103 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
104 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
105 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
106 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
107 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
110 if(mb->motion_type == MC_16X8)
112 /* 16x8 prediction */
113 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
114 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
115 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
116 slice_putbits(engine, mb->mv_field_sel[1][back], 1);
117 putmv(engine, mb->MV[1][back][0] - PMV[1][back][0], hor_f_code);
118 putmv(engine, mb->MV[1][back][1] - PMV[1][back][1], vert_f_code);
119 PMV[0][back][0] = mb->MV[0][back][0];
120 PMV[0][back][1] = mb->MV[0][back][1];
121 PMV[1][back][0] = mb->MV[1][back][0];
122 PMV[1][back][1] = mb->MV[1][back][1];
126 /* dual prime prediction */
127 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
128 putdmv(engine, mb->dmvector[0]);
129 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
130 putdmv(engine, mb->dmvector[1]);
131 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
132 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
138 void* slice_engine_loop(slice_engine_t *engine)
142 pthread_mutex_lock(&(engine->input_lock));
146 pict_data_s *picture = engine->picture;
147 int i, j, k, comp, cc;
151 short (*quant_blocks)[64] = picture->qblocks;
153 k = engine->start_row * mb_width;
154 for(j = engine->start_row; j < engine->end_row; j++)
156 /* macroblock row loop */
157 //printf("putpic 1\n");
158 //slice_testbits(engine);
159 for(i = 0; i < mb_width; i++)
161 mbinfo_s *cur_mb = &picture->mbinfo[k];
162 int cur_mb_blocks = k * block_count;
163 //pthread_mutex_lock(&test_lock);
164 /* macroblock loop */
167 slice_alignbits(engine);
168 /* slice header (6.2.4) */
169 if(mpeg1 || vertical_size <= 2800)
170 slice_putbits(engine, SLICE_MIN_START + j, 32); /* slice_start_code */
173 slice_putbits(engine, SLICE_MIN_START + (j & 127), 32); /* slice_start_code */
174 slice_putbits(engine, j >> 7, 3); /* slice_vertical_position_extension */
176 /* quantiser_scale_code */
177 //printf("putpic 1\n");slice_testbits(engine);
178 slice_putbits(engine,
179 picture->q_scale_type ? map_non_linear_mquant_hv[engine->prev_mquant] : engine->prev_mquant >> 1,
182 slice_putbits(engine, 0, 1); /* extra_bit_slice */
184 //printf("putpic 1 %d %d %d\n",engine->prev_mquant, map_non_linear_mquant_hv[engine->prev_mquant], engine->prev_mquant >> 1);
185 //slice_testbits(engine);
186 /* reset predictors */
188 for(cc = 0; cc < 3; cc++)
189 engine->dc_dct_pred[cc] = 0;
191 PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
192 PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
194 MBAinc = i + 1; /* first MBAinc denotes absolute position */
197 mb_type = cur_mb->mb_type;
199 /* determine mquant (rate control) */
200 cur_mb->mquant = ratectl_calc_mquant(engine->ratectl, picture, k);
202 /* quantize macroblock */
203 //printf("putpic 1\n");
204 //printf("putpic 1\n");
205 //slice_testbits(engine);
206 if(mb_type & MB_INTRA)
208 //printf("putpic 2 %d\n", cur_mb->mquant);
209 quant_intra_hv( picture,
210 picture->blocks[cur_mb_blocks],
211 quant_blocks[cur_mb_blocks],
215 //printf("putpic 3\n");
216 cur_mb->cbp = cbp = (1<<block_count) - 1;
220 //printf("putpic 4 %p %d\n", picture->blocks[cur_mb_blocks], cur_mb_blocks);
221 cbp = (*pquant_non_intra)(picture,
222 picture->blocks[cur_mb_blocks],
223 quant_blocks[cur_mb_blocks],
226 //printf("putpic 5\n");
229 mb_type|= MB_PATTERN;
231 //printf("putpic 6\n");
232 //printf("putpic 2\n");
233 //slice_testbits(engine);
235 /* output mquant if it has changed */
236 if(cbp && engine->prev_mquant != cur_mb->mquant)
239 /* check if macroblock can be skipped */
240 if(i != 0 && i != mb_width - 1 && !cbp)
242 /* no DCT coefficients and neither first nor last macroblock of slice */
244 if(picture->pict_type == P_TYPE &&
245 !(mb_type & MB_FORWARD))
247 /* P picture, no motion vectors -> skip */
249 /* reset predictors */
251 for(cc = 0; cc < 3; cc++)
252 engine->dc_dct_pred[cc] = 0;
254 PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
255 PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
257 cur_mb->mb_type = mb_type;
264 if(picture->pict_type == B_TYPE &&
265 picture->pict_struct == FRAME_PICTURE &&
266 cur_mb->motion_type == MC_FRAME &&
267 ((picture->mbinfo[k - 1].mb_type ^ mb_type) & (MB_FORWARD | MB_BACKWARD)) == 0 &&
268 (!(mb_type & MB_FORWARD) ||
269 (PMV[0][0][0] == cur_mb->MV[0][0][0] &&
270 PMV[0][0][1] == cur_mb->MV[0][0][1])) &&
271 (!(mb_type & MB_BACKWARD) ||
272 (PMV[0][1][0] == cur_mb->MV[0][1][0] &&
273 PMV[0][1][1] == cur_mb->MV[0][1][1])))
275 /* conditions for skipping in B frame pictures:
276 * - must be frame predicted
277 * - must be the same prediction type (forward/backward/interp.)
278 * as previous macroblock
279 * - relevant vectors (forward/backward/both) have to be the same
280 * as in previous macroblock
283 cur_mb->mb_type = mb_type;
290 if (picture->pict_type == B_TYPE &&
291 picture->pict_struct != FRAME_PICTURE &&
292 cur_mb->motion_type == MC_FIELD &&
293 ((picture->mbinfo[k - 1].mb_type ^ mb_type) & (MB_FORWARD | MB_BACKWARD))==0 &&
294 (!(mb_type & MB_FORWARD) ||
295 (PMV[0][0][0] == cur_mb->MV[0][0][0] &&
296 PMV[0][0][1] == cur_mb->MV[0][0][1] &&
297 cur_mb->mv_field_sel[0][0] == (picture->pict_struct == BOTTOM_FIELD))) &&
298 (!(mb_type & MB_BACKWARD) ||
299 (PMV[0][1][0] == cur_mb->MV[0][1][0] &&
300 PMV[0][1][1] == cur_mb->MV[0][1][1] &&
301 cur_mb->mv_field_sel[0][1] == (picture->pict_struct == BOTTOM_FIELD))))
303 /* conditions for skipping in B field pictures:
304 * - must be field predicted
305 * - must be the same prediction type (forward/backward/interp.)
306 * as previous macroblock
307 * - relevant vectors (forward/backward/both) have to be the same
308 * as in previous macroblock
309 * - relevant motion_vertical_field_selects have to be of same
310 * parity as current field
313 cur_mb->mb_type = mb_type;
320 //printf("putpic 3\n");
321 //slice_testbits(engine);
323 /* macroblock cannot be skipped */
326 /* there's no VLC for 'No MC, Not Coded':
327 * we have to transmit (0,0) motion vectors
329 if(picture->pict_type == P_TYPE &&
331 !(mb_type & MB_FORWARD))
332 mb_type |= MB_FORWARD;
334 /* macroblock_address_increment */
335 putaddrinc(engine, MBAinc);
337 putmbtype(engine, picture->pict_type, mb_type); /* macroblock type */
338 //printf("putpic 3\n");
339 //slice_testbits(engine);
341 if(mb_type & (MB_FORWARD | MB_BACKWARD) &&
342 !picture->frame_pred_dct)
343 slice_putbits(engine, cur_mb->motion_type, 2);
345 //printf("putpic %x %d %d %d\n", mb_type, picture->pict_struct, cbp, picture->frame_pred_dct);
346 if(picture->pict_struct == FRAME_PICTURE &&
348 !picture->frame_pred_dct)
349 slice_putbits(engine, cur_mb->dct_type, 1);
351 if(mb_type & MB_QUANT)
353 slice_putbits(engine,
354 picture->q_scale_type ? map_non_linear_mquant_hv[cur_mb->mquant] : cur_mb->mquant >> 1,
356 engine->prev_mquant = cur_mb->mquant;
359 if(mb_type & MB_FORWARD)
361 /* forward motion vectors, update predictors */
362 putmvs(engine, picture, cur_mb, PMV, 0);
365 if(mb_type & MB_BACKWARD)
367 /* backward motion vectors, update predictors */
368 putmvs(engine, picture, cur_mb, PMV, 1);
371 if(mb_type & MB_PATTERN)
373 putcbp(engine, (cbp >> (block_count - 6)) & 63);
374 if(chroma_format != CHROMA420)
375 slice_putbits(engine, cbp, block_count - 6);
378 //printf("putpic 4\n");
379 //slice_testbits(engine);
381 for(comp = 0; comp < block_count; comp++)
384 if(cbp & (1 << (block_count - 1 - comp)))
386 if(mb_type & MB_INTRA)
388 cc = (comp < 4) ? 0 : (comp & 1) + 1;
389 putintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp], cc);
393 putnonintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp]);
396 //printf("putpic 5\n");
397 //slice_testbits(engine);
400 /* reset predictors */
401 if(!(mb_type & MB_INTRA))
402 for(cc = 0; cc < 3; cc++)
403 engine->dc_dct_pred[cc] = 0;
405 if(mb_type & MB_INTRA ||
406 (picture->pict_type == P_TYPE && !(mb_type & MB_FORWARD)))
408 PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
409 PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
412 cur_mb->mb_type = mb_type;
414 //pthread_mutex_unlock(&test_lock);
418 pthread_mutex_unlock(&(engine->output_lock));
424 /* quantization / variable length encoding of a complete picture */
425 void putpict(pict_data_s *picture)
429 for(i = 0; i < processors; i++)
431 ratectl_init_pict(ratectl[i], picture); /* set up rate control */
434 /* picture header and picture coding extension */
436 if(!mpeg1) putpictcodext(picture);
437 /* Flush buffer before switching to slice mode */
441 for(i = 0; i < processors; i++)
443 slice_engines[i].prev_mquant = ratectl_start_mb(ratectl[i], picture);
444 slice_engines[i].picture = picture;
445 slice_engines[i].ratectl = ratectl[i];
446 slice_initbits(&slice_engines[i]);
448 pthread_mutex_unlock(&(slice_engines[i].input_lock));
451 /* Wait for completion and write slices */
452 for(i = 0; i < processors; i++)
454 pthread_mutex_lock(&(slice_engines[i].output_lock));
455 slice_finishslice(&slice_engines[i]);
458 for(i = 0; i < processors; i++)
459 ratectl_update_pict(ratectl[i], picture);
462 void start_slice_engines()
465 int rows_per_processor = (int)((float)mb_height2 / processors + 0.5);
468 pthread_mutexattr_t mutex_attr;
470 pthread_mutexattr_init(&mutex_attr);
471 pthread_attr_init(&attr);
472 // pthread_mutex_init(&test_lock, &mutex_attr);
474 slice_engines = calloc(1, sizeof(slice_engine_t) * processors);
475 for(i = 0; i < processors; i++)
477 slice_engines[i].start_row = current_row;
478 current_row += rows_per_processor;
479 if(current_row > mb_height2) current_row = mb_height2;
480 slice_engines[i].end_row = current_row;
482 pthread_mutex_init(&(slice_engines[i].input_lock), &mutex_attr);
483 pthread_mutex_lock(&(slice_engines[i].input_lock));
484 pthread_mutex_init(&(slice_engines[i].output_lock), &mutex_attr);
485 pthread_mutex_lock(&(slice_engines[i].output_lock));
486 slice_engines[i].done = 0;
487 pthread_create(&(slice_engines[i].tid),
489 (void*)slice_engine_loop,
494 void stop_slice_engines()
497 for(i = 0; i < processors; i++)
499 slice_engines[i].done = 1;
500 pthread_mutex_unlock(&(slice_engines[i].input_lock));
501 pthread_join(slice_engines[i].tid, 0);
502 pthread_mutex_destroy(&(slice_engines[i].input_lock));
503 pthread_mutex_destroy(&(slice_engines[i].output_lock));
504 if(slice_engines[i].slice_buffer) free(slice_engines[i].slice_buffer);
507 // pthread_mutex_destroy(&test_lock);