File: codecs.c

package info (click to toggle)
gmerlin-avdecoder 1.2.0~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,248 kB
  • sloc: ansic: 92,105; sh: 10,993; makefile: 736; sed: 16
file content (513 lines) | stat: -rw-r--r-- 10,413 bytes parent folder | download | duplicates (5)
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
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * 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/>.
 * *****************************************************************/

#include <avdec_private.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <config.h>
#include <codecs.h>
#include <pthread.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <utils.h>

// #define ENABLE_DEBUG

static void codecs_lock();
static void codecs_unlock();


#if (HAVE_REALDLL || HAVE_XADLL || HAVE_W32DLL)
static const char * find_directory(char const* const* dirs, const char * env_name)
  {
  struct stat st;
  const char * env_dir;
  int i;
  i = 0;
  while(dirs[i])
    {
    if(!stat(dirs[i], &st) && S_ISDIR(st.st_mode))
      return dirs[i];
    else
      i++;
    }
  
  if((env_dir = getenv(env_name)) && !stat(env_dir, &st) && S_ISDIR(st.st_mode))
    return env_dir;
    
  return NULL;
  }
#endif

#ifdef HAVE_REALDLL
static char const * const env_name_real = "GMERLIN_AVDEC_CODEC_PATH_REAL";

static char const * const real_dirs[] =
  {
    "/usr/lib/codecs",
    "/usr/local/lib/codecs",
    "/usr/lib/codecdlls",
    "/usr/lib/win32",
    "/usr/local/lib/win32",
    NULL
  };

static void bgav_set_dll_path_real()
  {
  if(bgav_dll_path_real)
    {
    free(bgav_dll_path_real);
    }
  bgav_dll_path_real = bgav_strdup(find_directory(real_dirs, env_name_real));
  }
#endif

#ifdef HAVE_XADLL
static const char * env_name_xanim =  "GMERLIN_AVDEC_CODEC_PATH_XANIM";

static char * xanim_dirs[] =
  {
    "/usr/lib/codecs",
    "/usr/local/lob/codecs",
    "/usr/lib/codecdlls",
    "/usr/lib/win32",
    "/usr/local/lib/win32",
    NULL
  };

static void bgav_set_dll_path_xanim()
  {
  if(bgav_dll_path_xanim)
    {
    free(bgav_dll_path_xanim);
    }
  bgav_dll_path_xanim =
    bgav_strdup(find_directory((const char * const*)xanim_dirs, env_name_xanim));
  }
#endif

#ifdef HAVE_W32DLL
static const char * env_name_win32 = "GMERLIN_AVDEC_CODEC_PATH_WIN32";

static char * win32_dirs[] =
  {
    "/usr/lib/codecs",
    "/usr/local/lob/codecs",
    "/usr/lib/codecdlls",
    "/usr/lib/win32",
    "/usr/local/lib/win32",
    NULL
  };
  
static int win_path_needs_delete = 0;

static void bgav_set_dll_path_win32()
  {
  if(win_path_needs_delete)
    {
    free(win32_def_path);
    }
  win32_def_path = bgav_strdup(find_directory(win32_dirs, env_name_win32));
  if(win32_def_path)
    win_path_needs_delete = 1;
  }
#endif

/* Simple codec registry */

/*
 *  Since we link all codecs statically,
 *  this stays quite simple
 */

static bgav_audio_decoder_t * audio_decoders = NULL;
static bgav_video_decoder_t * video_decoders = NULL;
static bgav_subtitle_overlay_decoder_t * subtitle_overlay_decoders = NULL;

static int codecs_initialized = 0;

static int num_audio_codecs = 0;
static int num_video_codecs = 0;
static int num_subtitle_overlay_codecs = 0;

static pthread_mutex_t codec_mutex;
static int mutex_initialized = 0;


static void codecs_lock()
  {
  if(!mutex_initialized)
    {
    pthread_mutex_init(&codec_mutex, NULL);
    mutex_initialized = 1;
    }
  pthread_mutex_lock(&codec_mutex);
  }

static void codecs_unlock()
  {
  pthread_mutex_unlock(&codec_mutex);
  }


