1 #include "plumbing/transcoding.h"
2 #include "libav.h"
3 
4 /**
5  *
6  */
7 static void
libav_log_callback(void * ptr,int level,const char * fmt,va_list vl)8 libav_log_callback(void *ptr, int level, const char *fmt, va_list vl)
9 {
10     char message[8192];
11     char *nl;
12     char *l;
13 
14     if ((level == AV_LOG_DEBUG) && !(tvhlog_options & TVHLOG_OPT_LIBAV))
15       return;
16 
17     memset(message, 0, sizeof(message));
18     vsnprintf(message, sizeof(message), fmt, vl);
19 
20     l = message;
21 
22     if(level == AV_LOG_DEBUG)
23 #if ENABLE_TRACE
24       level = LOG_TRACE;
25 #else
26       level = LOG_DEBUG;
27 #endif
28     else if(level == AV_LOG_VERBOSE)
29       level = LOG_DEBUG;
30     else if(level == AV_LOG_INFO)
31       level = LOG_INFO;
32     else if(level == AV_LOG_WARNING)
33       level = LOG_WARNING;
34     else if(level == AV_LOG_ERROR)
35       level = LOG_ERR;
36     else if(level == AV_LOG_FATAL)
37       level = LOG_CRIT;
38     else if(level == AV_LOG_PANIC)
39       level = LOG_EMERG;
40 
41     if (level == LOG_INFO) {
42       if (!strncmp(message, "--prefix=/", 10))
43         return;
44     }
45 
46     while(l < message + sizeof(message)) {
47       nl = strstr(l, "\n");
48       if(nl)
49         *nl = '\0';
50 
51       if(!strlen(l))
52         break;
53 
54       tvhlog(level, LS_LIBAV, "%s", l);
55 
56       if(!nl)
57         break;
58 
59       l = nl + 1;
60     }
61 }
62 
63 /**
64  * Translate a component type to a libavcodec id
65  */
66 enum AVCodecID
streaming_component_type2codec_id(streaming_component_type_t type)67 streaming_component_type2codec_id(streaming_component_type_t type)
68 {
69   enum AVCodecID codec_id = AV_CODEC_ID_NONE;
70 
71   switch(type) {
72   case SCT_H264:
73     codec_id = AV_CODEC_ID_H264;
74     break;
75   case SCT_MPEG2VIDEO:
76     codec_id = AV_CODEC_ID_MPEG2VIDEO;
77     break;
78   case SCT_VP8:
79     codec_id = AV_CODEC_ID_VP8;
80     break;
81   case SCT_VP9:
82     codec_id = AV_CODEC_ID_VP9;
83     break;
84   case SCT_HEVC:
85     codec_id = AV_CODEC_ID_HEVC;
86     break;
87   case SCT_AC3:
88     codec_id = AV_CODEC_ID_AC3;
89     break;
90   case SCT_EAC3:
91     codec_id = AV_CODEC_ID_EAC3;
92     break;
93   case SCT_MP4A:
94   case SCT_AAC:
95     codec_id = AV_CODEC_ID_AAC;
96     break;
97   case SCT_MPEG2AUDIO:
98     codec_id = AV_CODEC_ID_MP2;
99     break;
100   case SCT_VORBIS:
101     codec_id = AV_CODEC_ID_VORBIS;
102     break;
103   case SCT_DVBSUB:
104     codec_id = AV_CODEC_ID_DVB_SUBTITLE;
105     break;
106   case SCT_TEXTSUB:
107     codec_id = AV_CODEC_ID_TEXT;
108     break;
109  case SCT_TELETEXT:
110     codec_id = AV_CODEC_ID_DVB_TELETEXT;
111     break;
112   default:
113     codec_id = AV_CODEC_ID_NONE;
114     break;
115   }
116 
117   return codec_id;
118 }
119 
120 
121 /**
122  * Translate a libavcodec id to a component type
123  */
124 streaming_component_type_t
codec_id2streaming_component_type(enum AVCodecID id)125 codec_id2streaming_component_type(enum AVCodecID id)
126 {
127   streaming_component_type_t type = SCT_NONE;
128 
129   switch(id) {
130   case AV_CODEC_ID_H264:
131     type = SCT_H264;
132     break;
133   case AV_CODEC_ID_MPEG2VIDEO:
134     type = SCT_MPEG2VIDEO;
135     break;
136   case AV_CODEC_ID_VP8:
137     type = SCT_VP8;
138     break;
139   case AV_CODEC_ID_VP9:
140     type = SCT_VP9;
141     break;
142   case AV_CODEC_ID_HEVC:
143     type = SCT_HEVC;
144     break;
145   case AV_CODEC_ID_AC3:
146     type = SCT_AC3;
147     break;
148   case AV_CODEC_ID_EAC3:
149     type = SCT_EAC3;
150     break;
151   case AV_CODEC_ID_AAC:
152     type = SCT_AAC;
153     break;
154   case AV_CODEC_ID_MP2:
155     type = SCT_MPEG2AUDIO;
156     break;
157   case AV_CODEC_ID_VORBIS:
158     type = SCT_VORBIS;
159     break;
160   case AV_CODEC_ID_DVB_SUBTITLE:
161     type = SCT_DVBSUB;
162     break;
163   case AV_CODEC_ID_TEXT:
164     type = SCT_TEXTSUB;
165     break;
166   case AV_CODEC_ID_DVB_TELETEXT:
167     type = SCT_TELETEXT;
168     break;
169   case AV_CODEC_ID_NONE:
170     type = SCT_NONE;
171     break;
172   default:
173     type = SCT_UNKNOWN;
174     break;
175   }
176 
177   return type;
178 }
179 
180 
181 /**
182  *
183  */
184 int
libav_is_encoder(AVCodec * codec)185 libav_is_encoder(AVCodec *codec)
186 {
187 #if LIBAVCODEC_VERSION_INT >= ((54<<16)+(7<<8)+0)
188   return av_codec_is_encoder(codec);
189 #else
190   return codec->encode || codec->encode2;
191 #endif
192 }
193 
194 /**
195  *
196  */
197 void
libav_set_loglevel(void)198 libav_set_loglevel(void)
199 {
200   int level = AV_LOG_VERBOSE;
201 
202   if (tvhlog_options & TVHLOG_OPT_LIBAV)
203     level = AV_LOG_DEBUG;
204 
205   av_log_set_level(level);
206 }
207 
208 /**
209  *
210  */
211 void
libav_init(void)212 libav_init(void)
213 {
214   av_log_set_callback(libav_log_callback);
215   libav_set_loglevel();
216   av_register_all();
217   avfilter_register_all();
218   transcoding_init();
219 }
220