]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/ffmpeg.c
* ffmpeg: --ffmpeg-truncated is now an int :
[vlc] / modules / codec / ffmpeg / ffmpeg.c
1 /*****************************************************************************
2  * ffmpeg.c: video decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: ffmpeg.c,v 1.35 2003/05/07 15:44:59 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/aout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34
35 #include <string.h>
36
37 #ifdef HAVE_SYS_TIMES_H
38 #   include <sys/times.h>
39 #endif
40
41 /* ffmpeg header */
42 #ifdef HAVE_FFMPEG_AVCODEC_H
43 #   include <ffmpeg/avcodec.h>
44 #else
45 #   include <avcodec.h>
46 #endif
47
48 #if LIBAVCODEC_BUILD < 4655
49 #   error You must have a libavcodec >= 4655 (get CVS)
50 #endif
51
52
53 #include "ffmpeg.h"
54
55 #ifdef LIBAVCODEC_PP
56 #   ifdef HAVE_POSTPROC_POSTPROCESS_H
57 #       include <postproc/postprocess.h>
58 #   else
59 #       include <libpostproc/postprocess.h>
60 #   endif
61 #else
62 #   include "postprocessing/postprocessing.h"
63 #endif
64
65 #include "video.h" // video ffmpeg specific
66 #include "audio.h" // audio ffmpeg specific
67
68 /*
69  * Local prototypes
70  */
71 int             E_(OpenChroma)  ( vlc_object_t * );
72 static int      OpenDecoder     ( vlc_object_t * );
73 static int      RunDecoder      ( decoder_fifo_t * );
74
75 static int      InitThread      ( generic_thread_t * );
76 static void     EndThread       ( generic_thread_t * );
77
78
79 static int      b_ffmpeginit = 0;
80
81 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t, int *, int *, char ** );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86
87 #define ERROR_RESILIENCE_LONGTEXT \
88     "ffmpeg can make errors resiliences.          \n" \
89     "Nevertheless, with buggy encoder (like ISO MPEG-4 encoder from M$) " \
90     "this will produce a lot of errors.\n" \
91     "Valid range is -1 to 99 (-1 disable all errors resiliences)."
92
93 #define HURRY_UP_LONGTEXT \
94     "Allow the decoder to partially decode or skip frame(s) " \
95     "when there not enough time.\n It's usefull with low CPU power " \
96     "but it could produce broken pictures."
97
98 #define POSTPROCESSING_Q_LONGTEXT \
99     "Quality of post processing\n"\
100     "Valid range is 0 to 6"
101
102 #define POSTPROCESSING_AQ_LONGTEXT \
103     "Post processing quality is selected upon time left " \
104     "but no more than requested quality\n" \
105     "Not yet implemented !"
106
107 #define WORKAROUND_BUGS_LONGTEXT \
108     "Try to fix some bugs\n" \
109     "1  autodetect\n" \
110     "2  old msmpeg4\n" \
111     "4  xvid interlaced\n" \
112     "8  ump4 \n" \
113     "16 no padding\n" \
114     "32 ac vlc" \
115     "64 Qpel chroma"
116
117 /* FIXME (cut/past from ffmpeg */
118 #define LIBAVCODEC_PP_LONGHELP \
119 "<filterName>[:<option>[:<option>...]][[,|/][-]<filterName>[:<option>...]]...\n" \
120 "long form example:\n" \
121 "vdeblock:autoq/hdeblock:autoq/linblenddeint    default,-vdeblock\n" \
122 "short form example:\n" \
123 "vb:a/hb:a/lb                                   de,-vb\n" \
124 "more examples:\n" \
125 "tn:64:128:256\n" \
126 "Filters                        Options\n" \
127 "short  long name       short   long option     Description\n" \
128 "*      *               a       autoq           cpu power dependant enabler\n" \
129 "                       c       chrom           chrominance filtring enabled\n" \
130 "                       y       nochrom         chrominance filtring disabled\n" \
131 "hb     hdeblock        (2 Threshold)           horizontal deblocking filter\n" \
132 "       1. difference factor: default=64, higher -> more deblocking\n" \
133 "       2. flatness threshold: default=40, lower -> more deblocking\n" \
134 "                       the h & v deblocking filters share these\n" \
135 "                       so u cant set different thresholds for h / v\n" \
136 "vb     vdeblock        (2 Threshold)           vertical deblocking filter\n" \
137 "h1     x1hdeblock                              Experimental h deblock filter 1\n" \
138 "v1     x1vdeblock                              Experimental v deblock filter 1\n" \
139 "dr     dering                                  Deringing filter\n" \
140 "al     autolevels                              automatic brightness / contrast\n" \
141 "                       f       fullyrange      stretch luminance to (0..255)\n" \
142 "lb     linblenddeint                           linear blend deinterlacer\n" \
143 "li     linipoldeint                            linear interpolating deinterlace\n" \
144 "ci     cubicipoldeint                          cubic interpolating deinterlacer\n" \
145 "md     mediandeint                             median deinterlacer\n" \
146 "fd     ffmpegdeint                             ffmpeg deinterlacer\n" \
147 "de     default                                 hb:a,vb:a,dr:a,al\n" \
148 "fa     fast                                    h1:a,v1:a,dr:a,al\n" \
149 "tn     tmpnoise        (3 Thresholds)          Temporal Noise Reducer\n" \
150 "                       1. <= 2. <= 3.          larger -> stronger filtering\n" \
151 "fq     forceQuant      <quantizer>             Force quantizer\n"
152
153 vlc_module_begin();
154     add_category_hint( N_("ffmpeg"), NULL, VLC_FALSE );
155     set_capability( "decoder", 70 );
156     set_callbacks( OpenDecoder, NULL );
157     set_description( _("ffmpeg audio/video decoder((MS)MPEG4,SVQ1,H263,WMV,WMA)") );
158
159     add_bool( "ffmpeg-dr", 0, NULL,
160               "direct rendering",
161               "direct rendering", VLC_TRUE );
162     add_integer ( "ffmpeg-error-resilience", -1, NULL,
163                   "error resilience", ERROR_RESILIENCE_LONGTEXT, VLC_TRUE );
164     add_integer ( "ffmpeg-workaround-bugs", 1, NULL,
165                   "workaround bugs", WORKAROUND_BUGS_LONGTEXT, VLC_FALSE );
166     add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT, VLC_FALSE );
167     add_integer( "ffmpeg-truncated", -1, NULL, "truncated stream", "truncated stream -1:auto,0:disable,:1:enable", VLC_FALSE );
168     add_category_hint( N_("Post processing"), NULL, VLC_FALSE );
169
170     add_integer( "ffmpeg-pp-q", 0, NULL,
171                  "post processing quality", POSTPROCESSING_Q_LONGTEXT, VLC_FALSE );
172 #ifdef LIBAVCODEC_PP
173     add_string( "ffmpeg-pp-name", "default", NULL,
174                 "ffmpeg postproc filter chains", LIBAVCODEC_PP_LONGHELP, VLC_TRUE );
175 #else
176     add_module( "ffmpeg-pp", "postprocessing",NULL, NULL,
177                 N_( "ffmpeg postprocessing module" ), NULL, VLC_FALSE );
178     add_bool( "ffmpeg-pp-auto", 0, NULL,
179               "auto-level Post processing quality", POSTPROCESSING_AQ_LONGTEXT, VLC_FALSE );
180     add_bool( "ffmpeg-db-yv", 0, NULL,
181               "force vertical luminance deblocking",
182               "force vertical luminance deblocking (override other settings)", VLC_TRUE );
183     add_bool( "ffmpeg-db-yh", 0, NULL,
184               "force horizontal luminance deblocking",
185               "force horizontal luminance deblocking (override other settings)", VLC_TRUE );
186     add_bool( "ffmpeg-db-cv", 0, NULL,
187               "force vertical chrominance deblocking",
188               "force vertical chrominance deblocking (override other settings)", VLC_TRUE );
189     add_bool( "ffmpeg-db-ch", 0, NULL,
190               "force horizontal chrominance deblocking",
191               "force horizontal chrominance deblocking (override other settings) ", VLC_TRUE );
192     add_bool( "ffmpeg-dr-y", 0, NULL,
193               "force luminance deringing",
194               "force luminance deringing (override other settings)", VLC_TRUE );
195     add_bool( "ffmpeg-dr-c", 0, NULL,
196               "force chrominance deringing",
197               "force chrominance deringing (override other settings)", VLC_TRUE );
198 #endif
199
200     /* chroma conversion submodule */
201     add_submodule();
202     set_capability( "chroma", 50 );
203     set_callbacks( E_(OpenChroma), NULL );
204     set_description( _("ffmpeg chroma conversion") );
205
206 vlc_module_end();
207
208 /*****************************************************************************
209  * OpenDecoder: probe the decoder and return score
210  *****************************************************************************
211  * Tries to launch a decoder and return score so that the interface is able
212  * to chose.
213  *****************************************************************************/
214 static int OpenDecoder( vlc_object_t *p_this )
215 {
216     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
217
218     if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL, NULL ) )
219     {
220         p_fifo->pf_run = RunDecoder;
221         return VLC_SUCCESS;
222     }
223
224     return VLC_EGENERIC;
225 }
226
227 typedef union decoder_thread_u
228 {
229     generic_thread_t gen;
230     adec_thread_t    audio;
231     vdec_thread_t    video;
232
233 } decoder_thread_t;
234
235
236 /*****************************************************************************
237  * RunDecoder: this function is called just after the thread is created
238  *****************************************************************************/
239 static int RunDecoder( decoder_fifo_t *p_fifo )
240 {
241     generic_thread_t *p_decoder;
242     int b_error;
243
244     if ( !(p_decoder = malloc( sizeof( decoder_thread_t ) ) ) )
245     {
246         msg_Err( p_fifo, "out of memory" );
247         DecoderError( p_fifo );
248         return( -1 );
249     }
250     memset( p_decoder, 0, sizeof( decoder_thread_t ) );
251
252     p_decoder->p_fifo = p_fifo;
253
254     if( InitThread( p_decoder ) != 0 )
255     {
256         msg_Err( p_fifo, "initialization failed" );
257         DecoderError( p_fifo );
258         return( -1 );
259     }
260
261     while( (!p_decoder->p_fifo->b_die) && (!p_decoder->p_fifo->b_error) )
262     {
263         switch( p_decoder->i_cat )
264         {
265             case VIDEO_ES:
266                 E_( DecodeThread_Video )( (vdec_thread_t*)p_decoder );
267                 break;
268             case AUDIO_ES:
269                 E_( DecodeThread_Audio )( (adec_thread_t*)p_decoder );
270                 break;
271         }
272     }
273
274     if( ( b_error = p_decoder->p_fifo->b_error ) )
275     {
276         DecoderError( p_decoder->p_fifo );
277     }
278
279     EndThread( p_decoder );
280
281     if( b_error )
282     {
283         return( -1 );
284     }
285
286     return( 0 );
287 }
288
289 /*****************************************************************************
290  *
291  * Functions that initialize, decode and end the decoding process
292  *
293  *****************************************************************************/
294
295 /*****************************************************************************
296  * InitThread: initialize vdec output thread
297  *****************************************************************************
298  * This function is called from decoder_Run and performs the second step
299  * of the initialization. It returns 0 on success. Note that the thread's
300  * flag are not modified inside this function.
301  *
302  * ffmpeg codec will be open, some memory allocated. But Vout is not yet
303  *   open (done after the first decoded frame)
304  *****************************************************************************/
305
306 static int InitThread( generic_thread_t *p_decoder )
307 {
308     int i_result;
309
310      /* *** init ffmpeg library (libavcodec) *** */
311     if( !b_ffmpeginit )
312     {
313         avcodec_init();
314         avcodec_register_all();
315         b_ffmpeginit = 1;
316
317         msg_Dbg( p_decoder->p_fifo, "libavcodec initialized (interface "
318                                     LIBAVCODEC_BUILD_STR ")" );
319     }
320     else
321     {
322         msg_Dbg( p_decoder->p_fifo, "libavcodec already initialized" );
323     }
324
325     /* *** determine codec type *** */
326     ffmpeg_GetFfmpegCodec( p_decoder->p_fifo->i_fourcc,
327                            &p_decoder->i_cat,
328                            &p_decoder->i_codec_id,
329                            &p_decoder->psz_namecodec );
330
331     /* *** ask ffmpeg for a decoder *** */
332     if( !( p_decoder->p_codec =
333                 avcodec_find_decoder( p_decoder->i_codec_id ) ) )
334     {
335         msg_Err( p_decoder->p_fifo,
336                  "codec not found (%s)",
337                  p_decoder->psz_namecodec );
338         return( -1 );
339     }
340
341      /* *** Get a p_context *** */
342     p_decoder->p_context = avcodec_alloc_context();
343
344     switch( p_decoder->i_cat )
345     {
346         case VIDEO_ES:
347             i_result = E_( InitThread_Video )( (vdec_thread_t*)p_decoder );
348             break;
349         case AUDIO_ES:
350             i_result = E_( InitThread_Audio )( (adec_thread_t*)p_decoder );
351             break;
352         default:
353             i_result = -1;
354     }
355
356     p_decoder->pts = 0;
357     p_decoder->p_buffer = NULL;
358     p_decoder->i_buffer = 0;
359     p_decoder->i_buffer_size = 0;
360
361     return( i_result );
362 }
363
364 /*****************************************************************************
365  * EndThread: thread destruction
366  *****************************************************************************
367  * This function is called when the thread ends after a sucessful
368  * initialization.
369  *****************************************************************************/
370 static void EndThread( generic_thread_t *p_decoder )
371 {
372
373     if( !p_decoder )
374     {
375         return;
376     }
377
378     if( p_decoder->p_context != NULL)
379     {
380         FREE( p_decoder->p_context->extradata );
381         avcodec_close( p_decoder->p_context );
382         msg_Dbg( p_decoder->p_fifo,
383                  "ffmpeg codec (%s) stopped",
384                  p_decoder->psz_namecodec );
385         free( p_decoder->p_context );
386     }
387
388     FREE( p_decoder->p_buffer );
389
390     switch( p_decoder->i_cat )
391     {
392         case AUDIO_ES:
393             E_( EndThread_Audio )( (adec_thread_t*)p_decoder );
394             break;
395         case VIDEO_ES:
396             E_( EndThread_Video )( (vdec_thread_t*)p_decoder );
397             break;
398     }
399
400     free( p_decoder );
401 }
402
403 /*****************************************************************************
404  * locales Functions
405  *****************************************************************************/
406
407 int E_( GetPESData )( u8 *p_buf, int i_max, pes_packet_t *p_pes )
408 {
409     int i_copy;
410     int i_count;
411
412     data_packet_t   *p_data;
413
414     i_count = 0;
415     p_data = p_pes->p_first;
416     while( p_data != NULL && i_count < i_max )
417     {
418
419         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
420                         i_max - i_count );
421
422         if( i_copy > 0 )
423         {
424             memcpy( p_buf,
425                     p_data->p_payload_start,
426                     i_copy );
427         }
428
429         p_data = p_data->p_next;
430         i_count += i_copy;
431         p_buf   += i_copy;
432     }
433
434     if( i_count < i_max )
435     {
436         memset( p_buf, 0, i_max - i_count );
437     }
438     return( i_count );
439 }
440
441
442 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t i_fourcc,
443                                   int *pi_cat,
444                                   int *pi_ffmpeg_codec,
445                                   char **ppsz_name )
446 {
447     int i_cat;
448     int i_codec;
449     char *psz_name;
450
451     switch( i_fourcc )
452     {
453 #if 0
454         /* XXX don't use it */
455         case FOURCC_mpgv:
456             i_cat = VIDEO_ES;
457             i_codec = CODEC_ID_MPEG1VIDEO;
458             psz_name = "MPEG-1/2 Video";
459             break;
460 #endif
461         case FOURCC_DIV1:
462         case FOURCC_div1:
463         case FOURCC_MPG4:
464         case FOURCC_mpg4:
465             i_cat = VIDEO_ES;
466             i_codec = CODEC_ID_MSMPEG4V1;
467             psz_name = "MS MPEG-4 v1";
468             break;
469
470         case FOURCC_DIV2:
471         case FOURCC_div2:
472         case FOURCC_MP42:
473         case FOURCC_mp42:
474             i_cat = VIDEO_ES;
475             i_codec = CODEC_ID_MSMPEG4V2;
476             psz_name = "MS MPEG-4 v2";
477             break;
478
479         case FOURCC_MPG3:
480         case FOURCC_mpg3:
481         case FOURCC_div3:
482         case FOURCC_MP43:
483         case FOURCC_mp43:
484         case FOURCC_DIV3:
485         case FOURCC_DIV4:
486         case FOURCC_div4:
487         case FOURCC_DIV5:
488         case FOURCC_div5:
489         case FOURCC_DIV6:
490         case FOURCC_div6:
491         case FOURCC_AP41:
492         case FOURCC_3VID:
493         case FOURCC_3vid:
494         case FOURCC_3IVD:
495         case FOURCC_3ivd:
496             i_cat = VIDEO_ES;
497             i_codec = CODEC_ID_MSMPEG4V3;
498             psz_name = "MS MPEG-4 v3";
499             break;
500
501         case FOURCC_SVQ1:
502             i_cat = VIDEO_ES;
503             i_codec = CODEC_ID_SVQ1;
504             psz_name = "SVQ-1 (Sorenson Video v1)";
505             break;
506
507         case FOURCC_DIVX:
508         case FOURCC_divx:
509         case FOURCC_MP4S:
510         case FOURCC_mp4s:
511         case FOURCC_M4S2:
512         case FOURCC_m4s2:
513         case FOURCC_xvid:
514         case FOURCC_XVID:
515         case FOURCC_XviD:
516         case FOURCC_DX50:
517         case FOURCC_mp4v:
518         case FOURCC_4:
519         /* 3iv1 is unsupported by ffmpeg
520            putting it here gives extreme distorted images
521         case FOURCC_3IV1:
522         case FOURCC_3iv1:
523         */
524         case FOURCC_3IV2:
525         case FOURCC_3iv2:
526             i_cat = VIDEO_ES;
527             i_codec = CODEC_ID_MPEG4;
528             psz_name = "MPEG-4";
529             break;
530 /* FIXME FOURCC_H263P exist but what fourcc ? */
531         case FOURCC_H263:
532         case FOURCC_h263:
533         case FOURCC_U263:
534             i_cat = VIDEO_ES;
535             i_codec = CODEC_ID_H263;
536             psz_name = "H263";
537             break;
538
539         case FOURCC_I263:
540         case FOURCC_i263:
541             i_cat = VIDEO_ES;
542             i_codec = CODEC_ID_H263I;
543             psz_name = "I263.I";
544             break;
545         case FOURCC_WMV1:
546             i_cat = VIDEO_ES;
547             i_codec = CODEC_ID_WMV1;
548             psz_name ="Windows Media Video 1";
549             break;
550         case FOURCC_WMV2:
551         case FOURCC_MSS1:
552             i_cat = VIDEO_ES;
553             i_codec = CODEC_ID_WMV2;
554             psz_name ="Windows Media Video 2";
555             break;
556         case FOURCC_MJPG:
557         case FOURCC_mjpg:
558         case FOURCC_mjpa:
559         case FOURCC_jpeg:
560         case FOURCC_JPEG:
561         case FOURCC_JFIF:
562             i_cat = VIDEO_ES;
563             i_codec = CODEC_ID_MJPEG;
564             psz_name = "Motion JPEG";
565             break;
566         case FOURCC_mjpb:
567             i_cat = VIDEO_ES;
568             i_codec = CODEC_ID_MJPEGB;
569             psz_name = "Motion JPEG B";
570             break;
571         case FOURCC_dvsl:
572         case FOURCC_dvsd:
573         case FOURCC_DVSD:
574         case FOURCC_dvhd:
575         case FOURCC_dvc:
576         case FOURCC_dvp:
577             i_cat = VIDEO_ES;
578             i_codec = CODEC_ID_DVVIDEO;
579             psz_name = "DV video";
580             break;
581
582         case FOURCC_MAC3:
583             i_cat = AUDIO_ES;
584             i_codec = CODEC_ID_MACE3;
585             psz_name = "MACE-3 audio";
586             break;
587         case FOURCC_MAC6:
588             i_cat = AUDIO_ES;
589             i_codec = CODEC_ID_MACE6;
590             psz_name = "MACE-6 audio";
591             break;
592         case FOURCC_dvau:
593             i_cat = AUDIO_ES;
594             i_codec = CODEC_ID_DVAUDIO;
595             psz_name = "DV audio";
596             break;
597
598         case FOURCC_WMA1:
599         case FOURCC_wma1:
600             i_cat = AUDIO_ES;
601             i_codec = CODEC_ID_WMAV1;
602             psz_name ="Windows Media Audio 1";
603             break;
604         case FOURCC_WMA2:
605         case FOURCC_wma2:
606             i_cat = AUDIO_ES;
607             i_codec = CODEC_ID_WMAV2;
608             psz_name ="Windows Media Audio 2";
609             break;
610
611 #if LIBAVCODEC_BUILD >= 4663
612         case FOURCC_IV31:
613         case FOURCC_iv31:
614         case FOURCC_IV32:
615         case FOURCC_iv32:
616             i_cat    = VIDEO_ES;
617             i_codec  = CODEC_ID_INDEO3;
618             psz_name = "Indeo v3";
619             break;
620 #endif
621         default:
622             i_cat = UNKNOWN_ES;
623             i_codec = CODEC_ID_NONE;
624             psz_name = NULL;
625             break;
626     }
627
628     if( i_codec != CODEC_ID_NONE )
629     {
630         if( pi_cat ) *pi_cat = i_cat;
631         if( pi_ffmpeg_codec ) *pi_ffmpeg_codec = i_codec;
632         if( ppsz_name ) *ppsz_name = psz_name;
633         return( VLC_TRUE );
634     }
635
636     return( VLC_FALSE );
637 }
638
639
640