File: gap_vid_api_util.c

package info (click to toggle)
gimp-gap 2.6.0+dfsg-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 20,720 kB
  • ctags: 7,997
  • sloc: ansic: 119,817; sh: 3,890; makefile: 932; lisp: 97; pascal: 55
file content (601 lines) | stat: -rw-r--r-- 16,958 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* vid_api_util.c
 *
 * GAP Video read API implementation of utility procedures.
 *
 * 2004.03.14   hof created
 *
 */

#include <glib/gstdio.h>

/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX END fcache procedures */


/* CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC START copies gap_lib procedures */

/* --------------------
 * p_get_filesize
 * --------------------
 *   get the filesize of a file.
 * in: fname: The filename of the file to check.
 * returns: The filesize.
 */
static gint32
p_get_filesize(char *fname)
{
  struct stat  stat_buf;

  if (0 != g_stat(fname, &stat_buf))
  {
    printf ("stat error on '%s'\n", fname);
    return(0);
  }

  return((gint32)(stat_buf.st_size));
}  /* end p_get_filesize */


/* ----------------------------------
 * p_alloc_fname
 * ----------------------------------
 * build the framname by concatenating basename, nr and extension.
 * the Number part has leading zeroes, depending
 * on filenames with the same basename and extension on disc.
 *
 * if no similar discfiles were found 6 digits (with leading zeroes)
 * are used per default.
 *
 * if a similar discfilename is found, the number of digits/leading zeroes
 * is set equal to the discfile found.
 * example:
 *   basename == "frame_", nr == 5, ext == ".xcf"
 *   - discfile was found with name:  "frame_00001.xcf"
 *     return ("frame_00001.xcf");
 *
 *   - discfile was found with name:  "frame_001.xcf"
 *     return ("frame_001.xcf");
 *
 * return the resulting framename string
 *   (the calling program should g_free this string
 *    after use)
 */
static char*
p_alloc_fname(char *basename, long nr, char *extension)
{
  gchar *l_fname;
  gint   l_digits_used;
  gint   l_len;
  long   l_nr_chk;

  if(basename == NULL) return (NULL);
  l_len = (strlen(basename)  + strlen(extension) + 10);
  l_fname = (char *)g_malloc(l_len);

    l_digits_used = 6;
    if(nr < 10000000)
    {
       /* try to figure out if the frame numbers are in
        * 4-digit style, with leading zeroes  "frame_0001.xcf"
        * or not                              "frame_1.xcf"
        */
       l_nr_chk = nr;

       while(l_nr_chk >= 0)
       {
         /* check if frame is on disk with 6-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%06ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 6;
            break;
         }

         /* check if frame is on disk with 8-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%08ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 8;
            break;
         }

         /* check if frame is on disk with 7-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%07ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 7;
            break;
         }

         /* check if frame is on disk with 5-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%05ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 5;
            break;
         }

         /* check if frame is on disk with 4-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%04ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 4;
            break;
         }

         /* check if frame is on disk with 3-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%03ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 3;
            break;
         }

         /* check if frame is on disk with 2-digit style framenumber */
         g_snprintf(l_fname, l_len, "%s%02ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 2;
            break;
         }



         /* now check for filename without leading zeroes in the framenumber */
         g_snprintf(l_fname, l_len, "%s%ld%s", basename, l_nr_chk, extension);
         if (g_file_test(l_fname, G_FILE_TEST_EXISTS))
         {
            l_digits_used = 1;
            break;
         }
         l_nr_chk--;

         /* if the frames nr and nr-1  were not found
          * try to check frames 1 and 0
          * to limit down the loop to max 4 cycles
          */
         if((l_nr_chk == nr -2) && (l_nr_chk > 1))
         {
           l_nr_chk = 1;
         }
      }
    }
    else
    {
      /* numbers > 10000000 have 9 digits or more */
      l_digits_used = 0;
    }

  g_free(l_fname);

  switch(l_digits_used)
  {
    case 6:  l_fname = g_strdup_printf("%s%06ld%s", basename, nr, extension);
             break;
    case 8:  l_fname = g_strdup_printf("%s%08ld%s", basename, nr, extension);
             break;
    case 7:  l_fname = g_strdup_printf("%s%07ld%s", basename, nr, extension);
             break;
    case 5:  l_fname = g_strdup_printf("%s%05ld%s", basename, nr, extension);
             break;
    case 4:  l_fname = g_strdup_printf("%s%04ld%s", basename, nr, extension);
             break;
    case 3:  l_fname = g_strdup_printf("%s%03ld%s", basename, nr, extension);
             break;
    case 2:  l_fname = g_strdup_printf("%s%02ld%s", basename, nr, extension);
             break;
    default: l_fname = g_strdup_printf("%s%ld%s", basename, nr, extension);
             break;
  }
  return(l_fname);
}    /* end p_alloc_fname */


