Coverage Report

Created: 2026-05-21 08:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/avcodec/avcodec.c
Line
Count
Source
1
/*****************************************************************************
2
 * avcodec.c: video and audio decoder and encoder using libavcodec
3
 *****************************************************************************
4
 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Gildas Bazin <gbazin@videolan.org>
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
/*****************************************************************************
25
 * Preamble
26
 *****************************************************************************/
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include <vlc_codec.h>
34
#include <vlc_demux.h>
35
#include <vlc_avcodec.h>
36
#include <vlc_cpu.h>
37
38
#include <libavcodec/avcodec.h>
39
40
#include "avcodec.h"
41
#include "chroma.h"
42
#include "avcommon.h"
43
44
/****************************************************************************
45
 * Local prototypes
46
 ****************************************************************************/
47
static const int  frame_skip_list[] = { -1, 0, 1, 2, 3, 4 };
48
static const char *const frame_skip_list_text[] =
49
  { N_("None"), N_("Default"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
50
51
static const int  idct_skip_list[] = { -1, 0, 1, 2, 3, 4 };
52
static const char *const idct_skip_list_text[] =
53
  { N_("None"), N_("Default"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
54
55
static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
56
static const char *const nloopf_list_text[] =
57
  { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
58
59
#ifdef ENABLE_SOUT
60
static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
61
static const char *const enc_hq_list_text[] = {
62
    N_("rd"), N_("bits"), N_("simple") };
63
#endif
64
65
#ifdef MERGE_FFMPEG
66
# ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
67
#  include "../../demux/avformat/avformat.h"
68
# endif
69
# include "../../access/avio.h"
70
# include "../../packetizer/avparser.h"
71
#endif
72
73
/*****************************************************************************
74
 * Module descriptor
75
 *****************************************************************************/
76
#define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
77
        "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
78
        "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
79
80
138
vlc_module_begin ()
81
69
    set_shortname( "FFmpeg")
82
69
    set_subcategory( SUBCAT_INPUT_VCODEC )
83
    /* decoder main module */
84
69
    set_description( N_("FFmpeg audio/video decoder") )
85
69
    set_help( MODULE_DESCRIPTION )
86
69
    set_section( N_("Decoding") , NULL )
87
88
69
    add_shortcut("ffmpeg")
89
69
    set_capability("video decoder", 70)
90
138
    set_callbacks(InitVideoDec, EndVideoDec)
91
92
69
    add_submodule()
93
69
    add_shortcut("ffmpeghw")
94
69
    set_capability("video decoder", 10001)
95
138
    set_callbacks(InitVideoHwDec, EndVideoDec)
96
97
69
    add_submodule()
98
69
    add_shortcut("ffmpeg")
99
69
    set_capability("audio decoder", 70)
100
138
    set_callbacks(InitAudioDec, EndAudioDec)
101
102
69
    add_submodule()
103
69
    add_shortcut("ffmpeg")
104
69
    set_capability("spu decoder", 70)
105
138
    set_callbacks(InitSubtitleDec, EndSubtitleDec)
106
107
69
    add_bool( "avcodec-dr", true, DR_TEXT, NULL )
108
69
    add_bool( "avcodec-corrupted", true, CORRUPTED_TEXT, CORRUPTED_LONGTEXT )
109
69
    add_obsolete_integer ( "avcodec-error-resilience" ) /* removed since 4.0.0 */
110
69
    add_obsolete_integer ( "avcodec-workaround-bugs" ) /* removed since 4.0.0 */
111
69
    add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT )
112
69
    add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
113
69
        SKIP_FRAME_LONGTEXT )
114
69
        change_integer_list( frame_skip_list, frame_skip_list_text )
115
69
    add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
116
69
        SKIP_IDCT_LONGTEXT )
117
69
        change_integer_list( idct_skip_list, idct_skip_list_text )
118
69
    add_obsolete_integer( "avcodec-vismv" ) /* removed since 3.0.0 */
119
69
    add_obsolete_bool( "avcodec-fast" ) /* removed since 4.0.0 */
120
69
    add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
121
69
                  SKIPLOOPF_LONGTEXT)
122
69
        change_safe ()
123
69
        change_integer_list( nloopf_list, nloopf_list_text )
124
125
69
    add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT )
