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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * vout_synchro.c : frame dropping routines
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: vout_synchro.c 8013 2004-06-22 19:31:54Z fenrir $
  6.  *
  7.  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  8.  *          Samuel Hocevar <sam@via.ecp.fr>
  9.  *          Jean-Marc Dressler <polux@via.ecp.fr>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  24.  *****************************************************************************/
  25. /*
  26.  * DISCUSSION : How to Write an efficient Frame-Dropping Algorithm
  27.  * ==========
  28.  *
  29.  * This implementation is based on mathematical and statistical
  30.  * developments. Older implementations used an enslavement, considering
  31.  * that if we're late when reading an I picture, we will decode one frame
  32.  * less. It had a tendancy to derive, and wasn't responsive enough, which
  33.  * would have caused trouble with the stream control stuff.
  34.  *
  35.  * 1. Structure of a picture stream
  36.  *    =============================
  37.  * Between 2 I's, we have for instance :
  38.  *    I   B   P   B   P   B   P   B   P   B   P   B   I
  39.  *    t0  t1  t2  t3  t4  t5  t6  t7  t8  t9  t10 t11 t12
  40.  * Please bear in mind that B's and IP's will be inverted when displaying
  41.  * (decoding order != presentation order). Thus, t1 < t0.
  42.  *
  43.  * 2. Definitions
  44.  *    ===========
  45.  * t[0..12]     : Presentation timestamps of pictures 0..12.
  46.  * t            : Current timestamp, at the moment of the decoding.
  47.  * T            : Picture period, T = 1/frame_rate.
  48.  * tau[I,P,B]   : Mean time to decode an [I,P,B] picture.
  49.  * tauYUV       : Mean time to render a picture (given by the video_output).
  50.  * tau碵I,P,B] = 2 * tau[I,P,B] + tauYUV
  51.  *              : Mean time + typical difference (estimated to tau/2, that
  52.  *                needs to be confirmed) + render time.
  53.  * DELTA        : A given error margin.
  54.  *
  55.  * 3. General considerations
  56.  *    ======================
  57.  * We define three types of machines :
  58.  *      14T > tauI : machines capable of decoding all I pictures
  59.  *      2T > tauP  : machines capable of decoding all P pictures
  60.  *      T > tauB   : machines capable of decoding all B pictures
  61.  *
  62.  * 4. Decoding of an I picture
  63.  *    ========================
  64.  * On fast machines, we decode all I's.
  65.  * Otherwise :
  66.  * We can decode an I picture if we simply have enough time to decode it
  67.  * before displaying :
  68.  *      t0 - t > tau碔 + DELTA
  69.  *
  70.  * 5. Decoding of a P picture
  71.  *    =======================
  72.  * On fast machines, we decode all P's.
  73.  * Otherwise :
  74.  * First criterion : have time to decode it.
  75.  *      t2 - t > tau碢 + DELTA
  76.  *
  77.  * Second criterion : it shouldn't prevent us from displaying the forthcoming
  78.  * I picture, which is more important.
  79.  *      t12 - t > tau碢 + tau碔 + DELTA
  80.  *
  81.  * 6. Decoding of a B picture
  82.  *    =======================
  83.  * On fast machines, we decode all B's. Otherwise :
  84.  *      t1 - t > tau碆 + DELTA
  85.  * Since the next displayed I or P is already decoded, we don't have to
  86.  * worry about it.
  87.  *
  88.  * I hope you will have a pleasant flight and do not forget your life
  89.  * jacket.
  90.  *                                                  --Meuuh (2000-12-29)
  91.  */
  92. /*****************************************************************************
  93.  * Preamble
  94.  *****************************************************************************/
  95. #include <stdlib.h>                                                /* free() */
  96. #include <string.h>                                    /* memcpy(), memset() */
  97. #include <vlc/vlc.h>
  98. #include <vlc/vout.h>
  99. #include <vlc/input.h>
  100. #include "vout_synchro.h"
  101. /*
  102.  * Local prototypes
  103.  */
  104. /* Error margins */
  105. #define DELTA                   (int)(0.075*CLOCK_FREQ)
  106. #define MAX_VALID_TAU           (int)(0.3*CLOCK_FREQ)
  107. #define DEFAULT_NB_P            5
  108. #define DEFAULT_NB_B            1
  109. /*****************************************************************************
  110.  * vout_SynchroInit : You know what ?
  111.  *****************************************************************************/
  112. vout_synchro_t * __vout_SynchroInit( vlc_object_t * p_object,
  113.                                      int i_frame_rate )
  114. {
  115.     vout_synchro_t * p_synchro = vlc_object_create( p_object,
  116.                                                   sizeof(vout_synchro_t) );
  117.     if ( p_synchro == NULL )
  118.     {
  119.         msg_Err( p_object, "out of memory" );
  120.         return NULL;
  121.     }
  122.     vlc_object_attach( p_synchro, p_object );
  123.     /* We use a fake stream pattern, which is often right. */
  124.     p_synchro->i_n_p = p_synchro->i_eta_p = DEFAULT_NB_P;
  125.     p_synchro->i_n_b = p_synchro->i_eta_b = DEFAULT_NB_B;
  126.     memset( p_synchro->p_tau, 0, 4 * sizeof(mtime_t) );
  127.     memset( p_synchro->pi_meaningful, 0, 4 * sizeof(unsigned int) );
  128.     p_synchro->i_nb_ref = 0;
  129.     p_synchro->i_trash_nb_ref = p_synchro->i_dec_nb_ref = 0;
  130.     p_synchro->current_pts = mdate() + DEFAULT_PTS_DELAY;
  131.     p_synchro->backward_pts = 0;
  132.     p_synchro->i_current_period = p_synchro->i_backward_period = 0;
  133.     p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic =
  134.         p_synchro->i_pic = 0;
  135.     p_synchro->i_frame_rate = i_frame_rate;
  136.     return p_synchro;
  137. }
  138. /*****************************************************************************
  139.  * vout_SynchroRelease : You know what ?
  140.  *****************************************************************************/
  141. void vout_SynchroRelease( vout_synchro_t * p_synchro )
  142. {
  143.     vlc_object_detach( p_synchro );
  144.     vlc_object_destroy( p_synchro );
  145. }
  146. /*****************************************************************************
  147.  * vout_SynchroReset : Reset the reference picture counter
  148.  *****************************************************************************/
  149. void vout_SynchroReset( vout_synchro_t * p_synchro )
  150. {
  151.     p_synchro->i_nb_ref = 0;
  152.     p_synchro->i_trash_nb_ref = p_synchro->i_dec_nb_ref = 0;
  153. }
  154. /*****************************************************************************
  155.  * vout_SynchroChoose : Decide whether we will decode a picture or not
  156.  *****************************************************************************/
  157. vlc_bool_t vout_SynchroChoose( vout_synchro_t * p_synchro, int i_coding_type,
  158.                                int i_render_time )
  159. {
  160. #define TAU_PRIME( coding_type )    (p_synchro->p_tau[(coding_type)] 
  161.                                     + (p_synchro->p_tau[(coding_type)] >> 1) 
  162.                                     + p_synchro->i_render_time)
  163. #define S (*p_synchro)
  164.     /* VPAR_SYNCHRO_DEFAULT */
  165.     mtime_t         now, period;
  166.     mtime_t         pts = 0;
  167.     vlc_bool_t      b_decode = 0;
  168.     now = mdate();
  169.     period = 1000000 * 1001 / p_synchro->i_frame_rate
  170.                      * p_synchro->i_current_rate / INPUT_RATE_DEFAULT;
  171.     p_synchro->i_render_time = i_render_time;
  172.     switch( i_coding_type )
  173.     {
  174.     case I_CODING_TYPE:
  175.         if( S.backward_pts )
  176.         {
  177.             pts = S.backward_pts;
  178.         }
  179.         else
  180.         {
  181.             /* displaying order : B B P B B I
  182.              *                      ^       ^
  183.              *                      |       +- current picture
  184.              *                      +- current PTS
  185.              */
  186.             pts = S.current_pts + period * (S.i_n_b + 2);
  187.         }
  188.         if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
  189.                 S.p_tau[I_CODING_TYPE] )
  190.         {
  191.             b_decode = 1;
  192.         }
  193.         else
  194.         {
  195.             b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
  196.         }
  197.         if( !b_decode )
  198.         {
  199.             msg_Warn( p_synchro,
  200.                       "synchro trashing I ("I64Fd")", pts - now );
  201.         }
  202.         break;
  203.     case P_CODING_TYPE:
  204.         if( S.backward_pts )
  205.         {
  206.             pts = S.backward_pts;
  207.         }
  208.         else
  209.         {
  210.             pts = S.current_pts + period * (S.i_n_b + 1);
  211.         }
  212.         if( p_synchro->i_nb_ref < 1 )
  213.         {
  214.             b_decode = 0;
  215.         }
  216.         else if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
  217.                 S.p_tau[I_CODING_TYPE] )
  218.         {
  219.             if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
  220.             {
  221.                 /* Security in case we're _really_ late */
  222.                 b_decode = (pts - now > 0);
  223.             }
  224.             else
  225.             {
  226.                 b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
  227.                 /* next I */
  228.                 b_decode &= (pts - now
  229.                               + period
  230.                           * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 ))
  231.                             > (TAU_PRIME(P_CODING_TYPE)
  232.                                 + TAU_PRIME(I_CODING_TYPE) + DELTA);
  233.             }
  234.         }
  235.         else
  236.         {
  237.             b_decode = 0;
  238.         }
  239.         break;
  240.     case B_CODING_TYPE:
  241.         pts = S.current_pts;
  242.         if( p_synchro->i_nb_ref < 2 )
  243.         {
  244.             b_decode = 0;
  245.         }
  246.         else if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
  247.         {
  248.             b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
  249.         }
  250.         else
  251.         {
  252.             b_decode = 0;
  253.         }
  254.     }
  255.     if( !b_decode )
  256.     {
  257.         S.i_not_chosen_pic++;
  258.     }
  259.     return( b_decode );
  260. #undef S
  261. #undef TAU_PRIME
  262. }
  263. /*****************************************************************************
  264.  * vout_SynchroTrash : Update counters when we trash a picture
  265.  *****************************************************************************/
  266. void vout_SynchroTrash( vout_synchro_t * p_synchro )
  267. {
  268.     p_synchro->i_trashed_pic++;
  269.     p_synchro->i_nb_ref = p_synchro->i_trash_nb_ref;
  270. }
  271. /*****************************************************************************
  272.  * vout_SynchroDecode : Update timers when we decide to decode a picture
  273.  *****************************************************************************/
  274. void vout_SynchroDecode( vout_synchro_t * p_synchro )
  275. {
  276.     p_synchro->decoding_start = mdate();
  277.     p_synchro->i_nb_ref = p_synchro->i_dec_nb_ref;
  278. }
  279. /*****************************************************************************
  280.  * vout_SynchroEnd : Called when the image is totally decoded
  281.  *****************************************************************************/
  282. void vout_SynchroEnd( vout_synchro_t * p_synchro, int i_coding_type,
  283.                       vlc_bool_t b_garbage )
  284. {
  285.     mtime_t     tau;
  286.     if( !b_garbage )
  287.     {
  288.         tau = mdate() - p_synchro->decoding_start;
  289.         /* If duration too high, something happened (pause ?), so don't
  290.          * take it into account. */
  291.         if( tau < 3 * p_synchro->p_tau[i_coding_type]
  292.              || ( !p_synchro->pi_meaningful[i_coding_type]
  293.                    && tau < MAX_VALID_TAU ) )
  294.         {
  295.             /* Mean with average tau, to ensure stability. */
  296.             p_synchro->p_tau[i_coding_type] =
  297.                 (p_synchro->pi_meaningful[i_coding_type]
  298.                  * p_synchro->p_tau[i_coding_type] + tau)
  299.                 / (p_synchro->pi_meaningful[i_coding_type] + 1);
  300.             if( p_synchro->pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
  301.             {
  302.                 p_synchro->pi_meaningful[i_coding_type]++;
  303.             }
  304.         }
  305.     }
  306. }
  307. /*****************************************************************************
  308.  * vout_SynchroDate : When an image has been decoded, ask for its date
  309.  *****************************************************************************/
  310. mtime_t vout_SynchroDate( vout_synchro_t * p_synchro )
  311. {
  312.     /* No need to lock, since PTS are only used by the video parser. */
  313.     return p_synchro->current_pts;
  314. }
  315. /*****************************************************************************
  316.  * vout_SynchroNewPicture: Update stream structure and PTS
  317.  *****************************************************************************/
  318. void vout_SynchroNewPicture( vout_synchro_t * p_synchro, int i_coding_type,
  319.                              int i_repeat_field, mtime_t next_pts,
  320.                              mtime_t next_dts, int i_current_rate )
  321. {
  322.     mtime_t         period = 1000000 * 1001 / p_synchro->i_frame_rate
  323.                               * i_current_rate / INPUT_RATE_DEFAULT;
  324. #if 0
  325.     mtime_t         now = mdate();
  326. #endif
  327.     p_synchro->i_current_rate = i_current_rate;
  328.     switch( i_coding_type )
  329.     {
  330.     case I_CODING_TYPE:
  331.         if( p_synchro->i_eta_p
  332.              && p_synchro->i_eta_p != p_synchro->i_n_p )
  333.         {
  334.             msg_Dbg( p_synchro,
  335.                      "stream periodicity changed from P[%d] to P[%d]",
  336.                      p_synchro->i_n_p, p_synchro->i_eta_p );
  337.             p_synchro->i_n_p = p_synchro->i_eta_p;
  338.         }
  339.         p_synchro->i_eta_p = p_synchro->i_eta_b = 0;
  340.         p_synchro->i_trash_nb_ref = 0;
  341.         if( p_synchro->i_nb_ref < 2 )
  342.             p_synchro->i_dec_nb_ref = p_synchro->i_nb_ref + 1;
  343.         else
  344.             p_synchro->i_dec_nb_ref = p_synchro->i_nb_ref;
  345. #if 0
  346.         msg_Dbg( p_synchro, "I("I64Fd") P("I64Fd")[%d] B("I64Fd")"
  347.               "[%d] YUV("I64Fd") : trashed %d:%d/%d",
  348.               p_synchro->p_tau[I_CODING_TYPE],
  349.               p_synchro->p_tau[P_CODING_TYPE],
  350.               p_synchro->i_n_p,
  351.               p_synchro->p_tau[B_CODING_TYPE],
  352.               p_synchro->i_n_b,
  353.               p_synchro->i_render_time,
  354.               p_synchro->i_not_chosen_pic,
  355.               p_synchro->i_trashed_pic -
  356.               p_synchro->i_not_chosen_pic,
  357.               p_synchro->i_pic );
  358.         p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic
  359.             = p_synchro->i_pic = 0;
  360. #else
  361.         if ( p_synchro->i_pic >= 100 )
  362.         {
  363.             msg_Dbg( p_synchro, "decoded %d/%d pictures",
  364.                      p_synchro->i_pic
  365.                        - p_synchro->i_trashed_pic,
  366.                      p_synchro->i_pic );
  367.             p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic
  368.                 = p_synchro->i_pic = 0;
  369.         }
  370. #endif
  371.         break;
  372.     case P_CODING_TYPE:
  373.         p_synchro->i_eta_p++;
  374.         if( p_synchro->i_eta_b
  375.              && p_synchro->i_eta_b != p_synchro->i_n_b )
  376.         {
  377.             msg_Dbg( p_synchro,
  378.                      "stream periodicity changed from B[%d] to B[%d]",
  379.                      p_synchro->i_n_b, p_synchro->i_eta_b );
  380.             p_synchro->i_n_b = p_synchro->i_eta_b;
  381.         }
  382.         p_synchro->i_eta_b = 0;
  383.         p_synchro->i_dec_nb_ref = 2;
  384.         p_synchro->i_trash_nb_ref = 0;
  385.         break;
  386.     case B_CODING_TYPE:
  387.         p_synchro->i_eta_b++;
  388.         p_synchro->i_dec_nb_ref = p_synchro->i_trash_nb_ref
  389.             = p_synchro->i_nb_ref;
  390.         break;
  391.     }
  392.     p_synchro->current_pts += p_synchro->i_current_period
  393.                                         * (period >> 1);
  394. #define PTS_THRESHOLD   (period >> 2)
  395.     if( i_coding_type == B_CODING_TYPE )
  396.     {
  397.         /* A video frame can be displayed 1, 2 or 3 times, according to
  398.          * repeat_first_field, top_field_first, progressive_sequence and
  399.          * progressive_frame. */
  400.         p_synchro->i_current_period = i_repeat_field;
  401.         if( next_pts )
  402.         {
  403.             if( next_pts - p_synchro->current_pts
  404.                     > PTS_THRESHOLD
  405.                  || p_synchro->current_pts - next_pts
  406.                     > PTS_THRESHOLD )
  407.             {
  408.                 msg_Warn( p_synchro, "vout synchro warning: pts != "
  409.                           "current_date ("I64Fd")",
  410.                           p_synchro->current_pts
  411.                               - next_pts );
  412.             }
  413.             p_synchro->current_pts = next_pts;
  414.         }
  415.     }
  416.     else
  417.     {
  418.         p_synchro->i_current_period = p_synchro->i_backward_period;
  419.         p_synchro->i_backward_period = i_repeat_field;
  420.         if( p_synchro->backward_pts )
  421.         {
  422.             if( next_dts &&
  423.                 (next_dts - p_synchro->backward_pts
  424.                     > PTS_THRESHOLD
  425.               || p_synchro->backward_pts - next_dts
  426.                     > PTS_THRESHOLD) )
  427.             {
  428.                 msg_Warn( p_synchro, "backward_pts != dts ("I64Fd")",
  429.                            next_dts
  430.                                - p_synchro->backward_pts );
  431.             }
  432.             if( p_synchro->backward_pts - p_synchro->current_pts
  433.                     > PTS_THRESHOLD
  434.                  || p_synchro->current_pts - p_synchro->backward_pts
  435.                     > PTS_THRESHOLD )
  436.             {
  437.                 msg_Warn( p_synchro,
  438.                           "backward_pts != current_pts ("I64Fd")",
  439.                           p_synchro->current_pts
  440.                               - p_synchro->backward_pts );
  441.             }
  442.             p_synchro->current_pts = p_synchro->backward_pts;
  443.             p_synchro->backward_pts = 0;
  444.         }
  445.         else if( next_dts )
  446.         {
  447.             if( next_dts - p_synchro->current_pts
  448.                     > PTS_THRESHOLD
  449.                  || p_synchro->current_pts - next_dts
  450.                     > PTS_THRESHOLD )
  451.             {
  452.                 msg_Warn( p_synchro, "dts != current_pts ("I64Fd")",
  453.                           p_synchro->current_pts
  454.                               - next_dts );
  455.             }
  456.             /* By definition of a DTS. */
  457.             p_synchro->current_pts = next_dts;
  458.             next_dts = 0;
  459.         }
  460.         if( next_pts )
  461.         {
  462.             /* Store the PTS for the next time we have to date an I picture. */
  463.             p_synchro->backward_pts = next_pts;
  464.             next_pts = 0;
  465.         }
  466.     }
  467. #undef PTS_THRESHOLD
  468. #if 0
  469.     /* Removed for incompatibility with slow motion */
  470.     if( p_synchro->current_pts + DEFAULT_PTS_DELAY < now )
  471.     {
  472.         /* We cannot be _that_ late, something must have happened, reinit
  473.          * the dates. */
  474.         msg_Warn( p_synchro, "PTS << now ("I64Fd"), resetting",
  475.                    now - p_synchro->current_pts - DEFAULT_PTS_DELAY );
  476.         p_synchro->current_pts = now + DEFAULT_PTS_DELAY;
  477.     }
  478.     if( p_synchro->backward_pts
  479.          && p_synchro->backward_pts + DEFAULT_PTS_DELAY < now )
  480.     {
  481.         /* The same. */
  482.         p_synchro->backward_pts = 0;
  483.     }
  484. #endif
  485.     p_synchro->i_pic++;
  486. }