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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * transrate.c: MPEG2 video transrating module
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 VideoLAN
  5.  * $Id: transrate.c 8162 2004-07-10 17:20:59Z fenrir $
  6.  *
  7.  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  8.  *          Laurent Aimar <fenrir@via.ecp.fr>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define NDEBUG 1
  30. #include <assert.h>
  31. #include <math.h>
  32. #include <vlc/vlc.h>
  33. #include <vlc/sout.h>
  34. #include <vlc/input.h>
  35. #include "transrate.h"
  36. /*****************************************************************************
  37.  * Exported prototypes
  38.  *****************************************************************************/
  39. static int      Open    ( vlc_object_t * );
  40. static void     Close   ( vlc_object_t * );
  41. static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
  42. static int               Del ( sout_stream_t *, sout_stream_id_t * );
  43. static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
  44. static int  transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
  45. /*****************************************************************************
  46.  * Module descriptor
  47.  *****************************************************************************/
  48. vlc_module_begin();
  49.     set_description( _("MPEG2 video transrating stream output") );
  50.     set_capability( "sout stream", 50 );
  51.     add_shortcut( "transrate" );
  52.     set_callbacks( Open, Close );
  53. vlc_module_end();
  54. struct sout_stream_sys_t
  55. {
  56.     sout_stream_t   *p_out;
  57.     int             i_vbitrate;
  58.     mtime_t         i_shaping_delay;
  59.     int             b_mpeg4_matrix;
  60.     mtime_t         i_dts, i_pts;
  61. };
  62. /*****************************************************************************
  63.  * Open:
  64.  *****************************************************************************/
  65. static int Open( vlc_object_t *p_this )
  66. {
  67.     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
  68.     sout_stream_sys_t *p_sys;
  69.     char *val;
  70.     p_sys = malloc( sizeof( sout_stream_sys_t ) );
  71.     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
  72.     p_sys->i_vbitrate   = 0;
  73.     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
  74.     {
  75.         p_sys->i_vbitrate = atoi( val );
  76.         if( p_sys->i_vbitrate < 16000 )
  77.         {
  78.             p_sys->i_vbitrate *= 1000;
  79.         }
  80.     }
  81.     else
  82.     {
  83.         p_sys->i_vbitrate = 3000000;
  84.     }
  85.     p_sys->i_shaping_delay = 500000;
  86.     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "shaping" ) ) )
  87.     {
  88.         p_sys->i_shaping_delay = (int64_t)atoi( val ) * 1000;
  89.         if( p_sys->i_shaping_delay <= 0 )
  90.         {
  91.             msg_Err( p_stream,
  92.                      "invalid shaping ("I64Fd"ms) reseting to 500ms",
  93.                      p_sys->i_shaping_delay / 1000 );
  94.             p_sys->i_shaping_delay = 500000;
  95.         }
  96.     }
  97.     p_sys->b_mpeg4_matrix = 0;
  98.     if( sout_cfg_find( p_stream->p_cfg, "mpeg4-matrix" ) )
  99.     {
  100.         p_sys->b_mpeg4_matrix = 1;
  101.     }
  102.     msg_Dbg( p_stream, "codec video %dkb/s max gop="I64Fd"us",
  103.              p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
  104.     if( !p_sys->p_out )
  105.     {
  106.         msg_Err( p_stream, "cannot create chain" );
  107.         free( p_sys );
  108.         return VLC_EGENERIC;
  109.     }
  110.     p_stream->pf_add    = Add;
  111.     p_stream->pf_del    = Del;
  112.     p_stream->pf_send   = Send;
  113.     p_stream->p_sys     = p_sys;
  114.     return VLC_SUCCESS;
  115. }
  116. /*****************************************************************************
  117.  * Close:
  118.  *****************************************************************************/
  119. static void Close( vlc_object_t * p_this )
  120. {
  121.     sout_stream_t       *p_stream = (sout_stream_t *)p_this;
  122.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  123.     sout_StreamDelete( p_sys->p_out );
  124.     free( p_sys );
  125. }
  126. static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
  127. {
  128.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  129.     sout_stream_id_t    *id;
  130.     id = malloc( sizeof( sout_stream_id_t ) );
  131.     id->id = NULL;
  132.     if( p_fmt->i_cat == VIDEO_ES
  133.             && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
  134.     {
  135.         msg_Dbg( p_stream,
  136.                  "creating video transrating for fcc=`%4.4s'",
  137.                  (char*)&p_fmt->i_codec );
  138.         id->p_current_buffer = NULL;
  139.         id->p_next_gop = NULL;
  140.         id->i_next_gop_duration = 0;
  141.         id->i_next_gop_size = 0;
  142.         memset( &id->tr, 0, sizeof( transrate_t ) );
  143.         id->tr.bs.i_byte_in = id->tr.bs.i_byte_out = 0;
  144.         id->tr.mpeg4_matrix = p_sys->b_mpeg4_matrix;
  145.         /* open output stream */
  146.         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
  147.         id->b_transrate = VLC_TRUE;
  148.     }
  149.     else
  150.     {
  151.         msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
  152.         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
  153.         id->b_transrate = VLC_FALSE;
  154.         if( id->id == NULL )
  155.         {
  156.             free( id );
  157.             return NULL;
  158.         }
  159.     }
  160.     return id;
  161. }
  162. static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
  163. {
  164.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  165.     if( id->id )
  166.     {
  167.         p_sys->p_out->pf_del( p_sys->p_out, id->id );
  168.     }
  169.     free( id );
  170.     return VLC_SUCCESS;
  171. }
  172. static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
  173.                  block_t *p_buffer )
  174. {
  175.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  176.     if( id->b_transrate )
  177.     {
  178.         block_t *p_buffer_out;
  179.         /* be sure to have at least 8 bytes of padding (maybe only 4) */
  180.         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + 8 );
  181.         p_buffer->i_buffer -= 8;
  182.         memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, 8 );
  183.         transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
  184.         if( p_buffer_out )
  185.         {
  186.             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
  187.         }
  188.         return VLC_SUCCESS;
  189.     }
  190.     else if( id->id != NULL )
  191.     {
  192.         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
  193.     }
  194.     else
  195.     {
  196.         block_Release( p_buffer );
  197.         return VLC_EGENERIC;
  198.     }
  199. }
  200. static int transrate_video_process( sout_stream_t *p_stream,
  201.                sout_stream_id_t *id, block_t *in, block_t **out )
  202. {
  203.     transrate_t    *tr = &id->tr;
  204.     bs_transrate_t *bs = &tr->bs;
  205.     *out = NULL;
  206.     while ( in != NULL )
  207.     {
  208.         block_t * p_next = in->p_next;
  209.         int i_flags = in->i_flags;
  210.         in->p_next = NULL;
  211.         block_ChainAppend( &id->p_next_gop, in );
  212.         id->i_next_gop_duration += in->i_length;
  213.         id->i_next_gop_size += in->i_buffer;
  214.         in = p_next;
  215.         if( ((i_flags & BLOCK_FLAG_TYPE_I )
  216.                 && id->i_next_gop_duration >= 300000)
  217.               || (id->i_next_gop_duration > p_stream->p_sys->i_shaping_delay) )
  218.         {
  219.             mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
  220.                                     / (id->i_next_gop_duration / 1000);
  221.             mtime_t i_new_bitrate;
  222.             id->tr.i_total_input = id->i_next_gop_size;
  223.             id->tr.i_remaining_input = id->i_next_gop_size;
  224.             id->tr.i_wanted_output = (p_stream->p_sys->i_vbitrate)
  225.                                     * (id->i_next_gop_duration / 1000) / 8000;
  226.             id->tr.i_current_output = 0;
  227.             id->p_current_buffer = id->p_next_gop;
  228.             while ( id->p_current_buffer != NULL )
  229.             {
  230.                 block_t * p_next = id->p_current_buffer->p_next;
  231.                 if ( !p_stream->p_sys->b_mpeg4_matrix
  232.                        && id->tr.i_wanted_output >= id->tr.i_total_input )
  233.                 {
  234.                     bs->i_byte_out += id->p_current_buffer->i_buffer;
  235.                     id->p_current_buffer->p_next = NULL;
  236.                     block_ChainAppend( out, id->p_current_buffer );
  237.                 }
  238.                 else
  239.                 {
  240.                     if ( process_frame( p_stream, id, id->p_current_buffer,
  241.                                         out, 0 ) < 0 )
  242.                     {
  243.                         id->p_current_buffer->p_next = NULL;
  244.                         block_ChainAppend( out, id->p_current_buffer );
  245.                         if ( p_stream->p_sys->b_mpeg4_matrix )
  246.                             id->tr.i_wanted_output = id->tr.i_total_input;
  247.                     }
  248.                     else
  249.                     {
  250.                         block_Release( id->p_current_buffer );
  251.                     }
  252.                 }
  253.                 id->p_current_buffer = p_next;
  254.             }
  255.             if ( id->tr.i_wanted_output < id->tr.i_total_input )
  256.             {
  257.                 i_new_bitrate = (mtime_t)tr->i_current_output * 8000
  258.                                     / (id->i_next_gop_duration / 1000);
  259.                 if (i_new_bitrate > p_stream->p_sys->i_vbitrate + 300000)
  260.                     msg_Err(p_stream, "%lld -> %lld d=%lld",
  261.                             i_bitrate, i_new_bitrate,
  262.                             id->i_next_gop_duration);
  263.                 else
  264.                     msg_Dbg(p_stream, "%lld -> %lld d=%lld",
  265.                             i_bitrate, i_new_bitrate,
  266.                             id->i_next_gop_duration);
  267.             }
  268.             id->p_next_gop = NULL;
  269.             id->i_next_gop_duration = 0;
  270.             id->i_next_gop_size = 0;
  271.         }
  272.     }
  273.     return VLC_SUCCESS;
  274. }