126
69
    add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT )
127
69
    add_obsolete_string( "avcodec-hw" ) /* removed since 4.0.0 */
128
69
    add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT )
129
69
    add_string( "avcodec-options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT )
130
131
132
69
#ifdef ENABLE_SOUT
133
    /* encoder submodule */
134
69
    add_submodule ()
135
69
    add_shortcut( "ffmpeg" )
136
69
    set_section( N_("Encoding") , NULL )
137
69
    set_description( N_("FFmpeg video encoder") )
138
69
    set_capability( "video encoder", 100 )
139
69
    set_callback( InitVideoEnc )
140
141
69
    add_submodule()
142
69
    set_description( N_("FFmpeg video encoder") )
143
69
    set_capability( "image encoder", 100 )
144
69
    set_callback( InitVideoEnc )
145
146
69
    add_submodule()
147
69
    add_shortcut( "ffmpeg" )
148
69
    set_section( N_("Encoding") , NULL )
149
69
    set_description( N_("FFmpeg audio encoder") )
150
69
    set_capability( "audio encoder", 100 )
151
69
    set_callback( InitVideoEnc )
152
153
69
    add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT )
154
69
    add_string( ENC_CFG_PREFIX "hq", "rd", ENC_HQ_TEXT,
155
69
                ENC_HQ_LONGTEXT )
156
69
        change_string_list( enc_hq_list, enc_hq_list_text )
157
69
    add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
158
69
                 ENC_KEYINT_LONGTEXT )
159
69
    add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
160
69
                 ENC_BFRAMES_LONGTEXT )
161
69
    add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
162
69
              ENC_HURRYUP_LONGTEXT )
163
69
    add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
164
69
              ENC_INTERLACE_LONGTEXT )
165
69
    add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
166
69
              ENC_INTERLACE_ME_LONGTEXT )
167
69
    add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
168
69
                 ENC_VT_LONGTEXT )
169
69
    add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
170
69
              ENC_PRE_ME_LONGTEXT )
171
69
    add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
172
69
                 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT )
173
69
    add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
174
69
               ENC_RC_BUF_AGGR_TEXT, NULL )
175
69
    add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
176
69
               ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT )
177
69
    add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
178
69
                 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT )
179
69
    add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
180
69
              ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT )
181
69
    add_integer( ENC_CFG_PREFIX "qmin", 0,
182
69
                 ENC_QMIN_TEXT, NULL )
183
69
    add_integer( ENC_CFG_PREFIX "qmax", 0,
184
69
                 ENC_QMAX_TEXT, NULL )
185
69
    add_bool( ENC_CFG_PREFIX "trellis", false,
186
69
              ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT )
187
69
    add_float( ENC_CFG_PREFIX "qscale", 3,
188
69
               ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT )
189
69
    add_integer( ENC_CFG_PREFIX "strict", 0,
190
69
                 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT )
191
69
        change_integer_range( -2, 2 )
192
69
    add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
193
69
               ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT )
194
69
    add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
195
69
               ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT )
196
69
    add_float( ENC_CFG_PREFIX "p-masking", 0.0,
197
69
               ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT )
198
69
    add_float( ENC_CFG_PREFIX "border-masking", 0.0,
199
69
               ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT )
200
69
    add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
201
69
                 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT )
202
69
    add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
203
69
                 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT )
204
205
    /* Audio AAC encoder profile */
206
69
    add_string( ENC_CFG_PREFIX "aac-profile", "low",
207
69
                ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT )
