File: utils.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,432 kB
  • sloc: ansic: 89,363; sh: 667; makefile: 650; awk: 43; sed: 16
file content (456 lines) | stat: -rw-r--r-- 11,107 bytes parent folder | download
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
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



#define _GNU_SOURCE /* vasprintf */

#include <avdec_private.h>

#define LOG_DOMAIN "utils"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <regex.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <pthread.h>
#else
#include <sys/socket.h>
#include <sys/select.h>
#endif

#include <utils.h>

void bgav_dump_fourcc(uint32_t fourcc)
  {
  if((fourcc & 0xffff0000) || !(fourcc))
    gavl_dprintf( "%c%c%c%c (%08x)",
            (fourcc & 0xFF000000) >> 24,
            (fourcc & 0x00FF0000) >> 16,
            (fourcc & 0x0000FF00) >> 8,
            (fourcc & 0x000000FF),
            fourcc);
  else
    gavl_dprintf( "WavID: 0x%04x", fourcc);
    
  }

int bgav_check_fourcc(uint32_t fourcc, const uint32_t * fourccs)
  {
  int i = 0;
  while(fourccs[i])
    {
    if(fourccs[i] == fourcc)
      return 1;
    else
      i++;
    }
  return 0;
  }


/*
 *  Read a single line from a filedescriptor
 *
 *  ret will be reallocated if neccesary and ret_alloc will
 *  be updated then
 *
 *  The string will be 0 terminated, a trailing \r or \n will
 *  be removed
 */
#define BYTES_TO_ALLOC 1024

int bgav_read_line_fd(const bgav_options_t * opt,
                      int fd, char ** ret, int * ret_alloc, int milliseconds)
  {
  char * pos;
  char c;
  int bytes_read;
  bytes_read = 0;
  /* Allocate Memory for the case we have none */
  if(!(*ret_alloc))
    {
    *ret_alloc = BYTES_TO_ALLOC;
    *ret = realloc(*ret, *ret_alloc);
    **ret = '\0';
    }
  pos = *ret;
  while(1)
    {
    if(!bgav_read_data_fd(opt, fd, (uint8_t*)(&c), 1, milliseconds))
      {
      if(!bytes_read)
        return 0;
      break;
      }
    /*
     *  Line break sequence
     *  is starting, remove the rest from the stream
     */
    if(c == '\n')
      {
      break;
      }
    /* Reallocate buffer */
    else if(c != '\r')
      {
      if(bytes_read+2 >= *ret_alloc)
        {
        *ret_alloc += BYTES_TO_ALLOC;
        *ret = realloc(*ret, *ret_alloc);
        pos = &((*ret)[bytes_read]);
        }
      /* Put the byte and advance pointer */
      *pos = c;
      pos++;
      bytes_read++;
      }
    }
  *pos = '\0';
  return 1;
  }

int bgav_read_data_fd(const bgav_options_t * opt,
                      int fd, uint8_t * ret, int len, int milliseconds)
  {
  int bytes_read = 0;
  int result;
  fd_set rset;
  struct timeval timeout;

  int flags = 0;


  //  if(milliseconds < 0)
  //    flags = MSG_WAITALL;
  
  while(bytes_read < len)
    {
    if(milliseconds >= 0)
      { 
      FD_ZERO (&rset);
      FD_SET  (fd, &rset);
     
      timeout.tv_sec  = milliseconds / 1000;
      timeout.tv_usec = (milliseconds % 1000) * 1000;
    
      if((result = select (fd+1, &rset, NULL, NULL, &timeout)) <= 0)
        {
//        fprintf(stderr, "Read timed out %d\n", milliseconds);
        return bytes_read;
        }
      }

    result = recv(fd, ret + bytes_read, len - bytes_read, flags);
    if(result > 0)
      bytes_read += result;
    else if(result <= 0)
      {
      if(result <0)
        gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
                 "recv failed: %s", strerror(errno));
      return bytes_read;
      }
    }
  return bytes_read;
  }

