summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/extract_yuv.c
blob: eedbcc5f191fb730090c1eb78226d2057545290f (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
/*
 *  extract_yuv.c
 *
 *  Copyright (C) Thomas Oestreich - June 2001
 *  Copyright (C) Francesco Romani - March 2006
 *
 *  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.
 *
 */

#include "config.h"

#include "transcode.h"
#include "framebuffer.h"
#include "tcinfo.h"
#include "libtc/libtc.h"
#include "libtc/ratiocodes.h"
#include "libtc/tcframes.h"

#include "ioaux.h"
#include "avilib/avilib.h"
#include "tc.h"

#if defined HAVE_MJPEGTOOLS
/* assert using new code (FIXME?) */

#if defined(HAVE_MJPEGTOOLS_INC)
#include "yuv4mpeg.h"
#include "mpegconsts.h"
#else
#include "mjpegtools/yuv4mpeg.h"
#include "mjpegtools/mpegconsts.h"
#endif

/* ------------------------------------------------------------
 *
 * yuv extract thread
 *
 * magic: TC_MAGIC_YUV4MPEG
 *        TC_MAGIC_RAW      <-- default
 *
 *
 * ------------------------------------------------------------*/


static int extract_yuv_y4m(info_t *ipipe)
{
    vframe_list_t *vptr = NULL;
    uint8_t *planes[3];
    int planesize[3];
    int ch_mode, w, h, ret = 0, i = 0, errnum;

    y4m_frame_info_t frameinfo;
    y4m_stream_info_t streaminfo;

    /* initialize stream-information */
    y4m_accept_extensions(1);
    y4m_init_stream_info(&streaminfo);
    y4m_init_frame_info(&frameinfo);
    
    errnum = y4m_read_stream_header(ipipe->fd_in, &streaminfo);
    if (errnum != Y4M_OK) {
        tc_log_error(__FILE__, "Couldn't read YUV4MPEG header: %s!",
                     y4m_strerr (errnum));
        return 1;
    }
    if (y4m_si_get_plane_count(&streaminfo) != 3) {
        tc_log_error(__FILE__, "Only 3-plane formats supported");
        return 1;
    }
    ch_mode =  y4m_si_get_chroma(&streaminfo);
    if (ch_mode != Y4M_CHROMA_420JPEG && ch_mode != Y4M_CHROMA_420MPEG2
      && ch_mode != Y4M_CHROMA_420PALDV) {
        tc_log_error(__FILE__, "sorry, chroma mode `%s' (%i) not supported",
                     y4m_chroma_description(ch_mode), ch_mode);
        return 1;
    }
    
    w = y4m_si_get_width(&streaminfo);
    h = y4m_si_get_height(&streaminfo);
    vptr = tc_new_video_frame(w, h, TC_CODEC_YUV420P, TC_TRUE);

    if (!vptr) {
        tc_log_error(__FILE__, "can't allocate buffer (%ix%i)", w, h);
        return 1;
    }
    planes[0] = vptr->video_buf_Y[0];
    planes[1] = vptr->video_buf_U[0];
    planes[2] = vptr->video_buf_V[0];

    planesize[0] = y4m_si_get_plane_length(&streaminfo, 0);
    planesize[1] = y4m_si_get_plane_length(&streaminfo, 1);
    planesize[2] = y4m_si_get_plane_length(&streaminfo, 2);
    
    while (1) {
        errnum = y4m_read_frame(ipipe->fd_in, &streaminfo, &frameinfo, planes);
        if (errnum != Y4M_OK) {
            break;
        }
        for (i = 0; i < 3; i++) {
            ret = tc_pwrite(ipipe->fd_out, planes[i], planesize[i]);
            if (ret != planesize[i]) {
                tc_log_perror(__FILE__, "error while writing output data");
                break;
            }
        }
    }

    tc_del_video_frame(vptr);
    y4m_fini_frame_info(&frameinfo);
    y4m_fini_stream_info(&streaminfo);

    return 0;
}

static int extract_yuv_avi(info_t *ipipe)
{
    avi_t *avifile=NULL;
    char *video;

    int key;
    long frames, bytes, n;

    if (ipipe->nav_seek_file) {
        avifile = AVI_open_indexfd(ipipe->fd_in, 0, ipipe->nav_seek_file);
    } else {
        avifile = AVI_open_fd(ipipe->fd_in, 1);
    }
    if (NULL == avifile) {
        AVI_print_error("AVI open");
        return 1;
    }

    // read video info;
    frames =  AVI_video_frames(avifile);
    if (ipipe->frame_limit[1] < frames) {
        frames=ipipe->frame_limit[1];
    }

    if (ipipe->verbose & TC_STATS) {
        tc_log_info(__FILE__, "%ld video frames", frames);
    }
    // allocate space, assume max buffer size
    video = tc_bufalloc(SIZE_RGB_FRAME);
    if (video == NULL) {
        tc_log_error(__FILE__, "out of memory");
        return 1;
    }

    AVI_set_video_position(avifile, ipipe->frame_limit[0]);
    for (n = ipipe->frame_limit[0]; n <= frames; ++n) {
        bytes = AVI_read_frame(avifile, video, &key);
        if (bytes < 0) {
            return 1;
        }
        if (tc_pwrite(ipipe->fd_out, video, bytes) != bytes) {
            tc_log_perror(__FILE__, "error while writing output data");
            return 1;
        }
    }
    tc_buffree(video);

    return 0;
}

static int extract_yuv_raw(info_t *ipipe)
{
    if (ipipe->magic == TC_MAGIC_UNKNOWN) {
        tc_log_warn(__FILE__, "no file type specified, assuming (%s)",
                    filetype(TC_MAGIC_RAW));
    }
    return tc_preadwrite(ipipe->fd_in, ipipe->fd_out);
}

void extract_yuv(info_t *ipipe)
{
    int error = 0;
    
    switch(ipipe->magic) {
      case TC_MAGIC_YUV4MPEG:
        error = extract_yuv_y4m(ipipe);
        break;
      case TC_MAGIC_AVI:
        error = extract_yuv_avi(ipipe);
        break;
      case TC_MAGIC_RAW:
      default:
        error = extract_yuv_raw(ipipe);
        break;
    }
    if (error) {
        tc_log_error(__FILE__, "write failed");
        import_exit(error);
    }
}

void probe_yuv(info_t *ipipe)
{
    int errnum = Y4M_OK;
    y4m_frame_info_t frameinfo;
    y4m_stream_info_t streaminfo;
    y4m_ratio_t r;

    /* initialize stream-information */
    y4m_accept_extensions(1);
    y4m_init_stream_info(&streaminfo);
    y4m_init_frame_info(&frameinfo);
    
    errnum = y4m_read_stream_header(ipipe->fd_in, &streaminfo);
    if (errnum != Y4M_OK) {
        tc_log_error(__FILE__, "Couldn't read YUV4MPEG header: %s!",
		     y4m_strerr(errnum));
        import_exit(1);
    }

    ipipe->probe_info->width = y4m_si_get_width(&streaminfo);
    ipipe->probe_info->height = y4m_si_get_height(&streaminfo);
   
    r = y4m_si_get_framerate(&streaminfo);
    ipipe->probe_info->fps = (double)r.n / (double)r.d;
    tc_frc_code_from_ratio(&(ipipe->probe_info->frc), r.n, r.d);

    r = y4m_si_get_sampleaspect(&streaminfo);
    tc_asr_code_from_ratio(&(ipipe->probe_info->asr), r.n, r.d);
   
    ipipe->probe_info->codec=TC_CODEC_YUV420P;
    ipipe->probe_info->magic=TC_MAGIC_YUV4MPEG;

    y4m_fini_frame_info(&frameinfo);
    y4m_fini_stream_info(&streaminfo);
}

#else			/* HAVE_MJPEGTOOLS */

void extract_yuv(info_t *ipipe)
{
    tc_log_error(__FILE__, "No support for YUV4MPEG compiled in.");
    tc_log_error(__FILE__, "Recompile with mjpegtools support enabled.");
    import_exit(1);
}
        
void probe_yuv(info_t * ipipe)
{
    tc_log_error(__FILE__, "No support for YUV4MPEG compiled in.");
    tc_log_error(__FILE__, "Recompile with mjpegtools support enabled.");
    ipipe->probe_info->codec = TC_CODEC_UNKNOWN;
    ipipe->probe_info->magic = TC_MAGIC_UNKNOWN;
}

#endif /* HAVE_MJPEGTOOLS */

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

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