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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* multiplex_y4m.c -- pack a yuv420p stream in YUV4MPEG2 format
* and/or a pcm stream in WAVE format
* (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/>.
*/
#include "config.h"
#include "transcode.h"
#include "libtc/optstr.h"
#include "libtc/ratiocodes.h"
#include "libtc/tcmodule-plugin.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#if defined(HAVE_MJPEGTOOLS_INC)
#include "yuv4mpeg.h"
#include "mpegconsts.h"
#else
#include "mjpegtools/yuv4mpeg.h"
#include "mjpegtools/mpegconsts.h"
#endif
#include "avilib/wavlib.h"
#define MOD_NAME "multiplex_y4m.so"
#define MOD_VERSION "v0.0.1 (2006-03-22)"
#define MOD_CAP "write YUV4MPEG2 video and WAVE audio streams"
#define MOD_FEATURES \
TC_MODULE_FEATURE_MULTIPLEX|TC_MODULE_FEATURE_VIDEO|TC_MODULE_FEATURE_AUDIO
#define MOD_FLAGS \
TC_MODULE_FLAG_RECONFIGURABLE
#define YW_VID_EXT "y4m"
#define YW_AUD_EXT "wav"
/*
* 'yw_' prefix is used internally to avoid any name clash
* with mjpegtools's y4m_* routines
*/
/* XXX */
static const char yw_help[] = ""
"Overview:\n"
" this module writes a yuv420p video stream using YUV4MPEG2 format\n"
" and/or a pcm stream using WAVE format.\n"
"Options:\n"
" help produce module overview and options explanations\n";
typedef struct {
int fd_vid;
WAV wav;
y4m_frame_info_t frameinfo;
y4m_stream_info_t streaminfo;
int width;
int height;
} YWPrivateData;
static int yw_inspect(TCModuleInstance *self,
const char *options, const char **value)
{
TC_MODULE_SELF_CHECK(self, "inspect");
if (optstr_lookup(options, "help")) {
*value = yw_help;
}
return TC_OK;
}
static int yw_open_video(YWPrivateData *pd, const char *filename,
vob_t *vob)
{
int asr, ret;
y4m_ratio_t framerate;
y4m_ratio_t asr_rate;
/* avoid fd loss in case of failed configuration */
if (pd->fd_vid == -1) {
pd->fd_vid = open(filename,
O_RDWR|O_CREAT|O_TRUNC,
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (pd->fd_vid == -1) {
tc_log_error(MOD_NAME, "failed to open video stream file '%s'"
" (reason: %s)", filename,
strerror(errno));
return TC_ERROR;
}
}
y4m_init_stream_info(&(pd->streaminfo));
//note: this is the real framerate of the raw stream
framerate = (vob->ex_frc == 0) ?mpeg_conform_framerate(vob->ex_fps)
:mpeg_framerate(vob->ex_frc);
if (framerate.n == 0 && framerate.d == 0) {
framerate.n = vob->ex_fps * 1000;
framerate.d = 1000;
}
asr = (vob->ex_asr < 0) ?vob->im_asr :vob->ex_asr;
tc_asr_code_to_ratio(asr, &asr_rate.n, &asr_rate.d);
y4m_init_stream_info(&(pd->streaminfo));
y4m_si_set_framerate(&(pd->streaminfo), framerate);
if (vob->encode_fields == TC_ENCODE_FIELDS_TOP_FIRST) {
y4m_si_set_interlace(&(pd->streaminfo), Y4M_ILACE_TOP_FIRST);
} else if (vob->encode_fields == TC_ENCODE_FIELDS_BOTTOM_FIRST) {
y4m_si_set_interlace(&(pd->streaminfo), Y4M_ILACE_BOTTOM_FIRST);
} else if (vob->encode_fields == TC_ENCODE_FIELDS_PROGRESSIVE) {
y4m_si_set_interlace(&(pd->streaminfo), Y4M_ILACE_NONE);
}
/* XXX */
y4m_si_set_sampleaspect(&(pd->streaminfo),
y4m_guess_sar(pd->width,
pd->height,
asr_rate));
y4m_si_set_height(&(pd->streaminfo), pd->height);
y4m_si_set_width(&(pd->streaminfo), pd->width);
/* Y4M_CHROMA_420JPEG 4:2:0, H/V centered, for JPEG/MPEG-1 */
/* Y4M_CHROMA_420MPEG2 4:2:0, H cosited, for MPEG-2 */
/* Y4M_CHROMA_420PALDV 4:2:0, alternating Cb/Cr, for PAL-DV */
y4m_si_set_chroma(&(pd->streaminfo), Y4M_CHROMA_420JPEG); // XXX
ret = y4m_write_stream_header(pd->fd_vid, &(pd->streaminfo));
if (ret != Y4M_OK) {
tc_log_warn(MOD_NAME, "failed to write video YUV4MPEG2 header: %s",
y4m_strerr(ret));
return TC_ERROR;
}
return TC_OK;
}
static int yw_open_audio(YWPrivateData *pd, const char *filename,
vob_t *vob)
{
WAVError err;
int rate;
pd->wav = wav_open(filename, WAV_WRITE, &err);
if (!pd->wav) {
tc_log_error(MOD_NAME, "failed to open audio stream file '%s'"
" (reason: %s)", filename,
wav_strerror(err));
return TC_ERROR;
}
rate = (vob->mp3frequency != 0) ?vob->mp3frequency :vob->a_rate;
wav_set_bits(pd->wav, vob->dm_bits);
wav_set_rate(pd->wav, rate);
wav_set_bitrate(pd->wav, vob->dm_chan * rate * vob->dm_bits/8);
wav_set_channels(pd->wav, vob->dm_chan);
return TC_OK;
}
static int yw_configure(TCModuleInstance *self,
const char *options, vob_t *vob)
{
char vid_name[PATH_MAX];
char aud_name[PATH_MAX];
YWPrivateData *pd = NULL;
int ret;
TC_MODULE_SELF_CHECK(self, "configure");
pd = self->userdata;
if (vob->audio_out_file == NULL
|| !strcmp(vob->audio_out_file, "/dev/null")) {
/* use affine names */
tc_snprintf(vid_name, PATH_MAX, "%s.%s",
vob->video_out_file, YW_VID_EXT);
tc_snprintf(aud_name, PATH_MAX, "%s.%s",
vob->video_out_file, YW_AUD_EXT);
} else {
/* copy names verbatim */
strlcpy(vid_name, vob->video_out_file, PATH_MAX);
strlcpy(aud_name, vob->audio_out_file, PATH_MAX);
}
pd->width = vob->ex_v_width;
pd->height = vob->ex_v_height;
ret = yw_open_video(pd, vid_name, vob);
if (ret != TC_OK) {
return ret;
}
ret = yw_open_audio(pd, aud_name, vob);
if (ret != TC_OK) {
return ret;
}
if (vob->verbose >= TC_DEBUG) {
tc_log_info(MOD_NAME, "video output: %s (%s)",
vid_name, (pd->fd_vid == -1) ?"FAILED" :"OK");
tc_log_info(MOD_NAME, "audio output: %s (%s)",
aud_name, (pd->wav == NULL) ?"FAILED" :"OK");
}
return TC_OK;
}
static int yw_stop(TCModuleInstance *self)
{
YWPrivateData *pd = NULL;
int verr, aerr;
TC_MODULE_SELF_CHECK(self, "stop");
pd = self->userdata;
if (pd->fd_vid != -1) {
verr = close(pd->fd_vid);
if (verr) {
tc_log_error(MOD_NAME, "closing video file: %s",
strerror(errno));
return TC_ERROR;
}
y4m_fini_frame_info(&pd->frameinfo);
y4m_fini_stream_info(&(pd->streaminfo));
pd->fd_vid = -1;
}
if (pd->wav != NULL) {
aerr = wav_close(pd->wav);
if (aerr != 0) {
tc_log_error(MOD_NAME, "closing audio file: %s",
wav_strerror(wav_last_error(pd->wav)));
return TC_ERROR;
}
pd->wav = NULL;
}
return TC_OK;
}
static int yw_multiplex(TCModuleInstance *self,
vframe_list_t *vframe, aframe_list_t *aframe)
{
ssize_t w_aud = 0, w_vid = 0;
YWPrivateData *pd = NULL;
TC_MODULE_SELF_CHECK(self, "multiplex");
pd = self->userdata;
if (vframe != NULL && vframe->video_len > 0) {
uint8_t *planes[3];
int ret = 0;
y4m_init_frame_info(&pd->frameinfo);
YUV_INIT_PLANES(planes, vframe->video_buf, IMG_YUV420P,
pd->width, pd->height);
ret = y4m_write_frame(pd->fd_vid, &(pd->streaminfo),
&pd->frameinfo, planes);
if (ret != Y4M_OK) {
tc_log_warn(MOD_NAME, "error while writing video frame: %s",
y4m_strerr(ret));
return TC_ERROR;
}
w_vid = vframe->video_len;
}
if (aframe != NULL && aframe->audio_len > 0) {
w_aud = wav_write_data(pd->wav, aframe->audio_buf, aframe->audio_len);
if (w_aud != aframe->audio_len) {
tc_log_warn(MOD_NAME, "error while writing audio frame: %s",
wav_strerror(wav_last_error(pd->wav)));
return TC_ERROR;
}
}
return (int)(w_vid + w_aud);
}
static int yw_init(TCModuleInstance *self, uint32_t features)
{
YWPrivateData *pd = NULL;
TC_MODULE_SELF_CHECK(self, "init");
TC_MODULE_INIT_CHECK(self, MOD_FEATURES, features);
pd = tc_malloc(sizeof(YWPrivateData));
if (pd == NULL) {
return TC_ERROR;
}
pd->width = 0;
pd->height = 0;
pd->fd_vid = -1;
pd->wav = NULL;
y4m_init_stream_info(&(pd->streaminfo));
/* frameinfo will be initialized at each multiplex call */
if (verbose) {
tc_log_info(MOD_NAME, "%s %s", MOD_VERSION, MOD_CAP);
}
self->userdata = pd;
return TC_OK;
}
static int yw_fini(TCModuleInstance *self)
{
TC_MODULE_SELF_CHECK(self, "fini");
yw_stop(self);
tc_free(self->userdata);
self->userdata = NULL;
return TC_OK;
}
/*************************************************************************/
static const TCCodecID yw_codecs_in[] = { TC_CODEC_YUV420P, TC_CODEC_PCM,
TC_CODEC_ERROR };
/* a multiplexor is at the end of pipeline */
static const TCCodecID yw_codecs_out[] = { TC_CODEC_ERROR };
static const TCFormatID yw_formats_in[] = { TC_FORMAT_ERROR };
static const TCFormatID yw_formats_out[] = {
TC_FORMAT_YUV4MPEG, TC_FORMAT_WAV, TC_FORMAT_ERROR
};
static const TCModuleInfo yw_info = {
.features = MOD_FEATURES,
.flags = MOD_FLAGS,
.name = MOD_NAME,
.version = MOD_VERSION,
.description = MOD_CAP,
.codecs_in = yw_codecs_in,
.codecs_out = yw_codecs_out,
.formats_in = yw_formats_in,
.formats_out = yw_formats_out
};
static const TCModuleClass yw_class = {
TC_MODULE_CLASS_HEAD(yw),
.init = yw_init,
.fini = yw_fini,
.configure = yw_configure,
.stop = yw_stop,
.inspect = yw_inspect,
.multiplex = yw_multiplex,
};
TC_MODULE_ENTRY_POINT(yw);
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|