int bgav_slurp_file(const char * location,
                    bgav_packet_t * p,
                    const bgav_options_t * opt)
  {
  bgav_input_context_t * input;
  input = bgav_input_create(NULL, opt);
  if(!bgav_input_open(input, location))
    {
    bgav_input_destroy(input);
    return 0;
    }
  if(!input->total_bytes)
    {
    bgav_input_destroy(input);
    return 0;
    }

  gavl_packet_alloc(p, input->total_bytes);
  
  if(bgav_input_read_data(input, p->buf.buf, input->total_bytes) <
     input->total_bytes)
    {
    bgav_input_destroy(input);
    return 0;
    }
  p->buf.len = input->total_bytes;
  bgav_input_destroy(input);
  return 1;
  }

int bgav_check_file_read(const char * filename)
  {
  if(access(filename, R_OK))
    return 0;
  return 1;
  }

/* Search file for writing */

char * bgav_search_file_write(const bgav_options_t * opt,
                              const char * directory, const char * file)
  {
  char * home_dir;
  char * testpath;
  char * pos1;
  char * pos2;
  FILE * testfile;

  if(!file)
    return NULL;
  
  testpath = malloc((PATH_MAX+1) * sizeof(char));
  
  home_dir = getenv("HOME");

  /* Try to open the file */

  snprintf(testpath, PATH_MAX,
           "%s/.%s/%s/%s", home_dir, PACKAGE, directory, file);
  testfile = fopen(testpath, "a");
  if(testfile)
    {
    fclose(testfile);
    return testpath;
    }
  
  if(errno != ENOENT)
    {
    free(testpath);
    return NULL;
    }

  /*
   *  No such file or directory can mean, that the directory 
   *  doesn't exist
   */
  
  pos1 = &testpath[strlen(home_dir)+1];
  
  while(1)
    {
    pos2 = strchr(pos1, '/');

    if(!pos2)
      break;

    *pos2 = '\0';

#ifdef _WIN32
    if(mkdir(testpath) == -1)
#else
    if(mkdir(testpath, S_IRUSR|S_IWUSR|S_IXUSR) == -1)
#endif
      {
      if(errno != EEXIST)
        {
        *pos2 = '/';
        break;
        }
      }
    else
      gavl_log(GAVL_LOG_INFO, LOG_DOMAIN,
               "Created directory %s", testpath);
    
    *pos2 = '/';
    pos1 = pos2;
    pos1++;
    }

  /* Try once more to open the file */

  testfile = fopen(testpath, "w");

  if(testfile)
    {
    fclose(testfile);
    return testpath;
    }
  free(testpath);
  return NULL;
  }

char * bgav_search_file_read(const bgav_options_t * opt,
                             const char * directory, const char * file)
  {
  char * home_dir;
  char * test_path;

  home_dir = getenv("HOME");

  test_path = malloc((PATH_MAX+1) * sizeof(*test_path));
  snprintf(test_path, PATH_MAX, "%s/.%s/%s/%s",
           home_dir, PACKAGE, directory, file);

  if(bgav_check_file_read(test_path))
    return test_path;

  free(test_path);
  return NULL;
  }

int bgav_match_regexp(const char * str, const char * regexp)
  {
  int ret = 0;
  regex_t exp;
  memset(&exp, 0, sizeof(exp));

  /*
    `REG_EXTENDED'
     Treat the pattern as an extended regular expression, rather than
     as a basic regular expression.

     `REG_ICASE'
     Ignore case when matching letters.

     `REG_NOSUB'
     Don't bother storing the contents of the MATCHES-PTR array.

     `REG_NEWLINE'
     Treat a newline in STRING as dividing STRING into multiple lines,
     so that `$' can match before the newline and `^' can match after.
     Also, don't permit `.' to match a newline, and don't permit
     `[^...]' to match a newline.
  */
  
  regcomp(&exp, regexp, REG_NOSUB|REG_EXTENDED);
  if(!regexec(&exp, str, 0, NULL, 0))
    ret = 1;
  regfree(&exp);
  return ret;
  }


static const struct
  {
  gavl_codec_id_t id;
  uint32_t fourcc;
  }
