summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/libtc/tcframes.c
blob: ff9fe6acaccd8c01e67f6ef2d24cdec65fa93bc5 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
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
/*
 * tcframes.c -- common generic audio/video/whatever frame allocation/disposal
 *               routines for transcode.
 * (C) 2005-2010 - Francesco Romani <fromani -at- gmail -dot- com>
 *
 * This file is part of transcode, a video stream processing tool.
 *
 * transcode 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.
 *
 * transcode 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, see <http://www.gnu.org/licenses/>.
 */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "src/tc_defaults.h"

#include "tcframes.h"

int tc_video_planes_size(size_t psizes[3],
                         int width, int height, int format)
{
    switch (format) {
      case CODEC_RAW: /* worst case paranoia, fallback */
      case CODEC_RAW_RGB: /* worst case paranoia again, fallback */
      case CODEC_RAW_YUV: /* worst case paranoia again, fallback */
      case CODEC_RGB: /* backward compatibility, fallback */
      case TC_CODEC_RGB:
        psizes[0] = width * height;
        psizes[1] = width * height;
        psizes[2] = width * height;
        break;
      case CODEC_YUV422: /* backward compatibility, fallback */
      case TC_CODEC_YUV422P:
        psizes[0] = width * height;
        psizes[1] = width * height / 2;
        psizes[2] = width * height / 2;
        break;
      case CODEC_YUV: /* backward compatibility, fallback */
      case TC_CODEC_YUV420P:
        psizes[0] = width * height;
        psizes[1] = width * height / 4;
        psizes[2] = width * height / 4;
        break;
      default: /* unknown */
        psizes[0] = 0;
        psizes[1] = 0;
        psizes[2] = 0;
        return TC_NULL_MATCH;
    }
    return 0;
}

/* blank )set to zero) rightmost X bits of I */
#define TRUNC_VALUE(i, x)	(((i) >> (x)) << (x))

size_t tc_audio_frame_size(double samples, int channels,
                           int bits, int *adjust)
{
    double rawsize = samples * (bits/8) * channels;
    size_t asize = TRUNC_VALUE(((size_t)rawsize), 2);
    int leap1 = 0, leap2 = 0, leap = 0;

    leap1 = TC_LEAP_FRAME * (rawsize - asize);
    leap2 = -leap1 + TC_LEAP_FRAME * (bits/8) * channels;
    leap1 = TRUNC_VALUE(leap1, 2);
    leap2 = TRUNC_VALUE(leap2, 2);

    if (leap1 < leap2) {
        leap = leap1;
    } else {
        leap = -leap2;
    	asize += (bits/8) * channels;
    }
    *adjust = leap;
    return asize;
}

#undef TRUNC_VALUE

void tc_init_video_frame(vframe_list_t *vptr,
                         int width, int height, int format)
{
    size_t psizes[3];

    tc_video_planes_size(psizes, width, height, format);

    vptr->video_buf_RGB[0] = vptr->internal_video_buf_0;
    vptr->video_buf_RGB[1] = vptr->internal_video_buf_1;

    vptr->video_buf_Y[0] = vptr->internal_video_buf_0;
    vptr->video_buf_U[0] = vptr->video_buf_Y[0] + psizes[0];
    vptr->video_buf_V[0] = vptr->video_buf_U[0] + psizes[1];

    vptr->video_buf_Y[1] = vptr->internal_video_buf_1;
    vptr->video_buf_U[1] = vptr->video_buf_Y[1] + psizes[0];
    vptr->video_buf_V[1] = vptr->video_buf_U[1] + psizes[1];

    vptr->video_buf = vptr->internal_video_buf_0;
    vptr->video_buf2 = vptr->internal_video_buf_1;
    vptr->free = 1;

    vptr->video_size = psizes[0] + psizes[1] + psizes[2];
    vptr->video_len = vptr->video_size; /* default */
}

void tc_init_audio_frame(aframe_list_t *aptr,
                         double samples, int channels, int bits)
{
    int unused = 0;
    
    aptr->audio_size = tc_audio_frame_size(samples, channels, bits,
                                           &unused);
    aptr->audio_buf = aptr->internal_audio_buf;
}


