r1019: Fix crash in Ogg file handling.
[cinelerra/simeon] / 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 "config.h"
32 #include "global.h"
33
34 /* output motion vectors (6.2.5.2, 6.3.16.2)
35  *
36  * this routine also updates the predictions for motion vectors (PMV)
37  */
38  
39 static void putmvs(slice_engine_t *engine,
40         pict_data_s *picture,
41         mbinfo_s *mb,
42         int PMV[2][2][2],
43         int back)
44 {
45         int hor_f_code;
46         int vert_f_code;
47
48         if( back )
49         {
50                 hor_f_code = picture->back_hor_f_code;
51                 vert_f_code = picture->back_vert_f_code;
52         }
53         else
54         {
55                 hor_f_code = picture->forw_hor_f_code;
56                 vert_f_code = picture->forw_vert_f_code;
57         }
58
59
60         if(picture->pict_struct == FRAME_PICTURE)
61         {
62         if(mb->motion_type == MC_FRAME)
63         {
64 /* frame prediction */
65                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
66                 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
67                 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
68                 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
69         }
70         else 
71                 if (mb->motion_type == MC_FIELD)
72         {
73                 /* field prediction */
74                 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
75                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
76                 putmv(engine, (mb->MV[0][back][1] >> 1) - (PMV[0][back][1] >> 1), vert_f_code);
77                 slice_putbits(engine, mb->mv_field_sel[1][back], 1);
78                 putmv(engine, mb->MV[1][back][0] - PMV[1][back][0], hor_f_code);
79                 putmv(engine, (mb->MV[1][back][1] >> 1) - (PMV[1][back][1] >> 1), vert_f_code);
80                 PMV[0][back][0] = mb->MV[0][back][0];
81                 PMV[0][back][1] = mb->MV[0][back][1];
82                 PMV[1][back][0] = mb->MV[1][back][0];
83                 PMV[1][back][1] = mb->MV[1][back][1];
84         }
85         else
86         {
87                 /* dual prime prediction */
88                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
89                 putdmv(engine, mb->dmvector[0]);
90                 putmv(engine, (mb->MV[0][back][1] >> 1) - (PMV[0][back][1] >> 1), vert_f_code);
91                 putdmv(engine, mb->dmvector[1]);
92                 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
93                 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
94         }
95         }
96         else
97         {
98         /* field picture */
99         if(mb->motion_type == MC_FIELD)
100         {
101                 /* field prediction */
102                 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
103                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
104                 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
105                 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
106                 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
107         }
108         else 
109                 if(mb->motion_type == MC_16X8)
110         {
111                 /* 16x8 prediction */
112                 slice_putbits(engine, mb->mv_field_sel[0][back], 1);
113                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
114                 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
115                 slice_putbits(engine, mb->mv_field_sel[1][back], 1);
116                 putmv(engine, mb->MV[1][back][0] - PMV[1][back][0], hor_f_code);
117                 putmv(engine, mb->MV[1][back][1] - PMV[1][back][1], vert_f_code);
118                 PMV[0][back][0] = mb->MV[0][back][0];
119                 PMV[0][back][1] = mb->MV[0][back][1];
120                 PMV[1][back][0] = mb->MV[1][back][0];
121                 PMV[1][back][1] = mb->MV[1][back][1];
122         }
123         else
124         {
125                 /* dual prime prediction */
126                 putmv(engine, mb->MV[0][back][0] - PMV[0][back][0], hor_f_code);
127                 putdmv(engine, mb->dmvector[0]);
128                 putmv(engine, mb->MV[0][back][1] - PMV[0][back][1], vert_f_code);
129                 putdmv(engine, mb->dmvector[1]);
130                 PMV[0][back][0] = PMV[1][back][0] = mb->MV[0][back][0];
131                 PMV[0][back][1] = PMV[1][back][1] = mb->MV[0][back][1];
132         }
133         }
134 }
135
136
137 void* slice_engine_loop(slice_engine_t *engine)
138 {
139         while(!engine->done)
140         {
141                 pthread_mutex_lock(&(engine->input_lock));
142
143                 if(!engine->done)
144                 {
145                         pict_data_s *picture = engine->picture;
146                         int i, j, k, comp, cc;
147                         int mb_type;
148                         int PMV[2][2][2];
149                         int cbp, MBAinc = 0;
150                         short (*quant_blocks)[64] = picture->qblocks;
151
152                         k = engine->start_row * mb_width;
153                         for(j = engine->start_row; j < engine->end_row; j++)
154                         {
155 /* macroblock row loop */
156 //printf("putpic 1\n");
157 //slice_testbits(engine);
158                         for(i = 0; i < mb_width; i++)
159                         {
160                                         mbinfo_s *cur_mb = &picture->mbinfo[k];
161                                         int cur_mb_blocks = k * block_count;
162 //pthread_mutex_lock(&test_lock);
163 /* macroblock loop */
164                                 if(i == 0)
165                                 {
166                                                 slice_alignbits(engine);
167 /* slice header (6.2.4) */
168                                         if(mpeg1 || vertical_size <= 2800)
169                                                 slice_putbits(engine, SLICE_MIN_START + j, 32); /* slice_start_code */
170                                         else
171                                         {
172                                                 slice_putbits(engine, SLICE_MIN_START + (j & 127), 32); /* slice_start_code */
173                                                 slice_putbits(engine, j >> 7, 3); /* slice_vertical_position_extension */
174                                         }
175 /* quantiser_scale_code */
176 //printf("putpic 1\n");slice_testbits(engine);
177                                         slice_putbits(engine, 
178                                                         picture->q_scale_type ? map_non_linear_mquant_hv[engine->prev_mquant] : engine->prev_mquant >> 1, 
179                                                         5);
180
181                                         slice_putbits(engine, 0, 1); /* extra_bit_slice */
182
183 //printf("putpic 1 %d %d %d\n",engine->prev_mquant, map_non_linear_mquant_hv[engine->prev_mquant], engine->prev_mquant >> 1);
184 //slice_testbits(engine);
185                                         /* reset predictors */
186
187                                         for(cc = 0; cc < 3; cc++)
188                                                 engine->dc_dct_pred[cc] = 0;
189
190                                         PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
191                                         PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
192
193                                         MBAinc = i + 1; /* first MBAinc denotes absolute position */
194                                 }
195
196                                 mb_type = cur_mb->mb_type;
197
198 /* determine mquant (rate control) */
199                                 cur_mb->mquant = ratectl_calc_mquant(engine->ratectl, picture, k);
200
201 /* quantize macroblock */
202 //printf("putpic 1\n");
203 //printf("putpic 1\n");
204 //slice_testbits(engine);
205                                 if(mb_type & MB_INTRA)
206                                 {
207 //printf("putpic 2 %d\n", cur_mb->mquant);
208                                                 quant_intra_hv( picture,
209                                                                  picture->blocks[cur_mb_blocks],
210                                                                          quant_blocks[cur_mb_blocks],
211                                                                          cur_mb->mquant, 
212                                                                          &cur_mb->mquant );
213
214 //printf("putpic 3\n");
215                                                 cur_mb->cbp = cbp = (1<<block_count) - 1;
216                                 }
217                                 else
218                                 {
219 //printf("putpic 4 %p %d\n", picture->blocks[cur_mb_blocks], cur_mb_blocks);
220                                                 cbp = (*pquant_non_intra)(picture,
221                                                                                           picture->blocks[cur_mb_blocks],
222                                                                                           quant_blocks[cur_mb_blocks],
223                                                                                           cur_mb->mquant,
224                                                                                           &cur_mb->mquant);
225 //printf("putpic 5\n");
226                                                 cur_mb->cbp = cbp;
227                                                 if (cbp)
228                                                         mb_type|= MB_PATTERN;
229                                 }
230 //printf("putpic 6\n");
231 //printf("putpic 2\n");
232 //slice_testbits(engine);
233
234 /* output mquant if it has changed */
235                                 if(cbp && engine->prev_mquant != cur_mb->mquant)
236                                         mb_type |= MB_QUANT;
237
238 /* check if macroblock can be skipped */
239                                 if(i != 0 && i != mb_width - 1 && !cbp)
240                                 {
241 /* no DCT coefficients and neither first nor last macroblock of slice */
242
243                                         if(picture->pict_type == P_TYPE && 
244                                                         !(mb_type & MB_FORWARD))
245                                         {
246                                                 /* P picture, no motion vectors -> skip */
247
248                                                 /* reset predictors */
249
250                                                 for(cc = 0; cc < 3; cc++)
251                                                  engine->dc_dct_pred[cc] = 0;
252
253                                                 PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
254                                                 PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
255
256                                                 cur_mb->mb_type = mb_type;
257                                                 cur_mb->skipped = 1;
258                                                 MBAinc++;
259                                                 k++;
260                                                 continue;
261                                         }
262
263                                         if(picture->pict_type == B_TYPE && 
264                                                         picture->pict_struct == FRAME_PICTURE &&
265                                         cur_mb->motion_type == MC_FRAME && 
266                                         ((picture->mbinfo[k - 1].mb_type ^ mb_type) & (MB_FORWARD | MB_BACKWARD)) == 0 &&
267                                         (!(mb_type & MB_FORWARD) ||
268                                                 (PMV[0][0][0] == cur_mb->MV[0][0][0] &&
269                                                  PMV[0][0][1] == cur_mb->MV[0][0][1])) && 
270                                         (!(mb_type & MB_BACKWARD) ||
271                                                 (PMV[0][1][0] == cur_mb->MV[0][1][0] &&
272                                                  PMV[0][1][1] == cur_mb->MV[0][1][1])))
273                                         {
274 /* conditions for skipping in B frame pictures:
275  * - must be frame predicted
276  * - must be the same prediction type (forward/backward/interp.)
277  *   as previous macroblock
278  * - relevant vectors (forward/backward/both) have to be the same
279  *   as in previous macroblock
280  */
281
282                                                 cur_mb->mb_type = mb_type;
283                                                 cur_mb->skipped = 1;
284                                                 MBAinc++;
285                                                 k++;
286                                                 continue;
287                                         }
288
289                                         if (picture->pict_type == B_TYPE && 
290                                                         picture->pict_struct != FRAME_PICTURE && 
291                                         cur_mb->motion_type == MC_FIELD && 
292                                         ((picture->mbinfo[k - 1].mb_type ^ mb_type) & (MB_FORWARD | MB_BACKWARD))==0 && 
293                                         (!(mb_type & MB_FORWARD) ||
294                                                 (PMV[0][0][0] == cur_mb->MV[0][0][0] &&
295                                                  PMV[0][0][1] == cur_mb->MV[0][0][1] &&
296                                                  cur_mb->mv_field_sel[0][0] == (picture->pict_struct == BOTTOM_FIELD))) && 
297                                         (!(mb_type & MB_BACKWARD) ||
298                                                 (PMV[0][1][0] == cur_mb->MV[0][1][0] &&
299                                                  PMV[0][1][1] == cur_mb->MV[0][1][1] &&
300                                                  cur_mb->mv_field_sel[0][1] == (picture->pict_struct == BOTTOM_FIELD))))
301                                         {
302 /* conditions for skipping in B field pictures:
303  * - must be field predicted
304  * - must be the same prediction type (forward/backward/interp.)
305  *   as previous macroblock
306  * - relevant vectors (forward/backward/both) have to be the same
307  *   as in previous macroblock
308  * - relevant motion_vertical_field_selects have to be of same
309  *   parity as current field
310  */
311
312                                                 cur_mb->mb_type = mb_type;
313                                                 cur_mb->skipped = 1;
314                                                 MBAinc++;
315                                                 k++;
316                                                 continue;
317                                         }
318                                 }
319 //printf("putpic 3\n");
320 //slice_testbits(engine);
321
322 /* macroblock cannot be skipped */
323                                 cur_mb->skipped = 0;
324
325 /* there's no VLC for 'No MC, Not Coded':
326  * we have to transmit (0,0) motion vectors
327  */
328                                 if(picture->pict_type == P_TYPE && 
329                                                 !cbp && 
330                                                 !(mb_type & MB_FORWARD))
331                                         mb_type |= MB_FORWARD;
332
333 /* macroblock_address_increment */
334                                 putaddrinc(engine, MBAinc); 
335                                 MBAinc = 1;
336                                 putmbtype(engine, picture->pict_type, mb_type); /* macroblock type */
337 //printf("putpic 3\n");
338 //slice_testbits(engine);
339
340                                 if(mb_type & (MB_FORWARD | MB_BACKWARD) && 
341                                                 !picture->frame_pred_dct)
342                                         slice_putbits(engine, cur_mb->motion_type, 2);
343
344 //printf("putpic %x %d %d %d\n", mb_type, picture->pict_struct, cbp, picture->frame_pred_dct);
345                                 if(picture->pict_struct == FRAME_PICTURE && 
346                                                 cbp && 
347                                                 !picture->frame_pred_dct)
348                                         slice_putbits(engine, cur_mb->dct_type, 1);
349
350                                 if(mb_type & MB_QUANT)
351                                 {
352                                         slice_putbits(engine, 
353                                                         picture->q_scale_type ? map_non_linear_mquant_hv[cur_mb->mquant] : cur_mb->mquant >> 1,
354                                                         5);
355                                         engine->prev_mquant = cur_mb->mquant;
356                                 }
357
358                                 if(mb_type & MB_FORWARD)
359                                 {
360 /* forward motion vectors, update predictors */
361                                         putmvs(engine, picture, cur_mb, PMV, 0);
362                                 }
363
364                                 if(mb_type & MB_BACKWARD)
365                                 {
366 /* backward motion vectors, update predictors */
367                                         putmvs(engine, picture,  cur_mb, PMV, 1);
368                                 }
369
370                                 if(mb_type & MB_PATTERN)
371                                 {
372                                         putcbp(engine, (cbp >> (block_count - 6)) & 63);
373                                         if(chroma_format != CHROMA420)
374                                                 slice_putbits(engine, cbp, block_count - 6);
375                                 }
376
377 //printf("putpic 4\n");
378 //slice_testbits(engine);
379
380                                 for(comp = 0; comp < block_count; comp++)
381                                 {
382 /* block loop */
383                                         if(cbp & (1 << (block_count - 1 - comp)))
384                                         {
385                                           if(mb_type & MB_INTRA)
386                                           {
387                                           cc = (comp < 4) ? 0 : (comp & 1) + 1;
388                                           putintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp], cc);
389
390                                           }
391                                           else
392                                           putnonintrablk(engine, picture, quant_blocks[cur_mb_blocks + comp]);
393                                         }
394                                 }
395 //printf("putpic 5\n");
396 //slice_testbits(engine);
397 //sleep(1);
398
399                                 /* reset predictors */
400                                 if(!(mb_type & MB_INTRA))
401                                         for(cc = 0; cc < 3; cc++)
402                                                 engine->dc_dct_pred[cc] = 0;
403
404                                 if(mb_type & MB_INTRA || 
405                                                 (picture->pict_type == P_TYPE && !(mb_type & MB_FORWARD)))
406                                 {
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;
409                                 }
410
411                                 cur_mb->mb_type = mb_type;
412                                 k++;
413 //pthread_mutex_unlock(&test_lock);
414                         }
415                         }
416                 }
417                 pthread_mutex_unlock(&(engine->output_lock));
418         }
419 }
420
421
422 /* quantization / variable length encoding of a complete picture */
423 void putpict(pict_data_s *picture)
424 {
425         int i, prev_mquant;
426
427         for(i = 0; i < processors; i++)
428         {
429                 ratectl_init_pict(ratectl[i], picture); /* set up rate control */
430         }
431
432 /* picture header and picture coding extension */
433         putpicthdr(picture);
434         if(!mpeg1) putpictcodext(picture);
435 /* Flush buffer before switching to slice mode */
436         alignbits();
437
438 /* Start loop */
439         for(i = 0; i < processors; i++)
440         {
441                 slice_engines[i].prev_mquant = ratectl_start_mb(ratectl[i], picture);
442                 slice_engines[i].picture = picture;
443                 slice_engines[i].ratectl = ratectl[i];
444                 slice_initbits(&slice_engines[i]);
445
446                 pthread_mutex_unlock(&(slice_engines[i].input_lock));
447         }
448
449 /* Wait for completion and write slices */
450         for(i = 0; i < processors; i++)
451         {
452                 pthread_mutex_lock(&(slice_engines[i].output_lock));
453                 slice_finishslice(&slice_engines[i]);
454         }
455
456         for(i = 0; i < processors; i++)
457                 ratectl_update_pict(ratectl[i], picture);
458 }
459
460 void start_slice_engines()
461 {
462         int i;
463         int rows_per_processor = (int)((float)mb_height2 / processors + 0.5);
464         int current_row = 0;
465         pthread_attr_t attr;
466         pthread_mutexattr_t mutex_attr;
467
468         pthread_mutexattr_init(&mutex_attr);
469         pthread_attr_init(&attr);
470 //      pthread_mutex_init(&test_lock, &mutex_attr);
471
472         slice_engines = calloc(1, sizeof(slice_engine_t) * processors);
473         for(i = 0; i < processors; i++)
474         {
475                 slice_engines[i].start_row = current_row;
476                 current_row += rows_per_processor;
477                 if(current_row > mb_height2) current_row = mb_height2;
478                 slice_engines[i].end_row = current_row;
479                 
480                 pthread_mutex_init(&(slice_engines[i].input_lock), &mutex_attr);
481                 pthread_mutex_lock(&(slice_engines[i].input_lock));
482                 pthread_mutex_init(&(slice_engines[i].output_lock), &mutex_attr);
483                 pthread_mutex_lock(&(slice_engines[i].output_lock));
484                 slice_engines[i].done = 0;
485                 pthread_create(&(slice_engines[i].tid), 
486                         &attr, 
487                         (void*)slice_engine_loop, 
488                         &slice_engines[i]);
489         }
490 }
491
492 void stop_slice_engines()
493 {
494         int i;
495         for(i = 0; i < processors; i++)
496         {
497                 slice_engines[i].done = 1;
498                 pthread_mutex_unlock(&(slice_engines[i].input_lock));
499                 pthread_join(slice_engines[i].tid, 0);
500                 pthread_mutex_destroy(&(slice_engines[i].input_lock));
501                 pthread_mutex_destroy(&(slice_engines[i].output_lock));
502                 if(slice_engines[i].slice_buffer) free(slice_engines[i].slice_buffer);
503         }
504         free(slice_engines);
505 //      pthread_mutex_destroy(&test_lock);
506 }