r1026: Videoscope layout tweaks.
[cinelerra_cv/ct.git] / quicktime / encore50 / mom_util.c
blobb11ea713cd8a6b264b6b4174aae704b306db22c6
2 /**************************************************************************
3 * *
4 * This code is developed by Adam Li. This software is an *
5 * implementation of a part of one or more MPEG-4 Video tools as *
6 * specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 * software module in hardware or software products are advised that its *
8 * use may infringe existing patents or copyrights, and any such use *
9 * would be at such party's own risk. The original developer of this *
10 * software module and his/her company, and subsequent editors and their *
11 * companies (including Project Mayo), will have no liability for use of *
12 * this software or modifications or derivatives thereof. *
13 * *
14 * Project Mayo gives users of the Codec a license to this software *
15 * module or modifications thereof for use in hardware or software *
16 * products claiming conformance to the MPEG-4 Video Standard as *
17 * described in the Open DivX license. *
18 * *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
21 * *
22 **************************************************************************/
24 /**************************************************************************
26 * mom_util.c
28 * Copyright (C) 2001 Project Mayo
30 * Adam Li
32 * DivX Advance Research Center <darc@projectmayo.com>
34 **************************************************************************/
36 /* This file contains some utility functions to manipulate Image data */
37 /* structures. */
38 /* Some codes of this project come from MoMuSys MPEG-4 implementation. */
39 /* Please see seperate acknowledgement file for a list of contributors. */
41 #include "mom_util.h"
43 /* private prototypes */
44 Char *emalloc(Int n);
45 Char *ecalloc(Int n, Int s);
46 Char *erealloc(Char *p, Int n);
47 Void CopyImageI(ImageI *image_in, ImageI *image_out);
48 Void CopyImageF(ImageF *image_in, ImageF *image_out);
49 Void SetConstantImageI(ImageI *image, SInt val);
50 Void SetConstantImageF(ImageF *image, Float val);
51 Void SubImageI(ImageI *image_in1, ImageI *image_in2, ImageI *image_out);
52 Void SubImageF(ImageF *image_in1, ImageF *image_in2, ImageF *image_out);
55 /* The following is the Image maintance functions. */
57 /***********************************************************CommentBegin******
59 * -- AllocImage -- Allocates memory for an Image structure
61 * Purpose :
62 * Allocates memory for an Image structure, including memory
63 * for the actual image pixels. The image pixels can be of type
64 * UChar, SInt or Float depending on the parameter type.
66 * Return values :
67 * A pointer to the allocated Image structure.
69 * Description :
70 * Allocates memory for the Image if possible, otherwise a
71 * memory allocation error is signalled and the program will
72 * exit.
74 ***********************************************************CommentEnd********/
76 Image *
77 AllocImage(UInt size_x, UInt size_y, ImageType type)
79 Image *image;
81 image = (Image *) emalloc(sizeof(Image));
83 image->version = VERSION;
84 image->x = size_x;
85 image->y = size_y;
86 image->upperodd = 0;
87 image->grid = 's';
88 image->type = type;
89 image->data = (ImageData *) emalloc(sizeof(ImageData));
91 switch(type)
93 case SHORT_TYPE:
94 image->data->s = (SInt *) ecalloc(size_x*size_y,sizeof(SInt));
95 break;
97 case FLOAT_TYPE:
98 image->data->f = (Float *) ecalloc(size_x*size_y,sizeof(Float));
99 break;
101 case UCHAR_TYPE:
102 image->data->u = (UChar *) ecalloc(size_x*size_y,sizeof(UChar));
103 break;
106 image->f = image->data->s; /* For compatibility with KHOROS */
108 return(image);
112 /***********************************************************CommentBegin******
114 * -- FreeImage -- Frees up all the memory associated with an Image structure
116 * Purpose :
117 * Frees up all the memory associated with an Image structure
119 ***********************************************************CommentEnd********/
121 Void
122 FreeImage(Image *image)
124 SInt *ps;
125 Float *pf;
126 UChar *pu;
128 if (image == NULL) return;
130 switch(image->type)
132 case SHORT_TYPE:
133 ps = (SInt *)GetImageData(image);
134 if(ps != NULL) free((Char *)ps);
135 free((Char *) image->data);
136 free((Char *)image);
137 break;
139 case FLOAT_TYPE:
140 pf = (Float *)GetImageData(image);
141 if(pf != NULL) free((Char *)pf);
142 free((Char *) image->data);
143 free((Char *)image);
144 break;
146 case UCHAR_TYPE:
147 pu = (UChar *)GetImageData(image);
148 if(pu != NULL) free((Char *)pu);
149 free((Char *) image->data);
150 free((Char *)image);
151 break;
156 /***********************************************************CommentBegin******
158 * -- CopyImage -- Copies the pixel values of one image to another image
160 * Purpose :
161 * Copies the pixel values of one image to another image.
163 * Description :
164 * Currently, the function exits with an error if
165 * the source and destination images are not of the same allocated
166 * size and type.
168 * Also, the images of type UChar are not yet supported.
170 ***********************************************************CommentEnd********/
172 Void
173 CopyImage(Image *image_in, Image *image_out)
175 switch(image_out->type)
177 case SHORT_TYPE:
178 CopyImageI(image_in,image_out);
179 break;
181 case FLOAT_TYPE:
182 CopyImageF(image_in,image_out);
183 break;
189 /***********************************************************CommentBegin******
191 * -- CopyImageI --
193 ***********************************************************CommentEnd********/
195 Void
196 CopyImageI(ImageI *image_in, ImageI *image_out)
198 SInt *p_in = image_in->data->s,
199 *p_out = image_out->data->s,
200 *p_end;
202 UInt sx_in = image_in->x,
203 sx_out = image_out->x,
204 sy_in = image_in->y,
205 sy_out = image_out->y,
206 sxy_in = sx_in * sy_in;
208 p_end = p_in + sxy_in;
210 while (p_in != p_end)
212 *p_out = *p_in;
213 p_in++;
214 p_out++;
220 /***********************************************************CommentBegin******
222 * -- CopyImageF -- Copies image_in to image_out
224 ***********************************************************CommentEnd********/
226 Void
227 CopyImageF(ImageF *image_in, ImageF *image_out)
229 Float *p_in = image_in->data->f,
230 *p_out = image_out->data->f,
231 *p_end;
233 UInt sx_in = image_in->x,
234 sx_out = image_out->x,
235 sy_in = image_in->y,
236 sy_out = image_out->y,
237 sxy_in = sx_in * sy_in;
239 p_end = p_in + sxy_in;
241 while (p_in != p_end)
243 *p_out = *p_in;
244 p_in++;
245 p_out++;
251 /***********************************************************CommentBegin******
253 * -- SetConstantImage -- Sets each pixel in the Image to val
255 * Purpose :
256 * Sets each pixel in the Image to val.
258 * Side effects :
259 * Note val is a Float. If image is not a Float image then
260 * val is cast to the appropriate type i.e. SInt, unsigned
261 * Char.
263 * Description :
264 * Images of type UChar are not yet supported.
266 ***********************************************************CommentEnd********/
268 Void
269 SetConstantImage(Image *image, Float val)
271 switch(image->type)
273 case SHORT_TYPE:
274 SetConstantImageI(image,(SInt)val);
275 break;
277 case FLOAT_TYPE:
278 SetConstantImageF(image,val);
279 break;
285 /***********************************************************CommentBegin******
287 * -- SetConstantImageI --
289 ***********************************************************CommentEnd********/
291 Void
292 SetConstantImageI(ImageI *image, SInt val)
294 SInt *p = image->data->s,
295 *p_end;
297 UInt sxy = image->x * image->y;
299 if (val == 0)
300 memset (p, 0, sxy * 2);
301 else
303 p_end = p + sxy;
305 while (p != p_end)
307 *p = val;
308 p++;
314 /***********************************************************CommentBegin******
316 * -- SetConstantImageF --
318 ***********************************************************CommentEnd********/
320 Void
321 SetConstantImageF(ImageF *image, Float val)
323 Float *p = image->data->f,
324 *p_end;
326 UInt sxy = image->x * image->y;
328 p_end = p + sxy;
330 while (p != p_end)
332 *p = val;
333 p++;
339 /***********************************************************CommentBegin******
341 * -- SubImage -- Subtracts two images
343 * Purpose :
344 * Subtracts two images and stores the result in the third
345 * i.e. image_out = image_in1 - image_in2
347 * Description :
348 * Currently, the function exits with an error if
349 * the source and destination images are not of the same allocated
350 * size and type.
352 * Also, the images of type UChar are not yet supported.
354 ***********************************************************CommentEnd********/
356 Void
357 SubImage(Image *image_in1, Image *image_in2, Image *image_out)
359 switch(image_in1->type)
361 case SHORT_TYPE:
362 SubImageI(image_in1,image_in2,image_out);
363 break;
365 case FLOAT_TYPE:
366 SubImageF(image_in1,image_in2,image_out);
367 break;
371 /***********************************************************CommentBegin******
373 * -- SubImageI --
375 ***********************************************************CommentEnd********/
377 Void
378 SubImageI(ImageI *image_in1, ImageI *image_in2, ImageI *image_out)
380 SInt *p = image_out->data->s,
381 *p1 = image_in1->data->s,
382 *p2 = image_in2->data->s,
383 *p_end;
385 UInt sx_in1 = image_in1->x,
386 sx_in2 = image_in2->x,
387 sx_out = image_out->x,
388 sy_in1 = image_in1->y,
389 sy_in2 = image_in2->y,
390 sy_out = image_out->y,
391 sxy = sx_out * sy_out;
393 p_end = p + sxy;
395 while (p != p_end)
397 *p = *p1 - *p2;
398 p++;
399 p1++;
400 p2++;
405 /***********************************************************CommentBegin******
407 * -- SubImageF --
409 ***********************************************************CommentEnd********/
411 Void
412 SubImageF(ImageF *image_in1, ImageF *image_in2, ImageF *image_out)
414 Float *p = image_out->data->f,
415 *p1 = image_in1->data->f,
416 *p2 = image_in2->data->f,
417 *p_end;
419 UInt sx_in1 = image_in1->x,
420 sx_in2 = image_in2->x,
421 sx_out = image_out->x,
422 sy_in1 = image_in1->y,
423 sy_in2 = image_in2->y,
424 sy_out = image_out->y,
425 sxy = sx_out * sy_out;
427 p_end = p + sxy;
429 while (p != p_end)
431 *p = *p1 - *p2;
432 p++;
433 p1++;
434 p2++;
439 /* The following is Vop maintance functions. */
441 Vop *
442 SallocVop()
444 Vop *vop;
446 vop = (Vop *)ecalloc(1,sizeof(Vop));
448 return(vop);
452 Vop *
453 AllocVop(UInt x, UInt y)
455 Vop *vop;
457 Image *y_chan,
458 *u_chan,
459 *v_chan;
461 /* first allocate memory for the data structure */
462 vop = SallocVop();
464 vop->width = x;
465 vop->height = y;
467 /* Allocate image fields */
468 y_chan = AllocImage(x,y,SHORT_TYPE);
469 u_chan = AllocImage(x/2,y/2,SHORT_TYPE);
470 v_chan = AllocImage(x/2,y/2,SHORT_TYPE);
472 /* Include image fields in structure */
473 FreeImage(vop->y_chan);
474 vop->y_chan = y_chan;
476 FreeImage(vop->u_chan);
477 vop->u_chan = u_chan;
479 FreeImage(vop->v_chan);
480 vop->v_chan = v_chan;
482 return(vop);
486 Void
487 SfreeVop (Vop *vop)
489 free ((Char*)vop);
490 /* vop = NULL; */
491 return;
495 Void
496 FreeVop(Vop *vop)
498 Image *data=NULL; /*SpSc: added the initialization */
500 if(vop != NULL) {
502 /* Deallocate memory for image fields */
504 data = vop->y_chan;
505 FreeImage(data);
507 data = vop->u_chan;
508 FreeImage(data);
510 data = vop->v_chan;
511 FreeImage(data);
513 SfreeVop(vop);
516 return;
520 Void
521 CopyVopNonImageField(Vop *in,Vop *out)
523 /* out->quant_precision = in->quant_precision;
524 out->bits_per_pixel = in->bits_per_pixel;
526 // out->mod_time_base = in->mod_time_base;
527 // out->time_inc = in->time_inc;
528 out->prediction_type = in->prediction_type;
529 /* out->rounding_type = in->rounding_type;
530 out->width = in->width;
531 out->height = in->height;
532 out->hor_spat_ref = in->hor_spat_ref;
533 out->ver_spat_ref = in->ver_spat_ref;
534 out->quantizer = in->quantizer;
535 out->intra_quantizer = in->intra_quantizer;
536 out->time_increment_resolution = in->time_increment_resolution;
538 out->intra_acdc_pred_disable = in->intra_acdc_pred_disable;
539 out->fcode_for = in->fcode_for;
540 out->sr_for = in->sr_for;
542 out->intra_dc_vlc_thr = in->intra_dc_vlc_thr;
547 /* The following is Memory allocation functions. */
549 /***********************************************************CommentBegin******
551 * -- emalloc -- Memory allocation with error handling
553 * Purpose :
554 * Memory allocation with error handling.
556 * Description :
557 * WARNING: There is a problem when you want to allocate more memory
558 * than size can handle. Malloc gets as argument an unsigned. It poses
559 * a problem when sizeof(unsigned) < sizeof(Char *)...
561 ***********************************************************CommentEnd********/
563 Char *
564 emalloc(Int n)
566 Char *p;
568 p = (Char *) malloc((UInt)n);
569 return p;
572 /***********************************************************CommentBegin******
574 * -- ecalloc -- Memory allocation with error handling
576 * Purpose :
577 * Memory allocation with error handling.
579 * Description :
580 * WARNING: There is a problem when you want to allocate more memory
581 * than size can handle. Malloc gets as argument an unsigned. It poses
582 * a problem when sizeof(unsigned) < sizeof(Char *)...
584 ***********************************************************CommentEnd********/
586 Char *
587 ecalloc(Int n, Int s)
589 Char *p;
591 p = (Char *) calloc((UInt)n,(UInt)s);
593 return p;
596 /***********************************************************CommentBegin******
598 * -- erealloc -- Memory re-allocation with error handling.
600 * Purpose :
601 * Memory re-allocation with error handling
603 * Description :
604 * WARNING: There is a problem when you want to allocate more memory
605 * than size can handle. Malloc gets as argument an unsigned. It poses
606 * a problem when sizeof(unsigned) < sizeof(Char *)...
608 ***********************************************************CommentEnd********/
610 Char *
611 erealloc(Char *p, Int n)
613 p = (Char *) realloc(p,(UInt)n);
614 return p;