1 /*
2  *  extract_lzo.c
3  *
4  *  Copyright (C) Tilmann Bitterberg - 2003
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 "transcode.h"
25 #include "libtc/libtc.h"
26 #include "tcinfo.h"
27 
28 #include "ioaux.h"
29 #include "avilib/avilib.h"
30 #include "tc.h"
31 
32 #ifdef HAVE_LZO
33 
34 #include "libtc/tc_lzo.h"
35 
36 #define BUFFER_SIZE SIZE_RGB_FRAME<<1
37 
long2str(long a,unsigned char * b)38 inline static void long2str(long a, unsigned char *b)
39 {
40       b[0] = (a&0xff000000)>>24;
41       b[1] = (a&0x00ff0000)>>16;
42       b[2] = (a&0x0000ff00)>>8;
43       b[3] = (a&0x000000ff);
44 }
45 
extract_lzo(info_t * ipipe)46 void extract_lzo(info_t *ipipe)
47 {
48 
49   avi_t *avifile=NULL;
50   char *video;
51 
52   int key, error=0;
53 
54   long frames, bytes, n;
55 
56   switch(ipipe->magic) {
57 
58   case TC_MAGIC_AVI:
59 
60     // scan file
61     if (ipipe->nav_seek_file) {
62       if(NULL == (avifile = AVI_open_indexfd(ipipe->fd_in,0,ipipe->nav_seek_file))) {
63 	AVI_print_error("AVI open");
64 	import_exit(1);
65       }
66     } else {
67       if(NULL == (avifile = AVI_open_fd(ipipe->fd_in,1))) {
68 	AVI_print_error("AVI open");
69 	import_exit(1);
70       }
71     }
72 
73     // read video info;
74 
75     frames =  AVI_video_frames(avifile);
76     if (ipipe->frame_limit[1] < frames)
77       {
78 	frames=ipipe->frame_limit[1];
79       }
80 
81 
82     if(ipipe->verbose & TC_STATS)
83       tc_log_msg(__FILE__, "%ld video frames", frames);
84 
85     // allocate space, assume max buffer size
86     if((video = tc_zalloc(SIZE_RGB_FRAME))==NULL) {
87       tc_log_msg(__FILE__, "out of memory");
88       error=1;
89       break;
90     }
91 
92     (int)AVI_set_video_position(avifile,ipipe->frame_limit[0]);
93     for (n=ipipe->frame_limit[0]; n<=frames; ++n) {
94       // video
95       if((bytes = AVI_read_frame(avifile, video, &key))<0) {
96 	error=1;
97 	break;
98       }
99       if(tc_pwrite(ipipe->fd_out, video, bytes)!=bytes) {
100 	error=1;
101 	break;
102       }
103     }
104 
105     free(video);
106 
107     break;
108 
109   case TC_MAGIC_RAW:
110   default:
111 
112     if(ipipe->magic == TC_MAGIC_UNKNOWN)
113       tc_log_warn(__FILE__, "no file type specified, assuming %s",
114 		  filetype(TC_MAGIC_RAW));
115 
116     error = tc_preadwrite(ipipe->fd_in, ipipe->fd_out);
117     if (error < 0)
118         error = 1;
119 
120     break;
121   }
122 
123   import_exit(error);
124 }
125 
126 #else
extract_lzo(info_t * ipipe)127 void extract_lzo(info_t *ipipe)
128 {
129     tc_log_error(__FILE__, "No support for LZO configured -- exiting");
130     import_exit(1);
131 }
132 #endif
133 
134