1 /*
2  *  extract_rgb.c
3  *
4  *  Copyright (C) Thomas Oestreich - June 2001
5  *
6  *  This file is part of transcode, a video stream processing tool
7  *
8  *  transcode is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  transcode is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with GNU Make; see the file COPYING.  If not, write to
20  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23 
24 #include <stdint.h>
25 
26 #include "transcode.h"
27 #include "libtc/libtc.h"
28 #include "tcinfo.h"
29 
30 #include "ioaux.h"
31 #include "avilib/avilib.h"
32 #include "tc.h"
33 
34 
35 /* ------------------------------------------------------------
36  *
37  * rgb extract thread
38  *
39  * magic: TC_MAGIC_AVI
40  *        TC_MAGIC_RAW  <-- default
41  *
42  * ------------------------------------------------------------*/
43 
44 
extract_rgb(info_t * ipipe)45 void extract_rgb(info_t *ipipe)
46 {
47     uint8_t *video;
48     avi_t *avifile = NULL;
49     int key, error = 0;
50     long frames, bytes, n;
51 
52     switch (ipipe->magic) {
53       case TC_MAGIC_AVI:
54 	if (ipipe->nav_seek_file) {
55             avifile = AVI_open_indexfd(ipipe->fd_in, 0, ipipe->nav_seek_file);
56         } else {
57             avifile = AVI_open_fd(ipipe->fd_in, 1);
58         }
59         if (NULL == avifile) {
60             AVI_print_error("AVI open");
61             import_exit(1);
62         }
63 
64         frames = AVI_video_frames(avifile);
65         if (ipipe->frame_limit[1] < frames) {
66             frames = ipipe->frame_limit[1];
67         }
68 
69         if (ipipe->verbose & TC_STATS) {
70             tc_log_msg(__FILE__, "%ld video frames", frames);
71         }
72 
73         video = tc_bufalloc(SIZE_RGB_FRAME);
74         if (!video) {
75             error = 1;
76             break;
77         }
78 
79         AVI_set_video_position(avifile, ipipe->frame_limit[0]);
80         /* FIXME: should this be < rather than <= ? */
81         for (n = ipipe->frame_limit[0]; n <= frames; n++) {
82             bytes = AVI_read_frame(avifile, video, &key);
83             if (bytes < 0) {
84                 error = 1;
85                 break;
86             }
87             if (tc_pwrite(ipipe->fd_out, video, bytes) != bytes) {
88                 error = 1;
89                 break;
90             }
91         }
92 
93         tc_buffree(video);
94         break;
95 
96       case TC_MAGIC_RAW: /* fallthrough */
97       default:
98         if (ipipe->magic == TC_MAGIC_UNKNOWN) {
99             tc_log_warn(__FILE__, "no file type specified, assuming %s",
100 			            filetype(TC_MAGIC_RAW));
101 
102             error = tc_preadwrite(ipipe->fd_in, ipipe->fd_out);
103             break;
104         }
105     }
106 
107     if (error) {
108         tc_log_perror(__FILE__, "error while writing data");
109       	import_exit(error);
110     }
111 }
112 
113 /*************************************************************************/
114 
115 /*
116  * Local variables:
117  *   c-file-style: "stroustrup"
118  *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
119  *   indent-tabs-mode: nil
120  * End:
121  *
122  * vim: expandtab shiftwidth=4:
123  */
124