summaryrefslogtreecommitdiffstats
path: root/core/Mpeg2Dec.c
blob: 043c416dc82527d2fa2fc9ae8d8fabd9aa131bed (plain)
1
2
3
4
5
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
/* $Id: Mpeg2Dec.c,v 1.15 2004/05/02 16:25:00 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.m0k.org/>.
   It may be used under the terms of the GNU General Public License. */

#include "HBInternal.h"

#include "mpeg2dec/mpeg2.h"

struct HBWork
{
    HB_WORK_COMMON_MEMBERS

    HBHandle           * handle;
    HBTitle            * title;
    int                  pass;
    mpeg2dec_t         * libmpeg2;
    const mpeg2_info_t * info;
    int                  lateField;
};

/* Local prototypes */
static int Mpeg2DecWork( HBWork * );

HBWork * HBMpeg2DecInit( HBHandle * handle, HBTitle * title )
{
    HBWork * w ;
    if( !( w = malloc( sizeof( HBWork ) ) ) )
    {
        HBLog( "HBMpeg2Dec: malloc() failed, gonna crash" );
        return NULL;
    }

    w->name   = strdup( "Mpeg2Dec" );
    w->work   = Mpeg2DecWork;

    w->handle = handle;
    w->title  = title;

    w->pass          = 42;
    w->libmpeg2      = NULL;
    w->info          = NULL;
    w->lateField     = 0;

    return w;
}

void HBMpeg2DecClose( HBWork ** _w )
{
    HBWork * w = *_w;

    if( w->libmpeg2 )
    {
        HBLog( "HBMpeg2Dec: closing libmpeg2 (pass %d)", w->pass );
        mpeg2_close( w->libmpeg2 );
    }
    free( w->name );
    free( w );

    *_w = NULL;
}

static int Mpeg2DecWork( HBWork * w )
{
    HBTitle     * title = w->title;
    HBBuffer    * mpeg2Buffer;
    HBBuffer    * rawBuffer;
    mpeg2_state_t state;

    if( HBFifoIsHalfFull( title->rawFifo ) )
    {
        return 0;
    }

    /* Get a new buffer to decode */
    if( !( mpeg2Buffer = HBFifoPop( title->inFifo ) ) )
    {
        return 0;
    }

    /* Init or re-init if needed */
    if( mpeg2Buffer->pass != w->pass )
    {
        if( w->libmpeg2 )
        {
            HBLog( "HBMpeg2Dec: closing libmpeg2 (pass %d)", w->pass );
            mpeg2_close( w->libmpeg2 );
        }

        w->pass = mpeg2Buffer->pass;

        HBLog( "HBMpeg2Dec: opening libmpeg2 (pass %d)", w->pass );
#ifdef HB_NOMMX
        mpeg2_accel( 0 );
#endif
        w->libmpeg2  = mpeg2_init();
        w->info      = mpeg2_info( w->libmpeg2 );
        w->lateField = 0;
    }

    /* Decode */
    mpeg2_buffer( w->libmpeg2, mpeg2Buffer->data,
                  mpeg2Buffer->data + mpeg2Buffer->size );

    for( ;; )
    {
        state = mpeg2_parse( w->libmpeg2 );

        if( state == STATE_BUFFER )
        {
            break;
        }
        else if( ( state == STATE_SLICE || state == STATE_END ) &&
                 w->info->display_fbuf )
        {
            rawBuffer = HBBufferInit( 3 * title->inWidth *
                                      title->inHeight );

            /* TODO: make libmpeg2 write directly in our buffer */
            memcpy( rawBuffer->data, w->info->display_fbuf->buf[0],
                    title->inWidth * title->inHeight );
            memcpy( rawBuffer->data + title->inWidth * title->inHeight,
                    w->info->display_fbuf->buf[1],
                    title->inWidth * title->inHeight / 4 );
            memcpy( rawBuffer->data + title->inWidth * title->inHeight +
                        title->inWidth * title->inHeight / 4,
                    w->info->display_fbuf->buf[2],
                    title->inWidth * title->inHeight / 4 );

            rawBuffer->position = mpeg2Buffer->position;
            rawBuffer->pass     = mpeg2Buffer->pass;

            /* NTSC pulldown kludge */
            if( w->info->display_picture->nb_fields == 3 )
            {
                rawBuffer->repeat = w->lateField;
                w->lateField      = !w->lateField;
            }
            else
            {
                rawBuffer->repeat = 0;
            }

            if( !HBFifoPush( title->rawFifo, &rawBuffer ) )
            {
                HBLog( "HBMpeg2Dec: HBFifoPush failed" );
            }
        }
        else if( state == STATE_INVALID )
        {
            /* Shouldn't happen on a DVD */
            HBLog( "HBMpeg2Dec: STATE_INVALID" );
        }
    }

    HBBufferClose( &mpeg2Buffer );

    return 1;
}