void bgav_codecs_dump()
  {
  bgav_audio_decoder_t * ad;
  bgav_video_decoder_t * vd;
  bgav_subtitle_overlay_decoder_t * sod;
  int i;
  bgav_codecs_init(NULL);
  
  /* Print */
  ad = audio_decoders;

  bgav_dprintf("<h2>Audio codecs</h2>\n");

  bgav_dprintf("<ul>\n");
  for(i = 0; i < num_audio_codecs; i++)
    {
    bgav_dprintf("<li>%s\n", ad->name);
    ad = ad->next;
    }
  bgav_dprintf("</ul>\n");
  
  bgav_dprintf("<h2>Video codecs</h2>\n");
  bgav_dprintf("<ul>\n");
  vd = video_decoders;
  for(i = 0; i < num_video_codecs; i++)
    {
    bgav_dprintf("<li>%s\n", vd->name);
    vd = vd->next;
    }
  bgav_dprintf("</ul>\n");

  bgav_dprintf("<h2>Graphical subtitle codecs</h2>\n");
  bgav_dprintf("<ul>\n");
  sod = subtitle_overlay_decoders;
  for(i = 0; i < num_subtitle_overlay_codecs; i++)
    {
    bgav_dprintf("<li>%s\n", sod->name);
    sod = sod->next;
    }
  bgav_dprintf("</ul>\n");

  }


void bgav_codecs_init(bgav_options_t * opt)
  {
  codecs_lock();
  if(codecs_initialized)
    {
    codecs_unlock();
    return;
    }
  codecs_initialized = 1;
  
  /* ffmpeg codecs should be initialized BEFORE any DLL codecs */
#ifdef HAVE_LIBAVCODEC
  bgav_init_audio_decoders_ffmpeg(opt);
  bgav_init_video_decoders_ffmpeg(opt);
#endif
#ifdef HAVE_VORBIS
  bgav_init_audio_decoders_vorbis();
#endif

#ifdef HAVE_LIBA52
  bgav_init_audio_decoders_a52();
#endif

#ifdef HAVE_DCA
  bgav_init_audio_decoders_dca();
#endif

#ifdef HAVE_MAD
  bgav_init_audio_decoders_mad();
#endif

#ifdef HAVE_LIBPNG
  bgav_init_video_decoders_png();
#endif

#ifdef HAVE_OPENJPEG
  bgav_init_video_decoders_openjpeg();
#endif

#ifdef HAVE_LIBTIFF
  bgav_init_video_decoders_tiff();
#endif

#ifdef HAVE_THEORADEC
  bgav_init_video_decoders_theora();
#endif

#ifdef HAVE_SCHROEDINGER
  bgav_init_video_decoders_schroedinger();
#endif

#ifdef HAVE_SPEEX
  bgav_init_audio_decoders_speex();
#endif

#ifdef HAVE_FAAD2
  bgav_init_audio_decoders_faad2();
#endif

#ifdef HAVE_FLAC
  bgav_init_audio_decoders_flac();
#endif
  
#ifdef HAVE_LIBMPEG2
  bgav_init_video_decoders_libmpeg2();
#endif
  
#ifdef HAVE_XADLL

  bgav_set_dll_path_xanim();

  if(bgav_dll_path_xanim)
    {
    bgav_init_video_decoders_xadll(opt);
    }
#endif

#ifdef HAVE_REALDLL
  
  bgav_set_dll_path_real();

  if(bgav_dll_path_real)
    {
    bgav_init_video_decoders_real(opt);
    bgav_init_audio_decoders_real(opt);
    }
#endif

#ifdef HAVE_W32DLL
  
  bgav_set_dll_path_win32();

  if(win_path_needs_delete)
    {
    bgav_init_video_decoders_win32(opt);
    bgav_init_audio_decoders_win32(opt);
    bgav_init_audio_decoders_qtwin32(opt);
    }
  
#endif
  
  bgav_init_audio_decoders_gavl();
  bgav_init_audio_decoders_pcm();
#ifdef HAVE_LIBGSM
  bgav_init_audio_decoders_gsm();
#endif
  bgav_init_video_decoders_aviraw();
  bgav_init_video_decoders_qtraw();
  bgav_init_video_decoders_yuv();
  bgav_init_video_decoders_tga();
  bgav_init_video_decoders_rtjpeg();
  bgav_init_video_decoders_gavl();

  bgav_init_subtitle_overlay_decoders_dvd();

  codecs_unlock();
  
  }

