summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/import_dv.c
blob: 934fa7570adac589a6dc81acb2e7f716f22f79f2 (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
/*
 *  import_dv.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    "import_dv.so"
#define MOD_VERSION "v0.3.1 (2003-10-14)"
#define MOD_CODEC   "(video) DV | (audio) PCM"

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

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

#define MOD_PRE dv
#include "import_def.h"


char import_cmd_buf[TC_BUF_MAX];

static int frame_size=0;
static FILE *fd=NULL;
static uint8_t *tmpbuf = NULL;
static int yuv422_mode = 0, width, height;
static TCVHandle tcvhandle = 0;

/* ------------------------------------------------------------
 *
 * open stream
 *
 * ------------------------------------------------------------*/

MOD_open
{

  char cat_buf[TC_BUF_MAX];
  char yuv_buf[16];
  long sret;

  if(param->flag == TC_VIDEO) {

    //directory mode?
    sret = tc_file_check(vob->video_in_file);
    if (sret < 0) {
        return(TC_IMPORT_ERROR);
    }
    if(sret == 1) {
        tc_snprintf(cat_buf, TC_BUF_MAX, "tccat");
    } else {
        if(vob->im_v_string) {
            tc_snprintf(cat_buf, TC_BUF_MAX, "tcextract -x dv %s",
			            vob->im_v_string);
        } else {
            tc_snprintf(cat_buf, TC_BUF_MAX, "tcextract -x dv");
        }
    }

    //yuy2 mode?
    (vob->dv_yuy2_mode) ?
        tc_snprintf(yuv_buf, 16, "-y yuv420p -Y") :
        tc_snprintf(yuv_buf, 16, "-y yuv420p");

    param->fd = NULL;
    yuv422_mode = 0;

    switch(vob->im_v_codec) {

    case CODEC_RGB:

      sret = tc_snprintf(import_cmd_buf, TC_BUF_MAX,
                      "%s -i \"%s\" -d %d | tcdecode -x dv -y rgb -d %d -Q %d",
                      cat_buf, vob->video_in_file, vob->verbose, vob->verbose,
                      vob->quality);
      if (sret < 0)
          return(TC_IMPORT_ERROR);

      // popen
      if((param->fd = popen(import_cmd_buf, "r"))== NULL) {
	return(TC_IMPORT_ERROR);
      }

      break;

    case CODEC_YUV:

      sret = tc_snprintf(import_cmd_buf, TC_BUF_MAX,
			 "%s -i \"%s\" -d %d | tcdecode -x dv %s -d %d -Q %d",
			 cat_buf, vob->video_in_file, vob->verbose, yuv_buf,
			 vob->verbose, vob->quality);
      if (sret < 0)
	return(TC_IMPORT_ERROR);

      // for reading
      frame_size = (vob->im_v_width * vob->im_v_height * 3)/2;

      param->fd = NULL;

      // popen
      if((fd = popen(import_cmd_buf, "r"))== NULL) {
	return(TC_IMPORT_ERROR);
      }

      break;

    case CODEC_YUV422:

      sret = tc_snprintf(import_cmd_buf, TC_BUF_MAX,
			 "%s -i \"%s\" -d %d |"
			 " tcdecode -x dv -y yuy2 -d %d -Q %d",
			 cat_buf, vob->video_in_file, vob->verbose,
			 vob->verbose, vob->quality);
      if (sret < 0)
	return(TC_IMPORT_ERROR);

      // for reading
      frame_size = vob->im_v_width * vob->im_v_height * 2;

      tmpbuf = tc_malloc(frame_size);
      if (!tmpbuf) {
	tc_log_error(MOD_NAME, "out of memory");
	return(TC_IMPORT_ERROR);
      }

      tcvhandle = tcv_init();
      if (!tcvhandle) {
	tc_log_error(MOD_NAME, "tcv_init() failed");
	return(TC_IMPORT_ERROR);
      }

      yuv422_mode = 1;
      width = vob->im_v_width;
      height = vob->im_v_height;

      param->fd = NULL;

      // popen
      if((fd = popen(import_cmd_buf, "r"))== NULL) {
	return(TC_IMPORT_ERROR);
      }

      break;


    case CODEC_RAW:
    case CODEC_RAW_YUV:

      sret = tc_snprintf(import_cmd_buf, TC_BUF_MAX, "%s -i \"%s\" -d %d",
			 cat_buf, vob->video_in_file, vob->verbose);
      if (sret < 0)
	return(TC_IMPORT_ERROR);

      // for reading
      frame_size = (vob->im_v_height==PAL_H) ?
          TC_FRAME_DV_PAL : TC_FRAME_DV_NTSC;

      param->fd = NULL;

      // popen
      if((fd = popen(import_cmd_buf, "r"))== NULL) {
	return(TC_IMPORT_ERROR);
      }

      break;


    default:
      tc_log_warn(MOD_NAME, "invalid import codec request 0x%x",
		      vob->im_v_codec);
      return(TC_IMPORT_ERROR);

    }

    // print out
    if(verbose_flag) tc_log_info(MOD_NAME, "%s", import_cmd_buf);

    return(TC_IMPORT_OK);
  }

  if(param->flag == TC_AUDIO) {

    //directory mode?
    if(tc_file_check(vob->audio_in_file)) {
        tc_snprintf(cat_buf, TC_BUF_MAX, "tccat");
    } else {
        if(vob->im_a_string) {
            tc_snprintf(cat_buf, TC_BUF_MAX, "tcextract -x dv %s",
            			vob->im_a_string);
        } else {
            tc_snprintf(cat_buf, TC_BUF_MAX, "tcextract -x dv");
        }
    }

    sret = tc_snprintf(import_cmd_buf, TC_BUF_MAX,
		       "%s -i \"%s\" -d %d | tcdecode -x dv -y pcm -d %d",
		       cat_buf, vob->audio_in_file, vob->verbose,
		       vob->verbose);
    if (sret < 0)
      return(TC_IMPORT_ERROR);

    // print out
    if(verbose_flag) tc_log_info(MOD_NAME, "%s", import_cmd_buf);

    param->fd = NULL;

    // popen
    if((param->fd = popen(import_cmd_buf, "r"))== NULL) {
	tc_log_perror(MOD_NAME, "popen PCM stream");
	return(TC_IMPORT_ERROR);
    }

    return(TC_IMPORT_OK);
  }

  return(TC_IMPORT_ERROR);

}

/* ------------------------------------------------------------
 *
 * decode  stream
 *
 * ------------------------------------------------------------*/

MOD_decode
{

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

    // video and YUV only
    if(param->flag == TC_VIDEO && frame_size==0) return(TC_IMPORT_ERROR);

    // return true yuv frame size as physical size of video data
    param->size = frame_size;

    if (yuv422_mode) {
        if (fread(tmpbuf, frame_size, 1, fd) !=1)
            return(TC_IMPORT_ERROR);
	tcv_convert(tcvhandle, tmpbuf, param->buffer, width, height,
		    IMG_YUY2, IMG_YUV422P);
    } else {
        if (fread(param->buffer, frame_size, 1, fd) !=1)
            return(TC_IMPORT_ERROR);
    }

    return(TC_IMPORT_OK);
}

/* ------------------------------------------------------------
 *
 * close stream
 *
 * ------------------------------------------------------------*/


MOD_close
{
  if(param->fd != NULL) pclose(param->fd);

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

  if(param->flag == TC_VIDEO) {

    if(fd) pclose(fd);
    fd=NULL;

    if (tcvhandle)
      tcv_free(tcvhandle);
    tcvhandle=0;

    free(tmpbuf);
    tmpbuf=NULL;

    return(TC_IMPORT_OK);

  }

  return(TC_IMPORT_ERROR);
}