dv.c 9.27 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * dv.c: a decoder for DV video
 *****************************************************************************
 * Copyright (C) 2002 VideoLAN
5
 * $Id: dv.c,v 1.2 2002/08/12 09:34:15 sam Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *      
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/decoder.h>

#include <libdv/dv_types.h>
#include <libdv/dv.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int RunDecoder  ( decoder_fifo_t * );
static int OpenDecoder ( vlc_object_t * );

static u32 GetFourCC   ( dv_sample_t );
static pes_packet_t *GetFirstPES( decoder_fifo_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("DV video decoder") );
    set_capability( "decoder", 70 );
    set_callbacks( OpenDecoder, NULL );
vlc_module_end();

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************
 * The fourcc format for DV is "dvsd"
 *****************************************************************************/
static int OpenDecoder ( vlc_object_t *p_this )
{
    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;

    if( p_fifo->i_fourcc != VLC_FOURCC('d','v','s','d') )
    {
        return VLC_EGENERIC;
    }

    p_fifo->pf_run = RunDecoder;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * RunDecoder: this function is called just after the thread is created
 *****************************************************************************/
static int RunDecoder ( decoder_fifo_t *p_fifo )
{
    u8 *p_buffer;
    int i_data = 120000;
    int i_aspect;

    bit_stream_t    bit_stream;
    dv_decoder_t *  p_decoder;
    vout_thread_t * p_vout;
    
    p_buffer = malloc( i_data );
    if( !p_buffer )
    {
        msg_Err( p_fifo, "out of memory" );
        p_fifo->b_error = 1;
        DecoderError( p_fifo );
        return -1;
    }

    p_decoder = dv_decoder_new( TRUE, FALSE, FALSE );
    if( !p_decoder )
    {
        msg_Err( p_fifo, "cannot create DV decoder" );
        free( p_buffer );
        p_fifo->b_error = 1;
        DecoderError( p_fifo );
        return -1;
    }

    InitBitstream( &bit_stream, p_fifo, NULL, NULL );

    /* Fill the buffer */
    GetChunk( &bit_stream, p_buffer, i_data );

    while( !p_fifo->b_die && !p_fifo->b_error )
    {
        /* Parsing the beginning of the stream */
        if( dv_parse_header( p_decoder, p_buffer ) < 0 )
        {
            fprintf(stderr, "parse error\n");
            p_fifo->b_error = 1;
            break;
        }

        if( dv_format_wide( p_decoder ) )
        {
            msg_Dbg( p_fifo, "aspect is 4:3" );
            i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
        }
        else if( dv_format_normal( p_decoder ) )
        {
            msg_Dbg( p_fifo, "aspect is 16:9" );
            i_aspect = VOUT_ASPECT_FACTOR * 4/3;//16 / 9;
        }
        else
        {
            msg_Dbg( p_fifo, "aspect is square pixels" );
            i_aspect = VOUT_ASPECT_FACTOR
                        * p_decoder->width / p_decoder->height;
        }

        if( p_decoder->frame_size <= i_data )
        {
            /* XXX: what to do? */
            i_data = p_decoder->frame_size;
        }
        else
        {
            p_buffer = realloc( p_buffer, p_decoder->frame_size );
        }
    
        /* Don't trust the sucker */
        //p_decoder->quality = p_decoder->video->quality;
        p_decoder->quality = DV_QUALITY_BEST;
        p_decoder->prev_frame_decoded = 0;

        /* Spawn a video output if there is none. First we look amongst our
         * children, then we look for any other vout that might be available */
        p_vout = vlc_object_find( p_fifo, VLC_OBJECT_VOUT, FIND_CHILD );
        if( !p_vout ) 
        {
            p_vout = vlc_object_find( p_fifo, VLC_OBJECT_VOUT, FIND_ANYWHERE );
        }

        if( p_vout )
        {
            if( p_vout->render.i_width != p_decoder->width
             || p_vout->render.i_height != p_decoder->height
             || p_vout->render.i_chroma != GetFourCC( p_decoder->sampling )
             || p_vout->render.i_aspect != i_aspect )
            {
                /* We are not interested in this format, close this vout */
165
                vlc_object_detach( p_vout );
166 167 168 169 170 171 172
                vlc_object_release( p_vout );
                vout_DestroyThread( p_vout );
                p_vout = NULL;
            }
            else
            {
                /* This video output is cool! Hijack it. */
173
                vlc_object_detach( p_vout );
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
                vlc_object_attach( p_vout, p_fifo );
                vlc_object_release( p_vout );
            }
        }

        if( !p_vout )
        {
            msg_Dbg( p_fifo, "no vout present, spawning one" );

            p_vout = vout_CreateThread( p_fifo,
                                        p_decoder->width, p_decoder->height,
                                        GetFourCC( p_decoder->sampling ),
                                        i_aspect );
        }

        /* Main loop */
        while( !p_fifo->b_die && !p_fifo->b_error )
        {
            mtime_t i_pts = 0;
            pes_packet_t *p_pes;

            GetChunk( &bit_stream, p_buffer + i_data,
                                   p_decoder->frame_size - i_data );
            i_data = p_decoder->frame_size;

            p_pes = GetFirstPES( p_fifo );

            if( p_pes )
            {
                /* Don't trust the sucker */
                //i_pts = p_pes->i_pts + DEFAULT_PTS_DELAY;
                i_pts = mdate() + DEFAULT_PTS_DELAY;
            }

            if( p_fifo->b_die || p_fifo->b_error )
            {
                break;
            }

            if( dv_parse_header( p_decoder, p_buffer ) > 0 )
            {
                fprintf(stderr, "size changed\n");
            }

            if( p_vout && ( !p_decoder->prev_frame_decoded
                             || dv_frame_changed( p_decoder ) ) )
            {
                picture_t *p_pic;
                u8 *pixels[3];
                int pitches[3], i;

                while( !(p_pic = vout_CreatePicture( p_vout, 0, 0, 0 ) ) )
                {
                    if( p_fifo->b_die || p_fifo->b_error )
                    {
                        break;
                    } 
                    msleep( VOUT_OUTMEM_SLEEP );
                }

                if( !p_pic )
                {
                    break;
                }

                for( i = 0 ; i < p_pic->i_planes ; i++ )
                {
                    pixels[i] = p_pic->p[i].p_pixels;
                    pitches[i] = p_pic->p[i].i_pitch;
                }

                dv_decode_full_frame( p_decoder, p_buffer,
                                      e_dv_color_yuv, pixels, pitches );
                p_decoder->prev_frame_decoded = 1;

                vout_DatePicture( p_vout, p_pic, i_pts );
                vout_DisplayPicture( p_vout, p_pic );
            }

            i_data = 0;
        }

        if( p_vout )
        {
            vout_DestroyThread( p_vout );
        }
    }

    free( p_buffer );

    if( p_fifo->b_error )
    {
        DecoderError( p_fifo );
        return -1;
    }

    return 0;
}

static u32 GetFourCC( dv_sample_t x )
{
    switch( x )
    {
        case e_dv_sample_411: return VLC_FOURCC('Y','U','Y','2');
        case e_dv_sample_420: return VLC_FOURCC('Y','U','Y','2');
        case e_dv_sample_422: return VLC_FOURCC('Y','U','Y','2');
        default: return 0;
    }
}

static pes_packet_t *GetFirstPES( decoder_fifo_t *p_fifo )
{
    pes_packet_t *p_pes;

    vlc_mutex_lock( &p_fifo->data_lock );

    /* if fifo is empty wait */
    while( !p_fifo->p_first )
    {
        if( p_fifo->b_die )
        {
            vlc_mutex_unlock( &p_fifo->data_lock );
            return NULL;
        } 
        vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
    }
    p_pes = p_fifo->p_first;

    vlc_mutex_unlock( &p_fifo->data_lock );
    
    return p_pes;
}