208
209
69
    add_string( ENC_CFG_PREFIX "options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT )
210
69
#endif /* ENABLE_SOUT */
211
212
#ifdef MERGE_FFMPEG
213
    add_submodule ()
214
# ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
215
#   include "../../demux/avformat/avformat.c"
216
# endif
217
    add_submodule ()
218
        AVIO_MODULE
219
    add_submodule ()
220
        AVPARSER_MODULE
221
#endif
222
69
vlc_module_end ()
223
224
AVCodecContext *ffmpeg_AllocContext( decoder_t *p_dec,
225
                                     const AVCodec **restrict codecp )
226
887k
{
227
887k
    enum AVCodecID i_codec_id;
228
887k
    const char *psz_namecodec;
229
887k
    const AVCodec *p_codec = NULL;
230
231
    /* *** determine codec type *** */
232
887k
    if( !GetFfmpegCodec( p_dec->fmt_in, &i_codec_id, &psz_namecodec ) ||
233
858k
         i_codec_id == AV_CODEC_ID_RAWVIDEO )
234
29.9k
         return NULL;
235
236
857k
    msg_Dbg( p_dec, "using %s %s", AVPROVIDER(LIBAVCODEC), LIBAVCODEC_IDENT );
237
238
    /* Initialization must be done before avcodec_find_decoder() */
239
857k
    vlc_init_avcodec(VLC_OBJECT(p_dec));
240
241
    /* *** ask ffmpeg for a decoder *** */
242
857k
    char *psz_decoder = var_InheritString( p_dec, "avcodec-codec" );
243
857k
    if( psz_decoder != NULL )
244
0
    {
245
0
        p_codec = avcodec_find_decoder_by_name( psz_decoder );
246
0
        if( !p_codec )
247
0
            msg_Err( p_dec, "Decoder `%s' not found", psz_decoder );
248
0
        else if( p_codec->id != i_codec_id )
249
0
        {
250
0
            msg_Err( p_dec, "Decoder `%s' can't handle %4.4s",
251
0
                    psz_decoder, (char*)&p_dec->fmt_in->i_codec );
252
0
            p_codec = NULL;
253
0
        }
254
0
        free( psz_decoder );
255
0
    }
256
857k
    if( !p_codec )
257
857k
        p_codec = avcodec_find_decoder( i_codec_id );
258
857k
    if( !p_codec )
259
365
    {
260
365
        msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
261
365
        return NULL;
262
365
    }
263
264
857k
    *codecp = p_codec;
265
266
    /* *** get a p_context *** */
267
857k
    AVCodecContext *avctx = avcodec_alloc_context3(p_codec);
268
857k
    if( unlikely(avctx == NULL) )
269
0
        return NULL;
270
271
857k
    avctx->debug = var_InheritInteger( p_dec, "avcodec-debug" );
272
857k
    avctx->opaque = p_dec;
273
857k
    return avctx;
274
857k
}
275
276
/*****************************************************************************
277
 * ffmpeg_OpenCodec:
278
 *****************************************************************************/
279
int ffmpeg_OpenCodec( decoder_t *p_dec, AVCodecContext *ctx,
280
                      const AVCodec *codec )
281
851k
{
282
851k
    char *psz_opts = var_InheritString( p_dec, "avcodec-options" );
283
851k
    AVDictionary *options = NULL;
284
851k
    int ret;
285
286
851k
    if (psz_opts) {
287
0
        vlc_av_get_options(psz_opts, &options);
288
0
        free(psz_opts);
289
0
    }
290
291
851k
    vlc_avcodec_lock();
292
851k
    ret = avcodec_open2( ctx, codec, options ? &options : NULL );
293
851k
    vlc_avcodec_unlock();
294
295
851k
    AVDictionaryEntry *t = NULL;
296
851k
    while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
297
0
        msg_Err( p_dec, "Unknown option \"%s\"", t->key );
298
0
    }
299
851k
    av_dict_free(&options);
300
301
851k
    if( ret < 0 )
302
68.7k
    {
303
68.7k
        msg_Err( p_dec, "cannot start codec (%s)", codec->name );
304
68.7k
        return VLC_EGENERIC;
305
68.7k
    }
306
307
782k
    msg_Dbg( p_dec, "codec (%s) started", codec->name );
308
782k
    return VLC_SUCCESS;
309
851k
}