summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/export/export_dvraw.c
blob: b96100b2095ecc81866527224bc86c5a098bb887 (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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 *  export_dvraw.c
 *
 *  Copyright (C) Thomas Oestreich - June 2001
 *
 *  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, 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define MOD_NAME    "export_dvraw.so"
#define MOD_VERSION "v0.4.1 (2007-08-17)"
#define MOD_CODEC   "(video) Digital Video | (audio) PCM"

#include "transcode.h"
#include "libtc/libtc.h"
#include "libtc/optstr.h"
#include "libtcvideo/tcvideo.h"

#include <stdio.h>
#include <stdlib.h>
#include <libdv/dv.h>

static int verbose_flag=TC_QUIET;
static int capability_flag=TC_CAP_PCM|TC_CAP_RGB|TC_CAP_YUV|TC_CAP_VID|TC_CAP_YUV422;

#define MOD_PRE dvraw
#include "export_def.h"

static int fd = -1;

/* only 2 channesl supported */
#define MAX_CHANNELS 2
static int16_t *audio_bufs[MAX_CHANNELS] = { NULL, NULL };

static uint8_t *target = NULL, *vbuf = NULL;

static dv_encoder_t *encoder = NULL;
static uint8_t *pixels[3] = { NULL, NULL, NULL }, *tmp_buf = NULL;
static TCVHandle tcvhandle;

static int frame_size = 0, format = 0;
static int pass_through = 0;

static int chans = 0, rate = 0;
static int dv_yuy2_mode = 0;
static int dv_uyvy_mode = 0;
static int is_PAL = 0;

/* ------------------------------------------------------------
 *
 * init codec
 *
 * ------------------------------------------------------------*/

MOD_init
{
    int i;

    if (param->flag == TC_VIDEO) {
        is_PAL = (vob->ex_v_height == PAL_H);
        target = tc_bufalloc(TC_FRAME_DV_PAL);
        vbuf   = tc_bufalloc(PAL_W * PAL_H * 3);

        tcvhandle = tcv_init();

        if (vob->dv_yuy2_mode == 1) {
            tmp_buf = tc_bufalloc(PAL_W * PAL_H * 2); //max frame
            dv_yuy2_mode = 1;
        }

        if (vob->im_v_codec == CODEC_YUV422) {
            tmp_buf = tc_bufalloc(PAL_W * PAL_H * 2); //max frame
            dv_uyvy_mode = 1;
        }

        encoder = dv_encoder_new(FALSE, FALSE, FALSE);
        return TC_OK;
    }

    if (param->flag == TC_AUDIO) {
        // tmp audio buffer
        for ( i = 0; i < MAX_CHANNELS; i++) {
            audio_bufs[i] = tc_malloc(DV_AUDIO_MAX_SAMPLES * sizeof(int16_t));
            if (!(audio_bufs[i])) {
                tc_log_error(MOD_NAME, "out of memory");
                return TC_ERROR;
            }
        }
        return TC_OK;
    }

    return(TC_ERROR);
}

/* ------------------------------------------------------------
 *
 * open outputfile
 *
 * ------------------------------------------------------------*/

MOD_open
{
    int bytealignment = 0, bytespersecond = 0, bytesperframe = 0;

    if (param->flag == TC_VIDEO) {
        fd = open(vob->video_out_file, O_RDWR|O_CREAT|O_TRUNC,
		          S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
        if (fd < 0) {
            tc_log_perror(MOD_NAME, "open file");
            return TC_ERROR;
        }

        switch (vob->im_v_codec) {
          case CODEC_RGB:
            format = 0;
            if (verbose >= TC_DEBUG)
                tc_log_info(MOD_NAME, "raw format is RGB");
            break;
          case CODEC_YUV:
            format = 1;
            if (verbose >= TC_DEBUG)
                tc_log_info(MOD_NAME, "raw format is YUV420P");
            break;
          case CODEC_YUV422:
            format = 2;
            if (verbose >= TC_DEBUG)
                tc_log_info(MOD_NAME, "raw format is YUV422");
            break;
          case CODEC_RAW:
          case CODEC_RAW_YUV:
            format = 1;
            pass_through = 1;
            break;
          default:
            tc_log_warn(MOD_NAME, "codec not supported");
            return TC_ERROR;
        }

        frame_size = (is_PAL) ?TC_FRAME_DV_PAL :TC_FRAME_DV_NTSC;

        if (verbose >= TC_DEBUG)
            tc_log_info(MOD_NAME, "encoding to %s DV",
                        (is_PAL) ?"PAL" :"NTSC");

        // Store aspect ratio - ex_asr uses the value 3 for 16x9 (XXX: tricky)
        encoder->is16x9 = (((vob->ex_asr < 0) ?vob->im_asr :vob->ex_asr) == 3);
        encoder->isPAL = is_PAL;
        encoder->vlc_encode_passes = 3;
        encoder->static_qno = 0;
        if (vob->ex_v_string != NULL)
            if (optstr_get(vob->ex_v_string, "qno", "%d", &encoder->static_qno) == 1)
                tc_log_info(MOD_NAME, "using quantisation: %d", encoder->static_qno);
        encoder->force_dct = DV_DCT_AUTO;

        return TC_OK;
    }

    if (param->flag == TC_AUDIO) {
        if (!encoder) {
            tc_log_warn(MOD_NAME, "-y XXX,dvraw is not possible without the video");
            tc_log_warn(MOD_NAME, "export module also being dvraw");
            return TC_ERROR;
        }
        chans = vob->dm_chan;
        //re-sampling only with -J resample possible
        rate = vob->a_rate;

        bytealignment = (chans == 2) ?4 :2;
        bytespersecond = rate * bytealignment;
        bytesperframe = bytespersecond/(is_PAL ?25 :30);

        if(verbose >= TC_DEBUG)
            tc_log_info(MOD_NAME, "audio: CH=%d, f=%d, balign=%d, bps=%d, bpf=%d",
		                chans, rate, bytealignment, bytespersecond, bytesperframe);

        return TC_OK;
    }
    return TC_ERROR;
}

/* ------------------------------------------------------------
 *
 * encode and export
 *
 * ------------------------------------------------------------*/

MOD_encode
{
    int i;

    if (param->flag == TC_VIDEO) {
        if (pass_through) {
            ac_memcpy(target, param->buffer, frame_size);
        } else {
            ac_memcpy(vbuf, param->buffer, param->size);
        }

        return TC_OK;
    }

    if (param->flag == TC_AUDIO) {
        int16_t *abufs[2] = { audio_bufs[0], audio_bufs[1] }; /* working copies */
        time_t now = time(NULL);
        int achans = chans;

        if (!pass_through) {
            if (dv_uyvy_mode) {
                tcv_convert(tcvhandle,
                		    vbuf, tmp_buf, PAL_W, (encoder->isPAL) ? PAL_H : NTSC_H,
		                    (format==2) ? IMG_YUV422P : IMG_YUV420P, IMG_UYVY);
                pixels[0] = pixels[1] = pixels[2] = tmp_buf;
            } else if (dv_yuy2_mode) {
            	tcv_convert(tcvhandle,
		                    vbuf, tmp_buf, PAL_W, (encoder->isPAL) ? PAL_H : NTSC_H,
                		    (format==2) ? IMG_YUV422P : IMG_YUV420P, IMG_YUY2);
            	pixels[0] = pixels[1] = pixels[2] = tmp_buf;
            } else {
                pixels[0] = vbuf;
                if (encoder->isPAL) {
                    pixels[1] = pixels[0] + PAL_W*PAL_H;
                    pixels[2] = pixels[1] + (PAL_W/2)*(format==2?PAL_H:PAL_H/2);
                } else {
                    pixels[1] = pixels[0] + NTSC_W*NTSC_H;
                    pixels[2] = pixels[1] + (NTSC_W/2)*(format==2?NTSC_H:NTSC_H/2);
                }
            }

            dv_encode_full_frame(encoder, pixels, (format)?e_dv_color_yuv:e_dv_color_rgb, target);
        } //no pass-through
        /* 
         * samples * channels * bits = size;
         * so
         * samples = size / (channels * bits)
         */
        encoder->samples_this_frame = param->size / (chans * sizeof(int16_t));
        dv_encode_metadata(target, encoder->isPAL, encoder->is16x9, &now, 0);
        dv_encode_timecode(target, encoder->isPAL, 0);

#ifdef WORDS_BIGENDIAN
        for (i=0; i<param->size; i+=2) {
	        uint8_t tmp = param->buffer[i];
            param->buffer[i] = param->buffer[i+1];
            param->buffer[i+1] = tmp;
        }
#endif

        // Although dv_encode_full_audio supports 4 channels, the internal
        // PCM data (param->buffer) is only carrying 2 channels so only deal
        // with 1 or 2 channel audio.
        // Work around apparent bug in dv_encode_full_audio when chans == 1
        // by putting silence in 2nd channel and calling with chans = 2
        if (chans == 1) {
            abufs[0] = (int16_t *)param->buffer; /* avoid ac_memcpy */
            memset(abufs[1], 0, DV_AUDIO_MAX_SAMPLES * 2);
            achans = 2;
        } else {
            // assume 2 channel, demultiplex for libdv API
            for(i = 0; i < param->size/4; i++) { // XXX magic number
                abufs[0][i] = ((int16_t *)param->buffer)[i * 2    ];
                abufs[1][i] = ((int16_t *)param->buffer)[i * 2 + 1];
            }
        }
        dv_encode_full_audio(encoder, abufs, achans, rate, target);

        if (tc_pwrite(fd, target, frame_size) != frame_size) {
            tc_log_perror(MOD_NAME, "write frame");
            return TC_ERROR;
        }

        return TC_OK;
    }

    return TC_ERROR;
}

/* ------------------------------------------------------------
 *
 * stop encoder
 *
 * ------------------------------------------------------------*/

MOD_stop
{
    int i;

    if (param->flag == TC_VIDEO) {
        dv_encoder_free(encoder);
        tcv_free(tcvhandle);
        return TC_OK;
    }

    if (param->flag == TC_AUDIO) {
        for(i = 0; i < MAX_CHANNELS; i++)
            tc_free(audio_bufs[i]);
        return TC_OK;
    }

    return TC_ERROR;
}

/* ------------------------------------------------------------
 *
 * close outputfiles
 *
 * ------------------------------------------------------------*/

MOD_close
{
    if(param->flag == TC_VIDEO) {
        close(fd);
        return TC_OK;
    }

    if (param->flag == TC_AUDIO)
        return TC_OK;

    return TC_ERROR;
}