asf.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:10k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * asf.c: MMS access plug-in
  3.  *****************************************************************************
  4.  * Copyright (C) 2001-2004 VideoLAN
  5.  * $Id: asf.c 7903 2004-06-05 21:53:24Z fenrir $
  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. #include <stdlib.h>
  24. #include <vlc/vlc.h>
  25. #include "asf.h"
  26. #include "buffer.h"
  27. static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
  28. {
  29.     return( ( p_guid1->v1 == p_guid2->v1 &&
  30.               p_guid1->v2 == p_guid2->v2 &&
  31.               p_guid1->v3 == p_guid2->v3 &&
  32.               p_guid1->v4[0] == p_guid2->v4[0] &&
  33.               p_guid1->v4[1] == p_guid2->v4[1] &&
  34.               p_guid1->v4[2] == p_guid2->v4[2] &&
  35.               p_guid1->v4[3] == p_guid2->v4[3] &&
  36.               p_guid1->v4[4] == p_guid2->v4[4] &&
  37.               p_guid1->v4[5] == p_guid2->v4[5] &&
  38.               p_guid1->v4[6] == p_guid2->v4[6] &&
  39.               p_guid1->v4[7] == p_guid2->v4[7] ) ? 1 : 0 );
  40. }
  41. void E_( GenerateGuid )( guid_t *p_guid )
  42. {
  43.     int i;
  44.     srand( mdate() & 0xffffffff );
  45.     /* FIXME should be generated using random data */
  46.     p_guid->v1 = 0xbabac001;
  47.     p_guid->v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
  48.     p_guid->v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
  49.     for( i = 0; i < 8; i++ )
  50.     {
  51.         p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
  52.     }
  53. }
  54. void E_( asf_HeaderParse )( asf_header_t *hdr,
  55.                             uint8_t *p_header, int i_header )
  56. {
  57.     var_buffer_t buffer;
  58.     guid_t      guid;
  59.     uint64_t    i_size;
  60.     int         i;
  61.     hdr->i_file_size = 0;
  62.     hdr->i_data_packets_count = 0;
  63.     hdr->i_min_data_packet_size = 0;
  64.     for( i = 0; i < 128; i++ )
  65.     {
  66.         hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
  67.         hdr->stream[i].i_selected = 0;
  68.         hdr->stream[i].i_bitrate = -1;
  69.     }
  70.     //fprintf( stderr, " ---------------------header:%dn", i_header );
  71.     var_buffer_initread( &buffer, p_header, i_header );
  72.     var_buffer_getguid( &buffer, &guid );
  73.     if( !CmpGuid( &guid, &asf_object_header_guid ) )
  74.     {
  75. //        XXX Error
  76. //        fprintf( stderr, " ---------------------ERROR------n" );
  77.     }
  78.     var_buffer_getmemory( &buffer, NULL, 30 - 16 );
  79.     for( ;; )
  80.     {
  81.         //fprintf( stderr, " ---------------------data:%dn", buffer.i_data );
  82.         var_buffer_getguid( &buffer, &guid );
  83.         i_size = var_buffer_get64( &buffer );
  84.         //fprintf( stderr, "  guid=0x%8.8x-0x%4.4x-0x%4.4x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x size=%lldn",
  85.         //                  guid.v1,guid.v2, guid.v3,
  86.         //                  guid.v4[0],guid.v4[1],guid.v4[2],guid.v4[3],
  87.         //                  guid.v4[4],guid.v4[5],guid.v4[6],guid.v4[7],
  88.         //                  i_size );
  89.         if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
  90.         {
  91.             var_buffer_getmemory( &buffer, NULL, 16 );
  92.             hdr->i_file_size            = var_buffer_get64( &buffer );
  93.             var_buffer_getmemory( &buffer, NULL, 8 );
  94.             hdr->i_data_packets_count   = var_buffer_get64( &buffer );
  95.             var_buffer_getmemory( &buffer, NULL, 8+8+8+4);
  96.             hdr->i_min_data_packet_size = var_buffer_get32( &buffer );
  97.             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
  98.         }
  99.         else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
  100.         {
  101.             int     i_stream_id;
  102.             guid_t  stream_type;
  103.             var_buffer_getguid( &buffer, &stream_type );
  104.             var_buffer_getmemory( &buffer, NULL, 32 );
  105.             i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
  106.             //fprintf( stderr, " 1---------------------skip:%lldn", i_size - 24 - 32 - 16 - 1 );
  107.             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
  108.             if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
  109.             {
  110.                 //fprintf( stderr, "nvideo stream[%d] foundn", i_stream_id );
  111.                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
  112.             }
  113.             else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
  114.             {
  115.                 //fprintf( stderr, "naudio stream[%d] foundn", i_stream_id );
  116.                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
  117.             }
  118.             else
  119.             {
  120.                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
  121.             }
  122.         }
  123.         else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
  124.         {
  125.             int     i_count;
  126.             uint8_t i_stream_id;
  127.             i_count = var_buffer_get16( &buffer );
  128.             i_size -= 2;
  129.             while( i_count > 0 )
  130.             {
  131.                 i_stream_id = var_buffer_get16( &buffer )&0x7f;
  132.                 hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
  133.                 i_count--;
  134.                 i_size -= 6;
  135.             }
  136.             //fprintf( stderr, " 2---------------------skip:%lldn", i_size - 24);
  137.             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
  138.         }
  139.         else
  140.         {
  141.             //fprintf( stderr, " 3---------------------skip:%lldn", i_size - 24);
  142.             // skip unknown guid
  143.             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
  144.         }
  145.         if( var_buffer_readempty( &buffer ) )
  146.             return;
  147.     }
  148. }
  149. void E_( asf_StreamSelect ) ( asf_header_t *hdr,
  150.                               int i_bitrate_max,
  151.                               vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
  152. {
  153.     /* XXX FIXME use mututal eclusion information */
  154.     int i;
  155.     int i_audio, i_video;
  156.     int i_bitrate_total;
  157. #if 0
  158.     char *psz_stream;
  159. #endif
  160.     i_audio = 0;
  161.     i_video = 0;
  162.     i_bitrate_total = 0;
  163.     if( b_all )
  164.     {
  165.         /* select all valid stream */
  166.         for( i = 1; i < 128; i++ )
  167.         {
  168.             if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
  169.             {
  170.                 hdr->stream[i].i_selected = 1;
  171.             }
  172.         }
  173.         return;
  174.     }
  175.     else
  176.     {
  177.         for( i = 0; i < 128; i++ )
  178.         {
  179.             hdr->stream[i].i_selected = 0; /* by default, not selected */
  180.         }
  181.     }
  182.     /* big test:
  183.      * select a stream if
  184.      *    - no audio nor video stream
  185.      *    - or:
  186.      *         - if i_bitrate_max not set keep the highest bitrate
  187.      *         - if i_bitrate_max is set, keep stream that make we used best
  188.      *           quality regarding i_bitrate_max
  189.      *
  190.      * XXX: little buggy:
  191.      *        - it doesn't use mutual exclusion info..
  192.      *        - when selecting a better stream we could select
  193.      *        something that make i_bitrate_total> i_bitrate_max
  194.      */
  195.     for( i = 1; i < 128; i++ )
  196.     {
  197.         if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
  198.         {
  199.             continue;
  200.         }
  201.         else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
  202.                  ( i_audio <= 0 ||
  203.                     ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
  204.                           ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
  205.                                             < i_bitrate_max || !i_bitrate_max) ) ||
  206.                         ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
  207.                               i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
  208.                       ) )  ) )
  209.         {
  210.             /* unselect old stream */
  211.             if( i_audio > 0 )
  212.             {
  213.                 hdr->stream[i_audio].i_selected = 0;
  214.                 if( hdr->stream[i_audio].i_bitrate> 0 )
  215.                 {
  216.                     i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
  217.                 }
  218.             }
  219.             hdr->stream[i].i_selected = 1;
  220.             if( hdr->stream[i].i_bitrate> 0 )
  221.             {
  222.                 i_bitrate_total += hdr->stream[i].i_bitrate;
  223.             }
  224.             i_audio = i;
  225.         }
  226.         else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
  227.                  ( i_video <= 0 ||
  228.                     (
  229.                         ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
  230.                             ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
  231.                                             < i_bitrate_max || !i_bitrate_max) ) ||
  232.                           ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
  233.                             i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
  234.                         ) ) )  )
  235.         {
  236.             /* unselect old stream */
  237.             if( i_video > 0 )
  238.             {
  239.                 hdr->stream[i_video].i_selected = 0;
  240.                 if( hdr->stream[i_video].i_bitrate> 0 )
  241.                 {
  242.                     i_bitrate_total -= hdr->stream[i_video].i_bitrate;
  243.                 }
  244.             }
  245.             hdr->stream[i].i_selected = 1;
  246.             if( hdr->stream[i].i_bitrate> 0 )
  247.             {
  248.                 i_bitrate_total += hdr->stream[i].i_bitrate;
  249.             }
  250.             i_video = i;
  251.         }
  252.     }
  253. }