vframe_list_t *tc_new_video_frame(int width, int height, int format,
                                  int partial)
{
    vframe_list_t *vptr = NULL;
    size_t psizes[3] = { 0, 0, 0 };

    int ret = tc_video_planes_size(psizes, width, height, format);
    if (ret == 0) {
        vptr = tc_alloc_video_frame(psizes[0] + psizes[1] + psizes[2],
                                    partial);

        if (vptr != NULL) {
            tc_init_video_frame(vptr, width, height, format);
        }
    }
    return vptr;
}

aframe_list_t *tc_new_audio_frame(double samples, int channels, int bits)
{
    aframe_list_t *aptr = NULL;
    int unused = 0;
    size_t asize = tc_audio_frame_size(samples, channels, bits, &unused);

    aptr = tc_alloc_audio_frame(asize);

    if (aptr != NULL) {
        tc_init_audio_frame(aptr, samples, channels, bits);
    }
    return aptr;
}

#define TC_FRAME_EXTRA_SIZE     128

/* 
 * About TC_FRAME_EXTRA_SIZE:
 *
 * This is an emergency parachute for codecs that delivers
 * encoded frames *larger* than raw ones. Such beasts exists,
 * even LZO does it in some (AFAIK uncommon) circumstances.
 *
 * On those cases, the Sane Thing To Do from the encoder
 * viewpoint is to deliver an header + payload content, where
 * 'header' is a standard frame header with one flag set
 * (1 bit in the best, very unlikely, case) meaning that 
 * following payload is uncompressed.
 *
 * So, EXTRA_SIZE is supposed to catch such (corner) cases
 * by providing enough extra data for sane headers (for example,
 * LZO header is 16 byte long).
 *
 * Please note that this issue affects only
 * demultiplexor -> decoder and
 * encoder -> multiplexor communications.
 *
 * Yes, that's a bit hackish. Anyone has a better, more generic
 * and clean solution? Remember that frames must be pre-allocated,
 * allocating them on-demand isn't a viable alternative.
 */

vframe_list_t *tc_alloc_video_frame(size_t size, int partial)
{
    vframe_list_t *vptr = tc_zalloc(sizeof(vframe_list_t));

#ifdef TC_FRAME_EXTRA_SIZE
    size += TC_FRAME_EXTRA_SIZE;
#endif

    if (vptr != NULL) {
#ifdef STATBUFFER
        vptr->internal_video_buf_0 = tc_bufalloc(size);
        if (vptr->internal_video_buf_0 == NULL) {
            tc_free(vptr);
            return NULL;
        }
        if (!partial) {
            vptr->internal_video_buf_1 = tc_bufalloc(size);
            if (vptr->internal_video_buf_1 == NULL) {
                tc_buffree(vptr->internal_video_buf_0);
                tc_free(vptr);
                return NULL;
            }
        } else {
            vptr->internal_video_buf_1 = NULL;
        }
        vptr->video_size = size;
#endif /* STATBUFFER */
    }
    return vptr;

}

aframe_list_t *tc_alloc_audio_frame(size_t size)
{
    aframe_list_t *aptr = tc_zalloc(sizeof(aframe_list_t));

#ifdef TC_FRAME_EXTRA_SIZE
    size += TC_FRAME_EXTRA_SIZE;
#endif

    if (aptr != NULL) {
#ifdef STATBUFFER
        aptr->internal_audio_buf = tc_bufalloc(size);
        if (aptr->internal_audio_buf == NULL) {
            tc_free(aptr);
            return NULL;
        }
        aptr->audio_size = size;
#endif /* STATBUFFER */
    }
    return aptr;
}

void tc_del_video_frame(vframe_list_t *vptr)
{
    if (vptr != NULL) {
#ifdef STATBUFFER
        if (vptr->internal_video_buf_1 != NULL) {
            tc_buffree(vptr->internal_video_buf_1);
        }
        tc_buffree(vptr->internal_video_buf_0);
#endif
        tc_free(vptr);
    }
}

void tc_del_audio_frame(aframe_list_t *aptr)
{
    if (aptr != NULL) {
#ifdef STATBUFFER
        tc_buffree(aptr->internal_audio_buf);
#endif
        tc_free(aptr);
    }
}

/*************************************************************************/

/*
 * Local variables:
 *   c-file-style: "stroustrup"
 *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
 *   indent-tabs-mode: nil
 * End:
 *
 * vim: expandtab shiftwidth=4:
 */