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