Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

582 lines
11 KiB

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