/* ----------------------------------------------------
 * DELETE image
 * ----------------------------------------------------
 * this procedure deletes a frame image
 * (only from the GIMP allocated Memory)
 *
 * further it makes a Workaround for a memory leak Problem in gimp-1.2.2
 */
static void
p_gimp_image_delete(gint32 image_id)
{
    gimp_image_undo_disable(image_id); /* clear undo stack */
    gimp_image_scale(image_id, 2, 2);
    if(gap_debug) printf("SCALED down to 2x2 id = %d (workaround for gimp_image-delete problem)\n", (int)image_id);

    gimp_image_undo_enable(image_id); /* clear undo stack */
    gimp_image_delete(image_id);
}

/* CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC END copies of gap_lib procedures */

/* WARNING!
   This is a deprecated GIMP API from #include <libgimpmath/gimpmd5.h>
   Someone should update the code to use GChecksum from glib
 */
void gimp_md5_get_digest (const gchar *buffer,
                          gint         buffer_size,
                          guchar       digest[16]);


/* --------------------------------
 * GVA_md5_string
 * --------------------------------
 * name must point to a buffer of at least 32 Bytes
 * tht will be filled with the MD5 hashstring.
 */
void
GVA_md5_string(char *name, const char *uri)
{
  if(uri)
  {
    guchar       digest[16];
    guchar       n;
    gint         i;
    
    gimp_md5_get_digest (uri, -1, digest);

    for (i = 0; i < 16; i++)
    {
        n = (digest[i] >> 4) & 0xF;
        name[i * 2]     = (n > 9) ? 'a' + n - 10 : '0' + n;

        n = digest[i] & 0xF;
        name[i * 2 + 1] = (n > 9) ? 'a' + n - 10 : '0' + n;
    }

    name[32] = '\0';
  }  
}  /* end GVA_md5_string */


/* --------------------------------
 * GVA_filename_to_uri
 * --------------------------------
 */
gchar*
GVA_filename_to_uri(const char *filename)
{
  gchar *uri;
  gchar *absolute;

  if (! g_path_is_absolute (filename))
  {
      gchar *current;

      current = g_get_current_dir ();
      absolute = g_build_filename (current, filename, NULL);
      g_free (current);
  }
  else
  {
      absolute = g_strdup (filename);
  }

  uri = g_filename_to_uri (absolute, NULL, NULL);

  g_free (absolute);

  return uri;
}  /* end GVA_filename_to_uri */


/* --------------------------------
 * GVA_util_check_mpg_frame_type
 * --------------------------------
 * this procedure is typically used after the caller
 * has fetched uncompressed MPEG frames (using GVA_get_video_chunk)
 * to find out the type of the fetched frame.
 * in:     buffer that should contain (at least) one compressed MPEG frame
 *         that may be prefixed by Sequence Header or GOP header data
 * return: int frametype of the 1.st frame found in the buffer.
 *         where 1 == intra, 2 == predicted, 3 == bidrectional predicted
 *         -1 is returned if no frame was found.
 */
gint
GVA_util_check_mpg_frame_type(unsigned char *buffer, gint32 buf_size)
{
  unsigned long code;
  gint     l_idx;
  gint     l_frame_type;
  unsigned l_picture_number;
  gint32   l_max_check_size;
  
  l_max_check_size = buf_size;
  
  l_frame_type = GVA_MPGFRAME_UNKNOWN;
  l_picture_number = 0;
  code = 0;
  l_idx = 0;
  while(l_idx < l_max_check_size)
  {
    code <<= 8;
    code |= buffer[l_idx++];
    
    if(code == GVA_MPGHDR_PICTURE_START_CODE)
    {
      /* found a picture start code
       * get next 10 bits for picture_number
       */
      l_picture_number =(unsigned long)buffer[l_idx] << 2;
      l_picture_number |= (unsigned long)((buffer[l_idx +1] >> 6) & 0x3);

      /* get next 3 bits for frame_type information */
      l_frame_type = ((buffer[l_idx +1] >> 3) & 0x7);
      break;
    }
  }
  

  if(gap_debug)
  {
    printf("GVA_util_check_mpg_frame_type: Frame type:%d  local_picnum:%d l_idx:%d\n"
          ,(int)l_frame_type
          ,(int)l_picture_number
          ,(int)l_idx
          );
  }
  
  return(l_frame_type);
}  /* end GVA_util_check_mpg_frame_type */


