summaryrefslogtreecommitdiffstats
path: root/core/Mpeg2Decoder.cpp
blob: 1ba4945bcf2cca20c0a592e250920780d5bc654c (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
/* $Id: Mpeg2Decoder.cpp,v 1.14 2003/09/30 14:38:15 titer Exp $

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

#include "Mpeg2Decoder.h"
#include "Manager.h"
#include "Fifo.h"

extern "C" {
#include <mpeg2dec/mpeg2.h>
}
#include <ffmpeg/avcodec.h>

HBMpeg2Decoder::HBMpeg2Decoder( HBManager * manager, HBTitle * title )
    : HBThread( "mpeg2decoder" )
{
    fManager     = manager;
    fTitle       = title;
}

void HBMpeg2Decoder::DoWork()
{
    if( !( fMpeg2Buffer = Pop( fTitle->fMpeg2Fifo ) ) )
    {
        return;
    }

    fPass = fMpeg2Buffer->fPass;
    Init();

    do
    {
        while( fSuspend )
        {
            Snooze( 10000 );
        }

        if( fMpeg2Buffer->fPass != fPass )
        {
            Close();
            fPass = fMpeg2Buffer->fPass;
            Init();
        }

        DecodeBuffer();
    }
    while( ( fMpeg2Buffer = Pop( fTitle->fMpeg2Fifo ) ) );
}

void HBMpeg2Decoder::Init()
{
    fLateField = false;

    /* Init libmpeg2 */
    fHandle = mpeg2_init();
}

void HBMpeg2Decoder::Close()
{
    /* Close libmpeg2 */
    mpeg2_close( fHandle );
}

void HBMpeg2Decoder::DecodeBuffer()
{
    const mpeg2_info_t * info = mpeg2_info( fHandle );

    /* Feed libmpeg2 */
    mpeg2_buffer( fHandle, fMpeg2Buffer->fData,
                  fMpeg2Buffer->fData + fMpeg2Buffer->fSize );

    mpeg2_state_t state;
    for( ;; )
    {
        state = mpeg2_parse( fHandle );

        if( state == STATE_BUFFER )
        {
            break;
        }
        else if( ( state == STATE_SLICE || state == STATE_END ) &&
                   info->display_fbuf )
        {
            HBBuffer * rawBuffer = new HBBuffer( 3 * fTitle->fInWidth *
                                                 fTitle->fInHeight / 2 );

            /* TODO : make libmpeg2 write directly in our buffer */
            memcpy( rawBuffer->fData,
                    info->display_fbuf->buf[0],
                    fTitle->fInWidth * fTitle->fInHeight );
            memcpy( rawBuffer->fData + fTitle->fInWidth *
                        fTitle->fInHeight,
                    info->display_fbuf->buf[1],
                    fTitle->fInWidth * fTitle->fInHeight / 4 );
            memcpy( rawBuffer->fData + fTitle->fInWidth *
                        fTitle->fInHeight + fTitle->fInWidth *
                        fTitle->fInHeight / 4,
                    info->display_fbuf->buf[2],
                    fTitle->fInWidth * fTitle->fInHeight / 4 );

            rawBuffer->fPosition = fMpeg2Buffer->fPosition;
            rawBuffer->fPass     = fPass;

            /* NTSC pulldown kludge */
            if( info->display_picture->nb_fields == 3 )
            {
                if( fLateField )
                {
                    HBBuffer * buffer =
                        new HBBuffer( rawBuffer->fSize );
                    buffer->fPosition = rawBuffer->fPosition;
                    buffer->fPass     = rawBuffer->fPass;
                    memcpy( buffer->fData, rawBuffer->fData,
                            buffer->fSize );
                    Push( fTitle->fRawFifo, buffer );
                }
                fLateField = !fLateField;
            }

            /* Send it to the encoder */
            if( !( Push( fTitle->fRawFifo, rawBuffer ) ) )
            {
                break;
            }
        }
        else if( state == STATE_INVALID )
        {
            /* Shouldn't happen on a DVD */
            Log( "HBMpeg2Decoder : STATE_INVALID" );
        }
    }

    delete fMpeg2Buffer;
}