1 /*
2  * Copyright (c) 2013 Andrew Kelley
3  *
4  * This file is part of libgroove, which is MIT licensed.
5  * See http://opensource.org/licenses/MIT
6  */
7 
8 #include "groove.h"
9 #include "config.h"
10 
11 #include <libavfilter/avfilter.h>
12 #include <libavformat/avformat.h>
13 #include <libavutil/channel_layout.h>
14 #include <pthread.h>
15 
16 static int should_deinit_network = 0;
17 
my_lockmgr_cb(void ** mutex,enum AVLockOp op)18 static int my_lockmgr_cb(void **mutex, enum AVLockOp op) {
19     if (mutex == NULL)
20         return -1;
21     pthread_mutex_t *pmutex;
22     switch (op) {
23         case AV_LOCK_CREATE:
24             pmutex = av_mallocz(sizeof(pthread_mutex_t));
25             *mutex = pmutex;
26             return pthread_mutex_init(pmutex, NULL);
27         case AV_LOCK_OBTAIN:
28             pmutex = *mutex;
29             return pthread_mutex_lock(pmutex);
30         case AV_LOCK_RELEASE:
31             pmutex = *mutex;
32             return pthread_mutex_unlock(pmutex);
33         case AV_LOCK_DESTROY:
34             pmutex = *mutex;
35             int err = pthread_mutex_destroy(pmutex);
36             av_free(pmutex);
37             *mutex = NULL;
38             return err;
39     }
40     return 0;
41 }
42 
groove_init(void)43 int groove_init(void) {
44     av_lockmgr_register(&my_lockmgr_cb);
45 
46     srand(time(NULL));
47 
48     // register all codecs, demux and protocols
49     avcodec_register_all();
50     av_register_all();
51     avformat_network_init();
52     avfilter_register_all();
53 
54     should_deinit_network = 1;
55 
56     av_log_set_level(AV_LOG_QUIET);
57     return 0;
58 }
59 
groove_finish(void)60 void groove_finish(void) {
61     if (should_deinit_network) {
62         avformat_network_deinit();
63         should_deinit_network = 0;
64     }
65 }
66 
groove_set_logging(int level)67 void groove_set_logging(int level) {
68     av_log_set_level(level);
69 }
70 
groove_channel_layout_count(uint64_t channel_layout)71 int groove_channel_layout_count(uint64_t channel_layout) {
72     return av_get_channel_layout_nb_channels(channel_layout);
73 }
74 
groove_channel_layout_default(int count)75 uint64_t groove_channel_layout_default(int count) {
76     return av_get_default_channel_layout(count);
77 }
78 
groove_sample_format_bytes_per_sample(enum GrooveSampleFormat format)79 int groove_sample_format_bytes_per_sample(enum GrooveSampleFormat format) {
80     return av_get_bytes_per_sample((enum AVSampleFormat)format);
81 }
82 
groove_audio_formats_equal(const struct GrooveAudioFormat * a,const struct GrooveAudioFormat * b)83 int groove_audio_formats_equal(const struct GrooveAudioFormat *a, const struct GrooveAudioFormat *b) {
84     return (a->sample_rate    == b->sample_rate &&
85             a->channel_layout == b->channel_layout &&
86             a->sample_fmt     == b->sample_fmt);
87 }
88 
groove_version(void)89 const char *groove_version(void) {
90     return GROOVE_VERSION_STRING;
91 }
92 
groove_version_major(void)93 int groove_version_major(void) {
94     return GROOVE_VERSION_MAJOR;
95 }
96 
groove_version_minor(void)97 int groove_version_minor(void) {
98     return GROOVE_VERSION_MINOR;
99 }
100 
groove_version_patch(void)101 int groove_version_patch(void) {
102     return GROOVE_VERSION_PATCH;
103 }
104