]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/encoder.c
avcodec: encoder: Check memory allocations.
[vlc] / modules / codec / avcodec / encoder.c
1 /*****************************************************************************
2  * encoder.c: video and audio encoder using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
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
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 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 General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, 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 <vlc_common.h>
36 #include <vlc_aout.h>
37 #include <vlc_sout.h>
38 #include <vlc_codec.h>
39 #include <vlc_dialog.h>
40 #include <vlc_avcodec.h>
41 #include <vlc_cpu.h>
42
43 /* ffmpeg header */
44 #define HAVE_MMX 1
45 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
46 #   include <libavcodec/avcodec.h>
47 #elif defined(HAVE_FFMPEG_AVCODEC_H)
48 #   include <ffmpeg/avcodec.h>
49 #else
50 #   include <avcodec.h>
51 #endif
52
53 #include "avcodec.h"
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 int  OpenEncoder ( vlc_object_t * );
67 void CloseEncoder( vlc_object_t * );
68
69 static block_t *EncodeVideo( encoder_t *, picture_t * );
70 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
71
72 struct thread_context_t;
73
74 /*****************************************************************************
75  * thread_context_t : for multithreaded encoding
76  *****************************************************************************/
77 struct thread_context_t
78 {
79     VLC_COMMON_MEMBERS
80
81     AVCodecContext  *p_context;
82     int             (* pf_func)(AVCodecContext *c, void *arg);
83     void            *arg;
84     int             i_ret;
85
86     vlc_mutex_t     lock;
87     vlc_cond_t      cond;
88     bool            b_work, b_done;
89 };
90
91 /*****************************************************************************
92  * encoder_sys_t : ffmpeg encoder descriptor
93  *****************************************************************************/
94 struct encoder_sys_t
95 {
96     /*
97      * Ffmpeg properties
98      */
99     AVCodec         *p_codec;
100     AVCodecContext  *p_context;
101
102     /*
103      * Common properties
104      */
105     char *p_buffer;
106     uint8_t *p_buffer_out;
107     size_t i_buffer_out;
108
109     /*
110      * Video properties
111      */
112     mtime_t i_last_ref_pts;
113     mtime_t i_buggy_pts_detect;
114     mtime_t i_last_pts;
115     bool    b_inited;
116
117     /*
118      * Audio properties
119      */
120     int i_sample_bytes;
121     int i_frame_size;
122     int i_samples_delay;
123     mtime_t i_pts;
124
125     /* Encoding settings */
126     int        i_key_int;
127     int        i_b_frames;
128     int        i_vtolerance;
129     int        i_qmin;
130     int        i_qmax;
131     int        i_hq;
132     int        i_rc_buffer_size;
133     float      f_rc_buffer_aggressivity;
134     bool       b_pre_me;
135     bool       b_hurry_up;
136     bool       b_interlace, b_interlace_me;
137     float      f_i_quant_factor;
138     int        i_noise_reduction;
139     bool       b_mpeg4_matrix;
140     bool       b_trellis;
141     int        i_quality; /* for VBR */
142     float      f_lumi_masking, f_dark_masking, f_p_masking, f_border_masking;
143     int        i_luma_elim, i_chroma_elim;
144     int        i_aac_profile; /* AAC profile to use.*/
145     /* Used to work around stupid timestamping behaviour in libavcodec */
146     uint64_t i_framenum;
147     mtime_t  pi_delay_pts[MAX_FRAME_DELAY];
148 };
149
150 static const char *const ppsz_enc_options[] = {
151     "keyint", "bframes", "vt", "qmin", "qmax", "hq",
152     "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
153     "interlace", "interlace-me", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
154     "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
155     "p-masking", "border-masking", "luma-elim-threshold",
156     "chroma-elim-threshold",
157      "aac-profile",
158      NULL
159 };
160
161 static const uint16_t mpa_bitrate_tab[2][15] =
162 {
163     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
164     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
165 };
166
167 static const uint16_t mpa_freq_tab[6] =
168 { 44100, 48000, 32000, 22050, 24000, 16000 };
169
170 static const uint16_t mpeg4_default_intra_matrix[64] = {
171   8, 17, 18, 19, 21, 23, 25, 27,
172  17, 18, 19, 21, 23, 25, 27, 28,
173  20, 21, 22, 23, 24, 26, 28, 30,
174  21, 22, 23, 24, 26, 28, 30, 32,
175  22, 23, 24, 26, 28, 30, 32, 35,
176  23, 24, 26, 28, 30, 32, 35, 38,
177  25, 26, 28, 30, 32, 35, 38, 41,
178  27, 28, 30, 32, 35, 38, 41, 45,
179 };
180
181 static const uint16_t mpeg4_default_non_intra_matrix[64] = {
182  16, 17, 18, 19, 20, 21, 22, 23,
183  17, 18, 19, 20, 21, 22, 23, 24,
184  18, 19, 20, 21, 22, 23, 24, 25,
185  19, 20, 21, 22, 23, 24, 26, 27,
186  20, 21, 22, 23, 25, 26, 27, 28,
187  21, 22, 23, 24, 26, 27, 28, 30,
188  22, 23, 24, 26, 27, 28, 30, 31,
189  23, 24, 25, 27, 28, 30, 31, 33,
190 };
191
192 /*****************************************************************************
193  * OpenEncoder: probe the encoder
194  *****************************************************************************/
195
196 int OpenEncoder( vlc_object_t *p_this )
197 {
198     encoder_t *p_enc = (encoder_t *)p_this;
199     encoder_sys_t *p_sys;
200     AVCodecContext *p_context;
201     AVCodec *p_codec;
202     int i_codec_id, i_cat;
203     const char *psz_namecodec;
204     float f_val;
205     char *psz_val;
206     int i_val;
207
208     if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
209                              &psz_namecodec ) )
210     {
211         if( TestFfmpegChroma( -1, p_enc->fmt_out.i_codec ) != VLC_SUCCESS )
212         {
213             /* handed chroma output */
214             return VLC_EGENERIC;
215         }
216         i_cat      = VIDEO_ES;
217         i_codec_id = CODEC_ID_RAWVIDEO;
218         psz_namecodec = "Raw video";
219     }
220
221     if( p_enc->fmt_out.i_cat == VIDEO_ES && i_cat != VIDEO_ES )
222     {
223         msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
224         dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
225                         _("\"%s\" is no video encoder."), psz_namecodec );
226         return VLC_EGENERIC;
227     }
228
229     if( p_enc->fmt_out.i_cat == AUDIO_ES && i_cat != AUDIO_ES )
230     {
231         msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
232         dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
233                         _("\"%s\" is no audio encoder."), psz_namecodec );
234         return VLC_EGENERIC;
235     }
236
237     if( p_enc->fmt_out.i_cat == SPU_ES )
238     {
239         /* We don't support subtitle encoding */
240         return VLC_EGENERIC;
241     }
242
243     /* Initialization must be done before avcodec_find_encoder() */
244     InitLibavcodec( p_this );
245
246     p_codec = avcodec_find_encoder( i_codec_id );
247     if( !p_codec )
248     {
249         msg_Err( p_enc, "cannot find encoder %s\n"
250 "*** Your FFMPEG installation is crippled.   ***\n"
251 "*** Please check with your FFMPEG packager. ***\n"
252 "*** This is NOT a VLC media player issue.   ***", psz_namecodec );
253
254         dialog_Fatal( p_enc, _("Streaming / Transcoding failed"), _(
255 /* I have had enough of all these MPEG-3 transcoding bug reports.
256  * Downstream packager, you had better not patch this out, or I will be really
257  * annoyed. Think about it - you don't want to fork the VLC translation files,
258  * do you? -- Courmisch, 2008-10-22 */
259 "It seems your FFMPEG (libavcodec) installation lacks the following encoder:\n"
260 "%s.\n"
261 "If you don't know how to fix this, ask for support from your distribution.\n"
262 "\n"
263 "This is not an error inside VLC media player.\n"
264 "Do not contact the VideoLAN project about this issue.\n"),
265             psz_namecodec );
266         return VLC_EGENERIC;
267     }
268
269     /* Allocate the memory needed to store the encoder's structure */
270     if( ( p_sys = calloc( 1, sizeof(encoder_sys_t) ) ) == NULL )
271         return VLC_ENOMEM;
272     p_enc->p_sys = p_sys;
273     p_sys->p_codec = p_codec;
274
275     p_enc->pf_encode_video = EncodeVideo;
276     p_enc->pf_encode_audio = EncodeAudio;
277
278     p_sys->p_buffer = NULL;
279     p_sys->p_buffer_out = NULL;
280     p_sys->i_buffer_out = 0;
281
282     p_sys->p_context = p_context = avcodec_alloc_context();
283     p_sys->p_context->codec_id = p_sys->p_codec->id;
284     p_context->debug = var_InheritInteger( p_enc, "ffmpeg-debug" );
285     p_context->opaque = (void *)p_this;
286
287     /* Set CPU capabilities */
288     unsigned i_cpu = vlc_CPU();
289     p_context->dsp_mask = 0;
290     if( !(i_cpu & CPU_CAPABILITY_MMX) )
291     {
292         p_context->dsp_mask |= FF_MM_MMX;
293     }
294     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
295     {
296         p_context->dsp_mask |= FF_MM_MMXEXT;
297     }
298     if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
299     {
300         p_context->dsp_mask |= FF_MM_3DNOW;
301     }
302     if( !(i_cpu & CPU_CAPABILITY_SSE) )
303     {
304         p_context->dsp_mask |= FF_MM_SSE;
305         p_context->dsp_mask |= FF_MM_SSE2;
306     }
307
308     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
309
310     p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
311     p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );
312     p_sys->i_vtolerance = var_GetInteger( p_enc, ENC_CFG_PREFIX "vt" ) * 1000;
313     p_sys->b_interlace = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace" );
314     p_sys->b_interlace_me = var_GetBool( p_enc, ENC_CFG_PREFIX "interlace-me" );
315     p_sys->b_pre_me = var_GetBool( p_enc, ENC_CFG_PREFIX "pre-me" );
316     p_sys->b_hurry_up = var_GetBool( p_enc, ENC_CFG_PREFIX "hurry-up" );
317
318     if( p_sys->b_hurry_up )
319     {
320         /* hurry up mode needs noise reduction, even small */
321         p_sys->i_noise_reduction = 1;
322     }
323
324     p_sys->i_rc_buffer_size = var_GetInteger( p_enc, ENC_CFG_PREFIX "rc-buffer-size" );
325     p_sys->f_rc_buffer_aggressivity = var_GetFloat( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity" );
326     p_sys->f_i_quant_factor = var_GetFloat( p_enc, ENC_CFG_PREFIX "i-quant-factor" );
327     p_sys->i_noise_reduction = var_GetInteger( p_enc, ENC_CFG_PREFIX "noise-reduction" );
328     p_sys->b_mpeg4_matrix = var_GetBool( p_enc, ENC_CFG_PREFIX "mpeg4-matrix" );
329
330     f_val = var_GetFloat( p_enc, ENC_CFG_PREFIX "qscale" );
331     if( f_val < 0.01 || f_val > 255.0 ) f_val = 0;
332     p_sys->i_quality = (int)(FF_QP2LAMBDA * f_val + 0.5);
333
334     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "hq" );
335     p_sys->i_hq = FF_MB_DECISION_RD;
336     if( psz_val && *psz_val )
337     {
338         if( !strcmp( psz_val, "rd" ) )
339             p_sys->i_hq = FF_MB_DECISION_RD;
340         else if( !strcmp( psz_val, "bits" ) )
341             p_sys->i_hq = FF_MB_DECISION_BITS;
342         else if( !strcmp( psz_val, "simple" ) )
343             p_sys->i_hq = FF_MB_DECISION_SIMPLE;
344         else
345             p_sys->i_hq = FF_MB_DECISION_RD;
346     }
347     else
348         p_sys->i_hq = FF_MB_DECISION_RD;
349     free( psz_val );
350
351     p_sys->i_qmin = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" );
352     p_sys->i_qmax = var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" );
353     p_sys->b_trellis = var_GetBool( p_enc, ENC_CFG_PREFIX "trellis" );
354
355     i_val = var_GetInteger( p_enc, ENC_CFG_PREFIX "strict" );
356     if( i_val < - 1 || i_val > 1 ) i_val = 0;
357     p_context->strict_std_compliance = i_val;
358
359     p_sys->f_lumi_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "lumi-masking" );
360     p_sys->f_dark_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "dark-masking" );
361     p_sys->f_p_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "p-masking" );
362     p_sys->f_border_masking = var_GetFloat( p_enc, ENC_CFG_PREFIX "border-masking" );
363     p_sys->i_luma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "luma-elim-threshold" );
364     p_sys->i_chroma_elim = var_GetInteger( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold" );
365
366     psz_val = var_GetString( p_enc, ENC_CFG_PREFIX "aac-profile" );
367     /* ffmpeg uses faac encoder atm, and it has issues with
368      * other than low-complexity profile, so default to that */
369     p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
370     if( psz_val && *psz_val )
371     {
372         if( !strncmp( psz_val, "main", 4 ) )
373             p_sys->i_aac_profile = FF_PROFILE_AAC_MAIN;
374         else if( !strncmp( psz_val, "low", 3 ) )
375             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
376 #if 0    /* Not supported by FAAC encoder */
377         else if( !strncmp( psz_val, "ssr", 3 ) )
378             p_sys->i_aac_profile = FF_PROFILE_AAC_SSR;
379 #endif
380         else  if( !strncmp( psz_val, "ltp", 3 ) )
381             p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
382         else
383         {
384             msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
385             p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
386         }
387     }
388     free( psz_val );
389
390     if( p_enc->fmt_in.i_cat == VIDEO_ES )
391     {
392         if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
393         {
394             msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
395                       p_enc->fmt_in.video.i_height );
396             free( p_sys );
397             return VLC_EGENERIC;
398         }
399
400         p_context->codec_type = CODEC_TYPE_VIDEO;
401
402         p_context->width = p_enc->fmt_in.video.i_width;
403         p_context->height = p_enc->fmt_in.video.i_height;
404
405         p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
406         p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
407
408         /* Defaults from ffmpeg.c */
409         p_context->qblur = 0.5;
410         p_context->qcompress = 0.5;
411         p_context->b_quant_offset = 1.25;
412         p_context->b_quant_factor = 1.25;
413         p_context->i_quant_offset = 0.0;
414         p_context->i_quant_factor = -0.8;
415
416         p_context->lumi_masking = p_sys->f_lumi_masking;
417         p_context->dark_masking = p_sys->f_dark_masking;
418         p_context->p_masking = p_sys->f_p_masking;
419         p_context->border_masking = p_sys->f_border_masking;
420         p_context->luma_elim_threshold = p_sys->i_luma_elim;
421         p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
422
423         if( p_sys->i_key_int > 0 )
424             p_context->gop_size = p_sys->i_key_int;
425         p_context->max_b_frames =
426             __MAX( __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES ), 0 );
427         p_context->b_frame_strategy = 0;
428         if( !p_context->max_b_frames  &&
429             (  p_enc->fmt_out.i_codec == VLC_CODEC_MPGV ||
430                p_enc->fmt_out.i_codec == VLC_CODEC_MP2V ||
431                p_enc->fmt_out.i_codec == VLC_CODEC_MP1V ) )
432             p_context->flags |= CODEC_FLAG_LOW_DELAY;
433
434         av_reduce( &p_context->sample_aspect_ratio.num,
435                    &p_context->sample_aspect_ratio.den,
436                    p_enc->fmt_in.video.i_sar_num,
437                    p_enc->fmt_in.video.i_sar_den, 1 << 30 );
438
439         p_sys->p_buffer_out = NULL;
440
441         p_enc->fmt_in.i_codec = VLC_CODEC_I420;
442         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
443         GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
444
445         if( p_codec->pix_fmts )
446         {
447             const enum PixelFormat *p = p_codec->pix_fmts;
448             for( ; *p != -1; p++ )
449             {
450                 if( *p == p_context->pix_fmt ) break;
451             }
452             if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
453             GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
454             p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
455         }
456
457
458         if ( p_sys->f_i_quant_factor != 0.0 )
459             p_context->i_quant_factor = p_sys->f_i_quant_factor;
460
461         p_context->noise_reduction = p_sys->i_noise_reduction;
462
463         if ( p_sys->b_mpeg4_matrix )
464         {
465             p_context->intra_matrix = mpeg4_default_intra_matrix;
466             p_context->inter_matrix = mpeg4_default_non_intra_matrix;
467         }
468
469         if ( p_sys->b_pre_me )
470         {
471             p_context->pre_me = 1;
472             p_context->me_pre_cmp = FF_CMP_CHROMA;
473         }
474
475         if ( p_sys->b_interlace )
476         {
477             if ( p_context->height <= 280 )
478             {
479                 if ( p_context->height != 16 || p_context->width != 16 )
480                     msg_Warn( p_enc,
481                         "disabling interlaced video because height=%d <= 280",
482                         p_context->height );
483             }
484             else
485             {
486                 p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
487                 if ( p_sys->b_interlace_me )
488                     p_context->flags |= CODEC_FLAG_INTERLACED_ME;
489             }
490         }
491
492         p_context->trellis = p_sys->b_trellis;
493
494         if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
495             p_context->flags |= CODEC_FLAG_QSCALE;
496         /* These codecs cause libavcodec to exit if thread_count is > 1.
497            See libavcodec/mpegvideo_enc.c:MPV_encode_init and
498            libavcodec/svq3.c , WMV2 calls MPV_encode_init also.
499          */
500         if ( i_codec_id == CODEC_ID_FLV1 ||
501              i_codec_id == CODEC_ID_H261 ||
502              i_codec_id == CODEC_ID_LJPEG ||
503              i_codec_id == CODEC_ID_MJPEG ||
504              i_codec_id == CODEC_ID_H263 ||
505              i_codec_id == CODEC_ID_H263P ||
506              i_codec_id == CODEC_ID_MSMPEG4V1 ||
507              i_codec_id == CODEC_ID_MSMPEG4V2 ||
508              i_codec_id == CODEC_ID_MSMPEG4V3 ||
509              i_codec_id == CODEC_ID_WMV1 ||
510              i_codec_id == CODEC_ID_WMV2 ||
511              i_codec_id == CODEC_ID_RV10 ||
512              i_codec_id == CODEC_ID_RV20 ||
513              i_codec_id == CODEC_ID_SVQ3 )
514             p_enc->i_threads = 1;
515
516         if ( p_enc->i_threads >= 1 )
517             avcodec_thread_init( p_context, p_enc->i_threads );
518         else
519             avcodec_thread_init( p_context, vlc_GetCPUCount() );
520
521         if( p_sys->i_vtolerance > 0 )
522             p_context->bit_rate_tolerance = p_sys->i_vtolerance;
523
524         /* usually if someone sets bitrate, he likes more to get that bitrate
525          * over quality should help 'normal' user to get asked bitrate
526          */
527         if( p_enc->fmt_out.i_bitrate > 0 && p_sys->i_qmax == 0 && p_sys->i_qmin == 0 )
528         {
529             p_sys->i_qmax = 51;
530             p_sys->i_qmin = 3;
531         }
532
533         if( p_sys->i_qmin > 0 )
534         {
535             p_context->mb_qmin = p_context->qmin = p_sys->i_qmin;
536             p_context->mb_lmin = p_context->lmin = p_sys->i_qmin * FF_QP2LAMBDA;
537         }
538         if( p_sys->i_qmax > 0 )
539         {
540             p_context->mb_qmax = p_context->qmax = p_sys->i_qmax;
541             p_context->mb_lmax = p_context->lmax = p_sys->i_qmax * FF_QP2LAMBDA;
542         }
543         p_context->max_qdiff = 3;
544
545         p_context->mb_decision = p_sys->i_hq;
546
547         if( p_sys->i_quality )
548         {
549             p_context->flags |= CODEC_FLAG_QSCALE;
550             p_context->global_quality = p_sys->i_quality;
551         }
552         else
553         {
554             p_context->rc_qsquish = 1.0;
555             p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
556             p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
557             p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
558             /* This is from ffmpeg's ffmpeg.c : */
559             p_context->rc_initial_buffer_occupancy
560                 = p_sys->i_rc_buffer_size * 3/4;
561             p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
562         }
563     }
564     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
565     {
566         /* work around bug in libmp3lame encoding */
567         if( i_codec_id == CODEC_ID_MP3 && p_enc->fmt_in.audio.i_channels > 2 )
568             p_enc->fmt_in.audio.i_channels = 2;
569
570         p_context->codec_type  = CODEC_TYPE_AUDIO;
571         p_context->sample_fmt  = p_codec->sample_fmts ?
572                                     p_codec->sample_fmts[0] :
573                                     SAMPLE_FMT_S16;
574         p_enc->fmt_in.i_codec  = VLC_CODEC_S16N;
575         p_context->sample_rate = p_enc->fmt_out.audio.i_rate;
576         p_context->channels    = p_enc->fmt_out.audio.i_channels;
577
578         if ( p_enc->fmt_out.i_codec == VLC_CODEC_MP4A )
579         {
580             /* XXX: FAAC does resample only when setting the INPUT samplerate
581              * to the desired value (-R option of the faac frontend)
582             p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
583             /* vlc should default to low-complexity profile, faac encoder
584              * has bug and aac audio has issues otherwise atm */
585             p_context->profile = p_sys->i_aac_profile;
586         }
587     }
588
589     /* Misc parameters */
590     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
591
592 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 69, 2 )
593     /* Set reasonable defaults to VP8, based on
594        libvpx-720p preset from libvpx ffmpeg-patch */
595     if( i_codec_id == CODEC_ID_VP8 )
596     {
597         /* Lets give bitrate tolerance */
598         p_context->bit_rate_tolerance = __MAX(2 * p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance );
599         /* default to 120 frames between keyframe */
600         if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) )
601            p_context->gop_size = 120;
602         /* Don't set rc-values atm, they were from time before
603            libvpx was officially in ffmpeg */
604         //p_context->rc_max_rate = 24 * 1000 * 1000; //24M
605         //p_context->rc_min_rate = 40 * 1000; // 40k
606         /* seems that ffmpeg presets have 720p as divider for buffers */
607         if( p_enc->fmt_out.video.i_height >= 720 )
608         {
609            /* Check that we don't overrun users qmin/qmax values */
610            if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
611            {
612               p_context->mb_qmin = p_context->qmin = 10;
613               p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA;
614            }
615
616            if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) )
617            {
618               p_context->mb_qmax = p_context->qmax = 42;
619               p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA;
620            }
621
622         } else {
623            if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) )
624            {
625               p_context->mb_qmin = p_context->qmin = 1;
626               p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA;
627            }
628         }
629
630
631 #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */
632         p_context->lag = 16;
633         p_context->level = 216;
634         p_context->profile = 0;
635         p_context->rc_buffer_aggressivity = 0.95;
636         p_context->token_partitions = 4;
637         p_context->mb_static_threshold = 0;
638 #endif
639     }
640 #endif
641
642     if( i_codec_id == CODEC_ID_RAWVIDEO )
643     {
644         /* XXX: hack: Force same codec (will be handled by transcode) */
645         p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
646         GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
647     }
648
649     /* Make sure we get extradata filled by the encoder */
650     p_context->extradata_size = 0;
651     p_context->extradata = NULL;
652     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
653
654     int ret;
655     vlc_avcodec_lock();
656     ret = avcodec_open( p_context, p_codec );
657     vlc_avcodec_unlock();
658     if( ret )
659     {
660         if( p_enc->fmt_in.i_cat == AUDIO_ES &&
661              (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
662                || i_codec_id == CODEC_ID_MP3) )
663         {
664             if( p_context->channels > 2 )
665             {
666                 p_context->channels = 2;
667                 p_enc->fmt_in.audio.i_channels = 2; // FIXME
668                 msg_Warn( p_enc, "stereo mode selected (codec limitation)" );
669             }
670
671             if( i_codec_id == CODEC_ID_MP2 || i_codec_id == CODEC_ID_MP3 )
672             {
673                 int i_frequency, i;
674
675                 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
676                 {
677                     if ( p_enc->fmt_out.audio.i_rate
678                             == mpa_freq_tab[i_frequency] )
679                         break;
680                 }
681                 if ( i_frequency == 6 )
682                 {
683                     msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
684                              p_enc->fmt_out.audio.i_rate );
685                     free( p_sys );
686                     return VLC_EGENERIC;
687                 }
688
689                 for ( i = 1; i < 14; i++ )
690                 {
691                     if ( p_enc->fmt_out.i_bitrate / 1000
692                           <= mpa_bitrate_tab[i_frequency / 3][i] )
693                         break;
694                 }
695                 if ( p_enc->fmt_out.i_bitrate / 1000
696                       != mpa_bitrate_tab[i_frequency / 3][i] )
697                 {
698                     msg_Warn( p_enc,
699                               "MPEG audio doesn't support bitrate=%d, using %d",
700                               p_enc->fmt_out.i_bitrate,
701                               mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
702                     p_enc->fmt_out.i_bitrate =
703                         mpa_bitrate_tab[i_frequency / 3][i] * 1000;
704                     p_context->bit_rate = p_enc->fmt_out.i_bitrate;
705                 }
706             }
707
708             p_context->codec = NULL;
709             vlc_avcodec_lock();
710             ret = avcodec_open( p_context, p_codec );
711             vlc_avcodec_unlock();
712             if( ret )
713             {
714                 msg_Err( p_enc, "cannot open encoder" );
715                 dialog_Fatal( p_enc,
716                                 _("Streaming / Transcoding failed"),
717                                 "%s", _("VLC could not open the encoder.") );
718                 free( p_sys );
719                 return VLC_EGENERIC;
720             }
721         }
722         else
723         {
724             msg_Err( p_enc, "cannot open encoder" );
725             dialog_Fatal( p_enc, _("Streaming / Transcoding failed"),
726                             "%s", _("VLC could not open the encoder.") );
727             free( p_sys );
728             return VLC_EGENERIC;
729         }
730     }
731
732     if( i_codec_id == CODEC_ID_FLAC )
733     {
734         p_enc->fmt_out.i_extra = 4 + 1 + 3 + p_context->extradata_size;
735         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
736         if( p_enc->fmt_out.p_extra )
737         {
738             uint8_t *p = p_enc->fmt_out.p_extra;
739             p[0] = 0x66;    /* f */
740             p[1] = 0x4C;    /* L */
741             p[2] = 0x61;    /* a */
742             p[3] = 0x43;    /* C */
743             p[4] = 0x80;    /* streaminfo block, last block before audio */
744             p[5] = ( p_context->extradata_size >> 16 ) & 0xff;
745             p[6] = ( p_context->extradata_size >>  8 ) & 0xff;
746             p[7] = ( p_context->extradata_size       ) & 0xff;
747             memcpy( &p[8], p_context->extradata, p_context->extradata_size );
748         }
749         else
750         {
751             p_enc->fmt_out.i_extra = 0;
752         }
753     }
754     else
755     {
756         p_enc->fmt_out.i_extra = p_context->extradata_size;
757         if( p_enc->fmt_out.i_extra )
758         {
759             p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
760             if ( p_enc->fmt_out.p_extra == NULL )
761             {
762                 goto error;
763             }
764             memcpy( p_enc->fmt_out.p_extra, p_context->extradata,
765                     p_enc->fmt_out.i_extra );
766         }
767     }
768
769     p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
770
771     if( p_enc->fmt_in.i_cat == AUDIO_ES )
772     {
773         GetVlcAudioFormat( &p_enc->fmt_in.i_codec,
774                            &p_enc->fmt_in.audio.i_bitspersample,
775                            p_sys->p_context->sample_fmt );
776         p_sys->i_sample_bytes = (p_enc->fmt_in.audio.i_bitspersample / 8) *
777                                 p_context->channels;
778         p_sys->i_frame_size = p_context->frame_size > 1 ?
779                                     p_context->frame_size :
780                                     RAW_AUDIO_FRAME_SIZE;
781         p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes );
782         if ( p_sys->p_buffer == NULL )
783         {
784             goto error;
785         }
786         p_enc->fmt_out.audio.i_blockalign = p_context->block_align;
787         p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( vlc_fourcc_GetCodec( AUDIO_ES, p_enc->fmt_out.i_codec ) );
788
789         if( p_context->frame_size > 1 )
790             p_sys->i_buffer_out = 8 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
791         else
792             p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes;
793         p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
794         if ( p_sys->p_buffer_out == NULL )
795         {
796             goto error;
797         }
798     }
799
800     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
801
802     return VLC_SUCCESS;
803 error:
804     free( p_enc->fmt_out.p_extra );
805     free( p_sys->p_buffer );
806     free( p_sys->p_buffer_out );
807     free( p_sys );
808     return VLC_ENOMEM;
809 }
810
811 /****************************************************************************
812  * EncodeVideo: the whole thing
813  ****************************************************************************/
814 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
815 {
816     encoder_sys_t *p_sys = p_enc->p_sys;
817     AVFrame frame;
818     int i_out, i_plane;
819
820     /* Initialize the video output buffer the first time.
821      * This is done here instead of OpenEncoder() because we need the actual
822      * bits_per_pixel value, without having to assume anything.
823      */
824     if ( p_sys->p_buffer_out == NULL )
825     {
826         int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ?
827                             p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3;
828
829         p_sys->i_buffer_out = p_sys->p_context->height * p_sys->p_context->width
830             * bytesPerPixel + 200;  /* some room for potential headers (such as BMP) */
831
832         if( p_sys->i_buffer_out < FF_MIN_BUFFER_SIZE )
833             p_sys->i_buffer_out = FF_MIN_BUFFER_SIZE;
834         p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
835         if ( p_sys->p_buffer_out == NULL )
836             return NULL;
837     }
838
839     memset( &frame, 0, sizeof( AVFrame ) );
840     if( likely(p_pict) ) {
841        for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ )
842        {
843            frame.data[i_plane] = p_pict->p[i_plane].p_pixels;
844            frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch;
845        }
846
847        /* Let ffmpeg select the frame type */
848        frame.pict_type = 0;
849
850        frame.repeat_pict = p_pict->i_nb_fields - 2;
851        frame.interlaced_frame = !p_pict->b_progressive;
852        frame.top_field_first = !!p_pict->b_top_field_first;
853
854        /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
855        if( p_enc->fmt_out.i_codec != VLC_CODEC_MP4V )
856        {
857            frame.pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE;
858
859            if ( p_sys->b_hurry_up && frame.pts != (int64_t)AV_NOPTS_VALUE )
860            {
861                mtime_t current_date = mdate();
862
863                if ( current_date + HURRY_UP_GUARD3 > frame.pts )
864                {
865                    p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
866                    p_sys->p_context->trellis = 0;
867                    msg_Dbg( p_enc, "hurry up mode 3" );
868                }
869                else
870                {
871                    p_sys->p_context->mb_decision = p_sys->i_hq;
872
873                    if ( current_date + HURRY_UP_GUARD2 > frame.pts )
874                    {
875                        p_sys->p_context->trellis = 0;
876                        p_sys->p_context->noise_reduction = p_sys->i_noise_reduction
877                             + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500;
878                        msg_Dbg( p_enc, "hurry up mode 2" );
879                    }
880                    else
881                    {
882                        p_sys->p_context->trellis = p_sys->b_trellis;
883
884                        p_sys->p_context->noise_reduction =
885                            p_sys->i_noise_reduction;
886                    }
887                }
888
889                if ( current_date + HURRY_UP_GUARD1 > frame.pts )
890                {
891                    frame.pict_type = FF_P_TYPE;
892                    /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
893                }
894            }
895        }
896        else
897        {
898            frame.pts = (int64_t)AV_NOPTS_VALUE;
899        }
900
901        if ( frame.pts != (int64_t)AV_NOPTS_VALUE && frame.pts != 0 )
902        {
903            if ( p_sys->i_last_pts == frame.pts )
904            {
905                msg_Warn( p_enc, "almost fed libavcodec with two frames with the "
906                          "same PTS (%"PRId64 ")", frame.pts );
907                return NULL;
908            }
909            else if ( p_sys->i_last_pts > frame.pts )
910            {
911                msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
912                          "past (current: %"PRId64 ", last: %"PRId64")",
913                          frame.pts, p_sys->i_last_pts );
914                return NULL;
915            }
916            else
917            {
918                p_sys->i_last_pts = frame.pts;
919            }
920        }
921
922        frame.quality = p_sys->i_quality;
923
924        /* Ugly work-around for stupid libavcodec behaviour */
925        p_sys->i_framenum++;
926        p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts;
927        frame.pts = p_sys->i_framenum * AV_TIME_BASE *
928            p_enc->fmt_in.video.i_frame_rate_base;
929        frame.pts += p_enc->fmt_in.video.i_frame_rate - 1;
930        frame.pts /= p_enc->fmt_in.video.i_frame_rate;
931        /* End work-around */
932
933        i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
934                                      p_sys->i_buffer_out, &frame );
935     }
936     else
937     {
938        i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out,
939                                      p_sys->i_buffer_out, NULL);
940     }
941
942     if( i_out > 0 )
943     {
944         block_t *p_block = block_New( p_enc, i_out );
945         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
946
947         /* FIXME, 3-2 pulldown is not handled correctly */
948         p_block->i_length = INT64_C(1000000) *
949             p_enc->fmt_in.video.i_frame_rate_base /
950                 p_enc->fmt_in.video.i_frame_rate;
951
952         if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay )
953         {
954             /* No delay -> output pts == input pts */
955             if( p_pict )
956                 p_block->i_dts = p_pict->date;
957             p_block->i_pts = p_block->i_dts;
958         }
959         else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE &&
960             p_sys->p_context->coded_frame->pts != 0 &&
961             p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts )
962         {
963             p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
964             p_block->i_pts = p_sys->p_context->coded_frame->pts;
965
966             /* Ugly work-around for stupid libavcodec behaviour */
967             {
968                 int64_t i_framenum = p_block->i_pts *
969                     p_enc->fmt_in.video.i_frame_rate /
970                     p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE;
971
972                 p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY];
973             }
974             /* End work-around */
975
976             if( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE &&
977                 p_sys->p_context->coded_frame->pict_type != FF_P_TYPE )
978             {
979                 p_block->i_dts = p_block->i_pts;
980             }
981             else
982             {
983                 if( p_sys->i_last_ref_pts )
984                 {
985                     p_block->i_dts = p_sys->i_last_ref_pts;
986                 }
987                 else
988                 {
989                     /* Let's put something sensible */
990                     p_block->i_dts = p_block->i_pts;
991                 }
992
993                 p_sys->i_last_ref_pts = p_block->i_pts;
994             }
995         }
996         else
997         {
998             /* Buggy libavcodec which doesn't update coded_frame->pts
999              * correctly */
1000             p_block->i_dts = p_block->i_pts = p_pict->date;
1001         }
1002
1003         switch ( p_sys->p_context->coded_frame->pict_type )
1004         {
1005         case FF_I_TYPE:
1006             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1007             break;
1008         case FF_P_TYPE:
1009             p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1010             break;
1011         case FF_B_TYPE:
1012             p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1013             break;
1014         }
1015
1016         return p_block;
1017     }
1018
1019     return NULL;
1020 }
1021
1022 /****************************************************************************
1023  * EncodeAudio: the whole thing
1024  ****************************************************************************/
1025 static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
1026 {
1027     encoder_sys_t *p_sys = p_enc->p_sys;
1028
1029     block_t *p_block, *p_chain = NULL;
1030
1031     uint8_t *p_buffer = p_aout_buf->p_buffer;
1032     int i_samples = p_aout_buf->i_nb_samples;
1033     int i_samples_delay = p_sys->i_samples_delay;
1034
1035     p_sys->i_pts = p_aout_buf->i_pts -
1036                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1037                 (mtime_t)p_enc->fmt_in.audio.i_rate;
1038
1039     p_sys->i_samples_delay += i_samples;
1040
1041     while( p_sys->i_samples_delay >= p_sys->i_frame_size )
1042     {
1043         void *p_samples;
1044         int i_out;
1045
1046         if( i_samples_delay )
1047         {
1048             /* Take care of the left-over from last time */
1049             int i_delay_size = i_samples_delay;
1050             int i_size = (p_sys->i_frame_size - i_delay_size) *
1051                          p_sys->i_sample_bytes;
1052
1053             memcpy( p_sys->p_buffer + i_delay_size * p_sys->i_sample_bytes,
1054                     p_buffer, i_size );
1055             p_buffer -= i_delay_size * p_sys->i_sample_bytes;
1056             i_samples += i_samples_delay;
1057             i_samples_delay = 0;
1058
1059             p_samples = p_sys->p_buffer;
1060         }
1061         else
1062         {
1063             p_samples = p_buffer;
1064         }
1065
1066         i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
1067                                       p_sys->i_buffer_out, p_samples );
1068
1069 #if 0
1070         msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
1071 #endif
1072         p_buffer += p_sys->i_frame_size * p_sys->i_sample_bytes;
1073         p_sys->i_samples_delay -= p_sys->i_frame_size;
1074         i_samples -= p_sys->i_frame_size;
1075
1076         if( i_out <= 0 )
1077             continue;
1078
1079         p_block = block_New( p_enc, i_out );
1080         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1081
1082         p_block->i_length = (mtime_t)1000000 *
1083             (mtime_t)p_sys->i_frame_size /
1084             (mtime_t)p_sys->p_context->sample_rate;
1085
1086         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1087
1088         /* Update pts */
1089         p_sys->i_pts += p_block->i_length;
1090         block_ChainAppend( &p_chain, p_block );
1091     }
1092
1093     /* Backup the remaining raw samples */
1094     if( i_samples )
1095     {
1096         memcpy( &p_sys->p_buffer[i_samples_delay * p_sys->i_sample_bytes],
1097                 p_buffer,
1098                 i_samples * p_sys->i_sample_bytes );
1099     }
1100
1101     return p_chain;
1102 }
1103
1104 /*****************************************************************************
1105  * CloseEncoder: ffmpeg encoder destruction
1106  *****************************************************************************/
1107 void CloseEncoder( vlc_object_t *p_this )
1108 {
1109     encoder_t *p_enc = (encoder_t *)p_this;
1110     encoder_sys_t *p_sys = p_enc->p_sys;
1111
1112     vlc_avcodec_lock();
1113     avcodec_close( p_sys->p_context );
1114     vlc_avcodec_unlock();
1115     av_free( p_sys->p_context );
1116
1117     free( p_sys->p_buffer );
1118     free( p_sys->p_buffer_out );
1119
1120     free( p_sys );
1121 }