File: revelcore.cpp

package info (click to toggle)
asc 2.6.1.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 81,740 kB
  • sloc: cpp: 158,704; sh: 11,544; ansic: 6,736; makefile: 604; perl: 138
file content (178 lines) | stat: -rw-r--r-- 4,738 bytes parent folder | download | duplicates (8)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright (C) (2004) (Cort Stratton) <cort at cortstratton dot 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "revel.h"
#include "BaseEncoder.h"

#include "XvidEncoder.h"

#include <cassert>
#include <cstdio>
#include <string>
using std::string;
#include <map>
using std::map;

namespace
{
    typedef map<int,Revel_BaseEncoder*> EncoderMap;
    typedef EncoderMap::iterator EncoderMapItor;
    EncoderMap g_encoders;
    int g_nextHandle = 0;

    bool IsEncoderValid(const int handle)
    {
        EncoderMapItor itor = g_encoders.find(handle);
        if (itor == g_encoders.end())
            return false;
        return true;
    }

    Revel_Error EncoderFactory(const Revel_VideoCodec codec,
        Revel_BaseEncoder **encoderOut)
    {
        Revel_BaseEncoder *enc = NULL;
        
        switch(codec)
        {
        case REVEL_CD_XVID:
            enc = new Revel_XvidEncoder;
            break;
        default:
            return REVEL_ERR_PARAMS;
        }

        if (enc == NULL)
            return REVEL_ERR_MEMORY;
        *encoderOut = enc;
        return REVEL_ERR_NONE;
    }
}

int Revel_GetVersion(void)
{
    return REVEL_VERSION;
}
int Revel_GetApiVersion(void)
{
    return REVEL_API_VERSION;
}

Revel_Error Revel_CreateEncoder(int *encoderHandle)
{
    if (encoderHandle == NULL)
        return REVEL_ERR_PARAMS;

    *encoderHandle = g_nextHandle++;
    g_encoders[*encoderHandle] = NULL;
    
    return REVEL_ERR_NONE;
}


void Revel_InitializeParams(Revel_Params *params)
{
    if (params == NULL)
        return;
    params->width = params->height = 1;
    params->hasAudio = 0;
    params->audioBits = 16;
    params->audioChannels = 2;
    params->audioRate = 44100;
    params->audioSampleFormat = REVEL_ASF_UNKNOWN;
    params->codec = REVEL_CD_XVID;
    params->frameRate = 30;
    params->quality = 1.0f;
}


Revel_Error Revel_EncodeStart(int encoderHandle, const char* filename,
                              Revel_Params *params)
{
    if (filename == NULL || params == NULL)
        return REVEL_ERR_PARAMS;
    if (!IsEncoderValid(encoderHandle))
        return REVEL_ERR_INVALID_HANDLE;

    // Create the codec-specific encoder if it doesn't already exist.
    if (g_encoders[encoderHandle] == NULL)
    {
        Revel_BaseEncoder *newEnc = NULL;
        Revel_Error revError = EncoderFactory(params->codec, &newEnc);
        if (revError != REVEL_ERR_NONE)
            return revError;
        g_encoders[encoderHandle] = newEnc;
    }

    return g_encoders[encoderHandle]->EncodeStart(filename, *params);
}


Revel_Error Revel_EncodeFrame(int encoderHandle,
                              Revel_VideoFrame *frame, int *frameSize)
{
    if (frame == NULL)
        return REVEL_ERR_PARAMS;
    if (!IsEncoderValid(encoderHandle))
        return REVEL_ERR_INVALID_HANDLE;
    if (g_encoders[encoderHandle] == NULL)
        return REVEL_ERR_BUSY;
    return g_encoders[encoderHandle]->EncodeFrame(*frame, frameSize);
}


Revel_Error Revel_EncodeAudio(int encoderHandle, void *sampleBuffer,
                              int numBytes, int *numTotalBytes)
{
    if (!IsEncoderValid(encoderHandle))
        return REVEL_ERR_INVALID_HANDLE;
    if (g_encoders[encoderHandle] == NULL)
        return REVEL_ERR_BUSY;
    return g_encoders[encoderHandle]->EncodeAudio(sampleBuffer, numBytes,
        numTotalBytes);
}


Revel_Error Revel_EncodeEnd(int encoderHandle, int *totalSize)
{
    if (!IsEncoderValid(encoderHandle))
        return REVEL_ERR_INVALID_HANDLE;
    if (g_encoders[encoderHandle] == NULL)
        return REVEL_ERR_BUSY;
    return g_encoders[encoderHandle]->EncodeEnd(totalSize);
}



Revel_Error Revel_DestroyEncoder(int encoderHandle)
{
    if (!IsEncoderValid(encoderHandle))
        return REVEL_ERR_NONE; // okay to delete invalid handles.

    // Delete the encoder, if there is one.
    if (g_encoders[encoderHandle] != NULL)
    {
        delete g_encoders[encoderHandle];
        g_encoders[encoderHandle] = NULL;
    }

    // Finally, remove the map entry.
    g_encoders.erase(encoderHandle);
    return REVEL_ERR_NONE;
}