1 /*****************************************************************************
2  * avcommon.h: common code for libav*
3  *****************************************************************************
4  * Copyright (C) 2012 VLC authors and VideoLAN
5  * $Id: 8c8298014ff95600cb5a171b93331979e3174047 $
6  *
7  * Authors: Rafaël Carré <funman@videolanorg>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 
24 #ifndef AVCOMMON_H
25 #define AVCOMMON_H 1
26 
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <vlc_avcodec.h>
32 #include <vlc_configuration.h>
33 #include <vlc_variables.h>
34 #include <vlc_es.h>
35 
36 #include <limits.h>
37 
38 #include "avcommon_compat.h"
39 
40 #ifdef HAVE_LIBAVUTIL_AVUTIL_H
41 # include <libavutil/avutil.h>
42 # include <libavutil/dict.h>
43 # include <libavutil/cpu.h>
44 # include <libavutil/log.h>
45 
46 #define AV_OPTIONS_TEXT     N_("Advanced options")
47 #define AV_OPTIONS_LONGTEXT N_("Advanced options, in the form {opt=val,opt2=val2}.")
48 
49 #define AV_RESET_TS_TEXT     "Reset timestamps"
50 #define AV_RESET_TS_LONGTEXT "The muxed content will start near a 0 timestamp."
51 
vlc_av_get_options(const char * psz_opts,AVDictionary ** pp_dict)52 static inline void vlc_av_get_options(const char *psz_opts, AVDictionary** pp_dict)
53 {
54     config_chain_t *cfg = NULL;
55     config_ChainParseOptions(&cfg, psz_opts);
56     while (cfg) {
57         config_chain_t *next = cfg->p_next;
58         av_dict_set(pp_dict, cfg->psz_name, cfg->psz_value, 0);
59         free(cfg->psz_name);
60         free(cfg->psz_value);
61         free(cfg);
62         cfg = next;
63     }
64 }
65 
vlc_init_avutil(vlc_object_t * obj)66 static inline void vlc_init_avutil(vlc_object_t *obj)
67 {
68     int level = AV_LOG_QUIET;
69 
70     if (!var_InheritBool(obj, "quiet")) {
71         int64_t verbose = var_InheritInteger(obj, "verbose");
72         if (verbose >= 0) switch(verbose + VLC_MSG_ERR) {
73         case VLC_MSG_ERR:
74             level = AV_LOG_ERROR;
75             break;
76         case VLC_MSG_WARN:
77             level = AV_LOG_WARNING;
78             break;
79         case VLC_MSG_INFO:
80             level = AV_LOG_INFO;
81             break;
82         case VLC_MSG_DBG:
83             level = AV_LOG_VERBOSE;
84             break;
85         case VLC_MSG_DBG+1:
86             level = AV_LOG_DEBUG;
87         default:
88             break;
89         }
90     }
91 
92     av_log_set_level(level);
93 
94     msg_Dbg(obj, "CPU flags: 0x%08x", av_get_cpu_flags());
95 }
96 #endif
97 
98 #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
99 # include <libavformat/avformat.h>
vlc_init_avformat(vlc_object_t * obj)100 static inline void vlc_init_avformat(vlc_object_t *obj)
101 {
102     vlc_avcodec_lock();
103 
104     vlc_init_avutil(obj);
105 
106     avformat_network_init();
107 
108     av_register_all();
109 
110     vlc_avcodec_unlock();
111 }
112 #endif
113 
114 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
115 # include <libavcodec/avcodec.h>
vlc_init_avcodec(vlc_object_t * obj)116 static inline void vlc_init_avcodec(vlc_object_t *obj)
117 {
118     vlc_avcodec_lock();
119 
120     vlc_init_avutil(obj);
121 
122     avcodec_register_all();
123 
124     vlc_avcodec_unlock();
125 }
126 #endif
127 
FromAVRational(const AVRational rat)128 static inline vlc_rational_t FromAVRational(const AVRational rat)
129 {
130     return (vlc_rational_t){.num = rat.num, .den = rat.den};
131 }
132 
set_video_color_settings(const video_format_t * p_fmt,AVCodecContext * p_context)133 static inline void set_video_color_settings( const video_format_t *p_fmt, AVCodecContext *p_context )
134 {
135     if( p_fmt->b_color_range_full )
136         p_context->color_range = AVCOL_RANGE_JPEG;
137 
138     switch( p_fmt->space )
139     {
140         case COLOR_SPACE_BT709:
141             p_context->colorspace = AVCOL_SPC_BT709;
142             break;
143         case COLOR_SPACE_BT601:
144             p_context->colorspace = AVCOL_SPC_BT470BG;
145             break;
146         case COLOR_SPACE_BT2020:
147             p_context->colorspace = AVCOL_SPC_BT2020_CL;
148             break;
149         default:
150             p_context->colorspace = AVCOL_SPC_UNSPECIFIED;
151             break;
152     }
153 
154     switch( p_fmt->transfer )
155     {
156         case TRANSFER_FUNC_LINEAR:
157             p_context->color_trc = AVCOL_TRC_LINEAR;
158             break;
159         case TRANSFER_FUNC_SRGB:
160             p_context->color_trc = AVCOL_TRC_GAMMA22;
161             break;
162         case TRANSFER_FUNC_BT470_BG:
163             p_context->color_trc = AVCOL_TRC_GAMMA28;
164             break;
165         case TRANSFER_FUNC_BT470_M:
166             p_context->color_trc = AVCOL_TRC_GAMMA22;
167             break;
168         case TRANSFER_FUNC_BT709:
169             p_context->color_trc = AVCOL_TRC_BT709;
170             break;
171         case TRANSFER_FUNC_SMPTE_ST2084:
172             p_context->color_trc = AVCOL_TRC_SMPTEST2084;
173             break;
174         case TRANSFER_FUNC_SMPTE_240:
175             p_context->color_trc = AVCOL_TRC_SMPTE240M;
176             break;
177         default:
178             p_context->color_trc = AVCOL_TRC_UNSPECIFIED;
179             break;
180     }
181     switch( p_fmt->primaries )
182     {
183         case COLOR_PRIMARIES_BT601_525:
184             p_context->color_primaries = AVCOL_PRI_SMPTE170M;
185             break;
186         case COLOR_PRIMARIES_BT601_625:
187             p_context->color_primaries = AVCOL_PRI_BT470BG;
188             break;
189         case COLOR_PRIMARIES_BT709:
190             p_context->color_primaries = AVCOL_PRI_BT709;
191             break;
192         case COLOR_PRIMARIES_BT2020:
193             p_context->color_primaries = AVCOL_PRI_BT2020;
194             break;
195         case COLOR_PRIMARIES_FCC1953:
196             p_context->color_primaries = AVCOL_PRI_BT470M;
197             break;
198         default:
199             p_context->color_primaries = AVCOL_PRI_UNSPECIFIED;
200             break;
201     }
202 }
203 
204 #endif
205