/* -------------------------------
 * GVA_util_fix_mpg_timecode
 * -------------------------------
 * In: buffer (in length buf_size)
 *     the buffer should contain one compressed MPEG
 *     frame, optionally prefixed by Sequence Header, GOP Header ...
 * If the buffer contains a GOP header,
 * the timecode in the GOP Header is replaced with a new timecode,
 * matching master_frame_nr at playbackrate of master_framerate (fps)
 *
 * the timecode is not replaced if the old code is all zero (00:00:00:00)
 */
void
GVA_util_fix_mpg_timecode(unsigned char *buffer
                         ,gint32 buf_size
                         ,gdouble master_framerate
                         ,gint32  master_frame_nr
                         )
{
  unsigned long code;
  gint          l_idx;

  
  code = 0;
  l_idx = 0;
  while(l_idx < buf_size)
  {
    code <<= 8;
    code |= buffer[l_idx++];
    
    if(code == GVA_MPGHDR_GOP_START_CODE)
    {
      int hour, minute, second, frame;
      float carry;

      /* Get the time old time code (including the drop_frame flag in the 1.st byte) */
      code = (unsigned long)buffer[l_idx] << 24;
      code |= (unsigned long)buffer[l_idx + 1] << 16;
      code |= (unsigned long)buffer[l_idx + 2] << 8;
      code |= (unsigned long)buffer[l_idx + 3];

      hour = code >> 26 & 0x1f;
      minute = code >> 20 & 0x3f;
      second = code >> 13 & 0x3f;
      frame = code >> 7 & 0x3f;

      if(gap_debug)
      {
        printf("Timecode old: %02d:%02d:%02d:%02d ", hour, minute, second, frame);
      }

      if((hour == 0)
      && (minute == 0)
      && (second == 0)
      && (frame == 0))
      {
        if(gap_debug)
        {
          printf("\n");
        }
      }
      else
      {
        /* Write a new time code */
        hour = (long)((float)(master_frame_nr - 1) / master_framerate / 3600);
        carry = hour * 3600 * master_framerate;
        minute = (long)((float)(master_frame_nr - 1 - carry) / master_framerate / 60);
        carry += minute * 60 * master_framerate;
        second = (long)((float)(master_frame_nr - 1 - carry) / master_framerate);
        carry += second * master_framerate;
        frame = (master_frame_nr - 1 - carry);

        buffer[l_idx] = ((code >> 24) & 0x80) | (hour << 2) | (minute >> 4);
        buffer[l_idx + 1] = ((code >> 16) & 0x08) | ((minute & 0xf) << 4) | (second >> 3);
        buffer[l_idx + 2] = ((second & 0x7) << 5) | (frame >> 1);
        buffer[l_idx + 3] = (code & 0x7f) | ((frame & 0x1) << 7);

        if(gap_debug)
        {
          printf("new: %02d:%02d:%02d:%02d\n", hour, minute, second, frame);
        }
      }

      break;
    }
  }
}  /* end GVA_util_fix_mpg_timecode */


/* ----------------------------------------
 * GVA_util_calculate_mpeg_frameheader_size
 * ----------------------------------------
 * scan the buffer for the 1st Mpeg picture start code.
 * all information from start of the buffer inclusive the picuture header
 * are considered as MPG header information.
 * (TODO: )
 *
 * returns the size of frame/gop header or 0 if no header is present.
 */
