1 /*****************************************************************************
2  * encoder.c: video and audio encoder using the libavcodec library
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
5  * $Id: 670d03d7ef3ba5bc1f89b925b93aa4a28e197b87 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  * Part of the file Copyright (C) FFmpeg Project Developers
11  * (mpeg4_default matrixes)
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27 
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34 
35 #include <math.h>
36 
37 #include <vlc_common.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_dialog.h>
42 #include <vlc_avcodec.h>
43 #include <vlc_cpu.h>
44 
45 #include <libavcodec/avcodec.h>
46 #include <libavutil/channel_layout.h>
47 
48 #include "avcodec.h"
49 #include "avcommon.h"
50 
51 #if LIBAVUTIL_VERSION_CHECK( 52,2,6,0,0 )
52 # include <libavutil/channel_layout.h>
53 #endif
54 
55 #define HURRY_UP_GUARD1 (450000)
56 #define HURRY_UP_GUARD2 (300000)
57 #define HURRY_UP_GUARD3 (100000)
58 
59 #define MAX_FRAME_DELAY (FF_MAX_B_FRAMES + 2)
60 
61 #define RAW_AUDIO_FRAME_SIZE (2048)
62 
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static block_t *EncodeVideo( encoder_t *, picture_t * );
67 static block_t *EncodeAudio( encoder_t *, block_t * );
68 
69 struct thread_context_t;
70 
71 /*****************************************************************************
72  * thread_context_t : for multithreaded encoding
73  *****************************************************************************/
74 struct thread_context_t
75 {
76     VLC_COMMON_MEMBERS
77 
78     AVCodecContext  *p_context;
79     int             (* pf_func)(AVCodecContext *c, void *arg);
80     void            *arg;
81     int             i_ret;
82 
83     vlc_mutex_t     lock;
84     vlc_cond_t      cond;
85     bool            b_work, b_done;
86 };
87 
88 /*****************************************************************************
89  * encoder_sys_t : libavcodec encoder descriptor
90  *****************************************************************************/
91 struct encoder_sys_t
92 {
93     /*
94      * libavcodec properties
95      */
96     AVCodec         *p_codec;
97     AVCodecContext  *p_context;
98 
99     /*
100      * Common buffer mainly for audio as frame size in there needs usually be constant
101      */
102     uint8_t *p_buffer;
103     size_t i_buffer_out;
104     uint8_t *p_interleave_buf;
105 
106     /*
107      * Video properties
108      */
109     mtime_t i_last_ref_pts;
110     mtime_t i_buggy_pts_detect;
111     mtime_t i_last_pts;
112     bool    b_inited;
113 
114     /*
115      * Audio properties
116      */
117     size_t i_sample_bytes;
118     size_t i_frame_size;
119     size_t i_samples_delay; //How much samples in delay buffer
120     bool b_planar;
121     bool b_variable;    //Encoder can be fed with any size frames not just frame_size
122     mtime_t i_pts;
123     date_t  buffer_date;
124 
125     /* Multichannel (>2) channel reordering */
126     uint8_t    i_channels_to_reorder;
127     uint8_t    pi_reorder_layout[AOUT_CHAN_MAX];
128 
129     /* Encoding settings */
130     int        i_key_int;
131     int        i_b_frames;
132     int        i_vtolerance;
133     int        i_qmin;
134     int        i_qmax;
135     int        i_hq;
136     int        i_rc_buffer_size;
137     float      f_rc_buffer_aggressivity;
138     bool       b_pre_me;
139     bool       b_hurry_up;
140     bool       b_interlace, b_interlace_me;
141     float      f_i_quant_factor;
142     bool       b_mpeg4_matrix;
143     bool       b_trellis;
144     int        i_quality; /* for VBR */
145     float      f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
146     int        i_aac_profile; /* AAC profile to use.*/
147 
148     AVFrame    *frame;
149 };
150 
151 
152 /* Taken from audio.c*/
153 static const uint64_t pi_channels_map[][2] =
154 {
155     { AV_CH_FRONT_LEFT,        AOUT_CHAN_LEFT },
156     { AV_CH_FRONT_RIGHT,       AOUT_CHAN_RIGHT },
157     { AV_CH_FRONT_CENTER,      AOUT_CHAN_CENTER },
158     { AV_CH_LOW_FREQUENCY,     AOUT_CHAN_LFE },
159     { AV_CH_BACK_LEFT,         AOUT_CHAN_REARLEFT },
160     { AV_CH_BACK_RIGHT,        AOUT_CHAN_REARRIGHT },
161     { AV_CH_FRONT_LEFT_OF_CENTER, 0 },
162     { AV_CH_FRONT_RIGHT_OF_CENTER, 0 },
163     { AV_CH_BACK_CENTER,       AOUT_CHAN_REARCENTER },
164     { AV_CH_SIDE_LEFT,         AOUT_CHAN_MIDDLELEFT },
165     { AV_CH_SIDE_RIGHT,        AOUT_CHAN_MIDDLERIGHT },
166     { AV_CH_TOP_CENTER,        0 },
167     { AV_CH_TOP_FRONT_LEFT,    0 },
168     { AV_CH_TOP_FRONT_CENTER,  0 },
169     { AV_CH_TOP_FRONT_RIGHT,   0 },
170     { AV_CH_TOP_BACK_LEFT,     0 },
171     { AV_CH_TOP_BACK_CENTER,   0 },
172     { AV_CH_TOP_BACK_RIGHT,    0 },
173     { AV_CH_STEREO_LEFT,       0 },
174     { AV_CH_STEREO_RIGHT,      0 },
175 };
176 
177 static const uint32_t channel_mask[][2] = {
178     {0,0},
179     {AOUT_CHAN_CENTER, AV_CH_LAYOUT_MONO},
180     {AOUT_CHANS_STEREO, AV_CH_LAYOUT_STEREO},
181     {AOUT_CHANS_2_1, AV_CH_LAYOUT_2POINT1},
182     {AOUT_CHANS_4_0, AV_CH_LAYOUT_4POINT0},
183     {AOUT_CHANS_4_1, AV_CH_LAYOUT_4POINT1},
184     {AOUT_CHANS_5_1, AV_CH_LAYOUT_5POINT1_BACK},
185     {AOUT_CHANS_7_0, AV_CH_LAYOUT_7POINT0},
186     {AOUT_CHANS_7_1, AV_CH_LAYOUT_7POINT1},
187     {AOUT_CHANS_8_1, AV_CH_LAYOUT_OCTAGONAL},
188 };
189 
190 static const char *const ppsz_enc_options[] = {
191     "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
192     "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
193     "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
194     "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
195     "p-masking", "border-masking",
196     "aac-profile", "options",
197     NULL
198 };
199 
200 static const uint16_t mpa_bitrate_tab[2][15] =
201 {
202     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
203     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
204 };
205 
206 static const uint16_t mpa_freq_tab[6] =
207 { 44100, 48000, 32000, 22050, 24000, 16000 };
208 
209 static const uint16_t mpeg4_default_intra_matrix[64] = {
210   8, 17, 18, 19, 21, 23, 25, 27,
211  17, 18, 19, 21, 23, 25, 27, 28,
212  20, 21, 22, 23, 24, 26, 28, 30,
213  21, 22, 23, 24, 26, 28, 30, 32,
214  22, 23, 24, 26, 28, 30, 32, 35,
215  23, 24, 26, 28, 30, 32, 35, 38,
216  25, 26, 28, 30, 32, 35, 38, 41,
217  27, 28, 30, 32, 35, 38, 41, 45,
218 };
219 
220 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
221  16, 17, 18, 19, 20, 21, 22, 23,
222  17, 18, 19, 20, 21, 22, 23, 24,
223  18, 19, 20, 21, 22, 23, 24, 25,
224  19, 20, 21, 22, 23, 24, 26, 27,
225  20, 21, 22, 23, 25, 26, 27, 28,
226  21, 22, 23, 24, 26, 27, 28, 30,
227  22, 23, 24, 26, 27, 28, 30, 31,
228  23, 24, 25, 27, 28, 30, 31, 33,
229 };
230 
231 static const int DEFAULT_ALIGN = 0;
232 
233 
234 /*****************************************************************************
235  * InitVideoEnc: probe the encoder
236  *****************************************************************************/
probe_video_frame_rate(encoder_t * p_enc,AVCodecContext * p_context,AVCodec * p_codec)237 static void probe_video_frame_rate( encoder_t *p_enc, AVCodecContext *p_context, AVCodec *p_codec )
238 {
239     /* if we don't have i_frame_rate_base, we are probing and just checking if we can find codec
240      * so set fps to requested fps if asked by user or input fps is availabled */
241     p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base ? p_enc->fmt_in.video.i_frame_rate_base : 1;
242 
243     // MP4V doesn't like CLOCK_FREQ denominator in time_base, so use 1/25 as default for that
244     p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate_base ? p_enc->fmt_in.video.i_frame_rate :
245                                   ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4V ? 25 : CLOCK_FREQ );
246 
247     msg_Dbg( p_enc, "Time base for probing set to %d/%d", p_context->time_base.num, p_context->time_base.den );
248     if( p_codec->supported_framerates )
249     {
250         /* We are finding fps values so 1/time_base */
251         AVRational target = {
252             .num = p_context->time_base.den,
253             .den = p_context->time_base.num
254         };
255         int idx = av_find_nearest_q_idx(target, p_codec->supported_framerates);
256 
257         p_context->time_base.num = p_codec->supported_framerates[idx].den ?
258                                     p_codec->supported_framerates[idx].den : 1;
259         p_context->time_base.den = p_codec->supported_framerates[idx].den ?
260                                     p_codec->supported_framerates[idx].num : CLOCK_FREQ;
261 
262         /* If we have something reasonable on supported framerates, use that*/
263         if( p_context->time_base.den && p_context->time_base.den < CLOCK_FREQ )
264         {
265             p_enc->fmt_out.video.i_frame_rate_base =
266                 p_enc->fmt_in.video.i_frame_rate_base =
267                 p_context->time_base.num;
268             p_enc->fmt_out.video.i_frame_rate =
269                 p_enc->fmt_in.video.i_frame_rate =
270                 p_context->time_base.den;
271         }
272     }
273     msg_Dbg( p_enc, "Time base set to %d/%d", p_context->time_base.num, p_context->time_base.den );
274 }
275 
add_av_option_int(encoder_t * p_enc,AVDictionary ** pp_dict,const char * psz_name,int i_value)276 static void add_av_option_int( encoder_t *p_enc, AVDictionary** pp_dict, const char* psz_name, int i_value )
277 {
278     char buff[32];
279     if ( snprintf( buff, sizeof(buff), "%d", i_value ) < 0 )
280         return;
281     if( av_dict_set( pp_dict, psz_name, buff, 0 ) < 0 )
282         msg_Warn( p_enc, "Failed to set encoder option %s", psz_name );
283 }
284 
add_av_option_float(encoder_t * p_enc,AVDictionary ** pp_dict,const char * psz_name,float f_value)285 static void add_av_option_float( encoder_t *p_enc, AVDictionary** pp_dict, const char* psz_name, float f_value )
286 {
287     char buff[128];
288     if ( snprintf( buff, sizeof(buff), "%f", f_value ) < 0 )
289         return;
290     if( av_dict_set( pp_dict, psz_name, buff, 0 ) < 0 )
291         msg_Warn( p_enc, "Failed to set encoder option %s", psz_name );
292 }
293 
InitVideoEnc(vlc_object_t * p_this)294 int InitVideoEnc( vlc_object_t *p_this )
295 {
296     encoder_t *p_enc = (encoder_t *)p_this;
297     encoder_sys_t *p_sys;
298     AVCodecContext *p_context;
299     AVCodec *p_codec = NULL;
300     unsigned i_codec_id;
301     const char *psz_namecodec;
302     float f_val;
303     char *psz_val;
304 
305     msg_Dbg( p_this, "using %s %s", AVPROVIDER(LIBAVCODEC), LIBAVCODEC_IDENT );
306 
307     /* Initialization must be done before avcodec_find_encoder() */
308     vlc_init_avcodec(p_this);
309 
310     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
311 
312     switch( p_enc->fmt_out.i_cat )
313     {
314         case VIDEO_ES:
315             if( p_enc->fmt_out.i_codec == VLC_CODEC_MP1V )
316             {
317                 i_codec_id = AV_CODEC_ID_MPEG1VIDEO;
318                 psz_namecodec = "MPEG-1 video";
319                 break;
320             }
321             if( GetFfmpegCodec( VIDEO_ES, p_enc->fmt_out.i_codec, &i_codec_id,
322                                 &psz_namecodec ) )
323                 break;
324             if( FindFfmpegChroma( p_enc->fmt_out.i_codec ) != AV_PIX_FMT_NONE )
325             {
326                 i_codec_id = AV_CODEC_ID_RAWVIDEO;
327                 psz_namecodec = "Raw video";
328                 break;
329             }
330             return VLC_EGENERIC;
331 
332         case AUDIO_ES:
333             if( GetFfmpegCodec( AUDIO_ES, p_enc->fmt_out.i_codec, &i_codec_id,
334                                 &psz_namecodec ) )
335                 break;
336             /* fall through */
337         default:
338             /* We don't support subtitle encoding */
339             return VLC_EGENERIC;
340     }
341 
342     char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
343     if( psz_encoder && *psz_encoder )
344     {
345         p_codec = avcodec_find_encoder_by_name( psz_encoder );
346         if( !p_codec )
347         {
348             msg_Err( p_this, "Encoder `%s' not found", psz_encoder );
349             free( psz_encoder );
350             return VLC_EGENERIC;
351         }
352         else if( p_codec->id != i_codec_id )
353         {
354             msg_Err( p_this, "Encoder `%s' can't handle %4.4s",
355                     psz_encoder, (char*)&p_enc->fmt_out.i_codec );
356             free( psz_encoder );
357             return VLC_EGENERIC;
358         }
359     }
360     free( psz_encoder );
361     if( !p_codec )
362         p_codec = avcodec_find_encoder( i_codec_id );
363     if( !p_codec )
364     {
365         msg_Err( p_enc, "cannot find encoder %s\n"
366 "*** Your Libav/FFmpeg installation is crippled.   ***\n"
367 "*** Please check with your Libav/FFmpeg packager. ***\n"
368 "*** This is NOT a VLC media player issue.   ***", psz_namecodec );
369 
370 #if !defined(_WIN32)
371         vlc_dialog_display_error( p_enc, _("Streaming / Transcoding failed"), _(
372 /* I have had enough of all these MPEG-3 transcoding bug reports.
373  * Downstream packager, you had better not patch this out, or I will be really
374  * annoyed. Think about it - you don't want to fork the VLC translation files,
375  * do you? -- Courmisch, 2008-10-22 */
376 "It seems your Libav/FFmpeg (libavcodec) installation lacks the following encoder:\n"
377 "%s.\n"
378 "If you don't know how to fix this, ask for support from your distribution.\n"
379 "\n"
380 "This is not an error inside VLC media player.\n"
381 "Do not contact the VideoLAN project about this issue.\n"),
382             psz_namecodec );
383 #endif
384 
385         return VLC_EGENERIC;
386     }
387 
388     /* Allocate the memory needed to store the encoder's structure */
389     if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
390         return VLC_ENOMEM;
391     p_enc->p_sys = p_sys;
392     p_sys->i_samples_delay = 0;
393     p_sys->p_codec = p_codec;
394     p_sys->b_planar = false;
395 
396     p_sys->p_buffer = NULL;
397     p_sys->p_interleave_buf = NULL;
398     p_sys->i_buffer_out = 0;
399 
400     p_context = avcodec_alloc_context3(p_codec);
401     if( unlikely(p_context == NULL) )
402     {
403         free( p_sys );
404         return VLC_ENOMEM;
405     }
406     p_sys->p_context = p_context;
407     p_sys->p_context->codec_id = p_sys->p_codec->id;
408     p_context->thread_type = 0;
409     p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
410     p_context->opaque = (void *)p_this;
411 
412     p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
413     p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
414     p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
415     p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
416     p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
417     p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
418     p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
419 
420     p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
421     p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
422     p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
423     p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
424 
425     f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
426 
427     p_sys->i_quality = 0;
428     if( f_val < .01f || f_val > 255.f )
429         f_val = 0.f;
430     else
431         p_sys->i_quality = lroundf(FF_QP2LAMBDA * f_val);
432 
433     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
434     p_sys->i_hq = FF_MB_DECISION_RD;
435     if( psz_val && *psz_val )
436     {
437         if( !strcmp( psz_val, "rd" ) )
438             p_sys->i_hq = FF_MB_DECISION_RD;
439         else if( !strcmp( psz_val, "bits" ) )
440             p_sys->i_hq = FF_MB_DECISION_BITS;
441         else if( !strcmp( psz_val, "simple" ) )
442             p_sys->i_hq = FF_MB_DECISION_SIMPLE;
443         else
444             p_sys->i_hq = FF_MB_DECISION_RD;
445     }
446     else
447         p_sys->i_hq = FF_MB_DECISION_RD;
448     free( psz_val );
449 
450     p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
451     p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
452     p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
453 
454     p_context->strict_std_compliance = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
455 
456     p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
457     p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
458     p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
459     p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
460 
461     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
462     /* libavcodec uses faac encoder atm, and it has issues with
463      * other than low-complexity profile, so default to that */
464     p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
465     if( psz_val && *psz_val )
466     {
467         if( !strncmp( psz_val, "main", 4 ) )
468             p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
469         else if( !strncmp( psz_val, "low", 3 ) )
470             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
471         else if( !strncmp( psz_val, "ssr", 3 ) )
472             p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
473         else if( !strncmp( psz_val, "ltp", 3 ) )
474             p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
475 /* These require libavcodec with libfdk-aac */
476         else if( !strncmp( psz_val, "hev2", 4 ) )
477             p_sys->i_aac_profile = FF_PROFILE_AAC_HE_V2;
478         else if( !strncmp( psz_val, "hev1", 4 ) )
479             p_sys->i_aac_profile = FF_PROFILE_AAC_HE;
480         else if( !strncmp( psz_val, "ld", 2 ) )
481             p_sys->i_aac_profile = FF_PROFILE_AAC_LD;
482         else if( !strncmp( psz_val, "eld", 3 ) )
483             p_sys->i_aac_profile = FF_PROFILE_AAC_ELD;
484         else
485         {
486             msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
487             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
488         }
489     }
490     free( psz_val );
491     AVDictionary *options = NULL;
492 
493     if( p_enc->fmt_in.i_cat == VIDEO_ES )
494     {
495         if( !p_enc->fmt_in.video.i_visible_width || !p_enc->fmt_in.video.i_visible_height )
496         {
497             msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_visible_width,
498                       p_enc->fmt_in.video.i_visible_height );
499             avcodec_free_context( &p_context );
500             free( p_sys );
501             return VLC_EGENERIC;
502         }
503 
504         p_context->codec_type = AVMEDIA_TYPE_VIDEO;
505 
506         p_context->width = p_enc->fmt_in.video.i_visible_width;
507         p_context->height = p_enc->fmt_in.video.i_visible_height;
508 
509         probe_video_frame_rate( p_enc, p_context, p_codec );
510         set_video_color_settings( &p_enc->fmt_in.video, p_context );
511 
512         /* Defaults from ffmpeg.c */
513         p_context->qblur = 0.5;
514         p_context->qcompress = 0.5;
515         p_context->b_quant_offset = 1.25;
516         p_context->b_quant_factor = 1.25;
517         p_context->i_quant_offset = 0.0;
518         p_context->i_quant_factor = -0.8;
519 
520         p_context->lumi_masking = p_sys->f_lumi_masking;
521         p_context->dark_masking = p_sys->f_dark_masking;
522         p_context->p_masking = p_sys->f_p_masking;
523         add_av_option_float( p_enc, &options, "border_mask", p_sys->f_border_masking );
524 
525         if( p_sys->i_key_int > 0 )
526             p_context->gop_size = p_sys->i_key_int;
527         p_context->max_b_frames =
528             VLC_CLIP( p_sys->i_b_frames, 0, FF_MAX_B_FRAMES );
529         if( !p_context->max_b_frames  &&
530             (  p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
531                p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ) )
532             p_context->flags |= AV_CODEC_FLAG_LOW_DELAY;
533 
534         av_reduce( &p_context->sample_aspect_ratio.num,
535                    &p_context->sample_aspect_ratio.den,
536                    p_enc->fmt_in.video.i_sar_num,
537                    p_enc->fmt_in.video.i_sar_den, 1 << 30 );
538 
539 
540         p_enc->fmt_in.i_codec = VLC_CODEC_I420;
541 
542         /* Very few application support YUV in TIFF, not even VLC */
543         if( p_enc->fmt_out.i_codec == VLC_CODEC_TIFF )
544             p_enc->fmt_in.i_codec = VLC_CODEC_RGB24;
545 
546         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
547         GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
548 
549         if( p_codec->pix_fmts )
550         {
551             static const enum AVPixelFormat vlc_pix_fmts[] = {
552                 AV_PIX_FMT_YUV420P,
553                 AV_PIX_FMT_NV12,
554                 AV_PIX_FMT_RGB24,
555             };
556             bool found = false;
557             const enum PixelFormat *p = p_codec->pix_fmts;
558             for( ; !found && *p != -1; p++ )
559             {
560                 for( size_t i = 0; i < ARRAY_SIZE(vlc_pix_fmts); ++i )
561                 {
562                     if( *p == vlc_pix_fmts[i] )
563                     {
564                         found = true;
565                         p_context->pix_fmt = *p;
566                         break;
567                     }
568                 }
569             }
570             if (!found) p_context->pix_fmt = p_codec->pix_fmts[0];
571             GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
572             p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
573         }
574 
575 
576         if ( p_sys->f_i_quant_factor != 0.f )
577             p_context->i_quant_factor = p_sys->f_i_quant_factor;
578 
579         int nr = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
580         add_av_option_int( p_enc, &options, "noise_reduction", nr );
581 
582         if ( p_sys->b_mpeg4_matrix )
583         {
584             p_context->intra_matrix = mpeg4_default_intra_matrix;
585             p_context->inter_matrix = mpeg4_default_non_intra_matrix;
586         }
587 
588         if ( p_sys->b_pre_me )
589         {
590             add_av_option_int( p_enc, &options, "mepre", 1 );
591             p_context->me_pre_cmp = FF_CMP_CHROMA;
592         }
593 
594         if ( p_sys->b_interlace )
595         {
596             if ( p_context->height <= 280 )
597             {
598                 if ( p_context->height != 16 || p_context->width != 16 )
599                     msg_Warn( p_enc,
600                         "disabling interlaced video because height=%d <= 280",
601                         p_context->height );
602             }
603             else
604             {
605                 p_context->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
606                 if ( p_sys->b_interlace_me )
607                     p_context->flags |= AV_CODEC_FLAG_INTERLACED_ME;
608             }
609         }
610 
611         p_context->trellis = p_sys->b_trellis;
612 
613         if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
614             p_context->flags |= AV_CODEC_FLAG_QSCALE;
615         /* These codecs cause libavcodec to exit if thread_count is > 1.
616            See libavcodec/mpegvideo_enc.c:MPV_encode_init and
617            libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
618          */
619         if ( i_codec_id == AV_CODEC_ID_FLV1 ||
620              i_codec_id == AV_CODEC_ID_H261 ||
621              i_codec_id == AV_CODEC_ID_LJPEG ||
622              i_codec_id == AV_CODEC_ID_MJPEG ||
623              i_codec_id == AV_CODEC_ID_H263 ||
624              i_codec_id == AV_CODEC_ID_H263P ||
625              i_codec_id == AV_CODEC_ID_MSMPEG4V1 ||
626              i_codec_id == AV_CODEC_ID_MSMPEG4V2 ||
627              i_codec_id == AV_CODEC_ID_MSMPEG4V3 ||
628              i_codec_id == AV_CODEC_ID_WMV1 ||
629              i_codec_id == AV_CODEC_ID_WMV2 ||
630              i_codec_id == AV_CODEC_ID_RV10 ||
631              i_codec_id == AV_CODEC_ID_RV20 ||
632              i_codec_id == AV_CODEC_ID_SVQ3 )
633             p_enc->i_threads = 1;
634 
635         if( p_sys->i_vtolerance > 0 )
636             p_context->bit_rate_tolerance = p_sys->i_vtolerance;
637 
638         /* usually if someone sets bitrate, he likes more to get that bitrate
639          * over quality should help 'normal' user to get asked bitrate
640          */
641         if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
642         {
643             p_sys->i_qmax = 51;
644             p_sys->i_qmin = 3;
645         }
646 
647         if( p_sys->i_qmin > 0 )
648         {
649             p_context->qmin = p_sys->i_qmin;
650             p_context->mb_lmin = p_sys->i_qmin * FF_QP2LAMBDA;
651             add_av_option_int( p_enc, &options, "lmin", p_context->mb_lmin);
652         }
653         if( p_sys->i_qmax > 0 )
654         {
655             p_context->qmax = p_sys->i_qmax;
656             p_context->mb_lmax = p_sys->i_qmax * FF_QP2LAMBDA;
657             add_av_option_int( p_enc, &options, "lmax", p_context->mb_lmax);
658         }
659         p_context->max_qdiff = 3;
660 
661         p_context->mb_decision = p_sys->i_hq;
662 
663         if( p_sys->i_quality && !p_enc->fmt_out.i_bitrate )
664         {
665             p_context->flags |= AV_CODEC_FLAG_QSCALE;
666             p_context->global_quality = p_sys->i_quality;
667         }
668         else
669         {
670             av_dict_set(&options, "qsquish", "1.0", 0);
671             /* Default to 1/2 second buffer for given bitrate unless defined otherwise*/
672             if( !p_sys->i_rc_buffer_size )
673             {
674                 p_sys->i_rc_buffer_size = p_enc->fmt_out.i_bitrate * 8 / 2;
675             }
676             msg_Dbg( p_enc, "rc buffer size %d bits", p_sys->i_rc_buffer_size );
677             /* Set maxrate/minrate to bitrate to try to get CBR */
678             p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
679             p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
680             p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
681             /* This is from ffmpeg's ffmpeg.c : */
682             p_context->rc_initial_buffer_occupancy
683                 = p_sys->i_rc_buffer_size * 3/4;
684             add_av_option_float( p_enc, &options, "rc_buffer_aggressivity", p_sys->f_rc_buffer_aggressivity );
685         }
686     }
687     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
688     {
689         p_context->codec_type  = AVMEDIA_TYPE_AUDIO;
690         p_context->sample_fmt  = p_codec->sample_fmts ?
691                                     p_codec->sample_fmts[0] :
692                                     AV_SAMPLE_FMT_S16;
693 
694         /* Try to match avcodec input format to vlc format so we could avoid one
695            format conversion */
696         if( GetVlcAudioFormat( p_context->sample_fmt ) != p_enc->fmt_in.i_codec
697             && p_codec->sample_fmts )
698         {
699             msg_Dbg( p_enc, "Trying to find more suitable sample format instead of %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
700             for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
701             {
702                 if( GetVlcAudioFormat( p_codec->sample_fmts[i] ) == p_enc->fmt_in.i_codec )
703                 {
704                     p_context->sample_fmt = p_codec->sample_fmts[i];
705                     msg_Dbg( p_enc, "Using %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
706                     break;
707                 }
708             }
709         }
710         p_sys->b_planar = av_sample_fmt_is_planar( p_context->sample_fmt );
711         // Try if we can use interleaved format for codec input as VLC doesn't really do planar audio yet
712         // FIXME: Remove when planar/interleaved audio in vlc is equally supported
713         if( p_sys->b_planar && p_codec->sample_fmts )
714         {
715             msg_Dbg( p_enc, "Trying to find packet sample format instead of planar %s", av_get_sample_fmt_name( p_context->sample_fmt ) );
716             for( unsigned int i=0; p_codec->sample_fmts[i] != -1; i++ )
717             {
718                 if( !av_sample_fmt_is_planar( p_codec->sample_fmts[i] ) )
719                 {
720                     p_context->sample_fmt = p_codec->sample_fmts[i];
721                     msg_Dbg( p_enc, "Changing to packet format %s as new sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
722                     break;
723                 }
724             }
725         }
726         msg_Dbg( p_enc, "Ended up using %s as sample format", av_get_sample_fmt_name( p_context->sample_fmt ) );
727         p_enc->fmt_in.i_codec  = GetVlcAudioFormat( p_context->sample_fmt );
728         p_sys->b_planar = av_sample_fmt_is_planar( p_context->sample_fmt );
729 
730         p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
731         date_Init( &p_sys->buffer_date, p_enc->fmt_out.audio.i_rate, 1 );
732         date_Set( &p_sys->buffer_date, AV_NOPTS_VALUE );
733         p_context->time_base.num = 1;
734         p_context->time_base.den = p_context->sample_rate;
735         p_context->channels      = p_enc->fmt_out.audio.i_channels;
736 #if LIBAVUTIL_VERSION_CHECK( 52, 2, 6, 0, 0)
737         p_context->channel_layout = channel_mask[p_context->channels][1];
738 
739         /* Setup Channel ordering for multichannel audio
740          * as VLC channel order isn't same as libavcodec expects
741          */
742 
743         p_sys->i_channels_to_reorder = 0;
744 
745         /* Specified order
746          * Copied from audio.c
747          */
748         const unsigned i_order_max = 8 * sizeof(p_context->channel_layout);
749         uint32_t pi_order_dst[AOUT_CHAN_MAX] = { };
750         uint32_t order_mask = 0;
751         int i_channels_src = 0;
752 
753         if( p_context->channel_layout )
754         {
755             msg_Dbg( p_enc, "Creating channel order for reordering");
756             for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
757             {
758                 if( p_context->channel_layout & pi_channels_map[i][0] )
759                 {
760                     msg_Dbg( p_enc, "%d %"PRIx64" mapped to %"PRIx64"", i_channels_src, pi_channels_map[i][0], pi_channels_map[i][1]);
761                     pi_order_dst[i_channels_src++] = pi_channels_map[i][1];
762                     order_mask |= pi_channels_map[i][1];
763                 }
764             }
765         }
766         else
767         {
768             msg_Dbg( p_enc, "Creating default channel order for reordering");
769             /* Create default order  */
770             for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
771             {
772                 if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
773                 {
774                     msg_Dbg( p_enc, "%d channel is %"PRIx64"", i_channels_src, pi_channels_map[i][1]);
775                     pi_order_dst[i_channels_src++] = pi_channels_map[i][1];
776                     order_mask |= pi_channels_map[i][1];
777                 }
778             }
779         }
780         if( i_channels_src != p_context->channels )
781             msg_Err( p_enc, "Channel layout not understood" );
782 
783         p_sys->i_channels_to_reorder =
784             aout_CheckChannelReorder( NULL, pi_order_dst, order_mask,
785                                       p_sys->pi_reorder_layout );
786 #endif
787 
788         if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
789         {
790             /* XXX: FAAC does resample only when setting the INPUT samplerate
791              * to the desired value (-R option of the faac frontend)
792             p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
793             /* vlc should default to low-complexity profile, faac encoder
794              * has bug and aac audio has issues otherwise atm */
795             p_context->profile = p_sys->i_aac_profile;
796         }
797     }
798 
799     /* Misc parameters */
800     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
801 
802     /* Set reasonable defaults to VP8, based on
803        libvpx-720p preset from libvpx ffmpeg-patch */
804     if( i_codec_id == AV_CODEC_ID_VP8 )
805     {
806         /* Lets give bitrate tolerance */
807         p_context->bit_rate_tolerance = __MAX(2 * (int)p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
808         /* default to 120 frames between keyframe */
809         if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
810             p_context->gop_size = 120;
811         /* Don't set rc-values atm, they were from time before
812            libvpx was officially in FFmpeg */
813         //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
814         //p_context->rc_min_rate = 40 * 1000; // 40k
815         /* seems that FFmpeg presets have 720p as divider for buffers */
816         if( p_enc->fmt_out.video.i_visible_height >= 720 )
817         {
818             /* Check that we don't overrun users qmin/qmax values */
819             if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
820             {
821                 p_context->qmin = 10;
822                 p_context->mb_lmin = 10 * FF_QP2LAMBDA;
823                 add_av_option_int( p_enc, &options, "lmin", p_context->mb_lmin );
824             }
825 
826             if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
827             {
828                 p_context->qmax = 42;
829                 p_context->mb_lmax = 42 * FF_QP2LAMBDA;
830                 add_av_option_int( p_enc, &options, "lmax", p_context->mb_lmax );
831             }
832 
833         } else if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
834         {
835                 p_context->qmin = 1;
836                 p_context->mb_lmin = FF_QP2LAMBDA;
837                 add_av_option_int( p_enc, &options, "lmin", p_context->mb_lmin );
838         }
839 
840 
841 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
842         p_context->lag = 16;
843         p_context->level = 216;
844         p_context->profile = 0;
845         p_context->rc_buffer_aggressivity = 0.95;
846         p_context->token_partitions = 4;
847         p_context->mb_static_threshold = 0;
848 #endif
849     }
850 
851     if( i_codec_id == AV_CODEC_ID_RAWVIDEO )
852     {
853         /* XXX: hack: Force same codec (will be handled by transcode) */
854         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
855         GetFfmpegChroma( &p_context->pix_fmt, &p_enc->fmt_in.video );
856     }
857 
858     /* Make sure we get extradata filled by the encoder */
859     p_context->extradata_size = 0;
860     p_context->extradata = NULL;
861     p_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
862 
863     if( p_enc->i_threads >= 1)
864         p_context->thread_count = p_enc->i_threads;
865     else
866         p_context->thread_count = vlc_GetCPUCount();
867 
868     int ret;
869     char *psz_opts = var_InheritString(p_enc, ENC_CFG_PREFIX "options");
870     if (psz_opts) {
871         vlc_av_get_options(psz_opts, &options);
872         free(psz_opts);
873     }
874 
875     vlc_avcodec_lock();
876     ret = avcodec_open2( p_context, p_codec, options ? &options : NULL );
877     vlc_avcodec_unlock();
878 
879     AVDictionaryEntry *t = NULL;
880     while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
881         msg_Err(p_enc, "Unknown option \"%s\"", t->key);
882     }
883 
884     if( ret )
885     {
886         if( p_enc->fmt_in.i_cat != AUDIO_ES ||
887                 (p_context->channels <= 2 && i_codec_id != AV_CODEC_ID_MP2
888                  && i_codec_id != AV_CODEC_ID_MP3) )
889 errmsg:
890         {
891             static const char types[][12] = {
892                 [UNKNOWN_ES] = N_("unknown"), [VIDEO_ES] = N_("video"),
893                 [AUDIO_ES] = N_("audio"), [SPU_ES] = N_("subpicture"),
894             };
895             const char *type = types[0];
896             union
897             {
898                 vlc_fourcc_t value;
899                 char txt[4];
900             } fcc = { .value = p_enc->fmt_out.i_codec };
901 
902             if (likely((unsigned)p_enc->fmt_in.i_cat < sizeof (types) / sizeof (types[0])))
903                 type = types[p_enc->fmt_in.i_cat];
904             msg_Err( p_enc, "cannot open %4.4s %s encoder", fcc.txt, type );
905             vlc_dialog_display_error( p_enc, _("Streaming / Transcoding failed"),
906                 _("VLC could not open the %4.4s %s encoder."),
907                 fcc.txt, vlc_gettext(type) );
908             av_dict_free(&options);
909             goto error;
910         }
911 
912         if( p_context->channels > 2 )
913         {
914             p_context->channels = 2;
915             p_context->channel_layout = channel_mask[p_context->channels][1];
916 
917             /* Change fmt_in in order to ask for a channels conversion */
918             p_enc->fmt_in.audio.i_channels =
919             p_enc->fmt_out.audio.i_channels = 2;
920             p_enc->fmt_in.audio.i_physical_channels =
921             p_enc->fmt_out.audio.i_physical_channels = AOUT_CHANS_STEREO;
922             p_sys->i_channels_to_reorder = 0;
923             msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
924         }
925 
926         if( i_codec_id == AV_CODEC_ID_MP2 || i_codec_id == AV_CODEC_ID_MP3 )
927         {
928             int i_frequency, i;
929             es_format_t *fmt = &p_enc->fmt_out;
930 
931             for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
932                 if ( fmt->audio.i_rate == mpa_freq_tab[i_frequency] )
933                     break;
934 
935             if ( i_frequency == 6 )
936             {
937                 msg_Warn( p_enc, "MPEG audio doesn't support frequency=%d",
938                         fmt->audio.i_rate );
939                 /* Fallback to 44100 hz */
940                 p_context->sample_rate = p_enc->fmt_in.audio.i_rate =
941                 p_enc->fmt_out.audio.i_rate = 44100;
942             }
943 
944             for ( i = 1; i < 14; i++ )
945                 if (fmt->i_bitrate/1000 <= mpa_bitrate_tab[i_frequency / 3][i])
946                     break;
947 
948             if (fmt->i_bitrate / 1000 != mpa_bitrate_tab[i_frequency / 3][i])
949             {
950                 msg_Warn( p_enc,
951                         "MPEG audio doesn't support bitrate=%d, using %d",
952                         fmt->i_bitrate,
953                         mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
954                 fmt->i_bitrate = mpa_bitrate_tab[i_frequency / 3][i] * 1000;
955                 p_context->bit_rate = fmt->i_bitrate;
956             }
957         }
958 
959         p_context->codec = NULL;
960         vlc_avcodec_lock();
961         ret = avcodec_open2( p_context, p_codec, options ? &options : NULL );
962         vlc_avcodec_unlock();
963         if( ret )
964             goto errmsg;
965     }
966 
967     av_dict_free(&options);
968 
969     if( i_codec_id == AV_CODEC_ID_FLAC )
970     {
971         p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
972         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
973         if( p_enc->fmt_out.p_extra )
974         {
975             uint8_t *p = p_enc->fmt_out.p_extra;
976             p[0] = 0x66;    /* f */
977             p[1] = 0x4C;    /* L */
978             p[2] = 0x61;    /* a */
979             p[3] = 0x43;    /* C */
980             p[4] = 0x80;    /* streaminfo block, last block before audio */
981             p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
982             p[6] = ( p_context->extradata_size >>  8 ) & 0xff;
983             p[7] = ( p_context->extradata_size       ) & 0xff;
984             memcpy( &p[8], p_context->extradata, p_context->extradata_size );
985         }
986         else
987         {
988             p_enc->fmt_out.i_extra = 0;
989         }
990     }
991     else
992     {
993         p_enc->fmt_out.i_extra = p_context->extradata_size;
994         if( p_enc->fmt_out.i_extra )
995         {
996             p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
997             if ( p_enc->fmt_out.p_extra == NULL )
998             {
999                 goto error;
1000             }
1001             memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
1002                     p_enc->fmt_out.i_extra );
1003         }
1004     }
1005 
1006     p_context->flags &= ~AV_CODEC_FLAG_GLOBAL_HEADER;
1007 
1008     if( p_enc->fmt_in.i_cat == AUDIO_ES )
1009     {
1010         p_enc->fmt_in.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
1011         p_enc->fmt_in.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_in.i_codec );
1012 
1013         p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8);
1014         p_sys->i_frame_size = p_context->frame_size > 1 ?
1015                                     p_context->frame_size :
1016                                     AV_INPUT_BUFFER_MIN_SIZE;
1017         p_sys->i_buffer_out = av_samples_get_buffer_size(NULL,
1018                 p_sys->p_context->channels, p_sys->i_frame_size,
1019                 p_sys->p_context->sample_fmt, DEFAULT_ALIGN);
1020         p_sys->p_buffer = av_malloc( p_sys->i_buffer_out );
1021         if ( unlikely( p_sys->p_buffer == NULL ) )
1022         {
1023             goto error;
1024         }
1025         p_enc->fmt_out.audio.i_frame_length = p_context->frame_size;
1026         p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
1027         p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( p_enc->fmt_out.i_codec );
1028         //b_variable tells if we can feed any size frames to encoder
1029         p_sys->b_variable = p_context->frame_size ? false : true;
1030 
1031 
1032         if( p_sys->b_planar )
1033         {
1034             p_sys->p_interleave_buf = av_malloc( p_sys->i_buffer_out );
1035             if( unlikely( p_sys->p_interleave_buf == NULL ) )
1036                 goto error;
1037         }
1038     }
1039 
1040     p_sys->frame = av_frame_alloc();
1041     if( !p_sys->frame )
1042     {
1043         goto error;
1044     }
1045     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
1046 
1047     p_enc->pf_encode_video = EncodeVideo;
1048     p_enc->pf_encode_audio = EncodeAudio;
1049 
1050 
1051     return VLC_SUCCESS;
1052 error:
1053     free( p_enc->fmt_out.p_extra );
1054     av_free( p_sys->p_buffer );
1055     av_free( p_sys->p_interleave_buf );
1056     avcodec_free_context( &p_context );
1057     free( p_sys );
1058     return VLC_ENOMEM;
1059 }
1060 
1061 typedef struct
1062 {
1063     block_t self;
1064     AVPacket packet;
1065 } vlc_av_packet_t;
1066 
vlc_av_packet_Release(block_t * block)1067 static void vlc_av_packet_Release(block_t *block)
1068 {
1069     vlc_av_packet_t *b = (void *) block;
1070 
1071     av_packet_unref(&b->packet);
1072     free(b);
1073 }
1074 
vlc_av_packet_Wrap(AVPacket * packet,mtime_t i_length,AVCodecContext * context)1075 static block_t *vlc_av_packet_Wrap(AVPacket *packet, mtime_t i_length, AVCodecContext *context )
1076 {
1077     if ( packet->data == NULL &&
1078          packet->flags == 0 &&
1079          packet->pts == AV_NOPTS_VALUE &&
1080          packet->dts == AV_NOPTS_VALUE )
1081         return NULL; /* totally empty AVPacket */
1082 
1083     vlc_av_packet_t *b = malloc( sizeof( *b ) );
1084     if( unlikely(b == NULL) )
1085         return NULL;
1086 
1087     block_t *p_block = &b->self;
1088 
1089     block_Init( p_block, packet->data, packet->size );
1090     p_block->i_nb_samples = 0;
1091     p_block->pf_release = vlc_av_packet_Release;
1092     b->packet = *packet;
1093 
1094     p_block->i_length = i_length;
1095     p_block->i_pts = packet->pts;
1096     p_block->i_dts = packet->dts;
1097     if( unlikely( packet->flags & AV_PKT_FLAG_CORRUPT ) )
1098         p_block->i_flags |= BLOCK_FLAG_CORRUPTED;
1099     if( packet->flags & AV_PKT_FLAG_KEY )
1100         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1101     p_block->i_pts = p_block->i_pts * CLOCK_FREQ * context->time_base.num / context->time_base.den;
1102     p_block->i_dts = p_block->i_dts * CLOCK_FREQ * context->time_base.num / context->time_base.den;
1103 
1104     return p_block;
1105 }
1106 
check_hurry_up(encoder_sys_t * p_sys,AVFrame * frame,encoder_t * p_enc)1107 static void check_hurry_up( encoder_sys_t *p_sys, AVFrame *frame, encoder_t *p_enc )
1108 {
1109     mtime_t current_date = mdate();
1110 
1111     if ( current_date + HURRY_UP_GUARD3 > frame->pts )
1112     {
1113         p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
1114         p_sys->p_context->trellis = 0;
1115         msg_Dbg( p_enc, "hurry up mode 3" );
1116     }
1117     else
1118     {
1119         p_sys->p_context->mb_decision = p_sys->i_hq;
1120 
1121         if ( current_date + HURRY_UP_GUARD2 > frame->pts )
1122         {
1123             p_sys->p_context->trellis = 0;
1124             msg_Dbg( p_enc, "hurry up mode 2" );
1125         }
1126         else
1127         {
1128             p_sys->p_context->trellis = p_sys->b_trellis;
1129         }
1130     }
1131 
1132     if ( current_date + HURRY_UP_GUARD1 > frame->pts )
1133     {
1134         frame->pict_type = AV_PICTURE_TYPE_P;
1135         /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
1136     }
1137 }
1138 
encode_avframe(encoder_t * p_enc,encoder_sys_t * p_sys,AVFrame * frame)1139 static block_t *encode_avframe( encoder_t *p_enc, encoder_sys_t *p_sys, AVFrame *frame )
1140 {
1141     AVPacket av_pkt;
1142     av_pkt.data = NULL;
1143     av_pkt.size = 0;
1144 
1145     av_init_packet( &av_pkt );
1146 
1147     int ret = avcodec_send_frame( p_sys->p_context, frame );
1148     if( frame && ret != 0 && ret != AVERROR(EAGAIN) )
1149     {
1150         msg_Warn( p_enc, "cannot send one frame to encoder %d", ret );
1151         return NULL;
1152     }
1153     ret = avcodec_receive_packet( p_sys->p_context, &av_pkt );
1154     if( ret != 0 && ret != AVERROR(EAGAIN) )
1155     {
1156         msg_Warn( p_enc, "cannot encode one frame" );
1157         return NULL;
1158     }
1159 
1160     block_t *p_block = vlc_av_packet_Wrap( &av_pkt,
1161             av_pkt.duration / p_sys->p_context->time_base.den, p_sys->p_context );
1162     if( unlikely(p_block == NULL) )
1163     {
1164         av_packet_unref( &av_pkt );
1165         return NULL;
1166     }
1167     return p_block;
1168 }
1169 
1170 /****************************************************************************
1171  * EncodeVideo: the whole thing
1172  ****************************************************************************/
EncodeVideo(encoder_t * p_enc,picture_t * p_pict)1173 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
1174 {
1175     encoder_sys_t *p_sys = p_enc->p_sys;
1176     int i_plane;
1177 
1178     AVFrame *frame = NULL;
1179     if( likely(p_pict) ) {
1180         frame = p_sys->frame;
1181         av_frame_unref( frame );
1182 
1183         for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
1184         {
1185             p_sys->frame->data[i_plane] = p_pict->p[i_plane].p_pixels;
1186             p_sys->frame->linesize[i_plane] = p_pict->p[i_plane].i_pitch;
1187         }
1188 
1189         /* Let libavcodec select the frame type */
1190         frame->pict_type = 0;
1191 
1192         frame->repeat_pict = p_pict->i_nb_fields - 2;
1193         frame->interlaced_frame = !p_pict->b_progressive;
1194         frame->top_field_first = !!p_pict->b_top_field_first;
1195 
1196         frame->format = p_sys->p_context->pix_fmt;
1197         frame->width = p_sys->p_context->width;
1198         frame->height = p_sys->p_context->height;
1199 
1200         /* Set the pts of the frame being encoded
1201          * avcodec likes pts to be in time_base units
1202          * frame number */
1203         if( likely( p_pict->date > VLC_TS_INVALID ) )
1204             frame->pts = p_pict->date * p_sys->p_context->time_base.den /
1205                           CLOCK_FREQ / p_sys->p_context->time_base.num;
1206         else
1207             frame->pts = AV_NOPTS_VALUE;
1208 
1209         if ( p_sys->b_hurry_up && frame->pts != AV_NOPTS_VALUE )
1210             check_hurry_up( p_sys, frame, p_enc );
1211 
1212         if ( ( frame->pts != AV_NOPTS_VALUE ) && ( frame->pts != VLC_TS_INVALID ) )
1213         {
1214             if ( p_sys->i_last_pts == frame->pts )
1215             {
1216                 msg_Warn( p_enc, "almost fed libavcodec with two frames with "
1217                           "the same PTS (%"PRId64 ")", frame->pts );
1218                 return NULL;
1219             }
1220             else if ( p_sys->i_last_pts > frame->pts )
1221             {
1222                 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
1223                          "past (current: %"PRId64 ", last: %"PRId64")",
1224                          frame->pts, p_sys->i_last_pts );
1225                 return NULL;
1226             }
1227             else
1228                 p_sys->i_last_pts = frame->pts;
1229         }
1230 
1231         frame->quality = p_sys->i_quality;
1232     }
1233 
1234     block_t *p_block = encode_avframe( p_enc, p_sys, frame );
1235 
1236     if( p_block )
1237     {
1238        switch ( p_sys->p_context->coded_frame->pict_type )
1239        {
1240        case AV_PICTURE_TYPE_I:
1241        case AV_PICTURE_TYPE_SI:
1242            p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1243            break;
1244        case AV_PICTURE_TYPE_P:
1245        case AV_PICTURE_TYPE_SP:
1246            p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1247            break;
1248        case AV_PICTURE_TYPE_B:
1249        case AV_PICTURE_TYPE_BI:
1250            p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1251            break;
1252        default:
1253            p_block->i_flags |= BLOCK_FLAG_TYPE_PB;
1254        }
1255     }
1256 
1257     return p_block;
1258 }
1259 
handle_delay_buffer(encoder_t * p_enc,encoder_sys_t * p_sys,unsigned int buffer_delay,block_t * p_aout_buf,size_t leftover_samples)1260 static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, unsigned int buffer_delay,
1261                                      block_t *p_aout_buf, size_t leftover_samples )
1262 {
1263     block_t *p_block = NULL;
1264     //How much we need to copy from new packet
1265     const size_t leftover = leftover_samples * p_sys->p_context->channels * p_sys->i_sample_bytes;
1266 
1267     av_frame_unref( p_sys->frame );
1268     p_sys->frame->format     = p_sys->p_context->sample_fmt;
1269     p_sys->frame->nb_samples = leftover_samples + p_sys->i_samples_delay;
1270 
1271     p_sys->frame->pts        = date_Get( &p_sys->buffer_date ) * p_sys->p_context->time_base.den /
1272                                 CLOCK_FREQ / p_sys->p_context->time_base.num;
1273 
1274     if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1275         date_Increment( &p_sys->buffer_date, p_sys->frame->nb_samples );
1276 
1277     if( likely( p_aout_buf ) )
1278     {
1279 
1280         p_aout_buf->i_nb_samples -= leftover_samples;
1281         memcpy( p_sys->p_buffer+buffer_delay, p_aout_buf->p_buffer, leftover );
1282 
1283         // We need to deinterleave from p_aout_buf to p_buffer the leftover bytes
1284         if( p_sys->b_planar )
1285             aout_Deinterleave( p_sys->p_interleave_buf, p_sys->p_buffer,
1286                 p_sys->i_frame_size, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
1287         else
1288             memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer, leftover);
1289 
1290         p_aout_buf->p_buffer     += leftover;
1291         p_aout_buf->i_buffer     -= leftover;
1292         if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1293             p_aout_buf->i_pts         = date_Get( &p_sys->buffer_date );
1294     }
1295 
1296     if(unlikely( ( (leftover + buffer_delay) < p_sys->i_buffer_out ) &&
1297                  !(p_sys->p_codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME )))
1298     {
1299         msg_Dbg( p_enc, "No small last frame support, padding");
1300         size_t padding_size = p_sys->i_buffer_out - (leftover+buffer_delay);
1301         memset( p_sys->p_buffer + (leftover+buffer_delay), 0, padding_size );
1302         buffer_delay += padding_size;
1303     }
1304     if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
1305             p_sys->p_context->sample_fmt, p_sys->b_planar ? p_sys->p_interleave_buf : p_sys->p_buffer,
1306             p_sys->i_buffer_out,
1307             DEFAULT_ALIGN) < 0 )
1308     {
1309         msg_Err( p_enc, "filling error on fillup" );
1310         p_sys->frame->nb_samples = 0;
1311     }
1312 
1313 
1314     p_sys->i_samples_delay = 0;
1315 
1316     p_block = encode_avframe( p_enc, p_sys, p_sys->frame );
1317 
1318     return p_block;
1319 }
1320 
1321 /****************************************************************************
1322  * EncodeAudio: the whole thing
1323  ****************************************************************************/
EncodeAudio(encoder_t * p_enc,block_t * p_aout_buf)1324 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
1325 {
1326     encoder_sys_t *p_sys = p_enc->p_sys;
1327 
1328     block_t *p_block, *p_chain = NULL;
1329     size_t buffer_delay = 0, i_samples_left = 0;
1330 
1331 
1332     //i_bytes_left is amount of bytes we get
1333     i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
1334     buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_sys->p_context->channels;
1335 
1336     //p_sys->i_buffer_out = p_sys->i_frame_size * chan * p_sys->i_sample_bytes
1337     //Calculate how many bytes we would need from current buffer to fill frame
1338     size_t leftover_samples = __MAX(0,__MIN((ssize_t)i_samples_left, (ssize_t)(p_sys->i_frame_size - p_sys->i_samples_delay)));
1339 
1340     if( p_aout_buf && ( p_aout_buf->i_pts > VLC_TS_INVALID ) )
1341     {
1342         date_Set( &p_sys->buffer_date, p_aout_buf->i_pts );
1343         /* take back amount we have leftover from previous buffer*/
1344         if( p_sys->i_samples_delay > 0 )
1345             date_Decrement( &p_sys->buffer_date, p_sys->i_samples_delay );
1346     }
1347     /* Handle reordering here so we have p_sys->p_buffer always in correct
1348      * order already */
1349     if( p_aout_buf && p_sys->i_channels_to_reorder > 0 )
1350     {
1351         aout_ChannelReorder( p_aout_buf->p_buffer, p_aout_buf->i_buffer,
1352              p_sys->i_channels_to_reorder, p_sys->pi_reorder_layout,
1353              p_enc->fmt_in.i_codec );
1354     }
1355 
1356     // Check if we have enough samples in delay_buffer and current p_aout_buf to fill frame
1357     // Or if we are cleaning up
1358     if( ( buffer_delay > 0 ) &&
1359             ( ( p_aout_buf && ( leftover_samples <= p_aout_buf->i_nb_samples ) &&
1360                ( (leftover_samples + p_sys->i_samples_delay ) >= p_sys->i_frame_size )
1361               ) ||
1362              ( !p_aout_buf )
1363             )
1364          )
1365     {
1366         p_chain = handle_delay_buffer( p_enc, p_sys, buffer_delay, p_aout_buf, leftover_samples );
1367         buffer_delay = 0;
1368         if( unlikely( !p_chain ) )
1369             return NULL;
1370     }
1371 
1372     if( unlikely( !p_aout_buf ) )
1373     {
1374         msg_Dbg(p_enc,"Flushing..");
1375         do {
1376             p_block = encode_avframe( p_enc, p_sys, NULL );
1377             if( likely( p_block ) )
1378             {
1379                 block_ChainAppend( &p_chain, p_block );
1380             }
1381         } while( p_block );
1382         return p_chain;
1383     }
1384 
1385 
1386     while( ( p_aout_buf->i_nb_samples >= p_sys->i_frame_size ) ||
1387            ( p_sys->b_variable && p_aout_buf->i_nb_samples ) )
1388     {
1389         av_frame_unref( p_sys->frame );
1390 
1391         if( p_sys->b_variable )
1392             p_sys->frame->nb_samples = p_aout_buf->i_nb_samples;
1393         else
1394             p_sys->frame->nb_samples = p_sys->i_frame_size;
1395         p_sys->frame->format     = p_sys->p_context->sample_fmt;
1396         p_sys->frame->pts        = date_Get( &p_sys->buffer_date ) * p_sys->p_context->time_base.den /
1397                                     CLOCK_FREQ / p_sys->p_context->time_base.num;
1398 
1399         const int in_bytes = p_sys->frame->nb_samples *
1400             p_sys->p_context->channels * p_sys->i_sample_bytes;
1401 
1402         if( p_sys->b_planar )
1403         {
1404             aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer,
1405                                p_sys->frame->nb_samples, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
1406 
1407         }
1408         else
1409         {
1410             memcpy(p_sys->p_buffer, p_aout_buf->p_buffer, in_bytes);
1411         }
1412 
1413         if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
1414                                     p_sys->p_context->sample_fmt,
1415                                     p_sys->p_buffer,
1416                                     p_sys->i_buffer_out,
1417                                     DEFAULT_ALIGN) < 0 )
1418         {
1419                  msg_Err( p_enc, "filling error on encode" );
1420                  p_sys->frame->nb_samples = 0;
1421         }
1422 
1423         p_aout_buf->p_buffer     += in_bytes;
1424         p_aout_buf->i_buffer     -= in_bytes;
1425         p_aout_buf->i_nb_samples -= p_sys->frame->nb_samples;
1426         if( likely( p_sys->frame->pts != AV_NOPTS_VALUE) )
1427             date_Increment( &p_sys->buffer_date, p_sys->frame->nb_samples );
1428 
1429         p_block = encode_avframe( p_enc, p_sys, p_sys->frame );
1430         if( likely( p_block ) )
1431             block_ChainAppend( &p_chain, p_block );
1432     }
1433 
1434     // We have leftover samples that don't fill frame_size, and libavcodec doesn't seem to like
1435     // that frame has more data than p_sys->i_frame_size most of the cases currently.
1436     if( p_aout_buf->i_nb_samples > 0 )
1437     {
1438        memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer,
1439                p_aout_buf->i_nb_samples * p_sys->i_sample_bytes * p_sys->p_context->channels);
1440        p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
1441     }
1442 
1443     return p_chain;
1444 }
1445 
1446 /*****************************************************************************
1447  * EndVideoEnc: libavcodec encoder destruction
1448  *****************************************************************************/
EndVideoEnc(vlc_object_t * p_this)1449 void EndVideoEnc( vlc_object_t *p_this )
1450 {
1451     encoder_t *p_enc = (encoder_t *)p_this;
1452     encoder_sys_t *p_sys = p_enc->p_sys;
1453 
1454     av_frame_free( &p_sys->frame );
1455 
1456     vlc_avcodec_lock();
1457     avcodec_close( p_sys->p_context );
1458     vlc_avcodec_unlock();
1459     avcodec_free_context( &p_sys->p_context );
1460 
1461 
1462     av_free( p_sys->p_interleave_buf );
1463     av_free( p_sys->p_buffer );
1464 
1465     free( p_sys );
1466 }
1467