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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * dv.c: a decoder for DV video
  3.  *****************************************************************************
  4.  * Copyright (C) 2002 VideoLAN
  5.  * $Id: dv.c 6961 2004-03-05 17:34:23Z sam $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  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.  * Preamble
  25.  *****************************************************************************/
  26. #include <vlc/vlc.h>
  27. #include <vlc/vout.h>
  28. #include <vlc/decoder.h>
  29. #undef vlc_error /*vlc_error is defined in the libdv headers, but not
  30.                   * used in thes file */
  31. #include <libdv/dv_types.h>
  32. #include <libdv/dv.h>
  33. /*****************************************************************************
  34.  * Local prototypes
  35.  *****************************************************************************/
  36. static int RunDecoder  ( decoder_fifo_t * );
  37. static int OpenDecoder ( vlc_object_t * );
  38. static vlc_fourcc_t GetFourCC   ( dv_sample_t );
  39. /*****************************************************************************
  40.  * Module descriptor
  41.  *****************************************************************************/
  42. vlc_module_begin();
  43.     set_description( _("DV video decoder") );
  44.     set_capability( "decoder", 70 );
  45.     set_callbacks( OpenDecoder, NULL );
  46. vlc_module_end();
  47. /*****************************************************************************
  48.  * OpenDecoder: probe the decoder and return score
  49.  *****************************************************************************
  50.  * The fourcc format for DV is "dvsd"
  51.  *****************************************************************************/
  52. static int OpenDecoder ( vlc_object_t *p_this )
  53. {
  54.     decoder_t *p_dec = (decoder_t*)p_this;
  55.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','s','d') )
  56.     {
  57.         return VLC_EGENERIC;
  58.     }
  59.     p_dec->pf_run = RunDecoder;
  60.     return VLC_SUCCESS;
  61. }
  62. /*****************************************************************************
  63.  * RunDecoder: this function is called just after the thread is created
  64.  *****************************************************************************/
  65. static int RunDecoder ( decoder_fifo_t *p_fifo )
  66. {
  67.     uint8_t *p_buffer;
  68.     pes_packet_t *p_pes = NULL;
  69.     unsigned int i_data = 120000;
  70.     int i_aspect;
  71.     bit_stream_t    bit_stream;
  72.     dv_decoder_t *  p_decoder;
  73.     vout_thread_t * p_vout;
  74.     p_buffer = malloc( i_data );
  75.     if( !p_buffer )
  76.     {
  77.         msg_Err( p_fifo, "out of memory" );
  78.         p_fifo->b_error = 1;
  79.         DecoderError( p_fifo );
  80.         return -1;
  81.     }
  82.     p_decoder = dv_decoder_new( TRUE, FALSE, FALSE );
  83.     if( !p_decoder )
  84.     {
  85.         msg_Err( p_fifo, "cannot start DV decoder" );
  86.         free( p_buffer );
  87.         p_fifo->b_error = 1;
  88.         DecoderError( p_fifo );
  89.         return -1;
  90.     }
  91.     if( InitBitstream( &bit_stream, p_fifo, NULL, NULL ) != VLC_SUCCESS )
  92.     {
  93.         msg_Err( p_fifo, "cannot initialize bitstream" );
  94.         free( p_buffer );
  95.         p_fifo->b_error = 1;
  96.         DecoderError( p_fifo );
  97.         return -1;
  98.     }
  99.     /* Fill the buffer */
  100.     GetChunk( &bit_stream, p_buffer, i_data );
  101.     while( !p_fifo->b_die && !p_fifo->b_error )
  102.     {
  103.         /* Parsing the beginning of the stream */
  104.         if( dv_parse_header( p_decoder, p_buffer ) < 0 )
  105.         {
  106.             msg_Err(p_fifo, "parse error");
  107.             p_fifo->b_error = 1;
  108.             break;
  109.         }
  110.         if( dv_format_wide( p_decoder ) )
  111.         {
  112.             msg_Dbg( p_fifo, "aspect ratio is 4:3" );
  113.             i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  114.         }
  115.         else if( dv_format_normal( p_decoder ) )
  116.         {
  117.             msg_Dbg( p_fifo, "aspect ratio is 16:9" );
  118.             i_aspect = VOUT_ASPECT_FACTOR * 4/3;//16 / 9;
  119.         }
  120.         else
  121.         {
  122.             msg_Dbg( p_fifo, "aspect is square pixels" );
  123.             i_aspect = VOUT_ASPECT_FACTOR
  124.                         * p_decoder->width / p_decoder->height;
  125.         }
  126.         if( p_decoder->frame_size <= i_data )
  127.         {
  128.             /* XXX: what to do? */
  129.             i_data = p_decoder->frame_size;
  130.         }
  131.         else
  132.         {
  133.             p_buffer = realloc( p_buffer, p_decoder->frame_size );
  134.         }
  135.         /* Don't trust the sucker */
  136.         //p_decoder->quality = p_decoder->video->quality;
  137.         p_decoder->quality = DV_QUALITY_BEST;
  138.         p_decoder->prev_frame_decoded = 0;
  139.         /* Spawn a video output if there is none. First we look amongst our
  140.          * children, then we look for any other vout that might be available */
  141.         p_vout = vout_Request( p_fifo, NULL,
  142.                                p_decoder->width, p_decoder->height,
  143.                                GetFourCC( p_decoder->sampling ), i_aspect );
  144.         /* Main loop */
  145.         while( !p_fifo->b_die && !p_fifo->b_error )
  146.         {
  147.             mtime_t i_pts = 0;
  148.             GetChunk( &bit_stream, p_buffer + i_data,
  149.                                    p_decoder->frame_size - i_data );
  150.             i_data = p_decoder->frame_size;
  151.             if( p_pes )
  152.             {
  153.                 input_DeletePES( p_fifo->p_packets_mgt, p_pes );
  154.             }
  155.             input_ExtractPES( p_fifo, &p_pes );
  156.             if( p_pes )
  157.             {
  158.                 /* Don't trust the sucker */
  159.                 //i_pts = p_pes->i_pts + DEFAULT_PTS_DELAY;
  160.                 i_pts = mdate() + DEFAULT_PTS_DELAY;
  161.             }
  162.             if( p_fifo->b_die || p_fifo->b_error )
  163.             {
  164.                 break;
  165.             }
  166.             if( dv_parse_header( p_decoder, p_buffer ) > 0 )
  167.             {
  168.                 msg_Warn(p_fifo, "size changed");
  169.             }
  170.             if( p_vout && ( !p_decoder->prev_frame_decoded
  171.                              || dv_frame_changed( p_decoder ) ) )
  172.             {
  173.                 picture_t *p_pic;
  174.                 uint8_t *pixels[3];
  175.                 int pitches[3], i;
  176.                 while( !(p_pic = vout_CreatePicture( p_vout, 0, 0, 0 ) ) )
  177.                 {
  178.                     if( p_fifo->b_die || p_fifo->b_error )
  179.                     {
  180.                         break;
  181.                     }
  182.                     msleep( VOUT_OUTMEM_SLEEP );
  183.                 }
  184.                 if( !p_pic )
  185.                 {
  186.                     break;
  187.                 }
  188.                 for( i = 0 ; i < p_pic->i_planes ; i++ )
  189.                 {
  190.                     pixels[i] = p_pic->p[i].p_pixels;
  191.                     pitches[i] = p_pic->p[i].i_pitch;
  192.                 }
  193.                 dv_decode_full_frame( p_decoder, p_buffer,
  194.                                       e_dv_color_yuv, pixels, pitches );
  195.                 p_decoder->prev_frame_decoded = 1;
  196.                 vout_DatePicture( p_vout, p_pic, i_pts );
  197.                 vout_DisplayPicture( p_vout, p_pic );
  198.             }
  199.             i_data = 0;
  200.         }
  201.         vout_Request( p_fifo, p_vout, 0, 0, 0, 0 );
  202.     }
  203.     if( p_pes )
  204.     {
  205.         input_DeletePES( p_fifo->p_packets_mgt, p_pes );
  206.     }
  207.     free( p_buffer );
  208.     CloseBitstream( &bit_stream );
  209.     if( p_fifo->b_error )
  210.     {
  211.         DecoderError( p_fifo );
  212.         return -1;
  213.     }
  214.     return 0;
  215. }
  216. static vlc_fourcc_t GetFourCC( dv_sample_t x )
  217. {
  218.     switch( x )
  219.     {
  220.         case e_dv_sample_411: return VLC_FOURCC('Y','U','Y','2');
  221.         case e_dv_sample_420: return VLC_FOURCC('Y','U','Y','2');
  222.         case e_dv_sample_422: return VLC_FOURCC('Y','U','Y','2');
  223.         default: return 0;
  224.     }
  225. }