1 /*
2  *  export_ac3.c
3  *
4  *  Copyright (C) Daniel Pittman, 2003, based on export_ogg.c which was:
5  *  Copyright (C) Tilmann Bitterberg, July 2002
6  *
7  *  This file is part of transcode, a video stream processing tool
8  *
9  *  transcode is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2, or (at your option)
12  *  any later version.
13  *
14  *  transcode is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with GNU Make; see the file COPYING.  If not, write to
21  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24 
25 #define MOD_NAME    "export_ac3.so"
26 #define MOD_VERSION "v0.1 (2003-02-26)"
27 #define MOD_CODEC   "(video) null | (audio) ac3"
28 
29 #include "transcode.h"
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 
34 static int   verbose_flag=TC_QUIET;
35 static int   capability_flag=TC_CAP_PCM;
36 
37 #define MOD_PRE ac3
38 #include "export_def.h"
39 static FILE *pFile = NULL;
40 
p_write(char * buf,size_t len)41 static inline int p_write (char *buf, size_t len)
42 {
43     size_t n  = 0;
44     size_t r  = 0;
45     int    fd = fileno (pFile);
46 
47     while (r < len)
48     {
49         if ((n = write (fd, buf + r, len - r)) < 0)
50 	    return n;
51 
52         r += n;
53     }
54 
55     return r;
56 }
57 
58 /* ------------------------------------------------------------
59  *
60  * open codec
61  *
62  * ------------------------------------------------------------*/
63 
64 MOD_open
65 {
66     int result;
67 
68     /* check for ffmpeg */
69     if (tc_test_program("ffmpeg") != 0) return (TC_EXPORT_ERROR);
70 
71     if (param->flag == TC_AUDIO) {
72 	char buf [PATH_MAX];
73 
74 	if (vob->mp3bitrate == 0) {
75             tc_log_warn (MOD_NAME, "Please set the export audio bitrate");
76             return(TC_EXPORT_ERROR);
77         }
78 
79 	if (vob->mp3frequency == 0) {
80             tc_log_warn (MOD_NAME, "Please set the export audio sample rate");
81             return(TC_EXPORT_ERROR);
82         }
83 
84 	tc_log_warn(MOD_NAME, "*** This module is non-optimal ***");
85 	tc_log_warn(MOD_NAME, "*** Use -N 0x2000 instead of -y ...,ac3 (faster) ***");
86 
87         result = tc_snprintf (buf, PATH_MAX,
88                               "ffmpeg -y -f s%dle -ac %d -ar %d -i - -ab %dk -acodec ac3 %s%s",
89                               vob->dm_bits,
90                               vob->dm_chan,
91                               vob->mp3frequency,
92                               vob->mp3bitrate,
93                               vob->audio_out_file,
94                               vob->verbose > 1 ? "" : " >/dev/null 2>&1");
95 
96 	if (result < 0)
97             return(TC_EXPORT_ERROR);
98 
99         if (verbose > 0)
100 	    tc_log_info(MOD_NAME, "%s", buf);
101 
102 	if ((pFile = popen (buf, "w")) == NULL)
103 	    return(TC_EXPORT_ERROR);
104 
105 	return(TC_EXPORT_OK);
106 
107     }
108     if (param->flag == TC_VIDEO)
109 	return(TC_EXPORT_OK);
110 
111     // invalid flag
112     return(TC_EXPORT_ERROR);
113 }
114 
115 /* ------------------------------------------------------------
116  *
117  * init codec
118  *
119  * ------------------------------------------------------------*/
120 
121 MOD_init
122 {
123     if(param->flag == TC_AUDIO)
124     {
125         return(0);
126     }
127 
128     if (param->flag == TC_VIDEO)
129 	return(0);
130 
131     // invalid flag
132     return(TC_EXPORT_ERROR);
133 }
134 
135 
136 /* ------------------------------------------------------------
137  *
138  * encode and export
139  *
140  * ------------------------------------------------------------*/
141 
142 MOD_encode
143 {
144     if(param->flag == TC_AUDIO)
145     {
146         if (p_write (param->buffer, param->size) != param->size)
147         {
148             tc_log_perror(MOD_NAME, "write audio frame");
149             return(TC_EXPORT_ERROR);
150         }
151         return (0);
152     }
153 
154     if (param->flag == TC_VIDEO)
155         return(0);
156 
157     // invalid flag
158     return(TC_EXPORT_ERROR);
159 }
160 
161 
162 /* ------------------------------------------------------------
163  *
164  * stop codec
165  *
166  * ------------------------------------------------------------*/
167 
168 MOD_stop
169 {
170     if (param->flag == TC_VIDEO)
171         return (0);
172 
173     if (param->flag == TC_AUDIO)
174 	return (0);
175 
176     return(TC_EXPORT_ERROR);
177 }
178 
179 
180 /* ------------------------------------------------------------
181  *
182  * close codec
183  *
184  * ------------------------------------------------------------*/
185 
186 MOD_close
187 {
188     if (param->flag == TC_VIDEO)
189 	return (0);
190 
191     if (param->flag == TC_AUDIO)
192     {
193         if (pFile)
194 	  pclose (pFile);
195 
196 	pFile = NULL;
197 
198         return(0);
199     }
200 
201     return (TC_EXPORT_ERROR);
202 }
203 
204 /* vim: sw=4
205  */
206