void bgav_audio_decoder_register(bgav_audio_decoder_t * dec)
  {
  bgav_audio_decoder_t * before;
  if(!audio_decoders)
    audio_decoders = dec;
  else
    {
    before = audio_decoders;
    while(before->next)
      before = before->next;
    before->next = dec;
    }
  dec->next = NULL;
  num_audio_codecs++;
  }

void bgav_video_decoder_register(bgav_video_decoder_t * dec)
  {
  bgav_video_decoder_t * before;
  if(!video_decoders)
    video_decoders = dec;
  else
    {
    before = video_decoders;
    while(before->next)
      before = before->next;
    before->next = dec;
    }
  dec->next = NULL;
  num_video_codecs++;
  }

void bgav_subtitle_overlay_decoder_register(bgav_subtitle_overlay_decoder_t * dec)
  {
  bgav_subtitle_overlay_decoder_t * before;
  if(!subtitle_overlay_decoders)
    subtitle_overlay_decoders = dec;
  else
    {
    before = subtitle_overlay_decoders;
    while(before->next)
      before = before->next;
    before->next = dec;
    }
  dec->next = NULL;
  num_subtitle_overlay_codecs++;
  
  }

bgav_audio_decoder_t * bgav_find_audio_decoder(bgav_stream_t * s)
  {
  bgav_audio_decoder_t * cur;
  int i;
  codecs_lock();
  cur = audio_decoders;
  //  if(!codecs_initialized)
  //    bgav_codecs_init();

  
  while(cur)
    {
    i = 0;
    while(cur->fourccs[i])
      {
      if(cur->fourccs[i] == s->fourcc)
        {
        codecs_unlock();
        return cur;
        }
      else
        i++;
      }
    cur = cur->next;
    }
  codecs_unlock();
  return NULL;
  }

bgav_video_decoder_t * bgav_find_video_decoder(bgav_stream_t * s)
  {
  bgav_video_decoder_t * cur;
  int i;
  codecs_lock();

  //  if(!codecs_initialized)
  //    bgav_codecs_init();
  
  cur = video_decoders;

  while(cur)
    {
    i = 0;
    while(cur->fourccs[i])
      {
      if(cur->fourccs[i] == s->fourcc)
        {
        codecs_unlock();
        return cur;
        }
      else
        i++;
      }
    cur = cur->next;
    }
  codecs_unlock();
  return NULL;
  }

bgav_subtitle_overlay_decoder_t * bgav_find_subtitle_overlay_decoder(bgav_stream_t * s)
  {
  bgav_subtitle_overlay_decoder_t * cur;
  int i;
  codecs_lock();

  //  if(!codecs_initialized)
  //    bgav_codecs_init();
  
  cur = subtitle_overlay_decoders;

  while(cur)
    {
    i = 0;
    while(cur->fourccs[i])
      {
      if(cur->fourccs[i] == s->fourcc)
        {
        codecs_unlock();
        return cur;
        }
      else
        i++;
      }
    cur = cur->next;
    }
  codecs_unlock();
  return NULL;
  }


/* Free codec strings */

/***************************************************************
 * This will hopefully make the destruction for dynamic loading
 * (Trick comes from a 1995 version of the ELF Howto, so it
 * should work everywhere now)
 ***************************************************************/

#if defined(__GNUC__) && defined(__ELF__)
static void __cleanup() __attribute__ ((destructor));
 
static void __cleanup()
  {
  if(mutex_initialized)
    pthread_mutex_destroy(&codec_mutex);
    
#ifdef HAVE_W32DLL
  if(win_path_needs_delete)
    {
    free(win32_def_path);
    }
#endif
#ifdef HAVE_REALDLL
  if(bgav_dll_path_real)
    {
    free(bgav_dll_path_real);
    }
#endif  
#ifdef HAVE_XADLL
  if(bgav_dll_path_xanim)
    {
    free(bgav_dll_path_xanim);
    }
#endif
  }

#endif