demux.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:13k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * demux.c
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 the VideoLAN team
  5.  * $Id: b939c101eb2fdb42bb26dd3b25ff70414bd8f31e $
  6.  *
  7.  * Author: 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include "demux.h"
  27. #include <libvlc.h>
  28. #include <vlc_codec.h>
  29. #include <vlc_meta.h>
  30. static bool SkipID3Tag( demux_t * );
  31. static bool SkipAPETag( demux_t *p_demux );
  32. /*****************************************************************************
  33.  * demux_New:
  34.  *  if s is NULL then load a access_demux
  35.  *****************************************************************************/
  36. demux_t *__demux_New( vlc_object_t *p_obj,
  37.                        const char *psz_access, const char *psz_demux,
  38.                        const char *psz_path,
  39.                        stream_t *s, es_out_t *out, bool b_quick )
  40. {
  41.     static const char typename[] = "demux";
  42.     demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
  43.                                           VLC_OBJECT_GENERIC, typename );
  44.     const char *psz_module;
  45.     if( p_demux == NULL ) return NULL;
  46.     /* Parse URL */
  47.     p_demux->psz_access = strdup( psz_access );
  48.     p_demux->psz_demux  = strdup( psz_demux );
  49.     p_demux->psz_path   = strdup( psz_path );
  50.     /* Take into account "demux" to be able to do :demux=dump */
  51.     if( p_demux->psz_demux && *p_demux->psz_demux == '' )
  52.     {
  53.         free( p_demux->psz_demux );
  54.         p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
  55.         if( p_demux->psz_demux == NULL )
  56.             p_demux->psz_demux = strdup( "" );
  57.     }
  58.     if( !b_quick )
  59.     {
  60.         msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
  61.                  p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
  62.     }
  63.     p_demux->s          = s;
  64.     p_demux->out        = out;
  65.     p_demux->pf_demux   = NULL;
  66.     p_demux->pf_control = NULL;
  67.     p_demux->p_sys      = NULL;
  68.     p_demux->info.i_update = 0;
  69.     p_demux->info.i_title  = 0;
  70.     p_demux->info.i_seekpoint = 0;
  71.     if( s ) psz_module = p_demux->psz_demux;
  72.     else psz_module = p_demux->psz_access;
  73.     if( s && *psz_module == '' && strrchr( p_demux->psz_path, '.' ) )
  74.     {
  75.        /* XXX: add only file without any problem here and with strong detection.
  76.         *  - no .mp3, .a52, ... (aac is added as it works only by file ext
  77.         *     anyway
  78.         *  - wav can't be added 'cause of a52 and dts in them as raw audio
  79.          */
  80.          static const struct { char ext[5]; char demux[9]; } exttodemux[] =
  81.          {
  82.             { "aac",  "aac" },
  83.             { "aiff", "aiff" },
  84.             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
  85.             { "avi",  "avi" },
  86.             { "au",   "au" },
  87.             { "flac", "flac" },
  88.             { "dv",   "dv" },
  89.             { "drc",  "dirac" },
  90.             { "m3u",  "m3u" },
  91.             { "m3u8", "m3u8" },
  92.             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
  93.             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
  94.             { "nsv",  "nsv" },
  95.             { "ogg",  "ogg" }, { "ogm",  "ogg" }, /* legacy Ogg */
  96.             { "oga",  "ogg" }, { "spx",  "ogg" }, { "ogv", "ogg" },
  97.             { "ogx",  "ogg" }, /*RFC5334*/
  98.             { "pva",  "pva" },
  99.             { "rm",   "rm" },
  100.             { "m4v",  "m4v" },
  101.             { "h264", "h264" },
  102.             { "voc",  "voc" },
  103.             { "mid",  "smf" }, { "rmi",  "smf" },
  104.             { "",  "" },
  105.         };
  106.         /* Here, we don't mind if it does not work, it must be quick */
  107.         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
  108.         {
  109.             { "mp3", "mpga" },
  110.             { "ogg", "ogg" },
  111.             { "wma", "asf" },
  112.             { "", "" }
  113.         };
  114.         const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
  115.         int  i;
  116.         if( !b_quick )
  117.         {
  118.             for( i = 0; exttodemux[i].ext[0]; i++ )
  119.             {
  120.                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
  121.                 {
  122.                     psz_module = exttodemux[i].demux;
  123.                     break;
  124.                 }
  125.             }
  126.         }
  127.         else
  128.         {
  129.             for( i = 0; exttodemux_quick[i].ext[0]; i++ )
  130.             {
  131.                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
  132.                 {
  133.                     psz_module = exttodemux_quick[i].demux;
  134.                     break;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     /* Before module_need (for var_Create...) */
  140.     vlc_object_attach( p_demux, p_obj );
  141.     if( s )
  142.     {
  143.         /* ID3/APE tags will mess-up demuxer probing so we skip it here.
  144.          * ID3/APE parsers will called later on in the demuxer to access the
  145.          * skipped info. */
  146.         if( !SkipID3Tag( p_demux ) )
  147.             SkipAPETag( p_demux );
  148.         p_demux->p_module =
  149.             module_need( p_demux, "demux", psz_module,
  150.                          !strcmp( psz_module, p_demux->psz_demux ) ?
  151.                          true : false );
  152.     }
  153.     else
  154.     {
  155.         p_demux->p_module =
  156.             module_need( p_demux, "access_demux", psz_module,
  157.                          !strcmp( psz_module, p_demux->psz_access ) ?
  158.                          true : false );
  159.     }
  160.     if( p_demux->p_module == NULL )
  161.     {
  162.         vlc_object_detach( p_demux );
  163.         free( p_demux->psz_path );
  164.         free( p_demux->psz_demux );
  165.         free( p_demux->psz_access );
  166.         vlc_object_release( p_demux );
  167.         return NULL;
  168.     }
  169.     return p_demux;
  170. }
  171. /*****************************************************************************
  172.  * demux_Delete:
  173.  *****************************************************************************/
  174. void demux_Delete( demux_t *p_demux )
  175. {
  176.     module_unneed( p_demux, p_demux->p_module );
  177.     vlc_object_detach( p_demux );
  178.     free( p_demux->psz_path );
  179.     free( p_demux->psz_demux );
  180.     free( p_demux->psz_access );
  181.     vlc_object_release( p_demux );
  182. }
  183. /*****************************************************************************
  184.  * demux_vaControlHelper:
  185.  *****************************************************************************/
  186. int demux_vaControlHelper( stream_t *s,
  187.                             int64_t i_start, int64_t i_end,
  188.                             int64_t i_bitrate, int i_align,
  189.                             int i_query, va_list args )
  190. {
  191.     int64_t i_tell;
  192.     double  f, *pf;
  193.     int64_t i64, *pi64;
  194.     if( i_end < 0 )    i_end   = stream_Size( s );
  195.     if( i_start < 0 )  i_start = 0;
  196.     if( i_align <= 0 ) i_align = 1;
  197.     i_tell = stream_Tell( s );
  198.     switch( i_query )
  199.     {
  200.         case DEMUX_GET_LENGTH:
  201.             pi64 = (int64_t*)va_arg( args, int64_t * );
  202.             if( i_bitrate > 0 && i_end > i_start )
  203.             {
  204.                 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
  205.                 return VLC_SUCCESS;
  206.             }
  207.             return VLC_EGENERIC;
  208.         case DEMUX_GET_TIME:
  209.             pi64 = (int64_t*)va_arg( args, int64_t * );
  210.             if( i_bitrate > 0 && i_tell >= i_start )
  211.             {
  212.                 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
  213.                 return VLC_SUCCESS;
  214.             }
  215.             return VLC_EGENERIC;
  216.         case DEMUX_GET_POSITION:
  217.             pf = (double*)va_arg( args, double * );
  218.             if( i_start < i_end )
  219.             {
  220.                 *pf = (double)( i_tell - i_start ) /
  221.                       (double)( i_end  - i_start );
  222.                 return VLC_SUCCESS;
  223.             }
  224.             return VLC_EGENERIC;
  225.         case DEMUX_SET_POSITION:
  226.             f = (double)va_arg( args, double );
  227.             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
  228.             {
  229.                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
  230.                 if( stream_Seek( s, i_start + i_block * i_align ) )
  231.                 {
  232.                     return VLC_EGENERIC;
  233.                 }
  234.                 return VLC_SUCCESS;
  235.             }
  236.             return VLC_EGENERIC;
  237.         case DEMUX_SET_TIME:
  238.             i64 = (int64_t)va_arg( args, int64_t );
  239.             if( i_bitrate > 0 && i64 >= 0 )
  240.             {
  241.                 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
  242.                 if( stream_Seek( s, i_start + i_block * i_align ) )
  243.                 {
  244.                     return VLC_EGENERIC;
  245.                 }
  246.                 return VLC_SUCCESS;
  247.             }
  248.             return VLC_EGENERIC;
  249.         case DEMUX_GET_FPS:
  250.         case DEMUX_GET_META:
  251.         case DEMUX_HAS_UNSUPPORTED_META:
  252.         case DEMUX_SET_NEXT_DEMUX_TIME:
  253.         case DEMUX_GET_TITLE_INFO:
  254.         case DEMUX_SET_GROUP:
  255.         case DEMUX_GET_ATTACHMENTS:
  256.         case DEMUX_CAN_RECORD:
  257.         case DEMUX_SET_RECORD_STATE:
  258.             return VLC_EGENERIC;
  259.         default:
  260.             msg_Err( s, "unknown query in demux_vaControlDefault" );
  261.             return VLC_EGENERIC;
  262.     }
  263. }
  264. /****************************************************************************
  265.  * Utility functions
  266.  ****************************************************************************/
  267. decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
  268. {
  269.     decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
  270.     if( !p_packetizer )
  271.     {
  272.         es_format_Clean( p_fmt );
  273.         return NULL;
  274.     }
  275.     p_fmt->b_packetized = false;
  276.     p_packetizer->pf_decode_audio = NULL;
  277.     p_packetizer->pf_decode_video = NULL;
  278.     p_packetizer->pf_decode_sub = NULL;
  279.     p_packetizer->pf_packetize = NULL;
  280.     p_packetizer->fmt_in = *p_fmt;
  281.     es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
  282.     p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
  283.     if( !p_packetizer->p_module )
  284.     {
  285.         es_format_Clean( p_fmt );
  286.         vlc_object_release( p_packetizer );
  287.         msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
  288.         return NULL;
  289.     }
  290.     return p_packetizer;
  291. }
  292. void demux_PacketizerDestroy( decoder_t *p_packetizer )
  293. {
  294.     if( p_packetizer->p_module )
  295.         module_unneed( p_packetizer, p_packetizer->p_module );
  296.     es_format_Clean( &p_packetizer->fmt_in );
  297.     if( p_packetizer->p_description )
  298.         vlc_meta_Delete( p_packetizer->p_description );
  299.     vlc_object_release( p_packetizer );
  300. }
  301. static bool SkipID3Tag( demux_t *p_demux )
  302. {
  303.     const uint8_t *p_peek;
  304.     uint8_t version, revision;
  305.     int i_size;
  306.     int b_footer;
  307.     if( !p_demux->s )
  308.         return false;
  309.     /* Get 10 byte id3 header */
  310.     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
  311.         return false;
  312.     if( memcmp( p_peek, "ID3", 3 ) )
  313.         return false;
  314.     version = p_peek[3];
  315.     revision = p_peek[4];
  316.     b_footer = p_peek[5] & 0x10;
  317.     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
  318.     if( b_footer ) i_size += 10;
  319.     i_size += 10;
  320.     /* Skip the entire tag */
  321.     stream_Read( p_demux->s, NULL, i_size );
  322.     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
  323.              version, revision, i_size );
  324.     return true;
  325. }
  326. static bool SkipAPETag( demux_t *p_demux )
  327. {
  328.     const uint8_t *p_peek;
  329.     int i_version;
  330.     int i_size;
  331.     uint32_t flags;
  332.     if( !p_demux->s )
  333.         return false;
  334.     /* Get 32 byte ape header */
  335.     if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
  336.         return false;
  337.     if( memcmp( p_peek, "APETAGEX", 8 ) )
  338.         return false;
  339.     i_version = GetDWLE( &p_peek[8] );
  340.     flags = GetDWLE( &p_peek[8+4+4] );
  341.     if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
  342.         return false;
  343.     i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
  344.     /* Skip the entire tag */
  345.     stream_Read( p_demux->s, NULL, i_size );
  346.     msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
  347.              i_version/1000, i_size );
  348.     return true;
  349. }