fourccs[] =
  {
    { GAVL_CODEC_ID_NONE,      BGAV_MK_FOURCC('g','a','v','f') },
    /* Audio */
    { GAVL_CODEC_ID_ALAW,      BGAV_MK_FOURCC('a','l','a','w') }, 
    { GAVL_CODEC_ID_ULAW,      BGAV_MK_FOURCC('u','l','a','w') }, 
    { GAVL_CODEC_ID_MP2,       BGAV_MK_FOURCC('.','m','p','2') }, 
    { GAVL_CODEC_ID_MP3,       BGAV_MK_FOURCC('.','m','p','3') }, 
    { GAVL_CODEC_ID_AC3,       BGAV_MK_FOURCC('.','a','c','3') }, 
    { GAVL_CODEC_ID_AAC,       BGAV_MK_FOURCC('m','p','4','a') }, 
    { GAVL_CODEC_ID_VORBIS,    BGAV_MK_FOURCC('V','B','I','S') }, 
    { GAVL_CODEC_ID_FLAC,      BGAV_MK_FOURCC('F','L','A','C') }, 
    { GAVL_CODEC_ID_OPUS,      BGAV_MK_FOURCC('O','P','U','S') }, 
    { GAVL_CODEC_ID_SPEEX,     BGAV_MK_FOURCC('S','P','E','X') }, 
    { GAVL_CODEC_ID_DTS,       BGAV_MK_FOURCC('d','t','s',' ') }, 
    
    /* Video */
    { GAVL_CODEC_ID_JPEG,      BGAV_MK_FOURCC('j','p','e','g') }, 
    { GAVL_CODEC_ID_PNG,       BGAV_MK_FOURCC('p','n','g',' ') }, 
    { GAVL_CODEC_ID_TIFF,      BGAV_MK_FOURCC('t','i','f','f') }, 
    { GAVL_CODEC_ID_TGA,       BGAV_MK_FOURCC('t','g','a',' ') }, 
    { GAVL_CODEC_ID_MPEG1,     BGAV_MK_FOURCC('m','p','v','1') }, 
    { GAVL_CODEC_ID_MPEG2,     BGAV_MK_FOURCC('m','p','v','2') },
    { GAVL_CODEC_ID_MPEG4_ASP, BGAV_MK_FOURCC('m','p','4','v') },
    { GAVL_CODEC_ID_H264,      BGAV_MK_FOURCC('H','2','6','4') },
    { GAVL_CODEC_ID_THEORA,    BGAV_MK_FOURCC('T','H','R','A') },
    { GAVL_CODEC_ID_DIRAC,     BGAV_MK_FOURCC('d','r','a','c') },
    { GAVL_CODEC_ID_DV,        BGAV_MK_FOURCC('D','V',' ',' ') },
    { GAVL_CODEC_ID_VP8,       BGAV_MK_FOURCC('V','P','8','0') },
    { GAVL_CODEC_ID_DIV3,      BGAV_MK_FOURCC('D','I','V','3') },

    /* Overlay */
    { GAVL_CODEC_ID_DVDSUB,    BGAV_MK_FOURCC('D','V','D','S') },
    { /* End */                                                },
  };

uint32_t bgav_compression_id_2_fourcc(gavl_codec_id_t id)
  {
  int i = 0;
  while(fourccs[i].fourcc)
    {
    if(fourccs[i].id == id)
      return fourccs[i].fourcc;
    i++;
    }
  return 0;
  }

uint32_t * bgav_get_vobsub_palette(const char * str)
  {
  int index;
  uint32_t * pal = NULL;
  float r, g, b;
  int y, u, v;
        
  char ** colors = gavl_strbreak(str, ',');

  index = 0;
  while(colors[index])
    index++;
  if(index == 16)
    {
    index = 0;
    pal = malloc(16 * sizeof(*pal));

    for(index = 0; index < 16; index++)
      {
      pal[index] = strtol(colors[index], NULL, 16);

      /* Now it gets insane: The vobsub program
         converts the YCbCr palette from the IFO file to RGB,
         so we need to convert it back

         http://guliverkli.svn.sourceforge.net/viewvc/guliverkli/
         trunk/guliverkli/src/subtitles/
         VobSubFile.cpp?revision=605&view=markup
         (line 821)
      */
            
      r = (pal[index] >> 16) & 0xff;
      g = (pal[index] >>  8) & 0xff;
      b = pal[index] & 0xff;

      y =  (int)((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
      u =  (int)(-(0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
      v =  (int)((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
      pal[index] = y << 16 | u << 8 | v;
      }
    }
  gavl_strbreak_free(colors);
  return pal;  
  }