decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $
  20. */
  21. # include "libmad_config.h"
  22. # include "libmad_global.h"
  23. # ifdef HAVE_SYS_TYPES_H
  24. # include <sys/types.h>
  25. # endif
  26. # ifdef HAVE_SYS_WAIT_H
  27. # include <sys/wait.h>
  28. # endif
  29. # ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. # endif
  32. # ifdef HAVE_FCNTL_H
  33. # include <fcntl.h>
  34. # endif
  35. # include <stdlib.h>
  36. # ifdef HAVE_ERRNO_H
  37. # include <errno.h>
  38. # endif
  39. # include "stream.h"
  40. # include "frame.h"
  41. # include "synth.h"
  42. # include "decoder.h"
  43. /*
  44. * NAME: decoder->init()
  45. * DESCRIPTION: initialize a decoder object with callback routines
  46. */
  47. void mad_decoder_init(struct mad_decoder *decoder, void *data,
  48. enum mad_flow (*input_func)(void *,
  49. struct mad_stream *),
  50. enum mad_flow (*header_func)(void *,
  51. struct mad_header const *),
  52. enum mad_flow (*filter_func)(void *,
  53. struct mad_stream const *,
  54. struct mad_frame *),
  55. enum mad_flow (*output_func)(void *,
  56. struct mad_header const *,
  57. struct mad_pcm *),
  58. enum mad_flow (*error_func)(void *,
  59. struct mad_stream *,
  60. struct mad_frame *),
  61. enum mad_flow (*message_func)(void *,
  62. void *, unsigned int *))
  63. {
  64. decoder->mode = -1;
  65. decoder->options = 0;
  66. decoder->async.pid = 0;
  67. decoder->async.in = -1;
  68. decoder->async.out = -1;
  69. decoder->sync = 0;
  70. decoder->cb_data = data;
  71. decoder->input_func = input_func;
  72. decoder->header_func = header_func;
  73. decoder->filter_func = filter_func;
  74. decoder->output_func = output_func;
  75. decoder->error_func = error_func;
  76. decoder->message_func = message_func;
  77. }
  78. int mad_decoder_finish(struct mad_decoder *decoder)
  79. {
  80. # if defined(USE_ASYNC)
  81. if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
  82. pid_t pid;
  83. int status;
  84. close(decoder->async.in);
  85. do
  86. pid = waitpid(decoder->async.pid, &status, 0);
  87. while (pid == -1 && errno == EINTR);
  88. decoder->mode = -1;
  89. close(decoder->async.out);
  90. decoder->async.pid = 0;
  91. decoder->async.in = -1;
  92. decoder->async.out = -1;
  93. if (pid == -1)
  94. return -1;
  95. return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
  96. }
  97. # endif
  98. return 0;
  99. }
  100. # if defined(USE_ASYNC)
  101. static
  102. enum mad_flow send_io(int fd, void const *data, size_t len)
  103. {
  104. char const *ptr = data;
  105. ssize_t count;
  106. while (len) {
  107. do
  108. count = write(fd, ptr, len);
  109. while (count == -1 && errno == EINTR);
  110. if (count == -1)
  111. return MAD_FLOW_BREAK;
  112. len -= count;
  113. ptr += count;
  114. }
  115. return MAD_FLOW_CONTINUE;
  116. }
  117. static
  118. enum mad_flow receive_io(int fd, void *buffer, size_t len)
  119. {
  120. char *ptr = buffer;
  121. ssize_t count;
  122. while (len) {
  123. do
  124. count = read(fd, ptr, len);
  125. while (count == -1 && errno == EINTR);
  126. if (count == -1)
  127. return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
  128. else if (count == 0)
  129. return MAD_FLOW_STOP;
  130. len -= count;
  131. ptr += count;
  132. }
  133. return MAD_FLOW_CONTINUE;
  134. }
  135. static
  136. enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
  137. {
  138. int flags, blocking;
  139. enum mad_flow result;
  140. flags = fcntl(fd, F_GETFL);
  141. if (flags == -1)
  142. return MAD_FLOW_BREAK;
  143. blocking = flags & ~O_NONBLOCK;
  144. if (blocking != flags &&
  145. fcntl(fd, F_SETFL, blocking) == -1)
  146. return MAD_FLOW_BREAK;
  147. result = receive_io(fd, buffer, len);
  148. if (flags != blocking &&
  149. fcntl(fd, F_SETFL, flags) == -1)
  150. return MAD_FLOW_BREAK;
  151. return result;
  152. }
  153. static
  154. enum mad_flow send(int fd, void const *message, unsigned int size)
  155. {
  156. enum mad_flow result;
  157. /* send size */
  158. result = send_io(fd, &size, sizeof(size));
  159. /* send message */
  160. if (result == MAD_FLOW_CONTINUE)
  161. result = send_io(fd, message, size);
  162. return result;
  163. }
  164. static
  165. enum mad_flow receive(int fd, void **message, unsigned int *size)
  166. {
  167. enum mad_flow result;
  168. unsigned int actual;
  169. if (*message == 0)
  170. *size = 0;
  171. /* receive size */
  172. result = receive_io(fd, &actual, sizeof(actual));
  173. /* receive message */
  174. if (result == MAD_FLOW_CONTINUE) {
  175. if (actual > *size)
  176. actual -= *size;
  177. else {
  178. *size = actual;
  179. actual = 0;
  180. }
  181. if (*size > 0) {
  182. if (*message == 0) {
  183. *message = malloc(*size);
  184. if (*message == 0)
  185. return MAD_FLOW_BREAK;
  186. }
  187. result = receive_io_blocking(fd, *message, *size);
  188. }
  189. /* throw away remainder of message */
  190. while (actual && result == MAD_FLOW_CONTINUE) {
  191. char sink[256];
  192. unsigned int len;
  193. len = actual > sizeof(sink) ? sizeof(sink) : actual;
  194. result = receive_io_blocking(fd, sink, len);
  195. actual -= len;
  196. }
  197. }
  198. return result;
  199. }
  200. static
  201. enum mad_flow check_message(struct mad_decoder *decoder)
  202. {
  203. enum mad_flow result;
  204. void *message = 0;
  205. unsigned int size;
  206. result = receive(decoder->async.in, &message, &size);
  207. if (result == MAD_FLOW_CONTINUE) {
  208. if (decoder->message_func == 0)
  209. size = 0;
  210. else {
  211. result = decoder->message_func(decoder->cb_data, message, &size);
  212. if (result == MAD_FLOW_IGNORE ||
  213. result == MAD_FLOW_BREAK)
  214. size = 0;
  215. }
  216. if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
  217. result = MAD_FLOW_BREAK;
  218. }
  219. if (message)
  220. free(message);
  221. return result;
  222. }
  223. # endif
  224. static
  225. enum mad_flow error_default(void *data, struct mad_stream *stream,
  226. struct mad_frame *frame)
  227. {
  228. int *bad_last_frame = data;
  229. switch (stream->error) {
  230. case MAD_ERROR_BADCRC:
  231. if (*bad_last_frame)
  232. mad_frame_mute(frame);
  233. else
  234. *bad_last_frame = 1;
  235. return MAD_FLOW_IGNORE;
  236. default:
  237. return MAD_FLOW_CONTINUE;
  238. }
  239. }
  240. static
  241. int run_sync(struct mad_decoder *decoder)
  242. {
  243. enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
  244. void *error_data;
  245. int bad_last_frame = 0;
  246. struct mad_stream *stream;
  247. struct mad_frame *frame;
  248. struct mad_synth *synth;
  249. int result = 0;
  250. if (decoder->input_func == 0)
  251. return 0;
  252. if (decoder->error_func) {
  253. error_func = decoder->error_func;
  254. error_data = decoder->cb_data;
  255. }
  256. else {
  257. error_func = error_default;
  258. error_data = &bad_last_frame;
  259. }
  260. stream = &decoder->sync->stream;
  261. frame = &decoder->sync->frame;
  262. synth = &decoder->sync->synth;
  263. mad_stream_init(stream);
  264. mad_frame_init(frame);
  265. mad_synth_init(synth);
  266. mad_stream_options(stream, decoder->options);
  267. do {
  268. switch (decoder->input_func(decoder->cb_data, stream)) {
  269. case MAD_FLOW_STOP:
  270. goto done;
  271. case MAD_FLOW_BREAK:
  272. goto fail;
  273. case MAD_FLOW_IGNORE:
  274. continue;
  275. case MAD_FLOW_CONTINUE:
  276. break;
  277. }
  278. while (1) {
  279. # if defined(USE_ASYNC)
  280. if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
  281. switch (check_message(decoder)) {
  282. case MAD_FLOW_IGNORE:
  283. case MAD_FLOW_CONTINUE:
  284. break;
  285. case MAD_FLOW_BREAK:
  286. goto fail;
  287. case MAD_FLOW_STOP:
  288. goto done;
  289. }
  290. }
  291. # endif
  292. if (decoder->header_func) {
  293. if (mad_header_decode(&frame->header, stream) == -1) {
  294. if (!MAD_RECOVERABLE(stream->error))
  295. break;
  296. switch (error_func(error_data, stream, frame)) {
  297. case MAD_FLOW_STOP:
  298. goto done;
  299. case MAD_FLOW_BREAK:
  300. goto fail;
  301. case MAD_FLOW_IGNORE:
  302. case MAD_FLOW_CONTINUE:
  303. default:
  304. continue;
  305. }
  306. }
  307. switch (decoder->header_func(decoder->cb_data, &frame->header)) {
  308. case MAD_FLOW_STOP:
  309. goto done;
  310. case MAD_FLOW_BREAK:
  311. goto fail;
  312. case MAD_FLOW_IGNORE:
  313. continue;
  314. case MAD_FLOW_CONTINUE:
  315. break;
  316. }
  317. }
  318. if (mad_frame_decode(frame, stream) == -1) {
  319. if (!MAD_RECOVERABLE(stream->error))
  320. break;
  321. switch (error_func(error_data, stream, frame)) {
  322. case MAD_FLOW_STOP:
  323. goto done;
  324. case MAD_FLOW_BREAK:
  325. goto fail;
  326. case MAD_FLOW_IGNORE:
  327. break;
  328. case MAD_FLOW_CONTINUE:
  329. default:
  330. continue;
  331. }
  332. }
  333. else
  334. bad_last_frame = 0;
  335. if (decoder->filter_func) {
  336. switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
  337. case MAD_FLOW_STOP:
  338. goto done;
  339. case MAD_FLOW_BREAK:
  340. goto fail;
  341. case MAD_FLOW_IGNORE:
  342. continue;
  343. case MAD_FLOW_CONTINUE:
  344. break;
  345. }
  346. }
  347. mad_synth_frame(synth, frame);
  348. if (decoder->output_func) {
  349. switch (decoder->output_func(decoder->cb_data,
  350. &frame->header, &synth->pcm)) {
  351. case MAD_FLOW_STOP:
  352. goto done;
  353. case MAD_FLOW_BREAK:
  354. goto fail;
  355. case MAD_FLOW_IGNORE:
  356. case MAD_FLOW_CONTINUE:
  357. break;
  358. }
  359. }
  360. }
  361. }
  362. while (stream->error == MAD_ERROR_BUFLEN);
  363. fail:
  364. result = -1;
  365. done:
  366. mad_synth_finish(synth);
  367. mad_frame_finish(frame);
  368. mad_stream_finish(stream);
  369. return result;
  370. }
  371. # if defined(USE_ASYNC)
  372. static
  373. int run_async(struct mad_decoder *decoder)
  374. {
  375. pid_t pid;
  376. int ptoc[2], ctop[2], flags;
  377. if (pipe(ptoc) == -1)
  378. return -1;
  379. if (pipe(ctop) == -1) {
  380. close(ptoc[0]);
  381. close(ptoc[1]);
  382. return -1;
  383. }
  384. flags = fcntl(ptoc[0], F_GETFL);
  385. if (flags == -1 ||
  386. fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
  387. close(ctop[0]);
  388. close(ctop[1]);
  389. close(ptoc[0]);
  390. close(ptoc[1]);
  391. return -1;
  392. }
  393. pid = fork();
  394. if (pid == -1) {
  395. close(ctop[0]);
  396. close(ctop[1]);
  397. close(ptoc[0]);
  398. close(ptoc[1]);
  399. return -1;
  400. }
  401. decoder->async.pid = pid;
  402. if (pid) {
  403. /* parent */
  404. close(ptoc[0]);
  405. close(ctop[1]);
  406. decoder->async.in = ctop[0];
  407. decoder->async.out = ptoc[1];
  408. return 0;
  409. }
  410. /* child */
  411. close(ptoc[1]);
  412. close(ctop[0]);
  413. decoder->async.in = ptoc[0];
  414. decoder->async.out = ctop[1];
  415. _exit(run_sync(decoder));
  416. /* not reached */
  417. return -1;
  418. }
  419. # endif
  420. /*
  421. * NAME: decoder->run()
  422. * DESCRIPTION: run the decoder thread either synchronously or asynchronously
  423. */
  424. int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
  425. {
  426. int result;
  427. int (*run)(struct mad_decoder *) = 0;
  428. switch (decoder->mode = mode) {
  429. case MAD_DECODER_MODE_SYNC:
  430. run = run_sync;
  431. break;
  432. case MAD_DECODER_MODE_ASYNC:
  433. # if defined(USE_ASYNC)
  434. run = run_async;
  435. # endif
  436. break;
  437. }
  438. if (run == 0)
  439. return -1;
  440. decoder->sync = malloc(sizeof(*decoder->sync));
  441. if (decoder->sync == 0)
  442. return -1;
  443. result = run(decoder);
  444. free(decoder->sync);
  445. decoder->sync = 0;
  446. return result;
  447. }
  448. /*
  449. * NAME: decoder->message()
  450. * DESCRIPTION: send a message to and receive a reply from the decoder process
  451. */
  452. int mad_decoder_message(struct mad_decoder *decoder,
  453. void *message, unsigned int *len)
  454. {
  455. # if defined(USE_ASYNC)
  456. if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
  457. send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
  458. receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
  459. return -1;
  460. return 0;
  461. # else
  462. return -1;
  463. # endif
  464. }