gint32
GVA_util_calculate_mpeg_frameheader_size(unsigned char *buffer
                         ,gint32 buf_size
                         )
{
  unsigned long code;
  gint     l_idx;
  gint     l_frame_type;
  unsigned l_picture_number;
  gint32   l_max_check_size;
  gint32   l_hdr_size;
  
  l_max_check_size = buf_size;
  
  l_frame_type = GVA_MPGFRAME_UNKNOWN;
  l_picture_number = 0;
  l_hdr_size = 0;
  code = 0;
  l_idx = 0;
  while(l_idx < l_max_check_size)
  {
    code <<= 8;
    code |= buffer[l_idx++];
    
    if(code == GVA_MPGHDR_PICTURE_START_CODE)
    {
      /* found a picture start code
       * get next 10 bits for picture_number
       */
      l_picture_number =(unsigned long)buffer[l_idx] << 2;
      l_picture_number |= (unsigned long)((buffer[l_idx +1] >> 6) & 0x3);

      /* get next 3 bits for frame_type information */
      l_frame_type = ((buffer[l_idx +1] >> 3) & 0x7);
      
      l_hdr_size = l_idx + 2; // TODO dont know if there are more bytes in the picture header ???
      break;
    }
  }
  

  if(gap_debug)
  {
    printf("GVA_util_calculate_mpeg_frameheader_size: %d  l_picture_number:%d frame_type:%d (1=I,2=P,3=B)\n"
          ,(int)l_hdr_size
          ,(int)l_picture_number
          ,(int)l_frame_type
          );
  }
  
  return(l_hdr_size);
}  /* end GVA_util_calculate_mpeg_frameheader_size */


/* ----------------------------------------
 * GVA_util_check_jpg_picture
 * ----------------------------------------
 * scan the buffer for the 1st JPEG picture.
 * This implementation checks for the jpeg typical "magic number"
 *    ff d8 ff
 * TODO: if libjpeg is available (#ifdef HAVE_??LIBJPG)
 *       we colud try to a more sophisticated check
 *       via jpeg_read_header from memory...
 *       
 * returns TRUE if the specified buffer contains a JPEG image.
 */
gboolean
GVA_util_check_jpg_picture(unsigned char *buffer
                         ,gint32 buf_size
                         ,gint32 max_check_size
                         ,gint32 *hdr_size
                         )
{
  gint     l_idx;
  gint32   l_max_check_size;
  gboolean l_jpeg_magic_number_found;
  
  l_max_check_size = MAX(max_check_size, 1);
  l_jpeg_magic_number_found = FALSE;
  l_idx = 0;
  while(l_idx < l_max_check_size)
  {
    /* check magic number */
    if ((buffer[l_idx]    == 0xff) 
    &&  (buffer[l_idx +1] == 0xd8)
    &&  (buffer[l_idx +2] == 0xff))
    {
      *hdr_size = l_idx;
      l_jpeg_magic_number_found = TRUE;
      break;
    }
    l_idx++;
  }
  

  if(gap_debug)
  {
    printf("GVA_util_check_jpg_magic_number: l_jpeg_magic_number_found:%d at idx:%d hdr_size:%d\n"
          ,(int)l_jpeg_magic_number_found
          ,(int)l_idx
          ,(int)*hdr_size
          );
  }
  
  return(l_jpeg_magic_number_found);
}  /* end GVA_util_check_jpg_picture */


/* ----------------------------------------
 * GVA_util_check_png_picture
 * ----------------------------------------
 * scan the buffer for the 1st PNG picture.
 * This implementation checks for the PNG typical "magic number"
 *   89 50 4e 47  (.PNG)
 *       
 * returns TRUE if the specified buffer contains a PNG image.
 */
gboolean
GVA_util_check_png_picture(unsigned char *buffer
                         ,gint32 buf_size
                         ,gint32 max_check_size
                         ,gint32 *hdr_size
                         )
{
  gint     l_idx;
  gint32   l_max_check_size;
  gboolean l_png_magic_number_found;
  
  l_max_check_size = MAX(max_check_size, 1);
  l_png_magic_number_found = FALSE;
  l_idx = 0;
  while(l_idx < l_max_check_size)
  {
    /* check magic number */
    if ((buffer[l_idx]    == 0x89) 
    &&  (buffer[l_idx +1] == 0x50)    // 'P'
    &&  (buffer[l_idx +2] == 0x4e)    // 'N'
    &&  (buffer[l_idx +3] == 0x47))   // 'G'
    {
      *hdr_size = l_idx;
      l_png_magic_number_found = TRUE;
      break;
    }
    l_idx++;
  }
  

  if(gap_debug)
  {
    printf("GVA_util_check_png_picture: l_png_magic_number_found:%d at idx:%d hdr_size:%d\n"
          ,(int)l_png_magic_number_found
          ,(int)l_idx
          ,(int)*hdr_size
          );
  }
  
  return(l_png_magic_number_found);
}  /* end GVA_util_check_png_picture */