version update
[goodguy/cinelerra.git] / cinelerra-5.1 / mpeg2enc / putpic.c
1 /* putpic.c, block and motion vector encoding routines                      */
2
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
5 /*
6  * Disclaimer of Warranty
7  *
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.
15  *
16  * This disclaimer of warranty extends to the user of these programs and user's
17  * customers, employees, agents, transferees, successors, and assigns.
18  *
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
21  * patents.
22  *
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
26  * design.
27  *
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "config.h"
33 #include "global.h"
34
35 /* output motion vectors (6.2.5.2, 6.3.16.2)
36  *
37  * this routine also updates the predictions for motion vectors (PMV)
38  */
39  
40 static void putmvs(slice_engine_t *engine,
41         pict_data_s *picture,
42         mbinfo_s *mb,
43         int PMV[2][2][2],
44         int back)
45 {
46         int hor_f_code;
47         int vert_f_code;
48
49         if( back )
50         {
51                 hor_f_code = picture->back_hor_f_code;
52                 vert_f_code = picture->back_vert_f_code;
53         }
54         else
55         {
56                 hor_f_code = picture->forw_hor_f_code;
57                 vert_f_code = picture->forw_vert_f_code;
58         }
59
60
61         if(picture->pict_struct == FRAME_PICTURE)
62         {
63         if(mb->motion_type == MC_FRAME)
64         {
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];
70         }
71         else 
72                 if (mb->motion_type == MC_FIELD)
73         {
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];
85         }
86         else
87         {
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];
95         }
96         }
97         else
98         {
99         /* field picture */
100         if(mb->motion_type == MC_FIELD)
101         {
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];
108         }
109         else 
110                 if(mb->motion_type == MC_16X8)
111         {
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];
123         }
124         else
125         {
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];
133         }
134         }
135 }
136
137
138 void* slice_engine_loop(slice_engine_t *engine)
139 {
140         while(!engine->done)
141         {
142                 pthread_mutex_lock(&(engine->input_lock));
143
144                 if(!engine->done)
145                 {
146                         pict_data_s *picture = engine->picture;
147                         int i, j, k, comp, cc;
148                         int mb_type;
149                         int PMV[2][2][2];
150                         int cbp, MBAinc = 0;
151                         short (*quant_blocks)[64] = picture->qblocks;
152
153                         k = engine->start_row * mb_width;
154                         for(j = engine->start_row; j < engine->end_row; j++)
155                         {
156 /* macroblock row loop */
157 //printf("putpic 1\n");
158 //slice_testbits(engine);
159                         for(i = 0; i < mb_width; i++)
160                         {
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 */
165                                 if(i == 0)
166                                 {
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 */
171                                         else
172                                         {
173                                                 slice_putbits(engine, SLICE_MIN_START + (j & 127), 32); /* slice_start_code */
174                                                 slice_putbits(engine, j >> 7, 3); /* slice_vertical_position_extension */
175                                         }
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, 
180                                                         5);
181
182                                         slice_putbits(engine, 0, 1); /* extra_bit_slice */
183
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 */
187
188                                         for(cc = 0; cc < 3; cc++)
189                                                 engine->dc_dct_pred[cc] = 0;
190
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;
193
194                                         MBAinc = i + 1; /* first MBAinc denotes absolute position */
195                                 }
196
197                                 mb_type = cur_mb->mb_type;
198
199 /* determine mquant (rate control) */
200                                 cur_mb->mquant = ratectl_calc_mquant(engine->ratectl, picture, k);
201
202 /* quantize macroblock */
203 //printf("putpic 1\n");
204 //printf("putpic 1\n");
205 //slice_testbits(engine);
206                                 if(mb_type & MB_INTRA)
207                                 {
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],
212                                                                          cur_mb->mquant, 
213                                                                          &cur_mb->mquant );
214
215 //printf("putpic 3\n");
216                                                 cur_mb->cbp = cbp = (1<<block_count) - 1;
217                                 }
218                                 else
219                                 {
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],
224                                                                                           cur_mb->mquant,
225                                                                                           &cur_mb->mquant);
226 //printf("putpic 5\n");
227                                                 cur_mb->cbp = cbp;
228                                                 if (cbp)
229                                                         mb_type|= MB_PATTERN;
230                                 }
231 //printf("putpic 6\n");
232 //printf("putpic 2\n");
233 //slice_testbits(engine);
234
235 /* output mquant if it has changed */
236                                 if(cbp && engine->prev_mquant != cur_mb->mquant)
237                                         mb_type |= MB_QUANT;
238
239 /* check if macroblock can be skipped */
240                                 if(i != 0 && i != mb_width - 1 && !cbp)
241                                 {
242 /* no DCT coefficients and neither first nor last macroblock of slice */
243
244                                         if(picture->pict_type == P_TYPE && 
245                                                         !(mb_type & MB_FORWARD))
246                                         {
247                                                 /* P picture, no motion vectors -> skip */
248
249                                                 /* reset predictors */
250
251                                                 for(cc = 0; cc < 3; cc++)
252                                                  engine->dc_dct_pred[cc] = 0;
253
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;
256
257                                                 cur_mb->mb_type = mb_type;
258                                                 cur_mb->skipped = 1;
259                                                 MBAinc++;
260                                                 k++;
261                                                 continue;
262                                         }
263
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])))
274                                         {
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
281  */
282
283                                                 cur_mb->mb_type = mb_type;
284                                                 cur_mb->skipped = 1;
285                                                 MBAinc++;
286                                                 k++;
287                                                 continue;
288                                         }
289
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))))
302                                         {
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
311  */
312
313                                                 cur_mb->mb_type = mb_type;
314                                                 cur_mb->skipped = 1;
315                                                 MBAinc++;
316                                                 k++;
317                                                 continue;
318                                         }
319                                 }
320 //printf("putpic 3\n");
321 //slice_testbits(engine);
322
323 /* macroblock cannot be skipped */
324                                 cur_mb->skipped = 0;
325
326 /* there's no VLC for 'No MC, Not Coded':
327  * we have to transmit (0,0) motion vectors
328  */
329                                 if(picture->pict_type == P_TYPE && 
330                                                 !cbp && 
331                                                 !(mb_type & MB_FORWARD))
332                                         mb_type |= MB_FORWARD;
333
334 /* macroblock_address_increment */
335                                 putaddrinc(engine, MBAinc); 
336                                 MBAinc = 1;
337                                 putmbtype(engine, picture->pict_type, mb_type); /* macroblock type */
338 //printf("putpic 3\n");
339 //slice_testbits(engine);
340
341                                 if(mb_type & (MB_FORWARD | MB_BACKWARD) && 
342                                                 !picture->frame_pred_dct)
343                                         slice_putbits(engine, cur_mb->motion_type, 2);
344
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 && 
347                                                 cbp && 
348                                                 !picture->frame_pred_dct)
349                                         slice_putbits(engine, cur_mb->dct_type, 1);
350
351                                 if(mb_type & MB_QUANT)
352                                 {
353                                         slice_putbits(engine, 
354                                                         picture->q_scale_type ? map_non_linear_mquant_hv[cur_mb->mquant] : cur_mb->mquant >> 1,
355                                                         5);
356                                         engine->prev_mquant = cur_mb->mquant;
357                                 }
358
359                                 if(mb_type & MB_FORWARD)
360                                 {
361 /* forward motion vectors, update predictors */
362                                         putmvs(engine, picture, cur_mb, PMV, 0);
363                                 }
364
365                                 if(mb_type & MB_BACKWARD)
366                                 {
367 /* backward motion vectors, update predictors */
368                                         putmvs(engine, picture,  cur_mb, PMV, 1);
369                                 }
370
371                                 if(mb_type & MB_PATTERN)
372                                 {
373                                         putcbp(engine, (cbp >> (block_count - 6)) & 63);
374                                         if(chroma_format != CHROMA420)
375                                                 slice_putbits(engine, cbp, block_count - 6);
376                                 }
377
378 //printf("putpic 4\n");
379 //slice_testbits(engine);
380
381                                 for(comp = 0; comp < block_count; comp++)
382                                 {
383 /* block loop */
384                                         if(cbp & (1 << (block_count - 1 - comp)))
385                                         {
386                                           if(mb_type & MB_INTRA)
387                                           {
388                                           cc = (comp < 4) ? 0 : (comp & 1) + 1;
389                                           putintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp], cc);
390
391                                           }
392                                           else
393                                           putnonintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp]);
394                                         }
395                                 }
396 //printf("putpic 5\n");
397 //slice_testbits(engine);
398 //sleep(1);
399
400                                 /* reset predictors */
401                                 if(!(mb_type & MB_INTRA))
402                                         for(cc = 0; cc < 3; cc++)
403                                                 engine->dc_dct_pred[cc] = 0;
404
405                                 if(mb_type & MB_INTRA || 
406                                                 (picture->pict_type == P_TYPE && !(mb_type & MB_FORWARD)))
407                                 {
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;
410                                 }
411
412                                 cur_mb->mb_type = mb_type;
413                                 k++;
414 //pthread_mutex_unlock(&test_lock);
415                         }
416                         }
417                 }
418                 pthread_mutex_unlock(&(engine->output_lock));
419         }
420         return 0;
421 }
422
423
424 /* quantization / variable length encoding of a complete picture */
425 void putpict(pict_data_s *picture)
426 {
427         int i;
428
429         for(i = 0; i < processors; i++)
430         {
431                 ratectl_init_pict(ratectl[i], picture); /* set up rate control */
432         }
433
434 /* picture header and picture coding extension */
435         putpicthdr(picture);
436         if(!mpeg1) putpictcodext(picture);
437 /* Flush buffer before switching to slice mode */
438         alignbits();
439
440 /* Start loop */
441         for(i = 0; i < processors; i++)
442         {
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]);
447
448                 pthread_mutex_unlock(&(slice_engines[i].input_lock));
449         }
450
451 /* Wait for completion and write slices */
452         for(i = 0; i < processors; i++)
453         {
454                 pthread_mutex_lock(&(slice_engines[i].output_lock));
455                 slice_finishslice(&slice_engines[i]);
456         }
457
458         for(i = 0; i < processors; i++)
459                 ratectl_update_pict(ratectl[i], picture);
460 }
461
462 void start_slice_engines()
463 {
464         int i;
465         int rows_per_processor = (int)((float)mb_height2 / processors + 0.5);
466         int current_row = 0;
467         pthread_attr_t attr;
468         pthread_mutexattr_t mutex_attr;
469
470         pthread_mutexattr_init(&mutex_attr);
471         pthread_attr_init(&attr);
472 //      pthread_mutex_init(&test_lock, &mutex_attr);
473
474         slice_engines = calloc(1, sizeof(slice_engine_t) * processors);
475         for(i = 0; i < processors; i++)
476         {
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;
481                 
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), 
488                         &attr, 
489                         (void*)slice_engine_loop, 
490                         &slice_engines[i]);
491         }
492 }
493
494 void stop_slice_engines()
495 {
496         int i;
497         for(i = 0; i < processors; i++)
498         {
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);
505         }
506         free(slice_engines);
507 //      pthread_mutex_destroy(&test_lock);
508 }